minisocks.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * net/dccp/minisocks.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/dccp.h>
  13. #include <linux/gfp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/timer.h>
  17. #include <net/sock.h>
  18. #include <net/xfrm.h>
  19. #include <net/inet_timewait_sock.h>
  20. #include "ackvec.h"
  21. #include "ccid.h"
  22. #include "dccp.h"
  23. #include "feat.h"
  24. struct inet_timewait_death_row dccp_death_row = {
  25. .sysctl_max_tw_buckets = NR_FILE * 2,
  26. .hashinfo = &dccp_hashinfo,
  27. };
  28. EXPORT_SYMBOL_GPL(dccp_death_row);
  29. void dccp_time_wait(struct sock *sk, int state, int timeo)
  30. {
  31. struct inet_timewait_sock *tw;
  32. tw = inet_twsk_alloc(sk, &dccp_death_row, state);
  33. if (tw != NULL) {
  34. const struct inet_connection_sock *icsk = inet_csk(sk);
  35. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  36. #if IS_ENABLED(CONFIG_IPV6)
  37. if (tw->tw_family == PF_INET6) {
  38. tw->tw_v6_daddr = sk->sk_v6_daddr;
  39. tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  40. tw->tw_ipv6only = sk->sk_ipv6only;
  41. }
  42. #endif
  43. /* Get the TIME_WAIT timeout firing. */
  44. if (timeo < rto)
  45. timeo = rto;
  46. tw->tw_timeout = DCCP_TIMEWAIT_LEN;
  47. if (state == DCCP_TIME_WAIT)
  48. timeo = DCCP_TIMEWAIT_LEN;
  49. inet_twsk_schedule(tw, timeo);
  50. /* Linkage updates. */
  51. __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
  52. inet_twsk_put(tw);
  53. } else {
  54. /* Sorry, if we're out of memory, just CLOSE this
  55. * socket up. We've got bigger problems than
  56. * non-graceful socket closings.
  57. */
  58. DCCP_WARN("time wait bucket table overflow\n");
  59. }
  60. dccp_done(sk);
  61. }
  62. struct sock *dccp_create_openreq_child(const struct sock *sk,
  63. const struct request_sock *req,
  64. const struct sk_buff *skb)
  65. {
  66. /*
  67. * Step 3: Process LISTEN state
  68. *
  69. * (* Generate a new socket and switch to that socket *)
  70. * Set S := new socket for this port pair
  71. */
  72. struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
  73. if (newsk != NULL) {
  74. struct dccp_request_sock *dreq = dccp_rsk(req);
  75. struct inet_connection_sock *newicsk = inet_csk(newsk);
  76. struct dccp_sock *newdp = dccp_sk(newsk);
  77. newdp->dccps_role = DCCP_ROLE_SERVER;
  78. newdp->dccps_hc_rx_ackvec = NULL;
  79. newdp->dccps_service_list = NULL;
  80. newdp->dccps_service = dreq->dreq_service;
  81. newdp->dccps_timestamp_echo = dreq->dreq_timestamp_echo;
  82. newdp->dccps_timestamp_time = dreq->dreq_timestamp_time;
  83. newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
  84. INIT_LIST_HEAD(&newdp->dccps_featneg);
  85. /*
  86. * Step 3: Process LISTEN state
  87. *
  88. * Choose S.ISS (initial seqno) or set from Init Cookies
  89. * Initialize S.GAR := S.ISS
  90. * Set S.ISR, S.GSR from packet (or Init Cookies)
  91. *
  92. * Setting AWL/AWH and SWL/SWH happens as part of the feature
  93. * activation below, as these windows all depend on the local
  94. * and remote Sequence Window feature values (7.5.2).
  95. */
  96. newdp->dccps_iss = dreq->dreq_iss;
  97. newdp->dccps_gss = dreq->dreq_gss;
  98. newdp->dccps_gar = newdp->dccps_iss;
  99. newdp->dccps_isr = dreq->dreq_isr;
  100. newdp->dccps_gsr = dreq->dreq_gsr;
  101. /*
  102. * Activate features: initialise CCIDs, sequence windows etc.
  103. */
  104. if (dccp_feat_activate_values(newsk, &dreq->dreq_featneg)) {
  105. /* It is still raw copy of parent, so invalidate
  106. * destructor and make plain sk_free() */
  107. newsk->sk_destruct = NULL;
  108. bh_unlock_sock(newsk);
  109. sk_free(newsk);
  110. return NULL;
  111. }
  112. dccp_init_xmit_timers(newsk);
  113. DCCP_INC_STATS_BH(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. {
  190. int ret = 0;
  191. const int state = child->sk_state;
  192. if (!sock_owned_by_user(child)) {
  193. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
  194. skb->len);
  195. /* Wakeup parent, send SIGIO */
  196. if (state == DCCP_RESPOND && child->sk_state != state)
  197. parent->sk_data_ready(parent);
  198. } else {
  199. /* Alas, it is possible again, because we do lookup
  200. * in main socket hash table and lock on listening
  201. * socket does not protect us more.
  202. */
  203. __sk_add_backlog(child, skb);
  204. }
  205. bh_unlock_sock(child);
  206. sock_put(child);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL_GPL(dccp_child_process);
  210. void dccp_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  211. struct request_sock *rsk)
  212. {
  213. DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
  214. }
  215. EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
  216. int dccp_reqsk_init(struct request_sock *req,
  217. struct dccp_sock const *dp, struct sk_buff const *skb)
  218. {
  219. struct dccp_request_sock *dreq = dccp_rsk(req);
  220. spin_lock_init(&dreq->dreq_lock);
  221. inet_rsk(req)->ir_rmt_port = dccp_hdr(skb)->dccph_sport;
  222. inet_rsk(req)->ir_num = ntohs(dccp_hdr(skb)->dccph_dport);
  223. inet_rsk(req)->acked = 0;
  224. dreq->dreq_timestamp_echo = 0;
  225. /* inherit feature negotiation options from listening socket */
  226. return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
  227. }
  228. EXPORT_SYMBOL_GPL(dccp_reqsk_init);