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