patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / sctp / sm_statefuns.c
1 /* SCTP kernel reference Implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2002 Intel Corp.
6  * Copyright (c) 2002      Nokia Corp.
7  *
8  * This file is part of the SCTP kernel reference Implementation
9  *
10  * This is part of the SCTP Linux Kernel Reference Implementation.
11  *
12  * These are the state functions for the state machine.
13  *
14  * The SCTP reference implementation is free software;
15  * you can redistribute it and/or modify it under the terms of
16  * the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * The SCTP reference implementation is distributed in the hope that it
21  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22  *                 ************************
23  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  * See the GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with GNU CC; see the file COPYING.  If not, write to
28  * the Free Software Foundation, 59 Temple Place - Suite 330,
29  * Boston, MA 02111-1307, USA.
30  *
31  * Please send any bug reports or fixes you make to the
32  * email address(es):
33  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
34  *
35  * Or submit a bug report through the following website:
36  *    http://www.sf.net/projects/lksctp
37  *
38  * Written or modified by:
39  *    La Monte H.P. Yarroll <piggy@acm.org>
40  *    Karl Knutson          <karl@athena.chicago.il.us>
41  *    Mathew Kotowsky       <kotowsky@sctp.org>
42  *    Sridhar Samudrala     <samudrala@us.ibm.com>
43  *    Jon Grimm             <jgrimm@us.ibm.com>
44  *    Hui Huang             <hui.huang@nokia.com>
45  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
46  *    Daisy Chang           <daisyc@us.ibm.com>
47  *    Ardelle Fan           <ardelle.fan@intel.com>
48  *    Ryan Layer            <rmlayer@us.ibm.com>
49  *    Kevin Gao             <kevin.gao@intel.com>
50  *
51  * Any bugs reported given to us we will try to fix... any fixes shared will
52  * be incorporated into the next SCTP release.
53  */
54
55 #include <linux/types.h>
56 #include <linux/kernel.h>
57 #include <linux/ip.h>
58 #include <linux/ipv6.h>
59 #include <linux/net.h>
60 #include <linux/inet.h>
61 #include <net/sock.h>
62 #include <net/inet_ecn.h>
63 #include <linux/skbuff.h>
64 #include <net/sctp/sctp.h>
65 #include <net/sctp/sm.h>
66 #include <net/sctp/structs.h>
67
68 /**********************************************************
69  * These are the state functions for handling chunk events.
70  **********************************************************/
71
72 /*
73  * Process the final SHUTDOWN COMPLETE.
74  *
75  * Section: 4 (C) (diagram), 9.2
76  * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
77  * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
78  * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
79  * should stop the T2-shutdown timer and remove all knowledge of the
80  * association (and thus the association enters the CLOSED state).
81  *
82  * Verification Tag: 8.5.1(C)
83  * C) Rules for packet carrying SHUTDOWN COMPLETE:
84  * ...
85  * - The receiver of a SHUTDOWN COMPLETE shall accept the packet if the
86  *   Verification Tag field of the packet matches its own tag OR it is
87  *   set to its peer's tag and the T bit is set in the Chunk Flags.
88  *   Otherwise, the receiver MUST silently discard the packet and take
89  *   no further action. An endpoint MUST ignore the SHUTDOWN COMPLETE if
90  *   it is not in the SHUTDOWN-ACK-SENT state.
91  *
92  * Inputs
93  * (endpoint, asoc, chunk)
94  *
95  * Outputs
96  * (asoc, reply_msg, msg_up, timers, counters)
97  *
98  * The return value is the disposition of the chunk.
99  */
100 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
101                                   const struct sctp_association *asoc,
102                                   const sctp_subtype_t type,
103                                   void *arg,
104                                   sctp_cmd_seq_t *commands)
105 {
106         struct sctp_chunk *chunk = arg;
107         struct sctp_ulpevent *ev;
108
109         /* RFC 2960 6.10 Bundling
110          *
111          * An endpoint MUST NOT bundle INIT, INIT ACK or
112          * SHUTDOWN COMPLETE with any other chunks.
113          */
114         if (!chunk->singleton)
115                 return SCTP_DISPOSITION_VIOLATION;
116
117         if (!sctp_vtag_verify_either(chunk, asoc))
118                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
119
120         /* RFC 2960 10.2 SCTP-to-ULP
121          *
122          * H) SHUTDOWN COMPLETE notification
123          *
124          * When SCTP completes the shutdown procedures (section 9.2) this
125          * notification is passed to the upper layer.
126          */
127         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
128                                              0, 0, 0, GFP_ATOMIC);
129         if (!ev)
130                 goto nomem;
131
132         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
133
134         /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
135          * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
136          * not the chunk should be discarded. If the endpoint is in
137          * the SHUTDOWN-ACK-SENT state the endpoint should stop the
138          * T2-shutdown timer and remove all knowledge of the
139          * association (and thus the association enters the CLOSED
140          * state).
141          */
142         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
143                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
144
145         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
146                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
147
148         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
149                         SCTP_STATE(SCTP_STATE_CLOSED));
150
151         SCTP_INC_STATS(SctpShutdowns);
152         SCTP_DEC_STATS(SctpCurrEstab);
153
154         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
155
156         return SCTP_DISPOSITION_DELETE_TCB;
157
158 nomem:
159         return SCTP_DISPOSITION_NOMEM;
160 }
161
162 /*
163  * Respond to a normal INIT chunk.
164  * We are the side that is being asked for an association.
165  *
166  * Section: 5.1 Normal Establishment of an Association, B
167  * B) "Z" shall respond immediately with an INIT ACK chunk.  The
168  *    destination IP address of the INIT ACK MUST be set to the source
169  *    IP address of the INIT to which this INIT ACK is responding.  In
170  *    the response, besides filling in other parameters, "Z" must set the
171  *    Verification Tag field to Tag_A, and also provide its own
172  *    Verification Tag (Tag_Z) in the Initiate Tag field.
173  *
174  * Verification Tag: Must be 0. 
175  *
176  * Inputs
177  * (endpoint, asoc, chunk)
178  *
179  * Outputs
180  * (asoc, reply_msg, msg_up, timers, counters)
181  *
182  * The return value is the disposition of the chunk.
183  */
184 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
185                                         const struct sctp_association *asoc,
186                                         const sctp_subtype_t type,
187                                         void *arg,
188                                         sctp_cmd_seq_t *commands)
189 {
190         struct sctp_chunk *chunk = arg;
191         struct sctp_chunk *repl;
192         struct sctp_association *new_asoc;
193         struct sctp_chunk *err_chunk;
194         struct sctp_packet *packet;
195         sctp_unrecognized_param_t *unk_param;
196         struct sock *sk;
197         int len;
198
199         /* 6.10 Bundling
200          * An endpoint MUST NOT bundle INIT, INIT ACK or
201          * SHUTDOWN COMPLETE with any other chunks.
202          */
203         if (!chunk->singleton)
204                 return SCTP_DISPOSITION_VIOLATION;
205
206         /* If the packet is an OOTB packet which is temporarily on the
207          * control endpoint, respond with an ABORT.
208          */
209         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
210                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
211
212         sk = ep->base.sk;
213         /* If the endpoint is not listening or if the number of associations
214          * on the TCP-style socket exceed the max backlog, respond with an
215          * ABORT.
216          */
217         if (!sctp_sstate(sk, LISTENING) ||
218             (sctp_style(sk, TCP) &&
219              (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)))
220                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
221
222         /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
223          * Tag. 
224          */
225         if (chunk->sctp_hdr->vtag != 0)
226                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
227
228         /* Verify the INIT chunk before processing it. */
229         err_chunk = NULL;
230         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
231                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
232                               &err_chunk)) {
233                 /* This chunk contains fatal error. It is to be discarded.
234                  * Send an ABORT, with causes if there is any.
235                  */
236                 if (err_chunk) {
237                         packet = sctp_abort_pkt_new(ep, asoc, arg,
238                                         (__u8 *)(err_chunk->chunk_hdr) +
239                                         sizeof(sctp_chunkhdr_t),
240                                         ntohs(err_chunk->chunk_hdr->length) -
241                                         sizeof(sctp_chunkhdr_t));
242
243                         sctp_chunk_free(err_chunk);
244
245                         if (packet) {
246                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
247                                                 SCTP_PACKET(packet));
248                                 SCTP_INC_STATS(SctpOutCtrlChunks);
249                                 return SCTP_DISPOSITION_CONSUME;
250                         } else {
251                                 return SCTP_DISPOSITION_NOMEM;
252                         }
253                 } else {
254                         return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
255                                                     commands);
256                 }
257         }
258
259         /* Grab the INIT header.  */
260         chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
261
262         /* Tag the variable length parameters.  */
263         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
264
265         new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
266         if (!new_asoc)
267                 goto nomem;
268
269         /* The call, sctp_process_init(), can fail on memory allocation.  */
270         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
271                                sctp_source(chunk),
272                                (sctp_init_chunk_t *)chunk->chunk_hdr,
273                                GFP_ATOMIC))
274                 goto nomem_init;
275
276         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
277
278         /* B) "Z" shall respond immediately with an INIT ACK chunk.  */
279
280         /* If there are errors need to be reported for unknown parameters,
281          * make sure to reserve enough room in the INIT ACK for them.
282          */
283         len = 0;
284         if (err_chunk)
285                 len = ntohs(err_chunk->chunk_hdr->length) -
286                         sizeof(sctp_chunkhdr_t);
287
288         if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
289                 goto nomem_ack;
290
291         repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
292         if (!repl)
293                 goto nomem_ack;
294
295         /* If there are errors need to be reported for unknown parameters,
296          * include them in the outgoing INIT ACK as "Unrecognized parameter"
297          * parameter.
298          */
299         if (err_chunk) {
300                 /* Get the "Unrecognized parameter" parameter(s) out of the
301                  * ERROR chunk generated by sctp_verify_init(). Since the
302                  * error cause code for "unknown parameter" and the
303                  * "Unrecognized parameter" type is the same, we can
304                  * construct the parameters in INIT ACK by copying the
305                  * ERROR causes over.
306                  */
307                 unk_param = (sctp_unrecognized_param_t *)
308                             ((__u8 *)(err_chunk->chunk_hdr) +
309                             sizeof(sctp_chunkhdr_t));
310                 /* Replace the cause code with the "Unrecognized parameter"
311                  * parameter type.
312                  */
313                 sctp_addto_chunk(repl, len, unk_param);
314                 sctp_chunk_free(err_chunk);
315         }
316
317         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
318
319         /*
320          * Note:  After sending out INIT ACK with the State Cookie parameter,
321          * "Z" MUST NOT allocate any resources, nor keep any states for the
322          * new association.  Otherwise, "Z" will be vulnerable to resource
323          * attacks.
324          */
325         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
326
327         return SCTP_DISPOSITION_DELETE_TCB;
328
329 nomem_ack:
330         if (err_chunk)
331                 sctp_chunk_free(err_chunk);
332 nomem_init:
333         sctp_association_free(new_asoc);
334 nomem:
335         return SCTP_DISPOSITION_NOMEM;
336 }
337
338 /*
339  * Respond to a normal INIT ACK chunk.
340  * We are the side that is initiating the association.
341  *
342  * Section: 5.1 Normal Establishment of an Association, C
343  * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
344  *    timer and leave COOKIE-WAIT state. "A" shall then send the State
345  *    Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
346  *    the T1-cookie timer, and enter the COOKIE-ECHOED state.
347  *
348  *    Note: The COOKIE ECHO chunk can be bundled with any pending outbound
349  *    DATA chunks, but it MUST be the first chunk in the packet and
350  *    until the COOKIE ACK is returned the sender MUST NOT send any
351  *    other packets to the peer.
352  *
353  * Verification Tag: 3.3.3
354  *   If the value of the Initiate Tag in a received INIT ACK chunk is
355  *   found to be 0, the receiver MUST treat it as an error and close the
356  *   association by transmitting an ABORT.
357  *
358  * Inputs
359  * (endpoint, asoc, chunk)
360  *
361  * Outputs
362  * (asoc, reply_msg, msg_up, timers, counters)
363  *
364  * The return value is the disposition of the chunk.
365  */
366 sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
367                                        const struct sctp_association *asoc,
368                                        const sctp_subtype_t type,
369                                        void *arg,
370                                        sctp_cmd_seq_t *commands)
371 {
372         struct sctp_chunk *chunk = arg;
373         sctp_init_chunk_t *initchunk;
374         __u32 init_tag;
375         struct sctp_chunk *err_chunk;
376         struct sctp_packet *packet;
377         sctp_disposition_t ret;
378
379         /* 6.10 Bundling
380          * An endpoint MUST NOT bundle INIT, INIT ACK or
381          * SHUTDOWN COMPLETE with any other chunks.
382          */
383         if (!chunk->singleton)
384                 return SCTP_DISPOSITION_VIOLATION;
385
386         if (!sctp_vtag_verify(chunk, asoc))
387                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
388
389         /* Grab the INIT header.  */
390         chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
391
392         init_tag = ntohl(chunk->subh.init_hdr->init_tag);
393
394         /* Verification Tag: 3.3.3
395          *   If the value of the Initiate Tag in a received INIT ACK
396          *   chunk is found to be 0, the receiver MUST treat it as an
397          *   error and close the association by transmitting an ABORT.
398          */
399         if (!init_tag) {
400                 struct sctp_chunk *reply = sctp_make_abort(asoc, chunk, 0);
401                 if (!reply)
402                         goto nomem;
403
404                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
405                 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
406                                 SCTP_STATE(SCTP_STATE_CLOSED));
407                 SCTP_INC_STATS(SctpAborteds);
408                 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
409                 return SCTP_DISPOSITION_DELETE_TCB;
410         }
411
412         /* Verify the INIT chunk before processing it. */
413         err_chunk = NULL;
414         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
415                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
416                               &err_chunk)) {
417
418                 SCTP_INC_STATS(SctpAborteds);
419
420                 /* This chunk contains fatal error. It is to be discarded.
421                  * Send an ABORT, with causes if there is any.
422                  */
423                 if (err_chunk) {
424                         packet = sctp_abort_pkt_new(ep, asoc, arg,
425                                         (__u8 *)(err_chunk->chunk_hdr) +
426                                         sizeof(sctp_chunkhdr_t),
427                                         ntohs(err_chunk->chunk_hdr->length) -
428                                         sizeof(sctp_chunkhdr_t));
429
430                         sctp_chunk_free(err_chunk);
431
432                         if (packet) {
433                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
434                                                 SCTP_PACKET(packet));
435                                 SCTP_INC_STATS(SctpOutCtrlChunks);
436                                 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
437                                                 SCTP_STATE(SCTP_STATE_CLOSED));
438                                 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB,
439                                                 SCTP_NULL());
440                                 return SCTP_DISPOSITION_CONSUME;
441                         } else {
442                                 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
443                                                 SCTP_STATE(SCTP_STATE_CLOSED));
444                                 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB,
445                                                 SCTP_NULL());
446                                 return SCTP_DISPOSITION_NOMEM;
447                         }
448                 } else {
449                         ret = sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
450                                                    commands);
451                         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
452                                         SCTP_STATE(SCTP_STATE_CLOSED));
453                         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB,
454                                         SCTP_NULL());
455                         return ret;
456                 }
457         }
458
459         /* Tag the variable length parameters.  Note that we never
460          * convert the parameters in an INIT chunk.
461          */
462         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
463
464         initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
465
466         sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
467                         SCTP_PEER_INIT(initchunk));
468
469         /* 5.1 C) "A" shall stop the T1-init timer and leave
470          * COOKIE-WAIT state.  "A" shall then ... start the T1-cookie
471          * timer, and enter the COOKIE-ECHOED state.
472          */
473         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
474                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
475         sctp_add_cmd_sf(commands, SCTP_CMD_COUNTER_RESET,
476                         SCTP_COUNTER(SCTP_COUNTER_INIT_ERROR));
477         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
478                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
479         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
480                         SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
481
482         /* 5.1 C) "A" shall then send the State Cookie received in the
483          * INIT ACK chunk in a COOKIE ECHO chunk, ...
484          */
485         /* If there is any errors to report, send the ERROR chunk generated
486          * for unknown parameters as well.
487          */
488         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
489                         SCTP_CHUNK(err_chunk));
490
491         return SCTP_DISPOSITION_CONSUME;
492
493 nomem:
494         return SCTP_DISPOSITION_NOMEM;
495 }
496
497 /*
498  * Respond to a normal COOKIE ECHO chunk.
499  * We are the side that is being asked for an association.
500  *
501  * Section: 5.1 Normal Establishment of an Association, D
502  * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
503  *    with a COOKIE ACK chunk after building a TCB and moving to
504  *    the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
505  *    any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
506  *    chunk MUST be the first chunk in the packet.
507  *
508  *   IMPLEMENTATION NOTE: An implementation may choose to send the
509  *   Communication Up notification to the SCTP user upon reception
510  *   of a valid COOKIE ECHO chunk.
511  *
512  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
513  * D) Rules for packet carrying a COOKIE ECHO
514  *
515  * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
516  *   Initial Tag received in the INIT ACK.
517  *
518  * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
519  *
520  * Inputs
521  * (endpoint, asoc, chunk)
522  *
523  * Outputs
524  * (asoc, reply_msg, msg_up, timers, counters)
525  *
526  * The return value is the disposition of the chunk.
527  */
528 sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
529                                       const struct sctp_association *asoc,
530                                       const sctp_subtype_t type, void *arg,
531                                       sctp_cmd_seq_t *commands)
532 {
533         struct sctp_chunk *chunk = arg;
534         struct sctp_association *new_asoc;
535         sctp_init_chunk_t *peer_init;
536         struct sctp_chunk *repl;
537         struct sctp_ulpevent *ev;
538         int error = 0;
539         struct sctp_chunk *err_chk_p;
540
541         /* If the packet is an OOTB packet which is temporarily on the
542          * control endpoint, respond with an ABORT.
543          */
544         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
545                 return sctp_sf_ootb(ep, asoc, type, arg, commands);
546
547         /* "Decode" the chunk.  We have no optional parameters so we
548          * are in good shape.
549          */
550         chunk->subh.cookie_hdr =
551                 (struct sctp_signed_cookie *)chunk->skb->data;
552         skb_pull(chunk->skb,
553                  ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t));
554
555         /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
556          * "Z" will reply with a COOKIE ACK chunk after building a TCB
557          * and moving to the ESTABLISHED state.
558          */
559         new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
560                                       &err_chk_p);
561
562         /* FIXME:
563          * If the re-build failed, what is the proper error path
564          * from here?
565          *
566          * [We should abort the association. --piggy]
567          */
568         if (!new_asoc) {
569                 /* FIXME: Several errors are possible.  A bad cookie should
570                  * be silently discarded, but think about logging it too.
571                  */
572                 switch (error) {
573                 case -SCTP_IERROR_NOMEM:
574                         goto nomem;
575
576                 case -SCTP_IERROR_STALE_COOKIE:
577                         sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
578                                                    err_chk_p);
579                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
580
581                 case -SCTP_IERROR_BAD_SIG:
582                 default:
583                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
584                 };
585         }
586
587         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
588         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
589                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
590         SCTP_INC_STATS(SctpCurrEstab);
591         SCTP_INC_STATS(SctpPassiveEstabs);
592         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
593
594         if (new_asoc->autoclose)
595                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
596                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
597
598         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
599
600         /* Re-build the bind address for the association is done in
601          * the sctp_unpack_cookie() already.
602          */
603         /* This is a brand-new association, so these are not yet side
604          * effects--it is safe to run them here.
605          */
606         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
607
608         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
609                                &chunk->subh.cookie_hdr->c.peer_addr,
610                                peer_init, GFP_ATOMIC))
611                 goto nomem_init;
612
613         repl = sctp_make_cookie_ack(new_asoc, chunk);
614         if (!repl)
615                 goto nomem_repl;
616
617         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
618
619         /* RFC 2960 5.1 Normal Establishment of an Association
620          *
621          * D) IMPLEMENTATION NOTE: An implementation may choose to
622          * send the Communication Up notification to the SCTP user
623          * upon reception of a valid COOKIE ECHO chunk.
624          */
625         ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
626                                              new_asoc->c.sinit_num_ostreams,
627                                              new_asoc->c.sinit_max_instreams,
628                                              GFP_ATOMIC);
629         if (!ev)
630                 goto nomem_ev;
631
632         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
633
634         return SCTP_DISPOSITION_CONSUME;
635
636 nomem_ev:
637         sctp_chunk_free(repl);
638 nomem_repl:
639 nomem_init:
640         sctp_association_free(new_asoc);
641 nomem:
642         return SCTP_DISPOSITION_NOMEM;
643 }
644
645 /*
646  * Respond to a normal COOKIE ACK chunk.
647  * We are the side that is being asked for an association.
648  *
649  * RFC 2960 5.1 Normal Establishment of an Association
650  *
651  * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
652  *    COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
653  *    timer. It may also notify its ULP about the successful
654  *    establishment of the association with a Communication Up
655  *    notification (see Section 10).
656  *
657  * Verification Tag:
658  * Inputs
659  * (endpoint, asoc, chunk)
660  *
661  * Outputs
662  * (asoc, reply_msg, msg_up, timers, counters)
663  *
664  * The return value is the disposition of the chunk.
665  */
666 sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
667                                       const struct sctp_association *asoc,
668                                       const sctp_subtype_t type, void *arg,
669                                       sctp_cmd_seq_t *commands)
670 {
671         struct sctp_chunk *chunk = arg;
672         struct sctp_ulpevent *ev;
673
674         if (!sctp_vtag_verify(chunk, asoc))
675                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
676
677         /* RFC 2960 5.1 Normal Establishment of an Association
678          *
679          * E) Upon reception of the COOKIE ACK, endpoint "A" will move
680          * from the COOKIE-ECHOED state to the ESTABLISHED state,
681          * stopping the T1-cookie timer.
682          */
683         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
684                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
685         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
686                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
687         SCTP_INC_STATS(SctpCurrEstab);
688         SCTP_INC_STATS(SctpActiveEstabs);
689         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
690         if (asoc->autoclose)
691                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
692                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
693         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
694
695         /* It may also notify its ULP about the successful
696          * establishment of the association with a Communication Up
697          * notification (see Section 10).
698          */
699         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
700                                              0, asoc->c.sinit_num_ostreams,
701                                              asoc->c.sinit_max_instreams,
702                                              GFP_ATOMIC);
703
704         if (!ev)
705                 goto nomem;
706
707         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
708
709         return SCTP_DISPOSITION_CONSUME;
710 nomem:
711         return SCTP_DISPOSITION_NOMEM;
712 }
713
714 /* Generate and sendout a heartbeat packet.  */
715 sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
716                                      const struct sctp_association *asoc,
717                                      const sctp_subtype_t type,
718                                      void *arg,
719                                      sctp_cmd_seq_t *commands)
720 {
721         struct sctp_transport *transport = (struct sctp_transport *) arg;
722         struct sctp_chunk *reply;
723         sctp_sender_hb_info_t hbinfo;
724         size_t paylen = 0;
725
726         hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
727         hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
728         hbinfo.daddr = transport->ipaddr;
729         hbinfo.sent_at = jiffies;
730
731         /* Send a heartbeat to our peer.  */
732         paylen = sizeof(sctp_sender_hb_info_t);
733         reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
734         if (!reply)
735                 return SCTP_DISPOSITION_NOMEM;
736
737         /* Set rto_pending indicating that an RTT measurement
738          * is started with this heartbeat chunk.
739          */
740         sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
741                         SCTP_TRANSPORT(transport));
742
743         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
744         return SCTP_DISPOSITION_CONSUME;
745 }
746
747 /* Generate a HEARTBEAT packet on the given transport.  */
748 sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
749                                         const struct sctp_association *asoc,
750                                         const sctp_subtype_t type,
751                                         void *arg,
752                                         sctp_cmd_seq_t *commands)
753 {
754         struct sctp_transport *transport = (struct sctp_transport *) arg;
755
756         if (asoc->overall_error_count > asoc->max_retrans) {
757                 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
758                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
759                                 SCTP_U32(SCTP_ERROR_NO_ERROR));
760                 SCTP_INC_STATS(SctpAborteds);
761                 SCTP_DEC_STATS(SctpCurrEstab);
762                 return SCTP_DISPOSITION_DELETE_TCB;
763         }
764
765         /* Section 3.3.5.
766          * The Sender-specific Heartbeat Info field should normally include
767          * information about the sender's current time when this HEARTBEAT
768          * chunk is sent and the destination transport address to which this
769          * HEARTBEAT is sent (see Section 8.3).
770          */
771
772         if (transport->hb_allowed) {
773                 if (SCTP_DISPOSITION_NOMEM ==
774                                 sctp_sf_heartbeat(ep, asoc, type, arg,
775                                                   commands))
776                         return SCTP_DISPOSITION_NOMEM;
777                 /* Set transport error counter and association error counter
778                  * when sending heartbeat.
779                  */
780                 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
781                                 SCTP_TRANSPORT(transport));
782         }
783         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
784                         SCTP_TRANSPORT(transport));
785
786         return SCTP_DISPOSITION_CONSUME;
787 }
788
789 /*
790  * Process an heartbeat request.
791  *
792  * Section: 8.3 Path Heartbeat
793  * The receiver of the HEARTBEAT should immediately respond with a
794  * HEARTBEAT ACK that contains the Heartbeat Information field copied
795  * from the received HEARTBEAT chunk.
796  *
797  * Verification Tag:  8.5 Verification Tag [Normal verification]
798  * When receiving an SCTP packet, the endpoint MUST ensure that the
799  * value in the Verification Tag field of the received SCTP packet
800  * matches its own Tag. If the received Verification Tag value does not
801  * match the receiver's own tag value, the receiver shall silently
802  * discard the packet and shall not process it any further except for
803  * those cases listed in Section 8.5.1 below.
804  *
805  * Inputs
806  * (endpoint, asoc, chunk)
807  *
808  * Outputs
809  * (asoc, reply_msg, msg_up, timers, counters)
810  *
811  * The return value is the disposition of the chunk.
812  */
813 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
814                                     const struct sctp_association *asoc,
815                                     const sctp_subtype_t type,
816                                     void *arg,
817                                     sctp_cmd_seq_t *commands)
818 {
819         struct sctp_chunk *chunk = arg;
820         struct sctp_chunk *reply;
821         size_t paylen = 0;
822
823         if (!sctp_vtag_verify(chunk, asoc))
824                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
825
826         /* 8.3 The receiver of the HEARTBEAT should immediately
827          * respond with a HEARTBEAT ACK that contains the Heartbeat
828          * Information field copied from the received HEARTBEAT chunk.
829          */
830         chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
831         paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
832         skb_pull(chunk->skb, paylen);
833
834         reply = sctp_make_heartbeat_ack(asoc, chunk,
835                                         chunk->subh.hb_hdr, paylen);
836         if (!reply)
837                 goto nomem;
838
839         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
840         return SCTP_DISPOSITION_CONSUME;
841
842 nomem:
843         return SCTP_DISPOSITION_NOMEM;
844 }
845
846 /*
847  * Process the returning HEARTBEAT ACK.
848  *
849  * Section: 8.3 Path Heartbeat
850  * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
851  * should clear the error counter of the destination transport
852  * address to which the HEARTBEAT was sent, and mark the destination
853  * transport address as active if it is not so marked. The endpoint may
854  * optionally report to the upper layer when an inactive destination
855  * address is marked as active due to the reception of the latest
856  * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
857  * clear the association overall error count as well (as defined
858  * in section 8.1).
859  *
860  * The receiver of the HEARTBEAT ACK should also perform an RTT
861  * measurement for that destination transport address using the time
862  * value carried in the HEARTBEAT ACK chunk.
863  *
864  * Verification Tag:  8.5 Verification Tag [Normal verification]
865  *
866  * Inputs
867  * (endpoint, asoc, chunk)
868  *
869  * Outputs
870  * (asoc, reply_msg, msg_up, timers, counters)
871  *
872  * The return value is the disposition of the chunk.
873  */
874 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
875                                         const struct sctp_association *asoc,
876                                         const sctp_subtype_t type,
877                                         void *arg,
878                                         sctp_cmd_seq_t *commands)
879 {
880         struct sctp_chunk *chunk = arg;
881         union sctp_addr from_addr;
882         struct sctp_transport *link;
883         sctp_sender_hb_info_t *hbinfo;
884         unsigned long max_interval;
885
886         if (!sctp_vtag_verify(chunk, asoc))
887                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
888
889         hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
890         from_addr = hbinfo->daddr;
891         link = sctp_assoc_lookup_paddr(asoc, &from_addr);
892
893         /* This should never happen, but lets log it if so.  */
894         if (!link) {
895                 printk(KERN_WARNING
896                        "%s: Could not find address %d.%d.%d.%d\n",
897                        __FUNCTION__, NIPQUAD(from_addr.v4.sin_addr));
898                 return SCTP_DISPOSITION_DISCARD;
899         }
900
901         max_interval = link->hb_interval + link->rto;
902
903         /* Check if the timestamp looks valid.  */
904         if (time_after(hbinfo->sent_at, jiffies) ||
905             time_after(jiffies, hbinfo->sent_at + max_interval)) {
906                 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp"
907                                   "received for transport: %p\n",
908                                    __FUNCTION__, link);
909                 return SCTP_DISPOSITION_DISCARD;
910         }
911
912         /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
913          * the HEARTBEAT should clear the error counter of the
914          * destination transport address to which the HEARTBEAT was
915          * sent and mark the destination transport address as active if
916          * it is not so marked.
917          */
918         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
919
920         return SCTP_DISPOSITION_CONSUME;
921 }
922
923 /* Helper function to send out an abort for the restart
924  * condition.
925  */
926 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
927                                       struct sctp_chunk *init,
928                                       sctp_cmd_seq_t *commands)
929 {
930         int len;
931         struct sctp_packet *pkt;
932         union sctp_addr_param *addrparm;
933         struct sctp_errhdr *errhdr;
934         struct sctp_endpoint *ep;
935         char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
936         struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
937
938         /* Build the error on the stack.   We are way to malloc crazy
939          * throughout the code today.
940          */
941         errhdr = (struct sctp_errhdr *)buffer;
942         addrparm = (union sctp_addr_param *)errhdr->variable;
943
944         /* Copy into a parm format. */
945         len = af->to_addr_param(ssa, addrparm);
946         len += sizeof(sctp_errhdr_t);
947
948         errhdr->cause = SCTP_ERROR_RESTART;
949         errhdr->length = htons(len);
950
951         /* Assign to the control socket. */
952         ep = sctp_sk((sctp_get_ctl_sock()))->ep;
953
954         /* Association is NULL since this may be a restart attack and we
955          * want to send back the attacker's vtag.
956          */
957         pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
958
959         if (!pkt)
960                 goto out;
961         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
962
963         SCTP_INC_STATS(SctpOutCtrlChunks);
964
965         /* Discard the rest of the inbound packet. */
966         sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
967
968 out:
969         /* Even if there is no memory, treat as a failure so
970          * the packet will get dropped.
971          */
972         return 0;
973 }
974
975 /* A restart is occurring, check to make sure no new addresses
976  * are being added as we may be under a takeover attack.
977  */
978 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
979                                        const struct sctp_association *asoc,
980                                        struct sctp_chunk *init,
981                                        sctp_cmd_seq_t *commands)
982 {
983         struct sctp_transport *new_addr, *addr;
984         struct list_head *pos, *pos2;
985         int found;
986
987         /* Implementor's Guide - Sectin 5.2.2
988          * ...
989          * Before responding the endpoint MUST check to see if the
990          * unexpected INIT adds new addresses to the association. If new
991          * addresses are added to the association, the endpoint MUST respond
992          * with an ABORT..
993          */
994
995         /* Search through all current addresses and make sure
996          * we aren't adding any new ones.
997          */
998         new_addr = 0;
999         found = 0;
1000
1001         list_for_each(pos, &new_asoc->peer.transport_addr_list) {
1002                 new_addr = list_entry(pos, struct sctp_transport, transports);
1003                 found = 0;
1004                 list_for_each(pos2, &asoc->peer.transport_addr_list) {
1005                         addr = list_entry(pos2, struct sctp_transport,
1006                                           transports);
1007                         if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1008                                                 &addr->ipaddr)) {
1009                                 found = 1;
1010                                 break;
1011                         }
1012                 }
1013                 if (!found)
1014                         break;
1015         }
1016
1017         /* If a new address was added, ABORT the sender. */
1018         if (!found && new_addr) {
1019                 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1020         }
1021
1022         /* Return success if all addresses were found. */
1023         return found;
1024 }
1025
1026 /* Populate the verification/tie tags based on overlapping INIT
1027  * scenario.
1028  *
1029  * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1030  */
1031 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1032                                   const struct sctp_association *asoc)
1033 {
1034         switch (asoc->state) {
1035
1036         /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1037
1038         case SCTP_STATE_COOKIE_WAIT:
1039                 new_asoc->c.my_vtag     = asoc->c.my_vtag;
1040                 new_asoc->c.my_ttag     = asoc->c.my_vtag;
1041                 new_asoc->c.peer_ttag   = 0;
1042                 break;
1043
1044         case SCTP_STATE_COOKIE_ECHOED:
1045                 new_asoc->c.my_vtag     = asoc->c.my_vtag;
1046                 new_asoc->c.my_ttag     = asoc->c.my_vtag;
1047                 new_asoc->c.peer_ttag   = asoc->c.peer_vtag;
1048                 break;
1049
1050         /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1051          * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1052          */
1053         default:
1054                 new_asoc->c.my_ttag   = asoc->c.my_vtag;
1055                 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1056                 break;
1057         };
1058
1059         /* Other parameters for the endpoint SHOULD be copied from the
1060          * existing parameters of the association (e.g. number of
1061          * outbound streams) into the INIT ACK and cookie.
1062          */
1063         new_asoc->rwnd                  = asoc->rwnd;
1064         new_asoc->c.sinit_num_ostreams  = asoc->c.sinit_num_ostreams;
1065         new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1066         new_asoc->c.initial_tsn         = asoc->c.initial_tsn;
1067 }
1068
1069 /*
1070  * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1071  * handling action.
1072  *
1073  * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1074  *
1075  * Returns value representing action to be taken.   These action values
1076  * correspond to Action/Description values in RFC 2960, Table 2.
1077  */
1078 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1079                                  const struct sctp_association *asoc)
1080 {
1081         /* In this case, the peer may have restarted.  */
1082         if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1083             (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1084             (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1085             (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1086                 return 'A';
1087
1088         /* Collision case B. */
1089         if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1090             ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1091              (0 == asoc->c.peer_vtag))) {
1092                 return 'B';
1093         }
1094
1095         /* Collision case D. */
1096         if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1097             (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1098                 return 'D';
1099
1100         /* Collision case C. */
1101         if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1102             (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1103             (0 == new_asoc->c.my_ttag) &&
1104             (0 == new_asoc->c.peer_ttag))
1105                 return 'C';
1106
1107         /* No match to any of the special cases; discard this packet. */
1108         return 'E';
1109 }
1110
1111 /* Common helper routine for both duplicate and simulataneous INIT
1112  * chunk handling.
1113  */
1114 static sctp_disposition_t sctp_sf_do_unexpected_init(
1115         const struct sctp_endpoint *ep,
1116         const struct sctp_association *asoc,
1117         const sctp_subtype_t type,
1118         void *arg, sctp_cmd_seq_t *commands)
1119 {
1120         sctp_disposition_t retval;
1121         struct sctp_chunk *chunk = arg;
1122         struct sctp_chunk *repl;
1123         struct sctp_association *new_asoc;
1124         struct sctp_chunk *err_chunk;
1125         struct sctp_packet *packet;
1126         sctp_unrecognized_param_t *unk_param;
1127         int len;
1128
1129         /* 6.10 Bundling
1130          * An endpoint MUST NOT bundle INIT, INIT ACK or
1131          * SHUTDOWN COMPLETE with any other chunks.
1132          */
1133         if (!chunk->singleton)
1134                 return SCTP_DISPOSITION_VIOLATION;
1135
1136         /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1137          * Tag. 
1138          */
1139         if (chunk->sctp_hdr->vtag != 0)
1140                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1141
1142         /* Grab the INIT header.  */
1143         chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1144
1145         /* Tag the variable length parameters.  */
1146         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1147
1148         /* Verify the INIT chunk before processing it. */
1149         err_chunk = NULL;
1150         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1151                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1152                               &err_chunk)) {
1153                 /* This chunk contains fatal error. It is to be discarded.
1154                  * Send an ABORT, with causes if there is any.
1155                  */
1156                 if (err_chunk) {
1157                         packet = sctp_abort_pkt_new(ep, asoc, arg,
1158                                         (__u8 *)(err_chunk->chunk_hdr) +
1159                                         sizeof(sctp_chunkhdr_t),
1160                                         ntohs(err_chunk->chunk_hdr->length) -
1161                                         sizeof(sctp_chunkhdr_t));
1162
1163                         if (packet) {
1164                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1165                                                 SCTP_PACKET(packet));
1166                                 SCTP_INC_STATS(SctpOutCtrlChunks);
1167                                 retval = SCTP_DISPOSITION_CONSUME;
1168                         } else {
1169                                 retval = SCTP_DISPOSITION_NOMEM;
1170                         }
1171                         goto cleanup;
1172                 } else {
1173                         return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1174                                                     commands);
1175                 }
1176         }
1177
1178         /*
1179          * Other parameters for the endpoint SHOULD be copied from the
1180          * existing parameters of the association (e.g. number of
1181          * outbound streams) into the INIT ACK and cookie.
1182          * FIXME:  We are copying parameters from the endpoint not the
1183          * association.
1184          */
1185         new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1186         if (!new_asoc)
1187                 goto nomem;
1188
1189         /* In the outbound INIT ACK the endpoint MUST copy its current
1190          * Verification Tag and Peers Verification tag into a reserved
1191          * place (local tie-tag and per tie-tag) within the state cookie.
1192          */
1193         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1194                                sctp_source(chunk),
1195                                (sctp_init_chunk_t *)chunk->chunk_hdr,
1196                                GFP_ATOMIC)) {
1197                 retval = SCTP_DISPOSITION_NOMEM;
1198                 goto nomem_init;
1199         }
1200
1201         /* Make sure no new addresses are being added during the
1202          * restart.   Do not do this check for COOKIE-WAIT state,
1203          * since there are no peer addresses to check against.
1204          * Upon return an ABORT will have been sent if needed.
1205          */
1206         if (!sctp_state(asoc, COOKIE_WAIT)) {
1207                 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1208                                                  commands)) {
1209                         retval = SCTP_DISPOSITION_CONSUME;
1210                         goto cleanup_asoc;
1211                 }
1212         }
1213
1214         sctp_tietags_populate(new_asoc, asoc);
1215
1216         /* B) "Z" shall respond immediately with an INIT ACK chunk.  */
1217
1218         /* If there are errors need to be reported for unknown parameters,
1219          * make sure to reserve enough room in the INIT ACK for them.
1220          */
1221         len = 0;
1222         if (err_chunk) {
1223                 len = ntohs(err_chunk->chunk_hdr->length) -
1224                         sizeof(sctp_chunkhdr_t);
1225         }
1226
1227         if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1228                 goto nomem;
1229
1230         repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1231         if (!repl)
1232                 goto nomem;
1233
1234         /* If there are errors need to be reported for unknown parameters,
1235          * include them in the outgoing INIT ACK as "Unrecognized parameter"
1236          * parameter.
1237          */
1238         if (err_chunk) {
1239                 /* Get the "Unrecognized parameter" parameter(s) out of the
1240                  * ERROR chunk generated by sctp_verify_init(). Since the
1241                  * error cause code for "unknown parameter" and the
1242                  * "Unrecognized parameter" type is the same, we can
1243                  * construct the parameters in INIT ACK by copying the
1244                  * ERROR causes over.
1245                  */
1246                 unk_param = (sctp_unrecognized_param_t *)
1247                             ((__u8 *)(err_chunk->chunk_hdr) +
1248                             sizeof(sctp_chunkhdr_t));
1249                 /* Replace the cause code with the "Unrecognized parameter"
1250                  * parameter type.
1251                  */
1252                 sctp_addto_chunk(repl, len, unk_param);
1253         }
1254
1255         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1256         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1257
1258         /*
1259          * Note: After sending out INIT ACK with the State Cookie parameter,
1260          * "Z" MUST NOT allocate any resources for this new association.
1261          * Otherwise, "Z" will be vulnerable to resource attacks.
1262          */
1263         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1264         retval = SCTP_DISPOSITION_CONSUME;
1265
1266 cleanup:
1267         if (err_chunk)
1268                 sctp_chunk_free(err_chunk);
1269         return retval;
1270 nomem:
1271         retval = SCTP_DISPOSITION_NOMEM;
1272         goto cleanup;
1273 nomem_init:
1274 cleanup_asoc:
1275         sctp_association_free(new_asoc);
1276         goto cleanup;
1277 }
1278
1279 /*
1280  * Handle simultanous INIT.
1281  * This means we started an INIT and then we got an INIT request from
1282  * our peer.
1283  *
1284  * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1285  * This usually indicates an initialization collision, i.e., each
1286  * endpoint is attempting, at about the same time, to establish an
1287  * association with the other endpoint.
1288  *
1289  * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1290  * endpoint MUST respond with an INIT ACK using the same parameters it
1291  * sent in its original INIT chunk (including its Verification Tag,
1292  * unchanged). These original parameters are combined with those from the
1293  * newly received INIT chunk. The endpoint shall also generate a State
1294  * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1295  * INIT to calculate the State Cookie.
1296  *
1297  * After that, the endpoint MUST NOT change its state, the T1-init
1298  * timer shall be left running and the corresponding TCB MUST NOT be
1299  * destroyed. The normal procedures for handling State Cookies when
1300  * a TCB exists will resolve the duplicate INITs to a single association.
1301  *
1302  * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1303  * its Tie-Tags with the Tag information of itself and its peer (see
1304  * section 5.2.2 for a description of the Tie-Tags).
1305  *
1306  * Verification Tag: Not explicit, but an INIT can not have a valid
1307  * verification tag, so we skip the check.
1308  *
1309  * Inputs
1310  * (endpoint, asoc, chunk)
1311  *
1312  * Outputs
1313  * (asoc, reply_msg, msg_up, timers, counters)
1314  *
1315  * The return value is the disposition of the chunk.
1316  */
1317 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1318                                     const struct sctp_association *asoc,
1319                                     const sctp_subtype_t type,
1320                                     void *arg,
1321                                     sctp_cmd_seq_t *commands)
1322 {
1323         /* Call helper to do the real work for both simulataneous and
1324          * duplicate INIT chunk handling.
1325          */
1326         return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1327 }
1328
1329 /*
1330  * Handle duplicated INIT messages.  These are usually delayed
1331  * restransmissions.
1332  *
1333  * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1334  * COOKIE-ECHOED and COOKIE-WAIT
1335  *
1336  * Unless otherwise stated, upon reception of an unexpected INIT for
1337  * this association, the endpoint shall generate an INIT ACK with a
1338  * State Cookie.  In the outbound INIT ACK the endpoint MUST copy its
1339  * current Verification Tag and peer's Verification Tag into a reserved
1340  * place within the state cookie.  We shall refer to these locations as
1341  * the Peer's-Tie-Tag and the Local-Tie-Tag.  The outbound SCTP packet
1342  * containing this INIT ACK MUST carry a Verification Tag value equal to
1343  * the Initiation Tag found in the unexpected INIT.  And the INIT ACK
1344  * MUST contain a new Initiation Tag (randomly generated see Section
1345  * 5.3.1).  Other parameters for the endpoint SHOULD be copied from the
1346  * existing parameters of the association (e.g. number of outbound
1347  * streams) into the INIT ACK and cookie.
1348  *
1349  * After sending out the INIT ACK, the endpoint shall take no further
1350  * actions, i.e., the existing association, including its current state,
1351  * and the corresponding TCB MUST NOT be changed.
1352  *
1353  * Note: Only when a TCB exists and the association is not in a COOKIE-
1354  * WAIT state are the Tie-Tags populated.  For a normal association INIT
1355  * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1356  * set to 0 (indicating that no previous TCB existed).  The INIT ACK and
1357  * State Cookie are populated as specified in section 5.2.1.
1358  *
1359  * Verification Tag: Not specified, but an INIT has no way of knowing
1360  * what the verification tag could be, so we ignore it.
1361  *
1362  * Inputs
1363  * (endpoint, asoc, chunk)
1364  *
1365  * Outputs
1366  * (asoc, reply_msg, msg_up, timers, counters)
1367  *
1368  * The return value is the disposition of the chunk.
1369  */
1370 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1371                                         const struct sctp_association *asoc,
1372                                         const sctp_subtype_t type,
1373                                         void *arg,
1374                                         sctp_cmd_seq_t *commands)
1375 {
1376         /* Call helper to do the real work for both simulataneous and
1377          * duplicate INIT chunk handling.
1378          */
1379         return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1380 }
1381
1382
1383
1384 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1385  *
1386  * Section 5.2.4
1387  *  A)  In this case, the peer may have restarted.
1388  */
1389 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1390                                         const struct sctp_association *asoc,
1391                                         struct sctp_chunk *chunk,
1392                                         sctp_cmd_seq_t *commands,
1393                                         struct sctp_association *new_asoc)
1394 {
1395         sctp_init_chunk_t *peer_init;
1396         struct sctp_ulpevent *ev;
1397         struct sctp_chunk *repl;
1398         struct sctp_chunk *err;
1399         sctp_disposition_t disposition;
1400
1401         /* new_asoc is a brand-new association, so these are not yet
1402          * side effects--it is safe to run them here.
1403          */
1404         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1405
1406         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1407                                sctp_source(chunk), peer_init,
1408                                GFP_ATOMIC))
1409                 goto nomem;
1410
1411         /* Make sure no new addresses are being added during the
1412          * restart.  Though this is a pretty complicated attack
1413          * since you'd have to get inside the cookie.
1414          */
1415         if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1416                 return SCTP_DISPOSITION_CONSUME;
1417         }
1418
1419         /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1420          * the peer has restarted (Action A), it MUST NOT setup a new
1421          * association but instead resend the SHUTDOWN ACK and send an ERROR
1422          * chunk with a "Cookie Received while Shutting Down" error cause to
1423          * its peer.
1424         */
1425         if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1426                 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1427                                 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1428                                 chunk, commands);
1429                 if (SCTP_DISPOSITION_NOMEM == disposition)
1430                         goto nomem;
1431
1432                 err = sctp_make_op_error(asoc, chunk,
1433                                          SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1434                                          NULL, 0);
1435                 if (err)
1436                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1437                                         SCTP_CHUNK(err));
1438
1439                 return SCTP_DISPOSITION_CONSUME;
1440         }
1441
1442         /* For now, fail any unsent/unacked data.  Consider the optional
1443          * choice of resending of this data.
1444          */
1445         sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1446
1447         /* Update the content of current association. */
1448         sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1449
1450         repl = sctp_make_cookie_ack(new_asoc, chunk);
1451         if (!repl)
1452                 goto nomem;
1453
1454         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1455
1456         /* Report association restart to upper layer. */
1457         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1458                                              new_asoc->c.sinit_num_ostreams,
1459                                              new_asoc->c.sinit_max_instreams,
1460                                              GFP_ATOMIC);
1461         if (!ev)
1462                 goto nomem_ev;
1463
1464         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1465         return SCTP_DISPOSITION_CONSUME;
1466
1467 nomem_ev:
1468         sctp_chunk_free(repl);
1469 nomem:
1470         return SCTP_DISPOSITION_NOMEM;
1471 }
1472
1473 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1474  *
1475  * Section 5.2.4
1476  *   B) In this case, both sides may be attempting to start an association
1477  *      at about the same time but the peer endpoint started its INIT
1478  *      after responding to the local endpoint's INIT
1479  */
1480 /* This case represents an initialization collision.  */
1481 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1482                                         const struct sctp_association *asoc,
1483                                         struct sctp_chunk *chunk,
1484                                         sctp_cmd_seq_t *commands,
1485                                         struct sctp_association *new_asoc)
1486 {
1487         sctp_init_chunk_t *peer_init;
1488         struct sctp_ulpevent *ev;
1489         struct sctp_chunk *repl;
1490
1491         /* new_asoc is a brand-new association, so these are not yet
1492          * side effects--it is safe to run them here.
1493          */
1494         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1495         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1496                                sctp_source(chunk), peer_init,
1497                                GFP_ATOMIC))
1498                 goto nomem;
1499
1500         /* Update the content of current association.  */
1501         sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1502         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1503                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
1504         SCTP_INC_STATS(SctpCurrEstab);
1505         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1506
1507         repl = sctp_make_cookie_ack(new_asoc, chunk);
1508         if (!repl)
1509                 goto nomem;
1510
1511         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1512         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1513
1514         /* RFC 2960 5.1 Normal Establishment of an Association
1515          *
1516          * D) IMPLEMENTATION NOTE: An implementation may choose to
1517          * send the Communication Up notification to the SCTP user
1518          * upon reception of a valid COOKIE ECHO chunk.
1519          */
1520         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP, 0,
1521                                              new_asoc->c.sinit_num_ostreams,
1522                                              new_asoc->c.sinit_max_instreams,
1523                                              GFP_ATOMIC);
1524         if (!ev)
1525                 goto nomem_ev;
1526
1527         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1528         return SCTP_DISPOSITION_CONSUME;
1529
1530 nomem_ev:
1531         sctp_chunk_free(repl);
1532 nomem:
1533         return SCTP_DISPOSITION_NOMEM;
1534 }
1535
1536 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1537  *
1538  * Section 5.2.4
1539  *  C) In this case, the local endpoint's cookie has arrived late.
1540  *     Before it arrived, the local endpoint sent an INIT and received an
1541  *     INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1542  *     but a new tag of its own.
1543  */
1544 /* This case represents an initialization collision.  */
1545 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1546                                         const struct sctp_association *asoc,
1547                                         struct sctp_chunk *chunk,
1548                                         sctp_cmd_seq_t *commands,
1549                                         struct sctp_association *new_asoc)
1550 {
1551         /* The cookie should be silently discarded.
1552          * The endpoint SHOULD NOT change states and should leave
1553          * any timers running.
1554          */
1555         return SCTP_DISPOSITION_DISCARD;
1556 }
1557
1558 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1559  *
1560  * Section 5.2.4
1561  *
1562  * D) When both local and remote tags match the endpoint should always
1563  *    enter the ESTABLISHED state, if it has not already done so.
1564  */
1565 /* This case represents an initialization collision.  */
1566 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1567                                         const struct sctp_association *asoc,
1568                                         struct sctp_chunk *chunk,
1569                                         sctp_cmd_seq_t *commands,
1570                                         struct sctp_association *new_asoc)
1571 {
1572         struct sctp_ulpevent *ev = NULL;
1573         struct sctp_chunk *repl;
1574
1575         /* Clarification from Implementor's Guide:
1576          * D) When both local and remote tags match the endpoint should
1577          * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1578          * It should stop any cookie timer that may be running and send
1579          * a COOKIE ACK.
1580          */
1581
1582         /* Don't accidentally move back into established state. */
1583         if (asoc->state < SCTP_STATE_ESTABLISHED) {
1584                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1585                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1586                 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1587                                 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1588                 SCTP_INC_STATS(SctpCurrEstab);
1589                 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1590                                 SCTP_NULL());
1591
1592                 /* RFC 2960 5.1 Normal Establishment of an Association
1593                  *
1594                  * D) IMPLEMENTATION NOTE: An implementation may choose
1595                  * to send the Communication Up notification to the
1596                  * SCTP user upon reception of a valid COOKIE
1597                  * ECHO chunk.
1598                  */
1599                 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0,
1600                                              SCTP_COMM_UP, 0,
1601                                              new_asoc->c.sinit_num_ostreams,
1602                                              new_asoc->c.sinit_max_instreams,
1603                                              GFP_ATOMIC);
1604                 if (!ev)
1605                         goto nomem;
1606                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1607                                 SCTP_ULPEVENT(ev));
1608         }
1609         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1610
1611         repl = sctp_make_cookie_ack(new_asoc, chunk);
1612         if (!repl)
1613                 goto nomem;
1614
1615         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1616         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1617
1618         return SCTP_DISPOSITION_CONSUME;
1619
1620 nomem:
1621         if (ev)
1622                 sctp_ulpevent_free(ev);
1623         return SCTP_DISPOSITION_NOMEM;
1624 }
1625
1626 /*
1627  * Handle a duplicate COOKIE-ECHO.  This usually means a cookie-carrying
1628  * chunk was retransmitted and then delayed in the network.
1629  *
1630  * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1631  *
1632  * Verification Tag: None.  Do cookie validation.
1633  *
1634  * Inputs
1635  * (endpoint, asoc, chunk)
1636  *
1637  * Outputs
1638  * (asoc, reply_msg, msg_up, timers, counters)
1639  *
1640  * The return value is the disposition of the chunk.
1641  */
1642 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1643                                         const struct sctp_association *asoc,
1644                                         const sctp_subtype_t type,
1645                                         void *arg,
1646                                         sctp_cmd_seq_t *commands)
1647 {
1648         sctp_disposition_t retval;
1649         struct sctp_chunk *chunk = arg;
1650         struct sctp_association *new_asoc;
1651         int error = 0;
1652         char action;
1653         struct sctp_chunk *err_chk_p;
1654
1655         /* "Decode" the chunk.  We have no optional parameters so we
1656          * are in good shape.
1657          */
1658         chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1659         skb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1660                  sizeof(sctp_chunkhdr_t));
1661
1662         /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1663          * of a duplicate COOKIE ECHO match the Verification Tags of the
1664          * current association, consider the State Cookie valid even if
1665          * the lifespan is exceeded.
1666          */
1667         new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1668                                       &err_chk_p);
1669
1670         /* FIXME:
1671          * If the re-build failed, what is the proper error path
1672          * from here?
1673          *
1674          * [We should abort the association. --piggy]
1675          */
1676         if (!new_asoc) {
1677                 /* FIXME: Several errors are possible.  A bad cookie should
1678                  * be silently discarded, but think about logging it too.
1679                  */
1680                 switch (error) {
1681                 case -SCTP_IERROR_NOMEM:
1682                         goto nomem;
1683
1684                 case -SCTP_IERROR_STALE_COOKIE:
1685                         sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
1686                                                    err_chk_p);
1687                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1688                 case -SCTP_IERROR_BAD_SIG:
1689                 default:
1690                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1691                 };
1692         }
1693
1694         /* Compare the tie_tag in cookie with the verification tag of
1695          * current association.
1696          */
1697         action = sctp_tietags_compare(new_asoc, asoc);
1698
1699         switch (action) {
1700         case 'A': /* Association restart. */
1701                 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
1702                                               new_asoc);
1703                 break;
1704
1705         case 'B': /* Collision case B. */
1706                 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
1707                                               new_asoc);
1708                 break;
1709
1710         case 'C': /* Collision case C. */
1711                 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
1712                                               new_asoc);
1713                 break;
1714
1715         case 'D': /* Collision case D. */
1716                 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
1717                                               new_asoc);
1718                 break;
1719
1720         default: /* Discard packet for all others. */
1721                 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1722                 break;
1723         };
1724
1725         /* Delete the tempory new association. */
1726         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1727         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1728
1729         return retval;
1730
1731 nomem:
1732         return SCTP_DISPOSITION_NOMEM;
1733 }
1734
1735 /*
1736  * Process an ABORT.  (SHUTDOWN-PENDING state)
1737  *
1738  * See sctp_sf_do_9_1_abort().
1739  */
1740 sctp_disposition_t sctp_sf_shutdown_pending_abort(
1741         const struct sctp_endpoint *ep,
1742         const struct sctp_association *asoc,
1743         const sctp_subtype_t type,
1744         void *arg,
1745         sctp_cmd_seq_t *commands)
1746 {
1747         struct sctp_chunk *chunk = arg;
1748
1749         if (!sctp_vtag_verify_either(chunk, asoc))
1750                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1751
1752         /* Stop the T5-shutdown guard timer.  */
1753         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1754                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
1755
1756         return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1757 }
1758
1759 /*
1760  * Process an ABORT.  (SHUTDOWN-SENT state)
1761  *
1762  * See sctp_sf_do_9_1_abort().
1763  */
1764 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
1765                                         const struct sctp_association *asoc,
1766                                         const sctp_subtype_t type,
1767                                         void *arg,
1768                                         sctp_cmd_seq_t *commands)
1769 {
1770         struct sctp_chunk *chunk = arg;
1771
1772         if (!sctp_vtag_verify_either(chunk, asoc))
1773                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1774
1775         /* Stop the T2-shutdown timer. */
1776         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1777                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
1778
1779         /* Stop the T5-shutdown guard timer.  */
1780         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1781                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
1782
1783         return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1784 }
1785
1786 /*
1787  * Process an ABORT.  (SHUTDOWN-ACK-SENT state)
1788  *
1789  * See sctp_sf_do_9_1_abort().
1790  */
1791 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
1792         const struct sctp_endpoint *ep,
1793         const struct sctp_association *asoc,
1794         const sctp_subtype_t type,
1795         void *arg,
1796         sctp_cmd_seq_t *commands)
1797 {
1798         /* The same T2 timer, so we should be able to use
1799          * common function with the SHUTDOWN-SENT state.
1800          */
1801         return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
1802 }
1803
1804 /*
1805  * Handle an Error received in COOKIE_ECHOED state.
1806  *
1807  * Only handle the error type of stale COOKIE Error, the other errors will
1808  * be ignored.
1809  *
1810  * Inputs
1811  * (endpoint, asoc, chunk)
1812  *
1813  * Outputs
1814  * (asoc, reply_msg, msg_up, timers, counters)
1815  *
1816  * The return value is the disposition of the chunk.
1817  */
1818 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
1819                                         const struct sctp_association *asoc,
1820                                         const sctp_subtype_t type,
1821                                         void *arg,
1822                                         sctp_cmd_seq_t *commands)
1823 {
1824         struct sctp_chunk *chunk = arg;
1825         sctp_errhdr_t *err;
1826
1827         /* Process the error here */
1828         /* FUTURE FIXME:  When PR-SCTP related and other optional
1829          * parms are emitted, this will have to change to handle multiple
1830          * errors.
1831          */
1832         sctp_walk_errors(err, chunk->chunk_hdr) {
1833                 if (SCTP_ERROR_STALE_COOKIE == err->cause)
1834                         return sctp_sf_do_5_2_6_stale(ep, asoc, type, 
1835                                                         arg, commands);
1836         }
1837         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1838 }
1839
1840 /*
1841  * Handle a Stale COOKIE Error
1842  *
1843  * Section: 5.2.6 Handle Stale COOKIE Error
1844  * If the association is in the COOKIE-ECHOED state, the endpoint may elect
1845  * one of the following three alternatives.
1846  * ...
1847  * 3) Send a new INIT chunk to the endpoint, adding a Cookie
1848  *    Preservative parameter requesting an extension to the lifetime of
1849  *    the State Cookie. When calculating the time extension, an
1850  *    implementation SHOULD use the RTT information measured based on the
1851  *    previous COOKIE ECHO / ERROR exchange, and should add no more
1852  *    than 1 second beyond the measured RTT, due to long State Cookie
1853  *    lifetimes making the endpoint more subject to a replay attack.
1854  *
1855  * Verification Tag:  Not explicit, but safe to ignore.
1856  *
1857  * Inputs
1858  * (endpoint, asoc, chunk)
1859  *
1860  * Outputs
1861  * (asoc, reply_msg, msg_up, timers, counters)
1862  *
1863  * The return value is the disposition of the chunk.
1864  */
1865 sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
1866                                           const struct sctp_association *asoc,
1867                                           const sctp_subtype_t type,
1868                                           void *arg,
1869                                           sctp_cmd_seq_t *commands)
1870 {
1871         struct sctp_chunk *chunk = arg;
1872         time_t stale;
1873         sctp_cookie_preserve_param_t bht;
1874         sctp_errhdr_t *err;
1875         struct list_head *pos;
1876         struct sctp_transport *t;
1877         struct sctp_chunk *reply;
1878         struct sctp_bind_addr *bp;
1879         int attempts;
1880
1881         attempts = asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1;
1882
1883         if (attempts >= asoc->max_init_attempts) {
1884                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
1885                                 SCTP_U32(SCTP_ERROR_STALE_COOKIE));
1886                 return SCTP_DISPOSITION_DELETE_TCB;
1887         }
1888
1889         err = (sctp_errhdr_t *)(chunk->skb->data);
1890
1891         /* When calculating the time extension, an implementation
1892          * SHOULD use the RTT information measured based on the
1893          * previous COOKIE ECHO / ERROR exchange, and should add no
1894          * more than 1 second beyond the measured RTT, due to long
1895          * State Cookie lifetimes making the endpoint more subject to
1896          * a replay attack.
1897          * Measure of Staleness's unit is usec. (1/1000000 sec)
1898          * Suggested Cookie Life-span Increment's unit is msec.
1899          * (1/1000 sec)
1900          * In general, if you use the suggested cookie life, the value
1901          * found in the field of measure of staleness should be doubled
1902          * to give ample time to retransmit the new cookie and thus
1903          * yield a higher probability of success on the reattempt.
1904          */
1905         stale = ntohl(*(suseconds_t *)((u8 *)err + sizeof(sctp_errhdr_t)));
1906         stale = (stale * 2) / 1000;
1907
1908         bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
1909         bht.param_hdr.length = htons(sizeof(bht));
1910         bht.lifespan_increment = htonl(stale);
1911
1912         /* Build that new INIT chunk.  */
1913         bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
1914         reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
1915         if (!reply)
1916                 goto nomem;
1917
1918         sctp_addto_chunk(reply, sizeof(bht), &bht);
1919
1920         /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
1921         sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
1922
1923         /* Cast away the const modifier, as we want to just
1924          * rerun it through as a sideffect.
1925          */
1926         sctp_add_cmd_sf(commands, SCTP_CMD_COUNTER_INC,
1927                         SCTP_COUNTER(SCTP_COUNTER_INIT_ERROR));
1928
1929         /* If we've sent any data bundled with COOKIE-ECHO we need to
1930          * resend.
1931          */
1932         list_for_each(pos, &asoc->peer.transport_addr_list) {
1933                 t = list_entry(pos, struct sctp_transport, transports);
1934                 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(t));
1935         }
1936
1937         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1938                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1939         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1940                         SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
1941         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
1942                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
1943
1944         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1945
1946         return SCTP_DISPOSITION_CONSUME;
1947
1948 nomem:
1949         return SCTP_DISPOSITION_NOMEM;
1950 }
1951
1952 /*
1953  * Process an ABORT.
1954  *
1955  * Section: 9.1
1956  * After checking the Verification Tag, the receiving endpoint shall
1957  * remove the association from its record, and shall report the
1958  * termination to its upper layer.
1959  *
1960  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
1961  * B) Rules for packet carrying ABORT:
1962  *
1963  *  - The endpoint shall always fill in the Verification Tag field of the
1964  *    outbound packet with the destination endpoint's tag value if it
1965  *    is known.
1966  *
1967  *  - If the ABORT is sent in response to an OOTB packet, the endpoint
1968  *    MUST follow the procedure described in Section 8.4.
1969  *
1970  *  - The receiver MUST accept the packet if the Verification Tag
1971  *    matches either its own tag, OR the tag of its peer. Otherwise, the
1972  *    receiver MUST silently discard the packet and take no further
1973  *    action.
1974  *
1975  * Inputs
1976  * (endpoint, asoc, chunk)
1977  *
1978  * Outputs
1979  * (asoc, reply_msg, msg_up, timers, counters)
1980  *
1981  * The return value is the disposition of the chunk.
1982  */
1983 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
1984                                         const struct sctp_association *asoc,
1985                                         const sctp_subtype_t type,
1986                                         void *arg,
1987                                         sctp_cmd_seq_t *commands)
1988 {
1989         struct sctp_chunk *chunk = arg;
1990         unsigned len;
1991         __u16 error = SCTP_ERROR_NO_ERROR;
1992
1993         if (!sctp_vtag_verify_either(chunk, asoc))
1994                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1995
1996         /* Check that chunk header looks valid.  */
1997         len = ntohs(chunk->chunk_hdr->length);
1998         if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
1999                 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2000
2001
2002         /* ASSOC_FAILED will DELETE_TCB. */
2003         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_U32(error));
2004         SCTP_INC_STATS(SctpAborteds);
2005         SCTP_DEC_STATS(SctpCurrEstab);
2006
2007         return SCTP_DISPOSITION_ABORT;
2008 }
2009
2010 /*
2011  * Process an ABORT.  (COOKIE-WAIT state)
2012  *
2013  * See sctp_sf_do_9_1_abort() above.
2014  */
2015 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2016                                      const struct sctp_association *asoc,
2017                                      const sctp_subtype_t type,
2018                                      void *arg,
2019                                      sctp_cmd_seq_t *commands)
2020 {
2021         struct sctp_chunk *chunk = arg;
2022         unsigned len;
2023         __u16 error = SCTP_ERROR_NO_ERROR;
2024
2025         if (!sctp_vtag_verify_either(chunk, asoc))
2026                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2027
2028         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2029                         SCTP_STATE(SCTP_STATE_CLOSED));
2030         SCTP_INC_STATS(SctpAborteds);
2031         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2032                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2033
2034         /* Check that chunk header looks valid.  */
2035         len = ntohs(chunk->chunk_hdr->length);
2036         if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2037                 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2038
2039         /* CMD_INIT_FAILED will DELETE_TCB. */
2040         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED, SCTP_U32(error));
2041
2042         return SCTP_DISPOSITION_ABORT;
2043 }
2044
2045 /*
2046  * Process an ABORT.  (COOKIE-ECHOED state)
2047  *
2048  * See sctp_sf_do_9_1_abort() above.
2049  */
2050 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2051                                                const struct sctp_association *asoc,
2052                                                const sctp_subtype_t type,
2053                                                void *arg,
2054                                                sctp_cmd_seq_t *commands)
2055 {
2056         /* There is a single T1 timer, so we should be able to use
2057          * common function with the COOKIE-WAIT state.
2058          */
2059         return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2060 }
2061
2062 /*
2063  * sctp_sf_do_9_2_shut
2064  *
2065  * Section: 9.2
2066  * Upon the reception of the SHUTDOWN, the peer endpoint shall
2067  *  - enter the SHUTDOWN-RECEIVED state,
2068  *
2069  *  - stop accepting new data from its SCTP user
2070  *
2071  *  - verify, by checking the Cumulative TSN Ack field of the chunk,
2072  *    that all its outstanding DATA chunks have been received by the
2073  *    SHUTDOWN sender.
2074  *
2075  * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2076  * send a SHUTDOWN in response to a ULP request. And should discard
2077  * subsequent SHUTDOWN chunks.
2078  *
2079  * If there are still outstanding DATA chunks left, the SHUTDOWN
2080  * receiver shall continue to follow normal data transmission
2081  * procedures defined in Section 6 until all outstanding DATA chunks
2082  * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2083  * new data from its SCTP user.
2084  *
2085  * Verification Tag:  8.5 Verification Tag [Normal verification]
2086  *
2087  * Inputs
2088  * (endpoint, asoc, chunk)
2089  *
2090  * Outputs
2091  * (asoc, reply_msg, msg_up, timers, counters)
2092  *
2093  * The return value is the disposition of the chunk.
2094  */
2095 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2096                                            const struct sctp_association *asoc,
2097                                            const sctp_subtype_t type,
2098                                            void *arg,
2099                                            sctp_cmd_seq_t *commands)
2100 {
2101         struct sctp_chunk *chunk = arg;
2102         sctp_shutdownhdr_t *sdh;
2103         sctp_disposition_t disposition;
2104         struct sctp_ulpevent *ev;
2105
2106         /* Convert the elaborate header.  */
2107         sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2108         skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2109         chunk->subh.shutdown_hdr = sdh;
2110
2111         if (!sctp_vtag_verify(chunk, asoc))
2112                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2113
2114         /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2115          *  - enter the SHUTDOWN-RECEIVED state,
2116          *  - stop accepting new data from its SCTP user
2117          *
2118          * [This is implicit in the new state.]
2119          */
2120         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2121                         SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2122         disposition = SCTP_DISPOSITION_CONSUME;
2123
2124         if (sctp_outq_is_empty(&asoc->outqueue)) {
2125                 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2126                                                           arg, commands);
2127         }
2128
2129         if (SCTP_DISPOSITION_NOMEM == disposition)
2130                 goto out;
2131
2132         /*  - verify, by checking the Cumulative TSN Ack field of the
2133          *    chunk, that all its outstanding DATA chunks have been
2134          *    received by the SHUTDOWN sender.
2135          */
2136         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2137                         SCTP_U32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2138
2139         /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2140          * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2141          * inform the application that it should cease sending data.
2142          */
2143         ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2144         if (!ev) {
2145                 disposition = SCTP_DISPOSITION_NOMEM;
2146                 goto out;       
2147         }
2148         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2149
2150 out:
2151         return disposition;
2152 }
2153
2154 /* RFC 2960 9.2
2155  * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2156  * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2157  * transport addresses (either in the IP addresses or in the INIT chunk)
2158  * that belong to this association, it should discard the INIT chunk and
2159  * retransmit the SHUTDOWN ACK chunk.
2160  */
2161 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2162                                     const struct sctp_association *asoc,
2163                                     const sctp_subtype_t type,
2164                                     void *arg,
2165                                     sctp_cmd_seq_t *commands)
2166 {
2167         struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2168         struct sctp_chunk *reply;
2169
2170         reply = sctp_make_shutdown_ack(asoc, chunk);
2171         if (NULL == reply)
2172                 goto nomem;
2173
2174         /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2175          * the T2-SHUTDOWN timer.
2176          */
2177         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2178
2179         /* and restart the T2-shutdown timer. */
2180         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2181                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2182
2183         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2184
2185         return SCTP_DISPOSITION_CONSUME;
2186 nomem:
2187         return SCTP_DISPOSITION_NOMEM;
2188 }
2189
2190 /*
2191  * sctp_sf_do_ecn_cwr
2192  *
2193  * Section:  Appendix A: Explicit Congestion Notification
2194  *
2195  * CWR:
2196  *
2197  * RFC 2481 details a specific bit for a sender to send in the header of
2198  * its next outbound TCP segment to indicate to its peer that it has
2199  * reduced its congestion window.  This is termed the CWR bit.  For
2200  * SCTP the same indication is made by including the CWR chunk.
2201  * This chunk contains one data element, i.e. the TSN number that
2202  * was sent in the ECNE chunk.  This element represents the lowest
2203  * TSN number in the datagram that was originally marked with the
2204  * CE bit.
2205  *
2206  * Verification Tag: 8.5 Verification Tag [Normal verification]
2207  * Inputs
2208  * (endpoint, asoc, chunk)
2209  *
2210  * Outputs
2211  * (asoc, reply_msg, msg_up, timers, counters)
2212  *
2213  * The return value is the disposition of the chunk.
2214  */
2215 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2216                                       const struct sctp_association *asoc,
2217                                       const sctp_subtype_t type,
2218                                       void *arg,
2219                                       sctp_cmd_seq_t *commands)
2220 {
2221         sctp_cwrhdr_t *cwr;
2222         struct sctp_chunk *chunk = arg;
2223
2224         if (!sctp_vtag_verify(chunk, asoc))
2225                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2226
2227         cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2228         skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2229
2230         cwr->lowest_tsn = ntohl(cwr->lowest_tsn);
2231
2232         /* Does this CWR ack the last sent congestion notification? */
2233         if (TSN_lte(asoc->last_ecne_tsn, cwr->lowest_tsn)) {
2234                 /* Stop sending ECNE. */
2235                 sctp_add_cmd_sf(commands,
2236                                 SCTP_CMD_ECN_CWR,
2237                                 SCTP_U32(cwr->lowest_tsn));
2238         }
2239         return SCTP_DISPOSITION_CONSUME;
2240 }
2241
2242 /*
2243  * sctp_sf_do_ecne
2244  *
2245  * Section:  Appendix A: Explicit Congestion Notification
2246  *
2247  * ECN-Echo
2248  *
2249  * RFC 2481 details a specific bit for a receiver to send back in its
2250  * TCP acknowledgements to notify the sender of the Congestion
2251  * Experienced (CE) bit having arrived from the network.  For SCTP this
2252  * same indication is made by including the ECNE chunk.  This chunk
2253  * contains one data element, i.e. the lowest TSN associated with the IP
2254  * datagram marked with the CE bit.....
2255  *
2256  * Verification Tag: 8.5 Verification Tag [Normal verification]
2257  * Inputs
2258  * (endpoint, asoc, chunk)
2259  *
2260  * Outputs
2261  * (asoc, reply_msg, msg_up, timers, counters)
2262  *
2263  * The return value is the disposition of the chunk.
2264  */
2265 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2266                                    const struct sctp_association *asoc,
2267                                    const sctp_subtype_t type,
2268                                    void *arg,
2269                                    sctp_cmd_seq_t *commands)
2270 {
2271         sctp_ecnehdr_t *ecne;
2272         struct sctp_chunk *chunk = arg;
2273
2274         if (!sctp_vtag_verify(chunk, asoc))
2275                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2276
2277         ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2278         skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2279
2280         /* If this is a newer ECNE than the last CWR packet we sent out */
2281         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2282                         SCTP_U32(ntohl(ecne->lowest_tsn)));
2283
2284         return SCTP_DISPOSITION_CONSUME;
2285 }
2286
2287 /*
2288  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
2289  *
2290  * The SCTP endpoint MUST always acknowledge the reception of each valid
2291  * DATA chunk.
2292  *
2293  * The guidelines on delayed acknowledgement algorithm specified in
2294  * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2295  * acknowledgement SHOULD be generated for at least every second packet
2296  * (not every second DATA chunk) received, and SHOULD be generated within
2297  * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2298  * situations it may be beneficial for an SCTP transmitter to be more
2299  * conservative than the algorithms detailed in this document allow.
2300  * However, an SCTP transmitter MUST NOT be more aggressive than the
2301  * following algorithms allow.
2302  *
2303  * A SCTP receiver MUST NOT generate more than one SACK for every
2304  * incoming packet, other than to update the offered window as the
2305  * receiving application consumes new data.
2306  *
2307  * Verification Tag:  8.5 Verification Tag [Normal verification]
2308  *
2309  * Inputs
2310  * (endpoint, asoc, chunk)
2311  *
2312  * Outputs
2313  * (asoc, reply_msg, msg_up, timers, counters)
2314  *
2315  * The return value is the disposition of the chunk.
2316  */
2317 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2318                                         const struct sctp_association *asoc,
2319                                         const sctp_subtype_t type,
2320                                         void *arg,
2321                                         sctp_cmd_seq_t *commands)
2322 {
2323         struct sctp_chunk *chunk = arg;
2324         sctp_datahdr_t *data_hdr;
2325         struct sctp_chunk *err;
2326         size_t datalen;
2327         sctp_verb_t deliver;
2328         int tmp;
2329         __u32 tsn;
2330
2331         if (!sctp_vtag_verify(chunk, asoc)) {
2332                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2333                                 SCTP_NULL());
2334                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2335         }
2336
2337         data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
2338         skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
2339
2340         tsn = ntohl(data_hdr->tsn);
2341         SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
2342
2343         /* ASSERT:  Now skb->data is really the user data.  */
2344
2345         /* Process ECN based congestion.
2346          *
2347          * Since the chunk structure is reused for all chunks within
2348          * a packet, we use ecn_ce_done to track if we've already
2349          * done CE processing for this packet.
2350          *
2351          * We need to do ECN processing even if we plan to discard the
2352          * chunk later.
2353          */
2354
2355         if (!chunk->ecn_ce_done) {
2356                 struct sctp_af *af;
2357                 chunk->ecn_ce_done = 1;
2358
2359                 af = sctp_get_af_specific(
2360                         ipver2af(chunk->skb->nh.iph->version));
2361
2362                 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
2363                         /* Do real work as sideffect. */
2364                         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
2365                                         SCTP_U32(tsn));
2366                 }
2367         }
2368
2369         tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
2370         if (tmp < 0) {
2371                 /* The TSN is too high--silently discard the chunk and
2372                  * count on it getting retransmitted later.
2373                  */
2374                 goto discard_noforce;
2375         } else if (tmp > 0) {
2376                 /* This is a duplicate.  Record it.  */
2377                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
2378                 goto discard_force;
2379         }
2380
2381         /* This is a new TSN.  */
2382
2383         /* Discard if there is no room in the receive window.
2384          * Actually, allow a little bit of overflow (up to a MTU).
2385          */
2386         datalen = ntohs(chunk->chunk_hdr->length);
2387         datalen -= sizeof(sctp_data_chunk_t);
2388
2389         deliver = SCTP_CMD_CHUNK_ULP;
2390
2391         /* Think about partial delivery. */
2392         if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
2393
2394                 /* Even if we don't accept this chunk there is
2395                  * memory pressure.
2396                  */
2397                 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
2398         }
2399
2400         /* Spill over rwnd a little bit.  Note: While allowed, this spill over
2401          * seems a bit troublesome in that frag_point varies based on
2402          * PMTU.  In cases, such as loopback, this might be a rather
2403          * large spill over.
2404          */
2405         if (!asoc->rwnd || asoc->rwnd_over ||
2406             (datalen > asoc->rwnd + asoc->frag_point)) {
2407
2408                 /* If this is the next TSN, consider reneging to make
2409                  * room.   Note: Playing nice with a confused sender.  A
2410                  * malicious sender can still eat up all our buffer
2411                  * space and in the future we may want to detect and
2412                  * do more drastic reneging.
2413                  */
2414                 if (sctp_tsnmap_has_gap(&asoc->peer.tsn_map) &&
2415                     (sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1) == tsn) {
2416                         SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
2417                         deliver = SCTP_CMD_RENEGE;
2418                 } else {
2419                         SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
2420                                           "rwnd: %d\n", tsn, datalen,
2421                                           asoc->rwnd);
2422                         goto discard_force;
2423                 }
2424         }
2425
2426         /*
2427          * Section 3.3.10.9 No User Data (9)
2428          *
2429          * Cause of error
2430          * ---------------
2431          * No User Data:  This error cause is returned to the originator of a
2432          * DATA chunk if a received DATA chunk has no user data.
2433          */
2434         if (unlikely(0 == datalen)) {
2435                 err = sctp_make_abort_no_data(asoc, chunk, tsn);
2436                 if (err) {
2437                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
2438                                         SCTP_CHUNK(err));
2439                 }
2440                 /* We are going to ABORT, so we might as well stop
2441                  * processing the rest of the chunks in the packet.
2442                  */
2443                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
2444                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
2445                                 SCTP_U32(SCTP_ERROR_NO_DATA));
2446                 SCTP_INC_STATS(SctpAborteds);
2447                 SCTP_DEC_STATS(SctpCurrEstab);
2448                 return SCTP_DISPOSITION_CONSUME;
2449         }
2450
2451         /* If definately accepting the DATA chunk, record its TSN, otherwise
2452          * wait for renege processing.
2453          */
2454         if (SCTP_CMD_CHUNK_ULP == deliver)
2455                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
2456
2457         /* Note: Some chunks may get overcounted (if we drop) or overcounted
2458          * if we renege and the chunk arrives again.
2459          */
2460         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
2461                 SCTP_INC_STATS(SctpInUnorderChunks);
2462         else
2463                 SCTP_INC_STATS(SctpInOrderChunks);
2464
2465         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2466          *
2467          * If an endpoint receive a DATA chunk with an invalid stream
2468          * identifier, it shall acknowledge the reception of the DATA chunk
2469          * following the normal procedure, immediately send an ERROR chunk
2470          * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
2471          * and discard the DATA chunk.
2472          */
2473         if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
2474                 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
2475                                          &data_hdr->stream,
2476                                          sizeof(data_hdr->stream));
2477                 if (err)
2478                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
2479                                         SCTP_CHUNK(err));
2480                 goto discard_noforce;
2481         }
2482
2483         /* Send the data up to the user.  Note:  Schedule  the
2484          * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
2485          * chunk needs the updated rwnd.
2486          */
2487         sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
2488
2489         if (asoc->autoclose) {
2490                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2491                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2492         }
2493
2494         /* If this is the last chunk in a packet, we need to count it
2495          * toward sack generation.  Note that we need to SACK every
2496          * OTHER packet containing data chunks, EVEN IF WE DISCARD
2497          * THEM.  We elect to NOT generate SACK's if the chunk fails
2498          * the verification tag test.
2499          *
2500          * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2501          *
2502          * The SCTP endpoint MUST always acknowledge the reception of
2503          * each valid DATA chunk.
2504          *
2505          * The guidelines on delayed acknowledgement algorithm
2506          * specified in  Section 4.2 of [RFC2581] SHOULD be followed.
2507          * Specifically, an acknowledgement SHOULD be generated for at
2508          * least every second packet (not every second DATA chunk)
2509          * received, and SHOULD be generated within 200 ms of the
2510          * arrival of any unacknowledged DATA chunk.  In some
2511          * situations it may be beneficial for an SCTP transmitter to
2512          * be more conservative than the algorithms detailed in this
2513          * document allow. However, an SCTP transmitter MUST NOT be
2514          * more aggressive than the following algorithms allow.
2515          */
2516         if (chunk->end_of_packet) {
2517                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2518
2519                 /* Start the SACK timer.  */
2520                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2521                                 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
2522         }
2523
2524         return SCTP_DISPOSITION_CONSUME;
2525
2526 discard_force:
2527         /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2528          *
2529          * When a packet arrives with duplicate DATA chunk(s) and with
2530          * no new DATA chunk(s), the endpoint MUST immediately send a
2531          * SACK with no delay.  If a packet arrives with duplicate
2532          * DATA chunk(s) bundled with new DATA chunks, the endpoint
2533          * MAY immediately send a SACK.  Normally receipt of duplicate
2534          * DATA chunks will occur when the original SACK chunk was lost
2535          * and the peer's RTO has expired.  The duplicate TSN number(s)
2536          * SHOULD be reported in the SACK as duplicate.
2537          */
2538         /* In our case, we split the MAY SACK advice up whether or not
2539          * the last chunk is a duplicate.'
2540          */
2541         if (chunk->end_of_packet)
2542                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2543         return SCTP_DISPOSITION_DISCARD;
2544
2545 discard_noforce:
2546         if (chunk->end_of_packet) {
2547                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2548
2549                 /* Start the SACK timer.  */
2550                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2551                                 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
2552         }
2553         return SCTP_DISPOSITION_DISCARD;
2554 }
2555
2556 /*
2557  * sctp_sf_eat_data_fast_4_4
2558  *
2559  * Section: 4 (4)
2560  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2561  *    DATA chunks without delay.
2562  *
2563  * Verification Tag:  8.5 Verification Tag [Normal verification]
2564  * Inputs
2565  * (endpoint, asoc, chunk)
2566  *
2567  * Outputs
2568  * (asoc, reply_msg, msg_up, timers, counters)
2569  *
2570  * The return value is the disposition of the chunk.
2571  */
2572 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2573                                      const struct sctp_association *asoc,
2574                                      const sctp_subtype_t type,
2575                                      void *arg,
2576                                      sctp_cmd_seq_t *commands)
2577 {
2578         struct sctp_chunk *chunk = arg;
2579         sctp_datahdr_t *data_hdr;
2580         struct sctp_chunk *err;
2581         size_t datalen;
2582         int tmp;
2583         __u32 tsn;
2584
2585         if (!sctp_vtag_verify(chunk, asoc)) {
2586                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2587                                 SCTP_NULL());
2588                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2589         }
2590
2591         data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *) chunk->skb->data;
2592         skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
2593
2594         tsn = ntohl(data_hdr->tsn);
2595
2596         SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
2597
2598         /* ASSERT:  Now skb->data is really the user data.  */
2599
2600         /* Process ECN based congestion.
2601          *
2602          * Since the chunk structure is reused for all chunks within
2603          * a packet, we use ecn_ce_done to track if we've already
2604          * done CE processing for this packet.
2605          *
2606          * We need to do ECN processing even if we plan to discard the
2607          * chunk later.
2608          */
2609         if (!chunk->ecn_ce_done) {
2610                 struct sctp_af *af;
2611                 chunk->ecn_ce_done = 1;
2612
2613                 af = sctp_get_af_specific(
2614                         ipver2af(chunk->skb->nh.iph->version));
2615
2616                 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
2617                         /* Do real work as sideffect. */
2618                         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
2619                                         SCTP_U32(tsn));
2620                 }
2621         }
2622
2623         tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
2624         if (tmp < 0) {
2625                 /* The TSN is too high--silently discard the chunk and
2626                  * count on it getting retransmitted later.
2627                  */
2628                 goto gen_shutdown;
2629         } else if (tmp > 0) {
2630                 /* This is a duplicate.  Record it.  */
2631                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
2632                 goto gen_shutdown;
2633         }
2634
2635         /* This is a new TSN.  */
2636
2637         datalen = ntohs(chunk->chunk_hdr->length);
2638         datalen -= sizeof(sctp_data_chunk_t);
2639
2640         /*
2641          * Section 3.3.10.9 No User Data (9)
2642          *
2643          * Cause of error
2644          * ---------------
2645          * No User Data:  This error cause is returned to the originator of a
2646          * DATA chunk if a received DATA chunk has no user data.
2647          */
2648         if (unlikely(0 == datalen)) {
2649                 err = sctp_make_abort_no_data(asoc, chunk, tsn);
2650                 if (err) {
2651                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
2652                                         SCTP_CHUNK(err));
2653                 }
2654                 /* We are going to ABORT, so we might as well stop
2655                  * processing the rest of the chunks in the packet.
2656                  */
2657                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
2658                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
2659                                 SCTP_U32(SCTP_ERROR_NO_DATA));
2660                 SCTP_INC_STATS(SctpAborteds);
2661                 SCTP_DEC_STATS(SctpCurrEstab);
2662                 return SCTP_DISPOSITION_CONSUME;
2663         }
2664
2665         /* We are accepting this DATA chunk. */
2666
2667         /* Record the fact that we have received this TSN.  */
2668         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
2669
2670         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
2671                 SCTP_INC_STATS(SctpInUnorderChunks);
2672         else
2673                 SCTP_INC_STATS(SctpInOrderChunks);
2674
2675         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2676          *
2677          * If an endpoint receive a DATA chunk with an invalid stream
2678          * identifier, it shall acknowledge the reception of the DATA chunk
2679          * following the normal procedure, immediately send an ERROR chunk
2680          * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
2681          * and discard the DATA chunk.
2682          */
2683         if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
2684                 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
2685                                          &data_hdr->stream,
2686                                          sizeof(data_hdr->stream));
2687                 if (err) {
2688                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
2689                                         SCTP_CHUNK(err));
2690                 }
2691         }
2692
2693         /* Go a head and force a SACK, since we are shutting down. */
2694 gen_shutdown:
2695         /* Implementor's Guide.
2696          *
2697          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
2698          * respond to each received packet containing one or more DATA chunk(s)
2699          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
2700          */
2701         if (chunk->end_of_packet) {
2702                 /* We must delay the chunk creation since the cumulative
2703                  * TSN has not been updated yet.
2704                  */
2705                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
2706                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2707                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2708                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2709         }
2710         return SCTP_DISPOSITION_CONSUME;
2711 }
2712
2713 /*
2714  * Section: 6.2  Processing a Received SACK
2715  * D) Any time a SACK arrives, the endpoint performs the following:
2716  *
2717  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
2718  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
2719  *     increasing, a SACK whose Cumulative TSN Ack is less than the
2720  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
2721  *
2722  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
2723  *     of bytes still outstanding after processing the Cumulative TSN Ack
2724  *     and the Gap Ack Blocks.
2725  *
2726  *     iii) If the SACK is missing a TSN that was previously
2727  *     acknowledged via a Gap Ack Block (e.g., the data receiver
2728  *     reneged on the data), then mark the corresponding DATA chunk
2729  *     as available for retransmit:  Mark it as missing for fast
2730  *     retransmit as described in Section 7.2.4 and if no retransmit
2731  *     timer is running for the destination address to which the DATA
2732  *     chunk was originally transmitted, then T3-rtx is started for
2733  *     that destination address.
2734  *
2735  * Verification Tag:  8.5 Verification Tag [Normal verification]
2736  *
2737  * Inputs
2738  * (endpoint, asoc, chunk)
2739  *
2740  * Outputs
2741  * (asoc, reply_msg, msg_up, timers, counters)
2742  *
2743  * The return value is the disposition of the chunk.
2744  */
2745 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
2746                                         const struct sctp_association *asoc,
2747                                         const sctp_subtype_t type,
2748                                         void *arg,
2749                                         sctp_cmd_seq_t *commands)
2750 {
2751         struct sctp_chunk *chunk = arg;
2752         sctp_sackhdr_t *sackh;
2753         __u32 ctsn;
2754
2755         if (!sctp_vtag_verify(chunk, asoc))
2756                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2757
2758         /* Pull the SACK chunk from the data buffer */
2759         sackh = sctp_sm_pull_sack(chunk);
2760         /* Was this a bogus SACK? */
2761         if (!sackh)
2762                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2763         chunk->subh.sack_hdr = sackh;
2764         ctsn = ntohl(sackh->cum_tsn_ack);
2765
2766         /* i) If Cumulative TSN Ack is less than the Cumulative TSN
2767          *     Ack Point, then drop the SACK.  Since Cumulative TSN
2768          *     Ack is monotonically increasing, a SACK whose
2769          *     Cumulative TSN Ack is less than the Cumulative TSN Ack
2770          *     Point indicates an out-of-order SACK.
2771          */
2772         if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2773                 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2774                 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2775                 return SCTP_DISPOSITION_DISCARD;
2776         }
2777
2778         /* Return this SACK for further processing.  */
2779         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
2780
2781         /* Note: We do the rest of the work on the PROCESS_SACK
2782          * sideeffect.
2783          */
2784         return SCTP_DISPOSITION_CONSUME;
2785 }
2786
2787 /*
2788  * Generate an ABORT in response to a packet.
2789  *
2790  * Section: 8.4 Handle "Out of the blue" Packets
2791  *
2792  * 8) The receiver should respond to the sender of the OOTB packet
2793  *    with an ABORT.  When sending the ABORT, the receiver of the
2794  *    OOTB packet MUST fill in the Verification Tag field of the
2795  *    outbound packet with the value found in the Verification Tag
2796  *    field of the OOTB packet and set the T-bit in the Chunk Flags
2797  *    to indicate that no TCB was found.  After sending this ABORT,
2798  *    the receiver of the OOTB packet shall discard the OOTB packet
2799  *    and take no further action.
2800  *
2801  * Verification Tag:
2802  *
2803  * The return value is the disposition of the chunk.
2804 */
2805 sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
2806                                         const struct sctp_association *asoc,
2807                                         const sctp_subtype_t type,
2808                                         void *arg,
2809                                         sctp_cmd_seq_t *commands)
2810 {
2811         struct sctp_packet *packet = NULL;
2812         struct sctp_chunk *chunk = arg;
2813         struct sctp_chunk *abort;
2814
2815         packet = sctp_ootb_pkt_new(asoc, chunk);
2816
2817         if (packet) {
2818                 /* Make an ABORT. The T bit will be set if the asoc
2819                  * is NULL.
2820                  */
2821                 abort = sctp_make_abort(asoc, chunk, 0);
2822                 if (!abort) {
2823                         sctp_ootb_pkt_free(packet);
2824                         return SCTP_DISPOSITION_NOMEM;
2825                 }
2826
2827                 /* Set the skb to the belonging sock for accounting.  */
2828                 abort->skb->sk = ep->base.sk;
2829
2830                 sctp_packet_append_chunk(packet, abort);
2831
2832                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
2833                                 SCTP_PACKET(packet));
2834
2835                 SCTP_INC_STATS(SctpOutCtrlChunks);
2836
2837                 return SCTP_DISPOSITION_CONSUME;
2838         }
2839
2840         return SCTP_DISPOSITION_NOMEM;
2841 }
2842
2843 /*
2844  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
2845  * event as ULP notification for each cause included in the chunk.
2846  *
2847  * API 5.3.1.3 - SCTP_REMOTE_ERROR
2848  *
2849  * The return value is the disposition of the chunk.
2850 */
2851 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
2852                                         const struct sctp_association *asoc,
2853                                         const sctp_subtype_t type,
2854                                         void *arg,
2855                                         sctp_cmd_seq_t *commands)
2856 {
2857         struct sctp_chunk *chunk = arg;
2858         struct sctp_ulpevent *ev;
2859
2860         while (chunk->chunk_end > chunk->skb->data) {
2861                 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
2862                                                      GFP_ATOMIC);
2863                 if (!ev)
2864                         goto nomem;
2865
2866                 if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
2867                                   SCTP_ULPEVENT(ev))) {
2868                         sctp_ulpevent_free(ev);
2869                         goto nomem;
2870                 }
2871
2872                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
2873                                 SCTP_CHUNK(chunk));     
2874         }
2875         return SCTP_DISPOSITION_CONSUME;
2876
2877 nomem:
2878         return SCTP_DISPOSITION_NOMEM;
2879 }
2880
2881 /*
2882  * Process an inbound SHUTDOWN ACK.
2883  *
2884  * From Section 9.2:
2885  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
2886  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
2887  * peer, and remove all record of the association.
2888  *
2889  * The return value is the disposition.
2890  */
2891 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
2892                                         const struct sctp_association *asoc,
2893                                         const sctp_subtype_t type,
2894                                         void *arg,
2895                                         sctp_cmd_seq_t *commands)
2896 {
2897         struct sctp_chunk *chunk = arg;
2898         struct sctp_chunk *reply;
2899         struct sctp_ulpevent *ev;
2900
2901         if (!sctp_vtag_verify(chunk, asoc))
2902                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2903
2904         /* 10.2 H) SHUTDOWN COMPLETE notification
2905          *
2906          * When SCTP completes the shutdown procedures (section 9.2) this
2907          * notification is passed to the upper layer.
2908          */
2909         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
2910                                              0, 0, 0, GFP_ATOMIC);
2911         if (!ev)
2912                 goto nomem;
2913
2914         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2915
2916         /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
2917          * stop the T2-shutdown timer,
2918          */
2919         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2920                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2921
2922         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2923                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2924
2925         /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
2926         reply = sctp_make_shutdown_complete(asoc, chunk);
2927         if (!reply)
2928                 goto nomem;
2929
2930         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2931                         SCTP_STATE(SCTP_STATE_CLOSED));
2932         SCTP_INC_STATS(SctpShutdowns);
2933         SCTP_DEC_STATS(SctpCurrEstab);
2934         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2935
2936         /* ...and remove all record of the association. */
2937         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2938         return SCTP_DISPOSITION_DELETE_TCB;
2939
2940 nomem:
2941         return SCTP_DISPOSITION_NOMEM;
2942 }
2943
2944 /*
2945  * RFC 2960, 8.4 - Handle "Out of the blue" Packets
2946  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
2947  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
2948  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
2949  *    packet must fill in the Verification Tag field of the outbound
2950  *    packet with the Verification Tag received in the SHUTDOWN ACK and
2951  *    set the T-bit in the Chunk Flags to indicate that no TCB was
2952  *    found. Otherwise,
2953  *
2954  * 8) The receiver should respond to the sender of the OOTB packet with
2955  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
2956  *    MUST fill in the Verification Tag field of the outbound packet
2957  *    with the value found in the Verification Tag field of the OOTB
2958  *    packet and set the T-bit in the Chunk Flags to indicate that no
2959  *    TCB was found.  After sending this ABORT, the receiver of the OOTB
2960  *    packet shall discard the OOTB packet and take no further action.
2961  */
2962 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
2963                                 const struct sctp_association *asoc,
2964                                 const sctp_subtype_t type,
2965                                 void *arg,
2966                                 sctp_cmd_seq_t *commands)
2967 {
2968         struct sctp_chunk *chunk = arg;
2969         struct sk_buff *skb = chunk->skb;
2970         sctp_chunkhdr_t *ch;
2971         __u8 *ch_end;
2972         int ootb_shut_ack = 0;
2973
2974         SCTP_INC_STATS(SctpOutOfBlues);
2975
2976         ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
2977         do {
2978                 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
2979
2980                 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
2981                         ootb_shut_ack = 1;
2982
2983                 ch = (sctp_chunkhdr_t *) ch_end;
2984         } while (ch_end < skb->tail);
2985
2986         if (ootb_shut_ack)
2987                 sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
2988         else
2989                 sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
2990
2991         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2992 }
2993
2994 /*
2995  * Handle an "Out of the blue" SHUTDOWN ACK.
2996  *
2997  * Section: 8.4 5)
2998  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
2999  *   respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3000  *   When sending the SHUTDOWN COMPLETE, the receiver of the OOTB packet
3001  *   must fill in the Verification Tag field of the outbound packet with
3002  *   the Verification Tag received in the SHUTDOWN ACK and set the
3003  *   T-bit in the Chunk Flags to indicate that no TCB was found.
3004  *
3005  * Inputs
3006  * (endpoint, asoc, type, arg, commands)
3007  *
3008  * Outputs
3009  * (sctp_disposition_t)
3010  *
3011  * The return value is the disposition of the chunk.
3012  */
3013 sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3014                                       const struct sctp_association *asoc,
3015                                       const sctp_subtype_t type,
3016                                       void *arg,
3017                                       sctp_cmd_seq_t *commands)
3018 {
3019         struct sctp_packet *packet = NULL;
3020         struct sctp_chunk *chunk = arg;
3021         struct sctp_chunk *shut;
3022
3023         packet = sctp_ootb_pkt_new(asoc, chunk);
3024
3025         if (packet) {
3026                 /* Make an SHUTDOWN_COMPLETE.
3027                  * The T bit will be set if the asoc is NULL.
3028                  */
3029                 shut = sctp_make_shutdown_complete(asoc, chunk);
3030                 if (!shut) {
3031                         sctp_ootb_pkt_free(packet);
3032                         return SCTP_DISPOSITION_NOMEM;
3033                 }
3034
3035                 /* Set the skb to the belonging sock for accounting.  */
3036                 shut->skb->sk = ep->base.sk;
3037
3038                 sctp_packet_append_chunk(packet, shut);
3039
3040                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3041                                 SCTP_PACKET(packet));
3042
3043                 SCTP_INC_STATS(SctpOutCtrlChunks);
3044
3045                 return SCTP_DISPOSITION_CONSUME;
3046         }
3047
3048         return SCTP_DISPOSITION_NOMEM;
3049 }
3050
3051 /*
3052  * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3053  *
3054  * Verification Tag:  8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3055  *   If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3056  *   procedures in section 8.4 SHOULD be followed, in other words it
3057  *   should be treated as an Out Of The Blue packet.
3058  *   [This means that we do NOT check the Verification Tag on these
3059  *   chunks. --piggy ]
3060  *
3061  */
3062 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3063                                       const struct sctp_association *asoc,
3064                                       const sctp_subtype_t type,
3065                                       void *arg,
3066                                       sctp_cmd_seq_t *commands)
3067 {
3068         /* Although we do have an association in this case, it corresponds
3069          * to a restarted association. So the packet is treated as an OOTB
3070          * packet and the state function that handles OOTB SHUTDOWN_ACK is
3071          * called with a NULL association.
3072          */
3073         return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3074 }
3075
3076 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.  */
3077 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3078                                      const struct sctp_association *asoc,
3079                                      const sctp_subtype_t type, void *arg,
3080                                      sctp_cmd_seq_t *commands)
3081 {
3082         struct sctp_chunk       *chunk = arg;
3083         struct sctp_chunk       *asconf_ack = NULL;
3084         sctp_addiphdr_t         *hdr;
3085         __u32                   serial;
3086
3087         hdr = (sctp_addiphdr_t *)chunk->skb->data;
3088         serial = ntohl(hdr->serial);
3089
3090         /* ADDIP 4.2 C1) Compare the value of the serial number to the value
3091          * the endpoint stored in a new association variable
3092          * 'Peer-Serial-Number'. 
3093          */
3094         if (serial == asoc->peer.addip_serial + 1) {
3095                 /* ADDIP 4.2 C2) If the value found in the serial number is
3096                  * equal to the ('Peer-Serial-Number' + 1), the endpoint MUST
3097                  * do V1-V5.
3098                  */
3099                 asconf_ack = sctp_process_asconf((struct sctp_association *)
3100                                                  asoc, chunk);
3101                 if (!asconf_ack)
3102                         return SCTP_DISPOSITION_NOMEM;
3103         } else if (serial == asoc->peer.addip_serial) {
3104                 /* ADDIP 4.2 C3) If the value found in the serial number is
3105                  * equal to the value stored in the 'Peer-Serial-Number'
3106                  * IMPLEMENTATION NOTE: As an optimization a receiver may wish
3107                  * to save the last ASCONF-ACK for some predetermined period of                  * time and instead of re-processing the ASCONF (with the same
3108                  * serial number) it may just re-transmit the ASCONF-ACK.
3109                  */
3110                 if (asoc->addip_last_asconf_ack)
3111                         asconf_ack = asoc->addip_last_asconf_ack;
3112                 else
3113                         return SCTP_DISPOSITION_DISCARD;
3114         } else {
3115                 /* ADDIP 4.2 C4) Otherwise, the ASCONF Chunk is discarded since 
3116                  * it must be either a stale packet or from an attacker.
3117                  */     
3118                 return SCTP_DISPOSITION_DISCARD;
3119         }
3120
3121         /* ADDIP 4.2 C5) In both cases C2 and C3 the ASCONF-ACK MUST be sent
3122          * back to the source address contained in the IP header of the ASCONF
3123          * being responded to.
3124          */
3125         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3126         
3127         return SCTP_DISPOSITION_CONSUME;
3128 }
3129
3130 /*
3131  * ADDIP Section 4.3 General rules for address manipulation
3132  * When building TLV parameters for the ASCONF Chunk that will add or
3133  * delete IP addresses the D0 to D13 rules should be applied:
3134  */
3135 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3136                                          const struct sctp_association *asoc,
3137                                          const sctp_subtype_t type, void *arg,
3138                                          sctp_cmd_seq_t *commands)
3139 {
3140         struct sctp_chunk       *asconf_ack = arg;
3141         struct sctp_chunk       *last_asconf = asoc->addip_last_asconf;
3142         struct sctp_chunk       *abort;
3143         sctp_addiphdr_t         *addip_hdr;
3144         __u32                   sent_serial, rcvd_serial;
3145
3146         addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3147         rcvd_serial = ntohl(addip_hdr->serial);
3148
3149         if (last_asconf) {
3150                 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3151                 sent_serial = ntohl(addip_hdr->serial);
3152         } else {
3153                 sent_serial = asoc->addip_serial - 1;
3154         }
3155
3156         /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3157          * equal to the next serial number to be used but no ASCONF chunk is
3158          * outstanding the endpoint MUST ABORT the association. Note that a
3159          * sequence number is greater than if it is no more than 2^^31-1
3160          * larger than the current sequence number (using serial arithmetic).
3161          */
3162         if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3163             !(asoc->addip_last_asconf)) {
3164                 abort = sctp_make_abort(asoc, asconf_ack,
3165                                         sizeof(sctp_errhdr_t));
3166                 if (abort) {
3167                         sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, NULL, 0);
3168                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3169                                         SCTP_CHUNK(abort));
3170                 }
3171                 /* We are going to ABORT, so we might as well stop
3172                  * processing the rest of the chunks in the packet.
3173                  */
3174                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3175                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3176                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3177                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3178                                 SCTP_U32(SCTP_ERROR_ASCONF_ACK));
3179                 SCTP_INC_STATS(SctpAborteds);
3180                 SCTP_DEC_STATS(SctpCurrEstab);
3181                 return SCTP_DISPOSITION_ABORT;
3182         }
3183
3184         if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3185                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3186                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3187
3188                 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3189                                              asconf_ack))
3190                         return SCTP_DISPOSITION_CONSUME;
3191
3192                 abort = sctp_make_abort(asoc, asconf_ack,
3193                                         sizeof(sctp_errhdr_t));
3194                 if (abort) {
3195                         sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, NULL, 0);
3196                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3197                                         SCTP_CHUNK(abort));
3198                 }
3199                 /* We are going to ABORT, so we might as well stop
3200                  * processing the rest of the chunks in the packet.
3201                  */
3202                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3203                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3204                                 SCTP_U32(SCTP_ERROR_ASCONF_ACK));
3205                 SCTP_INC_STATS(SctpAborteds);
3206                 SCTP_DEC_STATS(SctpCurrEstab);
3207                 return SCTP_DISPOSITION_ABORT;
3208         }
3209
3210         return SCTP_DISPOSITION_DISCARD;
3211 }
3212
3213 /*
3214  * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3215  *
3216  * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3217  * its cumulative TSN point to the value carried in the FORWARD TSN
3218  * chunk, and then MUST further advance its cumulative TSN point locally
3219  * if possible.
3220  * After the above processing, the data receiver MUST stop reporting any
3221  * missing TSNs earlier than or equal to the new cumulative TSN point.
3222  *
3223  * Verification Tag:  8.5 Verification Tag [Normal verification]
3224  *
3225  * The return value is the disposition of the chunk.
3226  */
3227 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3228                                        const struct sctp_association *asoc,
3229                                        const sctp_subtype_t type,
3230                                        void *arg,
3231                                        sctp_cmd_seq_t *commands)
3232 {
3233         struct sctp_chunk *chunk = arg;
3234         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3235         __u16 len;
3236         __u32 tsn;
3237
3238         if (!sctp_vtag_verify(chunk, asoc)) {
3239                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3240                                 SCTP_NULL());
3241                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3242         }
3243
3244         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3245         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3246         len = ntohs(chunk->chunk_hdr->length);
3247         len -= sizeof(struct sctp_chunkhdr);
3248         skb_pull(chunk->skb, len);
3249
3250         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3251         SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3252
3253         /* The TSN is too high--silently discard the chunk and count on it
3254          * getting retransmitted later.
3255          */
3256         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3257                 goto discard_noforce;
3258
3259         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3260         if (len > sizeof(struct sctp_fwdtsn_hdr))
3261                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN, 
3262                                 SCTP_CHUNK(chunk));
3263         
3264         /* Count this as receiving DATA. */
3265         if (asoc->autoclose) {
3266                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3267                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3268         }
3269         
3270         /* FIXME: For now send a SACK, but DATA processing may
3271          * send another. 
3272          */
3273         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3274         /* Start the SACK timer.  */
3275         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3276                         SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
3277
3278         return SCTP_DISPOSITION_CONSUME;
3279
3280 discard_noforce:
3281         return SCTP_DISPOSITION_DISCARD;
3282 }
3283
3284 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3285         const struct sctp_endpoint *ep,
3286         const struct sctp_association *asoc,
3287         const sctp_subtype_t type,
3288         void *arg,
3289         sctp_cmd_seq_t *commands)
3290 {
3291         struct sctp_chunk *chunk = arg;
3292         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3293         __u16 len;
3294         __u32 tsn;
3295
3296         if (!sctp_vtag_verify(chunk, asoc)) {
3297                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3298                                 SCTP_NULL());
3299                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3300         }
3301
3302         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3303         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3304         len = ntohs(chunk->chunk_hdr->length);
3305         len -= sizeof(struct sctp_chunkhdr);
3306         skb_pull(chunk->skb, len);
3307
3308         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3309         SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3310
3311         /* The TSN is too high--silently discard the chunk and count on it
3312          * getting retransmitted later.
3313          */
3314         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3315                 goto gen_shutdown;
3316
3317         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3318         if (len > sizeof(struct sctp_fwdtsn_hdr))
3319                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN, 
3320                                 SCTP_CHUNK(chunk));
3321         
3322         /* Go a head and force a SACK, since we are shutting down. */
3323 gen_shutdown:
3324         /* Implementor's Guide.
3325          *
3326          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3327          * respond to each received packet containing one or more DATA chunk(s)
3328          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3329          */
3330         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3331         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3332         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3333                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3334
3335         return SCTP_DISPOSITION_CONSUME;
3336 }
3337
3338 /*
3339  * Process an unknown chunk.
3340  *
3341  * Section: 3.2. Also, 2.1 in the implementor's guide.
3342  *
3343  * Chunk Types are encoded such that the highest-order two bits specify
3344  * the action that must be taken if the processing endpoint does not
3345  * recognize the Chunk Type.
3346  *
3347  * 00 - Stop processing this SCTP packet and discard it, do not process
3348  *      any further chunks within it.
3349  *
3350  * 01 - Stop processing this SCTP packet and discard it, do not process
3351  *      any further chunks within it, and report the unrecognized
3352  *      chunk in an 'Unrecognized Chunk Type'.
3353  *
3354  * 10 - Skip this chunk and continue processing.
3355  *
3356  * 11 - Skip this chunk and continue processing, but report in an ERROR
3357  *      Chunk using the 'Unrecognized Chunk Type' cause of error.
3358  *
3359  * The return value is the disposition of the chunk.
3360  */
3361 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
3362                                      const struct sctp_association *asoc,
3363                                      const sctp_subtype_t type,
3364                                      void *arg,
3365                                      sctp_cmd_seq_t *commands)
3366 {
3367         struct sctp_chunk *unk_chunk = arg;
3368         struct sctp_chunk *err_chunk;
3369         sctp_chunkhdr_t *hdr;
3370
3371         SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
3372
3373         if (!sctp_vtag_verify(unk_chunk, asoc))
3374                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3375
3376         switch (type.chunk & SCTP_CID_ACTION_MASK) {
3377         case SCTP_CID_ACTION_DISCARD:
3378                 /* Discard the packet.  */
3379                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3380                 break;
3381         case SCTP_CID_ACTION_DISCARD_ERR:
3382                 /* Discard the packet.  */
3383                 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3384
3385                 /* Generate an ERROR chunk as response. */
3386                 hdr = unk_chunk->chunk_hdr;
3387                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3388                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3389                                                WORD_ROUND(ntohs(hdr->length)));
3390                 if (err_chunk) {
3391                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3392                                         SCTP_CHUNK(err_chunk));
3393                 }
3394                 return SCTP_DISPOSITION_CONSUME;
3395                 break;
3396         case SCTP_CID_ACTION_SKIP:
3397                 /* Skip the chunk.  */
3398                 return SCTP_DISPOSITION_DISCARD;
3399                 break;
3400         case SCTP_CID_ACTION_SKIP_ERR:
3401                 /* Generate an ERROR chunk as response. */
3402                 hdr = unk_chunk->chunk_hdr;
3403                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3404                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3405                                                WORD_ROUND(ntohs(hdr->length)));
3406                 if (err_chunk) {
3407                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3408                                         SCTP_CHUNK(err_chunk));
3409                 }
3410                 /* Skip the chunk.  */
3411                 return SCTP_DISPOSITION_CONSUME;
3412                 break;
3413         default:
3414                 break;
3415         }
3416
3417         return SCTP_DISPOSITION_DISCARD;
3418 }
3419
3420 /*
3421  * Discard the chunk.
3422  *
3423  * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
3424  * [Too numerous to mention...]
3425  * Verification Tag: No verification needed.
3426  * Inputs
3427  * (endpoint, asoc, chunk)
3428  *
3429  * Outputs
3430  * (asoc, reply_msg, msg_up, timers, counters)
3431  *
3432  * The return value is the disposition of the chunk.
3433  */
3434 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
3435                                          const struct sctp_association *asoc,
3436                                          const sctp_subtype_t type,
3437                                          void *arg,
3438                                          sctp_cmd_seq_t *commands)
3439 {
3440         SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
3441         return SCTP_DISPOSITION_DISCARD;
3442 }
3443
3444 /*
3445  * Discard the whole packet.
3446  *
3447  * Section: 8.4 2)
3448  *
3449  * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
3450  *    silently discard the OOTB packet and take no further action.
3451  *    Otherwise,
3452  *
3453  * Verification Tag: No verification necessary
3454  *
3455  * Inputs
3456  * (endpoint, asoc, chunk)
3457  *
3458  * Outputs
3459  * (asoc, reply_msg, msg_up, timers, counters)
3460  *
3461  * The return value is the disposition of the chunk.
3462  */
3463 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
3464                                     const struct sctp_association *asoc,
3465                                     const sctp_subtype_t type,
3466                                     void *arg,
3467                                     sctp_cmd_seq_t *commands)
3468 {
3469         sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3470
3471         return SCTP_DISPOSITION_CONSUME;
3472 }
3473
3474
3475 /*
3476  * The other end is violating protocol.
3477  *
3478  * Section: Not specified
3479  * Verification Tag: Not specified
3480  * Inputs
3481  * (endpoint, asoc, chunk)
3482  *
3483  * Outputs
3484  * (asoc, reply_msg, msg_up, timers, counters)
3485  *
3486  * We simply tag the chunk as a violation.  The state machine will log
3487  * the violation and continue.
3488  */
3489 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
3490                                      const struct sctp_association *asoc,
3491                                      const sctp_subtype_t type,
3492                                      void *arg,
3493                                      sctp_cmd_seq_t *commands)
3494 {
3495         return SCTP_DISPOSITION_VIOLATION;
3496 }
3497
3498 /***************************************************************************
3499  * These are the state functions for handling primitive (Section 10) events.
3500  ***************************************************************************/
3501 /*
3502  * sctp_sf_do_prm_asoc
3503  *
3504  * Section: 10.1 ULP-to-SCTP
3505  * B) Associate
3506  *
3507  * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
3508  * outbound stream count)
3509  * -> association id [,destination transport addr list] [,outbound stream
3510  * count]
3511  *
3512  * This primitive allows the upper layer to initiate an association to a
3513  * specific peer endpoint.
3514  *
3515  * The peer endpoint shall be specified by one of the transport addresses
3516  * which defines the endpoint (see Section 1.4).  If the local SCTP
3517  * instance has not been initialized, the ASSOCIATE is considered an
3518  * error.
3519  * [This is not relevant for the kernel implementation since we do all
3520  * initialization at boot time.  It we hadn't initialized we wouldn't
3521  * get anywhere near this code.]
3522  *
3523  * An association id, which is a local handle to the SCTP association,
3524  * will be returned on successful establishment of the association. If
3525  * SCTP is not able to open an SCTP association with the peer endpoint,
3526  * an error is returned.
3527  * [In the kernel implementation, the struct sctp_association needs to
3528  * be created BEFORE causing this primitive to run.]
3529  *
3530  * Other association parameters may be returned, including the
3531  * complete destination transport addresses of the peer as well as the
3532  * outbound stream count of the local endpoint. One of the transport
3533  * address from the returned destination addresses will be selected by
3534  * the local endpoint as default primary path for sending SCTP packets
3535  * to this peer.  The returned "destination transport addr list" can
3536  * be used by the ULP to change the default primary path or to force
3537  * sending a packet to a specific transport address.  [All of this
3538  * stuff happens when the INIT ACK arrives.  This is a NON-BLOCKING
3539  * function.]
3540  *
3541  * Mandatory attributes:
3542  *
3543  * o local SCTP instance name - obtained from the INITIALIZE operation.
3544  *   [This is the argument asoc.]
3545  * o destination transport addr - specified as one of the transport
3546  * addresses of the peer endpoint with which the association is to be
3547  * established.
3548  *  [This is asoc->peer.active_path.]
3549  * o outbound stream count - the number of outbound streams the ULP
3550  * would like to open towards this peer endpoint.
3551  * [BUG: This is not currently implemented.]
3552  * Optional attributes:
3553  *
3554  * None.
3555  *
3556  * The return value is a disposition.
3557  */
3558 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
3559                                        const struct sctp_association *asoc,
3560                                        const sctp_subtype_t type,
3561                                        void *arg,
3562                                        sctp_cmd_seq_t *commands)
3563 {
3564         struct sctp_chunk *repl;
3565
3566         /* The comment below says that we enter COOKIE-WAIT AFTER
3567          * sending the INIT, but that doesn't actually work in our
3568          * implementation...
3569          */
3570         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3571                         SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
3572
3573         /* RFC 2960 5.1 Normal Establishment of an Association
3574          *
3575          * A) "A" first sends an INIT chunk to "Z".  In the INIT, "A"
3576          * must provide its Verification Tag (Tag_A) in the Initiate
3577          * Tag field.  Tag_A SHOULD be a random number in the range of
3578          * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
3579          */
3580
3581         repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
3582         if (!repl)
3583                 goto nomem;
3584
3585         /* Cast away the const modifier, as we want to just
3586          * rerun it through as a sideffect.
3587          */
3588         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC,
3589                         SCTP_ASOC((struct sctp_association *) asoc));
3590
3591         /* After sending the INIT, "A" starts the T1-init timer and
3592          * enters the COOKIE-WAIT state.
3593          */
3594         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
3595                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
3596         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
3597         return SCTP_DISPOSITION_CONSUME;
3598
3599 nomem:
3600         return SCTP_DISPOSITION_NOMEM;
3601 }
3602
3603 /*
3604  * Process the SEND primitive.
3605  *
3606  * Section: 10.1 ULP-to-SCTP
3607  * E) Send
3608  *
3609  * Format: SEND(association id, buffer address, byte count [,context]
3610  *         [,stream id] [,life time] [,destination transport address]
3611  *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
3612  * -> result
3613  *
3614  * This is the main method to send user data via SCTP.
3615  *
3616  * Mandatory attributes:
3617  *
3618  *  o association id - local handle to the SCTP association
3619  *
3620  *  o buffer address - the location where the user message to be
3621  *    transmitted is stored;
3622  *
3623  *  o byte count - The size of the user data in number of bytes;
3624  *
3625  * Optional attributes:
3626  *
3627  *  o context - an optional 32 bit integer that will be carried in the
3628  *    sending failure notification to the ULP if the transportation of
3629  *    this User Message fails.
3630  *
3631  *  o stream id - to indicate which stream to send the data on. If not
3632  *    specified, stream 0 will be used.
3633  *
3634  *  o life time - specifies the life time of the user data. The user data
3635  *    will not be sent by SCTP after the life time expires. This
3636  *    parameter can be used to avoid efforts to transmit stale
3637  *    user messages. SCTP notifies the ULP if the data cannot be
3638  *    initiated to transport (i.e. sent to the destination via SCTP's
3639  *    send primitive) within the life time variable. However, the
3640  *    user data will be transmitted if SCTP has attempted to transmit a
3641  *    chunk before the life time expired.
3642  *
3643  *  o destination transport address - specified as one of the destination
3644  *    transport addresses of the peer endpoint to which this packet
3645  *    should be sent. Whenever possible, SCTP should use this destination
3646  *    transport address for sending the packets, instead of the current
3647  *    primary path.
3648  *
3649  *  o unorder flag - this flag, if present, indicates that the user
3650  *    would like the data delivered in an unordered fashion to the peer
3651  *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
3652  *    message).
3653  *
3654  *  o no-bundle flag - instructs SCTP not to bundle this user data with
3655  *    other outbound DATA chunks. SCTP MAY still bundle even when
3656  *    this flag is present, when faced with network congestion.
3657  *
3658  *  o payload protocol-id - A 32 bit unsigned integer that is to be
3659  *    passed to the peer indicating the type of payload protocol data
3660  *    being transmitted. This value is passed as opaque data by SCTP.
3661  *
3662  * The return value is the disposition.
3663  */
3664 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
3665                                        const struct sctp_association *asoc,
3666                                        const sctp_subtype_t type,
3667                                        void *arg,
3668                                        sctp_cmd_seq_t *commands)
3669 {
3670         struct sctp_chunk *chunk = arg;
3671
3672         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
3673         return SCTP_DISPOSITION_CONSUME;
3674 }
3675
3676 /*
3677  * Process the SHUTDOWN primitive.
3678  *
3679  * Section: 10.1:
3680  * C) Shutdown
3681  *
3682  * Format: SHUTDOWN(association id)
3683  * -> result
3684  *
3685  * Gracefully closes an association. Any locally queued user data
3686  * will be delivered to the peer. The association will be terminated only
3687  * after the peer acknowledges all the SCTP packets sent.  A success code
3688  * will be returned on successful termination of the association. If
3689  * attempting to terminate the association results in a failure, an error
3690  * code shall be returned.
3691  *
3692  * Mandatory attributes:
3693  *
3694  *  o association id - local handle to the SCTP association
3695  *
3696  * Optional attributes:
3697  *
3698  * None.
3699  *
3700  * The return value is the disposition.
3701  */
3702 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
3703         const struct sctp_endpoint *ep,
3704         const struct sctp_association *asoc,
3705         const sctp_subtype_t type,
3706         void *arg,
3707         sctp_cmd_seq_t *commands)
3708 {
3709         int disposition;
3710
3711         /* From 9.2 Shutdown of an Association
3712          * Upon receipt of the SHUTDOWN primitive from its upper
3713          * layer, the endpoint enters SHUTDOWN-PENDING state and
3714          * remains there until all outstanding data has been
3715          * acknowledged by its peer. The endpoint accepts no new data
3716          * from its upper layer, but retransmits data to the far end
3717          * if necessary to fill gaps.
3718          */
3719         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3720                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
3721
3722         /* sctpimpguide-05 Section 2.12.2
3723          * The sender of the SHUTDOWN MAY also start an overall guard timer
3724          * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
3725          */
3726         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
3727                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3728
3729         disposition = SCTP_DISPOSITION_CONSUME;
3730         if (sctp_outq_is_empty(&asoc->outqueue)) {
3731                 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
3732                                                             arg, commands);
3733         }
3734         return disposition;
3735 }
3736
3737 /*
3738  * Process the ABORT primitive.
3739  *
3740  * Section: 10.1:
3741  * C) Abort
3742  *
3743  * Format: Abort(association id [, cause code])
3744  * -> result
3745  *
3746  * Ungracefully closes an association. Any locally queued user data
3747  * will be discarded and an ABORT chunk is sent to the peer.  A success code
3748  * will be returned on successful abortion of the association. If
3749  * attempting to abort the association results in a failure, an error
3750  * code shall be returned.
3751  *
3752  * Mandatory attributes:
3753  *
3754  *  o association id - local handle to the SCTP association
3755  *
3756  * Optional attributes:
3757  *
3758  *  o cause code - reason of the abort to be passed to the peer
3759  *
3760  * None.
3761  *
3762  * The return value is the disposition.
3763  */
3764 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
3765         const struct sctp_endpoint *ep,
3766         const struct sctp_association *asoc,
3767         const sctp_subtype_t type,
3768         void *arg,
3769         sctp_cmd_seq_t *commands)
3770 {
3771         /* From 9.1 Abort of an Association
3772          * Upon receipt of the ABORT primitive from its upper
3773          * layer, the endpoint enters CLOSED state and
3774          * discard all outstanding data has been
3775          * acknowledged by its peer. The endpoint accepts no new data
3776          * from its upper layer, but retransmits data to the far end
3777          * if necessary to fill gaps.
3778          */
3779         struct msghdr *msg = arg;
3780         struct sctp_chunk *abort;
3781         sctp_disposition_t retval;
3782
3783         retval = SCTP_DISPOSITION_CONSUME;
3784
3785         /* Generate ABORT chunk to send the peer.  */
3786         abort = sctp_make_abort_user(asoc, NULL, msg);
3787         if (!abort)
3788                 retval = SCTP_DISPOSITION_NOMEM;
3789         else
3790                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
3791
3792         /* Even if we can't send the ABORT due to low memory delete the
3793          * TCB.  This is a departure from our typical NOMEM handling.
3794          */
3795
3796         /* Delete the established association. */
3797         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3798                         SCTP_U32(SCTP_ERROR_USER_ABORT));
3799
3800         SCTP_INC_STATS(SctpAborteds);
3801         SCTP_DEC_STATS(SctpCurrEstab);
3802
3803         return retval;
3804 }
3805
3806 /* We tried an illegal operation on an association which is closed.  */
3807 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
3808                                         const struct sctp_association *asoc,
3809                                         const sctp_subtype_t type,
3810                                         void *arg,
3811                                         sctp_cmd_seq_t *commands)
3812 {
3813         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
3814         return SCTP_DISPOSITION_CONSUME;
3815 }
3816
3817 /* We tried an illegal operation on an association which is shutting
3818  * down.
3819  */
3820 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
3821                                           const struct sctp_association *asoc,
3822                                           const sctp_subtype_t type,
3823                                           void *arg,
3824                                           sctp_cmd_seq_t *commands)
3825 {
3826         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
3827                         SCTP_ERROR(-ESHUTDOWN));
3828         return SCTP_DISPOSITION_CONSUME;
3829 }
3830
3831 /*
3832  * sctp_cookie_wait_prm_shutdown
3833  *
3834  * Section: 4 Note: 2
3835  * Verification Tag:
3836  * Inputs
3837  * (endpoint, asoc)
3838  *
3839  * The RFC does not explicitly address this issue, but is the route through the
3840  * state table when someone issues a shutdown while in COOKIE_WAIT state.
3841  *
3842  * Outputs
3843  * (timers)
3844  */
3845 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
3846         const struct sctp_endpoint *ep,
3847         const struct sctp_association *asoc,
3848         const sctp_subtype_t type,
3849         void *arg,
3850         sctp_cmd_seq_t *commands)
3851 {
3852         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3853                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
3854
3855         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3856                         SCTP_STATE(SCTP_STATE_CLOSED));
3857
3858         SCTP_INC_STATS(SctpShutdowns);
3859
3860         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3861
3862         return SCTP_DISPOSITION_DELETE_TCB;
3863 }
3864
3865 /*
3866  * sctp_cookie_echoed_prm_shutdown
3867  *
3868  * Section: 4 Note: 2
3869  * Verification Tag:
3870  * Inputs
3871  * (endpoint, asoc)
3872  *
3873  * The RFC does not explcitly address this issue, but is the route through the
3874  * state table when someone issues a shutdown while in COOKIE_ECHOED state.
3875  *
3876  * Outputs
3877  * (timers)
3878  */
3879 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
3880         const struct sctp_endpoint *ep,
3881         const struct sctp_association *asoc,
3882         const sctp_subtype_t type,
3883         void *arg, sctp_cmd_seq_t *commands)
3884 {
3885         /* There is a single T1 timer, so we should be able to use
3886          * common function with the COOKIE-WAIT state.
3887          */
3888         return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
3889 }
3890
3891 /*
3892  * sctp_sf_cookie_wait_prm_abort
3893  *
3894  * Section: 4 Note: 2
3895  * Verification Tag:
3896  * Inputs
3897  * (endpoint, asoc)
3898  *
3899  * The RFC does not explicitly address this issue, but is the route through the
3900  * state table when someone issues an abort while in COOKIE_WAIT state.
3901  *
3902  * Outputs
3903  * (timers)
3904  */
3905 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
3906         const struct sctp_endpoint *ep,
3907         const struct sctp_association *asoc,
3908         const sctp_subtype_t type,
3909         void *arg,
3910         sctp_cmd_seq_t *commands)
3911 {
3912         struct msghdr *msg = arg;
3913         struct sctp_chunk *abort;
3914         sctp_disposition_t retval;
3915
3916         /* Stop T1-init timer */
3917         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3918                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
3919         retval = SCTP_DISPOSITION_CONSUME;
3920
3921         /* Generate ABORT chunk to send the peer */
3922         abort = sctp_make_abort_user(asoc, NULL, msg);
3923         if (!abort)
3924                 retval = SCTP_DISPOSITION_NOMEM;
3925         else
3926                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
3927
3928         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3929                         SCTP_STATE(SCTP_STATE_CLOSED));
3930
3931         SCTP_INC_STATS(SctpAborteds);
3932
3933         /* Even if we can't send the ABORT due to low memory delete the
3934          * TCB.  This is a departure from our typical NOMEM handling.
3935          */
3936
3937         /* Delete the established association. */
3938         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
3939                         SCTP_U32(SCTP_ERROR_USER_ABORT));
3940
3941         return retval;
3942 }
3943
3944 /*
3945  * sctp_sf_cookie_echoed_prm_abort
3946  *
3947  * Section: 4 Note: 3
3948  * Verification Tag:
3949  * Inputs
3950  * (endpoint, asoc)
3951  *
3952  * The RFC does not explcitly address this issue, but is the route through the
3953  * state table when someone issues an abort while in COOKIE_ECHOED state.
3954  *
3955  * Outputs
3956  * (timers)
3957  */
3958 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
3959         const struct sctp_endpoint *ep,
3960         const struct sctp_association *asoc,
3961         const sctp_subtype_t type,
3962         void *arg,
3963         sctp_cmd_seq_t *commands)
3964 {
3965         /* There is a single T1 timer, so we should be able to use
3966          * common function with the COOKIE-WAIT state.
3967          */
3968         return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
3969 }
3970
3971 /*
3972  * sctp_sf_shutdown_pending_prm_abort
3973  *
3974  * Inputs
3975  * (endpoint, asoc)
3976  *
3977  * The RFC does not explicitly address this issue, but is the route through the
3978  * state table when someone issues an abort while in SHUTDOWN-PENDING state.
3979  *
3980  * Outputs
3981  * (timers)
3982  */
3983 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
3984         const struct sctp_endpoint *ep,
3985         const struct sctp_association *asoc,
3986         const sctp_subtype_t type,
3987         void *arg,
3988         sctp_cmd_seq_t *commands)
3989 {
3990         /* Stop the T5-shutdown guard timer.  */
3991         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3992                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3993
3994         return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
3995 }
3996
3997 /*
3998  * sctp_sf_shutdown_sent_prm_abort
3999  *
4000  * Inputs
4001  * (endpoint, asoc)
4002  *
4003  * The RFC does not explicitly address this issue, but is the route through the
4004  * state table when someone issues an abort while in SHUTDOWN-SENT state.
4005  *
4006  * Outputs
4007  * (timers)
4008  */
4009 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4010         const struct sctp_endpoint *ep,
4011         const struct sctp_association *asoc,
4012         const sctp_subtype_t type,
4013         void *arg,
4014         sctp_cmd_seq_t *commands)
4015 {
4016         /* Stop the T2-shutdown timer.  */
4017         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4018                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4019
4020         /* Stop the T5-shutdown guard timer.  */
4021         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4022                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4023
4024         return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4025 }
4026
4027 /*
4028  * sctp_sf_cookie_echoed_prm_abort
4029  *
4030  * Inputs
4031  * (endpoint, asoc)
4032  *
4033  * The RFC does not explcitly address this issue, but is the route through the
4034  * state table when someone issues an abort while in COOKIE_ECHOED state.
4035  *
4036  * Outputs
4037  * (timers)
4038  */
4039 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4040         const struct sctp_endpoint *ep,
4041         const struct sctp_association *asoc,
4042         const sctp_subtype_t type,
4043         void *arg,
4044         sctp_cmd_seq_t *commands)
4045 {
4046         /* The same T2 timer, so we should be able to use
4047          * common function with the SHUTDOWN-SENT state.
4048          */
4049         return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4050 }
4051
4052 /*
4053  * Process the REQUESTHEARTBEAT primitive
4054  *
4055  * 10.1 ULP-to-SCTP
4056  * J) Request Heartbeat
4057  *
4058  * Format: REQUESTHEARTBEAT(association id, destination transport address)
4059  *
4060  * -> result
4061  *
4062  * Instructs the local endpoint to perform a HeartBeat on the specified
4063  * destination transport address of the given association. The returned
4064  * result should indicate whether the transmission of the HEARTBEAT
4065  * chunk to the destination address is successful.
4066  *
4067  * Mandatory attributes:
4068  *
4069  * o association id - local handle to the SCTP association
4070  *
4071  * o destination transport address - the transport address of the
4072  *   association on which a heartbeat should be issued.
4073  */
4074 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4075                                         const struct sctp_endpoint *ep,
4076                                         const struct sctp_association *asoc,
4077                                         const sctp_subtype_t type,
4078                                         void *arg,
4079                                         sctp_cmd_seq_t *commands)
4080 {
4081         return sctp_sf_heartbeat(ep, asoc, type, (struct sctp_transport *)arg,
4082                                  commands);
4083 }
4084
4085 /*
4086  * ADDIP Section 4.1 ASCONF Chunk Procedures
4087  * When an endpoint has an ASCONF signaled change to be sent to the
4088  * remote endpoint it should do A1 to A9
4089  */
4090 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4091                                         const struct sctp_association *asoc,
4092                                         const sctp_subtype_t type,
4093                                         void *arg,
4094                                         sctp_cmd_seq_t *commands)
4095 {
4096         struct sctp_chunk *chunk = arg;
4097
4098         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4099         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4100                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4101         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4102         return SCTP_DISPOSITION_CONSUME;
4103 }
4104
4105 /*
4106  * Ignore the primitive event
4107  *
4108  * The return value is the disposition of the primitive.
4109  */
4110 sctp_disposition_t sctp_sf_ignore_primitive(
4111         const struct sctp_endpoint *ep,
4112         const struct sctp_association *asoc,
4113         const sctp_subtype_t type,
4114         void *arg,
4115         sctp_cmd_seq_t *commands)
4116 {
4117         SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
4118         return SCTP_DISPOSITION_DISCARD;
4119 }
4120
4121 /***************************************************************************
4122  * These are the state functions for the OTHER events.
4123  ***************************************************************************/
4124
4125 /*
4126  * Start the shutdown negotiation.
4127  *
4128  * From Section 9.2:
4129  * Once all its outstanding data has been acknowledged, the endpoint
4130  * shall send a SHUTDOWN chunk to its peer including in the Cumulative
4131  * TSN Ack field the last sequential TSN it has received from the peer.
4132  * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
4133  * state. If the timer expires, the endpoint must re-send the SHUTDOWN
4134  * with the updated last sequential TSN received from its peer.
4135  *
4136  * The return value is the disposition.
4137  */
4138 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
4139         const struct sctp_endpoint *ep,
4140         const struct sctp_association *asoc,
4141         const sctp_subtype_t type,
4142         void *arg,
4143         sctp_cmd_seq_t *commands)
4144 {
4145         struct sctp_chunk *reply;
4146
4147         /* Once all its outstanding data has been acknowledged, the
4148          * endpoint shall send a SHUTDOWN chunk to its peer including
4149          * in the Cumulative TSN Ack field the last sequential TSN it
4150          * has received from the peer.
4151          */
4152         reply = sctp_make_shutdown(asoc, NULL);
4153         if (!reply)
4154                 goto nomem;
4155
4156         /* Set the transport for the SHUTDOWN chunk and the timeout for the
4157          * T2-shutdown timer.
4158          */
4159         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4160
4161         /* It shall then start the T2-shutdown timer */
4162         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4163                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4164
4165         if (asoc->autoclose)
4166                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4167                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4168
4169         /* and enter the SHUTDOWN-SENT state.  */
4170         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4171                         SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
4172
4173         /* sctp-implguide 2.10 Issues with Heartbeating and failover
4174          *
4175          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
4176          * or SHUTDOWN-ACK.
4177          */
4178         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
4179
4180         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4181
4182         return SCTP_DISPOSITION_CONSUME;
4183
4184 nomem:
4185         return SCTP_DISPOSITION_NOMEM;
4186 }
4187
4188 /*
4189  * Generate a SHUTDOWN ACK now that everything is SACK'd.
4190  *
4191  * From Section 9.2:
4192  *
4193  * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
4194  * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
4195  * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
4196  * endpoint must re-send the SHUTDOWN ACK.
4197  *
4198  * The return value is the disposition.
4199  */
4200 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
4201         const struct sctp_endpoint *ep,
4202         const struct sctp_association *asoc,
4203         const sctp_subtype_t type,
4204         void *arg,
4205         sctp_cmd_seq_t *commands)
4206 {
4207         struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
4208         struct sctp_chunk *reply;
4209
4210         /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
4211          * shall send a SHUTDOWN ACK ...
4212          */
4213         reply = sctp_make_shutdown_ack(asoc, chunk);
4214         if (!reply)
4215                 goto nomem;
4216
4217         /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
4218          * the T2-shutdown timer.
4219          */
4220         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4221
4222         /* and start/restart a T2-shutdown timer of its own, */
4223         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4224                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4225
4226         if (asoc->autoclose)
4227                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4228                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4229
4230         /* Enter the SHUTDOWN-ACK-SENT state.  */
4231         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4232                         SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
4233
4234         /* sctp-implguide 2.10 Issues with Heartbeating and failover
4235          *
4236          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
4237          * or SHUTDOWN-ACK.
4238          */
4239         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
4240
4241         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4242
4243         return SCTP_DISPOSITION_CONSUME;
4244
4245 nomem:
4246         return SCTP_DISPOSITION_NOMEM;
4247 }
4248
4249 /*
4250  * Ignore the event defined as other
4251  *
4252  * The return value is the disposition of the event.
4253  */
4254 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
4255                                         const struct sctp_association *asoc,
4256                                         const sctp_subtype_t type,
4257                                         void *arg,
4258                                         sctp_cmd_seq_t *commands)
4259 {
4260         SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
4261         return SCTP_DISPOSITION_DISCARD;
4262 }
4263
4264 /************************************************************
4265  * These are the state functions for handling timeout events.
4266  ************************************************************/
4267
4268 /*
4269  * RTX Timeout
4270  *
4271  * Section: 6.3.3 Handle T3-rtx Expiration
4272  *
4273  * Whenever the retransmission timer T3-rtx expires for a destination
4274  * address, do the following:
4275  * [See below]
4276  *
4277  * The return value is the disposition of the chunk.
4278  */
4279 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
4280                                         const struct sctp_association *asoc,
4281                                         const sctp_subtype_t type,
4282                                         void *arg,
4283                                         sctp_cmd_seq_t *commands)
4284 {
4285         struct sctp_transport *transport = arg;
4286
4287         if (asoc->overall_error_count >= asoc->max_retrans) {
4288                 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
4289                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4290                                 SCTP_U32(SCTP_ERROR_NO_ERROR));
4291                 SCTP_INC_STATS(SctpAborteds);
4292                 SCTP_DEC_STATS(SctpCurrEstab);
4293                 return SCTP_DISPOSITION_DELETE_TCB;
4294         }
4295
4296         /* E1) For the destination address for which the timer
4297          * expires, adjust its ssthresh with rules defined in Section
4298          * 7.2.3 and set the cwnd <- MTU.
4299          */
4300
4301         /* E2) For the destination address for which the timer
4302          * expires, set RTO <- RTO * 2 ("back off the timer").  The
4303          * maximum value discussed in rule C7 above (RTO.max) may be
4304          * used to provide an upper bound to this doubling operation.
4305          */
4306
4307         /* E3) Determine how many of the earliest (i.e., lowest TSN)
4308          * outstanding DATA chunks for the address for which the
4309          * T3-rtx has expired will fit into a single packet, subject
4310          * to the MTU constraint for the path corresponding to the
4311          * destination transport address to which the retransmission
4312          * is being sent (this may be different from the address for
4313          * which the timer expires [see Section 6.4]).  Call this
4314          * value K. Bundle and retransmit those K DATA chunks in a
4315          * single packet to the destination endpoint.
4316          *
4317          * Note: Any DATA chunks that were sent to the address for
4318          * which the T3-rtx timer expired but did not fit in one MTU
4319          * (rule E3 above), should be marked for retransmission and
4320          * sent as soon as cwnd allows (normally when a SACK arrives).
4321          */
4322
4323         /* NB: Rules E4 and F1 are implicit in R1.  */
4324         sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
4325
4326         /* Do some failure management (Section 8.2). */
4327         sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
4328
4329         return SCTP_DISPOSITION_CONSUME;
4330 }
4331
4332 /*
4333  * Generate delayed SACK on timeout
4334  *
4335  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
4336  *
4337  * The guidelines on delayed acknowledgement algorithm specified in
4338  * Section 4.2 of [RFC2581] SHOULD be followed.  Specifically, an
4339  * acknowledgement SHOULD be generated for at least every second packet
4340  * (not every second DATA chunk) received, and SHOULD be generated
4341  * within 200 ms of the arrival of any unacknowledged DATA chunk.  In
4342  * some situations it may be beneficial for an SCTP transmitter to be
4343  * more conservative than the algorithms detailed in this document
4344  * allow. However, an SCTP transmitter MUST NOT be more aggressive than
4345  * the following algorithms allow.
4346  */
4347 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
4348                                        const struct sctp_association *asoc,
4349                                        const sctp_subtype_t type,
4350                                        void *arg,
4351                                        sctp_cmd_seq_t *commands)
4352 {
4353         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4354         return SCTP_DISPOSITION_CONSUME;
4355 }
4356
4357 /*
4358  * sctp_sf_t1_timer_expire
4359  *
4360  * Section: 4 Note: 2
4361  * Verification Tag:
4362  * Inputs
4363  * (endpoint, asoc)
4364  *
4365  *  RFC 2960 Section 4 Notes
4366  *  2) If the T1-init timer expires, the endpoint MUST retransmit INIT
4367  *     and re-start the T1-init timer without changing state.  This MUST
4368  *     be repeated up to 'Max.Init.Retransmits' times.  After that, the
4369  *     endpoint MUST abort the initialization process and report the
4370  *     error to SCTP user.
4371  *
4372  *   3) If the T1-cookie timer expires, the endpoint MUST retransmit
4373  *     COOKIE ECHO and re-start the T1-cookie timer without changing
4374  *     state.  This MUST be repeated up to 'Max.Init.Retransmits' times.
4375  *     After that, the endpoint MUST abort the initialization process and
4376  *     report the error to SCTP user.
4377  *
4378  * Outputs
4379  * (timers, events)
4380  *
4381  */
4382 sctp_disposition_t sctp_sf_t1_timer_expire(const struct sctp_endpoint *ep,
4383                                            const struct sctp_association *asoc,
4384                                            const sctp_subtype_t type,
4385                                            void *arg,
4386                                            sctp_cmd_seq_t *commands)
4387 {
4388         struct sctp_chunk *repl;
4389         struct sctp_bind_addr *bp;
4390         sctp_event_timeout_t timer = (sctp_event_timeout_t) arg;
4391         int timeout;
4392         int attempts;
4393
4394         timeout = asoc->timeouts[timer];
4395         attempts = asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1;
4396         repl = NULL;
4397
4398         SCTP_DEBUG_PRINTK("Timer T1 expired.\n");
4399
4400         if (attempts < asoc->max_init_attempts) {
4401                 switch (timer) {
4402                 case SCTP_EVENT_TIMEOUT_T1_INIT:
4403                         bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
4404                         repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
4405                         break;
4406
4407                 case SCTP_EVENT_TIMEOUT_T1_COOKIE:
4408                         repl = sctp_make_cookie_echo(asoc, NULL);
4409                         break;
4410
4411                 default:
4412                         BUG();
4413                         break;
4414                 };
4415
4416                 if (!repl)
4417                         goto nomem;
4418
4419                 /* Issue a sideeffect to do the needed accounting. */
4420                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
4421                                 SCTP_TO(timer));
4422                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4423         } else {
4424                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4425                                 SCTP_U32(SCTP_ERROR_NO_ERROR));
4426                 return SCTP_DISPOSITION_DELETE_TCB;
4427         }
4428
4429         return SCTP_DISPOSITION_CONSUME;
4430
4431 nomem:
4432         return SCTP_DISPOSITION_NOMEM;
4433 }
4434
4435 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
4436  * with the updated last sequential TSN received from its peer.
4437  *
4438  * An endpoint should limit the number of retransmissions of the
4439  * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
4440  * If this threshold is exceeded the endpoint should destroy the TCB and
4441  * MUST report the peer endpoint unreachable to the upper layer (and
4442  * thus the association enters the CLOSED state).  The reception of any
4443  * packet from its peer (i.e. as the peer sends all of its queued DATA
4444  * chunks) should clear the endpoint's retransmission count and restart
4445  * the T2-Shutdown timer,  giving its peer ample opportunity to transmit
4446  * all of its queued DATA chunks that have not yet been sent.
4447  */
4448 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
4449                                            const struct sctp_association *asoc,
4450                                            const sctp_subtype_t type,
4451                                            void *arg,
4452                                            sctp_cmd_seq_t *commands)
4453 {
4454         struct sctp_chunk *reply = NULL;
4455
4456         SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
4457         if (asoc->overall_error_count >= asoc->max_retrans) {
4458                 /* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
4459                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4460                                 SCTP_U32(SCTP_ERROR_NO_ERROR));
4461                 SCTP_INC_STATS(SctpAborteds);
4462                 SCTP_DEC_STATS(SctpCurrEstab);
4463                 return SCTP_DISPOSITION_DELETE_TCB;
4464         }
4465
4466         switch (asoc->state) {
4467         case SCTP_STATE_SHUTDOWN_SENT:
4468                 reply = sctp_make_shutdown(asoc, NULL);
4469                 break;
4470
4471         case SCTP_STATE_SHUTDOWN_ACK_SENT:
4472                 reply = sctp_make_shutdown_ack(asoc, NULL);
4473                 break;
4474
4475         default:
4476                 BUG();
4477                 break;
4478         };
4479
4480         if (!reply)
4481                 goto nomem;
4482
4483         /* Do some failure management (Section 8.2). */
4484         sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
4485                         SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
4486
4487         /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
4488          * the T2-shutdown timer.
4489          */
4490         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4491
4492         /* Restart the T2-shutdown timer.  */
4493         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4494                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4495         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4496         return SCTP_DISPOSITION_CONSUME;
4497
4498 nomem:
4499         return SCTP_DISPOSITION_NOMEM;
4500 }
4501
4502 /*
4503  * ADDIP Section 4.1 ASCONF CHunk Procedures
4504  * If the T4 RTO timer expires the endpoint should do B1 to B5
4505  */
4506 sctp_disposition_t sctp_sf_t4_timer_expire(
4507         const struct sctp_endpoint *ep,
4508         const struct sctp_association *asoc,
4509         const sctp_subtype_t type,
4510         void *arg,
4511         sctp_cmd_seq_t *commands)
4512 {
4513         struct sctp_chunk *chunk = asoc->addip_last_asconf;
4514         struct sctp_transport *transport = chunk->transport;
4515
4516         /* ADDIP 4.1 B1) Increment the error counters and perform path failure
4517          * detection on the appropriate destination address as defined in
4518          * RFC2960 [5] section 8.1 and 8.2.
4519          */
4520         sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
4521
4522         /* Reconfig T4 timer and transport. */
4523         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4524
4525         /* ADDIP 4.1 B2) Increment the association error counters and perform
4526          * endpoint failure detection on the association as defined in
4527          * RFC2960 [5] section 8.1 and 8.2.
4528          * association error counter is incremented in SCTP_CMD_STRIKE.
4529          */
4530         if (asoc->overall_error_count >= asoc->max_retrans) {
4531                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4532                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4533                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4534                                 SCTP_U32(SCTP_ERROR_NO_ERROR));
4535                 SCTP_INC_STATS(SctpAborteds);
4536                 SCTP_INC_STATS(SctpCurrEstab);
4537                 return SCTP_DISPOSITION_ABORT;
4538         }
4539
4540         /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
4541          * the ASCONF chunk was sent by doubling the RTO timer value.
4542          * This is done in SCTP_CMD_STRIKE.
4543          */
4544
4545         /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
4546          * choose an alternate destination address (please refer to RFC2960
4547          * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
4548          * chunk, it MUST be the same (including its serial number) as the last 
4549          * ASCONF sent.
4550          */
4551         sctp_chunk_hold(asoc->addip_last_asconf);
4552         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4553                         SCTP_CHUNK(asoc->addip_last_asconf));
4554
4555         /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
4556          * destination is selected, then the RTO used will be that of the new
4557          * destination address.
4558          */
4559         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4560                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4561
4562         return SCTP_DISPOSITION_CONSUME;
4563 }
4564
4565 /* sctpimpguide-05 Section 2.12.2
4566  * The sender of the SHUTDOWN MAY also start an overall guard timer
4567  * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4568  * At the expiration of this timer the sender SHOULD abort the association
4569  * by sending an ABORT chunk.
4570  */
4571 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
4572                                            const struct sctp_association *asoc,
4573                                            const sctp_subtype_t type,
4574                                            void *arg,
4575                                            sctp_cmd_seq_t *commands)
4576 {
4577         struct sctp_chunk *reply = NULL;
4578
4579         SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
4580
4581         reply = sctp_make_abort(asoc, NULL, 0);
4582         if (!reply)
4583                 goto nomem;
4584
4585         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4586         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4587                         SCTP_U32(SCTP_ERROR_NO_ERROR));
4588
4589         return SCTP_DISPOSITION_DELETE_TCB;
4590 nomem:
4591         return SCTP_DISPOSITION_NOMEM;
4592 }
4593
4594 /* Handle expiration of AUTOCLOSE timer.  When the autoclose timer expires,
4595  * the association is automatically closed by starting the shutdown process.
4596  * The work that needs to be done is same as when SHUTDOWN is initiated by
4597  * the user.  So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
4598  */
4599 sctp_disposition_t sctp_sf_autoclose_timer_expire(
4600         const struct sctp_endpoint *ep,
4601         const struct sctp_association *asoc,
4602         const sctp_subtype_t type,
4603         void *arg,
4604         sctp_cmd_seq_t *commands)
4605 {
4606         int disposition;
4607
4608         /* From 9.2 Shutdown of an Association
4609          * Upon receipt of the SHUTDOWN primitive from its upper
4610          * layer, the endpoint enters SHUTDOWN-PENDING state and
4611          * remains there until all outstanding data has been
4612          * acknowledged by its peer. The endpoint accepts no new data
4613          * from its upper layer, but retransmits data to the far end
4614          * if necessary to fill gaps.
4615          */
4616         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4617                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4618
4619         /* sctpimpguide-05 Section 2.12.2
4620          * The sender of the SHUTDOWN MAY also start an overall guard timer
4621          * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4622          */
4623         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4624                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4625         disposition = SCTP_DISPOSITION_CONSUME;
4626         if (sctp_outq_is_empty(&asoc->outqueue)) {
4627                 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4628                                                             arg, commands);
4629         }
4630         return disposition;
4631 }
4632
4633 /*****************************************************************************
4634  * These are sa state functions which could apply to all types of events.
4635  ****************************************************************************/
4636
4637 /*
4638  * This table entry is not implemented.
4639  *
4640  * Inputs
4641  * (endpoint, asoc, chunk)
4642  *
4643  * The return value is the disposition of the chunk.
4644  */
4645 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
4646                                     const struct sctp_association *asoc,
4647                                     const sctp_subtype_t type,
4648                                     void *arg,
4649                                     sctp_cmd_seq_t *commands)
4650 {
4651         return SCTP_DISPOSITION_NOT_IMPL;
4652 }
4653
4654 /*
4655  * This table entry represents a bug.
4656  *
4657  * Inputs
4658  * (endpoint, asoc, chunk)
4659  *
4660  * The return value is the disposition of the chunk.
4661  */
4662 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
4663                                const struct sctp_association *asoc,
4664                                const sctp_subtype_t type,
4665                                void *arg,
4666                                sctp_cmd_seq_t *commands)
4667 {
4668         return SCTP_DISPOSITION_BUG;
4669 }
4670
4671 /*
4672  * This table entry represents the firing of a timer in the wrong state.
4673  * Since timer deletion cannot be guaranteed a timer 'may' end up firing
4674  * when the association is in the wrong state.   This event should
4675  * be ignored, so as to prevent any rearming of the timer.
4676  *
4677  * Inputs
4678  * (endpoint, asoc, chunk)
4679  *
4680  * The return value is the disposition of the chunk.
4681  */
4682 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
4683                                         const struct sctp_association *asoc,
4684                                         const sctp_subtype_t type,
4685                                         void *arg,
4686                                         sctp_cmd_seq_t *commands)
4687 {
4688         SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
4689         return SCTP_DISPOSITION_CONSUME;
4690 }
4691
4692 /********************************************************************
4693  * 2nd Level Abstractions
4694  ********************************************************************/
4695
4696 /* Pull the SACK chunk based on the SACK header. */
4697 struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
4698 {
4699         struct sctp_sackhdr *sack;
4700         unsigned int len;
4701         __u16 num_blocks;
4702         __u16 num_dup_tsns;
4703
4704         /* Protect ourselves from reading too far into
4705          * the skb from a bogus sender.
4706          */
4707         sack = (struct sctp_sackhdr *) chunk->skb->data;
4708
4709         num_blocks = ntohs(sack->num_gap_ack_blocks);
4710         num_dup_tsns = ntohs(sack->num_dup_tsns);
4711         len = sizeof(struct sctp_sackhdr);
4712         len = (num_blocks + num_dup_tsns) * sizeof(__u32);
4713         if (len > chunk->skb->len)
4714                 return NULL;
4715
4716         skb_pull(chunk->skb, len);
4717
4718         return sack;
4719 }
4720
4721 /* Create an ABORT packet to be sent as a response, with the specified
4722  * error causes.
4723  */
4724 struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
4725                                   const struct sctp_association *asoc,
4726                                   struct sctp_chunk *chunk,
4727                                   const void *payload,
4728                                   size_t paylen)
4729 {
4730         struct sctp_packet *packet;
4731         struct sctp_chunk *abort;
4732
4733         packet = sctp_ootb_pkt_new(asoc, chunk);
4734
4735         if (packet) {
4736                 /* Make an ABORT.
4737                  * The T bit will be set if the asoc is NULL.
4738                  */
4739                 abort = sctp_make_abort(asoc, chunk, paylen);
4740                 if (!abort) {
4741                         sctp_ootb_pkt_free(packet);
4742                         return NULL;
4743                 }
4744                 /* Add specified error causes, i.e., payload, to the
4745                  * end of the chunk.
4746                  */
4747                 sctp_addto_chunk(abort, paylen, payload);
4748
4749                 /* Set the skb to the belonging sock for accounting.  */
4750                 abort->skb->sk = ep->base.sk;
4751
4752                 sctp_packet_append_chunk(packet, abort);
4753
4754         }
4755
4756         return packet;
4757 }
4758
4759 /* Allocate a packet for responding in the OOTB conditions.  */
4760 struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
4761                                  const struct sctp_chunk *chunk)
4762 {
4763         struct sctp_packet *packet;
4764         struct sctp_transport *transport;
4765         __u16 sport;
4766         __u16 dport;
4767         __u32 vtag;
4768
4769         /* Get the source and destination port from the inbound packet.  */
4770         sport = ntohs(chunk->sctp_hdr->dest);
4771         dport = ntohs(chunk->sctp_hdr->source);
4772
4773         /* The V-tag is going to be the same as the inbound packet if no
4774          * association exists, otherwise, use the peer's vtag.
4775          */
4776         if (asoc) {
4777                 vtag = asoc->peer.i.init_tag;
4778         } else {
4779                 /* Special case the INIT and stale COOKIE_ECHO as there is no
4780                  * vtag yet.
4781                  */
4782                 switch(chunk->chunk_hdr->type) {
4783                 case SCTP_CID_INIT:
4784                 {
4785                         sctp_init_chunk_t *init;
4786
4787                         init = (sctp_init_chunk_t *)chunk->chunk_hdr;
4788                         vtag = ntohl(init->init_hdr.init_tag);
4789                         break;
4790                 }
4791                 default:        
4792                         vtag = ntohl(chunk->sctp_hdr->vtag);
4793                         break;
4794                 }
4795         }
4796
4797         /* Make a transport for the bucket, Eliza... */
4798         transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
4799         if (!transport)
4800                 goto nomem;
4801
4802         /* Cache a route for the transport with the chunk's destination as
4803          * the source address.
4804          */
4805         sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
4806                              sctp_sk(sctp_get_ctl_sock()));
4807
4808         packet = sctp_packet_init(&transport->packet, transport, sport, dport);
4809         packet = sctp_packet_config(packet, vtag, 0);
4810
4811         return packet;
4812
4813 nomem:
4814         return NULL;
4815 }
4816
4817 /* Free the packet allocated earlier for responding in the OOTB condition.  */
4818 void sctp_ootb_pkt_free(struct sctp_packet *packet)
4819 {
4820         sctp_transport_free(packet->transport);
4821 }
4822
4823 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found  */
4824 void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
4825                                 const struct sctp_association *asoc,
4826                                 const struct sctp_chunk *chunk,
4827                                 sctp_cmd_seq_t *commands,
4828                                 struct sctp_chunk *err_chunk)
4829 {
4830         struct sctp_packet *packet;
4831
4832         if (err_chunk) {
4833                 packet = sctp_ootb_pkt_new(asoc, chunk);
4834                 if (packet) {
4835                         struct sctp_signed_cookie *cookie;
4836
4837                         /* Override the OOTB vtag from the cookie. */
4838                         cookie = chunk->subh.cookie_hdr;
4839                         packet->vtag = cookie->c.peer_vtag;
4840                         
4841                         /* Set the skb to the belonging sock for accounting. */
4842                         err_chunk->skb->sk = ep->base.sk;
4843                         sctp_packet_append_chunk(packet, err_chunk);
4844                         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4845                                         SCTP_PACKET(packet));
4846                         SCTP_INC_STATS(SctpOutCtrlChunks);
4847                 } else
4848                         sctp_chunk_free (err_chunk);
4849         }
4850 }