Merge tag 'seccomp-v6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[linux-2.6-block.git] / net / ipv6 / xfrm6_input.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
4 *
5 * Authors:
6 * Mitsuru KANDA @USAGI
67ba4152
IM
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
1da177e4
LT
9 * YOSHIFUJI Hideaki @USAGI
10 * IPv6 support
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
b05e1066
PM
15#include <linux/netfilter.h>
16#include <linux/netfilter_ipv6.h>
1da177e4
LT
17#include <net/ipv6.h>
18#include <net/xfrm.h>
221ddb72
SK
19#include <net/protocol.h>
20#include <net/gro.h>
1da177e4 21
63c43787
ND
22int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
23 struct ip6_tnl *t)
1da177e4 24{
63c43787 25 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
2fcb45b6 26 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
716062fd
HX
27 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
28 return xfrm_input(skb, nexthdr, spi, 0);
29}
30EXPORT_SYMBOL(xfrm6_rcv_spi);
b05e1066 31
acf568ee
HX
32static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
33 struct sk_buff *skb)
34{
0146dca7
SD
35 if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
36 kfree_skb(skb);
37 return NET_RX_DROP;
38 }
39
40 return 0;
acf568ee
HX
41}
42
716062fd
HX
43int xfrm6_transport_finish(struct sk_buff *skb, int async)
44{
7785bba2 45 struct xfrm_offload *xo = xfrm_offload(skb);
e9cba694 46 int nhlen = skb->data - skb_network_header(skb);
7785bba2 47
60d5fcfb
HX
48 skb_network_header(skb)[IP6CB(skb)->nhoff] =
49 XFRM_MODE_SKB_CB(skb)->protocol;
50
0883ae0e
HX
51#ifndef CONFIG_NETFILTER
52 if (!async)
53 return 1;
54#endif
55
e9cba694 56 __skb_push(skb, nhlen);
7c88e21a 57 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
e9cba694 58 skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
b05e1066 59
7785bba2
SK
60 if (xo && (xo->flags & XFRM_GRO)) {
61 skb_mac_header_rebuild(skb);
bfc0698b 62 skb_reset_transport_header(skb);
0146dca7 63 return 0;
7785bba2
SK
64 }
65
29a26a56
EB
66 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
67 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
acf568ee 68 xfrm6_transport_finish2);
0146dca7
SD
69 return 0;
70}
71
221ddb72 72static int __xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
0146dca7
SD
73{
74 struct udp_sock *up = udp_sk(sk);
75 struct udphdr *uh;
76 struct ipv6hdr *ip6h;
77 int len;
78 int ip6hlen = sizeof(struct ipv6hdr);
0146dca7
SD
79 __u8 *udpdata;
80 __be32 *udpdata32;
70a36f57 81 u16 encap_type;
0146dca7 82
70a36f57 83 encap_type = READ_ONCE(up->encap_type);
0146dca7
SD
84 /* if this is not encapsulated socket, then just return now */
85 if (!encap_type)
86 return 1;
87
88 /* If this is a paged skb, make sure we pull up
89 * whatever data we need to look at. */
90 len = skb->len - sizeof(struct udphdr);
91 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
92 return 1;
93
94 /* Now we can get the pointers */
95 uh = udp_hdr(skb);
96 udpdata = (__u8 *)uh + sizeof(struct udphdr);
97 udpdata32 = (__be32 *)udpdata;
98
99 switch (encap_type) {
100 default:
101 case UDP_ENCAP_ESPINUDP:
102 /* Check if this is a keepalive packet. If so, eat it. */
103 if (len == 1 && udpdata[0] == 0xff) {
221ddb72 104 return -EINVAL;
0146dca7
SD
105 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
106 /* ESP Packet without Non-ESP header */
107 len = sizeof(struct udphdr);
108 } else
109 /* Must be an IKE packet.. pass it through */
110 return 1;
111 break;
112 case UDP_ENCAP_ESPINUDP_NON_IKE:
113 /* Check if this is a keepalive packet. If so, eat it. */
114 if (len == 1 && udpdata[0] == 0xff) {
221ddb72 115 return -EINVAL;
0146dca7
SD
116 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
117 udpdata32[0] == 0 && udpdata32[1] == 0) {
118
119 /* ESP Packet with Non-IKE marker */
120 len = sizeof(struct udphdr) + 2 * sizeof(u32);
121 } else
122 /* Must be an IKE packet.. pass it through */
123 return 1;
124 break;
125 }
126
127 /* At this point we are sure that this is an ESPinUDP packet,
128 * so we need to remove 'len' bytes from the packet (the UDP
129 * header and optional ESP marker bytes) and then modify the
130 * protocol to ESP, and then call into the transform receiver.
131 */
132 if (skb_unclone(skb, GFP_ATOMIC))
221ddb72 133 return -EINVAL;
0146dca7
SD
134
135 /* Now we can update and verify the packet length... */
136 ip6h = ipv6_hdr(skb);
137 ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
138 if (skb->len < ip6hlen + len) {
139 /* packet is too small!?! */
221ddb72 140 return -EINVAL;
0146dca7
SD
141 }
142
143 /* pull the data buffer up to the ESP header and set the
144 * transport header to point to ESP. Keep UDP on the stack
145 * for later.
146 */
221ddb72
SK
147 if (pull) {
148 __skb_pull(skb, len);
149 skb_reset_transport_header(skb);
150 } else {
151 skb_set_transport_header(skb, len);
152 }
0146dca7
SD
153
154 /* process ESP */
0146dca7 155 return 0;
1da177e4
LT
156}
157
221ddb72
SK
158/* If it's a keepalive packet, then just eat it.
159 * If it's an encapsulated packet, then pass it to the
160 * IPsec xfrm input.
161 * Returns 0 if skb passed to xfrm or was dropped.
162 * Returns >0 if skb should be passed to UDP.
163 * Returns <0 if skb should be resubmitted (-ret is protocol)
164 */
165int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
166{
167 int ret;
168
53a5b4f2
SK
169 if (skb->protocol == htons(ETH_P_IP))
170 return xfrm4_udp_encap_rcv(sk, skb);
171
221ddb72
SK
172 ret = __xfrm6_udp_encap_rcv(sk, skb, true);
173 if (!ret)
174 return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0,
175 udp_sk(sk)->encap_type);
176
177 if (ret < 0) {
178 kfree_skb(skb);
179 return 0;
180 }
181
182 return ret;
183}
184
185struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
186 struct sk_buff *skb)
187{
188 int offset = skb_gro_offset(skb);
189 const struct net_offload *ops;
190 struct sk_buff *pp = NULL;
191 int ret;
192
53a5b4f2
SK
193 if (skb->protocol == htons(ETH_P_IP))
194 return xfrm4_gro_udp_encap_rcv(sk, head, skb);
195
221ddb72
SK
196 offset = offset - sizeof(struct udphdr);
197
198 if (!pskb_pull(skb, offset))
199 return NULL;
200
201 rcu_read_lock();
202 ops = rcu_dereference(inet6_offloads[IPPROTO_ESP]);
203 if (!ops || !ops->callbacks.gro_receive)
204 goto out;
205
206 ret = __xfrm6_udp_encap_rcv(sk, skb, false);
207 if (ret)
208 goto out;
209
210 skb_push(skb, offset);
211 NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
212
213 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
214 rcu_read_unlock();
215
216 return pp;
217
218out:
219 rcu_read_unlock();
220 skb_push(skb, offset);
221 NAPI_GRO_CB(skb)->same_flow = 0;
222 NAPI_GRO_CB(skb)->flush = 1;
223
224 return NULL;
225}
226
63c43787 227int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
1da177e4 228{
33b5ecb8 229 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
63c43787 230 0, t);
1da177e4 231}
63c43787 232EXPORT_SYMBOL(xfrm6_rcv_tnl);
7159039a 233
63c43787
ND
234int xfrm6_rcv(struct sk_buff *skb)
235{
236 return xfrm6_rcv_tnl(skb, NULL);
237}
238EXPORT_SYMBOL(xfrm6_rcv);
fbd9a5b4
MN
239int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
240 xfrm_address_t *saddr, u8 proto)
241{
59c9940e 242 struct net *net = dev_net(skb->dev);
1ab1457c 243 struct xfrm_state *x = NULL;
0ca64da1 244 struct sec_path *sp;
fbd9a5b4
MN
245 int i = 0;
246
0ca64da1
FW
247 sp = secpath_set(skb);
248 if (!sp) {
b0fcee82
SK
249 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
250 goto drop;
9473e1f6
MN
251 }
252
0ca64da1 253 if (1 + sp->len == XFRM_MAX_DEPTH) {
59c9940e 254 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
9473e1f6
MN
255 goto drop;
256 }
257
fbd9a5b4
MN
258 for (i = 0; i < 3; i++) {
259 xfrm_address_t *dst, *src;
a002c6fd 260
fbd9a5b4
MN
261 switch (i) {
262 case 0:
263 dst = daddr;
264 src = saddr;
265 break;
266 case 1:
267 /* lookup state with wild-card source address */
fbd9a5b4 268 dst = daddr;
a002c6fd 269 src = (xfrm_address_t *)&in6addr_any;
fbd9a5b4 270 break;
fbd9a5b4 271 default:
1ab1457c 272 /* lookup state with wild-card addresses */
a002c6fd
YH
273 dst = (xfrm_address_t *)&in6addr_any;
274 src = (xfrm_address_t *)&in6addr_any;
fbd9a5b4 275 break;
1ab1457c 276 }
fbd9a5b4 277
bd55775c 278 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
fbd9a5b4
MN
279 if (!x)
280 continue;
281
282 spin_lock(&x->lock);
283
a002c6fd
YH
284 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
285 likely(x->km.state == XFRM_STATE_VALID) &&
286 !xfrm_state_check_expire(x)) {
fbd9a5b4 287 spin_unlock(&x->lock);
a002c6fd
YH
288 if (x->type->input(x, skb) > 0) {
289 /* found a valid state */
290 break;
291 }
292 } else
fbd9a5b4 293 spin_unlock(&x->lock);
fbd9a5b4 294
a002c6fd
YH
295 xfrm_state_put(x);
296 x = NULL;
fbd9a5b4
MN
297 }
298
9473e1f6 299 if (!x) {
59c9940e 300 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
afeb14b4 301 xfrm_audit_state_notfound_simple(skb, AF_INET6);
fbd9a5b4 302 goto drop;
fbd9a5b4
MN
303 }
304
2294be0f 305 sp->xvec[sp->len++] = x;
9473e1f6
MN
306
307 spin_lock(&x->lock);
fbd9a5b4 308
9473e1f6
MN
309 x->curlft.bytes += skb->len;
310 x->curlft.packets++;
311
312 spin_unlock(&x->lock);
fbd9a5b4
MN
313
314 return 1;
9473e1f6 315
fbd9a5b4 316drop:
fbd9a5b4
MN
317 return -1;
318}
7159039a 319EXPORT_SYMBOL(xfrm6_input_addr);