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>
26 struct inet_timewait_death_row dccp_death_row = {
27 .sysctl_max_tw_buckets = NR_FILE * 2,
28 .period = DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
29 .death_lock = SPIN_LOCK_UNLOCKED,
30 .hashinfo = &dccp_hashinfo,
31 .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
32 (unsigned long)&dccp_death_row),
33 .twkill_work = __WORK_INITIALIZER(dccp_death_row.twkill_work,
34 inet_twdr_twkill_work,
36 /* Short-time timewait calendar */
39 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
40 (unsigned long)&dccp_death_row),
43 void dccp_time_wait(struct sock *sk, int state, int timeo)
45 struct inet_timewait_sock *tw = NULL;
47 if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
48 tw = inet_twsk_alloc(sk, state);
51 const struct inet_connection_sock *icsk = inet_csk(sk);
52 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
54 /* Linkage updates. */
55 __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
57 /* Get the TIME_WAIT timeout firing. */
61 tw->tw_timeout = DCCP_TIMEWAIT_LEN;
62 if (state == DCCP_TIME_WAIT)
63 timeo = DCCP_TIMEWAIT_LEN;
65 inet_twsk_schedule(tw, &dccp_death_row, timeo,
69 /* Sorry, if we're out of memory, just CLOSE this
70 * socket up. We've got bigger problems than
71 * non-graceful socket closings.
73 LIMIT_NETDEBUG(KERN_INFO "DCCP: time wait bucket "
80 struct sock *dccp_create_openreq_child(struct sock *sk,
81 const struct request_sock *req,
82 const struct sk_buff *skb)
85 * Step 3: Process LISTEN state
87 * // Generate a new socket and switch to that socket
88 * Set S := new socket for this port pair
90 struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
93 const struct dccp_request_sock *dreq = dccp_rsk(req);
94 struct inet_connection_sock *newicsk = inet_csk(sk);
95 struct dccp_sock *newdp = dccp_sk(newsk);
97 newdp->dccps_role = DCCP_ROLE_SERVER;
98 newdp->dccps_hc_rx_ackvec = NULL;
99 newdp->dccps_service_list = NULL;
100 newdp->dccps_service = dreq->dreq_service;
101 newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
102 do_gettimeofday(&newdp->dccps_epoch);
104 if (newdp->dccps_options.dccpo_send_ack_vector) {
105 newdp->dccps_hc_rx_ackvec =
106 dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
109 * XXX: We're using the same CCIDs set on the parent,
110 * i.e. sk_clone copied the master sock and left the
111 * CCID pointers for this child, that is why we do the
114 if (unlikely(newdp->dccps_hc_rx_ackvec == NULL))
118 if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid,
120 ccid_hc_tx_init(newdp->dccps_hc_tx_ccid,
122 dccp_ackvec_free(newdp->dccps_hc_rx_ackvec);
123 ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
124 ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
126 /* It is still raw copy of parent, so invalidate
127 * destructor and make plain sk_free() */
128 newsk->sk_destruct = NULL;
133 __ccid_get(newdp->dccps_hc_rx_ccid);
134 __ccid_get(newdp->dccps_hc_tx_ccid);
137 * Step 3: Process LISTEN state
139 * Choose S.ISS (initial seqno) or set from Init Cookie
140 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
144 /* See dccp_v4_conn_request */
145 newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
147 newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
148 dccp_update_gsr(newsk, dreq->dreq_isr);
150 newdp->dccps_iss = dreq->dreq_iss;
151 dccp_update_gss(newsk, dreq->dreq_iss);
154 * SWL and AWL are initially adjusted so that they are not less than
155 * the initial Sequence Numbers received and sent, respectively:
156 * SWL := max(GSR + 1 - floor(W/4), ISR),
157 * AWL := max(GSS - W' + 1, ISS).
158 * These adjustments MUST be applied only at the beginning of the
161 dccp_set_seqno(&newdp->dccps_swl,
162 max48(newdp->dccps_swl, newdp->dccps_isr));
163 dccp_set_seqno(&newdp->dccps_awl,
164 max48(newdp->dccps_awl, newdp->dccps_iss));
166 dccp_init_xmit_timers(newsk);
168 DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
174 * Process an incoming packet for RESPOND sockets represented
175 * as an request_sock.
177 struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
178 struct request_sock *req,
179 struct request_sock **prev)
181 struct sock *child = NULL;
183 /* Check for retransmitted REQUEST */
184 if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
185 if (after48(DCCP_SKB_CB(skb)->dccpd_seq,
186 dccp_rsk(req)->dreq_isr)) {
187 struct dccp_request_sock *dreq = dccp_rsk(req);
189 dccp_pr_debug("Retransmitted REQUEST\n");
190 /* Send another RESPONSE packet */
191 dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
192 dccp_set_seqno(&dreq->dreq_isr,
193 DCCP_SKB_CB(skb)->dccpd_seq);
194 req->rsk_ops->rtx_syn_ack(sk, req, NULL);
196 /* Network Duplicate, discard packet */
200 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
202 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
203 dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
207 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
208 dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
211 DCCP_SKB_CB(skb)->dccpd_ack_seq,
213 dccp_rsk(req)->dreq_iss);
217 child = dccp_v4_request_recv_sock(sk, skb, req, NULL);
219 goto listen_overflow;
221 /* FIXME: deal with options */
223 inet_csk_reqsk_queue_unlink(sk, req, prev);
224 inet_csk_reqsk_queue_removed(sk, req);
225 inet_csk_reqsk_queue_add(sk, req, child);
229 dccp_pr_debug("listen_overflow!\n");
230 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
232 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
233 req->rsk_ops->send_reset(skb);
235 inet_csk_reqsk_queue_drop(sk, req, prev);
240 * Queue segment on the new socket if the new socket is active,
241 * otherwise we just shortcircuit this and continue with
244 int dccp_child_process(struct sock *parent, struct sock *child,
248 const int state = child->sk_state;
250 if (!sock_owned_by_user(child)) {
251 ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
254 /* Wakeup parent, send SIGIO */
255 if (state == DCCP_RESPOND && child->sk_state != state)
256 parent->sk_data_ready(parent, 0);
258 /* Alas, it is possible again, because we do lookup
259 * in main socket hash table and lock on listening
260 * socket does not protect us more.
262 sk_add_backlog(child, skb);
265 bh_unlock_sock(child);