request_sock.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * NET Generic infrastructure for Network protocols.
  4. *
  5. * Definitions for request_sock
  6. *
  7. * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  8. *
  9. * From code originally in include/net/tcp.h
  10. */
  11. #ifndef _REQUEST_SOCK_H
  12. #define _REQUEST_SOCK_H
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/types.h>
  16. #include <linux/bug.h>
  17. #include <linux/refcount.h>
  18. #include <net/sock.h>
  19. #include <net/rstreason.h>
  20. struct request_sock;
  21. struct sk_buff;
  22. struct dst_entry;
  23. struct proto;
  24. struct request_sock_ops {
  25. int family;
  26. unsigned int obj_size;
  27. struct kmem_cache *slab;
  28. char *slab_name;
  29. int (*rtx_syn_ack)(const struct sock *sk,
  30. struct request_sock *req);
  31. void (*send_ack)(const struct sock *sk, struct sk_buff *skb,
  32. struct request_sock *req);
  33. void (*send_reset)(const struct sock *sk,
  34. struct sk_buff *skb,
  35. enum sk_rst_reason reason);
  36. void (*destructor)(struct request_sock *req);
  37. void (*syn_ack_timeout)(const struct request_sock *req);
  38. };
  39. int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req);
  40. struct saved_syn {
  41. u32 mac_hdrlen;
  42. u32 network_hdrlen;
  43. u32 tcp_hdrlen;
  44. u8 data[];
  45. };
  46. /* struct request_sock - mini sock to represent a connection request
  47. */
  48. struct request_sock {
  49. struct sock_common __req_common;
  50. #define rsk_refcnt __req_common.skc_refcnt
  51. #define rsk_hash __req_common.skc_hash
  52. #define rsk_listener __req_common.skc_listener
  53. #define rsk_window_clamp __req_common.skc_window_clamp
  54. #define rsk_rcv_wnd __req_common.skc_rcv_wnd
  55. struct request_sock *dl_next;
  56. u16 mss;
  57. u8 num_retrans; /* number of retransmits */
  58. u8 syncookie:1; /* True if
  59. * 1) tcpopts needs to be encoded in
  60. * TS of SYN+ACK
  61. * 2) ACK is validated by BPF kfunc.
  62. */
  63. u8 num_timeout:7; /* number of timeouts */
  64. u32 ts_recent;
  65. struct timer_list rsk_timer;
  66. const struct request_sock_ops *rsk_ops;
  67. struct sock *sk;
  68. struct saved_syn *saved_syn;
  69. u32 secid;
  70. u32 peer_secid;
  71. u32 timeout;
  72. };
  73. static inline struct request_sock *inet_reqsk(const struct sock *sk)
  74. {
  75. return (struct request_sock *)sk;
  76. }
  77. static inline struct sock *req_to_sk(struct request_sock *req)
  78. {
  79. return (struct sock *)req;
  80. }
  81. /**
  82. * skb_steal_sock - steal a socket from an sk_buff
  83. * @skb: sk_buff to steal the socket from
  84. * @refcounted: is set to true if the socket is reference-counted
  85. * @prefetched: is set to true if the socket was assigned from bpf
  86. */
  87. static inline struct sock *skb_steal_sock(struct sk_buff *skb,
  88. bool *refcounted, bool *prefetched)
  89. {
  90. struct sock *sk = skb->sk;
  91. if (!sk) {
  92. *prefetched = false;
  93. *refcounted = false;
  94. return NULL;
  95. }
  96. *prefetched = skb_sk_is_prefetched(skb);
  97. if (*prefetched) {
  98. #if IS_ENABLED(CONFIG_SYN_COOKIES)
  99. if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) {
  100. struct request_sock *req = inet_reqsk(sk);
  101. *refcounted = false;
  102. sk = req->rsk_listener;
  103. req->rsk_listener = NULL;
  104. return sk;
  105. }
  106. #endif
  107. *refcounted = sk_is_refcounted(sk);
  108. } else {
  109. *refcounted = true;
  110. }
  111. skb->destructor = NULL;
  112. skb->sk = NULL;
  113. return sk;
  114. }
  115. static inline void __reqsk_free(struct request_sock *req)
  116. {
  117. req->rsk_ops->destructor(req);
  118. if (req->rsk_listener)
  119. sock_put(req->rsk_listener);
  120. kfree(req->saved_syn);
  121. kmem_cache_free(req->rsk_ops->slab, req);
  122. }
  123. static inline void reqsk_free(struct request_sock *req)
  124. {
  125. DEBUG_NET_WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0);
  126. __reqsk_free(req);
  127. }
  128. static inline void reqsk_put(struct request_sock *req)
  129. {
  130. if (refcount_dec_and_test(&req->rsk_refcnt))
  131. __reqsk_free(req);
  132. }
  133. /*
  134. * For a TCP Fast Open listener -
  135. * lock - protects the access to all the reqsk, which is co-owned by
  136. * the listener and the child socket.
  137. * qlen - pending TFO requests (still in TCP_SYN_RECV).
  138. * max_qlen - max TFO reqs allowed before TFO is disabled.
  139. *
  140. * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
  141. * structure above. But there is some implementation difficulty due to
  142. * listen_sock being part of request_sock_queue hence will be freed when
  143. * a listener is stopped. But TFO related fields may continue to be
  144. * accessed even after a listener is closed, until its sk_refcnt drops
  145. * to 0 implying no more outstanding TFO reqs. One solution is to keep
  146. * listen_opt around until sk_refcnt drops to 0. But there is some other
  147. * complexity that needs to be resolved. E.g., a listener can be disabled
  148. * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
  149. */
  150. struct fastopen_queue {
  151. struct request_sock *rskq_rst_head; /* Keep track of past TFO */
  152. struct request_sock *rskq_rst_tail; /* requests that caused RST.
  153. * This is part of the defense
  154. * against spoofing attack.
  155. */
  156. spinlock_t lock;
  157. int qlen; /* # of pending (TCP_SYN_RECV) reqs */
  158. int max_qlen; /* != 0 iff TFO is currently enabled */
  159. struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */
  160. };
  161. /** struct request_sock_queue - queue of request_socks
  162. *
  163. * @rskq_accept_head - FIFO head of established children
  164. * @rskq_accept_tail - FIFO tail of established children
  165. * @rskq_defer_accept - User waits for some data after accept()
  166. *
  167. */
  168. struct request_sock_queue {
  169. spinlock_t rskq_lock;
  170. u8 rskq_defer_accept;
  171. u32 synflood_warned;
  172. atomic_t qlen;
  173. atomic_t young;
  174. struct request_sock *rskq_accept_head;
  175. struct request_sock *rskq_accept_tail;
  176. struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine
  177. * if TFO is enabled.
  178. */
  179. };
  180. void reqsk_queue_alloc(struct request_sock_queue *queue);
  181. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  182. bool reset);
  183. static inline bool reqsk_queue_empty(const struct request_sock_queue *queue)
  184. {
  185. return READ_ONCE(queue->rskq_accept_head) == NULL;
  186. }
  187. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
  188. struct sock *parent)
  189. {
  190. struct request_sock *req;
  191. spin_lock_bh(&queue->rskq_lock);
  192. req = queue->rskq_accept_head;
  193. if (req) {
  194. sk_acceptq_removed(parent);
  195. WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
  196. if (queue->rskq_accept_head == NULL)
  197. queue->rskq_accept_tail = NULL;
  198. }
  199. spin_unlock_bh(&queue->rskq_lock);
  200. return req;
  201. }
  202. static inline void reqsk_queue_removed(struct request_sock_queue *queue,
  203. const struct request_sock *req)
  204. {
  205. if (req->num_timeout == 0)
  206. atomic_dec(&queue->young);
  207. atomic_dec(&queue->qlen);
  208. }
  209. static inline void reqsk_queue_added(struct request_sock_queue *queue)
  210. {
  211. atomic_inc(&queue->young);
  212. atomic_inc(&queue->qlen);
  213. }
  214. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  215. {
  216. return atomic_read(&queue->qlen);
  217. }
  218. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  219. {
  220. return atomic_read(&queue->young);
  221. }
  222. /* RFC 7323 2.3 Using the Window Scale Option
  223. * The window field (SEG.WND) of every outgoing segment, with the
  224. * exception of <SYN> segments, MUST be right-shifted by
  225. * Rcv.Wind.Shift bits.
  226. *
  227. * This means the SEG.WND carried in SYNACK can not exceed 65535.
  228. * We use this property to harden TCP stack while in NEW_SYN_RECV state.
  229. */
  230. static inline u32 tcp_synack_window(const struct request_sock *req)
  231. {
  232. return min(req->rsk_rcv_wnd, 65535U);
  233. }
  234. #endif /* _REQUEST_SOCK_H */