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