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