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