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