1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3237896Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4237896Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10231038Stuexen *    this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14231038Stuexen *    the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163957Srrs__FBSDID("$FreeBSD$");
35237896Stuexen
36163953Srrs#include <netinet/sctp_os.h>
37163953Srrs#include <netinet/sctp_var.h>
38167598Srrs#include <netinet/sctp_sysctl.h>
39163953Srrs#include <netinet/sctp_pcb.h>
40163953Srrs#include <netinet/sctp_header.h>
41163953Srrs#include <netinet/sctputil.h>
42163953Srrs#include <netinet/sctp_output.h>
43163953Srrs#include <netinet/sctp_asconf.h>
44172091Srrs#include <netinet/sctp_timer.h>
45163953Srrs
46163953Srrs/*
47163953Srrs * debug flags:
48163953Srrs * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
49163953Srrs * SCTP_DEBUG_ASCONF2: detailed info
50163953Srrs */
51163953Srrs
52163953Srrs
53163953Srrs/*
54252889Stuexen * RFC 5061
55163953Srrs *
56163953Srrs * An ASCONF parameter queue exists per asoc which holds the pending address
57163953Srrs * operations.  Lists are updated upon receipt of ASCONF-ACK.
58163953Srrs *
59171477Srrs * A restricted_addrs list exists per assoc to hold local addresses that are
60171477Srrs * not (yet) usable by the assoc as a source address.  These addresses are
61171477Srrs * either pending an ASCONF operation (and exist on the ASCONF parameter
62171477Srrs * queue), or they are permanently restricted (the peer has returned an
63171477Srrs * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
64171477Srrs *
65163953Srrs * Deleted addresses are always immediately removed from the lists as they will
66163953Srrs * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
67163953Srrs * only if allowed.
68163953Srrs */
69163953Srrs
70163953Srrs/*
71171477Srrs * ASCONF parameter processing.
72171477Srrs * response_required: set if a reply is required (eg. SUCCESS_REPORT).
73171477Srrs * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
74171477Srrs * FIX: allocating this many mbufs on the fly is pretty inefficient...
75163953Srrs */
76163953Srrsstatic struct mbuf *
77163953Srrssctp_asconf_success_response(uint32_t id)
78163953Srrs{
79163953Srrs	struct mbuf *m_reply = NULL;
80163953Srrs	struct sctp_asconf_paramhdr *aph;
81163953Srrs
82163953Srrs	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
83163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
84163953Srrs	if (m_reply == NULL) {
85169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
86169420Srrs		    "asconf_success_response: couldn't get mbuf!\n");
87231039Stuexen		return (NULL);
88163953Srrs	}
89163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
90163953Srrs	aph->correlation_id = id;
91163953Srrs	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
92163953Srrs	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
93165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
94163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
95163953Srrs
96231039Stuexen	return (m_reply);
97163953Srrs}
98163953Srrs
99163953Srrsstatic struct mbuf *
100163953Srrssctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
101163953Srrs    uint16_t tlv_length)
102163953Srrs{
103163953Srrs	struct mbuf *m_reply = NULL;
104163953Srrs	struct sctp_asconf_paramhdr *aph;
105163953Srrs	struct sctp_error_cause *error;
106163953Srrs	uint8_t *tlv;
107163953Srrs
108163953Srrs	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
109163953Srrs	    tlv_length +
110163953Srrs	    sizeof(struct sctp_error_cause)),
111163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
112163953Srrs	if (m_reply == NULL) {
113169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
114169420Srrs		    "asconf_error_response: couldn't get mbuf!\n");
115231039Stuexen		return (NULL);
116163953Srrs	}
117163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
118163953Srrs	error = (struct sctp_error_cause *)(aph + 1);
119163953Srrs
120163953Srrs	aph->correlation_id = id;
121163953Srrs	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
122163953Srrs	error->code = htons(cause);
123163953Srrs	error->length = tlv_length + sizeof(struct sctp_error_cause);
124163953Srrs	aph->ph.param_length = error->length +
125163953Srrs	    sizeof(struct sctp_asconf_paramhdr);
126163953Srrs
127163953Srrs	if (aph->ph.param_length > MLEN) {
128169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
129169420Srrs		    "asconf_error_response: tlv_length (%xh) too big\n",
130169420Srrs		    tlv_length);
131163953Srrs		sctp_m_freem(m_reply);	/* discard */
132231039Stuexen		return (NULL);
133163953Srrs	}
134163953Srrs	if (error_tlv != NULL) {
135163953Srrs		tlv = (uint8_t *) (error + 1);
136163953Srrs		memcpy(tlv, error_tlv, tlv_length);
137163953Srrs	}
138165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
139163953Srrs	error->length = htons(error->length);
140163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
141163953Srrs
142231039Stuexen	return (m_reply);
143163953Srrs}
144163953Srrs
145163953Srrsstatic struct mbuf *
146252889Stuexensctp_process_asconf_add_ip(struct sockaddr *src, struct sctp_asconf_paramhdr *aph,
147224641Stuexen    struct sctp_tcb *stcb, int send_hb, int response_required)
148163953Srrs{
149224641Stuexen	struct sctp_nets *net;
150163953Srrs	struct mbuf *m_reply = NULL;
151252889Stuexen	struct sockaddr_storage sa_store;
152221249Stuexen	struct sctp_paramhdr *ph;
153266054Stuexen	uint16_t param_type, aparam_length;
154266054Stuexen
155266054Stuexen#if defined(INET) || defined(INET6)
156266054Stuexen	uint16_t param_length;
157266054Stuexen
158266054Stuexen#endif
159163953Srrs	struct sockaddr *sa;
160163953Srrs	int zero_address = 0;
161225571Stuexen	int bad_address = 0;
162163953Srrs
163221249Stuexen#ifdef INET
164221249Stuexen	struct sockaddr_in *sin;
165221249Stuexen	struct sctp_ipv4addr_param *v4addr;
166221249Stuexen
167221249Stuexen#endif
168163953Srrs#ifdef INET6
169163953Srrs	struct sockaddr_in6 *sin6;
170163953Srrs	struct sctp_ipv6addr_param *v6addr;
171163953Srrs
172221249Stuexen#endif
173163953Srrs
174163953Srrs	aparam_length = ntohs(aph->ph.param_length);
175221249Stuexen	ph = (struct sctp_paramhdr *)(aph + 1);
176221249Stuexen	param_type = ntohs(ph->param_type);
177266054Stuexen#if defined(INET) || defined(INET6)
178221249Stuexen	param_length = ntohs(ph->param_length);
179266054Stuexen#endif
180163953Srrs	sa = (struct sockaddr *)&sa_store;
181163953Srrs	switch (param_type) {
182221249Stuexen#ifdef INET
183163953Srrs	case SCTP_IPV4_ADDRESS:
184163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
185163953Srrs			/* invalid param size */
186231039Stuexen			return (NULL);
187163953Srrs		}
188221249Stuexen		v4addr = (struct sctp_ipv4addr_param *)ph;
189163953Srrs		sin = (struct sockaddr_in *)&sa_store;
190163953Srrs		bzero(sin, sizeof(*sin));
191163953Srrs		sin->sin_family = AF_INET;
192163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
193163953Srrs		sin->sin_port = stcb->rport;
194163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
195225571Stuexen		if ((sin->sin_addr.s_addr == INADDR_BROADCAST) ||
196225571Stuexen		    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
197225584Stuexen			bad_address = 1;
198225571Stuexen		}
199163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
200163953Srrs			zero_address = 1;
201169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
202169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
203163953Srrs		break;
204221249Stuexen#endif
205221249Stuexen#ifdef INET6
206163953Srrs	case SCTP_IPV6_ADDRESS:
207163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
208163953Srrs			/* invalid param size */
209231039Stuexen			return (NULL);
210163953Srrs		}
211221249Stuexen		v6addr = (struct sctp_ipv6addr_param *)ph;
212163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
213163953Srrs		bzero(sin6, sizeof(*sin6));
214163953Srrs		sin6->sin6_family = AF_INET6;
215163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
216163953Srrs		sin6->sin6_port = stcb->rport;
217163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
218163953Srrs		    sizeof(struct in6_addr));
219225571Stuexen		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
220225571Stuexen			bad_address = 1;
221225571Stuexen		}
222163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
223163953Srrs			zero_address = 1;
224169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
225169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
226221249Stuexen		break;
227169420Srrs#endif
228163953Srrs	default:
229163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
230225571Stuexen		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
231163953Srrs		    aparam_length);
232231039Stuexen		return (m_reply);
233163953Srrs	}			/* end switch */
234163953Srrs
235163953Srrs	/* if 0.0.0.0/::0, add the source address instead */
236179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
237252889Stuexen		sa = src;
238169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
239169420Srrs		    "process_asconf_add_ip: using source addr ");
240252889Stuexen		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
241163953Srrs	}
242163953Srrs	/* add the address */
243225571Stuexen	if (bad_address) {
244225571Stuexen		m_reply = sctp_asconf_error_response(aph->correlation_id,
245225571Stuexen		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
246225571Stuexen		    aparam_length);
247225571Stuexen	} else if (sctp_add_remote_addr(stcb, sa, &net, SCTP_DONOT_SETSCOPE,
248165220Srrs	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
249169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
250169420Srrs		    "process_asconf_add_ip: error adding address\n");
251163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
252163953Srrs		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
253163953Srrs		    aparam_length);
254163953Srrs	} else {
255163953Srrs		/* notify upper layer */
256172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
257163953Srrs		if (response_required) {
258163953Srrs			m_reply =
259163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
260163953Srrs		}
261224641Stuexen		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
262169655Srrs		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
263224641Stuexen		    stcb, net);
264224641Stuexen		if (send_hb) {
265224641Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
266224641Stuexen		}
267163953Srrs	}
268231039Stuexen	return (m_reply);
269163953Srrs}
270163953Srrs
271163953Srrsstatic int
272171572Srrssctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
273163953Srrs{
274163953Srrs	struct sctp_nets *src_net, *net;
275163953Srrs
276163953Srrs	/* make sure the source address exists as a destination net */
277163953Srrs	src_net = sctp_findnet(stcb, src);
278163953Srrs	if (src_net == NULL) {
279163953Srrs		/* not found */
280231039Stuexen		return (-1);
281163953Srrs	}
282163953Srrs	/* delete all destination addresses except the source */
283163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
284163953Srrs		if (net != src_net) {
285163953Srrs			/* delete this address */
286163953Srrs			sctp_remove_net(stcb, net);
287169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
288169420Srrs			    "asconf_del_remote_addrs_except: deleting ");
289169420Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
290169420Srrs			    (struct sockaddr *)&net->ro._l_addr);
291163953Srrs			/* notify upper layer */
292163953Srrs			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
293172090Srrs			    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
294163953Srrs		}
295163953Srrs	}
296231039Stuexen	return (0);
297163953Srrs}
298163953Srrs
299163953Srrsstatic struct mbuf *
300252889Stuexensctp_process_asconf_delete_ip(struct sockaddr *src,
301252889Stuexen    struct sctp_asconf_paramhdr *aph,
302163953Srrs    struct sctp_tcb *stcb, int response_required)
303163953Srrs{
304163953Srrs	struct mbuf *m_reply = NULL;
305252889Stuexen	struct sockaddr_storage sa_store;
306221249Stuexen	struct sctp_paramhdr *ph;
307266054Stuexen	uint16_t param_type, aparam_length;
308266054Stuexen
309266054Stuexen#if defined(INET) || defined(INET6)
310266054Stuexen	uint16_t param_length;
311266054Stuexen
312266054Stuexen#endif
313163953Srrs	struct sockaddr *sa;
314163953Srrs	int zero_address = 0;
315163953Srrs	int result;
316163953Srrs
317221249Stuexen#ifdef INET
318221249Stuexen	struct sockaddr_in *sin;
319221249Stuexen	struct sctp_ipv4addr_param *v4addr;
320221249Stuexen
321221249Stuexen#endif
322163953Srrs#ifdef INET6
323163953Srrs	struct sockaddr_in6 *sin6;
324163953Srrs	struct sctp_ipv6addr_param *v6addr;
325163953Srrs
326221249Stuexen#endif
327163953Srrs
328163953Srrs	aparam_length = ntohs(aph->ph.param_length);
329221249Stuexen	ph = (struct sctp_paramhdr *)(aph + 1);
330221249Stuexen	param_type = ntohs(ph->param_type);
331266054Stuexen#if defined(INET) || defined(INET6)
332221249Stuexen	param_length = ntohs(ph->param_length);
333266054Stuexen#endif
334163953Srrs	sa = (struct sockaddr *)&sa_store;
335163953Srrs	switch (param_type) {
336221249Stuexen#ifdef INET
337163953Srrs	case SCTP_IPV4_ADDRESS:
338163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
339163953Srrs			/* invalid param size */
340231039Stuexen			return (NULL);
341163953Srrs		}
342221249Stuexen		v4addr = (struct sctp_ipv4addr_param *)ph;
343163953Srrs		sin = (struct sockaddr_in *)&sa_store;
344163953Srrs		bzero(sin, sizeof(*sin));
345163953Srrs		sin->sin_family = AF_INET;
346163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
347163953Srrs		sin->sin_port = stcb->rport;
348163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
349163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
350163953Srrs			zero_address = 1;
351169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
352169420Srrs		    "process_asconf_delete_ip: deleting ");
353169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
354163953Srrs		break;
355221249Stuexen#endif
356221249Stuexen#ifdef INET6
357163953Srrs	case SCTP_IPV6_ADDRESS:
358163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
359163953Srrs			/* invalid param size */
360231039Stuexen			return (NULL);
361163953Srrs		}
362221249Stuexen		v6addr = (struct sctp_ipv6addr_param *)ph;
363163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
364163953Srrs		bzero(sin6, sizeof(*sin6));
365163953Srrs		sin6->sin6_family = AF_INET6;
366163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
367163953Srrs		sin6->sin6_port = stcb->rport;
368163953Srrs		memcpy(&sin6->sin6_addr, v6addr->addr,
369163953Srrs		    sizeof(struct in6_addr));
370163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
371163953Srrs			zero_address = 1;
372169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
373169420Srrs		    "process_asconf_delete_ip: deleting ");
374169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
375221249Stuexen		break;
376169420Srrs#endif
377163953Srrs	default:
378163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
379163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
380163953Srrs		    aparam_length);
381231039Stuexen		return (m_reply);
382163953Srrs	}
383163953Srrs
384163953Srrs	/* make sure the source address is not being deleted */
385252889Stuexen	if (sctp_cmpaddr(sa, src)) {
386163953Srrs		/* trying to delete the source address! */
387169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
388163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
389163953Srrs		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
390163953Srrs		    aparam_length);
391231039Stuexen		return (m_reply);
392163953Srrs	}
393163953Srrs	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
394179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
395252889Stuexen		result = sctp_asconf_del_remote_addrs_except(stcb, src);
396163953Srrs
397163953Srrs		if (result) {
398163953Srrs			/* src address did not exist? */
399169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
400163953Srrs			/* what error to reply with?? */
401163953Srrs			m_reply =
402163953Srrs			    sctp_asconf_error_response(aph->correlation_id,
403163953Srrs			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
404163953Srrs			    aparam_length);
405163953Srrs		} else if (response_required) {
406163953Srrs			m_reply =
407163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
408163953Srrs		}
409231039Stuexen		return (m_reply);
410163953Srrs	}
411163953Srrs	/* delete the address */
412163953Srrs	result = sctp_del_remote_addr(stcb, sa);
413163953Srrs	/*
414163953Srrs	 * note if result == -2, the address doesn't exist in the asoc but
415163953Srrs	 * since it's being deleted anyways, we just ack the delete -- but
416163953Srrs	 * this probably means something has already gone awry
417163953Srrs	 */
418163953Srrs	if (result == -1) {
419163953Srrs		/* only one address in the asoc */
420169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
421163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
422163953Srrs		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
423163953Srrs		    aparam_length);
424163953Srrs	} else {
425163953Srrs		if (response_required) {
426163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
427163953Srrs		}
428163953Srrs		/* notify upper layer */
429172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
430163953Srrs	}
431231039Stuexen	return (m_reply);
432163953Srrs}
433163953Srrs
434163953Srrsstatic struct mbuf *
435252889Stuexensctp_process_asconf_set_primary(struct sockaddr *src,
436171572Srrs    struct sctp_asconf_paramhdr *aph,
437171572Srrs    struct sctp_tcb *stcb, int response_required)
438163953Srrs{
439163953Srrs	struct mbuf *m_reply = NULL;
440252889Stuexen	struct sockaddr_storage sa_store;
441221249Stuexen	struct sctp_paramhdr *ph;
442266054Stuexen	uint16_t param_type, aparam_length;
443266054Stuexen
444266054Stuexen#if defined(INET) || defined(INET6)
445266054Stuexen	uint16_t param_length;
446266054Stuexen
447266054Stuexen#endif
448163953Srrs	struct sockaddr *sa;
449163953Srrs	int zero_address = 0;
450163953Srrs
451221249Stuexen#ifdef INET
452221249Stuexen	struct sockaddr_in *sin;
453221249Stuexen	struct sctp_ipv4addr_param *v4addr;
454221249Stuexen
455221249Stuexen#endif
456163953Srrs#ifdef INET6
457163953Srrs	struct sockaddr_in6 *sin6;
458163953Srrs	struct sctp_ipv6addr_param *v6addr;
459163953Srrs
460221249Stuexen#endif
461163953Srrs
462163953Srrs	aparam_length = ntohs(aph->ph.param_length);
463221249Stuexen	ph = (struct sctp_paramhdr *)(aph + 1);
464221249Stuexen	param_type = ntohs(ph->param_type);
465266054Stuexen#if defined(INET) || defined(INET6)
466221249Stuexen	param_length = ntohs(ph->param_length);
467266054Stuexen#endif
468163953Srrs	sa = (struct sockaddr *)&sa_store;
469163953Srrs	switch (param_type) {
470221249Stuexen#ifdef INET
471163953Srrs	case SCTP_IPV4_ADDRESS:
472163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
473163953Srrs			/* invalid param size */
474231039Stuexen			return (NULL);
475163953Srrs		}
476221249Stuexen		v4addr = (struct sctp_ipv4addr_param *)ph;
477163953Srrs		sin = (struct sockaddr_in *)&sa_store;
478163953Srrs		bzero(sin, sizeof(*sin));
479163953Srrs		sin->sin_family = AF_INET;
480163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
481163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
482163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
483163953Srrs			zero_address = 1;
484169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
485169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
486163953Srrs		break;
487221249Stuexen#endif
488221249Stuexen#ifdef INET6
489163953Srrs	case SCTP_IPV6_ADDRESS:
490163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
491163953Srrs			/* invalid param size */
492231039Stuexen			return (NULL);
493163953Srrs		}
494221249Stuexen		v6addr = (struct sctp_ipv6addr_param *)ph;
495163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
496163953Srrs		bzero(sin6, sizeof(*sin6));
497163953Srrs		sin6->sin6_family = AF_INET6;
498163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
499163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
500163953Srrs		    sizeof(struct in6_addr));
501163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
502163953Srrs			zero_address = 1;
503169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
504169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
505221249Stuexen		break;
506169420Srrs#endif
507163953Srrs	default:
508163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
509163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
510163953Srrs		    aparam_length);
511231039Stuexen		return (m_reply);
512163953Srrs	}
513163953Srrs
514163953Srrs	/* if 0.0.0.0/::0, use the source address instead */
515179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
516252889Stuexen		sa = src;
517169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
518169420Srrs		    "process_asconf_set_primary: using source addr ");
519252889Stuexen		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
520163953Srrs	}
521163953Srrs	/* set the primary address */
522163953Srrs	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
523169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
524169420Srrs		    "process_asconf_set_primary: primary address set\n");
525163953Srrs		/* notify upper layer */
526172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
527224641Stuexen		if ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_REACHABLE) &&
528224641Stuexen		    (!(stcb->asoc.primary_destination->dest_state & SCTP_ADDR_PF)) &&
529224641Stuexen		    (stcb->asoc.alternate)) {
530224641Stuexen			sctp_free_remote_addr(stcb->asoc.alternate);
531224641Stuexen			stcb->asoc.alternate = NULL;
532224641Stuexen		}
533163953Srrs		if (response_required) {
534163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
535163953Srrs		}
536172091Srrs		/*
537172091Srrs		 * Mobility adaptation. Ideally, when the reception of SET
538172091Srrs		 * PRIMARY with DELETE IP ADDRESS of the previous primary
539172091Srrs		 * destination, unacknowledged DATA are retransmitted
540172091Srrs		 * immediately to the new primary destination for seamless
541206137Stuexen		 * handover. If the destination is UNCONFIRMED and marked to
542206137Stuexen		 * REQ_PRIM, The retransmission occur when reception of the
543206137Stuexen		 * HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
544172091Srrs		 * sctp_input.c) Also, when change of the primary
545172091Srrs		 * destination, it is better that all subsequent new DATA
546172091Srrs		 * containing already queued DATA are transmitted to the new
547172091Srrs		 * primary destination. (by micchie)
548172091Srrs		 */
549172091Srrs		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
550172091Srrs		    SCTP_MOBILITY_BASE) ||
551172091Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
552172091Srrs		    SCTP_MOBILITY_FASTHANDOFF)) &&
553172091Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
554172091Srrs		    SCTP_MOBILITY_PRIM_DELETED) &&
555172091Srrs		    (stcb->asoc.primary_destination->dest_state &
556172091Srrs		    SCTP_ADDR_UNCONFIRMED) == 0) {
557172091Srrs
558172091Srrs			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
559172091Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
560172091Srrs			    SCTP_MOBILITY_FASTHANDOFF)) {
561172091Srrs				sctp_assoc_immediate_retrans(stcb,
562172091Srrs				    stcb->asoc.primary_destination);
563172091Srrs			}
564172091Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
565172091Srrs			    SCTP_MOBILITY_BASE)) {
566212712Stuexen				sctp_move_chunks_from_net(stcb,
567212712Stuexen				    stcb->asoc.deleted_primary);
568172091Srrs			}
569172091Srrs			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
570172091Srrs			    stcb->asoc.deleted_primary);
571172091Srrs		}
572163953Srrs	} else {
573163953Srrs		/* couldn't set the requested primary address! */
574169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
575169420Srrs		    "process_asconf_set_primary: set primary failed!\n");
576163953Srrs		/* must have been an invalid address, so report */
577163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
578163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
579163953Srrs		    aparam_length);
580163953Srrs	}
581163953Srrs
582231039Stuexen	return (m_reply);
583163953Srrs}
584163953Srrs
585163953Srrs/*
586163953Srrs * handles an ASCONF chunk.
587163953Srrs * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
588163953Srrs */
589163953Srrsvoid
590163953Srrssctp_handle_asconf(struct mbuf *m, unsigned int offset,
591252889Stuexen    struct sockaddr *src,
592171990Srrs    struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
593171990Srrs    int first)
594163953Srrs{
595163953Srrs	struct sctp_association *asoc;
596163953Srrs	uint32_t serial_num;
597171990Srrs	struct mbuf *n, *m_ack, *m_result, *m_tail;
598163953Srrs	struct sctp_asconf_ack_chunk *ack_cp;
599163953Srrs	struct sctp_asconf_paramhdr *aph, *ack_aph;
600163953Srrs	struct sctp_ipv6addr_param *p_addr;
601224641Stuexen	unsigned int asconf_limit, cnt;
602163953Srrs	int error = 0;		/* did an error occur? */
603163953Srrs
604163953Srrs	/* asconf param buffer */
605166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
606171990Srrs	struct sctp_asconf_ack *ack, *ack_next;
607163953Srrs
608163953Srrs	/* verify minimum length */
609163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
610169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
611169420Srrs		    "handle_asconf: chunk too small = %xh\n",
612169420Srrs		    ntohs(cp->ch.chunk_length));
613163953Srrs		return;
614163953Srrs	}
615163953Srrs	asoc = &stcb->asoc;
616163953Srrs	serial_num = ntohl(cp->serial_number);
617163953Srrs
618216825Stuexen	if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
619163953Srrs		/* got a duplicate ASCONF */
620169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
621169420Srrs		    "handle_asconf: got duplicate serial number = %xh\n",
622169420Srrs		    serial_num);
623163953Srrs		return;
624163953Srrs	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
625169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
626169420Srrs		    serial_num, asoc->asconf_seq_in + 1);
627163953Srrs		return;
628163953Srrs	}
629163953Srrs	/* it's the expected "next" sequence number, so process it */
630163953Srrs	asoc->asconf_seq_in = serial_num;	/* update sequence */
631163953Srrs	/* get length of all the param's in the ASCONF */
632163953Srrs	asconf_limit = offset + ntohs(cp->ch.chunk_length);
633169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1,
634169420Srrs	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
635169420Srrs	    asconf_limit, serial_num);
636171990Srrs
637171990Srrs	if (first) {
638171990Srrs		/* delete old cache */
639221249Stuexen		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: Now processing first ASCONF. Try to delete old cache\n");
640171990Srrs
641216822Stuexen		TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
642171990Srrs			if (ack->serial_number == serial_num)
643171990Srrs				break;
644171990Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: delete old(%u) < first(%u)\n",
645171990Srrs			    ack->serial_number, serial_num);
646216822Stuexen			TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
647171990Srrs			if (ack->data != NULL) {
648171990Srrs				sctp_m_freem(ack->data);
649171990Srrs			}
650179783Srrs			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
651171990Srrs		}
652163953Srrs	}
653165647Srrs	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
654163953Srrs	    M_DONTWAIT, 1, MT_DATA);
655163953Srrs	if (m_ack == NULL) {
656169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
657169420Srrs		    "handle_asconf: couldn't get mbuf!\n");
658163953Srrs		return;
659163953Srrs	}
660163953Srrs	m_tail = m_ack;		/* current reply chain's tail */
661163953Srrs
662163953Srrs	/* fill in ASCONF-ACK header */
663163953Srrs	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
664163953Srrs	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
665163953Srrs	ack_cp->ch.chunk_flags = 0;
666163953Srrs	ack_cp->serial_number = htonl(serial_num);
667163953Srrs	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
668165647Srrs	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
669163953Srrs	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
670163953Srrs
671163953Srrs	/* skip the lookup address parameter */
672163953Srrs	offset += sizeof(struct sctp_asconf_chunk);
673163953Srrs	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
674163953Srrs	if (p_addr == NULL) {
675169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
676169420Srrs		    "handle_asconf: couldn't get lookup addr!\n");
677163953Srrs		/* respond with a missing/invalid mandatory parameter error */
678163953Srrs		return;
679163953Srrs	}
680163953Srrs	/* param_length is already validated in process_control... */
681163953Srrs	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
682163953Srrs
683163953Srrs	/* get pointer to first asconf param in ASCONF-ACK */
684163953Srrs	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
685163953Srrs	if (ack_aph == NULL) {
686169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Gak in asconf2\n");
687163953Srrs		return;
688163953Srrs	}
689163953Srrs	/* get pointer to first asconf param in ASCONF */
690163953Srrs	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
691163953Srrs	if (aph == NULL) {
692169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
693163953Srrs		goto send_reply;
694163953Srrs	}
695163953Srrs	/* process through all parameters */
696224641Stuexen	cnt = 0;
697163953Srrs	while (aph != NULL) {
698163953Srrs		unsigned int param_length, param_type;
699163953Srrs
700163953Srrs		param_type = ntohs(aph->ph.param_type);
701163953Srrs		param_length = ntohs(aph->ph.param_length);
702163953Srrs		if (offset + param_length > asconf_limit) {
703163953Srrs			/* parameter goes beyond end of chunk! */
704163953Srrs			sctp_m_freem(m_ack);
705163953Srrs			return;
706163953Srrs		}
707163953Srrs		m_result = NULL;
708163953Srrs
709163953Srrs		if (param_length > sizeof(aparam_buf)) {
710169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
711163953Srrs			sctp_m_freem(m_ack);
712163953Srrs			return;
713163953Srrs		}
714163953Srrs		if (param_length <= sizeof(struct sctp_paramhdr)) {
715169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
716163953Srrs			sctp_m_freem(m_ack);
717163953Srrs		}
718163953Srrs		/* get the entire parameter */
719163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
720163953Srrs		if (aph == NULL) {
721169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
722163953Srrs			sctp_m_freem(m_ack);
723163953Srrs			return;
724163953Srrs		}
725163953Srrs		switch (param_type) {
726163953Srrs		case SCTP_ADD_IP_ADDRESS:
727163953Srrs			asoc->peer_supports_asconf = 1;
728252889Stuexen			m_result = sctp_process_asconf_add_ip(src, aph, stcb,
729224641Stuexen			    (cnt < SCTP_BASE_SYSCTL(sctp_hb_maxburst)), error);
730224641Stuexen			cnt++;
731163953Srrs			break;
732163953Srrs		case SCTP_DEL_IP_ADDRESS:
733163953Srrs			asoc->peer_supports_asconf = 1;
734252889Stuexen			m_result = sctp_process_asconf_delete_ip(src, aph, stcb,
735163953Srrs			    error);
736163953Srrs			break;
737163953Srrs		case SCTP_ERROR_CAUSE_IND:
738163953Srrs			/* not valid in an ASCONF chunk */
739163953Srrs			break;
740163953Srrs		case SCTP_SET_PRIM_ADDR:
741163953Srrs			asoc->peer_supports_asconf = 1;
742252889Stuexen			m_result = sctp_process_asconf_set_primary(src, aph,
743163953Srrs			    stcb, error);
744163953Srrs			break;
745185694Srrs		case SCTP_NAT_VTAGS:
746185694Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
747185694Srrs			break;
748163953Srrs		case SCTP_SUCCESS_REPORT:
749163953Srrs			/* not valid in an ASCONF chunk */
750163953Srrs			break;
751163953Srrs		case SCTP_ULP_ADAPTATION:
752163953Srrs			/* FIX */
753163953Srrs			break;
754163953Srrs		default:
755163953Srrs			if ((param_type & 0x8000) == 0) {
756163953Srrs				/* Been told to STOP at this param */
757163953Srrs				asconf_limit = offset;
758163953Srrs				/*
759163953Srrs				 * FIX FIX - We need to call
760163953Srrs				 * sctp_arethere_unrecognized_parameters()
761163953Srrs				 * to get a operr and send it for any
762163953Srrs				 * param's with the 0x4000 bit set OR do it
763163953Srrs				 * here ourselves... note we still must STOP
764163953Srrs				 * if the 0x8000 bit is clear.
765163953Srrs				 */
766163953Srrs			}
767163953Srrs			/* unknown/invalid param type */
768163953Srrs			break;
769163953Srrs		}		/* switch */
770163953Srrs
771163953Srrs		/* add any (error) result to the reply mbuf chain */
772163953Srrs		if (m_result != NULL) {
773165647Srrs			SCTP_BUF_NEXT(m_tail) = m_result;
774163953Srrs			m_tail = m_result;
775163953Srrs			/* update lengths, make sure it's aligned too */
776165647Srrs			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
777165647Srrs			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
778163953Srrs			/* set flag to force success reports */
779163953Srrs			error = 1;
780163953Srrs		}
781163953Srrs		offset += SCTP_SIZE32(param_length);
782163953Srrs		/* update remaining ASCONF message length to process */
783163953Srrs		if (offset >= asconf_limit) {
784163953Srrs			/* no more data in the mbuf chain */
785163953Srrs			break;
786163953Srrs		}
787163953Srrs		/* get pointer to next asconf param */
788163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
789163953Srrs		    sizeof(struct sctp_asconf_paramhdr),
790163953Srrs		    (uint8_t *) & aparam_buf);
791163953Srrs		if (aph == NULL) {
792163953Srrs			/* can't get an asconf paramhdr */
793169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
794163953Srrs			/* FIX ME - add error here... */
795163953Srrs		}
796169420Srrs	}
797163953Srrs
798163953Srrssend_reply:
799163953Srrs	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
800163953Srrs	/* save the ASCONF-ACK reply */
801179783Srrs	ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
802171990Srrs	    struct sctp_asconf_ack);
803171990Srrs	if (ack == NULL) {
804171990Srrs		sctp_m_freem(m_ack);
805171990Srrs		return;
806171990Srrs	}
807171990Srrs	ack->serial_number = serial_num;
808171990Srrs	ack->last_sent_to = NULL;
809171990Srrs	ack->data = m_ack;
810209178Stuexen	ack->len = 0;
811221249Stuexen	for (n = m_ack; n != NULL; n = SCTP_BUF_NEXT(n)) {
812171990Srrs		ack->len += SCTP_BUF_LEN(n);
813171990Srrs	}
814171990Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
815163953Srrs
816163953Srrs	/* see if last_control_chunk_from is set properly (use IP src addr) */
817163953Srrs	if (stcb->asoc.last_control_chunk_from == NULL) {
818163953Srrs		/*
819163953Srrs		 * this could happen if the source address was just newly
820163953Srrs		 * added
821163953Srrs		 */
822169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
823237904Stuexen		SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
824237904Stuexen		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
825237904Stuexen		/* look up the from address */
826237904Stuexen		stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, src);
827237904Stuexen#ifdef SCTP_DEBUG
828237904Stuexen		if (stcb->asoc.last_control_chunk_from == NULL) {
829237904Stuexen			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
830163953Srrs		}
831169420Srrs#endif
832163953Srrs	}
833163953Srrs}
834163953Srrs
835163953Srrs/*
836163953Srrs * does the address match? returns 0 if not, 1 if so
837163953Srrs */
838163953Srrsstatic uint32_t
839163953Srrssctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
840163953Srrs{
841221249Stuexen	switch (sa->sa_family) {
842163953Srrs#ifdef INET6
843221249Stuexen	case AF_INET6:
844221249Stuexen		{
845221249Stuexen			/* XXX scopeid */
846221249Stuexen			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
847163953Srrs
848221249Stuexen			if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
849221249Stuexen			    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
850221249Stuexen			    sizeof(struct in6_addr)) == 0)) {
851221249Stuexen				return (1);
852221249Stuexen			}
853221249Stuexen			break;
854163953Srrs		}
855221249Stuexen#endif
856221249Stuexen#ifdef INET
857221249Stuexen	case AF_INET:
858221249Stuexen		{
859221249Stuexen			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
860163953Srrs
861221249Stuexen			if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
862221249Stuexen			    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
863221249Stuexen			    sizeof(struct in_addr)) == 0)) {
864221249Stuexen				return (1);
865221249Stuexen			}
866221249Stuexen			break;
867163953Srrs		}
868221249Stuexen#endif
869221249Stuexen	default:
870221249Stuexen		break;
871163953Srrs	}
872163953Srrs	return (0);
873163953Srrs}
874163953Srrs
875163953Srrs/*
876179157Srrs * does the address match? returns 0 if not, 1 if so
877179157Srrs */
878179157Srrsstatic uint32_t
879221249Stuexensctp_addr_match(struct sctp_paramhdr *ph, struct sockaddr *sa)
880179157Srrs{
881266054Stuexen#if defined(INET) || defined(INET6)
882179157Srrs	uint16_t param_type, param_length;
883179157Srrs
884221249Stuexen	param_type = ntohs(ph->param_type);
885221249Stuexen	param_length = ntohs(ph->param_length);
886266054Stuexen#endif
887221249Stuexen	switch (sa->sa_family) {
888179157Srrs#ifdef INET6
889221249Stuexen	case AF_INET6:
890221249Stuexen		{
891221249Stuexen			/* XXX scopeid */
892221249Stuexen			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
893221249Stuexen			struct sctp_ipv6addr_param *v6addr;
894179157Srrs
895221249Stuexen			v6addr = (struct sctp_ipv6addr_param *)ph;
896221249Stuexen			if ((param_type == SCTP_IPV6_ADDRESS) &&
897266054Stuexen			    (param_length == sizeof(struct sctp_ipv6addr_param)) &&
898221249Stuexen			    (memcmp(&v6addr->addr, &sin6->sin6_addr,
899221249Stuexen			    sizeof(struct in6_addr)) == 0)) {
900221249Stuexen				return (1);
901221249Stuexen			}
902221249Stuexen			break;
903179157Srrs		}
904180955Srrs#endif
905221249Stuexen#ifdef INET
906221249Stuexen	case AF_INET:
907221249Stuexen		{
908221249Stuexen			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
909221249Stuexen			struct sctp_ipv4addr_param *v4addr;
910179157Srrs
911221249Stuexen			v4addr = (struct sctp_ipv4addr_param *)ph;
912221249Stuexen			if ((param_type == SCTP_IPV4_ADDRESS) &&
913266054Stuexen			    (param_length == sizeof(struct sctp_ipv4addr_param)) &&
914221249Stuexen			    (memcmp(&v4addr->addr, &sin->sin_addr,
915221249Stuexen			    sizeof(struct in_addr)) == 0)) {
916221249Stuexen				return (1);
917221249Stuexen			}
918221249Stuexen			break;
919179157Srrs		}
920221249Stuexen#endif
921221249Stuexen	default:
922221249Stuexen		break;
923179157Srrs	}
924179157Srrs	return (0);
925179157Srrs}
926179157Srrs
927179157Srrs/*
928163953Srrs * Cleanup for non-responded/OP ERR'd ASCONF
929163953Srrs */
930163953Srrsvoid
931163953Srrssctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
932163953Srrs{
933163953Srrs	/* mark peer as ASCONF incapable */
934163953Srrs	stcb->asoc.peer_supports_asconf = 0;
935163953Srrs	/*
936163953Srrs	 * clear out any existing asconfs going out
937163953Srrs	 */
938169655Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
939169655Srrs	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
940179157Srrs	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
941163953Srrs	/* remove the old ASCONF on our outbound queue */
942163953Srrs	sctp_toss_old_asconf(stcb);
943163953Srrs}
944163953Srrs
945163953Srrs/*
946171858Srrs * cleanup any cached source addresses that may be topologically
947171858Srrs * incorrect after a new address has been added to this interface.
948171858Srrs */
949171858Srrsstatic void
950171858Srrssctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
951171858Srrs{
952171858Srrs	struct sctp_nets *net;
953171858Srrs
954171858Srrs	/*
955171858Srrs	 * Ideally, we want to only clear cached routes and source addresses
956171858Srrs	 * that are topologically incorrect.  But since there is no easy way
957171858Srrs	 * to know whether the newly added address on the ifn would cause a
958171858Srrs	 * routing change (i.e. a new egress interface would be chosen)
959171858Srrs	 * without doing a new routing lookup and source address selection,
960171858Srrs	 * we will (for now) just flush any cached route using a different
961171858Srrs	 * ifn (and cached source addrs) and let output re-choose them
962171858Srrs	 * during the next send on that net.
963171858Srrs	 */
964171858Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
965171858Srrs		/*
966171858Srrs		 * clear any cached route (and cached source address) if the
967171858Srrs		 * route's interface is NOT the same as the address change.
968171858Srrs		 * If it's the same interface, just clear the cached source
969171858Srrs		 * address.
970171858Srrs		 */
971171858Srrs		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
972209178Stuexen		    ((ifn == NULL) ||
973209178Stuexen		    (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
974171858Srrs			/* clear any cached route */
975171858Srrs			RTFREE(net->ro.ro_rt);
976171858Srrs			net->ro.ro_rt = NULL;
977171858Srrs		}
978171858Srrs		/* clear any cached source address */
979171858Srrs		if (net->src_addr_selected) {
980171858Srrs			sctp_free_ifa(net->ro._s_addr);
981171858Srrs			net->ro._s_addr = NULL;
982171858Srrs			net->src_addr_selected = 0;
983171858Srrs		}
984171858Srrs	}
985171858Srrs}
986171858Srrs
987172091Srrs
988172091Srrsvoid
989172091Srrssctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
990172091Srrs{
991172091Srrs	int error;
992172091Srrs
993172118Srrs	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
994172118Srrs		return;
995172118Srrs	}
996172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
997172091Srrs		return;
998172091Srrs	}
999172091Srrs	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
1000172157Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
1001172091Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1002172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
1003172091Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
1004172091Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1005172091Srrs		    stcb->asoc.deleted_primary,
1006172091Srrs		    SCTP_FROM_SCTP_TIMER + SCTP_LOC_8);
1007172091Srrs		stcb->asoc.num_send_timers_up--;
1008172091Srrs		if (stcb->asoc.num_send_timers_up < 0) {
1009172091Srrs			stcb->asoc.num_send_timers_up = 0;
1010172091Srrs		}
1011172091Srrs		SCTP_TCB_LOCK_ASSERT(stcb);
1012172091Srrs		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1013172091Srrs		    stcb->asoc.deleted_primary);
1014172091Srrs		if (error) {
1015172091Srrs			SCTP_INP_DECR_REF(stcb->sctp_ep);
1016172091Srrs			return;
1017172091Srrs		}
1018172091Srrs		SCTP_TCB_LOCK_ASSERT(stcb);
1019172091Srrs#ifdef SCTP_AUDITING_ENABLED
1020207099Stuexen		sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
1021172091Srrs#endif
1022172091Srrs		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1023172091Srrs		if ((stcb->asoc.num_send_timers_up == 0) &&
1024172091Srrs		    (stcb->asoc.sent_queue_cnt > 0)) {
1025172091Srrs			struct sctp_tmit_chunk *chk;
1026172091Srrs
1027172091Srrs			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1028172091Srrs			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
1029172091Srrs			    stcb, chk->whoTo);
1030172091Srrs		}
1031172091Srrs	}
1032172091Srrs	return;
1033172091Srrs}
1034172091Srrs
1035171990Srrsstatic int
1036171990Srrs    sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
1037171990Srrs
1038172091Srrsvoid
1039171990Srrssctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
1040171990Srrs{
1041171990Srrs	struct sctp_tmit_chunk *chk;
1042171990Srrs
1043172157Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
1044171990Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
1045171990Srrs	    SCTP_FROM_SCTP_TIMER + SCTP_LOC_5);
1046171990Srrs	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
1047171990Srrs	net->error_count = 0;
1048171990Srrs	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1049171990Srrs		if (chk->whoTo == net) {
1050172091Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
1051172091Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1052172091Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1053172396Srrs				sctp_flight_size_decrease(chk);
1054172396Srrs				sctp_total_flight_decrease(stcb, chk);
1055172396Srrs				net->marked_retrans++;
1056172396Srrs				stcb->asoc.marked_retrans++;
1057172091Srrs			}
1058171990Srrs		}
1059171990Srrs	}
1060172396Srrs	if (net->marked_retrans) {
1061172396Srrs		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1062172396Srrs	}
1063171990Srrs}
1064171990Srrs
1065171990Srrsstatic void
1066171990Srrssctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
1067171990Srrs{
1068171990Srrs	struct sctp_nets *net;
1069171990Srrs	int addrnum, changed;
1070171990Srrs
1071171990Srrs	/*
1072171990Srrs	 * If number of local valid addresses is 1, the valid address is
1073206137Stuexen	 * probably newly added address. Several valid addresses in this
1074171990Srrs	 * association.  A source address may not be changed.  Additionally,
1075171990Srrs	 * they can be configured on a same interface as "alias" addresses.
1076171990Srrs	 * (by micchie)
1077171990Srrs	 */
1078171990Srrs	addrnum = sctp_local_addr_count(stcb);
1079171990Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
1080171990Srrs	    addrnum);
1081171990Srrs	if (addrnum == 1) {
1082171990Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1083171990Srrs			/* clear any cached route and source address */
1084171990Srrs			if (net->ro.ro_rt) {
1085171990Srrs				RTFREE(net->ro.ro_rt);
1086171990Srrs				net->ro.ro_rt = NULL;
1087171990Srrs			}
1088171990Srrs			if (net->src_addr_selected) {
1089171990Srrs				sctp_free_ifa(net->ro._s_addr);
1090171990Srrs				net->ro._s_addr = NULL;
1091171990Srrs				net->src_addr_selected = 0;
1092171990Srrs			}
1093171990Srrs			/* Retransmit unacknowledged DATA chunks immediately */
1094171990Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1095171990Srrs			    SCTP_MOBILITY_FASTHANDOFF)) {
1096171990Srrs				sctp_net_immediate_retrans(stcb, net);
1097171990Srrs			}
1098171990Srrs			/* also, SET PRIMARY is maybe already sent */
1099171990Srrs		}
1100171990Srrs		return;
1101171990Srrs	}
1102171990Srrs	/* Multiple local addresses exsist in the association.  */
1103171990Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1104171990Srrs		/* clear any cached route and source address */
1105171990Srrs		if (net->ro.ro_rt) {
1106171990Srrs			RTFREE(net->ro.ro_rt);
1107171990Srrs			net->ro.ro_rt = NULL;
1108171990Srrs		}
1109171990Srrs		if (net->src_addr_selected) {
1110171990Srrs			sctp_free_ifa(net->ro._s_addr);
1111171990Srrs			net->ro._s_addr = NULL;
1112171990Srrs			net->src_addr_selected = 0;
1113171990Srrs		}
1114171990Srrs		/*
1115171990Srrs		 * Check if the nexthop is corresponding to the new address.
1116171990Srrs		 * If the new address is corresponding to the current
1117206137Stuexen		 * nexthop, the path will be changed. If the new address is
1118171990Srrs		 * NOT corresponding to the current nexthop, the path will
1119171990Srrs		 * not be changed.
1120171990Srrs		 */
1121171990Srrs		SCTP_RTALLOC((sctp_route_t *) & net->ro,
1122171990Srrs		    stcb->sctp_ep->def_vrf_id);
1123171990Srrs		if (net->ro.ro_rt == NULL)
1124171990Srrs			continue;
1125171990Srrs
1126172137Srrs		changed = 0;
1127221249Stuexen		switch (net->ro._l_addr.sa.sa_family) {
1128221249Stuexen#ifdef INET
1129221249Stuexen		case AF_INET:
1130221249Stuexen			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *) & net->ro)) {
1131171990Srrs				changed = 1;
1132221249Stuexen			}
1133221249Stuexen			break;
1134221249Stuexen#endif
1135178251Srrs#ifdef INET6
1136221249Stuexen		case AF_INET6:
1137171990Srrs			if (sctp_v6src_match_nexthop(
1138221249Stuexen			    &newifa->address.sin6, (sctp_route_t *) & net->ro)) {
1139171990Srrs				changed = 1;
1140221249Stuexen			}
1141221249Stuexen			break;
1142221249Stuexen#endif
1143221249Stuexen		default:
1144221249Stuexen			break;
1145171990Srrs		}
1146171990Srrs		/*
1147171990Srrs		 * if the newly added address does not relate routing
1148171990Srrs		 * information, we skip.
1149171990Srrs		 */
1150171990Srrs		if (changed == 0)
1151171990Srrs			continue;
1152171990Srrs		/* Retransmit unacknowledged DATA chunks immediately */
1153171990Srrs		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1154171990Srrs		    SCTP_MOBILITY_FASTHANDOFF)) {
1155171990Srrs			sctp_net_immediate_retrans(stcb, net);
1156171990Srrs		}
1157171990Srrs		/* Send SET PRIMARY for this new address */
1158171990Srrs		if (net == stcb->asoc.primary_destination) {
1159171990Srrs			(void)sctp_asconf_queue_mgmt(stcb, newifa,
1160171990Srrs			    SCTP_SET_PRIM_ADDR);
1161171990Srrs		}
1162171990Srrs	}
1163171990Srrs}
1164171990Srrs
1165171858Srrs/*
1166163953Srrs * process an ADD/DELETE IP ack from peer.
1167171572Srrs * addr: corresponding sctp_ifa to the address being added/deleted.
1168163953Srrs * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1169163953Srrs * flag: 1=success, 0=failure.
1170163953Srrs */
1171163953Srrsstatic void
1172231038Stuexensctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr, uint32_t flag)
1173163953Srrs{
1174163953Srrs	/*
1175163953Srrs	 * do the necessary asoc list work- if we get a failure indication,
1176171858Srrs	 * leave the address on the assoc's restricted list.  If we get a
1177171858Srrs	 * success indication, remove the address from the restricted list.
1178163953Srrs	 */
1179163953Srrs	/*
1180163953Srrs	 * Note: this will only occur for ADD_IP_ADDRESS, since
1181163953Srrs	 * DEL_IP_ADDRESS is never actually added to the list...
1182163953Srrs	 */
1183163953Srrs	if (flag) {
1184171572Srrs		/* success case, so remove from the restricted list */
1185171572Srrs		sctp_del_local_addr_restricted(stcb, addr);
1186171858Srrs
1187172190Srrs		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1188172396Srrs		    SCTP_MOBILITY_BASE) ||
1189172396Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1190172396Srrs		    SCTP_MOBILITY_FASTHANDOFF)) {
1191171990Srrs			sctp_path_check_and_react(stcb, addr);
1192171990Srrs			return;
1193171990Srrs		}
1194172190Srrs		/* clear any cached/topologically incorrect source addresses */
1195171858Srrs		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1196163953Srrs	}
1197163953Srrs	/* else, leave it on the list */
1198163953Srrs}
1199163953Srrs
1200163953Srrs/*
1201171572Srrs * add an asconf add/delete/set primary IP address parameter to the queue.
1202163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1203171572Srrs * returns 0 if queued, -1 if not queued/removed.
1204171572Srrs * NOTE: if adding, but a delete for the same address is already scheduled
1205171572Srrs * (and not yet sent out), simply remove it from queue.  Same for deleting
1206171572Srrs * an address already scheduled for add.  If a duplicate operation is found,
1207171572Srrs * ignore the new one.
1208163953Srrs */
1209171572Srrsstatic int
1210171572Srrssctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1211171477Srrs    uint16_t type)
1212163953Srrs{
1213163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1214163953Srrs
1215163953Srrs	/* make sure the request isn't already in the queue */
1216216822Stuexen	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1217163953Srrs		/* address match? */
1218167598Srrs		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1219163953Srrs			continue;
1220179157Srrs		/*
1221179157Srrs		 * is the request already in queue but not sent? pass the
1222179157Srrs		 * request already sent in order to resolve the following
1223179157Srrs		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1224179157Srrs		 * can't remove the ADD request already sent 3. arrival of
1225179157Srrs		 * ADD
1226179157Srrs		 */
1227179157Srrs		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1228163953Srrs			return (-1);
1229163953Srrs		}
1230163953Srrs		/* is the negative request already in queue, and not sent */
1231171572Srrs		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
1232171572Srrs		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1233171572Srrs			/* add requested, delete already queued */
1234163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1235171572Srrs			/* remove the ifa from the restricted list */
1236171572Srrs			sctp_del_local_addr_restricted(stcb, ifa);
1237171572Srrs			/* free the asconf param */
1238170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1239171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
1240163953Srrs			return (-1);
1241163953Srrs		}
1242171572Srrs		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
1243171572Srrs		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
1244171572Srrs			/* delete requested, add already queued */
1245171572Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1246171572Srrs			/* remove the aa->ifa from the restricted list */
1247171572Srrs			sctp_del_local_addr_restricted(stcb, aa->ifa);
1248171572Srrs			/* free the asconf param */
1249171572Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1250171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1251171572Srrs			return (-1);
1252171572Srrs		}
1253163953Srrs	}			/* for each aa */
1254163953Srrs
1255163953Srrs	/* adding new request to the queue */
1256171572Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1257171572Srrs	    SCTP_M_ASC_ADDR);
1258163953Srrs	if (aa == NULL) {
1259163953Srrs		/* didn't get memory */
1260171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1261163953Srrs		return (-1);
1262163953Srrs	}
1263185694Srrs	aa->special_del = 0;
1264163953Srrs	/* fill in asconf address parameter fields */
1265163953Srrs	/* top level elements are "networked" during send */
1266163953Srrs	aa->ap.aph.ph.param_type = type;
1267163953Srrs	aa->ifa = ifa;
1268168299Srrs	atomic_add_int(&ifa->refcount, 1);
1269163953Srrs	/* correlation_id filled in during send routine later... */
1270221249Stuexen	switch (ifa->address.sa.sa_family) {
1271221249Stuexen#ifdef INET6
1272221249Stuexen	case AF_INET6:
1273221249Stuexen		{
1274221249Stuexen			struct sockaddr_in6 *sin6;
1275163953Srrs
1276221249Stuexen			sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
1277221249Stuexen			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1278221249Stuexen			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1279221249Stuexen			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1280221249Stuexen			    sizeof(struct sctp_ipv6addr_param);
1281221249Stuexen			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1282221249Stuexen			    sizeof(struct in6_addr));
1283221249Stuexen			break;
1284221249Stuexen		}
1285221249Stuexen#endif
1286221249Stuexen#ifdef INET
1287221249Stuexen	case AF_INET:
1288221249Stuexen		{
1289221249Stuexen			struct sockaddr_in *sin;
1290163953Srrs
1291221249Stuexen			sin = (struct sockaddr_in *)&ifa->address.sa;
1292221249Stuexen			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1293221249Stuexen			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1294221249Stuexen			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1295221249Stuexen			    sizeof(struct sctp_ipv4addr_param);
1296221249Stuexen			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1297221249Stuexen			    sizeof(struct in_addr));
1298221249Stuexen			break;
1299221249Stuexen		}
1300221249Stuexen#endif
1301221249Stuexen	default:
1302163953Srrs		/* invalid family! */
1303170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1304172190Srrs		sctp_free_ifa(ifa);
1305163953Srrs		return (-1);
1306163953Srrs	}
1307163953Srrs	aa->sent = 0;		/* clear sent flag */
1308163953Srrs
1309179157Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1310163953Srrs#ifdef SCTP_DEBUG
1311218232Srrs	if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
1312179157Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
1313179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1314265956Stuexen			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1315179157Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
1316179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1317265956Stuexen			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1318179157Srrs		} else {
1319179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1320265956Stuexen			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1321163953Srrs		}
1322179157Srrs	}
1323169420Srrs#endif
1324163953Srrs
1325163953Srrs	return (0);
1326163953Srrs}
1327163953Srrs
1328171572Srrs
1329163953Srrs/*
1330171572Srrs * add an asconf operation for the given ifa and type.
1331171572Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1332171572Srrs * returns 0 if completed, -1 if not completed, 1 if immediate send is
1333171572Srrs * advisable.
1334171572Srrs */
1335171572Srrsstatic int
1336171572Srrssctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1337171572Srrs    uint16_t type)
1338171572Srrs{
1339171572Srrs	uint32_t status;
1340171572Srrs	int pending_delete_queued = 0;
1341171572Srrs
1342171572Srrs	/* see if peer supports ASCONF */
1343171572Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1344171572Srrs		return (-1);
1345171572Srrs	}
1346171572Srrs	/*
1347171572Srrs	 * if this is deleting the last address from the assoc, mark it as
1348171572Srrs	 * pending.
1349171572Srrs	 */
1350171572Srrs	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending &&
1351171572Srrs	    (sctp_local_addr_count(stcb) < 2)) {
1352171572Srrs		/* set the pending delete info only */
1353171572Srrs		stcb->asoc.asconf_del_pending = 1;
1354171572Srrs		stcb->asoc.asconf_addr_del_pending = ifa;
1355171572Srrs		atomic_add_int(&ifa->refcount, 1);
1356171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF2,
1357171572Srrs		    "asconf_queue_add: mark delete last address pending\n");
1358171572Srrs		return (-1);
1359171572Srrs	}
1360179157Srrs	/* queue an asconf parameter */
1361179157Srrs	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1362179157Srrs
1363171572Srrs	/*
1364171572Srrs	 * if this is an add, and there is a delete also pending (i.e. the
1365171572Srrs	 * last local address is being changed), queue the pending delete
1366171572Srrs	 * too.
1367171572Srrs	 */
1368179157Srrs	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
1369171572Srrs		/* queue in the pending delete */
1370171572Srrs		if (sctp_asconf_queue_mgmt(stcb,
1371171572Srrs		    stcb->asoc.asconf_addr_del_pending,
1372171572Srrs		    SCTP_DEL_IP_ADDRESS) == 0) {
1373171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queing pending delete\n");
1374171572Srrs			pending_delete_queued = 1;
1375171572Srrs			/* clear out the pending delete info */
1376171572Srrs			stcb->asoc.asconf_del_pending = 0;
1377171572Srrs			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
1378171572Srrs			stcb->asoc.asconf_addr_del_pending = NULL;
1379171572Srrs		}
1380171572Srrs	}
1381179157Srrs	if (pending_delete_queued) {
1382171572Srrs		struct sctp_nets *net;
1383171572Srrs
1384171572Srrs		/*
1385171572Srrs		 * since we know that the only/last address is now being
1386171572Srrs		 * changed in this case, reset the cwnd/rto on all nets to
1387171572Srrs		 * start as a new address and path.  Also clear the error
1388171572Srrs		 * counts to give the assoc the best chance to complete the
1389171572Srrs		 * address change.
1390171572Srrs		 */
1391171572Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1392171572Srrs			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
1393171572Srrs			    net);
1394171572Srrs			net->RTO = 0;
1395171572Srrs			net->error_count = 0;
1396171572Srrs		}
1397171572Srrs		stcb->asoc.overall_error_count = 0;
1398179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1399171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1400171943Srrs			    stcb->asoc.overall_error_count,
1401171943Srrs			    0,
1402171943Srrs			    SCTP_FROM_SCTP_ASCONF,
1403171943Srrs			    __LINE__);
1404171943Srrs		}
1405171572Srrs		/* queue in an advisory set primary too */
1406171572Srrs		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
1407171572Srrs		/* let caller know we should send this out immediately */
1408171572Srrs		status = 1;
1409171572Srrs	}
1410171572Srrs	return (status);
1411171572Srrs}
1412171572Srrs
1413172190Srrs/*-
1414172190Srrs * add an asconf delete IP address parameter to the queue by sockaddr and
1415172190Srrs * possibly with no sctp_ifa available.  This is only called by the routine
1416172190Srrs * that checks the addresses in an INIT-ACK against the current address list.
1417163953Srrs * returns 0 if completed, non-zero if not completed.
1418172190Srrs * NOTE: if an add is already scheduled (and not yet sent out), simply
1419172190Srrs * remove it from queue.  If a duplicate operation is found, ignore the
1420172190Srrs * new one.
1421163953Srrs */
1422171572Srrsstatic int
1423172190Srrssctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1424163953Srrs{
1425168299Srrs	struct sctp_ifa *ifa;
1426163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1427167598Srrs	uint32_t vrf_id;
1428163953Srrs
1429169420Srrs	if (stcb == NULL) {
1430169420Srrs		return (-1);
1431169420Srrs	}
1432163953Srrs	/* see if peer supports ASCONF */
1433163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1434163953Srrs		return (-1);
1435163953Srrs	}
1436163953Srrs	/* make sure the request isn't already in the queue */
1437216822Stuexen	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1438163953Srrs		/* address match? */
1439163953Srrs		if (sctp_asconf_addr_match(aa, sa) == 0)
1440163953Srrs			continue;
1441163953Srrs		/* is the request already in queue (sent or not) */
1442172190Srrs		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1443163953Srrs			return (-1);
1444163953Srrs		}
1445163953Srrs		/* is the negative request already in queue, and not sent */
1446163953Srrs		if (aa->sent == 1)
1447163953Srrs			continue;
1448172190Srrs		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1449172190Srrs			/* add already queued, so remove existing entry */
1450163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1451171572Srrs			sctp_del_local_addr_restricted(stcb, aa->ifa);
1452163953Srrs			/* free the entry */
1453170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1454163953Srrs			return (-1);
1455163953Srrs		}
1456163953Srrs	}			/* for each aa */
1457172190Srrs
1458172190Srrs	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1459168299Srrs	if (stcb) {
1460168299Srrs		vrf_id = stcb->asoc.vrf_id;
1461168299Srrs	} else {
1462168299Srrs		vrf_id = SCTP_DEFAULT_VRFID;
1463168299Srrs	}
1464172190Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
1465163953Srrs
1466163953Srrs	/* adding new request to the queue */
1467171572Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1468171572Srrs	    SCTP_M_ASC_ADDR);
1469163953Srrs	if (aa == NULL) {
1470163953Srrs		/* didn't get memory */
1471169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1472172190Srrs		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1473163953Srrs		return (-1);
1474163953Srrs	}
1475185694Srrs	aa->special_del = 0;
1476163953Srrs	/* fill in asconf address parameter fields */
1477163953Srrs	/* top level elements are "networked" during send */
1478172190Srrs	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1479168299Srrs	aa->ifa = ifa;
1480172190Srrs	if (ifa)
1481172190Srrs		atomic_add_int(&ifa->refcount, 1);
1482163953Srrs	/* correlation_id filled in during send routine later... */
1483221249Stuexen	switch (sa->sa_family) {
1484221249Stuexen#ifdef INET6
1485221249Stuexen	case AF_INET6:
1486221249Stuexen		{
1487221249Stuexen			/* IPv6 address */
1488221249Stuexen			struct sockaddr_in6 *sin6;
1489163953Srrs
1490221249Stuexen			sin6 = (struct sockaddr_in6 *)sa;
1491221249Stuexen			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1492221249Stuexen			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1493221249Stuexen			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1494221249Stuexen			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1495221249Stuexen			    sizeof(struct in6_addr));
1496221249Stuexen			break;
1497221249Stuexen		}
1498221249Stuexen#endif
1499221249Stuexen#ifdef INET
1500221249Stuexen	case AF_INET:
1501221249Stuexen		{
1502221249Stuexen			/* IPv4 address */
1503221249Stuexen			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1504163953Srrs
1505221249Stuexen			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1506221249Stuexen			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1507221249Stuexen			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1508221249Stuexen			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1509221249Stuexen			    sizeof(struct in_addr));
1510221249Stuexen			break;
1511221249Stuexen		}
1512221249Stuexen#endif
1513221249Stuexen	default:
1514163953Srrs		/* invalid family! */
1515170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1516172190Srrs		if (ifa)
1517172190Srrs			sctp_free_ifa(ifa);
1518163953Srrs		return (-1);
1519163953Srrs	}
1520163953Srrs	aa->sent = 0;		/* clear sent flag */
1521163953Srrs
1522172190Srrs	/* delete goes to the back of the queue */
1523172190Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1524163953Srrs
1525172218Srrs	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1526163953Srrs	return (0);
1527163953Srrs}
1528163953Srrs
1529163953Srrs/*
1530163953Srrs * find a specific asconf param on our "sent" queue
1531163953Srrs */
1532163953Srrsstatic struct sctp_asconf_addr *
1533163953Srrssctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1534163953Srrs{
1535163953Srrs	struct sctp_asconf_addr *aa;
1536163953Srrs
1537163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1538163953Srrs		if (aa->ap.aph.correlation_id == correlation_id &&
1539163953Srrs		    aa->sent == 1) {
1540163953Srrs			/* found it */
1541163953Srrs			return (aa);
1542163953Srrs		}
1543163953Srrs	}
1544163953Srrs	/* didn't find it */
1545163953Srrs	return (NULL);
1546163953Srrs}
1547163953Srrs
1548163953Srrs/*
1549163953Srrs * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1550163953Srrs * notifications based on the error response
1551163953Srrs */
1552163953Srrsstatic void
1553163953Srrssctp_asconf_process_error(struct sctp_tcb *stcb,
1554163953Srrs    struct sctp_asconf_paramhdr *aph)
1555163953Srrs{
1556163953Srrs	struct sctp_error_cause *eh;
1557163953Srrs	struct sctp_paramhdr *ph;
1558163953Srrs	uint16_t param_type;
1559163953Srrs	uint16_t error_code;
1560163953Srrs
1561163953Srrs	eh = (struct sctp_error_cause *)(aph + 1);
1562163953Srrs	ph = (struct sctp_paramhdr *)(eh + 1);
1563163953Srrs	/* validate lengths */
1564163953Srrs	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1565163953Srrs	    htons(aph->ph.param_length)) {
1566163953Srrs		/* invalid error cause length */
1567169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1568169420Srrs		    "asconf_process_error: cause element too long\n");
1569163953Srrs		return;
1570163953Srrs	}
1571163953Srrs	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1572163953Srrs	    htons(eh->length)) {
1573163953Srrs		/* invalid included TLV length */
1574169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1575169420Srrs		    "asconf_process_error: included TLV too long\n");
1576163953Srrs		return;
1577163953Srrs	}
1578163953Srrs	/* which error code ? */
1579163953Srrs	error_code = ntohs(eh->code);
1580163953Srrs	param_type = ntohs(aph->ph.param_type);
1581163953Srrs	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1582163953Srrs	switch (error_code) {
1583163953Srrs	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1584163953Srrs		/* we allow ourselves to "try again" for this error */
1585163953Srrs		break;
1586163953Srrs	default:
1587163953Srrs		/* peer can't handle it... */
1588163953Srrs		switch (param_type) {
1589163953Srrs		case SCTP_ADD_IP_ADDRESS:
1590163953Srrs		case SCTP_DEL_IP_ADDRESS:
1591163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1592163953Srrs			break;
1593163953Srrs		case SCTP_SET_PRIM_ADDR:
1594163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1595163953Srrs			break;
1596163953Srrs		default:
1597163953Srrs			break;
1598163953Srrs		}
1599163953Srrs	}
1600163953Srrs}
1601163953Srrs
1602163953Srrs/*
1603171477Srrs * process an asconf queue param.
1604171477Srrs * aparam: parameter to process, will be removed from the queue.
1605171477Srrs * flag: 1=success case, 0=failure case
1606163953Srrs */
1607163953Srrsstatic void
1608163953Srrssctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1609163953Srrs    struct sctp_asconf_addr *aparam, uint32_t flag)
1610163953Srrs{
1611163953Srrs	uint16_t param_type;
1612163953Srrs
1613163953Srrs	/* process this param */
1614163953Srrs	param_type = aparam->ap.aph.ph.param_type;
1615163953Srrs	switch (param_type) {
1616163953Srrs	case SCTP_ADD_IP_ADDRESS:
1617169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1618169420Srrs		    "process_param_ack: added IP address\n");
1619231038Stuexen		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, flag);
1620163953Srrs		break;
1621163953Srrs	case SCTP_DEL_IP_ADDRESS:
1622169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1623169420Srrs		    "process_param_ack: deleted IP address\n");
1624163953Srrs		/* nothing really to do... lists already updated */
1625163953Srrs		break;
1626163953Srrs	case SCTP_SET_PRIM_ADDR:
1627179157Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1628179157Srrs		    "process_param_ack: set primary IP address\n");
1629163953Srrs		/* nothing to do... peer may start using this addr */
1630163953Srrs		if (flag == 0)
1631163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1632163953Srrs		break;
1633163953Srrs	default:
1634163953Srrs		/* should NEVER happen */
1635163953Srrs		break;
1636163953Srrs	}
1637163953Srrs
1638163953Srrs	/* remove the param and free it */
1639163953Srrs	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1640172190Srrs	if (aparam->ifa)
1641172190Srrs		sctp_free_ifa(aparam->ifa);
1642170091Srrs	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1643163953Srrs}
1644163953Srrs
1645163953Srrs/*
1646163953Srrs * cleanup from a bad asconf ack parameter
1647163953Srrs */
1648163953Srrsstatic void
1649163953Srrssctp_asconf_ack_clear(struct sctp_tcb *stcb)
1650163953Srrs{
1651163953Srrs	/* assume peer doesn't really know how to do asconfs */
1652163953Srrs	stcb->asoc.peer_supports_asconf = 0;
1653163953Srrs	/* XXX we could free the pending queue here */
1654163953Srrs}
1655163953Srrs
1656163953Srrsvoid
1657163953Srrssctp_handle_asconf_ack(struct mbuf *m, int offset,
1658163953Srrs    struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1659172190Srrs    struct sctp_nets *net, int *abort_no_unlock)
1660163953Srrs{
1661163953Srrs	struct sctp_association *asoc;
1662163953Srrs	uint32_t serial_num;
1663163953Srrs	uint16_t ack_length;
1664163953Srrs	struct sctp_asconf_paramhdr *aph;
1665163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1666163953Srrs	uint32_t last_error_id = 0;	/* last error correlation id */
1667163953Srrs	uint32_t id;
1668163953Srrs	struct sctp_asconf_addr *ap;
1669163953Srrs
1670163953Srrs	/* asconf param buffer */
1671166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1672163953Srrs
1673163953Srrs	/* verify minimum length */
1674163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1675169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1676169420Srrs		    "handle_asconf_ack: chunk too small = %xh\n",
1677169420Srrs		    ntohs(cp->ch.chunk_length));
1678163953Srrs		return;
1679163953Srrs	}
1680163953Srrs	asoc = &stcb->asoc;
1681163953Srrs	serial_num = ntohl(cp->serial_number);
1682163953Srrs
1683163953Srrs	/*
1684163953Srrs	 * NOTE: we may want to handle this differently- currently, we will
1685163953Srrs	 * abort when we get an ack for the expected serial number + 1 (eg.
1686163953Srrs	 * we didn't send it), process an ack normally if it is the expected
1687163953Srrs	 * serial number, and re-send the previous ack for *ALL* other
1688163953Srrs	 * serial numbers
1689163953Srrs	 */
1690163953Srrs
1691163953Srrs	/*
1692163953Srrs	 * if the serial number is the next expected, but I didn't send it,
1693163953Srrs	 * abort the asoc, since someone probably just hijacked us...
1694163953Srrs	 */
1695163953Srrs	if (serial_num == (asoc->asconf_seq_out + 1)) {
1696169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1697237884Stuexen		sctp_abort_an_association(stcb->sctp_ep, stcb, NULL, SCTP_SO_NOT_LOCKED);
1698172190Srrs		*abort_no_unlock = 1;
1699163953Srrs		return;
1700163953Srrs	}
1701179157Srrs	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1702163953Srrs		/* got a duplicate/unexpected ASCONF-ACK */
1703169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1704179157Srrs		    serial_num, asoc->asconf_seq_out_acked + 1);
1705163953Srrs		return;
1706163953Srrs	}
1707179157Srrs	if (serial_num == asoc->asconf_seq_out - 1) {
1708179157Srrs		/* stop our timer */
1709179157Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
1710179157Srrs		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1711163953Srrs	}
1712163953Srrs	/* process the ASCONF-ACK contents */
1713163953Srrs	ack_length = ntohs(cp->ch.chunk_length) -
1714163953Srrs	    sizeof(struct sctp_asconf_ack_chunk);
1715163953Srrs	offset += sizeof(struct sctp_asconf_ack_chunk);
1716163953Srrs	/* process through all parameters */
1717163953Srrs	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1718163953Srrs		unsigned int param_length, param_type;
1719163953Srrs
1720163953Srrs		/* get pointer to next asconf parameter */
1721163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1722163953Srrs		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1723163953Srrs		if (aph == NULL) {
1724163953Srrs			/* can't get an asconf paramhdr */
1725163953Srrs			sctp_asconf_ack_clear(stcb);
1726163953Srrs			return;
1727163953Srrs		}
1728163953Srrs		param_type = ntohs(aph->ph.param_type);
1729163953Srrs		param_length = ntohs(aph->ph.param_length);
1730163953Srrs		if (param_length > ack_length) {
1731163953Srrs			sctp_asconf_ack_clear(stcb);
1732163953Srrs			return;
1733163953Srrs		}
1734163953Srrs		if (param_length < sizeof(struct sctp_paramhdr)) {
1735163953Srrs			sctp_asconf_ack_clear(stcb);
1736163953Srrs			return;
1737163953Srrs		}
1738163953Srrs		/* get the complete parameter... */
1739163953Srrs		if (param_length > sizeof(aparam_buf)) {
1740169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
1741169420Srrs			    "param length (%u) larger than buffer size!\n", param_length);
1742163953Srrs			sctp_asconf_ack_clear(stcb);
1743163953Srrs			return;
1744163953Srrs		}
1745163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1746163953Srrs		if (aph == NULL) {
1747163953Srrs			sctp_asconf_ack_clear(stcb);
1748163953Srrs			return;
1749163953Srrs		}
1750163953Srrs		/* correlation_id is transparent to peer, no ntohl needed */
1751163953Srrs		id = aph->correlation_id;
1752163953Srrs
1753163953Srrs		switch (param_type) {
1754163953Srrs		case SCTP_ERROR_CAUSE_IND:
1755163953Srrs			last_error_id = id;
1756163953Srrs			/* find the corresponding asconf param in our queue */
1757163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1758163953Srrs			if (ap == NULL) {
1759163953Srrs				/* hmm... can't find this in our queue! */
1760163953Srrs				break;
1761163953Srrs			}
1762163953Srrs			/* process the parameter, failed flag */
1763163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 0);
1764163953Srrs			/* process the error response */
1765163953Srrs			sctp_asconf_process_error(stcb, aph);
1766163953Srrs			break;
1767163953Srrs		case SCTP_SUCCESS_REPORT:
1768163953Srrs			/* find the corresponding asconf param in our queue */
1769163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1770163953Srrs			if (ap == NULL) {
1771163953Srrs				/* hmm... can't find this in our queue! */
1772163953Srrs				break;
1773163953Srrs			}
1774163953Srrs			/* process the parameter, success flag */
1775163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 1);
1776163953Srrs			break;
1777163953Srrs		default:
1778163953Srrs			break;
1779163953Srrs		}		/* switch */
1780163953Srrs
1781163953Srrs		/* update remaining ASCONF-ACK message length to process */
1782163953Srrs		ack_length -= SCTP_SIZE32(param_length);
1783163953Srrs		if (ack_length <= 0) {
1784163953Srrs			/* no more data in the mbuf chain */
1785163953Srrs			break;
1786163953Srrs		}
1787163953Srrs		offset += SCTP_SIZE32(param_length);
1788163953Srrs	}			/* while */
1789163953Srrs
1790163953Srrs	/*
1791163953Srrs	 * if there are any "sent" params still on the queue, these are
1792163953Srrs	 * implicitly "success", or "failed" (if we got an error back) ...
1793163953Srrs	 * so process these appropriately
1794163953Srrs	 *
1795163953Srrs	 * we assume that the correlation_id's are monotonically increasing
1796163953Srrs	 * beginning from 1 and that we don't have *that* many outstanding
1797163953Srrs	 * at any given time
1798163953Srrs	 */
1799163953Srrs	if (last_error_id == 0)
1800163953Srrs		last_error_id--;/* set to "max" value */
1801216822Stuexen	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1802163953Srrs		if (aa->sent == 1) {
1803163953Srrs			/*
1804163953Srrs			 * implicitly successful or failed if correlation_id
1805163953Srrs			 * < last_error_id, then success else, failure
1806163953Srrs			 */
1807163953Srrs			if (aa->ap.aph.correlation_id < last_error_id)
1808171477Srrs				sctp_asconf_process_param_ack(stcb, aa, 1);
1809163953Srrs			else
1810171477Srrs				sctp_asconf_process_param_ack(stcb, aa, 0);
1811163953Srrs		} else {
1812163953Srrs			/*
1813163953Srrs			 * since we always process in order (FIFO queue) if
1814163953Srrs			 * we reach one that hasn't been sent, the rest
1815163953Srrs			 * should not have been sent either. so, we're
1816163953Srrs			 * done...
1817163953Srrs			 */
1818163953Srrs			break;
1819163953Srrs		}
1820163953Srrs	}
1821163953Srrs
1822163953Srrs	/* update the next sequence number to use */
1823179157Srrs	asoc->asconf_seq_out_acked++;
1824163953Srrs	/* remove the old ASCONF on our outbound queue */
1825163953Srrs	sctp_toss_old_asconf(stcb);
1826163953Srrs	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1827171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
1828163953Srrs		/* we have more params, so restart our timer */
1829163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1830163953Srrs		    stcb, net);
1831171572Srrs#else
1832171572Srrs		/* we have more params, so send out more */
1833172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1834171572Srrs#endif
1835163953Srrs	}
1836163953Srrs}
1837163953Srrs
1838178251Srrs#ifdef INET6
1839163953Srrsstatic uint32_t
1840163953Srrssctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1841163953Srrs{
1842163953Srrs	struct sockaddr_in6 *sin6, *net6;
1843163953Srrs	struct sctp_nets *net;
1844163953Srrs
1845163953Srrs	if (sa->sa_family != AF_INET6) {
1846163953Srrs		/* wrong family */
1847163953Srrs		return (0);
1848163953Srrs	}
1849163953Srrs	sin6 = (struct sockaddr_in6 *)sa;
1850163953Srrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1851163953Srrs		/* not link local address */
1852163953Srrs		return (0);
1853163953Srrs	}
1854163953Srrs	/* hunt through our destination nets list for this scope_id */
1855163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1856163953Srrs		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1857163953Srrs		    AF_INET6)
1858163953Srrs			continue;
1859163953Srrs		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1860163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1861163953Srrs			continue;
1862163953Srrs		if (sctp_is_same_scope(sin6, net6)) {
1863163953Srrs			/* found one */
1864163953Srrs			return (1);
1865163953Srrs		}
1866163953Srrs	}
1867163953Srrs	/* didn't find one */
1868163953Srrs	return (0);
1869163953Srrs}
1870163953Srrs
1871178251Srrs#endif
1872178251Srrs
1873163953Srrs/*
1874163953Srrs * address management functions
1875163953Srrs */
1876163953Srrsstatic void
1877163953Srrssctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1878172190Srrs    struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1879163953Srrs{
1880163953Srrs	int status;
1881163953Srrs
1882224641Stuexen	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 ||
1883163953Srrs	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1884163953Srrs		/* subset bound, no ASCONF allowed case, so ignore */
1885163953Srrs		return;
1886163953Srrs	}
1887163953Srrs	/*
1888163953Srrs	 * note: we know this is not the subset bound, no ASCONF case eg.
1889163953Srrs	 * this is boundall or subset bound w/ASCONF allowed
1890163953Srrs	 */
1891163953Srrs
1892267799Stuexen	/* first, make sure that the address is IPv4 or IPv6 and not jailed */
1893221249Stuexen	switch (ifa->address.sa.sa_family) {
1894221249Stuexen#ifdef INET6
1895221249Stuexen	case AF_INET6:
1896267799Stuexen		if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1897267799Stuexen		    &ifa->address.sin6.sin6_addr) != 0) {
1898267799Stuexen			return;
1899267799Stuexen		}
1900221249Stuexen		break;
1901221249Stuexen#endif
1902221249Stuexen#ifdef INET
1903221249Stuexen	case AF_INET:
1904267799Stuexen		if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1905267799Stuexen		    &ifa->address.sin.sin_addr) != 0) {
1906267799Stuexen			return;
1907267799Stuexen		}
1908221249Stuexen		break;
1909221249Stuexen#endif
1910221249Stuexen	default:
1911163953Srrs		return;
1912163953Srrs	}
1913221249Stuexen#ifdef INET6
1914163953Srrs	/* make sure we're "allowed" to add this type of addr */
1915167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1916163953Srrs		/* invalid if we're not a v6 endpoint */
1917163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1918163953Srrs			return;
1919163953Srrs		/* is the v6 addr really valid ? */
1920167598Srrs		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1921163953Srrs			return;
1922163953Srrs		}
1923163953Srrs	}
1924221249Stuexen#endif
1925163953Srrs	/* put this address on the "pending/do not use yet" list */
1926171572Srrs	sctp_add_local_addr_restricted(stcb, ifa);
1927163953Srrs	/*
1928163953Srrs	 * check address scope if address is out of scope, don't queue
1929163953Srrs	 * anything... note: this would leave the address on both inp and
1930163953Srrs	 * asoc lists
1931163953Srrs	 */
1932178251Srrs	switch (ifa->address.sa.sa_family) {
1933178251Srrs#ifdef INET6
1934178251Srrs	case AF_INET6:
1935178251Srrs		{
1936178251Srrs			struct sockaddr_in6 *sin6;
1937163953Srrs
1938178251Srrs			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1939178251Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1940178251Srrs				/* we skip unspecifed addresses */
1941163953Srrs				return;
1942163953Srrs			}
1943178251Srrs			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1944252961Stuexen				if (stcb->asoc.scope.local_scope == 0) {
1945178251Srrs					return;
1946178251Srrs				}
1947178251Srrs				/* is it the right link local scope? */
1948178251Srrs				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1949178251Srrs					return;
1950178251Srrs				}
1951178251Srrs			}
1952252961Stuexen			if (stcb->asoc.scope.site_scope == 0 &&
1953178251Srrs			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1954163953Srrs				return;
1955163953Srrs			}
1956178251Srrs			break;
1957163953Srrs		}
1958178251Srrs#endif
1959221249Stuexen#ifdef INET
1960178251Srrs	case AF_INET:
1961178251Srrs		{
1962178251Srrs			struct sockaddr_in *sin;
1963178251Srrs			struct in6pcb *inp6;
1964163953Srrs
1965178251Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1966178251Srrs			/* invalid if we are a v6 only endpoint */
1967178251Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1968178251Srrs			    SCTP_IPV6_V6ONLY(inp6))
1969178251Srrs				return;
1970163953Srrs
1971178251Srrs			sin = (struct sockaddr_in *)&ifa->address.sa;
1972178251Srrs			if (sin->sin_addr.s_addr == 0) {
1973178251Srrs				/* we skip unspecifed addresses */
1974178251Srrs				return;
1975178251Srrs			}
1976252961Stuexen			if (stcb->asoc.scope.ipv4_local_scope == 0 &&
1977178251Srrs			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1978178251Srrs				return;
1979178251Srrs			}
1980178251Srrs			break;
1981163953Srrs		}
1982221249Stuexen#endif
1983178251Srrs	default:
1984163953Srrs		/* else, not AF_INET or AF_INET6, so skip */
1985163953Srrs		return;
1986163953Srrs	}
1987163953Srrs
1988163953Srrs	/* queue an asconf for this address add/delete */
1989163953Srrs	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1990163953Srrs		/* does the peer do asconf? */
1991163953Srrs		if (stcb->asoc.peer_supports_asconf) {
1992163953Srrs			/* queue an asconf for this addr */
1993163953Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
1994171572Srrs
1995163953Srrs			/*
1996171572Srrs			 * if queued ok, and in the open state, send out the
1997171572Srrs			 * ASCONF.  If in the non-open state, these will be
1998171572Srrs			 * sent when the state goes open.
1999163953Srrs			 */
2000163953Srrs			if (status == 0 &&
2001163953Srrs			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2002171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2003163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
2004163953Srrs				    stcb, stcb->asoc.primary_destination);
2005171572Srrs#else
2006224641Stuexen				sctp_send_asconf(stcb, NULL, addr_locked);
2007171572Srrs#endif
2008163953Srrs			}
2009163953Srrs		}
2010163953Srrs	}
2011163953Srrs}
2012163953Srrs
2013167598Srrs
2014167598Srrsint
2015231038Stuexensctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2016163953Srrs{
2017167598Srrs	struct sctp_asconf_iterator *asc;
2018167598Srrs	struct sctp_ifa *ifa;
2019167598Srrs	struct sctp_laddr *l;
2020167598Srrs	int cnt_invalid = 0;
2021163953Srrs
2022167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2023167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2024167598Srrs		ifa = l->ifa;
2025221249Stuexen		switch (ifa->address.sa.sa_family) {
2026221249Stuexen#ifdef INET6
2027221249Stuexen		case AF_INET6:
2028167598Srrs			/* invalid if we're not a v6 endpoint */
2029167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2030167598Srrs				cnt_invalid++;
2031167598Srrs				if (asc->cnt == cnt_invalid)
2032167598Srrs					return (1);
2033167598Srrs			}
2034221249Stuexen			break;
2035221249Stuexen#endif
2036221249Stuexen#ifdef INET
2037221249Stuexen		case AF_INET:
2038221249Stuexen			{
2039221249Stuexen				/* invalid if we are a v6 only endpoint */
2040221249Stuexen				struct in6pcb *inp6;
2041163953Srrs
2042221249Stuexen				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2043221249Stuexen				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2044221249Stuexen				    SCTP_IPV6_V6ONLY(inp6)) {
2045221249Stuexen					cnt_invalid++;
2046221249Stuexen					if (asc->cnt == cnt_invalid)
2047221249Stuexen						return (1);
2048221249Stuexen				}
2049221249Stuexen				break;
2050163953Srrs			}
2051221249Stuexen#endif
2052221249Stuexen		default:
2053167598Srrs			/* invalid address family */
2054167598Srrs			cnt_invalid++;
2055167598Srrs			if (asc->cnt == cnt_invalid)
2056167598Srrs				return (1);
2057163953Srrs		}
2058163953Srrs	}
2059167598Srrs	return (0);
2060163953Srrs}
2061163953Srrs
2062171572Srrsstatic int
2063231038Stuexensctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2064163953Srrs{
2065167598Srrs	struct sctp_ifa *ifa;
2066167598Srrs	struct sctp_asconf_iterator *asc;
2067167598Srrs	struct sctp_laddr *laddr, *nladdr, *l;
2068163953Srrs
2069167598Srrs	/* Only for specific case not bound all */
2070167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2071167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2072167598Srrs		ifa = l->ifa;
2073167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
2074169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2075169655Srrs			    sctp_nxt_addr) {
2076167598Srrs				if (laddr->ifa == ifa) {
2077167598Srrs					laddr->action = 0;
2078167598Srrs					break;
2079167598Srrs				}
2080167598Srrs			}
2081167598Srrs		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
2082216822Stuexen			LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
2083167598Srrs				/* remove only after all guys are done */
2084167598Srrs				if (laddr->ifa == ifa) {
2085167598Srrs					sctp_del_local_addr_ep(inp, ifa);
2086167598Srrs				}
2087167598Srrs			}
2088167598Srrs		}
2089163953Srrs	}
2090167598Srrs	return (0);
2091163953Srrs}
2092163953Srrs
2093167598Srrsvoid
2094171572Srrssctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2095231038Stuexen    void *ptr, uint32_t val SCTP_UNUSED)
2096163953Srrs{
2097167598Srrs	struct sctp_asconf_iterator *asc;
2098167598Srrs	struct sctp_ifa *ifa;
2099167598Srrs	struct sctp_laddr *l;
2100167598Srrs	int cnt_invalid = 0;
2101167598Srrs	int type, status;
2102171572Srrs	int num_queued = 0;
2103163953Srrs
2104167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2105167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2106167598Srrs		ifa = l->ifa;
2107167598Srrs		type = l->action;
2108170751Srrs
2109170751Srrs		/* address's vrf_id must be the vrf_id of the assoc */
2110170751Srrs		if (ifa->vrf_id != stcb->asoc.vrf_id) {
2111170751Srrs			continue;
2112170751Srrs		}
2113167598Srrs		/* Same checks again for assoc */
2114178251Srrs		switch (ifa->address.sa.sa_family) {
2115178251Srrs#ifdef INET6
2116178251Srrs		case AF_INET6:
2117178251Srrs			{
2118178251Srrs				/* invalid if we're not a v6 endpoint */
2119178251Srrs				struct sockaddr_in6 *sin6;
2120163953Srrs
2121178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2122178251Srrs					cnt_invalid++;
2123178251Srrs					if (asc->cnt == cnt_invalid)
2124178251Srrs						return;
2125178251Srrs					else
2126178251Srrs						continue;
2127167598Srrs				}
2128178251Srrs				sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2129178251Srrs				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2130178251Srrs					/* we skip unspecifed addresses */
2131167598Srrs					continue;
2132167598Srrs				}
2133267799Stuexen				if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2134267799Stuexen				    &sin6->sin6_addr) != 0) {
2135267799Stuexen					continue;
2136267799Stuexen				}
2137178251Srrs				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2138252961Stuexen					if (stcb->asoc.scope.local_scope == 0) {
2139178251Srrs						continue;
2140178251Srrs					}
2141178251Srrs					/* is it the right link local scope? */
2142178251Srrs					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2143178251Srrs						continue;
2144178251Srrs					}
2145178251Srrs				}
2146178251Srrs				break;
2147167598Srrs			}
2148178251Srrs#endif
2149221249Stuexen#ifdef INET
2150178251Srrs		case AF_INET:
2151178251Srrs			{
2152178251Srrs				/* invalid if we are a v6 only endpoint */
2153178251Srrs				struct in6pcb *inp6;
2154178251Srrs				struct sockaddr_in *sin;
2155163953Srrs
2156178251Srrs				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2157178251Srrs				/* invalid if we are a v6 only endpoint */
2158178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2159178251Srrs				    SCTP_IPV6_V6ONLY(inp6))
2160178251Srrs					continue;
2161167598Srrs
2162178251Srrs				sin = (struct sockaddr_in *)&ifa->address.sa;
2163178251Srrs				if (sin->sin_addr.s_addr == 0) {
2164178251Srrs					/* we skip unspecifed addresses */
2165167598Srrs					continue;
2166178251Srrs				}
2167267799Stuexen				if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2168267799Stuexen				    &sin->sin_addr) != 0) {
2169267799Stuexen					continue;
2170267799Stuexen				}
2171252961Stuexen				if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2172178251Srrs				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2173201758Smbr					continue;
2174178251Srrs				}
2175178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2176178251Srrs				    SCTP_IPV6_V6ONLY(inp6)) {
2177178251Srrs					cnt_invalid++;
2178178251Srrs					if (asc->cnt == cnt_invalid)
2179178251Srrs						return;
2180178251Srrs					else
2181178251Srrs						continue;
2182178251Srrs				}
2183178251Srrs				break;
2184167598Srrs			}
2185221249Stuexen#endif
2186178251Srrs		default:
2187167598Srrs			/* invalid address family */
2188167598Srrs			cnt_invalid++;
2189167598Srrs			if (asc->cnt == cnt_invalid)
2190163953Srrs				return;
2191167598Srrs			else
2192167598Srrs				continue;
2193178251Srrs			break;
2194163953Srrs		}
2195163953Srrs
2196167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
2197171572Srrs			/* prevent this address from being used as a source */
2198171572Srrs			sctp_add_local_addr_restricted(stcb, ifa);
2199167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
2200163953Srrs			struct sctp_nets *net;
2201163953Srrs
2202163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2203169352Srrs				sctp_rtentry_t *rt;
2204163953Srrs
2205163953Srrs				/* delete this address if cached */
2206172091Srrs				if (net->ro._s_addr == ifa) {
2207167598Srrs					sctp_free_ifa(net->ro._s_addr);
2208167598Srrs					net->ro._s_addr = NULL;
2209167598Srrs					net->src_addr_selected = 0;
2210167598Srrs					rt = net->ro.ro_rt;
2211167598Srrs					if (rt) {
2212167598Srrs						RTFREE(rt);
2213167598Srrs						net->ro.ro_rt = NULL;
2214167598Srrs					}
2215167598Srrs					/*
2216167598Srrs					 * Now we deleted our src address,
2217167598Srrs					 * should we not also now reset the
2218167598Srrs					 * cwnd/rto to start as if its a new
2219167598Srrs					 * address?
2220167598Srrs					 */
2221171440Srrs					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2222170428Srrs					net->RTO = 0;
2223167598Srrs
2224163953Srrs				}
2225167598Srrs			}
2226167598Srrs		} else if (type == SCTP_SET_PRIM_ADDR) {
2227167598Srrs			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2228171572Srrs				/* must validate the ifa is in the ep */
2229167598Srrs				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
2230167598Srrs					continue;
2231163953Srrs				}
2232167598Srrs			} else {
2233167598Srrs				/* Need to check scopes for this guy */
2234252961Stuexen				if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2235167598Srrs					continue;
2236167598Srrs				}
2237163953Srrs			}
2238163953Srrs		}
2239167598Srrs		/* queue an asconf for this address add/delete */
2240171572Srrs		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
2241171572Srrs		    stcb->asoc.peer_supports_asconf) {
2242171572Srrs			/* queue an asconf for this addr */
2243171572Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
2244171572Srrs			/*
2245171572Srrs			 * if queued ok, and in the open state, update the
2246171572Srrs			 * count of queued params.  If in the non-open
2247171572Srrs			 * state, these get sent when the assoc goes open.
2248171572Srrs			 */
2249171572Srrs			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2250171572Srrs				if (status >= 0) {
2251171572Srrs					num_queued++;
2252167598Srrs				}
2253167598Srrs			}
2254167598Srrs		}
2255163953Srrs	}
2256171572Srrs	/*
2257171572Srrs	 * If we have queued params in the open state, send out an ASCONF.
2258171572Srrs	 */
2259171572Srrs	if (num_queued > 0) {
2260224641Stuexen		sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2261171572Srrs	}
2262163953Srrs}
2263163953Srrs
2264171572Srrsvoid
2265231038Stuexensctp_asconf_iterator_end(void *ptr, uint32_t val SCTP_UNUSED)
2266167598Srrs{
2267167598Srrs	struct sctp_asconf_iterator *asc;
2268167598Srrs	struct sctp_ifa *ifa;
2269216822Stuexen	struct sctp_laddr *l, *nl;
2270167598Srrs
2271167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2272216822Stuexen	LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
2273167598Srrs		ifa = l->ifa;
2274167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
2275167598Srrs			/* Clear the defer use flag */
2276167598Srrs			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2277167598Srrs		}
2278167598Srrs		sctp_free_ifa(ifa);
2279179783Srrs		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
2280167598Srrs		SCTP_DECR_LADDR_COUNT();
2281167598Srrs	}
2282170091Srrs	SCTP_FREE(asc, SCTP_M_ASC_IT);
2283167598Srrs}
2284167598Srrs
2285163953Srrs/*
2286171572Srrs * sa is the sockaddr to ask the peer to set primary to.
2287171572Srrs * returns: 0 = completed, -1 = error
2288163953Srrs */
2289171943Srrsint32_t
2290163953Srrssctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2291163953Srrs{
2292172190Srrs	uint32_t vrf_id;
2293172190Srrs	struct sctp_ifa *ifa;
2294163953Srrs
2295172190Srrs	/* find the ifa for the desired set primary */
2296172218Srrs	vrf_id = stcb->asoc.vrf_id;
2297172190Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
2298172190Srrs	if (ifa == NULL) {
2299172190Srrs		/* Invalid address */
2300172190Srrs		return (-1);
2301172190Srrs	}
2302163953Srrs	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2303172190Srrs	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2304163953Srrs		/* set primary queuing succeeded */
2305171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2306171572Srrs		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2307252927Stuexen		    (void *)stcb);
2308171572Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2309163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2310171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2311163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2312163953Srrs			    stcb->sctp_ep, stcb,
2313163953Srrs			    stcb->asoc.primary_destination);
2314171572Srrs#else
2315224641Stuexen			sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2316171572Srrs#endif
2317163953Srrs		}
2318163953Srrs	} else {
2319169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2320252927Stuexen		    (void *)stcb);
2321169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2322163953Srrs		return (-1);
2323163953Srrs	}
2324163953Srrs	return (0);
2325163953Srrs}
2326163953Srrs
2327163953Srrsvoid
2328167598Srrssctp_set_primary_ip_address(struct sctp_ifa *ifa)
2329163953Srrs{
2330163953Srrs	struct sctp_inpcb *inp;
2331163953Srrs
2332163953Srrs	/* go through all our PCB's */
2333179783Srrs	LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
2334163953Srrs		struct sctp_tcb *stcb;
2335163953Srrs
2336163953Srrs		/* process for all associations for this endpoint */
2337163953Srrs		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2338163953Srrs			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2339163953Srrs			if (!sctp_asconf_queue_add(stcb, ifa,
2340163953Srrs			    SCTP_SET_PRIM_ADDR)) {
2341163953Srrs				/* set primary queuing succeeded */
2342172090Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
2343252927Stuexen				    (void *)stcb);
2344171572Srrs				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
2345171572Srrs				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2346171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2347163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2348163953Srrs					    stcb->sctp_ep, stcb,
2349163953Srrs					    stcb->asoc.primary_destination);
2350171572Srrs#else
2351224641Stuexen					sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2352171572Srrs#endif
2353163953Srrs				}
2354163953Srrs			}
2355163953Srrs		}		/* for each stcb */
2356163953Srrs	}			/* for each inp */
2357163953Srrs}
2358163953Srrs
2359179157Srrsint
2360179157Srrssctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2361179157Srrs{
2362179157Srrs	struct sctp_tmit_chunk *chk, *nchk;
2363179157Srrs	unsigned int offset, asconf_limit;
2364179157Srrs	struct sctp_asconf_chunk *acp;
2365179157Srrs	struct sctp_asconf_paramhdr *aph;
2366179157Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2367221249Stuexen	struct sctp_paramhdr *ph;
2368179157Srrs	int add_cnt, del_cnt;
2369179157Srrs	uint16_t last_param_type;
2370179157Srrs
2371179157Srrs	add_cnt = del_cnt = 0;
2372179157Srrs	last_param_type = 0;
2373216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
2374179157Srrs		if (chk->data == NULL) {
2375179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2376179157Srrs			continue;
2377179157Srrs		}
2378179157Srrs		offset = 0;
2379179157Srrs		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2380179157Srrs		offset += sizeof(struct sctp_asconf_chunk);
2381179157Srrs		asconf_limit = ntohs(acp->ch.chunk_length);
2382221249Stuexen		ph = (struct sctp_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2383221249Stuexen		if (ph == NULL) {
2384179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2385179157Srrs			continue;
2386179157Srrs		}
2387221249Stuexen		offset += ntohs(ph->param_length);
2388179157Srrs
2389179157Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2390179157Srrs		if (aph == NULL) {
2391179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2392179157Srrs			continue;
2393179157Srrs		}
2394179157Srrs		while (aph != NULL) {
2395179157Srrs			unsigned int param_length, param_type;
2396179157Srrs
2397179157Srrs			param_type = ntohs(aph->ph.param_type);
2398179157Srrs			param_length = ntohs(aph->ph.param_length);
2399179157Srrs			if (offset + param_length > asconf_limit) {
2400179157Srrs				/* parameter goes beyond end of chunk! */
2401179157Srrs				break;
2402179157Srrs			}
2403179157Srrs			if (param_length > sizeof(aparam_buf)) {
2404179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2405179157Srrs				break;
2406179157Srrs			}
2407179157Srrs			if (param_length <= sizeof(struct sctp_paramhdr)) {
2408179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2409179157Srrs				break;
2410179157Srrs			}
2411179157Srrs			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2412179157Srrs			if (aph == NULL) {
2413179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2414179157Srrs				break;
2415179157Srrs			}
2416221249Stuexen			ph = (struct sctp_paramhdr *)(aph + 1);
2417221249Stuexen			if (sctp_addr_match(ph, &sctp_ifa->address.sa) != 0) {
2418179157Srrs				switch (param_type) {
2419179157Srrs				case SCTP_ADD_IP_ADDRESS:
2420179157Srrs					add_cnt++;
2421179157Srrs					break;
2422179157Srrs				case SCTP_DEL_IP_ADDRESS:
2423179157Srrs					del_cnt++;
2424179157Srrs					break;
2425179157Srrs				default:
2426179157Srrs					break;
2427179157Srrs				}
2428179157Srrs				last_param_type = param_type;
2429179157Srrs			}
2430179157Srrs			offset += SCTP_SIZE32(param_length);
2431179157Srrs			if (offset >= asconf_limit) {
2432179157Srrs				/* no more data in the mbuf chain */
2433179157Srrs				break;
2434179157Srrs			}
2435179157Srrs			/* get pointer to next asconf param */
2436179157Srrs			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2437179157Srrs		}
2438179157Srrs	}
2439179157Srrs
2440179157Srrs	/*
2441179157Srrs	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2442179157Srrs	 * or DEL -> ADD
2443179157Srrs	 */
2444179157Srrs	if (add_cnt > del_cnt ||
2445179157Srrs	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2446231039Stuexen		return (1);
2447179157Srrs	}
2448231039Stuexen	return (0);
2449179157Srrs}
2450179157Srrs
2451163953Srrsstatic struct sockaddr *
2452172190Srrssctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2453163953Srrs{
2454167598Srrs	struct sctp_vrf *vrf = NULL;
2455167598Srrs	struct sctp_ifn *sctp_ifn;
2456167598Srrs	struct sctp_ifa *sctp_ifa;
2457163953Srrs
2458172190Srrs	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2459172218Srrs		SCTP_IPI_ADDR_RLOCK();
2460167598Srrs	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2461169420Srrs	if (vrf == NULL) {
2462172190Srrs		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2463172218Srrs			SCTP_IPI_ADDR_RUNLOCK();
2464169420Srrs		return (NULL);
2465169420Srrs	}
2466167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2467252961Stuexen		if (stcb->asoc.scope.loopback_scope == 0 &&
2468167598Srrs		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2469163953Srrs			/* Skip if loopback_scope not set */
2470163953Srrs			continue;
2471163953Srrs		}
2472167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2473221249Stuexen			switch (sctp_ifa->address.sa.sa_family) {
2474221249Stuexen#ifdef INET
2475221249Stuexen			case AF_INET:
2476252961Stuexen				if (stcb->asoc.scope.ipv4_addr_legal) {
2477221249Stuexen					struct sockaddr_in *sin;
2478163953Srrs
2479221249Stuexen					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
2480221249Stuexen					if (sin->sin_addr.s_addr == 0) {
2481221249Stuexen						/* skip unspecifed addresses */
2482221249Stuexen						continue;
2483221249Stuexen					}
2484267799Stuexen					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
2485267799Stuexen					    &sin->sin_addr) != 0) {
2486267799Stuexen						continue;
2487267799Stuexen					}
2488252961Stuexen					if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2489221249Stuexen					    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2490221249Stuexen						continue;
2491221249Stuexen
2492221249Stuexen					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2493221249Stuexen					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2494221249Stuexen						continue;
2495221249Stuexen					/*
2496221249Stuexen					 * found a valid local v4 address to
2497221249Stuexen					 * use
2498221249Stuexen					 */
2499221249Stuexen					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2500221249Stuexen						SCTP_IPI_ADDR_RUNLOCK();
2501221249Stuexen					return (&sctp_ifa->address.sa);
2502163953Srrs				}
2503221249Stuexen				break;
2504221249Stuexen#endif
2505221249Stuexen#ifdef INET6
2506221249Stuexen			case AF_INET6:
2507252961Stuexen				if (stcb->asoc.scope.ipv6_addr_legal) {
2508221249Stuexen					struct sockaddr_in6 *sin6;
2509163953Srrs
2510221249Stuexen					if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2511221249Stuexen						continue;
2512221249Stuexen					}
2513221249Stuexen					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
2514221249Stuexen					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2515221249Stuexen						/*
2516221249Stuexen						 * we skip unspecifed
2517221249Stuexen						 * addresses
2518221249Stuexen						 */
2519221249Stuexen						continue;
2520221249Stuexen					}
2521267799Stuexen					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
2522267799Stuexen					    &sin6->sin6_addr) != 0) {
2523267799Stuexen						continue;
2524267799Stuexen					}
2525252961Stuexen					if (stcb->asoc.scope.local_scope == 0 &&
2526221249Stuexen					    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2527221249Stuexen						continue;
2528252961Stuexen					if (stcb->asoc.scope.site_scope == 0 &&
2529221249Stuexen					    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2530221249Stuexen						continue;
2531163953Srrs
2532221249Stuexen					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2533221249Stuexen					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2534221249Stuexen						continue;
2535221249Stuexen					/*
2536221249Stuexen					 * found a valid local v6 address to
2537221249Stuexen					 * use
2538221249Stuexen					 */
2539221249Stuexen					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2540221249Stuexen						SCTP_IPI_ADDR_RUNLOCK();
2541221249Stuexen					return (&sctp_ifa->address.sa);
2542167598Srrs				}
2543221249Stuexen				break;
2544221249Stuexen#endif
2545221249Stuexen			default:
2546221249Stuexen				break;
2547163953Srrs			}
2548163953Srrs		}
2549163953Srrs	}
2550163953Srrs	/* no valid addresses found */
2551172190Srrs	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2552172218Srrs		SCTP_IPI_ADDR_RUNLOCK();
2553163953Srrs	return (NULL);
2554163953Srrs}
2555163953Srrs
2556163953Srrsstatic struct sockaddr *
2557163953Srrssctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2558163953Srrs{
2559163953Srrs	struct sctp_laddr *laddr;
2560163953Srrs
2561163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2562163953Srrs		if (laddr->ifa == NULL) {
2563163953Srrs			continue;
2564163953Srrs		}
2565163953Srrs		/* is the address restricted ? */
2566179157Srrs		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2567179157Srrs		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2568163953Srrs			continue;
2569163953Srrs
2570163953Srrs		/* found a valid local address to use */
2571167598Srrs		return (&laddr->ifa->address.sa);
2572163953Srrs	}
2573163953Srrs	/* no valid addresses found */
2574163953Srrs	return (NULL);
2575163953Srrs}
2576163953Srrs
2577163953Srrs/*
2578171477Srrs * builds an ASCONF chunk from queued ASCONF params.
2579171477Srrs * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2580163953Srrs */
2581163953Srrsstruct mbuf *
2582172190Srrssctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2583163953Srrs{
2584163953Srrs	struct mbuf *m_asconf, *m_asconf_chk;
2585163953Srrs	struct sctp_asconf_addr *aa;
2586163953Srrs	struct sctp_asconf_chunk *acp;
2587163953Srrs	struct sctp_asconf_paramhdr *aph;
2588163953Srrs	struct sctp_asconf_addr_param *aap;
2589163953Srrs	uint32_t p_length;
2590163953Srrs	uint32_t correlation_id = 1;	/* 0 is reserved... */
2591163953Srrs	caddr_t ptr, lookup_ptr;
2592163953Srrs	uint8_t lookup_used = 0;
2593163953Srrs
2594163953Srrs	/* are there any asconf params to send? */
2595179157Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2596179157Srrs		if (aa->sent == 0)
2597179157Srrs			break;
2598163953Srrs	}
2599179157Srrs	if (aa == NULL)
2600171572Srrs		return (NULL);
2601179157Srrs
2602163953Srrs	/*
2603163953Srrs	 * get a chunk header mbuf and a cluster for the asconf params since
2604163953Srrs	 * it's simpler to fill in the asconf chunk header lookup address on
2605163953Srrs	 * the fly
2606163953Srrs	 */
2607165647Srrs	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2608163953Srrs	if (m_asconf_chk == NULL) {
2609163953Srrs		/* no mbuf's */
2610169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2611169420Srrs		    "compose_asconf: couldn't get chunk mbuf!\n");
2612163953Srrs		return (NULL);
2613163953Srrs	}
2614165647Srrs	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2615163953Srrs	if (m_asconf == NULL) {
2616163953Srrs		/* no mbuf's */
2617169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2618169420Srrs		    "compose_asconf: couldn't get mbuf!\n");
2619163953Srrs		sctp_m_freem(m_asconf_chk);
2620163953Srrs		return (NULL);
2621163953Srrs	}
2622165647Srrs	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2623165647Srrs	SCTP_BUF_LEN(m_asconf) = 0;
2624163953Srrs	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2625163953Srrs	bzero(acp, sizeof(struct sctp_asconf_chunk));
2626163953Srrs	/* save pointers to lookup address and asconf params */
2627163953Srrs	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2628163953Srrs	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2629163953Srrs
2630163953Srrs	/* fill in chunk header info */
2631163953Srrs	acp->ch.chunk_type = SCTP_ASCONF;
2632163953Srrs	acp->ch.chunk_flags = 0;
2633163953Srrs	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2634179157Srrs	stcb->asoc.asconf_seq_out++;
2635163953Srrs
2636163953Srrs	/* add parameters... up to smallest MTU allowed */
2637163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2638179157Srrs		if (aa->sent)
2639179157Srrs			continue;
2640163953Srrs		/* get the parameter length */
2641163953Srrs		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2642163953Srrs		/* will it fit in current chunk? */
2643266057Stuexen		if ((SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) ||
2644266057Stuexen		    (SCTP_BUF_LEN(m_asconf) + p_length > MCLBYTES)) {
2645163953Srrs			/* won't fit, so we're done with this chunk */
2646163953Srrs			break;
2647163953Srrs		}
2648163953Srrs		/* assign (and store) a correlation id */
2649163953Srrs		aa->ap.aph.correlation_id = correlation_id++;
2650163953Srrs
2651163953Srrs		/*
2652163953Srrs		 * fill in address if we're doing a delete this is a simple
2653163953Srrs		 * way for us to fill in the correlation address, which
2654163953Srrs		 * should only be used by the peer if we're deleting our
2655163953Srrs		 * source address and adding a new address (e.g. renumbering
2656163953Srrs		 * case)
2657163953Srrs		 */
2658163953Srrs		if (lookup_used == 0 &&
2659185694Srrs		    (aa->special_del == 0) &&
2660163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2661163953Srrs			struct sctp_ipv6addr_param *lookup;
2662163953Srrs			uint16_t p_size, addr_size;
2663163953Srrs
2664163953Srrs			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2665163953Srrs			lookup->ph.param_type =
2666163953Srrs			    htons(aa->ap.addrp.ph.param_type);
2667163953Srrs			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2668163953Srrs				/* copy IPv6 address */
2669163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2670163953Srrs				addr_size = sizeof(struct in6_addr);
2671163953Srrs			} else {
2672163953Srrs				/* copy IPv4 address */
2673163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2674163953Srrs				addr_size = sizeof(struct in_addr);
2675163953Srrs			}
2676163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2677163953Srrs			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2678165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2679163953Srrs			lookup_used = 1;
2680163953Srrs		}
2681163953Srrs		/* copy into current space */
2682163953Srrs		memcpy(ptr, &aa->ap, p_length);
2683163953Srrs
2684163953Srrs		/* network elements and update lengths */
2685163953Srrs		aph = (struct sctp_asconf_paramhdr *)ptr;
2686163953Srrs		aap = (struct sctp_asconf_addr_param *)ptr;
2687163953Srrs		/* correlation_id is transparent to peer, no htonl needed */
2688163953Srrs		aph->ph.param_type = htons(aph->ph.param_type);
2689163953Srrs		aph->ph.param_length = htons(aph->ph.param_length);
2690163953Srrs		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2691163953Srrs		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2692163953Srrs
2693165647Srrs		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2694163953Srrs		ptr += SCTP_SIZE32(p_length);
2695163953Srrs
2696163953Srrs		/*
2697163953Srrs		 * these params are removed off the pending list upon
2698163953Srrs		 * getting an ASCONF-ACK back from the peer, just set flag
2699163953Srrs		 */
2700163953Srrs		aa->sent = 1;
2701163953Srrs	}
2702163953Srrs	/* check to see if the lookup addr has been populated yet */
2703163953Srrs	if (lookup_used == 0) {
2704163953Srrs		/* NOTE: if the address param is optional, can skip this... */
2705163953Srrs		/* add any valid (existing) address... */
2706163953Srrs		struct sctp_ipv6addr_param *lookup;
2707163953Srrs		uint16_t p_size, addr_size;
2708163953Srrs		struct sockaddr *found_addr;
2709163953Srrs		caddr_t addr_ptr;
2710163953Srrs
2711163953Srrs		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2712172190Srrs			found_addr = sctp_find_valid_localaddr(stcb,
2713172190Srrs			    addr_locked);
2714163953Srrs		else
2715163953Srrs			found_addr = sctp_find_valid_localaddr_ep(stcb);
2716163953Srrs
2717163953Srrs		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2718163953Srrs		if (found_addr != NULL) {
2719221249Stuexen			switch (found_addr->sa_family) {
2720221249Stuexen#ifdef INET6
2721221249Stuexen			case AF_INET6:
2722163953Srrs				/* copy IPv6 address */
2723163953Srrs				lookup->ph.param_type =
2724163953Srrs				    htons(SCTP_IPV6_ADDRESS);
2725163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2726163953Srrs				addr_size = sizeof(struct in6_addr);
2727163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2728163953Srrs				    found_addr)->sin6_addr;
2729221249Stuexen				break;
2730221249Stuexen#endif
2731221249Stuexen#ifdef INET
2732221249Stuexen			case AF_INET:
2733163953Srrs				/* copy IPv4 address */
2734163953Srrs				lookup->ph.param_type =
2735163953Srrs				    htons(SCTP_IPV4_ADDRESS);
2736163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2737163953Srrs				addr_size = sizeof(struct in_addr);
2738163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2739163953Srrs				    found_addr)->sin_addr;
2740221249Stuexen				break;
2741221249Stuexen#endif
2742221249Stuexen			default:
2743221249Stuexen				p_size = 0;
2744221249Stuexen				addr_size = 0;
2745221249Stuexen				addr_ptr = NULL;
2746221249Stuexen				break;
2747163953Srrs			}
2748163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2749163953Srrs			memcpy(lookup->addr, addr_ptr, addr_size);
2750165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2751163953Srrs		} else {
2752163953Srrs			/* uh oh... don't have any address?? */
2753169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
2754169420Srrs			    "compose_asconf: no lookup addr!\n");
2755221249Stuexen			/* XXX for now, we send a IPv4 address of 0.0.0.0 */
2756163953Srrs			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2757163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2758163953Srrs			bzero(lookup->addr, sizeof(struct in_addr));
2759165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2760163953Srrs		}
2761163953Srrs	}
2762163953Srrs	/* chain it all together */
2763165647Srrs	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2764169655Srrs	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2765266056Stuexen	acp->ch.chunk_length = htons(*retlen);
2766163953Srrs
2767163953Srrs	return (m_asconf_chk);
2768163953Srrs}
2769163953Srrs
2770163953Srrs/*
2771163953Srrs * section to handle address changes before an association is up eg. changes
2772163953Srrs * during INIT/INIT-ACK/COOKIE-ECHO handshake
2773163953Srrs */
2774163953Srrs
2775163953Srrs/*
2776163953Srrs * processes the (local) addresses in the INIT-ACK chunk
2777163953Srrs */
2778163953Srrsstatic void
2779163953Srrssctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2780163953Srrs    unsigned int offset, unsigned int length)
2781163953Srrs{
2782163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2783163953Srrs	uint16_t plen, ptype;
2784167598Srrs	struct sctp_ifa *sctp_ifa;
2785221249Stuexen
2786221249Stuexen#ifdef INET6
2787238613Stuexen	struct sctp_ipv6addr_param addr6_store;
2788163953Srrs	struct sockaddr_in6 sin6;
2789221249Stuexen
2790221249Stuexen#endif
2791221249Stuexen#ifdef INET
2792238613Stuexen	struct sctp_ipv4addr_param addr4_store;
2793163953Srrs	struct sockaddr_in sin;
2794221249Stuexen
2795221249Stuexen#endif
2796163953Srrs	struct sockaddr *sa;
2797167598Srrs	uint32_t vrf_id;
2798163953Srrs
2799169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2800169420Srrs	if (stcb == NULL)	/* Un-needed check for SA */
2801169420Srrs		return;
2802163953Srrs
2803163953Srrs	/* convert to upper bound */
2804163953Srrs	length += offset;
2805163953Srrs
2806163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2807163953Srrs		return;
2808163953Srrs	}
2809163953Srrs	/* init the addresses */
2810221249Stuexen#ifdef INET6
2811163953Srrs	bzero(&sin6, sizeof(sin6));
2812163953Srrs	sin6.sin6_family = AF_INET6;
2813163953Srrs	sin6.sin6_len = sizeof(sin6);
2814163953Srrs	sin6.sin6_port = stcb->rport;
2815221249Stuexen#endif
2816163953Srrs
2817221249Stuexen#ifdef INET
2818163953Srrs	bzero(&sin, sizeof(sin));
2819221249Stuexen	sin.sin_family = AF_INET;
2820163953Srrs	sin.sin_len = sizeof(sin);
2821163953Srrs	sin.sin_port = stcb->rport;
2822221249Stuexen#endif
2823163953Srrs
2824163953Srrs	/* go through the addresses in the init-ack */
2825221249Stuexen	ph = (struct sctp_paramhdr *)
2826221249Stuexen	    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2827221249Stuexen	    (uint8_t *) & tmp_param);
2828163953Srrs	while (ph != NULL) {
2829163953Srrs		ptype = ntohs(ph->param_type);
2830163953Srrs		plen = ntohs(ph->param_length);
2831221249Stuexen		switch (ptype) {
2832221249Stuexen#ifdef INET6
2833221249Stuexen		case SCTP_IPV6_ADDRESS:
2834221249Stuexen			{
2835221249Stuexen				struct sctp_ipv6addr_param *a6p;
2836163953Srrs
2837221249Stuexen				/* get the entire IPv6 address param */
2838221249Stuexen				a6p = (struct sctp_ipv6addr_param *)
2839221249Stuexen				    sctp_m_getptr(m, offset,
2840221249Stuexen				    sizeof(struct sctp_ipv6addr_param),
2841238613Stuexen				    (uint8_t *) & addr6_store);
2842221249Stuexen				if (plen != sizeof(struct sctp_ipv6addr_param) ||
2843221249Stuexen				    a6p == NULL) {
2844221249Stuexen					return;
2845221249Stuexen				}
2846221249Stuexen				memcpy(&sin6.sin6_addr, a6p->addr,
2847221249Stuexen				    sizeof(struct in6_addr));
2848221249Stuexen				sa = (struct sockaddr *)&sin6;
2849221249Stuexen				break;
2850163953Srrs			}
2851221249Stuexen#endif
2852221249Stuexen#ifdef INET
2853221249Stuexen		case SCTP_IPV4_ADDRESS:
2854221249Stuexen			{
2855221249Stuexen				struct sctp_ipv4addr_param *a4p;
2856163953Srrs
2857221249Stuexen				/* get the entire IPv4 address param */
2858221249Stuexen				a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2859221249Stuexen				    sizeof(struct sctp_ipv4addr_param),
2860238613Stuexen				    (uint8_t *) & addr4_store);
2861221249Stuexen				if (plen != sizeof(struct sctp_ipv4addr_param) ||
2862221249Stuexen				    a4p == NULL) {
2863221249Stuexen					return;
2864221249Stuexen				}
2865221249Stuexen				sin.sin_addr.s_addr = a4p->addr;
2866221249Stuexen				sa = (struct sockaddr *)&sin;
2867221410Stuexen				break;
2868163953Srrs			}
2869221249Stuexen#endif
2870221249Stuexen		default:
2871163953Srrs			goto next_addr;
2872163953Srrs		}
2873163953Srrs
2874163953Srrs		/* see if this address really (still) exists */
2875168299Srrs		if (stcb) {
2876168299Srrs			vrf_id = stcb->asoc.vrf_id;
2877168299Srrs		} else {
2878168299Srrs			vrf_id = SCTP_DEFAULT_VRFID;
2879168299Srrs		}
2880172190Srrs		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id,
2881172190Srrs		    SCTP_ADDR_NOT_LOCKED);
2882167598Srrs		if (sctp_ifa == NULL) {
2883163953Srrs			/* address doesn't exist anymore */
2884163953Srrs			int status;
2885163953Srrs
2886163953Srrs			/* are ASCONFs allowed ? */
2887163953Srrs			if ((sctp_is_feature_on(stcb->sctp_ep,
2888163953Srrs			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2889163953Srrs			    stcb->asoc.peer_supports_asconf) {
2890163953Srrs				/* queue an ASCONF DEL_IP_ADDRESS */
2891172190Srrs				status = sctp_asconf_queue_sa_delete(stcb, sa);
2892163953Srrs				/*
2893171572Srrs				 * if queued ok, and in correct state, send
2894171572Srrs				 * out the ASCONF.
2895163953Srrs				 */
2896163953Srrs				if (status == 0 &&
2897163953Srrs				    SCTP_GET_STATE(&stcb->asoc) ==
2898163953Srrs				    SCTP_STATE_OPEN) {
2899171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2900163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2901163953Srrs					    stcb->sctp_ep, stcb,
2902163953Srrs					    stcb->asoc.primary_destination);
2903171572Srrs#else
2904224641Stuexen					sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2905171572Srrs#endif
2906163953Srrs				}
2907163953Srrs			}
2908163953Srrs		}
2909163953Srrsnext_addr:
2910163953Srrs		/*
2911163953Srrs		 * Sanity check:  Make sure the length isn't 0, otherwise
2912163953Srrs		 * we'll be stuck in this loop for a long time...
2913163953Srrs		 */
2914163953Srrs		if (SCTP_SIZE32(plen) == 0) {
2915169420Srrs			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2916163953Srrs			    plen, ptype);
2917163953Srrs			return;
2918163953Srrs		}
2919163953Srrs		/* get next parameter */
2920163953Srrs		offset += SCTP_SIZE32(plen);
2921163953Srrs		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2922163953Srrs			return;
2923163953Srrs		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2924163953Srrs		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2925163953Srrs	}			/* while */
2926163953Srrs}
2927163953Srrs
2928163953Srrs/* FIX ME: need to verify return result for v6 address type if v6 disabled */
2929163953Srrs/*
2930163953Srrs * checks to see if a specific address is in the initack address list returns
2931163953Srrs * 1 if found, 0 if not
2932163953Srrs */
2933163953Srrsstatic uint32_t
2934231038Stuexensctp_addr_in_initack(struct mbuf *m, uint32_t offset, uint32_t length, struct sockaddr *sa)
2935163953Srrs{
2936163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2937163953Srrs	uint16_t plen, ptype;
2938221249Stuexen
2939221249Stuexen#ifdef INET
2940163953Srrs	struct sockaddr_in *sin;
2941163953Srrs	struct sctp_ipv4addr_param *a4p;
2942238613Stuexen	struct sctp_ipv6addr_param addr4_store;
2943163953Srrs
2944221249Stuexen#endif
2945163953Srrs#ifdef INET6
2946167598Srrs	struct sockaddr_in6 *sin6;
2947163953Srrs	struct sctp_ipv6addr_param *a6p;
2948238613Stuexen	struct sctp_ipv6addr_param addr6_store;
2949167598Srrs	struct sockaddr_in6 sin6_tmp;
2950163953Srrs
2951221249Stuexen#endif
2952163953Srrs
2953221249Stuexen	switch (sa->sa_family) {
2954221249Stuexen#ifdef INET
2955221249Stuexen	case AF_INET:
2956221249Stuexen		break;
2957221249Stuexen#endif
2958163953Srrs#ifdef INET6
2959221249Stuexen	case AF_INET6:
2960221249Stuexen		break;
2961221249Stuexen#endif
2962221249Stuexen	default:
2963163953Srrs		return (0);
2964221249Stuexen	}
2965163953Srrs
2966169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2967169420Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2968163953Srrs	/* convert to upper bound */
2969163953Srrs	length += offset;
2970163953Srrs
2971163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2972169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2973169420Srrs		    "find_initack_addr: invalid offset?\n");
2974163953Srrs		return (0);
2975163953Srrs	}
2976163953Srrs	/* go through the addresses in the init-ack */
2977163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2978163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2979163953Srrs	while (ph != NULL) {
2980163953Srrs		ptype = ntohs(ph->param_type);
2981163953Srrs		plen = ntohs(ph->param_length);
2982221249Stuexen		switch (ptype) {
2983163953Srrs#ifdef INET6
2984221249Stuexen		case SCTP_IPV6_ADDRESS:
2985221249Stuexen			if (sa->sa_family == AF_INET6) {
2986221249Stuexen				/* get the entire IPv6 address param */
2987221249Stuexen				if (plen != sizeof(struct sctp_ipv6addr_param)) {
2988221249Stuexen					break;
2989221249Stuexen				}
2990221249Stuexen				/* get the entire IPv6 address param */
2991221249Stuexen				a6p = (struct sctp_ipv6addr_param *)
2992221249Stuexen				    sctp_m_getptr(m, offset,
2993221249Stuexen				    sizeof(struct sctp_ipv6addr_param),
2994238613Stuexen				    (uint8_t *) & addr6_store);
2995221249Stuexen				if (a6p == NULL) {
2996221249Stuexen					return (0);
2997221249Stuexen				}
2998221249Stuexen				sin6 = (struct sockaddr_in6 *)sa;
2999221249Stuexen				if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
3000221249Stuexen					/* create a copy and clear scope */
3001221249Stuexen					memcpy(&sin6_tmp, sin6,
3002221249Stuexen					    sizeof(struct sockaddr_in6));
3003221249Stuexen					sin6 = &sin6_tmp;
3004221249Stuexen					in6_clearscope(&sin6->sin6_addr);
3005221249Stuexen				}
3006221249Stuexen				if (memcmp(&sin6->sin6_addr, a6p->addr,
3007221249Stuexen				    sizeof(struct in6_addr)) == 0) {
3008221249Stuexen					/* found it */
3009221249Stuexen					return (1);
3010221249Stuexen				}
3011163953Srrs			}
3012221249Stuexen			break;
3013163953Srrs#endif				/* INET6 */
3014221249Stuexen#ifdef INET
3015221249Stuexen		case SCTP_IPV4_ADDRESS:
3016221249Stuexen			if (sa->sa_family == AF_INET) {
3017221249Stuexen				if (plen != sizeof(struct sctp_ipv4addr_param)) {
3018221249Stuexen					break;
3019221249Stuexen				}
3020221249Stuexen				/* get the entire IPv4 address param */
3021221249Stuexen				a4p = (struct sctp_ipv4addr_param *)
3022221249Stuexen				    sctp_m_getptr(m, offset,
3023221249Stuexen				    sizeof(struct sctp_ipv4addr_param),
3024238613Stuexen				    (uint8_t *) & addr4_store);
3025221249Stuexen				if (a4p == NULL) {
3026221249Stuexen					return (0);
3027221249Stuexen				}
3028221249Stuexen				sin = (struct sockaddr_in *)sa;
3029221249Stuexen				if (sin->sin_addr.s_addr == a4p->addr) {
3030221249Stuexen					/* found it */
3031221249Stuexen					return (1);
3032221249Stuexen				}
3033163953Srrs			}
3034221249Stuexen			break;
3035221249Stuexen#endif
3036221249Stuexen		default:
3037221249Stuexen			break;
3038163953Srrs		}
3039163953Srrs		/* get next parameter */
3040163953Srrs		offset += SCTP_SIZE32(plen);
3041221249Stuexen		if (offset + sizeof(struct sctp_paramhdr) > length) {
3042163953Srrs			return (0);
3043221249Stuexen		}
3044163953Srrs		ph = (struct sctp_paramhdr *)
3045163953Srrs		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
3046163953Srrs		    (uint8_t *) & tmp_param);
3047163953Srrs	}			/* while */
3048163953Srrs	/* not found! */
3049163953Srrs	return (0);
3050163953Srrs}
3051163953Srrs
3052163953Srrs/*
3053163953Srrs * makes sure that the current endpoint local addr list is consistent with
3054163953Srrs * the new association (eg. subset bound, asconf allowed) adds addresses as
3055163953Srrs * necessary
3056163953Srrs */
3057163953Srrsstatic void
3058163953Srrssctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3059163953Srrs    int length, struct sockaddr *init_addr)
3060163953Srrs{
3061163953Srrs	struct sctp_laddr *laddr;
3062163953Srrs
3063163953Srrs	/* go through the endpoint list */
3064163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3065163953Srrs		/* be paranoid and validate the laddr */
3066163953Srrs		if (laddr->ifa == NULL) {
3067169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
3068169420Srrs			    "check_addr_list_ep: laddr->ifa is NULL");
3069163953Srrs			continue;
3070163953Srrs		}
3071167598Srrs		if (laddr->ifa == NULL) {
3072169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
3073163953Srrs			continue;
3074163953Srrs		}
3075163953Srrs		/* do i have it implicitly? */
3076167598Srrs		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3077163953Srrs			continue;
3078163953Srrs		}
3079163953Srrs		/* check to see if in the init-ack */
3080231038Stuexen		if (!sctp_addr_in_initack(m, offset, length, &laddr->ifa->address.sa)) {
3081163953Srrs			/* try to add it */
3082163953Srrs			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
3083172190Srrs			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3084163953Srrs		}
3085163953Srrs	}
3086163953Srrs}
3087163953Srrs
3088163953Srrs/*
3089163953Srrs * makes sure that the current kernel address list is consistent with the new
3090163953Srrs * association (with all addrs bound) adds addresses as necessary
3091163953Srrs */
3092163953Srrsstatic void
3093163953Srrssctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3094163953Srrs    int length, struct sockaddr *init_addr,
3095163953Srrs    uint16_t local_scope, uint16_t site_scope,
3096163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
3097163953Srrs{
3098167598Srrs	struct sctp_vrf *vrf = NULL;
3099167598Srrs	struct sctp_ifn *sctp_ifn;
3100167598Srrs	struct sctp_ifa *sctp_ifa;
3101167598Srrs	uint32_t vrf_id;
3102163953Srrs
3103231038Stuexen#ifdef INET
3104231038Stuexen	struct sockaddr_in *sin;
3105231038Stuexen
3106231038Stuexen#endif
3107231038Stuexen#ifdef INET6
3108231038Stuexen	struct sockaddr_in6 *sin6;
3109231038Stuexen
3110231038Stuexen#endif
3111231038Stuexen
3112168299Srrs	if (stcb) {
3113168299Srrs		vrf_id = stcb->asoc.vrf_id;
3114168299Srrs	} else {
3115169420Srrs		return;
3116168299Srrs	}
3117172218Srrs	SCTP_IPI_ADDR_RLOCK();
3118167598Srrs	vrf = sctp_find_vrf(vrf_id);
3119167598Srrs	if (vrf == NULL) {
3120172218Srrs		SCTP_IPI_ADDR_RUNLOCK();
3121167598Srrs		return;
3122167598Srrs	}
3123163953Srrs	/* go through all our known interfaces */
3124167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3125167598Srrs		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3126163953Srrs			/* skip loopback interface */
3127163953Srrs			continue;
3128163953Srrs		}
3129163953Srrs		/* go through each interface address */
3130167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3131163953Srrs			/* do i have it implicitly? */
3132167598Srrs			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3133163953Srrs				continue;
3134163953Srrs			}
3135231038Stuexen			switch (sctp_ifa->address.sa.sa_family) {
3136231038Stuexen#ifdef INET
3137231038Stuexen			case AF_INET:
3138231038Stuexen				sin = (struct sockaddr_in *)&sctp_ifa->address.sin;
3139267799Stuexen				if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3140267799Stuexen				    &sin->sin_addr) != 0) {
3141267799Stuexen					continue;
3142267799Stuexen				}
3143231038Stuexen				if ((ipv4_scope == 0) &&
3144231038Stuexen				    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3145231038Stuexen					/* private address not in scope */
3146231038Stuexen					continue;
3147231038Stuexen				}
3148231038Stuexen				break;
3149231038Stuexen#endif
3150231038Stuexen#ifdef INET6
3151231038Stuexen			case AF_INET6:
3152231038Stuexen				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sin6;
3153267799Stuexen				if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3154267799Stuexen				    &sin6->sin6_addr) != 0) {
3155267799Stuexen					continue;
3156267799Stuexen				}
3157231038Stuexen				if ((local_scope == 0) &&
3158231038Stuexen				    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3159231038Stuexen					continue;
3160231038Stuexen				}
3161231038Stuexen				if ((site_scope == 0) &&
3162231038Stuexen				    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3163231038Stuexen					continue;
3164231038Stuexen				}
3165231038Stuexen				break;
3166231038Stuexen#endif
3167231038Stuexen			default:
3168231038Stuexen				break;
3169231038Stuexen			}
3170163953Srrs			/* check to see if in the init-ack */
3171231038Stuexen			if (!sctp_addr_in_initack(m, offset, length, &sctp_ifa->address.sa)) {
3172163953Srrs				/* try to add it */
3173163953Srrs				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
3174172190Srrs				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
3175172190Srrs				    SCTP_ADDR_LOCKED);
3176163953Srrs			}
3177163953Srrs		}		/* end foreach ifa */
3178163953Srrs	}			/* end foreach ifn */
3179172218Srrs	SCTP_IPI_ADDR_RUNLOCK();
3180163953Srrs}
3181163953Srrs
3182163953Srrs/*
3183163953Srrs * validates an init-ack chunk (from a cookie-echo) with current addresses
3184163953Srrs * adds addresses from the init-ack into our local address list, if needed
3185163953Srrs * queues asconf adds/deletes addresses as needed and makes appropriate list
3186163953Srrs * changes for source address selection m, offset: points to the start of the
3187163953Srrs * address list in an init-ack chunk length: total length of the address
3188163953Srrs * params only init_addr: address where my INIT-ACK was sent from
3189163953Srrs */
3190163953Srrsvoid
3191163953Srrssctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3192163953Srrs    int length, struct sockaddr *init_addr,
3193163953Srrs    uint16_t local_scope, uint16_t site_scope,
3194163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
3195163953Srrs{
3196163953Srrs	/* process the local addresses in the initack */
3197163953Srrs	sctp_process_initack_addresses(stcb, m, offset, length);
3198163953Srrs
3199163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3200163953Srrs		/* bound all case */
3201163953Srrs		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3202163953Srrs		    local_scope, site_scope, ipv4_scope, loopback_scope);
3203163953Srrs	} else {
3204163953Srrs		/* subset bound case */
3205163953Srrs		if (sctp_is_feature_on(stcb->sctp_ep,
3206163953Srrs		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3207163953Srrs			/* asconf's allowed */
3208163953Srrs			sctp_check_address_list_ep(stcb, m, offset, length,
3209163953Srrs			    init_addr);
3210163953Srrs		}
3211163953Srrs		/* else, no asconfs allowed, so what we sent is what we get */
3212163953Srrs	}
3213163953Srrs}
3214163953Srrs
3215163953Srrs/*
3216163953Srrs * sctp_bindx() support
3217163953Srrs */
3218163953Srrsuint32_t
3219169655Srrssctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
3220170744Srrs    uint32_t type, uint32_t vrf_id, struct sctp_ifa *sctp_ifap)
3221163953Srrs{
3222167598Srrs	struct sctp_ifa *ifa;
3223218521Stuexen	struct sctp_laddr *laddr, *nladdr;
3224163953Srrs
3225167598Srrs	if (sa->sa_len == 0) {
3226171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3227163953Srrs		return (EINVAL);
3228167598Srrs	}
3229170744Srrs	if (sctp_ifap) {
3230170744Srrs		ifa = sctp_ifap;
3231170744Srrs	} else if (type == SCTP_ADD_IP_ADDRESS) {
3232167598Srrs		/* For an add the address MUST be on the system */
3233172091Srrs		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
3234167598Srrs	} else if (type == SCTP_DEL_IP_ADDRESS) {
3235167598Srrs		/* For a delete we need to find it in the inp */
3236172091Srrs		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
3237167598Srrs	} else {
3238167598Srrs		ifa = NULL;
3239167598Srrs	}
3240163953Srrs	if (ifa != NULL) {
3241167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
3242167598Srrs			sctp_add_local_addr_ep(inp, ifa, type);
3243167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
3244169655Srrs			if (inp->laddr_count < 2) {
3245169655Srrs				/* can't delete the last local address */
3246171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3247169655Srrs				return (EINVAL);
3248169655Srrs			}
3249169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
3250169655Srrs			    sctp_nxt_addr) {
3251167598Srrs				if (ifa == laddr->ifa) {
3252167598Srrs					/* Mark in the delete */
3253167598Srrs					laddr->action = type;
3254167598Srrs				}
3255163953Srrs			}
3256163953Srrs		}
3257218521Stuexen		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3258199372Stuexen			/*
3259199372Stuexen			 * There is no need to start the iterator if the inp
3260199372Stuexen			 * has no associations.
3261199372Stuexen			 */
3262218521Stuexen			if (type == SCTP_DEL_IP_ADDRESS) {
3263218521Stuexen				LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3264218521Stuexen					if (laddr->ifa == ifa) {
3265218521Stuexen						sctp_del_local_addr_ep(inp, ifa);
3266218521Stuexen					}
3267218521Stuexen				}
3268218521Stuexen			}
3269218521Stuexen		} else {
3270199372Stuexen			struct sctp_asconf_iterator *asc;
3271199372Stuexen			struct sctp_laddr *wi;
3272199372Stuexen
3273199372Stuexen			SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
3274199372Stuexen			    sizeof(struct sctp_asconf_iterator),
3275199372Stuexen			    SCTP_M_ASC_IT);
3276199372Stuexen			if (asc == NULL) {
3277199372Stuexen				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3278199372Stuexen				return (ENOMEM);
3279199372Stuexen			}
3280199372Stuexen			wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3281199372Stuexen			if (wi == NULL) {
3282199372Stuexen				SCTP_FREE(asc, SCTP_M_ASC_IT);
3283199372Stuexen				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3284199372Stuexen				return (ENOMEM);
3285199372Stuexen			}
3286199372Stuexen			LIST_INIT(&asc->list_of_work);
3287199372Stuexen			asc->cnt = 1;
3288199372Stuexen			SCTP_INCR_LADDR_COUNT();
3289199372Stuexen			wi->ifa = ifa;
3290199372Stuexen			wi->action = type;
3291199372Stuexen			atomic_add_int(&ifa->refcount, 1);
3292199372Stuexen			LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
3293199372Stuexen			(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
3294199372Stuexen			    sctp_asconf_iterator_stcb,
3295199372Stuexen			    sctp_asconf_iterator_ep_end,
3296199372Stuexen			    SCTP_PCB_ANY_FLAGS,
3297199372Stuexen			    SCTP_PCB_ANY_FEATURES,
3298199372Stuexen			    SCTP_ASOC_ANY_STATE,
3299199372Stuexen			    (void *)asc, 0,
3300199372Stuexen			    sctp_asconf_iterator_end, inp, 0);
3301199372Stuexen		}
3302199372Stuexen		return (0);
3303163953Srrs	} else {
3304163953Srrs		/* invalid address! */
3305171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3306163953Srrs		return (EADDRNOTAVAIL);
3307163953Srrs	}
3308163953Srrs}
3309185694Srrs
3310185694Srrsvoid
3311185694Srrssctp_asconf_send_nat_state_update(struct sctp_tcb *stcb,
3312185694Srrs    struct sctp_nets *net)
3313185694Srrs{
3314185694Srrs	struct sctp_asconf_addr *aa;
3315185694Srrs	struct sctp_ifa *sctp_ifap;
3316185694Srrs	struct sctp_asconf_tag_param *vtag;
3317221249Stuexen
3318221249Stuexen#ifdef INET
3319185694Srrs	struct sockaddr_in *to;
3320185694Srrs
3321221249Stuexen#endif
3322185694Srrs#ifdef INET6
3323185694Srrs	struct sockaddr_in6 *to6;
3324185694Srrs
3325185694Srrs#endif
3326185694Srrs	if (net == NULL) {
3327185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
3328185694Srrs		return;
3329185694Srrs	}
3330185694Srrs	if (stcb == NULL) {
3331185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
3332185694Srrs		return;
3333185694Srrs	}
3334185694Srrs	/*
3335185694Srrs	 * Need to have in the asconf: - vtagparam(my_vtag/peer_vtag) -
3336185694Srrs	 * add(0.0.0.0) - del(0.0.0.0) - Any global addresses add(addr)
3337185694Srrs	 */
3338185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3339185694Srrs	    SCTP_M_ASC_ADDR);
3340185694Srrs	if (aa == NULL) {
3341185694Srrs		/* didn't get memory */
3342185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3343185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3344185694Srrs		return;
3345185694Srrs	}
3346185694Srrs	aa->special_del = 0;
3347185694Srrs	/* fill in asconf address parameter fields */
3348185694Srrs	/* top level elements are "networked" during send */
3349185694Srrs	aa->ifa = NULL;
3350185694Srrs	aa->sent = 0;		/* clear sent flag */
3351185694Srrs	vtag = (struct sctp_asconf_tag_param *)&aa->ap.aph;
3352185694Srrs	vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
3353185694Srrs	vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
3354185694Srrs	vtag->local_vtag = htonl(stcb->asoc.my_vtag);
3355185694Srrs	vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
3356185694Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3357185694Srrs
3358185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3359185694Srrs	    SCTP_M_ASC_ADDR);
3360185694Srrs	if (aa == NULL) {
3361185694Srrs		/* didn't get memory */
3362185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3363185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3364185694Srrs		return;
3365185694Srrs	}
3366185694Srrs	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3367185694Srrs	/* fill in asconf address parameter fields */
3368185694Srrs	/* ADD(0.0.0.0) */
3369221249Stuexen	switch (net->ro._l_addr.sa.sa_family) {
3370221249Stuexen#ifdef INET
3371221249Stuexen	case AF_INET:
3372185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3373185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3374185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3375185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3376185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3377185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3378221249Stuexen		break;
3379221249Stuexen#endif
3380185694Srrs#ifdef INET6
3381221249Stuexen	case AF_INET6:
3382185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3383185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3384185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3385185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3386185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3387185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3388221249Stuexen		break;
3389221249Stuexen#endif
3390185694Srrs	}
3391185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3392185694Srrs	    SCTP_M_ASC_ADDR);
3393185694Srrs	if (aa == NULL) {
3394185694Srrs		/* didn't get memory */
3395185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3396185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3397185694Srrs		return;
3398185694Srrs	}
3399185694Srrs	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3400185694Srrs	/* fill in asconf address parameter fields */
3401185694Srrs	/* ADD(0.0.0.0) */
3402221249Stuexen	switch (net->ro._l_addr.sa.sa_family) {
3403221249Stuexen#ifdef INET
3404221249Stuexen	case AF_INET:
3405185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3406185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3407185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3408185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3409185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3410185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3411221249Stuexen		break;
3412221249Stuexen#endif
3413185694Srrs#ifdef INET6
3414221249Stuexen	case AF_INET6:
3415185694Srrs		aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3416185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3417185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3418185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3419185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3420185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3421221249Stuexen		break;
3422221249Stuexen#endif
3423185694Srrs	}
3424185694Srrs	/* Now we must hunt the addresses and add all global addresses */
3425185694Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3426185694Srrs		struct sctp_vrf *vrf = NULL;
3427185694Srrs		struct sctp_ifn *sctp_ifnp;
3428185694Srrs		uint32_t vrf_id;
3429185694Srrs
3430185694Srrs		vrf_id = stcb->sctp_ep->def_vrf_id;
3431185694Srrs		vrf = sctp_find_vrf(vrf_id);
3432185694Srrs		if (vrf == NULL) {
3433185694Srrs			goto skip_rest;
3434185694Srrs		}
3435185694Srrs		SCTP_IPI_ADDR_RLOCK();
3436185694Srrs		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3437185694Srrs			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3438221249Stuexen				switch (sctp_ifap->address.sa.sa_family) {
3439221249Stuexen#ifdef INET
3440221249Stuexen				case AF_INET:
3441185694Srrs					to = &sctp_ifap->address.sin;
3442267799Stuexen					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3443267799Stuexen					    &to->sin_addr) != 0) {
3444267799Stuexen						continue;
3445267799Stuexen					}
3446185694Srrs					if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3447185694Srrs						continue;
3448185694Srrs					}
3449185694Srrs					if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3450185694Srrs						continue;
3451185694Srrs					}
3452221249Stuexen					break;
3453221249Stuexen#endif
3454185694Srrs#ifdef INET6
3455221249Stuexen				case AF_INET6:
3456185694Srrs					to6 = &sctp_ifap->address.sin6;
3457267799Stuexen					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3458267799Stuexen					    &to6->sin6_addr) != 0) {
3459267799Stuexen						continue;
3460267799Stuexen					}
3461185694Srrs					if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3462185694Srrs						continue;
3463185694Srrs					}
3464185694Srrs					if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3465185694Srrs						continue;
3466185694Srrs					}
3467221249Stuexen					break;
3468221249Stuexen#endif
3469221249Stuexen				default:
3470221249Stuexen					continue;
3471185694Srrs				}
3472185694Srrs				sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3473185694Srrs			}
3474185694Srrs		}
3475185694Srrs		SCTP_IPI_ADDR_RUNLOCK();
3476185694Srrs	} else {
3477185694Srrs		struct sctp_laddr *laddr;
3478185694Srrs
3479185694Srrs		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3480185694Srrs			if (laddr->ifa == NULL) {
3481185694Srrs				continue;
3482185694Srrs			}
3483185694Srrs			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3484185694Srrs				/*
3485185694Srrs				 * Address being deleted by the system, dont
3486185694Srrs				 * list.
3487185694Srrs				 */
3488185694Srrs				continue;
3489185694Srrs			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3490185694Srrs				/*
3491185694Srrs				 * Address being deleted on this ep don't
3492185694Srrs				 * list.
3493185694Srrs				 */
3494185694Srrs				continue;
3495185694Srrs			}
3496185694Srrs			sctp_ifap = laddr->ifa;
3497221249Stuexen			switch (sctp_ifap->address.sa.sa_family) {
3498221249Stuexen#ifdef INET
3499221249Stuexen			case AF_INET:
3500185694Srrs				to = &sctp_ifap->address.sin;
3501185694Srrs				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3502185694Srrs					continue;
3503185694Srrs				}
3504185694Srrs				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3505185694Srrs					continue;
3506185694Srrs				}
3507221249Stuexen				break;
3508221249Stuexen#endif
3509185694Srrs#ifdef INET6
3510221249Stuexen			case AF_INET6:
3511185694Srrs				to6 = &sctp_ifap->address.sin6;
3512185694Srrs				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3513185694Srrs					continue;
3514185694Srrs				}
3515185694Srrs				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3516185694Srrs					continue;
3517185694Srrs				}
3518221249Stuexen				break;
3519221249Stuexen#endif
3520221249Stuexen			default:
3521221249Stuexen				continue;
3522185694Srrs			}
3523185694Srrs			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3524185694Srrs		}
3525185694Srrs	}
3526185694Srrsskip_rest:
3527185694Srrs	/* Now we must send the asconf into the queue */
3528224641Stuexen	sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
3529185694Srrs}
3530