/*
* Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Cisco Systems, Inc.
* 4. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* We must have V6 so the size of the proto can be calculated. Otherwise
* we would not allocate enough for Net/Open BSD :-<
*/
#include <net/if.h>
#include <netinet/in_pcb.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/ip6protosw.h>
#include <netinet6/in6_var.h>
#include <netinet6/in6_pcb.h>
/*
* The TCP model represents a substantial overhead in that we get
* an additional hash table to keep explicit connections in. The
* listening TCP endpoint will exist in the usual ephash above and
* accept only INIT's. It will be incapable of sending off an INIT.
* When a dg arrives we must look in the normal ephash. If we find
* a TCP endpoint that will tell us to go to the specific endpoint
* hash and re-hash to find the right assoc/socket. If we find a
* UDP model socket we then must complete the lookup. If this fails,
* i.e. no association can be found then we must continue to see if
* a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
* acts like a TCP model connected socket).
*/
struct sctppcbhead *sctp_tcpephash;
u_long hashtcpmark;
uint32_t hashtblsize;
struct sctppcbhead listhead;
struct sctpiterators iteratorhead;
/* ep zone info */
#if defined(__FreeBSD__) || defined(__APPLE__)
#if __FreeBSD_version >= 500000
struct uma_zone *ipi_zone_ep;
struct uma_zone *ipi_zone_asoc;
struct uma_zone *ipi_zone_laddr;
struct uma_zone *ipi_zone_net;
struct uma_zone *ipi_zone_chunk;
struct uma_zone *ipi_zone_sockq;
#else
struct vm_zone *ipi_zone_ep;
struct vm_zone *ipi_zone_asoc;
struct vm_zone *ipi_zone_laddr;
struct vm_zone *ipi_zone_net;
struct vm_zone *ipi_zone_chunk;
struct vm_zone *ipi_zone_sockq;
#endif
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
struct pool ipi_zone_ep;
struct pool ipi_zone_asoc;
struct pool ipi_zone_laddr;
struct pool ipi_zone_net;
struct pool ipi_zone_chunk;
struct pool ipi_zone_sockq;
struct pool ipi_zone_hash;
#endif
/* for port allocations */
uint16_t lastport;
uint16_t lastlow;
uint16_t lasthi;
};
extern uint32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
/*
* Here we have all the relevant information for each SCTP entity created.
* We will need to modify this as approprate. We also need to figure out
* how to access /dev/random.
*/
struct sctp_pcb {
unsigned int time_of_secret_change; /* number of seconds from timeval.tv_sec */
uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
unsigned int size_of_a_cookie;
unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
unsigned int sctp_minrto;
unsigned int sctp_maxrto;
unsigned int initial_rto;
/* various thresholds */
/* Max times I will init at a guy */
uint16_t max_init_times;
/* Max times I will send before we consider someone dead */
uint16_t max_send_times;
uint16_t def_net_failure;
/* number of streams to pre-open on a association */
uint16_t pre_open_stream_count;
uint16_t max_open_streams_intome;
/*
* This timer is kept running per endpoint. When it fires it
* will change the secret key. The default is once a hour
*/
struct sctp_timer signature_change;
int def_cookie_life;
/* defaults to 0 */
int auto_close_time;
uint32_t initial_sequence_debug;
uint32_t adaption_layer_indicator;
uint8_t max_burst;
char current_secret_number;
char last_secret_number;
};
struct sctp_inpcb {
/*
* put an inpcb in front of it all, kind of a waste but we need
* to for compatibility with all the other stuff.
*/
union {
struct inpcb inp;
char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
~SCTP_ALIGNM1];
} ip_inp;
LIST_ENTRY(sctp_inpcb) sctp_list; /* lists all endpoints */
/* hash of all endpoints for model */
LIST_ENTRY(sctp_inpcb) sctp_hash;
/* count of local addresses bound, 0 if bound all */
int laddr_count;
/* list of addrs in use by the EP */
struct sctpladdr sctp_addr_list;
/* used for source address selection rotation */
struct sctp_laddr *next_addr_touse;
struct ifnet *next_ifn_touse;
/* back pointer to our socket */
struct socket *sctp_socket;
uint32_t sctp_flags; /* flag set */
struct sctp_pcb sctp_ep; /* SCTP ep data */
/* head of the hash of all associations */
struct sctpasochead *sctp_tcbhash;
u_long sctp_hashmark;
/* head of the list of all associations */
struct sctpasochead sctp_asoc_list;
/* queue of TCB's waiting to stuff data up the socket */
struct sctpsocketq sctp_queue_list;
void *sctp_tcb_at_block;
struct sctp_iterator *inp_starting_point_for_iterator;
int error_on_block;
uint32_t sctp_frag_point;
uint32_t sctp_vtag_first;
struct mbuf *pkt, *pkt_last, *sb_last_mpkt;
struct mbuf *control;
#if !(defined(__FreeBSD__) || defined(__APPLE__))
#ifndef INP_IPV6
#define INP_IPV6 0x1
#endif
#ifndef INP_IPV4
#define INP_IPV4 0x2
#endif
u_char inp_vflag;
u_char inp_ip_ttl;
u_char inp_ip_tos;
u_char inp_ip_resv;
#endif
#if defined(__FreeBSD__) && __FreeBSD_version >= 503000
struct mtx inp_mtx;
struct mtx inp_create_mtx;
u_int32_t refcount;
#elif defined(__NetBSD__)
kmutex_t inp_mtx;
kmutex_t inp_create_mtx;
u_int32_t refcount;
#endif
};
struct sctp_tcb {
struct socket *sctp_socket; /* back pointer to socket */
struct sctp_inpcb *sctp_ep; /* back pointer to ep */
LIST_ENTRY(sctp_tcb) sctp_tcbhash; /* next link in hash table */
LIST_ENTRY(sctp_tcb) sctp_tcblist; /* list of all of the TCB's */
LIST_ENTRY(sctp_tcb) sctp_asocs;
struct sctp_association asoc;
uint16_t rport; /* remote port in network format */
uint16_t resv;
#if defined(__FreeBSD__) && __FreeBSD_version >= 503000
struct mtx tcb_mtx;
#elif defined(__NetBSD__)
kmutex_t tcb_mtx;
#endif
};
/* General locking concepts:
* The goal of our locking is to of course provide
* consistency and yet minimize overhead. We will
* attempt to use non-recursive locks which are supposed
* to be quite inexpensive. Now in order to do this the goal
* is that most functions are not aware of locking. Once we
* have a TCB we lock it and unlock when we are through. This
* means that the TCB lock is kind-of a "global" lock when
* working on an association. Caution must be used when
* asserting a TCB_LOCK since if we recurse we deadlock.
*
* Most other locks (INP and INFO) attempt to localize
* the locking i.e. we try to contain the lock and
* unlock within the function that needs to lock it. This
* sometimes mean we do extra locks and unlocks and lose
* a bit of efficiency, but if the performance statements about
* non-recursive locks are true this should not be a problem.
* One issue that arises with this only lock when needed
* is that if an implicit association setup is done we
* have a problem. If at the time I lookup an association
* I have NULL in the tcb return, by the time I call to
* create the association some other processor could
* have created it. This is what the CREATE lock on
* the endpoint. Places where we will be implicitly
* creating the association OR just creating an association
* (the connect call) will assert the CREATE_INP lock. This
* will assure us that during all the lookup of INP and INFO
* if another creator is also locking/looking up we can
* gate the two to synchronize. So the CREATE_INP lock is
* also another one we must use extreme caution in locking
* to make sure we don't hit a re-entrancy issue.
*
* For non FreeBSD 5.x and above we provide a bunch
* of EMPTY lock macro's so we can blatantly put locks
* everywhere and they reduce to nothing on NetBSD/OpenBSD
* and FreeBSD 4.x
*
*/
/* When working with the global SCTP lists we lock and unlock
* the INP_INFO lock. So when we go to lookup an association
* we will want to do a SCTP_INP_INFO_RLOCK() and then when
* we want to add a new association to the sctppcbinfo list's
* we will do a SCTP_INP_INFO_WLOCK().
*/
/*
* FIX ME, all locks right now have a
* recursive check/panic to validate that I
* don't have any lock recursion going on.
*/
#ifdef xyzzy
#define SCTP_INP_INFO_RLOCK() do { \
if (mtx_owned(&sctppcbinfo.ipi_ep_mtx)) \
panic("INP INFO Recursive Lock-R"); \
mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
} while (0)
#define SCTP_INP_INFO_WLOCK() do { \
if (mtx_owned(&sctppcbinfo.ipi_ep_mtx)) \
panic("INP INFO Recursive Lock-W"); \
mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
} while (0)
/* The INP locks we will use for locking an SCTP endpoint, so for
* example if we want to change something at the endpoint level for
* example cookie secrets we lock the INP level.
*/
#define SCTP_INP_LOCK_INIT(_inp) \
mtx_init(&(_inp)->inp_mtx, "sctp", "inp", MTX_DEF | MTX_DUPOK)
/* For the majority of things (once we have found the association) we
* will lock the actual association mutex. This will protect all
* the assoiciation level queues and streams and such. We will
* need to lock the socket layer when we stuff data up into
* the receiving sb_mb. I.e. we will need to do an extra
* SOCKBUF_LOCK(&so->so_rcv) even though the association is
* locked.
*/
#define SCTP_TCB_LOCK_INIT(_tcb) \
mutex_init(&(_tcb)->tcb_mtx, MUTEX_DEFAULT, IPL_NET)
#define SCTP_TCB_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_mtx)
#define SCTP_TCB_LOCK(_tcb) do { \
if (!mtx_owned(&(_tcb->sctp_ep->inp_mtx))) \
panic("TCB locking and no INP lock"); \
if (mtx_owned(&(_tcb)->tcb_mtx)) \
panic("TCB Lock-recursive"); \
mtx_lock(&(_tcb)->tcb_mtx); \
} while (0)
#define SCTP_TCB_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_mtx)
#define SCTP_ITERATOR_LOCK_INIT() \
mtx_init(&sctppcbinfo.it_mtx, "sctp", "iterator", MTX_DEF)
#define SCTP_ITERATOR_LOCK() do { \
if (mtx_owned(&sctppcbinfo.it_mtx)) \
panic("Iterator Lock"); \
mtx_lock(&sctppcbinfo.it_mtx); \
} while (0)
/* The INP locks we will use for locking an SCTP endpoint, so for
* example if we want to change something at the endpoint level for
* example cookie secrets we lock the INP level.
*/
#define SCTP_INP_LOCK_INIT(_inp) \
mutex_init(&(_inp)->inp_mtx, MUTEX_DEFAULT, IPL_NET)
/* For the majority of things (once we have found the association) we
* will lock the actual association mutex. This will protect all
* the assoiciation level queues and streams and such. We will
* need to lock the socket layer when we stuff data up into
* the receiving sb_mb. I.e. we will need to do an extra
* SOCKBUF_LOCK(&so->so_rcv) even though the association is
* locked.
*/
/*
* For this call ep_addr, the to is the destination endpoint address
* of the peer (relative to outbound). The from field is only used if
* the TCP model is enabled and helps distingush amongst the subset
* bound (non-boundall). The TCP model MAY change the actual ep field,
* this is why it is passed.
*/
struct sctp_tcb *sctp_findassociation_ep_addr(struct sctp_inpcb **,
struct sockaddr *, struct sctp_nets **, struct sockaddr *, struct sctp_tcb *);
/* Null in last arg inpcb indicate run on ALL ep's. Specific
* inp in last arg indicates run on ONLY assoc's of the
* specified endpoint.
*/
int
sctp_initiate_iterator(asoc_func af, uint32_t, uint32_t, void *, uint32_t,
end_func ef, struct sctp_inpcb *);