xref: /linux/net/ipv4/udp_offload.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPV4 GSO/GRO offload support
4  *	Linux INET implementation
5  *
6  *	UDPv4 GSO support
7  */
8 
9 #include <linux/skbuff.h>
10 #include <net/gro.h>
11 #include <net/gso.h>
12 #include <net/udp.h>
13 #include <net/protocol.h>
14 #include <net/inet_common.h>
15 #include <net/udp_tunnel.h>
16 
17 #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL)
18 
19 /*
20  * Dummy GRO tunnel callback, exists mainly to avoid dangling/NULL
21  * values for the udp tunnel static call.
22  */
23 static struct sk_buff *dummy_gro_rcv(struct sock *sk,
24 				     struct list_head *head,
25 				     struct sk_buff *skb)
26 {
27 	NAPI_GRO_CB(skb)->flush = 1;
28 	return NULL;
29 }
30 
31 typedef struct sk_buff *(*udp_tunnel_gro_rcv_t)(struct sock *sk,
32 						struct list_head *head,
33 						struct sk_buff *skb);
34 
35 struct udp_tunnel_type_entry {
36 	udp_tunnel_gro_rcv_t gro_receive;
37 	refcount_t count;
38 };
39 
40 #define UDP_MAX_TUNNEL_TYPES (IS_ENABLED(CONFIG_GENEVE) + \
41 			      IS_ENABLED(CONFIG_VXLAN) * 2 + \
42 			      IS_ENABLED(CONFIG_NET_FOU) * 2 + \
43 			      IS_ENABLED(CONFIG_XFRM) * 2)
44 
45 DEFINE_STATIC_CALL(udp_tunnel_gro_rcv, dummy_gro_rcv);
46 static DEFINE_STATIC_KEY_FALSE(udp_tunnel_static_call);
47 static DEFINE_MUTEX(udp_tunnel_gro_type_lock);
48 static struct udp_tunnel_type_entry udp_tunnel_gro_types[UDP_MAX_TUNNEL_TYPES];
49 static unsigned int udp_tunnel_gro_type_nr;
50 static DEFINE_SPINLOCK(udp_tunnel_gro_lock);
51 
52 void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add)
53 {
54 	bool is_ipv6 = sk->sk_family == AF_INET6;
55 	struct udp_sock *tup, *up = udp_sk(sk);
56 	struct udp_tunnel_gro *udp_tunnel_gro;
57 
58 	spin_lock(&udp_tunnel_gro_lock);
59 	udp_tunnel_gro = &net->ipv4.udp_tunnel_gro[is_ipv6];
60 	if (add)
61 		hlist_add_head(&up->tunnel_list, &udp_tunnel_gro->list);
62 	else if (up->tunnel_list.pprev)
63 		hlist_del_init(&up->tunnel_list);
64 
65 	if (udp_tunnel_gro->list.first &&
66 	    !udp_tunnel_gro->list.first->next) {
67 		tup = hlist_entry(udp_tunnel_gro->list.first, struct udp_sock,
68 				  tunnel_list);
69 
70 		rcu_assign_pointer(udp_tunnel_gro->sk, (struct sock *)tup);
71 	} else {
72 		RCU_INIT_POINTER(udp_tunnel_gro->sk, NULL);
73 	}
74 
75 	spin_unlock(&udp_tunnel_gro_lock);
76 }
77 EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_lookup);
78 
79 void udp_tunnel_update_gro_rcv(struct sock *sk, bool add)
80 {
81 	struct udp_tunnel_type_entry *cur = NULL;
82 	struct udp_sock *up = udp_sk(sk);
83 	int i, old_gro_type_nr;
84 
85 	if (!UDP_MAX_TUNNEL_TYPES || !up->gro_receive)
86 		return;
87 
88 	mutex_lock(&udp_tunnel_gro_type_lock);
89 
90 	/* Check if the static call is permanently disabled. */
91 	if (udp_tunnel_gro_type_nr > UDP_MAX_TUNNEL_TYPES)
92 		goto out;
93 
94 	for (i = 0; i < udp_tunnel_gro_type_nr; i++)
95 		if (udp_tunnel_gro_types[i].gro_receive == up->gro_receive)
96 			cur = &udp_tunnel_gro_types[i];
97 
98 	old_gro_type_nr = udp_tunnel_gro_type_nr;
99 	if (add) {
100 		/*
101 		 * Update the matching entry, if found, or add a new one
102 		 * if needed
103 		 */
104 		if (cur) {
105 			refcount_inc(&cur->count);
106 			goto out;
107 		}
108 
109 		if (unlikely(udp_tunnel_gro_type_nr == UDP_MAX_TUNNEL_TYPES)) {
110 			pr_err_once("Too many UDP tunnel types, please increase UDP_MAX_TUNNEL_TYPES\n");
111 			/* Ensure static call will never be enabled */
112 			udp_tunnel_gro_type_nr = UDP_MAX_TUNNEL_TYPES + 1;
113 		} else {
114 			cur = &udp_tunnel_gro_types[udp_tunnel_gro_type_nr++];
115 			refcount_set(&cur->count, 1);
116 			cur->gro_receive = up->gro_receive;
117 		}
118 	} else {
119 		/*
120 		 * The stack cleanups only successfully added tunnel, the
121 		 * lookup on removal should never fail.
122 		 */
123 		if (WARN_ON_ONCE(!cur))
124 			goto out;
125 
126 		if (!refcount_dec_and_test(&cur->count))
127 			goto out;
128 
129 		/* Avoid gaps, so that the enable tunnel has always id 0 */
130 		*cur = udp_tunnel_gro_types[--udp_tunnel_gro_type_nr];
131 	}
132 
133 	if (udp_tunnel_gro_type_nr == 1) {
134 		static_call_update(udp_tunnel_gro_rcv,
135 				   udp_tunnel_gro_types[0].gro_receive);
136 		static_branch_enable(&udp_tunnel_static_call);
137 	} else if (old_gro_type_nr == 1) {
138 		static_branch_disable(&udp_tunnel_static_call);
139 		static_call_update(udp_tunnel_gro_rcv, dummy_gro_rcv);
140 	}
141 
142 out:
143 	mutex_unlock(&udp_tunnel_gro_type_lock);
144 }
145 EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_rcv);
146 
147 static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk,
148 					  struct list_head *head,
149 					  struct sk_buff *skb)
150 {
151 	if (static_branch_likely(&udp_tunnel_static_call)) {
152 		if (unlikely(gro_recursion_inc_test(skb))) {
153 			NAPI_GRO_CB(skb)->flush |= 1;
154 			return NULL;
155 		}
156 		return static_call(udp_tunnel_gro_rcv)(sk, head, skb);
157 	}
158 	return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
159 }
160 
161 #else
162 
163 static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk,
164 					  struct list_head *head,
165 					  struct sk_buff *skb)
166 {
167 	return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb);
168 }
169 
170 #endif
171 
172 static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
173 	netdev_features_t features,
174 	struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
175 					     netdev_features_t features),
176 	__be16 new_protocol, bool is_ipv6)
177 {
178 	int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
179 	bool remcsum, need_csum, offload_csum, gso_partial;
180 	struct sk_buff *segs = ERR_PTR(-EINVAL);
181 	u16 mac_offset = skb->mac_header;
182 	__be16 protocol = skb->protocol;
183 	u16 mac_len = skb->mac_len;
184 	int udp_offset, outer_hlen;
185 	struct udphdr *uh;
186 	__wsum partial;
187 	bool need_ipsec;
188 
189 	if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
190 		goto out;
191 
192 	uh = udp_hdr(skb);
193 
194 	/* Adjust partial header checksum to negate old length.
195 	 * We cannot rely on the value contained in uh->len as it is
196 	 * possible that the actual value exceeds the boundaries of the
197 	 * 16 bit length field due to the header being added outside of an
198 	 * IP or IPv6 frame that was already limited to 64K - 1.
199 	 */
200 	if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)
201 		partial = (__force __wsum)uh->len;
202 	else
203 		partial = (__force __wsum)htonl(skb->len);
204 	partial = csum_sub(csum_unfold(uh->check), partial);
205 
206 	/* setup inner skb. */
207 	skb->encapsulation = 0;
208 	SKB_GSO_CB(skb)->encap_level = 0;
209 	__skb_pull(skb, tnl_hlen);
210 	skb_reset_mac_header(skb);
211 	skb_set_network_header(skb, skb_inner_network_offset(skb));
212 	skb_set_transport_header(skb, skb_inner_transport_offset(skb));
213 	skb->mac_len = skb_inner_network_offset(skb);
214 	skb->protocol = new_protocol;
215 
216 	need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
217 	skb->encap_hdr_csum = need_csum;
218 
219 	remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
220 	skb->remcsum_offload = remcsum;
221 
222 	need_ipsec = (skb_dst(skb) && dst_xfrm(skb_dst(skb))) || skb_sec_path(skb);
223 	/* Try to offload checksum if possible */
224 	offload_csum = !!(need_csum &&
225 			  !need_ipsec &&
226 			  (skb->dev->features &
227 			   (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
228 				      (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
229 
230 	features &= skb->dev->hw_enc_features;
231 	if (need_csum)
232 		features &= ~NETIF_F_SCTP_CRC;
233 
234 	/* The only checksum offload we care about from here on out is the
235 	 * outer one so strip the existing checksum feature flags and
236 	 * instead set the flag based on our outer checksum offload value.
237 	 */
238 	if (remcsum) {
239 		features &= ~NETIF_F_CSUM_MASK;
240 		if (!need_csum || offload_csum)
241 			features |= NETIF_F_HW_CSUM;
242 	}
243 
244 	/* segment inner packet. */
245 	segs = gso_inner_segment(skb, features);
246 	if (IS_ERR_OR_NULL(segs)) {
247 		skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
248 				     mac_len);
249 		goto out;
250 	}
251 
252 	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
253 
254 	outer_hlen = skb_tnl_header_len(skb);
255 	udp_offset = outer_hlen - tnl_hlen;
256 	skb = segs;
257 	do {
258 		unsigned int len;
259 
260 		if (remcsum)
261 			skb->ip_summed = CHECKSUM_NONE;
262 
263 		/* Set up inner headers if we are offloading inner checksum */
264 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
265 			skb_reset_inner_headers(skb);
266 			skb->encapsulation = 1;
267 		}
268 
269 		skb->mac_len = mac_len;
270 		skb->protocol = protocol;
271 
272 		__skb_push(skb, outer_hlen);
273 		skb_reset_mac_header(skb);
274 		skb_set_network_header(skb, mac_len);
275 		skb_set_transport_header(skb, udp_offset);
276 		len = skb->len - udp_offset;
277 		uh = udp_hdr(skb);
278 
279 		/* If we are only performing partial GSO the inner header
280 		 * will be using a length value equal to only one MSS sized
281 		 * segment instead of the entire frame.
282 		 */
283 		if (gso_partial && skb_is_gso(skb)) {
284 			udp_set_len_short(uh, skb_shinfo(skb)->gso_size +
285 					  SKB_GSO_CB(skb)->data_offset +
286 					  skb->head - (unsigned char *)uh);
287 		} else {
288 			udp_set_len_short(uh, len);
289 		}
290 
291 		if (!need_csum)
292 			continue;
293 
294 		uh->check = ~csum_fold(csum_add(partial,
295 				       (__force __wsum)htonl(len)));
296 
297 		if (skb->encapsulation || !offload_csum) {
298 			uh->check = gso_make_checksum(skb, ~uh->check);
299 			if (uh->check == 0)
300 				uh->check = CSUM_MANGLED_0;
301 		} else {
302 			skb->ip_summed = CHECKSUM_PARTIAL;
303 			skb->csum_start = skb_transport_header(skb) - skb->head;
304 			skb->csum_offset = offsetof(struct udphdr, check);
305 		}
306 	} while ((skb = skb->next));
307 out:
308 	return segs;
309 }
310 
311 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
312 				       netdev_features_t features,
313 				       bool is_ipv6)
314 {
315 	const struct net_offload __rcu **offloads;
316 	__be16 protocol = skb->protocol;
317 	const struct net_offload *ops;
318 	struct sk_buff *segs = ERR_PTR(-EINVAL);
319 	struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
320 					     netdev_features_t features);
321 
322 	rcu_read_lock();
323 
324 	switch (skb->inner_protocol_type) {
325 	case ENCAP_TYPE_ETHER:
326 		protocol = skb->inner_protocol;
327 		gso_inner_segment = skb_mac_gso_segment;
328 		break;
329 	case ENCAP_TYPE_IPPROTO:
330 		offloads = is_ipv6 ? inet6_offloads : inet_offloads;
331 		ops = rcu_dereference(offloads[skb->inner_ipproto]);
332 		if (!ops || !ops->callbacks.gso_segment)
333 			goto out_unlock;
334 		gso_inner_segment = ops->callbacks.gso_segment;
335 		break;
336 	default:
337 		goto out_unlock;
338 	}
339 
340 	segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
341 					protocol, is_ipv6);
342 
343 out_unlock:
344 	rcu_read_unlock();
345 
346 	return segs;
347 }
348 
349 static void __udpv4_gso_segment_csum(struct sk_buff *seg,
350 				     __be32 *oldip, __be32 *newip,
351 				     __be16 *oldport, __be16 *newport)
352 {
353 	struct udphdr *uh;
354 	struct iphdr *iph;
355 
356 	if (*oldip == *newip && *oldport == *newport)
357 		return;
358 
359 	uh = udp_hdr(seg);
360 	iph = ip_hdr(seg);
361 
362 	if (uh->check) {
363 		inet_proto_csum_replace4(&uh->check, seg, *oldip, *newip,
364 					 true);
365 		inet_proto_csum_replace2(&uh->check, seg, *oldport, *newport,
366 					 false);
367 		if (!uh->check)
368 			uh->check = CSUM_MANGLED_0;
369 	}
370 	*oldport = *newport;
371 
372 	csum_replace4(&iph->check, *oldip, *newip);
373 	*oldip = *newip;
374 }
375 
376 static struct sk_buff *__udpv4_gso_segment_list_csum(struct sk_buff *segs)
377 {
378 	struct sk_buff *seg;
379 	struct udphdr *uh, *uh2;
380 	struct iphdr *iph, *iph2;
381 
382 	seg = segs;
383 	uh = udp_hdr(seg);
384 	iph = ip_hdr(seg);
385 
386 	if ((udp_hdr(seg)->dest == udp_hdr(seg->next)->dest) &&
387 	    (udp_hdr(seg)->source == udp_hdr(seg->next)->source) &&
388 	    (ip_hdr(seg)->daddr == ip_hdr(seg->next)->daddr) &&
389 	    (ip_hdr(seg)->saddr == ip_hdr(seg->next)->saddr))
390 		return segs;
391 
392 	while ((seg = seg->next)) {
393 		uh2 = udp_hdr(seg);
394 		iph2 = ip_hdr(seg);
395 
396 		__udpv4_gso_segment_csum(seg,
397 					 &iph2->saddr, &iph->saddr,
398 					 &uh2->source, &uh->source);
399 		__udpv4_gso_segment_csum(seg,
400 					 &iph2->daddr, &iph->daddr,
401 					 &uh2->dest, &uh->dest);
402 	}
403 
404 	return segs;
405 }
406 
407 static void __udpv6_gso_segment_csum(struct sk_buff *seg,
408 				     struct in6_addr *oldip,
409 				     const struct in6_addr *newip,
410 				     __be16 *oldport, __be16 newport)
411 {
412 	struct udphdr *uh = udp_hdr(seg);
413 
414 	if (ipv6_addr_equal(oldip, newip) && *oldport == newport)
415 		return;
416 
417 	if (uh->check) {
418 		inet_proto_csum_replace16(&uh->check, seg, oldip->s6_addr32,
419 					  newip->s6_addr32, true);
420 
421 		inet_proto_csum_replace2(&uh->check, seg, *oldport, newport,
422 					 false);
423 		if (!uh->check)
424 			uh->check = CSUM_MANGLED_0;
425 	}
426 
427 	*oldip = *newip;
428 	*oldport = newport;
429 }
430 
431 static struct sk_buff *__udpv6_gso_segment_list_csum(struct sk_buff *segs)
432 {
433 	const struct ipv6hdr *iph;
434 	const struct udphdr *uh;
435 	struct ipv6hdr *iph2;
436 	struct sk_buff *seg;
437 	struct udphdr *uh2;
438 
439 	seg = segs;
440 	uh = udp_hdr(seg);
441 	iph = ipv6_hdr(seg);
442 	uh2 = udp_hdr(seg->next);
443 	iph2 = ipv6_hdr(seg->next);
444 
445 	if (!(*(const u32 *)&uh->source ^ *(const u32 *)&uh2->source) &&
446 	    ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
447 	    ipv6_addr_equal(&iph->daddr, &iph2->daddr))
448 		return segs;
449 
450 	while ((seg = seg->next)) {
451 		uh2 = udp_hdr(seg);
452 		iph2 = ipv6_hdr(seg);
453 
454 		__udpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
455 					 &uh2->source, uh->source);
456 		__udpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
457 					 &uh2->dest, uh->dest);
458 	}
459 
460 	return segs;
461 }
462 
463 static struct sk_buff *__udp_gso_segment_list(struct sk_buff *skb,
464 					      netdev_features_t features,
465 					      bool is_ipv6)
466 {
467 	unsigned int mss = skb_shinfo(skb)->gso_size;
468 
469 	skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
470 	if (IS_ERR(skb))
471 		return skb;
472 
473 	udp_set_len_short(udp_hdr(skb), sizeof(struct udphdr) + mss);
474 
475 	if (is_ipv6)
476 		return __udpv6_gso_segment_list_csum(skb);
477 	else
478 		return __udpv4_gso_segment_list_csum(skb);
479 }
480 
481 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
482 				  netdev_features_t features, bool is_ipv6)
483 {
484 	struct sock *sk = gso_skb->sk;
485 	unsigned int sum_truesize = 0;
486 	struct sk_buff *segs, *seg;
487 	struct udphdr *uh;
488 	unsigned int mss;
489 	bool copy_dtor;
490 	__sum16 check;
491 	int ret = 0;
492 	u16 newlen;
493 
494 	mss = skb_shinfo(gso_skb)->gso_size;
495 	if (gso_skb->len <= sizeof(*uh) + mss)
496 		return ERR_PTR(-EINVAL);
497 
498 	if (unlikely(skb_checksum_start(gso_skb) !=
499 		     skb_transport_header(gso_skb) &&
500 		     !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST)))
501 		return ERR_PTR(-EINVAL);
502 
503 	/* We don't know if egress device can segment and checksum the packet
504 	 * when IPv6 extension headers are present. Fall back to software GSO.
505 	 */
506 	if (gso_skb->ip_summed != CHECKSUM_PARTIAL)
507 		features &= ~(NETIF_F_GSO_UDP_L4 | NETIF_F_CSUM_MASK);
508 
509 	if (skb_gso_ok(gso_skb, features | NETIF_F_GSO_ROBUST)) {
510 		/* Packet is from an untrusted source, reset gso_segs. */
511 		skb_shinfo(gso_skb)->gso_segs = DIV_ROUND_UP(gso_skb->len - sizeof(*uh),
512 							     mss);
513 		return NULL;
514 	}
515 
516 	if (skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST) {
517 		/* Detect modified geometry and pass those to skb_segment.
518 		 * SKB_GSO_UDP_L4 is used by both IPv4 and IPv6: also make sure
519 		 * the frag_list skbs still have the IP family we segment for,
520 		 * as their network headers are not rebuilt.
521 		 */
522 		if ((skb_pagelen(gso_skb) - sizeof(*uh) == skb_shinfo(gso_skb)->gso_size) &&
523 		    !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_DODGY) &&
524 		    ip_hdr(skb_shinfo(gso_skb)->frag_list)->version == (is_ipv6 ? 6 : 4))
525 			return __udp_gso_segment_list(gso_skb, features, is_ipv6);
526 
527 		ret = __skb_linearize(gso_skb);
528 		if (ret)
529 			return ERR_PTR(ret);
530 
531 		 /* Setup csum, as fraglist skips this in udp4_gro_receive. */
532 		gso_skb->csum_start = skb_transport_header(gso_skb) - gso_skb->head;
533 		gso_skb->csum_offset = offsetof(struct udphdr, check);
534 		gso_skb->ip_summed = CHECKSUM_PARTIAL;
535 
536 		uh = udp_hdr(gso_skb);
537 		if (is_ipv6)
538 			uh->check = ~udp_v6_check(gso_skb->len,
539 						  &ipv6_hdr(gso_skb)->saddr,
540 						  &ipv6_hdr(gso_skb)->daddr, 0);
541 		else
542 			uh->check = ~udp_v4_check(gso_skb->len,
543 						  ip_hdr(gso_skb)->saddr,
544 						  ip_hdr(gso_skb)->daddr, 0);
545 	}
546 
547 	skb_pull(gso_skb, sizeof(*uh));
548 
549 	/* clear destructor to avoid skb_segment assigning it to tail */
550 	copy_dtor = gso_skb->destructor == sock_wfree;
551 	if (copy_dtor) {
552 		gso_skb->destructor = NULL;
553 		gso_skb->sk = NULL;
554 	}
555 
556 	segs = skb_segment(gso_skb, features);
557 	if (IS_ERR_OR_NULL(segs)) {
558 		if (copy_dtor) {
559 			gso_skb->destructor = sock_wfree;
560 			gso_skb->sk = sk;
561 		}
562 		return segs;
563 	}
564 
565 	seg = segs;
566 	uh = udp_hdr(seg);
567 
568 	/* preserve TX timestamp flags and TS key for first segment */
569 	skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey;
570 	skb_shinfo(seg)->tx_flags |=
571 			(skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP);
572 
573 	/* compute checksum adjustment based on old length versus new */
574 	newlen = sizeof(*uh) + mss;
575 	check = csum16_add(csum16_sub(uh->check, uh->len), htons(newlen));
576 
577 	for (;;) {
578 		if (copy_dtor) {
579 			seg->destructor = sock_wfree;
580 			seg->sk = sk;
581 			sum_truesize += seg->truesize;
582 		}
583 
584 		if (!seg->next)
585 			break;
586 
587 		udp_set_len_short(uh, newlen);
588 		uh->check = check;
589 
590 		if (seg->ip_summed == CHECKSUM_PARTIAL)
591 			gso_reset_checksum(seg, ~check);
592 		else
593 			uh->check = gso_make_checksum(seg, ~check) ? :
594 				    CSUM_MANGLED_0;
595 
596 		seg = seg->next;
597 		uh = udp_hdr(seg);
598 	}
599 
600 	/* Unless skb fits perfectly as GSO_PARTIAL, the trailing
601 	 * segment may not be full MSS, account for that in the checksum
602 	 */
603 	if (!skb_is_gso(seg))
604 		newlen = skb_tail_pointer(seg) - skb_transport_header(seg) + seg->data_len;
605 	check = csum16_add(csum16_sub(uh->check, uh->len), htons(newlen));
606 
607 	udp_set_len_short(uh, newlen);
608 	uh->check = check;
609 
610 	if (seg->ip_summed == CHECKSUM_PARTIAL)
611 		gso_reset_checksum(seg, ~check);
612 	else
613 		uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
614 
615 	/* On the TX path, CHECKSUM_NONE and CHECKSUM_UNNECESSARY have the same
616 	 * meaning. However, check for bad offloads in the GSO stack expects the
617 	 * latter, if the checksum was calculated in software. To vouch for the
618 	 * segment skbs we actually need to set it on the gso_skb.
619 	 */
620 	if (gso_skb->ip_summed == CHECKSUM_NONE)
621 		gso_skb->ip_summed = CHECKSUM_UNNECESSARY;
622 
623 	/* update refcount for the packet */
624 	if (copy_dtor) {
625 		int delta = sum_truesize - gso_skb->truesize;
626 
627 		/* In some pathological cases, delta can be negative.
628 		 * We need to either use refcount_add() or refcount_sub_and_test()
629 		 */
630 		if (likely(delta >= 0))
631 			refcount_add(delta, &sk->sk_wmem_alloc);
632 		else
633 			WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
634 	}
635 	return segs;
636 }
637 
638 static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
639 					 netdev_features_t features)
640 {
641 	struct sk_buff *segs = ERR_PTR(-EINVAL);
642 	unsigned int mss;
643 	__wsum csum;
644 	struct udphdr *uh;
645 	struct iphdr *iph;
646 
647 	if (skb->encapsulation &&
648 	    (skb_shinfo(skb)->gso_type &
649 	     (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
650 		segs = skb_udp_tunnel_segment(skb, features, false);
651 		goto out;
652 	}
653 
654 	if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
655 		goto out;
656 
657 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
658 		goto out;
659 
660 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
661 		return __udp_gso_segment(skb, features, false);
662 
663 	mss = skb_shinfo(skb)->gso_size;
664 	if (unlikely(skb->len <= mss))
665 		goto out;
666 
667 	/* Do software UFO. Complete and fill in the UDP checksum as
668 	 * HW cannot do checksum of UDP packets sent as multiple
669 	 * IP fragments.
670 	 */
671 
672 	uh = udp_hdr(skb);
673 	iph = ip_hdr(skb);
674 
675 	uh->check = 0;
676 	csum = skb_checksum(skb, 0, skb->len, 0);
677 	uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
678 	if (uh->check == 0)
679 		uh->check = CSUM_MANGLED_0;
680 
681 	skb->ip_summed = CHECKSUM_UNNECESSARY;
682 
683 	/* If there is no outer header we can fake a checksum offload
684 	 * due to the fact that we have already done the checksum in
685 	 * software prior to segmenting the frame.
686 	 */
687 	if (!skb->encap_hdr_csum)
688 		features |= NETIF_F_HW_CSUM;
689 
690 	/* Fragment the skb. IP headers of the fragments are updated in
691 	 * inet_gso_segment()
692 	 */
693 	segs = skb_segment(skb, features);
694 out:
695 	return segs;
696 }
697 
698 
699 #define UDP_GRO_CNT_MAX 64
700 static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
701 					       struct sk_buff *skb)
702 {
703 	struct udphdr *uh = udp_gro_udphdr(skb);
704 	struct sk_buff *pp = NULL;
705 	struct udphdr *uh2;
706 	struct sk_buff *p;
707 	unsigned int ulen;
708 	int ret = 0;
709 	int flush;
710 
711 	/* requires non zero csum, for symmetry with GSO */
712 	if (!uh->check) {
713 		NAPI_GRO_CB(skb)->flush = 1;
714 		return NULL;
715 	}
716 
717 	ulen = udp_get_len_short(uh);
718 
719 	/* pull encapsulating udp header */
720 	skb_gro_pull(skb, sizeof(struct udphdr));
721 
722 	list_for_each_entry(p, head, list) {
723 		if (!NAPI_GRO_CB(p)->same_flow)
724 			continue;
725 
726 		uh2 = udp_hdr(p);
727 
728 		/* Match ports only, as csum is always non zero */
729 		if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
730 			NAPI_GRO_CB(p)->same_flow = 0;
731 			continue;
732 		}
733 
734 		if (NAPI_GRO_CB(skb)->is_flist != NAPI_GRO_CB(p)->is_flist) {
735 			NAPI_GRO_CB(skb)->flush = 1;
736 			return p;
737 		}
738 
739 		flush = gro_receive_network_flush(uh, uh2, p);
740 
741 		/* Terminate the flow on len mismatch or if it grow "too much".
742 		 * Under small packet flood GRO count could elsewhere grow a lot
743 		 * leading to excessive truesize values.
744 		 * On len mismatch merge the first packet shorter than gso_size,
745 		 * otherwise complete the GRO packet.
746 		 */
747 		if (ulen > udp_get_len_short(uh2) || flush) {
748 			pp = p;
749 		} else {
750 			if (NAPI_GRO_CB(skb)->is_flist) {
751 				skb_gro_frag0_invalidate(skb);
752 				if (!pskb_may_pull(skb, skb_gro_offset(skb))) {
753 					NAPI_GRO_CB(skb)->flush = 1;
754 					return NULL;
755 				}
756 				if ((skb->ip_summed != p->ip_summed) ||
757 				    (skb->csum_level != p->csum_level)) {
758 					NAPI_GRO_CB(skb)->flush = 1;
759 					return NULL;
760 				}
761 				skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
762 				ret = skb_gro_receive_list(p, skb);
763 			} else {
764 				skb_gro_postpull_rcsum(skb, uh,
765 						       sizeof(struct udphdr));
766 
767 				ret = skb_gro_receive(p, skb);
768 			}
769 		}
770 
771 		if (ret || ulen != udp_get_len_short(uh2) ||
772 		    NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX)
773 			pp = p;
774 
775 		return pp;
776 	}
777 
778 	/* mismatch, but we never need to flush */
779 	return NULL;
780 }
781 
782 struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
783 				struct udphdr *uh, struct sock *sk)
784 {
785 	struct sk_buff *pp = NULL;
786 	struct sk_buff *p;
787 	struct udphdr *uh2;
788 	unsigned int off = skb_gro_offset(skb);
789 	unsigned int ulen;
790 	int flush = 1;
791 
792 	/* Do not deal with padded or malicious packets, sorry! */
793 	ulen = udp_get_len_short(uh);
794 	if (ulen <= sizeof(*uh) || ulen != skb_gro_len(skb))
795 		goto out;
796 
797 	/* We can do L4 aggregation only if the packet can't land in a tunnel
798 	 * otherwise we could corrupt the inner stream. Detecting such packets
799 	 * cannot be foolproof and the aggregation might still happen in some
800 	 * cases. Such packets should be caught in udp_unexpected_gso later.
801 	 */
802 	NAPI_GRO_CB(skb)->is_flist = 0;
803 	if (!sk || !udp_sk(sk)->gro_receive) {
804 		/* If the packet was locally encapsulated in a UDP tunnel that
805 		 * wasn't detected above, do not GRO.
806 		 */
807 		if (skb->encapsulation)
808 			goto out;
809 
810 		if (skb->dev->features & NETIF_F_GRO_FRAGLIST)
811 			NAPI_GRO_CB(skb)->is_flist = sk ? !udp_test_bit(GRO_ENABLED, sk) : 1;
812 
813 		if ((!sk && (skb->dev->features & NETIF_F_GRO_UDP_FWD)) ||
814 		    (sk && udp_test_bit(GRO_ENABLED, sk)) || NAPI_GRO_CB(skb)->is_flist)
815 			return call_gro_receive(udp_gro_receive_segment, head, skb);
816 
817 		/* no GRO, be sure flush the current packet */
818 		goto out;
819 	}
820 
821 	if (NAPI_GRO_CB(skb)->encap_mark ||
822 	    (uh->check && skb->ip_summed != CHECKSUM_PARTIAL &&
823 	     NAPI_GRO_CB(skb)->csum_cnt == 0 &&
824 	     !NAPI_GRO_CB(skb)->csum_valid))
825 		goto out;
826 
827 	/* mark that this skb passed once through the tunnel gro layer */
828 	NAPI_GRO_CB(skb)->encap_mark = 1;
829 
830 	flush = 0;
831 
832 	list_for_each_entry(p, head, list) {
833 		if (!NAPI_GRO_CB(p)->same_flow)
834 			continue;
835 
836 		uh2 = (struct udphdr   *)(p->data + off);
837 
838 		/* Match ports and either checksums are either both zero
839 		 * or nonzero.
840 		 */
841 		if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
842 		    (!uh->check ^ !uh2->check)) {
843 			NAPI_GRO_CB(p)->same_flow = 0;
844 			continue;
845 		}
846 	}
847 
848 	skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
849 	skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
850 	pp = udp_tunnel_gro_rcv(sk, head, skb);
851 
852 out:
853 	skb_gro_flush_final(skb, pp, flush);
854 	return pp;
855 }
856 
857 static struct sock *udp4_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
858 					__be16 dport)
859 {
860 	const struct iphdr *iph = skb_gro_network_header(skb);
861 	struct net *net = dev_net_rcu(skb->dev);
862 	struct sock *sk;
863 	int iif, sdif;
864 
865 	sk = udp_tunnel_sk(net, false);
866 	if (sk && dport == htons(sk->sk_num))
867 		return sk;
868 
869 	inet_get_iif_sdif(skb, &iif, &sdif);
870 
871 	return __udp4_lib_lookup(net, iph->saddr, sport,
872 				 iph->daddr, dport, iif, sdif, NULL);
873 }
874 
875 INDIRECT_CALLABLE_SCOPE
876 struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb)
877 {
878 	struct udphdr *uh = udp_gro_udphdr(skb);
879 	struct sock *sk = NULL;
880 	struct sk_buff *pp;
881 
882 	if (unlikely(!uh))
883 		goto flush;
884 
885 	/* Don't bother verifying checksum if we're going to flush anyway. */
886 	if (NAPI_GRO_CB(skb)->flush)
887 		goto skip;
888 
889 	if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
890 						 inet_gro_compute_pseudo))
891 		goto flush;
892 	else if (uh->check)
893 		skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
894 					     inet_gro_compute_pseudo);
895 skip:
896 	if (static_branch_unlikely(&udp_encap_needed_key))
897 		sk = udp4_gro_lookup_skb(skb, uh->source, uh->dest);
898 
899 	pp = udp_gro_receive(head, skb, uh, sk);
900 	return pp;
901 
902 flush:
903 	NAPI_GRO_CB(skb)->flush = 1;
904 	return NULL;
905 }
906 
907 static int udp_gro_complete_segment(struct sk_buff *skb)
908 {
909 	struct udphdr *uh = udp_hdr(skb);
910 
911 	skb->csum_start = (unsigned char *)uh - skb->head;
912 	skb->csum_offset = offsetof(struct udphdr, check);
913 	skb->ip_summed = CHECKSUM_PARTIAL;
914 
915 	skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
916 	skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
917 
918 	if (skb->encapsulation)
919 		skb->inner_transport_header = skb->transport_header;
920 
921 	return 0;
922 }
923 
924 int udp_gro_complete(struct sk_buff *skb, int nhoff,
925 		     udp_lookup_t lookup)
926 {
927 	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
928 	unsigned int newlen = skb->len - nhoff;
929 	struct sock *sk;
930 	int err;
931 
932 	udp_set_len(uh, newlen);
933 
934 	sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
935 				udp4_lib_lookup_skb, skb, uh->source, uh->dest);
936 	if (sk && udp_sk(sk)->gro_complete) {
937 		skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM
938 					: SKB_GSO_UDP_TUNNEL;
939 
940 		/* clear the encap mark, so that inner frag_list gro_complete
941 		 * can take place
942 		 */
943 		NAPI_GRO_CB(skb)->encap_mark = 0;
944 
945 		/* Set encapsulation before calling into inner gro_complete()
946 		 * functions to make them set up the inner offsets.
947 		 */
948 		skb->encapsulation = 1;
949 		err = udp_sk(sk)->gro_complete(sk, skb,
950 				nhoff + sizeof(struct udphdr));
951 	} else {
952 		err = udp_gro_complete_segment(skb);
953 	}
954 
955 	if (skb->remcsum_offload)
956 		skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
957 
958 	return err;
959 }
960 
961 INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
962 {
963 	const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
964 	const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
965 	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
966 
967 	/* do fraglist only if there is no outer UDP encap (or we already processed it) */
968 	if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
969 		udp_set_len(uh, skb->len - nhoff);
970 
971 		skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
972 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
973 
974 		__skb_incr_checksum_unnecessary(skb);
975 
976 		return 0;
977 	}
978 
979 	if (uh->check)
980 		uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
981 					  iph->daddr, 0);
982 
983 	return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
984 }
985 
986 int __init udpv4_offload_init(void)
987 {
988 	net_hotdata.udpv4_offload = (struct net_offload) {
989 		.callbacks = {
990 			.gso_segment = udp4_ufo_fragment,
991 			.gro_receive  =	udp4_gro_receive,
992 			.gro_complete =	udp4_gro_complete,
993 		},
994 	};
995 
996 	return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP);
997 }
998