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