blob: ef299fbd7c26840880060d1be1ee7f8529f690b6 [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/input.c
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02003 *
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07004 * 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
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070013#include <linux/dccp.h>
14#include <linux/skbuff.h>
15
16#include <net/sock.h>
17
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -070018#include "ackvec.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070019#include "ccid.h"
20#include "dccp.h"
21
Ingo Molnarbd5435e2007-10-17 19:33:06 -070022/* rate-limit for syncs in reply to sequence-invalid packets; RFC 4340, 7.5.4 */
23int sysctl_dccp_sync_ratelimit __read_mostly = HZ / 8;
24
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070025static void dccp_fin(struct sock *sk, struct sk_buff *skb)
26{
27 sk->sk_shutdown |= RCV_SHUTDOWN;
28 sock_set_flag(sk, SOCK_DONE);
29 __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
30 __skb_queue_tail(&sk->sk_receive_queue, skb);
31 skb_set_owner_r(skb, sk);
32 sk->sk_data_ready(sk, 0);
33}
34
35static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
36{
Arnaldo Carvalho de Melo017487d2006-03-20 19:25:24 -080037 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -070038 dccp_fin(sk, skb);
39 dccp_set_state(sk, DCCP_CLOSED);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080040 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070041}
42
43static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
44{
45 /*
46 * Step 7: Check for unexpected packet types
47 * If (S.is_server and P.type == CloseReq)
48 * Send Sync packet acknowledging P.seqno
49 * Drop packet and return
50 */
51 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030052 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070053 return;
54 }
55
Arnaldo Carvalho de Melo811265b2005-09-13 19:03:15 -030056 if (sk->sk_state != DCCP_CLOSING)
57 dccp_set_state(sk, DCCP_CLOSING);
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -070058 dccp_send_close(sk, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070059}
60
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -020061static u8 dccp_reset_code_convert(const u8 code)
62{
63 const u8 error_code[] = {
64 [DCCP_RESET_CODE_CLOSED] = 0, /* normal termination */
65 [DCCP_RESET_CODE_UNSPECIFIED] = 0, /* nothing known */
66 [DCCP_RESET_CODE_ABORTED] = ECONNRESET,
67
68 [DCCP_RESET_CODE_NO_CONNECTION] = ECONNREFUSED,
69 [DCCP_RESET_CODE_CONNECTION_REFUSED] = ECONNREFUSED,
70 [DCCP_RESET_CODE_TOO_BUSY] = EUSERS,
71 [DCCP_RESET_CODE_AGGRESSION_PENALTY] = EDQUOT,
72
73 [DCCP_RESET_CODE_PACKET_ERROR] = ENOMSG,
74 [DCCP_RESET_CODE_BAD_INIT_COOKIE] = EBADR,
75 [DCCP_RESET_CODE_BAD_SERVICE_CODE] = EBADRQC,
76 [DCCP_RESET_CODE_OPTION_ERROR] = EILSEQ,
77 [DCCP_RESET_CODE_MANDATORY_ERROR] = EOPNOTSUPP,
78 };
79
80 return code >= DCCP_MAX_RESET_CODES ? 0 : error_code[code];
81}
82
83static void dccp_rcv_reset(struct sock *sk, struct sk_buff *skb)
84{
85 u8 err = dccp_reset_code_convert(dccp_hdr_reset(skb)->dccph_reset_code);
86
87 sk->sk_err = err;
88
89 /* Queue the equivalent of TCP fin so that dccp_recvmsg exits the loop */
90 dccp_fin(sk, skb);
91
92 if (err && !sock_flag(sk, SOCK_DEAD))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080093 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -020094 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
95}
96
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080097static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070098{
99 struct dccp_sock *dp = dccp_sk(sk);
100
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800101 if (dccp_msk(sk)->dccpms_send_ack_vector)
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700102 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
103 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700104}
105
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200106static void dccp_deliver_input_to_ccids(struct sock *sk, struct sk_buff *skb)
107{
108 const struct dccp_sock *dp = dccp_sk(sk);
109
110 /* Don't deliver to RX CCID when node has shut down read end. */
111 if (!(sk->sk_shutdown & RCV_SHUTDOWN))
112 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
113 /*
114 * Until the TX queue has been drained, we can not honour SHUT_WR, since
115 * we need received feedback as input to adjust congestion control.
116 */
117 if (sk->sk_write_queue.qlen > 0 || !(sk->sk_shutdown & SEND_SHUTDOWN))
118 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
119}
120
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700121static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
122{
123 const struct dccp_hdr *dh = dccp_hdr(skb);
124 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700125 u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
126 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700127
128 /*
129 * Step 5: Prepare sequence numbers for Sync
130 * If P.type == Sync or P.type == SyncAck,
131 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
132 * / * P is valid, so update sequence number variables
133 * accordingly. After this update, P will pass the tests
134 * in Step 6. A SyncAck is generated if necessary in
135 * Step 15 * /
136 * Update S.GSR, S.SWL, S.SWH
137 * Otherwise,
138 * Drop packet and return
139 */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200140 if (dh->dccph_type == DCCP_PKT_SYNC ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700141 dh->dccph_type == DCCP_PKT_SYNCACK) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700142 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
143 dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
144 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700145 else
146 return -1;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300147 }
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900148
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700149 /*
150 * Step 6: Check sequence numbers
151 * Let LSWL = S.SWL and LAWL = S.AWL
152 * If P.type == CloseReq or P.type == Close or P.type == Reset,
153 * LSWL := S.GSR + 1, LAWL := S.GAR
154 * If LSWL <= P.seqno <= S.SWH
155 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
156 * Update S.GSR, S.SWL, S.SWH
157 * If P.type != Sync,
158 * Update S.GAR
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700159 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300160 lswl = dp->dccps_swl;
161 lawl = dp->dccps_awl;
162
163 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300164 dh->dccph_type == DCCP_PKT_CLOSE ||
165 dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700166 lswl = ADD48(dp->dccps_gsr, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700167 lawl = dp->dccps_gar;
168 }
169
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700170 if (between48(seqno, lswl, dp->dccps_swh) &&
171 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
172 between48(ackno, lawl, dp->dccps_awh))) {
173 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700174
175 if (dh->dccph_type != DCCP_PKT_SYNC &&
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700176 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
177 dp->dccps_gar = ackno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700178 } else {
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300179 unsigned long now = jiffies;
180 /*
181 * Step 6: Check sequence numbers
182 * Otherwise,
183 * If P.type == Reset,
184 * Send Sync packet acknowledging S.GSR
185 * Otherwise,
186 * Send Sync packet acknowledging P.seqno
187 * Drop packet and return
188 *
189 * These Syncs are rate-limited as per RFC 4340, 7.5.4:
190 * at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
191 */
192 if (time_before(now, (dp->dccps_rate_last +
193 sysctl_dccp_sync_ratelimit)))
194 return 0;
195
Gerrit Renker59348b12006-11-20 18:39:23 -0200196 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
197 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
198 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
199 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700200 (unsigned long long) lswl, (unsigned long long) seqno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200201 (unsigned long long) dp->dccps_swh,
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700202 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
203 : "exists",
204 (unsigned long long) lawl, (unsigned long long) ackno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200205 (unsigned long long) dp->dccps_awh);
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300206
207 dp->dccps_rate_last = now;
208
Gerrit Renkere155d762007-09-25 22:41:56 -0700209 if (dh->dccph_type == DCCP_PKT_RESET)
210 seqno = dp->dccps_gsr;
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700211 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700212 return -1;
213 }
214
215 return 0;
216}
217
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800218static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
219 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700220{
221 struct dccp_sock *dp = dccp_sk(sk);
222
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700223 switch (dccp_hdr(skb)->dccph_type) {
224 case DCCP_PKT_DATAACK:
225 case DCCP_PKT_DATA:
226 /*
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200227 * FIXME: schedule DATA_DROPPED (RFC 4340, 11.7.2) if and when
228 * - sk_shutdown == RCV_SHUTDOWN, use Code 1, "Not Listening"
229 * - sk_receive_queue is full, use Code 2, "Receive Buffer"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700230 */
231 __skb_pull(skb, dh->dccph_doff * 4);
232 __skb_queue_tail(&sk->sk_receive_queue, skb);
233 skb_set_owner_r(skb, sk);
234 sk->sk_data_ready(sk, 0);
235 return 0;
236 case DCCP_PKT_ACK:
237 goto discard;
238 case DCCP_PKT_RESET:
239 /*
240 * Step 9: Process Reset
241 * If P.type == Reset,
242 * Tear down connection
243 * S.state := TIMEWAIT
244 * Set TIMEWAIT timer
245 * Drop packet and return
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200246 */
247 dccp_rcv_reset(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700248 return 0;
249 case DCCP_PKT_CLOSEREQ:
250 dccp_rcv_closereq(sk, skb);
251 goto discard;
252 case DCCP_PKT_CLOSE:
253 dccp_rcv_close(sk, skb);
254 return 0;
255 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200256 /* Step 7
257 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700258 * or (S.is_client and P.type == Request)
259 * or (S.state >= OPEN and P.type == Request
260 * and P.seqno >= S.OSR)
261 * or (S.state >= OPEN and P.type == Response
262 * and P.seqno >= S.OSR)
263 * or (S.state == RESPOND and P.type == Data),
264 * Send Sync packet acknowledging P.seqno
265 * Drop packet and return
266 */
267 if (dp->dccps_role != DCCP_ROLE_LISTEN)
268 goto send_sync;
269 goto check_seq;
270 case DCCP_PKT_RESPONSE:
271 if (dp->dccps_role != DCCP_ROLE_CLIENT)
272 goto send_sync;
273check_seq:
Gerrit Renker8d13bf92007-03-20 13:08:19 -0300274 if (dccp_delta_seqno(dp->dccps_osr,
275 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700276send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300277 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
278 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700279 }
280 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300281 case DCCP_PKT_SYNC:
282 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
283 DCCP_PKT_SYNCACK);
284 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700285 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300286 *
287 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
288 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700289 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300290 */
291 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700292 }
293
294 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
295discard:
296 __kfree_skb(skb);
297 return 0;
298}
299
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800300int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
301 const struct dccp_hdr *dh, const unsigned len)
302{
303 struct dccp_sock *dp = dccp_sk(sk);
304
305 if (dccp_check_seqno(sk, skb))
306 goto discard;
307
308 if (dccp_parse_options(sk, skb))
309 goto discard;
310
311 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
312 dccp_event_ack_recv(sk, skb);
313
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800314 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800315 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
316 DCCP_SKB_CB(skb)->dccpd_seq,
317 DCCP_ACKVEC_STATE_RECEIVED))
318 goto discard;
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200319 dccp_deliver_input_to_ccids(sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800320
321 return __dccp_rcv_established(sk, skb, dh, len);
322discard:
323 __kfree_skb(skb);
324 return 0;
325}
326
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800327EXPORT_SYMBOL_GPL(dccp_rcv_established);
328
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700329static int dccp_rcv_request_sent_state_process(struct sock *sk,
330 struct sk_buff *skb,
331 const struct dccp_hdr *dh,
332 const unsigned len)
333{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200334 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700335 * Step 4: Prepare sequence numbers in REQUEST
336 * If S.state == REQUEST,
337 * If (P.type == Response or P.type == Reset)
338 * and S.AWL <= P.ackno <= S.AWH,
339 * / * Set sequence number variables corresponding to the
340 * other endpoint, so P will pass the tests in Step 6 * /
341 * Set S.GSR, S.ISR, S.SWL, S.SWH
342 * / * Response processing continues in Step 10; Reset
343 * processing continues in Step 9 * /
344 */
345 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
346 const struct inet_connection_sock *icsk = inet_csk(sk);
347 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker3393da82007-09-25 22:40:44 -0700348 long tstamp = dccp_timestamp();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700349
350 /* Stop the REQUEST timer */
351 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
352 BUG_TRAP(sk->sk_send_head != NULL);
353 __kfree_skb(sk->sk_send_head);
354 sk->sk_send_head = NULL;
355
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300356 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
357 dp->dccps_awl, dp->dccps_awh)) {
358 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
359 "P.ackno=%llu, S.AWH=%llu \n",
360 (unsigned long long)dp->dccps_awl,
361 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
362 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700363 goto out_invalid_packet;
364 }
365
Andrea Bittauafe00252006-03-20 17:43:56 -0800366 if (dccp_parse_options(sk, skb))
367 goto out_invalid_packet;
368
Gerrit Renker3393da82007-09-25 22:40:44 -0700369 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
370 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
371 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
372 dp->dccps_options_received.dccpor_timestamp_echo));
Gerrit Renker89560b52007-03-20 15:27:17 -0300373
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900374 if (dccp_msk(sk)->dccpms_send_ack_vector &&
375 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
376 DCCP_SKB_CB(skb)->dccpd_seq,
377 DCCP_ACKVEC_STATE_RECEIVED))
378 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800379
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700380 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300381 dccp_update_gsr(sk, dp->dccps_isr);
382 /*
383 * SWL and AWL are initially adjusted so that they are not less than
384 * the initial Sequence Numbers received and sent, respectively:
385 * SWL := max(GSR + 1 - floor(W/4), ISR),
386 * AWL := max(GSS - W' + 1, ISS).
387 * These adjustments MUST be applied only at the beginning of the
388 * connection.
389 *
390 * AWL was adjusted in dccp_v4_connect -acme
391 */
392 dccp_set_seqno(&dp->dccps_swl,
393 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700394
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800395 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700396
397 /*
398 * Step 10: Process REQUEST state (second part)
399 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300400 * / * If we get here, P is a valid Response from the
401 * server (see Step 4), and we should move to
402 * PARTOPEN state. PARTOPEN means send an Ack,
403 * don't send Data packets, retransmit Acks
404 * periodically, and always include any Init Cookie
405 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700406 * S.state := PARTOPEN
407 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200408 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300409 * / * Step 12 will send the Ack completing the
410 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700411 */
412 dccp_set_state(sk, DCCP_PARTOPEN);
413
414 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800415 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700416
417 if (!sock_flag(sk, SOCK_DEAD)) {
418 sk->sk_state_change(sk);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800419 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700420 }
421
422 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
423 icsk->icsk_accept_queue.rskq_defer_accept) {
424 /* Save one ACK. Data will be ready after
425 * several ticks, if write_pending is set.
426 *
427 * It may be deleted, but with this feature tcpdumps
428 * look so _wonderfully_ clever, that I was not able
429 * to stand against the temptation 8) --ANK
430 */
431 /*
432 * OK, in DCCP we can as well do a similar trick, its
433 * even in the draft, but there is no need for us to
434 * schedule an ack here, as dccp_sendmsg does this for
435 * us, also stated in the draft. -acme
436 */
437 __kfree_skb(skb);
438 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200439 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700440 dccp_send_ack(sk);
441 return -1;
442 }
443
444out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700445 /* dccp_v4_do_rcv will send a reset */
446 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200447 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700448}
449
450static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
451 struct sk_buff *skb,
452 const struct dccp_hdr *dh,
453 const unsigned len)
454{
455 int queued = 0;
456
457 switch (dh->dccph_type) {
458 case DCCP_PKT_RESET:
459 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
460 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700461 case DCCP_PKT_DATA:
462 if (sk->sk_state == DCCP_RESPOND)
463 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700464 case DCCP_PKT_DATAACK:
465 case DCCP_PKT_ACK:
466 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300467 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
468 * here but only if we haven't used the DELACK timer for
469 * something else, like sending a delayed ack for a TIMESTAMP
470 * echo, etc, for now were not clearing it, sending an extra
471 * ACK when there is nothing else to do in DELACK is not a big
472 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700473 */
474
475 /* Stop the PARTOPEN timer */
476 if (sk->sk_state == DCCP_PARTOPEN)
477 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
478
479 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
480 dccp_set_state(sk, DCCP_OPEN);
481
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700482 if (dh->dccph_type == DCCP_PKT_DATAACK ||
483 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800484 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300485 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800486 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700487 }
488 break;
489 }
490
491 return queued;
492}
493
494int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
495 struct dccp_hdr *dh, unsigned len)
496{
497 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700498 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700499 const int old_state = sk->sk_state;
500 int queued = 0;
501
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300502 /*
503 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300504 *
505 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200506 * If P.type == Request or P contains a valid Init Cookie option,
507 * (* Must scan the packet's options to check for Init
508 * Cookies. Only Init Cookies are processed here,
509 * however; other options are processed in Step 8. This
510 * scan need only be performed if the endpoint uses Init
511 * Cookies *)
512 * (* Generate a new socket and switch to that socket *)
513 * Set S := new socket for this port pair
514 * S.state = RESPOND
515 * Choose S.ISS (initial seqno) or set from Init Cookies
516 * Initialize S.GAR := S.ISS
517 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
518 * Cookies Continue with S.state == RESPOND
519 * (* A Response packet will be generated in Step 11 *)
520 * Otherwise,
521 * Generate Reset(No Connection) unless P.type == Reset
522 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300523 */
524 if (sk->sk_state == DCCP_LISTEN) {
525 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800526 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
527 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300528 return 1;
529
530 /* FIXME: do congestion control initialization */
531 goto discard;
532 }
533 if (dh->dccph_type == DCCP_PKT_RESET)
534 goto discard;
535
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700536 /* Caller (dccp_v4_do_rcv) will send Reset */
537 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300538 return 1;
539 }
540
541 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700542 if (dccp_check_seqno(sk, skb))
543 goto discard;
544
545 /*
546 * Step 8: Process options and mark acknowledgeable
547 */
548 if (dccp_parse_options(sk, skb))
549 goto discard;
550
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700551 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700552 dccp_event_ack_recv(sk, skb);
553
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200554 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700555 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200556 DCCP_SKB_CB(skb)->dccpd_seq,
557 DCCP_ACKVEC_STATE_RECEIVED))
558 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800559
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200560 dccp_deliver_input_to_ccids(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700561 }
562
563 /*
564 * Step 9: Process Reset
565 * If P.type == Reset,
566 * Tear down connection
567 * S.state := TIMEWAIT
568 * Set TIMEWAIT timer
569 * Drop packet and return
570 */
571 if (dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200572 dccp_rcv_reset(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700573 return 0;
574 /*
575 * Step 7: Check for unexpected packet types
576 * If (S.is_server and P.type == CloseReq)
577 * or (S.is_server and P.type == Response)
578 * or (S.is_client and P.type == Request)
579 * or (S.state == RESPOND and P.type == Data),
580 * Send Sync packet acknowledging P.seqno
581 * Drop packet and return
582 */
583 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300584 (dh->dccph_type == DCCP_PKT_RESPONSE ||
585 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700586 (dp->dccps_role == DCCP_ROLE_CLIENT &&
587 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300588 (sk->sk_state == DCCP_RESPOND &&
589 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700590 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700591 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700592 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
593 dccp_rcv_closereq(sk, skb);
594 goto discard;
595 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
596 dccp_rcv_close(sk, skb);
597 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700598 }
599
600 switch (sk->sk_state) {
601 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700602 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700603 return 1;
604
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700605 case DCCP_REQUESTING:
606 /* FIXME: do congestion control initialization */
607
608 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
609 if (queued >= 0)
610 return queued;
611
612 __kfree_skb(skb);
613 return 0;
614
615 case DCCP_RESPOND:
616 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300617 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
618 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700619 break;
620 }
621
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300622 if (dh->dccph_type == DCCP_PKT_ACK ||
623 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700624 switch (old_state) {
625 case DCCP_PARTOPEN:
626 sk->sk_state_change(sk);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800627 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700628 break;
629 }
Gerrit Renker08831702007-09-26 10:30:05 -0300630 } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
631 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
632 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700633 }
634
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200635 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700636discard:
637 __kfree_skb(skb);
638 }
639 return 0;
640}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800641
642EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
Gerrit Renker4712a792007-03-20 15:23:18 -0300643
644/**
Gerrit Renker3393da82007-09-25 22:40:44 -0700645 * dccp_sample_rtt - Validate and finalise computation of RTT sample
646 * @delta: number of microseconds between packet and acknowledgment
647 * The routine is kept generic to work in different contexts. It should be
648 * called immediately when the ACK used for the RTT sample arrives.
Gerrit Renker4712a792007-03-20 15:23:18 -0300649 */
Gerrit Renker3393da82007-09-25 22:40:44 -0700650u32 dccp_sample_rtt(struct sock *sk, long delta)
Gerrit Renker4712a792007-03-20 15:23:18 -0300651{
Gerrit Renker3393da82007-09-25 22:40:44 -0700652 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
653 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
Gerrit Renker4712a792007-03-20 15:23:18 -0300654
655 if (unlikely(delta <= 0)) {
Gerrit Renker3393da82007-09-25 22:40:44 -0700656 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300657 return DCCP_SANE_RTT_MIN;
658 }
Gerrit Renker3393da82007-09-25 22:40:44 -0700659 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
660 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300661 return DCCP_SANE_RTT_MAX;
662 }
663
664 return delta;
665}
666
667EXPORT_SYMBOL_GPL(dccp_sample_rtt);