PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/net/dccp/minisocks.c

http://github.com/torvalds/linux
C | 269 lines | 160 code | 42 blank | 67 comment | 28 complexity | 56e3accf9964099349595d28350e1e63 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/dccp/minisocks.c
  4. *
  5. * An implementation of the DCCP protocol
  6. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. */
  8. #include <linux/dccp.h>
  9. #include <linux/gfp.h>
  10. #include <linux/kernel.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/timer.h>
  13. #include <net/sock.h>
  14. #include <net/xfrm.h>
  15. #include <net/inet_timewait_sock.h>
  16. #include "ackvec.h"
  17. #include "ccid.h"
  18. #include "dccp.h"
  19. #include "feat.h"
  20. struct inet_timewait_death_row dccp_death_row = {
  21. .sysctl_max_tw_buckets = NR_FILE * 2,
  22. .hashinfo = &dccp_hashinfo,
  23. };
  24. EXPORT_SYMBOL_GPL(dccp_death_row);
  25. void dccp_time_wait(struct sock *sk, int state, int timeo)
  26. {
  27. struct inet_timewait_sock *tw;
  28. tw = inet_twsk_alloc(sk, &dccp_death_row, state);
  29. if (tw != NULL) {
  30. const struct inet_connection_sock *icsk = inet_csk(sk);
  31. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. if (tw->tw_family == PF_INET6) {
  34. tw->tw_v6_daddr = sk->sk_v6_daddr;
  35. tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  36. tw->tw_ipv6only = sk->sk_ipv6only;
  37. }
  38. #endif
  39. /* Get the TIME_WAIT timeout firing. */
  40. if (timeo < rto)
  41. timeo = rto;
  42. if (state == DCCP_TIME_WAIT)
  43. timeo = DCCP_TIMEWAIT_LEN;
  44. /* tw_timer is pinned, so we need to make sure BH are disabled
  45. * in following section, otherwise timer handler could run before
  46. * we complete the initialization.
  47. */
  48. local_bh_disable();
  49. inet_twsk_schedule(tw, timeo);
  50. /* Linkage updates.
  51. * Note that access to tw after this point is illegal.
  52. */
  53. inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
  54. local_bh_enable();
  55. } else {
  56. /* Sorry, if we're out of memory, just CLOSE this
  57. * socket up. We've got bigger problems than
  58. * non-graceful socket closings.
  59. */
  60. DCCP_WARN("time wait bucket table overflow\n");
  61. }
  62. dccp_done(sk);
  63. }
  64. struct sock *dccp_create_openreq_child(const struct sock *sk,
  65. const struct request_sock *req,
  66. const struct sk_buff *skb)
  67. {
  68. /*
  69. * Step 3: Process LISTEN state
  70. *
  71. * (* Generate a new socket and switch to that socket *)
  72. * Set S := new socket for this port pair
  73. */
  74. struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
  75. if (newsk != NULL) {
  76. struct dccp_request_sock *dreq = dccp_rsk(req);
  77. struct inet_connection_sock *newicsk = inet_csk(newsk);
  78. struct dccp_sock *newdp = dccp_sk(newsk);
  79. newdp->dccps_role = DCCP_ROLE_SERVER;
  80. newdp->dccps_hc_rx_ackvec = NULL;
  81. newdp->dccps_service_list = NULL;
  82. newdp->dccps_service = dreq->dreq_service;
  83. newdp->dccps_timestamp_echo = dreq->dreq_timestamp_echo;
  84. newdp->dccps_timestamp_time = dreq->dreq_timestamp_time;
  85. newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
  86. INIT_LIST_HEAD(&newdp->dccps_featneg);
  87. /*
  88. * Step 3: Process LISTEN state
  89. *
  90. * Choose S.ISS (initial seqno) or set from Init Cookies
  91. * Initialize S.GAR := S.ISS
  92. * Set S.ISR, S.GSR from packet (or Init Cookies)
  93. *
  94. * Setting AWL/AWH and SWL/SWH happens as part of the feature
  95. * activation below, as these windows all depend on the local
  96. * and remote Sequence Window feature values (7.5.2).
  97. */
  98. newdp->dccps_iss = dreq->dreq_iss;
  99. newdp->dccps_gss = dreq->dreq_gss;
  100. newdp->dccps_gar = newdp->dccps_iss;
  101. newdp->dccps_isr = dreq->dreq_isr;
  102. newdp->dccps_gsr = dreq->dreq_gsr;
  103. /*
  104. * Activate features: initialise CCIDs, sequence windows etc.
  105. */
  106. if (dccp_feat_activate_values(newsk, &dreq->dreq_featneg)) {
  107. sk_free_unlock_clone(newsk);
  108. return NULL;
  109. }
  110. dccp_init_xmit_timers(newsk);
  111. __DCCP_INC_STATS(DCCP_MIB_PASSIVEOPENS);
  112. }
  113. return newsk;
  114. }
  115. EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
  116. /*
  117. * Process an incoming packet for RESPOND sockets represented
  118. * as an request_sock.
  119. */
  120. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  121. struct request_sock *req)
  122. {
  123. struct sock *child = NULL;
  124. struct dccp_request_sock *dreq = dccp_rsk(req);
  125. bool own_req;
  126. /* TCP/DCCP listeners became lockless.
  127. * DCCP stores complex state in its request_sock, so we need
  128. * a protection for them, now this code runs without being protected
  129. * by the parent (listener) lock.
  130. */
  131. spin_lock_bh(&dreq->dreq_lock);
  132. /* Check for retransmitted REQUEST */
  133. if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
  134. if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dreq->dreq_gsr)) {
  135. dccp_pr_debug("Retransmitted REQUEST\n");
  136. dreq->dreq_gsr = DCCP_SKB_CB(skb)->dccpd_seq;
  137. /*
  138. * Send another RESPONSE packet
  139. * To protect against Request floods, increment retrans
  140. * counter (backoff, monitored by dccp_response_timer).
  141. */
  142. inet_rtx_syn_ack(sk, req);
  143. }
  144. /* Network Duplicate, discard packet */
  145. goto out;
  146. }
  147. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  148. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
  149. dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
  150. goto drop;
  151. /* Invalid ACK */
  152. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  153. dreq->dreq_iss, dreq->dreq_gss)) {
  154. dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
  155. "dreq_iss=%llu, dreq_gss=%llu\n",
  156. (unsigned long long)
  157. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  158. (unsigned long long) dreq->dreq_iss,
  159. (unsigned long long) dreq->dreq_gss);
  160. goto drop;
  161. }
  162. if (dccp_parse_options(sk, dreq, skb))
  163. goto drop;
  164. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
  165. req, &own_req);
  166. if (child) {
  167. child = inet_csk_complete_hashdance(sk, child, req, own_req);
  168. goto out;
  169. }
  170. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  171. drop:
  172. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
  173. req->rsk_ops->send_reset(sk, skb);
  174. inet_csk_reqsk_queue_drop(sk, req);
  175. out:
  176. spin_unlock_bh(&dreq->dreq_lock);
  177. return child;
  178. }
  179. EXPORT_SYMBOL_GPL(dccp_check_req);
  180. /*
  181. * Queue segment on the new socket if the new socket is active,
  182. * otherwise we just shortcircuit this and continue with
  183. * the new socket.
  184. */
  185. int dccp_child_process(struct sock *parent, struct sock *child,
  186. struct sk_buff *skb)
  187. __releases(child)
  188. {
  189. int ret = 0;
  190. const int state = child->sk_state;
  191. if (!sock_owned_by_user(child)) {
  192. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
  193. skb->len);
  194. /* Wakeup parent, send SIGIO */
  195. if (state == DCCP_RESPOND && child->sk_state != state)
  196. parent->sk_data_ready(parent);
  197. } else {
  198. /* Alas, it is possible again, because we do lookup
  199. * in main socket hash table and lock on listening
  200. * socket does not protect us more.
  201. */
  202. __sk_add_backlog(child, skb);
  203. }
  204. bh_unlock_sock(child);
  205. sock_put(child);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(dccp_child_process);
  209. void dccp_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  210. struct request_sock *rsk)
  211. {
  212. DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
  213. }
  214. EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
  215. int dccp_reqsk_init(struct request_sock *req,
  216. struct dccp_sock const *dp, struct sk_buff const *skb)
  217. {
  218. struct dccp_request_sock *dreq = dccp_rsk(req);
  219. spin_lock_init(&dreq->dreq_lock);
  220. inet_rsk(req)->ir_rmt_port = dccp_hdr(skb)->dccph_sport;
  221. inet_rsk(req)->ir_num = ntohs(dccp_hdr(skb)->dccph_dport);
  222. inet_rsk(req)->acked = 0;
  223. dreq->dreq_timestamp_echo = 0;
  224. /* inherit feature negotiation options from listening socket */
  225. return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
  226. }
  227. EXPORT_SYMBOL_GPL(dccp_reqsk_init);