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