blob: d54c5744e3db5f742f22f70b9b4c9b8a72a9d77c [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
49 proto = opth->nexthdr;
50 __skb_pull(skb, len);
51 }
52
53 return proto;
54}
55
56static int ipv6_gso_send_check(struct sk_buff *skb)
57{
58 const struct ipv6hdr *ipv6h;
59 const struct net_offload *ops;
60 int err = -EINVAL;
61
62 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
63 goto out;
64
65 ipv6h = ipv6_hdr(skb);
66 __skb_pull(skb, sizeof(*ipv6h));
67 err = -EPROTONOSUPPORT;
68
Vlad Yasevichd1da9322012-11-15 08:49:16 +000069 ops = rcu_dereference(inet6_offloads[
70 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
71
Vlad Yasevichf191a1d2012-11-15 08:49:23 +000072 if (likely(ops && ops->callbacks.gso_send_check)) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +000073 skb_reset_transport_header(skb);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +000074 err = ops->callbacks.gso_send_check(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +000075 }
Vlad Yasevichd1da9322012-11-15 08:49:16 +000076
77out:
78 return err;
79}
80
81static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
82 netdev_features_t features)
83{
84 struct sk_buff *segs = ERR_PTR(-EINVAL);
85 struct ipv6hdr *ipv6h;
86 const struct net_offload *ops;
87 int proto;
88 struct frag_hdr *fptr;
89 unsigned int unfrag_ip6hlen;
90 u8 *prevhdr;
91 int offset = 0;
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010092 bool encap, udpfrag;
Eric Dumazetd3e5e002013-10-20 20:47:29 -070093 int nhoff;
Vlad Yasevichd1da9322012-11-15 08:49:16 +000094
Vlad Yasevichd1da9322012-11-15 08:49:16 +000095 if (unlikely(skb_shinfo(skb)->gso_type &
96 ~(SKB_GSO_UDP |
97 SKB_GSO_DODGY |
98 SKB_GSO_TCP_ECN |
Pravin B Shelar68c33162013-02-14 14:02:41 +000099 SKB_GSO_GRE |
Eric Dumazetcb32f512013-10-19 11:42:57 -0700100 SKB_GSO_IPIP |
Eric Dumazet61c1db72013-10-20 20:47:30 -0700101 SKB_GSO_SIT |
Pravin B Shelar73136262013-03-07 13:21:51 +0000102 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700103 SKB_GSO_UDP_TUNNEL_CSUM |
Simon Horman0d89d202013-05-23 21:02:52 +0000104 SKB_GSO_MPLS |
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000105 SKB_GSO_TCPV6 |
106 0)))
107 goto out;
108
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700109 skb_reset_network_header(skb);
110 nhoff = skb_network_header(skb) - skb_mac_header(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000111 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
112 goto out;
113
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100114 encap = SKB_GSO_CB(skb)->encap_level > 0;
115 if (encap)
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700116 features = skb->dev->hw_enc_features & netif_skb_features(skb);
117 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
118
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000119 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);
Eric Dumazetb917eb12013-10-18 14:43:55 -0700124
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100125 if (skb->encapsulation &&
126 skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
127 udpfrag = proto == IPPROTO_UDP && encap;
128 else
129 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
130
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000131 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000132 if (likely(ops && ops->callbacks.gso_segment)) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000133 skb_reset_transport_header(skb);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000134 segs = ops->callbacks.gso_segment(skb, features);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000135 }
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000136
137 if (IS_ERR(segs))
138 goto out;
139
140 for (skb = segs; skb; skb = skb->next) {
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700141 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
142 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700143 skb->network_header = (u8 *)ipv6h - skb->head;
144
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100145 if (udpfrag) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000146 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700147 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000148 fptr->frag_off = htons(offset);
149 if (skb->next != NULL)
150 fptr->frag_off |= htons(IP6_MF);
151 offset += (ntohs(ipv6h->payload_len) -
152 sizeof(struct frag_hdr));
153 }
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100154 if (encap)
155 skb_reset_inner_headers(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000156 }
157
158out:
159 return segs;
160}
161
Jerry Chu299603e82013-12-11 20:53:45 -0800162/* Return the total length of all the extension hdrs, following the same
163 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
164 */
165static int ipv6_exthdrs_len(struct ipv6hdr *iph,
166 const struct net_offload **opps)
167{
Jerry Chu810c23a2013-12-15 18:48:07 -0800168 struct ipv6_opt_hdr *opth = (void *)iph;
169 int len = 0, proto, optlen = sizeof(*iph);
Jerry Chu299603e82013-12-11 20:53:45 -0800170
171 proto = iph->nexthdr;
172 for (;;) {
173 if (proto != NEXTHDR_HOP) {
174 *opps = rcu_dereference(inet6_offloads[proto]);
175 if (unlikely(!(*opps)))
176 break;
177 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
178 break;
179 }
Jerry Chu810c23a2013-12-15 18:48:07 -0800180 opth = (void *)opth + optlen;
181 optlen = ipv6_optlen(opth);
Jerry Chu299603e82013-12-11 20:53:45 -0800182 len += optlen;
183 proto = opth->nexthdr;
184 }
185 return len;
186}
187
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000188static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
189 struct sk_buff *skb)
190{
191 const struct net_offload *ops;
192 struct sk_buff **pp = NULL;
193 struct sk_buff *p;
194 struct ipv6hdr *iph;
195 unsigned int nlen;
196 unsigned int hlen;
197 unsigned int off;
Jerry Chubf5a7552014-01-07 10:23:19 -0800198 u16 flush = 1;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000199 int proto;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000200
201 off = skb_gro_offset(skb);
202 hlen = off + sizeof(*iph);
203 iph = skb_gro_header_fast(skb, off);
204 if (skb_gro_header_hard(skb, hlen)) {
205 iph = skb_gro_header_slow(skb, hlen, off);
206 if (unlikely(!iph))
207 goto out;
208 }
209
Jerry Chu299603e82013-12-11 20:53:45 -0800210 skb_set_network_header(skb, off);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000211 skb_gro_pull(skb, sizeof(*iph));
212 skb_set_transport_header(skb, skb_gro_offset(skb));
213
214 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
215
216 rcu_read_lock();
217 proto = iph->nexthdr;
218 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000219 if (!ops || !ops->callbacks.gro_receive) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000220 __pskb_pull(skb, skb_gro_offset(skb));
221 proto = ipv6_gso_pull_exthdrs(skb, proto);
222 skb_gro_pull(skb, -skb_transport_offset(skb));
223 skb_reset_transport_header(skb);
224 __skb_push(skb, skb_gro_offset(skb));
225
226 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000227 if (!ops || !ops->callbacks.gro_receive)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000228 goto out_unlock;
229
230 iph = ipv6_hdr(skb);
231 }
232
233 NAPI_GRO_CB(skb)->proto = proto;
234
235 flush--;
236 nlen = skb_network_header_len(skb);
237
238 for (p = *head; p; p = p->next) {
239 const struct ipv6hdr *iph2;
240 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
241
242 if (!NAPI_GRO_CB(p)->same_flow)
243 continue;
244
Jerry Chu299603e82013-12-11 20:53:45 -0800245 iph2 = (struct ipv6hdr *)(p->data + off);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000246 first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ;
247
Jerry Chu299603e82013-12-11 20:53:45 -0800248 /* All fields must match except length and Traffic Class.
249 * XXX skbs on the gro_list have all been parsed and pulled
250 * already so we don't need to compare nlen
251 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
252 * memcmp() alone below is suffcient, right?
253 */
254 if ((first_word & htonl(0xF00FFFFF)) ||
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000255 memcmp(&iph->nexthdr, &iph2->nexthdr,
256 nlen - offsetof(struct ipv6hdr, nexthdr))) {
257 NAPI_GRO_CB(p)->same_flow = 0;
258 continue;
259 }
260 /* flush if Traffic Class fields are different */
261 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
262 NAPI_GRO_CB(p)->flush |= flush;
263 }
264
265 NAPI_GRO_CB(skb)->flush |= flush;
266
Eric Dumazet4de462a2014-05-19 21:56:34 -0700267 skb_gro_postpull_rcsum(skb, iph, nlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000268
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000269 pp = ops->callbacks.gro_receive(head, skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000270
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000271out_unlock:
272 rcu_read_unlock();
273
274out:
275 NAPI_GRO_CB(skb)->flush |= flush;
276
277 return pp;
278}
279
Jerry Chu299603e82013-12-11 20:53:45 -0800280static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000281{
282 const struct net_offload *ops;
Jerry Chu299603e82013-12-11 20:53:45 -0800283 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000284 int err = -ENOSYS;
285
Jerry Chu299603e82013-12-11 20:53:45 -0800286 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000287
288 rcu_read_lock();
Jerry Chu299603e82013-12-11 20:53:45 -0800289
290 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000291 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000292 goto out_unlock;
293
Jerry Chu299603e82013-12-11 20:53:45 -0800294 err = ops->callbacks.gro_complete(skb, nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000295
296out_unlock:
297 rcu_read_unlock();
298
299 return err;
300}
301
302static struct packet_offload ipv6_packet_offload __read_mostly = {
303 .type = cpu_to_be16(ETH_P_IPV6),
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000304 .callbacks = {
305 .gso_send_check = ipv6_gso_send_check,
306 .gso_segment = ipv6_gso_segment,
307 .gro_receive = ipv6_gro_receive,
308 .gro_complete = ipv6_gro_complete,
309 },
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000310};
311
Eric Dumazet61c1db72013-10-20 20:47:30 -0700312static const struct net_offload sit_offload = {
313 .callbacks = {
314 .gso_send_check = ipv6_gso_send_check,
315 .gso_segment = ipv6_gso_segment,
316 },
317};
318
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000319static int __init ipv6_offload_init(void)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000320{
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000321
322 if (tcpv6_offload_init() < 0)
323 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
324 if (udp_offload_init() < 0)
325 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
326 if (ipv6_exthdrs_offload_init() < 0)
327 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
328
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000329 dev_add_offload(&ipv6_packet_offload);
Eric Dumazet61c1db72013-10-20 20:47:30 -0700330
331 inet_add_offload(&sit_offload, IPPROTO_IPV6);
332
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000333 return 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000334}
335
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000336fs_initcall(ipv6_offload_init);