4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
13 #include <linux/config.h>
14 #include <linux/dccp.h>
15 #include <linux/skbuff.h>
16 #include <linux/timer.h>
20 #include <net/inet_timewait_sock.h>
25 struct inet_timewait_death_row dccp_death_row = {
26 .sysctl_max_tw_buckets = NR_FILE * 2,
27 .period = DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
28 .death_lock = SPIN_LOCK_UNLOCKED,
29 .hashinfo = &dccp_hashinfo,
30 .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
31 (unsigned long)&dccp_death_row),
32 .twkill_work = __WORK_INITIALIZER(dccp_death_row.twkill_work,
33 inet_twdr_twkill_work,
35 /* Short-time timewait calendar */
38 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
39 (unsigned long)&dccp_death_row),
42 void dccp_time_wait(struct sock *sk, int state, int timeo)
44 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);
50 const struct inet_connection_sock *icsk = inet_csk(sk);
51 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
53 /* Linkage updates. */
54 __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
56 /* Get the TIME_WAIT timeout firing. */
60 tw->tw_timeout = DCCP_TIMEWAIT_LEN;
61 if (state == DCCP_TIME_WAIT)
62 timeo = DCCP_TIMEWAIT_LEN;
64 inet_twsk_schedule(tw, &dccp_death_row, timeo,
68 /* Sorry, if we're out of memory, just CLOSE this
69 * socket up. We've got bigger problems than
70 * non-graceful socket closings.
72 LIMIT_NETDEBUG(KERN_INFO "DCCP: time wait bucket "
79 struct sock *dccp_create_openreq_child(struct sock *sk,
80 const struct request_sock *req,
81 const struct sk_buff *skb)
84 * Step 3: Process LISTEN state
86 * // Generate a new socket and switch to that socket
87 * Set S := new socket for this port pair
89 struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
92 const struct dccp_request_sock *dreq = dccp_rsk(req);
93 struct inet_connection_sock *newicsk = inet_csk(sk);
94 struct dccp_sock *newdp = dccp_sk(newsk);
96 newdp->dccps_hc_rx_ackpkts = NULL;
97 newdp->dccps_role = DCCP_ROLE_SERVER;
98 newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
99 do_gettimeofday(&newdp->dccps_epoch);
101 if (newdp->dccps_options.dccpo_send_ack_vector) {
102 newdp->dccps_hc_rx_ackpkts =
103 dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
106 * XXX: We're using the same CCIDs set on the parent,
107 * i.e. sk_clone copied the master sock and left the
108 * CCID pointers for this child, that is why we do the
111 if (unlikely(newdp->dccps_hc_rx_ackpkts == NULL))
115 if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid,
117 ccid_hc_tx_init(newdp->dccps_hc_tx_ccid,
119 dccp_ackpkts_free(newdp->dccps_hc_rx_ackpkts);
120 ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
121 ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
123 /* It is still raw copy of parent, so invalidate
124 * destructor and make plain sk_free() */
125 newsk->sk_destruct = NULL;
130 __ccid_get(newdp->dccps_hc_rx_ccid);
131 __ccid_get(newdp->dccps_hc_tx_ccid);
134 * Step 3: Process LISTEN state
136 * Choose S.ISS (initial seqno) or set from Init Cookie
137 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
141 /* See dccp_v4_conn_request */
142 newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
144 newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
145 dccp_update_gsr(newsk, dreq->dreq_isr);
147 newdp->dccps_iss = dreq->dreq_iss;
148 dccp_update_gss(newsk, dreq->dreq_iss);
151 * SWL and AWL are initially adjusted so that they are not less than
152 * the initial Sequence Numbers received and sent, respectively:
153 * SWL := max(GSR + 1 - floor(W/4), ISR),
154 * AWL := max(GSS - W' + 1, ISS).
155 * These adjustments MUST be applied only at the beginning of the
158 dccp_set_seqno(&newdp->dccps_swl,
159 max48(newdp->dccps_swl, newdp->dccps_isr));
160 dccp_set_seqno(&newdp->dccps_awl,
161 max48(newdp->dccps_awl, newdp->dccps_iss));
163 dccp_init_xmit_timers(newsk);
165 DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
171 * Process an incoming packet for RESPOND sockets represented
172 * as an request_sock.
174 struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
175 struct request_sock *req,
176 struct request_sock **prev)
178 struct sock *child = NULL;
180 /* Check for retransmitted REQUEST */
181 if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
182 if (after48(DCCP_SKB_CB(skb)->dccpd_seq,
183 dccp_rsk(req)->dreq_isr)) {
184 struct dccp_request_sock *dreq = dccp_rsk(req);
186 dccp_pr_debug("Retransmitted REQUEST\n");
187 /* Send another RESPONSE packet */
188 dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
189 dccp_set_seqno(&dreq->dreq_isr,
190 DCCP_SKB_CB(skb)->dccpd_seq);
191 req->rsk_ops->rtx_syn_ack(sk, req, NULL);
193 /* Network Duplicate, discard packet */
197 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
199 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
200 dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
204 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
205 dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
208 DCCP_SKB_CB(skb)->dccpd_ack_seq,
210 dccp_rsk(req)->dreq_iss);
214 child = dccp_v4_request_recv_sock(sk, skb, req, NULL);
216 goto listen_overflow;
218 /* FIXME: deal with options */
220 inet_csk_reqsk_queue_unlink(sk, req, prev);
221 inet_csk_reqsk_queue_removed(sk, req);
222 inet_csk_reqsk_queue_add(sk, req, child);
226 dccp_pr_debug("listen_overflow!\n");
227 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
229 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
230 req->rsk_ops->send_reset(skb);
232 inet_csk_reqsk_queue_drop(sk, req, prev);
237 * Queue segment on the new socket if the new socket is active,
238 * otherwise we just shortcircuit this and continue with
241 int dccp_child_process(struct sock *parent, struct sock *child,
245 const int state = child->sk_state;
247 if (!sock_owned_by_user(child)) {
248 ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
251 /* Wakeup parent, send SIGIO */
252 if (state == DCCP_RESPOND && child->sk_state != state)
253 parent->sk_data_ready(parent, 0);
255 /* Alas, it is possible again, because we do lookup
256 * in main socket hash table and lock on listening
257 * socket does not protect us more.
259 sk_add_backlog(child, skb);
262 bh_unlock_sock(child);