[CCID2]: Replace cwnd assignment-function with assignment
[linux-2.6.git] / net / dccp / input.c
1 /*
2  *  net/dccp/input.c
3  *
4  *  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
13 #include <linux/dccp.h>
14 #include <linux/skbuff.h>
15
16 #include <net/sock.h>
17
18 #include "ackvec.h"
19 #include "ccid.h"
20 #include "dccp.h"
21
22 /* rate-limit for syncs in reply to sequence-invalid packets; RFC 4340, 7.5.4 */
23 int sysctl_dccp_sync_ratelimit  __read_mostly = HZ / 8;
24
25 static void dccp_fin(struct sock *sk, struct sk_buff *skb)
26 {
27         sk->sk_shutdown |= RCV_SHUTDOWN;
28         sock_set_flag(sk, SOCK_DONE);
29         __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
30         __skb_queue_tail(&sk->sk_receive_queue, skb);
31         skb_set_owner_r(skb, sk);
32         sk->sk_data_ready(sk, 0);
33 }
34
35 static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
36 {
37         dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
38         dccp_fin(sk, skb);
39         dccp_set_state(sk, DCCP_CLOSED);
40         sk_wake_async(sk, 1, POLL_HUP);
41 }
42
43 static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
44 {
45         /*
46          *   Step 7: Check for unexpected packet types
47          *      If (S.is_server and P.type == CloseReq)
48          *        Send Sync packet acknowledging P.seqno
49          *        Drop packet and return
50          */
51         if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
52                 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
53                 return;
54         }
55
56         if (sk->sk_state != DCCP_CLOSING)
57                 dccp_set_state(sk, DCCP_CLOSING);
58         dccp_send_close(sk, 0);
59 }
60
61 static u8 dccp_reset_code_convert(const u8 code)
62 {
63         const u8 error_code[] = {
64         [DCCP_RESET_CODE_CLOSED]             = 0,       /* normal termination */
65         [DCCP_RESET_CODE_UNSPECIFIED]        = 0,       /* nothing known */
66         [DCCP_RESET_CODE_ABORTED]            = ECONNRESET,
67
68         [DCCP_RESET_CODE_NO_CONNECTION]      = ECONNREFUSED,
69         [DCCP_RESET_CODE_CONNECTION_REFUSED] = ECONNREFUSED,
70         [DCCP_RESET_CODE_TOO_BUSY]           = EUSERS,
71         [DCCP_RESET_CODE_AGGRESSION_PENALTY] = EDQUOT,
72
73         [DCCP_RESET_CODE_PACKET_ERROR]       = ENOMSG,
74         [DCCP_RESET_CODE_BAD_INIT_COOKIE]    = EBADR,
75         [DCCP_RESET_CODE_BAD_SERVICE_CODE]   = EBADRQC,
76         [DCCP_RESET_CODE_OPTION_ERROR]       = EILSEQ,
77         [DCCP_RESET_CODE_MANDATORY_ERROR]    = EOPNOTSUPP,
78         };
79
80         return code >= DCCP_MAX_RESET_CODES ? 0 : error_code[code];
81 }
82
83 static void dccp_rcv_reset(struct sock *sk, struct sk_buff *skb)
84 {
85         u8 err = dccp_reset_code_convert(dccp_hdr_reset(skb)->dccph_reset_code);
86
87         sk->sk_err = err;
88
89         /* Queue the equivalent of TCP fin so that dccp_recvmsg exits the loop */
90         dccp_fin(sk, skb);
91
92         if (err && !sock_flag(sk, SOCK_DEAD))
93                 sk_wake_async(sk, 0, POLL_ERR);
94         dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
95 }
96
97 static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
98 {
99         struct dccp_sock *dp = dccp_sk(sk);
100
101         if (dccp_msk(sk)->dccpms_send_ack_vector)
102                 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
103                                             DCCP_SKB_CB(skb)->dccpd_ack_seq);
104 }
105
106 static void dccp_deliver_input_to_ccids(struct sock *sk, struct sk_buff *skb)
107 {
108         const struct dccp_sock *dp = dccp_sk(sk);
109
110         /* Don't deliver to RX CCID when node has shut down read end. */
111         if (!(sk->sk_shutdown & RCV_SHUTDOWN))
112                 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
113         /*
114          * Until the TX queue has been drained, we can not honour SHUT_WR, since
115          * we need received feedback as input to adjust congestion control.
116          */
117         if (sk->sk_write_queue.qlen > 0 || !(sk->sk_shutdown & SEND_SHUTDOWN))
118                 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
119 }
120
121 static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
122 {
123         const struct dccp_hdr *dh = dccp_hdr(skb);
124         struct dccp_sock *dp = dccp_sk(sk);
125         u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
126                         ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
127
128         /*
129          *   Step 5: Prepare sequence numbers for Sync
130          *     If P.type == Sync or P.type == SyncAck,
131          *        If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
132          *           / * P is valid, so update sequence number variables
133          *               accordingly.  After this update, P will pass the tests
134          *               in Step 6.  A SyncAck is generated if necessary in
135          *               Step 15 * /
136          *           Update S.GSR, S.SWL, S.SWH
137          *        Otherwise,
138          *           Drop packet and return
139          */
140         if (dh->dccph_type == DCCP_PKT_SYNC ||
141             dh->dccph_type == DCCP_PKT_SYNCACK) {
142                 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
143                     dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
144                         dccp_update_gsr(sk, seqno);
145                 else
146                         return -1;
147         }
148
149         /*
150          *   Step 6: Check sequence numbers
151          *      Let LSWL = S.SWL and LAWL = S.AWL
152          *      If P.type == CloseReq or P.type == Close or P.type == Reset,
153          *        LSWL := S.GSR + 1, LAWL := S.GAR
154          *      If LSWL <= P.seqno <= S.SWH
155          *           and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
156          *        Update S.GSR, S.SWL, S.SWH
157          *        If P.type != Sync,
158          *           Update S.GAR
159          */
160         lswl = dp->dccps_swl;
161         lawl = dp->dccps_awl;
162
163         if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
164             dh->dccph_type == DCCP_PKT_CLOSE ||
165             dh->dccph_type == DCCP_PKT_RESET) {
166                 lswl = ADD48(dp->dccps_gsr, 1);
167                 lawl = dp->dccps_gar;
168         }
169
170         if (between48(seqno, lswl, dp->dccps_swh) &&
171             (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
172              between48(ackno, lawl, dp->dccps_awh))) {
173                 dccp_update_gsr(sk, seqno);
174
175                 if (dh->dccph_type != DCCP_PKT_SYNC &&
176                     (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
177                         dp->dccps_gar = ackno;
178         } else {
179                 unsigned long now = jiffies;
180                 /*
181                  *   Step 6: Check sequence numbers
182                  *      Otherwise,
183                  *         If P.type == Reset,
184                  *            Send Sync packet acknowledging S.GSR
185                  *         Otherwise,
186                  *            Send Sync packet acknowledging P.seqno
187                  *      Drop packet and return
188                  *
189                  *   These Syncs are rate-limited as per RFC 4340, 7.5.4:
190                  *   at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
191                  */
192                 if (time_before(now, (dp->dccps_rate_last +
193                                       sysctl_dccp_sync_ratelimit)))
194                         return 0;
195
196                 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
197                           "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
198                           "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
199                           "sending SYNC...\n",  dccp_packet_name(dh->dccph_type),
200                           (unsigned long long) lswl, (unsigned long long) seqno,
201                           (unsigned long long) dp->dccps_swh,
202                           (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
203                                                               : "exists",
204                           (unsigned long long) lawl, (unsigned long long) ackno,
205                           (unsigned long long) dp->dccps_awh);
206
207                 dp->dccps_rate_last = now;
208
209                 if (dh->dccph_type == DCCP_PKT_RESET)
210                         seqno = dp->dccps_gsr;
211                 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
212                 return -1;
213         }
214
215         return 0;
216 }
217
218 static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
219                                   const struct dccp_hdr *dh, const unsigned len)
220 {
221         struct dccp_sock *dp = dccp_sk(sk);
222
223         switch (dccp_hdr(skb)->dccph_type) {
224         case DCCP_PKT_DATAACK:
225         case DCCP_PKT_DATA:
226                 /*
227                  * FIXME: schedule DATA_DROPPED (RFC 4340, 11.7.2) if and when
228                  * - sk_shutdown == RCV_SHUTDOWN, use Code 1, "Not Listening"
229                  * - sk_receive_queue is full, use Code 2, "Receive Buffer"
230                  */
231                 __skb_pull(skb, dh->dccph_doff * 4);
232                 __skb_queue_tail(&sk->sk_receive_queue, skb);
233                 skb_set_owner_r(skb, sk);
234                 sk->sk_data_ready(sk, 0);
235                 return 0;
236         case DCCP_PKT_ACK:
237                 goto discard;
238         case DCCP_PKT_RESET:
239                 /*
240                  *  Step 9: Process Reset
241                  *      If P.type == Reset,
242                  *              Tear down connection
243                  *              S.state := TIMEWAIT
244                  *              Set TIMEWAIT timer
245                  *              Drop packet and return
246                  */
247                 dccp_rcv_reset(sk, skb);
248                 return 0;
249         case DCCP_PKT_CLOSEREQ:
250                 dccp_rcv_closereq(sk, skb);
251                 goto discard;
252         case DCCP_PKT_CLOSE:
253                 dccp_rcv_close(sk, skb);
254                 return 0;
255         case DCCP_PKT_REQUEST:
256                 /* Step 7
257                  *   or (S.is_server and P.type == Response)
258                  *   or (S.is_client and P.type == Request)
259                  *   or (S.state >= OPEN and P.type == Request
260                  *      and P.seqno >= S.OSR)
261                  *    or (S.state >= OPEN and P.type == Response
262                  *      and P.seqno >= S.OSR)
263                  *    or (S.state == RESPOND and P.type == Data),
264                  *  Send Sync packet acknowledging P.seqno
265                  *  Drop packet and return
266                  */
267                 if (dp->dccps_role != DCCP_ROLE_LISTEN)
268                         goto send_sync;
269                 goto check_seq;
270         case DCCP_PKT_RESPONSE:
271                 if (dp->dccps_role != DCCP_ROLE_CLIENT)
272                         goto send_sync;
273 check_seq:
274                 if (dccp_delta_seqno(dp->dccps_osr,
275                                      DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
276 send_sync:
277                         dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
278                                        DCCP_PKT_SYNC);
279                 }
280                 break;
281         case DCCP_PKT_SYNC:
282                 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
283                                DCCP_PKT_SYNCACK);
284                 /*
285                  * From RFC 4340, sec. 5.7
286                  *
287                  * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
288                  * MAY have non-zero-length application data areas, whose
289                  * contents receivers MUST ignore.
290                  */
291                 goto discard;
292         }
293
294         DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
295 discard:
296         __kfree_skb(skb);
297         return 0;
298 }
299
300 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
301                          const struct dccp_hdr *dh, const unsigned len)
302 {
303         struct dccp_sock *dp = dccp_sk(sk);
304
305         if (dccp_check_seqno(sk, skb))
306                 goto discard;
307
308         if (dccp_parse_options(sk, skb))
309                 goto discard;
310
311         if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
312                 dccp_event_ack_recv(sk, skb);
313
314         if (dccp_msk(sk)->dccpms_send_ack_vector &&
315             dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
316                             DCCP_SKB_CB(skb)->dccpd_seq,
317                             DCCP_ACKVEC_STATE_RECEIVED))
318                 goto discard;
319         dccp_deliver_input_to_ccids(sk, skb);
320
321         return __dccp_rcv_established(sk, skb, dh, len);
322 discard:
323         __kfree_skb(skb);
324         return 0;
325 }
326
327 EXPORT_SYMBOL_GPL(dccp_rcv_established);
328
329 static int dccp_rcv_request_sent_state_process(struct sock *sk,
330                                                struct sk_buff *skb,
331                                                const struct dccp_hdr *dh,
332                                                const unsigned len)
333 {
334         /*
335          *  Step 4: Prepare sequence numbers in REQUEST
336          *     If S.state == REQUEST,
337          *        If (P.type == Response or P.type == Reset)
338          *              and S.AWL <= P.ackno <= S.AWH,
339          *           / * Set sequence number variables corresponding to the
340          *              other endpoint, so P will pass the tests in Step 6 * /
341          *           Set S.GSR, S.ISR, S.SWL, S.SWH
342          *           / * Response processing continues in Step 10; Reset
343          *              processing continues in Step 9 * /
344         */
345         if (dh->dccph_type == DCCP_PKT_RESPONSE) {
346                 const struct inet_connection_sock *icsk = inet_csk(sk);
347                 struct dccp_sock *dp = dccp_sk(sk);
348                 long tstamp = dccp_timestamp();
349
350                 /* Stop the REQUEST timer */
351                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
352                 BUG_TRAP(sk->sk_send_head != NULL);
353                 __kfree_skb(sk->sk_send_head);
354                 sk->sk_send_head = NULL;
355
356                 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
357                                dp->dccps_awl, dp->dccps_awh)) {
358                         dccp_pr_debug("invalid ackno: S.AWL=%llu, "
359                                       "P.ackno=%llu, S.AWH=%llu \n",
360                                       (unsigned long long)dp->dccps_awl,
361                            (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
362                                       (unsigned long long)dp->dccps_awh);
363                         goto out_invalid_packet;
364                 }
365
366                 if (dccp_parse_options(sk, skb))
367                         goto out_invalid_packet;
368
369                 /* Obtain usec RTT sample from SYN exchange (used by CCID 3) */
370                 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
371                         dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
372                             dp->dccps_options_received.dccpor_timestamp_echo));
373
374                 if (dccp_msk(sk)->dccpms_send_ack_vector &&
375                     dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
376                                     DCCP_SKB_CB(skb)->dccpd_seq,
377                                     DCCP_ACKVEC_STATE_RECEIVED))
378                         goto out_invalid_packet; /* FIXME: change error code */
379
380                 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
381                 dccp_update_gsr(sk, dp->dccps_isr);
382                 /*
383                  * SWL and AWL are initially adjusted so that they are not less than
384                  * the initial Sequence Numbers received and sent, respectively:
385                  *      SWL := max(GSR + 1 - floor(W/4), ISR),
386                  *      AWL := max(GSS - W' + 1, ISS).
387                  * These adjustments MUST be applied only at the beginning of the
388                  * connection.
389                  *
390                  * AWL was adjusted in dccp_v4_connect -acme
391                  */
392                 dccp_set_seqno(&dp->dccps_swl,
393                                max48(dp->dccps_swl, dp->dccps_isr));
394
395                 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
396
397                 /*
398                  *    Step 10: Process REQUEST state (second part)
399                  *       If S.state == REQUEST,
400                  *        / * If we get here, P is a valid Response from the
401                  *            server (see Step 4), and we should move to
402                  *            PARTOPEN state. PARTOPEN means send an Ack,
403                  *            don't send Data packets, retransmit Acks
404                  *            periodically, and always include any Init Cookie
405                  *            from the Response * /
406                  *        S.state := PARTOPEN
407                  *        Set PARTOPEN timer
408                  *        Continue with S.state == PARTOPEN
409                  *        / * Step 12 will send the Ack completing the
410                  *            three-way handshake * /
411                  */
412                 dccp_set_state(sk, DCCP_PARTOPEN);
413
414                 /* Make sure socket is routed, for correct metrics. */
415                 icsk->icsk_af_ops->rebuild_header(sk);
416
417                 if (!sock_flag(sk, SOCK_DEAD)) {
418                         sk->sk_state_change(sk);
419                         sk_wake_async(sk, 0, POLL_OUT);
420                 }
421
422                 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
423                     icsk->icsk_accept_queue.rskq_defer_accept) {
424                         /* Save one ACK. Data will be ready after
425                          * several ticks, if write_pending is set.
426                          *
427                          * It may be deleted, but with this feature tcpdumps
428                          * look so _wonderfully_ clever, that I was not able
429                          * to stand against the temptation 8)     --ANK
430                          */
431                         /*
432                          * OK, in DCCP we can as well do a similar trick, its
433                          * even in the draft, but there is no need for us to
434                          * schedule an ack here, as dccp_sendmsg does this for
435                          * us, also stated in the draft. -acme
436                          */
437                         __kfree_skb(skb);
438                         return 0;
439                 }
440                 dccp_send_ack(sk);
441                 return -1;
442         }
443
444 out_invalid_packet:
445         /* dccp_v4_do_rcv will send a reset */
446         DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
447         return 1;
448 }
449
450 static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
451                                                    struct sk_buff *skb,
452                                                    const struct dccp_hdr *dh,
453                                                    const unsigned len)
454 {
455         int queued = 0;
456
457         switch (dh->dccph_type) {
458         case DCCP_PKT_RESET:
459                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
460                 break;
461         case DCCP_PKT_DATA:
462                 if (sk->sk_state == DCCP_RESPOND)
463                         break;
464         case DCCP_PKT_DATAACK:
465         case DCCP_PKT_ACK:
466                 /*
467                  * FIXME: we should be reseting the PARTOPEN (DELACK) timer
468                  * here but only if we haven't used the DELACK timer for
469                  * something else, like sending a delayed ack for a TIMESTAMP
470                  * echo, etc, for now were not clearing it, sending an extra
471                  * ACK when there is nothing else to do in DELACK is not a big
472                  * deal after all.
473                  */
474
475                 /* Stop the PARTOPEN timer */
476                 if (sk->sk_state == DCCP_PARTOPEN)
477                         inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
478
479                 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
480                 dccp_set_state(sk, DCCP_OPEN);
481
482                 if (dh->dccph_type == DCCP_PKT_DATAACK ||
483                     dh->dccph_type == DCCP_PKT_DATA) {
484                         __dccp_rcv_established(sk, skb, dh, len);
485                         queued = 1; /* packet was queued
486                                        (by __dccp_rcv_established) */
487                 }
488                 break;
489         }
490
491         return queued;
492 }
493
494 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
495                            struct dccp_hdr *dh, unsigned len)
496 {
497         struct dccp_sock *dp = dccp_sk(sk);
498         struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
499         const int old_state = sk->sk_state;
500         int queued = 0;
501
502         /*
503          *  Step 3: Process LISTEN state
504          *
505          *     If S.state == LISTEN,
506          *       If P.type == Request or P contains a valid Init Cookie option,
507          *            (* Must scan the packet's options to check for Init
508          *               Cookies.  Only Init Cookies are processed here,
509          *               however; other options are processed in Step 8.  This
510          *               scan need only be performed if the endpoint uses Init
511          *               Cookies *)
512          *            (* Generate a new socket and switch to that socket *)
513          *            Set S := new socket for this port pair
514          *            S.state = RESPOND
515          *            Choose S.ISS (initial seqno) or set from Init Cookies
516          *            Initialize S.GAR := S.ISS
517          *            Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
518          *            Cookies Continue with S.state == RESPOND
519          *            (* A Response packet will be generated in Step 11 *)
520          *       Otherwise,
521          *            Generate Reset(No Connection) unless P.type == Reset
522          *            Drop packet and return
523          */
524         if (sk->sk_state == DCCP_LISTEN) {
525                 if (dh->dccph_type == DCCP_PKT_REQUEST) {
526                         if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
527                                                                     skb) < 0)
528                                 return 1;
529
530                         /* FIXME: do congestion control initialization */
531                         goto discard;
532                 }
533                 if (dh->dccph_type == DCCP_PKT_RESET)
534                         goto discard;
535
536                 /* Caller (dccp_v4_do_rcv) will send Reset */
537                 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
538                 return 1;
539         }
540
541         if (sk->sk_state != DCCP_REQUESTING) {
542                 if (dccp_check_seqno(sk, skb))
543                         goto discard;
544
545                 /*
546                  * Step 8: Process options and mark acknowledgeable
547                  */
548                 if (dccp_parse_options(sk, skb))
549                         goto discard;
550
551                 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
552                         dccp_event_ack_recv(sk, skb);
553
554                 if (dccp_msk(sk)->dccpms_send_ack_vector &&
555                     dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
556                                     DCCP_SKB_CB(skb)->dccpd_seq,
557                                     DCCP_ACKVEC_STATE_RECEIVED))
558                         goto discard;
559
560                 dccp_deliver_input_to_ccids(sk, skb);
561         }
562
563         /*
564          *  Step 9: Process Reset
565          *      If P.type == Reset,
566          *              Tear down connection
567          *              S.state := TIMEWAIT
568          *              Set TIMEWAIT timer
569          *              Drop packet and return
570         */
571         if (dh->dccph_type == DCCP_PKT_RESET) {
572                 dccp_rcv_reset(sk, skb);
573                 return 0;
574                 /*
575                  *   Step 7: Check for unexpected packet types
576                  *      If (S.is_server and P.type == CloseReq)
577                  *          or (S.is_server and P.type == Response)
578                  *          or (S.is_client and P.type == Request)
579                  *          or (S.state == RESPOND and P.type == Data),
580                  *        Send Sync packet acknowledging P.seqno
581                  *        Drop packet and return
582                  */
583         } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
584                     (dh->dccph_type == DCCP_PKT_RESPONSE ||
585                      dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
586                     (dp->dccps_role == DCCP_ROLE_CLIENT &&
587                      dh->dccph_type == DCCP_PKT_REQUEST) ||
588                     (sk->sk_state == DCCP_RESPOND &&
589                      dh->dccph_type == DCCP_PKT_DATA)) {
590                 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
591                 goto discard;
592         } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
593                 dccp_rcv_closereq(sk, skb);
594                 goto discard;
595         } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
596                 dccp_rcv_close(sk, skb);
597                 return 0;
598         }
599
600         switch (sk->sk_state) {
601         case DCCP_CLOSED:
602                 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
603                 return 1;
604
605         case DCCP_REQUESTING:
606                 /* FIXME: do congestion control initialization */
607
608                 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
609                 if (queued >= 0)
610                         return queued;
611
612                 __kfree_skb(skb);
613                 return 0;
614
615         case DCCP_RESPOND:
616         case DCCP_PARTOPEN:
617                 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
618                                                                  dh, len);
619                 break;
620         }
621
622         if (dh->dccph_type == DCCP_PKT_ACK ||
623             dh->dccph_type == DCCP_PKT_DATAACK) {
624                 switch (old_state) {
625                 case DCCP_PARTOPEN:
626                         sk->sk_state_change(sk);
627                         sk_wake_async(sk, 0, POLL_OUT);
628                         break;
629                 }
630         } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
631                 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
632                 goto discard;
633         }
634
635         if (!queued) {
636 discard:
637                 __kfree_skb(skb);
638         }
639         return 0;
640 }
641
642 EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
643
644 /**
645  *  dccp_sample_rtt  -  Validate and finalise computation of RTT sample
646  *  @delta:     number of microseconds between packet and acknowledgment
647  *  The routine is kept generic to work in different contexts. It should be
648  *  called immediately when the ACK used for the RTT sample arrives.
649  */
650 u32 dccp_sample_rtt(struct sock *sk, long delta)
651 {
652         /* dccpor_elapsed_time is either zeroed out or set and > 0 */
653         delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
654
655         if (unlikely(delta <= 0)) {
656                 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
657                 return DCCP_SANE_RTT_MIN;
658         }
659         if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
660                 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
661                 return DCCP_SANE_RTT_MAX;
662         }
663
664         return delta;
665 }
666
667 EXPORT_SYMBOL_GPL(dccp_sample_rtt);