blob: 6276b23fc20417daa6e87970408a40c76d7909ba [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
105 * Otherwise,
106 * Send Sync packet acknowledging P.seqno
107 * Drop packet and return
108 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300109 lswl = dp->dccps_swl;
110 lawl = dp->dccps_awl;
111
112 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300113 dh->dccph_type == DCCP_PKT_CLOSE ||
114 dh->dccph_type == DCCP_PKT_RESET) {
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700115 lswl = ADD48(dp->dccps_gsr, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700116 lawl = dp->dccps_gar;
117 }
118
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700119 if (between48(seqno, lswl, dp->dccps_swh) &&
120 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
121 between48(ackno, lawl, dp->dccps_awh))) {
122 dccp_update_gsr(sk, seqno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700123
124 if (dh->dccph_type != DCCP_PKT_SYNC &&
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700125 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
126 dp->dccps_gar = ackno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700127 } else {
Gerrit Renker59348b12006-11-20 18:39:23 -0200128 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
129 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
130 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
131 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700132 (unsigned long long) lswl, (unsigned long long) seqno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200133 (unsigned long long) dp->dccps_swh,
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700134 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
135 : "exists",
136 (unsigned long long) lawl, (unsigned long long) ackno,
Gerrit Renker59348b12006-11-20 18:39:23 -0200137 (unsigned long long) dp->dccps_awh);
Gerrit Renkercbe1f5f2007-09-25 22:41:19 -0700138 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700139 return -1;
140 }
141
142 return 0;
143}
144
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800145static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
146 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700147{
148 struct dccp_sock *dp = dccp_sk(sk);
149
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700150 switch (dccp_hdr(skb)->dccph_type) {
151 case DCCP_PKT_DATAACK:
152 case DCCP_PKT_DATA:
153 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300154 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
155 * option if it is.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700156 */
157 __skb_pull(skb, dh->dccph_doff * 4);
158 __skb_queue_tail(&sk->sk_receive_queue, skb);
159 skb_set_owner_r(skb, sk);
160 sk->sk_data_ready(sk, 0);
161 return 0;
162 case DCCP_PKT_ACK:
163 goto discard;
164 case DCCP_PKT_RESET:
165 /*
166 * Step 9: Process Reset
167 * If P.type == Reset,
168 * Tear down connection
169 * S.state := TIMEWAIT
170 * Set TIMEWAIT timer
171 * Drop packet and return
172 */
173 dccp_fin(sk, skb);
174 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
175 return 0;
176 case DCCP_PKT_CLOSEREQ:
177 dccp_rcv_closereq(sk, skb);
178 goto discard;
179 case DCCP_PKT_CLOSE:
180 dccp_rcv_close(sk, skb);
181 return 0;
182 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200183 /* Step 7
184 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700185 * or (S.is_client and P.type == Request)
186 * or (S.state >= OPEN and P.type == Request
187 * and P.seqno >= S.OSR)
188 * or (S.state >= OPEN and P.type == Response
189 * and P.seqno >= S.OSR)
190 * or (S.state == RESPOND and P.type == Data),
191 * Send Sync packet acknowledging P.seqno
192 * Drop packet and return
193 */
194 if (dp->dccps_role != DCCP_ROLE_LISTEN)
195 goto send_sync;
196 goto check_seq;
197 case DCCP_PKT_RESPONSE:
198 if (dp->dccps_role != DCCP_ROLE_CLIENT)
199 goto send_sync;
200check_seq:
Gerrit Renker8d13bf92007-03-20 13:08:19 -0300201 if (dccp_delta_seqno(dp->dccps_osr,
202 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700203send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300204 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
205 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700206 }
207 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300208 case DCCP_PKT_SYNC:
209 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
210 DCCP_PKT_SYNCACK);
211 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700212 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300213 *
214 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
215 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700216 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300217 */
218 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700219 }
220
221 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
222discard:
223 __kfree_skb(skb);
224 return 0;
225}
226
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800227int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
228 const struct dccp_hdr *dh, const unsigned len)
229{
230 struct dccp_sock *dp = dccp_sk(sk);
231
232 if (dccp_check_seqno(sk, skb))
233 goto discard;
234
235 if (dccp_parse_options(sk, skb))
236 goto discard;
237
238 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
239 dccp_event_ack_recv(sk, skb);
240
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800241 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800242 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
243 DCCP_SKB_CB(skb)->dccpd_seq,
244 DCCP_ACKVEC_STATE_RECEIVED))
245 goto discard;
246
Gerrit Renker151a9932007-03-07 12:53:48 -0800247 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
248 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800249
250 return __dccp_rcv_established(sk, skb, dh, len);
251discard:
252 __kfree_skb(skb);
253 return 0;
254}
255
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800256EXPORT_SYMBOL_GPL(dccp_rcv_established);
257
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700258static int dccp_rcv_request_sent_state_process(struct sock *sk,
259 struct sk_buff *skb,
260 const struct dccp_hdr *dh,
261 const unsigned len)
262{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200263 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700264 * Step 4: Prepare sequence numbers in REQUEST
265 * If S.state == REQUEST,
266 * If (P.type == Response or P.type == Reset)
267 * and S.AWL <= P.ackno <= S.AWH,
268 * / * Set sequence number variables corresponding to the
269 * other endpoint, so P will pass the tests in Step 6 * /
270 * Set S.GSR, S.ISR, S.SWL, S.SWH
271 * / * Response processing continues in Step 10; Reset
272 * processing continues in Step 9 * /
273 */
274 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
275 const struct inet_connection_sock *icsk = inet_csk(sk);
276 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker3393da82007-09-25 22:40:44 -0700277 long tstamp = dccp_timestamp();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700278
279 /* Stop the REQUEST timer */
280 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
281 BUG_TRAP(sk->sk_send_head != NULL);
282 __kfree_skb(sk->sk_send_head);
283 sk->sk_send_head = NULL;
284
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300285 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
286 dp->dccps_awl, dp->dccps_awh)) {
287 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
288 "P.ackno=%llu, S.AWH=%llu \n",
289 (unsigned long long)dp->dccps_awl,
290 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
291 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700292 goto out_invalid_packet;
293 }
294
Andrea Bittauafe00252006-03-20 17:43:56 -0800295 if (dccp_parse_options(sk, skb))
296 goto out_invalid_packet;
297
Gerrit Renker3393da82007-09-25 22:40:44 -0700298 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
299 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
300 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
301 dp->dccps_options_received.dccpor_timestamp_echo));
Gerrit Renker89560b52007-03-20 15:27:17 -0300302
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900303 if (dccp_msk(sk)->dccpms_send_ack_vector &&
304 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
305 DCCP_SKB_CB(skb)->dccpd_seq,
306 DCCP_ACKVEC_STATE_RECEIVED))
307 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800308
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700309 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300310 dccp_update_gsr(sk, dp->dccps_isr);
311 /*
312 * SWL and AWL are initially adjusted so that they are not less than
313 * the initial Sequence Numbers received and sent, respectively:
314 * SWL := max(GSR + 1 - floor(W/4), ISR),
315 * AWL := max(GSS - W' + 1, ISS).
316 * These adjustments MUST be applied only at the beginning of the
317 * connection.
318 *
319 * AWL was adjusted in dccp_v4_connect -acme
320 */
321 dccp_set_seqno(&dp->dccps_swl,
322 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700323
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800324 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700325
326 /*
327 * Step 10: Process REQUEST state (second part)
328 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300329 * / * If we get here, P is a valid Response from the
330 * server (see Step 4), and we should move to
331 * PARTOPEN state. PARTOPEN means send an Ack,
332 * don't send Data packets, retransmit Acks
333 * periodically, and always include any Init Cookie
334 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700335 * S.state := PARTOPEN
336 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200337 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300338 * / * Step 12 will send the Ack completing the
339 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700340 */
341 dccp_set_state(sk, DCCP_PARTOPEN);
342
343 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800344 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700345
346 if (!sock_flag(sk, SOCK_DEAD)) {
347 sk->sk_state_change(sk);
348 sk_wake_async(sk, 0, POLL_OUT);
349 }
350
351 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
352 icsk->icsk_accept_queue.rskq_defer_accept) {
353 /* Save one ACK. Data will be ready after
354 * several ticks, if write_pending is set.
355 *
356 * It may be deleted, but with this feature tcpdumps
357 * look so _wonderfully_ clever, that I was not able
358 * to stand against the temptation 8) --ANK
359 */
360 /*
361 * OK, in DCCP we can as well do a similar trick, its
362 * even in the draft, but there is no need for us to
363 * schedule an ack here, as dccp_sendmsg does this for
364 * us, also stated in the draft. -acme
365 */
366 __kfree_skb(skb);
367 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200368 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700369 dccp_send_ack(sk);
370 return -1;
371 }
372
373out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700374 /* dccp_v4_do_rcv will send a reset */
375 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200376 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700377}
378
379static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
380 struct sk_buff *skb,
381 const struct dccp_hdr *dh,
382 const unsigned len)
383{
384 int queued = 0;
385
386 switch (dh->dccph_type) {
387 case DCCP_PKT_RESET:
388 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
389 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700390 case DCCP_PKT_DATA:
391 if (sk->sk_state == DCCP_RESPOND)
392 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700393 case DCCP_PKT_DATAACK:
394 case DCCP_PKT_ACK:
395 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300396 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
397 * here but only if we haven't used the DELACK timer for
398 * something else, like sending a delayed ack for a TIMESTAMP
399 * echo, etc, for now were not clearing it, sending an extra
400 * ACK when there is nothing else to do in DELACK is not a big
401 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700402 */
403
404 /* Stop the PARTOPEN timer */
405 if (sk->sk_state == DCCP_PARTOPEN)
406 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
407
408 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
409 dccp_set_state(sk, DCCP_OPEN);
410
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700411 if (dh->dccph_type == DCCP_PKT_DATAACK ||
412 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800413 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300414 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800415 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700416 }
417 break;
418 }
419
420 return queued;
421}
422
423int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
424 struct dccp_hdr *dh, unsigned len)
425{
426 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700427 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700428 const int old_state = sk->sk_state;
429 int queued = 0;
430
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300431 /*
432 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300433 *
434 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200435 * If P.type == Request or P contains a valid Init Cookie option,
436 * (* Must scan the packet's options to check for Init
437 * Cookies. Only Init Cookies are processed here,
438 * however; other options are processed in Step 8. This
439 * scan need only be performed if the endpoint uses Init
440 * Cookies *)
441 * (* Generate a new socket and switch to that socket *)
442 * Set S := new socket for this port pair
443 * S.state = RESPOND
444 * Choose S.ISS (initial seqno) or set from Init Cookies
445 * Initialize S.GAR := S.ISS
446 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
447 * Cookies Continue with S.state == RESPOND
448 * (* A Response packet will be generated in Step 11 *)
449 * Otherwise,
450 * Generate Reset(No Connection) unless P.type == Reset
451 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300452 */
453 if (sk->sk_state == DCCP_LISTEN) {
454 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800455 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
456 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300457 return 1;
458
459 /* FIXME: do congestion control initialization */
460 goto discard;
461 }
462 if (dh->dccph_type == DCCP_PKT_RESET)
463 goto discard;
464
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700465 /* Caller (dccp_v4_do_rcv) will send Reset */
466 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300467 return 1;
468 }
469
470 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700471 if (dccp_check_seqno(sk, skb))
472 goto discard;
473
474 /*
475 * Step 8: Process options and mark acknowledgeable
476 */
477 if (dccp_parse_options(sk, skb))
478 goto discard;
479
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700480 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700481 dccp_event_ack_recv(sk, skb);
482
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200483 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700484 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200485 DCCP_SKB_CB(skb)->dccpd_seq,
486 DCCP_ACKVEC_STATE_RECEIVED))
487 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800488
Gerrit Renker151a9932007-03-07 12:53:48 -0800489 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
490 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700491 }
492
493 /*
494 * Step 9: Process Reset
495 * If P.type == Reset,
496 * Tear down connection
497 * S.state := TIMEWAIT
498 * Set TIMEWAIT timer
499 * Drop packet and return
500 */
501 if (dh->dccph_type == DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300502 /*
503 * Queue the equivalent of TCP fin so that dccp_recvmsg
504 * exits the loop
505 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700506 dccp_fin(sk, skb);
507 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
508 return 0;
509 /*
510 * Step 7: Check for unexpected packet types
511 * If (S.is_server and P.type == CloseReq)
512 * or (S.is_server and P.type == Response)
513 * or (S.is_client and P.type == Request)
514 * or (S.state == RESPOND and P.type == Data),
515 * Send Sync packet acknowledging P.seqno
516 * Drop packet and return
517 */
518 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300519 (dh->dccph_type == DCCP_PKT_RESPONSE ||
520 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700521 (dp->dccps_role == DCCP_ROLE_CLIENT &&
522 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300523 (sk->sk_state == DCCP_RESPOND &&
524 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700525 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700526 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700527 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
528 dccp_rcv_closereq(sk, skb);
529 goto discard;
530 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
531 dccp_rcv_close(sk, skb);
532 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700533 }
534
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300535 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700536 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300537 goto discard;
538 }
539
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700540 switch (sk->sk_state) {
541 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700542 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700543 return 1;
544
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700545 case DCCP_REQUESTING:
546 /* FIXME: do congestion control initialization */
547
548 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
549 if (queued >= 0)
550 return queued;
551
552 __kfree_skb(skb);
553 return 0;
554
555 case DCCP_RESPOND:
556 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300557 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
558 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700559 break;
560 }
561
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300562 if (dh->dccph_type == DCCP_PKT_ACK ||
563 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700564 switch (old_state) {
565 case DCCP_PARTOPEN:
566 sk->sk_state_change(sk);
567 sk_wake_async(sk, 0, POLL_OUT);
568 break;
569 }
570 }
571
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200572 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700573discard:
574 __kfree_skb(skb);
575 }
576 return 0;
577}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800578
579EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
Gerrit Renker4712a792007-03-20 15:23:18 -0300580
581/**
Gerrit Renker3393da82007-09-25 22:40:44 -0700582 * dccp_sample_rtt - Validate and finalise computation of RTT sample
583 * @delta: number of microseconds between packet and acknowledgment
584 * The routine is kept generic to work in different contexts. It should be
585 * called immediately when the ACK used for the RTT sample arrives.
Gerrit Renker4712a792007-03-20 15:23:18 -0300586 */
Gerrit Renker3393da82007-09-25 22:40:44 -0700587u32 dccp_sample_rtt(struct sock *sk, long delta)
Gerrit Renker4712a792007-03-20 15:23:18 -0300588{
Gerrit Renker3393da82007-09-25 22:40:44 -0700589 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
590 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
Gerrit Renker4712a792007-03-20 15:23:18 -0300591
592 if (unlikely(delta <= 0)) {
Gerrit Renker3393da82007-09-25 22:40:44 -0700593 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300594 return DCCP_SANE_RTT_MIN;
595 }
Gerrit Renker3393da82007-09-25 22:40:44 -0700596 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
597 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
Gerrit Renker4712a792007-03-20 15:23:18 -0300598 return DCCP_SANE_RTT_MAX;
599 }
600
601 return delta;
602}
603
604EXPORT_SYMBOL_GPL(dccp_sample_rtt);