blob: f9386160cbee0288e294ea2cd8ba3b5be65cdbf6 [file] [log] [blame]
Daniel Borkmannda5bab02013-06-08 12:56:03 +02001/*
2 * IPV4 GSO/GRO offload support
3 * Linux INET 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 * UDPv4 GSO support
11 */
12
13#include <linux/skbuff.h>
14#include <net/udp.h>
15#include <net/protocol.h>
16
Or Gerlitzb582ef02014-01-20 13:59:19 +020017static DEFINE_SPINLOCK(udp_offload_lock);
Shlomo Pongratza1d0cd82014-01-22 15:23:29 +020018static struct udp_offload_priv __rcu *udp_offload_base __read_mostly;
Or Gerlitzb582ef02014-01-20 13:59:19 +020019
Shlomo Pongratza664a4f2014-02-02 15:42:10 +020020#define udp_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&udp_offload_lock))
21
Or Gerlitzb582ef02014-01-20 13:59:19 +020022struct udp_offload_priv {
23 struct udp_offload *offload;
24 struct rcu_head rcu;
25 struct udp_offload_priv __rcu *next;
26};
27
Tom Herbert8bce6d72014-09-29 20:22:29 -070028static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
29 netdev_features_t features,
30 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
31 netdev_features_t features),
Tom Herbert4bcb8772014-11-04 09:06:52 -080032 __be16 new_protocol, bool is_ipv6)
Tom Herbert155e0102014-07-13 19:49:56 -070033{
34 struct sk_buff *segs = ERR_PTR(-EINVAL);
35 u16 mac_offset = skb->mac_header;
36 int mac_len = skb->mac_len;
37 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
38 __be16 protocol = skb->protocol;
39 netdev_features_t enc_features;
40 int udp_offset, outer_hlen;
41 unsigned int oldlen;
Tom Herbert4bcb8772014-11-04 09:06:52 -080042 bool need_csum = !!(skb_shinfo(skb)->gso_type &
43 SKB_GSO_UDP_TUNNEL_CSUM);
Tom Herberte585f232014-11-04 09:06:54 -080044 bool remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
45 bool offload_csum = false, dont_encap = (need_csum || remcsum);
Tom Herbert155e0102014-07-13 19:49:56 -070046
47 oldlen = (u16)~skb->len;
48
49 if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
50 goto out;
51
52 skb->encapsulation = 0;
53 __skb_pull(skb, tnl_hlen);
54 skb_reset_mac_header(skb);
55 skb_set_network_header(skb, skb_inner_network_offset(skb));
56 skb->mac_len = skb_inner_network_offset(skb);
Tom Herbert8bce6d72014-09-29 20:22:29 -070057 skb->protocol = new_protocol;
Tom Herbert4bcb8772014-11-04 09:06:52 -080058 skb->encap_hdr_csum = need_csum;
Tom Herberte585f232014-11-04 09:06:54 -080059 skb->remcsum_offload = remcsum;
Tom Herbert155e0102014-07-13 19:49:56 -070060
Tom Herbert4bcb8772014-11-04 09:06:52 -080061 /* Try to offload checksum if possible */
62 offload_csum = !!(need_csum &&
63 (skb->dev->features &
64 (is_ipv6 ? NETIF_F_V6_CSUM : NETIF_F_V4_CSUM)));
Tom Herbert155e0102014-07-13 19:49:56 -070065
66 /* segment inner packet. */
Florian Westphal1e16aa32014-10-20 13:49:16 +020067 enc_features = skb->dev->hw_enc_features & features;
Tom Herbert8bce6d72014-09-29 20:22:29 -070068 segs = gso_inner_segment(skb, enc_features);
Himangi Saraogi27446442014-07-27 12:38:38 +053069 if (IS_ERR_OR_NULL(segs)) {
Tom Herbert155e0102014-07-13 19:49:56 -070070 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
71 mac_len);
72 goto out;
73 }
74
75 outer_hlen = skb_tnl_header_len(skb);
76 udp_offset = outer_hlen - tnl_hlen;
77 skb = segs;
78 do {
79 struct udphdr *uh;
80 int len;
Tom Herbert4bcb8772014-11-04 09:06:52 -080081 __be32 delta;
Tom Herbert155e0102014-07-13 19:49:56 -070082
Tom Herbert4bcb8772014-11-04 09:06:52 -080083 if (dont_encap) {
84 skb->encapsulation = 0;
85 skb->ip_summed = CHECKSUM_NONE;
86 } else {
87 /* Only set up inner headers if we might be offloading
88 * inner checksum.
89 */
90 skb_reset_inner_headers(skb);
91 skb->encapsulation = 1;
92 }
Tom Herbert155e0102014-07-13 19:49:56 -070093
94 skb->mac_len = mac_len;
Tom Herbert4bcb8772014-11-04 09:06:52 -080095 skb->protocol = protocol;
Tom Herbert155e0102014-07-13 19:49:56 -070096
97 skb_push(skb, outer_hlen);
98 skb_reset_mac_header(skb);
99 skb_set_network_header(skb, mac_len);
100 skb_set_transport_header(skb, udp_offset);
101 len = skb->len - udp_offset;
102 uh = udp_hdr(skb);
103 uh->len = htons(len);
104
Tom Herbert4bcb8772014-11-04 09:06:52 -0800105 if (!need_csum)
106 continue;
Tom Herbert155e0102014-07-13 19:49:56 -0700107
Tom Herbert4bcb8772014-11-04 09:06:52 -0800108 delta = htonl(oldlen + len);
109
110 uh->check = ~csum_fold((__force __wsum)
111 ((__force u32)uh->check +
112 (__force u32)delta));
Tom Herbert4bcb8772014-11-04 09:06:52 -0800113 if (offload_csum) {
114 skb->ip_summed = CHECKSUM_PARTIAL;
115 skb->csum_start = skb_transport_header(skb) - skb->head;
116 skb->csum_offset = offsetof(struct udphdr, check);
Tom Herberte585f232014-11-04 09:06:54 -0800117 } else if (remcsum) {
118 /* Need to calculate checksum from scratch,
119 * inner checksums are never when doing
120 * remote_checksum_offload.
121 */
122
123 skb->csum = skb_checksum(skb, udp_offset,
124 skb->len - udp_offset,
125 0);
126 uh->check = csum_fold(skb->csum);
127 if (uh->check == 0)
128 uh->check = CSUM_MANGLED_0;
Tom Herbert4bcb8772014-11-04 09:06:52 -0800129 } else {
Tom Herbert155e0102014-07-13 19:49:56 -0700130 uh->check = gso_make_checksum(skb, ~uh->check);
131
132 if (uh->check == 0)
133 uh->check = CSUM_MANGLED_0;
134 }
Tom Herbert155e0102014-07-13 19:49:56 -0700135 } while ((skb = skb->next));
136out:
137 return segs;
138}
139
Tom Herbert8bce6d72014-09-29 20:22:29 -0700140struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
141 netdev_features_t features,
142 bool is_ipv6)
143{
144 __be16 protocol = skb->protocol;
145 const struct net_offload **offloads;
146 const struct net_offload *ops;
147 struct sk_buff *segs = ERR_PTR(-EINVAL);
148 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
149 netdev_features_t features);
150
151 rcu_read_lock();
152
153 switch (skb->inner_protocol_type) {
154 case ENCAP_TYPE_ETHER:
155 protocol = skb->inner_protocol;
156 gso_inner_segment = skb_mac_gso_segment;
157 break;
158 case ENCAP_TYPE_IPPROTO:
159 offloads = is_ipv6 ? inet6_offloads : inet_offloads;
160 ops = rcu_dereference(offloads[skb->inner_ipproto]);
161 if (!ops || !ops->callbacks.gso_segment)
162 goto out_unlock;
163 gso_inner_segment = ops->callbacks.gso_segment;
164 break;
165 default:
166 goto out_unlock;
167 }
168
169 segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
Tom Herbert4bcb8772014-11-04 09:06:52 -0800170 protocol, is_ipv6);
Tom Herbert8bce6d72014-09-29 20:22:29 -0700171
172out_unlock:
173 rcu_read_unlock();
174
175 return segs;
176}
177
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200178static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
179 netdev_features_t features)
180{
181 struct sk_buff *segs = ERR_PTR(-EINVAL);
182 unsigned int mss;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800183 __wsum csum;
Tom Herbertf71470b2014-09-20 14:52:29 -0700184 struct udphdr *uh;
185 struct iphdr *iph;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800186
187 if (skb->encapsulation &&
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700188 (skb_shinfo(skb)->gso_type &
189 (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
Tom Herbert8bce6d72014-09-29 20:22:29 -0700190 segs = skb_udp_tunnel_segment(skb, features, false);
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800191 goto out;
192 }
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200193
Tom Herbertf71470b2014-09-20 14:52:29 -0700194 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
195 goto out;
196
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200197 mss = skb_shinfo(skb)->gso_size;
198 if (unlikely(skb->len <= mss))
199 goto out;
200
201 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
202 /* Packet is from an untrusted source, reset gso_segs. */
203 int type = skb_shinfo(skb)->gso_type;
204
205 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
206 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700207 SKB_GSO_UDP_TUNNEL_CSUM |
Tom Herberte585f232014-11-04 09:06:54 -0800208 SKB_GSO_TUNNEL_REMCSUM |
Eric Dumazetcb32f512013-10-19 11:42:57 -0700209 SKB_GSO_IPIP |
Pravin B Shelar59b93b42014-11-05 15:27:48 -0800210 SKB_GSO_GRE | SKB_GSO_GRE_CSUM) ||
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200211 !(type & (SKB_GSO_UDP))))
212 goto out;
213
214 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
215
216 segs = NULL;
217 goto out;
218 }
219
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800220 /* Do software UFO. Complete and fill in the UDP checksum as
221 * HW cannot do checksum of UDP packets sent as multiple
222 * IP fragments.
223 */
Tom Herbertf71470b2014-09-20 14:52:29 -0700224
225 uh = udp_hdr(skb);
226 iph = ip_hdr(skb);
227
228 uh->check = 0;
229 csum = skb_checksum(skb, 0, skb->len, 0);
230 uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
231 if (uh->check == 0)
232 uh->check = CSUM_MANGLED_0;
233
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800234 skb->ip_summed = CHECKSUM_NONE;
235
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200236 /* Fragment the skb. IP headers of the fragments are updated in
237 * inet_gso_segment()
238 */
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800239 segs = skb_segment(skb, features);
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200240out:
241 return segs;
242}
243
Or Gerlitzb582ef02014-01-20 13:59:19 +0200244int udp_add_offload(struct udp_offload *uo)
245{
Or Gerlitzb5aaab12014-01-29 18:08:59 +0200246 struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200247
248 if (!new_offload)
249 return -ENOMEM;
250
251 new_offload->offload = uo;
252
253 spin_lock(&udp_offload_lock);
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200254 new_offload->next = udp_offload_base;
255 rcu_assign_pointer(udp_offload_base, new_offload);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200256 spin_unlock(&udp_offload_lock);
257
258 return 0;
259}
260EXPORT_SYMBOL(udp_add_offload);
261
262static void udp_offload_free_routine(struct rcu_head *head)
263{
264 struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu);
265 kfree(ou_priv);
266}
267
268void udp_del_offload(struct udp_offload *uo)
269{
270 struct udp_offload_priv __rcu **head = &udp_offload_base;
271 struct udp_offload_priv *uo_priv;
272
273 spin_lock(&udp_offload_lock);
274
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200275 uo_priv = udp_deref_protected(*head);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200276 for (; uo_priv != NULL;
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200277 uo_priv = udp_deref_protected(*head)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200278 if (uo_priv->offload == uo) {
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200279 rcu_assign_pointer(*head,
280 udp_deref_protected(uo_priv->next));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200281 goto unlock;
282 }
283 head = &uo_priv->next;
284 }
Shlomo Pongratza1d0cd82014-01-22 15:23:29 +0200285 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200286unlock:
287 spin_unlock(&udp_offload_lock);
Ian Morris00db4122015-04-03 09:17:27 +0100288 if (uo_priv)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200289 call_rcu(&uo_priv->rcu, udp_offload_free_routine);
290}
291EXPORT_SYMBOL(udp_del_offload);
292
Tom Herbert57c67ff2014-08-22 13:34:44 -0700293struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
294 struct udphdr *uh)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200295{
296 struct udp_offload_priv *uo_priv;
297 struct sk_buff *p, **pp = NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700298 struct udphdr *uh2;
299 unsigned int off = skb_gro_offset(skb);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200300 int flush = 1;
301
302 if (NAPI_GRO_CB(skb)->udp_mark ||
Tom Herbert662880f2014-08-27 21:26:56 -0700303 (skb->ip_summed != CHECKSUM_PARTIAL &&
304 NAPI_GRO_CB(skb)->csum_cnt == 0 &&
305 !NAPI_GRO_CB(skb)->csum_valid))
Or Gerlitzb582ef02014-01-20 13:59:19 +0200306 goto out;
307
308 /* mark that this skb passed once through the udp gro layer */
309 NAPI_GRO_CB(skb)->udp_mark = 1;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200310
311 rcu_read_lock();
312 uo_priv = rcu_dereference(udp_offload_base);
313 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
314 if (uo_priv->offload->port == uh->dest &&
315 uo_priv->offload->callbacks.gro_receive)
316 goto unflush;
317 }
318 goto out_unlock;
319
320unflush:
321 flush = 0;
322
323 for (p = *head; p; p = p->next) {
324 if (!NAPI_GRO_CB(p)->same_flow)
325 continue;
326
327 uh2 = (struct udphdr *)(p->data + off);
Tom Herbert57c67ff2014-08-22 13:34:44 -0700328
329 /* Match ports and either checksums are either both zero
330 * or nonzero.
331 */
332 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
333 (!uh->check ^ !uh2->check)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200334 NAPI_GRO_CB(p)->same_flow = 0;
335 continue;
336 }
337 }
338
339 skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700340 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
Tom Herbertafe93322014-09-17 12:25:57 -0700341 NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
Tom Herberta2b12f32015-01-12 17:00:37 -0800342 pp = uo_priv->offload->callbacks.gro_receive(head, skb,
343 uo_priv->offload);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200344
345out_unlock:
346 rcu_read_unlock();
347out:
348 NAPI_GRO_CB(skb)->flush |= flush;
349 return pp;
350}
351
Tom Herbert57c67ff2014-08-22 13:34:44 -0700352static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
353 struct sk_buff *skb)
354{
355 struct udphdr *uh = udp_gro_udphdr(skb);
356
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700357 if (unlikely(!uh))
358 goto flush;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700359
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700360 /* Don't bother verifying checksum if we're going to flush anyway. */
Scott Wood2d8f7e22014-09-10 21:23:18 -0500361 if (NAPI_GRO_CB(skb)->flush)
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700362 goto skip;
363
364 if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
365 inet_gro_compute_pseudo))
366 goto flush;
367 else if (uh->check)
368 skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
369 inet_gro_compute_pseudo);
370skip:
Tom Herbertefc98d02014-10-03 15:48:08 -0700371 NAPI_GRO_CB(skb)->is_ipv6 = 0;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700372 return udp_gro_receive(head, skb, uh);
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700373
374flush:
375 NAPI_GRO_CB(skb)->flush = 1;
376 return NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700377}
378
379int udp_gro_complete(struct sk_buff *skb, int nhoff)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200380{
381 struct udp_offload_priv *uo_priv;
382 __be16 newlen = htons(skb->len - nhoff);
383 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
384 int err = -ENOSYS;
385
386 uh->len = newlen;
387
388 rcu_read_lock();
389
390 uo_priv = rcu_dereference(udp_offload_base);
391 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
392 if (uo_priv->offload->port == uh->dest &&
393 uo_priv->offload->callbacks.gro_complete)
394 break;
395 }
396
Ian Morris00db4122015-04-03 09:17:27 +0100397 if (uo_priv) {
Tom Herbertafe93322014-09-17 12:25:57 -0700398 NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
Tom Herberta2b12f32015-01-12 17:00:37 -0800399 err = uo_priv->offload->callbacks.gro_complete(skb,
400 nhoff + sizeof(struct udphdr),
401 uo_priv->offload);
Tom Herbertafe93322014-09-17 12:25:57 -0700402 }
Or Gerlitzb582ef02014-01-20 13:59:19 +0200403
404 rcu_read_unlock();
Tom Herbert6db93ea2015-02-10 16:30:29 -0800405
406 if (skb->remcsum_offload)
407 skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
408
409 skb->encapsulation = 1;
410 skb_set_inner_mac_header(skb, nhoff + sizeof(struct udphdr));
411
Or Gerlitzb582ef02014-01-20 13:59:19 +0200412 return err;
413}
414
Eric Dumazet72bb17b2014-09-09 08:29:12 -0700415static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
Tom Herbert57c67ff2014-08-22 13:34:44 -0700416{
417 const struct iphdr *iph = ip_hdr(skb);
418 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
419
Tom Herbert6db93ea2015-02-10 16:30:29 -0800420 if (uh->check) {
421 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700422 uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
423 iph->daddr, 0);
Tom Herbert6db93ea2015-02-10 16:30:29 -0800424 } else {
425 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
426 }
Tom Herbert57c67ff2014-08-22 13:34:44 -0700427
428 return udp_gro_complete(skb, nhoff);
429}
430
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200431static const struct net_offload udpv4_offload = {
432 .callbacks = {
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200433 .gso_segment = udp4_ufo_fragment,
Tom Herbert57c67ff2014-08-22 13:34:44 -0700434 .gro_receive = udp4_gro_receive,
435 .gro_complete = udp4_gro_complete,
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200436 },
437};
438
439int __init udpv4_offload_init(void)
440{
441 return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
442}