blob: 787e55f4796cf5a8772b18dd8338692fa82bee21 [file] [log] [blame]
Vlad Yasevichd1da9322012-11-15 08:49:16 +00001/*
2 * IPV6 GSO/GRO offload support
3 * Linux INET6 implementation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
11#include <linux/kernel.h>
12#include <linux/socket.h>
13#include <linux/netdevice.h>
14#include <linux/skbuff.h>
Vlad Yasevichc6b641a2012-11-15 08:49:22 +000015#include <linux/printk.h>
Vlad Yasevichd1da9322012-11-15 08:49:16 +000016
17#include <net/protocol.h>
18#include <net/ipv6.h>
19
20#include "ip6_offload.h"
21
22static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
23{
24 const struct net_offload *ops = NULL;
25
26 for (;;) {
27 struct ipv6_opt_hdr *opth;
28 int len;
29
30 if (proto != NEXTHDR_HOP) {
31 ops = rcu_dereference(inet6_offloads[proto]);
32
33 if (unlikely(!ops))
34 break;
35
36 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
37 break;
38 }
39
40 if (unlikely(!pskb_may_pull(skb, 8)))
41 break;
42
43 opth = (void *)skb->data;
44 len = ipv6_optlen(opth);
45
46 if (unlikely(!pskb_may_pull(skb, len)))
47 break;
48
Li RongQingfc6fb412014-10-18 17:27:42 +080049 opth = (void *)skb->data;
Vlad Yasevichd1da9322012-11-15 08:49:16 +000050 proto = opth->nexthdr;
51 __skb_pull(skb, len);
52 }
53
54 return proto;
55}
56
Vlad Yasevichd1da9322012-11-15 08:49:16 +000057static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
58 netdev_features_t features)
59{
60 struct sk_buff *segs = ERR_PTR(-EINVAL);
61 struct ipv6hdr *ipv6h;
62 const struct net_offload *ops;
63 int proto;
64 struct frag_hdr *fptr;
65 unsigned int unfrag_ip6hlen;
Alexander Duyck802ab552016-04-10 21:45:03 -040066 unsigned int payload_len;
Vlad Yasevichd1da9322012-11-15 08:49:16 +000067 u8 *prevhdr;
68 int offset = 0;
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010069 bool encap, udpfrag;
Eric Dumazetd3e5e002013-10-20 20:47:29 -070070 int nhoff;
Vlad Yasevichd1da9322012-11-15 08:49:16 +000071
Eric Dumazetd3e5e002013-10-20 20:47:29 -070072 skb_reset_network_header(skb);
73 nhoff = skb_network_header(skb) - skb_mac_header(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +000074 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
75 goto out;
76
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010077 encap = SKB_GSO_CB(skb)->encap_level > 0;
78 if (encap)
Florian Westphal1e16aa32014-10-20 13:49:16 +020079 features &= skb->dev->hw_enc_features;
Eric Dumazetd3e5e002013-10-20 20:47:29 -070080 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
81
Vlad Yasevichd1da9322012-11-15 08:49:16 +000082 ipv6h = ipv6_hdr(skb);
83 __skb_pull(skb, sizeof(*ipv6h));
84 segs = ERR_PTR(-EPROTONOSUPPORT);
85
86 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
Eric Dumazetb917eb12013-10-18 14:43:55 -070087
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010088 if (skb->encapsulation &&
Tom Herbert7e133182016-05-18 09:06:10 -070089 skb_shinfo(skb)->gso_type & (SKB_GSO_IPXIP4 | SKB_GSO_IPXIP6))
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010090 udpfrag = proto == IPPROTO_UDP && encap;
91 else
92 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
93
Vlad Yasevichd1da9322012-11-15 08:49:16 +000094 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +000095 if (likely(ops && ops->callbacks.gso_segment)) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +000096 skb_reset_transport_header(skb);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +000097 segs = ops->callbacks.gso_segment(skb, features);
Vlad Yasevichd1da9322012-11-15 08:49:16 +000098 }
Vlad Yasevichd1da9322012-11-15 08:49:16 +000099
100 if (IS_ERR(segs))
101 goto out;
102
103 for (skb = segs; skb; skb = skb->next) {
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700104 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
Alexander Duyck802ab552016-04-10 21:45:03 -0400105 if (skb_is_gso(skb))
106 payload_len = skb_shinfo(skb)->gso_size +
107 SKB_GSO_CB(skb)->data_offset +
108 skb->head - (unsigned char *)(ipv6h + 1);
109 else
110 payload_len = skb->len - nhoff - sizeof(*ipv6h);
111 ipv6h->payload_len = htons(payload_len);
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700112 skb->network_header = (u8 *)ipv6h - skb->head;
113
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100114 if (udpfrag) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000115 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700116 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000117 fptr->frag_off = htons(offset);
Ian Morris53b24b82015-03-29 14:00:05 +0100118 if (skb->next)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000119 fptr->frag_off |= htons(IP6_MF);
120 offset += (ntohs(ipv6h->payload_len) -
121 sizeof(struct frag_hdr));
122 }
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100123 if (encap)
124 skb_reset_inner_headers(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000125 }
126
127out:
128 return segs;
129}
130
Jerry Chu299603e82013-12-11 20:53:45 -0800131/* Return the total length of all the extension hdrs, following the same
132 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
133 */
134static int ipv6_exthdrs_len(struct ipv6hdr *iph,
135 const struct net_offload **opps)
136{
Jerry Chu810c23a2013-12-15 18:48:07 -0800137 struct ipv6_opt_hdr *opth = (void *)iph;
138 int len = 0, proto, optlen = sizeof(*iph);
Jerry Chu299603e82013-12-11 20:53:45 -0800139
140 proto = iph->nexthdr;
141 for (;;) {
142 if (proto != NEXTHDR_HOP) {
143 *opps = rcu_dereference(inet6_offloads[proto]);
144 if (unlikely(!(*opps)))
145 break;
146 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
147 break;
148 }
Jerry Chu810c23a2013-12-15 18:48:07 -0800149 opth = (void *)opth + optlen;
150 optlen = ipv6_optlen(opth);
Jerry Chu299603e82013-12-11 20:53:45 -0800151 len += optlen;
152 proto = opth->nexthdr;
153 }
154 return len;
155}
156
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000157static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
158 struct sk_buff *skb)
159{
160 const struct net_offload *ops;
161 struct sk_buff **pp = NULL;
162 struct sk_buff *p;
163 struct ipv6hdr *iph;
164 unsigned int nlen;
165 unsigned int hlen;
166 unsigned int off;
Jerry Chubf5a7552014-01-07 10:23:19 -0800167 u16 flush = 1;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000168 int proto;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000169
170 off = skb_gro_offset(skb);
171 hlen = off + sizeof(*iph);
172 iph = skb_gro_header_fast(skb, off);
173 if (skb_gro_header_hard(skb, hlen)) {
174 iph = skb_gro_header_slow(skb, hlen, off);
175 if (unlikely(!iph))
176 goto out;
177 }
178
Jerry Chu299603e82013-12-11 20:53:45 -0800179 skb_set_network_header(skb, off);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000180 skb_gro_pull(skb, sizeof(*iph));
181 skb_set_transport_header(skb, skb_gro_offset(skb));
182
183 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
184
185 rcu_read_lock();
186 proto = iph->nexthdr;
187 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000188 if (!ops || !ops->callbacks.gro_receive) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000189 __pskb_pull(skb, skb_gro_offset(skb));
190 proto = ipv6_gso_pull_exthdrs(skb, proto);
191 skb_gro_pull(skb, -skb_transport_offset(skb));
192 skb_reset_transport_header(skb);
193 __skb_push(skb, skb_gro_offset(skb));
194
195 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000196 if (!ops || !ops->callbacks.gro_receive)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000197 goto out_unlock;
198
199 iph = ipv6_hdr(skb);
200 }
201
202 NAPI_GRO_CB(skb)->proto = proto;
203
204 flush--;
205 nlen = skb_network_header_len(skb);
206
207 for (p = *head; p; p = p->next) {
208 const struct ipv6hdr *iph2;
209 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
210
211 if (!NAPI_GRO_CB(p)->same_flow)
212 continue;
213
Jerry Chu299603e82013-12-11 20:53:45 -0800214 iph2 = (struct ipv6hdr *)(p->data + off);
Ian Morris67ba4152014-08-24 21:53:10 +0100215 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000216
Jerry Chu299603e82013-12-11 20:53:45 -0800217 /* All fields must match except length and Traffic Class.
218 * XXX skbs on the gro_list have all been parsed and pulled
219 * already so we don't need to compare nlen
220 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
221 * memcmp() alone below is suffcient, right?
222 */
223 if ((first_word & htonl(0xF00FFFFF)) ||
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000224 memcmp(&iph->nexthdr, &iph2->nexthdr,
225 nlen - offsetof(struct ipv6hdr, nexthdr))) {
226 NAPI_GRO_CB(p)->same_flow = 0;
227 continue;
228 }
229 /* flush if Traffic Class fields are different */
230 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
231 NAPI_GRO_CB(p)->flush |= flush;
Tom Herbert03d56da2014-09-09 11:23:14 -0700232
Alexander Duyck15305452016-04-10 21:44:57 -0400233 /* If the previous IP ID value was based on an atomic
234 * datagram we can overwrite the value and ignore it.
235 */
236 if (NAPI_GRO_CB(skb)->is_atomic)
237 NAPI_GRO_CB(p)->flush_id = 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000238 }
239
Alexander Duyck15305452016-04-10 21:44:57 -0400240 NAPI_GRO_CB(skb)->is_atomic = true;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000241 NAPI_GRO_CB(skb)->flush |= flush;
242
Eric Dumazet4de462a2014-05-19 21:56:34 -0700243 skb_gro_postpull_rcsum(skb, iph, nlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000244
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000245 pp = ops->callbacks.gro_receive(head, skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000246
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000247out_unlock:
248 rcu_read_unlock();
249
250out:
251 NAPI_GRO_CB(skb)->flush |= flush;
252
253 return pp;
254}
255
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700256static struct sk_buff **sit_gro_receive(struct sk_buff **head,
257 struct sk_buff *skb)
258{
259 if (NAPI_GRO_CB(skb)->encap_mark) {
260 NAPI_GRO_CB(skb)->flush = 1;
261 return NULL;
262 }
263
264 NAPI_GRO_CB(skb)->encap_mark = 1;
265
266 return ipv6_gro_receive(head, skb);
267}
268
Jerry Chu299603e82013-12-11 20:53:45 -0800269static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000270{
271 const struct net_offload *ops;
Jerry Chu299603e82013-12-11 20:53:45 -0800272 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000273 int err = -ENOSYS;
274
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700275 if (skb->encapsulation)
276 skb_set_inner_network_header(skb, nhoff);
277
Jerry Chu299603e82013-12-11 20:53:45 -0800278 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000279
280 rcu_read_lock();
Jerry Chu299603e82013-12-11 20:53:45 -0800281
282 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000283 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000284 goto out_unlock;
285
Jerry Chu299603e82013-12-11 20:53:45 -0800286 err = ops->callbacks.gro_complete(skb, nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000287
288out_unlock:
289 rcu_read_unlock();
290
291 return err;
292}
293
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700294static int sit_gro_complete(struct sk_buff *skb, int nhoff)
295{
296 skb->encapsulation = 1;
Tom Herbert7e133182016-05-18 09:06:10 -0700297 skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700298 return ipv6_gro_complete(skb, nhoff);
299}
300
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000301static struct packet_offload ipv6_packet_offload __read_mostly = {
302 .type = cpu_to_be16(ETH_P_IPV6),
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000303 .callbacks = {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000304 .gso_segment = ipv6_gso_segment,
305 .gro_receive = ipv6_gro_receive,
306 .gro_complete = ipv6_gro_complete,
307 },
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000308};
309
Eric Dumazet61c1db72013-10-20 20:47:30 -0700310static const struct net_offload sit_offload = {
311 .callbacks = {
Eric Dumazet61c1db72013-10-20 20:47:30 -0700312 .gso_segment = ipv6_gso_segment,
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700313 .gro_receive = sit_gro_receive,
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700314 .gro_complete = sit_gro_complete,
Eric Dumazet61c1db72013-10-20 20:47:30 -0700315 },
316};
317
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000318static int __init ipv6_offload_init(void)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000319{
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000320
321 if (tcpv6_offload_init() < 0)
322 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000323 if (ipv6_exthdrs_offload_init() < 0)
324 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
325
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000326 dev_add_offload(&ipv6_packet_offload);
Eric Dumazet61c1db72013-10-20 20:47:30 -0700327
328 inet_add_offload(&sit_offload, IPPROTO_IPV6);
329
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000330 return 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000331}
332
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000333fs_initcall(ipv6_offload_init);