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