minisocks.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/config.h>
  13. #include <linux/dccp.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 "ccid.h"
  20. #include "dccp.h"
  21. void dccp_time_wait(struct sock *sk, int state, int timeo)
  22. {
  23. /* FIXME: Implement */
  24. dccp_pr_debug("Want to help? Start here\n");
  25. dccp_set_state(sk, state);
  26. }
  27. /* This is for handling early-kills of TIME_WAIT sockets. */
  28. void dccp_tw_deschedule(struct inet_timewait_sock *tw)
  29. {
  30. dccp_pr_debug("Want to help? Start here\n");
  31. __inet_twsk_kill(tw, &dccp_hashinfo);
  32. }
  33. struct sock *dccp_create_openreq_child(struct sock *sk,
  34. const struct request_sock *req,
  35. const struct sk_buff *skb)
  36. {
  37. /*
  38. * Step 3: Process LISTEN state
  39. *
  40. * // Generate a new socket and switch to that socket
  41. * Set S := new socket for this port pair
  42. */
  43. struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
  44. if (newsk != NULL) {
  45. const struct dccp_request_sock *dreq = dccp_rsk(req);
  46. struct inet_connection_sock *newicsk = inet_csk(sk);
  47. struct dccp_sock *newdp = dccp_sk(newsk);
  48. newdp->dccps_hc_rx_ackpkts = NULL;
  49. newdp->dccps_role = DCCP_ROLE_SERVER;
  50. newicsk->icsk_rto = TCP_TIMEOUT_INIT;
  51. if (newdp->dccps_options.dccpo_send_ack_vector) {
  52. newdp->dccps_hc_rx_ackpkts = dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
  53. GFP_ATOMIC);
  54. /*
  55. * XXX: We're using the same CCIDs set on the parent, i.e. sk_clone
  56. * copied the master sock and left the CCID pointers for this child,
  57. * that is why we do the __ccid_get calls.
  58. */
  59. if (unlikely(newdp->dccps_hc_rx_ackpkts == NULL))
  60. goto out_free;
  61. }
  62. if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid, newsk) != 0 ||
  63. ccid_hc_tx_init(newdp->dccps_hc_tx_ccid, newsk) != 0)) {
  64. dccp_ackpkts_free(newdp->dccps_hc_rx_ackpkts);
  65. ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
  66. ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
  67. out_free:
  68. /* It is still raw copy of parent, so invalidate
  69. * destructor and make plain sk_free() */
  70. newsk->sk_destruct = NULL;
  71. sk_free(newsk);
  72. return NULL;
  73. }
  74. __ccid_get(newdp->dccps_hc_rx_ccid);
  75. __ccid_get(newdp->dccps_hc_tx_ccid);
  76. /*
  77. * Step 3: Process LISTEN state
  78. *
  79. * Choose S.ISS (initial seqno) or set from Init Cookie
  80. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  81. */
  82. /* See dccp_v4_conn_request */
  83. newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
  84. newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
  85. dccp_update_gsr(newsk, dreq->dreq_isr);
  86. newdp->dccps_iss = dreq->dreq_iss;
  87. dccp_update_gss(newsk, dreq->dreq_iss);
  88. dccp_init_xmit_timers(newsk);
  89. DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
  90. }
  91. return newsk;
  92. }
  93. /*
  94. * Process an incoming packet for RESPOND sockets represented
  95. * as an request_sock.
  96. */
  97. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  98. struct request_sock *req,
  99. struct request_sock **prev)
  100. {
  101. struct sock *child = NULL;
  102. /* Check for retransmitted REQUEST */
  103. if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
  104. if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dccp_rsk(req)->dreq_isr)) {
  105. struct dccp_request_sock *dreq = dccp_rsk(req);
  106. dccp_pr_debug("Retransmitted REQUEST\n");
  107. /* Send another RESPONSE packet */
  108. dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
  109. dccp_set_seqno(&dreq->dreq_isr, DCCP_SKB_CB(skb)->dccpd_seq);
  110. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  111. }
  112. /* Network Duplicate, discard packet */
  113. return NULL;
  114. }
  115. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  116. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
  117. dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
  118. goto drop;
  119. /* Invalid ACK */
  120. if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
  121. dccp_pr_debug("Invalid ACK number: ack_seq=%llu, dreq_iss=%llu\n",
  122. (unsigned long long)
  123. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  124. (unsigned long long)
  125. dccp_rsk(req)->dreq_iss);
  126. goto drop;
  127. }
  128. child = dccp_v4_request_recv_sock(sk, skb, req, NULL);
  129. if (child == NULL)
  130. goto listen_overflow;
  131. /* FIXME: deal with options */
  132. inet_csk_reqsk_queue_unlink(sk, req, prev);
  133. inet_csk_reqsk_queue_removed(sk, req);
  134. inet_csk_reqsk_queue_add(sk, req, child);
  135. out:
  136. return child;
  137. listen_overflow:
  138. dccp_pr_debug("listen_overflow!\n");
  139. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  140. drop:
  141. if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
  142. req->rsk_ops->send_reset(skb);
  143. inet_csk_reqsk_queue_drop(sk, req, prev);
  144. goto out;
  145. }
  146. /*
  147. * Queue segment on the new socket if the new socket is active,
  148. * otherwise we just shortcircuit this and continue with
  149. * the new socket.
  150. */
  151. int dccp_child_process(struct sock *parent, struct sock *child,
  152. struct sk_buff *skb)
  153. {
  154. int ret = 0;
  155. const int state = child->sk_state;
  156. if (!sock_owned_by_user(child)) {
  157. ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb), skb->len);
  158. /* Wakeup parent, send SIGIO */
  159. if (state == DCCP_RESPOND && child->sk_state != state)
  160. parent->sk_data_ready(parent, 0);
  161. } else {
  162. /* Alas, it is possible again, because we do lookup
  163. * in main socket hash table and lock on listening
  164. * socket does not protect us more.
  165. */
  166. sk_add_backlog(child, skb);
  167. }
  168. bh_unlock_sock(child);
  169. sock_put(child);
  170. return ret;
  171. }