[DCCP]: Only deliver to the CCID rx side in charge
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / dccp / input.c
blob4a3279cd684c5a25246ca8d5e3742b9e3e13c0b9
1 /*
2 * net/dccp/input.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
13 #include <linux/dccp.h>
14 #include <linux/skbuff.h>
16 #include <net/sock.h>
18 #include "ackvec.h"
19 #include "ccid.h"
20 #include "dccp.h"
22 static void dccp_fin(struct sock *sk, struct sk_buff *skb)
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);
32 static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
34 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
35 dccp_fin(sk, skb);
36 dccp_set_state(sk, DCCP_CLOSED);
37 sk_wake_async(sk, 1, POLL_HUP);
40 static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
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
48 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
49 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
50 return;
53 if (sk->sk_state != DCCP_CLOSING)
54 dccp_set_state(sk, DCCP_CLOSING);
55 dccp_send_close(sk, 0);
58 static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
60 struct dccp_sock *dp = dccp_sk(sk);
62 if (dccp_msk(sk)->dccpms_send_ack_vector)
63 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
64 DCCP_SKB_CB(skb)->dccpd_ack_seq);
67 static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
69 const struct dccp_hdr *dh = dccp_hdr(skb);
70 struct dccp_sock *dp = dccp_sk(sk);
71 u64 lswl, lawl;
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
85 if (dh->dccph_type == DCCP_PKT_SYNC ||
86 dh->dccph_type == DCCP_PKT_SYNCACK) {
87 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
88 dp->dccps_awl, dp->dccps_awh) &&
89 !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;
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
109 lswl = dp->dccps_swl;
110 lawl = dp->dccps_awl;
112 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
113 dh->dccph_type == DCCP_PKT_CLOSE ||
114 dh->dccph_type == DCCP_PKT_RESET) {
115 lswl = dp->dccps_gsr;
116 dccp_inc_seqno(&lswl);
117 lawl = dp->dccps_gar;
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 ||
122 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
123 lawl, dp->dccps_awh))) {
124 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
126 if (dh->dccph_type != DCCP_PKT_SYNC &&
127 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
128 DCCP_PKT_WITHOUT_ACK_SEQ))
129 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
130 } else {
131 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 ==
139 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
140 (unsigned long long) lawl,
141 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
142 (unsigned long long) dp->dccps_awh);
143 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
144 return -1;
147 return 0;
150 static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
151 const struct dccp_hdr *dh, const unsigned len)
153 struct dccp_sock *dp = dccp_sk(sk);
155 switch (dccp_hdr(skb)->dccph_type) {
156 case DCCP_PKT_DATAACK:
157 case DCCP_PKT_DATA:
159 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
160 * option if it is.
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:
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
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:
188 /* Step 7
189 * or (S.is_server and P.type == Response)
190 * 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
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;
205 check_seq:
206 if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) {
207 send_sync:
208 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
209 DCCP_PKT_SYNC);
211 break;
212 case DCCP_PKT_SYNC:
213 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
214 DCCP_PKT_SYNCACK);
216 * From RFC 4340, sec. 5.7
218 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
219 * MAY have non-zero-length application data areas, whose
220 * contents receivers MUST ignore.
222 goto discard;
225 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
226 discard:
227 __kfree_skb(skb);
228 return 0;
231 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
232 const struct dccp_hdr *dh, const unsigned len)
234 struct dccp_sock *dp = dccp_sk(sk);
236 if (dccp_check_seqno(sk, skb))
237 goto discard;
239 if (dccp_parse_options(sk, skb))
240 goto discard;
242 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
243 dccp_event_ack_recv(sk, skb);
245 if (dccp_msk(sk)->dccpms_send_ack_vector &&
246 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
247 DCCP_SKB_CB(skb)->dccpd_seq,
248 DCCP_ACKVEC_STATE_RECEIVED))
249 goto discard;
252 * Deliver to the CCID module in charge.
253 * FIXME: Currently DCCP operates one-directional only, i.e. a listening
254 * server is not at the same time a connecting client. There is
255 * not much sense in delivering to both rx/tx sides at the moment
256 * (only one is active at a time); when moving to bidirectional
257 * service, this needs to be revised.
259 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER)
260 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
261 else
262 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
264 return __dccp_rcv_established(sk, skb, dh, len);
265 discard:
266 __kfree_skb(skb);
267 return 0;
270 EXPORT_SYMBOL_GPL(dccp_rcv_established);
272 static int dccp_rcv_request_sent_state_process(struct sock *sk,
273 struct sk_buff *skb,
274 const struct dccp_hdr *dh,
275 const unsigned len)
278 * Step 4: Prepare sequence numbers in REQUEST
279 * If S.state == REQUEST,
280 * If (P.type == Response or P.type == Reset)
281 * and S.AWL <= P.ackno <= S.AWH,
282 * / * Set sequence number variables corresponding to the
283 * other endpoint, so P will pass the tests in Step 6 * /
284 * Set S.GSR, S.ISR, S.SWL, S.SWH
285 * / * Response processing continues in Step 10; Reset
286 * processing continues in Step 9 * /
288 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
289 const struct inet_connection_sock *icsk = inet_csk(sk);
290 struct dccp_sock *dp = dccp_sk(sk);
292 /* Stop the REQUEST timer */
293 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
294 BUG_TRAP(sk->sk_send_head != NULL);
295 __kfree_skb(sk->sk_send_head);
296 sk->sk_send_head = NULL;
298 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
299 dp->dccps_awl, dp->dccps_awh)) {
300 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
301 "P.ackno=%llu, S.AWH=%llu \n",
302 (unsigned long long)dp->dccps_awl,
303 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
304 (unsigned long long)dp->dccps_awh);
305 goto out_invalid_packet;
308 if (dccp_parse_options(sk, skb))
309 goto out_invalid_packet;
311 if (dccp_msk(sk)->dccpms_send_ack_vector &&
312 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
313 DCCP_SKB_CB(skb)->dccpd_seq,
314 DCCP_ACKVEC_STATE_RECEIVED))
315 goto out_invalid_packet; /* FIXME: change error code */
317 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
318 dccp_update_gsr(sk, dp->dccps_isr);
320 * SWL and AWL are initially adjusted so that they are not less than
321 * the initial Sequence Numbers received and sent, respectively:
322 * SWL := max(GSR + 1 - floor(W/4), ISR),
323 * AWL := max(GSS - W' + 1, ISS).
324 * These adjustments MUST be applied only at the beginning of the
325 * connection.
327 * AWL was adjusted in dccp_v4_connect -acme
329 dccp_set_seqno(&dp->dccps_swl,
330 max48(dp->dccps_swl, dp->dccps_isr));
332 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
335 * Step 10: Process REQUEST state (second part)
336 * If S.state == REQUEST,
337 * / * If we get here, P is a valid Response from the
338 * server (see Step 4), and we should move to
339 * PARTOPEN state. PARTOPEN means send an Ack,
340 * don't send Data packets, retransmit Acks
341 * periodically, and always include any Init Cookie
342 * from the Response * /
343 * S.state := PARTOPEN
344 * Set PARTOPEN timer
345 * Continue with S.state == PARTOPEN
346 * / * Step 12 will send the Ack completing the
347 * three-way handshake * /
349 dccp_set_state(sk, DCCP_PARTOPEN);
351 /* Make sure socket is routed, for correct metrics. */
352 icsk->icsk_af_ops->rebuild_header(sk);
354 if (!sock_flag(sk, SOCK_DEAD)) {
355 sk->sk_state_change(sk);
356 sk_wake_async(sk, 0, POLL_OUT);
359 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
360 icsk->icsk_accept_queue.rskq_defer_accept) {
361 /* Save one ACK. Data will be ready after
362 * several ticks, if write_pending is set.
364 * It may be deleted, but with this feature tcpdumps
365 * look so _wonderfully_ clever, that I was not able
366 * to stand against the temptation 8) --ANK
369 * OK, in DCCP we can as well do a similar trick, its
370 * even in the draft, but there is no need for us to
371 * schedule an ack here, as dccp_sendmsg does this for
372 * us, also stated in the draft. -acme
374 __kfree_skb(skb);
375 return 0;
377 dccp_send_ack(sk);
378 return -1;
381 out_invalid_packet:
382 /* dccp_v4_do_rcv will send a reset */
383 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
384 return 1;
387 static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
388 struct sk_buff *skb,
389 const struct dccp_hdr *dh,
390 const unsigned len)
392 int queued = 0;
394 switch (dh->dccph_type) {
395 case DCCP_PKT_RESET:
396 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
397 break;
398 case DCCP_PKT_DATA:
399 if (sk->sk_state == DCCP_RESPOND)
400 break;
401 case DCCP_PKT_DATAACK:
402 case DCCP_PKT_ACK:
404 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
405 * here but only if we haven't used the DELACK timer for
406 * something else, like sending a delayed ack for a TIMESTAMP
407 * echo, etc, for now were not clearing it, sending an extra
408 * ACK when there is nothing else to do in DELACK is not a big
409 * deal after all.
412 /* Stop the PARTOPEN timer */
413 if (sk->sk_state == DCCP_PARTOPEN)
414 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
416 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
417 dccp_set_state(sk, DCCP_OPEN);
419 if (dh->dccph_type == DCCP_PKT_DATAACK ||
420 dh->dccph_type == DCCP_PKT_DATA) {
421 __dccp_rcv_established(sk, skb, dh, len);
422 queued = 1; /* packet was queued
423 (by __dccp_rcv_established) */
425 break;
428 return queued;
431 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
432 struct dccp_hdr *dh, unsigned len)
434 struct dccp_sock *dp = dccp_sk(sk);
435 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
436 const int old_state = sk->sk_state;
437 int queued = 0;
440 * Step 3: Process LISTEN state
442 * If S.state == LISTEN,
443 * If P.type == Request or P contains a valid Init Cookie option,
444 * (* Must scan the packet's options to check for Init
445 * Cookies. Only Init Cookies are processed here,
446 * however; other options are processed in Step 8. This
447 * scan need only be performed if the endpoint uses Init
448 * Cookies *)
449 * (* Generate a new socket and switch to that socket *)
450 * Set S := new socket for this port pair
451 * S.state = RESPOND
452 * Choose S.ISS (initial seqno) or set from Init Cookies
453 * Initialize S.GAR := S.ISS
454 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
455 * Cookies Continue with S.state == RESPOND
456 * (* A Response packet will be generated in Step 11 *)
457 * Otherwise,
458 * Generate Reset(No Connection) unless P.type == Reset
459 * Drop packet and return
461 if (sk->sk_state == DCCP_LISTEN) {
462 if (dh->dccph_type == DCCP_PKT_REQUEST) {
463 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
464 skb) < 0)
465 return 1;
467 /* FIXME: do congestion control initialization */
468 goto discard;
470 if (dh->dccph_type == DCCP_PKT_RESET)
471 goto discard;
473 /* Caller (dccp_v4_do_rcv) will send Reset */
474 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
475 return 1;
478 if (sk->sk_state != DCCP_REQUESTING) {
479 if (dccp_check_seqno(sk, skb))
480 goto discard;
483 * Step 8: Process options and mark acknowledgeable
485 if (dccp_parse_options(sk, skb))
486 goto discard;
488 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
489 dccp_event_ack_recv(sk, skb);
491 if (dccp_msk(sk)->dccpms_send_ack_vector &&
492 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
493 DCCP_SKB_CB(skb)->dccpd_seq,
494 DCCP_ACKVEC_STATE_RECEIVED))
495 goto discard;
497 /* XXX see the comments in dccp_rcv_established about this */
498 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER)
499 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
500 else
501 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
505 * Step 9: Process Reset
506 * If P.type == Reset,
507 * Tear down connection
508 * S.state := TIMEWAIT
509 * Set TIMEWAIT timer
510 * Drop packet and return
512 if (dh->dccph_type == DCCP_PKT_RESET) {
514 * Queue the equivalent of TCP fin so that dccp_recvmsg
515 * exits the loop
517 dccp_fin(sk, skb);
518 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
519 return 0;
521 * Step 7: Check for unexpected packet types
522 * If (S.is_server and P.type == CloseReq)
523 * or (S.is_server and P.type == Response)
524 * or (S.is_client and P.type == Request)
525 * or (S.state == RESPOND and P.type == Data),
526 * Send Sync packet acknowledging P.seqno
527 * Drop packet and return
529 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
530 (dh->dccph_type == DCCP_PKT_RESPONSE ||
531 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
532 (dp->dccps_role == DCCP_ROLE_CLIENT &&
533 dh->dccph_type == DCCP_PKT_REQUEST) ||
534 (sk->sk_state == DCCP_RESPOND &&
535 dh->dccph_type == DCCP_PKT_DATA)) {
536 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
537 goto discard;
538 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
539 dccp_rcv_closereq(sk, skb);
540 goto discard;
541 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
542 dccp_rcv_close(sk, skb);
543 return 0;
546 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
547 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
548 goto discard;
551 switch (sk->sk_state) {
552 case DCCP_CLOSED:
553 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
554 return 1;
556 case DCCP_REQUESTING:
557 /* FIXME: do congestion control initialization */
559 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
560 if (queued >= 0)
561 return queued;
563 __kfree_skb(skb);
564 return 0;
566 case DCCP_RESPOND:
567 case DCCP_PARTOPEN:
568 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
569 dh, len);
570 break;
573 if (dh->dccph_type == DCCP_PKT_ACK ||
574 dh->dccph_type == DCCP_PKT_DATAACK) {
575 switch (old_state) {
576 case DCCP_PARTOPEN:
577 sk->sk_state_change(sk);
578 sk_wake_async(sk, 0, POLL_OUT);
579 break;
583 if (!queued) {
584 discard:
585 __kfree_skb(skb);
587 return 0;
590 EXPORT_SYMBOL_GPL(dccp_rcv_state_process);