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