]>
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 | ||
49 | proto = opth->nexthdr; | |
50 | __skb_pull(skb, len); | |
51 | } | |
52 | ||
53 | return proto; | |
54 | } | |
55 | ||
56 | 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 | 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 | ||
d1da932e VY |
69 | ops = rcu_dereference(inet6_offloads[ |
70 | ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]); | |
71 | ||
f191a1d1 | 72 | if (likely(ops && ops->callbacks.gso_send_check)) { |
d1da932e | 73 | skb_reset_transport_header(skb); |
f191a1d1 | 74 | err = ops->callbacks.gso_send_check(skb); |
d1da932e | 75 | } |
d1da932e VY |
76 | |
77 | out: | |
78 | return err; | |
79 | } | |
80 | ||
81 | 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; | |
d949d826 | 92 | bool tunnel; |
d3e5e006 | 93 | int nhoff; |
d1da932e | 94 | |
d1da932e VY |
95 | if (unlikely(skb_shinfo(skb)->gso_type & |
96 | ~(SKB_GSO_UDP | | |
97 | SKB_GSO_DODGY | | |
98 | SKB_GSO_TCP_ECN | | |
68c33163 | 99 | SKB_GSO_GRE | |
cb32f511 | 100 | SKB_GSO_IPIP | |
73136267 | 101 | SKB_GSO_UDP_TUNNEL | |
0d89d203 | 102 | SKB_GSO_MPLS | |
d1da932e VY |
103 | SKB_GSO_TCPV6 | |
104 | 0))) | |
105 | goto out; | |
106 | ||
d3e5e006 ED |
107 | skb_reset_network_header(skb); |
108 | nhoff = skb_network_header(skb) - skb_mac_header(skb); | |
d1da932e VY |
109 | if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) |
110 | goto out; | |
111 | ||
d3e5e006 ED |
112 | tunnel = SKB_GSO_CB(skb)->encap_level > 0; |
113 | if (tunnel) | |
114 | features = skb->dev->hw_enc_features & netif_skb_features(skb); | |
115 | SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h); | |
116 | ||
d1da932e VY |
117 | ipv6h = ipv6_hdr(skb); |
118 | __skb_pull(skb, sizeof(*ipv6h)); | |
119 | segs = ERR_PTR(-EPROTONOSUPPORT); | |
120 | ||
121 | proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); | |
b917eb15 | 122 | |
d1da932e | 123 | ops = rcu_dereference(inet6_offloads[proto]); |
f191a1d1 | 124 | if (likely(ops && ops->callbacks.gso_segment)) { |
d1da932e | 125 | skb_reset_transport_header(skb); |
f191a1d1 | 126 | segs = ops->callbacks.gso_segment(skb, features); |
d1da932e | 127 | } |
d1da932e VY |
128 | |
129 | if (IS_ERR(segs)) | |
130 | goto out; | |
131 | ||
132 | for (skb = segs; skb; skb = skb->next) { | |
d3e5e006 ED |
133 | ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff); |
134 | ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h)); | |
135 | if (tunnel) { | |
136 | skb_reset_inner_headers(skb); | |
137 | skb->encapsulation = 1; | |
138 | } | |
139 | skb->network_header = (u8 *)ipv6h - skb->head; | |
140 | ||
d949d826 | 141 | if (!tunnel && proto == IPPROTO_UDP) { |
d1da932e | 142 | unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); |
d3e5e006 | 143 | fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen); |
d1da932e VY |
144 | fptr->frag_off = htons(offset); |
145 | if (skb->next != NULL) | |
146 | fptr->frag_off |= htons(IP6_MF); | |
147 | offset += (ntohs(ipv6h->payload_len) - | |
148 | sizeof(struct frag_hdr)); | |
149 | } | |
150 | } | |
151 | ||
152 | out: | |
153 | return segs; | |
154 | } | |
155 | ||
156 | static struct sk_buff **ipv6_gro_receive(struct sk_buff **head, | |
157 | struct sk_buff *skb) | |
158 | { | |
159 | const struct net_offload *ops; | |
160 | struct sk_buff **pp = NULL; | |
161 | struct sk_buff *p; | |
162 | struct ipv6hdr *iph; | |
163 | unsigned int nlen; | |
164 | unsigned int hlen; | |
165 | unsigned int off; | |
166 | int flush = 1; | |
167 | int proto; | |
168 | __wsum csum; | |
169 | ||
170 | off = skb_gro_offset(skb); | |
171 | hlen = off + sizeof(*iph); | |
172 | iph = skb_gro_header_fast(skb, off); | |
173 | if (skb_gro_header_hard(skb, hlen)) { | |
174 | iph = skb_gro_header_slow(skb, hlen, off); | |
175 | if (unlikely(!iph)) | |
176 | goto out; | |
177 | } | |
178 | ||
179 | skb_gro_pull(skb, sizeof(*iph)); | |
180 | skb_set_transport_header(skb, skb_gro_offset(skb)); | |
181 | ||
182 | flush += ntohs(iph->payload_len) != skb_gro_len(skb); | |
183 | ||
184 | rcu_read_lock(); | |
185 | proto = iph->nexthdr; | |
186 | ops = rcu_dereference(inet6_offloads[proto]); | |
f191a1d1 | 187 | if (!ops || !ops->callbacks.gro_receive) { |
d1da932e VY |
188 | __pskb_pull(skb, skb_gro_offset(skb)); |
189 | proto = ipv6_gso_pull_exthdrs(skb, proto); | |
190 | skb_gro_pull(skb, -skb_transport_offset(skb)); | |
191 | skb_reset_transport_header(skb); | |
192 | __skb_push(skb, skb_gro_offset(skb)); | |
193 | ||
194 | ops = rcu_dereference(inet6_offloads[proto]); | |
f191a1d1 | 195 | if (!ops || !ops->callbacks.gro_receive) |
d1da932e VY |
196 | goto out_unlock; |
197 | ||
198 | iph = ipv6_hdr(skb); | |
199 | } | |
200 | ||
201 | NAPI_GRO_CB(skb)->proto = proto; | |
202 | ||
203 | flush--; | |
204 | nlen = skb_network_header_len(skb); | |
205 | ||
206 | for (p = *head; p; p = p->next) { | |
207 | const struct ipv6hdr *iph2; | |
208 | __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */ | |
209 | ||
210 | if (!NAPI_GRO_CB(p)->same_flow) | |
211 | continue; | |
212 | ||
213 | iph2 = ipv6_hdr(p); | |
214 | first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ; | |
215 | ||
216 | /* All fields must match except length and Traffic Class. */ | |
217 | if (nlen != skb_network_header_len(p) || | |
218 | (first_word & htonl(0xF00FFFFF)) || | |
219 | memcmp(&iph->nexthdr, &iph2->nexthdr, | |
220 | nlen - offsetof(struct ipv6hdr, nexthdr))) { | |
221 | NAPI_GRO_CB(p)->same_flow = 0; | |
222 | continue; | |
223 | } | |
224 | /* flush if Traffic Class fields are different */ | |
225 | NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000)); | |
226 | NAPI_GRO_CB(p)->flush |= flush; | |
227 | } | |
228 | ||
229 | NAPI_GRO_CB(skb)->flush |= flush; | |
230 | ||
231 | csum = skb->csum; | |
232 | skb_postpull_rcsum(skb, iph, skb_network_header_len(skb)); | |
233 | ||
f191a1d1 | 234 | pp = ops->callbacks.gro_receive(head, skb); |
d1da932e VY |
235 | |
236 | skb->csum = csum; | |
237 | ||
238 | out_unlock: | |
239 | rcu_read_unlock(); | |
240 | ||
241 | out: | |
242 | NAPI_GRO_CB(skb)->flush |= flush; | |
243 | ||
244 | return pp; | |
245 | } | |
246 | ||
247 | static int ipv6_gro_complete(struct sk_buff *skb) | |
248 | { | |
249 | const struct net_offload *ops; | |
250 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
251 | int err = -ENOSYS; | |
252 | ||
253 | iph->payload_len = htons(skb->len - skb_network_offset(skb) - | |
254 | sizeof(*iph)); | |
255 | ||
256 | rcu_read_lock(); | |
257 | ops = rcu_dereference(inet6_offloads[NAPI_GRO_CB(skb)->proto]); | |
f191a1d1 | 258 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
d1da932e VY |
259 | goto out_unlock; |
260 | ||
f191a1d1 | 261 | err = ops->callbacks.gro_complete(skb); |
d1da932e VY |
262 | |
263 | out_unlock: | |
264 | rcu_read_unlock(); | |
265 | ||
266 | return err; | |
267 | } | |
268 | ||
269 | static struct packet_offload ipv6_packet_offload __read_mostly = { | |
270 | .type = cpu_to_be16(ETH_P_IPV6), | |
f191a1d1 VY |
271 | .callbacks = { |
272 | .gso_send_check = ipv6_gso_send_check, | |
273 | .gso_segment = ipv6_gso_segment, | |
274 | .gro_receive = ipv6_gro_receive, | |
275 | .gro_complete = ipv6_gro_complete, | |
276 | }, | |
d1da932e VY |
277 | }; |
278 | ||
c6b641a4 | 279 | static int __init ipv6_offload_init(void) |
d1da932e | 280 | { |
c6b641a4 VY |
281 | |
282 | if (tcpv6_offload_init() < 0) | |
283 | pr_crit("%s: Cannot add TCP protocol offload\n", __func__); | |
284 | if (udp_offload_init() < 0) | |
285 | pr_crit("%s: Cannot add UDP protocol offload\n", __func__); | |
286 | if (ipv6_exthdrs_offload_init() < 0) | |
287 | pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); | |
288 | ||
d1da932e | 289 | dev_add_offload(&ipv6_packet_offload); |
c6b641a4 | 290 | return 0; |
d1da932e VY |
291 | } |
292 | ||
c6b641a4 | 293 | fs_initcall(ipv6_offload_init); |