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