blob: 91014d32488ded4a7eeb0e00d27ee925bc48f8da [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 &
72 ~(SKB_GSO_UDP |
73 SKB_GSO_DODGY |
74 SKB_GSO_TCP_ECN |
Pravin B Shelar68c33162013-02-14 14:02:41 +000075 SKB_GSO_GRE |
Tom Herbert4749c092014-06-04 17:20:23 -070076 SKB_GSO_GRE_CSUM |
Eric Dumazetcb32f512013-10-19 11:42:57 -070077 SKB_GSO_IPIP |
Eric Dumazet61c1db72013-10-20 20:47:30 -070078 SKB_GSO_SIT |
Pravin B Shelar73136262013-03-07 13:21:51 +000079 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -070080 SKB_GSO_UDP_TUNNEL_CSUM |
Simon Horman0d89d202013-05-23 21:02:52 +000081 SKB_GSO_MPLS |
Vlad Yasevichd1da9322012-11-15 08:49:16 +000082 SKB_GSO_TCPV6 |
83 0)))
84 goto out;
85
Eric Dumazetd3e5e002013-10-20 20:47:29 -070086 skb_reset_network_header(skb);
87 nhoff = skb_network_header(skb) - skb_mac_header(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +000088 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
89 goto out;
90
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +010091 encap = SKB_GSO_CB(skb)->encap_level > 0;
92 if (encap)
Eric Dumazetd3e5e002013-10-20 20:47:29 -070093 features = skb->dev->hw_enc_features & netif_skb_features(skb);
94 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
95
Vlad Yasevichd1da9322012-11-15 08:49:16 +000096 ipv6h = ipv6_hdr(skb);
97 __skb_pull(skb, sizeof(*ipv6h));
98 segs = ERR_PTR(-EPROTONOSUPPORT);
99
100 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
Eric Dumazetb917eb12013-10-18 14:43:55 -0700101
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100102 if (skb->encapsulation &&
103 skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
104 udpfrag = proto == IPPROTO_UDP && encap;
105 else
106 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
107
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000108 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000109 if (likely(ops && ops->callbacks.gso_segment)) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000110 skb_reset_transport_header(skb);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000111 segs = ops->callbacks.gso_segment(skb, features);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000112 }
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000113
114 if (IS_ERR(segs))
115 goto out;
116
117 for (skb = segs; skb; skb = skb->next) {
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700118 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
119 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700120 skb->network_header = (u8 *)ipv6h - skb->head;
121
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100122 if (udpfrag) {
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000123 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
Eric Dumazetd3e5e002013-10-20 20:47:29 -0700124 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000125 fptr->frag_off = htons(offset);
126 if (skb->next != NULL)
127 fptr->frag_off |= htons(IP6_MF);
128 offset += (ntohs(ipv6h->payload_len) -
129 sizeof(struct frag_hdr));
130 }
Hannes Frederic Sowa91a48a22014-02-24 00:48:05 +0100131 if (encap)
132 skb_reset_inner_headers(skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000133 }
134
135out:
136 return segs;
137}
138
Jerry Chu299603e82013-12-11 20:53:45 -0800139/* Return the total length of all the extension hdrs, following the same
140 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
141 */
142static int ipv6_exthdrs_len(struct ipv6hdr *iph,
143 const struct net_offload **opps)
144{
Jerry Chu810c23a2013-12-15 18:48:07 -0800145 struct ipv6_opt_hdr *opth = (void *)iph;
146 int len = 0, proto, optlen = sizeof(*iph);
Jerry Chu299603e82013-12-11 20:53:45 -0800147
148 proto = iph->nexthdr;
149 for (;;) {
150 if (proto != NEXTHDR_HOP) {
151 *opps = rcu_dereference(inet6_offloads[proto]);
152 if (unlikely(!(*opps)))
153 break;
154 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
155 break;
156 }
Jerry Chu810c23a2013-12-15 18:48:07 -0800157 opth = (void *)opth + optlen;
158 optlen = ipv6_optlen(opth);
Jerry Chu299603e82013-12-11 20:53:45 -0800159 len += optlen;
160 proto = opth->nexthdr;
161 }
162 return len;
163}
164
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000165static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
166 struct sk_buff *skb)
167{
168 const struct net_offload *ops;
169 struct sk_buff **pp = NULL;
170 struct sk_buff *p;
171 struct ipv6hdr *iph;
172 unsigned int nlen;
173 unsigned int hlen;
174 unsigned int off;
Jerry Chubf5a7552014-01-07 10:23:19 -0800175 u16 flush = 1;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000176 int proto;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000177
178 off = skb_gro_offset(skb);
179 hlen = off + sizeof(*iph);
180 iph = skb_gro_header_fast(skb, off);
181 if (skb_gro_header_hard(skb, hlen)) {
182 iph = skb_gro_header_slow(skb, hlen, off);
183 if (unlikely(!iph))
184 goto out;
185 }
186
Jerry Chu299603e82013-12-11 20:53:45 -0800187 skb_set_network_header(skb, off);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000188 skb_gro_pull(skb, sizeof(*iph));
189 skb_set_transport_header(skb, skb_gro_offset(skb));
190
191 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
192
193 rcu_read_lock();
194 proto = iph->nexthdr;
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 __pskb_pull(skb, skb_gro_offset(skb));
198 proto = ipv6_gso_pull_exthdrs(skb, proto);
199 skb_gro_pull(skb, -skb_transport_offset(skb));
200 skb_reset_transport_header(skb);
201 __skb_push(skb, skb_gro_offset(skb));
202
203 ops = rcu_dereference(inet6_offloads[proto]);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000204 if (!ops || !ops->callbacks.gro_receive)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000205 goto out_unlock;
206
207 iph = ipv6_hdr(skb);
208 }
209
210 NAPI_GRO_CB(skb)->proto = proto;
211
212 flush--;
213 nlen = skb_network_header_len(skb);
214
215 for (p = *head; p; p = p->next) {
216 const struct ipv6hdr *iph2;
217 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
218
219 if (!NAPI_GRO_CB(p)->same_flow)
220 continue;
221
Jerry Chu299603e82013-12-11 20:53:45 -0800222 iph2 = (struct ipv6hdr *)(p->data + off);
Ian Morris67ba4152014-08-24 21:53:10 +0100223 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000224
Jerry Chu299603e82013-12-11 20:53:45 -0800225 /* All fields must match except length and Traffic Class.
226 * XXX skbs on the gro_list have all been parsed and pulled
227 * already so we don't need to compare nlen
228 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
229 * memcmp() alone below is suffcient, right?
230 */
231 if ((first_word & htonl(0xF00FFFFF)) ||
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000232 memcmp(&iph->nexthdr, &iph2->nexthdr,
233 nlen - offsetof(struct ipv6hdr, nexthdr))) {
234 NAPI_GRO_CB(p)->same_flow = 0;
235 continue;
236 }
237 /* flush if Traffic Class fields are different */
238 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
239 NAPI_GRO_CB(p)->flush |= flush;
Tom Herbert03d56da2014-09-09 11:23:14 -0700240
241 /* Clear flush_id, there's really no concept of ID in IPv6. */
242 NAPI_GRO_CB(p)->flush_id = 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000243 }
244
245 NAPI_GRO_CB(skb)->flush |= flush;
246
Eric Dumazet4de462a2014-05-19 21:56:34 -0700247 skb_gro_postpull_rcsum(skb, iph, nlen);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000248
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000249 pp = ops->callbacks.gro_receive(head, skb);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000250
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000251out_unlock:
252 rcu_read_unlock();
253
254out:
255 NAPI_GRO_CB(skb)->flush |= flush;
256
257 return pp;
258}
259
Jerry Chu299603e82013-12-11 20:53:45 -0800260static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000261{
262 const struct net_offload *ops;
Jerry Chu299603e82013-12-11 20:53:45 -0800263 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000264 int err = -ENOSYS;
265
Jerry Chu299603e82013-12-11 20:53:45 -0800266 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000267
268 rcu_read_lock();
Jerry Chu299603e82013-12-11 20:53:45 -0800269
270 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000271 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000272 goto out_unlock;
273
Jerry Chu299603e82013-12-11 20:53:45 -0800274 err = ops->callbacks.gro_complete(skb, nhoff);
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000275
276out_unlock:
277 rcu_read_unlock();
278
279 return err;
280}
281
282static struct packet_offload ipv6_packet_offload __read_mostly = {
283 .type = cpu_to_be16(ETH_P_IPV6),
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000284 .callbacks = {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +0000285 .gso_segment = ipv6_gso_segment,
286 .gro_receive = ipv6_gro_receive,
287 .gro_complete = ipv6_gro_complete,
288 },
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000289};
290
Eric Dumazet61c1db72013-10-20 20:47:30 -0700291static const struct net_offload sit_offload = {
292 .callbacks = {
Eric Dumazet61c1db72013-10-20 20:47:30 -0700293 .gso_segment = ipv6_gso_segment,
Tom Herbert19424e02014-09-09 11:23:16 -0700294 .gro_receive = ipv6_gro_receive,
295 .gro_complete = ipv6_gro_complete,
Eric Dumazet61c1db72013-10-20 20:47:30 -0700296 },
297};
298
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000299static int __init ipv6_offload_init(void)
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000300{
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000301
302 if (tcpv6_offload_init() < 0)
303 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
304 if (udp_offload_init() < 0)
305 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
306 if (ipv6_exthdrs_offload_init() < 0)
307 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
308
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000309 dev_add_offload(&ipv6_packet_offload);
Eric Dumazet61c1db72013-10-20 20:47:30 -0700310
311 inet_add_offload(&sit_offload, IPPROTO_IPV6);
312
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000313 return 0;
Vlad Yasevichd1da9322012-11-15 08:49:16 +0000314}
315
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000316fs_initcall(ipv6_offload_init);