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