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