xref: /freebsd/sys/netinet/sctp_asconf.c (revision 2678fe1ee9213da435ccb398bfa81d825710e6a6)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
35d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
45d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart /* $KAME: sctp_asconf.c,v 1.24 2005/03/06 16:04:16 itojun Exp $	 */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36cef8ad06SRandall Stewart __FBSDID("$FreeBSD$");
37f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3942551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
42f8829a4aSRandall Stewart #include <netinet/sctputil.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
45851b7298SRandall Stewart #include <netinet/sctp_timer.h>
46f8829a4aSRandall Stewart 
47f8829a4aSRandall Stewart /*
48f8829a4aSRandall Stewart  * debug flags:
49f8829a4aSRandall Stewart  * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
50f8829a4aSRandall Stewart  * SCTP_DEBUG_ASCONF2: detailed info
51f8829a4aSRandall Stewart  */
52f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
53f8829a4aSRandall Stewart #endif				/* SCTP_DEBUG */
54f8829a4aSRandall Stewart 
55f8829a4aSRandall Stewart 
56ad81507eSRandall Stewart static void
57f8829a4aSRandall Stewart sctp_asconf_get_source_ip(struct mbuf *m, struct sockaddr *sa)
58f8829a4aSRandall Stewart {
59f8829a4aSRandall Stewart 	struct ip *iph;
60f8829a4aSRandall Stewart 	struct sockaddr_in *sin;
61f8829a4aSRandall Stewart 
62f8829a4aSRandall Stewart #ifdef INET6
63f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
64f8829a4aSRandall Stewart 
65f8829a4aSRandall Stewart #endif
66f8829a4aSRandall Stewart 
67f8829a4aSRandall Stewart 	iph = mtod(m, struct ip *);
68f8829a4aSRandall Stewart 	if (iph->ip_v == IPVERSION) {
69f8829a4aSRandall Stewart 		/* IPv4 source */
70f8829a4aSRandall Stewart 		sin = (struct sockaddr_in *)sa;
71f8829a4aSRandall Stewart 		bzero(sin, sizeof(*sin));
72f8829a4aSRandall Stewart 		sin->sin_family = AF_INET;
73f8829a4aSRandall Stewart 		sin->sin_len = sizeof(struct sockaddr_in);
74f8829a4aSRandall Stewart 		sin->sin_port = 0;
75f8829a4aSRandall Stewart 		sin->sin_addr.s_addr = iph->ip_src.s_addr;
76ad81507eSRandall Stewart 		return;
77f8829a4aSRandall Stewart 	}
78f8829a4aSRandall Stewart #ifdef INET6
79f8829a4aSRandall Stewart 	else if (iph->ip_v == (IPV6_VERSION >> 4)) {
80f8829a4aSRandall Stewart 		/* IPv6 source */
81f8829a4aSRandall Stewart 		struct ip6_hdr *ip6;
82f8829a4aSRandall Stewart 
83f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)sa;
84f8829a4aSRandall Stewart 		bzero(sin6, sizeof(*sin6));
85f8829a4aSRandall Stewart 		sin6->sin6_family = AF_INET6;
86f8829a4aSRandall Stewart 		sin6->sin6_len = sizeof(struct sockaddr_in6);
87f8829a4aSRandall Stewart 		sin6->sin6_port = 0;
88f8829a4aSRandall Stewart 		ip6 = mtod(m, struct ip6_hdr *);
89f8829a4aSRandall Stewart 		sin6->sin6_addr = ip6->ip6_src;
90ad81507eSRandall Stewart 		return;
91f8829a4aSRandall Stewart 	}
92f8829a4aSRandall Stewart #endif				/* INET6 */
93f8829a4aSRandall Stewart 	else
94ad81507eSRandall Stewart 		return;
95f8829a4aSRandall Stewart }
96f8829a4aSRandall Stewart 
97f8829a4aSRandall Stewart /*
98f8829a4aSRandall Stewart  * draft-ietf-tsvwg-addip-sctp
99f8829a4aSRandall Stewart  *
100f8829a4aSRandall Stewart  * An ASCONF parameter queue exists per asoc which holds the pending address
101f8829a4aSRandall Stewart  * operations.  Lists are updated upon receipt of ASCONF-ACK.
102f8829a4aSRandall Stewart  *
10318e198d3SRandall Stewart  * A restricted_addrs list exists per assoc to hold local addresses that are
10418e198d3SRandall Stewart  * not (yet) usable by the assoc as a source address.  These addresses are
10518e198d3SRandall Stewart  * either pending an ASCONF operation (and exist on the ASCONF parameter
10618e198d3SRandall Stewart  * queue), or they are permanently restricted (the peer has returned an
10718e198d3SRandall Stewart  * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
10818e198d3SRandall Stewart  *
109f8829a4aSRandall Stewart  * Deleted addresses are always immediately removed from the lists as they will
110f8829a4aSRandall Stewart  * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
111f8829a4aSRandall Stewart  * only if allowed.
112f8829a4aSRandall Stewart  */
113f8829a4aSRandall Stewart 
114f8829a4aSRandall Stewart /*
11518e198d3SRandall Stewart  * ASCONF parameter processing.
11618e198d3SRandall Stewart  * response_required: set if a reply is required (eg. SUCCESS_REPORT).
11718e198d3SRandall Stewart  * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
11818e198d3SRandall Stewart  * FIX: allocating this many mbufs on the fly is pretty inefficient...
119f8829a4aSRandall Stewart  */
120f8829a4aSRandall Stewart static struct mbuf *
121f8829a4aSRandall Stewart sctp_asconf_success_response(uint32_t id)
122f8829a4aSRandall Stewart {
123f8829a4aSRandall Stewart 	struct mbuf *m_reply = NULL;
124f8829a4aSRandall Stewart 	struct sctp_asconf_paramhdr *aph;
125f8829a4aSRandall Stewart 
126f8829a4aSRandall Stewart 	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
127f8829a4aSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
128f8829a4aSRandall Stewart 	if (m_reply == NULL) {
129ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
130ad81507eSRandall Stewart 		    "asconf_success_response: couldn't get mbuf!\n");
131f8829a4aSRandall Stewart 		return NULL;
132f8829a4aSRandall Stewart 	}
133f8829a4aSRandall Stewart 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
134f8829a4aSRandall Stewart 	aph->correlation_id = id;
135f8829a4aSRandall Stewart 	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
136f8829a4aSRandall Stewart 	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
137139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
138f8829a4aSRandall Stewart 	aph->ph.param_length = htons(aph->ph.param_length);
139f8829a4aSRandall Stewart 
140f8829a4aSRandall Stewart 	return m_reply;
141f8829a4aSRandall Stewart }
142f8829a4aSRandall Stewart 
143f8829a4aSRandall Stewart static struct mbuf *
144f8829a4aSRandall Stewart sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
145f8829a4aSRandall Stewart     uint16_t tlv_length)
146f8829a4aSRandall Stewart {
147f8829a4aSRandall Stewart 	struct mbuf *m_reply = NULL;
148f8829a4aSRandall Stewart 	struct sctp_asconf_paramhdr *aph;
149f8829a4aSRandall Stewart 	struct sctp_error_cause *error;
150f8829a4aSRandall Stewart 	uint8_t *tlv;
151f8829a4aSRandall Stewart 
152f8829a4aSRandall Stewart 	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
153f8829a4aSRandall Stewart 	    tlv_length +
154f8829a4aSRandall Stewart 	    sizeof(struct sctp_error_cause)),
155f8829a4aSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
156f8829a4aSRandall Stewart 	if (m_reply == NULL) {
157ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
158ad81507eSRandall Stewart 		    "asconf_error_response: couldn't get mbuf!\n");
159f8829a4aSRandall Stewart 		return NULL;
160f8829a4aSRandall Stewart 	}
161f8829a4aSRandall Stewart 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
162f8829a4aSRandall Stewart 	error = (struct sctp_error_cause *)(aph + 1);
163f8829a4aSRandall Stewart 
164f8829a4aSRandall Stewart 	aph->correlation_id = id;
165f8829a4aSRandall Stewart 	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
166f8829a4aSRandall Stewart 	error->code = htons(cause);
167f8829a4aSRandall Stewart 	error->length = tlv_length + sizeof(struct sctp_error_cause);
168f8829a4aSRandall Stewart 	aph->ph.param_length = error->length +
169f8829a4aSRandall Stewart 	    sizeof(struct sctp_asconf_paramhdr);
170f8829a4aSRandall Stewart 
171f8829a4aSRandall Stewart 	if (aph->ph.param_length > MLEN) {
172ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
173ad81507eSRandall Stewart 		    "asconf_error_response: tlv_length (%xh) too big\n",
174f8829a4aSRandall Stewart 		    tlv_length);
175f8829a4aSRandall Stewart 		sctp_m_freem(m_reply);	/* discard */
176f8829a4aSRandall Stewart 		return NULL;
177f8829a4aSRandall Stewart 	}
178f8829a4aSRandall Stewart 	if (error_tlv != NULL) {
179f8829a4aSRandall Stewart 		tlv = (uint8_t *) (error + 1);
180f8829a4aSRandall Stewart 		memcpy(tlv, error_tlv, tlv_length);
181f8829a4aSRandall Stewart 	}
182139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
183f8829a4aSRandall Stewart 	error->length = htons(error->length);
184f8829a4aSRandall Stewart 	aph->ph.param_length = htons(aph->ph.param_length);
185f8829a4aSRandall Stewart 
186f8829a4aSRandall Stewart 	return m_reply;
187f8829a4aSRandall Stewart }
188f8829a4aSRandall Stewart 
189f8829a4aSRandall Stewart static struct mbuf *
190f8829a4aSRandall Stewart sctp_process_asconf_add_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
191f8829a4aSRandall Stewart     struct sctp_tcb *stcb, int response_required)
192f8829a4aSRandall Stewart {
193f8829a4aSRandall Stewart 	struct mbuf *m_reply = NULL;
194f8829a4aSRandall Stewart 	struct sockaddr_storage sa_source, sa_store;
195f8829a4aSRandall Stewart 	struct sctp_ipv4addr_param *v4addr;
196f8829a4aSRandall Stewart 	uint16_t param_type, param_length, aparam_length;
197f8829a4aSRandall Stewart 	struct sockaddr *sa;
198f8829a4aSRandall Stewart 	struct sockaddr_in *sin;
199f8829a4aSRandall Stewart 	int zero_address = 0;
200f8829a4aSRandall Stewart 
201f8829a4aSRandall Stewart #ifdef INET6
202f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
203f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param *v6addr;
204f8829a4aSRandall Stewart 
205f8829a4aSRandall Stewart #endif				/* INET6 */
206f8829a4aSRandall Stewart 
207f8829a4aSRandall Stewart 	aparam_length = ntohs(aph->ph.param_length);
208f8829a4aSRandall Stewart 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
209f8829a4aSRandall Stewart #ifdef INET6
210f8829a4aSRandall Stewart 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
211f8829a4aSRandall Stewart #endif				/* INET6 */
212f8829a4aSRandall Stewart 	param_type = ntohs(v4addr->ph.param_type);
213f8829a4aSRandall Stewart 	param_length = ntohs(v4addr->ph.param_length);
214f8829a4aSRandall Stewart 
215f8829a4aSRandall Stewart 	sa = (struct sockaddr *)&sa_store;
216f8829a4aSRandall Stewart 	switch (param_type) {
217f8829a4aSRandall Stewart 	case SCTP_IPV4_ADDRESS:
218f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
219f8829a4aSRandall Stewart 			/* invalid param size */
220f8829a4aSRandall Stewart 			return NULL;
221f8829a4aSRandall Stewart 		}
222f8829a4aSRandall Stewart 		sin = (struct sockaddr_in *)&sa_store;
223f8829a4aSRandall Stewart 		bzero(sin, sizeof(*sin));
224f8829a4aSRandall Stewart 		sin->sin_family = AF_INET;
225f8829a4aSRandall Stewart 		sin->sin_len = sizeof(struct sockaddr_in);
226f8829a4aSRandall Stewart 		sin->sin_port = stcb->rport;
227f8829a4aSRandall Stewart 		sin->sin_addr.s_addr = v4addr->addr;
228f8829a4aSRandall Stewart 		if (sin->sin_addr.s_addr == INADDR_ANY)
229f8829a4aSRandall Stewart 			zero_address = 1;
230ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
231ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
232f8829a4aSRandall Stewart 		break;
233f8829a4aSRandall Stewart 	case SCTP_IPV6_ADDRESS:
234f8829a4aSRandall Stewart #ifdef INET6
235f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
236f8829a4aSRandall Stewart 			/* invalid param size */
237f8829a4aSRandall Stewart 			return NULL;
238f8829a4aSRandall Stewart 		}
239f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)&sa_store;
240f8829a4aSRandall Stewart 		bzero(sin6, sizeof(*sin6));
241f8829a4aSRandall Stewart 		sin6->sin6_family = AF_INET6;
242f8829a4aSRandall Stewart 		sin6->sin6_len = sizeof(struct sockaddr_in6);
243f8829a4aSRandall Stewart 		sin6->sin6_port = stcb->rport;
244f8829a4aSRandall Stewart 		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
245f8829a4aSRandall Stewart 		    sizeof(struct in6_addr));
246f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
247f8829a4aSRandall Stewart 			zero_address = 1;
248ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
249ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
250f8829a4aSRandall Stewart #else
251f8829a4aSRandall Stewart 		/* IPv6 not enabled! */
252f8829a4aSRandall Stewart 		/* FIX ME: currently sends back an invalid param error */
253f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
254f8829a4aSRandall Stewart 		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph, aparam_length);
255ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
256ad81507eSRandall Stewart 		    "process_asconf_add_ip: v6 disabled- skipping ");
257ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
258f8829a4aSRandall Stewart 		return m_reply;
259ad81507eSRandall Stewart #endif
260f8829a4aSRandall Stewart 		break;
261f8829a4aSRandall Stewart 	default:
262f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
263f8829a4aSRandall Stewart 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
264f8829a4aSRandall Stewart 		    aparam_length);
265f8829a4aSRandall Stewart 		return m_reply;
266f8829a4aSRandall Stewart 	}			/* end switch */
267f8829a4aSRandall Stewart 
268f8829a4aSRandall Stewart 	/* if 0.0.0.0/::0, add the source address instead */
269b3f1ea41SRandall Stewart 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
270f8829a4aSRandall Stewart 		sa = (struct sockaddr *)&sa_source;
271f8829a4aSRandall Stewart 		sctp_asconf_get_source_ip(m, sa);
272ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
273ad81507eSRandall Stewart 		    "process_asconf_add_ip: using source addr ");
274ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
275f8829a4aSRandall Stewart 	}
276f8829a4aSRandall Stewart 	/* add the address */
277a5d547adSRandall Stewart 	if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE,
278a5d547adSRandall Stewart 	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
279ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
280ad81507eSRandall Stewart 		    "process_asconf_add_ip: error adding address\n");
281f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
282f8829a4aSRandall Stewart 		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
283f8829a4aSRandall Stewart 		    aparam_length);
284f8829a4aSRandall Stewart 	} else {
285f8829a4aSRandall Stewart 		/* notify upper layer */
286ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
287f8829a4aSRandall Stewart 		if (response_required) {
288f8829a4aSRandall Stewart 			m_reply =
289f8829a4aSRandall Stewart 			    sctp_asconf_success_response(aph->correlation_id);
290f8829a4aSRandall Stewart 		}
2913c503c28SRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
2923c503c28SRandall Stewart 		    NULL, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
2933c503c28SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2943c503c28SRandall Stewart 		    stcb, NULL);
295f8829a4aSRandall Stewart 	}
296f8829a4aSRandall Stewart 
297f8829a4aSRandall Stewart 	return m_reply;
298f8829a4aSRandall Stewart }
299f8829a4aSRandall Stewart 
300f8829a4aSRandall Stewart static int
3011b649582SRandall Stewart sctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
302f8829a4aSRandall Stewart {
303f8829a4aSRandall Stewart 	struct sctp_nets *src_net, *net;
304f8829a4aSRandall Stewart 
305f8829a4aSRandall Stewart 	/* make sure the source address exists as a destination net */
306f8829a4aSRandall Stewart 	src_net = sctp_findnet(stcb, src);
307f8829a4aSRandall Stewart 	if (src_net == NULL) {
308f8829a4aSRandall Stewart 		/* not found */
309f8829a4aSRandall Stewart 		return -1;
310f8829a4aSRandall Stewart 	}
311f8829a4aSRandall Stewart 	/* delete all destination addresses except the source */
312f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
313f8829a4aSRandall Stewart 		if (net != src_net) {
314f8829a4aSRandall Stewart 			/* delete this address */
315f8829a4aSRandall Stewart 			sctp_remove_net(stcb, net);
316ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1,
317ad81507eSRandall Stewart 			    "asconf_del_remote_addrs_except: deleting ");
318ad81507eSRandall Stewart 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
319ad81507eSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr);
320f8829a4aSRandall Stewart 			/* notify upper layer */
321f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
322ceaad40aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
323f8829a4aSRandall Stewart 		}
324f8829a4aSRandall Stewart 	}
325f8829a4aSRandall Stewart 	return 0;
326f8829a4aSRandall Stewart }
327f8829a4aSRandall Stewart 
328f8829a4aSRandall Stewart static struct mbuf *
329f8829a4aSRandall Stewart sctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
330f8829a4aSRandall Stewart     struct sctp_tcb *stcb, int response_required)
331f8829a4aSRandall Stewart {
332f8829a4aSRandall Stewart 	struct mbuf *m_reply = NULL;
333f8829a4aSRandall Stewart 	struct sockaddr_storage sa_source, sa_store;
334f8829a4aSRandall Stewart 	struct sctp_ipv4addr_param *v4addr;
335f8829a4aSRandall Stewart 	uint16_t param_type, param_length, aparam_length;
336f8829a4aSRandall Stewart 	struct sockaddr *sa;
337f8829a4aSRandall Stewart 	struct sockaddr_in *sin;
338f8829a4aSRandall Stewart 	int zero_address = 0;
339f8829a4aSRandall Stewart 	int result;
340f8829a4aSRandall Stewart 
341f8829a4aSRandall Stewart #ifdef INET6
342f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
343f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param *v6addr;
344f8829a4aSRandall Stewart 
345f8829a4aSRandall Stewart #endif				/* INET6 */
346f8829a4aSRandall Stewart 
347f8829a4aSRandall Stewart 	/* get the source IP address for src and 0.0.0.0/::0 delete checks */
348f8829a4aSRandall Stewart 	sctp_asconf_get_source_ip(m, (struct sockaddr *)&sa_source);
349f8829a4aSRandall Stewart 
350f8829a4aSRandall Stewart 	aparam_length = ntohs(aph->ph.param_length);
351f8829a4aSRandall Stewart 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
352f8829a4aSRandall Stewart #ifdef INET6
353f8829a4aSRandall Stewart 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
354f8829a4aSRandall Stewart #endif				/* INET6 */
355f8829a4aSRandall Stewart 	param_type = ntohs(v4addr->ph.param_type);
356f8829a4aSRandall Stewart 	param_length = ntohs(v4addr->ph.param_length);
357f8829a4aSRandall Stewart 
358f8829a4aSRandall Stewart 	sa = (struct sockaddr *)&sa_store;
359f8829a4aSRandall Stewart 	switch (param_type) {
360f8829a4aSRandall Stewart 	case SCTP_IPV4_ADDRESS:
361f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
362f8829a4aSRandall Stewart 			/* invalid param size */
363f8829a4aSRandall Stewart 			return NULL;
364f8829a4aSRandall Stewart 		}
365f8829a4aSRandall Stewart 		sin = (struct sockaddr_in *)&sa_store;
366f8829a4aSRandall Stewart 		bzero(sin, sizeof(*sin));
367f8829a4aSRandall Stewart 		sin->sin_family = AF_INET;
368f8829a4aSRandall Stewart 		sin->sin_len = sizeof(struct sockaddr_in);
369f8829a4aSRandall Stewart 		sin->sin_port = stcb->rport;
370f8829a4aSRandall Stewart 		sin->sin_addr.s_addr = v4addr->addr;
371f8829a4aSRandall Stewart 		if (sin->sin_addr.s_addr == INADDR_ANY)
372f8829a4aSRandall Stewart 			zero_address = 1;
373ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
374ad81507eSRandall Stewart 		    "process_asconf_delete_ip: deleting ");
375ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
376f8829a4aSRandall Stewart 		break;
377f8829a4aSRandall Stewart 	case SCTP_IPV6_ADDRESS:
378f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
379f8829a4aSRandall Stewart 			/* invalid param size */
380f8829a4aSRandall Stewart 			return NULL;
381f8829a4aSRandall Stewart 		}
382f8829a4aSRandall Stewart #ifdef INET6
383f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)&sa_store;
384f8829a4aSRandall Stewart 		bzero(sin6, sizeof(*sin6));
385f8829a4aSRandall Stewart 		sin6->sin6_family = AF_INET6;
386f8829a4aSRandall Stewart 		sin6->sin6_len = sizeof(struct sockaddr_in6);
387f8829a4aSRandall Stewart 		sin6->sin6_port = stcb->rport;
388f8829a4aSRandall Stewart 		memcpy(&sin6->sin6_addr, v6addr->addr,
389f8829a4aSRandall Stewart 		    sizeof(struct in6_addr));
390f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
391f8829a4aSRandall Stewart 			zero_address = 1;
392ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
393ad81507eSRandall Stewart 		    "process_asconf_delete_ip: deleting ");
394ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
395f8829a4aSRandall Stewart #else
396f8829a4aSRandall Stewart 		/* IPv6 not enabled!  No "action" needed; just ack it */
397ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
398ad81507eSRandall Stewart 		    "process_asconf_delete_ip: v6 disabled- ignoring: ");
399ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
400f8829a4aSRandall Stewart 		/* just respond with a "success" ASCONF-ACK */
401f8829a4aSRandall Stewart 		return NULL;
402ad81507eSRandall Stewart #endif
403f8829a4aSRandall Stewart 		break;
404f8829a4aSRandall Stewart 	default:
405f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
406f8829a4aSRandall Stewart 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
407f8829a4aSRandall Stewart 		    aparam_length);
408f8829a4aSRandall Stewart 		return m_reply;
409f8829a4aSRandall Stewart 	}
410f8829a4aSRandall Stewart 
411f8829a4aSRandall Stewart 	/* make sure the source address is not being deleted */
412f8829a4aSRandall Stewart 	if (sctp_cmpaddr(sa, (struct sockaddr *)&sa_source)) {
413f8829a4aSRandall Stewart 		/* trying to delete the source address! */
414ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
415f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
416f8829a4aSRandall Stewart 		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
417f8829a4aSRandall Stewart 		    aparam_length);
418f8829a4aSRandall Stewart 		return m_reply;
419f8829a4aSRandall Stewart 	}
420f8829a4aSRandall Stewart 	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
421b3f1ea41SRandall Stewart 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
422f8829a4aSRandall Stewart 		result = sctp_asconf_del_remote_addrs_except(stcb,
423f8829a4aSRandall Stewart 		    (struct sockaddr *)&sa_source);
424f8829a4aSRandall Stewart 
425f8829a4aSRandall Stewart 		if (result) {
426f8829a4aSRandall Stewart 			/* src address did not exist? */
427ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
428f8829a4aSRandall Stewart 			/* what error to reply with?? */
429f8829a4aSRandall Stewart 			m_reply =
430f8829a4aSRandall Stewart 			    sctp_asconf_error_response(aph->correlation_id,
431f8829a4aSRandall Stewart 			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
432f8829a4aSRandall Stewart 			    aparam_length);
433f8829a4aSRandall Stewart 		} else if (response_required) {
434f8829a4aSRandall Stewart 			m_reply =
435f8829a4aSRandall Stewart 			    sctp_asconf_success_response(aph->correlation_id);
436f8829a4aSRandall Stewart 		}
437f8829a4aSRandall Stewart 		return m_reply;
438f8829a4aSRandall Stewart 	}
439f8829a4aSRandall Stewart 	/* delete the address */
440f8829a4aSRandall Stewart 	result = sctp_del_remote_addr(stcb, sa);
441f8829a4aSRandall Stewart 	/*
442f8829a4aSRandall Stewart 	 * note if result == -2, the address doesn't exist in the asoc but
443f8829a4aSRandall Stewart 	 * since it's being deleted anyways, we just ack the delete -- but
444f8829a4aSRandall Stewart 	 * this probably means something has already gone awry
445f8829a4aSRandall Stewart 	 */
446f8829a4aSRandall Stewart 	if (result == -1) {
447f8829a4aSRandall Stewart 		/* only one address in the asoc */
448ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
449f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
450f8829a4aSRandall Stewart 		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
451f8829a4aSRandall Stewart 		    aparam_length);
452f8829a4aSRandall Stewart 	} else {
453f8829a4aSRandall Stewart 		if (response_required) {
454f8829a4aSRandall Stewart 			m_reply = sctp_asconf_success_response(aph->correlation_id);
455f8829a4aSRandall Stewart 		}
456f8829a4aSRandall Stewart 		/* notify upper layer */
457ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
458f8829a4aSRandall Stewart 	}
459f8829a4aSRandall Stewart 	return m_reply;
460f8829a4aSRandall Stewart }
461f8829a4aSRandall Stewart 
462f8829a4aSRandall Stewart static struct mbuf *
463f8829a4aSRandall Stewart sctp_process_asconf_set_primary(struct mbuf *m,
4641b649582SRandall Stewart     struct sctp_asconf_paramhdr *aph,
4651b649582SRandall Stewart     struct sctp_tcb *stcb, int response_required)
466f8829a4aSRandall Stewart {
467f8829a4aSRandall Stewart 	struct mbuf *m_reply = NULL;
468f8829a4aSRandall Stewart 	struct sockaddr_storage sa_source, sa_store;
469f8829a4aSRandall Stewart 	struct sctp_ipv4addr_param *v4addr;
470f8829a4aSRandall Stewart 	uint16_t param_type, param_length, aparam_length;
471f8829a4aSRandall Stewart 	struct sockaddr *sa;
472f8829a4aSRandall Stewart 	struct sockaddr_in *sin;
473f8829a4aSRandall Stewart 	int zero_address = 0;
474f8829a4aSRandall Stewart 
475f8829a4aSRandall Stewart #ifdef INET6
476f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
477f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param *v6addr;
478f8829a4aSRandall Stewart 
479f8829a4aSRandall Stewart #endif				/* INET6 */
480f8829a4aSRandall Stewart 
481f8829a4aSRandall Stewart 	aparam_length = ntohs(aph->ph.param_length);
482f8829a4aSRandall Stewart 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
483f8829a4aSRandall Stewart #ifdef INET6
484f8829a4aSRandall Stewart 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
485f8829a4aSRandall Stewart #endif				/* INET6 */
486f8829a4aSRandall Stewart 	param_type = ntohs(v4addr->ph.param_type);
487f8829a4aSRandall Stewart 	param_length = ntohs(v4addr->ph.param_length);
488f8829a4aSRandall Stewart 
489f8829a4aSRandall Stewart 	sa = (struct sockaddr *)&sa_store;
490f8829a4aSRandall Stewart 	switch (param_type) {
491f8829a4aSRandall Stewart 	case SCTP_IPV4_ADDRESS:
492f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
493f8829a4aSRandall Stewart 			/* invalid param size */
494f8829a4aSRandall Stewart 			return NULL;
495f8829a4aSRandall Stewart 		}
496f8829a4aSRandall Stewart 		sin = (struct sockaddr_in *)&sa_store;
497f8829a4aSRandall Stewart 		bzero(sin, sizeof(*sin));
498f8829a4aSRandall Stewart 		sin->sin_family = AF_INET;
499f8829a4aSRandall Stewart 		sin->sin_len = sizeof(struct sockaddr_in);
500f8829a4aSRandall Stewart 		sin->sin_addr.s_addr = v4addr->addr;
501f8829a4aSRandall Stewart 		if (sin->sin_addr.s_addr == INADDR_ANY)
502f8829a4aSRandall Stewart 			zero_address = 1;
503ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
504ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
505f8829a4aSRandall Stewart 		break;
506f8829a4aSRandall Stewart 	case SCTP_IPV6_ADDRESS:
507f8829a4aSRandall Stewart 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
508f8829a4aSRandall Stewart 			/* invalid param size */
509f8829a4aSRandall Stewart 			return NULL;
510f8829a4aSRandall Stewart 		}
511f8829a4aSRandall Stewart #ifdef INET6
512f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)&sa_store;
513f8829a4aSRandall Stewart 		bzero(sin6, sizeof(*sin6));
514f8829a4aSRandall Stewart 		sin6->sin6_family = AF_INET6;
515f8829a4aSRandall Stewart 		sin6->sin6_len = sizeof(struct sockaddr_in6);
516f8829a4aSRandall Stewart 		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
517f8829a4aSRandall Stewart 		    sizeof(struct in6_addr));
518f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
519f8829a4aSRandall Stewart 			zero_address = 1;
520ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
521ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
522f8829a4aSRandall Stewart #else
523f8829a4aSRandall Stewart 		/* IPv6 not enabled!  No "action" needed; just ack it */
524ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
525ad81507eSRandall Stewart 		    "process_asconf_set_primary: v6 disabled- ignoring: ");
526ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
527f8829a4aSRandall Stewart 		/* just respond with a "success" ASCONF-ACK */
528f8829a4aSRandall Stewart 		return NULL;
529ad81507eSRandall Stewart #endif
530f8829a4aSRandall Stewart 		break;
531f8829a4aSRandall Stewart 	default:
532f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
533f8829a4aSRandall Stewart 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
534f8829a4aSRandall Stewart 		    aparam_length);
535f8829a4aSRandall Stewart 		return m_reply;
536f8829a4aSRandall Stewart 	}
537f8829a4aSRandall Stewart 
538f8829a4aSRandall Stewart 	/* if 0.0.0.0/::0, use the source address instead */
539b3f1ea41SRandall Stewart 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
540f8829a4aSRandall Stewart 		sa = (struct sockaddr *)&sa_source;
541f8829a4aSRandall Stewart 		sctp_asconf_get_source_ip(m, sa);
542ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
543ad81507eSRandall Stewart 		    "process_asconf_set_primary: using source addr ");
544ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
545f8829a4aSRandall Stewart 	}
546f8829a4aSRandall Stewart 	/* set the primary address */
547f8829a4aSRandall Stewart 	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
548ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
549ad81507eSRandall Stewart 		    "process_asconf_set_primary: primary address set\n");
550f8829a4aSRandall Stewart 		/* notify upper layer */
551ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
552f8829a4aSRandall Stewart 
553f8829a4aSRandall Stewart 		if (response_required) {
554f8829a4aSRandall Stewart 			m_reply = sctp_asconf_success_response(aph->correlation_id);
555f8829a4aSRandall Stewart 		}
556851b7298SRandall Stewart 		/*
557851b7298SRandall Stewart 		 * Mobility adaptation. Ideally, when the reception of SET
558851b7298SRandall Stewart 		 * PRIMARY with DELETE IP ADDRESS of the previous primary
559851b7298SRandall Stewart 		 * destination, unacknowledged DATA are retransmitted
560851b7298SRandall Stewart 		 * immediately to the new primary destination for seamless
561b5c16493SMichael Tuexen 		 * handover. If the destination is UNCONFIRMED and marked to
562b5c16493SMichael Tuexen 		 * REQ_PRIM, The retransmission occur when reception of the
563b5c16493SMichael Tuexen 		 * HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
564851b7298SRandall Stewart 		 * sctp_input.c) Also, when change of the primary
565851b7298SRandall Stewart 		 * destination, it is better that all subsequent new DATA
566851b7298SRandall Stewart 		 * containing already queued DATA are transmitted to the new
567851b7298SRandall Stewart 		 * primary destination. (by micchie)
568851b7298SRandall Stewart 		 */
569851b7298SRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
570851b7298SRandall Stewart 		    SCTP_MOBILITY_BASE) ||
571851b7298SRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
572851b7298SRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
573851b7298SRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
574851b7298SRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED) &&
575851b7298SRandall Stewart 		    (stcb->asoc.primary_destination->dest_state &
576851b7298SRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) == 0) {
577851b7298SRandall Stewart 
578851b7298SRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
579851b7298SRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
580851b7298SRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
581851b7298SRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
582851b7298SRandall Stewart 				    stcb->asoc.primary_destination);
583851b7298SRandall Stewart 			}
584851b7298SRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
585851b7298SRandall Stewart 			    SCTP_MOBILITY_BASE)) {
5869eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
5879eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
588851b7298SRandall Stewart 			}
589851b7298SRandall Stewart 			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
590851b7298SRandall Stewart 			    stcb->asoc.deleted_primary);
591851b7298SRandall Stewart 		}
592f8829a4aSRandall Stewart 	} else {
593f8829a4aSRandall Stewart 		/* couldn't set the requested primary address! */
594ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
595ad81507eSRandall Stewart 		    "process_asconf_set_primary: set primary failed!\n");
596f8829a4aSRandall Stewart 		/* must have been an invalid address, so report */
597f8829a4aSRandall Stewart 		m_reply = sctp_asconf_error_response(aph->correlation_id,
598f8829a4aSRandall Stewart 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
599f8829a4aSRandall Stewart 		    aparam_length);
600f8829a4aSRandall Stewart 	}
601f8829a4aSRandall Stewart 
602f8829a4aSRandall Stewart 	return m_reply;
603f8829a4aSRandall Stewart }
604f8829a4aSRandall Stewart 
605f8829a4aSRandall Stewart /*
606f8829a4aSRandall Stewart  * handles an ASCONF chunk.
607f8829a4aSRandall Stewart  * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
608f8829a4aSRandall Stewart  */
609f8829a4aSRandall Stewart void
610f8829a4aSRandall Stewart sctp_handle_asconf(struct mbuf *m, unsigned int offset,
6112afb3e84SRandall Stewart     struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
6122afb3e84SRandall Stewart     int first)
613f8829a4aSRandall Stewart {
614f8829a4aSRandall Stewart 	struct sctp_association *asoc;
615f8829a4aSRandall Stewart 	uint32_t serial_num;
6162afb3e84SRandall Stewart 	struct mbuf *n, *m_ack, *m_result, *m_tail;
617f8829a4aSRandall Stewart 	struct sctp_asconf_ack_chunk *ack_cp;
618f8829a4aSRandall Stewart 	struct sctp_asconf_paramhdr *aph, *ack_aph;
619f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param *p_addr;
620f8829a4aSRandall Stewart 	unsigned int asconf_limit;
621f8829a4aSRandall Stewart 	int error = 0;		/* did an error occur? */
622f8829a4aSRandall Stewart 
623f8829a4aSRandall Stewart 	/* asconf param buffer */
624f42a358aSRandall Stewart 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
6252afb3e84SRandall Stewart 	struct sctp_asconf_ack *ack, *ack_next;
626f8829a4aSRandall Stewart 
627f8829a4aSRandall Stewart 	/* verify minimum length */
628f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
629ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
630ad81507eSRandall Stewart 		    "handle_asconf: chunk too small = %xh\n",
631f8829a4aSRandall Stewart 		    ntohs(cp->ch.chunk_length));
632f8829a4aSRandall Stewart 		return;
633f8829a4aSRandall Stewart 	}
634f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
635f8829a4aSRandall Stewart 	serial_num = ntohl(cp->serial_number);
636f8829a4aSRandall Stewart 
63720b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
638f8829a4aSRandall Stewart 		/* got a duplicate ASCONF */
639ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
640ad81507eSRandall Stewart 		    "handle_asconf: got duplicate serial number = %xh\n",
641f8829a4aSRandall Stewart 		    serial_num);
642f8829a4aSRandall Stewart 		return;
643f8829a4aSRandall Stewart 	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
644ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
645f8829a4aSRandall Stewart 		    serial_num, asoc->asconf_seq_in + 1);
646f8829a4aSRandall Stewart 		return;
647f8829a4aSRandall Stewart 	}
648f8829a4aSRandall Stewart 	/* it's the expected "next" sequence number, so process it */
649f8829a4aSRandall Stewart 	asoc->asconf_seq_in = serial_num;	/* update sequence */
650f8829a4aSRandall Stewart 	/* get length of all the param's in the ASCONF */
651f8829a4aSRandall Stewart 	asconf_limit = offset + ntohs(cp->ch.chunk_length);
652ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF1,
653ad81507eSRandall Stewart 	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
654f8829a4aSRandall Stewart 	    asconf_limit, serial_num);
6552afb3e84SRandall Stewart 
6562afb3e84SRandall Stewart 	if (first) {
6572afb3e84SRandall Stewart 		/* delete old cache */
6582afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: Now processing firstASCONF. Try to delte old cache\n");
6592afb3e84SRandall Stewart 
6604a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
6612afb3e84SRandall Stewart 			if (ack->serial_number == serial_num)
6622afb3e84SRandall Stewart 				break;
6632afb3e84SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: delete old(%u) < first(%u)\n",
6642afb3e84SRandall Stewart 			    ack->serial_number, serial_num);
6654a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
6662afb3e84SRandall Stewart 			if (ack->data != NULL) {
6672afb3e84SRandall Stewart 				sctp_m_freem(ack->data);
6682afb3e84SRandall Stewart 			}
669b3f1ea41SRandall Stewart 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
6702afb3e84SRandall Stewart 		}
671f8829a4aSRandall Stewart 	}
672139bc87fSRandall Stewart 	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
673f8829a4aSRandall Stewart 	    M_DONTWAIT, 1, MT_DATA);
674f8829a4aSRandall Stewart 	if (m_ack == NULL) {
675ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
676ad81507eSRandall Stewart 		    "handle_asconf: couldn't get mbuf!\n");
677f8829a4aSRandall Stewart 		return;
678f8829a4aSRandall Stewart 	}
679f8829a4aSRandall Stewart 	m_tail = m_ack;		/* current reply chain's tail */
680f8829a4aSRandall Stewart 
681f8829a4aSRandall Stewart 	/* fill in ASCONF-ACK header */
682f8829a4aSRandall Stewart 	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
683f8829a4aSRandall Stewart 	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
684f8829a4aSRandall Stewart 	ack_cp->ch.chunk_flags = 0;
685f8829a4aSRandall Stewart 	ack_cp->serial_number = htonl(serial_num);
686f8829a4aSRandall Stewart 	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
687139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
688f8829a4aSRandall Stewart 	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
689f8829a4aSRandall Stewart 
690f8829a4aSRandall Stewart 	/* skip the lookup address parameter */
691f8829a4aSRandall Stewart 	offset += sizeof(struct sctp_asconf_chunk);
692f8829a4aSRandall Stewart 	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
693f8829a4aSRandall Stewart 	if (p_addr == NULL) {
694ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
695ad81507eSRandall Stewart 		    "handle_asconf: couldn't get lookup addr!\n");
696f8829a4aSRandall Stewart 		/* respond with a missing/invalid mandatory parameter error */
697f8829a4aSRandall Stewart 		return;
698f8829a4aSRandall Stewart 	}
699f8829a4aSRandall Stewart 	/* param_length is already validated in process_control... */
700f8829a4aSRandall Stewart 	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
701f8829a4aSRandall Stewart 
702f8829a4aSRandall Stewart 	/* get pointer to first asconf param in ASCONF-ACK */
703f8829a4aSRandall Stewart 	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
704f8829a4aSRandall Stewart 	if (ack_aph == NULL) {
705ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Gak in asconf2\n");
706f8829a4aSRandall Stewart 		return;
707f8829a4aSRandall Stewart 	}
708f8829a4aSRandall Stewart 	/* get pointer to first asconf param in ASCONF */
709f8829a4aSRandall Stewart 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
710f8829a4aSRandall Stewart 	if (aph == NULL) {
711ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
712f8829a4aSRandall Stewart 		goto send_reply;
713f8829a4aSRandall Stewart 	}
714f8829a4aSRandall Stewart 	/* process through all parameters */
715f8829a4aSRandall Stewart 	while (aph != NULL) {
716f8829a4aSRandall Stewart 		unsigned int param_length, param_type;
717f8829a4aSRandall Stewart 
718f8829a4aSRandall Stewart 		param_type = ntohs(aph->ph.param_type);
719f8829a4aSRandall Stewart 		param_length = ntohs(aph->ph.param_length);
720f8829a4aSRandall Stewart 		if (offset + param_length > asconf_limit) {
721f8829a4aSRandall Stewart 			/* parameter goes beyond end of chunk! */
722f8829a4aSRandall Stewart 			sctp_m_freem(m_ack);
723f8829a4aSRandall Stewart 			return;
724f8829a4aSRandall Stewart 		}
725f8829a4aSRandall Stewart 		m_result = NULL;
726f8829a4aSRandall Stewart 
727f8829a4aSRandall Stewart 		if (param_length > sizeof(aparam_buf)) {
728ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
729f8829a4aSRandall Stewart 			sctp_m_freem(m_ack);
730f8829a4aSRandall Stewart 			return;
731f8829a4aSRandall Stewart 		}
732f8829a4aSRandall Stewart 		if (param_length <= sizeof(struct sctp_paramhdr)) {
733ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
734f8829a4aSRandall Stewart 			sctp_m_freem(m_ack);
735f8829a4aSRandall Stewart 		}
736f8829a4aSRandall Stewart 		/* get the entire parameter */
737f8829a4aSRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
738f8829a4aSRandall Stewart 		if (aph == NULL) {
739ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
740f8829a4aSRandall Stewart 			sctp_m_freem(m_ack);
741f8829a4aSRandall Stewart 			return;
742f8829a4aSRandall Stewart 		}
743f8829a4aSRandall Stewart 		switch (param_type) {
744f8829a4aSRandall Stewart 		case SCTP_ADD_IP_ADDRESS:
745f8829a4aSRandall Stewart 			asoc->peer_supports_asconf = 1;
746f8829a4aSRandall Stewart 			m_result = sctp_process_asconf_add_ip(m, aph, stcb,
747f8829a4aSRandall Stewart 			    error);
748f8829a4aSRandall Stewart 			break;
749f8829a4aSRandall Stewart 		case SCTP_DEL_IP_ADDRESS:
750f8829a4aSRandall Stewart 			asoc->peer_supports_asconf = 1;
751f8829a4aSRandall Stewart 			m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
752f8829a4aSRandall Stewart 			    error);
753f8829a4aSRandall Stewart 			break;
754f8829a4aSRandall Stewart 		case SCTP_ERROR_CAUSE_IND:
755f8829a4aSRandall Stewart 			/* not valid in an ASCONF chunk */
756f8829a4aSRandall Stewart 			break;
757f8829a4aSRandall Stewart 		case SCTP_SET_PRIM_ADDR:
758f8829a4aSRandall Stewart 			asoc->peer_supports_asconf = 1;
759f8829a4aSRandall Stewart 			m_result = sctp_process_asconf_set_primary(m, aph,
760f8829a4aSRandall Stewart 			    stcb, error);
761f8829a4aSRandall Stewart 			break;
762830d754dSRandall Stewart 		case SCTP_NAT_VTAGS:
763830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
764830d754dSRandall Stewart 			break;
765f8829a4aSRandall Stewart 		case SCTP_SUCCESS_REPORT:
766f8829a4aSRandall Stewart 			/* not valid in an ASCONF chunk */
767f8829a4aSRandall Stewart 			break;
768f8829a4aSRandall Stewart 		case SCTP_ULP_ADAPTATION:
769f8829a4aSRandall Stewart 			/* FIX */
770f8829a4aSRandall Stewart 			break;
771f8829a4aSRandall Stewart 		default:
772f8829a4aSRandall Stewart 			if ((param_type & 0x8000) == 0) {
773f8829a4aSRandall Stewart 				/* Been told to STOP at this param */
774f8829a4aSRandall Stewart 				asconf_limit = offset;
775f8829a4aSRandall Stewart 				/*
776f8829a4aSRandall Stewart 				 * FIX FIX - We need to call
777f8829a4aSRandall Stewart 				 * sctp_arethere_unrecognized_parameters()
778f8829a4aSRandall Stewart 				 * to get a operr and send it for any
779f8829a4aSRandall Stewart 				 * param's with the 0x4000 bit set OR do it
780f8829a4aSRandall Stewart 				 * here ourselves... note we still must STOP
781f8829a4aSRandall Stewart 				 * if the 0x8000 bit is clear.
782f8829a4aSRandall Stewart 				 */
783f8829a4aSRandall Stewart 			}
784f8829a4aSRandall Stewart 			/* unknown/invalid param type */
785f8829a4aSRandall Stewart 			break;
786f8829a4aSRandall Stewart 		}		/* switch */
787f8829a4aSRandall Stewart 
788f8829a4aSRandall Stewart 		/* add any (error) result to the reply mbuf chain */
789f8829a4aSRandall Stewart 		if (m_result != NULL) {
790139bc87fSRandall Stewart 			SCTP_BUF_NEXT(m_tail) = m_result;
791f8829a4aSRandall Stewart 			m_tail = m_result;
792f8829a4aSRandall Stewart 			/* update lengths, make sure it's aligned too */
793139bc87fSRandall Stewart 			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
794139bc87fSRandall Stewart 			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
795f8829a4aSRandall Stewart 			/* set flag to force success reports */
796f8829a4aSRandall Stewart 			error = 1;
797f8829a4aSRandall Stewart 		}
798f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(param_length);
799f8829a4aSRandall Stewart 		/* update remaining ASCONF message length to process */
800f8829a4aSRandall Stewart 		if (offset >= asconf_limit) {
801f8829a4aSRandall Stewart 			/* no more data in the mbuf chain */
802f8829a4aSRandall Stewart 			break;
803f8829a4aSRandall Stewart 		}
804f8829a4aSRandall Stewart 		/* get pointer to next asconf param */
805f8829a4aSRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
806f8829a4aSRandall Stewart 		    sizeof(struct sctp_asconf_paramhdr),
807f8829a4aSRandall Stewart 		    (uint8_t *) & aparam_buf);
808f8829a4aSRandall Stewart 		if (aph == NULL) {
809f8829a4aSRandall Stewart 			/* can't get an asconf paramhdr */
810ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
811f8829a4aSRandall Stewart 			/* FIX ME - add error here... */
812f8829a4aSRandall Stewart 		}
813ad81507eSRandall Stewart 	}
814f8829a4aSRandall Stewart 
815f8829a4aSRandall Stewart send_reply:
816f8829a4aSRandall Stewart 	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
817f8829a4aSRandall Stewart 	/* save the ASCONF-ACK reply */
818b3f1ea41SRandall Stewart 	ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
8192afb3e84SRandall Stewart 	    struct sctp_asconf_ack);
8202afb3e84SRandall Stewart 	if (ack == NULL) {
8212afb3e84SRandall Stewart 		sctp_m_freem(m_ack);
8222afb3e84SRandall Stewart 		return;
8232afb3e84SRandall Stewart 	}
8242afb3e84SRandall Stewart 	ack->serial_number = serial_num;
8252afb3e84SRandall Stewart 	ack->last_sent_to = NULL;
8262afb3e84SRandall Stewart 	ack->data = m_ack;
827fc066a61SMichael Tuexen 	ack->len = 0;
8282afb3e84SRandall Stewart 	n = m_ack;
8292afb3e84SRandall Stewart 	while (n) {
8302afb3e84SRandall Stewart 		ack->len += SCTP_BUF_LEN(n);
8312afb3e84SRandall Stewart 		n = SCTP_BUF_NEXT(n);
8322afb3e84SRandall Stewart 	}
8332afb3e84SRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
834f8829a4aSRandall Stewart 
835f8829a4aSRandall Stewart 	/* see if last_control_chunk_from is set properly (use IP src addr) */
836f8829a4aSRandall Stewart 	if (stcb->asoc.last_control_chunk_from == NULL) {
837f8829a4aSRandall Stewart 		/*
838f8829a4aSRandall Stewart 		 * this could happen if the source address was just newly
839f8829a4aSRandall Stewart 		 * added
840f8829a4aSRandall Stewart 		 */
841f8829a4aSRandall Stewart 		struct ip *iph;
842f8829a4aSRandall Stewart 		struct sctphdr *sh;
843f8829a4aSRandall Stewart 		struct sockaddr_storage from_store;
844f8829a4aSRandall Stewart 		struct sockaddr *from = (struct sockaddr *)&from_store;
845f8829a4aSRandall Stewart 
846ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
847f8829a4aSRandall Stewart 		/* pullup already done, IP options already stripped */
848f8829a4aSRandall Stewart 		iph = mtod(m, struct ip *);
849f8829a4aSRandall Stewart 		sh = (struct sctphdr *)((caddr_t)iph + sizeof(*iph));
8505e2c2d87SRandall Stewart 		switch (iph->ip_v) {
8515e2c2d87SRandall Stewart 		case IPVERSION:
8525e2c2d87SRandall Stewart 			{
853f8829a4aSRandall Stewart 				struct sockaddr_in *from4;
854f8829a4aSRandall Stewart 
855f8829a4aSRandall Stewart 				from4 = (struct sockaddr_in *)&from_store;
856f8829a4aSRandall Stewart 				bzero(from4, sizeof(*from4));
857f8829a4aSRandall Stewart 				from4->sin_family = AF_INET;
858f8829a4aSRandall Stewart 				from4->sin_len = sizeof(struct sockaddr_in);
859f8829a4aSRandall Stewart 				from4->sin_addr.s_addr = iph->ip_src.s_addr;
860f8829a4aSRandall Stewart 				from4->sin_port = sh->src_port;
8615e2c2d87SRandall Stewart 				break;
8625e2c2d87SRandall Stewart 			}
8635e2c2d87SRandall Stewart #ifdef INET6
8645e2c2d87SRandall Stewart 		case IPV6_VERSION >> 4:
8655e2c2d87SRandall Stewart 			{
866f8829a4aSRandall Stewart 				struct ip6_hdr *ip6;
867f8829a4aSRandall Stewart 				struct sockaddr_in6 *from6;
868f8829a4aSRandall Stewart 
869f8829a4aSRandall Stewart 				ip6 = mtod(m, struct ip6_hdr *);
870f8829a4aSRandall Stewart 				from6 = (struct sockaddr_in6 *)&from_store;
871f8829a4aSRandall Stewart 				bzero(from6, sizeof(*from6));
872f8829a4aSRandall Stewart 				from6->sin6_family = AF_INET6;
873f8829a4aSRandall Stewart 				from6->sin6_len = sizeof(struct sockaddr_in6);
874f8829a4aSRandall Stewart 				from6->sin6_addr = ip6->ip6_src;
875f8829a4aSRandall Stewart 				from6->sin6_port = sh->src_port;
8765e2c2d87SRandall Stewart 				/*
8775e2c2d87SRandall Stewart 				 * Get the scopes in properly to the sin6
8785e2c2d87SRandall Stewart 				 * addr's
8795e2c2d87SRandall Stewart 				 */
880f8829a4aSRandall Stewart 				/* we probably don't need these operations */
881f8829a4aSRandall Stewart 				(void)sa6_recoverscope(from6);
882fc14de76SRandall Stewart 				sa6_embedscope(from6,
883482444b4SRandall Stewart 				    MODULE_GLOBAL(ip6_use_defzone));
884fc14de76SRandall Stewart 
8855e2c2d87SRandall Stewart 				break;
8865e2c2d87SRandall Stewart 			}
8875e2c2d87SRandall Stewart #endif
8885e2c2d87SRandall Stewart 		default:
889f8829a4aSRandall Stewart 			/* unknown address type */
890f8829a4aSRandall Stewart 			from = NULL;
891f8829a4aSRandall Stewart 		}
892f8829a4aSRandall Stewart 		if (from != NULL) {
893ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
894ad81507eSRandall Stewart 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, from);
895f8829a4aSRandall Stewart 			/* look up the from address */
896f8829a4aSRandall Stewart 			stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, from);
897f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
898ad81507eSRandall Stewart 			if (stcb->asoc.last_control_chunk_from == NULL)
899ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
900ad81507eSRandall Stewart #endif
901f8829a4aSRandall Stewart 		}
902f8829a4aSRandall Stewart 	}
903f8829a4aSRandall Stewart }
904f8829a4aSRandall Stewart 
905f8829a4aSRandall Stewart /*
906f8829a4aSRandall Stewart  * does the address match? returns 0 if not, 1 if so
907f8829a4aSRandall Stewart  */
908f8829a4aSRandall Stewart static uint32_t
909f8829a4aSRandall Stewart sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
910f8829a4aSRandall Stewart {
911f8829a4aSRandall Stewart #ifdef INET6
912f8829a4aSRandall Stewart 	if (sa->sa_family == AF_INET6) {
913f8829a4aSRandall Stewart 		/* IPv6 sa address */
914f8829a4aSRandall Stewart 		/* XXX scopeid */
915f8829a4aSRandall Stewart 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
916f8829a4aSRandall Stewart 
917f8829a4aSRandall Stewart 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
918f8829a4aSRandall Stewart 		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
919f8829a4aSRandall Stewart 		    sizeof(struct in6_addr)) == 0)) {
920f8829a4aSRandall Stewart 			return (1);
921f8829a4aSRandall Stewart 		}
922f8829a4aSRandall Stewart 	} else
923f8829a4aSRandall Stewart #endif				/* INET6 */
924f8829a4aSRandall Stewart 	if (sa->sa_family == AF_INET) {
925f8829a4aSRandall Stewart 		/* IPv4 sa address */
926f8829a4aSRandall Stewart 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
927f8829a4aSRandall Stewart 
928f8829a4aSRandall Stewart 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
929f8829a4aSRandall Stewart 		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
930f8829a4aSRandall Stewart 		    sizeof(struct in_addr)) == 0)) {
931f8829a4aSRandall Stewart 			return (1);
932f8829a4aSRandall Stewart 		}
933f8829a4aSRandall Stewart 	}
934f8829a4aSRandall Stewart 	return (0);
935f8829a4aSRandall Stewart }
936f8829a4aSRandall Stewart 
937f8829a4aSRandall Stewart /*
938c54a18d2SRandall Stewart  * does the address match? returns 0 if not, 1 if so
939c54a18d2SRandall Stewart  */
940c54a18d2SRandall Stewart static uint32_t
941c54a18d2SRandall Stewart sctp_addr_match(
942c54a18d2SRandall Stewart     struct sctp_ipv6addr_param *v6addr,
943c54a18d2SRandall Stewart     struct sockaddr *sa)
944c54a18d2SRandall Stewart {
945c54a18d2SRandall Stewart 	uint16_t param_type, param_length;
946c54a18d2SRandall Stewart 	struct sctp_ipv4addr_param *v4addr = (struct sctp_ipv4addr_param *)v6addr;
947c54a18d2SRandall Stewart 
948d6af161aSRandall Stewart #ifdef INET6
949c54a18d2SRandall Stewart 	if (sa->sa_family == AF_INET6) {
950c54a18d2SRandall Stewart 		/* IPv6 sa address */
951c54a18d2SRandall Stewart 		/* XXX scopeid */
952c54a18d2SRandall Stewart 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
953c54a18d2SRandall Stewart 
954c54a18d2SRandall Stewart 		param_type = ntohs(v6addr->ph.param_type);
955c54a18d2SRandall Stewart 		param_length = ntohs(v6addr->ph.param_length);
956c54a18d2SRandall Stewart 
957c54a18d2SRandall Stewart 		if ((param_type == SCTP_IPV6_ADDRESS) &&
958c54a18d2SRandall Stewart 		    param_length == sizeof(struct sctp_ipv6addr_param) &&
959c54a18d2SRandall Stewart 		    (memcmp(&v6addr->addr, &sin6->sin6_addr,
960c54a18d2SRandall Stewart 		    sizeof(struct in6_addr)) == 0)) {
961c54a18d2SRandall Stewart 			return (1);
962c54a18d2SRandall Stewart 		}
963d6af161aSRandall Stewart 	}
964d6af161aSRandall Stewart #endif
965c54a18d2SRandall Stewart 	if (sa->sa_family == AF_INET) {
966c54a18d2SRandall Stewart 		/* IPv4 sa address */
967c54a18d2SRandall Stewart 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
968c54a18d2SRandall Stewart 
969c54a18d2SRandall Stewart 		param_type = ntohs(v4addr->ph.param_type);
970c54a18d2SRandall Stewart 		param_length = ntohs(v4addr->ph.param_length);
971c54a18d2SRandall Stewart 
972c54a18d2SRandall Stewart 		if ((param_type == SCTP_IPV4_ADDRESS) &&
973c54a18d2SRandall Stewart 		    param_length == sizeof(struct sctp_ipv4addr_param) &&
974c54a18d2SRandall Stewart 		    (memcmp(&v4addr->addr, &sin->sin_addr,
975c54a18d2SRandall Stewart 		    sizeof(struct in_addr)) == 0)) {
976c54a18d2SRandall Stewart 			return (1);
977c54a18d2SRandall Stewart 		}
978c54a18d2SRandall Stewart 	}
979c54a18d2SRandall Stewart 	return (0);
980c54a18d2SRandall Stewart }
981c54a18d2SRandall Stewart 
982c54a18d2SRandall Stewart /*
983f8829a4aSRandall Stewart  * Cleanup for non-responded/OP ERR'd ASCONF
984f8829a4aSRandall Stewart  */
985f8829a4aSRandall Stewart void
986f8829a4aSRandall Stewart sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
987f8829a4aSRandall Stewart {
988f8829a4aSRandall Stewart 	/* mark peer as ASCONF incapable */
989f8829a4aSRandall Stewart 	stcb->asoc.peer_supports_asconf = 0;
990f8829a4aSRandall Stewart 	/*
991f8829a4aSRandall Stewart 	 * clear out any existing asconfs going out
992f8829a4aSRandall Stewart 	 */
9933c503c28SRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
9943c503c28SRandall Stewart 	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
995c54a18d2SRandall Stewart 	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
996f8829a4aSRandall Stewart 	/* remove the old ASCONF on our outbound queue */
997f8829a4aSRandall Stewart 	sctp_toss_old_asconf(stcb);
998f8829a4aSRandall Stewart }
999f8829a4aSRandall Stewart 
1000f8829a4aSRandall Stewart /*
10012dad8a55SRandall Stewart  * cleanup any cached source addresses that may be topologically
10022dad8a55SRandall Stewart  * incorrect after a new address has been added to this interface.
10032dad8a55SRandall Stewart  */
10042dad8a55SRandall Stewart static void
10052dad8a55SRandall Stewart sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
10062dad8a55SRandall Stewart {
10072dad8a55SRandall Stewart 	struct sctp_nets *net;
10082dad8a55SRandall Stewart 
10092dad8a55SRandall Stewart 	/*
10102dad8a55SRandall Stewart 	 * Ideally, we want to only clear cached routes and source addresses
10112dad8a55SRandall Stewart 	 * that are topologically incorrect.  But since there is no easy way
10122dad8a55SRandall Stewart 	 * to know whether the newly added address on the ifn would cause a
10132dad8a55SRandall Stewart 	 * routing change (i.e. a new egress interface would be chosen)
10142dad8a55SRandall Stewart 	 * without doing a new routing lookup and source address selection,
10152dad8a55SRandall Stewart 	 * we will (for now) just flush any cached route using a different
10162dad8a55SRandall Stewart 	 * ifn (and cached source addrs) and let output re-choose them
10172dad8a55SRandall Stewart 	 * during the next send on that net.
10182dad8a55SRandall Stewart 	 */
10192dad8a55SRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10202dad8a55SRandall Stewart 		/*
10212dad8a55SRandall Stewart 		 * clear any cached route (and cached source address) if the
10222dad8a55SRandall Stewart 		 * route's interface is NOT the same as the address change.
10232dad8a55SRandall Stewart 		 * If it's the same interface, just clear the cached source
10242dad8a55SRandall Stewart 		 * address.
10252dad8a55SRandall Stewart 		 */
10262dad8a55SRandall Stewart 		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
1027fc066a61SMichael Tuexen 		    ((ifn == NULL) ||
1028fc066a61SMichael Tuexen 		    (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
10292dad8a55SRandall Stewart 			/* clear any cached route */
10302dad8a55SRandall Stewart 			RTFREE(net->ro.ro_rt);
10312dad8a55SRandall Stewart 			net->ro.ro_rt = NULL;
10322dad8a55SRandall Stewart 		}
10332dad8a55SRandall Stewart 		/* clear any cached source address */
10342dad8a55SRandall Stewart 		if (net->src_addr_selected) {
10352dad8a55SRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
10362dad8a55SRandall Stewart 			net->ro._s_addr = NULL;
10372dad8a55SRandall Stewart 			net->src_addr_selected = 0;
10382dad8a55SRandall Stewart 		}
10392dad8a55SRandall Stewart 	}
10402dad8a55SRandall Stewart }
10412dad8a55SRandall Stewart 
1042851b7298SRandall Stewart 
1043851b7298SRandall Stewart void
1044851b7298SRandall Stewart sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
1045851b7298SRandall Stewart {
1046851b7298SRandall Stewart 	int error;
1047851b7298SRandall Stewart 
1048851b7298SRandall Stewart 	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
1049851b7298SRandall Stewart 		return;
1050851b7298SRandall Stewart 	}
1051851b7298SRandall Stewart 	if (stcb->asoc.deleted_primary == NULL) {
1052851b7298SRandall Stewart 		return;
1053851b7298SRandall Stewart 	}
1054851b7298SRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
1055b27a6b7dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
1056851b7298SRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1057851b7298SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
1058851b7298SRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
1059851b7298SRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1060851b7298SRandall Stewart 		    stcb->asoc.deleted_primary,
1061851b7298SRandall Stewart 		    SCTP_FROM_SCTP_TIMER + SCTP_LOC_8);
1062851b7298SRandall Stewart 		stcb->asoc.num_send_timers_up--;
1063851b7298SRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1064851b7298SRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1065851b7298SRandall Stewart 		}
1066851b7298SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1067851b7298SRandall Stewart 		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1068851b7298SRandall Stewart 		    stcb->asoc.deleted_primary);
1069851b7298SRandall Stewart 		if (error) {
1070851b7298SRandall Stewart 			SCTP_INP_DECR_REF(stcb->sctp_ep);
1071851b7298SRandall Stewart 			return;
1072851b7298SRandall Stewart 		}
1073851b7298SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1074851b7298SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1075f31e6c7fSMichael Tuexen 		sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
1076851b7298SRandall Stewart #endif
1077851b7298SRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1078851b7298SRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
1079851b7298SRandall Stewart 		    (stcb->asoc.sent_queue_cnt > 0)) {
1080851b7298SRandall Stewart 			struct sctp_tmit_chunk *chk;
1081851b7298SRandall Stewart 
1082851b7298SRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1083851b7298SRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
1084851b7298SRandall Stewart 			    stcb, chk->whoTo);
1085851b7298SRandall Stewart 		}
1086851b7298SRandall Stewart 	}
1087851b7298SRandall Stewart 	return;
1088851b7298SRandall Stewart }
1089851b7298SRandall Stewart 
10902afb3e84SRandall Stewart static int
10912afb3e84SRandall Stewart     sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
10922afb3e84SRandall Stewart 
1093851b7298SRandall Stewart void
10942afb3e84SRandall Stewart sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
10952afb3e84SRandall Stewart {
10962afb3e84SRandall Stewart 	struct sctp_tmit_chunk *chk;
10972afb3e84SRandall Stewart 
1098b27a6b7dSRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
10992afb3e84SRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
11002afb3e84SRandall Stewart 	    SCTP_FROM_SCTP_TIMER + SCTP_LOC_5);
11012afb3e84SRandall Stewart 	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
11022afb3e84SRandall Stewart 	net->error_count = 0;
11032afb3e84SRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
11042afb3e84SRandall Stewart 		if (chk->whoTo == net) {
1105851b7298SRandall Stewart 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
11062afb3e84SRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
11072afb3e84SRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1108d55b0b1bSRandall Stewart 				sctp_flight_size_decrease(chk);
1109d55b0b1bSRandall Stewart 				sctp_total_flight_decrease(stcb, chk);
1110d55b0b1bSRandall Stewart 				net->marked_retrans++;
1111d55b0b1bSRandall Stewart 				stcb->asoc.marked_retrans++;
11122afb3e84SRandall Stewart 			}
11132afb3e84SRandall Stewart 		}
11142afb3e84SRandall Stewart 	}
1115d55b0b1bSRandall Stewart 	if (net->marked_retrans) {
1116d55b0b1bSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1117d55b0b1bSRandall Stewart 	}
1118851b7298SRandall Stewart }
11192afb3e84SRandall Stewart 
11202afb3e84SRandall Stewart static void
11212afb3e84SRandall Stewart sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
11222afb3e84SRandall Stewart {
11232afb3e84SRandall Stewart 	struct sctp_nets *net;
11242afb3e84SRandall Stewart 	int addrnum, changed;
11252afb3e84SRandall Stewart 
11262afb3e84SRandall Stewart 	/*
11272afb3e84SRandall Stewart 	 * If number of local valid addresses is 1, the valid address is
11282afb3e84SRandall Stewart 	 * probably newly added address. Several valid addresses in this
11292afb3e84SRandall Stewart 	 * association.  A source address may not be changed.  Additionally,
11302afb3e84SRandall Stewart 	 * they can be configured on a same interface as "alias" addresses.
11312afb3e84SRandall Stewart 	 * (by micchie)
11322afb3e84SRandall Stewart 	 */
11332afb3e84SRandall Stewart 	addrnum = sctp_local_addr_count(stcb);
11342afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
11352afb3e84SRandall Stewart 	    addrnum);
11362afb3e84SRandall Stewart 	if (addrnum == 1) {
11372afb3e84SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
11382afb3e84SRandall Stewart 			/* clear any cached route and source address */
11392afb3e84SRandall Stewart 			if (net->ro.ro_rt) {
11402afb3e84SRandall Stewart 				RTFREE(net->ro.ro_rt);
11412afb3e84SRandall Stewart 				net->ro.ro_rt = NULL;
11422afb3e84SRandall Stewart 			}
11432afb3e84SRandall Stewart 			if (net->src_addr_selected) {
11442afb3e84SRandall Stewart 				sctp_free_ifa(net->ro._s_addr);
11452afb3e84SRandall Stewart 				net->ro._s_addr = NULL;
11462afb3e84SRandall Stewart 				net->src_addr_selected = 0;
11472afb3e84SRandall Stewart 			}
11482afb3e84SRandall Stewart 			/* Retransmit unacknowledged DATA chunks immediately */
11492afb3e84SRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
11502afb3e84SRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
11512afb3e84SRandall Stewart 				sctp_net_immediate_retrans(stcb, net);
11522afb3e84SRandall Stewart 			}
11532afb3e84SRandall Stewart 			/* also, SET PRIMARY is maybe already sent */
11542afb3e84SRandall Stewart 		}
11552afb3e84SRandall Stewart 		return;
11562afb3e84SRandall Stewart 	}
11572afb3e84SRandall Stewart 	/* Multiple local addresses exsist in the association.  */
11582afb3e84SRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
11592afb3e84SRandall Stewart 		/* clear any cached route and source address */
11602afb3e84SRandall Stewart 		if (net->ro.ro_rt) {
11612afb3e84SRandall Stewart 			RTFREE(net->ro.ro_rt);
11622afb3e84SRandall Stewart 			net->ro.ro_rt = NULL;
11632afb3e84SRandall Stewart 		}
11642afb3e84SRandall Stewart 		if (net->src_addr_selected) {
11652afb3e84SRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
11662afb3e84SRandall Stewart 			net->ro._s_addr = NULL;
11672afb3e84SRandall Stewart 			net->src_addr_selected = 0;
11682afb3e84SRandall Stewart 		}
11692afb3e84SRandall Stewart 		/*
11702afb3e84SRandall Stewart 		 * Check if the nexthop is corresponding to the new address.
11712afb3e84SRandall Stewart 		 * If the new address is corresponding to the current
11722afb3e84SRandall Stewart 		 * nexthop, the path will be changed. If the new address is
11732afb3e84SRandall Stewart 		 * NOT corresponding to the current nexthop, the path will
11742afb3e84SRandall Stewart 		 * not be changed.
11752afb3e84SRandall Stewart 		 */
11762afb3e84SRandall Stewart 		SCTP_RTALLOC((sctp_route_t *) & net->ro,
11772afb3e84SRandall Stewart 		    stcb->sctp_ep->def_vrf_id);
11782afb3e84SRandall Stewart 		if (net->ro.ro_rt == NULL)
11792afb3e84SRandall Stewart 			continue;
11802afb3e84SRandall Stewart 
11812afb3e84SRandall Stewart 		changed = 0;
11822afb3e84SRandall Stewart 		if (net->ro._l_addr.sa.sa_family == AF_INET) {
11832afb3e84SRandall Stewart 			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *) & net->ro))
11842afb3e84SRandall Stewart 				changed = 1;
11852afb3e84SRandall Stewart 		}
11865e2c2d87SRandall Stewart #ifdef INET6
11872afb3e84SRandall Stewart 		if (net->ro._l_addr.sa.sa_family == AF_INET6) {
11882afb3e84SRandall Stewart 			if (sctp_v6src_match_nexthop(
11892afb3e84SRandall Stewart 			    &newifa->address.sin6, (sctp_route_t *) & net->ro))
11902afb3e84SRandall Stewart 				changed = 1;
11912afb3e84SRandall Stewart 		}
11925e2c2d87SRandall Stewart #endif
11932afb3e84SRandall Stewart 		/*
11942afb3e84SRandall Stewart 		 * if the newly added address does not relate routing
11952afb3e84SRandall Stewart 		 * information, we skip.
11962afb3e84SRandall Stewart 		 */
11972afb3e84SRandall Stewart 		if (changed == 0)
11982afb3e84SRandall Stewart 			continue;
11992afb3e84SRandall Stewart 		/* Retransmit unacknowledged DATA chunks immediately */
12002afb3e84SRandall Stewart 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
12012afb3e84SRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) {
12022afb3e84SRandall Stewart 			sctp_net_immediate_retrans(stcb, net);
12032afb3e84SRandall Stewart 		}
12042afb3e84SRandall Stewart 		/* Send SET PRIMARY for this new address */
12052afb3e84SRandall Stewart 		if (net == stcb->asoc.primary_destination) {
12062afb3e84SRandall Stewart 			(void)sctp_asconf_queue_mgmt(stcb, newifa,
12072afb3e84SRandall Stewart 			    SCTP_SET_PRIM_ADDR);
12082afb3e84SRandall Stewart 		}
12092afb3e84SRandall Stewart 	}
12102afb3e84SRandall Stewart }
12112afb3e84SRandall Stewart 
12122dad8a55SRandall Stewart /*
1213f8829a4aSRandall Stewart  * process an ADD/DELETE IP ack from peer.
12141b649582SRandall Stewart  * addr: corresponding sctp_ifa to the address being added/deleted.
1215f8829a4aSRandall Stewart  * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1216f8829a4aSRandall Stewart  * flag: 1=success, 0=failure.
1217f8829a4aSRandall Stewart  */
1218f8829a4aSRandall Stewart static void
121942551e99SRandall Stewart sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr,
1220f8829a4aSRandall Stewart     uint16_t type, uint32_t flag)
1221f8829a4aSRandall Stewart {
1222f8829a4aSRandall Stewart 	/*
1223f8829a4aSRandall Stewart 	 * do the necessary asoc list work- if we get a failure indication,
12242dad8a55SRandall Stewart 	 * leave the address on the assoc's restricted list.  If we get a
12252dad8a55SRandall Stewart 	 * success indication, remove the address from the restricted list.
1226f8829a4aSRandall Stewart 	 */
1227f8829a4aSRandall Stewart 	/*
1228f8829a4aSRandall Stewart 	 * Note: this will only occur for ADD_IP_ADDRESS, since
1229f8829a4aSRandall Stewart 	 * DEL_IP_ADDRESS is never actually added to the list...
1230f8829a4aSRandall Stewart 	 */
1231f8829a4aSRandall Stewart 	if (flag) {
12321b649582SRandall Stewart 		/* success case, so remove from the restricted list */
12331b649582SRandall Stewart 		sctp_del_local_addr_restricted(stcb, addr);
12342dad8a55SRandall Stewart 
12353232788eSRandall Stewart 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1236d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
1237d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1238d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) {
12392afb3e84SRandall Stewart 			sctp_path_check_and_react(stcb, addr);
12402afb3e84SRandall Stewart 			return;
12412afb3e84SRandall Stewart 		}
12423232788eSRandall Stewart 		/* clear any cached/topologically incorrect source addresses */
12432dad8a55SRandall Stewart 		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1244f8829a4aSRandall Stewart 	}
1245f8829a4aSRandall Stewart 	/* else, leave it on the list */
1246f8829a4aSRandall Stewart }
1247f8829a4aSRandall Stewart 
1248f8829a4aSRandall Stewart /*
12491b649582SRandall Stewart  * add an asconf add/delete/set primary IP address parameter to the queue.
1250f8829a4aSRandall Stewart  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
12511b649582SRandall Stewart  * returns 0 if queued, -1 if not queued/removed.
12521b649582SRandall Stewart  * NOTE: if adding, but a delete for the same address is already scheduled
12531b649582SRandall Stewart  * (and not yet sent out), simply remove it from queue.  Same for deleting
12541b649582SRandall Stewart  * an address already scheduled for add.  If a duplicate operation is found,
12551b649582SRandall Stewart  * ignore the new one.
1256f8829a4aSRandall Stewart  */
12571b649582SRandall Stewart static int
12581b649582SRandall Stewart sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
125918e198d3SRandall Stewart     uint16_t type)
1260f8829a4aSRandall Stewart {
1261f8829a4aSRandall Stewart 	struct sctp_asconf_addr *aa, *aa_next;
1262f8829a4aSRandall Stewart 	struct sockaddr *sa;
1263f8829a4aSRandall Stewart 
1264f8829a4aSRandall Stewart 	/* make sure the request isn't already in the queue */
12654a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1266f8829a4aSRandall Stewart 		/* address match? */
126742551e99SRandall Stewart 		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1268f8829a4aSRandall Stewart 			continue;
1269c54a18d2SRandall Stewart 		/*
1270c54a18d2SRandall Stewart 		 * is the request already in queue but not sent? pass the
1271c54a18d2SRandall Stewart 		 * request already sent in order to resolve the following
1272c54a18d2SRandall Stewart 		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1273c54a18d2SRandall Stewart 		 * can't remove the ADD request already sent 3. arrival of
1274c54a18d2SRandall Stewart 		 * ADD
1275c54a18d2SRandall Stewart 		 */
1276c54a18d2SRandall Stewart 		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1277f8829a4aSRandall Stewart 			return (-1);
1278f8829a4aSRandall Stewart 		}
1279f8829a4aSRandall Stewart 		/* is the negative request already in queue, and not sent */
12801b649582SRandall Stewart 		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
12811b649582SRandall Stewart 		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1282f8829a4aSRandall Stewart 			/* add requested, delete already queued */
1283f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
12841b649582SRandall Stewart 			/* remove the ifa from the restricted list */
12851b649582SRandall Stewart 			sctp_del_local_addr_restricted(stcb, ifa);
12861b649582SRandall Stewart 			/* free the asconf param */
1287207304d4SRandall Stewart 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
12881b649582SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
12891b649582SRandall Stewart 			return (-1);
12901b649582SRandall Stewart 		}
12911b649582SRandall Stewart 		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
12921b649582SRandall Stewart 		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
12931b649582SRandall Stewart 			/* delete requested, add already queued */
12941b649582SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
12951b649582SRandall Stewart 			/* remove the aa->ifa from the restricted list */
12961b649582SRandall Stewart 			sctp_del_local_addr_restricted(stcb, aa->ifa);
12971b649582SRandall Stewart 			/* free the asconf param */
12981b649582SRandall Stewart 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
12991b649582SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1300f8829a4aSRandall Stewart 			return (-1);
1301f8829a4aSRandall Stewart 		}
1302f8829a4aSRandall Stewart 	}			/* for each aa */
1303f8829a4aSRandall Stewart 
1304f8829a4aSRandall Stewart 	/* adding new request to the queue */
13051b649582SRandall Stewart 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
13061b649582SRandall Stewart 	    SCTP_M_ASC_ADDR);
1307f8829a4aSRandall Stewart 	if (aa == NULL) {
1308f8829a4aSRandall Stewart 		/* didn't get memory */
13091b649582SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1310f8829a4aSRandall Stewart 		return (-1);
1311f8829a4aSRandall Stewart 	}
1312830d754dSRandall Stewart 	aa->special_del = 0;
1313f8829a4aSRandall Stewart 	/* fill in asconf address parameter fields */
1314f8829a4aSRandall Stewart 	/* top level elements are "networked" during send */
1315f8829a4aSRandall Stewart 	aa->ap.aph.ph.param_type = type;
1316f8829a4aSRandall Stewart 	aa->ifa = ifa;
1317bff64a4dSRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
1318f8829a4aSRandall Stewart 	/* correlation_id filled in during send routine later... */
131942551e99SRandall Stewart 	if (ifa->address.sa.sa_family == AF_INET6) {
1320f8829a4aSRandall Stewart 		/* IPv6 address */
1321f8829a4aSRandall Stewart 		struct sockaddr_in6 *sin6;
1322f8829a4aSRandall Stewart 
132342551e99SRandall Stewart 		sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
1324f8829a4aSRandall Stewart 		sa = (struct sockaddr *)sin6;
1325f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1326f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
13271b649582SRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1328f8829a4aSRandall Stewart 		    sizeof(struct sctp_ipv6addr_param);
1329f8829a4aSRandall Stewart 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1330f8829a4aSRandall Stewart 		    sizeof(struct in6_addr));
133142551e99SRandall Stewart 	} else if (ifa->address.sa.sa_family == AF_INET) {
1332f8829a4aSRandall Stewart 		/* IPv4 address */
13331b649582SRandall Stewart 		struct sockaddr_in *sin;
1334f8829a4aSRandall Stewart 
13351b649582SRandall Stewart 		sin = (struct sockaddr_in *)&ifa->address.sa;
1336f8829a4aSRandall Stewart 		sa = (struct sockaddr *)sin;
1337f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1338f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
13391b649582SRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1340f8829a4aSRandall Stewart 		    sizeof(struct sctp_ipv4addr_param);
1341f8829a4aSRandall Stewart 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1342f8829a4aSRandall Stewart 		    sizeof(struct in_addr));
1343f8829a4aSRandall Stewart 	} else {
1344f8829a4aSRandall Stewart 		/* invalid family! */
1345207304d4SRandall Stewart 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
13463232788eSRandall Stewart 		sctp_free_ifa(ifa);
1347f8829a4aSRandall Stewart 		return (-1);
1348f8829a4aSRandall Stewart 	}
1349f8829a4aSRandall Stewart 	aa->sent = 0;		/* clear sent flag */
1350f8829a4aSRandall Stewart 
1351f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1352f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
1353dec0177dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
1354c54a18d2SRandall Stewart 		if (type == SCTP_ADD_IP_ADDRESS) {
1355c54a18d2SRandall Stewart 			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1356c54a18d2SRandall Stewart 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1357c54a18d2SRandall Stewart 		} else if (type == SCTP_DEL_IP_ADDRESS) {
13581b649582SRandall Stewart 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1359ad81507eSRandall Stewart 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1360f8829a4aSRandall Stewart 		} else {
13611b649582SRandall Stewart 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1362ad81507eSRandall Stewart 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1363f8829a4aSRandall Stewart 		}
1364f8829a4aSRandall Stewart 	}
1365ad81507eSRandall Stewart #endif
1366f8829a4aSRandall Stewart 
1367f8829a4aSRandall Stewart 	return (0);
1368f8829a4aSRandall Stewart }
1369f8829a4aSRandall Stewart 
13701b649582SRandall Stewart 
13711b649582SRandall Stewart /*
13721b649582SRandall Stewart  * add an asconf operation for the given ifa and type.
13731b649582SRandall Stewart  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
13741b649582SRandall Stewart  * returns 0 if completed, -1 if not completed, 1 if immediate send is
13751b649582SRandall Stewart  * advisable.
13761b649582SRandall Stewart  */
13771b649582SRandall Stewart static int
13781b649582SRandall Stewart sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
13791b649582SRandall Stewart     uint16_t type)
13801b649582SRandall Stewart {
13811b649582SRandall Stewart 	uint32_t status;
13821b649582SRandall Stewart 	int pending_delete_queued = 0;
13831b649582SRandall Stewart 
13841b649582SRandall Stewart 	/* see if peer supports ASCONF */
13851b649582SRandall Stewart 	if (stcb->asoc.peer_supports_asconf == 0) {
13861b649582SRandall Stewart 		return (-1);
13871b649582SRandall Stewart 	}
13881b649582SRandall Stewart 	/*
13891b649582SRandall Stewart 	 * if this is deleting the last address from the assoc, mark it as
13901b649582SRandall Stewart 	 * pending.
13911b649582SRandall Stewart 	 */
13921b649582SRandall Stewart 	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending &&
13931b649582SRandall Stewart 	    (sctp_local_addr_count(stcb) < 2)) {
13941b649582SRandall Stewart 		/* set the pending delete info only */
13951b649582SRandall Stewart 		stcb->asoc.asconf_del_pending = 1;
13961b649582SRandall Stewart 		stcb->asoc.asconf_addr_del_pending = ifa;
13971b649582SRandall Stewart 		atomic_add_int(&ifa->refcount, 1);
13981b649582SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF2,
13991b649582SRandall Stewart 		    "asconf_queue_add: mark delete last address pending\n");
14001b649582SRandall Stewart 		return (-1);
14011b649582SRandall Stewart 	}
1402c54a18d2SRandall Stewart 	/* queue an asconf parameter */
1403c54a18d2SRandall Stewart 	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1404c54a18d2SRandall Stewart 
14051b649582SRandall Stewart 	/*
14061b649582SRandall Stewart 	 * if this is an add, and there is a delete also pending (i.e. the
14071b649582SRandall Stewart 	 * last local address is being changed), queue the pending delete
14081b649582SRandall Stewart 	 * too.
14091b649582SRandall Stewart 	 */
1410c54a18d2SRandall Stewart 	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
14111b649582SRandall Stewart 		/* queue in the pending delete */
14121b649582SRandall Stewart 		if (sctp_asconf_queue_mgmt(stcb,
14131b649582SRandall Stewart 		    stcb->asoc.asconf_addr_del_pending,
14141b649582SRandall Stewart 		    SCTP_DEL_IP_ADDRESS) == 0) {
14151b649582SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queing pending delete\n");
14161b649582SRandall Stewart 			pending_delete_queued = 1;
14171b649582SRandall Stewart 			/* clear out the pending delete info */
14181b649582SRandall Stewart 			stcb->asoc.asconf_del_pending = 0;
14191b649582SRandall Stewart 			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
14201b649582SRandall Stewart 			stcb->asoc.asconf_addr_del_pending = NULL;
14211b649582SRandall Stewart 		}
14221b649582SRandall Stewart 	}
1423c54a18d2SRandall Stewart 	if (pending_delete_queued) {
14241b649582SRandall Stewart 		struct sctp_nets *net;
14251b649582SRandall Stewart 
14261b649582SRandall Stewart 		/*
14271b649582SRandall Stewart 		 * since we know that the only/last address is now being
14281b649582SRandall Stewart 		 * changed in this case, reset the cwnd/rto on all nets to
14291b649582SRandall Stewart 		 * start as a new address and path.  Also clear the error
14301b649582SRandall Stewart 		 * counts to give the assoc the best chance to complete the
14311b649582SRandall Stewart 		 * address change.
14321b649582SRandall Stewart 		 */
14331b649582SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
14341b649582SRandall Stewart 			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
14351b649582SRandall Stewart 			    net);
14361b649582SRandall Stewart 			net->RTO = 0;
14371b649582SRandall Stewart 			net->error_count = 0;
14381b649582SRandall Stewart 		}
14391b649582SRandall Stewart 		stcb->asoc.overall_error_count = 0;
1440b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1441c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1442c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1443c4739e2fSRandall Stewart 			    0,
1444c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_ASCONF,
1445c4739e2fSRandall Stewart 			    __LINE__);
1446c4739e2fSRandall Stewart 		}
14471b649582SRandall Stewart 		/* queue in an advisory set primary too */
14481b649582SRandall Stewart 		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
14491b649582SRandall Stewart 		/* let caller know we should send this out immediately */
14501b649582SRandall Stewart 		status = 1;
14511b649582SRandall Stewart 	}
14521b649582SRandall Stewart 	return (status);
14531b649582SRandall Stewart }
14541b649582SRandall Stewart 
14553232788eSRandall Stewart /*-
14563232788eSRandall Stewart  * add an asconf delete IP address parameter to the queue by sockaddr and
14573232788eSRandall Stewart  * possibly with no sctp_ifa available.  This is only called by the routine
14583232788eSRandall Stewart  * that checks the addresses in an INIT-ACK against the current address list.
1459f8829a4aSRandall Stewart  * returns 0 if completed, non-zero if not completed.
14603232788eSRandall Stewart  * NOTE: if an add is already scheduled (and not yet sent out), simply
14613232788eSRandall Stewart  * remove it from queue.  If a duplicate operation is found, ignore the
14623232788eSRandall Stewart  * new one.
1463f8829a4aSRandall Stewart  */
14641b649582SRandall Stewart static int
14653232788eSRandall Stewart sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1466f8829a4aSRandall Stewart {
1467bff64a4dSRandall Stewart 	struct sctp_ifa *ifa;
1468f8829a4aSRandall Stewart 	struct sctp_asconf_addr *aa, *aa_next;
146942551e99SRandall Stewart 	uint32_t vrf_id;
1470f8829a4aSRandall Stewart 
1471ad81507eSRandall Stewart 	if (stcb == NULL) {
1472ad81507eSRandall Stewart 		return (-1);
1473ad81507eSRandall Stewart 	}
1474f8829a4aSRandall Stewart 	/* see if peer supports ASCONF */
1475f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_asconf == 0) {
1476f8829a4aSRandall Stewart 		return (-1);
1477f8829a4aSRandall Stewart 	}
1478f8829a4aSRandall Stewart 	/* make sure the request isn't already in the queue */
14794a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1480f8829a4aSRandall Stewart 		/* address match? */
1481f8829a4aSRandall Stewart 		if (sctp_asconf_addr_match(aa, sa) == 0)
1482f8829a4aSRandall Stewart 			continue;
1483f8829a4aSRandall Stewart 		/* is the request already in queue (sent or not) */
14843232788eSRandall Stewart 		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1485f8829a4aSRandall Stewart 			return (-1);
1486f8829a4aSRandall Stewart 		}
1487f8829a4aSRandall Stewart 		/* is the negative request already in queue, and not sent */
1488f8829a4aSRandall Stewart 		if (aa->sent == 1)
1489f8829a4aSRandall Stewart 			continue;
14903232788eSRandall Stewart 		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
14913232788eSRandall Stewart 			/* add already queued, so remove existing entry */
1492f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
14931b649582SRandall Stewart 			sctp_del_local_addr_restricted(stcb, aa->ifa);
1494f8829a4aSRandall Stewart 			/* free the entry */
1495207304d4SRandall Stewart 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1496f8829a4aSRandall Stewart 			return (-1);
1497f8829a4aSRandall Stewart 		}
1498f8829a4aSRandall Stewart 	}			/* for each aa */
14993232788eSRandall Stewart 
15003232788eSRandall Stewart 	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1501bff64a4dSRandall Stewart 	if (stcb) {
1502bff64a4dSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
1503bff64a4dSRandall Stewart 	} else {
1504bff64a4dSRandall Stewart 		vrf_id = SCTP_DEFAULT_VRFID;
1505bff64a4dSRandall Stewart 	}
1506851b7298SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
15073232788eSRandall Stewart 
1508f8829a4aSRandall Stewart 	/* adding new request to the queue */
15091b649582SRandall Stewart 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
15101b649582SRandall Stewart 	    SCTP_M_ASC_ADDR);
1511f8829a4aSRandall Stewart 	if (aa == NULL) {
1512f8829a4aSRandall Stewart 		/* didn't get memory */
1513ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
15143232788eSRandall Stewart 		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1515f8829a4aSRandall Stewart 		return (-1);
1516f8829a4aSRandall Stewart 	}
1517830d754dSRandall Stewart 	aa->special_del = 0;
1518f8829a4aSRandall Stewart 	/* fill in asconf address parameter fields */
1519f8829a4aSRandall Stewart 	/* top level elements are "networked" during send */
15203232788eSRandall Stewart 	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1521bff64a4dSRandall Stewart 	aa->ifa = ifa;
15223232788eSRandall Stewart 	if (ifa)
1523bff64a4dSRandall Stewart 		atomic_add_int(&ifa->refcount, 1);
1524f8829a4aSRandall Stewart 	/* correlation_id filled in during send routine later... */
1525f8829a4aSRandall Stewart 	if (sa->sa_family == AF_INET6) {
1526f8829a4aSRandall Stewart 		/* IPv6 address */
1527f8829a4aSRandall Stewart 		struct sockaddr_in6 *sin6;
1528f8829a4aSRandall Stewart 
1529f8829a4aSRandall Stewart 		sin6 = (struct sockaddr_in6 *)sa;
1530f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1531f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1532f8829a4aSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1533f8829a4aSRandall Stewart 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1534f8829a4aSRandall Stewart 		    sizeof(struct in6_addr));
1535f8829a4aSRandall Stewart 	} else if (sa->sa_family == AF_INET) {
1536f8829a4aSRandall Stewart 		/* IPv4 address */
1537f8829a4aSRandall Stewart 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1538f8829a4aSRandall Stewart 
1539f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1540f8829a4aSRandall Stewart 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1541f8829a4aSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1542f8829a4aSRandall Stewart 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1543f8829a4aSRandall Stewart 		    sizeof(struct in_addr));
1544f8829a4aSRandall Stewart 	} else {
1545f8829a4aSRandall Stewart 		/* invalid family! */
1546207304d4SRandall Stewart 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
15473232788eSRandall Stewart 		if (ifa)
15483232788eSRandall Stewart 			sctp_free_ifa(ifa);
1549f8829a4aSRandall Stewart 		return (-1);
1550f8829a4aSRandall Stewart 	}
1551f8829a4aSRandall Stewart 	aa->sent = 0;		/* clear sent flag */
1552f8829a4aSRandall Stewart 
15533232788eSRandall Stewart 	/* delete goes to the back of the queue */
1554f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1555f8829a4aSRandall Stewart 
1556c99efcf6SRandall Stewart 	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1557f8829a4aSRandall Stewart 	return (0);
1558f8829a4aSRandall Stewart }
1559f8829a4aSRandall Stewart 
1560f8829a4aSRandall Stewart /*
1561f8829a4aSRandall Stewart  * find a specific asconf param on our "sent" queue
1562f8829a4aSRandall Stewart  */
1563f8829a4aSRandall Stewart static struct sctp_asconf_addr *
1564f8829a4aSRandall Stewart sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1565f8829a4aSRandall Stewart {
1566f8829a4aSRandall Stewart 	struct sctp_asconf_addr *aa;
1567f8829a4aSRandall Stewart 
1568f8829a4aSRandall Stewart 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1569f8829a4aSRandall Stewart 		if (aa->ap.aph.correlation_id == correlation_id &&
1570f8829a4aSRandall Stewart 		    aa->sent == 1) {
1571f8829a4aSRandall Stewart 			/* found it */
1572f8829a4aSRandall Stewart 			return (aa);
1573f8829a4aSRandall Stewart 		}
1574f8829a4aSRandall Stewart 	}
1575f8829a4aSRandall Stewart 	/* didn't find it */
1576f8829a4aSRandall Stewart 	return (NULL);
1577f8829a4aSRandall Stewart }
1578f8829a4aSRandall Stewart 
1579f8829a4aSRandall Stewart /*
1580f8829a4aSRandall Stewart  * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1581f8829a4aSRandall Stewart  * notifications based on the error response
1582f8829a4aSRandall Stewart  */
1583f8829a4aSRandall Stewart static void
1584f8829a4aSRandall Stewart sctp_asconf_process_error(struct sctp_tcb *stcb,
1585f8829a4aSRandall Stewart     struct sctp_asconf_paramhdr *aph)
1586f8829a4aSRandall Stewart {
1587f8829a4aSRandall Stewart 	struct sctp_error_cause *eh;
1588f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
1589f8829a4aSRandall Stewart 	uint16_t param_type;
1590f8829a4aSRandall Stewart 	uint16_t error_code;
1591f8829a4aSRandall Stewart 
1592f8829a4aSRandall Stewart 	eh = (struct sctp_error_cause *)(aph + 1);
1593f8829a4aSRandall Stewart 	ph = (struct sctp_paramhdr *)(eh + 1);
1594f8829a4aSRandall Stewart 	/* validate lengths */
1595f8829a4aSRandall Stewart 	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1596f8829a4aSRandall Stewart 	    htons(aph->ph.param_length)) {
1597f8829a4aSRandall Stewart 		/* invalid error cause length */
1598ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1599ad81507eSRandall Stewart 		    "asconf_process_error: cause element too long\n");
1600f8829a4aSRandall Stewart 		return;
1601f8829a4aSRandall Stewart 	}
1602f8829a4aSRandall Stewart 	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1603f8829a4aSRandall Stewart 	    htons(eh->length)) {
1604f8829a4aSRandall Stewart 		/* invalid included TLV length */
1605ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1606ad81507eSRandall Stewart 		    "asconf_process_error: included TLV too long\n");
1607f8829a4aSRandall Stewart 		return;
1608f8829a4aSRandall Stewart 	}
1609f8829a4aSRandall Stewart 	/* which error code ? */
1610f8829a4aSRandall Stewart 	error_code = ntohs(eh->code);
1611f8829a4aSRandall Stewart 	param_type = ntohs(aph->ph.param_type);
1612f8829a4aSRandall Stewart 	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1613f8829a4aSRandall Stewart 	switch (error_code) {
1614f8829a4aSRandall Stewart 	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1615f8829a4aSRandall Stewart 		/* we allow ourselves to "try again" for this error */
1616f8829a4aSRandall Stewart 		break;
1617f8829a4aSRandall Stewart 	default:
1618f8829a4aSRandall Stewart 		/* peer can't handle it... */
1619f8829a4aSRandall Stewart 		switch (param_type) {
1620f8829a4aSRandall Stewart 		case SCTP_ADD_IP_ADDRESS:
1621f8829a4aSRandall Stewart 		case SCTP_DEL_IP_ADDRESS:
1622f8829a4aSRandall Stewart 			stcb->asoc.peer_supports_asconf = 0;
1623f8829a4aSRandall Stewart 			break;
1624f8829a4aSRandall Stewart 		case SCTP_SET_PRIM_ADDR:
1625f8829a4aSRandall Stewart 			stcb->asoc.peer_supports_asconf = 0;
1626f8829a4aSRandall Stewart 			break;
1627f8829a4aSRandall Stewart 		default:
1628f8829a4aSRandall Stewart 			break;
1629f8829a4aSRandall Stewart 		}
1630f8829a4aSRandall Stewart 	}
1631f8829a4aSRandall Stewart }
1632f8829a4aSRandall Stewart 
1633f8829a4aSRandall Stewart /*
163418e198d3SRandall Stewart  * process an asconf queue param.
163518e198d3SRandall Stewart  * aparam: parameter to process, will be removed from the queue.
163618e198d3SRandall Stewart  * flag: 1=success case, 0=failure case
1637f8829a4aSRandall Stewart  */
1638f8829a4aSRandall Stewart static void
1639f8829a4aSRandall Stewart sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1640f8829a4aSRandall Stewart     struct sctp_asconf_addr *aparam, uint32_t flag)
1641f8829a4aSRandall Stewart {
1642f8829a4aSRandall Stewart 	uint16_t param_type;
1643f8829a4aSRandall Stewart 
1644f8829a4aSRandall Stewart 	/* process this param */
1645f8829a4aSRandall Stewart 	param_type = aparam->ap.aph.ph.param_type;
1646f8829a4aSRandall Stewart 	switch (param_type) {
1647f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1648ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1649ad81507eSRandall Stewart 		    "process_param_ack: added IP address\n");
1650f8829a4aSRandall Stewart 		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1651f8829a4aSRandall Stewart 		break;
1652f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1653ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1654ad81507eSRandall Stewart 		    "process_param_ack: deleted IP address\n");
1655f8829a4aSRandall Stewart 		/* nothing really to do... lists already updated */
1656f8829a4aSRandall Stewart 		break;
1657f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1658c54a18d2SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1659c54a18d2SRandall Stewart 		    "process_param_ack: set primary IP address\n");
1660f8829a4aSRandall Stewart 		/* nothing to do... peer may start using this addr */
1661f8829a4aSRandall Stewart 		if (flag == 0)
1662f8829a4aSRandall Stewart 			stcb->asoc.peer_supports_asconf = 0;
1663f8829a4aSRandall Stewart 		break;
1664f8829a4aSRandall Stewart 	default:
1665f8829a4aSRandall Stewart 		/* should NEVER happen */
1666f8829a4aSRandall Stewart 		break;
1667f8829a4aSRandall Stewart 	}
1668f8829a4aSRandall Stewart 
1669f8829a4aSRandall Stewart 	/* remove the param and free it */
1670f8829a4aSRandall Stewart 	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
16713232788eSRandall Stewart 	if (aparam->ifa)
1672bff64a4dSRandall Stewart 		sctp_free_ifa(aparam->ifa);
1673207304d4SRandall Stewart 	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1674f8829a4aSRandall Stewart }
1675f8829a4aSRandall Stewart 
1676f8829a4aSRandall Stewart /*
1677f8829a4aSRandall Stewart  * cleanup from a bad asconf ack parameter
1678f8829a4aSRandall Stewart  */
1679f8829a4aSRandall Stewart static void
1680f8829a4aSRandall Stewart sctp_asconf_ack_clear(struct sctp_tcb *stcb)
1681f8829a4aSRandall Stewart {
1682f8829a4aSRandall Stewart 	/* assume peer doesn't really know how to do asconfs */
1683f8829a4aSRandall Stewart 	stcb->asoc.peer_supports_asconf = 0;
1684f8829a4aSRandall Stewart 	/* XXX we could free the pending queue here */
1685f8829a4aSRandall Stewart }
1686f8829a4aSRandall Stewart 
1687f8829a4aSRandall Stewart void
1688f8829a4aSRandall Stewart sctp_handle_asconf_ack(struct mbuf *m, int offset,
1689f8829a4aSRandall Stewart     struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
16903232788eSRandall Stewart     struct sctp_nets *net, int *abort_no_unlock)
1691f8829a4aSRandall Stewart {
1692f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1693f8829a4aSRandall Stewart 	uint32_t serial_num;
1694f8829a4aSRandall Stewart 	uint16_t ack_length;
1695f8829a4aSRandall Stewart 	struct sctp_asconf_paramhdr *aph;
1696f8829a4aSRandall Stewart 	struct sctp_asconf_addr *aa, *aa_next;
1697f8829a4aSRandall Stewart 	uint32_t last_error_id = 0;	/* last error correlation id */
1698f8829a4aSRandall Stewart 	uint32_t id;
1699f8829a4aSRandall Stewart 	struct sctp_asconf_addr *ap;
1700f8829a4aSRandall Stewart 
1701f8829a4aSRandall Stewart 	/* asconf param buffer */
1702f42a358aSRandall Stewart 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1703f8829a4aSRandall Stewart 
1704f8829a4aSRandall Stewart 	/* verify minimum length */
1705f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1706ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1707ad81507eSRandall Stewart 		    "handle_asconf_ack: chunk too small = %xh\n",
1708f8829a4aSRandall Stewart 		    ntohs(cp->ch.chunk_length));
1709f8829a4aSRandall Stewart 		return;
1710f8829a4aSRandall Stewart 	}
1711f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1712f8829a4aSRandall Stewart 	serial_num = ntohl(cp->serial_number);
1713f8829a4aSRandall Stewart 
1714f8829a4aSRandall Stewart 	/*
1715f8829a4aSRandall Stewart 	 * NOTE: we may want to handle this differently- currently, we will
1716f8829a4aSRandall Stewart 	 * abort when we get an ack for the expected serial number + 1 (eg.
1717f8829a4aSRandall Stewart 	 * we didn't send it), process an ack normally if it is the expected
1718f8829a4aSRandall Stewart 	 * serial number, and re-send the previous ack for *ALL* other
1719f8829a4aSRandall Stewart 	 * serial numbers
1720f8829a4aSRandall Stewart 	 */
1721f8829a4aSRandall Stewart 
1722f8829a4aSRandall Stewart 	/*
1723f8829a4aSRandall Stewart 	 * if the serial number is the next expected, but I didn't send it,
1724f8829a4aSRandall Stewart 	 * abort the asoc, since someone probably just hijacked us...
1725f8829a4aSRandall Stewart 	 */
1726f8829a4aSRandall Stewart 	if (serial_num == (asoc->asconf_seq_out + 1)) {
1727ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1728f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1729ceaad40aSRandall Stewart 		    SCTP_CAUSE_ILLEGAL_ASCONF_ACK, NULL, SCTP_SO_NOT_LOCKED);
17303232788eSRandall Stewart 		*abort_no_unlock = 1;
1731f8829a4aSRandall Stewart 		return;
1732f8829a4aSRandall Stewart 	}
1733c54a18d2SRandall Stewart 	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1734f8829a4aSRandall Stewart 		/* got a duplicate/unexpected ASCONF-ACK */
1735ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1736c54a18d2SRandall Stewart 		    serial_num, asoc->asconf_seq_out_acked + 1);
1737f8829a4aSRandall Stewart 		return;
1738f8829a4aSRandall Stewart 	}
1739c54a18d2SRandall Stewart 	if (serial_num == asoc->asconf_seq_out - 1) {
1740f8829a4aSRandall Stewart 		/* stop our timer */
17413c503c28SRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
17423c503c28SRandall Stewart 		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1743c54a18d2SRandall Stewart 	}
1744f8829a4aSRandall Stewart 	/* process the ASCONF-ACK contents */
1745f8829a4aSRandall Stewart 	ack_length = ntohs(cp->ch.chunk_length) -
1746f8829a4aSRandall Stewart 	    sizeof(struct sctp_asconf_ack_chunk);
1747f8829a4aSRandall Stewart 	offset += sizeof(struct sctp_asconf_ack_chunk);
1748f8829a4aSRandall Stewart 	/* process through all parameters */
1749f8829a4aSRandall Stewart 	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1750f8829a4aSRandall Stewart 		unsigned int param_length, param_type;
1751f8829a4aSRandall Stewart 
1752f8829a4aSRandall Stewart 		/* get pointer to next asconf parameter */
1753f8829a4aSRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1754f8829a4aSRandall Stewart 		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1755f8829a4aSRandall Stewart 		if (aph == NULL) {
1756f8829a4aSRandall Stewart 			/* can't get an asconf paramhdr */
1757f8829a4aSRandall Stewart 			sctp_asconf_ack_clear(stcb);
1758f8829a4aSRandall Stewart 			return;
1759f8829a4aSRandall Stewart 		}
1760f8829a4aSRandall Stewart 		param_type = ntohs(aph->ph.param_type);
1761f8829a4aSRandall Stewart 		param_length = ntohs(aph->ph.param_length);
1762f8829a4aSRandall Stewart 		if (param_length > ack_length) {
1763f8829a4aSRandall Stewart 			sctp_asconf_ack_clear(stcb);
1764f8829a4aSRandall Stewart 			return;
1765f8829a4aSRandall Stewart 		}
1766f8829a4aSRandall Stewart 		if (param_length < sizeof(struct sctp_paramhdr)) {
1767f8829a4aSRandall Stewart 			sctp_asconf_ack_clear(stcb);
1768f8829a4aSRandall Stewart 			return;
1769f8829a4aSRandall Stewart 		}
1770f8829a4aSRandall Stewart 		/* get the complete parameter... */
1771f8829a4aSRandall Stewart 		if (param_length > sizeof(aparam_buf)) {
1772ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1,
1773ad81507eSRandall Stewart 			    "param length (%u) larger than buffer size!\n", param_length);
1774f8829a4aSRandall Stewart 			sctp_asconf_ack_clear(stcb);
1775f8829a4aSRandall Stewart 			return;
1776f8829a4aSRandall Stewart 		}
1777f8829a4aSRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1778f8829a4aSRandall Stewart 		if (aph == NULL) {
1779f8829a4aSRandall Stewart 			sctp_asconf_ack_clear(stcb);
1780f8829a4aSRandall Stewart 			return;
1781f8829a4aSRandall Stewart 		}
1782f8829a4aSRandall Stewart 		/* correlation_id is transparent to peer, no ntohl needed */
1783f8829a4aSRandall Stewart 		id = aph->correlation_id;
1784f8829a4aSRandall Stewart 
1785f8829a4aSRandall Stewart 		switch (param_type) {
1786f8829a4aSRandall Stewart 		case SCTP_ERROR_CAUSE_IND:
1787f8829a4aSRandall Stewart 			last_error_id = id;
1788f8829a4aSRandall Stewart 			/* find the corresponding asconf param in our queue */
1789f8829a4aSRandall Stewart 			ap = sctp_asconf_find_param(stcb, id);
1790f8829a4aSRandall Stewart 			if (ap == NULL) {
1791f8829a4aSRandall Stewart 				/* hmm... can't find this in our queue! */
1792f8829a4aSRandall Stewart 				break;
1793f8829a4aSRandall Stewart 			}
1794f8829a4aSRandall Stewart 			/* process the parameter, failed flag */
1795f8829a4aSRandall Stewart 			sctp_asconf_process_param_ack(stcb, ap, 0);
1796f8829a4aSRandall Stewart 			/* process the error response */
1797f8829a4aSRandall Stewart 			sctp_asconf_process_error(stcb, aph);
1798f8829a4aSRandall Stewart 			break;
1799f8829a4aSRandall Stewart 		case SCTP_SUCCESS_REPORT:
1800f8829a4aSRandall Stewart 			/* find the corresponding asconf param in our queue */
1801f8829a4aSRandall Stewart 			ap = sctp_asconf_find_param(stcb, id);
1802f8829a4aSRandall Stewart 			if (ap == NULL) {
1803f8829a4aSRandall Stewart 				/* hmm... can't find this in our queue! */
1804f8829a4aSRandall Stewart 				break;
1805f8829a4aSRandall Stewart 			}
1806f8829a4aSRandall Stewart 			/* process the parameter, success flag */
1807f8829a4aSRandall Stewart 			sctp_asconf_process_param_ack(stcb, ap, 1);
1808f8829a4aSRandall Stewart 			break;
1809f8829a4aSRandall Stewart 		default:
1810f8829a4aSRandall Stewart 			break;
1811f8829a4aSRandall Stewart 		}		/* switch */
1812f8829a4aSRandall Stewart 
1813f8829a4aSRandall Stewart 		/* update remaining ASCONF-ACK message length to process */
1814f8829a4aSRandall Stewart 		ack_length -= SCTP_SIZE32(param_length);
1815f8829a4aSRandall Stewart 		if (ack_length <= 0) {
1816f8829a4aSRandall Stewart 			/* no more data in the mbuf chain */
1817f8829a4aSRandall Stewart 			break;
1818f8829a4aSRandall Stewart 		}
1819f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(param_length);
1820f8829a4aSRandall Stewart 	}			/* while */
1821f8829a4aSRandall Stewart 
1822f8829a4aSRandall Stewart 	/*
1823f8829a4aSRandall Stewart 	 * if there are any "sent" params still on the queue, these are
1824f8829a4aSRandall Stewart 	 * implicitly "success", or "failed" (if we got an error back) ...
1825f8829a4aSRandall Stewart 	 * so process these appropriately
1826f8829a4aSRandall Stewart 	 *
1827f8829a4aSRandall Stewart 	 * we assume that the correlation_id's are monotonically increasing
1828f8829a4aSRandall Stewart 	 * beginning from 1 and that we don't have *that* many outstanding
1829f8829a4aSRandall Stewart 	 * at any given time
1830f8829a4aSRandall Stewart 	 */
1831f8829a4aSRandall Stewart 	if (last_error_id == 0)
1832f8829a4aSRandall Stewart 		last_error_id--;/* set to "max" value */
18334a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1834f8829a4aSRandall Stewart 		if (aa->sent == 1) {
1835f8829a4aSRandall Stewart 			/*
1836f8829a4aSRandall Stewart 			 * implicitly successful or failed if correlation_id
1837f8829a4aSRandall Stewart 			 * < last_error_id, then success else, failure
1838f8829a4aSRandall Stewart 			 */
1839f8829a4aSRandall Stewart 			if (aa->ap.aph.correlation_id < last_error_id)
184018e198d3SRandall Stewart 				sctp_asconf_process_param_ack(stcb, aa, 1);
1841f8829a4aSRandall Stewart 			else
184218e198d3SRandall Stewart 				sctp_asconf_process_param_ack(stcb, aa, 0);
1843f8829a4aSRandall Stewart 		} else {
1844f8829a4aSRandall Stewart 			/*
1845f8829a4aSRandall Stewart 			 * since we always process in order (FIFO queue) if
1846f8829a4aSRandall Stewart 			 * we reach one that hasn't been sent, the rest
1847f8829a4aSRandall Stewart 			 * should not have been sent either. so, we're
1848f8829a4aSRandall Stewart 			 * done...
1849f8829a4aSRandall Stewart 			 */
1850f8829a4aSRandall Stewart 			break;
1851f8829a4aSRandall Stewart 		}
1852f8829a4aSRandall Stewart 	}
1853f8829a4aSRandall Stewart 
1854f8829a4aSRandall Stewart 	/* update the next sequence number to use */
1855c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked++;
1856f8829a4aSRandall Stewart 	/* remove the old ASCONF on our outbound queue */
1857f8829a4aSRandall Stewart 	sctp_toss_old_asconf(stcb);
1858f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
18591b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
1860f8829a4aSRandall Stewart 		/* we have more params, so restart our timer */
1861f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1862f8829a4aSRandall Stewart 		    stcb, net);
18631b649582SRandall Stewart #else
18641b649582SRandall Stewart 		/* we have more params, so send out more */
18653232788eSRandall Stewart 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
18661b649582SRandall Stewart #endif
1867f8829a4aSRandall Stewart 	}
1868f8829a4aSRandall Stewart }
1869f8829a4aSRandall Stewart 
18705e2c2d87SRandall Stewart #ifdef INET6
1871f8829a4aSRandall Stewart static uint32_t
1872f8829a4aSRandall Stewart sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1873f8829a4aSRandall Stewart {
1874f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6, *net6;
1875f8829a4aSRandall Stewart 	struct sctp_nets *net;
1876f8829a4aSRandall Stewart 
1877f8829a4aSRandall Stewart 	if (sa->sa_family != AF_INET6) {
1878f8829a4aSRandall Stewart 		/* wrong family */
1879f8829a4aSRandall Stewart 		return (0);
1880f8829a4aSRandall Stewart 	}
1881f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)sa;
1882f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1883f8829a4aSRandall Stewart 		/* not link local address */
1884f8829a4aSRandall Stewart 		return (0);
1885f8829a4aSRandall Stewart 	}
1886f8829a4aSRandall Stewart 	/* hunt through our destination nets list for this scope_id */
1887f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1888f8829a4aSRandall Stewart 		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1889f8829a4aSRandall Stewart 		    AF_INET6)
1890f8829a4aSRandall Stewart 			continue;
1891f8829a4aSRandall Stewart 		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1892f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1893f8829a4aSRandall Stewart 			continue;
1894f8829a4aSRandall Stewart 		if (sctp_is_same_scope(sin6, net6)) {
1895f8829a4aSRandall Stewart 			/* found one */
1896f8829a4aSRandall Stewart 			return (1);
1897f8829a4aSRandall Stewart 		}
1898f8829a4aSRandall Stewart 	}
1899f8829a4aSRandall Stewart 	/* didn't find one */
1900f8829a4aSRandall Stewart 	return (0);
1901f8829a4aSRandall Stewart }
1902f8829a4aSRandall Stewart 
19035e2c2d87SRandall Stewart #endif
19045e2c2d87SRandall Stewart 
1905f8829a4aSRandall Stewart /*
1906f8829a4aSRandall Stewart  * address management functions
1907f8829a4aSRandall Stewart  */
1908f8829a4aSRandall Stewart static void
1909f8829a4aSRandall Stewart sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
19103232788eSRandall Stewart     struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1911f8829a4aSRandall Stewart {
1912f8829a4aSRandall Stewart 	int status;
1913f8829a4aSRandall Stewart 
1914f8829a4aSRandall Stewart 
1915f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1916f8829a4aSRandall Stewart 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1917f8829a4aSRandall Stewart 		/* subset bound, no ASCONF allowed case, so ignore */
1918f8829a4aSRandall Stewart 		return;
1919f8829a4aSRandall Stewart 	}
1920f8829a4aSRandall Stewart 	/*
1921f8829a4aSRandall Stewart 	 * note: we know this is not the subset bound, no ASCONF case eg.
1922f8829a4aSRandall Stewart 	 * this is boundall or subset bound w/ASCONF allowed
1923f8829a4aSRandall Stewart 	 */
1924f8829a4aSRandall Stewart 
1925f8829a4aSRandall Stewart 	/* first, make sure it's a good address family */
192642551e99SRandall Stewart 	if (ifa->address.sa.sa_family != AF_INET6 &&
192742551e99SRandall Stewart 	    ifa->address.sa.sa_family != AF_INET) {
1928f8829a4aSRandall Stewart 		return;
1929f8829a4aSRandall Stewart 	}
1930f8829a4aSRandall Stewart 	/* make sure we're "allowed" to add this type of addr */
193142551e99SRandall Stewart 	if (ifa->address.sa.sa_family == AF_INET6) {
1932f8829a4aSRandall Stewart 		/* invalid if we're not a v6 endpoint */
1933f8829a4aSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1934f8829a4aSRandall Stewart 			return;
1935f8829a4aSRandall Stewart 		/* is the v6 addr really valid ? */
193642551e99SRandall Stewart 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1937f8829a4aSRandall Stewart 			return;
1938f8829a4aSRandall Stewart 		}
1939f8829a4aSRandall Stewart 	}
1940f8829a4aSRandall Stewart 	/* put this address on the "pending/do not use yet" list */
19411b649582SRandall Stewart 	sctp_add_local_addr_restricted(stcb, ifa);
1942f8829a4aSRandall Stewart 	/*
1943f8829a4aSRandall Stewart 	 * check address scope if address is out of scope, don't queue
1944f8829a4aSRandall Stewart 	 * anything... note: this would leave the address on both inp and
1945f8829a4aSRandall Stewart 	 * asoc lists
1946f8829a4aSRandall Stewart 	 */
19475e2c2d87SRandall Stewart 	switch (ifa->address.sa.sa_family) {
19485e2c2d87SRandall Stewart #ifdef INET6
19495e2c2d87SRandall Stewart 	case AF_INET6:
19505e2c2d87SRandall Stewart 		{
1951f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6;
1952f8829a4aSRandall Stewart 
195342551e99SRandall Stewart 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1954f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1955f8829a4aSRandall Stewart 				/* we skip unspecifed addresses */
1956f8829a4aSRandall Stewart 				return;
1957f8829a4aSRandall Stewart 			}
1958f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1959f8829a4aSRandall Stewart 				if (stcb->asoc.local_scope == 0) {
1960f8829a4aSRandall Stewart 					return;
1961f8829a4aSRandall Stewart 				}
1962f8829a4aSRandall Stewart 				/* is it the right link local scope? */
196342551e99SRandall Stewart 				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1964f8829a4aSRandall Stewart 					return;
1965f8829a4aSRandall Stewart 				}
1966f8829a4aSRandall Stewart 			}
1967f8829a4aSRandall Stewart 			if (stcb->asoc.site_scope == 0 &&
1968f8829a4aSRandall Stewart 			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1969f8829a4aSRandall Stewart 				return;
1970f8829a4aSRandall Stewart 			}
19715e2c2d87SRandall Stewart 			break;
19725e2c2d87SRandall Stewart 		}
19735e2c2d87SRandall Stewart #endif
19745e2c2d87SRandall Stewart 	case AF_INET:
19755e2c2d87SRandall Stewart 		{
1976f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
1977f8829a4aSRandall Stewart 			struct in6pcb *inp6;
1978f8829a4aSRandall Stewart 
1979f8829a4aSRandall Stewart 			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1980f8829a4aSRandall Stewart 			/* invalid if we are a v6 only endpoint */
1981f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
198244b7479bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp6))
1983f8829a4aSRandall Stewart 				return;
1984f8829a4aSRandall Stewart 
198542551e99SRandall Stewart 			sin = (struct sockaddr_in *)&ifa->address.sa;
1986f8829a4aSRandall Stewart 			if (sin->sin_addr.s_addr == 0) {
1987f8829a4aSRandall Stewart 				/* we skip unspecifed addresses */
1988f8829a4aSRandall Stewart 				return;
1989f8829a4aSRandall Stewart 			}
1990f8829a4aSRandall Stewart 			if (stcb->asoc.ipv4_local_scope == 0 &&
1991f8829a4aSRandall Stewart 			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1992f8829a4aSRandall Stewart 				return;
1993f8829a4aSRandall Stewart 			}
19945e2c2d87SRandall Stewart 			break;
19955e2c2d87SRandall Stewart 		}
19965e2c2d87SRandall Stewart 	default:
1997f8829a4aSRandall Stewart 		/* else, not AF_INET or AF_INET6, so skip */
1998f8829a4aSRandall Stewart 		return;
1999f8829a4aSRandall Stewart 	}
2000f8829a4aSRandall Stewart 
2001f8829a4aSRandall Stewart 	/* queue an asconf for this address add/delete */
2002f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
2003f8829a4aSRandall Stewart 		/* does the peer do asconf? */
2004f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_asconf) {
2005f8829a4aSRandall Stewart 			/* queue an asconf for this addr */
2006f8829a4aSRandall Stewart 			status = sctp_asconf_queue_add(stcb, ifa, type);
20071b649582SRandall Stewart 
2008f8829a4aSRandall Stewart 			/*
20091b649582SRandall Stewart 			 * if queued ok, and in the open state, send out the
20101b649582SRandall Stewart 			 * ASCONF.  If in the non-open state, these will be
20111b649582SRandall Stewart 			 * sent when the state goes open.
2012f8829a4aSRandall Stewart 			 */
2013f8829a4aSRandall Stewart 			if (status == 0 &&
2014f8829a4aSRandall Stewart 			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
20151b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2016f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
2017f8829a4aSRandall Stewart 				    stcb, stcb->asoc.primary_destination);
20181b649582SRandall Stewart #else
20193232788eSRandall Stewart 				sctp_send_asconf(stcb, stcb->asoc.primary_destination,
20203232788eSRandall Stewart 				    addr_locked);
20211b649582SRandall Stewart #endif
2022f8829a4aSRandall Stewart 			}
2023f8829a4aSRandall Stewart 		}
2024f8829a4aSRandall Stewart 	}
2025f8829a4aSRandall Stewart }
2026f8829a4aSRandall Stewart 
202742551e99SRandall Stewart 
202842551e99SRandall Stewart int
20291b649582SRandall Stewart sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val)
2030f8829a4aSRandall Stewart {
203142551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
203242551e99SRandall Stewart 	struct sctp_ifa *ifa;
203342551e99SRandall Stewart 	struct sctp_laddr *l;
203442551e99SRandall Stewart 	int cnt_invalid = 0;
2035f8829a4aSRandall Stewart 
203642551e99SRandall Stewart 	asc = (struct sctp_asconf_iterator *)ptr;
203742551e99SRandall Stewart 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
203842551e99SRandall Stewart 		ifa = l->ifa;
203942551e99SRandall Stewart 		if (ifa->address.sa.sa_family == AF_INET6) {
2040f8829a4aSRandall Stewart 			/* invalid if we're not a v6 endpoint */
2041f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
204242551e99SRandall Stewart 				cnt_invalid++;
204342551e99SRandall Stewart 				if (asc->cnt == cnt_invalid)
204442551e99SRandall Stewart 					return (1);
204542551e99SRandall Stewart 				else
204642551e99SRandall Stewart 					continue;
2047f8829a4aSRandall Stewart 			}
204842551e99SRandall Stewart 		} else if (ifa->address.sa.sa_family == AF_INET) {
2049f8829a4aSRandall Stewart 			/* invalid if we are a v6 only endpoint */
2050f8829a4aSRandall Stewart 			struct in6pcb *inp6;
2051f8829a4aSRandall Stewart 
2052f8829a4aSRandall Stewart 			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2053f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
205444b7479bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp6)) {
205542551e99SRandall Stewart 				cnt_invalid++;
205642551e99SRandall Stewart 				if (asc->cnt == cnt_invalid)
205742551e99SRandall Stewart 					return (1);
205842551e99SRandall Stewart 				else
205942551e99SRandall Stewart 					continue;
2060f8829a4aSRandall Stewart 			}
2061f8829a4aSRandall Stewart 		} else {
2062f8829a4aSRandall Stewart 			/* invalid address family */
206342551e99SRandall Stewart 			cnt_invalid++;
206442551e99SRandall Stewart 			if (asc->cnt == cnt_invalid)
206542551e99SRandall Stewart 				return (1);
2066f8829a4aSRandall Stewart 			else
206742551e99SRandall Stewart 				continue;
2068f8829a4aSRandall Stewart 		}
206942551e99SRandall Stewart 	}
207042551e99SRandall Stewart 	return (0);
207142551e99SRandall Stewart }
2072f8829a4aSRandall Stewart 
20731b649582SRandall Stewart static int
20741b649582SRandall Stewart sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val)
207542551e99SRandall Stewart {
207642551e99SRandall Stewart 	struct sctp_ifa *ifa;
207742551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
207842551e99SRandall Stewart 	struct sctp_laddr *laddr, *nladdr, *l;
207942551e99SRandall Stewart 
208042551e99SRandall Stewart 	/* Only for specific case not bound all */
208142551e99SRandall Stewart 	asc = (struct sctp_asconf_iterator *)ptr;
208242551e99SRandall Stewart 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
208342551e99SRandall Stewart 		ifa = l->ifa;
208442551e99SRandall Stewart 		if (l->action == SCTP_ADD_IP_ADDRESS) {
20853c503c28SRandall Stewart 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
20863c503c28SRandall Stewart 			    sctp_nxt_addr) {
208742551e99SRandall Stewart 				if (laddr->ifa == ifa) {
208842551e99SRandall Stewart 					laddr->action = 0;
208942551e99SRandall Stewart 					break;
209042551e99SRandall Stewart 				}
209142551e99SRandall Stewart 			}
209242551e99SRandall Stewart 		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
20934a9ef3f8SMichael Tuexen 			LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
209442551e99SRandall Stewart 				/* remove only after all guys are done */
209542551e99SRandall Stewart 				if (laddr->ifa == ifa) {
209642551e99SRandall Stewart 					sctp_del_local_addr_ep(inp, ifa);
209742551e99SRandall Stewart 				}
209842551e99SRandall Stewart 			}
209942551e99SRandall Stewart 		}
210042551e99SRandall Stewart 	}
210142551e99SRandall Stewart 	return (0);
210242551e99SRandall Stewart }
210342551e99SRandall Stewart 
210442551e99SRandall Stewart void
21051b649582SRandall Stewart sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
21061b649582SRandall Stewart     void *ptr, uint32_t val)
210742551e99SRandall Stewart {
210842551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
210942551e99SRandall Stewart 	struct sctp_ifa *ifa;
211042551e99SRandall Stewart 	struct sctp_laddr *l;
211142551e99SRandall Stewart 	int cnt_invalid = 0;
211242551e99SRandall Stewart 	int type, status;
21131b649582SRandall Stewart 	int num_queued = 0;
211442551e99SRandall Stewart 
211542551e99SRandall Stewart 	asc = (struct sctp_asconf_iterator *)ptr;
211642551e99SRandall Stewart 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
211742551e99SRandall Stewart 		ifa = l->ifa;
211842551e99SRandall Stewart 		type = l->action;
211922a67197SRandall Stewart 
212022a67197SRandall Stewart 		/* address's vrf_id must be the vrf_id of the assoc */
212122a67197SRandall Stewart 		if (ifa->vrf_id != stcb->asoc.vrf_id) {
212222a67197SRandall Stewart 			continue;
212322a67197SRandall Stewart 		}
212442551e99SRandall Stewart 		/* Same checks again for assoc */
21255e2c2d87SRandall Stewart 		switch (ifa->address.sa.sa_family) {
21265e2c2d87SRandall Stewart #ifdef INET6
21275e2c2d87SRandall Stewart 		case AF_INET6:
21285e2c2d87SRandall Stewart 			{
212942551e99SRandall Stewart 				/* invalid if we're not a v6 endpoint */
213042551e99SRandall Stewart 				struct sockaddr_in6 *sin6;
213142551e99SRandall Stewart 
213242551e99SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
213342551e99SRandall Stewart 					cnt_invalid++;
213442551e99SRandall Stewart 					if (asc->cnt == cnt_invalid)
213542551e99SRandall Stewart 						return;
213642551e99SRandall Stewart 					else
213742551e99SRandall Stewart 						continue;
213842551e99SRandall Stewart 				}
213942551e99SRandall Stewart 				sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
214042551e99SRandall Stewart 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
214142551e99SRandall Stewart 					/* we skip unspecifed addresses */
214242551e99SRandall Stewart 					continue;
214342551e99SRandall Stewart 				}
214442551e99SRandall Stewart 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
214542551e99SRandall Stewart 					if (stcb->asoc.local_scope == 0) {
214642551e99SRandall Stewart 						continue;
214742551e99SRandall Stewart 					}
214842551e99SRandall Stewart 					/* is it the right link local scope? */
214942551e99SRandall Stewart 					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
215042551e99SRandall Stewart 						continue;
215142551e99SRandall Stewart 					}
215242551e99SRandall Stewart 				}
21535e2c2d87SRandall Stewart 				break;
21545e2c2d87SRandall Stewart 			}
21555e2c2d87SRandall Stewart #endif
21565e2c2d87SRandall Stewart 		case AF_INET:
21575e2c2d87SRandall Stewart 			{
215842551e99SRandall Stewart 				/* invalid if we are a v6 only endpoint */
215942551e99SRandall Stewart 				struct in6pcb *inp6;
216042551e99SRandall Stewart 				struct sockaddr_in *sin;
216142551e99SRandall Stewart 
216242551e99SRandall Stewart 				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
216342551e99SRandall Stewart 				/* invalid if we are a v6 only endpoint */
216442551e99SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
216542551e99SRandall Stewart 				    SCTP_IPV6_V6ONLY(inp6))
216642551e99SRandall Stewart 					continue;
216742551e99SRandall Stewart 
216842551e99SRandall Stewart 				sin = (struct sockaddr_in *)&ifa->address.sa;
216942551e99SRandall Stewart 				if (sin->sin_addr.s_addr == 0) {
217042551e99SRandall Stewart 					/* we skip unspecifed addresses */
217142551e99SRandall Stewart 					continue;
217242551e99SRandall Stewart 				}
217342551e99SRandall Stewart 				if (stcb->asoc.ipv4_local_scope == 0 &&
217442551e99SRandall Stewart 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2175c2ede4b3SMartin Blapp 					continue;
217642551e99SRandall Stewart 				}
217742551e99SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
217842551e99SRandall Stewart 				    SCTP_IPV6_V6ONLY(inp6)) {
217942551e99SRandall Stewart 					cnt_invalid++;
218042551e99SRandall Stewart 					if (asc->cnt == cnt_invalid)
218142551e99SRandall Stewart 						return;
218242551e99SRandall Stewart 					else
218342551e99SRandall Stewart 						continue;
218442551e99SRandall Stewart 				}
21855e2c2d87SRandall Stewart 				break;
21865e2c2d87SRandall Stewart 			}
21875e2c2d87SRandall Stewart 		default:
218842551e99SRandall Stewart 			/* invalid address family */
218942551e99SRandall Stewart 			cnt_invalid++;
219042551e99SRandall Stewart 			if (asc->cnt == cnt_invalid)
2191f8829a4aSRandall Stewart 				return;
219242551e99SRandall Stewart 			else
219342551e99SRandall Stewart 				continue;
21945e2c2d87SRandall Stewart 			break;
2195f8829a4aSRandall Stewart 		}
2196f8829a4aSRandall Stewart 
219742551e99SRandall Stewart 		if (type == SCTP_ADD_IP_ADDRESS) {
21981b649582SRandall Stewart 			/* prevent this address from being used as a source */
21991b649582SRandall Stewart 			sctp_add_local_addr_restricted(stcb, ifa);
220042551e99SRandall Stewart 		} else if (type == SCTP_DEL_IP_ADDRESS) {
2201f8829a4aSRandall Stewart 			struct sctp_nets *net;
2202f8829a4aSRandall Stewart 
2203f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
220417205eccSRandall Stewart 				sctp_rtentry_t *rt;
2205f8829a4aSRandall Stewart 
2206f8829a4aSRandall Stewart 				/* delete this address if cached */
2207851b7298SRandall Stewart 				if (net->ro._s_addr == ifa) {
220842551e99SRandall Stewart 					sctp_free_ifa(net->ro._s_addr);
220942551e99SRandall Stewart 					net->ro._s_addr = NULL;
221042551e99SRandall Stewart 					net->src_addr_selected = 0;
2211f8829a4aSRandall Stewart 					rt = net->ro.ro_rt;
221242551e99SRandall Stewart 					if (rt) {
221342551e99SRandall Stewart 						RTFREE(rt);
2214f8829a4aSRandall Stewart 						net->ro.ro_rt = NULL;
2215f8829a4aSRandall Stewart 					}
221642551e99SRandall Stewart 					/*
221742551e99SRandall Stewart 					 * Now we deleted our src address,
221842551e99SRandall Stewart 					 * should we not also now reset the
221942551e99SRandall Stewart 					 * cwnd/rto to start as if its a new
222042551e99SRandall Stewart 					 * address?
222142551e99SRandall Stewart 					 */
2222b54d3a6cSRandall Stewart 					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2223108df27cSRandall Stewart 					net->RTO = 0;
222442551e99SRandall Stewart 
2225f8829a4aSRandall Stewart 				}
2226f8829a4aSRandall Stewart 			}
222742551e99SRandall Stewart 		} else if (type == SCTP_SET_PRIM_ADDR) {
222842551e99SRandall Stewart 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
22291b649582SRandall Stewart 				/* must validate the ifa is in the ep */
223042551e99SRandall Stewart 				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
223142551e99SRandall Stewart 					continue;
2232f8829a4aSRandall Stewart 				}
223342551e99SRandall Stewart 			} else {
223442551e99SRandall Stewart 				/* Need to check scopes for this guy */
223542551e99SRandall Stewart 				if (sctp_is_address_in_scope(ifa,
223642551e99SRandall Stewart 				    stcb->asoc.ipv4_addr_legal,
223742551e99SRandall Stewart 				    stcb->asoc.ipv6_addr_legal,
223842551e99SRandall Stewart 				    stcb->asoc.loopback_scope,
223942551e99SRandall Stewart 				    stcb->asoc.ipv4_local_scope,
224042551e99SRandall Stewart 				    stcb->asoc.local_scope,
224142551e99SRandall Stewart 				    stcb->asoc.site_scope, 0) == 0) {
224242551e99SRandall Stewart 					continue;
2243f8829a4aSRandall Stewart 				}
224442551e99SRandall Stewart 			}
224542551e99SRandall Stewart 		}
224642551e99SRandall Stewart 		/* queue an asconf for this address add/delete */
22471b649582SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
22481b649582SRandall Stewart 		    stcb->asoc.peer_supports_asconf) {
224942551e99SRandall Stewart 			/* queue an asconf for this addr */
225042551e99SRandall Stewart 			status = sctp_asconf_queue_add(stcb, ifa, type);
225142551e99SRandall Stewart 			/*
22521b649582SRandall Stewart 			 * if queued ok, and in the open state, update the
22531b649582SRandall Stewart 			 * count of queued params.  If in the non-open
22541b649582SRandall Stewart 			 * state, these get sent when the assoc goes open.
225542551e99SRandall Stewart 			 */
22561b649582SRandall Stewart 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
22571b649582SRandall Stewart 				if (status >= 0) {
22581b649582SRandall Stewart 					num_queued++;
225942551e99SRandall Stewart 				}
226042551e99SRandall Stewart 			}
226142551e99SRandall Stewart 		}
226242551e99SRandall Stewart 	}
22631b649582SRandall Stewart 	/*
22641b649582SRandall Stewart 	 * If we have queued params in the open state, send out an ASCONF.
22651b649582SRandall Stewart 	 */
22661b649582SRandall Stewart 	if (num_queued > 0) {
22673232788eSRandall Stewart 		sctp_send_asconf(stcb, stcb->asoc.primary_destination,
22683232788eSRandall Stewart 		    SCTP_ADDR_NOT_LOCKED);
22691b649582SRandall Stewart 	}
227042551e99SRandall Stewart }
227142551e99SRandall Stewart 
227242551e99SRandall Stewart void
22731b649582SRandall Stewart sctp_asconf_iterator_end(void *ptr, uint32_t val)
227442551e99SRandall Stewart {
227542551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
227642551e99SRandall Stewart 	struct sctp_ifa *ifa;
22774a9ef3f8SMichael Tuexen 	struct sctp_laddr *l, *nl;
227842551e99SRandall Stewart 
227942551e99SRandall Stewart 	asc = (struct sctp_asconf_iterator *)ptr;
22804a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
228142551e99SRandall Stewart 		ifa = l->ifa;
228242551e99SRandall Stewart 		if (l->action == SCTP_ADD_IP_ADDRESS) {
228342551e99SRandall Stewart 			/* Clear the defer use flag */
228442551e99SRandall Stewart 			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
228542551e99SRandall Stewart 		}
228642551e99SRandall Stewart 		sctp_free_ifa(ifa);
2287b3f1ea41SRandall Stewart 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
228842551e99SRandall Stewart 		SCTP_DECR_LADDR_COUNT();
228942551e99SRandall Stewart 	}
2290207304d4SRandall Stewart 	SCTP_FREE(asc, SCTP_M_ASC_IT);
2291f8829a4aSRandall Stewart }
2292f8829a4aSRandall Stewart 
2293f8829a4aSRandall Stewart /*
22941b649582SRandall Stewart  * sa is the sockaddr to ask the peer to set primary to.
22951b649582SRandall Stewart  * returns: 0 = completed, -1 = error
2296f8829a4aSRandall Stewart  */
2297c4739e2fSRandall Stewart int32_t
2298f8829a4aSRandall Stewart sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2299f8829a4aSRandall Stewart {
23003232788eSRandall Stewart 	uint32_t vrf_id;
23013232788eSRandall Stewart 	struct sctp_ifa *ifa;
2302f8829a4aSRandall Stewart 
23033232788eSRandall Stewart 	/* find the ifa for the desired set primary */
23043232788eSRandall Stewart 	vrf_id = stcb->asoc.vrf_id;
23053232788eSRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
23063232788eSRandall Stewart 	if (ifa == NULL) {
23073232788eSRandall Stewart 		/* Invalid address */
23083232788eSRandall Stewart 		return (-1);
23093232788eSRandall Stewart 	}
2310f8829a4aSRandall Stewart 	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
23113232788eSRandall Stewart 	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2312f8829a4aSRandall Stewart 		/* set primary queuing succeeded */
2313ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2314ad81507eSRandall Stewart 		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2315f8829a4aSRandall Stewart 		    stcb);
2316ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
23171b649582SRandall Stewart 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
23181b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
23191b649582SRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
23201b649582SRandall Stewart 			    stcb->sctp_ep, stcb,
23211b649582SRandall Stewart 			    stcb->asoc.primary_destination);
23221b649582SRandall Stewart #else
23233232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
23243232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
23251b649582SRandall Stewart #endif
23261b649582SRandall Stewart 		}
2327f8829a4aSRandall Stewart 	} else {
2328ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2329f8829a4aSRandall Stewart 		    stcb);
2330ad81507eSRandall Stewart 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2331f8829a4aSRandall Stewart 		return (-1);
2332f8829a4aSRandall Stewart 	}
2333f8829a4aSRandall Stewart 	return (0);
2334f8829a4aSRandall Stewart }
2335f8829a4aSRandall Stewart 
2336f8829a4aSRandall Stewart void
233742551e99SRandall Stewart sctp_set_primary_ip_address(struct sctp_ifa *ifa)
2338f8829a4aSRandall Stewart {
2339f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
2340f8829a4aSRandall Stewart 
2341f8829a4aSRandall Stewart 	/* go through all our PCB's */
2342b3f1ea41SRandall Stewart 	LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
2343f8829a4aSRandall Stewart 		struct sctp_tcb *stcb;
2344f8829a4aSRandall Stewart 
2345f8829a4aSRandall Stewart 		/* process for all associations for this endpoint */
2346f8829a4aSRandall Stewart 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2347f8829a4aSRandall Stewart 			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2348f8829a4aSRandall Stewart 			if (!sctp_asconf_queue_add(stcb, ifa,
2349f8829a4aSRandall Stewart 			    SCTP_SET_PRIM_ADDR)) {
2350f8829a4aSRandall Stewart 				/* set primary queuing succeeded */
2351ceaad40aSRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
2352f8829a4aSRandall Stewart 				    stcb);
2353ad81507eSRandall Stewart 				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
23541b649582SRandall Stewart 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
23551b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
23561b649582SRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
23571b649582SRandall Stewart 					    stcb->sctp_ep, stcb,
23581b649582SRandall Stewart 					    stcb->asoc.primary_destination);
23591b649582SRandall Stewart #else
23603232788eSRandall Stewart 					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
23613232788eSRandall Stewart 					    SCTP_ADDR_NOT_LOCKED);
23621b649582SRandall Stewart #endif
23631b649582SRandall Stewart 				}
2364f8829a4aSRandall Stewart 			}
2365f8829a4aSRandall Stewart 		}		/* for each stcb */
2366f8829a4aSRandall Stewart 	}			/* for each inp */
2367f8829a4aSRandall Stewart }
2368f8829a4aSRandall Stewart 
2369c54a18d2SRandall Stewart int
2370c54a18d2SRandall Stewart sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2371c54a18d2SRandall Stewart {
2372c54a18d2SRandall Stewart 	struct sctp_tmit_chunk *chk, *nchk;
2373c54a18d2SRandall Stewart 	unsigned int offset, asconf_limit;
2374c54a18d2SRandall Stewart 	struct sctp_asconf_chunk *acp;
2375c54a18d2SRandall Stewart 	struct sctp_asconf_paramhdr *aph;
2376c54a18d2SRandall Stewart 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2377c54a18d2SRandall Stewart 	struct sctp_ipv6addr_param *p_addr;
2378c54a18d2SRandall Stewart 	int add_cnt, del_cnt;
2379c54a18d2SRandall Stewart 	uint16_t last_param_type;
2380c54a18d2SRandall Stewart 
2381c54a18d2SRandall Stewart 	add_cnt = del_cnt = 0;
2382c54a18d2SRandall Stewart 	last_param_type = 0;
23834a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
2384c54a18d2SRandall Stewart 		if (chk->data == NULL) {
2385c54a18d2SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2386c54a18d2SRandall Stewart 			continue;
2387c54a18d2SRandall Stewart 		}
2388c54a18d2SRandall Stewart 		offset = 0;
2389c54a18d2SRandall Stewart 		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2390c54a18d2SRandall Stewart 		offset += sizeof(struct sctp_asconf_chunk);
2391c54a18d2SRandall Stewart 		asconf_limit = ntohs(acp->ch.chunk_length);
2392c54a18d2SRandall Stewart 		p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2393c54a18d2SRandall Stewart 		if (p_addr == NULL) {
2394c54a18d2SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2395c54a18d2SRandall Stewart 			continue;
2396c54a18d2SRandall Stewart 		}
2397c54a18d2SRandall Stewart 		offset += ntohs(p_addr->ph.param_length);
2398c54a18d2SRandall Stewart 
2399c54a18d2SRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2400c54a18d2SRandall Stewart 		if (aph == NULL) {
2401c54a18d2SRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2402c54a18d2SRandall Stewart 			continue;
2403c54a18d2SRandall Stewart 		}
2404c54a18d2SRandall Stewart 		while (aph != NULL) {
2405c54a18d2SRandall Stewart 			unsigned int param_length, param_type;
2406c54a18d2SRandall Stewart 
2407c54a18d2SRandall Stewart 			param_type = ntohs(aph->ph.param_type);
2408c54a18d2SRandall Stewart 			param_length = ntohs(aph->ph.param_length);
2409c54a18d2SRandall Stewart 			if (offset + param_length > asconf_limit) {
2410c54a18d2SRandall Stewart 				/* parameter goes beyond end of chunk! */
2411c54a18d2SRandall Stewart 				break;
2412c54a18d2SRandall Stewart 			}
2413c54a18d2SRandall Stewart 			if (param_length > sizeof(aparam_buf)) {
2414c54a18d2SRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2415c54a18d2SRandall Stewart 				break;
2416c54a18d2SRandall Stewart 			}
2417c54a18d2SRandall Stewart 			if (param_length <= sizeof(struct sctp_paramhdr)) {
2418c54a18d2SRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2419c54a18d2SRandall Stewart 				break;
2420c54a18d2SRandall Stewart 			}
2421c54a18d2SRandall Stewart 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2422c54a18d2SRandall Stewart 			if (aph == NULL) {
2423c54a18d2SRandall Stewart 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2424c54a18d2SRandall Stewart 				break;
2425c54a18d2SRandall Stewart 			}
2426c54a18d2SRandall Stewart 			p_addr = (struct sctp_ipv6addr_param *)(aph + 1);
2427c54a18d2SRandall Stewart 			if (sctp_addr_match(p_addr, &sctp_ifa->address.sa) != 0) {
2428c54a18d2SRandall Stewart 				switch (param_type) {
2429c54a18d2SRandall Stewart 				case SCTP_ADD_IP_ADDRESS:
2430c54a18d2SRandall Stewart 					add_cnt++;
2431c54a18d2SRandall Stewart 					break;
2432c54a18d2SRandall Stewart 				case SCTP_DEL_IP_ADDRESS:
2433c54a18d2SRandall Stewart 					del_cnt++;
2434c54a18d2SRandall Stewart 					break;
2435c54a18d2SRandall Stewart 				default:
2436c54a18d2SRandall Stewart 					break;
2437c54a18d2SRandall Stewart 				}
2438c54a18d2SRandall Stewart 				last_param_type = param_type;
2439c54a18d2SRandall Stewart 			}
2440c54a18d2SRandall Stewart 			offset += SCTP_SIZE32(param_length);
2441c54a18d2SRandall Stewart 			if (offset >= asconf_limit) {
2442c54a18d2SRandall Stewart 				/* no more data in the mbuf chain */
2443c54a18d2SRandall Stewart 				break;
2444c54a18d2SRandall Stewart 			}
2445c54a18d2SRandall Stewart 			/* get pointer to next asconf param */
2446c54a18d2SRandall Stewart 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2447c54a18d2SRandall Stewart 		}
2448c54a18d2SRandall Stewart 	}
2449c54a18d2SRandall Stewart 
2450c54a18d2SRandall Stewart 	/*
2451c54a18d2SRandall Stewart 	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2452c54a18d2SRandall Stewart 	 * or DEL -> ADD
2453c54a18d2SRandall Stewart 	 */
2454c54a18d2SRandall Stewart 	if (add_cnt > del_cnt ||
2455c54a18d2SRandall Stewart 	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2456c54a18d2SRandall Stewart 		return 1;
2457c54a18d2SRandall Stewart 	}
2458c54a18d2SRandall Stewart 	return 0;
2459c54a18d2SRandall Stewart }
2460c54a18d2SRandall Stewart 
2461f8829a4aSRandall Stewart static struct sockaddr *
24623232788eSRandall Stewart sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2463f8829a4aSRandall Stewart {
246442551e99SRandall Stewart 	struct sctp_vrf *vrf = NULL;
246542551e99SRandall Stewart 	struct sctp_ifn *sctp_ifn;
246642551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
2467f8829a4aSRandall Stewart 
24683232788eSRandall Stewart 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2469c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
247042551e99SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2471ad81507eSRandall Stewart 	if (vrf == NULL) {
24723232788eSRandall Stewart 		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2473c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
2474ad81507eSRandall Stewart 		return (NULL);
2475ad81507eSRandall Stewart 	}
247642551e99SRandall Stewart 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
247742551e99SRandall Stewart 		if (stcb->asoc.loopback_scope == 0 &&
247842551e99SRandall Stewart 		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2479f8829a4aSRandall Stewart 			/* Skip if loopback_scope not set */
2480f8829a4aSRandall Stewart 			continue;
2481f8829a4aSRandall Stewart 		}
248242551e99SRandall Stewart 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
248342551e99SRandall Stewart 			if (sctp_ifa->address.sa.sa_family == AF_INET &&
2484f8829a4aSRandall Stewart 			    stcb->asoc.ipv4_addr_legal) {
2485f8829a4aSRandall Stewart 				struct sockaddr_in *sin;
2486f8829a4aSRandall Stewart 
248742551e99SRandall Stewart 				sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
2488f8829a4aSRandall Stewart 				if (sin->sin_addr.s_addr == 0) {
2489f8829a4aSRandall Stewart 					/* skip unspecifed addresses */
2490f8829a4aSRandall Stewart 					continue;
2491f8829a4aSRandall Stewart 				}
2492f8829a4aSRandall Stewart 				if (stcb->asoc.ipv4_local_scope == 0 &&
2493f8829a4aSRandall Stewart 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2494f8829a4aSRandall Stewart 					continue;
2495f8829a4aSRandall Stewart 
2496c54a18d2SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2497c54a18d2SRandall Stewart 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2498f8829a4aSRandall Stewart 					continue;
2499f8829a4aSRandall Stewart 				/* found a valid local v4 address to use */
25003232788eSRandall Stewart 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2501c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
250242551e99SRandall Stewart 				return (&sctp_ifa->address.sa);
250342551e99SRandall Stewart 			} else if (sctp_ifa->address.sa.sa_family == AF_INET6 &&
2504f8829a4aSRandall Stewart 			    stcb->asoc.ipv6_addr_legal) {
2505f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin6;
2506f8829a4aSRandall Stewart 
250742551e99SRandall Stewart 				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2508f8829a4aSRandall Stewart 					continue;
250942551e99SRandall Stewart 				}
251042551e99SRandall Stewart 				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
2511f8829a4aSRandall Stewart 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2512f8829a4aSRandall Stewart 					/* we skip unspecifed addresses */
2513f8829a4aSRandall Stewart 					continue;
2514f8829a4aSRandall Stewart 				}
2515f8829a4aSRandall Stewart 				if (stcb->asoc.local_scope == 0 &&
2516f8829a4aSRandall Stewart 				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2517f8829a4aSRandall Stewart 					continue;
2518f8829a4aSRandall Stewart 				if (stcb->asoc.site_scope == 0 &&
2519f8829a4aSRandall Stewart 				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2520f8829a4aSRandall Stewart 					continue;
2521f8829a4aSRandall Stewart 
2522c54a18d2SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2523c54a18d2SRandall Stewart 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2524c54a18d2SRandall Stewart 					continue;
2525f8829a4aSRandall Stewart 				/* found a valid local v6 address to use */
25263232788eSRandall Stewart 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2527c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
252842551e99SRandall Stewart 				return (&sctp_ifa->address.sa);
2529f8829a4aSRandall Stewart 			}
2530f8829a4aSRandall Stewart 		}
2531f8829a4aSRandall Stewart 	}
2532f8829a4aSRandall Stewart 	/* no valid addresses found */
25333232788eSRandall Stewart 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2534c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
2535f8829a4aSRandall Stewart 	return (NULL);
2536f8829a4aSRandall Stewart }
2537f8829a4aSRandall Stewart 
2538f8829a4aSRandall Stewart static struct sockaddr *
2539f8829a4aSRandall Stewart sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2540f8829a4aSRandall Stewart {
2541f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
2542f8829a4aSRandall Stewart 
2543f8829a4aSRandall Stewart 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2544f8829a4aSRandall Stewart 		if (laddr->ifa == NULL) {
2545f8829a4aSRandall Stewart 			continue;
2546f8829a4aSRandall Stewart 		}
2547f8829a4aSRandall Stewart 		/* is the address restricted ? */
2548c54a18d2SRandall Stewart 		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2549c54a18d2SRandall Stewart 		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2550f8829a4aSRandall Stewart 			continue;
2551f8829a4aSRandall Stewart 
2552f8829a4aSRandall Stewart 		/* found a valid local address to use */
255342551e99SRandall Stewart 		return (&laddr->ifa->address.sa);
2554f8829a4aSRandall Stewart 	}
2555f8829a4aSRandall Stewart 	/* no valid addresses found */
2556f8829a4aSRandall Stewart 	return (NULL);
2557f8829a4aSRandall Stewart }
2558f8829a4aSRandall Stewart 
2559f8829a4aSRandall Stewart /*
256018e198d3SRandall Stewart  * builds an ASCONF chunk from queued ASCONF params.
256118e198d3SRandall Stewart  * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2562f8829a4aSRandall Stewart  */
2563f8829a4aSRandall Stewart struct mbuf *
25643232788eSRandall Stewart sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2565f8829a4aSRandall Stewart {
2566f8829a4aSRandall Stewart 	struct mbuf *m_asconf, *m_asconf_chk;
2567f8829a4aSRandall Stewart 	struct sctp_asconf_addr *aa;
2568f8829a4aSRandall Stewart 	struct sctp_asconf_chunk *acp;
2569f8829a4aSRandall Stewart 	struct sctp_asconf_paramhdr *aph;
2570f8829a4aSRandall Stewart 	struct sctp_asconf_addr_param *aap;
2571f8829a4aSRandall Stewart 	uint32_t p_length;
2572f8829a4aSRandall Stewart 	uint32_t correlation_id = 1;	/* 0 is reserved... */
2573f8829a4aSRandall Stewart 	caddr_t ptr, lookup_ptr;
2574f8829a4aSRandall Stewart 	uint8_t lookup_used = 0;
2575f8829a4aSRandall Stewart 
2576f8829a4aSRandall Stewart 	/* are there any asconf params to send? */
2577c54a18d2SRandall Stewart 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2578c54a18d2SRandall Stewart 		if (aa->sent == 0)
2579c54a18d2SRandall Stewart 			break;
2580f8829a4aSRandall Stewart 	}
2581c54a18d2SRandall Stewart 	if (aa == NULL)
25821b649582SRandall Stewart 		return (NULL);
2583c54a18d2SRandall Stewart 
2584f8829a4aSRandall Stewart 	/*
2585f8829a4aSRandall Stewart 	 * get a chunk header mbuf and a cluster for the asconf params since
2586f8829a4aSRandall Stewart 	 * it's simpler to fill in the asconf chunk header lookup address on
2587f8829a4aSRandall Stewart 	 * the fly
2588f8829a4aSRandall Stewart 	 */
2589139bc87fSRandall Stewart 	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2590f8829a4aSRandall Stewart 	if (m_asconf_chk == NULL) {
2591f8829a4aSRandall Stewart 		/* no mbuf's */
2592ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2593ad81507eSRandall Stewart 		    "compose_asconf: couldn't get chunk mbuf!\n");
2594f8829a4aSRandall Stewart 		return (NULL);
2595f8829a4aSRandall Stewart 	}
2596139bc87fSRandall Stewart 	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2597f8829a4aSRandall Stewart 	if (m_asconf == NULL) {
2598f8829a4aSRandall Stewart 		/* no mbuf's */
2599ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2600ad81507eSRandall Stewart 		    "compose_asconf: couldn't get mbuf!\n");
2601f8829a4aSRandall Stewart 		sctp_m_freem(m_asconf_chk);
2602f8829a4aSRandall Stewart 		return (NULL);
2603f8829a4aSRandall Stewart 	}
2604139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2605139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_asconf) = 0;
2606f8829a4aSRandall Stewart 	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2607f8829a4aSRandall Stewart 	bzero(acp, sizeof(struct sctp_asconf_chunk));
2608f8829a4aSRandall Stewart 	/* save pointers to lookup address and asconf params */
2609f8829a4aSRandall Stewart 	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2610f8829a4aSRandall Stewart 	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2611f8829a4aSRandall Stewart 
2612f8829a4aSRandall Stewart 	/* fill in chunk header info */
2613f8829a4aSRandall Stewart 	acp->ch.chunk_type = SCTP_ASCONF;
2614f8829a4aSRandall Stewart 	acp->ch.chunk_flags = 0;
2615f8829a4aSRandall Stewart 	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2616c54a18d2SRandall Stewart 	stcb->asoc.asconf_seq_out++;
2617f8829a4aSRandall Stewart 
2618f8829a4aSRandall Stewart 	/* add parameters... up to smallest MTU allowed */
2619f8829a4aSRandall Stewart 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2620c54a18d2SRandall Stewart 		if (aa->sent)
2621c54a18d2SRandall Stewart 			continue;
2622f8829a4aSRandall Stewart 		/* get the parameter length */
2623f8829a4aSRandall Stewart 		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2624f8829a4aSRandall Stewart 		/* will it fit in current chunk? */
2625139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) {
2626f8829a4aSRandall Stewart 			/* won't fit, so we're done with this chunk */
2627f8829a4aSRandall Stewart 			break;
2628f8829a4aSRandall Stewart 		}
2629f8829a4aSRandall Stewart 		/* assign (and store) a correlation id */
2630f8829a4aSRandall Stewart 		aa->ap.aph.correlation_id = correlation_id++;
2631f8829a4aSRandall Stewart 
2632f8829a4aSRandall Stewart 		/*
2633f8829a4aSRandall Stewart 		 * fill in address if we're doing a delete this is a simple
2634f8829a4aSRandall Stewart 		 * way for us to fill in the correlation address, which
2635f8829a4aSRandall Stewart 		 * should only be used by the peer if we're deleting our
2636f8829a4aSRandall Stewart 		 * source address and adding a new address (e.g. renumbering
2637f8829a4aSRandall Stewart 		 * case)
2638f8829a4aSRandall Stewart 		 */
2639f8829a4aSRandall Stewart 		if (lookup_used == 0 &&
2640830d754dSRandall Stewart 		    (aa->special_del == 0) &&
2641f8829a4aSRandall Stewart 		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2642f8829a4aSRandall Stewart 			struct sctp_ipv6addr_param *lookup;
2643f8829a4aSRandall Stewart 			uint16_t p_size, addr_size;
2644f8829a4aSRandall Stewart 
2645f8829a4aSRandall Stewart 			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2646f8829a4aSRandall Stewart 			lookup->ph.param_type =
2647f8829a4aSRandall Stewart 			    htons(aa->ap.addrp.ph.param_type);
2648f8829a4aSRandall Stewart 			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2649f8829a4aSRandall Stewart 				/* copy IPv6 address */
2650f8829a4aSRandall Stewart 				p_size = sizeof(struct sctp_ipv6addr_param);
2651f8829a4aSRandall Stewart 				addr_size = sizeof(struct in6_addr);
2652f8829a4aSRandall Stewart 			} else {
2653f8829a4aSRandall Stewart 				/* copy IPv4 address */
2654f8829a4aSRandall Stewart 				p_size = sizeof(struct sctp_ipv4addr_param);
2655f8829a4aSRandall Stewart 				addr_size = sizeof(struct in_addr);
2656f8829a4aSRandall Stewart 			}
2657f8829a4aSRandall Stewart 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2658f8829a4aSRandall Stewart 			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2659139bc87fSRandall Stewart 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2660f8829a4aSRandall Stewart 			lookup_used = 1;
2661f8829a4aSRandall Stewart 		}
2662f8829a4aSRandall Stewart 		/* copy into current space */
2663f8829a4aSRandall Stewart 		memcpy(ptr, &aa->ap, p_length);
2664f8829a4aSRandall Stewart 
2665f8829a4aSRandall Stewart 		/* network elements and update lengths */
2666f8829a4aSRandall Stewart 		aph = (struct sctp_asconf_paramhdr *)ptr;
2667f8829a4aSRandall Stewart 		aap = (struct sctp_asconf_addr_param *)ptr;
2668f8829a4aSRandall Stewart 		/* correlation_id is transparent to peer, no htonl needed */
2669f8829a4aSRandall Stewart 		aph->ph.param_type = htons(aph->ph.param_type);
2670f8829a4aSRandall Stewart 		aph->ph.param_length = htons(aph->ph.param_length);
2671f8829a4aSRandall Stewart 		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2672f8829a4aSRandall Stewart 		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2673f8829a4aSRandall Stewart 
2674139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2675f8829a4aSRandall Stewart 		ptr += SCTP_SIZE32(p_length);
2676f8829a4aSRandall Stewart 
2677f8829a4aSRandall Stewart 		/*
2678f8829a4aSRandall Stewart 		 * these params are removed off the pending list upon
2679f8829a4aSRandall Stewart 		 * getting an ASCONF-ACK back from the peer, just set flag
2680f8829a4aSRandall Stewart 		 */
2681f8829a4aSRandall Stewart 		aa->sent = 1;
2682f8829a4aSRandall Stewart 	}
2683f8829a4aSRandall Stewart 	/* check to see if the lookup addr has been populated yet */
2684f8829a4aSRandall Stewart 	if (lookup_used == 0) {
2685f8829a4aSRandall Stewart 		/* NOTE: if the address param is optional, can skip this... */
2686f8829a4aSRandall Stewart 		/* add any valid (existing) address... */
2687f8829a4aSRandall Stewart 		struct sctp_ipv6addr_param *lookup;
2688f8829a4aSRandall Stewart 		uint16_t p_size, addr_size;
2689f8829a4aSRandall Stewart 		struct sockaddr *found_addr;
2690f8829a4aSRandall Stewart 		caddr_t addr_ptr;
2691f8829a4aSRandall Stewart 
2692f8829a4aSRandall Stewart 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
26933232788eSRandall Stewart 			found_addr = sctp_find_valid_localaddr(stcb,
26943232788eSRandall Stewart 			    addr_locked);
2695f8829a4aSRandall Stewart 		else
2696f8829a4aSRandall Stewart 			found_addr = sctp_find_valid_localaddr_ep(stcb);
2697f8829a4aSRandall Stewart 
2698f8829a4aSRandall Stewart 		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2699f8829a4aSRandall Stewart 		if (found_addr != NULL) {
2700f8829a4aSRandall Stewart 			if (found_addr->sa_family == AF_INET6) {
2701f8829a4aSRandall Stewart 				/* copy IPv6 address */
2702f8829a4aSRandall Stewart 				lookup->ph.param_type =
2703f8829a4aSRandall Stewart 				    htons(SCTP_IPV6_ADDRESS);
2704f8829a4aSRandall Stewart 				p_size = sizeof(struct sctp_ipv6addr_param);
2705f8829a4aSRandall Stewart 				addr_size = sizeof(struct in6_addr);
2706f8829a4aSRandall Stewart 				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2707f8829a4aSRandall Stewart 				    found_addr)->sin6_addr;
2708f8829a4aSRandall Stewart 			} else {
2709f8829a4aSRandall Stewart 				/* copy IPv4 address */
2710f8829a4aSRandall Stewart 				lookup->ph.param_type =
2711f8829a4aSRandall Stewart 				    htons(SCTP_IPV4_ADDRESS);
2712f8829a4aSRandall Stewart 				p_size = sizeof(struct sctp_ipv4addr_param);
2713f8829a4aSRandall Stewart 				addr_size = sizeof(struct in_addr);
2714f8829a4aSRandall Stewart 				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2715f8829a4aSRandall Stewart 				    found_addr)->sin_addr;
2716f8829a4aSRandall Stewart 			}
2717f8829a4aSRandall Stewart 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2718f8829a4aSRandall Stewart 			memcpy(lookup->addr, addr_ptr, addr_size);
2719139bc87fSRandall Stewart 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2720f8829a4aSRandall Stewart 			lookup_used = 1;
2721f8829a4aSRandall Stewart 		} else {
2722f8829a4aSRandall Stewart 			/* uh oh... don't have any address?? */
2723ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1,
2724ad81507eSRandall Stewart 			    "compose_asconf: no lookup addr!\n");
2725f8829a4aSRandall Stewart 			/* for now, we send a IPv4 address of 0.0.0.0 */
2726f8829a4aSRandall Stewart 			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2727f8829a4aSRandall Stewart 			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2728f8829a4aSRandall Stewart 			bzero(lookup->addr, sizeof(struct in_addr));
2729139bc87fSRandall Stewart 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2730f8829a4aSRandall Stewart 			lookup_used = 1;
2731f8829a4aSRandall Stewart 		}
2732f8829a4aSRandall Stewart 	}
2733f8829a4aSRandall Stewart 	/* chain it all together */
2734139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
27353c503c28SRandall Stewart 	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
27363c503c28SRandall Stewart 	acp->ch.chunk_length = ntohs(*retlen);
2737f8829a4aSRandall Stewart 
2738f8829a4aSRandall Stewart 	return (m_asconf_chk);
2739f8829a4aSRandall Stewart }
2740f8829a4aSRandall Stewart 
2741f8829a4aSRandall Stewart /*
2742f8829a4aSRandall Stewart  * section to handle address changes before an association is up eg. changes
2743f8829a4aSRandall Stewart  * during INIT/INIT-ACK/COOKIE-ECHO handshake
2744f8829a4aSRandall Stewart  */
2745f8829a4aSRandall Stewart 
2746f8829a4aSRandall Stewart /*
2747f8829a4aSRandall Stewart  * processes the (local) addresses in the INIT-ACK chunk
2748f8829a4aSRandall Stewart  */
2749f8829a4aSRandall Stewart static void
2750f8829a4aSRandall Stewart sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2751f8829a4aSRandall Stewart     unsigned int offset, unsigned int length)
2752f8829a4aSRandall Stewart {
2753f8829a4aSRandall Stewart 	struct sctp_paramhdr tmp_param, *ph;
2754f8829a4aSRandall Stewart 	uint16_t plen, ptype;
275542551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
2756f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param addr_store;
2757f8829a4aSRandall Stewart 	struct sockaddr_in6 sin6;
2758f8829a4aSRandall Stewart 	struct sockaddr_in sin;
2759f8829a4aSRandall Stewart 	struct sockaddr *sa;
276042551e99SRandall Stewart 	uint32_t vrf_id;
2761f8829a4aSRandall Stewart 
2762ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2763ad81507eSRandall Stewart 	if (stcb == NULL)	/* Un-needed check for SA */
2764ad81507eSRandall Stewart 		return;
2765f8829a4aSRandall Stewart 
2766f8829a4aSRandall Stewart 	/* convert to upper bound */
2767f8829a4aSRandall Stewart 	length += offset;
2768f8829a4aSRandall Stewart 
2769f8829a4aSRandall Stewart 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2770f8829a4aSRandall Stewart 		return;
2771f8829a4aSRandall Stewart 	}
2772f8829a4aSRandall Stewart 	/* init the addresses */
2773f8829a4aSRandall Stewart 	bzero(&sin6, sizeof(sin6));
2774f8829a4aSRandall Stewart 	sin6.sin6_family = AF_INET6;
2775f8829a4aSRandall Stewart 	sin6.sin6_len = sizeof(sin6);
2776f8829a4aSRandall Stewart 	sin6.sin6_port = stcb->rport;
2777f8829a4aSRandall Stewart 
2778f8829a4aSRandall Stewart 	bzero(&sin, sizeof(sin));
2779f8829a4aSRandall Stewart 	sin.sin_len = sizeof(sin);
2780f8829a4aSRandall Stewart 	sin.sin_family = AF_INET;
2781f8829a4aSRandall Stewart 	sin.sin_port = stcb->rport;
2782f8829a4aSRandall Stewart 
2783f8829a4aSRandall Stewart 	/* go through the addresses in the init-ack */
2784f8829a4aSRandall Stewart 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2785f8829a4aSRandall Stewart 	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2786f8829a4aSRandall Stewart 	while (ph != NULL) {
2787f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
2788f8829a4aSRandall Stewart 		plen = ntohs(ph->param_length);
2789f8829a4aSRandall Stewart 		if (ptype == SCTP_IPV6_ADDRESS) {
2790f8829a4aSRandall Stewart 			struct sctp_ipv6addr_param *a6p;
2791f8829a4aSRandall Stewart 
2792f8829a4aSRandall Stewart 			/* get the entire IPv6 address param */
2793f8829a4aSRandall Stewart 			a6p = (struct sctp_ipv6addr_param *)
2794f8829a4aSRandall Stewart 			    sctp_m_getptr(m, offset,
2795f8829a4aSRandall Stewart 			    sizeof(struct sctp_ipv6addr_param),
2796f8829a4aSRandall Stewart 			    (uint8_t *) & addr_store);
2797f8829a4aSRandall Stewart 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2798f8829a4aSRandall Stewart 			    a6p == NULL) {
2799f8829a4aSRandall Stewart 				return;
2800f8829a4aSRandall Stewart 			}
2801f8829a4aSRandall Stewart 			memcpy(&sin6.sin6_addr, a6p->addr,
2802f8829a4aSRandall Stewart 			    sizeof(struct in6_addr));
2803f8829a4aSRandall Stewart 			sa = (struct sockaddr *)&sin6;
2804f8829a4aSRandall Stewart 		} else if (ptype == SCTP_IPV4_ADDRESS) {
2805f8829a4aSRandall Stewart 			struct sctp_ipv4addr_param *a4p;
2806f8829a4aSRandall Stewart 
2807f8829a4aSRandall Stewart 			/* get the entire IPv4 address param */
280842551e99SRandall Stewart 			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
280942551e99SRandall Stewart 			    sizeof(struct sctp_ipv4addr_param),
281042551e99SRandall Stewart 			    (uint8_t *) & addr_store);
2811f8829a4aSRandall Stewart 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2812f8829a4aSRandall Stewart 			    a4p == NULL) {
2813f8829a4aSRandall Stewart 				return;
2814f8829a4aSRandall Stewart 			}
2815f8829a4aSRandall Stewart 			sin.sin_addr.s_addr = a4p->addr;
2816f8829a4aSRandall Stewart 			sa = (struct sockaddr *)&sin;
2817f8829a4aSRandall Stewart 		} else {
2818f8829a4aSRandall Stewart 			goto next_addr;
2819f8829a4aSRandall Stewart 		}
2820f8829a4aSRandall Stewart 
2821f8829a4aSRandall Stewart 		/* see if this address really (still) exists */
2822bff64a4dSRandall Stewart 		if (stcb) {
2823bff64a4dSRandall Stewart 			vrf_id = stcb->asoc.vrf_id;
2824bff64a4dSRandall Stewart 		} else {
282542551e99SRandall Stewart 			vrf_id = SCTP_DEFAULT_VRFID;
2826bff64a4dSRandall Stewart 		}
28273232788eSRandall Stewart 		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id,
28283232788eSRandall Stewart 		    SCTP_ADDR_NOT_LOCKED);
282942551e99SRandall Stewart 		if (sctp_ifa == NULL) {
2830f8829a4aSRandall Stewart 			/* address doesn't exist anymore */
2831f8829a4aSRandall Stewart 			int status;
2832f8829a4aSRandall Stewart 
2833f8829a4aSRandall Stewart 			/* are ASCONFs allowed ? */
2834f8829a4aSRandall Stewart 			if ((sctp_is_feature_on(stcb->sctp_ep,
2835f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2836f8829a4aSRandall Stewart 			    stcb->asoc.peer_supports_asconf) {
2837f8829a4aSRandall Stewart 				/* queue an ASCONF DEL_IP_ADDRESS */
28383232788eSRandall Stewart 				status = sctp_asconf_queue_sa_delete(stcb, sa);
2839f8829a4aSRandall Stewart 				/*
28401b649582SRandall Stewart 				 * if queued ok, and in correct state, send
28411b649582SRandall Stewart 				 * out the ASCONF.
2842f8829a4aSRandall Stewart 				 */
2843f8829a4aSRandall Stewart 				if (status == 0 &&
2844f8829a4aSRandall Stewart 				    SCTP_GET_STATE(&stcb->asoc) ==
2845f8829a4aSRandall Stewart 				    SCTP_STATE_OPEN) {
28461b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2847f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2848f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
2849f8829a4aSRandall Stewart 					    stcb->asoc.primary_destination);
28501b649582SRandall Stewart #else
28513232788eSRandall Stewart 					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28523232788eSRandall Stewart 					    SCTP_ADDR_NOT_LOCKED);
28531b649582SRandall Stewart #endif
2854f8829a4aSRandall Stewart 				}
2855f8829a4aSRandall Stewart 			}
2856f8829a4aSRandall Stewart 		}
2857f8829a4aSRandall Stewart next_addr:
2858f8829a4aSRandall Stewart 		/*
2859f8829a4aSRandall Stewart 		 * Sanity check:  Make sure the length isn't 0, otherwise
2860f8829a4aSRandall Stewart 		 * we'll be stuck in this loop for a long time...
2861f8829a4aSRandall Stewart 		 */
2862f8829a4aSRandall Stewart 		if (SCTP_SIZE32(plen) == 0) {
2863ad81507eSRandall Stewart 			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2864f8829a4aSRandall Stewart 			    plen, ptype);
2865f8829a4aSRandall Stewart 			return;
2866f8829a4aSRandall Stewart 		}
2867f8829a4aSRandall Stewart 		/* get next parameter */
2868f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(plen);
2869f8829a4aSRandall Stewart 		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2870f8829a4aSRandall Stewart 			return;
2871f8829a4aSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2872f8829a4aSRandall Stewart 		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2873f8829a4aSRandall Stewart 	}			/* while */
2874f8829a4aSRandall Stewart }
2875f8829a4aSRandall Stewart 
2876f8829a4aSRandall Stewart /* FIX ME: need to verify return result for v6 address type if v6 disabled */
2877f8829a4aSRandall Stewart /*
2878f8829a4aSRandall Stewart  * checks to see if a specific address is in the initack address list returns
2879f8829a4aSRandall Stewart  * 1 if found, 0 if not
2880f8829a4aSRandall Stewart  */
2881f8829a4aSRandall Stewart static uint32_t
2882f8829a4aSRandall Stewart sctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, uint32_t offset,
2883f8829a4aSRandall Stewart     uint32_t length, struct sockaddr *sa)
2884f8829a4aSRandall Stewart {
2885f8829a4aSRandall Stewart 	struct sctp_paramhdr tmp_param, *ph;
2886f8829a4aSRandall Stewart 	uint16_t plen, ptype;
2887f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param addr_store;
2888f8829a4aSRandall Stewart 	struct sockaddr_in *sin;
2889f8829a4aSRandall Stewart 	struct sctp_ipv4addr_param *a4p;
2890f8829a4aSRandall Stewart 
2891f8829a4aSRandall Stewart #ifdef INET6
289242551e99SRandall Stewart 	struct sockaddr_in6 *sin6;
2893f8829a4aSRandall Stewart 	struct sctp_ipv6addr_param *a6p;
289442551e99SRandall Stewart 	struct sockaddr_in6 sin6_tmp;
2895f8829a4aSRandall Stewart 
2896f8829a4aSRandall Stewart #endif				/* INET6 */
2897f8829a4aSRandall Stewart 
2898f8829a4aSRandall Stewart 	if (
2899f8829a4aSRandall Stewart #ifdef INET6
2900f8829a4aSRandall Stewart 	    (sa->sa_family != AF_INET6) &&
2901f8829a4aSRandall Stewart #endif				/* INET6 */
2902f8829a4aSRandall Stewart 	    (sa->sa_family != AF_INET))
2903f8829a4aSRandall Stewart 		return (0);
2904f8829a4aSRandall Stewart 
2905ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2906ad81507eSRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2907f8829a4aSRandall Stewart 	/* convert to upper bound */
2908f8829a4aSRandall Stewart 	length += offset;
2909f8829a4aSRandall Stewart 
2910f8829a4aSRandall Stewart 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2911ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2912ad81507eSRandall Stewart 		    "find_initack_addr: invalid offset?\n");
2913f8829a4aSRandall Stewart 		return (0);
2914f8829a4aSRandall Stewart 	}
2915f8829a4aSRandall Stewart 	/* go through the addresses in the init-ack */
2916f8829a4aSRandall Stewart 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2917f8829a4aSRandall Stewart 	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2918f8829a4aSRandall Stewart 	while (ph != NULL) {
2919f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
2920f8829a4aSRandall Stewart 		plen = ntohs(ph->param_length);
2921f8829a4aSRandall Stewart #ifdef INET6
2922f8829a4aSRandall Stewart 		if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2923f8829a4aSRandall Stewart 			/* get the entire IPv6 address param */
2924f8829a4aSRandall Stewart 			a6p = (struct sctp_ipv6addr_param *)
2925f8829a4aSRandall Stewart 			    sctp_m_getptr(m, offset,
2926f8829a4aSRandall Stewart 			    sizeof(struct sctp_ipv6addr_param),
2927f8829a4aSRandall Stewart 			    (uint8_t *) & addr_store);
2928f8829a4aSRandall Stewart 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2929ad81507eSRandall Stewart 			    (ph == NULL) ||
2930ad81507eSRandall Stewart 			    (a6p == NULL)) {
2931f8829a4aSRandall Stewart 				return (0);
2932f8829a4aSRandall Stewart 			}
2933f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
2934f8829a4aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2935f8829a4aSRandall Stewart 				/* create a copy and clear scope */
2936f8829a4aSRandall Stewart 				memcpy(&sin6_tmp, sin6,
2937f8829a4aSRandall Stewart 				    sizeof(struct sockaddr_in6));
2938f8829a4aSRandall Stewart 				sin6 = &sin6_tmp;
2939f8829a4aSRandall Stewart 				in6_clearscope(&sin6->sin6_addr);
2940f8829a4aSRandall Stewart 			}
2941f8829a4aSRandall Stewart 			if (memcmp(&sin6->sin6_addr, a6p->addr,
2942f8829a4aSRandall Stewart 			    sizeof(struct in6_addr)) == 0) {
2943f8829a4aSRandall Stewart 				/* found it */
2944f8829a4aSRandall Stewart 				return (1);
2945f8829a4aSRandall Stewart 			}
2946f8829a4aSRandall Stewart 		} else
2947f8829a4aSRandall Stewart #endif				/* INET6 */
2948f8829a4aSRandall Stewart 
2949f8829a4aSRandall Stewart 			if (ptype == SCTP_IPV4_ADDRESS &&
2950f8829a4aSRandall Stewart 		    sa->sa_family == AF_INET) {
2951f8829a4aSRandall Stewart 			/* get the entire IPv4 address param */
2952f8829a4aSRandall Stewart 			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
2953f8829a4aSRandall Stewart 			    offset, sizeof(struct sctp_ipv4addr_param),
2954f8829a4aSRandall Stewart 			    (uint8_t *) & addr_store);
2955f8829a4aSRandall Stewart 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2956ad81507eSRandall Stewart 			    (ph == NULL) ||
2957ad81507eSRandall Stewart 			    (a4p == NULL)) {
2958f8829a4aSRandall Stewart 				return (0);
2959f8829a4aSRandall Stewart 			}
2960f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
2961f8829a4aSRandall Stewart 			if (sin->sin_addr.s_addr == a4p->addr) {
2962f8829a4aSRandall Stewart 				/* found it */
2963f8829a4aSRandall Stewart 				return (1);
2964f8829a4aSRandall Stewart 			}
2965f8829a4aSRandall Stewart 		}
2966f8829a4aSRandall Stewart 		/* get next parameter */
2967f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(plen);
2968f8829a4aSRandall Stewart 		if (offset + sizeof(struct sctp_paramhdr) > length)
2969f8829a4aSRandall Stewart 			return (0);
2970f8829a4aSRandall Stewart 		ph = (struct sctp_paramhdr *)
2971f8829a4aSRandall Stewart 		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2972f8829a4aSRandall Stewart 		    (uint8_t *) & tmp_param);
2973f8829a4aSRandall Stewart 	}			/* while */
2974f8829a4aSRandall Stewart 	/* not found! */
2975f8829a4aSRandall Stewart 	return (0);
2976f8829a4aSRandall Stewart }
2977f8829a4aSRandall Stewart 
2978f8829a4aSRandall Stewart /*
2979f8829a4aSRandall Stewart  * makes sure that the current endpoint local addr list is consistent with
2980f8829a4aSRandall Stewart  * the new association (eg. subset bound, asconf allowed) adds addresses as
2981f8829a4aSRandall Stewart  * necessary
2982f8829a4aSRandall Stewart  */
2983f8829a4aSRandall Stewart static void
2984f8829a4aSRandall Stewart sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2985f8829a4aSRandall Stewart     int length, struct sockaddr *init_addr)
2986f8829a4aSRandall Stewart {
2987f8829a4aSRandall Stewart 	struct sctp_laddr *laddr;
2988f8829a4aSRandall Stewart 
2989f8829a4aSRandall Stewart 	/* go through the endpoint list */
2990f8829a4aSRandall Stewart 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2991f8829a4aSRandall Stewart 		/* be paranoid and validate the laddr */
2992f8829a4aSRandall Stewart 		if (laddr->ifa == NULL) {
2993ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1,
2994ad81507eSRandall Stewart 			    "check_addr_list_ep: laddr->ifa is NULL");
2995f8829a4aSRandall Stewart 			continue;
2996f8829a4aSRandall Stewart 		}
299742551e99SRandall Stewart 		if (laddr->ifa == NULL) {
2998ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
2999f8829a4aSRandall Stewart 			continue;
3000f8829a4aSRandall Stewart 		}
3001f8829a4aSRandall Stewart 		/* do i have it implicitly? */
300242551e99SRandall Stewart 		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3003f8829a4aSRandall Stewart 			continue;
3004f8829a4aSRandall Stewart 		}
3005f8829a4aSRandall Stewart 		/* check to see if in the init-ack */
3006f8829a4aSRandall Stewart 		if (!sctp_addr_in_initack(stcb, m, offset, length,
300742551e99SRandall Stewart 		    &laddr->ifa->address.sa)) {
3008f8829a4aSRandall Stewart 			/* try to add it */
3009f8829a4aSRandall Stewart 			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
30103232788eSRandall Stewart 			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3011f8829a4aSRandall Stewart 		}
3012f8829a4aSRandall Stewart 	}
3013f8829a4aSRandall Stewart }
3014f8829a4aSRandall Stewart 
3015f8829a4aSRandall Stewart /*
3016f8829a4aSRandall Stewart  * makes sure that the current kernel address list is consistent with the new
3017f8829a4aSRandall Stewart  * association (with all addrs bound) adds addresses as necessary
3018f8829a4aSRandall Stewart  */
3019f8829a4aSRandall Stewart static void
3020f8829a4aSRandall Stewart sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3021f8829a4aSRandall Stewart     int length, struct sockaddr *init_addr,
3022f8829a4aSRandall Stewart     uint16_t local_scope, uint16_t site_scope,
3023f8829a4aSRandall Stewart     uint16_t ipv4_scope, uint16_t loopback_scope)
3024f8829a4aSRandall Stewart {
302542551e99SRandall Stewart 	struct sctp_vrf *vrf = NULL;
302642551e99SRandall Stewart 	struct sctp_ifn *sctp_ifn;
302742551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
302842551e99SRandall Stewart 	uint32_t vrf_id;
3029f8829a4aSRandall Stewart 
3030bff64a4dSRandall Stewart 	if (stcb) {
3031bff64a4dSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
3032bff64a4dSRandall Stewart 	} else {
3033ad81507eSRandall Stewart 		return;
3034bff64a4dSRandall Stewart 	}
3035c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
303642551e99SRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
303742551e99SRandall Stewart 	if (vrf == NULL) {
3038c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
303942551e99SRandall Stewart 		return;
304042551e99SRandall Stewart 	}
3041f8829a4aSRandall Stewart 	/* go through all our known interfaces */
304242551e99SRandall Stewart 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
304342551e99SRandall Stewart 		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3044f8829a4aSRandall Stewart 			/* skip loopback interface */
3045f8829a4aSRandall Stewart 			continue;
3046f8829a4aSRandall Stewart 		}
3047f8829a4aSRandall Stewart 		/* go through each interface address */
304842551e99SRandall Stewart 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3049f8829a4aSRandall Stewart 			/* do i have it implicitly? */
305042551e99SRandall Stewart 			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3051f8829a4aSRandall Stewart 				continue;
3052f8829a4aSRandall Stewart 			}
3053f8829a4aSRandall Stewart 			/* check to see if in the init-ack */
3054f8829a4aSRandall Stewart 			if (!sctp_addr_in_initack(stcb, m, offset, length,
305542551e99SRandall Stewart 			    &sctp_ifa->address.sa)) {
3056f8829a4aSRandall Stewart 				/* try to add it */
3057f8829a4aSRandall Stewart 				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
30583232788eSRandall Stewart 				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
30593232788eSRandall Stewart 				    SCTP_ADDR_LOCKED);
3060f8829a4aSRandall Stewart 			}
3061f8829a4aSRandall Stewart 		}		/* end foreach ifa */
3062f8829a4aSRandall Stewart 	}			/* end foreach ifn */
3063c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
3064f8829a4aSRandall Stewart }
3065f8829a4aSRandall Stewart 
3066f8829a4aSRandall Stewart /*
3067f8829a4aSRandall Stewart  * validates an init-ack chunk (from a cookie-echo) with current addresses
3068f8829a4aSRandall Stewart  * adds addresses from the init-ack into our local address list, if needed
3069f8829a4aSRandall Stewart  * queues asconf adds/deletes addresses as needed and makes appropriate list
3070f8829a4aSRandall Stewart  * changes for source address selection m, offset: points to the start of the
3071f8829a4aSRandall Stewart  * address list in an init-ack chunk length: total length of the address
3072f8829a4aSRandall Stewart  * params only init_addr: address where my INIT-ACK was sent from
3073f8829a4aSRandall Stewart  */
3074f8829a4aSRandall Stewart void
3075f8829a4aSRandall Stewart sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3076f8829a4aSRandall Stewart     int length, struct sockaddr *init_addr,
3077f8829a4aSRandall Stewart     uint16_t local_scope, uint16_t site_scope,
3078f8829a4aSRandall Stewart     uint16_t ipv4_scope, uint16_t loopback_scope)
3079f8829a4aSRandall Stewart {
3080f8829a4aSRandall Stewart 	/* process the local addresses in the initack */
3081f8829a4aSRandall Stewart 	sctp_process_initack_addresses(stcb, m, offset, length);
3082f8829a4aSRandall Stewart 
3083f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3084f8829a4aSRandall Stewart 		/* bound all case */
3085f8829a4aSRandall Stewart 		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3086f8829a4aSRandall Stewart 		    local_scope, site_scope, ipv4_scope, loopback_scope);
3087f8829a4aSRandall Stewart 	} else {
3088f8829a4aSRandall Stewart 		/* subset bound case */
3089f8829a4aSRandall Stewart 		if (sctp_is_feature_on(stcb->sctp_ep,
3090f8829a4aSRandall Stewart 		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3091f8829a4aSRandall Stewart 			/* asconf's allowed */
3092f8829a4aSRandall Stewart 			sctp_check_address_list_ep(stcb, m, offset, length,
3093f8829a4aSRandall Stewart 			    init_addr);
3094f8829a4aSRandall Stewart 		}
3095f8829a4aSRandall Stewart 		/* else, no asconfs allowed, so what we sent is what we get */
3096f8829a4aSRandall Stewart 	}
3097f8829a4aSRandall Stewart }
3098f8829a4aSRandall Stewart 
3099f8829a4aSRandall Stewart /*
3100f8829a4aSRandall Stewart  * sctp_bindx() support
3101f8829a4aSRandall Stewart  */
3102f8829a4aSRandall Stewart uint32_t
31033c503c28SRandall Stewart sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
310480fefe0aSRandall Stewart     uint32_t type, uint32_t vrf_id, struct sctp_ifa *sctp_ifap)
3105f8829a4aSRandall Stewart {
310642551e99SRandall Stewart 	struct sctp_ifa *ifa;
3107*2678fe1eSMichael Tuexen 	struct sctp_laddr *laddr, *nladdr;
3108f8829a4aSRandall Stewart 
310942551e99SRandall Stewart 	if (sa->sa_len == 0) {
3110c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3111f8829a4aSRandall Stewart 		return (EINVAL);
311242551e99SRandall Stewart 	}
311380fefe0aSRandall Stewart 	if (sctp_ifap) {
311480fefe0aSRandall Stewart 		ifa = sctp_ifap;
311580fefe0aSRandall Stewart 	} else if (type == SCTP_ADD_IP_ADDRESS) {
311642551e99SRandall Stewart 		/* For an add the address MUST be on the system */
3117851b7298SRandall Stewart 		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
311842551e99SRandall Stewart 	} else if (type == SCTP_DEL_IP_ADDRESS) {
311942551e99SRandall Stewart 		/* For a delete we need to find it in the inp */
3120851b7298SRandall Stewart 		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
312142551e99SRandall Stewart 	} else {
312242551e99SRandall Stewart 		ifa = NULL;
312342551e99SRandall Stewart 	}
3124f8829a4aSRandall Stewart 	if (ifa != NULL) {
312542551e99SRandall Stewart 		if (type == SCTP_ADD_IP_ADDRESS) {
312642551e99SRandall Stewart 			sctp_add_local_addr_ep(inp, ifa, type);
312742551e99SRandall Stewart 		} else if (type == SCTP_DEL_IP_ADDRESS) {
31283c503c28SRandall Stewart 			if (inp->laddr_count < 2) {
31293c503c28SRandall Stewart 				/* can't delete the last local address */
3130c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
31313c503c28SRandall Stewart 				return (EINVAL);
31323c503c28SRandall Stewart 			}
31333c503c28SRandall Stewart 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
31343c503c28SRandall Stewart 			    sctp_nxt_addr) {
313542551e99SRandall Stewart 				if (ifa == laddr->ifa) {
313642551e99SRandall Stewart 					/* Mark in the delete */
313742551e99SRandall Stewart 					laddr->action = type;
313842551e99SRandall Stewart 				}
313942551e99SRandall Stewart 			}
314042551e99SRandall Stewart 		}
3141*2678fe1eSMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
314287b4fcd3SMichael Tuexen 			/*
314387b4fcd3SMichael Tuexen 			 * There is no need to start the iterator if the inp
314487b4fcd3SMichael Tuexen 			 * has no associations.
314587b4fcd3SMichael Tuexen 			 */
3146*2678fe1eSMichael Tuexen 			if (type == SCTP_DEL_IP_ADDRESS) {
3147*2678fe1eSMichael Tuexen 				LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3148*2678fe1eSMichael Tuexen 					if (laddr->ifa == ifa) {
3149*2678fe1eSMichael Tuexen 						sctp_del_local_addr_ep(inp, ifa);
3150*2678fe1eSMichael Tuexen 					}
3151*2678fe1eSMichael Tuexen 				}
3152*2678fe1eSMichael Tuexen 			}
3153*2678fe1eSMichael Tuexen 		} else {
315487b4fcd3SMichael Tuexen 			struct sctp_asconf_iterator *asc;
315587b4fcd3SMichael Tuexen 			struct sctp_laddr *wi;
315687b4fcd3SMichael Tuexen 
315787b4fcd3SMichael Tuexen 			SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
315887b4fcd3SMichael Tuexen 			    sizeof(struct sctp_asconf_iterator),
315987b4fcd3SMichael Tuexen 			    SCTP_M_ASC_IT);
316087b4fcd3SMichael Tuexen 			if (asc == NULL) {
316187b4fcd3SMichael Tuexen 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
316287b4fcd3SMichael Tuexen 				return (ENOMEM);
316387b4fcd3SMichael Tuexen 			}
316487b4fcd3SMichael Tuexen 			wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
316587b4fcd3SMichael Tuexen 			if (wi == NULL) {
316687b4fcd3SMichael Tuexen 				SCTP_FREE(asc, SCTP_M_ASC_IT);
316787b4fcd3SMichael Tuexen 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
316887b4fcd3SMichael Tuexen 				return (ENOMEM);
316987b4fcd3SMichael Tuexen 			}
317042551e99SRandall Stewart 			LIST_INIT(&asc->list_of_work);
317142551e99SRandall Stewart 			asc->cnt = 1;
317242551e99SRandall Stewart 			SCTP_INCR_LADDR_COUNT();
317342551e99SRandall Stewart 			wi->ifa = ifa;
317442551e99SRandall Stewart 			wi->action = type;
317542551e99SRandall Stewart 			atomic_add_int(&ifa->refcount, 1);
317642551e99SRandall Stewart 			LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
31771b649582SRandall Stewart 			(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
31781b649582SRandall Stewart 			    sctp_asconf_iterator_stcb,
31791b649582SRandall Stewart 			    sctp_asconf_iterator_ep_end,
318042551e99SRandall Stewart 			    SCTP_PCB_ANY_FLAGS,
31813c503c28SRandall Stewart 			    SCTP_PCB_ANY_FEATURES,
31821b649582SRandall Stewart 			    SCTP_ASOC_ANY_STATE,
31831b649582SRandall Stewart 			    (void *)asc, 0,
31841b649582SRandall Stewart 			    sctp_asconf_iterator_end, inp, 0);
318587b4fcd3SMichael Tuexen 		}
318687b4fcd3SMichael Tuexen 		return (0);
3187f8829a4aSRandall Stewart 	} else {
3188f8829a4aSRandall Stewart 		/* invalid address! */
3189c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3190f8829a4aSRandall Stewart 		return (EADDRNOTAVAIL);
3191f8829a4aSRandall Stewart 	}
3192f8829a4aSRandall Stewart }
3193830d754dSRandall Stewart 
3194830d754dSRandall Stewart void
3195830d754dSRandall Stewart sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb,
3196830d754dSRandall Stewart     struct sctp_nets *net)
3197830d754dSRandall Stewart {
3198830d754dSRandall Stewart 	struct sctp_asconf_addr *aa;
3199830d754dSRandall Stewart 	struct sctp_ifa *sctp_ifap;
3200830d754dSRandall Stewart 	struct sctp_asconf_tag_param *vtag;
3201830d754dSRandall Stewart 	struct sockaddr_in *to;
3202830d754dSRandall Stewart 
3203830d754dSRandall Stewart #ifdef INET6
3204830d754dSRandall Stewart 	struct sockaddr_in6 *to6;
3205830d754dSRandall Stewart 
3206830d754dSRandall Stewart #endif
3207830d754dSRandall Stewart 	if (net == NULL) {
3208830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
3209830d754dSRandall Stewart 		return;
3210830d754dSRandall Stewart 	}
3211830d754dSRandall Stewart 	if (stcb == NULL) {
3212830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
3213830d754dSRandall Stewart 		return;
3214830d754dSRandall Stewart 	}
3215830d754dSRandall Stewart 	/*
3216830d754dSRandall Stewart 	 * Need to have in the asconf: - vtagparam(my_vtag/peer_vtag) -
3217830d754dSRandall Stewart 	 * add(0.0.0.0) - del(0.0.0.0) - Any global addresses add(addr)
3218830d754dSRandall Stewart 	 */
3219830d754dSRandall Stewart 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3220830d754dSRandall Stewart 	    SCTP_M_ASC_ADDR);
3221830d754dSRandall Stewart 	if (aa == NULL) {
3222830d754dSRandall Stewart 		/* didn't get memory */
3223830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
3224830d754dSRandall Stewart 		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3225830d754dSRandall Stewart 		return;
3226830d754dSRandall Stewart 	}
3227830d754dSRandall Stewart 	aa->special_del = 0;
3228830d754dSRandall Stewart 	/* fill in asconf address parameter fields */
3229830d754dSRandall Stewart 	/* top level elements are "networked" during send */
3230830d754dSRandall Stewart 	aa->ifa = NULL;
3231830d754dSRandall Stewart 	aa->sent = 0;		/* clear sent flag */
3232830d754dSRandall Stewart 	vtag = (struct sctp_asconf_tag_param *)&aa->ap.aph;
3233830d754dSRandall Stewart 	vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
3234830d754dSRandall Stewart 	vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
3235830d754dSRandall Stewart 	vtag->local_vtag = htonl(stcb->asoc.my_vtag);
3236830d754dSRandall Stewart 	vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
3237830d754dSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3238830d754dSRandall Stewart 
3239830d754dSRandall Stewart 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3240830d754dSRandall Stewart 	    SCTP_M_ASC_ADDR);
3241830d754dSRandall Stewart 	if (aa == NULL) {
3242830d754dSRandall Stewart 		/* didn't get memory */
3243830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
3244830d754dSRandall Stewart 		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3245830d754dSRandall Stewart 		return;
3246830d754dSRandall Stewart 	}
3247830d754dSRandall Stewart 	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3248830d754dSRandall Stewart 	/* fill in asconf address parameter fields */
3249830d754dSRandall Stewart 	/* ADD(0.0.0.0) */
3250830d754dSRandall Stewart 	if (net->ro._l_addr.sa.sa_family == AF_INET) {
3251830d754dSRandall Stewart 		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3252830d754dSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3253830d754dSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3254830d754dSRandall Stewart 		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3255830d754dSRandall Stewart 		/* No need to add an address, we are using 0.0.0.0 */
3256830d754dSRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3257830d754dSRandall Stewart 	}
3258830d754dSRandall Stewart #ifdef INET6
3259830d754dSRandall Stewart 	else if (net->ro._l_addr.sa.sa_family == AF_INET6) {
3260830d754dSRandall Stewart 		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3261830d754dSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3262830d754dSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3263830d754dSRandall Stewart 		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3264830d754dSRandall Stewart 		/* No need to add an address, we are using 0.0.0.0 */
3265830d754dSRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3266830d754dSRandall Stewart 	}
3267830d754dSRandall Stewart #endif				/* INET6 */
3268830d754dSRandall Stewart 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3269830d754dSRandall Stewart 	    SCTP_M_ASC_ADDR);
3270830d754dSRandall Stewart 	if (aa == NULL) {
3271830d754dSRandall Stewart 		/* didn't get memory */
3272830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1,
3273830d754dSRandall Stewart 		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3274830d754dSRandall Stewart 		return;
3275830d754dSRandall Stewart 	}
3276830d754dSRandall Stewart 	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3277830d754dSRandall Stewart 	/* fill in asconf address parameter fields */
3278830d754dSRandall Stewart 	/* ADD(0.0.0.0) */
3279830d754dSRandall Stewart 	if (net->ro._l_addr.sa.sa_family == AF_INET) {
3280830d754dSRandall Stewart 		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3281830d754dSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3282830d754dSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3283830d754dSRandall Stewart 		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3284830d754dSRandall Stewart 		/* No need to add an address, we are using 0.0.0.0 */
3285830d754dSRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3286830d754dSRandall Stewart 	}
3287830d754dSRandall Stewart #ifdef INET6
3288830d754dSRandall Stewart 	else if (net->ro._l_addr.sa.sa_family == AF_INET6) {
3289830d754dSRandall Stewart 		aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3290830d754dSRandall Stewart 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3291830d754dSRandall Stewart 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3292830d754dSRandall Stewart 		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3293830d754dSRandall Stewart 		/* No need to add an address, we are using 0.0.0.0 */
3294830d754dSRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3295830d754dSRandall Stewart 	}
3296830d754dSRandall Stewart #endif				/* INET6 */
3297830d754dSRandall Stewart 	/* Now we must hunt the addresses and add all global addresses */
3298830d754dSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3299830d754dSRandall Stewart 		struct sctp_vrf *vrf = NULL;
3300830d754dSRandall Stewart 		struct sctp_ifn *sctp_ifnp;
3301830d754dSRandall Stewart 		uint32_t vrf_id;
3302830d754dSRandall Stewart 
3303830d754dSRandall Stewart 		vrf_id = stcb->sctp_ep->def_vrf_id;
3304830d754dSRandall Stewart 		vrf = sctp_find_vrf(vrf_id);
3305830d754dSRandall Stewart 		if (vrf == NULL) {
3306830d754dSRandall Stewart 			goto skip_rest;
3307830d754dSRandall Stewart 		}
3308830d754dSRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
3309830d754dSRandall Stewart 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3310830d754dSRandall Stewart 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3311830d754dSRandall Stewart 				if (sctp_ifap->address.sa.sa_family == AF_INET) {
3312830d754dSRandall Stewart 					to = &sctp_ifap->address.sin;
3313830d754dSRandall Stewart 
3314830d754dSRandall Stewart 					if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3315830d754dSRandall Stewart 						continue;
3316830d754dSRandall Stewart 					}
3317830d754dSRandall Stewart 					if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3318830d754dSRandall Stewart 						continue;
3319830d754dSRandall Stewart 					}
3320830d754dSRandall Stewart 				}
3321830d754dSRandall Stewart #ifdef INET6
3322830d754dSRandall Stewart 				else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
3323830d754dSRandall Stewart 					to6 = &sctp_ifap->address.sin6;
3324830d754dSRandall Stewart 					if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3325830d754dSRandall Stewart 						continue;
3326830d754dSRandall Stewart 					}
3327830d754dSRandall Stewart 					if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3328830d754dSRandall Stewart 						continue;
3329830d754dSRandall Stewart 					}
3330830d754dSRandall Stewart 				}
3331830d754dSRandall Stewart #endif
3332830d754dSRandall Stewart 				sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3333830d754dSRandall Stewart 			}
3334830d754dSRandall Stewart 		}
3335830d754dSRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
3336830d754dSRandall Stewart 	} else {
3337830d754dSRandall Stewart 		struct sctp_laddr *laddr;
3338830d754dSRandall Stewart 
3339830d754dSRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3340830d754dSRandall Stewart 			if (laddr->ifa == NULL) {
3341830d754dSRandall Stewart 				continue;
3342830d754dSRandall Stewart 			}
3343830d754dSRandall Stewart 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3344830d754dSRandall Stewart 				/*
3345830d754dSRandall Stewart 				 * Address being deleted by the system, dont
3346830d754dSRandall Stewart 				 * list.
3347830d754dSRandall Stewart 				 */
3348830d754dSRandall Stewart 				continue;
3349830d754dSRandall Stewart 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3350830d754dSRandall Stewart 				/*
3351830d754dSRandall Stewart 				 * Address being deleted on this ep don't
3352830d754dSRandall Stewart 				 * list.
3353830d754dSRandall Stewart 				 */
3354830d754dSRandall Stewart 				continue;
3355830d754dSRandall Stewart 			}
3356830d754dSRandall Stewart 			sctp_ifap = laddr->ifa;
3357830d754dSRandall Stewart 			if (sctp_ifap->address.sa.sa_family == AF_INET) {
3358830d754dSRandall Stewart 				to = &sctp_ifap->address.sin;
3359830d754dSRandall Stewart 
3360830d754dSRandall Stewart 				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3361830d754dSRandall Stewart 					continue;
3362830d754dSRandall Stewart 				}
3363830d754dSRandall Stewart 				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3364830d754dSRandall Stewart 					continue;
3365830d754dSRandall Stewart 				}
3366830d754dSRandall Stewart 			}
3367830d754dSRandall Stewart #ifdef INET6
3368830d754dSRandall Stewart 			else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
3369830d754dSRandall Stewart 				to6 = &sctp_ifap->address.sin6;
3370830d754dSRandall Stewart 				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3371830d754dSRandall Stewart 					continue;
3372830d754dSRandall Stewart 				}
3373830d754dSRandall Stewart 				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3374830d754dSRandall Stewart 					continue;
3375830d754dSRandall Stewart 				}
3376830d754dSRandall Stewart 			}
3377830d754dSRandall Stewart #endif
3378830d754dSRandall Stewart 			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3379830d754dSRandall Stewart 		}
3380830d754dSRandall Stewart 	}
3381830d754dSRandall Stewart skip_rest:
3382830d754dSRandall Stewart 	/* Now we must send the asconf into the queue */
3383830d754dSRandall Stewart 	sctp_send_asconf(stcb, net, 0);
3384830d754dSRandall Stewart }
3385