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