blob: 82e9f30760283aca2f3d9468573aea607bd5bf6b [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;
66 u8 *prevhdr;
67 int offset = 0;
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010068 bool encap, udpfrag;
Eric Dumazetd3e5e002013-10-20 20:47:29 -070069 int nhoff;
Vlad Yasevichd1da9322012-11-15 08:49:16 +000070
Vlad Yasevichd1da9322012-11-15 08:49:16 +000071 if (unlikely(skb_shinfo(skb)->gso_type &
Alexander Duyckb6fef4c2014-11-21 19:37:09 -080072 ~(SKB_GSO_TCPV4 |
73 SKB_GSO_UDP |
Vlad Yasevichd1da9322012-11-15 08:49:16 +000074 SKB_GSO_DODGY |
75 SKB_GSO_TCP_ECN |
Pravin B Shelar68c33162013-02-14 14:02:41 +000076 SKB_GSO_GRE |
Tom Herbert4749c092014-06-04 17:20:23 -070077 SKB_GSO_GRE_CSUM |
Eric Dumazetcb32f512013-10-19 11:42:57 -070078 SKB_GSO_IPIP |
Eric Dumazet61c1db72013-10-20 20:47:30 -070079 SKB_GSO_SIT |
Pravin B Shelar73136262013-03-07 13:21:51 +000080 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -070081 SKB_GSO_UDP_TUNNEL_CSUM |
Tom Herberte585f232014-11-04 09:06:54 -080082 SKB_GSO_TUNNEL_REMCSUM |
Vlad Yasevichd1da9322012-11-15 08:49:16 +000083 SKB_GSO_TCPV6 |
84 0)))
85 goto out;
86
Eric Dumazetd3e5e002013-10-20 20:47:29 -070087 skb_reset_network_header(skb);
88 nhoff = skb_network_header(skb) - skb_mac_header(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +000089 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
90 goto out;
91
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010092 encap = SKB_GSO_CB(skb)->encap_level > 0;
93 if (encap)
Florian Westphal1e16aa32014-10-20 13:49:16 +020094 features &= skb->dev->hw_enc_features;
Eric Dumazetd3e5e002013-10-20 20:47:29 -070095 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
96
Vlad Yasevichd1da9322012-11-15 08:49:16 +000097 ipv6h = ipv6_hdr(skb);
98 __skb_pull(skb, sizeof(*ipv6h));
99 segs = ERR_PTR(-EPROTONOSUPPORT);
100
101 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
Eric Dumazetb917eb12013-10-18 14:43:55 -0700102
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100103 if (skb->encapsulation &&
104 skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
105 udpfrag = proto == IPPROTO_UDP && encap;
106 else
107 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
108
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000109 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000110 if (likely(ops && ops->callbacks.gso_segment)) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000111 skb_reset_transport_header(skb);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000112 segs = ops->callbacks.gso_segment(skb, features);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000113 }
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000114
115 if (IS_ERR(segs))
116 goto out;
117
118 for (skb = segs; skb; skb = skb->next) {
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700119 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
120 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700121 skb->network_header = (u8 *)ipv6h - skb->head;
122
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100123 if (udpfrag) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000124 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700125 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000126 fptr->frag_off = htons(offset);
Ian Morris53b24b82015-03-29 14:00:05 +0100127 if (skb->next)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000128 fptr->frag_off |= htons(IP6_MF);
129 offset += (ntohs(ipv6h->payload_len) -
130 sizeof(struct frag_hdr));
131 }
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100132 if (encap)
133 skb_reset_inner_headers(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000134 }
135
136out:
137 return segs;
138}
139
Jerry Chu299603e82013-12-11 20:53:45 -0800140/* Return the total length of all the extension hdrs, following the same
141 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
142 */
143static int ipv6_exthdrs_len(struct ipv6hdr *iph,
144 const struct net_offload **opps)
145{
Jerry Chu810c23a2013-12-15 18:48:07 -0800146 struct ipv6_opt_hdr *opth = (void *)iph;
147 int len = 0, proto, optlen = sizeof(*iph);
Jerry Chu299603e82013-12-11 20:53:45 -0800148
149 proto = iph->nexthdr;
150 for (;;) {
151 if (proto != NEXTHDR_HOP) {
152 *opps = rcu_dereference(inet6_offloads[proto]);
153 if (unlikely(!(*opps)))
154 break;
155 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
156 break;
157 }
Jerry Chu810c23a2013-12-15 18:48:07 -0800158 opth = (void *)opth + optlen;
159 optlen = ipv6_optlen(opth);
Jerry Chu299603e82013-12-11 20:53:45 -0800160 len += optlen;
161 proto = opth->nexthdr;
162 }
163 return len;
164}
165
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000166static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
167 struct sk_buff *skb)
168{
169 const struct net_offload *ops;
170 struct sk_buff **pp = NULL;
171 struct sk_buff *p;
172 struct ipv6hdr *iph;
173 unsigned int nlen;
174 unsigned int hlen;
175 unsigned int off;
Jerry Chubf5a7552014-01-07 10:23:19 -0800176 u16 flush = 1;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000177 int proto;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000178
179 off = skb_gro_offset(skb);
180 hlen = off + sizeof(*iph);
181 iph = skb_gro_header_fast(skb, off);
182 if (skb_gro_header_hard(skb, hlen)) {
183 iph = skb_gro_header_slow(skb, hlen, off);
184 if (unlikely(!iph))
185 goto out;
186 }
187
Jerry Chu299603e82013-12-11 20:53:45 -0800188 skb_set_network_header(skb, off);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000189 skb_gro_pull(skb, sizeof(*iph));
190 skb_set_transport_header(skb, skb_gro_offset(skb));
191
192 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
193
194 rcu_read_lock();
195 proto = iph->nexthdr;
196 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000197 if (!ops || !ops->callbacks.gro_receive) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000198 __pskb_pull(skb, skb_gro_offset(skb));
199 proto = ipv6_gso_pull_exthdrs(skb, proto);
200 skb_gro_pull(skb, -skb_transport_offset(skb));
201 skb_reset_transport_header(skb);
202 __skb_push(skb, skb_gro_offset(skb));
203
204 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000205 if (!ops || !ops->callbacks.gro_receive)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000206 goto out_unlock;
207
208 iph = ipv6_hdr(skb);
209 }
210
211 NAPI_GRO_CB(skb)->proto = proto;
212
213 flush--;
214 nlen = skb_network_header_len(skb);
215
216 for (p = *head; p; p = p->next) {
217 const struct ipv6hdr *iph2;
218 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
219
220 if (!NAPI_GRO_CB(p)->same_flow)
221 continue;
222
Jerry Chu299603e82013-12-11 20:53:45 -0800223 iph2 = (struct ipv6hdr *)(p->data + off);
Ian Morris67ba4152014-08-24 21:53:10 +0100224 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000225
Jerry Chu299603e82013-12-11 20:53:45 -0800226 /* All fields must match except length and Traffic Class.
227 * XXX skbs on the gro_list have all been parsed and pulled
228 * already so we don't need to compare nlen
229 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
230 * memcmp() alone below is suffcient, right?
231 */
232 if ((first_word & htonl(0xF00FFFFF)) ||
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000233 memcmp(&iph->nexthdr, &iph2->nexthdr,
234 nlen - offsetof(struct ipv6hdr, nexthdr))) {
235 NAPI_GRO_CB(p)->same_flow = 0;
236 continue;
237 }
238 /* flush if Traffic Class fields are different */
239 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
240 NAPI_GRO_CB(p)->flush |= flush;
Tom Herbert03d56da2014-09-09 11:23:14 -0700241
242 /* Clear flush_id, there's really no concept of ID in IPv6. */
243 NAPI_GRO_CB(p)->flush_id = 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000244 }
245
246 NAPI_GRO_CB(skb)->flush |= flush;
247
Eric Dumazet4de462a2014-05-19 21:56:34 -0700248 skb_gro_postpull_rcsum(skb, iph, nlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000249
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000250 pp = ops->callbacks.gro_receive(head, skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000251
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000252out_unlock:
253 rcu_read_unlock();
254
255out:
256 NAPI_GRO_CB(skb)->flush |= flush;
257
258 return pp;
259}
260
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700261static struct sk_buff **sit_gro_receive(struct sk_buff **head,
262 struct sk_buff *skb)
263{
264 if (NAPI_GRO_CB(skb)->encap_mark) {
265 NAPI_GRO_CB(skb)->flush = 1;
266 return NULL;
267 }
268
269 NAPI_GRO_CB(skb)->encap_mark = 1;
270
271 return ipv6_gro_receive(head, skb);
272}
273
Jerry Chu299603e82013-12-11 20:53:45 -0800274static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000275{
276 const struct net_offload *ops;
Jerry Chu299603e82013-12-11 20:53:45 -0800277 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000278 int err = -ENOSYS;
279
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700280 if (skb->encapsulation)
281 skb_set_inner_network_header(skb, nhoff);
282
Jerry Chu299603e82013-12-11 20:53:45 -0800283 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000284
285 rcu_read_lock();
Jerry Chu299603e82013-12-11 20:53:45 -0800286
287 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000288 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000289 goto out_unlock;
290
Jerry Chu299603e82013-12-11 20:53:45 -0800291 err = ops->callbacks.gro_complete(skb, nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000292
293out_unlock:
294 rcu_read_unlock();
295
296 return err;
297}
298
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700299static int sit_gro_complete(struct sk_buff *skb, int nhoff)
300{
301 skb->encapsulation = 1;
302 skb_shinfo(skb)->gso_type |= SKB_GSO_SIT;
303 return ipv6_gro_complete(skb, nhoff);
304}
305
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000306static struct packet_offload ipv6_packet_offload __read_mostly = {
307 .type = cpu_to_be16(ETH_P_IPV6),
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000308 .callbacks = {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000309 .gso_segment = ipv6_gso_segment,
310 .gro_receive = ipv6_gro_receive,
311 .gro_complete = ipv6_gro_complete,
312 },
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000313};
314
Eric Dumazet61c1db72013-10-20 20:47:30 -0700315static const struct net_offload sit_offload = {
316 .callbacks = {
Eric Dumazet61c1db72013-10-20 20:47:30 -0700317 .gso_segment = ipv6_gso_segment,
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700318 .gro_receive = sit_gro_receive,
Eric Dumazetfeec0cb2015-10-19 20:40:17 -0700319 .gro_complete = sit_gro_complete,
Eric Dumazet61c1db72013-10-20 20:47:30 -0700320 },
321};
322
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000323static int __init ipv6_offload_init(void)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000324{
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000325
326 if (tcpv6_offload_init() < 0)
327 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
328 if (udp_offload_init() < 0)
329 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
330 if (ipv6_exthdrs_offload_init() < 0)
331 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
332
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000333 dev_add_offload(&ipv6_packet_offload);
Eric Dumazet61c1db72013-10-20 20:47:30 -0700334
335 inet_add_offload(&sit_offload, IPPROTO_IPV6);
336
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000337 return 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000338}
339
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000340fs_initcall(ipv6_offload_init);