blob: 3b651701cfe49244e0f44cacb5e31b6c2b160218 [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
22static void dccp_fin(struct sock *sk, struct sk_buff *skb)
23{
24 sk->sk_shutdown |= RCV_SHUTDOWN;
25 sock_set_flag(sk, SOCK_DONE);
26 __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
27 __skb_queue_tail(&sk->sk_receive_queue, skb);
28 skb_set_owner_r(skb, sk);
29 sk->sk_data_ready(sk, 0);
30}
31
32static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
33{
Arnaldo Carvalho de Melo017487d2006-03-20 19:25:24 -080034 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -070035 dccp_fin(sk, skb);
36 dccp_set_state(sk, DCCP_CLOSED);
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -070037 sk_wake_async(sk, 1, POLL_HUP);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070038}
39
40static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
41{
42 /*
43 * Step 7: Check for unexpected packet types
44 * If (S.is_server and P.type == CloseReq)
45 * Send Sync packet acknowledging P.seqno
46 * Drop packet and return
47 */
48 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030049 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070050 return;
51 }
52
Arnaldo Carvalho de Melo811265b2005-09-13 19:03:15 -030053 if (sk->sk_state != DCCP_CLOSING)
54 dccp_set_state(sk, DCCP_CLOSING);
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -070055 dccp_send_close(sk, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070056}
57
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080058static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070059{
60 struct dccp_sock *dp = dccp_sk(sk);
61
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080062 if (dccp_msk(sk)->dccpms_send_ack_vector)
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -070063 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
64 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065}
66
67static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
68{
69 const struct dccp_hdr *dh = dccp_hdr(skb);
70 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030071 u64 lswl, lawl;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070072
73 /*
74 * Step 5: Prepare sequence numbers for Sync
75 * If P.type == Sync or P.type == SyncAck,
76 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
77 * / * P is valid, so update sequence number variables
78 * accordingly. After this update, P will pass the tests
79 * in Step 6. A SyncAck is generated if necessary in
80 * Step 15 * /
81 * Update S.GSR, S.SWL, S.SWH
82 * Otherwise,
83 * Drop packet and return
84 */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -020085 if (dh->dccph_type == DCCP_PKT_SYNC ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070086 dh->dccph_type == DCCP_PKT_SYNCACK) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030087 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
88 dp->dccps_awl, dp->dccps_awh) &&
Gerrit Renker8d13bf92007-03-20 13:08:19 -030089 dccp_delta_seqno(dp->dccps_swl,
90 DCCP_SKB_CB(skb)->dccpd_seq) >= 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070091 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
92 else
93 return -1;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030094 }
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +090095
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070096 /*
97 * Step 6: Check sequence numbers
98 * Let LSWL = S.SWL and LAWL = S.AWL
99 * If P.type == CloseReq or P.type == Close or P.type == Reset,
100 * LSWL := S.GSR + 1, LAWL := S.GAR
101 * If LSWL <= P.seqno <= S.SWH
102 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
103 * Update S.GSR, S.SWL, S.SWH
104 * If P.type != Sync,
105 * Update S.GAR
106 * Otherwise,
107 * Send Sync packet acknowledging P.seqno
108 * Drop packet and return
109 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300110 lswl = dp->dccps_swl;
111 lawl = dp->dccps_awl;
112
113 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300114 dh->dccph_type == DCCP_PKT_CLOSE ||
115 dh->dccph_type == DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700116 lswl = dp->dccps_gsr;
117 dccp_inc_seqno(&lswl);
118 lawl = dp->dccps_gar;
119 }
120
121 if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
122 (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300123 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
124 lawl, dp->dccps_awh))) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700125 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
126
127 if (dh->dccph_type != DCCP_PKT_SYNC &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300128 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
129 DCCP_PKT_WITHOUT_ACK_SEQ))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700130 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
131 } else {
Gerrit Renker59348b12006-11-20 18:39:23 -0200132 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
133 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
134 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
135 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
136 (unsigned long long) lswl,
137 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq,
138 (unsigned long long) dp->dccps_swh,
139 (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900140 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
Gerrit Renker59348b12006-11-20 18:39:23 -0200141 (unsigned long long) lawl,
142 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
143 (unsigned long long) dp->dccps_awh);
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300144 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700145 return -1;
146 }
147
148 return 0;
149}
150
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800151static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
152 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700153{
154 struct dccp_sock *dp = dccp_sk(sk);
155
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700156 switch (dccp_hdr(skb)->dccph_type) {
157 case DCCP_PKT_DATAACK:
158 case DCCP_PKT_DATA:
159 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300160 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
161 * option if it is.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700162 */
163 __skb_pull(skb, dh->dccph_doff * 4);
164 __skb_queue_tail(&sk->sk_receive_queue, skb);
165 skb_set_owner_r(skb, sk);
166 sk->sk_data_ready(sk, 0);
167 return 0;
168 case DCCP_PKT_ACK:
169 goto discard;
170 case DCCP_PKT_RESET:
171 /*
172 * Step 9: Process Reset
173 * If P.type == Reset,
174 * Tear down connection
175 * S.state := TIMEWAIT
176 * Set TIMEWAIT timer
177 * Drop packet and return
178 */
179 dccp_fin(sk, skb);
180 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
181 return 0;
182 case DCCP_PKT_CLOSEREQ:
183 dccp_rcv_closereq(sk, skb);
184 goto discard;
185 case DCCP_PKT_CLOSE:
186 dccp_rcv_close(sk, skb);
187 return 0;
188 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200189 /* Step 7
190 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700191 * or (S.is_client and P.type == Request)
192 * or (S.state >= OPEN and P.type == Request
193 * and P.seqno >= S.OSR)
194 * or (S.state >= OPEN and P.type == Response
195 * and P.seqno >= S.OSR)
196 * or (S.state == RESPOND and P.type == Data),
197 * Send Sync packet acknowledging P.seqno
198 * Drop packet and return
199 */
200 if (dp->dccps_role != DCCP_ROLE_LISTEN)
201 goto send_sync;
202 goto check_seq;
203 case DCCP_PKT_RESPONSE:
204 if (dp->dccps_role != DCCP_ROLE_CLIENT)
205 goto send_sync;
206check_seq:
Gerrit Renker8d13bf92007-03-20 13:08:19 -0300207 if (dccp_delta_seqno(dp->dccps_osr,
208 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700209send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300210 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
211 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700212 }
213 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300214 case DCCP_PKT_SYNC:
215 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
216 DCCP_PKT_SYNCACK);
217 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700218 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300219 *
220 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
221 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700222 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300223 */
224 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700225 }
226
227 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
228discard:
229 __kfree_skb(skb);
230 return 0;
231}
232
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800233int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
234 const struct dccp_hdr *dh, const unsigned len)
235{
236 struct dccp_sock *dp = dccp_sk(sk);
237
238 if (dccp_check_seqno(sk, skb))
239 goto discard;
240
241 if (dccp_parse_options(sk, skb))
242 goto discard;
243
244 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
245 dccp_event_ack_recv(sk, skb);
246
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800247 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800248 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
249 DCCP_SKB_CB(skb)->dccpd_seq,
250 DCCP_ACKVEC_STATE_RECEIVED))
251 goto discard;
252
Gerrit Renker151a9932007-03-07 12:53:48 -0800253 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
254 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800255
256 return __dccp_rcv_established(sk, skb, dh, len);
257discard:
258 __kfree_skb(skb);
259 return 0;
260}
261
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800262EXPORT_SYMBOL_GPL(dccp_rcv_established);
263
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700264static int dccp_rcv_request_sent_state_process(struct sock *sk,
265 struct sk_buff *skb,
266 const struct dccp_hdr *dh,
267 const unsigned len)
268{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200269 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700270 * Step 4: Prepare sequence numbers in REQUEST
271 * If S.state == REQUEST,
272 * If (P.type == Response or P.type == Reset)
273 * and S.AWL <= P.ackno <= S.AWH,
274 * / * Set sequence number variables corresponding to the
275 * other endpoint, so P will pass the tests in Step 6 * /
276 * Set S.GSR, S.ISR, S.SWL, S.SWH
277 * / * Response processing continues in Step 10; Reset
278 * processing continues in Step 9 * /
279 */
280 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
281 const struct inet_connection_sock *icsk = inet_csk(sk);
282 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker3393da82007-09-25 22:40:44 -0700283 long tstamp = dccp_timestamp();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700284
285 /* Stop the REQUEST timer */
286 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
287 BUG_TRAP(sk->sk_send_head != NULL);
288 __kfree_skb(sk->sk_send_head);
289 sk->sk_send_head = NULL;
290
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300291 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
292 dp->dccps_awl, dp->dccps_awh)) {
293 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
294 "P.ackno=%llu, S.AWH=%llu \n",
295 (unsigned long long)dp->dccps_awl,
296 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
297 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700298 goto out_invalid_packet;
299 }
300
Andrea Bittauafe00252006-03-20 17:43:56 -0800301 if (dccp_parse_options(sk, skb))
302 goto out_invalid_packet;
303
Gerrit Renker3393da82007-09-25 22:40:44 -0700304 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
305 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
306 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
307 dp->dccps_options_received.dccpor_timestamp_echo));
Gerrit Renker89560b52007-03-20 15:27:17 -0300308
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900309 if (dccp_msk(sk)->dccpms_send_ack_vector &&
310 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
311 DCCP_SKB_CB(skb)->dccpd_seq,
312 DCCP_ACKVEC_STATE_RECEIVED))
313 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800314
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700315 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300316 dccp_update_gsr(sk, dp->dccps_isr);
317 /*
318 * SWL and AWL are initially adjusted so that they are not less than
319 * the initial Sequence Numbers received and sent, respectively:
320 * SWL := max(GSR + 1 - floor(W/4), ISR),
321 * AWL := max(GSS - W' + 1, ISS).
322 * These adjustments MUST be applied only at the beginning of the
323 * connection.
324 *
325 * AWL was adjusted in dccp_v4_connect -acme
326 */
327 dccp_set_seqno(&dp->dccps_swl,
328 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700329
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800330 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700331
332 /*
333 * Step 10: Process REQUEST state (second part)
334 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300335 * / * If we get here, P is a valid Response from the
336 * server (see Step 4), and we should move to
337 * PARTOPEN state. PARTOPEN means send an Ack,
338 * don't send Data packets, retransmit Acks
339 * periodically, and always include any Init Cookie
340 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700341 * S.state := PARTOPEN
342 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200343 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300344 * / * Step 12 will send the Ack completing the
345 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700346 */
347 dccp_set_state(sk, DCCP_PARTOPEN);
348
349 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800350 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700351
352 if (!sock_flag(sk, SOCK_DEAD)) {
353 sk->sk_state_change(sk);
354 sk_wake_async(sk, 0, POLL_OUT);
355 }
356
357 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
358 icsk->icsk_accept_queue.rskq_defer_accept) {
359 /* Save one ACK. Data will be ready after
360 * several ticks, if write_pending is set.
361 *
362 * It may be deleted, but with this feature tcpdumps
363 * look so _wonderfully_ clever, that I was not able
364 * to stand against the temptation 8) --ANK
365 */
366 /*
367 * OK, in DCCP we can as well do a similar trick, its
368 * even in the draft, but there is no need for us to
369 * schedule an ack here, as dccp_sendmsg does this for
370 * us, also stated in the draft. -acme
371 */
372 __kfree_skb(skb);
373 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200374 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700375 dccp_send_ack(sk);
376 return -1;
377 }
378
379out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700380 /* dccp_v4_do_rcv will send a reset */
381 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200382 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700383}
384
385static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
386 struct sk_buff *skb,
387 const struct dccp_hdr *dh,
388 const unsigned len)
389{
390 int queued = 0;
391
392 switch (dh->dccph_type) {
393 case DCCP_PKT_RESET:
394 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
395 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700396 case DCCP_PKT_DATA:
397 if (sk->sk_state == DCCP_RESPOND)
398 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700399 case DCCP_PKT_DATAACK:
400 case DCCP_PKT_ACK:
401 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300402 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
403 * here but only if we haven't used the DELACK timer for
404 * something else, like sending a delayed ack for a TIMESTAMP
405 * echo, etc, for now were not clearing it, sending an extra
406 * ACK when there is nothing else to do in DELACK is not a big
407 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700408 */
409
410 /* Stop the PARTOPEN timer */
411 if (sk->sk_state == DCCP_PARTOPEN)
412 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
413
414 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
415 dccp_set_state(sk, DCCP_OPEN);
416
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700417 if (dh->dccph_type == DCCP_PKT_DATAACK ||
418 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800419 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300420 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800421 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700422 }
423 break;
424 }
425
426 return queued;
427}
428
429int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
430 struct dccp_hdr *dh, unsigned len)
431{
432 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700433 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700434 const int old_state = sk->sk_state;
435 int queued = 0;
436
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300437 /*
438 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300439 *
440 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200441 * If P.type == Request or P contains a valid Init Cookie option,
442 * (* Must scan the packet's options to check for Init
443 * Cookies. Only Init Cookies are processed here,
444 * however; other options are processed in Step 8. This
445 * scan need only be performed if the endpoint uses Init
446 * Cookies *)
447 * (* Generate a new socket and switch to that socket *)
448 * Set S := new socket for this port pair
449 * S.state = RESPOND
450 * Choose S.ISS (initial seqno) or set from Init Cookies
451 * Initialize S.GAR := S.ISS
452 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
453 * Cookies Continue with S.state == RESPOND
454 * (* A Response packet will be generated in Step 11 *)
455 * Otherwise,
456 * Generate Reset(No Connection) unless P.type == Reset
457 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300458 */
459 if (sk->sk_state == DCCP_LISTEN) {
460 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800461 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
462 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300463 return 1;
464
465 /* FIXME: do congestion control initialization */
466 goto discard;
467 }
468 if (dh->dccph_type == DCCP_PKT_RESET)
469 goto discard;
470
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700471 /* Caller (dccp_v4_do_rcv) will send Reset */
472 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300473 return 1;
474 }
475
476 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700477 if (dccp_check_seqno(sk, skb))
478 goto discard;
479
480 /*
481 * Step 8: Process options and mark acknowledgeable
482 */
483 if (dccp_parse_options(sk, skb))
484 goto discard;
485
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700486 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700487 dccp_event_ack_recv(sk, skb);
488
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200489 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700490 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200491 DCCP_SKB_CB(skb)->dccpd_seq,
492 DCCP_ACKVEC_STATE_RECEIVED))
493 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800494
Gerrit Renker151a9932007-03-07 12:53:48 -0800495 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
496 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700497 }
498
499 /*
500 * Step 9: Process Reset
501 * If P.type == Reset,
502 * Tear down connection
503 * S.state := TIMEWAIT
504 * Set TIMEWAIT timer
505 * Drop packet and return
506 */
507 if (dh->dccph_type == DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300508 /*
509 * Queue the equivalent of TCP fin so that dccp_recvmsg
510 * exits the loop
511 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700512 dccp_fin(sk, skb);
513 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
514 return 0;
515 /*
516 * Step 7: Check for unexpected packet types
517 * If (S.is_server and P.type == CloseReq)
518 * or (S.is_server and P.type == Response)
519 * or (S.is_client and P.type == Request)
520 * or (S.state == RESPOND and P.type == Data),
521 * Send Sync packet acknowledging P.seqno
522 * Drop packet and return
523 */
524 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300525 (dh->dccph_type == DCCP_PKT_RESPONSE ||
526 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700527 (dp->dccps_role == DCCP_ROLE_CLIENT &&
528 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300529 (sk->sk_state == DCCP_RESPOND &&
530 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700531 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700532 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700533 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
534 dccp_rcv_closereq(sk, skb);
535 goto discard;
536 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
537 dccp_rcv_close(sk, skb);
538 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700539 }
540
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300541 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700542 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300543 goto discard;
544 }
545
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700546 switch (sk->sk_state) {
547 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700548 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700549 return 1;
550
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700551 case DCCP_REQUESTING:
552 /* FIXME: do congestion control initialization */
553
554 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
555 if (queued >= 0)
556 return queued;
557
558 __kfree_skb(skb);
559 return 0;
560
561 case DCCP_RESPOND:
562 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300563 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
564 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700565 break;
566 }
567
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300568 if (dh->dccph_type == DCCP_PKT_ACK ||
569 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700570 switch (old_state) {
571 case DCCP_PARTOPEN:
572 sk->sk_state_change(sk);
573 sk_wake_async(sk, 0, POLL_OUT);
574 break;
575 }
576 }
577
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200578 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700579discard:
580 __kfree_skb(skb);
581 }
582 return 0;
583}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800584
585EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
Gerrit Renker4712a792007-03-20 15:23:18 -0300586
587/**
Gerrit Renker3393da82007-09-25 22:40:44 -0700588 * dccp_sample_rtt - Validate and finalise computation of RTT sample
589 * @delta: number of microseconds between packet and acknowledgment
590 * The routine is kept generic to work in different contexts. It should be
591 * called immediately when the ACK used for the RTT sample arrives.
Gerrit Renker4712a792007-03-20 15:23:18 -0300592 */
Gerrit Renker3393da82007-09-25 22:40:44 -0700593u32 dccp_sample_rtt(struct sock *sk, long delta)
Gerrit Renker4712a792007-03-20 15:23:18 -0300594{
Gerrit Renker3393da82007-09-25 22:40:44 -0700595 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
596 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
Gerrit Renker4712a792007-03-20 15:23:18 -0300597
598 if (unlikely(delta <= 0)) {
Gerrit Renker3393da82007-09-25 22:40:44 -0700599 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300600 return DCCP_SANE_RTT_MIN;
601 }
Gerrit Renker3393da82007-09-25 22:40:44 -0700602 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
603 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300604 return DCCP_SANE_RTT_MAX;
605 }
606
607 return delta;
608}
609
610EXPORT_SYMBOL_GPL(dccp_sample_rtt);