sctp_asconf.c revision 197288
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs/* $KAME: sctp_asconf.c,v 1.24 2005/03/06 16:04:16 itojun Exp $	 */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163957Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_asconf.c 197288 2009-09-17 15:11:12Z rrs $");
35163953Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <netinet/sctp_var.h>
37167598Srrs#include <netinet/sctp_sysctl.h>
38163953Srrs#include <netinet/sctp_pcb.h>
39163953Srrs#include <netinet/sctp_header.h>
40163953Srrs#include <netinet/sctputil.h>
41163953Srrs#include <netinet/sctp_output.h>
42163953Srrs#include <netinet/sctp_asconf.h>
43172091Srrs#include <netinet/sctp_timer.h>
44163953Srrs
45163953Srrs/*
46163953Srrs * debug flags:
47163953Srrs * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
48163953Srrs * SCTP_DEBUG_ASCONF2: detailed info
49163953Srrs */
50163953Srrs#ifdef SCTP_DEBUG
51163953Srrs#endif				/* SCTP_DEBUG */
52163953Srrs
53163953Srrs
54169420Srrsstatic void
55163953Srrssctp_asconf_get_source_ip(struct mbuf *m, struct sockaddr *sa)
56163953Srrs{
57163953Srrs	struct ip *iph;
58163953Srrs	struct sockaddr_in *sin;
59163953Srrs
60163953Srrs#ifdef INET6
61163953Srrs	struct sockaddr_in6 *sin6;
62163953Srrs
63163953Srrs#endif
64163953Srrs
65163953Srrs	iph = mtod(m, struct ip *);
66163953Srrs	if (iph->ip_v == IPVERSION) {
67163953Srrs		/* IPv4 source */
68163953Srrs		sin = (struct sockaddr_in *)sa;
69163953Srrs		bzero(sin, sizeof(*sin));
70163953Srrs		sin->sin_family = AF_INET;
71163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
72163953Srrs		sin->sin_port = 0;
73163953Srrs		sin->sin_addr.s_addr = iph->ip_src.s_addr;
74169420Srrs		return;
75163953Srrs	}
76163953Srrs#ifdef INET6
77163953Srrs	else if (iph->ip_v == (IPV6_VERSION >> 4)) {
78163953Srrs		/* IPv6 source */
79163953Srrs		struct ip6_hdr *ip6;
80163953Srrs
81163953Srrs		sin6 = (struct sockaddr_in6 *)sa;
82163953Srrs		bzero(sin6, sizeof(*sin6));
83163953Srrs		sin6->sin6_family = AF_INET6;
84163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
85163953Srrs		sin6->sin6_port = 0;
86163953Srrs		ip6 = mtod(m, struct ip6_hdr *);
87163953Srrs		sin6->sin6_addr = ip6->ip6_src;
88169420Srrs		return;
89163953Srrs	}
90163953Srrs#endif				/* INET6 */
91163953Srrs	else
92169420Srrs		return;
93163953Srrs}
94163953Srrs
95163953Srrs/*
96163953Srrs * draft-ietf-tsvwg-addip-sctp
97163953Srrs *
98163953Srrs * An ASCONF parameter queue exists per asoc which holds the pending address
99163953Srrs * operations.  Lists are updated upon receipt of ASCONF-ACK.
100163953Srrs *
101171477Srrs * A restricted_addrs list exists per assoc to hold local addresses that are
102171477Srrs * not (yet) usable by the assoc as a source address.  These addresses are
103171477Srrs * either pending an ASCONF operation (and exist on the ASCONF parameter
104171477Srrs * queue), or they are permanently restricted (the peer has returned an
105171477Srrs * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
106171477Srrs *
107163953Srrs * Deleted addresses are always immediately removed from the lists as they will
108163953Srrs * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
109163953Srrs * only if allowed.
110163953Srrs */
111163953Srrs
112163953Srrs/*
113171477Srrs * ASCONF parameter processing.
114171477Srrs * response_required: set if a reply is required (eg. SUCCESS_REPORT).
115171477Srrs * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
116171477Srrs * FIX: allocating this many mbufs on the fly is pretty inefficient...
117163953Srrs */
118163953Srrsstatic struct mbuf *
119163953Srrssctp_asconf_success_response(uint32_t id)
120163953Srrs{
121163953Srrs	struct mbuf *m_reply = NULL;
122163953Srrs	struct sctp_asconf_paramhdr *aph;
123163953Srrs
124163953Srrs	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
125163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
126163953Srrs	if (m_reply == NULL) {
127169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
128169420Srrs		    "asconf_success_response: couldn't get mbuf!\n");
129163953Srrs		return NULL;
130163953Srrs	}
131163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
132163953Srrs	aph->correlation_id = id;
133163953Srrs	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
134163953Srrs	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
135165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
136163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
137163953Srrs
138163953Srrs	return m_reply;
139163953Srrs}
140163953Srrs
141163953Srrsstatic struct mbuf *
142163953Srrssctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
143163953Srrs    uint16_t tlv_length)
144163953Srrs{
145163953Srrs	struct mbuf *m_reply = NULL;
146163953Srrs	struct sctp_asconf_paramhdr *aph;
147163953Srrs	struct sctp_error_cause *error;
148163953Srrs	uint8_t *tlv;
149163953Srrs
150163953Srrs	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
151163953Srrs	    tlv_length +
152163953Srrs	    sizeof(struct sctp_error_cause)),
153163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
154163953Srrs	if (m_reply == NULL) {
155169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
156169420Srrs		    "asconf_error_response: couldn't get mbuf!\n");
157163953Srrs		return NULL;
158163953Srrs	}
159163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
160163953Srrs	error = (struct sctp_error_cause *)(aph + 1);
161163953Srrs
162163953Srrs	aph->correlation_id = id;
163163953Srrs	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
164163953Srrs	error->code = htons(cause);
165163953Srrs	error->length = tlv_length + sizeof(struct sctp_error_cause);
166163953Srrs	aph->ph.param_length = error->length +
167163953Srrs	    sizeof(struct sctp_asconf_paramhdr);
168163953Srrs
169163953Srrs	if (aph->ph.param_length > MLEN) {
170169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
171169420Srrs		    "asconf_error_response: tlv_length (%xh) too big\n",
172169420Srrs		    tlv_length);
173163953Srrs		sctp_m_freem(m_reply);	/* discard */
174163953Srrs		return NULL;
175163953Srrs	}
176163953Srrs	if (error_tlv != NULL) {
177163953Srrs		tlv = (uint8_t *) (error + 1);
178163953Srrs		memcpy(tlv, error_tlv, tlv_length);
179163953Srrs	}
180165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
181163953Srrs	error->length = htons(error->length);
182163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
183163953Srrs
184163953Srrs	return m_reply;
185163953Srrs}
186163953Srrs
187163953Srrsstatic struct mbuf *
188163953Srrssctp_process_asconf_add_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
189163953Srrs    struct sctp_tcb *stcb, int response_required)
190163953Srrs{
191163953Srrs	struct mbuf *m_reply = NULL;
192163953Srrs	struct sockaddr_storage sa_source, sa_store;
193163953Srrs	struct sctp_ipv4addr_param *v4addr;
194163953Srrs	uint16_t param_type, param_length, aparam_length;
195163953Srrs	struct sockaddr *sa;
196163953Srrs	struct sockaddr_in *sin;
197163953Srrs	int zero_address = 0;
198163953Srrs
199163953Srrs#ifdef INET6
200163953Srrs	struct sockaddr_in6 *sin6;
201163953Srrs	struct sctp_ipv6addr_param *v6addr;
202163953Srrs
203163953Srrs#endif				/* INET6 */
204163953Srrs
205163953Srrs	aparam_length = ntohs(aph->ph.param_length);
206163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
207163953Srrs#ifdef INET6
208163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
209163953Srrs#endif				/* INET6 */
210163953Srrs	param_type = ntohs(v4addr->ph.param_type);
211163953Srrs	param_length = ntohs(v4addr->ph.param_length);
212163953Srrs
213163953Srrs	sa = (struct sockaddr *)&sa_store;
214163953Srrs	switch (param_type) {
215163953Srrs	case SCTP_IPV4_ADDRESS:
216163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
217163953Srrs			/* invalid param size */
218163953Srrs			return NULL;
219163953Srrs		}
220163953Srrs		sin = (struct sockaddr_in *)&sa_store;
221163953Srrs		bzero(sin, sizeof(*sin));
222163953Srrs		sin->sin_family = AF_INET;
223163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
224163953Srrs		sin->sin_port = stcb->rport;
225163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
226163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
227163953Srrs			zero_address = 1;
228169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
229169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
230163953Srrs		break;
231163953Srrs	case SCTP_IPV6_ADDRESS:
232163953Srrs#ifdef INET6
233163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
234163953Srrs			/* invalid param size */
235163953Srrs			return NULL;
236163953Srrs		}
237163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
238163953Srrs		bzero(sin6, sizeof(*sin6));
239163953Srrs		sin6->sin6_family = AF_INET6;
240163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
241163953Srrs		sin6->sin6_port = stcb->rport;
242163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
243163953Srrs		    sizeof(struct in6_addr));
244163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
245163953Srrs			zero_address = 1;
246169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
247169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
248163953Srrs#else
249163953Srrs		/* IPv6 not enabled! */
250163953Srrs		/* FIX ME: currently sends back an invalid param error */
251163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
252163953Srrs		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph, aparam_length);
253169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
254169420Srrs		    "process_asconf_add_ip: v6 disabled- skipping ");
255169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
256163953Srrs		return m_reply;
257169420Srrs#endif
258163953Srrs		break;
259163953Srrs	default:
260163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
261163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
262163953Srrs		    aparam_length);
263163953Srrs		return m_reply;
264163953Srrs	}			/* end switch */
265163953Srrs
266163953Srrs	/* if 0.0.0.0/::0, add the source address instead */
267179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
268163953Srrs		sa = (struct sockaddr *)&sa_source;
269163953Srrs		sctp_asconf_get_source_ip(m, sa);
270169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
271169420Srrs		    "process_asconf_add_ip: using source addr ");
272169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
273163953Srrs	}
274163953Srrs	/* add the address */
275165220Srrs	if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE,
276165220Srrs	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
277169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
278169420Srrs		    "process_asconf_add_ip: error adding address\n");
279163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
280163953Srrs		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
281163953Srrs		    aparam_length);
282163953Srrs	} else {
283163953Srrs		/* notify upper layer */
284172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
285163953Srrs		if (response_required) {
286163953Srrs			m_reply =
287163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
288163953Srrs		}
289169655Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
290169655Srrs		    NULL, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
291169655Srrs		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
292169655Srrs		    stcb, NULL);
293163953Srrs	}
294163953Srrs
295163953Srrs	return m_reply;
296163953Srrs}
297163953Srrs
298163953Srrsstatic int
299171572Srrssctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
300163953Srrs{
301163953Srrs	struct sctp_nets *src_net, *net;
302163953Srrs
303163953Srrs	/* make sure the source address exists as a destination net */
304163953Srrs	src_net = sctp_findnet(stcb, src);
305163953Srrs	if (src_net == NULL) {
306163953Srrs		/* not found */
307163953Srrs		return -1;
308163953Srrs	}
309163953Srrs	/* delete all destination addresses except the source */
310163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
311163953Srrs		if (net != src_net) {
312163953Srrs			/* delete this address */
313163953Srrs			sctp_remove_net(stcb, net);
314169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
315169420Srrs			    "asconf_del_remote_addrs_except: deleting ");
316169420Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
317169420Srrs			    (struct sockaddr *)&net->ro._l_addr);
318163953Srrs			/* notify upper layer */
319163953Srrs			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
320172090Srrs			    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
321163953Srrs		}
322163953Srrs	}
323163953Srrs	return 0;
324163953Srrs}
325163953Srrs
326163953Srrsstatic struct mbuf *
327163953Srrssctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
328163953Srrs    struct sctp_tcb *stcb, int response_required)
329163953Srrs{
330163953Srrs	struct mbuf *m_reply = NULL;
331163953Srrs	struct sockaddr_storage sa_source, sa_store;
332163953Srrs	struct sctp_ipv4addr_param *v4addr;
333163953Srrs	uint16_t param_type, param_length, aparam_length;
334163953Srrs	struct sockaddr *sa;
335163953Srrs	struct sockaddr_in *sin;
336163953Srrs	int zero_address = 0;
337163953Srrs	int result;
338163953Srrs
339163953Srrs#ifdef INET6
340163953Srrs	struct sockaddr_in6 *sin6;
341163953Srrs	struct sctp_ipv6addr_param *v6addr;
342163953Srrs
343163953Srrs#endif				/* INET6 */
344163953Srrs
345163953Srrs	/* get the source IP address for src and 0.0.0.0/::0 delete checks */
346163953Srrs	sctp_asconf_get_source_ip(m, (struct sockaddr *)&sa_source);
347163953Srrs
348163953Srrs	aparam_length = ntohs(aph->ph.param_length);
349163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
350163953Srrs#ifdef INET6
351163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
352163953Srrs#endif				/* INET6 */
353163953Srrs	param_type = ntohs(v4addr->ph.param_type);
354163953Srrs	param_length = ntohs(v4addr->ph.param_length);
355163953Srrs
356163953Srrs	sa = (struct sockaddr *)&sa_store;
357163953Srrs	switch (param_type) {
358163953Srrs	case SCTP_IPV4_ADDRESS:
359163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
360163953Srrs			/* invalid param size */
361163953Srrs			return NULL;
362163953Srrs		}
363163953Srrs		sin = (struct sockaddr_in *)&sa_store;
364163953Srrs		bzero(sin, sizeof(*sin));
365163953Srrs		sin->sin_family = AF_INET;
366163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
367163953Srrs		sin->sin_port = stcb->rport;
368163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
369163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
370163953Srrs			zero_address = 1;
371169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
372169420Srrs		    "process_asconf_delete_ip: deleting ");
373169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
374163953Srrs		break;
375163953Srrs	case SCTP_IPV6_ADDRESS:
376163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
377163953Srrs			/* invalid param size */
378163953Srrs			return NULL;
379163953Srrs		}
380163953Srrs#ifdef INET6
381163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
382163953Srrs		bzero(sin6, sizeof(*sin6));
383163953Srrs		sin6->sin6_family = AF_INET6;
384163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
385163953Srrs		sin6->sin6_port = stcb->rport;
386163953Srrs		memcpy(&sin6->sin6_addr, v6addr->addr,
387163953Srrs		    sizeof(struct in6_addr));
388163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
389163953Srrs			zero_address = 1;
390169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
391169420Srrs		    "process_asconf_delete_ip: deleting ");
392169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
393163953Srrs#else
394163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
395169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
396169420Srrs		    "process_asconf_delete_ip: v6 disabled- ignoring: ");
397169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
398163953Srrs		/* just respond with a "success" ASCONF-ACK */
399163953Srrs		return NULL;
400169420Srrs#endif
401163953Srrs		break;
402163953Srrs	default:
403163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
404163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
405163953Srrs		    aparam_length);
406163953Srrs		return m_reply;
407163953Srrs	}
408163953Srrs
409163953Srrs	/* make sure the source address is not being deleted */
410163953Srrs	if (sctp_cmpaddr(sa, (struct sockaddr *)&sa_source)) {
411163953Srrs		/* trying to delete the source address! */
412169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
413163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
414163953Srrs		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
415163953Srrs		    aparam_length);
416163953Srrs		return m_reply;
417163953Srrs	}
418163953Srrs	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
419179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
420163953Srrs		result = sctp_asconf_del_remote_addrs_except(stcb,
421163953Srrs		    (struct sockaddr *)&sa_source);
422163953Srrs
423163953Srrs		if (result) {
424163953Srrs			/* src address did not exist? */
425169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
426163953Srrs			/* what error to reply with?? */
427163953Srrs			m_reply =
428163953Srrs			    sctp_asconf_error_response(aph->correlation_id,
429163953Srrs			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
430163953Srrs			    aparam_length);
431163953Srrs		} else if (response_required) {
432163953Srrs			m_reply =
433163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
434163953Srrs		}
435163953Srrs		return m_reply;
436163953Srrs	}
437163953Srrs	/* delete the address */
438163953Srrs	result = sctp_del_remote_addr(stcb, sa);
439163953Srrs	/*
440163953Srrs	 * note if result == -2, the address doesn't exist in the asoc but
441163953Srrs	 * since it's being deleted anyways, we just ack the delete -- but
442163953Srrs	 * this probably means something has already gone awry
443163953Srrs	 */
444163953Srrs	if (result == -1) {
445163953Srrs		/* only one address in the asoc */
446169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
447163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
448163953Srrs		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
449163953Srrs		    aparam_length);
450163953Srrs	} else {
451163953Srrs		if (response_required) {
452163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
453163953Srrs		}
454163953Srrs		/* notify upper layer */
455172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
456163953Srrs	}
457163953Srrs	return m_reply;
458163953Srrs}
459163953Srrs
460163953Srrsstatic struct mbuf *
461163953Srrssctp_process_asconf_set_primary(struct mbuf *m,
462171572Srrs    struct sctp_asconf_paramhdr *aph,
463171572Srrs    struct sctp_tcb *stcb, int response_required)
464163953Srrs{
465163953Srrs	struct mbuf *m_reply = NULL;
466163953Srrs	struct sockaddr_storage sa_source, sa_store;
467163953Srrs	struct sctp_ipv4addr_param *v4addr;
468163953Srrs	uint16_t param_type, param_length, aparam_length;
469163953Srrs	struct sockaddr *sa;
470163953Srrs	struct sockaddr_in *sin;
471163953Srrs	int zero_address = 0;
472163953Srrs
473163953Srrs#ifdef INET6
474163953Srrs	struct sockaddr_in6 *sin6;
475163953Srrs	struct sctp_ipv6addr_param *v6addr;
476163953Srrs
477163953Srrs#endif				/* INET6 */
478163953Srrs
479163953Srrs	aparam_length = ntohs(aph->ph.param_length);
480163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
481163953Srrs#ifdef INET6
482163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
483163953Srrs#endif				/* INET6 */
484163953Srrs	param_type = ntohs(v4addr->ph.param_type);
485163953Srrs	param_length = ntohs(v4addr->ph.param_length);
486163953Srrs
487163953Srrs	sa = (struct sockaddr *)&sa_store;
488163953Srrs	switch (param_type) {
489163953Srrs	case SCTP_IPV4_ADDRESS:
490163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
491163953Srrs			/* invalid param size */
492163953Srrs			return NULL;
493163953Srrs		}
494163953Srrs		sin = (struct sockaddr_in *)&sa_store;
495163953Srrs		bzero(sin, sizeof(*sin));
496163953Srrs		sin->sin_family = AF_INET;
497163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
498163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
499163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
500163953Srrs			zero_address = 1;
501169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
502169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
503163953Srrs		break;
504163953Srrs	case SCTP_IPV6_ADDRESS:
505163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
506163953Srrs			/* invalid param size */
507163953Srrs			return NULL;
508163953Srrs		}
509163953Srrs#ifdef INET6
510163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
511163953Srrs		bzero(sin6, sizeof(*sin6));
512163953Srrs		sin6->sin6_family = AF_INET6;
513163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
514163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
515163953Srrs		    sizeof(struct in6_addr));
516163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
517163953Srrs			zero_address = 1;
518169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
519169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
520163953Srrs#else
521163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
522169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
523169420Srrs		    "process_asconf_set_primary: v6 disabled- ignoring: ");
524169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
525163953Srrs		/* just respond with a "success" ASCONF-ACK */
526163953Srrs		return NULL;
527169420Srrs#endif
528163953Srrs		break;
529163953Srrs	default:
530163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
531163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
532163953Srrs		    aparam_length);
533163953Srrs		return m_reply;
534163953Srrs	}
535163953Srrs
536163953Srrs	/* if 0.0.0.0/::0, use the source address instead */
537179783Srrs	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
538163953Srrs		sa = (struct sockaddr *)&sa_source;
539163953Srrs		sctp_asconf_get_source_ip(m, sa);
540169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
541169420Srrs		    "process_asconf_set_primary: using source addr ");
542169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
543163953Srrs	}
544163953Srrs	/* set the primary address */
545163953Srrs	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
546169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
547169420Srrs		    "process_asconf_set_primary: primary address set\n");
548163953Srrs		/* notify upper layer */
549172090Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
550163953Srrs
551163953Srrs		if (response_required) {
552163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
553163953Srrs		}
554172091Srrs		/*
555172091Srrs		 * Mobility adaptation. Ideally, when the reception of SET
556172091Srrs		 * PRIMARY with DELETE IP ADDRESS of the previous primary
557172091Srrs		 * destination, unacknowledged DATA are retransmitted
558172091Srrs		 * immediately to the new primary destination for seamless
559172091Srrs		 * handover.  If the destination is UNCONFIRMED and marked
560172091Srrs		 * to REQ_PRIM, The retransmission occur when reception of
561172091Srrs		 * the HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
562172091Srrs		 * sctp_input.c) Also, when change of the primary
563172091Srrs		 * destination, it is better that all subsequent new DATA
564172091Srrs		 * containing already queued DATA are transmitted to the new
565172091Srrs		 * primary destination. (by micchie)
566172091Srrs		 */
567172091Srrs		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
568172091Srrs		    SCTP_MOBILITY_BASE) ||
569172091Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
570172091Srrs		    SCTP_MOBILITY_FASTHANDOFF)) &&
571172091Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
572172091Srrs		    SCTP_MOBILITY_PRIM_DELETED) &&
573172091Srrs		    (stcb->asoc.primary_destination->dest_state &
574172091Srrs		    SCTP_ADDR_UNCONFIRMED) == 0) {
575172091Srrs
576172091Srrs			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
577172091Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
578172091Srrs			    SCTP_MOBILITY_FASTHANDOFF)) {
579172091Srrs				sctp_assoc_immediate_retrans(stcb,
580172091Srrs				    stcb->asoc.primary_destination);
581172091Srrs			}
582172091Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
583172091Srrs			    SCTP_MOBILITY_BASE)) {
584172091Srrs				sctp_move_chunks_from_deleted_prim(stcb,
585172091Srrs				    stcb->asoc.primary_destination);
586172091Srrs			}
587172091Srrs			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
588172091Srrs			    stcb->asoc.deleted_primary);
589172091Srrs		}
590163953Srrs	} else {
591163953Srrs		/* couldn't set the requested primary address! */
592169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
593169420Srrs		    "process_asconf_set_primary: set primary failed!\n");
594163953Srrs		/* must have been an invalid address, so report */
595163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
596163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
597163953Srrs		    aparam_length);
598163953Srrs	}
599163953Srrs
600163953Srrs	return m_reply;
601163953Srrs}
602163953Srrs
603163953Srrs/*
604163953Srrs * handles an ASCONF chunk.
605163953Srrs * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
606163953Srrs */
607163953Srrsvoid
608163953Srrssctp_handle_asconf(struct mbuf *m, unsigned int offset,
609171990Srrs    struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
610171990Srrs    int first)
611163953Srrs{
612163953Srrs	struct sctp_association *asoc;
613163953Srrs	uint32_t serial_num;
614171990Srrs	struct mbuf *n, *m_ack, *m_result, *m_tail;
615163953Srrs	struct sctp_asconf_ack_chunk *ack_cp;
616163953Srrs	struct sctp_asconf_paramhdr *aph, *ack_aph;
617163953Srrs	struct sctp_ipv6addr_param *p_addr;
618163953Srrs	unsigned int asconf_limit;
619163953Srrs	int error = 0;		/* did an error occur? */
620163953Srrs
621163953Srrs	/* asconf param buffer */
622166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
623171990Srrs	struct sctp_asconf_ack *ack, *ack_next;
624163953Srrs
625163953Srrs	/* verify minimum length */
626163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
627169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
628169420Srrs		    "handle_asconf: chunk too small = %xh\n",
629169420Srrs		    ntohs(cp->ch.chunk_length));
630163953Srrs		return;
631163953Srrs	}
632163953Srrs	asoc = &stcb->asoc;
633163953Srrs	serial_num = ntohl(cp->serial_number);
634163953Srrs
635171990Srrs	if (compare_with_wrap(asoc->asconf_seq_in, serial_num, MAX_SEQ) ||
636171990Srrs	    serial_num == asoc->asconf_seq_in) {
637163953Srrs		/* got a duplicate ASCONF */
638169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
639169420Srrs		    "handle_asconf: got duplicate serial number = %xh\n",
640169420Srrs		    serial_num);
641163953Srrs		return;
642163953Srrs	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
643169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
644169420Srrs		    serial_num, asoc->asconf_seq_in + 1);
645163953Srrs		return;
646163953Srrs	}
647163953Srrs	/* it's the expected "next" sequence number, so process it */
648163953Srrs	asoc->asconf_seq_in = serial_num;	/* update sequence */
649163953Srrs	/* get length of all the param's in the ASCONF */
650163953Srrs	asconf_limit = offset + ntohs(cp->ch.chunk_length);
651169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1,
652169420Srrs	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
653169420Srrs	    asconf_limit, serial_num);
654171990Srrs
655171990Srrs	if (first) {
656171990Srrs		/* delete old cache */
657171990Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: Now processing firstASCONF. Try to delte old cache\n");
658171990Srrs
659171990Srrs		ack = TAILQ_FIRST(&stcb->asoc.asconf_ack_sent);
660171990Srrs		while (ack != NULL) {
661171990Srrs			ack_next = TAILQ_NEXT(ack, next);
662171990Srrs			if (ack->serial_number == serial_num)
663171990Srrs				break;
664171990Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: delete old(%u) < first(%u)\n",
665171990Srrs			    ack->serial_number, serial_num);
666171990Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_ack_sent, ack, next);
667171990Srrs			if (ack->data != NULL) {
668171990Srrs				sctp_m_freem(ack->data);
669171990Srrs			}
670179783Srrs			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
671171990Srrs			ack = ack_next;
672171990Srrs		}
673163953Srrs	}
674165647Srrs	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
675163953Srrs	    M_DONTWAIT, 1, MT_DATA);
676163953Srrs	if (m_ack == NULL) {
677169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
678169420Srrs		    "handle_asconf: couldn't get mbuf!\n");
679163953Srrs		return;
680163953Srrs	}
681163953Srrs	m_tail = m_ack;		/* current reply chain's tail */
682163953Srrs
683163953Srrs	/* fill in ASCONF-ACK header */
684163953Srrs	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
685163953Srrs	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
686163953Srrs	ack_cp->ch.chunk_flags = 0;
687163953Srrs	ack_cp->serial_number = htonl(serial_num);
688163953Srrs	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
689165647Srrs	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
690163953Srrs	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
691163953Srrs
692163953Srrs	/* skip the lookup address parameter */
693163953Srrs	offset += sizeof(struct sctp_asconf_chunk);
694163953Srrs	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
695163953Srrs	if (p_addr == NULL) {
696169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
697169420Srrs		    "handle_asconf: couldn't get lookup addr!\n");
698163953Srrs		/* respond with a missing/invalid mandatory parameter error */
699163953Srrs		return;
700163953Srrs	}
701163953Srrs	/* param_length is already validated in process_control... */
702163953Srrs	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
703163953Srrs
704163953Srrs	/* get pointer to first asconf param in ASCONF-ACK */
705163953Srrs	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
706163953Srrs	if (ack_aph == NULL) {
707169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Gak in asconf2\n");
708163953Srrs		return;
709163953Srrs	}
710163953Srrs	/* get pointer to first asconf param in ASCONF */
711163953Srrs	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
712163953Srrs	if (aph == NULL) {
713169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
714163953Srrs		goto send_reply;
715163953Srrs	}
716163953Srrs	/* process through all parameters */
717163953Srrs	while (aph != NULL) {
718163953Srrs		unsigned int param_length, param_type;
719163953Srrs
720163953Srrs		param_type = ntohs(aph->ph.param_type);
721163953Srrs		param_length = ntohs(aph->ph.param_length);
722163953Srrs		if (offset + param_length > asconf_limit) {
723163953Srrs			/* parameter goes beyond end of chunk! */
724163953Srrs			sctp_m_freem(m_ack);
725163953Srrs			return;
726163953Srrs		}
727163953Srrs		m_result = NULL;
728163953Srrs
729163953Srrs		if (param_length > sizeof(aparam_buf)) {
730169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
731163953Srrs			sctp_m_freem(m_ack);
732163953Srrs			return;
733163953Srrs		}
734163953Srrs		if (param_length <= sizeof(struct sctp_paramhdr)) {
735169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
736163953Srrs			sctp_m_freem(m_ack);
737163953Srrs		}
738163953Srrs		/* get the entire parameter */
739163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
740163953Srrs		if (aph == NULL) {
741169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
742163953Srrs			sctp_m_freem(m_ack);
743163953Srrs			return;
744163953Srrs		}
745163953Srrs		switch (param_type) {
746163953Srrs		case SCTP_ADD_IP_ADDRESS:
747163953Srrs			asoc->peer_supports_asconf = 1;
748163953Srrs			m_result = sctp_process_asconf_add_ip(m, aph, stcb,
749163953Srrs			    error);
750163953Srrs			break;
751163953Srrs		case SCTP_DEL_IP_ADDRESS:
752163953Srrs			asoc->peer_supports_asconf = 1;
753163953Srrs			m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
754163953Srrs			    error);
755163953Srrs			break;
756163953Srrs		case SCTP_ERROR_CAUSE_IND:
757163953Srrs			/* not valid in an ASCONF chunk */
758163953Srrs			break;
759163953Srrs		case SCTP_SET_PRIM_ADDR:
760163953Srrs			asoc->peer_supports_asconf = 1;
761163953Srrs			m_result = sctp_process_asconf_set_primary(m, aph,
762163953Srrs			    stcb, error);
763163953Srrs			break;
764185694Srrs		case SCTP_NAT_VTAGS:
765185694Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
766185694Srrs			break;
767163953Srrs		case SCTP_SUCCESS_REPORT:
768163953Srrs			/* not valid in an ASCONF chunk */
769163953Srrs			break;
770163953Srrs		case SCTP_ULP_ADAPTATION:
771163953Srrs			/* FIX */
772163953Srrs			break;
773163953Srrs		default:
774163953Srrs			if ((param_type & 0x8000) == 0) {
775163953Srrs				/* Been told to STOP at this param */
776163953Srrs				asconf_limit = offset;
777163953Srrs				/*
778163953Srrs				 * FIX FIX - We need to call
779163953Srrs				 * sctp_arethere_unrecognized_parameters()
780163953Srrs				 * to get a operr and send it for any
781163953Srrs				 * param's with the 0x4000 bit set OR do it
782163953Srrs				 * here ourselves... note we still must STOP
783163953Srrs				 * if the 0x8000 bit is clear.
784163953Srrs				 */
785163953Srrs			}
786163953Srrs			/* unknown/invalid param type */
787163953Srrs			break;
788163953Srrs		}		/* switch */
789163953Srrs
790163953Srrs		/* add any (error) result to the reply mbuf chain */
791163953Srrs		if (m_result != NULL) {
792165647Srrs			SCTP_BUF_NEXT(m_tail) = m_result;
793163953Srrs			m_tail = m_result;
794163953Srrs			/* update lengths, make sure it's aligned too */
795165647Srrs			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
796165647Srrs			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
797163953Srrs			/* set flag to force success reports */
798163953Srrs			error = 1;
799163953Srrs		}
800163953Srrs		offset += SCTP_SIZE32(param_length);
801163953Srrs		/* update remaining ASCONF message length to process */
802163953Srrs		if (offset >= asconf_limit) {
803163953Srrs			/* no more data in the mbuf chain */
804163953Srrs			break;
805163953Srrs		}
806163953Srrs		/* get pointer to next asconf param */
807163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
808163953Srrs		    sizeof(struct sctp_asconf_paramhdr),
809163953Srrs		    (uint8_t *) & aparam_buf);
810163953Srrs		if (aph == NULL) {
811163953Srrs			/* can't get an asconf paramhdr */
812169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
813163953Srrs			/* FIX ME - add error here... */
814163953Srrs		}
815169420Srrs	}
816163953Srrs
817163953Srrssend_reply:
818163953Srrs	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
819163953Srrs	/* save the ASCONF-ACK reply */
820179783Srrs	ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
821171990Srrs	    struct sctp_asconf_ack);
822171990Srrs	if (ack == NULL) {
823171990Srrs		sctp_m_freem(m_ack);
824171990Srrs		return;
825171990Srrs	}
826171990Srrs	ack->serial_number = serial_num;
827171990Srrs	ack->last_sent_to = NULL;
828171990Srrs	ack->data = m_ack;
829171990Srrs	n = m_ack;
830171990Srrs	while (n) {
831171990Srrs		ack->len += SCTP_BUF_LEN(n);
832171990Srrs		n = SCTP_BUF_NEXT(n);
833171990Srrs	}
834171990Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
835163953Srrs
836163953Srrs	/* see if last_control_chunk_from is set properly (use IP src addr) */
837163953Srrs	if (stcb->asoc.last_control_chunk_from == NULL) {
838163953Srrs		/*
839163953Srrs		 * this could happen if the source address was just newly
840163953Srrs		 * added
841163953Srrs		 */
842163953Srrs		struct ip *iph;
843163953Srrs		struct sctphdr *sh;
844163953Srrs		struct sockaddr_storage from_store;
845163953Srrs		struct sockaddr *from = (struct sockaddr *)&from_store;
846163953Srrs
847169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
848163953Srrs		/* pullup already done, IP options already stripped */
849163953Srrs		iph = mtod(m, struct ip *);
850163953Srrs		sh = (struct sctphdr *)((caddr_t)iph + sizeof(*iph));
851178251Srrs		switch (iph->ip_v) {
852178251Srrs		case IPVERSION:
853178251Srrs			{
854178251Srrs				struct sockaddr_in *from4;
855163953Srrs
856178251Srrs				from4 = (struct sockaddr_in *)&from_store;
857178251Srrs				bzero(from4, sizeof(*from4));
858178251Srrs				from4->sin_family = AF_INET;
859178251Srrs				from4->sin_len = sizeof(struct sockaddr_in);
860178251Srrs				from4->sin_addr.s_addr = iph->ip_src.s_addr;
861178251Srrs				from4->sin_port = sh->src_port;
862178251Srrs				break;
863178251Srrs			}
864178251Srrs#ifdef INET6
865178251Srrs		case IPV6_VERSION >> 4:
866178251Srrs			{
867178251Srrs				struct ip6_hdr *ip6;
868178251Srrs				struct sockaddr_in6 *from6;
869163953Srrs
870178251Srrs				ip6 = mtod(m, struct ip6_hdr *);
871178251Srrs				from6 = (struct sockaddr_in6 *)&from_store;
872178251Srrs				bzero(from6, sizeof(*from6));
873178251Srrs				from6->sin6_family = AF_INET6;
874178251Srrs				from6->sin6_len = sizeof(struct sockaddr_in6);
875178251Srrs				from6->sin6_addr = ip6->ip6_src;
876178251Srrs				from6->sin6_port = sh->src_port;
877178251Srrs				/*
878178251Srrs				 * Get the scopes in properly to the sin6
879178251Srrs				 * addr's
880178251Srrs				 */
881178251Srrs				/* we probably don't need these operations */
882178251Srrs				(void)sa6_recoverscope(from6);
883180387Srrs				sa6_embedscope(from6,
884197288Srrs				    MODULE_GLOBAL(ip6_use_defzone));
885180387Srrs
886178251Srrs				break;
887178251Srrs			}
888178251Srrs#endif
889178251Srrs		default:
890163953Srrs			/* unknown address type */
891163953Srrs			from = NULL;
892163953Srrs		}
893163953Srrs		if (from != NULL) {
894169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
895169420Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, from);
896163953Srrs			/* look up the from address */
897163953Srrs			stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, from);
898163953Srrs#ifdef SCTP_DEBUG
899169420Srrs			if (stcb->asoc.last_control_chunk_from == NULL)
900169420Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
901169420Srrs#endif
902163953Srrs		}
903163953Srrs	}
904163953Srrs}
905163953Srrs
906163953Srrs/*
907163953Srrs * does the address match? returns 0 if not, 1 if so
908163953Srrs */
909163953Srrsstatic uint32_t
910163953Srrssctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
911163953Srrs{
912163953Srrs#ifdef INET6
913163953Srrs	if (sa->sa_family == AF_INET6) {
914163953Srrs		/* IPv6 sa address */
915163953Srrs		/* XXX scopeid */
916163953Srrs		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
917163953Srrs
918163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
919163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
920163953Srrs		    sizeof(struct in6_addr)) == 0)) {
921163953Srrs			return (1);
922163953Srrs		}
923163953Srrs	} else
924163953Srrs#endif				/* INET6 */
925163953Srrs	if (sa->sa_family == AF_INET) {
926163953Srrs		/* IPv4 sa address */
927163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
928163953Srrs
929163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
930163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
931163953Srrs		    sizeof(struct in_addr)) == 0)) {
932163953Srrs			return (1);
933163953Srrs		}
934163953Srrs	}
935163953Srrs	return (0);
936163953Srrs}
937163953Srrs
938163953Srrs/*
939179157Srrs * does the address match? returns 0 if not, 1 if so
940179157Srrs */
941179157Srrsstatic uint32_t
942179157Srrssctp_addr_match(
943179157Srrs    struct sctp_ipv6addr_param *v6addr,
944179157Srrs    struct sockaddr *sa)
945179157Srrs{
946179157Srrs	uint16_t param_type, param_length;
947180955Srrs	struct sctp_ipv4addr_param *v4addr = (struct sctp_ipv4addr_param *)v6addr;
948179157Srrs
949179157Srrs#ifdef INET6
950179157Srrs	if (sa->sa_family == AF_INET6) {
951179157Srrs		/* IPv6 sa address */
952179157Srrs		/* XXX scopeid */
953179157Srrs		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
954179157Srrs
955179157Srrs		param_type = ntohs(v6addr->ph.param_type);
956179157Srrs		param_length = ntohs(v6addr->ph.param_length);
957179157Srrs
958179157Srrs		if ((param_type == SCTP_IPV6_ADDRESS) &&
959179157Srrs		    param_length == sizeof(struct sctp_ipv6addr_param) &&
960179157Srrs		    (memcmp(&v6addr->addr, &sin6->sin6_addr,
961179157Srrs		    sizeof(struct in6_addr)) == 0)) {
962179157Srrs			return (1);
963179157Srrs		}
964180955Srrs	}
965180955Srrs#endif
966179157Srrs	if (sa->sa_family == AF_INET) {
967179157Srrs		/* IPv4 sa address */
968179157Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
969179157Srrs
970179157Srrs		param_type = ntohs(v4addr->ph.param_type);
971179157Srrs		param_length = ntohs(v4addr->ph.param_length);
972179157Srrs
973179157Srrs		if ((param_type == SCTP_IPV4_ADDRESS) &&
974179157Srrs		    param_length == sizeof(struct sctp_ipv4addr_param) &&
975179157Srrs		    (memcmp(&v4addr->addr, &sin->sin_addr,
976179157Srrs		    sizeof(struct in_addr)) == 0)) {
977179157Srrs			return (1);
978179157Srrs		}
979179157Srrs	}
980179157Srrs	return (0);
981179157Srrs}
982179157Srrs
983179157Srrs/*
984163953Srrs * Cleanup for non-responded/OP ERR'd ASCONF
985163953Srrs */
986163953Srrsvoid
987163953Srrssctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
988163953Srrs{
989163953Srrs	/* mark peer as ASCONF incapable */
990163953Srrs	stcb->asoc.peer_supports_asconf = 0;
991163953Srrs	/*
992163953Srrs	 * clear out any existing asconfs going out
993163953Srrs	 */
994169655Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
995169655Srrs	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
996179157Srrs	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
997163953Srrs	/* remove the old ASCONF on our outbound queue */
998163953Srrs	sctp_toss_old_asconf(stcb);
999163953Srrs}
1000163953Srrs
1001163953Srrs/*
1002171858Srrs * cleanup any cached source addresses that may be topologically
1003171858Srrs * incorrect after a new address has been added to this interface.
1004171858Srrs */
1005171858Srrsstatic void
1006171858Srrssctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
1007171858Srrs{
1008171858Srrs	struct sctp_nets *net;
1009171858Srrs
1010171858Srrs	/*
1011171858Srrs	 * Ideally, we want to only clear cached routes and source addresses
1012171858Srrs	 * that are topologically incorrect.  But since there is no easy way
1013171858Srrs	 * to know whether the newly added address on the ifn would cause a
1014171858Srrs	 * routing change (i.e. a new egress interface would be chosen)
1015171858Srrs	 * without doing a new routing lookup and source address selection,
1016171858Srrs	 * we will (for now) just flush any cached route using a different
1017171858Srrs	 * ifn (and cached source addrs) and let output re-choose them
1018171858Srrs	 * during the next send on that net.
1019171858Srrs	 */
1020171858Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1021171858Srrs		/*
1022171858Srrs		 * clear any cached route (and cached source address) if the
1023171858Srrs		 * route's interface is NOT the same as the address change.
1024171858Srrs		 * If it's the same interface, just clear the cached source
1025171858Srrs		 * address.
1026171858Srrs		 */
1027171858Srrs		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
1028171858Srrs		    SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index) {
1029171858Srrs			/* clear any cached route */
1030171858Srrs			RTFREE(net->ro.ro_rt);
1031171858Srrs			net->ro.ro_rt = NULL;
1032171858Srrs		}
1033171858Srrs		/* clear any cached source address */
1034171858Srrs		if (net->src_addr_selected) {
1035171858Srrs			sctp_free_ifa(net->ro._s_addr);
1036171858Srrs			net->ro._s_addr = NULL;
1037171858Srrs			net->src_addr_selected = 0;
1038171858Srrs		}
1039171858Srrs	}
1040171858Srrs}
1041171858Srrs
1042172091Srrsvoid
1043172091Srrssctp_move_chunks_from_deleted_prim(struct sctp_tcb *stcb, struct sctp_nets *dst)
1044172091Srrs{
1045172091Srrs	struct sctp_association *asoc;
1046172091Srrs	struct sctp_stream_out *outs;
1047172091Srrs	struct sctp_tmit_chunk *chk;
1048172091Srrs	struct sctp_stream_queue_pending *sp;
1049172091Srrs
1050172091Srrs	if (dst->dest_state & SCTP_ADDR_UNCONFIRMED) {
1051172091Srrs		return;
1052172091Srrs	}
1053172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1054172091Srrs		return;
1055172091Srrs	}
1056172091Srrs	asoc = &stcb->asoc;
1057172091Srrs
1058172091Srrs	/*
1059172091Srrs	 * now through all the streams checking for chunks sent to our bad
1060172091Srrs	 * network.
1061172091Srrs	 */
1062172091Srrs	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1063172091Srrs		/* now clean up any chunks here */
1064172091Srrs		TAILQ_FOREACH(sp, &outs->outqueue, next) {
1065172091Srrs			if (sp->net == asoc->deleted_primary) {
1066172091Srrs				sctp_free_remote_addr(sp->net);
1067172091Srrs				sp->net = dst;
1068172091Srrs				atomic_add_int(&dst->ref_count, 1);
1069172091Srrs			}
1070172091Srrs		}
1071172091Srrs	}
1072172091Srrs	/* Now check the pending queue */
1073172091Srrs	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
1074172091Srrs		if (chk->whoTo == asoc->deleted_primary) {
1075172091Srrs			sctp_free_remote_addr(chk->whoTo);
1076172091Srrs			chk->whoTo = dst;
1077172091Srrs			atomic_add_int(&dst->ref_count, 1);
1078172091Srrs		}
1079172091Srrs	}
1080172091Srrs
1081172091Srrs}
1082172091Srrs
1083172091Srrs
1084172091Srrsvoid
1085172091Srrssctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
1086172091Srrs{
1087172091Srrs	int error;
1088172091Srrs
1089172118Srrs	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
1090172118Srrs		return;
1091172118Srrs	}
1092172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1093172091Srrs		return;
1094172091Srrs	}
1095172091Srrs	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
1096172157Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
1097172091Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1098172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
1099172091Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
1100172091Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1101172091Srrs		    stcb->asoc.deleted_primary,
1102172091Srrs		    SCTP_FROM_SCTP_TIMER + SCTP_LOC_8);
1103172091Srrs		stcb->asoc.num_send_timers_up--;
1104172091Srrs		if (stcb->asoc.num_send_timers_up < 0) {
1105172091Srrs			stcb->asoc.num_send_timers_up = 0;
1106172091Srrs		}
1107172091Srrs		SCTP_TCB_LOCK_ASSERT(stcb);
1108172091Srrs		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1109172091Srrs		    stcb->asoc.deleted_primary);
1110172091Srrs		if (error) {
1111172091Srrs			SCTP_INP_DECR_REF(stcb->sctp_ep);
1112172091Srrs			return;
1113172091Srrs		}
1114172091Srrs		SCTP_TCB_LOCK_ASSERT(stcb);
1115172091Srrs#ifdef SCTP_AUDITING_ENABLED
1116172091Srrs		sctp_auditing(4, stcb->sctp_ep, stcb->asoc.deleted_primary);
1117172091Srrs#endif
1118172091Srrs		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1119172091Srrs		if ((stcb->asoc.num_send_timers_up == 0) &&
1120172091Srrs		    (stcb->asoc.sent_queue_cnt > 0)) {
1121172091Srrs			struct sctp_tmit_chunk *chk;
1122172091Srrs
1123172091Srrs			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1124172091Srrs			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
1125172091Srrs			    stcb, chk->whoTo);
1126172091Srrs		}
1127172091Srrs	}
1128172091Srrs	return;
1129172091Srrs}
1130172091Srrs
1131171990Srrsstatic int
1132171990Srrs    sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
1133171990Srrs
1134172091Srrsvoid
1135171990Srrssctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
1136171990Srrs{
1137171990Srrs	struct sctp_tmit_chunk *chk;
1138171990Srrs
1139172157Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
1140171990Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
1141171990Srrs	    SCTP_FROM_SCTP_TIMER + SCTP_LOC_5);
1142171990Srrs	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
1143171990Srrs	net->error_count = 0;
1144171990Srrs	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1145171990Srrs		if (chk->whoTo == net) {
1146172091Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
1147172091Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1148172091Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1149172396Srrs				sctp_flight_size_decrease(chk);
1150172396Srrs				sctp_total_flight_decrease(stcb, chk);
1151172396Srrs				net->marked_retrans++;
1152172396Srrs				stcb->asoc.marked_retrans++;
1153172091Srrs			}
1154171990Srrs		}
1155171990Srrs	}
1156172396Srrs	if (net->marked_retrans) {
1157172396Srrs		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1158172396Srrs	}
1159171990Srrs}
1160171990Srrs
1161171990Srrsstatic void
1162171990Srrssctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
1163171990Srrs{
1164171990Srrs	struct sctp_nets *net;
1165171990Srrs	int addrnum, changed;
1166171990Srrs
1167171990Srrs	/*
1168171990Srrs	 * If number of local valid addresses is 1, the valid address is
1169171990Srrs	 * probably newly added address.  Several valid addresses in this
1170171990Srrs	 * association.  A source address may not be changed.  Additionally,
1171171990Srrs	 * they can be configured on a same interface as "alias" addresses.
1172171990Srrs	 * (by micchie)
1173171990Srrs	 */
1174171990Srrs	addrnum = sctp_local_addr_count(stcb);
1175171990Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
1176171990Srrs	    addrnum);
1177171990Srrs	if (addrnum == 1) {
1178171990Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1179171990Srrs			/* clear any cached route and source address */
1180171990Srrs			if (net->ro.ro_rt) {
1181171990Srrs				RTFREE(net->ro.ro_rt);
1182171990Srrs				net->ro.ro_rt = NULL;
1183171990Srrs			}
1184171990Srrs			if (net->src_addr_selected) {
1185171990Srrs				sctp_free_ifa(net->ro._s_addr);
1186171990Srrs				net->ro._s_addr = NULL;
1187171990Srrs				net->src_addr_selected = 0;
1188171990Srrs			}
1189171990Srrs			/* Retransmit unacknowledged DATA chunks immediately */
1190171990Srrs			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1191171990Srrs			    SCTP_MOBILITY_FASTHANDOFF)) {
1192171990Srrs				sctp_net_immediate_retrans(stcb, net);
1193171990Srrs			}
1194171990Srrs			/* also, SET PRIMARY is maybe already sent */
1195171990Srrs		}
1196171990Srrs		return;
1197171990Srrs	}
1198171990Srrs	/* Multiple local addresses exsist in the association.  */
1199171990Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1200171990Srrs		/* clear any cached route and source address */
1201171990Srrs		if (net->ro.ro_rt) {
1202171990Srrs			RTFREE(net->ro.ro_rt);
1203171990Srrs			net->ro.ro_rt = NULL;
1204171990Srrs		}
1205171990Srrs		if (net->src_addr_selected) {
1206171990Srrs			sctp_free_ifa(net->ro._s_addr);
1207171990Srrs			net->ro._s_addr = NULL;
1208171990Srrs			net->src_addr_selected = 0;
1209171990Srrs		}
1210171990Srrs		/*
1211171990Srrs		 * Check if the nexthop is corresponding to the new address.
1212171990Srrs		 * If the new address is corresponding to the current
1213171990Srrs		 * nexthop, the path will be changed.  If the new address is
1214171990Srrs		 * NOT corresponding to the current nexthop, the path will
1215171990Srrs		 * not be changed.
1216171990Srrs		 */
1217171990Srrs		SCTP_RTALLOC((sctp_route_t *) & net->ro,
1218171990Srrs		    stcb->sctp_ep->def_vrf_id);
1219171990Srrs		if (net->ro.ro_rt == NULL)
1220171990Srrs			continue;
1221171990Srrs
1222172137Srrs		changed = 0;
1223171990Srrs		if (net->ro._l_addr.sa.sa_family == AF_INET) {
1224171990Srrs			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *) & net->ro))
1225171990Srrs				changed = 1;
1226171990Srrs		}
1227178251Srrs#ifdef INET6
1228171990Srrs		if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1229171990Srrs			if (sctp_v6src_match_nexthop(
1230171990Srrs			    &newifa->address.sin6, (sctp_route_t *) & net->ro))
1231171990Srrs				changed = 1;
1232171990Srrs		}
1233178251Srrs#endif
1234171990Srrs		/*
1235171990Srrs		 * if the newly added address does not relate routing
1236171990Srrs		 * information, we skip.
1237171990Srrs		 */
1238171990Srrs		if (changed == 0)
1239171990Srrs			continue;
1240171990Srrs		/* Retransmit unacknowledged DATA chunks immediately */
1241171990Srrs		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1242171990Srrs		    SCTP_MOBILITY_FASTHANDOFF)) {
1243171990Srrs			sctp_net_immediate_retrans(stcb, net);
1244171990Srrs		}
1245171990Srrs		/* Send SET PRIMARY for this new address */
1246171990Srrs		if (net == stcb->asoc.primary_destination) {
1247171990Srrs			(void)sctp_asconf_queue_mgmt(stcb, newifa,
1248171990Srrs			    SCTP_SET_PRIM_ADDR);
1249171990Srrs		}
1250171990Srrs	}
1251171990Srrs}
1252171990Srrs
1253171858Srrs/*
1254163953Srrs * process an ADD/DELETE IP ack from peer.
1255171572Srrs * addr: corresponding sctp_ifa to the address being added/deleted.
1256163953Srrs * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1257163953Srrs * flag: 1=success, 0=failure.
1258163953Srrs */
1259163953Srrsstatic void
1260167598Srrssctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr,
1261163953Srrs    uint16_t type, uint32_t flag)
1262163953Srrs{
1263163953Srrs	/*
1264163953Srrs	 * do the necessary asoc list work- if we get a failure indication,
1265171858Srrs	 * leave the address on the assoc's restricted list.  If we get a
1266171858Srrs	 * success indication, remove the address from the restricted list.
1267163953Srrs	 */
1268163953Srrs	/*
1269163953Srrs	 * Note: this will only occur for ADD_IP_ADDRESS, since
1270163953Srrs	 * DEL_IP_ADDRESS is never actually added to the list...
1271163953Srrs	 */
1272163953Srrs	if (flag) {
1273171572Srrs		/* success case, so remove from the restricted list */
1274171572Srrs		sctp_del_local_addr_restricted(stcb, addr);
1275171858Srrs
1276172190Srrs		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1277172396Srrs		    SCTP_MOBILITY_BASE) ||
1278172396Srrs		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1279172396Srrs		    SCTP_MOBILITY_FASTHANDOFF)) {
1280171990Srrs			sctp_path_check_and_react(stcb, addr);
1281171990Srrs			return;
1282171990Srrs		}
1283172190Srrs		/* clear any cached/topologically incorrect source addresses */
1284171858Srrs		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1285163953Srrs	}
1286163953Srrs	/* else, leave it on the list */
1287163953Srrs}
1288163953Srrs
1289163953Srrs/*
1290171572Srrs * add an asconf add/delete/set primary IP address parameter to the queue.
1291163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1292171572Srrs * returns 0 if queued, -1 if not queued/removed.
1293171572Srrs * NOTE: if adding, but a delete for the same address is already scheduled
1294171572Srrs * (and not yet sent out), simply remove it from queue.  Same for deleting
1295171572Srrs * an address already scheduled for add.  If a duplicate operation is found,
1296171572Srrs * ignore the new one.
1297163953Srrs */
1298171572Srrsstatic int
1299171572Srrssctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1300171477Srrs    uint16_t type)
1301163953Srrs{
1302163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1303163953Srrs	struct sockaddr *sa;
1304163953Srrs
1305163953Srrs	/* make sure the request isn't already in the queue */
1306163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1307163953Srrs	    aa = aa_next) {
1308163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1309163953Srrs		/* address match? */
1310167598Srrs		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1311163953Srrs			continue;
1312179157Srrs		/*
1313179157Srrs		 * is the request already in queue but not sent? pass the
1314179157Srrs		 * request already sent in order to resolve the following
1315179157Srrs		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1316179157Srrs		 * can't remove the ADD request already sent 3. arrival of
1317179157Srrs		 * ADD
1318179157Srrs		 */
1319179157Srrs		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1320163953Srrs			return (-1);
1321163953Srrs		}
1322163953Srrs		/* is the negative request already in queue, and not sent */
1323171572Srrs		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
1324171572Srrs		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1325171572Srrs			/* add requested, delete already queued */
1326163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1327171572Srrs			/* remove the ifa from the restricted list */
1328171572Srrs			sctp_del_local_addr_restricted(stcb, ifa);
1329171572Srrs			/* free the asconf param */
1330170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1331171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
1332163953Srrs			return (-1);
1333163953Srrs		}
1334171572Srrs		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
1335171572Srrs		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
1336171572Srrs			/* delete requested, add already queued */
1337171572Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1338171572Srrs			/* remove the aa->ifa from the restricted list */
1339171572Srrs			sctp_del_local_addr_restricted(stcb, aa->ifa);
1340171572Srrs			/* free the asconf param */
1341171572Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1342171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1343171572Srrs			return (-1);
1344171572Srrs		}
1345163953Srrs	}			/* for each aa */
1346163953Srrs
1347163953Srrs	/* adding new request to the queue */
1348171572Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1349171572Srrs	    SCTP_M_ASC_ADDR);
1350163953Srrs	if (aa == NULL) {
1351163953Srrs		/* didn't get memory */
1352171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1353163953Srrs		return (-1);
1354163953Srrs	}
1355185694Srrs	aa->special_del = 0;
1356163953Srrs	/* fill in asconf address parameter fields */
1357163953Srrs	/* top level elements are "networked" during send */
1358163953Srrs	aa->ap.aph.ph.param_type = type;
1359163953Srrs	aa->ifa = ifa;
1360168299Srrs	atomic_add_int(&ifa->refcount, 1);
1361163953Srrs	/* correlation_id filled in during send routine later... */
1362167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1363163953Srrs		/* IPv6 address */
1364163953Srrs		struct sockaddr_in6 *sin6;
1365163953Srrs
1366167598Srrs		sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
1367163953Srrs		sa = (struct sockaddr *)sin6;
1368163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1369163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1370171572Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1371163953Srrs		    sizeof(struct sctp_ipv6addr_param);
1372163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1373163953Srrs		    sizeof(struct in6_addr));
1374167598Srrs	} else if (ifa->address.sa.sa_family == AF_INET) {
1375163953Srrs		/* IPv4 address */
1376171572Srrs		struct sockaddr_in *sin;
1377163953Srrs
1378171572Srrs		sin = (struct sockaddr_in *)&ifa->address.sa;
1379163953Srrs		sa = (struct sockaddr *)sin;
1380163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1381163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1382171572Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1383163953Srrs		    sizeof(struct sctp_ipv4addr_param);
1384163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1385163953Srrs		    sizeof(struct in_addr));
1386163953Srrs	} else {
1387163953Srrs		/* invalid family! */
1388170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1389172190Srrs		sctp_free_ifa(ifa);
1390163953Srrs		return (-1);
1391163953Srrs	}
1392163953Srrs	aa->sent = 0;		/* clear sent flag */
1393163953Srrs
1394179157Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1395163953Srrs#ifdef SCTP_DEBUG
1396179783Srrs	if (SCTP_BASE_SYSCTL(sctp_debug_on) && SCTP_DEBUG_ASCONF2) {
1397179157Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
1398179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1399179157Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1400179157Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
1401179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1402179157Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1403179157Srrs		} else {
1404179157Srrs			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1405179157Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1406163953Srrs		}
1407179157Srrs	}
1408169420Srrs#endif
1409163953Srrs
1410163953Srrs	return (0);
1411163953Srrs}
1412163953Srrs
1413171572Srrs
1414163953Srrs/*
1415171572Srrs * add an asconf operation for the given ifa and type.
1416171572Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1417171572Srrs * returns 0 if completed, -1 if not completed, 1 if immediate send is
1418171572Srrs * advisable.
1419171572Srrs */
1420171572Srrsstatic int
1421171572Srrssctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1422171572Srrs    uint16_t type)
1423171572Srrs{
1424171572Srrs	uint32_t status;
1425171572Srrs	int pending_delete_queued = 0;
1426171572Srrs
1427171572Srrs	/* see if peer supports ASCONF */
1428171572Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1429171572Srrs		return (-1);
1430171572Srrs	}
1431171572Srrs	/*
1432171572Srrs	 * if this is deleting the last address from the assoc, mark it as
1433171572Srrs	 * pending.
1434171572Srrs	 */
1435171572Srrs	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending &&
1436171572Srrs	    (sctp_local_addr_count(stcb) < 2)) {
1437171572Srrs		/* set the pending delete info only */
1438171572Srrs		stcb->asoc.asconf_del_pending = 1;
1439171572Srrs		stcb->asoc.asconf_addr_del_pending = ifa;
1440171572Srrs		atomic_add_int(&ifa->refcount, 1);
1441171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF2,
1442171572Srrs		    "asconf_queue_add: mark delete last address pending\n");
1443171572Srrs		return (-1);
1444171572Srrs	}
1445179157Srrs	/* queue an asconf parameter */
1446179157Srrs	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1447179157Srrs
1448171572Srrs	/*
1449171572Srrs	 * if this is an add, and there is a delete also pending (i.e. the
1450171572Srrs	 * last local address is being changed), queue the pending delete
1451171572Srrs	 * too.
1452171572Srrs	 */
1453179157Srrs	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
1454171572Srrs		/* queue in the pending delete */
1455171572Srrs		if (sctp_asconf_queue_mgmt(stcb,
1456171572Srrs		    stcb->asoc.asconf_addr_del_pending,
1457171572Srrs		    SCTP_DEL_IP_ADDRESS) == 0) {
1458171572Srrs			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queing pending delete\n");
1459171572Srrs			pending_delete_queued = 1;
1460171572Srrs			/* clear out the pending delete info */
1461171572Srrs			stcb->asoc.asconf_del_pending = 0;
1462171572Srrs			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
1463171572Srrs			stcb->asoc.asconf_addr_del_pending = NULL;
1464171572Srrs		}
1465171572Srrs	}
1466179157Srrs	if (pending_delete_queued) {
1467171572Srrs		struct sctp_nets *net;
1468171572Srrs
1469171572Srrs		/*
1470171572Srrs		 * since we know that the only/last address is now being
1471171572Srrs		 * changed in this case, reset the cwnd/rto on all nets to
1472171572Srrs		 * start as a new address and path.  Also clear the error
1473171572Srrs		 * counts to give the assoc the best chance to complete the
1474171572Srrs		 * address change.
1475171572Srrs		 */
1476171572Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1477171572Srrs			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
1478171572Srrs			    net);
1479171572Srrs			net->RTO = 0;
1480171572Srrs			net->error_count = 0;
1481171572Srrs		}
1482171572Srrs		stcb->asoc.overall_error_count = 0;
1483179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1484171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1485171943Srrs			    stcb->asoc.overall_error_count,
1486171943Srrs			    0,
1487171943Srrs			    SCTP_FROM_SCTP_ASCONF,
1488171943Srrs			    __LINE__);
1489171943Srrs		}
1490171572Srrs		/* queue in an advisory set primary too */
1491171572Srrs		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
1492171572Srrs		/* let caller know we should send this out immediately */
1493171572Srrs		status = 1;
1494171572Srrs	}
1495171572Srrs	return (status);
1496171572Srrs}
1497171572Srrs
1498172190Srrs/*-
1499172190Srrs * add an asconf delete IP address parameter to the queue by sockaddr and
1500172190Srrs * possibly with no sctp_ifa available.  This is only called by the routine
1501172190Srrs * that checks the addresses in an INIT-ACK against the current address list.
1502163953Srrs * returns 0 if completed, non-zero if not completed.
1503172190Srrs * NOTE: if an add is already scheduled (and not yet sent out), simply
1504172190Srrs * remove it from queue.  If a duplicate operation is found, ignore the
1505172190Srrs * new one.
1506163953Srrs */
1507171572Srrsstatic int
1508172190Srrssctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1509163953Srrs{
1510168299Srrs	struct sctp_ifa *ifa;
1511163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1512167598Srrs	uint32_t vrf_id;
1513163953Srrs
1514169420Srrs	if (stcb == NULL) {
1515169420Srrs		return (-1);
1516169420Srrs	}
1517163953Srrs	/* see if peer supports ASCONF */
1518163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1519163953Srrs		return (-1);
1520163953Srrs	}
1521163953Srrs	/* make sure the request isn't already in the queue */
1522163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1523163953Srrs	    aa = aa_next) {
1524163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1525163953Srrs		/* address match? */
1526163953Srrs		if (sctp_asconf_addr_match(aa, sa) == 0)
1527163953Srrs			continue;
1528163953Srrs		/* is the request already in queue (sent or not) */
1529172190Srrs		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1530163953Srrs			return (-1);
1531163953Srrs		}
1532163953Srrs		/* is the negative request already in queue, and not sent */
1533163953Srrs		if (aa->sent == 1)
1534163953Srrs			continue;
1535172190Srrs		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1536172190Srrs			/* add already queued, so remove existing entry */
1537163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1538171572Srrs			sctp_del_local_addr_restricted(stcb, aa->ifa);
1539163953Srrs			/* free the entry */
1540170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1541163953Srrs			return (-1);
1542163953Srrs		}
1543163953Srrs	}			/* for each aa */
1544172190Srrs
1545172190Srrs	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1546168299Srrs	if (stcb) {
1547168299Srrs		vrf_id = stcb->asoc.vrf_id;
1548168299Srrs	} else {
1549168299Srrs		vrf_id = SCTP_DEFAULT_VRFID;
1550168299Srrs	}
1551172190Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
1552163953Srrs
1553163953Srrs	/* adding new request to the queue */
1554171572Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1555171572Srrs	    SCTP_M_ASC_ADDR);
1556163953Srrs	if (aa == NULL) {
1557163953Srrs		/* didn't get memory */
1558169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1559172190Srrs		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1560163953Srrs		return (-1);
1561163953Srrs	}
1562185694Srrs	aa->special_del = 0;
1563163953Srrs	/* fill in asconf address parameter fields */
1564163953Srrs	/* top level elements are "networked" during send */
1565172190Srrs	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1566168299Srrs	aa->ifa = ifa;
1567172190Srrs	if (ifa)
1568172190Srrs		atomic_add_int(&ifa->refcount, 1);
1569163953Srrs	/* correlation_id filled in during send routine later... */
1570163953Srrs	if (sa->sa_family == AF_INET6) {
1571163953Srrs		/* IPv6 address */
1572163953Srrs		struct sockaddr_in6 *sin6;
1573163953Srrs
1574163953Srrs		sin6 = (struct sockaddr_in6 *)sa;
1575163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1576163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1577163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1578163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1579163953Srrs		    sizeof(struct in6_addr));
1580163953Srrs	} else if (sa->sa_family == AF_INET) {
1581163953Srrs		/* IPv4 address */
1582163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1583163953Srrs
1584163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1585163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1586163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1587163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1588163953Srrs		    sizeof(struct in_addr));
1589163953Srrs	} else {
1590163953Srrs		/* invalid family! */
1591170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1592172190Srrs		if (ifa)
1593172190Srrs			sctp_free_ifa(ifa);
1594163953Srrs		return (-1);
1595163953Srrs	}
1596163953Srrs	aa->sent = 0;		/* clear sent flag */
1597163953Srrs
1598172190Srrs	/* delete goes to the back of the queue */
1599172190Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1600163953Srrs
1601172218Srrs	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1602163953Srrs	return (0);
1603163953Srrs}
1604163953Srrs
1605163953Srrs/*
1606163953Srrs * find a specific asconf param on our "sent" queue
1607163953Srrs */
1608163953Srrsstatic struct sctp_asconf_addr *
1609163953Srrssctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1610163953Srrs{
1611163953Srrs	struct sctp_asconf_addr *aa;
1612163953Srrs
1613163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1614163953Srrs		if (aa->ap.aph.correlation_id == correlation_id &&
1615163953Srrs		    aa->sent == 1) {
1616163953Srrs			/* found it */
1617163953Srrs			return (aa);
1618163953Srrs		}
1619163953Srrs	}
1620163953Srrs	/* didn't find it */
1621163953Srrs	return (NULL);
1622163953Srrs}
1623163953Srrs
1624163953Srrs/*
1625163953Srrs * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1626163953Srrs * notifications based on the error response
1627163953Srrs */
1628163953Srrsstatic void
1629163953Srrssctp_asconf_process_error(struct sctp_tcb *stcb,
1630163953Srrs    struct sctp_asconf_paramhdr *aph)
1631163953Srrs{
1632163953Srrs	struct sctp_error_cause *eh;
1633163953Srrs	struct sctp_paramhdr *ph;
1634163953Srrs	uint16_t param_type;
1635163953Srrs	uint16_t error_code;
1636163953Srrs
1637163953Srrs	eh = (struct sctp_error_cause *)(aph + 1);
1638163953Srrs	ph = (struct sctp_paramhdr *)(eh + 1);
1639163953Srrs	/* validate lengths */
1640163953Srrs	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1641163953Srrs	    htons(aph->ph.param_length)) {
1642163953Srrs		/* invalid error cause length */
1643169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1644169420Srrs		    "asconf_process_error: cause element too long\n");
1645163953Srrs		return;
1646163953Srrs	}
1647163953Srrs	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1648163953Srrs	    htons(eh->length)) {
1649163953Srrs		/* invalid included TLV length */
1650169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1651169420Srrs		    "asconf_process_error: included TLV too long\n");
1652163953Srrs		return;
1653163953Srrs	}
1654163953Srrs	/* which error code ? */
1655163953Srrs	error_code = ntohs(eh->code);
1656163953Srrs	param_type = ntohs(aph->ph.param_type);
1657163953Srrs	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1658163953Srrs	switch (error_code) {
1659163953Srrs	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1660163953Srrs		/* we allow ourselves to "try again" for this error */
1661163953Srrs		break;
1662163953Srrs	default:
1663163953Srrs		/* peer can't handle it... */
1664163953Srrs		switch (param_type) {
1665163953Srrs		case SCTP_ADD_IP_ADDRESS:
1666163953Srrs		case SCTP_DEL_IP_ADDRESS:
1667163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1668163953Srrs			break;
1669163953Srrs		case SCTP_SET_PRIM_ADDR:
1670163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1671163953Srrs			break;
1672163953Srrs		default:
1673163953Srrs			break;
1674163953Srrs		}
1675163953Srrs	}
1676163953Srrs}
1677163953Srrs
1678163953Srrs/*
1679171477Srrs * process an asconf queue param.
1680171477Srrs * aparam: parameter to process, will be removed from the queue.
1681171477Srrs * flag: 1=success case, 0=failure case
1682163953Srrs */
1683163953Srrsstatic void
1684163953Srrssctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1685163953Srrs    struct sctp_asconf_addr *aparam, uint32_t flag)
1686163953Srrs{
1687163953Srrs	uint16_t param_type;
1688163953Srrs
1689163953Srrs	/* process this param */
1690163953Srrs	param_type = aparam->ap.aph.ph.param_type;
1691163953Srrs	switch (param_type) {
1692163953Srrs	case SCTP_ADD_IP_ADDRESS:
1693169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1694169420Srrs		    "process_param_ack: added IP address\n");
1695163953Srrs		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1696163953Srrs		break;
1697163953Srrs	case SCTP_DEL_IP_ADDRESS:
1698169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1699169420Srrs		    "process_param_ack: deleted IP address\n");
1700163953Srrs		/* nothing really to do... lists already updated */
1701163953Srrs		break;
1702163953Srrs	case SCTP_SET_PRIM_ADDR:
1703179157Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1704179157Srrs		    "process_param_ack: set primary IP address\n");
1705163953Srrs		/* nothing to do... peer may start using this addr */
1706163953Srrs		if (flag == 0)
1707163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1708163953Srrs		break;
1709163953Srrs	default:
1710163953Srrs		/* should NEVER happen */
1711163953Srrs		break;
1712163953Srrs	}
1713163953Srrs
1714163953Srrs	/* remove the param and free it */
1715163953Srrs	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1716172190Srrs	if (aparam->ifa)
1717172190Srrs		sctp_free_ifa(aparam->ifa);
1718170091Srrs	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1719163953Srrs}
1720163953Srrs
1721163953Srrs/*
1722163953Srrs * cleanup from a bad asconf ack parameter
1723163953Srrs */
1724163953Srrsstatic void
1725163953Srrssctp_asconf_ack_clear(struct sctp_tcb *stcb)
1726163953Srrs{
1727163953Srrs	/* assume peer doesn't really know how to do asconfs */
1728163953Srrs	stcb->asoc.peer_supports_asconf = 0;
1729163953Srrs	/* XXX we could free the pending queue here */
1730163953Srrs}
1731163953Srrs
1732163953Srrsvoid
1733163953Srrssctp_handle_asconf_ack(struct mbuf *m, int offset,
1734163953Srrs    struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1735172190Srrs    struct sctp_nets *net, int *abort_no_unlock)
1736163953Srrs{
1737163953Srrs	struct sctp_association *asoc;
1738163953Srrs	uint32_t serial_num;
1739163953Srrs	uint16_t ack_length;
1740163953Srrs	struct sctp_asconf_paramhdr *aph;
1741163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1742163953Srrs	uint32_t last_error_id = 0;	/* last error correlation id */
1743163953Srrs	uint32_t id;
1744163953Srrs	struct sctp_asconf_addr *ap;
1745163953Srrs
1746163953Srrs	/* asconf param buffer */
1747166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1748163953Srrs
1749163953Srrs	/* verify minimum length */
1750163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1751169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1752169420Srrs		    "handle_asconf_ack: chunk too small = %xh\n",
1753169420Srrs		    ntohs(cp->ch.chunk_length));
1754163953Srrs		return;
1755163953Srrs	}
1756163953Srrs	asoc = &stcb->asoc;
1757163953Srrs	serial_num = ntohl(cp->serial_number);
1758163953Srrs
1759163953Srrs	/*
1760163953Srrs	 * NOTE: we may want to handle this differently- currently, we will
1761163953Srrs	 * abort when we get an ack for the expected serial number + 1 (eg.
1762163953Srrs	 * we didn't send it), process an ack normally if it is the expected
1763163953Srrs	 * serial number, and re-send the previous ack for *ALL* other
1764163953Srrs	 * serial numbers
1765163953Srrs	 */
1766163953Srrs
1767163953Srrs	/*
1768163953Srrs	 * if the serial number is the next expected, but I didn't send it,
1769163953Srrs	 * abort the asoc, since someone probably just hijacked us...
1770163953Srrs	 */
1771163953Srrs	if (serial_num == (asoc->asconf_seq_out + 1)) {
1772169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1773163953Srrs		sctp_abort_an_association(stcb->sctp_ep, stcb,
1774172090Srrs		    SCTP_CAUSE_ILLEGAL_ASCONF_ACK, NULL, SCTP_SO_NOT_LOCKED);
1775172190Srrs		*abort_no_unlock = 1;
1776163953Srrs		return;
1777163953Srrs	}
1778179157Srrs	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1779163953Srrs		/* got a duplicate/unexpected ASCONF-ACK */
1780169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1781179157Srrs		    serial_num, asoc->asconf_seq_out_acked + 1);
1782163953Srrs		return;
1783163953Srrs	}
1784179157Srrs	if (serial_num == asoc->asconf_seq_out - 1) {
1785179157Srrs		/* stop our timer */
1786179157Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
1787179157Srrs		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1788163953Srrs	}
1789163953Srrs	/* process the ASCONF-ACK contents */
1790163953Srrs	ack_length = ntohs(cp->ch.chunk_length) -
1791163953Srrs	    sizeof(struct sctp_asconf_ack_chunk);
1792163953Srrs	offset += sizeof(struct sctp_asconf_ack_chunk);
1793163953Srrs	/* process through all parameters */
1794163953Srrs	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1795163953Srrs		unsigned int param_length, param_type;
1796163953Srrs
1797163953Srrs		/* get pointer to next asconf parameter */
1798163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1799163953Srrs		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1800163953Srrs		if (aph == NULL) {
1801163953Srrs			/* can't get an asconf paramhdr */
1802163953Srrs			sctp_asconf_ack_clear(stcb);
1803163953Srrs			return;
1804163953Srrs		}
1805163953Srrs		param_type = ntohs(aph->ph.param_type);
1806163953Srrs		param_length = ntohs(aph->ph.param_length);
1807163953Srrs		if (param_length > ack_length) {
1808163953Srrs			sctp_asconf_ack_clear(stcb);
1809163953Srrs			return;
1810163953Srrs		}
1811163953Srrs		if (param_length < sizeof(struct sctp_paramhdr)) {
1812163953Srrs			sctp_asconf_ack_clear(stcb);
1813163953Srrs			return;
1814163953Srrs		}
1815163953Srrs		/* get the complete parameter... */
1816163953Srrs		if (param_length > sizeof(aparam_buf)) {
1817169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
1818169420Srrs			    "param length (%u) larger than buffer size!\n", param_length);
1819163953Srrs			sctp_asconf_ack_clear(stcb);
1820163953Srrs			return;
1821163953Srrs		}
1822163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1823163953Srrs		if (aph == NULL) {
1824163953Srrs			sctp_asconf_ack_clear(stcb);
1825163953Srrs			return;
1826163953Srrs		}
1827163953Srrs		/* correlation_id is transparent to peer, no ntohl needed */
1828163953Srrs		id = aph->correlation_id;
1829163953Srrs
1830163953Srrs		switch (param_type) {
1831163953Srrs		case SCTP_ERROR_CAUSE_IND:
1832163953Srrs			last_error_id = id;
1833163953Srrs			/* find the corresponding asconf param in our queue */
1834163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1835163953Srrs			if (ap == NULL) {
1836163953Srrs				/* hmm... can't find this in our queue! */
1837163953Srrs				break;
1838163953Srrs			}
1839163953Srrs			/* process the parameter, failed flag */
1840163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 0);
1841163953Srrs			/* process the error response */
1842163953Srrs			sctp_asconf_process_error(stcb, aph);
1843163953Srrs			break;
1844163953Srrs		case SCTP_SUCCESS_REPORT:
1845163953Srrs			/* find the corresponding asconf param in our queue */
1846163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1847163953Srrs			if (ap == NULL) {
1848163953Srrs				/* hmm... can't find this in our queue! */
1849163953Srrs				break;
1850163953Srrs			}
1851163953Srrs			/* process the parameter, success flag */
1852163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 1);
1853163953Srrs			break;
1854163953Srrs		default:
1855163953Srrs			break;
1856163953Srrs		}		/* switch */
1857163953Srrs
1858163953Srrs		/* update remaining ASCONF-ACK message length to process */
1859163953Srrs		ack_length -= SCTP_SIZE32(param_length);
1860163953Srrs		if (ack_length <= 0) {
1861163953Srrs			/* no more data in the mbuf chain */
1862163953Srrs			break;
1863163953Srrs		}
1864163953Srrs		offset += SCTP_SIZE32(param_length);
1865163953Srrs	}			/* while */
1866163953Srrs
1867163953Srrs	/*
1868163953Srrs	 * if there are any "sent" params still on the queue, these are
1869163953Srrs	 * implicitly "success", or "failed" (if we got an error back) ...
1870163953Srrs	 * so process these appropriately
1871163953Srrs	 *
1872163953Srrs	 * we assume that the correlation_id's are monotonically increasing
1873163953Srrs	 * beginning from 1 and that we don't have *that* many outstanding
1874163953Srrs	 * at any given time
1875163953Srrs	 */
1876163953Srrs	if (last_error_id == 0)
1877163953Srrs		last_error_id--;/* set to "max" value */
1878163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1879163953Srrs	    aa = aa_next) {
1880163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1881163953Srrs		if (aa->sent == 1) {
1882163953Srrs			/*
1883163953Srrs			 * implicitly successful or failed if correlation_id
1884163953Srrs			 * < last_error_id, then success else, failure
1885163953Srrs			 */
1886163953Srrs			if (aa->ap.aph.correlation_id < last_error_id)
1887171477Srrs				sctp_asconf_process_param_ack(stcb, aa, 1);
1888163953Srrs			else
1889171477Srrs				sctp_asconf_process_param_ack(stcb, aa, 0);
1890163953Srrs		} else {
1891163953Srrs			/*
1892163953Srrs			 * since we always process in order (FIFO queue) if
1893163953Srrs			 * we reach one that hasn't been sent, the rest
1894163953Srrs			 * should not have been sent either. so, we're
1895163953Srrs			 * done...
1896163953Srrs			 */
1897163953Srrs			break;
1898163953Srrs		}
1899163953Srrs	}
1900163953Srrs
1901163953Srrs	/* update the next sequence number to use */
1902179157Srrs	asoc->asconf_seq_out_acked++;
1903163953Srrs	/* remove the old ASCONF on our outbound queue */
1904163953Srrs	sctp_toss_old_asconf(stcb);
1905163953Srrs	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1906171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
1907163953Srrs		/* we have more params, so restart our timer */
1908163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1909163953Srrs		    stcb, net);
1910171572Srrs#else
1911171572Srrs		/* we have more params, so send out more */
1912172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1913171572Srrs#endif
1914163953Srrs	}
1915163953Srrs}
1916163953Srrs
1917178251Srrs#ifdef INET6
1918163953Srrsstatic uint32_t
1919163953Srrssctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1920163953Srrs{
1921163953Srrs	struct sockaddr_in6 *sin6, *net6;
1922163953Srrs	struct sctp_nets *net;
1923163953Srrs
1924163953Srrs	if (sa->sa_family != AF_INET6) {
1925163953Srrs		/* wrong family */
1926163953Srrs		return (0);
1927163953Srrs	}
1928163953Srrs	sin6 = (struct sockaddr_in6 *)sa;
1929163953Srrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1930163953Srrs		/* not link local address */
1931163953Srrs		return (0);
1932163953Srrs	}
1933163953Srrs	/* hunt through our destination nets list for this scope_id */
1934163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1935163953Srrs		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1936163953Srrs		    AF_INET6)
1937163953Srrs			continue;
1938163953Srrs		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1939163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1940163953Srrs			continue;
1941163953Srrs		if (sctp_is_same_scope(sin6, net6)) {
1942163953Srrs			/* found one */
1943163953Srrs			return (1);
1944163953Srrs		}
1945163953Srrs	}
1946163953Srrs	/* didn't find one */
1947163953Srrs	return (0);
1948163953Srrs}
1949163953Srrs
1950178251Srrs#endif
1951178251Srrs
1952163953Srrs/*
1953163953Srrs * address management functions
1954163953Srrs */
1955163953Srrsstatic void
1956163953Srrssctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1957172190Srrs    struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1958163953Srrs{
1959163953Srrs	int status;
1960163953Srrs
1961163953Srrs
1962163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1963163953Srrs	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1964163953Srrs		/* subset bound, no ASCONF allowed case, so ignore */
1965163953Srrs		return;
1966163953Srrs	}
1967163953Srrs	/*
1968163953Srrs	 * note: we know this is not the subset bound, no ASCONF case eg.
1969163953Srrs	 * this is boundall or subset bound w/ASCONF allowed
1970163953Srrs	 */
1971163953Srrs
1972163953Srrs	/* first, make sure it's a good address family */
1973167598Srrs	if (ifa->address.sa.sa_family != AF_INET6 &&
1974167598Srrs	    ifa->address.sa.sa_family != AF_INET) {
1975163953Srrs		return;
1976163953Srrs	}
1977163953Srrs	/* make sure we're "allowed" to add this type of addr */
1978167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1979163953Srrs		/* invalid if we're not a v6 endpoint */
1980163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1981163953Srrs			return;
1982163953Srrs		/* is the v6 addr really valid ? */
1983167598Srrs		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1984163953Srrs			return;
1985163953Srrs		}
1986163953Srrs	}
1987163953Srrs	/* put this address on the "pending/do not use yet" list */
1988171572Srrs	sctp_add_local_addr_restricted(stcb, ifa);
1989163953Srrs	/*
1990163953Srrs	 * check address scope if address is out of scope, don't queue
1991163953Srrs	 * anything... note: this would leave the address on both inp and
1992163953Srrs	 * asoc lists
1993163953Srrs	 */
1994178251Srrs	switch (ifa->address.sa.sa_family) {
1995178251Srrs#ifdef INET6
1996178251Srrs	case AF_INET6:
1997178251Srrs		{
1998178251Srrs			struct sockaddr_in6 *sin6;
1999163953Srrs
2000178251Srrs			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2001178251Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2002178251Srrs				/* we skip unspecifed addresses */
2003163953Srrs				return;
2004163953Srrs			}
2005178251Srrs			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2006178251Srrs				if (stcb->asoc.local_scope == 0) {
2007178251Srrs					return;
2008178251Srrs				}
2009178251Srrs				/* is it the right link local scope? */
2010178251Srrs				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2011178251Srrs					return;
2012178251Srrs				}
2013178251Srrs			}
2014178251Srrs			if (stcb->asoc.site_scope == 0 &&
2015178251Srrs			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2016163953Srrs				return;
2017163953Srrs			}
2018178251Srrs			break;
2019163953Srrs		}
2020178251Srrs#endif
2021178251Srrs	case AF_INET:
2022178251Srrs		{
2023178251Srrs			struct sockaddr_in *sin;
2024178251Srrs			struct in6pcb *inp6;
2025163953Srrs
2026178251Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2027178251Srrs			/* invalid if we are a v6 only endpoint */
2028178251Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2029178251Srrs			    SCTP_IPV6_V6ONLY(inp6))
2030178251Srrs				return;
2031163953Srrs
2032178251Srrs			sin = (struct sockaddr_in *)&ifa->address.sa;
2033178251Srrs			if (sin->sin_addr.s_addr == 0) {
2034178251Srrs				/* we skip unspecifed addresses */
2035178251Srrs				return;
2036178251Srrs			}
2037178251Srrs			if (stcb->asoc.ipv4_local_scope == 0 &&
2038178251Srrs			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2039178251Srrs				return;
2040178251Srrs			}
2041178251Srrs			break;
2042163953Srrs		}
2043178251Srrs	default:
2044163953Srrs		/* else, not AF_INET or AF_INET6, so skip */
2045163953Srrs		return;
2046163953Srrs	}
2047163953Srrs
2048163953Srrs	/* queue an asconf for this address add/delete */
2049163953Srrs	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
2050163953Srrs		/* does the peer do asconf? */
2051163953Srrs		if (stcb->asoc.peer_supports_asconf) {
2052163953Srrs			/* queue an asconf for this addr */
2053163953Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
2054171572Srrs
2055163953Srrs			/*
2056171572Srrs			 * if queued ok, and in the open state, send out the
2057171572Srrs			 * ASCONF.  If in the non-open state, these will be
2058171572Srrs			 * sent when the state goes open.
2059163953Srrs			 */
2060163953Srrs			if (status == 0 &&
2061163953Srrs			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2062171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2063163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
2064163953Srrs				    stcb, stcb->asoc.primary_destination);
2065171572Srrs#else
2066172190Srrs				sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2067172190Srrs				    addr_locked);
2068171572Srrs#endif
2069163953Srrs			}
2070163953Srrs		}
2071163953Srrs	}
2072163953Srrs}
2073163953Srrs
2074167598Srrs
2075167598Srrsint
2076171572Srrssctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val)
2077163953Srrs{
2078167598Srrs	struct sctp_asconf_iterator *asc;
2079167598Srrs	struct sctp_ifa *ifa;
2080167598Srrs	struct sctp_laddr *l;
2081167598Srrs	int type;
2082167598Srrs	int cnt_invalid = 0;
2083163953Srrs
2084167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2085167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2086167598Srrs		ifa = l->ifa;
2087167598Srrs		type = l->action;
2088167598Srrs		if (ifa->address.sa.sa_family == AF_INET6) {
2089167598Srrs			/* invalid if we're not a v6 endpoint */
2090167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2091167598Srrs				cnt_invalid++;
2092167598Srrs				if (asc->cnt == cnt_invalid)
2093167598Srrs					return (1);
2094167598Srrs				else
2095167598Srrs					continue;
2096167598Srrs			}
2097167598Srrs		} else if (ifa->address.sa.sa_family == AF_INET) {
2098167598Srrs			/* invalid if we are a v6 only endpoint */
2099167598Srrs			struct in6pcb *inp6;
2100163953Srrs
2101167598Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2102167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2103167598Srrs			    SCTP_IPV6_V6ONLY(inp6)) {
2104167598Srrs				cnt_invalid++;
2105167598Srrs				if (asc->cnt == cnt_invalid)
2106167598Srrs					return (1);
2107167598Srrs				else
2108167598Srrs					continue;
2109163953Srrs			}
2110163953Srrs		} else {
2111167598Srrs			/* invalid address family */
2112167598Srrs			cnt_invalid++;
2113167598Srrs			if (asc->cnt == cnt_invalid)
2114167598Srrs				return (1);
2115167598Srrs			else
2116167598Srrs				continue;
2117163953Srrs		}
2118163953Srrs	}
2119167598Srrs	return (0);
2120163953Srrs}
2121163953Srrs
2122171572Srrsstatic int
2123171572Srrssctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val)
2124163953Srrs{
2125167598Srrs	struct sctp_ifa *ifa;
2126167598Srrs	struct sctp_asconf_iterator *asc;
2127167598Srrs	struct sctp_laddr *laddr, *nladdr, *l;
2128163953Srrs
2129167598Srrs	/* Only for specific case not bound all */
2130167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2131167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2132167598Srrs		ifa = l->ifa;
2133167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
2134169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2135169655Srrs			    sctp_nxt_addr) {
2136167598Srrs				if (laddr->ifa == ifa) {
2137167598Srrs					laddr->action = 0;
2138167598Srrs					break;
2139167598Srrs				}
2140167598Srrs			}
2141167598Srrs		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
2142167598Srrs			laddr = LIST_FIRST(&inp->sctp_addr_list);
2143167598Srrs			while (laddr) {
2144167598Srrs				nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2145167598Srrs				/* remove only after all guys are done */
2146167598Srrs				if (laddr->ifa == ifa) {
2147167598Srrs					sctp_del_local_addr_ep(inp, ifa);
2148167598Srrs				}
2149167598Srrs				laddr = nladdr;
2150167598Srrs			}
2151167598Srrs		}
2152163953Srrs	}
2153167598Srrs	return (0);
2154163953Srrs}
2155163953Srrs
2156167598Srrsvoid
2157171572Srrssctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2158171572Srrs    void *ptr, uint32_t val)
2159163953Srrs{
2160167598Srrs	struct sctp_asconf_iterator *asc;
2161167598Srrs	struct sctp_ifa *ifa;
2162167598Srrs	struct sctp_laddr *l;
2163167598Srrs	int cnt_invalid = 0;
2164167598Srrs	int type, status;
2165171572Srrs	int num_queued = 0;
2166163953Srrs
2167167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2168167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2169167598Srrs		ifa = l->ifa;
2170167598Srrs		type = l->action;
2171170751Srrs
2172170751Srrs		/* address's vrf_id must be the vrf_id of the assoc */
2173170751Srrs		if (ifa->vrf_id != stcb->asoc.vrf_id) {
2174170751Srrs			continue;
2175170751Srrs		}
2176167598Srrs		/* Same checks again for assoc */
2177178251Srrs		switch (ifa->address.sa.sa_family) {
2178178251Srrs#ifdef INET6
2179178251Srrs		case AF_INET6:
2180178251Srrs			{
2181178251Srrs				/* invalid if we're not a v6 endpoint */
2182178251Srrs				struct sockaddr_in6 *sin6;
2183163953Srrs
2184178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2185178251Srrs					cnt_invalid++;
2186178251Srrs					if (asc->cnt == cnt_invalid)
2187178251Srrs						return;
2188178251Srrs					else
2189178251Srrs						continue;
2190167598Srrs				}
2191178251Srrs				sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2192178251Srrs				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2193178251Srrs					/* we skip unspecifed addresses */
2194167598Srrs					continue;
2195167598Srrs				}
2196178251Srrs				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2197178251Srrs					if (stcb->asoc.local_scope == 0) {
2198178251Srrs						continue;
2199178251Srrs					}
2200178251Srrs					/* is it the right link local scope? */
2201178251Srrs					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2202178251Srrs						continue;
2203178251Srrs					}
2204178251Srrs				}
2205178251Srrs				break;
2206167598Srrs			}
2207178251Srrs#endif
2208178251Srrs		case AF_INET:
2209178251Srrs			{
2210178251Srrs				/* invalid if we are a v6 only endpoint */
2211178251Srrs				struct in6pcb *inp6;
2212178251Srrs				struct sockaddr_in *sin;
2213163953Srrs
2214178251Srrs				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2215178251Srrs				/* invalid if we are a v6 only endpoint */
2216178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2217178251Srrs				    SCTP_IPV6_V6ONLY(inp6))
2218178251Srrs					continue;
2219167598Srrs
2220178251Srrs				sin = (struct sockaddr_in *)&ifa->address.sa;
2221178251Srrs				if (sin->sin_addr.s_addr == 0) {
2222178251Srrs					/* we skip unspecifed addresses */
2223167598Srrs					continue;
2224178251Srrs				}
2225178251Srrs				if (stcb->asoc.ipv4_local_scope == 0 &&
2226178251Srrs				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2227178251Srrs					continue;;
2228178251Srrs				}
2229178251Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2230178251Srrs				    SCTP_IPV6_V6ONLY(inp6)) {
2231178251Srrs					cnt_invalid++;
2232178251Srrs					if (asc->cnt == cnt_invalid)
2233178251Srrs						return;
2234178251Srrs					else
2235178251Srrs						continue;
2236178251Srrs				}
2237178251Srrs				break;
2238167598Srrs			}
2239178251Srrs		default:
2240167598Srrs			/* invalid address family */
2241167598Srrs			cnt_invalid++;
2242167598Srrs			if (asc->cnt == cnt_invalid)
2243163953Srrs				return;
2244167598Srrs			else
2245167598Srrs				continue;
2246178251Srrs			break;
2247163953Srrs		}
2248163953Srrs
2249167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
2250171572Srrs			/* prevent this address from being used as a source */
2251171572Srrs			sctp_add_local_addr_restricted(stcb, ifa);
2252167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
2253163953Srrs			struct sctp_nets *net;
2254163953Srrs
2255163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2256169352Srrs				sctp_rtentry_t *rt;
2257163953Srrs
2258163953Srrs				/* delete this address if cached */
2259172091Srrs				if (net->ro._s_addr == ifa) {
2260167598Srrs					sctp_free_ifa(net->ro._s_addr);
2261167598Srrs					net->ro._s_addr = NULL;
2262167598Srrs					net->src_addr_selected = 0;
2263167598Srrs					rt = net->ro.ro_rt;
2264167598Srrs					if (rt) {
2265167598Srrs						RTFREE(rt);
2266167598Srrs						net->ro.ro_rt = NULL;
2267167598Srrs					}
2268167598Srrs					/*
2269167598Srrs					 * Now we deleted our src address,
2270167598Srrs					 * should we not also now reset the
2271167598Srrs					 * cwnd/rto to start as if its a new
2272167598Srrs					 * address?
2273167598Srrs					 */
2274171440Srrs					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2275170428Srrs					net->RTO = 0;
2276167598Srrs
2277163953Srrs				}
2278167598Srrs			}
2279167598Srrs		} else if (type == SCTP_SET_PRIM_ADDR) {
2280167598Srrs			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2281171572Srrs				/* must validate the ifa is in the ep */
2282167598Srrs				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
2283167598Srrs					continue;
2284163953Srrs				}
2285167598Srrs			} else {
2286167598Srrs				/* Need to check scopes for this guy */
2287167598Srrs				if (sctp_is_address_in_scope(ifa,
2288167598Srrs				    stcb->asoc.ipv4_addr_legal,
2289167598Srrs				    stcb->asoc.ipv6_addr_legal,
2290167598Srrs				    stcb->asoc.loopback_scope,
2291167598Srrs				    stcb->asoc.ipv4_local_scope,
2292167598Srrs				    stcb->asoc.local_scope,
2293167598Srrs				    stcb->asoc.site_scope, 0) == 0) {
2294167598Srrs					continue;
2295167598Srrs				}
2296163953Srrs			}
2297163953Srrs		}
2298167598Srrs		/* queue an asconf for this address add/delete */
2299171572Srrs		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
2300171572Srrs		    stcb->asoc.peer_supports_asconf) {
2301171572Srrs			/* queue an asconf for this addr */
2302171572Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
2303171572Srrs			/*
2304171572Srrs			 * if queued ok, and in the open state, update the
2305171572Srrs			 * count of queued params.  If in the non-open
2306171572Srrs			 * state, these get sent when the assoc goes open.
2307171572Srrs			 */
2308171572Srrs			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2309171572Srrs				if (status >= 0) {
2310171572Srrs					num_queued++;
2311167598Srrs				}
2312167598Srrs			}
2313167598Srrs		}
2314163953Srrs	}
2315171572Srrs	/*
2316171572Srrs	 * If we have queued params in the open state, send out an ASCONF.
2317171572Srrs	 */
2318171572Srrs	if (num_queued > 0) {
2319172190Srrs		sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2320172190Srrs		    SCTP_ADDR_NOT_LOCKED);
2321171572Srrs	}
2322163953Srrs}
2323163953Srrs
2324171572Srrsvoid
2325171572Srrssctp_asconf_iterator_end(void *ptr, uint32_t val)
2326167598Srrs{
2327167598Srrs	struct sctp_asconf_iterator *asc;
2328167598Srrs	struct sctp_ifa *ifa;
2329167598Srrs	struct sctp_laddr *l, *l_next;
2330167598Srrs
2331167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
2332167598Srrs	l = LIST_FIRST(&asc->list_of_work);
2333167598Srrs	while (l != NULL) {
2334167598Srrs		l_next = LIST_NEXT(l, sctp_nxt_addr);
2335167598Srrs		ifa = l->ifa;
2336167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
2337167598Srrs			/* Clear the defer use flag */
2338167598Srrs			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2339167598Srrs		}
2340167598Srrs		sctp_free_ifa(ifa);
2341179783Srrs		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
2342167598Srrs		SCTP_DECR_LADDR_COUNT();
2343167598Srrs		l = l_next;
2344167598Srrs	}
2345170091Srrs	SCTP_FREE(asc, SCTP_M_ASC_IT);
2346167598Srrs}
2347167598Srrs
2348163953Srrs/*
2349171572Srrs * sa is the sockaddr to ask the peer to set primary to.
2350171572Srrs * returns: 0 = completed, -1 = error
2351163953Srrs */
2352171943Srrsint32_t
2353163953Srrssctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2354163953Srrs{
2355172190Srrs	uint32_t vrf_id;
2356172190Srrs	struct sctp_ifa *ifa;
2357163953Srrs
2358172190Srrs	/* find the ifa for the desired set primary */
2359172218Srrs	vrf_id = stcb->asoc.vrf_id;
2360172190Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
2361172190Srrs	if (ifa == NULL) {
2362172190Srrs		/* Invalid address */
2363172190Srrs		return (-1);
2364172190Srrs	}
2365163953Srrs	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2366172190Srrs	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2367163953Srrs		/* set primary queuing succeeded */
2368171572Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2369171572Srrs		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2370171572Srrs		    stcb);
2371171572Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2372163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2373171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2374163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2375163953Srrs			    stcb->sctp_ep, stcb,
2376163953Srrs			    stcb->asoc.primary_destination);
2377171572Srrs#else
2378172190Srrs			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2379172190Srrs			    SCTP_ADDR_NOT_LOCKED);
2380171572Srrs#endif
2381163953Srrs		}
2382163953Srrs	} else {
2383169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2384169420Srrs		    stcb);
2385169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2386163953Srrs		return (-1);
2387163953Srrs	}
2388163953Srrs	return (0);
2389163953Srrs}
2390163953Srrs
2391163953Srrsvoid
2392167598Srrssctp_set_primary_ip_address(struct sctp_ifa *ifa)
2393163953Srrs{
2394163953Srrs	struct sctp_inpcb *inp;
2395163953Srrs
2396163953Srrs	/* go through all our PCB's */
2397179783Srrs	LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
2398163953Srrs		struct sctp_tcb *stcb;
2399163953Srrs
2400163953Srrs		/* process for all associations for this endpoint */
2401163953Srrs		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2402163953Srrs			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2403163953Srrs			if (!sctp_asconf_queue_add(stcb, ifa,
2404163953Srrs			    SCTP_SET_PRIM_ADDR)) {
2405163953Srrs				/* set primary queuing succeeded */
2406172090Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
2407171572Srrs				    stcb);
2408171572Srrs				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
2409171572Srrs				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2410171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2411163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2412163953Srrs					    stcb->sctp_ep, stcb,
2413163953Srrs					    stcb->asoc.primary_destination);
2414171572Srrs#else
2415172190Srrs					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2416172190Srrs					    SCTP_ADDR_NOT_LOCKED);
2417171572Srrs#endif
2418163953Srrs				}
2419163953Srrs			}
2420163953Srrs		}		/* for each stcb */
2421163953Srrs	}			/* for each inp */
2422163953Srrs}
2423163953Srrs
2424179157Srrsint
2425179157Srrssctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2426179157Srrs{
2427179157Srrs	struct sctp_tmit_chunk *chk, *nchk;
2428179157Srrs	unsigned int offset, asconf_limit;
2429179157Srrs	struct sctp_asconf_chunk *acp;
2430179157Srrs	struct sctp_asconf_paramhdr *aph;
2431179157Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2432179157Srrs	struct sctp_ipv6addr_param *p_addr;
2433179157Srrs	int add_cnt, del_cnt;
2434179157Srrs	uint16_t last_param_type;
2435179157Srrs
2436179157Srrs	add_cnt = del_cnt = 0;
2437179157Srrs	last_param_type = 0;
2438179157Srrs	for (chk = TAILQ_FIRST(&stcb->asoc.asconf_send_queue); chk != NULL;
2439179157Srrs	    chk = nchk) {
2440179157Srrs		/* get next chk */
2441179157Srrs		nchk = TAILQ_NEXT(chk, sctp_next);
2442179157Srrs
2443179157Srrs		if (chk->data == NULL) {
2444179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2445179157Srrs			continue;
2446179157Srrs		}
2447179157Srrs		offset = 0;
2448179157Srrs		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2449179157Srrs		offset += sizeof(struct sctp_asconf_chunk);
2450179157Srrs		asconf_limit = ntohs(acp->ch.chunk_length);
2451179157Srrs		p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2452179157Srrs		if (p_addr == NULL) {
2453179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2454179157Srrs			continue;
2455179157Srrs		}
2456179157Srrs		offset += ntohs(p_addr->ph.param_length);
2457179157Srrs
2458179157Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2459179157Srrs		if (aph == NULL) {
2460179157Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2461179157Srrs			continue;
2462179157Srrs		}
2463179157Srrs		while (aph != NULL) {
2464179157Srrs			unsigned int param_length, param_type;
2465179157Srrs
2466179157Srrs			param_type = ntohs(aph->ph.param_type);
2467179157Srrs			param_length = ntohs(aph->ph.param_length);
2468179157Srrs			if (offset + param_length > asconf_limit) {
2469179157Srrs				/* parameter goes beyond end of chunk! */
2470179157Srrs				break;
2471179157Srrs			}
2472179157Srrs			if (param_length > sizeof(aparam_buf)) {
2473179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2474179157Srrs				break;
2475179157Srrs			}
2476179157Srrs			if (param_length <= sizeof(struct sctp_paramhdr)) {
2477179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2478179157Srrs				break;
2479179157Srrs			}
2480179157Srrs			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2481179157Srrs			if (aph == NULL) {
2482179157Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2483179157Srrs				break;
2484179157Srrs			}
2485179157Srrs			p_addr = (struct sctp_ipv6addr_param *)(aph + 1);
2486179157Srrs			if (sctp_addr_match(p_addr, &sctp_ifa->address.sa) != 0) {
2487179157Srrs				switch (param_type) {
2488179157Srrs				case SCTP_ADD_IP_ADDRESS:
2489179157Srrs					add_cnt++;
2490179157Srrs					break;
2491179157Srrs				case SCTP_DEL_IP_ADDRESS:
2492179157Srrs					del_cnt++;
2493179157Srrs					break;
2494179157Srrs				default:
2495179157Srrs					break;
2496179157Srrs				}
2497179157Srrs				last_param_type = param_type;
2498179157Srrs			}
2499179157Srrs			offset += SCTP_SIZE32(param_length);
2500179157Srrs			if (offset >= asconf_limit) {
2501179157Srrs				/* no more data in the mbuf chain */
2502179157Srrs				break;
2503179157Srrs			}
2504179157Srrs			/* get pointer to next asconf param */
2505179157Srrs			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2506179157Srrs		}
2507179157Srrs	}
2508179157Srrs
2509179157Srrs	/*
2510179157Srrs	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2511179157Srrs	 * or DEL -> ADD
2512179157Srrs	 */
2513179157Srrs	if (add_cnt > del_cnt ||
2514179157Srrs	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2515179157Srrs		return 1;
2516179157Srrs	}
2517179157Srrs	return 0;
2518179157Srrs}
2519179157Srrs
2520163953Srrsstatic struct sockaddr *
2521172190Srrssctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2522163953Srrs{
2523167598Srrs	struct sctp_vrf *vrf = NULL;
2524167598Srrs	struct sctp_ifn *sctp_ifn;
2525167598Srrs	struct sctp_ifa *sctp_ifa;
2526163953Srrs
2527172190Srrs	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2528172218Srrs		SCTP_IPI_ADDR_RLOCK();
2529167598Srrs	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2530169420Srrs	if (vrf == NULL) {
2531172190Srrs		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2532172218Srrs			SCTP_IPI_ADDR_RUNLOCK();
2533169420Srrs		return (NULL);
2534169420Srrs	}
2535167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2536167598Srrs		if (stcb->asoc.loopback_scope == 0 &&
2537167598Srrs		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2538163953Srrs			/* Skip if loopback_scope not set */
2539163953Srrs			continue;
2540163953Srrs		}
2541167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2542167598Srrs			if (sctp_ifa->address.sa.sa_family == AF_INET &&
2543163953Srrs			    stcb->asoc.ipv4_addr_legal) {
2544163953Srrs				struct sockaddr_in *sin;
2545163953Srrs
2546167598Srrs				sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
2547163953Srrs				if (sin->sin_addr.s_addr == 0) {
2548163953Srrs					/* skip unspecifed addresses */
2549163953Srrs					continue;
2550163953Srrs				}
2551163953Srrs				if (stcb->asoc.ipv4_local_scope == 0 &&
2552163953Srrs				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2553163953Srrs					continue;
2554163953Srrs
2555179157Srrs				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2556179157Srrs				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2557163953Srrs					continue;
2558163953Srrs				/* found a valid local v4 address to use */
2559172190Srrs				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2560172218Srrs					SCTP_IPI_ADDR_RUNLOCK();
2561167598Srrs				return (&sctp_ifa->address.sa);
2562167598Srrs			} else if (sctp_ifa->address.sa.sa_family == AF_INET6 &&
2563163953Srrs			    stcb->asoc.ipv6_addr_legal) {
2564163953Srrs				struct sockaddr_in6 *sin6;
2565163953Srrs
2566167598Srrs				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2567163953Srrs					continue;
2568167598Srrs				}
2569167598Srrs				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
2570163953Srrs				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2571163953Srrs					/* we skip unspecifed addresses */
2572163953Srrs					continue;
2573163953Srrs				}
2574163953Srrs				if (stcb->asoc.local_scope == 0 &&
2575163953Srrs				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2576163953Srrs					continue;
2577163953Srrs				if (stcb->asoc.site_scope == 0 &&
2578163953Srrs				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2579163953Srrs					continue;
2580163953Srrs
2581179157Srrs				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2582179157Srrs				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2583179157Srrs					continue;
2584163953Srrs				/* found a valid local v6 address to use */
2585172190Srrs				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2586172218Srrs					SCTP_IPI_ADDR_RUNLOCK();
2587167598Srrs				return (&sctp_ifa->address.sa);
2588163953Srrs			}
2589163953Srrs		}
2590163953Srrs	}
2591163953Srrs	/* no valid addresses found */
2592172190Srrs	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2593172218Srrs		SCTP_IPI_ADDR_RUNLOCK();
2594163953Srrs	return (NULL);
2595163953Srrs}
2596163953Srrs
2597163953Srrsstatic struct sockaddr *
2598163953Srrssctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2599163953Srrs{
2600163953Srrs	struct sctp_laddr *laddr;
2601163953Srrs
2602163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2603163953Srrs		if (laddr->ifa == NULL) {
2604163953Srrs			continue;
2605163953Srrs		}
2606163953Srrs		/* is the address restricted ? */
2607179157Srrs		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2608179157Srrs		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2609163953Srrs			continue;
2610163953Srrs
2611163953Srrs		/* found a valid local address to use */
2612167598Srrs		return (&laddr->ifa->address.sa);
2613163953Srrs	}
2614163953Srrs	/* no valid addresses found */
2615163953Srrs	return (NULL);
2616163953Srrs}
2617163953Srrs
2618163953Srrs/*
2619171477Srrs * builds an ASCONF chunk from queued ASCONF params.
2620171477Srrs * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2621163953Srrs */
2622163953Srrsstruct mbuf *
2623172190Srrssctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2624163953Srrs{
2625163953Srrs	struct mbuf *m_asconf, *m_asconf_chk;
2626163953Srrs	struct sctp_asconf_addr *aa;
2627163953Srrs	struct sctp_asconf_chunk *acp;
2628163953Srrs	struct sctp_asconf_paramhdr *aph;
2629163953Srrs	struct sctp_asconf_addr_param *aap;
2630163953Srrs	uint32_t p_length;
2631163953Srrs	uint32_t correlation_id = 1;	/* 0 is reserved... */
2632163953Srrs	caddr_t ptr, lookup_ptr;
2633163953Srrs	uint8_t lookup_used = 0;
2634163953Srrs
2635163953Srrs	/* are there any asconf params to send? */
2636179157Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2637179157Srrs		if (aa->sent == 0)
2638179157Srrs			break;
2639163953Srrs	}
2640179157Srrs	if (aa == NULL)
2641171572Srrs		return (NULL);
2642179157Srrs
2643163953Srrs	/*
2644163953Srrs	 * get a chunk header mbuf and a cluster for the asconf params since
2645163953Srrs	 * it's simpler to fill in the asconf chunk header lookup address on
2646163953Srrs	 * the fly
2647163953Srrs	 */
2648165647Srrs	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2649163953Srrs	if (m_asconf_chk == NULL) {
2650163953Srrs		/* no mbuf's */
2651169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2652169420Srrs		    "compose_asconf: couldn't get chunk mbuf!\n");
2653163953Srrs		return (NULL);
2654163953Srrs	}
2655165647Srrs	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2656163953Srrs	if (m_asconf == NULL) {
2657163953Srrs		/* no mbuf's */
2658169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2659169420Srrs		    "compose_asconf: couldn't get mbuf!\n");
2660163953Srrs		sctp_m_freem(m_asconf_chk);
2661163953Srrs		return (NULL);
2662163953Srrs	}
2663165647Srrs	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2664165647Srrs	SCTP_BUF_LEN(m_asconf) = 0;
2665163953Srrs	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2666163953Srrs	bzero(acp, sizeof(struct sctp_asconf_chunk));
2667163953Srrs	/* save pointers to lookup address and asconf params */
2668163953Srrs	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2669163953Srrs	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2670163953Srrs
2671163953Srrs	/* fill in chunk header info */
2672163953Srrs	acp->ch.chunk_type = SCTP_ASCONF;
2673163953Srrs	acp->ch.chunk_flags = 0;
2674163953Srrs	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2675179157Srrs	stcb->asoc.asconf_seq_out++;
2676163953Srrs
2677163953Srrs	/* add parameters... up to smallest MTU allowed */
2678163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2679179157Srrs		if (aa->sent)
2680179157Srrs			continue;
2681163953Srrs		/* get the parameter length */
2682163953Srrs		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2683163953Srrs		/* will it fit in current chunk? */
2684165647Srrs		if (SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) {
2685163953Srrs			/* won't fit, so we're done with this chunk */
2686163953Srrs			break;
2687163953Srrs		}
2688163953Srrs		/* assign (and store) a correlation id */
2689163953Srrs		aa->ap.aph.correlation_id = correlation_id++;
2690163953Srrs
2691163953Srrs		/*
2692163953Srrs		 * fill in address if we're doing a delete this is a simple
2693163953Srrs		 * way for us to fill in the correlation address, which
2694163953Srrs		 * should only be used by the peer if we're deleting our
2695163953Srrs		 * source address and adding a new address (e.g. renumbering
2696163953Srrs		 * case)
2697163953Srrs		 */
2698163953Srrs		if (lookup_used == 0 &&
2699185694Srrs		    (aa->special_del == 0) &&
2700163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2701163953Srrs			struct sctp_ipv6addr_param *lookup;
2702163953Srrs			uint16_t p_size, addr_size;
2703163953Srrs
2704163953Srrs			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2705163953Srrs			lookup->ph.param_type =
2706163953Srrs			    htons(aa->ap.addrp.ph.param_type);
2707163953Srrs			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2708163953Srrs				/* copy IPv6 address */
2709163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2710163953Srrs				addr_size = sizeof(struct in6_addr);
2711163953Srrs			} else {
2712163953Srrs				/* copy IPv4 address */
2713163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2714163953Srrs				addr_size = sizeof(struct in_addr);
2715163953Srrs			}
2716163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2717163953Srrs			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2718165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2719163953Srrs			lookup_used = 1;
2720163953Srrs		}
2721163953Srrs		/* copy into current space */
2722163953Srrs		memcpy(ptr, &aa->ap, p_length);
2723163953Srrs
2724163953Srrs		/* network elements and update lengths */
2725163953Srrs		aph = (struct sctp_asconf_paramhdr *)ptr;
2726163953Srrs		aap = (struct sctp_asconf_addr_param *)ptr;
2727163953Srrs		/* correlation_id is transparent to peer, no htonl needed */
2728163953Srrs		aph->ph.param_type = htons(aph->ph.param_type);
2729163953Srrs		aph->ph.param_length = htons(aph->ph.param_length);
2730163953Srrs		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2731163953Srrs		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2732163953Srrs
2733165647Srrs		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2734163953Srrs		ptr += SCTP_SIZE32(p_length);
2735163953Srrs
2736163953Srrs		/*
2737163953Srrs		 * these params are removed off the pending list upon
2738163953Srrs		 * getting an ASCONF-ACK back from the peer, just set flag
2739163953Srrs		 */
2740163953Srrs		aa->sent = 1;
2741163953Srrs	}
2742163953Srrs	/* check to see if the lookup addr has been populated yet */
2743163953Srrs	if (lookup_used == 0) {
2744163953Srrs		/* NOTE: if the address param is optional, can skip this... */
2745163953Srrs		/* add any valid (existing) address... */
2746163953Srrs		struct sctp_ipv6addr_param *lookup;
2747163953Srrs		uint16_t p_size, addr_size;
2748163953Srrs		struct sockaddr *found_addr;
2749163953Srrs		caddr_t addr_ptr;
2750163953Srrs
2751163953Srrs		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2752172190Srrs			found_addr = sctp_find_valid_localaddr(stcb,
2753172190Srrs			    addr_locked);
2754163953Srrs		else
2755163953Srrs			found_addr = sctp_find_valid_localaddr_ep(stcb);
2756163953Srrs
2757163953Srrs		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2758163953Srrs		if (found_addr != NULL) {
2759163953Srrs			if (found_addr->sa_family == AF_INET6) {
2760163953Srrs				/* copy IPv6 address */
2761163953Srrs				lookup->ph.param_type =
2762163953Srrs				    htons(SCTP_IPV6_ADDRESS);
2763163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2764163953Srrs				addr_size = sizeof(struct in6_addr);
2765163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2766163953Srrs				    found_addr)->sin6_addr;
2767163953Srrs			} else {
2768163953Srrs				/* copy IPv4 address */
2769163953Srrs				lookup->ph.param_type =
2770163953Srrs				    htons(SCTP_IPV4_ADDRESS);
2771163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2772163953Srrs				addr_size = sizeof(struct in_addr);
2773163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2774163953Srrs				    found_addr)->sin_addr;
2775163953Srrs			}
2776163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2777163953Srrs			memcpy(lookup->addr, addr_ptr, addr_size);
2778165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2779163953Srrs			lookup_used = 1;
2780163953Srrs		} else {
2781163953Srrs			/* uh oh... don't have any address?? */
2782169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
2783169420Srrs			    "compose_asconf: no lookup addr!\n");
2784163953Srrs			/* for now, we send a IPv4 address of 0.0.0.0 */
2785163953Srrs			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2786163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2787163953Srrs			bzero(lookup->addr, sizeof(struct in_addr));
2788165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2789163953Srrs			lookup_used = 1;
2790163953Srrs		}
2791163953Srrs	}
2792163953Srrs	/* chain it all together */
2793165647Srrs	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2794169655Srrs	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2795169655Srrs	acp->ch.chunk_length = ntohs(*retlen);
2796163953Srrs
2797163953Srrs	return (m_asconf_chk);
2798163953Srrs}
2799163953Srrs
2800163953Srrs/*
2801163953Srrs * section to handle address changes before an association is up eg. changes
2802163953Srrs * during INIT/INIT-ACK/COOKIE-ECHO handshake
2803163953Srrs */
2804163953Srrs
2805163953Srrs/*
2806163953Srrs * processes the (local) addresses in the INIT-ACK chunk
2807163953Srrs */
2808163953Srrsstatic void
2809163953Srrssctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2810163953Srrs    unsigned int offset, unsigned int length)
2811163953Srrs{
2812163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2813163953Srrs	uint16_t plen, ptype;
2814167598Srrs	struct sctp_ifa *sctp_ifa;
2815163953Srrs	struct sctp_ipv6addr_param addr_store;
2816163953Srrs	struct sockaddr_in6 sin6;
2817163953Srrs	struct sockaddr_in sin;
2818163953Srrs	struct sockaddr *sa;
2819167598Srrs	uint32_t vrf_id;
2820163953Srrs
2821169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2822169420Srrs	if (stcb == NULL)	/* Un-needed check for SA */
2823169420Srrs		return;
2824163953Srrs
2825163953Srrs	/* convert to upper bound */
2826163953Srrs	length += offset;
2827163953Srrs
2828163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2829163953Srrs		return;
2830163953Srrs	}
2831163953Srrs	/* init the addresses */
2832163953Srrs	bzero(&sin6, sizeof(sin6));
2833163953Srrs	sin6.sin6_family = AF_INET6;
2834163953Srrs	sin6.sin6_len = sizeof(sin6);
2835163953Srrs	sin6.sin6_port = stcb->rport;
2836163953Srrs
2837163953Srrs	bzero(&sin, sizeof(sin));
2838163953Srrs	sin.sin_len = sizeof(sin);
2839163953Srrs	sin.sin_family = AF_INET;
2840163953Srrs	sin.sin_port = stcb->rport;
2841163953Srrs
2842163953Srrs	/* go through the addresses in the init-ack */
2843163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2844163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2845163953Srrs	while (ph != NULL) {
2846163953Srrs		ptype = ntohs(ph->param_type);
2847163953Srrs		plen = ntohs(ph->param_length);
2848163953Srrs		if (ptype == SCTP_IPV6_ADDRESS) {
2849163953Srrs			struct sctp_ipv6addr_param *a6p;
2850163953Srrs
2851163953Srrs			/* get the entire IPv6 address param */
2852163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2853163953Srrs			    sctp_m_getptr(m, offset,
2854163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2855163953Srrs			    (uint8_t *) & addr_store);
2856163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2857163953Srrs			    a6p == NULL) {
2858163953Srrs				return;
2859163953Srrs			}
2860163953Srrs			memcpy(&sin6.sin6_addr, a6p->addr,
2861163953Srrs			    sizeof(struct in6_addr));
2862163953Srrs			sa = (struct sockaddr *)&sin6;
2863163953Srrs		} else if (ptype == SCTP_IPV4_ADDRESS) {
2864163953Srrs			struct sctp_ipv4addr_param *a4p;
2865163953Srrs
2866163953Srrs			/* get the entire IPv4 address param */
2867167598Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2868167598Srrs			    sizeof(struct sctp_ipv4addr_param),
2869167598Srrs			    (uint8_t *) & addr_store);
2870163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2871163953Srrs			    a4p == NULL) {
2872163953Srrs				return;
2873163953Srrs			}
2874163953Srrs			sin.sin_addr.s_addr = a4p->addr;
2875163953Srrs			sa = (struct sockaddr *)&sin;
2876163953Srrs		} else {
2877163953Srrs			goto next_addr;
2878163953Srrs		}
2879163953Srrs
2880163953Srrs		/* see if this address really (still) exists */
2881168299Srrs		if (stcb) {
2882168299Srrs			vrf_id = stcb->asoc.vrf_id;
2883168299Srrs		} else {
2884168299Srrs			vrf_id = SCTP_DEFAULT_VRFID;
2885168299Srrs		}
2886172190Srrs		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id,
2887172190Srrs		    SCTP_ADDR_NOT_LOCKED);
2888167598Srrs		if (sctp_ifa == NULL) {
2889163953Srrs			/* address doesn't exist anymore */
2890163953Srrs			int status;
2891163953Srrs
2892163953Srrs			/* are ASCONFs allowed ? */
2893163953Srrs			if ((sctp_is_feature_on(stcb->sctp_ep,
2894163953Srrs			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2895163953Srrs			    stcb->asoc.peer_supports_asconf) {
2896163953Srrs				/* queue an ASCONF DEL_IP_ADDRESS */
2897172190Srrs				status = sctp_asconf_queue_sa_delete(stcb, sa);
2898163953Srrs				/*
2899171572Srrs				 * if queued ok, and in correct state, send
2900171572Srrs				 * out the ASCONF.
2901163953Srrs				 */
2902163953Srrs				if (status == 0 &&
2903163953Srrs				    SCTP_GET_STATE(&stcb->asoc) ==
2904163953Srrs				    SCTP_STATE_OPEN) {
2905171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF
2906163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2907163953Srrs					    stcb->sctp_ep, stcb,
2908163953Srrs					    stcb->asoc.primary_destination);
2909171572Srrs#else
2910172190Srrs					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2911172190Srrs					    SCTP_ADDR_NOT_LOCKED);
2912171572Srrs#endif
2913163953Srrs				}
2914163953Srrs			}
2915163953Srrs		}
2916163953Srrsnext_addr:
2917163953Srrs		/*
2918163953Srrs		 * Sanity check:  Make sure the length isn't 0, otherwise
2919163953Srrs		 * we'll be stuck in this loop for a long time...
2920163953Srrs		 */
2921163953Srrs		if (SCTP_SIZE32(plen) == 0) {
2922169420Srrs			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2923163953Srrs			    plen, ptype);
2924163953Srrs			return;
2925163953Srrs		}
2926163953Srrs		/* get next parameter */
2927163953Srrs		offset += SCTP_SIZE32(plen);
2928163953Srrs		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2929163953Srrs			return;
2930163953Srrs		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2931163953Srrs		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2932163953Srrs	}			/* while */
2933163953Srrs}
2934163953Srrs
2935163953Srrs/* FIX ME: need to verify return result for v6 address type if v6 disabled */
2936163953Srrs/*
2937163953Srrs * checks to see if a specific address is in the initack address list returns
2938163953Srrs * 1 if found, 0 if not
2939163953Srrs */
2940163953Srrsstatic uint32_t
2941163953Srrssctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, uint32_t offset,
2942163953Srrs    uint32_t length, struct sockaddr *sa)
2943163953Srrs{
2944163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2945163953Srrs	uint16_t plen, ptype;
2946163953Srrs	struct sctp_ipv6addr_param addr_store;
2947163953Srrs	struct sockaddr_in *sin;
2948163953Srrs	struct sctp_ipv4addr_param *a4p;
2949163953Srrs
2950163953Srrs#ifdef INET6
2951167598Srrs	struct sockaddr_in6 *sin6;
2952163953Srrs	struct sctp_ipv6addr_param *a6p;
2953167598Srrs	struct sockaddr_in6 sin6_tmp;
2954163953Srrs
2955163953Srrs#endif				/* INET6 */
2956163953Srrs
2957163953Srrs	if (
2958163953Srrs#ifdef INET6
2959163953Srrs	    (sa->sa_family != AF_INET6) &&
2960163953Srrs#endif				/* INET6 */
2961163953Srrs	    (sa->sa_family != AF_INET))
2962163953Srrs		return (0);
2963163953Srrs
2964169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2965169420Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2966163953Srrs	/* convert to upper bound */
2967163953Srrs	length += offset;
2968163953Srrs
2969163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2970169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2971169420Srrs		    "find_initack_addr: invalid offset?\n");
2972163953Srrs		return (0);
2973163953Srrs	}
2974163953Srrs	/* go through the addresses in the init-ack */
2975163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2976163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2977163953Srrs	while (ph != NULL) {
2978163953Srrs		ptype = ntohs(ph->param_type);
2979163953Srrs		plen = ntohs(ph->param_length);
2980163953Srrs#ifdef INET6
2981163953Srrs		if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2982163953Srrs			/* get the entire IPv6 address param */
2983163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2984163953Srrs			    sctp_m_getptr(m, offset,
2985163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2986163953Srrs			    (uint8_t *) & addr_store);
2987163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2988169420Srrs			    (ph == NULL) ||
2989169420Srrs			    (a6p == NULL)) {
2990163953Srrs				return (0);
2991163953Srrs			}
2992163953Srrs			sin6 = (struct sockaddr_in6 *)sa;
2993163953Srrs			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2994163953Srrs				/* create a copy and clear scope */
2995163953Srrs				memcpy(&sin6_tmp, sin6,
2996163953Srrs				    sizeof(struct sockaddr_in6));
2997163953Srrs				sin6 = &sin6_tmp;
2998163953Srrs				in6_clearscope(&sin6->sin6_addr);
2999163953Srrs			}
3000163953Srrs			if (memcmp(&sin6->sin6_addr, a6p->addr,
3001163953Srrs			    sizeof(struct in6_addr)) == 0) {
3002163953Srrs				/* found it */
3003163953Srrs				return (1);
3004163953Srrs			}
3005163953Srrs		} else
3006163953Srrs#endif				/* INET6 */
3007163953Srrs
3008163953Srrs			if (ptype == SCTP_IPV4_ADDRESS &&
3009163953Srrs		    sa->sa_family == AF_INET) {
3010163953Srrs			/* get the entire IPv4 address param */
3011163953Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
3012163953Srrs			    offset, sizeof(struct sctp_ipv4addr_param),
3013163953Srrs			    (uint8_t *) & addr_store);
3014163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
3015169420Srrs			    (ph == NULL) ||
3016169420Srrs			    (a4p == NULL)) {
3017163953Srrs				return (0);
3018163953Srrs			}
3019163953Srrs			sin = (struct sockaddr_in *)sa;
3020163953Srrs			if (sin->sin_addr.s_addr == a4p->addr) {
3021163953Srrs				/* found it */
3022163953Srrs				return (1);
3023163953Srrs			}
3024163953Srrs		}
3025163953Srrs		/* get next parameter */
3026163953Srrs		offset += SCTP_SIZE32(plen);
3027163953Srrs		if (offset + sizeof(struct sctp_paramhdr) > length)
3028163953Srrs			return (0);
3029163953Srrs		ph = (struct sctp_paramhdr *)
3030163953Srrs		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
3031163953Srrs		    (uint8_t *) & tmp_param);
3032163953Srrs	}			/* while */
3033163953Srrs	/* not found! */
3034163953Srrs	return (0);
3035163953Srrs}
3036163953Srrs
3037163953Srrs/*
3038163953Srrs * makes sure that the current endpoint local addr list is consistent with
3039163953Srrs * the new association (eg. subset bound, asconf allowed) adds addresses as
3040163953Srrs * necessary
3041163953Srrs */
3042163953Srrsstatic void
3043163953Srrssctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3044163953Srrs    int length, struct sockaddr *init_addr)
3045163953Srrs{
3046163953Srrs	struct sctp_laddr *laddr;
3047163953Srrs
3048163953Srrs	/* go through the endpoint list */
3049163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3050163953Srrs		/* be paranoid and validate the laddr */
3051163953Srrs		if (laddr->ifa == NULL) {
3052169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
3053169420Srrs			    "check_addr_list_ep: laddr->ifa is NULL");
3054163953Srrs			continue;
3055163953Srrs		}
3056167598Srrs		if (laddr->ifa == NULL) {
3057169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
3058163953Srrs			continue;
3059163953Srrs		}
3060163953Srrs		/* do i have it implicitly? */
3061167598Srrs		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3062163953Srrs			continue;
3063163953Srrs		}
3064163953Srrs		/* check to see if in the init-ack */
3065163953Srrs		if (!sctp_addr_in_initack(stcb, m, offset, length,
3066167598Srrs		    &laddr->ifa->address.sa)) {
3067163953Srrs			/* try to add it */
3068163953Srrs			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
3069172190Srrs			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3070163953Srrs		}
3071163953Srrs	}
3072163953Srrs}
3073163953Srrs
3074163953Srrs/*
3075163953Srrs * makes sure that the current kernel address list is consistent with the new
3076163953Srrs * association (with all addrs bound) adds addresses as necessary
3077163953Srrs */
3078163953Srrsstatic void
3079163953Srrssctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3080163953Srrs    int length, struct sockaddr *init_addr,
3081163953Srrs    uint16_t local_scope, uint16_t site_scope,
3082163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
3083163953Srrs{
3084167598Srrs	struct sctp_vrf *vrf = NULL;
3085167598Srrs	struct sctp_ifn *sctp_ifn;
3086167598Srrs	struct sctp_ifa *sctp_ifa;
3087167598Srrs	uint32_t vrf_id;
3088163953Srrs
3089168299Srrs	if (stcb) {
3090168299Srrs		vrf_id = stcb->asoc.vrf_id;
3091168299Srrs	} else {
3092169420Srrs		return;
3093168299Srrs	}
3094172218Srrs	SCTP_IPI_ADDR_RLOCK();
3095167598Srrs	vrf = sctp_find_vrf(vrf_id);
3096167598Srrs	if (vrf == NULL) {
3097172218Srrs		SCTP_IPI_ADDR_RUNLOCK();
3098167598Srrs		return;
3099167598Srrs	}
3100163953Srrs	/* go through all our known interfaces */
3101167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3102167598Srrs		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3103163953Srrs			/* skip loopback interface */
3104163953Srrs			continue;
3105163953Srrs		}
3106163953Srrs		/* go through each interface address */
3107167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3108163953Srrs			/* do i have it implicitly? */
3109167598Srrs			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3110163953Srrs				continue;
3111163953Srrs			}
3112163953Srrs			/* check to see if in the init-ack */
3113163953Srrs			if (!sctp_addr_in_initack(stcb, m, offset, length,
3114167598Srrs			    &sctp_ifa->address.sa)) {
3115163953Srrs				/* try to add it */
3116163953Srrs				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
3117172190Srrs				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
3118172190Srrs				    SCTP_ADDR_LOCKED);
3119163953Srrs			}
3120163953Srrs		}		/* end foreach ifa */
3121163953Srrs	}			/* end foreach ifn */
3122172218Srrs	SCTP_IPI_ADDR_RUNLOCK();
3123163953Srrs}
3124163953Srrs
3125163953Srrs/*
3126163953Srrs * validates an init-ack chunk (from a cookie-echo) with current addresses
3127163953Srrs * adds addresses from the init-ack into our local address list, if needed
3128163953Srrs * queues asconf adds/deletes addresses as needed and makes appropriate list
3129163953Srrs * changes for source address selection m, offset: points to the start of the
3130163953Srrs * address list in an init-ack chunk length: total length of the address
3131163953Srrs * params only init_addr: address where my INIT-ACK was sent from
3132163953Srrs */
3133163953Srrsvoid
3134163953Srrssctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3135163953Srrs    int length, struct sockaddr *init_addr,
3136163953Srrs    uint16_t local_scope, uint16_t site_scope,
3137163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
3138163953Srrs{
3139163953Srrs	/* process the local addresses in the initack */
3140163953Srrs	sctp_process_initack_addresses(stcb, m, offset, length);
3141163953Srrs
3142163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3143163953Srrs		/* bound all case */
3144163953Srrs		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3145163953Srrs		    local_scope, site_scope, ipv4_scope, loopback_scope);
3146163953Srrs	} else {
3147163953Srrs		/* subset bound case */
3148163953Srrs		if (sctp_is_feature_on(stcb->sctp_ep,
3149163953Srrs		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3150163953Srrs			/* asconf's allowed */
3151163953Srrs			sctp_check_address_list_ep(stcb, m, offset, length,
3152163953Srrs			    init_addr);
3153163953Srrs		}
3154163953Srrs		/* else, no asconfs allowed, so what we sent is what we get */
3155163953Srrs	}
3156163953Srrs}
3157163953Srrs
3158163953Srrs/*
3159163953Srrs * sctp_bindx() support
3160163953Srrs */
3161163953Srrsuint32_t
3162169655Srrssctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
3163170744Srrs    uint32_t type, uint32_t vrf_id, struct sctp_ifa *sctp_ifap)
3164163953Srrs{
3165167598Srrs	struct sctp_ifa *ifa;
3166163953Srrs
3167167598Srrs	if (sa->sa_len == 0) {
3168171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3169163953Srrs		return (EINVAL);
3170167598Srrs	}
3171170744Srrs	if (sctp_ifap) {
3172170744Srrs		ifa = sctp_ifap;
3173170744Srrs	} else if (type == SCTP_ADD_IP_ADDRESS) {
3174167598Srrs		/* For an add the address MUST be on the system */
3175172091Srrs		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
3176167598Srrs	} else if (type == SCTP_DEL_IP_ADDRESS) {
3177167598Srrs		/* For a delete we need to find it in the inp */
3178172091Srrs		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
3179167598Srrs	} else {
3180167598Srrs		ifa = NULL;
3181167598Srrs	}
3182163953Srrs	if (ifa != NULL) {
3183167598Srrs		/* add this address */
3184167598Srrs		struct sctp_asconf_iterator *asc;
3185167598Srrs		struct sctp_laddr *wi;
3186163953Srrs
3187167598Srrs		SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
3188169655Srrs		    sizeof(struct sctp_asconf_iterator),
3189170091Srrs		    SCTP_M_ASC_IT);
3190167598Srrs		if (asc == NULL) {
3191171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3192167598Srrs			return (ENOMEM);
3193167598Srrs		}
3194179783Srrs		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr),
3195169655Srrs		    struct sctp_laddr);
3196167598Srrs		if (wi == NULL) {
3197170091Srrs			SCTP_FREE(asc, SCTP_M_ASC_IT);
3198171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3199167598Srrs			return (ENOMEM);
3200167598Srrs		}
3201167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
3202167598Srrs			sctp_add_local_addr_ep(inp, ifa, type);
3203167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
3204167598Srrs			struct sctp_laddr *laddr;
3205167598Srrs
3206169655Srrs			if (inp->laddr_count < 2) {
3207169655Srrs				/* can't delete the last local address */
3208170931Srrs				SCTP_FREE(asc, SCTP_M_ASC_IT);
3209179783Srrs				SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
3210171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3211169655Srrs				return (EINVAL);
3212169655Srrs			}
3213169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
3214169655Srrs			    sctp_nxt_addr) {
3215167598Srrs				if (ifa == laddr->ifa) {
3216167598Srrs					/* Mark in the delete */
3217167598Srrs					laddr->action = type;
3218167598Srrs				}
3219163953Srrs			}
3220163953Srrs		}
3221167598Srrs		LIST_INIT(&asc->list_of_work);
3222167598Srrs		asc->cnt = 1;
3223167598Srrs		SCTP_INCR_LADDR_COUNT();
3224167598Srrs		wi->ifa = ifa;
3225167598Srrs		wi->action = type;
3226167598Srrs		atomic_add_int(&ifa->refcount, 1);
3227167598Srrs		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
3228171572Srrs		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
3229171572Srrs		    sctp_asconf_iterator_stcb,
3230171572Srrs		    sctp_asconf_iterator_ep_end,
3231167598Srrs		    SCTP_PCB_ANY_FLAGS,
3232169655Srrs		    SCTP_PCB_ANY_FEATURES,
3233171572Srrs		    SCTP_ASOC_ANY_STATE,
3234171572Srrs		    (void *)asc, 0,
3235171572Srrs		    sctp_asconf_iterator_end, inp, 0);
3236163953Srrs	} else {
3237163953Srrs		/* invalid address! */
3238171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3239163953Srrs		return (EADDRNOTAVAIL);
3240163953Srrs	}
3241163953Srrs	return (0);
3242163953Srrs}
3243185694Srrs
3244185694Srrsvoid
3245185694Srrssctp_asconf_send_nat_state_update(struct sctp_tcb *stcb,
3246185694Srrs    struct sctp_nets *net)
3247185694Srrs{
3248185694Srrs	struct sctp_asconf_addr *aa;
3249185694Srrs	struct sctp_ifa *sctp_ifap;
3250185694Srrs	struct sctp_asconf_tag_param *vtag;
3251185694Srrs	struct sockaddr_in *to;
3252185694Srrs
3253185694Srrs#ifdef INET6
3254185694Srrs	struct sockaddr_in6 *to6;
3255185694Srrs
3256185694Srrs#endif
3257185694Srrs	if (net == NULL) {
3258185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
3259185694Srrs		return;
3260185694Srrs	}
3261185694Srrs	if (stcb == NULL) {
3262185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
3263185694Srrs		return;
3264185694Srrs	}
3265185694Srrs	/*
3266185694Srrs	 * Need to have in the asconf: - vtagparam(my_vtag/peer_vtag) -
3267185694Srrs	 * add(0.0.0.0) - del(0.0.0.0) - Any global addresses add(addr)
3268185694Srrs	 */
3269185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3270185694Srrs	    SCTP_M_ASC_ADDR);
3271185694Srrs	if (aa == NULL) {
3272185694Srrs		/* didn't get memory */
3273185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3274185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3275185694Srrs		return;
3276185694Srrs	}
3277185694Srrs	aa->special_del = 0;
3278185694Srrs	/* fill in asconf address parameter fields */
3279185694Srrs	/* top level elements are "networked" during send */
3280185694Srrs	aa->ifa = NULL;
3281185694Srrs	aa->sent = 0;		/* clear sent flag */
3282185694Srrs	vtag = (struct sctp_asconf_tag_param *)&aa->ap.aph;
3283185694Srrs	vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
3284185694Srrs	vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
3285185694Srrs	vtag->local_vtag = htonl(stcb->asoc.my_vtag);
3286185694Srrs	vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
3287185694Srrs	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3288185694Srrs
3289185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3290185694Srrs	    SCTP_M_ASC_ADDR);
3291185694Srrs	if (aa == NULL) {
3292185694Srrs		/* didn't get memory */
3293185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3294185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3295185694Srrs		return;
3296185694Srrs	}
3297185694Srrs	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3298185694Srrs	/* fill in asconf address parameter fields */
3299185694Srrs	/* ADD(0.0.0.0) */
3300185694Srrs	if (net->ro._l_addr.sa.sa_family == AF_INET) {
3301185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3302185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3303185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3304185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3305185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3306185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3307185694Srrs	}
3308185694Srrs#ifdef INET6
3309185694Srrs	else if (net->ro._l_addr.sa.sa_family == AF_INET6) {
3310185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3311185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3312185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3313185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3314185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3315185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3316185694Srrs	}
3317185694Srrs#endif				/* INET6 */
3318185694Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3319185694Srrs	    SCTP_M_ASC_ADDR);
3320185694Srrs	if (aa == NULL) {
3321185694Srrs		/* didn't get memory */
3322185694Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
3323185694Srrs		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3324185694Srrs		return;
3325185694Srrs	}
3326185694Srrs	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3327185694Srrs	/* fill in asconf address parameter fields */
3328185694Srrs	/* ADD(0.0.0.0) */
3329185694Srrs	if (net->ro._l_addr.sa.sa_family == AF_INET) {
3330185694Srrs		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3331185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3332185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3333185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3334185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3335185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3336185694Srrs	}
3337185694Srrs#ifdef INET6
3338185694Srrs	else if (net->ro._l_addr.sa.sa_family == AF_INET6) {
3339185694Srrs		aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3340185694Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3341185694Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3342185694Srrs		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3343185694Srrs		/* No need to add an address, we are using 0.0.0.0 */
3344185694Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3345185694Srrs	}
3346185694Srrs#endif				/* INET6 */
3347185694Srrs	/* Now we must hunt the addresses and add all global addresses */
3348185694Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3349185694Srrs		struct sctp_vrf *vrf = NULL;
3350185694Srrs		struct sctp_ifn *sctp_ifnp;
3351185694Srrs		uint32_t vrf_id;
3352185694Srrs
3353185694Srrs		vrf_id = stcb->sctp_ep->def_vrf_id;
3354185694Srrs		vrf = sctp_find_vrf(vrf_id);
3355185694Srrs		if (vrf == NULL) {
3356185694Srrs			goto skip_rest;
3357185694Srrs		}
3358185694Srrs		SCTP_IPI_ADDR_RLOCK();
3359185694Srrs		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3360185694Srrs			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3361185694Srrs				if (sctp_ifap->address.sa.sa_family == AF_INET) {
3362185694Srrs					to = &sctp_ifap->address.sin;
3363185694Srrs
3364185694Srrs					if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3365185694Srrs						continue;
3366185694Srrs					}
3367185694Srrs					if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3368185694Srrs						continue;
3369185694Srrs					}
3370185694Srrs				}
3371185694Srrs#ifdef INET6
3372185694Srrs				else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
3373185694Srrs					to6 = &sctp_ifap->address.sin6;
3374185694Srrs					if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3375185694Srrs						continue;
3376185694Srrs					}
3377185694Srrs					if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3378185694Srrs						continue;
3379185694Srrs					}
3380185694Srrs				}
3381185694Srrs#endif
3382185694Srrs				sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3383185694Srrs			}
3384185694Srrs		}
3385185694Srrs		SCTP_IPI_ADDR_RUNLOCK();
3386185694Srrs	} else {
3387185694Srrs		struct sctp_laddr *laddr;
3388185694Srrs
3389185694Srrs		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3390185694Srrs			if (laddr->ifa == NULL) {
3391185694Srrs				continue;
3392185694Srrs			}
3393185694Srrs			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3394185694Srrs				/*
3395185694Srrs				 * Address being deleted by the system, dont
3396185694Srrs				 * list.
3397185694Srrs				 */
3398185694Srrs				continue;
3399185694Srrs			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3400185694Srrs				/*
3401185694Srrs				 * Address being deleted on this ep don't
3402185694Srrs				 * list.
3403185694Srrs				 */
3404185694Srrs				continue;
3405185694Srrs			}
3406185694Srrs			sctp_ifap = laddr->ifa;
3407185694Srrs			if (sctp_ifap->address.sa.sa_family == AF_INET) {
3408185694Srrs				to = &sctp_ifap->address.sin;
3409185694Srrs
3410185694Srrs				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3411185694Srrs					continue;
3412185694Srrs				}
3413185694Srrs				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3414185694Srrs					continue;
3415185694Srrs				}
3416185694Srrs			}
3417185694Srrs#ifdef INET6
3418185694Srrs			else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
3419185694Srrs				to6 = &sctp_ifap->address.sin6;
3420185694Srrs				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3421185694Srrs					continue;
3422185694Srrs				}
3423185694Srrs				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3424185694Srrs					continue;
3425185694Srrs				}
3426185694Srrs			}
3427185694Srrs#endif
3428185694Srrs			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3429185694Srrs		}
3430185694Srrs	}
3431185694Srrsskip_rest:
3432185694Srrs	/* Now we must send the asconf into the queue */
3433185694Srrs	sctp_send_asconf(stcb, net, 0);
3434185694Srrs}
3435