minisocks.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /* Linkage updates. */
  44. __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
  45. /* Get the TIME_WAIT timeout firing. */
  46. if (timeo < rto)
  47. timeo = rto;
  48. tw->tw_timeout = DCCP_TIMEWAIT_LEN;
  49. if (state == DCCP_TIME_WAIT)
  50. timeo = DCCP_TIMEWAIT_LEN;
  51. inet_twsk_schedule(tw, timeo);
  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(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. sk_free(newsk);
  109. return NULL;
  110. }
  111. dccp_init_xmit_timers(newsk);
  112. DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
  113. }
  114. return newsk;
  115. }
  116. EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
  117. /*
  118. * Process an incoming packet for RESPOND sockets represented
  119. * as an request_sock.
  120. */
  121. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  122. struct request_sock *req)
  123. {
  124. struct sock *child = NULL;
  125. struct dccp_request_sock *dreq = dccp_rsk(req);
  126. /* Check for retransmitted REQUEST */
  127. if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
  128. if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dreq->dreq_gsr)) {
  129. dccp_pr_debug("Retransmitted REQUEST\n");
  130. dreq->dreq_gsr = DCCP_SKB_CB(skb)->dccpd_seq;
  131. /*
  132. * Send another RESPONSE packet
  133. * To protect against Request floods, increment retrans
  134. * counter (backoff, monitored by dccp_response_timer).
  135. */
  136. inet_rtx_syn_ack(sk, req);
  137. }
  138. /* Network Duplicate, discard packet */
  139. return NULL;
  140. }
  141. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  142. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
  143. dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
  144. goto drop;
  145. /* Invalid ACK */
  146. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  147. dreq->dreq_iss, dreq->dreq_gss)) {
  148. dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
  149. "dreq_iss=%llu, dreq_gss=%llu\n",
  150. (unsigned long long)
  151. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  152. (unsigned long long) dreq->dreq_iss,
  153. (unsigned long long) dreq->dreq_gss);
  154. goto drop;
  155. }
  156. if (dccp_parse_options(sk, dreq, skb))
  157. goto drop;
  158. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
  159. if (child == NULL)
  160. goto listen_overflow;
  161. inet_csk_reqsk_queue_drop(sk, req);
  162. inet_csk_reqsk_queue_add(sk, req, child);
  163. out:
  164. return child;
  165. listen_overflow:
  166. dccp_pr_debug("listen_overflow!\n");
  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);
  171. inet_csk_reqsk_queue_drop(sk, req);
  172. goto out;
  173. }
  174. EXPORT_SYMBOL_GPL(dccp_check_req);
  175. /*
  176. * Queue segment on the new socket if the new socket is active,
  177. * otherwise we just shortcircuit this and continue with
  178. * the new socket.
  179. */
  180. int dccp_child_process(struct sock *parent, struct sock *child,
  181. struct sk_buff *skb)
  182. {
  183. int ret = 0;
  184. const int state = child->sk_state;
  185. if (!sock_owned_by_user(child)) {
  186. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
  187. skb->len);
  188. /* Wakeup parent, send SIGIO */
  189. if (state == DCCP_RESPOND && child->sk_state != state)
  190. parent->sk_data_ready(parent);
  191. } else {
  192. /* Alas, it is possible again, because we do lookup
  193. * in main socket hash table and lock on listening
  194. * socket does not protect us more.
  195. */
  196. __sk_add_backlog(child, skb);
  197. }
  198. bh_unlock_sock(child);
  199. sock_put(child);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_GPL(dccp_child_process);
  203. void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  204. struct request_sock *rsk)
  205. {
  206. DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
  207. }
  208. EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
  209. int dccp_reqsk_init(struct request_sock *req,
  210. struct dccp_sock const *dp, struct sk_buff const *skb)
  211. {
  212. struct dccp_request_sock *dreq = dccp_rsk(req);
  213. inet_rsk(req)->ir_rmt_port = dccp_hdr(skb)->dccph_sport;
  214. inet_rsk(req)->ir_num = ntohs(dccp_hdr(skb)->dccph_dport);
  215. inet_rsk(req)->acked = 0;
  216. dreq->dreq_timestamp_echo = 0;
  217. /* inherit feature negotiation options from listening socket */
  218. return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
  219. }
  220. EXPORT_SYMBOL_GPL(dccp_reqsk_init);