blob: 4a18dc37056f45c25f339ce79f0cac4d0cf49b7c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* 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/sock.h>
25#include <net/snmp.h>
26#include <net/ip.h>
27#include <linux/ipv6.h>
28#include <linux/seq_file.h>
29#include <linux/poll.h>
30
31/**
32 * struct udp_skb_cb - UDP(-Lite) private variables
33 *
34 * @header: private variables used by IPv4/IPv6
35 * @cscov: checksum coverage length (UDP-Lite only)
36 * @partial_cov: if set indicates partial csum coverage
37 */
38struct udp_skb_cb {
39 union {
40 struct inet_skb_parm h4;
41#if IS_ENABLED(CONFIG_IPV6)
42 struct inet6_skb_parm h6;
43#endif
44 } header;
45 __u16 cscov;
46 __u8 partial_cov;
47};
48#define UDP_SKB_CB(__skb) ((struct udp_skb_cb *)((__skb)->cb))
49
50/**
51 * struct udp_hslot - UDP hash slot
52 *
53 * @head: head of list of sockets
54 * @count: number of sockets in 'head' list
55 * @lock: spinlock protecting changes to head/count
56 */
57struct udp_hslot {
58 struct hlist_head head;
59 int count;
60 spinlock_t lock;
61} __attribute__((aligned(2 * sizeof(long))));
62
63/**
64 * struct udp_table - UDP table
65 *
66 * @hash: hash table, sockets are hashed on (local port)
67 * @hash2: hash table, sockets are hashed on (local port, local address)
68 * @mask: number of slots in hash tables, minus 1
69 * @log: log2(number of slots in hash table)
70 */
71struct udp_table {
72 struct udp_hslot *hash;
73 struct udp_hslot *hash2;
74 unsigned int mask;
75 unsigned int log;
76};
77extern struct udp_table udp_table;
78void udp_table_init(struct udp_table *, const char *);
79static inline struct udp_hslot *udp_hashslot(struct udp_table *table,
80 struct net *net, unsigned int num)
81{
82 return &table->hash[udp_hashfn(net, num, table->mask)];
83}
84/*
85 * For secondary hash, net_hash_mix() is performed before calling
86 * udp_hashslot2(), this explains difference with udp_hashslot()
87 */
88static inline struct udp_hslot *udp_hashslot2(struct udp_table *table,
89 unsigned int hash)
90{
91 return &table->hash2[hash & table->mask];
92}
93
94extern struct proto udp_prot;
95
96extern atomic_long_t udp_memory_allocated;
97
98/* sysctl variables for udp */
99extern long sysctl_udp_mem[3];
100extern int sysctl_udp_rmem_min;
101extern int sysctl_udp_wmem_min;
102
103struct sk_buff;
104
105/*
106 * Generic checksumming routines for UDP(-Lite) v4 and v6
107 */
108static inline __sum16 __udp_lib_checksum_complete(struct sk_buff *skb)
109{
110 return (UDP_SKB_CB(skb)->cscov == skb->len ?
111 __skb_checksum_complete(skb) :
112 __skb_checksum_complete_head(skb, UDP_SKB_CB(skb)->cscov));
113}
114
115static inline int udp_lib_checksum_complete(struct sk_buff *skb)
116{
117 return !skb_csum_unnecessary(skb) &&
118 __udp_lib_checksum_complete(skb);
119}
120
121/**
122 * udp_csum_outgoing - compute UDPv4/v6 checksum over fragments
123 * @sk: socket we are writing to
124 * @skb: sk_buff containing the filled-in UDP header
125 * (checksum field must be zeroed out)
126 */
127static inline __wsum udp_csum_outgoing(struct sock *sk, struct sk_buff *skb)
128{
129 __wsum csum = csum_partial(skb_transport_header(skb),
130 sizeof(struct udphdr), 0);
131 skb_queue_walk(&sk->sk_write_queue, skb) {
132 csum = csum_add(csum, skb->csum);
133 }
134 return csum;
135}
136
137static inline __wsum udp_csum(struct sk_buff *skb)
138{
139 __wsum csum = csum_partial(skb_transport_header(skb),
140 sizeof(struct udphdr), skb->csum);
141
142 for (skb = skb_shinfo(skb)->frag_list; skb; skb = skb->next) {
143 csum = csum_add(csum, skb->csum);
144 }
145 return csum;
146}
147
148static inline __sum16 udp_v4_check(int len, __be32 saddr,
149 __be32 daddr, __wsum base)
150{
151 return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base);
152}
153
154void udp_set_csum(bool nocheck, struct sk_buff *skb,
155 __be32 saddr, __be32 daddr, int len);
156
157static inline void udp_csum_pull_header(struct sk_buff *skb)
158{
159 if (!skb->csum_valid && skb->ip_summed == CHECKSUM_NONE)
160 skb->csum = csum_partial(skb->data, sizeof(struct udphdr),
161 skb->csum);
162 skb_pull_rcsum(skb, sizeof(struct udphdr));
163 UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr);
164}
165
166typedef struct sock *(*udp_lookup_t)(struct sk_buff *skb, __be16 sport,
167 __be16 dport);
168
169struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
170 struct udphdr *uh, struct sock *sk);
171int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup);
172void udp_v6_early_demux(struct sk_buff *skb);
173
174struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
175 netdev_features_t features, bool is_ipv6);
176
177static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
178{
179 struct udphdr *uh;
180 unsigned int hlen, off;
181
182 off = skb_gro_offset(skb);
183 hlen = off + sizeof(*uh);
184 uh = skb_gro_header_fast(skb, off);
185 if (skb_gro_header_hard(skb, hlen))
186 uh = skb_gro_header_slow(skb, hlen, off);
187
188 return uh;
189}
190
191/* hash routines shared between UDPv4/6 and UDP-Litev4/6 */
192static inline int udp_lib_hash(struct sock *sk)
193{
194 BUG();
195 return 0;
196}
197
198void udp_lib_unhash(struct sock *sk);
199void udp_lib_rehash(struct sock *sk, u16 new_hash);
200
201static inline void udp_lib_close(struct sock *sk, long timeout)
202{
203 sk_common_release(sk);
204}
205
206int udp_lib_get_port(struct sock *sk, unsigned short snum,
207 unsigned int hash2_nulladdr);
208
209u32 udp_flow_hashrnd(void);
210
211static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
212 int min, int max, bool use_eth)
213{
214 u32 hash;
215
216 if (min >= max) {
217 /* Use default range */
218 inet_get_local_port_range(net, &min, &max);
219 }
220
221 hash = skb_get_hash(skb);
222 if (unlikely(!hash)) {
223 if (use_eth) {
224 /* Can't find a normal hash, caller has indicated an
225 * Ethernet packet so use that to compute a hash.
226 */
227 hash = jhash(skb->data, 2 * ETH_ALEN,
228 (__force u32) skb->protocol);
229 } else {
230 /* Can't derive any sort of hash for the packet, set
231 * to some consistent random value.
232 */
233 hash = udp_flow_hashrnd();
234 }
235 }
236
237 /* Since this is being sent on the wire obfuscate hash a bit
238 * to minimize possbility that any useful information to an
239 * attacker is leaked. Only upper 16 bits are relevant in the
240 * computation for 16 bit port value.
241 */
242 hash ^= hash << 16;
243
244 return htons((((u64) hash * (max - min)) >> 32) + min);
245}
246
247static inline int udp_rqueue_get(struct sock *sk)
248{
249 return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit);
250}
251
252static inline bool udp_sk_bound_dev_eq(struct net *net, int bound_dev_if,
253 int dif, int sdif)
254{
255#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
256 return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept),
257 bound_dev_if, dif, sdif);
258#else
259 return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
260#endif
261}
262
263/* net/ipv4/udp.c */
264void udp_destruct_common(struct sock *sk);
265void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len);
266int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb);
267void udp_skb_destructor(struct sock *sk, struct sk_buff *skb);
268struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
269 int noblock, int *off, int *err);
270static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags,
271 int noblock, int *err)
272{
273 int off = 0;
274
275 return __skb_recv_udp(sk, flags, noblock, &off, err);
276}
277
278int udp_v4_early_demux(struct sk_buff *skb);
279bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst);
280int udp_get_port(struct sock *sk, unsigned short snum,
281 int (*saddr_cmp)(const struct sock *,
282 const struct sock *));
283int udp_err(struct sk_buff *, u32);
284int udp_abort(struct sock *sk, int err);
285int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
286int udp_push_pending_frames(struct sock *sk);
287void udp_flush_pending_frames(struct sock *sk);
288int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
289void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
290int udp_rcv(struct sk_buff *skb);
291int udp_ioctl(struct sock *sk, int cmd, unsigned long arg);
292int udp_init_sock(struct sock *sk);
293int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
294int __udp_disconnect(struct sock *sk, int flags);
295int udp_disconnect(struct sock *sk, int flags);
296__poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
297struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
298 netdev_features_t features,
299 bool is_ipv6);
300int udp_lib_getsockopt(struct sock *sk, int level, int optname,
301 char __user *optval, int __user *optlen);
302int udp_lib_setsockopt(struct sock *sk, int level, int optname,
303 char __user *optval, unsigned int optlen,
304 int (*push_pending_frames)(struct sock *));
305struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
306 __be32 daddr, __be16 dport, int dif);
307struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
308 __be32 daddr, __be16 dport, int dif, int sdif,
309 struct udp_table *tbl, struct sk_buff *skb);
310struct sock *udp4_lib_lookup_skb(struct sk_buff *skb,
311 __be16 sport, __be16 dport);
312struct sock *udp6_lib_lookup(struct net *net,
313 const struct in6_addr *saddr, __be16 sport,
314 const struct in6_addr *daddr, __be16 dport,
315 int dif);
316struct sock *__udp6_lib_lookup(struct net *net,
317 const struct in6_addr *saddr, __be16 sport,
318 const struct in6_addr *daddr, __be16 dport,
319 int dif, int sdif, struct udp_table *tbl,
320 struct sk_buff *skb);
321struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
322 __be16 sport, __be16 dport);
323
324/* UDP uses skb->dev_scratch to cache as much information as possible and avoid
325 * possibly multiple cache miss on dequeue()
326 */
327struct udp_dev_scratch {
328 /* skb->truesize and the stateless bit are embedded in a single field;
329 * do not use a bitfield since the compiler emits better/smaller code
330 * this way
331 */
332 u32 _tsize_state;
333
334#if BITS_PER_LONG == 64
335 /* len and the bit needed to compute skb_csum_unnecessary
336 * will be on cold cache lines at recvmsg time.
337 * skb->len can be stored on 16 bits since the udp header has been
338 * already validated and pulled.
339 */
340 u16 len;
341 bool is_linear;
342 bool csum_unnecessary;
343#endif
344};
345
346static inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb)
347{
348 return (struct udp_dev_scratch *)&skb->dev_scratch;
349}
350
351#if BITS_PER_LONG == 64
352static inline unsigned int udp_skb_len(struct sk_buff *skb)
353{
354 return udp_skb_scratch(skb)->len;
355}
356
357static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
358{
359 return udp_skb_scratch(skb)->csum_unnecessary;
360}
361
362static inline bool udp_skb_is_linear(struct sk_buff *skb)
363{
364 return udp_skb_scratch(skb)->is_linear;
365}
366
367#else
368static inline unsigned int udp_skb_len(struct sk_buff *skb)
369{
370 return skb->len;
371}
372
373static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
374{
375 return skb_csum_unnecessary(skb);
376}
377
378static inline bool udp_skb_is_linear(struct sk_buff *skb)
379{
380 return !skb_is_nonlinear(skb);
381}
382#endif
383
384static inline int copy_linear_skb(struct sk_buff *skb, int len, int off,
385 struct iov_iter *to)
386{
387 int n;
388
389 n = copy_to_iter(skb->data + off, len, to);
390 if (n == len)
391 return 0;
392
393 iov_iter_revert(to, n);
394 return -EFAULT;
395}
396
397/*
398 * SNMP statistics for UDP and UDP-Lite
399 */
400#define UDP_INC_STATS(net, field, is_udplite) do { \
401 if (is_udplite) SNMP_INC_STATS((net)->mib.udplite_statistics, field); \
402 else SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0)
403#define __UDP_INC_STATS(net, field, is_udplite) do { \
404 if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_statistics, field); \
405 else __SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0)
406
407#define __UDP6_INC_STATS(net, field, is_udplite) do { \
408 if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);\
409 else __SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \
410} while(0)
411#define UDP6_INC_STATS(net, field, __lite) do { \
412 if (__lite) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field); \
413 else SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \
414} while(0)
415
416#if IS_ENABLED(CONFIG_IPV6)
417#define __UDPX_MIB(sk, ipv4) \
418({ \
419 ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \
420 sock_net(sk)->mib.udp_statistics) : \
421 (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 : \
422 sock_net(sk)->mib.udp_stats_in6); \
423})
424#else
425#define __UDPX_MIB(sk, ipv4) \
426({ \
427 IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \
428 sock_net(sk)->mib.udp_statistics; \
429})
430#endif
431
432#define __UDPX_INC_STATS(sk, field) \
433 __SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field)
434
435#ifdef CONFIG_PROC_FS
436struct udp_seq_afinfo {
437 sa_family_t family;
438 struct udp_table *udp_table;
439};
440
441struct udp_iter_state {
442 struct seq_net_private p;
443 int bucket;
444};
445
446void *udp_seq_start(struct seq_file *seq, loff_t *pos);
447void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
448void udp_seq_stop(struct seq_file *seq, void *v);
449
450extern const struct seq_operations udp_seq_ops;
451extern const struct seq_operations udp6_seq_ops;
452
453int udp4_proc_init(void);
454void udp4_proc_exit(void);
455#endif /* CONFIG_PROC_FS */
456
457int udpv4_offload_init(void);
458
459void udp_init(void);
460
461DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
462void udp_encap_enable(void);
463void udp_encap_disable(void);
464#if IS_ENABLED(CONFIG_IPV6)
465DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
466void udpv6_encap_enable(void);
467#endif
468
469static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
470 struct sk_buff *skb, bool ipv4)
471{
472 netdev_features_t features = NETIF_F_SG;
473 struct sk_buff *segs;
474
475 /* Avoid csum recalculation by skb_segment unless userspace explicitly
476 * asks for the final checksum values
477 */
478 if (!inet_get_convert_csum(sk))
479 features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
480
481 /* UDP segmentation expects packets of type CHECKSUM_PARTIAL or
482 * CHECKSUM_NONE in __udp_gso_segment. UDP GRO indeed builds partial
483 * packets in udp_gro_complete_segment. As does UDP GSO, verified by
484 * udp_send_skb. But when those packets are looped in dev_loopback_xmit
485 * their ip_summed CHECKSUM_NONE is changed to CHECKSUM_UNNECESSARY.
486 * Reset in this specific case, where PARTIAL is both correct and
487 * required.
488 */
489 if (skb->pkt_type == PACKET_LOOPBACK)
490 skb->ip_summed = CHECKSUM_PARTIAL;
491
492 /* the GSO CB lays after the UDP one, no need to save and restore any
493 * CB fragment
494 */
495 segs = __skb_gso_segment(skb, features, false);
496 if (IS_ERR_OR_NULL(segs)) {
497 int segs_nr = skb_shinfo(skb)->gso_segs;
498
499 atomic_add(segs_nr, &sk->sk_drops);
500 SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, segs_nr);
501 kfree_skb(skb);
502 return NULL;
503 }
504
505 consume_skb(skb);
506 return segs;
507}
508
509#endif /* _UDP_H */