]> Pileus Git - ~andy/linux/blob - net/sctp/sm_statefuns.c
sctp: Failover transmitted list on transport delete
[~andy/linux] / net / sctp / sm_statefuns.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2002 Intel Corp.
6  * Copyright (c) 2002      Nokia Corp.
7  *
8  * This is part of the SCTP Linux Kernel Implementation.
9  *
10  * These are the state functions for the state machine.
11  *
12  * This SCTP implementation is free software;
13  * you can redistribute it and/or modify it under the terms of
14  * the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This SCTP implementation is distributed in the hope that it
19  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20  *                 ************************
21  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22  * See the GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with GNU CC; see the file COPYING.  If not, write to
26  * the Free Software Foundation, 59 Temple Place - Suite 330,
27  * Boston, MA 02111-1307, USA.
28  *
29  * Please send any bug reports or fixes you make to the
30  * email address(es):
31  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
32  *
33  * Or submit a bug report through the following website:
34  *    http://www.sf.net/projects/lksctp
35  *
36  * Written or modified by:
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Karl Knutson          <karl@athena.chicago.il.us>
39  *    Mathew Kotowsky       <kotowsky@sctp.org>
40  *    Sridhar Samudrala     <samudrala@us.ibm.com>
41  *    Jon Grimm             <jgrimm@us.ibm.com>
42  *    Hui Huang             <hui.huang@nokia.com>
43  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
44  *    Daisy Chang           <daisyc@us.ibm.com>
45  *    Ardelle Fan           <ardelle.fan@intel.com>
46  *    Ryan Layer            <rmlayer@us.ibm.com>
47  *    Kevin Gao             <kevin.gao@intel.com>
48  *
49  * Any bugs reported given to us we will try to fix... any fixes shared will
50  * be incorporated into the next SCTP release.
51  */
52
53 #include <linux/types.h>
54 #include <linux/kernel.h>
55 #include <linux/ip.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <net/sock.h>
60 #include <net/inet_ecn.h>
61 #include <linux/skbuff.h>
62 #include <net/sctp/sctp.h>
63 #include <net/sctp/sm.h>
64 #include <net/sctp/structs.h>
65
66 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
67                                   const struct sctp_association *asoc,
68                                   struct sctp_chunk *chunk,
69                                   const void *payload,
70                                   size_t paylen);
71 static int sctp_eat_data(const struct sctp_association *asoc,
72                          struct sctp_chunk *chunk,
73                          sctp_cmd_seq_t *commands);
74 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75                                              const struct sctp_chunk *chunk);
76 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
77                                        const struct sctp_association *asoc,
78                                        const struct sctp_chunk *chunk,
79                                        sctp_cmd_seq_t *commands,
80                                        struct sctp_chunk *err_chunk);
81 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
82                                                  const struct sctp_association *asoc,
83                                                  const sctp_subtype_t type,
84                                                  void *arg,
85                                                  sctp_cmd_seq_t *commands);
86 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
87                                              const struct sctp_association *asoc,
88                                              const sctp_subtype_t type,
89                                              void *arg,
90                                              sctp_cmd_seq_t *commands);
91 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
92                                         const struct sctp_association *asoc,
93                                         const sctp_subtype_t type,
94                                         void *arg,
95                                         sctp_cmd_seq_t *commands);
96 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
97
98 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
99                                            __be16 error, int sk_err,
100                                            const struct sctp_association *asoc,
101                                            struct sctp_transport *transport);
102
103 static sctp_disposition_t sctp_sf_abort_violation(
104                                      const struct sctp_endpoint *ep,
105                                      const struct sctp_association *asoc,
106                                      void *arg,
107                                      sctp_cmd_seq_t *commands,
108                                      const __u8 *payload,
109                                      const size_t paylen);
110
111 static sctp_disposition_t sctp_sf_violation_chunklen(
112                                      const struct sctp_endpoint *ep,
113                                      const struct sctp_association *asoc,
114                                      const sctp_subtype_t type,
115                                      void *arg,
116                                      sctp_cmd_seq_t *commands);
117
118 static sctp_disposition_t sctp_sf_violation_paramlen(
119                                      const struct sctp_endpoint *ep,
120                                      const struct sctp_association *asoc,
121                                      const sctp_subtype_t type,
122                                      void *arg, void *ext,
123                                      sctp_cmd_seq_t *commands);
124
125 static sctp_disposition_t sctp_sf_violation_ctsn(
126                                      const struct sctp_endpoint *ep,
127                                      const struct sctp_association *asoc,
128                                      const sctp_subtype_t type,
129                                      void *arg,
130                                      sctp_cmd_seq_t *commands);
131
132 static sctp_disposition_t sctp_sf_violation_chunk(
133                                      const struct sctp_endpoint *ep,
134                                      const struct sctp_association *asoc,
135                                      const sctp_subtype_t type,
136                                      void *arg,
137                                      sctp_cmd_seq_t *commands);
138
139 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
140                                     const struct sctp_association *asoc,
141                                     const sctp_subtype_t type,
142                                     struct sctp_chunk *chunk);
143
144 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
145                                         const struct sctp_association *asoc,
146                                         const sctp_subtype_t type,
147                                         void *arg,
148                                         sctp_cmd_seq_t *commands);
149
150 /* Small helper function that checks if the chunk length
151  * is of the appropriate length.  The 'required_length' argument
152  * is set to be the size of a specific chunk we are testing.
153  * Return Values:  1 = Valid length
154  *                 0 = Invalid length
155  *
156  */
157 static inline int
158 sctp_chunk_length_valid(struct sctp_chunk *chunk,
159                            __u16 required_length)
160 {
161         __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
162
163         if (unlikely(chunk_length < required_length))
164                 return 0;
165
166         return 1;
167 }
168
169 /**********************************************************
170  * These are the state functions for handling chunk events.
171  **********************************************************/
172
173 /*
174  * Process the final SHUTDOWN COMPLETE.
175  *
176  * Section: 4 (C) (diagram), 9.2
177  * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
178  * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
179  * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
180  * should stop the T2-shutdown timer and remove all knowledge of the
181  * association (and thus the association enters the CLOSED state).
182  *
183  * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
184  * C) Rules for packet carrying SHUTDOWN COMPLETE:
185  * ...
186  * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
187  *   if the Verification Tag field of the packet matches its own tag and
188  *   the T bit is not set
189  *   OR
190  *   it is set to its peer's tag and the T bit is set in the Chunk
191  *   Flags.
192  *   Otherwise, the receiver MUST silently discard the packet
193  *   and take no further action.  An endpoint MUST ignore the
194  *   SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
195  *
196  * Inputs
197  * (endpoint, asoc, chunk)
198  *
199  * Outputs
200  * (asoc, reply_msg, msg_up, timers, counters)
201  *
202  * The return value is the disposition of the chunk.
203  */
204 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
205                                   const struct sctp_association *asoc,
206                                   const sctp_subtype_t type,
207                                   void *arg,
208                                   sctp_cmd_seq_t *commands)
209 {
210         struct sctp_chunk *chunk = arg;
211         struct sctp_ulpevent *ev;
212
213         if (!sctp_vtag_verify_either(chunk, asoc))
214                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
215
216         /* RFC 2960 6.10 Bundling
217          *
218          * An endpoint MUST NOT bundle INIT, INIT ACK or
219          * SHUTDOWN COMPLETE with any other chunks.
220          */
221         if (!chunk->singleton)
222                 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
223
224         /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
225         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
226                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
227                                                   commands);
228
229         /* RFC 2960 10.2 SCTP-to-ULP
230          *
231          * H) SHUTDOWN COMPLETE notification
232          *
233          * When SCTP completes the shutdown procedures (section 9.2) this
234          * notification is passed to the upper layer.
235          */
236         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
237                                              0, 0, 0, NULL, GFP_ATOMIC);
238         if (ev)
239                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
240                                 SCTP_ULPEVENT(ev));
241
242         /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
243          * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
244          * not the chunk should be discarded. If the endpoint is in
245          * the SHUTDOWN-ACK-SENT state the endpoint should stop the
246          * T2-shutdown timer and remove all knowledge of the
247          * association (and thus the association enters the CLOSED
248          * state).
249          */
250         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
252
253         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
255
256         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257                         SCTP_STATE(SCTP_STATE_CLOSED));
258
259         SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
261
262         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
263
264         return SCTP_DISPOSITION_DELETE_TCB;
265 }
266
267 /*
268  * Respond to a normal INIT chunk.
269  * We are the side that is being asked for an association.
270  *
271  * Section: 5.1 Normal Establishment of an Association, B
272  * B) "Z" shall respond immediately with an INIT ACK chunk.  The
273  *    destination IP address of the INIT ACK MUST be set to the source
274  *    IP address of the INIT to which this INIT ACK is responding.  In
275  *    the response, besides filling in other parameters, "Z" must set the
276  *    Verification Tag field to Tag_A, and also provide its own
277  *    Verification Tag (Tag_Z) in the Initiate Tag field.
278  *
279  * Verification Tag: Must be 0.
280  *
281  * Inputs
282  * (endpoint, asoc, chunk)
283  *
284  * Outputs
285  * (asoc, reply_msg, msg_up, timers, counters)
286  *
287  * The return value is the disposition of the chunk.
288  */
289 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
290                                         const struct sctp_association *asoc,
291                                         const sctp_subtype_t type,
292                                         void *arg,
293                                         sctp_cmd_seq_t *commands)
294 {
295         struct sctp_chunk *chunk = arg;
296         struct sctp_chunk *repl;
297         struct sctp_association *new_asoc;
298         struct sctp_chunk *err_chunk;
299         struct sctp_packet *packet;
300         sctp_unrecognized_param_t *unk_param;
301         int len;
302
303         /* 6.10 Bundling
304          * An endpoint MUST NOT bundle INIT, INIT ACK or
305          * SHUTDOWN COMPLETE with any other chunks.
306          *
307          * IG Section 2.11.2
308          * Furthermore, we require that the receiver of an INIT chunk MUST
309          * enforce these rules by silently discarding an arriving packet
310          * with an INIT chunk that is bundled with other chunks.
311          */
312         if (!chunk->singleton)
313                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
314
315         /* If the packet is an OOTB packet which is temporarily on the
316          * control endpoint, respond with an ABORT.
317          */
318         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319                 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         default:
2895                 BUG();
2896         }
2897
2898         if (asoc->autoclose) {
2899                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2900                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2901         }
2902
2903         /* If this is the last chunk in a packet, we need to count it
2904          * toward sack generation.  Note that we need to SACK every
2905          * OTHER packet containing data chunks, EVEN IF WE DISCARD
2906          * THEM.  We elect to NOT generate SACK's if the chunk fails
2907          * the verification tag test.
2908          *
2909          * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2910          *
2911          * The SCTP endpoint MUST always acknowledge the reception of
2912          * each valid DATA chunk.
2913          *
2914          * The guidelines on delayed acknowledgement algorithm
2915          * specified in  Section 4.2 of [RFC2581] SHOULD be followed.
2916          * Specifically, an acknowledgement SHOULD be generated for at
2917          * least every second packet (not every second DATA chunk)
2918          * received, and SHOULD be generated within 200 ms of the
2919          * arrival of any unacknowledged DATA chunk.  In some
2920          * situations it may be beneficial for an SCTP transmitter to
2921          * be more conservative than the algorithms detailed in this
2922          * document allow. However, an SCTP transmitter MUST NOT be
2923          * more aggressive than the following algorithms allow.
2924          */
2925         if (chunk->end_of_packet)
2926                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2927
2928         return SCTP_DISPOSITION_CONSUME;
2929
2930 discard_force:
2931         /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2932          *
2933          * When a packet arrives with duplicate DATA chunk(s) and with
2934          * no new DATA chunk(s), the endpoint MUST immediately send a
2935          * SACK with no delay.  If a packet arrives with duplicate
2936          * DATA chunk(s) bundled with new DATA chunks, the endpoint
2937          * MAY immediately send a SACK.  Normally receipt of duplicate
2938          * DATA chunks will occur when the original SACK chunk was lost
2939          * and the peer's RTO has expired.  The duplicate TSN number(s)
2940          * SHOULD be reported in the SACK as duplicate.
2941          */
2942         /* In our case, we split the MAY SACK advice up whether or not
2943          * the last chunk is a duplicate.'
2944          */
2945         if (chunk->end_of_packet)
2946                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2947         return SCTP_DISPOSITION_DISCARD;
2948
2949 discard_noforce:
2950         if (chunk->end_of_packet)
2951                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2952
2953         return SCTP_DISPOSITION_DISCARD;
2954 consume:
2955         return SCTP_DISPOSITION_CONSUME;
2956
2957 }
2958
2959 /*
2960  * sctp_sf_eat_data_fast_4_4
2961  *
2962  * Section: 4 (4)
2963  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2964  *    DATA chunks without delay.
2965  *
2966  * Verification Tag:  8.5 Verification Tag [Normal verification]
2967  * Inputs
2968  * (endpoint, asoc, chunk)
2969  *
2970  * Outputs
2971  * (asoc, reply_msg, msg_up, timers, counters)
2972  *
2973  * The return value is the disposition of the chunk.
2974  */
2975 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2976                                      const struct sctp_association *asoc,
2977                                      const sctp_subtype_t type,
2978                                      void *arg,
2979                                      sctp_cmd_seq_t *commands)
2980 {
2981         struct sctp_chunk *chunk = arg;
2982         int error;
2983
2984         if (!sctp_vtag_verify(chunk, asoc)) {
2985                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2986                                 SCTP_NULL());
2987                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2988         }
2989
2990         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2991                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2992                                                   commands);
2993
2994         error = sctp_eat_data(asoc, chunk, commands );
2995         switch (error) {
2996         case SCTP_IERROR_NO_ERROR:
2997         case SCTP_IERROR_HIGH_TSN:
2998         case SCTP_IERROR_DUP_TSN:
2999         case SCTP_IERROR_IGNORE_TSN:
3000         case SCTP_IERROR_BAD_STREAM:
3001                 break;
3002         case SCTP_IERROR_NO_DATA:
3003                 goto consume;
3004         default:
3005                 BUG();
3006         }
3007
3008         /* Go a head and force a SACK, since we are shutting down. */
3009
3010         /* Implementor's Guide.
3011          *
3012          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3013          * respond to each received packet containing one or more DATA chunk(s)
3014          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3015          */
3016         if (chunk->end_of_packet) {
3017                 /* We must delay the chunk creation since the cumulative
3018                  * TSN has not been updated yet.
3019                  */
3020                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3021                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3022                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3023                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3024         }
3025
3026 consume:
3027         return SCTP_DISPOSITION_CONSUME;
3028 }
3029
3030 /*
3031  * Section: 6.2  Processing a Received SACK
3032  * D) Any time a SACK arrives, the endpoint performs the following:
3033  *
3034  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3035  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
3036  *     increasing, a SACK whose Cumulative TSN Ack is less than the
3037  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
3038  *
3039  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
3040  *     of bytes still outstanding after processing the Cumulative TSN Ack
3041  *     and the Gap Ack Blocks.
3042  *
3043  *     iii) If the SACK is missing a TSN that was previously
3044  *     acknowledged via a Gap Ack Block (e.g., the data receiver
3045  *     reneged on the data), then mark the corresponding DATA chunk
3046  *     as available for retransmit:  Mark it as missing for fast
3047  *     retransmit as described in Section 7.2.4 and if no retransmit
3048  *     timer is running for the destination address to which the DATA
3049  *     chunk was originally transmitted, then T3-rtx is started for
3050  *     that destination address.
3051  *
3052  * Verification Tag:  8.5 Verification Tag [Normal verification]
3053  *
3054  * Inputs
3055  * (endpoint, asoc, chunk)
3056  *
3057  * Outputs
3058  * (asoc, reply_msg, msg_up, timers, counters)
3059  *
3060  * The return value is the disposition of the chunk.
3061  */
3062 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3063                                         const struct sctp_association *asoc,
3064                                         const sctp_subtype_t type,
3065                                         void *arg,
3066                                         sctp_cmd_seq_t *commands)
3067 {
3068         struct sctp_chunk *chunk = arg;
3069         sctp_sackhdr_t *sackh;
3070         __u32 ctsn;
3071
3072         if (!sctp_vtag_verify(chunk, asoc))
3073                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3074
3075         /* Make sure that the SACK chunk has a valid length. */
3076         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3077                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3078                                                   commands);
3079
3080         /* Pull the SACK chunk from the data buffer */
3081         sackh = sctp_sm_pull_sack(chunk);
3082         /* Was this a bogus SACK? */
3083         if (!sackh)
3084                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3085         chunk->subh.sack_hdr = sackh;
3086         ctsn = ntohl(sackh->cum_tsn_ack);
3087
3088         /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3089          *     Ack Point, then drop the SACK.  Since Cumulative TSN
3090          *     Ack is monotonically increasing, a SACK whose
3091          *     Cumulative TSN Ack is less than the Cumulative TSN Ack
3092          *     Point indicates an out-of-order SACK.
3093          */
3094         if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3095                 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3096                 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3097                 return SCTP_DISPOSITION_DISCARD;
3098         }
3099
3100         /* If Cumulative TSN Ack beyond the max tsn currently
3101          * send, terminating the association and respond to the
3102          * sender with an ABORT.
3103          */
3104         if (!TSN_lt(ctsn, asoc->next_tsn))
3105                 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3106
3107         /* Return this SACK for further processing.  */
3108         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3109
3110         /* Note: We do the rest of the work on the PROCESS_SACK
3111          * sideeffect.
3112          */
3113         return SCTP_DISPOSITION_CONSUME;
3114 }
3115
3116 /*
3117  * Generate an ABORT in response to a packet.
3118  *
3119  * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3120  *
3121  * 8) The receiver should respond to the sender of the OOTB packet with
3122  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3123  *    MUST fill in the Verification Tag field of the outbound packet
3124  *    with the value found in the Verification Tag field of the OOTB
3125  *    packet and set the T-bit in the Chunk Flags to indicate that the
3126  *    Verification Tag is reflected.  After sending this ABORT, the
3127  *    receiver of the OOTB packet shall discard the OOTB packet and take
3128  *    no further action.
3129  *
3130  * Verification Tag:
3131  *
3132  * The return value is the disposition of the chunk.
3133 */
3134 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
3135                                         const struct sctp_association *asoc,
3136                                         const sctp_subtype_t type,
3137                                         void *arg,
3138                                         sctp_cmd_seq_t *commands)
3139 {
3140         struct sctp_packet *packet = NULL;
3141         struct sctp_chunk *chunk = arg;
3142         struct sctp_chunk *abort;
3143
3144         packet = sctp_ootb_pkt_new(asoc, chunk);
3145
3146         if (packet) {
3147                 /* Make an ABORT. The T bit will be set if the asoc
3148                  * is NULL.
3149                  */
3150                 abort = sctp_make_abort(asoc, chunk, 0);
3151                 if (!abort) {
3152                         sctp_ootb_pkt_free(packet);
3153                         return SCTP_DISPOSITION_NOMEM;
3154                 }
3155
3156                 /* Reflect vtag if T-Bit is set */
3157                 if (sctp_test_T_bit(abort))
3158                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3159
3160                 /* Set the skb to the belonging sock for accounting.  */
3161                 abort->skb->sk = ep->base.sk;
3162
3163                 sctp_packet_append_chunk(packet, abort);
3164
3165                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3166                                 SCTP_PACKET(packet));
3167
3168                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3169
3170                 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3171                 return SCTP_DISPOSITION_CONSUME;
3172         }
3173
3174         return SCTP_DISPOSITION_NOMEM;
3175 }
3176
3177 /*
3178  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
3179  * event as ULP notification for each cause included in the chunk.
3180  *
3181  * API 5.3.1.3 - SCTP_REMOTE_ERROR
3182  *
3183  * The return value is the disposition of the chunk.
3184 */
3185 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3186                                         const struct sctp_association *asoc,
3187                                         const sctp_subtype_t type,
3188                                         void *arg,
3189                                         sctp_cmd_seq_t *commands)
3190 {
3191         struct sctp_chunk *chunk = arg;
3192
3193         if (!sctp_vtag_verify(chunk, asoc))
3194                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3195
3196         /* Make sure that the ERROR chunk has a valid length. */
3197         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3198                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3199                                                   commands);
3200
3201         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3202                         SCTP_CHUNK(chunk));
3203
3204         return SCTP_DISPOSITION_CONSUME;
3205 }
3206
3207 /*
3208  * Process an inbound SHUTDOWN ACK.
3209  *
3210  * From Section 9.2:
3211  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3212  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3213  * peer, and remove all record of the association.
3214  *
3215  * The return value is the disposition.
3216  */
3217 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3218                                         const struct sctp_association *asoc,
3219                                         const sctp_subtype_t type,
3220                                         void *arg,
3221                                         sctp_cmd_seq_t *commands)
3222 {
3223         struct sctp_chunk *chunk = arg;
3224         struct sctp_chunk *reply;
3225         struct sctp_ulpevent *ev;
3226
3227         if (!sctp_vtag_verify(chunk, asoc))
3228                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3229
3230         /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3231         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3232                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3233                                                   commands);
3234         /* 10.2 H) SHUTDOWN COMPLETE notification
3235          *
3236          * When SCTP completes the shutdown procedures (section 9.2) this
3237          * notification is passed to the upper layer.
3238          */
3239         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3240                                              0, 0, 0, NULL, GFP_ATOMIC);
3241         if (!ev)
3242                 goto nomem;
3243
3244         /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3245         reply = sctp_make_shutdown_complete(asoc, chunk);
3246         if (!reply)
3247                 goto nomem_chunk;
3248
3249         /* Do all the commands now (after allocation), so that we
3250          * have consistent state if memory allocation failes
3251          */
3252         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3253
3254         /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3255          * stop the T2-shutdown timer,
3256          */
3257         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3258                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3259
3260         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3261                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3262
3263         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3264                         SCTP_STATE(SCTP_STATE_CLOSED));
3265         SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3266         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3267         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3268
3269         /* ...and remove all record of the association. */
3270         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3271         return SCTP_DISPOSITION_DELETE_TCB;
3272
3273 nomem_chunk:
3274         sctp_ulpevent_free(ev);
3275 nomem:
3276         return SCTP_DISPOSITION_NOMEM;
3277 }
3278
3279 /*
3280  * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3281  *
3282  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3283  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3284  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3285  *    packet must fill in the Verification Tag field of the outbound
3286  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3287  *    set the T-bit in the Chunk Flags to indicate that the Verification
3288  *    Tag is reflected.
3289  *
3290  * 8) The receiver should respond to the sender of the OOTB packet with
3291  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3292  *    MUST fill in the Verification Tag field of the outbound packet
3293  *    with the value found in the Verification Tag field of the OOTB
3294  *    packet and set the T-bit in the Chunk Flags to indicate that the
3295  *    Verification Tag is reflected.  After sending this ABORT, the
3296  *    receiver of the OOTB packet shall discard the OOTB packet and take
3297  *    no further action.
3298  */
3299 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3300                                 const struct sctp_association *asoc,
3301                                 const sctp_subtype_t type,
3302                                 void *arg,
3303                                 sctp_cmd_seq_t *commands)
3304 {
3305         struct sctp_chunk *chunk = arg;
3306         struct sk_buff *skb = chunk->skb;
3307         sctp_chunkhdr_t *ch;
3308         __u8 *ch_end;
3309         int ootb_shut_ack = 0;
3310
3311         SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3312
3313         ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3314         do {
3315                 /* Report violation if the chunk is less then minimal */
3316                 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3317                         return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3318                                                   commands);
3319
3320                 /* Now that we know we at least have a chunk header,
3321                  * do things that are type appropriate.
3322                  */
3323                 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3324                         ootb_shut_ack = 1;
3325
3326                 /* RFC 2960, Section 3.3.7
3327                  *   Moreover, under any circumstances, an endpoint that
3328                  *   receives an ABORT  MUST NOT respond to that ABORT by
3329                  *   sending an ABORT of its own.
3330                  */
3331                 if (SCTP_CID_ABORT == ch->type)
3332                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3333
3334                 /* Report violation if chunk len overflows */
3335                 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3336                 if (ch_end > skb_tail_pointer(skb))
3337                         return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3338                                                   commands);
3339
3340                 ch = (sctp_chunkhdr_t *) ch_end;
3341         } while (ch_end < skb_tail_pointer(skb));
3342
3343         if (ootb_shut_ack)
3344                 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3345         else
3346                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3347 }
3348
3349 /*
3350  * Handle an "Out of the blue" SHUTDOWN ACK.
3351  *
3352  * Section: 8.4 5, sctpimpguide 2.41.
3353  *
3354  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3355  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3356  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3357  *    packet must fill in the Verification Tag field of the outbound
3358  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3359  *    set the T-bit in the Chunk Flags to indicate that the Verification
3360  *    Tag is reflected.
3361  *
3362  * Inputs
3363  * (endpoint, asoc, type, arg, commands)
3364  *
3365  * Outputs
3366  * (sctp_disposition_t)
3367  *
3368  * The return value is the disposition of the chunk.
3369  */
3370 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3371                                              const struct sctp_association *asoc,
3372                                              const sctp_subtype_t type,
3373                                              void *arg,
3374                                              sctp_cmd_seq_t *commands)
3375 {
3376         struct sctp_packet *packet = NULL;
3377         struct sctp_chunk *chunk = arg;
3378         struct sctp_chunk *shut;
3379
3380         packet = sctp_ootb_pkt_new(asoc, chunk);
3381
3382         if (packet) {
3383                 /* Make an SHUTDOWN_COMPLETE.
3384                  * The T bit will be set if the asoc is NULL.
3385                  */
3386                 shut = sctp_make_shutdown_complete(asoc, chunk);
3387                 if (!shut) {
3388                         sctp_ootb_pkt_free(packet);
3389                         return SCTP_DISPOSITION_NOMEM;
3390                 }
3391
3392                 /* Reflect vtag if T-Bit is set */
3393                 if (sctp_test_T_bit(shut))
3394                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3395
3396                 /* Set the skb to the belonging sock for accounting.  */
3397                 shut->skb->sk = ep->base.sk;
3398
3399                 sctp_packet_append_chunk(packet, shut);
3400
3401                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3402                                 SCTP_PACKET(packet));
3403
3404                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3405
3406                 /* If the chunk length is invalid, we don't want to process
3407                  * the reset of the packet.
3408                  */
3409                 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3410                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3411
3412                 /* We need to discard the rest of the packet to prevent
3413                  * potential bomming attacks from additional bundled chunks.
3414                  * This is documented in SCTP Threats ID.
3415                  */
3416                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3417         }
3418
3419         return SCTP_DISPOSITION_NOMEM;
3420 }
3421
3422 /*
3423  * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3424  *
3425  * Verification Tag:  8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3426  *   If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3427  *   procedures in section 8.4 SHOULD be followed, in other words it
3428  *   should be treated as an Out Of The Blue packet.
3429  *   [This means that we do NOT check the Verification Tag on these
3430  *   chunks. --piggy ]
3431  *
3432  */
3433 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3434                                       const struct sctp_association *asoc,
3435                                       const sctp_subtype_t type,
3436                                       void *arg,
3437                                       sctp_cmd_seq_t *commands)
3438 {
3439         struct sctp_chunk *chunk = arg;
3440
3441         /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3442         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3443                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3444                                                   commands);
3445
3446         /* Although we do have an association in this case, it corresponds
3447          * to a restarted association. So the packet is treated as an OOTB
3448          * packet and the state function that handles OOTB SHUTDOWN_ACK is
3449          * called with a NULL association.
3450          */
3451         SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3452
3453         return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3454 }
3455
3456 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.  */
3457 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3458                                      const struct sctp_association *asoc,
3459                                      const sctp_subtype_t type, void *arg,
3460                                      sctp_cmd_seq_t *commands)
3461 {
3462         struct sctp_chunk       *chunk = arg;
3463         struct sctp_chunk       *asconf_ack = NULL;
3464         struct sctp_paramhdr    *err_param = NULL;
3465         sctp_addiphdr_t         *hdr;
3466         union sctp_addr_param   *addr_param;
3467         __u32                   serial;
3468         int                     length;
3469
3470         if (!sctp_vtag_verify(chunk, asoc)) {
3471                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3472                                 SCTP_NULL());
3473                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3474         }
3475
3476         /* ADD-IP: Section 4.1.1
3477          * This chunk MUST be sent in an authenticated way by using
3478          * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3479          * is received unauthenticated it MUST be silently discarded as
3480          * described in [I-D.ietf-tsvwg-sctp-auth].
3481          */
3482         if (!sctp_addip_noauth && !chunk->auth)
3483                 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3484
3485         /* Make sure that the ASCONF ADDIP chunk has a valid length.  */
3486         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3487                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3488                                                   commands);
3489
3490         hdr = (sctp_addiphdr_t *)chunk->skb->data;
3491         serial = ntohl(hdr->serial);
3492
3493         addr_param = (union sctp_addr_param *)hdr->params;
3494         length = ntohs(addr_param->p.length);
3495         if (length < sizeof(sctp_paramhdr_t))
3496                 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3497                            (void *)addr_param, commands);
3498
3499         /* Verify the ASCONF chunk before processing it. */
3500         if (!sctp_verify_asconf(asoc,
3501                             (sctp_paramhdr_t *)((void *)addr_param + length),
3502                             (void *)chunk->chunk_end,
3503                             &err_param))
3504                 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3505                                                   (void *)err_param, commands);
3506
3507         /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3508          * the endpoint stored in a new association variable
3509          * 'Peer-Serial-Number'.
3510          */
3511         if (serial == asoc->peer.addip_serial + 1) {
3512                 /* If this is the first instance of ASCONF in the packet,
3513                  * we can clean our old ASCONF-ACKs.
3514                  */
3515                 if (!chunk->has_asconf)
3516                         sctp_assoc_clean_asconf_ack_cache(asoc);
3517
3518                 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3519                  * expected, process the ASCONF as described below and after
3520                  * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3521                  * the response packet and cache a copy of it (in the event it
3522                  * later needs to be retransmitted).
3523                  *
3524                  * Essentially, do V1-V5.
3525                  */
3526                 asconf_ack = sctp_process_asconf((struct sctp_association *)
3527                                                  asoc, chunk);
3528                 if (!asconf_ack)
3529                         return SCTP_DISPOSITION_NOMEM;
3530         } else if (serial < asoc->peer.addip_serial + 1) {
3531                 /* ADDIP 5.2 E2)
3532                  * If the value found in the Sequence Number is less than the
3533                  * ('Peer- Sequence-Number' + 1), simply skip to the next
3534                  * ASCONF, and include in the outbound response packet
3535                  * any previously cached ASCONF-ACK response that was
3536                  * sent and saved that matches the Sequence Number of the
3537                  * ASCONF.  Note: It is possible that no cached ASCONF-ACK
3538                  * Chunk exists.  This will occur when an older ASCONF
3539                  * arrives out of order.  In such a case, the receiver
3540                  * should skip the ASCONF Chunk and not include ASCONF-ACK
3541                  * Chunk for that chunk.
3542                  */
3543                 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3544                 if (!asconf_ack)
3545                         return SCTP_DISPOSITION_DISCARD;
3546
3547                 /* Reset the transport so that we select the correct one
3548                  * this time around.  This is to make sure that we don't
3549                  * accidentally use a stale transport that's been removed.
3550                  */
3551                 asconf_ack->transport = NULL;
3552         } else {
3553                 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3554                  * it must be either a stale packet or from an attacker.
3555                  */
3556                 return SCTP_DISPOSITION_DISCARD;
3557         }
3558
3559         /* ADDIP 5.2 E6)  The destination address of the SCTP packet
3560          * containing the ASCONF-ACK Chunks MUST be the source address of
3561          * the SCTP packet that held the ASCONF Chunks.
3562          *
3563          * To do this properly, we'll set the destination address of the chunk
3564          * and at the transmit time, will try look up the transport to use.
3565          * Since ASCONFs may be bundled, the correct transport may not be
3566          * created untill we process the entire packet, thus this workaround.
3567          */
3568         asconf_ack->dest = chunk->source;
3569         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3570
3571         return SCTP_DISPOSITION_CONSUME;
3572 }
3573
3574 /*
3575  * ADDIP Section 4.3 General rules for address manipulation
3576  * When building TLV parameters for the ASCONF Chunk that will add or
3577  * delete IP addresses the D0 to D13 rules should be applied:
3578  */
3579 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3580                                          const struct sctp_association *asoc,
3581                                          const sctp_subtype_t type, void *arg,
3582                                          sctp_cmd_seq_t *commands)
3583 {
3584         struct sctp_chunk       *asconf_ack = arg;
3585         struct sctp_chunk       *last_asconf = asoc->addip_last_asconf;
3586         struct sctp_chunk       *abort;
3587         struct sctp_paramhdr    *err_param = NULL;
3588         sctp_addiphdr_t         *addip_hdr;
3589         __u32                   sent_serial, rcvd_serial;
3590
3591         if (!sctp_vtag_verify(asconf_ack, asoc)) {
3592                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3593                                 SCTP_NULL());
3594                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3595         }
3596
3597         /* ADD-IP, Section 4.1.2:
3598          * This chunk MUST be sent in an authenticated way by using
3599          * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3600          * is received unauthenticated it MUST be silently discarded as
3601          * described in [I-D.ietf-tsvwg-sctp-auth].
3602          */
3603         if (!sctp_addip_noauth && !asconf_ack->auth)
3604                 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3605
3606         /* Make sure that the ADDIP chunk has a valid length.  */
3607         if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3608                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3609                                                   commands);
3610
3611         addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3612         rcvd_serial = ntohl(addip_hdr->serial);
3613
3614         /* Verify the ASCONF-ACK chunk before processing it. */
3615         if (!sctp_verify_asconf(asoc,
3616             (sctp_paramhdr_t *)addip_hdr->params,
3617             (void *)asconf_ack->chunk_end,
3618             &err_param))
3619                 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3620                            (void *)err_param, commands);
3621
3622         if (last_asconf) {
3623                 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3624                 sent_serial = ntohl(addip_hdr->serial);
3625         } else {
3626                 sent_serial = asoc->addip_serial - 1;
3627         }
3628
3629         /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3630          * equal to the next serial number to be used but no ASCONF chunk is
3631          * outstanding the endpoint MUST ABORT the association. Note that a
3632          * sequence number is greater than if it is no more than 2^^31-1
3633          * larger than the current sequence number (using serial arithmetic).
3634          */
3635         if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3636             !(asoc->addip_last_asconf)) {
3637                 abort = sctp_make_abort(asoc, asconf_ack,
3638                                         sizeof(sctp_errhdr_t));
3639                 if (abort) {
3640                         sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3641                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3642                                         SCTP_CHUNK(abort));
3643                 }
3644                 /* We are going to ABORT, so we might as well stop
3645                  * processing the rest of the chunks in the packet.
3646                  */
3647                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3648                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3649                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3650                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3651                                 SCTP_ERROR(ECONNABORTED));
3652                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3653                                 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3654                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3655                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3656                 return SCTP_DISPOSITION_ABORT;
3657         }
3658
3659         if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3660                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3661                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3662
3663                 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3664                                              asconf_ack))
3665                         return SCTP_DISPOSITION_CONSUME;
3666
3667                 abort = sctp_make_abort(asoc, asconf_ack,
3668                                         sizeof(sctp_errhdr_t));
3669                 if (abort) {
3670                         sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3671                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3672                                         SCTP_CHUNK(abort));
3673                 }
3674                 /* We are going to ABORT, so we might as well stop
3675                  * processing the rest of the chunks in the packet.
3676                  */
3677                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3678                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3679                                 SCTP_ERROR(ECONNABORTED));
3680                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3681                                 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3682                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3683                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3684                 return SCTP_DISPOSITION_ABORT;
3685         }
3686
3687         return SCTP_DISPOSITION_DISCARD;
3688 }
3689
3690 /*
3691  * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3692  *
3693  * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3694  * its cumulative TSN point to the value carried in the FORWARD TSN
3695  * chunk, and then MUST further advance its cumulative TSN point locally
3696  * if possible.
3697  * After the above processing, the data receiver MUST stop reporting any
3698  * missing TSNs earlier than or equal to the new cumulative TSN point.
3699  *
3700  * Verification Tag:  8.5 Verification Tag [Normal verification]
3701  *
3702  * The return value is the disposition of the chunk.
3703  */
3704 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3705                                        const struct sctp_association *asoc,
3706                                        const sctp_subtype_t type,
3707                                        void *arg,
3708                                        sctp_cmd_seq_t *commands)
3709 {
3710         struct sctp_chunk *chunk = arg;
3711         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3712         struct sctp_fwdtsn_skip *skip;
3713         __u16 len;
3714         __u32 tsn;
3715
3716         if (!sctp_vtag_verify(chunk, asoc)) {
3717                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3718                                 SCTP_NULL());
3719                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3720         }
3721
3722         /* Make sure that the FORWARD_TSN chunk has valid length.  */
3723         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3724                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3725                                                   commands);
3726
3727         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3728         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3729         len = ntohs(chunk->chunk_hdr->length);
3730         len -= sizeof(struct sctp_chunkhdr);
3731         skb_pull(chunk->skb, len);
3732
3733         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3734         SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3735
3736         /* The TSN is too high--silently discard the chunk and count on it
3737          * getting retransmitted later.
3738          */
3739         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3740                 goto discard_noforce;
3741
3742         /* Silently discard the chunk if stream-id is not valid */
3743         sctp_walk_fwdtsn(skip, chunk) {
3744                 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3745                         goto discard_noforce;
3746         }
3747
3748         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3749         if (len > sizeof(struct sctp_fwdtsn_hdr))
3750                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3751                                 SCTP_CHUNK(chunk));
3752
3753         /* Count this as receiving DATA. */
3754         if (asoc->autoclose) {
3755                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3756                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3757         }
3758
3759         /* FIXME: For now send a SACK, but DATA processing may
3760          * send another.
3761          */
3762         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3763
3764         return SCTP_DISPOSITION_CONSUME;
3765
3766 discard_noforce:
3767         return SCTP_DISPOSITION_DISCARD;
3768 }
3769
3770 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3771         const struct sctp_endpoint *ep,
3772         const struct sctp_association *asoc,
3773         const sctp_subtype_t type,
3774         void *arg,
3775         sctp_cmd_seq_t *commands)
3776 {
3777         struct sctp_chunk *chunk = arg;
3778         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3779         struct sctp_fwdtsn_skip *skip;
3780         __u16 len;
3781         __u32 tsn;
3782
3783         if (!sctp_vtag_verify(chunk, asoc)) {
3784                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3785                                 SCTP_NULL());
3786                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3787         }
3788
3789         /* Make sure that the FORWARD_TSN chunk has a valid length.  */
3790         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3791                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3792                                                   commands);
3793
3794         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3795         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3796         len = ntohs(chunk->chunk_hdr->length);
3797         len -= sizeof(struct sctp_chunkhdr);
3798         skb_pull(chunk->skb, len);
3799
3800         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3801         SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3802
3803         /* The TSN is too high--silently discard the chunk and count on it
3804          * getting retransmitted later.
3805          */
3806         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3807                 goto gen_shutdown;
3808
3809         /* Silently discard the chunk if stream-id is not valid */
3810         sctp_walk_fwdtsn(skip, chunk) {
3811                 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3812                         goto gen_shutdown;
3813         }
3814
3815         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3816         if (len > sizeof(struct sctp_fwdtsn_hdr))
3817                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3818                                 SCTP_CHUNK(chunk));
3819
3820         /* Go a head and force a SACK, since we are shutting down. */
3821 gen_shutdown:
3822         /* Implementor's Guide.
3823          *
3824          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3825          * respond to each received packet containing one or more DATA chunk(s)
3826          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3827          */
3828         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3829         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3830         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3831                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3832
3833         return SCTP_DISPOSITION_CONSUME;
3834 }
3835
3836 /*
3837  * SCTP-AUTH Section 6.3 Receving authenticated chukns
3838  *
3839  *    The receiver MUST use the HMAC algorithm indicated in the HMAC
3840  *    Identifier field.  If this algorithm was not specified by the
3841  *    receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3842  *    during association setup, the AUTH chunk and all chunks after it MUST
3843  *    be discarded and an ERROR chunk SHOULD be sent with the error cause
3844  *    defined in Section 4.1.
3845  *
3846  *    If an endpoint with no shared key receives a Shared Key Identifier
3847  *    other than 0, it MUST silently discard all authenticated chunks.  If
3848  *    the endpoint has at least one endpoint pair shared key for the peer,
3849  *    it MUST use the key specified by the Shared Key Identifier if a
3850  *    key has been configured for that Shared Key Identifier.  If no
3851  *    endpoint pair shared key has been configured for that Shared Key
3852  *    Identifier, all authenticated chunks MUST be silently discarded.
3853  *
3854  * Verification Tag:  8.5 Verification Tag [Normal verification]
3855  *
3856  * The return value is the disposition of the chunk.
3857  */
3858 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3859                                     const struct sctp_association *asoc,
3860                                     const sctp_subtype_t type,
3861                                     struct sctp_chunk *chunk)
3862 {
3863         struct sctp_authhdr *auth_hdr;
3864         struct sctp_hmac *hmac;
3865         unsigned int sig_len;
3866         __u16 key_id;
3867         __u8 *save_digest;
3868         __u8 *digest;
3869
3870         /* Pull in the auth header, so we can do some more verification */
3871         auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3872         chunk->subh.auth_hdr = auth_hdr;
3873         skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3874
3875         /* Make sure that we suport the HMAC algorithm from the auth
3876          * chunk.
3877          */
3878         if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3879                 return SCTP_IERROR_AUTH_BAD_HMAC;
3880
3881         /* Make sure that the provided shared key identifier has been
3882          * configured
3883          */
3884         key_id = ntohs(auth_hdr->shkey_id);
3885         if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3886                 return SCTP_IERROR_AUTH_BAD_KEYID;
3887
3888
3889         /* Make sure that the length of the signature matches what
3890          * we expect.
3891          */
3892         sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3893         hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3894         if (sig_len != hmac->hmac_len)
3895                 return SCTP_IERROR_PROTO_VIOLATION;
3896
3897         /* Now that we've done validation checks, we can compute and
3898          * verify the hmac.  The steps involved are:
3899          *  1. Save the digest from the chunk.
3900          *  2. Zero out the digest in the chunk.
3901          *  3. Compute the new digest
3902          *  4. Compare saved and new digests.
3903          */
3904         digest = auth_hdr->hmac;
3905         skb_pull(chunk->skb, sig_len);
3906
3907         save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3908         if (!save_digest)
3909                 goto nomem;
3910
3911         memset(digest, 0, sig_len);
3912
3913         sctp_auth_calculate_hmac(asoc, chunk->skb,
3914                                 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3915                                 GFP_ATOMIC);
3916
3917         /* Discard the packet if the digests do not match */
3918         if (memcmp(save_digest, digest, sig_len)) {
3919                 kfree(save_digest);
3920                 return SCTP_IERROR_BAD_SIG;
3921         }
3922
3923         kfree(save_digest);
3924         chunk->auth = 1;
3925
3926         return SCTP_IERROR_NO_ERROR;
3927 nomem:
3928         return SCTP_IERROR_NOMEM;
3929 }
3930
3931 sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3932                                     const struct sctp_association *asoc,
3933                                     const sctp_subtype_t type,
3934                                     void *arg,
3935                                     sctp_cmd_seq_t *commands)
3936 {
3937         struct sctp_authhdr *auth_hdr;
3938         struct sctp_chunk *chunk = arg;
3939         struct sctp_chunk *err_chunk;
3940         sctp_ierror_t error;
3941
3942         /* Make sure that the peer has AUTH capable */
3943         if (!asoc->peer.auth_capable)
3944                 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3945
3946         if (!sctp_vtag_verify(chunk, asoc)) {
3947                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3948                                 SCTP_NULL());
3949                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3950         }
3951
3952         /* Make sure that the AUTH chunk has valid length.  */
3953         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3954                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3955                                                   commands);
3956
3957         auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3958         error = sctp_sf_authenticate(ep, asoc, type, chunk);
3959         switch (error) {
3960                 case SCTP_IERROR_AUTH_BAD_HMAC:
3961                         /* Generate the ERROR chunk and discard the rest
3962                          * of the packet
3963                          */
3964                         err_chunk = sctp_make_op_error(asoc, chunk,
3965                                                         SCTP_ERROR_UNSUP_HMAC,
3966                                                         &auth_hdr->hmac_id,
3967                                                         sizeof(__u16));
3968                         if (err_chunk) {
3969                                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3970                                                 SCTP_CHUNK(err_chunk));
3971                         }
3972                         /* Fall Through */
3973                 case SCTP_IERROR_AUTH_BAD_KEYID:
3974                 case SCTP_IERROR_BAD_SIG:
3975                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3976                         break;
3977                 case SCTP_IERROR_PROTO_VIOLATION:
3978                         return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3979                                                           commands);
3980                         break;
3981                 case SCTP_IERROR_NOMEM:
3982                         return SCTP_DISPOSITION_NOMEM;
3983                 default:
3984                         break;
3985         }
3986
3987         if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3988                 struct sctp_ulpevent *ev;
3989
3990                 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3991                                     SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3992
3993                 if (!ev)
3994                         return -ENOMEM;
3995
3996                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3997                                 SCTP_ULPEVENT(ev));
3998         }
3999
4000         return SCTP_DISPOSITION_CONSUME;
4001 }
4002
4003 /*
4004  * Process an unknown chunk.
4005  *
4006  * Section: 3.2. Also, 2.1 in the implementor's guide.
4007  *
4008  * Chunk Types are encoded such that the highest-order two bits specify
4009  * the action that must be taken if the processing endpoint does not
4010  * recognize the Chunk Type.
4011  *
4012  * 00 - Stop processing this SCTP packet and discard it, do not process
4013  *      any further chunks within it.
4014  *
4015  * 01 - Stop processing this SCTP packet and discard it, do not process
4016  *      any further chunks within it, and report the unrecognized
4017  *      chunk in an 'Unrecognized Chunk Type'.
4018  *
4019  * 10 - Skip this chunk and continue processing.
4020  *
4021  * 11 - Skip this chunk and continue processing, but report in an ERROR
4022  *      Chunk using the 'Unrecognized Chunk Type' cause of error.
4023  *
4024  * The return value is the disposition of the chunk.
4025  */
4026 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4027                                      const struct sctp_association *asoc,
4028                                      const sctp_subtype_t type,
4029                                      void *arg,
4030                                      sctp_cmd_seq_t *commands)
4031 {
4032         struct sctp_chunk *unk_chunk = arg;
4033         struct sctp_chunk *err_chunk;
4034         sctp_chunkhdr_t *hdr;
4035
4036         SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4037
4038         if (!sctp_vtag_verify(unk_chunk, asoc))
4039                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4040
4041         /* Make sure that the chunk has a valid length.
4042          * Since we don't know the chunk type, we use a general
4043          * chunkhdr structure to make a comparison.
4044          */
4045         if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4046                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4047                                                   commands);
4048
4049         switch (type.chunk & SCTP_CID_ACTION_MASK) {
4050         case SCTP_CID_ACTION_DISCARD:
4051                 /* Discard the packet.  */
4052                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4053                 break;
4054         case SCTP_CID_ACTION_DISCARD_ERR:
4055                 /* Generate an ERROR chunk as response. */
4056                 hdr = unk_chunk->chunk_hdr;
4057                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4058                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4059                                                WORD_ROUND(ntohs(hdr->length)));
4060                 if (err_chunk) {
4061                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4062                                         SCTP_CHUNK(err_chunk));
4063                 }
4064
4065                 /* Discard the packet.  */
4066                 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4067                 return SCTP_DISPOSITION_CONSUME;
4068                 break;
4069         case SCTP_CID_ACTION_SKIP:
4070                 /* Skip the chunk.  */
4071                 return SCTP_DISPOSITION_DISCARD;
4072                 break;
4073         case SCTP_CID_ACTION_SKIP_ERR:
4074                 /* Generate an ERROR chunk as response. */
4075                 hdr = unk_chunk->chunk_hdr;
4076                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4077                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4078                                                WORD_ROUND(ntohs(hdr->length)));
4079                 if (err_chunk) {
4080                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4081                                         SCTP_CHUNK(err_chunk));
4082                 }
4083                 /* Skip the chunk.  */
4084                 return SCTP_DISPOSITION_CONSUME;
4085                 break;
4086         default:
4087                 break;
4088         }
4089
4090         return SCTP_DISPOSITION_DISCARD;
4091 }
4092
4093 /*
4094  * Discard the chunk.
4095  *
4096  * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4097  * [Too numerous to mention...]
4098  * Verification Tag: No verification needed.
4099  * Inputs
4100  * (endpoint, asoc, chunk)
4101  *
4102  * Outputs
4103  * (asoc, reply_msg, msg_up, timers, counters)
4104  *
4105  * The return value is the disposition of the chunk.
4106  */
4107 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4108                                          const struct sctp_association *asoc,
4109                                          const sctp_subtype_t type,
4110                                          void *arg,
4111                                          sctp_cmd_seq_t *commands)
4112 {
4113         struct sctp_chunk *chunk = arg;
4114
4115         /* Make sure that the chunk has a valid length.
4116          * Since we don't know the chunk type, we use a general
4117          * chunkhdr structure to make a comparison.
4118          */
4119         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4120                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4121                                                   commands);
4122
4123         SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4124         return SCTP_DISPOSITION_DISCARD;
4125 }
4126
4127 /*
4128  * Discard the whole packet.
4129  *
4130  * Section: 8.4 2)
4131  *
4132  * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4133  *    silently discard the OOTB packet and take no further action.
4134  *
4135  * Verification Tag: No verification necessary
4136  *
4137  * Inputs
4138  * (endpoint, asoc, chunk)
4139  *
4140  * Outputs
4141  * (asoc, reply_msg, msg_up, timers, counters)
4142  *
4143  * The return value is the disposition of the chunk.
4144  */
4145 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4146                                     const struct sctp_association *asoc,
4147                                     const sctp_subtype_t type,
4148                                     void *arg,
4149                                     sctp_cmd_seq_t *commands)
4150 {
4151         SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
4152         sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4153
4154         return SCTP_DISPOSITION_CONSUME;
4155 }
4156
4157
4158 /*
4159  * The other end is violating protocol.
4160  *
4161  * Section: Not specified
4162  * Verification Tag: Not specified
4163  * Inputs
4164  * (endpoint, asoc, chunk)
4165  *
4166  * Outputs
4167  * (asoc, reply_msg, msg_up, timers, counters)
4168  *
4169  * We simply tag the chunk as a violation.  The state machine will log
4170  * the violation and continue.
4171  */
4172 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4173                                      const struct sctp_association *asoc,
4174                                      const sctp_subtype_t type,
4175                                      void *arg,
4176                                      sctp_cmd_seq_t *commands)
4177 {
4178         struct sctp_chunk *chunk = arg;
4179
4180         /* Make sure that the chunk has a valid length. */
4181         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4182                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4183                                                   commands);
4184
4185         return SCTP_DISPOSITION_VIOLATION;
4186 }
4187
4188 /*
4189  * Common function to handle a protocol violation.
4190  */
4191 static sctp_disposition_t sctp_sf_abort_violation(
4192                                      const struct sctp_endpoint *ep,
4193                                      const struct sctp_association *asoc,
4194                                      void *arg,
4195                                      sctp_cmd_seq_t *commands,
4196                                      const __u8 *payload,
4197                                      const size_t paylen)
4198 {
4199         struct sctp_packet *packet = NULL;
4200         struct sctp_chunk *chunk =  arg;
4201         struct sctp_chunk *abort = NULL;
4202
4203         /* SCTP-AUTH, Section 6.3:
4204          *    It should be noted that if the receiver wants to tear
4205          *    down an association in an authenticated way only, the
4206          *    handling of malformed packets should not result in
4207          *    tearing down the association.
4208          *
4209          * This means that if we only want to abort associations
4210          * in an authenticated way (i.e AUTH+ABORT), then we
4211          * can't destroy this association just becuase the packet
4212          * was malformed.
4213          */
4214         if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4215                 goto discard;
4216
4217         /* Make the abort chunk. */
4218         abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4219         if (!abort)
4220                 goto nomem;
4221
4222         if (asoc) {
4223                 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4224                 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4225                     !asoc->peer.i.init_tag) {
4226                         sctp_initack_chunk_t *initack;
4227
4228                         initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4229                         if (!sctp_chunk_length_valid(chunk,
4230                                                      sizeof(sctp_initack_chunk_t)))
4231                                 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4232                         else {
4233                                 unsigned int inittag;
4234
4235                                 inittag = ntohl(initack->init_hdr.init_tag);
4236                                 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4237                                                 SCTP_U32(inittag));
4238                         }
4239                 }
4240
4241                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4242                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4243
4244                 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4245                         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4246                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4247                         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4248                                         SCTP_ERROR(ECONNREFUSED));
4249                         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4250                                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4251                 } else {
4252                         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4253                                         SCTP_ERROR(ECONNABORTED));
4254                         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4255                                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4256                         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4257                 }
4258         } else {
4259                 packet = sctp_ootb_pkt_new(asoc, chunk);
4260
4261                 if (!packet)
4262                         goto nomem_pkt;
4263
4264                 if (sctp_test_T_bit(abort))
4265                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4266
4267                 abort->skb->sk = ep->base.sk;
4268
4269                 sctp_packet_append_chunk(packet, abort);
4270
4271                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4272                         SCTP_PACKET(packet));
4273
4274                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4275         }
4276
4277         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4278
4279 discard:
4280         sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4281         return SCTP_DISPOSITION_ABORT;
4282
4283 nomem_pkt:
4284         sctp_chunk_free(abort);
4285 nomem:
4286         return SCTP_DISPOSITION_NOMEM;
4287 }
4288
4289 /*
4290  * Handle a protocol violation when the chunk length is invalid.
4291  * "Invalid" length is identified as smaller than the minimal length a
4292  * given chunk can be.  For example, a SACK chunk has invalid length
4293  * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4294  *
4295  * We inform the other end by sending an ABORT with a Protocol Violation
4296  * error code.
4297  *
4298  * Section: Not specified
4299  * Verification Tag:  Nothing to do
4300  * Inputs
4301  * (endpoint, asoc, chunk)
4302  *
4303  * Outputs
4304  * (reply_msg, msg_up, counters)
4305  *
4306  * Generate an  ABORT chunk and terminate the association.
4307  */
4308 static sctp_disposition_t sctp_sf_violation_chunklen(
4309                                      const struct sctp_endpoint *ep,
4310                                      const struct sctp_association *asoc,
4311                                      const sctp_subtype_t type,
4312                                      void *arg,
4313                                      sctp_cmd_seq_t *commands)
4314 {
4315         static const char err_str[]="The following chunk had invalid length:";
4316
4317         return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4318                                         sizeof(err_str));
4319 }
4320
4321 /*
4322  * Handle a protocol violation when the parameter length is invalid.
4323  * "Invalid" length is identified as smaller than the minimal length a
4324  * given parameter can be.
4325  */
4326 static sctp_disposition_t sctp_sf_violation_paramlen(
4327                                      const struct sctp_endpoint *ep,
4328                                      const struct sctp_association *asoc,
4329                                      const sctp_subtype_t type,
4330                                      void *arg, void *ext,
4331                                      sctp_cmd_seq_t *commands)
4332 {
4333         struct sctp_chunk *chunk =  arg;
4334         struct sctp_paramhdr *param = ext;
4335         struct sctp_chunk *abort = NULL;
4336
4337         if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4338                 goto discard;
4339
4340         /* Make the abort chunk. */
4341         abort = sctp_make_violation_paramlen(asoc, chunk, param);
4342         if (!abort)
4343                 goto nomem;
4344
4345         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4346         SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4347
4348         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4349                         SCTP_ERROR(ECONNABORTED));
4350         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4351                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4352         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4353         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4354
4355 discard:
4356         sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4357         return SCTP_DISPOSITION_ABORT;
4358 nomem:
4359         return SCTP_DISPOSITION_NOMEM;
4360 }
4361
4362 /* Handle a protocol violation when the peer trying to advance the
4363  * cumulative tsn ack to a point beyond the max tsn currently sent.
4364  *
4365  * We inform the other end by sending an ABORT with a Protocol Violation
4366  * error code.
4367  */
4368 static sctp_disposition_t sctp_sf_violation_ctsn(
4369                                      const struct sctp_endpoint *ep,
4370                                      const struct sctp_association *asoc,
4371                                      const sctp_subtype_t type,
4372                                      void *arg,
4373                                      sctp_cmd_seq_t *commands)
4374 {
4375         static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4376
4377         return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4378                                         sizeof(err_str));
4379 }
4380
4381 /* Handle protocol violation of an invalid chunk bundling.  For example,
4382  * when we have an association and we recieve bundled INIT-ACK, or
4383  * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4384  * statement from the specs.  Additinally, there might be an attacker
4385  * on the path and we may not want to continue this communication.
4386  */
4387 static sctp_disposition_t sctp_sf_violation_chunk(
4388                                      const struct sctp_endpoint *ep,
4389                                      const struct sctp_association *asoc,
4390                                      const sctp_subtype_t type,
4391                                      void *arg,
4392                                      sctp_cmd_seq_t *commands)
4393 {
4394         static const char err_str[]="The following chunk violates protocol:";
4395
4396         if (!asoc)
4397                 return sctp_sf_violation(ep, asoc, type, arg, commands);
4398
4399         return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4400                                         sizeof(err_str));
4401 }
4402 /***************************************************************************
4403  * These are the state functions for handling primitive (Section 10) events.
4404  ***************************************************************************/
4405 /*
4406  * sctp_sf_do_prm_asoc
4407  *
4408  * Section: 10.1 ULP-to-SCTP
4409  * B) Associate
4410  *
4411  * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4412  * outbound stream count)
4413  * -> association id [,destination transport addr list] [,outbound stream
4414  * count]
4415  *
4416  * This primitive allows the upper layer to initiate an association to a
4417  * specific peer endpoint.
4418  *
4419  * The peer endpoint shall be specified by one of the transport addresses
4420  * which defines the endpoint (see Section 1.4).  If the local SCTP
4421  * instance has not been initialized, the ASSOCIATE is considered an
4422  * error.
4423  * [This is not relevant for the kernel implementation since we do all
4424  * initialization at boot time.  It we hadn't initialized we wouldn't
4425  * get anywhere near this code.]
4426  *
4427  * An association id, which is a local handle to the SCTP association,
4428  * will be returned on successful establishment of the association. If
4429  * SCTP is not able to open an SCTP association with the peer endpoint,
4430  * an error is returned.
4431  * [In the kernel implementation, the struct sctp_association needs to
4432  * be created BEFORE causing this primitive to run.]
4433  *
4434  * Other association parameters may be returned, including the
4435  * complete destination transport addresses of the peer as well as the
4436  * outbound stream count of the local endpoint. One of the transport
4437  * address from the returned destination addresses will be selected by
4438  * the local endpoint as default primary path for sending SCTP packets
4439  * to this peer.  The returned "destination transport addr list" can
4440  * be used by the ULP to change the default primary path or to force
4441  * sending a packet to a specific transport address.  [All of this
4442  * stuff happens when the INIT ACK arrives.  This is a NON-BLOCKING
4443  * function.]
4444  *
4445  * Mandatory attributes:
4446  *
4447  * o local SCTP instance name - obtained from the INITIALIZE operation.
4448  *   [This is the argument asoc.]
4449  * o destination transport addr - specified as one of the transport
4450  * addresses of the peer endpoint with which the association is to be
4451  * established.
4452  *  [This is asoc->peer.active_path.]
4453  * o outbound stream count - the number of outbound streams the ULP
4454  * would like to open towards this peer endpoint.
4455  * [BUG: This is not currently implemented.]
4456  * Optional attributes:
4457  *
4458  * None.
4459  *
4460  * The return value is a disposition.
4461  */
4462 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4463                                        const struct sctp_association *asoc,
4464                                        const sctp_subtype_t type,
4465                                        void *arg,
4466                                        sctp_cmd_seq_t *commands)
4467 {
4468         struct sctp_chunk *repl;
4469         struct sctp_association* my_asoc;
4470
4471         /* The comment below says that we enter COOKIE-WAIT AFTER
4472          * sending the INIT, but that doesn't actually work in our
4473          * implementation...
4474          */
4475         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4476                         SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4477
4478         /* RFC 2960 5.1 Normal Establishment of an Association
4479          *
4480          * A) "A" first sends an INIT chunk to "Z".  In the INIT, "A"
4481          * must provide its Verification Tag (Tag_A) in the Initiate
4482          * Tag field.  Tag_A SHOULD be a random number in the range of
4483          * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4484          */
4485
4486         repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4487         if (!repl)
4488                 goto nomem;
4489
4490         /* Cast away the const modifier, as we want to just
4491          * rerun it through as a sideffect.
4492          */
4493         my_asoc = (struct sctp_association *)asoc;
4494         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4495
4496         /* Choose transport for INIT. */
4497         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4498                         SCTP_CHUNK(repl));
4499
4500         /* After sending the INIT, "A" starts the T1-init timer and
4501          * enters the COOKIE-WAIT state.
4502          */
4503         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4504                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4505         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4506         return SCTP_DISPOSITION_CONSUME;
4507
4508 nomem:
4509         return SCTP_DISPOSITION_NOMEM;
4510 }
4511
4512 /*
4513  * Process the SEND primitive.
4514  *
4515  * Section: 10.1 ULP-to-SCTP
4516  * E) Send
4517  *
4518  * Format: SEND(association id, buffer address, byte count [,context]
4519  *         [,stream id] [,life time] [,destination transport address]
4520  *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4521  * -> result
4522  *
4523  * This is the main method to send user data via SCTP.
4524  *
4525  * Mandatory attributes:
4526  *
4527  *  o association id - local handle to the SCTP association
4528  *
4529  *  o buffer address - the location where the user message to be
4530  *    transmitted is stored;
4531  *
4532  *  o byte count - The size of the user data in number of bytes;
4533  *
4534  * Optional attributes:
4535  *
4536  *  o context - an optional 32 bit integer that will be carried in the
4537  *    sending failure notification to the ULP if the transportation of
4538  *    this User Message fails.
4539  *
4540  *  o stream id - to indicate which stream to send the data on. If not
4541  *    specified, stream 0 will be used.
4542  *
4543  *  o life time - specifies the life time of the user data. The user data
4544  *    will not be sent by SCTP after the life time expires. This
4545  *    parameter can be used to avoid efforts to transmit stale
4546  *    user messages. SCTP notifies the ULP if the data cannot be
4547  *    initiated to transport (i.e. sent to the destination via SCTP's
4548  *    send primitive) within the life time variable. However, the
4549  *    user data will be transmitted if SCTP has attempted to transmit a
4550  *    chunk before the life time expired.
4551  *
4552  *  o destination transport address - specified as one of the destination
4553  *    transport addresses of the peer endpoint to which this packet
4554  *    should be sent. Whenever possible, SCTP should use this destination
4555  *    transport address for sending the packets, instead of the current
4556  *    primary path.
4557  *
4558  *  o unorder flag - this flag, if present, indicates that the user
4559  *    would like the data delivered in an unordered fashion to the peer
4560  *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
4561  *    message).
4562  *
4563  *  o no-bundle flag - instructs SCTP not to bundle this user data with
4564  *    other outbound DATA chunks. SCTP MAY still bundle even when
4565  *    this flag is present, when faced with network congestion.
4566  *
4567  *  o payload protocol-id - A 32 bit unsigned integer that is to be
4568  *    passed to the peer indicating the type of payload protocol data
4569  *    being transmitted. This value is passed as opaque data by SCTP.
4570  *
4571  * The return value is the disposition.
4572  */
4573 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4574                                        const struct sctp_association *asoc,
4575                                        const sctp_subtype_t type,
4576                                        void *arg,
4577                                        sctp_cmd_seq_t *commands)
4578 {
4579         struct sctp_datamsg *msg = arg;
4580
4581         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4582         return SCTP_DISPOSITION_CONSUME;
4583 }
4584
4585 /*
4586  * Process the SHUTDOWN primitive.
4587  *
4588  * Section: 10.1:
4589  * C) Shutdown
4590  *
4591  * Format: SHUTDOWN(association id)
4592  * -> result
4593  *
4594  * Gracefully closes an association. Any locally queued user data
4595  * will be delivered to the peer. The association will be terminated only
4596  * after the peer acknowledges all the SCTP packets sent.  A success code
4597  * will be returned on successful termination of the association. If
4598  * attempting to terminate the association results in a failure, an error
4599  * code shall be returned.
4600  *
4601  * Mandatory attributes:
4602  *
4603  *  o association id - local handle to the SCTP association
4604  *
4605  * Optional attributes:
4606  *
4607  * None.
4608  *
4609  * The return value is the disposition.
4610  */
4611 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4612         const struct sctp_endpoint *ep,
4613         const struct sctp_association *asoc,
4614         const sctp_subtype_t type,
4615         void *arg,
4616         sctp_cmd_seq_t *commands)
4617 {
4618         int disposition;
4619
4620         /* From 9.2 Shutdown of an Association
4621          * Upon receipt of the SHUTDOWN primitive from its upper
4622          * layer, the endpoint enters SHUTDOWN-PENDING state and
4623          * remains there until all outstanding data has been
4624          * acknowledged by its peer. The endpoint accepts no new data
4625          * from its upper layer, but retransmits data to the far end
4626          * if necessary to fill gaps.
4627          */
4628         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4629                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4630
4631         disposition = SCTP_DISPOSITION_CONSUME;
4632         if (sctp_outq_is_empty(&asoc->outqueue)) {
4633                 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4634                                                             arg, commands);
4635         }
4636         return disposition;
4637 }
4638
4639 /*
4640  * Process the ABORT primitive.
4641  *
4642  * Section: 10.1:
4643  * C) Abort
4644  *
4645  * Format: Abort(association id [, cause code])
4646  * -> result
4647  *
4648  * Ungracefully closes an association. Any locally queued user data
4649  * will be discarded and an ABORT chunk is sent to the peer.  A success code
4650  * will be returned on successful abortion of the association. If
4651  * attempting to abort the association results in a failure, an error
4652  * code shall be returned.
4653  *
4654  * Mandatory attributes:
4655  *
4656  *  o association id - local handle to the SCTP association
4657  *
4658  * Optional attributes:
4659  *
4660  *  o cause code - reason of the abort to be passed to the peer
4661  *
4662  * None.
4663  *
4664  * The return value is the disposition.
4665  */
4666 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4667         const struct sctp_endpoint *ep,
4668         const struct sctp_association *asoc,
4669         const sctp_subtype_t type,
4670         void *arg,
4671         sctp_cmd_seq_t *commands)
4672 {
4673         /* From 9.1 Abort of an Association
4674          * Upon receipt of the ABORT primitive from its upper
4675          * layer, the endpoint enters CLOSED state and
4676          * discard all outstanding data has been
4677          * acknowledged by its peer. The endpoint accepts no new data
4678          * from its upper layer, but retransmits data to the far end
4679          * if necessary to fill gaps.
4680          */
4681         struct sctp_chunk *abort = arg;
4682         sctp_disposition_t retval;
4683
4684         retval = SCTP_DISPOSITION_CONSUME;
4685
4686         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4687
4688         /* Even if we can't send the ABORT due to low memory delete the
4689          * TCB.  This is a departure from our typical NOMEM handling.
4690          */
4691
4692         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4693                         SCTP_ERROR(ECONNABORTED));
4694         /* Delete the established association. */
4695         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4696                         SCTP_PERR(SCTP_ERROR_USER_ABORT));
4697
4698         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4699         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4700
4701         return retval;
4702 }
4703
4704 /* We tried an illegal operation on an association which is closed.  */
4705 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4706                                         const struct sctp_association *asoc,
4707                                         const sctp_subtype_t type,
4708                                         void *arg,
4709                                         sctp_cmd_seq_t *commands)
4710 {
4711         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4712         return SCTP_DISPOSITION_CONSUME;
4713 }
4714
4715 /* We tried an illegal operation on an association which is shutting
4716  * down.
4717  */
4718 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4719                                           const struct sctp_association *asoc,
4720                                           const sctp_subtype_t type,
4721                                           void *arg,
4722                                           sctp_cmd_seq_t *commands)
4723 {
4724         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4725                         SCTP_ERROR(-ESHUTDOWN));
4726         return SCTP_DISPOSITION_CONSUME;
4727 }
4728
4729 /*
4730  * sctp_cookie_wait_prm_shutdown
4731  *
4732  * Section: 4 Note: 2
4733  * Verification Tag:
4734  * Inputs
4735  * (endpoint, asoc)
4736  *
4737  * The RFC does not explicitly address this issue, but is the route through the
4738  * state table when someone issues a shutdown while in COOKIE_WAIT state.
4739  *
4740  * Outputs
4741  * (timers)
4742  */
4743 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4744         const struct sctp_endpoint *ep,
4745         const struct sctp_association *asoc,
4746         const sctp_subtype_t type,
4747         void *arg,
4748         sctp_cmd_seq_t *commands)
4749 {
4750         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4751                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4752
4753         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4754                         SCTP_STATE(SCTP_STATE_CLOSED));
4755
4756         SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4757
4758         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4759
4760         return SCTP_DISPOSITION_DELETE_TCB;
4761 }
4762
4763 /*
4764  * sctp_cookie_echoed_prm_shutdown
4765  *
4766  * Section: 4 Note: 2
4767  * Verification Tag:
4768  * Inputs
4769  * (endpoint, asoc)
4770  *
4771  * The RFC does not explcitly address this issue, but is the route through the
4772  * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4773  *
4774  * Outputs
4775  * (timers)
4776  */
4777 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4778         const struct sctp_endpoint *ep,
4779         const struct sctp_association *asoc,
4780         const sctp_subtype_t type,
4781         void *arg, sctp_cmd_seq_t *commands)
4782 {
4783         /* There is a single T1 timer, so we should be able to use
4784          * common function with the COOKIE-WAIT state.
4785          */
4786         return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4787 }
4788
4789 /*
4790  * sctp_sf_cookie_wait_prm_abort
4791  *
4792  * Section: 4 Note: 2
4793  * Verification Tag:
4794  * Inputs
4795  * (endpoint, asoc)
4796  *
4797  * The RFC does not explicitly address this issue, but is the route through the
4798  * state table when someone issues an abort while in COOKIE_WAIT state.
4799  *
4800  * Outputs
4801  * (timers)
4802  */
4803 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4804         const struct sctp_endpoint *ep,
4805         const struct sctp_association *asoc,
4806         const sctp_subtype_t type,
4807         void *arg,
4808         sctp_cmd_seq_t *commands)
4809 {
4810         struct sctp_chunk *abort = arg;
4811         sctp_disposition_t retval;
4812
4813         /* Stop T1-init timer */
4814         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4815                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4816         retval = SCTP_DISPOSITION_CONSUME;
4817
4818         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4819
4820         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4821                         SCTP_STATE(SCTP_STATE_CLOSED));
4822
4823         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4824
4825         /* Even if we can't send the ABORT due to low memory delete the
4826          * TCB.  This is a departure from our typical NOMEM handling.
4827          */
4828
4829         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4830                         SCTP_ERROR(ECONNREFUSED));
4831         /* Delete the established association. */
4832         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4833                         SCTP_PERR(SCTP_ERROR_USER_ABORT));
4834
4835         return retval;
4836 }
4837
4838 /*
4839  * sctp_sf_cookie_echoed_prm_abort
4840  *
4841  * Section: 4 Note: 3
4842  * Verification Tag:
4843  * Inputs
4844  * (endpoint, asoc)
4845  *
4846  * The RFC does not explcitly address this issue, but is the route through the
4847  * state table when someone issues an abort while in COOKIE_ECHOED state.
4848  *
4849  * Outputs
4850  * (timers)
4851  */
4852 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4853         const struct sctp_endpoint *ep,
4854         const struct sctp_association *asoc,
4855         const sctp_subtype_t type,
4856         void *arg,
4857         sctp_cmd_seq_t *commands)
4858 {
4859         /* There is a single T1 timer, so we should be able to use
4860          * common function with the COOKIE-WAIT state.
4861          */
4862         return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4863 }
4864
4865 /*
4866  * sctp_sf_shutdown_pending_prm_abort
4867  *
4868  * Inputs
4869  * (endpoint, asoc)
4870  *
4871  * The RFC does not explicitly address this issue, but is the route through the
4872  * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4873  *
4874  * Outputs
4875  * (timers)
4876  */
4877 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4878         const struct sctp_endpoint *ep,
4879         const struct sctp_association *asoc,
4880         const sctp_subtype_t type,
4881         void *arg,
4882         sctp_cmd_seq_t *commands)
4883 {
4884         /* Stop the T5-shutdown guard timer.  */
4885         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4886                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4887
4888         return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4889 }
4890
4891 /*
4892  * sctp_sf_shutdown_sent_prm_abort
4893  *
4894  * Inputs
4895  * (endpoint, asoc)
4896  *
4897  * The RFC does not explicitly address this issue, but is the route through the
4898  * state table when someone issues an abort while in SHUTDOWN-SENT state.
4899  *
4900  * Outputs
4901  * (timers)
4902  */
4903 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4904         const struct sctp_endpoint *ep,
4905         const struct sctp_association *asoc,
4906         const sctp_subtype_t type,
4907         void *arg,
4908         sctp_cmd_seq_t *commands)
4909 {
4910         /* Stop the T2-shutdown timer.  */
4911         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4912                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4913
4914         /* Stop the T5-shutdown guard timer.  */
4915         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4916                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4917
4918         return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4919 }
4920
4921 /*
4922  * sctp_sf_cookie_echoed_prm_abort
4923  *
4924  * Inputs
4925  * (endpoint, asoc)
4926  *
4927  * The RFC does not explcitly address this issue, but is the route through the
4928  * state table when someone issues an abort while in COOKIE_ECHOED state.
4929  *
4930  * Outputs
4931  * (timers)
4932  */
4933 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4934         const struct sctp_endpoint *ep,
4935         const struct sctp_association *asoc,
4936         const sctp_subtype_t type,
4937         void *arg,
4938         sctp_cmd_seq_t *commands)
4939 {
4940         /* The same T2 timer, so we should be able to use
4941          * common function with the SHUTDOWN-SENT state.
4942          */
4943         return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4944 }
4945
4946 /*
4947  * Process the REQUESTHEARTBEAT primitive
4948  *
4949  * 10.1 ULP-to-SCTP
4950  * J) Request Heartbeat
4951  *
4952  * Format: REQUESTHEARTBEAT(association id, destination transport address)
4953  *
4954  * -> result
4955  *
4956  * Instructs the local endpoint to perform a HeartBeat on the specified
4957  * destination transport address of the given association. The returned
4958  * result should indicate whether the transmission of the HEARTBEAT
4959  * chunk to the destination address is successful.
4960  *
4961  * Mandatory attributes:
4962  *
4963  * o association id - local handle to the SCTP association
4964  *
4965  * o destination transport address - the transport address of the
4966  *   association on which a heartbeat should be issued.
4967  */
4968 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4969                                         const struct sctp_endpoint *ep,
4970                                         const struct sctp_association *asoc,
4971                                         const sctp_subtype_t type,
4972                                         void *arg,
4973                                         sctp_cmd_seq_t *commands)
4974 {
4975         if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4976                                       (struct sctp_transport *)arg, commands))
4977                 return SCTP_DISPOSITION_NOMEM;
4978
4979         /*
4980          * RFC 2960 (bis), section 8.3
4981          *
4982          *    D) Request an on-demand HEARTBEAT on a specific destination
4983          *    transport address of a given association.
4984          *
4985          *    The endpoint should increment the respective error  counter of
4986          *    the destination transport address each time a HEARTBEAT is sent
4987          *    to that address and not acknowledged within one RTO.
4988          *
4989          */
4990         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
4991                         SCTP_TRANSPORT(arg));
4992         return SCTP_DISPOSITION_CONSUME;
4993 }
4994
4995 /*
4996  * ADDIP Section 4.1 ASCONF Chunk Procedures
4997  * When an endpoint has an ASCONF signaled change to be sent to the
4998  * remote endpoint it should do A1 to A9
4999  */
5000 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5001                                         const struct sctp_association *asoc,
5002                                         const sctp_subtype_t type,
5003                                         void *arg,
5004                                         sctp_cmd_seq_t *commands)
5005 {
5006         struct sctp_chunk *chunk = arg;
5007
5008         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5009         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5010                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5011         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5012         return SCTP_DISPOSITION_CONSUME;
5013 }
5014
5015 /*
5016  * Ignore the primitive event
5017  *
5018  * The return value is the disposition of the primitive.
5019  */
5020 sctp_disposition_t sctp_sf_ignore_primitive(
5021         const struct sctp_endpoint *ep,
5022         const struct sctp_association *asoc,
5023         const sctp_subtype_t type,
5024         void *arg,
5025         sctp_cmd_seq_t *commands)
5026 {
5027         SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5028         return SCTP_DISPOSITION_DISCARD;
5029 }
5030
5031 /***************************************************************************
5032  * These are the state functions for the OTHER events.
5033  ***************************************************************************/
5034
5035 /*
5036  * Start the shutdown negotiation.
5037  *
5038  * From Section 9.2:
5039  * Once all its outstanding data has been acknowledged, the endpoint
5040  * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5041  * TSN Ack field the last sequential TSN it has received from the peer.
5042  * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5043  * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5044  * with the updated last sequential TSN received from its peer.
5045  *
5046  * The return value is the disposition.
5047  */
5048 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5049         const struct sctp_endpoint *ep,
5050         const struct sctp_association *asoc,
5051         const sctp_subtype_t type,
5052         void *arg,
5053         sctp_cmd_seq_t *commands)
5054 {
5055         struct sctp_chunk *reply;
5056
5057         /* Once all its outstanding data has been acknowledged, the
5058          * endpoint shall send a SHUTDOWN chunk to its peer including
5059          * in the Cumulative TSN Ack field the last sequential TSN it
5060          * has received from the peer.
5061          */
5062         reply = sctp_make_shutdown(asoc, NULL);
5063         if (!reply)
5064                 goto nomem;
5065
5066         /* Set the transport for the SHUTDOWN chunk and the timeout for the
5067          * T2-shutdown timer.
5068          */
5069         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5070
5071         /* It shall then start the T2-shutdown timer */
5072         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5073                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5074
5075         /* RFC 4960 Section 9.2
5076          * The sender of the SHUTDOWN MAY also start an overall guard timer
5077          * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5078          */
5079         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5080                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5081
5082         if (asoc->autoclose)
5083                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5084                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5085
5086         /* and enter the SHUTDOWN-SENT state.  */
5087         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5088                         SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5089
5090         /* sctp-implguide 2.10 Issues with Heartbeating and failover
5091          *
5092          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5093          * or SHUTDOWN-ACK.
5094          */
5095         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5096
5097         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5098
5099         return SCTP_DISPOSITION_CONSUME;
5100
5101 nomem:
5102         return SCTP_DISPOSITION_NOMEM;
5103 }
5104
5105 /*
5106  * Generate a SHUTDOWN ACK now that everything is SACK'd.
5107  *
5108  * From Section 9.2:
5109  *
5110  * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5111  * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5112  * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5113  * endpoint must re-send the SHUTDOWN ACK.
5114  *
5115  * The return value is the disposition.
5116  */
5117 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5118         const struct sctp_endpoint *ep,
5119         const struct sctp_association *asoc,
5120         const sctp_subtype_t type,
5121         void *arg,
5122         sctp_cmd_seq_t *commands)
5123 {
5124         struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5125         struct sctp_chunk *reply;
5126
5127         /* There are 2 ways of getting here:
5128          *    1) called in response to a SHUTDOWN chunk
5129          *    2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5130          *
5131          * For the case (2), the arg parameter is set to NULL.  We need
5132          * to check that we have a chunk before accessing it's fields.
5133          */
5134         if (chunk) {
5135                 if (!sctp_vtag_verify(chunk, asoc))
5136                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5137
5138                 /* Make sure that the SHUTDOWN chunk has a valid length. */
5139                 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5140                         return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5141                                                           commands);
5142         }
5143
5144         /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5145          * shall send a SHUTDOWN ACK ...
5146          */
5147         reply = sctp_make_shutdown_ack(asoc, chunk);
5148         if (!reply)
5149                 goto nomem;
5150
5151         /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5152          * the T2-shutdown timer.
5153          */
5154         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5155
5156         /* and start/restart a T2-shutdown timer of its own, */
5157         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5158                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5159
5160         if (asoc->autoclose)
5161                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5162                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5163
5164         /* Enter the SHUTDOWN-ACK-SENT state.  */
5165         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5166                         SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5167
5168         /* sctp-implguide 2.10 Issues with Heartbeating and failover
5169          *
5170          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5171          * or SHUTDOWN-ACK.
5172          */
5173         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5174
5175         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5176
5177         return SCTP_DISPOSITION_CONSUME;
5178
5179 nomem:
5180         return SCTP_DISPOSITION_NOMEM;
5181 }
5182
5183 /*
5184  * Ignore the event defined as other
5185  *
5186  * The return value is the disposition of the event.
5187  */
5188 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5189                                         const struct sctp_association *asoc,
5190                                         const sctp_subtype_t type,
5191                                         void *arg,
5192                                         sctp_cmd_seq_t *commands)
5193 {
5194         SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5195         return SCTP_DISPOSITION_DISCARD;
5196 }
5197
5198 /************************************************************
5199  * These are the state functions for handling timeout events.
5200  ************************************************************/
5201
5202 /*
5203  * RTX Timeout
5204  *
5205  * Section: 6.3.3 Handle T3-rtx Expiration
5206  *
5207  * Whenever the retransmission timer T3-rtx expires for a destination
5208  * address, do the following:
5209  * [See below]
5210  *
5211  * The return value is the disposition of the chunk.
5212  */
5213 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5214                                         const struct sctp_association *asoc,
5215                                         const sctp_subtype_t type,
5216                                         void *arg,
5217                                         sctp_cmd_seq_t *commands)
5218 {
5219         struct sctp_transport *transport = arg;
5220
5221         SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5222
5223         if (asoc->overall_error_count >= asoc->max_retrans) {
5224                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5225                                 SCTP_ERROR(ETIMEDOUT));
5226                 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5227                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5228                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5229                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5230                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5231                 return SCTP_DISPOSITION_DELETE_TCB;
5232         }
5233
5234         /* E1) For the destination address for which the timer
5235          * expires, adjust its ssthresh with rules defined in Section
5236          * 7.2.3 and set the cwnd <- MTU.
5237          */
5238
5239         /* E2) For the destination address for which the timer
5240          * expires, set RTO <- RTO * 2 ("back off the timer").  The
5241          * maximum value discussed in rule C7 above (RTO.max) may be
5242          * used to provide an upper bound to this doubling operation.
5243          */
5244
5245         /* E3) Determine how many of the earliest (i.e., lowest TSN)
5246          * outstanding DATA chunks for the address for which the
5247          * T3-rtx has expired will fit into a single packet, subject
5248          * to the MTU constraint for the path corresponding to the
5249          * destination transport address to which the retransmission
5250          * is being sent (this may be different from the address for
5251          * which the timer expires [see Section 6.4]).  Call this
5252          * value K. Bundle and retransmit those K DATA chunks in a
5253          * single packet to the destination endpoint.
5254          *
5255          * Note: Any DATA chunks that were sent to the address for
5256          * which the T3-rtx timer expired but did not fit in one MTU
5257          * (rule E3 above), should be marked for retransmission and
5258          * sent as soon as cwnd allows (normally when a SACK arrives).
5259          */
5260
5261         /* Do some failure management (Section 8.2). */
5262         sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5263
5264         /* NB: Rules E4 and F1 are implicit in R1.  */
5265         sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5266
5267         return SCTP_DISPOSITION_CONSUME;
5268 }
5269
5270 /*
5271  * Generate delayed SACK on timeout
5272  *
5273  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
5274  *
5275  * The guidelines on delayed acknowledgement algorithm specified in
5276  * Section 4.2 of [RFC2581] SHOULD be followed.  Specifically, an
5277  * acknowledgement SHOULD be generated for at least every second packet
5278  * (not every second DATA chunk) received, and SHOULD be generated
5279  * within 200 ms of the arrival of any unacknowledged DATA chunk.  In
5280  * some situations it may be beneficial for an SCTP transmitter to be
5281  * more conservative than the algorithms detailed in this document
5282  * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5283  * the following algorithms allow.
5284  */
5285 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5286                                        const struct sctp_association *asoc,
5287                                        const sctp_subtype_t type,
5288                                        void *arg,
5289                                        sctp_cmd_seq_t *commands)
5290 {
5291         SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
5292         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5293         return SCTP_DISPOSITION_CONSUME;
5294 }
5295
5296 /*
5297  * sctp_sf_t1_init_timer_expire
5298  *
5299  * Section: 4 Note: 2
5300  * Verification Tag:
5301  * Inputs
5302  * (endpoint, asoc)
5303  *
5304  *  RFC 2960 Section 4 Notes
5305  *  2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5306  *     and re-start the T1-init timer without changing state.  This MUST
5307  *     be repeated up to 'Max.Init.Retransmits' times.  After that, the
5308  *     endpoint MUST abort the initialization process and report the
5309  *     error to SCTP user.
5310  *
5311  * Outputs
5312  * (timers, events)
5313  *
5314  */
5315 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5316                                            const struct sctp_association *asoc,
5317                                            const sctp_subtype_t type,
5318                                            void *arg,
5319                                            sctp_cmd_seq_t *commands)
5320 {
5321         struct sctp_chunk *repl = NULL;
5322         struct sctp_bind_addr *bp;
5323         int attempts = asoc->init_err_counter + 1;
5324
5325         SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5326         SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
5327
5328         if (attempts <= asoc->max_init_attempts) {
5329                 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5330                 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5331                 if (!repl)
5332                         return SCTP_DISPOSITION_NOMEM;
5333
5334                 /* Choose transport for INIT. */
5335                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5336                                 SCTP_CHUNK(repl));
5337
5338                 /* Issue a sideeffect to do the needed accounting. */
5339                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5340                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5341
5342                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5343         } else {
5344                 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5345                                   " max_init_attempts: %d\n",
5346                                   attempts, asoc->max_init_attempts);
5347                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5348                                 SCTP_ERROR(ETIMEDOUT));
5349                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5350                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5351                 return SCTP_DISPOSITION_DELETE_TCB;
5352         }
5353
5354         return SCTP_DISPOSITION_CONSUME;
5355 }
5356
5357 /*
5358  * sctp_sf_t1_cookie_timer_expire
5359  *
5360  * Section: 4 Note: 2
5361  * Verification Tag:
5362  * Inputs
5363  * (endpoint, asoc)
5364  *
5365  *  RFC 2960 Section 4 Notes
5366  *  3) If the T1-cookie timer expires, the endpoint MUST retransmit
5367  *     COOKIE ECHO and re-start the T1-cookie timer without changing
5368  *     state.  This MUST be repeated up to 'Max.Init.Retransmits' times.
5369  *     After that, the endpoint MUST abort the initialization process and
5370  *     report the error to SCTP user.
5371  *
5372  * Outputs
5373  * (timers, events)
5374  *
5375  */
5376 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
5377                                            const struct sctp_association *asoc,
5378                                            const sctp_subtype_t type,
5379                                            void *arg,
5380                                            sctp_cmd_seq_t *commands)
5381 {
5382         struct sctp_chunk *repl = NULL;
5383         int attempts = asoc->init_err_counter + 1;
5384
5385         SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5386         SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
5387
5388         if (attempts <= asoc->max_init_attempts) {
5389                 repl = sctp_make_cookie_echo(asoc, NULL);
5390                 if (!repl)
5391                         return SCTP_DISPOSITION_NOMEM;
5392
5393                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5394                                 SCTP_CHUNK(repl));
5395                 /* Issue a sideeffect to do the needed accounting. */
5396                 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5397                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5398
5399                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5400         } else {
5401                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5402                                 SCTP_ERROR(ETIMEDOUT));
5403                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5404                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5405                 return SCTP_DISPOSITION_DELETE_TCB;
5406         }
5407
5408         return SCTP_DISPOSITION_CONSUME;
5409 }
5410
5411 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5412  * with the updated last sequential TSN received from its peer.
5413  *
5414  * An endpoint should limit the number of retransmissions of the
5415  * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5416  * If this threshold is exceeded the endpoint should destroy the TCB and
5417  * MUST report the peer endpoint unreachable to the upper layer (and
5418  * thus the association enters the CLOSED state).  The reception of any
5419  * packet from its peer (i.e. as the peer sends all of its queued DATA
5420  * chunks) should clear the endpoint's retransmission count and restart
5421  * the T2-Shutdown timer,  giving its peer ample opportunity to transmit
5422  * all of its queued DATA chunks that have not yet been sent.
5423  */
5424 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5425                                            const struct sctp_association *asoc,
5426                                            const sctp_subtype_t type,
5427                                            void *arg,
5428                                            sctp_cmd_seq_t *commands)
5429 {
5430         struct sctp_chunk *reply = NULL;
5431
5432         SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5433         SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5434
5435         ((struct sctp_association *)asoc)->shutdown_retries++;
5436
5437         if (asoc->overall_error_count >= asoc->max_retrans) {
5438                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5439                                 SCTP_ERROR(ETIMEDOUT));
5440                 /* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5441                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5442                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5443                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5444                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5445                 return SCTP_DISPOSITION_DELETE_TCB;
5446         }
5447
5448         switch (asoc->state) {
5449         case SCTP_STATE_SHUTDOWN_SENT:
5450                 reply = sctp_make_shutdown(asoc, NULL);
5451                 break;
5452
5453         case SCTP_STATE_SHUTDOWN_ACK_SENT:
5454                 reply = sctp_make_shutdown_ack(asoc, NULL);
5455                 break;
5456
5457         default:
5458                 BUG();
5459                 break;
5460         }
5461
5462         if (!reply)
5463                 goto nomem;
5464
5465         /* Do some failure management (Section 8.2).
5466          * If we remove the transport an SHUTDOWN was last sent to, don't
5467          * do failure management.
5468          */
5469         if (asoc->shutdown_last_sent_to)
5470                 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5471                                 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5472
5473         /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5474          * the T2-shutdown timer.
5475          */
5476         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5477
5478         /* Restart the T2-shutdown timer.  */
5479         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5480                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5481         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5482         return SCTP_DISPOSITION_CONSUME;
5483
5484 nomem:
5485         return SCTP_DISPOSITION_NOMEM;
5486 }
5487
5488 /*
5489  * ADDIP Section 4.1 ASCONF CHunk Procedures
5490  * If the T4 RTO timer expires the endpoint should do B1 to B5
5491  */
5492 sctp_disposition_t sctp_sf_t4_timer_expire(
5493         const struct sctp_endpoint *ep,
5494         const struct sctp_association *asoc,
5495         const sctp_subtype_t type,
5496         void *arg,
5497         sctp_cmd_seq_t *commands)
5498 {
5499         struct sctp_chunk *chunk = asoc->addip_last_asconf;
5500         struct sctp_transport *transport = chunk->transport;
5501
5502         SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5503
5504         /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5505          * detection on the appropriate destination address as defined in
5506          * RFC2960 [5] section 8.1 and 8.2.
5507          */
5508         if (transport)
5509                 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5510                                 SCTP_TRANSPORT(transport));
5511
5512         /* Reconfig T4 timer and transport. */
5513         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5514
5515         /* ADDIP 4.1 B2) Increment the association error counters and perform
5516          * endpoint failure detection on the association as defined in
5517          * RFC2960 [5] section 8.1 and 8.2.
5518          * association error counter is incremented in SCTP_CMD_STRIKE.
5519          */
5520         if (asoc->overall_error_count >= asoc->max_retrans) {
5521                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5522                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5523                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5524                                 SCTP_ERROR(ETIMEDOUT));
5525                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5526                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5527                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5528                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5529                 return SCTP_DISPOSITION_ABORT;
5530         }
5531
5532         /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5533          * the ASCONF chunk was sent by doubling the RTO timer value.
5534          * This is done in SCTP_CMD_STRIKE.
5535          */
5536
5537         /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5538          * choose an alternate destination address (please refer to RFC2960
5539          * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5540          * chunk, it MUST be the same (including its serial number) as the last
5541          * ASCONF sent.
5542          */
5543         sctp_chunk_hold(asoc->addip_last_asconf);
5544         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5545                         SCTP_CHUNK(asoc->addip_last_asconf));
5546
5547         /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5548          * destination is selected, then the RTO used will be that of the new
5549          * destination address.
5550          */
5551         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5552                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5553
5554         return SCTP_DISPOSITION_CONSUME;
5555 }
5556
5557 /* sctpimpguide-05 Section 2.12.2
5558  * The sender of the SHUTDOWN MAY also start an overall guard timer
5559  * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5560  * At the expiration of this timer the sender SHOULD abort the association
5561  * by sending an ABORT chunk.
5562  */
5563 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5564                                            const struct sctp_association *asoc,
5565                                            const sctp_subtype_t type,
5566                                            void *arg,
5567                                            sctp_cmd_seq_t *commands)
5568 {
5569         struct sctp_chunk *reply = NULL;
5570
5571         SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5572         SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5573
5574         reply = sctp_make_abort(asoc, NULL, 0);
5575         if (!reply)
5576                 goto nomem;
5577
5578         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5579         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5580                         SCTP_ERROR(ETIMEDOUT));
5581         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5582                         SCTP_PERR(SCTP_ERROR_NO_ERROR));
5583
5584         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5585         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5586
5587         return SCTP_DISPOSITION_DELETE_TCB;
5588 nomem:
5589         return SCTP_DISPOSITION_NOMEM;
5590 }
5591
5592 /* Handle expiration of AUTOCLOSE timer.  When the autoclose timer expires,
5593  * the association is automatically closed by starting the shutdown process.
5594  * The work that needs to be done is same as when SHUTDOWN is initiated by
5595  * the user.  So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5596  */
5597 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5598         const struct sctp_endpoint *ep,
5599         const struct sctp_association *asoc,
5600         const sctp_subtype_t type,
5601         void *arg,
5602         sctp_cmd_seq_t *commands)
5603 {
5604         int disposition;
5605
5606         SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5607
5608         /* From 9.2 Shutdown of an Association
5609          * Upon receipt of the SHUTDOWN primitive from its upper
5610          * layer, the endpoint enters SHUTDOWN-PENDING state and
5611          * remains there until all outstanding data has been
5612          * acknowledged by its peer. The endpoint accepts no new data
5613          * from its upper layer, but retransmits data to the far end
5614          * if necessary to fill gaps.
5615          */
5616         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5617                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5618
5619         disposition = SCTP_DISPOSITION_CONSUME;
5620         if (sctp_outq_is_empty(&asoc->outqueue)) {
5621                 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5622                                                             arg, commands);
5623         }
5624         return disposition;
5625 }
5626
5627 /*****************************************************************************
5628  * These are sa state functions which could apply to all types of events.
5629  ****************************************************************************/
5630
5631 /*
5632  * This table entry is not implemented.
5633  *
5634  * Inputs
5635  * (endpoint, asoc, chunk)
5636  *
5637  * The return value is the disposition of the chunk.
5638  */
5639 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5640                                     const struct sctp_association *asoc,
5641                                     const sctp_subtype_t type,
5642                                     void *arg,
5643                                     sctp_cmd_seq_t *commands)
5644 {
5645         return SCTP_DISPOSITION_NOT_IMPL;
5646 }
5647
5648 /*
5649  * This table entry represents a bug.
5650  *
5651  * Inputs
5652  * (endpoint, asoc, chunk)
5653  *
5654  * The return value is the disposition of the chunk.
5655  */
5656 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5657                                const struct sctp_association *asoc,
5658                                const sctp_subtype_t type,
5659                                void *arg,
5660                                sctp_cmd_seq_t *commands)
5661 {
5662         return SCTP_DISPOSITION_BUG;
5663 }
5664
5665 /*
5666  * This table entry represents the firing of a timer in the wrong state.
5667  * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5668  * when the association is in the wrong state.   This event should
5669  * be ignored, so as to prevent any rearming of the timer.
5670  *
5671  * Inputs
5672  * (endpoint, asoc, chunk)
5673  *
5674  * The return value is the disposition of the chunk.
5675  */
5676 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5677                                         const struct sctp_association *asoc,
5678                                         const sctp_subtype_t type,
5679                                         void *arg,
5680                                         sctp_cmd_seq_t *commands)
5681 {
5682         SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5683         return SCTP_DISPOSITION_CONSUME;
5684 }
5685
5686 /********************************************************************
5687  * 2nd Level Abstractions
5688  ********************************************************************/
5689
5690 /* Pull the SACK chunk based on the SACK header. */
5691 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5692 {
5693         struct sctp_sackhdr *sack;
5694         unsigned int len;
5695         __u16 num_blocks;
5696         __u16 num_dup_tsns;
5697
5698         /* Protect ourselves from reading too far into
5699          * the skb from a bogus sender.
5700          */
5701         sack = (struct sctp_sackhdr *) chunk->skb->data;
5702
5703         num_blocks = ntohs(sack->num_gap_ack_blocks);
5704         num_dup_tsns = ntohs(sack->num_dup_tsns);
5705         len = sizeof(struct sctp_sackhdr);
5706         len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5707         if (len > chunk->skb->len)
5708                 return NULL;
5709
5710         skb_pull(chunk->skb, len);
5711
5712         return sack;
5713 }
5714
5715 /* Create an ABORT packet to be sent as a response, with the specified
5716  * error causes.
5717  */
5718 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5719                                   const struct sctp_association *asoc,
5720                                   struct sctp_chunk *chunk,
5721                                   const void *payload,
5722                                   size_t paylen)
5723 {
5724         struct sctp_packet *packet;
5725         struct sctp_chunk *abort;
5726
5727         packet = sctp_ootb_pkt_new(asoc, chunk);
5728
5729         if (packet) {
5730                 /* Make an ABORT.
5731                  * The T bit will be set if the asoc is NULL.
5732                  */
5733                 abort = sctp_make_abort(asoc, chunk, paylen);
5734                 if (!abort) {
5735                         sctp_ootb_pkt_free(packet);
5736                         return NULL;
5737                 }
5738
5739                 /* Reflect vtag if T-Bit is set */
5740                 if (sctp_test_T_bit(abort))
5741                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5742
5743                 /* Add specified error causes, i.e., payload, to the
5744                  * end of the chunk.
5745                  */
5746                 sctp_addto_chunk(abort, paylen, payload);
5747
5748                 /* Set the skb to the belonging sock for accounting.  */
5749                 abort->skb->sk = ep->base.sk;
5750
5751                 sctp_packet_append_chunk(packet, abort);
5752
5753         }
5754
5755         return packet;
5756 }
5757
5758 /* Allocate a packet for responding in the OOTB conditions.  */
5759 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5760                                              const struct sctp_chunk *chunk)
5761 {
5762         struct sctp_packet *packet;
5763         struct sctp_transport *transport;
5764         __u16 sport;
5765         __u16 dport;
5766         __u32 vtag;
5767
5768         /* Get the source and destination port from the inbound packet.  */
5769         sport = ntohs(chunk->sctp_hdr->dest);
5770         dport = ntohs(chunk->sctp_hdr->source);
5771
5772         /* The V-tag is going to be the same as the inbound packet if no
5773          * association exists, otherwise, use the peer's vtag.
5774          */
5775         if (asoc) {
5776                 /* Special case the INIT-ACK as there is no peer's vtag
5777                  * yet.
5778                  */
5779                 switch(chunk->chunk_hdr->type) {
5780                 case SCTP_CID_INIT_ACK:
5781                 {
5782                         sctp_initack_chunk_t *initack;
5783
5784                         initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5785                         vtag = ntohl(initack->init_hdr.init_tag);
5786                         break;
5787                 }
5788                 default:
5789                         vtag = asoc->peer.i.init_tag;
5790                         break;
5791                 }
5792         } else {
5793                 /* Special case the INIT and stale COOKIE_ECHO as there is no
5794                  * vtag yet.
5795                  */
5796                 switch(chunk->chunk_hdr->type) {
5797                 case SCTP_CID_INIT:
5798                 {
5799                         sctp_init_chunk_t *init;
5800
5801                         init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5802                         vtag = ntohl(init->init_hdr.init_tag);
5803                         break;
5804                 }
5805                 default:
5806                         vtag = ntohl(chunk->sctp_hdr->vtag);
5807                         break;
5808                 }
5809         }
5810
5811         /* Make a transport for the bucket, Eliza... */
5812         transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5813         if (!transport)
5814                 goto nomem;
5815
5816         /* Cache a route for the transport with the chunk's destination as
5817          * the source address.
5818          */
5819         sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5820                              sctp_sk(sctp_get_ctl_sock()));
5821
5822         packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5823         packet = sctp_packet_config(packet, vtag, 0);
5824
5825         return packet;
5826
5827 nomem:
5828         return NULL;
5829 }
5830
5831 /* Free the packet allocated earlier for responding in the OOTB condition.  */
5832 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5833 {
5834         sctp_transport_free(packet->transport);
5835 }
5836
5837 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found  */
5838 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5839                                        const struct sctp_association *asoc,
5840                                        const struct sctp_chunk *chunk,
5841                                        sctp_cmd_seq_t *commands,
5842                                        struct sctp_chunk *err_chunk)
5843 {
5844         struct sctp_packet *packet;
5845
5846         if (err_chunk) {
5847                 packet = sctp_ootb_pkt_new(asoc, chunk);
5848                 if (packet) {
5849                         struct sctp_signed_cookie *cookie;
5850
5851                         /* Override the OOTB vtag from the cookie. */
5852                         cookie = chunk->subh.cookie_hdr;
5853                         packet->vtag = cookie->c.peer_vtag;
5854
5855                         /* Set the skb to the belonging sock for accounting. */
5856                         err_chunk->skb->sk = ep->base.sk;
5857                         sctp_packet_append_chunk(packet, err_chunk);
5858                         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5859                                         SCTP_PACKET(packet));
5860                         SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5861                 } else
5862                         sctp_chunk_free (err_chunk);
5863         }
5864 }
5865
5866
5867 /* Process a data chunk */
5868 static int sctp_eat_data(const struct sctp_association *asoc,
5869                          struct sctp_chunk *chunk,
5870                          sctp_cmd_seq_t *commands)
5871 {
5872         sctp_datahdr_t *data_hdr;
5873         struct sctp_chunk *err;
5874         size_t datalen;
5875         sctp_verb_t deliver;
5876         int tmp;
5877         __u32 tsn;
5878         struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5879         struct sock *sk = asoc->base.sk;
5880
5881         data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5882         skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5883
5884         tsn = ntohl(data_hdr->tsn);
5885         SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5886
5887         /* ASSERT:  Now skb->data is really the user data.  */
5888
5889         /* Process ECN based congestion.
5890          *
5891          * Since the chunk structure is reused for all chunks within
5892          * a packet, we use ecn_ce_done to track if we've already
5893          * done CE processing for this packet.
5894          *
5895          * We need to do ECN processing even if we plan to discard the
5896          * chunk later.
5897          */
5898
5899         if (!chunk->ecn_ce_done) {
5900                 struct sctp_af *af;
5901                 chunk->ecn_ce_done = 1;
5902
5903                 af = sctp_get_af_specific(
5904                         ipver2af(ip_hdr(chunk->skb)->version));
5905
5906                 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5907                         /* Do real work as sideffect. */
5908                         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5909                                         SCTP_U32(tsn));
5910                 }
5911         }
5912
5913         tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5914         if (tmp < 0) {
5915                 /* The TSN is too high--silently discard the chunk and
5916                  * count on it getting retransmitted later.
5917                  */
5918                 return SCTP_IERROR_HIGH_TSN;
5919         } else if (tmp > 0) {
5920                 /* This is a duplicate.  Record it.  */
5921                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5922                 return SCTP_IERROR_DUP_TSN;
5923         }
5924
5925         /* This is a new TSN.  */
5926
5927         /* Discard if there is no room in the receive window.
5928          * Actually, allow a little bit of overflow (up to a MTU).
5929          */
5930         datalen = ntohs(chunk->chunk_hdr->length);
5931         datalen -= sizeof(sctp_data_chunk_t);
5932
5933         deliver = SCTP_CMD_CHUNK_ULP;
5934
5935         /* Think about partial delivery. */
5936         if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5937
5938                 /* Even if we don't accept this chunk there is
5939                  * memory pressure.
5940                  */
5941                 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5942         }
5943
5944         /* Spill over rwnd a little bit.  Note: While allowed, this spill over
5945          * seems a bit troublesome in that frag_point varies based on
5946          * PMTU.  In cases, such as loopback, this might be a rather
5947          * large spill over.
5948          */
5949         if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5950             (datalen > asoc->rwnd + asoc->frag_point))) {
5951
5952                 /* If this is the next TSN, consider reneging to make
5953                  * room.   Note: Playing nice with a confused sender.  A
5954                  * malicious sender can still eat up all our buffer
5955                  * space and in the future we may want to detect and
5956                  * do more drastic reneging.
5957                  */
5958                 if (sctp_tsnmap_has_gap(map) &&
5959                     (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5960                         SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5961                         deliver = SCTP_CMD_RENEGE;
5962                 } else {
5963                         SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5964                                           "rwnd: %d\n", tsn, datalen,
5965                                           asoc->rwnd);
5966                         return SCTP_IERROR_IGNORE_TSN;
5967                 }
5968         }
5969
5970         /*
5971          * Also try to renege to limit our memory usage in the event that
5972          * we are under memory pressure
5973          * If we can't renege, don't worry about it, the sk_rmem_schedule
5974          * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5975          * memory usage too much
5976          */
5977         if (*sk->sk_prot_creator->memory_pressure) {
5978                 if (sctp_tsnmap_has_gap(map) &&
5979                    (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5980                         SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5981                         deliver = SCTP_CMD_RENEGE;
5982                  }
5983         }
5984
5985         /*
5986          * Section 3.3.10.9 No User Data (9)
5987          *
5988          * Cause of error
5989          * ---------------
5990          * No User Data:  This error cause is returned to the originator of a
5991          * DATA chunk if a received DATA chunk has no user data.
5992          */
5993         if (unlikely(0 == datalen)) {
5994                 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5995                 if (err) {
5996                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5997                                         SCTP_CHUNK(err));
5998                 }
5999                 /* We are going to ABORT, so we might as well stop
6000                  * processing the rest of the chunks in the packet.
6001                  */
6002                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6003                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6004                                 SCTP_ERROR(ECONNABORTED));
6005                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6006                                 SCTP_PERR(SCTP_ERROR_NO_DATA));
6007                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6008                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6009                 return SCTP_IERROR_NO_DATA;
6010         }
6011
6012         chunk->data_accepted = 1;
6013
6014         /* Note: Some chunks may get overcounted (if we drop) or overcounted
6015          * if we renege and the chunk arrives again.
6016          */
6017         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6018                 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6019         else
6020                 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6021
6022         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6023          *
6024          * If an endpoint receive a DATA chunk with an invalid stream
6025          * identifier, it shall acknowledge the reception of the DATA chunk
6026          * following the normal procedure, immediately send an ERROR chunk
6027          * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6028          * and discard the DATA chunk.
6029          */
6030         if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
6031                 /* Mark tsn as received even though we drop it */
6032                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6033
6034                 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6035                                          &data_hdr->stream,
6036                                          sizeof(data_hdr->stream));
6037                 if (err)
6038                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6039                                         SCTP_CHUNK(err));
6040                 return SCTP_IERROR_BAD_STREAM;
6041         }
6042
6043         /* Send the data up to the user.  Note:  Schedule  the
6044          * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6045          * chunk needs the updated rwnd.
6046          */
6047         sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6048
6049         return SCTP_IERROR_NO_ERROR;
6050 }