minisocks.c 7.5 KB

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