xref: /linux/net/ipv4/udp_offload.c (revision a010fe8d869635fd15c1d5768ba6a2f48795fa36)
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 	struct udphdr *uh = udp_hdr(skb);
182 	u16 mac_offset = skb->mac_header;
183 	__be16 protocol = skb->protocol;
184 	u16 mac_len = skb->mac_len;
185 	int udp_offset, outer_hlen;
186 	__wsum partial;
187 	bool need_ipsec;
188 
189 	if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
190 		goto out;
191 
192 	/* Adjust partial header checksum to negate old length.
193 	 * We cannot rely on the value contained in uh->len as it is
194 	 * possible that the actual value exceeds the boundaries of the
195 	 * 16 bit length field due to the header being added outside of an
196 	 * IP or IPv6 frame that was already limited to 64K - 1.
197 	 */
198 	if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)
199 		partial = (__force __wsum)uh->len;
200 	else
201 		partial = (__force __wsum)htonl(skb->len);
202 	partial = csum_sub(csum_unfold(uh->check), partial);
203 
204 	/* setup inner skb. */
205 	skb->encapsulation = 0;
206 	SKB_GSO_CB(skb)->encap_level = 0;
207 	__skb_pull(skb, tnl_hlen);
208 	skb_reset_mac_header(skb);
209 	skb_set_network_header(skb, skb_inner_network_offset(skb));
210 	skb_set_transport_header(skb, skb_inner_transport_offset(skb));
211 	skb->mac_len = skb_inner_network_offset(skb);
212 	skb->protocol = new_protocol;
213 
214 	need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
215 	skb->encap_hdr_csum = need_csum;
216 
217 	remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
218 	skb->remcsum_offload = remcsum;
219 
220 	need_ipsec = (skb_dst(skb) && dst_xfrm(skb_dst(skb))) || skb_sec_path(skb);
221 	/* Try to offload checksum if possible */
222 	offload_csum = !!(need_csum &&
223 			  !need_ipsec &&
224 			  (skb->dev->features &
225 			   (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
226 				      (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
227 
228 	features &= skb->dev->hw_enc_features;
229 	if (need_csum)
230 		features &= ~NETIF_F_SCTP_CRC;
231 
232 	/* The only checksum offload we care about from here on out is the
233 	 * outer one so strip the existing checksum feature flags and
234 	 * instead set the flag based on our outer checksum offload value.
235 	 */
236 	if (remcsum) {
237 		features &= ~NETIF_F_CSUM_MASK;
238 		if (!need_csum || offload_csum)
239 			features |= NETIF_F_HW_CSUM;
240 	}
241 
242 	/* segment inner packet. */
243 	segs = gso_inner_segment(skb, features);
244 	if (IS_ERR_OR_NULL(segs)) {
245 		skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
246 				     mac_len);
247 		goto out;
248 	}
249 
250 	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
251 
252 	outer_hlen = skb_tnl_header_len(skb);
253 	udp_offset = outer_hlen - tnl_hlen;
254 	skb = segs;
255 	do {
256 		unsigned int len;
257 
258 		if (remcsum)
259 			skb->ip_summed = CHECKSUM_NONE;
260 
261 		/* Set up inner headers if we are offloading inner checksum */
262 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
263 			skb_reset_inner_headers(skb);
264 			skb->encapsulation = 1;
265 		}
266 
267 		skb->mac_len = mac_len;
268 		skb->protocol = protocol;
269 
270 		__skb_push(skb, outer_hlen);
271 		skb_reset_mac_header(skb);
272 		skb_set_network_header(skb, mac_len);
273 		skb_set_transport_header(skb, udp_offset);
274 		len = skb->len - udp_offset;
275 		uh = udp_hdr(skb);
276 
277 		/* If we are only performing partial GSO the inner header
278 		 * will be using a length value equal to only one MSS sized
279 		 * segment instead of the entire frame.
280 		 */
281 		if (gso_partial && skb_is_gso(skb)) {
282 			uh->len = htons(skb_shinfo(skb)->gso_size +
283 					SKB_GSO_CB(skb)->data_offset +
284 					skb->head - (unsigned char *)uh);
285 		} else {
286 			uh->len = htons(len);
287 		}
288 
289 		if (!need_csum)
290 			continue;
291 
292 		uh->check = ~csum_fold(csum_add(partial,
293 				       (__force __wsum)htonl(len)));
294 
295 		if (skb->encapsulation || !offload_csum) {
296 			uh->check = gso_make_checksum(skb, ~uh->check);
297 			if (uh->check == 0)
298 				uh->check = CSUM_MANGLED_0;
299 		} else {
300 			skb->ip_summed = CHECKSUM_PARTIAL;
301 			skb->csum_start = skb_transport_header(skb) - skb->head;
302 			skb->csum_offset = offsetof(struct udphdr, check);
303 		}
304 	} while ((skb = skb->next));
305 out:
306 	return segs;
307 }
308 
309 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
310 				       netdev_features_t features,
311 				       bool is_ipv6)
312 {
313 	const struct net_offload __rcu **offloads;
314 	__be16 protocol = skb->protocol;
315 	const struct net_offload *ops;
316 	struct sk_buff *segs = ERR_PTR(-EINVAL);
317 	struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
318 					     netdev_features_t features);
319 
320 	rcu_read_lock();
321 
322 	switch (skb->inner_protocol_type) {
323 	case ENCAP_TYPE_ETHER:
324 		protocol = skb->inner_protocol;
325 		gso_inner_segment = skb_mac_gso_segment;
326 		break;
327 	case ENCAP_TYPE_IPPROTO:
328 		offloads = is_ipv6 ? inet6_offloads : inet_offloads;
329 		ops = rcu_dereference(offloads[skb->inner_ipproto]);
330 		if (!ops || !ops->callbacks.gso_segment)
331 			goto out_unlock;
332 		gso_inner_segment = ops->callbacks.gso_segment;
333 		break;
334 	default:
335 		goto out_unlock;
336 	}
337 
338 	segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
339 					protocol, is_ipv6);
340 
341 out_unlock:
342 	rcu_read_unlock();
343 
344 	return segs;
345 }
346 EXPORT_SYMBOL(skb_udp_tunnel_segment);
347 
348 static void __udpv4_gso_segment_csum(struct sk_buff *seg,
349 				     __be32 *oldip, __be32 *newip,
350 				     __be16 *oldport, __be16 *newport)
351 {
352 	struct udphdr *uh;
353 	struct iphdr *iph;
354 
355 	if (*oldip == *newip && *oldport == *newport)
356 		return;
357 
358 	uh = udp_hdr(seg);
359 	iph = ip_hdr(seg);
360 
361 	if (uh->check) {
362 		inet_proto_csum_replace4(&uh->check, seg, *oldip, *newip,
363 					 true);
364 		inet_proto_csum_replace2(&uh->check, seg, *oldport, *newport,
365 					 false);
366 		if (!uh->check)
367 			uh->check = CSUM_MANGLED_0;
368 	}
369 	*oldport = *newport;
370 
371 	csum_replace4(&iph->check, *oldip, *newip);
372 	*oldip = *newip;
373 }
374 
375 static struct sk_buff *__udpv4_gso_segment_list_csum(struct sk_buff *segs)
376 {
377 	struct sk_buff *seg;
378 	struct udphdr *uh, *uh2;
379 	struct iphdr *iph, *iph2;
380 
381 	seg = segs;
382 	uh = udp_hdr(seg);
383 	iph = ip_hdr(seg);
384 
385 	if ((udp_hdr(seg)->dest == udp_hdr(seg->next)->dest) &&
386 	    (udp_hdr(seg)->source == udp_hdr(seg->next)->source) &&
387 	    (ip_hdr(seg)->daddr == ip_hdr(seg->next)->daddr) &&
388 	    (ip_hdr(seg)->saddr == ip_hdr(seg->next)->saddr))
389 		return segs;
390 
391 	while ((seg = seg->next)) {
392 		uh2 = udp_hdr(seg);
393 		iph2 = ip_hdr(seg);
394 
395 		__udpv4_gso_segment_csum(seg,
396 					 &iph2->saddr, &iph->saddr,
397 					 &uh2->source, &uh->source);
398 		__udpv4_gso_segment_csum(seg,
399 					 &iph2->daddr, &iph->daddr,
400 					 &uh2->dest, &uh->dest);
401 	}
402 
403 	return segs;
404 }
405 
406 static void __udpv6_gso_segment_csum(struct sk_buff *seg,
407 				     struct in6_addr *oldip,
408 				     const struct in6_addr *newip,
409 				     __be16 *oldport, __be16 newport)
410 {
411 	struct udphdr *uh = udp_hdr(seg);
412 
413 	if (ipv6_addr_equal(oldip, newip) && *oldport == newport)
414 		return;
415 
416 	if (uh->check) {
417 		inet_proto_csum_replace16(&uh->check, seg, oldip->s6_addr32,
418 					  newip->s6_addr32, true);
419 
420 		inet_proto_csum_replace2(&uh->check, seg, *oldport, newport,
421 					 false);
422 		if (!uh->check)
423 			uh->check = CSUM_MANGLED_0;
424 	}
425 
426 	*oldip = *newip;
427 	*oldport = newport;
428 }
429 
430 static struct sk_buff *__udpv6_gso_segment_list_csum(struct sk_buff *segs)
431 {
432 	const struct ipv6hdr *iph;
433 	const struct udphdr *uh;
434 	struct ipv6hdr *iph2;
435 	struct sk_buff *seg;
436 	struct udphdr *uh2;
437 
438 	seg = segs;
439 	uh = udp_hdr(seg);
440 	iph = ipv6_hdr(seg);
441 	uh2 = udp_hdr(seg->next);
442 	iph2 = ipv6_hdr(seg->next);
443 
444 	if (!(*(const u32 *)&uh->source ^ *(const u32 *)&uh2->source) &&
445 	    ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
446 	    ipv6_addr_equal(&iph->daddr, &iph2->daddr))
447 		return segs;
448 
449 	while ((seg = seg->next)) {
450 		uh2 = udp_hdr(seg);
451 		iph2 = ipv6_hdr(seg);
452 
453 		__udpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
454 					 &uh2->source, uh->source);
455 		__udpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
456 					 &uh2->dest, uh->dest);
457 	}
458 
459 	return segs;
460 }
461 
462 static struct sk_buff *__udp_gso_segment_list(struct sk_buff *skb,
463 					      netdev_features_t features,
464 					      bool is_ipv6)
465 {
466 	unsigned int mss = skb_shinfo(skb)->gso_size;
467 
468 	skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
469 	if (IS_ERR(skb))
470 		return skb;
471 
472 	udp_hdr(skb)->len = htons(sizeof(struct udphdr) + mss);
473 
474 	if (is_ipv6)
475 		return __udpv6_gso_segment_list_csum(skb);
476 	else
477 		return __udpv4_gso_segment_list_csum(skb);
478 }
479 
480 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
481 				  netdev_features_t features, bool is_ipv6)
482 {
483 	struct sock *sk = gso_skb->sk;
484 	unsigned int sum_truesize = 0;
485 	struct sk_buff *segs, *seg;
486 	__be16 newlen, msslen;
487 	struct udphdr *uh;
488 	unsigned int mss;
489 	bool copy_dtor;
490 	__sum16 check;
491 	int ret = 0;
492 
493 	mss = skb_shinfo(gso_skb)->gso_size;
494 	if (gso_skb->len <= sizeof(*uh) + mss)
495 		return ERR_PTR(-EINVAL);
496 
497 	if (unlikely(skb_checksum_start(gso_skb) !=
498 		     skb_transport_header(gso_skb) &&
499 		     !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST)))
500 		return ERR_PTR(-EINVAL);
501 
502 	/* We don't know if egress device can segment and checksum the packet
503 	 * when IPv6 extension headers are present. Fall back to software GSO.
504 	 */
505 	if (gso_skb->ip_summed != CHECKSUM_PARTIAL)
506 		features &= ~(NETIF_F_GSO_UDP_L4 | NETIF_F_CSUM_MASK);
507 
508 	if (skb_gso_ok(gso_skb, features | NETIF_F_GSO_ROBUST)) {
509 		/* Packet is from an untrusted source, reset gso_segs. */
510 		skb_shinfo(gso_skb)->gso_segs = DIV_ROUND_UP(gso_skb->len - sizeof(*uh),
511 							     mss);
512 		return NULL;
513 	}
514 
515 	if (skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST) {
516 		 /* Detect modified geometry and pass those to skb_segment. */
517 		if ((skb_pagelen(gso_skb) - sizeof(*uh) == skb_shinfo(gso_skb)->gso_size) &&
518 		    !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_DODGY))
519 			return __udp_gso_segment_list(gso_skb, features, is_ipv6);
520 
521 		ret = __skb_linearize(gso_skb);
522 		if (ret)
523 			return ERR_PTR(ret);
524 
525 		 /* Setup csum, as fraglist skips this in udp4_gro_receive. */
526 		gso_skb->csum_start = skb_transport_header(gso_skb) - gso_skb->head;
527 		gso_skb->csum_offset = offsetof(struct udphdr, check);
528 		gso_skb->ip_summed = CHECKSUM_PARTIAL;
529 
530 		uh = udp_hdr(gso_skb);
531 		if (is_ipv6)
532 			uh->check = ~udp_v6_check(gso_skb->len,
533 						  &ipv6_hdr(gso_skb)->saddr,
534 						  &ipv6_hdr(gso_skb)->daddr, 0);
535 		else
536 			uh->check = ~udp_v4_check(gso_skb->len,
537 						  ip_hdr(gso_skb)->saddr,
538 						  ip_hdr(gso_skb)->daddr, 0);
539 	}
540 
541 	skb_pull(gso_skb, sizeof(*uh));
542 
543 	/* clear destructor to avoid skb_segment assigning it to tail */
544 	copy_dtor = gso_skb->destructor == sock_wfree;
545 	if (copy_dtor) {
546 		gso_skb->destructor = NULL;
547 		gso_skb->sk = NULL;
548 	}
549 
550 	segs = skb_segment(gso_skb, features);
551 	if (IS_ERR_OR_NULL(segs)) {
552 		if (copy_dtor) {
553 			gso_skb->destructor = sock_wfree;
554 			gso_skb->sk = sk;
555 		}
556 		return segs;
557 	}
558 
559 	msslen = htons(sizeof(*uh) + mss);
560 
561 	/* GSO partial and frag_list segmentation only requires splitting
562 	 * the frame into an MSS multiple and possibly a remainder, both
563 	 * cases return a GSO skb. So update the mss now.
564 	 */
565 	if (skb_is_gso(segs))
566 		mss *= skb_shinfo(segs)->gso_segs;
567 
568 	seg = segs;
569 	uh = udp_hdr(seg);
570 
571 	/* preserve TX timestamp flags and TS key for first segment */
572 	skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey;
573 	skb_shinfo(seg)->tx_flags |=
574 			(skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP);
575 
576 	/* compute checksum adjustment based on old length versus new */
577 	newlen = htons(sizeof(*uh) + mss);
578 	check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
579 
580 	for (;;) {
581 		if (copy_dtor) {
582 			seg->destructor = sock_wfree;
583 			seg->sk = sk;
584 			sum_truesize += seg->truesize;
585 		}
586 
587 		if (!seg->next)
588 			break;
589 
590 		uh->len = msslen;
591 		uh->check = check;
592 
593 		if (seg->ip_summed == CHECKSUM_PARTIAL)
594 			gso_reset_checksum(seg, ~check);
595 		else
596 			uh->check = gso_make_checksum(seg, ~check) ? :
597 				    CSUM_MANGLED_0;
598 
599 		seg = seg->next;
600 		uh = udp_hdr(seg);
601 	}
602 
603 	/* last packet can be partial gso_size, account for that in checksum */
604 	newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) +
605 		       seg->data_len);
606 	check = csum16_add(csum16_sub(uh->check, uh->len), newlen);
607 
608 	uh->len = newlen;
609 	uh->check = check;
610 
611 	if (seg->ip_summed == CHECKSUM_PARTIAL)
612 		gso_reset_checksum(seg, ~check);
613 	else
614 		uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0;
615 
616 	/* On the TX path, CHECKSUM_NONE and CHECKSUM_UNNECESSARY have the same
617 	 * meaning. However, check for bad offloads in the GSO stack expects the
618 	 * latter, if the checksum was calculated in software. To vouch for the
619 	 * segment skbs we actually need to set it on the gso_skb.
620 	 */
621 	if (gso_skb->ip_summed == CHECKSUM_NONE)
622 		gso_skb->ip_summed = CHECKSUM_UNNECESSARY;
623 
624 	/* update refcount for the packet */
625 	if (copy_dtor) {
626 		int delta = sum_truesize - gso_skb->truesize;
627 
628 		/* In some pathological cases, delta can be negative.
629 		 * We need to either use refcount_add() or refcount_sub_and_test()
630 		 */
631 		if (likely(delta >= 0))
632 			refcount_add(delta, &sk->sk_wmem_alloc);
633 		else
634 			WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
635 	}
636 	return segs;
637 }
638 EXPORT_SYMBOL_GPL(__udp_gso_segment);
639 
640 static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
641 					 netdev_features_t features)
642 {
643 	struct sk_buff *segs = ERR_PTR(-EINVAL);
644 	unsigned int mss;
645 	__wsum csum;
646 	struct udphdr *uh;
647 	struct iphdr *iph;
648 
649 	if (skb->encapsulation &&
650 	    (skb_shinfo(skb)->gso_type &
651 	     (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
652 		segs = skb_udp_tunnel_segment(skb, features, false);
653 		goto out;
654 	}
655 
656 	if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
657 		goto out;
658 
659 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
660 		goto out;
661 
662 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
663 		return __udp_gso_segment(skb, features, false);
664 
665 	mss = skb_shinfo(skb)->gso_size;
666 	if (unlikely(skb->len <= mss))
667 		goto out;
668 
669 	/* Do software UFO. Complete and fill in the UDP checksum as
670 	 * HW cannot do checksum of UDP packets sent as multiple
671 	 * IP fragments.
672 	 */
673 
674 	uh = udp_hdr(skb);
675 	iph = ip_hdr(skb);
676 
677 	uh->check = 0;
678 	csum = skb_checksum(skb, 0, skb->len, 0);
679 	uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
680 	if (uh->check == 0)
681 		uh->check = CSUM_MANGLED_0;
682 
683 	skb->ip_summed = CHECKSUM_UNNECESSARY;
684 
685 	/* If there is no outer header we can fake a checksum offload
686 	 * due to the fact that we have already done the checksum in
687 	 * software prior to segmenting the frame.
688 	 */
689 	if (!skb->encap_hdr_csum)
690 		features |= NETIF_F_HW_CSUM;
691 
692 	/* Fragment the skb. IP headers of the fragments are updated in
693 	 * inet_gso_segment()
694 	 */
695 	segs = skb_segment(skb, features);
696 out:
697 	return segs;
698 }
699 
700 
701 #define UDP_GRO_CNT_MAX 64
702 static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
703 					       struct sk_buff *skb)
704 {
705 	struct udphdr *uh = udp_gro_udphdr(skb);
706 	struct sk_buff *pp = NULL;
707 	struct udphdr *uh2;
708 	struct sk_buff *p;
709 	unsigned int ulen;
710 	int ret = 0;
711 	int flush;
712 
713 	/* requires non zero csum, for symmetry with GSO */
714 	if (!uh->check) {
715 		NAPI_GRO_CB(skb)->flush = 1;
716 		return NULL;
717 	}
718 
719 	/* Do not deal with padded or malicious packets, sorry ! */
720 	ulen = ntohs(uh->len);
721 	if (ulen <= sizeof(*uh) || ulen != skb_gro_len(skb)) {
722 		NAPI_GRO_CB(skb)->flush = 1;
723 		return NULL;
724 	}
725 	/* pull encapsulating udp header */
726 	skb_gro_pull(skb, sizeof(struct udphdr));
727 
728 	list_for_each_entry(p, head, list) {
729 		if (!NAPI_GRO_CB(p)->same_flow)
730 			continue;
731 
732 		uh2 = udp_hdr(p);
733 
734 		/* Match ports only, as csum is always non zero */
735 		if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
736 			NAPI_GRO_CB(p)->same_flow = 0;
737 			continue;
738 		}
739 
740 		if (NAPI_GRO_CB(skb)->is_flist != NAPI_GRO_CB(p)->is_flist) {
741 			NAPI_GRO_CB(skb)->flush = 1;
742 			return p;
743 		}
744 
745 		flush = gro_receive_network_flush(uh, uh2, p);
746 
747 		/* Terminate the flow on len mismatch or if it grow "too much".
748 		 * Under small packet flood GRO count could elsewhere grow a lot
749 		 * leading to excessive truesize values.
750 		 * On len mismatch merge the first packet shorter than gso_size,
751 		 * otherwise complete the GRO packet.
752 		 */
753 		if (ulen > ntohs(uh2->len) || flush) {
754 			pp = p;
755 		} else {
756 			if (NAPI_GRO_CB(skb)->is_flist) {
757 				if (!pskb_may_pull(skb, skb_gro_offset(skb))) {
758 					NAPI_GRO_CB(skb)->flush = 1;
759 					return NULL;
760 				}
761 				if ((skb->ip_summed != p->ip_summed) ||
762 				    (skb->csum_level != p->csum_level)) {
763 					NAPI_GRO_CB(skb)->flush = 1;
764 					return NULL;
765 				}
766 				skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
767 				ret = skb_gro_receive_list(p, skb);
768 			} else {
769 				skb_gro_postpull_rcsum(skb, uh,
770 						       sizeof(struct udphdr));
771 
772 				ret = skb_gro_receive(p, skb);
773 			}
774 		}
775 
776 		if (ret || ulen != ntohs(uh2->len) ||
777 		    NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX)
778 			pp = p;
779 
780 		return pp;
781 	}
782 
783 	/* mismatch, but we never need to flush */
784 	return NULL;
785 }
786 
787 struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
788 				struct udphdr *uh, struct sock *sk)
789 {
790 	struct sk_buff *pp = NULL;
791 	struct sk_buff *p;
792 	struct udphdr *uh2;
793 	unsigned int off = skb_gro_offset(skb);
794 	int flush = 1;
795 
796 	/* We can do L4 aggregation only if the packet can't land in a tunnel
797 	 * otherwise we could corrupt the inner stream. Detecting such packets
798 	 * cannot be foolproof and the aggregation might still happen in some
799 	 * cases. Such packets should be caught in udp_unexpected_gso later.
800 	 */
801 	NAPI_GRO_CB(skb)->is_flist = 0;
802 	if (!sk || !udp_sk(sk)->gro_receive) {
803 		/* If the packet was locally encapsulated in a UDP tunnel that
804 		 * wasn't detected above, do not GRO.
805 		 */
806 		if (skb->encapsulation)
807 			goto out;
808 
809 		if (skb->dev->features & NETIF_F_GRO_FRAGLIST)
810 			NAPI_GRO_CB(skb)->is_flist = sk ? !udp_test_bit(GRO_ENABLED, sk) : 1;
811 
812 		if ((!sk && (skb->dev->features & NETIF_F_GRO_UDP_FWD)) ||
813 		    (sk && udp_test_bit(GRO_ENABLED, sk)) || NAPI_GRO_CB(skb)->is_flist)
814 			return call_gro_receive(udp_gro_receive_segment, head, skb);
815 
816 		/* no GRO, be sure flush the current packet */
817 		goto out;
818 	}
819 
820 	if (NAPI_GRO_CB(skb)->encap_mark ||
821 	    (uh->check && skb->ip_summed != CHECKSUM_PARTIAL &&
822 	     NAPI_GRO_CB(skb)->csum_cnt == 0 &&
823 	     !NAPI_GRO_CB(skb)->csum_valid))
824 		goto out;
825 
826 	/* mark that this skb passed once through the tunnel gro layer */
827 	NAPI_GRO_CB(skb)->encap_mark = 1;
828 
829 	flush = 0;
830 
831 	list_for_each_entry(p, head, list) {
832 		if (!NAPI_GRO_CB(p)->same_flow)
833 			continue;
834 
835 		uh2 = (struct udphdr   *)(p->data + off);
836 
837 		/* Match ports and either checksums are either both zero
838 		 * or nonzero.
839 		 */
840 		if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
841 		    (!uh->check ^ !uh2->check)) {
842 			NAPI_GRO_CB(p)->same_flow = 0;
843 			continue;
844 		}
845 	}
846 
847 	skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
848 	skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
849 	pp = udp_tunnel_gro_rcv(sk, head, skb);
850 
851 out:
852 	skb_gro_flush_final(skb, pp, flush);
853 	return pp;
854 }
855 EXPORT_SYMBOL(udp_gro_receive);
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,
873 				 sdif, net->ipv4.udp_table, NULL);
874 }
875 
876 INDIRECT_CALLABLE_SCOPE
877 struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb)
878 {
879 	struct udphdr *uh = udp_gro_udphdr(skb);
880 	struct sock *sk = NULL;
881 	struct sk_buff *pp;
882 
883 	if (unlikely(!uh))
884 		goto flush;
885 
886 	/* Don't bother verifying checksum if we're going to flush anyway. */
887 	if (NAPI_GRO_CB(skb)->flush)
888 		goto skip;
889 
890 	if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
891 						 inet_gro_compute_pseudo))
892 		goto flush;
893 	else if (uh->check)
894 		skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
895 					     inet_gro_compute_pseudo);
896 skip:
897 	if (static_branch_unlikely(&udp_encap_needed_key))
898 		sk = udp4_gro_lookup_skb(skb, uh->source, uh->dest);
899 
900 	pp = udp_gro_receive(head, skb, uh, sk);
901 	return pp;
902 
903 flush:
904 	NAPI_GRO_CB(skb)->flush = 1;
905 	return NULL;
906 }
907 
908 static int udp_gro_complete_segment(struct sk_buff *skb)
909 {
910 	struct udphdr *uh = udp_hdr(skb);
911 
912 	skb->csum_start = (unsigned char *)uh - skb->head;
913 	skb->csum_offset = offsetof(struct udphdr, check);
914 	skb->ip_summed = CHECKSUM_PARTIAL;
915 
916 	skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
917 	skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
918 
919 	if (skb->encapsulation)
920 		skb->inner_transport_header = skb->transport_header;
921 
922 	return 0;
923 }
924 
925 int udp_gro_complete(struct sk_buff *skb, int nhoff,
926 		     udp_lookup_t lookup)
927 {
928 	__be16 newlen = htons(skb->len - nhoff);
929 	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
930 	struct sock *sk;
931 	int err;
932 
933 	uh->len = newlen;
934 
935 	sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb,
936 				udp4_lib_lookup_skb, skb, uh->source, uh->dest);
937 	if (sk && udp_sk(sk)->gro_complete) {
938 		skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM
939 					: SKB_GSO_UDP_TUNNEL;
940 
941 		/* clear the encap mark, so that inner frag_list gro_complete
942 		 * can take place
943 		 */
944 		NAPI_GRO_CB(skb)->encap_mark = 0;
945 
946 		/* Set encapsulation before calling into inner gro_complete()
947 		 * functions to make them set up the inner offsets.
948 		 */
949 		skb->encapsulation = 1;
950 		err = udp_sk(sk)->gro_complete(sk, skb,
951 				nhoff + sizeof(struct udphdr));
952 	} else {
953 		err = udp_gro_complete_segment(skb);
954 	}
955 
956 	if (skb->remcsum_offload)
957 		skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
958 
959 	return err;
960 }
961 EXPORT_SYMBOL(udp_gro_complete);
962 
963 INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
964 {
965 	const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
966 	const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
967 	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
968 
969 	/* do fraglist only if there is no outer UDP encap (or we already processed it) */
970 	if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
971 		uh->len = htons(skb->len - nhoff);
972 
973 		skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
974 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
975 
976 		__skb_incr_checksum_unnecessary(skb);
977 
978 		return 0;
979 	}
980 
981 	if (uh->check)
982 		uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
983 					  iph->daddr, 0);
984 
985 	return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
986 }
987 
988 int __init udpv4_offload_init(void)
989 {
990 	net_hotdata.udpv4_offload = (struct net_offload) {
991 		.callbacks = {
992 			.gso_segment = udp4_ufo_fragment,
993 			.gro_receive  =	udp4_gro_receive,
994 			.gro_complete =	udp4_gro_complete,
995 		},
996 	};
997 
998 	return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP);
999 }
1000