blob: 56c4c8b88b28f2e96220ed68a67d7473aa40c567 [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;
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +010024 possible_net_t net;
Or Gerlitzb582ef02014-01-20 13:59:19 +020025 struct rcu_head rcu;
26 struct udp_offload_priv __rcu *next;
27};
28
Tom Herbert8bce6d72014-09-29 20:22:29 -070029static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
30 netdev_features_t features,
31 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
32 netdev_features_t features),
Tom Herbert4bcb8772014-11-04 09:06:52 -080033 __be16 new_protocol, bool is_ipv6)
Tom Herbert155e0102014-07-13 19:49:56 -070034{
Alexander Duyckdbef4912016-02-05 15:28:20 -080035 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
Tom Herbert155e0102014-07-13 19:49:56 -070036 struct sk_buff *segs = ERR_PTR(-EINVAL);
Alexander Duyckfdaefd62016-02-05 15:28:14 -080037 bool remcsum, need_csum, offload_csum;
Alexander Duyckdbef4912016-02-05 15:28:20 -080038 struct udphdr *uh = udp_hdr(skb);
Tom Herbert155e0102014-07-13 19:49:56 -070039 u16 mac_offset = skb->mac_header;
Tom Herbert155e0102014-07-13 19:49:56 -070040 __be16 protocol = skb->protocol;
Alexander Duyckdbef4912016-02-05 15:28:20 -080041 u16 mac_len = skb->mac_len;
Tom Herbert155e0102014-07-13 19:49:56 -070042 int udp_offset, outer_hlen;
Alexander Duyckdbef4912016-02-05 15:28:20 -080043 u32 partial;
Tom Herbert155e0102014-07-13 19:49:56 -070044
45 if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
46 goto out;
47
Alexander Duyckdbef4912016-02-05 15:28:20 -080048 /* adjust partial header checksum to negate old length */
49 partial = (__force u32)uh->check + (__force u16)~uh->len;
50
51 /* setup inner skb. */
Tom Herbert155e0102014-07-13 19:49:56 -070052 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;
Alexander Duyckfdaefd62016-02-05 15:28:14 -080058
59 need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
Tom Herbert4bcb8772014-11-04 09:06:52 -080060 skb->encap_hdr_csum = need_csum;
Alexander Duyckfdaefd62016-02-05 15:28:14 -080061
62 remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
Tom Herberte585f232014-11-04 09:06:54 -080063 skb->remcsum_offload = remcsum;
Tom Herbert155e0102014-07-13 19:49:56 -070064
Tom Herbert4bcb8772014-11-04 09:06:52 -080065 /* Try to offload checksum if possible */
66 offload_csum = !!(need_csum &&
Alexander Duyckfdaefd62016-02-05 15:28:14 -080067 (skb->dev->features &
68 (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
69 (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
Tom Herbert155e0102014-07-13 19:49:56 -070070
Alexander Duyckbef3c6c2016-02-05 15:27:31 -080071 features &= skb->dev->hw_enc_features;
72
Alexander Duyck7fbeffe2016-02-05 15:27:43 -080073 /* The only checksum offload we care about from here on out is the
74 * outer one so strip the existing checksum feature flags and
75 * instead set the flag based on our outer checksum offload value.
76 */
77 if (remcsum) {
78 features &= ~NETIF_F_CSUM_MASK;
79 if (offload_csum)
80 features |= NETIF_F_HW_CSUM;
81 }
82
Tom Herbert155e0102014-07-13 19:49:56 -070083 /* segment inner packet. */
Alexander Duyckbef3c6c2016-02-05 15:27:31 -080084 segs = gso_inner_segment(skb, features);
Himangi Saraogi27446442014-07-27 12:38:38 +053085 if (IS_ERR_OR_NULL(segs)) {
Tom Herbert155e0102014-07-13 19:49:56 -070086 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
87 mac_len);
88 goto out;
89 }
90
91 outer_hlen = skb_tnl_header_len(skb);
92 udp_offset = outer_hlen - tnl_hlen;
93 skb = segs;
94 do {
Alexander Duyckdbef4912016-02-05 15:28:20 -080095 __be16 len;
Tom Herbert155e0102014-07-13 19:49:56 -070096
Alexander Duyckfdaefd62016-02-05 15:28:14 -080097 if (remcsum)
Tom Herbert4bcb8772014-11-04 09:06:52 -080098 skb->ip_summed = CHECKSUM_NONE;
Alexander Duyckfdaefd62016-02-05 15:28:14 -080099
100 /* Set up inner headers if we are offloading inner checksum */
101 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbert4bcb8772014-11-04 09:06:52 -0800102 skb_reset_inner_headers(skb);
103 skb->encapsulation = 1;
104 }
Tom Herbert155e0102014-07-13 19:49:56 -0700105
106 skb->mac_len = mac_len;
Tom Herbert4bcb8772014-11-04 09:06:52 -0800107 skb->protocol = protocol;
Tom Herbert155e0102014-07-13 19:49:56 -0700108
Alexander Duyckdbef4912016-02-05 15:28:20 -0800109 __skb_push(skb, outer_hlen);
Tom Herbert155e0102014-07-13 19:49:56 -0700110 skb_reset_mac_header(skb);
111 skb_set_network_header(skb, mac_len);
112 skb_set_transport_header(skb, udp_offset);
Alexander Duyckdbef4912016-02-05 15:28:20 -0800113 len = htons(skb->len - udp_offset);
Tom Herbert155e0102014-07-13 19:49:56 -0700114 uh = udp_hdr(skb);
Alexander Duyckdbef4912016-02-05 15:28:20 -0800115 uh->len = len;
Tom Herbert155e0102014-07-13 19:49:56 -0700116
Tom Herbert4bcb8772014-11-04 09:06:52 -0800117 if (!need_csum)
118 continue;
Tom Herbert155e0102014-07-13 19:49:56 -0700119
Tom Herbert4bcb8772014-11-04 09:06:52 -0800120 uh->check = ~csum_fold((__force __wsum)
Alexander Duyckdbef4912016-02-05 15:28:20 -0800121 ((__force u32)len + partial));
Alexander Duyckfdaefd62016-02-05 15:28:14 -0800122
123 if (skb->encapsulation || !offload_csum) {
124 uh->check = gso_make_checksum(skb, ~uh->check);
125 if (uh->check == 0)
126 uh->check = CSUM_MANGLED_0;
127 } else {
Tom Herbert4bcb8772014-11-04 09:06:52 -0800128 skb->ip_summed = CHECKSUM_PARTIAL;
129 skb->csum_start = skb_transport_header(skb) - skb->head;
130 skb->csum_offset = offsetof(struct udphdr, check);
Tom Herbert155e0102014-07-13 19:49:56 -0700131 }
Tom Herbert155e0102014-07-13 19:49:56 -0700132 } while ((skb = skb->next));
133out:
134 return segs;
135}
136
Tom Herbert8bce6d72014-09-29 20:22:29 -0700137struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
138 netdev_features_t features,
139 bool is_ipv6)
140{
141 __be16 protocol = skb->protocol;
142 const struct net_offload **offloads;
143 const struct net_offload *ops;
144 struct sk_buff *segs = ERR_PTR(-EINVAL);
145 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
146 netdev_features_t features);
147
148 rcu_read_lock();
149
150 switch (skb->inner_protocol_type) {
151 case ENCAP_TYPE_ETHER:
152 protocol = skb->inner_protocol;
153 gso_inner_segment = skb_mac_gso_segment;
154 break;
155 case ENCAP_TYPE_IPPROTO:
156 offloads = is_ipv6 ? inet6_offloads : inet_offloads;
157 ops = rcu_dereference(offloads[skb->inner_ipproto]);
158 if (!ops || !ops->callbacks.gso_segment)
159 goto out_unlock;
160 gso_inner_segment = ops->callbacks.gso_segment;
161 break;
162 default:
163 goto out_unlock;
164 }
165
166 segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
Tom Herbert4bcb8772014-11-04 09:06:52 -0800167 protocol, is_ipv6);
Tom Herbert8bce6d72014-09-29 20:22:29 -0700168
169out_unlock:
170 rcu_read_unlock();
171
172 return segs;
173}
174
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200175static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
176 netdev_features_t features)
177{
178 struct sk_buff *segs = ERR_PTR(-EINVAL);
179 unsigned int mss;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800180 __wsum csum;
Tom Herbertf71470b2014-09-20 14:52:29 -0700181 struct udphdr *uh;
182 struct iphdr *iph;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800183
184 if (skb->encapsulation &&
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700185 (skb_shinfo(skb)->gso_type &
186 (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
Tom Herbert8bce6d72014-09-29 20:22:29 -0700187 segs = skb_udp_tunnel_segment(skb, features, false);
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800188 goto out;
189 }
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200190
Tom Herbertf71470b2014-09-20 14:52:29 -0700191 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
192 goto out;
193
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200194 mss = skb_shinfo(skb)->gso_size;
195 if (unlikely(skb->len <= mss))
196 goto out;
197
198 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
199 /* Packet is from an untrusted source, reset gso_segs. */
200 int type = skb_shinfo(skb)->gso_type;
201
202 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
203 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700204 SKB_GSO_UDP_TUNNEL_CSUM |
Tom Herberte585f232014-11-04 09:06:54 -0800205 SKB_GSO_TUNNEL_REMCSUM |
Eric Dumazetcb32f512013-10-19 11:42:57 -0700206 SKB_GSO_IPIP |
Pravin B Shelar59b93b42014-11-05 15:27:48 -0800207 SKB_GSO_GRE | SKB_GSO_GRE_CSUM) ||
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200208 !(type & (SKB_GSO_UDP))))
209 goto out;
210
211 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
212
213 segs = NULL;
214 goto out;
215 }
216
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800217 /* Do software UFO. Complete and fill in the UDP checksum as
218 * HW cannot do checksum of UDP packets sent as multiple
219 * IP fragments.
220 */
Tom Herbertf71470b2014-09-20 14:52:29 -0700221
222 uh = udp_hdr(skb);
223 iph = ip_hdr(skb);
224
225 uh->check = 0;
226 csum = skb_checksum(skb, 0, skb->len, 0);
227 uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
228 if (uh->check == 0)
229 uh->check = CSUM_MANGLED_0;
230
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800231 skb->ip_summed = CHECKSUM_NONE;
232
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200233 /* Fragment the skb. IP headers of the fragments are updated in
234 * inet_gso_segment()
235 */
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800236 segs = skb_segment(skb, features);
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200237out:
238 return segs;
239}
240
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100241int udp_add_offload(struct net *net, struct udp_offload *uo)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200242{
Or Gerlitzb5aaab12014-01-29 18:08:59 +0200243 struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200244
245 if (!new_offload)
246 return -ENOMEM;
247
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100248 write_pnet(&new_offload->net, net);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200249 new_offload->offload = uo;
250
251 spin_lock(&udp_offload_lock);
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200252 new_offload->next = udp_offload_base;
253 rcu_assign_pointer(udp_offload_base, new_offload);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200254 spin_unlock(&udp_offload_lock);
255
256 return 0;
257}
258EXPORT_SYMBOL(udp_add_offload);
259
260static void udp_offload_free_routine(struct rcu_head *head)
261{
262 struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu);
263 kfree(ou_priv);
264}
265
266void udp_del_offload(struct udp_offload *uo)
267{
268 struct udp_offload_priv __rcu **head = &udp_offload_base;
269 struct udp_offload_priv *uo_priv;
270
271 spin_lock(&udp_offload_lock);
272
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200273 uo_priv = udp_deref_protected(*head);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200274 for (; uo_priv != NULL;
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200275 uo_priv = udp_deref_protected(*head)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200276 if (uo_priv->offload == uo) {
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200277 rcu_assign_pointer(*head,
278 udp_deref_protected(uo_priv->next));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200279 goto unlock;
280 }
281 head = &uo_priv->next;
282 }
Shlomo Pongratza1d0cd82014-01-22 15:23:29 +0200283 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200284unlock:
285 spin_unlock(&udp_offload_lock);
Ian Morris00db4122015-04-03 09:17:27 +0100286 if (uo_priv)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200287 call_rcu(&uo_priv->rcu, udp_offload_free_routine);
288}
289EXPORT_SYMBOL(udp_del_offload);
290
Tom Herbert57c67ff2014-08-22 13:34:44 -0700291struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
292 struct udphdr *uh)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200293{
294 struct udp_offload_priv *uo_priv;
295 struct sk_buff *p, **pp = NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700296 struct udphdr *uh2;
297 unsigned int off = skb_gro_offset(skb);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200298 int flush = 1;
299
300 if (NAPI_GRO_CB(skb)->udp_mark ||
Tom Herbert662880f2014-08-27 21:26:56 -0700301 (skb->ip_summed != CHECKSUM_PARTIAL &&
302 NAPI_GRO_CB(skb)->csum_cnt == 0 &&
303 !NAPI_GRO_CB(skb)->csum_valid))
Or Gerlitzb582ef02014-01-20 13:59:19 +0200304 goto out;
305
306 /* mark that this skb passed once through the udp gro layer */
307 NAPI_GRO_CB(skb)->udp_mark = 1;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200308
309 rcu_read_lock();
310 uo_priv = rcu_dereference(udp_offload_base);
311 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100312 if (net_eq(read_pnet(&uo_priv->net), dev_net(skb->dev)) &&
313 uo_priv->offload->port == uh->dest &&
Or Gerlitzb582ef02014-01-20 13:59:19 +0200314 uo_priv->offload->callbacks.gro_receive)
315 goto unflush;
316 }
317 goto out_unlock;
318
319unflush:
320 flush = 0;
321
322 for (p = *head; p; p = p->next) {
323 if (!NAPI_GRO_CB(p)->same_flow)
324 continue;
325
326 uh2 = (struct udphdr *)(p->data + off);
Tom Herbert57c67ff2014-08-22 13:34:44 -0700327
328 /* Match ports and either checksums are either both zero
329 * or nonzero.
330 */
331 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
332 (!uh->check ^ !uh2->check)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200333 NAPI_GRO_CB(p)->same_flow = 0;
334 continue;
335 }
336 }
337
338 skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700339 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
Tom Herbertafe93322014-09-17 12:25:57 -0700340 NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
Tom Herberta2b12f32015-01-12 17:00:37 -0800341 pp = uo_priv->offload->callbacks.gro_receive(head, skb,
342 uo_priv->offload);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200343
344out_unlock:
345 rcu_read_unlock();
346out:
347 NAPI_GRO_CB(skb)->flush |= flush;
348 return pp;
349}
350
Tom Herbert57c67ff2014-08-22 13:34:44 -0700351static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
352 struct sk_buff *skb)
353{
354 struct udphdr *uh = udp_gro_udphdr(skb);
355
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700356 if (unlikely(!uh))
357 goto flush;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700358
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700359 /* Don't bother verifying checksum if we're going to flush anyway. */
Scott Wood2d8f7e22014-09-10 21:23:18 -0500360 if (NAPI_GRO_CB(skb)->flush)
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700361 goto skip;
362
363 if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
364 inet_gro_compute_pseudo))
365 goto flush;
366 else if (uh->check)
367 skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
368 inet_gro_compute_pseudo);
369skip:
Tom Herbertefc98d02014-10-03 15:48:08 -0700370 NAPI_GRO_CB(skb)->is_ipv6 = 0;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700371 return udp_gro_receive(head, skb, uh);
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700372
373flush:
374 NAPI_GRO_CB(skb)->flush = 1;
375 return NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700376}
377
378int udp_gro_complete(struct sk_buff *skb, int nhoff)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200379{
380 struct udp_offload_priv *uo_priv;
381 __be16 newlen = htons(skb->len - nhoff);
382 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
383 int err = -ENOSYS;
384
385 uh->len = newlen;
386
387 rcu_read_lock();
388
389 uo_priv = rcu_dereference(udp_offload_base);
390 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100391 if (net_eq(read_pnet(&uo_priv->net), dev_net(skb->dev)) &&
392 uo_priv->offload->port == uh->dest &&
Or Gerlitzb582ef02014-01-20 13:59:19 +0200393 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}