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