blob: 64f9715173ac8bf3a8d641ae40ef95f67aa7a7a0 [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>
Paolo Abeni028e0a42018-12-14 11:51:59 +010016#include <net/inet_common.h>
Daniel Borkmannda5bab02013-06-08 12:56:03 +020017
Tom Herbert8bce6d72014-09-29 20:22:29 -070018static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
19 netdev_features_t features,
20 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
21 netdev_features_t features),
Tom Herbert4bcb8772014-11-04 09:06:52 -080022 __be16 new_protocol, bool is_ipv6)
Tom Herbert155e0102014-07-13 19:49:56 -070023{
Alexander Duyckdbef4912016-02-05 15:28:20 -080024 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
David S. Miller880388a2017-07-03 07:29:12 -070025 bool remcsum, need_csum, offload_csum, gso_partial;
Tom Herbert155e0102014-07-13 19:49:56 -070026 struct sk_buff *segs = ERR_PTR(-EINVAL);
Alexander Duyckdbef4912016-02-05 15:28:20 -080027 struct udphdr *uh = udp_hdr(skb);
Tom Herbert155e0102014-07-13 19:49:56 -070028 u16 mac_offset = skb->mac_header;
Tom Herbert155e0102014-07-13 19:49:56 -070029 __be16 protocol = skb->protocol;
Alexander Duyckdbef4912016-02-05 15:28:20 -080030 u16 mac_len = skb->mac_len;
Tom Herbert155e0102014-07-13 19:49:56 -070031 int udp_offset, outer_hlen;
Alexander Duyck08334822016-03-11 14:05:47 -080032 __wsum partial;
Ansis Attekab40c5f42017-04-21 15:23:05 -070033 bool need_ipsec;
Tom Herbert155e0102014-07-13 19:49:56 -070034
35 if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
36 goto out;
37
Alexander Duyck08334822016-03-11 14:05:47 -080038 /* Adjust partial header checksum to negate old length.
39 * We cannot rely on the value contained in uh->len as it is
40 * possible that the actual value exceeds the boundaries of the
41 * 16 bit length field due to the header being added outside of an
42 * IP or IPv6 frame that was already limited to 64K - 1.
43 */
Alexander Duyck802ab552016-04-10 21:45:03 -040044 if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)
45 partial = (__force __wsum)uh->len;
46 else
47 partial = (__force __wsum)htonl(skb->len);
48 partial = csum_sub(csum_unfold(uh->check), partial);
Alexander Duyckdbef4912016-02-05 15:28:20 -080049
50 /* setup inner skb. */
Tom Herbert155e0102014-07-13 19:49:56 -070051 skb->encapsulation = 0;
Alexander Duyck5197f342016-03-22 16:18:07 -070052 SKB_GSO_CB(skb)->encap_level = 0;
Tom Herbert155e0102014-07-13 19:49:56 -070053 __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
Ansis Attekab40c5f42017-04-21 15:23:05 -070065 need_ipsec = skb_dst(skb) && dst_xfrm(skb_dst(skb));
Tom Herbert4bcb8772014-11-04 09:06:52 -080066 /* Try to offload checksum if possible */
67 offload_csum = !!(need_csum &&
Ansis Attekab40c5f42017-04-21 15:23:05 -070068 !need_ipsec &&
Alexander Duyckfdaefd62016-02-05 15:28:14 -080069 (skb->dev->features &
70 (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
71 (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
Tom Herbert155e0102014-07-13 19:49:56 -070072
Alexander Duyckbef3c6c2016-02-05 15:27:31 -080073 features &= skb->dev->hw_enc_features;
74
Alexander Duyck7fbeffe2016-02-05 15:27:43 -080075 /* The only checksum offload we care about from here on out is the
76 * outer one so strip the existing checksum feature flags and
77 * instead set the flag based on our outer checksum offload value.
78 */
David S. Miller880388a2017-07-03 07:29:12 -070079 if (remcsum) {
Alexander Duyck7fbeffe2016-02-05 15:27:43 -080080 features &= ~NETIF_F_CSUM_MASK;
Alexander Duyck22463872016-02-24 16:46:21 -080081 if (!need_csum || offload_csum)
Alexander Duyck7fbeffe2016-02-05 15:27:43 -080082 features |= NETIF_F_HW_CSUM;
83 }
84
Tom Herbert155e0102014-07-13 19:49:56 -070085 /* segment inner packet. */
Alexander Duyckbef3c6c2016-02-05 15:27:31 -080086 segs = gso_inner_segment(skb, features);
Himangi Saraogi27446442014-07-27 12:38:38 +053087 if (IS_ERR_OR_NULL(segs)) {
Tom Herbert155e0102014-07-13 19:49:56 -070088 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
89 mac_len);
90 goto out;
91 }
92
Steffen Klassert07b26c92016-09-19 12:58:47 +020093 gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
94
Tom Herbert155e0102014-07-13 19:49:56 -070095 outer_hlen = skb_tnl_header_len(skb);
96 udp_offset = outer_hlen - tnl_hlen;
97 skb = segs;
98 do {
Alexander Duyck802ab552016-04-10 21:45:03 -040099 unsigned int len;
Tom Herbert155e0102014-07-13 19:49:56 -0700100
Alexander Duyckfdaefd62016-02-05 15:28:14 -0800101 if (remcsum)
Tom Herbert4bcb8772014-11-04 09:06:52 -0800102 skb->ip_summed = CHECKSUM_NONE;
Alexander Duyckfdaefd62016-02-05 15:28:14 -0800103
104 /* Set up inner headers if we are offloading inner checksum */
105 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbert4bcb8772014-11-04 09:06:52 -0800106 skb_reset_inner_headers(skb);
107 skb->encapsulation = 1;
108 }
Tom Herbert155e0102014-07-13 19:49:56 -0700109
110 skb->mac_len = mac_len;
Tom Herbert4bcb8772014-11-04 09:06:52 -0800111 skb->protocol = protocol;
Tom Herbert155e0102014-07-13 19:49:56 -0700112
Alexander Duyckdbef4912016-02-05 15:28:20 -0800113 __skb_push(skb, outer_hlen);
Tom Herbert155e0102014-07-13 19:49:56 -0700114 skb_reset_mac_header(skb);
115 skb_set_network_header(skb, mac_len);
116 skb_set_transport_header(skb, udp_offset);
Alexander Duyck802ab552016-04-10 21:45:03 -0400117 len = skb->len - udp_offset;
Tom Herbert155e0102014-07-13 19:49:56 -0700118 uh = udp_hdr(skb);
Alexander Duyck802ab552016-04-10 21:45:03 -0400119
120 /* If we are only performing partial GSO the inner header
121 * will be using a length value equal to only one MSS sized
122 * segment instead of the entire frame.
123 */
Alexey Kodanev3d0241d2017-10-06 19:02:35 +0300124 if (gso_partial && skb_is_gso(skb)) {
Alexander Duyck802ab552016-04-10 21:45:03 -0400125 uh->len = htons(skb_shinfo(skb)->gso_size +
126 SKB_GSO_CB(skb)->data_offset +
127 skb->head - (unsigned char *)uh);
128 } else {
129 uh->len = htons(len);
130 }
Tom Herbert155e0102014-07-13 19:49:56 -0700131
Tom Herbert4bcb8772014-11-04 09:06:52 -0800132 if (!need_csum)
133 continue;
Tom Herbert155e0102014-07-13 19:49:56 -0700134
Alexander Duyck802ab552016-04-10 21:45:03 -0400135 uh->check = ~csum_fold(csum_add(partial,
136 (__force __wsum)htonl(len)));
Alexander Duyckfdaefd62016-02-05 15:28:14 -0800137
138 if (skb->encapsulation || !offload_csum) {
139 uh->check = gso_make_checksum(skb, ~uh->check);
140 if (uh->check == 0)
141 uh->check = CSUM_MANGLED_0;
142 } else {
Tom Herbert4bcb8772014-11-04 09:06:52 -0800143 skb->ip_summed = CHECKSUM_PARTIAL;
144 skb->csum_start = skb_transport_header(skb) - skb->head;
145 skb->csum_offset = offsetof(struct udphdr, check);
Tom Herbert155e0102014-07-13 19:49:56 -0700146 }
Tom Herbert155e0102014-07-13 19:49:56 -0700147 } while ((skb = skb->next));
148out:
149 return segs;
150}
151
Tom Herbert8bce6d72014-09-29 20:22:29 -0700152struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
153 netdev_features_t features,
154 bool is_ipv6)
155{
156 __be16 protocol = skb->protocol;
157 const struct net_offload **offloads;
158 const struct net_offload *ops;
159 struct sk_buff *segs = ERR_PTR(-EINVAL);
160 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
161 netdev_features_t features);
162
163 rcu_read_lock();
164
165 switch (skb->inner_protocol_type) {
166 case ENCAP_TYPE_ETHER:
167 protocol = skb->inner_protocol;
168 gso_inner_segment = skb_mac_gso_segment;
169 break;
170 case ENCAP_TYPE_IPPROTO:
171 offloads = is_ipv6 ? inet6_offloads : inet_offloads;
172 ops = rcu_dereference(offloads[skb->inner_ipproto]);
173 if (!ops || !ops->callbacks.gso_segment)
174 goto out_unlock;
175 gso_inner_segment = ops->callbacks.gso_segment;
176 break;
177 default:
178 goto out_unlock;
179 }
180
181 segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
Tom Herbert4bcb8772014-11-04 09:06:52 -0800182 protocol, is_ipv6);
Tom Herbert8bce6d72014-09-29 20:22:29 -0700183
184out_unlock:
185 rcu_read_unlock();
186
187 return segs;
188}
Tom Herberta6024562016-04-05 08:22:51 -0700189EXPORT_SYMBOL(skb_udp_tunnel_segment);
Tom Herbert8bce6d72014-09-29 20:22:29 -0700190
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400191struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
Alexander Duyck9a0d41b2018-05-07 11:08:34 -0700192 netdev_features_t features)
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400193{
Willem de Bruijnad405852018-04-26 13:42:18 -0400194 struct sock *sk = gso_skb->sk;
195 unsigned int sum_truesize = 0;
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400196 struct sk_buff *segs, *seg;
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400197 struct udphdr *uh;
Alexander Duyckb21c0342018-05-07 11:08:28 -0700198 unsigned int mss;
Alexander Duyck04d55b22018-05-07 11:08:52 -0700199 bool copy_dtor;
Alexander Duyck9a0d41b2018-05-07 11:08:34 -0700200 __sum16 check;
201 __be16 newlen;
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400202
Alexander Duyckb21c0342018-05-07 11:08:28 -0700203 mss = skb_shinfo(gso_skb)->gso_size;
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400204 if (gso_skb->len <= sizeof(*uh) + mss)
205 return ERR_PTR(-EINVAL);
206
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400207 skb_pull(gso_skb, sizeof(*uh));
208
Willem de Bruijnad405852018-04-26 13:42:18 -0400209 /* clear destructor to avoid skb_segment assigning it to tail */
Alexander Duyck04d55b22018-05-07 11:08:52 -0700210 copy_dtor = gso_skb->destructor == sock_wfree;
211 if (copy_dtor)
212 gso_skb->destructor = NULL;
Willem de Bruijnad405852018-04-26 13:42:18 -0400213
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400214 segs = skb_segment(gso_skb, features);
Willem de Bruijnad405852018-04-26 13:42:18 -0400215 if (unlikely(IS_ERR_OR_NULL(segs))) {
Alexander Duyck04d55b22018-05-07 11:08:52 -0700216 if (copy_dtor)
217 gso_skb->destructor = sock_wfree;
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400218 return segs;
Willem de Bruijnad405852018-04-26 13:42:18 -0400219 }
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400220
Alexander Duyck6053d0f2018-05-07 11:08:46 -0700221 /* GSO partial and frag_list segmentation only requires splitting
222 * the frame into an MSS multiple and possibly a remainder, both
223 * cases return a GSO skb. So update the mss now.
224 */
225 if (skb_is_gso(segs))
226 mss *= skb_shinfo(segs)->gso_segs;
227
Alexander Duyck0ad65092018-05-07 11:08:40 -0700228 seg = segs;
229 uh = udp_hdr(seg);
Alexander Duyck9a0d41b2018-05-07 11:08:34 -0700230
231 /* compute checksum adjustment based on old length versus new */
232 newlen = htons(sizeof(*uh) + mss);
233 check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
234
Alexander Duyck0ad65092018-05-07 11:08:40 -0700235 for (;;) {
Alexander Duyck04d55b22018-05-07 11:08:52 -0700236 if (copy_dtor) {
237 seg->destructor = sock_wfree;
238 seg->sk = sk;
239 sum_truesize += seg->truesize;
240 }
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400241
Alexander Duyck0ad65092018-05-07 11:08:40 -0700242 if (!seg->next)
243 break;
Willem de Bruijnad405852018-04-26 13:42:18 -0400244
Alexander Duyck9a0d41b2018-05-07 11:08:34 -0700245 uh->len = newlen;
246 uh->check = check;
247
Alexander Duyck6053d0f2018-05-07 11:08:46 -0700248 if (seg->ip_summed == CHECKSUM_PARTIAL)
249 gso_reset_checksum(seg, ~check);
250 else
251 uh->check = gso_make_checksum(seg, ~check) ? :
252 CSUM_MANGLED_0;
253
Alexander Duyck0ad65092018-05-07 11:08:40 -0700254 seg = seg->next;
255 uh = udp_hdr(seg);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400256 }
257
Alexander Duyck0ad65092018-05-07 11:08:40 -0700258 /* last packet can be partial gso_size, account for that in checksum */
259 newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) +
260 seg->data_len);
261 check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
262
263 uh->len = newlen;
264 uh->check = check;
265
Alexander Duyck6053d0f2018-05-07 11:08:46 -0700266 if (seg->ip_summed == CHECKSUM_PARTIAL)
267 gso_reset_checksum(seg, ~check);
268 else
269 uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
270
Alexander Duyck0ad65092018-05-07 11:08:40 -0700271 /* update refcount for the packet */
Eric Dumazet575b65b2018-05-10 19:07:13 -0700272 if (copy_dtor) {
273 int delta = sum_truesize - gso_skb->truesize;
274
275 /* In some pathological cases, delta can be negative.
276 * We need to either use refcount_add() or refcount_sub_and_test()
277 */
278 if (likely(delta >= 0))
279 refcount_add(delta, &sk->sk_wmem_alloc);
280 else
281 WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
282 }
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400283 return segs;
284}
285EXPORT_SYMBOL_GPL(__udp_gso_segment);
286
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500287static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
288 netdev_features_t features)
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200289{
290 struct sk_buff *segs = ERR_PTR(-EINVAL);
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500291 unsigned int mss;
292 __wsum csum;
293 struct udphdr *uh;
294 struct iphdr *iph;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800295
296 if (skb->encapsulation &&
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700297 (skb_shinfo(skb)->gso_type &
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500298 (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
Tom Herbert8bce6d72014-09-29 20:22:29 -0700299 segs = skb_udp_tunnel_segment(skb, features, false);
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500300 goto out;
301 }
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200302
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400303 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
Willem de Bruijn121d57a2018-01-19 09:29:18 -0500304 goto out;
305
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500306 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
307 goto out;
308
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400309 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
Alexander Duyck6053d0f2018-05-07 11:08:46 -0700310 return __udp_gso_segment(skb, features);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -0400311
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500312 mss = skb_shinfo(skb)->gso_size;
313 if (unlikely(skb->len <= mss))
314 goto out;
315
316 /* Do software UFO. Complete and fill in the UDP checksum as
317 * HW cannot do checksum of UDP packets sent as multiple
318 * IP fragments.
319 */
320
321 uh = udp_hdr(skb);
322 iph = ip_hdr(skb);
323
324 uh->check = 0;
325 csum = skb_checksum(skb, 0, skb->len, 0);
326 uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
327 if (uh->check == 0)
328 uh->check = CSUM_MANGLED_0;
329
330 skb->ip_summed = CHECKSUM_UNNECESSARY;
331
332 /* If there is no outer header we can fake a checksum offload
333 * due to the fact that we have already done the checksum in
334 * software prior to segmenting the frame.
335 */
336 if (!skb->encap_hdr_csum)
337 features |= NETIF_F_HW_CSUM;
338
339 /* Fragment the skb. IP headers of the fragments are updated in
340 * inet_gso_segment()
341 */
342 segs = skb_segment(skb, features);
343out:
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200344 return segs;
345}
346
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100347#define UDP_GRO_CNT_MAX 64
348static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
349 struct sk_buff *skb)
350{
351 struct udphdr *uh = udp_hdr(skb);
352 struct sk_buff *pp = NULL;
353 struct udphdr *uh2;
354 struct sk_buff *p;
355
356 /* requires non zero csum, for symmetry with GSO */
357 if (!uh->check) {
358 NAPI_GRO_CB(skb)->flush = 1;
359 return NULL;
360 }
361
362 /* pull encapsulating udp header */
363 skb_gro_pull(skb, sizeof(struct udphdr));
364 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
365
366 list_for_each_entry(p, head, list) {
367 if (!NAPI_GRO_CB(p)->same_flow)
368 continue;
369
370 uh2 = udp_hdr(p);
371
372 /* Match ports only, as csum is always non zero */
373 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
374 NAPI_GRO_CB(p)->same_flow = 0;
375 continue;
376 }
377
378 /* Terminate the flow on len mismatch or if it grow "too much".
379 * Under small packet flood GRO count could elsewhere grow a lot
380 * leading to execessive truesize values
381 */
382 if (!skb_gro_receive(p, skb) &&
383 NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX)
384 pp = p;
385 else if (uh->len != uh2->len)
386 pp = p;
387
388 return pp;
389 }
390
391 /* mismatch, but we never need to flush */
392 return NULL;
393}
394
Paolo Abeni4f24ed72018-12-14 11:52:00 +0100395INDIRECT_CALLABLE_DECLARE(struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
396 __be16 sport, __be16 dport));
David Millerd4546c22018-06-24 14:13:49 +0900397struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
398 struct udphdr *uh, udp_lookup_t lookup)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200399{
David Millerd4546c22018-06-24 14:13:49 +0900400 struct sk_buff *pp = NULL;
401 struct sk_buff *p;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700402 struct udphdr *uh2;
403 unsigned int off = skb_gro_offset(skb);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200404 int flush = 1;
Tom Herberta6024562016-04-05 08:22:51 -0700405 struct sock *sk;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200406
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100407 rcu_read_lock();
Paolo Abeni4f24ed72018-12-14 11:52:00 +0100408 sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
409 udp4_lib_lookup_skb, skb, uh->source, uh->dest);
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100410 if (!sk)
411 goto out_unlock;
412
413 if (udp_sk(sk)->gro_enabled) {
414 pp = call_gro_receive(udp_gro_receive_segment, head, skb);
415 rcu_read_unlock();
416 return pp;
417 }
418
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700419 if (NAPI_GRO_CB(skb)->encap_mark ||
Tom Herbert662880f2014-08-27 21:26:56 -0700420 (skb->ip_summed != CHECKSUM_PARTIAL &&
421 NAPI_GRO_CB(skb)->csum_cnt == 0 &&
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100422 !NAPI_GRO_CB(skb)->csum_valid) ||
423 !udp_sk(sk)->gro_receive)
424 goto out_unlock;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200425
Jesse Grossfac8e0f2016-03-19 09:32:01 -0700426 /* mark that this skb passed once through the tunnel gro layer */
427 NAPI_GRO_CB(skb)->encap_mark = 1;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200428
Or Gerlitzb582ef02014-01-20 13:59:19 +0200429 flush = 0;
430
David Millerd4546c22018-06-24 14:13:49 +0900431 list_for_each_entry(p, head, list) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200432 if (!NAPI_GRO_CB(p)->same_flow)
433 continue;
434
435 uh2 = (struct udphdr *)(p->data + off);
Tom Herbert57c67ff2014-08-22 13:34:44 -0700436
437 /* Match ports and either checksums are either both zero
438 * or nonzero.
439 */
440 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
441 (!uh->check ^ !uh2->check)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200442 NAPI_GRO_CB(p)->same_flow = 0;
443 continue;
444 }
445 }
446
447 skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700448 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200449 pp = call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200450
451out_unlock:
452 rcu_read_unlock();
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200453 skb_gro_flush_final(skb, pp, flush);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200454 return pp;
455}
Tom Herberta6024562016-04-05 08:22:51 -0700456EXPORT_SYMBOL(udp_gro_receive);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200457
Paolo Abeni028e0a42018-12-14 11:51:59 +0100458INDIRECT_CALLABLE_SCOPE
459struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb)
Tom Herbert57c67ff2014-08-22 13:34:44 -0700460{
461 struct udphdr *uh = udp_gro_udphdr(skb);
462
Willem de Bruijnf2e9de22018-10-05 11:31:40 -0400463 if (unlikely(!uh) || !static_branch_unlikely(&udp_encap_needed_key))
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700464 goto flush;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700465
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700466 /* Don't bother verifying checksum if we're going to flush anyway. */
Scott Wood2d8f7e22014-09-10 21:23:18 -0500467 if (NAPI_GRO_CB(skb)->flush)
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700468 goto skip;
469
470 if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
471 inet_gro_compute_pseudo))
472 goto flush;
473 else if (uh->check)
474 skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
475 inet_gro_compute_pseudo);
476skip:
Tom Herbertefc98d02014-10-03 15:48:08 -0700477 NAPI_GRO_CB(skb)->is_ipv6 = 0;
Tom Herberta6024562016-04-05 08:22:51 -0700478 return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700479
480flush:
481 NAPI_GRO_CB(skb)->flush = 1;
482 return NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700483}
484
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100485static int udp_gro_complete_segment(struct sk_buff *skb)
486{
487 struct udphdr *uh = udp_hdr(skb);
488
489 skb->csum_start = (unsigned char *)uh - skb->head;
490 skb->csum_offset = offsetof(struct udphdr, check);
491 skb->ip_summed = CHECKSUM_PARTIAL;
492
493 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
494 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
495 return 0;
496}
497
Tom Herberta6024562016-04-05 08:22:51 -0700498int udp_gro_complete(struct sk_buff *skb, int nhoff,
499 udp_lookup_t lookup)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200500{
Or Gerlitzb582ef02014-01-20 13:59:19 +0200501 __be16 newlen = htons(skb->len - nhoff);
502 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
503 int err = -ENOSYS;
Tom Herberta6024562016-04-05 08:22:51 -0700504 struct sock *sk;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200505
506 uh->len = newlen;
507
508 rcu_read_lock();
Paolo Abeni4f24ed72018-12-14 11:52:00 +0100509 sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
510 udp4_lib_lookup_skb, skb, uh->source, uh->dest);
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100511 if (sk && udp_sk(sk)->gro_enabled) {
512 err = udp_gro_complete_segment(skb);
513 } else if (sk && udp_sk(sk)->gro_complete) {
514 skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM
515 : SKB_GSO_UDP_TUNNEL;
516
517 /* Set encapsulation before calling into inner gro_complete()
518 * functions to make them set up the inner offsets.
519 */
520 skb->encapsulation = 1;
Tom Herberta6024562016-04-05 08:22:51 -0700521 err = udp_sk(sk)->gro_complete(sk, skb,
522 nhoff + sizeof(struct udphdr));
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100523 }
Or Gerlitzb582ef02014-01-20 13:59:19 +0200524 rcu_read_unlock();
Tom Herbert6db93ea2015-02-10 16:30:29 -0800525
526 if (skb->remcsum_offload)
527 skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
528
Or Gerlitzb582ef02014-01-20 13:59:19 +0200529 return err;
530}
Tom Herberta6024562016-04-05 08:22:51 -0700531EXPORT_SYMBOL(udp_gro_complete);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200532
Paolo Abeni028e0a42018-12-14 11:51:59 +0100533INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
Tom Herbert57c67ff2014-08-22 13:34:44 -0700534{
535 const struct iphdr *iph = ip_hdr(skb);
536 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
537
Paolo Abenie20cf8d2018-11-07 12:38:29 +0100538 if (uh->check)
Tom Herbert57c67ff2014-08-22 13:34:44 -0700539 uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
540 iph->daddr, 0);
541
Tom Herberta6024562016-04-05 08:22:51 -0700542 return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
Tom Herbert57c67ff2014-08-22 13:34:44 -0700543}
544
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200545static const struct net_offload udpv4_offload = {
546 .callbacks = {
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500547 .gso_segment = udp4_ufo_fragment,
Tom Herbert57c67ff2014-08-22 13:34:44 -0700548 .gro_receive = udp4_gro_receive,
549 .gro_complete = udp4_gro_complete,
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200550 },
551};
552
553int __init udpv4_offload_init(void)
554{
555 return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
556}