blob: 19d7e1dbd87e99a98f463e944a6f20f052fd9856 [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);
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -070071 u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
72 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070073
74 /*
75 * Step 5: Prepare sequence numbers for Sync
76 * If P.type == Sync or P.type == SyncAck,
77 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
78 * / * P is valid, so update sequence number variables
79 * accordingly. After this update, P will pass the tests
80 * in Step 6. A SyncAck is generated if necessary in
81 * Step 15 * /
82 * Update S.GSR, S.SWL, S.SWH
83 * Otherwise,
84 * Drop packet and return
85 */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -020086 if (dh->dccph_type == DCCP_PKT_SYNC ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070087 dh->dccph_type == DCCP_PKT_SYNCACK) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -070088 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
89 dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
90 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070091 else
92 return -1;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -030093 }
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +090094
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070095 /*
96 * Step 6: Check sequence numbers
97 * Let LSWL = S.SWL and LAWL = S.AWL
98 * If P.type == CloseReq or P.type == Close or P.type == Reset,
99 * LSWL := S.GSR + 1, LAWL := S.GAR
100 * If LSWL <= P.seqno <= S.SWH
101 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
102 * Update S.GSR, S.SWL, S.SWH
103 * If P.type != Sync,
104 * Update S.GAR
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700105 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300106 lswl = dp->dccps_swl;
107 lawl = dp->dccps_awl;
108
109 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300110 dh->dccph_type == DCCP_PKT_CLOSE ||
111 dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700112 lswl = ADD48(dp->dccps_gsr, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700113 lawl = dp->dccps_gar;
114 }
115
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700116 if (between48(seqno, lswl, dp->dccps_swh) &&
117 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
118 between48(ackno, lawl, dp->dccps_awh))) {
119 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700120
121 if (dh->dccph_type != DCCP_PKT_SYNC &&
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700122 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
123 dp->dccps_gar = ackno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700124 } else {
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300125 unsigned long now = jiffies;
126 /*
127 * Step 6: Check sequence numbers
128 * Otherwise,
129 * If P.type == Reset,
130 * Send Sync packet acknowledging S.GSR
131 * Otherwise,
132 * Send Sync packet acknowledging P.seqno
133 * Drop packet and return
134 *
135 * These Syncs are rate-limited as per RFC 4340, 7.5.4:
136 * at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
137 */
138 if (time_before(now, (dp->dccps_rate_last +
139 sysctl_dccp_sync_ratelimit)))
140 return 0;
141
Gerrit Renker59348b12006-11-20 18:39:23 -0200142 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
143 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
144 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
145 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700146 (unsigned long long) lswl, (unsigned long long) seqno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200147 (unsigned long long) dp->dccps_swh,
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700148 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
149 : "exists",
150 (unsigned long long) lawl, (unsigned long long) ackno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200151 (unsigned long long) dp->dccps_awh);
Gerrit Renkera94f0f92007-09-26 11:31:49 -0300152
153 dp->dccps_rate_last = now;
154
Gerrit Renkere155d762007-09-25 22:41:56 -0700155 if (dh->dccph_type == DCCP_PKT_RESET)
156 seqno = dp->dccps_gsr;
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700157 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700158 return -1;
159 }
160
161 return 0;
162}
163
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800164static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
165 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700166{
167 struct dccp_sock *dp = dccp_sk(sk);
168
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700169 switch (dccp_hdr(skb)->dccph_type) {
170 case DCCP_PKT_DATAACK:
171 case DCCP_PKT_DATA:
172 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300173 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
174 * option if it is.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700175 */
176 __skb_pull(skb, dh->dccph_doff * 4);
177 __skb_queue_tail(&sk->sk_receive_queue, skb);
178 skb_set_owner_r(skb, sk);
179 sk->sk_data_ready(sk, 0);
180 return 0;
181 case DCCP_PKT_ACK:
182 goto discard;
183 case DCCP_PKT_RESET:
184 /*
185 * Step 9: Process Reset
186 * If P.type == Reset,
187 * Tear down connection
188 * S.state := TIMEWAIT
189 * Set TIMEWAIT timer
190 * Drop packet and return
191 */
192 dccp_fin(sk, skb);
193 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
194 return 0;
195 case DCCP_PKT_CLOSEREQ:
196 dccp_rcv_closereq(sk, skb);
197 goto discard;
198 case DCCP_PKT_CLOSE:
199 dccp_rcv_close(sk, skb);
200 return 0;
201 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200202 /* Step 7
203 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700204 * or (S.is_client and P.type == Request)
205 * or (S.state >= OPEN and P.type == Request
206 * and P.seqno >= S.OSR)
207 * or (S.state >= OPEN and P.type == Response
208 * and P.seqno >= S.OSR)
209 * or (S.state == RESPOND and P.type == Data),
210 * Send Sync packet acknowledging P.seqno
211 * Drop packet and return
212 */
213 if (dp->dccps_role != DCCP_ROLE_LISTEN)
214 goto send_sync;
215 goto check_seq;
216 case DCCP_PKT_RESPONSE:
217 if (dp->dccps_role != DCCP_ROLE_CLIENT)
218 goto send_sync;
219check_seq:
Gerrit Renker8d13bf92007-03-20 13:08:19 -0300220 if (dccp_delta_seqno(dp->dccps_osr,
221 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700222send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300223 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
224 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700225 }
226 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300227 case DCCP_PKT_SYNC:
228 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
229 DCCP_PKT_SYNCACK);
230 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700231 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300232 *
233 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
234 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700235 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300236 */
237 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700238 }
239
240 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
241discard:
242 __kfree_skb(skb);
243 return 0;
244}
245
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800246int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
247 const struct dccp_hdr *dh, const unsigned len)
248{
249 struct dccp_sock *dp = dccp_sk(sk);
250
251 if (dccp_check_seqno(sk, skb))
252 goto discard;
253
254 if (dccp_parse_options(sk, skb))
255 goto discard;
256
257 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
258 dccp_event_ack_recv(sk, skb);
259
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800260 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800261 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
262 DCCP_SKB_CB(skb)->dccpd_seq,
263 DCCP_ACKVEC_STATE_RECEIVED))
264 goto discard;
265
Gerrit Renker151a9932007-03-07 12:53:48 -0800266 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
267 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800268
269 return __dccp_rcv_established(sk, skb, dh, len);
270discard:
271 __kfree_skb(skb);
272 return 0;
273}
274
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800275EXPORT_SYMBOL_GPL(dccp_rcv_established);
276
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700277static int dccp_rcv_request_sent_state_process(struct sock *sk,
278 struct sk_buff *skb,
279 const struct dccp_hdr *dh,
280 const unsigned len)
281{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200282 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700283 * Step 4: Prepare sequence numbers in REQUEST
284 * If S.state == REQUEST,
285 * If (P.type == Response or P.type == Reset)
286 * and S.AWL <= P.ackno <= S.AWH,
287 * / * Set sequence number variables corresponding to the
288 * other endpoint, so P will pass the tests in Step 6 * /
289 * Set S.GSR, S.ISR, S.SWL, S.SWH
290 * / * Response processing continues in Step 10; Reset
291 * processing continues in Step 9 * /
292 */
293 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
294 const struct inet_connection_sock *icsk = inet_csk(sk);
295 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker3393da82007-09-25 22:40:44 -0700296 long tstamp = dccp_timestamp();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700297
298 /* Stop the REQUEST timer */
299 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
300 BUG_TRAP(sk->sk_send_head != NULL);
301 __kfree_skb(sk->sk_send_head);
302 sk->sk_send_head = NULL;
303
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300304 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
305 dp->dccps_awl, dp->dccps_awh)) {
306 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
307 "P.ackno=%llu, S.AWH=%llu \n",
308 (unsigned long long)dp->dccps_awl,
309 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
310 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700311 goto out_invalid_packet;
312 }
313
Andrea Bittauafe00252006-03-20 17:43:56 -0800314 if (dccp_parse_options(sk, skb))
315 goto out_invalid_packet;
316
Gerrit Renker3393da82007-09-25 22:40:44 -0700317 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
318 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
319 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
320 dp->dccps_options_received.dccpor_timestamp_echo));
Gerrit Renker89560b52007-03-20 15:27:17 -0300321
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900322 if (dccp_msk(sk)->dccpms_send_ack_vector &&
323 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
324 DCCP_SKB_CB(skb)->dccpd_seq,
325 DCCP_ACKVEC_STATE_RECEIVED))
326 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800327
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700328 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300329 dccp_update_gsr(sk, dp->dccps_isr);
330 /*
331 * SWL and AWL are initially adjusted so that they are not less than
332 * the initial Sequence Numbers received and sent, respectively:
333 * SWL := max(GSR + 1 - floor(W/4), ISR),
334 * AWL := max(GSS - W' + 1, ISS).
335 * These adjustments MUST be applied only at the beginning of the
336 * connection.
337 *
338 * AWL was adjusted in dccp_v4_connect -acme
339 */
340 dccp_set_seqno(&dp->dccps_swl,
341 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700342
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800343 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700344
345 /*
346 * Step 10: Process REQUEST state (second part)
347 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300348 * / * If we get here, P is a valid Response from the
349 * server (see Step 4), and we should move to
350 * PARTOPEN state. PARTOPEN means send an Ack,
351 * don't send Data packets, retransmit Acks
352 * periodically, and always include any Init Cookie
353 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700354 * S.state := PARTOPEN
355 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200356 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300357 * / * Step 12 will send the Ack completing the
358 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700359 */
360 dccp_set_state(sk, DCCP_PARTOPEN);
361
362 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800363 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700364
365 if (!sock_flag(sk, SOCK_DEAD)) {
366 sk->sk_state_change(sk);
367 sk_wake_async(sk, 0, POLL_OUT);
368 }
369
370 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
371 icsk->icsk_accept_queue.rskq_defer_accept) {
372 /* Save one ACK. Data will be ready after
373 * several ticks, if write_pending is set.
374 *
375 * It may be deleted, but with this feature tcpdumps
376 * look so _wonderfully_ clever, that I was not able
377 * to stand against the temptation 8) --ANK
378 */
379 /*
380 * OK, in DCCP we can as well do a similar trick, its
381 * even in the draft, but there is no need for us to
382 * schedule an ack here, as dccp_sendmsg does this for
383 * us, also stated in the draft. -acme
384 */
385 __kfree_skb(skb);
386 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200387 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700388 dccp_send_ack(sk);
389 return -1;
390 }
391
392out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700393 /* dccp_v4_do_rcv will send a reset */
394 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200395 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700396}
397
398static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
399 struct sk_buff *skb,
400 const struct dccp_hdr *dh,
401 const unsigned len)
402{
403 int queued = 0;
404
405 switch (dh->dccph_type) {
406 case DCCP_PKT_RESET:
407 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
408 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700409 case DCCP_PKT_DATA:
410 if (sk->sk_state == DCCP_RESPOND)
411 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700412 case DCCP_PKT_DATAACK:
413 case DCCP_PKT_ACK:
414 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300415 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
416 * here but only if we haven't used the DELACK timer for
417 * something else, like sending a delayed ack for a TIMESTAMP
418 * echo, etc, for now were not clearing it, sending an extra
419 * ACK when there is nothing else to do in DELACK is not a big
420 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700421 */
422
423 /* Stop the PARTOPEN timer */
424 if (sk->sk_state == DCCP_PARTOPEN)
425 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
426
427 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
428 dccp_set_state(sk, DCCP_OPEN);
429
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700430 if (dh->dccph_type == DCCP_PKT_DATAACK ||
431 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800432 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300433 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800434 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700435 }
436 break;
437 }
438
439 return queued;
440}
441
442int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
443 struct dccp_hdr *dh, unsigned len)
444{
445 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700446 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700447 const int old_state = sk->sk_state;
448 int queued = 0;
449
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300450 /*
451 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300452 *
453 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200454 * If P.type == Request or P contains a valid Init Cookie option,
455 * (* Must scan the packet's options to check for Init
456 * Cookies. Only Init Cookies are processed here,
457 * however; other options are processed in Step 8. This
458 * scan need only be performed if the endpoint uses Init
459 * Cookies *)
460 * (* Generate a new socket and switch to that socket *)
461 * Set S := new socket for this port pair
462 * S.state = RESPOND
463 * Choose S.ISS (initial seqno) or set from Init Cookies
464 * Initialize S.GAR := S.ISS
465 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
466 * Cookies Continue with S.state == RESPOND
467 * (* A Response packet will be generated in Step 11 *)
468 * Otherwise,
469 * Generate Reset(No Connection) unless P.type == Reset
470 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300471 */
472 if (sk->sk_state == DCCP_LISTEN) {
473 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800474 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
475 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300476 return 1;
477
478 /* FIXME: do congestion control initialization */
479 goto discard;
480 }
481 if (dh->dccph_type == DCCP_PKT_RESET)
482 goto discard;
483
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700484 /* Caller (dccp_v4_do_rcv) will send Reset */
485 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300486 return 1;
487 }
488
489 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700490 if (dccp_check_seqno(sk, skb))
491 goto discard;
492
493 /*
494 * Step 8: Process options and mark acknowledgeable
495 */
496 if (dccp_parse_options(sk, skb))
497 goto discard;
498
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700499 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700500 dccp_event_ack_recv(sk, skb);
501
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200502 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700503 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200504 DCCP_SKB_CB(skb)->dccpd_seq,
505 DCCP_ACKVEC_STATE_RECEIVED))
506 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800507
Gerrit Renker151a9932007-03-07 12:53:48 -0800508 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
509 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700510 }
511
512 /*
513 * Step 9: Process Reset
514 * If P.type == Reset,
515 * Tear down connection
516 * S.state := TIMEWAIT
517 * Set TIMEWAIT timer
518 * Drop packet and return
519 */
520 if (dh->dccph_type == DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300521 /*
522 * Queue the equivalent of TCP fin so that dccp_recvmsg
523 * exits the loop
524 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700525 dccp_fin(sk, skb);
526 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
527 return 0;
528 /*
529 * Step 7: Check for unexpected packet types
530 * If (S.is_server and P.type == CloseReq)
531 * or (S.is_server and P.type == Response)
532 * or (S.is_client and P.type == Request)
533 * or (S.state == RESPOND and P.type == Data),
534 * Send Sync packet acknowledging P.seqno
535 * Drop packet and return
536 */
537 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300538 (dh->dccph_type == DCCP_PKT_RESPONSE ||
539 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700540 (dp->dccps_role == DCCP_ROLE_CLIENT &&
541 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300542 (sk->sk_state == DCCP_RESPOND &&
543 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700544 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700545 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700546 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
547 dccp_rcv_closereq(sk, skb);
548 goto discard;
549 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
550 dccp_rcv_close(sk, skb);
551 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700552 }
553
554 switch (sk->sk_state) {
555 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700556 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700557 return 1;
558
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700559 case DCCP_REQUESTING:
560 /* FIXME: do congestion control initialization */
561
562 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
563 if (queued >= 0)
564 return queued;
565
566 __kfree_skb(skb);
567 return 0;
568
569 case DCCP_RESPOND:
570 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300571 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
572 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700573 break;
574 }
575
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300576 if (dh->dccph_type == DCCP_PKT_ACK ||
577 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700578 switch (old_state) {
579 case DCCP_PARTOPEN:
580 sk->sk_state_change(sk);
581 sk_wake_async(sk, 0, POLL_OUT);
582 break;
583 }
Gerrit Renker08831702007-09-26 10:30:05 -0300584 } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
585 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
586 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700587 }
588
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200589 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700590discard:
591 __kfree_skb(skb);
592 }
593 return 0;
594}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800595
596EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
Gerrit Renker4712a792007-03-20 15:23:18 -0300597
598/**
Gerrit Renker3393da82007-09-25 22:40:44 -0700599 * dccp_sample_rtt - Validate and finalise computation of RTT sample
600 * @delta: number of microseconds between packet and acknowledgment
601 * The routine is kept generic to work in different contexts. It should be
602 * called immediately when the ACK used for the RTT sample arrives.
Gerrit Renker4712a792007-03-20 15:23:18 -0300603 */
Gerrit Renker3393da82007-09-25 22:40:44 -0700604u32 dccp_sample_rtt(struct sock *sk, long delta)
Gerrit Renker4712a792007-03-20 15:23:18 -0300605{
Gerrit Renker3393da82007-09-25 22:40:44 -0700606 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
607 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
Gerrit Renker4712a792007-03-20 15:23:18 -0300608
609 if (unlikely(delta <= 0)) {
Gerrit Renker3393da82007-09-25 22:40:44 -0700610 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300611 return DCCP_SANE_RTT_MIN;
612 }
Gerrit Renker3393da82007-09-25 22:40:44 -0700613 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
614 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300615 return DCCP_SANE_RTT_MAX;
616 }
617
618 return delta;
619}
620
621EXPORT_SYMBOL_GPL(dccp_sample_rtt);