minisocks.c 7.4 KB

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