xref: /linux/include/net/udp.h (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Definitions for the UDP module.
8  *
9  * Version:	@(#)udp.h	1.0.2	05/07/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13  *
14  * Fixes:
15  *		Alan Cox	: Turned on udp checksums. I don't want to
16  *				  chase 'memory corruption' bugs that aren't!
17  */
18 #ifndef _UDP_H
19 #define _UDP_H
20 
21 #include <linux/list.h>
22 #include <linux/bug.h>
23 #include <net/inet_sock.h>
24 #include <net/gso.h>
25 #include <net/sock.h>
26 #include <net/snmp.h>
27 #include <net/ip.h>
28 #include <linux/ipv6.h>
29 #include <linux/seq_file.h>
30 #include <linux/poll.h>
31 #include <linux/indirect_call_wrapper.h>
32 
33 /**
34  *	struct udp_skb_cb  -  UDP(-Lite) private variables
35  *
36  *	@header:      private variables used by IPv4/IPv6
37  *	@cscov:       checksum coverage length (UDP-Lite only)
38  *	@partial_cov: if set indicates partial csum coverage
39  */
40 struct udp_skb_cb {
41 	union {
42 		struct inet_skb_parm	h4;
43 #if IS_ENABLED(CONFIG_IPV6)
44 		struct inet6_skb_parm	h6;
45 #endif
46 	} header;
47 	__u16		cscov;
48 	__u8		partial_cov;
49 };
50 #define UDP_SKB_CB(__skb)	((struct udp_skb_cb *)((__skb)->cb))
51 
52 /**
53  *	struct udp_hslot - UDP hash slot used by udp_table.hash/hash4
54  *
55  *	@head:	head of list of sockets
56  *	@nulls_head:	head of list of sockets, only used by hash4
57  *	@count:	number of sockets in 'head' list
58  *	@lock:	spinlock protecting changes to head/count
59  */
60 struct udp_hslot {
61 	union {
62 		struct hlist_head	head;
63 		/* hash4 uses hlist_nulls to avoid moving wrongly onto another
64 		 * hlist, because rehash() can happen with lookup().
65 		 */
66 		struct hlist_nulls_head	nulls_head;
67 	};
68 	int			count;
69 	spinlock_t		lock;
70 } __aligned(2 * sizeof(long));
71 
72 /**
73  *	struct udp_hslot_main - UDP hash slot used by udp_table.hash2
74  *
75  *	@hslot:	basic hash slot
76  *	@hash4_cnt: number of sockets in hslot4 of the same
77  *		    (local port, local address)
78  */
79 struct udp_hslot_main {
80 	struct udp_hslot	hslot; /* must be the first member */
81 #if !IS_ENABLED(CONFIG_BASE_SMALL)
82 	u32			hash4_cnt;
83 #endif
84 } __aligned(2 * sizeof(long));
85 #define UDP_HSLOT_MAIN(__hslot) ((struct udp_hslot_main *)(__hslot))
86 
87 /**
88  *	struct udp_table - UDP table
89  *
90  *	@hash:	hash table, sockets are hashed on (local port)
91  *	@hash2:	hash table, sockets are hashed on (local port, local address)
92  *	@hash4:	hash table, connected sockets are hashed on
93  *		(local port, local address, remote port, remote address)
94  *	@mask:	number of slots in hash tables, minus 1
95  *	@log:	log2(number of slots in hash table)
96  */
97 struct udp_table {
98 	struct udp_hslot	*hash;
99 	struct udp_hslot_main	*hash2;
100 #if !IS_ENABLED(CONFIG_BASE_SMALL)
101 	struct udp_hslot	*hash4;
102 #endif
103 	unsigned int		mask;
104 	unsigned int		log;
105 };
106 extern struct udp_table udp_table;
107 void udp_table_init(struct udp_table *, const char *);
108 static inline struct udp_hslot *udp_hashslot(struct udp_table *table,
109 					     const struct net *net,
110 					     unsigned int num)
111 {
112 	return &table->hash[udp_hashfn(net, num, table->mask)];
113 }
114 
115 /*
116  * For secondary hash, net_hash_mix() is performed before calling
117  * udp_hashslot2(), this explains difference with udp_hashslot()
118  */
119 static inline struct udp_hslot *udp_hashslot2(struct udp_table *table,
120 					      unsigned int hash)
121 {
122 	return &table->hash2[hash & table->mask].hslot;
123 }
124 
125 #if IS_ENABLED(CONFIG_BASE_SMALL)
126 static inline void udp_table_hash4_init(struct udp_table *table)
127 {
128 }
129 
130 static inline struct udp_hslot *udp_hashslot4(struct udp_table *table,
131 					      unsigned int hash)
132 {
133 	BUILD_BUG();
134 	return NULL;
135 }
136 
137 static inline bool udp_hashed4(const struct sock *sk)
138 {
139 	return false;
140 }
141 
142 static inline unsigned int udp_hash4_slot_size(void)
143 {
144 	return 0;
145 }
146 
147 static inline bool udp_has_hash4(const struct udp_hslot *hslot2)
148 {
149 	return false;
150 }
151 
152 static inline void udp_hash4_inc(struct udp_hslot *hslot2)
153 {
154 }
155 
156 static inline void udp_hash4_dec(struct udp_hslot *hslot2)
157 {
158 }
159 #else /* !CONFIG_BASE_SMALL */
160 
161 /* Must be called with table->hash2 initialized */
162 static inline void udp_table_hash4_init(struct udp_table *table)
163 {
164 	table->hash4 = (void *)(table->hash2 + (table->mask + 1));
165 	for (int i = 0; i <= table->mask; i++) {
166 		table->hash2[i].hash4_cnt = 0;
167 
168 		INIT_HLIST_NULLS_HEAD(&table->hash4[i].nulls_head, i);
169 		table->hash4[i].count = 0;
170 		spin_lock_init(&table->hash4[i].lock);
171 	}
172 }
173 
174 static inline struct udp_hslot *udp_hashslot4(struct udp_table *table,
175 					      unsigned int hash)
176 {
177 	return &table->hash4[hash & table->mask];
178 }
179 
180 static inline bool udp_hashed4(const struct sock *sk)
181 {
182 	return !hlist_nulls_unhashed(&udp_sk(sk)->udp_lrpa_node);
183 }
184 
185 static inline unsigned int udp_hash4_slot_size(void)
186 {
187 	return sizeof(struct udp_hslot);
188 }
189 
190 static inline bool udp_has_hash4(const struct udp_hslot *hslot2)
191 {
192 	return UDP_HSLOT_MAIN(hslot2)->hash4_cnt;
193 }
194 
195 static inline void udp_hash4_inc(struct udp_hslot *hslot2)
196 {
197 	UDP_HSLOT_MAIN(hslot2)->hash4_cnt++;
198 }
199 
200 static inline void udp_hash4_dec(struct udp_hslot *hslot2)
201 {
202 	UDP_HSLOT_MAIN(hslot2)->hash4_cnt--;
203 }
204 #endif /* CONFIG_BASE_SMALL */
205 
206 extern struct proto udp_prot;
207 
208 DECLARE_PER_CPU(int, udp_memory_per_cpu_fw_alloc);
209 
210 /* sysctl variables for udp */
211 extern long sysctl_udp_mem[3];
212 extern int sysctl_udp_rmem_min;
213 extern int sysctl_udp_wmem_min;
214 
215 struct sk_buff;
216 
217 /*
218  *	Generic checksumming routines for UDP(-Lite) v4 and v6
219  */
220 static inline __sum16 __udp_lib_checksum_complete(struct sk_buff *skb)
221 {
222 	return (UDP_SKB_CB(skb)->cscov == skb->len ?
223 		__skb_checksum_complete(skb) :
224 		__skb_checksum_complete_head(skb, UDP_SKB_CB(skb)->cscov));
225 }
226 
227 static inline int udp_lib_checksum_complete(struct sk_buff *skb)
228 {
229 	return !skb_csum_unnecessary(skb) &&
230 		__udp_lib_checksum_complete(skb);
231 }
232 
233 /**
234  * 	udp_csum_outgoing  -  compute UDPv4/v6 checksum over fragments
235  * 	@sk: 	socket we are writing to
236  * 	@skb: 	sk_buff containing the filled-in UDP header
237  * 	        (checksum field must be zeroed out)
238  */
239 static inline __wsum udp_csum_outgoing(struct sock *sk, struct sk_buff *skb)
240 {
241 	__wsum csum = csum_partial(skb_transport_header(skb),
242 				   sizeof(struct udphdr), 0);
243 	skb_queue_walk(&sk->sk_write_queue, skb) {
244 		csum = csum_add(csum, skb->csum);
245 	}
246 	return csum;
247 }
248 
249 static inline __wsum udp_csum(struct sk_buff *skb)
250 {
251 	__wsum csum = csum_partial(skb_transport_header(skb),
252 				   sizeof(struct udphdr), skb->csum);
253 
254 	for (skb = skb_shinfo(skb)->frag_list; skb; skb = skb->next) {
255 		csum = csum_add(csum, skb->csum);
256 	}
257 	return csum;
258 }
259 
260 static inline __sum16 udp_v4_check(int len, __be32 saddr,
261 				   __be32 daddr, __wsum base)
262 {
263 	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base);
264 }
265 
266 void udp_set_csum(bool nocheck, struct sk_buff *skb,
267 		  __be32 saddr, __be32 daddr, int len);
268 
269 static inline void udp_csum_pull_header(struct sk_buff *skb)
270 {
271 	if (!skb->csum_valid && skb->ip_summed == CHECKSUM_NONE)
272 		skb->csum = csum_partial(skb->data, sizeof(struct udphdr),
273 					 skb->csum);
274 	skb_pull_rcsum(skb, sizeof(struct udphdr));
275 	UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr);
276 }
277 
278 typedef struct sock *(*udp_lookup_t)(const struct sk_buff *skb, __be16 sport,
279 				     __be16 dport);
280 
281 void udp_v6_early_demux(struct sk_buff *skb);
282 INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
283 
284 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
285 				  netdev_features_t features, bool is_ipv6);
286 
287 static inline int udp_lib_init_sock(struct sock *sk)
288 {
289 	struct udp_sock *up = udp_sk(sk);
290 
291 	sk->sk_drop_counters = &up->drop_counters;
292 	skb_queue_head_init(&up->reader_queue);
293 	INIT_HLIST_NODE(&up->tunnel_list);
294 	up->forward_threshold = sk->sk_rcvbuf >> 2;
295 	set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
296 
297 	up->udp_prod_queue = kcalloc(nr_node_ids, sizeof(*up->udp_prod_queue),
298 				     GFP_KERNEL);
299 	if (!up->udp_prod_queue)
300 		return -ENOMEM;
301 	for (int i = 0; i < nr_node_ids; i++)
302 		init_llist_head(&up->udp_prod_queue[i].ll_root);
303 	return 0;
304 }
305 
306 static inline void udp_drops_inc(struct sock *sk)
307 {
308 	numa_drop_add(&udp_sk(sk)->drop_counters, 1);
309 }
310 
311 /* hash routines shared between UDPv4/6 and UDP-Litev4/6 */
312 static inline int udp_lib_hash(struct sock *sk)
313 {
314 	BUG();
315 	return 0;
316 }
317 
318 void udp_lib_unhash(struct sock *sk);
319 void udp_lib_rehash(struct sock *sk, u16 new_hash, u16 new_hash4);
320 u32 udp_ehashfn(const struct net *net, const __be32 laddr, const __u16 lport,
321 		const __be32 faddr, const __be16 fport);
322 
323 static inline void udp_lib_close(struct sock *sk, long timeout)
324 {
325 	sk_common_release(sk);
326 }
327 
328 /* hash4 routines shared between UDPv4/6 */
329 #if IS_ENABLED(CONFIG_BASE_SMALL)
330 static inline void udp_lib_hash4(struct sock *sk, u16 hash)
331 {
332 }
333 
334 static inline void udp4_hash4(struct sock *sk)
335 {
336 }
337 #else /* !CONFIG_BASE_SMALL */
338 void udp_lib_hash4(struct sock *sk, u16 hash);
339 void udp4_hash4(struct sock *sk);
340 #endif /* CONFIG_BASE_SMALL */
341 
342 int udp_lib_get_port(struct sock *sk, unsigned short snum,
343 		     unsigned int hash2_nulladdr);
344 
345 u32 udp_flow_hashrnd(void);
346 
347 static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
348 				       int min, int max, bool use_eth)
349 {
350 	u32 hash;
351 
352 	if (min >= max) {
353 		/* Use default range */
354 		inet_get_local_port_range(net, &min, &max);
355 	}
356 
357 	hash = skb_get_hash(skb);
358 	if (unlikely(!hash)) {
359 		if (use_eth) {
360 			/* Can't find a normal hash, caller has indicated an
361 			 * Ethernet packet so use that to compute a hash.
362 			 */
363 			hash = jhash(skb->data, 2 * ETH_ALEN,
364 				     (__force u32) skb->protocol);
365 		} else {
366 			/* Can't derive any sort of hash for the packet, set
367 			 * to some consistent random value.
368 			 */
369 			hash = udp_flow_hashrnd();
370 		}
371 	}
372 
373 	/* Since this is being sent on the wire obfuscate hash a bit
374 	 * to minimize possibility that any useful information to an
375 	 * attacker is leaked. Only upper 16 bits are relevant in the
376 	 * computation for 16 bit port value.
377 	 */
378 	hash ^= hash << 16;
379 
380 	return htons((((u64) hash * (max - min)) >> 32) + min);
381 }
382 
383 static inline int udp_rqueue_get(struct sock *sk)
384 {
385 	return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit);
386 }
387 
388 static inline bool udp_sk_bound_dev_eq(const struct net *net, int bound_dev_if,
389 				       int dif, int sdif)
390 {
391 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
392 	return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept),
393 				 bound_dev_if, dif, sdif);
394 #else
395 	return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
396 #endif
397 }
398 
399 /* net/ipv4/udp.c */
400 void udp_destruct_common(struct sock *sk);
401 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len);
402 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb);
403 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb);
404 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, int *off,
405 			       int *err);
406 static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags,
407 					   int *err)
408 {
409 	int off = 0;
410 
411 	return __skb_recv_udp(sk, flags, &off, err);
412 }
413 
414 enum skb_drop_reason udp_v4_early_demux(struct sk_buff *skb);
415 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst);
416 int udp_err(struct sk_buff *, u32);
417 int udp_abort(struct sock *sk, int err);
418 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
419 void udp_splice_eof(struct socket *sock);
420 int udp_push_pending_frames(struct sock *sk);
421 void udp_flush_pending_frames(struct sock *sk);
422 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
423 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
424 int udp_rcv(struct sk_buff *skb);
425 int udp_ioctl(struct sock *sk, int cmd, int *karg);
426 int udp_init_sock(struct sock *sk);
427 int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
428 int __udp_disconnect(struct sock *sk, int flags);
429 int udp_disconnect(struct sock *sk, int flags);
430 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
431 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
432 				       netdev_features_t features,
433 				       bool is_ipv6);
434 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
435 		       char __user *optval, int __user *optlen);
436 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
437 		       sockptr_t optval, unsigned int optlen,
438 		       int (*push_pending_frames)(struct sock *));
439 struct sock *udp4_lib_lookup(const struct net *net, __be32 saddr, __be16 sport,
440 			     __be32 daddr, __be16 dport, int dif);
441 struct sock *__udp4_lib_lookup(const struct net *net, __be32 saddr,
442 			       __be16 sport,
443 			       __be32 daddr, __be16 dport, int dif, int sdif,
444 			       struct udp_table *tbl, struct sk_buff *skb);
445 struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
446 				 __be16 sport, __be16 dport);
447 struct sock *udp6_lib_lookup(const struct net *net,
448 			     const struct in6_addr *saddr, __be16 sport,
449 			     const struct in6_addr *daddr, __be16 dport,
450 			     int dif);
451 struct sock *__udp6_lib_lookup(const struct net *net,
452 			       const struct in6_addr *saddr, __be16 sport,
453 			       const struct in6_addr *daddr, __be16 dport,
454 			       int dif, int sdif, struct udp_table *tbl,
455 			       struct sk_buff *skb);
456 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
457 				 __be16 sport, __be16 dport);
458 int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
459 
460 /* UDP uses skb->dev_scratch to cache as much information as possible and avoid
461  * possibly multiple cache miss on dequeue()
462  */
463 struct udp_dev_scratch {
464 	/* skb->truesize and the stateless bit are embedded in a single field;
465 	 * do not use a bitfield since the compiler emits better/smaller code
466 	 * this way
467 	 */
468 	u32 _tsize_state;
469 
470 #if BITS_PER_LONG == 64
471 	/* len and the bit needed to compute skb_csum_unnecessary
472 	 * will be on cold cache lines at recvmsg time.
473 	 * skb->len can be stored on 16 bits since the udp header has been
474 	 * already validated and pulled.
475 	 */
476 	u16 len;
477 	bool is_linear;
478 	bool csum_unnecessary;
479 #endif
480 };
481 
482 static inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb)
483 {
484 	return (struct udp_dev_scratch *)&skb->dev_scratch;
485 }
486 
487 #if BITS_PER_LONG == 64
488 static inline unsigned int udp_skb_len(struct sk_buff *skb)
489 {
490 	return udp_skb_scratch(skb)->len;
491 }
492 
493 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
494 {
495 	return udp_skb_scratch(skb)->csum_unnecessary;
496 }
497 
498 static inline bool udp_skb_is_linear(struct sk_buff *skb)
499 {
500 	return udp_skb_scratch(skb)->is_linear;
501 }
502 
503 #else
504 static inline unsigned int udp_skb_len(struct sk_buff *skb)
505 {
506 	return skb->len;
507 }
508 
509 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
510 {
511 	return skb_csum_unnecessary(skb);
512 }
513 
514 static inline bool udp_skb_is_linear(struct sk_buff *skb)
515 {
516 	return !skb_is_nonlinear(skb);
517 }
518 #endif
519 
520 static inline int copy_linear_skb(struct sk_buff *skb, int len, int off,
521 				  struct iov_iter *to)
522 {
523 	return copy_to_iter_full(skb->data + off, len, to) ? 0 : -EFAULT;
524 }
525 
526 /*
527  * 	SNMP statistics for UDP and UDP-Lite
528  */
529 #define UDP_INC_STATS(net, field, is_udplite)		      do { \
530 	if (is_udplite) SNMP_INC_STATS((net)->mib.udplite_statistics, field);       \
531 	else		SNMP_INC_STATS((net)->mib.udp_statistics, field);  }  while(0)
532 #define __UDP_INC_STATS(net, field, is_udplite) 	      do { \
533 	if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_statistics, field);         \
534 	else		__SNMP_INC_STATS((net)->mib.udp_statistics, field);    }  while(0)
535 
536 #define __UDP6_INC_STATS(net, field, is_udplite)	    do { \
537 	if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);\
538 	else		__SNMP_INC_STATS((net)->mib.udp_stats_in6, field);  \
539 } while(0)
540 #define UDP6_INC_STATS(net, field, __lite)		    do { \
541 	if (__lite) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);  \
542 	else	    SNMP_INC_STATS((net)->mib.udp_stats_in6, field);      \
543 } while(0)
544 
545 #if IS_ENABLED(CONFIG_IPV6)
546 #define __UDPX_MIB(sk, ipv4)						\
547 ({									\
548 	ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics :	\
549 				 sock_net(sk)->mib.udp_statistics) :	\
550 		(IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 :	\
551 				 sock_net(sk)->mib.udp_stats_in6);	\
552 })
553 #else
554 #define __UDPX_MIB(sk, ipv4)						\
555 ({									\
556 	IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics :		\
557 			 sock_net(sk)->mib.udp_statistics;		\
558 })
559 #endif
560 
561 #define __UDPX_INC_STATS(sk, field) \
562 	__SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field)
563 
564 #ifdef CONFIG_PROC_FS
565 struct udp_seq_afinfo {
566 	sa_family_t			family;
567 	struct udp_table		*udp_table;
568 };
569 
570 struct udp_iter_state {
571 	struct seq_net_private  p;
572 	int			bucket;
573 };
574 
575 void *udp_seq_start(struct seq_file *seq, loff_t *pos);
576 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
577 void udp_seq_stop(struct seq_file *seq, void *v);
578 
579 extern const struct seq_operations udp_seq_ops;
580 extern const struct seq_operations udp6_seq_ops;
581 
582 int udp4_proc_init(void);
583 void udp4_proc_exit(void);
584 #endif /* CONFIG_PROC_FS */
585 
586 int udpv4_offload_init(void);
587 
588 void udp_init(void);
589 
590 DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
591 void udp_encap_enable(void);
592 void udp_encap_disable(void);
593 #if IS_ENABLED(CONFIG_IPV6)
594 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
595 void udpv6_encap_enable(void);
596 #endif
597 
598 static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
599 					      struct sk_buff *skb, bool ipv4)
600 {
601 	netdev_features_t features = NETIF_F_SG;
602 	struct sk_buff *segs;
603 	int drop_count;
604 
605 	/*
606 	 * Segmentation in UDP receive path is only for UDP GRO, drop udp
607 	 * fragmentation offload (UFO) packets.
608 	 */
609 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
610 		drop_count = 1;
611 		goto drop;
612 	}
613 
614 	/* Avoid csum recalculation by skb_segment unless userspace explicitly
615 	 * asks for the final checksum values
616 	 */
617 	if (!inet_get_convert_csum(sk))
618 		features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
619 
620 	/* UDP segmentation expects packets of type CHECKSUM_PARTIAL or
621 	 * CHECKSUM_NONE in __udp_gso_segment. UDP GRO indeed builds partial
622 	 * packets in udp_gro_complete_segment. As does UDP GSO, verified by
623 	 * udp_send_skb. But when those packets are looped in dev_loopback_xmit
624 	 * their ip_summed CHECKSUM_NONE is changed to CHECKSUM_UNNECESSARY.
625 	 * Reset in this specific case, where PARTIAL is both correct and
626 	 * required.
627 	 */
628 	if (skb->pkt_type == PACKET_LOOPBACK)
629 		skb->ip_summed = CHECKSUM_PARTIAL;
630 
631 	/* the GSO CB lays after the UDP one, no need to save and restore any
632 	 * CB fragment
633 	 */
634 	segs = __skb_gso_segment(skb, features, false);
635 	if (IS_ERR_OR_NULL(segs)) {
636 		drop_count = skb_shinfo(skb)->gso_segs;
637 		goto drop;
638 	}
639 
640 	consume_skb(skb);
641 	return segs;
642 
643 drop:
644 	sk_drops_add(sk, drop_count);
645 	SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, drop_count);
646 	kfree_skb(skb);
647 	return NULL;
648 }
649 
650 static inline void udp_post_segment_fix_csum(struct sk_buff *skb)
651 {
652 	/* UDP-lite can't land here - no GRO */
653 	WARN_ON_ONCE(UDP_SKB_CB(skb)->partial_cov);
654 
655 	/* UDP packets generated with UDP_SEGMENT and traversing:
656 	 *
657 	 * UDP tunnel(xmit) -> veth (segmentation) -> veth (gro) -> UDP tunnel (rx)
658 	 *
659 	 * can reach an UDP socket with CHECKSUM_NONE, because
660 	 * __iptunnel_pull_header() converts CHECKSUM_PARTIAL into NONE.
661 	 * SKB_GSO_UDP_L4 or SKB_GSO_FRAGLIST packets with no UDP tunnel will
662 	 * have a valid checksum, as the GRO engine validates the UDP csum
663 	 * before the aggregation and nobody strips such info in between.
664 	 * Instead of adding another check in the tunnel fastpath, we can force
665 	 * a valid csum after the segmentation.
666 	 * Additionally fixup the UDP CB.
667 	 */
668 	UDP_SKB_CB(skb)->cscov = skb->len;
669 	if (skb->ip_summed == CHECKSUM_NONE && !skb->csum_valid)
670 		skb->csum_valid = 1;
671 }
672 
673 #ifdef CONFIG_BPF_SYSCALL
674 struct sk_psock;
675 int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
676 #endif
677 
678 #endif	/* _UDP_H */
679