minisocks.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/kernel.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/timer.h>
  16. #include <net/sock.h>
  17. #include <net/xfrm.h>
  18. #include <net/inet_timewait_sock.h>
  19. #include "ackvec.h"
  20. #include "ccid.h"
  21. #include "dccp.h"
  22. #include "feat.h"
  23. struct inet_timewait_death_row dccp_death_row = {
  24. .sysctl_max_tw_buckets = NR_FILE * 2,
  25. .period = DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
  26. .death_lock = __SPIN_LOCK_UNLOCKED(dccp_death_row.death_lock),
  27. .hashinfo = &dccp_hashinfo,
  28. .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
  29. (unsigned long)&dccp_death_row),
  30. .twkill_work = __WORK_INITIALIZER(dccp_death_row.twkill_work,
  31. inet_twdr_twkill_work),
  32. /* Short-time timewait calendar */
  33. .twcal_hand = -1,
  34. .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
  35. (unsigned long)&dccp_death_row),
  36. };
  37. EXPORT_SYMBOL_GPL(dccp_death_row);
  38. void dccp_minisock_init(struct dccp_minisock *dmsk)
  39. {
  40. dmsk->dccpms_sequence_window = sysctl_dccp_feat_sequence_window;
  41. dmsk->dccpms_send_ack_vector = sysctl_dccp_feat_send_ack_vector;
  42. }
  43. void dccp_time_wait(struct sock *sk, int state, int timeo)
  44. {
  45. struct inet_timewait_sock *tw = NULL;
  46. if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
  47. tw = inet_twsk_alloc(sk, state);
  48. if (tw != NULL) {
  49. const struct inet_connection_sock *icsk = inet_csk(sk);
  50. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  51. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  52. if (tw->tw_family == PF_INET6) {
  53. const struct ipv6_pinfo *np = inet6_sk(sk);
  54. struct inet6_timewait_sock *tw6;
  55. tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
  56. tw6 = inet6_twsk((struct sock *)tw);
  57. ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
  58. ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
  59. tw->tw_ipv6only = np->ipv6only;
  60. }
  61. #endif
  62. /* Linkage updates. */
  63. __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
  64. /* Get the TIME_WAIT timeout firing. */
  65. if (timeo < rto)
  66. timeo = rto;
  67. tw->tw_timeout = DCCP_TIMEWAIT_LEN;
  68. if (state == DCCP_TIME_WAIT)
  69. timeo = DCCP_TIMEWAIT_LEN;
  70. inet_twsk_schedule(tw, &dccp_death_row, timeo,
  71. DCCP_TIMEWAIT_LEN);
  72. inet_twsk_put(tw);
  73. } else {
  74. /* Sorry, if we're out of memory, just CLOSE this
  75. * socket up. We've got bigger problems than
  76. * non-graceful socket closings.
  77. */
  78. DCCP_WARN("time wait bucket table overflow\n");
  79. }
  80. dccp_done(sk);
  81. }
  82. struct sock *dccp_create_openreq_child(struct sock *sk,
  83. const struct request_sock *req,
  84. const struct sk_buff *skb)
  85. {
  86. /*
  87. * Step 3: Process LISTEN state
  88. *
  89. * (* Generate a new socket and switch to that socket *)
  90. * Set S := new socket for this port pair
  91. */
  92. struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
  93. if (newsk != NULL) {
  94. struct dccp_request_sock *dreq = dccp_rsk(req);
  95. struct inet_connection_sock *newicsk = inet_csk(newsk);
  96. struct dccp_sock *newdp = dccp_sk(newsk);
  97. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  98. newdp->dccps_role = DCCP_ROLE_SERVER;
  99. newdp->dccps_hc_rx_ackvec = NULL;
  100. newdp->dccps_service_list = NULL;
  101. newdp->dccps_service = dreq->dreq_service;
  102. newdp->dccps_timestamp_echo = dreq->dreq_timestamp_echo;
  103. newdp->dccps_timestamp_time = dreq->dreq_timestamp_time;
  104. newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
  105. INIT_LIST_HEAD(&newdp->dccps_featneg);
  106. /*
  107. * Step 3: Process LISTEN state
  108. *
  109. * Choose S.ISS (initial seqno) or set from Init Cookies
  110. * Initialize S.GAR := S.ISS
  111. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
  112. */
  113. /* See dccp_v4_conn_request */
  114. newdmsk->dccpms_sequence_window = req->rcv_wnd;
  115. newdp->dccps_gar = newdp->dccps_iss = dreq->dreq_iss;
  116. dccp_update_gss(newsk, dreq->dreq_iss);
  117. newdp->dccps_isr = dreq->dreq_isr;
  118. dccp_update_gsr(newsk, dreq->dreq_isr);
  119. /*
  120. * SWL and AWL are initially adjusted so that they are not less than
  121. * the initial Sequence Numbers received and sent, respectively:
  122. * SWL := max(GSR + 1 - floor(W/4), ISR),
  123. * AWL := max(GSS - W' + 1, ISS).
  124. * These adjustments MUST be applied only at the beginning of the
  125. * connection.
  126. */
  127. dccp_set_seqno(&newdp->dccps_swl,
  128. max48(newdp->dccps_swl, newdp->dccps_isr));
  129. dccp_set_seqno(&newdp->dccps_awl,
  130. max48(newdp->dccps_awl, newdp->dccps_iss));
  131. /*
  132. * Activate features after initialising the sequence numbers,
  133. * since CCID initialisation may depend on GSS, ISR, ISS etc.
  134. */
  135. if (dccp_feat_activate_values(newsk, &dreq->dreq_featneg)) {
  136. /* It is still raw copy of parent, so invalidate
  137. * destructor and make plain sk_free() */
  138. newsk->sk_destruct = NULL;
  139. sk_free(newsk);
  140. return NULL;
  141. }
  142. dccp_init_xmit_timers(newsk);
  143. DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
  144. }
  145. return newsk;
  146. }
  147. EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
  148. /*
  149. * Process an incoming packet for RESPOND sockets represented
  150. * as an request_sock.
  151. */
  152. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  153. struct request_sock *req,
  154. struct request_sock **prev)
  155. {
  156. struct sock *child = NULL;
  157. struct dccp_request_sock *dreq = dccp_rsk(req);
  158. /* Check for retransmitted REQUEST */
  159. if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
  160. if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dreq->dreq_isr)) {
  161. dccp_pr_debug("Retransmitted REQUEST\n");
  162. dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq;
  163. /*
  164. * Send another RESPONSE packet
  165. * To protect against Request floods, increment retrans
  166. * counter (backoff, monitored by dccp_response_timer).
  167. */
  168. req->retrans++;
  169. req->rsk_ops->rtx_syn_ack(sk, req);
  170. }
  171. /* Network Duplicate, discard packet */
  172. return NULL;
  173. }
  174. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  175. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
  176. dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
  177. goto drop;
  178. /* Invalid ACK */
  179. if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dreq->dreq_iss) {
  180. dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
  181. "dreq_iss=%llu\n",
  182. (unsigned long long)
  183. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  184. (unsigned long long) dreq->dreq_iss);
  185. goto drop;
  186. }
  187. if (dccp_parse_options(sk, dreq, skb))
  188. goto drop;
  189. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
  190. if (child == NULL)
  191. goto listen_overflow;
  192. inet_csk_reqsk_queue_unlink(sk, req, prev);
  193. inet_csk_reqsk_queue_removed(sk, req);
  194. inet_csk_reqsk_queue_add(sk, req, child);
  195. out:
  196. return child;
  197. listen_overflow:
  198. dccp_pr_debug("listen_overflow!\n");
  199. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  200. drop:
  201. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
  202. req->rsk_ops->send_reset(sk, skb);
  203. inet_csk_reqsk_queue_drop(sk, req, prev);
  204. goto out;
  205. }
  206. EXPORT_SYMBOL_GPL(dccp_check_req);
  207. /*
  208. * Queue segment on the new socket if the new socket is active,
  209. * otherwise we just shortcircuit this and continue with
  210. * the new socket.
  211. */
  212. int dccp_child_process(struct sock *parent, struct sock *child,
  213. struct sk_buff *skb)
  214. {
  215. int ret = 0;
  216. const int state = child->sk_state;
  217. if (!sock_owned_by_user(child)) {
  218. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
  219. skb->len);
  220. /* Wakeup parent, send SIGIO */
  221. if (state == DCCP_RESPOND && child->sk_state != state)
  222. parent->sk_data_ready(parent, 0);
  223. } else {
  224. /* Alas, it is possible again, because we do lookup
  225. * in main socket hash table and lock on listening
  226. * socket does not protect us more.
  227. */
  228. sk_add_backlog(child, skb);
  229. }
  230. bh_unlock_sock(child);
  231. sock_put(child);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(dccp_child_process);
  235. void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  236. struct request_sock *rsk)
  237. {
  238. DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
  239. }
  240. EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
  241. int dccp_reqsk_init(struct request_sock *req,
  242. struct dccp_sock const *dp, struct sk_buff const *skb)
  243. {
  244. struct dccp_request_sock *dreq = dccp_rsk(req);
  245. inet_rsk(req)->rmt_port = dccp_hdr(skb)->dccph_sport;
  246. inet_rsk(req)->loc_port = dccp_hdr(skb)->dccph_dport;
  247. inet_rsk(req)->acked = 0;
  248. req->rcv_wnd = sysctl_dccp_feat_sequence_window;
  249. dreq->dreq_timestamp_echo = 0;
  250. /* inherit feature negotiation options from listening socket */
  251. return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
  252. }
  253. EXPORT_SYMBOL_GPL(dccp_reqsk_init);