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