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