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 EXPORT_SYMBOL_GPL(dccp_death_row);
45 void dccp_time_wait(struct sock *sk, int state, int timeo)
47 struct inet_timewait_sock *tw = NULL;
49 if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
50 tw = inet_twsk_alloc(sk, state);
53 const struct inet_connection_sock *icsk = inet_csk(sk);
54 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
56 /* Linkage updates. */
57 __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
59 /* Get the TIME_WAIT timeout firing. */
63 tw->tw_timeout = DCCP_TIMEWAIT_LEN;
64 if (state == DCCP_TIME_WAIT)
65 timeo = DCCP_TIMEWAIT_LEN;
67 inet_twsk_schedule(tw, &dccp_death_row, timeo,
71 /* Sorry, if we're out of memory, just CLOSE this
72 * socket up. We've got bigger problems than
73 * non-graceful socket closings.
75 LIMIT_NETDEBUG(KERN_INFO "DCCP: time wait bucket "
82 struct sock *dccp_create_openreq_child(struct sock *sk,
83 const struct request_sock *req,
84 const struct sk_buff *skb)
87 * Step 3: Process LISTEN state
89 * // Generate a new socket and switch to that socket
90 * Set S := new socket for this port pair
92 struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
95 const struct dccp_request_sock *dreq = dccp_rsk(req);
96 struct inet_connection_sock *newicsk = inet_csk(sk);
97 struct dccp_sock *newdp = dccp_sk(newsk);
99 newdp->dccps_role = DCCP_ROLE_SERVER;
100 newdp->dccps_hc_rx_ackvec = NULL;
101 newdp->dccps_service_list = NULL;
102 newdp->dccps_service = dreq->dreq_service;
103 newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
104 do_gettimeofday(&newdp->dccps_epoch);
106 if (newdp->dccps_options.dccpo_send_ack_vector) {
107 newdp->dccps_hc_rx_ackvec =
108 dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
111 * XXX: We're using the same CCIDs set on the parent,
112 * i.e. sk_clone copied the master sock and left the
113 * CCID pointers for this child, that is why we do the
116 if (unlikely(newdp->dccps_hc_rx_ackvec == NULL))
120 if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid,
122 ccid_hc_tx_init(newdp->dccps_hc_tx_ccid,
124 dccp_ackvec_free(newdp->dccps_hc_rx_ackvec);
125 ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
126 ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
128 /* It is still raw copy of parent, so invalidate
129 * destructor and make plain sk_free() */
130 newsk->sk_destruct = NULL;
135 __ccid_get(newdp->dccps_hc_rx_ccid);
136 __ccid_get(newdp->dccps_hc_tx_ccid);
139 * Step 3: Process LISTEN state
141 * Choose S.ISS (initial seqno) or set from Init Cookie
142 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
146 /* See dccp_v4_conn_request */
147 newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
149 newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
150 dccp_update_gsr(newsk, dreq->dreq_isr);
152 newdp->dccps_iss = dreq->dreq_iss;
153 dccp_update_gss(newsk, dreq->dreq_iss);
156 * SWL and AWL are initially adjusted so that they are not less than
157 * the initial Sequence Numbers received and sent, respectively:
158 * SWL := max(GSR + 1 - floor(W/4), ISR),
159 * AWL := max(GSS - W' + 1, ISS).
160 * These adjustments MUST be applied only at the beginning of the
163 dccp_set_seqno(&newdp->dccps_swl,
164 max48(newdp->dccps_swl, newdp->dccps_isr));
165 dccp_set_seqno(&newdp->dccps_awl,
166 max48(newdp->dccps_awl, newdp->dccps_iss));
168 dccp_init_xmit_timers(newsk);
170 DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
175 EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
178 * Process an incoming packet for RESPOND sockets represented
179 * as an request_sock.
181 struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
182 struct request_sock *req,
183 struct request_sock **prev)
185 struct sock *child = NULL;
187 /* Check for retransmitted REQUEST */
188 if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
189 if (after48(DCCP_SKB_CB(skb)->dccpd_seq,
190 dccp_rsk(req)->dreq_isr)) {
191 struct dccp_request_sock *dreq = dccp_rsk(req);
193 dccp_pr_debug("Retransmitted REQUEST\n");
194 /* Send another RESPONSE packet */
195 dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
196 dccp_set_seqno(&dreq->dreq_isr,
197 DCCP_SKB_CB(skb)->dccpd_seq);
198 req->rsk_ops->rtx_syn_ack(sk, req, NULL);
200 /* Network Duplicate, discard packet */
204 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
206 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
207 dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
211 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
212 dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
215 DCCP_SKB_CB(skb)->dccpd_ack_seq,
217 dccp_rsk(req)->dreq_iss);
221 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
223 goto listen_overflow;
225 /* FIXME: deal with options */
227 inet_csk_reqsk_queue_unlink(sk, req, prev);
228 inet_csk_reqsk_queue_removed(sk, req);
229 inet_csk_reqsk_queue_add(sk, req, child);
233 dccp_pr_debug("listen_overflow!\n");
234 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
236 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
237 req->rsk_ops->send_reset(skb);
239 inet_csk_reqsk_queue_drop(sk, req, prev);
243 EXPORT_SYMBOL_GPL(dccp_check_req);
246 * Queue segment on the new socket if the new socket is active,
247 * otherwise we just shortcircuit this and continue with
250 int dccp_child_process(struct sock *parent, struct sock *child,
254 const int state = child->sk_state;
256 if (!sock_owned_by_user(child)) {
257 ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
260 /* Wakeup parent, send SIGIO */
261 if (state == DCCP_RESPOND && child->sk_state != state)
262 parent->sk_data_ready(parent, 0);
264 /* Alas, it is possible again, because we do lookup
265 * in main socket hash table and lock on listening
266 * socket does not protect us more.
268 sk_add_backlog(child, skb);
271 bh_unlock_sock(child);
276 EXPORT_SYMBOL_GPL(dccp_child_process);