sctp/ipv6.c: use ipv6_addr_copy
[safe/jmp/linux-2.6] / net / sctp / sm_statefuns.c
index 32f57f4..3a0cd07 100644 (file)
@@ -1,23 +1,21 @@
-/* SCTP kernel reference Implementation
+/* SCTP kernel implementation
  * (C) Copyright IBM Corp. 2001, 2004
  * Copyright (c) 1999-2000 Cisco, Inc.
  * Copyright (c) 1999-2001 Motorola, Inc.
  * Copyright (c) 2001-2002 Intel Corp.
  * Copyright (c) 2002      Nokia Corp.
  *
- * This file is part of the SCTP kernel reference Implementation
- *
- * This is part of the SCTP Linux Kernel Reference Implementation.
+ * This is part of the SCTP Linux Kernel Implementation.
  *
  * These are the state functions for the state machine.
  *
- * The SCTP reference implementation is free software;
+ * This SCTP implementation is free software;
  * you can redistribute it and/or modify it under the terms of
  * the GNU General Public License as published by
  * the Free Software Foundation; either version 2, or (at your option)
  * any later version.
  *
- * The SCTP reference implementation is distributed in the hope that it
+ * This SCTP implementation is distributed in the hope that it
  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  *                 ************************
  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
@@ -90,13 +88,26 @@ static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
                                             const sctp_subtype_t type,
                                             void *arg,
                                             sctp_cmd_seq_t *commands);
+static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
+                                       const struct sctp_association *asoc,
+                                       const sctp_subtype_t type,
+                                       void *arg,
+                                       sctp_cmd_seq_t *commands);
 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
 
 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
-                                          __u16 error, int sk_err,
+                                          __be16 error, int sk_err,
                                           const struct sctp_association *asoc,
                                           struct sctp_transport *transport);
 
+static sctp_disposition_t sctp_sf_abort_violation(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands,
+                                    const __u8 *payload,
+                                    const size_t paylen);
+
 static sctp_disposition_t sctp_sf_violation_chunklen(
                                     const struct sctp_endpoint *ep,
                                     const struct sctp_association *asoc,
@@ -104,6 +115,38 @@ static sctp_disposition_t sctp_sf_violation_chunklen(
                                     void *arg,
                                     sctp_cmd_seq_t *commands);
 
+static sctp_disposition_t sctp_sf_violation_paramlen(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg, void *ext,
+                                    sctp_cmd_seq_t *commands);
+
+static sctp_disposition_t sctp_sf_violation_ctsn(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands);
+
+static sctp_disposition_t sctp_sf_violation_chunk(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands);
+
+static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
+                                   const struct sctp_association *asoc,
+                                   const sctp_subtype_t type,
+                                   struct sctp_chunk *chunk);
+
+static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
+                                       const struct sctp_association *asoc,
+                                       const sctp_subtype_t type,
+                                       void *arg,
+                                       sctp_cmd_seq_t *commands);
+
 /* Small helper function that checks if the chunk length
  * is of the appropriate length.  The 'required_length' argument
  * is set to be the size of a specific chunk we are testing.
@@ -167,16 +210,21 @@ sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
        struct sctp_chunk *chunk = arg;
        struct sctp_ulpevent *ev;
 
+       if (!sctp_vtag_verify_either(chunk, asoc))
+               return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+
        /* RFC 2960 6.10 Bundling
         *
         * An endpoint MUST NOT bundle INIT, INIT ACK or
         * SHUTDOWN COMPLETE with any other chunks.
         */
        if (!chunk->singleton)
-               return SCTP_DISPOSITION_VIOLATION;
+               return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
 
-       if (!sctp_vtag_verify_either(chunk, asoc))
-               return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+       /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
 
        /* RFC 2960 10.2 SCTP-to-ULP
         *
@@ -186,11 +234,10 @@ sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
         * notification is passed to the upper layer.
         */
        ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
-                                            0, 0, 0, GFP_ATOMIC);
-       if (!ev)
-               goto nomem;
-
-       sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
+                                            0, 0, 0, NULL, GFP_ATOMIC);
+       if (ev)
+               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
+                               SCTP_ULPEVENT(ev));
 
        /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
         * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
@@ -215,9 +262,6 @@ sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
 
        return SCTP_DISPOSITION_DELETE_TCB;
-
-nomem:
-       return SCTP_DISPOSITION_NOMEM;
 }
 
 /*
@@ -232,7 +276,7 @@ nomem:
  *    Verification Tag field to Tag_A, and also provide its own
  *    Verification Tag (Tag_Z) in the Initiate Tag field.
  *
- * Verification Tag: Must be 0. 
+ * Verification Tag: Must be 0.
  *
  * Inputs
  * (endpoint, asoc, chunk)
@@ -254,13 +298,12 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
        struct sctp_chunk *err_chunk;
        struct sctp_packet *packet;
        sctp_unrecognized_param_t *unk_param;
-       struct sock *sk;
        int len;
 
        /* 6.10 Bundling
         * An endpoint MUST NOT bundle INIT, INIT ACK or
         * SHUTDOWN COMPLETE with any other chunks.
-        * 
+        *
         * IG Section 2.11.2
         * Furthermore, we require that the receiver of an INIT chunk MUST
         * enforce these rules by silently discarding an arriving packet
@@ -272,21 +315,13 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
        /* If the packet is an OOTB packet which is temporarily on the
         * control endpoint, respond with an ABORT.
         */
-       if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
-               return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
-
-       sk = ep->base.sk;
-       /* If the endpoint is not listening or if the number of associations
-        * on the TCP-style socket exceed the max backlog, respond with an
-        * ABORT.
-        */
-       if (!sctp_sstate(sk, LISTENING) ||
-           (sctp_style(sk, TCP) &&
-            sk_acceptq_is_full(sk)))
+       if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
+               SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
                return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
+       }
 
        /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
-        * Tag. 
+        * Tag.
         */
        if (chunk->sctp_hdr->vtag != 0)
                return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
@@ -330,7 +365,7 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
                }
        }
 
-        /* Grab the INIT header.  */
+       /* Grab the INIT header.  */
        chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
 
        /* Tag the variable length parameters.  */
@@ -347,8 +382,6 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
                               GFP_ATOMIC))
                goto nomem_init;
 
-       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
-
        /* B) "Z" shall respond immediately with an INIT ACK chunk.  */
 
        /* If there are errors need to be reported for unknown parameters,
@@ -360,11 +393,11 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
                        sizeof(sctp_chunkhdr_t);
 
        if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
-               goto nomem_ack;
+               goto nomem_init;
 
        repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
        if (!repl)
-               goto nomem_ack;
+               goto nomem_init;
 
        /* If there are errors need to be reported for unknown parameters,
         * include them in the outgoing INIT ACK as "Unrecognized parameter"
@@ -388,6 +421,8 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
                sctp_chunk_free(err_chunk);
        }
 
+       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
+
        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
 
        /*
@@ -400,12 +435,11 @@ sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
 
        return SCTP_DISPOSITION_DELETE_TCB;
 
-nomem_ack:
-       if (err_chunk)
-               sctp_chunk_free(err_chunk);
 nomem_init:
        sctp_association_free(new_asoc);
 nomem:
+       if (err_chunk)
+               sctp_chunk_free(err_chunk);
        return SCTP_DISPOSITION_NOMEM;
 }
 
@@ -445,56 +479,38 @@ sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
 {
        struct sctp_chunk *chunk = arg;
        sctp_init_chunk_t *initchunk;
-       __u32 init_tag;
        struct sctp_chunk *err_chunk;
        struct sctp_packet *packet;
-       __u16 error;
 
        if (!sctp_vtag_verify(chunk, asoc))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
-       /* Make sure that the INIT-ACK chunk has a valid length */
-       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
-               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
-                                                 commands);
        /* 6.10 Bundling
         * An endpoint MUST NOT bundle INIT, INIT ACK or
         * SHUTDOWN COMPLETE with any other chunks.
         */
        if (!chunk->singleton)
-               return SCTP_DISPOSITION_VIOLATION;
+               return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
 
+       /* Make sure that the INIT-ACK chunk has a valid length */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
        /* Grab the INIT header.  */
        chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
 
-       init_tag = ntohl(chunk->subh.init_hdr->init_tag);
-
-       /* Verification Tag: 3.3.3
-        *   If the value of the Initiate Tag in a received INIT ACK
-        *   chunk is found to be 0, the receiver MUST treat it as an
-        *   error and close the association by transmitting an ABORT.
-        */
-       if (!init_tag) {
-               struct sctp_chunk *reply = sctp_make_abort(asoc, chunk, 0);
-               if (!reply)
-                       goto nomem;
-
-               sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
-               return sctp_stop_t1_and_abort(commands, SCTP_ERROR_INV_PARAM,
-                                             ECONNREFUSED, asoc,
-                                             chunk->transport);
-       }
-
        /* Verify the INIT chunk before processing it. */
        err_chunk = NULL;
        if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
                              (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
                              &err_chunk)) {
 
-               SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
+               sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
 
                /* This chunk contains fatal error. It is to be discarded.
-                * Send an ABORT, with causes if there is any.
+                * Send an ABORT, with causes.  If there are no causes,
+                * then there wasn't enough memory.  Just terminate
+                * the association.
                 */
                if (err_chunk) {
                        packet = sctp_abort_pkt_new(ep, asoc, arg,
@@ -510,13 +526,24 @@ sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
                                                SCTP_PACKET(packet));
                                SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
                                error = SCTP_ERROR_INV_PARAM;
-                       } else {
-                               error = SCTP_ERROR_NO_RESOURCE;
                        }
-               } else {
-                       sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
-                       error = SCTP_ERROR_INV_PARAM;
                }
+
+               /* SCTP-AUTH, Section 6.3:
+                *    It should be noted that if the receiver wants to tear
+                *    down an association in an authenticated way only, the
+                *    handling of malformed packets should not result in
+                *    tearing down the association.
+                *
+                * This means that if we only want to abort associations
+                * in an authenticated way (i.e AUTH+ABORT), then we
+                * can't destroy this association just becuase the packet
+                * was malformed.
+                */
+               if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+                       return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+
+               SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
                                                asoc, chunk->transport);
        }
@@ -545,6 +572,11 @@ sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
                        SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
 
+       /* SCTP-AUTH: genereate the assocition shared keys so that
+        * we can potentially signe the COOKIE-ECHO.
+        */
+       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
+
        /* 5.1 C) "A" shall then send the State Cookie received in the
         * INIT ACK chunk in a COOKIE ECHO chunk, ...
         */
@@ -555,9 +587,6 @@ sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
                        SCTP_CHUNK(err_chunk));
 
        return SCTP_DISPOSITION_CONSUME;
-
-nomem:
-       return SCTP_DISPOSITION_NOMEM;
 }
 
 /*
@@ -600,15 +629,18 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
        struct sctp_association *new_asoc;
        sctp_init_chunk_t *peer_init;
        struct sctp_chunk *repl;
-       struct sctp_ulpevent *ev;
+       struct sctp_ulpevent *ev, *ai_ev = NULL;
        int error = 0;
        struct sctp_chunk *err_chk_p;
+       struct sock *sk;
 
        /* If the packet is an OOTB packet which is temporarily on the
         * control endpoint, respond with an ABORT.
         */
-       if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
-               return sctp_sf_ootb(ep, asoc, type, arg, commands);
+       if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
+               SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
+               return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
+       }
 
        /* Make sure that the COOKIE_ECHO chunk has a valid length.
         * In this case, we check that we have enough for at least a
@@ -618,10 +650,19 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
+       /* If the endpoint is not listening or if the number of associations
+        * on the TCP-style socket exceed the max backlog, respond with an
+        * ABORT.
+        */
+       sk = ep->base.sk;
+       if (!sctp_sstate(sk, LISTENING) ||
+           (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
+               return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
+
        /* "Decode" the chunk.  We have no optional parameters so we
         * are in good shape.
         */
-        chunk->subh.cookie_hdr =
+       chunk->subh.cookie_hdr =
                (struct sctp_signed_cookie *)chunk->skb->data;
        if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
                                         sizeof(sctp_chunkhdr_t)))
@@ -656,23 +697,13 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
                case -SCTP_IERROR_BAD_SIG:
                default:
                        return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
-               };
+               }
        }
 
-       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
-       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
-                       SCTP_STATE(SCTP_STATE_ESTABLISHED));
-       SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
-       SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
-       sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
-
-       if (new_asoc->autoclose)
-               sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
-                               SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
-
-       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
 
-       /* Re-build the bind address for the association is done in
+       /* Delay state machine commands until later.
+        *
+        * Re-build the bind address for the association is done in
         * the sctp_unpack_cookie() already.
         */
        /* This is a brand-new association, so these are not yet side
@@ -685,11 +716,47 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
                               peer_init, GFP_ATOMIC))
                goto nomem_init;
 
+       /* SCTP-AUTH:  Now that we've populate required fields in
+        * sctp_process_init, set up the assocaition shared keys as
+        * necessary so that we can potentially authenticate the ACK
+        */
+       error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
+       if (error)
+               goto nomem_init;
+
+       /* SCTP-AUTH:  auth_chunk pointer is only set when the cookie-echo
+        * is supposed to be authenticated and we have to do delayed
+        * authentication.  We've just recreated the association using
+        * the information in the cookie and now it's much easier to
+        * do the authentication.
+        */
+       if (chunk->auth_chunk) {
+               struct sctp_chunk auth;
+               sctp_ierror_t ret;
+
+               /* set-up our fake chunk so that we can process it */
+               auth.skb = chunk->auth_chunk;
+               auth.asoc = chunk->asoc;
+               auth.sctp_hdr = chunk->sctp_hdr;
+               auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
+                                           sizeof(sctp_chunkhdr_t));
+               skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
+               auth.transport = chunk->transport;
+
+               ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
+
+               /* We can now safely free the auth_chunk clone */
+               kfree_skb(chunk->auth_chunk);
+
+               if (ret != SCTP_IERROR_NO_ERROR) {
+                       sctp_association_free(new_asoc);
+                       return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+               }
+       }
+
        repl = sctp_make_cookie_ack(new_asoc, chunk);
        if (!repl)
-               goto nomem_repl;
-
-       sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
+               goto nomem_init;
 
        /* RFC 2960 5.1 Normal Establishment of an Association
         *
@@ -700,32 +767,55 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
        ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
                                             new_asoc->c.sinit_num_ostreams,
                                             new_asoc->c.sinit_max_instreams,
-                                            GFP_ATOMIC);
+                                            NULL, GFP_ATOMIC);
        if (!ev)
                goto nomem_ev;
 
-       sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
-
-       /* Sockets API Draft Section 5.3.1.6    
-        * When a peer sends a Adaption Layer Indication parameter , SCTP
+       /* Sockets API Draft Section 5.3.1.6
+        * When a peer sends a Adaptation Layer Indication parameter , SCTP
         * delivers this notification to inform the application that of the
-        * peers requested adaption layer.
+        * peers requested adaptation layer.
         */
-       if (new_asoc->peer.adaption_ind) {
-               ev = sctp_ulpevent_make_adaption_indication(new_asoc,
+       if (new_asoc->peer.adaptation_ind) {
+               ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
                                                            GFP_ATOMIC);
-               if (!ev)
-                       goto nomem_ev;
+               if (!ai_ev)
+                       goto nomem_aiev;
+       }
+
+       /* Add all the state machine commands now since we've created
+        * everything.  This way we don't introduce memory corruptions
+        * during side-effect processing and correclty count established
+        * associations.
+        */
+       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
+       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
+                       SCTP_STATE(SCTP_STATE_ESTABLISHED));
+       SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
+       SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
+       sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
+
+       if (new_asoc->autoclose)
+               sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
+                               SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
+
+       /* This will send the COOKIE ACK */
+       sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
+
+       /* Queue the ASSOC_CHANGE event */
+       sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
 
+       /* Send up the Adaptation Layer Indication event */
+       if (ai_ev)
                sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
-                               SCTP_ULPEVENT(ev));
-       }
+                               SCTP_ULPEVENT(ai_ev));
 
        return SCTP_DISPOSITION_CONSUME;
 
+nomem_aiev:
+       sctp_ulpevent_free(ev);
 nomem_ev:
        sctp_chunk_free(repl);
-nomem_repl:
 nomem_init:
        sctp_association_free(new_asoc);
 nomem:
@@ -795,7 +885,6 @@ sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
        if (asoc->autoclose)
                sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
                                SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
-       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
 
        /* It may also notify its ULP about the successful
         * establishment of the association with a Communication Up
@@ -804,7 +893,7 @@ sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
        ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
                                             0, asoc->c.sinit_num_ostreams,
                                             asoc->c.sinit_max_instreams,
-                                            GFP_ATOMIC);
+                                            NULL, GFP_ATOMIC);
 
        if (!ev)
                goto nomem;
@@ -812,12 +901,12 @@ sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
 
        /* Sockets API Draft Section 5.3.1.6
-        * When a peer sends a Adaption Layer Indication parameter , SCTP
+        * When a peer sends a Adaptation Layer Indication parameter , SCTP
         * delivers this notification to inform the application that of the
-        * peers requested adaption layer.
+        * peers requested adaptation layer.
         */
-       if (asoc->peer.adaption_ind) {
-               ev = sctp_ulpevent_make_adaption_indication(asoc, GFP_ATOMIC);
+       if (asoc->peer.adaptation_ind) {
+               ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
                if (!ev)
                        goto nomem;
 
@@ -873,12 +962,12 @@ sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
 {
        struct sctp_transport *transport = (struct sctp_transport *) arg;
 
-       if (asoc->overall_error_count >= asoc->max_retrans) {
+       if (asoc->overall_error_count > asoc->max_retrans) {
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_DELETE_TCB;
@@ -905,7 +994,7 @@ sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
                        SCTP_TRANSPORT(transport));
 
-        return SCTP_DISPOSITION_CONSUME;
+       return SCTP_DISPOSITION_CONSUME;
 }
 
 /*
@@ -1032,19 +1121,19 @@ sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
        /* This should never happen, but lets log it if so.  */
        if (unlikely(!link)) {
                if (from_addr.sa.sa_family == AF_INET6) {
-                       printk(KERN_WARNING
-                              "%s association %p could not find address "
-                              NIP6_FMT "\n",
-                              __FUNCTION__,
-                              asoc,
-                              NIP6(from_addr.v6.sin6_addr));
+                       if (net_ratelimit())
+                               printk(KERN_WARNING
+                                   "%s association %p could not find address %pI6\n",
+                                   __func__,
+                                   asoc,
+                                   &from_addr.v6.sin6_addr);
                } else {
-                       printk(KERN_WARNING
-                              "%s association %p could not find address "
-                              NIPQUAD_FMT "\n",
-                              __FUNCTION__,
-                              asoc,
-                              NIPQUAD(from_addr.v4.sin_addr.s_addr));
+                       if (net_ratelimit())
+                               printk(KERN_WARNING
+                                   "%s association %p could not find address %pI4\n",
+                                   __func__,
+                                   asoc,
+                                   &from_addr.v4.sin_addr.s_addr);
                }
                return SCTP_DISPOSITION_DISCARD;
        }
@@ -1058,9 +1147,9 @@ sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
        /* Check if the timestamp looks valid.  */
        if (time_after(hbinfo->sent_at, jiffies) ||
            time_after(jiffies, hbinfo->sent_at + max_interval)) {
-               SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp"
+               SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
                                  "received for transport: %p\n",
-                                  __FUNCTION__, link);
+                                  __func__, link);
                return SCTP_DISPOSITION_DISCARD;
        }
 
@@ -1136,7 +1225,6 @@ static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
                                       sctp_cmd_seq_t *commands)
 {
        struct sctp_transport *new_addr, *addr;
-       struct list_head *pos, *pos2;
        int found;
 
        /* Implementor's Guide - Sectin 5.2.2
@@ -1153,12 +1241,11 @@ static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
        new_addr = NULL;
        found = 0;
 
-       list_for_each(pos, &new_asoc->peer.transport_addr_list) {
-               new_addr = list_entry(pos, struct sctp_transport, transports);
+       list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
+                       transports) {
                found = 0;
-               list_for_each(pos2, &asoc->peer.transport_addr_list) {
-                       addr = list_entry(pos2, struct sctp_transport,
-                                         transports);
+               list_for_each_entry(addr, &asoc->peer.transport_addr_list,
+                               transports) {
                        if (sctp_cmp_addr_exact(&new_addr->ipaddr,
                                                &addr->ipaddr)) {
                                found = 1;
@@ -1209,7 +1296,7 @@ static void sctp_tietags_populate(struct sctp_association *new_asoc,
                new_asoc->c.my_ttag   = asoc->c.my_vtag;
                new_asoc->c.peer_ttag = asoc->c.peer_vtag;
                break;
-       };
+       }
 
        /* Other parameters for the endpoint SHOULD be copied from the
         * existing parameters of the association (e.g. number of
@@ -1294,7 +1381,7 @@ static sctp_disposition_t sctp_sf_do_unexpected_init(
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
        /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
-        * Tag. 
+        * Tag.
         */
        if (chunk->sctp_hdr->vtag != 0)
                return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
@@ -1360,10 +1447,8 @@ static sctp_disposition_t sctp_sf_do_unexpected_init(
        if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
                               sctp_source(chunk),
                               (sctp_init_chunk_t *)chunk->chunk_hdr,
-                              GFP_ATOMIC)) {
-               retval = SCTP_DISPOSITION_NOMEM;
-               goto nomem_init;
-       }
+                              GFP_ATOMIC))
+               goto nomem;
 
        /* Make sure no new addresses are being added during the
         * restart.   Do not do this check for COOKIE-WAIT state,
@@ -1374,7 +1459,7 @@ static sctp_disposition_t sctp_sf_do_unexpected_init(
                if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
                                                 commands)) {
                        retval = SCTP_DISPOSITION_CONSUME;
-                       goto cleanup_asoc;
+                       goto nomem_retval;
                }
        }
 
@@ -1430,17 +1515,17 @@ static sctp_disposition_t sctp_sf_do_unexpected_init(
        sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
        retval = SCTP_DISPOSITION_CONSUME;
 
+       return retval;
+
+nomem:
+       retval = SCTP_DISPOSITION_NOMEM;
+nomem_retval:
+       if (new_asoc)
+               sctp_association_free(new_asoc);
 cleanup:
        if (err_chunk)
                sctp_chunk_free(err_chunk);
        return retval;
-nomem:
-       retval = SCTP_DISPOSITION_NOMEM;
-       goto cleanup;
-nomem_init:
-cleanup_asoc:
-       sctp_association_free(new_asoc);
-       goto cleanup;
 }
 
 /*
@@ -1547,6 +1632,28 @@ sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
 }
 
 
+/*
+ * Unexpected INIT-ACK handler.
+ *
+ * Section 5.2.3
+ * If an INIT ACK received by an endpoint in any state other than the
+ * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
+ * An unexpected INIT ACK usually indicates the processing of an old or
+ * duplicated INIT chunk.
+*/
+sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
+                                           const struct sctp_association *asoc,
+                                           const sctp_subtype_t type,
+                                           void *arg, sctp_cmd_seq_t *commands)
+{
+       /* Per the above section, we'll discard the chunk if we have an
+        * endpoint.  If this is an OOTB INIT-ACK, treat it as such.
+        */
+       if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
+               return sctp_sf_ootb(ep, asoc, type, arg, commands);
+       else
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
+}
 
 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
  *
@@ -1611,23 +1718,21 @@ static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
 
-       /* Update the content of current association. */
-       sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
-
        repl = sctp_make_cookie_ack(new_asoc, chunk);
        if (!repl)
                goto nomem;
 
-       sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
-
        /* Report association restart to upper layer. */
        ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
                                             new_asoc->c.sinit_num_ostreams,
                                             new_asoc->c.sinit_max_instreams,
-                                            GFP_ATOMIC);
+                                            NULL, GFP_ATOMIC);
        if (!ev)
                goto nomem_ev;
 
+       /* Update the content of current association. */
+       sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
+       sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
        sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
        return SCTP_DISPOSITION_CONSUME;
 
@@ -1652,7 +1757,6 @@ static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
                                        struct sctp_association *new_asoc)
 {
        sctp_init_chunk_t *peer_init;
-       struct sctp_ulpevent *ev;
        struct sctp_chunk *repl;
 
        /* new_asoc is a brand-new association, so these are not yet
@@ -1676,41 +1780,34 @@ static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
                goto nomem;
 
        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
-       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
 
        /* RFC 2960 5.1 Normal Establishment of an Association
         *
         * D) IMPLEMENTATION NOTE: An implementation may choose to
         * send the Communication Up notification to the SCTP user
         * upon reception of a valid COOKIE ECHO chunk.
+        *
+        * Sadly, this needs to be implemented as a side-effect, because
+        * we are not guaranteed to have set the association id of the real
+        * association and so these notifications need to be delayed until
+        * the association id is allocated.
         */
-       ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP, 0,
-                                            new_asoc->c.sinit_num_ostreams,
-                                            new_asoc->c.sinit_max_instreams,
-                                            GFP_ATOMIC);
-       if (!ev)
-               goto nomem_ev;
 
-       sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
+       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
 
        /* Sockets API Draft Section 5.3.1.6
-        * When a peer sends a Adaption Layer Indication parameter , SCTP
+        * When a peer sends a Adaptation Layer Indication parameter , SCTP
         * delivers this notification to inform the application that of the
-        * peers requested adaption layer.
+        * peers requested adaptation layer.
+        *
+        * This also needs to be done as a side effect for the same reason as
+        * above.
         */
-       if (asoc->peer.adaption_ind) {
-               ev = sctp_ulpevent_make_adaption_indication(asoc, GFP_ATOMIC);
-               if (!ev)
-                       goto nomem_ev;
-
-               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
-                               SCTP_ULPEVENT(ev));
-       }
+       if (asoc->peer.adaptation_ind)
+               sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
 
        return SCTP_DISPOSITION_CONSUME;
 
-nomem_ev:
-       sctp_chunk_free(repl);
 nomem:
        return SCTP_DISPOSITION_NOMEM;
 }
@@ -1751,14 +1848,14 @@ static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
                                        sctp_cmd_seq_t *commands,
                                        struct sctp_association *new_asoc)
 {
-       struct sctp_ulpevent *ev = NULL;
+       struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
        struct sctp_chunk *repl;
 
        /* Clarification from Implementor's Guide:
         * D) When both local and remote tags match the endpoint should
-         * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
-         * It should stop any cookie timer that may be running and send
-         * a COOKIE ACK.
+        * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
+        * It should stop any cookie timer that may be running and send
+        * a COOKIE ACK.
         */
 
        /* Don't accidentally move back into established state. */
@@ -1778,43 +1875,46 @@ static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
                 * SCTP user upon reception of a valid COOKIE
                 * ECHO chunk.
                 */
-               ev = sctp_ulpevent_make_assoc_change(new_asoc, 0,
+               ev = sctp_ulpevent_make_assoc_change(asoc, 0,
                                             SCTP_COMM_UP, 0,
-                                            new_asoc->c.sinit_num_ostreams,
-                                            new_asoc->c.sinit_max_instreams,
-                                             GFP_ATOMIC);
+                                            asoc->c.sinit_num_ostreams,
+                                            asoc->c.sinit_max_instreams,
+                                            NULL, GFP_ATOMIC);
                if (!ev)
                        goto nomem;
-               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
-                               SCTP_ULPEVENT(ev));
 
                /* Sockets API Draft Section 5.3.1.6
-                * When a peer sends a Adaption Layer Indication parameter,
+                * When a peer sends a Adaptation Layer Indication parameter,
                 * SCTP delivers this notification to inform the application
-                * that of the peers requested adaption layer.
+                * that of the peers requested adaptation layer.
                 */
-               if (new_asoc->peer.adaption_ind) {
-                       ev = sctp_ulpevent_make_adaption_indication(new_asoc,
+               if (asoc->peer.adaptation_ind) {
+                       ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
                                                                 GFP_ATOMIC);
-                       if (!ev)
+                       if (!ai_ev)
                                goto nomem;
 
-                       sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
-                                       SCTP_ULPEVENT(ev));
                }
        }
-       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
 
        repl = sctp_make_cookie_ack(new_asoc, chunk);
        if (!repl)
                goto nomem;
 
        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
-       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
+
+       if (ev)
+               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
+                               SCTP_ULPEVENT(ev));
+       if (ai_ev)
+               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
+                                       SCTP_ULPEVENT(ai_ev));
 
        return SCTP_DISPOSITION_CONSUME;
 
 nomem:
+       if (ai_ev)
+               sctp_ulpevent_free(ai_ev);
        if (ev)
                sctp_ulpevent_free(ev);
        return SCTP_DISPOSITION_NOMEM;
@@ -1861,7 +1961,7 @@ sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
        /* "Decode" the chunk.  We have no optional parameters so we
         * are in good shape.
         */
-        chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
+       chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
        if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
                                        sizeof(sctp_chunkhdr_t)))
                goto nomem;
@@ -1895,7 +1995,7 @@ sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
                case -SCTP_IERROR_BAD_SIG:
                default:
                        return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
-               };
+               }
        }
 
        /* Compare the tie_tag in cookie with the verification tag of
@@ -1927,7 +2027,7 @@ sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
        default: /* Discard packet for all others. */
                retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
                break;
-        };
+       }
 
        /* Delete the tempory new association. */
        sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
@@ -1969,11 +2069,16 @@ sctp_disposition_t sctp_sf_shutdown_pending_abort(
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
-       /* Stop the T5-shutdown guard timer.  */
-       sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
-                       SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
+       /* ADD-IP: Special case for ABORT chunks
+        * F4)  One special consideration is that ABORT Chunks arriving
+        * destined to the IP address being deleted MUST be
+        * ignored (see Section 5.3.1 for further details).
+        */
+       if (SCTP_ADDR_DEL ==
+                   sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
 
-       return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
+       return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
 }
 
 /*
@@ -2005,6 +2110,15 @@ sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
+       /* ADD-IP: Special case for ABORT chunks
+        * F4)  One special consideration is that ABORT Chunks arriving
+        * destined to the IP address being deleted MUST be
+        * ignored (see Section 5.3.1 for further details).
+        */
+       if (SCTP_ADDR_DEL ==
+                   sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
+
        /* Stop the T2-shutdown timer. */
        sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
                        SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
@@ -2013,7 +2127,7 @@ sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
                        SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
 
-       return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
+       return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
 }
 
 /*
@@ -2074,7 +2188,7 @@ sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
         */
        sctp_walk_errors(err, chunk->chunk_hdr) {
                if (SCTP_ERROR_STALE_COOKIE == err->cause)
-                       return sctp_sf_do_5_2_6_stale(ep, asoc, type, 
+                       return sctp_sf_do_5_2_6_stale(ep, asoc, type,
                                                        arg, commands);
        }
 
@@ -2129,7 +2243,7 @@ static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                               SCTP_U32(SCTP_ERROR_STALE_COOKIE));
+                               SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
                return SCTP_DISPOSITION_DELETE_TCB;
        }
 
@@ -2149,7 +2263,7 @@ static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
         * to give ample time to retransmit the new cookie and thus
         * yield a higher probability of success on the reattempt.
         */
-       stale = ntohl(*(suseconds_t *)((u8 *)err + sizeof(sctp_errhdr_t)));
+       stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
        stale = (stale * 2) / 1000;
 
        bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
@@ -2176,10 +2290,10 @@ static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
 
-       /* If we've sent any data bundled with COOKIE-ECHO we will need to 
-        * resend 
+       /* If we've sent any data bundled with COOKIE-ECHO we will need to
+        * resend
         */
-       sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, 
+       sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
                        SCTP_TRANSPORT(asoc->peer.primary_path));
 
        /* Cast away the const modifier, as we want to just
@@ -2240,8 +2354,6 @@ sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
                                        sctp_cmd_seq_t *commands)
 {
        struct sctp_chunk *chunk = arg;
-       unsigned len;
-       __u16 error = SCTP_ERROR_NO_ERROR;
 
        if (!sctp_vtag_verify_either(chunk, asoc))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
@@ -2259,14 +2371,36 @@ sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
+       /* ADD-IP: Special case for ABORT chunks
+        * F4)  One special consideration is that ABORT Chunks arriving
+        * destined to the IP address being deleted MUST be
+        * ignored (see Section 5.3.1 for further details).
+        */
+       if (SCTP_ADDR_DEL ==
+                   sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
+
+       return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
+}
+
+static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
+                                       const struct sctp_association *asoc,
+                                       const sctp_subtype_t type,
+                                       void *arg,
+                                       sctp_cmd_seq_t *commands)
+{
+       struct sctp_chunk *chunk = arg;
+       unsigned len;
+       __be16 error = SCTP_ERROR_NO_ERROR;
+
        /* See if we have an error cause code in the chunk.  */
        len = ntohs(chunk->chunk_hdr->length);
        if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
                error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
 
        sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
-       /* ASSOC_FAILED will DELETE_TCB. */
-       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_U32(error));
+       /* ASSOC_FAILED will DELETE_TCB. */
+       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
        SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
        SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
 
@@ -2286,7 +2420,7 @@ sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
 {
        struct sctp_chunk *chunk = arg;
        unsigned len;
-       __u16 error = SCTP_ERROR_NO_ERROR;
+       __be16 error = SCTP_ERROR_NO_ERROR;
 
        if (!sctp_vtag_verify_either(chunk, asoc))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
@@ -2348,7 +2482,7 @@ sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
  * This is common code called by several sctp_sf_*_abort() functions above.
  */
 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
-                                          __u16 error, int sk_err,
+                                          __be16 error, int sk_err,
                                           const struct sctp_association *asoc,
                                           struct sctp_transport *transport)
 {
@@ -2361,7 +2495,7 @@ static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
        sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
        /* CMD_INIT_FAILED will DELETE_TCB. */
        sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                       SCTP_U32(error));
+                       SCTP_PERR(error));
        return SCTP_DISPOSITION_ABORT;
 }
 
@@ -2408,6 +2542,7 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
        sctp_shutdownhdr_t *sdh;
        sctp_disposition_t disposition;
        struct sctp_ulpevent *ev;
+       __u32 ctsn;
 
        if (!sctp_vtag_verify(chunk, asoc))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
@@ -2422,6 +2557,14 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
        sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
        skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
        chunk->subh.shutdown_hdr = sdh;
+       ctsn = ntohl(sdh->cum_tsn_ack);
+
+       /* If Cumulative TSN Ack beyond the max tsn currently
+        * send, terminating the association and respond to the
+        * sender with an ABORT.
+        */
+       if (!TSN_lt(ctsn, asoc->next_tsn))
+               return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
 
        /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
         * When a peer sends a SHUTDOWN, SCTP delivers this notification to
@@ -2430,7 +2573,7 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
        ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
        if (!ev) {
                disposition = SCTP_DISPOSITION_NOMEM;
-               goto out;       
+               goto out;
        }
        sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
 
@@ -2457,17 +2600,62 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
         *    received by the SHUTDOWN sender.
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
-                       SCTP_U32(chunk->subh.shutdown_hdr->cum_tsn_ack));
+                       SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
 
 out:
        return disposition;
 }
 
-/* RFC 2960 9.2
- * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
- * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
- * transport addresses (either in the IP addresses or in the INIT chunk)
- * that belong to this association, it should discard the INIT chunk and
+/*
+ * sctp_sf_do_9_2_shut_ctsn
+ *
+ * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
+ * it MUST NOT send a SHUTDOWN in response to a ULP request.
+ * The Cumulative TSN Ack of the received SHUTDOWN chunk
+ * MUST be processed.
+ */
+sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
+                                          const struct sctp_association *asoc,
+                                          const sctp_subtype_t type,
+                                          void *arg,
+                                          sctp_cmd_seq_t *commands)
+{
+       struct sctp_chunk *chunk = arg;
+       sctp_shutdownhdr_t *sdh;
+
+       if (!sctp_vtag_verify(chunk, asoc))
+               return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+
+       /* Make sure that the SHUTDOWN chunk has a valid length. */
+       if (!sctp_chunk_length_valid(chunk,
+                                     sizeof(struct sctp_shutdown_chunk_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
+       sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
+
+       /* If Cumulative TSN Ack beyond the max tsn currently
+        * send, terminating the association and respond to the
+        * sender with an ABORT.
+        */
+       if (!TSN_lt(ntohl(sdh->cum_tsn_ack), asoc->next_tsn))
+               return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
+
+       /* verify, by checking the Cumulative TSN Ack field of the
+        * chunk, that all its outstanding DATA chunks have been
+        * received by the SHUTDOWN sender.
+        */
+       sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
+                       SCTP_BE32(sdh->cum_tsn_ack));
+
+       return SCTP_DISPOSITION_CONSUME;
+}
+
+/* RFC 2960 9.2
+ * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
+ * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
+ * transport addresses (either in the IP addresses or in the INIT chunk)
+ * that belong to this association, it should discard the INIT chunk and
  * retransmit the SHUTDOWN ACK chunk.
  */
 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
@@ -2479,6 +2667,11 @@ sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
        struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
        struct sctp_chunk *reply;
 
+       /* Make sure that the chunk has a valid length */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
        /* Since we are not going to really process this INIT, there
         * is no point in verifying chunk boundries.  Just generate
         * the SHUTDOWN ACK.
@@ -2536,6 +2729,7 @@ sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
 {
        sctp_cwrhdr_t *cwr;
        struct sctp_chunk *chunk = arg;
+       u32 lowest_tsn;
 
        if (!sctp_vtag_verify(chunk, asoc))
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
@@ -2543,18 +2737,18 @@ sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
                return sctp_sf_violation_chunklen(ep, asoc, type, arg,
                                                  commands);
-               
+
        cwr = (sctp_cwrhdr_t *) chunk->skb->data;
        skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
 
-       cwr->lowest_tsn = ntohl(cwr->lowest_tsn);
+       lowest_tsn = ntohl(cwr->lowest_tsn);
 
        /* Does this CWR ack the last sent congestion notification? */
-       if (TSN_lte(asoc->last_ecne_tsn, cwr->lowest_tsn)) {
+       if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
                /* Stop sending ECNE. */
                sctp_add_cmd_sf(commands,
                                SCTP_CMD_ECN_CWR,
-                               SCTP_U32(cwr->lowest_tsn));
+                               SCTP_U32(lowest_tsn));
        }
        return SCTP_DISPOSITION_CONSUME;
 }
@@ -2651,7 +2845,7 @@ sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
                sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
                                SCTP_NULL());
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
-        }
+       }
 
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
                return sctp_sf_violation_chunklen(ep, asoc, type, arg,
@@ -2733,7 +2927,7 @@ discard_noforce:
        return SCTP_DISPOSITION_DISCARD;
 consume:
        return SCTP_DISPOSITION_CONSUME;
-       
+
 }
 
 /*
@@ -2877,6 +3071,13 @@ sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
                return SCTP_DISPOSITION_DISCARD;
        }
 
+       /* If Cumulative TSN Ack beyond the max tsn currently
+        * send, terminating the association and respond to the
+        * sender with an ABORT.
+        */
+       if (!TSN_lt(ctsn, asoc->next_tsn))
+               return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
+
        /* Return this SACK for further processing.  */
        sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
 
@@ -2904,7 +3105,7 @@ sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
  *
  * The return value is the disposition of the chunk.
 */
-sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
+static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
                                        const struct sctp_association *asoc,
                                        const sctp_subtype_t type,
                                        void *arg,
@@ -2920,7 +3121,7 @@ sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
                /* Make an ABORT. The T bit will be set if the asoc
                 * is NULL.
                 */
-               abort = sctp_make_abort(asoc, chunk, 0);
+               abort = sctp_make_abort(asoc, chunk, 0);
                if (!abort) {
                        sctp_ootb_pkt_free(packet);
                        return SCTP_DISPOSITION_NOMEM;
@@ -2940,6 +3141,7 @@ sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
 
                SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
 
+               sctp_sf_pdiscard(ep, asoc, type, arg, commands);
                return SCTP_DISPOSITION_CONSUME;
        }
 
@@ -2977,14 +3179,10 @@ sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
                if (!ev)
                        goto nomem;
 
-               if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
-                                 SCTP_ULPEVENT(ev))) {
-                       sctp_ulpevent_free(ev);
-                       goto nomem;
-               }
-
+               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
+                               SCTP_ULPEVENT(ev));
                sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
-                               SCTP_CHUNK(chunk));     
+                               SCTP_CHUNK(chunk));
        }
        return SCTP_DISPOSITION_CONSUME;
 
@@ -3019,17 +3217,24 @@ sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
                return sctp_sf_violation_chunklen(ep, asoc, type, arg,
                                                  commands);
-
        /* 10.2 H) SHUTDOWN COMPLETE notification
         *
         * When SCTP completes the shutdown procedures (section 9.2) this
         * notification is passed to the upper layer.
         */
        ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
-                                            0, 0, 0, GFP_ATOMIC);
+                                            0, 0, 0, NULL, GFP_ATOMIC);
        if (!ev)
                goto nomem;
 
+       /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
+       reply = sctp_make_shutdown_complete(asoc, chunk);
+       if (!reply)
+               goto nomem_chunk;
+
+       /* Do all the commands now (after allocation), so that we
+        * have consistent state if memory allocation failes
+        */
        sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
 
        /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
@@ -3041,11 +3246,6 @@ sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
                        SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
 
-       /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
-       reply = sctp_make_shutdown_complete(asoc, chunk);
-       if (!reply)
-               goto nomem;
-
        sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
                        SCTP_STATE(SCTP_STATE_CLOSED));
        SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
@@ -3056,6 +3256,8 @@ sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
        return SCTP_DISPOSITION_DELETE_TCB;
 
+nomem_chunk:
+       sctp_ulpevent_free(ev);
 nomem:
        return SCTP_DISPOSITION_NOMEM;
 }
@@ -3096,14 +3298,14 @@ sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
 
        ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
        do {
-               /* Break out if chunk length is less then minimal. */
+               /* Report violation if the chunk is less then minimal */
                if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
-                       break;
-
-               ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
-               if (ch_end > skb->tail)
-                       break;
+                       return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
 
+               /* Now that we know we at least have a chunk header,
+                * do things that are type appropriate.
+                */
                if (SCTP_CID_SHUTDOWN_ACK == ch->type)
                        ootb_shut_ack = 1;
 
@@ -3114,16 +3316,20 @@ sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
                 */
                if (SCTP_CID_ABORT == ch->type)
                        return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
-                       
+
+               /* Report violation if chunk len overflows */
+               ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
+               if (ch_end > skb_tail_pointer(skb))
+                       return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
                ch = (sctp_chunkhdr_t *) ch_end;
-       } while (ch_end < skb->tail);
+       } while (ch_end < skb_tail_pointer(skb));
 
        if (ootb_shut_ack)
-               sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
+               return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
        else
-               sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
-
-       return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+               return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
 }
 
 /*
@@ -3161,8 +3367,8 @@ static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
 
        if (packet) {
                /* Make an SHUTDOWN_COMPLETE.
-                * The T bit will be set if the asoc is NULL.
-                */
+                * The T bit will be set if the asoc is NULL.
+                */
                shut = sctp_make_shutdown_complete(asoc, chunk);
                if (!shut) {
                        sctp_ootb_pkt_free(packet);
@@ -3189,7 +3395,11 @@ static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
                if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
                        return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
 
-               return SCTP_DISPOSITION_CONSUME;
+               /* We need to discard the rest of the packet to prevent
+                * potential bomming attacks from additional bundled chunks.
+                * This is documented in SCTP Threats ID.
+                */
+               return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
        }
 
        return SCTP_DISPOSITION_NOMEM;
@@ -3212,11 +3422,20 @@ sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
                                      void *arg,
                                      sctp_cmd_seq_t *commands)
 {
+       struct sctp_chunk *chunk = arg;
+
+       /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
        /* Although we do have an association in this case, it corresponds
         * to a restarted association. So the packet is treated as an OOTB
         * packet and the state function that handles OOTB SHUTDOWN_ACK is
         * called with a NULL association.
         */
+       SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
+
        return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
 }
 
@@ -3228,8 +3447,11 @@ sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
 {
        struct sctp_chunk       *chunk = arg;
        struct sctp_chunk       *asconf_ack = NULL;
+       struct sctp_paramhdr    *err_param = NULL;
        sctp_addiphdr_t         *hdr;
+       union sctp_addr_param   *addr_param;
        __u32                   serial;
+       int                     length;
 
        if (!sctp_vtag_verify(chunk, asoc)) {
                sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
@@ -3237,6 +3459,15 @@ sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
        }
 
+       /* ADD-IP: Section 4.1.1
+        * This chunk MUST be sent in an authenticated way by using
+        * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
+        * is received unauthenticated it MUST be silently discarded as
+        * described in [I-D.ietf-tsvwg-sctp-auth].
+        */
+       if (!sctp_addip_noauth && !chunk->auth)
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
+
        /* Make sure that the ASCONF ADDIP chunk has a valid length.  */
        if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
                return sctp_sf_violation_chunklen(ep, asoc, type, arg,
@@ -3245,44 +3476,78 @@ sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
        hdr = (sctp_addiphdr_t *)chunk->skb->data;
        serial = ntohl(hdr->serial);
 
-       /* ADDIP 4.2 C1) Compare the value of the serial number to the value
+       addr_param = (union sctp_addr_param *)hdr->params;
+       length = ntohs(addr_param->p.length);
+       if (length < sizeof(sctp_paramhdr_t))
+               return sctp_sf_violation_paramlen(ep, asoc, type, arg,
+                          (void *)addr_param, commands);
+
+       /* Verify the ASCONF chunk before processing it. */
+       if (!sctp_verify_asconf(asoc,
+                           (sctp_paramhdr_t *)((void *)addr_param + length),
+                           (void *)chunk->chunk_end,
+                           &err_param))
+               return sctp_sf_violation_paramlen(ep, asoc, type, arg,
+                                                 (void *)err_param, commands);
+
+       /* ADDIP 5.2 E1) Compare the value of the serial number to the value
         * the endpoint stored in a new association variable
-        * 'Peer-Serial-Number'. 
+        * 'Peer-Serial-Number'.
         */
        if (serial == asoc->peer.addip_serial + 1) {
-               /* ADDIP 4.2 C2) If the value found in the serial number is
-                * equal to the ('Peer-Serial-Number' + 1), the endpoint MUST
-                * do V1-V5.
+               /* If this is the first instance of ASCONF in the packet,
+                * we can clean our old ASCONF-ACKs.
+                */
+               if (!chunk->has_asconf)
+                       sctp_assoc_clean_asconf_ack_cache(asoc);
+
+               /* ADDIP 5.2 E4) When the Sequence Number matches the next one
+                * expected, process the ASCONF as described below and after
+                * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
+                * the response packet and cache a copy of it (in the event it
+                * later needs to be retransmitted).
+                *
+                * Essentially, do V1-V5.
                 */
                asconf_ack = sctp_process_asconf((struct sctp_association *)
                                                 asoc, chunk);
                if (!asconf_ack)
                        return SCTP_DISPOSITION_NOMEM;
-       } else if (serial == asoc->peer.addip_serial) {
-               /* ADDIP 4.2 C3) If the value found in the serial number is
-                * equal to the value stored in the 'Peer-Serial-Number'
-                * IMPLEMENTATION NOTE: As an optimization a receiver may wish
-                * to save the last ASCONF-ACK for some predetermined period of
-                * time and instead of re-processing the ASCONF (with the same
-                * serial number) it may just re-transmit the ASCONF-ACK.
+       } else if (serial < asoc->peer.addip_serial + 1) {
+               /* ADDIP 5.2 E2)
+                * If the value found in the Sequence Number is less than the
+                * ('Peer- Sequence-Number' + 1), simply skip to the next
+                * ASCONF, and include in the outbound response packet
+                * any previously cached ASCONF-ACK response that was
+                * sent and saved that matches the Sequence Number of the
+                * ASCONF.  Note: It is possible that no cached ASCONF-ACK
+                * Chunk exists.  This will occur when an older ASCONF
+                * arrives out of order.  In such a case, the receiver
+                * should skip the ASCONF Chunk and not include ASCONF-ACK
+                * Chunk for that chunk.
                 */
-               if (asoc->addip_last_asconf_ack)
-                       asconf_ack = asoc->addip_last_asconf_ack;
-               else
+               asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
+               if (!asconf_ack)
                        return SCTP_DISPOSITION_DISCARD;
        } else {
-               /* ADDIP 4.2 C4) Otherwise, the ASCONF Chunk is discarded since 
+               /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
                 * it must be either a stale packet or from an attacker.
-                */     
+                */
                return SCTP_DISPOSITION_DISCARD;
        }
 
-       /* ADDIP 4.2 C5) In both cases C2 and C3 the ASCONF-ACK MUST be sent
-        * back to the source address contained in the IP header of the ASCONF
-        * being responded to.
+       /* ADDIP 5.2 E6)  The destination address of the SCTP packet
+        * containing the ASCONF-ACK Chunks MUST be the source address of
+        * the SCTP packet that held the ASCONF Chunks.
+        *
+        * To do this properly, we'll set the destination address of the chunk
+        * and at the transmit time, will try look up the transport to use.
+        * Since ASCONFs may be bundled, the correct transport may not be
+        * created untill we process the entire packet, thus this workaround.
         */
+       asconf_ack->dest = chunk->source;
        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
-       
+
        return SCTP_DISPOSITION_CONSUME;
 }
 
@@ -3293,12 +3558,13 @@ sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
  */
 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                                         const struct sctp_association *asoc,
-                                        const sctp_subtype_t type, void *arg,
+                                        const sctp_subtype_t type, void *arg,
                                         sctp_cmd_seq_t *commands)
 {
        struct sctp_chunk       *asconf_ack = arg;
        struct sctp_chunk       *last_asconf = asoc->addip_last_asconf;
        struct sctp_chunk       *abort;
+       struct sctp_paramhdr    *err_param = NULL;
        sctp_addiphdr_t         *addip_hdr;
        __u32                   sent_serial, rcvd_serial;
 
@@ -3308,6 +3574,15 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
        }
 
+       /* ADD-IP, Section 4.1.2:
+        * This chunk MUST be sent in an authenticated way by using
+        * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
+        * is received unauthenticated it MUST be silently discarded as
+        * described in [I-D.ietf-tsvwg-sctp-auth].
+        */
+       if (!sctp_addip_noauth && !asconf_ack->auth)
+               return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
+
        /* Make sure that the ADDIP chunk has a valid length.  */
        if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
                return sctp_sf_violation_chunklen(ep, asoc, type, arg,
@@ -3316,6 +3591,14 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
        addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
        rcvd_serial = ntohl(addip_hdr->serial);
 
+       /* Verify the ASCONF-ACK chunk before processing it. */
+       if (!sctp_verify_asconf(asoc,
+           (sctp_paramhdr_t *)addip_hdr->params,
+           (void *)asconf_ack->chunk_end,
+           &err_param))
+               return sctp_sf_violation_paramlen(ep, asoc, type, arg,
+                          (void *)err_param, commands);
+
        if (last_asconf) {
                addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
                sent_serial = ntohl(addip_hdr->serial);
@@ -3334,7 +3617,7 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                abort = sctp_make_abort(asoc, asconf_ack,
                                        sizeof(sctp_errhdr_t));
                if (abort) {
-                       sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, NULL, 0);
+                       sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
                        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
                                        SCTP_CHUNK(abort));
                }
@@ -3345,9 +3628,9 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                                SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
                sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
-                               SCTP_ERROR(ECONNABORTED));
+                               SCTP_ERROR(ECONNABORTED));
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_ASCONF_ACK));
+                               SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_ABORT;
@@ -3364,7 +3647,7 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                abort = sctp_make_abort(asoc, asconf_ack,
                                        sizeof(sctp_errhdr_t));
                if (abort) {
-                       sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, NULL, 0);
+                       sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
                        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
                                        SCTP_CHUNK(abort));
                }
@@ -3373,9 +3656,9 @@ sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
                 */
                sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
-                               SCTP_ERROR(ECONNABORTED));
+                               SCTP_ERROR(ECONNABORTED));
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_ASCONF_ACK));
+                               SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_ABORT;
@@ -3406,6 +3689,7 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
 {
        struct sctp_chunk *chunk = arg;
        struct sctp_fwdtsn_hdr *fwdtsn_hdr;
+       struct sctp_fwdtsn_skip *skip;
        __u16 len;
        __u32 tsn;
 
@@ -3427,7 +3711,7 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
        skb_pull(chunk->skb, len);
 
        tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
-       SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
+       SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
 
        /* The TSN is too high--silently discard the chunk and count on it
         * getting retransmitted later.
@@ -3435,19 +3719,25 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
        if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
                goto discard_noforce;
 
+       /* Silently discard the chunk if stream-id is not valid */
+       sctp_walk_fwdtsn(skip, chunk) {
+               if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
+                       goto discard_noforce;
+       }
+
        sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
        if (len > sizeof(struct sctp_fwdtsn_hdr))
-               sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN, 
+               sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
                                SCTP_CHUNK(chunk));
-       
+
        /* Count this as receiving DATA. */
        if (asoc->autoclose) {
                sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
                                SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
        }
-       
+
        /* FIXME: For now send a SACK, but DATA processing may
-        * send another. 
+        * send another.
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
 
@@ -3466,6 +3756,7 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
 {
        struct sctp_chunk *chunk = arg;
        struct sctp_fwdtsn_hdr *fwdtsn_hdr;
+       struct sctp_fwdtsn_skip *skip;
        __u16 len;
        __u32 tsn;
 
@@ -3487,7 +3778,7 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
        skb_pull(chunk->skb, len);
 
        tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
-       SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
+       SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
 
        /* The TSN is too high--silently discard the chunk and count on it
         * getting retransmitted later.
@@ -3495,11 +3786,17 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
        if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
                goto gen_shutdown;
 
+       /* Silently discard the chunk if stream-id is not valid */
+       sctp_walk_fwdtsn(skip, chunk) {
+               if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
+                       goto gen_shutdown;
+       }
+
        sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
        if (len > sizeof(struct sctp_fwdtsn_hdr))
-               sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN, 
+               sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
                                SCTP_CHUNK(chunk));
-       
+
        /* Go a head and force a SACK, since we are shutting down. */
 gen_shutdown:
        /* Implementor's Guide.
@@ -3513,7 +3810,174 @@ gen_shutdown:
        sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
                        SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
 
-        return SCTP_DISPOSITION_CONSUME;
+       return SCTP_DISPOSITION_CONSUME;
+}
+
+/*
+ * SCTP-AUTH Section 6.3 Receving authenticated chukns
+ *
+ *    The receiver MUST use the HMAC algorithm indicated in the HMAC
+ *    Identifier field.  If this algorithm was not specified by the
+ *    receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
+ *    during association setup, the AUTH chunk and all chunks after it MUST
+ *    be discarded and an ERROR chunk SHOULD be sent with the error cause
+ *    defined in Section 4.1.
+ *
+ *    If an endpoint with no shared key receives a Shared Key Identifier
+ *    other than 0, it MUST silently discard all authenticated chunks.  If
+ *    the endpoint has at least one endpoint pair shared key for the peer,
+ *    it MUST use the key specified by the Shared Key Identifier if a
+ *    key has been configured for that Shared Key Identifier.  If no
+ *    endpoint pair shared key has been configured for that Shared Key
+ *    Identifier, all authenticated chunks MUST be silently discarded.
+ *
+ * Verification Tag:  8.5 Verification Tag [Normal verification]
+ *
+ * The return value is the disposition of the chunk.
+ */
+static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
+                                   const struct sctp_association *asoc,
+                                   const sctp_subtype_t type,
+                                   struct sctp_chunk *chunk)
+{
+       struct sctp_authhdr *auth_hdr;
+       struct sctp_hmac *hmac;
+       unsigned int sig_len;
+       __u16 key_id;
+       __u8 *save_digest;
+       __u8 *digest;
+
+       /* Pull in the auth header, so we can do some more verification */
+       auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
+       chunk->subh.auth_hdr = auth_hdr;
+       skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
+
+       /* Make sure that we suport the HMAC algorithm from the auth
+        * chunk.
+        */
+       if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
+               return SCTP_IERROR_AUTH_BAD_HMAC;
+
+       /* Make sure that the provided shared key identifier has been
+        * configured
+        */
+       key_id = ntohs(auth_hdr->shkey_id);
+       if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
+               return SCTP_IERROR_AUTH_BAD_KEYID;
+
+
+       /* Make sure that the length of the signature matches what
+        * we expect.
+        */
+       sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
+       hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
+       if (sig_len != hmac->hmac_len)
+               return SCTP_IERROR_PROTO_VIOLATION;
+
+       /* Now that we've done validation checks, we can compute and
+        * verify the hmac.  The steps involved are:
+        *  1. Save the digest from the chunk.
+        *  2. Zero out the digest in the chunk.
+        *  3. Compute the new digest
+        *  4. Compare saved and new digests.
+        */
+       digest = auth_hdr->hmac;
+       skb_pull(chunk->skb, sig_len);
+
+       save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
+       if (!save_digest)
+               goto nomem;
+
+       memset(digest, 0, sig_len);
+
+       sctp_auth_calculate_hmac(asoc, chunk->skb,
+                               (struct sctp_auth_chunk *)chunk->chunk_hdr,
+                               GFP_ATOMIC);
+
+       /* Discard the packet if the digests do not match */
+       if (memcmp(save_digest, digest, sig_len)) {
+               kfree(save_digest);
+               return SCTP_IERROR_BAD_SIG;
+       }
+
+       kfree(save_digest);
+       chunk->auth = 1;
+
+       return SCTP_IERROR_NO_ERROR;
+nomem:
+       return SCTP_IERROR_NOMEM;
+}
+
+sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
+                                   const struct sctp_association *asoc,
+                                   const sctp_subtype_t type,
+                                   void *arg,
+                                   sctp_cmd_seq_t *commands)
+{
+       struct sctp_authhdr *auth_hdr;
+       struct sctp_chunk *chunk = arg;
+       struct sctp_chunk *err_chunk;
+       sctp_ierror_t error;
+
+       /* Make sure that the peer has AUTH capable */
+       if (!asoc->peer.auth_capable)
+               return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
+
+       if (!sctp_vtag_verify(chunk, asoc)) {
+               sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
+                               SCTP_NULL());
+               return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+       }
+
+       /* Make sure that the AUTH chunk has valid length.  */
+       if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
+       auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
+       error = sctp_sf_authenticate(ep, asoc, type, chunk);
+       switch (error) {
+               case SCTP_IERROR_AUTH_BAD_HMAC:
+                       /* Generate the ERROR chunk and discard the rest
+                        * of the packet
+                        */
+                       err_chunk = sctp_make_op_error(asoc, chunk,
+                                                       SCTP_ERROR_UNSUP_HMAC,
+                                                       &auth_hdr->hmac_id,
+                                                       sizeof(__u16));
+                       if (err_chunk) {
+                               sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
+                                               SCTP_CHUNK(err_chunk));
+                       }
+                       /* Fall Through */
+               case SCTP_IERROR_AUTH_BAD_KEYID:
+               case SCTP_IERROR_BAD_SIG:
+                       return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
+                       break;
+               case SCTP_IERROR_PROTO_VIOLATION:
+                       return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                         commands);
+                       break;
+               case SCTP_IERROR_NOMEM:
+                       return SCTP_DISPOSITION_NOMEM;
+               default:
+                       break;
+       }
+
+       if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
+               struct sctp_ulpevent *ev;
+
+               ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
+                                   SCTP_AUTH_NEWKEY, GFP_ATOMIC);
+
+               if (!ev)
+                       return -ENOMEM;
+
+               sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
+                               SCTP_ULPEVENT(ev));
+       }
+
+       return SCTP_DISPOSITION_CONSUME;
 }
 
 /*
@@ -3568,9 +4032,6 @@ sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
                return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
                break;
        case SCTP_CID_ACTION_DISCARD_ERR:
-               /* Discard the packet.  */
-               sctp_sf_pdiscard(ep, asoc, type, arg, commands);
-
                /* Generate an ERROR chunk as response. */
                hdr = unk_chunk->chunk_hdr;
                err_chunk = sctp_make_op_error(asoc, unk_chunk,
@@ -3580,6 +4041,9 @@ sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
                        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
                                        SCTP_CHUNK(err_chunk));
                }
+
+               /* Discard the packet.  */
+               sctp_sf_pdiscard(ep, asoc, type, arg, commands);
                return SCTP_DISPOSITION_CONSUME;
                break;
        case SCTP_CID_ACTION_SKIP:
@@ -3626,6 +4090,16 @@ sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
                                         void *arg,
                                         sctp_cmd_seq_t *commands)
 {
+       struct sctp_chunk *chunk = arg;
+
+       /* Make sure that the chunk has a valid length.
+        * Since we don't know the chunk type, we use a general
+        * chunkhdr structure to make a comparison.
+        */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
        SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
        return SCTP_DISPOSITION_DISCARD;
 }
@@ -3681,18 +4155,125 @@ sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
                                     void *arg,
                                     sctp_cmd_seq_t *commands)
 {
+       struct sctp_chunk *chunk = arg;
+
+       /* Make sure that the chunk has a valid length. */
+       if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
+               return sctp_sf_violation_chunklen(ep, asoc, type, arg,
+                                                 commands);
+
        return SCTP_DISPOSITION_VIOLATION;
 }
 
+/*
+ * Common function to handle a protocol violation.
+ */
+static sctp_disposition_t sctp_sf_abort_violation(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands,
+                                    const __u8 *payload,
+                                    const size_t paylen)
+{
+       struct sctp_packet *packet = NULL;
+       struct sctp_chunk *chunk =  arg;
+       struct sctp_chunk *abort = NULL;
+
+       /* SCTP-AUTH, Section 6.3:
+        *    It should be noted that if the receiver wants to tear
+        *    down an association in an authenticated way only, the
+        *    handling of malformed packets should not result in
+        *    tearing down the association.
+        *
+        * This means that if we only want to abort associations
+        * in an authenticated way (i.e AUTH+ABORT), then we
+        * can't destroy this association just becuase the packet
+        * was malformed.
+        */
+       if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+               goto discard;
+
+       /* Make the abort chunk. */
+       abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
+       if (!abort)
+               goto nomem;
+
+       if (asoc) {
+               /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
+               if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
+                   !asoc->peer.i.init_tag) {
+                       sctp_initack_chunk_t *initack;
+
+                       initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
+                       if (!sctp_chunk_length_valid(chunk,
+                                                    sizeof(sctp_initack_chunk_t)))
+                               abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
+                       else {
+                               unsigned int inittag;
+
+                               inittag = ntohl(initack->init_hdr.init_tag);
+                               sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
+                                               SCTP_U32(inittag));
+                       }
+               }
+
+               sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
+               SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
+
+               if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
+                       sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
+                                       SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
+                       sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
+                                       SCTP_ERROR(ECONNREFUSED));
+                       sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
+                                       SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
+               } else {
+                       sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
+                                       SCTP_ERROR(ECONNABORTED));
+                       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
+                                       SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
+                       SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
+               }
+       } else {
+               packet = sctp_ootb_pkt_new(asoc, chunk);
+
+               if (!packet)
+                       goto nomem_pkt;
+
+               if (sctp_test_T_bit(abort))
+                       packet->vtag = ntohl(chunk->sctp_hdr->vtag);
+
+               abort->skb->sk = ep->base.sk;
+
+               sctp_packet_append_chunk(packet, abort);
+
+               sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
+                       SCTP_PACKET(packet));
+
+               SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
+       }
+
+       SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
+
+discard:
+       sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
+       return SCTP_DISPOSITION_ABORT;
+
+nomem_pkt:
+       sctp_chunk_free(abort);
+nomem:
+       return SCTP_DISPOSITION_NOMEM;
+}
 
 /*
  * Handle a protocol violation when the chunk length is invalid.
- * "Invalid" length is identified as smaller then the minimal length a
+ * "Invalid" length is identified as smaller than the minimal length a
  * given chunk can be.  For example, a SACK chunk has invalid length
- * if it's length is set to be smaller then the size of sctp_sack_chunk_t.
+ * if its length is set to be smaller than the size of sctp_sack_chunk_t.
  *
  * We inform the other end by sending an ABORT with a Protocol Violation
- * error code. 
+ * error code.
  *
  * Section: Not specified
  * Verification Tag:  Nothing to do
@@ -3711,44 +4292,93 @@ static sctp_disposition_t sctp_sf_violation_chunklen(
                                     void *arg,
                                     sctp_cmd_seq_t *commands)
 {
+       static const char err_str[]="The following chunk had invalid length:";
+
+       return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
+                                       sizeof(err_str));
+}
+
+/*
+ * Handle a protocol violation when the parameter length is invalid.
+ * "Invalid" length is identified as smaller than the minimal length a
+ * given parameter can be.
+ */
+static sctp_disposition_t sctp_sf_violation_paramlen(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg, void *ext,
+                                    sctp_cmd_seq_t *commands)
+{
        struct sctp_chunk *chunk =  arg;
+       struct sctp_paramhdr *param = ext;
        struct sctp_chunk *abort = NULL;
-       char               err_str[]="The following chunk had invalid length:";
+
+       if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+               goto discard;
 
        /* Make the abort chunk. */
-       abort = sctp_make_abort_violation(asoc, chunk, err_str,
-                                         sizeof(err_str));
+       abort = sctp_make_violation_paramlen(asoc, chunk, param);
        if (!abort)
                goto nomem;
 
        sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
        SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
 
-       if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
-               sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
-                               SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
-               sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
-                               SCTP_ERROR(ECONNREFUSED));
-               sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                               SCTP_U32(SCTP_ERROR_PROTO_VIOLATION));
-       } else {
-               sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
-                               SCTP_ERROR(ECONNABORTED));
-               sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_PROTO_VIOLATION));
-               SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
-       }
-
-       sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
-
+       sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
+                       SCTP_ERROR(ECONNABORTED));
+       sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
+                       SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
+       SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
        SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
-       
-       return SCTP_DISPOSITION_ABORT;
 
+discard:
+       sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
+       return SCTP_DISPOSITION_ABORT;
 nomem:
        return SCTP_DISPOSITION_NOMEM;
 }
 
+/* Handle a protocol violation when the peer trying to advance the
+ * cumulative tsn ack to a point beyond the max tsn currently sent.
+ *
+ * We inform the other end by sending an ABORT with a Protocol Violation
+ * error code.
+ */
+static sctp_disposition_t sctp_sf_violation_ctsn(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands)
+{
+       static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
+
+       return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
+                                       sizeof(err_str));
+}
+
+/* Handle protocol violation of an invalid chunk bundling.  For example,
+ * when we have an association and we recieve bundled INIT-ACK, or
+ * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
+ * statement from the specs.  Additinally, there might be an attacker
+ * on the path and we may not want to continue this communication.
+ */
+static sctp_disposition_t sctp_sf_violation_chunk(
+                                    const struct sctp_endpoint *ep,
+                                    const struct sctp_association *asoc,
+                                    const sctp_subtype_t type,
+                                    void *arg,
+                                    sctp_cmd_seq_t *commands)
+{
+       static const char err_str[]="The following chunk violates protocol:";
+
+       if (!asoc)
+               return sctp_sf_violation(ep, asoc, type, arg, commands);
+
+       return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
+                                       sizeof(err_str));
+}
 /***************************************************************************
  * These are the state functions for handling primitive (Section 10) events.
  ***************************************************************************/
@@ -3816,6 +4446,7 @@ sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
                                       sctp_cmd_seq_t *commands)
 {
        struct sctp_chunk *repl;
+       struct sctp_association* my_asoc;
 
        /* The comment below says that we enter COOKIE-WAIT AFTER
         * sending the INIT, but that doesn't actually work in our
@@ -3839,8 +4470,8 @@ sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
        /* Cast away the const modifier, as we want to just
         * rerun it through as a sideffect.
         */
-       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC,
-                       SCTP_ASOC((struct sctp_association *) asoc));
+       my_asoc = (struct sctp_association *)asoc;
+       sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
 
        /* Choose transport for INIT. */
        sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
@@ -3977,13 +4608,6 @@ sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
        sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
                        SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
 
-       /* sctpimpguide-05 Section 2.12.2
-        * The sender of the SHUTDOWN MAY also start an overall guard timer
-        * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
-        */
-       sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
-                       SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
-
        disposition = SCTP_DISPOSITION_CONSUME;
        if (sctp_outq_is_empty(&asoc->outqueue)) {
                disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
@@ -4049,7 +4673,7 @@ sctp_disposition_t sctp_sf_do_9_1_prm_abort(
                        SCTP_ERROR(ECONNABORTED));
        /* Delete the established association. */
        sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                       SCTP_U32(SCTP_ERROR_USER_ABORT));
+                       SCTP_PERR(SCTP_ERROR_USER_ABORT));
 
        SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
        SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
@@ -4186,7 +4810,7 @@ sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
                        SCTP_ERROR(ECONNREFUSED));
        /* Delete the established association. */
        sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                       SCTP_U32(SCTP_ERROR_USER_ABORT));
+                       SCTP_PERR(SCTP_ERROR_USER_ABORT));
 
        return retval;
 }
@@ -4328,8 +4952,24 @@ sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
                                        void *arg,
                                        sctp_cmd_seq_t *commands)
 {
-       return sctp_sf_heartbeat(ep, asoc, type, (struct sctp_transport *)arg,
-                                commands);
+       if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
+                                     (struct sctp_transport *)arg, commands))
+               return SCTP_DISPOSITION_NOMEM;
+
+       /*
+        * RFC 2960 (bis), section 8.3
+        *
+        *    D) Request an on-demand HEARTBEAT on a specific destination
+        *    transport address of a given association.
+        *
+        *    The endpoint should increment the respective error  counter of
+        *    the destination transport address each time a HEARTBEAT is sent
+        *    to that address and not acknowledged within one RTO.
+        *
+        */
+       sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
+                       SCTP_TRANSPORT(arg));
+       return SCTP_DISPOSITION_CONSUME;
 }
 
 /*
@@ -4412,6 +5052,13 @@ sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
        sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
                        SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
 
+       /* RFC 4960 Section 9.2
+        * The sender of the SHUTDOWN MAY also start an overall guard timer
+        * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
+        */
+       sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
+                       SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
+
        if (asoc->autoclose)
                sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
                                SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
@@ -4423,7 +5070,7 @@ sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
        /* sctp-implguide 2.10 Issues with Heartbeating and failover
         *
         * HEARTBEAT ... is discontinued after sending either SHUTDOWN
-         * or SHUTDOWN-ACK.
+        * or SHUTDOWN-ACK.
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
 
@@ -4501,7 +5148,7 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
        /* sctp-implguide 2.10 Issues with Heartbeating and failover
         *
         * HEARTBEAT ... is discontinued after sending either SHUTDOWN
-         * or SHUTDOWN-ACK.
+        * or SHUTDOWN-ACK.
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
 
@@ -4558,7 +5205,7 @@ sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
                                SCTP_ERROR(ETIMEDOUT));
                /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_DELETE_TCB;
@@ -4591,12 +5238,12 @@ sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
         * sent as soon as cwnd allows (normally when a SACK arrives).
         */
 
-       /* NB: Rules E4 and F1 are implicit in R1.  */
-       sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
-
        /* Do some failure management (Section 8.2). */
        sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
 
+       /* NB: Rules E4 and F1 are implicit in R1.  */
+       sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
+
        return SCTP_DISPOSITION_CONSUME;
 }
 
@@ -4680,7 +5327,7 @@ sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                return SCTP_DISPOSITION_DELETE_TCB;
        }
 
@@ -4723,6 +5370,8 @@ sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep
                if (!repl)
                        return SCTP_DISPOSITION_NOMEM;
 
+               sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
+                               SCTP_CHUNK(repl));
                /* Issue a sideeffect to do the needed accounting. */
                sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
                                SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
@@ -4732,7 +5381,7 @@ sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                return SCTP_DISPOSITION_DELETE_TCB;
        }
 
@@ -4763,12 +5412,14 @@ sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
        SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
        SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
 
+       ((struct sctp_association *)asoc)->shutdown_retries++;
+
        if (asoc->overall_error_count >= asoc->max_retrans) {
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                /* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_DELETE_TCB;
@@ -4786,7 +5437,7 @@ sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
        default:
                BUG();
                break;
-       };
+       }
 
        if (!reply)
                goto nomem;
@@ -4846,9 +5497,9 @@ sctp_disposition_t sctp_sf_t4_timer_expire(
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ETIMEDOUT));
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_ERROR));
+                               SCTP_PERR(SCTP_ERROR_NO_ERROR));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
-               SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
+               SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_DISPOSITION_ABORT;
        }
 
@@ -4860,7 +5511,7 @@ sctp_disposition_t sctp_sf_t4_timer_expire(
        /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
         * choose an alternate destination address (please refer to RFC2960
         * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
-        * chunk, it MUST be the same (including its serial number) as the last 
+        * chunk, it MUST be the same (including its serial number) as the last
         * ASCONF sent.
         */
        sctp_chunk_hold(asoc->addip_last_asconf);
@@ -4902,7 +5553,10 @@ sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
        sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                        SCTP_ERROR(ETIMEDOUT));
        sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                       SCTP_U32(SCTP_ERROR_NO_ERROR));
+                       SCTP_PERR(SCTP_ERROR_NO_ERROR));
+
+       SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
+       SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
 
        return SCTP_DISPOSITION_DELETE_TCB;
 nomem:
@@ -4936,12 +5590,6 @@ sctp_disposition_t sctp_sf_autoclose_timer_expire(
        sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
                        SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
 
-       /* sctpimpguide-05 Section 2.12.2
-        * The sender of the SHUTDOWN MAY also start an overall guard timer
-        * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
-        */
-       sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
-                       SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
        disposition = SCTP_DISPOSITION_CONSUME;
        if (sctp_outq_is_empty(&asoc->outqueue)) {
                disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
@@ -5099,7 +5747,22 @@ static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc
         * association exists, otherwise, use the peer's vtag.
         */
        if (asoc) {
-               vtag = asoc->peer.i.init_tag;
+               /* Special case the INIT-ACK as there is no peer's vtag
+                * yet.
+                */
+               switch(chunk->chunk_hdr->type) {
+               case SCTP_CID_INIT_ACK:
+               {
+                       sctp_initack_chunk_t *initack;
+
+                       initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
+                       vtag = ntohl(initack->init_hdr.init_tag);
+                       break;
+               }
+               default:
+                       vtag = asoc->peer.i.init_tag;
+                       break;
+               }
        } else {
                /* Special case the INIT and stale COOKIE_ECHO as there is no
                 * vtag yet.
@@ -5113,7 +5776,7 @@ static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc
                        vtag = ntohl(init->init_hdr.init_tag);
                        break;
                }
-               default:        
+               default:
                        vtag = ntohl(chunk->sctp_hdr->vtag);
                        break;
                }
@@ -5162,7 +5825,7 @@ static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
                        /* Override the OOTB vtag from the cookie. */
                        cookie = chunk->subh.cookie_hdr;
                        packet->vtag = cookie->c.peer_vtag;
-                       
+
                        /* Set the skb to the belonging sock for accounting. */
                        err_chunk->skb->sk = ep->base.sk;
                        sctp_packet_append_chunk(packet, err_chunk);
@@ -5186,10 +5849,8 @@ static int sctp_eat_data(const struct sctp_association *asoc,
        sctp_verb_t deliver;
        int tmp;
        __u32 tsn;
-       int account_value;
        struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
        struct sock *sk = asoc->base.sk;
-       int rcvbuf_over = 0;
 
        data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
        skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
@@ -5199,48 +5860,6 @@ static int sctp_eat_data(const struct sctp_association *asoc,
 
        /* ASSERT:  Now skb->data is really the user data.  */
 
-       /*
-        * If we are established, and we have used up our receive buffer
-        * memory, think about droping the frame.
-        * Note that we have an opportunity to improve performance here.
-        * If we accept one chunk from an skbuff, we have to keep all the
-        * memory of that skbuff around until the chunk is read into user
-        * space. Therefore, once we accept 1 chunk we may as well accept all
-        * remaining chunks in the skbuff. The data_accepted flag helps us do
-        * that.
-        */
-       if ((asoc->state == SCTP_STATE_ESTABLISHED) && (!chunk->data_accepted)) {
-               /*
-                * If the receive buffer policy is 1, then each
-                * association can allocate up to sk_rcvbuf bytes
-                * otherwise, all the associations in aggregate
-                * may allocate up to sk_rcvbuf bytes
-                */
-               if (asoc->ep->rcvbuf_policy)
-                       account_value = atomic_read(&asoc->rmem_alloc);
-               else
-                       account_value = atomic_read(&sk->sk_rmem_alloc);
-               if (account_value > sk->sk_rcvbuf) {
-                       /*
-                        * We need to make forward progress, even when we are
-                        * under memory pressure, so we always allow the
-                        * next tsn after the ctsn ack point to be accepted.
-                        * This lets us avoid deadlocks in which we have to
-                        * drop frames that would otherwise let us drain the
-                        * receive queue.
-                        */
-                       if ((sctp_tsnmap_get_ctsn(map) + 1) != tsn)
-                               return SCTP_IERROR_IGNORE_TSN;
-
-                       /*
-                        * We're going to accept the frame but we should renege
-                        * to make space for it. This will send us down that
-                        * path later in this function.
-                        */
-                       rcvbuf_over = 1;
-               }
-       }
-
        /* Process ECN based congestion.
         *
         * Since the chunk structure is reused for all chunks within
@@ -5256,7 +5875,7 @@ static int sctp_eat_data(const struct sctp_association *asoc,
                chunk->ecn_ce_done = 1;
 
                af = sctp_get_af_specific(
-                       ipver2af(chunk->skb->nh.iph->version));
+                       ipver2af(ip_hdr(chunk->skb)->version));
 
                if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
                        /* Do real work as sideffect. */
@@ -5296,22 +5915,13 @@ static int sctp_eat_data(const struct sctp_association *asoc,
                sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
        }
 
-        /* Spill over rwnd a little bit.  Note: While allowed, this spill over
+       /* Spill over rwnd a little bit.  Note: While allowed, this spill over
         * seems a bit troublesome in that frag_point varies based on
         * PMTU.  In cases, such as loopback, this might be a rather
         * large spill over.
-        * NOTE: If we have a full receive buffer here, we only renege if
-        * our receiver can still make progress without the tsn being
-        * received. We do this because in the event that the associations
-        * receive queue is empty we are filling a leading gap, and since
-        * reneging moves the gap to the end of the tsn stream, we are likely
-        * to stall again very shortly. Avoiding the renege when we fill a
-        * leading gap is a good heuristic for avoiding such steady state
-        * stalls.
-        */
-       if (!asoc->rwnd || asoc->rwnd_over ||
-           (datalen > asoc->rwnd + asoc->frag_point) ||
-           (rcvbuf_over && (!skb_queue_len(&sk->sk_receive_queue)))) {
+        */
+       if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
+           (datalen > asoc->rwnd + asoc->frag_point))) {
 
                /* If this is the next TSN, consider reneging to make
                 * room.   Note: Playing nice with a confused sender.  A
@@ -5332,6 +5942,21 @@ static int sctp_eat_data(const struct sctp_association *asoc,
        }
 
        /*
+        * Also try to renege to limit our memory usage in the event that
+        * we are under memory pressure
+        * If we can't renege, don't worry about it, the sk_rmem_schedule
+        * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
+        * memory usage too much
+        */
+       if (*sk->sk_prot_creator->memory_pressure) {
+               if (sctp_tsnmap_has_gap(map) &&
+                  (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
+                       SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
+                       deliver = SCTP_CMD_RENEGE;
+                }
+       }
+
+       /*
         * Section 3.3.10.9 No User Data (9)
         *
         * Cause of error
@@ -5352,18 +5977,12 @@ static int sctp_eat_data(const struct sctp_association *asoc,
                sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
                                SCTP_ERROR(ECONNABORTED));
                sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
-                               SCTP_U32(SCTP_ERROR_NO_DATA));
+                               SCTP_PERR(SCTP_ERROR_NO_DATA));
                SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
                SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
                return SCTP_IERROR_NO_DATA;
        }
 
-       /* If definately accepting the DATA chunk, record its TSN, otherwise
-        * wait for renege processing.
-        */
-       if (SCTP_CMD_CHUNK_ULP == deliver)
-               sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
-
        chunk->data_accepted = 1;
 
        /* Note: Some chunks may get overcounted (if we drop) or overcounted
@@ -5383,6 +6002,9 @@ static int sctp_eat_data(const struct sctp_association *asoc,
         * and discard the DATA chunk.
         */
        if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
+               /* Mark tsn as received even though we drop it */
+               sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
+
                err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
                                         &data_hdr->stream,
                                         sizeof(data_hdr->stream));