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