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