12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d1da932eSVlad Yasevich /*
3d1da932eSVlad Yasevich * IPV6 GSO/GRO offload support
4d1da932eSVlad Yasevich * Linux INET6 implementation
5d1da932eSVlad Yasevich */
6d1da932eSVlad Yasevich
7d1da932eSVlad Yasevich #include <linux/kernel.h>
8d1da932eSVlad Yasevich #include <linux/socket.h>
9d1da932eSVlad Yasevich #include <linux/netdevice.h>
10d1da932eSVlad Yasevich #include <linux/skbuff.h>
11c6b641a4SVlad Yasevich #include <linux/printk.h>
12d1da932eSVlad Yasevich
13d1da932eSVlad Yasevich #include <net/protocol.h>
14d1da932eSVlad Yasevich #include <net/ipv6.h>
15b8921ca8STom Herbert #include <net/inet_common.h>
165521d95eSEric Dumazet #include <net/tcp.h>
176db69328SEric Dumazet #include <net/udp.h>
1804f00ab2SLeon Romanovsky #include <net/gro.h>
19d457a0e3SEric Dumazet #include <net/gso.h>
20d1da932eSVlad Yasevich
21d1da932eSVlad Yasevich #include "ip6_offload.h"
22d1da932eSVlad Yasevich
23028e0a47SPaolo Abeni /* All GRO functions are always builtin, except UDP over ipv6, which lays in
24028e0a47SPaolo Abeni * ipv6 module, as it depends on UDPv6 lookup function, so we need special care
25028e0a47SPaolo Abeni * when ipv6 is built as a module
26028e0a47SPaolo Abeni */
27028e0a47SPaolo Abeni #if IS_BUILTIN(CONFIG_IPV6)
28028e0a47SPaolo Abeni #define INDIRECT_CALL_L4(f, f2, f1, ...) INDIRECT_CALL_2(f, f2, f1, __VA_ARGS__)
29028e0a47SPaolo Abeni #else
30028e0a47SPaolo Abeni #define INDIRECT_CALL_L4(f, f2, f1, ...) INDIRECT_CALL_1(f, f2, __VA_ARGS__)
31028e0a47SPaolo Abeni #endif
32028e0a47SPaolo Abeni
33028e0a47SPaolo Abeni #define indirect_call_gro_receive_l4(f2, f1, cb, head, skb) \
34028e0a47SPaolo Abeni ({ \
35028e0a47SPaolo Abeni unlikely(gro_recursion_inc_test(skb)) ? \
36028e0a47SPaolo Abeni NAPI_GRO_CB(skb)->flush |= 1, NULL : \
37028e0a47SPaolo Abeni INDIRECT_CALL_L4(cb, f2, f1, head, skb); \
38028e0a47SPaolo Abeni })
39028e0a47SPaolo Abeni
ipv6_gro_pull_exthdrs(struct sk_buff * skb,int off,int proto)407f7b0ebbSRichard Gobert static int ipv6_gro_pull_exthdrs(struct sk_buff *skb, int off, int proto)
417f7b0ebbSRichard Gobert {
427f7b0ebbSRichard Gobert const struct net_offload *ops = NULL;
437f7b0ebbSRichard Gobert struct ipv6_opt_hdr *opth;
447f7b0ebbSRichard Gobert
457f7b0ebbSRichard Gobert for (;;) {
467f7b0ebbSRichard Gobert int len;
477f7b0ebbSRichard Gobert
487f7b0ebbSRichard Gobert ops = rcu_dereference(inet6_offloads[proto]);
497f7b0ebbSRichard Gobert
507f7b0ebbSRichard Gobert if (unlikely(!ops))
517f7b0ebbSRichard Gobert break;
527f7b0ebbSRichard Gobert
537f7b0ebbSRichard Gobert if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
547f7b0ebbSRichard Gobert break;
557f7b0ebbSRichard Gobert
567f7b0ebbSRichard Gobert opth = skb_gro_header(skb, off + sizeof(*opth), off);
577f7b0ebbSRichard Gobert if (unlikely(!opth))
587f7b0ebbSRichard Gobert break;
597f7b0ebbSRichard Gobert
607f7b0ebbSRichard Gobert len = ipv6_optlen(opth);
617f7b0ebbSRichard Gobert
627f7b0ebbSRichard Gobert opth = skb_gro_header(skb, off + len, off);
637f7b0ebbSRichard Gobert if (unlikely(!opth))
647f7b0ebbSRichard Gobert break;
657f7b0ebbSRichard Gobert proto = opth->nexthdr;
667f7b0ebbSRichard Gobert
677f7b0ebbSRichard Gobert off += len;
687f7b0ebbSRichard Gobert }
697f7b0ebbSRichard Gobert
707f7b0ebbSRichard Gobert skb_gro_pull(skb, off - skb_network_offset(skb));
717f7b0ebbSRichard Gobert return proto;
727f7b0ebbSRichard Gobert }
737f7b0ebbSRichard Gobert
ipv6_gso_pull_exthdrs(struct sk_buff * skb,int proto)74d1da932eSVlad Yasevich static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
75d1da932eSVlad Yasevich {
76d1da932eSVlad Yasevich const struct net_offload *ops = NULL;
77d1da932eSVlad Yasevich
78d1da932eSVlad Yasevich for (;;) {
79d1da932eSVlad Yasevich struct ipv6_opt_hdr *opth;
80d1da932eSVlad Yasevich int len;
81d1da932eSVlad Yasevich
82d1da932eSVlad Yasevich if (proto != NEXTHDR_HOP) {
83d1da932eSVlad Yasevich ops = rcu_dereference(inet6_offloads[proto]);
84d1da932eSVlad Yasevich
85d1da932eSVlad Yasevich if (unlikely(!ops))
86d1da932eSVlad Yasevich break;
87d1da932eSVlad Yasevich
88d1da932eSVlad Yasevich if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
89d1da932eSVlad Yasevich break;
90d1da932eSVlad Yasevich }
91d1da932eSVlad Yasevich
92d1da932eSVlad Yasevich if (unlikely(!pskb_may_pull(skb, 8)))
93d1da932eSVlad Yasevich break;
94d1da932eSVlad Yasevich
95d1da932eSVlad Yasevich opth = (void *)skb->data;
96d1da932eSVlad Yasevich len = ipv6_optlen(opth);
97d1da932eSVlad Yasevich
98d1da932eSVlad Yasevich if (unlikely(!pskb_may_pull(skb, len)))
99d1da932eSVlad Yasevich break;
100d1da932eSVlad Yasevich
101fc6fb41cSLi RongQing opth = (void *)skb->data;
102d1da932eSVlad Yasevich proto = opth->nexthdr;
103d1da932eSVlad Yasevich __skb_pull(skb, len);
104d1da932eSVlad Yasevich }
105d1da932eSVlad Yasevich
106d1da932eSVlad Yasevich return proto;
107d1da932eSVlad Yasevich }
108d1da932eSVlad Yasevich
ipv6_gso_segment(struct sk_buff * skb,netdev_features_t features)109d1da932eSVlad Yasevich static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
110d1da932eSVlad Yasevich netdev_features_t features)
111d1da932eSVlad Yasevich {
112d1da932eSVlad Yasevich struct sk_buff *segs = ERR_PTR(-EINVAL);
113d1da932eSVlad Yasevich struct ipv6hdr *ipv6h;
114d1da932eSVlad Yasevich const struct net_offload *ops;
11589300468SCoco Li int proto, err;
116d1da932eSVlad Yasevich struct frag_hdr *fptr;
117802ab55aSAlexander Duyck unsigned int payload_len;
118d1da932eSVlad Yasevich u8 *prevhdr;
119d1da932eSVlad Yasevich int offset = 0;
12091a48a2eSHannes Frederic Sowa bool encap, udpfrag;
121d3e5e006SEric Dumazet int nhoff;
12207b26c94SSteffen Klassert bool gso_partial;
123d1da932eSVlad Yasevich
124d3e5e006SEric Dumazet skb_reset_network_header(skb);
12589300468SCoco Li err = ipv6_hopopt_jumbo_remove(skb);
12689300468SCoco Li if (err)
12709f3d1a3SEric Dumazet return ERR_PTR(err);
128d3e5e006SEric Dumazet nhoff = skb_network_header(skb) - skb_mac_header(skb);
129d1da932eSVlad Yasevich if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
130d1da932eSVlad Yasevich goto out;
131d1da932eSVlad Yasevich
13291a48a2eSHannes Frederic Sowa encap = SKB_GSO_CB(skb)->encap_level > 0;
13391a48a2eSHannes Frederic Sowa if (encap)
1341e16aa3dSFlorian Westphal features &= skb->dev->hw_enc_features;
135d3e5e006SEric Dumazet SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
136d3e5e006SEric Dumazet
137d1da932eSVlad Yasevich ipv6h = ipv6_hdr(skb);
138d1da932eSVlad Yasevich __skb_pull(skb, sizeof(*ipv6h));
139d1da932eSVlad Yasevich segs = ERR_PTR(-EPROTONOSUPPORT);
140d1da932eSVlad Yasevich
141d1da932eSVlad Yasevich proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
142b917eb15SEric Dumazet
14391a48a2eSHannes Frederic Sowa if (skb->encapsulation &&
1447e13318dSTom Herbert skb_shinfo(skb)->gso_type & (SKB_GSO_IPXIP4 | SKB_GSO_IPXIP6))
145ee80d1ebSWillem de Bruijn udpfrag = proto == IPPROTO_UDP && encap &&
146ee80d1ebSWillem de Bruijn (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
14791a48a2eSHannes Frederic Sowa else
148ee80d1ebSWillem de Bruijn udpfrag = proto == IPPROTO_UDP && !skb->encapsulation &&
149ee80d1ebSWillem de Bruijn (skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
15091a48a2eSHannes Frederic Sowa
151d1da932eSVlad Yasevich ops = rcu_dereference(inet6_offloads[proto]);
152f191a1d1SVlad Yasevich if (likely(ops && ops->callbacks.gso_segment)) {
153d1da932eSVlad Yasevich skb_reset_transport_header(skb);
154f191a1d1SVlad Yasevich segs = ops->callbacks.gso_segment(skb, features);
155cc20ccedSTao Liu if (!segs)
156cc20ccedSTao Liu skb->network_header = skb_mac_header(skb) + nhoff - skb->head;
157d1da932eSVlad Yasevich }
158d1da932eSVlad Yasevich
1596b6ebb6bSArtem Savkov if (IS_ERR_OR_NULL(segs))
160d1da932eSVlad Yasevich goto out;
161d1da932eSVlad Yasevich
16207b26c94SSteffen Klassert gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
16307b26c94SSteffen Klassert
164d1da932eSVlad Yasevich for (skb = segs; skb; skb = skb->next) {
165d3e5e006SEric Dumazet ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
1663d0241d5SAlexey Kodanev if (gso_partial && skb_is_gso(skb))
167802ab55aSAlexander Duyck payload_len = skb_shinfo(skb)->gso_size +
168802ab55aSAlexander Duyck SKB_GSO_CB(skb)->data_offset +
169802ab55aSAlexander Duyck skb->head - (unsigned char *)(ipv6h + 1);
170802ab55aSAlexander Duyck else
171802ab55aSAlexander Duyck payload_len = skb->len - nhoff - sizeof(*ipv6h);
172802ab55aSAlexander Duyck ipv6h->payload_len = htons(payload_len);
173d3e5e006SEric Dumazet skb->network_header = (u8 *)ipv6h - skb->head;
174c56cae23SToke Høiland-Jørgensen skb_reset_mac_len(skb);
175d3e5e006SEric Dumazet
17691a48a2eSHannes Frederic Sowa if (udpfrag) {
1777dd7eb95SDavid S. Miller int err = ip6_find_1stfragopt(skb, &prevhdr);
178e3e86b51SDavid S. Miller if (err < 0) {
179e3e86b51SDavid S. Miller kfree_skb_list(segs);
1807dd7eb95SDavid S. Miller return ERR_PTR(err);
181e3e86b51SDavid S. Miller }
1827dd7eb95SDavid S. Miller fptr = (struct frag_hdr *)((u8 *)ipv6h + err);
183d1da932eSVlad Yasevich fptr->frag_off = htons(offset);
18453b24b8fSIan Morris if (skb->next)
185d1da932eSVlad Yasevich fptr->frag_off |= htons(IP6_MF);
186d1da932eSVlad Yasevich offset += (ntohs(ipv6h->payload_len) -
187d1da932eSVlad Yasevich sizeof(struct frag_hdr));
188d1da932eSVlad Yasevich }
18991a48a2eSHannes Frederic Sowa if (encap)
19091a48a2eSHannes Frederic Sowa skb_reset_inner_headers(skb);
191d1da932eSVlad Yasevich }
192d1da932eSVlad Yasevich
193d1da932eSVlad Yasevich out:
194d1da932eSVlad Yasevich return segs;
195d1da932eSVlad Yasevich }
196d1da932eSVlad Yasevich
197299603e8SJerry Chu /* Return the total length of all the extension hdrs, following the same
198299603e8SJerry Chu * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
199299603e8SJerry Chu */
ipv6_exthdrs_len(struct ipv6hdr * iph,const struct net_offload ** opps)200299603e8SJerry Chu static int ipv6_exthdrs_len(struct ipv6hdr *iph,
201299603e8SJerry Chu const struct net_offload **opps)
202299603e8SJerry Chu {
203810c23a3SJerry Chu struct ipv6_opt_hdr *opth = (void *)iph;
204810c23a3SJerry Chu int len = 0, proto, optlen = sizeof(*iph);
205299603e8SJerry Chu
206299603e8SJerry Chu proto = iph->nexthdr;
207299603e8SJerry Chu for (;;) {
208299603e8SJerry Chu if (proto != NEXTHDR_HOP) {
209299603e8SJerry Chu *opps = rcu_dereference(inet6_offloads[proto]);
210299603e8SJerry Chu if (unlikely(!(*opps)))
211299603e8SJerry Chu break;
212299603e8SJerry Chu if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
213299603e8SJerry Chu break;
214299603e8SJerry Chu }
215f52d81dcSHannes Frederic Sowa opth = (void *)opth + optlen;
216810c23a3SJerry Chu optlen = ipv6_optlen(opth);
217299603e8SJerry Chu len += optlen;
218299603e8SJerry Chu proto = opth->nexthdr;
219299603e8SJerry Chu }
220299603e8SJerry Chu return len;
221299603e8SJerry Chu }
222299603e8SJerry Chu
ipv6_gro_receive(struct list_head * head,struct sk_buff * skb)223aaa5d90bSPaolo Abeni INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
224d1da932eSVlad Yasevich struct sk_buff *skb)
225d1da932eSVlad Yasevich {
226d1da932eSVlad Yasevich const struct net_offload *ops;
227d4546c25SDavid Miller struct sk_buff *pp = NULL;
228d1da932eSVlad Yasevich struct sk_buff *p;
229d1da932eSVlad Yasevich struct ipv6hdr *iph;
230d1da932eSVlad Yasevich unsigned int nlen;
231d1da932eSVlad Yasevich unsigned int hlen;
232d1da932eSVlad Yasevich unsigned int off;
233bf5a755fSJerry Chu u16 flush = 1;
234d1da932eSVlad Yasevich int proto;
235d1da932eSVlad Yasevich
236d1da932eSVlad Yasevich off = skb_gro_offset(skb);
237d1da932eSVlad Yasevich hlen = off + sizeof(*iph);
238cb628a9aSRichard Gobert iph = skb_gro_header(skb, hlen, off);
239d1da932eSVlad Yasevich if (unlikely(!iph))
240d1da932eSVlad Yasevich goto out;
241d1da932eSVlad Yasevich
242299603e8SJerry Chu skb_set_network_header(skb, off);
243*af276a5aSRichard Gobert NAPI_GRO_CB(skb)->inner_network_offset = off;
244d1da932eSVlad Yasevich
2457f7b0ebbSRichard Gobert flush += ntohs(iph->payload_len) != skb->len - hlen;
246d1da932eSVlad Yasevich
247d1da932eSVlad Yasevich proto = iph->nexthdr;
248d1da932eSVlad Yasevich ops = rcu_dereference(inet6_offloads[proto]);
249f191a1d1SVlad Yasevich if (!ops || !ops->callbacks.gro_receive) {
2507f7b0ebbSRichard Gobert proto = ipv6_gro_pull_exthdrs(skb, hlen, proto);
251d1da932eSVlad Yasevich
252d1da932eSVlad Yasevich ops = rcu_dereference(inet6_offloads[proto]);
253f191a1d1SVlad Yasevich if (!ops || !ops->callbacks.gro_receive)
254fc1ca334SEric Dumazet goto out;
255d1da932eSVlad Yasevich
2567f7b0ebbSRichard Gobert iph = skb_gro_network_header(skb);
2577f7b0ebbSRichard Gobert } else {
2587f7b0ebbSRichard Gobert skb_gro_pull(skb, sizeof(*iph));
259d1da932eSVlad Yasevich }
260d1da932eSVlad Yasevich
2617f7b0ebbSRichard Gobert skb_set_transport_header(skb, skb_gro_offset(skb));
2627f7b0ebbSRichard Gobert
263d1da932eSVlad Yasevich NAPI_GRO_CB(skb)->proto = proto;
264d1da932eSVlad Yasevich
265d1da932eSVlad Yasevich flush--;
266d1da932eSVlad Yasevich nlen = skb_network_header_len(skb);
267d1da932eSVlad Yasevich
268d4546c25SDavid Miller list_for_each_entry(p, head, list) {
269d1da932eSVlad Yasevich const struct ipv6hdr *iph2;
270d1da932eSVlad Yasevich __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
271d1da932eSVlad Yasevich
272d1da932eSVlad Yasevich if (!NAPI_GRO_CB(p)->same_flow)
273d1da932eSVlad Yasevich continue;
274d1da932eSVlad Yasevich
275299603e8SJerry Chu iph2 = (struct ipv6hdr *)(p->data + off);
276d1da932eSVlad Yasevich first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
277d1da932eSVlad Yasevich
278299603e8SJerry Chu /* All fields must match except length and Traffic Class.
279299603e8SJerry Chu * XXX skbs on the gro_list have all been parsed and pulled
280299603e8SJerry Chu * already so we don't need to compare nlen
281299603e8SJerry Chu * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
2820b215b97SEric Dumazet * memcmp() alone below is sufficient, right?
283299603e8SJerry Chu */
284299603e8SJerry Chu if ((first_word & htonl(0xF00FFFFF)) ||
2850b215b97SEric Dumazet !ipv6_addr_equal(&iph->saddr, &iph2->saddr) ||
2860b215b97SEric Dumazet !ipv6_addr_equal(&iph->daddr, &iph2->daddr) ||
2876fc2f383SJakub Kicinski iph->nexthdr != iph2->nexthdr) {
2880b215b97SEric Dumazet not_same_flow:
289d1da932eSVlad Yasevich NAPI_GRO_CB(p)->same_flow = 0;
290d1da932eSVlad Yasevich continue;
291d1da932eSVlad Yasevich }
2920b215b97SEric Dumazet if (unlikely(nlen > sizeof(struct ipv6hdr))) {
2930b215b97SEric Dumazet if (memcmp(iph + 1, iph2 + 1,
2940b215b97SEric Dumazet nlen - sizeof(struct ipv6hdr)))
2950b215b97SEric Dumazet goto not_same_flow;
2960b215b97SEric Dumazet }
297d1da932eSVlad Yasevich /* flush if Traffic Class fields are different */
2986fc2f383SJakub Kicinski NAPI_GRO_CB(p)->flush |= !!((first_word & htonl(0x0FF00000)) |
2996fc2f383SJakub Kicinski (__force __be32)(iph->hop_limit ^ iph2->hop_limit));
300d1da932eSVlad Yasevich NAPI_GRO_CB(p)->flush |= flush;
30103d56daaSTom Herbert
3021530545eSAlexander Duyck /* If the previous IP ID value was based on an atomic
3031530545eSAlexander Duyck * datagram we can overwrite the value and ignore it.
3041530545eSAlexander Duyck */
3051530545eSAlexander Duyck if (NAPI_GRO_CB(skb)->is_atomic)
30603d56daaSTom Herbert NAPI_GRO_CB(p)->flush_id = 0;
307d1da932eSVlad Yasevich }
308d1da932eSVlad Yasevich
3091530545eSAlexander Duyck NAPI_GRO_CB(skb)->is_atomic = true;
310d1da932eSVlad Yasevich NAPI_GRO_CB(skb)->flush |= flush;
311d1da932eSVlad Yasevich
3124de462abSEric Dumazet skb_gro_postpull_rcsum(skb, iph, nlen);
313d1da932eSVlad Yasevich
314028e0a47SPaolo Abeni pp = indirect_call_gro_receive_l4(tcp6_gro_receive, udp6_gro_receive,
315028e0a47SPaolo Abeni ops->callbacks.gro_receive, head, skb);
316d1da932eSVlad Yasevich
317d1da932eSVlad Yasevich out:
3185f114163SSteffen Klassert skb_gro_flush_final(skb, pp, flush);
319d1da932eSVlad Yasevich
320d1da932eSVlad Yasevich return pp;
321d1da932eSVlad Yasevich }
322d1da932eSVlad Yasevich
sit_ip6ip6_gro_receive(struct list_head * head,struct sk_buff * skb)323d4546c25SDavid Miller static struct sk_buff *sit_ip6ip6_gro_receive(struct list_head *head,
324fac8e0f5SJesse Gross struct sk_buff *skb)
325fac8e0f5SJesse Gross {
326815d22e5STom Herbert /* Common GRO receive for SIT and IP6IP6 */
327815d22e5STom Herbert
328fac8e0f5SJesse Gross if (NAPI_GRO_CB(skb)->encap_mark) {
329fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->flush = 1;
330fac8e0f5SJesse Gross return NULL;
331fac8e0f5SJesse Gross }
332fac8e0f5SJesse Gross
333fac8e0f5SJesse Gross NAPI_GRO_CB(skb)->encap_mark = 1;
334fac8e0f5SJesse Gross
335fac8e0f5SJesse Gross return ipv6_gro_receive(head, skb);
336fac8e0f5SJesse Gross }
337fac8e0f5SJesse Gross
ip4ip6_gro_receive(struct list_head * head,struct sk_buff * skb)338d4546c25SDavid Miller static struct sk_buff *ip4ip6_gro_receive(struct list_head *head,
339b8921ca8STom Herbert struct sk_buff *skb)
340b8921ca8STom Herbert {
341b8921ca8STom Herbert /* Common GRO receive for SIT and IP6IP6 */
342b8921ca8STom Herbert
343b8921ca8STom Herbert if (NAPI_GRO_CB(skb)->encap_mark) {
344b8921ca8STom Herbert NAPI_GRO_CB(skb)->flush = 1;
345b8921ca8STom Herbert return NULL;
346b8921ca8STom Herbert }
347b8921ca8STom Herbert
348b8921ca8STom Herbert NAPI_GRO_CB(skb)->encap_mark = 1;
349b8921ca8STom Herbert
350b8921ca8STom Herbert return inet_gro_receive(head, skb);
351b8921ca8STom Herbert }
352b8921ca8STom Herbert
ipv6_gro_complete(struct sk_buff * skb,int nhoff)353aaa5d90bSPaolo Abeni INDIRECT_CALLABLE_SCOPE int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
354d1da932eSVlad Yasevich {
355d1da932eSVlad Yasevich const struct net_offload *ops;
35681fbc812SEric Dumazet struct ipv6hdr *iph;
357d1da932eSVlad Yasevich int err = -ENOSYS;
35881fbc812SEric Dumazet u32 payload_len;
359d1da932eSVlad Yasevich
360294acf1cSPaolo Abeni if (skb->encapsulation) {
361294acf1cSPaolo Abeni skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IPV6));
362feec0cb3SEric Dumazet skb_set_inner_network_header(skb, nhoff);
363294acf1cSPaolo Abeni }
364feec0cb3SEric Dumazet
36581fbc812SEric Dumazet payload_len = skb->len - nhoff - sizeof(*iph);
36681fbc812SEric Dumazet if (unlikely(payload_len > IPV6_MAXPLEN)) {
36781fbc812SEric Dumazet struct hop_jumbo_hdr *hop_jumbo;
36881fbc812SEric Dumazet int hoplen = sizeof(*hop_jumbo);
36981fbc812SEric Dumazet
37081fbc812SEric Dumazet /* Move network header left */
37181fbc812SEric Dumazet memmove(skb_mac_header(skb) - hoplen, skb_mac_header(skb),
37281fbc812SEric Dumazet skb->transport_header - skb->mac_header);
37381fbc812SEric Dumazet skb->data -= hoplen;
37481fbc812SEric Dumazet skb->len += hoplen;
37581fbc812SEric Dumazet skb->mac_header -= hoplen;
37681fbc812SEric Dumazet skb->network_header -= hoplen;
37781fbc812SEric Dumazet iph = (struct ipv6hdr *)(skb->data + nhoff);
37881fbc812SEric Dumazet hop_jumbo = (struct hop_jumbo_hdr *)(iph + 1);
37981fbc812SEric Dumazet
38081fbc812SEric Dumazet /* Build hop-by-hop options */
38181fbc812SEric Dumazet hop_jumbo->nexthdr = iph->nexthdr;
38281fbc812SEric Dumazet hop_jumbo->hdrlen = 0;
38381fbc812SEric Dumazet hop_jumbo->tlv_type = IPV6_TLV_JUMBO;
38481fbc812SEric Dumazet hop_jumbo->tlv_len = 4;
38581fbc812SEric Dumazet hop_jumbo->jumbo_payload_len = htonl(payload_len + hoplen);
38681fbc812SEric Dumazet
38781fbc812SEric Dumazet iph->nexthdr = NEXTHDR_HOP;
38881fbc812SEric Dumazet iph->payload_len = 0;
38981fbc812SEric Dumazet } else {
39081fbc812SEric Dumazet iph = (struct ipv6hdr *)(skb->data + nhoff);
39181fbc812SEric Dumazet iph->payload_len = htons(payload_len);
39281fbc812SEric Dumazet }
393d1da932eSVlad Yasevich
394299603e8SJerry Chu nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
395f191a1d1SVlad Yasevich if (WARN_ON(!ops || !ops->callbacks.gro_complete))
396627b94f7SEric Dumazet goto out;
397d1da932eSVlad Yasevich
398028e0a47SPaolo Abeni err = INDIRECT_CALL_L4(ops->callbacks.gro_complete, tcp6_gro_complete,
399028e0a47SPaolo Abeni udp6_gro_complete, skb, nhoff);
400d1da932eSVlad Yasevich
401627b94f7SEric Dumazet out:
402d1da932eSVlad Yasevich return err;
403d1da932eSVlad Yasevich }
404d1da932eSVlad Yasevich
sit_gro_complete(struct sk_buff * skb,int nhoff)405feec0cb3SEric Dumazet static int sit_gro_complete(struct sk_buff *skb, int nhoff)
406feec0cb3SEric Dumazet {
407feec0cb3SEric Dumazet skb->encapsulation = 1;
4087e13318dSTom Herbert skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
409feec0cb3SEric Dumazet return ipv6_gro_complete(skb, nhoff);
410feec0cb3SEric Dumazet }
411feec0cb3SEric Dumazet
ip6ip6_gro_complete(struct sk_buff * skb,int nhoff)412815d22e5STom Herbert static int ip6ip6_gro_complete(struct sk_buff *skb, int nhoff)
413815d22e5STom Herbert {
414815d22e5STom Herbert skb->encapsulation = 1;
415815d22e5STom Herbert skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
416815d22e5STom Herbert return ipv6_gro_complete(skb, nhoff);
417815d22e5STom Herbert }
418815d22e5STom Herbert
ip4ip6_gro_complete(struct sk_buff * skb,int nhoff)419b8921ca8STom Herbert static int ip4ip6_gro_complete(struct sk_buff *skb, int nhoff)
420b8921ca8STom Herbert {
421b8921ca8STom Herbert skb->encapsulation = 1;
422b8921ca8STom Herbert skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP6;
423b8921ca8STom Herbert return inet_gro_complete(skb, nhoff);
424b8921ca8STom Herbert }
425b8921ca8STom Herbert
426d1da932eSVlad Yasevich static struct packet_offload ipv6_packet_offload __read_mostly = {
427d1da932eSVlad Yasevich .type = cpu_to_be16(ETH_P_IPV6),
428f191a1d1SVlad Yasevich .callbacks = {
429d1da932eSVlad Yasevich .gso_segment = ipv6_gso_segment,
430d1da932eSVlad Yasevich .gro_receive = ipv6_gro_receive,
431d1da932eSVlad Yasevich .gro_complete = ipv6_gro_complete,
432f191a1d1SVlad Yasevich },
433d1da932eSVlad Yasevich };
434d1da932eSVlad Yasevich
sit_gso_segment(struct sk_buff * skb,netdev_features_t features)435418e897eSWillem de Bruijn static struct sk_buff *sit_gso_segment(struct sk_buff *skb,
436418e897eSWillem de Bruijn netdev_features_t features)
437418e897eSWillem de Bruijn {
438418e897eSWillem de Bruijn if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
439418e897eSWillem de Bruijn return ERR_PTR(-EINVAL);
440418e897eSWillem de Bruijn
441418e897eSWillem de Bruijn return ipv6_gso_segment(skb, features);
442418e897eSWillem de Bruijn }
443418e897eSWillem de Bruijn
ip4ip6_gso_segment(struct sk_buff * skb,netdev_features_t features)444418e897eSWillem de Bruijn static struct sk_buff *ip4ip6_gso_segment(struct sk_buff *skb,
445418e897eSWillem de Bruijn netdev_features_t features)
446418e897eSWillem de Bruijn {
447418e897eSWillem de Bruijn if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
448418e897eSWillem de Bruijn return ERR_PTR(-EINVAL);
449418e897eSWillem de Bruijn
450418e897eSWillem de Bruijn return inet_gso_segment(skb, features);
451418e897eSWillem de Bruijn }
452418e897eSWillem de Bruijn
ip6ip6_gso_segment(struct sk_buff * skb,netdev_features_t features)453418e897eSWillem de Bruijn static struct sk_buff *ip6ip6_gso_segment(struct sk_buff *skb,
454418e897eSWillem de Bruijn netdev_features_t features)
455418e897eSWillem de Bruijn {
456418e897eSWillem de Bruijn if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP6))
457418e897eSWillem de Bruijn return ERR_PTR(-EINVAL);
458418e897eSWillem de Bruijn
459418e897eSWillem de Bruijn return ipv6_gso_segment(skb, features);
460418e897eSWillem de Bruijn }
461418e897eSWillem de Bruijn
46261c1db7fSEric Dumazet static const struct net_offload sit_offload = {
46361c1db7fSEric Dumazet .callbacks = {
464418e897eSWillem de Bruijn .gso_segment = sit_gso_segment,
465815d22e5STom Herbert .gro_receive = sit_ip6ip6_gro_receive,
466feec0cb3SEric Dumazet .gro_complete = sit_gro_complete,
46761c1db7fSEric Dumazet },
46861c1db7fSEric Dumazet };
46961c1db7fSEric Dumazet
470b8921ca8STom Herbert static const struct net_offload ip4ip6_offload = {
471b8921ca8STom Herbert .callbacks = {
472418e897eSWillem de Bruijn .gso_segment = ip4ip6_gso_segment,
473b8921ca8STom Herbert .gro_receive = ip4ip6_gro_receive,
474b8921ca8STom Herbert .gro_complete = ip4ip6_gro_complete,
475b8921ca8STom Herbert },
476b8921ca8STom Herbert };
477b8921ca8STom Herbert
478815d22e5STom Herbert static const struct net_offload ip6ip6_offload = {
479815d22e5STom Herbert .callbacks = {
480418e897eSWillem de Bruijn .gso_segment = ip6ip6_gso_segment,
481815d22e5STom Herbert .gro_receive = sit_ip6ip6_gro_receive,
482815d22e5STom Herbert .gro_complete = ip6ip6_gro_complete,
483815d22e5STom Herbert },
484815d22e5STom Herbert };
ipv6_offload_init(void)485c6b641a4SVlad Yasevich static int __init ipv6_offload_init(void)
486d1da932eSVlad Yasevich {
487c6b641a4SVlad Yasevich
488c6b641a4SVlad Yasevich if (tcpv6_offload_init() < 0)
489c6b641a4SVlad Yasevich pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
490c6b641a4SVlad Yasevich if (ipv6_exthdrs_offload_init() < 0)
491c6b641a4SVlad Yasevich pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
492c6b641a4SVlad Yasevich
493d1da932eSVlad Yasevich dev_add_offload(&ipv6_packet_offload);
49461c1db7fSEric Dumazet
49561c1db7fSEric Dumazet inet_add_offload(&sit_offload, IPPROTO_IPV6);
496815d22e5STom Herbert inet6_add_offload(&ip6ip6_offload, IPPROTO_IPV6);
497b8921ca8STom Herbert inet6_add_offload(&ip4ip6_offload, IPPROTO_IPIP);
49861c1db7fSEric Dumazet
499c6b641a4SVlad Yasevich return 0;
500d1da932eSVlad Yasevich }
501d1da932eSVlad Yasevich
502c6b641a4SVlad Yasevich fs_initcall(ipv6_offload_init);
503