OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sctp/] [sm_make_chunk.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/* SCTP kernel reference Implementation
/* SCTP kernel reference Implementation
 * (C) Copyright IBM Corp. 2001, 2003
 * (C) Copyright IBM Corp. 2001, 2003
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001-2002 Intel Corp.
 * Copyright (c) 2001-2002 Intel Corp.
 *
 *
 * This file is part of the SCTP kernel reference Implementation
 * This file is part of the SCTP kernel reference Implementation
 *
 *
 * This file includes part of the implementation of the add-IP extension,
 * This file includes part of the implementation of the add-IP extension,
 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
 * for the SCTP kernel reference Implementation.
 * for the SCTP kernel reference Implementation.
 *
 *
 * These functions work with the state functions in sctp_sm_statefuns.c
 * These functions work with the state functions in sctp_sm_statefuns.c
 * to implement the state operations.  These functions implement the
 * to implement the state operations.  These functions implement the
 * steps which require modifying existing data structures.
 * steps which require modifying existing data structures.
 *
 *
 * The SCTP reference implementation is free software;
 * The SCTP reference implementation is free software;
 * you can redistribute it and/or modify it under the terms of
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * any later version.
 *
 *
 * The SCTP reference implementation is distributed in the hope that it
 * The SCTP reference implementation is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * See the GNU General Public License for more details.
 *
 *
 * You should have received a copy of the GNU General Public License
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * Boston, MA 02111-1307, USA.
 *
 *
 * Please send any bug reports or fixes you make to the
 * Please send any bug reports or fixes you make to the
 * email address(es):
 * email address(es):
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
 *
 *
 * Or submit a bug report through the following website:
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *    http://www.sf.net/projects/lksctp
 *
 *
 * Written or modified by:
 * Written or modified by:
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    C. Robin              <chris@hundredacre.ac.uk>
 *    C. Robin              <chris@hundredacre.ac.uk>
 *    Jon Grimm             <jgrimm@us.ibm.com>
 *    Jon Grimm             <jgrimm@us.ibm.com>
 *    Xingang Guo           <xingang.guo@intel.com>
 *    Xingang Guo           <xingang.guo@intel.com>
 *    Dajiang Zhang         <dajiang.zhang@nokia.com>
 *    Dajiang Zhang         <dajiang.zhang@nokia.com>
 *    Sridhar Samudrala     <sri@us.ibm.com>
 *    Sridhar Samudrala     <sri@us.ibm.com>
 *    Daisy Chang           <daisyc@us.ibm.com>
 *    Daisy Chang           <daisyc@us.ibm.com>
 *    Ardelle Fan           <ardelle.fan@intel.com>
 *    Ardelle Fan           <ardelle.fan@intel.com>
 *    Kevin Gao             <kevin.gao@intel.com>
 *    Kevin Gao             <kevin.gao@intel.com>
 *
 *
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 * be incorporated into the next SCTP release.
 */
 */
 
 
#include <linux/types.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/ipv6.h>
#include <linux/net.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/inet.h>
#include <asm/scatterlist.h>
#include <asm/scatterlist.h>
#include <linux/crypto.h>
#include <linux/crypto.h>
#include <net/sock.h>
#include <net/sock.h>
 
 
#include <linux/skbuff.h>
#include <linux/skbuff.h>
#include <linux/random.h>       /* for get_random_bytes */
#include <linux/random.h>       /* for get_random_bytes */
#include <net/sctp/sctp.h>
#include <net/sctp/sctp.h>
#include <net/sctp/sm.h>
#include <net/sctp/sm.h>
 
 
extern kmem_cache_t *sctp_chunk_cachep;
extern kmem_cache_t *sctp_chunk_cachep;
 
 
/* What was the inbound interface for this chunk? */
/* What was the inbound interface for this chunk? */
int sctp_chunk_iif(const struct sctp_chunk *chunk)
int sctp_chunk_iif(const struct sctp_chunk *chunk)
{
{
        struct sctp_af *af;
        struct sctp_af *af;
        int iif = 0;
        int iif = 0;
 
 
        af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
        af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
        if (af)
        if (af)
                iif = af->skb_iif(chunk->skb);
                iif = af->skb_iif(chunk->skb);
 
 
        return iif;
        return iif;
}
}
 
 
/* RFC 2960 3.3.2 Initiation (INIT) (1)
/* RFC 2960 3.3.2 Initiation (INIT) (1)
 *
 *
 * Note 2: The ECN capable field is reserved for future use of
 * Note 2: The ECN capable field is reserved for future use of
 * Explicit Congestion Notification.
 * Explicit Congestion Notification.
 */
 */
static const sctp_ecn_capable_param_t ecap_param = {
static const sctp_ecn_capable_param_t ecap_param = {
        {
        {
                SCTP_PARAM_ECN_CAPABLE,
                SCTP_PARAM_ECN_CAPABLE,
                __constant_htons(sizeof(sctp_ecn_capable_param_t)),
                __constant_htons(sizeof(sctp_ecn_capable_param_t)),
        }
        }
};
};
 
 
/* A helper to initialize to initialize an op error inside a
/* A helper to initialize to initialize an op error inside a
 * provided chunk, as most cause codes will be embedded inside an
 * provided chunk, as most cause codes will be embedded inside an
 * abort chunk.
 * abort chunk.
 */
 */
void  sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
void  sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
                      const void *payload, size_t paylen)
                      const void *payload, size_t paylen)
{
{
        sctp_errhdr_t err;
        sctp_errhdr_t err;
        int padlen;
        int padlen;
        __u16 len;
        __u16 len;
 
 
        /* Cause code constants are now defined in network order.  */
        /* Cause code constants are now defined in network order.  */
        err.cause = cause_code;
        err.cause = cause_code;
        len = sizeof(sctp_errhdr_t) + paylen;
        len = sizeof(sctp_errhdr_t) + paylen;
        padlen = len % 4;
        padlen = len % 4;
        err.length  = htons(len);
        err.length  = htons(len);
        len += padlen;
        len += padlen;
        sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
        sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
        chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
        chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
}
}
 
 
/* 3.3.2 Initiation (INIT) (1)
/* 3.3.2 Initiation (INIT) (1)
 *
 *
 * This chunk is used to initiate a SCTP association between two
 * This chunk is used to initiate a SCTP association between two
 * endpoints. The format of the INIT chunk is shown below:
 * endpoints. The format of the INIT chunk is shown below:
 *
 *
 *     0                   1                   2                   3
 *     0                   1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
 *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                         Initiate Tag                          |
 *    |                         Initiate Tag                          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |           Advertised Receiver Window Credit (a_rwnd)          |
 *    |           Advertised Receiver Window Credit (a_rwnd)          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
 *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                          Initial TSN                          |
 *    |                          Initial TSN                          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    \                                                               \
 *    \                                                               \
 *    /              Optional/Variable-Length Parameters              /
 *    /              Optional/Variable-Length Parameters              /
 *    \                                                               \
 *    \                                                               \
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 *
 *
 * The INIT chunk contains the following parameters. Unless otherwise
 * The INIT chunk contains the following parameters. Unless otherwise
 * noted, each parameter MUST only be included once in the INIT chunk.
 * noted, each parameter MUST only be included once in the INIT chunk.
 *
 *
 * Fixed Parameters                     Status
 * Fixed Parameters                     Status
 * ----------------------------------------------
 * ----------------------------------------------
 * Initiate Tag                        Mandatory
 * Initiate Tag                        Mandatory
 * Advertised Receiver Window Credit   Mandatory
 * Advertised Receiver Window Credit   Mandatory
 * Number of Outbound Streams          Mandatory
 * Number of Outbound Streams          Mandatory
 * Number of Inbound Streams           Mandatory
 * Number of Inbound Streams           Mandatory
 * Initial TSN                         Mandatory
 * Initial TSN                         Mandatory
 *
 *
 * Variable Parameters                  Status     Type Value
 * Variable Parameters                  Status     Type Value
 * -------------------------------------------------------------
 * -------------------------------------------------------------
 * IPv4 Address (Note 1)               Optional    5
 * IPv4 Address (Note 1)               Optional    5
 * IPv6 Address (Note 1)               Optional    6
 * IPv6 Address (Note 1)               Optional    6
 * Cookie Preservative                 Optional    9
 * Cookie Preservative                 Optional    9
 * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
 * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
 * Host Name Address (Note 3)          Optional    11
 * Host Name Address (Note 3)          Optional    11
 * Supported Address Types (Note 4)    Optional    12
 * Supported Address Types (Note 4)    Optional    12
 */
 */
struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
                             const struct sctp_bind_addr *bp,
                             const struct sctp_bind_addr *bp,
                             int gfp, int vparam_len)
                             int gfp, int vparam_len)
{
{
        sctp_inithdr_t init;
        sctp_inithdr_t init;
        union sctp_params addrs;
        union sctp_params addrs;
        size_t chunksize;
        size_t chunksize;
        struct sctp_chunk *retval = NULL;
        struct sctp_chunk *retval = NULL;
        int num_types, addrs_len = 0;
        int num_types, addrs_len = 0;
        struct sctp_opt *sp;
        struct sctp_opt *sp;
        sctp_supported_addrs_param_t sat;
        sctp_supported_addrs_param_t sat;
        __u16 types[2];
        __u16 types[2];
 
 
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
         *
         *
         * Note 1: The INIT chunks can contain multiple addresses that
         * Note 1: The INIT chunks can contain multiple addresses that
         * can be IPv4 and/or IPv6 in any combination.
         * can be IPv4 and/or IPv6 in any combination.
         */
         */
        retval = NULL;
        retval = NULL;
 
 
        /* Convert the provided bind address list to raw format. */
        /* Convert the provided bind address list to raw format. */
        addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
        addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
 
 
        init.init_tag              = htonl(asoc->c.my_vtag);
        init.init_tag              = htonl(asoc->c.my_vtag);
        init.a_rwnd                = htonl(asoc->rwnd);
        init.a_rwnd                = htonl(asoc->rwnd);
        init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
        init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
        init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
        init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
        init.initial_tsn           = htonl(asoc->c.initial_tsn);
        init.initial_tsn           = htonl(asoc->c.initial_tsn);
 
 
        /* How many address types are needed? */
        /* How many address types are needed? */
        sp = sctp_sk(asoc->base.sk);
        sp = sctp_sk(asoc->base.sk);
        num_types = sp->pf->supported_addrs(sp, types);
        num_types = sp->pf->supported_addrs(sp, types);
 
 
        chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
        chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
        chunksize += sizeof(ecap_param);
        chunksize += sizeof(ecap_param);
        chunksize += vparam_len;
        chunksize += vparam_len;
 
 
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
         *
         *
         * Note 3: An INIT chunk MUST NOT contain more than one Host
         * Note 3: An INIT chunk MUST NOT contain more than one Host
         * Name address parameter. Moreover, the sender of the INIT
         * Name address parameter. Moreover, the sender of the INIT
         * MUST NOT combine any other address types with the Host Name
         * MUST NOT combine any other address types with the Host Name
         * address in the INIT. The receiver of INIT MUST ignore any
         * address in the INIT. The receiver of INIT MUST ignore any
         * other address types if the Host Name address parameter is
         * other address types if the Host Name address parameter is
         * present in the received INIT chunk.
         * present in the received INIT chunk.
         *
         *
         * PLEASE DO NOT FIXME [This version does not support Host Name.]
         * PLEASE DO NOT FIXME [This version does not support Host Name.]
         */
         */
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        retval->subh.init_hdr =
        retval->subh.init_hdr =
                sctp_addto_chunk(retval, sizeof(init), &init);
                sctp_addto_chunk(retval, sizeof(init), &init);
        retval->param_hdr.v =
        retval->param_hdr.v =
                sctp_addto_chunk(retval, addrs_len, addrs.v);
                sctp_addto_chunk(retval, addrs_len, addrs.v);
 
 
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
         *
         *
         * Note 4: This parameter, when present, specifies all the
         * Note 4: This parameter, when present, specifies all the
         * address types the sending endpoint can support. The absence
         * address types the sending endpoint can support. The absence
         * of this parameter indicates that the sending endpoint can
         * of this parameter indicates that the sending endpoint can
         * support any address type.
         * support any address type.
         */
         */
        sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
        sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
        sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
        sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
        sctp_addto_chunk(retval, sizeof(sat), &sat);
        sctp_addto_chunk(retval, sizeof(sat), &sat);
        sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
        sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
 
 
        sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
        sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
nodata:
nodata:
        if (addrs.v)
        if (addrs.v)
                kfree(addrs.v);
                kfree(addrs.v);
        return retval;
        return retval;
}
}
 
 
struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
                                 const struct sctp_chunk *chunk,
                                 const struct sctp_chunk *chunk,
                                 int gfp, int unkparam_len)
                                 int gfp, int unkparam_len)
{
{
        sctp_inithdr_t initack;
        sctp_inithdr_t initack;
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        union sctp_params addrs;
        union sctp_params addrs;
        int addrs_len;
        int addrs_len;
        sctp_cookie_param_t *cookie;
        sctp_cookie_param_t *cookie;
        int cookie_len;
        int cookie_len;
        size_t chunksize;
        size_t chunksize;
 
 
        retval = NULL;
        retval = NULL;
 
 
        /* Note: there may be no addresses to embed. */
        /* Note: there may be no addresses to embed. */
        addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
        addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
 
 
        initack.init_tag                = htonl(asoc->c.my_vtag);
        initack.init_tag                = htonl(asoc->c.my_vtag);
        initack.a_rwnd                  = htonl(asoc->rwnd);
        initack.a_rwnd                  = htonl(asoc->rwnd);
        initack.num_outbound_streams    = htons(asoc->c.sinit_num_ostreams);
        initack.num_outbound_streams    = htons(asoc->c.sinit_num_ostreams);
        initack.num_inbound_streams     = htons(asoc->c.sinit_max_instreams);
        initack.num_inbound_streams     = htons(asoc->c.sinit_max_instreams);
        initack.initial_tsn             = htonl(asoc->c.initial_tsn);
        initack.initial_tsn             = htonl(asoc->c.initial_tsn);
 
 
        /* FIXME:  We really ought to build the cookie right
        /* FIXME:  We really ought to build the cookie right
         * into the packet instead of allocating more fresh memory.
         * into the packet instead of allocating more fresh memory.
         */
         */
        cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
        cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
                                  addrs.v, addrs_len);
                                  addrs.v, addrs_len);
        if (!cookie)
        if (!cookie)
                goto nomem_cookie;
                goto nomem_cookie;
 
 
        /* Calculate the total size of allocation, include the reserved
        /* Calculate the total size of allocation, include the reserved
         * space for reporting unknown parameters if it is specified.
         * space for reporting unknown parameters if it is specified.
         */
         */
        chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
        chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
 
 
        /* Tell peer that we'll do ECN only if peer advertised such cap.  */
        /* Tell peer that we'll do ECN only if peer advertised such cap.  */
        if (asoc->peer.ecn_capable)
        if (asoc->peer.ecn_capable)
                chunksize += sizeof(ecap_param);
                chunksize += sizeof(ecap_param);
 
 
        /* Now allocate and fill out the chunk.  */
        /* Now allocate and fill out the chunk.  */
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
        if (!retval)
        if (!retval)
                goto nomem_chunk;
                goto nomem_chunk;
 
 
        /* Per the advice in RFC 2960 6.4, send this reply to
        /* Per the advice in RFC 2960 6.4, send this reply to
         * the source of the INIT packet.
         * the source of the INIT packet.
         */
         */
        retval->transport = chunk->transport;
        retval->transport = chunk->transport;
        retval->subh.init_hdr =
        retval->subh.init_hdr =
                sctp_addto_chunk(retval, sizeof(initack), &initack);
                sctp_addto_chunk(retval, sizeof(initack), &initack);
        retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
        retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
        sctp_addto_chunk(retval, cookie_len, cookie);
        sctp_addto_chunk(retval, cookie_len, cookie);
        if (asoc->peer.ecn_capable)
        if (asoc->peer.ecn_capable)
                sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
                sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
 
 
        /* We need to remove the const qualifier at this point.  */
        /* We need to remove the const qualifier at this point.  */
        retval->asoc = (struct sctp_association *) asoc;
        retval->asoc = (struct sctp_association *) asoc;
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it received the DATA or control chunk
         * address from which it received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [INIT ACK back to where the INIT came from.]
         * [INIT ACK back to where the INIT came from.]
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
nomem_chunk:
nomem_chunk:
        kfree(cookie);
        kfree(cookie);
nomem_cookie:
nomem_cookie:
        if (addrs.v)
        if (addrs.v)
                kfree(addrs.v);
                kfree(addrs.v);
        return retval;
        return retval;
}
}
 
 
/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
 *
 *
 * This chunk is used only during the initialization of an association.
 * This chunk is used only during the initialization of an association.
 * It is sent by the initiator of an association to its peer to complete
 * It is sent by the initiator of an association to its peer to complete
 * the initialization process. This chunk MUST precede any DATA chunk
 * the initialization process. This chunk MUST precede any DATA chunk
 * sent within the association, but MAY be bundled with one or more DATA
 * sent within the association, but MAY be bundled with one or more DATA
 * chunks in the same packet.
 * chunks in the same packet.
 *
 *
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |   Type = 10   |Chunk  Flags   |         Length                |
 *     |   Type = 10   |Chunk  Flags   |         Length                |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     /                     Cookie                                    /
 *     /                     Cookie                                    /
 *     \                                                               \
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * Chunk Flags: 8 bit
 * Chunk Flags: 8 bit
 *
 *
 *   Set to zero on transmit and ignored on receipt.
 *   Set to zero on transmit and ignored on receipt.
 *
 *
 * Length: 16 bits (unsigned integer)
 * Length: 16 bits (unsigned integer)
 *
 *
 *   Set to the size of the chunk in bytes, including the 4 bytes of
 *   Set to the size of the chunk in bytes, including the 4 bytes of
 *   the chunk header and the size of the Cookie.
 *   the chunk header and the size of the Cookie.
 *
 *
 * Cookie: variable size
 * Cookie: variable size
 *
 *
 *   This field must contain the exact cookie received in the
 *   This field must contain the exact cookie received in the
 *   State Cookie parameter from the previous INIT ACK.
 *   State Cookie parameter from the previous INIT ACK.
 *
 *
 *   An implementation SHOULD make the cookie as small as possible
 *   An implementation SHOULD make the cookie as small as possible
 *   to insure interoperability.
 *   to insure interoperability.
 */
 */
struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
                                    const struct sctp_chunk *chunk)
                                    const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        void *cookie;
        void *cookie;
        int cookie_len;
        int cookie_len;
 
 
        cookie = asoc->peer.cookie;
        cookie = asoc->peer.cookie;
        cookie_len = asoc->peer.cookie_len;
        cookie_len = asoc->peer.cookie_len;
 
 
        /* Build a cookie echo chunk.  */
        /* Build a cookie echo chunk.  */
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
        retval->subh.cookie_hdr =
        retval->subh.cookie_hdr =
                sctp_addto_chunk(retval, cookie_len, cookie);
                sctp_addto_chunk(retval, cookie_len, cookie);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [COOKIE ECHO back to where the INIT ACK came from.]
         * [COOKIE ECHO back to where the INIT ACK came from.]
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
 *
 *
 * This chunk is used only during the initialization of an
 * This chunk is used only during the initialization of an
 * association.  It is used to acknowledge the receipt of a COOKIE
 * association.  It is used to acknowledge the receipt of a COOKIE
 * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
 * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
 * within the association, but MAY be bundled with one or more DATA
 * within the association, but MAY be bundled with one or more DATA
 * chunks or SACK chunk in the same SCTP packet.
 * chunks or SACK chunk in the same SCTP packet.
 *
 *
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
 *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * Chunk Flags: 8 bits
 * Chunk Flags: 8 bits
 *
 *
 *   Set to zero on transmit and ignored on receipt.
 *   Set to zero on transmit and ignored on receipt.
 */
 */
struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
                                   const struct sctp_chunk *chunk)
                                   const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [COOKIE ACK back to where the COOKIE ECHO came from.]
         * [COOKIE ACK back to where the COOKIE ECHO came from.]
         */
         */
        if (retval && chunk)
        if (retval && chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
        return retval;
        return retval;
}
}
 
 
/*
/*
 *  Appendix A: Explicit Congestion Notification:
 *  Appendix A: Explicit Congestion Notification:
 *  CWR:
 *  CWR:
 *
 *
 *  RFC 2481 details a specific bit for a sender to send in the header of
 *  RFC 2481 details a specific bit for a sender to send in the header of
 *  its next outbound TCP segment to indicate to its peer that it has
 *  its next outbound TCP segment to indicate to its peer that it has
 *  reduced its congestion window.  This is termed the CWR bit.  For
 *  reduced its congestion window.  This is termed the CWR bit.  For
 *  SCTP the same indication is made by including the CWR chunk.
 *  SCTP the same indication is made by including the CWR chunk.
 *  This chunk contains one data element, i.e. the TSN number that
 *  This chunk contains one data element, i.e. the TSN number that
 *  was sent in the ECNE chunk.  This element represents the lowest
 *  was sent in the ECNE chunk.  This element represents the lowest
 *  TSN number in the datagram that was originally marked with the
 *  TSN number in the datagram that was originally marked with the
 *  CE bit.
 *  CE bit.
 *
 *
 *     0                   1                   2                   3
 *     0                   1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
 *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                      Lowest TSN Number                        |
 *    |                      Lowest TSN Number                        |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 *     Note: The CWR is considered a Control chunk.
 *     Note: The CWR is considered a Control chunk.
 */
 */
struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
                            const __u32 lowest_tsn,
                            const __u32 lowest_tsn,
                            const struct sctp_chunk *chunk)
                            const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        sctp_cwrhdr_t cwr;
        sctp_cwrhdr_t cwr;
 
 
        cwr.lowest_tsn = htonl(lowest_tsn);
        cwr.lowest_tsn = htonl(lowest_tsn);
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
                                 sizeof(sctp_cwrhdr_t));
                                 sizeof(sctp_cwrhdr_t));
 
 
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        retval->subh.ecn_cwr_hdr =
        retval->subh.ecn_cwr_hdr =
                sctp_addto_chunk(retval, sizeof(cwr), &cwr);
                sctp_addto_chunk(retval, sizeof(cwr), &cwr);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [Report a reduced congestion window back to where the ECNE
         * [Report a reduced congestion window back to where the ECNE
         * came from.]
         * came from.]
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Make an ECNE chunk.  This is a congestion experienced report.  */
/* Make an ECNE chunk.  This is a congestion experienced report.  */
struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
                             const __u32 lowest_tsn)
                             const __u32 lowest_tsn)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        sctp_ecnehdr_t ecne;
        sctp_ecnehdr_t ecne;
 
 
        ecne.lowest_tsn = htonl(lowest_tsn);
        ecne.lowest_tsn = htonl(lowest_tsn);
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
                                 sizeof(sctp_ecnehdr_t));
                                 sizeof(sctp_ecnehdr_t));
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
        retval->subh.ecne_hdr =
        retval->subh.ecne_hdr =
                sctp_addto_chunk(retval, sizeof(ecne), &ecne);
                sctp_addto_chunk(retval, sizeof(ecne), &ecne);
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Make a DATA chunk for the given association from the provided
/* Make a DATA chunk for the given association from the provided
 * parameters.  However, do not populate the data payload.
 * parameters.  However, do not populate the data payload.
 */
 */
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
                                       const struct sctp_sndrcvinfo *sinfo,
                                       const struct sctp_sndrcvinfo *sinfo,
                                       int data_len, __u8 flags, __u16 ssn)
                                       int data_len, __u8 flags, __u16 ssn)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        struct sctp_datahdr dp;
        struct sctp_datahdr dp;
        int chunk_len;
        int chunk_len;
 
 
        /* We assign the TSN as LATE as possible, not here when
        /* We assign the TSN as LATE as possible, not here when
         * creating the chunk.
         * creating the chunk.
         */
         */
        dp.tsn = 0;
        dp.tsn = 0;
        dp.stream = htons(sinfo->sinfo_stream);
        dp.stream = htons(sinfo->sinfo_stream);
        dp.ppid   = sinfo->sinfo_ppid;
        dp.ppid   = sinfo->sinfo_ppid;
 
 
        /* Set the flags for an unordered send.  */
        /* Set the flags for an unordered send.  */
        if (sinfo->sinfo_flags & MSG_UNORDERED) {
        if (sinfo->sinfo_flags & MSG_UNORDERED) {
                flags |= SCTP_DATA_UNORDERED;
                flags |= SCTP_DATA_UNORDERED;
                dp.ssn = 0;
                dp.ssn = 0;
        } else
        } else
                dp.ssn = htons(ssn);
                dp.ssn = htons(ssn);
 
 
        chunk_len = sizeof(dp) + data_len;
        chunk_len = sizeof(dp) + data_len;
        retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
        retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
        retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
        memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
        memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Make a DATA chunk for the given association.  Populate the data
/* Make a DATA chunk for the given association.  Populate the data
 * payload.
 * payload.
 */
 */
struct sctp_chunk *sctp_make_datafrag(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_datafrag(struct sctp_association *asoc,
                                 const struct sctp_sndrcvinfo *sinfo,
                                 const struct sctp_sndrcvinfo *sinfo,
                                 int data_len, const __u8 *data,
                                 int data_len, const __u8 *data,
                                 __u8 flags, __u16 ssn)
                                 __u8 flags, __u16 ssn)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
        retval = sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
        if (retval)
        if (retval)
                sctp_addto_chunk(retval, data_len, data);
                sctp_addto_chunk(retval, data_len, data);
 
 
        return retval;
        return retval;
}
}
 
 
/* Make a DATA chunk for the given association to ride on stream id
/* Make a DATA chunk for the given association to ride on stream id
 * 'stream', with a payload id of 'payload', and a body of 'data'.
 * 'stream', with a payload id of 'payload', and a body of 'data'.
 */
 */
struct sctp_chunk *sctp_make_data(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_data(struct sctp_association *asoc,
                             const struct sctp_sndrcvinfo *sinfo,
                             const struct sctp_sndrcvinfo *sinfo,
                             int data_len, const __u8 *data)
                             int data_len, const __u8 *data)
{
{
        struct sctp_chunk *retval = NULL;
        struct sctp_chunk *retval = NULL;
 
 
        retval = sctp_make_data_empty(asoc, sinfo, data_len);
        retval = sctp_make_data_empty(asoc, sinfo, data_len);
        if (retval)
        if (retval)
                sctp_addto_chunk(retval, data_len, data);
                sctp_addto_chunk(retval, data_len, data);
        return retval;
        return retval;
}
}
 
 
/* Make a DATA chunk for the given association to ride on stream id
/* Make a DATA chunk for the given association to ride on stream id
 * 'stream', with a payload id of 'payload', and a body big enough to
 * 'stream', with a payload id of 'payload', and a body big enough to
 * hold 'data_len' octets of data.  We use this version when we need
 * hold 'data_len' octets of data.  We use this version when we need
 * to build the message AFTER allocating memory.
 * to build the message AFTER allocating memory.
 */
 */
struct sctp_chunk *sctp_make_data_empty(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_data_empty(struct sctp_association *asoc,
                                   const struct sctp_sndrcvinfo *sinfo,
                                   const struct sctp_sndrcvinfo *sinfo,
                                   int data_len)
                                   int data_len)
{
{
        __u8 flags = SCTP_DATA_NOT_FRAG;
        __u8 flags = SCTP_DATA_NOT_FRAG;
 
 
        return sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, 0);
        return sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, 0);
}
}
 
 
/* Create a selective ackowledgement (SACK) for the given
/* Create a selective ackowledgement (SACK) for the given
 * association.  This reports on which TSN's we've seen to date,
 * association.  This reports on which TSN's we've seen to date,
 * including duplicates and gaps.
 * including duplicates and gaps.
 */
 */
struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        struct sctp_sackhdr sack;
        struct sctp_sackhdr sack;
        int len;
        int len;
        __u32 ctsn;
        __u32 ctsn;
        __u16 num_gabs, num_dup_tsns;
        __u16 num_gabs, num_dup_tsns;
        struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
        struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
 
 
        ctsn = sctp_tsnmap_get_ctsn(map);
        ctsn = sctp_tsnmap_get_ctsn(map);
        SCTP_DEBUG_PRINTK("sackCTSNAck sent:  0x%x.\n", ctsn);
        SCTP_DEBUG_PRINTK("sackCTSNAck sent:  0x%x.\n", ctsn);
 
 
        /* How much room is needed in the chunk? */
        /* How much room is needed in the chunk? */
        num_gabs = sctp_tsnmap_num_gabs(map);
        num_gabs = sctp_tsnmap_num_gabs(map);
        num_dup_tsns = sctp_tsnmap_num_dups(map);
        num_dup_tsns = sctp_tsnmap_num_dups(map);
 
 
        /* Initialize the SACK header.  */
        /* Initialize the SACK header.  */
        sack.cum_tsn_ack            = htonl(ctsn);
        sack.cum_tsn_ack            = htonl(ctsn);
        sack.a_rwnd                 = htonl(asoc->a_rwnd);
        sack.a_rwnd                 = htonl(asoc->a_rwnd);
        sack.num_gap_ack_blocks     = htons(num_gabs);
        sack.num_gap_ack_blocks     = htons(num_gabs);
        sack.num_dup_tsns           = htons(num_dup_tsns);
        sack.num_dup_tsns           = htons(num_dup_tsns);
 
 
        len = sizeof(sack)
        len = sizeof(sack)
                + sizeof(struct sctp_gap_ack_block) * num_gabs
                + sizeof(struct sctp_gap_ack_block) * num_gabs
                + sizeof(__u32) * num_dup_tsns;
                + sizeof(__u32) * num_dup_tsns;
 
 
        /* Create the chunk.  */
        /* Create the chunk.  */
        retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
        retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, etc.) to the same destination transport
         * HEARTBEAT ACK, etc.) to the same destination transport
         * address from which it received the DATA or control chunk to
         * address from which it received the DATA or control chunk to
         * which it is replying.  This rule should also be followed if
         * which it is replying.  This rule should also be followed if
         * the endpoint is bundling DATA chunks together with the
         * the endpoint is bundling DATA chunks together with the
         * reply chunk.
         * reply chunk.
         *
         *
         * However, when acknowledging multiple DATA chunks received
         * However, when acknowledging multiple DATA chunks received
         * in packets from different source addresses in a single
         * in packets from different source addresses in a single
         * SACK, the SACK chunk may be transmitted to one of the
         * SACK, the SACK chunk may be transmitted to one of the
         * destination transport addresses from which the DATA or
         * destination transport addresses from which the DATA or
         * control chunks being acknowledged were received.
         * control chunks being acknowledged were received.
         *
         *
         * [BUG:  We do not implement the following paragraph.
         * [BUG:  We do not implement the following paragraph.
         * Perhaps we should remember the last transport we used for a
         * Perhaps we should remember the last transport we used for a
         * SACK and avoid that (if possible) if we have seen any
         * SACK and avoid that (if possible) if we have seen any
         * duplicates. --piggy]
         * duplicates. --piggy]
         *
         *
         * When a receiver of a duplicate DATA chunk sends a SACK to a
         * When a receiver of a duplicate DATA chunk sends a SACK to a
         * multi- homed endpoint it MAY be beneficial to vary the
         * multi- homed endpoint it MAY be beneficial to vary the
         * destination address and not use the source address of the
         * destination address and not use the source address of the
         * DATA chunk.  The reason being that receiving a duplicate
         * DATA chunk.  The reason being that receiving a duplicate
         * from a multi-homed endpoint might indicate that the return
         * from a multi-homed endpoint might indicate that the return
         * path (as specified in the source address of the DATA chunk)
         * path (as specified in the source address of the DATA chunk)
         * for the SACK is broken.
         * for the SACK is broken.
         *
         *
         * [Send to the address from which we last received a DATA chunk.]
         * [Send to the address from which we last received a DATA chunk.]
         */
         */
        retval->transport = asoc->peer.last_data_from;
        retval->transport = asoc->peer.last_data_from;
 
 
        retval->subh.sack_hdr =
        retval->subh.sack_hdr =
                sctp_addto_chunk(retval, sizeof(sack), &sack);
                sctp_addto_chunk(retval, sizeof(sack), &sack);
 
 
        /* Add the gap ack block information.   */
        /* Add the gap ack block information.   */
        if (num_gabs)
        if (num_gabs)
                sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
                sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
                                 sctp_tsnmap_get_gabs(map));
                                 sctp_tsnmap_get_gabs(map));
 
 
        /* Add the duplicate TSN information.  */
        /* Add the duplicate TSN information.  */
        if (num_dup_tsns)
        if (num_dup_tsns)
                sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
                sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
                                 sctp_tsnmap_get_dups(map));
                                 sctp_tsnmap_get_dups(map));
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Make a SHUTDOWN chunk. */
/* Make a SHUTDOWN chunk. */
struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
                                      const struct sctp_chunk *chunk)
                                      const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        sctp_shutdownhdr_t shut;
        sctp_shutdownhdr_t shut;
        __u32 ctsn;
        __u32 ctsn;
 
 
        ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
        ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
        shut.cum_tsn_ack = htonl(ctsn);
        shut.cum_tsn_ack = htonl(ctsn);
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
                                 sizeof(sctp_shutdownhdr_t));
                                 sizeof(sctp_shutdownhdr_t));
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        retval->subh.shutdown_hdr =
        retval->subh.shutdown_hdr =
                sctp_addto_chunk(retval, sizeof(shut), &shut);
                sctp_addto_chunk(retval, sizeof(shut), &shut);
 
 
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
                                     const struct sctp_chunk *chunk)
                                     const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [ACK back to where the SHUTDOWN came from.]
         * [ACK back to where the SHUTDOWN came from.]
         */
         */
        if (retval && chunk)
        if (retval && chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
        return retval;
        return retval;
}
}
 
 
struct sctp_chunk *sctp_make_shutdown_complete(
struct sctp_chunk *sctp_make_shutdown_complete(
        const struct sctp_association *asoc,
        const struct sctp_association *asoc,
        const struct sctp_chunk *chunk)
        const struct sctp_chunk *chunk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        __u8 flags = 0;
        __u8 flags = 0;
 
 
        /* Maybe set the T-bit if we have no association. */
        /* Maybe set the T-bit if we have no association. */
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
         * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
         * came from.]
         * came from.]
         */
         */
        if (retval && chunk)
        if (retval && chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
        return retval;
        return retval;
}
}
 
 
/* Create an ABORT.  Note that we set the T bit if we have no
/* Create an ABORT.  Note that we set the T bit if we have no
 * association.
 * association.
 */
 */
struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
                              const struct sctp_chunk *chunk,
                              const struct sctp_chunk *chunk,
                              const size_t hint)
                              const size_t hint)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        __u8 flags = 0;
        __u8 flags = 0;
 
 
        /* Maybe set the T-bit if we have no association.  */
        /* Maybe set the T-bit if we have no association.  */
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
        retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [ABORT back to where the offender came from.]
         * [ABORT back to where the offender came from.]
         */
         */
        if (retval && chunk)
        if (retval && chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
        return retval;
        return retval;
}
}
 
 
/* Helper to create ABORT with a NO_USER_DATA error.  */
/* Helper to create ABORT with a NO_USER_DATA error.  */
struct sctp_chunk *sctp_make_abort_no_data(
struct sctp_chunk *sctp_make_abort_no_data(
        const struct sctp_association *asoc,
        const struct sctp_association *asoc,
        const struct sctp_chunk *chunk, __u32 tsn)
        const struct sctp_chunk *chunk, __u32 tsn)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        __u32 payload;
        __u32 payload;
 
 
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
                                 + sizeof(tsn));
                                 + sizeof(tsn));
 
 
        if (!retval)
        if (!retval)
                goto no_mem;
                goto no_mem;
 
 
        /* Put the tsn back into network byte order.  */
        /* Put the tsn back into network byte order.  */
        payload = htonl(tsn);
        payload = htonl(tsn);
        sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
        sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
                        sizeof(payload));
                        sizeof(payload));
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [ABORT back to where the offender came from.]
         * [ABORT back to where the offender came from.]
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
no_mem:
no_mem:
        return retval;
        return retval;
}
}
 
 
/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
                                   const struct sctp_chunk *chunk,
                                   const struct sctp_chunk *chunk,
                                   const struct msghdr *msg)
                                   const struct msghdr *msg)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        void *payload = NULL, *payoff;
        void *payload = NULL, *payoff;
        size_t paylen = 0;
        size_t paylen = 0;
        struct iovec *iov = NULL;
        struct iovec *iov = NULL;
        int iovlen = 0;
        int iovlen = 0;
 
 
        if (msg) {
        if (msg) {
                iov = msg->msg_iov;
                iov = msg->msg_iov;
                iovlen = msg->msg_iovlen;
                iovlen = msg->msg_iovlen;
                paylen = get_user_iov_size(iov, iovlen);
                paylen = get_user_iov_size(iov, iovlen);
        }
        }
 
 
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
        if (!retval)
        if (!retval)
                goto err_chunk;
                goto err_chunk;
 
 
        if (paylen) {
        if (paylen) {
                /* Put the msg_iov together into payload.  */
                /* Put the msg_iov together into payload.  */
                payload = kmalloc(paylen, GFP_ATOMIC);
                payload = kmalloc(paylen, GFP_ATOMIC);
                if (!payload)
                if (!payload)
                        goto err_payload;
                        goto err_payload;
                payoff = payload;
                payoff = payload;
 
 
                for (; iovlen > 0; --iovlen) {
                for (; iovlen > 0; --iovlen) {
                        if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
                        if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
                                goto err_copy;
                                goto err_copy;
                        payoff += iov->iov_len;
                        payoff += iov->iov_len;
                        iov++;
                        iov++;
                }
                }
        }
        }
 
 
        sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
        sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
 
 
        if (paylen)
        if (paylen)
                kfree(payload);
                kfree(payload);
 
 
        return retval;
        return retval;
 
 
err_copy:
err_copy:
        kfree(payload);
        kfree(payload);
err_payload:
err_payload:
        sctp_chunk_free(retval);
        sctp_chunk_free(retval);
        retval = NULL;
        retval = NULL;
err_chunk:
err_chunk:
        return retval;
        return retval;
}
}
 
 
/* Make a HEARTBEAT chunk.  */
/* Make a HEARTBEAT chunk.  */
struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
                                  const struct sctp_transport *transport,
                                  const struct sctp_transport *transport,
                                  const void *payload, const size_t paylen)
                                  const void *payload, const size_t paylen)
{
{
        struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
        struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
                                                    0, paylen);
                                                    0, paylen);
 
 
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        /* Cast away the 'const', as this is just telling the chunk
        /* Cast away the 'const', as this is just telling the chunk
         * what transport it belongs to.
         * what transport it belongs to.
         */
         */
        retval->transport = (struct sctp_transport *) transport;
        retval->transport = (struct sctp_transport *) transport;
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
                                      const struct sctp_chunk *chunk,
                                      const struct sctp_chunk *chunk,
                                      const void *payload, const size_t paylen)
                                      const void *payload, const size_t paylen)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval  = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
        retval  = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * HEARTBEAT ACK, * etc.) to the same destination transport
         * address from which it * received the DATA or control chunk
         * address from which it * received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         * [HBACK back to where the HEARTBEAT came from.]
         * [HBACK back to where the HEARTBEAT came from.]
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Create an Operation Error chunk with the specified space reserved.
/* Create an Operation Error chunk with the specified space reserved.
 * This routine can be used for containing multiple causes in the chunk.
 * This routine can be used for containing multiple causes in the chunk.
 */
 */
struct sctp_chunk *sctp_make_op_error_space(
struct sctp_chunk *sctp_make_op_error_space(
        const struct sctp_association *asoc,
        const struct sctp_association *asoc,
        const struct sctp_chunk *chunk,
        const struct sctp_chunk *chunk,
        size_t size)
        size_t size)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
        retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
                                 sizeof(sctp_errhdr_t) + size);
                                 sizeof(sctp_errhdr_t) + size);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
         *
         *
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
         * HEARTBEAT ACK, etc.) to the same destination transport
         * HEARTBEAT ACK, etc.) to the same destination transport
         * address from which it received the DATA or control chunk
         * address from which it received the DATA or control chunk
         * to which it is replying.
         * to which it is replying.
         *
         *
         */
         */
        if (chunk)
        if (chunk)
                retval->transport = chunk->transport;
                retval->transport = chunk->transport;
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Create an Operation Error chunk.  */
/* Create an Operation Error chunk.  */
struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
                                 const struct sctp_chunk *chunk,
                                 const struct sctp_chunk *chunk,
                                 __u16 cause_code, const void *payload,
                                 __u16 cause_code, const void *payload,
                                 size_t paylen)
                                 size_t paylen)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = sctp_make_op_error_space(asoc, chunk, paylen);
        retval = sctp_make_op_error_space(asoc, chunk, paylen);
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
 
 
        sctp_init_cause(retval, cause_code, payload, paylen);
        sctp_init_cause(retval, cause_code, payload, paylen);
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/********************************************************************
/********************************************************************
 * 2nd Level Abstractions
 * 2nd Level Abstractions
 ********************************************************************/
 ********************************************************************/
 
 
/* Turn an skb into a chunk.
/* Turn an skb into a chunk.
 * FIXME: Eventually move the structure directly inside the skb->cb[].
 * FIXME: Eventually move the structure directly inside the skb->cb[].
 */
 */
struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
                            const struct sctp_association *asoc,
                            const struct sctp_association *asoc,
                            struct sock *sk)
                            struct sock *sk)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
 
 
        retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
        retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
 
 
        if (!retval)
        if (!retval)
                goto nodata;
                goto nodata;
        memset(retval, 0, sizeof(struct sctp_chunk));
        memset(retval, 0, sizeof(struct sctp_chunk));
 
 
        if (!sk) {
        if (!sk) {
                SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
                SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
        }
        }
 
 
        retval->skb             = skb;
        retval->skb             = skb;
        retval->asoc            = (struct sctp_association *)asoc;
        retval->asoc            = (struct sctp_association *)asoc;
        retval->resent          = 0;
        retval->resent          = 0;
        retval->has_tsn         = 0;
        retval->has_tsn         = 0;
        retval->has_ssn         = 0;
        retval->has_ssn         = 0;
        retval->rtt_in_progress = 0;
        retval->rtt_in_progress = 0;
        retval->sent_at         = 0;
        retval->sent_at         = 0;
        retval->singleton       = 1;
        retval->singleton       = 1;
        retval->end_of_packet   = 0;
        retval->end_of_packet   = 0;
        retval->ecn_ce_done     = 0;
        retval->ecn_ce_done     = 0;
        retval->pdiscard        = 0;
        retval->pdiscard        = 0;
 
 
        /* sctpimpguide-05.txt Section 2.8.2
        /* sctpimpguide-05.txt Section 2.8.2
         * M1) Each time a new DATA chunk is transmitted
         * M1) Each time a new DATA chunk is transmitted
         * set the 'TSN.Missing.Report' count for that TSN to 0. The
         * set the 'TSN.Missing.Report' count for that TSN to 0. The
         * 'TSN.Missing.Report' count will be used to determine missing chunks
         * 'TSN.Missing.Report' count will be used to determine missing chunks
         * and when to fast retransmit.
         * and when to fast retransmit.
         */
         */
        retval->tsn_missing_report = 0;
        retval->tsn_missing_report = 0;
        retval->tsn_gap_acked = 0;
        retval->tsn_gap_acked = 0;
        retval->fast_retransmit = 0;
        retval->fast_retransmit = 0;
 
 
        /* If this is a fragmented message, track all fragments
        /* If this is a fragmented message, track all fragments
         * of the message (for SEND_FAILED).
         * of the message (for SEND_FAILED).
         */
         */
        retval->msg = NULL;
        retval->msg = NULL;
 
 
        /* Polish the bead hole.  */
        /* Polish the bead hole.  */
        INIT_LIST_HEAD(&retval->transmitted_list);
        INIT_LIST_HEAD(&retval->transmitted_list);
        INIT_LIST_HEAD(&retval->frag_list);
        INIT_LIST_HEAD(&retval->frag_list);
        SCTP_DBG_OBJCNT_INC(chunk);
        SCTP_DBG_OBJCNT_INC(chunk);
        atomic_set(&retval->refcnt, 1);
        atomic_set(&retval->refcnt, 1);
 
 
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Set chunk->source and dest based on the IP header in chunk->skb.  */
/* Set chunk->source and dest based on the IP header in chunk->skb.  */
void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
                     union sctp_addr *dest)
                     union sctp_addr *dest)
{
{
        memcpy(&chunk->source, src, sizeof(union sctp_addr));
        memcpy(&chunk->source, src, sizeof(union sctp_addr));
        memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
        memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
}
}
 
 
/* Extract the source address from a chunk.  */
/* Extract the source address from a chunk.  */
const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
{
{
        /* If we have a known transport, use that.  */
        /* If we have a known transport, use that.  */
        if (chunk->transport) {
        if (chunk->transport) {
                return &chunk->transport->ipaddr;
                return &chunk->transport->ipaddr;
        } else {
        } else {
                /* Otherwise, extract it from the IP header.  */
                /* Otherwise, extract it from the IP header.  */
                return &chunk->source;
                return &chunk->source;
        }
        }
}
}
 
 
/* Create a new chunk, setting the type and flags headers from the
/* Create a new chunk, setting the type and flags headers from the
 * arguments, reserving enough space for a 'paylen' byte payload.
 * arguments, reserving enough space for a 'paylen' byte payload.
 */
 */
struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
                                   __u8 type, __u8 flags, int paylen)
                                   __u8 type, __u8 flags, int paylen)
{
{
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        sctp_chunkhdr_t *chunk_hdr;
        sctp_chunkhdr_t *chunk_hdr;
        struct sk_buff *skb;
        struct sk_buff *skb;
        struct sock *sk;
        struct sock *sk;
 
 
        /* No need to allocate LL here, as this is only a chunk. */
        /* No need to allocate LL here, as this is only a chunk. */
        skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
        skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
                        GFP_ATOMIC);
                        GFP_ATOMIC);
        if (!skb)
        if (!skb)
                goto nodata;
                goto nodata;
 
 
        /* Make room for the chunk header.  */
        /* Make room for the chunk header.  */
        chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
        chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
        chunk_hdr->type   = type;
        chunk_hdr->type   = type;
        chunk_hdr->flags  = flags;
        chunk_hdr->flags  = flags;
        chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
        chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
 
 
        sk = asoc ? asoc->base.sk : NULL;
        sk = asoc ? asoc->base.sk : NULL;
        retval = sctp_chunkify(skb, asoc, sk);
        retval = sctp_chunkify(skb, asoc, sk);
        if (!retval) {
        if (!retval) {
                kfree_skb(skb);
                kfree_skb(skb);
                goto nodata;
                goto nodata;
        }
        }
 
 
        retval->chunk_hdr = chunk_hdr;
        retval->chunk_hdr = chunk_hdr;
        retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
        retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
 
 
        /* Set the skb to the belonging sock for accounting.  */
        /* Set the skb to the belonging sock for accounting.  */
        skb->sk = sk;
        skb->sk = sk;
 
 
        return retval;
        return retval;
nodata:
nodata:
        return NULL;
        return NULL;
}
}
 
 
 
 
/* Release the memory occupied by a chunk.  */
/* Release the memory occupied by a chunk.  */
static void sctp_chunk_destroy(struct sctp_chunk *chunk)
static void sctp_chunk_destroy(struct sctp_chunk *chunk)
{
{
        /* Free the chunk skb data and the SCTP_chunk stub itself. */
        /* Free the chunk skb data and the SCTP_chunk stub itself. */
        dev_kfree_skb(chunk->skb);
        dev_kfree_skb(chunk->skb);
 
 
        SCTP_DBG_OBJCNT_DEC(chunk);
        SCTP_DBG_OBJCNT_DEC(chunk);
        kmem_cache_free(sctp_chunk_cachep, chunk);
        kmem_cache_free(sctp_chunk_cachep, chunk);
}
}
 
 
/* Possibly, free the chunk.  */
/* Possibly, free the chunk.  */
void sctp_chunk_free(struct sctp_chunk *chunk)
void sctp_chunk_free(struct sctp_chunk *chunk)
{
{
        /* Make sure that we are not on any list.  */
        /* Make sure that we are not on any list.  */
        skb_unlink((struct sk_buff *) chunk);
        skb_unlink((struct sk_buff *) chunk);
        list_del_init(&chunk->transmitted_list);
        list_del_init(&chunk->transmitted_list);
 
 
        /* Release our reference on the message tracker. */
        /* Release our reference on the message tracker. */
        if (chunk->msg)
        if (chunk->msg)
                sctp_datamsg_put(chunk->msg);
                sctp_datamsg_put(chunk->msg);
 
 
        sctp_chunk_put(chunk);
        sctp_chunk_put(chunk);
}
}
 
 
/* Grab a reference to the chunk. */
/* Grab a reference to the chunk. */
void sctp_chunk_hold(struct sctp_chunk *ch)
void sctp_chunk_hold(struct sctp_chunk *ch)
{
{
        atomic_inc(&ch->refcnt);
        atomic_inc(&ch->refcnt);
}
}
 
 
/* Release a reference to the chunk. */
/* Release a reference to the chunk. */
void sctp_chunk_put(struct sctp_chunk *ch)
void sctp_chunk_put(struct sctp_chunk *ch)
{
{
        if (atomic_dec_and_test(&ch->refcnt))
        if (atomic_dec_and_test(&ch->refcnt))
                sctp_chunk_destroy(ch);
                sctp_chunk_destroy(ch);
}
}
 
 
/* Append bytes to the end of a chunk.  Will panic if chunk is not big
/* Append bytes to the end of a chunk.  Will panic if chunk is not big
 * enough.
 * enough.
 */
 */
void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
{
{
        void *target;
        void *target;
        void *padding;
        void *padding;
        int chunklen = ntohs(chunk->chunk_hdr->length);
        int chunklen = ntohs(chunk->chunk_hdr->length);
        int padlen = chunklen % 4;
        int padlen = chunklen % 4;
 
 
        padding = skb_put(chunk->skb, padlen);
        padding = skb_put(chunk->skb, padlen);
        target = skb_put(chunk->skb, len);
        target = skb_put(chunk->skb, len);
 
 
        memset(padding, 0, padlen);
        memset(padding, 0, padlen);
        memcpy(target, data, len);
        memcpy(target, data, len);
 
 
        /* Adjust the chunk length field.  */
        /* Adjust the chunk length field.  */
        chunk->chunk_hdr->length = htons(chunklen + padlen + len);
        chunk->chunk_hdr->length = htons(chunklen + padlen + len);
        chunk->chunk_end = chunk->skb->tail;
        chunk->chunk_end = chunk->skb->tail;
 
 
        return target;
        return target;
}
}
 
 
/* Append bytes from user space to the end of a chunk.  Will panic if
/* Append bytes from user space to the end of a chunk.  Will panic if
 * chunk is not big enough.
 * chunk is not big enough.
 * Returns a kernel err value.
 * Returns a kernel err value.
 */
 */
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
                          struct iovec *data)
                          struct iovec *data)
{
{
        __u8 *target;
        __u8 *target;
        int err = 0;
        int err = 0;
 
 
        /* Make room in chunk for data.  */
        /* Make room in chunk for data.  */
        target = skb_put(chunk->skb, len);
        target = skb_put(chunk->skb, len);
 
 
        /* Copy data (whole iovec) into chunk */
        /* Copy data (whole iovec) into chunk */
        if ((err = memcpy_fromiovecend(target, data, off, len)))
        if ((err = memcpy_fromiovecend(target, data, off, len)))
                goto out;
                goto out;
 
 
        /* Adjust the chunk length field.  */
        /* Adjust the chunk length field.  */
        chunk->chunk_hdr->length =
        chunk->chunk_hdr->length =
                htons(ntohs(chunk->chunk_hdr->length) + len);
                htons(ntohs(chunk->chunk_hdr->length) + len);
        chunk->chunk_end = chunk->skb->tail;
        chunk->chunk_end = chunk->skb->tail;
 
 
out:
out:
        return err;
        return err;
}
}
 
 
/* Helper function to assign a TSN if needed.  This assumes that both
/* Helper function to assign a TSN if needed.  This assumes that both
 * the data_hdr and association have already been assigned.
 * the data_hdr and association have already been assigned.
 */
 */
void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
{
{
        __u16 ssn;
        __u16 ssn;
        __u16 sid;
        __u16 sid;
 
 
        if (chunk->has_ssn)
        if (chunk->has_ssn)
                return;
                return;
 
 
        /* This is the last possible instant to assign a SSN. */
        /* This is the last possible instant to assign a SSN. */
        if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
        if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
                ssn = 0;
                ssn = 0;
        } else {
        } else {
                sid = htons(chunk->subh.data_hdr->stream);
                sid = htons(chunk->subh.data_hdr->stream);
                if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
                if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
                        ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
                        ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
                else
                else
                        ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
                        ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
                ssn = htons(ssn);
                ssn = htons(ssn);
        }
        }
 
 
        chunk->subh.data_hdr->ssn = ssn;
        chunk->subh.data_hdr->ssn = ssn;
        chunk->has_ssn = 1;
        chunk->has_ssn = 1;
}
}
 
 
/* Helper function to assign a TSN if needed.  This assumes that both
/* Helper function to assign a TSN if needed.  This assumes that both
 * the data_hdr and association have already been assigned.
 * the data_hdr and association have already been assigned.
 */
 */
void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
{
{
        if (!chunk->has_tsn) {
        if (!chunk->has_tsn) {
                /* This is the last possible instant to
                /* This is the last possible instant to
                 * assign a TSN.
                 * assign a TSN.
                 */
                 */
                chunk->subh.data_hdr->tsn =
                chunk->subh.data_hdr->tsn =
                        htonl(sctp_association_get_next_tsn(chunk->asoc));
                        htonl(sctp_association_get_next_tsn(chunk->asoc));
                chunk->has_tsn = 1;
                chunk->has_tsn = 1;
        }
        }
}
}
 
 
/* Create a CLOSED association to use with an incoming packet.  */
/* Create a CLOSED association to use with an incoming packet.  */
struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
                                        struct sctp_chunk *chunk, int gfp)
                                        struct sctp_chunk *chunk, int gfp)
{
{
        struct sctp_association *asoc;
        struct sctp_association *asoc;
        struct sk_buff *skb;
        struct sk_buff *skb;
        sctp_scope_t scope;
        sctp_scope_t scope;
        struct sctp_af *af;
        struct sctp_af *af;
 
 
        /* Create the bare association.  */
        /* Create the bare association.  */
        scope = sctp_scope(sctp_source(chunk));
        scope = sctp_scope(sctp_source(chunk));
        asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
        asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
        if (!asoc)
        if (!asoc)
                goto nodata;
                goto nodata;
        asoc->temp = 1;
        asoc->temp = 1;
        skb = chunk->skb;
        skb = chunk->skb;
        /* Create an entry for the source address of the packet.  */
        /* Create an entry for the source address of the packet.  */
        af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
        af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
        if (unlikely(!af))
        if (unlikely(!af))
                goto fail;
                goto fail;
        af->from_skb(&asoc->c.peer_addr, skb, 1);
        af->from_skb(&asoc->c.peer_addr, skb, 1);
nodata:
nodata:
        return asoc;
        return asoc;
 
 
fail:
fail:
        sctp_association_free(asoc);
        sctp_association_free(asoc);
        return NULL;
        return NULL;
}
}
 
 
/* Build a cookie representing asoc.
/* Build a cookie representing asoc.
 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
 */
 */
sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
                                      const struct sctp_association *asoc,
                                      const struct sctp_association *asoc,
                                      const struct sctp_chunk *init_chunk,
                                      const struct sctp_chunk *init_chunk,
                                      int *cookie_len,
                                      int *cookie_len,
                                      const __u8 *raw_addrs, int addrs_len)
                                      const __u8 *raw_addrs, int addrs_len)
{
{
        sctp_cookie_param_t *retval;
        sctp_cookie_param_t *retval;
        struct sctp_signed_cookie *cookie;
        struct sctp_signed_cookie *cookie;
        struct scatterlist sg;
        struct scatterlist sg;
        int headersize, bodysize;
        int headersize, bodysize;
        unsigned int keylen;
        unsigned int keylen;
        char *key;
        char *key;
 
 
        headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
        headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
        bodysize = sizeof(struct sctp_cookie)
        bodysize = sizeof(struct sctp_cookie)
                + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
                + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
 
 
        /* Pad out the cookie to a multiple to make the signature
        /* Pad out the cookie to a multiple to make the signature
         * functions simpler to write.
         * functions simpler to write.
         */
         */
        if (bodysize % SCTP_COOKIE_MULTIPLE)
        if (bodysize % SCTP_COOKIE_MULTIPLE)
                bodysize += SCTP_COOKIE_MULTIPLE
                bodysize += SCTP_COOKIE_MULTIPLE
                        - (bodysize % SCTP_COOKIE_MULTIPLE);
                        - (bodysize % SCTP_COOKIE_MULTIPLE);
        *cookie_len = headersize + bodysize;
        *cookie_len = headersize + bodysize;
 
 
        retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
        retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
 
 
        if (!retval) {
        if (!retval) {
                *cookie_len = 0;
                *cookie_len = 0;
                goto nodata;
                goto nodata;
        }
        }
 
 
        /* Clear this memory since we are sending this data structure
        /* Clear this memory since we are sending this data structure
         * out on the network.
         * out on the network.
         */
         */
        memset(retval, 0x00, *cookie_len);
        memset(retval, 0x00, *cookie_len);
        cookie = (struct sctp_signed_cookie *) retval->body;
        cookie = (struct sctp_signed_cookie *) retval->body;
 
 
        /* Set up the parameter header.  */
        /* Set up the parameter header.  */
        retval->p.type = SCTP_PARAM_STATE_COOKIE;
        retval->p.type = SCTP_PARAM_STATE_COOKIE;
        retval->p.length = htons(*cookie_len);
        retval->p.length = htons(*cookie_len);
 
 
        /* Copy the cookie part of the association itself.  */
        /* Copy the cookie part of the association itself.  */
        cookie->c = asoc->c;
        cookie->c = asoc->c;
        /* Save the raw address list length in the cookie. */
        /* Save the raw address list length in the cookie. */
        cookie->c.raw_addr_list_len = addrs_len;
        cookie->c.raw_addr_list_len = addrs_len;
 
 
        /* Set an expiration time for the cookie.  */
        /* Set an expiration time for the cookie.  */
        do_gettimeofday(&cookie->c.expiration);
        do_gettimeofday(&cookie->c.expiration);
        TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
        TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
 
 
        /* Copy the peer's init packet.  */
        /* Copy the peer's init packet.  */
        memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
        memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
               ntohs(init_chunk->chunk_hdr->length));
               ntohs(init_chunk->chunk_hdr->length));
 
 
        /* Copy the raw local address list of the association. */
        /* Copy the raw local address list of the association. */
        memcpy((__u8 *)&cookie->c.peer_init[0] +
        memcpy((__u8 *)&cookie->c.peer_init[0] +
               ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
               ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
 
 
        if (sctp_sk(ep->base.sk)->hmac) {
        if (sctp_sk(ep->base.sk)->hmac) {
                /* Sign the message.  */
                /* Sign the message.  */
                sg.page = virt_to_page(&cookie->c);
                sg.page = virt_to_page(&cookie->c);
                sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
                sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
                sg.length = bodysize;
                sg.length = bodysize;
                keylen = SCTP_SECRET_SIZE;
                keylen = SCTP_SECRET_SIZE;
                key = (char *)ep->secret_key[ep->current_key];
                key = (char *)ep->secret_key[ep->current_key];
 
 
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
                                 &sg, 1, cookie->signature);
                                 &sg, 1, cookie->signature);
        }
        }
 
 
nodata:
nodata:
        return retval;
        return retval;
}
}
 
 
/* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
/* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
struct sctp_association *sctp_unpack_cookie(
struct sctp_association *sctp_unpack_cookie(
        const struct sctp_endpoint *ep,
        const struct sctp_endpoint *ep,
        const struct sctp_association *asoc,
        const struct sctp_association *asoc,
        struct sctp_chunk *chunk, int gfp,
        struct sctp_chunk *chunk, int gfp,
        int *error, struct sctp_chunk **errp)
        int *error, struct sctp_chunk **errp)
{
{
        struct sctp_association *retval = NULL;
        struct sctp_association *retval = NULL;
        struct sctp_signed_cookie *cookie;
        struct sctp_signed_cookie *cookie;
        struct sctp_cookie *bear_cookie;
        struct sctp_cookie *bear_cookie;
        int headersize, bodysize, fixed_size;
        int headersize, bodysize, fixed_size;
        __u8 digest[SCTP_SIGNATURE_SIZE];
        __u8 digest[SCTP_SIGNATURE_SIZE];
        struct scatterlist sg;
        struct scatterlist sg;
        unsigned int keylen, len;
        unsigned int keylen, len;
        char *key;
        char *key;
        sctp_scope_t scope;
        sctp_scope_t scope;
        struct sk_buff *skb = chunk->skb;
        struct sk_buff *skb = chunk->skb;
 
 
        headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
        headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
        bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
        bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
        fixed_size = headersize + sizeof(struct sctp_cookie);
        fixed_size = headersize + sizeof(struct sctp_cookie);
 
 
        /* Verify that the chunk looks like it even has a cookie.
        /* Verify that the chunk looks like it even has a cookie.
         * There must be enough room for our cookie and our peer's
         * There must be enough room for our cookie and our peer's
         * INIT chunk.
         * INIT chunk.
         */
         */
        len = ntohs(chunk->chunk_hdr->length);
        len = ntohs(chunk->chunk_hdr->length);
        if (len < fixed_size + sizeof(struct sctp_chunkhdr))
        if (len < fixed_size + sizeof(struct sctp_chunkhdr))
                goto malformed;
                goto malformed;
 
 
        /* Verify that the cookie has been padded out. */
        /* Verify that the cookie has been padded out. */
        if (bodysize % SCTP_COOKIE_MULTIPLE)
        if (bodysize % SCTP_COOKIE_MULTIPLE)
                goto malformed;
                goto malformed;
 
 
        /* Process the cookie.  */
        /* Process the cookie.  */
        cookie = chunk->subh.cookie_hdr;
        cookie = chunk->subh.cookie_hdr;
        bear_cookie = &cookie->c;
        bear_cookie = &cookie->c;
 
 
        if (!sctp_sk(ep->base.sk)->hmac)
        if (!sctp_sk(ep->base.sk)->hmac)
                goto no_hmac;
                goto no_hmac;
 
 
        /* Check the signature.  */
        /* Check the signature.  */
        keylen = SCTP_SECRET_SIZE;
        keylen = SCTP_SECRET_SIZE;
        sg.page = virt_to_page(bear_cookie);
        sg.page = virt_to_page(bear_cookie);
        sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
        sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
        sg.length = bodysize;
        sg.length = bodysize;
        key = (char *)ep->secret_key[ep->current_key];
        key = (char *)ep->secret_key[ep->current_key];
 
 
        memset(digest, 0x00, sizeof(digest));
        memset(digest, 0x00, sizeof(digest));
        sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
        sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
                         1, digest);
                         1, digest);
 
 
        if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
        if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
                /* Try the previous key. */
                /* Try the previous key. */
                key = (char *)ep->secret_key[ep->last_key];
                key = (char *)ep->secret_key[ep->last_key];
                memset(digest, 0x00, sizeof(digest));
                memset(digest, 0x00, sizeof(digest));
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
                                 &sg, 1, digest);
                                 &sg, 1, digest);
 
 
                if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
                if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
                        /* Yikes!  Still bad signature! */
                        /* Yikes!  Still bad signature! */
                        *error = -SCTP_IERROR_BAD_SIG;
                        *error = -SCTP_IERROR_BAD_SIG;
                        goto fail;
                        goto fail;
                }
                }
        }
        }
 
 
no_hmac:
no_hmac:
        /* Check to see if the cookie is stale.  If there is already
        /* Check to see if the cookie is stale.  If there is already
         * an association, there is no need to check cookie's expiration
         * an association, there is no need to check cookie's expiration
         * for init collision case of lost COOKIE ACK.
         * for init collision case of lost COOKIE ACK.
         */
         */
        if (!asoc && tv_lt(bear_cookie->expiration, skb->stamp)) {
        if (!asoc && tv_lt(bear_cookie->expiration, skb->stamp)) {
                __u16 len;
                __u16 len;
                /*
                /*
                 * Section 3.3.10.3 Stale Cookie Error (3)
                 * Section 3.3.10.3 Stale Cookie Error (3)
                 *
                 *
                 * Cause of error
                 * Cause of error
                 * ---------------
                 * ---------------
                 * Stale Cookie Error:  Indicates the receipt of a valid State
                 * Stale Cookie Error:  Indicates the receipt of a valid State
                 * Cookie that has expired.
                 * Cookie that has expired.
                 */
                 */
                len = ntohs(chunk->chunk_hdr->length);
                len = ntohs(chunk->chunk_hdr->length);
                *errp = sctp_make_op_error_space(asoc, chunk, len);
                *errp = sctp_make_op_error_space(asoc, chunk, len);
                if (*errp) {
                if (*errp) {
                        suseconds_t usecs = (skb->stamp.tv_sec -
                        suseconds_t usecs = (skb->stamp.tv_sec -
                                bear_cookie->expiration.tv_sec) * 1000000L +
                                bear_cookie->expiration.tv_sec) * 1000000L +
                                skb->stamp.tv_usec -
                                skb->stamp.tv_usec -
                                bear_cookie->expiration.tv_usec;
                                bear_cookie->expiration.tv_usec;
 
 
                        usecs = htonl(usecs);
                        usecs = htonl(usecs);
                        sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
                        sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
                                        &usecs, sizeof(usecs));
                                        &usecs, sizeof(usecs));
                        *error = -SCTP_IERROR_STALE_COOKIE;
                        *error = -SCTP_IERROR_STALE_COOKIE;
                } else
                } else
                        *error = -SCTP_IERROR_NOMEM;
                        *error = -SCTP_IERROR_NOMEM;
 
 
                goto fail;
                goto fail;
        }
        }
 
 
        /* Make a new base association.  */
        /* Make a new base association.  */
        scope = sctp_scope(sctp_source(chunk));
        scope = sctp_scope(sctp_source(chunk));
        retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
        retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
        if (!retval) {
        if (!retval) {
                *error = -SCTP_IERROR_NOMEM;
                *error = -SCTP_IERROR_NOMEM;
                goto fail;
                goto fail;
        }
        }
 
 
        /* Set up our peer's port number.  */
        /* Set up our peer's port number.  */
        retval->peer.port = ntohs(chunk->sctp_hdr->source);
        retval->peer.port = ntohs(chunk->sctp_hdr->source);
 
 
        /* Populate the association from the cookie.  */
        /* Populate the association from the cookie.  */
        memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
        memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
 
 
        if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
        if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
                                                 GFP_ATOMIC) < 0) {
                                                 GFP_ATOMIC) < 0) {
                *error = -SCTP_IERROR_NOMEM;
                *error = -SCTP_IERROR_NOMEM;
                goto fail;
                goto fail;
        }
        }
 
 
        /* Also, add the destination address. */
        /* Also, add the destination address. */
        if (list_empty(&retval->base.bind_addr.address_list)) {
        if (list_empty(&retval->base.bind_addr.address_list)) {
                sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
                sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
                                   GFP_ATOMIC);
                                   GFP_ATOMIC);
        }
        }
 
 
        retval->next_tsn = retval->c.initial_tsn;
        retval->next_tsn = retval->c.initial_tsn;
        retval->ctsn_ack_point = retval->next_tsn - 1;
        retval->ctsn_ack_point = retval->next_tsn - 1;
        retval->addip_serial = retval->c.initial_tsn;
        retval->addip_serial = retval->c.initial_tsn;
 
 
        /* The INIT stuff will be done by the side effects.  */
        /* The INIT stuff will be done by the side effects.  */
        return retval;
        return retval;
 
 
fail:
fail:
        if (retval)
        if (retval)
                sctp_association_free(retval);
                sctp_association_free(retval);
 
 
        return NULL;
        return NULL;
 
 
malformed:
malformed:
        /* Yikes!  The packet is either corrupt or deliberately
        /* Yikes!  The packet is either corrupt or deliberately
         * malformed.
         * malformed.
         */
         */
        *error = -SCTP_IERROR_MALFORMED;
        *error = -SCTP_IERROR_MALFORMED;
        goto fail;
        goto fail;
}
}
 
 
/********************************************************************
/********************************************************************
 * 3rd Level Abstractions
 * 3rd Level Abstractions
 ********************************************************************/
 ********************************************************************/
 
 
struct __sctp_missing {
struct __sctp_missing {
        __u32 num_missing;
        __u32 num_missing;
        __u16 type;
        __u16 type;
}  __attribute__((packed));;
}  __attribute__((packed));;
 
 
/*
/*
 * Report a missing mandatory parameter.
 * Report a missing mandatory parameter.
 */
 */
static int sctp_process_missing_param(const struct sctp_association *asoc,
static int sctp_process_missing_param(const struct sctp_association *asoc,
                                      sctp_param_t paramtype,
                                      sctp_param_t paramtype,
                                      struct sctp_chunk *chunk,
                                      struct sctp_chunk *chunk,
                                      struct sctp_chunk **errp)
                                      struct sctp_chunk **errp)
{
{
        struct __sctp_missing report;
        struct __sctp_missing report;
        __u16 len;
        __u16 len;
 
 
        len = WORD_ROUND(sizeof(report));
        len = WORD_ROUND(sizeof(report));
 
 
        /* Make an ERROR chunk, preparing enough room for
        /* Make an ERROR chunk, preparing enough room for
         * returning multiple unknown parameters.
         * returning multiple unknown parameters.
         */
         */
        if (!*errp)
        if (!*errp)
                *errp = sctp_make_op_error_space(asoc, chunk, len);
                *errp = sctp_make_op_error_space(asoc, chunk, len);
 
 
        if (*errp) {
        if (*errp) {
                report.num_missing = htonl(1);
                report.num_missing = htonl(1);
                report.type = paramtype;
                report.type = paramtype;
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
                                &report, sizeof(report));
                                &report, sizeof(report));
        }
        }
 
 
        /* Stop processing this chunk. */
        /* Stop processing this chunk. */
        return 0;
        return 0;
}
}
 
 
/* Report an Invalid Mandatory Parameter.  */
/* Report an Invalid Mandatory Parameter.  */
static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
                                      struct sctp_chunk *chunk,
                                      struct sctp_chunk *chunk,
                                      struct sctp_chunk **errp)
                                      struct sctp_chunk **errp)
{
{
        /* Invalid Mandatory Parameter Error has no payload. */
        /* Invalid Mandatory Parameter Error has no payload. */
 
 
        if (!*errp)
        if (!*errp)
                *errp = sctp_make_op_error_space(asoc, chunk, 0);
                *errp = sctp_make_op_error_space(asoc, chunk, 0);
 
 
        if (*errp)
        if (*errp)
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
 
 
        /* Stop processing this chunk. */
        /* Stop processing this chunk. */
        return 0;
        return 0;
}
}
 
 
/* Do not attempt to handle the HOST_NAME parm.  However, do
/* Do not attempt to handle the HOST_NAME parm.  However, do
 * send back an indicator to the peer.
 * send back an indicator to the peer.
 */
 */
static int sctp_process_hn_param(const struct sctp_association *asoc,
static int sctp_process_hn_param(const struct sctp_association *asoc,
                                 union sctp_params param,
                                 union sctp_params param,
                                 struct sctp_chunk *chunk,
                                 struct sctp_chunk *chunk,
                                 struct sctp_chunk **errp)
                                 struct sctp_chunk **errp)
{
{
        __u16 len = ntohs(param.p->length);
        __u16 len = ntohs(param.p->length);
 
 
        /* Make an ERROR chunk. */
        /* Make an ERROR chunk. */
        if (!*errp)
        if (!*errp)
                *errp = sctp_make_op_error_space(asoc, chunk, len);
                *errp = sctp_make_op_error_space(asoc, chunk, len);
 
 
        if (*errp)
        if (*errp)
                sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
                sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
                                param.v, len);
                                param.v, len);
 
 
        /* Stop processing this chunk. */
        /* Stop processing this chunk. */
        return 0;
        return 0;
}
}
 
 
/* RFC 3.2.1 & the Implementers Guide 2.2.
/* RFC 3.2.1 & the Implementers Guide 2.2.
 *
 *
 * The Parameter Types are encoded such that the
 * The Parameter Types are encoded such that the
 * highest-order two bits specify the action that must be
 * highest-order two bits specify the action that must be
 * taken if the processing endpoint does not recognize the
 * taken if the processing endpoint does not recognize the
 * Parameter Type.
 * Parameter Type.
 *
 *
 * 00 - Stop processing this SCTP chunk and discard it,
 * 00 - Stop processing this SCTP chunk and discard it,
 *      do not process any further chunks within it.
 *      do not process any further chunks within it.
 *
 *
 * 01 - Stop processing this SCTP chunk and discard it,
 * 01 - Stop processing this SCTP chunk and discard it,
 *      do not process any further chunks within it, and report
 *      do not process any further chunks within it, and report
 *      the unrecognized parameter in an 'Unrecognized
 *      the unrecognized parameter in an 'Unrecognized
 *      Parameter Type' (in either an ERROR or in the INIT ACK).
 *      Parameter Type' (in either an ERROR or in the INIT ACK).
 *
 *
 * 10 - Skip this parameter and continue processing.
 * 10 - Skip this parameter and continue processing.
 *
 *
 * 11 - Skip this parameter and continue processing but
 * 11 - Skip this parameter and continue processing but
 *      report the unrecognized parameter in an
 *      report the unrecognized parameter in an
 *      'Unrecognized Parameter Type' (in either an ERROR or in
 *      'Unrecognized Parameter Type' (in either an ERROR or in
 *      the INIT ACK).
 *      the INIT ACK).
 *
 *
 * Return value:
 * Return value:
 *      0 - discard the chunk
 *      0 - discard the chunk
 *      1 - continue with the chunk
 *      1 - continue with the chunk
 */
 */
static int sctp_process_unk_param(const struct sctp_association *asoc,
static int sctp_process_unk_param(const struct sctp_association *asoc,
                                  union sctp_params param,
                                  union sctp_params param,
                                  struct sctp_chunk *chunk,
                                  struct sctp_chunk *chunk,
                                  struct sctp_chunk **errp)
                                  struct sctp_chunk **errp)
{
{
        int retval = 1;
        int retval = 1;
 
 
        switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
        switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
        case SCTP_PARAM_ACTION_DISCARD:
        case SCTP_PARAM_ACTION_DISCARD:
                retval =  0;
                retval =  0;
                break;
                break;
        case SCTP_PARAM_ACTION_DISCARD_ERR:
        case SCTP_PARAM_ACTION_DISCARD_ERR:
                retval =  0;
                retval =  0;
                /* Make an ERROR chunk, preparing enough room for
                /* Make an ERROR chunk, preparing enough room for
                 * returning multiple unknown parameters.
                 * returning multiple unknown parameters.
                 */
                 */
                if (NULL == *errp)
                if (NULL == *errp)
                        *errp = sctp_make_op_error_space(asoc, chunk,
                        *errp = sctp_make_op_error_space(asoc, chunk,
                                        ntohs(chunk->chunk_hdr->length));
                                        ntohs(chunk->chunk_hdr->length));
 
 
                if (*errp)
                if (*errp)
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
                                        param.v,
                                        param.v,
                                        WORD_ROUND(ntohs(param.p->length)));
                                        WORD_ROUND(ntohs(param.p->length)));
 
 
                break;
                break;
        case SCTP_PARAM_ACTION_SKIP:
        case SCTP_PARAM_ACTION_SKIP:
                break;
                break;
        case SCTP_PARAM_ACTION_SKIP_ERR:
        case SCTP_PARAM_ACTION_SKIP_ERR:
                /* Make an ERROR chunk, preparing enough room for
                /* Make an ERROR chunk, preparing enough room for
                 * returning multiple unknown parameters.
                 * returning multiple unknown parameters.
                 */
                 */
                if (NULL == *errp)
                if (NULL == *errp)
                        *errp = sctp_make_op_error_space(asoc, chunk,
                        *errp = sctp_make_op_error_space(asoc, chunk,
                                        ntohs(chunk->chunk_hdr->length));
                                        ntohs(chunk->chunk_hdr->length));
 
 
                if (*errp) {
                if (*errp) {
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
                                        param.v,
                                        param.v,
                                        WORD_ROUND(ntohs(param.p->length)));
                                        WORD_ROUND(ntohs(param.p->length)));
                } else {
                } else {
                        /* If there is no memory for generating the ERROR
                        /* If there is no memory for generating the ERROR
                         * report as specified, an ABORT will be triggered
                         * report as specified, an ABORT will be triggered
                         * to the peer and the association won't be
                         * to the peer and the association won't be
                         * established.
                         * established.
                         */
                         */
                        retval = 0;
                        retval = 0;
                }
                }
 
 
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
 
 
        return retval;
        return retval;
}
}
 
 
/* Find unrecognized parameters in the chunk.
/* Find unrecognized parameters in the chunk.
 * Return values:
 * Return values:
 *      0 - discard the chunk
 *      0 - discard the chunk
 *      1 - continue with the chunk
 *      1 - continue with the chunk
 */
 */
static int sctp_verify_param(const struct sctp_association *asoc,
static int sctp_verify_param(const struct sctp_association *asoc,
                             union sctp_params param,
                             union sctp_params param,
                             sctp_cid_t cid,
                             sctp_cid_t cid,
                             struct sctp_chunk *chunk,
                             struct sctp_chunk *chunk,
                             struct sctp_chunk **err_chunk)
                             struct sctp_chunk **err_chunk)
{
{
        int retval = 1;
        int retval = 1;
 
 
        /* FIXME - This routine is not looking at each parameter per the
        /* FIXME - This routine is not looking at each parameter per the
         * chunk type, i.e., unrecognized parameters should be further
         * chunk type, i.e., unrecognized parameters should be further
         * identified based on the chunk id.
         * identified based on the chunk id.
         */
         */
 
 
        switch (param.p->type) {
        switch (param.p->type) {
        case SCTP_PARAM_IPV4_ADDRESS:
        case SCTP_PARAM_IPV4_ADDRESS:
        case SCTP_PARAM_IPV6_ADDRESS:
        case SCTP_PARAM_IPV6_ADDRESS:
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
        case SCTP_PARAM_STATE_COOKIE:
        case SCTP_PARAM_STATE_COOKIE:
        case SCTP_PARAM_HEARTBEAT_INFO:
        case SCTP_PARAM_HEARTBEAT_INFO:
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
        case SCTP_PARAM_ECN_CAPABLE:
        case SCTP_PARAM_ECN_CAPABLE:
                break;
                break;
 
 
        case SCTP_PARAM_HOST_NAME_ADDRESS:
        case SCTP_PARAM_HOST_NAME_ADDRESS:
                /* Tell the peer, we won't support this param.  */
                /* Tell the peer, we won't support this param.  */
                return sctp_process_hn_param(asoc, param, chunk, err_chunk);
                return sctp_process_hn_param(asoc, param, chunk, err_chunk);
        default:
        default:
                SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
                SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
                                ntohs(param.p->type), cid);
                                ntohs(param.p->type), cid);
                return sctp_process_unk_param(asoc, param, chunk, err_chunk);
                return sctp_process_unk_param(asoc, param, chunk, err_chunk);
 
 
                break;
                break;
        }
        }
        return retval;
        return retval;
}
}
 
 
/* Verify the INIT packet before we process it.  */
/* Verify the INIT packet before we process it.  */
int sctp_verify_init(const struct sctp_association *asoc,
int sctp_verify_init(const struct sctp_association *asoc,
                     sctp_cid_t cid,
                     sctp_cid_t cid,
                     sctp_init_chunk_t *peer_init,
                     sctp_init_chunk_t *peer_init,
                     struct sctp_chunk *chunk,
                     struct sctp_chunk *chunk,
                     struct sctp_chunk **errp)
                     struct sctp_chunk **errp)
{
{
        union sctp_params param;
        union sctp_params param;
        int has_cookie = 0;
        int has_cookie = 0;
 
 
        /* Verify stream values are non-zero. */
        /* Verify stream values are non-zero. */
        if ((0 == peer_init->init_hdr.num_outbound_streams) ||
        if ((0 == peer_init->init_hdr.num_outbound_streams) ||
            (0 == peer_init->init_hdr.num_inbound_streams)) {
            (0 == peer_init->init_hdr.num_inbound_streams)) {
 
 
                sctp_process_inv_mandatory(asoc, chunk, errp);
                sctp_process_inv_mandatory(asoc, chunk, errp);
                return 0;
                return 0;
        }
        }
 
 
        /* Check for missing mandatory parameters.  */
        /* Check for missing mandatory parameters.  */
        sctp_walk_params(param, peer_init, init_hdr.params) {
        sctp_walk_params(param, peer_init, init_hdr.params) {
 
 
                if (SCTP_PARAM_STATE_COOKIE == param.p->type)
                if (SCTP_PARAM_STATE_COOKIE == param.p->type)
                        has_cookie = 1;
                        has_cookie = 1;
 
 
        } /* for (loop through all parameters) */
        } /* for (loop through all parameters) */
 
 
        /* The only missing mandatory param possible today is
        /* The only missing mandatory param possible today is
         * the state cookie for an INIT-ACK chunk.
         * the state cookie for an INIT-ACK chunk.
         */
         */
        if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
        if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
                sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
                sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
                                           chunk, errp);
                                           chunk, errp);
                return 0;
                return 0;
        }
        }
 
 
        /* Find unrecognized parameters. */
        /* Find unrecognized parameters. */
 
 
        sctp_walk_params(param, peer_init, init_hdr.params) {
        sctp_walk_params(param, peer_init, init_hdr.params) {
 
 
                if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
                if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
                        if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
                        if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
                                return 0;
                                return 0;
                        else
                        else
                                return 1;
                                return 1;
                }
                }
 
 
        } /* for (loop through all parameters) */
        } /* for (loop through all parameters) */
 
 
        return 1;
        return 1;
}
}
 
 
/* Unpack the parameters in an INIT packet into an association.
/* Unpack the parameters in an INIT packet into an association.
 * Returns 0 on failure, else success.
 * Returns 0 on failure, else success.
 * FIXME:  This is an association method.
 * FIXME:  This is an association method.
 */
 */
int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
                      const union sctp_addr *peer_addr,
                      const union sctp_addr *peer_addr,
                      sctp_init_chunk_t *peer_init, int gfp)
                      sctp_init_chunk_t *peer_init, int gfp)
{
{
        union sctp_params param;
        union sctp_params param;
        struct sctp_transport *transport;
        struct sctp_transport *transport;
        struct list_head *pos, *temp;
        struct list_head *pos, *temp;
        char *cookie;
        char *cookie;
 
 
        /* We must include the address that the INIT packet came from.
        /* We must include the address that the INIT packet came from.
         * This is the only address that matters for an INIT packet.
         * This is the only address that matters for an INIT packet.
         * When processing a COOKIE ECHO, we retrieve the from address
         * When processing a COOKIE ECHO, we retrieve the from address
         * of the INIT from the cookie.
         * of the INIT from the cookie.
         */
         */
 
 
        /* This implementation defaults to making the first transport
        /* This implementation defaults to making the first transport
         * added as the primary transport.  The source address seems to
         * added as the primary transport.  The source address seems to
         * be a a better choice than any of the embedded addresses.
         * be a a better choice than any of the embedded addresses.
         */
         */
        if (peer_addr)
        if (peer_addr)
                if(!sctp_assoc_add_peer(asoc, peer_addr, gfp))
                if(!sctp_assoc_add_peer(asoc, peer_addr, gfp))
                        goto nomem;
                        goto nomem;
 
 
        /* Process the initialization parameters.  */
        /* Process the initialization parameters.  */
 
 
        sctp_walk_params(param, peer_init, init_hdr.params) {
        sctp_walk_params(param, peer_init, init_hdr.params) {
 
 
                if (!sctp_process_param(asoc, param, peer_addr, gfp))
                if (!sctp_process_param(asoc, param, peer_addr, gfp))
                        goto clean_up;
                        goto clean_up;
        }
        }
 
 
        /* The fixed INIT headers are always in network byte
        /* The fixed INIT headers are always in network byte
         * order.
         * order.
         */
         */
        asoc->peer.i.init_tag =
        asoc->peer.i.init_tag =
                ntohl(peer_init->init_hdr.init_tag);
                ntohl(peer_init->init_hdr.init_tag);
        asoc->peer.i.a_rwnd =
        asoc->peer.i.a_rwnd =
                ntohl(peer_init->init_hdr.a_rwnd);
                ntohl(peer_init->init_hdr.a_rwnd);
        asoc->peer.i.num_outbound_streams =
        asoc->peer.i.num_outbound_streams =
                ntohs(peer_init->init_hdr.num_outbound_streams);
                ntohs(peer_init->init_hdr.num_outbound_streams);
        asoc->peer.i.num_inbound_streams =
        asoc->peer.i.num_inbound_streams =
                ntohs(peer_init->init_hdr.num_inbound_streams);
                ntohs(peer_init->init_hdr.num_inbound_streams);
        asoc->peer.i.initial_tsn =
        asoc->peer.i.initial_tsn =
                ntohl(peer_init->init_hdr.initial_tsn);
                ntohl(peer_init->init_hdr.initial_tsn);
 
 
        /* Apply the upper bounds for output streams based on peer's
        /* Apply the upper bounds for output streams based on peer's
         * number of inbound streams.
         * number of inbound streams.
         */
         */
        if (asoc->c.sinit_num_ostreams  >
        if (asoc->c.sinit_num_ostreams  >
            ntohs(peer_init->init_hdr.num_inbound_streams)) {
            ntohs(peer_init->init_hdr.num_inbound_streams)) {
                asoc->c.sinit_num_ostreams =
                asoc->c.sinit_num_ostreams =
                        ntohs(peer_init->init_hdr.num_inbound_streams);
                        ntohs(peer_init->init_hdr.num_inbound_streams);
        }
        }
 
 
        if (asoc->c.sinit_max_instreams >
        if (asoc->c.sinit_max_instreams >
            ntohs(peer_init->init_hdr.num_outbound_streams)) {
            ntohs(peer_init->init_hdr.num_outbound_streams)) {
                asoc->c.sinit_max_instreams =
                asoc->c.sinit_max_instreams =
                        ntohs(peer_init->init_hdr.num_outbound_streams);
                        ntohs(peer_init->init_hdr.num_outbound_streams);
        }
        }
 
 
        /* Copy Initiation tag from INIT to VT_peer in cookie.   */
        /* Copy Initiation tag from INIT to VT_peer in cookie.   */
        asoc->c.peer_vtag = asoc->peer.i.init_tag;
        asoc->c.peer_vtag = asoc->peer.i.init_tag;
 
 
        /* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
        /* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
        asoc->peer.rwnd = asoc->peer.i.a_rwnd;
        asoc->peer.rwnd = asoc->peer.i.a_rwnd;
 
 
        /* Copy cookie in case we need to resend COOKIE-ECHO. */
        /* Copy cookie in case we need to resend COOKIE-ECHO. */
        cookie = asoc->peer.cookie;
        cookie = asoc->peer.cookie;
        if (cookie) {
        if (cookie) {
                asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
                asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
                if (!asoc->peer.cookie)
                if (!asoc->peer.cookie)
                        goto clean_up;
                        goto clean_up;
                memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
                memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
        }
        }
 
 
        /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
        /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
         * high (for example, implementations MAY use the size of the receiver
         * high (for example, implementations MAY use the size of the receiver
         * advertised window).
         * advertised window).
         */
         */
        list_for_each(pos, &asoc->peer.transport_addr_list) {
        list_for_each(pos, &asoc->peer.transport_addr_list) {
                transport = list_entry(pos, struct sctp_transport, transports);
                transport = list_entry(pos, struct sctp_transport, transports);
                transport->ssthresh = asoc->peer.i.a_rwnd;
                transport->ssthresh = asoc->peer.i.a_rwnd;
        }
        }
 
 
        /* Set up the TSN tracking pieces.  */
        /* Set up the TSN tracking pieces.  */
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
                         asoc->peer.i.initial_tsn);
                         asoc->peer.i.initial_tsn);
 
 
        /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
        /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
         *
         *
         * The stream sequence number in all the streams shall start
         * The stream sequence number in all the streams shall start
         * from 0 when the association is established.  Also, when the
         * from 0 when the association is established.  Also, when the
         * stream sequence number reaches the value 65535 the next
         * stream sequence number reaches the value 65535 the next
         * stream sequence number shall be set to 0.
         * stream sequence number shall be set to 0.
         */
         */
 
 
        /* Allocate storage for the negotiated streams if it is not a temporary          * association.
        /* Allocate storage for the negotiated streams if it is not a temporary          * association.
         */
         */
        if (!asoc->temp) {
        if (!asoc->temp) {
                asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
                asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
                                               asoc->c.sinit_num_ostreams, gfp);
                                               asoc->c.sinit_num_ostreams, gfp);
                if (!asoc->ssnmap)
                if (!asoc->ssnmap)
                        goto nomem_ssnmap;
                        goto nomem_ssnmap;
        }
        }
 
 
        /* ADDIP Section 4.1 ASCONF Chunk Procedures
        /* ADDIP Section 4.1 ASCONF Chunk Procedures
         *
         *
         * When an endpoint has an ASCONF signaled change to be sent to the
         * When an endpoint has an ASCONF signaled change to be sent to the
         * remote endpoint it should do the following:
         * remote endpoint it should do the following:
         * ...
         * ...
         * A2) A serial number should be assigned to the Chunk. The serial
         * A2) A serial number should be assigned to the Chunk. The serial
         * number should be a monotonically increasing number. All serial
         * number should be a monotonically increasing number. All serial
         * numbers are defined to be initialized at the start of the
         * numbers are defined to be initialized at the start of the
         * association to the same value as the Initial TSN.
         * association to the same value as the Initial TSN.
         */
         */
        asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
        asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
        return 1;
        return 1;
 
 
nomem_ssnmap:
nomem_ssnmap:
clean_up:
clean_up:
        /* Release the transport structures. */
        /* Release the transport structures. */
        list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
        list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
                transport = list_entry(pos, struct sctp_transport, transports);
                transport = list_entry(pos, struct sctp_transport, transports);
                list_del_init(pos);
                list_del_init(pos);
                sctp_transport_free(transport);
                sctp_transport_free(transport);
        }
        }
nomem:
nomem:
        return 0;
        return 0;
}
}
 
 
 
 
/* Update asoc with the option described in param.
/* Update asoc with the option described in param.
 *
 *
 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
 *
 *
 * asoc is the association to update.
 * asoc is the association to update.
 * param is the variable length parameter to use for update.
 * param is the variable length parameter to use for update.
 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
 * If the current packet is an INIT we want to minimize the amount of
 * If the current packet is an INIT we want to minimize the amount of
 * work we do.  In particular, we should not build transport
 * work we do.  In particular, we should not build transport
 * structures for the addresses.
 * structures for the addresses.
 */
 */
int sctp_process_param(struct sctp_association *asoc, union sctp_params param,
int sctp_process_param(struct sctp_association *asoc, union sctp_params param,
                       const union sctp_addr *peer_addr, int gfp)
                       const union sctp_addr *peer_addr, int gfp)
{
{
        union sctp_addr addr;
        union sctp_addr addr;
        int i;
        int i;
        __u16 sat;
        __u16 sat;
        int retval = 1;
        int retval = 1;
        sctp_scope_t scope;
        sctp_scope_t scope;
        time_t stale;
        time_t stale;
        struct sctp_af *af;
        struct sctp_af *af;
 
 
        /* We maintain all INIT parameters in network byte order all the
        /* We maintain all INIT parameters in network byte order all the
         * time.  This allows us to not worry about whether the parameters
         * time.  This allows us to not worry about whether the parameters
         * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
         * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
         */
         */
        switch (param.p->type) {
        switch (param.p->type) {
        case SCTP_PARAM_IPV6_ADDRESS:
        case SCTP_PARAM_IPV6_ADDRESS:
                if (PF_INET6 != asoc->base.sk->sk_family)
                if (PF_INET6 != asoc->base.sk->sk_family)
                        break;
                        break;
                /* Fall through. */
                /* Fall through. */
        case SCTP_PARAM_IPV4_ADDRESS:
        case SCTP_PARAM_IPV4_ADDRESS:
                af = sctp_get_af_specific(param_type2af(param.p->type));
                af = sctp_get_af_specific(param_type2af(param.p->type));
                af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
                af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
                scope = sctp_scope(peer_addr);
                scope = sctp_scope(peer_addr);
                if (sctp_in_scope(&addr, scope))
                if (sctp_in_scope(&addr, scope))
                        if (!sctp_assoc_add_peer(asoc, &addr, gfp))
                        if (!sctp_assoc_add_peer(asoc, &addr, gfp))
                                return 0;
                                return 0;
                break;
                break;
 
 
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
                if (!sctp_cookie_preserve_enable)
                if (!sctp_cookie_preserve_enable)
                        break;
                        break;
 
 
                stale = ntohl(param.life->lifespan_increment);
                stale = ntohl(param.life->lifespan_increment);
 
 
                /* Suggested Cookie Life span increment's unit is msec,
                /* Suggested Cookie Life span increment's unit is msec,
                 * (1/1000sec).
                 * (1/1000sec).
                 */
                 */
                asoc->cookie_life.tv_sec += stale / 1000;
                asoc->cookie_life.tv_sec += stale / 1000;
                asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
                asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
                break;
                break;
 
 
        case SCTP_PARAM_HOST_NAME_ADDRESS:
        case SCTP_PARAM_HOST_NAME_ADDRESS:
                SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
                SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
                break;
                break;
 
 
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
                /* Turn off the default values first so we'll know which
                /* Turn off the default values first so we'll know which
                 * ones are really set by the peer.
                 * ones are really set by the peer.
                 */
                 */
                asoc->peer.ipv4_address = 0;
                asoc->peer.ipv4_address = 0;
                asoc->peer.ipv6_address = 0;
                asoc->peer.ipv6_address = 0;
 
 
                /* Cycle through address types; avoid divide by 0. */
                /* Cycle through address types; avoid divide by 0. */
                sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
                sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
                if (sat)
                if (sat)
                        sat /= sizeof(__u16);
                        sat /= sizeof(__u16);
 
 
                for (i = 0; i < sat; ++i) {
                for (i = 0; i < sat; ++i) {
                        switch (param.sat->types[i]) {
                        switch (param.sat->types[i]) {
                        case SCTP_PARAM_IPV4_ADDRESS:
                        case SCTP_PARAM_IPV4_ADDRESS:
                                asoc->peer.ipv4_address = 1;
                                asoc->peer.ipv4_address = 1;
                                break;
                                break;
 
 
                        case SCTP_PARAM_IPV6_ADDRESS:
                        case SCTP_PARAM_IPV6_ADDRESS:
                                asoc->peer.ipv6_address = 1;
                                asoc->peer.ipv6_address = 1;
                                break;
                                break;
 
 
                        case SCTP_PARAM_HOST_NAME_ADDRESS:
                        case SCTP_PARAM_HOST_NAME_ADDRESS:
                                asoc->peer.hostname_address = 1;
                                asoc->peer.hostname_address = 1;
                                break;
                                break;
 
 
                        default: /* Just ignore anything else.  */
                        default: /* Just ignore anything else.  */
                                break;
                                break;
                        };
                        };
                }
                }
                break;
                break;
 
 
        case SCTP_PARAM_STATE_COOKIE:
        case SCTP_PARAM_STATE_COOKIE:
                asoc->peer.cookie_len =
                asoc->peer.cookie_len =
                        ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
                        ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
                asoc->peer.cookie = param.cookie->body;
                asoc->peer.cookie = param.cookie->body;
                break;
                break;
 
 
        case SCTP_PARAM_HEARTBEAT_INFO:
        case SCTP_PARAM_HEARTBEAT_INFO:
                /* Would be odd to receive, but it causes no problems. */
                /* Would be odd to receive, but it causes no problems. */
                break;
                break;
 
 
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
                /* Rejected during verify stage. */
                /* Rejected during verify stage. */
                break;
                break;
 
 
        case SCTP_PARAM_ECN_CAPABLE:
        case SCTP_PARAM_ECN_CAPABLE:
                asoc->peer.ecn_capable = 1;
                asoc->peer.ecn_capable = 1;
                break;
                break;
 
 
        default:
        default:
                /* Any unrecognized parameters should have been caught
                /* Any unrecognized parameters should have been caught
                 * and handled by sctp_verify_param() which should be
                 * and handled by sctp_verify_param() which should be
                 * called prior to this routine.  Simply log the error
                 * called prior to this routine.  Simply log the error
                 * here.
                 * here.
                 */
                 */
                SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
                SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
                                  ntohs(param.p->type), asoc);
                                  ntohs(param.p->type), asoc);
                break;
                break;
        };
        };
 
 
        return retval;
        return retval;
}
}
 
 
/* Select a new verification tag.  */
/* Select a new verification tag.  */
__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
{
{
        /* I believe that this random number generator complies with RFC1750.
        /* I believe that this random number generator complies with RFC1750.
         * A tag of 0 is reserved for special cases (e.g. INIT).
         * A tag of 0 is reserved for special cases (e.g. INIT).
         */
         */
        __u32 x;
        __u32 x;
 
 
        do {
        do {
                get_random_bytes(&x, sizeof(__u32));
                get_random_bytes(&x, sizeof(__u32));
        } while (x == 0);
        } while (x == 0);
 
 
        return x;
        return x;
}
}
 
 
/* Select an initial TSN to send during startup.  */
/* Select an initial TSN to send during startup.  */
__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
{
{
        __u32 retval;
        __u32 retval;
 
 
        get_random_bytes(&retval, sizeof(__u32));
        get_random_bytes(&retval, sizeof(__u32));
        return retval;
        return retval;
}
}
 
 
/*
/*
 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
 *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Serial Number                           |
 *     |                       Serial Number                           |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                    Address Parameter                          |
 *     |                    Address Parameter                          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                     ASCONF Parameter #1                       |
 *     |                     ASCONF Parameter #1                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     \                                                               \
 *     \                                                               \
 *     /                             ....                              /
 *     /                             ....                              /
 *     \                                                               \
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                     ASCONF Parameter #N                       |
 *     |                     ASCONF Parameter #N                       |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * Address Parameter and other parameter will not be wrapped in this function
 * Address Parameter and other parameter will not be wrapped in this function
 */
 */
struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
                                    union sctp_addr *addr, int vparam_len)
                                    union sctp_addr *addr, int vparam_len)
{
{
        sctp_addiphdr_t asconf;
        sctp_addiphdr_t asconf;
        struct sctp_chunk *retval;
        struct sctp_chunk *retval;
        int length = sizeof(asconf) + vparam_len;
        int length = sizeof(asconf) + vparam_len;
        union sctp_addr_param addrparam;
        union sctp_addr_param addrparam;
        int addrlen;
        int addrlen;
        struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
        struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
 
 
        addrlen = af->to_addr_param(addr, &addrparam);
        addrlen = af->to_addr_param(addr, &addrparam);
        if (!addrlen)
        if (!addrlen)
                return NULL;
                return NULL;
        length += addrlen;
        length += addrlen;
 
 
        /* Create the chunk.  */
        /* Create the chunk.  */
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
        if (!retval)
        if (!retval)
                return NULL;
                return NULL;
 
 
        asconf.serial = htonl(asoc->addip_serial++);
        asconf.serial = htonl(asoc->addip_serial++);
 
 
        retval->subh.addip_hdr =
        retval->subh.addip_hdr =
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
        retval->param_hdr.v =
        retval->param_hdr.v =
                sctp_addto_chunk(retval, addrlen, &addrparam);
                sctp_addto_chunk(retval, addrlen, &addrparam);
 
 
        return retval;
        return retval;
}
}
 
 
/* ADDIP
/* ADDIP
 * 3.2.1 Add IP Address
 * 3.2.1 Add IP Address
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |        Type = 0xC001          |    Length = Variable          |
 *     |        Type = 0xC001          |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * 3.2.2 Delete IP Address
 * 3.2.2 Delete IP Address
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |        Type = 0xC002          |    Length = Variable          |
 *     |        Type = 0xC002          |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 */
 */
struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
                                              union sctp_addr         *laddr,
                                              union sctp_addr         *laddr,
                                              struct sockaddr         *addrs,
                                              struct sockaddr         *addrs,
                                              int                     addrcnt,
                                              int                     addrcnt,
                                              __u16                   flags)
                                              __u16                   flags)
{
{
        sctp_addip_param_t      param;
        sctp_addip_param_t      param;
        struct sctp_chunk       *retval;
        struct sctp_chunk       *retval;
        union sctp_addr_param   addr_param;
        union sctp_addr_param   addr_param;
        union sctp_addr         *addr;
        union sctp_addr         *addr;
        void                    *addr_buf;
        void                    *addr_buf;
        struct sctp_af          *af;
        struct sctp_af          *af;
        int                     paramlen = sizeof(param);
        int                     paramlen = sizeof(param);
        int                     addr_param_len = 0;
        int                     addr_param_len = 0;
        int                     totallen = 0;
        int                     totallen = 0;
        int                     i;
        int                     i;
 
 
        /* Get total length of all the address parameters. */
        /* Get total length of all the address parameters. */
        addr_buf = addrs;
        addr_buf = addrs;
        for (i = 0; i < addrcnt; i++) {
        for (i = 0; i < addrcnt; i++) {
                addr = (union sctp_addr *)addr_buf;
                addr = (union sctp_addr *)addr_buf;
                af = sctp_get_af_specific(addr->v4.sin_family);
                af = sctp_get_af_specific(addr->v4.sin_family);
                addr_param_len = af->to_addr_param(addr, &addr_param);
                addr_param_len = af->to_addr_param(addr, &addr_param);
 
 
                totallen += paramlen;
                totallen += paramlen;
                totallen += addr_param_len;
                totallen += addr_param_len;
 
 
                addr_buf += af->sockaddr_len;
                addr_buf += af->sockaddr_len;
        }
        }
 
 
        /* Create an asconf chunk with the required length. */
        /* Create an asconf chunk with the required length. */
        retval = sctp_make_asconf(asoc, laddr, totallen);
        retval = sctp_make_asconf(asoc, laddr, totallen);
        if (!retval)
        if (!retval)
                return NULL;
                return NULL;
 
 
        /* Add the address parameters to the asconf chunk. */
        /* Add the address parameters to the asconf chunk. */
        addr_buf = addrs;
        addr_buf = addrs;
        for (i = 0; i < addrcnt; i++) {
        for (i = 0; i < addrcnt; i++) {
                addr = (union sctp_addr *)addr_buf;
                addr = (union sctp_addr *)addr_buf;
                af = sctp_get_af_specific(addr->v4.sin_family);
                af = sctp_get_af_specific(addr->v4.sin_family);
                addr_param_len = af->to_addr_param(addr, &addr_param);
                addr_param_len = af->to_addr_param(addr, &addr_param);
                param.param_hdr.type = flags;
                param.param_hdr.type = flags;
                param.param_hdr.length = htons(paramlen + addr_param_len);
                param.param_hdr.length = htons(paramlen + addr_param_len);
                param.crr_id = i;
                param.crr_id = i;
 
 
                sctp_addto_chunk(retval, paramlen, &param);
                sctp_addto_chunk(retval, paramlen, &param);
                sctp_addto_chunk(retval, addr_param_len, &addr_param);
                sctp_addto_chunk(retval, addr_param_len, &addr_param);
 
 
                addr_buf += af->sockaddr_len;
                addr_buf += af->sockaddr_len;
        }
        }
        return retval;
        return retval;
}
}
 
 
/* ADDIP
/* ADDIP
 * 3.2.4 Set Primary IP Address
 * 3.2.4 Set Primary IP Address
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |        Type =0xC004           |    Length = Variable          |
 *     |        Type =0xC004           |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * Create an ASCONF chunk with Set Primary IP address parameter.
 * Create an ASCONF chunk with Set Primary IP address parameter.
 */
 */
struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
                                             union sctp_addr *addr)
                                             union sctp_addr *addr)
{
{
        sctp_addip_param_t      param;
        sctp_addip_param_t      param;
        struct sctp_chunk       *retval;
        struct sctp_chunk       *retval;
        int                     len = sizeof(param);
        int                     len = sizeof(param);
        union sctp_addr_param   addrparam;
        union sctp_addr_param   addrparam;
        int                     addrlen;
        int                     addrlen;
        struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
        struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
 
 
        addrlen = af->to_addr_param(addr, &addrparam);
        addrlen = af->to_addr_param(addr, &addrparam);
        if (!addrlen)
        if (!addrlen)
                return NULL;
                return NULL;
        len += addrlen;
        len += addrlen;
 
 
        /* Create the chunk and make asconf header. */
        /* Create the chunk and make asconf header. */
        retval = sctp_make_asconf(asoc, addr, len);
        retval = sctp_make_asconf(asoc, addr, len);
        if (!retval)
        if (!retval)
                return NULL;
                return NULL;
 
 
        param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
        param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
        param.param_hdr.length = htons(len);
        param.param_hdr.length = htons(len);
        param.crr_id = 0;
        param.crr_id = 0;
 
 
        sctp_addto_chunk(retval, sizeof(param), &param);
        sctp_addto_chunk(retval, sizeof(param), &param);
        sctp_addto_chunk(retval, addrlen, &addrparam);
        sctp_addto_chunk(retval, addrlen, &addrparam);
 
 
        return retval;
        return retval;
}
}
 
 
/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
 *      0                   1                   2                   3
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
 *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Serial Number                           |
 *     |                       Serial Number                           |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                 ASCONF Parameter Response#1                   |
 *     |                 ASCONF Parameter Response#1                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     \                                                               \
 *     \                                                               \
 *     /                             ....                              /
 *     /                             ....                              /
 *     \                                                               \
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                 ASCONF Parameter Response#N                   |
 *     |                 ASCONF Parameter Response#N                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
 */
 */
struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
                                        __u32 serial, int vparam_len)
                                        __u32 serial, int vparam_len)
{
{
        sctp_addiphdr_t         asconf;
        sctp_addiphdr_t         asconf;
        struct sctp_chunk       *retval;
        struct sctp_chunk       *retval;
        int                     length = sizeof(asconf) + vparam_len;
        int                     length = sizeof(asconf) + vparam_len;
 
 
        /* Create the chunk.  */
        /* Create the chunk.  */
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
        if (!retval)
        if (!retval)
                return NULL;
                return NULL;
 
 
        asconf.serial = htonl(serial);
        asconf.serial = htonl(serial);
 
 
        retval->subh.addip_hdr =
        retval->subh.addip_hdr =
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
 
 
        return retval;
        return retval;
}
}
 
 
/* Add response parameters to an ASCONF_ACK chunk. */
/* Add response parameters to an ASCONF_ACK chunk. */
static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
                              __u16 err_code, sctp_addip_param_t *asconf_param)
                              __u16 err_code, sctp_addip_param_t *asconf_param)
{
{
        sctp_addip_param_t      ack_param;
        sctp_addip_param_t      ack_param;
        sctp_errhdr_t           err_param;
        sctp_errhdr_t           err_param;
        int                     asconf_param_len = 0;
        int                     asconf_param_len = 0;
        int                     err_param_len = 0;
        int                     err_param_len = 0;
        __u16                   response_type;
        __u16                   response_type;
 
 
        if (SCTP_ERROR_NO_ERROR == err_code) {
        if (SCTP_ERROR_NO_ERROR == err_code) {
                response_type = SCTP_PARAM_SUCCESS_REPORT;
                response_type = SCTP_PARAM_SUCCESS_REPORT;
        } else {
        } else {
                response_type = SCTP_PARAM_ERR_CAUSE;
                response_type = SCTP_PARAM_ERR_CAUSE;
                err_param_len = sizeof(err_param);
                err_param_len = sizeof(err_param);
                if (asconf_param)
                if (asconf_param)
                        asconf_param_len =
                        asconf_param_len =
                                 ntohs(asconf_param->param_hdr.length);
                                 ntohs(asconf_param->param_hdr.length);
        }
        }
 
 
        /* Add Success Indication or Error Cause Indication parameter. */
        /* Add Success Indication or Error Cause Indication parameter. */
        ack_param.param_hdr.type = response_type;
        ack_param.param_hdr.type = response_type;
        ack_param.param_hdr.length = htons(sizeof(ack_param) +
        ack_param.param_hdr.length = htons(sizeof(ack_param) +
                                           err_param_len +
                                           err_param_len +
                                           asconf_param_len);
                                           asconf_param_len);
        ack_param.crr_id = crr_id;
        ack_param.crr_id = crr_id;
        sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
        sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
 
 
        if (SCTP_ERROR_NO_ERROR == err_code)
        if (SCTP_ERROR_NO_ERROR == err_code)
                return;
                return;
 
 
        /* Add Error Cause parameter. */
        /* Add Error Cause parameter. */
        err_param.cause = err_code;
        err_param.cause = err_code;
        err_param.length = htons(err_param_len + asconf_param_len);
        err_param.length = htons(err_param_len + asconf_param_len);
        sctp_addto_chunk(chunk, err_param_len, &err_param);
        sctp_addto_chunk(chunk, err_param_len, &err_param);
 
 
        /* Add the failed TLV copied from ASCONF chunk. */
        /* Add the failed TLV copied from ASCONF chunk. */
        if (asconf_param)
        if (asconf_param)
                sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
                sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
}
}
 
 
/* Process a asconf parameter. */
/* Process a asconf parameter. */
static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
                                       struct sctp_chunk *asconf,
                                       struct sctp_chunk *asconf,
                                       sctp_addip_param_t *asconf_param)
                                       sctp_addip_param_t *asconf_param)
{
{
        struct sctp_transport *peer;
        struct sctp_transport *peer;
        struct sctp_af *af;
        struct sctp_af *af;
        union sctp_addr addr;
        union sctp_addr addr;
        struct list_head *pos;
        struct list_head *pos;
        union sctp_addr_param *addr_param;
        union sctp_addr_param *addr_param;
 
 
        addr_param = (union sctp_addr_param *)
        addr_param = (union sctp_addr_param *)
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
 
 
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
        if (unlikely(!af))
        if (unlikely(!af))
                return SCTP_ERROR_INV_PARAM;
                return SCTP_ERROR_INV_PARAM;
 
 
        af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
        af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
        switch (asconf_param->param_hdr.type) {
        switch (asconf_param->param_hdr.type) {
        case SCTP_PARAM_ADD_IP:
        case SCTP_PARAM_ADD_IP:
                /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
                /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
                 * request and does not have the local resources to add this
                 * request and does not have the local resources to add this
                 * new address to the association, it MUST return an Error
                 * new address to the association, it MUST return an Error
                 * Cause TLV set to the new error code 'Operation Refused
                 * Cause TLV set to the new error code 'Operation Refused
                 * Due to Resource Shortage'.
                 * Due to Resource Shortage'.
                 */
                 */
 
 
                peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC);
                peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC);
                if (!peer)
                if (!peer)
                        return SCTP_ERROR_RSRC_LOW;
                        return SCTP_ERROR_RSRC_LOW;
 
 
                /* Start the heartbeat timer. */
                /* Start the heartbeat timer. */
                if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
                if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
                        sctp_transport_hold(peer);
                        sctp_transport_hold(peer);
                break;
                break;
        case SCTP_PARAM_DEL_IP:
        case SCTP_PARAM_DEL_IP:
                /* ADDIP 4.3 D7) If a request is received to delete the
                /* ADDIP 4.3 D7) If a request is received to delete the
                 * last remaining IP address of a peer endpoint, the receiver
                 * last remaining IP address of a peer endpoint, the receiver
                 * MUST send an Error Cause TLV with the error cause set to the
                 * MUST send an Error Cause TLV with the error cause set to the
                 * new error code 'Request to Delete Last Remaining IP Address'.
                 * new error code 'Request to Delete Last Remaining IP Address'.
                 */
                 */
                pos = asoc->peer.transport_addr_list.next;
                pos = asoc->peer.transport_addr_list.next;
                if (pos->next == &asoc->peer.transport_addr_list)
                if (pos->next == &asoc->peer.transport_addr_list)
                        return SCTP_ERROR_DEL_LAST_IP;
                        return SCTP_ERROR_DEL_LAST_IP;
 
 
                /* ADDIP 4.3 D8) If a request is received to delete an IP
                /* ADDIP 4.3 D8) If a request is received to delete an IP
                 * address which is also the source address of the IP packet
                 * address which is also the source address of the IP packet
                 * which contained the ASCONF chunk, the receiver MUST reject
                 * which contained the ASCONF chunk, the receiver MUST reject
                 * this request. To reject the request the receiver MUST send
                 * this request. To reject the request the receiver MUST send
                 * an Error Cause TLV set to the new error code 'Request to
                 * an Error Cause TLV set to the new error code 'Request to
                 * Delete Source IP Address'
                 * Delete Source IP Address'
                 */
                 */
                if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
                if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
                        return SCTP_ERROR_DEL_SRC_IP;
                        return SCTP_ERROR_DEL_SRC_IP;
 
 
                sctp_assoc_del_peer(asoc, &addr);
                sctp_assoc_del_peer(asoc, &addr);
                break;
                break;
        case SCTP_PARAM_SET_PRIMARY:
        case SCTP_PARAM_SET_PRIMARY:
                peer = sctp_assoc_lookup_paddr(asoc, &addr);
                peer = sctp_assoc_lookup_paddr(asoc, &addr);
                if (!peer)
                if (!peer)
                        return SCTP_ERROR_INV_PARAM;
                        return SCTP_ERROR_INV_PARAM;
 
 
                sctp_assoc_set_primary(asoc, peer);
                sctp_assoc_set_primary(asoc, peer);
                break;
                break;
        default:
        default:
                return SCTP_ERROR_INV_PARAM;
                return SCTP_ERROR_INV_PARAM;
                break;
                break;
        }
        }
 
 
        return SCTP_ERROR_NO_ERROR;
        return SCTP_ERROR_NO_ERROR;
}
}
 
 
/* Process an incoming ASCONF chunk with the next expected serial no. and
/* Process an incoming ASCONF chunk with the next expected serial no. and
 * return an ASCONF_ACK chunk to be sent in response.
 * return an ASCONF_ACK chunk to be sent in response.
 */
 */
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
                                       struct sctp_chunk *asconf)
                                       struct sctp_chunk *asconf)
{
{
        sctp_addiphdr_t         *hdr;
        sctp_addiphdr_t         *hdr;
        union sctp_addr_param   *addr_param;
        union sctp_addr_param   *addr_param;
        sctp_addip_param_t      *asconf_param;
        sctp_addip_param_t      *asconf_param;
        struct sctp_chunk       *asconf_ack;
        struct sctp_chunk       *asconf_ack;
 
 
        __u16   err_code;
        __u16   err_code;
        int     length = 0;
        int     length = 0;
        int     chunk_len = asconf->skb->len;
        int     chunk_len = asconf->skb->len;
        __u32   serial;
        __u32   serial;
        int     all_param_pass = 1;
        int     all_param_pass = 1;
 
 
        hdr = (sctp_addiphdr_t *)asconf->skb->data;
        hdr = (sctp_addiphdr_t *)asconf->skb->data;
        serial = ntohl(hdr->serial);
        serial = ntohl(hdr->serial);
 
 
        /* Skip the addiphdr and store a pointer to address parameter.  */
        /* Skip the addiphdr and store a pointer to address parameter.  */
        length = sizeof(sctp_addiphdr_t);
        length = sizeof(sctp_addiphdr_t);
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
        chunk_len -= length;
        chunk_len -= length;
 
 
        /* Skip the address parameter and store a pointer to the first
        /* Skip the address parameter and store a pointer to the first
         * asconf paramter.
         * asconf paramter.
         */
         */
        length = ntohs(addr_param->v4.param_hdr.length);
        length = ntohs(addr_param->v4.param_hdr.length);
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
        chunk_len -= length;
        chunk_len -= length;
 
 
        /* create an ASCONF_ACK chunk.
        /* create an ASCONF_ACK chunk.
         * Based on the definitions of parameters, we know that the size of
         * Based on the definitions of parameters, we know that the size of
         * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
         * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
         * paramters.
         * paramters.
         */
         */
        asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
        asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
        if (!asconf_ack)
        if (!asconf_ack)
                goto done;
                goto done;
 
 
        /* Process the TLVs contained within the ASCONF chunk. */
        /* Process the TLVs contained within the ASCONF chunk. */
        while (chunk_len > 0) {
        while (chunk_len > 0) {
                err_code = sctp_process_asconf_param(asoc, asconf,
                err_code = sctp_process_asconf_param(asoc, asconf,
                                                     asconf_param);
                                                     asconf_param);
                /* ADDIP 4.1 A7)
                /* ADDIP 4.1 A7)
                 * If an error response is received for a TLV parameter,
                 * If an error response is received for a TLV parameter,
                 * all TLVs with no response before the failed TLV are
                 * all TLVs with no response before the failed TLV are
                 * considered successful if not reported.  All TLVs after
                 * considered successful if not reported.  All TLVs after
                 * the failed response are considered unsuccessful unless
                 * the failed response are considered unsuccessful unless
                 * a specific success indication is present for the parameter.
                 * a specific success indication is present for the parameter.
                 */
                 */
                if (SCTP_ERROR_NO_ERROR != err_code)
                if (SCTP_ERROR_NO_ERROR != err_code)
                        all_param_pass = 0;
                        all_param_pass = 0;
 
 
                if (!all_param_pass)
                if (!all_param_pass)
                        sctp_add_asconf_response(asconf_ack,
                        sctp_add_asconf_response(asconf_ack,
                                                 asconf_param->crr_id, err_code,
                                                 asconf_param->crr_id, err_code,
                                                 asconf_param);
                                                 asconf_param);
 
 
                /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
                /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
                 * an IP address sends an 'Out of Resource' in its response, it
                 * an IP address sends an 'Out of Resource' in its response, it
                 * MUST also fail any subsequent add or delete requests bundled
                 * MUST also fail any subsequent add or delete requests bundled
                 * in the ASCONF.
                 * in the ASCONF.
                 */
                 */
                if (SCTP_ERROR_RSRC_LOW == err_code)
                if (SCTP_ERROR_RSRC_LOW == err_code)
                        goto done;
                        goto done;
 
 
                /* Move to the next ASCONF param. */
                /* Move to the next ASCONF param. */
                length = ntohs(asconf_param->param_hdr.length);
                length = ntohs(asconf_param->param_hdr.length);
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
                                                      length);
                                                      length);
                chunk_len -= length;
                chunk_len -= length;
        }
        }
 
 
done:
done:
        asoc->peer.addip_serial++;
        asoc->peer.addip_serial++;
 
 
        /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
        /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
         * after freeing the reference to old asconf ack if any.
         * after freeing the reference to old asconf ack if any.
         */
         */
        if (asconf_ack) {
        if (asconf_ack) {
                if (asoc->addip_last_asconf_ack)
                if (asoc->addip_last_asconf_ack)
                        sctp_chunk_free(asoc->addip_last_asconf_ack);
                        sctp_chunk_free(asoc->addip_last_asconf_ack);
 
 
                sctp_chunk_hold(asconf_ack);
                sctp_chunk_hold(asconf_ack);
                asoc->addip_last_asconf_ack = asconf_ack;
                asoc->addip_last_asconf_ack = asconf_ack;
        }
        }
 
 
        return asconf_ack;
        return asconf_ack;
}
}
 
 
/* Process a asconf parameter that is successfully acked. */
/* Process a asconf parameter that is successfully acked. */
static int sctp_asconf_param_success(struct sctp_association *asoc,
static int sctp_asconf_param_success(struct sctp_association *asoc,
                                     sctp_addip_param_t *asconf_param)
                                     sctp_addip_param_t *asconf_param)
{
{
        struct sctp_af *af;
        struct sctp_af *af;
        union sctp_addr addr;
        union sctp_addr addr;
        struct sctp_bind_addr *bp = &asoc->base.bind_addr;
        struct sctp_bind_addr *bp = &asoc->base.bind_addr;
        union sctp_addr_param *addr_param;
        union sctp_addr_param *addr_param;
        int retval = 0;
        int retval = 0;
 
 
        addr_param = (union sctp_addr_param *)
        addr_param = (union sctp_addr_param *)
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
 
 
        /* We have checked the packet before, so we do not check again. */
        /* We have checked the packet before, so we do not check again. */
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
        af->from_addr_param(&addr, addr_param, bp->port, 0);
        af->from_addr_param(&addr, addr_param, bp->port, 0);
 
 
        switch (asconf_param->param_hdr.type) {
        switch (asconf_param->param_hdr.type) {
        case SCTP_PARAM_ADD_IP:
        case SCTP_PARAM_ADD_IP:
                sctp_local_bh_disable();
                sctp_local_bh_disable();
                sctp_write_lock(&asoc->base.addr_lock);
                sctp_write_lock(&asoc->base.addr_lock);
                retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
                retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
                sctp_write_unlock(&asoc->base.addr_lock);
                sctp_write_unlock(&asoc->base.addr_lock);
                sctp_local_bh_enable();
                sctp_local_bh_enable();
                break;
                break;
        case SCTP_PARAM_DEL_IP:
        case SCTP_PARAM_DEL_IP:
                sctp_local_bh_disable();
                sctp_local_bh_disable();
                sctp_write_lock(&asoc->base.addr_lock);
                sctp_write_lock(&asoc->base.addr_lock);
                retval = sctp_del_bind_addr(bp, &addr);
                retval = sctp_del_bind_addr(bp, &addr);
                sctp_write_unlock(&asoc->base.addr_lock);
                sctp_write_unlock(&asoc->base.addr_lock);
                sctp_local_bh_enable();
                sctp_local_bh_enable();
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
 
 
        return retval;
        return retval;
}
}
 
 
/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
 * for the given asconf parameter.  If there is no response for this parameter,
 * for the given asconf parameter.  If there is no response for this parameter,
 * return the error code based on the third argument 'no_err'.
 * return the error code based on the third argument 'no_err'.
 * ADDIP 4.1
 * ADDIP 4.1
 * A7) If an error response is received for a TLV parameter, all TLVs with no
 * A7) If an error response is received for a TLV parameter, all TLVs with no
 * response before the failed TLV are considered successful if not reported.
 * response before the failed TLV are considered successful if not reported.
 * All TLVs after the failed response are considered unsuccessful unless a
 * All TLVs after the failed response are considered unsuccessful unless a
 * specific success indication is present for the parameter.
 * specific success indication is present for the parameter.
 */
 */
static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
                                      sctp_addip_param_t *asconf_param,
                                      sctp_addip_param_t *asconf_param,
                                      int no_err)
                                      int no_err)
{
{
        sctp_addip_param_t      *asconf_ack_param;
        sctp_addip_param_t      *asconf_ack_param;
        sctp_errhdr_t           *err_param;
        sctp_errhdr_t           *err_param;
        int                     length;
        int                     length;
        int                     asconf_ack_len = asconf_ack->skb->len;
        int                     asconf_ack_len = asconf_ack->skb->len;
        __u16                   err_code;
        __u16                   err_code;
 
 
        if (no_err)
        if (no_err)
                err_code = SCTP_ERROR_NO_ERROR;
                err_code = SCTP_ERROR_NO_ERROR;
        else
        else
                err_code = SCTP_ERROR_REQ_REFUSED;
                err_code = SCTP_ERROR_REQ_REFUSED;
 
 
        /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
        /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
         * the first asconf_ack parameter.
         * the first asconf_ack parameter.
         */
         */
        length = sizeof(sctp_addiphdr_t);
        length = sizeof(sctp_addiphdr_t);
        asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
        asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
                                                  length);
                                                  length);
        asconf_ack_len -= length;
        asconf_ack_len -= length;
 
 
        while (asconf_ack_len > 0) {
        while (asconf_ack_len > 0) {
                if (asconf_ack_param->crr_id == asconf_param->crr_id) {
                if (asconf_ack_param->crr_id == asconf_param->crr_id) {
                        switch(asconf_ack_param->param_hdr.type) {
                        switch(asconf_ack_param->param_hdr.type) {
                        case SCTP_PARAM_SUCCESS_REPORT:
                        case SCTP_PARAM_SUCCESS_REPORT:
                                return SCTP_ERROR_NO_ERROR;
                                return SCTP_ERROR_NO_ERROR;
                        case SCTP_PARAM_ERR_CAUSE:
                        case SCTP_PARAM_ERR_CAUSE:
                                length = sizeof(sctp_addip_param_t);
                                length = sizeof(sctp_addip_param_t);
                                err_param = (sctp_errhdr_t *)
                                err_param = (sctp_errhdr_t *)
                                           ((void *)asconf_ack_param + length);
                                           ((void *)asconf_ack_param + length);
                                asconf_ack_len -= length;
                                asconf_ack_len -= length;
                                if (asconf_ack_len > 0)
                                if (asconf_ack_len > 0)
                                        return err_param->cause;
                                        return err_param->cause;
                                else
                                else
                                        return SCTP_ERROR_INV_PARAM;
                                        return SCTP_ERROR_INV_PARAM;
                                break;
                                break;
                        default:
                        default:
                                return SCTP_ERROR_INV_PARAM;
                                return SCTP_ERROR_INV_PARAM;
                        }
                        }
                }
                }
 
 
                length = ntohs(asconf_ack_param->param_hdr.length);
                length = ntohs(asconf_ack_param->param_hdr.length);
                asconf_ack_param = (sctp_addip_param_t *)
                asconf_ack_param = (sctp_addip_param_t *)
                                        ((void *)asconf_ack_param + length);
                                        ((void *)asconf_ack_param + length);
                asconf_ack_len -= length;
                asconf_ack_len -= length;
        }
        }
 
 
        return err_code;
        return err_code;
}
}
 
 
/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
int sctp_process_asconf_ack(struct sctp_association *asoc,
int sctp_process_asconf_ack(struct sctp_association *asoc,
                            struct sctp_chunk *asconf_ack)
                            struct sctp_chunk *asconf_ack)
{
{
        struct sctp_chunk       *asconf = asoc->addip_last_asconf;
        struct sctp_chunk       *asconf = asoc->addip_last_asconf;
        union sctp_addr_param   *addr_param;
        union sctp_addr_param   *addr_param;
        sctp_addip_param_t      *asconf_param;
        sctp_addip_param_t      *asconf_param;
        int     length = 0;
        int     length = 0;
        int     asconf_len = asconf->skb->len;
        int     asconf_len = asconf->skb->len;
        int     all_param_pass = 0;
        int     all_param_pass = 0;
        int     no_err = 1;
        int     no_err = 1;
        int     retval = 0;
        int     retval = 0;
        __u16   err_code = SCTP_ERROR_NO_ERROR;
        __u16   err_code = SCTP_ERROR_NO_ERROR;
 
 
        /* Skip the chunkhdr and addiphdr from the last asconf sent and store
        /* Skip the chunkhdr and addiphdr from the last asconf sent and store
         * a pointer to address parameter.
         * a pointer to address parameter.
         */
         */
        length = sizeof(sctp_addip_chunk_t);
        length = sizeof(sctp_addip_chunk_t);
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
        asconf_len -= length;
        asconf_len -= length;
 
 
        /* Skip the address parameter in the last asconf sent and store a
        /* Skip the address parameter in the last asconf sent and store a
         * pointer to the first asconf paramter.
         * pointer to the first asconf paramter.
         */
         */
        length = ntohs(addr_param->v4.param_hdr.length);
        length = ntohs(addr_param->v4.param_hdr.length);
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
        asconf_len -= length;
        asconf_len -= length;
 
 
        /* ADDIP 4.1
        /* ADDIP 4.1
         * A8) If there is no response(s) to specific TLV parameter(s), and no
         * A8) If there is no response(s) to specific TLV parameter(s), and no
         * failures are indicated, then all request(s) are considered
         * failures are indicated, then all request(s) are considered
         * successful.
         * successful.
         */
         */
        if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
        if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
                all_param_pass = 1;
                all_param_pass = 1;
 
 
        /* Process the TLVs contained in the last sent ASCONF chunk. */
        /* Process the TLVs contained in the last sent ASCONF chunk. */
        while (asconf_len > 0) {
        while (asconf_len > 0) {
                if (all_param_pass)
                if (all_param_pass)
                        err_code = SCTP_ERROR_NO_ERROR;
                        err_code = SCTP_ERROR_NO_ERROR;
                else {
                else {
                        err_code = sctp_get_asconf_response(asconf_ack,
                        err_code = sctp_get_asconf_response(asconf_ack,
                                                            asconf_param,
                                                            asconf_param,
                                                            no_err);
                                                            no_err);
                        if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
                        if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
                                no_err = 0;
                                no_err = 0;
                }
                }
 
 
                switch (err_code) {
                switch (err_code) {
                case SCTP_ERROR_NO_ERROR:
                case SCTP_ERROR_NO_ERROR:
                        retval = sctp_asconf_param_success(asoc, asconf_param);
                        retval = sctp_asconf_param_success(asoc, asconf_param);
                        break;
                        break;
 
 
                case SCTP_ERROR_RSRC_LOW:
                case SCTP_ERROR_RSRC_LOW:
                        retval = 1;
                        retval = 1;
                        break;
                        break;
 
 
                case SCTP_ERROR_INV_PARAM:
                case SCTP_ERROR_INV_PARAM:
                        /* Disable sending this type of asconf parameter in
                        /* Disable sending this type of asconf parameter in
                         * future.
                         * future.
                         */
                         */
                        asoc->peer.addip_disabled_mask |=
                        asoc->peer.addip_disabled_mask |=
                                asconf_param->param_hdr.type;
                                asconf_param->param_hdr.type;
                        break;
                        break;
 
 
                case SCTP_ERROR_REQ_REFUSED:
                case SCTP_ERROR_REQ_REFUSED:
                case SCTP_ERROR_DEL_LAST_IP:
                case SCTP_ERROR_DEL_LAST_IP:
                case SCTP_ERROR_DEL_SRC_IP:
                case SCTP_ERROR_DEL_SRC_IP:
                default:
                default:
                         break;
                         break;
                }
                }
 
 
                /* Skip the processed asconf parameter and move to the next
                /* Skip the processed asconf parameter and move to the next
                 * one.
                 * one.
                 */
                 */
                length = ntohs(asconf_param->param_hdr.length);
                length = ntohs(asconf_param->param_hdr.length);
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
                                                      length);
                                                      length);
                asconf_len -= length;
                asconf_len -= length;
        }
        }
 
 
        /* Free the cached last sent asconf chunk. */
        /* Free the cached last sent asconf chunk. */
        sctp_chunk_free(asconf);
        sctp_chunk_free(asconf);
        asoc->addip_last_asconf = NULL;
        asoc->addip_last_asconf = NULL;
 
 
        /* Send the next asconf chunk from the addip chunk queue. */
        /* Send the next asconf chunk from the addip chunk queue. */
        asconf = (struct sctp_chunk *)__skb_dequeue(&asoc->addip_chunks);
        asconf = (struct sctp_chunk *)__skb_dequeue(&asoc->addip_chunks);
        if (asconf) {
        if (asconf) {
                /* Hold the chunk until an ASCONF_ACK is received. */
                /* Hold the chunk until an ASCONF_ACK is received. */
                sctp_chunk_hold(asconf);
                sctp_chunk_hold(asconf);
                if (sctp_primitive_ASCONF(asoc, asconf))
                if (sctp_primitive_ASCONF(asoc, asconf))
                        sctp_chunk_free(asconf);
                        sctp_chunk_free(asconf);
                else
                else
                        asoc->addip_last_asconf = asconf;
                        asoc->addip_last_asconf = asconf;
        }
        }
 
 
        return retval;
        return retval;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.