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