xref: /freebsd/sys/netinet/sctp_pcb.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $	 */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <sys/proc.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_pcb.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp.h>
45 #include <netinet/sctp_header.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_output.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_dtrace_define.h>
51 #include <netinet/udp.h>
52 #ifdef INET6
53 #include <netinet6/ip6_var.h>
54 #endif
55 #include <sys/sched.h>
56 #include <sys/smp.h>
57 #include <sys/unistd.h>
58 
59 
60 VNET_DEFINE(struct sctp_base_info, system_base_info);
61 
62 /* FIX: we don't handle multiple link local scopes */
63 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
64 #ifdef INET6
65 int
66 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
67 {
68 	struct sockaddr_in6 tmp_a, tmp_b;
69 
70 	memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
71 	if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
72 		return 0;
73 	}
74 	memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
75 	if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
76 		return 0;
77 	}
78 	return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
79 }
80 
81 #endif
82 
83 void
84 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
85 {
86 	/*
87 	 * We really don't need to lock this, but I will just because it
88 	 * does not hurt.
89 	 */
90 	SCTP_INP_INFO_RLOCK();
91 	spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
92 	spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
93 	spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
94 	spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
95 	spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
96 	spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
97 	spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
98 	spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
99 
100 	SCTP_INP_INFO_RUNLOCK();
101 }
102 
103 /*
104  * Addresses are added to VRF's (Virtual Router's). For BSD we
105  * have only the default VRF 0. We maintain a hash list of
106  * VRF's. Each VRF has its own list of sctp_ifn's. Each of
107  * these has a list of addresses. When we add a new address
108  * to a VRF we lookup the ifn/ifn_index, if the ifn does
109  * not exist we create it and add it to the list of IFN's
110  * within the VRF. Once we have the sctp_ifn, we add the
111  * address to the list. So we look something like:
112  *
113  * hash-vrf-table
114  *   vrf-> ifn-> ifn -> ifn
115  *   vrf    |
116  *    ...   +--ifa-> ifa -> ifa
117  *   vrf
118  *
119  * We keep these separate lists since the SCTP subsystem will
120  * point to these from its source address selection nets structure.
121  * When an address is deleted it does not happen right away on
122  * the SCTP side, it gets scheduled. What we do when a
123  * delete happens is immediately remove the address from
124  * the master list and decrement the refcount. As our
125  * addip iterator works through and frees the src address
126  * selection pointing to the sctp_ifa, eventually the refcount
127  * will reach 0 and we will delete it. Note that it is assumed
128  * that any locking on system level ifn/ifa is done at the
129  * caller of these functions and these routines will only
130  * lock the SCTP structures as they add or delete things.
131  *
132  * Other notes on VRF concepts.
133  *  - An endpoint can be in multiple VRF's
134  *  - An association lives within a VRF and only one VRF.
135  *  - Any incoming packet we can deduce the VRF for by
136  *    looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
137  *  - Any downward send call or connect call must supply the
138  *    VRF via ancillary data or via some sort of set default
139  *    VRF socket option call (again for BSD no brainer since
140  *    the VRF is always 0).
141  *  - An endpoint may add multiple VRF's to it.
142  *  - Listening sockets can accept associations in any
143  *    of the VRF's they are in but the assoc will end up
144  *    in only one VRF (gotten from the packet or connect/send).
145  *
146  */
147 
148 struct sctp_vrf *
149 sctp_allocate_vrf(int vrf_id)
150 {
151 	struct sctp_vrf *vrf = NULL;
152 	struct sctp_vrflist *bucket;
153 
154 	/* First allocate the VRF structure */
155 	vrf = sctp_find_vrf(vrf_id);
156 	if (vrf) {
157 		/* Already allocated */
158 		return (vrf);
159 	}
160 	SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
161 	    SCTP_M_VRF);
162 	if (vrf == NULL) {
163 		/* No memory */
164 #ifdef INVARIANTS
165 		panic("No memory for VRF:%d", vrf_id);
166 #endif
167 		return (NULL);
168 	}
169 	/* setup the VRF */
170 	memset(vrf, 0, sizeof(struct sctp_vrf));
171 	vrf->vrf_id = vrf_id;
172 	LIST_INIT(&vrf->ifnlist);
173 	vrf->total_ifa_count = 0;
174 	vrf->refcount = 0;
175 	/* now also setup table ids */
176 	SCTP_INIT_VRF_TABLEID(vrf);
177 	/* Init the HASH of addresses */
178 	vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
179 	    &vrf->vrf_addr_hashmark);
180 	if (vrf->vrf_addr_hash == NULL) {
181 		/* No memory */
182 #ifdef INVARIANTS
183 		panic("No memory for VRF:%d", vrf_id);
184 #endif
185 		SCTP_FREE(vrf, SCTP_M_VRF);
186 		return (NULL);
187 	}
188 	/* Add it to the hash table */
189 	bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
190 	LIST_INSERT_HEAD(bucket, vrf, next_vrf);
191 	atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
192 	return (vrf);
193 }
194 
195 
196 struct sctp_ifn *
197 sctp_find_ifn(void *ifn, uint32_t ifn_index)
198 {
199 	struct sctp_ifn *sctp_ifnp;
200 	struct sctp_ifnlist *hash_ifn_head;
201 
202 	/*
203 	 * We assume the lock is held for the addresses if that's wrong
204 	 * problems could occur :-)
205 	 */
206 	hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
207 	LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
208 		if (sctp_ifnp->ifn_index == ifn_index) {
209 			return (sctp_ifnp);
210 		}
211 		if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
212 			return (sctp_ifnp);
213 		}
214 	}
215 	return (NULL);
216 }
217 
218 
219 
220 struct sctp_vrf *
221 sctp_find_vrf(uint32_t vrf_id)
222 {
223 	struct sctp_vrflist *bucket;
224 	struct sctp_vrf *liste;
225 
226 	bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
227 	LIST_FOREACH(liste, bucket, next_vrf) {
228 		if (vrf_id == liste->vrf_id) {
229 			return (liste);
230 		}
231 	}
232 	return (NULL);
233 }
234 
235 void
236 sctp_free_vrf(struct sctp_vrf *vrf)
237 {
238 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
239 		if (vrf->vrf_addr_hash) {
240 			SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
241 			vrf->vrf_addr_hash = NULL;
242 		}
243 		/* We zero'd the count */
244 		LIST_REMOVE(vrf, next_vrf);
245 		SCTP_FREE(vrf, SCTP_M_VRF);
246 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
247 	}
248 }
249 
250 void
251 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
252 {
253 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
254 		/* We zero'd the count */
255 		if (sctp_ifnp->vrf) {
256 			sctp_free_vrf(sctp_ifnp->vrf);
257 		}
258 		SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
259 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
260 	}
261 }
262 
263 void
264 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
265 {
266 	struct sctp_ifn *sctp_ifnp;
267 
268 	sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
269 	if (sctp_ifnp != NULL) {
270 		sctp_ifnp->ifn_mtu = mtu;
271 	}
272 }
273 
274 
275 void
276 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
277 {
278 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
279 		/* We zero'd the count */
280 		if (sctp_ifap->ifn_p) {
281 			sctp_free_ifn(sctp_ifap->ifn_p);
282 		}
283 		SCTP_FREE(sctp_ifap, SCTP_M_IFA);
284 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
285 	}
286 }
287 
288 static void
289 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
290 {
291 	struct sctp_ifn *found;
292 
293 	found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
294 	if (found == NULL) {
295 		/* Not in the list.. sorry */
296 		return;
297 	}
298 	if (hold_addr_lock == 0)
299 		SCTP_IPI_ADDR_WLOCK();
300 	LIST_REMOVE(sctp_ifnp, next_bucket);
301 	LIST_REMOVE(sctp_ifnp, next_ifn);
302 	SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
303 	    sctp_ifnp->registered_af);
304 	if (hold_addr_lock == 0)
305 		SCTP_IPI_ADDR_WUNLOCK();
306 	/* Take away the reference, and possibly free it */
307 	sctp_free_ifn(sctp_ifnp);
308 }
309 
310 void
311 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
312     const char *if_name, uint32_t ifn_index)
313 {
314 	struct sctp_vrf *vrf;
315 	struct sctp_ifa *sctp_ifap = NULL;
316 
317 	SCTP_IPI_ADDR_RLOCK();
318 	vrf = sctp_find_vrf(vrf_id);
319 	if (vrf == NULL) {
320 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
321 		goto out;
322 
323 	}
324 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
325 	if (sctp_ifap == NULL) {
326 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
327 		goto out;
328 	}
329 	if (sctp_ifap->ifn_p == NULL) {
330 		SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
331 		goto out;
332 	}
333 	if (if_name) {
334 		if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
335 			SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
336 			    sctp_ifap->ifn_p->ifn_name, if_name);
337 			goto out;
338 		}
339 	} else {
340 		if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
341 			SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
342 			    sctp_ifap->ifn_p->ifn_index, ifn_index);
343 			goto out;
344 		}
345 	}
346 
347 	sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
348 	sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
349 out:
350 	SCTP_IPI_ADDR_RUNLOCK();
351 }
352 
353 void
354 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
355     const char *if_name, uint32_t ifn_index)
356 {
357 	struct sctp_vrf *vrf;
358 	struct sctp_ifa *sctp_ifap = NULL;
359 
360 	SCTP_IPI_ADDR_RLOCK();
361 	vrf = sctp_find_vrf(vrf_id);
362 	if (vrf == NULL) {
363 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
364 		goto out;
365 
366 	}
367 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
368 	if (sctp_ifap == NULL) {
369 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
370 		goto out;
371 	}
372 	if (sctp_ifap->ifn_p == NULL) {
373 		SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
374 		goto out;
375 	}
376 	if (if_name) {
377 		if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
378 			SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
379 			    sctp_ifap->ifn_p->ifn_name, if_name);
380 			goto out;
381 		}
382 	} else {
383 		if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
384 			SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
385 			    sctp_ifap->ifn_p->ifn_index, ifn_index);
386 			goto out;
387 		}
388 	}
389 
390 	sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
391 	sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
392 out:
393 	SCTP_IPI_ADDR_RUNLOCK();
394 }
395 
396 /*-
397  * Add an ifa to an ifn.
398  * Register the interface as necessary.
399  * NOTE: ADDR write lock MUST be held.
400  */
401 static void
402 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
403 {
404 	int ifa_af;
405 
406 	LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
407 	sctp_ifap->ifn_p = sctp_ifnp;
408 	atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
409 	/* update address counts */
410 	sctp_ifnp->ifa_count++;
411 	ifa_af = sctp_ifap->address.sa.sa_family;
412 	switch (ifa_af) {
413 #ifdef INET
414 	case AF_INET:
415 		sctp_ifnp->num_v4++;
416 		break;
417 #endif
418 #ifdef INET6
419 	case AF_INET6:
420 		sctp_ifnp->num_v6++;
421 		break;
422 #endif
423 	default:
424 		break;
425 	}
426 	if (sctp_ifnp->ifa_count == 1) {
427 		/* register the new interface */
428 		SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
429 		sctp_ifnp->registered_af = ifa_af;
430 	}
431 }
432 
433 /*-
434  * Remove an ifa from its ifn.
435  * If no more addresses exist, remove the ifn too. Otherwise, re-register
436  * the interface based on the remaining address families left.
437  * NOTE: ADDR write lock MUST be held.
438  */
439 static void
440 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
441 {
442 	uint32_t ifn_index;
443 
444 	LIST_REMOVE(sctp_ifap, next_ifa);
445 	if (sctp_ifap->ifn_p) {
446 		/* update address counts */
447 		sctp_ifap->ifn_p->ifa_count--;
448 		switch (sctp_ifap->address.sa.sa_family) {
449 #ifdef INET
450 		case AF_INET:
451 			sctp_ifap->ifn_p->num_v4--;
452 			break;
453 #endif
454 #ifdef INET6
455 		case AF_INET6:
456 			sctp_ifap->ifn_p->num_v6--;
457 			break;
458 #endif
459 		default:
460 			break;
461 		}
462 
463 		ifn_index = sctp_ifap->ifn_p->ifn_index;
464 		if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
465 			/* remove the ifn, possibly freeing it */
466 			sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
467 		} else {
468 			/* re-register address family type, if needed */
469 			if ((sctp_ifap->ifn_p->num_v6 == 0) &&
470 			    (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
471 				SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET6);
472 				SCTP_REGISTER_INTERFACE(ifn_index, AF_INET);
473 				sctp_ifap->ifn_p->registered_af = AF_INET;
474 			} else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
475 			    (sctp_ifap->ifn_p->registered_af == AF_INET)) {
476 				SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET);
477 				SCTP_REGISTER_INTERFACE(ifn_index, AF_INET6);
478 				sctp_ifap->ifn_p->registered_af = AF_INET6;
479 			}
480 			/* free the ifn refcount */
481 			sctp_free_ifn(sctp_ifap->ifn_p);
482 		}
483 		sctp_ifap->ifn_p = NULL;
484 	}
485 }
486 
487 struct sctp_ifa *
488 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
489     uint32_t ifn_type, const char *if_name, void *ifa,
490     struct sockaddr *addr, uint32_t ifa_flags,
491     int dynamic_add)
492 {
493 	struct sctp_vrf *vrf;
494 	struct sctp_ifn *sctp_ifnp = NULL;
495 	struct sctp_ifa *sctp_ifap = NULL;
496 	struct sctp_ifalist *hash_addr_head;
497 	struct sctp_ifnlist *hash_ifn_head;
498 	uint32_t hash_of_addr;
499 	int new_ifn_af = 0;
500 
501 #ifdef SCTP_DEBUG
502 	SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
503 	SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
504 #endif
505 	SCTP_IPI_ADDR_WLOCK();
506 	sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
507 	if (sctp_ifnp) {
508 		vrf = sctp_ifnp->vrf;
509 	} else {
510 		vrf = sctp_find_vrf(vrf_id);
511 		if (vrf == NULL) {
512 			vrf = sctp_allocate_vrf(vrf_id);
513 			if (vrf == NULL) {
514 				SCTP_IPI_ADDR_WUNLOCK();
515 				return (NULL);
516 			}
517 		}
518 	}
519 	if (sctp_ifnp == NULL) {
520 		/*
521 		 * build one and add it, can't hold lock until after malloc
522 		 * done though.
523 		 */
524 		SCTP_IPI_ADDR_WUNLOCK();
525 		SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
526 		    sizeof(struct sctp_ifn), SCTP_M_IFN);
527 		if (sctp_ifnp == NULL) {
528 #ifdef INVARIANTS
529 			panic("No memory for IFN");
530 #endif
531 			return (NULL);
532 		}
533 		memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
534 		sctp_ifnp->ifn_index = ifn_index;
535 		sctp_ifnp->ifn_p = ifn;
536 		sctp_ifnp->ifn_type = ifn_type;
537 		sctp_ifnp->refcount = 0;
538 		sctp_ifnp->vrf = vrf;
539 		atomic_add_int(&vrf->refcount, 1);
540 		sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
541 		if (if_name != NULL) {
542 			snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
543 		} else {
544 			snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
545 		}
546 		hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
547 		LIST_INIT(&sctp_ifnp->ifalist);
548 		SCTP_IPI_ADDR_WLOCK();
549 		LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
550 		LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
551 		atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
552 		new_ifn_af = 1;
553 	}
554 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
555 	if (sctp_ifap) {
556 		/* Hmm, it already exists? */
557 		if ((sctp_ifap->ifn_p) &&
558 		    (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
559 			SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
560 			    sctp_ifap->ifn_p->ifn_name, ifn_index,
561 			    sctp_ifap);
562 			if (new_ifn_af) {
563 				/* Remove the created one that we don't want */
564 				sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
565 			}
566 			if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
567 				/* easy to solve, just switch back to active */
568 				SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
569 				sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
570 				sctp_ifap->ifn_p = sctp_ifnp;
571 				atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
572 			}
573 	exit_stage_left:
574 			SCTP_IPI_ADDR_WUNLOCK();
575 			return (sctp_ifap);
576 		} else {
577 			if (sctp_ifap->ifn_p) {
578 				/*
579 				 * The last IFN gets the address, remove the
580 				 * old one
581 				 */
582 				SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
583 				    sctp_ifap, sctp_ifap->ifn_p->ifn_name,
584 				    sctp_ifap->ifn_p->ifn_index, if_name,
585 				    ifn_index);
586 				/* remove the address from the old ifn */
587 				sctp_remove_ifa_from_ifn(sctp_ifap);
588 				/* move the address over to the new ifn */
589 				sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
590 				goto exit_stage_left;
591 			} else {
592 				/* repair ifnp which was NULL ? */
593 				sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
594 				SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
595 				    sctp_ifnp, sctp_ifap);
596 				sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
597 			}
598 			goto exit_stage_left;
599 		}
600 	}
601 	SCTP_IPI_ADDR_WUNLOCK();
602 	SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
603 	if (sctp_ifap == NULL) {
604 #ifdef INVARIANTS
605 		panic("No memory for IFA");
606 #endif
607 		return (NULL);
608 	}
609 	memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
610 	sctp_ifap->ifn_p = sctp_ifnp;
611 	atomic_add_int(&sctp_ifnp->refcount, 1);
612 	sctp_ifap->vrf_id = vrf_id;
613 	sctp_ifap->ifa = ifa;
614 	memcpy(&sctp_ifap->address, addr, addr->sa_len);
615 	sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
616 	sctp_ifap->flags = ifa_flags;
617 	/* Set scope */
618 	switch (sctp_ifap->address.sa.sa_family) {
619 #ifdef INET
620 	case AF_INET:
621 		{
622 			struct sockaddr_in *sin;
623 
624 			sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
625 			if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
626 			    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
627 				sctp_ifap->src_is_loop = 1;
628 			}
629 			if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
630 				sctp_ifap->src_is_priv = 1;
631 			}
632 			sctp_ifnp->num_v4++;
633 			if (new_ifn_af)
634 				new_ifn_af = AF_INET;
635 			break;
636 		}
637 #endif
638 #ifdef INET6
639 	case AF_INET6:
640 		{
641 			/* ok to use deprecated addresses? */
642 			struct sockaddr_in6 *sin6;
643 
644 			sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
645 			if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
646 			    (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
647 				sctp_ifap->src_is_loop = 1;
648 			}
649 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
650 				sctp_ifap->src_is_priv = 1;
651 			}
652 			sctp_ifnp->num_v6++;
653 			if (new_ifn_af)
654 				new_ifn_af = AF_INET6;
655 			break;
656 		}
657 #endif
658 	default:
659 		new_ifn_af = 0;
660 		break;
661 	}
662 	hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
663 
664 	if ((sctp_ifap->src_is_priv == 0) &&
665 	    (sctp_ifap->src_is_loop == 0)) {
666 		sctp_ifap->src_is_glob = 1;
667 	}
668 	SCTP_IPI_ADDR_WLOCK();
669 	hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
670 	LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
671 	sctp_ifap->refcount = 1;
672 	LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
673 	sctp_ifnp->ifa_count++;
674 	vrf->total_ifa_count++;
675 	atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
676 	if (new_ifn_af) {
677 		SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
678 		sctp_ifnp->registered_af = new_ifn_af;
679 	}
680 	SCTP_IPI_ADDR_WUNLOCK();
681 	if (dynamic_add) {
682 		/*
683 		 * Bump up the refcount so that when the timer completes it
684 		 * will drop back down.
685 		 */
686 		struct sctp_laddr *wi;
687 
688 		atomic_add_int(&sctp_ifap->refcount, 1);
689 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
690 		if (wi == NULL) {
691 			/*
692 			 * Gak, what can we do? We have lost an address
693 			 * change can you say HOSED?
694 			 */
695 			SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
696 			/* Opps, must decrement the count */
697 			sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
698 			    if_name);
699 			return (NULL);
700 		}
701 		SCTP_INCR_LADDR_COUNT();
702 		bzero(wi, sizeof(*wi));
703 		(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
704 		wi->ifa = sctp_ifap;
705 		wi->action = SCTP_ADD_IP_ADDRESS;
706 
707 		SCTP_WQ_ADDR_LOCK();
708 		LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
709 		SCTP_WQ_ADDR_UNLOCK();
710 
711 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
712 		    (struct sctp_inpcb *)NULL,
713 		    (struct sctp_tcb *)NULL,
714 		    (struct sctp_nets *)NULL);
715 	} else {
716 		/* it's ready for use */
717 		sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
718 	}
719 	return (sctp_ifap);
720 }
721 
722 void
723 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
724     uint32_t ifn_index, const char *if_name)
725 {
726 	struct sctp_vrf *vrf;
727 	struct sctp_ifa *sctp_ifap = NULL;
728 
729 	SCTP_IPI_ADDR_WLOCK();
730 	vrf = sctp_find_vrf(vrf_id);
731 	if (vrf == NULL) {
732 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
733 		goto out_now;
734 	}
735 #ifdef SCTP_DEBUG
736 	SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
737 	SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
738 #endif
739 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
740 	if (sctp_ifap) {
741 		/* Validate the delete */
742 		if (sctp_ifap->ifn_p) {
743 			int valid = 0;
744 
745 			/*-
746 			 * The name has priority over the ifn_index
747 			 * if its given. We do this especially for
748 			 * panda who might recycle indexes fast.
749 			 */
750 			if (if_name) {
751 				if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
752 					/* They match its a correct delete */
753 					valid = 1;
754 				}
755 			}
756 			if (!valid) {
757 				/* last ditch check ifn_index */
758 				if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
759 					valid = 1;
760 				}
761 			}
762 			if (!valid) {
763 				SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
764 				    ifn_index, ((if_name == NULL) ? "NULL" : if_name));
765 				SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
766 				    sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
767 				SCTP_IPI_ADDR_WUNLOCK();
768 				return;
769 			}
770 		}
771 		SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap);
772 		sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
773 		sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
774 		vrf->total_ifa_count--;
775 		LIST_REMOVE(sctp_ifap, next_bucket);
776 		sctp_remove_ifa_from_ifn(sctp_ifap);
777 	}
778 #ifdef SCTP_DEBUG
779 	else {
780 		SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
781 		    ifn_index);
782 		SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
783 	}
784 #endif
785 
786 out_now:
787 	SCTP_IPI_ADDR_WUNLOCK();
788 	if (sctp_ifap) {
789 		struct sctp_laddr *wi;
790 
791 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
792 		if (wi == NULL) {
793 			/*
794 			 * Gak, what can we do? We have lost an address
795 			 * change can you say HOSED?
796 			 */
797 			SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
798 
799 			/* Oops, must decrement the count */
800 			sctp_free_ifa(sctp_ifap);
801 			return;
802 		}
803 		SCTP_INCR_LADDR_COUNT();
804 		bzero(wi, sizeof(*wi));
805 		(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
806 		wi->ifa = sctp_ifap;
807 		wi->action = SCTP_DEL_IP_ADDRESS;
808 		SCTP_WQ_ADDR_LOCK();
809 		/*
810 		 * Should this really be a tailq? As it is we will process
811 		 * the newest first :-0
812 		 */
813 		LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
814 		SCTP_WQ_ADDR_UNLOCK();
815 
816 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
817 		    (struct sctp_inpcb *)NULL,
818 		    (struct sctp_tcb *)NULL,
819 		    (struct sctp_nets *)NULL);
820 	}
821 	return;
822 }
823 
824 
825 static struct sctp_tcb *
826 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
827     struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
828 {
829 	/**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
830 	/*
831 	 * If we support the TCP model, then we must now dig through to see
832 	 * if we can find our endpoint in the list of tcp ep's.
833 	 */
834 	uint16_t lport, rport;
835 	struct sctppcbhead *ephead;
836 	struct sctp_inpcb *inp;
837 	struct sctp_laddr *laddr;
838 	struct sctp_tcb *stcb;
839 	struct sctp_nets *net;
840 
841 	if ((to == NULL) || (from == NULL)) {
842 		return (NULL);
843 	}
844 	switch (to->sa_family) {
845 #ifdef INET
846 	case AF_INET:
847 		if (from->sa_family == AF_INET) {
848 			lport = ((struct sockaddr_in *)to)->sin_port;
849 			rport = ((struct sockaddr_in *)from)->sin_port;
850 		} else {
851 			return (NULL);
852 		}
853 		break;
854 #endif
855 #ifdef INET6
856 	case AF_INET6:
857 		if (from->sa_family == AF_INET6) {
858 			lport = ((struct sockaddr_in6 *)to)->sin6_port;
859 			rport = ((struct sockaddr_in6 *)from)->sin6_port;
860 		} else {
861 			return (NULL);
862 		}
863 		break;
864 #endif
865 	default:
866 		return (NULL);
867 	}
868 	ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
869 	/*
870 	 * Ok now for each of the guys in this bucket we must look and see:
871 	 * - Does the remote port match. - Does there single association's
872 	 * addresses match this address (to). If so we update p_ep to point
873 	 * to this ep and return the tcb from it.
874 	 */
875 	LIST_FOREACH(inp, ephead, sctp_hash) {
876 		SCTP_INP_RLOCK(inp);
877 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
878 			SCTP_INP_RUNLOCK(inp);
879 			continue;
880 		}
881 		if (lport != inp->sctp_lport) {
882 			SCTP_INP_RUNLOCK(inp);
883 			continue;
884 		}
885 		if (inp->def_vrf_id != vrf_id) {
886 			SCTP_INP_RUNLOCK(inp);
887 			continue;
888 		}
889 		/* check to see if the ep has one of the addresses */
890 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
891 			/* We are NOT bound all, so look further */
892 			int match = 0;
893 
894 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
895 
896 				if (laddr->ifa == NULL) {
897 					SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
898 					continue;
899 				}
900 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
901 					SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
902 					continue;
903 				}
904 				if (laddr->ifa->address.sa.sa_family ==
905 				    to->sa_family) {
906 					/* see if it matches */
907 
908 #ifdef INET
909 					if (from->sa_family == AF_INET) {
910 						struct sockaddr_in *intf_addr,
911 						           *sin;
912 
913 						intf_addr = &laddr->ifa->address.sin;
914 						sin = (struct sockaddr_in *)to;
915 						if (sin->sin_addr.s_addr ==
916 						    intf_addr->sin_addr.s_addr) {
917 							match = 1;
918 							break;
919 						}
920 					}
921 #endif
922 #ifdef INET6
923 					if (from->sa_family == AF_INET6) {
924 						struct sockaddr_in6 *intf_addr6;
925 						struct sockaddr_in6 *sin6;
926 
927 						sin6 = (struct sockaddr_in6 *)
928 						    to;
929 						intf_addr6 = &laddr->ifa->address.sin6;
930 
931 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
932 						    intf_addr6)) {
933 							match = 1;
934 							break;
935 						}
936 					}
937 #endif
938 				}
939 			}
940 			if (match == 0) {
941 				/* This endpoint does not have this address */
942 				SCTP_INP_RUNLOCK(inp);
943 				continue;
944 			}
945 		}
946 		/*
947 		 * Ok if we hit here the ep has the address, does it hold
948 		 * the tcb?
949 		 */
950 
951 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
952 		if (stcb == NULL) {
953 			SCTP_INP_RUNLOCK(inp);
954 			continue;
955 		}
956 		SCTP_TCB_LOCK(stcb);
957 		if (stcb->rport != rport) {
958 			/* remote port does not match. */
959 			SCTP_TCB_UNLOCK(stcb);
960 			SCTP_INP_RUNLOCK(inp);
961 			continue;
962 		}
963 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
964 			SCTP_TCB_UNLOCK(stcb);
965 			SCTP_INP_RUNLOCK(inp);
966 			continue;
967 		}
968 		/* Does this TCB have a matching address? */
969 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
970 
971 			if (net->ro._l_addr.sa.sa_family != from->sa_family) {
972 				/* not the same family, can't be a match */
973 				continue;
974 			}
975 			switch (from->sa_family) {
976 #ifdef INET
977 			case AF_INET:
978 				{
979 					struct sockaddr_in *sin, *rsin;
980 
981 					sin = (struct sockaddr_in *)&net->ro._l_addr;
982 					rsin = (struct sockaddr_in *)from;
983 					if (sin->sin_addr.s_addr ==
984 					    rsin->sin_addr.s_addr) {
985 						/* found it */
986 						if (netp != NULL) {
987 							*netp = net;
988 						}
989 						/*
990 						 * Update the endpoint
991 						 * pointer
992 						 */
993 						*inp_p = inp;
994 						SCTP_INP_RUNLOCK(inp);
995 						return (stcb);
996 					}
997 					break;
998 				}
999 #endif
1000 #ifdef INET6
1001 			case AF_INET6:
1002 				{
1003 					struct sockaddr_in6 *sin6, *rsin6;
1004 
1005 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1006 					rsin6 = (struct sockaddr_in6 *)from;
1007 					if (SCTP6_ARE_ADDR_EQUAL(sin6,
1008 					    rsin6)) {
1009 						/* found it */
1010 						if (netp != NULL) {
1011 							*netp = net;
1012 						}
1013 						/*
1014 						 * Update the endpoint
1015 						 * pointer
1016 						 */
1017 						*inp_p = inp;
1018 						SCTP_INP_RUNLOCK(inp);
1019 						return (stcb);
1020 					}
1021 					break;
1022 				}
1023 #endif
1024 			default:
1025 				/* TSNH */
1026 				break;
1027 			}
1028 		}
1029 		SCTP_TCB_UNLOCK(stcb);
1030 		SCTP_INP_RUNLOCK(inp);
1031 	}
1032 	return (NULL);
1033 }
1034 
1035 static int
1036 sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
1037 {
1038 	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1039 	int ipv4_addr_legal, ipv6_addr_legal;
1040 	struct sctp_vrf *vrf;
1041 	struct sctp_ifn *sctp_ifn;
1042 	struct sctp_ifa *sctp_ifa;
1043 
1044 	loopback_scope = stcb->asoc.loopback_scope;
1045 	ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1046 	local_scope = stcb->asoc.local_scope;
1047 	site_scope = stcb->asoc.site_scope;
1048 	ipv4_addr_legal = ipv6_addr_legal = 0;
1049 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1050 		ipv6_addr_legal = 1;
1051 		if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
1052 			ipv4_addr_legal = 1;
1053 		}
1054 	} else {
1055 		ipv4_addr_legal = 1;
1056 	}
1057 
1058 	SCTP_IPI_ADDR_RLOCK();
1059 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
1060 	if (vrf == NULL) {
1061 		/* no vrf, no addresses */
1062 		SCTP_IPI_ADDR_RUNLOCK();
1063 		return (0);
1064 	}
1065 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1066 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1067 			if ((loopback_scope == 0) &&
1068 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1069 				continue;
1070 			}
1071 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1072 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
1073 				    (!sctp_is_addr_pending(stcb, sctp_ifa))) {
1074 					/*
1075 					 * We allow pending addresses, where
1076 					 * we have sent an asconf-add to be
1077 					 * considered valid.
1078 					 */
1079 					continue;
1080 				}
1081 				switch (sctp_ifa->address.sa.sa_family) {
1082 #ifdef INET
1083 				case AF_INET:
1084 					if (ipv4_addr_legal) {
1085 						struct sockaddr_in *sin,
1086 						           *rsin;
1087 
1088 						sin = &sctp_ifa->address.sin;
1089 						rsin = (struct sockaddr_in *)to;
1090 						if ((ipv4_local_scope == 0) &&
1091 						    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1092 							continue;
1093 						}
1094 						if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1095 							SCTP_IPI_ADDR_RUNLOCK();
1096 							return (1);
1097 						}
1098 					}
1099 					break;
1100 #endif
1101 #ifdef INET6
1102 				case AF_INET6:
1103 					if (ipv6_addr_legal) {
1104 						struct sockaddr_in6 *sin6,
1105 						            *rsin6;
1106 
1107 						sin6 = &sctp_ifa->address.sin6;
1108 						rsin6 = (struct sockaddr_in6 *)to;
1109 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1110 							if (local_scope == 0)
1111 								continue;
1112 							if (sin6->sin6_scope_id == 0) {
1113 								if (sa6_recoverscope(sin6) != 0)
1114 									continue;
1115 							}
1116 						}
1117 						if ((site_scope == 0) &&
1118 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1119 							continue;
1120 						}
1121 						if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1122 							SCTP_IPI_ADDR_RUNLOCK();
1123 							return (1);
1124 						}
1125 					}
1126 					break;
1127 #endif
1128 				default:
1129 					/* TSNH */
1130 					break;
1131 				}
1132 			}
1133 		}
1134 	} else {
1135 		struct sctp_laddr *laddr;
1136 
1137 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1138 			if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
1139 			    (!sctp_is_addr_pending(stcb, laddr->ifa))) {
1140 				/*
1141 				 * We allow pending addresses, where we have
1142 				 * sent an asconf-add to be considered
1143 				 * valid.
1144 				 */
1145 				continue;
1146 			}
1147 			if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1148 				continue;
1149 			}
1150 			switch (to->sa_family) {
1151 #ifdef INET
1152 			case AF_INET:
1153 				{
1154 					struct sockaddr_in *sin, *rsin;
1155 
1156 					sin = (struct sockaddr_in *)&laddr->ifa->address.sin;
1157 					rsin = (struct sockaddr_in *)to;
1158 					if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1159 						SCTP_IPI_ADDR_RUNLOCK();
1160 						return (1);
1161 					}
1162 					break;
1163 				}
1164 #endif
1165 #ifdef INET6
1166 			case AF_INET6:
1167 				{
1168 					struct sockaddr_in6 *sin6, *rsin6;
1169 
1170 					sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1171 					rsin6 = (struct sockaddr_in6 *)to;
1172 					if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1173 						SCTP_IPI_ADDR_RUNLOCK();
1174 						return (1);
1175 					}
1176 					break;
1177 				}
1178 
1179 #endif
1180 			default:
1181 				/* TSNH */
1182 				break;
1183 			}
1184 
1185 		}
1186 	}
1187 	SCTP_IPI_ADDR_RUNLOCK();
1188 	return (0);
1189 }
1190 
1191 /*
1192  * rules for use
1193  *
1194  * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1195  * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1196  * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1197  * NULL.
1198  */
1199 
1200 struct sctp_tcb *
1201 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1202     struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1203 {
1204 	struct sctpasochead *head;
1205 	struct sctp_inpcb *inp;
1206 	struct sctp_tcb *stcb = NULL;
1207 	struct sctp_nets *net;
1208 	uint16_t rport;
1209 
1210 	inp = *inp_p;
1211 	if (remote->sa_family == AF_INET) {
1212 		rport = (((struct sockaddr_in *)remote)->sin_port);
1213 	} else if (remote->sa_family == AF_INET6) {
1214 		rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1215 	} else {
1216 		return (NULL);
1217 	}
1218 	if (locked_tcb) {
1219 		/*
1220 		 * UN-lock so we can do proper locking here this occurs when
1221 		 * called from load_addresses_from_init.
1222 		 */
1223 		atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1224 		SCTP_TCB_UNLOCK(locked_tcb);
1225 	}
1226 	SCTP_INP_INFO_RLOCK();
1227 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1228 		/*-
1229 		 * Now either this guy is our listener or it's the
1230 		 * connector. If it is the one that issued the connect, then
1231 		 * it's only chance is to be the first TCB in the list. If
1232 		 * it is the acceptor, then do the special_lookup to hash
1233 		 * and find the real inp.
1234 		 */
1235 		if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1236 			/* to is peer addr, from is my addr */
1237 			stcb = sctp_tcb_special_locate(inp_p, remote, local,
1238 			    netp, inp->def_vrf_id);
1239 			if ((stcb != NULL) && (locked_tcb == NULL)) {
1240 				/* we have a locked tcb, lower refcount */
1241 				SCTP_INP_DECR_REF(inp);
1242 			}
1243 			if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1244 				SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1245 				SCTP_TCB_LOCK(locked_tcb);
1246 				atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1247 				SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1248 			}
1249 			SCTP_INP_INFO_RUNLOCK();
1250 			return (stcb);
1251 		} else {
1252 			SCTP_INP_WLOCK(inp);
1253 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1254 				goto null_return;
1255 			}
1256 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1257 			if (stcb == NULL) {
1258 				goto null_return;
1259 			}
1260 			SCTP_TCB_LOCK(stcb);
1261 
1262 			if (stcb->rport != rport) {
1263 				/* remote port does not match. */
1264 				SCTP_TCB_UNLOCK(stcb);
1265 				goto null_return;
1266 			}
1267 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1268 				SCTP_TCB_UNLOCK(stcb);
1269 				goto null_return;
1270 			}
1271 			if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1272 				SCTP_TCB_UNLOCK(stcb);
1273 				goto null_return;
1274 			}
1275 			/* now look at the list of remote addresses */
1276 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1277 #ifdef INVARIANTS
1278 				if (net == (TAILQ_NEXT(net, sctp_next))) {
1279 					panic("Corrupt net list");
1280 				}
1281 #endif
1282 				if (net->ro._l_addr.sa.sa_family !=
1283 				    remote->sa_family) {
1284 					/* not the same family */
1285 					continue;
1286 				}
1287 				switch (remote->sa_family) {
1288 #ifdef INET
1289 				case AF_INET:
1290 					{
1291 						struct sockaddr_in *sin,
1292 						           *rsin;
1293 
1294 						sin = (struct sockaddr_in *)
1295 						    &net->ro._l_addr;
1296 						rsin = (struct sockaddr_in *)remote;
1297 						if (sin->sin_addr.s_addr ==
1298 						    rsin->sin_addr.s_addr) {
1299 							/* found it */
1300 							if (netp != NULL) {
1301 								*netp = net;
1302 							}
1303 							if (locked_tcb == NULL) {
1304 								SCTP_INP_DECR_REF(inp);
1305 							} else if (locked_tcb != stcb) {
1306 								SCTP_TCB_LOCK(locked_tcb);
1307 							}
1308 							if (locked_tcb) {
1309 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1310 							}
1311 							SCTP_INP_WUNLOCK(inp);
1312 							SCTP_INP_INFO_RUNLOCK();
1313 							return (stcb);
1314 						}
1315 						break;
1316 					}
1317 #endif
1318 #ifdef INET6
1319 				case AF_INET6:
1320 					{
1321 						struct sockaddr_in6 *sin6,
1322 						            *rsin6;
1323 
1324 						sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1325 						rsin6 = (struct sockaddr_in6 *)remote;
1326 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
1327 						    rsin6)) {
1328 							/* found it */
1329 							if (netp != NULL) {
1330 								*netp = net;
1331 							}
1332 							if (locked_tcb == NULL) {
1333 								SCTP_INP_DECR_REF(inp);
1334 							} else if (locked_tcb != stcb) {
1335 								SCTP_TCB_LOCK(locked_tcb);
1336 							}
1337 							if (locked_tcb) {
1338 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1339 							}
1340 							SCTP_INP_WUNLOCK(inp);
1341 							SCTP_INP_INFO_RUNLOCK();
1342 							return (stcb);
1343 						}
1344 						break;
1345 					}
1346 #endif
1347 				default:
1348 					/* TSNH */
1349 					break;
1350 				}
1351 			}
1352 			SCTP_TCB_UNLOCK(stcb);
1353 		}
1354 	} else {
1355 		SCTP_INP_WLOCK(inp);
1356 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1357 			goto null_return;
1358 		}
1359 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1360 		    inp->sctp_hashmark)];
1361 		if (head == NULL) {
1362 			goto null_return;
1363 		}
1364 		LIST_FOREACH(stcb, head, sctp_tcbhash) {
1365 			if (stcb->rport != rport) {
1366 				/* remote port does not match */
1367 				continue;
1368 			}
1369 			SCTP_TCB_LOCK(stcb);
1370 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1371 				SCTP_TCB_UNLOCK(stcb);
1372 				continue;
1373 			}
1374 			if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1375 				SCTP_TCB_UNLOCK(stcb);
1376 				continue;
1377 			}
1378 			/* now look at the list of remote addresses */
1379 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1380 #ifdef INVARIANTS
1381 				if (net == (TAILQ_NEXT(net, sctp_next))) {
1382 					panic("Corrupt net list");
1383 				}
1384 #endif
1385 				if (net->ro._l_addr.sa.sa_family !=
1386 				    remote->sa_family) {
1387 					/* not the same family */
1388 					continue;
1389 				}
1390 				switch (remote->sa_family) {
1391 #ifdef INET
1392 				case AF_INET:
1393 					{
1394 						struct sockaddr_in *sin,
1395 						           *rsin;
1396 
1397 						sin = (struct sockaddr_in *)
1398 						    &net->ro._l_addr;
1399 						rsin = (struct sockaddr_in *)remote;
1400 						if (sin->sin_addr.s_addr ==
1401 						    rsin->sin_addr.s_addr) {
1402 							/* found it */
1403 							if (netp != NULL) {
1404 								*netp = net;
1405 							}
1406 							if (locked_tcb == NULL) {
1407 								SCTP_INP_DECR_REF(inp);
1408 							} else if (locked_tcb != stcb) {
1409 								SCTP_TCB_LOCK(locked_tcb);
1410 							}
1411 							if (locked_tcb) {
1412 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1413 							}
1414 							SCTP_INP_WUNLOCK(inp);
1415 							SCTP_INP_INFO_RUNLOCK();
1416 							return (stcb);
1417 						}
1418 						break;
1419 					}
1420 #endif
1421 #ifdef INET6
1422 				case AF_INET6:
1423 					{
1424 						struct sockaddr_in6 *sin6,
1425 						            *rsin6;
1426 
1427 						sin6 = (struct sockaddr_in6 *)
1428 						    &net->ro._l_addr;
1429 						rsin6 = (struct sockaddr_in6 *)remote;
1430 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
1431 						    rsin6)) {
1432 							/* found it */
1433 							if (netp != NULL) {
1434 								*netp = net;
1435 							}
1436 							if (locked_tcb == NULL) {
1437 								SCTP_INP_DECR_REF(inp);
1438 							} else if (locked_tcb != stcb) {
1439 								SCTP_TCB_LOCK(locked_tcb);
1440 							}
1441 							if (locked_tcb) {
1442 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1443 							}
1444 							SCTP_INP_WUNLOCK(inp);
1445 							SCTP_INP_INFO_RUNLOCK();
1446 							return (stcb);
1447 						}
1448 						break;
1449 					}
1450 #endif
1451 				default:
1452 					/* TSNH */
1453 					break;
1454 				}
1455 			}
1456 			SCTP_TCB_UNLOCK(stcb);
1457 		}
1458 	}
1459 null_return:
1460 	/* clean up for returning null */
1461 	if (locked_tcb) {
1462 		SCTP_TCB_LOCK(locked_tcb);
1463 		atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1464 	}
1465 	SCTP_INP_WUNLOCK(inp);
1466 	SCTP_INP_INFO_RUNLOCK();
1467 	/* not found */
1468 	return (NULL);
1469 }
1470 
1471 /*
1472  * Find an association for a specific endpoint using the association id given
1473  * out in the COMM_UP notification
1474  */
1475 
1476 struct sctp_tcb *
1477 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1478 {
1479 	/*
1480 	 * Use my the assoc_id to find a endpoint
1481 	 */
1482 	struct sctpasochead *head;
1483 	struct sctp_tcb *stcb;
1484 	uint32_t id;
1485 
1486 	if (inp == NULL) {
1487 		SCTP_PRINTF("TSNH ep_associd\n");
1488 		return (NULL);
1489 	}
1490 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1491 		SCTP_PRINTF("TSNH ep_associd0\n");
1492 		return (NULL);
1493 	}
1494 	id = (uint32_t) asoc_id;
1495 	head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1496 	if (head == NULL) {
1497 		/* invalid id TSNH */
1498 		SCTP_PRINTF("TSNH ep_associd1\n");
1499 		return (NULL);
1500 	}
1501 	LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1502 		if (stcb->asoc.assoc_id == id) {
1503 			if (inp != stcb->sctp_ep) {
1504 				/*
1505 				 * some other guy has the same id active (id
1506 				 * collision ??).
1507 				 */
1508 				SCTP_PRINTF("TSNH ep_associd2\n");
1509 				continue;
1510 			}
1511 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1512 				continue;
1513 			}
1514 			if (want_lock) {
1515 				SCTP_TCB_LOCK(stcb);
1516 			}
1517 			return (stcb);
1518 		}
1519 	}
1520 	return (NULL);
1521 }
1522 
1523 
1524 struct sctp_tcb *
1525 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1526 {
1527 	struct sctp_tcb *stcb;
1528 
1529 	SCTP_INP_RLOCK(inp);
1530 	stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1531 	SCTP_INP_RUNLOCK(inp);
1532 	return (stcb);
1533 }
1534 
1535 
1536 static struct sctp_inpcb *
1537 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1538     uint16_t lport, uint32_t vrf_id)
1539 {
1540 	struct sctp_inpcb *inp;
1541 	struct sctp_laddr *laddr;
1542 
1543 #ifdef INET
1544 	struct sockaddr_in *sin;
1545 
1546 #endif
1547 #ifdef INET6
1548 	struct sockaddr_in6 *sin6;
1549 	struct sockaddr_in6 *intf_addr6;
1550 
1551 #endif
1552 
1553 	int fnd;
1554 
1555 	/*
1556 	 * Endpoint probe expects that the INP_INFO is locked.
1557 	 */
1558 #ifdef INET
1559 	sin = NULL;
1560 #endif
1561 #ifdef INET6
1562 	sin6 = NULL;
1563 #endif
1564 	switch (nam->sa_family) {
1565 #ifdef INET
1566 	case AF_INET:
1567 		sin = (struct sockaddr_in *)nam;
1568 		break;
1569 #endif
1570 #ifdef INET6
1571 	case AF_INET6:
1572 		sin6 = (struct sockaddr_in6 *)nam;
1573 		break;
1574 #endif
1575 	default:
1576 		/* unsupported family */
1577 		return (NULL);
1578 	}
1579 
1580 	if (head == NULL)
1581 		return (NULL);
1582 
1583 	LIST_FOREACH(inp, head, sctp_hash) {
1584 		SCTP_INP_RLOCK(inp);
1585 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1586 			SCTP_INP_RUNLOCK(inp);
1587 			continue;
1588 		}
1589 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1590 		    (inp->sctp_lport == lport)) {
1591 			/* got it */
1592 #ifdef INET
1593 			if ((nam->sa_family == AF_INET) &&
1594 			    (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1595 			    SCTP_IPV6_V6ONLY(inp)) {
1596 				/* IPv4 on a IPv6 socket with ONLY IPv6 set */
1597 				SCTP_INP_RUNLOCK(inp);
1598 				continue;
1599 			}
1600 #endif
1601 #ifdef INET6
1602 			/* A V6 address and the endpoint is NOT bound V6 */
1603 			if (nam->sa_family == AF_INET6 &&
1604 			    (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1605 				SCTP_INP_RUNLOCK(inp);
1606 				continue;
1607 			}
1608 #endif
1609 			/* does a VRF id match? */
1610 			fnd = 0;
1611 			if (inp->def_vrf_id == vrf_id)
1612 				fnd = 1;
1613 
1614 			SCTP_INP_RUNLOCK(inp);
1615 			if (!fnd)
1616 				continue;
1617 			return (inp);
1618 		}
1619 		SCTP_INP_RUNLOCK(inp);
1620 	}
1621 	switch (nam->sa_family) {
1622 #ifdef INET
1623 	case AF_INET:
1624 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1625 			/* Can't hunt for one that has no address specified */
1626 			return (NULL);
1627 		}
1628 		break;
1629 #endif
1630 #ifdef INET6
1631 	case AF_INET6:
1632 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1633 			/* Can't hunt for one that has no address specified */
1634 			return (NULL);
1635 		}
1636 		break;
1637 #endif
1638 	default:
1639 		break;
1640 	}
1641 	/*
1642 	 * ok, not bound to all so see if we can find a EP bound to this
1643 	 * address.
1644 	 */
1645 	LIST_FOREACH(inp, head, sctp_hash) {
1646 		SCTP_INP_RLOCK(inp);
1647 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1648 			SCTP_INP_RUNLOCK(inp);
1649 			continue;
1650 		}
1651 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1652 			SCTP_INP_RUNLOCK(inp);
1653 			continue;
1654 		}
1655 		/*
1656 		 * Ok this could be a likely candidate, look at all of its
1657 		 * addresses
1658 		 */
1659 		if (inp->sctp_lport != lport) {
1660 			SCTP_INP_RUNLOCK(inp);
1661 			continue;
1662 		}
1663 		/* does a VRF id match? */
1664 		fnd = 0;
1665 		if (inp->def_vrf_id == vrf_id)
1666 			fnd = 1;
1667 
1668 		if (!fnd) {
1669 			SCTP_INP_RUNLOCK(inp);
1670 			continue;
1671 		}
1672 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1673 			if (laddr->ifa == NULL) {
1674 				SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1675 				    __FUNCTION__);
1676 				continue;
1677 			}
1678 			SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1679 			    laddr->ifa);
1680 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1681 				SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1682 				continue;
1683 			}
1684 			if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1685 				/* possible, see if it matches */
1686 				switch (nam->sa_family) {
1687 #ifdef INET
1688 				case AF_INET:
1689 					if (sin->sin_addr.s_addr ==
1690 					    laddr->ifa->address.sin.sin_addr.s_addr) {
1691 						SCTP_INP_RUNLOCK(inp);
1692 						return (inp);
1693 					}
1694 					break;
1695 #endif
1696 #ifdef INET6
1697 				case AF_INET6:
1698 					intf_addr6 = &laddr->ifa->address.sin6;
1699 					if (SCTP6_ARE_ADDR_EQUAL(sin6,
1700 					    intf_addr6)) {
1701 						SCTP_INP_RUNLOCK(inp);
1702 						return (inp);
1703 					}
1704 					break;
1705 #endif
1706 				}
1707 			}
1708 		}
1709 		SCTP_INP_RUNLOCK(inp);
1710 	}
1711 	return (NULL);
1712 }
1713 
1714 
1715 static struct sctp_inpcb *
1716 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1717 {
1718 	struct sctppcbhead *head;
1719 	struct sctp_inpcb *t_inp;
1720 	int fnd;
1721 
1722 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1723 	    SCTP_BASE_INFO(hashmark))];
1724 	LIST_FOREACH(t_inp, head, sctp_hash) {
1725 		if (t_inp->sctp_lport != lport) {
1726 			continue;
1727 		}
1728 		/* is it in the VRF in question */
1729 		fnd = 0;
1730 		if (t_inp->def_vrf_id == vrf_id)
1731 			fnd = 1;
1732 		if (!fnd)
1733 			continue;
1734 
1735 		/* This one is in use. */
1736 		/* check the v6/v4 binding issue */
1737 		if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1738 		    SCTP_IPV6_V6ONLY(t_inp)) {
1739 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1740 				/* collision in V6 space */
1741 				return (t_inp);
1742 			} else {
1743 				/* inp is BOUND_V4 no conflict */
1744 				continue;
1745 			}
1746 		} else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1747 			/* t_inp is bound v4 and v6, conflict always */
1748 			return (t_inp);
1749 		} else {
1750 			/* t_inp is bound only V4 */
1751 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1752 			    SCTP_IPV6_V6ONLY(inp)) {
1753 				/* no conflict */
1754 				continue;
1755 			}
1756 			/* else fall through to conflict */
1757 		}
1758 		return (t_inp);
1759 	}
1760 	return (NULL);
1761 }
1762 
1763 
1764 int
1765 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
1766 {
1767 	/* For 1-2-1 with port reuse */
1768 	struct sctppcbhead *head;
1769 	struct sctp_inpcb *tinp;
1770 
1771 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
1772 		/* only works with port reuse on */
1773 		return (-1);
1774 	}
1775 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
1776 		return (0);
1777 	}
1778 	SCTP_INP_RUNLOCK(inp);
1779 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
1780 	    SCTP_BASE_INFO(hashmark))];
1781 	/* Kick out all non-listeners to the TCP hash */
1782 	LIST_FOREACH(tinp, head, sctp_hash) {
1783 		if (tinp->sctp_lport != inp->sctp_lport) {
1784 			continue;
1785 		}
1786 		if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1787 			continue;
1788 		}
1789 		if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1790 			continue;
1791 		}
1792 		if (tinp->sctp_socket->so_qlimit) {
1793 			continue;
1794 		}
1795 		SCTP_INP_WLOCK(tinp);
1796 		LIST_REMOVE(tinp, sctp_hash);
1797 		head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
1798 		tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
1799 		LIST_INSERT_HEAD(head, tinp, sctp_hash);
1800 		SCTP_INP_WUNLOCK(tinp);
1801 	}
1802 	SCTP_INP_WLOCK(inp);
1803 	/* Pull from where he was */
1804 	LIST_REMOVE(inp, sctp_hash);
1805 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
1806 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
1807 	LIST_INSERT_HEAD(head, inp, sctp_hash);
1808 	SCTP_INP_WUNLOCK(inp);
1809 	SCTP_INP_RLOCK(inp);
1810 	return (0);
1811 }
1812 
1813 
1814 struct sctp_inpcb *
1815 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1816     uint32_t vrf_id)
1817 {
1818 	/*
1819 	 * First we check the hash table to see if someone has this port
1820 	 * bound with just the port.
1821 	 */
1822 	struct sctp_inpcb *inp;
1823 	struct sctppcbhead *head;
1824 	int lport;
1825 	unsigned int i;
1826 
1827 #ifdef INET
1828 	struct sockaddr_in *sin;
1829 
1830 #endif
1831 #ifdef INET6
1832 	struct sockaddr_in6 *sin6;
1833 
1834 #endif
1835 
1836 	switch (nam->sa_family) {
1837 #ifdef INET
1838 	case AF_INET:
1839 		sin = (struct sockaddr_in *)nam;
1840 		lport = ((struct sockaddr_in *)nam)->sin_port;
1841 		break;
1842 #endif
1843 #ifdef INET6
1844 	case AF_INET6:
1845 		sin6 = (struct sockaddr_in6 *)nam;
1846 		lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1847 		break;
1848 #endif
1849 	default:
1850 		return (NULL);
1851 	}
1852 	/*
1853 	 * I could cheat here and just cast to one of the types but we will
1854 	 * do it right. It also provides the check against an Unsupported
1855 	 * type too.
1856 	 */
1857 	/* Find the head of the ALLADDR chain */
1858 	if (have_lock == 0) {
1859 		SCTP_INP_INFO_RLOCK();
1860 	}
1861 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1862 	    SCTP_BASE_INFO(hashmark))];
1863 	inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1864 
1865 	/*
1866 	 * If the TCP model exists it could be that the main listening
1867 	 * endpoint is gone but there still exists a connected socket for
1868 	 * this guy. If so we can return the first one that we find. This
1869 	 * may NOT be the correct one so the caller should be wary on the
1870 	 * returned INP. Currently the only caller that sets find_tcp_pool
1871 	 * is in bindx where we are verifying that a user CAN bind the
1872 	 * address. He either has bound it already, or someone else has, or
1873 	 * its open to bind, so this is good enough.
1874 	 */
1875 	if (inp == NULL && find_tcp_pool) {
1876 		for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
1877 			head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
1878 			inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1879 			if (inp) {
1880 				break;
1881 			}
1882 		}
1883 	}
1884 	if (inp) {
1885 		SCTP_INP_INCR_REF(inp);
1886 	}
1887 	if (have_lock == 0) {
1888 		SCTP_INP_INFO_RUNLOCK();
1889 	}
1890 	return (inp);
1891 }
1892 
1893 /*
1894  * Find an association for an endpoint with the pointer to whom you want to
1895  * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1896  * need to change the *to to some other struct like a mbuf...
1897  */
1898 struct sctp_tcb *
1899 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1900     struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1901     uint32_t vrf_id)
1902 {
1903 	struct sctp_inpcb *inp = NULL;
1904 	struct sctp_tcb *retval;
1905 
1906 	SCTP_INP_INFO_RLOCK();
1907 	if (find_tcp_pool) {
1908 		if (inp_p != NULL) {
1909 			retval = sctp_tcb_special_locate(inp_p, from, to, netp,
1910 			    vrf_id);
1911 		} else {
1912 			retval = sctp_tcb_special_locate(&inp, from, to, netp,
1913 			    vrf_id);
1914 		}
1915 		if (retval != NULL) {
1916 			SCTP_INP_INFO_RUNLOCK();
1917 			return (retval);
1918 		}
1919 	}
1920 	inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1921 	if (inp_p != NULL) {
1922 		*inp_p = inp;
1923 	}
1924 	SCTP_INP_INFO_RUNLOCK();
1925 
1926 	if (inp == NULL) {
1927 		return (NULL);
1928 	}
1929 	/*
1930 	 * ok, we have an endpoint, now lets find the assoc for it (if any)
1931 	 * we now place the source address or from in the to of the find
1932 	 * endpoint call. Since in reality this chain is used from the
1933 	 * inbound packet side.
1934 	 */
1935 	if (inp_p != NULL) {
1936 		retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1937 		    NULL);
1938 	} else {
1939 		retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
1940 		    NULL);
1941 	}
1942 	return retval;
1943 }
1944 
1945 
1946 /*
1947  * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1948  * find all addresses that the sender has specified in any address list. Each
1949  * address will be used to lookup the TCB and see if one exits.
1950  */
1951 static struct sctp_tcb *
1952 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1953     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1954     struct sockaddr *dest)
1955 {
1956 	struct sctp_paramhdr *phdr, parm_buf;
1957 	struct sctp_tcb *retval;
1958 	uint32_t ptype, plen;
1959 
1960 #ifdef INET
1961 	struct sockaddr_in sin4;
1962 
1963 #endif
1964 #ifdef INET6
1965 	struct sockaddr_in6 sin6;
1966 
1967 #endif
1968 
1969 #ifdef INET
1970 	memset(&sin4, 0, sizeof(sin4));
1971 	sin4.sin_len = sizeof(sin4);
1972 	sin4.sin_family = AF_INET;
1973 	sin4.sin_port = sh->src_port;
1974 #endif
1975 #ifdef INET6
1976 	memset(&sin6, 0, sizeof(sin6));
1977 	sin6.sin6_len = sizeof(sin6);
1978 	sin6.sin6_family = AF_INET6;
1979 	sin6.sin6_port = sh->src_port;
1980 #endif
1981 
1982 	retval = NULL;
1983 	offset += sizeof(struct sctp_init_chunk);
1984 
1985 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1986 	while (phdr != NULL) {
1987 		/* now we must see if we want the parameter */
1988 		ptype = ntohs(phdr->param_type);
1989 		plen = ntohs(phdr->param_length);
1990 		if (plen == 0) {
1991 			break;
1992 		}
1993 #ifdef INET
1994 		if (ptype == SCTP_IPV4_ADDRESS &&
1995 		    plen == sizeof(struct sctp_ipv4addr_param)) {
1996 			/* Get the rest of the address */
1997 			struct sctp_ipv4addr_param ip4_parm, *p4;
1998 
1999 			phdr = sctp_get_next_param(m, offset,
2000 			    (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
2001 			if (phdr == NULL) {
2002 				return (NULL);
2003 			}
2004 			p4 = (struct sctp_ipv4addr_param *)phdr;
2005 			memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2006 			/* look it up */
2007 			retval = sctp_findassociation_ep_addr(inp_p,
2008 			    (struct sockaddr *)&sin4, netp, dest, NULL);
2009 			if (retval != NULL) {
2010 				return (retval);
2011 			}
2012 		}
2013 #endif
2014 #ifdef INET6
2015 		if (ptype == SCTP_IPV6_ADDRESS &&
2016 		    plen == sizeof(struct sctp_ipv6addr_param)) {
2017 			/* Get the rest of the address */
2018 			struct sctp_ipv6addr_param ip6_parm, *p6;
2019 
2020 			phdr = sctp_get_next_param(m, offset,
2021 			    (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
2022 			if (phdr == NULL) {
2023 				return (NULL);
2024 			}
2025 			p6 = (struct sctp_ipv6addr_param *)phdr;
2026 			memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2027 			/* look it up */
2028 			retval = sctp_findassociation_ep_addr(inp_p,
2029 			    (struct sockaddr *)&sin6, netp, dest, NULL);
2030 			if (retval != NULL) {
2031 				return (retval);
2032 			}
2033 		}
2034 #endif
2035 		offset += SCTP_SIZE32(plen);
2036 		phdr = sctp_get_next_param(m, offset, &parm_buf,
2037 		    sizeof(parm_buf));
2038 	}
2039 	return (NULL);
2040 }
2041 
2042 static struct sctp_tcb *
2043 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2044     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2045     uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2046 {
2047 	/*
2048 	 * Use my vtag to hash. If we find it we then verify the source addr
2049 	 * is in the assoc. If all goes well we save a bit on rec of a
2050 	 * packet.
2051 	 */
2052 	struct sctpasochead *head;
2053 	struct sctp_nets *net;
2054 	struct sctp_tcb *stcb;
2055 
2056 	*netp = NULL;
2057 	*inp_p = NULL;
2058 	SCTP_INP_INFO_RLOCK();
2059 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2060 	    SCTP_BASE_INFO(hashasocmark))];
2061 	if (head == NULL) {
2062 		/* invalid vtag */
2063 		SCTP_INP_INFO_RUNLOCK();
2064 		return (NULL);
2065 	}
2066 	LIST_FOREACH(stcb, head, sctp_asocs) {
2067 		SCTP_INP_RLOCK(stcb->sctp_ep);
2068 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2069 			SCTP_INP_RUNLOCK(stcb->sctp_ep);
2070 			continue;
2071 		}
2072 		SCTP_TCB_LOCK(stcb);
2073 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
2074 		if (stcb->asoc.my_vtag == vtag) {
2075 			/* candidate */
2076 			if (stcb->rport != rport) {
2077 				SCTP_TCB_UNLOCK(stcb);
2078 				continue;
2079 			}
2080 			if (stcb->sctp_ep->sctp_lport != lport) {
2081 				SCTP_TCB_UNLOCK(stcb);
2082 				continue;
2083 			}
2084 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2085 				SCTP_TCB_UNLOCK(stcb);
2086 				continue;
2087 			}
2088 			/* RRS:Need toaddr check here */
2089 			if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2090 				/* Endpoint does not own this address */
2091 				SCTP_TCB_UNLOCK(stcb);
2092 				continue;
2093 			}
2094 			if (remote_tag) {
2095 				/*
2096 				 * If we have both vtags that's all we match
2097 				 * on
2098 				 */
2099 				if (stcb->asoc.peer_vtag == remote_tag) {
2100 					/*
2101 					 * If both tags match we consider it
2102 					 * conclusive and check NO
2103 					 * source/destination addresses
2104 					 */
2105 					goto conclusive;
2106 				}
2107 			}
2108 			if (skip_src_check) {
2109 		conclusive:
2110 				if (from) {
2111 					net = sctp_findnet(stcb, from);
2112 				} else {
2113 					*netp = NULL;	/* unknown */
2114 				}
2115 				if (inp_p)
2116 					*inp_p = stcb->sctp_ep;
2117 				SCTP_INP_INFO_RUNLOCK();
2118 				return (stcb);
2119 			}
2120 			net = sctp_findnet(stcb, from);
2121 			if (net) {
2122 				/* yep its him. */
2123 				*netp = net;
2124 				SCTP_STAT_INCR(sctps_vtagexpress);
2125 				*inp_p = stcb->sctp_ep;
2126 				SCTP_INP_INFO_RUNLOCK();
2127 				return (stcb);
2128 			} else {
2129 				/*
2130 				 * not him, this should only happen in rare
2131 				 * cases so I peg it.
2132 				 */
2133 				SCTP_STAT_INCR(sctps_vtagbogus);
2134 			}
2135 		}
2136 		SCTP_TCB_UNLOCK(stcb);
2137 	}
2138 	SCTP_INP_INFO_RUNLOCK();
2139 	return (NULL);
2140 }
2141 
2142 /*
2143  * Find an association with the pointer to the inbound IP packet. This can be
2144  * a IPv4 or IPv6 packet.
2145  */
2146 struct sctp_tcb *
2147 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
2148     struct sctphdr *sh, struct sctp_chunkhdr *ch,
2149     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2150 {
2151 	int find_tcp_pool;
2152 	struct ip *iph;
2153 	struct sctp_tcb *retval;
2154 	struct sockaddr_storage to_store, from_store;
2155 	struct sockaddr *to = (struct sockaddr *)&to_store;
2156 	struct sockaddr *from = (struct sockaddr *)&from_store;
2157 	struct sctp_inpcb *inp;
2158 
2159 	iph = mtod(m, struct ip *);
2160 	switch (iph->ip_v) {
2161 #ifdef INET
2162 	case IPVERSION:
2163 		{
2164 			/* its IPv4 */
2165 			struct sockaddr_in *from4;
2166 
2167 			from4 = (struct sockaddr_in *)&from_store;
2168 			bzero(from4, sizeof(*from4));
2169 			from4->sin_family = AF_INET;
2170 			from4->sin_len = sizeof(struct sockaddr_in);
2171 			from4->sin_addr.s_addr = iph->ip_src.s_addr;
2172 			from4->sin_port = sh->src_port;
2173 			break;
2174 		}
2175 #endif
2176 #ifdef INET6
2177 	case IPV6_VERSION >> 4:
2178 		{
2179 			/* its IPv6 */
2180 			struct ip6_hdr *ip6;
2181 			struct sockaddr_in6 *from6;
2182 
2183 			ip6 = mtod(m, struct ip6_hdr *);
2184 			from6 = (struct sockaddr_in6 *)&from_store;
2185 			bzero(from6, sizeof(*from6));
2186 			from6->sin6_family = AF_INET6;
2187 			from6->sin6_len = sizeof(struct sockaddr_in6);
2188 			from6->sin6_addr = ip6->ip6_src;
2189 			from6->sin6_port = sh->src_port;
2190 			/* Get the scopes in properly to the sin6 addr's */
2191 			/* we probably don't need these operations */
2192 			(void)sa6_recoverscope(from6);
2193 			sa6_embedscope(from6, MODULE_GLOBAL(ip6_use_defzone));
2194 			break;
2195 		}
2196 #endif
2197 	default:
2198 		/* Currently not supported. */
2199 		return (NULL);
2200 	}
2201 
2202 
2203 	switch (iph->ip_v) {
2204 #ifdef INET
2205 	case IPVERSION:
2206 		{
2207 			/* its IPv4 */
2208 			struct sockaddr_in *to4;
2209 
2210 			to4 = (struct sockaddr_in *)&to_store;
2211 			bzero(to4, sizeof(*to4));
2212 			to4->sin_family = AF_INET;
2213 			to4->sin_len = sizeof(struct sockaddr_in);
2214 			to4->sin_addr.s_addr = iph->ip_dst.s_addr;
2215 			to4->sin_port = sh->dest_port;
2216 			break;
2217 		}
2218 #endif
2219 #ifdef INET6
2220 	case IPV6_VERSION >> 4:
2221 		{
2222 			/* its IPv6 */
2223 			struct ip6_hdr *ip6;
2224 			struct sockaddr_in6 *to6;
2225 
2226 			ip6 = mtod(m, struct ip6_hdr *);
2227 			to6 = (struct sockaddr_in6 *)&to_store;
2228 			bzero(to6, sizeof(*to6));
2229 			to6->sin6_family = AF_INET6;
2230 			to6->sin6_len = sizeof(struct sockaddr_in6);
2231 			to6->sin6_addr = ip6->ip6_dst;
2232 			to6->sin6_port = sh->dest_port;
2233 			/* Get the scopes in properly to the sin6 addr's */
2234 			/* we probably don't need these operations */
2235 			(void)sa6_recoverscope(to6);
2236 			sa6_embedscope(to6, MODULE_GLOBAL(ip6_use_defzone));
2237 			break;
2238 		}
2239 #endif
2240 	default:
2241 		/* TSNH */
2242 		break;
2243 	}
2244 	if (sh->v_tag) {
2245 		/* we only go down this path if vtag is non-zero */
2246 		retval = sctp_findassoc_by_vtag(from, to, ntohl(sh->v_tag),
2247 		    inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2248 		if (retval) {
2249 			return (retval);
2250 		}
2251 	}
2252 	find_tcp_pool = 0;
2253 	if ((ch->chunk_type != SCTP_INITIATION) &&
2254 	    (ch->chunk_type != SCTP_INITIATION_ACK) &&
2255 	    (ch->chunk_type != SCTP_COOKIE_ACK) &&
2256 	    (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2257 		/* Other chunk types go to the tcp pool. */
2258 		find_tcp_pool = 1;
2259 	}
2260 	if (inp_p) {
2261 		retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
2262 		    find_tcp_pool, vrf_id);
2263 		inp = *inp_p;
2264 	} else {
2265 		retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
2266 		    find_tcp_pool, vrf_id);
2267 	}
2268 	SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
2269 	if (retval == NULL && inp) {
2270 		/* Found a EP but not this address */
2271 		if ((ch->chunk_type == SCTP_INITIATION) ||
2272 		    (ch->chunk_type == SCTP_INITIATION_ACK)) {
2273 			/*-
2274 			 * special hook, we do NOT return linp or an
2275 			 * association that is linked to an existing
2276 			 * association that is under the TCP pool (i.e. no
2277 			 * listener exists). The endpoint finding routine
2278 			 * will always find a listener before examining the
2279 			 * TCP pool.
2280 			 */
2281 			if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2282 				if (inp_p) {
2283 					*inp_p = NULL;
2284 				}
2285 				return (NULL);
2286 			}
2287 			retval = sctp_findassociation_special_addr(m, iphlen,
2288 			    offset, sh, &inp, netp, to);
2289 			if (inp_p != NULL) {
2290 				*inp_p = inp;
2291 			}
2292 		}
2293 	}
2294 	SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
2295 	return (retval);
2296 }
2297 
2298 /*
2299  * lookup an association by an ASCONF lookup address.
2300  * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2301  */
2302 struct sctp_tcb *
2303 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
2304     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2305 {
2306 	struct sctp_tcb *stcb;
2307 	struct sockaddr_storage local_store, remote_store;
2308 	struct sockaddr *to;
2309 	struct ip *iph;
2310 	struct sctp_paramhdr parm_buf, *phdr;
2311 	int ptype;
2312 	int zero_address = 0;
2313 
2314 #ifdef INET
2315 	struct sockaddr_in *sin;
2316 
2317 #endif
2318 #ifdef INET6
2319 	struct ip6_hdr *ip6;
2320 	struct sockaddr_in6 *sin6;
2321 
2322 #endif
2323 
2324 	memset(&local_store, 0, sizeof(local_store));
2325 	memset(&remote_store, 0, sizeof(remote_store));
2326 	to = (struct sockaddr *)&local_store;
2327 	/* First get the destination address setup too. */
2328 	iph = mtod(m, struct ip *);
2329 	switch (iph->ip_v) {
2330 #ifdef INET
2331 	case IPVERSION:
2332 		/* its IPv4 */
2333 		sin = (struct sockaddr_in *)&local_store;
2334 		sin->sin_family = AF_INET;
2335 		sin->sin_len = sizeof(*sin);
2336 		sin->sin_port = sh->dest_port;
2337 		sin->sin_addr.s_addr = iph->ip_dst.s_addr;
2338 		break;
2339 #endif
2340 #ifdef INET6
2341 	case IPV6_VERSION >> 4:
2342 		/* its IPv6 */
2343 		ip6 = mtod(m, struct ip6_hdr *);
2344 		sin6 = (struct sockaddr_in6 *)&local_store;
2345 		sin6->sin6_family = AF_INET6;
2346 		sin6->sin6_len = sizeof(*sin6);
2347 		sin6->sin6_port = sh->dest_port;
2348 		sin6->sin6_addr = ip6->ip6_dst;
2349 		break;
2350 #endif
2351 	default:
2352 		return NULL;
2353 	}
2354 
2355 	phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2356 	    &parm_buf, sizeof(struct sctp_paramhdr));
2357 	if (phdr == NULL) {
2358 		SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2359 		    __FUNCTION__);
2360 		return NULL;
2361 	}
2362 	ptype = (int)((uint32_t) ntohs(phdr->param_type));
2363 	/* get the correlation address */
2364 	switch (ptype) {
2365 #ifdef INET6
2366 	case SCTP_IPV6_ADDRESS:
2367 		{
2368 			/* ipv6 address param */
2369 			struct sctp_ipv6addr_param *p6, p6_buf;
2370 
2371 			if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2372 				return NULL;
2373 			}
2374 			p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2375 			    offset + sizeof(struct sctp_asconf_chunk),
2376 			    &p6_buf.ph, sizeof(*p6));
2377 			if (p6 == NULL) {
2378 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2379 				    __FUNCTION__);
2380 				return (NULL);
2381 			}
2382 			sin6 = (struct sockaddr_in6 *)&remote_store;
2383 			sin6->sin6_family = AF_INET6;
2384 			sin6->sin6_len = sizeof(*sin6);
2385 			sin6->sin6_port = sh->src_port;
2386 			memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2387 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2388 				zero_address = 1;
2389 			break;
2390 		}
2391 #endif
2392 #ifdef INET
2393 	case SCTP_IPV4_ADDRESS:
2394 		{
2395 			/* ipv4 address param */
2396 			struct sctp_ipv4addr_param *p4, p4_buf;
2397 
2398 			if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2399 				return NULL;
2400 			}
2401 			p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2402 			    offset + sizeof(struct sctp_asconf_chunk),
2403 			    &p4_buf.ph, sizeof(*p4));
2404 			if (p4 == NULL) {
2405 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2406 				    __FUNCTION__);
2407 				return (NULL);
2408 			}
2409 			sin = (struct sockaddr_in *)&remote_store;
2410 			sin->sin_family = AF_INET;
2411 			sin->sin_len = sizeof(*sin);
2412 			sin->sin_port = sh->src_port;
2413 			memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2414 			if (sin->sin_addr.s_addr == INADDR_ANY)
2415 				zero_address = 1;
2416 			break;
2417 		}
2418 #endif
2419 	default:
2420 		/* invalid address param type */
2421 		return NULL;
2422 	}
2423 
2424 	if (zero_address) {
2425 		stcb = sctp_findassoc_by_vtag(NULL, to, ntohl(sh->v_tag), inp_p,
2426 		    netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2427 		/*
2428 		 * printf("findassociation_ep_asconf: zero lookup address
2429 		 * finds stcb 0x%x\n", (uint32_t)stcb);
2430 		 */
2431 	} else {
2432 		stcb = sctp_findassociation_ep_addr(inp_p,
2433 		    (struct sockaddr *)&remote_store, netp,
2434 		    to, NULL);
2435 	}
2436 	return (stcb);
2437 }
2438 
2439 
2440 /*
2441  * allocate a sctp_inpcb and setup a temporary binding to a port/all
2442  * addresses. This way if we don't get a bind we by default pick a ephemeral
2443  * port with all addresses bound.
2444  */
2445 int
2446 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2447 {
2448 	/*
2449 	 * we get called when a new endpoint starts up. We need to allocate
2450 	 * the sctp_inpcb structure from the zone and init it. Mark it as
2451 	 * unbound and find a port that we can use as an ephemeral with
2452 	 * INADDR_ANY. If the user binds later no problem we can then add in
2453 	 * the specific addresses. And setup the default parameters for the
2454 	 * EP.
2455 	 */
2456 	int i, error;
2457 	struct sctp_inpcb *inp;
2458 	struct sctp_pcb *m;
2459 	struct timeval time;
2460 	sctp_sharedkey_t *null_key;
2461 
2462 	error = 0;
2463 
2464 	SCTP_INP_INFO_WLOCK();
2465 	inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2466 	if (inp == NULL) {
2467 		SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2468 		SCTP_INP_INFO_WUNLOCK();
2469 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2470 		return (ENOBUFS);
2471 	}
2472 	/* zap it */
2473 	bzero(inp, sizeof(*inp));
2474 
2475 	/* bump generations */
2476 	/* setup socket pointers */
2477 	inp->sctp_socket = so;
2478 	inp->ip_inp.inp.inp_socket = so;
2479 #ifdef INET6
2480 	if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2481 		inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2482 	}
2483 #endif
2484 	inp->sctp_associd_counter = 1;
2485 	inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2486 	inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2487 	inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2488 	inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
2489 	/* init the small hash table we use to track asocid <-> tcb */
2490 	inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2491 	if (inp->sctp_asocidhash == NULL) {
2492 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2493 		SCTP_INP_INFO_WUNLOCK();
2494 		return (ENOBUFS);
2495 	}
2496 #ifdef IPSEC
2497 	{
2498 		struct inpcbpolicy *pcb_sp = NULL;
2499 
2500 		error = ipsec_init_policy(so, &pcb_sp);
2501 		/* Arrange to share the policy */
2502 		inp->ip_inp.inp.inp_sp = pcb_sp;
2503 		((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2504 	}
2505 	if (error != 0) {
2506 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2507 		SCTP_INP_INFO_WUNLOCK();
2508 		return error;
2509 	}
2510 #endif				/* IPSEC */
2511 	SCTP_INCR_EP_COUNT();
2512 	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2513 	SCTP_INP_INFO_WUNLOCK();
2514 
2515 	so->so_pcb = (caddr_t)inp;
2516 
2517 	if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2518 		/* UDP style socket */
2519 		inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2520 		    SCTP_PCB_FLAGS_UNBOUND);
2521 		/* Be sure it is NON-BLOCKING IO for UDP */
2522 		/* SCTP_SET_SO_NBIO(so); */
2523 	} else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2524 		/* TCP style socket */
2525 		inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2526 		    SCTP_PCB_FLAGS_UNBOUND);
2527 		/* Be sure we have blocking IO by default */
2528 		SCTP_CLEAR_SO_NBIO(so);
2529 	} else {
2530 		/*
2531 		 * unsupported socket type (RAW, etc)- in case we missed it
2532 		 * in protosw
2533 		 */
2534 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2535 		so->so_pcb = NULL;
2536 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2537 		return (EOPNOTSUPP);
2538 	}
2539 	if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2540 		sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2541 		sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2542 	} else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2543 		sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2544 		sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2545 	} else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2546 		sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2547 		sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2548 	}
2549 	inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2550 	    &inp->sctp_hashmark);
2551 	if (inp->sctp_tcbhash == NULL) {
2552 		SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2553 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2554 		so->so_pcb = NULL;
2555 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2556 		return (ENOBUFS);
2557 	}
2558 	inp->def_vrf_id = vrf_id;
2559 
2560 	SCTP_INP_INFO_WLOCK();
2561 	SCTP_INP_LOCK_INIT(inp);
2562 	INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2563 	SCTP_INP_READ_INIT(inp);
2564 	SCTP_ASOC_CREATE_LOCK_INIT(inp);
2565 	/* lock the new ep */
2566 	SCTP_INP_WLOCK(inp);
2567 
2568 	/* add it to the info area */
2569 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2570 	SCTP_INP_INFO_WUNLOCK();
2571 
2572 	TAILQ_INIT(&inp->read_queue);
2573 	LIST_INIT(&inp->sctp_addr_list);
2574 
2575 	LIST_INIT(&inp->sctp_asoc_list);
2576 
2577 #ifdef SCTP_TRACK_FREED_ASOCS
2578 	/* TEMP CODE */
2579 	LIST_INIT(&inp->sctp_asoc_free_list);
2580 #endif
2581 	/* Init the timer structure for signature change */
2582 	SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2583 	inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2584 
2585 	/* now init the actual endpoint default data */
2586 	m = &inp->sctp_ep;
2587 
2588 	/* setup the base timeout information */
2589 	m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC);	/* needed ? */
2590 	m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC);	/* needed ? */
2591 	m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2592 	m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2593 	m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2594 	m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2595 	m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2596 	/* all max/min max are in ms */
2597 	m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2598 	m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2599 	m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2600 	m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2601 	m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2602 
2603 	m->max_open_streams_intome = MAX_SCTP_STREAMS;
2604 
2605 	m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2606 	m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2607 	m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2608 	m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2609 	m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2610 	m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2611 	m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2612 	m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2613 
2614 	m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2615 	m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2616 	/* number of streams to pre-open on a association */
2617 	m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2618 
2619 	/* Add adaptation cookie */
2620 	m->adaptation_layer_indicator = 0x504C5253;
2621 
2622 	/* seed random number generator */
2623 	m->random_counter = 1;
2624 	m->store_at = SCTP_SIGNATURE_SIZE;
2625 	SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2626 	sctp_fill_random_store(m);
2627 
2628 	/* Minimum cookie size */
2629 	m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2630 	    sizeof(struct sctp_state_cookie);
2631 	m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2632 
2633 	/* Setup the initial secret */
2634 	(void)SCTP_GETTIME_TIMEVAL(&time);
2635 	m->time_of_secret_change = time.tv_sec;
2636 
2637 	for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2638 		m->secret_key[0][i] = sctp_select_initial_TSN(m);
2639 	}
2640 	sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2641 
2642 	/* How long is a cookie good for ? */
2643 	m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2644 	/*
2645 	 * Initialize authentication parameters
2646 	 */
2647 	m->local_hmacs = sctp_default_supported_hmaclist();
2648 	m->local_auth_chunks = sctp_alloc_chunklist();
2649 	m->default_dscp = 0;
2650 #ifdef INET6
2651 	m->default_flowlabel = 0;
2652 #endif
2653 	sctp_auth_set_default_chunks(m->local_auth_chunks);
2654 	LIST_INIT(&m->shared_keys);
2655 	/* add default NULL key as key id 0 */
2656 	null_key = sctp_alloc_sharedkey();
2657 	sctp_insert_sharedkey(&m->shared_keys, null_key);
2658 	SCTP_INP_WUNLOCK(inp);
2659 #ifdef SCTP_LOG_CLOSING
2660 	sctp_log_closing(inp, NULL, 12);
2661 #endif
2662 	return (error);
2663 }
2664 
2665 
2666 void
2667 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2668     struct sctp_tcb *stcb)
2669 {
2670 	struct sctp_nets *net;
2671 	uint16_t lport, rport;
2672 	struct sctppcbhead *head;
2673 	struct sctp_laddr *laddr, *oladdr;
2674 
2675 	atomic_add_int(&stcb->asoc.refcnt, 1);
2676 	SCTP_TCB_UNLOCK(stcb);
2677 	SCTP_INP_INFO_WLOCK();
2678 	SCTP_INP_WLOCK(old_inp);
2679 	SCTP_INP_WLOCK(new_inp);
2680 	SCTP_TCB_LOCK(stcb);
2681 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
2682 
2683 	new_inp->sctp_ep.time_of_secret_change =
2684 	    old_inp->sctp_ep.time_of_secret_change;
2685 	memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2686 	    sizeof(old_inp->sctp_ep.secret_key));
2687 	new_inp->sctp_ep.current_secret_number =
2688 	    old_inp->sctp_ep.current_secret_number;
2689 	new_inp->sctp_ep.last_secret_number =
2690 	    old_inp->sctp_ep.last_secret_number;
2691 	new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2692 
2693 	/* make it so new data pours into the new socket */
2694 	stcb->sctp_socket = new_inp->sctp_socket;
2695 	stcb->sctp_ep = new_inp;
2696 
2697 	/* Copy the port across */
2698 	lport = new_inp->sctp_lport = old_inp->sctp_lport;
2699 	rport = stcb->rport;
2700 	/* Pull the tcb from the old association */
2701 	LIST_REMOVE(stcb, sctp_tcbhash);
2702 	LIST_REMOVE(stcb, sctp_tcblist);
2703 	if (stcb->asoc.in_asocid_hash) {
2704 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
2705 	}
2706 	/* Now insert the new_inp into the TCP connected hash */
2707 	head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2708 
2709 	LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2710 	/* Its safe to access */
2711 	new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2712 
2713 	/* Now move the tcb into the endpoint list */
2714 	LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2715 	/*
2716 	 * Question, do we even need to worry about the ep-hash since we
2717 	 * only have one connection? Probably not :> so lets get rid of it
2718 	 * and not suck up any kernel memory in that.
2719 	 */
2720 	if (stcb->asoc.in_asocid_hash) {
2721 		struct sctpasochead *lhd;
2722 
2723 		lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2724 		    new_inp->hashasocidmark)];
2725 		LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2726 	}
2727 	/* Ok. Let's restart timer. */
2728 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2729 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2730 		    stcb, net);
2731 	}
2732 
2733 	SCTP_INP_INFO_WUNLOCK();
2734 	if (new_inp->sctp_tcbhash != NULL) {
2735 		SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2736 		new_inp->sctp_tcbhash = NULL;
2737 	}
2738 	if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2739 		/* Subset bound, so copy in the laddr list from the old_inp */
2740 		LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2741 			laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2742 			if (laddr == NULL) {
2743 				/*
2744 				 * Gak, what can we do? This assoc is really
2745 				 * HOSED. We probably should send an abort
2746 				 * here.
2747 				 */
2748 				SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2749 				continue;
2750 			}
2751 			SCTP_INCR_LADDR_COUNT();
2752 			bzero(laddr, sizeof(*laddr));
2753 			(void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2754 			laddr->ifa = oladdr->ifa;
2755 			atomic_add_int(&laddr->ifa->refcount, 1);
2756 			LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2757 			    sctp_nxt_addr);
2758 			new_inp->laddr_count++;
2759 			if (oladdr == stcb->asoc.last_used_address) {
2760 				stcb->asoc.last_used_address = laddr;
2761 			}
2762 		}
2763 	}
2764 	/*
2765 	 * Now any running timers need to be adjusted since we really don't
2766 	 * care if they are running or not just blast in the new_inp into
2767 	 * all of them.
2768 	 */
2769 
2770 	stcb->asoc.dack_timer.ep = (void *)new_inp;
2771 	stcb->asoc.asconf_timer.ep = (void *)new_inp;
2772 	stcb->asoc.strreset_timer.ep = (void *)new_inp;
2773 	stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2774 	stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2775 	stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2776 	stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2777 	/* now what about the nets? */
2778 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2779 		net->pmtu_timer.ep = (void *)new_inp;
2780 		net->hb_timer.ep = (void *)new_inp;
2781 		net->rxt_timer.ep = (void *)new_inp;
2782 	}
2783 	SCTP_INP_WUNLOCK(new_inp);
2784 	SCTP_INP_WUNLOCK(old_inp);
2785 }
2786 
2787 
2788 
2789 
2790 /* sctp_ifap is used to bypass normal local address validation checks */
2791 int
2792 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2793     struct sctp_ifa *sctp_ifap, struct thread *p)
2794 {
2795 	/* bind a ep to a socket address */
2796 	struct sctppcbhead *head;
2797 	struct sctp_inpcb *inp, *inp_tmp;
2798 	struct inpcb *ip_inp;
2799 	int port_reuse_active = 0;
2800 	int bindall;
2801 	uint16_t lport;
2802 	int error;
2803 	uint32_t vrf_id;
2804 
2805 	lport = 0;
2806 	error = 0;
2807 	bindall = 1;
2808 	inp = (struct sctp_inpcb *)so->so_pcb;
2809 	ip_inp = (struct inpcb *)so->so_pcb;
2810 #ifdef SCTP_DEBUG
2811 	if (addr) {
2812 		SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2813 		    ntohs(((struct sockaddr_in *)addr)->sin_port));
2814 		SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2815 		SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2816 	}
2817 #endif
2818 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2819 		/* already did a bind, subsequent binds NOT allowed ! */
2820 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2821 		return (EINVAL);
2822 	}
2823 #ifdef INVARIANTS
2824 	if (p == NULL)
2825 		panic("null proc/thread");
2826 #endif
2827 	if (addr != NULL) {
2828 		switch (addr->sa_family) {
2829 #ifdef INET
2830 		case AF_INET:
2831 			{
2832 				struct sockaddr_in *sin;
2833 
2834 				/* IPV6_V6ONLY socket? */
2835 				if (SCTP_IPV6_V6ONLY(ip_inp)) {
2836 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2837 					return (EINVAL);
2838 				}
2839 				if (addr->sa_len != sizeof(*sin)) {
2840 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2841 					return (EINVAL);
2842 				}
2843 				sin = (struct sockaddr_in *)addr;
2844 				lport = sin->sin_port;
2845 				/*
2846 				 * For LOOPBACK the prison_local_ip4() call
2847 				 * will transmute the ip address to the
2848 				 * proper value.
2849 				 */
2850 				if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
2851 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2852 					return (error);
2853 				}
2854 				if (sin->sin_addr.s_addr != INADDR_ANY) {
2855 					bindall = 0;
2856 				}
2857 				break;
2858 			}
2859 #endif
2860 #ifdef INET6
2861 		case AF_INET6:
2862 			{
2863 				/*
2864 				 * Only for pure IPv6 Address. (No IPv4
2865 				 * Mapped!)
2866 				 */
2867 				struct sockaddr_in6 *sin6;
2868 
2869 				sin6 = (struct sockaddr_in6 *)addr;
2870 
2871 				if (addr->sa_len != sizeof(*sin6)) {
2872 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2873 					return (EINVAL);
2874 				}
2875 				lport = sin6->sin6_port;
2876 
2877 				/*
2878 				 * For LOOPBACK the prison_local_ip6() call
2879 				 * will transmute the ipv6 address to the
2880 				 * proper value.
2881 				 */
2882 				if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
2883 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2884 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2885 					return (error);
2886 				}
2887 				if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2888 					bindall = 0;
2889 					/* KAME hack: embed scopeid */
2890 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
2891 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2892 						return (EINVAL);
2893 					}
2894 				}
2895 				/* this must be cleared for ifa_ifwithaddr() */
2896 				sin6->sin6_scope_id = 0;
2897 				break;
2898 			}
2899 #endif
2900 		default:
2901 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2902 			return (EAFNOSUPPORT);
2903 		}
2904 	}
2905 	SCTP_INP_INFO_WLOCK();
2906 	SCTP_INP_WLOCK(inp);
2907 	/* Setup a vrf_id to be the default for the non-bind-all case. */
2908 	vrf_id = inp->def_vrf_id;
2909 
2910 	/* increase our count due to the unlock we do */
2911 	SCTP_INP_INCR_REF(inp);
2912 	if (lport) {
2913 		/*
2914 		 * Did the caller specify a port? if so we must see if a ep
2915 		 * already has this one bound.
2916 		 */
2917 		/* got to be root to get at low ports */
2918 		if (ntohs(lport) < IPPORT_RESERVED) {
2919 			if (p && (error =
2920 			    priv_check(p, PRIV_NETINET_RESERVEDPORT)
2921 			    )) {
2922 				SCTP_INP_DECR_REF(inp);
2923 				SCTP_INP_WUNLOCK(inp);
2924 				SCTP_INP_INFO_WUNLOCK();
2925 				return (error);
2926 			}
2927 		}
2928 		if (p == NULL) {
2929 			SCTP_INP_DECR_REF(inp);
2930 			SCTP_INP_WUNLOCK(inp);
2931 			SCTP_INP_INFO_WUNLOCK();
2932 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2933 			return (error);
2934 		}
2935 		SCTP_INP_WUNLOCK(inp);
2936 		if (bindall) {
2937 			vrf_id = inp->def_vrf_id;
2938 			inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2939 			if (inp_tmp != NULL) {
2940 				/*
2941 				 * lock guy returned and lower count note
2942 				 * that we are not bound so inp_tmp should
2943 				 * NEVER be inp. And it is this inp
2944 				 * (inp_tmp) that gets the reference bump,
2945 				 * so we must lower it.
2946 				 */
2947 				SCTP_INP_DECR_REF(inp_tmp);
2948 				/* unlock info */
2949 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2950 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2951 					/*
2952 					 * Ok, must be one-2-one and
2953 					 * allowing port re-use
2954 					 */
2955 					port_reuse_active = 1;
2956 					goto continue_anyway;
2957 				}
2958 				SCTP_INP_DECR_REF(inp);
2959 				SCTP_INP_INFO_WUNLOCK();
2960 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2961 				return (EADDRINUSE);
2962 			}
2963 		} else {
2964 			inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2965 			if (inp_tmp != NULL) {
2966 				/*
2967 				 * lock guy returned and lower count note
2968 				 * that we are not bound so inp_tmp should
2969 				 * NEVER be inp. And it is this inp
2970 				 * (inp_tmp) that gets the reference bump,
2971 				 * so we must lower it.
2972 				 */
2973 				SCTP_INP_DECR_REF(inp_tmp);
2974 				/* unlock info */
2975 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2976 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2977 					/*
2978 					 * Ok, must be one-2-one and
2979 					 * allowing port re-use
2980 					 */
2981 					port_reuse_active = 1;
2982 					goto continue_anyway;
2983 				}
2984 				SCTP_INP_DECR_REF(inp);
2985 				SCTP_INP_INFO_WUNLOCK();
2986 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2987 				return (EADDRINUSE);
2988 			}
2989 		}
2990 continue_anyway:
2991 		SCTP_INP_WLOCK(inp);
2992 		if (bindall) {
2993 			/* verify that no lport is not used by a singleton */
2994 			if ((port_reuse_active == 0) &&
2995 			    (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))
2996 			    ) {
2997 				/* Sorry someone already has this one bound */
2998 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2999 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3000 					port_reuse_active = 1;
3001 				} else {
3002 					SCTP_INP_DECR_REF(inp);
3003 					SCTP_INP_WUNLOCK(inp);
3004 					SCTP_INP_INFO_WUNLOCK();
3005 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3006 					return (EADDRINUSE);
3007 				}
3008 			}
3009 		}
3010 	} else {
3011 		uint16_t first, last, candidate;
3012 		uint16_t count;
3013 		int done;
3014 
3015 		if (ip_inp->inp_flags & INP_HIGHPORT) {
3016 			first = MODULE_GLOBAL(ipport_hifirstauto);
3017 			last = MODULE_GLOBAL(ipport_hilastauto);
3018 		} else if (ip_inp->inp_flags & INP_LOWPORT) {
3019 			if (p && (error =
3020 			    priv_check(p, PRIV_NETINET_RESERVEDPORT)
3021 			    )) {
3022 				SCTP_INP_DECR_REF(inp);
3023 				SCTP_INP_WUNLOCK(inp);
3024 				SCTP_INP_INFO_WUNLOCK();
3025 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3026 				return (error);
3027 			}
3028 			first = MODULE_GLOBAL(ipport_lowfirstauto);
3029 			last = MODULE_GLOBAL(ipport_lowlastauto);
3030 		} else {
3031 			first = MODULE_GLOBAL(ipport_firstauto);
3032 			last = MODULE_GLOBAL(ipport_lastauto);
3033 		}
3034 		if (first > last) {
3035 			uint16_t temp;
3036 
3037 			temp = first;
3038 			first = last;
3039 			last = temp;
3040 		}
3041 		count = last - first + 1;	/* number of candidates */
3042 		candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3043 
3044 		done = 0;
3045 		while (!done) {
3046 			if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3047 				done = 1;
3048 			}
3049 			if (!done) {
3050 				if (--count == 0) {
3051 					SCTP_INP_DECR_REF(inp);
3052 					SCTP_INP_WUNLOCK(inp);
3053 					SCTP_INP_INFO_WUNLOCK();
3054 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3055 					return (EADDRINUSE);
3056 				}
3057 				if (candidate == last)
3058 					candidate = first;
3059 				else
3060 					candidate = candidate + 1;
3061 			}
3062 		}
3063 		lport = htons(candidate);
3064 	}
3065 	SCTP_INP_DECR_REF(inp);
3066 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3067 	    SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3068 		/*
3069 		 * this really should not happen. The guy did a non-blocking
3070 		 * bind and then did a close at the same time.
3071 		 */
3072 		SCTP_INP_WUNLOCK(inp);
3073 		SCTP_INP_INFO_WUNLOCK();
3074 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3075 		return (EINVAL);
3076 	}
3077 	/* ok we look clear to give out this port, so lets setup the binding */
3078 	if (bindall) {
3079 		/* binding to all addresses, so just set in the proper flags */
3080 		inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3081 		/* set the automatic addr changes from kernel flag */
3082 		if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3083 			sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3084 			sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3085 		} else {
3086 			sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3087 			sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3088 		}
3089 		if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3090 			sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3091 		} else {
3092 			sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3093 		}
3094 		/*
3095 		 * set the automatic mobility_base from kernel flag (by
3096 		 * micchie)
3097 		 */
3098 		if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3099 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3100 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3101 		} else {
3102 			sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3103 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3104 		}
3105 		/*
3106 		 * set the automatic mobility_fasthandoff from kernel flag
3107 		 * (by micchie)
3108 		 */
3109 		if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3110 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3111 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3112 		} else {
3113 			sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3114 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3115 		}
3116 	} else {
3117 		/*
3118 		 * bind specific, make sure flags is off and add a new
3119 		 * address structure to the sctp_addr_list inside the ep
3120 		 * structure.
3121 		 *
3122 		 * We will need to allocate one and insert it at the head. The
3123 		 * socketopt call can just insert new addresses in there as
3124 		 * well. It will also have to do the embed scope kame hack
3125 		 * too (before adding).
3126 		 */
3127 		struct sctp_ifa *ifa;
3128 		struct sockaddr_storage store_sa;
3129 
3130 		memset(&store_sa, 0, sizeof(store_sa));
3131 		switch (addr->sa_family) {
3132 		case AF_INET:
3133 			{
3134 				struct sockaddr_in *sin;
3135 
3136 				sin = (struct sockaddr_in *)&store_sa;
3137 				memcpy(sin, addr, sizeof(struct sockaddr_in));
3138 				sin->sin_port = 0;
3139 				break;
3140 			}
3141 		case AF_INET6:
3142 			{
3143 				struct sockaddr_in6 *sin6;
3144 
3145 				sin6 = (struct sockaddr_in6 *)&store_sa;
3146 				memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3147 				sin6->sin6_port = 0;
3148 				break;
3149 			}
3150 		default:
3151 			break;
3152 		}
3153 		/*
3154 		 * first find the interface with the bound address need to
3155 		 * zero out the port to find the address! yuck! can't do
3156 		 * this earlier since need port for sctp_pcb_findep()
3157 		 */
3158 		if (sctp_ifap != NULL)
3159 			ifa = sctp_ifap;
3160 		else {
3161 			/*
3162 			 * Note for BSD we hit here always other O/S's will
3163 			 * pass things in via the sctp_ifap argument
3164 			 * (Panda).
3165 			 */
3166 			ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3167 			    vrf_id, SCTP_ADDR_NOT_LOCKED);
3168 		}
3169 		if (ifa == NULL) {
3170 			/* Can't find an interface with that address */
3171 			SCTP_INP_WUNLOCK(inp);
3172 			SCTP_INP_INFO_WUNLOCK();
3173 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3174 			return (EADDRNOTAVAIL);
3175 		}
3176 #ifdef INET6
3177 		if (addr->sa_family == AF_INET6) {
3178 			/* GAK, more FIXME IFA lock? */
3179 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3180 				/* Can't bind a non-existent addr. */
3181 				SCTP_INP_WUNLOCK(inp);
3182 				SCTP_INP_INFO_WUNLOCK();
3183 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3184 				return (EINVAL);
3185 			}
3186 		}
3187 #endif
3188 		/* we're not bound all */
3189 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3190 		/* allow bindx() to send ASCONF's for binding changes */
3191 		sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3192 		/* clear automatic addr changes from kernel flag */
3193 		sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3194 
3195 		/* add this address to the endpoint list */
3196 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3197 		if (error != 0) {
3198 			SCTP_INP_WUNLOCK(inp);
3199 			SCTP_INP_INFO_WUNLOCK();
3200 			return (error);
3201 		}
3202 		inp->laddr_count++;
3203 	}
3204 	/* find the bucket */
3205 	if (port_reuse_active) {
3206 		/* Put it into tcp 1-2-1 hash */
3207 		head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3208 		inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3209 	} else {
3210 		head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3211 	}
3212 	/* put it in the bucket */
3213 	LIST_INSERT_HEAD(head, inp, sctp_hash);
3214 	SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3215 	    head, ntohs(lport), port_reuse_active);
3216 	/* set in the port */
3217 	inp->sctp_lport = lport;
3218 
3219 	/* turn off just the unbound flag */
3220 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3221 	SCTP_INP_WUNLOCK(inp);
3222 	SCTP_INP_INFO_WUNLOCK();
3223 	return (0);
3224 }
3225 
3226 
3227 static void
3228 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3229 {
3230 	struct sctp_iterator *it, *nit;
3231 
3232 	/*
3233 	 * We enter with the only the ITERATOR_LOCK in place and a write
3234 	 * lock on the inp_info stuff.
3235 	 */
3236 	it = sctp_it_ctl.cur_it;
3237 	if (it && (it->vn != curvnet)) {
3238 		/* Its not looking at our VNET */
3239 		return;
3240 	}
3241 	if (it && (it->inp == inp)) {
3242 		/*
3243 		 * This is tricky and we hold the iterator lock, but when it
3244 		 * returns and gets the lock (when we release it) the
3245 		 * iterator will try to operate on inp. We need to stop that
3246 		 * from happening. But of course the iterator has a
3247 		 * reference on the stcb and inp. We can mark it and it will
3248 		 * stop.
3249 		 *
3250 		 * If its a single iterator situation, we set the end iterator
3251 		 * flag. Otherwise we set the iterator to go to the next
3252 		 * inp.
3253 		 *
3254 		 */
3255 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3256 			sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3257 		} else {
3258 			sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3259 		}
3260 	}
3261 	/*
3262 	 * Now go through and remove any single reference to our inp that
3263 	 * may be still pending on the list
3264 	 */
3265 	SCTP_IPI_ITERATOR_WQ_LOCK();
3266 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3267 		if (it->vn != curvnet) {
3268 			continue;
3269 		}
3270 		if (it->inp == inp) {
3271 			/* This one points to me is it inp specific? */
3272 			if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3273 				/* Remove and free this one */
3274 				TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3275 				    it, sctp_nxt_itr);
3276 				if (it->function_atend != NULL) {
3277 					(*it->function_atend) (it->pointer, it->val);
3278 				}
3279 				SCTP_FREE(it, SCTP_M_ITER);
3280 			} else {
3281 				it->inp = LIST_NEXT(it->inp, sctp_list);
3282 				if (it->inp) {
3283 					SCTP_INP_INCR_REF(it->inp);
3284 				}
3285 			}
3286 			/*
3287 			 * When its put in the refcnt is incremented so decr
3288 			 * it
3289 			 */
3290 			SCTP_INP_DECR_REF(inp);
3291 		}
3292 	}
3293 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
3294 }
3295 
3296 /* release sctp_inpcb unbind the port */
3297 void
3298 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3299 {
3300 	/*
3301 	 * Here we free a endpoint. We must find it (if it is in the Hash
3302 	 * table) and remove it from there. Then we must also find it in the
3303 	 * overall list and remove it from there. After all removals are
3304 	 * complete then any timer has to be stopped. Then start the actual
3305 	 * freeing. a) Any local lists. b) Any associations. c) The hash of
3306 	 * all associations. d) finally the ep itself.
3307 	 */
3308 	struct sctp_pcb *m;
3309 	struct sctp_tcb *asoc, *nasoc;
3310 	struct sctp_laddr *laddr, *nladdr;
3311 	struct inpcb *ip_pcb;
3312 	struct socket *so;
3313 	int being_refed = 0;
3314 	struct sctp_queued_to_read *sq, *nsq;
3315 	int cnt;
3316 	sctp_sharedkey_t *shared_key, *nshared_key;
3317 
3318 
3319 #ifdef SCTP_LOG_CLOSING
3320 	sctp_log_closing(inp, NULL, 0);
3321 #endif
3322 	SCTP_ITERATOR_LOCK();
3323 	/* mark any iterators on the list or being processed */
3324 	sctp_iterator_inp_being_freed(inp);
3325 	SCTP_ITERATOR_UNLOCK();
3326 	so = inp->sctp_socket;
3327 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3328 		/* been here before.. eeks.. get out of here */
3329 		SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3330 #ifdef SCTP_LOG_CLOSING
3331 		sctp_log_closing(inp, NULL, 1);
3332 #endif
3333 		return;
3334 	}
3335 	SCTP_ASOC_CREATE_LOCK(inp);
3336 	SCTP_INP_INFO_WLOCK();
3337 
3338 	SCTP_INP_WLOCK(inp);
3339 	if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3340 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3341 		/* socket is gone, so no more wakeups allowed */
3342 		inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3343 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3344 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3345 
3346 	}
3347 	/* First time through we have the socket lock, after that no more. */
3348 	sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3349 	    SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3350 
3351 	if (inp->control) {
3352 		sctp_m_freem(inp->control);
3353 		inp->control = NULL;
3354 	}
3355 	if (inp->pkt) {
3356 		sctp_m_freem(inp->pkt);
3357 		inp->pkt = NULL;
3358 	}
3359 	m = &inp->sctp_ep;
3360 	ip_pcb = &inp->ip_inp.inp;	/* we could just cast the main pointer
3361 					 * here but I will be nice :> (i.e.
3362 					 * ip_pcb = ep;) */
3363 	if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3364 		int cnt_in_sd;
3365 
3366 		cnt_in_sd = 0;
3367 		LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3368 			SCTP_TCB_LOCK(asoc);
3369 			if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3370 				/* Skip guys being freed */
3371 				cnt_in_sd++;
3372 				if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3373 					/*
3374 					 * Special case - we did not start a
3375 					 * kill timer on the asoc due to it
3376 					 * was not closed. So go ahead and
3377 					 * start it now.
3378 					 */
3379 					asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3380 					sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3381 				}
3382 				SCTP_TCB_UNLOCK(asoc);
3383 				continue;
3384 			}
3385 			if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3386 			    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3387 			    (asoc->asoc.total_output_queue_size == 0)) {
3388 				/*
3389 				 * If we have data in queue, we don't want
3390 				 * to just free since the app may have done,
3391 				 * send()/close or connect/send/close. And
3392 				 * it wants the data to get across first.
3393 				 */
3394 				/* Just abandon things in the front states */
3395 				if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3396 				    SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3397 					cnt_in_sd++;
3398 				}
3399 				continue;
3400 			}
3401 			/* Disconnect the socket please */
3402 			asoc->sctp_socket = NULL;
3403 			asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3404 			if ((asoc->asoc.size_on_reasm_queue > 0) ||
3405 			    (asoc->asoc.control_pdapi) ||
3406 			    (asoc->asoc.size_on_all_streams > 0) ||
3407 			    (so && (so->so_rcv.sb_cc > 0))
3408 			    ) {
3409 				/* Left with Data unread */
3410 				struct mbuf *op_err;
3411 
3412 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3413 				    0, M_DONTWAIT, 1, MT_DATA);
3414 				if (op_err) {
3415 					/* Fill in the user initiated abort */
3416 					struct sctp_paramhdr *ph;
3417 					uint32_t *ippp;
3418 
3419 					SCTP_BUF_LEN(op_err) =
3420 					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3421 					ph = mtod(op_err,
3422 					    struct sctp_paramhdr *);
3423 					ph->param_type = htons(
3424 					    SCTP_CAUSE_USER_INITIATED_ABT);
3425 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
3426 					ippp = (uint32_t *) (ph + 1);
3427 					*ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
3428 				}
3429 				asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3430 #if defined(SCTP_PANIC_ON_ABORT)
3431 				panic("inpcb_free does an abort");
3432 #endif
3433 				sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3434 				SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3435 				if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3436 				    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3437 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3438 				}
3439 				if (sctp_free_assoc(inp, asoc,
3440 				    SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3441 					cnt_in_sd++;
3442 				}
3443 				continue;
3444 			} else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3445 				    TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3446 				    (asoc->asoc.stream_queue_cnt == 0)
3447 			    ) {
3448 				if (asoc->asoc.locked_on_sending) {
3449 					goto abort_anyway;
3450 				}
3451 				if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3452 				    (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3453 					struct sctp_nets *netp;
3454 
3455 					if (asoc->asoc.alternate) {
3456 						netp = asoc->asoc.alternate;
3457 					} else {
3458 						netp = asoc->asoc.primary_destination;
3459 					}
3460 					/*
3461 					 * there is nothing queued to send,
3462 					 * so I send shutdown
3463 					 */
3464 					sctp_send_shutdown(asoc, netp);
3465 					if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3466 					    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3467 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3468 					}
3469 					SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3470 					SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3471 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3472 					    netp);
3473 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3474 					    asoc->asoc.primary_destination);
3475 					sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3476 				}
3477 			} else {
3478 				/* mark into shutdown pending */
3479 				struct sctp_stream_queue_pending *sp;
3480 
3481 				asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3482 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3483 				    asoc->asoc.primary_destination);
3484 				if (asoc->asoc.locked_on_sending) {
3485 					sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3486 					    sctp_streamhead);
3487 					if (sp == NULL) {
3488 						SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3489 						    asoc->asoc.locked_on_sending,
3490 						    asoc->asoc.locked_on_sending->stream_no);
3491 					} else {
3492 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
3493 							asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3494 					}
3495 				}
3496 				if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3497 				    TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3498 				    (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3499 					struct mbuf *op_err;
3500 
3501 			abort_anyway:
3502 					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3503 					    0, M_DONTWAIT, 1, MT_DATA);
3504 					if (op_err) {
3505 						/*
3506 						 * Fill in the user
3507 						 * initiated abort
3508 						 */
3509 						struct sctp_paramhdr *ph;
3510 						uint32_t *ippp;
3511 
3512 						SCTP_BUF_LEN(op_err) =
3513 						    (sizeof(struct sctp_paramhdr) +
3514 						    sizeof(uint32_t));
3515 						ph = mtod(op_err,
3516 						    struct sctp_paramhdr *);
3517 						ph->param_type = htons(
3518 						    SCTP_CAUSE_USER_INITIATED_ABT);
3519 						ph->param_length = htons(SCTP_BUF_LEN(op_err));
3520 						ippp = (uint32_t *) (ph + 1);
3521 						*ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
3522 					}
3523 					asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3524 #if defined(SCTP_PANIC_ON_ABORT)
3525 					panic("inpcb_free does an abort");
3526 #endif
3527 
3528 					sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3529 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3530 					if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3531 					    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3532 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3533 					}
3534 					if (sctp_free_assoc(inp, asoc,
3535 					    SCTP_PCBFREE_NOFORCE,
3536 					    SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3537 						cnt_in_sd++;
3538 					}
3539 					continue;
3540 				} else {
3541 					sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3542 				}
3543 			}
3544 			cnt_in_sd++;
3545 			SCTP_TCB_UNLOCK(asoc);
3546 		}
3547 		/* now is there some left in our SHUTDOWN state? */
3548 		if (cnt_in_sd) {
3549 #ifdef SCTP_LOG_CLOSING
3550 			sctp_log_closing(inp, NULL, 2);
3551 #endif
3552 			inp->sctp_socket = NULL;
3553 			SCTP_INP_WUNLOCK(inp);
3554 			SCTP_ASOC_CREATE_UNLOCK(inp);
3555 			SCTP_INP_INFO_WUNLOCK();
3556 			return;
3557 		}
3558 	}
3559 	inp->sctp_socket = NULL;
3560 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3561 	    SCTP_PCB_FLAGS_UNBOUND) {
3562 		/*
3563 		 * ok, this guy has been bound. It's port is somewhere in
3564 		 * the SCTP_BASE_INFO(hash table). Remove it!
3565 		 */
3566 		LIST_REMOVE(inp, sctp_hash);
3567 		inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3568 	}
3569 	/*
3570 	 * If there is a timer running to kill us, forget it, since it may
3571 	 * have a contest on the INP lock.. which would cause us to die ...
3572 	 */
3573 	cnt = 0;
3574 	LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3575 		SCTP_TCB_LOCK(asoc);
3576 		if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3577 			if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3578 				asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3579 				sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3580 			}
3581 			cnt++;
3582 			SCTP_TCB_UNLOCK(asoc);
3583 			continue;
3584 		}
3585 		/* Free associations that are NOT killing us */
3586 		if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3587 		    ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3588 			struct mbuf *op_err;
3589 			uint32_t *ippp;
3590 
3591 			op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3592 			    0, M_DONTWAIT, 1, MT_DATA);
3593 			if (op_err) {
3594 				/* Fill in the user initiated abort */
3595 				struct sctp_paramhdr *ph;
3596 
3597 				SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3598 				    sizeof(uint32_t));
3599 				ph = mtod(op_err, struct sctp_paramhdr *);
3600 				ph->param_type = htons(
3601 				    SCTP_CAUSE_USER_INITIATED_ABT);
3602 				ph->param_length = htons(SCTP_BUF_LEN(op_err));
3603 				ippp = (uint32_t *) (ph + 1);
3604 				*ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3605 
3606 			}
3607 			asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3608 #if defined(SCTP_PANIC_ON_ABORT)
3609 			panic("inpcb_free does an abort");
3610 #endif
3611 			sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3612 			SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3613 		} else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3614 			cnt++;
3615 			SCTP_TCB_UNLOCK(asoc);
3616 			continue;
3617 		}
3618 		if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3619 		    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3620 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3621 		}
3622 		if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3623 			cnt++;
3624 		}
3625 	}
3626 	if (cnt) {
3627 		/* Ok we have someone out there that will kill us */
3628 		(void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3629 #ifdef SCTP_LOG_CLOSING
3630 		sctp_log_closing(inp, NULL, 3);
3631 #endif
3632 		SCTP_INP_WUNLOCK(inp);
3633 		SCTP_ASOC_CREATE_UNLOCK(inp);
3634 		SCTP_INP_INFO_WUNLOCK();
3635 		return;
3636 	}
3637 	if (SCTP_INP_LOCK_CONTENDED(inp))
3638 		being_refed++;
3639 	if (SCTP_INP_READ_CONTENDED(inp))
3640 		being_refed++;
3641 	if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
3642 		being_refed++;
3643 
3644 	if ((inp->refcount) ||
3645 	    (being_refed) ||
3646 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3647 		(void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3648 #ifdef SCTP_LOG_CLOSING
3649 		sctp_log_closing(inp, NULL, 4);
3650 #endif
3651 		sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3652 		SCTP_INP_WUNLOCK(inp);
3653 		SCTP_ASOC_CREATE_UNLOCK(inp);
3654 		SCTP_INP_INFO_WUNLOCK();
3655 		return;
3656 	}
3657 	inp->sctp_ep.signature_change.type = 0;
3658 	inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3659 	/*
3660 	 * Remove it from the list .. last thing we need a lock for.
3661 	 */
3662 	LIST_REMOVE(inp, sctp_list);
3663 	SCTP_INP_WUNLOCK(inp);
3664 	SCTP_ASOC_CREATE_UNLOCK(inp);
3665 	SCTP_INP_INFO_WUNLOCK();
3666 	/*
3667 	 * Now we release all locks. Since this INP cannot be found anymore
3668 	 * except possibly by the kill timer that might be running. We call
3669 	 * the drain function here. It should hit the case were it sees the
3670 	 * ACTIVE flag cleared and exit out freeing us to proceed and
3671 	 * destroy everything.
3672 	 */
3673 	if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
3674 		(void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
3675 	} else {
3676 		/* Probably un-needed */
3677 		(void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3678 	}
3679 
3680 #ifdef SCTP_LOG_CLOSING
3681 	sctp_log_closing(inp, NULL, 5);
3682 #endif
3683 
3684 
3685 	if ((inp->sctp_asocidhash) != NULL) {
3686 		SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3687 		inp->sctp_asocidhash = NULL;
3688 	}
3689 	/* sa_ignore FREED_MEMORY */
3690 	TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3691 		/* Its only abandoned if it had data left */
3692 		if (sq->length)
3693 			SCTP_STAT_INCR(sctps_left_abandon);
3694 
3695 		TAILQ_REMOVE(&inp->read_queue, sq, next);
3696 		sctp_free_remote_addr(sq->whoFrom);
3697 		if (so)
3698 			so->so_rcv.sb_cc -= sq->length;
3699 		if (sq->data) {
3700 			sctp_m_freem(sq->data);
3701 			sq->data = NULL;
3702 		}
3703 		/*
3704 		 * no need to free the net count, since at this point all
3705 		 * assoc's are gone.
3706 		 */
3707 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3708 		SCTP_DECR_READQ_COUNT();
3709 	}
3710 	/* Now the sctp_pcb things */
3711 	/*
3712 	 * free each asoc if it is not already closed/free. we can't use the
3713 	 * macro here since le_next will get freed as part of the
3714 	 * sctp_free_assoc() call.
3715 	 */
3716 	cnt = 0;
3717 	if (so) {
3718 #ifdef IPSEC
3719 		ipsec_delete_pcbpolicy(ip_pcb);
3720 #endif				/* IPSEC */
3721 
3722 		/* Unlocks not needed since the socket is gone now */
3723 	}
3724 	if (ip_pcb->inp_options) {
3725 		(void)sctp_m_free(ip_pcb->inp_options);
3726 		ip_pcb->inp_options = 0;
3727 	}
3728 #ifdef INET6
3729 	if (ip_pcb->inp_vflag & INP_IPV6) {
3730 		struct in6pcb *in6p;
3731 
3732 		in6p = (struct in6pcb *)inp;
3733 		ip6_freepcbopts(in6p->in6p_outputopts);
3734 	}
3735 #endif				/* INET6 */
3736 	ip_pcb->inp_vflag = 0;
3737 	/* free up authentication fields */
3738 	if (inp->sctp_ep.local_auth_chunks != NULL)
3739 		sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3740 	if (inp->sctp_ep.local_hmacs != NULL)
3741 		sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3742 
3743 	LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3744 		LIST_REMOVE(shared_key, next);
3745 		sctp_free_sharedkey(shared_key);
3746 		/* sa_ignore FREED_MEMORY */
3747 	}
3748 
3749 	/*
3750 	 * if we have an address list the following will free the list of
3751 	 * ifaddr's that are set into this ep. Again macro limitations here,
3752 	 * since the LIST_FOREACH could be a bad idea.
3753 	 */
3754 	LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3755 		sctp_remove_laddr(laddr);
3756 	}
3757 
3758 #ifdef SCTP_TRACK_FREED_ASOCS
3759 	/* TEMP CODE */
3760 	LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
3761 		LIST_REMOVE(asoc, sctp_tcblist);
3762 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3763 		SCTP_DECR_ASOC_COUNT();
3764 	}
3765 	/* *** END TEMP CODE *** */
3766 #endif
3767 	/* Now lets see about freeing the EP hash table. */
3768 	if (inp->sctp_tcbhash != NULL) {
3769 		SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3770 		inp->sctp_tcbhash = NULL;
3771 	}
3772 	/* Now we must put the ep memory back into the zone pool */
3773 	INP_LOCK_DESTROY(&inp->ip_inp.inp);
3774 	SCTP_INP_LOCK_DESTROY(inp);
3775 	SCTP_INP_READ_DESTROY(inp);
3776 	SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3777 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3778 	SCTP_DECR_EP_COUNT();
3779 }
3780 
3781 
3782 struct sctp_nets *
3783 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3784 {
3785 	struct sctp_nets *net;
3786 
3787 	/* locate the address */
3788 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3789 		if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3790 			return (net);
3791 	}
3792 	return (NULL);
3793 }
3794 
3795 
3796 int
3797 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3798 {
3799 	struct sctp_ifa *sctp_ifa;
3800 
3801 	sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3802 	if (sctp_ifa) {
3803 		return (1);
3804 	} else {
3805 		return (0);
3806 	}
3807 }
3808 
3809 /*
3810  * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3811  * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3812  * stats of stuff.
3813  */
3814 int
3815 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3816     struct sctp_nets **netp, int set_scope, int from)
3817 {
3818 	/*
3819 	 * The following is redundant to the same lines in the
3820 	 * sctp_aloc_assoc() but is needed since others call the add address
3821 	 * function
3822 	 */
3823 	struct sctp_nets *net, *netfirst;
3824 	int addr_inscope;
3825 
3826 	SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3827 	    from);
3828 	SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3829 
3830 	netfirst = sctp_findnet(stcb, newaddr);
3831 	if (netfirst) {
3832 		/*
3833 		 * Lie and return ok, we don't want to make the association
3834 		 * go away for this behavior. It will happen in the TCP
3835 		 * model in a connected socket. It does not reach the hash
3836 		 * table until after the association is built so it can't be
3837 		 * found. Mark as reachable, since the initial creation will
3838 		 * have been cleared and the NOT_IN_ASSOC flag will have
3839 		 * been added... and we don't want to end up removing it
3840 		 * back out.
3841 		 */
3842 		if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3843 			netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3844 			    SCTP_ADDR_UNCONFIRMED);
3845 		} else {
3846 			netfirst->dest_state = SCTP_ADDR_REACHABLE;
3847 		}
3848 
3849 		return (0);
3850 	}
3851 	addr_inscope = 1;
3852 	switch (newaddr->sa_family) {
3853 #ifdef INET
3854 	case AF_INET:
3855 		{
3856 			struct sockaddr_in *sin;
3857 
3858 			sin = (struct sockaddr_in *)newaddr;
3859 			if (sin->sin_addr.s_addr == 0) {
3860 				/* Invalid address */
3861 				return (-1);
3862 			}
3863 			/* zero out the bzero area */
3864 			memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3865 
3866 			/* assure len is set */
3867 			sin->sin_len = sizeof(struct sockaddr_in);
3868 			if (set_scope) {
3869 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3870 				stcb->ipv4_local_scope = 1;
3871 #else
3872 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3873 					stcb->asoc.ipv4_local_scope = 1;
3874 				}
3875 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
3876 			} else {
3877 				/* Validate the address is in scope */
3878 				if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3879 				    (stcb->asoc.ipv4_local_scope == 0)) {
3880 					addr_inscope = 0;
3881 				}
3882 			}
3883 			break;
3884 		}
3885 #endif
3886 #ifdef INET6
3887 	case AF_INET6:
3888 		{
3889 			struct sockaddr_in6 *sin6;
3890 
3891 			sin6 = (struct sockaddr_in6 *)newaddr;
3892 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3893 				/* Invalid address */
3894 				return (-1);
3895 			}
3896 			/* assure len is set */
3897 			sin6->sin6_len = sizeof(struct sockaddr_in6);
3898 			if (set_scope) {
3899 				if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3900 					stcb->asoc.loopback_scope = 1;
3901 					stcb->asoc.local_scope = 0;
3902 					stcb->asoc.ipv4_local_scope = 1;
3903 					stcb->asoc.site_scope = 1;
3904 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3905 					/*
3906 					 * If the new destination is a
3907 					 * LINK_LOCAL we must have common
3908 					 * site scope. Don't set the local
3909 					 * scope since we may not share all
3910 					 * links, only loopback can do this.
3911 					 * Links on the local network would
3912 					 * also be on our private network
3913 					 * for v4 too.
3914 					 */
3915 					stcb->asoc.ipv4_local_scope = 1;
3916 					stcb->asoc.site_scope = 1;
3917 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3918 					/*
3919 					 * If the new destination is
3920 					 * SITE_LOCAL then we must have site
3921 					 * scope in common.
3922 					 */
3923 					stcb->asoc.site_scope = 1;
3924 				}
3925 			} else {
3926 				/* Validate the address is in scope */
3927 				if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3928 				    (stcb->asoc.loopback_scope == 0)) {
3929 					addr_inscope = 0;
3930 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3931 				    (stcb->asoc.local_scope == 0)) {
3932 					addr_inscope = 0;
3933 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3934 				    (stcb->asoc.site_scope == 0)) {
3935 					addr_inscope = 0;
3936 				}
3937 			}
3938 			break;
3939 		}
3940 #endif
3941 	default:
3942 		/* not supported family type */
3943 		return (-1);
3944 	}
3945 	net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3946 	if (net == NULL) {
3947 		return (-1);
3948 	}
3949 	SCTP_INCR_RADDR_COUNT();
3950 	bzero(net, sizeof(struct sctp_nets));
3951 	(void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3952 	memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3953 	switch (newaddr->sa_family) {
3954 #ifdef INET
3955 	case AF_INET:
3956 		((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3957 		break;
3958 #endif
3959 #ifdef INET6
3960 	case AF_INET6:
3961 		((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3962 		break;
3963 #endif
3964 	default:
3965 		break;
3966 	}
3967 	net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3968 	if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3969 		stcb->asoc.loopback_scope = 1;
3970 		stcb->asoc.ipv4_local_scope = 1;
3971 		stcb->asoc.local_scope = 0;
3972 		stcb->asoc.site_scope = 1;
3973 		addr_inscope = 1;
3974 	}
3975 	net->failure_threshold = stcb->asoc.def_net_failure;
3976 	net->pf_threshold = stcb->asoc.def_net_pf_threshold;
3977 	if (addr_inscope == 0) {
3978 		net->dest_state = (SCTP_ADDR_REACHABLE |
3979 		    SCTP_ADDR_OUT_OF_SCOPE);
3980 	} else {
3981 		if (from == SCTP_ADDR_IS_CONFIRMED)
3982 			/* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3983 			net->dest_state = SCTP_ADDR_REACHABLE;
3984 		else
3985 			net->dest_state = SCTP_ADDR_REACHABLE |
3986 			    SCTP_ADDR_UNCONFIRMED;
3987 	}
3988 	/*
3989 	 * We set this to 0, the timer code knows that this means its an
3990 	 * initial value
3991 	 */
3992 	net->rto_needed = 1;
3993 	net->RTO = 0;
3994 	net->RTO_measured = 0;
3995 	stcb->asoc.numnets++;
3996 	*(&net->ref_count) = 1;
3997 	net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
3998 	if (SCTP_BASE_SYSCTL(sctp_udp_tunneling_for_client_enable)) {
3999 		net->port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4000 	} else {
4001 		net->port = 0;
4002 	}
4003 	net->dscp = stcb->asoc.default_dscp;
4004 #ifdef INET6
4005 	net->flowlabel = stcb->asoc.default_flowlabel;
4006 #endif
4007 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4008 		net->dest_state |= SCTP_ADDR_NOHB;
4009 	} else {
4010 		net->dest_state &= ~SCTP_ADDR_NOHB;
4011 	}
4012 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4013 		net->dest_state |= SCTP_ADDR_NO_PMTUD;
4014 	} else {
4015 		net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4016 	}
4017 	net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4018 	/* Init the timer structure */
4019 	SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4020 	SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4021 	SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4022 
4023 	/* Now generate a route for this guy */
4024 #ifdef INET6
4025 	/* KAME hack: embed scopeid */
4026 	if (newaddr->sa_family == AF_INET6) {
4027 		struct sockaddr_in6 *sin6;
4028 
4029 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4030 		(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4031 		sin6->sin6_scope_id = 0;
4032 	}
4033 #endif
4034 	SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
4035 
4036 	if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4037 		/* Get source address */
4038 		net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4039 		    stcb,
4040 		    (sctp_route_t *) & net->ro,
4041 		    net,
4042 		    0,
4043 		    stcb->asoc.vrf_id);
4044 		/* Now get the interface MTU */
4045 		if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
4046 			net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4047 		}
4048 		if (net->mtu > 0) {
4049 			uint32_t rmtu;
4050 
4051 			rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4052 			if (rmtu == 0) {
4053 				/*
4054 				 * Start things off to match mtu of
4055 				 * interface please.
4056 				 */
4057 				SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4058 				    net->ro.ro_rt, net->mtu);
4059 			} else {
4060 				/*
4061 				 * we take the route mtu over the interface,
4062 				 * since the route may be leading out the
4063 				 * loopback, or a different interface.
4064 				 */
4065 				net->mtu = rmtu;
4066 			}
4067 		}
4068 	}
4069 	if (net->mtu == 0) {
4070 		switch (newaddr->sa_family) {
4071 #ifdef INET
4072 		case AF_INET:
4073 			net->mtu = SCTP_DEFAULT_MTU;
4074 			break;
4075 #endif
4076 #ifdef INET6
4077 		case AF_INET6:
4078 			net->mtu = 1280;
4079 			break;
4080 #endif
4081 		default:
4082 			break;
4083 		}
4084 	}
4085 	if (net->port) {
4086 		net->mtu -= (uint32_t) sizeof(struct udphdr);
4087 	}
4088 	if (from == SCTP_ALLOC_ASOC) {
4089 		stcb->asoc.smallest_mtu = net->mtu;
4090 	}
4091 	if (stcb->asoc.smallest_mtu > net->mtu) {
4092 		stcb->asoc.smallest_mtu = net->mtu;
4093 	}
4094 #ifdef INET6
4095 	if (newaddr->sa_family == AF_INET6) {
4096 		struct sockaddr_in6 *sin6;
4097 
4098 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4099 		(void)sa6_recoverscope(sin6);
4100 	}
4101 #endif
4102 
4103 	/* JRS - Use the congestion control given in the CC module */
4104 	if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
4105 		(*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
4106 
4107 	/*
4108 	 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4109 	 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4110 	 */
4111 	net->find_pseudo_cumack = 1;
4112 	net->find_rtx_pseudo_cumack = 1;
4113 	net->src_addr_selected = 0;
4114 	/* Choose an initial flowid. */
4115 	net->flowid = stcb->asoc.my_vtag ^
4116 	    ntohs(stcb->rport) ^
4117 	    ntohs(stcb->sctp_ep->sctp_lport);
4118 #ifdef INVARIANTS
4119 	net->flowidset = 1;
4120 #endif
4121 	if (netp) {
4122 		*netp = net;
4123 	}
4124 	netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4125 	if (net->ro.ro_rt == NULL) {
4126 		/* Since we have no route put it at the back */
4127 		TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4128 	} else if (netfirst == NULL) {
4129 		/* We are the first one in the pool. */
4130 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4131 	} else if (netfirst->ro.ro_rt == NULL) {
4132 		/*
4133 		 * First one has NO route. Place this one ahead of the first
4134 		 * one.
4135 		 */
4136 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4137 	} else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4138 		/*
4139 		 * This one has a different interface than the one at the
4140 		 * top of the list. Place it ahead.
4141 		 */
4142 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4143 	} else {
4144 		/*
4145 		 * Ok we have the same interface as the first one. Move
4146 		 * forward until we find either a) one with a NULL route...
4147 		 * insert ahead of that b) one with a different ifp.. insert
4148 		 * after that. c) end of the list.. insert at the tail.
4149 		 */
4150 		struct sctp_nets *netlook;
4151 
4152 		do {
4153 			netlook = TAILQ_NEXT(netfirst, sctp_next);
4154 			if (netlook == NULL) {
4155 				/* End of the list */
4156 				TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4157 				break;
4158 			} else if (netlook->ro.ro_rt == NULL) {
4159 				/* next one has NO route */
4160 				TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4161 				break;
4162 			} else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4163 				TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4164 				    net, sctp_next);
4165 				break;
4166 			}
4167 			/* Shift forward */
4168 			netfirst = netlook;
4169 		} while (netlook != NULL);
4170 	}
4171 
4172 	/* got to have a primary set */
4173 	if (stcb->asoc.primary_destination == 0) {
4174 		stcb->asoc.primary_destination = net;
4175 	} else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4176 		    (net->ro.ro_rt) &&
4177 	    ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4178 		/* No route to current primary adopt new primary */
4179 		stcb->asoc.primary_destination = net;
4180 	}
4181 	/* Validate primary is first */
4182 	net = TAILQ_FIRST(&stcb->asoc.nets);
4183 	if ((net != stcb->asoc.primary_destination) &&
4184 	    (stcb->asoc.primary_destination)) {
4185 		/*
4186 		 * first one on the list is NOT the primary sctp_cmpaddr()
4187 		 * is much more efficient if the primary is the first on the
4188 		 * list, make it so.
4189 		 */
4190 		TAILQ_REMOVE(&stcb->asoc.nets,
4191 		    stcb->asoc.primary_destination, sctp_next);
4192 		TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4193 		    stcb->asoc.primary_destination, sctp_next);
4194 	}
4195 	return (0);
4196 }
4197 
4198 
4199 static uint32_t
4200 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4201 {
4202 	uint32_t id;
4203 	struct sctpasochead *head;
4204 	struct sctp_tcb *lstcb;
4205 
4206 	SCTP_INP_WLOCK(inp);
4207 try_again:
4208 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4209 		/* TSNH */
4210 		SCTP_INP_WUNLOCK(inp);
4211 		return (0);
4212 	}
4213 	/*
4214 	 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4215 	 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4216 	 */
4217 	if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4218 		inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4219 	}
4220 	id = inp->sctp_associd_counter;
4221 	inp->sctp_associd_counter++;
4222 	lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t) id, 0);
4223 	if (lstcb) {
4224 		goto try_again;
4225 	}
4226 	head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4227 	LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4228 	stcb->asoc.in_asocid_hash = 1;
4229 	SCTP_INP_WUNLOCK(inp);
4230 	return id;
4231 }
4232 
4233 /*
4234  * allocate an association and add it to the endpoint. The caller must be
4235  * careful to add all additional addresses once they are know right away or
4236  * else the assoc will be may experience a blackout scenario.
4237  */
4238 struct sctp_tcb *
4239 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4240     int *error, uint32_t override_tag, uint32_t vrf_id,
4241     struct thread *p
4242 )
4243 {
4244 	/* note the p argument is only valid in unbound sockets */
4245 
4246 	struct sctp_tcb *stcb;
4247 	struct sctp_association *asoc;
4248 	struct sctpasochead *head;
4249 	uint16_t rport;
4250 	int err;
4251 
4252 	/*
4253 	 * Assumption made here: Caller has done a
4254 	 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4255 	 * address does not exist already.
4256 	 */
4257 	if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4258 		/* Hit max assoc, sorry no more */
4259 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4260 		*error = ENOBUFS;
4261 		return (NULL);
4262 	}
4263 	if (firstaddr == NULL) {
4264 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4265 		*error = EINVAL;
4266 		return (NULL);
4267 	}
4268 	SCTP_INP_RLOCK(inp);
4269 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4270 	    ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4271 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4272 		/*
4273 		 * If its in the TCP pool, its NOT allowed to create an
4274 		 * association. The parent listener needs to call
4275 		 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4276 		 * off, or connected one does this.. its an error.
4277 		 */
4278 		SCTP_INP_RUNLOCK(inp);
4279 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4280 		*error = EINVAL;
4281 		return (NULL);
4282 	}
4283 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4284 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4285 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4286 		    (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4287 			SCTP_INP_RUNLOCK(inp);
4288 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4289 			*error = EINVAL;
4290 			return (NULL);
4291 		}
4292 	}
4293 	SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4294 #ifdef SCTP_DEBUG
4295 	if (firstaddr) {
4296 		SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4297 		switch (firstaddr->sa_family) {
4298 #ifdef INET
4299 		case AF_INET:
4300 			SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4301 			    ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4302 			break;
4303 #endif
4304 #ifdef INET6
4305 		case AF_INET6:
4306 			SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4307 			    ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4308 			break;
4309 #endif
4310 		default:
4311 			break;
4312 		}
4313 	} else {
4314 		SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4315 	}
4316 #endif				/* SCTP_DEBUG */
4317 	switch (firstaddr->sa_family) {
4318 #ifdef INET
4319 	case AF_INET:
4320 		{
4321 			struct sockaddr_in *sin;
4322 
4323 			sin = (struct sockaddr_in *)firstaddr;
4324 			if ((ntohs(sin->sin_port) == 0) ||
4325 			    (sin->sin_addr.s_addr == INADDR_ANY) ||
4326 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4327 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
4328 				/* Invalid address */
4329 				SCTP_INP_RUNLOCK(inp);
4330 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4331 				*error = EINVAL;
4332 				return (NULL);
4333 			}
4334 			rport = sin->sin_port;
4335 			break;
4336 		}
4337 #endif
4338 #ifdef INET6
4339 	case AF_INET6:
4340 		{
4341 			struct sockaddr_in6 *sin6;
4342 
4343 			sin6 = (struct sockaddr_in6 *)firstaddr;
4344 			if ((ntohs(sin6->sin6_port) == 0) ||
4345 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4346 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4347 				/* Invalid address */
4348 				SCTP_INP_RUNLOCK(inp);
4349 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4350 				*error = EINVAL;
4351 				return (NULL);
4352 			}
4353 			rport = sin6->sin6_port;
4354 			break;
4355 		}
4356 #endif
4357 	default:
4358 		/* not supported family type */
4359 		SCTP_INP_RUNLOCK(inp);
4360 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4361 		*error = EINVAL;
4362 		return (NULL);
4363 	}
4364 	SCTP_INP_RUNLOCK(inp);
4365 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4366 		/*
4367 		 * If you have not performed a bind, then we need to do the
4368 		 * ephemeral bind for you.
4369 		 */
4370 		if ((err = sctp_inpcb_bind(inp->sctp_socket,
4371 		    (struct sockaddr *)NULL,
4372 		    (struct sctp_ifa *)NULL,
4373 		    p
4374 		    ))) {
4375 			/* bind error, probably perm */
4376 			*error = err;
4377 			return (NULL);
4378 		}
4379 	}
4380 	stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4381 	if (stcb == NULL) {
4382 		/* out of memory? */
4383 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4384 		*error = ENOMEM;
4385 		return (NULL);
4386 	}
4387 	SCTP_INCR_ASOC_COUNT();
4388 
4389 	bzero(stcb, sizeof(*stcb));
4390 	asoc = &stcb->asoc;
4391 
4392 	asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4393 	SCTP_TCB_LOCK_INIT(stcb);
4394 	SCTP_TCB_SEND_LOCK_INIT(stcb);
4395 	stcb->rport = rport;
4396 	/* setup back pointer's */
4397 	stcb->sctp_ep = inp;
4398 	stcb->sctp_socket = inp->sctp_socket;
4399 	if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4400 		/* failed */
4401 		SCTP_TCB_LOCK_DESTROY(stcb);
4402 		SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4403 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4404 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4405 		SCTP_DECR_ASOC_COUNT();
4406 		*error = err;
4407 		return (NULL);
4408 	}
4409 	/* and the port */
4410 	SCTP_INP_INFO_WLOCK();
4411 	SCTP_INP_WLOCK(inp);
4412 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4413 		/* inpcb freed while alloc going on */
4414 		SCTP_TCB_LOCK_DESTROY(stcb);
4415 		SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4416 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4417 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4418 		SCTP_INP_WUNLOCK(inp);
4419 		SCTP_INP_INFO_WUNLOCK();
4420 		SCTP_DECR_ASOC_COUNT();
4421 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4422 		*error = EINVAL;
4423 		return (NULL);
4424 	}
4425 	SCTP_TCB_LOCK(stcb);
4426 
4427 	/* now that my_vtag is set, add it to the hash */
4428 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4429 	/* put it in the bucket in the vtag hash of assoc's for the system */
4430 	LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4431 	SCTP_INP_INFO_WUNLOCK();
4432 
4433 	if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4434 		/* failure.. memory error? */
4435 		if (asoc->strmout) {
4436 			SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4437 			asoc->strmout = NULL;
4438 		}
4439 		if (asoc->mapping_array) {
4440 			SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4441 			asoc->mapping_array = NULL;
4442 		}
4443 		if (asoc->nr_mapping_array) {
4444 			SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4445 			asoc->nr_mapping_array = NULL;
4446 		}
4447 		SCTP_DECR_ASOC_COUNT();
4448 		SCTP_TCB_LOCK_DESTROY(stcb);
4449 		SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4450 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4451 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4452 		SCTP_INP_WUNLOCK(inp);
4453 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4454 		*error = ENOBUFS;
4455 		return (NULL);
4456 	}
4457 	/* Init all the timers */
4458 	SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4459 	SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4460 	SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4461 	SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4462 	SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4463 	SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4464 	SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4465 
4466 	LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4467 	/* now file the port under the hash as well */
4468 	if (inp->sctp_tcbhash != NULL) {
4469 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4470 		    inp->sctp_hashmark)];
4471 		LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4472 	}
4473 	SCTP_INP_WUNLOCK(inp);
4474 	SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4475 	return (stcb);
4476 }
4477 
4478 
4479 void
4480 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4481 {
4482 	struct sctp_association *asoc;
4483 
4484 	asoc = &stcb->asoc;
4485 	asoc->numnets--;
4486 	TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4487 	if (net == asoc->primary_destination) {
4488 		/* Reset primary */
4489 		struct sctp_nets *lnet;
4490 
4491 		lnet = TAILQ_FIRST(&asoc->nets);
4492 		/*
4493 		 * Mobility adaptation Ideally, if deleted destination is
4494 		 * the primary, it becomes a fast retransmission trigger by
4495 		 * the subsequent SET PRIMARY. (by micchie)
4496 		 */
4497 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4498 		    SCTP_MOBILITY_BASE) ||
4499 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
4500 		    SCTP_MOBILITY_FASTHANDOFF)) {
4501 			SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4502 			if (asoc->deleted_primary != NULL) {
4503 				SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4504 				goto out;
4505 			}
4506 			asoc->deleted_primary = net;
4507 			atomic_add_int(&net->ref_count, 1);
4508 			memset(&net->lastsa, 0, sizeof(net->lastsa));
4509 			memset(&net->lastsv, 0, sizeof(net->lastsv));
4510 			sctp_mobility_feature_on(stcb->sctp_ep,
4511 			    SCTP_MOBILITY_PRIM_DELETED);
4512 			sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4513 			    stcb->sctp_ep, stcb, NULL);
4514 		}
4515 out:
4516 		/* Try to find a confirmed primary */
4517 		asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4518 	}
4519 	if (net == asoc->last_data_chunk_from) {
4520 		/* Reset primary */
4521 		asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4522 	}
4523 	if (net == asoc->last_control_chunk_from) {
4524 		/* Clear net */
4525 		asoc->last_control_chunk_from = NULL;
4526 	}
4527 	if (net == stcb->asoc.alternate) {
4528 		sctp_free_remote_addr(stcb->asoc.alternate);
4529 		stcb->asoc.alternate = NULL;
4530 	}
4531 	sctp_free_remote_addr(net);
4532 }
4533 
4534 /*
4535  * remove a remote endpoint address from an association, it will fail if the
4536  * address does not exist.
4537  */
4538 int
4539 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4540 {
4541 	/*
4542 	 * Here we need to remove a remote address. This is quite simple, we
4543 	 * first find it in the list of address for the association
4544 	 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4545 	 * on that item. Note we do not allow it to be removed if there are
4546 	 * no other addresses.
4547 	 */
4548 	struct sctp_association *asoc;
4549 	struct sctp_nets *net, *nnet;
4550 
4551 	asoc = &stcb->asoc;
4552 
4553 	/* locate the address */
4554 	TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4555 		if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4556 			continue;
4557 		}
4558 		if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4559 		    remaddr)) {
4560 			/* we found the guy */
4561 			if (asoc->numnets < 2) {
4562 				/* Must have at LEAST two remote addresses */
4563 				return (-1);
4564 			} else {
4565 				sctp_remove_net(stcb, net);
4566 				return (0);
4567 			}
4568 		}
4569 	}
4570 	/* not found. */
4571 	return (-2);
4572 }
4573 
4574 void
4575 sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4576 {
4577 	struct sctpvtaghead *chain;
4578 	struct sctp_tagblock *twait_block;
4579 	int found = 0;
4580 	int i;
4581 
4582 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4583 	if (!LIST_EMPTY(chain)) {
4584 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4585 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4586 				if ((twait_block->vtag_block[i].v_tag == tag) &&
4587 				    (twait_block->vtag_block[i].lport == lport) &&
4588 				    (twait_block->vtag_block[i].rport == rport)) {
4589 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
4590 					twait_block->vtag_block[i].v_tag = 0;
4591 					twait_block->vtag_block[i].lport = 0;
4592 					twait_block->vtag_block[i].rport = 0;
4593 					found = 1;
4594 					break;
4595 				}
4596 			}
4597 			if (found)
4598 				break;
4599 		}
4600 	}
4601 }
4602 
4603 int
4604 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4605 {
4606 	struct sctpvtaghead *chain;
4607 	struct sctp_tagblock *twait_block;
4608 	int found = 0;
4609 	int i;
4610 
4611 	SCTP_INP_INFO_WLOCK();
4612 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4613 	if (!LIST_EMPTY(chain)) {
4614 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4615 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4616 				if ((twait_block->vtag_block[i].v_tag == tag) &&
4617 				    (twait_block->vtag_block[i].lport == lport) &&
4618 				    (twait_block->vtag_block[i].rport == rport)) {
4619 					found = 1;
4620 					break;
4621 				}
4622 			}
4623 			if (found)
4624 				break;
4625 		}
4626 	}
4627 	SCTP_INP_INFO_WUNLOCK();
4628 	return (found);
4629 }
4630 
4631 
4632 void
4633 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
4634 {
4635 	struct sctpvtaghead *chain;
4636 	struct sctp_tagblock *twait_block;
4637 	struct timeval now;
4638 	int set, i;
4639 
4640 	if (time == 0) {
4641 		/* Its disabled */
4642 		return;
4643 	}
4644 	(void)SCTP_GETTIME_TIMEVAL(&now);
4645 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4646 	set = 0;
4647 	if (!LIST_EMPTY(chain)) {
4648 		/* Block(s) present, lets find space, and expire on the fly */
4649 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4650 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4651 				if ((twait_block->vtag_block[i].v_tag == 0) &&
4652 				    !set) {
4653 					twait_block->vtag_block[i].tv_sec_at_expire =
4654 					    now.tv_sec + time;
4655 					twait_block->vtag_block[i].v_tag = tag;
4656 					twait_block->vtag_block[i].lport = lport;
4657 					twait_block->vtag_block[i].rport = rport;
4658 					set = 1;
4659 				} else if ((twait_block->vtag_block[i].v_tag) &&
4660 				    ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4661 					/* Audit expires this guy */
4662 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
4663 					twait_block->vtag_block[i].v_tag = 0;
4664 					twait_block->vtag_block[i].lport = 0;
4665 					twait_block->vtag_block[i].rport = 0;
4666 					if (set == 0) {
4667 						/* Reuse it for my new tag */
4668 						twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
4669 						twait_block->vtag_block[i].v_tag = tag;
4670 						twait_block->vtag_block[i].lport = lport;
4671 						twait_block->vtag_block[i].rport = rport;
4672 						set = 1;
4673 					}
4674 				}
4675 			}
4676 			if (set) {
4677 				/*
4678 				 * We only do up to the block where we can
4679 				 * place our tag for audits
4680 				 */
4681 				break;
4682 			}
4683 		}
4684 	}
4685 	/* Need to add a new block to chain */
4686 	if (!set) {
4687 		SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4688 		    sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4689 		if (twait_block == NULL) {
4690 #ifdef INVARIANTS
4691 			panic("Can not alloc tagblock");
4692 #endif
4693 			return;
4694 		}
4695 		memset(twait_block, 0, sizeof(struct sctp_tagblock));
4696 		LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4697 		twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4698 		twait_block->vtag_block[0].v_tag = tag;
4699 		twait_block->vtag_block[0].lport = lport;
4700 		twait_block->vtag_block[0].rport = rport;
4701 	}
4702 }
4703 
4704 
4705 
4706 /*-
4707  * Free the association after un-hashing the remote port. This
4708  * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4709  * expect that the input to this function IS a locked TCB.
4710  * It will return 0, if it did NOT destroy the association (instead
4711  * it unlocks it. It will return NON-zero if it either destroyed the
4712  * association OR the association is already destroyed.
4713  */
4714 int
4715 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4716 {
4717 	int i;
4718 	struct sctp_association *asoc;
4719 	struct sctp_nets *net, *nnet;
4720 	struct sctp_laddr *laddr, *naddr;
4721 	struct sctp_tmit_chunk *chk, *nchk;
4722 	struct sctp_asconf_addr *aparam, *naparam;
4723 	struct sctp_asconf_ack *aack, *naack;
4724 	struct sctp_stream_reset_list *strrst, *nstrrst;
4725 	struct sctp_queued_to_read *sq, *nsq;
4726 	struct sctp_stream_queue_pending *sp, *nsp;
4727 	sctp_sharedkey_t *shared_key, *nshared_key;
4728 	struct socket *so;
4729 
4730 	/* first, lets purge the entry from the hash table. */
4731 
4732 #ifdef SCTP_LOG_CLOSING
4733 	sctp_log_closing(inp, stcb, 6);
4734 #endif
4735 	if (stcb->asoc.state == 0) {
4736 #ifdef SCTP_LOG_CLOSING
4737 		sctp_log_closing(inp, NULL, 7);
4738 #endif
4739 		/* there is no asoc, really TSNH :-0 */
4740 		return (1);
4741 	}
4742 	if (stcb->asoc.alternate) {
4743 		sctp_free_remote_addr(stcb->asoc.alternate);
4744 		stcb->asoc.alternate = NULL;
4745 	}
4746 	/* TEMP CODE */
4747 	if (stcb->freed_from_where == 0) {
4748 		/* Only record the first place free happened from */
4749 		stcb->freed_from_where = from_location;
4750 	}
4751 	/* TEMP CODE */
4752 
4753 	asoc = &stcb->asoc;
4754 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4755 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4756 		/* nothing around */
4757 		so = NULL;
4758 	else
4759 		so = inp->sctp_socket;
4760 
4761 	/*
4762 	 * We used timer based freeing if a reader or writer is in the way.
4763 	 * So we first check if we are actually being called from a timer,
4764 	 * if so we abort early if a reader or writer is still in the way.
4765 	 */
4766 	if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4767 	    (from_inpcbfree == SCTP_NORMAL_PROC)) {
4768 		/*
4769 		 * is it the timer driving us? if so are the reader/writers
4770 		 * gone?
4771 		 */
4772 		if (stcb->asoc.refcnt) {
4773 			/* nope, reader or writer in the way */
4774 			sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4775 			/* no asoc destroyed */
4776 			SCTP_TCB_UNLOCK(stcb);
4777 #ifdef SCTP_LOG_CLOSING
4778 			sctp_log_closing(inp, stcb, 8);
4779 #endif
4780 			return (0);
4781 		}
4782 	}
4783 	/* now clean up any other timers */
4784 	(void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4785 	asoc->dack_timer.self = NULL;
4786 	(void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4787 	/*-
4788 	 * For stream reset we don't blast this unless
4789 	 * it is a str-reset timer, it might be the
4790 	 * free-asoc timer which we DON'T want to
4791 	 * disturb.
4792 	 */
4793 	if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4794 		asoc->strreset_timer.self = NULL;
4795 	(void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4796 	asoc->asconf_timer.self = NULL;
4797 	(void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4798 	asoc->autoclose_timer.self = NULL;
4799 	(void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4800 	asoc->shut_guard_timer.self = NULL;
4801 	(void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4802 	asoc->delayed_event_timer.self = NULL;
4803 	/* Mobility adaptation */
4804 	(void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4805 	asoc->delete_prim_timer.self = NULL;
4806 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4807 		(void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4808 		net->rxt_timer.self = NULL;
4809 		(void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4810 		net->pmtu_timer.self = NULL;
4811 		(void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4812 		net->hb_timer.self = NULL;
4813 	}
4814 	/* Now the read queue needs to be cleaned up (only once) */
4815 	if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4816 		stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4817 		SCTP_INP_READ_LOCK(inp);
4818 		TAILQ_FOREACH(sq, &inp->read_queue, next) {
4819 			if (sq->stcb == stcb) {
4820 				sq->do_not_ref_stcb = 1;
4821 				sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4822 				/*
4823 				 * If there is no end, there never will be
4824 				 * now.
4825 				 */
4826 				if (sq->end_added == 0) {
4827 					/* Held for PD-API clear that. */
4828 					sq->pdapi_aborted = 1;
4829 					sq->held_length = 0;
4830 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4831 						/*
4832 						 * Need to add a PD-API
4833 						 * aborted indication.
4834 						 * Setting the control_pdapi
4835 						 * assures that it will be
4836 						 * added right after this
4837 						 * msg.
4838 						 */
4839 						uint32_t strseq;
4840 
4841 						stcb->asoc.control_pdapi = sq;
4842 						strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4843 						sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4844 						    stcb,
4845 						    SCTP_PARTIAL_DELIVERY_ABORTED,
4846 						    (void *)&strseq,
4847 						    SCTP_SO_LOCKED);
4848 						stcb->asoc.control_pdapi = NULL;
4849 					}
4850 				}
4851 				/* Add an end to wake them */
4852 				sq->end_added = 1;
4853 			}
4854 		}
4855 		SCTP_INP_READ_UNLOCK(inp);
4856 		if (stcb->block_entry) {
4857 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4858 			stcb->block_entry->error = ECONNRESET;
4859 			stcb->block_entry = NULL;
4860 		}
4861 	}
4862 	if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4863 		/*
4864 		 * Someone holds a reference OR the socket is unaccepted
4865 		 * yet.
4866 		 */
4867 		if ((stcb->asoc.refcnt) ||
4868 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4869 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4870 			stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4871 			sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4872 		}
4873 		SCTP_TCB_UNLOCK(stcb);
4874 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4875 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4876 			/* nothing around */
4877 			so = NULL;
4878 		if (so) {
4879 			/* Wake any reader/writers */
4880 			sctp_sorwakeup(inp, so);
4881 			sctp_sowwakeup(inp, so);
4882 		}
4883 #ifdef SCTP_LOG_CLOSING
4884 		sctp_log_closing(inp, stcb, 9);
4885 #endif
4886 		/* no asoc destroyed */
4887 		return (0);
4888 	}
4889 #ifdef SCTP_LOG_CLOSING
4890 	sctp_log_closing(inp, stcb, 10);
4891 #endif
4892 	/*
4893 	 * When I reach here, no others want to kill the assoc yet.. and I
4894 	 * own the lock. Now its possible an abort comes in when I do the
4895 	 * lock exchange below to grab all the locks to do the final take
4896 	 * out. to prevent this we increment the count, which will start a
4897 	 * timer and blow out above thus assuring us that we hold exclusive
4898 	 * killing of the asoc. Note that after getting back the TCB lock we
4899 	 * will go ahead and increment the counter back up and stop any
4900 	 * timer a passing stranger may have started :-S
4901 	 */
4902 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
4903 		atomic_add_int(&stcb->asoc.refcnt, 1);
4904 
4905 		SCTP_TCB_UNLOCK(stcb);
4906 		SCTP_INP_INFO_WLOCK();
4907 		SCTP_INP_WLOCK(inp);
4908 		SCTP_TCB_LOCK(stcb);
4909 	}
4910 	/* Double check the GONE flag */
4911 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4912 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4913 		/* nothing around */
4914 		so = NULL;
4915 
4916 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4917 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4918 		/*
4919 		 * For TCP type we need special handling when we are
4920 		 * connected. We also include the peel'ed off ones to.
4921 		 */
4922 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4923 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4924 			inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4925 			if (so) {
4926 				SOCK_LOCK(so);
4927 				if (so->so_rcv.sb_cc == 0) {
4928 					so->so_state &= ~(SS_ISCONNECTING |
4929 					    SS_ISDISCONNECTING |
4930 					    SS_ISCONFIRMING |
4931 					    SS_ISCONNECTED);
4932 				}
4933 				socantrcvmore_locked(so);
4934 				sctp_sowwakeup(inp, so);
4935 				sctp_sorwakeup(inp, so);
4936 				SCTP_SOWAKEUP(so);
4937 			}
4938 		}
4939 	}
4940 	/*
4941 	 * Make it invalid too, that way if its about to run it will abort
4942 	 * and return.
4943 	 */
4944 	/* re-increment the lock */
4945 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
4946 		atomic_add_int(&stcb->asoc.refcnt, -1);
4947 	}
4948 	if (stcb->asoc.refcnt) {
4949 		stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4950 		sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4951 		if (from_inpcbfree == SCTP_NORMAL_PROC) {
4952 			SCTP_INP_INFO_WUNLOCK();
4953 			SCTP_INP_WUNLOCK(inp);
4954 		}
4955 		SCTP_TCB_UNLOCK(stcb);
4956 		return (0);
4957 	}
4958 	asoc->state = 0;
4959 	if (inp->sctp_tcbhash) {
4960 		LIST_REMOVE(stcb, sctp_tcbhash);
4961 	}
4962 	if (stcb->asoc.in_asocid_hash) {
4963 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4964 	}
4965 	/* Now lets remove it from the list of ALL associations in the EP */
4966 	LIST_REMOVE(stcb, sctp_tcblist);
4967 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
4968 		SCTP_INP_INCR_REF(inp);
4969 		SCTP_INP_WUNLOCK(inp);
4970 	}
4971 	/* pull from vtag hash */
4972 	LIST_REMOVE(stcb, sctp_asocs);
4973 	sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
4974 	    inp->sctp_lport, stcb->rport);
4975 
4976 	/*
4977 	 * Now restop the timers to be sure this is paranoia at is finest!
4978 	 */
4979 	(void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4980 	(void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4981 	(void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4982 	(void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4983 	(void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4984 	(void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4985 	(void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4986 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4987 		(void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4988 		(void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4989 		(void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4990 	}
4991 
4992 	asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4993 	/*
4994 	 * The chunk lists and such SHOULD be empty but we check them just
4995 	 * in case.
4996 	 */
4997 	/* anything on the wheel needs to be removed */
4998 	for (i = 0; i < asoc->streamoutcnt; i++) {
4999 		struct sctp_stream_out *outs;
5000 
5001 		outs = &asoc->strmout[i];
5002 		/* now clean up any chunks here */
5003 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5004 			TAILQ_REMOVE(&outs->outqueue, sp, next);
5005 			if (sp->data) {
5006 				if (so) {
5007 					/* Still an open socket - report */
5008 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
5009 					    SCTP_NOTIFY_DATAGRAM_UNSENT,
5010 					    (void *)sp, SCTP_SO_LOCKED);
5011 				}
5012 				if (sp->data) {
5013 					sctp_m_freem(sp->data);
5014 					sp->data = NULL;
5015 					sp->tail_mbuf = NULL;
5016 				}
5017 			}
5018 			if (sp->net) {
5019 				sctp_free_remote_addr(sp->net);
5020 				sp->net = NULL;
5021 			}
5022 			sctp_free_spbufspace(stcb, asoc, sp);
5023 			if (sp->holds_key_ref)
5024 				sctp_auth_key_release(stcb, sp->auth_keyid, SCTP_SO_LOCKED);
5025 			/* Free the zone stuff  */
5026 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
5027 			SCTP_DECR_STRMOQ_COUNT();
5028 			/* sa_ignore FREED_MEMORY */
5029 		}
5030 	}
5031 	/* sa_ignore FREED_MEMORY */
5032 	TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5033 		TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5034 		SCTP_FREE(strrst, SCTP_M_STRESET);
5035 	}
5036 	TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5037 		TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5038 		if (sq->data) {
5039 			sctp_m_freem(sq->data);
5040 			sq->data = NULL;
5041 		}
5042 		sctp_free_remote_addr(sq->whoFrom);
5043 		sq->whoFrom = NULL;
5044 		sq->stcb = NULL;
5045 		/* Free the ctl entry */
5046 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
5047 		SCTP_DECR_READQ_COUNT();
5048 		/* sa_ignore FREED_MEMORY */
5049 	}
5050 	TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5051 		TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5052 		if (chk->data) {
5053 			sctp_m_freem(chk->data);
5054 			chk->data = NULL;
5055 		}
5056 		if (chk->holds_key_ref)
5057 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5058 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5059 		SCTP_DECR_CHK_COUNT();
5060 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5061 		asoc->free_chunk_cnt--;
5062 		/* sa_ignore FREED_MEMORY */
5063 	}
5064 	/* pending send queue SHOULD be empty */
5065 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5066 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5067 		if (chk->data) {
5068 			if (so) {
5069 				/* Still a socket? */
5070 				sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5071 				    SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_LOCKED);
5072 			}
5073 			if (chk->data) {
5074 				sctp_m_freem(chk->data);
5075 				chk->data = NULL;
5076 			}
5077 		}
5078 		if (chk->holds_key_ref)
5079 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5080 		if (chk->whoTo) {
5081 			sctp_free_remote_addr(chk->whoTo);
5082 			chk->whoTo = NULL;
5083 		}
5084 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5085 		SCTP_DECR_CHK_COUNT();
5086 		/* sa_ignore FREED_MEMORY */
5087 	}
5088 	/* sent queue SHOULD be empty */
5089 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5090 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5091 		if (chk->data) {
5092 			if (so) {
5093 				/* Still a socket? */
5094 				sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5095 				    SCTP_NOTIFY_DATAGRAM_SENT, chk, SCTP_SO_LOCKED);
5096 			}
5097 			if (chk->data) {
5098 				sctp_m_freem(chk->data);
5099 				chk->data = NULL;
5100 			}
5101 		}
5102 		if (chk->holds_key_ref)
5103 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5104 		sctp_free_remote_addr(chk->whoTo);
5105 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5106 		SCTP_DECR_CHK_COUNT();
5107 		/* sa_ignore FREED_MEMORY */
5108 	}
5109 	/* control queue MAY not be empty */
5110 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5111 		TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5112 		if (chk->data) {
5113 			sctp_m_freem(chk->data);
5114 			chk->data = NULL;
5115 		}
5116 		if (chk->holds_key_ref)
5117 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5118 		sctp_free_remote_addr(chk->whoTo);
5119 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5120 		SCTP_DECR_CHK_COUNT();
5121 		/* sa_ignore FREED_MEMORY */
5122 	}
5123 	/* ASCONF queue MAY not be empty */
5124 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5125 		TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5126 		if (chk->data) {
5127 			sctp_m_freem(chk->data);
5128 			chk->data = NULL;
5129 		}
5130 		if (chk->holds_key_ref)
5131 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5132 		sctp_free_remote_addr(chk->whoTo);
5133 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5134 		SCTP_DECR_CHK_COUNT();
5135 		/* sa_ignore FREED_MEMORY */
5136 	}
5137 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5138 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5139 		if (chk->data) {
5140 			sctp_m_freem(chk->data);
5141 			chk->data = NULL;
5142 		}
5143 		if (chk->holds_key_ref)
5144 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5145 		sctp_free_remote_addr(chk->whoTo);
5146 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5147 		SCTP_DECR_CHK_COUNT();
5148 		/* sa_ignore FREED_MEMORY */
5149 	}
5150 
5151 	if (asoc->mapping_array) {
5152 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5153 		asoc->mapping_array = NULL;
5154 	}
5155 	if (asoc->nr_mapping_array) {
5156 		SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5157 		asoc->nr_mapping_array = NULL;
5158 	}
5159 	/* the stream outs */
5160 	if (asoc->strmout) {
5161 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5162 		asoc->strmout = NULL;
5163 	}
5164 	asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5165 	if (asoc->strmin) {
5166 		struct sctp_queued_to_read *ctl, *nctl;
5167 
5168 		for (i = 0; i < asoc->streamincnt; i++) {
5169 			TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5170 				TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5171 				sctp_free_remote_addr(ctl->whoFrom);
5172 				if (ctl->data) {
5173 					sctp_m_freem(ctl->data);
5174 					ctl->data = NULL;
5175 				}
5176 				/*
5177 				 * We don't free the address here since all
5178 				 * the net's were freed above.
5179 				 */
5180 				SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5181 				SCTP_DECR_READQ_COUNT();
5182 			}
5183 		}
5184 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5185 		asoc->strmin = NULL;
5186 	}
5187 	asoc->streamincnt = 0;
5188 	TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5189 #ifdef INVARIANTS
5190 		if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5191 			panic("no net's left alloc'ed, or list points to itself");
5192 		}
5193 #endif
5194 		TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5195 		sctp_free_remote_addr(net);
5196 	}
5197 	LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5198 		/* sa_ignore FREED_MEMORY */
5199 		sctp_remove_laddr(laddr);
5200 	}
5201 
5202 	/* pending asconf (address) parameters */
5203 	TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5204 		/* sa_ignore FREED_MEMORY */
5205 		TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5206 		SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
5207 	}
5208 	TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5209 		/* sa_ignore FREED_MEMORY */
5210 		TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5211 		if (aack->data != NULL) {
5212 			sctp_m_freem(aack->data);
5213 		}
5214 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5215 	}
5216 	/* clean up auth stuff */
5217 	if (asoc->local_hmacs)
5218 		sctp_free_hmaclist(asoc->local_hmacs);
5219 	if (asoc->peer_hmacs)
5220 		sctp_free_hmaclist(asoc->peer_hmacs);
5221 
5222 	if (asoc->local_auth_chunks)
5223 		sctp_free_chunklist(asoc->local_auth_chunks);
5224 	if (asoc->peer_auth_chunks)
5225 		sctp_free_chunklist(asoc->peer_auth_chunks);
5226 
5227 	sctp_free_authinfo(&asoc->authinfo);
5228 
5229 	LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5230 		LIST_REMOVE(shared_key, next);
5231 		sctp_free_sharedkey(shared_key);
5232 		/* sa_ignore FREED_MEMORY */
5233 	}
5234 
5235 	/* Insert new items here :> */
5236 
5237 	/* Get rid of LOCK */
5238 	SCTP_TCB_LOCK_DESTROY(stcb);
5239 	SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5240 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
5241 		SCTP_INP_INFO_WUNLOCK();
5242 		SCTP_INP_RLOCK(inp);
5243 	}
5244 #ifdef SCTP_TRACK_FREED_ASOCS
5245 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5246 		/* now clean up the tasoc itself */
5247 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5248 		SCTP_DECR_ASOC_COUNT();
5249 	} else {
5250 		LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5251 	}
5252 #else
5253 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5254 	SCTP_DECR_ASOC_COUNT();
5255 #endif
5256 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
5257 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5258 			/*
5259 			 * If its NOT the inp_free calling us AND sctp_close
5260 			 * as been called, we call back...
5261 			 */
5262 			SCTP_INP_RUNLOCK(inp);
5263 			/*
5264 			 * This will start the kill timer (if we are the
5265 			 * last one) since we hold an increment yet. But
5266 			 * this is the only safe way to do this since
5267 			 * otherwise if the socket closes at the same time
5268 			 * we are here we might collide in the cleanup.
5269 			 */
5270 			sctp_inpcb_free(inp,
5271 			    SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5272 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
5273 			SCTP_INP_DECR_REF(inp);
5274 			goto out_of;
5275 		} else {
5276 			/* The socket is still open. */
5277 			SCTP_INP_DECR_REF(inp);
5278 		}
5279 	}
5280 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
5281 		SCTP_INP_RUNLOCK(inp);
5282 	}
5283 out_of:
5284 	/* destroyed the asoc */
5285 #ifdef SCTP_LOG_CLOSING
5286 	sctp_log_closing(inp, NULL, 11);
5287 #endif
5288 	return (1);
5289 }
5290 
5291 
5292 
5293 /*
5294  * determine if a destination is "reachable" based upon the addresses bound
5295  * to the current endpoint (e.g. only v4 or v6 currently bound)
5296  */
5297 /*
5298  * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5299  * assoc level v4/v6 flags, as the assoc *may* not have the same address
5300  * types bound as its endpoint
5301  */
5302 int
5303 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5304 {
5305 	struct sctp_inpcb *inp;
5306 	int answer;
5307 
5308 	/*
5309 	 * No locks here, the TCB, in all cases is already locked and an
5310 	 * assoc is up. There is either a INP lock by the caller applied (in
5311 	 * asconf case when deleting an address) or NOT in the HB case,
5312 	 * however if HB then the INP increment is up and the INP will not
5313 	 * be removed (on top of the fact that we have a TCB lock). So we
5314 	 * only want to read the sctp_flags, which is either bound-all or
5315 	 * not.. no protection needed since once an assoc is up you can't be
5316 	 * changing your binding.
5317 	 */
5318 	inp = stcb->sctp_ep;
5319 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5320 		/* if bound all, destination is not restricted */
5321 		/*
5322 		 * RRS: Question during lock work: Is this correct? If you
5323 		 * are bound-all you still might need to obey the V4--V6
5324 		 * flags??? IMO this bound-all stuff needs to be removed!
5325 		 */
5326 		return (1);
5327 	}
5328 	/* NOTE: all "scope" checks are done when local addresses are added */
5329 	switch (destaddr->sa_family) {
5330 	case AF_INET6:
5331 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5332 		break;
5333 	case AF_INET:
5334 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5335 		break;
5336 	default:
5337 		/* invalid family, so it's unreachable */
5338 		answer = 0;
5339 		break;
5340 	}
5341 	return (answer);
5342 }
5343 
5344 /*
5345  * update the inp_vflags on an endpoint
5346  */
5347 static void
5348 sctp_update_ep_vflag(struct sctp_inpcb *inp)
5349 {
5350 	struct sctp_laddr *laddr;
5351 
5352 	/* first clear the flag */
5353 	inp->ip_inp.inp.inp_vflag = 0;
5354 	/* set the flag based on addresses on the ep list */
5355 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5356 		if (laddr->ifa == NULL) {
5357 			SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5358 			    __FUNCTION__);
5359 			continue;
5360 		}
5361 		if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5362 			continue;
5363 		}
5364 		switch (laddr->ifa->address.sa.sa_family) {
5365 #ifdef INET6
5366 		case AF_INET6:
5367 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5368 			break;
5369 #endif
5370 #ifdef INET
5371 		case AF_INET:
5372 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5373 			break;
5374 #endif
5375 		default:
5376 			break;
5377 		}
5378 	}
5379 }
5380 
5381 /*
5382  * Add the address to the endpoint local address list There is nothing to be
5383  * done if we are bound to all addresses
5384  */
5385 void
5386 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5387 {
5388 	struct sctp_laddr *laddr;
5389 	int fnd, error = 0;
5390 
5391 	fnd = 0;
5392 
5393 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5394 		/* You are already bound to all. You have it already */
5395 		return;
5396 	}
5397 #ifdef INET6
5398 	if (ifa->address.sa.sa_family == AF_INET6) {
5399 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5400 			/* Can't bind a non-useable addr. */
5401 			return;
5402 		}
5403 	}
5404 #endif
5405 	/* first, is it already present? */
5406 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5407 		if (laddr->ifa == ifa) {
5408 			fnd = 1;
5409 			break;
5410 		}
5411 	}
5412 
5413 	if (fnd == 0) {
5414 		/* Not in the ep list */
5415 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5416 		if (error != 0)
5417 			return;
5418 		inp->laddr_count++;
5419 		/* update inp_vflag flags */
5420 		switch (ifa->address.sa.sa_family) {
5421 #ifdef INET6
5422 		case AF_INET6:
5423 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5424 			break;
5425 #endif
5426 #ifdef INET6
5427 		case AF_INET:
5428 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5429 			break;
5430 #endif
5431 		default:
5432 			break;
5433 		}
5434 	}
5435 	return;
5436 }
5437 
5438 
5439 /*
5440  * select a new (hopefully reachable) destination net (should only be used
5441  * when we deleted an ep addr that is the only usable source address to reach
5442  * the destination net)
5443  */
5444 static void
5445 sctp_select_primary_destination(struct sctp_tcb *stcb)
5446 {
5447 	struct sctp_nets *net;
5448 
5449 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5450 		/* for now, we'll just pick the first reachable one we find */
5451 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5452 			continue;
5453 		if (sctp_destination_is_reachable(stcb,
5454 		    (struct sockaddr *)&net->ro._l_addr)) {
5455 			/* found a reachable destination */
5456 			stcb->asoc.primary_destination = net;
5457 		}
5458 	}
5459 	/* I can't there from here! ...we're gonna die shortly... */
5460 }
5461 
5462 
5463 /*
5464  * Delete the address from the endpoint local address list There is nothing
5465  * to be done if we are bound to all addresses
5466  */
5467 void
5468 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5469 {
5470 	struct sctp_laddr *laddr;
5471 	int fnd;
5472 
5473 	fnd = 0;
5474 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5475 		/* You are already bound to all. You have it already */
5476 		return;
5477 	}
5478 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5479 		if (laddr->ifa == ifa) {
5480 			fnd = 1;
5481 			break;
5482 		}
5483 	}
5484 	if (fnd && (inp->laddr_count < 2)) {
5485 		/* can't delete unless there are at LEAST 2 addresses */
5486 		return;
5487 	}
5488 	if (fnd) {
5489 		/*
5490 		 * clean up any use of this address go through our
5491 		 * associations and clear any last_used_address that match
5492 		 * this one for each assoc, see if a new primary_destination
5493 		 * is needed
5494 		 */
5495 		struct sctp_tcb *stcb;
5496 
5497 		/* clean up "next_addr_touse" */
5498 		if (inp->next_addr_touse == laddr)
5499 			/* delete this address */
5500 			inp->next_addr_touse = NULL;
5501 
5502 		/* clean up "last_used_address" */
5503 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5504 			struct sctp_nets *net;
5505 
5506 			SCTP_TCB_LOCK(stcb);
5507 			if (stcb->asoc.last_used_address == laddr)
5508 				/* delete this address */
5509 				stcb->asoc.last_used_address = NULL;
5510 			/*
5511 			 * Now spin through all the nets and purge any ref
5512 			 * to laddr
5513 			 */
5514 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5515 				if (net->ro._s_addr &&
5516 				    (net->ro._s_addr->ifa == laddr->ifa)) {
5517 					/* Yep, purge src address selected */
5518 					sctp_rtentry_t *rt;
5519 
5520 					/* delete this address if cached */
5521 					rt = net->ro.ro_rt;
5522 					if (rt != NULL) {
5523 						RTFREE(rt);
5524 						net->ro.ro_rt = NULL;
5525 					}
5526 					sctp_free_ifa(net->ro._s_addr);
5527 					net->ro._s_addr = NULL;
5528 					net->src_addr_selected = 0;
5529 				}
5530 			}
5531 			SCTP_TCB_UNLOCK(stcb);
5532 		}		/* for each tcb */
5533 		/* remove it from the ep list */
5534 		sctp_remove_laddr(laddr);
5535 		inp->laddr_count--;
5536 		/* update inp_vflag flags */
5537 		sctp_update_ep_vflag(inp);
5538 	}
5539 	return;
5540 }
5541 
5542 /*
5543  * Add the address to the TCB local address restricted list.
5544  * This is a "pending" address list (eg. addresses waiting for an
5545  * ASCONF-ACK response) and cannot be used as a valid source address.
5546  */
5547 void
5548 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5549 {
5550 	struct sctp_inpcb *inp;
5551 	struct sctp_laddr *laddr;
5552 	struct sctpladdr *list;
5553 
5554 	/*
5555 	 * Assumes TCB is locked.. and possibly the INP. May need to
5556 	 * confirm/fix that if we need it and is not the case.
5557 	 */
5558 	list = &stcb->asoc.sctp_restricted_addrs;
5559 
5560 	inp = stcb->sctp_ep;
5561 #ifdef INET6
5562 	if (ifa->address.sa.sa_family == AF_INET6) {
5563 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5564 			/* Can't bind a non-existent addr. */
5565 			return;
5566 		}
5567 	}
5568 #endif
5569 	/* does the address already exist? */
5570 	LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5571 		if (laddr->ifa == ifa) {
5572 			return;
5573 		}
5574 	}
5575 
5576 	/* add to the list */
5577 	(void)sctp_insert_laddr(list, ifa, 0);
5578 	return;
5579 }
5580 
5581 /*
5582  * insert an laddr entry with the given ifa for the desired list
5583  */
5584 int
5585 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5586 {
5587 	struct sctp_laddr *laddr;
5588 
5589 	laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5590 	if (laddr == NULL) {
5591 		/* out of memory? */
5592 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5593 		return (EINVAL);
5594 	}
5595 	SCTP_INCR_LADDR_COUNT();
5596 	bzero(laddr, sizeof(*laddr));
5597 	(void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5598 	laddr->ifa = ifa;
5599 	laddr->action = act;
5600 	atomic_add_int(&ifa->refcount, 1);
5601 	/* insert it */
5602 	LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5603 
5604 	return (0);
5605 }
5606 
5607 /*
5608  * Remove an laddr entry from the local address list (on an assoc)
5609  */
5610 void
5611 sctp_remove_laddr(struct sctp_laddr *laddr)
5612 {
5613 
5614 	/* remove from the list */
5615 	LIST_REMOVE(laddr, sctp_nxt_addr);
5616 	sctp_free_ifa(laddr->ifa);
5617 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5618 	SCTP_DECR_LADDR_COUNT();
5619 }
5620 
5621 /*
5622  * Remove a local address from the TCB local address restricted list
5623  */
5624 void
5625 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5626 {
5627 	struct sctp_inpcb *inp;
5628 	struct sctp_laddr *laddr;
5629 
5630 	/*
5631 	 * This is called by asconf work. It is assumed that a) The TCB is
5632 	 * locked and b) The INP is locked. This is true in as much as I can
5633 	 * trace through the entry asconf code where I did these locks.
5634 	 * Again, the ASCONF code is a bit different in that it does lock
5635 	 * the INP during its work often times. This must be since we don't
5636 	 * want other proc's looking up things while what they are looking
5637 	 * up is changing :-D
5638 	 */
5639 
5640 	inp = stcb->sctp_ep;
5641 	/* if subset bound and don't allow ASCONF's, can't delete last */
5642 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5643 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5644 		if (stcb->sctp_ep->laddr_count < 2) {
5645 			/* can't delete last address */
5646 			return;
5647 		}
5648 	}
5649 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5650 		/* remove the address if it exists */
5651 		if (laddr->ifa == NULL)
5652 			continue;
5653 		if (laddr->ifa == ifa) {
5654 			sctp_remove_laddr(laddr);
5655 			return;
5656 		}
5657 	}
5658 
5659 	/* address not found! */
5660 	return;
5661 }
5662 
5663 /*
5664  * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5665  */
5666 /* sysctl */
5667 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5668 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5669 
5670 
5671 
5672 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5673 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
5674 int *sctp_cpuarry = NULL;
5675 void
5676 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
5677 {
5678 	/* Queue a packet to a processor for the specified core */
5679 	struct sctp_mcore_queue *qent;
5680 	struct sctp_mcore_ctrl *wkq;
5681 	int need_wake = 0;
5682 
5683 	if (sctp_mcore_workers == NULL) {
5684 		/* Something went way bad during setup */
5685 		sctp_input_with_port(m, off, 0);
5686 		return;
5687 	}
5688 	SCTP_MALLOC(qent, struct sctp_mcore_queue *,
5689 	    (sizeof(struct sctp_mcore_queue)),
5690 	    SCTP_M_MCORE);
5691 	if (qent == NULL) {
5692 		/* This is trouble  */
5693 		sctp_input_with_port(m, off, 0);
5694 		return;
5695 	}
5696 	qent->vn = curvnet;
5697 	qent->m = m;
5698 	qent->off = off;
5699 	qent->v6 = 0;
5700 	wkq = &sctp_mcore_workers[cpu_to_use];
5701 	SCTP_MCORE_QLOCK(wkq);
5702 
5703 	TAILQ_INSERT_TAIL(&wkq->que, qent, next);
5704 	if (wkq->running == 0) {
5705 		need_wake = 1;
5706 	}
5707 	SCTP_MCORE_QUNLOCK(wkq);
5708 	if (need_wake) {
5709 		wakeup(&wkq->running);
5710 	}
5711 }
5712 
5713 static void
5714 sctp_mcore_thread(void *arg)
5715 {
5716 
5717 	struct sctp_mcore_ctrl *wkq;
5718 	struct sctp_mcore_queue *qent;
5719 
5720 	wkq = (struct sctp_mcore_ctrl *)arg;
5721 	struct mbuf *m;
5722 	int off, v6;
5723 
5724 	/* Wait for first tickle */
5725 	SCTP_MCORE_LOCK(wkq);
5726 	wkq->running = 0;
5727 	msleep(&wkq->running,
5728 	    &wkq->core_mtx,
5729 	    0, "wait for pkt", 0);
5730 	SCTP_MCORE_UNLOCK(wkq);
5731 
5732 	/* Bind to our cpu */
5733 	thread_lock(curthread);
5734 	sched_bind(curthread, wkq->cpuid);
5735 	thread_unlock(curthread);
5736 
5737 	/* Now lets start working */
5738 	SCTP_MCORE_LOCK(wkq);
5739 	/* Now grab lock and go */
5740 	while (1) {
5741 		SCTP_MCORE_QLOCK(wkq);
5742 skip_sleep:
5743 		wkq->running = 1;
5744 		qent = TAILQ_FIRST(&wkq->que);
5745 		if (qent) {
5746 			TAILQ_REMOVE(&wkq->que, qent, next);
5747 			SCTP_MCORE_QUNLOCK(wkq);
5748 			CURVNET_SET(qent->vn);
5749 			m = qent->m;
5750 			off = qent->off;
5751 			v6 = qent->v6;
5752 			SCTP_FREE(qent, SCTP_M_MCORE);
5753 			if (v6 == 0) {
5754 				sctp_input_with_port(m, off, 0);
5755 			} else {
5756 				printf("V6 not yet supported\n");
5757 				sctp_m_freem(m);
5758 			}
5759 			CURVNET_RESTORE();
5760 			SCTP_MCORE_QLOCK(wkq);
5761 		}
5762 		wkq->running = 0;
5763 		if (!TAILQ_EMPTY(&wkq->que)) {
5764 			goto skip_sleep;
5765 		}
5766 		SCTP_MCORE_QUNLOCK(wkq);
5767 		msleep(&wkq->running,
5768 		    &wkq->core_mtx,
5769 		    0, "wait for pkt", 0);
5770 	};
5771 }
5772 
5773 static void
5774 sctp_startup_mcore_threads(void)
5775 {
5776 	int i, cpu;
5777 
5778 	if (mp_ncpus == 1)
5779 		return;
5780 
5781 	if (sctp_mcore_workers != NULL) {
5782 		/*
5783 		 * Already been here in some previous vnet?
5784 		 */
5785 		return;
5786 	}
5787 	SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
5788 	    ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)),
5789 	    SCTP_M_MCORE);
5790 	if (sctp_mcore_workers == NULL) {
5791 		/* TSNH I hope */
5792 		return;
5793 	}
5794 	memset(sctp_mcore_workers, 0, ((mp_maxid + 1) *
5795 	    sizeof(struct sctp_mcore_ctrl)));
5796 	/* Init the structures */
5797 	for (i = 0; i <= mp_maxid; i++) {
5798 		TAILQ_INIT(&sctp_mcore_workers[i].que);
5799 		SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
5800 		SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
5801 		sctp_mcore_workers[i].cpuid = i;
5802 	}
5803 	if (sctp_cpuarry == NULL) {
5804 		SCTP_MALLOC(sctp_cpuarry, int *,
5805 		    (mp_ncpus * sizeof(int)),
5806 		    SCTP_M_MCORE);
5807 		i = 0;
5808 		CPU_FOREACH(cpu) {
5809 			sctp_cpuarry[i] = cpu;
5810 			i++;
5811 		}
5812 	}
5813 	/* Now start them all */
5814 	CPU_FOREACH(cpu) {
5815 		(void)kproc_create(sctp_mcore_thread,
5816 		    (void *)&sctp_mcore_workers[cpu],
5817 		    &sctp_mcore_workers[cpu].thread_proc,
5818 		    RFPROC,
5819 		    SCTP_KTHREAD_PAGES,
5820 		    SCTP_MCORE_NAME);
5821 
5822 	}
5823 }
5824 
5825 #endif
5826 
5827 void
5828 sctp_pcb_init()
5829 {
5830 	/*
5831 	 * SCTP initialization for the PCB structures should be called by
5832 	 * the sctp_init() funciton.
5833 	 */
5834 	int i;
5835 	struct timeval tv;
5836 
5837 	if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5838 		/* error I was called twice */
5839 		return;
5840 	}
5841 	SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5842 
5843 #if defined(SCTP_LOCAL_TRACE_BUF)
5844 	bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5845 #endif
5846 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5847 	SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
5848 	    ((mp_maxid + 1) * sizeof(struct sctpstat)),
5849 	    SCTP_M_MCORE);
5850 #endif
5851 	(void)SCTP_GETTIME_TIMEVAL(&tv);
5852 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5853 	bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid + 1)));
5854 	SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec;
5855 	SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec;
5856 #else
5857 	bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
5858 	SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5859 	SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5860 #endif
5861 	/* init the empty list of (All) Endpoints */
5862 	LIST_INIT(&SCTP_BASE_INFO(listhead));
5863 
5864 
5865 	/* init the hash table of endpoints */
5866 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5867 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5868 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5869 	SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5870 	    &SCTP_BASE_INFO(hashasocmark));
5871 	SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5872 	    &SCTP_BASE_INFO(hashmark));
5873 	SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5874 	    &SCTP_BASE_INFO(hashtcpmark));
5875 	SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5876 
5877 
5878 	SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5879 	    &SCTP_BASE_INFO(hashvrfmark));
5880 
5881 	SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5882 	    &SCTP_BASE_INFO(vrf_ifn_hashmark));
5883 	/* init the zones */
5884 	/*
5885 	 * FIX ME: Should check for NULL returns, but if it does fail we are
5886 	 * doomed to panic anyways... add later maybe.
5887 	 */
5888 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5889 	    sizeof(struct sctp_inpcb), maxsockets);
5890 
5891 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5892 	    sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5893 
5894 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5895 	    sizeof(struct sctp_laddr),
5896 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5897 
5898 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5899 	    sizeof(struct sctp_nets),
5900 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5901 
5902 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5903 	    sizeof(struct sctp_tmit_chunk),
5904 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5905 
5906 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5907 	    sizeof(struct sctp_queued_to_read),
5908 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5909 
5910 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5911 	    sizeof(struct sctp_stream_queue_pending),
5912 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5913 
5914 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5915 	    sizeof(struct sctp_asconf),
5916 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5917 
5918 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5919 	    sizeof(struct sctp_asconf_ack),
5920 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5921 
5922 
5923 	/* Master Lock INIT for info structure */
5924 	SCTP_INP_INFO_LOCK_INIT();
5925 	SCTP_STATLOG_INIT_LOCK();
5926 
5927 	SCTP_IPI_COUNT_INIT();
5928 	SCTP_IPI_ADDR_INIT();
5929 #ifdef SCTP_PACKET_LOGGING
5930 	SCTP_IP_PKTLOG_INIT();
5931 #endif
5932 	LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5933 
5934 	SCTP_WQ_ADDR_INIT();
5935 	/* not sure if we need all the counts */
5936 	SCTP_BASE_INFO(ipi_count_ep) = 0;
5937 	/* assoc/tcb zone info */
5938 	SCTP_BASE_INFO(ipi_count_asoc) = 0;
5939 	/* local addrlist zone info */
5940 	SCTP_BASE_INFO(ipi_count_laddr) = 0;
5941 	/* remote addrlist zone info */
5942 	SCTP_BASE_INFO(ipi_count_raddr) = 0;
5943 	/* chunk info */
5944 	SCTP_BASE_INFO(ipi_count_chunk) = 0;
5945 
5946 	/* socket queue zone info */
5947 	SCTP_BASE_INFO(ipi_count_readq) = 0;
5948 
5949 	/* stream out queue cont */
5950 	SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5951 
5952 	SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5953 	SCTP_BASE_INFO(ipi_free_chunks) = 0;
5954 
5955 	SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5956 
5957 	/* Init the TIMEWAIT list */
5958 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5959 		LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
5960 	}
5961 
5962 	sctp_startup_iterator();
5963 
5964 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5965 	sctp_startup_mcore_threads();
5966 #endif
5967 
5968 	/*
5969 	 * INIT the default VRF which for BSD is the only one, other O/S's
5970 	 * may have more. But initially they must start with one and then
5971 	 * add the VRF's as addresses are added.
5972 	 */
5973 	sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5974 }
5975 
5976 /*
5977  * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5978  */
5979 void
5980 sctp_pcb_finish(void)
5981 {
5982 	struct sctp_vrflist *vrf_bucket;
5983 	struct sctp_vrf *vrf, *nvrf;
5984 	struct sctp_ifn *ifn, *nifn;
5985 	struct sctp_ifa *ifa, *nifa;
5986 	struct sctpvtaghead *chain;
5987 	struct sctp_tagblock *twait_block, *prev_twait_block;
5988 	struct sctp_laddr *wi, *nwi;
5989 	int i;
5990 
5991 	/*
5992 	 * Free BSD the it thread never exits but we do clean up. The only
5993 	 * way freebsd reaches here if we have VRF's but we still add the
5994 	 * ifdef to make it compile on old versions.
5995 	 */
5996 	{
5997 		struct sctp_iterator *it, *nit;
5998 
5999 		SCTP_IPI_ITERATOR_WQ_LOCK();
6000 		TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6001 			if (it->vn != curvnet) {
6002 				continue;
6003 			}
6004 			TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6005 			if (it->function_atend != NULL) {
6006 				(*it->function_atend) (it->pointer, it->val);
6007 			}
6008 			SCTP_FREE(it, SCTP_M_ITER);
6009 		}
6010 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
6011 		SCTP_ITERATOR_LOCK();
6012 		if ((sctp_it_ctl.cur_it) &&
6013 		    (sctp_it_ctl.cur_it->vn == curvnet)) {
6014 			sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6015 		}
6016 		SCTP_ITERATOR_UNLOCK();
6017 	}
6018 
6019 	SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
6020 	SCTP_WQ_ADDR_LOCK();
6021 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6022 		LIST_REMOVE(wi, sctp_nxt_addr);
6023 		SCTP_DECR_LADDR_COUNT();
6024 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6025 	}
6026 	SCTP_WQ_ADDR_UNLOCK();
6027 
6028 	/*
6029 	 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
6030 	 * destroyed first).
6031 	 */
6032 	vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6033 	LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6034 		LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6035 			LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6036 				/* free the ifa */
6037 				LIST_REMOVE(ifa, next_bucket);
6038 				LIST_REMOVE(ifa, next_ifa);
6039 				SCTP_FREE(ifa, SCTP_M_IFA);
6040 			}
6041 			/* free the ifn */
6042 			LIST_REMOVE(ifn, next_bucket);
6043 			LIST_REMOVE(ifn, next_ifn);
6044 			SCTP_FREE(ifn, SCTP_M_IFN);
6045 		}
6046 		SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6047 		/* free the vrf */
6048 		LIST_REMOVE(vrf, next_vrf);
6049 		SCTP_FREE(vrf, SCTP_M_VRF);
6050 	}
6051 	/* free the vrf hashes */
6052 	SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6053 	SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
6054 
6055 	/*
6056 	 * free the TIMEWAIT list elements malloc'd in the function
6057 	 * sctp_add_vtag_to_timewait()...
6058 	 */
6059 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6060 		chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6061 		if (!LIST_EMPTY(chain)) {
6062 			prev_twait_block = NULL;
6063 			LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6064 				if (prev_twait_block) {
6065 					SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6066 				}
6067 				prev_twait_block = twait_block;
6068 			}
6069 			SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6070 		}
6071 	}
6072 
6073 	/* free the locks and mutexes */
6074 #ifdef SCTP_PACKET_LOGGING
6075 	SCTP_IP_PKTLOG_DESTROY();
6076 #endif
6077 	SCTP_IPI_ADDR_DESTROY();
6078 	SCTP_STATLOG_DESTROY();
6079 	SCTP_INP_INFO_LOCK_DESTROY();
6080 
6081 	SCTP_WQ_ADDR_DESTROY();
6082 
6083 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6084 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6085 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6086 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6087 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6088 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6089 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6090 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6091 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6092 	/* Get rid of other stuff to */
6093 	if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6094 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6095 	if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6096 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6097 	if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6098 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6099 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6100 	SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6101 #endif
6102 }
6103 
6104 
6105 int
6106 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6107     int iphlen, int offset, int limit, struct sctphdr *sh,
6108     struct sockaddr *altsa)
6109 {
6110 	/*
6111 	 * grub through the INIT pulling addresses and loading them to the
6112 	 * nets structure in the asoc. The from address in the mbuf should
6113 	 * also be loaded (if it is not already). This routine can be called
6114 	 * with either INIT or INIT-ACK's as long as the m points to the IP
6115 	 * packet and the offset points to the beginning of the parameters.
6116 	 */
6117 	struct sctp_inpcb *inp, *l_inp;
6118 	struct sctp_nets *net, *nnet, *net_tmp;
6119 	struct ip *iph;
6120 	struct sctp_paramhdr *phdr, parm_buf;
6121 	struct sctp_tcb *stcb_tmp;
6122 	uint16_t ptype, plen;
6123 	struct sockaddr *sa;
6124 	struct sockaddr_storage dest_store;
6125 	struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
6126 	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6127 	struct sctp_auth_random *p_random = NULL;
6128 	uint16_t random_len = 0;
6129 	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6130 	struct sctp_auth_hmac_algo *hmacs = NULL;
6131 	uint16_t hmacs_len = 0;
6132 	uint8_t saw_asconf = 0;
6133 	uint8_t saw_asconf_ack = 0;
6134 	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6135 	struct sctp_auth_chunk_list *chunks = NULL;
6136 	uint16_t num_chunks = 0;
6137 	sctp_key_t *new_key;
6138 	uint32_t keylen;
6139 	int got_random = 0, got_hmacs = 0, got_chklist = 0;
6140 	uint8_t ecn_allowed;
6141 
6142 #ifdef INET
6143 	struct sockaddr_in sin;
6144 
6145 #endif
6146 #ifdef INET6
6147 	struct sockaddr_in6 sin6;
6148 
6149 #endif
6150 
6151 	/* First get the destination address setup too. */
6152 #ifdef INET
6153 	memset(&sin, 0, sizeof(sin));
6154 	sin.sin_family = AF_INET;
6155 	sin.sin_len = sizeof(sin);
6156 	sin.sin_port = stcb->rport;
6157 #endif
6158 #ifdef INET6
6159 	memset(&sin6, 0, sizeof(sin6));
6160 	sin6.sin6_family = AF_INET6;
6161 	sin6.sin6_len = sizeof(struct sockaddr_in6);
6162 	sin6.sin6_port = stcb->rport;
6163 #endif
6164 	iph = mtod(m, struct ip *);
6165 	switch (iph->ip_v) {
6166 #ifdef INET
6167 	case IPVERSION:
6168 		{
6169 			/* its IPv4 */
6170 			struct sockaddr_in *sin_2;
6171 
6172 			sin_2 = (struct sockaddr_in *)(local_sa);
6173 			memset(sin_2, 0, sizeof(sin));
6174 			sin_2->sin_family = AF_INET;
6175 			sin_2->sin_len = sizeof(sin);
6176 			sin_2->sin_port = sh->dest_port;
6177 			sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
6178 			if (altsa) {
6179 				/*
6180 				 * For cookies we use the src address NOT
6181 				 * from the packet but from the original
6182 				 * INIT.
6183 				 */
6184 				sa = altsa;
6185 			} else {
6186 				sin.sin_addr = iph->ip_src;
6187 				sa = (struct sockaddr *)&sin;
6188 			}
6189 			break;
6190 		}
6191 #endif
6192 #ifdef INET6
6193 	case IPV6_VERSION >> 4:
6194 		{
6195 			/* its IPv6 */
6196 			struct ip6_hdr *ip6;
6197 			struct sockaddr_in6 *sin6_2;
6198 
6199 			ip6 = mtod(m, struct ip6_hdr *);
6200 			sin6_2 = (struct sockaddr_in6 *)(local_sa);
6201 			memset(sin6_2, 0, sizeof(sin6));
6202 			sin6_2->sin6_family = AF_INET6;
6203 			sin6_2->sin6_len = sizeof(struct sockaddr_in6);
6204 			sin6_2->sin6_port = sh->dest_port;
6205 			sin6_2->sin6_addr = ip6->ip6_dst;
6206 			if (altsa) {
6207 				/*
6208 				 * For cookies we use the src address NOT
6209 				 * from the packet but from the original
6210 				 * INIT.
6211 				 */
6212 				sa = altsa;
6213 			} else {
6214 				sin6.sin6_addr = ip6->ip6_src;
6215 				sa = (struct sockaddr *)&sin6;
6216 			}
6217 			break;
6218 		}
6219 #endif
6220 	default:
6221 		return (-1);
6222 		break;
6223 	}
6224 	/* Turn off ECN until we get through all params */
6225 	ecn_allowed = 0;
6226 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6227 		/* mark all addresses that we have currently on the list */
6228 		net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6229 	}
6230 	/* does the source address already exist? if so skip it */
6231 	l_inp = inp = stcb->sctp_ep;
6232 
6233 	atomic_add_int(&stcb->asoc.refcnt, 1);
6234 	stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
6235 	atomic_add_int(&stcb->asoc.refcnt, -1);
6236 
6237 	if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6238 		/* we must add the source address */
6239 		/* no scope set here since we have a tcb already. */
6240 		switch (sa->sa_family) {
6241 #ifdef INET
6242 		case AF_INET:
6243 			if (stcb->asoc.ipv4_addr_legal) {
6244 				if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6245 					return (-1);
6246 				}
6247 			}
6248 			break;
6249 #endif
6250 #ifdef INET6
6251 		case AF_INET6:
6252 			if (stcb->asoc.ipv6_addr_legal) {
6253 				if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6254 					return (-2);
6255 				}
6256 			}
6257 			break;
6258 #endif
6259 		default:
6260 			break;
6261 		}
6262 	} else {
6263 		if (net_tmp != NULL && stcb_tmp == stcb) {
6264 			net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6265 		} else if (stcb_tmp != stcb) {
6266 			/* It belongs to another association? */
6267 			if (stcb_tmp)
6268 				SCTP_TCB_UNLOCK(stcb_tmp);
6269 			return (-3);
6270 		}
6271 	}
6272 	if (stcb->asoc.state == 0) {
6273 		/* the assoc was freed? */
6274 		return (-4);
6275 	}
6276 	/*
6277 	 * peer must explicitly turn this on. This may have been initialized
6278 	 * to be "on" in order to allow local addr changes while INIT's are
6279 	 * in flight.
6280 	 */
6281 	stcb->asoc.peer_supports_asconf = 0;
6282 	/* now we must go through each of the params. */
6283 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
6284 	while (phdr) {
6285 		ptype = ntohs(phdr->param_type);
6286 		plen = ntohs(phdr->param_length);
6287 		/*
6288 		 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
6289 		 * (int)plen);
6290 		 */
6291 		if (offset + plen > limit) {
6292 			break;
6293 		}
6294 		if (plen == 0) {
6295 			break;
6296 		}
6297 #ifdef INET
6298 		if (ptype == SCTP_IPV4_ADDRESS) {
6299 			if (stcb->asoc.ipv4_addr_legal) {
6300 				struct sctp_ipv4addr_param *p4, p4_buf;
6301 
6302 				/* ok get the v4 address and check/add */
6303 				phdr = sctp_get_next_param(m, offset,
6304 				    (struct sctp_paramhdr *)&p4_buf,
6305 				    sizeof(p4_buf));
6306 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
6307 				    phdr == NULL) {
6308 					return (-5);
6309 				}
6310 				p4 = (struct sctp_ipv4addr_param *)phdr;
6311 				sin.sin_addr.s_addr = p4->addr;
6312 				if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
6313 					/* Skip multi-cast addresses */
6314 					goto next_param;
6315 				}
6316 				if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
6317 				    (sin.sin_addr.s_addr == INADDR_ANY)) {
6318 					goto next_param;
6319 				}
6320 				sa = (struct sockaddr *)&sin;
6321 				inp = stcb->sctp_ep;
6322 				atomic_add_int(&stcb->asoc.refcnt, 1);
6323 				stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6324 				    local_sa, stcb);
6325 				atomic_add_int(&stcb->asoc.refcnt, -1);
6326 
6327 				if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
6328 				    inp == NULL) {
6329 					/* we must add the source address */
6330 					/*
6331 					 * no scope set since we have a tcb
6332 					 * already
6333 					 */
6334 
6335 					/*
6336 					 * we must validate the state again
6337 					 * here
6338 					 */
6339 			add_it_now:
6340 					if (stcb->asoc.state == 0) {
6341 						/* the assoc was freed? */
6342 						return (-7);
6343 					}
6344 					if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
6345 						return (-8);
6346 					}
6347 				} else if (stcb_tmp == stcb) {
6348 					if (stcb->asoc.state == 0) {
6349 						/* the assoc was freed? */
6350 						return (-10);
6351 					}
6352 					if (net != NULL) {
6353 						/* clear flag */
6354 						net->dest_state &=
6355 						    ~SCTP_ADDR_NOT_IN_ASSOC;
6356 					}
6357 				} else {
6358 					/*
6359 					 * strange, address is in another
6360 					 * assoc? straighten out locks.
6361 					 */
6362 					if (stcb_tmp) {
6363 						if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6364 							/*
6365 							 * in setup state we
6366 							 * abort this guy
6367 							 */
6368 							sctp_abort_an_association(stcb_tmp->sctp_ep,
6369 							    stcb_tmp, 1, NULL, 0);
6370 							goto add_it_now;
6371 						}
6372 						SCTP_TCB_UNLOCK(stcb_tmp);
6373 					}
6374 					if (stcb->asoc.state == 0) {
6375 						/* the assoc was freed? */
6376 						return (-12);
6377 					}
6378 					return (-13);
6379 				}
6380 			}
6381 		} else
6382 #endif
6383 #ifdef INET6
6384 		if (ptype == SCTP_IPV6_ADDRESS) {
6385 			if (stcb->asoc.ipv6_addr_legal) {
6386 				/* ok get the v6 address and check/add */
6387 				struct sctp_ipv6addr_param *p6, p6_buf;
6388 
6389 				phdr = sctp_get_next_param(m, offset,
6390 				    (struct sctp_paramhdr *)&p6_buf,
6391 				    sizeof(p6_buf));
6392 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
6393 				    phdr == NULL) {
6394 					return (-14);
6395 				}
6396 				p6 = (struct sctp_ipv6addr_param *)phdr;
6397 				memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6398 				    sizeof(p6->addr));
6399 				if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
6400 					/* Skip multi-cast addresses */
6401 					goto next_param;
6402 				}
6403 				if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
6404 					/*
6405 					 * Link local make no sense without
6406 					 * scope
6407 					 */
6408 					goto next_param;
6409 				}
6410 				sa = (struct sockaddr *)&sin6;
6411 				inp = stcb->sctp_ep;
6412 				atomic_add_int(&stcb->asoc.refcnt, 1);
6413 				stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6414 				    local_sa, stcb);
6415 				atomic_add_int(&stcb->asoc.refcnt, -1);
6416 				if (stcb_tmp == NULL &&
6417 				    (inp == stcb->sctp_ep || inp == NULL)) {
6418 					/*
6419 					 * we must validate the state again
6420 					 * here
6421 					 */
6422 			add_it_now6:
6423 					if (stcb->asoc.state == 0) {
6424 						/* the assoc was freed? */
6425 						return (-16);
6426 					}
6427 					/*
6428 					 * we must add the address, no scope
6429 					 * set
6430 					 */
6431 					if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
6432 						return (-17);
6433 					}
6434 				} else if (stcb_tmp == stcb) {
6435 					/*
6436 					 * we must validate the state again
6437 					 * here
6438 					 */
6439 					if (stcb->asoc.state == 0) {
6440 						/* the assoc was freed? */
6441 						return (-19);
6442 					}
6443 					if (net != NULL) {
6444 						/* clear flag */
6445 						net->dest_state &=
6446 						    ~SCTP_ADDR_NOT_IN_ASSOC;
6447 					}
6448 				} else {
6449 					/*
6450 					 * strange, address is in another
6451 					 * assoc? straighten out locks.
6452 					 */
6453 					if (stcb_tmp)
6454 						if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6455 							/*
6456 							 * in setup state we
6457 							 * abort this guy
6458 							 */
6459 							sctp_abort_an_association(stcb_tmp->sctp_ep,
6460 							    stcb_tmp, 1, NULL, 0);
6461 							goto add_it_now6;
6462 						}
6463 					SCTP_TCB_UNLOCK(stcb_tmp);
6464 
6465 					if (stcb->asoc.state == 0) {
6466 						/* the assoc was freed? */
6467 						return (-21);
6468 					}
6469 					return (-22);
6470 				}
6471 			}
6472 		} else
6473 #endif
6474 		if (ptype == SCTP_ECN_CAPABLE) {
6475 			ecn_allowed = 1;
6476 		} else if (ptype == SCTP_ULP_ADAPTATION) {
6477 			if (stcb->asoc.state != SCTP_STATE_OPEN) {
6478 				struct sctp_adaptation_layer_indication ai,
6479 				                                *aip;
6480 
6481 				phdr = sctp_get_next_param(m, offset,
6482 				    (struct sctp_paramhdr *)&ai, sizeof(ai));
6483 				aip = (struct sctp_adaptation_layer_indication *)phdr;
6484 				if (aip) {
6485 					stcb->asoc.peers_adaptation = ntohl(aip->indication);
6486 					stcb->asoc.adaptation_needed = 1;
6487 				}
6488 			}
6489 		} else if (ptype == SCTP_SET_PRIM_ADDR) {
6490 			struct sctp_asconf_addr_param lstore, *fee;
6491 			int lptype;
6492 			struct sockaddr *lsa = NULL;
6493 
6494 #ifdef INET
6495 			struct sctp_asconf_addrv4_param *fii;
6496 
6497 #endif
6498 
6499 			stcb->asoc.peer_supports_asconf = 1;
6500 			if (plen > sizeof(lstore)) {
6501 				return (-23);
6502 			}
6503 			phdr = sctp_get_next_param(m, offset,
6504 			    (struct sctp_paramhdr *)&lstore,
6505 			    min(plen, sizeof(lstore)));
6506 			if (phdr == NULL) {
6507 				return (-24);
6508 			}
6509 			fee = (struct sctp_asconf_addr_param *)phdr;
6510 			lptype = ntohs(fee->addrp.ph.param_type);
6511 			switch (lptype) {
6512 #ifdef INET
6513 			case SCTP_IPV4_ADDRESS:
6514 				if (plen !=
6515 				    sizeof(struct sctp_asconf_addrv4_param)) {
6516 					SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
6517 					    (int)sizeof(struct sctp_asconf_addrv4_param),
6518 					    plen);
6519 				} else {
6520 					fii = (struct sctp_asconf_addrv4_param *)fee;
6521 					sin.sin_addr.s_addr = fii->addrp.addr;
6522 					lsa = (struct sockaddr *)&sin;
6523 				}
6524 				break;
6525 #endif
6526 #ifdef INET6
6527 			case SCTP_IPV6_ADDRESS:
6528 				if (plen !=
6529 				    sizeof(struct sctp_asconf_addr_param)) {
6530 					SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
6531 					    (int)sizeof(struct sctp_asconf_addr_param),
6532 					    plen);
6533 				} else {
6534 					memcpy(sin6.sin6_addr.s6_addr,
6535 					    fee->addrp.addr,
6536 					    sizeof(fee->addrp.addr));
6537 					lsa = (struct sockaddr *)&sin6;
6538 				}
6539 				break;
6540 #endif
6541 			default:
6542 				break;
6543 			}
6544 			if (lsa) {
6545 				(void)sctp_set_primary_addr(stcb, sa, NULL);
6546 			}
6547 		} else if (ptype == SCTP_HAS_NAT_SUPPORT) {
6548 			stcb->asoc.peer_supports_nat = 1;
6549 		} else if (ptype == SCTP_PRSCTP_SUPPORTED) {
6550 			/* Peer supports pr-sctp */
6551 			stcb->asoc.peer_supports_prsctp = 1;
6552 		} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6553 			/* A supported extension chunk */
6554 			struct sctp_supported_chunk_types_param *pr_supported;
6555 			uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6556 			int num_ent, i;
6557 
6558 			phdr = sctp_get_next_param(m, offset,
6559 			    (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
6560 			if (phdr == NULL) {
6561 				return (-25);
6562 			}
6563 			stcb->asoc.peer_supports_asconf = 0;
6564 			stcb->asoc.peer_supports_prsctp = 0;
6565 			stcb->asoc.peer_supports_pktdrop = 0;
6566 			stcb->asoc.peer_supports_strreset = 0;
6567 			stcb->asoc.peer_supports_nr_sack = 0;
6568 			stcb->asoc.peer_supports_auth = 0;
6569 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6570 			num_ent = plen - sizeof(struct sctp_paramhdr);
6571 			for (i = 0; i < num_ent; i++) {
6572 				switch (pr_supported->chunk_types[i]) {
6573 				case SCTP_ASCONF:
6574 				case SCTP_ASCONF_ACK:
6575 					stcb->asoc.peer_supports_asconf = 1;
6576 					break;
6577 				case SCTP_FORWARD_CUM_TSN:
6578 					stcb->asoc.peer_supports_prsctp = 1;
6579 					break;
6580 				case SCTP_PACKET_DROPPED:
6581 					stcb->asoc.peer_supports_pktdrop = 1;
6582 					break;
6583 				case SCTP_NR_SELECTIVE_ACK:
6584 					stcb->asoc.peer_supports_nr_sack = 1;
6585 					break;
6586 				case SCTP_STREAM_RESET:
6587 					stcb->asoc.peer_supports_strreset = 1;
6588 					break;
6589 				case SCTP_AUTHENTICATION:
6590 					stcb->asoc.peer_supports_auth = 1;
6591 					break;
6592 				default:
6593 					/* one I have not learned yet */
6594 					break;
6595 
6596 				}
6597 			}
6598 		} else if (ptype == SCTP_RANDOM) {
6599 			if (plen > sizeof(random_store))
6600 				break;
6601 			if (got_random) {
6602 				/* already processed a RANDOM */
6603 				goto next_param;
6604 			}
6605 			phdr = sctp_get_next_param(m, offset,
6606 			    (struct sctp_paramhdr *)random_store,
6607 			    min(sizeof(random_store), plen));
6608 			if (phdr == NULL)
6609 				return (-26);
6610 			p_random = (struct sctp_auth_random *)phdr;
6611 			random_len = plen - sizeof(*p_random);
6612 			/* enforce the random length */
6613 			if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6614 				SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6615 				return (-27);
6616 			}
6617 			got_random = 1;
6618 		} else if (ptype == SCTP_HMAC_LIST) {
6619 			int num_hmacs;
6620 			int i;
6621 
6622 			if (plen > sizeof(hmacs_store))
6623 				break;
6624 			if (got_hmacs) {
6625 				/* already processed a HMAC list */
6626 				goto next_param;
6627 			}
6628 			phdr = sctp_get_next_param(m, offset,
6629 			    (struct sctp_paramhdr *)hmacs_store,
6630 			    min(plen, sizeof(hmacs_store)));
6631 			if (phdr == NULL)
6632 				return (-28);
6633 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
6634 			hmacs_len = plen - sizeof(*hmacs);
6635 			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6636 			/* validate the hmac list */
6637 			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6638 				return (-29);
6639 			}
6640 			if (stcb->asoc.peer_hmacs != NULL)
6641 				sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6642 			stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6643 			if (stcb->asoc.peer_hmacs != NULL) {
6644 				for (i = 0; i < num_hmacs; i++) {
6645 					(void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6646 					    ntohs(hmacs->hmac_ids[i]));
6647 				}
6648 			}
6649 			got_hmacs = 1;
6650 		} else if (ptype == SCTP_CHUNK_LIST) {
6651 			int i;
6652 
6653 			if (plen > sizeof(chunks_store))
6654 				break;
6655 			if (got_chklist) {
6656 				/* already processed a Chunks list */
6657 				goto next_param;
6658 			}
6659 			phdr = sctp_get_next_param(m, offset,
6660 			    (struct sctp_paramhdr *)chunks_store,
6661 			    min(plen, sizeof(chunks_store)));
6662 			if (phdr == NULL)
6663 				return (-30);
6664 			chunks = (struct sctp_auth_chunk_list *)phdr;
6665 			num_chunks = plen - sizeof(*chunks);
6666 			if (stcb->asoc.peer_auth_chunks != NULL)
6667 				sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6668 			else
6669 				stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6670 			for (i = 0; i < num_chunks; i++) {
6671 				(void)sctp_auth_add_chunk(chunks->chunk_types[i],
6672 				    stcb->asoc.peer_auth_chunks);
6673 				/* record asconf/asconf-ack if listed */
6674 				if (chunks->chunk_types[i] == SCTP_ASCONF)
6675 					saw_asconf = 1;
6676 				if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6677 					saw_asconf_ack = 1;
6678 
6679 			}
6680 			got_chklist = 1;
6681 		} else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6682 			    (ptype == SCTP_STATE_COOKIE) ||
6683 			    (ptype == SCTP_UNRECOG_PARAM) ||
6684 			    (ptype == SCTP_COOKIE_PRESERVE) ||
6685 			    (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6686 			    (ptype == SCTP_ADD_IP_ADDRESS) ||
6687 			    (ptype == SCTP_DEL_IP_ADDRESS) ||
6688 			    (ptype == SCTP_ERROR_CAUSE_IND) ||
6689 		    (ptype == SCTP_SUCCESS_REPORT)) {
6690 			 /* don't care */ ;
6691 		} else {
6692 			if ((ptype & 0x8000) == 0x0000) {
6693 				/*
6694 				 * must stop processing the rest of the
6695 				 * param's. Any report bits were handled
6696 				 * with the call to
6697 				 * sctp_arethere_unrecognized_parameters()
6698 				 * when the INIT or INIT-ACK was first seen.
6699 				 */
6700 				break;
6701 			}
6702 		}
6703 
6704 next_param:
6705 		offset += SCTP_SIZE32(plen);
6706 		if (offset >= limit) {
6707 			break;
6708 		}
6709 		phdr = sctp_get_next_param(m, offset, &parm_buf,
6710 		    sizeof(parm_buf));
6711 	}
6712 	/* Now check to see if we need to purge any addresses */
6713 	TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6714 		if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6715 		    SCTP_ADDR_NOT_IN_ASSOC) {
6716 			/* This address has been removed from the asoc */
6717 			/* remove and free it */
6718 			stcb->asoc.numnets--;
6719 			TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6720 			sctp_free_remote_addr(net);
6721 			if (net == stcb->asoc.primary_destination) {
6722 				stcb->asoc.primary_destination = NULL;
6723 				sctp_select_primary_destination(stcb);
6724 			}
6725 		}
6726 	}
6727 	if (ecn_allowed == 0) {
6728 		stcb->asoc.ecn_allowed = 0;
6729 	}
6730 	/* validate authentication required parameters */
6731 	if (got_random && got_hmacs) {
6732 		stcb->asoc.peer_supports_auth = 1;
6733 	} else {
6734 		stcb->asoc.peer_supports_auth = 0;
6735 	}
6736 	if (!stcb->asoc.peer_supports_auth && got_chklist) {
6737 		/* peer does not support auth but sent a chunks list? */
6738 		return (-31);
6739 	}
6740 	if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6741 	    !stcb->asoc.peer_supports_auth) {
6742 		/* peer supports asconf but not auth? */
6743 		return (-32);
6744 	} else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6745 	    ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6746 		return (-33);
6747 	}
6748 	/* concatenate the full random key */
6749 	keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6750 	if (chunks != NULL) {
6751 		keylen += sizeof(*chunks) + num_chunks;
6752 	}
6753 	new_key = sctp_alloc_key(keylen);
6754 	if (new_key != NULL) {
6755 		/* copy in the RANDOM */
6756 		if (p_random != NULL) {
6757 			keylen = sizeof(*p_random) + random_len;
6758 			bcopy(p_random, new_key->key, keylen);
6759 		}
6760 		/* append in the AUTH chunks */
6761 		if (chunks != NULL) {
6762 			bcopy(chunks, new_key->key + keylen,
6763 			    sizeof(*chunks) + num_chunks);
6764 			keylen += sizeof(*chunks) + num_chunks;
6765 		}
6766 		/* append in the HMACs */
6767 		if (hmacs != NULL) {
6768 			bcopy(hmacs, new_key->key + keylen,
6769 			    sizeof(*hmacs) + hmacs_len);
6770 		}
6771 	} else {
6772 		/* failed to get memory for the key */
6773 		return (-34);
6774 	}
6775 	if (stcb->asoc.authinfo.peer_random != NULL)
6776 		sctp_free_key(stcb->asoc.authinfo.peer_random);
6777 	stcb->asoc.authinfo.peer_random = new_key;
6778 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6779 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6780 
6781 	return (0);
6782 }
6783 
6784 int
6785 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6786     struct sctp_nets *net)
6787 {
6788 	/* make sure the requested primary address exists in the assoc */
6789 	if (net == NULL && sa)
6790 		net = sctp_findnet(stcb, sa);
6791 
6792 	if (net == NULL) {
6793 		/* didn't find the requested primary address! */
6794 		return (-1);
6795 	} else {
6796 		/* set the primary address */
6797 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6798 			/* Must be confirmed, so queue to set */
6799 			net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6800 			return (0);
6801 		}
6802 		stcb->asoc.primary_destination = net;
6803 		if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
6804 			sctp_free_remote_addr(stcb->asoc.alternate);
6805 			stcb->asoc.alternate = NULL;
6806 		}
6807 		net = TAILQ_FIRST(&stcb->asoc.nets);
6808 		if (net != stcb->asoc.primary_destination) {
6809 			/*
6810 			 * first one on the list is NOT the primary
6811 			 * sctp_cmpaddr() is much more efficient if the
6812 			 * primary is the first on the list, make it so.
6813 			 */
6814 			TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6815 			TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6816 		}
6817 		return (0);
6818 	}
6819 }
6820 
6821 int
6822 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now, int save_in_twait)
6823 {
6824 	/*
6825 	 * This function serves two purposes. It will see if a TAG can be
6826 	 * re-used and return 1 for yes it is ok and 0 for don't use that
6827 	 * tag. A secondary function it will do is purge out old tags that
6828 	 * can be removed.
6829 	 */
6830 	struct sctpvtaghead *chain;
6831 	struct sctp_tagblock *twait_block;
6832 	struct sctpasochead *head;
6833 	struct sctp_tcb *stcb;
6834 	int i;
6835 
6836 	SCTP_INP_INFO_RLOCK();
6837 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6838 	    SCTP_BASE_INFO(hashasocmark))];
6839 	if (head == NULL) {
6840 		/* invalid vtag */
6841 		goto skip_vtag_check;
6842 	}
6843 	LIST_FOREACH(stcb, head, sctp_asocs) {
6844 		/*
6845 		 * We choose not to lock anything here. TCB's can't be
6846 		 * removed since we have the read lock, so they can't be
6847 		 * freed on us, same thing for the INP. I may be wrong with
6848 		 * this assumption, but we will go with it for now :-)
6849 		 */
6850 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6851 			continue;
6852 		}
6853 		if (stcb->asoc.my_vtag == tag) {
6854 			/* candidate */
6855 			if (stcb->rport != rport) {
6856 				continue;
6857 			}
6858 			if (stcb->sctp_ep->sctp_lport != lport) {
6859 				continue;
6860 			}
6861 			/* Its a used tag set */
6862 			SCTP_INP_INFO_RUNLOCK();
6863 			return (0);
6864 		}
6865 	}
6866 skip_vtag_check:
6867 
6868 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
6869 	/* Now what about timed wait ? */
6870 	if (!LIST_EMPTY(chain)) {
6871 		/*
6872 		 * Block(s) are present, lets see if we have this tag in the
6873 		 * list
6874 		 */
6875 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6876 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6877 				if (twait_block->vtag_block[i].v_tag == 0) {
6878 					/* not used */
6879 					continue;
6880 				} else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6881 				    now->tv_sec) {
6882 					/* Audit expires this guy */
6883 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
6884 					twait_block->vtag_block[i].v_tag = 0;
6885 					twait_block->vtag_block[i].lport = 0;
6886 					twait_block->vtag_block[i].rport = 0;
6887 				} else if ((twait_block->vtag_block[i].v_tag == tag) &&
6888 					    (twait_block->vtag_block[i].lport == lport) &&
6889 				    (twait_block->vtag_block[i].rport == rport)) {
6890 					/* Bad tag, sorry :< */
6891 					SCTP_INP_INFO_RUNLOCK();
6892 					return (0);
6893 				}
6894 			}
6895 		}
6896 	}
6897 	SCTP_INP_INFO_RUNLOCK();
6898 	return (1);
6899 }
6900 
6901 
6902 static sctp_assoc_t reneged_asoc_ids[256];
6903 static uint8_t reneged_at = 0;
6904 
6905 
6906 static void
6907 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6908 {
6909 	/*
6910 	 * We must hunt this association for MBUF's past the cumack (i.e.
6911 	 * out of order data that we can renege on).
6912 	 */
6913 	struct sctp_association *asoc;
6914 	struct sctp_tmit_chunk *chk, *nchk;
6915 	uint32_t cumulative_tsn_p1;
6916 	struct sctp_queued_to_read *ctl, *nctl;
6917 	int cnt, strmat;
6918 	uint32_t gap, i;
6919 	int fnd = 0;
6920 
6921 	/* We look for anything larger than the cum-ack + 1 */
6922 
6923 	asoc = &stcb->asoc;
6924 	if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6925 		/* none we can reneg on. */
6926 		return;
6927 	}
6928 	SCTP_STAT_INCR(sctps_protocol_drains_done);
6929 	cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6930 	cnt = 0;
6931 	/* First look in the re-assembly queue */
6932 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
6933 		if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
6934 			/* Yep it is above cum-ack */
6935 			cnt++;
6936 			SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
6937 			asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6938 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6939 			SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6940 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6941 			if (chk->data) {
6942 				sctp_m_freem(chk->data);
6943 				chk->data = NULL;
6944 			}
6945 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6946 		}
6947 	}
6948 	/* Ok that was fun, now we will drain all the inbound streams? */
6949 	for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6950 		TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
6951 			if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
6952 				/* Yep it is above cum-ack */
6953 				cnt++;
6954 				SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
6955 				asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6956 				sctp_ucount_decr(asoc->cnt_on_all_streams);
6957 				SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6958 				TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
6959 				if (ctl->data) {
6960 					sctp_m_freem(ctl->data);
6961 					ctl->data = NULL;
6962 				}
6963 				sctp_free_remote_addr(ctl->whoFrom);
6964 				SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6965 				SCTP_DECR_READQ_COUNT();
6966 			}
6967 		}
6968 	}
6969 	if (cnt) {
6970 		/* We must back down to see what the new highest is */
6971 		for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6972 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6973 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6974 				asoc->highest_tsn_inside_map = i;
6975 				fnd = 1;
6976 				break;
6977 			}
6978 		}
6979 		if (!fnd) {
6980 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6981 		}
6982 		/*
6983 		 * Question, should we go through the delivery queue? The
6984 		 * only reason things are on here is the app not reading OR
6985 		 * a p-d-api up. An attacker COULD send enough in to
6986 		 * initiate the PD-API and then send a bunch of stuff to
6987 		 * other streams... these would wind up on the delivery
6988 		 * queue.. and then we would not get to them. But in order
6989 		 * to do this I then have to back-track and un-deliver
6990 		 * sequence numbers in streams.. el-yucko. I think for now
6991 		 * we will NOT look at the delivery queue and leave it to be
6992 		 * something to consider later. An alternative would be to
6993 		 * abort the P-D-API with a notification and then deliver
6994 		 * the data.... Or another method might be to keep track of
6995 		 * how many times the situation occurs and if we see a
6996 		 * possible attack underway just abort the association.
6997 		 */
6998 #ifdef SCTP_DEBUG
6999 		SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7000 #endif
7001 		/*
7002 		 * Now do we need to find a new
7003 		 * asoc->highest_tsn_inside_map?
7004 		 */
7005 		asoc->last_revoke_count = cnt;
7006 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
7007 		/* sa_ignore NO_NULL_CHK */
7008 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7009 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
7010 		reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
7011 		reneged_at++;
7012 	}
7013 	/*
7014 	 * Another issue, in un-setting the TSN's in the mapping array we
7015 	 * DID NOT adjust the highest_tsn marker.  This will cause one of
7016 	 * two things to occur. It may cause us to do extra work in checking
7017 	 * for our mapping array movement. More importantly it may cause us
7018 	 * to SACK every datagram. This may not be a bad thing though since
7019 	 * we will recover once we get our cum-ack above and all this stuff
7020 	 * we dumped recovered.
7021 	 */
7022 }
7023 
7024 void
7025 sctp_drain()
7026 {
7027 	/*
7028 	 * We must walk the PCB lists for ALL associations here. The system
7029 	 * is LOW on MBUF's and needs help. This is where reneging will
7030 	 * occur. We really hope this does NOT happen!
7031 	 */
7032 	VNET_ITERATOR_DECL(vnet_iter);
7033 	VNET_LIST_RLOCK_NOSLEEP();
7034 	VNET_FOREACH(vnet_iter) {
7035 		CURVNET_SET(vnet_iter);
7036 		struct sctp_inpcb *inp;
7037 		struct sctp_tcb *stcb;
7038 
7039 		SCTP_STAT_INCR(sctps_protocol_drain_calls);
7040 		if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7041 #ifdef VIMAGE
7042 			continue;
7043 #else
7044 			return;
7045 #endif
7046 		}
7047 		SCTP_INP_INFO_RLOCK();
7048 		LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7049 			/* For each endpoint */
7050 			SCTP_INP_RLOCK(inp);
7051 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7052 				/* For each association */
7053 				SCTP_TCB_LOCK(stcb);
7054 				sctp_drain_mbufs(inp, stcb);
7055 				SCTP_TCB_UNLOCK(stcb);
7056 			}
7057 			SCTP_INP_RUNLOCK(inp);
7058 		}
7059 		SCTP_INP_INFO_RUNLOCK();
7060 		CURVNET_RESTORE();
7061 	}
7062 	VNET_LIST_RUNLOCK_NOSLEEP();
7063 }
7064 
7065 /*
7066  * start a new iterator
7067  * iterates through all endpoints and associations based on the pcb_state
7068  * flags and asoc_state.  "af" (mandatory) is executed for all matching
7069  * assocs and "ef" (optional) is executed when the iterator completes.
7070  * "inpf" (optional) is executed for each new endpoint as it is being
7071  * iterated through. inpe (optional) is called when the inp completes
7072  * its way through all the stcbs.
7073  */
7074 int
7075 sctp_initiate_iterator(inp_func inpf,
7076     asoc_func af,
7077     inp_func inpe,
7078     uint32_t pcb_state,
7079     uint32_t pcb_features,
7080     uint32_t asoc_state,
7081     void *argp,
7082     uint32_t argi,
7083     end_func ef,
7084     struct sctp_inpcb *s_inp,
7085     uint8_t chunk_output_off)
7086 {
7087 	struct sctp_iterator *it = NULL;
7088 
7089 	if (af == NULL) {
7090 		return (-1);
7091 	}
7092 	SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7093 	    SCTP_M_ITER);
7094 	if (it == NULL) {
7095 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7096 		return (ENOMEM);
7097 	}
7098 	memset(it, 0, sizeof(*it));
7099 	it->function_assoc = af;
7100 	it->function_inp = inpf;
7101 	if (inpf)
7102 		it->done_current_ep = 0;
7103 	else
7104 		it->done_current_ep = 1;
7105 	it->function_atend = ef;
7106 	it->pointer = argp;
7107 	it->val = argi;
7108 	it->pcb_flags = pcb_state;
7109 	it->pcb_features = pcb_features;
7110 	it->asoc_state = asoc_state;
7111 	it->function_inp_end = inpe;
7112 	it->no_chunk_output = chunk_output_off;
7113 	it->vn = curvnet;
7114 	if (s_inp) {
7115 		/* Assume lock is held here */
7116 		it->inp = s_inp;
7117 		SCTP_INP_INCR_REF(it->inp);
7118 		it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7119 	} else {
7120 		SCTP_INP_INFO_RLOCK();
7121 		it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7122 		if (it->inp) {
7123 			SCTP_INP_INCR_REF(it->inp);
7124 		}
7125 		SCTP_INP_INFO_RUNLOCK();
7126 		it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7127 
7128 	}
7129 	SCTP_IPI_ITERATOR_WQ_LOCK();
7130 
7131 	TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7132 	if (sctp_it_ctl.iterator_running == 0) {
7133 		sctp_wakeup_iterator();
7134 	}
7135 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
7136 	/* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
7137 	return (0);
7138 }
7139