xref: /freebsd/sys/netinet/sctp_pcb.c (revision e8623834ca29b562687db945bdd12a3e2fe4aeb1)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4830d754dSRandall Stewart  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
3693164cf9SRandall Stewart #include <sys/proc.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctputil.h>
41f8829a4aSRandall Stewart #include <netinet/sctp.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
4642551e99SRandall Stewart #include <netinet/sctp_bsd_addr.h>
474474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
48c54a18d2SRandall Stewart #include <netinet/udp.h>
494474d71aSMichael Tuexen #endif
5058bdb691SMichael Tuexen #ifdef INET6
5158bdb691SMichael Tuexen #include <netinet6/ip6_var.h>
5258bdb691SMichael Tuexen #endif
53bfc46083SRandall Stewart #include <sys/sched.h>
54bfc46083SRandall Stewart #include <sys/smp.h>
55bfc46083SRandall Stewart #include <sys/unistd.h>
56f8829a4aSRandall Stewart 
57f8829a4aSRandall Stewart /* FIX: we don't handle multiple link local scopes */
58f8829a4aSRandall Stewart /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
595e2c2d87SRandall Stewart #ifdef INET6
60f8829a4aSRandall Stewart int
SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 * a,struct sockaddr_in6 * b)61c54a18d2SRandall Stewart SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
62f8829a4aSRandall Stewart {
63c54a18d2SRandall Stewart 	struct sockaddr_in6 tmp_a, tmp_b;
64f8829a4aSRandall Stewart 
65c54a18d2SRandall Stewart 	memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
66482444b4SRandall Stewart 	if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
6760990c0cSMichael Tuexen 		return (0);
68c54a18d2SRandall Stewart 	}
69c54a18d2SRandall Stewart 	memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
70482444b4SRandall Stewart 	if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
7160990c0cSMichael Tuexen 		return (0);
72c54a18d2SRandall Stewart 	}
73c54a18d2SRandall Stewart 	return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
74f8829a4aSRandall Stewart }
755e2c2d87SRandall Stewart #endif
765e2c2d87SRandall Stewart 
77f8829a4aSRandall Stewart void
sctp_fill_pcbinfo(struct sctp_pcbinfo * spcb)78f8829a4aSRandall Stewart sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
79f8829a4aSRandall Stewart {
80f8829a4aSRandall Stewart 	/*
81f8829a4aSRandall Stewart 	 * We really don't need to lock this, but I will just because it
82f8829a4aSRandall Stewart 	 * does not hurt.
83f8829a4aSRandall Stewart 	 */
84f8829a4aSRandall Stewart 	SCTP_INP_INFO_RLOCK();
85b3f1ea41SRandall Stewart 	spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
86b3f1ea41SRandall Stewart 	spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
87b3f1ea41SRandall Stewart 	spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
88b3f1ea41SRandall Stewart 	spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
89b3f1ea41SRandall Stewart 	spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
90b3f1ea41SRandall Stewart 	spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
91b3f1ea41SRandall Stewart 	spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
92b3f1ea41SRandall Stewart 	spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
93f8829a4aSRandall Stewart 	SCTP_INP_INFO_RUNLOCK();
94f8829a4aSRandall Stewart }
95f8829a4aSRandall Stewart 
96a1cb341bSMichael Tuexen /*-
9742551e99SRandall Stewart  * Addresses are added to VRF's (Virtual Router's). For BSD we
9842551e99SRandall Stewart  * have only the default VRF 0. We maintain a hash list of
9942551e99SRandall Stewart  * VRF's. Each VRF has its own list of sctp_ifn's. Each of
10042551e99SRandall Stewart  * these has a list of addresses. When we add a new address
10142551e99SRandall Stewart  * to a VRF we lookup the ifn/ifn_index, if the ifn does
10242551e99SRandall Stewart  * not exist we create it and add it to the list of IFN's
10342551e99SRandall Stewart  * within the VRF. Once we have the sctp_ifn, we add the
10442551e99SRandall Stewart  * address to the list. So we look something like:
10542551e99SRandall Stewart  *
10642551e99SRandall Stewart  * hash-vrf-table
10742551e99SRandall Stewart  *   vrf-> ifn-> ifn -> ifn
10842551e99SRandall Stewart  *   vrf    |
10942551e99SRandall Stewart  *    ...   +--ifa-> ifa -> ifa
11042551e99SRandall Stewart  *   vrf
11142551e99SRandall Stewart  *
112b1ce21c6SRebecca Cran  * We keep these separate lists since the SCTP subsystem will
11342551e99SRandall Stewart  * point to these from its source address selection nets structure.
11442551e99SRandall Stewart  * When an address is deleted it does not happen right away on
11542551e99SRandall Stewart  * the SCTP side, it gets scheduled. What we do when a
11642551e99SRandall Stewart  * delete happens is immediately remove the address from
11742551e99SRandall Stewart  * the master list and decrement the refcount. As our
11842551e99SRandall Stewart  * addip iterator works through and frees the src address
11942551e99SRandall Stewart  * selection pointing to the sctp_ifa, eventually the refcount
12042551e99SRandall Stewart  * will reach 0 and we will delete it. Note that it is assumed
12142551e99SRandall Stewart  * that any locking on system level ifn/ifa is done at the
12242551e99SRandall Stewart  * caller of these functions and these routines will only
12342551e99SRandall Stewart  * lock the SCTP structures as they add or delete things.
12442551e99SRandall Stewart  *
12542551e99SRandall Stewart  * Other notes on VRF concepts.
12642551e99SRandall Stewart  *  - An endpoint can be in multiple VRF's
12742551e99SRandall Stewart  *  - An association lives within a VRF and only one VRF.
12842551e99SRandall Stewart  *  - Any incoming packet we can deduce the VRF for by
12942551e99SRandall Stewart  *    looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
13042551e99SRandall Stewart  *  - Any downward send call or connect call must supply the
13142551e99SRandall Stewart  *    VRF via ancillary data or via some sort of set default
13242551e99SRandall Stewart  *    VRF socket option call (again for BSD no brainer since
13342551e99SRandall Stewart  *    the VRF is always 0).
13442551e99SRandall Stewart  *  - An endpoint may add multiple VRF's to it.
13542551e99SRandall Stewart  *  - Listening sockets can accept associations in any
13642551e99SRandall Stewart  *    of the VRF's they are in but the assoc will end up
13742551e99SRandall Stewart  *    in only one VRF (gotten from the packet or connect/send).
13842551e99SRandall Stewart  *
13942551e99SRandall Stewart  */
14042551e99SRandall Stewart 
14142551e99SRandall Stewart struct sctp_vrf *
sctp_allocate_vrf(int vrf_id)14217205eccSRandall Stewart sctp_allocate_vrf(int vrf_id)
14342551e99SRandall Stewart {
14442551e99SRandall Stewart 	struct sctp_vrf *vrf = NULL;
14542551e99SRandall Stewart 	struct sctp_vrflist *bucket;
14642551e99SRandall Stewart 
14742551e99SRandall Stewart 	/* First allocate the VRF structure */
14817205eccSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
14942551e99SRandall Stewart 	if (vrf) {
15042551e99SRandall Stewart 		/* Already allocated */
15142551e99SRandall Stewart 		return (vrf);
15242551e99SRandall Stewart 	}
15342551e99SRandall Stewart 	SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
154207304d4SRandall Stewart 	    SCTP_M_VRF);
15542551e99SRandall Stewart 	if (vrf == NULL) {
15642551e99SRandall Stewart 		/* No memory */
15742551e99SRandall Stewart #ifdef INVARIANTS
15817205eccSRandall Stewart 		panic("No memory for VRF:%d", vrf_id);
15942551e99SRandall Stewart #endif
16042551e99SRandall Stewart 		return (NULL);
16142551e99SRandall Stewart 	}
16242551e99SRandall Stewart 	/* setup the VRF */
16342551e99SRandall Stewart 	memset(vrf, 0, sizeof(struct sctp_vrf));
16417205eccSRandall Stewart 	vrf->vrf_id = vrf_id;
16542551e99SRandall Stewart 	LIST_INIT(&vrf->ifnlist);
16642551e99SRandall Stewart 	vrf->total_ifa_count = 0;
16780fefe0aSRandall Stewart 	vrf->refcount = 0;
168f4c93d24SRandall Stewart 	/* now also setup table ids */
169f4c93d24SRandall Stewart 	SCTP_INIT_VRF_TABLEID(vrf);
1706a27c376SRandall Stewart 	/* Init the HASH of addresses */
17117205eccSRandall Stewart 	vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
17217205eccSRandall Stewart 	    &vrf->vrf_addr_hashmark);
1736a27c376SRandall Stewart 	if (vrf->vrf_addr_hash == NULL) {
1746a27c376SRandall Stewart 		/* No memory */
1756a27c376SRandall Stewart #ifdef INVARIANTS
17617205eccSRandall Stewart 		panic("No memory for VRF:%d", vrf_id);
17717205eccSRandall Stewart #endif
178207304d4SRandall Stewart 		SCTP_FREE(vrf, SCTP_M_VRF);
17917205eccSRandall Stewart 		return (NULL);
18017205eccSRandall Stewart 	}
1810053ed28SMichael Tuexen 
18242551e99SRandall Stewart 	/* Add it to the hash table */
183b3f1ea41SRandall Stewart 	bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
18442551e99SRandall Stewart 	LIST_INSERT_HEAD(bucket, vrf, next_vrf);
185b3f1ea41SRandall Stewart 	atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
18642551e99SRandall Stewart 	return (vrf);
18742551e99SRandall Stewart }
18842551e99SRandall Stewart 
18942551e99SRandall Stewart struct sctp_ifn *
sctp_find_ifn(void * ifn,uint32_t ifn_index)190f4c93d24SRandall Stewart sctp_find_ifn(void *ifn, uint32_t ifn_index)
19142551e99SRandall Stewart {
19242551e99SRandall Stewart 	struct sctp_ifn *sctp_ifnp;
19317205eccSRandall Stewart 	struct sctp_ifnlist *hash_ifn_head;
19442551e99SRandall Stewart 
1957f0ad227SMichael Tuexen 	SCTP_IPI_ADDR_LOCK_ASSERT();
196523913c9SMichael Tuexen 	KASSERT(ifn != NULL, ("sctp_find_ifn(NULL, %u) called", ifn_index));
197b3f1ea41SRandall Stewart 	hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
19817205eccSRandall Stewart 	LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
199523913c9SMichael Tuexen 		if (sctp_ifnp->ifn_index == ifn_index &&
200523913c9SMichael Tuexen 		    sctp_ifnp->ifn_p == ifn) {
201e4ac0183SMichael Tuexen 			break;
202e4ac0183SMichael Tuexen 		}
203e4ac0183SMichael Tuexen 	}
20442551e99SRandall Stewart 	return (sctp_ifnp);
20542551e99SRandall Stewart }
20642551e99SRandall Stewart 
20742551e99SRandall Stewart struct sctp_vrf *
sctp_find_vrf(uint32_t vrf_id)20817205eccSRandall Stewart sctp_find_vrf(uint32_t vrf_id)
20942551e99SRandall Stewart {
21042551e99SRandall Stewart 	struct sctp_vrflist *bucket;
21142551e99SRandall Stewart 	struct sctp_vrf *liste;
21242551e99SRandall Stewart 
213b3f1ea41SRandall Stewart 	bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
21442551e99SRandall Stewart 	LIST_FOREACH(liste, bucket, next_vrf) {
21517205eccSRandall Stewart 		if (vrf_id == liste->vrf_id) {
21642551e99SRandall Stewart 			return (liste);
21742551e99SRandall Stewart 		}
21842551e99SRandall Stewart 	}
21942551e99SRandall Stewart 	return (NULL);
22042551e99SRandall Stewart }
22142551e99SRandall Stewart 
22280fefe0aSRandall Stewart void
sctp_free_vrf(struct sctp_vrf * vrf)22380fefe0aSRandall Stewart sctp_free_vrf(struct sctp_vrf *vrf)
22480fefe0aSRandall Stewart {
225830d754dSRandall Stewart 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
2267a846e9aSRandall Stewart 		if (vrf->vrf_addr_hash) {
2277a846e9aSRandall Stewart 			SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
2287a846e9aSRandall Stewart 			vrf->vrf_addr_hash = NULL;
2297a846e9aSRandall Stewart 		}
23080fefe0aSRandall Stewart 		/* We zero'd the count */
23180fefe0aSRandall Stewart 		LIST_REMOVE(vrf, next_vrf);
23280fefe0aSRandall Stewart 		SCTP_FREE(vrf, SCTP_M_VRF);
233b3f1ea41SRandall Stewart 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
23480fefe0aSRandall Stewart 	}
23580fefe0aSRandall Stewart }
236bff64a4dSRandall Stewart 
237e1a09d1eSMichael Tuexen static void
sctp_free_ifn(struct sctp_ifn * sctp_ifnp)23817205eccSRandall Stewart sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
23917205eccSRandall Stewart {
240830d754dSRandall Stewart 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
24117205eccSRandall Stewart 		/* We zero'd the count */
24280fefe0aSRandall Stewart 		if (sctp_ifnp->vrf) {
24380fefe0aSRandall Stewart 			sctp_free_vrf(sctp_ifnp->vrf);
24480fefe0aSRandall Stewart 		}
245207304d4SRandall Stewart 		SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
246b3f1ea41SRandall Stewart 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
24717205eccSRandall Stewart 	}
24817205eccSRandall Stewart }
24917205eccSRandall Stewart 
25017205eccSRandall Stewart void
sctp_free_ifa(struct sctp_ifa * sctp_ifap)25142551e99SRandall Stewart sctp_free_ifa(struct sctp_ifa *sctp_ifap)
25242551e99SRandall Stewart {
253830d754dSRandall Stewart 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
25442551e99SRandall Stewart 		/* We zero'd the count */
25580fefe0aSRandall Stewart 		if (sctp_ifap->ifn_p) {
25680fefe0aSRandall Stewart 			sctp_free_ifn(sctp_ifap->ifn_p);
25780fefe0aSRandall Stewart 		}
258207304d4SRandall Stewart 		SCTP_FREE(sctp_ifap, SCTP_M_IFA);
259b3f1ea41SRandall Stewart 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
26042551e99SRandall Stewart 	}
26142551e99SRandall Stewart }
26242551e99SRandall Stewart 
26317205eccSRandall Stewart static void
sctp_delete_ifn(struct sctp_ifn * sctp_ifnp)2642e9761ebSMichael Tuexen sctp_delete_ifn(struct sctp_ifn *sctp_ifnp)
26517205eccSRandall Stewart {
26617205eccSRandall Stewart 
2672e9761ebSMichael Tuexen 	SCTP_IPI_ADDR_WLOCK_ASSERT();
2682e9761ebSMichael Tuexen 	if (sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index) == NULL) {
26917205eccSRandall Stewart 		/* Not in the list.. sorry */
27017205eccSRandall Stewart 		return;
27117205eccSRandall Stewart 	}
27217205eccSRandall Stewart 	LIST_REMOVE(sctp_ifnp, next_bucket);
27317205eccSRandall Stewart 	LIST_REMOVE(sctp_ifnp, next_ifn);
27417205eccSRandall Stewart 	/* Take away the reference, and possibly free it */
27517205eccSRandall Stewart 	sctp_free_ifn(sctp_ifnp);
27617205eccSRandall Stewart }
27717205eccSRandall Stewart 
278baf3da66SRandall Stewart /*-
279baf3da66SRandall Stewart  * Add an ifa to an ifn.
280baf3da66SRandall Stewart  * Register the interface as necessary.
281baf3da66SRandall Stewart  */
282baf3da66SRandall Stewart static void
sctp_add_ifa_to_ifn(struct sctp_ifn * sctp_ifnp,struct sctp_ifa * sctp_ifap)283baf3da66SRandall Stewart sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
284baf3da66SRandall Stewart {
285baf3da66SRandall Stewart 	int ifa_af;
286baf3da66SRandall Stewart 
2874466a97eSMichael Tuexen 	SCTP_IPI_ADDR_WLOCK_ASSERT();
288baf3da66SRandall Stewart 	LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
289baf3da66SRandall Stewart 	sctp_ifap->ifn_p = sctp_ifnp;
290baf3da66SRandall Stewart 	atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
291baf3da66SRandall Stewart 	/* update address counts */
292baf3da66SRandall Stewart 	sctp_ifnp->ifa_count++;
293baf3da66SRandall Stewart 	ifa_af = sctp_ifap->address.sa.sa_family;
294e6194c2eSMichael Tuexen 	switch (ifa_af) {
295e6194c2eSMichael Tuexen #ifdef INET
296e6194c2eSMichael Tuexen 	case AF_INET:
297baf3da66SRandall Stewart 		sctp_ifnp->num_v4++;
298e6194c2eSMichael Tuexen 		break;
299e6194c2eSMichael Tuexen #endif
300e6194c2eSMichael Tuexen #ifdef INET6
301e6194c2eSMichael Tuexen 	case AF_INET6:
302baf3da66SRandall Stewart 		sctp_ifnp->num_v6++;
303e6194c2eSMichael Tuexen 		break;
304e6194c2eSMichael Tuexen #endif
305e6194c2eSMichael Tuexen 	default:
306e6194c2eSMichael Tuexen 		break;
307e6194c2eSMichael Tuexen 	}
308baf3da66SRandall Stewart 	if (sctp_ifnp->ifa_count == 1) {
309baf3da66SRandall Stewart 		/* register the new interface */
310baf3da66SRandall Stewart 		sctp_ifnp->registered_af = ifa_af;
311baf3da66SRandall Stewart 	}
312baf3da66SRandall Stewart }
313baf3da66SRandall Stewart 
314baf3da66SRandall Stewart /*-
315baf3da66SRandall Stewart  * Remove an ifa from its ifn.
316baf3da66SRandall Stewart  * If no more addresses exist, remove the ifn too. Otherwise, re-register
317baf3da66SRandall Stewart  * the interface based on the remaining address families left.
318baf3da66SRandall Stewart  */
319baf3da66SRandall Stewart static void
sctp_remove_ifa_from_ifn(struct sctp_ifa * sctp_ifap)320baf3da66SRandall Stewart sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
321baf3da66SRandall Stewart {
3224466a97eSMichael Tuexen 	SCTP_IPI_ADDR_WLOCK_ASSERT();
323baf3da66SRandall Stewart 	LIST_REMOVE(sctp_ifap, next_ifa);
324baf3da66SRandall Stewart 	if (sctp_ifap->ifn_p) {
325baf3da66SRandall Stewart 		/* update address counts */
326baf3da66SRandall Stewart 		sctp_ifap->ifn_p->ifa_count--;
327e6194c2eSMichael Tuexen 		switch (sctp_ifap->address.sa.sa_family) {
328e6194c2eSMichael Tuexen #ifdef INET
329e6194c2eSMichael Tuexen 		case AF_INET:
330baf3da66SRandall Stewart 			sctp_ifap->ifn_p->num_v4--;
331e6194c2eSMichael Tuexen 			break;
332e6194c2eSMichael Tuexen #endif
333e6194c2eSMichael Tuexen #ifdef INET6
334e6194c2eSMichael Tuexen 		case AF_INET6:
335e6194c2eSMichael Tuexen 			sctp_ifap->ifn_p->num_v6--;
336e6194c2eSMichael Tuexen 			break;
337e6194c2eSMichael Tuexen #endif
338e6194c2eSMichael Tuexen 		default:
339e6194c2eSMichael Tuexen 			break;
340e6194c2eSMichael Tuexen 		}
341baf3da66SRandall Stewart 
34283fc1165SMichael Tuexen 		if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
343baf3da66SRandall Stewart 			/* remove the ifn, possibly freeing it */
3442e9761ebSMichael Tuexen 			sctp_delete_ifn(sctp_ifap->ifn_p);
345baf3da66SRandall Stewart 		} else {
346baf3da66SRandall Stewart 			/* re-register address family type, if needed */
347baf3da66SRandall Stewart 			if ((sctp_ifap->ifn_p->num_v6 == 0) &&
348baf3da66SRandall Stewart 			    (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
349baf3da66SRandall Stewart 				sctp_ifap->ifn_p->registered_af = AF_INET;
350baf3da66SRandall Stewart 			} else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
351baf3da66SRandall Stewart 			    (sctp_ifap->ifn_p->registered_af == AF_INET)) {
352baf3da66SRandall Stewart 				sctp_ifap->ifn_p->registered_af = AF_INET6;
353baf3da66SRandall Stewart 			}
354baf3da66SRandall Stewart 			/* free the ifn refcount */
355baf3da66SRandall Stewart 			sctp_free_ifn(sctp_ifap->ifn_p);
356baf3da66SRandall Stewart 		}
357baf3da66SRandall Stewart 		sctp_ifap->ifn_p = NULL;
358baf3da66SRandall Stewart 	}
359baf3da66SRandall Stewart }
36017205eccSRandall Stewart 
36142551e99SRandall Stewart struct sctp_ifa *
sctp_add_addr_to_vrf(uint32_t vrf_id,void * ifn,uint32_t ifn_index,uint32_t ifn_type,const char * if_name,void * ifa,struct sockaddr * addr,uint32_t ifa_flags,int dynamic_add)36217205eccSRandall Stewart sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
363baf3da66SRandall Stewart     uint32_t ifn_type, const char *if_name, void *ifa,
364baf3da66SRandall Stewart     struct sockaddr *addr, uint32_t ifa_flags,
36517205eccSRandall Stewart     int dynamic_add)
36642551e99SRandall Stewart {
36742551e99SRandall Stewart 	struct sctp_vrf *vrf;
3687f0ad227SMichael Tuexen 	struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp;
3697f0ad227SMichael Tuexen 	struct sctp_ifa *sctp_ifap, *new_sctp_ifap;
37017205eccSRandall Stewart 	struct sctp_ifalist *hash_addr_head;
37117205eccSRandall Stewart 	struct sctp_ifnlist *hash_ifn_head;
3726a27c376SRandall Stewart 	uint32_t hash_of_addr;
37342551e99SRandall Stewart 
374baf3da66SRandall Stewart #ifdef SCTP_DEBUG
375baf3da66SRandall Stewart 	SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
376baf3da66SRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
377baf3da66SRandall Stewart #endif
3787f0ad227SMichael Tuexen 	SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *,
3797f0ad227SMichael Tuexen 	    sizeof(struct sctp_ifn), SCTP_M_IFN);
3807f0ad227SMichael Tuexen 	if (new_sctp_ifnp == NULL) {
3817f0ad227SMichael Tuexen #ifdef INVARIANTS
3827f0ad227SMichael Tuexen 		panic("No memory for IFN");
3837f0ad227SMichael Tuexen #endif
3847f0ad227SMichael Tuexen 		return (NULL);
3857f0ad227SMichael Tuexen 	}
3867f0ad227SMichael Tuexen 	SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
3877f0ad227SMichael Tuexen 	if (new_sctp_ifap == NULL) {
3887f0ad227SMichael Tuexen #ifdef INVARIANTS
3897f0ad227SMichael Tuexen 		panic("No memory for IFA");
3907f0ad227SMichael Tuexen #endif
3917f0ad227SMichael Tuexen 		SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
3927f0ad227SMichael Tuexen 		return (NULL);
3937f0ad227SMichael Tuexen 	}
3947f0ad227SMichael Tuexen 
395c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_WLOCK();
39680fefe0aSRandall Stewart 	sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
39780fefe0aSRandall Stewart 	if (sctp_ifnp) {
39880fefe0aSRandall Stewart 		vrf = sctp_ifnp->vrf;
39980fefe0aSRandall Stewart 	} else {
40017205eccSRandall Stewart 		vrf = sctp_find_vrf(vrf_id);
40142551e99SRandall Stewart 		if (vrf == NULL) {
40217205eccSRandall Stewart 			vrf = sctp_allocate_vrf(vrf_id);
40342551e99SRandall Stewart 			if (vrf == NULL) {
404c99efcf6SRandall Stewart 				SCTP_IPI_ADDR_WUNLOCK();
4057f0ad227SMichael Tuexen 				SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
4067f0ad227SMichael Tuexen 				SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
40742551e99SRandall Stewart 				return (NULL);
40842551e99SRandall Stewart 			}
40942551e99SRandall Stewart 		}
41080fefe0aSRandall Stewart 	}
41142551e99SRandall Stewart 	if (sctp_ifnp == NULL) {
41242551e99SRandall Stewart 		/*
41342551e99SRandall Stewart 		 * build one and add it, can't hold lock until after malloc
41442551e99SRandall Stewart 		 * done though.
41542551e99SRandall Stewart 		 */
4167f0ad227SMichael Tuexen 		sctp_ifnp = new_sctp_ifnp;
4177f0ad227SMichael Tuexen 		new_sctp_ifnp = NULL;
418eacc51c5SRandall Stewart 		memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
41942551e99SRandall Stewart 		sctp_ifnp->ifn_index = ifn_index;
42042551e99SRandall Stewart 		sctp_ifnp->ifn_p = ifn;
42142551e99SRandall Stewart 		sctp_ifnp->ifn_type = ifn_type;
422b201f536SRandall Stewart 		sctp_ifnp->refcount = 0;
42342551e99SRandall Stewart 		sctp_ifnp->vrf = vrf;
42480fefe0aSRandall Stewart 		atomic_add_int(&vrf->refcount, 1);
4253ca204c9SMichael Tuexen 		sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index);
42617205eccSRandall Stewart 		if (if_name != NULL) {
427999f86d6SMichael Tuexen 			SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
42817205eccSRandall Stewart 		} else {
429999f86d6SMichael Tuexen 			SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
43017205eccSRandall Stewart 		}
431b3f1ea41SRandall Stewart 		hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
43242551e99SRandall Stewart 		LIST_INIT(&sctp_ifnp->ifalist);
43317205eccSRandall Stewart 		LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
43442551e99SRandall Stewart 		LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
435b3f1ea41SRandall Stewart 		atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
43642551e99SRandall Stewart 	}
437851b7298SRandall Stewart 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
438a05620b0SMichael Tuexen 	if (sctp_ifap != NULL) {
439a05620b0SMichael Tuexen 		/* The address being added is already or still known. */
440a05620b0SMichael Tuexen 		if (sctp_ifap->ifn_p != NULL) {
441523913c9SMichael Tuexen 			if (sctp_ifap->ifn_p->ifn_index == ifn_index &&
442523913c9SMichael Tuexen 			    sctp_ifap->ifn_p->ifn_p == ifn) {
443a05620b0SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_PCB4,
444a05620b0SMichael Tuexen 				    "Using existing ifn %s (0x%x) for ifa %p\n",
445baf3da66SRandall Stewart 				    sctp_ifap->ifn_p->ifn_name, ifn_index,
446dd294dceSMichael Tuexen 				    (void *)sctp_ifap);
44702478e65SMichael Tuexen 				if (new_sctp_ifnp == NULL) {
448a05620b0SMichael Tuexen 					/* Remove the created one not used. */
4492e9761ebSMichael Tuexen 					sctp_delete_ifn(sctp_ifnp);
45080fefe0aSRandall Stewart 				}
45142551e99SRandall Stewart 				if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
452a05620b0SMichael Tuexen 					/* Switch back to active. */
453a05620b0SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_PCB4,
454a05620b0SMichael Tuexen 					    "Clearing deleted ifa flag\n");
45542551e99SRandall Stewart 					sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
45642551e99SRandall Stewart 					sctp_ifap->ifn_p = sctp_ifnp;
45780fefe0aSRandall Stewart 					atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
458baf3da66SRandall Stewart 				}
45942551e99SRandall Stewart 			} else {
46042551e99SRandall Stewart 				/*
461034b88b0SMichael Tuexen 				 * The last IFN gets the address, remove the
462d08713dcSMichael Tuexen 				 * old one.
46342551e99SRandall Stewart 				 */
464a05620b0SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_PCB4,
465a05620b0SMichael Tuexen 				    "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
466a05620b0SMichael Tuexen 				    (void *)sctp_ifap,
467a05620b0SMichael Tuexen 				    sctp_ifap->ifn_p->ifn_name,
468baf3da66SRandall Stewart 				    sctp_ifap->ifn_p->ifn_index, if_name,
469baf3da66SRandall Stewart 				    ifn_index);
470baf3da66SRandall Stewart 				/* remove the address from the old ifn */
471baf3da66SRandall Stewart 				sctp_remove_ifa_from_ifn(sctp_ifap);
472baf3da66SRandall Stewart 				/* move the address over to the new ifn */
473baf3da66SRandall Stewart 				sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
474a05620b0SMichael Tuexen 			}
47542551e99SRandall Stewart 		} else {
476a05620b0SMichael Tuexen 			/* Repair ifn_p, which was NULL... */
47742551e99SRandall Stewart 			sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
478a05620b0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_PCB4,
479a05620b0SMichael Tuexen 			    "Repairing ifn %p for ifa %p\n",
480dd294dceSMichael Tuexen 			    (void *)sctp_ifnp, (void *)sctp_ifap);
481baf3da66SRandall Stewart 			sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
48242551e99SRandall Stewart 		}
483a05620b0SMichael Tuexen 		SCTP_IPI_ADDR_WUNLOCK();
484a05620b0SMichael Tuexen 		if (new_sctp_ifnp != NULL) {
485a05620b0SMichael Tuexen 			SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
48642551e99SRandall Stewart 		}
487a05620b0SMichael Tuexen 		SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
488a05620b0SMichael Tuexen 		return (sctp_ifap);
48942551e99SRandall Stewart 	}
490a05620b0SMichael Tuexen 	KASSERT(sctp_ifnp != NULL,
491a05620b0SMichael Tuexen 	    ("sctp_add_addr_to_vrf: sctp_ifnp == NULL"));
492a05620b0SMichael Tuexen 	KASSERT(sctp_ifap == NULL,
493a05620b0SMichael Tuexen 	    ("sctp_add_addr_to_vrf: sctp_ifap (%p) != NULL", sctp_ifap));
4947f0ad227SMichael Tuexen 	sctp_ifap = new_sctp_ifap;
495132dea7dSRandall Stewart 	memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
49642551e99SRandall Stewart 	sctp_ifap->ifn_p = sctp_ifnp;
49742551e99SRandall Stewart 	atomic_add_int(&sctp_ifnp->refcount, 1);
49822a67197SRandall Stewart 	sctp_ifap->vrf_id = vrf_id;
49942551e99SRandall Stewart 	sctp_ifap->ifa = ifa;
50042551e99SRandall Stewart 	memcpy(&sctp_ifap->address, addr, addr->sa_len);
50142551e99SRandall Stewart 	sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
50242551e99SRandall Stewart 	sctp_ifap->flags = ifa_flags;
50342551e99SRandall Stewart 	/* Set scope */
5045e2c2d87SRandall Stewart 	switch (sctp_ifap->address.sa.sa_family) {
505e6194c2eSMichael Tuexen #ifdef INET
5065e2c2d87SRandall Stewart 	case AF_INET:
5075e2c2d87SRandall Stewart 		{
50842551e99SRandall Stewart 			struct sockaddr_in *sin;
50942551e99SRandall Stewart 
51024aaac8dSMichael Tuexen 			sin = &sctp_ifap->address.sin;
51142551e99SRandall Stewart 			if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
51242551e99SRandall Stewart 			    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
51342551e99SRandall Stewart 				sctp_ifap->src_is_loop = 1;
51442551e99SRandall Stewart 			}
51542551e99SRandall Stewart 			if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
51642551e99SRandall Stewart 				sctp_ifap->src_is_priv = 1;
51742551e99SRandall Stewart 			}
518ad21a364SRandall Stewart 			sctp_ifnp->num_v4++;
51902478e65SMichael Tuexen 			if (new_sctp_ifnp == NULL)
52002478e65SMichael Tuexen 				sctp_ifnp->registered_af = AF_INET;
5215e2c2d87SRandall Stewart 			break;
5225e2c2d87SRandall Stewart 		}
523e6194c2eSMichael Tuexen #endif
5245e2c2d87SRandall Stewart #ifdef INET6
5255e2c2d87SRandall Stewart 	case AF_INET6:
5265e2c2d87SRandall Stewart 		{
52742551e99SRandall Stewart 			/* ok to use deprecated addresses? */
52842551e99SRandall Stewart 			struct sockaddr_in6 *sin6;
52942551e99SRandall Stewart 
53024aaac8dSMichael Tuexen 			sin6 = &sctp_ifap->address.sin6;
53142551e99SRandall Stewart 			if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
53242551e99SRandall Stewart 			    (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
53342551e99SRandall Stewart 				sctp_ifap->src_is_loop = 1;
53442551e99SRandall Stewart 			}
53542551e99SRandall Stewart 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
53642551e99SRandall Stewart 				sctp_ifap->src_is_priv = 1;
53742551e99SRandall Stewart 			}
538ad21a364SRandall Stewart 			sctp_ifnp->num_v6++;
53902478e65SMichael Tuexen 			if (new_sctp_ifnp == NULL)
54002478e65SMichael Tuexen 				sctp_ifnp->registered_af = AF_INET6;
5415e2c2d87SRandall Stewart 			break;
5425e2c2d87SRandall Stewart 		}
5435e2c2d87SRandall Stewart #endif
5445e2c2d87SRandall Stewart 	default:
5455e2c2d87SRandall Stewart 		break;
54642551e99SRandall Stewart 	}
5476a27c376SRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
5486a27c376SRandall Stewart 
54942551e99SRandall Stewart 	if ((sctp_ifap->src_is_priv == 0) &&
55042551e99SRandall Stewart 	    (sctp_ifap->src_is_loop == 0)) {
55142551e99SRandall Stewart 		sctp_ifap->src_is_glob = 1;
55242551e99SRandall Stewart 	}
55317205eccSRandall Stewart 	hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
55417205eccSRandall Stewart 	LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
55542551e99SRandall Stewart 	sctp_ifap->refcount = 1;
55642551e99SRandall Stewart 	LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
55742551e99SRandall Stewart 	sctp_ifnp->ifa_count++;
55842551e99SRandall Stewart 	vrf->total_ifa_count++;
559b3f1ea41SRandall Stewart 	atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
560c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_WUNLOCK();
5617f0ad227SMichael Tuexen 	if (new_sctp_ifnp != NULL) {
5627f0ad227SMichael Tuexen 		SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
5637f0ad227SMichael Tuexen 	}
5647f0ad227SMichael Tuexen 
565d06c82f1SRandall Stewart 	if (dynamic_add) {
566d06c82f1SRandall Stewart 		/*
567d06c82f1SRandall Stewart 		 * Bump up the refcount so that when the timer completes it
568d06c82f1SRandall Stewart 		 * will drop back down.
569d06c82f1SRandall Stewart 		 */
570d06c82f1SRandall Stewart 		struct sctp_laddr *wi;
571d06c82f1SRandall Stewart 
572d06c82f1SRandall Stewart 		atomic_add_int(&sctp_ifap->refcount, 1);
573b3f1ea41SRandall Stewart 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
574d06c82f1SRandall Stewart 		if (wi == NULL) {
575d06c82f1SRandall Stewart 			/*
576d06c82f1SRandall Stewart 			 * Gak, what can we do? We have lost an address
577d06c82f1SRandall Stewart 			 * change can you say HOSED?
578d06c82f1SRandall Stewart 			 */
579baf3da66SRandall Stewart 			SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
580d06c82f1SRandall Stewart 			/* Opps, must decrement the count */
581523913c9SMichael Tuexen 			sctp_del_addr_from_vrf(vrf_id, addr, ifn, ifn_index);
582d06c82f1SRandall Stewart 			return (NULL);
583d06c82f1SRandall Stewart 		}
584d06c82f1SRandall Stewart 		SCTP_INCR_LADDR_COUNT();
5855ba7f91fSMichael Tuexen 		memset(wi, 0, sizeof(*wi));
586d61a0ae0SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
587d06c82f1SRandall Stewart 		wi->ifa = sctp_ifap;
588d06c82f1SRandall Stewart 		wi->action = SCTP_ADD_IP_ADDRESS;
589f7517433SRandall Stewart 
590f7517433SRandall Stewart 		SCTP_WQ_ADDR_LOCK();
591b3f1ea41SRandall Stewart 		LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
592d06c82f1SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
593d06c82f1SRandall Stewart 		    (struct sctp_inpcb *)NULL,
594d06c82f1SRandall Stewart 		    (struct sctp_tcb *)NULL,
595d06c82f1SRandall Stewart 		    (struct sctp_nets *)NULL);
5962c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
59717205eccSRandall Stewart 	} else {
59817205eccSRandall Stewart 		/* it's ready for use */
59917205eccSRandall Stewart 		sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
600d06c82f1SRandall Stewart 	}
60142551e99SRandall Stewart 	return (sctp_ifap);
60242551e99SRandall Stewart }
60342551e99SRandall Stewart 
604d06c82f1SRandall Stewart void
sctp_del_addr_from_vrf(uint32_t vrf_id,struct sockaddr * addr,void * ifn,uint32_t ifn_index)60517205eccSRandall Stewart sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
606523913c9SMichael Tuexen     void *ifn, uint32_t ifn_index)
60742551e99SRandall Stewart {
60842551e99SRandall Stewart 	struct sctp_vrf *vrf;
609d08713dcSMichael Tuexen 	struct sctp_ifa *sctp_ifap;
61042551e99SRandall Stewart 
611c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_WLOCK();
61217205eccSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
61342551e99SRandall Stewart 	if (vrf == NULL) {
614baf3da66SRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
615d08713dcSMichael Tuexen 		SCTP_IPI_ADDR_WUNLOCK();
616d08713dcSMichael Tuexen 		return;
61742551e99SRandall Stewart 	}
6180053ed28SMichael Tuexen 
619baf3da66SRandall Stewart #ifdef SCTP_DEBUG
620baf3da66SRandall Stewart 	SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
621baf3da66SRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
622baf3da66SRandall Stewart #endif
623851b7298SRandall Stewart 	sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
624d08713dcSMichael Tuexen 	if (sctp_ifap != NULL) {
625851b7298SRandall Stewart 		/* Validate the delete */
626851b7298SRandall Stewart 		if (sctp_ifap->ifn_p) {
627523913c9SMichael Tuexen 			if (ifn_index != sctp_ifap->ifn_p->ifn_index ||
628523913c9SMichael Tuexen 			    ifn != sctp_ifap->ifn_p->ifn_p) {
629518a1163SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d (%p) ifname:%s - ignoring delete\n",
630523913c9SMichael Tuexen 				    sctp_ifap->ifn_p->ifn_index,
631523913c9SMichael Tuexen 				    sctp_ifap->ifn_p->ifn_p,
632523913c9SMichael Tuexen 				    sctp_ifap->ifn_p->ifn_name);
633c99efcf6SRandall Stewart 				SCTP_IPI_ADDR_WUNLOCK();
634851b7298SRandall Stewart 				return;
635851b7298SRandall Stewart 			}
636851b7298SRandall Stewart 		}
637dd294dceSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
63842551e99SRandall Stewart 		sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
6399ba5b6b7SMichael Tuexen 		/*
6409ba5b6b7SMichael Tuexen 		 * We don't set the flag. This means that the structure will
6419ba5b6b7SMichael Tuexen 		 * hang around in EP's that have bound specific to it until
6429ba5b6b7SMichael Tuexen 		 * they close. This gives us TCP like behavior if someone
6439ba5b6b7SMichael Tuexen 		 * removes an address (or for that matter adds it right
6449ba5b6b7SMichael Tuexen 		 * back).
6459ba5b6b7SMichael Tuexen 		 */
6469ba5b6b7SMichael Tuexen 		/* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
64742551e99SRandall Stewart 		vrf->total_ifa_count--;
6486a27c376SRandall Stewart 		LIST_REMOVE(sctp_ifap, next_bucket);
649baf3da66SRandall Stewart 		sctp_remove_ifa_from_ifn(sctp_ifap);
65017205eccSRandall Stewart 	}
65117205eccSRandall Stewart #ifdef SCTP_DEBUG
65217205eccSRandall Stewart 	else {
653baf3da66SRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
65442551e99SRandall Stewart 		    ifn_index);
655ce20b48aSMichael Tuexen 		SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
65642551e99SRandall Stewart 	}
65717205eccSRandall Stewart #endif
658ad81507eSRandall Stewart 
659c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_WUNLOCK();
660d08713dcSMichael Tuexen 	if (sctp_ifap != NULL) {
661d06c82f1SRandall Stewart 		struct sctp_laddr *wi;
662d06c82f1SRandall Stewart 
663b3f1ea41SRandall Stewart 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
664d06c82f1SRandall Stewart 		if (wi == NULL) {
665d06c82f1SRandall Stewart 			/*
666d06c82f1SRandall Stewart 			 * Gak, what can we do? We have lost an address
667d06c82f1SRandall Stewart 			 * change can you say HOSED?
668d06c82f1SRandall Stewart 			 */
669baf3da66SRandall Stewart 			SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
670d06c82f1SRandall Stewart 
671c99efcf6SRandall Stewart 			/* Oops, must decrement the count */
672d06c82f1SRandall Stewart 			sctp_free_ifa(sctp_ifap);
673d06c82f1SRandall Stewart 			return;
674d06c82f1SRandall Stewart 		}
675d06c82f1SRandall Stewart 		SCTP_INCR_LADDR_COUNT();
6765ba7f91fSMichael Tuexen 		memset(wi, 0, sizeof(*wi));
677d61a0ae0SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
678d06c82f1SRandall Stewart 		wi->ifa = sctp_ifap;
679d06c82f1SRandall Stewart 		wi->action = SCTP_DEL_IP_ADDRESS;
680f7517433SRandall Stewart 		SCTP_WQ_ADDR_LOCK();
681d06c82f1SRandall Stewart 		/*
682d06c82f1SRandall Stewart 		 * Should this really be a tailq? As it is we will process
683d06c82f1SRandall Stewart 		 * the newest first :-0
684d06c82f1SRandall Stewart 		 */
685b3f1ea41SRandall Stewart 		LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
686d06c82f1SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
687d06c82f1SRandall Stewart 		    (struct sctp_inpcb *)NULL,
688d06c82f1SRandall Stewart 		    (struct sctp_tcb *)NULL,
689d06c82f1SRandall Stewart 		    (struct sctp_nets *)NULL);
6902c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
691d06c82f1SRandall Stewart 	}
692d06c82f1SRandall Stewart 	return;
69342551e99SRandall Stewart }
694f8829a4aSRandall Stewart 
695749c49acSMichael Tuexen static int
sctp_does_stcb_own_this_addr(struct sctp_tcb * stcb,struct sockaddr * to)696749c49acSMichael Tuexen sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
697749c49acSMichael Tuexen {
698b54ddf22SMichael Tuexen 	int loopback_scope;
699b54ddf22SMichael Tuexen #if defined(INET)
700b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
701b54ddf22SMichael Tuexen #endif
702b54ddf22SMichael Tuexen #if defined(INET6)
703b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
704b54ddf22SMichael Tuexen #endif
705749c49acSMichael Tuexen 	struct sctp_vrf *vrf;
706749c49acSMichael Tuexen 	struct sctp_ifn *sctp_ifn;
707749c49acSMichael Tuexen 	struct sctp_ifa *sctp_ifa;
708749c49acSMichael Tuexen 
709a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
710b54ddf22SMichael Tuexen #if defined(INET)
711a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
712b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
713b54ddf22SMichael Tuexen #endif
714b54ddf22SMichael Tuexen #if defined(INET6)
715a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
716a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
717a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
718b54ddf22SMichael Tuexen #endif
719749c49acSMichael Tuexen 
720749c49acSMichael Tuexen 	SCTP_IPI_ADDR_RLOCK();
721749c49acSMichael Tuexen 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
722749c49acSMichael Tuexen 	if (vrf == NULL) {
723749c49acSMichael Tuexen 		/* no vrf, no addresses */
724749c49acSMichael Tuexen 		SCTP_IPI_ADDR_RUNLOCK();
725749c49acSMichael Tuexen 		return (0);
726749c49acSMichael Tuexen 	}
7270053ed28SMichael Tuexen 
728749c49acSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
729749c49acSMichael Tuexen 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
730749c49acSMichael Tuexen 			if ((loopback_scope == 0) &&
731749c49acSMichael Tuexen 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
732749c49acSMichael Tuexen 				continue;
733749c49acSMichael Tuexen 			}
734749c49acSMichael Tuexen 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
735ca85e948SMichael Tuexen 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
736ca85e948SMichael Tuexen 				    (!sctp_is_addr_pending(stcb, sctp_ifa))) {
737ca85e948SMichael Tuexen 					/*
738ca85e948SMichael Tuexen 					 * We allow pending addresses, where
739ca85e948SMichael Tuexen 					 * we have sent an asconf-add to be
740ca85e948SMichael Tuexen 					 * considered valid.
741ca85e948SMichael Tuexen 					 */
742749c49acSMichael Tuexen 					continue;
743ca85e948SMichael Tuexen 				}
744dcb3fc4cSMichael Tuexen 				if (sctp_ifa->address.sa.sa_family != to->sa_family) {
745dcb3fc4cSMichael Tuexen 					continue;
746dcb3fc4cSMichael Tuexen 				}
747749c49acSMichael Tuexen 				switch (sctp_ifa->address.sa.sa_family) {
748749c49acSMichael Tuexen #ifdef INET
749749c49acSMichael Tuexen 				case AF_INET:
750749c49acSMichael Tuexen 					if (ipv4_addr_legal) {
751749c49acSMichael Tuexen 						struct sockaddr_in *sin,
752749c49acSMichael Tuexen 						           *rsin;
753749c49acSMichael Tuexen 
754749c49acSMichael Tuexen 						sin = &sctp_ifa->address.sin;
755749c49acSMichael Tuexen 						rsin = (struct sockaddr_in *)to;
756749c49acSMichael Tuexen 						if ((ipv4_local_scope == 0) &&
757749c49acSMichael Tuexen 						    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
758749c49acSMichael Tuexen 							continue;
759749c49acSMichael Tuexen 						}
7606ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
7616ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
7626ba22f19SMichael Tuexen 							continue;
7636ba22f19SMichael Tuexen 						}
764749c49acSMichael Tuexen 						if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
765749c49acSMichael Tuexen 							SCTP_IPI_ADDR_RUNLOCK();
766749c49acSMichael Tuexen 							return (1);
767749c49acSMichael Tuexen 						}
768749c49acSMichael Tuexen 					}
769749c49acSMichael Tuexen 					break;
770749c49acSMichael Tuexen #endif
771749c49acSMichael Tuexen #ifdef INET6
772749c49acSMichael Tuexen 				case AF_INET6:
773749c49acSMichael Tuexen 					if (ipv6_addr_legal) {
774749c49acSMichael Tuexen 						struct sockaddr_in6 *sin6,
775749c49acSMichael Tuexen 						            *rsin6;
776749c49acSMichael Tuexen 
777749c49acSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
778749c49acSMichael Tuexen 						rsin6 = (struct sockaddr_in6 *)to;
7796ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
7806ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
7816ba22f19SMichael Tuexen 							continue;
7826ba22f19SMichael Tuexen 						}
783749c49acSMichael Tuexen 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
784749c49acSMichael Tuexen 							if (local_scope == 0)
785749c49acSMichael Tuexen 								continue;
786749c49acSMichael Tuexen 							if (sin6->sin6_scope_id == 0) {
787749c49acSMichael Tuexen 								if (sa6_recoverscope(sin6) != 0)
788749c49acSMichael Tuexen 									continue;
789749c49acSMichael Tuexen 							}
790749c49acSMichael Tuexen 						}
791749c49acSMichael Tuexen 						if ((site_scope == 0) &&
792749c49acSMichael Tuexen 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
793749c49acSMichael Tuexen 							continue;
794749c49acSMichael Tuexen 						}
795749c49acSMichael Tuexen 						if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
796749c49acSMichael Tuexen 							SCTP_IPI_ADDR_RUNLOCK();
797749c49acSMichael Tuexen 							return (1);
798749c49acSMichael Tuexen 						}
799749c49acSMichael Tuexen 					}
800749c49acSMichael Tuexen 					break;
801749c49acSMichael Tuexen #endif
802749c49acSMichael Tuexen 				default:
803749c49acSMichael Tuexen 					/* TSNH */
804749c49acSMichael Tuexen 					break;
805749c49acSMichael Tuexen 				}
806749c49acSMichael Tuexen 			}
807749c49acSMichael Tuexen 		}
808749c49acSMichael Tuexen 	} else {
809749c49acSMichael Tuexen 		struct sctp_laddr *laddr;
810749c49acSMichael Tuexen 
811749c49acSMichael Tuexen 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
812f0d44a49SMichael Tuexen 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
813f0d44a49SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
814f0d44a49SMichael Tuexen 				continue;
815f0d44a49SMichael Tuexen 			}
816ca85e948SMichael Tuexen 			if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
817ca85e948SMichael Tuexen 			    (!sctp_is_addr_pending(stcb, laddr->ifa))) {
818ca85e948SMichael Tuexen 				/*
819ca85e948SMichael Tuexen 				 * We allow pending addresses, where we have
820ca85e948SMichael Tuexen 				 * sent an asconf-add to be considered
821ca85e948SMichael Tuexen 				 * valid.
822ca85e948SMichael Tuexen 				 */
823749c49acSMichael Tuexen 				continue;
824749c49acSMichael Tuexen 			}
825749c49acSMichael Tuexen 			if (laddr->ifa->address.sa.sa_family != to->sa_family) {
826749c49acSMichael Tuexen 				continue;
827749c49acSMichael Tuexen 			}
828749c49acSMichael Tuexen 			switch (to->sa_family) {
829749c49acSMichael Tuexen #ifdef INET
830749c49acSMichael Tuexen 			case AF_INET:
831749c49acSMichael Tuexen 				{
832749c49acSMichael Tuexen 					struct sockaddr_in *sin, *rsin;
833749c49acSMichael Tuexen 
83424aaac8dSMichael Tuexen 					sin = &laddr->ifa->address.sin;
835749c49acSMichael Tuexen 					rsin = (struct sockaddr_in *)to;
836749c49acSMichael Tuexen 					if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
837749c49acSMichael Tuexen 						SCTP_IPI_ADDR_RUNLOCK();
838749c49acSMichael Tuexen 						return (1);
839749c49acSMichael Tuexen 					}
840749c49acSMichael Tuexen 					break;
841749c49acSMichael Tuexen 				}
842749c49acSMichael Tuexen #endif
843749c49acSMichael Tuexen #ifdef INET6
844749c49acSMichael Tuexen 			case AF_INET6:
845749c49acSMichael Tuexen 				{
846749c49acSMichael Tuexen 					struct sockaddr_in6 *sin6, *rsin6;
847749c49acSMichael Tuexen 
84824aaac8dSMichael Tuexen 					sin6 = &laddr->ifa->address.sin6;
849749c49acSMichael Tuexen 					rsin6 = (struct sockaddr_in6 *)to;
850749c49acSMichael Tuexen 					if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
851749c49acSMichael Tuexen 						SCTP_IPI_ADDR_RUNLOCK();
852749c49acSMichael Tuexen 						return (1);
853749c49acSMichael Tuexen 					}
854749c49acSMichael Tuexen 					break;
855749c49acSMichael Tuexen 				}
856749c49acSMichael Tuexen 
857749c49acSMichael Tuexen #endif
858749c49acSMichael Tuexen 			default:
859749c49acSMichael Tuexen 				/* TSNH */
860749c49acSMichael Tuexen 				break;
861749c49acSMichael Tuexen 			}
862749c49acSMichael Tuexen 		}
863749c49acSMichael Tuexen 	}
864749c49acSMichael Tuexen 	SCTP_IPI_ADDR_RUNLOCK();
865749c49acSMichael Tuexen 	return (0);
866749c49acSMichael Tuexen }
867749c49acSMichael Tuexen 
868f0d44a49SMichael Tuexen static struct sctp_tcb *
sctp_tcb_special_locate(struct sctp_inpcb ** inp_p,struct sockaddr * from,struct sockaddr * to,struct sctp_nets ** netp,uint32_t vrf_id)869f0d44a49SMichael Tuexen sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
870f0d44a49SMichael Tuexen     struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
871f0d44a49SMichael Tuexen {
872f0d44a49SMichael Tuexen 	/**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
873f0d44a49SMichael Tuexen 	/*
874f0d44a49SMichael Tuexen 	 * If we support the TCP model, then we must now dig through to see
875f0d44a49SMichael Tuexen 	 * if we can find our endpoint in the list of tcp ep's.
876f0d44a49SMichael Tuexen 	 */
877f0d44a49SMichael Tuexen 	uint16_t lport, rport;
878f0d44a49SMichael Tuexen 	struct sctppcbhead *ephead;
879f0d44a49SMichael Tuexen 	struct sctp_inpcb *inp;
880f0d44a49SMichael Tuexen 	struct sctp_laddr *laddr;
881f0d44a49SMichael Tuexen 	struct sctp_tcb *stcb;
882f0d44a49SMichael Tuexen 	struct sctp_nets *net;
883f0d44a49SMichael Tuexen 
884f0d44a49SMichael Tuexen 	if ((to == NULL) || (from == NULL)) {
885f0d44a49SMichael Tuexen 		return (NULL);
886f0d44a49SMichael Tuexen 	}
8870053ed28SMichael Tuexen 
888f0d44a49SMichael Tuexen 	switch (to->sa_family) {
889f0d44a49SMichael Tuexen #ifdef INET
890f0d44a49SMichael Tuexen 	case AF_INET:
891f0d44a49SMichael Tuexen 		if (from->sa_family == AF_INET) {
892f0d44a49SMichael Tuexen 			lport = ((struct sockaddr_in *)to)->sin_port;
893f0d44a49SMichael Tuexen 			rport = ((struct sockaddr_in *)from)->sin_port;
894f0d44a49SMichael Tuexen 		} else {
895f0d44a49SMichael Tuexen 			return (NULL);
896f0d44a49SMichael Tuexen 		}
897f0d44a49SMichael Tuexen 		break;
898f0d44a49SMichael Tuexen #endif
899f0d44a49SMichael Tuexen #ifdef INET6
900f0d44a49SMichael Tuexen 	case AF_INET6:
901f0d44a49SMichael Tuexen 		if (from->sa_family == AF_INET6) {
902f0d44a49SMichael Tuexen 			lport = ((struct sockaddr_in6 *)to)->sin6_port;
903f0d44a49SMichael Tuexen 			rport = ((struct sockaddr_in6 *)from)->sin6_port;
904f0d44a49SMichael Tuexen 		} else {
905f0d44a49SMichael Tuexen 			return (NULL);
906f0d44a49SMichael Tuexen 		}
907f0d44a49SMichael Tuexen 		break;
908f0d44a49SMichael Tuexen #endif
909f0d44a49SMichael Tuexen 	default:
910f0d44a49SMichael Tuexen 		return (NULL);
911f0d44a49SMichael Tuexen 	}
912f0d44a49SMichael Tuexen 	ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
913f0d44a49SMichael Tuexen 	/*
914f0d44a49SMichael Tuexen 	 * Ok now for each of the guys in this bucket we must look and see:
915f0d44a49SMichael Tuexen 	 * - Does the remote port match. - Does there single association's
916f0d44a49SMichael Tuexen 	 * addresses match this address (to). If so we update p_ep to point
917f0d44a49SMichael Tuexen 	 * to this ep and return the tcb from it.
918f0d44a49SMichael Tuexen 	 */
919f0d44a49SMichael Tuexen 	LIST_FOREACH(inp, ephead, sctp_hash) {
920f0d44a49SMichael Tuexen 		SCTP_INP_RLOCK(inp);
921f0d44a49SMichael Tuexen 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
922f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
923f0d44a49SMichael Tuexen 			continue;
924f0d44a49SMichael Tuexen 		}
925f0d44a49SMichael Tuexen 		if (lport != inp->sctp_lport) {
926f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
927f0d44a49SMichael Tuexen 			continue;
928f0d44a49SMichael Tuexen 		}
9296ba22f19SMichael Tuexen 		switch (to->sa_family) {
9306ba22f19SMichael Tuexen #ifdef INET
9316ba22f19SMichael Tuexen 		case AF_INET:
9326ba22f19SMichael Tuexen 			{
9336ba22f19SMichael Tuexen 				struct sockaddr_in *sin;
9346ba22f19SMichael Tuexen 
9356ba22f19SMichael Tuexen 				sin = (struct sockaddr_in *)to;
9366ba22f19SMichael Tuexen 				if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
9376ba22f19SMichael Tuexen 				    &sin->sin_addr) != 0) {
9386ba22f19SMichael Tuexen 					SCTP_INP_RUNLOCK(inp);
9396ba22f19SMichael Tuexen 					continue;
9406ba22f19SMichael Tuexen 				}
9416ba22f19SMichael Tuexen 				break;
9426ba22f19SMichael Tuexen 			}
9436ba22f19SMichael Tuexen #endif
9446ba22f19SMichael Tuexen #ifdef INET6
9456ba22f19SMichael Tuexen 		case AF_INET6:
9466ba22f19SMichael Tuexen 			{
9476ba22f19SMichael Tuexen 				struct sockaddr_in6 *sin6;
9486ba22f19SMichael Tuexen 
9496ba22f19SMichael Tuexen 				sin6 = (struct sockaddr_in6 *)to;
9506ba22f19SMichael Tuexen 				if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
9516ba22f19SMichael Tuexen 				    &sin6->sin6_addr) != 0) {
9526ba22f19SMichael Tuexen 					SCTP_INP_RUNLOCK(inp);
9536ba22f19SMichael Tuexen 					continue;
9546ba22f19SMichael Tuexen 				}
9556ba22f19SMichael Tuexen 				break;
9566ba22f19SMichael Tuexen 			}
9576ba22f19SMichael Tuexen #endif
9586ba22f19SMichael Tuexen 		default:
9596ba22f19SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
9606ba22f19SMichael Tuexen 			continue;
9616ba22f19SMichael Tuexen 		}
962f0d44a49SMichael Tuexen 		if (inp->def_vrf_id != vrf_id) {
963f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
964f0d44a49SMichael Tuexen 			continue;
965f0d44a49SMichael Tuexen 		}
966f0d44a49SMichael Tuexen 		/* check to see if the ep has one of the addresses */
967f0d44a49SMichael Tuexen 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
968f0d44a49SMichael Tuexen 			/* We are NOT bound all, so look further */
969f0d44a49SMichael Tuexen 			int match = 0;
970f0d44a49SMichael Tuexen 
971f0d44a49SMichael Tuexen 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
972f0d44a49SMichael Tuexen 				if (laddr->ifa == NULL) {
9736e9c45e0SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
974f0d44a49SMichael Tuexen 					continue;
975f0d44a49SMichael Tuexen 				}
976f0d44a49SMichael Tuexen 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
977f0d44a49SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
978f0d44a49SMichael Tuexen 					continue;
979f0d44a49SMichael Tuexen 				}
980f0d44a49SMichael Tuexen 				if (laddr->ifa->address.sa.sa_family ==
981f0d44a49SMichael Tuexen 				    to->sa_family) {
982f0d44a49SMichael Tuexen 					/* see if it matches */
983f0d44a49SMichael Tuexen #ifdef INET
984f0d44a49SMichael Tuexen 					if (from->sa_family == AF_INET) {
985f0d44a49SMichael Tuexen 						struct sockaddr_in *intf_addr,
986f0d44a49SMichael Tuexen 						           *sin;
987f0d44a49SMichael Tuexen 
988f0d44a49SMichael Tuexen 						intf_addr = &laddr->ifa->address.sin;
989f0d44a49SMichael Tuexen 						sin = (struct sockaddr_in *)to;
990f0d44a49SMichael Tuexen 						if (sin->sin_addr.s_addr ==
991f0d44a49SMichael Tuexen 						    intf_addr->sin_addr.s_addr) {
992f0d44a49SMichael Tuexen 							match = 1;
993f0d44a49SMichael Tuexen 							break;
994f0d44a49SMichael Tuexen 						}
995f0d44a49SMichael Tuexen 					}
996f0d44a49SMichael Tuexen #endif
997f0d44a49SMichael Tuexen #ifdef INET6
998f0d44a49SMichael Tuexen 					if (from->sa_family == AF_INET6) {
999f0d44a49SMichael Tuexen 						struct sockaddr_in6 *intf_addr6;
1000f0d44a49SMichael Tuexen 						struct sockaddr_in6 *sin6;
1001f0d44a49SMichael Tuexen 
1002f0d44a49SMichael Tuexen 						sin6 = (struct sockaddr_in6 *)
1003f0d44a49SMichael Tuexen 						    to;
1004f0d44a49SMichael Tuexen 						intf_addr6 = &laddr->ifa->address.sin6;
1005f0d44a49SMichael Tuexen 
1006f0d44a49SMichael Tuexen 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
1007f0d44a49SMichael Tuexen 						    intf_addr6)) {
1008f0d44a49SMichael Tuexen 							match = 1;
1009f0d44a49SMichael Tuexen 							break;
1010f0d44a49SMichael Tuexen 						}
1011f0d44a49SMichael Tuexen 					}
1012f0d44a49SMichael Tuexen #endif
1013f0d44a49SMichael Tuexen 				}
1014f0d44a49SMichael Tuexen 			}
1015f0d44a49SMichael Tuexen 			if (match == 0) {
1016f0d44a49SMichael Tuexen 				/* This endpoint does not have this address */
1017f0d44a49SMichael Tuexen 				SCTP_INP_RUNLOCK(inp);
1018f0d44a49SMichael Tuexen 				continue;
1019f0d44a49SMichael Tuexen 			}
1020f0d44a49SMichael Tuexen 		}
1021f0d44a49SMichael Tuexen 		/*
1022f0d44a49SMichael Tuexen 		 * Ok if we hit here the ep has the address, does it hold
1023f0d44a49SMichael Tuexen 		 * the tcb?
1024f0d44a49SMichael Tuexen 		 */
1025f0d44a49SMichael Tuexen 		/* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
1026f0d44a49SMichael Tuexen 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1027f0d44a49SMichael Tuexen 		if (stcb == NULL) {
1028f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
1029f0d44a49SMichael Tuexen 			continue;
1030f0d44a49SMichael Tuexen 		}
1031f0d44a49SMichael Tuexen 		SCTP_TCB_LOCK(stcb);
1032f0d44a49SMichael Tuexen 		if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1033f0d44a49SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1034f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
1035f0d44a49SMichael Tuexen 			continue;
1036f0d44a49SMichael Tuexen 		}
1037f0d44a49SMichael Tuexen 		if (stcb->rport != rport) {
1038f0d44a49SMichael Tuexen 			/* remote port does not match. */
1039f0d44a49SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1040f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
1041f0d44a49SMichael Tuexen 			continue;
1042f0d44a49SMichael Tuexen 		}
1043f0d44a49SMichael Tuexen 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1044f0d44a49SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1045f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
1046f0d44a49SMichael Tuexen 			continue;
1047f0d44a49SMichael Tuexen 		}
1048f0d44a49SMichael Tuexen 		if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1049f0d44a49SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1050f0d44a49SMichael Tuexen 			SCTP_INP_RUNLOCK(inp);
1051f0d44a49SMichael Tuexen 			continue;
1052f0d44a49SMichael Tuexen 		}
1053f0d44a49SMichael Tuexen 		/* Does this TCB have a matching address? */
1054f0d44a49SMichael Tuexen 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1055f0d44a49SMichael Tuexen 			if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1056f0d44a49SMichael Tuexen 				/* not the same family, can't be a match */
1057f0d44a49SMichael Tuexen 				continue;
1058f0d44a49SMichael Tuexen 			}
1059f0d44a49SMichael Tuexen 			switch (from->sa_family) {
1060f0d44a49SMichael Tuexen #ifdef INET
1061f0d44a49SMichael Tuexen 			case AF_INET:
1062f0d44a49SMichael Tuexen 				{
1063f0d44a49SMichael Tuexen 					struct sockaddr_in *sin, *rsin;
1064f0d44a49SMichael Tuexen 
1065f0d44a49SMichael Tuexen 					sin = (struct sockaddr_in *)&net->ro._l_addr;
1066f0d44a49SMichael Tuexen 					rsin = (struct sockaddr_in *)from;
1067f0d44a49SMichael Tuexen 					if (sin->sin_addr.s_addr ==
1068f0d44a49SMichael Tuexen 					    rsin->sin_addr.s_addr) {
1069f0d44a49SMichael Tuexen 						/* found it */
1070f0d44a49SMichael Tuexen 						if (netp != NULL) {
1071f0d44a49SMichael Tuexen 							*netp = net;
1072f0d44a49SMichael Tuexen 						}
1073b7b84c0eSMichael Tuexen 						/*
1074b7b84c0eSMichael Tuexen 						 * Update the endpoint
1075b7b84c0eSMichael Tuexen 						 * pointer
1076b7b84c0eSMichael Tuexen 						 */
1077f0d44a49SMichael Tuexen 						*inp_p = inp;
1078f0d44a49SMichael Tuexen 						SCTP_INP_RUNLOCK(inp);
1079f0d44a49SMichael Tuexen 						return (stcb);
1080f0d44a49SMichael Tuexen 					}
1081f0d44a49SMichael Tuexen 					break;
1082f0d44a49SMichael Tuexen 				}
1083f0d44a49SMichael Tuexen #endif
1084f0d44a49SMichael Tuexen #ifdef INET6
1085f0d44a49SMichael Tuexen 			case AF_INET6:
1086f0d44a49SMichael Tuexen 				{
1087f0d44a49SMichael Tuexen 					struct sockaddr_in6 *sin6, *rsin6;
1088f0d44a49SMichael Tuexen 
1089f0d44a49SMichael Tuexen 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1090f0d44a49SMichael Tuexen 					rsin6 = (struct sockaddr_in6 *)from;
1091f0d44a49SMichael Tuexen 					if (SCTP6_ARE_ADDR_EQUAL(sin6,
1092f0d44a49SMichael Tuexen 					    rsin6)) {
1093f0d44a49SMichael Tuexen 						/* found it */
1094f0d44a49SMichael Tuexen 						if (netp != NULL) {
1095f0d44a49SMichael Tuexen 							*netp = net;
1096f0d44a49SMichael Tuexen 						}
1097b7b84c0eSMichael Tuexen 						/*
1098b7b84c0eSMichael Tuexen 						 * Update the endpoint
1099b7b84c0eSMichael Tuexen 						 * pointer
1100b7b84c0eSMichael Tuexen 						 */
1101f0d44a49SMichael Tuexen 						*inp_p = inp;
1102f0d44a49SMichael Tuexen 						SCTP_INP_RUNLOCK(inp);
1103f0d44a49SMichael Tuexen 						return (stcb);
1104f0d44a49SMichael Tuexen 					}
1105f0d44a49SMichael Tuexen 					break;
1106f0d44a49SMichael Tuexen 				}
1107f0d44a49SMichael Tuexen #endif
1108f0d44a49SMichael Tuexen 			default:
1109f0d44a49SMichael Tuexen 				/* TSNH */
1110f0d44a49SMichael Tuexen 				break;
1111f0d44a49SMichael Tuexen 			}
1112f0d44a49SMichael Tuexen 		}
1113f0d44a49SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1114f0d44a49SMichael Tuexen 		SCTP_INP_RUNLOCK(inp);
1115f0d44a49SMichael Tuexen 	}
1116f0d44a49SMichael Tuexen 	return (NULL);
1117f0d44a49SMichael Tuexen }
1118f0d44a49SMichael Tuexen 
1119f8829a4aSRandall Stewart /*
1120f8829a4aSRandall Stewart  * rules for use
1121f8829a4aSRandall Stewart  *
1122f8829a4aSRandall Stewart  * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1123f8829a4aSRandall Stewart  * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1124f8829a4aSRandall Stewart  * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1125f8829a4aSRandall Stewart  * NULL.
1126f8829a4aSRandall Stewart  */
1127f8829a4aSRandall Stewart 
1128f8829a4aSRandall Stewart struct sctp_tcb *
sctp_findassociation_ep_addr(struct sctp_inpcb ** inp_p,struct sockaddr * remote,struct sctp_nets ** netp,struct sockaddr * local,struct sctp_tcb * locked_tcb)1129f8829a4aSRandall Stewart sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1130f8829a4aSRandall Stewart     struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1131f8829a4aSRandall Stewart {
1132f8829a4aSRandall Stewart 	struct sctpasochead *head;
1133f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1134bff64a4dSRandall Stewart 	struct sctp_tcb *stcb = NULL;
1135f8829a4aSRandall Stewart 	struct sctp_nets *net;
1136f8829a4aSRandall Stewart 	uint16_t rport;
1137f8829a4aSRandall Stewart 
1138f8829a4aSRandall Stewart 	inp = *inp_p;
1139b5e0cd79SMichael Tuexen 	switch (remote->sa_family) {
1140b5e0cd79SMichael Tuexen #ifdef INET
1141b5e0cd79SMichael Tuexen 	case AF_INET:
1142f8829a4aSRandall Stewart 		rport = (((struct sockaddr_in *)remote)->sin_port);
1143b5e0cd79SMichael Tuexen 		break;
1144b5e0cd79SMichael Tuexen #endif
1145b5e0cd79SMichael Tuexen #ifdef INET6
1146b5e0cd79SMichael Tuexen 	case AF_INET6:
1147f8829a4aSRandall Stewart 		rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1148b5e0cd79SMichael Tuexen 		break;
1149b5e0cd79SMichael Tuexen #endif
1150b5e0cd79SMichael Tuexen 	default:
1151f8829a4aSRandall Stewart 		return (NULL);
1152f8829a4aSRandall Stewart 	}
1153f8829a4aSRandall Stewart 	if (locked_tcb) {
1154f8829a4aSRandall Stewart 		/*
1155f8829a4aSRandall Stewart 		 * UN-lock so we can do proper locking here this occurs when
1156f8829a4aSRandall Stewart 		 * called from load_addresses_from_init.
1157f8829a4aSRandall Stewart 		 */
1158c4739e2fSRandall Stewart 		atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1159f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(locked_tcb);
1160f8829a4aSRandall Stewart 	}
1161f8829a4aSRandall Stewart 	SCTP_INP_INFO_RLOCK();
1162e3976bb8SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1163e3976bb8SMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1164bff64a4dSRandall Stewart 		/*-
1165f8829a4aSRandall Stewart 		 * Now either this guy is our listener or it's the
1166f8829a4aSRandall Stewart 		 * connector. If it is the one that issued the connect, then
1167f8829a4aSRandall Stewart 		 * it's only chance is to be the first TCB in the list. If
1168f8829a4aSRandall Stewart 		 * it is the acceptor, then do the special_lookup to hash
1169f8829a4aSRandall Stewart 		 * and find the real inp.
1170f8829a4aSRandall Stewart 		 */
11715d08768aSMichael Tuexen 		if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
1172f8829a4aSRandall Stewart 			/* to is peer addr, from is my addr */
1173f8829a4aSRandall Stewart 			stcb = sctp_tcb_special_locate(inp_p, remote, local,
1174bff64a4dSRandall Stewart 			    netp, inp->def_vrf_id);
1175f8829a4aSRandall Stewart 			if ((stcb != NULL) && (locked_tcb == NULL)) {
1176f8829a4aSRandall Stewart 				/* we have a locked tcb, lower refcount */
1177f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1178f8829a4aSRandall Stewart 			}
1179f8829a4aSRandall Stewart 			if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1180f8829a4aSRandall Stewart 				SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1181f8829a4aSRandall Stewart 				SCTP_TCB_LOCK(locked_tcb);
1182c4739e2fSRandall Stewart 				atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1183f8829a4aSRandall Stewart 				SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1184f8829a4aSRandall Stewart 			}
1185f8829a4aSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1186f8829a4aSRandall Stewart 			return (stcb);
1187f8829a4aSRandall Stewart 		} else {
1188f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1189f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1190f8829a4aSRandall Stewart 				goto null_return;
1191f8829a4aSRandall Stewart 			}
1192f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1193f8829a4aSRandall Stewart 			if (stcb == NULL) {
1194f8829a4aSRandall Stewart 				goto null_return;
1195f8829a4aSRandall Stewart 			}
1196f8829a4aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1197fb8fb8f8SRandall Stewart 
1198f8829a4aSRandall Stewart 			if (stcb->rport != rport) {
1199f8829a4aSRandall Stewart 				/* remote port does not match. */
1200f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1201f8829a4aSRandall Stewart 				goto null_return;
1202f8829a4aSRandall Stewart 			}
1203fb8fb8f8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1204fb8fb8f8SRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1205fb8fb8f8SRandall Stewart 				goto null_return;
1206fb8fb8f8SRandall Stewart 			}
1207370d524fSMichael Tuexen 			if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1208749c49acSMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1209749c49acSMichael Tuexen 				goto null_return;
1210749c49acSMichael Tuexen 			}
1211f8829a4aSRandall Stewart 			/* now look at the list of remote addresses */
1212f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1213a5d547adSRandall Stewart #ifdef INVARIANTS
1214f8829a4aSRandall Stewart 				if (net == (TAILQ_NEXT(net, sctp_next))) {
1215f8829a4aSRandall Stewart 					panic("Corrupt net list");
1216f8829a4aSRandall Stewart 				}
1217f8829a4aSRandall Stewart #endif
1218f8829a4aSRandall Stewart 				if (net->ro._l_addr.sa.sa_family !=
1219f8829a4aSRandall Stewart 				    remote->sa_family) {
1220f8829a4aSRandall Stewart 					/* not the same family */
1221f8829a4aSRandall Stewart 					continue;
1222f8829a4aSRandall Stewart 				}
12235e2c2d87SRandall Stewart 				switch (remote->sa_family) {
1224e6194c2eSMichael Tuexen #ifdef INET
12255e2c2d87SRandall Stewart 				case AF_INET:
12265e2c2d87SRandall Stewart 					{
12275e2c2d87SRandall Stewart 						struct sockaddr_in *sin,
12285e2c2d87SRandall Stewart 						           *rsin;
1229f8829a4aSRandall Stewart 
1230f8829a4aSRandall Stewart 						sin = (struct sockaddr_in *)
1231f8829a4aSRandall Stewart 						    &net->ro._l_addr;
1232f8829a4aSRandall Stewart 						rsin = (struct sockaddr_in *)remote;
1233f8829a4aSRandall Stewart 						if (sin->sin_addr.s_addr ==
1234f8829a4aSRandall Stewart 						    rsin->sin_addr.s_addr) {
1235f8829a4aSRandall Stewart 							/* found it */
1236f8829a4aSRandall Stewart 							if (netp != NULL) {
1237f8829a4aSRandall Stewart 								*netp = net;
1238f8829a4aSRandall Stewart 							}
1239f8829a4aSRandall Stewart 							if (locked_tcb == NULL) {
1240f8829a4aSRandall Stewart 								SCTP_INP_DECR_REF(inp);
1241f8829a4aSRandall Stewart 							} else if (locked_tcb != stcb) {
1242f8829a4aSRandall Stewart 								SCTP_TCB_LOCK(locked_tcb);
1243f8829a4aSRandall Stewart 							}
1244c4739e2fSRandall Stewart 							if (locked_tcb) {
1245c4739e2fSRandall Stewart 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1246c4739e2fSRandall Stewart 							}
12470053ed28SMichael Tuexen 
1248f8829a4aSRandall Stewart 							SCTP_INP_WUNLOCK(inp);
1249f8829a4aSRandall Stewart 							SCTP_INP_INFO_RUNLOCK();
1250f8829a4aSRandall Stewart 							return (stcb);
1251f8829a4aSRandall Stewart 						}
12525e2c2d87SRandall Stewart 						break;
12535e2c2d87SRandall Stewart 					}
1254e6194c2eSMichael Tuexen #endif
12555e2c2d87SRandall Stewart #ifdef INET6
12565e2c2d87SRandall Stewart 				case AF_INET6:
12575e2c2d87SRandall Stewart 					{
12585e2c2d87SRandall Stewart 						struct sockaddr_in6 *sin6,
12595e2c2d87SRandall Stewart 						            *rsin6;
1260f8829a4aSRandall Stewart 
1261f8829a4aSRandall Stewart 						sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1262f8829a4aSRandall Stewart 						rsin6 = (struct sockaddr_in6 *)remote;
1263c54a18d2SRandall Stewart 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
1264c54a18d2SRandall Stewart 						    rsin6)) {
1265f8829a4aSRandall Stewart 							/* found it */
1266f8829a4aSRandall Stewart 							if (netp != NULL) {
1267f8829a4aSRandall Stewart 								*netp = net;
1268f8829a4aSRandall Stewart 							}
1269f8829a4aSRandall Stewart 							if (locked_tcb == NULL) {
1270f8829a4aSRandall Stewart 								SCTP_INP_DECR_REF(inp);
1271f8829a4aSRandall Stewart 							} else if (locked_tcb != stcb) {
1272f8829a4aSRandall Stewart 								SCTP_TCB_LOCK(locked_tcb);
1273f8829a4aSRandall Stewart 							}
1274c4739e2fSRandall Stewart 							if (locked_tcb) {
1275c4739e2fSRandall Stewart 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1276c4739e2fSRandall Stewart 							}
1277f8829a4aSRandall Stewart 							SCTP_INP_WUNLOCK(inp);
1278f8829a4aSRandall Stewart 							SCTP_INP_INFO_RUNLOCK();
1279f8829a4aSRandall Stewart 							return (stcb);
1280f8829a4aSRandall Stewart 						}
12815e2c2d87SRandall Stewart 						break;
12825e2c2d87SRandall Stewart 					}
12835e2c2d87SRandall Stewart #endif
12845e2c2d87SRandall Stewart 				default:
12855e2c2d87SRandall Stewart 					/* TSNH */
12865e2c2d87SRandall Stewart 					break;
1287f8829a4aSRandall Stewart 				}
1288f8829a4aSRandall Stewart 			}
1289f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1290f8829a4aSRandall Stewart 		}
1291f8829a4aSRandall Stewart 	} else {
1292f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(inp);
1293f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1294f8829a4aSRandall Stewart 			goto null_return;
1295f8829a4aSRandall Stewart 		}
1296f8829a4aSRandall Stewart 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1297f8829a4aSRandall Stewart 		    inp->sctp_hashmark)];
1298f8829a4aSRandall Stewart 		LIST_FOREACH(stcb, head, sctp_tcbhash) {
1299f8829a4aSRandall Stewart 			if (stcb->rport != rport) {
1300f8829a4aSRandall Stewart 				/* remote port does not match */
1301f8829a4aSRandall Stewart 				continue;
1302f8829a4aSRandall Stewart 			}
1303f8829a4aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1304fb8fb8f8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1305fb8fb8f8SRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1306fb8fb8f8SRandall Stewart 				continue;
1307fb8fb8f8SRandall Stewart 			}
1308370d524fSMichael Tuexen 			if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1309749c49acSMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1310749c49acSMichael Tuexen 				continue;
1311749c49acSMichael Tuexen 			}
1312fb8fb8f8SRandall Stewart 			/* now look at the list of remote addresses */
1313f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1314a5d547adSRandall Stewart #ifdef INVARIANTS
1315f8829a4aSRandall Stewart 				if (net == (TAILQ_NEXT(net, sctp_next))) {
1316f8829a4aSRandall Stewart 					panic("Corrupt net list");
1317f8829a4aSRandall Stewart 				}
1318f8829a4aSRandall Stewart #endif
1319f8829a4aSRandall Stewart 				if (net->ro._l_addr.sa.sa_family !=
1320f8829a4aSRandall Stewart 				    remote->sa_family) {
1321f8829a4aSRandall Stewart 					/* not the same family */
1322f8829a4aSRandall Stewart 					continue;
1323f8829a4aSRandall Stewart 				}
13245e2c2d87SRandall Stewart 				switch (remote->sa_family) {
1325e6194c2eSMichael Tuexen #ifdef INET
13265e2c2d87SRandall Stewart 				case AF_INET:
13275e2c2d87SRandall Stewart 					{
13285e2c2d87SRandall Stewart 						struct sockaddr_in *sin,
13295e2c2d87SRandall Stewart 						           *rsin;
1330f8829a4aSRandall Stewart 
1331f8829a4aSRandall Stewart 						sin = (struct sockaddr_in *)
1332f8829a4aSRandall Stewart 						    &net->ro._l_addr;
1333f8829a4aSRandall Stewart 						rsin = (struct sockaddr_in *)remote;
1334f8829a4aSRandall Stewart 						if (sin->sin_addr.s_addr ==
1335f8829a4aSRandall Stewart 						    rsin->sin_addr.s_addr) {
1336f8829a4aSRandall Stewart 							/* found it */
1337f8829a4aSRandall Stewart 							if (netp != NULL) {
1338f8829a4aSRandall Stewart 								*netp = net;
1339f8829a4aSRandall Stewart 							}
1340f8829a4aSRandall Stewart 							if (locked_tcb == NULL) {
1341f8829a4aSRandall Stewart 								SCTP_INP_DECR_REF(inp);
1342f8829a4aSRandall Stewart 							} else if (locked_tcb != stcb) {
1343f8829a4aSRandall Stewart 								SCTP_TCB_LOCK(locked_tcb);
1344f8829a4aSRandall Stewart 							}
1345c4739e2fSRandall Stewart 							if (locked_tcb) {
1346c4739e2fSRandall Stewart 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1347c4739e2fSRandall Stewart 							}
1348f8829a4aSRandall Stewart 							SCTP_INP_WUNLOCK(inp);
1349f8829a4aSRandall Stewart 							SCTP_INP_INFO_RUNLOCK();
1350f8829a4aSRandall Stewart 							return (stcb);
1351f8829a4aSRandall Stewart 						}
13525e2c2d87SRandall Stewart 						break;
13535e2c2d87SRandall Stewart 					}
1354e6194c2eSMichael Tuexen #endif
13555e2c2d87SRandall Stewart #ifdef INET6
13565e2c2d87SRandall Stewart 				case AF_INET6:
13575e2c2d87SRandall Stewart 					{
13585e2c2d87SRandall Stewart 						struct sockaddr_in6 *sin6,
13595e2c2d87SRandall Stewart 						            *rsin6;
1360f8829a4aSRandall Stewart 
1361f8829a4aSRandall Stewart 						sin6 = (struct sockaddr_in6 *)
1362f8829a4aSRandall Stewart 						    &net->ro._l_addr;
1363f8829a4aSRandall Stewart 						rsin6 = (struct sockaddr_in6 *)remote;
1364c54a18d2SRandall Stewart 						if (SCTP6_ARE_ADDR_EQUAL(sin6,
1365c54a18d2SRandall Stewart 						    rsin6)) {
1366f8829a4aSRandall Stewart 							/* found it */
1367f8829a4aSRandall Stewart 							if (netp != NULL) {
1368f8829a4aSRandall Stewart 								*netp = net;
1369f8829a4aSRandall Stewart 							}
1370f8829a4aSRandall Stewart 							if (locked_tcb == NULL) {
1371f8829a4aSRandall Stewart 								SCTP_INP_DECR_REF(inp);
1372f8829a4aSRandall Stewart 							} else if (locked_tcb != stcb) {
1373f8829a4aSRandall Stewart 								SCTP_TCB_LOCK(locked_tcb);
1374f8829a4aSRandall Stewart 							}
1375c4739e2fSRandall Stewart 							if (locked_tcb) {
1376c4739e2fSRandall Stewart 								atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1377c4739e2fSRandall Stewart 							}
1378f8829a4aSRandall Stewart 							SCTP_INP_WUNLOCK(inp);
1379f8829a4aSRandall Stewart 							SCTP_INP_INFO_RUNLOCK();
1380f8829a4aSRandall Stewart 							return (stcb);
1381f8829a4aSRandall Stewart 						}
13825e2c2d87SRandall Stewart 						break;
13835e2c2d87SRandall Stewart 					}
13845e2c2d87SRandall Stewart #endif
13855e2c2d87SRandall Stewart 				default:
13865e2c2d87SRandall Stewart 					/* TSNH */
13875e2c2d87SRandall Stewart 					break;
1388f8829a4aSRandall Stewart 				}
1389f8829a4aSRandall Stewart 			}
1390f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1391f8829a4aSRandall Stewart 		}
1392f8829a4aSRandall Stewart 	}
1393f8829a4aSRandall Stewart null_return:
1394f8829a4aSRandall Stewart 	/* clean up for returning null */
1395f8829a4aSRandall Stewart 	if (locked_tcb) {
1396f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(locked_tcb);
1397c4739e2fSRandall Stewart 		atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1398f8829a4aSRandall Stewart 	}
1399f8829a4aSRandall Stewart 	SCTP_INP_WUNLOCK(inp);
1400f8829a4aSRandall Stewart 	SCTP_INP_INFO_RUNLOCK();
1401f8829a4aSRandall Stewart 	/* not found */
1402f8829a4aSRandall Stewart 	return (NULL);
1403f8829a4aSRandall Stewart }
1404f8829a4aSRandall Stewart 
1405f8829a4aSRandall Stewart /*
1406f8829a4aSRandall Stewart  * Find an association for a specific endpoint using the association id given
1407f8829a4aSRandall Stewart  * out in the COMM_UP notification
1408f8829a4aSRandall Stewart  */
1409f8829a4aSRandall Stewart struct sctp_tcb *
sctp_findasoc_ep_asocid_locked(struct sctp_inpcb * inp,sctp_assoc_t asoc_id,int want_lock)1410830d754dSRandall Stewart sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1411f8829a4aSRandall Stewart {
1412f8829a4aSRandall Stewart 	/*
1413f8829a4aSRandall Stewart 	 * Use my the assoc_id to find a endpoint
1414f8829a4aSRandall Stewart 	 */
1415f8829a4aSRandall Stewart 	struct sctpasochead *head;
1416f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1417f8829a4aSRandall Stewart 	uint32_t id;
1418f8829a4aSRandall Stewart 
1419830d754dSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1420830d754dSRandall Stewart 		SCTP_PRINTF("TSNH ep_associd0\n");
1421830d754dSRandall Stewart 		return (NULL);
1422830d754dSRandall Stewart 	}
1423f8829a4aSRandall Stewart 	id = (uint32_t)asoc_id;
1424830d754dSRandall Stewart 	head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1425f8829a4aSRandall Stewart 	if (head == NULL) {
1426f8829a4aSRandall Stewart 		/* invalid id TSNH */
1427830d754dSRandall Stewart 		SCTP_PRINTF("TSNH ep_associd1\n");
1428f8829a4aSRandall Stewart 		return (NULL);
1429f8829a4aSRandall Stewart 	}
1430830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1431f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_id == id) {
1432f8829a4aSRandall Stewart 			if (inp != stcb->sctp_ep) {
1433f8829a4aSRandall Stewart 				/*
1434f8829a4aSRandall Stewart 				 * some other guy has the same id active (id
1435f8829a4aSRandall Stewart 				 * collision ??).
1436f8829a4aSRandall Stewart 				 */
1437830d754dSRandall Stewart 				SCTP_PRINTF("TSNH ep_associd2\n");
1438f8829a4aSRandall Stewart 				continue;
1439f8829a4aSRandall Stewart 			}
1440fb8fb8f8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1441fb8fb8f8SRandall Stewart 				continue;
1442fb8fb8f8SRandall Stewart 			}
1443f8829a4aSRandall Stewart 			if (want_lock) {
1444f8829a4aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
1445f8829a4aSRandall Stewart 			}
1446f8829a4aSRandall Stewart 			return (stcb);
1447f8829a4aSRandall Stewart 		}
1448f8829a4aSRandall Stewart 	}
1449f8829a4aSRandall Stewart 	return (NULL);
1450f8829a4aSRandall Stewart }
1451830d754dSRandall Stewart 
1452830d754dSRandall Stewart struct sctp_tcb *
sctp_findassociation_ep_asocid(struct sctp_inpcb * inp,sctp_assoc_t asoc_id,int want_lock)1453830d754dSRandall Stewart sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1454830d754dSRandall Stewart {
1455830d754dSRandall Stewart 	struct sctp_tcb *stcb;
1456830d754dSRandall Stewart 
1457830d754dSRandall Stewart 	SCTP_INP_RLOCK(inp);
1458830d754dSRandall Stewart 	stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1459830d754dSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1460f8829a4aSRandall Stewart 	return (stcb);
1461f8829a4aSRandall Stewart }
1462f8829a4aSRandall Stewart 
1463a1cb341bSMichael Tuexen /*
1464a1cb341bSMichael Tuexen  * Endpoint probe expects that the INP_INFO is locked.
1465a1cb341bSMichael Tuexen  */
1466f8829a4aSRandall Stewart static struct sctp_inpcb *
sctp_endpoint_probe(struct sockaddr * nam,struct sctppcbhead * head,uint16_t lport,uint32_t vrf_id)1467f8829a4aSRandall Stewart sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
146842551e99SRandall Stewart     uint16_t lport, uint32_t vrf_id)
1469f8829a4aSRandall Stewart {
1470f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1471f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
1472e6194c2eSMichael Tuexen #ifdef INET
1473e6194c2eSMichael Tuexen 	struct sockaddr_in *sin;
1474e6194c2eSMichael Tuexen #endif
14755e2c2d87SRandall Stewart #ifdef INET6
1476e6194c2eSMichael Tuexen 	struct sockaddr_in6 *sin6;
14775e2c2d87SRandall Stewart 	struct sockaddr_in6 *intf_addr6;
14785e2c2d87SRandall Stewart #endif
147942551e99SRandall Stewart 	int fnd;
1480f8829a4aSRandall Stewart 
1481e6194c2eSMichael Tuexen #ifdef INET
1482f8829a4aSRandall Stewart 	sin = NULL;
1483e6194c2eSMichael Tuexen #endif
14845e2c2d87SRandall Stewart #ifdef INET6
14855e2c2d87SRandall Stewart 	sin6 = NULL;
14865e2c2d87SRandall Stewart #endif
14875e2c2d87SRandall Stewart 	switch (nam->sa_family) {
1488e6194c2eSMichael Tuexen #ifdef INET
14895e2c2d87SRandall Stewart 	case AF_INET:
14905e2c2d87SRandall Stewart 		sin = (struct sockaddr_in *)nam;
14915e2c2d87SRandall Stewart 		break;
1492e6194c2eSMichael Tuexen #endif
14935e2c2d87SRandall Stewart #ifdef INET6
14945e2c2d87SRandall Stewart 	case AF_INET6:
14955e2c2d87SRandall Stewart 		sin6 = (struct sockaddr_in6 *)nam;
14965e2c2d87SRandall Stewart 		break;
14975e2c2d87SRandall Stewart #endif
14985e2c2d87SRandall Stewart 	default:
1499f8829a4aSRandall Stewart 		/* unsupported family */
1500f8829a4aSRandall Stewart 		return (NULL);
1501f8829a4aSRandall Stewart 	}
15026d9e8f2bSRandall Stewart 
1503f8829a4aSRandall Stewart 	if (head == NULL)
1504f8829a4aSRandall Stewart 		return (NULL);
1505830d754dSRandall Stewart 
1506f8829a4aSRandall Stewart 	LIST_FOREACH(inp, head, sctp_hash) {
1507f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(inp);
1508f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1509f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1510f8829a4aSRandall Stewart 			continue;
1511f8829a4aSRandall Stewart 		}
1512f8829a4aSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1513f8829a4aSRandall Stewart 		    (inp->sctp_lport == lport)) {
1514f8829a4aSRandall Stewart 			/* got it */
15156ba22f19SMichael Tuexen 			switch (nam->sa_family) {
1516e6194c2eSMichael Tuexen #ifdef INET
15176ba22f19SMichael Tuexen 			case AF_INET:
15186ba22f19SMichael Tuexen 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
151944b7479bSRandall Stewart 				    SCTP_IPV6_V6ONLY(inp)) {
1520b7b84c0eSMichael Tuexen 					/*
1521b7b84c0eSMichael Tuexen 					 * IPv4 on a IPv6 socket with ONLY
1522b7b84c0eSMichael Tuexen 					 * IPv6 set
1523b7b84c0eSMichael Tuexen 					 */
1524f8829a4aSRandall Stewart 					SCTP_INP_RUNLOCK(inp);
1525f8829a4aSRandall Stewart 					continue;
1526f8829a4aSRandall Stewart 				}
15276ba22f19SMichael Tuexen 				if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
15286ba22f19SMichael Tuexen 				    &sin->sin_addr) != 0) {
15296ba22f19SMichael Tuexen 					SCTP_INP_RUNLOCK(inp);
15306ba22f19SMichael Tuexen 					continue;
15316ba22f19SMichael Tuexen 				}
15326ba22f19SMichael Tuexen 				break;
1533e6194c2eSMichael Tuexen #endif
1534e6194c2eSMichael Tuexen #ifdef INET6
15356ba22f19SMichael Tuexen 			case AF_INET6:
1536b7b84c0eSMichael Tuexen 				/*
1537b7b84c0eSMichael Tuexen 				 * A V6 address and the endpoint is NOT
1538b7b84c0eSMichael Tuexen 				 * bound V6
1539b7b84c0eSMichael Tuexen 				 */
15406ba22f19SMichael Tuexen 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1541f8829a4aSRandall Stewart 					SCTP_INP_RUNLOCK(inp);
1542f8829a4aSRandall Stewart 					continue;
1543f8829a4aSRandall Stewart 				}
15446ba22f19SMichael Tuexen 				if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
15456ba22f19SMichael Tuexen 				    &sin6->sin6_addr) != 0) {
15466ba22f19SMichael Tuexen 					SCTP_INP_RUNLOCK(inp);
15476ba22f19SMichael Tuexen 					continue;
15486ba22f19SMichael Tuexen 				}
15496ba22f19SMichael Tuexen 				break;
1550e6194c2eSMichael Tuexen #endif
15516ba22f19SMichael Tuexen 			default:
15526ba22f19SMichael Tuexen 				break;
15536ba22f19SMichael Tuexen 			}
155442551e99SRandall Stewart 			/* does a VRF id match? */
155542551e99SRandall Stewart 			fnd = 0;
155642551e99SRandall Stewart 			if (inp->def_vrf_id == vrf_id)
155742551e99SRandall Stewart 				fnd = 1;
155842551e99SRandall Stewart 
1559f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
156042551e99SRandall Stewart 			if (!fnd)
156142551e99SRandall Stewart 				continue;
1562f8829a4aSRandall Stewart 			return (inp);
1563f8829a4aSRandall Stewart 		}
1564f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1565f8829a4aSRandall Stewart 	}
1566e6194c2eSMichael Tuexen 	switch (nam->sa_family) {
1567e6194c2eSMichael Tuexen #ifdef INET
1568e6194c2eSMichael Tuexen 	case AF_INET:
1569e6194c2eSMichael Tuexen 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1570f8829a4aSRandall Stewart 			/* Can't hunt for one that has no address specified */
1571f8829a4aSRandall Stewart 			return (NULL);
15725e2c2d87SRandall Stewart 		}
1573e6194c2eSMichael Tuexen 		break;
15745e2c2d87SRandall Stewart #endif
1575e6194c2eSMichael Tuexen #ifdef INET6
1576e6194c2eSMichael Tuexen 	case AF_INET6:
1577e6194c2eSMichael Tuexen 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1578e6194c2eSMichael Tuexen 			/* Can't hunt for one that has no address specified */
1579e6194c2eSMichael Tuexen 			return (NULL);
1580e6194c2eSMichael Tuexen 		}
1581e6194c2eSMichael Tuexen 		break;
1582e6194c2eSMichael Tuexen #endif
1583e6194c2eSMichael Tuexen 	default:
1584e6194c2eSMichael Tuexen 		break;
1585e6194c2eSMichael Tuexen 	}
1586f8829a4aSRandall Stewart 	/*
1587f8829a4aSRandall Stewart 	 * ok, not bound to all so see if we can find a EP bound to this
1588f8829a4aSRandall Stewart 	 * address.
1589f8829a4aSRandall Stewart 	 */
1590f8829a4aSRandall Stewart 	LIST_FOREACH(inp, head, sctp_hash) {
1591f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(inp);
1592f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1593f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1594f8829a4aSRandall Stewart 			continue;
1595f8829a4aSRandall Stewart 		}
1596f8829a4aSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1597f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1598f8829a4aSRandall Stewart 			continue;
1599f8829a4aSRandall Stewart 		}
1600f8829a4aSRandall Stewart 		/*
1601f8829a4aSRandall Stewart 		 * Ok this could be a likely candidate, look at all of its
1602f8829a4aSRandall Stewart 		 * addresses
1603f8829a4aSRandall Stewart 		 */
1604f8829a4aSRandall Stewart 		if (inp->sctp_lport != lport) {
1605f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1606f8829a4aSRandall Stewart 			continue;
1607f8829a4aSRandall Stewart 		}
160842551e99SRandall Stewart 		/* does a VRF id match? */
160942551e99SRandall Stewart 		fnd = 0;
161042551e99SRandall Stewart 		if (inp->def_vrf_id == vrf_id)
161142551e99SRandall Stewart 			fnd = 1;
161242551e99SRandall Stewart 
161342551e99SRandall Stewart 		if (!fnd) {
161442551e99SRandall Stewart 			SCTP_INP_RUNLOCK(inp);
161542551e99SRandall Stewart 			continue;
161642551e99SRandall Stewart 		}
1617f8829a4aSRandall Stewart 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1618f8829a4aSRandall Stewart 			if (laddr->ifa == NULL) {
1619ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
16206e9c45e0SMichael Tuexen 				    __func__);
1621f8829a4aSRandall Stewart 				continue;
1622f8829a4aSRandall Stewart 			}
1623ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1624dd294dceSMichael Tuexen 			    (void *)laddr->ifa);
162542551e99SRandall Stewart 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1626ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1627f8829a4aSRandall Stewart 				continue;
1628f8829a4aSRandall Stewart 			}
162942551e99SRandall Stewart 			if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1630f8829a4aSRandall Stewart 				/* possible, see if it matches */
16315e2c2d87SRandall Stewart 				switch (nam->sa_family) {
1632e6194c2eSMichael Tuexen #ifdef INET
16335e2c2d87SRandall Stewart 				case AF_INET:
1634f8829a4aSRandall Stewart 					if (sin->sin_addr.s_addr ==
1635e6194c2eSMichael Tuexen 					    laddr->ifa->address.sin.sin_addr.s_addr) {
1636f8829a4aSRandall Stewart 						SCTP_INP_RUNLOCK(inp);
1637f8829a4aSRandall Stewart 						return (inp);
1638f8829a4aSRandall Stewart 					}
16395e2c2d87SRandall Stewart 					break;
1640e6194c2eSMichael Tuexen #endif
16415e2c2d87SRandall Stewart #ifdef INET6
16425e2c2d87SRandall Stewart 				case AF_INET6:
164342551e99SRandall Stewart 					intf_addr6 = &laddr->ifa->address.sin6;
1644c54a18d2SRandall Stewart 					if (SCTP6_ARE_ADDR_EQUAL(sin6,
1645c54a18d2SRandall Stewart 					    intf_addr6)) {
1646f8829a4aSRandall Stewart 						SCTP_INP_RUNLOCK(inp);
1647f8829a4aSRandall Stewart 						return (inp);
1648f8829a4aSRandall Stewart 					}
16495e2c2d87SRandall Stewart 					break;
16505e2c2d87SRandall Stewart #endif
1651f8829a4aSRandall Stewart 				}
1652f8829a4aSRandall Stewart 			}
1653f8829a4aSRandall Stewart 		}
1654f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1655f8829a4aSRandall Stewart 	}
1656f8829a4aSRandall Stewart 	return (NULL);
1657f8829a4aSRandall Stewart }
1658f8829a4aSRandall Stewart 
16596d9e8f2bSRandall Stewart static struct sctp_inpcb *
sctp_isport_inuse(struct sctp_inpcb * inp,uint16_t lport,uint32_t vrf_id)16606d9e8f2bSRandall Stewart sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
16616d9e8f2bSRandall Stewart {
16626d9e8f2bSRandall Stewart 	struct sctppcbhead *head;
16636d9e8f2bSRandall Stewart 	struct sctp_inpcb *t_inp;
16646d9e8f2bSRandall Stewart 	int fnd;
16656d9e8f2bSRandall Stewart 
16666d9e8f2bSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
16676d9e8f2bSRandall Stewart 	    SCTP_BASE_INFO(hashmark))];
16686d9e8f2bSRandall Stewart 	LIST_FOREACH(t_inp, head, sctp_hash) {
16696d9e8f2bSRandall Stewart 		if (t_inp->sctp_lport != lport) {
16706d9e8f2bSRandall Stewart 			continue;
16716d9e8f2bSRandall Stewart 		}
16726d9e8f2bSRandall Stewart 		/* is it in the VRF in question */
16736d9e8f2bSRandall Stewart 		fnd = 0;
16746d9e8f2bSRandall Stewart 		if (t_inp->def_vrf_id == vrf_id)
16756d9e8f2bSRandall Stewart 			fnd = 1;
16766d9e8f2bSRandall Stewart 		if (!fnd)
16776d9e8f2bSRandall Stewart 			continue;
16786d9e8f2bSRandall Stewart 
16796d9e8f2bSRandall Stewart 		/* This one is in use. */
16806d9e8f2bSRandall Stewart 		/* check the v6/v4 binding issue */
16816d9e8f2bSRandall Stewart 		if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
16826d9e8f2bSRandall Stewart 		    SCTP_IPV6_V6ONLY(t_inp)) {
16836d9e8f2bSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
16846d9e8f2bSRandall Stewart 				/* collision in V6 space */
16856d9e8f2bSRandall Stewart 				return (t_inp);
16866d9e8f2bSRandall Stewart 			} else {
16876d9e8f2bSRandall Stewart 				/* inp is BOUND_V4 no conflict */
16886d9e8f2bSRandall Stewart 				continue;
16896d9e8f2bSRandall Stewart 			}
16906d9e8f2bSRandall Stewart 		} else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
16916d9e8f2bSRandall Stewart 			/* t_inp is bound v4 and v6, conflict always */
16926d9e8f2bSRandall Stewart 			return (t_inp);
16936d9e8f2bSRandall Stewart 		} else {
16946d9e8f2bSRandall Stewart 			/* t_inp is bound only V4 */
16956d9e8f2bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
16966d9e8f2bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
16976d9e8f2bSRandall Stewart 				/* no conflict */
16986d9e8f2bSRandall Stewart 				continue;
16996d9e8f2bSRandall Stewart 			}
17006d9e8f2bSRandall Stewart 			/* else fall through to conflict */
17016d9e8f2bSRandall Stewart 		}
17026d9e8f2bSRandall Stewart 		return (t_inp);
17036d9e8f2bSRandall Stewart 	}
17046d9e8f2bSRandall Stewart 	return (NULL);
17056d9e8f2bSRandall Stewart }
17066d9e8f2bSRandall Stewart 
17076d9e8f2bSRandall Stewart int
sctp_swap_inpcb_for_listen(struct sctp_inpcb * inp)17086d9e8f2bSRandall Stewart sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
17096d9e8f2bSRandall Stewart {
17106d9e8f2bSRandall Stewart 	/* For 1-2-1 with port reuse */
17116d9e8f2bSRandall Stewart 	struct sctppcbhead *head;
17120426123fSMichael Tuexen 	struct sctp_inpcb *tinp, *ninp;
17136d9e8f2bSRandall Stewart 
17146e3af632SMark Johnston 	SCTP_INP_INFO_WLOCK_ASSERT();
1715ee473117SMark Johnston 	SCTP_INP_WLOCK_ASSERT(inp);
17166e3af632SMark Johnston 
17176d9e8f2bSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
17186d9e8f2bSRandall Stewart 		/* only works with port reuse on */
17196d9e8f2bSRandall Stewart 		return (-1);
17206d9e8f2bSRandall Stewart 	}
17216d9e8f2bSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
17226d9e8f2bSRandall Stewart 		return (0);
17236d9e8f2bSRandall Stewart 	}
1724ee473117SMark Johnston 	SCTP_INP_WUNLOCK(inp);
17256d9e8f2bSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
17266d9e8f2bSRandall Stewart 	    SCTP_BASE_INFO(hashmark))];
17276d9e8f2bSRandall Stewart 	/* Kick out all non-listeners to the TCP hash */
17280426123fSMichael Tuexen 	LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
17296d9e8f2bSRandall Stewart 		if (tinp->sctp_lport != inp->sctp_lport) {
17306d9e8f2bSRandall Stewart 			continue;
17316d9e8f2bSRandall Stewart 		}
17326d9e8f2bSRandall Stewart 		if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
17336d9e8f2bSRandall Stewart 			continue;
17346d9e8f2bSRandall Stewart 		}
17356d9e8f2bSRandall Stewart 		if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
17366d9e8f2bSRandall Stewart 			continue;
17376d9e8f2bSRandall Stewart 		}
17385d08768aSMichael Tuexen 		if (SCTP_IS_LISTENING(tinp)) {
17396d9e8f2bSRandall Stewart 			continue;
17406d9e8f2bSRandall Stewart 		}
17416d9e8f2bSRandall Stewart 		SCTP_INP_WLOCK(tinp);
17426d9e8f2bSRandall Stewart 		LIST_REMOVE(tinp, sctp_hash);
17436d9e8f2bSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
17446d9e8f2bSRandall Stewart 		tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
17456d9e8f2bSRandall Stewart 		LIST_INSERT_HEAD(head, tinp, sctp_hash);
17466d9e8f2bSRandall Stewart 		SCTP_INP_WUNLOCK(tinp);
17476d9e8f2bSRandall Stewart 	}
17486d9e8f2bSRandall Stewart 	SCTP_INP_WLOCK(inp);
17496d9e8f2bSRandall Stewart 	/* Pull from where he was */
17506d9e8f2bSRandall Stewart 	LIST_REMOVE(inp, sctp_hash);
17516d9e8f2bSRandall Stewart 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
17526d9e8f2bSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
17536d9e8f2bSRandall Stewart 	LIST_INSERT_HEAD(head, inp, sctp_hash);
17546d9e8f2bSRandall Stewart 	return (0);
17556d9e8f2bSRandall Stewart }
17566d9e8f2bSRandall Stewart 
1757f8829a4aSRandall Stewart struct sctp_inpcb *
sctp_pcb_findep(struct sockaddr * nam,int find_tcp_pool,int have_lock,uint32_t vrf_id)1758ad81507eSRandall Stewart sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1759ad81507eSRandall Stewart     uint32_t vrf_id)
1760f8829a4aSRandall Stewart {
1761f8829a4aSRandall Stewart 	/*
1762f8829a4aSRandall Stewart 	 * First we check the hash table to see if someone has this port
1763f8829a4aSRandall Stewart 	 * bound with just the port.
1764f8829a4aSRandall Stewart 	 */
1765f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1766f8829a4aSRandall Stewart 	struct sctppcbhead *head;
1767f8829a4aSRandall Stewart 	int lport;
1768034b88b0SMichael Tuexen 	unsigned int i;
1769e6194c2eSMichael Tuexen #ifdef INET
1770e6194c2eSMichael Tuexen 	struct sockaddr_in *sin;
1771e6194c2eSMichael Tuexen #endif
1772e6194c2eSMichael Tuexen #ifdef INET6
1773e6194c2eSMichael Tuexen 	struct sockaddr_in6 *sin6;
1774e6194c2eSMichael Tuexen #endif
1775e6194c2eSMichael Tuexen 
1776e6194c2eSMichael Tuexen 	switch (nam->sa_family) {
1777e6194c2eSMichael Tuexen #ifdef INET
1778e6194c2eSMichael Tuexen 	case AF_INET:
1779f8829a4aSRandall Stewart 		sin = (struct sockaddr_in *)nam;
178060990c0cSMichael Tuexen 		lport = sin->sin_port;
1781e6194c2eSMichael Tuexen 		break;
1782e6194c2eSMichael Tuexen #endif
1783e6194c2eSMichael Tuexen #ifdef INET6
1784e6194c2eSMichael Tuexen 	case AF_INET6:
1785f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)nam;
178660990c0cSMichael Tuexen 		lport = sin6->sin6_port;
1787e6194c2eSMichael Tuexen 		break;
1788e6194c2eSMichael Tuexen #endif
1789e6194c2eSMichael Tuexen 	default:
1790f8829a4aSRandall Stewart 		return (NULL);
1791f8829a4aSRandall Stewart 	}
1792f8829a4aSRandall Stewart 	/*
1793f8829a4aSRandall Stewart 	 * I could cheat here and just cast to one of the types but we will
1794f8829a4aSRandall Stewart 	 * do it right. It also provides the check against an Unsupported
1795f8829a4aSRandall Stewart 	 * type too.
1796f8829a4aSRandall Stewart 	 */
1797f8829a4aSRandall Stewart 	/* Find the head of the ALLADDR chain */
1798f8829a4aSRandall Stewart 	if (have_lock == 0) {
1799f8829a4aSRandall Stewart 		SCTP_INP_INFO_RLOCK();
1800f8829a4aSRandall Stewart 	}
1801b3f1ea41SRandall Stewart 	head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1802b3f1ea41SRandall Stewart 	    SCTP_BASE_INFO(hashmark))];
180342551e99SRandall Stewart 	inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1804f8829a4aSRandall Stewart 
1805f8829a4aSRandall Stewart 	/*
1806f8829a4aSRandall Stewart 	 * If the TCP model exists it could be that the main listening
1807034b88b0SMichael Tuexen 	 * endpoint is gone but there still exists a connected socket for
1808034b88b0SMichael Tuexen 	 * this guy. If so we can return the first one that we find. This
1809034b88b0SMichael Tuexen 	 * may NOT be the correct one so the caller should be wary on the
1810034b88b0SMichael Tuexen 	 * returned INP. Currently the only caller that sets find_tcp_pool
1811034b88b0SMichael Tuexen 	 * is in bindx where we are verifying that a user CAN bind the
1812034b88b0SMichael Tuexen 	 * address. He either has bound it already, or someone else has, or
1813034b88b0SMichael Tuexen 	 * its open to bind, so this is good enough.
1814f8829a4aSRandall Stewart 	 */
1815f8829a4aSRandall Stewart 	if (inp == NULL && find_tcp_pool) {
1816034b88b0SMichael Tuexen 		for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
1817034b88b0SMichael Tuexen 			head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
181842551e99SRandall Stewart 			inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1819034b88b0SMichael Tuexen 			if (inp) {
1820034b88b0SMichael Tuexen 				break;
1821034b88b0SMichael Tuexen 			}
1822034b88b0SMichael Tuexen 		}
1823f8829a4aSRandall Stewart 	}
1824f8829a4aSRandall Stewart 	if (inp) {
1825f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1826f8829a4aSRandall Stewart 	}
1827f8829a4aSRandall Stewart 	if (have_lock == 0) {
1828f8829a4aSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
1829f8829a4aSRandall Stewart 	}
1830f8829a4aSRandall Stewart 	return (inp);
1831f8829a4aSRandall Stewart }
1832f8829a4aSRandall Stewart 
1833f8829a4aSRandall Stewart /*
1834f8829a4aSRandall Stewart  * Find an association for an endpoint with the pointer to whom you want to
1835f8829a4aSRandall Stewart  * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1836f8829a4aSRandall Stewart  * need to change the *to to some other struct like a mbuf...
1837f8829a4aSRandall Stewart  */
1838f8829a4aSRandall Stewart struct sctp_tcb *
sctp_findassociation_addr_sa(struct sockaddr * from,struct sockaddr * to,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,int find_tcp_pool,uint32_t vrf_id)1839b1754ad1SMichael Tuexen sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
18409a972525SRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
18419a972525SRandall Stewart     uint32_t vrf_id)
1842f8829a4aSRandall Stewart {
1843c105859eSRandall Stewart 	struct sctp_inpcb *inp = NULL;
1844f0d44a49SMichael Tuexen 	struct sctp_tcb *stcb;
1845f8829a4aSRandall Stewart 
1846f8829a4aSRandall Stewart 	SCTP_INP_INFO_RLOCK();
1847f8829a4aSRandall Stewart 	if (find_tcp_pool) {
1848f8829a4aSRandall Stewart 		if (inp_p != NULL) {
1849f0d44a49SMichael Tuexen 			stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
18509a972525SRandall Stewart 			    vrf_id);
1851f8829a4aSRandall Stewart 		} else {
1852f0d44a49SMichael Tuexen 			stcb = sctp_tcb_special_locate(&inp, from, to, netp,
18539a972525SRandall Stewart 			    vrf_id);
1854f8829a4aSRandall Stewart 		}
1855f0d44a49SMichael Tuexen 		if (stcb != NULL) {
1856f8829a4aSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1857f0d44a49SMichael Tuexen 			return (stcb);
1858f8829a4aSRandall Stewart 		}
1859f8829a4aSRandall Stewart 	}
186042551e99SRandall Stewart 	inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1861f8829a4aSRandall Stewart 	if (inp_p != NULL) {
1862f8829a4aSRandall Stewart 		*inp_p = inp;
1863f8829a4aSRandall Stewart 	}
1864f8829a4aSRandall Stewart 	SCTP_INP_INFO_RUNLOCK();
1865f8829a4aSRandall Stewart 	if (inp == NULL) {
1866f8829a4aSRandall Stewart 		return (NULL);
1867f8829a4aSRandall Stewart 	}
1868f8829a4aSRandall Stewart 	/*
1869f8829a4aSRandall Stewart 	 * ok, we have an endpoint, now lets find the assoc for it (if any)
1870f8829a4aSRandall Stewart 	 * we now place the source address or from in the to of the find
1871f8829a4aSRandall Stewart 	 * endpoint call. Since in reality this chain is used from the
1872f8829a4aSRandall Stewart 	 * inbound packet side.
1873f8829a4aSRandall Stewart 	 */
1874f8829a4aSRandall Stewart 	if (inp_p != NULL) {
1875f0d44a49SMichael Tuexen 		stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
18769a972525SRandall Stewart 		    NULL);
1877f8829a4aSRandall Stewart 	} else {
1878f0d44a49SMichael Tuexen 		stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
18799a972525SRandall Stewart 		    NULL);
1880f8829a4aSRandall Stewart 	}
1881f0d44a49SMichael Tuexen 	return (stcb);
1882f8829a4aSRandall Stewart }
1883f8829a4aSRandall Stewart 
1884f8829a4aSRandall Stewart /*
1885f8829a4aSRandall Stewart  * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1886f8829a4aSRandall Stewart  * find all addresses that the sender has specified in any address list. Each
1887f8829a4aSRandall Stewart  * address will be used to lookup the TCB and see if one exits.
1888f8829a4aSRandall Stewart  */
1889f8829a4aSRandall Stewart static struct sctp_tcb *
sctp_findassociation_special_addr(struct mbuf * m,int offset,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,struct sockaddr * dst)18907215cc1bSMichael Tuexen sctp_findassociation_special_addr(struct mbuf *m, int offset,
1891f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1892b1754ad1SMichael Tuexen     struct sockaddr *dst)
1893f8829a4aSRandall Stewart {
1894f4358911SMichael Tuexen 	struct sctp_paramhdr *phdr, param_buf;
1895b54ddf22SMichael Tuexen #if defined(INET) || defined(INET6)
1896f0d44a49SMichael Tuexen 	struct sctp_tcb *stcb;
1897b54ddf22SMichael Tuexen 	uint16_t ptype;
1898b54ddf22SMichael Tuexen #endif
1899b54ddf22SMichael Tuexen 	uint16_t plen;
1900e6194c2eSMichael Tuexen #ifdef INET
1901e6194c2eSMichael Tuexen 	struct sockaddr_in sin4;
1902e6194c2eSMichael Tuexen #endif
1903e6194c2eSMichael Tuexen #ifdef INET6
1904e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
1905e6194c2eSMichael Tuexen #endif
1906e6194c2eSMichael Tuexen 
1907e6194c2eSMichael Tuexen #ifdef INET
1908f8829a4aSRandall Stewart 	memset(&sin4, 0, sizeof(sin4));
1909f8829a4aSRandall Stewart 	sin4.sin_len = sizeof(sin4);
1910f8829a4aSRandall Stewart 	sin4.sin_family = AF_INET;
1911f8829a4aSRandall Stewart 	sin4.sin_port = sh->src_port;
1912e6194c2eSMichael Tuexen #endif
1913e6194c2eSMichael Tuexen #ifdef INET6
1914e6194c2eSMichael Tuexen 	memset(&sin6, 0, sizeof(sin6));
1915f8829a4aSRandall Stewart 	sin6.sin6_len = sizeof(sin6);
1916f8829a4aSRandall Stewart 	sin6.sin6_family = AF_INET6;
1917f8829a4aSRandall Stewart 	sin6.sin6_port = sh->src_port;
1918e6194c2eSMichael Tuexen #endif
1919f8829a4aSRandall Stewart 
1920f8829a4aSRandall Stewart 	offset += sizeof(struct sctp_init_chunk);
1921f8829a4aSRandall Stewart 
1922f4358911SMichael Tuexen 	phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
1923f8829a4aSRandall Stewart 	while (phdr != NULL) {
1924f8829a4aSRandall Stewart 		/* now we must see if we want the parameter */
1925b54ddf22SMichael Tuexen #if defined(INET) || defined(INET6)
1926f8829a4aSRandall Stewart 		ptype = ntohs(phdr->param_type);
1927b54ddf22SMichael Tuexen #endif
1928f8829a4aSRandall Stewart 		plen = ntohs(phdr->param_length);
1929f8829a4aSRandall Stewart 		if (plen == 0) {
1930f8829a4aSRandall Stewart 			break;
1931f8829a4aSRandall Stewart 		}
1932e6194c2eSMichael Tuexen #ifdef INET
1933f8829a4aSRandall Stewart 		if (ptype == SCTP_IPV4_ADDRESS &&
1934f8829a4aSRandall Stewart 		    plen == sizeof(struct sctp_ipv4addr_param)) {
1935f8829a4aSRandall Stewart 			/* Get the rest of the address */
1936f4358911SMichael Tuexen 			struct sctp_ipv4addr_param ip4_param, *p4;
1937f8829a4aSRandall Stewart 
1938f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
1939f4358911SMichael Tuexen 			    (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
1940f8829a4aSRandall Stewart 			if (phdr == NULL) {
1941f8829a4aSRandall Stewart 				return (NULL);
1942f8829a4aSRandall Stewart 			}
1943f8829a4aSRandall Stewart 			p4 = (struct sctp_ipv4addr_param *)phdr;
1944f8829a4aSRandall Stewart 			memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1945f8829a4aSRandall Stewart 			/* look it up */
1946f0d44a49SMichael Tuexen 			stcb = sctp_findassociation_ep_addr(inp_p,
1947b1754ad1SMichael Tuexen 			    (struct sockaddr *)&sin4, netp, dst, NULL);
1948f0d44a49SMichael Tuexen 			if (stcb != NULL) {
1949f0d44a49SMichael Tuexen 				return (stcb);
1950f8829a4aSRandall Stewart 			}
1951e6194c2eSMichael Tuexen 		}
1952e6194c2eSMichael Tuexen #endif
1953e6194c2eSMichael Tuexen #ifdef INET6
1954e6194c2eSMichael Tuexen 		if (ptype == SCTP_IPV6_ADDRESS &&
1955f8829a4aSRandall Stewart 		    plen == sizeof(struct sctp_ipv6addr_param)) {
1956f8829a4aSRandall Stewart 			/* Get the rest of the address */
1957f4358911SMichael Tuexen 			struct sctp_ipv6addr_param ip6_param, *p6;
1958f8829a4aSRandall Stewart 
1959f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
1960f4358911SMichael Tuexen 			    (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
1961f8829a4aSRandall Stewart 			if (phdr == NULL) {
1962f8829a4aSRandall Stewart 				return (NULL);
1963f8829a4aSRandall Stewart 			}
1964f8829a4aSRandall Stewart 			p6 = (struct sctp_ipv6addr_param *)phdr;
1965f8829a4aSRandall Stewart 			memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1966f8829a4aSRandall Stewart 			/* look it up */
1967f0d44a49SMichael Tuexen 			stcb = sctp_findassociation_ep_addr(inp_p,
1968b1754ad1SMichael Tuexen 			    (struct sockaddr *)&sin6, netp, dst, NULL);
1969f0d44a49SMichael Tuexen 			if (stcb != NULL) {
1970f0d44a49SMichael Tuexen 				return (stcb);
1971f8829a4aSRandall Stewart 			}
1972f8829a4aSRandall Stewart 		}
1973e6194c2eSMichael Tuexen #endif
1974f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(plen);
1975f4358911SMichael Tuexen 		phdr = sctp_get_next_param(m, offset, &param_buf,
1976f4358911SMichael Tuexen 		    sizeof(param_buf));
1977f8829a4aSRandall Stewart 	}
1978f8829a4aSRandall Stewart 	return (NULL);
1979f8829a4aSRandall Stewart }
1980f8829a4aSRandall Stewart 
1981f8829a4aSRandall Stewart static struct sctp_tcb *
sctp_findassoc_by_vtag(struct sockaddr * from,struct sockaddr * to,uint32_t vtag,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,uint16_t rport,uint16_t lport,int skip_src_check,uint32_t vrf_id,uint32_t remote_tag)1982830d754dSRandall Stewart sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
1983f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1984830d754dSRandall Stewart     uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
1985f8829a4aSRandall Stewart {
1986f8829a4aSRandall Stewart 	/*
1987f8829a4aSRandall Stewart 	 * Use my vtag to hash. If we find it we then verify the source addr
1988f8829a4aSRandall Stewart 	 * is in the assoc. If all goes well we save a bit on rec of a
1989f8829a4aSRandall Stewart 	 * packet.
1990f8829a4aSRandall Stewart 	 */
1991f8829a4aSRandall Stewart 	struct sctpasochead *head;
1992f8829a4aSRandall Stewart 	struct sctp_nets *net;
1993f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1994f8829a4aSRandall Stewart 
1995f8829a4aSRandall Stewart 	SCTP_INP_INFO_RLOCK();
1996b3f1ea41SRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
1997b3f1ea41SRandall Stewart 	    SCTP_BASE_INFO(hashasocmark))];
1998f8829a4aSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
1999f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(stcb->sctp_ep);
2000f8829a4aSRandall Stewart 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2001f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(stcb->sctp_ep);
2002d06c82f1SRandall Stewart 			continue;
2003f8829a4aSRandall Stewart 		}
20047215cc1bSMichael Tuexen 		if (stcb->sctp_ep->def_vrf_id != vrf_id) {
20057215cc1bSMichael Tuexen 			SCTP_INP_RUNLOCK(stcb->sctp_ep);
20067215cc1bSMichael Tuexen 			continue;
20077215cc1bSMichael Tuexen 		}
2008f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2009f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
2010f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag == vtag) {
2011f8829a4aSRandall Stewart 			/* candidate */
2012f8829a4aSRandall Stewart 			if (stcb->rport != rport) {
2013f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
2014f8829a4aSRandall Stewart 				continue;
2015f8829a4aSRandall Stewart 			}
2016f8829a4aSRandall Stewart 			if (stcb->sctp_ep->sctp_lport != lport) {
2017f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
2018f8829a4aSRandall Stewart 				continue;
2019f8829a4aSRandall Stewart 			}
2020fb8fb8f8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2021fb8fb8f8SRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
2022fb8fb8f8SRandall Stewart 				continue;
2023fb8fb8f8SRandall Stewart 			}
2024830d754dSRandall Stewart 			/* RRS:Need toaddr check here */
2025830d754dSRandall Stewart 			if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2026830d754dSRandall Stewart 				/* Endpoint does not own this address */
2027830d754dSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
2028830d754dSRandall Stewart 				continue;
2029830d754dSRandall Stewart 			}
2030830d754dSRandall Stewart 			if (remote_tag) {
2031b7b84c0eSMichael Tuexen 				/*
2032b7b84c0eSMichael Tuexen 				 * If we have both vtags that's all we match
2033b7b84c0eSMichael Tuexen 				 * on
2034b7b84c0eSMichael Tuexen 				 */
2035830d754dSRandall Stewart 				if (stcb->asoc.peer_vtag == remote_tag) {
2036830d754dSRandall Stewart 					/*
2037830d754dSRandall Stewart 					 * If both tags match we consider it
2038830d754dSRandall Stewart 					 * conclusive and check NO
2039830d754dSRandall Stewart 					 * source/destination addresses
2040830d754dSRandall Stewart 					 */
2041830d754dSRandall Stewart 					goto conclusive;
2042830d754dSRandall Stewart 				}
2043830d754dSRandall Stewart 			}
2044f8829a4aSRandall Stewart 			if (skip_src_check) {
2045830d754dSRandall Stewart 		conclusive:
2046830d754dSRandall Stewart 				if (from) {
204760990c0cSMichael Tuexen 					*netp = sctp_findnet(stcb, from);
2048830d754dSRandall Stewart 				} else {
2049f8829a4aSRandall Stewart 					*netp = NULL;	/* unknown */
2050830d754dSRandall Stewart 				}
2051ad81507eSRandall Stewart 				if (inp_p)
2052f8829a4aSRandall Stewart 					*inp_p = stcb->sctp_ep;
2053f8829a4aSRandall Stewart 				SCTP_INP_INFO_RUNLOCK();
2054f8829a4aSRandall Stewart 				return (stcb);
2055f8829a4aSRandall Stewart 			}
2056f8829a4aSRandall Stewart 			net = sctp_findnet(stcb, from);
2057f8829a4aSRandall Stewart 			if (net) {
2058f8829a4aSRandall Stewart 				/* yep its him. */
2059f8829a4aSRandall Stewart 				*netp = net;
2060f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_vtagexpress);
2061f8829a4aSRandall Stewart 				*inp_p = stcb->sctp_ep;
2062f8829a4aSRandall Stewart 				SCTP_INP_INFO_RUNLOCK();
2063f8829a4aSRandall Stewart 				return (stcb);
2064f8829a4aSRandall Stewart 			} else {
2065f8829a4aSRandall Stewart 				/*
2066f8829a4aSRandall Stewart 				 * not him, this should only happen in rare
2067f8829a4aSRandall Stewart 				 * cases so I peg it.
2068f8829a4aSRandall Stewart 				 */
2069f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_vtagbogus);
2070f8829a4aSRandall Stewart 			}
2071f8829a4aSRandall Stewart 		}
2072f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2073f8829a4aSRandall Stewart 	}
2074f8829a4aSRandall Stewart 	SCTP_INP_INFO_RUNLOCK();
2075f8829a4aSRandall Stewart 	return (NULL);
2076f8829a4aSRandall Stewart }
2077f8829a4aSRandall Stewart 
2078f8829a4aSRandall Stewart /*
2079f8829a4aSRandall Stewart  * Find an association with the pointer to the inbound IP packet. This can be
2080f8829a4aSRandall Stewart  * a IPv4 or IPv6 packet.
2081f8829a4aSRandall Stewart  */
2082f8829a4aSRandall Stewart struct sctp_tcb *
sctp_findassociation_addr(struct mbuf * m,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_chunkhdr * ch,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,uint32_t vrf_id)20837215cc1bSMichael Tuexen sctp_findassociation_addr(struct mbuf *m, int offset,
2084b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2085f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch,
2086bff64a4dSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2087f8829a4aSRandall Stewart {
2088f0d44a49SMichael Tuexen 	struct sctp_tcb *stcb;
2089f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
2090f8829a4aSRandall Stewart 
2091830d754dSRandall Stewart 	if (sh->v_tag) {
2092830d754dSRandall Stewart 		/* we only go down this path if vtag is non-zero */
2093f0d44a49SMichael Tuexen 		stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
2094830d754dSRandall Stewart 		    inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2095f0d44a49SMichael Tuexen 		if (stcb) {
2096f0d44a49SMichael Tuexen 			return (stcb);
2097830d754dSRandall Stewart 		}
2098830d754dSRandall Stewart 	}
20990053ed28SMichael Tuexen 
2100f8829a4aSRandall Stewart 	if (inp_p) {
2101f0d44a49SMichael Tuexen 		stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
210279b67faaSMichael Tuexen 		    1, vrf_id);
2103f8829a4aSRandall Stewart 		inp = *inp_p;
2104f8829a4aSRandall Stewart 	} else {
2105f0d44a49SMichael Tuexen 		stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
210679b67faaSMichael Tuexen 		    1, vrf_id);
2107f8829a4aSRandall Stewart 	}
2108f0d44a49SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2109f0d44a49SMichael Tuexen 	if (stcb == NULL && inp) {
2110f8829a4aSRandall Stewart 		/* Found a EP but not this address */
2111f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_INITIATION) ||
2112f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_INITIATION_ACK)) {
2113d06c82f1SRandall Stewart 			/*-
2114f8829a4aSRandall Stewart 			 * special hook, we do NOT return linp or an
2115f8829a4aSRandall Stewart 			 * association that is linked to an existing
2116f8829a4aSRandall Stewart 			 * association that is under the TCP pool (i.e. no
2117f8829a4aSRandall Stewart 			 * listener exists). The endpoint finding routine
2118b1ce21c6SRebecca Cran 			 * will always find a listener before examining the
2119f8829a4aSRandall Stewart 			 * TCP pool.
2120f8829a4aSRandall Stewart 			 */
2121f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2122f8829a4aSRandall Stewart 				if (inp_p) {
2123f8829a4aSRandall Stewart 					*inp_p = NULL;
2124f8829a4aSRandall Stewart 				}
2125f8829a4aSRandall Stewart 				return (NULL);
2126f8829a4aSRandall Stewart 			}
2127f0d44a49SMichael Tuexen 			stcb = sctp_findassociation_special_addr(m,
2128b1754ad1SMichael Tuexen 			    offset, sh, &inp, netp, dst);
2129f8829a4aSRandall Stewart 			if (inp_p != NULL) {
2130f8829a4aSRandall Stewart 				*inp_p = inp;
2131f8829a4aSRandall Stewart 			}
2132f8829a4aSRandall Stewart 		}
2133f8829a4aSRandall Stewart 	}
2134f0d44a49SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2135f0d44a49SMichael Tuexen 	return (stcb);
2136f8829a4aSRandall Stewart }
2137f8829a4aSRandall Stewart 
2138f8829a4aSRandall Stewart /*
2139f8829a4aSRandall Stewart  * lookup an association by an ASCONF lookup address.
2140f8829a4aSRandall Stewart  * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2141f8829a4aSRandall Stewart  */
2142f8829a4aSRandall Stewart struct sctp_tcb *
sctp_findassociation_ep_asconf(struct mbuf * m,int offset,struct sockaddr * dst,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,uint32_t vrf_id)21437215cc1bSMichael Tuexen sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2144b1754ad1SMichael Tuexen     struct sockaddr *dst, struct sctphdr *sh,
2145b1754ad1SMichael Tuexen     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2146f8829a4aSRandall Stewart {
2147f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
214824aaac8dSMichael Tuexen 	union sctp_sockstore remote_store;
2149f4358911SMichael Tuexen 	struct sctp_paramhdr param_buf, *phdr;
2150f8829a4aSRandall Stewart 	int ptype;
2151f8829a4aSRandall Stewart 	int zero_address = 0;
2152e6194c2eSMichael Tuexen #ifdef INET
2153e6194c2eSMichael Tuexen 	struct sockaddr_in *sin;
2154e6194c2eSMichael Tuexen #endif
2155e6194c2eSMichael Tuexen #ifdef INET6
2156e6194c2eSMichael Tuexen 	struct sockaddr_in6 *sin6;
2157e6194c2eSMichael Tuexen #endif
2158f8829a4aSRandall Stewart 
2159f8829a4aSRandall Stewart 	memset(&remote_store, 0, sizeof(remote_store));
2160f8829a4aSRandall Stewart 	phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2161f4358911SMichael Tuexen 	    &param_buf, sizeof(struct sctp_paramhdr));
2162f8829a4aSRandall Stewart 	if (phdr == NULL) {
2163ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
21646e9c45e0SMichael Tuexen 		    __func__);
2165f8829a4aSRandall Stewart 		return NULL;
2166f8829a4aSRandall Stewart 	}
2167f8829a4aSRandall Stewart 	ptype = (int)((uint32_t)ntohs(phdr->param_type));
2168f8829a4aSRandall Stewart 	/* get the correlation address */
21695e2c2d87SRandall Stewart 	switch (ptype) {
21705e2c2d87SRandall Stewart #ifdef INET6
21715e2c2d87SRandall Stewart 	case SCTP_IPV6_ADDRESS:
21725e2c2d87SRandall Stewart 		{
2173f8829a4aSRandall Stewart 			/* ipv6 address param */
2174f8829a4aSRandall Stewart 			struct sctp_ipv6addr_param *p6, p6_buf;
2175f8829a4aSRandall Stewart 
2176f8829a4aSRandall Stewart 			if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2177f8829a4aSRandall Stewart 				return NULL;
2178f8829a4aSRandall Stewart 			}
2179f8829a4aSRandall Stewart 			p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2180f8829a4aSRandall Stewart 			    offset + sizeof(struct sctp_asconf_chunk),
2181f4358911SMichael Tuexen 			    &p6_buf.ph, sizeof(p6_buf));
2182f8829a4aSRandall Stewart 			if (p6 == NULL) {
2183ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
21846e9c45e0SMichael Tuexen 				    __func__);
2185f8829a4aSRandall Stewart 				return (NULL);
2186f8829a4aSRandall Stewart 			}
218724aaac8dSMichael Tuexen 			sin6 = &remote_store.sin6;
2188f8829a4aSRandall Stewart 			sin6->sin6_family = AF_INET6;
2189f8829a4aSRandall Stewart 			sin6->sin6_len = sizeof(*sin6);
2190f8829a4aSRandall Stewart 			sin6->sin6_port = sh->src_port;
2191f8829a4aSRandall Stewart 			memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2192f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2193f8829a4aSRandall Stewart 				zero_address = 1;
21945e2c2d87SRandall Stewart 			break;
21955e2c2d87SRandall Stewart 		}
21965e2c2d87SRandall Stewart #endif
2197e6194c2eSMichael Tuexen #ifdef INET
21985e2c2d87SRandall Stewart 	case SCTP_IPV4_ADDRESS:
21995e2c2d87SRandall Stewart 		{
2200f8829a4aSRandall Stewart 			/* ipv4 address param */
2201f8829a4aSRandall Stewart 			struct sctp_ipv4addr_param *p4, p4_buf;
2202f8829a4aSRandall Stewart 
2203f8829a4aSRandall Stewart 			if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2204f8829a4aSRandall Stewart 				return NULL;
2205f8829a4aSRandall Stewart 			}
2206f8829a4aSRandall Stewart 			p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2207f8829a4aSRandall Stewart 			    offset + sizeof(struct sctp_asconf_chunk),
2208f4358911SMichael Tuexen 			    &p4_buf.ph, sizeof(p4_buf));
2209f8829a4aSRandall Stewart 			if (p4 == NULL) {
2210ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
22116e9c45e0SMichael Tuexen 				    __func__);
2212f8829a4aSRandall Stewart 				return (NULL);
2213f8829a4aSRandall Stewart 			}
221424aaac8dSMichael Tuexen 			sin = &remote_store.sin;
2215f8829a4aSRandall Stewart 			sin->sin_family = AF_INET;
2216f8829a4aSRandall Stewart 			sin->sin_len = sizeof(*sin);
2217f8829a4aSRandall Stewart 			sin->sin_port = sh->src_port;
2218f8829a4aSRandall Stewart 			memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2219f8829a4aSRandall Stewart 			if (sin->sin_addr.s_addr == INADDR_ANY)
2220f8829a4aSRandall Stewart 				zero_address = 1;
22215e2c2d87SRandall Stewart 			break;
22225e2c2d87SRandall Stewart 		}
2223e6194c2eSMichael Tuexen #endif
22245e2c2d87SRandall Stewart 	default:
2225f8829a4aSRandall Stewart 		/* invalid address param type */
2226f8829a4aSRandall Stewart 		return NULL;
2227f8829a4aSRandall Stewart 	}
2228f8829a4aSRandall Stewart 
2229f8829a4aSRandall Stewart 	if (zero_address) {
2230b1754ad1SMichael Tuexen 		stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
2231830d754dSRandall Stewart 		    netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
223263c6726eSMichael Tuexen 		if (stcb != NULL) {
223363c6726eSMichael Tuexen 			SCTP_INP_DECR_REF(*inp_p);
223463c6726eSMichael Tuexen 		}
2235f8829a4aSRandall Stewart 	} else {
2236f8829a4aSRandall Stewart 		stcb = sctp_findassociation_ep_addr(inp_p,
223724aaac8dSMichael Tuexen 		    &remote_store.sa, netp,
2238b1754ad1SMichael Tuexen 		    dst, NULL);
2239f8829a4aSRandall Stewart 	}
2240f8829a4aSRandall Stewart 	return (stcb);
2241f8829a4aSRandall Stewart }
2242f8829a4aSRandall Stewart 
2243f8829a4aSRandall Stewart /*
2244f8829a4aSRandall Stewart  * allocate a sctp_inpcb and setup a temporary binding to a port/all
2245f8829a4aSRandall Stewart  * addresses. This way if we don't get a bind we by default pick a ephemeral
2246f8829a4aSRandall Stewart  * port with all addresses bound.
2247f8829a4aSRandall Stewart  */
2248f8829a4aSRandall Stewart int
sctp_inpcb_alloc(struct socket * so,uint32_t vrf_id)2249f4c93d24SRandall Stewart sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2250f8829a4aSRandall Stewart {
2251f8829a4aSRandall Stewart 	/*
2252f8829a4aSRandall Stewart 	 * we get called when a new endpoint starts up. We need to allocate
2253f8829a4aSRandall Stewart 	 * the sctp_inpcb structure from the zone and init it. Mark it as
2254f8829a4aSRandall Stewart 	 * unbound and find a port that we can use as an ephemeral with
2255f8829a4aSRandall Stewart 	 * INADDR_ANY. If the user binds later no problem we can then add in
2256f8829a4aSRandall Stewart 	 * the specific addresses. And setup the default parameters for the
2257f8829a4aSRandall Stewart 	 * EP.
2258f8829a4aSRandall Stewart 	 */
2259f8829a4aSRandall Stewart 	int i, error;
2260f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
2261f8829a4aSRandall Stewart 	struct sctp_pcb *m;
2262f8829a4aSRandall Stewart 	struct timeval time;
2263f8829a4aSRandall Stewart 	sctp_sharedkey_t *null_key;
2264f8829a4aSRandall Stewart 
2265f8829a4aSRandall Stewart 	error = 0;
2266f8829a4aSRandall Stewart 
2267f8829a4aSRandall Stewart 	SCTP_INP_INFO_WLOCK();
2268b3f1ea41SRandall Stewart 	inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2269f8829a4aSRandall Stewart 	if (inp == NULL) {
2270ad81507eSRandall Stewart 		SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2271f8829a4aSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
2272c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2273f8829a4aSRandall Stewart 		return (ENOBUFS);
2274f8829a4aSRandall Stewart 	}
2275f8829a4aSRandall Stewart 	/* zap it */
22765ba7f91fSMichael Tuexen 	memset(inp, 0, sizeof(*inp));
2277f8829a4aSRandall Stewart 
2278f8829a4aSRandall Stewart 	/* bump generations */
2279f8829a4aSRandall Stewart 	/* setup socket pointers */
2280f8829a4aSRandall Stewart 	inp->sctp_socket = so;
2281f8829a4aSRandall Stewart 	inp->ip_inp.inp.inp_socket = so;
22826ba22f19SMichael Tuexen 	inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
228358bdb691SMichael Tuexen #ifdef INET6
22843457ccdaSMichael Tuexen 	if (INP_SOCKAF(so) == AF_INET6) {
228558bdb691SMichael Tuexen 		if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
228658bdb691SMichael Tuexen 			inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
228758bdb691SMichael Tuexen 		}
22883457ccdaSMichael Tuexen 		if (MODULE_GLOBAL(ip6_v6only)) {
22893457ccdaSMichael Tuexen 			inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
22903457ccdaSMichael Tuexen 		}
22913457ccdaSMichael Tuexen 	}
229258bdb691SMichael Tuexen #endif
2293830d754dSRandall Stewart 	inp->sctp_associd_counter = 1;
229462c1ff9cSRandall Stewart 	inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2295989453daSMichael Tuexen 	inp->sctp_frag_point = 0;
229659b6d5beSMichael Tuexen 	inp->max_cwnd = 0;
229720083c2eSMichael Tuexen 	inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2298f342355aSMichael Tuexen 	inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
2299dd973b0eSMichael Tuexen 	inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
230097a0ca5bSMichael Tuexen 	inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
2301c79bec9cSMichael Tuexen 	inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
2302317e00efSMichael Tuexen 	inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
2303caea9879SMichael Tuexen 	inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
2304cb9b8e6fSMichael Tuexen 	inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
230544249214SRandall Stewart 	inp->idata_supported = 0;
230652640d61SMichael Tuexen 	inp->rcv_edmid = SCTP_EDMID_NONE;
230744249214SRandall Stewart 
2308d089f9b9SMichael Tuexen 	inp->fibnum = so->so_fibnum;
2309830d754dSRandall Stewart 	/* init the small hash table we use to track asocid <-> tcb */
2310830d754dSRandall Stewart 	inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2311830d754dSRandall Stewart 	if (inp->sctp_asocidhash == NULL) {
23126ba22f19SMichael Tuexen 		crfree(inp->ip_inp.inp.inp_cred);
2313830d754dSRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2314830d754dSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
23152c6b25b4SRandall Stewart 		return (ENOBUFS);
2316830d754dSRandall Stewart 	}
2317f8829a4aSRandall Stewart 	SCTP_INCR_EP_COUNT();
2318482444b4SRandall Stewart 	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2319f8829a4aSRandall Stewart 	SCTP_INP_INFO_WUNLOCK();
2320f8829a4aSRandall Stewart 
2321f8829a4aSRandall Stewart 	so->so_pcb = (caddr_t)inp;
2322f8829a4aSRandall Stewart 
23231a3b5ce2SMichael Tuexen 	if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2324f8829a4aSRandall Stewart 		/* UDP style socket */
2325f8829a4aSRandall Stewart 		inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2326f8829a4aSRandall Stewart 		    SCTP_PCB_FLAGS_UNBOUND);
2327f8829a4aSRandall Stewart 		/* Be sure it is NON-BLOCKING IO for UDP */
232842551e99SRandall Stewart 		/* SCTP_SET_SO_NBIO(so); */
232942551e99SRandall Stewart 	} else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2330f8829a4aSRandall Stewart 		/* TCP style socket */
2331f8829a4aSRandall Stewart 		inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2332f8829a4aSRandall Stewart 		    SCTP_PCB_FLAGS_UNBOUND);
2333f8829a4aSRandall Stewart 		/* Be sure we have blocking IO by default */
2334afb908daSMichael Tuexen 		SOCK_LOCK(so);
233542551e99SRandall Stewart 		SCTP_CLEAR_SO_NBIO(so);
2336afb908daSMichael Tuexen 		SOCK_UNLOCK(so);
2337f8829a4aSRandall Stewart 	} else {
2338f8829a4aSRandall Stewart 		/*
2339f8829a4aSRandall Stewart 		 * unsupported socket type (RAW, etc)- in case we missed it
2340f8829a4aSRandall Stewart 		 * in protosw
2341f8829a4aSRandall Stewart 		 */
2342c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
234341291ef0SRandall Stewart 		so->so_pcb = NULL;
23446ba22f19SMichael Tuexen 		crfree(inp->ip_inp.inp.inp_cred);
2345b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2346f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
2347f8829a4aSRandall Stewart 	}
2348b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2349d61a0ae0SRandall Stewart 		sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2350c4739e2fSRandall Stewart 		sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2351b3f1ea41SRandall Stewart 	} else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2352c4739e2fSRandall Stewart 		sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2353c4739e2fSRandall Stewart 		sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2354b3f1ea41SRandall Stewart 	} else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2355c4739e2fSRandall Stewart 		sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2356c4739e2fSRandall Stewart 		sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2357c4739e2fSRandall Stewart 	}
2358b3f1ea41SRandall Stewart 	inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2359f8829a4aSRandall Stewart 	    &inp->sctp_hashmark);
2360f8829a4aSRandall Stewart 	if (inp->sctp_tcbhash == NULL) {
2361ad81507eSRandall Stewart 		SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2362c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
236341291ef0SRandall Stewart 		so->so_pcb = NULL;
23646ba22f19SMichael Tuexen 		crfree(inp->ip_inp.inp.inp_cred);
2365b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2366f8829a4aSRandall Stewart 		return (ENOBUFS);
2367f8829a4aSRandall Stewart 	}
2368f4c93d24SRandall Stewart 	inp->def_vrf_id = vrf_id;
236942551e99SRandall Stewart 
2370f8829a4aSRandall Stewart 	SCTP_INP_INFO_WLOCK();
2371f8829a4aSRandall Stewart 	SCTP_INP_LOCK_INIT(inp);
2372afad340aSGleb Smirnoff 	rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp",
2373afad340aSGleb Smirnoff 	    RW_RECURSE | RW_DUPOK);
23746cb8b3b5SMichael Tuexen 	SCTP_INP_READ_LOCK_INIT(inp);
2375f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK_INIT(inp);
2376f8829a4aSRandall Stewart 	/* lock the new ep */
2377f8829a4aSRandall Stewart 	SCTP_INP_WLOCK(inp);
2378f8829a4aSRandall Stewart 
2379f8829a4aSRandall Stewart 	/* add it to the info area */
2380b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2381f8829a4aSRandall Stewart 	SCTP_INP_INFO_WUNLOCK();
2382f8829a4aSRandall Stewart 
2383f8829a4aSRandall Stewart 	TAILQ_INIT(&inp->read_queue);
2384f8829a4aSRandall Stewart 	LIST_INIT(&inp->sctp_addr_list);
238542551e99SRandall Stewart 
2386f8829a4aSRandall Stewart 	LIST_INIT(&inp->sctp_asoc_list);
2387f8829a4aSRandall Stewart 
2388a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS
2389a5d547adSRandall Stewart 	/* TEMP CODE */
2390a5d547adSRandall Stewart 	LIST_INIT(&inp->sctp_asoc_free_list);
2391a5d547adSRandall Stewart #endif
2392f8829a4aSRandall Stewart 	/* Init the timer structure for signature change */
2393139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2394f8829a4aSRandall Stewart 	inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2395f8829a4aSRandall Stewart 
2396f8829a4aSRandall Stewart 	/* now init the actual endpoint default data */
2397f8829a4aSRandall Stewart 	m = &inp->sctp_ep;
2398f8829a4aSRandall Stewart 
2399f8829a4aSRandall Stewart 	/* setup the base timeout information */
240025ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC);	/* needed ? */
240125ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC);	/* needed ? */
240225ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
240325ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
240425ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
240525ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
240625ec3553SMichael Tuexen 	m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2407f8829a4aSRandall Stewart 	/* all max/min max are in ms */
2408b3f1ea41SRandall Stewart 	m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2409b3f1ea41SRandall Stewart 	m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2410b3f1ea41SRandall Stewart 	m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2411b3f1ea41SRandall Stewart 	m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2412b3f1ea41SRandall Stewart 	m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2413b3f1ea41SRandall Stewart 	m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2414b3f1ea41SRandall Stewart 	m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2415b3f1ea41SRandall Stewart 	m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2416ca85e948SMichael Tuexen 	m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2417f8829a4aSRandall Stewart 	m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2418f8829a4aSRandall Stewart 	m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2419b3f1ea41SRandall Stewart 	m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2420899288aeSRandall Stewart 	m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2421899288aeSRandall Stewart 
2422b3f1ea41SRandall Stewart 	m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2423f7a77f6fSMichael Tuexen 	m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2424ee1ccd92SMichael Tuexen 	m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
2425f8829a4aSRandall Stewart 	/* number of streams to pre-open on a association */
2426b3f1ea41SRandall Stewart 	m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2427f8829a4aSRandall Stewart 
242828a6addeSMichael Tuexen 	m->default_mtu = 0;
2429f8829a4aSRandall Stewart 	/* Add adaptation cookie */
24302416af26SMichael Tuexen 	m->adaptation_layer_indicator = 0;
24312416af26SMichael Tuexen 	m->adaptation_layer_indicator_provided = 0;
2432f8829a4aSRandall Stewart 
2433f8829a4aSRandall Stewart 	/* seed random number generator */
2434f8829a4aSRandall Stewart 	m->random_counter = 1;
2435f8829a4aSRandall Stewart 	m->store_at = SCTP_SIGNATURE_SIZE;
2436139bc87fSRandall Stewart 	SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2437f8829a4aSRandall Stewart 	sctp_fill_random_store(m);
2438f8829a4aSRandall Stewart 
2439f8829a4aSRandall Stewart 	/* Minimum cookie size */
2440f8829a4aSRandall Stewart 	m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2441f8829a4aSRandall Stewart 	    sizeof(struct sctp_state_cookie);
2442f8829a4aSRandall Stewart 	m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2443f8829a4aSRandall Stewart 
2444f8829a4aSRandall Stewart 	/* Setup the initial secret */
24456e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&time);
24469d8a3718SMichael Tuexen 	m->time_of_secret_change = time.tv_sec;
2447f8829a4aSRandall Stewart 
2448f8829a4aSRandall Stewart 	for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2449f8829a4aSRandall Stewart 		m->secret_key[0][i] = sctp_select_initial_TSN(m);
2450f8829a4aSRandall Stewart 	}
2451f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2452f8829a4aSRandall Stewart 
2453f8829a4aSRandall Stewart 	/* How long is a cookie good for ? */
245425ec3553SMichael Tuexen 	m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2455f8829a4aSRandall Stewart 	/*
2456f8829a4aSRandall Stewart 	 * Initialize authentication parameters
2457f8829a4aSRandall Stewart 	 */
2458f8829a4aSRandall Stewart 	m->local_hmacs = sctp_default_supported_hmaclist();
2459f8829a4aSRandall Stewart 	m->local_auth_chunks = sctp_alloc_chunklist();
2460c79bec9cSMichael Tuexen 	if (inp->asconf_supported) {
2461c79bec9cSMichael Tuexen 		sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
2462c79bec9cSMichael Tuexen 		sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
2463c79bec9cSMichael Tuexen 	}
246458bdb691SMichael Tuexen 	m->default_dscp = 0;
246558bdb691SMichael Tuexen #ifdef INET6
246658bdb691SMichael Tuexen 	m->default_flowlabel = 0;
246758bdb691SMichael Tuexen #endif
2468c9c58059SMichael Tuexen 	m->port = 0;		/* encapsulation disabled by default */
2469f8829a4aSRandall Stewart 	LIST_INIT(&m->shared_keys);
2470f8829a4aSRandall Stewart 	/* add default NULL key as key id 0 */
2471f8829a4aSRandall Stewart 	null_key = sctp_alloc_sharedkey();
2472f8829a4aSRandall Stewart 	sctp_insert_sharedkey(&m->shared_keys, null_key);
2473f8829a4aSRandall Stewart 	SCTP_INP_WUNLOCK(inp);
2474f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
2475f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 12);
2476f8829a4aSRandall Stewart #endif
2477f8829a4aSRandall Stewart 	return (error);
2478f8829a4aSRandall Stewart }
2479f8829a4aSRandall Stewart 
2480f8829a4aSRandall Stewart void
sctp_move_pcb_and_assoc(struct sctp_inpcb * old_inp,struct sctp_inpcb * new_inp,struct sctp_tcb * stcb)2481f8829a4aSRandall Stewart sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2482f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2483f8829a4aSRandall Stewart {
2484f8829a4aSRandall Stewart 	struct sctp_nets *net;
2485f8829a4aSRandall Stewart 	uint16_t lport, rport;
2486f8829a4aSRandall Stewart 	struct sctppcbhead *head;
2487f8829a4aSRandall Stewart 	struct sctp_laddr *laddr, *oladdr;
2488f8829a4aSRandall Stewart 
24892afb3e84SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
2490f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
2491f8829a4aSRandall Stewart 	SCTP_INP_INFO_WLOCK();
2492f8829a4aSRandall Stewart 	SCTP_INP_WLOCK(old_inp);
2493f8829a4aSRandall Stewart 	SCTP_INP_WLOCK(new_inp);
2494f8829a4aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
24952afb3e84SRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
2496f8829a4aSRandall Stewart 
249754912d47SMichael Tuexen #ifdef INET6
2498d79676fbSMichael Tuexen 	if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2499d79676fbSMichael Tuexen 		new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
2500d79676fbSMichael Tuexen 		if (old_inp->ip_inp.inp.in6p_outputopts) {
2501d79676fbSMichael Tuexen 			new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
2502d79676fbSMichael Tuexen 		}
250354912d47SMichael Tuexen 	}
250454912d47SMichael Tuexen #endif
250554912d47SMichael Tuexen #if defined(INET) && defined(INET6)
250654912d47SMichael Tuexen 	else
250754912d47SMichael Tuexen #endif
250854912d47SMichael Tuexen #ifdef INET
250954912d47SMichael Tuexen 	{
2510d79676fbSMichael Tuexen 		new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
2511d79676fbSMichael Tuexen 		new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
2512d79676fbSMichael Tuexen 	}
251354912d47SMichael Tuexen #endif
2514f8829a4aSRandall Stewart 	new_inp->sctp_ep.time_of_secret_change =
2515f8829a4aSRandall Stewart 	    old_inp->sctp_ep.time_of_secret_change;
2516f8829a4aSRandall Stewart 	memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2517f8829a4aSRandall Stewart 	    sizeof(old_inp->sctp_ep.secret_key));
2518f8829a4aSRandall Stewart 	new_inp->sctp_ep.current_secret_number =
2519f8829a4aSRandall Stewart 	    old_inp->sctp_ep.current_secret_number;
2520f8829a4aSRandall Stewart 	new_inp->sctp_ep.last_secret_number =
2521f8829a4aSRandall Stewart 	    old_inp->sctp_ep.last_secret_number;
2522f8829a4aSRandall Stewart 	new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2523f8829a4aSRandall Stewart 
2524f8829a4aSRandall Stewart 	/* make it so new data pours into the new socket */
2525f8829a4aSRandall Stewart 	stcb->sctp_socket = new_inp->sctp_socket;
2526f8829a4aSRandall Stewart 	stcb->sctp_ep = new_inp;
2527f8829a4aSRandall Stewart 
2528f8829a4aSRandall Stewart 	/* Copy the port across */
2529f8829a4aSRandall Stewart 	lport = new_inp->sctp_lport = old_inp->sctp_lport;
2530f8829a4aSRandall Stewart 	rport = stcb->rport;
2531f8829a4aSRandall Stewart 	/* Pull the tcb from the old association */
2532f8829a4aSRandall Stewart 	LIST_REMOVE(stcb, sctp_tcbhash);
2533f8829a4aSRandall Stewart 	LIST_REMOVE(stcb, sctp_tcblist);
2534a99b6783SRandall Stewart 	if (stcb->asoc.in_asocid_hash) {
2535a99b6783SRandall Stewart 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
2536a99b6783SRandall Stewart 	}
2537f8829a4aSRandall Stewart 	/* Now insert the new_inp into the TCP connected hash */
2538034b88b0SMichael Tuexen 	head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2539f8829a4aSRandall Stewart 
2540f8829a4aSRandall Stewart 	LIST_INSERT_HEAD(head, new_inp, sctp_hash);
254193164cf9SRandall Stewart 	/* Its safe to access */
254293164cf9SRandall Stewart 	new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2543f8829a4aSRandall Stewart 
2544f8829a4aSRandall Stewart 	/* Now move the tcb into the endpoint list */
2545f8829a4aSRandall Stewart 	LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2546f8829a4aSRandall Stewart 	/*
2547f8829a4aSRandall Stewart 	 * Question, do we even need to worry about the ep-hash since we
2548f8829a4aSRandall Stewart 	 * only have one connection? Probably not :> so lets get rid of it
2549f8829a4aSRandall Stewart 	 * and not suck up any kernel memory in that.
2550f8829a4aSRandall Stewart 	 */
2551a99b6783SRandall Stewart 	if (stcb->asoc.in_asocid_hash) {
2552a99b6783SRandall Stewart 		struct sctpasochead *lhd;
2553f8829a4aSRandall Stewart 
2554a99b6783SRandall Stewart 		lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2555a99b6783SRandall Stewart 		    new_inp->hashasocidmark)];
2556a99b6783SRandall Stewart 		LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2557a99b6783SRandall Stewart 	}
2558f8829a4aSRandall Stewart 	/* Ok. Let's restart timer. */
2559f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2560f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2561f8829a4aSRandall Stewart 		    stcb, net);
2562f8829a4aSRandall Stewart 	}
2563f8829a4aSRandall Stewart 
2564f8829a4aSRandall Stewart 	SCTP_INP_INFO_WUNLOCK();
2565f8829a4aSRandall Stewart 	if (new_inp->sctp_tcbhash != NULL) {
256644b7479bSRandall Stewart 		SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2567f8829a4aSRandall Stewart 		new_inp->sctp_tcbhash = NULL;
2568f8829a4aSRandall Stewart 	}
2569f8829a4aSRandall Stewart 	if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2570f8829a4aSRandall Stewart 		/* Subset bound, so copy in the laddr list from the old_inp */
2571f8829a4aSRandall Stewart 		LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2572b3f1ea41SRandall Stewart 			laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2573f8829a4aSRandall Stewart 			if (laddr == NULL) {
2574f8829a4aSRandall Stewart 				/*
2575f8829a4aSRandall Stewart 				 * Gak, what can we do? This assoc is really
2576f8829a4aSRandall Stewart 				 * HOSED. We probably should send an abort
2577f8829a4aSRandall Stewart 				 * here.
2578f8829a4aSRandall Stewart 				 */
2579ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2580f8829a4aSRandall Stewart 				continue;
2581f8829a4aSRandall Stewart 			}
2582f8829a4aSRandall Stewart 			SCTP_INCR_LADDR_COUNT();
25835ba7f91fSMichael Tuexen 			memset(laddr, 0, sizeof(*laddr));
2584d61a0ae0SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2585f8829a4aSRandall Stewart 			laddr->ifa = oladdr->ifa;
2586bff64a4dSRandall Stewart 			atomic_add_int(&laddr->ifa->refcount, 1);
2587f8829a4aSRandall Stewart 			LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2588f8829a4aSRandall Stewart 			    sctp_nxt_addr);
2589f8829a4aSRandall Stewart 			new_inp->laddr_count++;
25903d2443ccSMichael Tuexen 			if (oladdr == stcb->asoc.last_used_address) {
25913d2443ccSMichael Tuexen 				stcb->asoc.last_used_address = laddr;
25923d2443ccSMichael Tuexen 			}
2593f8829a4aSRandall Stewart 		}
2594f8829a4aSRandall Stewart 	}
25958745f898SMichael Tuexen 	/* Now any running timers need to be adjusted. */
25968745f898SMichael Tuexen 	if (stcb->asoc.dack_timer.ep == old_inp) {
25978745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
25988745f898SMichael Tuexen 		stcb->asoc.dack_timer.ep = new_inp;
25998745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26008745f898SMichael Tuexen 	}
26018745f898SMichael Tuexen 	if (stcb->asoc.asconf_timer.ep == old_inp) {
26028745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
26038745f898SMichael Tuexen 		stcb->asoc.asconf_timer.ep = new_inp;
26048745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26058745f898SMichael Tuexen 	}
26068745f898SMichael Tuexen 	if (stcb->asoc.strreset_timer.ep == old_inp) {
26078745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
26088745f898SMichael Tuexen 		stcb->asoc.strreset_timer.ep = new_inp;
26098745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26108745f898SMichael Tuexen 	}
26118745f898SMichael Tuexen 	if (stcb->asoc.shut_guard_timer.ep == old_inp) {
26128745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
26138745f898SMichael Tuexen 		stcb->asoc.shut_guard_timer.ep = new_inp;
26148745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26158745f898SMichael Tuexen 	}
26168745f898SMichael Tuexen 	if (stcb->asoc.autoclose_timer.ep == old_inp) {
26178745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
26188745f898SMichael Tuexen 		stcb->asoc.autoclose_timer.ep = new_inp;
26198745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26208745f898SMichael Tuexen 	}
26218745f898SMichael Tuexen 	if (stcb->asoc.delete_prim_timer.ep == old_inp) {
26228745f898SMichael Tuexen 		SCTP_INP_DECR_REF(old_inp);
26238745f898SMichael Tuexen 		stcb->asoc.delete_prim_timer.ep = new_inp;
26248745f898SMichael Tuexen 		SCTP_INP_INCR_REF(new_inp);
26258745f898SMichael Tuexen 	}
2626f8829a4aSRandall Stewart 	/* now what about the nets? */
2627f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
26288745f898SMichael Tuexen 		if (net->pmtu_timer.ep == old_inp) {
26298745f898SMichael Tuexen 			SCTP_INP_DECR_REF(old_inp);
26308745f898SMichael Tuexen 			net->pmtu_timer.ep = new_inp;
26318745f898SMichael Tuexen 			SCTP_INP_INCR_REF(new_inp);
26328745f898SMichael Tuexen 		}
26338745f898SMichael Tuexen 		if (net->hb_timer.ep == old_inp) {
26348745f898SMichael Tuexen 			SCTP_INP_DECR_REF(old_inp);
26358745f898SMichael Tuexen 			net->hb_timer.ep = new_inp;
26368745f898SMichael Tuexen 			SCTP_INP_INCR_REF(new_inp);
26378745f898SMichael Tuexen 		}
26388745f898SMichael Tuexen 		if (net->rxt_timer.ep == old_inp) {
26398745f898SMichael Tuexen 			SCTP_INP_DECR_REF(old_inp);
26408745f898SMichael Tuexen 			net->rxt_timer.ep = new_inp;
26418745f898SMichael Tuexen 			SCTP_INP_INCR_REF(new_inp);
26428745f898SMichael Tuexen 		}
2643f8829a4aSRandall Stewart 	}
2644f8829a4aSRandall Stewart 	SCTP_INP_WUNLOCK(new_inp);
2645f8829a4aSRandall Stewart 	SCTP_INP_WUNLOCK(old_inp);
2646f8829a4aSRandall Stewart }
2647f8829a4aSRandall Stewart 
26482b1c7de4SMichael Tuexen /*
26492b1c7de4SMichael Tuexen  * insert an laddr entry with the given ifa for the desired list
26502b1c7de4SMichael Tuexen  */
26512b1c7de4SMichael Tuexen static int
sctp_insert_laddr(struct sctpladdr * list,struct sctp_ifa * ifa,uint32_t act)26522b1c7de4SMichael Tuexen sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
26532b1c7de4SMichael Tuexen {
26542b1c7de4SMichael Tuexen 	struct sctp_laddr *laddr;
2655f8829a4aSRandall Stewart 
26562b1c7de4SMichael Tuexen 	laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
26572b1c7de4SMichael Tuexen 	if (laddr == NULL) {
26582b1c7de4SMichael Tuexen 		/* out of memory? */
26592b1c7de4SMichael Tuexen 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
26602b1c7de4SMichael Tuexen 		return (EINVAL);
26612b1c7de4SMichael Tuexen 	}
26622b1c7de4SMichael Tuexen 	SCTP_INCR_LADDR_COUNT();
26635ba7f91fSMichael Tuexen 	memset(laddr, 0, sizeof(*laddr));
26642b1c7de4SMichael Tuexen 	(void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
26652b1c7de4SMichael Tuexen 	laddr->ifa = ifa;
26662b1c7de4SMichael Tuexen 	laddr->action = act;
26672b1c7de4SMichael Tuexen 	atomic_add_int(&ifa->refcount, 1);
26682b1c7de4SMichael Tuexen 	/* insert it */
26692b1c7de4SMichael Tuexen 	LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
2670f8829a4aSRandall Stewart 
26712b1c7de4SMichael Tuexen 	return (0);
26722b1c7de4SMichael Tuexen }
26732b1c7de4SMichael Tuexen 
26742b1c7de4SMichael Tuexen /*
26752b1c7de4SMichael Tuexen  * Remove an laddr entry from the local address list (on an assoc)
26762b1c7de4SMichael Tuexen  */
26772b1c7de4SMichael Tuexen static void
sctp_remove_laddr(struct sctp_laddr * laddr)26782b1c7de4SMichael Tuexen sctp_remove_laddr(struct sctp_laddr *laddr)
26792b1c7de4SMichael Tuexen {
26802b1c7de4SMichael Tuexen 
26812b1c7de4SMichael Tuexen 	/* remove from the list */
26822b1c7de4SMichael Tuexen 	LIST_REMOVE(laddr, sctp_nxt_addr);
26832b1c7de4SMichael Tuexen 	sctp_free_ifa(laddr->ifa);
26842b1c7de4SMichael Tuexen 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
26852b1c7de4SMichael Tuexen 	SCTP_DECR_LADDR_COUNT();
26862b1c7de4SMichael Tuexen }
2687f8829a4aSRandall Stewart 
2688457abbb8SMark Johnston /*
2689457abbb8SMark Johnston  * Bind the socket, with the PCB and global info locks held.  Note, if a
2690457abbb8SMark Johnston  * socket address is specified, the PCB lock may be dropped and re-acquired.
2691457abbb8SMark Johnston  *
2692457abbb8SMark Johnston  * sctp_ifap is used to bypass normal local address validation checks.
2693457abbb8SMark Johnston  */
2694f8829a4aSRandall Stewart int
sctp_inpcb_bind_locked(struct sctp_inpcb * inp,struct sockaddr * addr,struct sctp_ifa * sctp_ifap,struct thread * td)2695457abbb8SMark Johnston sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
26960d29e4bcSMark Johnston     struct sctp_ifa *sctp_ifap, struct thread *td)
2697f8829a4aSRandall Stewart {
2698f8829a4aSRandall Stewart 	/* bind a ep to a socket address */
2699f8829a4aSRandall Stewart 	struct sctppcbhead *head;
2700457abbb8SMark Johnston 	struct sctp_inpcb *inp_tmp;
2701f8829a4aSRandall Stewart 	struct inpcb *ip_inp;
27026d9e8f2bSRandall Stewart 	int port_reuse_active = 0;
2703f8829a4aSRandall Stewart 	int bindall;
2704f8829a4aSRandall Stewart 	uint16_t lport;
2705f8829a4aSRandall Stewart 	int error;
270642551e99SRandall Stewart 	uint32_t vrf_id;
2707f8829a4aSRandall Stewart 
27080d29e4bcSMark Johnston 	KASSERT(td != NULL, ("%s: null thread", __func__));
27090d29e4bcSMark Johnston 
27104a36122bSMark Johnston 	error = 0;
2711f8829a4aSRandall Stewart 	lport = 0;
2712f8829a4aSRandall Stewart 	bindall = 1;
2713457abbb8SMark Johnston 	ip_inp = &inp->ip_inp.inp;
2714457abbb8SMark Johnston 
2715457abbb8SMark Johnston 	SCTP_INP_INFO_WLOCK_ASSERT();
2716457abbb8SMark Johnston 	SCTP_INP_WLOCK_ASSERT(inp);
2717457abbb8SMark Johnston 
2718f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
2719f8829a4aSRandall Stewart 	if (addr) {
2720ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
2721f8829a4aSRandall Stewart 		    ntohs(((struct sockaddr_in *)addr)->sin_port));
2722ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
2723ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2724f8829a4aSRandall Stewart 	}
2725ad81507eSRandall Stewart #endif
2726f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
27274a36122bSMark Johnston 		error = EINVAL;
2728f8829a4aSRandall Stewart 		/* already did a bind, subsequent binds NOT allowed ! */
27294a36122bSMark Johnston 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27304a36122bSMark Johnston 		goto out;
2731f8829a4aSRandall Stewart 	}
2732f8829a4aSRandall Stewart 	if (addr != NULL) {
27335e2c2d87SRandall Stewart 		switch (addr->sa_family) {
2734e6194c2eSMichael Tuexen #ifdef INET
27355e2c2d87SRandall Stewart 		case AF_INET:
27365e2c2d87SRandall Stewart 			{
2737f8829a4aSRandall Stewart 				struct sockaddr_in *sin;
2738f8829a4aSRandall Stewart 
2739f8829a4aSRandall Stewart 				/* IPV6_V6ONLY socket? */
274094962f6bSMichael Tuexen 				if (SCTP_IPV6_V6ONLY(inp)) {
27414a36122bSMark Johnston 					error = EINVAL;
27424a36122bSMark Johnston 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27434a36122bSMark Johnston 					goto out;
2744f8829a4aSRandall Stewart 				}
2745c4739e2fSRandall Stewart 				if (addr->sa_len != sizeof(*sin)) {
27464a36122bSMark Johnston 					error = EINVAL;
27474a36122bSMark Johnston 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27484a36122bSMark Johnston 					goto out;
2749c4739e2fSRandall Stewart 				}
27500053ed28SMichael Tuexen 
2751f8829a4aSRandall Stewart 				sin = (struct sockaddr_in *)addr;
2752f8829a4aSRandall Stewart 				lport = sin->sin_port;
275318e198d3SRandall Stewart 				/*
2754830d754dSRandall Stewart 				 * For LOOPBACK the prison_local_ip4() call
2755830d754dSRandall Stewart 				 * will transmute the ip address to the
2756830d754dSRandall Stewart 				 * proper value.
275718e198d3SRandall Stewart 				 */
27580d29e4bcSMark Johnston 				if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
2759c3b8c73cSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27604a36122bSMark Johnston 					goto out;
276118e198d3SRandall Stewart 				}
2762f8829a4aSRandall Stewart 				if (sin->sin_addr.s_addr != INADDR_ANY) {
2763f8829a4aSRandall Stewart 					bindall = 0;
2764f8829a4aSRandall Stewart 				}
27655e2c2d87SRandall Stewart 				break;
27665e2c2d87SRandall Stewart 			}
2767e6194c2eSMichael Tuexen #endif
27685e2c2d87SRandall Stewart #ifdef INET6
27695e2c2d87SRandall Stewart 		case AF_INET6:
27705e2c2d87SRandall Stewart 			{
2771b7b84c0eSMichael Tuexen 				/*
2772b7b84c0eSMichael Tuexen 				 * Only for pure IPv6 Address. (No IPv4
2773b7b84c0eSMichael Tuexen 				 * Mapped!)
2774b7b84c0eSMichael Tuexen 				 */
2775f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin6;
2776f8829a4aSRandall Stewart 
2777f8829a4aSRandall Stewart 				sin6 = (struct sockaddr_in6 *)addr;
2778c4739e2fSRandall Stewart 				if (addr->sa_len != sizeof(*sin6)) {
27794a36122bSMark Johnston 					error = EINVAL;
27804a36122bSMark Johnston 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27814a36122bSMark Johnston 					goto out;
2782c4739e2fSRandall Stewart 				}
2783f8829a4aSRandall Stewart 				lport = sin6->sin6_port;
278418e198d3SRandall Stewart 				/*
2785830d754dSRandall Stewart 				 * For LOOPBACK the prison_local_ip6() call
2786830d754dSRandall Stewart 				 * will transmute the ipv6 address to the
2787830d754dSRandall Stewart 				 * proper value.
278818e198d3SRandall Stewart 				 */
27890d29e4bcSMark Johnston 				if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
2790c3b8c73cSRandall Stewart 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2791c3b8c73cSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
27924a36122bSMark Johnston 					goto out;
2793413628a7SBjoern A. Zeeb 				}
2794f8829a4aSRandall Stewart 				if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2795f8829a4aSRandall Stewart 					bindall = 0;
2796f8829a4aSRandall Stewart 					/* KAME hack: embed scopeid */
2797482444b4SRandall Stewart 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
27984a36122bSMark Johnston 						error = EINVAL;
27994a36122bSMark Johnston 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
28004a36122bSMark Johnston 						goto out;
2801f8829a4aSRandall Stewart 					}
2802c4739e2fSRandall Stewart 				}
2803f8829a4aSRandall Stewart 				/* this must be cleared for ifa_ifwithaddr() */
2804f8829a4aSRandall Stewart 				sin6->sin6_scope_id = 0;
28055e2c2d87SRandall Stewart 				break;
28065e2c2d87SRandall Stewart 			}
28075e2c2d87SRandall Stewart #endif
28085e2c2d87SRandall Stewart 		default:
28094a36122bSMark Johnston 			error = EAFNOSUPPORT;
28104a36122bSMark Johnston 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
28114a36122bSMark Johnston 			goto out;
2812f8829a4aSRandall Stewart 		}
2813f8829a4aSRandall Stewart 	}
28143c503c28SRandall Stewart 	/* Setup a vrf_id to be the default for the non-bind-all case. */
281542551e99SRandall Stewart 	vrf_id = inp->def_vrf_id;
281642551e99SRandall Stewart 
281793908fceSMark Johnston 	if (lport) {
2818f8829a4aSRandall Stewart 		/*
28198225a9bcSMichael Tuexen 		 * Did the caller specify a port? if so we must see if an ep
2820f8829a4aSRandall Stewart 		 * already has this one bound.
2821f8829a4aSRandall Stewart 		 */
2822f8829a4aSRandall Stewart 		/* got to be root to get at low ports */
2823457abbb8SMark Johnston 		if (ntohs(lport) < IPPORT_RESERVED &&
2824457abbb8SMark Johnston 		    (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
28254a36122bSMark Johnston 			goto out;
2826f8829a4aSRandall Stewart 		}
2827457abbb8SMark Johnston 		SCTP_INP_INCR_REF(inp);
2828f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
282942551e99SRandall Stewart 		if (bindall) {
283042551e99SRandall Stewart 			vrf_id = inp->def_vrf_id;
283142551e99SRandall Stewart 			inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2832f8829a4aSRandall Stewart 			if (inp_tmp != NULL) {
2833f8829a4aSRandall Stewart 				/*
283442551e99SRandall Stewart 				 * lock guy returned and lower count note
283542551e99SRandall Stewart 				 * that we are not bound so inp_tmp should
283642551e99SRandall Stewart 				 * NEVER be inp. And it is this inp
283742551e99SRandall Stewart 				 * (inp_tmp) that gets the reference bump,
283842551e99SRandall Stewart 				 * so we must lower it.
2839f8829a4aSRandall Stewart 				 */
2840f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp_tmp);
2841f8829a4aSRandall Stewart 				/* unlock info */
28426d9e8f2bSRandall Stewart 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
28436d9e8f2bSRandall Stewart 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2844b7b84c0eSMichael Tuexen 					/*
2845b7b84c0eSMichael Tuexen 					 * Ok, must be one-2-one and
2846b7b84c0eSMichael Tuexen 					 * allowing port re-use
2847b7b84c0eSMichael Tuexen 					 */
28486d9e8f2bSRandall Stewart 					port_reuse_active = 1;
28496d9e8f2bSRandall Stewart 					goto continue_anyway;
28506d9e8f2bSRandall Stewart 				}
2851457abbb8SMark Johnston 				SCTP_INP_WLOCK(inp);
28526d9e8f2bSRandall Stewart 				SCTP_INP_DECR_REF(inp);
28534a36122bSMark Johnston 				error = EADDRINUSE;
28544a36122bSMark Johnston 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2855457abbb8SMark Johnston 				goto out;
2856f8829a4aSRandall Stewart 			}
285742551e99SRandall Stewart 		} else {
285842551e99SRandall Stewart 			inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
285942551e99SRandall Stewart 			if (inp_tmp != NULL) {
286042551e99SRandall Stewart 				/*
286142551e99SRandall Stewart 				 * lock guy returned and lower count note
286242551e99SRandall Stewart 				 * that we are not bound so inp_tmp should
286342551e99SRandall Stewart 				 * NEVER be inp. And it is this inp
286442551e99SRandall Stewart 				 * (inp_tmp) that gets the reference bump,
286542551e99SRandall Stewart 				 * so we must lower it.
286642551e99SRandall Stewart 				 */
286742551e99SRandall Stewart 				SCTP_INP_DECR_REF(inp_tmp);
286842551e99SRandall Stewart 				/* unlock info */
28696d9e8f2bSRandall Stewart 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
28706d9e8f2bSRandall Stewart 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2871b7b84c0eSMichael Tuexen 					/*
2872b7b84c0eSMichael Tuexen 					 * Ok, must be one-2-one and
2873b7b84c0eSMichael Tuexen 					 * allowing port re-use
2874b7b84c0eSMichael Tuexen 					 */
28756d9e8f2bSRandall Stewart 					port_reuse_active = 1;
28766d9e8f2bSRandall Stewart 					goto continue_anyway;
28776d9e8f2bSRandall Stewart 				}
2878457abbb8SMark Johnston 				SCTP_INP_WLOCK(inp);
28796d9e8f2bSRandall Stewart 				SCTP_INP_DECR_REF(inp);
28804a36122bSMark Johnston 				error = EADDRINUSE;
28814a36122bSMark Johnston 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2882457abbb8SMark Johnston 				goto out;
288342551e99SRandall Stewart 			}
288442551e99SRandall Stewart 		}
28856d9e8f2bSRandall Stewart continue_anyway:
2886f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(inp);
288793908fceSMark Johnston 		SCTP_INP_DECR_REF(inp);
2888f8829a4aSRandall Stewart 		if (bindall) {
2889f8829a4aSRandall Stewart 			/* verify that no lport is not used by a singleton */
28906d9e8f2bSRandall Stewart 			if ((port_reuse_active == 0) &&
28918225a9bcSMichael Tuexen 			    (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
2892f8829a4aSRandall Stewart 				/* Sorry someone already has this one bound */
28936d9e8f2bSRandall Stewart 				if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
28946d9e8f2bSRandall Stewart 				    (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
28956d9e8f2bSRandall Stewart 					port_reuse_active = 1;
28966d9e8f2bSRandall Stewart 				} else {
28974a36122bSMark Johnston 					error = EADDRINUSE;
28984a36122bSMark Johnston 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
28994a36122bSMark Johnston 					goto out;
2900f8829a4aSRandall Stewart 				}
2901f8829a4aSRandall Stewart 			}
29026d9e8f2bSRandall Stewart 		}
2903f8829a4aSRandall Stewart 	} else {
29049a972525SRandall Stewart 		uint16_t first, last, candidate;
2905d61a0ae0SRandall Stewart 		uint16_t count;
2906f8829a4aSRandall Stewart 
2907d61a0ae0SRandall Stewart 		if (ip_inp->inp_flags & INP_HIGHPORT) {
2908482444b4SRandall Stewart 			first = MODULE_GLOBAL(ipport_hifirstauto);
2909482444b4SRandall Stewart 			last = MODULE_GLOBAL(ipport_hilastauto);
2910d61a0ae0SRandall Stewart 		} else if (ip_inp->inp_flags & INP_LOWPORT) {
29110d29e4bcSMark Johnston 			if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
2912c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
29134a36122bSMark Johnston 				goto out;
2914d61a0ae0SRandall Stewart 			}
2915482444b4SRandall Stewart 			first = MODULE_GLOBAL(ipport_lowfirstauto);
2916482444b4SRandall Stewart 			last = MODULE_GLOBAL(ipport_lowlastauto);
2917d61a0ae0SRandall Stewart 		} else {
2918482444b4SRandall Stewart 			first = MODULE_GLOBAL(ipport_firstauto);
2919482444b4SRandall Stewart 			last = MODULE_GLOBAL(ipport_lastauto);
2920d61a0ae0SRandall Stewart 		}
2921d61a0ae0SRandall Stewart 		if (first > last) {
2922d61a0ae0SRandall Stewart 			uint16_t temp;
2923f8829a4aSRandall Stewart 
2924d61a0ae0SRandall Stewart 			temp = first;
2925d61a0ae0SRandall Stewart 			first = last;
2926d61a0ae0SRandall Stewart 			last = temp;
2927f8829a4aSRandall Stewart 		}
2928d61a0ae0SRandall Stewart 		count = last - first + 1;	/* number of candidates */
29299a972525SRandall Stewart 		candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
29303c503c28SRandall Stewart 
29312496d812SMark Johnston 		for (;;) {
29326d9e8f2bSRandall Stewart 			if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
29332496d812SMark Johnston 				lport = htons(candidate);
29342496d812SMark Johnston 				break;
2935f8829a4aSRandall Stewart 			}
2936d61a0ae0SRandall Stewart 			if (--count == 0) {
29374a36122bSMark Johnston 				error = EADDRINUSE;
29384a36122bSMark Johnston 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
29394a36122bSMark Johnston 				goto out;
2940f8829a4aSRandall Stewart 			}
29419a972525SRandall Stewart 			if (candidate == last)
29429a972525SRandall Stewart 				candidate = first;
2943d61a0ae0SRandall Stewart 			else
29449a972525SRandall Stewart 				candidate = candidate + 1;
2945f8829a4aSRandall Stewart 		}
2946f8829a4aSRandall Stewart 	}
2947f8829a4aSRandall Stewart 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2948f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2949f8829a4aSRandall Stewart 		/*
2950f8829a4aSRandall Stewart 		 * this really should not happen. The guy did a non-blocking
2951f8829a4aSRandall Stewart 		 * bind and then did a close at the same time.
2952f8829a4aSRandall Stewart 		 */
29534a36122bSMark Johnston 		error = EINVAL;
29544a36122bSMark Johnston 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
29554a36122bSMark Johnston 		goto out;
2956f8829a4aSRandall Stewart 	}
2957f8829a4aSRandall Stewart 	/* ok we look clear to give out this port, so lets setup the binding */
2958f8829a4aSRandall Stewart 	if (bindall) {
2959f8829a4aSRandall Stewart 		/* binding to all addresses, so just set in the proper flags */
2960f8829a4aSRandall Stewart 		inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2961f8829a4aSRandall Stewart 		/* set the automatic addr changes from kernel flag */
2962b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
2963c4739e2fSRandall Stewart 			sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2964f8829a4aSRandall Stewart 			sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2965f8829a4aSRandall Stewart 		} else {
2966c4739e2fSRandall Stewart 			sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2967f8829a4aSRandall Stewart 			sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2968f8829a4aSRandall Stewart 		}
2969b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
2970c54a18d2SRandall Stewart 			sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2971c54a18d2SRandall Stewart 		} else {
2972c54a18d2SRandall Stewart 			sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2973c54a18d2SRandall Stewart 		}
29742afb3e84SRandall Stewart 		/*
29752afb3e84SRandall Stewart 		 * set the automatic mobility_base from kernel flag (by
29762afb3e84SRandall Stewart 		 * micchie)
29772afb3e84SRandall Stewart 		 */
2978b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
29792afb3e84SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
2980851b7298SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
29812afb3e84SRandall Stewart 		} else {
29822afb3e84SRandall Stewart 			sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
2983851b7298SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
29842afb3e84SRandall Stewart 		}
29852afb3e84SRandall Stewart 		/*
29862afb3e84SRandall Stewart 		 * set the automatic mobility_fasthandoff from kernel flag
29872afb3e84SRandall Stewart 		 * (by micchie)
29882afb3e84SRandall Stewart 		 */
2989b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
29902afb3e84SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
2991851b7298SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
29922afb3e84SRandall Stewart 		} else {
29932afb3e84SRandall Stewart 			sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
2994851b7298SRandall Stewart 			sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
29952afb3e84SRandall Stewart 		}
2996f8829a4aSRandall Stewart 	} else {
2997f8829a4aSRandall Stewart 		/*
2998f8829a4aSRandall Stewart 		 * bind specific, make sure flags is off and add a new
2999f8829a4aSRandall Stewart 		 * address structure to the sctp_addr_list inside the ep
3000f8829a4aSRandall Stewart 		 * structure.
3001f8829a4aSRandall Stewart 		 *
30024c7fb0cfSMichael Tuexen 		 * We will need to allocate one and insert it at the head.
30034c7fb0cfSMichael Tuexen 		 * The socketopt call can just insert new addresses in there
30044c7fb0cfSMichael Tuexen 		 * as well. It will also have to do the embed scope kame
30054c7fb0cfSMichael Tuexen 		 * hack too (before adding).
3006f8829a4aSRandall Stewart 		 */
300742551e99SRandall Stewart 		struct sctp_ifa *ifa;
300824aaac8dSMichael Tuexen 		union sctp_sockstore store;
3009f8829a4aSRandall Stewart 
301024aaac8dSMichael Tuexen 		memset(&store, 0, sizeof(store));
3011e6194c2eSMichael Tuexen 		switch (addr->sa_family) {
3012e0e00a4dSMichael Tuexen #ifdef INET
3013e6194c2eSMichael Tuexen 		case AF_INET:
301424aaac8dSMichael Tuexen 			memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
301524aaac8dSMichael Tuexen 			store.sin.sin_port = 0;
3016e6194c2eSMichael Tuexen 			break;
3017e0e00a4dSMichael Tuexen #endif
3018e0e00a4dSMichael Tuexen #ifdef INET6
3019e6194c2eSMichael Tuexen 		case AF_INET6:
302024aaac8dSMichael Tuexen 			memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
302124aaac8dSMichael Tuexen 			store.sin6.sin6_port = 0;
3022e6194c2eSMichael Tuexen 			break;
3023e0e00a4dSMichael Tuexen #endif
3024e6194c2eSMichael Tuexen 		default:
3025e6194c2eSMichael Tuexen 			break;
3026f8829a4aSRandall Stewart 		}
3027f8829a4aSRandall Stewart 		/*
3028f8829a4aSRandall Stewart 		 * first find the interface with the bound address need to
3029f8829a4aSRandall Stewart 		 * zero out the port to find the address! yuck! can't do
3030f8829a4aSRandall Stewart 		 * this earlier since need port for sctp_pcb_findep()
3031f8829a4aSRandall Stewart 		 */
30328225a9bcSMichael Tuexen 		if (sctp_ifap != NULL) {
30331b649582SRandall Stewart 			ifa = sctp_ifap;
30348225a9bcSMichael Tuexen 		} else {
30351b649582SRandall Stewart 			/*
30361b649582SRandall Stewart 			 * Note for BSD we hit here always other O/S's will
30372cf33471SMichael Tuexen 			 * pass things in via the sctp_ifap argument.
30381b649582SRandall Stewart 			 */
303924aaac8dSMichael Tuexen 			ifa = sctp_find_ifa_by_addr(&store.sa,
3040851b7298SRandall Stewart 			    vrf_id, SCTP_ADDR_NOT_LOCKED);
30411b649582SRandall Stewart 		}
3042f8829a4aSRandall Stewart 		if (ifa == NULL) {
30434a36122bSMark Johnston 			error = EADDRNOTAVAIL;
3044f8829a4aSRandall Stewart 			/* Can't find an interface with that address */
30454a36122bSMark Johnston 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
30464a36122bSMark Johnston 			goto out;
3047f8829a4aSRandall Stewart 		}
3048e6194c2eSMichael Tuexen #ifdef INET6
3049f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET6) {
305042551e99SRandall Stewart 			/* GAK, more FIXME IFA lock? */
305142551e99SRandall Stewart 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3052f8829a4aSRandall Stewart 				/* Can't bind a non-existent addr. */
30534a36122bSMark Johnston 				error = EINVAL;
30544a36122bSMark Johnston 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
30554a36122bSMark Johnston 				goto out;
3056f8829a4aSRandall Stewart 			}
3057f8829a4aSRandall Stewart 		}
3058e6194c2eSMichael Tuexen #endif
3059f8829a4aSRandall Stewart 		/* we're not bound all */
3060f8829a4aSRandall Stewart 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
30613c503c28SRandall Stewart 		/* allow bindx() to send ASCONF's for binding changes */
306242551e99SRandall Stewart 		sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
306380fefe0aSRandall Stewart 		/* clear automatic addr changes from kernel flag */
3064f8829a4aSRandall Stewart 		sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
306542551e99SRandall Stewart 
3066f8829a4aSRandall Stewart 		/* add this address to the endpoint list */
306742551e99SRandall Stewart 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
30684a36122bSMark Johnston 		if (error != 0)
30694a36122bSMark Johnston 			goto out;
3070f8829a4aSRandall Stewart 		inp->laddr_count++;
3071f8829a4aSRandall Stewart 	}
3072f8829a4aSRandall Stewart 	/* find the bucket */
30736d9e8f2bSRandall Stewart 	if (port_reuse_active) {
30746d9e8f2bSRandall Stewart 		/* Put it into tcp 1-2-1 hash */
3075034b88b0SMichael Tuexen 		head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
30766d9e8f2bSRandall Stewart 		inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
30776d9e8f2bSRandall Stewart 	} else {
3078034b88b0SMichael Tuexen 		head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
30796d9e8f2bSRandall Stewart 	}
3080f8829a4aSRandall Stewart 	/* put it in the bucket */
3081f8829a4aSRandall Stewart 	LIST_INSERT_HEAD(head, inp, sctp_hash);
30826d9e8f2bSRandall Stewart 	SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3083dd294dceSMichael Tuexen 	    (void *)head, ntohs(lport), port_reuse_active);
3084f8829a4aSRandall Stewart 	/* set in the port */
3085f8829a4aSRandall Stewart 	inp->sctp_lport = lport;
3086f8829a4aSRandall Stewart 
3087f8829a4aSRandall Stewart 	/* turn off just the unbound flag */
30884a36122bSMark Johnston 	KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
30894a36122bSMark Johnston 	    ("%s: inp %p is already bound", __func__, inp));
3090f8829a4aSRandall Stewart 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
30914a36122bSMark Johnston out:
3092457abbb8SMark Johnston 	return (error);
3093457abbb8SMark Johnston }
3094457abbb8SMark Johnston 
3095457abbb8SMark Johnston int
sctp_inpcb_bind(struct socket * so,struct sockaddr * addr,struct sctp_ifa * sctp_ifap,struct thread * td)3096457abbb8SMark Johnston sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3097457abbb8SMark Johnston     struct sctp_ifa *sctp_ifap, struct thread *td)
3098457abbb8SMark Johnston {
3099457abbb8SMark Johnston 	struct sctp_inpcb *inp;
3100457abbb8SMark Johnston 	int error;
3101457abbb8SMark Johnston 
3102457abbb8SMark Johnston 	inp = so->so_pcb;
3103457abbb8SMark Johnston 	SCTP_INP_INFO_WLOCK();
3104457abbb8SMark Johnston 	SCTP_INP_WLOCK(inp);
3105457abbb8SMark Johnston 	error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
3106f8829a4aSRandall Stewart 	SCTP_INP_WUNLOCK(inp);
3107f8829a4aSRandall Stewart 	SCTP_INP_INFO_WUNLOCK();
31084a36122bSMark Johnston 	return (error);
3109f8829a4aSRandall Stewart }
3110f8829a4aSRandall Stewart 
3111f8829a4aSRandall Stewart static void
sctp_iterator_inp_being_freed(struct sctp_inpcb * inp)3112f7517433SRandall Stewart sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3113f8829a4aSRandall Stewart {
3114f7517433SRandall Stewart 	struct sctp_iterator *it, *nit;
3115f8829a4aSRandall Stewart 
3116f8829a4aSRandall Stewart 	/*
3117f8829a4aSRandall Stewart 	 * We enter with the only the ITERATOR_LOCK in place and a write
3118f8829a4aSRandall Stewart 	 * lock on the inp_info stuff.
3119f8829a4aSRandall Stewart 	 */
3120f7517433SRandall Stewart 	it = sctp_it_ctl.cur_it;
3121f7517433SRandall Stewart 	if (it && (it->vn != curvnet)) {
3122f7517433SRandall Stewart 		/* Its not looking at our VNET */
3123f7517433SRandall Stewart 		return;
3124f7517433SRandall Stewart 	}
3125f7517433SRandall Stewart 	if (it && (it->inp == inp)) {
3126f8829a4aSRandall Stewart 		/*
3127f7517433SRandall Stewart 		 * This is tricky and we hold the iterator lock, but when it
3128f7517433SRandall Stewart 		 * returns and gets the lock (when we release it) the
3129f7517433SRandall Stewart 		 * iterator will try to operate on inp. We need to stop that
3130f7517433SRandall Stewart 		 * from happening. But of course the iterator has a
3131f7517433SRandall Stewart 		 * reference on the stcb and inp. We can mark it and it will
3132f7517433SRandall Stewart 		 * stop.
3133f7517433SRandall Stewart 		 *
31344c7fb0cfSMichael Tuexen 		 * If its a single iterator situation, we set the end
31354c7fb0cfSMichael Tuexen 		 * iterator flag. Otherwise we set the iterator to go to the
31364c7fb0cfSMichael Tuexen 		 * next inp.
3137f7517433SRandall Stewart 		 *
3138f8829a4aSRandall Stewart 		 */
3139f7517433SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3140f7517433SRandall Stewart 			sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3141f7517433SRandall Stewart 		} else {
3142f7517433SRandall Stewart 			sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3143f7517433SRandall Stewart 		}
3144f7517433SRandall Stewart 	}
3145f7517433SRandall Stewart 	/*
3146f7517433SRandall Stewart 	 * Now go through and remove any single reference to our inp that
3147f7517433SRandall Stewart 	 * may be still pending on the list
3148f7517433SRandall Stewart 	 */
3149f7517433SRandall Stewart 	SCTP_IPI_ITERATOR_WQ_LOCK();
31504a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3151f7517433SRandall Stewart 		if (it->vn != curvnet) {
3152f8829a4aSRandall Stewart 			continue;
3153f7517433SRandall Stewart 		}
3154f8829a4aSRandall Stewart 		if (it->inp == inp) {
3155f7517433SRandall Stewart 			/* This one points to me is it inp specific? */
3156f8829a4aSRandall Stewart 			if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3157f7517433SRandall Stewart 				/* Remove and free this one */
3158f7517433SRandall Stewart 				TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3159f7517433SRandall Stewart 				    it, sctp_nxt_itr);
3160f7517433SRandall Stewart 				if (it->function_atend != NULL) {
3161f7517433SRandall Stewart 					(*it->function_atend) (it->pointer, it->val);
3162f7517433SRandall Stewart 				}
3163f7517433SRandall Stewart 				SCTP_FREE(it, SCTP_M_ITER);
3164f8829a4aSRandall Stewart 			} else {
3165f7517433SRandall Stewart 				it->inp = LIST_NEXT(it->inp, sctp_list);
3166fc066a61SMichael Tuexen 				if (it->inp) {
3167fc066a61SMichael Tuexen 					SCTP_INP_INCR_REF(it->inp);
3168fc066a61SMichael Tuexen 				}
3169f8829a4aSRandall Stewart 			}
3170b7b84c0eSMichael Tuexen 			/*
3171b7b84c0eSMichael Tuexen 			 * When its put in the refcnt is incremented so decr
3172b7b84c0eSMichael Tuexen 			 * it
3173b7b84c0eSMichael Tuexen 			 */
3174ec4c19fcSRandall Stewart 			SCTP_INP_DECR_REF(inp);
3175f8829a4aSRandall Stewart 		}
3176f8829a4aSRandall Stewart 	}
3177f7517433SRandall Stewart 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
3178f8829a4aSRandall Stewart }
3179f8829a4aSRandall Stewart 
3180f8829a4aSRandall Stewart /* release sctp_inpcb unbind the port */
3181f8829a4aSRandall Stewart void
sctp_inpcb_free(struct sctp_inpcb * inp,int immediate,int from)3182f8829a4aSRandall Stewart sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3183f8829a4aSRandall Stewart {
3184f8829a4aSRandall Stewart 	/*
3185f8829a4aSRandall Stewart 	 * Here we free a endpoint. We must find it (if it is in the Hash
3186f8829a4aSRandall Stewart 	 * table) and remove it from there. Then we must also find it in the
3187f8829a4aSRandall Stewart 	 * overall list and remove it from there. After all removals are
3188f8829a4aSRandall Stewart 	 * complete then any timer has to be stopped. Then start the actual
3189f8829a4aSRandall Stewart 	 * freeing. a) Any local lists. b) Any associations. c) The hash of
3190f8829a4aSRandall Stewart 	 * all associations. d) finally the ep itself.
3191f8829a4aSRandall Stewart 	 */
31923c3d77bdSMichael Tuexen 	struct sctp_tcb *stcb, *nstcb;
3193f8829a4aSRandall Stewart 	struct sctp_laddr *laddr, *nladdr;
3194f8829a4aSRandall Stewart 	struct inpcb *ip_pcb;
3195f8829a4aSRandall Stewart 	struct socket *so;
31968ce4a9a2SRandall Stewart 	int being_refed = 0;
31974a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
319893164cf9SRandall Stewart 	int cnt;
31994a9ef3f8SMichael Tuexen 	sctp_sharedkey_t *shared_key, *nshared_key;
3200f8829a4aSRandall Stewart 
3201f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
3202f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 0);
3203f8829a4aSRandall Stewart #endif
3204f8829a4aSRandall Stewart 	SCTP_ITERATOR_LOCK();
32052c6b25b4SRandall Stewart 	/* mark any iterators on the list or being processed */
32062c6b25b4SRandall Stewart 	sctp_iterator_inp_being_freed(inp);
32072c6b25b4SRandall Stewart 	SCTP_ITERATOR_UNLOCK();
3208c17b531bSMark Johnston 
3209f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK(inp);
3210f8829a4aSRandall Stewart 	SCTP_INP_INFO_WLOCK();
3211f8829a4aSRandall Stewart 	SCTP_INP_WLOCK(inp);
3212c17b531bSMark Johnston 	so = inp->sctp_socket;
3213c17b531bSMark Johnston 	KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
3214c17b531bSMark Johnston 	    ("%s: inp %p still has socket", __func__, inp));
3215c17b531bSMark Johnston 	KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
3216c17b531bSMark Johnston 	    ("%s: double free of inp %p", __func__, inp));
3217b3a44e46SRandall Stewart 	if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3218b3a44e46SRandall Stewart 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3219b3a44e46SRandall Stewart 		/* socket is gone, so no more wakeups allowed */
3220b3a44e46SRandall Stewart 		inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3221b3a44e46SRandall Stewart 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3222b3a44e46SRandall Stewart 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3223b3a44e46SRandall Stewart 	}
3224ad81507eSRandall Stewart 	/* First time through we have the socket lock, after that no more. */
3225a5d547adSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3226a5d547adSRandall Stewart 	    SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3227f8829a4aSRandall Stewart 
3228f8829a4aSRandall Stewart 	if (inp->control) {
3229f8829a4aSRandall Stewart 		sctp_m_freem(inp->control);
3230f8829a4aSRandall Stewart 		inp->control = NULL;
3231f8829a4aSRandall Stewart 	}
3232f8829a4aSRandall Stewart 	if (inp->pkt) {
3233f8829a4aSRandall Stewart 		sctp_m_freem(inp->pkt);
3234f8829a4aSRandall Stewart 		inp->pkt = NULL;
3235f8829a4aSRandall Stewart 	}
3236f8829a4aSRandall Stewart 	ip_pcb = &inp->ip_inp.inp;	/* we could just cast the main pointer
3237f8829a4aSRandall Stewart 					 * here but I will be nice :> (i.e.
3238f8829a4aSRandall Stewart 					 * ip_pcb = ep;) */
3239b0552ae2SRandall Stewart 	if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3240f8829a4aSRandall Stewart 		int cnt_in_sd;
3241f8829a4aSRandall Stewart 
3242f8829a4aSRandall Stewart 		cnt_in_sd = 0;
32433c3d77bdSMichael Tuexen 		LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
32443c3d77bdSMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3245eeba2221SMichael Tuexen 			/* Disconnect the socket please. */
32463c3d77bdSMichael Tuexen 			stcb->sctp_socket = NULL;
3247eeba2221SMichael Tuexen 			SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3248eeba2221SMichael Tuexen 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3249f8829a4aSRandall Stewart 				/* Skip guys being freed */
3250f8829a4aSRandall Stewart 				cnt_in_sd++;
32513c3d77bdSMichael Tuexen 				if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
325262fb761fSRandall Stewart 					/*
325362fb761fSRandall Stewart 					 * Special case - we did not start a
325462fb761fSRandall Stewart 					 * kill timer on the asoc due to it
325562fb761fSRandall Stewart 					 * was not closed. So go ahead and
325662fb761fSRandall Stewart 					 * start it now.
325762fb761fSRandall Stewart 					 */
32583c3d77bdSMichael Tuexen 					SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
32593c3d77bdSMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
326062fb761fSRandall Stewart 				}
32613c3d77bdSMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
3262f8829a4aSRandall Stewart 				continue;
3263f8829a4aSRandall Stewart 			}
32643c3d77bdSMichael Tuexen 			if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
32653c3d77bdSMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) &&
32663c3d77bdSMichael Tuexen 			    (stcb->asoc.total_output_queue_size == 0)) {
326775298de2SRandall Stewart 				/*
326875298de2SRandall Stewart 				 * If we have data in queue, we don't want
326975298de2SRandall Stewart 				 * to just free since the app may have done,
327075298de2SRandall Stewart 				 * send()/close or connect/send/close. And
327175298de2SRandall Stewart 				 * it wants the data to get across first.
327275298de2SRandall Stewart 				 */
32732afb3e84SRandall Stewart 				/* Just abandon things in the front states */
32743c3d77bdSMichael Tuexen 				if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE,
3275c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3276c4739e2fSRandall Stewart 					cnt_in_sd++;
3277c4739e2fSRandall Stewart 				}
3278f8829a4aSRandall Stewart 				continue;
3279f8829a4aSRandall Stewart 			}
32803c3d77bdSMichael Tuexen 			if ((stcb->asoc.size_on_reasm_queue > 0) ||
32813c3d77bdSMichael Tuexen 			    (stcb->asoc.size_on_all_streams > 0) ||
3282edc5b6eaSMichael Tuexen 			    ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) {
3283f8829a4aSRandall Stewart 				/* Left with Data unread */
3284f8829a4aSRandall Stewart 				struct mbuf *op_err;
3285f8829a4aSRandall Stewart 
3286ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
32873c3d77bdSMichael Tuexen 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
32883c3d77bdSMichael Tuexen 				sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3289f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_aborted);
32903c3d77bdSMichael Tuexen 				if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
32913c3d77bdSMichael Tuexen 				    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3292f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3293f8829a4aSRandall Stewart 				}
32943c3d77bdSMichael Tuexen 				if (sctp_free_assoc(inp, stcb,
3295c4739e2fSRandall Stewart 				    SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3296c4739e2fSRandall Stewart 					cnt_in_sd++;
3297c4739e2fSRandall Stewart 				}
3298f8829a4aSRandall Stewart 				continue;
32993c3d77bdSMichael Tuexen 			} else if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
33003c3d77bdSMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
33013c3d77bdSMichael Tuexen 			    (stcb->asoc.stream_queue_cnt == 0)) {
33023c3d77bdSMichael Tuexen 				if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) {
3303f8829a4aSRandall Stewart 					goto abort_anyway;
3304f8829a4aSRandall Stewart 				}
33053c3d77bdSMichael Tuexen 				if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
33063c3d77bdSMichael Tuexen 				    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3307ca85e948SMichael Tuexen 					struct sctp_nets *netp;
3308ca85e948SMichael Tuexen 
3309f8829a4aSRandall Stewart 					/*
3310f8829a4aSRandall Stewart 					 * there is nothing queued to send,
3311f8829a4aSRandall Stewart 					 * so I send shutdown
3312f8829a4aSRandall Stewart 					 */
33133c3d77bdSMichael Tuexen 					if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
33143c3d77bdSMichael Tuexen 					    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3315f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3316f42a358aSRandall Stewart 					}
33173c3d77bdSMichael Tuexen 					SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
33183c3d77bdSMichael Tuexen 					sctp_stop_timers_for_shutdown(stcb);
33193c3d77bdSMichael Tuexen 					if (stcb->asoc.alternate) {
33203c3d77bdSMichael Tuexen 						netp = stcb->asoc.alternate;
3321c39cfa1fSMichael Tuexen 					} else {
33223c3d77bdSMichael Tuexen 						netp = stcb->asoc.primary_destination;
3323c39cfa1fSMichael Tuexen 					}
33243c3d77bdSMichael Tuexen 					sctp_send_shutdown(stcb, netp);
33253c3d77bdSMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
3326ca85e948SMichael Tuexen 					    netp);
33273c3d77bdSMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
33283c3d77bdSMichael Tuexen 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3329f8829a4aSRandall Stewart 				}
3330f8829a4aSRandall Stewart 			} else {
3331f8829a4aSRandall Stewart 				/* mark into shutdown pending */
33323c3d77bdSMichael Tuexen 				SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
33333c3d77bdSMichael Tuexen 				if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) {
33343c3d77bdSMichael Tuexen 					SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
3335f8829a4aSRandall Stewart 				}
33363c3d77bdSMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
33373c3d77bdSMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
33383c3d77bdSMichael Tuexen 				    (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3339f8829a4aSRandall Stewart 					struct mbuf *op_err;
3340f8829a4aSRandall Stewart 
3341f8829a4aSRandall Stewart 			abort_anyway:
3342ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
33433c3d77bdSMichael Tuexen 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
33443c3d77bdSMichael Tuexen 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3345f8829a4aSRandall Stewart 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
33463c3d77bdSMichael Tuexen 					if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
33473c3d77bdSMichael Tuexen 					    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3348f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3349f8829a4aSRandall Stewart 					}
33503c3d77bdSMichael Tuexen 					if (sctp_free_assoc(inp, stcb,
3351c4739e2fSRandall Stewart 					    SCTP_PCBFREE_NOFORCE,
3352c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3353c4739e2fSRandall Stewart 						cnt_in_sd++;
3354c4739e2fSRandall Stewart 					}
3355f8829a4aSRandall Stewart 					continue;
33562afb3e84SRandall Stewart 				} else {
33573c3d77bdSMichael Tuexen 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3358f8829a4aSRandall Stewart 				}
3359f8829a4aSRandall Stewart 			}
3360f8829a4aSRandall Stewart 			cnt_in_sd++;
33613c3d77bdSMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3362f8829a4aSRandall Stewart 		}
3363f8829a4aSRandall Stewart 		/* now is there some left in our SHUTDOWN state? */
3364f8829a4aSRandall Stewart 		if (cnt_in_sd) {
3365f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
3366f8829a4aSRandall Stewart 			sctp_log_closing(inp, NULL, 2);
3367f8829a4aSRandall Stewart #endif
33684a16c2c8SRandall Stewart 			inp->sctp_socket = NULL;
3369b3a44e46SRandall Stewart 			SCTP_INP_WUNLOCK(inp);
3370b3a44e46SRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
3371b3a44e46SRandall Stewart 			SCTP_INP_INFO_WUNLOCK();
3372f8829a4aSRandall Stewart 			return;
3373f8829a4aSRandall Stewart 		}
3374f8829a4aSRandall Stewart 	}
3375f8829a4aSRandall Stewart 	inp->sctp_socket = NULL;
33769b2a35b3SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3377f8829a4aSRandall Stewart 		/*
3378f8829a4aSRandall Stewart 		 * ok, this guy has been bound. It's port is somewhere in
3379b3f1ea41SRandall Stewart 		 * the SCTP_BASE_INFO(hash table). Remove it!
3380f8829a4aSRandall Stewart 		 */
3381f8829a4aSRandall Stewart 		LIST_REMOVE(inp, sctp_hash);
3382f8829a4aSRandall Stewart 		inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3383f8829a4aSRandall Stewart 	}
33840053ed28SMichael Tuexen 
3385f8829a4aSRandall Stewart 	/*
3386f8829a4aSRandall Stewart 	 * If there is a timer running to kill us, forget it, since it may
3387f8829a4aSRandall Stewart 	 * have a contest on the INP lock.. which would cause us to die ...
3388f8829a4aSRandall Stewart 	 */
3389f8829a4aSRandall Stewart 	cnt = 0;
33903c3d77bdSMichael Tuexen 	LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
33913c3d77bdSMichael Tuexen 		SCTP_TCB_LOCK(stcb);
3392205f3e15SMichael Tuexen 		if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3393205f3e15SMichael Tuexen 			/* Disconnect the socket please */
33943c3d77bdSMichael Tuexen 			stcb->sctp_socket = NULL;
33953c3d77bdSMichael Tuexen 			SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3396205f3e15SMichael Tuexen 		}
33973c3d77bdSMichael Tuexen 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
33983c3d77bdSMichael Tuexen 			if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
33993c3d77bdSMichael Tuexen 				SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
34003c3d77bdSMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
34017c82e9faSRandall Stewart 			}
3402f8829a4aSRandall Stewart 			cnt++;
34033c3d77bdSMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3404f8829a4aSRandall Stewart 			continue;
3405f8829a4aSRandall Stewart 		}
3406f8829a4aSRandall Stewart 		/* Free associations that are NOT killing us */
34073c3d77bdSMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
34083c3d77bdSMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3409f8829a4aSRandall Stewart 			struct mbuf *op_err;
3410f8829a4aSRandall Stewart 
3411ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
34123c3d77bdSMichael Tuexen 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
34133c3d77bdSMichael Tuexen 			sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3414f8829a4aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_aborted);
34153c3d77bdSMichael Tuexen 		} else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3416f8829a4aSRandall Stewart 			cnt++;
34173c3d77bdSMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3418f8829a4aSRandall Stewart 			continue;
3419f8829a4aSRandall Stewart 		}
34203c3d77bdSMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
34213c3d77bdSMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3422f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3423f8829a4aSRandall Stewart 		}
34243c3d77bdSMichael Tuexen 		if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
3425b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3426c4739e2fSRandall Stewart 			cnt++;
3427c4739e2fSRandall Stewart 		}
3428f8829a4aSRandall Stewart 	}
3429f8829a4aSRandall Stewart 	if (cnt) {
3430f8829a4aSRandall Stewart 		/* Ok we have someone out there that will kill us */
3431f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
3432f8829a4aSRandall Stewart 		sctp_log_closing(inp, NULL, 3);
3433f8829a4aSRandall Stewart #endif
3434b3a44e46SRandall Stewart 		SCTP_INP_WUNLOCK(inp);
3435b3a44e46SRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
3436b3a44e46SRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
3437f8829a4aSRandall Stewart 		return;
3438f8829a4aSRandall Stewart 	}
34398ce4a9a2SRandall Stewart 	if (SCTP_INP_LOCK_CONTENDED(inp))
34408ce4a9a2SRandall Stewart 		being_refed++;
34418ce4a9a2SRandall Stewart 	if (SCTP_INP_READ_CONTENDED(inp))
34428ce4a9a2SRandall Stewart 		being_refed++;
34438ce4a9a2SRandall Stewart 	if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
34448ce4a9a2SRandall Stewart 		being_refed++;
34458745f898SMichael Tuexen 	/* NOTE: 0 refcount also means no timers are referencing us. */
34460c7dc840SRandall Stewart 	if ((inp->refcount) ||
34478ce4a9a2SRandall Stewart 	    (being_refed) ||
34488ce4a9a2SRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3449b3a44e46SRandall Stewart #ifdef SCTP_LOG_CLOSING
3450b3a44e46SRandall Stewart 		sctp_log_closing(inp, NULL, 4);
3451b3a44e46SRandall Stewart #endif
3452f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3453f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
3454f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
3455f8829a4aSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
3456f8829a4aSRandall Stewart 		return;
3457f8829a4aSRandall Stewart 	}
3458f8829a4aSRandall Stewart 	inp->sctp_ep.signature_change.type = 0;
3459f8829a4aSRandall Stewart 	inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3460b3a44e46SRandall Stewart 	/*
3461b3a44e46SRandall Stewart 	 * Remove it from the list .. last thing we need a lock for.
3462b3a44e46SRandall Stewart 	 */
3463b3a44e46SRandall Stewart 	LIST_REMOVE(inp, sctp_list);
3464b3a44e46SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
3465b3a44e46SRandall Stewart 	SCTP_ASOC_CREATE_UNLOCK(inp);
3466b3a44e46SRandall Stewart 	SCTP_INP_INFO_WUNLOCK();
3467f8829a4aSRandall Stewart 
3468f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
3469f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 5);
3470f8829a4aSRandall Stewart #endif
3471830d754dSRandall Stewart 	if ((inp->sctp_asocidhash) != NULL) {
3472830d754dSRandall Stewart 		SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3473830d754dSRandall Stewart 		inp->sctp_asocidhash = NULL;
3474830d754dSRandall Stewart 	}
34753c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
34764a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3477f1f73e57SRandall Stewart 		/* Its only abandoned if it had data left */
3478f1f73e57SRandall Stewart 		if (sq->length)
3479f1f73e57SRandall Stewart 			SCTP_STAT_INCR(sctps_left_abandon);
3480f1f73e57SRandall Stewart 
3481f8829a4aSRandall Stewart 		TAILQ_REMOVE(&inp->read_queue, sq, next);
3482f8829a4aSRandall Stewart 		sctp_free_remote_addr(sq->whoFrom);
3483f8829a4aSRandall Stewart 		if (so)
3484c6207881SMichael Tuexen 			SCTP_SB_DECR(&so->so_rcv, sq->length);
3485f8829a4aSRandall Stewart 		if (sq->data) {
3486f8829a4aSRandall Stewart 			sctp_m_freem(sq->data);
3487f8829a4aSRandall Stewart 			sq->data = NULL;
3488f8829a4aSRandall Stewart 		}
3489f8829a4aSRandall Stewart 		/*
3490f8829a4aSRandall Stewart 		 * no need to free the net count, since at this point all
3491f8829a4aSRandall Stewart 		 * assoc's are gone.
3492f8829a4aSRandall Stewart 		 */
349344249214SRandall Stewart 		sctp_free_a_readq(NULL, sq);
3494f8829a4aSRandall Stewart 	}
3495f8829a4aSRandall Stewart 	/* Now the sctp_pcb things */
3496f8829a4aSRandall Stewart 	/*
3497f8829a4aSRandall Stewart 	 * free each asoc if it is not already closed/free. we can't use the
3498f8829a4aSRandall Stewart 	 * macro here since le_next will get freed as part of the
3499f8829a4aSRandall Stewart 	 * sctp_free_assoc() call.
3500f8829a4aSRandall Stewart 	 */
3501f8829a4aSRandall Stewart 	if (ip_pcb->inp_options) {
3502f8829a4aSRandall Stewart 		(void)sctp_m_free(ip_pcb->inp_options);
3503f8829a4aSRandall Stewart 		ip_pcb->inp_options = 0;
3504f8829a4aSRandall Stewart 	}
3505f8829a4aSRandall Stewart #ifdef INET6
350694962f6bSMichael Tuexen 	if (ip_pcb->inp_vflag & INP_IPV6) {
350794962f6bSMichael Tuexen 		ip6_freepcbopts(ip_pcb->in6p_outputopts);
350894962f6bSMichael Tuexen 	}
3509f8829a4aSRandall Stewart #endif				/* INET6 */
3510f8829a4aSRandall Stewart 	ip_pcb->inp_vflag = 0;
3511f8829a4aSRandall Stewart 	/* free up authentication fields */
3512f8829a4aSRandall Stewart 	if (inp->sctp_ep.local_auth_chunks != NULL)
3513f8829a4aSRandall Stewart 		sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3514f8829a4aSRandall Stewart 	if (inp->sctp_ep.local_hmacs != NULL)
3515f8829a4aSRandall Stewart 		sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3516f8829a4aSRandall Stewart 
35174a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3518f8829a4aSRandall Stewart 		LIST_REMOVE(shared_key, next);
3519f8829a4aSRandall Stewart 		sctp_free_sharedkey(shared_key);
35203c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
3521f8829a4aSRandall Stewart 	}
3522f8829a4aSRandall Stewart 
3523f8829a4aSRandall Stewart 	/*
3524f8829a4aSRandall Stewart 	 * if we have an address list the following will free the list of
3525f8829a4aSRandall Stewart 	 * ifaddr's that are set into this ep. Again macro limitations here,
3526f8829a4aSRandall Stewart 	 * since the LIST_FOREACH could be a bad idea.
3527f8829a4aSRandall Stewart 	 */
35284a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
352942551e99SRandall Stewart 		sctp_remove_laddr(laddr);
3530f8829a4aSRandall Stewart 	}
3531a5d547adSRandall Stewart 
3532a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS
3533a5d547adSRandall Stewart 	/* TEMP CODE */
35343c3d77bdSMichael Tuexen 	LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) {
35353c3d77bdSMichael Tuexen 		LIST_REMOVE(stcb, sctp_tcblist);
35363c3d77bdSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
3537a5d547adSRandall Stewart 		SCTP_DECR_ASOC_COUNT();
3538a5d547adSRandall Stewart 	}
3539a5d547adSRandall Stewart 	/* *** END TEMP CODE *** */
3540a5d547adSRandall Stewart #endif
3541f8829a4aSRandall Stewart 	/* Now lets see about freeing the EP hash table. */
3542f8829a4aSRandall Stewart 	if (inp->sctp_tcbhash != NULL) {
354344b7479bSRandall Stewart 		SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
354444b7479bSRandall Stewart 		inp->sctp_tcbhash = NULL;
3545f8829a4aSRandall Stewart 	}
3546f8829a4aSRandall Stewart 	/* Now we must put the ep memory back into the zone pool */
35476ba22f19SMichael Tuexen 	crfree(inp->ip_inp.inp.inp_cred);
3548e1461651SRandall Stewart 	INP_LOCK_DESTROY(&inp->ip_inp.inp);
3549f8829a4aSRandall Stewart 	SCTP_INP_LOCK_DESTROY(inp);
35506cb8b3b5SMichael Tuexen 	SCTP_INP_READ_LOCK_DESTROY(inp);
3551f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3552b3f1ea41SRandall Stewart 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3553f8829a4aSRandall Stewart 	SCTP_DECR_EP_COUNT();
3554f8829a4aSRandall Stewart }
3555f8829a4aSRandall Stewart 
3556f8829a4aSRandall Stewart struct sctp_nets *
sctp_findnet(struct sctp_tcb * stcb,struct sockaddr * addr)3557f8829a4aSRandall Stewart sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3558f8829a4aSRandall Stewart {
3559f8829a4aSRandall Stewart 	struct sctp_nets *net;
3560f8829a4aSRandall Stewart 
3561f8829a4aSRandall Stewart 	/* locate the address */
3562f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3563f8829a4aSRandall Stewart 		if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3564f8829a4aSRandall Stewart 			return (net);
3565f8829a4aSRandall Stewart 	}
3566f8829a4aSRandall Stewart 	return (NULL);
3567f8829a4aSRandall Stewart }
3568f8829a4aSRandall Stewart 
3569f8829a4aSRandall Stewart int
sctp_is_address_on_local_host(struct sockaddr * addr,uint32_t vrf_id)357042551e99SRandall Stewart sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3571f8829a4aSRandall Stewart {
357242551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
3573f8829a4aSRandall Stewart 
3574851b7298SRandall Stewart 	sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
357542551e99SRandall Stewart 	if (sctp_ifa) {
3576f8829a4aSRandall Stewart 		return (1);
357742551e99SRandall Stewart 	} else {
3578f8829a4aSRandall Stewart 		return (0);
3579f8829a4aSRandall Stewart 	}
358042551e99SRandall Stewart }
358142551e99SRandall Stewart 
3582ad81507eSRandall Stewart /*
3583ad81507eSRandall Stewart  * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3584ad81507eSRandall Stewart  * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3585ad81507eSRandall Stewart  * stats of stuff.
3586ad81507eSRandall Stewart  */
3587f8829a4aSRandall Stewart int
sctp_add_remote_addr(struct sctp_tcb * stcb,struct sockaddr * newaddr,struct sctp_nets ** netp,uint16_t port,int set_scope,int from)3588f8829a4aSRandall Stewart sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
35897154bf4aSMichael Tuexen     struct sctp_nets **netp, uint16_t port, int set_scope, int from)
3590f8829a4aSRandall Stewart {
3591f8829a4aSRandall Stewart 	/*
3592f8829a4aSRandall Stewart 	 * The following is redundant to the same lines in the
3593034b88b0SMichael Tuexen 	 * sctp_aloc_assoc() but is needed since others call the add address
3594034b88b0SMichael Tuexen 	 * function
3595f8829a4aSRandall Stewart 	 */
3596f8829a4aSRandall Stewart 	struct sctp_nets *net, *netfirst;
3597f8829a4aSRandall Stewart 	int addr_inscope;
3598f8829a4aSRandall Stewart 
3599ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3600ad81507eSRandall Stewart 	    from);
3601ad81507eSRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3602f8829a4aSRandall Stewart 
3603f8829a4aSRandall Stewart 	netfirst = sctp_findnet(stcb, newaddr);
3604f8829a4aSRandall Stewart 	if (netfirst) {
3605f8829a4aSRandall Stewart 		/*
3606f8829a4aSRandall Stewart 		 * Lie and return ok, we don't want to make the association
3607f8829a4aSRandall Stewart 		 * go away for this behavior. It will happen in the TCP
3608f8829a4aSRandall Stewart 		 * model in a connected socket. It does not reach the hash
3609f8829a4aSRandall Stewart 		 * table until after the association is built so it can't be
3610f8829a4aSRandall Stewart 		 * found. Mark as reachable, since the initial creation will
3611f8829a4aSRandall Stewart 		 * have been cleared and the NOT_IN_ASSOC flag will have
3612f8829a4aSRandall Stewart 		 * been added... and we don't want to end up removing it
3613f8829a4aSRandall Stewart 		 * back out.
3614f8829a4aSRandall Stewart 		 */
3615f8829a4aSRandall Stewart 		if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3616f8829a4aSRandall Stewart 			netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3617f8829a4aSRandall Stewart 			    SCTP_ADDR_UNCONFIRMED);
3618f8829a4aSRandall Stewart 		} else {
3619f8829a4aSRandall Stewart 			netfirst->dest_state = SCTP_ADDR_REACHABLE;
3620f8829a4aSRandall Stewart 		}
3621f8829a4aSRandall Stewart 
3622f8829a4aSRandall Stewart 		return (0);
3623f8829a4aSRandall Stewart 	}
3624f8829a4aSRandall Stewart 	addr_inscope = 1;
3625e6194c2eSMichael Tuexen 	switch (newaddr->sa_family) {
3626e6194c2eSMichael Tuexen #ifdef INET
3627e6194c2eSMichael Tuexen 	case AF_INET:
3628e6194c2eSMichael Tuexen 		{
3629f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
3630f8829a4aSRandall Stewart 
3631f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)newaddr;
3632f8829a4aSRandall Stewart 			if (sin->sin_addr.s_addr == 0) {
3633f8829a4aSRandall Stewart 				/* Invalid address */
3634f8829a4aSRandall Stewart 				return (-1);
3635f8829a4aSRandall Stewart 			}
36365ba7f91fSMichael Tuexen 			/* zero out the zero area */
3637f8829a4aSRandall Stewart 			memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3638f8829a4aSRandall Stewart 
3639f8829a4aSRandall Stewart 			/* assure len is set */
3640f8829a4aSRandall Stewart 			sin->sin_len = sizeof(struct sockaddr_in);
3641f8829a4aSRandall Stewart 			if (set_scope) {
3642f8829a4aSRandall Stewart 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3643a1cb341bSMichael Tuexen 					stcb->asoc.scope.ipv4_local_scope = 1;
3644f8829a4aSRandall Stewart 				}
3645f8829a4aSRandall Stewart 			} else {
3646f8829a4aSRandall Stewart 				/* Validate the address is in scope */
3647f8829a4aSRandall Stewart 				if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3648a1cb341bSMichael Tuexen 				    (stcb->asoc.scope.ipv4_local_scope == 0)) {
3649f8829a4aSRandall Stewart 					addr_inscope = 0;
3650f8829a4aSRandall Stewart 				}
3651f8829a4aSRandall Stewart 			}
3652e6194c2eSMichael Tuexen 			break;
3653e6194c2eSMichael Tuexen 		}
3654e6194c2eSMichael Tuexen #endif
3655c54a18d2SRandall Stewart #ifdef INET6
3656e6194c2eSMichael Tuexen 	case AF_INET6:
3657e6194c2eSMichael Tuexen 		{
3658f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6;
3659f8829a4aSRandall Stewart 
3660f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)newaddr;
3661f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3662f8829a4aSRandall Stewart 				/* Invalid address */
3663f8829a4aSRandall Stewart 				return (-1);
3664f8829a4aSRandall Stewart 			}
3665f8829a4aSRandall Stewart 			/* assure len is set */
3666f8829a4aSRandall Stewart 			sin6->sin6_len = sizeof(struct sockaddr_in6);
3667f8829a4aSRandall Stewart 			if (set_scope) {
366842551e99SRandall Stewart 				if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3669a1cb341bSMichael Tuexen 					stcb->asoc.scope.loopback_scope = 1;
3670a1cb341bSMichael Tuexen 					stcb->asoc.scope.local_scope = 0;
3671a1cb341bSMichael Tuexen 					stcb->asoc.scope.ipv4_local_scope = 1;
3672a1cb341bSMichael Tuexen 					stcb->asoc.scope.site_scope = 1;
3673f8829a4aSRandall Stewart 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3674f8829a4aSRandall Stewart 					/*
3675e6194c2eSMichael Tuexen 					 * If the new destination is a
3676e6194c2eSMichael Tuexen 					 * LINK_LOCAL we must have common
3677e6194c2eSMichael Tuexen 					 * site scope. Don't set the local
3678e6194c2eSMichael Tuexen 					 * scope since we may not share all
3679e6194c2eSMichael Tuexen 					 * links, only loopback can do this.
3680e6194c2eSMichael Tuexen 					 * Links on the local network would
3681e6194c2eSMichael Tuexen 					 * also be on our private network
3682e6194c2eSMichael Tuexen 					 * for v4 too.
3683f8829a4aSRandall Stewart 					 */
3684a1cb341bSMichael Tuexen 					stcb->asoc.scope.ipv4_local_scope = 1;
3685a1cb341bSMichael Tuexen 					stcb->asoc.scope.site_scope = 1;
3686f8829a4aSRandall Stewart 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3687f8829a4aSRandall Stewart 					/*
3688e6194c2eSMichael Tuexen 					 * If the new destination is
3689e6194c2eSMichael Tuexen 					 * SITE_LOCAL then we must have site
3690e6194c2eSMichael Tuexen 					 * scope in common.
3691f8829a4aSRandall Stewart 					 */
3692a1cb341bSMichael Tuexen 					stcb->asoc.scope.site_scope = 1;
3693f8829a4aSRandall Stewart 				}
3694f8829a4aSRandall Stewart 			} else {
3695f8829a4aSRandall Stewart 				/* Validate the address is in scope */
3696f8829a4aSRandall Stewart 				if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3697a1cb341bSMichael Tuexen 				    (stcb->asoc.scope.loopback_scope == 0)) {
3698f8829a4aSRandall Stewart 					addr_inscope = 0;
3699f8829a4aSRandall Stewart 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3700a1cb341bSMichael Tuexen 				    (stcb->asoc.scope.local_scope == 0)) {
3701f8829a4aSRandall Stewart 					addr_inscope = 0;
3702f8829a4aSRandall Stewart 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3703a1cb341bSMichael Tuexen 				    (stcb->asoc.scope.site_scope == 0)) {
3704f8829a4aSRandall Stewart 					addr_inscope = 0;
3705f8829a4aSRandall Stewart 				}
3706f8829a4aSRandall Stewart 			}
3707e6194c2eSMichael Tuexen 			break;
3708e6194c2eSMichael Tuexen 		}
3709c54a18d2SRandall Stewart #endif
3710e6194c2eSMichael Tuexen 	default:
3711f8829a4aSRandall Stewart 		/* not supported family type */
3712f8829a4aSRandall Stewart 		return (-1);
3713f8829a4aSRandall Stewart 	}
3714b3f1ea41SRandall Stewart 	net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3715f8829a4aSRandall Stewart 	if (net == NULL) {
3716f8829a4aSRandall Stewart 		return (-1);
3717f8829a4aSRandall Stewart 	}
3718f8829a4aSRandall Stewart 	SCTP_INCR_RADDR_COUNT();
37195ba7f91fSMichael Tuexen 	memset(net, 0, sizeof(struct sctp_nets));
37206e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3721f8829a4aSRandall Stewart 	memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3722e6194c2eSMichael Tuexen 	switch (newaddr->sa_family) {
3723e6194c2eSMichael Tuexen #ifdef INET
3724e6194c2eSMichael Tuexen 	case AF_INET:
3725f8829a4aSRandall Stewart 		((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3726e6194c2eSMichael Tuexen 		break;
3727e6194c2eSMichael Tuexen #endif
3728e6194c2eSMichael Tuexen #ifdef INET6
3729e6194c2eSMichael Tuexen 	case AF_INET6:
3730f8829a4aSRandall Stewart 		((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3731e6194c2eSMichael Tuexen 		break;
3732e6194c2eSMichael Tuexen #endif
3733e6194c2eSMichael Tuexen 	default:
3734e6194c2eSMichael Tuexen 		break;
3735f8829a4aSRandall Stewart 	}
373642551e99SRandall Stewart 	net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
373742551e99SRandall Stewart 	if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3738a1cb341bSMichael Tuexen 		stcb->asoc.scope.loopback_scope = 1;
3739a1cb341bSMichael Tuexen 		stcb->asoc.scope.ipv4_local_scope = 1;
3740a1cb341bSMichael Tuexen 		stcb->asoc.scope.local_scope = 0;
3741a1cb341bSMichael Tuexen 		stcb->asoc.scope.site_scope = 1;
374242551e99SRandall Stewart 		addr_inscope = 1;
374342551e99SRandall Stewart 	}
3744f8829a4aSRandall Stewart 	net->failure_threshold = stcb->asoc.def_net_failure;
3745ca85e948SMichael Tuexen 	net->pf_threshold = stcb->asoc.def_net_pf_threshold;
3746f8829a4aSRandall Stewart 	if (addr_inscope == 0) {
3747f8829a4aSRandall Stewart 		net->dest_state = (SCTP_ADDR_REACHABLE |
3748f8829a4aSRandall Stewart 		    SCTP_ADDR_OUT_OF_SCOPE);
3749f8829a4aSRandall Stewart 	} else {
3750a5d547adSRandall Stewart 		if (from == SCTP_ADDR_IS_CONFIRMED)
3751a5d547adSRandall Stewart 			/* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3752f8829a4aSRandall Stewart 			net->dest_state = SCTP_ADDR_REACHABLE;
3753f8829a4aSRandall Stewart 		else
3754f8829a4aSRandall Stewart 			net->dest_state = SCTP_ADDR_REACHABLE |
3755f8829a4aSRandall Stewart 			    SCTP_ADDR_UNCONFIRMED;
3756f8829a4aSRandall Stewart 	}
3757108df27cSRandall Stewart 	/*
3758108df27cSRandall Stewart 	 * We set this to 0, the timer code knows that this means its an
3759108df27cSRandall Stewart 	 * initial value
3760108df27cSRandall Stewart 	 */
3761f79aab18SRandall Stewart 	net->rto_needed = 1;
3762108df27cSRandall Stewart 	net->RTO = 0;
37639a972525SRandall Stewart 	net->RTO_measured = 0;
3764f8829a4aSRandall Stewart 	stcb->asoc.numnets++;
3765c9c58059SMichael Tuexen 	net->ref_count = 1;
3766a21779f0SRandall Stewart 	net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
37677154bf4aSMichael Tuexen 	net->port = port;
3768b10f2dc8SMichael Tuexen 	net->dscp = stcb->asoc.default_dscp;
376958bdb691SMichael Tuexen #ifdef INET6
3770b10f2dc8SMichael Tuexen 	net->flowlabel = stcb->asoc.default_flowlabel;
377158bdb691SMichael Tuexen #endif
377280c79bbeSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
3773ca85e948SMichael Tuexen 		net->dest_state |= SCTP_ADDR_NOHB;
3774ca85e948SMichael Tuexen 	} else {
3775ca85e948SMichael Tuexen 		net->dest_state &= ~SCTP_ADDR_NOHB;
3776ca85e948SMichael Tuexen 	}
377780c79bbeSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
377880c79bbeSMichael Tuexen 		net->dest_state |= SCTP_ADDR_NO_PMTUD;
377980c79bbeSMichael Tuexen 	} else {
378080c79bbeSMichael Tuexen 		net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
378180c79bbeSMichael Tuexen 	}
3782ca85e948SMichael Tuexen 	net->heart_beat_delay = stcb->asoc.heart_beat_delay;
3783f8829a4aSRandall Stewart 	/* Init the timer structure */
3784139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3785139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3786ca85e948SMichael Tuexen 	SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
3787f8829a4aSRandall Stewart 
3788f8829a4aSRandall Stewart 	/* Now generate a route for this guy */
37895e2c2d87SRandall Stewart #ifdef INET6
3790f8829a4aSRandall Stewart 	/* KAME hack: embed scopeid */
3791f8829a4aSRandall Stewart 	if (newaddr->sa_family == AF_INET6) {
3792f8829a4aSRandall Stewart 		struct sockaddr_in6 *sin6;
3793f8829a4aSRandall Stewart 
3794f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3795482444b4SRandall Stewart 		(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
3796f8829a4aSRandall Stewart 		sin6->sin6_scope_id = 0;
3797f8829a4aSRandall Stewart 	}
37985e2c2d87SRandall Stewart #endif
3799d089f9b9SMichael Tuexen 	SCTP_RTALLOC((sctp_route_t *)&net->ro,
3800d089f9b9SMichael Tuexen 	    stcb->asoc.vrf_id,
3801d089f9b9SMichael Tuexen 	    stcb->sctp_ep->fibnum);
3802bff64a4dSRandall Stewart 
380310e0318aSMichael Tuexen 	net->src_addr_selected = 0;
380417205eccSRandall Stewart 	if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
380517205eccSRandall Stewart 		/* Get source address */
380617205eccSRandall Stewart 		net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
380717205eccSRandall Stewart 		    stcb,
380817205eccSRandall Stewart 		    (sctp_route_t *)&net->ro,
380917205eccSRandall Stewart 		    net,
381017205eccSRandall Stewart 		    0,
381117205eccSRandall Stewart 		    stcb->asoc.vrf_id);
381228a6addeSMichael Tuexen 		if (stcb->asoc.default_mtu > 0) {
381328a6addeSMichael Tuexen 			net->mtu = stcb->asoc.default_mtu;
381428a6addeSMichael Tuexen 			switch (net->ro._l_addr.sa.sa_family) {
381528a6addeSMichael Tuexen #ifdef INET
381628a6addeSMichael Tuexen 			case AF_INET:
381728a6addeSMichael Tuexen 				net->mtu += SCTP_MIN_V4_OVERHEAD;
381828a6addeSMichael Tuexen 				break;
381928a6addeSMichael Tuexen #endif
382028a6addeSMichael Tuexen #ifdef INET6
382128a6addeSMichael Tuexen 			case AF_INET6:
382228a6addeSMichael Tuexen 				net->mtu += SCTP_MIN_OVERHEAD;
382328a6addeSMichael Tuexen 				break;
382428a6addeSMichael Tuexen #endif
382528a6addeSMichael Tuexen 			default:
382628a6addeSMichael Tuexen 				break;
382728a6addeSMichael Tuexen 			}
382828a6addeSMichael Tuexen #if defined(INET) || defined(INET6)
382928a6addeSMichael Tuexen 			if (net->port) {
383028a6addeSMichael Tuexen 				net->mtu += (uint32_t)sizeof(struct udphdr);
383128a6addeSMichael Tuexen 			}
383228a6addeSMichael Tuexen #endif
383328a6addeSMichael Tuexen 		} else if (net->ro._s_addr != NULL) {
383410e0318aSMichael Tuexen 			uint32_t imtu, rmtu, hcmtu;
383510e0318aSMichael Tuexen 
3836f64a0b06SMichael Tuexen 			net->src_addr_selected = 1;
383717205eccSRandall Stewart 			/* Now get the interface MTU */
3838f64a0b06SMichael Tuexen 			if (net->ro._s_addr->ifn_p != NULL) {
3839274a0e4aSMichael Tuexen 				/*
3840274a0e4aSMichael Tuexen 				 * XXX: Should we here just use
3841274a0e4aSMichael Tuexen 				 * net->ro._s_addr->ifn_p->ifn_mtu
3842274a0e4aSMichael Tuexen 				 */
3843274a0e4aSMichael Tuexen 				imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p,
3844274a0e4aSMichael Tuexen 				    net->ro._s_addr->ifn_p->ifn_index);
3845f64a0b06SMichael Tuexen 			} else {
384610e0318aSMichael Tuexen 				imtu = 0;
3847f64a0b06SMichael Tuexen 			}
3848983066f0SAlexander V. Chernikov 			rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
384910e0318aSMichael Tuexen 			hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
385010e0318aSMichael Tuexen 			net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
385117205eccSRandall Stewart 		}
3852a62e467aSMichael Tuexen 	}
3853a62e467aSMichael Tuexen 	if (net->mtu == 0) {
385428a6addeSMichael Tuexen 		if (stcb->asoc.default_mtu > 0) {
385528a6addeSMichael Tuexen 			net->mtu = stcb->asoc.default_mtu;
385628a6addeSMichael Tuexen 			switch (net->ro._l_addr.sa.sa_family) {
385728a6addeSMichael Tuexen #ifdef INET
385828a6addeSMichael Tuexen 			case AF_INET:
385928a6addeSMichael Tuexen 				net->mtu += SCTP_MIN_V4_OVERHEAD;
386028a6addeSMichael Tuexen 				break;
386128a6addeSMichael Tuexen #endif
386228a6addeSMichael Tuexen #ifdef INET6
386328a6addeSMichael Tuexen 			case AF_INET6:
386428a6addeSMichael Tuexen 				net->mtu += SCTP_MIN_OVERHEAD;
386528a6addeSMichael Tuexen 				break;
386628a6addeSMichael Tuexen #endif
386728a6addeSMichael Tuexen 			default:
386828a6addeSMichael Tuexen 				break;
386928a6addeSMichael Tuexen 			}
387028a6addeSMichael Tuexen #if defined(INET) || defined(INET6)
387128a6addeSMichael Tuexen 			if (net->port) {
387228a6addeSMichael Tuexen 				net->mtu += (uint32_t)sizeof(struct udphdr);
387328a6addeSMichael Tuexen 			}
387428a6addeSMichael Tuexen #endif
387528a6addeSMichael Tuexen 		} else {
3876a62e467aSMichael Tuexen 			switch (newaddr->sa_family) {
3877a62e467aSMichael Tuexen #ifdef INET
3878a62e467aSMichael Tuexen 			case AF_INET:
3879a62e467aSMichael Tuexen 				net->mtu = SCTP_DEFAULT_MTU;
3880a62e467aSMichael Tuexen 				break;
3881a62e467aSMichael Tuexen #endif
3882a62e467aSMichael Tuexen #ifdef INET6
3883a62e467aSMichael Tuexen 			case AF_INET6:
3884a62e467aSMichael Tuexen 				net->mtu = 1280;
3885a62e467aSMichael Tuexen 				break;
3886a62e467aSMichael Tuexen #endif
3887a62e467aSMichael Tuexen 			default:
3888a62e467aSMichael Tuexen 				break;
3889a62e467aSMichael Tuexen 			}
3890a62e467aSMichael Tuexen 		}
389128a6addeSMichael Tuexen 	}
38924474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
3893a62e467aSMichael Tuexen 	if (net->port) {
3894a62e467aSMichael Tuexen 		net->mtu -= (uint32_t)sizeof(struct udphdr);
3895a62e467aSMichael Tuexen 	}
38964474d71aSMichael Tuexen #endif
3897a5d547adSRandall Stewart 	if (from == SCTP_ALLOC_ASOC) {
3898f8829a4aSRandall Stewart 		stcb->asoc.smallest_mtu = net->mtu;
3899f8829a4aSRandall Stewart 	}
3900a62e467aSMichael Tuexen 	if (stcb->asoc.smallest_mtu > net->mtu) {
39012de2ae33SMichael Tuexen 		sctp_pathmtu_adjustment(stcb, net->mtu, true);
3902f8829a4aSRandall Stewart 	}
3903c54a18d2SRandall Stewart #ifdef INET6
3904c54a18d2SRandall Stewart 	if (newaddr->sa_family == AF_INET6) {
3905c54a18d2SRandall Stewart 		struct sockaddr_in6 *sin6;
3906c54a18d2SRandall Stewart 
3907c54a18d2SRandall Stewart 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3908c54a18d2SRandall Stewart 		(void)sa6_recoverscope(sin6);
3909c54a18d2SRandall Stewart 	}
3910c54a18d2SRandall Stewart #endif
3911a62e467aSMichael Tuexen 
3912b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
3913299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
3914299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
3915f8829a4aSRandall Stewart 
3916f8829a4aSRandall Stewart 	/*
3917f8829a4aSRandall Stewart 	 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3918f8829a4aSRandall Stewart 	 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3919f8829a4aSRandall Stewart 	 */
3920f8829a4aSRandall Stewart 	net->find_pseudo_cumack = 1;
3921f8829a4aSRandall Stewart 	net->find_rtx_pseudo_cumack = 1;
39224c97400fSMichael Tuexen 	/* Choose an initial flowid. */
39234c97400fSMichael Tuexen 	net->flowid = stcb->asoc.my_vtag ^
39244c97400fSMichael Tuexen 	    ntohs(stcb->rport) ^
39254c97400fSMichael Tuexen 	    ntohs(stcb->sctp_ep->sctp_lport);
392636ad8372SSepherosa Ziehau 	net->flowtype = M_HASHTYPE_OPAQUE_HASH;
39277ffa2290SMichael Tuexen 	if (netp) {
39287ffa2290SMichael Tuexen 		*netp = net;
39297ffa2290SMichael Tuexen 	}
3930f8829a4aSRandall Stewart 	netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3931983066f0SAlexander V. Chernikov 	if (net->ro.ro_nh == NULL) {
3932f8829a4aSRandall Stewart 		/* Since we have no route put it at the back */
3933f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3934f8829a4aSRandall Stewart 	} else if (netfirst == NULL) {
3935f8829a4aSRandall Stewart 		/* We are the first one in the pool. */
3936f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3937983066f0SAlexander V. Chernikov 	} else if (netfirst->ro.ro_nh == NULL) {
3938f8829a4aSRandall Stewart 		/*
3939f8829a4aSRandall Stewart 		 * First one has NO route. Place this one ahead of the first
3940f8829a4aSRandall Stewart 		 * one.
3941f8829a4aSRandall Stewart 		 */
3942f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3943983066f0SAlexander V. Chernikov 	} else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
3944f8829a4aSRandall Stewart 		/*
3945f8829a4aSRandall Stewart 		 * This one has a different interface than the one at the
3946f8829a4aSRandall Stewart 		 * top of the list. Place it ahead.
3947f8829a4aSRandall Stewart 		 */
3948f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3949f8829a4aSRandall Stewart 	} else {
3950f8829a4aSRandall Stewart 		/*
3951f8829a4aSRandall Stewart 		 * Ok we have the same interface as the first one. Move
3952f8829a4aSRandall Stewart 		 * forward until we find either a) one with a NULL route...
3953f8829a4aSRandall Stewart 		 * insert ahead of that b) one with a different ifp.. insert
3954f8829a4aSRandall Stewart 		 * after that. c) end of the list.. insert at the tail.
3955f8829a4aSRandall Stewart 		 */
3956f8829a4aSRandall Stewart 		struct sctp_nets *netlook;
3957f8829a4aSRandall Stewart 
3958f8829a4aSRandall Stewart 		do {
3959f8829a4aSRandall Stewart 			netlook = TAILQ_NEXT(netfirst, sctp_next);
3960f8829a4aSRandall Stewart 			if (netlook == NULL) {
3961f8829a4aSRandall Stewart 				/* End of the list */
3962bff64a4dSRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3963f8829a4aSRandall Stewart 				break;
3964983066f0SAlexander V. Chernikov 			} else if (netlook->ro.ro_nh == NULL) {
3965f8829a4aSRandall Stewart 				/* next one has NO route */
3966f8829a4aSRandall Stewart 				TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3967f8829a4aSRandall Stewart 				break;
3968983066f0SAlexander V. Chernikov 			} else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
3969f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3970f8829a4aSRandall Stewart 				    net, sctp_next);
3971f8829a4aSRandall Stewart 				break;
3972f8829a4aSRandall Stewart 			}
3973f8829a4aSRandall Stewart 			/* Shift forward */
3974f8829a4aSRandall Stewart 			netfirst = netlook;
3975f8829a4aSRandall Stewart 		} while (netlook != NULL);
3976f8829a4aSRandall Stewart 	}
3977f8829a4aSRandall Stewart 
3978f8829a4aSRandall Stewart 	/* got to have a primary set */
3979f8829a4aSRandall Stewart 	if (stcb->asoc.primary_destination == 0) {
3980f8829a4aSRandall Stewart 		stcb->asoc.primary_destination = net;
3981983066f0SAlexander V. Chernikov 	} else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
3982983066f0SAlexander V. Chernikov 		    (net->ro.ro_nh) &&
3983f8829a4aSRandall Stewart 	    ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3984f8829a4aSRandall Stewart 		/* No route to current primary adopt new primary */
3985f8829a4aSRandall Stewart 		stcb->asoc.primary_destination = net;
3986f8829a4aSRandall Stewart 	}
3987f8829a4aSRandall Stewart 	/* Validate primary is first */
3988f8829a4aSRandall Stewart 	net = TAILQ_FIRST(&stcb->asoc.nets);
3989f8829a4aSRandall Stewart 	if ((net != stcb->asoc.primary_destination) &&
3990f8829a4aSRandall Stewart 	    (stcb->asoc.primary_destination)) {
3991f8829a4aSRandall Stewart 		/*
3992f8829a4aSRandall Stewart 		 * first one on the list is NOT the primary sctp_cmpaddr()
3993b1ce21c6SRebecca Cran 		 * is much more efficient if the primary is the first on the
3994f8829a4aSRandall Stewart 		 * list, make it so.
3995f8829a4aSRandall Stewart 		 */
3996f8829a4aSRandall Stewart 		TAILQ_REMOVE(&stcb->asoc.nets,
3997f8829a4aSRandall Stewart 		    stcb->asoc.primary_destination, sctp_next);
3998f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3999f8829a4aSRandall Stewart 		    stcb->asoc.primary_destination, sctp_next);
4000f8829a4aSRandall Stewart 	}
4001f8829a4aSRandall Stewart 	return (0);
4002f8829a4aSRandall Stewart }
4003f8829a4aSRandall Stewart 
4004830d754dSRandall Stewart static uint32_t
sctp_aloc_a_assoc_id(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4005b0471b4bSMichael Tuexen sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4006b0471b4bSMichael Tuexen {
4007830d754dSRandall Stewart 	uint32_t id;
4008830d754dSRandall Stewart 	struct sctpasochead *head;
4009830d754dSRandall Stewart 	struct sctp_tcb *lstcb;
4010830d754dSRandall Stewart 
4011830d754dSRandall Stewart try_again:
4012830d754dSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4013830d754dSRandall Stewart 		/* TSNH */
4014830d754dSRandall Stewart 		return (0);
4015830d754dSRandall Stewart 	}
4016f3d06a3cSRandall Stewart 	/*
4017e2e7c62eSMichael Tuexen 	 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4018e2e7c62eSMichael Tuexen 	 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4019f3d06a3cSRandall Stewart 	 */
4020e2e7c62eSMichael Tuexen 	if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4021e2e7c62eSMichael Tuexen 		inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4022f3d06a3cSRandall Stewart 	}
4023830d754dSRandall Stewart 	id = inp->sctp_associd_counter;
4024830d754dSRandall Stewart 	inp->sctp_associd_counter++;
4025830d754dSRandall Stewart 	lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4026830d754dSRandall Stewart 	if (lstcb) {
4027830d754dSRandall Stewart 		goto try_again;
4028830d754dSRandall Stewart 	}
4029830d754dSRandall Stewart 	head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4030830d754dSRandall Stewart 	LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4031830d754dSRandall Stewart 	stcb->asoc.in_asocid_hash = 1;
4032be62c88bSMichael Tuexen 	return (id);
4033830d754dSRandall Stewart }
4034830d754dSRandall Stewart 
4035f8829a4aSRandall Stewart /*
4036f8829a4aSRandall Stewart  * allocate an association and add it to the endpoint. The caller must be
4037f8829a4aSRandall Stewart  * careful to add all additional addresses once they are know right away or
4038f8829a4aSRandall Stewart  * else the assoc will be may experience a blackout scenario.
4039f8829a4aSRandall Stewart  */
40402d5c48ecSMark Johnston static struct sctp_tcb *
sctp_aloc_assoc_locked(struct sctp_inpcb * inp,struct sockaddr * firstaddr,int * error,uint32_t override_tag,uint32_t initial_tsn,uint32_t vrf_id,uint16_t o_streams,uint16_t port,struct thread * p,int initialize_auth_params)40412d5c48ecSMark Johnston sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4042c7f048abSMichael Tuexen     int *error, uint32_t override_tag, uint32_t initial_tsn,
4043c7f048abSMichael Tuexen     uint32_t vrf_id, uint16_t o_streams, uint16_t port,
40448a956abeSMichael Tuexen     struct thread *p,
40458a956abeSMichael Tuexen     int initialize_auth_params)
4046f8829a4aSRandall Stewart {
404752be287eSRandall Stewart 	/* note the p argument is only valid in unbound sockets */
404852be287eSRandall Stewart 
4049f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
4050f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4051f8829a4aSRandall Stewart 	struct sctpasochead *head;
4052f8829a4aSRandall Stewart 	uint16_t rport;
4053f8829a4aSRandall Stewart 	int err;
4054f8829a4aSRandall Stewart 
40552d5c48ecSMark Johnston 	SCTP_INP_INFO_WLOCK_ASSERT();
40562d5c48ecSMark Johnston 	SCTP_INP_WLOCK_ASSERT(inp);
40572d5c48ecSMark Johnston 
4058f8829a4aSRandall Stewart 	/*
4059f8829a4aSRandall Stewart 	 * Assumption made here: Caller has done a
4060f8829a4aSRandall Stewart 	 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4061f8829a4aSRandall Stewart 	 * address does not exist already.
4062f8829a4aSRandall Stewart 	 */
4063b3f1ea41SRandall Stewart 	if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4064f8829a4aSRandall Stewart 		/* Hit max assoc, sorry no more */
4065c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4066f8829a4aSRandall Stewart 		*error = ENOBUFS;
4067f8829a4aSRandall Stewart 		return (NULL);
4068f8829a4aSRandall Stewart 	}
4069ad81507eSRandall Stewart 	if (firstaddr == NULL) {
4070c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4071ad81507eSRandall Stewart 		*error = EINVAL;
4072ad81507eSRandall Stewart 		return (NULL);
4073ad81507eSRandall Stewart 	}
40742d5c48ecSMark Johnston 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
40752d5c48ecSMark Johnston 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
40762d5c48ecSMark Johnston 		*error = EINVAL;
40772d5c48ecSMark Johnston 		return (NULL);
40782d5c48ecSMark Johnston 	}
40796d9e8f2bSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
40806d9e8f2bSRandall Stewart 	    ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
40816d9e8f2bSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4082f8829a4aSRandall Stewart 		/*
4083f8829a4aSRandall Stewart 		 * If its in the TCP pool, its NOT allowed to create an
4084f8829a4aSRandall Stewart 		 * association. The parent listener needs to call
4085f8829a4aSRandall Stewart 		 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4086f8829a4aSRandall Stewart 		 * off, or connected one does this.. its an error.
4087f8829a4aSRandall Stewart 		 */
4088c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4089f8829a4aSRandall Stewart 		*error = EINVAL;
4090f8829a4aSRandall Stewart 		return (NULL);
4091f8829a4aSRandall Stewart 	}
4092c55b70ceSMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4093c55b70ceSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4094c55b70ceSMichael Tuexen 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4095c55b70ceSMichael Tuexen 		    (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4096c55b70ceSMichael Tuexen 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4097c55b70ceSMichael Tuexen 			*error = EINVAL;
4098c55b70ceSMichael Tuexen 			return (NULL);
4099c55b70ceSMichael Tuexen 		}
4100c55b70ceSMichael Tuexen 	}
4101ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4102f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
41036e55db54SRandall Stewart 	if (firstaddr) {
4104ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4105e6194c2eSMichael Tuexen 		switch (firstaddr->sa_family) {
4106e6194c2eSMichael Tuexen #ifdef INET
4107e6194c2eSMichael Tuexen 		case AF_INET:
4108ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4109f8829a4aSRandall Stewart 			    ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4110e6194c2eSMichael Tuexen 			break;
4111e6194c2eSMichael Tuexen #endif
4112e6194c2eSMichael Tuexen #ifdef INET6
4113e6194c2eSMichael Tuexen 		case AF_INET6:
4114e6194c2eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4115e6194c2eSMichael Tuexen 			    ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4116e6194c2eSMichael Tuexen 			break;
4117e6194c2eSMichael Tuexen #endif
4118e6194c2eSMichael Tuexen 		default:
4119e6194c2eSMichael Tuexen 			break;
4120e6194c2eSMichael Tuexen 		}
4121ad81507eSRandall Stewart 	} else {
4122ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4123f8829a4aSRandall Stewart 	}
4124f8829a4aSRandall Stewart #endif				/* SCTP_DEBUG */
4125e6194c2eSMichael Tuexen 	switch (firstaddr->sa_family) {
4126e6194c2eSMichael Tuexen #ifdef INET
4127e6194c2eSMichael Tuexen 	case AF_INET:
4128e6194c2eSMichael Tuexen 		{
4129f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4130f8829a4aSRandall Stewart 
4131f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)firstaddr;
413292776dfdSMichael Tuexen 			if ((ntohs(sin->sin_port) == 0) ||
4133532106f7SGleb Smirnoff 			    in_broadcast(sin->sin_addr) ||
4134b15f5411SMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
41359b2a35b3SMichael Tuexen 			    ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
4136b15f5411SMichael Tuexen 			    (SCTP_IPV6_V6ONLY(inp) != 0))) {
4137f8829a4aSRandall Stewart 				/* Invalid address */
4138c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4139f8829a4aSRandall Stewart 				*error = EINVAL;
4140f8829a4aSRandall Stewart 				return (NULL);
4141f8829a4aSRandall Stewart 			}
4142f8829a4aSRandall Stewart 			rport = sin->sin_port;
4143e6194c2eSMichael Tuexen 			break;
4144e6194c2eSMichael Tuexen 		}
4145e6194c2eSMichael Tuexen #endif
4146e6194c2eSMichael Tuexen #ifdef INET6
4147e6194c2eSMichael Tuexen 	case AF_INET6:
4148e6194c2eSMichael Tuexen 		{
4149f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6;
4150f8829a4aSRandall Stewart 
4151f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)firstaddr;
415292776dfdSMichael Tuexen 			if ((ntohs(sin6->sin6_port) == 0) ||
415392776dfdSMichael Tuexen 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4154b15f5411SMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
4155b15f5411SMichael Tuexen 			    ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
4156f8829a4aSRandall Stewart 				/* Invalid address */
4157c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4158f8829a4aSRandall Stewart 				*error = EINVAL;
4159f8829a4aSRandall Stewart 				return (NULL);
4160f8829a4aSRandall Stewart 			}
4161f8829a4aSRandall Stewart 			rport = sin6->sin6_port;
4162e6194c2eSMichael Tuexen 			break;
4163e6194c2eSMichael Tuexen 		}
4164e6194c2eSMichael Tuexen #endif
4165e6194c2eSMichael Tuexen 	default:
4166f8829a4aSRandall Stewart 		/* not supported family type */
4167c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4168f8829a4aSRandall Stewart 		*error = EINVAL;
4169f8829a4aSRandall Stewart 		return (NULL);
4170f8829a4aSRandall Stewart 	}
4171f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4172f8829a4aSRandall Stewart 		/*
4173f8829a4aSRandall Stewart 		 * If you have not performed a bind, then we need to do the
4174b1ce21c6SRebecca Cran 		 * ephemeral bind for you.
4175f8829a4aSRandall Stewart 		 */
41762d5c48ecSMark Johnston 		if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
4177f8829a4aSRandall Stewart 			/* bind error, probably perm */
4178f8829a4aSRandall Stewart 			*error = err;
4179f8829a4aSRandall Stewart 			return (NULL);
4180f8829a4aSRandall Stewart 		}
4181f8829a4aSRandall Stewart 	}
4182b3f1ea41SRandall Stewart 	stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4183f8829a4aSRandall Stewart 	if (stcb == NULL) {
4184f8829a4aSRandall Stewart 		/* out of memory? */
4185c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4186f8829a4aSRandall Stewart 		*error = ENOMEM;
4187f8829a4aSRandall Stewart 		return (NULL);
4188f8829a4aSRandall Stewart 	}
4189f8829a4aSRandall Stewart 	SCTP_INCR_ASOC_COUNT();
4190f8829a4aSRandall Stewart 
41915ba7f91fSMichael Tuexen 	memset(stcb, 0, sizeof(*stcb));
4192f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4193830d754dSRandall Stewart 
4194f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_INIT(stcb);
4195830d754dSRandall Stewart 	stcb->rport = rport;
4196f8829a4aSRandall Stewart 	/* setup back pointer's */
4197f8829a4aSRandall Stewart 	stcb->sctp_ep = inp;
4198f8829a4aSRandall Stewart 	stcb->sctp_socket = inp->sctp_socket;
4199c7f048abSMichael Tuexen 	if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
4200f8829a4aSRandall Stewart 		/* failed */
4201f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_DESTROY(stcb);
4202b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4203f8829a4aSRandall Stewart 		SCTP_DECR_ASOC_COUNT();
4204f8829a4aSRandall Stewart 		*error = err;
4205f8829a4aSRandall Stewart 		return (NULL);
4206f8829a4aSRandall Stewart 	}
4207f8829a4aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
4208f8829a4aSRandall Stewart 
4209be62c88bSMichael Tuexen 	asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4210f8829a4aSRandall Stewart 	/* now that my_vtag is set, add it to the hash */
4211830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4212f8829a4aSRandall Stewart 	/* put it in the bucket in the vtag hash of assoc's for the system */
4213f8829a4aSRandall Stewart 	LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4214f8829a4aSRandall Stewart 
42156176f9d6SMichael Tuexen 	if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
4216f8829a4aSRandall Stewart 		/* failure.. memory error? */
4217207304d4SRandall Stewart 		if (asoc->strmout) {
4218207304d4SRandall Stewart 			SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4219207304d4SRandall Stewart 			asoc->strmout = NULL;
4220207304d4SRandall Stewart 		}
4221207304d4SRandall Stewart 		if (asoc->mapping_array) {
4222207304d4SRandall Stewart 			SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4223207304d4SRandall Stewart 			asoc->mapping_array = NULL;
4224207304d4SRandall Stewart 		}
4225bf1be571SRandall Stewart 		if (asoc->nr_mapping_array) {
4226bf1be571SRandall Stewart 			SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4227bf1be571SRandall Stewart 			asoc->nr_mapping_array = NULL;
4228bf1be571SRandall Stewart 		}
4229f8829a4aSRandall Stewart 		SCTP_DECR_ASOC_COUNT();
42303b3d05d7SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
4231f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_DESTROY(stcb);
42322d5c48ecSMark Johnston 		LIST_REMOVE(stcb, sctp_asocs);
4233830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4234b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4235c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4236f8829a4aSRandall Stewart 		*error = ENOBUFS;
4237f8829a4aSRandall Stewart 		return (NULL);
4238f8829a4aSRandall Stewart 	}
4239f8829a4aSRandall Stewart 	/* Init all the timers */
4240139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4241139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4242139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4243139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4244139bc87fSRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4245851b7298SRandall Stewart 	SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4246139bc87fSRandall Stewart 
4247f8829a4aSRandall Stewart 	LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4248f8829a4aSRandall Stewart 	/* now file the port under the hash as well */
4249f8829a4aSRandall Stewart 	if (inp->sctp_tcbhash != NULL) {
4250f8829a4aSRandall Stewart 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4251f8829a4aSRandall Stewart 		    inp->sctp_hashmark)];
4252f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4253f8829a4aSRandall Stewart 	}
42548a956abeSMichael Tuexen 	if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
42558a956abeSMichael Tuexen 		sctp_initialize_auth_params(inp, stcb);
42568a956abeSMichael Tuexen 	}
4257dd294dceSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
4258f8829a4aSRandall Stewart 	return (stcb);
4259f8829a4aSRandall Stewart }
4260f8829a4aSRandall Stewart 
42612d5c48ecSMark Johnston struct sctp_tcb *
sctp_aloc_assoc(struct sctp_inpcb * inp,struct sockaddr * firstaddr,int * error,uint32_t override_tag,uint32_t initial_tsn,uint32_t vrf_id,uint16_t o_streams,uint16_t port,struct thread * p,int initialize_auth_params)42622d5c48ecSMark Johnston sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
42632d5c48ecSMark Johnston     int *error, uint32_t override_tag, uint32_t initial_tsn,
42642d5c48ecSMark Johnston     uint32_t vrf_id, uint16_t o_streams, uint16_t port,
42652d5c48ecSMark Johnston     struct thread *p,
42662d5c48ecSMark Johnston     int initialize_auth_params)
42672d5c48ecSMark Johnston {
42682d5c48ecSMark Johnston 	struct sctp_tcb *stcb;
42692d5c48ecSMark Johnston 
42702d5c48ecSMark Johnston 	SCTP_INP_INFO_WLOCK();
42712d5c48ecSMark Johnston 	SCTP_INP_WLOCK(inp);
42722d5c48ecSMark Johnston 	stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
42732d5c48ecSMark Johnston 	    initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
42742d5c48ecSMark Johnston 	SCTP_INP_INFO_WUNLOCK();
42752d5c48ecSMark Johnston 	SCTP_INP_WUNLOCK(inp);
42762d5c48ecSMark Johnston 	return (stcb);
42772d5c48ecSMark Johnston }
42782d5c48ecSMark Johnston 
42792d5c48ecSMark Johnston struct sctp_tcb *
sctp_aloc_assoc_connected(struct sctp_inpcb * inp,struct sockaddr * firstaddr,int * error,uint32_t override_tag,uint32_t initial_tsn,uint32_t vrf_id,uint16_t o_streams,uint16_t port,struct thread * p,int initialize_auth_params)42802d5c48ecSMark Johnston sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
42812d5c48ecSMark Johnston     int *error, uint32_t override_tag, uint32_t initial_tsn,
42822d5c48ecSMark Johnston     uint32_t vrf_id, uint16_t o_streams, uint16_t port,
42832d5c48ecSMark Johnston     struct thread *p,
42842d5c48ecSMark Johnston     int initialize_auth_params)
42852d5c48ecSMark Johnston {
42862d5c48ecSMark Johnston 	struct sctp_tcb *stcb;
42872d5c48ecSMark Johnston 
42882d5c48ecSMark Johnston 	SCTP_INP_INFO_WLOCK();
42892d5c48ecSMark Johnston 	SCTP_INP_WLOCK(inp);
42902d5c48ecSMark Johnston 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
42912d5c48ecSMark Johnston 	    SCTP_IS_LISTENING(inp)) {
42922d5c48ecSMark Johnston 		SCTP_INP_INFO_WUNLOCK();
42932d5c48ecSMark Johnston 		SCTP_INP_WUNLOCK(inp);
42942d5c48ecSMark Johnston 		*error = EINVAL;
42952d5c48ecSMark Johnston 		return (NULL);
42962d5c48ecSMark Johnston 	}
42972d5c48ecSMark Johnston 	stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
42982d5c48ecSMark Johnston 	    initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
42992d5c48ecSMark Johnston 	SCTP_INP_INFO_WUNLOCK();
43002d5c48ecSMark Johnston 	if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
43012d5c48ecSMark Johnston 		inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
43022d5c48ecSMark Johnston 		soisconnecting(inp->sctp_socket);
43032d5c48ecSMark Johnston 	}
43042d5c48ecSMark Johnston 	SCTP_INP_WUNLOCK(inp);
43052d5c48ecSMark Johnston 	return (stcb);
43062d5c48ecSMark Johnston }
43072d5c48ecSMark Johnston 
4308f8829a4aSRandall Stewart void
sctp_remove_net(struct sctp_tcb * stcb,struct sctp_nets * net)4309f8829a4aSRandall Stewart sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4310f8829a4aSRandall Stewart {
43116a34ec63SMichael Tuexen 	struct sctp_inpcb *inp;
4312f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4313f8829a4aSRandall Stewart 
43146a34ec63SMichael Tuexen 	inp = stcb->sctp_ep;
4315f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4316f8829a4aSRandall Stewart 	asoc->numnets--;
4317f8829a4aSRandall Stewart 	TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4318f8829a4aSRandall Stewart 	if (net == asoc->primary_destination) {
4319f8829a4aSRandall Stewart 		/* Reset primary */
4320f8829a4aSRandall Stewart 		struct sctp_nets *lnet;
4321f8829a4aSRandall Stewart 
4322f8829a4aSRandall Stewart 		lnet = TAILQ_FIRST(&asoc->nets);
4323851b7298SRandall Stewart 		/*
4324851b7298SRandall Stewart 		 * Mobility adaptation Ideally, if deleted destination is
4325851b7298SRandall Stewart 		 * the primary, it becomes a fast retransmission trigger by
4326851b7298SRandall Stewart 		 * the subsequent SET PRIMARY. (by micchie)
4327851b7298SRandall Stewart 		 */
4328851b7298SRandall Stewart 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4329b201f536SRandall Stewart 		    SCTP_MOBILITY_BASE) ||
4330b201f536SRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
4331851b7298SRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) {
4332851b7298SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4333851b7298SRandall Stewart 			if (asoc->deleted_primary != NULL) {
4334851b7298SRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4335c54a18d2SRandall Stewart 				goto out;
4336851b7298SRandall Stewart 			}
4337851b7298SRandall Stewart 			asoc->deleted_primary = net;
4338851b7298SRandall Stewart 			atomic_add_int(&net->ref_count, 1);
4339d55b0b1bSRandall Stewart 			memset(&net->lastsa, 0, sizeof(net->lastsa));
4340d55b0b1bSRandall Stewart 			memset(&net->lastsv, 0, sizeof(net->lastsv));
4341851b7298SRandall Stewart 			sctp_mobility_feature_on(stcb->sctp_ep,
4342851b7298SRandall Stewart 			    SCTP_MOBILITY_PRIM_DELETED);
4343851b7298SRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4344851b7298SRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
4345851b7298SRandall Stewart 		}
4346c54a18d2SRandall Stewart out:
4347f8829a4aSRandall Stewart 		/* Try to find a confirmed primary */
434842551e99SRandall Stewart 		asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4349f8829a4aSRandall Stewart 	}
4350f8829a4aSRandall Stewart 	if (net == asoc->last_data_chunk_from) {
4351f8829a4aSRandall Stewart 		/* Reset primary */
4352f8829a4aSRandall Stewart 		asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4353f8829a4aSRandall Stewart 	}
4354f8829a4aSRandall Stewart 	if (net == asoc->last_control_chunk_from) {
4355f8829a4aSRandall Stewart 		/* Clear net */
4356f8829a4aSRandall Stewart 		asoc->last_control_chunk_from = NULL;
4357f8829a4aSRandall Stewart 	}
43585ac83902SMichael Tuexen 	if (net == asoc->last_net_cmt_send_started) {
43595ac83902SMichael Tuexen 		/* Clear net */
43605ac83902SMichael Tuexen 		asoc->last_net_cmt_send_started = NULL;
43615ac83902SMichael Tuexen 	}
4362ca85e948SMichael Tuexen 	if (net == stcb->asoc.alternate) {
4363ca85e948SMichael Tuexen 		sctp_free_remote_addr(stcb->asoc.alternate);
4364ca85e948SMichael Tuexen 		stcb->asoc.alternate = NULL;
4365ca85e948SMichael Tuexen 	}
43666a34ec63SMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
43676a34ec63SMichael Tuexen 	    SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
43686a34ec63SMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
43696a34ec63SMichael Tuexen 	    SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
43709803f01cSMichael Tuexen 	net->dest_state |= SCTP_ADDR_BEING_DELETED;
437142551e99SRandall Stewart 	sctp_free_remote_addr(net);
4372f8829a4aSRandall Stewart }
4373f8829a4aSRandall Stewart 
4374f8829a4aSRandall Stewart /*
4375f8829a4aSRandall Stewart  * remove a remote endpoint address from an association, it will fail if the
4376f8829a4aSRandall Stewart  * address does not exist.
4377f8829a4aSRandall Stewart  */
4378f8829a4aSRandall Stewart int
sctp_del_remote_addr(struct sctp_tcb * stcb,struct sockaddr * remaddr)4379f8829a4aSRandall Stewart sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4380f8829a4aSRandall Stewart {
4381f8829a4aSRandall Stewart 	/*
4382f8829a4aSRandall Stewart 	 * Here we need to remove a remote address. This is quite simple, we
4383f8829a4aSRandall Stewart 	 * first find it in the list of address for the association
4384f8829a4aSRandall Stewart 	 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4385f8829a4aSRandall Stewart 	 * on that item. Note we do not allow it to be removed if there are
4386f8829a4aSRandall Stewart 	 * no other addresses.
4387f8829a4aSRandall Stewart 	 */
4388f8829a4aSRandall Stewart 	struct sctp_association *asoc;
43894a9ef3f8SMichael Tuexen 	struct sctp_nets *net, *nnet;
4390f8829a4aSRandall Stewart 
4391f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4392f8829a4aSRandall Stewart 
4393f8829a4aSRandall Stewart 	/* locate the address */
43944a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4395f8829a4aSRandall Stewart 		if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4396f8829a4aSRandall Stewart 			continue;
4397f8829a4aSRandall Stewart 		}
4398f8829a4aSRandall Stewart 		if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4399f8829a4aSRandall Stewart 		    remaddr)) {
4400f8829a4aSRandall Stewart 			/* we found the guy */
4401f8829a4aSRandall Stewart 			if (asoc->numnets < 2) {
4402f8829a4aSRandall Stewart 				/* Must have at LEAST two remote addresses */
4403f8829a4aSRandall Stewart 				return (-1);
4404f8829a4aSRandall Stewart 			} else {
4405f8829a4aSRandall Stewart 				sctp_remove_net(stcb, net);
4406f8829a4aSRandall Stewart 				return (0);
4407f8829a4aSRandall Stewart 			}
4408f8829a4aSRandall Stewart 		}
4409f8829a4aSRandall Stewart 	}
4410f8829a4aSRandall Stewart 	/* not found. */
4411f8829a4aSRandall Stewart 	return (-2);
4412f8829a4aSRandall Stewart }
4413f8829a4aSRandall Stewart 
441458a7bf12SMichael Tuexen static bool
sctp_is_in_timewait(uint32_t tag,uint16_t lport,uint16_t rport,time_t now)441542aeb8d4SMichael Tuexen sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, time_t now)
4416851b7298SRandall Stewart {
4417851b7298SRandall Stewart 	struct sctpvtaghead *chain;
4418851b7298SRandall Stewart 	struct sctp_tagblock *twait_block;
4419851b7298SRandall Stewart 	int i;
4420851b7298SRandall Stewart 
44212d5c48ecSMark Johnston 	SCTP_INP_INFO_LOCK_ASSERT();
4422b3f1ea41SRandall Stewart 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4423851b7298SRandall Stewart 	LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4424851b7298SRandall Stewart 		for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
442558a7bf12SMichael Tuexen 			if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
442658a7bf12SMichael Tuexen 			    (twait_block->vtag_block[i].v_tag == tag) &&
4427830d754dSRandall Stewart 			    (twait_block->vtag_block[i].lport == lport) &&
4428830d754dSRandall Stewart 			    (twait_block->vtag_block[i].rport == rport)) {
442958a7bf12SMichael Tuexen 				return (true);
4430851b7298SRandall Stewart 			}
4431851b7298SRandall Stewart 		}
4432851b7298SRandall Stewart 	}
443358a7bf12SMichael Tuexen 	return (false);
4434851b7298SRandall Stewart }
4435f8829a4aSRandall Stewart 
443658a7bf12SMichael Tuexen static void
sctp_set_vtag_block(struct sctp_timewait * vtag_block,time_t time,uint32_t tag,uint16_t lport,uint16_t rport)443742aeb8d4SMichael Tuexen sctp_set_vtag_block(struct sctp_timewait *vtag_block, time_t time,
443858a7bf12SMichael Tuexen     uint32_t tag, uint16_t lport, uint16_t rport)
4439fb8fb8f8SRandall Stewart {
444058a7bf12SMichael Tuexen 	vtag_block->tv_sec_at_expire = time;
444158a7bf12SMichael Tuexen 	vtag_block->v_tag = tag;
444258a7bf12SMichael Tuexen 	vtag_block->lport = lport;
444358a7bf12SMichael Tuexen 	vtag_block->rport = rport;
4444fb8fb8f8SRandall Stewart }
4445fb8fb8f8SRandall Stewart 
444658a7bf12SMichael Tuexen static void
sctp_add_vtag_to_timewait(uint32_t tag,uint16_t lport,uint16_t rport)444758a7bf12SMichael Tuexen sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4448f8829a4aSRandall Stewart {
4449f8829a4aSRandall Stewart 	struct sctpvtaghead *chain;
4450f8829a4aSRandall Stewart 	struct sctp_tagblock *twait_block;
4451f8829a4aSRandall Stewart 	struct timeval now;
445242aeb8d4SMichael Tuexen 	time_t time;
445358a7bf12SMichael Tuexen 	int i;
445458a7bf12SMichael Tuexen 	bool set;
4455f8829a4aSRandall Stewart 
445658a7bf12SMichael Tuexen 	SCTP_INP_INFO_WLOCK_ASSERT();
44576e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
445842aeb8d4SMichael Tuexen 	time = now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
4459b3f1ea41SRandall Stewart 	chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
446058a7bf12SMichael Tuexen 	set = false;
4461f8829a4aSRandall Stewart 	LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
446256f778aaSMichael Tuexen 		/* Block(s) present, lets find space, and expire on the fly */
4463f8829a4aSRandall Stewart 		for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
446458a7bf12SMichael Tuexen 			if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
446558a7bf12SMichael Tuexen 				sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
446658a7bf12SMichael Tuexen 				set = true;
446758a7bf12SMichael Tuexen 				continue;
446858a7bf12SMichael Tuexen 			}
446958a7bf12SMichael Tuexen 			if ((twait_block->vtag_block[i].v_tag != 0) &&
447042aeb8d4SMichael Tuexen 			    (twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
447158a7bf12SMichael Tuexen 				if (set) {
4472f8829a4aSRandall Stewart 					/* Audit expires this guy */
447358a7bf12SMichael Tuexen 					sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
447458a7bf12SMichael Tuexen 				} else {
447558a7bf12SMichael Tuexen 					/* Reuse it for the new tag */
447658a7bf12SMichael Tuexen 					sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
447758a7bf12SMichael Tuexen 					set = true;
4478f8829a4aSRandall Stewart 				}
4479f8829a4aSRandall Stewart 			}
4480f8829a4aSRandall Stewart 		}
4481f8829a4aSRandall Stewart 		if (set) {
4482f8829a4aSRandall Stewart 			/*
448356f778aaSMichael Tuexen 			 * We only do up to the block where we can place our
448456f778aaSMichael Tuexen 			 * tag for audits
4485f8829a4aSRandall Stewart 			 */
4486f8829a4aSRandall Stewart 			break;
4487f8829a4aSRandall Stewart 		}
4488f8829a4aSRandall Stewart 	}
4489f8829a4aSRandall Stewart 	/* Need to add a new block to chain */
4490f8829a4aSRandall Stewart 	if (!set) {
4491f8829a4aSRandall Stewart 		SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4492207304d4SRandall Stewart 		    sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4493f8829a4aSRandall Stewart 		if (twait_block == NULL) {
4494f8829a4aSRandall Stewart 			return;
4495f8829a4aSRandall Stewart 		}
449644b7479bSRandall Stewart 		memset(twait_block, 0, sizeof(struct sctp_tagblock));
4497f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
449858a7bf12SMichael Tuexen 		sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
4499f8829a4aSRandall Stewart 	}
4500f8829a4aSRandall Stewart }
4501f8829a4aSRandall Stewart 
450244249214SRandall Stewart void
sctp_clean_up_stream(struct sctp_tcb * stcb,struct sctp_readhead * rh)450344249214SRandall Stewart sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
450444249214SRandall Stewart {
450544249214SRandall Stewart 	struct sctp_tmit_chunk *chk, *nchk;
450628cd0699SMichael Tuexen 	struct sctp_queued_to_read *control, *ncontrol;
450744249214SRandall Stewart 
450828cd0699SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
450928cd0699SMichael Tuexen 		TAILQ_REMOVE(rh, control, next_instrm);
451028cd0699SMichael Tuexen 		control->on_strm_q = 0;
451128cd0699SMichael Tuexen 		if (control->on_read_q == 0) {
451228cd0699SMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
451328cd0699SMichael Tuexen 			if (control->data) {
451428cd0699SMichael Tuexen 				sctp_m_freem(control->data);
451528cd0699SMichael Tuexen 				control->data = NULL;
451644249214SRandall Stewart 			}
451744249214SRandall Stewart 		}
451844249214SRandall Stewart 		/* Reassembly free? */
451928cd0699SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
452028cd0699SMichael Tuexen 			TAILQ_REMOVE(&control->reasm, chk, sctp_next);
452144249214SRandall Stewart 			if (chk->data) {
452244249214SRandall Stewart 				sctp_m_freem(chk->data);
452344249214SRandall Stewart 				chk->data = NULL;
452444249214SRandall Stewart 			}
452544249214SRandall Stewart 			if (chk->holds_key_ref)
452644249214SRandall Stewart 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
452744249214SRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
452844249214SRandall Stewart 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
452944249214SRandall Stewart 			SCTP_DECR_CHK_COUNT();
453044249214SRandall Stewart 			/* sa_ignore FREED_MEMORY */
453144249214SRandall Stewart 		}
453244249214SRandall Stewart 		/*
453344249214SRandall Stewart 		 * We don't free the address here since all the net's were
453444249214SRandall Stewart 		 * freed above.
453544249214SRandall Stewart 		 */
453628cd0699SMichael Tuexen 		if (control->on_read_q == 0) {
453728cd0699SMichael Tuexen 			sctp_free_a_readq(stcb, control);
453844249214SRandall Stewart 		}
453944249214SRandall Stewart 	}
454044249214SRandall Stewart }
4541f8829a4aSRandall Stewart 
4542c4739e2fSRandall Stewart /*-
4543c4739e2fSRandall Stewart  * Free the association after un-hashing the remote port. This
4544c4739e2fSRandall Stewart  * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4545c4739e2fSRandall Stewart  * expect that the input to this function IS a locked TCB.
4546c4739e2fSRandall Stewart  * It will return 0, if it did NOT destroy the association (instead
4547c4739e2fSRandall Stewart  * it unlocks it. It will return NON-zero if it either destroyed the
4548c4739e2fSRandall Stewart  * association OR the association is already destroyed.
4549f8829a4aSRandall Stewart  */
4550f8829a4aSRandall Stewart int
sctp_free_assoc(struct sctp_inpcb * inp,struct sctp_tcb * stcb,int from_inpcbfree,int from_location)4551a5d547adSRandall Stewart sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4552f8829a4aSRandall Stewart {
4553f8829a4aSRandall Stewart 	int i;
4554f8829a4aSRandall Stewart 	struct sctp_association *asoc;
45554a9ef3f8SMichael Tuexen 	struct sctp_nets *net, *nnet;
45564a9ef3f8SMichael Tuexen 	struct sctp_laddr *laddr, *naddr;
45574a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
45584a9ef3f8SMichael Tuexen 	struct sctp_asconf_addr *aparam, *naparam;
45594a9ef3f8SMichael Tuexen 	struct sctp_asconf_ack *aack, *naack;
45604a9ef3f8SMichael Tuexen 	struct sctp_stream_reset_list *strrst, *nstrrst;
45614a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
45624a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
45634a9ef3f8SMichael Tuexen 	sctp_sharedkey_t *shared_key, *nshared_key;
4564f8829a4aSRandall Stewart 	struct socket *so;
4565f8829a4aSRandall Stewart 
4566f8829a4aSRandall Stewart 	/* first, lets purge the entry from the hash table. */
45675ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
4568f8829a4aSRandall Stewart 
4569f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
4570f8829a4aSRandall Stewart 	sctp_log_closing(inp, stcb, 6);
4571f8829a4aSRandall Stewart #endif
4572f8829a4aSRandall Stewart 	if (stcb->asoc.state == 0) {
4573f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
4574f8829a4aSRandall Stewart 		sctp_log_closing(inp, NULL, 7);
4575f8829a4aSRandall Stewart #endif
4576f8829a4aSRandall Stewart 		/* there is no asoc, really TSNH :-0 */
4577f8829a4aSRandall Stewart 		return (1);
4578f8829a4aSRandall Stewart 	}
4579ca85e948SMichael Tuexen 	if (stcb->asoc.alternate) {
4580ca85e948SMichael Tuexen 		sctp_free_remote_addr(stcb->asoc.alternate);
4581ca85e948SMichael Tuexen 		stcb->asoc.alternate = NULL;
4582ca85e948SMichael Tuexen 	}
4583a5d547adSRandall Stewart 	/* TEMP CODE */
4584a5d547adSRandall Stewart 	if (stcb->freed_from_where == 0) {
4585a5d547adSRandall Stewart 		/* Only record the first place free happened from */
4586a5d547adSRandall Stewart 		stcb->freed_from_where = from_location;
4587a5d547adSRandall Stewart 	}
4588a5d547adSRandall Stewart 	/* TEMP CODE */
4589a5d547adSRandall Stewart 
4590f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4591f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4592f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4593f8829a4aSRandall Stewart 		/* nothing around */
4594f8829a4aSRandall Stewart 		so = NULL;
4595f8829a4aSRandall Stewart 	else
4596f8829a4aSRandall Stewart 		so = inp->sctp_socket;
4597f8829a4aSRandall Stewart 
4598f8829a4aSRandall Stewart 	/*
4599f8829a4aSRandall Stewart 	 * We used timer based freeing if a reader or writer is in the way.
4600f8829a4aSRandall Stewart 	 * So we first check if we are actually being called from a timer,
4601f8829a4aSRandall Stewart 	 * if so we abort early if a reader or writer is still in the way.
4602f8829a4aSRandall Stewart 	 */
4603f8829a4aSRandall Stewart 	if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4604a5d547adSRandall Stewart 	    (from_inpcbfree == SCTP_NORMAL_PROC)) {
4605f8829a4aSRandall Stewart 		/*
4606f8829a4aSRandall Stewart 		 * is it the timer driving us? if so are the reader/writers
4607f8829a4aSRandall Stewart 		 * gone?
4608f8829a4aSRandall Stewart 		 */
4609f8829a4aSRandall Stewart 		if (stcb->asoc.refcnt) {
4610f8829a4aSRandall Stewart 			/* nope, reader or writer in the way */
4611f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4612f8829a4aSRandall Stewart 			/* no asoc destroyed */
4613f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
4614f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
4615f8829a4aSRandall Stewart 			sctp_log_closing(inp, stcb, 8);
4616f8829a4aSRandall Stewart #endif
4617f8829a4aSRandall Stewart 			return (0);
4618f8829a4aSRandall Stewart 		}
4619f8829a4aSRandall Stewart 	}
46205555400aSMichael Tuexen 	/* Now clean up any other timers */
46218803350dSMichael Tuexen 	sctp_stop_association_timers(stcb, false);
4622f4ad963cSRandall Stewart 	/* Now the read queue needs to be cleaned up (only once) */
4623f4ad963cSRandall Stewart 	if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4624839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
4625f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4626f8829a4aSRandall Stewart 		TAILQ_FOREACH(sq, &inp->read_queue, next) {
4627f8829a4aSRandall Stewart 			if (sq->stcb == stcb) {
4628f8829a4aSRandall Stewart 				sq->do_not_ref_stcb = 1;
4629f8829a4aSRandall Stewart 				sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4630f8829a4aSRandall Stewart 				/*
463103b0b021SRandall Stewart 				 * If there is no end, there never will be
463203b0b021SRandall Stewart 				 * now.
4633f8829a4aSRandall Stewart 				 */
463403b0b021SRandall Stewart 				if (sq->end_added == 0) {
46354f14d4b6SMichael Tuexen 					/* Held for PD-API, clear that. */
463603b0b021SRandall Stewart 					sq->pdapi_aborted = 1;
4637f8829a4aSRandall Stewart 					sq->held_length = 0;
4638e2e7c62eSMichael Tuexen 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4639810ec536SMichael Tuexen 						sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4640810ec536SMichael Tuexen 						    stcb,
4641810ec536SMichael Tuexen 						    SCTP_PARTIAL_DELIVERY_ABORTED,
4642749a7fb5SMichael Tuexen 						    (void *)sq,
4643810ec536SMichael Tuexen 						    SCTP_SO_LOCKED);
4644f8829a4aSRandall Stewart 					}
464503b0b021SRandall Stewart 					/* Add an end to wake them */
4646f8829a4aSRandall Stewart 					sq->end_added = 1;
4647f8829a4aSRandall Stewart 				}
4648f8829a4aSRandall Stewart 			}
4649749a7fb5SMichael Tuexen 		}
4650f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4651f8829a4aSRandall Stewart 		if (stcb->block_entry) {
4652c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4653f8829a4aSRandall Stewart 			stcb->block_entry->error = ECONNRESET;
4654f8829a4aSRandall Stewart 			stcb->block_entry = NULL;
4655f8829a4aSRandall Stewart 		}
4656f4ad963cSRandall Stewart 	}
465788a7eb29SRandall Stewart 	if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4658f4ad963cSRandall Stewart 		/*
465988a7eb29SRandall Stewart 		 * Someone holds a reference OR the socket is unaccepted
466088a7eb29SRandall Stewart 		 * yet.
4661f4ad963cSRandall Stewart 		 */
466262fb761fSRandall Stewart 		if ((stcb->asoc.refcnt) ||
466362fb761fSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
466462fb761fSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4665839d21d6SMichael Tuexen 			SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
4666a5d547adSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
466762fb761fSRandall Stewart 		}
4668a5d547adSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4669a5d547adSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4670a5d547adSRandall Stewart 			/* nothing around */
4671a5d547adSRandall Stewart 			so = NULL;
4672f4ad963cSRandall Stewart 		if (so) {
4673f4ad963cSRandall Stewart 			/* Wake any reader/writers */
4674f4ad963cSRandall Stewart 			sctp_sorwakeup(inp, so);
4675f4ad963cSRandall Stewart 			sctp_sowwakeup(inp, so);
4676f4ad963cSRandall Stewart 		}
4677d35be50fSMark Johnston 		SCTP_TCB_UNLOCK(stcb);
46780053ed28SMichael Tuexen 
4679f4ad963cSRandall Stewart #ifdef SCTP_LOG_CLOSING
4680f4ad963cSRandall Stewart 		sctp_log_closing(inp, stcb, 9);
4681f4ad963cSRandall Stewart #endif
4682f4ad963cSRandall Stewart 		/* no asoc destroyed */
4683f4ad963cSRandall Stewart 		return (0);
4684f4ad963cSRandall Stewart 	}
4685f4ad963cSRandall Stewart #ifdef SCTP_LOG_CLOSING
4686f4ad963cSRandall Stewart 	sctp_log_closing(inp, stcb, 10);
4687f4ad963cSRandall Stewart #endif
4688a5d547adSRandall Stewart 	/*
4689a5d547adSRandall Stewart 	 * When I reach here, no others want to kill the assoc yet.. and I
4690a5d547adSRandall Stewart 	 * own the lock. Now its possible an abort comes in when I do the
4691a5d547adSRandall Stewart 	 * lock exchange below to grab all the locks to do the final take
4692a5d547adSRandall Stewart 	 * out. to prevent this we increment the count, which will start a
4693a5d547adSRandall Stewart 	 * timer and blow out above thus assuring us that we hold exclusive
4694a5d547adSRandall Stewart 	 * killing of the asoc. Note that after getting back the TCB lock we
4695a5d547adSRandall Stewart 	 * will go ahead and increment the counter back up and stop any
4696a5d547adSRandall Stewart 	 * timer a passing stranger may have started :-S
4697a5d547adSRandall Stewart 	 */
4698a5d547adSRandall Stewart 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
4699a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4700a5d547adSRandall Stewart 
4701a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4702a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
4703a5d547adSRandall Stewart 		SCTP_INP_WLOCK(inp);
4704a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4705f8829a4aSRandall Stewart 	}
4706a5d547adSRandall Stewart 	/* Double check the GONE flag */
4707a5d547adSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4708a5d547adSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4709a5d547adSRandall Stewart 		/* nothing around */
4710a5d547adSRandall Stewart 		so = NULL;
4711a5d547adSRandall Stewart 
471224187cfeSMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
471324187cfeSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
471424187cfeSMichael Tuexen 		/*
471524187cfeSMichael Tuexen 		 * For TCP type we need special handling when we are
471624187cfeSMichael Tuexen 		 * connected. We also include the peel'ed off ones to.
471724187cfeSMichael Tuexen 		 */
471824187cfeSMichael Tuexen 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
471924187cfeSMichael Tuexen 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
472024187cfeSMichael Tuexen 			inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
472124187cfeSMichael Tuexen 			if (so) {
472224187cfeSMichael Tuexen 				SOCKBUF_LOCK(&so->so_rcv);
472324187cfeSMichael Tuexen 				so->so_state &= ~(SS_ISCONNECTING |
472424187cfeSMichael Tuexen 				    SS_ISDISCONNECTING |
472524187cfeSMichael Tuexen 				    SS_ISCONNECTED);
472624187cfeSMichael Tuexen 				so->so_state |= SS_ISDISCONNECTED;
472724187cfeSMichael Tuexen 				socantrcvmore_locked(so);
472824187cfeSMichael Tuexen 				socantsendmore(so);
472924187cfeSMichael Tuexen 				sctp_sowwakeup(inp, so);
473024187cfeSMichael Tuexen 				sctp_sorwakeup(inp, so);
473124187cfeSMichael Tuexen 				SCTP_SOWAKEUP(so);
473224187cfeSMichael Tuexen 			}
473324187cfeSMichael Tuexen 		}
473424187cfeSMichael Tuexen 	}
473524187cfeSMichael Tuexen 
4736f8829a4aSRandall Stewart 	/*
4737f8829a4aSRandall Stewart 	 * Make it invalid too, that way if its about to run it will abort
4738f8829a4aSRandall Stewart 	 * and return.
4739f8829a4aSRandall Stewart 	 */
4740f8829a4aSRandall Stewart 	/* re-increment the lock */
4741a5d547adSRandall Stewart 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
47423c1ba6f3SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4743f8829a4aSRandall Stewart 	}
4744fc066a61SMichael Tuexen 	if (stcb->asoc.refcnt) {
4745839d21d6SMichael Tuexen 		SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
4746fc066a61SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4747fc066a61SMichael Tuexen 		if (from_inpcbfree == SCTP_NORMAL_PROC) {
4748fc066a61SMichael Tuexen 			SCTP_INP_INFO_WUNLOCK();
4749fc066a61SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
4750fc066a61SMichael Tuexen 		}
4751fc066a61SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
4752fc066a61SMichael Tuexen 		return (0);
4753fc066a61SMichael Tuexen 	}
4754f8829a4aSRandall Stewart 	asoc->state = 0;
4755f8829a4aSRandall Stewart 	if (inp->sctp_tcbhash) {
4756f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_tcbhash);
4757f8829a4aSRandall Stewart 	}
4758830d754dSRandall Stewart 	if (stcb->asoc.in_asocid_hash) {
4759830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_tcbasocidhash);
4760f8829a4aSRandall Stewart 	}
476165f30a39SMark Johnston 	if (inp->sctp_socket == NULL) {
476265f30a39SMark Johnston 		stcb->sctp_socket = NULL;
476365f30a39SMark Johnston 	}
4764f8829a4aSRandall Stewart 	/* Now lets remove it from the list of ALL associations in the EP */
4765f8829a4aSRandall Stewart 	LIST_REMOVE(stcb, sctp_tcblist);
4766a5d547adSRandall Stewart 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
4767f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
4768f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
4769f8829a4aSRandall Stewart 	}
4770f8829a4aSRandall Stewart 	/* pull from vtag hash */
4771f8829a4aSRandall Stewart 	LIST_REMOVE(stcb, sctp_asocs);
477258a7bf12SMichael Tuexen 	sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport);
4773f8829a4aSRandall Stewart 
477444b7479bSRandall Stewart 	/*
4775b5c16493SMichael Tuexen 	 * Now restop the timers to be sure this is paranoia at is finest!
477644b7479bSRandall Stewart 	 */
47778803350dSMichael Tuexen 	sctp_stop_association_timers(stcb, true);
477844b7479bSRandall Stewart 
4779f8829a4aSRandall Stewart 	/*
4780f8829a4aSRandall Stewart 	 * The chunk lists and such SHOULD be empty but we check them just
4781f8829a4aSRandall Stewart 	 * in case.
4782f8829a4aSRandall Stewart 	 */
4783f8829a4aSRandall Stewart 	/* anything on the wheel needs to be removed */
4784f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
4785f8829a4aSRandall Stewart 		struct sctp_stream_out *outs;
4786f8829a4aSRandall Stewart 
4787f8829a4aSRandall Stewart 		outs = &asoc->strmout[i];
4788f8829a4aSRandall Stewart 		/* now clean up any chunks here */
47894a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
47904d58b0c3SMichael Tuexen 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4791f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
4792762ae0ecSMichael Tuexen 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
4793d07b2ac6SMichael Tuexen 			sctp_free_spbufspace(stcb, asoc, sp);
4794f8829a4aSRandall Stewart 			if (sp->data) {
47958db924deSRandall Stewart 				if (so) {
47968db924deSRandall Stewart 					/* Still an open socket - report */
47978db924deSRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
47981edc9dbaSMichael Tuexen 					    0, (void *)sp, SCTP_SO_LOCKED);
47998db924deSRandall Stewart 				}
48008db924deSRandall Stewart 				if (sp->data) {
4801f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
4802f8829a4aSRandall Stewart 					sp->data = NULL;
4803f8829a4aSRandall Stewart 					sp->tail_mbuf = NULL;
4804d07b2ac6SMichael Tuexen 					sp->length = 0;
4805f8829a4aSRandall Stewart 				}
48068db924deSRandall Stewart 			}
48079eea4a2dSMichael Tuexen 			if (sp->net) {
4808f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
48099eea4a2dSMichael Tuexen 				sp->net = NULL;
48109eea4a2dSMichael Tuexen 			}
4811d07b2ac6SMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
4812f8829a4aSRandall Stewart 		}
4813f8829a4aSRandall Stewart 	}
48143c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
48154a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
48164a9ef3f8SMichael Tuexen 		TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
48174a9ef3f8SMichael Tuexen 		SCTP_FREE(strrst, SCTP_M_STRESET);
4818f8829a4aSRandall Stewart 	}
48194a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
4820f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4821f8829a4aSRandall Stewart 		if (sq->data) {
4822f8829a4aSRandall Stewart 			sctp_m_freem(sq->data);
4823f8829a4aSRandall Stewart 			sq->data = NULL;
4824f8829a4aSRandall Stewart 		}
4825f8829a4aSRandall Stewart 		sctp_free_remote_addr(sq->whoFrom);
4826f8829a4aSRandall Stewart 		sq->whoFrom = NULL;
4827f8829a4aSRandall Stewart 		sq->stcb = NULL;
4828f8829a4aSRandall Stewart 		/* Free the ctl entry */
482944249214SRandall Stewart 		sctp_free_a_readq(stcb, sq);
48303c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4831f8829a4aSRandall Stewart 	}
48324a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
4833f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4834f8829a4aSRandall Stewart 		if (chk->data) {
4835f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4836f8829a4aSRandall Stewart 			chk->data = NULL;
4837f8829a4aSRandall Stewart 		}
4838830d754dSRandall Stewart 		if (chk->holds_key_ref)
4839689e6a5fSMichael Tuexen 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4840b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4841f8829a4aSRandall Stewart 		SCTP_DECR_CHK_COUNT();
4842b3f1ea41SRandall Stewart 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
4843f8829a4aSRandall Stewart 		asoc->free_chunk_cnt--;
48443c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4845f8829a4aSRandall Stewart 	}
4846f8829a4aSRandall Stewart 	/* pending send queue SHOULD be empty */
48474a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
484849656eefSMichael Tuexen 		if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
484949656eefSMichael Tuexen 			asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4850325c8c46SMichael Tuexen #ifdef INVARIANTS
4851325c8c46SMichael Tuexen 		} else {
485249656eefSMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4853325c8c46SMichael Tuexen #endif
4854325c8c46SMichael Tuexen 		}
4855f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4856f8829a4aSRandall Stewart 		if (chk->data) {
48578db924deSRandall Stewart 			if (so) {
48588db924deSRandall Stewart 				/* Still a socket? */
48591edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
48601edc9dbaSMichael Tuexen 				    0, chk, SCTP_SO_LOCKED);
48618db924deSRandall Stewart 			}
48628db924deSRandall Stewart 			if (chk->data) {
4863f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
4864f8829a4aSRandall Stewart 				chk->data = NULL;
4865f8829a4aSRandall Stewart 			}
48668db924deSRandall Stewart 		}
4867830d754dSRandall Stewart 		if (chk->holds_key_ref)
4868689e6a5fSMichael Tuexen 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
48699eea4a2dSMichael Tuexen 		if (chk->whoTo) {
4870f8829a4aSRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
48719eea4a2dSMichael Tuexen 			chk->whoTo = NULL;
48729eea4a2dSMichael Tuexen 		}
4873b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4874f8829a4aSRandall Stewart 		SCTP_DECR_CHK_COUNT();
48753c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4876f8829a4aSRandall Stewart 	}
4877f8829a4aSRandall Stewart 	/* sent queue SHOULD be empty */
48784a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4879325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
488049656eefSMichael Tuexen 			if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
488149656eefSMichael Tuexen 				asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4882a7ad6026SMichael Tuexen #ifdef INVARIANTS
4883a7ad6026SMichael Tuexen 			} else {
488449656eefSMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4885a7ad6026SMichael Tuexen #endif
4886a7ad6026SMichael Tuexen 			}
4887a7ad6026SMichael Tuexen 		}
4888f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4889f8829a4aSRandall Stewart 		if (chk->data) {
48908db924deSRandall Stewart 			if (so) {
48918db924deSRandall Stewart 				/* Still a socket? */
48921edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
48931edc9dbaSMichael Tuexen 				    0, chk, SCTP_SO_LOCKED);
48948db924deSRandall Stewart 			}
48958db924deSRandall Stewart 			if (chk->data) {
4896f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
4897f8829a4aSRandall Stewart 				chk->data = NULL;
4898f8829a4aSRandall Stewart 			}
48998db924deSRandall Stewart 		}
4900830d754dSRandall Stewart 		if (chk->holds_key_ref)
4901689e6a5fSMichael Tuexen 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4902f8829a4aSRandall Stewart 		sctp_free_remote_addr(chk->whoTo);
4903b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4904f8829a4aSRandall Stewart 		SCTP_DECR_CHK_COUNT();
49053c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4906f8829a4aSRandall Stewart 	}
4907325c8c46SMichael Tuexen #ifdef INVARIANTS
4908325c8c46SMichael Tuexen 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4909325c8c46SMichael Tuexen 		if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
4910325c8c46SMichael Tuexen 			panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
4911325c8c46SMichael Tuexen 		}
4912325c8c46SMichael Tuexen 	}
4913325c8c46SMichael Tuexen #endif
4914f8829a4aSRandall Stewart 	/* control queue MAY not be empty */
49154a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
4916f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4917f8829a4aSRandall Stewart 		if (chk->data) {
4918f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4919f8829a4aSRandall Stewart 			chk->data = NULL;
4920f8829a4aSRandall Stewart 		}
4921830d754dSRandall Stewart 		if (chk->holds_key_ref)
4922689e6a5fSMichael Tuexen 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4923f8829a4aSRandall Stewart 		sctp_free_remote_addr(chk->whoTo);
4924b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4925f8829a4aSRandall Stewart 		SCTP_DECR_CHK_COUNT();
49263c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4927f8829a4aSRandall Stewart 	}
4928c54a18d2SRandall Stewart 	/* ASCONF queue MAY not be empty */
49294a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
4930c54a18d2SRandall Stewart 		TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
4931c54a18d2SRandall Stewart 		if (chk->data) {
4932c54a18d2SRandall Stewart 			sctp_m_freem(chk->data);
4933c54a18d2SRandall Stewart 			chk->data = NULL;
4934c54a18d2SRandall Stewart 		}
4935830d754dSRandall Stewart 		if (chk->holds_key_ref)
4936689e6a5fSMichael Tuexen 			sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4937c54a18d2SRandall Stewart 		sctp_free_remote_addr(chk->whoTo);
4938b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4939c54a18d2SRandall Stewart 		SCTP_DECR_CHK_COUNT();
4940c54a18d2SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4941c54a18d2SRandall Stewart 	}
4942f8829a4aSRandall Stewart 	if (asoc->mapping_array) {
4943207304d4SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4944f8829a4aSRandall Stewart 		asoc->mapping_array = NULL;
4945f8829a4aSRandall Stewart 	}
4946bf1be571SRandall Stewart 	if (asoc->nr_mapping_array) {
4947bf1be571SRandall Stewart 		SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4948bf1be571SRandall Stewart 		asoc->nr_mapping_array = NULL;
4949bf1be571SRandall Stewart 	}
4950f8829a4aSRandall Stewart 	/* the stream outs */
4951f8829a4aSRandall Stewart 	if (asoc->strmout) {
4952207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4953f8829a4aSRandall Stewart 		asoc->strmout = NULL;
4954f8829a4aSRandall Stewart 	}
4955ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = 0;
4956f8829a4aSRandall Stewart 	if (asoc->strmin) {
4957f8829a4aSRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
495844249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
495944249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
4960f8829a4aSRandall Stewart 		}
4961207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4962f8829a4aSRandall Stewart 		asoc->strmin = NULL;
4963f8829a4aSRandall Stewart 	}
4964f8829a4aSRandall Stewart 	asoc->streamincnt = 0;
49654a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4966a5d547adSRandall Stewart #ifdef INVARIANTS
49674a9ef3f8SMichael Tuexen 		if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
4968f8829a4aSRandall Stewart 			panic("no net's left alloc'ed, or list points to itself");
4969f8829a4aSRandall Stewart 		}
49704a9ef3f8SMichael Tuexen #endif
4971f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4972f8829a4aSRandall Stewart 		sctp_free_remote_addr(net);
4973f8829a4aSRandall Stewart 	}
49744a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
49753c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
497642551e99SRandall Stewart 		sctp_remove_laddr(laddr);
4977f8829a4aSRandall Stewart 	}
497842551e99SRandall Stewart 
4979f8829a4aSRandall Stewart 	/* pending asconf (address) parameters */
49804a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
49813c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
4982f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4983207304d4SRandall Stewart 		SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4984f8829a4aSRandall Stewart 	}
49854a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
4986851b7298SRandall Stewart 		/* sa_ignore FREED_MEMORY */
49872afb3e84SRandall Stewart 		TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
49882afb3e84SRandall Stewart 		if (aack->data != NULL) {
49892afb3e84SRandall Stewart 			sctp_m_freem(aack->data);
49902afb3e84SRandall Stewart 		}
4991b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
4992f8829a4aSRandall Stewart 	}
4993f8829a4aSRandall Stewart 	/* clean up auth stuff */
4994f8829a4aSRandall Stewart 	if (asoc->local_hmacs)
4995f8829a4aSRandall Stewart 		sctp_free_hmaclist(asoc->local_hmacs);
4996f8829a4aSRandall Stewart 	if (asoc->peer_hmacs)
4997f8829a4aSRandall Stewart 		sctp_free_hmaclist(asoc->peer_hmacs);
4998f8829a4aSRandall Stewart 
4999f8829a4aSRandall Stewart 	if (asoc->local_auth_chunks)
5000f8829a4aSRandall Stewart 		sctp_free_chunklist(asoc->local_auth_chunks);
5001f8829a4aSRandall Stewart 	if (asoc->peer_auth_chunks)
5002f8829a4aSRandall Stewart 		sctp_free_chunklist(asoc->peer_auth_chunks);
5003f8829a4aSRandall Stewart 
5004f8829a4aSRandall Stewart 	sctp_free_authinfo(&asoc->authinfo);
5005f8829a4aSRandall Stewart 
50064a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5007f8829a4aSRandall Stewart 		LIST_REMOVE(shared_key, next);
5008f8829a4aSRandall Stewart 		sctp_free_sharedkey(shared_key);
50093c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
5010f8829a4aSRandall Stewart 	}
5011f8829a4aSRandall Stewart 
5012f8829a4aSRandall Stewart 	/* Insert new items here :> */
5013f8829a4aSRandall Stewart 
5014f8829a4aSRandall Stewart 	/* Get rid of LOCK */
50153b3d05d7SMichael Tuexen 	SCTP_TCB_UNLOCK(stcb);
5016f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_DESTROY(stcb);
5017a5d547adSRandall Stewart 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
5018a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
5019a5d547adSRandall Stewart 		SCTP_INP_RLOCK(inp);
5020a5d547adSRandall Stewart 	}
5021a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS
5022a5d547adSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5023f8829a4aSRandall Stewart 		/* now clean up the tasoc itself */
5024b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5025f8829a4aSRandall Stewart 		SCTP_DECR_ASOC_COUNT();
5026a5d547adSRandall Stewart 	} else {
5027a5d547adSRandall Stewart 		LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5028a5d547adSRandall Stewart 	}
5029a5d547adSRandall Stewart #else
5030b3f1ea41SRandall Stewart 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5031a5d547adSRandall Stewart 	SCTP_DECR_ASOC_COUNT();
5032a5d547adSRandall Stewart #endif
5033a5d547adSRandall Stewart 	if (from_inpcbfree == SCTP_NORMAL_PROC) {
5034f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5035f8829a4aSRandall Stewart 			/*
5036f8829a4aSRandall Stewart 			 * If its NOT the inp_free calling us AND sctp_close
5037f8829a4aSRandall Stewart 			 * as been called, we call back...
5038f8829a4aSRandall Stewart 			 */
5039f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
5040f8829a4aSRandall Stewart 			/*
5041f8829a4aSRandall Stewart 			 * This will start the kill timer (if we are the
5042034b88b0SMichael Tuexen 			 * last one) since we hold an increment yet. But
5043034b88b0SMichael Tuexen 			 * this is the only safe way to do this since
5044034b88b0SMichael Tuexen 			 * otherwise if the socket closes at the same time
5045034b88b0SMichael Tuexen 			 * we are here we might collide in the cleanup.
5046f8829a4aSRandall Stewart 			 */
5047b0552ae2SRandall Stewart 			sctp_inpcb_free(inp,
5048b0552ae2SRandall Stewart 			    SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5049b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
5050f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
5051f8829a4aSRandall Stewart 		} else {
5052f8829a4aSRandall Stewart 			/* The socket is still open. */
5053f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
5054f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
5055f8829a4aSRandall Stewart 		}
5056bca18028SMichael Tuexen 	}
5057f8829a4aSRandall Stewart 	/* destroyed the asoc */
5058f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
5059f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 11);
5060f8829a4aSRandall Stewart #endif
5061f8829a4aSRandall Stewart 	return (1);
5062f8829a4aSRandall Stewart }
5063f8829a4aSRandall Stewart 
5064f8829a4aSRandall Stewart /*
5065f8829a4aSRandall Stewart  * determine if a destination is "reachable" based upon the addresses bound
5066f8829a4aSRandall Stewart  * to the current endpoint (e.g. only v4 or v6 currently bound)
5067f8829a4aSRandall Stewart  */
5068f8829a4aSRandall Stewart /*
5069f8829a4aSRandall Stewart  * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5070f8829a4aSRandall Stewart  * assoc level v4/v6 flags, as the assoc *may* not have the same address
5071f8829a4aSRandall Stewart  * types bound as its endpoint
5072f8829a4aSRandall Stewart  */
5073f8829a4aSRandall Stewart int
sctp_destination_is_reachable(struct sctp_tcb * stcb,struct sockaddr * destaddr)5074f8829a4aSRandall Stewart sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5075f8829a4aSRandall Stewart {
5076f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
5077f8829a4aSRandall Stewart 	int answer;
5078f8829a4aSRandall Stewart 
5079f8829a4aSRandall Stewart 	/*
5080f8829a4aSRandall Stewart 	 * No locks here, the TCB, in all cases is already locked and an
5081f8829a4aSRandall Stewart 	 * assoc is up. There is either a INP lock by the caller applied (in
5082f8829a4aSRandall Stewart 	 * asconf case when deleting an address) or NOT in the HB case,
5083f8829a4aSRandall Stewart 	 * however if HB then the INP increment is up and the INP will not
5084f8829a4aSRandall Stewart 	 * be removed (on top of the fact that we have a TCB lock). So we
5085f8829a4aSRandall Stewart 	 * only want to read the sctp_flags, which is either bound-all or
5086f8829a4aSRandall Stewart 	 * not.. no protection needed since once an assoc is up you can't be
5087f8829a4aSRandall Stewart 	 * changing your binding.
5088f8829a4aSRandall Stewart 	 */
5089f8829a4aSRandall Stewart 	inp = stcb->sctp_ep;
5090f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5091f8829a4aSRandall Stewart 		/* if bound all, destination is not restricted */
5092f8829a4aSRandall Stewart 		/*
5093f8829a4aSRandall Stewart 		 * RRS: Question during lock work: Is this correct? If you
5094f8829a4aSRandall Stewart 		 * are bound-all you still might need to obey the V4--V6
5095f8829a4aSRandall Stewart 		 * flags??? IMO this bound-all stuff needs to be removed!
5096f8829a4aSRandall Stewart 		 */
5097f8829a4aSRandall Stewart 		return (1);
5098f8829a4aSRandall Stewart 	}
5099f8829a4aSRandall Stewart 	/* NOTE: all "scope" checks are done when local addresses are added */
5100e6194c2eSMichael Tuexen 	switch (destaddr->sa_family) {
5101e0e00a4dSMichael Tuexen #ifdef INET6
5102e6194c2eSMichael Tuexen 	case AF_INET6:
5103f8829a4aSRandall Stewart 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5104e6194c2eSMichael Tuexen 		break;
5105e0e00a4dSMichael Tuexen #endif
5106e0e00a4dSMichael Tuexen #ifdef INET
5107e6194c2eSMichael Tuexen 	case AF_INET:
5108f8829a4aSRandall Stewart 		answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5109e6194c2eSMichael Tuexen 		break;
5110e0e00a4dSMichael Tuexen #endif
5111e6194c2eSMichael Tuexen 	default:
5112f8829a4aSRandall Stewart 		/* invalid family, so it's unreachable */
5113f8829a4aSRandall Stewart 		answer = 0;
5114e6194c2eSMichael Tuexen 		break;
5115f8829a4aSRandall Stewart 	}
5116f8829a4aSRandall Stewart 	return (answer);
5117f8829a4aSRandall Stewart }
5118f8829a4aSRandall Stewart 
5119f8829a4aSRandall Stewart /*
5120f8829a4aSRandall Stewart  * update the inp_vflags on an endpoint
5121f8829a4aSRandall Stewart  */
5122f8829a4aSRandall Stewart static void
sctp_update_ep_vflag(struct sctp_inpcb * inp)5123f8829a4aSRandall Stewart sctp_update_ep_vflag(struct sctp_inpcb *inp)
5124f8829a4aSRandall Stewart {
5125f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
5126f8829a4aSRandall Stewart 
5127f8829a4aSRandall Stewart 	/* first clear the flag */
5128f8829a4aSRandall Stewart 	inp->ip_inp.inp.inp_vflag = 0;
5129f8829a4aSRandall Stewart 	/* set the flag based on addresses on the ep list */
5130f8829a4aSRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5131f8829a4aSRandall Stewart 		if (laddr->ifa == NULL) {
5132ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
51336e9c45e0SMichael Tuexen 			    __func__);
5134f8829a4aSRandall Stewart 			continue;
5135f8829a4aSRandall Stewart 		}
51360053ed28SMichael Tuexen 
513742551e99SRandall Stewart 		if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5138f8829a4aSRandall Stewart 			continue;
5139f8829a4aSRandall Stewart 		}
5140e6194c2eSMichael Tuexen 		switch (laddr->ifa->address.sa.sa_family) {
5141e6194c2eSMichael Tuexen #ifdef INET6
5142e6194c2eSMichael Tuexen 		case AF_INET6:
5143f8829a4aSRandall Stewart 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5144e6194c2eSMichael Tuexen 			break;
5145e6194c2eSMichael Tuexen #endif
5146e6194c2eSMichael Tuexen #ifdef INET
5147e6194c2eSMichael Tuexen 		case AF_INET:
5148f8829a4aSRandall Stewart 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5149e6194c2eSMichael Tuexen 			break;
5150e6194c2eSMichael Tuexen #endif
5151e6194c2eSMichael Tuexen 		default:
5152e6194c2eSMichael Tuexen 			break;
5153f8829a4aSRandall Stewart 		}
5154f8829a4aSRandall Stewart 	}
5155f8829a4aSRandall Stewart }
5156f8829a4aSRandall Stewart 
5157f8829a4aSRandall Stewart /*
5158f8829a4aSRandall Stewart  * Add the address to the endpoint local address list There is nothing to be
5159f8829a4aSRandall Stewart  * done if we are bound to all addresses
5160f8829a4aSRandall Stewart  */
5161ad81507eSRandall Stewart void
sctp_add_local_addr_ep(struct sctp_inpcb * inp,struct sctp_ifa * ifa,uint32_t action)516242551e99SRandall Stewart sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5163f8829a4aSRandall Stewart {
5164f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
516576f8482aSMichael Tuexen 	struct sctp_tcb *stcb;
5166ad81507eSRandall Stewart 	int fnd, error = 0;
5167f8829a4aSRandall Stewart 
5168f8829a4aSRandall Stewart 	fnd = 0;
5169f8829a4aSRandall Stewart 
5170f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5171f8829a4aSRandall Stewart 		/* You are already bound to all. You have it already */
5172ad81507eSRandall Stewart 		return;
5173f8829a4aSRandall Stewart 	}
5174e6194c2eSMichael Tuexen #ifdef INET6
517542551e99SRandall Stewart 	if (ifa->address.sa.sa_family == AF_INET6) {
517642551e99SRandall Stewart 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
517742551e99SRandall Stewart 			/* Can't bind a non-useable addr. */
5178ad81507eSRandall Stewart 			return;
5179f8829a4aSRandall Stewart 		}
518042551e99SRandall Stewart 	}
5181e6194c2eSMichael Tuexen #endif
5182f8829a4aSRandall Stewart 	/* first, is it already present? */
5183f8829a4aSRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5184f8829a4aSRandall Stewart 		if (laddr->ifa == ifa) {
5185f8829a4aSRandall Stewart 			fnd = 1;
5186f8829a4aSRandall Stewart 			break;
5187f8829a4aSRandall Stewart 		}
5188f8829a4aSRandall Stewart 	}
5189f8829a4aSRandall Stewart 
519042551e99SRandall Stewart 	if (fnd == 0) {
519142551e99SRandall Stewart 		/* Not in the ep list */
519242551e99SRandall Stewart 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5193f8829a4aSRandall Stewart 		if (error != 0)
5194ad81507eSRandall Stewart 			return;
5195f8829a4aSRandall Stewart 		inp->laddr_count++;
5196f8829a4aSRandall Stewart 		/* update inp_vflag flags */
5197e6194c2eSMichael Tuexen 		switch (ifa->address.sa.sa_family) {
5198e6194c2eSMichael Tuexen #ifdef INET6
5199e6194c2eSMichael Tuexen 		case AF_INET6:
5200f8829a4aSRandall Stewart 			inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5201e6194c2eSMichael Tuexen 			break;
5202e6194c2eSMichael Tuexen #endif
5203e0e00a4dSMichael Tuexen #ifdef INET
5204e6194c2eSMichael Tuexen 		case AF_INET:
5205f8829a4aSRandall Stewart 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5206e6194c2eSMichael Tuexen 			break;
5207e6194c2eSMichael Tuexen #endif
5208e6194c2eSMichael Tuexen 		default:
5209e6194c2eSMichael Tuexen 			break;
5210f8829a4aSRandall Stewart 		}
521176f8482aSMichael Tuexen 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
521276f8482aSMichael Tuexen 			sctp_add_local_addr_restricted(stcb, ifa);
521376f8482aSMichael Tuexen 		}
5214f8829a4aSRandall Stewart 	}
5215ad81507eSRandall Stewart 	return;
5216f8829a4aSRandall Stewart }
5217f8829a4aSRandall Stewart 
5218f8829a4aSRandall Stewart /*
5219f8829a4aSRandall Stewart  * select a new (hopefully reachable) destination net (should only be used
5220f8829a4aSRandall Stewart  * when we deleted an ep addr that is the only usable source address to reach
5221f8829a4aSRandall Stewart  * the destination net)
5222f8829a4aSRandall Stewart  */
5223f8829a4aSRandall Stewart static void
sctp_select_primary_destination(struct sctp_tcb * stcb)5224f8829a4aSRandall Stewart sctp_select_primary_destination(struct sctp_tcb *stcb)
5225f8829a4aSRandall Stewart {
5226f8829a4aSRandall Stewart 	struct sctp_nets *net;
5227f8829a4aSRandall Stewart 
5228f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5229f8829a4aSRandall Stewart 		/* for now, we'll just pick the first reachable one we find */
5230f8829a4aSRandall Stewart 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5231f8829a4aSRandall Stewart 			continue;
5232f8829a4aSRandall Stewart 		if (sctp_destination_is_reachable(stcb,
5233f8829a4aSRandall Stewart 		    (struct sockaddr *)&net->ro._l_addr)) {
5234f8829a4aSRandall Stewart 			/* found a reachable destination */
5235f8829a4aSRandall Stewart 			stcb->asoc.primary_destination = net;
5236f8829a4aSRandall Stewart 		}
5237f8829a4aSRandall Stewart 	}
5238f8829a4aSRandall Stewart 	/* I can't there from here! ...we're gonna die shortly... */
5239f8829a4aSRandall Stewart }
5240f8829a4aSRandall Stewart 
5241f8829a4aSRandall Stewart /*
524264a3a630SMichael Tuexen  * Delete the address from the endpoint local address list. There is nothing
5243f8829a4aSRandall Stewart  * to be done if we are bound to all addresses
5244f8829a4aSRandall Stewart  */
5245ad81507eSRandall Stewart void
sctp_del_local_addr_ep(struct sctp_inpcb * inp,struct sctp_ifa * ifa)524642551e99SRandall Stewart sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5247f8829a4aSRandall Stewart {
5248f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
5249f8829a4aSRandall Stewart 	int fnd;
5250f8829a4aSRandall Stewart 
5251f8829a4aSRandall Stewart 	fnd = 0;
5252f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5253f8829a4aSRandall Stewart 		/* You are already bound to all. You have it already */
5254ad81507eSRandall Stewart 		return;
5255f8829a4aSRandall Stewart 	}
5256f8829a4aSRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5257f8829a4aSRandall Stewart 		if (laddr->ifa == ifa) {
5258f8829a4aSRandall Stewart 			fnd = 1;
5259f8829a4aSRandall Stewart 			break;
5260f8829a4aSRandall Stewart 		}
5261f8829a4aSRandall Stewart 	}
5262f8829a4aSRandall Stewart 	if (fnd && (inp->laddr_count < 2)) {
5263f8829a4aSRandall Stewart 		/* can't delete unless there are at LEAST 2 addresses */
5264ad81507eSRandall Stewart 		return;
5265f8829a4aSRandall Stewart 	}
526642551e99SRandall Stewart 	if (fnd) {
5267f8829a4aSRandall Stewart 		/*
5268f8829a4aSRandall Stewart 		 * clean up any use of this address go through our
5269f8829a4aSRandall Stewart 		 * associations and clear any last_used_address that match
5270f8829a4aSRandall Stewart 		 * this one for each assoc, see if a new primary_destination
5271f8829a4aSRandall Stewart 		 * is needed
5272f8829a4aSRandall Stewart 		 */
5273f8829a4aSRandall Stewart 		struct sctp_tcb *stcb;
5274f8829a4aSRandall Stewart 
5275f8829a4aSRandall Stewart 		/* clean up "next_addr_touse" */
5276f8829a4aSRandall Stewart 		if (inp->next_addr_touse == laddr)
5277f8829a4aSRandall Stewart 			/* delete this address */
5278f8829a4aSRandall Stewart 			inp->next_addr_touse = NULL;
5279f8829a4aSRandall Stewart 
5280f8829a4aSRandall Stewart 		/* clean up "last_used_address" */
5281f8829a4aSRandall Stewart 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
528242551e99SRandall Stewart 			struct sctp_nets *net;
528342551e99SRandall Stewart 
528442551e99SRandall Stewart 			SCTP_TCB_LOCK(stcb);
5285f8829a4aSRandall Stewart 			if (stcb->asoc.last_used_address == laddr)
5286f8829a4aSRandall Stewart 				/* delete this address */
5287f8829a4aSRandall Stewart 				stcb->asoc.last_used_address = NULL;
5288b7b84c0eSMichael Tuexen 			/*
5289b7b84c0eSMichael Tuexen 			 * Now spin through all the nets and purge any ref
5290b7b84c0eSMichael Tuexen 			 * to laddr
5291b7b84c0eSMichael Tuexen 			 */
529242551e99SRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
529364a3a630SMichael Tuexen 				if (net->ro._s_addr == laddr->ifa) {
529442551e99SRandall Stewart 					/* Yep, purge src address selected */
5295983066f0SAlexander V. Chernikov 					RO_NHFREE(&net->ro);
529642551e99SRandall Stewart 					sctp_free_ifa(net->ro._s_addr);
529742551e99SRandall Stewart 					net->ro._s_addr = NULL;
529842551e99SRandall Stewart 					net->src_addr_selected = 0;
529942551e99SRandall Stewart 				}
530042551e99SRandall Stewart 			}
530142551e99SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
530242551e99SRandall Stewart 		}		/* for each tcb */
5303f8829a4aSRandall Stewart 		/* remove it from the ep list */
5304f8829a4aSRandall Stewart 		sctp_remove_laddr(laddr);
5305f8829a4aSRandall Stewart 		inp->laddr_count--;
5306f8829a4aSRandall Stewart 		/* update inp_vflag flags */
5307f8829a4aSRandall Stewart 		sctp_update_ep_vflag(inp);
5308f8829a4aSRandall Stewart 	}
5309ad81507eSRandall Stewart 	return;
5310f8829a4aSRandall Stewart }
5311f8829a4aSRandall Stewart 
5312f8829a4aSRandall Stewart /*
53131b649582SRandall Stewart  * Add the address to the TCB local address restricted list.
53141b649582SRandall Stewart  * This is a "pending" address list (eg. addresses waiting for an
53151b649582SRandall Stewart  * ASCONF-ACK response) and cannot be used as a valid source address.
5316f8829a4aSRandall Stewart  */
5317ad81507eSRandall Stewart void
sctp_add_local_addr_restricted(struct sctp_tcb * stcb,struct sctp_ifa * ifa)53181b649582SRandall Stewart sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5319f8829a4aSRandall Stewart {
5320f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
532142551e99SRandall Stewart 	struct sctpladdr *list;
5322f8829a4aSRandall Stewart 
5323f8829a4aSRandall Stewart 	/*
532442551e99SRandall Stewart 	 * Assumes TCB is locked.. and possibly the INP. May need to
5325f8829a4aSRandall Stewart 	 * confirm/fix that if we need it and is not the case.
5326f8829a4aSRandall Stewart 	 */
532742551e99SRandall Stewart 	list = &stcb->asoc.sctp_restricted_addrs;
5328f8829a4aSRandall Stewart 
5329e6194c2eSMichael Tuexen #ifdef INET6
533042551e99SRandall Stewart 	if (ifa->address.sa.sa_family == AF_INET6) {
533142551e99SRandall Stewart 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5332f8829a4aSRandall Stewart 			/* Can't bind a non-existent addr. */
5333ad81507eSRandall Stewart 			return;
5334f8829a4aSRandall Stewart 		}
533542551e99SRandall Stewart 	}
5336e6194c2eSMichael Tuexen #endif
5337f8829a4aSRandall Stewart 	/* does the address already exist? */
533842551e99SRandall Stewart 	LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5339f8829a4aSRandall Stewart 		if (laddr->ifa == ifa) {
5340ad81507eSRandall Stewart 			return;
5341f8829a4aSRandall Stewart 		}
5342f8829a4aSRandall Stewart 	}
5343f8829a4aSRandall Stewart 
5344f8829a4aSRandall Stewart 	/* add to the list */
5345ad81507eSRandall Stewart 	(void)sctp_insert_laddr(list, ifa, 0);
5346ad81507eSRandall Stewart 	return;
5347f8829a4aSRandall Stewart }
5348f8829a4aSRandall Stewart 
5349f8829a4aSRandall Stewart /*
53501b649582SRandall Stewart  * Remove a local address from the TCB local address restricted list
5351f8829a4aSRandall Stewart  */
5352ad81507eSRandall Stewart void
sctp_del_local_addr_restricted(struct sctp_tcb * stcb,struct sctp_ifa * ifa)53531b649582SRandall Stewart sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5354f8829a4aSRandall Stewart {
5355f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
5356f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
5357f8829a4aSRandall Stewart 
5358f8829a4aSRandall Stewart 	/*
5359f8829a4aSRandall Stewart 	 * This is called by asconf work. It is assumed that a) The TCB is
5360f8829a4aSRandall Stewart 	 * locked and b) The INP is locked. This is true in as much as I can
5361f8829a4aSRandall Stewart 	 * trace through the entry asconf code where I did these locks.
5362f8829a4aSRandall Stewart 	 * Again, the ASCONF code is a bit different in that it does lock
5363f8829a4aSRandall Stewart 	 * the INP during its work often times. This must be since we don't
5364f8829a4aSRandall Stewart 	 * want other proc's looking up things while what they are looking
5365f8829a4aSRandall Stewart 	 * up is changing :-D
5366f8829a4aSRandall Stewart 	 */
5367f8829a4aSRandall Stewart 
5368f8829a4aSRandall Stewart 	inp = stcb->sctp_ep;
5369f8829a4aSRandall Stewart 	/* if subset bound and don't allow ASCONF's, can't delete last */
5370f8829a4aSRandall Stewart 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
537118e198d3SRandall Stewart 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
537218e198d3SRandall Stewart 		if (stcb->sctp_ep->laddr_count < 2) {
5373f8829a4aSRandall Stewart 			/* can't delete last address */
5374ad81507eSRandall Stewart 			return;
5375f8829a4aSRandall Stewart 		}
5376f8829a4aSRandall Stewart 	}
537742551e99SRandall Stewart 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5378f8829a4aSRandall Stewart 		/* remove the address if it exists */
5379f8829a4aSRandall Stewart 		if (laddr->ifa == NULL)
5380f8829a4aSRandall Stewart 			continue;
5381f8829a4aSRandall Stewart 		if (laddr->ifa == ifa) {
5382f8829a4aSRandall Stewart 			sctp_remove_laddr(laddr);
5383ad81507eSRandall Stewart 			return;
5384f8829a4aSRandall Stewart 		}
5385f8829a4aSRandall Stewart 	}
5386f8829a4aSRandall Stewart 
5387f8829a4aSRandall Stewart 	/* address not found! */
5388ad81507eSRandall Stewart 	return;
5389f8829a4aSRandall Stewart }
5390f8829a4aSRandall Stewart 
5391f8829a4aSRandall Stewart /* sysctl */
5392f8829a4aSRandall Stewart static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5393f8829a4aSRandall Stewart static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5394f8829a4aSRandall Stewart 
53952f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
5396bfc46083SRandall Stewart struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
53970071ee5eSRandall Stewart int *sctp_cpuarry = NULL;
53982f9e6db0SMichael Tuexen 
5399bfc46083SRandall Stewart void
sctp_queue_to_mcore(struct mbuf * m,int off,int cpu_to_use)5400bfc46083SRandall Stewart sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
5401bfc46083SRandall Stewart {
5402bfc46083SRandall Stewart 	/* Queue a packet to a processor for the specified core */
5403bfc46083SRandall Stewart 	struct sctp_mcore_queue *qent;
5404bfc46083SRandall Stewart 	struct sctp_mcore_ctrl *wkq;
5405bfc46083SRandall Stewart 	int need_wake = 0;
5406bfc46083SRandall Stewart 
5407bfc46083SRandall Stewart 	if (sctp_mcore_workers == NULL) {
5408bfc46083SRandall Stewart 		/* Something went way bad during setup */
5409bfc46083SRandall Stewart 		sctp_input_with_port(m, off, 0);
5410bfc46083SRandall Stewart 		return;
5411bfc46083SRandall Stewart 	}
5412bfc46083SRandall Stewart 	SCTP_MALLOC(qent, struct sctp_mcore_queue *,
5413bfc46083SRandall Stewart 	    (sizeof(struct sctp_mcore_queue)),
5414bfc46083SRandall Stewart 	    SCTP_M_MCORE);
5415bfc46083SRandall Stewart 	if (qent == NULL) {
5416bfc46083SRandall Stewart 		/* This is trouble  */
5417bfc46083SRandall Stewart 		sctp_input_with_port(m, off, 0);
5418bfc46083SRandall Stewart 		return;
5419bfc46083SRandall Stewart 	}
5420bfc46083SRandall Stewart 	qent->vn = curvnet;
5421bfc46083SRandall Stewart 	qent->m = m;
5422bfc46083SRandall Stewart 	qent->off = off;
5423bfc46083SRandall Stewart 	qent->v6 = 0;
5424bfc46083SRandall Stewart 	wkq = &sctp_mcore_workers[cpu_to_use];
5425bfc46083SRandall Stewart 	SCTP_MCORE_QLOCK(wkq);
5426bfc46083SRandall Stewart 
5427bfc46083SRandall Stewart 	TAILQ_INSERT_TAIL(&wkq->que, qent, next);
5428bfc46083SRandall Stewart 	if (wkq->running == 0) {
5429bfc46083SRandall Stewart 		need_wake = 1;
5430bfc46083SRandall Stewart 	}
5431bfc46083SRandall Stewart 	SCTP_MCORE_QUNLOCK(wkq);
5432bfc46083SRandall Stewart 	if (need_wake) {
5433bfc46083SRandall Stewart 		wakeup(&wkq->running);
5434bfc46083SRandall Stewart 	}
5435bfc46083SRandall Stewart }
5436bfc46083SRandall Stewart 
5437bfc46083SRandall Stewart static void
sctp_mcore_thread(void * arg)5438bfc46083SRandall Stewart sctp_mcore_thread(void *arg)
5439bfc46083SRandall Stewart {
5440bfc46083SRandall Stewart 
5441bfc46083SRandall Stewart 	struct sctp_mcore_ctrl *wkq;
5442bfc46083SRandall Stewart 	struct sctp_mcore_queue *qent;
5443bfc46083SRandall Stewart 
5444bfc46083SRandall Stewart 	wkq = (struct sctp_mcore_ctrl *)arg;
5445bfc46083SRandall Stewart 	struct mbuf *m;
5446bfc46083SRandall Stewart 	int off, v6;
5447bfc46083SRandall Stewart 
5448bfc46083SRandall Stewart 	/* Wait for first tickle */
5449bfc46083SRandall Stewart 	SCTP_MCORE_LOCK(wkq);
5450bfc46083SRandall Stewart 	wkq->running = 0;
5451bfc46083SRandall Stewart 	msleep(&wkq->running,
5452bfc46083SRandall Stewart 	    &wkq->core_mtx,
5453bfc46083SRandall Stewart 	    0, "wait for pkt", 0);
5454bfc46083SRandall Stewart 	SCTP_MCORE_UNLOCK(wkq);
5455bfc46083SRandall Stewart 
5456bfc46083SRandall Stewart 	/* Bind to our cpu */
5457bfc46083SRandall Stewart 	thread_lock(curthread);
5458bfc46083SRandall Stewart 	sched_bind(curthread, wkq->cpuid);
5459bfc46083SRandall Stewart 	thread_unlock(curthread);
5460bfc46083SRandall Stewart 
5461bfc46083SRandall Stewart 	/* Now lets start working */
5462bfc46083SRandall Stewart 	SCTP_MCORE_LOCK(wkq);
5463bfc46083SRandall Stewart 	/* Now grab lock and go */
54647215cc1bSMichael Tuexen 	for (;;) {
5465bfc46083SRandall Stewart 		SCTP_MCORE_QLOCK(wkq);
5466bfc46083SRandall Stewart skip_sleep:
5467bfc46083SRandall Stewart 		wkq->running = 1;
5468bfc46083SRandall Stewart 		qent = TAILQ_FIRST(&wkq->que);
5469bfc46083SRandall Stewart 		if (qent) {
5470bfc46083SRandall Stewart 			TAILQ_REMOVE(&wkq->que, qent, next);
5471bfc46083SRandall Stewart 			SCTP_MCORE_QUNLOCK(wkq);
5472bfc46083SRandall Stewart 			CURVNET_SET(qent->vn);
5473bfc46083SRandall Stewart 			m = qent->m;
5474bfc46083SRandall Stewart 			off = qent->off;
5475bfc46083SRandall Stewart 			v6 = qent->v6;
5476bfc46083SRandall Stewart 			SCTP_FREE(qent, SCTP_M_MCORE);
5477bfc46083SRandall Stewart 			if (v6 == 0) {
5478bfc46083SRandall Stewart 				sctp_input_with_port(m, off, 0);
5479bfc46083SRandall Stewart 			} else {
5480cd3fd531SMichael Tuexen 				SCTP_PRINTF("V6 not yet supported\n");
5481bfc46083SRandall Stewart 				sctp_m_freem(m);
5482bfc46083SRandall Stewart 			}
5483bfc46083SRandall Stewart 			CURVNET_RESTORE();
5484bfc46083SRandall Stewart 			SCTP_MCORE_QLOCK(wkq);
5485bfc46083SRandall Stewart 		}
5486bfc46083SRandall Stewart 		wkq->running = 0;
5487bfc46083SRandall Stewart 		if (!TAILQ_EMPTY(&wkq->que)) {
5488bfc46083SRandall Stewart 			goto skip_sleep;
5489bfc46083SRandall Stewart 		}
5490bfc46083SRandall Stewart 		SCTP_MCORE_QUNLOCK(wkq);
5491bfc46083SRandall Stewart 		msleep(&wkq->running,
5492bfc46083SRandall Stewart 		    &wkq->core_mtx,
5493bfc46083SRandall Stewart 		    0, "wait for pkt", 0);
549460990c0cSMichael Tuexen 	}
5495bfc46083SRandall Stewart }
5496bfc46083SRandall Stewart 
5497bfc46083SRandall Stewart static void
sctp_startup_mcore_threads(void)5498bfc46083SRandall Stewart sctp_startup_mcore_threads(void)
5499bfc46083SRandall Stewart {
55000071ee5eSRandall Stewart 	int i, cpu;
5501bfc46083SRandall Stewart 
55020071ee5eSRandall Stewart 	if (mp_ncpus == 1)
5503bfc46083SRandall Stewart 		return;
5504bfc46083SRandall Stewart 
55050071ee5eSRandall Stewart 	if (sctp_mcore_workers != NULL) {
55060071ee5eSRandall Stewart 		/*
55070071ee5eSRandall Stewart 		 * Already been here in some previous vnet?
55080071ee5eSRandall Stewart 		 */
55090071ee5eSRandall Stewart 		return;
55100071ee5eSRandall Stewart 	}
5511bfc46083SRandall Stewart 	SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
55120071ee5eSRandall Stewart 	    ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)),
5513bfc46083SRandall Stewart 	    SCTP_M_MCORE);
5514bfc46083SRandall Stewart 	if (sctp_mcore_workers == NULL) {
5515bfc46083SRandall Stewart 		/* TSNH I hope */
5516bfc46083SRandall Stewart 		return;
5517bfc46083SRandall Stewart 	}
55180071ee5eSRandall Stewart 	memset(sctp_mcore_workers, 0, ((mp_maxid + 1) *
5519bfc46083SRandall Stewart 	    sizeof(struct sctp_mcore_ctrl)));
5520bfc46083SRandall Stewart 	/* Init the structures */
55210071ee5eSRandall Stewart 	for (i = 0; i <= mp_maxid; i++) {
5522bfc46083SRandall Stewart 		TAILQ_INIT(&sctp_mcore_workers[i].que);
5523bfc46083SRandall Stewart 		SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
5524bfc46083SRandall Stewart 		SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
5525bfc46083SRandall Stewart 		sctp_mcore_workers[i].cpuid = i;
5526bfc46083SRandall Stewart 	}
55270071ee5eSRandall Stewart 	if (sctp_cpuarry == NULL) {
55280071ee5eSRandall Stewart 		SCTP_MALLOC(sctp_cpuarry, int *,
55290071ee5eSRandall Stewart 		    (mp_ncpus * sizeof(int)),
55300071ee5eSRandall Stewart 		    SCTP_M_MCORE);
55310071ee5eSRandall Stewart 		i = 0;
55320071ee5eSRandall Stewart 		CPU_FOREACH(cpu) {
55330071ee5eSRandall Stewart 			sctp_cpuarry[i] = cpu;
55340071ee5eSRandall Stewart 			i++;
55350071ee5eSRandall Stewart 		}
55360071ee5eSRandall Stewart 	}
5537bfc46083SRandall Stewart 	/* Now start them all */
55380071ee5eSRandall Stewart 	CPU_FOREACH(cpu) {
5539bfc46083SRandall Stewart 		(void)kproc_create(sctp_mcore_thread,
55400071ee5eSRandall Stewart 		    (void *)&sctp_mcore_workers[cpu],
55410071ee5eSRandall Stewart 		    &sctp_mcore_workers[cpu].thread_proc,
55425a50eb65SJohn Baldwin 		    0,
5543bfc46083SRandall Stewart 		    SCTP_KTHREAD_PAGES,
5544bfc46083SRandall Stewart 		    SCTP_MCORE_NAME);
5545bfc46083SRandall Stewart 	}
5546bfc46083SRandall Stewart }
5547bfc46083SRandall Stewart #endif
5548bfc46083SRandall Stewart 
5549dac91eb7SZhenlei Huang #define VALIDATE_LOADER_TUNABLE(var_name, prefix)		\
5550dac91eb7SZhenlei Huang 	if (SCTP_BASE_SYSCTL(var_name) < prefix##_MIN ||	\
5551dac91eb7SZhenlei Huang 	    SCTP_BASE_SYSCTL(var_name) > prefix##_MAX)		\
5552dac91eb7SZhenlei Huang 		SCTP_BASE_SYSCTL(var_name) = prefix##_DEFAULT
5553dac91eb7SZhenlei Huang 
5554f8829a4aSRandall Stewart void
sctp_pcb_init(void)5555e30a1788SMichael Tuexen sctp_pcb_init(void)
5556f8829a4aSRandall Stewart {
5557f8829a4aSRandall Stewart 	/*
5558f8829a4aSRandall Stewart 	 * SCTP initialization for the PCB structures should be called by
5559cd0a4ff6SPedro F. Giffuni 	 * the sctp_init() function.
5560f8829a4aSRandall Stewart 	 */
5561f8829a4aSRandall Stewart 	int i;
5562851b7298SRandall Stewart 	struct timeval tv;
5563f8829a4aSRandall Stewart 
5564b3f1ea41SRandall Stewart 	if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5565f8829a4aSRandall Stewart 		/* error I was called twice */
5566f8829a4aSRandall Stewart 		return;
5567f8829a4aSRandall Stewart 	}
5568b3f1ea41SRandall Stewart 	SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5569f8829a4aSRandall Stewart 
5570b27a6b7dSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
55715ba7f91fSMichael Tuexen 	memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
5572b27a6b7dSRandall Stewart #endif
55732f9e6db0SMichael Tuexen #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5574ae26e0a4SRandall Stewart 	SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
55750071ee5eSRandall Stewart 	    ((mp_maxid + 1) * sizeof(struct sctpstat)),
5576ae26e0a4SRandall Stewart 	    SCTP_M_MCORE);
5577ae26e0a4SRandall Stewart #endif
5578851b7298SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&tv);
55792f9e6db0SMichael Tuexen #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
55805ba7f91fSMichael Tuexen 	memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid + 1));
5581ff014514SRandall Stewart 	SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
5582ff014514SRandall Stewart 	SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
5583ff014514SRandall Stewart #else
55845ba7f91fSMichael Tuexen 	memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
5585b3f1ea41SRandall Stewart 	SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
5586b3f1ea41SRandall Stewart 	SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
5587ff014514SRandall Stewart #endif
5588f8829a4aSRandall Stewart 	/* init the empty list of (All) Endpoints */
5589b3f1ea41SRandall Stewart 	LIST_INIT(&SCTP_BASE_INFO(listhead));
5590f8829a4aSRandall Stewart 
5591f8829a4aSRandall Stewart 	/* init the hash table of endpoints */
5592b3f1ea41SRandall Stewart 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5593b3f1ea41SRandall Stewart 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5594b3f1ea41SRandall Stewart 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5595dac91eb7SZhenlei Huang 	VALIDATE_LOADER_TUNABLE(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE);
5596dac91eb7SZhenlei Huang 	VALIDATE_LOADER_TUNABLE(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE);
5597dac91eb7SZhenlei Huang 	VALIDATE_LOADER_TUNABLE(sctp_chunkscale, SCTPCTL_CHUNKSCALE);
5598b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5599b3f1ea41SRandall Stewart 	    &SCTP_BASE_INFO(hashasocmark));
5600b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5601b3f1ea41SRandall Stewart 	    &SCTP_BASE_INFO(hashmark));
5602b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5603b3f1ea41SRandall Stewart 	    &SCTP_BASE_INFO(hashtcpmark));
5604b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5605b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5606b3f1ea41SRandall Stewart 	    &SCTP_BASE_INFO(hashvrfmark));
560742551e99SRandall Stewart 
5608b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5609b3f1ea41SRandall Stewart 	    &SCTP_BASE_INFO(vrf_ifn_hashmark));
5610f8829a4aSRandall Stewart 	/* init the zones */
5611f8829a4aSRandall Stewart 	/*
5612f8829a4aSRandall Stewart 	 * FIX ME: Should check for NULL returns, but if it does fail we are
5613f8829a4aSRandall Stewart 	 * doomed to panic anyways... add later maybe.
5614f8829a4aSRandall Stewart 	 */
5615b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5616f8829a4aSRandall Stewart 	    sizeof(struct sctp_inpcb), maxsockets);
5617f8829a4aSRandall Stewart 
5618b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5619f8829a4aSRandall Stewart 	    sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5620f8829a4aSRandall Stewart 
5621b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5622f8829a4aSRandall Stewart 	    sizeof(struct sctp_laddr),
5623f8829a4aSRandall Stewart 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5624f8829a4aSRandall Stewart 
5625b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5626f8829a4aSRandall Stewart 	    sizeof(struct sctp_nets),
5627f8829a4aSRandall Stewart 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5628f8829a4aSRandall Stewart 
5629b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5630f8829a4aSRandall Stewart 	    sizeof(struct sctp_tmit_chunk),
5631b3f1ea41SRandall Stewart 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5632f8829a4aSRandall Stewart 
5633b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5634f8829a4aSRandall Stewart 	    sizeof(struct sctp_queued_to_read),
5635b3f1ea41SRandall Stewart 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5636f8829a4aSRandall Stewart 
5637b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5638f8829a4aSRandall Stewart 	    sizeof(struct sctp_stream_queue_pending),
5639b3f1ea41SRandall Stewart 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5640f8829a4aSRandall Stewart 
5641b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5642c54a18d2SRandall Stewart 	    sizeof(struct sctp_asconf),
5643b3f1ea41SRandall Stewart 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5644c54a18d2SRandall Stewart 
5645b3f1ea41SRandall Stewart 	SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
56462afb3e84SRandall Stewart 	    sizeof(struct sctp_asconf_ack),
5647b3f1ea41SRandall Stewart 	    (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
56482afb3e84SRandall Stewart 
5649f8829a4aSRandall Stewart 	/* Master Lock INIT for info structure */
5650f8829a4aSRandall Stewart 	SCTP_INP_INFO_LOCK_INIT();
5651f8829a4aSRandall Stewart 	SCTP_STATLOG_INIT_LOCK();
5652a5d547adSRandall Stewart 
5653f8829a4aSRandall Stewart 	SCTP_IPI_COUNT_INIT();
5654f8829a4aSRandall Stewart 	SCTP_IPI_ADDR_INIT();
5655207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
5656207304d4SRandall Stewart 	SCTP_IP_PKTLOG_INIT();
5657207304d4SRandall Stewart #endif
5658b3f1ea41SRandall Stewart 	LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5659f8829a4aSRandall Stewart 
5660f7517433SRandall Stewart 	SCTP_WQ_ADDR_INIT();
5661f8829a4aSRandall Stewart 	/* not sure if we need all the counts */
5662b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_ep) = 0;
5663f8829a4aSRandall Stewart 	/* assoc/tcb zone info */
5664b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_asoc) = 0;
5665f8829a4aSRandall Stewart 	/* local addrlist zone info */
5666b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_laddr) = 0;
5667f8829a4aSRandall Stewart 	/* remote addrlist zone info */
5668b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_raddr) = 0;
5669f8829a4aSRandall Stewart 	/* chunk info */
5670b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_chunk) = 0;
5671f8829a4aSRandall Stewart 
5672f8829a4aSRandall Stewart 	/* socket queue zone info */
5673b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_readq) = 0;
5674f8829a4aSRandall Stewart 
5675f8829a4aSRandall Stewart 	/* stream out queue cont */
5676b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5677f8829a4aSRandall Stewart 
5678b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5679b3f1ea41SRandall Stewart 	SCTP_BASE_INFO(ipi_free_chunks) = 0;
5680f8829a4aSRandall Stewart 
5681b3f1ea41SRandall Stewart 	SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5682f8829a4aSRandall Stewart 
5683f8829a4aSRandall Stewart 	/* Init the TIMEWAIT list */
5684a1e13272SRandall Stewart 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5685f5366806SMichael Tuexen 		LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
5686f8829a4aSRandall Stewart 	}
568742551e99SRandall Stewart 	sctp_startup_iterator();
568842551e99SRandall Stewart 
56892f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
5690bfc46083SRandall Stewart 	sctp_startup_mcore_threads();
5691bfc46083SRandall Stewart #endif
5692bfc46083SRandall Stewart 
569342551e99SRandall Stewart 	/*
569442551e99SRandall Stewart 	 * INIT the default VRF which for BSD is the only one, other O/S's
569542551e99SRandall Stewart 	 * may have more. But initially they must start with one and then
569642551e99SRandall Stewart 	 * add the VRF's as addresses are added.
569742551e99SRandall Stewart 	 */
569842551e99SRandall Stewart 	sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5699f8829a4aSRandall Stewart }
5700f8829a4aSRandall Stewart 
5701c54a18d2SRandall Stewart /*
5702b3f1ea41SRandall Stewart  * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5703c54a18d2SRandall Stewart  */
5704c54a18d2SRandall Stewart void
sctp_pcb_finish(void)5705c54a18d2SRandall Stewart sctp_pcb_finish(void)
5706c54a18d2SRandall Stewart {
5707c54a18d2SRandall Stewart 	struct sctp_vrflist *vrf_bucket;
57084a9ef3f8SMichael Tuexen 	struct sctp_vrf *vrf, *nvrf;
57094a9ef3f8SMichael Tuexen 	struct sctp_ifn *ifn, *nifn;
57104a9ef3f8SMichael Tuexen 	struct sctp_ifa *ifa, *nifa;
5711b3f1ea41SRandall Stewart 	struct sctpvtaghead *chain;
5712b3f1ea41SRandall Stewart 	struct sctp_tagblock *twait_block, *prev_twait_block;
57134a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
5714b3f1ea41SRandall Stewart 	int i;
5715f7517433SRandall Stewart 	struct sctp_iterator *it, *nit;
5716b6c57802SMichael Tuexen 
57172b1c7de4SMichael Tuexen 	if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
57182b1c7de4SMichael Tuexen 		SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
57192b1c7de4SMichael Tuexen 		return;
57202b1c7de4SMichael Tuexen 	}
57212b1c7de4SMichael Tuexen 	SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
5722f4f34bdeSMichael Tuexen 	/*
5723f4f34bdeSMichael Tuexen 	 * In FreeBSD the iterator thread never exits but we do clean up.
5724f4f34bdeSMichael Tuexen 	 * The only way FreeBSD reaches here is if we have VRF's but we
5725f4f34bdeSMichael Tuexen 	 * still add the ifdef to make it compile on old versions.
5726f4f34bdeSMichael Tuexen 	 */
57272b1c7de4SMichael Tuexen retry:
5728b6c57802SMichael Tuexen 	SCTP_IPI_ITERATOR_WQ_LOCK();
57292b1c7de4SMichael Tuexen 	/*
57302b1c7de4SMichael Tuexen 	 * sctp_iterator_worker() might be working on an it entry without
57312b1c7de4SMichael Tuexen 	 * holding the lock.  We won't find it on the list either and
57322b1c7de4SMichael Tuexen 	 * continue and free/destroy it.  While holding the lock, spin, to
57332b1c7de4SMichael Tuexen 	 * avoid the race condition as sctp_iterator_worker() will have to
57343f53d622SMichael Tuexen 	 * wait to re-acquire the lock.
57352b1c7de4SMichael Tuexen 	 */
57362b1c7de4SMichael Tuexen 	if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
57372b1c7de4SMichael Tuexen 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
57382b1c7de4SMichael Tuexen 		SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
57392b1c7de4SMichael Tuexen 		    "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
57402b1c7de4SMichael Tuexen 		DELAY(10);
57412b1c7de4SMichael Tuexen 		goto retry;
57422b1c7de4SMichael Tuexen 	}
57434a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
5744f7517433SRandall Stewart 		if (it->vn != curvnet) {
5745f7517433SRandall Stewart 			continue;
5746f7517433SRandall Stewart 		}
57474a9ef3f8SMichael Tuexen 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
5748f7517433SRandall Stewart 		if (it->function_atend != NULL) {
5749f7517433SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
5750f7517433SRandall Stewart 		}
5751f7517433SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
5752f7517433SRandall Stewart 	}
5753f7517433SRandall Stewart 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
5754f7517433SRandall Stewart 	SCTP_ITERATOR_LOCK();
5755f7517433SRandall Stewart 	if ((sctp_it_ctl.cur_it) &&
5756f7517433SRandall Stewart 	    (sctp_it_ctl.cur_it->vn == curvnet)) {
5757f7517433SRandall Stewart 		sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
5758f7517433SRandall Stewart 	}
5759f7517433SRandall Stewart 	SCTP_ITERATOR_UNLOCK();
57602b1c7de4SMichael Tuexen 	SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
5761f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
57624a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
5763b6c57802SMichael Tuexen 		LIST_REMOVE(wi, sctp_nxt_addr);
5764b6c57802SMichael Tuexen 		SCTP_DECR_LADDR_COUNT();
576524110da0SMichael Tuexen 		if (wi->action == SCTP_DEL_IP_ADDRESS) {
576624110da0SMichael Tuexen 			SCTP_FREE(wi->ifa, SCTP_M_IFA);
576724110da0SMichael Tuexen 		}
5768b6c57802SMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
5769b6c57802SMichael Tuexen 	}
5770f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
5771b6c57802SMichael Tuexen 
5772c54a18d2SRandall Stewart 	/*
5773c54a18d2SRandall Stewart 	 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5774c54a18d2SRandall Stewart 	 * destroyed first).
5775c54a18d2SRandall Stewart 	 */
57767f0ad227SMichael Tuexen 	SCTP_IPI_ADDR_WLOCK();
5777b3f1ea41SRandall Stewart 	vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
57784a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
57794a9ef3f8SMichael Tuexen 		LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
57804a9ef3f8SMichael Tuexen 			LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
5781c54a18d2SRandall Stewart 				/* free the ifa */
5782c54a18d2SRandall Stewart 				LIST_REMOVE(ifa, next_bucket);
5783c54a18d2SRandall Stewart 				LIST_REMOVE(ifa, next_ifa);
5784c54a18d2SRandall Stewart 				SCTP_FREE(ifa, SCTP_M_IFA);
5785c54a18d2SRandall Stewart 			}
5786c54a18d2SRandall Stewart 			/* free the ifn */
5787c54a18d2SRandall Stewart 			LIST_REMOVE(ifn, next_bucket);
5788c54a18d2SRandall Stewart 			LIST_REMOVE(ifn, next_ifn);
5789c54a18d2SRandall Stewart 			SCTP_FREE(ifn, SCTP_M_IFN);
5790c54a18d2SRandall Stewart 		}
5791c54a18d2SRandall Stewart 		SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
5792c54a18d2SRandall Stewart 		/* free the vrf */
5793c54a18d2SRandall Stewart 		LIST_REMOVE(vrf, next_vrf);
5794c54a18d2SRandall Stewart 		SCTP_FREE(vrf, SCTP_M_VRF);
5795c54a18d2SRandall Stewart 	}
57967f0ad227SMichael Tuexen 	SCTP_IPI_ADDR_WUNLOCK();
5797c54a18d2SRandall Stewart 	/* free the vrf hashes */
5798b3f1ea41SRandall Stewart 	SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
5799b3f1ea41SRandall Stewart 	SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
5800b3f1ea41SRandall Stewart 
5801b3f1ea41SRandall Stewart 	/*
5802b3f1ea41SRandall Stewart 	 * free the TIMEWAIT list elements malloc'd in the function
5803b3f1ea41SRandall Stewart 	 * sctp_add_vtag_to_timewait()...
5804b3f1ea41SRandall Stewart 	 */
5805a1e13272SRandall Stewart 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5806b3f1ea41SRandall Stewart 		chain = &SCTP_BASE_INFO(vtag_timewait)[i];
580783fc1165SMichael Tuexen 		if (!LIST_EMPTY(chain)) {
5808b3f1ea41SRandall Stewart 			prev_twait_block = NULL;
5809b3f1ea41SRandall Stewart 			LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5810b3f1ea41SRandall Stewart 				if (prev_twait_block) {
5811b3f1ea41SRandall Stewart 					SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5812b3f1ea41SRandall Stewart 				}
5813b3f1ea41SRandall Stewart 				prev_twait_block = twait_block;
5814b3f1ea41SRandall Stewart 			}
5815b3f1ea41SRandall Stewart 			SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5816b3f1ea41SRandall Stewart 		}
5817b3f1ea41SRandall Stewart 	}
5818c54a18d2SRandall Stewart 
5819c54a18d2SRandall Stewart 	/* free the locks and mutexes */
5820c54a18d2SRandall Stewart #ifdef SCTP_PACKET_LOGGING
5821c54a18d2SRandall Stewart 	SCTP_IP_PKTLOG_DESTROY();
5822c54a18d2SRandall Stewart #endif
5823c54a18d2SRandall Stewart 	SCTP_IPI_ADDR_DESTROY();
5824c54a18d2SRandall Stewart 	SCTP_STATLOG_DESTROY();
5825c54a18d2SRandall Stewart 	SCTP_INP_INFO_LOCK_DESTROY();
5826c54a18d2SRandall Stewart 
5827f7517433SRandall Stewart 	SCTP_WQ_ADDR_DESTROY();
5828f7517433SRandall Stewart 
58292b1c7de4SMichael Tuexen 	/* Get rid of other stuff too. */
58302b1c7de4SMichael Tuexen 	if (SCTP_BASE_INFO(sctp_asochash) != NULL)
58312b1c7de4SMichael Tuexen 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
58322b1c7de4SMichael Tuexen 	if (SCTP_BASE_INFO(sctp_ephash) != NULL)
58332b1c7de4SMichael Tuexen 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
58342b1c7de4SMichael Tuexen 	if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
58352b1c7de4SMichael Tuexen 		SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
58362b1c7de4SMichael Tuexen 
5837b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
5838b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
5839b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
5840b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
5841b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
5842b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
5843b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
5844b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
5845b3f1ea41SRandall Stewart 	SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
58462f9e6db0SMichael Tuexen #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5847ae26e0a4SRandall Stewart 	SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
5848ae26e0a4SRandall Stewart #endif
5849c54a18d2SRandall Stewart }
5850c54a18d2SRandall Stewart 
5851f8829a4aSRandall Stewart int
sctp_load_addresses_from_init(struct sctp_tcb * stcb,struct mbuf * m,int offset,int limit,struct sockaddr * src,struct sockaddr * dst,struct sockaddr * altsa,uint16_t port)5852f8829a4aSRandall Stewart sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
5853b1754ad1SMichael Tuexen     int offset, int limit,
5854b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5855ec70917fSMichael Tuexen     struct sockaddr *altsa, uint16_t port)
5856f8829a4aSRandall Stewart {
5857f8829a4aSRandall Stewart 	/*
5858f8829a4aSRandall Stewart 	 * grub through the INIT pulling addresses and loading them to the
5859f8829a4aSRandall Stewart 	 * nets structure in the asoc. The from address in the mbuf should
5860f8829a4aSRandall Stewart 	 * also be loaded (if it is not already). This routine can be called
5861f8829a4aSRandall Stewart 	 * with either INIT or INIT-ACK's as long as the m points to the IP
5862f8829a4aSRandall Stewart 	 * packet and the offset points to the beginning of the parameters.
5863f8829a4aSRandall Stewart 	 */
586460990c0cSMichael Tuexen 	struct sctp_inpcb *inp;
58654a9ef3f8SMichael Tuexen 	struct sctp_nets *net, *nnet, *net_tmp;
5866f4358911SMichael Tuexen 	struct sctp_paramhdr *phdr, param_buf;
5867f8829a4aSRandall Stewart 	struct sctp_tcb *stcb_tmp;
5868f8829a4aSRandall Stewart 	uint16_t ptype, plen;
5869f8829a4aSRandall Stewart 	struct sockaddr *sa;
5870f42a358aSRandall Stewart 	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
58715e54f665SRandall Stewart 	struct sctp_auth_random *p_random = NULL;
5872f8829a4aSRandall Stewart 	uint16_t random_len = 0;
5873f42a358aSRandall Stewart 	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
5874f8829a4aSRandall Stewart 	struct sctp_auth_hmac_algo *hmacs = NULL;
5875f8829a4aSRandall Stewart 	uint16_t hmacs_len = 0;
58762afb3e84SRandall Stewart 	uint8_t saw_asconf = 0;
58772afb3e84SRandall Stewart 	uint8_t saw_asconf_ack = 0;
5878f42a358aSRandall Stewart 	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
5879f8829a4aSRandall Stewart 	struct sctp_auth_chunk_list *chunks = NULL;
5880f8829a4aSRandall Stewart 	uint16_t num_chunks = 0;
5881f8829a4aSRandall Stewart 	sctp_key_t *new_key;
5882f8829a4aSRandall Stewart 	uint32_t keylen;
5883f8829a4aSRandall Stewart 	int got_random = 0, got_hmacs = 0, got_chklist = 0;
5884c79bec9cSMichael Tuexen 	uint8_t peer_supports_ecn;
5885c79bec9cSMichael Tuexen 	uint8_t peer_supports_prsctp;
5886c79bec9cSMichael Tuexen 	uint8_t peer_supports_auth;
5887c79bec9cSMichael Tuexen 	uint8_t peer_supports_asconf;
5888c79bec9cSMichael Tuexen 	uint8_t peer_supports_asconf_ack;
5889c79bec9cSMichael Tuexen 	uint8_t peer_supports_reconfig;
5890c79bec9cSMichael Tuexen 	uint8_t peer_supports_nrsack;
5891c79bec9cSMichael Tuexen 	uint8_t peer_supports_pktdrop;
589244249214SRandall Stewart 	uint8_t peer_supports_idata;
5893e6194c2eSMichael Tuexen #ifdef INET
5894e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
5895e6194c2eSMichael Tuexen #endif
5896e6194c2eSMichael Tuexen #ifdef INET6
5897e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
5898e6194c2eSMichael Tuexen #endif
5899e6194c2eSMichael Tuexen 
5900e6194c2eSMichael Tuexen 	/* First get the destination address setup too. */
5901e6194c2eSMichael Tuexen #ifdef INET
5902e6194c2eSMichael Tuexen 	memset(&sin, 0, sizeof(sin));
5903f8829a4aSRandall Stewart 	sin.sin_family = AF_INET;
5904f8829a4aSRandall Stewart 	sin.sin_len = sizeof(sin);
5905f8829a4aSRandall Stewart 	sin.sin_port = stcb->rport;
5906e6194c2eSMichael Tuexen #endif
5907e6194c2eSMichael Tuexen #ifdef INET6
5908e6194c2eSMichael Tuexen 	memset(&sin6, 0, sizeof(sin6));
5909f8829a4aSRandall Stewart 	sin6.sin6_family = AF_INET6;
5910f8829a4aSRandall Stewart 	sin6.sin6_len = sizeof(struct sockaddr_in6);
5911f8829a4aSRandall Stewart 	sin6.sin6_port = stcb->rport;
5912e6194c2eSMichael Tuexen #endif
5913dc81ec89SMichael Tuexen 	if (altsa) {
5914dc81ec89SMichael Tuexen 		sa = altsa;
5915dc81ec89SMichael Tuexen 	} else {
5916b1754ad1SMichael Tuexen 		sa = src;
5917f8829a4aSRandall Stewart 	}
591844249214SRandall Stewart 	peer_supports_idata = 0;
5919c79bec9cSMichael Tuexen 	peer_supports_ecn = 0;
5920c79bec9cSMichael Tuexen 	peer_supports_prsctp = 0;
5921c79bec9cSMichael Tuexen 	peer_supports_auth = 0;
5922c79bec9cSMichael Tuexen 	peer_supports_asconf = 0;
5923b954d816SMichael Tuexen 	peer_supports_asconf_ack = 0;
5924c79bec9cSMichael Tuexen 	peer_supports_reconfig = 0;
5925c79bec9cSMichael Tuexen 	peer_supports_nrsack = 0;
5926c79bec9cSMichael Tuexen 	peer_supports_pktdrop = 0;
5927f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5928f8829a4aSRandall Stewart 		/* mark all addresses that we have currently on the list */
5929f8829a4aSRandall Stewart 		net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
5930f8829a4aSRandall Stewart 	}
5931f8829a4aSRandall Stewart 	/* does the source address already exist? if so skip it */
593260990c0cSMichael Tuexen 	inp = stcb->sctp_ep;
593350cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5934b1754ad1SMichael Tuexen 	stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
59353c1ba6f3SMichael Tuexen 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
5936f8829a4aSRandall Stewart 
5937f8829a4aSRandall Stewart 	if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
5938f8829a4aSRandall Stewart 		/* we must add the source address */
5939f8829a4aSRandall Stewart 		/* no scope set here since we have a tcb already. */
5940e6194c2eSMichael Tuexen 		switch (sa->sa_family) {
5941e6194c2eSMichael Tuexen #ifdef INET
5942e6194c2eSMichael Tuexen 		case AF_INET:
5943a1cb341bSMichael Tuexen 			if (stcb->asoc.scope.ipv4_addr_legal) {
5944ec70917fSMichael Tuexen 				if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
5945f8829a4aSRandall Stewart 					return (-1);
5946f8829a4aSRandall Stewart 				}
5947e6194c2eSMichael Tuexen 			}
5948e6194c2eSMichael Tuexen 			break;
5949e6194c2eSMichael Tuexen #endif
5950e6194c2eSMichael Tuexen #ifdef INET6
5951e6194c2eSMichael Tuexen 		case AF_INET6:
5952a1cb341bSMichael Tuexen 			if (stcb->asoc.scope.ipv6_addr_legal) {
5953ec70917fSMichael Tuexen 				if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
5954f8829a4aSRandall Stewart 					return (-2);
5955f8829a4aSRandall Stewart 				}
5956f8829a4aSRandall Stewart 			}
5957e6194c2eSMichael Tuexen 			break;
5958e6194c2eSMichael Tuexen #endif
5959e6194c2eSMichael Tuexen 		default:
5960e6194c2eSMichael Tuexen 			break;
5961e6194c2eSMichael Tuexen 		}
5962f8829a4aSRandall Stewart 	} else {
5963f8829a4aSRandall Stewart 		if (net_tmp != NULL && stcb_tmp == stcb) {
5964f8829a4aSRandall Stewart 			net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
5965f8829a4aSRandall Stewart 		} else if (stcb_tmp != stcb) {
5966f8829a4aSRandall Stewart 			/* It belongs to another association? */
5967eacc51c5SRandall Stewart 			if (stcb_tmp)
5968f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb_tmp);
5969f8829a4aSRandall Stewart 			return (-3);
5970f8829a4aSRandall Stewart 		}
5971f8829a4aSRandall Stewart 	}
5972f8829a4aSRandall Stewart 	if (stcb->asoc.state == 0) {
5973f8829a4aSRandall Stewart 		/* the assoc was freed? */
5974f8829a4aSRandall Stewart 		return (-4);
5975f8829a4aSRandall Stewart 	}
5976f8829a4aSRandall Stewart 	/* now we must go through each of the params. */
5977f4358911SMichael Tuexen 	phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
5978f8829a4aSRandall Stewart 	while (phdr) {
5979f8829a4aSRandall Stewart 		ptype = ntohs(phdr->param_type);
5980f8829a4aSRandall Stewart 		plen = ntohs(phdr->param_length);
5981f8829a4aSRandall Stewart 		/*
5982cd3fd531SMichael Tuexen 		 * SCTP_PRINTF("ptype => %0x, plen => %d\n",
5983cd3fd531SMichael Tuexen 		 * (uint32_t)ptype, (int)plen);
5984f8829a4aSRandall Stewart 		 */
5985f8829a4aSRandall Stewart 		if (offset + plen > limit) {
5986f8829a4aSRandall Stewart 			break;
5987f8829a4aSRandall Stewart 		}
59886088175aSMichael Tuexen 		if (plen < sizeof(struct sctp_paramhdr)) {
5989f8829a4aSRandall Stewart 			break;
5990f8829a4aSRandall Stewart 		}
5991e6194c2eSMichael Tuexen #ifdef INET
5992f8829a4aSRandall Stewart 		if (ptype == SCTP_IPV4_ADDRESS) {
5993a1cb341bSMichael Tuexen 			if (stcb->asoc.scope.ipv4_addr_legal) {
5994f8829a4aSRandall Stewart 				struct sctp_ipv4addr_param *p4, p4_buf;
5995f8829a4aSRandall Stewart 
5996f8829a4aSRandall Stewart 				/* ok get the v4 address and check/add */
5997f8829a4aSRandall Stewart 				phdr = sctp_get_next_param(m, offset,
5998a99b6783SRandall Stewart 				    (struct sctp_paramhdr *)&p4_buf,
5999a99b6783SRandall Stewart 				    sizeof(p4_buf));
6000f8829a4aSRandall Stewart 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
6001f8829a4aSRandall Stewart 				    phdr == NULL) {
6002f8829a4aSRandall Stewart 					return (-5);
6003f8829a4aSRandall Stewart 				}
6004f8829a4aSRandall Stewart 				p4 = (struct sctp_ipv4addr_param *)phdr;
6005f8829a4aSRandall Stewart 				sin.sin_addr.s_addr = p4->addr;
6006fc048708SMichael Tuexen 				if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
6007d06c82f1SRandall Stewart 					/* Skip multi-cast addresses */
6008d06c82f1SRandall Stewart 					goto next_param;
6009d06c82f1SRandall Stewart 				}
6010532106f7SGleb Smirnoff 				if (in_broadcast(sin.sin_addr)) {
601117205eccSRandall Stewart 					goto next_param;
601217205eccSRandall Stewart 				}
6013f8829a4aSRandall Stewart 				sa = (struct sockaddr *)&sin;
6014f8829a4aSRandall Stewart 				inp = stcb->sctp_ep;
601550cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
6016f8829a4aSRandall Stewart 				stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6017b1754ad1SMichael Tuexen 				    dst, stcb);
60183c1ba6f3SMichael Tuexen 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
6019f8829a4aSRandall Stewart 
6020f8829a4aSRandall Stewart 				if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
6021f8829a4aSRandall Stewart 				    inp == NULL) {
6022f8829a4aSRandall Stewart 					/* we must add the source address */
6023f8829a4aSRandall Stewart 					/*
6024f8829a4aSRandall Stewart 					 * no scope set since we have a tcb
6025f8829a4aSRandall Stewart 					 * already
6026f8829a4aSRandall Stewart 					 */
6027f8829a4aSRandall Stewart 
6028f8829a4aSRandall Stewart 					/*
6029f8829a4aSRandall Stewart 					 * we must validate the state again
6030f8829a4aSRandall Stewart 					 * here
6031f8829a4aSRandall Stewart 					 */
60326d9e8f2bSRandall Stewart 			add_it_now:
6033f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6034f8829a4aSRandall Stewart 						/* the assoc was freed? */
6035f8829a4aSRandall Stewart 						return (-7);
6036f8829a4aSRandall Stewart 					}
6037ec70917fSMichael Tuexen 					if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
6038f8829a4aSRandall Stewart 						return (-8);
6039f8829a4aSRandall Stewart 					}
6040f8829a4aSRandall Stewart 				} else if (stcb_tmp == stcb) {
6041f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6042f8829a4aSRandall Stewart 						/* the assoc was freed? */
6043f8829a4aSRandall Stewart 						return (-10);
6044f8829a4aSRandall Stewart 					}
6045f8829a4aSRandall Stewart 					if (net != NULL) {
6046f8829a4aSRandall Stewart 						/* clear flag */
6047f8829a4aSRandall Stewart 						net->dest_state &=
6048f8829a4aSRandall Stewart 						    ~SCTP_ADDR_NOT_IN_ASSOC;
6049f8829a4aSRandall Stewart 					}
6050f8829a4aSRandall Stewart 				} else {
6051f8829a4aSRandall Stewart 					/*
6052f8829a4aSRandall Stewart 					 * strange, address is in another
6053f8829a4aSRandall Stewart 					 * assoc? straighten out locks.
6054f8829a4aSRandall Stewart 					 */
60556d9e8f2bSRandall Stewart 					if (stcb_tmp) {
6056839d21d6SMichael Tuexen 						if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
6057267dbe63SMichael Tuexen 							struct mbuf *op_err;
6058267dbe63SMichael Tuexen 							char msg[SCTP_DIAG_INFO_LEN];
6059267dbe63SMichael Tuexen 
6060b7b84c0eSMichael Tuexen 							/*
6061b7b84c0eSMichael Tuexen 							 * in setup state we
6062b7b84c0eSMichael Tuexen 							 * abort this guy
6063b7b84c0eSMichael Tuexen 							 */
6064999f86d6SMichael Tuexen 							SCTP_SNPRINTF(msg, sizeof(msg),
6065821bae7cSMichael Tuexen 							    "%s:%d at %s", __FILE__, __LINE__, __func__);
6066267dbe63SMichael Tuexen 							op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6067267dbe63SMichael Tuexen 							    msg);
60686d9e8f2bSRandall Stewart 							sctp_abort_an_association(stcb_tmp->sctp_ep,
6069105b68b4SMichael Tuexen 							    stcb_tmp, op_err, false,
6070267dbe63SMichael Tuexen 							    SCTP_SO_NOT_LOCKED);
60716d9e8f2bSRandall Stewart 							goto add_it_now;
60726d9e8f2bSRandall Stewart 						}
6073b54d3a6cSRandall Stewart 						SCTP_TCB_UNLOCK(stcb_tmp);
60746d9e8f2bSRandall Stewart 					}
60750053ed28SMichael Tuexen 
6076f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6077f8829a4aSRandall Stewart 						/* the assoc was freed? */
6078f8829a4aSRandall Stewart 						return (-12);
6079f8829a4aSRandall Stewart 					}
6080f8829a4aSRandall Stewart 					return (-13);
6081f8829a4aSRandall Stewart 				}
6082f8829a4aSRandall Stewart 			}
6083e6194c2eSMichael Tuexen 		} else
6084e6194c2eSMichael Tuexen #endif
6085e6194c2eSMichael Tuexen #ifdef INET6
6086e6194c2eSMichael Tuexen 		if (ptype == SCTP_IPV6_ADDRESS) {
6087a1cb341bSMichael Tuexen 			if (stcb->asoc.scope.ipv6_addr_legal) {
6088f8829a4aSRandall Stewart 				/* ok get the v6 address and check/add */
6089f8829a4aSRandall Stewart 				struct sctp_ipv6addr_param *p6, p6_buf;
6090f8829a4aSRandall Stewart 
6091f8829a4aSRandall Stewart 				phdr = sctp_get_next_param(m, offset,
6092a99b6783SRandall Stewart 				    (struct sctp_paramhdr *)&p6_buf,
6093a99b6783SRandall Stewart 				    sizeof(p6_buf));
6094f8829a4aSRandall Stewart 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
6095f8829a4aSRandall Stewart 				    phdr == NULL) {
6096f8829a4aSRandall Stewart 					return (-14);
6097f8829a4aSRandall Stewart 				}
6098f8829a4aSRandall Stewart 				p6 = (struct sctp_ipv6addr_param *)phdr;
6099f8829a4aSRandall Stewart 				memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6100f8829a4aSRandall Stewart 				    sizeof(p6->addr));
6101d06c82f1SRandall Stewart 				if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
6102d06c82f1SRandall Stewart 					/* Skip multi-cast addresses */
6103d06c82f1SRandall Stewart 					goto next_param;
6104d06c82f1SRandall Stewart 				}
6105d06c82f1SRandall Stewart 				if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
6106b7b84c0eSMichael Tuexen 					/*
6107b7b84c0eSMichael Tuexen 					 * Link local make no sense without
6108b7b84c0eSMichael Tuexen 					 * scope
6109b7b84c0eSMichael Tuexen 					 */
6110d06c82f1SRandall Stewart 					goto next_param;
6111d06c82f1SRandall Stewart 				}
6112f8829a4aSRandall Stewart 				sa = (struct sockaddr *)&sin6;
6113f8829a4aSRandall Stewart 				inp = stcb->sctp_ep;
611450cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
6115f8829a4aSRandall Stewart 				stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6116b1754ad1SMichael Tuexen 				    dst, stcb);
61173c1ba6f3SMichael Tuexen 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
6118a99b6783SRandall Stewart 				if (stcb_tmp == NULL &&
6119a99b6783SRandall Stewart 				    (inp == stcb->sctp_ep || inp == NULL)) {
6120f8829a4aSRandall Stewart 					/*
6121f8829a4aSRandall Stewart 					 * we must validate the state again
6122f8829a4aSRandall Stewart 					 * here
6123f8829a4aSRandall Stewart 					 */
61246d9e8f2bSRandall Stewart 			add_it_now6:
6125f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6126f8829a4aSRandall Stewart 						/* the assoc was freed? */
6127f8829a4aSRandall Stewart 						return (-16);
6128f8829a4aSRandall Stewart 					}
6129f8829a4aSRandall Stewart 					/*
6130f8829a4aSRandall Stewart 					 * we must add the address, no scope
6131f8829a4aSRandall Stewart 					 * set
6132f8829a4aSRandall Stewart 					 */
6133ec70917fSMichael Tuexen 					if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
6134f8829a4aSRandall Stewart 						return (-17);
6135f8829a4aSRandall Stewart 					}
6136f8829a4aSRandall Stewart 				} else if (stcb_tmp == stcb) {
6137f8829a4aSRandall Stewart 					/*
6138f8829a4aSRandall Stewart 					 * we must validate the state again
6139f8829a4aSRandall Stewart 					 * here
6140f8829a4aSRandall Stewart 					 */
6141f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6142f8829a4aSRandall Stewart 						/* the assoc was freed? */
6143f8829a4aSRandall Stewart 						return (-19);
6144f8829a4aSRandall Stewart 					}
6145f8829a4aSRandall Stewart 					if (net != NULL) {
6146f8829a4aSRandall Stewart 						/* clear flag */
6147f8829a4aSRandall Stewart 						net->dest_state &=
6148f8829a4aSRandall Stewart 						    ~SCTP_ADDR_NOT_IN_ASSOC;
6149f8829a4aSRandall Stewart 					}
6150f8829a4aSRandall Stewart 				} else {
6151f8829a4aSRandall Stewart 					/*
6152f8829a4aSRandall Stewart 					 * strange, address is in another
6153f8829a4aSRandall Stewart 					 * assoc? straighten out locks.
6154f8829a4aSRandall Stewart 					 */
6155267dbe63SMichael Tuexen 					if (stcb_tmp) {
6156839d21d6SMichael Tuexen 						if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
6157267dbe63SMichael Tuexen 							struct mbuf *op_err;
6158267dbe63SMichael Tuexen 							char msg[SCTP_DIAG_INFO_LEN];
6159267dbe63SMichael Tuexen 
6160b7b84c0eSMichael Tuexen 							/*
6161b7b84c0eSMichael Tuexen 							 * in setup state we
6162b7b84c0eSMichael Tuexen 							 * abort this guy
6163b7b84c0eSMichael Tuexen 							 */
6164999f86d6SMichael Tuexen 							SCTP_SNPRINTF(msg, sizeof(msg),
6165821bae7cSMichael Tuexen 							    "%s:%d at %s", __FILE__, __LINE__, __func__);
6166267dbe63SMichael Tuexen 							op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6167267dbe63SMichael Tuexen 							    msg);
61686d9e8f2bSRandall Stewart 							sctp_abort_an_association(stcb_tmp->sctp_ep,
6169105b68b4SMichael Tuexen 							    stcb_tmp, op_err, false,
6170267dbe63SMichael Tuexen 							    SCTP_SO_NOT_LOCKED);
61716d9e8f2bSRandall Stewart 							goto add_it_now6;
61726d9e8f2bSRandall Stewart 						}
6173b54d3a6cSRandall Stewart 						SCTP_TCB_UNLOCK(stcb_tmp);
6174267dbe63SMichael Tuexen 					}
6175f8829a4aSRandall Stewart 					if (stcb->asoc.state == 0) {
6176f8829a4aSRandall Stewart 						/* the assoc was freed? */
6177f8829a4aSRandall Stewart 						return (-21);
6178f8829a4aSRandall Stewart 					}
6179f8829a4aSRandall Stewart 					return (-22);
6180f8829a4aSRandall Stewart 				}
6181f8829a4aSRandall Stewart 			}
6182e6194c2eSMichael Tuexen 		} else
6183e6194c2eSMichael Tuexen #endif
6184e6194c2eSMichael Tuexen 		if (ptype == SCTP_ECN_CAPABLE) {
6185c79bec9cSMichael Tuexen 			peer_supports_ecn = 1;
6186f8829a4aSRandall Stewart 		} else if (ptype == SCTP_ULP_ADAPTATION) {
6187f8829a4aSRandall Stewart 			if (stcb->asoc.state != SCTP_STATE_OPEN) {
6188f8829a4aSRandall Stewart 				struct sctp_adaptation_layer_indication ai,
6189f8829a4aSRandall Stewart 				                                *aip;
6190f8829a4aSRandall Stewart 
6191f8829a4aSRandall Stewart 				phdr = sctp_get_next_param(m, offset,
6192f8829a4aSRandall Stewart 				    (struct sctp_paramhdr *)&ai, sizeof(ai));
6193f8829a4aSRandall Stewart 				aip = (struct sctp_adaptation_layer_indication *)phdr;
61946e55db54SRandall Stewart 				if (aip) {
61952afb3e84SRandall Stewart 					stcb->asoc.peers_adaptation = ntohl(aip->indication);
61962afb3e84SRandall Stewart 					stcb->asoc.adaptation_needed = 1;
6197f8829a4aSRandall Stewart 				}
61986e55db54SRandall Stewart 			}
6199f8829a4aSRandall Stewart 		} else if (ptype == SCTP_SET_PRIM_ADDR) {
6200f8829a4aSRandall Stewart 			struct sctp_asconf_addr_param lstore, *fee;
6201f8829a4aSRandall Stewart 			int lptype;
6202f8829a4aSRandall Stewart 			struct sockaddr *lsa = NULL;
6203e6194c2eSMichael Tuexen #ifdef INET
6204e6194c2eSMichael Tuexen 			struct sctp_asconf_addrv4_param *fii;
6205e6194c2eSMichael Tuexen #endif
6206e6194c2eSMichael Tuexen 
6207c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
6208c79bec9cSMichael Tuexen 				return (-100);
6209c79bec9cSMichael Tuexen 			}
6210f8829a4aSRandall Stewart 			if (plen > sizeof(lstore)) {
6211f8829a4aSRandall Stewart 				return (-23);
6212f8829a4aSRandall Stewart 			}
62136088175aSMichael Tuexen 			if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
62146088175aSMichael Tuexen 				return (-101);
62156088175aSMichael Tuexen 			}
6216f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
6217a99b6783SRandall Stewart 			    (struct sctp_paramhdr *)&lstore,
6218f4358911SMichael Tuexen 			    plen);
6219f8829a4aSRandall Stewart 			if (phdr == NULL) {
6220f8829a4aSRandall Stewart 				return (-24);
6221f8829a4aSRandall Stewart 			}
6222f8829a4aSRandall Stewart 			fee = (struct sctp_asconf_addr_param *)phdr;
6223f8829a4aSRandall Stewart 			lptype = ntohs(fee->addrp.ph.param_type);
6224e6194c2eSMichael Tuexen 			switch (lptype) {
6225e6194c2eSMichael Tuexen #ifdef INET
6226e6194c2eSMichael Tuexen 			case SCTP_IPV4_ADDRESS:
6227f8829a4aSRandall Stewart 				if (plen !=
6228f8829a4aSRandall Stewart 				    sizeof(struct sctp_asconf_addrv4_param)) {
6229ad81507eSRandall Stewart 					SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
6230f8829a4aSRandall Stewart 					    (int)sizeof(struct sctp_asconf_addrv4_param),
6231f8829a4aSRandall Stewart 					    plen);
6232f8829a4aSRandall Stewart 				} else {
6233f8829a4aSRandall Stewart 					fii = (struct sctp_asconf_addrv4_param *)fee;
6234f8829a4aSRandall Stewart 					sin.sin_addr.s_addr = fii->addrp.addr;
6235f8829a4aSRandall Stewart 					lsa = (struct sockaddr *)&sin;
6236f8829a4aSRandall Stewart 				}
6237e6194c2eSMichael Tuexen 				break;
6238e6194c2eSMichael Tuexen #endif
6239e6194c2eSMichael Tuexen #ifdef INET6
6240e6194c2eSMichael Tuexen 			case SCTP_IPV6_ADDRESS:
6241f8829a4aSRandall Stewart 				if (plen !=
6242f8829a4aSRandall Stewart 				    sizeof(struct sctp_asconf_addr_param)) {
6243ad81507eSRandall Stewart 					SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
6244f8829a4aSRandall Stewart 					    (int)sizeof(struct sctp_asconf_addr_param),
6245f8829a4aSRandall Stewart 					    plen);
6246f8829a4aSRandall Stewart 				} else {
6247f8829a4aSRandall Stewart 					memcpy(sin6.sin6_addr.s6_addr,
6248f8829a4aSRandall Stewart 					    fee->addrp.addr,
6249f8829a4aSRandall Stewart 					    sizeof(fee->addrp.addr));
6250f8829a4aSRandall Stewart 					lsa = (struct sockaddr *)&sin6;
6251f8829a4aSRandall Stewart 				}
6252e6194c2eSMichael Tuexen 				break;
6253e6194c2eSMichael Tuexen #endif
6254e6194c2eSMichael Tuexen 			default:
6255e6194c2eSMichael Tuexen 				break;
6256f8829a4aSRandall Stewart 			}
6257f8829a4aSRandall Stewart 			if (lsa) {
6258ad81507eSRandall Stewart 				(void)sctp_set_primary_addr(stcb, sa, NULL);
6259f8829a4aSRandall Stewart 			}
6260830d754dSRandall Stewart 		} else if (ptype == SCTP_HAS_NAT_SUPPORT) {
6261830d754dSRandall Stewart 			stcb->asoc.peer_supports_nat = 1;
6262f8829a4aSRandall Stewart 		} else if (ptype == SCTP_PRSCTP_SUPPORTED) {
6263f8829a4aSRandall Stewart 			/* Peer supports pr-sctp */
6264c79bec9cSMichael Tuexen 			peer_supports_prsctp = 1;
62654a2b92d9SMichael Tuexen 		} else if (ptype == SCTP_ZERO_CHECKSUM_ACCEPTABLE) {
626652640d61SMichael Tuexen 			struct sctp_zero_checksum_acceptable zero_chksum,
626752640d61SMichael Tuexen 			                             *zero_chksum_p;
626852640d61SMichael Tuexen 
626952640d61SMichael Tuexen 			phdr = sctp_get_next_param(m, offset,
627052640d61SMichael Tuexen 			    (struct sctp_paramhdr *)&zero_chksum,
627152640d61SMichael Tuexen 			    sizeof(struct sctp_zero_checksum_acceptable));
627252640d61SMichael Tuexen 			if (phdr != NULL) {
62734a2b92d9SMichael Tuexen 				/*
627452640d61SMichael Tuexen 				 * Only send zero checksums if the upper
627552640d61SMichael Tuexen 				 * layer has enabled the support for the
627652640d61SMichael Tuexen 				 * same method as allowed by the peer.
62774a2b92d9SMichael Tuexen 				 */
627852640d61SMichael Tuexen 				zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr;
627952640d61SMichael Tuexen 				if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) &&
628052640d61SMichael Tuexen 				    (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) {
628152640d61SMichael Tuexen 					stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid;
628252640d61SMichael Tuexen 				}
62834a2b92d9SMichael Tuexen 			}
6284f8829a4aSRandall Stewart 		} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6285f8829a4aSRandall Stewart 			/* A supported extension chunk */
6286f8829a4aSRandall Stewart 			struct sctp_supported_chunk_types_param *pr_supported;
6287f42a358aSRandall Stewart 			uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6288f8829a4aSRandall Stewart 			int num_ent, i;
6289f8829a4aSRandall Stewart 
6290f4358911SMichael Tuexen 			if (plen > sizeof(local_store)) {
6291f4358911SMichael Tuexen 				return (-35);
6292f4358911SMichael Tuexen 			}
6293f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
6294f4358911SMichael Tuexen 			    (struct sctp_paramhdr *)&local_store, plen);
6295f8829a4aSRandall Stewart 			if (phdr == NULL) {
6296f8829a4aSRandall Stewart 				return (-25);
6297f8829a4aSRandall Stewart 			}
6298f8829a4aSRandall Stewart 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6299f8829a4aSRandall Stewart 			num_ent = plen - sizeof(struct sctp_paramhdr);
6300f8829a4aSRandall Stewart 			for (i = 0; i < num_ent; i++) {
6301f8829a4aSRandall Stewart 				switch (pr_supported->chunk_types[i]) {
6302f8829a4aSRandall Stewart 				case SCTP_ASCONF:
6303c79bec9cSMichael Tuexen 					peer_supports_asconf = 1;
6304142a4d9eSMichael Tuexen 					break;
6305f8829a4aSRandall Stewart 				case SCTP_ASCONF_ACK:
6306c79bec9cSMichael Tuexen 					peer_supports_asconf_ack = 1;
6307f8829a4aSRandall Stewart 					break;
6308f8829a4aSRandall Stewart 				case SCTP_FORWARD_CUM_TSN:
6309c79bec9cSMichael Tuexen 					peer_supports_prsctp = 1;
6310f8829a4aSRandall Stewart 					break;
6311f8829a4aSRandall Stewart 				case SCTP_PACKET_DROPPED:
6312c79bec9cSMichael Tuexen 					peer_supports_pktdrop = 1;
6313f8829a4aSRandall Stewart 					break;
63148933fa13SRandall Stewart 				case SCTP_NR_SELECTIVE_ACK:
6315c79bec9cSMichael Tuexen 					peer_supports_nrsack = 1;
63168933fa13SRandall Stewart 					break;
6317f8829a4aSRandall Stewart 				case SCTP_STREAM_RESET:
6318c79bec9cSMichael Tuexen 					peer_supports_reconfig = 1;
6319f8829a4aSRandall Stewart 					break;
6320f8829a4aSRandall Stewart 				case SCTP_AUTHENTICATION:
6321c79bec9cSMichael Tuexen 					peer_supports_auth = 1;
6322f8829a4aSRandall Stewart 					break;
632344249214SRandall Stewart 				case SCTP_IDATA:
632444249214SRandall Stewart 					peer_supports_idata = 1;
632544249214SRandall Stewart 					break;
6326f8829a4aSRandall Stewart 				default:
6327f8829a4aSRandall Stewart 					/* one I have not learned yet */
6328f8829a4aSRandall Stewart 					break;
6329f8829a4aSRandall Stewart 				}
6330f8829a4aSRandall Stewart 			}
6331f8829a4aSRandall Stewart 		} else if (ptype == SCTP_RANDOM) {
6332f42a358aSRandall Stewart 			if (plen > sizeof(random_store))
6333f8829a4aSRandall Stewart 				break;
6334f8829a4aSRandall Stewart 			if (got_random) {
6335f8829a4aSRandall Stewart 				/* already processed a RANDOM */
6336f8829a4aSRandall Stewart 				goto next_param;
6337f8829a4aSRandall Stewart 			}
6338f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
6339f42a358aSRandall Stewart 			    (struct sctp_paramhdr *)random_store,
6340f4358911SMichael Tuexen 			    plen);
6341f8829a4aSRandall Stewart 			if (phdr == NULL)
6342f8829a4aSRandall Stewart 				return (-26);
63435e54f665SRandall Stewart 			p_random = (struct sctp_auth_random *)phdr;
63445e54f665SRandall Stewart 			random_len = plen - sizeof(*p_random);
6345f8829a4aSRandall Stewart 			/* enforce the random length */
6346f8829a4aSRandall Stewart 			if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6347ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6348f8829a4aSRandall Stewart 				return (-27);
6349f8829a4aSRandall Stewart 			}
6350f8829a4aSRandall Stewart 			got_random = 1;
6351f8829a4aSRandall Stewart 		} else if (ptype == SCTP_HMAC_LIST) {
635247b80412SMichael Tuexen 			uint16_t num_hmacs;
635347b80412SMichael Tuexen 			uint16_t i;
6354f8829a4aSRandall Stewart 
6355f42a358aSRandall Stewart 			if (plen > sizeof(hmacs_store))
6356f8829a4aSRandall Stewart 				break;
6357f8829a4aSRandall Stewart 			if (got_hmacs) {
6358f8829a4aSRandall Stewart 				/* already processed a HMAC list */
6359f8829a4aSRandall Stewart 				goto next_param;
6360f8829a4aSRandall Stewart 			}
6361f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
6362f42a358aSRandall Stewart 			    (struct sctp_paramhdr *)hmacs_store,
6363f4358911SMichael Tuexen 			    plen);
6364f8829a4aSRandall Stewart 			if (phdr == NULL)
6365f8829a4aSRandall Stewart 				return (-28);
6366f8829a4aSRandall Stewart 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
6367f8829a4aSRandall Stewart 			hmacs_len = plen - sizeof(*hmacs);
6368f8829a4aSRandall Stewart 			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6369f8829a4aSRandall Stewart 			/* validate the hmac list */
6370f8829a4aSRandall Stewart 			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6371f8829a4aSRandall Stewart 				return (-29);
6372f8829a4aSRandall Stewart 			}
6373f8829a4aSRandall Stewart 			if (stcb->asoc.peer_hmacs != NULL)
6374f8829a4aSRandall Stewart 				sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6375f8829a4aSRandall Stewart 			stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6376f8829a4aSRandall Stewart 			if (stcb->asoc.peer_hmacs != NULL) {
6377f8829a4aSRandall Stewart 				for (i = 0; i < num_hmacs; i++) {
63786e55db54SRandall Stewart 					(void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6379f8829a4aSRandall Stewart 					    ntohs(hmacs->hmac_ids[i]));
6380f8829a4aSRandall Stewart 				}
6381f8829a4aSRandall Stewart 			}
6382f8829a4aSRandall Stewart 			got_hmacs = 1;
6383f8829a4aSRandall Stewart 		} else if (ptype == SCTP_CHUNK_LIST) {
6384f8829a4aSRandall Stewart 			int i;
6385f8829a4aSRandall Stewart 
6386f42a358aSRandall Stewart 			if (plen > sizeof(chunks_store))
6387f8829a4aSRandall Stewart 				break;
6388f8829a4aSRandall Stewart 			if (got_chklist) {
6389f8829a4aSRandall Stewart 				/* already processed a Chunks list */
6390f8829a4aSRandall Stewart 				goto next_param;
6391f8829a4aSRandall Stewart 			}
6392f8829a4aSRandall Stewart 			phdr = sctp_get_next_param(m, offset,
6393f42a358aSRandall Stewart 			    (struct sctp_paramhdr *)chunks_store,
6394f4358911SMichael Tuexen 			    plen);
6395f8829a4aSRandall Stewart 			if (phdr == NULL)
6396f8829a4aSRandall Stewart 				return (-30);
6397f8829a4aSRandall Stewart 			chunks = (struct sctp_auth_chunk_list *)phdr;
6398f8829a4aSRandall Stewart 			num_chunks = plen - sizeof(*chunks);
6399f8829a4aSRandall Stewart 			if (stcb->asoc.peer_auth_chunks != NULL)
6400f8829a4aSRandall Stewart 				sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6401f8829a4aSRandall Stewart 			else
6402f8829a4aSRandall Stewart 				stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6403f8829a4aSRandall Stewart 			for (i = 0; i < num_chunks; i++) {
64046e55db54SRandall Stewart 				(void)sctp_auth_add_chunk(chunks->chunk_types[i],
6405f8829a4aSRandall Stewart 				    stcb->asoc.peer_auth_chunks);
64062afb3e84SRandall Stewart 				/* record asconf/asconf-ack if listed */
64072afb3e84SRandall Stewart 				if (chunks->chunk_types[i] == SCTP_ASCONF)
64082afb3e84SRandall Stewart 					saw_asconf = 1;
64092afb3e84SRandall Stewart 				if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
64102afb3e84SRandall Stewart 					saw_asconf_ack = 1;
6411f8829a4aSRandall Stewart 			}
6412f8829a4aSRandall Stewart 			got_chklist = 1;
6413f8829a4aSRandall Stewart 		} else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6414f8829a4aSRandall Stewart 			    (ptype == SCTP_STATE_COOKIE) ||
6415f8829a4aSRandall Stewart 			    (ptype == SCTP_UNRECOG_PARAM) ||
6416f8829a4aSRandall Stewart 			    (ptype == SCTP_COOKIE_PRESERVE) ||
6417f8829a4aSRandall Stewart 			    (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6418f8829a4aSRandall Stewart 			    (ptype == SCTP_ADD_IP_ADDRESS) ||
6419f8829a4aSRandall Stewart 			    (ptype == SCTP_DEL_IP_ADDRESS) ||
6420f8829a4aSRandall Stewart 			    (ptype == SCTP_ERROR_CAUSE_IND) ||
6421f8829a4aSRandall Stewart 		    (ptype == SCTP_SUCCESS_REPORT)) {
6422fbc6840bSMichael Tuexen 			/* don't care */
6423f8829a4aSRandall Stewart 		} else {
6424f8829a4aSRandall Stewart 			if ((ptype & 0x8000) == 0x0000) {
6425f8829a4aSRandall Stewart 				/*
6426f8829a4aSRandall Stewart 				 * must stop processing the rest of the
6427f8829a4aSRandall Stewart 				 * param's. Any report bits were handled
6428f8829a4aSRandall Stewart 				 * with the call to
6429f8829a4aSRandall Stewart 				 * sctp_arethere_unrecognized_parameters()
6430f8829a4aSRandall Stewart 				 * when the INIT or INIT-ACK was first seen.
6431f8829a4aSRandall Stewart 				 */
6432f8829a4aSRandall Stewart 				break;
6433f8829a4aSRandall Stewart 			}
6434f8829a4aSRandall Stewart 		}
6435a99b6783SRandall Stewart 
6436f8829a4aSRandall Stewart next_param:
6437f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(plen);
6438f8829a4aSRandall Stewart 		if (offset >= limit) {
6439f8829a4aSRandall Stewart 			break;
6440f8829a4aSRandall Stewart 		}
6441f4358911SMichael Tuexen 		phdr = sctp_get_next_param(m, offset, &param_buf,
6442f4358911SMichael Tuexen 		    sizeof(param_buf));
6443f8829a4aSRandall Stewart 	}
6444f8829a4aSRandall Stewart 	/* Now check to see if we need to purge any addresses */
64454a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6446f8829a4aSRandall Stewart 		if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6447f8829a4aSRandall Stewart 		    SCTP_ADDR_NOT_IN_ASSOC) {
6448f8829a4aSRandall Stewart 			/* This address has been removed from the asoc */
6449f8829a4aSRandall Stewart 			/* remove and free it */
6450f8829a4aSRandall Stewart 			stcb->asoc.numnets--;
6451f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
64528b3d0f64SMichael Tuexen 			if (net == stcb->asoc.alternate) {
64538b3d0f64SMichael Tuexen 				sctp_free_remote_addr(stcb->asoc.alternate);
64548b3d0f64SMichael Tuexen 				stcb->asoc.alternate = NULL;
64558b3d0f64SMichael Tuexen 			}
6456f8829a4aSRandall Stewart 			if (net == stcb->asoc.primary_destination) {
6457f8829a4aSRandall Stewart 				stcb->asoc.primary_destination = NULL;
6458f8829a4aSRandall Stewart 				sctp_select_primary_destination(stcb);
6459f8829a4aSRandall Stewart 			}
64608b3d0f64SMichael Tuexen 			sctp_free_remote_addr(net);
6461f8829a4aSRandall Stewart 		}
6462f8829a4aSRandall Stewart 	}
6463c79bec9cSMichael Tuexen 	if ((stcb->asoc.ecn_supported == 1) &&
6464c79bec9cSMichael Tuexen 	    (peer_supports_ecn == 0)) {
6465c79bec9cSMichael Tuexen 		stcb->asoc.ecn_supported = 0;
6466f8829a4aSRandall Stewart 	}
6467c79bec9cSMichael Tuexen 	if ((stcb->asoc.prsctp_supported == 1) &&
6468c79bec9cSMichael Tuexen 	    (peer_supports_prsctp == 0)) {
6469c79bec9cSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
6470c79bec9cSMichael Tuexen 	}
6471c79bec9cSMichael Tuexen 	if ((stcb->asoc.auth_supported == 1) &&
6472c79bec9cSMichael Tuexen 	    ((peer_supports_auth == 0) ||
6473c79bec9cSMichael Tuexen 	    (got_random == 0) || (got_hmacs == 0))) {
6474c79bec9cSMichael Tuexen 		stcb->asoc.auth_supported = 0;
6475c79bec9cSMichael Tuexen 	}
6476c79bec9cSMichael Tuexen 	if ((stcb->asoc.asconf_supported == 1) &&
6477c79bec9cSMichael Tuexen 	    ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
6478c79bec9cSMichael Tuexen 	    (stcb->asoc.auth_supported == 0) ||
6479c79bec9cSMichael Tuexen 	    (saw_asconf == 0) || (saw_asconf_ack == 0))) {
6480c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
6481c79bec9cSMichael Tuexen 	}
6482c79bec9cSMichael Tuexen 	if ((stcb->asoc.reconfig_supported == 1) &&
6483c79bec9cSMichael Tuexen 	    (peer_supports_reconfig == 0)) {
6484c79bec9cSMichael Tuexen 		stcb->asoc.reconfig_supported = 0;
6485c79bec9cSMichael Tuexen 	}
648644249214SRandall Stewart 	if ((stcb->asoc.idata_supported == 1) &&
648744249214SRandall Stewart 	    (peer_supports_idata == 0)) {
648844249214SRandall Stewart 		stcb->asoc.idata_supported = 0;
648944249214SRandall Stewart 	}
6490c79bec9cSMichael Tuexen 	if ((stcb->asoc.nrsack_supported == 1) &&
6491c79bec9cSMichael Tuexen 	    (peer_supports_nrsack == 0)) {
6492c79bec9cSMichael Tuexen 		stcb->asoc.nrsack_supported = 0;
6493c79bec9cSMichael Tuexen 	}
6494c79bec9cSMichael Tuexen 	if ((stcb->asoc.pktdrop_supported == 1) &&
6495c79bec9cSMichael Tuexen 	    (peer_supports_pktdrop == 0)) {
6496c79bec9cSMichael Tuexen 		stcb->asoc.pktdrop_supported = 0;
6497c79bec9cSMichael Tuexen 	}
6498c79bec9cSMichael Tuexen 	/* validate authentication required parameters */
6499c79bec9cSMichael Tuexen 	if ((peer_supports_auth == 0) && (got_chklist == 1)) {
65005e54f665SRandall Stewart 		/* peer does not support auth but sent a chunks list? */
65015e54f665SRandall Stewart 		return (-31);
65025e54f665SRandall Stewart 	}
6503c79bec9cSMichael Tuexen 	if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
65045e54f665SRandall Stewart 		/* peer supports asconf but not auth? */
65055e54f665SRandall Stewart 		return (-32);
6506c79bec9cSMichael Tuexen 	} else if ((peer_supports_asconf == 1) &&
6507c79bec9cSMichael Tuexen 		    (peer_supports_auth == 1) &&
65082afb3e84SRandall Stewart 	    ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
65092afb3e84SRandall Stewart 		return (-33);
6510f8829a4aSRandall Stewart 	}
6511f8829a4aSRandall Stewart 	/* concatenate the full random key */
6512a99b6783SRandall Stewart 	keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6513a99b6783SRandall Stewart 	if (chunks != NULL) {
6514a99b6783SRandall Stewart 		keylen += sizeof(*chunks) + num_chunks;
6515a99b6783SRandall Stewart 	}
6516f42a358aSRandall Stewart 	new_key = sctp_alloc_key(keylen);
6517f42a358aSRandall Stewart 	if (new_key != NULL) {
6518f42a358aSRandall Stewart 		/* copy in the RANDOM */
65195e54f665SRandall Stewart 		if (p_random != NULL) {
65205e54f665SRandall Stewart 			keylen = sizeof(*p_random) + random_len;
65215ba7f91fSMichael Tuexen 			memcpy(new_key->key, p_random, keylen);
652213500cbbSMichael Tuexen 		} else {
652313500cbbSMichael Tuexen 			keylen = 0;
6524f42a358aSRandall Stewart 		}
6525f8829a4aSRandall Stewart 		/* append in the AUTH chunks */
6526f42a358aSRandall Stewart 		if (chunks != NULL) {
65275ba7f91fSMichael Tuexen 			memcpy(new_key->key + keylen, chunks,
6528f42a358aSRandall Stewart 			    sizeof(*chunks) + num_chunks);
6529f42a358aSRandall Stewart 			keylen += sizeof(*chunks) + num_chunks;
6530f42a358aSRandall Stewart 		}
6531f8829a4aSRandall Stewart 		/* append in the HMACs */
6532f42a358aSRandall Stewart 		if (hmacs != NULL) {
65335ba7f91fSMichael Tuexen 			memcpy(new_key->key + keylen, hmacs,
6534f42a358aSRandall Stewart 			    sizeof(*hmacs) + hmacs_len);
6535f42a358aSRandall Stewart 		}
65365661a9edSMichael Tuexen 	} else {
65375e54f665SRandall Stewart 		/* failed to get memory for the key */
65382afb3e84SRandall Stewart 		return (-34);
6539f8829a4aSRandall Stewart 	}
6540f8829a4aSRandall Stewart 	if (stcb->asoc.authinfo.peer_random != NULL)
6541f8829a4aSRandall Stewart 		sctp_free_key(stcb->asoc.authinfo.peer_random);
6542f8829a4aSRandall Stewart 	stcb->asoc.authinfo.peer_random = new_key;
6543f8829a4aSRandall Stewart 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6544f8829a4aSRandall Stewart 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6545f8829a4aSRandall Stewart 
6546f8829a4aSRandall Stewart 	return (0);
6547f8829a4aSRandall Stewart }
6548f8829a4aSRandall Stewart 
6549f8829a4aSRandall Stewart int
sctp_set_primary_addr(struct sctp_tcb * stcb,struct sockaddr * sa,struct sctp_nets * net)6550f8829a4aSRandall Stewart sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6551f8829a4aSRandall Stewart     struct sctp_nets *net)
6552f8829a4aSRandall Stewart {
6553f8829a4aSRandall Stewart 	/* make sure the requested primary address exists in the assoc */
6554f8829a4aSRandall Stewart 	if (net == NULL && sa)
6555f8829a4aSRandall Stewart 		net = sctp_findnet(stcb, sa);
6556f8829a4aSRandall Stewart 
6557f8829a4aSRandall Stewart 	if (net == NULL) {
6558f8829a4aSRandall Stewart 		/* didn't find the requested primary address! */
6559f8829a4aSRandall Stewart 		return (-1);
6560f8829a4aSRandall Stewart 	} else {
6561f8829a4aSRandall Stewart 		/* set the primary address */
6562f8829a4aSRandall Stewart 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
656342551e99SRandall Stewart 			/* Must be confirmed, so queue to set */
656442551e99SRandall Stewart 			net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
656542551e99SRandall Stewart 			return (0);
6566f8829a4aSRandall Stewart 		}
6567f8829a4aSRandall Stewart 		stcb->asoc.primary_destination = net;
65689b2a35b3SMichael Tuexen 		if (((net->dest_state & SCTP_ADDR_PF) == 0) &&
65699b2a35b3SMichael Tuexen 		    (stcb->asoc.alternate != NULL)) {
6570ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
6571ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
6572ca85e948SMichael Tuexen 		}
6573f8829a4aSRandall Stewart 		net = TAILQ_FIRST(&stcb->asoc.nets);
6574f8829a4aSRandall Stewart 		if (net != stcb->asoc.primary_destination) {
6575f8829a4aSRandall Stewart 			/*
6576f8829a4aSRandall Stewart 			 * first one on the list is NOT the primary
6577b1ce21c6SRebecca Cran 			 * sctp_cmpaddr() is much more efficient if the
6578f8829a4aSRandall Stewart 			 * primary is the first on the list, make it so.
6579f8829a4aSRandall Stewart 			 */
6580f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6581f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6582f8829a4aSRandall Stewart 		}
6583f8829a4aSRandall Stewart 		return (0);
6584f8829a4aSRandall Stewart 	}
6585f8829a4aSRandall Stewart }
6586f8829a4aSRandall Stewart 
658758a7bf12SMichael Tuexen bool
sctp_is_vtag_good(uint32_t tag,uint16_t lport,uint16_t rport,struct timeval * now)65887215cc1bSMichael Tuexen sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
6589f8829a4aSRandall Stewart {
6590830d754dSRandall Stewart 	struct sctpasochead *head;
6591f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
6592f8829a4aSRandall Stewart 
65932d5c48ecSMark Johnston 	SCTP_INP_INFO_LOCK_ASSERT();
65942d5c48ecSMark Johnston 
659558a7bf12SMichael Tuexen 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
6596f8829a4aSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
6597f8829a4aSRandall Stewart 		/*
6598830d754dSRandall Stewart 		 * We choose not to lock anything here. TCB's can't be
6599830d754dSRandall Stewart 		 * removed since we have the read lock, so they can't be
6600830d754dSRandall Stewart 		 * freed on us, same thing for the INP. I may be wrong with
6601830d754dSRandall Stewart 		 * this assumption, but we will go with it for now :-)
6602f8829a4aSRandall Stewart 		 */
6603830d754dSRandall Stewart 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6604830d754dSRandall Stewart 			continue;
6605f8829a4aSRandall Stewart 		}
6606830d754dSRandall Stewart 		if (stcb->asoc.my_vtag == tag) {
6607f8829a4aSRandall Stewart 			/* candidate */
6608830d754dSRandall Stewart 			if (stcb->rport != rport) {
6609830d754dSRandall Stewart 				continue;
6610830d754dSRandall Stewart 			}
6611830d754dSRandall Stewart 			if (stcb->sctp_ep->sctp_lport != lport) {
6612830d754dSRandall Stewart 				continue;
6613830d754dSRandall Stewart 			}
661458a7bf12SMichael Tuexen 			/* The tag is currently used, so don't use it. */
66154181fa2aSMichael Tuexen 			return (false);
661658a7bf12SMichael Tuexen 		}
661758a7bf12SMichael Tuexen 	}
661842aeb8d4SMichael Tuexen 	return (!sctp_is_in_timewait(tag, lport, rport, now->tv_sec));
6619f8829a4aSRandall Stewart }
6620f8829a4aSRandall Stewart 
6621f8829a4aSRandall Stewart static void
sctp_drain_mbufs(struct sctp_tcb * stcb)66227215cc1bSMichael Tuexen sctp_drain_mbufs(struct sctp_tcb *stcb)
6623f8829a4aSRandall Stewart {
6624f8829a4aSRandall Stewart 	/*
6625f8829a4aSRandall Stewart 	 * We must hunt this association for MBUF's past the cumack (i.e.
6626f8829a4aSRandall Stewart 	 * out of order data that we can renege on).
6627f8829a4aSRandall Stewart 	 */
6628f8829a4aSRandall Stewart 	struct sctp_association *asoc;
6629f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *nchk;
6630b5c16493SMichael Tuexen 	uint32_t cumulative_tsn_p1;
663128cd0699SMichael Tuexen 	struct sctp_queued_to_read *control, *ncontrol;
6632b5c16493SMichael Tuexen 	int cnt, strmat;
6633b5c16493SMichael Tuexen 	uint32_t gap, i;
6634b5c16493SMichael Tuexen 	int fnd = 0;
6635f8829a4aSRandall Stewart 
6636f8829a4aSRandall Stewart 	/* We look for anything larger than the cum-ack + 1 */
6637f8829a4aSRandall Stewart 
6638f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
6639f8829a4aSRandall Stewart 	if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6640f8829a4aSRandall Stewart 		/* none we can reneg on. */
6641f8829a4aSRandall Stewart 		return;
6642f8829a4aSRandall Stewart 	}
6643a5d547adSRandall Stewart 	SCTP_STAT_INCR(sctps_protocol_drains_done);
6644f8829a4aSRandall Stewart 	cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6645f8829a4aSRandall Stewart 	cnt = 0;
6646f8829a4aSRandall Stewart 	/* Ok that was fun, now we will drain all the inbound streams? */
6647f8829a4aSRandall Stewart 	for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
664828cd0699SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
6649d1ea5fa9SMichael Tuexen #ifdef INVARIANTS
665028cd0699SMichael Tuexen 			if (control->on_strm_q != SCTP_ON_ORDERED) {
6651d1ea5fa9SMichael Tuexen 				panic("Huh control: %p on_q: %d -- not ordered?",
665228cd0699SMichael Tuexen 				    control, control->on_strm_q);
6653d1ea5fa9SMichael Tuexen 			}
6654d1ea5fa9SMichael Tuexen #endif
665528cd0699SMichael Tuexen 			if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
6656f8829a4aSRandall Stewart 				/* Yep it is above cum-ack */
6657f8829a4aSRandall Stewart 				cnt++;
665828cd0699SMichael Tuexen 				SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
665928cd0699SMichael Tuexen 				KASSERT(control->length > 0, ("control has zero length"));
666028cd0699SMichael Tuexen 				if (asoc->size_on_all_streams >= control->length) {
666128cd0699SMichael Tuexen 					asoc->size_on_all_streams -= control->length;
666228cd0699SMichael Tuexen 				} else {
6663d1ea5fa9SMichael Tuexen #ifdef INVARIANTS
666428cd0699SMichael Tuexen 					panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
666528cd0699SMichael Tuexen #else
666628cd0699SMichael Tuexen 					asoc->size_on_all_streams = 0;
6667d1ea5fa9SMichael Tuexen #endif
666828cd0699SMichael Tuexen 				}
666944249214SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
667044249214SRandall Stewart 				SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
667128cd0699SMichael Tuexen 				if (control->on_read_q) {
667228cd0699SMichael Tuexen 					TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
667328cd0699SMichael Tuexen 					control->on_read_q = 0;
6674d1ea5fa9SMichael Tuexen 				}
667528cd0699SMichael Tuexen 				TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
667628cd0699SMichael Tuexen 				control->on_strm_q = 0;
667728cd0699SMichael Tuexen 				if (control->data) {
667828cd0699SMichael Tuexen 					sctp_m_freem(control->data);
667928cd0699SMichael Tuexen 					control->data = NULL;
668044249214SRandall Stewart 				}
668128cd0699SMichael Tuexen 				sctp_free_remote_addr(control->whoFrom);
668244249214SRandall Stewart 				/* Now its reasm? */
668328cd0699SMichael Tuexen 				TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
668444249214SRandall Stewart 					cnt++;
668549656eefSMichael Tuexen 					SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
668628cd0699SMichael Tuexen 					KASSERT(chk->send_size > 0, ("chunk has zero length"));
668728cd0699SMichael Tuexen 					if (asoc->size_on_reasm_queue >= chk->send_size) {
668828cd0699SMichael Tuexen 						asoc->size_on_reasm_queue -= chk->send_size;
668928cd0699SMichael Tuexen 					} else {
669028cd0699SMichael Tuexen #ifdef INVARIANTS
669128cd0699SMichael Tuexen 						panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
669228cd0699SMichael Tuexen #else
669328cd0699SMichael Tuexen 						asoc->size_on_reasm_queue = 0;
669428cd0699SMichael Tuexen #endif
669528cd0699SMichael Tuexen 					}
669644249214SRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_reasm_queue);
669744249214SRandall Stewart 					SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
669828cd0699SMichael Tuexen 					TAILQ_REMOVE(&control->reasm, chk, sctp_next);
669944249214SRandall Stewart 					if (chk->data) {
670044249214SRandall Stewart 						sctp_m_freem(chk->data);
670144249214SRandall Stewart 						chk->data = NULL;
670244249214SRandall Stewart 					}
670344249214SRandall Stewart 					sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
670444249214SRandall Stewart 				}
670528cd0699SMichael Tuexen 				sctp_free_a_readq(stcb, control);
670628cd0699SMichael Tuexen 			}
670728cd0699SMichael Tuexen 		}
670828cd0699SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
670928cd0699SMichael Tuexen #ifdef INVARIANTS
671028cd0699SMichael Tuexen 			if (control->on_strm_q != SCTP_ON_UNORDERED) {
671128cd0699SMichael Tuexen 				panic("Huh control: %p on_q: %d -- not unordered?",
671228cd0699SMichael Tuexen 				    control, control->on_strm_q);
671328cd0699SMichael Tuexen 			}
671428cd0699SMichael Tuexen #endif
671528cd0699SMichael Tuexen 			if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
671628cd0699SMichael Tuexen 				/* Yep it is above cum-ack */
671728cd0699SMichael Tuexen 				cnt++;
671828cd0699SMichael Tuexen 				SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
671928cd0699SMichael Tuexen 				KASSERT(control->length > 0, ("control has zero length"));
672028cd0699SMichael Tuexen 				if (asoc->size_on_all_streams >= control->length) {
672128cd0699SMichael Tuexen 					asoc->size_on_all_streams -= control->length;
672228cd0699SMichael Tuexen 				} else {
672328cd0699SMichael Tuexen #ifdef INVARIANTS
672428cd0699SMichael Tuexen 					panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
672528cd0699SMichael Tuexen #else
672628cd0699SMichael Tuexen 					asoc->size_on_all_streams = 0;
672728cd0699SMichael Tuexen #endif
672828cd0699SMichael Tuexen 				}
672928cd0699SMichael Tuexen 				sctp_ucount_decr(asoc->cnt_on_all_streams);
673028cd0699SMichael Tuexen 				SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
673128cd0699SMichael Tuexen 				if (control->on_read_q) {
673228cd0699SMichael Tuexen 					TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
673328cd0699SMichael Tuexen 					control->on_read_q = 0;
673428cd0699SMichael Tuexen 				}
673528cd0699SMichael Tuexen 				TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
673628cd0699SMichael Tuexen 				control->on_strm_q = 0;
673728cd0699SMichael Tuexen 				if (control->data) {
673828cd0699SMichael Tuexen 					sctp_m_freem(control->data);
673928cd0699SMichael Tuexen 					control->data = NULL;
674028cd0699SMichael Tuexen 				}
674128cd0699SMichael Tuexen 				sctp_free_remote_addr(control->whoFrom);
674228cd0699SMichael Tuexen 				/* Now its reasm? */
674328cd0699SMichael Tuexen 				TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
674428cd0699SMichael Tuexen 					cnt++;
674528cd0699SMichael Tuexen 					SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
674628cd0699SMichael Tuexen 					KASSERT(chk->send_size > 0, ("chunk has zero length"));
674728cd0699SMichael Tuexen 					if (asoc->size_on_reasm_queue >= chk->send_size) {
674828cd0699SMichael Tuexen 						asoc->size_on_reasm_queue -= chk->send_size;
674928cd0699SMichael Tuexen 					} else {
675028cd0699SMichael Tuexen #ifdef INVARIANTS
675128cd0699SMichael Tuexen 						panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
675228cd0699SMichael Tuexen #else
675328cd0699SMichael Tuexen 						asoc->size_on_reasm_queue = 0;
675428cd0699SMichael Tuexen #endif
675528cd0699SMichael Tuexen 					}
675628cd0699SMichael Tuexen 					sctp_ucount_decr(asoc->cnt_on_reasm_queue);
675728cd0699SMichael Tuexen 					SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
675828cd0699SMichael Tuexen 					TAILQ_REMOVE(&control->reasm, chk, sctp_next);
675928cd0699SMichael Tuexen 					if (chk->data) {
676028cd0699SMichael Tuexen 						sctp_m_freem(chk->data);
676128cd0699SMichael Tuexen 						chk->data = NULL;
676228cd0699SMichael Tuexen 					}
676328cd0699SMichael Tuexen 					sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
676428cd0699SMichael Tuexen 				}
676528cd0699SMichael Tuexen 				sctp_free_a_readq(stcb, control);
6766f8829a4aSRandall Stewart 			}
6767f8829a4aSRandall Stewart 		}
6768f8829a4aSRandall Stewart 	}
6769b5c16493SMichael Tuexen 	if (cnt) {
6770b5c16493SMichael Tuexen 		/* We must back down to see what the new highest is */
677120b07a4dSMichael Tuexen 		for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6772b5c16493SMichael Tuexen 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6773b5c16493SMichael Tuexen 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6774b5c16493SMichael Tuexen 				asoc->highest_tsn_inside_map = i;
6775b5c16493SMichael Tuexen 				fnd = 1;
6776b5c16493SMichael Tuexen 				break;
6777b5c16493SMichael Tuexen 			}
6778b5c16493SMichael Tuexen 		}
6779b5c16493SMichael Tuexen 		if (!fnd) {
6780b5c16493SMichael Tuexen 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6781b5c16493SMichael Tuexen 		}
67820053ed28SMichael Tuexen 
6783f8829a4aSRandall Stewart 		/*
6784b5c16493SMichael Tuexen 		 * Question, should we go through the delivery queue? The
6785b5c16493SMichael Tuexen 		 * only reason things are on here is the app not reading OR
6786b5c16493SMichael Tuexen 		 * a p-d-api up. An attacker COULD send enough in to
6787b5c16493SMichael Tuexen 		 * initiate the PD-API and then send a bunch of stuff to
6788b5c16493SMichael Tuexen 		 * other streams... these would wind up on the delivery
6789b5c16493SMichael Tuexen 		 * queue.. and then we would not get to them. But in order
6790b5c16493SMichael Tuexen 		 * to do this I then have to back-track and un-deliver
6791b5c16493SMichael Tuexen 		 * sequence numbers in streams.. el-yucko. I think for now
6792b5c16493SMichael Tuexen 		 * we will NOT look at the delivery queue and leave it to be
6793b5c16493SMichael Tuexen 		 * something to consider later. An alternative would be to
6794b5c16493SMichael Tuexen 		 * abort the P-D-API with a notification and then deliver
6795b5c16493SMichael Tuexen 		 * the data.... Or another method might be to keep track of
6796b5c16493SMichael Tuexen 		 * how many times the situation occurs and if we see a
6797b5c16493SMichael Tuexen 		 * possible attack underway just abort the association.
6798f8829a4aSRandall Stewart 		 */
6799f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
6800ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6801ad81507eSRandall Stewart #endif
6802f8829a4aSRandall Stewart 		/*
6803f8829a4aSRandall Stewart 		 * Now do we need to find a new
6804f8829a4aSRandall Stewart 		 * asoc->highest_tsn_inside_map?
6805f8829a4aSRandall Stewart 		 */
6806f8829a4aSRandall Stewart 		asoc->last_revoke_count = cnt;
68075555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
68086a34ec63SMichael Tuexen 		    SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
680904ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
6810689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
6811ceaad40aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6812f8829a4aSRandall Stewart 	}
6813f8829a4aSRandall Stewart 	/*
6814f8829a4aSRandall Stewart 	 * Another issue, in un-setting the TSN's in the mapping array we
6815034b88b0SMichael Tuexen 	 * DID NOT adjust the highest_tsn marker.  This will cause one of
6816034b88b0SMichael Tuexen 	 * two things to occur. It may cause us to do extra work in checking
6817034b88b0SMichael Tuexen 	 * for our mapping array movement. More importantly it may cause us
6818034b88b0SMichael Tuexen 	 * to SACK every datagram. This may not be a bad thing though since
6819034b88b0SMichael Tuexen 	 * we will recover once we get our cum-ack above and all this stuff
6820034b88b0SMichael Tuexen 	 * we dumped recovered.
6821f8829a4aSRandall Stewart 	 */
6822f8829a4aSRandall Stewart }
6823f8829a4aSRandall Stewart 
682481a34d37SGleb Smirnoff static void
sctp_drain(void * arg __unused,int flags __unused)6825*8ee127efSSHENGYI HONG sctp_drain(void *arg __unused, int flags __unused)
6826f8829a4aSRandall Stewart {
682781a34d37SGleb Smirnoff 	struct epoch_tracker et;
6828f83db644SMichael Tuexen 
682981a34d37SGleb Smirnoff 	VNET_ITERATOR_DECL(vnet_iter);
683081a34d37SGleb Smirnoff 
683181a34d37SGleb Smirnoff 	NET_EPOCH_ENTER(et);
6832f8829a4aSRandall Stewart 	/*
6833f8829a4aSRandall Stewart 	 * We must walk the PCB lists for ALL associations here. The system
6834f8829a4aSRandall Stewart 	 * is LOW on MBUF's and needs help. This is where reneging will
6835f8829a4aSRandall Stewart 	 * occur. We really hope this does NOT happen!
6836f8829a4aSRandall Stewart 	 */
68378518270eSMichael Tuexen 	VNET_LIST_RLOCK_NOSLEEP();
68388518270eSMichael Tuexen 	VNET_FOREACH(vnet_iter) {
68398518270eSMichael Tuexen 		CURVNET_SET(vnet_iter);
6840f8829a4aSRandall Stewart 		struct sctp_inpcb *inp;
6841f8829a4aSRandall Stewart 		struct sctp_tcb *stcb;
6842f8829a4aSRandall Stewart 
68434b6492f5SMichael Tuexen 		SCTP_STAT_INCR(sctps_protocol_drain_calls);
68444b6492f5SMichael Tuexen 		if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
68454b6492f5SMichael Tuexen #ifdef VIMAGE
68464b6492f5SMichael Tuexen 			continue;
68474b6492f5SMichael Tuexen #else
684881a34d37SGleb Smirnoff 			NET_EPOCH_EXIT(et);
68494b6492f5SMichael Tuexen 			return;
68504b6492f5SMichael Tuexen #endif
68514b6492f5SMichael Tuexen 		}
6852f8829a4aSRandall Stewart 		SCTP_INP_INFO_RLOCK();
6853b3f1ea41SRandall Stewart 		LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6854f8829a4aSRandall Stewart 			/* For each endpoint */
6855f8829a4aSRandall Stewart 			SCTP_INP_RLOCK(inp);
6856f8829a4aSRandall Stewart 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6857f8829a4aSRandall Stewart 				/* For each association */
6858f8829a4aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
68597215cc1bSMichael Tuexen 				sctp_drain_mbufs(stcb);
6860f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
6861f8829a4aSRandall Stewart 			}
6862f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
6863f8829a4aSRandall Stewart 		}
6864f8829a4aSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
68658518270eSMichael Tuexen 		CURVNET_RESTORE();
68668518270eSMichael Tuexen 	}
68678518270eSMichael Tuexen 	VNET_LIST_RUNLOCK_NOSLEEP();
686881a34d37SGleb Smirnoff 	NET_EPOCH_EXIT(et);
6869f8829a4aSRandall Stewart }
6870f83db644SMichael Tuexen 
687181a34d37SGleb Smirnoff EVENTHANDLER_DEFINE(vm_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
687281a34d37SGleb Smirnoff EVENTHANDLER_DEFINE(mbuf_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
6873f8829a4aSRandall Stewart 
6874f8829a4aSRandall Stewart /*
6875f8829a4aSRandall Stewart  * start a new iterator
6876f8829a4aSRandall Stewart  * iterates through all endpoints and associations based on the pcb_state
6877f8829a4aSRandall Stewart  * flags and asoc_state.  "af" (mandatory) is executed for all matching
6878f8829a4aSRandall Stewart  * assocs and "ef" (optional) is executed when the iterator completes.
6879f8829a4aSRandall Stewart  * "inpf" (optional) is executed for each new endpoint as it is being
6880bff64a4dSRandall Stewart  * iterated through. inpe (optional) is called when the inp completes
6881bff64a4dSRandall Stewart  * its way through all the stcbs.
6882f8829a4aSRandall Stewart  */
6883f8829a4aSRandall Stewart int
sctp_initiate_iterator(inp_func inpf,asoc_func af,inp_func inpe,uint32_t pcb_state,uint32_t pcb_features,uint32_t asoc_state,void * argp,uint32_t argi,end_func ef,struct sctp_inpcb * s_inp,uint8_t chunk_output_off)688442551e99SRandall Stewart sctp_initiate_iterator(inp_func inpf,
688542551e99SRandall Stewart     asoc_func af,
688642551e99SRandall Stewart     inp_func inpe,
688742551e99SRandall Stewart     uint32_t pcb_state,
688842551e99SRandall Stewart     uint32_t pcb_features,
688942551e99SRandall Stewart     uint32_t asoc_state,
689042551e99SRandall Stewart     void *argp,
689142551e99SRandall Stewart     uint32_t argi,
689242551e99SRandall Stewart     end_func ef,
689342551e99SRandall Stewart     struct sctp_inpcb *s_inp,
689442551e99SRandall Stewart     uint8_t chunk_output_off)
6895f8829a4aSRandall Stewart {
6896f8829a4aSRandall Stewart 	struct sctp_iterator *it = NULL;
6897f8829a4aSRandall Stewart 
6898f8829a4aSRandall Stewart 	if (af == NULL) {
6899f8829a4aSRandall Stewart 		return (-1);
6900f8829a4aSRandall Stewart 	}
69012b1c7de4SMichael Tuexen 	if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
69022b1c7de4SMichael Tuexen 		SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
69032b1c7de4SMichael Tuexen 		    SCTP_BASE_VAR(sctp_pcb_initialized));
69042b1c7de4SMichael Tuexen 		return (-1);
69052b1c7de4SMichael Tuexen 	}
6906f8829a4aSRandall Stewart 	SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6907207304d4SRandall Stewart 	    SCTP_M_ITER);
6908f8829a4aSRandall Stewart 	if (it == NULL) {
6909c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
69104b66d476SMichael Tuexen 		return (-1);
6911f8829a4aSRandall Stewart 	}
6912f8829a4aSRandall Stewart 	memset(it, 0, sizeof(*it));
6913f8829a4aSRandall Stewart 	it->function_assoc = af;
6914f8829a4aSRandall Stewart 	it->function_inp = inpf;
691542551e99SRandall Stewart 	if (inpf)
691642551e99SRandall Stewart 		it->done_current_ep = 0;
691742551e99SRandall Stewart 	else
691842551e99SRandall Stewart 		it->done_current_ep = 1;
6919f8829a4aSRandall Stewart 	it->function_atend = ef;
6920f8829a4aSRandall Stewart 	it->pointer = argp;
6921f8829a4aSRandall Stewart 	it->val = argi;
6922f8829a4aSRandall Stewart 	it->pcb_flags = pcb_state;
6923f8829a4aSRandall Stewart 	it->pcb_features = pcb_features;
6924f8829a4aSRandall Stewart 	it->asoc_state = asoc_state;
692542551e99SRandall Stewart 	it->function_inp_end = inpe;
6926f8829a4aSRandall Stewart 	it->no_chunk_output = chunk_output_off;
6927f7517433SRandall Stewart 	it->vn = curvnet;
6928f8829a4aSRandall Stewart 	if (s_inp) {
6929ec4c19fcSRandall Stewart 		/* Assume lock is held here */
6930f8829a4aSRandall Stewart 		it->inp = s_inp;
6931ec4c19fcSRandall Stewart 		SCTP_INP_INCR_REF(it->inp);
6932f8829a4aSRandall Stewart 		it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6933f8829a4aSRandall Stewart 	} else {
6934f8829a4aSRandall Stewart 		SCTP_INP_INFO_RLOCK();
6935b3f1ea41SRandall Stewart 		it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6936ec4c19fcSRandall Stewart 		if (it->inp) {
6937ec4c19fcSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
6938ec4c19fcSRandall Stewart 		}
6939f8829a4aSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
6940f8829a4aSRandall Stewart 		it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6941f8829a4aSRandall Stewart 	}
694242551e99SRandall Stewart 	SCTP_IPI_ITERATOR_WQ_LOCK();
69432b1c7de4SMichael Tuexen 	if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
69442b1c7de4SMichael Tuexen 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
69452b1c7de4SMichael Tuexen 		SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
69462b1c7de4SMichael Tuexen 		    SCTP_BASE_VAR(sctp_pcb_initialized), it);
69472b1c7de4SMichael Tuexen 		SCTP_FREE(it, SCTP_M_ITER);
69482b1c7de4SMichael Tuexen 		return (-1);
69492b1c7de4SMichael Tuexen 	}
6950f7517433SRandall Stewart 	TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6951f7517433SRandall Stewart 	if (sctp_it_ctl.iterator_running == 0) {
695242551e99SRandall Stewart 		sctp_wakeup_iterator();
695342551e99SRandall Stewart 	}
695442551e99SRandall Stewart 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
69553c503c28SRandall Stewart 	/* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
6956f8829a4aSRandall Stewart 	return (0);
6957f8829a4aSRandall Stewart }
6958a5c2009dSMichael Tuexen 
6959a5c2009dSMichael Tuexen /*
6960a5c2009dSMichael Tuexen  * Atomically add flags to the sctp_flags of an inp.
6961a5c2009dSMichael Tuexen  * To be used when the write lock of the inp is not held.
6962a5c2009dSMichael Tuexen  */
6963a5c2009dSMichael Tuexen void
sctp_pcb_add_flags(struct sctp_inpcb * inp,uint32_t flags)6964a5c2009dSMichael Tuexen sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags)
6965a5c2009dSMichael Tuexen {
6966a5c2009dSMichael Tuexen 	uint32_t old_flags, new_flags;
6967a5c2009dSMichael Tuexen 
6968a5c2009dSMichael Tuexen 	do {
6969a5c2009dSMichael Tuexen 		old_flags = inp->sctp_flags;
6970a5c2009dSMichael Tuexen 		new_flags = old_flags | flags;
6971a5c2009dSMichael Tuexen 	} while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0);
6972a5c2009dSMichael Tuexen }
6973