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