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