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