udp.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 protocol.
  8. *
  9. * Version: @(#)udp.h 1.0.2 04/28/93
  10. *
  11. * Author: Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. */
  13. #ifndef _LINUX_UDP_H
  14. #define _LINUX_UDP_H
  15. #include <net/inet_sock.h>
  16. #include <linux/skbuff.h>
  17. #include <net/netns/hash.h>
  18. #include <uapi/linux/udp.h>
  19. static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
  20. {
  21. return (struct udphdr *)skb_transport_header(skb);
  22. }
  23. #define UDP_HTABLE_SIZE_MIN (CONFIG_BASE_SMALL ? 128 : 256)
  24. static inline u32 udp_hashfn(const struct net *net, u32 num, u32 mask)
  25. {
  26. return (num + net_hash_mix(net)) & mask;
  27. }
  28. struct udp_sock {
  29. /* inet_sock has to be the first member */
  30. struct inet_sock inet;
  31. #define udp_port_hash inet.sk.__sk_common.skc_u16hashes[0]
  32. #define udp_portaddr_hash inet.sk.__sk_common.skc_u16hashes[1]
  33. #define udp_portaddr_node inet.sk.__sk_common.skc_portaddr_node
  34. int pending; /* Any pending frames ? */
  35. unsigned int corkflag; /* Cork is required */
  36. __u8 encap_type; /* Is this an Encapsulation socket? */
  37. unsigned char no_check6_tx:1,/* Send zero UDP6 checksums on TX? */
  38. no_check6_rx:1,/* Allow zero UDP6 checksums on RX? */
  39. encap_enabled:1, /* This socket enabled encap
  40. * processing; UDP tunnels and
  41. * different encapsulation layer set
  42. * this
  43. */
  44. gro_enabled:1, /* Request GRO aggregation */
  45. accept_udp_l4:1,
  46. accept_udp_fraglist:1;
  47. /*
  48. * Following member retains the information to create a UDP header
  49. * when the socket is uncorked.
  50. */
  51. __u16 len; /* total length of pending frames */
  52. __u16 gso_size;
  53. /*
  54. * Fields specific to UDP-Lite.
  55. */
  56. __u16 pcslen;
  57. __u16 pcrlen;
  58. /* indicator bits used by pcflag: */
  59. #define UDPLITE_BIT 0x1 /* set by udplite proto init function */
  60. #define UDPLITE_SEND_CC 0x2 /* set via udplite setsockopt */
  61. #define UDPLITE_RECV_CC 0x4 /* set via udplite setsocktopt */
  62. __u8 pcflag; /* marks socket as UDP-Lite if > 0 */
  63. __u8 unused[3];
  64. /*
  65. * For encapsulation sockets.
  66. */
  67. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  68. void (*encap_err_rcv)(struct sock *sk, struct sk_buff *skb, unsigned int udp_offset);
  69. int (*encap_err_lookup)(struct sock *sk, struct sk_buff *skb);
  70. void (*encap_destroy)(struct sock *sk);
  71. /* GRO functions for UDP socket */
  72. struct sk_buff * (*gro_receive)(struct sock *sk,
  73. struct list_head *head,
  74. struct sk_buff *skb);
  75. int (*gro_complete)(struct sock *sk,
  76. struct sk_buff *skb,
  77. int nhoff);
  78. /* udp_recvmsg try to use this before splicing sk_receive_queue */
  79. struct sk_buff_head reader_queue ____cacheline_aligned_in_smp;
  80. /* This field is dirtied by udp_recvmsg() */
  81. int forward_deficit;
  82. };
  83. #define UDP_MAX_SEGMENTS (1 << 6UL)
  84. static inline struct udp_sock *udp_sk(const struct sock *sk)
  85. {
  86. return (struct udp_sock *)sk;
  87. }
  88. static inline void udp_set_no_check6_tx(struct sock *sk, bool val)
  89. {
  90. udp_sk(sk)->no_check6_tx = val;
  91. }
  92. static inline void udp_set_no_check6_rx(struct sock *sk, bool val)
  93. {
  94. udp_sk(sk)->no_check6_rx = val;
  95. }
  96. static inline bool udp_get_no_check6_tx(struct sock *sk)
  97. {
  98. return udp_sk(sk)->no_check6_tx;
  99. }
  100. static inline bool udp_get_no_check6_rx(struct sock *sk)
  101. {
  102. return udp_sk(sk)->no_check6_rx;
  103. }
  104. static inline void udp_cmsg_recv(struct msghdr *msg, struct sock *sk,
  105. struct sk_buff *skb)
  106. {
  107. int gso_size;
  108. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
  109. gso_size = skb_shinfo(skb)->gso_size;
  110. put_cmsg(msg, SOL_UDP, UDP_GRO, sizeof(gso_size), &gso_size);
  111. }
  112. }
  113. static inline bool udp_unexpected_gso(struct sock *sk, struct sk_buff *skb)
  114. {
  115. if (!skb_is_gso(skb))
  116. return false;
  117. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4 && !udp_sk(sk)->accept_udp_l4)
  118. return true;
  119. if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST && !udp_sk(sk)->accept_udp_fraglist)
  120. return true;
  121. return false;
  122. }
  123. static inline void udp_allow_gso(struct sock *sk)
  124. {
  125. udp_sk(sk)->accept_udp_l4 = 1;
  126. udp_sk(sk)->accept_udp_fraglist = 1;
  127. }
  128. #define udp_portaddr_for_each_entry(__sk, list) \
  129. hlist_for_each_entry(__sk, list, __sk_common.skc_portaddr_node)
  130. #define udp_portaddr_for_each_entry_rcu(__sk, list) \
  131. hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node)
  132. #define IS_UDPLITE(__sk) (__sk->sk_protocol == IPPROTO_UDPLITE)
  133. #endif /* _LINUX_UDP_H */