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