blob: fe4b0fbfa5080ebf049f36b01f12c5c0e9185cdc [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
Gerrit Renker0c869622007-11-28 11:59:48 -020035static int dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070036{
Gerrit Renker0c869622007-11-28 11:59:48 -020037 int queued = 0;
38
39 switch (sk->sk_state) {
40 /*
41 * We ignore Close when received in one of the following states:
42 * - CLOSED (may be a late or duplicate packet)
43 * - PASSIVE_CLOSEREQ (the peer has sent a CloseReq earlier)
44 * - RESPOND (already handled by dccp_check_req)
45 */
46 case DCCP_CLOSING:
47 /*
48 * Simultaneous-close: receiving a Close after sending one. This
49 * can happen if both client and server perform active-close and
50 * will result in an endless ping-pong of crossing and retrans-
51 * mitted Close packets, which only terminates when one of the
52 * nodes times out (min. 64 seconds). Quicker convergence can be
53 * achieved when one of the nodes acts as tie-breaker.
54 * This is ok as both ends are done with data transfer and each
55 * end is just waiting for the other to acknowledge termination.
56 */
57 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT)
58 break;
59 /* fall through */
60 case DCCP_REQUESTING:
61 case DCCP_ACTIVE_CLOSEREQ:
62 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
63 dccp_done(sk);
64 break;
65 case DCCP_OPEN:
66 case DCCP_PARTOPEN:
67 /* Give waiting application a chance to read pending data */
68 queued = 1;
69 dccp_fin(sk, skb);
70 dccp_set_state(sk, DCCP_PASSIVE_CLOSE);
71 /* fall through */
72 case DCCP_PASSIVE_CLOSE:
73 /*
74 * Retransmitted Close: we have already enqueued the first one.
75 */
76 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
77 }
78 return queued;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070079}
80
Gerrit Renker0c869622007-11-28 11:59:48 -020081static int dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070082{
Gerrit Renker0c869622007-11-28 11:59:48 -020083 int queued = 0;
84
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070085 /*
86 * Step 7: Check for unexpected packet types
87 * If (S.is_server and P.type == CloseReq)
88 * Send Sync packet acknowledging P.seqno
89 * Drop packet and return
90 */
91 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030092 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
Gerrit Renker0c869622007-11-28 11:59:48 -020093 return queued;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070094 }
95
Gerrit Renker0c869622007-11-28 11:59:48 -020096 /* Step 13: process relevant Client states < CLOSEREQ */
97 switch (sk->sk_state) {
98 case DCCP_REQUESTING:
99 dccp_send_close(sk, 0);
Arnaldo Carvalho de Melo811265b2005-09-13 19:03:15 -0300100 dccp_set_state(sk, DCCP_CLOSING);
Gerrit Renker0c869622007-11-28 11:59:48 -0200101 break;
102 case DCCP_OPEN:
103 case DCCP_PARTOPEN:
104 /* Give waiting application a chance to read pending data */
105 queued = 1;
106 dccp_fin(sk, skb);
107 dccp_set_state(sk, DCCP_PASSIVE_CLOSEREQ);
108 /* fall through */
109 case DCCP_PASSIVE_CLOSEREQ:
110 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
111 }
112 return queued;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700113}
114
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200115static u8 dccp_reset_code_convert(const u8 code)
116{
117 const u8 error_code[] = {
118 [DCCP_RESET_CODE_CLOSED] = 0, /* normal termination */
119 [DCCP_RESET_CODE_UNSPECIFIED] = 0, /* nothing known */
120 [DCCP_RESET_CODE_ABORTED] = ECONNRESET,
121
122 [DCCP_RESET_CODE_NO_CONNECTION] = ECONNREFUSED,
123 [DCCP_RESET_CODE_CONNECTION_REFUSED] = ECONNREFUSED,
124 [DCCP_RESET_CODE_TOO_BUSY] = EUSERS,
125 [DCCP_RESET_CODE_AGGRESSION_PENALTY] = EDQUOT,
126
127 [DCCP_RESET_CODE_PACKET_ERROR] = ENOMSG,
128 [DCCP_RESET_CODE_BAD_INIT_COOKIE] = EBADR,
129 [DCCP_RESET_CODE_BAD_SERVICE_CODE] = EBADRQC,
130 [DCCP_RESET_CODE_OPTION_ERROR] = EILSEQ,
131 [DCCP_RESET_CODE_MANDATORY_ERROR] = EOPNOTSUPP,
132 };
133
134 return code >= DCCP_MAX_RESET_CODES ? 0 : error_code[code];
135}
136
137static void dccp_rcv_reset(struct sock *sk, struct sk_buff *skb)
138{
139 u8 err = dccp_reset_code_convert(dccp_hdr_reset(skb)->dccph_reset_code);
140
141 sk->sk_err = err;
142
143 /* Queue the equivalent of TCP fin so that dccp_recvmsg exits the loop */
144 dccp_fin(sk, skb);
145
146 if (err && !sock_flag(sk, SOCK_DEAD))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800147 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200148 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
149}
150
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800151static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700152{
153 struct dccp_sock *dp = dccp_sk(sk);
154
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800155 if (dccp_msk(sk)->dccpms_send_ack_vector)
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700156 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
157 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700158}
159
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200160static void dccp_deliver_input_to_ccids(struct sock *sk, struct sk_buff *skb)
161{
162 const struct dccp_sock *dp = dccp_sk(sk);
163
164 /* Don't deliver to RX CCID when node has shut down read end. */
165 if (!(sk->sk_shutdown & RCV_SHUTDOWN))
166 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
167 /*
168 * Until the TX queue has been drained, we can not honour SHUT_WR, since
169 * we need received feedback as input to adjust congestion control.
170 */
171 if (sk->sk_write_queue.qlen > 0 || !(sk->sk_shutdown & SEND_SHUTDOWN))
172 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
173}
174
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700175static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
176{
177 const struct dccp_hdr *dh = dccp_hdr(skb);
178 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700179 u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
180 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700181
182 /*
183 * Step 5: Prepare sequence numbers for Sync
184 * If P.type == Sync or P.type == SyncAck,
185 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
186 * / * P is valid, so update sequence number variables
187 * accordingly. After this update, P will pass the tests
188 * in Step 6. A SyncAck is generated if necessary in
189 * Step 15 * /
190 * Update S.GSR, S.SWL, S.SWH
191 * Otherwise,
192 * Drop packet and return
193 */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200194 if (dh->dccph_type == DCCP_PKT_SYNC ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700195 dh->dccph_type == DCCP_PKT_SYNCACK) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700196 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
197 dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
198 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700199 else
200 return -1;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300201 }
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900202
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700203 /*
204 * Step 6: Check sequence numbers
205 * Let LSWL = S.SWL and LAWL = S.AWL
206 * If P.type == CloseReq or P.type == Close or P.type == Reset,
207 * LSWL := S.GSR + 1, LAWL := S.GAR
208 * If LSWL <= P.seqno <= S.SWH
209 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
210 * Update S.GSR, S.SWL, S.SWH
211 * If P.type != Sync,
212 * Update S.GAR
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700213 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300214 lswl = dp->dccps_swl;
215 lawl = dp->dccps_awl;
216
217 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300218 dh->dccph_type == DCCP_PKT_CLOSE ||
219 dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700220 lswl = ADD48(dp->dccps_gsr, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700221 lawl = dp->dccps_gar;
222 }
223
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700224 if (between48(seqno, lswl, dp->dccps_swh) &&
225 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
226 between48(ackno, lawl, dp->dccps_awh))) {
227 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700228
229 if (dh->dccph_type != DCCP_PKT_SYNC &&
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700230 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
231 dp->dccps_gar = ackno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700232 } else {
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300233 unsigned long now = jiffies;
234 /*
235 * Step 6: Check sequence numbers
236 * Otherwise,
237 * If P.type == Reset,
238 * Send Sync packet acknowledging S.GSR
239 * Otherwise,
240 * Send Sync packet acknowledging P.seqno
241 * Drop packet and return
242 *
243 * These Syncs are rate-limited as per RFC 4340, 7.5.4:
244 * at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
245 */
246 if (time_before(now, (dp->dccps_rate_last +
247 sysctl_dccp_sync_ratelimit)))
248 return 0;
249
Gerrit Renker59348b12006-11-20 18:39:23 -0200250 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
251 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
252 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
253 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700254 (unsigned long long) lswl, (unsigned long long) seqno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200255 (unsigned long long) dp->dccps_swh,
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700256 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
257 : "exists",
258 (unsigned long long) lawl, (unsigned long long) ackno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200259 (unsigned long long) dp->dccps_awh);
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300260
261 dp->dccps_rate_last = now;
262
Gerrit Renkere155d762007-09-25 22:41:56 -0700263 if (dh->dccph_type == DCCP_PKT_RESET)
264 seqno = dp->dccps_gsr;
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700265 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700266 return -1;
267 }
268
269 return 0;
270}
271
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800272static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
273 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700274{
275 struct dccp_sock *dp = dccp_sk(sk);
276
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700277 switch (dccp_hdr(skb)->dccph_type) {
278 case DCCP_PKT_DATAACK:
279 case DCCP_PKT_DATA:
280 /*
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200281 * FIXME: schedule DATA_DROPPED (RFC 4340, 11.7.2) if and when
282 * - sk_shutdown == RCV_SHUTDOWN, use Code 1, "Not Listening"
283 * - sk_receive_queue is full, use Code 2, "Receive Buffer"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700284 */
285 __skb_pull(skb, dh->dccph_doff * 4);
286 __skb_queue_tail(&sk->sk_receive_queue, skb);
287 skb_set_owner_r(skb, sk);
288 sk->sk_data_ready(sk, 0);
289 return 0;
290 case DCCP_PKT_ACK:
291 goto discard;
292 case DCCP_PKT_RESET:
293 /*
294 * Step 9: Process Reset
295 * If P.type == Reset,
296 * Tear down connection
297 * S.state := TIMEWAIT
298 * Set TIMEWAIT timer
299 * Drop packet and return
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200300 */
301 dccp_rcv_reset(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700302 return 0;
303 case DCCP_PKT_CLOSEREQ:
Gerrit Renker0c869622007-11-28 11:59:48 -0200304 if (dccp_rcv_closereq(sk, skb))
305 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700306 goto discard;
307 case DCCP_PKT_CLOSE:
Gerrit Renker0c869622007-11-28 11:59:48 -0200308 if (dccp_rcv_close(sk, skb))
309 return 0;
310 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700311 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200312 /* Step 7
313 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700314 * or (S.is_client and P.type == Request)
315 * or (S.state >= OPEN and P.type == Request
316 * and P.seqno >= S.OSR)
317 * or (S.state >= OPEN and P.type == Response
318 * and P.seqno >= S.OSR)
319 * or (S.state == RESPOND and P.type == Data),
320 * Send Sync packet acknowledging P.seqno
321 * Drop packet and return
322 */
323 if (dp->dccps_role != DCCP_ROLE_LISTEN)
324 goto send_sync;
325 goto check_seq;
326 case DCCP_PKT_RESPONSE:
327 if (dp->dccps_role != DCCP_ROLE_CLIENT)
328 goto send_sync;
329check_seq:
Gerrit Renker8d13bf92007-03-20 13:08:19 -0300330 if (dccp_delta_seqno(dp->dccps_osr,
331 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700332send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300333 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
334 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700335 }
336 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300337 case DCCP_PKT_SYNC:
338 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
339 DCCP_PKT_SYNCACK);
340 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700341 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300342 *
343 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
344 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700345 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300346 */
347 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700348 }
349
350 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
351discard:
352 __kfree_skb(skb);
353 return 0;
354}
355
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800356int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
357 const struct dccp_hdr *dh, const unsigned len)
358{
359 struct dccp_sock *dp = dccp_sk(sk);
360
361 if (dccp_check_seqno(sk, skb))
362 goto discard;
363
364 if (dccp_parse_options(sk, skb))
365 goto discard;
366
367 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
368 dccp_event_ack_recv(sk, skb);
369
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800370 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800371 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
372 DCCP_SKB_CB(skb)->dccpd_seq,
373 DCCP_ACKVEC_STATE_RECEIVED))
374 goto discard;
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200375 dccp_deliver_input_to_ccids(sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800376
377 return __dccp_rcv_established(sk, skb, dh, len);
378discard:
379 __kfree_skb(skb);
380 return 0;
381}
382
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800383EXPORT_SYMBOL_GPL(dccp_rcv_established);
384
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700385static int dccp_rcv_request_sent_state_process(struct sock *sk,
386 struct sk_buff *skb,
387 const struct dccp_hdr *dh,
388 const unsigned len)
389{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200390 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700391 * Step 4: Prepare sequence numbers in REQUEST
392 * If S.state == REQUEST,
393 * If (P.type == Response or P.type == Reset)
394 * and S.AWL <= P.ackno <= S.AWH,
395 * / * Set sequence number variables corresponding to the
396 * other endpoint, so P will pass the tests in Step 6 * /
397 * Set S.GSR, S.ISR, S.SWL, S.SWH
398 * / * Response processing continues in Step 10; Reset
399 * processing continues in Step 9 * /
400 */
401 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
402 const struct inet_connection_sock *icsk = inet_csk(sk);
403 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker3393da82007-09-25 22:40:44 -0700404 long tstamp = dccp_timestamp();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700405
406 /* Stop the REQUEST timer */
407 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
408 BUG_TRAP(sk->sk_send_head != NULL);
409 __kfree_skb(sk->sk_send_head);
410 sk->sk_send_head = NULL;
411
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300412 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
413 dp->dccps_awl, dp->dccps_awh)) {
414 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
415 "P.ackno=%llu, S.AWH=%llu \n",
416 (unsigned long long)dp->dccps_awl,
417 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
418 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700419 goto out_invalid_packet;
420 }
421
Andrea Bittauafe00252006-03-20 17:43:56 -0800422 if (dccp_parse_options(sk, skb))
423 goto out_invalid_packet;
424
Gerrit Renker3393da82007-09-25 22:40:44 -0700425 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
426 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
427 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
428 dp->dccps_options_received.dccpor_timestamp_echo));
Gerrit Renker89560b52007-03-20 15:27:17 -0300429
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900430 if (dccp_msk(sk)->dccpms_send_ack_vector &&
431 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
432 DCCP_SKB_CB(skb)->dccpd_seq,
433 DCCP_ACKVEC_STATE_RECEIVED))
434 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800435
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700436 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300437 dccp_update_gsr(sk, dp->dccps_isr);
438 /*
439 * SWL and AWL are initially adjusted so that they are not less than
440 * the initial Sequence Numbers received and sent, respectively:
441 * SWL := max(GSR + 1 - floor(W/4), ISR),
442 * AWL := max(GSS - W' + 1, ISS).
443 * These adjustments MUST be applied only at the beginning of the
444 * connection.
445 *
446 * AWL was adjusted in dccp_v4_connect -acme
447 */
448 dccp_set_seqno(&dp->dccps_swl,
449 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700450
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800451 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700452
453 /*
454 * Step 10: Process REQUEST state (second part)
455 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300456 * / * If we get here, P is a valid Response from the
457 * server (see Step 4), and we should move to
458 * PARTOPEN state. PARTOPEN means send an Ack,
459 * don't send Data packets, retransmit Acks
460 * periodically, and always include any Init Cookie
461 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700462 * S.state := PARTOPEN
463 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200464 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300465 * / * Step 12 will send the Ack completing the
466 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700467 */
468 dccp_set_state(sk, DCCP_PARTOPEN);
469
470 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800471 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700472
473 if (!sock_flag(sk, SOCK_DEAD)) {
474 sk->sk_state_change(sk);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800475 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700476 }
477
478 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
479 icsk->icsk_accept_queue.rskq_defer_accept) {
480 /* Save one ACK. Data will be ready after
481 * several ticks, if write_pending is set.
482 *
483 * It may be deleted, but with this feature tcpdumps
484 * look so _wonderfully_ clever, that I was not able
485 * to stand against the temptation 8) --ANK
486 */
487 /*
488 * OK, in DCCP we can as well do a similar trick, its
489 * even in the draft, but there is no need for us to
490 * schedule an ack here, as dccp_sendmsg does this for
491 * us, also stated in the draft. -acme
492 */
493 __kfree_skb(skb);
494 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200495 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700496 dccp_send_ack(sk);
497 return -1;
498 }
499
500out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700501 /* dccp_v4_do_rcv will send a reset */
502 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200503 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504}
505
506static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
507 struct sk_buff *skb,
508 const struct dccp_hdr *dh,
509 const unsigned len)
510{
511 int queued = 0;
512
513 switch (dh->dccph_type) {
514 case DCCP_PKT_RESET:
515 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
516 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700517 case DCCP_PKT_DATA:
518 if (sk->sk_state == DCCP_RESPOND)
519 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700520 case DCCP_PKT_DATAACK:
521 case DCCP_PKT_ACK:
522 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300523 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
524 * here but only if we haven't used the DELACK timer for
525 * something else, like sending a delayed ack for a TIMESTAMP
526 * echo, etc, for now were not clearing it, sending an extra
527 * ACK when there is nothing else to do in DELACK is not a big
528 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700529 */
530
531 /* Stop the PARTOPEN timer */
532 if (sk->sk_state == DCCP_PARTOPEN)
533 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
534
535 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
536 dccp_set_state(sk, DCCP_OPEN);
537
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700538 if (dh->dccph_type == DCCP_PKT_DATAACK ||
539 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800540 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300541 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800542 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700543 }
544 break;
545 }
546
547 return queued;
548}
549
550int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
551 struct dccp_hdr *dh, unsigned len)
552{
553 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700554 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700555 const int old_state = sk->sk_state;
556 int queued = 0;
557
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300558 /*
559 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300560 *
561 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200562 * If P.type == Request or P contains a valid Init Cookie option,
563 * (* Must scan the packet's options to check for Init
564 * Cookies. Only Init Cookies are processed here,
565 * however; other options are processed in Step 8. This
566 * scan need only be performed if the endpoint uses Init
567 * Cookies *)
568 * (* Generate a new socket and switch to that socket *)
569 * Set S := new socket for this port pair
570 * S.state = RESPOND
571 * Choose S.ISS (initial seqno) or set from Init Cookies
572 * Initialize S.GAR := S.ISS
573 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
574 * Cookies Continue with S.state == RESPOND
575 * (* A Response packet will be generated in Step 11 *)
576 * Otherwise,
577 * Generate Reset(No Connection) unless P.type == Reset
578 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300579 */
580 if (sk->sk_state == DCCP_LISTEN) {
581 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800582 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
583 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300584 return 1;
585
586 /* FIXME: do congestion control initialization */
587 goto discard;
588 }
589 if (dh->dccph_type == DCCP_PKT_RESET)
590 goto discard;
591
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700592 /* Caller (dccp_v4_do_rcv) will send Reset */
593 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300594 return 1;
595 }
596
597 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700598 if (dccp_check_seqno(sk, skb))
599 goto discard;
600
601 /*
602 * Step 8: Process options and mark acknowledgeable
603 */
604 if (dccp_parse_options(sk, skb))
605 goto discard;
606
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700607 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700608 dccp_event_ack_recv(sk, skb);
609
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200610 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700611 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200612 DCCP_SKB_CB(skb)->dccpd_seq,
613 DCCP_ACKVEC_STATE_RECEIVED))
614 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800615
Gerrit Renker8e8c71f2007-11-21 09:56:48 -0200616 dccp_deliver_input_to_ccids(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700617 }
618
619 /*
620 * Step 9: Process Reset
621 * If P.type == Reset,
622 * Tear down connection
623 * S.state := TIMEWAIT
624 * Set TIMEWAIT timer
625 * Drop packet and return
626 */
627 if (dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkerd8ef2c22007-10-24 10:27:48 -0200628 dccp_rcv_reset(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700629 return 0;
630 /*
631 * Step 7: Check for unexpected packet types
632 * If (S.is_server and P.type == CloseReq)
633 * or (S.is_server and P.type == Response)
634 * or (S.is_client and P.type == Request)
635 * or (S.state == RESPOND and P.type == Data),
636 * Send Sync packet acknowledging P.seqno
637 * Drop packet and return
638 */
639 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300640 (dh->dccph_type == DCCP_PKT_RESPONSE ||
641 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700642 (dp->dccps_role == DCCP_ROLE_CLIENT &&
643 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300644 (sk->sk_state == DCCP_RESPOND &&
645 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700646 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700647 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700648 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
Gerrit Renker0c869622007-11-28 11:59:48 -0200649 if (dccp_rcv_closereq(sk, skb))
650 return 0;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700651 goto discard;
652 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
Gerrit Renker0c869622007-11-28 11:59:48 -0200653 if (dccp_rcv_close(sk, skb))
654 return 0;
655 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700656 }
657
658 switch (sk->sk_state) {
659 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700660 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700661 return 1;
662
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700663 case DCCP_REQUESTING:
664 /* FIXME: do congestion control initialization */
665
666 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
667 if (queued >= 0)
668 return queued;
669
670 __kfree_skb(skb);
671 return 0;
672
673 case DCCP_RESPOND:
674 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300675 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
676 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700677 break;
678 }
679
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300680 if (dh->dccph_type == DCCP_PKT_ACK ||
681 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700682 switch (old_state) {
683 case DCCP_PARTOPEN:
684 sk->sk_state_change(sk);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800685 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700686 break;
687 }
Gerrit Renker08831702007-09-26 10:30:05 -0300688 } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
689 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
690 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700691 }
692
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200693 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700694discard:
695 __kfree_skb(skb);
696 }
697 return 0;
698}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800699
700EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
Gerrit Renker4712a792007-03-20 15:23:18 -0300701
702/**
Gerrit Renker3393da82007-09-25 22:40:44 -0700703 * dccp_sample_rtt - Validate and finalise computation of RTT sample
704 * @delta: number of microseconds between packet and acknowledgment
705 * The routine is kept generic to work in different contexts. It should be
706 * called immediately when the ACK used for the RTT sample arrives.
Gerrit Renker4712a792007-03-20 15:23:18 -0300707 */
Gerrit Renker3393da82007-09-25 22:40:44 -0700708u32 dccp_sample_rtt(struct sock *sk, long delta)
Gerrit Renker4712a792007-03-20 15:23:18 -0300709{
Gerrit Renker3393da82007-09-25 22:40:44 -0700710 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
711 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
Gerrit Renker4712a792007-03-20 15:23:18 -0300712
713 if (unlikely(delta <= 0)) {
Gerrit Renker3393da82007-09-25 22:40:44 -0700714 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300715 return DCCP_SANE_RTT_MIN;
716 }
Gerrit Renker3393da82007-09-25 22:40:44 -0700717 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
718 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300719 return DCCP_SANE_RTT_MAX;
720 }
721
722 return delta;
723}
724
725EXPORT_SYMBOL_GPL(dccp_sample_rtt);