udp.h 16 KB

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