xref: /freebsd/sys/netinet/sctp_asconf.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_asconf.h>
44 #include <netinet/sctp_timer.h>
45 
46 /*
47  * debug flags:
48  * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
49  * SCTP_DEBUG_ASCONF2: detailed info
50  */
51 
52 /*
53  * RFC 5061
54  *
55  * An ASCONF parameter queue exists per asoc which holds the pending address
56  * operations.  Lists are updated upon receipt of ASCONF-ACK.
57  *
58  * A restricted_addrs list exists per assoc to hold local addresses that are
59  * not (yet) usable by the assoc as a source address.  These addresses are
60  * either pending an ASCONF operation (and exist on the ASCONF parameter
61  * queue), or they are permanently restricted (the peer has returned an
62  * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
63  *
64  * Deleted addresses are always immediately removed from the lists as they will
65  * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
66  * only if allowed.
67  */
68 
69 /*
70  * ASCONF parameter processing.
71  * response_required: set if a reply is required (eg. SUCCESS_REPORT).
72  * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
73  * FIX: allocating this many mbufs on the fly is pretty inefficient...
74  */
75 static struct mbuf *
76 sctp_asconf_success_response(uint32_t id)
77 {
78 	struct mbuf *m_reply = NULL;
79 	struct sctp_asconf_paramhdr *aph;
80 
81 	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
82 	    0, M_NOWAIT, 1, MT_DATA);
83 	if (m_reply == NULL) {
84 		SCTPDBG(SCTP_DEBUG_ASCONF1,
85 		    "asconf_success_response: couldn't get mbuf!\n");
86 		return (NULL);
87 	}
88 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
89 	aph->correlation_id = id;
90 	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
91 	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
92 	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
93 	aph->ph.param_length = htons(aph->ph.param_length);
94 
95 	return (m_reply);
96 }
97 
98 static struct mbuf *
99 sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
100     uint16_t tlv_length)
101 {
102 	struct mbuf *m_reply = NULL;
103 	struct sctp_asconf_paramhdr *aph;
104 	struct sctp_error_cause *error;
105 	uint32_t buf_len;
106 	uint16_t i, param_length, cause_length, padding_length;
107 	uint8_t *tlv;
108 
109 	if (error_tlv == NULL) {
110 		tlv_length = 0;
111 	}
112 	cause_length = sizeof(struct sctp_error_cause) + tlv_length;
113 	param_length = sizeof(struct sctp_asconf_paramhdr) + cause_length;
114 	padding_length = tlv_length % 4;
115 	if (padding_length != 0) {
116 		padding_length = 4 - padding_length;
117 	}
118 	buf_len = param_length + padding_length;
119 	if (buf_len > MLEN) {
120 		SCTPDBG(SCTP_DEBUG_ASCONF1,
121 		    "asconf_error_response: tlv_length (%xh) too big\n",
122 		    tlv_length);
123 		return (NULL);
124 	}
125 	m_reply = sctp_get_mbuf_for_msg(buf_len, 0, M_NOWAIT, 1, MT_DATA);
126 	if (m_reply == NULL) {
127 		SCTPDBG(SCTP_DEBUG_ASCONF1,
128 		    "asconf_error_response: couldn't get mbuf!\n");
129 		return (NULL);
130 	}
131 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
132 	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
133 	aph->ph.param_length = htons(param_length);
134 	aph->correlation_id = id;
135 	error = (struct sctp_error_cause *)(aph + 1);
136 	error->code = htons(cause);
137 	error->length = htons(cause_length);
138 	if (error_tlv != NULL) {
139 		tlv = (uint8_t *)(error + 1);
140 		memcpy(tlv, error_tlv, tlv_length);
141 		for (i = 0; i < padding_length; i++) {
142 			tlv[tlv_length + i] = 0;
143 		}
144 	}
145 	SCTP_BUF_LEN(m_reply) = buf_len;
146 	return (m_reply);
147 }
148 
149 static struct mbuf *
150 sctp_process_asconf_add_ip(struct sockaddr *src, struct sctp_asconf_paramhdr *aph,
151     struct sctp_tcb *stcb, int send_hb, int response_required)
152 {
153 	struct sctp_nets *net;
154 	struct mbuf *m_reply = NULL;
155 	union sctp_sockstore store;
156 	struct sctp_paramhdr *ph;
157 	uint16_t param_type, aparam_length;
158 #if defined(INET) || defined(INET6)
159 	uint16_t param_length;
160 #endif
161 	struct sockaddr *sa;
162 	int zero_address = 0;
163 	int bad_address = 0;
164 #ifdef INET
165 	struct sockaddr_in *sin;
166 	struct sctp_ipv4addr_param *v4addr;
167 #endif
168 #ifdef INET6
169 	struct sockaddr_in6 *sin6;
170 	struct sctp_ipv6addr_param *v6addr;
171 #endif
172 
173 	aparam_length = ntohs(aph->ph.param_length);
174 	if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
175 		return (NULL);
176 	}
177 	ph = (struct sctp_paramhdr *)(aph + 1);
178 	param_type = ntohs(ph->param_type);
179 #if defined(INET) || defined(INET6)
180 	param_length = ntohs(ph->param_length);
181 	if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
182 		return (NULL);
183 	}
184 #endif
185 	sa = &store.sa;
186 	switch (param_type) {
187 #ifdef INET
188 	case SCTP_IPV4_ADDRESS:
189 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
190 			/* invalid param size */
191 			return (NULL);
192 		}
193 		v4addr = (struct sctp_ipv4addr_param *)ph;
194 		sin = &store.sin;
195 		memset(sin, 0, sizeof(*sin));
196 		sin->sin_family = AF_INET;
197 		sin->sin_len = sizeof(struct sockaddr_in);
198 		sin->sin_port = stcb->rport;
199 		sin->sin_addr.s_addr = v4addr->addr;
200 		if ((sin->sin_addr.s_addr == INADDR_BROADCAST) ||
201 		    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
202 			bad_address = 1;
203 		}
204 		if (sin->sin_addr.s_addr == INADDR_ANY)
205 			zero_address = 1;
206 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
207 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
208 		break;
209 #endif
210 #ifdef INET6
211 	case SCTP_IPV6_ADDRESS:
212 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
213 			/* invalid param size */
214 			return (NULL);
215 		}
216 		v6addr = (struct sctp_ipv6addr_param *)ph;
217 		sin6 = &store.sin6;
218 		memset(sin6, 0, sizeof(*sin6));
219 		sin6->sin6_family = AF_INET6;
220 		sin6->sin6_len = sizeof(struct sockaddr_in6);
221 		sin6->sin6_port = stcb->rport;
222 		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
223 		    sizeof(struct in6_addr));
224 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
225 			bad_address = 1;
226 		}
227 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
228 			zero_address = 1;
229 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
230 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
231 		break;
232 #endif
233 	default:
234 		m_reply = sctp_asconf_error_response(aph->correlation_id,
235 		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *)aph,
236 		    aparam_length);
237 		return (m_reply);
238 	}			/* end switch */
239 
240 	/* if 0.0.0.0/::0, add the source address instead */
241 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
242 		sa = src;
243 		SCTPDBG(SCTP_DEBUG_ASCONF1,
244 		    "process_asconf_add_ip: using source addr ");
245 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
246 	}
247 	net = NULL;
248 	/* add the address */
249 	if (bad_address) {
250 		m_reply = sctp_asconf_error_response(aph->correlation_id,
251 		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *)aph,
252 		    aparam_length);
253 	} else if (sctp_add_remote_addr(stcb, sa, &net, stcb->asoc.port,
254 		    SCTP_DONOT_SETSCOPE,
255 	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
256 		SCTPDBG(SCTP_DEBUG_ASCONF1,
257 		    "process_asconf_add_ip: error adding address\n");
258 		m_reply = sctp_asconf_error_response(aph->correlation_id,
259 		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *)aph,
260 		    aparam_length);
261 	} else {
262 		if (response_required) {
263 			m_reply =
264 			    sctp_asconf_success_response(aph->correlation_id);
265 		}
266 		if (net != NULL) {
267 			/* notify upper layer */
268 			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
269 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
270 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
271 			    stcb, net);
272 			if (send_hb) {
273 				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
274 			}
275 		}
276 	}
277 	return (m_reply);
278 }
279 
280 static int
281 sctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
282 {
283 	struct sctp_nets *src_net, *net, *nnet;
284 
285 	/* make sure the source address exists as a destination net */
286 	src_net = sctp_findnet(stcb, src);
287 	if (src_net == NULL) {
288 		/* not found */
289 		return (-1);
290 	}
291 
292 	/* delete all destination addresses except the source */
293 	TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
294 		if (net != src_net) {
295 			/* delete this address */
296 			SCTPDBG(SCTP_DEBUG_ASCONF1,
297 			    "asconf_del_remote_addrs_except: deleting ");
298 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
299 			    (struct sockaddr *)&net->ro._l_addr);
300 			/* notify upper layer */
301 			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
302 			    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
303 			sctp_remove_net(stcb, net);
304 		}
305 	}
306 	return (0);
307 }
308 
309 static struct mbuf *
310 sctp_process_asconf_delete_ip(struct sockaddr *src,
311     struct sctp_asconf_paramhdr *aph,
312     struct sctp_tcb *stcb, int response_required)
313 {
314 	struct mbuf *m_reply = NULL;
315 	union sctp_sockstore store;
316 	struct sctp_paramhdr *ph;
317 	uint16_t param_type, aparam_length;
318 #if defined(INET) || defined(INET6)
319 	uint16_t param_length;
320 #endif
321 	struct sockaddr *sa;
322 	int zero_address = 0;
323 	int result;
324 #ifdef INET
325 	struct sockaddr_in *sin;
326 	struct sctp_ipv4addr_param *v4addr;
327 #endif
328 #ifdef INET6
329 	struct sockaddr_in6 *sin6;
330 	struct sctp_ipv6addr_param *v6addr;
331 #endif
332 
333 	aparam_length = ntohs(aph->ph.param_length);
334 	if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
335 		return (NULL);
336 	}
337 	ph = (struct sctp_paramhdr *)(aph + 1);
338 	param_type = ntohs(ph->param_type);
339 #if defined(INET) || defined(INET6)
340 	param_length = ntohs(ph->param_length);
341 	if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
342 		return (NULL);
343 	}
344 #endif
345 	sa = &store.sa;
346 	switch (param_type) {
347 #ifdef INET
348 	case SCTP_IPV4_ADDRESS:
349 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
350 			/* invalid param size */
351 			return (NULL);
352 		}
353 		v4addr = (struct sctp_ipv4addr_param *)ph;
354 		sin = &store.sin;
355 		memset(sin, 0, sizeof(*sin));
356 		sin->sin_family = AF_INET;
357 		sin->sin_len = sizeof(struct sockaddr_in);
358 		sin->sin_port = stcb->rport;
359 		sin->sin_addr.s_addr = v4addr->addr;
360 		if (sin->sin_addr.s_addr == INADDR_ANY)
361 			zero_address = 1;
362 		SCTPDBG(SCTP_DEBUG_ASCONF1,
363 		    "process_asconf_delete_ip: deleting ");
364 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
365 		break;
366 #endif
367 #ifdef INET6
368 	case SCTP_IPV6_ADDRESS:
369 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
370 			/* invalid param size */
371 			return (NULL);
372 		}
373 		v6addr = (struct sctp_ipv6addr_param *)ph;
374 		sin6 = &store.sin6;
375 		memset(sin6, 0, sizeof(*sin6));
376 		sin6->sin6_family = AF_INET6;
377 		sin6->sin6_len = sizeof(struct sockaddr_in6);
378 		sin6->sin6_port = stcb->rport;
379 		memcpy(&sin6->sin6_addr, v6addr->addr,
380 		    sizeof(struct in6_addr));
381 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
382 			zero_address = 1;
383 		SCTPDBG(SCTP_DEBUG_ASCONF1,
384 		    "process_asconf_delete_ip: deleting ");
385 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
386 		break;
387 #endif
388 	default:
389 		m_reply = sctp_asconf_error_response(aph->correlation_id,
390 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *)aph,
391 		    aparam_length);
392 		return (m_reply);
393 	}
394 
395 	/* make sure the source address is not being deleted */
396 	if (sctp_cmpaddr(sa, src)) {
397 		/* trying to delete the source address! */
398 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
399 		m_reply = sctp_asconf_error_response(aph->correlation_id,
400 		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *)aph,
401 		    aparam_length);
402 		return (m_reply);
403 	}
404 
405 	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
406 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
407 		result = sctp_asconf_del_remote_addrs_except(stcb, src);
408 
409 		if (result) {
410 			/* src address did not exist? */
411 			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
412 			/* what error to reply with?? */
413 			m_reply =
414 			    sctp_asconf_error_response(aph->correlation_id,
415 			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *)aph,
416 			    aparam_length);
417 		} else if (response_required) {
418 			m_reply =
419 			    sctp_asconf_success_response(aph->correlation_id);
420 		}
421 		return (m_reply);
422 	}
423 
424 	/* delete the address */
425 	result = sctp_del_remote_addr(stcb, sa);
426 	/*
427 	 * note if result == -2, the address doesn't exist in the asoc but
428 	 * since it's being deleted anyways, we just ack the delete -- but
429 	 * this probably means something has already gone awry
430 	 */
431 	if (result == -1) {
432 		/* only one address in the asoc */
433 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
434 		m_reply = sctp_asconf_error_response(aph->correlation_id,
435 		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *)aph,
436 		    aparam_length);
437 	} else {
438 		if (response_required) {
439 			m_reply = sctp_asconf_success_response(aph->correlation_id);
440 		}
441 		/* notify upper layer */
442 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
443 	}
444 	return (m_reply);
445 }
446 
447 static struct mbuf *
448 sctp_process_asconf_set_primary(struct sockaddr *src,
449     struct sctp_asconf_paramhdr *aph,
450     struct sctp_tcb *stcb, int response_required)
451 {
452 	struct mbuf *m_reply = NULL;
453 	union sctp_sockstore store;
454 	struct sctp_paramhdr *ph;
455 	uint16_t param_type, aparam_length;
456 #if defined(INET) || defined(INET6)
457 	uint16_t param_length;
458 #endif
459 	struct sockaddr *sa;
460 	int zero_address = 0;
461 #ifdef INET
462 	struct sockaddr_in *sin;
463 	struct sctp_ipv4addr_param *v4addr;
464 #endif
465 #ifdef INET6
466 	struct sockaddr_in6 *sin6;
467 	struct sctp_ipv6addr_param *v6addr;
468 #endif
469 
470 	aparam_length = ntohs(aph->ph.param_length);
471 	if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
472 		return (NULL);
473 	}
474 	ph = (struct sctp_paramhdr *)(aph + 1);
475 	param_type = ntohs(ph->param_type);
476 #if defined(INET) || defined(INET6)
477 	param_length = ntohs(ph->param_length);
478 	if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
479 		return (NULL);
480 	}
481 #endif
482 	sa = &store.sa;
483 	switch (param_type) {
484 #ifdef INET
485 	case SCTP_IPV4_ADDRESS:
486 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
487 			/* invalid param size */
488 			return (NULL);
489 		}
490 		v4addr = (struct sctp_ipv4addr_param *)ph;
491 		sin = &store.sin;
492 		memset(sin, 0, sizeof(*sin));
493 		sin->sin_family = AF_INET;
494 		sin->sin_len = sizeof(struct sockaddr_in);
495 		sin->sin_addr.s_addr = v4addr->addr;
496 		if (sin->sin_addr.s_addr == INADDR_ANY)
497 			zero_address = 1;
498 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
499 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
500 		break;
501 #endif
502 #ifdef INET6
503 	case SCTP_IPV6_ADDRESS:
504 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
505 			/* invalid param size */
506 			return (NULL);
507 		}
508 		v6addr = (struct sctp_ipv6addr_param *)ph;
509 		sin6 = &store.sin6;
510 		memset(sin6, 0, sizeof(*sin6));
511 		sin6->sin6_family = AF_INET6;
512 		sin6->sin6_len = sizeof(struct sockaddr_in6);
513 		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
514 		    sizeof(struct in6_addr));
515 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
516 			zero_address = 1;
517 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
518 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
519 		break;
520 #endif
521 	default:
522 		m_reply = sctp_asconf_error_response(aph->correlation_id,
523 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *)aph,
524 		    aparam_length);
525 		return (m_reply);
526 	}
527 
528 	/* if 0.0.0.0/::0, use the source address instead */
529 	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
530 		sa = src;
531 		SCTPDBG(SCTP_DEBUG_ASCONF1,
532 		    "process_asconf_set_primary: using source addr ");
533 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
534 	}
535 	/* set the primary address */
536 	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
537 		SCTPDBG(SCTP_DEBUG_ASCONF1,
538 		    "process_asconf_set_primary: primary address set\n");
539 		/* notify upper layer */
540 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
541 		if ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_REACHABLE) &&
542 		    ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_PF) == 0) &&
543 		    (stcb->asoc.alternate != NULL)) {
544 			sctp_free_remote_addr(stcb->asoc.alternate);
545 			stcb->asoc.alternate = NULL;
546 		}
547 		if (response_required) {
548 			m_reply = sctp_asconf_success_response(aph->correlation_id);
549 		}
550 		/*
551 		 * Mobility adaptation. Ideally, when the reception of SET
552 		 * PRIMARY with DELETE IP ADDRESS of the previous primary
553 		 * destination, unacknowledged DATA are retransmitted
554 		 * immediately to the new primary destination for seamless
555 		 * handover. If the destination is UNCONFIRMED and marked to
556 		 * REQ_PRIM, The retransmission occur when reception of the
557 		 * HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
558 		 * sctp_input.c) Also, when change of the primary
559 		 * destination, it is better that all subsequent new DATA
560 		 * containing already queued DATA are transmitted to the new
561 		 * primary destination. (by micchie)
562 		 */
563 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
564 		    SCTP_MOBILITY_BASE) ||
565 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
566 		    SCTP_MOBILITY_FASTHANDOFF)) &&
567 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
568 		    SCTP_MOBILITY_PRIM_DELETED) &&
569 		    (stcb->asoc.primary_destination->dest_state &
570 		    SCTP_ADDR_UNCONFIRMED) == 0) {
571 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
572 			    stcb->sctp_ep, stcb, NULL,
573 			    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
574 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
575 			    SCTP_MOBILITY_FASTHANDOFF)) {
576 				sctp_assoc_immediate_retrans(stcb,
577 				    stcb->asoc.primary_destination);
578 			}
579 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
580 			    SCTP_MOBILITY_BASE)) {
581 				sctp_move_chunks_from_net(stcb,
582 				    stcb->asoc.deleted_primary);
583 			}
584 			sctp_delete_prim_timer(stcb->sctp_ep, stcb);
585 		}
586 	} else {
587 		/* couldn't set the requested primary address! */
588 		SCTPDBG(SCTP_DEBUG_ASCONF1,
589 		    "process_asconf_set_primary: set primary failed!\n");
590 		/* must have been an invalid address, so report */
591 		m_reply = sctp_asconf_error_response(aph->correlation_id,
592 		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *)aph,
593 		    aparam_length);
594 	}
595 
596 	return (m_reply);
597 }
598 
599 /*
600  * handles an ASCONF chunk.
601  * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
602  */
603 void
604 sctp_handle_asconf(struct mbuf *m, unsigned int offset,
605     struct sockaddr *src,
606     struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
607     int first)
608 {
609 	struct sctp_association *asoc;
610 	uint32_t serial_num;
611 	struct mbuf *n, *m_ack, *m_result, *m_tail;
612 	struct sctp_asconf_ack_chunk *ack_cp;
613 	struct sctp_asconf_paramhdr *aph;
614 	struct sctp_ipv6addr_param *p_addr;
615 	unsigned int asconf_limit, cnt;
616 	int error = 0;		/* did an error occur? */
617 
618 	/* asconf param buffer */
619 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
620 	struct sctp_asconf_ack *ack, *ack_next;
621 
622 	/* verify minimum length */
623 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
624 		SCTPDBG(SCTP_DEBUG_ASCONF1,
625 		    "handle_asconf: chunk too small = %xh\n",
626 		    ntohs(cp->ch.chunk_length));
627 		return;
628 	}
629 	asoc = &stcb->asoc;
630 	serial_num = ntohl(cp->serial_number);
631 
632 	if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
633 		/* got a duplicate ASCONF */
634 		SCTPDBG(SCTP_DEBUG_ASCONF1,
635 		    "handle_asconf: got duplicate serial number = %xh\n",
636 		    serial_num);
637 		return;
638 	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
639 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
640 		    serial_num, asoc->asconf_seq_in + 1);
641 		return;
642 	}
643 
644 	/* it's the expected "next" sequence number, so process it */
645 	asoc->asconf_seq_in = serial_num;	/* update sequence */
646 	/* get length of all the param's in the ASCONF */
647 	asconf_limit = offset + ntohs(cp->ch.chunk_length);
648 	SCTPDBG(SCTP_DEBUG_ASCONF1,
649 	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
650 	    asconf_limit, serial_num);
651 
652 	if (first) {
653 		/* delete old cache */
654 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: Now processing first ASCONF. Try to delete old cache\n");
655 
656 		TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
657 			if (ack->serial_number == serial_num)
658 				break;
659 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: delete old(%u) < first(%u)\n",
660 			    ack->serial_number, serial_num);
661 			TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
662 			if (ack->data != NULL) {
663 				sctp_m_freem(ack->data);
664 			}
665 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
666 		}
667 	}
668 
669 	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
670 	    M_NOWAIT, 1, MT_DATA);
671 	if (m_ack == NULL) {
672 		SCTPDBG(SCTP_DEBUG_ASCONF1,
673 		    "handle_asconf: couldn't get mbuf!\n");
674 		return;
675 	}
676 	m_tail = m_ack;		/* current reply chain's tail */
677 
678 	/* fill in ASCONF-ACK header */
679 	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
680 	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
681 	ack_cp->ch.chunk_flags = 0;
682 	ack_cp->serial_number = htonl(serial_num);
683 	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
684 	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
685 	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
686 
687 	/* skip the lookup address parameter */
688 	offset += sizeof(struct sctp_asconf_chunk);
689 	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
690 	if (p_addr == NULL) {
691 		SCTPDBG(SCTP_DEBUG_ASCONF1,
692 		    "handle_asconf: couldn't get lookup addr!\n");
693 		/* respond with a missing/invalid mandatory parameter error */
694 		sctp_m_freem(m_ack);
695 		return;
696 	}
697 	/* skip lookup addr */
698 	offset += SCTP_SIZE32(ntohs(p_addr->ph.param_length));
699 	/* get pointer to first asconf param in ASCONF */
700 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
701 	if (aph == NULL) {
702 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
703 		goto send_reply;
704 	}
705 	/* process through all parameters */
706 	cnt = 0;
707 	while (aph != NULL) {
708 		unsigned int param_length, param_type;
709 
710 		param_type = ntohs(aph->ph.param_type);
711 		param_length = ntohs(aph->ph.param_length);
712 		if (offset + param_length > asconf_limit) {
713 			/* parameter goes beyond end of chunk! */
714 			sctp_m_freem(m_ack);
715 			return;
716 		}
717 		m_result = NULL;
718 
719 		if (param_length > sizeof(aparam_buf)) {
720 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
721 			sctp_m_freem(m_ack);
722 			return;
723 		}
724 		if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
725 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
726 			sctp_m_freem(m_ack);
727 			return;
728 		}
729 		/* get the entire parameter */
730 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
731 		if (aph == NULL) {
732 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
733 			sctp_m_freem(m_ack);
734 			return;
735 		}
736 		switch (param_type) {
737 		case SCTP_ADD_IP_ADDRESS:
738 			m_result = sctp_process_asconf_add_ip(src, aph, stcb,
739 			    (cnt < SCTP_BASE_SYSCTL(sctp_hb_maxburst)), error);
740 			cnt++;
741 			break;
742 		case SCTP_DEL_IP_ADDRESS:
743 			m_result = sctp_process_asconf_delete_ip(src, aph, stcb,
744 			    error);
745 			break;
746 		case SCTP_ERROR_CAUSE_IND:
747 			/* not valid in an ASCONF chunk */
748 			break;
749 		case SCTP_SET_PRIM_ADDR:
750 			m_result = sctp_process_asconf_set_primary(src, aph,
751 			    stcb, error);
752 			break;
753 		case SCTP_NAT_VTAGS:
754 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
755 			break;
756 		case SCTP_SUCCESS_REPORT:
757 			/* not valid in an ASCONF chunk */
758 			break;
759 		case SCTP_ULP_ADAPTATION:
760 			/* FIX */
761 			break;
762 		default:
763 			if ((param_type & 0x8000) == 0) {
764 				/* Been told to STOP at this param */
765 				asconf_limit = offset;
766 				/*
767 				 * FIX FIX - We need to call
768 				 * sctp_arethere_unrecognized_parameters()
769 				 * to get a operr and send it for any
770 				 * param's with the 0x4000 bit set OR do it
771 				 * here ourselves... note we still must STOP
772 				 * if the 0x8000 bit is clear.
773 				 */
774 			}
775 			/* unknown/invalid param type */
776 			break;
777 		}		/* switch */
778 
779 		/* add any (error) result to the reply mbuf chain */
780 		if (m_result != NULL) {
781 			SCTP_BUF_NEXT(m_tail) = m_result;
782 			m_tail = m_result;
783 			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
784 			/* set flag to force success reports */
785 			error = 1;
786 		}
787 		offset += SCTP_SIZE32(param_length);
788 		/* update remaining ASCONF message length to process */
789 		if (offset >= asconf_limit) {
790 			/* no more data in the mbuf chain */
791 			break;
792 		}
793 		/* get pointer to next asconf param */
794 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
795 		    sizeof(struct sctp_asconf_paramhdr),
796 		    (uint8_t *)&aparam_buf);
797 		if (aph == NULL) {
798 			/* can't get an asconf paramhdr */
799 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
800 			/* FIX ME - add error here... */
801 		}
802 	}
803 
804 send_reply:
805 	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
806 	/* save the ASCONF-ACK reply */
807 	ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
808 	    struct sctp_asconf_ack);
809 	if (ack == NULL) {
810 		sctp_m_freem(m_ack);
811 		return;
812 	}
813 	ack->serial_number = serial_num;
814 	ack->last_sent_to = NULL;
815 	ack->data = m_ack;
816 	ack->len = 0;
817 	for (n = m_ack; n != NULL; n = SCTP_BUF_NEXT(n)) {
818 		ack->len += SCTP_BUF_LEN(n);
819 	}
820 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
821 
822 	/* see if last_control_chunk_from is set properly (use IP src addr) */
823 	if (stcb->asoc.last_control_chunk_from == NULL) {
824 		/*
825 		 * this could happen if the source address was just newly
826 		 * added
827 		 */
828 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
829 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
830 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
831 		/* look up the from address */
832 		stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, src);
833 #ifdef SCTP_DEBUG
834 		if (stcb->asoc.last_control_chunk_from == NULL) {
835 			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
836 		}
837 #endif
838 	}
839 }
840 
841 /*
842  * does the address match? returns 0 if not, 1 if so
843  */
844 static uint32_t
845 sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
846 {
847 	switch (sa->sa_family) {
848 #ifdef INET6
849 	case AF_INET6:
850 		{
851 			/* XXX scopeid */
852 			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
853 
854 			if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
855 			    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
856 			    sizeof(struct in6_addr)) == 0)) {
857 				return (1);
858 			}
859 			break;
860 		}
861 #endif
862 #ifdef INET
863 	case AF_INET:
864 		{
865 			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
866 
867 			if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
868 			    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
869 			    sizeof(struct in_addr)) == 0)) {
870 				return (1);
871 			}
872 			break;
873 		}
874 #endif
875 	default:
876 		break;
877 	}
878 	return (0);
879 }
880 
881 /*
882  * does the address match? returns 0 if not, 1 if so
883  */
884 static uint32_t
885 sctp_addr_match(struct sctp_paramhdr *ph, struct sockaddr *sa)
886 {
887 #if defined(INET) || defined(INET6)
888 	uint16_t param_type, param_length;
889 
890 	param_type = ntohs(ph->param_type);
891 	param_length = ntohs(ph->param_length);
892 #endif
893 	switch (sa->sa_family) {
894 #ifdef INET6
895 	case AF_INET6:
896 		{
897 			/* XXX scopeid */
898 			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
899 			struct sctp_ipv6addr_param *v6addr;
900 
901 			v6addr = (struct sctp_ipv6addr_param *)ph;
902 			if ((param_type == SCTP_IPV6_ADDRESS) &&
903 			    (param_length == sizeof(struct sctp_ipv6addr_param)) &&
904 			    (memcmp(&v6addr->addr, &sin6->sin6_addr,
905 			    sizeof(struct in6_addr)) == 0)) {
906 				return (1);
907 			}
908 			break;
909 		}
910 #endif
911 #ifdef INET
912 	case AF_INET:
913 		{
914 			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
915 			struct sctp_ipv4addr_param *v4addr;
916 
917 			v4addr = (struct sctp_ipv4addr_param *)ph;
918 			if ((param_type == SCTP_IPV4_ADDRESS) &&
919 			    (param_length == sizeof(struct sctp_ipv4addr_param)) &&
920 			    (memcmp(&v4addr->addr, &sin->sin_addr,
921 			    sizeof(struct in_addr)) == 0)) {
922 				return (1);
923 			}
924 			break;
925 		}
926 #endif
927 	default:
928 		break;
929 	}
930 	return (0);
931 }
932 
933 /*
934  * Cleanup for non-responded/OP ERR'd ASCONF
935  */
936 void
937 sctp_asconf_cleanup(struct sctp_tcb *stcb)
938 {
939 	/*
940 	 * clear out any existing asconfs going out
941 	 */
942 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
943 	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
944 	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
945 	/* remove the old ASCONF on our outbound queue */
946 	sctp_toss_old_asconf(stcb);
947 }
948 
949 /*
950  * cleanup any cached source addresses that may be topologically
951  * incorrect after a new address has been added to this interface.
952  */
953 static void
954 sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
955 {
956 	struct sctp_nets *net;
957 
958 	/*
959 	 * Ideally, we want to only clear cached routes and source addresses
960 	 * that are topologically incorrect.  But since there is no easy way
961 	 * to know whether the newly added address on the ifn would cause a
962 	 * routing change (i.e. a new egress interface would be chosen)
963 	 * without doing a new routing lookup and source address selection,
964 	 * we will (for now) just flush any cached route using a different
965 	 * ifn (and cached source addrs) and let output re-choose them
966 	 * during the next send on that net.
967 	 */
968 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
969 		/*
970 		 * clear any cached route (and cached source address) if the
971 		 * route's interface is NOT the same as the address change.
972 		 * If it's the same interface, just clear the cached source
973 		 * address.
974 		 */
975 		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
976 		    ((ifn == NULL) ||
977 		    (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
978 			/* clear any cached route */
979 			RO_NHFREE(&net->ro);
980 		}
981 		/* clear any cached source address */
982 		if (net->src_addr_selected) {
983 			sctp_free_ifa(net->ro._s_addr);
984 			net->ro._s_addr = NULL;
985 			net->src_addr_selected = 0;
986 		}
987 	}
988 }
989 
990 void
991 sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
992 {
993 	int error;
994 
995 	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
996 		return;
997 	}
998 	if (stcb->asoc.deleted_primary == NULL) {
999 		return;
1000 	}
1001 
1002 	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
1003 		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
1004 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1005 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
1006 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
1007 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1008 		    stcb->asoc.deleted_primary,
1009 		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1010 		stcb->asoc.num_send_timers_up--;
1011 		if (stcb->asoc.num_send_timers_up < 0) {
1012 			stcb->asoc.num_send_timers_up = 0;
1013 		}
1014 		SCTP_TCB_LOCK_ASSERT(stcb);
1015 		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1016 		    stcb->asoc.deleted_primary);
1017 		if (error) {
1018 			SCTP_INP_DECR_REF(stcb->sctp_ep);
1019 			return;
1020 		}
1021 		SCTP_TCB_LOCK_ASSERT(stcb);
1022 #ifdef SCTP_AUDITING_ENABLED
1023 		sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
1024 #endif
1025 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1026 		if ((stcb->asoc.num_send_timers_up == 0) &&
1027 		    (stcb->asoc.sent_queue_cnt > 0)) {
1028 			struct sctp_tmit_chunk *chk;
1029 
1030 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1031 				if (chk->whoTo != NULL) {
1032 					break;
1033 				}
1034 			}
1035 			if (chk != NULL) {
1036 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
1037 			}
1038 		}
1039 	}
1040 	return;
1041 }
1042 
1043 static int
1044     sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
1045 
1046 void
1047 sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
1048 {
1049 	struct sctp_tmit_chunk *chk;
1050 
1051 	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
1052 	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
1053 	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_4);
1054 	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
1055 	net->error_count = 0;
1056 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1057 		if (chk->whoTo == net) {
1058 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
1059 				chk->sent = SCTP_DATAGRAM_RESEND;
1060 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1061 				sctp_flight_size_decrease(chk);
1062 				sctp_total_flight_decrease(stcb, chk);
1063 				net->marked_retrans++;
1064 				stcb->asoc.marked_retrans++;
1065 			}
1066 		}
1067 	}
1068 	if (net->marked_retrans) {
1069 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1070 	}
1071 }
1072 
1073 static void
1074 sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
1075 {
1076 	struct sctp_nets *net;
1077 	int addrnum, changed;
1078 
1079 	/*
1080 	 * If number of local valid addresses is 1, the valid address is
1081 	 * probably newly added address. Several valid addresses in this
1082 	 * association.  A source address may not be changed.  Additionally,
1083 	 * they can be configured on a same interface as "alias" addresses.
1084 	 * (by micchie)
1085 	 */
1086 	addrnum = sctp_local_addr_count(stcb);
1087 	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
1088 	    addrnum);
1089 	if (addrnum == 1) {
1090 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1091 			/* clear any cached route and source address */
1092 			RO_NHFREE(&net->ro);
1093 			if (net->src_addr_selected) {
1094 				sctp_free_ifa(net->ro._s_addr);
1095 				net->ro._s_addr = NULL;
1096 				net->src_addr_selected = 0;
1097 			}
1098 			/* Retransmit unacknowledged DATA chunks immediately */
1099 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1100 			    SCTP_MOBILITY_FASTHANDOFF)) {
1101 				sctp_net_immediate_retrans(stcb, net);
1102 			}
1103 			/* also, SET PRIMARY is maybe already sent */
1104 		}
1105 		return;
1106 	}
1107 
1108 	/* Multiple local addresses exist in the association.  */
1109 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1110 		/* clear any cached route and source address */
1111 		RO_NHFREE(&net->ro);
1112 		if (net->src_addr_selected) {
1113 			sctp_free_ifa(net->ro._s_addr);
1114 			net->ro._s_addr = NULL;
1115 			net->src_addr_selected = 0;
1116 		}
1117 		/*
1118 		 * Check if the nexthop is corresponding to the new address.
1119 		 * If the new address is corresponding to the current
1120 		 * nexthop, the path will be changed. If the new address is
1121 		 * NOT corresponding to the current nexthop, the path will
1122 		 * not be changed.
1123 		 */
1124 		SCTP_RTALLOC((sctp_route_t *)&net->ro,
1125 		    stcb->sctp_ep->def_vrf_id,
1126 		    stcb->sctp_ep->fibnum);
1127 		if (net->ro.ro_nh == NULL)
1128 			continue;
1129 
1130 		changed = 0;
1131 		switch (net->ro._l_addr.sa.sa_family) {
1132 #ifdef INET
1133 		case AF_INET:
1134 			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *)&net->ro)) {
1135 				changed = 1;
1136 			}
1137 			break;
1138 #endif
1139 #ifdef INET6
1140 		case AF_INET6:
1141 			if (sctp_v6src_match_nexthop(
1142 			    &newifa->address.sin6, (sctp_route_t *)&net->ro)) {
1143 				changed = 1;
1144 			}
1145 			break;
1146 #endif
1147 		default:
1148 			break;
1149 		}
1150 		/*
1151 		 * if the newly added address does not relate routing
1152 		 * information, we skip.
1153 		 */
1154 		if (changed == 0)
1155 			continue;
1156 		/* Retransmit unacknowledged DATA chunks immediately */
1157 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1158 		    SCTP_MOBILITY_FASTHANDOFF)) {
1159 			sctp_net_immediate_retrans(stcb, net);
1160 		}
1161 		/* Send SET PRIMARY for this new address */
1162 		if (net == stcb->asoc.primary_destination) {
1163 			(void)sctp_asconf_queue_mgmt(stcb, newifa,
1164 			    SCTP_SET_PRIM_ADDR);
1165 		}
1166 	}
1167 }
1168 
1169 /*
1170  * process an ADD/DELETE IP ack from peer.
1171  * addr: corresponding sctp_ifa to the address being added/deleted.
1172  * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1173  * flag: 1=success, 0=failure.
1174  */
1175 static void
1176 sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr, uint32_t flag)
1177 {
1178 	/*
1179 	 * do the necessary asoc list work- if we get a failure indication,
1180 	 * leave the address on the assoc's restricted list.  If we get a
1181 	 * success indication, remove the address from the restricted list.
1182 	 */
1183 	/*
1184 	 * Note: this will only occur for ADD_IP_ADDRESS, since
1185 	 * DEL_IP_ADDRESS is never actually added to the list...
1186 	 */
1187 	if (flag) {
1188 		/* success case, so remove from the restricted list */
1189 		sctp_del_local_addr_restricted(stcb, addr);
1190 
1191 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1192 		    SCTP_MOBILITY_BASE) ||
1193 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1194 		    SCTP_MOBILITY_FASTHANDOFF)) {
1195 			sctp_path_check_and_react(stcb, addr);
1196 			return;
1197 		}
1198 		/* clear any cached/topologically incorrect source addresses */
1199 		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1200 	}
1201 	/* else, leave it on the list */
1202 }
1203 
1204 /*
1205  * add an asconf add/delete/set primary IP address parameter to the queue.
1206  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1207  * returns 0 if queued, -1 if not queued/removed.
1208  * NOTE: if adding, but a delete for the same address is already scheduled
1209  * (and not yet sent out), simply remove it from queue.  Same for deleting
1210  * an address already scheduled for add.  If a duplicate operation is found,
1211  * ignore the new one.
1212  */
1213 static int
1214 sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1215     uint16_t type)
1216 {
1217 	struct sctp_asconf_addr *aa, *aa_next;
1218 
1219 	/* make sure the request isn't already in the queue */
1220 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1221 		/* address match? */
1222 		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1223 			continue;
1224 		/*
1225 		 * is the request already in queue but not sent? pass the
1226 		 * request already sent in order to resolve the following
1227 		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1228 		 * can't remove the ADD request already sent 3. arrival of
1229 		 * ADD
1230 		 */
1231 		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1232 			return (-1);
1233 		}
1234 		/* is the negative request already in queue, and not sent */
1235 		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
1236 		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1237 			/* add requested, delete already queued */
1238 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1239 			/* remove the ifa from the restricted list */
1240 			sctp_del_local_addr_restricted(stcb, ifa);
1241 			/* free the asconf param */
1242 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1243 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
1244 			return (-1);
1245 		}
1246 		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
1247 		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
1248 			/* delete requested, add already queued */
1249 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1250 			/* remove the aa->ifa from the restricted list */
1251 			sctp_del_local_addr_restricted(stcb, aa->ifa);
1252 			/* free the asconf param */
1253 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1254 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1255 			return (-1);
1256 		}
1257 	}			/* for each aa */
1258 
1259 	/* adding new request to the queue */
1260 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1261 	    SCTP_M_ASC_ADDR);
1262 	if (aa == NULL) {
1263 		/* didn't get memory */
1264 		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1265 		return (-1);
1266 	}
1267 	aa->special_del = 0;
1268 	/* fill in asconf address parameter fields */
1269 	/* top level elements are "networked" during send */
1270 	aa->ap.aph.ph.param_type = type;
1271 	aa->ifa = ifa;
1272 	atomic_add_int(&ifa->refcount, 1);
1273 	/* correlation_id filled in during send routine later... */
1274 	switch (ifa->address.sa.sa_family) {
1275 #ifdef INET6
1276 	case AF_INET6:
1277 		{
1278 			struct sockaddr_in6 *sin6;
1279 
1280 			sin6 = &ifa->address.sin6;
1281 			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1282 			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1283 			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1284 			    sizeof(struct sctp_ipv6addr_param);
1285 			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1286 			    sizeof(struct in6_addr));
1287 			break;
1288 		}
1289 #endif
1290 #ifdef INET
1291 	case AF_INET:
1292 		{
1293 			struct sockaddr_in *sin;
1294 
1295 			sin = &ifa->address.sin;
1296 			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1297 			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1298 			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1299 			    sizeof(struct sctp_ipv4addr_param);
1300 			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1301 			    sizeof(struct in_addr));
1302 			break;
1303 		}
1304 #endif
1305 	default:
1306 		/* invalid family! */
1307 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1308 		sctp_free_ifa(ifa);
1309 		return (-1);
1310 	}
1311 	aa->sent = 0;		/* clear sent flag */
1312 
1313 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1314 #ifdef SCTP_DEBUG
1315 	if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
1316 		if (type == SCTP_ADD_IP_ADDRESS) {
1317 			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1318 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1319 		} else if (type == SCTP_DEL_IP_ADDRESS) {
1320 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1321 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1322 		} else {
1323 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1324 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1325 		}
1326 	}
1327 #endif
1328 
1329 	return (0);
1330 }
1331 
1332 /*
1333  * add an asconf operation for the given ifa and type.
1334  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1335  * returns 0 if completed, -1 if not completed, 1 if immediate send is
1336  * advisable.
1337  */
1338 static int
1339 sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1340     uint16_t type)
1341 {
1342 	uint32_t status;
1343 	int pending_delete_queued = 0;
1344 	int last;
1345 
1346 	/* see if peer supports ASCONF */
1347 	if (stcb->asoc.asconf_supported == 0) {
1348 		return (-1);
1349 	}
1350 
1351 	/*
1352 	 * if this is deleting the last address from the assoc, mark it as
1353 	 * pending.
1354 	 */
1355 	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending) {
1356 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1357 			last = (sctp_local_addr_count(stcb) == 0);
1358 		} else {
1359 			last = (sctp_local_addr_count(stcb) == 1);
1360 		}
1361 		if (last) {
1362 			/* set the pending delete info only */
1363 			stcb->asoc.asconf_del_pending = 1;
1364 			stcb->asoc.asconf_addr_del_pending = ifa;
1365 			atomic_add_int(&ifa->refcount, 1);
1366 			SCTPDBG(SCTP_DEBUG_ASCONF2,
1367 			    "asconf_queue_add: mark delete last address pending\n");
1368 			return (-1);
1369 		}
1370 	}
1371 
1372 	/* queue an asconf parameter */
1373 	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1374 
1375 	/*
1376 	 * if this is an add, and there is a delete also pending (i.e. the
1377 	 * last local address is being changed), queue the pending delete
1378 	 * too.
1379 	 */
1380 	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
1381 		/* queue in the pending delete */
1382 		if (sctp_asconf_queue_mgmt(stcb,
1383 		    stcb->asoc.asconf_addr_del_pending,
1384 		    SCTP_DEL_IP_ADDRESS) == 0) {
1385 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queuing pending delete\n");
1386 			pending_delete_queued = 1;
1387 			/* clear out the pending delete info */
1388 			stcb->asoc.asconf_del_pending = 0;
1389 			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
1390 			stcb->asoc.asconf_addr_del_pending = NULL;
1391 		}
1392 	}
1393 
1394 	if (pending_delete_queued) {
1395 		struct sctp_nets *net;
1396 
1397 		/*
1398 		 * since we know that the only/last address is now being
1399 		 * changed in this case, reset the cwnd/rto on all nets to
1400 		 * start as a new address and path.  Also clear the error
1401 		 * counts to give the assoc the best chance to complete the
1402 		 * address change.
1403 		 */
1404 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1405 			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
1406 			    net);
1407 			net->RTO = 0;
1408 			net->error_count = 0;
1409 		}
1410 		stcb->asoc.overall_error_count = 0;
1411 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1412 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1413 			    stcb->asoc.overall_error_count,
1414 			    0,
1415 			    SCTP_FROM_SCTP_ASCONF,
1416 			    __LINE__);
1417 		}
1418 
1419 		/* queue in an advisory set primary too */
1420 		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
1421 		/* let caller know we should send this out immediately */
1422 		status = 1;
1423 	}
1424 	return (status);
1425 }
1426 
1427 /*-
1428  * add an asconf delete IP address parameter to the queue by sockaddr and
1429  * possibly with no sctp_ifa available.  This is only called by the routine
1430  * that checks the addresses in an INIT-ACK against the current address list.
1431  * returns 0 if completed, non-zero if not completed.
1432  * NOTE: if an add is already scheduled (and not yet sent out), simply
1433  * remove it from queue.  If a duplicate operation is found, ignore the
1434  * new one.
1435  */
1436 static int
1437 sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1438 {
1439 	struct sctp_ifa *ifa;
1440 	struct sctp_asconf_addr *aa, *aa_next;
1441 
1442 	if (stcb == NULL) {
1443 		return (-1);
1444 	}
1445 	/* see if peer supports ASCONF */
1446 	if (stcb->asoc.asconf_supported == 0) {
1447 		return (-1);
1448 	}
1449 	/* make sure the request isn't already in the queue */
1450 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1451 		/* address match? */
1452 		if (sctp_asconf_addr_match(aa, sa) == 0)
1453 			continue;
1454 		/* is the request already in queue (sent or not) */
1455 		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1456 			return (-1);
1457 		}
1458 		/* is the negative request already in queue, and not sent */
1459 		if (aa->sent == 1)
1460 			continue;
1461 		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1462 			/* add already queued, so remove existing entry */
1463 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1464 			sctp_del_local_addr_restricted(stcb, aa->ifa);
1465 			/* free the entry */
1466 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1467 			return (-1);
1468 		}
1469 	}			/* for each aa */
1470 
1471 	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1472 	ifa = sctp_find_ifa_by_addr(sa, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
1473 
1474 	/* adding new request to the queue */
1475 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1476 	    SCTP_M_ASC_ADDR);
1477 	if (aa == NULL) {
1478 		/* didn't get memory */
1479 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1480 		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1481 		return (-1);
1482 	}
1483 	aa->special_del = 0;
1484 	/* fill in asconf address parameter fields */
1485 	/* top level elements are "networked" during send */
1486 	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1487 	aa->ifa = ifa;
1488 	if (ifa)
1489 		atomic_add_int(&ifa->refcount, 1);
1490 	/* correlation_id filled in during send routine later... */
1491 	switch (sa->sa_family) {
1492 #ifdef INET6
1493 	case AF_INET6:
1494 		{
1495 			/* IPv6 address */
1496 			struct sockaddr_in6 *sin6;
1497 
1498 			sin6 = (struct sockaddr_in6 *)sa;
1499 			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1500 			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1501 			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1502 			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1503 			    sizeof(struct in6_addr));
1504 			break;
1505 		}
1506 #endif
1507 #ifdef INET
1508 	case AF_INET:
1509 		{
1510 			/* IPv4 address */
1511 			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1512 
1513 			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1514 			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1515 			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1516 			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1517 			    sizeof(struct in_addr));
1518 			break;
1519 		}
1520 #endif
1521 	default:
1522 		/* invalid family! */
1523 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1524 		if (ifa)
1525 			sctp_free_ifa(ifa);
1526 		return (-1);
1527 	}
1528 	aa->sent = 0;		/* clear sent flag */
1529 
1530 	/* delete goes to the back of the queue */
1531 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1532 
1533 	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1534 	return (0);
1535 }
1536 
1537 /*
1538  * find a specific asconf param on our "sent" queue
1539  */
1540 static struct sctp_asconf_addr *
1541 sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1542 {
1543 	struct sctp_asconf_addr *aa;
1544 
1545 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1546 		if (aa->ap.aph.correlation_id == correlation_id &&
1547 		    aa->sent == 1) {
1548 			/* found it */
1549 			return (aa);
1550 		}
1551 	}
1552 	/* didn't find it */
1553 	return (NULL);
1554 }
1555 
1556 /*
1557  * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1558  * notifications based on the error response
1559  */
1560 static void
1561 sctp_asconf_process_error(struct sctp_tcb *stcb SCTP_UNUSED,
1562     struct sctp_asconf_paramhdr *aph)
1563 {
1564 	struct sctp_error_cause *eh;
1565 	struct sctp_paramhdr *ph;
1566 	uint16_t param_type;
1567 	uint16_t error_code;
1568 
1569 	eh = (struct sctp_error_cause *)(aph + 1);
1570 	ph = (struct sctp_paramhdr *)(eh + 1);
1571 	/* validate lengths */
1572 	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1573 	    htons(aph->ph.param_length)) {
1574 		/* invalid error cause length */
1575 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1576 		    "asconf_process_error: cause element too long\n");
1577 		return;
1578 	}
1579 	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1580 	    htons(eh->length)) {
1581 		/* invalid included TLV length */
1582 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1583 		    "asconf_process_error: included TLV too long\n");
1584 		return;
1585 	}
1586 	/* which error code ? */
1587 	error_code = ntohs(eh->code);
1588 	param_type = ntohs(aph->ph.param_type);
1589 	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1590 	switch (error_code) {
1591 	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1592 		/* we allow ourselves to "try again" for this error */
1593 		break;
1594 	default:
1595 		/* peer can't handle it... */
1596 		switch (param_type) {
1597 		case SCTP_ADD_IP_ADDRESS:
1598 		case SCTP_DEL_IP_ADDRESS:
1599 		case SCTP_SET_PRIM_ADDR:
1600 			break;
1601 		default:
1602 			break;
1603 		}
1604 	}
1605 }
1606 
1607 /*
1608  * process an asconf queue param.
1609  * aparam: parameter to process, will be removed from the queue.
1610  * flag: 1=success case, 0=failure case
1611  */
1612 static void
1613 sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1614     struct sctp_asconf_addr *aparam, uint32_t flag)
1615 {
1616 	uint16_t param_type;
1617 
1618 	/* process this param */
1619 	param_type = aparam->ap.aph.ph.param_type;
1620 	switch (param_type) {
1621 	case SCTP_ADD_IP_ADDRESS:
1622 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1623 		    "process_param_ack: added IP address\n");
1624 		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, flag);
1625 		break;
1626 	case SCTP_DEL_IP_ADDRESS:
1627 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1628 		    "process_param_ack: deleted IP address\n");
1629 		/* nothing really to do... lists already updated */
1630 		break;
1631 	case SCTP_SET_PRIM_ADDR:
1632 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1633 		    "process_param_ack: set primary IP address\n");
1634 		/* nothing to do... peer may start using this addr */
1635 		break;
1636 	default:
1637 		/* should NEVER happen */
1638 		break;
1639 	}
1640 
1641 	/* remove the param and free it */
1642 	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1643 	if (aparam->ifa)
1644 		sctp_free_ifa(aparam->ifa);
1645 	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1646 }
1647 
1648 /*
1649  * cleanup from a bad asconf ack parameter
1650  */
1651 static void
1652 sctp_asconf_ack_clear(struct sctp_tcb *stcb SCTP_UNUSED)
1653 {
1654 	/* assume peer doesn't really know how to do asconfs */
1655 	/* XXX we could free the pending queue here */
1656 
1657 }
1658 
1659 void
1660 sctp_handle_asconf_ack(struct mbuf *m, int offset,
1661     struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1662     struct sctp_nets *net, int *abort_no_unlock)
1663 {
1664 	struct sctp_association *asoc;
1665 	uint32_t serial_num;
1666 	uint16_t ack_length;
1667 	struct sctp_asconf_paramhdr *aph;
1668 	struct sctp_asconf_addr *aa, *aa_next;
1669 	uint32_t last_error_id = 0;	/* last error correlation id */
1670 	uint32_t id;
1671 	struct sctp_asconf_addr *ap;
1672 
1673 	/* asconf param buffer */
1674 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1675 
1676 	/* verify minimum length */
1677 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1678 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1679 		    "handle_asconf_ack: chunk too small = %xh\n",
1680 		    ntohs(cp->ch.chunk_length));
1681 		return;
1682 	}
1683 	asoc = &stcb->asoc;
1684 	serial_num = ntohl(cp->serial_number);
1685 
1686 	/*
1687 	 * NOTE: we may want to handle this differently- currently, we will
1688 	 * abort when we get an ack for the expected serial number + 1 (eg.
1689 	 * we didn't send it), process an ack normally if it is the expected
1690 	 * serial number, and re-send the previous ack for *ALL* other
1691 	 * serial numbers
1692 	 */
1693 
1694 	/*
1695 	 * if the serial number is the next expected, but I didn't send it,
1696 	 * abort the asoc, since someone probably just hijacked us...
1697 	 */
1698 	if (serial_num == (asoc->asconf_seq_out + 1)) {
1699 		struct mbuf *op_err;
1700 		char msg[SCTP_DIAG_INFO_LEN];
1701 
1702 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1703 		SCTP_SNPRINTF(msg, sizeof(msg), "Never sent serial number %8.8x", serial_num);
1704 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1705 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
1706 		*abort_no_unlock = 1;
1707 		return;
1708 	}
1709 	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1710 		/* got a duplicate/unexpected ASCONF-ACK */
1711 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1712 		    serial_num, asoc->asconf_seq_out_acked + 1);
1713 		return;
1714 	}
1715 
1716 	if (serial_num == asoc->asconf_seq_out - 1) {
1717 		/* stop our timer */
1718 		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
1719 		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_5);
1720 	}
1721 
1722 	/* process the ASCONF-ACK contents */
1723 	ack_length = ntohs(cp->ch.chunk_length) -
1724 	    sizeof(struct sctp_asconf_ack_chunk);
1725 	offset += sizeof(struct sctp_asconf_ack_chunk);
1726 	/* process through all parameters */
1727 	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1728 		unsigned int param_length, param_type;
1729 
1730 		/* get pointer to next asconf parameter */
1731 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1732 		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1733 		if (aph == NULL) {
1734 			/* can't get an asconf paramhdr */
1735 			sctp_asconf_ack_clear(stcb);
1736 			return;
1737 		}
1738 		param_type = ntohs(aph->ph.param_type);
1739 		param_length = ntohs(aph->ph.param_length);
1740 		if (param_length > ack_length) {
1741 			sctp_asconf_ack_clear(stcb);
1742 			return;
1743 		}
1744 		if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
1745 			sctp_asconf_ack_clear(stcb);
1746 			return;
1747 		}
1748 		/* get the complete parameter... */
1749 		if (param_length > sizeof(aparam_buf)) {
1750 			SCTPDBG(SCTP_DEBUG_ASCONF1,
1751 			    "param length (%u) larger than buffer size!\n", param_length);
1752 			sctp_asconf_ack_clear(stcb);
1753 			return;
1754 		}
1755 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1756 		if (aph == NULL) {
1757 			sctp_asconf_ack_clear(stcb);
1758 			return;
1759 		}
1760 		/* correlation_id is transparent to peer, no ntohl needed */
1761 		id = aph->correlation_id;
1762 
1763 		switch (param_type) {
1764 		case SCTP_ERROR_CAUSE_IND:
1765 			last_error_id = id;
1766 			/* find the corresponding asconf param in our queue */
1767 			ap = sctp_asconf_find_param(stcb, id);
1768 			if (ap == NULL) {
1769 				/* hmm... can't find this in our queue! */
1770 				break;
1771 			}
1772 			/* process the parameter, failed flag */
1773 			sctp_asconf_process_param_ack(stcb, ap, 0);
1774 			/* process the error response */
1775 			sctp_asconf_process_error(stcb, aph);
1776 			break;
1777 		case SCTP_SUCCESS_REPORT:
1778 			/* find the corresponding asconf param in our queue */
1779 			ap = sctp_asconf_find_param(stcb, id);
1780 			if (ap == NULL) {
1781 				/* hmm... can't find this in our queue! */
1782 				break;
1783 			}
1784 			/* process the parameter, success flag */
1785 			sctp_asconf_process_param_ack(stcb, ap, 1);
1786 			break;
1787 		default:
1788 			break;
1789 		}		/* switch */
1790 
1791 		/* update remaining ASCONF-ACK message length to process */
1792 		if (ack_length > SCTP_SIZE32(param_length)) {
1793 			ack_length -= SCTP_SIZE32(param_length);
1794 		} else {
1795 			break;
1796 		}
1797 		offset += SCTP_SIZE32(param_length);
1798 	}			/* while */
1799 
1800 	/*
1801 	 * if there are any "sent" params still on the queue, these are
1802 	 * implicitly "success", or "failed" (if we got an error back) ...
1803 	 * so process these appropriately
1804 	 *
1805 	 * we assume that the correlation_id's are monotonically increasing
1806 	 * beginning from 1 and that we don't have *that* many outstanding
1807 	 * at any given time
1808 	 */
1809 	if (last_error_id == 0)
1810 		last_error_id--;	/* set to "max" value */
1811 	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1812 		if (aa->sent == 1) {
1813 			/*
1814 			 * implicitly successful or failed if correlation_id
1815 			 * < last_error_id, then success else, failure
1816 			 */
1817 			if (aa->ap.aph.correlation_id < last_error_id)
1818 				sctp_asconf_process_param_ack(stcb, aa, 1);
1819 			else
1820 				sctp_asconf_process_param_ack(stcb, aa, 0);
1821 		} else {
1822 			/*
1823 			 * since we always process in order (FIFO queue) if
1824 			 * we reach one that hasn't been sent, the rest
1825 			 * should not have been sent either. so, we're
1826 			 * done...
1827 			 */
1828 			break;
1829 		}
1830 	}
1831 
1832 	/* update the next sequence number to use */
1833 	asoc->asconf_seq_out_acked++;
1834 	/* remove the old ASCONF on our outbound queue */
1835 	sctp_toss_old_asconf(stcb);
1836 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1837 #ifdef SCTP_TIMER_BASED_ASCONF
1838 		/* we have more params, so restart our timer */
1839 		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1840 		    stcb, net);
1841 #else
1842 		/* we have more params, so send out more */
1843 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1844 #endif
1845 	}
1846 }
1847 
1848 #ifdef INET6
1849 static uint32_t
1850 sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1851 {
1852 	struct sockaddr_in6 *sin6, *net6;
1853 	struct sctp_nets *net;
1854 
1855 	if (sa->sa_family != AF_INET6) {
1856 		/* wrong family */
1857 		return (0);
1858 	}
1859 	sin6 = (struct sockaddr_in6 *)sa;
1860 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1861 		/* not link local address */
1862 		return (0);
1863 	}
1864 	/* hunt through our destination nets list for this scope_id */
1865 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1866 		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1867 		    AF_INET6)
1868 			continue;
1869 		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1870 		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1871 			continue;
1872 		if (sctp_is_same_scope(sin6, net6)) {
1873 			/* found one */
1874 			return (1);
1875 		}
1876 	}
1877 	/* didn't find one */
1878 	return (0);
1879 }
1880 #endif
1881 
1882 /*
1883  * address management functions
1884  */
1885 static void
1886 sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1887     struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1888 {
1889 	int status;
1890 
1891 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 ||
1892 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1893 		/* subset bound, no ASCONF allowed case, so ignore */
1894 		return;
1895 	}
1896 	/*
1897 	 * note: we know this is not the subset bound, no ASCONF case eg.
1898 	 * this is boundall or subset bound w/ASCONF allowed
1899 	 */
1900 
1901 	/* first, make sure that the address is IPv4 or IPv6 and not jailed */
1902 	switch (ifa->address.sa.sa_family) {
1903 #ifdef INET6
1904 	case AF_INET6:
1905 		if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1906 		    &ifa->address.sin6.sin6_addr) != 0) {
1907 			return;
1908 		}
1909 		break;
1910 #endif
1911 #ifdef INET
1912 	case AF_INET:
1913 		if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1914 		    &ifa->address.sin.sin_addr) != 0) {
1915 			return;
1916 		}
1917 		break;
1918 #endif
1919 	default:
1920 		return;
1921 	}
1922 #ifdef INET6
1923 	/* make sure we're "allowed" to add this type of addr */
1924 	if (ifa->address.sa.sa_family == AF_INET6) {
1925 		/* invalid if we're not a v6 endpoint */
1926 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1927 			return;
1928 		/* is the v6 addr really valid ? */
1929 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1930 			return;
1931 		}
1932 	}
1933 #endif
1934 	/* put this address on the "pending/do not use yet" list */
1935 	sctp_add_local_addr_restricted(stcb, ifa);
1936 	/*
1937 	 * check address scope if address is out of scope, don't queue
1938 	 * anything... note: this would leave the address on both inp and
1939 	 * asoc lists
1940 	 */
1941 	switch (ifa->address.sa.sa_family) {
1942 #ifdef INET6
1943 	case AF_INET6:
1944 		{
1945 			struct sockaddr_in6 *sin6;
1946 
1947 			sin6 = &ifa->address.sin6;
1948 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1949 				/* we skip unspecified addresses */
1950 				return;
1951 			}
1952 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1953 				if (stcb->asoc.scope.local_scope == 0) {
1954 					return;
1955 				}
1956 				/* is it the right link local scope? */
1957 				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1958 					return;
1959 				}
1960 			}
1961 			if (stcb->asoc.scope.site_scope == 0 &&
1962 			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1963 				return;
1964 			}
1965 			break;
1966 		}
1967 #endif
1968 #ifdef INET
1969 	case AF_INET:
1970 		{
1971 			struct sockaddr_in *sin;
1972 
1973 			/* invalid if we are a v6 only endpoint */
1974 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1975 			    SCTP_IPV6_V6ONLY(inp))
1976 				return;
1977 
1978 			sin = &ifa->address.sin;
1979 			if (sin->sin_addr.s_addr == 0) {
1980 				/* we skip unspecified addresses */
1981 				return;
1982 			}
1983 			if (stcb->asoc.scope.ipv4_local_scope == 0 &&
1984 			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1985 				return;
1986 			}
1987 			break;
1988 		}
1989 #endif
1990 	default:
1991 		/* else, not AF_INET or AF_INET6, so skip */
1992 		return;
1993 	}
1994 
1995 	/* queue an asconf for this address add/delete */
1996 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1997 		/* does the peer do asconf? */
1998 		if (stcb->asoc.asconf_supported) {
1999 			/* queue an asconf for this addr */
2000 			status = sctp_asconf_queue_add(stcb, ifa, type);
2001 
2002 			/*
2003 			 * if queued ok, and in the open state, send out the
2004 			 * ASCONF.  If in the non-open state, these will be
2005 			 * sent when the state goes open.
2006 			 */
2007 			if (status == 0 &&
2008 			    ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
2009 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED))) {
2010 #ifdef SCTP_TIMER_BASED_ASCONF
2011 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
2012 				    stcb, stcb->asoc.primary_destination);
2013 #else
2014 				sctp_send_asconf(stcb, NULL, addr_locked);
2015 #endif
2016 			}
2017 		}
2018 	}
2019 }
2020 
2021 int
2022 sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2023 {
2024 	struct sctp_asconf_iterator *asc;
2025 	struct sctp_ifa *ifa;
2026 	struct sctp_laddr *l;
2027 	int cnt_invalid = 0;
2028 
2029 	asc = (struct sctp_asconf_iterator *)ptr;
2030 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2031 		ifa = l->ifa;
2032 		switch (ifa->address.sa.sa_family) {
2033 #ifdef INET6
2034 		case AF_INET6:
2035 			/* invalid if we're not a v6 endpoint */
2036 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2037 				cnt_invalid++;
2038 				if (asc->cnt == cnt_invalid)
2039 					return (1);
2040 			}
2041 			break;
2042 #endif
2043 #ifdef INET
2044 		case AF_INET:
2045 			{
2046 				/* invalid if we are a v6 only endpoint */
2047 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2048 				    SCTP_IPV6_V6ONLY(inp)) {
2049 					cnt_invalid++;
2050 					if (asc->cnt == cnt_invalid)
2051 						return (1);
2052 				}
2053 				break;
2054 			}
2055 #endif
2056 		default:
2057 			/* invalid address family */
2058 			cnt_invalid++;
2059 			if (asc->cnt == cnt_invalid)
2060 				return (1);
2061 		}
2062 	}
2063 	return (0);
2064 }
2065 
2066 static int
2067 sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2068 {
2069 	struct sctp_ifa *ifa;
2070 	struct sctp_asconf_iterator *asc;
2071 	struct sctp_laddr *laddr, *nladdr, *l;
2072 
2073 	/* Only for specific case not bound all */
2074 	asc = (struct sctp_asconf_iterator *)ptr;
2075 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2076 		ifa = l->ifa;
2077 		if (l->action == SCTP_ADD_IP_ADDRESS) {
2078 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2079 			    sctp_nxt_addr) {
2080 				if (laddr->ifa == ifa) {
2081 					laddr->action = 0;
2082 					break;
2083 				}
2084 			}
2085 		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
2086 			LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
2087 				/* remove only after all guys are done */
2088 				if (laddr->ifa == ifa) {
2089 					sctp_del_local_addr_ep(inp, ifa);
2090 				}
2091 			}
2092 		}
2093 	}
2094 	return (0);
2095 }
2096 
2097 void
2098 sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2099     void *ptr, uint32_t val SCTP_UNUSED)
2100 {
2101 	struct sctp_asconf_iterator *asc;
2102 	struct sctp_ifa *ifa;
2103 	struct sctp_laddr *l;
2104 	int cnt_invalid = 0;
2105 	int type, status;
2106 	int num_queued = 0;
2107 
2108 	asc = (struct sctp_asconf_iterator *)ptr;
2109 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2110 		ifa = l->ifa;
2111 		type = l->action;
2112 
2113 		/* address's vrf_id must be the vrf_id of the assoc */
2114 		if (ifa->vrf_id != stcb->asoc.vrf_id) {
2115 			continue;
2116 		}
2117 
2118 		/* Same checks again for assoc */
2119 		switch (ifa->address.sa.sa_family) {
2120 #ifdef INET6
2121 		case AF_INET6:
2122 			{
2123 				/* invalid if we're not a v6 endpoint */
2124 				struct sockaddr_in6 *sin6;
2125 
2126 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2127 					cnt_invalid++;
2128 					if (asc->cnt == cnt_invalid)
2129 						return;
2130 					else
2131 						continue;
2132 				}
2133 				sin6 = &ifa->address.sin6;
2134 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2135 					/* we skip unspecified addresses */
2136 					continue;
2137 				}
2138 				if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2139 				    &sin6->sin6_addr) != 0) {
2140 					continue;
2141 				}
2142 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2143 					if (stcb->asoc.scope.local_scope == 0) {
2144 						continue;
2145 					}
2146 					/* is it the right link local scope? */
2147 					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2148 						continue;
2149 					}
2150 				}
2151 				break;
2152 			}
2153 #endif
2154 #ifdef INET
2155 		case AF_INET:
2156 			{
2157 				/* invalid if we are a v6 only endpoint */
2158 				struct sockaddr_in *sin;
2159 
2160 				/* invalid if we are a v6 only endpoint */
2161 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2162 				    SCTP_IPV6_V6ONLY(inp))
2163 					continue;
2164 
2165 				sin = &ifa->address.sin;
2166 				if (sin->sin_addr.s_addr == 0) {
2167 					/* we skip unspecified addresses */
2168 					continue;
2169 				}
2170 				if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2171 				    &sin->sin_addr) != 0) {
2172 					continue;
2173 				}
2174 				if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2175 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2176 					continue;
2177 				}
2178 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2179 				    SCTP_IPV6_V6ONLY(inp)) {
2180 					cnt_invalid++;
2181 					if (asc->cnt == cnt_invalid)
2182 						return;
2183 					else
2184 						continue;
2185 				}
2186 				break;
2187 			}
2188 #endif
2189 		default:
2190 			/* invalid address family */
2191 			cnt_invalid++;
2192 			if (asc->cnt == cnt_invalid)
2193 				return;
2194 			else
2195 				continue;
2196 		}
2197 
2198 		if (type == SCTP_ADD_IP_ADDRESS) {
2199 			/* prevent this address from being used as a source */
2200 			sctp_add_local_addr_restricted(stcb, ifa);
2201 		} else if (type == SCTP_DEL_IP_ADDRESS) {
2202 			struct sctp_nets *net;
2203 
2204 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2205 				/* delete this address if cached */
2206 				if (net->ro._s_addr == ifa) {
2207 					sctp_free_ifa(net->ro._s_addr);
2208 					net->ro._s_addr = NULL;
2209 					net->src_addr_selected = 0;
2210 					RO_NHFREE(&net->ro);
2211 					/*
2212 					 * Now we deleted our src address,
2213 					 * should we not also now reset the
2214 					 * cwnd/rto to start as if its a new
2215 					 * address?
2216 					 */
2217 					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2218 					net->RTO = 0;
2219 				}
2220 			}
2221 		} else if (type == SCTP_SET_PRIM_ADDR) {
2222 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2223 				/* must validate the ifa is in the ep */
2224 				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
2225 					continue;
2226 				}
2227 			} else {
2228 				/* Need to check scopes for this guy */
2229 				if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2230 					continue;
2231 				}
2232 			}
2233 		}
2234 		/* queue an asconf for this address add/delete */
2235 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
2236 		    stcb->asoc.asconf_supported == 1) {
2237 			/* queue an asconf for this addr */
2238 			status = sctp_asconf_queue_add(stcb, ifa, type);
2239 			/*
2240 			 * if queued ok, and in the open state, update the
2241 			 * count of queued params.  If in the non-open
2242 			 * state, these get sent when the assoc goes open.
2243 			 */
2244 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
2245 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2246 				if (status >= 0) {
2247 					num_queued++;
2248 				}
2249 			}
2250 		}
2251 	}
2252 	/*
2253 	 * If we have queued params in the open state, send out an ASCONF.
2254 	 */
2255 	if (num_queued > 0) {
2256 		sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2257 	}
2258 }
2259 
2260 void
2261 sctp_asconf_iterator_end(void *ptr, uint32_t val SCTP_UNUSED)
2262 {
2263 	struct sctp_asconf_iterator *asc;
2264 	struct sctp_ifa *ifa;
2265 	struct sctp_laddr *l, *nl;
2266 
2267 	asc = (struct sctp_asconf_iterator *)ptr;
2268 	LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
2269 		ifa = l->ifa;
2270 		if (l->action == SCTP_ADD_IP_ADDRESS) {
2271 			/* Clear the defer use flag */
2272 			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2273 		}
2274 		sctp_free_ifa(ifa);
2275 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
2276 		SCTP_DECR_LADDR_COUNT();
2277 	}
2278 	SCTP_FREE(asc, SCTP_M_ASC_IT);
2279 }
2280 
2281 /*
2282  * sa is the sockaddr to ask the peer to set primary to.
2283  * returns: 0 = completed, -1 = error
2284  */
2285 int32_t
2286 sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2287 {
2288 	uint32_t vrf_id;
2289 	struct sctp_ifa *ifa;
2290 
2291 	/* find the ifa for the desired set primary */
2292 	vrf_id = stcb->asoc.vrf_id;
2293 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
2294 	if (ifa == NULL) {
2295 		/* Invalid address */
2296 		return (-1);
2297 	}
2298 
2299 	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2300 	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2301 		/* set primary queuing succeeded */
2302 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2303 		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2304 		    (void *)stcb);
2305 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2306 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
2307 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2308 #ifdef SCTP_TIMER_BASED_ASCONF
2309 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2310 			    stcb->sctp_ep, stcb,
2311 			    stcb->asoc.primary_destination);
2312 #else
2313 			sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2314 #endif
2315 		}
2316 	} else {
2317 		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2318 		    (void *)stcb);
2319 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2320 		return (-1);
2321 	}
2322 	return (0);
2323 }
2324 
2325 int
2326 sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2327 {
2328 	struct sctp_tmit_chunk *chk, *nchk;
2329 	unsigned int offset, asconf_limit;
2330 	struct sctp_asconf_chunk *acp;
2331 	struct sctp_asconf_paramhdr *aph;
2332 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2333 	struct sctp_paramhdr *ph;
2334 	int add_cnt, del_cnt;
2335 	uint16_t last_param_type;
2336 
2337 	add_cnt = del_cnt = 0;
2338 	last_param_type = 0;
2339 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
2340 		if (chk->data == NULL) {
2341 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2342 			continue;
2343 		}
2344 		offset = 0;
2345 		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2346 		offset += sizeof(struct sctp_asconf_chunk);
2347 		asconf_limit = ntohs(acp->ch.chunk_length);
2348 		ph = (struct sctp_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2349 		if (ph == NULL) {
2350 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2351 			continue;
2352 		}
2353 		offset += ntohs(ph->param_length);
2354 
2355 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2356 		if (aph == NULL) {
2357 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2358 			continue;
2359 		}
2360 		while (aph != NULL) {
2361 			unsigned int param_length, param_type;
2362 
2363 			param_type = ntohs(aph->ph.param_type);
2364 			param_length = ntohs(aph->ph.param_length);
2365 			if (offset + param_length > asconf_limit) {
2366 				/* parameter goes beyond end of chunk! */
2367 				break;
2368 			}
2369 			if (param_length > sizeof(aparam_buf)) {
2370 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2371 				break;
2372 			}
2373 			if (param_length <= sizeof(struct sctp_paramhdr)) {
2374 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2375 				break;
2376 			}
2377 
2378 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2379 			if (aph == NULL) {
2380 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2381 				break;
2382 			}
2383 
2384 			ph = (struct sctp_paramhdr *)(aph + 1);
2385 			if (sctp_addr_match(ph, &sctp_ifa->address.sa) != 0) {
2386 				switch (param_type) {
2387 				case SCTP_ADD_IP_ADDRESS:
2388 					add_cnt++;
2389 					break;
2390 				case SCTP_DEL_IP_ADDRESS:
2391 					del_cnt++;
2392 					break;
2393 				default:
2394 					break;
2395 				}
2396 				last_param_type = param_type;
2397 			}
2398 
2399 			offset += SCTP_SIZE32(param_length);
2400 			if (offset >= asconf_limit) {
2401 				/* no more data in the mbuf chain */
2402 				break;
2403 			}
2404 			/* get pointer to next asconf param */
2405 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2406 		}
2407 	}
2408 
2409 	/*
2410 	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2411 	 * or DEL -> ADD
2412 	 */
2413 	if (add_cnt > del_cnt ||
2414 	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2415 		return (1);
2416 	}
2417 	return (0);
2418 }
2419 
2420 static struct sockaddr *
2421 sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2422 {
2423 	struct sctp_vrf *vrf = NULL;
2424 	struct sctp_ifn *sctp_ifn;
2425 	struct sctp_ifa *sctp_ifa;
2426 
2427 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2428 		SCTP_IPI_ADDR_RLOCK();
2429 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2430 	if (vrf == NULL) {
2431 		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2432 			SCTP_IPI_ADDR_RUNLOCK();
2433 		return (NULL);
2434 	}
2435 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2436 		if (stcb->asoc.scope.loopback_scope == 0 &&
2437 		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2438 			/* Skip if loopback_scope not set */
2439 			continue;
2440 		}
2441 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2442 			switch (sctp_ifa->address.sa.sa_family) {
2443 #ifdef INET
2444 			case AF_INET:
2445 				if (stcb->asoc.scope.ipv4_addr_legal) {
2446 					struct sockaddr_in *sin;
2447 
2448 					sin = &sctp_ifa->address.sin;
2449 					if (sin->sin_addr.s_addr == 0) {
2450 						/*
2451 						 * skip unspecified
2452 						 * addresses
2453 						 */
2454 						continue;
2455 					}
2456 					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
2457 					    &sin->sin_addr) != 0) {
2458 						continue;
2459 					}
2460 					if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2461 					    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2462 						continue;
2463 
2464 					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2465 					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2466 						continue;
2467 					/*
2468 					 * found a valid local v4 address to
2469 					 * use
2470 					 */
2471 					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2472 						SCTP_IPI_ADDR_RUNLOCK();
2473 					return (&sctp_ifa->address.sa);
2474 				}
2475 				break;
2476 #endif
2477 #ifdef INET6
2478 			case AF_INET6:
2479 				if (stcb->asoc.scope.ipv6_addr_legal) {
2480 					struct sockaddr_in6 *sin6;
2481 
2482 					if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2483 						continue;
2484 					}
2485 
2486 					sin6 = &sctp_ifa->address.sin6;
2487 					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2488 						/*
2489 						 * we skip unspecified
2490 						 * addresses
2491 						 */
2492 						continue;
2493 					}
2494 					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
2495 					    &sin6->sin6_addr) != 0) {
2496 						continue;
2497 					}
2498 					if (stcb->asoc.scope.local_scope == 0 &&
2499 					    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2500 						continue;
2501 					if (stcb->asoc.scope.site_scope == 0 &&
2502 					    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2503 						continue;
2504 
2505 					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2506 					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2507 						continue;
2508 					/*
2509 					 * found a valid local v6 address to
2510 					 * use
2511 					 */
2512 					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2513 						SCTP_IPI_ADDR_RUNLOCK();
2514 					return (&sctp_ifa->address.sa);
2515 				}
2516 				break;
2517 #endif
2518 			default:
2519 				break;
2520 			}
2521 		}
2522 	}
2523 	/* no valid addresses found */
2524 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2525 		SCTP_IPI_ADDR_RUNLOCK();
2526 	return (NULL);
2527 }
2528 
2529 static struct sockaddr *
2530 sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2531 {
2532 	struct sctp_laddr *laddr;
2533 
2534 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2535 		if (laddr->ifa == NULL) {
2536 			continue;
2537 		}
2538 		/* is the address restricted ? */
2539 		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2540 		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2541 			continue;
2542 
2543 		/* found a valid local address to use */
2544 		return (&laddr->ifa->address.sa);
2545 	}
2546 	/* no valid addresses found */
2547 	return (NULL);
2548 }
2549 
2550 /*
2551  * builds an ASCONF chunk from queued ASCONF params.
2552  * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2553  */
2554 struct mbuf *
2555 sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2556 {
2557 	struct mbuf *m_asconf, *m_asconf_chk;
2558 	struct sctp_asconf_addr *aa;
2559 	struct sctp_asconf_chunk *acp;
2560 	struct sctp_asconf_paramhdr *aph;
2561 	struct sctp_asconf_addr_param *aap;
2562 	uint32_t p_length, overhead;
2563 	uint32_t correlation_id = 1;	/* 0 is reserved... */
2564 	caddr_t ptr, lookup_ptr;
2565 	uint8_t lookup_used = 0;
2566 
2567 	/* are there any asconf params to send? */
2568 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2569 		if (aa->sent == 0)
2570 			break;
2571 	}
2572 	if (aa == NULL)
2573 		return (NULL);
2574 
2575 	/* Consider IP header and SCTP common header. */
2576 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2577 		overhead = SCTP_MIN_OVERHEAD;
2578 	} else {
2579 		overhead = SCTP_MIN_V4_OVERHEAD;
2580 	}
2581 	/* Consider ASONF chunk. */
2582 	overhead += sizeof(struct sctp_asconf_chunk);
2583 	/* Consider AUTH chunk. */
2584 	overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
2585 	if (stcb->asoc.smallest_mtu <= overhead) {
2586 		/* MTU too small. */
2587 		return (NULL);
2588 	}
2589 	/*
2590 	 * get a chunk header mbuf and a cluster for the asconf params since
2591 	 * it's simpler to fill in the asconf chunk header lookup address on
2592 	 * the fly
2593 	 */
2594 	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_NOWAIT, 1, MT_DATA);
2595 	if (m_asconf_chk == NULL) {
2596 		/* no mbuf's */
2597 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2598 		    "sctp_compose_asconf: couldn't get chunk mbuf!\n");
2599 		return (NULL);
2600 	}
2601 	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
2602 	if (m_asconf == NULL) {
2603 		/* no mbuf's */
2604 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2605 		    "sctp_compose_asconf: couldn't get mbuf!\n");
2606 		sctp_m_freem(m_asconf_chk);
2607 		return (NULL);
2608 	}
2609 	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2610 	SCTP_BUF_LEN(m_asconf) = 0;
2611 	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2612 	memset(acp, 0, sizeof(struct sctp_asconf_chunk));
2613 	/* save pointers to lookup address and asconf params */
2614 	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2615 	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2616 
2617 	/* fill in chunk header info */
2618 	acp->ch.chunk_type = SCTP_ASCONF;
2619 	acp->ch.chunk_flags = 0;
2620 	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2621 	stcb->asoc.asconf_seq_out++;
2622 
2623 	/* add parameters... up to smallest MTU allowed */
2624 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2625 		if (aa->sent)
2626 			continue;
2627 		/* get the parameter length */
2628 		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2629 		/* will it fit in current chunk? */
2630 		if ((SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu - overhead) ||
2631 		    (SCTP_BUF_LEN(m_asconf) + p_length > MCLBYTES)) {
2632 			/* won't fit, so we're done with this chunk */
2633 			break;
2634 		}
2635 		/* assign (and store) a correlation id */
2636 		aa->ap.aph.correlation_id = correlation_id++;
2637 
2638 		/*
2639 		 * fill in address if we're doing a delete this is a simple
2640 		 * way for us to fill in the correlation address, which
2641 		 * should only be used by the peer if we're deleting our
2642 		 * source address and adding a new address (e.g. renumbering
2643 		 * case)
2644 		 */
2645 		if (lookup_used == 0 &&
2646 		    (aa->special_del == 0) &&
2647 		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2648 			struct sctp_ipv6addr_param *lookup;
2649 			uint16_t p_size, addr_size;
2650 
2651 			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2652 			lookup->ph.param_type =
2653 			    htons(aa->ap.addrp.ph.param_type);
2654 			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2655 				/* copy IPv6 address */
2656 				p_size = sizeof(struct sctp_ipv6addr_param);
2657 				addr_size = sizeof(struct in6_addr);
2658 			} else {
2659 				/* copy IPv4 address */
2660 				p_size = sizeof(struct sctp_ipv4addr_param);
2661 				addr_size = sizeof(struct in_addr);
2662 			}
2663 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2664 			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2665 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2666 			lookup_used = 1;
2667 		}
2668 		/* copy into current space */
2669 		memcpy(ptr, &aa->ap, p_length);
2670 
2671 		/* network elements and update lengths */
2672 		aph = (struct sctp_asconf_paramhdr *)ptr;
2673 		aap = (struct sctp_asconf_addr_param *)ptr;
2674 		/* correlation_id is transparent to peer, no htonl needed */
2675 		aph->ph.param_type = htons(aph->ph.param_type);
2676 		aph->ph.param_length = htons(aph->ph.param_length);
2677 		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2678 		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2679 
2680 		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2681 		ptr += SCTP_SIZE32(p_length);
2682 
2683 		/*
2684 		 * these params are removed off the pending list upon
2685 		 * getting an ASCONF-ACK back from the peer, just set flag
2686 		 */
2687 		aa->sent = 1;
2688 	}
2689 	/* check to see if the lookup addr has been populated yet */
2690 	if (lookup_used == 0) {
2691 		/* NOTE: if the address param is optional, can skip this... */
2692 		/* add any valid (existing) address... */
2693 		struct sctp_ipv6addr_param *lookup;
2694 		uint16_t p_size, addr_size;
2695 		struct sockaddr *found_addr;
2696 		caddr_t addr_ptr;
2697 
2698 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2699 			found_addr = sctp_find_valid_localaddr(stcb,
2700 			    addr_locked);
2701 		else
2702 			found_addr = sctp_find_valid_localaddr_ep(stcb);
2703 
2704 		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2705 		if (found_addr != NULL) {
2706 			switch (found_addr->sa_family) {
2707 #ifdef INET6
2708 			case AF_INET6:
2709 				/* copy IPv6 address */
2710 				lookup->ph.param_type =
2711 				    htons(SCTP_IPV6_ADDRESS);
2712 				p_size = sizeof(struct sctp_ipv6addr_param);
2713 				addr_size = sizeof(struct in6_addr);
2714 				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2715 				    found_addr)->sin6_addr;
2716 				break;
2717 #endif
2718 #ifdef INET
2719 			case AF_INET:
2720 				/* copy IPv4 address */
2721 				lookup->ph.param_type =
2722 				    htons(SCTP_IPV4_ADDRESS);
2723 				p_size = sizeof(struct sctp_ipv4addr_param);
2724 				addr_size = sizeof(struct in_addr);
2725 				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2726 				    found_addr)->sin_addr;
2727 				break;
2728 #endif
2729 			default:
2730 				SCTPDBG(SCTP_DEBUG_ASCONF1,
2731 				    "sctp_compose_asconf: no usable lookup addr (family = %d)!\n",
2732 				    found_addr->sa_family);
2733 				sctp_m_freem(m_asconf_chk);
2734 				sctp_m_freem(m_asconf);
2735 				return (NULL);
2736 			}
2737 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2738 			memcpy(lookup->addr, addr_ptr, addr_size);
2739 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2740 		} else {
2741 			/* uh oh... don't have any address?? */
2742 			SCTPDBG(SCTP_DEBUG_ASCONF1,
2743 			    "sctp_compose_asconf: no lookup addr!\n");
2744 			sctp_m_freem(m_asconf_chk);
2745 			sctp_m_freem(m_asconf);
2746 			return (NULL);
2747 		}
2748 	}
2749 	/* chain it all together */
2750 	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2751 	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2752 	acp->ch.chunk_length = htons(*retlen);
2753 
2754 	return (m_asconf_chk);
2755 }
2756 
2757 /*
2758  * section to handle address changes before an association is up eg. changes
2759  * during INIT/INIT-ACK/COOKIE-ECHO handshake
2760  */
2761 
2762 /*
2763  * processes the (local) addresses in the INIT-ACK chunk
2764  */
2765 static void
2766 sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2767     unsigned int offset, unsigned int length)
2768 {
2769 	struct sctp_paramhdr tmp_param, *ph;
2770 	uint16_t plen, ptype;
2771 	struct sctp_ifa *sctp_ifa;
2772 	union sctp_sockstore store;
2773 #ifdef INET6
2774 	struct sctp_ipv6addr_param addr6_store;
2775 #endif
2776 #ifdef INET
2777 	struct sctp_ipv4addr_param addr4_store;
2778 #endif
2779 
2780 	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2781 	if (stcb == NULL)	/* Un-needed check for SA */
2782 		return;
2783 
2784 	/* convert to upper bound */
2785 	length += offset;
2786 
2787 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2788 		return;
2789 	}
2790 	/* go through the addresses in the init-ack */
2791 	ph = (struct sctp_paramhdr *)
2792 	    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2793 	    (uint8_t *)&tmp_param);
2794 	while (ph != NULL) {
2795 		ptype = ntohs(ph->param_type);
2796 		plen = ntohs(ph->param_length);
2797 		switch (ptype) {
2798 #ifdef INET6
2799 		case SCTP_IPV6_ADDRESS:
2800 			{
2801 				struct sctp_ipv6addr_param *a6p;
2802 
2803 				/* get the entire IPv6 address param */
2804 				a6p = (struct sctp_ipv6addr_param *)
2805 				    sctp_m_getptr(m, offset,
2806 				    sizeof(struct sctp_ipv6addr_param),
2807 				    (uint8_t *)&addr6_store);
2808 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
2809 				    a6p == NULL) {
2810 					return;
2811 				}
2812 				memset(&store, 0, sizeof(union sctp_sockstore));
2813 				store.sin6.sin6_family = AF_INET6;
2814 				store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2815 				store.sin6.sin6_port = stcb->rport;
2816 				memcpy(&store.sin6.sin6_addr, a6p->addr, sizeof(struct in6_addr));
2817 				break;
2818 			}
2819 #endif
2820 #ifdef INET
2821 		case SCTP_IPV4_ADDRESS:
2822 			{
2823 				struct sctp_ipv4addr_param *a4p;
2824 
2825 				/* get the entire IPv4 address param */
2826 				a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2827 				    sizeof(struct sctp_ipv4addr_param),
2828 				    (uint8_t *)&addr4_store);
2829 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
2830 				    a4p == NULL) {
2831 					return;
2832 				}
2833 				memset(&store, 0, sizeof(union sctp_sockstore));
2834 				store.sin.sin_family = AF_INET;
2835 				store.sin.sin_len = sizeof(struct sockaddr_in);
2836 				store.sin.sin_port = stcb->rport;
2837 				store.sin.sin_addr.s_addr = a4p->addr;
2838 				break;
2839 			}
2840 #endif
2841 		default:
2842 			goto next_addr;
2843 		}
2844 
2845 		/* see if this address really (still) exists */
2846 		sctp_ifa = sctp_find_ifa_by_addr(&store.sa, stcb->asoc.vrf_id,
2847 		    SCTP_ADDR_NOT_LOCKED);
2848 		if (sctp_ifa == NULL) {
2849 			/* address doesn't exist anymore */
2850 			int status;
2851 
2852 			/* are ASCONFs allowed ? */
2853 			if ((sctp_is_feature_on(stcb->sctp_ep,
2854 			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2855 			    stcb->asoc.asconf_supported) {
2856 				/* queue an ASCONF DEL_IP_ADDRESS */
2857 				status = sctp_asconf_queue_sa_delete(stcb, &store.sa);
2858 				/*
2859 				 * if queued ok, and in correct state, send
2860 				 * out the ASCONF.
2861 				 */
2862 				if (status == 0 &&
2863 				    SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
2864 #ifdef SCTP_TIMER_BASED_ASCONF
2865 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2866 					    stcb->sctp_ep, stcb,
2867 					    stcb->asoc.primary_destination);
2868 #else
2869 					sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2870 #endif
2871 				}
2872 			}
2873 		}
2874 
2875 next_addr:
2876 		/*
2877 		 * Sanity check:  Make sure the length isn't 0, otherwise
2878 		 * we'll be stuck in this loop for a long time...
2879 		 */
2880 		if (SCTP_SIZE32(plen) == 0) {
2881 			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2882 			    plen, ptype);
2883 			return;
2884 		}
2885 		/* get next parameter */
2886 		offset += SCTP_SIZE32(plen);
2887 		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2888 			return;
2889 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2890 		    sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
2891 	}			/* while */
2892 }
2893 
2894 /* FIX ME: need to verify return result for v6 address type if v6 disabled */
2895 /*
2896  * checks to see if a specific address is in the initack address list returns
2897  * 1 if found, 0 if not
2898  */
2899 static uint32_t
2900 sctp_addr_in_initack(struct mbuf *m, uint32_t offset, uint32_t length, struct sockaddr *sa)
2901 {
2902 	struct sctp_paramhdr tmp_param, *ph;
2903 	uint16_t plen, ptype;
2904 #ifdef INET
2905 	struct sockaddr_in *sin;
2906 	struct sctp_ipv4addr_param *a4p;
2907 	struct sctp_ipv6addr_param addr4_store;
2908 #endif
2909 #ifdef INET6
2910 	struct sockaddr_in6 *sin6;
2911 	struct sctp_ipv6addr_param *a6p;
2912 	struct sctp_ipv6addr_param addr6_store;
2913 	struct sockaddr_in6 sin6_tmp;
2914 #endif
2915 
2916 	switch (sa->sa_family) {
2917 #ifdef INET
2918 	case AF_INET:
2919 		break;
2920 #endif
2921 #ifdef INET6
2922 	case AF_INET6:
2923 		break;
2924 #endif
2925 	default:
2926 		return (0);
2927 	}
2928 
2929 	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2930 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2931 	/* convert to upper bound */
2932 	length += offset;
2933 
2934 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2935 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2936 		    "find_initack_addr: invalid offset?\n");
2937 		return (0);
2938 	}
2939 	/* go through the addresses in the init-ack */
2940 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2941 	    sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
2942 	while (ph != NULL) {
2943 		ptype = ntohs(ph->param_type);
2944 		plen = ntohs(ph->param_length);
2945 		switch (ptype) {
2946 #ifdef INET6
2947 		case SCTP_IPV6_ADDRESS:
2948 			if (sa->sa_family == AF_INET6) {
2949 				/* get the entire IPv6 address param */
2950 				if (plen != sizeof(struct sctp_ipv6addr_param)) {
2951 					break;
2952 				}
2953 				/* get the entire IPv6 address param */
2954 				a6p = (struct sctp_ipv6addr_param *)
2955 				    sctp_m_getptr(m, offset,
2956 				    sizeof(struct sctp_ipv6addr_param),
2957 				    (uint8_t *)&addr6_store);
2958 				if (a6p == NULL) {
2959 					return (0);
2960 				}
2961 				sin6 = (struct sockaddr_in6 *)sa;
2962 				if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2963 					/* create a copy and clear scope */
2964 					memcpy(&sin6_tmp, sin6,
2965 					    sizeof(struct sockaddr_in6));
2966 					sin6 = &sin6_tmp;
2967 					in6_clearscope(&sin6->sin6_addr);
2968 				}
2969 				if (memcmp(&sin6->sin6_addr, a6p->addr,
2970 				    sizeof(struct in6_addr)) == 0) {
2971 					/* found it */
2972 					return (1);
2973 				}
2974 			}
2975 			break;
2976 #endif				/* INET6 */
2977 #ifdef INET
2978 		case SCTP_IPV4_ADDRESS:
2979 			if (sa->sa_family == AF_INET) {
2980 				if (plen != sizeof(struct sctp_ipv4addr_param)) {
2981 					break;
2982 				}
2983 				/* get the entire IPv4 address param */
2984 				a4p = (struct sctp_ipv4addr_param *)
2985 				    sctp_m_getptr(m, offset,
2986 				    sizeof(struct sctp_ipv4addr_param),
2987 				    (uint8_t *)&addr4_store);
2988 				if (a4p == NULL) {
2989 					return (0);
2990 				}
2991 				sin = (struct sockaddr_in *)sa;
2992 				if (sin->sin_addr.s_addr == a4p->addr) {
2993 					/* found it */
2994 					return (1);
2995 				}
2996 			}
2997 			break;
2998 #endif
2999 		default:
3000 			break;
3001 		}
3002 		/* get next parameter */
3003 		offset += SCTP_SIZE32(plen);
3004 		if (offset + sizeof(struct sctp_paramhdr) > length) {
3005 			return (0);
3006 		}
3007 		ph = (struct sctp_paramhdr *)
3008 		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
3009 		    (uint8_t *)&tmp_param);
3010 	}			/* while */
3011 	/* not found! */
3012 	return (0);
3013 }
3014 
3015 /*
3016  * makes sure that the current endpoint local addr list is consistent with
3017  * the new association (eg. subset bound, asconf allowed) adds addresses as
3018  * necessary
3019  */
3020 static void
3021 sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3022     int length, struct sockaddr *init_addr)
3023 {
3024 	struct sctp_laddr *laddr;
3025 
3026 	/* go through the endpoint list */
3027 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3028 		/* be paranoid and validate the laddr */
3029 		if (laddr->ifa == NULL) {
3030 			SCTPDBG(SCTP_DEBUG_ASCONF1,
3031 			    "check_addr_list_ep: laddr->ifa is NULL");
3032 			continue;
3033 		}
3034 		/* do i have it implicitly? */
3035 		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3036 			continue;
3037 		}
3038 		/* check to see if in the init-ack */
3039 		if (!sctp_addr_in_initack(m, offset, length, &laddr->ifa->address.sa)) {
3040 			/* try to add it */
3041 			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
3042 			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3043 		}
3044 	}
3045 }
3046 
3047 /*
3048  * makes sure that the current kernel address list is consistent with the new
3049  * association (with all addrs bound) adds addresses as necessary
3050  */
3051 static void
3052 sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3053     int length, struct sockaddr *init_addr,
3054     uint16_t local_scope, uint16_t site_scope,
3055     uint16_t ipv4_scope, uint16_t loopback_scope)
3056 {
3057 	struct sctp_vrf *vrf = NULL;
3058 	struct sctp_ifn *sctp_ifn;
3059 	struct sctp_ifa *sctp_ifa;
3060 	uint32_t vrf_id;
3061 #ifdef INET
3062 	struct sockaddr_in *sin;
3063 #endif
3064 #ifdef INET6
3065 	struct sockaddr_in6 *sin6;
3066 #endif
3067 
3068 	if (stcb) {
3069 		vrf_id = stcb->asoc.vrf_id;
3070 	} else {
3071 		return;
3072 	}
3073 	SCTP_IPI_ADDR_RLOCK();
3074 	vrf = sctp_find_vrf(vrf_id);
3075 	if (vrf == NULL) {
3076 		SCTP_IPI_ADDR_RUNLOCK();
3077 		return;
3078 	}
3079 	/* go through all our known interfaces */
3080 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3081 		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3082 			/* skip loopback interface */
3083 			continue;
3084 		}
3085 		/* go through each interface address */
3086 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3087 			/* do i have it implicitly? */
3088 			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3089 				continue;
3090 			}
3091 			switch (sctp_ifa->address.sa.sa_family) {
3092 #ifdef INET
3093 			case AF_INET:
3094 				sin = &sctp_ifa->address.sin;
3095 				if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3096 				    &sin->sin_addr) != 0) {
3097 					continue;
3098 				}
3099 				if ((ipv4_scope == 0) &&
3100 				    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3101 					/* private address not in scope */
3102 					continue;
3103 				}
3104 				break;
3105 #endif
3106 #ifdef INET6
3107 			case AF_INET6:
3108 				sin6 = &sctp_ifa->address.sin6;
3109 				if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3110 				    &sin6->sin6_addr) != 0) {
3111 					continue;
3112 				}
3113 				if ((local_scope == 0) &&
3114 				    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3115 					continue;
3116 				}
3117 				if ((site_scope == 0) &&
3118 				    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3119 					continue;
3120 				}
3121 				break;
3122 #endif
3123 			default:
3124 				break;
3125 			}
3126 			/* check to see if in the init-ack */
3127 			if (!sctp_addr_in_initack(m, offset, length, &sctp_ifa->address.sa)) {
3128 				/* try to add it */
3129 				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
3130 				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
3131 				    SCTP_ADDR_LOCKED);
3132 			}
3133 		}		/* end foreach ifa */
3134 	}			/* end foreach ifn */
3135 	SCTP_IPI_ADDR_RUNLOCK();
3136 }
3137 
3138 /*
3139  * validates an init-ack chunk (from a cookie-echo) with current addresses
3140  * adds addresses from the init-ack into our local address list, if needed
3141  * queues asconf adds/deletes addresses as needed and makes appropriate list
3142  * changes for source address selection m, offset: points to the start of the
3143  * address list in an init-ack chunk length: total length of the address
3144  * params only init_addr: address where my INIT-ACK was sent from
3145  */
3146 void
3147 sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3148     int length, struct sockaddr *init_addr,
3149     uint16_t local_scope, uint16_t site_scope,
3150     uint16_t ipv4_scope, uint16_t loopback_scope)
3151 {
3152 	/* process the local addresses in the initack */
3153 	sctp_process_initack_addresses(stcb, m, offset, length);
3154 
3155 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3156 		/* bound all case */
3157 		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3158 		    local_scope, site_scope, ipv4_scope, loopback_scope);
3159 	} else {
3160 		/* subset bound case */
3161 		if (sctp_is_feature_on(stcb->sctp_ep,
3162 		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3163 			/* asconf's allowed */
3164 			sctp_check_address_list_ep(stcb, m, offset, length,
3165 			    init_addr);
3166 		}
3167 		/* else, no asconfs allowed, so what we sent is what we get */
3168 	}
3169 }
3170 
3171 /*
3172  * sctp_bindx() support
3173  */
3174 uint32_t
3175 sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
3176     uint32_t type, uint32_t vrf_id)
3177 {
3178 	struct sctp_ifa *ifa;
3179 	struct sctp_laddr *laddr, *nladdr;
3180 
3181 	if (sa->sa_len == 0) {
3182 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3183 		return (EINVAL);
3184 	}
3185 	if (type == SCTP_ADD_IP_ADDRESS) {
3186 		/* For an add the address MUST be on the system */
3187 		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
3188 	} else if (type == SCTP_DEL_IP_ADDRESS) {
3189 		/* For a delete we need to find it in the inp */
3190 		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
3191 	} else {
3192 		ifa = NULL;
3193 	}
3194 	if (ifa != NULL) {
3195 		if (type == SCTP_ADD_IP_ADDRESS) {
3196 			sctp_add_local_addr_ep(inp, ifa, type);
3197 		} else if (type == SCTP_DEL_IP_ADDRESS) {
3198 			if (inp->laddr_count < 2) {
3199 				/* can't delete the last local address */
3200 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3201 				return (EINVAL);
3202 			}
3203 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
3204 			    sctp_nxt_addr) {
3205 				if (ifa == laddr->ifa) {
3206 					/* Mark in the delete */
3207 					laddr->action = type;
3208 				}
3209 			}
3210 		}
3211 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3212 			/*
3213 			 * There is no need to start the iterator if the inp
3214 			 * has no associations.
3215 			 */
3216 			if (type == SCTP_DEL_IP_ADDRESS) {
3217 				LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3218 					if (laddr->ifa == ifa) {
3219 						sctp_del_local_addr_ep(inp, ifa);
3220 					}
3221 				}
3222 			}
3223 		} else {
3224 			struct sctp_asconf_iterator *asc;
3225 			struct sctp_laddr *wi;
3226 			int ret;
3227 
3228 			SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
3229 			    sizeof(struct sctp_asconf_iterator),
3230 			    SCTP_M_ASC_IT);
3231 			if (asc == NULL) {
3232 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3233 				return (ENOMEM);
3234 			}
3235 			wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3236 			if (wi == NULL) {
3237 				SCTP_FREE(asc, SCTP_M_ASC_IT);
3238 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3239 				return (ENOMEM);
3240 			}
3241 			LIST_INIT(&asc->list_of_work);
3242 			asc->cnt = 1;
3243 			SCTP_INCR_LADDR_COUNT();
3244 			wi->ifa = ifa;
3245 			wi->action = type;
3246 			atomic_add_int(&ifa->refcount, 1);
3247 			LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
3248 			ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
3249 			    sctp_asconf_iterator_stcb,
3250 			    sctp_asconf_iterator_ep_end,
3251 			    SCTP_PCB_ANY_FLAGS,
3252 			    SCTP_PCB_ANY_FEATURES,
3253 			    SCTP_ASOC_ANY_STATE,
3254 			    (void *)asc, 0,
3255 			    sctp_asconf_iterator_end, inp, 0);
3256 			if (ret) {
3257 				SCTP_PRINTF("Failed to initiate iterator for addr_mgmt_ep_sa\n");
3258 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EFAULT);
3259 				sctp_asconf_iterator_end(asc, 0);
3260 				return (EFAULT);
3261 			}
3262 		}
3263 		return (0);
3264 	} else {
3265 		/* invalid address! */
3266 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3267 		return (EADDRNOTAVAIL);
3268 	}
3269 }
3270 
3271 void
3272 sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb, struct sctp_nets *net)
3273 {
3274 	struct sctp_asconf_addr *aa_vtag, *aa_add, *aa_del;
3275 	struct sctp_ifa *sctp_ifap;
3276 	struct sctp_asconf_tag_param *vtag;
3277 #ifdef INET
3278 	struct sockaddr_in *to;
3279 #endif
3280 #ifdef INET6
3281 	struct sockaddr_in6 *to6;
3282 #endif
3283 
3284 	if (net == NULL) {
3285 		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
3286 		return;
3287 	}
3288 	if (stcb == NULL) {
3289 		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
3290 		return;
3291 	}
3292 	/*
3293 	 * Need to have in the ASCONF: - VTAG(my_vtag/peer_vtag) -
3294 	 * ADD(wildcard) - DEL(wildcard) - ADD(Any global addresses)
3295 	 */
3296 	SCTP_MALLOC(aa_vtag, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
3297 	SCTP_MALLOC(aa_add, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
3298 	SCTP_MALLOC(aa_del, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
3299 
3300 	if ((aa_vtag == NULL) || (aa_add == NULL) || (aa_del == NULL)) {
3301 		/* Didn't get memory */
3302 		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3303 out:
3304 		if (aa_vtag != NULL) {
3305 			SCTP_FREE(aa_vtag, SCTP_M_ASC_ADDR);
3306 		}
3307 		if (aa_add != NULL) {
3308 			SCTP_FREE(aa_add, SCTP_M_ASC_ADDR);
3309 		}
3310 		if (aa_del != NULL) {
3311 			SCTP_FREE(aa_del, SCTP_M_ASC_ADDR);
3312 		}
3313 		return;
3314 	}
3315 	memset(aa_vtag, 0, sizeof(struct sctp_asconf_addr));
3316 	aa_vtag->special_del = 0;
3317 	/* Fill in ASCONF address parameter fields. */
3318 	/* Top level elements are "networked" during send. */
3319 	aa_vtag->ifa = NULL;
3320 	aa_vtag->sent = 0;	/* clear sent flag */
3321 	vtag = (struct sctp_asconf_tag_param *)&aa_vtag->ap.aph;
3322 	vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
3323 	vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
3324 	vtag->local_vtag = htonl(stcb->asoc.my_vtag);
3325 	vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
3326 
3327 	memset(aa_add, 0, sizeof(struct sctp_asconf_addr));
3328 	memset(aa_del, 0, sizeof(struct sctp_asconf_addr));
3329 	switch (net->ro._l_addr.sa.sa_family) {
3330 #ifdef INET
3331 	case AF_INET:
3332 		aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3333 		aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3334 		aa_add->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3335 		aa_add->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3336 		/* No need to fill the address, we are using 0.0.0.0 */
3337 		aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3338 		aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3339 		aa_del->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3340 		aa_del->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3341 		/* No need to fill the address, we are using 0.0.0.0 */
3342 		break;
3343 #endif
3344 #ifdef INET6
3345 	case AF_INET6:
3346 		aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3347 		aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3348 		aa_add->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3349 		aa_add->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3350 		/* No need to fill the address, we are using ::0 */
3351 		aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3352 		aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3353 		aa_del->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3354 		aa_del->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3355 		/* No need to fill the address, we are using ::0 */
3356 		break;
3357 #endif
3358 	default:
3359 		SCTPDBG(SCTP_DEBUG_ASCONF1,
3360 		    "sctp_asconf_send_nat_state_update: unknown address family %d\n",
3361 		    net->ro._l_addr.sa.sa_family);
3362 		goto out;
3363 	}
3364 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_vtag, next);
3365 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_add, next);
3366 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_del, next);
3367 
3368 	/* Now we must hunt the addresses and add all global addresses */
3369 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3370 		struct sctp_vrf *vrf = NULL;
3371 		struct sctp_ifn *sctp_ifnp;
3372 		uint32_t vrf_id;
3373 
3374 		vrf_id = stcb->sctp_ep->def_vrf_id;
3375 		vrf = sctp_find_vrf(vrf_id);
3376 		if (vrf == NULL) {
3377 			goto skip_rest;
3378 		}
3379 
3380 		SCTP_IPI_ADDR_RLOCK();
3381 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3382 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3383 				switch (sctp_ifap->address.sa.sa_family) {
3384 #ifdef INET
3385 				case AF_INET:
3386 					to = &sctp_ifap->address.sin;
3387 					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3388 					    &to->sin_addr) != 0) {
3389 						continue;
3390 					}
3391 					if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3392 						continue;
3393 					}
3394 					if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3395 						continue;
3396 					}
3397 					break;
3398 #endif
3399 #ifdef INET6
3400 				case AF_INET6:
3401 					to6 = &sctp_ifap->address.sin6;
3402 					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3403 					    &to6->sin6_addr) != 0) {
3404 						continue;
3405 					}
3406 					if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3407 						continue;
3408 					}
3409 					if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3410 						continue;
3411 					}
3412 					break;
3413 #endif
3414 				default:
3415 					continue;
3416 				}
3417 				sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3418 			}
3419 		}
3420 		SCTP_IPI_ADDR_RUNLOCK();
3421 	} else {
3422 		struct sctp_laddr *laddr;
3423 
3424 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3425 			if (laddr->ifa == NULL) {
3426 				continue;
3427 			}
3428 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3429 				/*
3430 				 * Address being deleted by the system, dont
3431 				 * list.
3432 				 */
3433 				continue;
3434 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3435 				/*
3436 				 * Address being deleted on this ep don't
3437 				 * list.
3438 				 */
3439 				continue;
3440 			}
3441 			sctp_ifap = laddr->ifa;
3442 			switch (sctp_ifap->address.sa.sa_family) {
3443 #ifdef INET
3444 			case AF_INET:
3445 				to = &sctp_ifap->address.sin;
3446 				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3447 					continue;
3448 				}
3449 				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3450 					continue;
3451 				}
3452 				break;
3453 #endif
3454 #ifdef INET6
3455 			case AF_INET6:
3456 				to6 = &sctp_ifap->address.sin6;
3457 				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3458 					continue;
3459 				}
3460 				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3461 					continue;
3462 				}
3463 				break;
3464 #endif
3465 			default:
3466 				continue;
3467 			}
3468 			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3469 		}
3470 	}
3471 skip_rest:
3472 	/* Now we must send the asconf into the queue */
3473 	sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
3474 }
3475