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