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