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