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