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