blob: 78b043c458bf95ac78aab62526408e09a72f8f00 [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) &&
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070089 !before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_swl))
90 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
91 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) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700115 lswl = dp->dccps_gsr;
116 dccp_inc_seqno(&lswl);
117 lawl = dp->dccps_gar;
118 }
119
120 if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
121 (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300122 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
123 lawl, dp->dccps_awh))) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700124 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
125
126 if (dh->dccph_type != DCCP_PKT_SYNC &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300127 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
128 DCCP_PKT_WITHOUT_ACK_SEQ))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700129 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
130 } else {
Gerrit Renker59348b12006-11-20 18:39:23 -0200131 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
132 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
133 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
134 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
135 (unsigned long long) lswl,
136 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq,
137 (unsigned long long) dp->dccps_swh,
138 (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900139 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
Gerrit Renker59348b12006-11-20 18:39:23 -0200140 (unsigned long long) lawl,
141 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
142 (unsigned long long) dp->dccps_awh);
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300143 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700144 return -1;
145 }
146
147 return 0;
148}
149
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800150static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
151 const struct dccp_hdr *dh, const unsigned len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700152{
153 struct dccp_sock *dp = dccp_sk(sk);
154
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700155 switch (dccp_hdr(skb)->dccph_type) {
156 case DCCP_PKT_DATAACK:
157 case DCCP_PKT_DATA:
158 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300159 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
160 * option if it is.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700161 */
162 __skb_pull(skb, dh->dccph_doff * 4);
163 __skb_queue_tail(&sk->sk_receive_queue, skb);
164 skb_set_owner_r(skb, sk);
165 sk->sk_data_ready(sk, 0);
166 return 0;
167 case DCCP_PKT_ACK:
168 goto discard;
169 case DCCP_PKT_RESET:
170 /*
171 * Step 9: Process Reset
172 * If P.type == Reset,
173 * Tear down connection
174 * S.state := TIMEWAIT
175 * Set TIMEWAIT timer
176 * Drop packet and return
177 */
178 dccp_fin(sk, skb);
179 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
180 return 0;
181 case DCCP_PKT_CLOSEREQ:
182 dccp_rcv_closereq(sk, skb);
183 goto discard;
184 case DCCP_PKT_CLOSE:
185 dccp_rcv_close(sk, skb);
186 return 0;
187 case DCCP_PKT_REQUEST:
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200188 /* Step 7
189 * or (S.is_server and P.type == Response)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700190 * or (S.is_client and P.type == Request)
191 * or (S.state >= OPEN and P.type == Request
192 * and P.seqno >= S.OSR)
193 * or (S.state >= OPEN and P.type == Response
194 * and P.seqno >= S.OSR)
195 * or (S.state == RESPOND and P.type == Data),
196 * Send Sync packet acknowledging P.seqno
197 * Drop packet and return
198 */
199 if (dp->dccps_role != DCCP_ROLE_LISTEN)
200 goto send_sync;
201 goto check_seq;
202 case DCCP_PKT_RESPONSE:
203 if (dp->dccps_role != DCCP_ROLE_CLIENT)
204 goto send_sync;
205check_seq:
206 if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) {
207send_sync:
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300208 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
209 DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700210 }
211 break;
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300212 case DCCP_PKT_SYNC:
213 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
214 DCCP_PKT_SYNCACK);
215 /*
Gerrit Renker0e64e942006-10-24 16:17:51 -0700216 * From RFC 4340, sec. 5.7
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300217 *
218 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
219 * MAY have non-zero-length application data areas, whose
Gerrit Renker0e64e942006-10-24 16:17:51 -0700220 * contents receivers MUST ignore.
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300221 */
222 goto discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700223 }
224
225 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
226discard:
227 __kfree_skb(skb);
228 return 0;
229}
230
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800231int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
232 const struct dccp_hdr *dh, const unsigned len)
233{
234 struct dccp_sock *dp = dccp_sk(sk);
235
236 if (dccp_check_seqno(sk, skb))
237 goto discard;
238
239 if (dccp_parse_options(sk, skb))
240 goto discard;
241
242 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
243 dccp_event_ack_recv(sk, skb);
244
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800245 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800246 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
247 DCCP_SKB_CB(skb)->dccpd_seq,
248 DCCP_ACKVEC_STATE_RECEIVED))
249 goto discard;
250
Gerrit Renker151a9932007-03-07 12:53:48 -0800251 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
252 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800253
254 return __dccp_rcv_established(sk, skb, dh, len);
255discard:
256 __kfree_skb(skb);
257 return 0;
258}
259
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800260EXPORT_SYMBOL_GPL(dccp_rcv_established);
261
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700262static int dccp_rcv_request_sent_state_process(struct sock *sk,
263 struct sk_buff *skb,
264 const struct dccp_hdr *dh,
265 const unsigned len)
266{
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200267 /*
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700268 * Step 4: Prepare sequence numbers in REQUEST
269 * If S.state == REQUEST,
270 * If (P.type == Response or P.type == Reset)
271 * and S.AWL <= P.ackno <= S.AWH,
272 * / * Set sequence number variables corresponding to the
273 * other endpoint, so P will pass the tests in Step 6 * /
274 * Set S.GSR, S.ISR, S.SWL, S.SWH
275 * / * Response processing continues in Step 10; Reset
276 * processing continues in Step 9 * /
277 */
278 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
279 const struct inet_connection_sock *icsk = inet_csk(sk);
280 struct dccp_sock *dp = dccp_sk(sk);
281
282 /* Stop the REQUEST timer */
283 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
284 BUG_TRAP(sk->sk_send_head != NULL);
285 __kfree_skb(sk->sk_send_head);
286 sk->sk_send_head = NULL;
287
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300288 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
289 dp->dccps_awl, dp->dccps_awh)) {
290 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
291 "P.ackno=%llu, S.AWH=%llu \n",
292 (unsigned long long)dp->dccps_awl,
293 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
294 (unsigned long long)dp->dccps_awh);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700295 goto out_invalid_packet;
296 }
297
Andrea Bittauafe00252006-03-20 17:43:56 -0800298 if (dccp_parse_options(sk, skb))
299 goto out_invalid_packet;
300
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900301 if (dccp_msk(sk)->dccpms_send_ack_vector &&
302 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
303 DCCP_SKB_CB(skb)->dccpd_seq,
304 DCCP_ACKVEC_STATE_RECEIVED))
305 goto out_invalid_packet; /* FIXME: change error code */
Andrea Bittau9e377202006-01-03 14:25:49 -0800306
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300308 dccp_update_gsr(sk, dp->dccps_isr);
309 /*
310 * SWL and AWL are initially adjusted so that they are not less than
311 * the initial Sequence Numbers received and sent, respectively:
312 * SWL := max(GSR + 1 - floor(W/4), ISR),
313 * AWL := max(GSS - W' + 1, ISS).
314 * These adjustments MUST be applied only at the beginning of the
315 * connection.
316 *
317 * AWL was adjusted in dccp_v4_connect -acme
318 */
319 dccp_set_seqno(&dp->dccps_swl,
320 max48(dp->dccps_swl, dp->dccps_isr));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700321
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800322 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700323
324 /*
325 * Step 10: Process REQUEST state (second part)
326 * If S.state == REQUEST,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300327 * / * If we get here, P is a valid Response from the
328 * server (see Step 4), and we should move to
329 * PARTOPEN state. PARTOPEN means send an Ack,
330 * don't send Data packets, retransmit Acks
331 * periodically, and always include any Init Cookie
332 * from the Response * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700333 * S.state := PARTOPEN
334 * Set PARTOPEN timer
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200335 * Continue with S.state == PARTOPEN
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300336 * / * Step 12 will send the Ack completing the
337 * three-way handshake * /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700338 */
339 dccp_set_state(sk, DCCP_PARTOPEN);
340
341 /* Make sure socket is routed, for correct metrics. */
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800342 icsk->icsk_af_ops->rebuild_header(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700343
344 if (!sock_flag(sk, SOCK_DEAD)) {
345 sk->sk_state_change(sk);
346 sk_wake_async(sk, 0, POLL_OUT);
347 }
348
349 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
350 icsk->icsk_accept_queue.rskq_defer_accept) {
351 /* Save one ACK. Data will be ready after
352 * several ticks, if write_pending is set.
353 *
354 * It may be deleted, but with this feature tcpdumps
355 * look so _wonderfully_ clever, that I was not able
356 * to stand against the temptation 8) --ANK
357 */
358 /*
359 * OK, in DCCP we can as well do a similar trick, its
360 * even in the draft, but there is no need for us to
361 * schedule an ack here, as dccp_sendmsg does this for
362 * us, also stated in the draft. -acme
363 */
364 __kfree_skb(skb);
365 return 0;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200366 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700367 dccp_send_ack(sk);
368 return -1;
369 }
370
371out_invalid_packet:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700372 /* dccp_v4_do_rcv will send a reset */
373 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200374 return 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700375}
376
377static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
378 struct sk_buff *skb,
379 const struct dccp_hdr *dh,
380 const unsigned len)
381{
382 int queued = 0;
383
384 switch (dh->dccph_type) {
385 case DCCP_PKT_RESET:
386 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
387 break;
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700388 case DCCP_PKT_DATA:
389 if (sk->sk_state == DCCP_RESPOND)
390 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700391 case DCCP_PKT_DATAACK:
392 case DCCP_PKT_ACK:
393 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300394 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
395 * here but only if we haven't used the DELACK timer for
396 * something else, like sending a delayed ack for a TIMESTAMP
397 * echo, etc, for now were not clearing it, sending an extra
398 * ACK when there is nothing else to do in DELACK is not a big
399 * deal after all.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700400 */
401
402 /* Stop the PARTOPEN timer */
403 if (sk->sk_state == DCCP_PARTOPEN)
404 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
405
406 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
407 dccp_set_state(sk, DCCP_OPEN);
408
Arnaldo Carvalho de Melo2a9bc9b2005-10-10 21:25:00 -0700409 if (dh->dccph_type == DCCP_PKT_DATAACK ||
410 dh->dccph_type == DCCP_PKT_DATA) {
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800411 __dccp_rcv_established(sk, skb, dh, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300412 queued = 1; /* packet was queued
Andrea Bittau709dd3a2006-01-03 14:25:17 -0800413 (by __dccp_rcv_established) */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700414 }
415 break;
416 }
417
418 return queued;
419}
420
421int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
422 struct dccp_hdr *dh, unsigned len)
423{
424 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700425 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700426 const int old_state = sk->sk_state;
427 int queued = 0;
428
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300429 /*
430 * Step 3: Process LISTEN state
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300431 *
432 * If S.state == LISTEN,
Gerrit Renkerd83ca5a2006-11-10 16:29:14 -0200433 * If P.type == Request or P contains a valid Init Cookie option,
434 * (* Must scan the packet's options to check for Init
435 * Cookies. Only Init Cookies are processed here,
436 * however; other options are processed in Step 8. This
437 * scan need only be performed if the endpoint uses Init
438 * Cookies *)
439 * (* Generate a new socket and switch to that socket *)
440 * Set S := new socket for this port pair
441 * S.state = RESPOND
442 * Choose S.ISS (initial seqno) or set from Init Cookies
443 * Initialize S.GAR := S.ISS
444 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
445 * Cookies Continue with S.state == RESPOND
446 * (* A Response packet will be generated in Step 11 *)
447 * Otherwise,
448 * Generate Reset(No Connection) unless P.type == Reset
449 * Drop packet and return
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300450 */
451 if (sk->sk_state == DCCP_LISTEN) {
452 if (dh->dccph_type == DCCP_PKT_REQUEST) {
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800453 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
454 skb) < 0)
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300455 return 1;
456
457 /* FIXME: do congestion control initialization */
458 goto discard;
459 }
460 if (dh->dccph_type == DCCP_PKT_RESET)
461 goto discard;
462
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700463 /* Caller (dccp_v4_do_rcv) will send Reset */
464 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo8649b0d2005-08-13 20:36:01 -0300465 return 1;
466 }
467
468 if (sk->sk_state != DCCP_REQUESTING) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700469 if (dccp_check_seqno(sk, skb))
470 goto discard;
471
472 /*
473 * Step 8: Process options and mark acknowledgeable
474 */
475 if (dccp_parse_options(sk, skb))
476 goto discard;
477
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700478 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700479 dccp_event_ack_recv(sk, skb);
480
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200481 if (dccp_msk(sk)->dccpms_send_ack_vector &&
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -0700482 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200483 DCCP_SKB_CB(skb)->dccpd_seq,
484 DCCP_ACKVEC_STATE_RECEIVED))
485 goto discard;
Andrea Bittaue84a9f52006-01-03 14:26:15 -0800486
Gerrit Renker151a9932007-03-07 12:53:48 -0800487 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
488 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700489 }
490
491 /*
492 * Step 9: Process Reset
493 * If P.type == Reset,
494 * Tear down connection
495 * S.state := TIMEWAIT
496 * Set TIMEWAIT timer
497 * Drop packet and return
498 */
499 if (dh->dccph_type == DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300500 /*
501 * Queue the equivalent of TCP fin so that dccp_recvmsg
502 * exits the loop
503 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504 dccp_fin(sk, skb);
505 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
506 return 0;
507 /*
508 * Step 7: Check for unexpected packet types
509 * If (S.is_server and P.type == CloseReq)
510 * or (S.is_server and P.type == Response)
511 * or (S.is_client and P.type == Request)
512 * or (S.state == RESPOND and P.type == Data),
513 * Send Sync packet acknowledging P.seqno
514 * Drop packet and return
515 */
516 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300517 (dh->dccph_type == DCCP_PKT_RESPONSE ||
518 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700519 (dp->dccps_role == DCCP_ROLE_CLIENT &&
520 dh->dccph_type == DCCP_PKT_REQUEST) ||
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300521 (sk->sk_state == DCCP_RESPOND &&
522 dh->dccph_type == DCCP_PKT_DATA)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700523 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700524 goto discard;
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700525 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
526 dccp_rcv_closereq(sk, skb);
527 goto discard;
528 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
529 dccp_rcv_close(sk, skb);
530 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700531 }
532
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300533 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700534 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
Arnaldo Carvalho de Melo2b802302005-09-13 19:05:08 -0300535 goto discard;
536 }
537
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700538 switch (sk->sk_state) {
539 case DCCP_CLOSED:
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700540 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700541 return 1;
542
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700543 case DCCP_REQUESTING:
544 /* FIXME: do congestion control initialization */
545
546 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
547 if (queued >= 0)
548 return queued;
549
550 __kfree_skb(skb);
551 return 0;
552
553 case DCCP_RESPOND:
554 case DCCP_PARTOPEN:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300555 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
556 dh, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700557 break;
558 }
559
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300560 if (dh->dccph_type == DCCP_PKT_ACK ||
561 dh->dccph_type == DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700562 switch (old_state) {
563 case DCCP_PARTOPEN:
564 sk->sk_state_change(sk);
565 sk_wake_async(sk, 0, POLL_OUT);
566 break;
567 }
568 }
569
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200570 if (!queued) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700571discard:
572 __kfree_skb(skb);
573 }
574 return 0;
575}
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800576
577EXPORT_SYMBOL_GPL(dccp_rcv_state_process);