]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv6/ip6_offload.c
net,IB/mlx: Bump all Mellanox driver versions
[mirror_ubuntu-artful-kernel.git] / net / ipv6 / ip6_offload.c
CommitLineData
d1da932e
VY
1/*
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>
c6b641a4 15#include <linux/printk.h>
d1da932e
VY
16
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
d1da932e
VY
69 ops = rcu_dereference(inet6_offloads[
70 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
71
f191a1d1 72 if (likely(ops && ops->callbacks.gso_send_check)) {
d1da932e 73 skb_reset_transport_header(skb);
f191a1d1 74 err = ops->callbacks.gso_send_check(skb);
d1da932e 75 }
d1da932e
VY
76
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;
d949d826 92 bool tunnel;
d3e5e006 93 int nhoff;
d1da932e 94
d1da932e
VY
95 if (unlikely(skb_shinfo(skb)->gso_type &
96 ~(SKB_GSO_UDP |
97 SKB_GSO_DODGY |
98 SKB_GSO_TCP_ECN |
68c33163 99 SKB_GSO_GRE |
cb32f511 100 SKB_GSO_IPIP |
61c1db7f 101 SKB_GSO_SIT |
73136267 102 SKB_GSO_UDP_TUNNEL |
0d89d203 103 SKB_GSO_MPLS |
d1da932e
VY
104 SKB_GSO_TCPV6 |
105 0)))
106 goto out;
107
d3e5e006
ED
108 skb_reset_network_header(skb);
109 nhoff = skb_network_header(skb) - skb_mac_header(skb);
d1da932e
VY
110 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
111 goto out;
112
d3e5e006
ED
113 tunnel = SKB_GSO_CB(skb)->encap_level > 0;
114 if (tunnel)
115 features = skb->dev->hw_enc_features & netif_skb_features(skb);
116 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
117
d1da932e
VY
118 ipv6h = ipv6_hdr(skb);
119 __skb_pull(skb, sizeof(*ipv6h));
120 segs = ERR_PTR(-EPROTONOSUPPORT);
121
122 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
b917eb15 123
d1da932e 124 ops = rcu_dereference(inet6_offloads[proto]);
f191a1d1 125 if (likely(ops && ops->callbacks.gso_segment)) {
d1da932e 126 skb_reset_transport_header(skb);
f191a1d1 127 segs = ops->callbacks.gso_segment(skb, features);
d1da932e 128 }
d1da932e
VY
129
130 if (IS_ERR(segs))
131 goto out;
132
133 for (skb = segs; skb; skb = skb->next) {
d3e5e006
ED
134 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
135 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
136 if (tunnel) {
137 skb_reset_inner_headers(skb);
138 skb->encapsulation = 1;
139 }
140 skb->network_header = (u8 *)ipv6h - skb->head;
141
d949d826 142 if (!tunnel && proto == IPPROTO_UDP) {
d1da932e 143 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
d3e5e006 144 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
d1da932e
VY
145 fptr->frag_off = htons(offset);
146 if (skb->next != NULL)
147 fptr->frag_off |= htons(IP6_MF);
148 offset += (ntohs(ipv6h->payload_len) -
149 sizeof(struct frag_hdr));
150 }
151 }
152
153out:
154 return segs;
155}
156
299603e8
JC
157/* Return the total length of all the extension hdrs, following the same
158 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
159 */
160static int ipv6_exthdrs_len(struct ipv6hdr *iph,
161 const struct net_offload **opps)
162{
810c23a3
JC
163 struct ipv6_opt_hdr *opth = (void *)iph;
164 int len = 0, proto, optlen = sizeof(*iph);
299603e8
JC
165
166 proto = iph->nexthdr;
167 for (;;) {
168 if (proto != NEXTHDR_HOP) {
169 *opps = rcu_dereference(inet6_offloads[proto]);
170 if (unlikely(!(*opps)))
171 break;
172 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
173 break;
174 }
810c23a3
JC
175 opth = (void *)opth + optlen;
176 optlen = ipv6_optlen(opth);
299603e8
JC
177 len += optlen;
178 proto = opth->nexthdr;
179 }
180 return len;
181}
182
d1da932e
VY
183static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
184 struct sk_buff *skb)
185{
186 const struct net_offload *ops;
187 struct sk_buff **pp = NULL;
188 struct sk_buff *p;
189 struct ipv6hdr *iph;
190 unsigned int nlen;
191 unsigned int hlen;
192 unsigned int off;
bf5a755f 193 u16 flush = 1;
d1da932e
VY
194 int proto;
195 __wsum csum;
196
197 off = skb_gro_offset(skb);
198 hlen = off + sizeof(*iph);
199 iph = skb_gro_header_fast(skb, off);
200 if (skb_gro_header_hard(skb, hlen)) {
201 iph = skb_gro_header_slow(skb, hlen, off);
202 if (unlikely(!iph))
203 goto out;
204 }
205
299603e8 206 skb_set_network_header(skb, off);
d1da932e
VY
207 skb_gro_pull(skb, sizeof(*iph));
208 skb_set_transport_header(skb, skb_gro_offset(skb));
209
210 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
211
212 rcu_read_lock();
213 proto = iph->nexthdr;
214 ops = rcu_dereference(inet6_offloads[proto]);
f191a1d1 215 if (!ops || !ops->callbacks.gro_receive) {
d1da932e
VY
216 __pskb_pull(skb, skb_gro_offset(skb));
217 proto = ipv6_gso_pull_exthdrs(skb, proto);
218 skb_gro_pull(skb, -skb_transport_offset(skb));
219 skb_reset_transport_header(skb);
220 __skb_push(skb, skb_gro_offset(skb));
221
222 ops = rcu_dereference(inet6_offloads[proto]);
f191a1d1 223 if (!ops || !ops->callbacks.gro_receive)
d1da932e
VY
224 goto out_unlock;
225
226 iph = ipv6_hdr(skb);
227 }
228
229 NAPI_GRO_CB(skb)->proto = proto;
230
231 flush--;
232 nlen = skb_network_header_len(skb);
233
234 for (p = *head; p; p = p->next) {
235 const struct ipv6hdr *iph2;
236 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
237
238 if (!NAPI_GRO_CB(p)->same_flow)
239 continue;
240
299603e8 241 iph2 = (struct ipv6hdr *)(p->data + off);
d1da932e
VY
242 first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ;
243
299603e8
JC
244 /* All fields must match except length and Traffic Class.
245 * XXX skbs on the gro_list have all been parsed and pulled
246 * already so we don't need to compare nlen
247 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
248 * memcmp() alone below is suffcient, right?
249 */
250 if ((first_word & htonl(0xF00FFFFF)) ||
d1da932e
VY
251 memcmp(&iph->nexthdr, &iph2->nexthdr,
252 nlen - offsetof(struct ipv6hdr, nexthdr))) {
253 NAPI_GRO_CB(p)->same_flow = 0;
254 continue;
255 }
256 /* flush if Traffic Class fields are different */
257 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
258 NAPI_GRO_CB(p)->flush |= flush;
259 }
260
261 NAPI_GRO_CB(skb)->flush |= flush;
262
263 csum = skb->csum;
264 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
265
f191a1d1 266 pp = ops->callbacks.gro_receive(head, skb);
d1da932e
VY
267
268 skb->csum = csum;
269
270out_unlock:
271 rcu_read_unlock();
272
273out:
274 NAPI_GRO_CB(skb)->flush |= flush;
275
276 return pp;
277}
278
299603e8 279static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
d1da932e
VY
280{
281 const struct net_offload *ops;
299603e8 282 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
d1da932e
VY
283 int err = -ENOSYS;
284
299603e8 285 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
d1da932e
VY
286
287 rcu_read_lock();
299603e8
JC
288
289 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
f191a1d1 290 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
d1da932e
VY
291 goto out_unlock;
292
299603e8 293 err = ops->callbacks.gro_complete(skb, nhoff);
d1da932e
VY
294
295out_unlock:
296 rcu_read_unlock();
297
298 return err;
299}
300
301static struct packet_offload ipv6_packet_offload __read_mostly = {
302 .type = cpu_to_be16(ETH_P_IPV6),
f191a1d1
VY
303 .callbacks = {
304 .gso_send_check = ipv6_gso_send_check,
305 .gso_segment = ipv6_gso_segment,
306 .gro_receive = ipv6_gro_receive,
307 .gro_complete = ipv6_gro_complete,
308 },
d1da932e
VY
309};
310
61c1db7f
ED
311static const struct net_offload sit_offload = {
312 .callbacks = {
313 .gso_send_check = ipv6_gso_send_check,
314 .gso_segment = ipv6_gso_segment,
315 },
316};
317
c6b641a4 318static int __init ipv6_offload_init(void)
d1da932e 319{
c6b641a4
VY
320
321 if (tcpv6_offload_init() < 0)
322 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
323 if (udp_offload_init() < 0)
324 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
325 if (ipv6_exthdrs_offload_init() < 0)
326 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
327
d1da932e 328 dev_add_offload(&ipv6_packet_offload);
61c1db7f
ED
329
330 inet_add_offload(&sit_offload, IPPROTO_IPV6);
331
c6b641a4 332 return 0;
d1da932e
VY
333}
334
c6b641a4 335fs_initcall(ipv6_offload_init);