xref: /linux/net/ipv6/ip6_offload.c (revision b3827c91cc9979fe04d99e016fb9c5f6260f29a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPV6 GSO/GRO offload support
4  *	Linux INET6 implementation
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/socket.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/printk.h>
12 
13 #include <net/protocol.h>
14 #include <net/ipv6.h>
15 #include <net/inet_common.h>
16 #include <net/tcp.h>
17 #include <net/udp.h>
18 #include <net/gro.h>
19 #include <net/gso.h>
20 
21 #include "ip6_offload.h"
22 #include "tcpv6_offload.c"
23 
24 static int ipv6_gro_pull_exthdrs(struct sk_buff *skb, int off, int proto)
25 {
26 	const struct net_offload *ops = NULL;
27 	struct ipv6_opt_hdr *opth;
28 
29 	for (;;) {
30 		int len;
31 
32 		ops = rcu_dereference(inet6_offloads[proto]);
33 
34 		if (unlikely(!ops))
35 			break;
36 
37 		if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
38 			break;
39 
40 		opth = skb_gro_header(skb, off + sizeof(*opth), off);
41 		if (unlikely(!opth))
42 			break;
43 
44 		len = ipv6_optlen(opth);
45 
46 		opth = skb_gro_header(skb, off + len, off);
47 		if (unlikely(!opth))
48 			break;
49 		proto = opth->nexthdr;
50 
51 		off += len;
52 	}
53 
54 	skb_gro_pull(skb, off - skb_gro_receive_network_offset(skb));
55 	return proto;
56 }
57 
58 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
59 {
60 	const struct net_offload *ops = NULL;
61 
62 	for (;;) {
63 		struct ipv6_opt_hdr *opth;
64 		int len;
65 
66 		ops = rcu_dereference(inet6_offloads[proto]);
67 
68 		if (unlikely(!ops))
69 			break;
70 
71 		if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
72 			break;
73 
74 		if (unlikely(!pskb_may_pull(skb, 8)))
75 			break;
76 
77 		opth = (void *)skb->data;
78 		len = ipv6_optlen(opth);
79 
80 		if (unlikely(!pskb_may_pull(skb, len)))
81 			break;
82 
83 		opth = (void *)skb->data;
84 		proto = opth->nexthdr;
85 		__skb_pull(skb, len);
86 	}
87 
88 	return proto;
89 }
90 
91 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
92 	netdev_features_t features)
93 {
94 	struct sk_buff *segs = ERR_PTR(-EINVAL);
95 	struct ipv6hdr *ipv6h;
96 	const struct net_offload *ops;
97 	int proto, err;
98 	struct frag_hdr *fptr;
99 	unsigned int payload_len;
100 	u8 *prevhdr;
101 	int offset = 0;
102 	bool encap, udpfrag;
103 	int nhoff;
104 	bool gso_partial;
105 
106 	skb_reset_network_header(skb);
107 	err = ipv6_hopopt_jumbo_remove(skb);
108 	if (err)
109 		return ERR_PTR(err);
110 	nhoff = skb_network_header(skb) - skb_mac_header(skb);
111 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
112 		goto out;
113 
114 	encap = SKB_GSO_CB(skb)->encap_level > 0;
115 	if (encap)
116 		features &= skb->dev->hw_enc_features;
117 	SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
118 
119 	ipv6h = ipv6_hdr(skb);
120 	__skb_pull(skb, sizeof(*ipv6h));
121 	segs = ERR_PTR(-EPROTONOSUPPORT);
122 
123 	proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
124 
125 	if (skb->encapsulation &&
126 	    skb_shinfo(skb)->gso_type & (SKB_GSO_IPXIP4 | SKB_GSO_IPXIP6))
127 		udpfrag = proto == IPPROTO_UDP && encap &&
128 			  (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
129 	else
130 		udpfrag = proto == IPPROTO_UDP && !skb->encapsulation &&
131 			  (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
132 
133 	ops = rcu_dereference(inet6_offloads[proto]);
134 	if (likely(ops && ops->callbacks.gso_segment)) {
135 		if (!skb_reset_transport_header_careful(skb))
136 			goto out;
137 
138 		segs = ops->callbacks.gso_segment(skb, features);
139 		if (!segs)
140 			skb->network_header = skb_mac_header(skb) + nhoff - skb->head;
141 	}
142 
143 	if (IS_ERR_OR_NULL(segs))
144 		goto out;
145 
146 	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
147 
148 	for (skb = segs; skb; skb = skb->next) {
149 		ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
150 		if (gso_partial && skb_is_gso(skb))
151 			payload_len = skb_shinfo(skb)->gso_size +
152 				      SKB_GSO_CB(skb)->data_offset +
153 				      skb->head - (unsigned char *)(ipv6h + 1);
154 		else
155 			payload_len = skb->len - nhoff - sizeof(*ipv6h);
156 		ipv6h->payload_len = htons(payload_len);
157 		skb->network_header = (u8 *)ipv6h - skb->head;
158 		skb_reset_mac_len(skb);
159 
160 		if (udpfrag) {
161 			int err = ip6_find_1stfragopt(skb, &prevhdr);
162 			if (err < 0) {
163 				kfree_skb_list(segs);
164 				return ERR_PTR(err);
165 			}
166 			fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
167 			fptr->frag_off = htons(offset);
168 			if (skb->next)
169 				fptr->frag_off |= htons(IP6_MF);
170 			offset += (ntohs(ipv6h->payload_len) -
171 				   sizeof(struct frag_hdr));
172 		}
173 		if (encap)
174 			skb_reset_inner_headers(skb);
175 	}
176 
177 out:
178 	return segs;
179 }
180 
181 /* Return the total length of all the extension hdrs, following the same
182  * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
183  */
184 static int ipv6_exthdrs_len(struct ipv6hdr *iph,
185 			    const struct net_offload **opps)
186 {
187 	struct ipv6_opt_hdr *opth = (void *)iph;
188 	int len = 0, proto, optlen = sizeof(*iph);
189 
190 	proto = iph->nexthdr;
191 	for (;;) {
192 		*opps = rcu_dereference(inet6_offloads[proto]);
193 		if (unlikely(!(*opps)))
194 			break;
195 		if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
196 			break;
197 
198 		opth = (void *)opth + optlen;
199 		optlen = ipv6_optlen(opth);
200 		len += optlen;
201 		proto = opth->nexthdr;
202 	}
203 	return len;
204 }
205 
206 INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
207 							 struct sk_buff *skb)
208 {
209 	const struct net_offload *ops;
210 	struct sk_buff *pp = NULL;
211 	struct sk_buff *p;
212 	struct ipv6hdr *iph;
213 	unsigned int nlen;
214 	unsigned int hlen;
215 	unsigned int off;
216 	u16 flush = 1;
217 	int proto;
218 
219 	off = skb_gro_offset(skb);
220 	hlen = off + sizeof(*iph);
221 	iph = skb_gro_header(skb, hlen, off);
222 	if (unlikely(!iph))
223 		goto out;
224 
225 	NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark] = off;
226 
227 	flush += ntohs(iph->payload_len) != skb->len - hlen;
228 
229 	proto = iph->nexthdr;
230 	ops = rcu_dereference(inet6_offloads[proto]);
231 	if (!ops || !ops->callbacks.gro_receive) {
232 		proto = ipv6_gro_pull_exthdrs(skb, hlen, proto);
233 
234 		ops = rcu_dereference(inet6_offloads[proto]);
235 		if (!ops || !ops->callbacks.gro_receive)
236 			goto out;
237 
238 		iph = skb_gro_network_header(skb);
239 	} else {
240 		skb_gro_pull(skb, sizeof(*iph));
241 	}
242 
243 	skb_set_transport_header(skb, skb_gro_offset(skb));
244 
245 	NAPI_GRO_CB(skb)->proto = proto;
246 
247 	flush--;
248 	nlen = skb_gro_offset(skb) - off;
249 
250 	list_for_each_entry(p, head, list) {
251 		const struct ipv6hdr *iph2;
252 		__be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
253 
254 		if (!NAPI_GRO_CB(p)->same_flow)
255 			continue;
256 
257 		iph2 = (struct ipv6hdr *)(p->data + off);
258 		first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
259 
260 		/* All fields must match except length and Traffic Class.
261 		 * XXX skbs on the gro_list have all been parsed and pulled
262 		 * already so we don't need to compare nlen
263 		 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
264 		 * memcmp() alone below is sufficient, right?
265 		 */
266 		 if ((first_word & htonl(0xF00FFFFF)) ||
267 		     !ipv6_addr_equal(&iph->saddr, &iph2->saddr) ||
268 		     !ipv6_addr_equal(&iph->daddr, &iph2->daddr) ||
269 		     iph->nexthdr != iph2->nexthdr) {
270 not_same_flow:
271 			NAPI_GRO_CB(p)->same_flow = 0;
272 			continue;
273 		}
274 		if (unlikely(nlen > sizeof(struct ipv6hdr))) {
275 			if (memcmp(iph + 1, iph2 + 1,
276 				   nlen - sizeof(struct ipv6hdr)))
277 				goto not_same_flow;
278 		}
279 	}
280 
281 	NAPI_GRO_CB(skb)->flush |= flush;
282 
283 	skb_gro_postpull_rcsum(skb, iph, nlen);
284 
285 	if (unlikely(gro_recursion_inc_test(skb))) {
286 		flush = 1;
287 		goto out;
288 	}
289 
290 	if (likely(proto == IPPROTO_TCP))
291 		pp = tcp6_gro_receive(head, skb);
292 #if IS_BUILTIN(CONFIG_IPV6)
293 	else if (likely(proto == IPPROTO_UDP))
294 		pp = udp6_gro_receive(head, skb);
295 #endif
296 	else
297 		pp = ops->callbacks.gro_receive(head, skb);
298 out:
299 	skb_gro_flush_final(skb, pp, flush);
300 
301 	return pp;
302 }
303 
304 static struct sk_buff *sit_ip6ip6_gro_receive(struct list_head *head,
305 					      struct sk_buff *skb)
306 {
307 	/* Common GRO receive for SIT and IP6IP6 */
308 
309 	if (NAPI_GRO_CB(skb)->encap_mark) {
310 		NAPI_GRO_CB(skb)->flush = 1;
311 		return NULL;
312 	}
313 
314 	NAPI_GRO_CB(skb)->encap_mark = 1;
315 
316 	return ipv6_gro_receive(head, skb);
317 }
318 
319 static struct sk_buff *ip4ip6_gro_receive(struct list_head *head,
320 					  struct sk_buff *skb)
321 {
322 	/* Common GRO receive for SIT and IP6IP6 */
323 
324 	if (NAPI_GRO_CB(skb)->encap_mark) {
325 		NAPI_GRO_CB(skb)->flush = 1;
326 		return NULL;
327 	}
328 
329 	NAPI_GRO_CB(skb)->encap_mark = 1;
330 
331 	return inet_gro_receive(head, skb);
332 }
333 
334 INDIRECT_CALLABLE_SCOPE int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
335 {
336 	const struct net_offload *ops;
337 	struct ipv6hdr *iph;
338 	int err = -ENOSYS;
339 	u32 payload_len;
340 
341 	if (skb->encapsulation) {
342 		skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
343 		skb_set_inner_network_header(skb, nhoff);
344 	}
345 
346 	payload_len = skb->len - nhoff - sizeof(*iph);
347 	if (unlikely(payload_len > IPV6_MAXPLEN)) {
348 		struct hop_jumbo_hdr *hop_jumbo;
349 		int hoplen = sizeof(*hop_jumbo);
350 
351 		/* Move network header left */
352 		memmove(skb_mac_header(skb) - hoplen, skb_mac_header(skb),
353 			skb->transport_header - skb->mac_header);
354 		skb->data -= hoplen;
355 		skb->len += hoplen;
356 		skb->mac_header -= hoplen;
357 		skb->network_header -= hoplen;
358 		iph = (struct ipv6hdr *)(skb->data + nhoff);
359 		hop_jumbo = (struct hop_jumbo_hdr *)(iph + 1);
360 
361 		/* Build hop-by-hop options */
362 		hop_jumbo->nexthdr = iph->nexthdr;
363 		hop_jumbo->hdrlen = 0;
364 		hop_jumbo->tlv_type = IPV6_TLV_JUMBO;
365 		hop_jumbo->tlv_len = 4;
366 		hop_jumbo->jumbo_payload_len = htonl(payload_len + hoplen);
367 
368 		iph->nexthdr = NEXTHDR_HOP;
369 		iph->payload_len = 0;
370 	} else {
371 		iph = (struct ipv6hdr *)(skb->data + nhoff);
372 		iph->payload_len = htons(payload_len);
373 	}
374 
375 	nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
376 
377 	if (likely(ops == &net_hotdata.tcpv6_offload))
378 		return tcp6_gro_complete(skb, nhoff);
379 #if IS_BUILTIN(CONFIG_IPV6)
380 	if (ops == &net_hotdata.udpv6_offload)
381 		return udp6_gro_complete(skb, nhoff);
382 #endif
383 
384 	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
385 		goto out;
386 
387 	err = ops->callbacks.gro_complete(skb, nhoff);
388 
389 out:
390 	return err;
391 }
392 
393 static int sit_gro_complete(struct sk_buff *skb, int nhoff)
394 {
395 	skb->encapsulation = 1;
396 	skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
397 	return ipv6_gro_complete(skb, nhoff);
398 }
399 
400 static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
401 {
402 	skb->encapsulation = 1;
403 	skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
404 	return ipv6_gro_complete(skb, nhoff);
405 }
406 
407 static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
408 {
409 	skb->encapsulation = 1;
410 	skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
411 	return inet_gro_complete(skb, nhoff);
412 }
413 
414 
415 static struct sk_buff *sit_gso_segment(struct sk_buff *skb,
416 				       netdev_features_t features)
417 {
418 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
419 		return ERR_PTR(-EINVAL);
420 
421 	return ipv6_gso_segment(skb, features);
422 }
423 
424 static struct sk_buff *ip4ip6_gso_segment(struct sk_buff *skb,
425 					  netdev_features_t features)
426 {
427 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
428 		return ERR_PTR(-EINVAL);
429 
430 	return inet_gso_segment(skb, features);
431 }
432 
433 static struct sk_buff *ip6ip6_gso_segment(struct sk_buff *skb,
434 					  netdev_features_t features)
435 {
436 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
437 		return ERR_PTR(-EINVAL);
438 
439 	return ipv6_gso_segment(skb, features);
440 }
441 
442 static const struct net_offload sit_offload = {
443 	.callbacks = {
444 		.gso_segment	= sit_gso_segment,
445 		.gro_receive    = sit_ip6ip6_gro_receive,
446 		.gro_complete   = sit_gro_complete,
447 	},
448 };
449 
450 static const struct net_offload ip4ip6_offload = {
451 	.callbacks = {
452 		.gso_segment	= ip4ip6_gso_segment,
453 		.gro_receive    = ip4ip6_gro_receive,
454 		.gro_complete   = ip4ip6_gro_complete,
455 	},
456 };
457 
458 static const struct net_offload ip6ip6_offload = {
459 	.callbacks = {
460 		.gso_segment	= ip6ip6_gso_segment,
461 		.gro_receive    = sit_ip6ip6_gro_receive,
462 		.gro_complete   = ip6ip6_gro_complete,
463 	},
464 };
465 static int __init ipv6_offload_init(void)
466 {
467 
468 	if (tcpv6_offload_init() < 0)
469 		pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
470 	if (ipv6_exthdrs_offload_init() < 0)
471 		pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
472 
473 	net_hotdata.ipv6_packet_offload = (struct packet_offload) {
474 		.type = cpu_to_be16(ETH_P_IPV6),
475 		.callbacks = {
476 			.gso_segment = ipv6_gso_segment,
477 			.gro_receive = ipv6_gro_receive,
478 			.gro_complete = ipv6_gro_complete,
479 		},
480 	};
481 	dev_add_offload(&net_hotdata.ipv6_packet_offload);
482 
483 	inet_add_offload(&sit_offload, IPPROTO_IPV6);
484 	inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
485 	inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
486 
487 	return 0;
488 }
489 
490 fs_initcall(ipv6_offload_init);
491