xref: /linux/include/net/udp.h (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
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 *);
udp_hashslot(struct udp_table * table,const struct net * net,unsigned int num)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  */
udp_hashslot2(struct udp_table * table,unsigned int hash)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)
udp_table_hash4_init(struct udp_table * table)126 static inline void udp_table_hash4_init(struct udp_table *table)
127 {
128 }
129 
udp_hashslot4(struct udp_table * table,unsigned int hash)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 
udp_hashed4(const struct sock * sk)137 static inline bool udp_hashed4(const struct sock *sk)
138 {
139 	return false;
140 }
141 
udp_hash4_slot_size(void)142 static inline unsigned int udp_hash4_slot_size(void)
143 {
144 	return 0;
145 }
146 
udp_has_hash4(const struct udp_hslot * hslot2)147 static inline bool udp_has_hash4(const struct udp_hslot *hslot2)
148 {
149 	return false;
150 }
151 
udp_hash4_inc(struct udp_hslot * hslot2)152 static inline void udp_hash4_inc(struct udp_hslot *hslot2)
153 {
154 }
155 
udp_hash4_dec(struct udp_hslot * hslot2)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 */
udp_table_hash4_init(struct udp_table * table)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 
udp_hashslot4(struct udp_table * table,unsigned int hash)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 
udp_hashed4(const struct sock * sk)180 static inline bool udp_hashed4(const struct sock *sk)
181 {
182 	return !hlist_nulls_unhashed(&udp_sk(sk)->udp_lrpa_node);
183 }
184 
udp_hash4_slot_size(void)185 static inline unsigned int udp_hash4_slot_size(void)
186 {
187 	return sizeof(struct udp_hslot);
188 }
189 
udp_has_hash4(const struct udp_hslot * hslot2)190 static inline bool udp_has_hash4(const struct udp_hslot *hslot2)
191 {
192 	return UDP_HSLOT_MAIN(hslot2)->hash4_cnt;
193 }
194 
udp_hash4_inc(struct udp_hslot * hslot2)195 static inline void udp_hash4_inc(struct udp_hslot *hslot2)
196 {
197 	UDP_HSLOT_MAIN(hslot2)->hash4_cnt++;
198 }
199 
udp_hash4_dec(struct udp_hslot * hslot2)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  */
__udp_lib_checksum_complete(struct sk_buff * skb)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 
udp_lib_checksum_complete(struct sk_buff * skb)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  */
udp_csum_outgoing(struct sock * sk,struct sk_buff * skb)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 
udp_csum(struct sk_buff * skb)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 
udp_v4_check(int len,__be32 saddr,__be32 daddr,__wsum base)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 
udp_csum_pull_header(struct sk_buff * skb)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 
udp_lib_init_sock(struct sock * sk)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 = kzalloc_objs(*up->udp_prod_queue, nr_node_ids);
298 	if (!up->udp_prod_queue)
299 		return -ENOMEM;
300 	for (int i = 0; i < nr_node_ids; i++)
301 		init_llist_head(&up->udp_prod_queue[i].ll_root);
302 	return 0;
303 }
304 
udp_drops_inc(struct sock * sk)305 static inline void udp_drops_inc(struct sock *sk)
306 {
307 	numa_drop_add(&udp_sk(sk)->drop_counters, 1);
308 }
309 
310 /* hash routines shared between UDPv4/6 and UDP-Litev4/6 */
udp_lib_hash(struct sock * sk)311 static inline int udp_lib_hash(struct sock *sk)
312 {
313 	BUG();
314 	return 0;
315 }
316 
317 void udp_lib_unhash(struct sock *sk);
318 void udp_lib_rehash(struct sock *sk, u16 new_hash, u16 new_hash4);
319 u32 udp_ehashfn(const struct net *net, const __be32 laddr, const __u16 lport,
320 		const __be32 faddr, const __be16 fport);
321 
udp_lib_close(struct sock * sk,long timeout)322 static inline void udp_lib_close(struct sock *sk, long timeout)
323 {
324 	sk_common_release(sk);
325 }
326 
327 /* hash4 routines shared between UDPv4/6 */
328 #if IS_ENABLED(CONFIG_BASE_SMALL)
udp_lib_hash4(struct sock * sk,u16 hash)329 static inline void udp_lib_hash4(struct sock *sk, u16 hash)
330 {
331 }
332 
udp4_hash4(struct sock * sk)333 static inline void udp4_hash4(struct sock *sk)
334 {
335 }
336 #else /* !CONFIG_BASE_SMALL */
337 void udp_lib_hash4(struct sock *sk, u16 hash);
338 void udp4_hash4(struct sock *sk);
339 #endif /* CONFIG_BASE_SMALL */
340 
341 int udp_lib_get_port(struct sock *sk, unsigned short snum,
342 		     unsigned int hash2_nulladdr);
343 
344 u32 udp_flow_hashrnd(void);
345 
udp_flow_src_port(struct net * net,struct sk_buff * skb,int min,int max,bool use_eth)346 static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
347 				       int min, int max, bool use_eth)
348 {
349 	u32 hash;
350 
351 	if (min >= max) {
352 		/* Use default range */
353 		inet_get_local_port_range(net, &min, &max);
354 	}
355 
356 	hash = skb_get_hash(skb);
357 	if (unlikely(!hash)) {
358 		if (use_eth) {
359 			/* Can't find a normal hash, caller has indicated an
360 			 * Ethernet packet so use that to compute a hash.
361 			 */
362 			hash = jhash(skb->data, 2 * ETH_ALEN,
363 				     (__force u32) skb->protocol);
364 		} else {
365 			/* Can't derive any sort of hash for the packet, set
366 			 * to some consistent random value.
367 			 */
368 			hash = udp_flow_hashrnd();
369 		}
370 	}
371 
372 	/* Since this is being sent on the wire obfuscate hash a bit
373 	 * to minimize possibility that any useful information to an
374 	 * attacker is leaked. Only upper 16 bits are relevant in the
375 	 * computation for 16 bit port value.
376 	 */
377 	hash ^= hash << 16;
378 
379 	return htons((((u64) hash * (max - min)) >> 32) + min);
380 }
381 
udp_rqueue_get(struct sock * sk)382 static inline int udp_rqueue_get(struct sock *sk)
383 {
384 	return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit);
385 }
386 
udp_sk_bound_dev_eq(const struct net * net,int bound_dev_if,int dif,int sdif)387 static inline bool udp_sk_bound_dev_eq(const struct net *net, int bound_dev_if,
388 				       int dif, int sdif)
389 {
390 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
391 	return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept),
392 				 bound_dev_if, dif, sdif);
393 #else
394 	return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
395 #endif
396 }
397 
398 /* net/ipv4/udp.c */
399 void udp_destruct_common(struct sock *sk);
400 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len);
401 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb);
402 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb);
403 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, int *off,
404 			       int *err);
skb_recv_udp(struct sock * sk,unsigned int flags,int * err)405 static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags,
406 					   int *err)
407 {
408 	int off = 0;
409 
410 	return __skb_recv_udp(sk, flags, &off, err);
411 }
412 
413 enum skb_drop_reason udp_v4_early_demux(struct sk_buff *skb);
414 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst);
415 int udp_err(struct sk_buff *, u32);
416 int udp_abort(struct sock *sk, int err);
417 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
418 void udp_splice_eof(struct socket *sock);
419 int udp_push_pending_frames(struct sock *sk);
420 void udp_flush_pending_frames(struct sock *sk);
421 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
422 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
423 int udp_rcv(struct sk_buff *skb);
424 int udp_ioctl(struct sock *sk, int cmd, int *karg);
425 int udp_init_sock(struct sock *sk);
426 int udp_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
427 int __udp_disconnect(struct sock *sk, int flags);
428 int udp_disconnect(struct sock *sk, int flags);
429 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
430 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
431 				       netdev_features_t features,
432 				       bool is_ipv6);
433 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
434 		       char __user *optval, int __user *optlen);
435 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
436 		       sockptr_t optval, unsigned int optlen,
437 		       int (*push_pending_frames)(struct sock *));
438 struct sock *udp4_lib_lookup(const struct net *net, __be32 saddr, __be16 sport,
439 			     __be32 daddr, __be16 dport, int dif);
440 struct sock *__udp4_lib_lookup(const struct net *net, __be32 saddr,
441 			       __be16 sport,
442 			       __be32 daddr, __be16 dport, int dif, int sdif,
443 			       struct udp_table *tbl, struct sk_buff *skb);
444 struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
445 				 __be16 sport, __be16 dport);
446 struct sock *udp6_lib_lookup(const struct net *net,
447 			     const struct in6_addr *saddr, __be16 sport,
448 			     const struct in6_addr *daddr, __be16 dport,
449 			     int dif);
450 struct sock *__udp6_lib_lookup(const struct net *net,
451 			       const struct in6_addr *saddr, __be16 sport,
452 			       const struct in6_addr *daddr, __be16 dport,
453 			       int dif, int sdif, struct udp_table *tbl,
454 			       struct sk_buff *skb);
455 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
456 				 __be16 sport, __be16 dport);
457 int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
458 
459 /* UDP uses skb->dev_scratch to cache as much information as possible and avoid
460  * possibly multiple cache miss on dequeue()
461  */
462 struct udp_dev_scratch {
463 	/* skb->truesize and the stateless bit are embedded in a single field;
464 	 * do not use a bitfield since the compiler emits better/smaller code
465 	 * this way
466 	 */
467 	u32 _tsize_state;
468 
469 #if BITS_PER_LONG == 64
470 	/* len and the bit needed to compute skb_csum_unnecessary
471 	 * will be on cold cache lines at recvmsg time.
472 	 * skb->len can be stored on 16 bits since the udp header has been
473 	 * already validated and pulled.
474 	 */
475 	u16 len;
476 	bool is_linear;
477 	bool csum_unnecessary;
478 #endif
479 };
480 
udp_skb_scratch(struct sk_buff * skb)481 static inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb)
482 {
483 	return (struct udp_dev_scratch *)&skb->dev_scratch;
484 }
485 
486 #if BITS_PER_LONG == 64
udp_skb_len(struct sk_buff * skb)487 static inline unsigned int udp_skb_len(struct sk_buff *skb)
488 {
489 	return udp_skb_scratch(skb)->len;
490 }
491 
udp_skb_csum_unnecessary(struct sk_buff * skb)492 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
493 {
494 	return udp_skb_scratch(skb)->csum_unnecessary;
495 }
496 
udp_skb_is_linear(struct sk_buff * skb)497 static inline bool udp_skb_is_linear(struct sk_buff *skb)
498 {
499 	return udp_skb_scratch(skb)->is_linear;
500 }
501 
502 #else
udp_skb_len(struct sk_buff * skb)503 static inline unsigned int udp_skb_len(struct sk_buff *skb)
504 {
505 	return skb->len;
506 }
507 
udp_skb_csum_unnecessary(struct sk_buff * skb)508 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
509 {
510 	return skb_csum_unnecessary(skb);
511 }
512 
udp_skb_is_linear(struct sk_buff * skb)513 static inline bool udp_skb_is_linear(struct sk_buff *skb)
514 {
515 	return !skb_is_nonlinear(skb);
516 }
517 #endif
518 
copy_linear_skb(struct sk_buff * skb,int len,int off,struct iov_iter * to)519 static inline int copy_linear_skb(struct sk_buff *skb, int len, int off,
520 				  struct iov_iter *to)
521 {
522 	return copy_to_iter_full(skb->data + off, len, to) ? 0 : -EFAULT;
523 }
524 
525 /*
526  * 	SNMP statistics for UDP and UDP-Lite
527  */
528 #define UDP_INC_STATS(net, field, is_udplite)		      do { \
529 	if (unlikely(is_udplite)) SNMP_INC_STATS((net)->mib.udplite_statistics, field);	\
530 	else		SNMP_INC_STATS((net)->mib.udp_statistics, field);  }  while(0)
531 #define __UDP_INC_STATS(net, field, is_udplite) 	      do { \
532 	if (unlikely(is_udplite)) __SNMP_INC_STATS((net)->mib.udplite_statistics, field);	\
533 	else		__SNMP_INC_STATS((net)->mib.udp_statistics, field);    }  while(0)
534 
535 #define __UDP6_INC_STATS(net, field, is_udplite)	    do { \
536 	if (unlikely(is_udplite)) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);	\
537 	else		__SNMP_INC_STATS((net)->mib.udp_stats_in6, field);  \
538 } while(0)
539 #define UDP6_INC_STATS(net, field, __lite)		    do { \
540 	if (unlikely(__lite)) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);	\
541 	else	    SNMP_INC_STATS((net)->mib.udp_stats_in6, field);      \
542 } while(0)
543 
544 #if IS_ENABLED(CONFIG_IPV6)
545 #define __UDPX_MIB(sk, ipv4)						\
546 ({									\
547 	ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics :	\
548 				 sock_net(sk)->mib.udp_statistics) :	\
549 		(IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 :	\
550 				 sock_net(sk)->mib.udp_stats_in6);	\
551 })
552 #else
553 #define __UDPX_MIB(sk, ipv4)						\
554 ({									\
555 	IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics :		\
556 			 sock_net(sk)->mib.udp_statistics;		\
557 })
558 #endif
559 
560 #define __UDPX_INC_STATS(sk, field) \
561 	__SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field)
562 
563 #ifdef CONFIG_PROC_FS
564 struct udp_seq_afinfo {
565 	sa_family_t			family;
566 	struct udp_table		*udp_table;
567 };
568 
569 struct udp_iter_state {
570 	struct seq_net_private  p;
571 	int			bucket;
572 };
573 
574 void *udp_seq_start(struct seq_file *seq, loff_t *pos);
575 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
576 void udp_seq_stop(struct seq_file *seq, void *v);
577 
578 extern const struct seq_operations udp_seq_ops;
579 extern const struct seq_operations udp6_seq_ops;
580 
581 int udp4_proc_init(void);
582 void udp4_proc_exit(void);
583 #endif /* CONFIG_PROC_FS */
584 
585 int udpv4_offload_init(void);
586 
587 void udp_init(void);
588 
589 DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
590 void udp_encap_enable(void);
591 void udp_encap_disable(void);
592 #if IS_ENABLED(CONFIG_IPV6)
593 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
594 void udpv6_encap_enable(void);
595 #endif
596 
udp_rcv_segment(struct sock * sk,struct sk_buff * skb,bool ipv4)597 static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
598 					      struct sk_buff *skb, bool ipv4)
599 {
600 	netdev_features_t features = NETIF_F_SG;
601 	struct sk_buff *segs;
602 	int drop_count;
603 
604 	/*
605 	 * Segmentation in UDP receive path is only for UDP GRO, drop udp
606 	 * fragmentation offload (UFO) packets.
607 	 */
608 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
609 		drop_count = 1;
610 		goto drop;
611 	}
612 
613 	/* Avoid csum recalculation by skb_segment unless userspace explicitly
614 	 * asks for the final checksum values
615 	 */
616 	if (!inet_get_convert_csum(sk))
617 		features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
618 
619 	/* UDP segmentation expects packets of type CHECKSUM_PARTIAL or
620 	 * CHECKSUM_NONE in __udp_gso_segment. UDP GRO indeed builds partial
621 	 * packets in udp_gro_complete_segment. As does UDP GSO, verified by
622 	 * udp_send_skb. But when those packets are looped in dev_loopback_xmit
623 	 * their ip_summed CHECKSUM_NONE is changed to CHECKSUM_UNNECESSARY.
624 	 * Reset in this specific case, where PARTIAL is both correct and
625 	 * required.
626 	 */
627 	if (skb->pkt_type == PACKET_LOOPBACK)
628 		skb->ip_summed = CHECKSUM_PARTIAL;
629 
630 	/* the GSO CB lays after the UDP one, no need to save and restore any
631 	 * CB fragment
632 	 */
633 	segs = __skb_gso_segment(skb, features, false);
634 	if (IS_ERR_OR_NULL(segs)) {
635 		drop_count = skb_shinfo(skb)->gso_segs;
636 		goto drop;
637 	}
638 
639 	consume_skb(skb);
640 	return segs;
641 
642 drop:
643 	sk_drops_add(sk, drop_count);
644 	SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, drop_count);
645 	kfree_skb(skb);
646 	return NULL;
647 }
648 
udp_post_segment_fix_csum(struct sk_buff * skb)649 static inline void udp_post_segment_fix_csum(struct sk_buff *skb)
650 {
651 	/* UDP-lite can't land here - no GRO */
652 	WARN_ON_ONCE(UDP_SKB_CB(skb)->partial_cov);
653 
654 	/* UDP packets generated with UDP_SEGMENT and traversing:
655 	 *
656 	 * UDP tunnel(xmit) -> veth (segmentation) -> veth (gro) -> UDP tunnel (rx)
657 	 *
658 	 * can reach an UDP socket with CHECKSUM_NONE, because
659 	 * __iptunnel_pull_header() converts CHECKSUM_PARTIAL into NONE.
660 	 * SKB_GSO_UDP_L4 or SKB_GSO_FRAGLIST packets with no UDP tunnel will
661 	 * have a valid checksum, as the GRO engine validates the UDP csum
662 	 * before the aggregation and nobody strips such info in between.
663 	 * Instead of adding another check in the tunnel fastpath, we can force
664 	 * a valid csum after the segmentation.
665 	 * Additionally fixup the UDP CB.
666 	 */
667 	UDP_SKB_CB(skb)->cscov = skb->len;
668 	if (skb->ip_summed == CHECKSUM_NONE && !skb->csum_valid)
669 		skb->csum_valid = 1;
670 }
671 
672 #ifdef CONFIG_BPF_SYSCALL
673 struct sk_psock;
674 int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
675 #endif
676 
677 #endif	/* _UDP_H */
678