Home | History | Annotate | Line # | Download | only in netinet
sctp_pcb.c revision 1.6
      1  1.1      rjs /* $KAME: sctp_pcb.c,v 1.39 2005/06/16 18:29:25 jinmei Exp $ */
      2  1.6      rjs /* $NetBSD: sctp_pcb.c,v 1.6 2016/05/22 22:18:41 rjs Exp $ */
      3  1.1      rjs 
      4  1.1      rjs /*
      5  1.1      rjs  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
      6  1.1      rjs  * All rights reserved.
      7  1.1      rjs  *
      8  1.1      rjs  * Redistribution and use in source and binary forms, with or without
      9  1.1      rjs  * modification, are permitted provided that the following conditions
     10  1.1      rjs  * are met:
     11  1.1      rjs  * 1. Redistributions of source code must retain the above copyright
     12  1.1      rjs  *    notice, this list of conditions and the following disclaimer.
     13  1.1      rjs  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      rjs  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      rjs  *    documentation and/or other materials provided with the distribution.
     16  1.1      rjs  * 3. All advertising materials mentioning features or use of this software
     17  1.1      rjs  *    must display the following acknowledgement:
     18  1.1      rjs  *      This product includes software developed by Cisco Systems, Inc.
     19  1.1      rjs  * 4. Neither the name of the project nor the names of its contributors
     20  1.1      rjs  *    may be used to endorse or promote products derived from this software
     21  1.1      rjs  *    without specific prior written permission.
     22  1.1      rjs  *
     23  1.1      rjs  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      rjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      rjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      rjs  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
     27  1.1      rjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      rjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      rjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      rjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      rjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      rjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      rjs  * SUCH DAMAGE.
     34  1.1      rjs  */
     35  1.1      rjs #include <sys/cdefs.h>
     36  1.6      rjs __KERNEL_RCSID(0, "$NetBSD: sctp_pcb.c,v 1.6 2016/05/22 22:18:41 rjs Exp $");
     37  1.1      rjs 
     38  1.1      rjs #ifdef _KERNEL_OPT
     39  1.1      rjs #include "opt_inet.h"
     40  1.1      rjs #include "opt_sctp.h"
     41  1.1      rjs #endif /* _KERNEL_OPT */
     42  1.1      rjs 
     43  1.1      rjs #include <sys/param.h>
     44  1.1      rjs #include <sys/systm.h>
     45  1.1      rjs #include <sys/malloc.h>
     46  1.1      rjs #include <sys/mbuf.h>
     47  1.1      rjs #include <sys/domain.h>
     48  1.1      rjs #include <sys/protosw.h>
     49  1.1      rjs #include <sys/socket.h>
     50  1.1      rjs #include <sys/socketvar.h>
     51  1.1      rjs #include <sys/proc.h>
     52  1.1      rjs #include <sys/kauth.h>
     53  1.1      rjs #include <sys/kernel.h>
     54  1.1      rjs #include <sys/sysctl.h>
     55  1.1      rjs #include <sys/rnd.h>
     56  1.1      rjs #include <sys/callout.h>
     57  1.1      rjs 
     58  1.1      rjs #include <machine/limits.h>
     59  1.1      rjs #include <machine/cpu.h>
     60  1.1      rjs 
     61  1.1      rjs #include <net/if.h>
     62  1.1      rjs #include <net/if_types.h>
     63  1.1      rjs #include <net/route.h>
     64  1.1      rjs #include <netinet/in.h>
     65  1.1      rjs #include <netinet/in_systm.h>
     66  1.1      rjs #include <netinet/ip.h>
     67  1.1      rjs #include <netinet/in_pcb.h>
     68  1.1      rjs #include <netinet/in_var.h>
     69  1.1      rjs #include <netinet/ip_var.h>
     70  1.1      rjs 
     71  1.1      rjs #ifdef INET6
     72  1.1      rjs #include <netinet/ip6.h>
     73  1.1      rjs #include <netinet6/ip6_var.h>
     74  1.1      rjs #include <netinet6/scope6_var.h>
     75  1.1      rjs #include <netinet6/in6_pcb.h>
     76  1.1      rjs #endif /* INET6 */
     77  1.1      rjs 
     78  1.1      rjs #ifdef IPSEC
     79  1.4      rjs #include <netipsec/ipsec.h>
     80  1.4      rjs #include <netipsec/key.h>
     81  1.1      rjs #endif /* IPSEC */
     82  1.1      rjs 
     83  1.1      rjs #include <netinet/sctp_var.h>
     84  1.1      rjs #include <netinet/sctp_pcb.h>
     85  1.1      rjs #include <netinet/sctputil.h>
     86  1.1      rjs #include <netinet/sctp.h>
     87  1.1      rjs #include <netinet/sctp_header.h>
     88  1.1      rjs #include <netinet/sctp_asconf.h>
     89  1.1      rjs #include <netinet/sctp_output.h>
     90  1.1      rjs #include <netinet/sctp_timer.h>
     91  1.1      rjs 
     92  1.1      rjs #ifndef SCTP_PCBHASHSIZE
     93  1.1      rjs /* default number of association hash buckets in each endpoint */
     94  1.1      rjs #define SCTP_PCBHASHSIZE 256
     95  1.1      rjs #endif
     96  1.1      rjs 
     97  1.1      rjs #ifdef SCTP_DEBUG
     98  1.1      rjs u_int32_t sctp_debug_on = SCTP_DEBUG_ALL;
     99  1.1      rjs #endif /* SCTP_DEBUG */
    100  1.1      rjs 
    101  1.1      rjs u_int32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
    102  1.1      rjs 
    103  1.1      rjs int sctp_pcbtblsize = SCTP_PCBHASHSIZE;
    104  1.1      rjs 
    105  1.1      rjs struct sctp_epinfo sctppcbinfo;
    106  1.1      rjs 
    107  1.1      rjs /* FIX: we don't handle multiple link local scopes */
    108  1.1      rjs /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
    109  1.1      rjs int
    110  1.1      rjs SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b)
    111  1.1      rjs {
    112  1.1      rjs 	struct in6_addr tmp_a, tmp_b;
    113  1.1      rjs 	/* use a copy of a and b */
    114  1.1      rjs 	tmp_a = *a;
    115  1.1      rjs 	tmp_b = *b;
    116  1.1      rjs 	in6_clearscope(&tmp_a);
    117  1.1      rjs 	in6_clearscope(&tmp_b);
    118  1.1      rjs 	return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
    119  1.1      rjs }
    120  1.1      rjs 
    121  1.1      rjs #if defined(__FreeBSD__) && __FreeBSD_version > 500000
    122  1.1      rjs 
    123  1.1      rjs #ifndef xyzzy
    124  1.1      rjs void sctp_validate_no_locks(void);
    125  1.1      rjs 
    126  1.1      rjs void
    127  1.1      rjs SCTP_INP_RLOCK(struct sctp_inpcb *inp)
    128  1.1      rjs {
    129  1.1      rjs 	struct sctp_tcb *stcb;
    130  1.1      rjs 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
    131  1.1      rjs 		if (mtx_owned(&(stcb)->tcb_mtx))
    132  1.1      rjs 			panic("I own TCB lock?");
    133  1.1      rjs 	}
    134  1.1      rjs         if (mtx_owned(&(inp)->inp_mtx))
    135  1.1      rjs 		panic("INP Recursive Lock-R");
    136  1.1      rjs         mtx_lock(&(inp)->inp_mtx);
    137  1.1      rjs }
    138  1.1      rjs 
    139  1.1      rjs void
    140  1.1      rjs SCTP_INP_WLOCK(struct sctp_inpcb *inp)
    141  1.1      rjs {
    142  1.1      rjs 	SCTP_INP_RLOCK(inp);
    143  1.1      rjs }
    144  1.1      rjs 
    145  1.1      rjs void
    146  1.1      rjs SCTP_INP_INFO_RLOCK()
    147  1.1      rjs {
    148  1.1      rjs 	struct sctp_inpcb *inp;
    149  1.1      rjs 	struct sctp_tcb *stcb;
    150  1.1      rjs 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
    151  1.1      rjs 		if (mtx_owned(&(inp)->inp_mtx))
    152  1.1      rjs 			panic("info-lock and own inp lock?");
    153  1.1      rjs 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
    154  1.1      rjs 			if (mtx_owned(&(stcb)->tcb_mtx))
    155  1.1      rjs 				panic("Info lock and own a tcb lock?");
    156  1.1      rjs 		}
    157  1.1      rjs 	}
    158  1.1      rjs 	if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
    159  1.1      rjs 		panic("INP INFO Recursive Lock-R");
    160  1.1      rjs 	mtx_lock(&sctppcbinfo.ipi_ep_mtx);
    161  1.1      rjs }
    162  1.1      rjs 
    163  1.1      rjs void
    164  1.1      rjs SCTP_INP_INFO_WLOCK()
    165  1.1      rjs {
    166  1.1      rjs 	SCTP_INP_INFO_RLOCK();
    167  1.1      rjs }
    168  1.1      rjs 
    169  1.1      rjs 
    170  1.1      rjs void sctp_validate_no_locks()
    171  1.1      rjs {
    172  1.1      rjs 	struct sctp_inpcb *inp;
    173  1.1      rjs 	struct sctp_tcb *stcb;
    174  1.1      rjs 
    175  1.1      rjs 	if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
    176  1.1      rjs 		panic("INP INFO lock is owned?");
    177  1.1      rjs 
    178  1.1      rjs 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
    179  1.1      rjs 		if (mtx_owned(&(inp)->inp_mtx))
    180  1.1      rjs 			panic("You own an INP lock?");
    181  1.1      rjs 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
    182  1.1      rjs 			if (mtx_owned(&(stcb)->tcb_mtx))
    183  1.1      rjs 				panic("You own a TCB lock?");
    184  1.1      rjs 		}
    185  1.1      rjs 	}
    186  1.1      rjs }
    187  1.1      rjs 
    188  1.1      rjs #endif
    189  1.1      rjs #endif
    190  1.1      rjs 
    191  1.1      rjs void
    192  1.1      rjs sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
    193  1.1      rjs {
    194  1.1      rjs 	/* We really don't need
    195  1.1      rjs 	 * to lock this, but I will
    196  1.1      rjs 	 * just because it does not hurt.
    197  1.1      rjs 	 */
    198  1.1      rjs 	SCTP_INP_INFO_RLOCK();
    199  1.1      rjs 	spcb->ep_count = sctppcbinfo.ipi_count_ep;
    200  1.1      rjs 	spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
    201  1.1      rjs 	spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
    202  1.1      rjs 	spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
    203  1.1      rjs 	spcb->chk_count = sctppcbinfo.ipi_count_chunk;
    204  1.1      rjs 	spcb->sockq_count = sctppcbinfo.ipi_count_sockq;
    205  1.1      rjs 	spcb->mbuf_track = sctppcbinfo.mbuf_track;
    206  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
    207  1.1      rjs }
    208  1.1      rjs 
    209  1.1      rjs 
    210  1.1      rjs /*
    211  1.1      rjs  * Notes on locks for FreeBSD 5 and up. All association
    212  1.1      rjs  * lookups that have a definte ep, the INP structure is
    213  1.1      rjs  * assumed to be locked for reading. If we need to go
    214  1.1      rjs  * find the INP (ususally when a **inp is passed) then
    215  1.1      rjs  * we must lock the INFO structure first and if needed
    216  1.1      rjs  * lock the INP too. Note that if we lock it we must
    217  1.1      rjs  *
    218  1.1      rjs  */
    219  1.1      rjs 
    220  1.1      rjs 
    221  1.1      rjs /*
    222  1.1      rjs  * Given a endpoint, look and find in its association list any association
    223  1.1      rjs  * with the "to" address given. This can be a "from" address, too, for
    224  1.1      rjs  * inbound packets. For outbound packets it is a true "to" address.
    225  1.1      rjs  */
    226  1.1      rjs static struct sctp_tcb *
    227  1.1      rjs sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
    228  1.1      rjs 			struct sockaddr *to, struct sctp_nets **netp)
    229  1.1      rjs {
    230  1.1      rjs 	/**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
    231  1.1      rjs 
    232  1.1      rjs 	/*
    233  1.1      rjs 	 * Note for this module care must be taken when observing what to is
    234  1.1      rjs 	 * for. In most of the rest of the code the TO field represents my
    235  1.1      rjs 	 * peer and the FROM field represents my address. For this module it
    236  1.1      rjs 	 * is reversed of that.
    237  1.1      rjs 	 */
    238  1.1      rjs 	/*
    239  1.1      rjs 	 * If we support the TCP model, then we must now dig through to
    240  1.1      rjs 	 * see if we can find our endpoint in the list of tcp ep's.
    241  1.1      rjs 	 */
    242  1.1      rjs 	uint16_t lport, rport;
    243  1.1      rjs 	struct sctppcbhead *ephead;
    244  1.1      rjs 	struct sctp_inpcb *inp;
    245  1.1      rjs 	struct sctp_laddr *laddr;
    246  1.1      rjs 	struct sctp_tcb *stcb;
    247  1.1      rjs 	struct sctp_nets *net;
    248  1.1      rjs 
    249  1.1      rjs 	if ((to == NULL) || (from == NULL)) {
    250  1.1      rjs 		return (NULL);
    251  1.1      rjs 	}
    252  1.1      rjs 
    253  1.1      rjs 	if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
    254  1.1      rjs 		lport = ((struct sockaddr_in *)to)->sin_port;
    255  1.1      rjs 		rport = ((struct sockaddr_in *)from)->sin_port;
    256  1.1      rjs 	} else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
    257  1.1      rjs 		lport = ((struct sockaddr_in6 *)to)->sin6_port;
    258  1.1      rjs 		rport = ((struct sockaddr_in6 *)from)->sin6_port;
    259  1.1      rjs 	} else {
    260  1.1      rjs 		return NULL;
    261  1.1      rjs 	}
    262  1.1      rjs 	ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
    263  1.1      rjs 						     (lport + rport), sctppcbinfo.hashtcpmark)];
    264  1.1      rjs 	/*
    265  1.1      rjs 	 * Ok now for each of the guys in this bucket we must look
    266  1.1      rjs 	 * and see:
    267  1.1      rjs 	 *  - Does the remote port match.
    268  1.1      rjs 	 *  - Does there single association's addresses match this
    269  1.1      rjs 	 *    address (to).
    270  1.1      rjs 	 * If so we update p_ep to point to this ep and return the
    271  1.1      rjs 	 * tcb from it.
    272  1.1      rjs 	 */
    273  1.1      rjs 	LIST_FOREACH(inp, ephead, sctp_hash) {
    274  1.1      rjs 		if (lport != inp->sctp_lport) {
    275  1.1      rjs 			continue;
    276  1.1      rjs 		}
    277  1.1      rjs 		SCTP_INP_RLOCK(inp);
    278  1.1      rjs 		/* check to see if the ep has one of the addresses */
    279  1.1      rjs 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
    280  1.1      rjs 			/* We are NOT bound all, so look further */
    281  1.1      rjs 			int match = 0;
    282  1.1      rjs 
    283  1.1      rjs 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
    284  1.1      rjs 				if (laddr->ifa == NULL) {
    285  1.1      rjs #ifdef SCTP_DEBUG
    286  1.1      rjs 					if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    287  1.1      rjs 						printf("An ounce of prevention is worth a pound of cure\n");
    288  1.1      rjs 					}
    289  1.1      rjs #endif
    290  1.1      rjs 					continue;
    291  1.1      rjs 				}
    292  1.1      rjs 				if (laddr->ifa->ifa_addr == NULL) {
    293  1.1      rjs #ifdef SCTP_DEBUG
    294  1.1      rjs 					if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    295  1.1      rjs 						printf("ifa with a NULL address\n");
    296  1.1      rjs 					}
    297  1.1      rjs #endif
    298  1.1      rjs 					continue;
    299  1.1      rjs 				}
    300  1.1      rjs 				if (laddr->ifa->ifa_addr->sa_family ==
    301  1.1      rjs 				    to->sa_family) {
    302  1.1      rjs 					/* see if it matches */
    303  1.1      rjs 					struct sockaddr_in *intf_addr, *sin;
    304  1.1      rjs 					intf_addr = (struct sockaddr_in *)
    305  1.1      rjs 						laddr->ifa->ifa_addr;
    306  1.1      rjs 					sin = (struct sockaddr_in *)to;
    307  1.1      rjs 					if (from->sa_family == AF_INET) {
    308  1.1      rjs 						if (sin->sin_addr.s_addr ==
    309  1.1      rjs 						    intf_addr->sin_addr.s_addr) {
    310  1.1      rjs 							match = 1;
    311  1.1      rjs 							SCTP_INP_RUNLOCK(inp);
    312  1.1      rjs 							break;
    313  1.1      rjs 						}
    314  1.1      rjs 					} else {
    315  1.1      rjs 						struct sockaddr_in6 *intf_addr6;
    316  1.1      rjs 						struct sockaddr_in6 *sin6;
    317  1.1      rjs 						sin6 = (struct sockaddr_in6 *)
    318  1.1      rjs 							to;
    319  1.1      rjs 						intf_addr6 = (struct sockaddr_in6 *)
    320  1.1      rjs 							laddr->ifa->ifa_addr;
    321  1.1      rjs 
    322  1.1      rjs 						if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
    323  1.1      rjs 									 &intf_addr6->sin6_addr)) {
    324  1.1      rjs 							match = 1;
    325  1.1      rjs 							SCTP_INP_RUNLOCK(inp);
    326  1.1      rjs 							break;
    327  1.1      rjs 						}
    328  1.1      rjs 					}
    329  1.1      rjs 				}
    330  1.1      rjs 			}
    331  1.1      rjs 			if (match == 0) {
    332  1.1      rjs 				/* This endpoint does not have this address */
    333  1.1      rjs 				SCTP_INP_RUNLOCK(inp);
    334  1.1      rjs 				continue;
    335  1.1      rjs 			}
    336  1.1      rjs 		}
    337  1.1      rjs 		/*
    338  1.1      rjs 		 * Ok if we hit here the ep has the address, does it hold the
    339  1.1      rjs 		 * tcb?
    340  1.1      rjs 		 */
    341  1.1      rjs 
    342  1.1      rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
    343  1.1      rjs 		if (stcb == NULL) {
    344  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    345  1.1      rjs 			continue;
    346  1.1      rjs 		}
    347  1.1      rjs 		SCTP_TCB_LOCK(stcb);
    348  1.1      rjs 		if (stcb->rport != rport) {
    349  1.1      rjs 			/* remote port does not match. */
    350  1.1      rjs 			SCTP_TCB_UNLOCK(stcb);
    351  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    352  1.1      rjs 			continue;
    353  1.1      rjs 		}
    354  1.1      rjs 		/* Does this TCB have a matching address? */
    355  1.1      rjs 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
    356  1.1      rjs 			if (sctp_cmpaddr(from, rtcache_getdst(&net->ro))) {
    357  1.1      rjs 				/* found it */
    358  1.1      rjs 				if (netp != NULL) {
    359  1.1      rjs 					*netp = net;
    360  1.1      rjs 				}
    361  1.1      rjs 				/* Update the endpoint pointer */
    362  1.1      rjs 				*inp_p = inp;
    363  1.1      rjs 				SCTP_INP_RUNLOCK(inp);
    364  1.1      rjs 				return (stcb);
    365  1.1      rjs 			}
    366  1.1      rjs 		}
    367  1.1      rjs 		SCTP_TCB_UNLOCK(stcb);
    368  1.1      rjs 
    369  1.1      rjs 		SCTP_INP_RUNLOCK(inp);
    370  1.1      rjs 	}
    371  1.1      rjs 	return (NULL);
    372  1.1      rjs }
    373  1.1      rjs 
    374  1.1      rjs struct sctp_tcb *
    375  1.1      rjs sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
    376  1.1      rjs     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
    377  1.1      rjs {
    378  1.1      rjs 	struct sctp_tcb *stcb;
    379  1.1      rjs 	struct sockaddr_in *sin;
    380  1.1      rjs 	struct sockaddr_in6 *sin6;
    381  1.1      rjs 	struct sockaddr_storage local_store, remote_store;
    382  1.1      rjs 	struct ip *iph;
    383  1.1      rjs 	struct sctp_paramhdr parm_buf, *phdr;
    384  1.1      rjs 	int ptype;
    385  1.1      rjs 
    386  1.1      rjs 	memset(&local_store, 0, sizeof(local_store));
    387  1.1      rjs 	memset(&remote_store, 0, sizeof(remote_store));
    388  1.1      rjs 
    389  1.1      rjs 	/* First get the destination address setup too. */
    390  1.1      rjs 	iph = mtod(m, struct ip *);
    391  1.1      rjs 	if (iph->ip_v == IPVERSION) {
    392  1.1      rjs 		/* its IPv4 */
    393  1.1      rjs 		sin = (struct sockaddr_in *)&local_store;
    394  1.1      rjs 		sin->sin_family = AF_INET;
    395  1.1      rjs 		sin->sin_len = sizeof(*sin);
    396  1.1      rjs 		sin->sin_port = sh->dest_port;
    397  1.1      rjs 		sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
    398  1.1      rjs 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
    399  1.1      rjs 		/* its IPv6 */
    400  1.1      rjs 		struct ip6_hdr *ip6;
    401  1.1      rjs 		ip6 = mtod(m, struct ip6_hdr *);
    402  1.1      rjs 		sin6 = (struct sockaddr_in6 *)&local_store;
    403  1.1      rjs 		sin6->sin6_family = AF_INET6;
    404  1.1      rjs 		sin6->sin6_len = sizeof(*sin6);
    405  1.1      rjs 		sin6->sin6_port = sh->dest_port;
    406  1.1      rjs 		sin6->sin6_addr = ip6->ip6_dst;
    407  1.1      rjs 	} else {
    408  1.1      rjs 		return NULL;
    409  1.1      rjs 	}
    410  1.1      rjs 
    411  1.1      rjs 	phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
    412  1.1      rjs 	    &parm_buf, sizeof(struct sctp_paramhdr));
    413  1.1      rjs 	if (phdr == NULL) {
    414  1.1      rjs #ifdef SCTP_DEBUG
    415  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
    416  1.1      rjs 			printf("sctp_process_control: failed to get asconf lookup addr\n");
    417  1.1      rjs 		}
    418  1.1      rjs #endif /* SCTP_DEBUG */
    419  1.1      rjs 		return NULL;
    420  1.1      rjs 	}
    421  1.1      rjs 	ptype = (int)((u_int)ntohs(phdr->param_type));
    422  1.1      rjs 	/* get the correlation address */
    423  1.1      rjs 	if (ptype == SCTP_IPV6_ADDRESS) {
    424  1.1      rjs 		/* ipv6 address param */
    425  1.1      rjs 		struct sctp_ipv6addr_param *p6, p6_buf;
    426  1.1      rjs 		if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
    427  1.1      rjs 			return NULL;
    428  1.1      rjs 		}
    429  1.1      rjs 
    430  1.1      rjs 		p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
    431  1.1      rjs 		    offset + sizeof(struct sctp_asconf_chunk),
    432  1.1      rjs 		    &p6_buf.ph, sizeof(*p6));
    433  1.1      rjs 		if (p6 == NULL) {
    434  1.1      rjs #ifdef SCTP_DEBUG
    435  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
    436  1.1      rjs 				printf("sctp_process_control: failed to get asconf v6 lookup addr\n");
    437  1.1      rjs 			}
    438  1.1      rjs #endif /* SCTP_DEBUG */
    439  1.1      rjs 			return (NULL);
    440  1.1      rjs 		}
    441  1.1      rjs 		sin6 = (struct sockaddr_in6 *)&remote_store;
    442  1.1      rjs 		sin6->sin6_family = AF_INET6;
    443  1.1      rjs 		sin6->sin6_len = sizeof(*sin6);
    444  1.1      rjs 		sin6->sin6_port = sh->src_port;
    445  1.1      rjs 		memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
    446  1.1      rjs 	} else if (ptype == SCTP_IPV4_ADDRESS) {
    447  1.1      rjs 		/* ipv4 address param */
    448  1.1      rjs 		struct sctp_ipv4addr_param *p4, p4_buf;
    449  1.1      rjs 		if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
    450  1.1      rjs 			return NULL;
    451  1.1      rjs 		}
    452  1.1      rjs 
    453  1.1      rjs 		p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
    454  1.1      rjs 		    offset + sizeof(struct sctp_asconf_chunk),
    455  1.1      rjs 		    &p4_buf.ph, sizeof(*p4));
    456  1.1      rjs 		if (p4 == NULL) {
    457  1.1      rjs #ifdef SCTP_DEBUG
    458  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
    459  1.1      rjs 				printf("sctp_process_control: failed to get asconf v4 lookup addr\n");
    460  1.1      rjs 			}
    461  1.1      rjs #endif /* SCTP_DEBUG */
    462  1.1      rjs 			return (NULL);
    463  1.1      rjs 		}
    464  1.1      rjs 		sin = (struct sockaddr_in *)&remote_store;
    465  1.1      rjs 		sin->sin_family = AF_INET;
    466  1.1      rjs 		sin->sin_len = sizeof(*sin);
    467  1.1      rjs 		sin->sin_port = sh->src_port;
    468  1.1      rjs 		memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
    469  1.1      rjs 	} else {
    470  1.1      rjs 		/* invalid address param type */
    471  1.1      rjs 		return NULL;
    472  1.1      rjs 	}
    473  1.1      rjs 
    474  1.1      rjs 	stcb = sctp_findassociation_ep_addr(inp_p,
    475  1.1      rjs 	    (struct sockaddr *)&remote_store, netp,
    476  1.1      rjs 	    (struct sockaddr *)&local_store, NULL);
    477  1.1      rjs 	return (stcb);
    478  1.1      rjs }
    479  1.1      rjs 
    480  1.1      rjs struct sctp_tcb *
    481  1.1      rjs sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
    482  1.1      rjs     struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
    483  1.1      rjs {
    484  1.1      rjs 	struct sctpasochead *head;
    485  1.1      rjs 	struct sctp_inpcb *inp;
    486  1.1      rjs 	struct sctp_tcb *stcb;
    487  1.1      rjs 	struct sctp_nets *net;
    488  1.1      rjs 	uint16_t rport;
    489  1.1      rjs 
    490  1.1      rjs 	inp = *inp_p;
    491  1.1      rjs 	if (remote->sa_family == AF_INET) {
    492  1.1      rjs 		rport = (((struct sockaddr_in *)remote)->sin_port);
    493  1.1      rjs 	} else if (remote->sa_family == AF_INET6) {
    494  1.1      rjs 		rport = (((struct sockaddr_in6 *)remote)->sin6_port);
    495  1.1      rjs 	} else {
    496  1.1      rjs 		return (NULL);
    497  1.1      rjs 	}
    498  1.1      rjs 	if (locked_tcb) {
    499  1.1      rjs 		/* UN-lock so we can do proper locking here
    500  1.1      rjs 		 * this occurs when called from load_addresses_from_init.
    501  1.1      rjs 		 */
    502  1.1      rjs 		SCTP_TCB_UNLOCK(locked_tcb);
    503  1.1      rjs 	}
    504  1.1      rjs 	SCTP_INP_INFO_RLOCK();
    505  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
    506  1.1      rjs 		/*
    507  1.1      rjs 		 * Now either this guy is our listner or it's the connector.
    508  1.1      rjs 		 * If it is the one that issued the connect, then it's only
    509  1.1      rjs 		 * chance is to be the first TCB in the list. If it is the
    510  1.1      rjs 		 * acceptor, then do the special_lookup to hash and find the
    511  1.1      rjs 		 * real inp.
    512  1.1      rjs 		 */
    513  1.1      rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
    514  1.1      rjs 			/* to is peer addr, from is my addr */
    515  1.1      rjs 			stcb = sctp_tcb_special_locate(inp_p, remote, local,
    516  1.1      rjs 						       netp);
    517  1.1      rjs 			if ((stcb != NULL) && (locked_tcb == NULL)){
    518  1.1      rjs 				/* we have a locked tcb, lower refcount */
    519  1.1      rjs 				SCTP_INP_WLOCK(inp);
    520  1.1      rjs 				SCTP_INP_DECR_REF(inp);
    521  1.1      rjs 				SCTP_INP_WUNLOCK(inp);
    522  1.1      rjs 			}
    523  1.1      rjs 			if (locked_tcb != NULL) {
    524  1.1      rjs 				SCTP_INP_RLOCK(locked_tcb->sctp_ep);
    525  1.1      rjs 				SCTP_TCB_LOCK(locked_tcb);
    526  1.1      rjs 				SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
    527  1.1      rjs 				if (stcb != NULL) {
    528  1.1      rjs 					SCTP_TCB_UNLOCK(stcb);
    529  1.1      rjs 				}
    530  1.1      rjs 			}
    531  1.1      rjs 			SCTP_INP_INFO_RUNLOCK();
    532  1.1      rjs 			return (stcb);
    533  1.1      rjs 		} else {
    534  1.1      rjs 			SCTP_INP_WLOCK(inp);
    535  1.1      rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
    536  1.1      rjs 			if (stcb == NULL) {
    537  1.1      rjs 				goto null_return;
    538  1.1      rjs 			}
    539  1.1      rjs 			SCTP_TCB_LOCK(stcb);
    540  1.1      rjs 			if (stcb->rport != rport) {
    541  1.1      rjs 				/* remote port does not match. */
    542  1.1      rjs 				SCTP_TCB_UNLOCK(stcb);
    543  1.1      rjs 				goto null_return;
    544  1.1      rjs 			}
    545  1.1      rjs 			/* now look at the list of remote addresses */
    546  1.1      rjs 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
    547  1.1      rjs 				if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
    548  1.1      rjs 					/* found it */
    549  1.1      rjs 					if (netp != NULL) {
    550  1.1      rjs 						*netp = net;
    551  1.1      rjs 					}
    552  1.1      rjs 					if (locked_tcb == NULL) {
    553  1.1      rjs 						SCTP_INP_DECR_REF(inp);
    554  1.1      rjs 					}
    555  1.1      rjs 					SCTP_INP_WUNLOCK(inp);
    556  1.1      rjs 					SCTP_INP_INFO_RUNLOCK();
    557  1.1      rjs 					return (stcb);
    558  1.1      rjs 				}
    559  1.1      rjs 			}
    560  1.1      rjs 			SCTP_TCB_UNLOCK(stcb);
    561  1.1      rjs 		}
    562  1.1      rjs 	} else {
    563  1.1      rjs 		SCTP_INP_WLOCK(inp);
    564  1.1      rjs 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
    565  1.1      rjs 							       inp->sctp_hashmark)];
    566  1.1      rjs 		if (head == NULL) {
    567  1.1      rjs 			goto null_return;
    568  1.1      rjs 		}
    569  1.1      rjs 		LIST_FOREACH(stcb, head, sctp_tcbhash) {
    570  1.1      rjs 			if (stcb->rport != rport) {
    571  1.1      rjs 				/* remote port does not match */
    572  1.1      rjs 				continue;
    573  1.1      rjs 			}
    574  1.1      rjs 			/* now look at the list of remote addresses */
    575  1.1      rjs 			SCTP_TCB_LOCK(stcb);
    576  1.1      rjs 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
    577  1.1      rjs 				if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
    578  1.1      rjs 					/* found it */
    579  1.1      rjs 					if (netp != NULL) {
    580  1.1      rjs 						*netp = net;
    581  1.1      rjs 					}
    582  1.1      rjs 					if (locked_tcb == NULL) {
    583  1.1      rjs 						SCTP_INP_DECR_REF(inp);
    584  1.1      rjs 					}
    585  1.1      rjs 					SCTP_INP_WUNLOCK(inp);
    586  1.1      rjs 					SCTP_INP_INFO_RUNLOCK();
    587  1.1      rjs 					return (stcb);
    588  1.1      rjs 				}
    589  1.1      rjs 			}
    590  1.1      rjs 			SCTP_TCB_UNLOCK(stcb);
    591  1.1      rjs 		}
    592  1.1      rjs 	}
    593  1.1      rjs  null_return:
    594  1.1      rjs 	/* clean up for returning null */
    595  1.1      rjs 	if (locked_tcb){
    596  1.1      rjs 		if (locked_tcb->sctp_ep != inp) {
    597  1.1      rjs 			SCTP_INP_RLOCK(locked_tcb->sctp_ep);
    598  1.1      rjs 			SCTP_TCB_LOCK(locked_tcb);
    599  1.1      rjs 			SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
    600  1.1      rjs 		} else {
    601  1.1      rjs 			SCTP_TCB_LOCK(locked_tcb);
    602  1.1      rjs 		}
    603  1.1      rjs 	}
    604  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
    605  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
    606  1.1      rjs 	/* not found */
    607  1.1      rjs 	return (NULL);
    608  1.1      rjs }
    609  1.1      rjs 
    610  1.1      rjs /*
    611  1.1      rjs  * Find an association for a specific endpoint using the association id
    612  1.1      rjs  * given out in the COMM_UP notification
    613  1.1      rjs  */
    614  1.1      rjs struct sctp_tcb *
    615  1.1      rjs sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, vaddr_t asoc_id)
    616  1.1      rjs {
    617  1.1      rjs 	/*
    618  1.1      rjs 	 * Use my the assoc_id to find a endpoint
    619  1.1      rjs 	 */
    620  1.1      rjs 	struct sctpasochead *head;
    621  1.1      rjs 	struct sctp_tcb *stcb;
    622  1.1      rjs 	u_int32_t vtag;
    623  1.1      rjs 
    624  1.1      rjs 	if (asoc_id == 0 || inp == NULL) {
    625  1.1      rjs 		return (NULL);
    626  1.1      rjs 	}
    627  1.1      rjs 	SCTP_INP_INFO_RLOCK();
    628  1.1      rjs 	vtag = (u_int32_t)asoc_id;
    629  1.1      rjs 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
    630  1.1      rjs 	    sctppcbinfo.hashasocmark)];
    631  1.1      rjs 	if (head == NULL) {
    632  1.1      rjs 		/* invalid vtag */
    633  1.1      rjs 		SCTP_INP_INFO_RUNLOCK();
    634  1.1      rjs 		return (NULL);
    635  1.1      rjs 	}
    636  1.1      rjs 	LIST_FOREACH(stcb, head, sctp_asocs) {
    637  1.1      rjs 		SCTP_INP_RLOCK(stcb->sctp_ep);
    638  1.1      rjs 		SCTP_TCB_LOCK(stcb);
    639  1.1      rjs 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
    640  1.1      rjs 		if (stcb->asoc.my_vtag == vtag) {
    641  1.1      rjs 			/* candidate */
    642  1.1      rjs 			if (inp != stcb->sctp_ep) {
    643  1.1      rjs 				/* some other guy has the
    644  1.1      rjs 				 * same vtag active (vtag collision).
    645  1.1      rjs 				 */
    646  1.1      rjs 				sctp_pegs[SCTP_VTAG_BOGUS]++;
    647  1.1      rjs 				SCTP_TCB_UNLOCK(stcb);
    648  1.1      rjs 				continue;
    649  1.1      rjs 			}
    650  1.1      rjs 			sctp_pegs[SCTP_VTAG_EXPR]++;
    651  1.1      rjs 			SCTP_INP_INFO_RUNLOCK();
    652  1.1      rjs 			return (stcb);
    653  1.1      rjs 		}
    654  1.1      rjs 		SCTP_TCB_UNLOCK(stcb);
    655  1.1      rjs 	}
    656  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
    657  1.1      rjs 	return (NULL);
    658  1.1      rjs }
    659  1.1      rjs 
    660  1.1      rjs static struct sctp_inpcb *
    661  1.1      rjs sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
    662  1.1      rjs 		    uint16_t lport)
    663  1.1      rjs {
    664  1.1      rjs 	struct sctp_inpcb *inp;
    665  1.1      rjs 	struct sockaddr_in *sin;
    666  1.1      rjs 	struct sockaddr_in6 *sin6;
    667  1.1      rjs 	struct sctp_laddr *laddr;
    668  1.1      rjs 
    669  1.1      rjs 	/* Endpoing probe expects
    670  1.1      rjs 	 * that the INP_INFO is locked.
    671  1.1      rjs 	 */
    672  1.1      rjs 	if (nam->sa_family == AF_INET) {
    673  1.1      rjs 		sin = (struct sockaddr_in *)nam;
    674  1.1      rjs 		sin6 = NULL;
    675  1.1      rjs 	} else if (nam->sa_family == AF_INET6) {
    676  1.1      rjs 		sin6 = (struct sockaddr_in6 *)nam;
    677  1.1      rjs 		sin = NULL;
    678  1.1      rjs 	} else {
    679  1.1      rjs 		/* unsupported family */
    680  1.1      rjs 		return (NULL);
    681  1.1      rjs 	}
    682  1.1      rjs 	if (head == NULL)
    683  1.1      rjs 		return (NULL);
    684  1.1      rjs 
    685  1.1      rjs 	LIST_FOREACH(inp, head, sctp_hash) {
    686  1.1      rjs 		SCTP_INP_RLOCK(inp);
    687  1.1      rjs 
    688  1.1      rjs 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
    689  1.1      rjs 		    (inp->sctp_lport == lport)) {
    690  1.1      rjs 			/* got it */
    691  1.1      rjs 			if ((nam->sa_family == AF_INET) &&
    692  1.1      rjs 			    (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
    693  1.1      rjs #if defined(__FreeBSD__) || defined(__APPLE__)
    694  1.1      rjs 			    (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
    695  1.1      rjs #else
    696  1.1      rjs #if defined(__OpenBSD__)
    697  1.1      rjs 			    (0)	/* For open bsd we do dual bind only */
    698  1.1      rjs #else
    699  1.1      rjs 			    (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
    700  1.1      rjs #endif
    701  1.1      rjs #endif
    702  1.1      rjs 				) {
    703  1.1      rjs 				/* IPv4 on a IPv6 socket with ONLY IPv6 set */
    704  1.1      rjs 				SCTP_INP_RUNLOCK(inp);
    705  1.1      rjs 				continue;
    706  1.1      rjs 			}
    707  1.1      rjs 			/* A V6 address and the endpoint is NOT bound V6 */
    708  1.1      rjs 			if (nam->sa_family == AF_INET6 &&
    709  1.1      rjs 			   (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
    710  1.1      rjs 				SCTP_INP_RUNLOCK(inp);
    711  1.1      rjs 				continue;
    712  1.1      rjs 			}
    713  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    714  1.1      rjs 			return (inp);
    715  1.1      rjs 		}
    716  1.1      rjs 		SCTP_INP_RUNLOCK(inp);
    717  1.1      rjs 	}
    718  1.1      rjs 
    719  1.1      rjs 	if ((nam->sa_family == AF_INET) &&
    720  1.1      rjs 	    (sin->sin_addr.s_addr == INADDR_ANY)) {
    721  1.1      rjs 		/* Can't hunt for one that has no address specified */
    722  1.1      rjs 		return (NULL);
    723  1.1      rjs 	} else if ((nam->sa_family == AF_INET6) &&
    724  1.1      rjs 		   (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
    725  1.1      rjs 		/* Can't hunt for one that has no address specified */
    726  1.1      rjs 		return (NULL);
    727  1.1      rjs 	}
    728  1.1      rjs 	/*
    729  1.1      rjs 	 * ok, not bound to all so see if we can find a EP bound to this
    730  1.1      rjs 	 * address.
    731  1.1      rjs 	 */
    732  1.1      rjs #ifdef SCTP_DEBUG
    733  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    734  1.1      rjs 		printf("Ok, there is NO bound-all available for port:%x\n", ntohs(lport));
    735  1.1      rjs 	}
    736  1.1      rjs #endif
    737  1.1      rjs 	LIST_FOREACH(inp, head, sctp_hash) {
    738  1.1      rjs 		SCTP_INP_RLOCK(inp);
    739  1.1      rjs 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
    740  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    741  1.1      rjs 			continue;
    742  1.1      rjs 		}
    743  1.1      rjs 		/*
    744  1.1      rjs 		 * Ok this could be a likely candidate, look at all of
    745  1.1      rjs 		 * its addresses
    746  1.1      rjs 		 */
    747  1.1      rjs 		if (inp->sctp_lport != lport) {
    748  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    749  1.1      rjs 			continue;
    750  1.1      rjs 		}
    751  1.1      rjs #ifdef SCTP_DEBUG
    752  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    753  1.1      rjs 			printf("Ok, found maching local port\n");
    754  1.1      rjs 		}
    755  1.1      rjs #endif
    756  1.1      rjs 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
    757  1.1      rjs 			if (laddr->ifa == NULL) {
    758  1.1      rjs #ifdef SCTP_DEBUG
    759  1.1      rjs 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    760  1.1      rjs 					printf("An ounce of prevention is worth a pound of cure\n");
    761  1.1      rjs 				}
    762  1.1      rjs #endif
    763  1.1      rjs 				continue;
    764  1.1      rjs 			}
    765  1.1      rjs #ifdef SCTP_DEBUG
    766  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    767  1.1      rjs 				printf("Ok laddr->ifa:%p is possible, ",
    768  1.1      rjs 				    laddr->ifa);
    769  1.1      rjs 			}
    770  1.1      rjs #endif
    771  1.1      rjs 			if (laddr->ifa->ifa_addr == NULL) {
    772  1.1      rjs #ifdef SCTP_DEBUG
    773  1.1      rjs 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    774  1.1      rjs 					printf("Huh IFA as an ifa_addr=NULL, ");
    775  1.1      rjs 				}
    776  1.1      rjs #endif
    777  1.1      rjs 				continue;
    778  1.1      rjs 			}
    779  1.1      rjs #ifdef SCTP_DEBUG
    780  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    781  1.1      rjs 				printf("Ok laddr->ifa:%p is possible, ",
    782  1.1      rjs 				    laddr->ifa->ifa_addr);
    783  1.1      rjs 				sctp_print_address(laddr->ifa->ifa_addr);
    784  1.1      rjs 				printf("looking for ");
    785  1.1      rjs 				sctp_print_address(nam);
    786  1.1      rjs 			}
    787  1.1      rjs #endif
    788  1.1      rjs 			if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
    789  1.1      rjs 				/* possible, see if it matches */
    790  1.1      rjs 				struct sockaddr_in *intf_addr;
    791  1.1      rjs 				intf_addr = (struct sockaddr_in *)
    792  1.1      rjs 				    laddr->ifa->ifa_addr;
    793  1.1      rjs 				if (nam->sa_family == AF_INET) {
    794  1.1      rjs 					if (sin->sin_addr.s_addr ==
    795  1.1      rjs 					    intf_addr->sin_addr.s_addr) {
    796  1.1      rjs #ifdef SCTP_DEBUG
    797  1.1      rjs 						if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    798  1.1      rjs 							printf("YES, return ep:%p\n", inp);
    799  1.1      rjs 						}
    800  1.1      rjs #endif
    801  1.1      rjs 						SCTP_INP_RUNLOCK(inp);
    802  1.1      rjs 						return (inp);
    803  1.1      rjs 					}
    804  1.1      rjs 				} else if (nam->sa_family == AF_INET6) {
    805  1.1      rjs 					struct sockaddr_in6 *intf_addr6;
    806  1.1      rjs 					intf_addr6 = (struct sockaddr_in6 *)
    807  1.1      rjs 					    laddr->ifa->ifa_addr;
    808  1.1      rjs 					if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
    809  1.1      rjs 				 	    &intf_addr6->sin6_addr)) {
    810  1.1      rjs #ifdef SCTP_DEBUG
    811  1.1      rjs 						if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    812  1.1      rjs 							printf("YES, return ep:%p\n", inp);
    813  1.1      rjs 						}
    814  1.1      rjs #endif
    815  1.1      rjs 						SCTP_INP_RUNLOCK(inp);
    816  1.1      rjs 						return (inp);
    817  1.1      rjs 					}
    818  1.1      rjs 				}
    819  1.1      rjs 			}
    820  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
    821  1.1      rjs 		}
    822  1.1      rjs 	}
    823  1.1      rjs #ifdef SCTP_DEBUG
    824  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    825  1.1      rjs 		printf("NO, Falls out to NULL\n");
    826  1.1      rjs 	}
    827  1.1      rjs #endif
    828  1.1      rjs 	return (NULL);
    829  1.1      rjs }
    830  1.1      rjs 
    831  1.1      rjs 
    832  1.1      rjs struct sctp_inpcb *
    833  1.1      rjs sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
    834  1.1      rjs {
    835  1.1      rjs 	/*
    836  1.1      rjs 	 * First we check the hash table to see if someone has this port
    837  1.1      rjs 	 * bound with just the port.
    838  1.1      rjs 	 */
    839  1.1      rjs 	struct sctp_inpcb *inp;
    840  1.1      rjs 	struct sctppcbhead *head;
    841  1.1      rjs 	int lport;
    842  1.1      rjs #ifdef SCTP_DEBUG
    843  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    844  1.1      rjs 		printf("Looking for endpoint %d :",
    845  1.1      rjs 		       ntohs(((struct sockaddr_in *)nam)->sin_port));
    846  1.1      rjs 		sctp_print_address(nam);
    847  1.1      rjs 	}
    848  1.1      rjs #endif
    849  1.1      rjs 	if (nam->sa_family == AF_INET) {
    850  1.1      rjs 		lport = ((struct sockaddr_in *)nam)->sin_port;
    851  1.1      rjs 	} else if (nam->sa_family == AF_INET6) {
    852  1.1      rjs 		lport = ((struct sockaddr_in6 *)nam)->sin6_port;
    853  1.1      rjs 	} else {
    854  1.1      rjs 		/* unsupported family */
    855  1.1      rjs 		return (NULL);
    856  1.1      rjs 	}
    857  1.1      rjs 	/*
    858  1.1      rjs 	 * I could cheat here and just cast to one of the types but we will
    859  1.1      rjs 	 * do it right. It also provides the check against an Unsupported
    860  1.1      rjs 	 * type too.
    861  1.1      rjs 	 */
    862  1.1      rjs 	/* Find the head of the ALLADDR chain */
    863  1.1      rjs 	if (have_lock == 0) {
    864  1.1      rjs 		SCTP_INP_INFO_RLOCK();
    865  1.1      rjs 	}
    866  1.1      rjs 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
    867  1.1      rjs 							     sctppcbinfo.hashmark)];
    868  1.1      rjs #ifdef SCTP_DEBUG
    869  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    870  1.1      rjs 		printf("Main hash to lookup at head:%p\n", head);
    871  1.1      rjs 	}
    872  1.1      rjs #endif
    873  1.1      rjs  	inp = sctp_endpoint_probe(nam, head, lport);
    874  1.1      rjs 
    875  1.1      rjs 	/*
    876  1.1      rjs 	 * If the TCP model exists it could be that the main listening
    877  1.1      rjs 	 * endpoint is gone but there exists a connected socket for this
    878  1.1      rjs 	 * guy yet. If so we can return the first one that we find. This
    879  1.1      rjs 	 * may NOT be the correct one but the sctp_findassociation_ep_addr
    880  1.1      rjs 	 * has further code to look at all TCP models.
    881  1.1      rjs 	 */
    882  1.1      rjs 	if (inp == NULL && find_tcp_pool) {
    883  1.1      rjs 		unsigned int i;
    884  1.1      rjs #ifdef SCTP_DEBUG
    885  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    886  1.1      rjs 			printf("EP was NULL and TCP model is supported\n");
    887  1.1      rjs 		}
    888  1.1      rjs #endif
    889  1.1      rjs 		for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
    890  1.1      rjs 			/*
    891  1.1      rjs 			 * This is real gross, but we do NOT have a remote
    892  1.1      rjs 			 * port at this point depending on who is calling. We
    893  1.1      rjs 			 * must therefore look for ANY one that matches our
    894  1.1      rjs 			 * local port :/
    895  1.1      rjs 			 */
    896  1.1      rjs 			head = &sctppcbinfo.sctp_tcpephash[i];
    897  1.1      rjs 			if (LIST_FIRST(head)) {
    898  1.1      rjs 				inp = sctp_endpoint_probe(nam, head, lport);
    899  1.1      rjs 				if (inp) {
    900  1.1      rjs 					/* Found one */
    901  1.1      rjs 					break;
    902  1.1      rjs 				}
    903  1.1      rjs 			}
    904  1.1      rjs 		}
    905  1.1      rjs 	}
    906  1.1      rjs #ifdef SCTP_DEBUG
    907  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
    908  1.1      rjs 		printf("EP to return is %p\n", inp);
    909  1.1      rjs 	}
    910  1.1      rjs #endif
    911  1.1      rjs 	if (have_lock == 0) {
    912  1.1      rjs 		if (inp) {
    913  1.1      rjs 			SCTP_INP_WLOCK(inp);
    914  1.1      rjs 			SCTP_INP_INCR_REF(inp);
    915  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
    916  1.1      rjs 		}
    917  1.1      rjs 		SCTP_INP_INFO_RUNLOCK();
    918  1.1      rjs 	} else {
    919  1.1      rjs 		if (inp) {
    920  1.1      rjs 			SCTP_INP_WLOCK(inp);
    921  1.1      rjs 			SCTP_INP_INCR_REF(inp);
    922  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
    923  1.1      rjs 		}
    924  1.1      rjs 	}
    925  1.1      rjs 	return (inp);
    926  1.1      rjs }
    927  1.1      rjs 
    928  1.1      rjs /*
    929  1.1      rjs  * Find an association for an endpoint with the pointer to whom you want
    930  1.1      rjs  * to send to and the endpoint pointer. The address can be IPv4 or IPv6.
    931  1.1      rjs  * We may need to change the *to to some other struct like a mbuf...
    932  1.1      rjs  */
    933  1.1      rjs struct sctp_tcb *
    934  1.1      rjs sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
    935  1.1      rjs     struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
    936  1.1      rjs {
    937  1.1      rjs 	struct sctp_inpcb *inp;
    938  1.1      rjs 	struct sctp_tcb *retval;
    939  1.1      rjs 
    940  1.1      rjs 	SCTP_INP_INFO_RLOCK();
    941  1.1      rjs 	if (find_tcp_pool) {
    942  1.1      rjs 		if (inp_p != NULL) {
    943  1.1      rjs 			retval = sctp_tcb_special_locate(inp_p, from, to, netp);
    944  1.1      rjs 		} else {
    945  1.1      rjs 			retval = sctp_tcb_special_locate(&inp, from, to, netp);
    946  1.1      rjs 		}
    947  1.1      rjs 		if (retval != NULL) {
    948  1.1      rjs 			SCTP_INP_INFO_RUNLOCK();
    949  1.1      rjs 			return (retval);
    950  1.1      rjs 		}
    951  1.1      rjs 	}
    952  1.1      rjs 	inp = sctp_pcb_findep(to, 0, 1);
    953  1.1      rjs 	if (inp_p != NULL) {
    954  1.1      rjs 		*inp_p = inp;
    955  1.1      rjs 	}
    956  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
    957  1.1      rjs 
    958  1.1      rjs 	if (inp == NULL) {
    959  1.1      rjs 		return (NULL);
    960  1.1      rjs 	}
    961  1.1      rjs 
    962  1.1      rjs 	/*
    963  1.1      rjs 	 * ok, we have an endpoint, now lets find the assoc for it (if any)
    964  1.1      rjs 	 * we now place the source address or from in the to of the find
    965  1.1      rjs 	 * endpoint call. Since in reality this chain is used from the
    966  1.1      rjs 	 * inbound packet side.
    967  1.1      rjs 	 */
    968  1.1      rjs 	if (inp_p != NULL) {
    969  1.1      rjs 		return (sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL));
    970  1.1      rjs 	} else {
    971  1.1      rjs 		return (sctp_findassociation_ep_addr(&inp, from, netp, to, NULL));
    972  1.1      rjs 	}
    973  1.1      rjs }
    974  1.1      rjs 
    975  1.1      rjs 
    976  1.1      rjs /*
    977  1.1      rjs  * This routine will grub through the mbuf that is a INIT or INIT-ACK and
    978  1.1      rjs  * find all addresses that the sender has specified in any address list.
    979  1.1      rjs  * Each address will be used to lookup the TCB and see if one exits.
    980  1.1      rjs  */
    981  1.1      rjs static struct sctp_tcb *
    982  1.1      rjs sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
    983  1.1      rjs     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
    984  1.1      rjs     struct sockaddr *dest)
    985  1.1      rjs {
    986  1.1      rjs 	struct sockaddr_in sin4;
    987  1.1      rjs 	struct sockaddr_in6 sin6;
    988  1.1      rjs 	struct sctp_paramhdr *phdr, parm_buf;
    989  1.1      rjs 	struct sctp_tcb *retval;
    990  1.1      rjs 	u_int32_t ptype, plen;
    991  1.1      rjs 
    992  1.1      rjs 	memset(&sin4, 0, sizeof(sin4));
    993  1.1      rjs 	memset(&sin6, 0, sizeof(sin6));
    994  1.1      rjs 	sin4.sin_len = sizeof(sin4);
    995  1.1      rjs 	sin4.sin_family = AF_INET;
    996  1.1      rjs 	sin4.sin_port = sh->src_port;
    997  1.1      rjs 	sin6.sin6_len = sizeof(sin6);
    998  1.1      rjs 	sin6.sin6_family = AF_INET6;
    999  1.1      rjs 	sin6.sin6_port = sh->src_port;
   1000  1.1      rjs 
   1001  1.1      rjs 	retval = NULL;
   1002  1.1      rjs 	offset += sizeof(struct sctp_init_chunk);
   1003  1.1      rjs 
   1004  1.1      rjs 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
   1005  1.1      rjs 	while (phdr != NULL) {
   1006  1.1      rjs 		/* now we must see if we want the parameter */
   1007  1.1      rjs 		ptype = ntohs(phdr->param_type);
   1008  1.1      rjs 		plen = ntohs(phdr->param_length);
   1009  1.1      rjs 		if (plen == 0) {
   1010  1.1      rjs #ifdef SCTP_DEBUG
   1011  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1012  1.1      rjs 				printf("sctp_findassociation_special_addr: Impossible length in parameter\n");
   1013  1.1      rjs 			}
   1014  1.1      rjs #endif /* SCTP_DEBUG */
   1015  1.1      rjs 			break;
   1016  1.1      rjs 		}
   1017  1.1      rjs 		if (ptype == SCTP_IPV4_ADDRESS &&
   1018  1.1      rjs 		    plen == sizeof(struct sctp_ipv4addr_param)) {
   1019  1.1      rjs 			/* Get the rest of the address */
   1020  1.1      rjs 			struct sctp_ipv4addr_param ip4_parm, *p4;
   1021  1.1      rjs 
   1022  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   1023  1.1      rjs 			    (struct sctp_paramhdr *)&ip4_parm, plen);
   1024  1.1      rjs 			if (phdr == NULL) {
   1025  1.1      rjs 				return (NULL);
   1026  1.1      rjs 			}
   1027  1.1      rjs 			p4 = (struct sctp_ipv4addr_param *)phdr;
   1028  1.1      rjs 			memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
   1029  1.1      rjs 			/* look it up */
   1030  1.1      rjs 			retval = sctp_findassociation_ep_addr(inp_p,
   1031  1.1      rjs 			    (struct sockaddr *)&sin4, netp, dest, NULL);
   1032  1.1      rjs 			if (retval != NULL) {
   1033  1.1      rjs 				return (retval);
   1034  1.1      rjs 			}
   1035  1.1      rjs 		} else if (ptype == SCTP_IPV6_ADDRESS &&
   1036  1.1      rjs 		    plen == sizeof(struct sctp_ipv6addr_param)) {
   1037  1.1      rjs 			/* Get the rest of the address */
   1038  1.1      rjs 			struct sctp_ipv6addr_param ip6_parm, *p6;
   1039  1.1      rjs 
   1040  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   1041  1.1      rjs 			    (struct sctp_paramhdr *)&ip6_parm, plen);
   1042  1.1      rjs 			if (phdr == NULL) {
   1043  1.1      rjs 				return (NULL);
   1044  1.1      rjs 			}
   1045  1.1      rjs 			p6 = (struct sctp_ipv6addr_param *)phdr;
   1046  1.1      rjs 			memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
   1047  1.1      rjs 			/* look it up */
   1048  1.1      rjs 			retval = sctp_findassociation_ep_addr(inp_p,
   1049  1.1      rjs 			    (struct sockaddr *)&sin6, netp, dest, NULL);
   1050  1.1      rjs 			if (retval != NULL) {
   1051  1.1      rjs 				return (retval);
   1052  1.1      rjs 			}
   1053  1.1      rjs 		}
   1054  1.1      rjs 		offset += SCTP_SIZE32(plen);
   1055  1.1      rjs 		phdr = sctp_get_next_param(m, offset, &parm_buf,
   1056  1.1      rjs 		    sizeof(parm_buf));
   1057  1.1      rjs 	}
   1058  1.1      rjs 	return (NULL);
   1059  1.1      rjs }
   1060  1.1      rjs 
   1061  1.1      rjs static struct sctp_tcb *
   1062  1.1      rjs sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
   1063  1.1      rjs     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
   1064  1.1      rjs     uint16_t lport)
   1065  1.1      rjs {
   1066  1.1      rjs 	/*
   1067  1.1      rjs 	 * Use my vtag to hash. If we find it we then verify the source addr
   1068  1.1      rjs 	 * is in the assoc. If all goes well we save a bit on rec of a packet.
   1069  1.1      rjs 	 */
   1070  1.1      rjs 	struct sctpasochead *head;
   1071  1.1      rjs 	struct sctp_nets *net;
   1072  1.1      rjs 	struct sctp_tcb *stcb;
   1073  1.1      rjs 
   1074  1.1      rjs 	SCTP_INP_INFO_RLOCK();
   1075  1.1      rjs 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
   1076  1.1      rjs 	    sctppcbinfo.hashasocmark)];
   1077  1.1      rjs 	if (head == NULL) {
   1078  1.1      rjs 		/* invalid vtag */
   1079  1.1      rjs 		SCTP_INP_INFO_RUNLOCK();
   1080  1.1      rjs 		return (NULL);
   1081  1.1      rjs 	}
   1082  1.1      rjs 	LIST_FOREACH(stcb, head, sctp_asocs) {
   1083  1.1      rjs 		SCTP_INP_RLOCK(stcb->sctp_ep);
   1084  1.1      rjs 		SCTP_TCB_LOCK(stcb);
   1085  1.1      rjs 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
   1086  1.1      rjs 		if (stcb->asoc.my_vtag == vtag) {
   1087  1.1      rjs 			/* candidate */
   1088  1.1      rjs 			if (stcb->rport != rport) {
   1089  1.1      rjs 				/*
   1090  1.1      rjs 				 * we could remove this if vtags are unique
   1091  1.1      rjs 				 * across the system.
   1092  1.1      rjs 				 */
   1093  1.1      rjs 				SCTP_TCB_UNLOCK(stcb);
   1094  1.1      rjs 				continue;
   1095  1.1      rjs 			}
   1096  1.1      rjs 			if (stcb->sctp_ep->sctp_lport != lport) {
   1097  1.1      rjs 				/*
   1098  1.1      rjs 				 * we could remove this if vtags are unique
   1099  1.1      rjs 				 * across the system.
   1100  1.1      rjs 				 */
   1101  1.1      rjs 				SCTP_TCB_UNLOCK(stcb);
   1102  1.1      rjs 				continue;
   1103  1.1      rjs 			}
   1104  1.1      rjs 			net = sctp_findnet(stcb, from);
   1105  1.1      rjs 			if (net) {
   1106  1.1      rjs 				/* yep its him. */
   1107  1.1      rjs 				*netp = net;
   1108  1.1      rjs 				sctp_pegs[SCTP_VTAG_EXPR]++;
   1109  1.1      rjs 				*inp_p = stcb->sctp_ep;
   1110  1.1      rjs 				SCTP_INP_INFO_RUNLOCK();
   1111  1.1      rjs 				return (stcb);
   1112  1.1      rjs 			} else {
   1113  1.1      rjs  				/* not him, this should only
   1114  1.1      rjs  				 * happen in rare cases so
   1115  1.1      rjs  				 * I peg it.
   1116  1.1      rjs   				 */
   1117  1.1      rjs  				sctp_pegs[SCTP_VTAG_BOGUS]++;
   1118  1.1      rjs 			}
   1119  1.1      rjs 		}
   1120  1.1      rjs 		SCTP_TCB_UNLOCK(stcb);
   1121  1.1      rjs 	}
   1122  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
   1123  1.1      rjs 	return (NULL);
   1124  1.1      rjs }
   1125  1.1      rjs 
   1126  1.1      rjs /*
   1127  1.1      rjs  * Find an association with the pointer to the inbound IP packet. This
   1128  1.1      rjs  * can be a IPv4 or IPv6 packet.
   1129  1.1      rjs  */
   1130  1.1      rjs struct sctp_tcb *
   1131  1.1      rjs sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
   1132  1.1      rjs     struct sctphdr *sh, struct sctp_chunkhdr *ch,
   1133  1.1      rjs     struct sctp_inpcb **inp_p, struct sctp_nets **netp)
   1134  1.1      rjs {
   1135  1.1      rjs 	int find_tcp_pool;
   1136  1.1      rjs 	struct ip *iph;
   1137  1.1      rjs 	struct sctp_tcb *retval;
   1138  1.1      rjs 	struct sockaddr_storage to_store, from_store;
   1139  1.1      rjs 	struct sockaddr *to = (struct sockaddr *)&to_store;
   1140  1.1      rjs 	struct sockaddr *from = (struct sockaddr *)&from_store;
   1141  1.1      rjs 	struct sctp_inpcb *inp;
   1142  1.1      rjs 
   1143  1.1      rjs 
   1144  1.1      rjs 	iph = mtod(m, struct ip *);
   1145  1.1      rjs 	if (iph->ip_v == IPVERSION) {
   1146  1.1      rjs 		/* its IPv4 */
   1147  1.1      rjs 		struct sockaddr_in *to4, *from4;
   1148  1.1      rjs 
   1149  1.1      rjs 		to4 = (struct sockaddr_in *)&to_store;
   1150  1.1      rjs 		from4 = (struct sockaddr_in *)&from_store;
   1151  1.1      rjs 		memset(to4, 0, sizeof(*to4));
   1152  1.1      rjs 		memset(from4, 0, sizeof(*from4));
   1153  1.1      rjs 		from4->sin_family = to4->sin_family = AF_INET;
   1154  1.1      rjs 		from4->sin_len = to4->sin_len = sizeof(struct sockaddr_in);
   1155  1.1      rjs 		from4->sin_addr.s_addr  = iph->ip_src.s_addr;
   1156  1.1      rjs 		to4->sin_addr.s_addr = iph->ip_dst.s_addr ;
   1157  1.1      rjs 		from4->sin_port = sh->src_port;
   1158  1.1      rjs 		to4->sin_port = sh->dest_port;
   1159  1.1      rjs 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   1160  1.1      rjs 		/* its IPv6 */
   1161  1.1      rjs 		struct ip6_hdr *ip6;
   1162  1.1      rjs 		struct sockaddr_in6 *to6, *from6;
   1163  1.1      rjs 
   1164  1.1      rjs 		ip6 = mtod(m, struct ip6_hdr *);
   1165  1.1      rjs 		to6 = (struct sockaddr_in6 *)&to_store;
   1166  1.1      rjs 		from6 = (struct sockaddr_in6 *)&from_store;
   1167  1.1      rjs 		memset(to6, 0, sizeof(*to6));
   1168  1.1      rjs 		memset(from6, 0, sizeof(*from6));
   1169  1.1      rjs 		from6->sin6_family = to6->sin6_family = AF_INET6;
   1170  1.1      rjs 		from6->sin6_len = to6->sin6_len = sizeof(struct sockaddr_in6);
   1171  1.1      rjs 		from6->sin6_addr = ip6->ip6_src;
   1172  1.1      rjs 		to6->sin6_addr = ip6->ip6_dst;
   1173  1.1      rjs 		from6->sin6_port = sh->src_port;
   1174  1.1      rjs 		to6->sin6_port = sh->dest_port;
   1175  1.1      rjs 		/* Get the scopes in properly to the sin6 addr's */
   1176  1.1      rjs #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
   1177  1.1      rjs 		/* We probably don't need this operation (jinmei@kame) */
   1178  1.1      rjs 		(void)in6_recoverscope(to6, &to6->sin6_addr, NULL);
   1179  1.1      rjs 		(void)in6_embedscope(&to6->sin6_addr, to6, NULL, NULL);
   1180  1.1      rjs 
   1181  1.1      rjs 		(void)in6_recoverscope(from6, &from6->sin6_addr, NULL);
   1182  1.1      rjs 		(void)in6_embedscope(&from6->sin6_addr, from6, NULL, NULL);
   1183  1.1      rjs #endif
   1184  1.1      rjs 	} else {
   1185  1.1      rjs 		/* Currently not supported. */
   1186  1.1      rjs 		return (NULL);
   1187  1.1      rjs 	}
   1188  1.1      rjs #ifdef SCTP_DEBUG
   1189  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1190  1.1      rjs 		printf("Looking for port %d address :",
   1191  1.1      rjs 		       ntohs(((struct sockaddr_in *)to)->sin_port));
   1192  1.1      rjs 		sctp_print_address(to);
   1193  1.1      rjs 		printf("From for port %d address :",
   1194  1.1      rjs 		       ntohs(((struct sockaddr_in *)from)->sin_port));
   1195  1.1      rjs 		sctp_print_address(from);
   1196  1.1      rjs 	}
   1197  1.1      rjs #endif
   1198  1.1      rjs 
   1199  1.1      rjs 	if (sh->v_tag) {
   1200  1.1      rjs 		/* we only go down this path if vtag is non-zero */
   1201  1.1      rjs 		retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
   1202  1.1      rjs 		    inp_p, netp, sh->src_port, sh->dest_port);
   1203  1.1      rjs 		if (retval) {
   1204  1.1      rjs 			return (retval);
   1205  1.1      rjs 		}
   1206  1.1      rjs 	}
   1207  1.1      rjs 	find_tcp_pool = 0;
   1208  1.1      rjs 	if ((ch->chunk_type != SCTP_INITIATION) &&
   1209  1.1      rjs 	    (ch->chunk_type != SCTP_INITIATION_ACK) &&
   1210  1.1      rjs 	    (ch->chunk_type != SCTP_COOKIE_ACK) &&
   1211  1.1      rjs 	    (ch->chunk_type != SCTP_COOKIE_ECHO)) {
   1212  1.1      rjs 		/* Other chunk types go to the tcp pool. */
   1213  1.1      rjs 		find_tcp_pool = 1;
   1214  1.1      rjs 	}
   1215  1.1      rjs 	if (inp_p) {
   1216  1.1      rjs 		retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
   1217  1.1      rjs 		    find_tcp_pool);
   1218  1.1      rjs 		inp = *inp_p;
   1219  1.1      rjs 	} else {
   1220  1.1      rjs 		retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
   1221  1.1      rjs 		    find_tcp_pool);
   1222  1.1      rjs 	}
   1223  1.1      rjs #ifdef SCTP_DEBUG
   1224  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1225  1.1      rjs 		printf("retval:%p inp:%p\n", retval, inp);
   1226  1.1      rjs 	}
   1227  1.1      rjs #endif
   1228  1.1      rjs 	if (retval == NULL && inp) {
   1229  1.1      rjs 		/* Found a EP but not this address */
   1230  1.1      rjs #ifdef SCTP_DEBUG
   1231  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1232  1.1      rjs 			printf("Found endpoint %p but no asoc - ep state:%x\n",
   1233  1.1      rjs 			    inp, inp->sctp_flags);
   1234  1.1      rjs 		}
   1235  1.1      rjs #endif
   1236  1.1      rjs 		if ((ch->chunk_type == SCTP_INITIATION) ||
   1237  1.1      rjs 		    (ch->chunk_type == SCTP_INITIATION_ACK)) {
   1238  1.1      rjs 			/*
   1239  1.1      rjs 			 * special hook, we do NOT return linp or an
   1240  1.1      rjs 			 * association that is linked to an existing
   1241  1.1      rjs 			 * association that is under the TCP pool (i.e. no
   1242  1.1      rjs 			 * listener exists). The endpoint finding routine
   1243  1.1      rjs 			 * will always find a listner before examining the
   1244  1.1      rjs 			 * TCP pool.
   1245  1.1      rjs 			 */
   1246  1.1      rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
   1247  1.1      rjs #ifdef SCTP_DEBUG
   1248  1.1      rjs 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1249  1.1      rjs 					printf("Gak, its in the TCP pool... return NULL");
   1250  1.1      rjs 				}
   1251  1.1      rjs #endif
   1252  1.1      rjs 				if (inp_p) {
   1253  1.1      rjs 					*inp_p = NULL;
   1254  1.1      rjs 				}
   1255  1.1      rjs 				return (NULL);
   1256  1.1      rjs 			}
   1257  1.1      rjs #ifdef SCTP_DEBUG
   1258  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1259  1.1      rjs 				printf("Now doing SPECIAL find\n");
   1260  1.1      rjs 			}
   1261  1.1      rjs #endif
   1262  1.1      rjs 			retval = sctp_findassociation_special_addr(m, iphlen,
   1263  1.1      rjs 			    offset, sh, inp_p, netp, to);
   1264  1.1      rjs 		}
   1265  1.1      rjs 	}
   1266  1.1      rjs #ifdef SCTP_DEBUG
   1267  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1268  1.1      rjs 	    printf("retval is %p\n", retval);
   1269  1.1      rjs 	}
   1270  1.1      rjs #endif
   1271  1.1      rjs 	return (retval);
   1272  1.1      rjs }
   1273  1.1      rjs 
   1274  1.1      rjs extern int sctp_max_burst_default;
   1275  1.1      rjs 
   1276  1.1      rjs extern unsigned int sctp_delayed_sack_time_default;
   1277  1.1      rjs extern unsigned int sctp_heartbeat_interval_default;
   1278  1.1      rjs extern unsigned int sctp_pmtu_raise_time_default;
   1279  1.1      rjs extern unsigned int sctp_shutdown_guard_time_default;
   1280  1.1      rjs extern unsigned int sctp_secret_lifetime_default;
   1281  1.1      rjs 
   1282  1.1      rjs extern unsigned int sctp_rto_max_default;
   1283  1.1      rjs extern unsigned int sctp_rto_min_default;
   1284  1.1      rjs extern unsigned int sctp_rto_initial_default;
   1285  1.1      rjs extern unsigned int sctp_init_rto_max_default;
   1286  1.1      rjs extern unsigned int sctp_valid_cookie_life_default;
   1287  1.1      rjs extern unsigned int sctp_init_rtx_max_default;
   1288  1.1      rjs extern unsigned int sctp_assoc_rtx_max_default;
   1289  1.1      rjs extern unsigned int sctp_path_rtx_max_default;
   1290  1.1      rjs extern unsigned int sctp_nr_outgoing_streams_default;
   1291  1.1      rjs 
   1292  1.1      rjs /*
   1293  1.1      rjs  * allocate a sctp_inpcb and setup a temporary binding to a port/all
   1294  1.1      rjs  * addresses. This way if we don't get a bind we by default pick a ephemeral
   1295  1.1      rjs  * port with all addresses bound.
   1296  1.1      rjs  */
   1297  1.1      rjs int
   1298  1.1      rjs sctp_inpcb_alloc(struct socket *so)
   1299  1.1      rjs {
   1300  1.1      rjs 	/*
   1301  1.1      rjs 	 * we get called when a new endpoint starts up. We need to allocate
   1302  1.1      rjs 	 * the sctp_inpcb structure from the zone and init it. Mark it as
   1303  1.1      rjs 	 * unbound and find a port that we can use as an ephemeral with
   1304  1.1      rjs 	 * INADDR_ANY. If the user binds later no problem we can then add
   1305  1.1      rjs 	 * in the specific addresses. And setup the default parameters for
   1306  1.1      rjs 	 * the EP.
   1307  1.1      rjs 	 */
   1308  1.1      rjs 	int i, error;
   1309  1.1      rjs 	struct sctp_inpcb *inp, *n_inp;
   1310  1.1      rjs 	struct sctp_pcb *m;
   1311  1.1      rjs 	struct timeval time;
   1312  1.1      rjs 
   1313  1.1      rjs 	error = 0;
   1314  1.1      rjs 
   1315  1.1      rjs         /* Hack alert:
   1316  1.1      rjs 	 *
   1317  1.1      rjs 	 * This code audits the entire INP list to see if
   1318  1.1      rjs 	 * any ep's that are in the GONE state are now
   1319  1.1      rjs 	 * all free. This should not happen really since when
   1320  1.1      rjs 	 * the last association if freed we should end up deleting
   1321  1.1      rjs 	 * the inpcb. This code including the locks should
   1322  1.1      rjs 	 * be taken out ... since the last set of fixes I
   1323  1.1      rjs 	 * have not seen the "Found a GONE on list" has not
   1324  1.1      rjs 	 * came out. But i am paranoid and we will leave this
   1325  1.1      rjs 	 * in at the cost of efficency on allocation of PCB's.
   1326  1.1      rjs 	 * Probably we should move this to the invariant
   1327  1.1      rjs 	 * compile options
   1328  1.1      rjs 	 */
   1329  1.1      rjs /* #ifdef INVARIANTS*/
   1330  1.1      rjs 	SCTP_INP_INFO_RLOCK();
   1331  1.1      rjs 	inp = LIST_FIRST(&sctppcbinfo.listhead);
   1332  1.1      rjs 	while (inp) {
   1333  1.1      rjs 		n_inp = LIST_NEXT(inp, sctp_list);
   1334  1.1      rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
   1335  1.1      rjs 			if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
   1336  1.1      rjs 				/* finish the job now */
   1337  1.1      rjs 				printf("Found a GONE on list\n");
   1338  1.1      rjs 				SCTP_INP_INFO_RUNLOCK();
   1339  1.1      rjs 				sctp_inpcb_free(inp, 1);
   1340  1.1      rjs 				SCTP_INP_INFO_RLOCK();
   1341  1.1      rjs 			}
   1342  1.1      rjs 		}
   1343  1.1      rjs 		inp = n_inp;
   1344  1.1      rjs 	}
   1345  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
   1346  1.1      rjs /* #endif INVARIANTS*/
   1347  1.1      rjs 
   1348  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   1349  1.1      rjs 	inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
   1350  1.1      rjs 	if (inp == NULL) {
   1351  1.1      rjs 		printf("Out of SCTP-INPCB structures - no resources\n");
   1352  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   1353  1.1      rjs 		return (ENOBUFS);
   1354  1.1      rjs 	}
   1355  1.1      rjs 
   1356  1.1      rjs 	/* zap it */
   1357  1.1      rjs 	memset(inp, 0, sizeof(*inp));
   1358  1.1      rjs 
   1359  1.1      rjs 	/* bump generations */
   1360  1.1      rjs 	inp->ip_inp.inp.inp_socket = so;
   1361  1.1      rjs 
   1362  1.1      rjs 	/* setup socket pointers */
   1363  1.1      rjs 	inp->sctp_socket = so;
   1364  1.1      rjs 
   1365  1.1      rjs 	/* setup inpcb socket too */
   1366  1.1      rjs 	inp->ip_inp.inp.inp_socket = so;
   1367  1.1      rjs 	inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
   1368  1.1      rjs #ifdef IPSEC
   1369  1.1      rjs #if !(defined(__OpenBSD__) || defined(__APPLE__))
   1370  1.1      rjs 	{
   1371  1.1      rjs 		struct inpcbpolicy *pcb_sp = NULL;
   1372  1.1      rjs 		error = ipsec_init_pcbpolicy(so, &pcb_sp);
   1373  1.1      rjs 		/* Arrange to share the policy */
   1374  1.1      rjs 		inp->ip_inp.inp.inp_sp = pcb_sp;
   1375  1.1      rjs 		((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
   1376  1.1      rjs 	}
   1377  1.1      rjs #else
   1378  1.1      rjs 	/* not sure what to do for openbsd here */
   1379  1.1      rjs 	error = 0;
   1380  1.1      rjs #endif
   1381  1.1      rjs 	if (error != 0) {
   1382  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
   1383  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   1384  1.1      rjs 		return error;
   1385  1.1      rjs 	}
   1386  1.1      rjs #endif /* IPSEC */
   1387  1.1      rjs 	sctppcbinfo.ipi_count_ep++;
   1388  1.1      rjs #if defined(__FreeBSD__) || defined(__APPLE__)
   1389  1.1      rjs 	inp->ip_inp.inp.inp_gencnt = ++sctppcbinfo.ipi_gencnt_ep;
   1390  1.1      rjs 	inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
   1391  1.1      rjs #else
   1392  1.1      rjs 	inp->inp_ip_ttl = ip_defttl;
   1393  1.1      rjs 	inp->inp_ip_tos = 0;
   1394  1.1      rjs #endif
   1395  1.1      rjs 
   1396  1.1      rjs 	so->so_pcb = (void *)inp;
   1397  1.1      rjs 
   1398  1.1      rjs 	if ((so->so_type == SOCK_DGRAM) ||
   1399  1.1      rjs 	    (so->so_type == SOCK_SEQPACKET)) {
   1400  1.1      rjs 		/* UDP style socket */
   1401  1.1      rjs 		inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
   1402  1.1      rjs 		    SCTP_PCB_FLAGS_UNBOUND);
   1403  1.1      rjs 		inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
   1404  1.1      rjs 		/* Be sure it is NON-BLOCKING IO for UDP */
   1405  1.1      rjs 		/*so->so_state |= SS_NBIO;*/
   1406  1.1      rjs 	} else if (so->so_type == SOCK_STREAM) {
   1407  1.1      rjs 		/* TCP style socket */
   1408  1.1      rjs 		inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
   1409  1.1      rjs 		    SCTP_PCB_FLAGS_UNBOUND);
   1410  1.1      rjs 		inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
   1411  1.1      rjs 		/* Be sure we have blocking IO bu default */
   1412  1.1      rjs 		so->so_state &= ~SS_NBIO;
   1413  1.1      rjs 	} else {
   1414  1.1      rjs 		/*
   1415  1.1      rjs 		 * unsupported socket type (RAW, etc)- in case we missed
   1416  1.1      rjs 		 * it in protosw
   1417  1.1      rjs 		 */
   1418  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
   1419  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   1420  1.1      rjs 		return (EOPNOTSUPP);
   1421  1.1      rjs 	}
   1422  1.1      rjs 	inp->sctp_tcbhash = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_hash);
   1423  1.1      rjs 	if (inp->sctp_tcbhash == NULL) {
   1424  1.1      rjs 		printf("Out of SCTP-INPCB->hashinit - no resources\n");
   1425  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
   1426  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   1427  1.1      rjs 		return (ENOBUFS);
   1428  1.1      rjs 	} else {
   1429  1.1      rjs 		for (i = 0; i < sctp_pcbtblsize; i++)
   1430  1.1      rjs 			LIST_INIT(&inp->sctp_tcbhash[i]);
   1431  1.1      rjs 		for (i = 1; i < sctp_pcbtblsize; i <<= 1)
   1432  1.1      rjs 			continue;
   1433  1.1      rjs 		inp->sctp_hashmark = i - 1;
   1434  1.1      rjs 	}
   1435  1.1      rjs         /* LOCK init's */
   1436  1.1      rjs 	SCTP_INP_LOCK_INIT(inp);
   1437  1.1      rjs 	SCTP_ASOC_CREATE_LOCK_INIT(inp);
   1438  1.1      rjs 	/* lock the new ep */
   1439  1.1      rjs 	SCTP_INP_WLOCK(inp);
   1440  1.1      rjs 
   1441  1.1      rjs 	/* add it to the info area */
   1442  1.1      rjs 	LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
   1443  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   1444  1.1      rjs 
   1445  1.1      rjs 	LIST_INIT(&inp->sctp_addr_list);
   1446  1.1      rjs 	LIST_INIT(&inp->sctp_asoc_list);
   1447  1.1      rjs 	TAILQ_INIT(&inp->sctp_queue_list);
   1448  1.1      rjs 	/* Init the timer structure for signature change */
   1449  1.1      rjs 	callout_init(&inp->sctp_ep.signature_change.timer, 0);
   1450  1.1      rjs 	inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
   1451  1.1      rjs 
   1452  1.1      rjs 	/* now init the actual endpoint default data */
   1453  1.1      rjs 	m = &inp->sctp_ep;
   1454  1.1      rjs 
   1455  1.1      rjs 	/* setup the base timeout information */
   1456  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
   1457  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
   1458  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
   1459  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_heartbeat_interval_default; /* this is in MSEC */
   1460  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
   1461  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
   1462  1.1      rjs 	m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
   1463  1.1      rjs 	/* all max/min max are in ms */
   1464  1.1      rjs 	m->sctp_maxrto = sctp_rto_max_default;
   1465  1.1      rjs 	m->sctp_minrto = sctp_rto_min_default;
   1466  1.1      rjs 	m->initial_rto = sctp_rto_initial_default;
   1467  1.1      rjs 	m->initial_init_rto_max = sctp_init_rto_max_default;
   1468  1.1      rjs 
   1469  1.1      rjs 	m->max_open_streams_intome = MAX_SCTP_STREAMS;
   1470  1.1      rjs 
   1471  1.1      rjs 	m->max_init_times = sctp_init_rtx_max_default;
   1472  1.1      rjs 	m->max_send_times = sctp_assoc_rtx_max_default;
   1473  1.1      rjs 	m->def_net_failure = sctp_path_rtx_max_default;
   1474  1.1      rjs 	m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
   1475  1.1      rjs 	m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
   1476  1.1      rjs 	m->max_burst = sctp_max_burst_default;
   1477  1.1      rjs 	/* number of streams to pre-open on a association */
   1478  1.1      rjs 	m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
   1479  1.1      rjs 
   1480  1.1      rjs 	/* Add adaption cookie */
   1481  1.1      rjs 	m->adaption_layer_indicator = 0x504C5253;
   1482  1.1      rjs 
   1483  1.1      rjs 	/* seed random number generator */
   1484  1.1      rjs 	m->random_counter = 1;
   1485  1.1      rjs 	m->store_at = SCTP_SIGNATURE_SIZE;
   1486  1.1      rjs #if defined(__FreeBSD__) && (__FreeBSD_version < 500000)
   1487  1.1      rjs 	read_random_unlimited(m->random_numbers, sizeof(m->random_numbers));
   1488  1.1      rjs #elif defined(__APPLE__) || (__FreeBSD_version > 500000)
   1489  1.1      rjs 	read_random(m->random_numbers, sizeof(m->random_numbers));
   1490  1.1      rjs #elif defined(__OpenBSD__)
   1491  1.1      rjs 	get_random_bytes(m->random_numbers, sizeof(m->random_numbers));
   1492  1.1      rjs #elif defined(__NetBSD__) && NRND > 0
   1493  1.1      rjs 	rnd_extract_data(m->random_numbers, sizeof(m->random_numbers),
   1494  1.1      rjs 			 RND_EXTRACT_ANY);
   1495  1.1      rjs #else
   1496  1.1      rjs 	{
   1497  1.1      rjs 		u_int32_t *ranm, *ranp;
   1498  1.1      rjs 		ranp = (u_int32_t *)&m->random_numbers;
   1499  1.1      rjs 		ranm = ranp + (SCTP_SIGNATURE_ALOC_SIZE/sizeof(u_int32_t));
   1500  1.1      rjs 		if ((u_long)ranp % 4) {
   1501  1.1      rjs 			/* not a even boundary? */
   1502  1.1      rjs 			ranp = (u_int32_t *)SCTP_SIZE32((u_long)ranp);
   1503  1.1      rjs 		}
   1504  1.1      rjs 		while (ranp < ranm) {
   1505  1.1      rjs 			*ranp = random();
   1506  1.1      rjs 			ranp++;
   1507  1.1      rjs 		}
   1508  1.1      rjs 	}
   1509  1.1      rjs #endif
   1510  1.1      rjs 	sctp_fill_random_store(m);
   1511  1.1      rjs 
   1512  1.1      rjs 	/* Minimum cookie size */
   1513  1.1      rjs 	m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
   1514  1.1      rjs 		sizeof(struct sctp_state_cookie);
   1515  1.1      rjs 	m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
   1516  1.1      rjs 
   1517  1.1      rjs 	/* Setup the initial secret */
   1518  1.1      rjs 	SCTP_GETTIME_TIMEVAL(&time);
   1519  1.1      rjs 	m->time_of_secret_change = time.tv_sec;
   1520  1.1      rjs 
   1521  1.1      rjs 	for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
   1522  1.1      rjs 		m->secret_key[0][i] = sctp_select_initial_TSN(m);
   1523  1.1      rjs 	}
   1524  1.1      rjs 	sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
   1525  1.1      rjs 
   1526  1.1      rjs 	/* How long is a cookie good for ? */
   1527  1.1      rjs 	m->def_cookie_life = sctp_valid_cookie_life_default;
   1528  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   1529  1.1      rjs 	return (error);
   1530  1.1      rjs }
   1531  1.1      rjs 
   1532  1.1      rjs 
   1533  1.1      rjs void
   1534  1.1      rjs sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
   1535  1.1      rjs     struct sctp_tcb *stcb)
   1536  1.1      rjs {
   1537  1.1      rjs 	uint16_t lport, rport;
   1538  1.1      rjs 	struct sctppcbhead *head;
   1539  1.1      rjs 	struct sctp_laddr *laddr, *oladdr;
   1540  1.1      rjs 
   1541  1.1      rjs 	SCTP_TCB_UNLOCK(stcb);
   1542  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   1543  1.1      rjs 	SCTP_INP_WLOCK(old_inp);
   1544  1.1      rjs 	SCTP_INP_WLOCK(new_inp);
   1545  1.1      rjs 	SCTP_TCB_LOCK(stcb);
   1546  1.1      rjs 
   1547  1.1      rjs 	new_inp->sctp_ep.time_of_secret_change =
   1548  1.1      rjs 	    old_inp->sctp_ep.time_of_secret_change;
   1549  1.1      rjs 	memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
   1550  1.1      rjs 	    sizeof(old_inp->sctp_ep.secret_key));
   1551  1.1      rjs 	new_inp->sctp_ep.current_secret_number =
   1552  1.1      rjs 	    old_inp->sctp_ep.current_secret_number;
   1553  1.1      rjs 	new_inp->sctp_ep.last_secret_number =
   1554  1.1      rjs 	    old_inp->sctp_ep.last_secret_number;
   1555  1.1      rjs 	new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
   1556  1.1      rjs 
   1557  1.1      rjs 	/* Copy the port across */
   1558  1.1      rjs 	lport = new_inp->sctp_lport = old_inp->sctp_lport;
   1559  1.1      rjs 	rport = stcb->rport;
   1560  1.1      rjs 	/* Pull the tcb from the old association */
   1561  1.1      rjs 	LIST_REMOVE(stcb, sctp_tcbhash);
   1562  1.1      rjs 	LIST_REMOVE(stcb, sctp_tcblist);
   1563  1.1      rjs 
   1564  1.1      rjs 	/* Now insert the new_inp into the TCP connected hash */
   1565  1.1      rjs 	head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
   1566  1.1      rjs 	    sctppcbinfo.hashtcpmark)];
   1567  1.1      rjs 
   1568  1.1      rjs 	LIST_INSERT_HEAD(head, new_inp, sctp_hash);
   1569  1.1      rjs 
   1570  1.1      rjs 	/* Now move the tcb into the endpoint list */
   1571  1.1      rjs 	LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
   1572  1.1      rjs 	/*
   1573  1.1      rjs 	 * Question, do we even need to worry about the ep-hash since
   1574  1.1      rjs 	 * we only have one connection? Probably not :> so lets
   1575  1.1      rjs 	 * get rid of it and not suck up any kernel memory in that.
   1576  1.1      rjs 	 */
   1577  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   1578  1.1      rjs 	stcb->sctp_socket = new_inp->sctp_socket;
   1579  1.1      rjs 	stcb->sctp_ep = new_inp;
   1580  1.1      rjs 	if (new_inp->sctp_tcbhash != NULL) {
   1581  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash,
   1582  1.1      rjs 			      new_inp->sctp_tcbhash);
   1583  1.1      rjs 		new_inp->sctp_tcbhash = NULL;
   1584  1.1      rjs 	}
   1585  1.1      rjs 	if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
   1586  1.1      rjs 		/* Subset bound, so copy in the laddr list from the old_inp */
   1587  1.1      rjs 		LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
   1588  1.1      rjs 			laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
   1589  1.1      rjs 			    sctppcbinfo.ipi_zone_laddr);
   1590  1.1      rjs 			if (laddr == NULL) {
   1591  1.1      rjs 				/*
   1592  1.1      rjs 				 * Gak, what can we do? This assoc is really
   1593  1.1      rjs 				 * HOSED. We probably should send an abort
   1594  1.1      rjs 				 * here.
   1595  1.1      rjs 				 */
   1596  1.1      rjs #ifdef SCTP_DEBUG
   1597  1.1      rjs 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1598  1.1      rjs 					printf("Association hosed in TCP model, out of laddr memory\n");
   1599  1.1      rjs 				}
   1600  1.1      rjs #endif /* SCTP_DEBUG */
   1601  1.1      rjs 				continue;
   1602  1.1      rjs 			}
   1603  1.1      rjs 			sctppcbinfo.ipi_count_laddr++;
   1604  1.1      rjs 			sctppcbinfo.ipi_gencnt_laddr++;
   1605  1.1      rjs 			memset(laddr, 0, sizeof(*laddr));
   1606  1.1      rjs 			laddr->ifa = oladdr->ifa;
   1607  1.1      rjs 			LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
   1608  1.1      rjs 			    sctp_nxt_addr);
   1609  1.1      rjs 			new_inp->laddr_count++;
   1610  1.1      rjs 		}
   1611  1.1      rjs 	}
   1612  1.1      rjs 	SCTP_INP_WUNLOCK(new_inp);
   1613  1.1      rjs 	SCTP_INP_WUNLOCK(old_inp);
   1614  1.1      rjs }
   1615  1.1      rjs 
   1616  1.1      rjs static int
   1617  1.1      rjs sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
   1618  1.1      rjs {
   1619  1.1      rjs 	struct sctppcbhead *head;
   1620  1.1      rjs 	struct sctp_inpcb *t_inp;
   1621  1.1      rjs 
   1622  1.1      rjs 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
   1623  1.1      rjs 	    sctppcbinfo.hashmark)];
   1624  1.1      rjs 	LIST_FOREACH(t_inp, head, sctp_hash) {
   1625  1.1      rjs 		if (t_inp->sctp_lport != lport) {
   1626  1.1      rjs 			continue;
   1627  1.1      rjs 		}
   1628  1.1      rjs 		/* This one is in use. */
   1629  1.1      rjs 		/* check the v6/v4 binding issue */
   1630  1.1      rjs 		if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   1631  1.1      rjs #if defined(__FreeBSD__)
   1632  1.1      rjs 		    (((struct inpcb *)t_inp)->inp_flags & IN6P_IPV6_V6ONLY)
   1633  1.1      rjs #else
   1634  1.1      rjs #if defined(__OpenBSD__)
   1635  1.1      rjs 		    (0)	/* For open bsd we do dual bind only */
   1636  1.1      rjs #else
   1637  1.1      rjs 		    (((struct in6pcb *)t_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
   1638  1.1      rjs #endif
   1639  1.1      rjs #endif
   1640  1.1      rjs 			) {
   1641  1.1      rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   1642  1.1      rjs 				/* collision in V6 space */
   1643  1.1      rjs 				return (1);
   1644  1.1      rjs 			} else {
   1645  1.1      rjs 				/* inp is BOUND_V4 no conflict */
   1646  1.1      rjs 				continue;
   1647  1.1      rjs 			}
   1648  1.1      rjs 		} else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   1649  1.1      rjs 			/* t_inp is bound v4 and v6, conflict always */
   1650  1.1      rjs 			return (1);
   1651  1.1      rjs 		} else {
   1652  1.1      rjs 			/* t_inp is bound only V4 */
   1653  1.1      rjs 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   1654  1.1      rjs #if defined(__FreeBSD__)
   1655  1.1      rjs 			    (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
   1656  1.1      rjs #else
   1657  1.1      rjs #if defined(__OpenBSD__)
   1658  1.1      rjs 			    (0)	/* For open bsd we do dual bind only */
   1659  1.1      rjs #else
   1660  1.1      rjs 			    (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
   1661  1.1      rjs #endif
   1662  1.1      rjs #endif
   1663  1.1      rjs 				) {
   1664  1.1      rjs 				/* no conflict */
   1665  1.1      rjs 				continue;
   1666  1.1      rjs 			}
   1667  1.1      rjs 			/* else fall through to conflict */
   1668  1.1      rjs 		}
   1669  1.1      rjs 		return (1);
   1670  1.1      rjs 	}
   1671  1.1      rjs 	return (0);
   1672  1.1      rjs }
   1673  1.1      rjs 
   1674  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   1675  1.1      rjs /*
   1676  1.1      rjs  * Don't know why, but without this there is an unknown reference when
   1677  1.1      rjs  * compiling NetBSD... hmm
   1678  1.1      rjs  */
   1679  1.1      rjs extern void in6_sin6_2_sin (struct sockaddr_in *, struct sockaddr_in6 *sin6);
   1680  1.1      rjs #endif
   1681  1.1      rjs 
   1682  1.1      rjs 
   1683  1.1      rjs int
   1684  1.1      rjs sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct lwp *l)
   1685  1.1      rjs {
   1686  1.1      rjs 	/* bind a ep to a socket address */
   1687  1.1      rjs 	struct sctppcbhead *head;
   1688  1.1      rjs 	struct sctp_inpcb *inp, *inp_tmp;
   1689  1.1      rjs 	int bindall;
   1690  1.1      rjs 	uint16_t lport;
   1691  1.1      rjs 	int error;
   1692  1.1      rjs 
   1693  1.1      rjs 	lport = 0;
   1694  1.1      rjs 	error = 0;
   1695  1.1      rjs 	bindall = 1;
   1696  1.1      rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   1697  1.1      rjs #ifdef SCTP_DEBUG
   1698  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1699  1.1      rjs 		if (addr) {
   1700  1.1      rjs 			printf("Bind called port:%d\n",
   1701  1.1      rjs 			       ntohs(((struct sockaddr_in *)addr)->sin_port));
   1702  1.1      rjs 			printf("Addr :");
   1703  1.1      rjs 			sctp_print_address(addr);
   1704  1.1      rjs 		}
   1705  1.1      rjs 	}
   1706  1.1      rjs #endif /* SCTP_DEBUG */
   1707  1.1      rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
   1708  1.1      rjs 		/* already did a bind, subsequent binds NOT allowed ! */
   1709  1.1      rjs 		return (EINVAL);
   1710  1.1      rjs 	}
   1711  1.1      rjs 
   1712  1.1      rjs 	if (addr != NULL) {
   1713  1.1      rjs 		if (addr->sa_family == AF_INET) {
   1714  1.1      rjs 			struct sockaddr_in *sin;
   1715  1.1      rjs 
   1716  1.1      rjs 			/* IPV6_V6ONLY socket? */
   1717  1.1      rjs 			if (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY) {
   1718  1.1      rjs 				return (EINVAL);
   1719  1.1      rjs 			}
   1720  1.1      rjs 
   1721  1.1      rjs 			if (addr->sa_len != sizeof(*sin))
   1722  1.1      rjs 				return (EINVAL);
   1723  1.1      rjs 
   1724  1.1      rjs 			sin = (struct sockaddr_in *)addr;
   1725  1.1      rjs 			lport = sin->sin_port;
   1726  1.1      rjs 
   1727  1.1      rjs 			if (sin->sin_addr.s_addr != INADDR_ANY) {
   1728  1.1      rjs 				bindall = 0;
   1729  1.1      rjs 			}
   1730  1.1      rjs 		} else if (addr->sa_family == AF_INET6) {
   1731  1.1      rjs 			/* Only for pure IPv6 Address. (No IPv4 Mapped!) */
   1732  1.1      rjs 			struct sockaddr_in6 *sin6;
   1733  1.1      rjs 
   1734  1.1      rjs 			sin6 = (struct sockaddr_in6 *)addr;
   1735  1.1      rjs 
   1736  1.1      rjs 			if (addr->sa_len != sizeof(*sin6))
   1737  1.1      rjs 				return (EINVAL);
   1738  1.1      rjs 
   1739  1.1      rjs 			lport = sin6->sin6_port;
   1740  1.1      rjs 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1741  1.1      rjs 				bindall = 0;
   1742  1.1      rjs 				/* KAME hack: embed scopeid */
   1743  1.1      rjs 				error = sa6_embedscope(sin6, ip6_use_defzone);
   1744  1.1      rjs 				if (error != 0)
   1745  1.1      rjs 					return (error);
   1746  1.1      rjs 			}
   1747  1.1      rjs #ifndef SCOPEDROUTING
   1748  1.1      rjs 			/* this must be cleared for ifa_ifwithaddr() */
   1749  1.1      rjs 			sin6->sin6_scope_id = 0;
   1750  1.1      rjs #endif /* SCOPEDROUTING */
   1751  1.1      rjs 		} else {
   1752  1.1      rjs 			return (EAFNOSUPPORT);
   1753  1.1      rjs 		}
   1754  1.1      rjs 	}
   1755  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   1756  1.1      rjs #ifdef SCTP_DEBUG
   1757  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1758  1.1      rjs 		printf("sctp_inpcb_bind: after SCTP_INP_INFO_WLOCK\n");
   1759  1.1      rjs 	}
   1760  1.1      rjs #endif /* SCTP_DEBUG */
   1761  1.1      rjs 	SCTP_INP_WLOCK(inp);
   1762  1.1      rjs 	/* increase our count due to the unlock we do */
   1763  1.1      rjs 	SCTP_INP_INCR_REF(inp);
   1764  1.1      rjs 	if (lport) {
   1765  1.1      rjs 		enum kauth_network_req req;
   1766  1.1      rjs 		/*
   1767  1.1      rjs 		 * Did the caller specify a port? if so we must see if a
   1768  1.1      rjs 		 * ep already has this one bound.
   1769  1.1      rjs 		 */
   1770  1.1      rjs 		if (ntohs(lport) < IPPORT_RESERVED)
   1771  1.1      rjs 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
   1772  1.1      rjs 		else
   1773  1.1      rjs 			req = KAUTH_REQ_NETWORK_BIND_PORT;
   1774  1.1      rjs 
   1775  1.1      rjs 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
   1776  1.1      rjs 		    req, so, addr, NULL);
   1777  1.1      rjs 		if (error) {
   1778  1.1      rjs 			SCTP_INP_DECR_REF(inp);
   1779  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
   1780  1.1      rjs 			SCTP_INP_INFO_WUNLOCK();
   1781  1.1      rjs 			return (EACCES);
   1782  1.1      rjs 		}
   1783  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   1784  1.1      rjs 		inp_tmp = sctp_pcb_findep(addr, 0, 1);
   1785  1.1      rjs 		if (inp_tmp != NULL) {
   1786  1.1      rjs 			/* lock guy returned and lower count
   1787  1.1      rjs 			 * note that we are not bound so inp_tmp
   1788  1.1      rjs 			 * should NEVER be inp. And it is this
   1789  1.1      rjs 			 * inp (inp_tmp) that gets the reference
   1790  1.1      rjs 			 * bump, so we must lower it.
   1791  1.1      rjs 			 */
   1792  1.1      rjs 			SCTP_INP_WLOCK(inp_tmp);
   1793  1.1      rjs 			SCTP_INP_DECR_REF(inp_tmp);
   1794  1.1      rjs 			SCTP_INP_WUNLOCK(inp_tmp);
   1795  1.1      rjs 
   1796  1.1      rjs 			/* unlock info */
   1797  1.1      rjs 			SCTP_INP_INFO_WUNLOCK();
   1798  1.1      rjs 			return (EADDRNOTAVAIL);
   1799  1.1      rjs 		}
   1800  1.1      rjs 		SCTP_INP_WLOCK(inp);
   1801  1.1      rjs 		if (bindall) {
   1802  1.1      rjs 			/* verify that no lport is not used by a singleton */
   1803  1.1      rjs 			if (sctp_isport_inuse(inp, lport)) {
   1804  1.1      rjs 				/* Sorry someone already has this one bound */
   1805  1.1      rjs 				SCTP_INP_DECR_REF(inp);
   1806  1.1      rjs 				SCTP_INP_WUNLOCK(inp);
   1807  1.1      rjs 				SCTP_INP_INFO_WUNLOCK();
   1808  1.1      rjs 				return (EADDRNOTAVAIL);
   1809  1.1      rjs 			}
   1810  1.1      rjs 		}
   1811  1.1      rjs 	} else {
   1812  1.1      rjs 		/*
   1813  1.1      rjs 		 * get any port but lets make sure no one has any address
   1814  1.1      rjs 		 * with this port bound
   1815  1.1      rjs 		 */
   1816  1.1      rjs 
   1817  1.1      rjs 		/*
   1818  1.1      rjs 		 * setup the inp to the top (I could use the union but this
   1819  1.1      rjs 		 * is just as easy
   1820  1.1      rjs 		 */
   1821  1.1      rjs 		uint32_t port_guess;
   1822  1.1      rjs 		uint16_t port_attempt;
   1823  1.1      rjs 		int not_done=1;
   1824  1.1      rjs 
   1825  1.1      rjs 		while (not_done) {
   1826  1.1      rjs 			port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
   1827  1.1      rjs 			port_attempt = (port_guess &  0x0000ffff);
   1828  1.1      rjs 			if (port_attempt == 0) {
   1829  1.1      rjs 				goto next_half;
   1830  1.1      rjs 			}
   1831  1.1      rjs 			if (port_attempt < IPPORT_RESERVED) {
   1832  1.1      rjs 				port_attempt += IPPORT_RESERVED;
   1833  1.1      rjs 			}
   1834  1.1      rjs 
   1835  1.1      rjs 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
   1836  1.1      rjs 				/* got a port we can use */
   1837  1.1      rjs 				not_done = 0;
   1838  1.1      rjs 				continue;
   1839  1.1      rjs 			}
   1840  1.1      rjs 			/* try upper half */
   1841  1.1      rjs 		next_half:
   1842  1.1      rjs 			port_attempt = ((port_guess >> 16) &  0x0000ffff);
   1843  1.1      rjs 			if (port_attempt == 0) {
   1844  1.1      rjs 				goto last_try;
   1845  1.1      rjs 			}
   1846  1.1      rjs 			if (port_attempt < IPPORT_RESERVED) {
   1847  1.1      rjs 				port_attempt += IPPORT_RESERVED;
   1848  1.1      rjs 			}
   1849  1.1      rjs 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
   1850  1.1      rjs 				/* got a port we can use */
   1851  1.1      rjs 				not_done = 0;
   1852  1.1      rjs 				continue;
   1853  1.1      rjs 			}
   1854  1.1      rjs 			/* try two half's added together */
   1855  1.1      rjs 		last_try:
   1856  1.1      rjs 			port_attempt = (((port_guess >> 16) &  0x0000ffff) + (port_guess & 0x0000ffff));
   1857  1.1      rjs 			if (port_attempt == 0) {
   1858  1.1      rjs 				/* get a new random number */
   1859  1.1      rjs 				continue;
   1860  1.1      rjs 			}
   1861  1.1      rjs 			if (port_attempt < IPPORT_RESERVED) {
   1862  1.1      rjs 				port_attempt += IPPORT_RESERVED;
   1863  1.1      rjs 			}
   1864  1.1      rjs 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
   1865  1.1      rjs 				/* got a port we can use */
   1866  1.1      rjs 				not_done = 0;
   1867  1.1      rjs 				continue;
   1868  1.1      rjs 			}
   1869  1.1      rjs 		}
   1870  1.1      rjs 		/* we don't get out of the loop until we have a port */
   1871  1.1      rjs 		lport = htons(port_attempt);
   1872  1.1      rjs 	}
   1873  1.1      rjs 	SCTP_INP_DECR_REF(inp);
   1874  1.1      rjs 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   1875  1.1      rjs 		/* this really should not happen. The guy
   1876  1.1      rjs 		 * did a non-blocking bind and then did a close
   1877  1.1      rjs 		 * at the same time.
   1878  1.1      rjs 		 */
   1879  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   1880  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   1881  1.1      rjs 		return (EINVAL);
   1882  1.1      rjs 	}
   1883  1.1      rjs 	/* ok we look clear to give out this port, so lets setup the binding */
   1884  1.1      rjs 	if (bindall) {
   1885  1.1      rjs 		/* binding to all addresses, so just set in the proper flags */
   1886  1.1      rjs 		inp->sctp_flags |= (SCTP_PCB_FLAGS_BOUNDALL |
   1887  1.1      rjs 		    SCTP_PCB_FLAGS_DO_ASCONF);
   1888  1.1      rjs 		/* set the automatic addr changes from kernel flag */
   1889  1.1      rjs 		if (sctp_auto_asconf == 0) {
   1890  1.1      rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
   1891  1.1      rjs 		} else {
   1892  1.1      rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
   1893  1.1      rjs 		}
   1894  1.1      rjs 	} else {
   1895  1.1      rjs 		/*
   1896  1.1      rjs 		 * bind specific, make sure flags is off and add a new address
   1897  1.1      rjs 		 * structure to the sctp_addr_list inside the ep structure.
   1898  1.1      rjs 		 *
   1899  1.1      rjs 		 * We will need to allocate one and insert it at the head.
   1900  1.1      rjs 		 * The socketopt call can just insert new addresses in there
   1901  1.1      rjs 		 * as well. It will also have to do the embed scope kame hack
   1902  1.1      rjs 		 * too (before adding).
   1903  1.1      rjs 		 */
   1904  1.1      rjs 		struct ifaddr *ifa;
   1905  1.1      rjs 		struct sockaddr_storage store_sa;
   1906  1.1      rjs 
   1907  1.1      rjs 		memset(&store_sa, 0, sizeof(store_sa));
   1908  1.1      rjs 		if (addr->sa_family == AF_INET) {
   1909  1.1      rjs 			struct sockaddr_in *sin;
   1910  1.1      rjs 
   1911  1.1      rjs 			sin = (struct sockaddr_in *)&store_sa;
   1912  1.1      rjs 			memcpy(sin, addr, sizeof(struct sockaddr_in));
   1913  1.1      rjs 			sin->sin_port = 0;
   1914  1.1      rjs 		} else if (addr->sa_family == AF_INET6) {
   1915  1.1      rjs 			struct sockaddr_in6 *sin6;
   1916  1.1      rjs 
   1917  1.1      rjs 			sin6 = (struct sockaddr_in6 *)&store_sa;
   1918  1.1      rjs 			memcpy(sin6, addr, sizeof(struct sockaddr_in6));
   1919  1.1      rjs 			sin6->sin6_port = 0;
   1920  1.1      rjs 		}
   1921  1.1      rjs 		/*
   1922  1.1      rjs 		 * first find the interface with the bound address
   1923  1.1      rjs 		 * need to zero out the port to find the address! yuck!
   1924  1.1      rjs 		 * can't do this earlier since need port for sctp_pcb_findep()
   1925  1.1      rjs 		 */
   1926  1.1      rjs 		ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
   1927  1.1      rjs 		if (ifa == NULL) {
   1928  1.1      rjs 			/* Can't find an interface with that address */
   1929  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
   1930  1.1      rjs 			SCTP_INP_INFO_WUNLOCK();
   1931  1.1      rjs 			return (EADDRNOTAVAIL);
   1932  1.1      rjs 		}
   1933  1.1      rjs 		if (addr->sa_family == AF_INET6) {
   1934  1.1      rjs 			struct in6_ifaddr *ifa6;
   1935  1.1      rjs 			ifa6 = (struct in6_ifaddr *)ifa;
   1936  1.1      rjs 			/*
   1937  1.1      rjs 			 * allow binding of deprecated addresses as per
   1938  1.1      rjs 			 * RFC 2462 and ipng discussion
   1939  1.1      rjs 			 */
   1940  1.1      rjs 			if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
   1941  1.1      rjs 			    IN6_IFF_ANYCAST |
   1942  1.1      rjs 			    IN6_IFF_NOTREADY)) {
   1943  1.1      rjs 				/* Can't bind a non-existent addr. */
   1944  1.1      rjs 				SCTP_INP_WUNLOCK(inp);
   1945  1.1      rjs 				SCTP_INP_INFO_WUNLOCK();
   1946  1.1      rjs 				return (EINVAL);
   1947  1.1      rjs 			}
   1948  1.1      rjs 		}
   1949  1.1      rjs 		/* we're not bound all */
   1950  1.1      rjs 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
   1951  1.1      rjs #if 0 /* use sysctl now */
   1952  1.1      rjs 		/* don't allow automatic addr changes from kernel */
   1953  1.1      rjs 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
   1954  1.1      rjs #endif
   1955  1.1      rjs 		/* set the automatic addr changes from kernel flag */
   1956  1.1      rjs 		if (sctp_auto_asconf == 0) {
   1957  1.1      rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
   1958  1.1      rjs 		} else {
   1959  1.1      rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
   1960  1.1      rjs 		}
   1961  1.1      rjs 		/* allow bindx() to send ASCONF's for binding changes */
   1962  1.1      rjs 		inp->sctp_flags |= SCTP_PCB_FLAGS_DO_ASCONF;
   1963  1.1      rjs 		/* add this address to the endpoint list */
   1964  1.1      rjs 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
   1965  1.1      rjs 		if (error != 0) {
   1966  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
   1967  1.1      rjs 			SCTP_INP_INFO_WUNLOCK();
   1968  1.1      rjs 			return (error);
   1969  1.1      rjs 		}
   1970  1.1      rjs 		inp->laddr_count++;
   1971  1.1      rjs 	}
   1972  1.1      rjs 	/* find the bucket */
   1973  1.1      rjs 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
   1974  1.1      rjs 	    sctppcbinfo.hashmark)];
   1975  1.1      rjs 	/* put it in the bucket */
   1976  1.1      rjs 	LIST_INSERT_HEAD(head, inp, sctp_hash);
   1977  1.1      rjs #ifdef SCTP_DEBUG
   1978  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1979  1.1      rjs 		printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
   1980  1.1      rjs 	}
   1981  1.1      rjs #endif
   1982  1.1      rjs 	/* set in the port */
   1983  1.1      rjs 	inp->sctp_lport = lport;
   1984  1.1      rjs 
   1985  1.1      rjs 	/* turn off just the unbound flag */
   1986  1.1      rjs 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
   1987  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   1988  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   1989  1.1      rjs 	return (0);
   1990  1.1      rjs }
   1991  1.1      rjs 
   1992  1.1      rjs 
   1993  1.1      rjs static void
   1994  1.1      rjs sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
   1995  1.1      rjs {
   1996  1.1      rjs 	struct sctp_iterator *it;
   1997  1.1      rjs 	/* We enter with the only the ITERATOR_LOCK in place and
   1998  1.1      rjs 	 * A write lock on the inp_info stuff.
   1999  1.1      rjs 	 */
   2000  1.1      rjs 
   2001  1.1      rjs 	/* Go through all iterators, we must do this since
   2002  1.1      rjs 	 * it is possible that some iterator does NOT have
   2003  1.1      rjs 	 * the lock, but is waiting for it. And the one that
   2004  1.1      rjs 	 * had the lock has either moved in the last iteration
   2005  1.1      rjs 	 * or we just cleared it above. We need to find all
   2006  1.1      rjs 	 * of those guys. The list of iterators should never
   2007  1.1      rjs 	 * be very big though.
   2008  1.1      rjs 	 */
   2009  1.1      rjs  	LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
   2010  1.1      rjs 		if (it == inp->inp_starting_point_for_iterator)
   2011  1.1      rjs 			/* skip this guy, he's special */
   2012  1.1      rjs 			continue;
   2013  1.1      rjs  		if (it->inp == inp) {
   2014  1.1      rjs 			/* This is tricky and we DON'T lock the iterator.
   2015  1.1      rjs 			 * Reason is he's running but waiting for me since
   2016  1.1      rjs 			 * inp->inp_starting_point_for_iterator has the lock
   2017  1.1      rjs 			 * on me (the guy above we skipped). This tells us
   2018  1.1      rjs 			 * its is not running but waiting for inp->inp_starting_point_for_iterator
   2019  1.1      rjs 			 * to be released by the guy that does have our INP in a lock.
   2020  1.1      rjs 			 */
   2021  1.1      rjs 			if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
   2022  1.1      rjs 				it->inp = NULL;
   2023  1.1      rjs 				it->stcb = NULL;
   2024  1.1      rjs 			} else {
   2025  1.1      rjs 				/* set him up to do the next guy not me */
   2026  1.1      rjs 				it->inp = inp_next;
   2027  1.1      rjs 				it->stcb = NULL;
   2028  1.1      rjs 			}
   2029  1.1      rjs 		}
   2030  1.1      rjs 	}
   2031  1.1      rjs 	it = inp->inp_starting_point_for_iterator;
   2032  1.1      rjs 	if (it) {
   2033  1.1      rjs 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
   2034  1.1      rjs 			it->inp = NULL;
   2035  1.1      rjs 		} else {
   2036  1.1      rjs 			it->inp = inp_next;
   2037  1.1      rjs 		}
   2038  1.1      rjs 		it->stcb = NULL;
   2039  1.1      rjs 	}
   2040  1.1      rjs }
   2041  1.1      rjs 
   2042  1.1      rjs /* release sctp_inpcb unbind the port */
   2043  1.1      rjs void
   2044  1.1      rjs sctp_inpcb_free(struct sctp_inpcb *inp, int immediate)
   2045  1.1      rjs {
   2046  1.1      rjs 	/*
   2047  1.1      rjs 	 * Here we free a endpoint. We must find it (if it is in the Hash
   2048  1.1      rjs 	 * table) and remove it from there. Then we must also find it in
   2049  1.1      rjs 	 * the overall list and remove it from there. After all removals are
   2050  1.1      rjs 	 * complete then any timer has to be stopped. Then start the actual
   2051  1.1      rjs 	 * freeing.
   2052  1.1      rjs 	 * a) Any local lists.
   2053  1.1      rjs 	 * b) Any associations.
   2054  1.1      rjs 	 * c) The hash of all associations.
   2055  1.1      rjs 	 * d) finally the ep itself.
   2056  1.1      rjs 	 */
   2057  1.1      rjs 	struct sctp_inpcb *inp_save;
   2058  1.1      rjs 	struct sctp_tcb *asoc, *nasoc;
   2059  1.1      rjs 	struct sctp_laddr *laddr, *nladdr;
   2060  1.1      rjs 	struct inpcb *ip_pcb;
   2061  1.1      rjs 	struct socket *so;
   2062  1.1      rjs 	struct sctp_socket_q_list *sq;
   2063  1.1      rjs 	int s, cnt;
   2064  1.1      rjs 
   2065  1.1      rjs 	s = splsoftnet();
   2066  1.1      rjs 	SCTP_ASOC_CREATE_LOCK(inp);
   2067  1.1      rjs 	SCTP_INP_WLOCK(inp);
   2068  1.1      rjs 
   2069  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
   2070  1.1      rjs 		/* been here before */
   2071  1.1      rjs 		splx(s);
   2072  1.1      rjs 		printf("Endpoint was all gone (dup free)?\n");
   2073  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   2074  1.1      rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   2075  1.1      rjs 		return;
   2076  1.1      rjs 	}
   2077  1.1      rjs 	sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
   2078  1.1      rjs 
   2079  1.1      rjs 	if (inp->control) {
   2080  1.1      rjs 		sctp_m_freem(inp->control);
   2081  1.1      rjs 		inp->control = NULL;
   2082  1.1      rjs 	}
   2083  1.1      rjs 	if (inp->pkt) {
   2084  1.1      rjs 		sctp_m_freem(inp->pkt);
   2085  1.1      rjs 		inp->pkt = NULL;
   2086  1.1      rjs 	}
   2087  1.1      rjs 	so  = inp->sctp_socket;
   2088  1.1      rjs 	ip_pcb = &inp->ip_inp.inp; /* we could just cast the main
   2089  1.1      rjs 				   * pointer here but I will
   2090  1.1      rjs 				   * be nice :> (i.e. ip_pcb = ep;)
   2091  1.1      rjs 				   */
   2092  1.1      rjs 
   2093  1.1      rjs 	if (immediate == 0) {
   2094  1.1      rjs 		int cnt_in_sd;
   2095  1.1      rjs 		cnt_in_sd = 0;
   2096  1.1      rjs 		for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
   2097  1.1      rjs 		     asoc = nasoc) {
   2098  1.1      rjs 			nasoc = LIST_NEXT(asoc, sctp_tcblist);
   2099  1.1      rjs 			if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
   2100  1.1      rjs 			    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
   2101  1.1      rjs 				/* Just abandon things in the front states */
   2102  1.1      rjs 				SCTP_TCB_LOCK(asoc);
   2103  1.1      rjs 				SCTP_INP_WUNLOCK(inp);
   2104  1.1      rjs 				sctp_free_assoc(inp, asoc);
   2105  1.1      rjs 				SCTP_INP_WLOCK(inp);
   2106  1.1      rjs 				continue;
   2107  1.1      rjs 			} else {
   2108  1.1      rjs 				asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
   2109  1.1      rjs 			}
   2110  1.1      rjs 			if ((asoc->asoc.size_on_delivery_queue  > 0) ||
   2111  1.1      rjs 			    (asoc->asoc.size_on_reasm_queue > 0) ||
   2112  1.1      rjs 			    (asoc->asoc.size_on_all_streams > 0) ||
   2113  1.1      rjs 			    (so && (so->so_rcv.sb_cc > 0))
   2114  1.1      rjs 				) {
   2115  1.1      rjs 				/* Left with Data unread */
   2116  1.1      rjs 				struct mbuf *op_err;
   2117  1.1      rjs 				MGET(op_err, M_DONTWAIT, MT_DATA);
   2118  1.1      rjs 				if (op_err) {
   2119  1.1      rjs 					/* Fill in the user initiated abort */
   2120  1.1      rjs 					struct sctp_paramhdr *ph;
   2121  1.1      rjs 					op_err->m_len =
   2122  1.1      rjs 					    sizeof(struct sctp_paramhdr);
   2123  1.1      rjs 					ph = mtod(op_err,
   2124  1.1      rjs 					    struct sctp_paramhdr *);
   2125  1.1      rjs 					ph->param_type = htons(
   2126  1.1      rjs 					    SCTP_CAUSE_USER_INITIATED_ABT);
   2127  1.1      rjs 					ph->param_length = htons(op_err->m_len);
   2128  1.1      rjs 				}
   2129  1.1      rjs 				SCTP_TCB_LOCK(asoc);
   2130  1.1      rjs 				sctp_send_abort_tcb(asoc, op_err);
   2131  1.1      rjs 
   2132  1.1      rjs 				SCTP_INP_WUNLOCK(inp);
   2133  1.1      rjs 				sctp_free_assoc(inp, asoc);
   2134  1.1      rjs 				SCTP_INP_WLOCK(inp);
   2135  1.1      rjs 				continue;
   2136  1.1      rjs 			} else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
   2137  1.1      rjs 			    TAILQ_EMPTY(&asoc->asoc.sent_queue)) {
   2138  1.1      rjs 				if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
   2139  1.1      rjs 				    (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
   2140  1.1      rjs 					/* there is nothing queued to send, so I send shutdown */
   2141  1.1      rjs 					SCTP_TCB_LOCK(asoc);
   2142  1.1      rjs 					sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
   2143  1.1      rjs 					asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
   2144  1.1      rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
   2145  1.1      rjs 							 asoc->asoc.primary_destination);
   2146  1.1      rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
   2147  1.1      rjs 							 asoc->asoc.primary_destination);
   2148  1.1      rjs 					sctp_chunk_output(inp, asoc, 1);
   2149  1.1      rjs 					SCTP_TCB_UNLOCK(asoc);
   2150  1.1      rjs 				}
   2151  1.1      rjs 			} else {
   2152  1.1      rjs 				/* mark into shutdown pending */
   2153  1.1      rjs 				asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
   2154  1.1      rjs 			}
   2155  1.1      rjs 			cnt_in_sd++;
   2156  1.1      rjs 		}
   2157  1.1      rjs 		/* now is there some left in our SHUTDOWN state? */
   2158  1.1      rjs 		if (cnt_in_sd) {
   2159  1.1      rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE;
   2160  1.1      rjs 			splx(s);
   2161  1.1      rjs 			SCTP_INP_WUNLOCK(inp);
   2162  1.1      rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
   2163  1.1      rjs 			return;
   2164  1.1      rjs 		}
   2165  1.1      rjs 	}
   2166  1.1      rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
   2167  1.1      rjs 	if (inp->refcount) {
   2168  1.1      rjs 		sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
   2169  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   2170  1.1      rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   2171  1.1      rjs 		return;
   2172  1.1      rjs 	}
   2173  1.1      rjs #endif
   2174  1.1      rjs 	inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
   2175  1.1      rjs 
   2176  1.1      rjs 	rtcache_validate(&ip_pcb->inp_route);
   2177  1.1      rjs 
   2178  1.1      rjs 	callout_stop(&inp->sctp_ep.signature_change.timer);
   2179  1.1      rjs 	callout_destroy(&inp->sctp_ep.signature_change.timer);
   2180  1.1      rjs 
   2181  1.1      rjs 	if (so) {
   2182  1.1      rjs 	/* First take care of socket level things */
   2183  1.1      rjs #ifdef IPSEC
   2184  1.1      rjs 		ipsec4_delete_pcbpolicy(ip_pcb);
   2185  1.1      rjs #endif /*IPSEC*/
   2186  1.1      rjs 		so->so_pcb = 0;
   2187  1.1      rjs 		sofree(so);
   2188  1.1      rjs 	}
   2189  1.1      rjs 
   2190  1.1      rjs 	if (ip_pcb->inp_options) {
   2191  1.1      rjs 		(void)m_free(ip_pcb->inp_options);
   2192  1.1      rjs 		ip_pcb->inp_options = 0;
   2193  1.1      rjs 	}
   2194  1.1      rjs 	rtcache_free(&ip_pcb->inp_route);
   2195  1.1      rjs 	if (ip_pcb->inp_moptions) {
   2196  1.1      rjs 		ip_freemoptions(ip_pcb->inp_moptions);
   2197  1.1      rjs 		ip_pcb->inp_moptions = 0;
   2198  1.1      rjs 	}
   2199  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   2200  1.1      rjs 	inp->inp_vflag = 0;
   2201  1.1      rjs #else
   2202  1.1      rjs 	ip_pcb->inp_vflag = 0;
   2203  1.1      rjs #endif
   2204  1.1      rjs 
   2205  1.1      rjs 	/* Now the sctp_pcb things */
   2206  1.1      rjs 	/*
   2207  1.1      rjs 	 * free each asoc if it is not already closed/free. we can't use
   2208  1.1      rjs 	 * the macro here since le_next will get freed as part of the
   2209  1.1      rjs 	 * sctp_free_assoc() call.
   2210  1.1      rjs 	 */
   2211  1.1      rjs 	cnt = 0;
   2212  1.1      rjs 	for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
   2213  1.1      rjs 	     asoc = nasoc) {
   2214  1.1      rjs 		nasoc = LIST_NEXT(asoc, sctp_tcblist);
   2215  1.1      rjs 		SCTP_TCB_LOCK(asoc);
   2216  1.1      rjs 		if (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) {
   2217  1.1      rjs 			struct mbuf *op_err;
   2218  1.1      rjs 			MGET(op_err, M_DONTWAIT, MT_DATA);
   2219  1.1      rjs 			if (op_err) {
   2220  1.1      rjs 				/* Fill in the user initiated abort */
   2221  1.1      rjs 				struct sctp_paramhdr *ph;
   2222  1.1      rjs 				op_err->m_len = sizeof(struct sctp_paramhdr);
   2223  1.1      rjs 				ph = mtod(op_err, struct sctp_paramhdr *);
   2224  1.1      rjs 				ph->param_type = htons(
   2225  1.1      rjs 				    SCTP_CAUSE_USER_INITIATED_ABT);
   2226  1.1      rjs 				ph->param_length = htons(op_err->m_len);
   2227  1.1      rjs 			}
   2228  1.1      rjs 			sctp_send_abort_tcb(asoc, op_err);
   2229  1.1      rjs 		}
   2230  1.1      rjs 		cnt++;
   2231  1.1      rjs 		/*
   2232  1.1      rjs 		 * sctp_free_assoc() will call sctp_inpcb_free(),
   2233  1.1      rjs 		 * if SCTP_PCB_FLAGS_SOCKET_GONE set.
   2234  1.1      rjs 		 * So, we clear it before sctp_free_assoc() making sure
   2235  1.1      rjs 		 * no double sctp_inpcb_free().
   2236  1.1      rjs 		 */
   2237  1.1      rjs 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_SOCKET_GONE;
   2238  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   2239  1.1      rjs 		sctp_free_assoc(inp, asoc);
   2240  1.1      rjs 		SCTP_INP_WLOCK(inp);
   2241  1.1      rjs 	}
   2242  1.1      rjs 	while ((sq = TAILQ_FIRST(&inp->sctp_queue_list)) != NULL) {
   2243  1.1      rjs 		TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
   2244  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
   2245  1.1      rjs 		sctppcbinfo.ipi_count_sockq--;
   2246  1.1      rjs 		sctppcbinfo.ipi_gencnt_sockq++;
   2247  1.1      rjs 	}
   2248  1.1      rjs 	inp->sctp_socket = 0;
   2249  1.1      rjs 	/* Now first we remove ourselves from the overall list of all EP's */
   2250  1.1      rjs 
   2251  1.1      rjs 	/* Unlock inp first, need correct order */
   2252  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   2253  1.1      rjs 	/* now iterator lock */
   2254  1.1      rjs 	SCTP_ITERATOR_LOCK();
   2255  1.1      rjs 	/* now info lock */
   2256  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   2257  1.1      rjs 	/* now reget the inp lock */
   2258  1.1      rjs 	SCTP_INP_WLOCK(inp);
   2259  1.1      rjs 
   2260  1.1      rjs 	inp_save = LIST_NEXT(inp, sctp_list);
   2261  1.1      rjs 	LIST_REMOVE(inp, sctp_list);
   2262  1.1      rjs 	/*
   2263  1.1      rjs 	 * Now the question comes as to if this EP was ever bound at all.
   2264  1.1      rjs 	 * If it was, then we must pull it out of the EP hash list.
   2265  1.1      rjs 	 */
   2266  1.1      rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
   2267  1.1      rjs 	    SCTP_PCB_FLAGS_UNBOUND) {
   2268  1.1      rjs 		/*
   2269  1.1      rjs 		 * ok, this guy has been bound. It's port is somewhere
   2270  1.1      rjs 		 * in the sctppcbinfo hash table. Remove it!
   2271  1.1      rjs 		 */
   2272  1.1      rjs 		LIST_REMOVE(inp, sctp_hash);
   2273  1.1      rjs 	}
   2274  1.1      rjs         /* fix any iterators only after out of the list */
   2275  1.1      rjs 	sctp_iterator_inp_being_freed(inp, inp_save);
   2276  1.1      rjs 	SCTP_ITERATOR_UNLOCK();
   2277  1.1      rjs 	/*
   2278  1.1      rjs 	 * if we have an address list the following will free the list of
   2279  1.1      rjs 	 * ifaddr's that are set into this ep. Again macro limitations here,
   2280  1.1      rjs 	 * since the LIST_FOREACH could be a bad idea.
   2281  1.1      rjs 	 */
   2282  1.1      rjs 	for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
   2283  1.1      rjs 	     laddr = nladdr) {
   2284  1.1      rjs 		nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
   2285  1.1      rjs 		LIST_REMOVE(laddr, sctp_nxt_addr);
   2286  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
   2287  1.1      rjs 		sctppcbinfo.ipi_gencnt_laddr++;
   2288  1.1      rjs 		sctppcbinfo.ipi_count_laddr--;
   2289  1.1      rjs 	}
   2290  1.1      rjs 
   2291  1.1      rjs 	/* Now lets see about freeing the EP hash table. */
   2292  1.1      rjs 	if (inp->sctp_tcbhash != NULL) {
   2293  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash, inp->sctp_tcbhash);
   2294  1.1      rjs 		inp->sctp_tcbhash = NULL;
   2295  1.1      rjs 	}
   2296  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   2297  1.1      rjs 	SCTP_ASOC_CREATE_UNLOCK(inp);
   2298  1.1      rjs 	SCTP_INP_LOCK_DESTROY(inp);
   2299  1.1      rjs 	SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
   2300  1.1      rjs 
   2301  1.1      rjs 	/* Now we must put the ep memory back into the zone pool */
   2302  1.1      rjs 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
   2303  1.1      rjs 	sctppcbinfo.ipi_count_ep--;
   2304  1.1      rjs 
   2305  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   2306  1.1      rjs 	splx(s);
   2307  1.1      rjs }
   2308  1.1      rjs 
   2309  1.1      rjs 
   2310  1.1      rjs struct sctp_nets *
   2311  1.1      rjs sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
   2312  1.1      rjs {
   2313  1.1      rjs 	struct sctp_nets *net;
   2314  1.1      rjs 
   2315  1.1      rjs 	/* use the peer's/remote port for lookup if unspecified */
   2316  1.1      rjs #if 0 /* why do we need to check the port for a nets list on an assoc? */
   2317  1.1      rjs 	if (stcb->rport != sin->sin_port) {
   2318  1.1      rjs 		/* we cheat and just a sin for this test */
   2319  1.1      rjs 		return (NULL);
   2320  1.1      rjs 	}
   2321  1.1      rjs #endif
   2322  1.1      rjs 	/* locate the address */
   2323  1.1      rjs 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   2324  1.1      rjs 		if (sctp_cmpaddr(addr, rtcache_getdst(&net->ro)))
   2325  1.1      rjs 			return (net);
   2326  1.1      rjs 	}
   2327  1.1      rjs 	return (NULL);
   2328  1.1      rjs }
   2329  1.1      rjs 
   2330  1.1      rjs 
   2331  1.1      rjs /*
   2332  1.1      rjs  * add's a remote endpoint address, done with the INIT/INIT-ACK
   2333  1.1      rjs  * as well as when a ASCONF arrives that adds it. It will also
   2334  1.1      rjs  * initialize all the cwnd stats of stuff.
   2335  1.1      rjs  */
   2336  1.1      rjs int
   2337  1.1      rjs sctp_is_address_on_local_host(struct sockaddr *addr)
   2338  1.1      rjs {
   2339  1.1      rjs 	struct ifnet *ifn;
   2340  1.1      rjs 	struct ifaddr *ifa;
   2341  1.5    ozaki 	int s;
   2342  1.5    ozaki 
   2343  1.5    ozaki 	s = pserialize_read_enter();
   2344  1.5    ozaki 	IFNET_READER_FOREACH(ifn) {
   2345  1.2  mlelstv 		IFADDR_FOREACH(ifa, ifn) {
   2346  1.1      rjs 			if (addr->sa_family == ifa->ifa_addr->sa_family) {
   2347  1.1      rjs 				/* same family */
   2348  1.1      rjs 				if (addr->sa_family == AF_INET) {
   2349  1.1      rjs 					struct sockaddr_in *sin, *sin_c;
   2350  1.1      rjs 					sin = (struct sockaddr_in *)addr;
   2351  1.1      rjs 					sin_c = (struct sockaddr_in *)
   2352  1.1      rjs 					    ifa->ifa_addr;
   2353  1.1      rjs 					if (sin->sin_addr.s_addr ==
   2354  1.1      rjs 					    sin_c->sin_addr.s_addr) {
   2355  1.1      rjs 						/* we are on the same machine */
   2356  1.5    ozaki 						pserialize_read_exit(s);
   2357  1.1      rjs 						return (1);
   2358  1.1      rjs 					}
   2359  1.1      rjs 				} else if (addr->sa_family == AF_INET6) {
   2360  1.1      rjs 					struct sockaddr_in6 *sin6, *sin_c6;
   2361  1.1      rjs 					sin6 = (struct sockaddr_in6 *)addr;
   2362  1.1      rjs 					sin_c6 = (struct sockaddr_in6 *)
   2363  1.1      rjs 					    ifa->ifa_addr;
   2364  1.1      rjs 					if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
   2365  1.1      rjs 					    &sin_c6->sin6_addr)) {
   2366  1.1      rjs 						/* we are on the same machine */
   2367  1.5    ozaki 						pserialize_read_exit(s);
   2368  1.1      rjs 						return (1);
   2369  1.1      rjs 					}
   2370  1.1      rjs 				}
   2371  1.1      rjs 			}
   2372  1.1      rjs 		}
   2373  1.1      rjs 	}
   2374  1.5    ozaki 	pserialize_read_exit(s);
   2375  1.5    ozaki 
   2376  1.1      rjs 	return (0);
   2377  1.1      rjs }
   2378  1.1      rjs 
   2379  1.1      rjs int
   2380  1.1      rjs sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
   2381  1.1      rjs     int set_scope, int from)
   2382  1.1      rjs {
   2383  1.1      rjs 	/*
   2384  1.1      rjs 	 * The following is redundant to the same lines in the
   2385  1.1      rjs 	 * sctp_aloc_assoc() but is needed since other's call the add
   2386  1.1      rjs 	 * address function
   2387  1.1      rjs 	 */
   2388  1.1      rjs 	struct sctp_nets *net, *netfirst;
   2389  1.1      rjs 	struct rtentry *rt, *netfirst_rt;
   2390  1.1      rjs 	int addr_inscope;
   2391  1.1      rjs 
   2392  1.1      rjs #ifdef SCTP_DEBUG
   2393  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   2394  1.1      rjs 		printf("Adding an address (from:%d) to the peer: ", from);
   2395  1.1      rjs 		sctp_print_address(newaddr);
   2396  1.1      rjs 	}
   2397  1.1      rjs #endif
   2398  1.1      rjs 	netfirst = sctp_findnet(stcb, newaddr);
   2399  1.1      rjs 	if (netfirst) {
   2400  1.1      rjs 		/*
   2401  1.1      rjs 		 * Lie and return ok, we don't want to make the association
   2402  1.1      rjs 		 * go away for this behavior. It will happen in the TCP model
   2403  1.1      rjs 		 * in a connected socket. It does not reach the hash table
   2404  1.1      rjs 		 * until after the association is built so it can't be found.
   2405  1.1      rjs 		 * Mark as reachable, since the initial creation will have
   2406  1.1      rjs 		 * been cleared and the NOT_IN_ASSOC flag will have been
   2407  1.1      rjs 		 * added... and we don't want to end up removing it back out.
   2408  1.1      rjs 		 */
   2409  1.1      rjs 		if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
   2410  1.1      rjs 			netfirst->dest_state = (SCTP_ADDR_REACHABLE|
   2411  1.1      rjs 			    SCTP_ADDR_UNCONFIRMED);
   2412  1.1      rjs 		} else {
   2413  1.1      rjs 			netfirst->dest_state = SCTP_ADDR_REACHABLE;
   2414  1.1      rjs 		}
   2415  1.1      rjs 
   2416  1.1      rjs 		return (0);
   2417  1.1      rjs 	}
   2418  1.1      rjs 	addr_inscope = 1;
   2419  1.1      rjs 	if (newaddr->sa_family == AF_INET) {
   2420  1.1      rjs 		struct sockaddr_in *sin;
   2421  1.1      rjs 		sin = (struct sockaddr_in *)newaddr;
   2422  1.1      rjs 		if (sin->sin_addr.s_addr == 0) {
   2423  1.1      rjs 			/* Invalid address */
   2424  1.1      rjs 			return (-1);
   2425  1.1      rjs 		}
   2426  1.1      rjs 		/* zero out the bzero area */
   2427  1.1      rjs 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
   2428  1.1      rjs 
   2429  1.1      rjs 		/* assure len is set */
   2430  1.1      rjs 		sin->sin_len = sizeof(struct sockaddr_in);
   2431  1.1      rjs 		if (set_scope) {
   2432  1.1      rjs #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
   2433  1.1      rjs 			stcb->ipv4_local_scope = 1;
   2434  1.1      rjs #else
   2435  1.1      rjs 			if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
   2436  1.1      rjs 				stcb->asoc.ipv4_local_scope = 1;
   2437  1.1      rjs 			}
   2438  1.1      rjs #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
   2439  1.1      rjs 
   2440  1.1      rjs 			if (sctp_is_address_on_local_host(newaddr)) {
   2441  1.1      rjs 				stcb->asoc.loopback_scope = 1;
   2442  1.1      rjs 				stcb->asoc.ipv4_local_scope = 1;
   2443  1.1      rjs 				stcb->asoc.local_scope = 1;
   2444  1.1      rjs 				stcb->asoc.site_scope = 1;
   2445  1.1      rjs 			}
   2446  1.1      rjs 		} else {
   2447  1.1      rjs 			if (from == 8) {
   2448  1.1      rjs 				/* From connectx */
   2449  1.1      rjs 				if (sctp_is_address_on_local_host(newaddr)) {
   2450  1.1      rjs 					stcb->asoc.loopback_scope = 1;
   2451  1.1      rjs 					stcb->asoc.ipv4_local_scope = 1;
   2452  1.1      rjs 					stcb->asoc.local_scope = 1;
   2453  1.1      rjs 					stcb->asoc.site_scope = 1;
   2454  1.1      rjs 				}
   2455  1.1      rjs 			}
   2456  1.1      rjs 			/* Validate the address is in scope */
   2457  1.1      rjs 			if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
   2458  1.1      rjs 			    (stcb->asoc.ipv4_local_scope == 0)) {
   2459  1.1      rjs 				addr_inscope = 0;
   2460  1.1      rjs 			}
   2461  1.1      rjs 		}
   2462  1.1      rjs 	} else if (newaddr->sa_family == AF_INET6) {
   2463  1.1      rjs 		struct sockaddr_in6 *sin6;
   2464  1.1      rjs 		sin6 = (struct sockaddr_in6 *)newaddr;
   2465  1.1      rjs 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   2466  1.1      rjs 			/* Invalid address */
   2467  1.1      rjs 			return (-1);
   2468  1.1      rjs 		}
   2469  1.1      rjs 		/* assure len is set */
   2470  1.1      rjs 		sin6->sin6_len = sizeof(struct sockaddr_in6);
   2471  1.1      rjs 		if (set_scope) {
   2472  1.1      rjs 			if (sctp_is_address_on_local_host(newaddr)) {
   2473  1.1      rjs 				stcb->asoc.loopback_scope = 1;
   2474  1.1      rjs 				stcb->asoc.local_scope = 1;
   2475  1.1      rjs 				stcb->asoc.ipv4_local_scope = 1;
   2476  1.1      rjs 				stcb->asoc.site_scope = 1;
   2477  1.1      rjs 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   2478  1.1      rjs 				/*
   2479  1.1      rjs 				 * If the new destination is a LINK_LOCAL
   2480  1.1      rjs 				 * we must have common site scope. Don't set
   2481  1.1      rjs 				 * the local scope since we may not share all
   2482  1.1      rjs 				 * links, only loopback can do this.
   2483  1.1      rjs  				 * Links on the local network would also
   2484  1.1      rjs  				 * be on our private network for v4 too.
   2485  1.1      rjs 				 */
   2486  1.1      rjs  				stcb->asoc.ipv4_local_scope = 1;
   2487  1.1      rjs 				stcb->asoc.site_scope = 1;
   2488  1.1      rjs 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
   2489  1.1      rjs 				/*
   2490  1.1      rjs 				 * If the new destination is SITE_LOCAL
   2491  1.1      rjs 				 * then we must have site scope in common.
   2492  1.1      rjs 				 */
   2493  1.1      rjs 				stcb->asoc.site_scope = 1;
   2494  1.1      rjs 			}
   2495  1.1      rjs 		} else {
   2496  1.1      rjs 			if (from == 8) {
   2497  1.1      rjs 				/* From connectx */
   2498  1.1      rjs 				if (sctp_is_address_on_local_host(newaddr)) {
   2499  1.1      rjs 					stcb->asoc.loopback_scope = 1;
   2500  1.1      rjs 					stcb->asoc.ipv4_local_scope = 1;
   2501  1.1      rjs 					stcb->asoc.local_scope = 1;
   2502  1.1      rjs 					stcb->asoc.site_scope = 1;
   2503  1.1      rjs 				}
   2504  1.1      rjs 			}
   2505  1.1      rjs 			/* Validate the address is in scope */
   2506  1.1      rjs 			if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
   2507  1.1      rjs 			    (stcb->asoc.loopback_scope == 0)) {
   2508  1.1      rjs 				addr_inscope = 0;
   2509  1.1      rjs 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
   2510  1.1      rjs 				   (stcb->asoc.local_scope == 0)) {
   2511  1.1      rjs 				addr_inscope = 0;
   2512  1.1      rjs 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
   2513  1.1      rjs 				   (stcb->asoc.site_scope == 0)) {
   2514  1.1      rjs 				addr_inscope = 0;
   2515  1.1      rjs 			}
   2516  1.1      rjs 		}
   2517  1.1      rjs 	} else {
   2518  1.1      rjs 		/* not supported family type */
   2519  1.1      rjs 		return (-1);
   2520  1.1      rjs 	}
   2521  1.1      rjs 	net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
   2522  1.1      rjs 	if (net == NULL) {
   2523  1.1      rjs 		return (-1);
   2524  1.1      rjs 	}
   2525  1.1      rjs 	sctppcbinfo.ipi_count_raddr++;
   2526  1.1      rjs 	sctppcbinfo.ipi_gencnt_raddr++;
   2527  1.1      rjs 	memset(net, 0, sizeof(*net));
   2528  1.1      rjs 	if (newaddr->sa_family == AF_INET) {
   2529  1.1      rjs 		((struct sockaddr_in *)newaddr)->sin_port = stcb->rport;
   2530  1.1      rjs 	} else if (newaddr->sa_family == AF_INET6) {
   2531  1.1      rjs 		((struct sockaddr_in6 *)newaddr)->sin6_port = stcb->rport;
   2532  1.1      rjs 	}
   2533  1.1      rjs 	net->addr_is_local = sctp_is_address_on_local_host(newaddr);
   2534  1.1      rjs 	net->failure_threshold = stcb->asoc.def_net_failure;
   2535  1.1      rjs 	if (addr_inscope == 0) {
   2536  1.1      rjs #ifdef SCTP_DEBUG
   2537  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   2538  1.1      rjs 			printf("Adding an address which is OUT OF SCOPE\n");
   2539  1.1      rjs 		}
   2540  1.1      rjs #endif /* SCTP_DEBUG */
   2541  1.1      rjs 		net->dest_state = (SCTP_ADDR_REACHABLE |
   2542  1.1      rjs 		    SCTP_ADDR_OUT_OF_SCOPE);
   2543  1.1      rjs 	} else {
   2544  1.1      rjs 		if (from == 8)
   2545  1.1      rjs 			/* 8 is passed by connect_x */
   2546  1.1      rjs 			net->dest_state = SCTP_ADDR_REACHABLE;
   2547  1.1      rjs 		else
   2548  1.1      rjs 			net->dest_state = SCTP_ADDR_REACHABLE |
   2549  1.1      rjs 			    SCTP_ADDR_UNCONFIRMED;
   2550  1.1      rjs 	}
   2551  1.1      rjs 	net->RTO = stcb->asoc.initial_rto;
   2552  1.1      rjs 	stcb->asoc.numnets++;
   2553  1.1      rjs 	net->ref_count = 1;
   2554  1.1      rjs 
   2555  1.1      rjs 	/* Init the timer structure */
   2556  1.1      rjs 	callout_init(&net->rxt_timer.timer, 0);
   2557  1.1      rjs 	callout_init(&net->pmtu_timer.timer, 0);
   2558  1.1      rjs 
   2559  1.1      rjs 	/* Now generate a route for this guy */
   2560  1.1      rjs 	/* KAME hack: embed scope zone ID */
   2561  1.1      rjs 	if (newaddr->sa_family == AF_INET6) {
   2562  1.1      rjs 		struct sockaddr_in6 *sin6;
   2563  1.1      rjs 		sin6 = (struct sockaddr_in6 *)newaddr;
   2564  1.1      rjs 		if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
   2565  1.1      rjs 			return (-1);
   2566  1.1      rjs 	}
   2567  1.1      rjs 	rt = rtcache_lookup(&net->ro, newaddr);
   2568  1.1      rjs 	if (rt) {
   2569  1.1      rjs 		net->mtu = rt->rt_ifp->if_mtu;
   2570  1.1      rjs 		if (from == 1) {
   2571  1.1      rjs 			stcb->asoc.smallest_mtu = net->mtu;
   2572  1.1      rjs 		}
   2573  1.1      rjs 		/* start things off to match mtu of interface please. */
   2574  1.1      rjs 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
   2575  1.1      rjs 	} else {
   2576  1.1      rjs 		net->mtu = stcb->asoc.smallest_mtu;
   2577  1.1      rjs 	}
   2578  1.1      rjs #ifdef SCTP_DEBUG
   2579  1.1      rjs 	printf("After lookup\n");
   2580  1.1      rjs #endif
   2581  1.1      rjs 	if (stcb->asoc.smallest_mtu > net->mtu) {
   2582  1.1      rjs 		stcb->asoc.smallest_mtu = net->mtu;
   2583  1.1      rjs 	}
   2584  1.1      rjs 	/* We take the max of the burst limit times a MTU or the INITIAL_CWND.
   2585  1.1      rjs 	 * We then limit this to 4 MTU's of sending.
   2586  1.1      rjs 	 */
   2587  1.1      rjs  	net->cwnd = min((net->mtu * 4), max((stcb->asoc.max_burst * net->mtu), SCTP_INITIAL_CWND));
   2588  1.1      rjs 
   2589  1.1      rjs 	/* we always get at LEAST 2 MTU's */
   2590  1.1      rjs 	if (net->cwnd < (2 * net->mtu)) {
   2591  1.1      rjs 		net->cwnd = 2 * net->mtu;
   2592  1.1      rjs 	}
   2593  1.1      rjs 
   2594  1.1      rjs 	net->ssthresh = stcb->asoc.peers_rwnd;
   2595  1.1      rjs 
   2596  1.1      rjs 	net->src_addr_selected = 0;
   2597  1.1      rjs 	netfirst = TAILQ_FIRST(&stcb->asoc.nets);
   2598  1.1      rjs 	if (rt == NULL) {
   2599  1.1      rjs 		/* Since we have no route put it at the back */
   2600  1.1      rjs 		TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
   2601  1.1      rjs 	} else if (netfirst == NULL) {
   2602  1.1      rjs 		/* We are the first one in the pool. */
   2603  1.1      rjs 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
   2604  1.1      rjs 	} else if ((netfirst_rt = rtcache_validate(&netfirst->ro)) == NULL) {
   2605  1.1      rjs 		/*
   2606  1.1      rjs 		 * First one has NO route. Place this one ahead of the
   2607  1.1      rjs 		 * first one.
   2608  1.1      rjs 		 */
   2609  1.1      rjs 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
   2610  1.1      rjs 	} else if (rt->rt_ifp != netfirst_rt->rt_ifp) {
   2611  1.1      rjs 		/*
   2612  1.1      rjs 		 * This one has a different interface than the one at the
   2613  1.1      rjs 		 * top of the list. Place it ahead.
   2614  1.1      rjs 		 */
   2615  1.1      rjs 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
   2616  1.1      rjs 	} else {
   2617  1.1      rjs 		/*
   2618  1.1      rjs 		 * Ok we have the same interface as the first one. Move
   2619  1.1      rjs 		 * forward until we find either
   2620  1.1      rjs 		 *   a) one with a NULL route... insert ahead of that
   2621  1.1      rjs 		 *   b) one with a different ifp.. insert after that.
   2622  1.1      rjs 		 *   c) end of the list.. insert at the tail.
   2623  1.1      rjs 		 */
   2624  1.1      rjs 		struct sctp_nets *netlook;
   2625  1.1      rjs 		struct rtentry *netlook_rt;
   2626  1.1      rjs 		do {
   2627  1.1      rjs 			netlook = TAILQ_NEXT(netfirst, sctp_next);
   2628  1.1      rjs 			if (netlook == NULL) {
   2629  1.1      rjs 				/* End of the list */
   2630  1.1      rjs 				TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
   2631  1.1      rjs 				    sctp_next);
   2632  1.1      rjs 				break;
   2633  1.1      rjs 			} else if ((netlook_rt = rtcache_validate(&netlook->ro)) == NULL) {
   2634  1.1      rjs 				/* next one has NO route */
   2635  1.1      rjs 				TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
   2636  1.1      rjs 				break;
   2637  1.1      rjs 			} else if (netlook_rt->rt_ifp != rt->rt_ifp) {
   2638  1.1      rjs 				TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
   2639  1.1      rjs 				    net, sctp_next);
   2640  1.1      rjs 				break;
   2641  1.1      rjs 			}
   2642  1.1      rjs 			/* Shift forward */
   2643  1.1      rjs 			netfirst = netlook;
   2644  1.1      rjs 		} while (netlook != NULL);
   2645  1.1      rjs 	}
   2646  1.1      rjs 	/* got to have a primary set */
   2647  1.1      rjs 	if (stcb->asoc.primary_destination == 0) {
   2648  1.1      rjs 		stcb->asoc.primary_destination = net;
   2649  1.1      rjs 	} else if (!rtcache_validate(&stcb->asoc.primary_destination->ro)) {
   2650  1.1      rjs 		/* No route to current primary adopt new primary */
   2651  1.1      rjs 		stcb->asoc.primary_destination = net;
   2652  1.1      rjs 	}
   2653  1.1      rjs 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
   2654  1.1      rjs 	    net);
   2655  1.1      rjs 
   2656  1.1      rjs 	return (0);
   2657  1.1      rjs }
   2658  1.1      rjs 
   2659  1.1      rjs 
   2660  1.1      rjs /*
   2661  1.1      rjs  * allocate an association and add it to the endpoint. The caller must
   2662  1.1      rjs  * be careful to add all additional addresses once they are know right
   2663  1.1      rjs  * away or else the assoc will be may experience a blackout scenario.
   2664  1.1      rjs  */
   2665  1.1      rjs struct sctp_tcb *
   2666  1.1      rjs sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
   2667  1.1      rjs     int for_a_init, int *error,  uint32_t override_tag)
   2668  1.1      rjs {
   2669  1.1      rjs 	struct sctp_tcb *stcb;
   2670  1.1      rjs 	struct sctp_association *asoc;
   2671  1.1      rjs 	struct sctpasochead *head;
   2672  1.1      rjs 	uint16_t rport;
   2673  1.1      rjs 	int err;
   2674  1.1      rjs 
   2675  1.1      rjs 	/*
   2676  1.1      rjs 	 * Assumption made here:
   2677  1.1      rjs 	 *  Caller has done a sctp_findassociation_ep_addr(ep, addr's);
   2678  1.1      rjs 	 *  to make sure the address does not exist already.
   2679  1.1      rjs 	 */
   2680  1.1      rjs 	if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
   2681  1.1      rjs 		/* Hit max assoc, sorry no more */
   2682  1.1      rjs 		*error = ENOBUFS;
   2683  1.1      rjs 		return (NULL);
   2684  1.1      rjs 	}
   2685  1.1      rjs 	SCTP_INP_RLOCK(inp);
   2686  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
   2687  1.1      rjs 		/*
   2688  1.1      rjs 		 * If its in the TCP pool, its NOT allowed to create an
   2689  1.1      rjs 		 * association. The parent listener needs to call
   2690  1.1      rjs 		 * sctp_aloc_assoc.. or the one-2-many socket. If a
   2691  1.1      rjs 		 * peeled off, or connected one does this.. its an error.
   2692  1.1      rjs 		 */
   2693  1.1      rjs 		SCTP_INP_RUNLOCK(inp);
   2694  1.1      rjs 		*error = EINVAL;
   2695  1.1      rjs 		return (NULL);
   2696  1.1      rjs  	}
   2697  1.1      rjs 
   2698  1.1      rjs #ifdef SCTP_DEBUG
   2699  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2700  1.1      rjs 		printf("Allocate an association for peer:");
   2701  1.1      rjs 		if (firstaddr)
   2702  1.1      rjs 			sctp_print_address(firstaddr);
   2703  1.1      rjs 		else
   2704  1.1      rjs 			printf("None\n");
   2705  1.1      rjs 		printf("Port:%d\n",
   2706  1.1      rjs 		       ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
   2707  1.1      rjs 	}
   2708  1.1      rjs #endif /* SCTP_DEBUG */
   2709  1.1      rjs 	if (firstaddr->sa_family == AF_INET) {
   2710  1.1      rjs 		struct sockaddr_in *sin;
   2711  1.1      rjs 		sin = (struct sockaddr_in *)firstaddr;
   2712  1.1      rjs 		if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
   2713  1.1      rjs 			/* Invalid address */
   2714  1.1      rjs #ifdef SCTP_DEBUG
   2715  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2716  1.1      rjs 				printf("peer address invalid\n");
   2717  1.1      rjs 			}
   2718  1.1      rjs #endif
   2719  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
   2720  1.1      rjs 			*error = EINVAL;
   2721  1.1      rjs 			return (NULL);
   2722  1.1      rjs 		}
   2723  1.1      rjs 		rport = sin->sin_port;
   2724  1.1      rjs 	} else if (firstaddr->sa_family == AF_INET6) {
   2725  1.1      rjs 		struct sockaddr_in6 *sin6;
   2726  1.1      rjs 		sin6 = (struct sockaddr_in6 *)firstaddr;
   2727  1.1      rjs 		if ((sin6->sin6_port == 0) ||
   2728  1.1      rjs 		    (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
   2729  1.1      rjs 			/* Invalid address */
   2730  1.1      rjs #ifdef SCTP_DEBUG
   2731  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2732  1.1      rjs 				printf("peer address invalid\n");
   2733  1.1      rjs 			}
   2734  1.1      rjs #endif
   2735  1.1      rjs 			SCTP_INP_RUNLOCK(inp);
   2736  1.1      rjs 			*error = EINVAL;
   2737  1.1      rjs 			return (NULL);
   2738  1.1      rjs 		}
   2739  1.1      rjs 		rport = sin6->sin6_port;
   2740  1.1      rjs 	} else {
   2741  1.1      rjs 		/* not supported family type */
   2742  1.1      rjs #ifdef SCTP_DEBUG
   2743  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2744  1.1      rjs 			printf("BAD family %d\n", firstaddr->sa_family);
   2745  1.1      rjs 		}
   2746  1.1      rjs #endif
   2747  1.1      rjs 		SCTP_INP_RUNLOCK(inp);
   2748  1.1      rjs 		*error = EINVAL;
   2749  1.1      rjs 		return (NULL);
   2750  1.1      rjs 	}
   2751  1.1      rjs 	SCTP_INP_RUNLOCK(inp);
   2752  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
   2753  1.1      rjs 		/*
   2754  1.1      rjs 		 * If you have not performed a bind, then we need to do
   2755  1.1      rjs 		 * the ephemerial bind for you.
   2756  1.1      rjs 		 */
   2757  1.1      rjs #ifdef SCTP_DEBUG
   2758  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2759  1.1      rjs 			printf("Doing implicit BIND\n");
   2760  1.1      rjs 		}
   2761  1.1      rjs #endif
   2762  1.1      rjs 
   2763  1.1      rjs 		if ((err = sctp_inpcb_bind(inp->sctp_socket,
   2764  1.1      rjs 		    (struct sockaddr *)NULL, (struct lwp *)NULL))){
   2765  1.1      rjs 			/* bind error, probably perm */
   2766  1.1      rjs #ifdef SCTP_DEBUG
   2767  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2768  1.1      rjs 				printf("BIND FAILS ret:%d\n", err);
   2769  1.1      rjs 			}
   2770  1.1      rjs #endif
   2771  1.1      rjs 
   2772  1.1      rjs 			*error = err;
   2773  1.1      rjs 			return (NULL);
   2774  1.1      rjs 		}
   2775  1.1      rjs 	}
   2776  1.1      rjs 	stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
   2777  1.1      rjs 	if (stcb == NULL) {
   2778  1.1      rjs 		/* out of memory? */
   2779  1.1      rjs #ifdef SCTP_DEBUG
   2780  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2781  1.1      rjs 			printf("aloc_assoc: no assoc mem left, stcb=NULL\n");
   2782  1.1      rjs 		}
   2783  1.1      rjs #endif
   2784  1.1      rjs 		*error = ENOMEM;
   2785  1.1      rjs 		return (NULL);
   2786  1.1      rjs 	}
   2787  1.1      rjs 	sctppcbinfo.ipi_count_asoc++;
   2788  1.1      rjs 	sctppcbinfo.ipi_gencnt_asoc++;
   2789  1.1      rjs 
   2790  1.1      rjs 	memset(stcb, 0, sizeof(*stcb));
   2791  1.1      rjs 	asoc = &stcb->asoc;
   2792  1.1      rjs 	SCTP_TCB_LOCK_INIT(stcb);
   2793  1.1      rjs 	/* setup back pointers */
   2794  1.1      rjs #ifdef SCTP_DEBUG
   2795  1.1      rjs 	printf("Before back pointers\n");
   2796  1.1      rjs #endif
   2797  1.1      rjs 	stcb->sctp_ep = inp;
   2798  1.1      rjs 	stcb->sctp_socket = inp->sctp_socket;
   2799  1.1      rjs 	if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
   2800  1.1      rjs 		/* failed */
   2801  1.1      rjs 		SCTP_TCB_LOCK_DESTROY (stcb);
   2802  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
   2803  1.1      rjs 		sctppcbinfo.ipi_count_asoc--;
   2804  1.1      rjs #ifdef SCTP_DEBUG
   2805  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2806  1.1      rjs 			printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
   2807  1.1      rjs 		}
   2808  1.1      rjs #endif
   2809  1.1      rjs 		*error = err;
   2810  1.1      rjs 		return (NULL);
   2811  1.1      rjs 	}
   2812  1.1      rjs 	/* and the port */
   2813  1.1      rjs 	stcb->rport = rport;
   2814  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   2815  1.1      rjs 	SCTP_INP_WLOCK(inp);
   2816  1.1      rjs 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   2817  1.1      rjs 		/* inpcb freed while alloc going on */
   2818  1.1      rjs 		SCTP_TCB_LOCK_DESTROY (stcb);
   2819  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
   2820  1.1      rjs 		SCTP_INP_WUNLOCK(inp);
   2821  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   2822  1.1      rjs 		sctppcbinfo.ipi_count_asoc--;
   2823  1.1      rjs #ifdef SCTP_DEBUG
   2824  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2825  1.1      rjs 			printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
   2826  1.1      rjs 		}
   2827  1.1      rjs #endif
   2828  1.1      rjs 		*error = EINVAL;
   2829  1.1      rjs 		return (NULL);
   2830  1.1      rjs 	}
   2831  1.1      rjs 	SCTP_TCB_LOCK(stcb);
   2832  1.1      rjs 
   2833  1.1      rjs 	/* now that my_vtag is set, add it to the  hash */
   2834  1.1      rjs 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
   2835  1.1      rjs 	     sctppcbinfo.hashasocmark)];
   2836  1.1      rjs 	/* put it in the bucket in the vtag hash of assoc's for the system */
   2837  1.1      rjs 	LIST_INSERT_HEAD(head, stcb, sctp_asocs);
   2838  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   2839  1.1      rjs 
   2840  1.1      rjs 
   2841  1.1      rjs 	if ((err = sctp_add_remote_addr(stcb, firstaddr, 1, 1))) {
   2842  1.1      rjs 		/* failure.. memory error? */
   2843  1.1      rjs 		if (asoc->strmout)
   2844  1.1      rjs 			free(asoc->strmout, M_PCB);
   2845  1.1      rjs 		if (asoc->mapping_array)
   2846  1.1      rjs 			free(asoc->mapping_array, M_PCB);
   2847  1.1      rjs 
   2848  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
   2849  1.1      rjs 		sctppcbinfo.ipi_count_asoc--;
   2850  1.1      rjs #ifdef SCTP_DEBUG
   2851  1.1      rjs 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
   2852  1.1      rjs 			printf("aloc_assoc: couldn't add remote addr!\n");
   2853  1.1      rjs 		}
   2854  1.1      rjs #endif
   2855  1.1      rjs 		SCTP_TCB_LOCK_DESTROY (stcb);
   2856  1.1      rjs 		*error = ENOBUFS;
   2857  1.1      rjs 		return (NULL);
   2858  1.1      rjs 	}
   2859  1.1      rjs 	/* Init all the timers */
   2860  1.1      rjs 	callout_init(&asoc->hb_timer.timer, 0);
   2861  1.1      rjs 	callout_init(&asoc->dack_timer.timer, 0);
   2862  1.1      rjs 	callout_init(&asoc->asconf_timer.timer, 0);
   2863  1.1      rjs 	callout_init(&asoc->shut_guard_timer.timer, 0);
   2864  1.1      rjs 	callout_init(&asoc->autoclose_timer.timer, 0);
   2865  1.1      rjs 	callout_init(&asoc->delayed_event_timer.timer, 0);
   2866  1.1      rjs 	LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
   2867  1.1      rjs 	/* now file the port under the hash as well */
   2868  1.1      rjs #ifdef SCTP_DEBUG
   2869  1.1      rjs 	printf("Before hashing %ld size %d\n",
   2870  1.1      rjs 		inp->sctp_hashmark, sctp_pcbtblsize);
   2871  1.1      rjs #endif
   2872  1.1      rjs 	if (inp->sctp_tcbhash != NULL) {
   2873  1.1      rjs 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
   2874  1.1      rjs 		   inp->sctp_hashmark)];
   2875  1.1      rjs 		LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
   2876  1.1      rjs 	}
   2877  1.1      rjs #ifdef SCTP_DEBUG
   2878  1.1      rjs 	printf("After hashing\n");
   2879  1.1      rjs #endif
   2880  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   2881  1.1      rjs #ifdef SCTP_DEBUG
   2882  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   2883  1.1      rjs 		printf("Association %p now allocated\n", stcb);
   2884  1.1      rjs 	}
   2885  1.1      rjs #endif
   2886  1.1      rjs 	return (stcb);
   2887  1.1      rjs }
   2888  1.1      rjs 
   2889  1.1      rjs void
   2890  1.1      rjs sctp_free_remote_addr(struct sctp_nets *net)
   2891  1.1      rjs {
   2892  1.1      rjs 	if (net == NULL)
   2893  1.1      rjs 		return;
   2894  1.1      rjs 	net->ref_count--;
   2895  1.1      rjs 	if (net->ref_count <= 0) {
   2896  1.1      rjs 		/* stop timer if running */
   2897  1.1      rjs 		callout_stop(&net->rxt_timer.timer);
   2898  1.1      rjs 		callout_stop(&net->pmtu_timer.timer);
   2899  1.1      rjs 		callout_destroy(&net->rxt_timer.timer);
   2900  1.1      rjs 		callout_destroy(&net->pmtu_timer.timer);
   2901  1.1      rjs 		net->dest_state = SCTP_ADDR_NOT_REACHABLE;
   2902  1.6      rjs 		rtcache_free(&net->ro);
   2903  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
   2904  1.1      rjs 		sctppcbinfo.ipi_count_raddr--;
   2905  1.1      rjs 	}
   2906  1.1      rjs }
   2907  1.1      rjs 
   2908  1.1      rjs /*
   2909  1.1      rjs  * remove a remote endpoint address from an association, it
   2910  1.1      rjs  * will fail if the address does not exist.
   2911  1.1      rjs  */
   2912  1.1      rjs int
   2913  1.1      rjs sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
   2914  1.1      rjs {
   2915  1.1      rjs 	/*
   2916  1.1      rjs 	 * Here we need to remove a remote address. This is quite simple, we
   2917  1.1      rjs 	 * first find it in the list of address for the association
   2918  1.1      rjs 	 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE on
   2919  1.1      rjs 	 * that item.
   2920  1.1      rjs 	 * Note we do not allow it to be removed if there are no other
   2921  1.1      rjs 	 * addresses.
   2922  1.1      rjs 	 */
   2923  1.1      rjs 	struct sctp_association *asoc;
   2924  1.1      rjs 	struct sctp_nets *net, *net_tmp;
   2925  1.1      rjs 	asoc = &stcb->asoc;
   2926  1.1      rjs 	if (asoc->numnets < 2) {
   2927  1.1      rjs 		/* Must have at LEAST two remote addresses */
   2928  1.1      rjs 		return (-1);
   2929  1.1      rjs 	}
   2930  1.1      rjs 	/* locate the address */
   2931  1.1      rjs 	for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
   2932  1.1      rjs 		net_tmp = TAILQ_NEXT(net, sctp_next);
   2933  1.1      rjs 		if (rtcache_getdst(&net->ro)->sa_family != remaddr->sa_family) {
   2934  1.1      rjs 			continue;
   2935  1.1      rjs 		}
   2936  1.1      rjs 		if (sctp_cmpaddr(rtcache_getdst(&net->ro), remaddr)) {
   2937  1.1      rjs 			/* we found the guy */
   2938  1.1      rjs 			asoc->numnets--;
   2939  1.1      rjs 			TAILQ_REMOVE(&asoc->nets, net, sctp_next);
   2940  1.1      rjs 			sctp_free_remote_addr(net);
   2941  1.1      rjs 			if (net == asoc->primary_destination) {
   2942  1.1      rjs 				/* Reset primary */
   2943  1.1      rjs 				struct sctp_nets *lnet;
   2944  1.1      rjs 				lnet = TAILQ_FIRST(&asoc->nets);
   2945  1.1      rjs 				/* Try to find a confirmed primary */
   2946  1.1      rjs 				asoc->primary_destination =
   2947  1.1      rjs 				    sctp_find_alternate_net(stcb, lnet);
   2948  1.1      rjs 			}
   2949  1.1      rjs 			if (net == asoc->last_data_chunk_from) {
   2950  1.1      rjs 				/* Reset primary */
   2951  1.1      rjs 				asoc->last_data_chunk_from =
   2952  1.1      rjs 				    TAILQ_FIRST(&asoc->nets);
   2953  1.1      rjs 			}
   2954  1.1      rjs 			if (net == asoc->last_control_chunk_from) {
   2955  1.1      rjs 				/* Reset primary */
   2956  1.1      rjs 				asoc->last_control_chunk_from =
   2957  1.1      rjs 				    TAILQ_FIRST(&asoc->nets);
   2958  1.1      rjs 			}
   2959  1.1      rjs 			if (net == asoc->asconf_last_sent_to) {
   2960  1.1      rjs 				/* Reset primary */
   2961  1.1      rjs 				asoc->asconf_last_sent_to =
   2962  1.1      rjs 				    TAILQ_FIRST(&asoc->nets);
   2963  1.1      rjs 			}
   2964  1.1      rjs 			return (0);
   2965  1.1      rjs 		}
   2966  1.1      rjs 	}
   2967  1.1      rjs 	/* not found. */
   2968  1.1      rjs 	return (-2);
   2969  1.1      rjs }
   2970  1.1      rjs 
   2971  1.1      rjs 
   2972  1.1      rjs static void
   2973  1.1      rjs sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, u_int32_t tag)
   2974  1.1      rjs {
   2975  1.1      rjs 	struct sctpvtaghead *chain;
   2976  1.1      rjs 	struct sctp_tagblock *twait_block;
   2977  1.1      rjs 	struct timeval now;
   2978  1.1      rjs 	int set, i;
   2979  1.1      rjs 	SCTP_GETTIME_TIMEVAL(&now);
   2980  1.1      rjs 	chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
   2981  1.1      rjs 	set = 0;
   2982  1.1      rjs 	if (!LIST_EMPTY(chain)) {
   2983  1.1      rjs 		/* Block(s) present, lets find space, and expire on the fly */
   2984  1.1      rjs 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
   2985  1.1      rjs 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
   2986  1.1      rjs 				if ((twait_block->vtag_block[i].v_tag == 0) &&
   2987  1.1      rjs 				    !set) {
   2988  1.1      rjs 					twait_block->vtag_block[0].tv_sec_at_expire =
   2989  1.1      rjs 					    now.tv_sec + SCTP_TIME_WAIT;
   2990  1.1      rjs 					twait_block->vtag_block[0].v_tag = tag;
   2991  1.1      rjs 					set = 1;
   2992  1.1      rjs 				} else if ((twait_block->vtag_block[i].v_tag) &&
   2993  1.1      rjs 				    ((long)twait_block->vtag_block[i].tv_sec_at_expire >
   2994  1.1      rjs 				    now.tv_sec)) {
   2995  1.1      rjs 					/* Audit expires this guy */
   2996  1.1      rjs 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
   2997  1.1      rjs 					twait_block->vtag_block[i].v_tag = 0;
   2998  1.1      rjs 					if (set == 0) {
   2999  1.1      rjs 						/* Reuse it for my new tag */
   3000  1.1      rjs 						twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
   3001  1.1      rjs 						twait_block->vtag_block[0].v_tag = tag;
   3002  1.1      rjs 						set = 1;
   3003  1.1      rjs 					}
   3004  1.1      rjs 				}
   3005  1.1      rjs 			}
   3006  1.1      rjs 			if (set) {
   3007  1.1      rjs 				/*
   3008  1.1      rjs 				 * We only do up to the block where we can
   3009  1.1      rjs 				 * place our tag for audits
   3010  1.1      rjs 				 */
   3011  1.1      rjs 				break;
   3012  1.1      rjs 			}
   3013  1.1      rjs 		}
   3014  1.1      rjs 	}
   3015  1.1      rjs 	/* Need to add a new block to chain */
   3016  1.1      rjs 	if (!set) {
   3017  1.1      rjs 		twait_block = malloc(sizeof(struct sctp_tagblock), M_PCB, M_NOWAIT);
   3018  1.1      rjs 		if (twait_block == NULL) {
   3019  1.1      rjs 			return;
   3020  1.1      rjs 		}
   3021  1.1      rjs 		memset(twait_block, 0, sizeof(struct sctp_timewait));
   3022  1.1      rjs 		LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
   3023  1.1      rjs 		twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
   3024  1.1      rjs 		    SCTP_TIME_WAIT;
   3025  1.1      rjs 		twait_block->vtag_block[0].v_tag = tag;
   3026  1.1      rjs 	}
   3027  1.1      rjs }
   3028  1.1      rjs 
   3029  1.1      rjs 
   3030  1.1      rjs static void
   3031  1.1      rjs sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   3032  1.1      rjs {
   3033  1.1      rjs 	struct sctp_iterator *it;
   3034  1.1      rjs 
   3035  1.1      rjs 
   3036  1.1      rjs 
   3037  1.1      rjs 	/* Unlock the tcb lock we do this so
   3038  1.1      rjs 	 * we avoid a dead lock scenario where
   3039  1.1      rjs 	 * the iterator is waiting on the TCB lock
   3040  1.1      rjs 	 * and the TCB lock is waiting on the iterator
   3041  1.1      rjs 	 * lock.
   3042  1.1      rjs 	 */
   3043  1.1      rjs 	SCTP_ITERATOR_LOCK();
   3044  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   3045  1.1      rjs 	SCTP_INP_WLOCK(inp);
   3046  1.1      rjs 	SCTP_TCB_LOCK(stcb);
   3047  1.1      rjs 
   3048  1.1      rjs 	it = stcb->asoc.stcb_starting_point_for_iterator;
   3049  1.1      rjs 	if (it == NULL) {
   3050  1.1      rjs 		return;
   3051  1.1      rjs 	}
   3052  1.1      rjs 	if (it->inp != stcb->sctp_ep) {
   3053  1.1      rjs 		/* hm, focused on the wrong one? */
   3054  1.1      rjs 		return;
   3055  1.1      rjs 	}
   3056  1.1      rjs 	if (it->stcb != stcb) {
   3057  1.1      rjs 		return;
   3058  1.1      rjs 	}
   3059  1.1      rjs 	it->stcb = LIST_NEXT(stcb, sctp_tcblist);
   3060  1.1      rjs 	if (it->stcb == NULL) {
   3061  1.1      rjs 		/* done with all asoc's in this assoc */
   3062  1.1      rjs 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
   3063  1.1      rjs 			it->inp = NULL;
   3064  1.1      rjs 		} else {
   3065  1.1      rjs 
   3066  1.1      rjs 			it->inp = LIST_NEXT(inp, sctp_list);
   3067  1.1      rjs 		}
   3068  1.1      rjs 	}
   3069  1.1      rjs }
   3070  1.1      rjs 
   3071  1.1      rjs /*
   3072  1.1      rjs  * Free the association after un-hashing the remote port.
   3073  1.1      rjs  */
   3074  1.1      rjs void
   3075  1.1      rjs sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   3076  1.1      rjs {
   3077  1.1      rjs 	struct sctp_association *asoc;
   3078  1.1      rjs 	struct sctp_nets *net, *prev;
   3079  1.1      rjs 	struct sctp_laddr *laddr;
   3080  1.1      rjs 	struct sctp_tmit_chunk *chk;
   3081  1.1      rjs 	struct sctp_asconf_addr *aparam;
   3082  1.1      rjs 	struct sctp_socket_q_list *sq;
   3083  1.1      rjs 	int s;
   3084  1.1      rjs 
   3085  1.1      rjs 	/* first, lets purge the entry from the hash table. */
   3086  1.1      rjs 	s = splsoftnet();
   3087  1.1      rjs 	if (stcb->asoc.state == 0) {
   3088  1.1      rjs 		printf("Freeing already free association:%p - huh??\n",
   3089  1.1      rjs 		    stcb);
   3090  1.1      rjs 		splx(s);
   3091  1.1      rjs 		return;
   3092  1.1      rjs 	}
   3093  1.1      rjs 	asoc = &stcb->asoc;
   3094  1.1      rjs 	asoc->state = 0;
   3095  1.1      rjs 	/* now clean up any other timers */
   3096  1.1      rjs 	callout_stop(&asoc->hb_timer.timer);
   3097  1.1      rjs 	callout_destroy(&asoc->hb_timer.timer);
   3098  1.1      rjs 	callout_stop(&asoc->dack_timer.timer);
   3099  1.1      rjs 	callout_destroy(&asoc->dack_timer.timer);
   3100  1.1      rjs 	callout_stop(&asoc->asconf_timer.timer);
   3101  1.1      rjs 	callout_destroy(&asoc->asconf_timer.timer);
   3102  1.1      rjs 	callout_stop(&asoc->shut_guard_timer.timer);
   3103  1.1      rjs 	callout_destroy(&asoc->shut_guard_timer.timer);
   3104  1.1      rjs 	callout_stop(&asoc->autoclose_timer.timer);
   3105  1.1      rjs 	callout_destroy(&asoc->autoclose_timer.timer);
   3106  1.1      rjs 	callout_stop(&asoc->delayed_event_timer.timer);
   3107  1.1      rjs 	callout_destroy(&asoc->delayed_event_timer.timer);
   3108  1.1      rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3109  1.1      rjs 		callout_stop(&net->rxt_timer.timer);
   3110  1.1      rjs 		callout_stop(&net->pmtu_timer.timer);
   3111  1.1      rjs 		callout_destroy(&net->rxt_timer.timer);
   3112  1.1      rjs 		callout_destroy(&net->pmtu_timer.timer);
   3113  1.1      rjs 	}
   3114  1.1      rjs 
   3115  1.1      rjs 	/* Iterator asoc being freed we send an
   3116  1.1      rjs 	 * unlocked TCB. It returns with INP_INFO
   3117  1.1      rjs 	 * and INP write locked and the TCB locked
   3118  1.1      rjs 	 * too and of course the iterator lock
   3119  1.1      rjs 	 * in place as well..
   3120  1.1      rjs 	 */
   3121  1.1      rjs 	SCTP_TCB_UNLOCK(stcb);
   3122  1.1      rjs 	sctp_iterator_asoc_being_freed(inp, stcb);
   3123  1.1      rjs 
   3124  1.1      rjs 	/* Null all of my entry's on the socket q */
   3125  1.1      rjs 	TAILQ_FOREACH(sq, &inp->sctp_queue_list, next_sq) {
   3126  1.1      rjs 		if (sq->tcb == stcb) {
   3127  1.1      rjs 			sq->tcb = NULL;
   3128  1.1      rjs 		}
   3129  1.1      rjs 	}
   3130  1.1      rjs 
   3131  1.1      rjs 	if (inp->sctp_tcb_at_block == (void *)stcb) {
   3132  1.1      rjs 		inp->error_on_block = ECONNRESET;
   3133  1.1      rjs 	}
   3134  1.1      rjs 
   3135  1.1      rjs 	if (inp->sctp_tcbhash) {
   3136  1.1      rjs 		LIST_REMOVE(stcb, sctp_tcbhash);
   3137  1.1      rjs 	}
   3138  1.1      rjs 	/* Now lets remove it from the list of ALL associations in the EP */
   3139  1.1      rjs 	LIST_REMOVE(stcb, sctp_tcblist);
   3140  1.1      rjs 	SCTP_INP_WUNLOCK(inp);
   3141  1.1      rjs 	SCTP_ITERATOR_UNLOCK();
   3142  1.1      rjs 
   3143  1.1      rjs 
   3144  1.1      rjs 	/* pull from vtag hash */
   3145  1.1      rjs 	LIST_REMOVE(stcb, sctp_asocs);
   3146  1.1      rjs 
   3147  1.1      rjs 	/*
   3148  1.1      rjs 	 * Now before we can free the assoc, we must  remove all of the
   3149  1.1      rjs 	 * networks and any other allocated space.. i.e. add removes here
   3150  1.1      rjs 	 * before the SCTP_ZONE_FREE() of the tasoc entry.
   3151  1.1      rjs 	 */
   3152  1.1      rjs 
   3153  1.1      rjs 	sctp_add_vtag_to_timewait(inp, asoc->my_vtag);
   3154  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   3155  1.1      rjs 	prev = NULL;
   3156  1.1      rjs 	while (!TAILQ_EMPTY(&asoc->nets)) {
   3157  1.1      rjs 		net = TAILQ_FIRST(&asoc->nets);
   3158  1.1      rjs 		/* pull from list */
   3159  1.1      rjs 		if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
   3160  1.1      rjs 			break;
   3161  1.1      rjs 		}
   3162  1.1      rjs 		prev = net;
   3163  1.1      rjs 		TAILQ_REMOVE(&asoc->nets, net, sctp_next);
   3164  1.6      rjs 		rtcache_free(&net->ro);
   3165  1.1      rjs 		/* free it */
   3166  1.1      rjs 		net->ref_count = 0;
   3167  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
   3168  1.1      rjs 		sctppcbinfo.ipi_count_raddr--;
   3169  1.1      rjs 	}
   3170  1.1      rjs 	/*
   3171  1.1      rjs 	 * The chunk lists and such SHOULD be empty but we check them
   3172  1.1      rjs 	 * just in case.
   3173  1.1      rjs 	 */
   3174  1.1      rjs 	/* anything on the wheel needs to be removed */
   3175  1.1      rjs 	while (!TAILQ_EMPTY(&asoc->out_wheel)) {
   3176  1.1      rjs 		struct sctp_stream_out *outs;
   3177  1.1      rjs 		outs = TAILQ_FIRST(&asoc->out_wheel);
   3178  1.1      rjs 		TAILQ_REMOVE(&asoc->out_wheel, outs, next_spoke);
   3179  1.1      rjs 		/* now clean up any chunks here */
   3180  1.1      rjs 		chk = TAILQ_FIRST(&outs->outqueue);
   3181  1.1      rjs 		while (chk) {
   3182  1.1      rjs 			TAILQ_REMOVE(&outs->outqueue, chk, sctp_next);
   3183  1.1      rjs 			if (chk->data) {
   3184  1.1      rjs 				sctp_m_freem(chk->data);
   3185  1.1      rjs 				chk->data = NULL;
   3186  1.1      rjs 			}
   3187  1.1      rjs 			chk->whoTo = NULL;
   3188  1.1      rjs 			chk->asoc = NULL;
   3189  1.1      rjs 			/* Free the chunk */
   3190  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3191  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3192  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3193  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3194  1.1      rjs 				panic("Chunk count is negative");
   3195  1.1      rjs 			}
   3196  1.1      rjs 			chk = TAILQ_FIRST(&outs->outqueue);
   3197  1.1      rjs 		}
   3198  1.1      rjs 		outs = TAILQ_FIRST(&asoc->out_wheel);
   3199  1.1      rjs 	}
   3200  1.1      rjs 
   3201  1.1      rjs 	if (asoc->pending_reply) {
   3202  1.1      rjs 		free(asoc->pending_reply, M_PCB);
   3203  1.1      rjs 		asoc->pending_reply = NULL;
   3204  1.1      rjs 	}
   3205  1.1      rjs 	chk = TAILQ_FIRST(&asoc->pending_reply_queue);
   3206  1.1      rjs 	while (chk) {
   3207  1.1      rjs 		TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
   3208  1.1      rjs 		if (chk->data) {
   3209  1.1      rjs 			sctp_m_freem(chk->data);
   3210  1.1      rjs 			chk->data = NULL;
   3211  1.1      rjs 		}
   3212  1.1      rjs 		chk->whoTo = NULL;
   3213  1.1      rjs 		chk->asoc = NULL;
   3214  1.1      rjs 		/* Free the chunk */
   3215  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3216  1.1      rjs 		sctppcbinfo.ipi_count_chunk--;
   3217  1.1      rjs 		sctppcbinfo.ipi_gencnt_chunk++;
   3218  1.1      rjs 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3219  1.1      rjs 			panic("Chunk count is negative");
   3220  1.1      rjs 		}
   3221  1.1      rjs 		chk = TAILQ_FIRST(&asoc->pending_reply_queue);
   3222  1.1      rjs 	}
   3223  1.1      rjs 	/* pending send queue SHOULD be empty */
   3224  1.1      rjs 	if (!TAILQ_EMPTY(&asoc->send_queue)) {
   3225  1.1      rjs 		chk = TAILQ_FIRST(&asoc->send_queue);
   3226  1.1      rjs 		while (chk) {
   3227  1.1      rjs 			TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
   3228  1.1      rjs 			if (chk->data) {
   3229  1.1      rjs 				sctp_m_freem(chk->data);
   3230  1.1      rjs 				chk->data = NULL;
   3231  1.1      rjs 			}
   3232  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3233  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3234  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3235  1.1      rjs 				panic("Chunk count is negative");
   3236  1.1      rjs 			}
   3237  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3238  1.1      rjs 			chk = TAILQ_FIRST(&asoc->send_queue);
   3239  1.1      rjs 		}
   3240  1.1      rjs 	}
   3241  1.1      rjs 	/* sent queue SHOULD be empty */
   3242  1.1      rjs 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
   3243  1.1      rjs 		chk = TAILQ_FIRST(&asoc->sent_queue);
   3244  1.1      rjs 		while (chk) {
   3245  1.1      rjs 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
   3246  1.1      rjs 			if (chk->data) {
   3247  1.1      rjs 				sctp_m_freem(chk->data);
   3248  1.1      rjs 				chk->data = NULL;
   3249  1.1      rjs 			}
   3250  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3251  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3252  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3253  1.1      rjs 				panic("Chunk count is negative");
   3254  1.1      rjs 			}
   3255  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3256  1.1      rjs 			chk = TAILQ_FIRST(&asoc->sent_queue);
   3257  1.1      rjs 		}
   3258  1.1      rjs 	}
   3259  1.1      rjs 	/* control queue MAY not be empty */
   3260  1.1      rjs 	if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
   3261  1.1      rjs 		chk = TAILQ_FIRST(&asoc->control_send_queue);
   3262  1.1      rjs 		while (chk) {
   3263  1.1      rjs 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   3264  1.1      rjs 			if (chk->data) {
   3265  1.1      rjs 				sctp_m_freem(chk->data);
   3266  1.1      rjs 				chk->data = NULL;
   3267  1.1      rjs 			}
   3268  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3269  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3270  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3271  1.1      rjs 				panic("Chunk count is negative");
   3272  1.1      rjs 			}
   3273  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3274  1.1      rjs 			chk = TAILQ_FIRST(&asoc->control_send_queue);
   3275  1.1      rjs 		}
   3276  1.1      rjs 	}
   3277  1.1      rjs 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
   3278  1.1      rjs 		chk = TAILQ_FIRST(&asoc->reasmqueue);
   3279  1.1      rjs 		while (chk) {
   3280  1.1      rjs 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
   3281  1.1      rjs 			if (chk->data) {
   3282  1.1      rjs 				sctp_m_freem(chk->data);
   3283  1.1      rjs 				chk->data = NULL;
   3284  1.1      rjs 			}
   3285  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3286  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3287  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3288  1.1      rjs 				panic("Chunk count is negative");
   3289  1.1      rjs 			}
   3290  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3291  1.1      rjs 			chk = TAILQ_FIRST(&asoc->reasmqueue);
   3292  1.1      rjs 		}
   3293  1.1      rjs 	}
   3294  1.1      rjs 	if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
   3295  1.1      rjs 		chk = TAILQ_FIRST(&asoc->delivery_queue);
   3296  1.1      rjs 		while (chk) {
   3297  1.1      rjs 			TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
   3298  1.1      rjs 			if (chk->data) {
   3299  1.1      rjs 				sctp_m_freem(chk->data);
   3300  1.1      rjs 				chk->data = NULL;
   3301  1.1      rjs 			}
   3302  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   3303  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   3304  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3305  1.1      rjs 				panic("Chunk count is negative");
   3306  1.1      rjs 			}
   3307  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   3308  1.1      rjs 			chk = TAILQ_FIRST(&asoc->delivery_queue);
   3309  1.1      rjs 		}
   3310  1.1      rjs 	}
   3311  1.1      rjs 	if (asoc->mapping_array) {
   3312  1.1      rjs 		free(asoc->mapping_array, M_PCB);
   3313  1.1      rjs 		asoc->mapping_array = NULL;
   3314  1.1      rjs 	}
   3315  1.1      rjs 
   3316  1.1      rjs 	/* the stream outs */
   3317  1.1      rjs 	if (asoc->strmout) {
   3318  1.1      rjs 		free(asoc->strmout, M_PCB);
   3319  1.1      rjs 		asoc->strmout = NULL;
   3320  1.1      rjs 	}
   3321  1.1      rjs 	asoc->streamoutcnt = 0;
   3322  1.1      rjs 	if (asoc->strmin) {
   3323  1.1      rjs 		int i;
   3324  1.1      rjs 		for (i = 0; i < asoc->streamincnt; i++) {
   3325  1.1      rjs 			if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
   3326  1.1      rjs 				/* We have somethings on the streamin queue */
   3327  1.1      rjs 				chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
   3328  1.1      rjs 				while (chk) {
   3329  1.1      rjs 					TAILQ_REMOVE(&asoc->strmin[i].inqueue,
   3330  1.1      rjs 					    chk, sctp_next);
   3331  1.1      rjs 					if (chk->data) {
   3332  1.1      rjs 						sctp_m_freem(chk->data);
   3333  1.1      rjs 						chk->data = NULL;
   3334  1.1      rjs 					}
   3335  1.1      rjs 					SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk,
   3336  1.1      rjs 					    chk);
   3337  1.1      rjs 					sctppcbinfo.ipi_count_chunk--;
   3338  1.1      rjs 					if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3339  1.1      rjs 						panic("Chunk count is negative");
   3340  1.1      rjs 					}
   3341  1.1      rjs 					sctppcbinfo.ipi_gencnt_chunk++;
   3342  1.1      rjs 					chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
   3343  1.1      rjs 				}
   3344  1.1      rjs 			}
   3345  1.1      rjs 		}
   3346  1.1      rjs 		free(asoc->strmin, M_PCB);
   3347  1.1      rjs 		asoc->strmin = NULL;
   3348  1.1      rjs 	}
   3349  1.1      rjs 	asoc->streamincnt = 0;
   3350  1.1      rjs 	/* local addresses, if any */
   3351  1.1      rjs 	while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
   3352  1.1      rjs 		laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
   3353  1.1      rjs 		LIST_REMOVE(laddr, sctp_nxt_addr);
   3354  1.1      rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
   3355  1.1      rjs 		sctppcbinfo.ipi_count_laddr--;
   3356  1.1      rjs 	}
   3357  1.1      rjs 	/* pending asconf (address) parameters */
   3358  1.1      rjs 	while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
   3359  1.1      rjs 		aparam = TAILQ_FIRST(&asoc->asconf_queue);
   3360  1.1      rjs 		TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
   3361  1.1      rjs 		free(aparam, M_PCB);
   3362  1.1      rjs 	}
   3363  1.1      rjs 	if (asoc->last_asconf_ack_sent != NULL) {
   3364  1.1      rjs 		sctp_m_freem(asoc->last_asconf_ack_sent);
   3365  1.1      rjs 		asoc->last_asconf_ack_sent = NULL;
   3366  1.1      rjs 	}
   3367  1.1      rjs 	/* Insert new items here :> */
   3368  1.1      rjs 
   3369  1.1      rjs 	/* Get rid of LOCK */
   3370  1.1      rjs 	SCTP_TCB_LOCK_DESTROY(stcb);
   3371  1.1      rjs 
   3372  1.1      rjs 	/* now clean up the tasoc itself */
   3373  1.1      rjs 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
   3374  1.1      rjs 	sctppcbinfo.ipi_count_asoc--;
   3375  1.1      rjs 	if ((inp->sctp_socket->so_snd.sb_cc) ||
   3376  1.1      rjs 	    (inp->sctp_socket->so_snd.sb_mbcnt)) {
   3377  1.1      rjs 		/* This will happen when a abort is done */
   3378  1.1      rjs 		inp->sctp_socket->so_snd.sb_cc = 0;
   3379  1.1      rjs 		inp->sctp_socket->so_snd.sb_mbcnt = 0;
   3380  1.1      rjs 	}
   3381  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   3382  1.1      rjs 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
   3383  1.1      rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   3384  1.1      rjs 				/*
   3385  1.1      rjs 				 * For the base fd, that is NOT in TCP pool we
   3386  1.1      rjs 				 * turn off the connected flag. This allows
   3387  1.1      rjs 				 * non-listening endpoints to connect/shutdown/
   3388  1.1      rjs 				 * connect.
   3389  1.1      rjs 				 */
   3390  1.1      rjs 				inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
   3391  1.1      rjs 				soisdisconnected(inp->sctp_socket);
   3392  1.1      rjs 			}
   3393  1.1      rjs 			/*
   3394  1.1      rjs 			 * For those that are in the TCP pool we just leave
   3395  1.1      rjs 			 * so it cannot be used. When they close the fd we
   3396  1.1      rjs 			 * will free it all.
   3397  1.1      rjs 			 */
   3398  1.1      rjs 		}
   3399  1.1      rjs 	}
   3400  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
   3401  1.1      rjs 		sctp_inpcb_free(inp, 0);
   3402  1.1      rjs 	}
   3403  1.1      rjs 	splx(s);
   3404  1.1      rjs }
   3405  1.1      rjs 
   3406  1.1      rjs 
   3407  1.1      rjs /*
   3408  1.1      rjs  * determine if a destination is "reachable" based upon the addresses
   3409  1.1      rjs  * bound to the current endpoint (e.g. only v4 or v6 currently bound)
   3410  1.1      rjs  */
   3411  1.1      rjs /*
   3412  1.1      rjs  * FIX: if we allow assoc-level bindx(), then this needs to be fixed
   3413  1.1      rjs  * to use assoc level v4/v6 flags, as the assoc *may* not have the
   3414  1.1      rjs  * same address types bound as its endpoint
   3415  1.1      rjs  */
   3416  1.1      rjs int
   3417  1.1      rjs sctp_destination_is_reachable(struct sctp_tcb *stcb, const struct sockaddr *destaddr)
   3418  1.1      rjs {
   3419  1.1      rjs 	struct sctp_inpcb *inp;
   3420  1.1      rjs 	int answer;
   3421  1.1      rjs 
   3422  1.1      rjs 	/* No locks here, the TCB, in all cases is already
   3423  1.1      rjs 	 * locked and an assoc is up. There is either a
   3424  1.1      rjs 	 * INP lock by the caller applied (in asconf case when
   3425  1.1      rjs 	 * deleting an address) or NOT in the HB case, however
   3426  1.1      rjs 	 * if HB then the INP increment is up and the INP
   3427  1.1      rjs 	 * will not be removed (on top of the fact that
   3428  1.1      rjs 	 * we have a TCB lock). So we only want to
   3429  1.1      rjs 	 * read the sctp_flags, which is either bound-all
   3430  1.1      rjs 	 * or not.. no protection needed since once an
   3431  1.1      rjs 	 * assoc is up you can't be changing your binding.
   3432  1.1      rjs 	 */
   3433  1.1      rjs 	inp = stcb->sctp_ep;
   3434  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3435  1.1      rjs 		/* if bound all, destination is not restricted */
   3436  1.1      rjs 		/* RRS: Question during lock work: Is this
   3437  1.1      rjs 		 * correct? If you are bound-all you still
   3438  1.1      rjs 		 * might need to obey the V4--V6 flags???
   3439  1.1      rjs 		 * IMO this bound-all stuff needs to be removed!
   3440  1.1      rjs 		 */
   3441  1.1      rjs 		return (1);
   3442  1.1      rjs 	}
   3443  1.1      rjs 	/* NOTE: all "scope" checks are done when local addresses are added */
   3444  1.1      rjs 	if (destaddr->sa_family == AF_INET6) {
   3445  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3446  1.1      rjs 		answer = inp->inp_vflag & INP_IPV6;
   3447  1.1      rjs #else
   3448  1.1      rjs 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
   3449  1.1      rjs #endif
   3450  1.1      rjs 	} else if (destaddr->sa_family == AF_INET) {
   3451  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3452  1.1      rjs 		answer = inp->inp_vflag & INP_IPV4;
   3453  1.1      rjs #else
   3454  1.1      rjs 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
   3455  1.1      rjs #endif
   3456  1.1      rjs 	} else {
   3457  1.1      rjs 		/* invalid family, so it's unreachable */
   3458  1.1      rjs 		answer = 0;
   3459  1.1      rjs 	}
   3460  1.1      rjs 	return (answer);
   3461  1.1      rjs }
   3462  1.1      rjs 
   3463  1.1      rjs /*
   3464  1.1      rjs  * update the inp_vflags on an endpoint
   3465  1.1      rjs  */
   3466  1.1      rjs static void
   3467  1.1      rjs sctp_update_ep_vflag(struct sctp_inpcb *inp) {
   3468  1.1      rjs 	struct sctp_laddr *laddr;
   3469  1.1      rjs 
   3470  1.1      rjs 	/* first clear the flag */
   3471  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3472  1.1      rjs 	inp->inp_vflag = 0;
   3473  1.1      rjs #else
   3474  1.1      rjs 	inp->ip_inp.inp.inp_vflag = 0;
   3475  1.1      rjs #endif
   3476  1.1      rjs 	/* set the flag based on addresses on the ep list */
   3477  1.1      rjs 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3478  1.1      rjs 		if (laddr->ifa == NULL) {
   3479  1.1      rjs #ifdef SCTP_DEBUG
   3480  1.1      rjs 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   3481  1.1      rjs 				printf("An ounce of prevention is worth a pound of cure\n");
   3482  1.1      rjs 			}
   3483  1.1      rjs #endif /* SCTP_DEBUG */
   3484  1.1      rjs 			continue;
   3485  1.1      rjs 		}
   3486  1.1      rjs 		if (laddr->ifa->ifa_addr) {
   3487  1.1      rjs 			continue;
   3488  1.1      rjs 		}
   3489  1.1      rjs 		if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
   3490  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3491  1.1      rjs 			inp->inp_vflag |= INP_IPV6;
   3492  1.1      rjs #else
   3493  1.1      rjs 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
   3494  1.1      rjs #endif
   3495  1.1      rjs 		} else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
   3496  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3497  1.1      rjs 			inp->inp_vflag |= INP_IPV4;
   3498  1.1      rjs #else
   3499  1.1      rjs 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
   3500  1.1      rjs #endif
   3501  1.1      rjs 		}
   3502  1.1      rjs 	}
   3503  1.1      rjs }
   3504  1.1      rjs 
   3505  1.1      rjs /*
   3506  1.1      rjs  * Add the address to the endpoint local address list
   3507  1.1      rjs  * There is nothing to be done if we are bound to all addresses
   3508  1.1      rjs  */
   3509  1.1      rjs int
   3510  1.1      rjs sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
   3511  1.1      rjs {
   3512  1.1      rjs 	struct sctp_laddr *laddr;
   3513  1.1      rjs 	int fnd, error;
   3514  1.1      rjs 	fnd = 0;
   3515  1.1      rjs 
   3516  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3517  1.1      rjs 		/* You are already bound to all. You have it already */
   3518  1.1      rjs 		return (0);
   3519  1.1      rjs 	}
   3520  1.1      rjs 	if (ifa->ifa_addr->sa_family == AF_INET6) {
   3521  1.1      rjs 		struct in6_ifaddr *ifa6;
   3522  1.1      rjs 		ifa6 = (struct in6_ifaddr *)ifa;
   3523  1.1      rjs 		if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
   3524  1.1      rjs 		    IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
   3525  1.1      rjs 			/* Can't bind a non-existent addr. */
   3526  1.1      rjs 			return (-1);
   3527  1.1      rjs 	}
   3528  1.1      rjs 	/* first, is it already present? */
   3529  1.1      rjs 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3530  1.1      rjs 		if (laddr->ifa == ifa) {
   3531  1.1      rjs 			fnd = 1;
   3532  1.1      rjs 			break;
   3533  1.1      rjs 		}
   3534  1.1      rjs 	}
   3535  1.1      rjs 
   3536  1.1      rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
   3537  1.1      rjs 		/* Not bound to all */
   3538  1.1      rjs 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
   3539  1.1      rjs 		if (error != 0)
   3540  1.1      rjs 			return (error);
   3541  1.1      rjs 		inp->laddr_count++;
   3542  1.1      rjs 		/* update inp_vflag flags */
   3543  1.1      rjs 		if (ifa->ifa_addr->sa_family == AF_INET6) {
   3544  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3545  1.1      rjs 			inp->inp_vflag |= INP_IPV6;
   3546  1.1      rjs #else
   3547  1.1      rjs 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
   3548  1.1      rjs #endif
   3549  1.1      rjs 		} else if (ifa->ifa_addr->sa_family == AF_INET) {
   3550  1.1      rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
   3551  1.1      rjs 			inp->inp_vflag |= INP_IPV4;
   3552  1.1      rjs #else
   3553  1.1      rjs 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
   3554  1.1      rjs #endif
   3555  1.1      rjs 		}
   3556  1.1      rjs 	}
   3557  1.1      rjs 	return (0);
   3558  1.1      rjs }
   3559  1.1      rjs 
   3560  1.1      rjs 
   3561  1.1      rjs /*
   3562  1.1      rjs  * select a new (hopefully reachable) destination net
   3563  1.1      rjs  * (should only be used when we deleted an ep addr that is the
   3564  1.1      rjs  * only usable source address to reach the destination net)
   3565  1.1      rjs  */
   3566  1.1      rjs static void
   3567  1.1      rjs sctp_select_primary_destination(struct sctp_tcb *stcb)
   3568  1.1      rjs {
   3569  1.1      rjs 	struct sctp_nets *net;
   3570  1.1      rjs 
   3571  1.1      rjs 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   3572  1.1      rjs 		/* for now, we'll just pick the first reachable one we find */
   3573  1.1      rjs 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
   3574  1.1      rjs 			continue;
   3575  1.1      rjs 		if (sctp_destination_is_reachable(stcb,
   3576  1.1      rjs 			rtcache_getdst(&net->ro))) {
   3577  1.1      rjs 			/* found a reachable destination */
   3578  1.1      rjs 			stcb->asoc.primary_destination = net;
   3579  1.1      rjs 		}
   3580  1.1      rjs 	}
   3581  1.1      rjs 	/* I can't there from here! ...we're gonna die shortly... */
   3582  1.1      rjs }
   3583  1.1      rjs 
   3584  1.1      rjs 
   3585  1.1      rjs /*
   3586  1.1      rjs  * Delete the address from the endpoint local address list
   3587  1.1      rjs  * There is nothing to be done if we are bound to all addresses
   3588  1.1      rjs  */
   3589  1.1      rjs int
   3590  1.1      rjs sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
   3591  1.1      rjs {
   3592  1.1      rjs 	struct sctp_laddr *laddr;
   3593  1.1      rjs 	int fnd;
   3594  1.1      rjs 	fnd = 0;
   3595  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3596  1.1      rjs 		/* You are already bound to all. You have it already */
   3597  1.1      rjs 		return (EINVAL);
   3598  1.1      rjs 	}
   3599  1.1      rjs 
   3600  1.1      rjs 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3601  1.1      rjs 		if (laddr->ifa == ifa) {
   3602  1.1      rjs 			fnd = 1;
   3603  1.1      rjs 			break;
   3604  1.1      rjs 		}
   3605  1.1      rjs 	}
   3606  1.1      rjs 	if (fnd && (inp->laddr_count < 2)) {
   3607  1.1      rjs 		/* can't delete unless there are at LEAST 2 addresses */
   3608  1.1      rjs 		return (-1);
   3609  1.1      rjs 	}
   3610  1.1      rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
   3611  1.1      rjs 		/*
   3612  1.1      rjs 		 * clean up any use of this address
   3613  1.1      rjs 		 * go through our associations and clear any
   3614  1.1      rjs 		 *  last_used_address that match this one
   3615  1.1      rjs 		 * for each assoc, see if a new primary_destination is needed
   3616  1.1      rjs 		 */
   3617  1.1      rjs 		struct sctp_tcb *stcb;
   3618  1.1      rjs 
   3619  1.1      rjs 		/* clean up "next_addr_touse" */
   3620  1.1      rjs 		if (inp->next_addr_touse == laddr)
   3621  1.1      rjs 			/* delete this address */
   3622  1.1      rjs 			inp->next_addr_touse = NULL;
   3623  1.1      rjs 
   3624  1.1      rjs 		/* clean up "last_used_address" */
   3625  1.1      rjs 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
   3626  1.1      rjs 			if (stcb->asoc.last_used_address == laddr)
   3627  1.1      rjs 				/* delete this address */
   3628  1.1      rjs 				stcb->asoc.last_used_address = NULL;
   3629  1.1      rjs 		} /* for each tcb */
   3630  1.1      rjs 
   3631  1.1      rjs 		/* remove it from the ep list */
   3632  1.1      rjs 		sctp_remove_laddr(laddr);
   3633  1.1      rjs 		inp->laddr_count--;
   3634  1.1      rjs 		/* update inp_vflag flags */
   3635  1.1      rjs 		sctp_update_ep_vflag(inp);
   3636  1.1      rjs 		/* select a new primary destination if needed */
   3637  1.1      rjs 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
   3638  1.1      rjs 			/* presume caller (sctp_asconf.c) already owns INP lock */
   3639  1.1      rjs 			SCTP_TCB_LOCK(stcb);
   3640  1.1      rjs 			if (sctp_destination_is_reachable(stcb,
   3641  1.1      rjs 			    rtcache_getdst(&stcb->asoc.primary_destination->ro)) == 0) {
   3642  1.1      rjs 				sctp_select_primary_destination(stcb);
   3643  1.1      rjs 			}
   3644  1.1      rjs 			SCTP_TCB_UNLOCK(stcb);
   3645  1.1      rjs 		} /* for each tcb */
   3646  1.1      rjs 	}
   3647  1.1      rjs 	return (0);
   3648  1.1      rjs }
   3649  1.1      rjs 
   3650  1.1      rjs /*
   3651  1.1      rjs  * Add the addr to the TCB local address list
   3652  1.1      rjs  * For the BOUNDALL or dynamic case, this is a "pending" address list
   3653  1.1      rjs  * (eg. addresses waiting for an ASCONF-ACK response)
   3654  1.1      rjs  * For the subset binding, static case, this is a "valid" address list
   3655  1.1      rjs  */
   3656  1.1      rjs int
   3657  1.1      rjs sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
   3658  1.1      rjs {
   3659  1.1      rjs 	struct sctp_laddr *laddr;
   3660  1.1      rjs 	int error;
   3661  1.1      rjs 
   3662  1.1      rjs 	/* Assumes TCP is locked.. and possiblye
   3663  1.1      rjs 	 * the INP. May need to confirm/fix that if
   3664  1.1      rjs 	 * we need it and is not the case.
   3665  1.1      rjs 	 */
   3666  1.1      rjs 	if (ifa->ifa_addr->sa_family == AF_INET6) {
   3667  1.1      rjs 		struct in6_ifaddr *ifa6;
   3668  1.1      rjs 		ifa6 = (struct in6_ifaddr *)ifa;
   3669  1.1      rjs 		if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
   3670  1.1      rjs 		    /* IN6_IFF_DEPRECATED | */
   3671  1.1      rjs 		    IN6_IFF_ANYCAST |
   3672  1.1      rjs 		    IN6_IFF_NOTREADY))
   3673  1.1      rjs 			/* Can't bind a non-existent addr. */
   3674  1.1      rjs 			return (-1);
   3675  1.1      rjs 	}
   3676  1.1      rjs 	/* does the address already exist? */
   3677  1.1      rjs 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
   3678  1.1      rjs 		if (laddr->ifa == ifa) {
   3679  1.1      rjs 			return (-1);
   3680  1.1      rjs 		}
   3681  1.1      rjs 	}
   3682  1.1      rjs 
   3683  1.1      rjs 	/* add to the list */
   3684  1.1      rjs 	error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
   3685  1.1      rjs 	if (error != 0)
   3686  1.1      rjs 		return (error);
   3687  1.1      rjs 	return (0);
   3688  1.1      rjs }
   3689  1.1      rjs 
   3690  1.1      rjs /*
   3691  1.1      rjs  * insert an laddr entry with the given ifa for the desired list
   3692  1.1      rjs  */
   3693  1.1      rjs int
   3694  1.1      rjs sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa) {
   3695  1.1      rjs 	struct sctp_laddr *laddr;
   3696  1.1      rjs 	int s;
   3697  1.1      rjs 
   3698  1.1      rjs 	s = splsoftnet();
   3699  1.1      rjs 
   3700  1.1      rjs 	laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
   3701  1.1      rjs 	if (laddr == NULL) {
   3702  1.1      rjs 		/* out of memory? */
   3703  1.1      rjs 		splx(s);
   3704  1.1      rjs 		return (EINVAL);
   3705  1.1      rjs 	}
   3706  1.1      rjs 	sctppcbinfo.ipi_count_laddr++;
   3707  1.1      rjs 	sctppcbinfo.ipi_gencnt_laddr++;
   3708  1.1      rjs 	memset(laddr, 0, sizeof(*laddr));
   3709  1.1      rjs 	laddr->ifa = ifa;
   3710  1.1      rjs 	/* insert it */
   3711  1.1      rjs 	LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
   3712  1.1      rjs 
   3713  1.1      rjs 	splx(s);
   3714  1.1      rjs 	return (0);
   3715  1.1      rjs }
   3716  1.1      rjs 
   3717  1.1      rjs /*
   3718  1.1      rjs  * Remove an laddr entry from the local address list (on an assoc)
   3719  1.1      rjs  */
   3720  1.1      rjs void
   3721  1.1      rjs sctp_remove_laddr(struct sctp_laddr *laddr)
   3722  1.1      rjs {
   3723  1.1      rjs 	int s;
   3724  1.1      rjs 	s = splsoftnet();
   3725  1.1      rjs 	/* remove from the list */
   3726  1.1      rjs 	LIST_REMOVE(laddr, sctp_nxt_addr);
   3727  1.1      rjs 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
   3728  1.1      rjs 	sctppcbinfo.ipi_count_laddr--;
   3729  1.1      rjs 	sctppcbinfo.ipi_gencnt_laddr++;
   3730  1.1      rjs 
   3731  1.1      rjs 	splx(s);
   3732  1.1      rjs }
   3733  1.1      rjs 
   3734  1.1      rjs /*
   3735  1.1      rjs  * Remove an address from the TCB local address list
   3736  1.1      rjs  */
   3737  1.1      rjs int
   3738  1.1      rjs sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
   3739  1.1      rjs {
   3740  1.1      rjs 	struct sctp_inpcb *inp;
   3741  1.1      rjs 	struct sctp_laddr *laddr;
   3742  1.1      rjs 
   3743  1.1      rjs 	/* This is called by asconf work. It is assumed that
   3744  1.1      rjs 	 * a) The TCB is locked
   3745  1.1      rjs 	 * and
   3746  1.1      rjs 	 * b) The INP is locked.
   3747  1.1      rjs 	 * This is true in as much as I can trace through
   3748  1.1      rjs 	 * the entry asconf code where I did these locks.
   3749  1.1      rjs 	 * Again, the ASCONF code is a bit different in
   3750  1.1      rjs 	 * that it does lock the INP during its work often
   3751  1.1      rjs 	 * times. This must be since we don't want other
   3752  1.1      rjs 	 * proc's looking up things while what they are
   3753  1.1      rjs 	 * looking up is changing :-D
   3754  1.1      rjs 	 */
   3755  1.1      rjs 
   3756  1.1      rjs 	inp = stcb->sctp_ep;
   3757  1.1      rjs 	/* if subset bound and don't allow ASCONF's, can't delete last */
   3758  1.1      rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
   3759  1.1      rjs 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
   3760  1.1      rjs 		if (stcb->asoc.numnets < 2) {
   3761  1.1      rjs 			/* can't delete last address */
   3762  1.1      rjs 			return (-1);
   3763  1.1      rjs 		}
   3764  1.1      rjs 	}
   3765  1.1      rjs 
   3766  1.1      rjs 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
   3767  1.1      rjs 		/* remove the address if it exists */
   3768  1.1      rjs 		if (laddr->ifa == NULL)
   3769  1.1      rjs 			continue;
   3770  1.1      rjs 		if (laddr->ifa == ifa) {
   3771  1.1      rjs 			sctp_remove_laddr(laddr);
   3772  1.1      rjs 			return (0);
   3773  1.1      rjs 		}
   3774  1.1      rjs 	}
   3775  1.1      rjs 
   3776  1.1      rjs 	/* address not found! */
   3777  1.1      rjs 	return (-1);
   3778  1.1      rjs }
   3779  1.1      rjs 
   3780  1.1      rjs /*
   3781  1.1      rjs  * Remove an address from the TCB local address list
   3782  1.1      rjs  * lookup using a sockaddr addr
   3783  1.1      rjs  */
   3784  1.1      rjs int
   3785  1.1      rjs sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
   3786  1.1      rjs {
   3787  1.1      rjs 	struct sctp_inpcb *inp;
   3788  1.1      rjs 	struct sctp_laddr *laddr;
   3789  1.1      rjs 	struct sockaddr *l_sa;
   3790  1.1      rjs 
   3791  1.1      rjs         /*
   3792  1.1      rjs          * This function I find does not seem to have a caller.
   3793  1.1      rjs 	 * As such we NEED TO DELETE this code. If we do
   3794  1.1      rjs 	 * find a caller, the caller MUST have locked the TCB
   3795  1.1      rjs 	 * at the least and probably the INP as well.
   3796  1.1      rjs          */
   3797  1.1      rjs 	inp = stcb->sctp_ep;
   3798  1.1      rjs 	/* if subset bound and don't allow ASCONF's, can't delete last */
   3799  1.1      rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
   3800  1.1      rjs 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
   3801  1.1      rjs 		if (stcb->asoc.numnets < 2) {
   3802  1.1      rjs 			/* can't delete last address */
   3803  1.1      rjs 			return (-1);
   3804  1.1      rjs 		}
   3805  1.1      rjs 	}
   3806  1.1      rjs 
   3807  1.1      rjs 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
   3808  1.1      rjs 		/* make sure the address exists */
   3809  1.1      rjs 		if (laddr->ifa == NULL)
   3810  1.1      rjs 			continue;
   3811  1.1      rjs 		if (laddr->ifa->ifa_addr == NULL)
   3812  1.1      rjs 			continue;
   3813  1.1      rjs 
   3814  1.1      rjs 		l_sa = laddr->ifa->ifa_addr;
   3815  1.1      rjs 		if (l_sa->sa_family == AF_INET6) {
   3816  1.1      rjs 			/* IPv6 address */
   3817  1.1      rjs 			struct sockaddr_in6 *sin1, *sin2;
   3818  1.1      rjs 			sin1 = (struct sockaddr_in6 *)l_sa;
   3819  1.1      rjs 			sin2 = (struct sockaddr_in6 *)sa;
   3820  1.1      rjs 			if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
   3821  1.1      rjs 			    sizeof(struct in6_addr)) == 0) {
   3822  1.1      rjs 				/* matched */
   3823  1.1      rjs 				sctp_remove_laddr(laddr);
   3824  1.1      rjs 				return (0);
   3825  1.1      rjs 			}
   3826  1.1      rjs 		} else if (l_sa->sa_family == AF_INET) {
   3827  1.1      rjs 			/* IPv4 address */
   3828  1.1      rjs 			struct sockaddr_in *sin1, *sin2;
   3829  1.1      rjs 			sin1 = (struct sockaddr_in *)l_sa;
   3830  1.1      rjs 			sin2 = (struct sockaddr_in *)sa;
   3831  1.1      rjs 			if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
   3832  1.1      rjs 				/* matched */
   3833  1.1      rjs 				sctp_remove_laddr(laddr);
   3834  1.1      rjs 				return (0);
   3835  1.1      rjs 			}
   3836  1.1      rjs 		} else {
   3837  1.1      rjs 			/* invalid family */
   3838  1.1      rjs 			return (-1);
   3839  1.1      rjs 		}
   3840  1.1      rjs 	} /* end foreach */
   3841  1.1      rjs 	/* address not found! */
   3842  1.1      rjs 	return (-1);
   3843  1.1      rjs }
   3844  1.1      rjs 
   3845  1.1      rjs static char sctp_pcb_initialized = 0;
   3846  1.1      rjs 
   3847  1.1      rjs #if defined(__FreeBSD__) || defined(__APPLE__)
   3848  1.1      rjs /* sysctl */
   3849  1.1      rjs static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
   3850  1.1      rjs static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
   3851  1.1      rjs 
   3852  1.1      rjs #endif /* FreeBSD || APPLE */
   3853  1.1      rjs 
   3854  1.1      rjs #ifndef SCTP_TCBHASHSIZE
   3855  1.1      rjs #define SCTP_TCBHASHSIZE 1024
   3856  1.1      rjs #endif
   3857  1.1      rjs 
   3858  1.1      rjs #ifndef SCTP_CHUNKQUEUE_SCALE
   3859  1.1      rjs #define SCTP_CHUNKQUEUE_SCALE 10
   3860  1.1      rjs #endif
   3861  1.1      rjs 
   3862  1.1      rjs void
   3863  1.1      rjs sctp_pcb_init(void)
   3864  1.1      rjs {
   3865  1.1      rjs 	/*
   3866  1.1      rjs 	 * SCTP initialization for the PCB structures
   3867  1.1      rjs 	 * should be called by the sctp_init() funciton.
   3868  1.1      rjs 	 */
   3869  1.1      rjs 	int i;
   3870  1.1      rjs 	int hashtblsize = SCTP_TCBHASHSIZE;
   3871  1.1      rjs 
   3872  1.1      rjs #if defined(__FreeBSD__) || defined(__APPLE__)
   3873  1.1      rjs 	int sctp_chunkscale = SCTP_CHUNKQUEUE_SCALE;
   3874  1.1      rjs #endif
   3875  1.1      rjs 
   3876  1.1      rjs 	if (sctp_pcb_initialized != 0) {
   3877  1.1      rjs 		/* error I was called twice */
   3878  1.1      rjs 		return;
   3879  1.1      rjs 	}
   3880  1.1      rjs 	sctp_pcb_initialized = 1;
   3881  1.1      rjs 
   3882  1.1      rjs 	/* Init all peg counts */
   3883  1.1      rjs 	for (i = 0; i < SCTP_NUMBER_OF_PEGS; i++) {
   3884  1.1      rjs 		sctp_pegs[i] = 0;
   3885  1.1      rjs 	}
   3886  1.1      rjs 
   3887  1.1      rjs 	/* init the empty list of (All) Endpoints */
   3888  1.1      rjs 	LIST_INIT(&sctppcbinfo.listhead);
   3889  1.1      rjs 
   3890  1.1      rjs 	/* init the iterator head */
   3891  1.1      rjs 	LIST_INIT(&sctppcbinfo.iteratorhead);
   3892  1.1      rjs 
   3893  1.1      rjs 	/* init the hash table of endpoints */
   3894  1.1      rjs #if defined(__FreeBSD__)
   3895  1.1      rjs #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
   3896  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &hashtblsize);
   3897  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
   3898  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
   3899  1.1      rjs #else
   3900  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
   3901  1.1      rjs 	    hashtblsize);
   3902  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
   3903  1.1      rjs 	    sctp_pcbtblsize);
   3904  1.1      rjs 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
   3905  1.1      rjs 	    sctp_chunkscale);
   3906  1.1      rjs #endif
   3907  1.1      rjs #endif
   3908  1.1      rjs 
   3909  1.1      rjs 	sctppcbinfo.sctp_asochash = hashinit((hashtblsize * 31), HASH_LIST,
   3910  1.1      rjs 			M_WAITOK, &sctppcbinfo.hashasocmark);
   3911  1.1      rjs 
   3912  1.1      rjs 	sctppcbinfo.sctp_ephash = hashinit(hashtblsize, HASH_LIST,
   3913  1.1      rjs 			M_WAITOK, &sctppcbinfo.hashmark);
   3914  1.1      rjs 
   3915  1.1      rjs 	sctppcbinfo.sctp_tcpephash = hashinit(hashtblsize, HASH_LIST,
   3916  1.1      rjs 			M_WAITOK, &sctppcbinfo.hashtcpmark);
   3917  1.1      rjs 
   3918  1.1      rjs 	sctppcbinfo.hashtblsize = hashtblsize;
   3919  1.1      rjs 
   3920  1.1      rjs 	/* init the zones */
   3921  1.1      rjs 	/*
   3922  1.1      rjs 	 * FIX ME: Should check for NULL returns, but if it does fail we
   3923  1.1      rjs 	 * are doomed to panic anyways... add later maybe.
   3924  1.1      rjs 	 */
   3925  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
   3926  1.1      rjs 	    sizeof(struct sctp_inpcb), maxsockets);
   3927  1.1      rjs 
   3928  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
   3929  1.1      rjs 	    sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
   3930  1.1      rjs 
   3931  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
   3932  1.1      rjs 	    sizeof(struct sctp_laddr),
   3933  1.1      rjs 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
   3934  1.1      rjs 
   3935  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
   3936  1.1      rjs 	    sizeof(struct sctp_nets),
   3937  1.1      rjs 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
   3938  1.1      rjs 
   3939  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
   3940  1.1      rjs 	    sizeof(struct sctp_tmit_chunk),
   3941  1.1      rjs 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address *
   3942  1.1      rjs 	    sctp_chunkscale));
   3943  1.1      rjs 
   3944  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_sockq, "sctp_sockq",
   3945  1.1      rjs 	    sizeof(struct sctp_socket_q_list),
   3946  1.1      rjs 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address *
   3947  1.1      rjs 	    sctp_chunkscale));
   3948  1.1      rjs 
   3949  1.1      rjs 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_hash, "sctp_hash",
   3950  1.1      rjs 		       sizeof(void *) * sctp_pcbtblsize, maxsockets);
   3951  1.1      rjs 
   3952  1.1      rjs         /* Master Lock INIT for info structure */
   3953  1.1      rjs 	SCTP_INP_INFO_LOCK_INIT();
   3954  1.1      rjs 	SCTP_ITERATOR_LOCK_INIT();
   3955  1.1      rjs 	/* not sure if we need all the counts */
   3956  1.1      rjs 	sctppcbinfo.ipi_count_ep = 0;
   3957  1.1      rjs 	sctppcbinfo.ipi_gencnt_ep = 0;
   3958  1.1      rjs 	/* assoc/tcb zone info */
   3959  1.1      rjs 	sctppcbinfo.ipi_count_asoc = 0;
   3960  1.1      rjs 	sctppcbinfo.ipi_gencnt_asoc = 0;
   3961  1.1      rjs 	/* local addrlist zone info */
   3962  1.1      rjs 	sctppcbinfo.ipi_count_laddr = 0;
   3963  1.1      rjs 	sctppcbinfo.ipi_gencnt_laddr = 0;
   3964  1.1      rjs 	/* remote addrlist zone info */
   3965  1.1      rjs 	sctppcbinfo.ipi_count_raddr = 0;
   3966  1.1      rjs 	sctppcbinfo.ipi_gencnt_raddr = 0;
   3967  1.1      rjs 	/* chunk info */
   3968  1.1      rjs 	sctppcbinfo.ipi_count_chunk = 0;
   3969  1.1      rjs 	sctppcbinfo.ipi_gencnt_chunk = 0;
   3970  1.1      rjs 
   3971  1.1      rjs 	/* socket queue zone info */
   3972  1.1      rjs 	sctppcbinfo.ipi_count_sockq = 0;
   3973  1.1      rjs 	sctppcbinfo.ipi_gencnt_sockq = 0;
   3974  1.1      rjs 
   3975  1.1      rjs 	/* mbuf tracker */
   3976  1.1      rjs 	sctppcbinfo.mbuf_track = 0;
   3977  1.1      rjs 	/* port stuff */
   3978  1.1      rjs #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
   3979  1.1      rjs 	sctppcbinfo.lastlow = ipport_firstauto;
   3980  1.1      rjs #else
   3981  1.1      rjs 	sctppcbinfo.lastlow = anonportmin;
   3982  1.1      rjs #endif
   3983  1.1      rjs 	/* Init the TIMEWAIT list */
   3984  1.1      rjs 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
   3985  1.1      rjs 		LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
   3986  1.1      rjs 	}
   3987  1.1      rjs 
   3988  1.1      rjs #if defined(_SCTP_NEEDS_CALLOUT_) && !defined(__APPLE__)
   3989  1.1      rjs 	TAILQ_INIT(&sctppcbinfo.callqueue);
   3990  1.1      rjs #endif
   3991  1.1      rjs 
   3992  1.1      rjs }
   3993  1.1      rjs 
   3994  1.1      rjs int
   3995  1.1      rjs sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
   3996  1.1      rjs     int iphlen, int offset, int limit, struct sctphdr *sh,
   3997  1.1      rjs     struct sockaddr *altsa)
   3998  1.1      rjs {
   3999  1.1      rjs 	/*
   4000  1.1      rjs 	 * grub through the INIT pulling addresses and
   4001  1.1      rjs 	 * loading them to the nets structure in the asoc.
   4002  1.1      rjs 	 * The from address in the mbuf should also be loaded
   4003  1.1      rjs 	 * (if it is not already). This routine can be called
   4004  1.1      rjs 	 * with either INIT or INIT-ACK's as long as the
   4005  1.1      rjs 	 * m points to the IP packet and the offset points
   4006  1.1      rjs 	 * to the beginning of the parameters.
   4007  1.1      rjs 	 */
   4008  1.1      rjs 	struct sctp_inpcb *inp, *l_inp;
   4009  1.1      rjs 	struct sctp_nets *net, *net_tmp;
   4010  1.1      rjs 	struct ip *iph;
   4011  1.1      rjs 	struct sctp_paramhdr *phdr, parm_buf;
   4012  1.1      rjs 	struct sctp_tcb *stcb_tmp;
   4013  1.1      rjs 	u_int16_t ptype, plen;
   4014  1.1      rjs 	struct sockaddr *sa;
   4015  1.1      rjs 	struct sockaddr_storage dest_store;
   4016  1.1      rjs 	struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
   4017  1.1      rjs 	struct sockaddr_in sin;
   4018  1.1      rjs 	struct sockaddr_in6 sin6;
   4019  1.1      rjs 
   4020  1.1      rjs 	/* First get the destination address setup too. */
   4021  1.1      rjs 	memset(&sin, 0, sizeof(sin));
   4022  1.1      rjs 	memset(&sin6, 0, sizeof(sin6));
   4023  1.1      rjs 
   4024  1.1      rjs 	sin.sin_family = AF_INET;
   4025  1.1      rjs 	sin.sin_len = sizeof(sin);
   4026  1.1      rjs 	sin.sin_port = stcb->rport;
   4027  1.1      rjs 
   4028  1.1      rjs 	sin6.sin6_family = AF_INET6;
   4029  1.1      rjs 	sin6.sin6_len = sizeof(struct sockaddr_in6);
   4030  1.1      rjs 	sin6.sin6_port = stcb->rport;
   4031  1.1      rjs 	if (altsa == NULL) {
   4032  1.1      rjs 		iph = mtod(m, struct ip *);
   4033  1.1      rjs 		if (iph->ip_v == IPVERSION) {
   4034  1.1      rjs 			/* its IPv4 */
   4035  1.1      rjs 			struct sockaddr_in *sin_2;
   4036  1.1      rjs 			sin_2 = (struct sockaddr_in *)(local_sa);
   4037  1.1      rjs 			memset(sin_2, 0, sizeof(sin));
   4038  1.1      rjs 			sin_2->sin_family = AF_INET;
   4039  1.1      rjs 			sin_2->sin_len = sizeof(sin);
   4040  1.1      rjs 			sin_2->sin_port = sh->dest_port;
   4041  1.1      rjs 			sin_2->sin_addr.s_addr = iph->ip_dst.s_addr ;
   4042  1.1      rjs 			sin.sin_addr = iph->ip_src;
   4043  1.1      rjs 			sa = (struct sockaddr *)&sin;
   4044  1.1      rjs 		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   4045  1.1      rjs 			/* its IPv6 */
   4046  1.1      rjs 			struct ip6_hdr *ip6;
   4047  1.1      rjs 			struct sockaddr_in6 *sin6_2;
   4048  1.1      rjs 
   4049  1.1      rjs 			ip6 = mtod(m, struct ip6_hdr *);
   4050  1.1      rjs 			sin6_2 = (struct sockaddr_in6 *)(local_sa);
   4051  1.1      rjs 			memset(sin6_2, 0, sizeof(sin6));
   4052  1.1      rjs 			sin6_2->sin6_family = AF_INET6;
   4053  1.1      rjs 			sin6_2->sin6_len = sizeof(struct sockaddr_in6);
   4054  1.1      rjs 			sin6_2->sin6_port = sh->dest_port;
   4055  1.1      rjs 			sin6.sin6_addr = ip6->ip6_src;
   4056  1.1      rjs 			sa = (struct sockaddr *)&sin6;
   4057  1.1      rjs 		} else {
   4058  1.1      rjs 			sa = NULL;
   4059  1.1      rjs 		}
   4060  1.1      rjs 	} else {
   4061  1.1      rjs 		/*
   4062  1.1      rjs 		 * For cookies we use the src address NOT from the packet
   4063  1.1      rjs 		 * but from the original INIT
   4064  1.1      rjs 		 */
   4065  1.1      rjs 		sa = altsa;
   4066  1.1      rjs 	}
   4067  1.1      rjs 	/* Turn off ECN until we get through all params */
   4068  1.1      rjs 	stcb->asoc.ecn_allowed = 0;
   4069  1.1      rjs 
   4070  1.1      rjs 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   4071  1.1      rjs 		/* mark all addresses that we have currently on the list */
   4072  1.1      rjs 		net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
   4073  1.1      rjs 	}
   4074  1.1      rjs 	/* does the source address already exist? if so skip it */
   4075  1.1      rjs 	l_inp = inp = stcb->sctp_ep;
   4076  1.1      rjs 	stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
   4077  1.1      rjs 	if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
   4078  1.1      rjs 		/* we must add the source address */
   4079  1.1      rjs 		/* no scope set here since we have a tcb already. */
   4080  1.1      rjs 		if ((sa->sa_family == AF_INET) &&
   4081  1.1      rjs 		    (stcb->asoc.ipv4_addr_legal)) {
   4082  1.1      rjs 			if (sctp_add_remote_addr(stcb, sa, 0, 2)) {
   4083  1.1      rjs 				return (-1);
   4084  1.1      rjs 			}
   4085  1.1      rjs 		} else if ((sa->sa_family == AF_INET6) &&
   4086  1.1      rjs 		    (stcb->asoc.ipv6_addr_legal)) {
   4087  1.1      rjs 			if (sctp_add_remote_addr(stcb, sa, 0, 3)) {
   4088  1.1      rjs 				return (-1);
   4089  1.1      rjs 			}
   4090  1.1      rjs 		}
   4091  1.1      rjs 	} else {
   4092  1.1      rjs 		if (net_tmp != NULL && stcb_tmp == stcb) {
   4093  1.1      rjs 			net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
   4094  1.1      rjs 		} else if (stcb_tmp != stcb) {
   4095  1.1      rjs 			/* It belongs to another association? */
   4096  1.1      rjs 			return (-1);
   4097  1.1      rjs 		}
   4098  1.1      rjs 	}
   4099  1.1      rjs 	/* since a unlock occured we must check the
   4100  1.1      rjs 	 * TCB's state and the pcb's gone flags.
   4101  1.1      rjs 	 */
   4102  1.1      rjs 	if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4103  1.1      rjs 		/* the user freed the ep */
   4104  1.1      rjs 		return (-1);
   4105  1.1      rjs 	}
   4106  1.1      rjs 	if (stcb->asoc.state == 0) {
   4107  1.1      rjs 		/* the assoc was freed? */
   4108  1.1      rjs 		return (-1);
   4109  1.1      rjs 	}
   4110  1.1      rjs 
   4111  1.1      rjs 	/* now we must go through each of the params. */
   4112  1.1      rjs 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
   4113  1.1      rjs 	while (phdr) {
   4114  1.1      rjs 		ptype = ntohs(phdr->param_type);
   4115  1.1      rjs 		plen = ntohs(phdr->param_length);
   4116  1.1      rjs 		/*printf("ptype => %d, plen => %d\n", ptype, plen);*/
   4117  1.1      rjs 		if (offset + plen > limit) {
   4118  1.1      rjs 			break;
   4119  1.1      rjs 		}
   4120  1.1      rjs 		if (plen == 0) {
   4121  1.1      rjs 			break;
   4122  1.1      rjs 		}
   4123  1.1      rjs 		if ((ptype == SCTP_IPV4_ADDRESS) &&
   4124  1.1      rjs 		    (stcb->asoc.ipv4_addr_legal)) {
   4125  1.1      rjs 			struct sctp_ipv4addr_param *p4, p4_buf;
   4126  1.1      rjs 			/* ok get the v4 address and check/add */
   4127  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   4128  1.1      rjs 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
   4129  1.1      rjs 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
   4130  1.1      rjs 			    phdr == NULL) {
   4131  1.1      rjs 				return (-1);
   4132  1.1      rjs 			}
   4133  1.1      rjs 			p4 = (struct sctp_ipv4addr_param *)phdr;
   4134  1.1      rjs 			sin.sin_addr.s_addr = p4->addr;
   4135  1.1      rjs 			sa = (struct sockaddr *)&sin;
   4136  1.1      rjs 			inp = stcb->sctp_ep;
   4137  1.1      rjs 			stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
   4138  1.1      rjs 			    local_sa, stcb);
   4139  1.1      rjs 
   4140  1.1      rjs 			if ((stcb_tmp== NULL && inp == stcb->sctp_ep) ||
   4141  1.1      rjs 			    inp == NULL) {
   4142  1.1      rjs 				/* we must add the source address */
   4143  1.1      rjs 				/* no scope set since we have a tcb already */
   4144  1.1      rjs 
   4145  1.1      rjs 				/* we must validate the state again here */
   4146  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4147  1.1      rjs 					/* the user freed the ep */
   4148  1.1      rjs 					return (-1);
   4149  1.1      rjs 				}
   4150  1.1      rjs 				if (stcb->asoc.state == 0) {
   4151  1.1      rjs 					/* the assoc was freed? */
   4152  1.1      rjs 					return (-1);
   4153  1.1      rjs 				}
   4154  1.1      rjs 				if (sctp_add_remote_addr(stcb, sa, 0, 4)) {
   4155  1.1      rjs 					return (-1);
   4156  1.1      rjs 				}
   4157  1.1      rjs 			} else if (stcb_tmp == stcb) {
   4158  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4159  1.1      rjs 					/* the user freed the ep */
   4160  1.1      rjs 					return (-1);
   4161  1.1      rjs 				}
   4162  1.1      rjs 				if (stcb->asoc.state == 0) {
   4163  1.1      rjs 					/* the assoc was freed? */
   4164  1.1      rjs 					return (-1);
   4165  1.1      rjs 				}
   4166  1.1      rjs 				if (net != NULL) {
   4167  1.1      rjs 					/* clear flag */
   4168  1.1      rjs 					net->dest_state &=
   4169  1.1      rjs 					    ~SCTP_ADDR_NOT_IN_ASSOC;
   4170  1.1      rjs 				}
   4171  1.1      rjs 			} else {
   4172  1.1      rjs 				/* strange, address is in another assoc?
   4173  1.1      rjs 				 * straighten out locks.
   4174  1.1      rjs 				 */
   4175  1.1      rjs 				SCTP_TCB_UNLOCK(stcb_tmp);
   4176  1.1      rjs 				SCTP_INP_RLOCK(inp);
   4177  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4178  1.1      rjs 					/* the user freed the ep */
   4179  1.1      rjs 					SCTP_INP_RUNLOCK(l_inp);
   4180  1.1      rjs 					return (-1);
   4181  1.1      rjs 				}
   4182  1.1      rjs 				if (stcb->asoc.state == 0) {
   4183  1.1      rjs 					/* the assoc was freed? */
   4184  1.1      rjs 					SCTP_INP_RUNLOCK(l_inp);
   4185  1.1      rjs 					return (-1);
   4186  1.1      rjs 				}
   4187  1.1      rjs 				SCTP_TCB_LOCK(stcb);
   4188  1.1      rjs 				SCTP_INP_RUNLOCK(stcb->sctp_ep);
   4189  1.1      rjs 				return (-1);
   4190  1.1      rjs 			}
   4191  1.1      rjs 		} else if ((ptype == SCTP_IPV6_ADDRESS) &&
   4192  1.1      rjs 		    (stcb->asoc.ipv6_addr_legal)) {
   4193  1.1      rjs 			/* ok get the v6 address and check/add */
   4194  1.1      rjs 			struct sctp_ipv6addr_param *p6, p6_buf;
   4195  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   4196  1.1      rjs 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
   4197  1.1      rjs 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
   4198  1.1      rjs 			    phdr == NULL) {
   4199  1.1      rjs 				return (-1);
   4200  1.1      rjs 			}
   4201  1.1      rjs 			p6 = (struct sctp_ipv6addr_param *)phdr;
   4202  1.1      rjs 			memcpy((void *)&sin6.sin6_addr, p6->addr,
   4203  1.1      rjs 			    sizeof(p6->addr));
   4204  1.1      rjs 			sa = (struct sockaddr *)&sin6;
   4205  1.1      rjs 			inp = stcb->sctp_ep;
   4206  1.1      rjs 			stcb_tmp= sctp_findassociation_ep_addr(&inp, sa, &net,
   4207  1.1      rjs 			    local_sa, stcb);
   4208  1.1      rjs 			if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
   4209  1.1      rjs 			    inp == NULL)) {
   4210  1.1      rjs 				/* we must validate the state again here */
   4211  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4212  1.1      rjs 					/* the user freed the ep */
   4213  1.1      rjs 					return (-1);
   4214  1.1      rjs 				}
   4215  1.1      rjs 				if (stcb->asoc.state == 0) {
   4216  1.1      rjs 					/* the assoc was freed? */
   4217  1.1      rjs 					return (-1);
   4218  1.1      rjs 				}
   4219  1.1      rjs 				/* we must add the address, no scope set */
   4220  1.1      rjs 				if (sctp_add_remote_addr(stcb, sa, 0, 5)) {
   4221  1.1      rjs 					return (-1);
   4222  1.1      rjs 				}
   4223  1.1      rjs 			} else if (stcb_tmp == stcb) {
   4224  1.1      rjs 				/* we must validate the state again here */
   4225  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4226  1.1      rjs 					/* the user freed the ep */
   4227  1.1      rjs 					return (-1);
   4228  1.1      rjs 				}
   4229  1.1      rjs 				if (stcb->asoc.state == 0) {
   4230  1.1      rjs 					/* the assoc was freed? */
   4231  1.1      rjs 					return (-1);
   4232  1.1      rjs 				}
   4233  1.1      rjs 				if (net != NULL) {
   4234  1.1      rjs 					/* clear flag */
   4235  1.1      rjs 					net->dest_state &=
   4236  1.1      rjs 					    ~SCTP_ADDR_NOT_IN_ASSOC;
   4237  1.1      rjs 				}
   4238  1.1      rjs 			} else {
   4239  1.1      rjs 				/* strange, address is in another assoc?
   4240  1.1      rjs 				 * straighten out locks.
   4241  1.1      rjs 				 */
   4242  1.1      rjs 				SCTP_TCB_UNLOCK(stcb_tmp);
   4243  1.1      rjs 				SCTP_INP_RLOCK(l_inp);
   4244  1.1      rjs 				/* we must validate the state again here */
   4245  1.1      rjs 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   4246  1.1      rjs 					/* the user freed the ep */
   4247  1.1      rjs 					SCTP_INP_RUNLOCK(l_inp);
   4248  1.1      rjs 					return (-1);
   4249  1.1      rjs 				}
   4250  1.1      rjs 				if (stcb->asoc.state == 0) {
   4251  1.1      rjs 					/* the assoc was freed? */
   4252  1.1      rjs 					SCTP_INP_RUNLOCK(l_inp);
   4253  1.1      rjs 					return (-1);
   4254  1.1      rjs 				}
   4255  1.1      rjs 				SCTP_TCB_LOCK(stcb);
   4256  1.1      rjs 				SCTP_INP_RUNLOCK(l_inp);
   4257  1.1      rjs 				return (-1);
   4258  1.1      rjs 			}
   4259  1.1      rjs 		} else if (ptype == SCTP_ECN_CAPABLE) {
   4260  1.1      rjs 			stcb->asoc.ecn_allowed = 1;
   4261  1.1      rjs 		} else if (ptype == SCTP_ULP_ADAPTION) {
   4262  1.1      rjs 			if (stcb->asoc.state != SCTP_STATE_OPEN) {
   4263  1.1      rjs 				struct sctp_adaption_layer_indication ai, *aip;
   4264  1.1      rjs 
   4265  1.1      rjs 				phdr = sctp_get_next_param(m, offset,
   4266  1.1      rjs 							   (struct sctp_paramhdr *)&ai, sizeof(ai));
   4267  1.1      rjs 				aip = (struct sctp_adaption_layer_indication *)phdr;
   4268  1.1      rjs 				sctp_ulp_notify(SCTP_NOTIFY_ADAPTION_INDICATION,
   4269  1.1      rjs 						stcb, ntohl(aip->indication), NULL);
   4270  1.1      rjs 			}
   4271  1.1      rjs 		} else if (ptype == SCTP_SET_PRIM_ADDR) {
   4272  1.1      rjs 			struct sctp_asconf_addr_param lstore, *fee;
   4273  1.1      rjs 			struct sctp_asconf_addrv4_param *fii;
   4274  1.1      rjs 			int lptype;
   4275  1.1      rjs 			struct sockaddr *lsa = NULL;
   4276  1.1      rjs 
   4277  1.1      rjs 			stcb->asoc.peer_supports_asconf = 1;
   4278  1.1      rjs 			stcb->asoc.peer_supports_asconf_setprim = 1;
   4279  1.1      rjs 			if (plen > sizeof(lstore)) {
   4280  1.1      rjs 				return (-1);
   4281  1.1      rjs 			}
   4282  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   4283  1.1      rjs     			    (struct sctp_paramhdr *)&lstore, plen);
   4284  1.1      rjs 			if (phdr == NULL) {
   4285  1.1      rjs 				return (-1);
   4286  1.1      rjs 			}
   4287  1.1      rjs 
   4288  1.1      rjs 			fee  = (struct sctp_asconf_addr_param *)phdr;
   4289  1.1      rjs 			lptype = ntohs(fee->addrp.ph.param_type);
   4290  1.1      rjs 			if (lptype == SCTP_IPV4_ADDRESS) {
   4291  1.1      rjs 				if (plen !=
   4292  1.1      rjs 				    sizeof(struct sctp_asconf_addrv4_param)) {
   4293  1.1      rjs 					printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
   4294  1.1      rjs 				           (int)sizeof(struct sctp_asconf_addrv4_param),
   4295  1.1      rjs 				           plen);
   4296  1.1      rjs 				} else {
   4297  1.1      rjs 					fii = (struct sctp_asconf_addrv4_param *)fee;
   4298  1.1      rjs 					sin.sin_addr.s_addr = fii->addrp.addr;
   4299  1.1      rjs 					lsa = (struct sockaddr *)&sin;
   4300  1.1      rjs 				}
   4301  1.1      rjs 			} else if (lptype == SCTP_IPV6_ADDRESS) {
   4302  1.1      rjs 				if (plen !=
   4303  1.1      rjs 				    sizeof(struct sctp_asconf_addr_param)) {
   4304  1.1      rjs 					printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
   4305  1.1      rjs 				           (int)sizeof(struct sctp_asconf_addr_param),
   4306  1.1      rjs 				           plen);
   4307  1.1      rjs 				} else {
   4308  1.1      rjs 					memcpy(sin6.sin6_addr.s6_addr,
   4309  1.1      rjs 					    fee->addrp.addr,
   4310  1.1      rjs 					    sizeof(fee->addrp.addr));
   4311  1.1      rjs 					lsa = (struct sockaddr *)&sin6;
   4312  1.1      rjs 				}
   4313  1.1      rjs 			}
   4314  1.1      rjs 			if (lsa) {
   4315  1.1      rjs 				sctp_set_primary_addr(stcb, sa, NULL);
   4316  1.1      rjs 			}
   4317  1.1      rjs 
   4318  1.1      rjs 		} else if (ptype == SCTP_PRSCTP_SUPPORTED) {
   4319  1.1      rjs 			/* Peer supports pr-sctp */
   4320  1.1      rjs 			stcb->asoc.peer_supports_prsctp = 1;
   4321  1.1      rjs 		} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
   4322  1.1      rjs 			/* A supported extension chunk */
   4323  1.1      rjs 			struct sctp_supported_chunk_types_param *pr_supported;
   4324  1.1      rjs 			uint8_t local_store[128];
   4325  1.1      rjs 			int num_ent, i;
   4326  1.1      rjs 
   4327  1.1      rjs 			phdr = sctp_get_next_param(m, offset,
   4328  1.1      rjs     			    (struct sctp_paramhdr *)&local_store, plen);
   4329  1.1      rjs 			if (phdr == NULL) {
   4330  1.1      rjs 				return (-1);
   4331  1.1      rjs 			}
   4332  1.1      rjs 			stcb->asoc.peer_supports_asconf = 0;
   4333  1.1      rjs 			stcb->asoc.peer_supports_asconf_setprim = 0;
   4334  1.1      rjs 			stcb->asoc.peer_supports_prsctp = 0;
   4335  1.1      rjs 			stcb->asoc.peer_supports_pktdrop = 0;
   4336  1.1      rjs 			stcb->asoc.peer_supports_strreset = 0;
   4337  1.1      rjs 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
   4338  1.1      rjs 			num_ent = plen - sizeof(struct sctp_paramhdr);
   4339  1.1      rjs 			for (i=0; i<num_ent; i++) {
   4340  1.1      rjs 				switch (pr_supported->chunk_types[i]) {
   4341  1.1      rjs 				case SCTP_ASCONF:
   4342  1.1      rjs 					stcb->asoc.peer_supports_asconf = 1;
   4343  1.1      rjs 					stcb->asoc.peer_supports_asconf_setprim = 1;
   4344  1.1      rjs 					break;
   4345  1.1      rjs 				case SCTP_ASCONF_ACK:
   4346  1.1      rjs 					stcb->asoc.peer_supports_asconf = 1;
   4347  1.1      rjs 					stcb->asoc.peer_supports_asconf_setprim = 1;
   4348  1.1      rjs 					break;
   4349  1.1      rjs 				case SCTP_FORWARD_CUM_TSN:
   4350  1.1      rjs 					stcb->asoc.peer_supports_prsctp = 1;
   4351  1.1      rjs 					break;
   4352  1.1      rjs 				case SCTP_PACKET_DROPPED:
   4353  1.1      rjs 					stcb->asoc.peer_supports_pktdrop = 1;
   4354  1.1      rjs 					break;
   4355  1.1      rjs 				case SCTP_STREAM_RESET:
   4356  1.1      rjs 					stcb->asoc.peer_supports_strreset = 1;
   4357  1.1      rjs 					break;
   4358  1.1      rjs 				default:
   4359  1.1      rjs 					/* one I have not learned yet */
   4360  1.1      rjs 					break;
   4361  1.1      rjs 
   4362  1.1      rjs 				}
   4363  1.1      rjs 			}
   4364  1.1      rjs 		} else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
   4365  1.1      rjs 			/* Peer supports ECN-nonce */
   4366  1.1      rjs 			stcb->asoc.peer_supports_ecn_nonce = 1;
   4367  1.1      rjs 			stcb->asoc.ecn_nonce_allowed = 1;
   4368  1.1      rjs 		} else if ((ptype == SCTP_HEARTBEAT_INFO) ||
   4369  1.1      rjs 			   (ptype == SCTP_STATE_COOKIE) ||
   4370  1.1      rjs 			   (ptype == SCTP_UNRECOG_PARAM) ||
   4371  1.1      rjs 			   (ptype == SCTP_COOKIE_PRESERVE) ||
   4372  1.1      rjs 			   (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
   4373  1.1      rjs 			   (ptype == SCTP_ADD_IP_ADDRESS) ||
   4374  1.1      rjs 			   (ptype == SCTP_DEL_IP_ADDRESS) ||
   4375  1.1      rjs 			   (ptype == SCTP_ERROR_CAUSE_IND) ||
   4376  1.1      rjs 			   (ptype == SCTP_SUCCESS_REPORT)) {
   4377  1.1      rjs 			/* don't care */;
   4378  1.1      rjs 		} else {
   4379  1.1      rjs 			if ((ptype & 0x8000) == 0x0000) {
   4380  1.1      rjs 				/* must stop processing the rest of
   4381  1.1      rjs 				 * the param's. Any report bits were
   4382  1.1      rjs 				 * handled with the call to sctp_arethere_unrecognized_parameters()
   4383  1.1      rjs 				 * when the INIT or INIT-ACK was first seen.
   4384  1.1      rjs 				 */
   4385  1.1      rjs 				break;
   4386  1.1      rjs 			}
   4387  1.1      rjs 		}
   4388  1.1      rjs 		offset += SCTP_SIZE32(plen);
   4389  1.1      rjs 		if (offset >= limit) {
   4390  1.1      rjs 			break;
   4391  1.1      rjs 		}
   4392  1.1      rjs 		phdr = sctp_get_next_param(m, offset, &parm_buf,
   4393  1.1      rjs 		    sizeof(parm_buf));
   4394  1.1      rjs 	}
   4395  1.1      rjs 	/* Now check to see if we need to purge any addresses */
   4396  1.1      rjs 	for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
   4397  1.1      rjs 		net_tmp = TAILQ_NEXT(net, sctp_next);
   4398  1.1      rjs 		if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
   4399  1.1      rjs 		    SCTP_ADDR_NOT_IN_ASSOC) {
   4400  1.1      rjs 			/* This address has been removed from the asoc */
   4401  1.1      rjs 			/* remove and free it */
   4402  1.1      rjs 			stcb->asoc.numnets--;
   4403  1.1      rjs 			TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
   4404  1.1      rjs 			sctp_free_remote_addr(net);
   4405  1.1      rjs 			if (net == stcb->asoc.primary_destination) {
   4406  1.1      rjs 				stcb->asoc.primary_destination = NULL;
   4407  1.1      rjs 				sctp_select_primary_destination(stcb);
   4408  1.1      rjs 			}
   4409  1.1      rjs 		}
   4410  1.1      rjs 	}
   4411  1.1      rjs 	return (0);
   4412  1.1      rjs }
   4413  1.1      rjs 
   4414  1.1      rjs int
   4415  1.1      rjs sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
   4416  1.1      rjs     struct sctp_nets *net)
   4417  1.1      rjs {
   4418  1.1      rjs 	/* make sure the requested primary address exists in the assoc */
   4419  1.1      rjs 	if (net == NULL && sa)
   4420  1.1      rjs 		net = sctp_findnet(stcb, sa);
   4421  1.1      rjs 
   4422  1.1      rjs 	if (net == NULL) {
   4423  1.1      rjs 		/* didn't find the requested primary address! */
   4424  1.1      rjs 		return (-1);
   4425  1.1      rjs 	} else {
   4426  1.1      rjs 		/* set the primary address */
   4427  1.1      rjs 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
   4428  1.1      rjs 			/* Must be confirmed */
   4429  1.1      rjs 			return (-1);
   4430  1.1      rjs 		}
   4431  1.1      rjs 		stcb->asoc.primary_destination = net;
   4432  1.1      rjs 		net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
   4433  1.1      rjs 		return (0);
   4434  1.1      rjs 	}
   4435  1.1      rjs }
   4436  1.1      rjs 
   4437  1.1      rjs 
   4438  1.1      rjs int
   4439  1.1      rjs sctp_is_vtag_good(struct sctp_inpcb *inp, u_int32_t tag, struct timeval *now)
   4440  1.1      rjs {
   4441  1.1      rjs 	/*
   4442  1.1      rjs 	 * This function serves two purposes. It will see if a TAG can be
   4443  1.1      rjs 	 * re-used and return 1 for yes it is ok and 0 for don't use that
   4444  1.1      rjs 	 * tag.
   4445  1.1      rjs 	 * A secondary function it will do is purge out old tags that can
   4446  1.1      rjs 	 * be removed.
   4447  1.1      rjs 	 */
   4448  1.1      rjs 	struct sctpasochead *head;
   4449  1.1      rjs 	struct sctpvtaghead *chain;
   4450  1.1      rjs 	struct sctp_tagblock *twait_block;
   4451  1.1      rjs 	struct sctp_tcb *stcb;
   4452  1.1      rjs 
   4453  1.1      rjs 	int i;
   4454  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   4455  1.1      rjs 	chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
   4456  1.1      rjs 	/* First is the vtag in use ? */
   4457  1.1      rjs 
   4458  1.1      rjs 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
   4459  1.1      rjs 	    sctppcbinfo.hashasocmark)];
   4460  1.1      rjs 	if (head == NULL) {
   4461  1.1      rjs 		SCTP_INP_INFO_WUNLOCK();
   4462  1.1      rjs 		return (0);
   4463  1.1      rjs 	}
   4464  1.1      rjs 	LIST_FOREACH(stcb, head, sctp_asocs) {
   4465  1.1      rjs 		if (stcb->asoc.my_vtag == tag) {
   4466  1.1      rjs 			/* We should remove this if and
   4467  1.1      rjs 			 * return 0 always if we want vtags
   4468  1.1      rjs 			 * unique across all endpoints. For
   4469  1.1      rjs 			 * now within a endpoint is ok.
   4470  1.1      rjs 			 */
   4471  1.1      rjs  			if (inp == stcb->sctp_ep) {
   4472  1.1      rjs 				/* bad tag, in use */
   4473  1.1      rjs 				SCTP_INP_INFO_WUNLOCK();
   4474  1.1      rjs 				return (0);
   4475  1.1      rjs 			}
   4476  1.1      rjs 		}
   4477  1.1      rjs 	}
   4478  1.1      rjs 	if (!LIST_EMPTY(chain)) {
   4479  1.1      rjs 		/*
   4480  1.1      rjs 		 * Block(s) are present, lets see if we have this tag in
   4481  1.1      rjs 		 * the list
   4482  1.1      rjs 		 */
   4483  1.1      rjs 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
   4484  1.1      rjs 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
   4485  1.1      rjs 				if (twait_block->vtag_block[i].v_tag == 0) {
   4486  1.1      rjs 					/* not used */
   4487  1.1      rjs 					continue;
   4488  1.1      rjs 				} else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
   4489  1.1      rjs 				    now->tv_sec) {
   4490  1.1      rjs 					/* Audit expires this guy */
   4491  1.1      rjs 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
   4492  1.1      rjs 					twait_block->vtag_block[i].v_tag = 0;
   4493  1.1      rjs 				} else if (twait_block->vtag_block[i].v_tag ==
   4494  1.1      rjs 				    tag) {
   4495  1.1      rjs 					/* Bad tag, sorry :< */
   4496  1.1      rjs 					SCTP_INP_INFO_WUNLOCK();
   4497  1.1      rjs 					return (0);
   4498  1.1      rjs 				}
   4499  1.1      rjs 			}
   4500  1.1      rjs 		}
   4501  1.1      rjs 	}
   4502  1.1      rjs 	/* Not found, ok to use the tag */
   4503  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   4504  1.1      rjs 	return (1);
   4505  1.1      rjs }
   4506  1.1      rjs 
   4507  1.1      rjs 
   4508  1.1      rjs /*
   4509  1.1      rjs  * Delete the address from the endpoint local address list
   4510  1.1      rjs  * Lookup using a sockaddr address (ie. not an ifaddr)
   4511  1.1      rjs  */
   4512  1.1      rjs int
   4513  1.1      rjs sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
   4514  1.1      rjs {
   4515  1.1      rjs 	struct sctp_laddr *laddr;
   4516  1.1      rjs 	struct sockaddr *l_sa;
   4517  1.1      rjs 	int found = 0;
   4518  1.1      rjs 	/* Here is another function I cannot find a
   4519  1.1      rjs 	 * caller for. As such we SHOULD delete it
   4520  1.1      rjs 	 * if we have no users. If we find a user that
   4521  1.1      rjs 	 * user MUST have the INP locked.
   4522  1.1      rjs 	 *
   4523  1.1      rjs 	 */
   4524  1.1      rjs 
   4525  1.1      rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   4526  1.1      rjs 		/* You are already bound to all. You have it already */
   4527  1.1      rjs 		return (EINVAL);
   4528  1.1      rjs 	}
   4529  1.1      rjs 
   4530  1.1      rjs 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   4531  1.1      rjs 		/* make sure the address exists */
   4532  1.1      rjs 		if (laddr->ifa == NULL)
   4533  1.1      rjs 			continue;
   4534  1.1      rjs 		if (laddr->ifa->ifa_addr == NULL)
   4535  1.1      rjs 			continue;
   4536  1.1      rjs 
   4537  1.1      rjs 		l_sa = laddr->ifa->ifa_addr;
   4538  1.1      rjs 		if (l_sa->sa_family == AF_INET6) {
   4539  1.1      rjs 			/* IPv6 address */
   4540  1.1      rjs 			struct sockaddr_in6 *sin1, *sin2;
   4541  1.1      rjs 			sin1 = (struct sockaddr_in6 *)l_sa;
   4542  1.1      rjs 			sin2 = (struct sockaddr_in6 *)sa;
   4543  1.1      rjs 			if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
   4544  1.1      rjs 			    sizeof(struct in6_addr)) == 0) {
   4545  1.1      rjs 				/* matched */
   4546  1.1      rjs 				found = 1;
   4547  1.1      rjs 				break;
   4548  1.1      rjs 			}
   4549  1.1      rjs 		} else if (l_sa->sa_family == AF_INET) {
   4550  1.1      rjs 			/* IPv4 address */
   4551  1.1      rjs 			struct sockaddr_in *sin1, *sin2;
   4552  1.1      rjs 			sin1 = (struct sockaddr_in *)l_sa;
   4553  1.1      rjs 			sin2 = (struct sockaddr_in *)sa;
   4554  1.1      rjs 			if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
   4555  1.1      rjs 				/* matched */
   4556  1.1      rjs 				found = 1;
   4557  1.1      rjs 				break;
   4558  1.1      rjs 			}
   4559  1.1      rjs 		} else {
   4560  1.1      rjs 			/* invalid family */
   4561  1.1      rjs 			return (-1);
   4562  1.1      rjs 		}
   4563  1.1      rjs 	}
   4564  1.1      rjs 
   4565  1.1      rjs 	if (found && inp->laddr_count < 2) {
   4566  1.1      rjs 		/* can't delete unless there are at LEAST 2 addresses */
   4567  1.1      rjs 		return (-1);
   4568  1.1      rjs 	}
   4569  1.1      rjs 
   4570  1.1      rjs 	if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
   4571  1.1      rjs 		/*
   4572  1.1      rjs 		 * remove it from the ep list, this should NOT be
   4573  1.1      rjs 		 * done until its really gone from the interface list and
   4574  1.1      rjs 		 * we won't be receiving more of these. Probably right
   4575  1.1      rjs 		 * away. If we do allow a removal of an address from
   4576  1.1      rjs 		 * an association (sub-set bind) than this should NOT
   4577  1.1      rjs 		 * be called until the all ASCONF come back from this
   4578  1.1      rjs 		 * association.
   4579  1.1      rjs 		 */
   4580  1.1      rjs 		sctp_remove_laddr(laddr);
   4581  1.1      rjs 		return (0);
   4582  1.1      rjs 	} else {
   4583  1.1      rjs 		return (-1);
   4584  1.1      rjs 	}
   4585  1.1      rjs }
   4586  1.1      rjs 
   4587  1.1      rjs static void
   4588  1.1      rjs sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   4589  1.1      rjs {
   4590  1.1      rjs 	/*
   4591  1.1      rjs 	 * We must hunt this association for MBUF's past the cumack
   4592  1.1      rjs 	 * (i.e. out of order data that we can renege on).
   4593  1.1      rjs 	 */
   4594  1.1      rjs 	struct sctp_association *asoc;
   4595  1.1      rjs 	struct sctp_tmit_chunk *chk, *nchk;
   4596  1.1      rjs 	u_int32_t cumulative_tsn_p1, tsn;
   4597  1.1      rjs 	int cnt, strmat, gap;
   4598  1.1      rjs 	/* We look for anything larger than the cum-ack + 1 */
   4599  1.1      rjs 
   4600  1.1      rjs 	asoc = &stcb->asoc;
   4601  1.1      rjs 	cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
   4602  1.1      rjs 	cnt = 0;
   4603  1.1      rjs 	/* First look in the re-assembly queue */
   4604  1.1      rjs 	chk = TAILQ_FIRST(&asoc->reasmqueue);
   4605  1.1      rjs 	while (chk) {
   4606  1.1      rjs 		/* Get the next one */
   4607  1.1      rjs 		nchk = TAILQ_NEXT(chk, sctp_next);
   4608  1.1      rjs 		if (compare_with_wrap(chk->rec.data.TSN_seq,
   4609  1.1      rjs 		    cumulative_tsn_p1, MAX_TSN)) {
   4610  1.1      rjs 			/* Yep it is above cum-ack */
   4611  1.1      rjs 			cnt++;
   4612  1.1      rjs 			tsn = chk->rec.data.TSN_seq;
   4613  1.1      rjs 			if (tsn >= asoc->mapping_array_base_tsn) {
   4614  1.1      rjs 				gap  = tsn - asoc->mapping_array_base_tsn;
   4615  1.1      rjs 			} else {
   4616  1.1      rjs 				gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
   4617  1.1      rjs 				    tsn + 1;
   4618  1.1      rjs 			}
   4619  1.1      rjs 			asoc->size_on_reasm_queue -= chk->send_size;
   4620  1.1      rjs 			asoc->cnt_on_reasm_queue--;
   4621  1.1      rjs 			SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
   4622  1.1      rjs 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
   4623  1.1      rjs 			if (chk->data) {
   4624  1.1      rjs 				sctp_m_freem(chk->data);
   4625  1.1      rjs 				chk->data = NULL;
   4626  1.1      rjs 			}
   4627  1.1      rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4628  1.1      rjs 			sctppcbinfo.ipi_count_chunk--;
   4629  1.1      rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4630  1.1      rjs 				panic("Chunk count is negative");
   4631  1.1      rjs 			}
   4632  1.1      rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   4633  1.1      rjs 		}
   4634  1.1      rjs 		chk = nchk;
   4635  1.1      rjs 	}
   4636  1.1      rjs 	/* Ok that was fun, now we will drain all the inbound streams? */
   4637  1.1      rjs 	for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
   4638  1.1      rjs 		chk = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
   4639  1.1      rjs 		while (chk) {
   4640  1.1      rjs 			nchk = TAILQ_NEXT(chk, sctp_next);
   4641  1.1      rjs 			if (compare_with_wrap(chk->rec.data.TSN_seq,
   4642  1.1      rjs 			    cumulative_tsn_p1, MAX_TSN)) {
   4643  1.1      rjs 				/* Yep it is above cum-ack */
   4644  1.1      rjs 				cnt++;
   4645  1.1      rjs 				tsn = chk->rec.data.TSN_seq;
   4646  1.1      rjs 				if (tsn >= asoc->mapping_array_base_tsn) {
   4647  1.1      rjs 					gap = tsn -
   4648  1.1      rjs 					    asoc->mapping_array_base_tsn;
   4649  1.1      rjs 				} else {
   4650  1.1      rjs 					gap = (MAX_TSN -
   4651  1.1      rjs 					    asoc->mapping_array_base_tsn) +
   4652  1.1      rjs 					    tsn + 1;
   4653  1.1      rjs 				}
   4654  1.1      rjs 				asoc->size_on_all_streams -= chk->send_size;
   4655  1.1      rjs 				asoc->cnt_on_all_streams--;
   4656  1.1      rjs 
   4657  1.1      rjs 				SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
   4658  1.1      rjs 				    gap);
   4659  1.1      rjs 				TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
   4660  1.1      rjs 				    chk, sctp_next);
   4661  1.1      rjs 				if (chk->data) {
   4662  1.1      rjs 					sctp_m_freem(chk->data);
   4663  1.1      rjs 					chk->data = NULL;
   4664  1.1      rjs 				}
   4665  1.1      rjs 				SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4666  1.1      rjs 				sctppcbinfo.ipi_count_chunk--;
   4667  1.1      rjs 				if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4668  1.1      rjs 					panic("Chunk count is negative");
   4669  1.1      rjs 				}
   4670  1.1      rjs 				sctppcbinfo.ipi_gencnt_chunk++;
   4671  1.1      rjs 			}
   4672  1.1      rjs 			chk = nchk;
   4673  1.1      rjs 		}
   4674  1.1      rjs 	}
   4675  1.1      rjs 	/*
   4676  1.1      rjs 	 * Question, should we go through the delivery queue?
   4677  1.1      rjs 	 * The only reason things are on here is the app not reading OR a
   4678  1.1      rjs 	 * p-d-api up. An attacker COULD send enough in to initiate the
   4679  1.1      rjs 	 * PD-API and then send a bunch of stuff to other streams... these
   4680  1.1      rjs 	 * would wind up on the delivery queue.. and then we would not get
   4681  1.1      rjs 	 * to them. But in order to do this I then have to back-track and
   4682  1.1      rjs 	 * un-deliver sequence numbers in streams.. el-yucko. I think for
   4683  1.1      rjs 	 * now we will NOT look at the delivery queue and leave it to be
   4684  1.1      rjs 	 * something to consider later. An alternative would be to abort
   4685  1.1      rjs 	 * the P-D-API with a notification and then deliver the data....
   4686  1.1      rjs 	 * Or another method might be to keep track of how many times the
   4687  1.1      rjs 	 * situation occurs and if we see a possible attack underway just
   4688  1.1      rjs 	 * abort the association.
   4689  1.1      rjs 	 */
   4690  1.1      rjs #ifdef SCTP_DEBUG
   4691  1.1      rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   4692  1.1      rjs 		if (cnt) {
   4693  1.1      rjs 			printf("Freed %d chunks from reneg harvest\n", cnt);
   4694  1.1      rjs 		}
   4695  1.1      rjs 	}
   4696  1.1      rjs #endif /* SCTP_DEBUG */
   4697  1.1      rjs 
   4698  1.1      rjs 	/*
   4699  1.1      rjs 	 * Another issue, in un-setting the TSN's in the mapping array we
   4700  1.1      rjs 	 * DID NOT adjust the higest_tsn marker.  This will cause one of
   4701  1.1      rjs 	 * two things to occur. It may cause us to do extra work in checking
   4702  1.1      rjs 	 * for our mapping array movement. More importantly it may cause us
   4703  1.1      rjs 	 * to SACK every datagram. This may not be a bad thing though since
   4704  1.1      rjs 	 * we will recover once we get our cum-ack above and all this stuff
   4705  1.1      rjs 	 * we dumped recovered.
   4706  1.1      rjs 	 */
   4707  1.1      rjs }
   4708  1.1      rjs 
   4709  1.1      rjs void
   4710  1.1      rjs sctp_drain(void)
   4711  1.1      rjs {
   4712  1.1      rjs 	/*
   4713  1.1      rjs 	 * We must walk the PCB lists for ALL associations here. The system
   4714  1.1      rjs 	 * is LOW on MBUF's and needs help. This is where reneging will
   4715  1.1      rjs 	 * occur. We really hope this does NOT happen!
   4716  1.1      rjs 	 */
   4717  1.1      rjs 	struct sctp_inpcb *inp;
   4718  1.1      rjs 	struct sctp_tcb *stcb;
   4719  1.1      rjs 
   4720  1.1      rjs 	SCTP_INP_INFO_RLOCK();
   4721  1.1      rjs 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
   4722  1.1      rjs 		/* For each endpoint */
   4723  1.1      rjs 		SCTP_INP_RLOCK(inp);
   4724  1.1      rjs 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
   4725  1.1      rjs 			/* For each association */
   4726  1.1      rjs 			SCTP_TCB_LOCK(stcb);
   4727  1.1      rjs 			sctp_drain_mbufs(inp, stcb);
   4728  1.1      rjs 			SCTP_TCB_UNLOCK(stcb);
   4729  1.1      rjs 		}
   4730  1.1      rjs 		SCTP_INP_RUNLOCK(inp);
   4731  1.1      rjs 	}
   4732  1.1      rjs 	SCTP_INP_INFO_RUNLOCK();
   4733  1.1      rjs }
   4734  1.1      rjs 
   4735  1.1      rjs int
   4736  1.1      rjs sctp_add_to_socket_q(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   4737  1.1      rjs {
   4738  1.1      rjs 	struct sctp_socket_q_list *sq;
   4739  1.1      rjs 
   4740  1.1      rjs 	/* write lock on INP assumed */
   4741  1.1      rjs 	if ((inp == NULL) || (stcb == NULL)) {
   4742  1.1      rjs 		/* I am paranoid */
   4743  1.1      rjs 		return (0);
   4744  1.1      rjs 	}
   4745  1.1      rjs 	sq = (struct sctp_socket_q_list *)SCTP_ZONE_GET(
   4746  1.1      rjs 	    sctppcbinfo.ipi_zone_sockq);
   4747  1.1      rjs 	if (sq == NULL) {
   4748  1.1      rjs 		/* out of sq structs */
   4749  1.1      rjs 		return (0);
   4750  1.1      rjs 	}
   4751  1.1      rjs 	sctppcbinfo.ipi_count_sockq++;
   4752  1.1      rjs 	sctppcbinfo.ipi_gencnt_sockq++;
   4753  1.1      rjs 	if (stcb)
   4754  1.1      rjs 		stcb->asoc.cnt_msg_on_sb++;
   4755  1.1      rjs 	sq->tcb = stcb;
   4756  1.1      rjs 	TAILQ_INSERT_TAIL(&inp->sctp_queue_list, sq, next_sq);
   4757  1.1      rjs 	return (1);
   4758  1.1      rjs }
   4759  1.1      rjs 
   4760  1.1      rjs 
   4761  1.1      rjs struct sctp_tcb *
   4762  1.1      rjs sctp_remove_from_socket_q(struct sctp_inpcb *inp)
   4763  1.1      rjs {
   4764  1.1      rjs 	struct sctp_tcb *stcb = NULL;
   4765  1.1      rjs 	struct sctp_socket_q_list *sq;
   4766  1.1      rjs 
   4767  1.1      rjs 	/* W-Lock on INP assumed held */
   4768  1.1      rjs 	sq = TAILQ_FIRST(&inp->sctp_queue_list);
   4769  1.1      rjs 	if (sq == NULL)
   4770  1.1      rjs 		return (NULL);
   4771  1.1      rjs 
   4772  1.1      rjs 	stcb = sq->tcb;
   4773  1.1      rjs 	TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
   4774  1.1      rjs 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
   4775  1.1      rjs 	sctppcbinfo.ipi_count_sockq--;
   4776  1.1      rjs 	sctppcbinfo.ipi_gencnt_sockq++;
   4777  1.1      rjs 	if (stcb) {
   4778  1.1      rjs 		stcb->asoc.cnt_msg_on_sb--;
   4779  1.1      rjs 	}
   4780  1.1      rjs 	return (stcb);
   4781  1.1      rjs }
   4782  1.1      rjs 
   4783  1.1      rjs int
   4784  1.1      rjs sctp_initiate_iterator(asoc_func af, uint32_t pcb_state, uint32_t asoc_state,
   4785  1.1      rjs 		       void *argp, uint32_t argi, end_func ef,
   4786  1.1      rjs 		       struct sctp_inpcb *s_inp)
   4787  1.1      rjs {
   4788  1.1      rjs 	struct sctp_iterator *it=NULL;
   4789  1.1      rjs 	int s;
   4790  1.1      rjs 	if (af == NULL) {
   4791  1.1      rjs 		return (-1);
   4792  1.1      rjs 	}
   4793  1.1      rjs 	it = malloc(sizeof(struct sctp_iterator), M_PCB, M_WAITOK);
   4794  1.1      rjs 	if (it == NULL) {
   4795  1.1      rjs 		return (ENOMEM);
   4796  1.1      rjs 	}
   4797  1.1      rjs 	memset(it, 0, sizeof(*it));
   4798  1.1      rjs 	it->function_toapply = af;
   4799  1.1      rjs 	it->function_atend = ef;
   4800  1.1      rjs 	it->pointer = argp;
   4801  1.1      rjs 	it->val = argi;
   4802  1.1      rjs 	it->pcb_flags = pcb_state;
   4803  1.1      rjs 	it->asoc_state = asoc_state;
   4804  1.1      rjs 	if (s_inp) {
   4805  1.1      rjs 		it->inp = s_inp;
   4806  1.1      rjs 		it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
   4807  1.1      rjs 	} else {
   4808  1.1      rjs 		SCTP_INP_INFO_RLOCK();
   4809  1.1      rjs 		it->inp = LIST_FIRST(&sctppcbinfo.listhead);
   4810  1.1      rjs 		SCTP_INP_INFO_RUNLOCK();
   4811  1.1      rjs 		it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
   4812  1.1      rjs 
   4813  1.1      rjs 	}
   4814  1.1      rjs 	/* Init the timer */
   4815  1.1      rjs 	callout_init(&it->tmr.timer, 0);
   4816  1.1      rjs 	/* add to the list of all iterators */
   4817  1.1      rjs 	SCTP_INP_INFO_WLOCK();
   4818  1.1      rjs 	LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
   4819  1.1      rjs 	SCTP_INP_INFO_WUNLOCK();
   4820  1.1      rjs 	s = splsoftnet();
   4821  1.1      rjs 	sctp_iterator_timer(it);
   4822  1.1      rjs 	splx(s);
   4823  1.1      rjs 	return (0);
   4824  1.1      rjs }
   4825  1.1      rjs 
   4826  1.1      rjs 
   4827