minisocks.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/dccp/minisocks.c
  4. *
  5. * An implementation of the DCCP protocol
  6. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. */
  8. #include <linux/dccp.h>
  9. #include <linux/gfp.h>
  10. #include <linux/kernel.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/timer.h>
  13. #include <net/sock.h>
  14. #include <net/xfrm.h>
  15. #include <net/inet_timewait_sock.h>
  16. #include "ackvec.h"
  17. #include "ccid.h"
  18. #include "dccp.h"
  19. #include "feat.h"
  20. struct inet_timewait_death_row dccp_death_row = {
  21. .sysctl_max_tw_buckets = NR_FILE * 2,
  22. .hashinfo = &dccp_hashinfo,
  23. };
  24. EXPORT_SYMBOL_GPL(dccp_death_row);
  25. void dccp_time_wait(struct sock *sk, int state, int timeo)
  26. {
  27. struct inet_timewait_sock *tw;
  28. tw = inet_twsk_alloc(sk, &dccp_death_row, state);
  29. if (tw != NULL) {
  30. const struct inet_connection_sock *icsk = inet_csk(sk);
  31. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. if (tw->tw_family == PF_INET6) {
  34. tw->tw_v6_daddr = sk->sk_v6_daddr;
  35. tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  36. tw->tw_ipv6only = sk->sk_ipv6only;
  37. }
  38. #endif
  39. /* Get the TIME_WAIT timeout firing. */
  40. if (timeo < rto)
  41. timeo = rto;
  42. if (state == DCCP_TIME_WAIT)
  43. timeo = DCCP_TIMEWAIT_LEN;
  44. /* tw_timer is pinned, so we need to make sure BH are disabled
  45. * in following section, otherwise timer handler could run before
  46. * we complete the initialization.
  47. */
  48. local_bh_disable();
  49. inet_twsk_schedule(tw, timeo);
  50. /* Linkage updates.
  51. * Note that access to tw after this point is illegal.
  52. */
  53. inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
  54. local_bh_enable();
  55. } else {
  56. /* Sorry, if we're out of memory, just CLOSE this
  57. * socket up. We've got bigger problems than
  58. * non-graceful socket closings.
  59. */
  60. DCCP_WARN("time wait bucket table overflow\n");
  61. }
  62. dccp_done(sk);
  63. }
  64. struct sock *dccp_create_openreq_child(const struct sock *sk,
  65. const struct request_sock *req,
  66. const struct sk_buff *skb)
  67. {
  68. /*
  69. * Step 3: Process LISTEN state
  70. *
  71. * (* Generate a new socket and switch to that socket *)
  72. * Set S := new socket for this port pair
  73. */
  74. struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
  75. if (newsk != NULL) {
  76. struct dccp_request_sock *dreq = dccp_rsk(req);
  77. struct inet_connection_sock *newicsk = inet_csk(newsk);
  78. struct dccp_sock *newdp = dccp_sk(newsk);
  79. newdp->dccps_role = DCCP_ROLE_SERVER;
  80. newdp->dccps_hc_rx_ackvec = NULL;
  81. newdp->dccps_service_list = NULL;
  82. newdp->dccps_hc_rx_ccid = NULL;
  83. newdp->dccps_hc_tx_ccid = NULL;
  84. newdp->dccps_service = dreq->dreq_service;
  85. newdp->dccps_timestamp_echo = dreq->dreq_timestamp_echo;
  86. newdp->dccps_timestamp_time = dreq->dreq_timestamp_time;
  87. newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
  88. INIT_LIST_HEAD(&newdp->dccps_featneg);
  89. /*
  90. * Step 3: Process LISTEN state
  91. *
  92. * Choose S.ISS (initial seqno) or set from Init Cookies
  93. * Initialize S.GAR := S.ISS
  94. * Set S.ISR, S.GSR from packet (or Init Cookies)
  95. *
  96. * Setting AWL/AWH and SWL/SWH happens as part of the feature
  97. * activation below, as these windows all depend on the local
  98. * and remote Sequence Window feature values (7.5.2).
  99. */
  100. newdp->dccps_iss = dreq->dreq_iss;
  101. newdp->dccps_gss = dreq->dreq_gss;
  102. newdp->dccps_gar = newdp->dccps_iss;
  103. newdp->dccps_isr = dreq->dreq_isr;
  104. newdp->dccps_gsr = dreq->dreq_gsr;
  105. /*
  106. * Activate features: initialise CCIDs, sequence windows etc.
  107. */
  108. if (dccp_feat_activate_values(newsk, &dreq->dreq_featneg)) {
  109. sk_free_unlock_clone(newsk);
  110. return NULL;
  111. }
  112. dccp_init_xmit_timers(newsk);
  113. __DCCP_INC_STATS(DCCP_MIB_PASSIVEOPENS);
  114. }
  115. return newsk;
  116. }
  117. EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
  118. /*
  119. * Process an incoming packet for RESPOND sockets represented
  120. * as an request_sock.
  121. */
  122. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  123. struct request_sock *req)
  124. {
  125. struct sock *child = NULL;
  126. struct dccp_request_sock *dreq = dccp_rsk(req);
  127. bool own_req;
  128. /* TCP/DCCP listeners became lockless.
  129. * DCCP stores complex state in its request_sock, so we need
  130. * a protection for them, now this code runs without being protected
  131. * by the parent (listener) lock.
  132. */
  133. spin_lock_bh(&dreq->dreq_lock);
  134. /* Check for retransmitted REQUEST */
  135. if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
  136. if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dreq->dreq_gsr)) {
  137. dccp_pr_debug("Retransmitted REQUEST\n");
  138. dreq->dreq_gsr = DCCP_SKB_CB(skb)->dccpd_seq;
  139. /*
  140. * Send another RESPONSE packet
  141. * To protect against Request floods, increment retrans
  142. * counter (backoff, monitored by dccp_response_timer).
  143. */
  144. inet_rtx_syn_ack(sk, req);
  145. }
  146. /* Network Duplicate, discard packet */
  147. goto out;
  148. }
  149. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  150. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
  151. dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
  152. goto drop;
  153. /* Invalid ACK */
  154. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  155. dreq->dreq_iss, dreq->dreq_gss)) {
  156. dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
  157. "dreq_iss=%llu, dreq_gss=%llu\n",
  158. (unsigned long long)
  159. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  160. (unsigned long long) dreq->dreq_iss,
  161. (unsigned long long) dreq->dreq_gss);
  162. goto drop;
  163. }
  164. if (dccp_parse_options(sk, dreq, skb))
  165. goto drop;
  166. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
  167. req, &own_req);
  168. if (child) {
  169. child = inet_csk_complete_hashdance(sk, child, req, own_req);
  170. goto out;
  171. }
  172. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  173. drop:
  174. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
  175. req->rsk_ops->send_reset(sk, skb);
  176. inet_csk_reqsk_queue_drop(sk, req);
  177. out:
  178. spin_unlock_bh(&dreq->dreq_lock);
  179. return child;
  180. }
  181. EXPORT_SYMBOL_GPL(dccp_check_req);
  182. /*
  183. * Queue segment on the new socket if the new socket is active,
  184. * otherwise we just shortcircuit this and continue with
  185. * the new socket.
  186. */
  187. int dccp_child_process(struct sock *parent, struct sock *child,
  188. struct sk_buff *skb)
  189. __releases(child)
  190. {
  191. int ret = 0;
  192. const int state = child->sk_state;
  193. if (!sock_owned_by_user(child)) {
  194. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
  195. skb->len);
  196. /* Wakeup parent, send SIGIO */
  197. if (state == DCCP_RESPOND && child->sk_state != state)
  198. parent->sk_data_ready(parent);
  199. } else {
  200. /* Alas, it is possible again, because we do lookup
  201. * in main socket hash table and lock on listening
  202. * socket does not protect us more.
  203. */
  204. __sk_add_backlog(child, skb);
  205. }
  206. bh_unlock_sock(child);
  207. sock_put(child);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL_GPL(dccp_child_process);
  211. void dccp_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  212. struct request_sock *rsk)
  213. {
  214. DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
  215. }
  216. EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
  217. int dccp_reqsk_init(struct request_sock *req,
  218. struct dccp_sock const *dp, struct sk_buff const *skb)
  219. {
  220. struct dccp_request_sock *dreq = dccp_rsk(req);
  221. spin_lock_init(&dreq->dreq_lock);
  222. inet_rsk(req)->ir_rmt_port = dccp_hdr(skb)->dccph_sport;
  223. inet_rsk(req)->ir_num = ntohs(dccp_hdr(skb)->dccph_dport);
  224. inet_rsk(req)->acked = 0;
  225. dreq->dreq_timestamp_echo = 0;
  226. /* inherit feature negotiation options from listening socket */
  227. return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
  228. }
  229. EXPORT_SYMBOL_GPL(dccp_reqsk_init);