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