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