udp.h 16 KB

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