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