sysctl: ipv6 route flushing (kill binary path)
[wrt350n-kernel.git] / net / dccp / input.c
blob19d7e1dbd87e99a98f463e944a6f20f052fd9856
1 /*
2 * net/dccp/input.c
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, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
72 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
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
86 if (dh->dccph_type == DCCP_PKT_SYNC ||
87 dh->dccph_type == DCCP_PKT_SYNCACK) {
88 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
89 dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
90 dccp_update_gsr(sk, seqno);
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
106 lswl = dp->dccps_swl;
107 lawl = dp->dccps_awl;
109 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
110 dh->dccph_type == DCCP_PKT_CLOSE ||
111 dh->dccph_type == DCCP_PKT_RESET) {
112 lswl = ADD48(dp->dccps_gsr, 1);
113 lawl = dp->dccps_gar;
116 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);
121 if (dh->dccph_type != DCCP_PKT_SYNC &&
122 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
123 dp->dccps_gar = ackno;
124 } else {
125 unsigned long now = jiffies;
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
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.
138 if (time_before(now, (dp->dccps_rate_last +
139 sysctl_dccp_sync_ratelimit)))
140 return 0;
142 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),
146 (unsigned long long) lswl, (unsigned long long) seqno,
147 (unsigned long long) dp->dccps_swh,
148 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
149 : "exists",
150 (unsigned long long) lawl, (unsigned long long) ackno,
151 (unsigned long long) dp->dccps_awh);
153 dp->dccps_rate_last = now;
155 if (dh->dccph_type == DCCP_PKT_RESET)
156 seqno = dp->dccps_gsr;
157 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
158 return -1;
161 return 0;
164 static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
165 const struct dccp_hdr *dh, const unsigned len)
167 struct dccp_sock *dp = dccp_sk(sk);
169 switch (dccp_hdr(skb)->dccph_type) {
170 case DCCP_PKT_DATAACK:
171 case DCCP_PKT_DATA:
173 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
174 * option if it is.
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:
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
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:
202 /* Step 7
203 * or (S.is_server and P.type == Response)
204 * 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
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;
219 check_seq:
220 if (dccp_delta_seqno(dp->dccps_osr,
221 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
222 send_sync:
223 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
224 DCCP_PKT_SYNC);
226 break;
227 case DCCP_PKT_SYNC:
228 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
229 DCCP_PKT_SYNCACK);
231 * From RFC 4340, sec. 5.7
233 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
234 * MAY have non-zero-length application data areas, whose
235 * contents receivers MUST ignore.
237 goto discard;
240 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
241 discard:
242 __kfree_skb(skb);
243 return 0;
246 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
247 const struct dccp_hdr *dh, const unsigned len)
249 struct dccp_sock *dp = dccp_sk(sk);
251 if (dccp_check_seqno(sk, skb))
252 goto discard;
254 if (dccp_parse_options(sk, skb))
255 goto discard;
257 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
258 dccp_event_ack_recv(sk, skb);
260 if (dccp_msk(sk)->dccpms_send_ack_vector &&
261 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
262 DCCP_SKB_CB(skb)->dccpd_seq,
263 DCCP_ACKVEC_STATE_RECEIVED))
264 goto discard;
266 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);
269 return __dccp_rcv_established(sk, skb, dh, len);
270 discard:
271 __kfree_skb(skb);
272 return 0;
275 EXPORT_SYMBOL_GPL(dccp_rcv_established);
277 static 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)
283 * 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 * /
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);
296 long tstamp = dccp_timestamp();
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;
304 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);
311 goto out_invalid_packet;
314 if (dccp_parse_options(sk, skb))
315 goto out_invalid_packet;
317 /* 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));
322 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 */
328 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
329 dccp_update_gsr(sk, dp->dccps_isr);
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.
338 * AWL was adjusted in dccp_v4_connect -acme
340 dccp_set_seqno(&dp->dccps_swl,
341 max48(dp->dccps_swl, dp->dccps_isr));
343 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
346 * Step 10: Process REQUEST state (second part)
347 * If S.state == REQUEST,
348 * / * 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 * /
354 * S.state := PARTOPEN
355 * Set PARTOPEN timer
356 * Continue with S.state == PARTOPEN
357 * / * Step 12 will send the Ack completing the
358 * three-way handshake * /
360 dccp_set_state(sk, DCCP_PARTOPEN);
362 /* Make sure socket is routed, for correct metrics. */
363 icsk->icsk_af_ops->rebuild_header(sk);
365 if (!sock_flag(sk, SOCK_DEAD)) {
366 sk->sk_state_change(sk);
367 sk_wake_async(sk, 0, POLL_OUT);
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.
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
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
385 __kfree_skb(skb);
386 return 0;
388 dccp_send_ack(sk);
389 return -1;
392 out_invalid_packet:
393 /* dccp_v4_do_rcv will send a reset */
394 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
395 return 1;
398 static 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)
403 int queued = 0;
405 switch (dh->dccph_type) {
406 case DCCP_PKT_RESET:
407 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
408 break;
409 case DCCP_PKT_DATA:
410 if (sk->sk_state == DCCP_RESPOND)
411 break;
412 case DCCP_PKT_DATAACK:
413 case DCCP_PKT_ACK:
415 * 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.
423 /* Stop the PARTOPEN timer */
424 if (sk->sk_state == DCCP_PARTOPEN)
425 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
427 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
428 dccp_set_state(sk, DCCP_OPEN);
430 if (dh->dccph_type == DCCP_PKT_DATAACK ||
431 dh->dccph_type == DCCP_PKT_DATA) {
432 __dccp_rcv_established(sk, skb, dh, len);
433 queued = 1; /* packet was queued
434 (by __dccp_rcv_established) */
436 break;
439 return queued;
442 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
443 struct dccp_hdr *dh, unsigned len)
445 struct dccp_sock *dp = dccp_sk(sk);
446 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
447 const int old_state = sk->sk_state;
448 int queued = 0;
451 * Step 3: Process LISTEN state
453 * If S.state == LISTEN,
454 * 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
472 if (sk->sk_state == DCCP_LISTEN) {
473 if (dh->dccph_type == DCCP_PKT_REQUEST) {
474 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
475 skb) < 0)
476 return 1;
478 /* FIXME: do congestion control initialization */
479 goto discard;
481 if (dh->dccph_type == DCCP_PKT_RESET)
482 goto discard;
484 /* Caller (dccp_v4_do_rcv) will send Reset */
485 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
486 return 1;
489 if (sk->sk_state != DCCP_REQUESTING) {
490 if (dccp_check_seqno(sk, skb))
491 goto discard;
494 * Step 8: Process options and mark acknowledgeable
496 if (dccp_parse_options(sk, skb))
497 goto discard;
499 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
500 dccp_event_ack_recv(sk, skb);
502 if (dccp_msk(sk)->dccpms_send_ack_vector &&
503 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
504 DCCP_SKB_CB(skb)->dccpd_seq,
505 DCCP_ACKVEC_STATE_RECEIVED))
506 goto discard;
508 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);
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
520 if (dh->dccph_type == DCCP_PKT_RESET) {
522 * Queue the equivalent of TCP fin so that dccp_recvmsg
523 * exits the loop
525 dccp_fin(sk, skb);
526 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
527 return 0;
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
537 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
538 (dh->dccph_type == DCCP_PKT_RESPONSE ||
539 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
540 (dp->dccps_role == DCCP_ROLE_CLIENT &&
541 dh->dccph_type == DCCP_PKT_REQUEST) ||
542 (sk->sk_state == DCCP_RESPOND &&
543 dh->dccph_type == DCCP_PKT_DATA)) {
544 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
545 goto discard;
546 } 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;
554 switch (sk->sk_state) {
555 case DCCP_CLOSED:
556 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
557 return 1;
559 case DCCP_REQUESTING:
560 /* FIXME: do congestion control initialization */
562 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
563 if (queued >= 0)
564 return queued;
566 __kfree_skb(skb);
567 return 0;
569 case DCCP_RESPOND:
570 case DCCP_PARTOPEN:
571 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
572 dh, len);
573 break;
576 if (dh->dccph_type == DCCP_PKT_ACK ||
577 dh->dccph_type == DCCP_PKT_DATAACK) {
578 switch (old_state) {
579 case DCCP_PARTOPEN:
580 sk->sk_state_change(sk);
581 sk_wake_async(sk, 0, POLL_OUT);
582 break;
584 } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
585 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
586 goto discard;
589 if (!queued) {
590 discard:
591 __kfree_skb(skb);
593 return 0;
596 EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
599 * 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.
604 u32 dccp_sample_rtt(struct sock *sk, long delta)
606 /* dccpor_elapsed_time is either zeroed out or set and > 0 */
607 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
609 if (unlikely(delta <= 0)) {
610 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
611 return DCCP_SANE_RTT_MIN;
613 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
614 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
615 return DCCP_SANE_RTT_MAX;
618 return delta;
621 EXPORT_SYMBOL_GPL(dccp_sample_rtt);