minisocks.c 7.2 KB

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