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