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