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