udp.h 19 KB

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