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