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