blob: 933e10db1789e9a0db1682eff3073eb269144c8b [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
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
13#include <linux/config.h>
14#include <linux/dccp.h>
15#include <linux/skbuff.h>
16#include <linux/timer.h>
17
18#include <net/sock.h>
19#include <net/xfrm.h>
20#include <net/inet_timewait_sock.h>
21
22#include "ccid.h"
23#include "dccp.h"
24
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -070025struct 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,
34 &dccp_death_row),
35/* Short-time timewait calendar */
36
37 .twcal_hand = -1,
38 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
39 (unsigned long)&dccp_death_row),
40};
41
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070042void dccp_time_wait(struct sock *sk, int state, int timeo)
43{
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -070044 struct inet_timewait_sock *tw = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070045
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -070046 if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
47 tw = inet_twsk_alloc(sk, state);
48
49 if (tw != NULL) {
50 const struct inet_connection_sock *icsk = inet_csk(sk);
51 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
52
53 /* Linkage updates. */
54 __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
55
56 /* Get the TIME_WAIT timeout firing. */
57 if (timeo < rto)
58 timeo = rto;
59
60 tw->tw_timeout = DCCP_TIMEWAIT_LEN;
61 if (state == DCCP_TIME_WAIT)
62 timeo = DCCP_TIMEWAIT_LEN;
63
64 inet_twsk_schedule(tw, &dccp_death_row, timeo,
65 DCCP_TIMEWAIT_LEN);
66 inet_twsk_put(tw);
67 } else {
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.
71 */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030072 LIMIT_NETDEBUG(KERN_INFO "DCCP: time wait bucket "
73 "table overflow\n");
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -070074 }
75
76 dccp_done(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070077}
78
79struct sock *dccp_create_openreq_child(struct sock *sk,
80 const struct request_sock *req,
81 const struct sk_buff *skb)
82{
83 /*
84 * Step 3: Process LISTEN state
85 *
86 * // Generate a new socket and switch to that socket
87 * Set S := new socket for this port pair
88 */
89 struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
90
91 if (newsk != NULL) {
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);
95
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -070096 newdp->dccps_role = DCCP_ROLE_SERVER;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070097 newdp->dccps_hc_rx_ackpkts = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -070098 newdp->dccps_service_list = NULL;
99 newdp->dccps_service = dreq->dreq_service;
100 newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300101 do_gettimeofday(&newdp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700102
103 if (newdp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300104 newdp->dccps_hc_rx_ackpkts =
105 dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
106 GFP_ATOMIC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700107 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300108 * XXX: We're using the same CCIDs set on the parent,
109 * i.e. sk_clone copied the master sock and left the
110 * CCID pointers for this child, that is why we do the
111 * __ccid_get calls.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700112 */
113 if (unlikely(newdp->dccps_hc_rx_ackpkts == NULL))
114 goto out_free;
115 }
116
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300117 if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid,
118 newsk) != 0 ||
119 ccid_hc_tx_init(newdp->dccps_hc_tx_ccid,
120 newsk) != 0)) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700121 dccp_ackpkts_free(newdp->dccps_hc_rx_ackpkts);
122 ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
123 ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
124out_free:
125 /* It is still raw copy of parent, so invalidate
126 * destructor and make plain sk_free() */
127 newsk->sk_destruct = NULL;
128 sk_free(newsk);
129 return NULL;
130 }
131
132 __ccid_get(newdp->dccps_hc_rx_ccid);
133 __ccid_get(newdp->dccps_hc_tx_ccid);
134
135 /*
136 * Step 3: Process LISTEN state
137 *
138 * Choose S.ISS (initial seqno) or set from Init Cookie
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300139 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
140 * Cookie
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700141 */
142
143 /* See dccp_v4_conn_request */
144 newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
145
146 newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
147 dccp_update_gsr(newsk, dreq->dreq_isr);
148
149 newdp->dccps_iss = dreq->dreq_iss;
150 dccp_update_gss(newsk, dreq->dreq_iss);
151
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300152 /*
153 * SWL and AWL are initially adjusted so that they are not less than
154 * the initial Sequence Numbers received and sent, respectively:
155 * SWL := max(GSR + 1 - floor(W/4), ISR),
156 * AWL := max(GSS - W' + 1, ISS).
157 * These adjustments MUST be applied only at the beginning of the
158 * connection.
159 */
160 dccp_set_seqno(&newdp->dccps_swl,
161 max48(newdp->dccps_swl, newdp->dccps_isr));
162 dccp_set_seqno(&newdp->dccps_awl,
163 max48(newdp->dccps_awl, newdp->dccps_iss));
164
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700165 dccp_init_xmit_timers(newsk);
166
167 DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
168 }
169 return newsk;
170}
171
172/*
173 * Process an incoming packet for RESPOND sockets represented
174 * as an request_sock.
175 */
176struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
177 struct request_sock *req,
178 struct request_sock **prev)
179{
180 struct sock *child = NULL;
181
182 /* Check for retransmitted REQUEST */
183 if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300184 if (after48(DCCP_SKB_CB(skb)->dccpd_seq,
185 dccp_rsk(req)->dreq_isr)) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700186 struct dccp_request_sock *dreq = dccp_rsk(req);
187
188 dccp_pr_debug("Retransmitted REQUEST\n");
189 /* Send another RESPONSE packet */
190 dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300191 dccp_set_seqno(&dreq->dreq_isr,
192 DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700193 req->rsk_ops->rtx_syn_ack(sk, req, NULL);
194 }
195 /* Network Duplicate, discard packet */
196 return NULL;
197 }
198
199 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
200
201 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
202 dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
203 goto drop;
204
205 /* Invalid ACK */
206 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300207 dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
208 "dreq_iss=%llu\n",
David S. Millerf6ccf552005-08-09 20:27:14 -0700209 (unsigned long long)
210 DCCP_SKB_CB(skb)->dccpd_ack_seq,
211 (unsigned long long)
212 dccp_rsk(req)->dreq_iss);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700213 goto drop;
214 }
215
216 child = dccp_v4_request_recv_sock(sk, skb, req, NULL);
217 if (child == NULL)
218 goto listen_overflow;
219
220 /* FIXME: deal with options */
221
222 inet_csk_reqsk_queue_unlink(sk, req, prev);
223 inet_csk_reqsk_queue_removed(sk, req);
224 inet_csk_reqsk_queue_add(sk, req, child);
225out:
226 return child;
227listen_overflow:
228 dccp_pr_debug("listen_overflow!\n");
229 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
230drop:
231 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
232 req->rsk_ops->send_reset(skb);
233
234 inet_csk_reqsk_queue_drop(sk, req, prev);
235 goto out;
236}
237
238/*
239 * Queue segment on the new socket if the new socket is active,
240 * otherwise we just shortcircuit this and continue with
241 * the new socket.
242 */
243int dccp_child_process(struct sock *parent, struct sock *child,
244 struct sk_buff *skb)
245{
246 int ret = 0;
247 const int state = child->sk_state;
248
249 if (!sock_owned_by_user(child)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300250 ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
251 skb->len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700252
253 /* Wakeup parent, send SIGIO */
254 if (state == DCCP_RESPOND && child->sk_state != state)
255 parent->sk_data_ready(parent, 0);
256 } else {
257 /* Alas, it is possible again, because we do lookup
258 * in main socket hash table and lock on listening
259 * socket does not protect us more.
260 */
261 sk_add_backlog(child, skb);
262 }
263
264 bh_unlock_sock(child);
265 sock_put(child);
266 return ret;
267}