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