xref: /freebsd/sys/netinet/sctp_asconf.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
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, ip6_use_defzone);
881 				break;
882 			}
883 #endif
884 		default:
885 			/* unknown address type */
886 			from = NULL;
887 		}
888 		if (from != NULL) {
889 			SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
890 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, from);
891 			/* look up the from address */
892 			stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, from);
893 #ifdef SCTP_DEBUG
894 			if (stcb->asoc.last_control_chunk_from == NULL)
895 				SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
896 #endif
897 		}
898 	}
899 }
900 
901 /*
902  * does the address match? returns 0 if not, 1 if so
903  */
904 static uint32_t
905 sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
906 {
907 #ifdef INET6
908 	if (sa->sa_family == AF_INET6) {
909 		/* IPv6 sa address */
910 		/* XXX scopeid */
911 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
912 
913 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
914 		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
915 		    sizeof(struct in6_addr)) == 0)) {
916 			return (1);
917 		}
918 	} else
919 #endif				/* INET6 */
920 	if (sa->sa_family == AF_INET) {
921 		/* IPv4 sa address */
922 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
923 
924 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
925 		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
926 		    sizeof(struct in_addr)) == 0)) {
927 			return (1);
928 		}
929 	}
930 	return (0);
931 }
932 
933 /*
934  * does the address match? returns 0 if not, 1 if so
935  */
936 static uint32_t
937 sctp_addr_match(
938 #ifdef INET6
939     struct sctp_ipv6addr_param *v6addr,
940 #else
941     struct sctp_ipv4addr_param *v4addr,
942 #endif
943     struct sockaddr *sa)
944 {
945 	uint16_t param_type, param_length;
946 
947 #ifdef INET6
948 	struct sctp_ipv4addr_param *v4addr = (struct sctp_ipv4addr_param *)v6addr;
949 
950 	if (sa->sa_family == AF_INET6) {
951 		/* IPv6 sa address */
952 		/* XXX scopeid */
953 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
954 
955 		param_type = ntohs(v6addr->ph.param_type);
956 		param_length = ntohs(v6addr->ph.param_length);
957 
958 		if ((param_type == SCTP_IPV6_ADDRESS) &&
959 		    param_length == sizeof(struct sctp_ipv6addr_param) &&
960 		    (memcmp(&v6addr->addr, &sin6->sin6_addr,
961 		    sizeof(struct in6_addr)) == 0)) {
962 			return (1);
963 		}
964 	} else
965 #endif				/* INET6 */
966 	if (sa->sa_family == AF_INET) {
967 		/* IPv4 sa address */
968 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
969 
970 		param_type = ntohs(v4addr->ph.param_type);
971 		param_length = ntohs(v4addr->ph.param_length);
972 
973 		if ((param_type == SCTP_IPV4_ADDRESS) &&
974 		    param_length == sizeof(struct sctp_ipv4addr_param) &&
975 		    (memcmp(&v4addr->addr, &sin->sin_addr,
976 		    sizeof(struct in_addr)) == 0)) {
977 			return (1);
978 		}
979 	}
980 	return (0);
981 }
982 
983 /*
984  * Cleanup for non-responded/OP ERR'd ASCONF
985  */
986 void
987 sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
988 {
989 	/* mark peer as ASCONF incapable */
990 	stcb->asoc.peer_supports_asconf = 0;
991 	/*
992 	 * clear out any existing asconfs going out
993 	 */
994 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
995 	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
996 	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
997 	/* remove the old ASCONF on our outbound queue */
998 	sctp_toss_old_asconf(stcb);
999 }
1000 
1001 /*
1002  * cleanup any cached source addresses that may be topologically
1003  * incorrect after a new address has been added to this interface.
1004  */
1005 static void
1006 sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
1007 {
1008 	struct sctp_nets *net;
1009 
1010 	/*
1011 	 * Ideally, we want to only clear cached routes and source addresses
1012 	 * that are topologically incorrect.  But since there is no easy way
1013 	 * to know whether the newly added address on the ifn would cause a
1014 	 * routing change (i.e. a new egress interface would be chosen)
1015 	 * without doing a new routing lookup and source address selection,
1016 	 * we will (for now) just flush any cached route using a different
1017 	 * ifn (and cached source addrs) and let output re-choose them
1018 	 * during the next send on that net.
1019 	 */
1020 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1021 		/*
1022 		 * clear any cached route (and cached source address) if the
1023 		 * route's interface is NOT the same as the address change.
1024 		 * If it's the same interface, just clear the cached source
1025 		 * address.
1026 		 */
1027 		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
1028 		    SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index) {
1029 			/* clear any cached route */
1030 			RTFREE(net->ro.ro_rt);
1031 			net->ro.ro_rt = NULL;
1032 		}
1033 		/* clear any cached source address */
1034 		if (net->src_addr_selected) {
1035 			sctp_free_ifa(net->ro._s_addr);
1036 			net->ro._s_addr = NULL;
1037 			net->src_addr_selected = 0;
1038 		}
1039 	}
1040 }
1041 
1042 void
1043 sctp_move_chunks_from_deleted_prim(struct sctp_tcb *stcb, struct sctp_nets *dst)
1044 {
1045 	struct sctp_association *asoc;
1046 	struct sctp_stream_out *outs;
1047 	struct sctp_tmit_chunk *chk;
1048 	struct sctp_stream_queue_pending *sp;
1049 
1050 	if (dst->dest_state & SCTP_ADDR_UNCONFIRMED) {
1051 		return;
1052 	}
1053 	if (stcb->asoc.deleted_primary == NULL) {
1054 		return;
1055 	}
1056 	asoc = &stcb->asoc;
1057 
1058 	/*
1059 	 * now through all the streams checking for chunks sent to our bad
1060 	 * network.
1061 	 */
1062 	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1063 		/* now clean up any chunks here */
1064 		TAILQ_FOREACH(sp, &outs->outqueue, next) {
1065 			if (sp->net == asoc->deleted_primary) {
1066 				sctp_free_remote_addr(sp->net);
1067 				sp->net = dst;
1068 				atomic_add_int(&dst->ref_count, 1);
1069 			}
1070 		}
1071 	}
1072 	/* Now check the pending queue */
1073 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
1074 		if (chk->whoTo == asoc->deleted_primary) {
1075 			sctp_free_remote_addr(chk->whoTo);
1076 			chk->whoTo = dst;
1077 			atomic_add_int(&dst->ref_count, 1);
1078 		}
1079 	}
1080 
1081 }
1082 
1083 
1084 void
1085 sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
1086 {
1087 	int error;
1088 
1089 	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
1090 		return;
1091 	}
1092 	if (stcb->asoc.deleted_primary == NULL) {
1093 		return;
1094 	}
1095 	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
1096 		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
1097 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1098 		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
1099 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
1100 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1101 		    stcb->asoc.deleted_primary,
1102 		    SCTP_FROM_SCTP_TIMER + SCTP_LOC_8);
1103 		stcb->asoc.num_send_timers_up--;
1104 		if (stcb->asoc.num_send_timers_up < 0) {
1105 			stcb->asoc.num_send_timers_up = 0;
1106 		}
1107 		SCTP_TCB_LOCK_ASSERT(stcb);
1108 		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1109 		    stcb->asoc.deleted_primary);
1110 		if (error) {
1111 			SCTP_INP_DECR_REF(stcb->sctp_ep);
1112 			return;
1113 		}
1114 		SCTP_TCB_LOCK_ASSERT(stcb);
1115 #ifdef SCTP_AUDITING_ENABLED
1116 		sctp_auditing(4, stcb->sctp_ep, stcb->asoc.deleted_primary);
1117 #endif
1118 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1119 		if ((stcb->asoc.num_send_timers_up == 0) &&
1120 		    (stcb->asoc.sent_queue_cnt > 0)) {
1121 			struct sctp_tmit_chunk *chk;
1122 
1123 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1124 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
1125 			    stcb, chk->whoTo);
1126 		}
1127 	}
1128 	return;
1129 }
1130 
1131 static int
1132     sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
1133 
1134 void
1135 sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
1136 {
1137 	struct sctp_tmit_chunk *chk;
1138 
1139 	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
1140 	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
1141 	    SCTP_FROM_SCTP_TIMER + SCTP_LOC_5);
1142 	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
1143 	net->error_count = 0;
1144 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1145 		if (chk->whoTo == net) {
1146 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
1147 				chk->sent = SCTP_DATAGRAM_RESEND;
1148 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1149 				sctp_flight_size_decrease(chk);
1150 				sctp_total_flight_decrease(stcb, chk);
1151 				net->marked_retrans++;
1152 				stcb->asoc.marked_retrans++;
1153 			}
1154 		}
1155 	}
1156 	if (net->marked_retrans) {
1157 		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1158 	}
1159 }
1160 
1161 static void
1162 sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
1163 {
1164 	struct sctp_nets *net;
1165 	int addrnum, changed;
1166 
1167 	/*
1168 	 * If number of local valid addresses is 1, the valid address is
1169 	 * probably newly added address.  Several valid addresses in this
1170 	 * association.  A source address may not be changed.  Additionally,
1171 	 * they can be configured on a same interface as "alias" addresses.
1172 	 * (by micchie)
1173 	 */
1174 	addrnum = sctp_local_addr_count(stcb);
1175 	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
1176 	    addrnum);
1177 	if (addrnum == 1) {
1178 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1179 			/* clear any cached route and source address */
1180 			if (net->ro.ro_rt) {
1181 				RTFREE(net->ro.ro_rt);
1182 				net->ro.ro_rt = NULL;
1183 			}
1184 			if (net->src_addr_selected) {
1185 				sctp_free_ifa(net->ro._s_addr);
1186 				net->ro._s_addr = NULL;
1187 				net->src_addr_selected = 0;
1188 			}
1189 			/* Retransmit unacknowledged DATA chunks immediately */
1190 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1191 			    SCTP_MOBILITY_FASTHANDOFF)) {
1192 				sctp_net_immediate_retrans(stcb, net);
1193 			}
1194 			/* also, SET PRIMARY is maybe already sent */
1195 		}
1196 		return;
1197 	}
1198 	/* Multiple local addresses exsist in the association.  */
1199 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1200 		/* clear any cached route and source address */
1201 		if (net->ro.ro_rt) {
1202 			RTFREE(net->ro.ro_rt);
1203 			net->ro.ro_rt = NULL;
1204 		}
1205 		if (net->src_addr_selected) {
1206 			sctp_free_ifa(net->ro._s_addr);
1207 			net->ro._s_addr = NULL;
1208 			net->src_addr_selected = 0;
1209 		}
1210 		/*
1211 		 * Check if the nexthop is corresponding to the new address.
1212 		 * If the new address is corresponding to the current
1213 		 * nexthop, the path will be changed.  If the new address is
1214 		 * NOT corresponding to the current nexthop, the path will
1215 		 * not be changed.
1216 		 */
1217 		SCTP_RTALLOC((sctp_route_t *) & net->ro,
1218 		    stcb->sctp_ep->def_vrf_id);
1219 		if (net->ro.ro_rt == NULL)
1220 			continue;
1221 
1222 		changed = 0;
1223 		if (net->ro._l_addr.sa.sa_family == AF_INET) {
1224 			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *) & net->ro))
1225 				changed = 1;
1226 		}
1227 #ifdef INET6
1228 		if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1229 			if (sctp_v6src_match_nexthop(
1230 			    &newifa->address.sin6, (sctp_route_t *) & net->ro))
1231 				changed = 1;
1232 		}
1233 #endif
1234 		/*
1235 		 * if the newly added address does not relate routing
1236 		 * information, we skip.
1237 		 */
1238 		if (changed == 0)
1239 			continue;
1240 		/* Retransmit unacknowledged DATA chunks immediately */
1241 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1242 		    SCTP_MOBILITY_FASTHANDOFF)) {
1243 			sctp_net_immediate_retrans(stcb, net);
1244 		}
1245 		/* Send SET PRIMARY for this new address */
1246 		if (net == stcb->asoc.primary_destination) {
1247 			(void)sctp_asconf_queue_mgmt(stcb, newifa,
1248 			    SCTP_SET_PRIM_ADDR);
1249 		}
1250 	}
1251 }
1252 
1253 /*
1254  * process an ADD/DELETE IP ack from peer.
1255  * addr: corresponding sctp_ifa to the address being added/deleted.
1256  * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1257  * flag: 1=success, 0=failure.
1258  */
1259 static void
1260 sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr,
1261     uint16_t type, uint32_t flag)
1262 {
1263 	/*
1264 	 * do the necessary asoc list work- if we get a failure indication,
1265 	 * leave the address on the assoc's restricted list.  If we get a
1266 	 * success indication, remove the address from the restricted list.
1267 	 */
1268 	/*
1269 	 * Note: this will only occur for ADD_IP_ADDRESS, since
1270 	 * DEL_IP_ADDRESS is never actually added to the list...
1271 	 */
1272 	if (flag) {
1273 		/* success case, so remove from the restricted list */
1274 		sctp_del_local_addr_restricted(stcb, addr);
1275 
1276 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1277 		    SCTP_MOBILITY_BASE) ||
1278 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1279 		    SCTP_MOBILITY_FASTHANDOFF)) {
1280 			sctp_path_check_and_react(stcb, addr);
1281 			return;
1282 		}
1283 		/* clear any cached/topologically incorrect source addresses */
1284 		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1285 	}
1286 	/* else, leave it on the list */
1287 }
1288 
1289 /*
1290  * add an asconf add/delete/set primary IP address parameter to the queue.
1291  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1292  * returns 0 if queued, -1 if not queued/removed.
1293  * NOTE: if adding, but a delete for the same address is already scheduled
1294  * (and not yet sent out), simply remove it from queue.  Same for deleting
1295  * an address already scheduled for add.  If a duplicate operation is found,
1296  * ignore the new one.
1297  */
1298 static int
1299 sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1300     uint16_t type)
1301 {
1302 	struct sctp_asconf_addr *aa, *aa_next;
1303 	struct sockaddr *sa;
1304 
1305 	/* make sure the request isn't already in the queue */
1306 	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1307 	    aa = aa_next) {
1308 		aa_next = TAILQ_NEXT(aa, next);
1309 		/* address match? */
1310 		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1311 			continue;
1312 		/*
1313 		 * is the request already in queue but not sent? pass the
1314 		 * request already sent in order to resolve the following
1315 		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1316 		 * can't remove the ADD request already sent 3. arrival of
1317 		 * ADD
1318 		 */
1319 		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1320 			return (-1);
1321 		}
1322 		/* is the negative request already in queue, and not sent */
1323 		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
1324 		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1325 			/* add requested, delete already queued */
1326 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1327 			/* remove the ifa from the restricted list */
1328 			sctp_del_local_addr_restricted(stcb, ifa);
1329 			/* free the asconf param */
1330 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1331 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
1332 			return (-1);
1333 		}
1334 		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
1335 		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
1336 			/* delete requested, add already queued */
1337 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1338 			/* remove the aa->ifa from the restricted list */
1339 			sctp_del_local_addr_restricted(stcb, aa->ifa);
1340 			/* free the asconf param */
1341 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1342 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1343 			return (-1);
1344 		}
1345 	}			/* for each aa */
1346 
1347 	/* adding new request to the queue */
1348 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1349 	    SCTP_M_ASC_ADDR);
1350 	if (aa == NULL) {
1351 		/* didn't get memory */
1352 		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1353 		return (-1);
1354 	}
1355 	/* fill in asconf address parameter fields */
1356 	/* top level elements are "networked" during send */
1357 	aa->ap.aph.ph.param_type = type;
1358 	aa->ifa = ifa;
1359 	atomic_add_int(&ifa->refcount, 1);
1360 	/* correlation_id filled in during send routine later... */
1361 	if (ifa->address.sa.sa_family == AF_INET6) {
1362 		/* IPv6 address */
1363 		struct sockaddr_in6 *sin6;
1364 
1365 		sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
1366 		sa = (struct sockaddr *)sin6;
1367 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1368 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1369 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1370 		    sizeof(struct sctp_ipv6addr_param);
1371 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1372 		    sizeof(struct in6_addr));
1373 	} else if (ifa->address.sa.sa_family == AF_INET) {
1374 		/* IPv4 address */
1375 		struct sockaddr_in *sin;
1376 
1377 		sin = (struct sockaddr_in *)&ifa->address.sa;
1378 		sa = (struct sockaddr *)sin;
1379 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1380 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1381 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1382 		    sizeof(struct sctp_ipv4addr_param);
1383 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1384 		    sizeof(struct in_addr));
1385 	} else {
1386 		/* invalid family! */
1387 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1388 		sctp_free_ifa(ifa);
1389 		return (-1);
1390 	}
1391 	aa->sent = 0;		/* clear sent flag */
1392 
1393 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1394 #ifdef SCTP_DEBUG
1395 	if (SCTP_BASE_SYSCTL(sctp_debug_on) && SCTP_DEBUG_ASCONF2) {
1396 		if (type == SCTP_ADD_IP_ADDRESS) {
1397 			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1398 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1399 		} else if (type == SCTP_DEL_IP_ADDRESS) {
1400 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1401 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1402 		} else {
1403 			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1404 			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1405 		}
1406 	}
1407 #endif
1408 
1409 	return (0);
1410 }
1411 
1412 
1413 /*
1414  * add an asconf operation for the given ifa and type.
1415  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1416  * returns 0 if completed, -1 if not completed, 1 if immediate send is
1417  * advisable.
1418  */
1419 static int
1420 sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1421     uint16_t type)
1422 {
1423 	uint32_t status;
1424 	int pending_delete_queued = 0;
1425 
1426 	/* see if peer supports ASCONF */
1427 	if (stcb->asoc.peer_supports_asconf == 0) {
1428 		return (-1);
1429 	}
1430 	/*
1431 	 * if this is deleting the last address from the assoc, mark it as
1432 	 * pending.
1433 	 */
1434 	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending &&
1435 	    (sctp_local_addr_count(stcb) < 2)) {
1436 		/* set the pending delete info only */
1437 		stcb->asoc.asconf_del_pending = 1;
1438 		stcb->asoc.asconf_addr_del_pending = ifa;
1439 		atomic_add_int(&ifa->refcount, 1);
1440 		SCTPDBG(SCTP_DEBUG_ASCONF2,
1441 		    "asconf_queue_add: mark delete last address pending\n");
1442 		return (-1);
1443 	}
1444 	/* queue an asconf parameter */
1445 	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1446 
1447 	/*
1448 	 * if this is an add, and there is a delete also pending (i.e. the
1449 	 * last local address is being changed), queue the pending delete
1450 	 * too.
1451 	 */
1452 	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
1453 		/* queue in the pending delete */
1454 		if (sctp_asconf_queue_mgmt(stcb,
1455 		    stcb->asoc.asconf_addr_del_pending,
1456 		    SCTP_DEL_IP_ADDRESS) == 0) {
1457 			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queing pending delete\n");
1458 			pending_delete_queued = 1;
1459 			/* clear out the pending delete info */
1460 			stcb->asoc.asconf_del_pending = 0;
1461 			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
1462 			stcb->asoc.asconf_addr_del_pending = NULL;
1463 		}
1464 	}
1465 	if (pending_delete_queued) {
1466 		struct sctp_nets *net;
1467 
1468 		/*
1469 		 * since we know that the only/last address is now being
1470 		 * changed in this case, reset the cwnd/rto on all nets to
1471 		 * start as a new address and path.  Also clear the error
1472 		 * counts to give the assoc the best chance to complete the
1473 		 * address change.
1474 		 */
1475 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1476 			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
1477 			    net);
1478 			net->RTO = 0;
1479 			net->error_count = 0;
1480 		}
1481 		stcb->asoc.overall_error_count = 0;
1482 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1483 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1484 			    stcb->asoc.overall_error_count,
1485 			    0,
1486 			    SCTP_FROM_SCTP_ASCONF,
1487 			    __LINE__);
1488 		}
1489 		/* queue in an advisory set primary too */
1490 		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
1491 		/* let caller know we should send this out immediately */
1492 		status = 1;
1493 	}
1494 	return (status);
1495 }
1496 
1497 /*-
1498  * add an asconf delete IP address parameter to the queue by sockaddr and
1499  * possibly with no sctp_ifa available.  This is only called by the routine
1500  * that checks the addresses in an INIT-ACK against the current address list.
1501  * returns 0 if completed, non-zero if not completed.
1502  * NOTE: if an add is already scheduled (and not yet sent out), simply
1503  * remove it from queue.  If a duplicate operation is found, ignore the
1504  * new one.
1505  */
1506 static int
1507 sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1508 {
1509 	struct sctp_ifa *ifa;
1510 	struct sctp_asconf_addr *aa, *aa_next;
1511 	uint32_t vrf_id;
1512 
1513 	if (stcb == NULL) {
1514 		return (-1);
1515 	}
1516 	/* see if peer supports ASCONF */
1517 	if (stcb->asoc.peer_supports_asconf == 0) {
1518 		return (-1);
1519 	}
1520 	/* make sure the request isn't already in the queue */
1521 	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1522 	    aa = aa_next) {
1523 		aa_next = TAILQ_NEXT(aa, next);
1524 		/* address match? */
1525 		if (sctp_asconf_addr_match(aa, sa) == 0)
1526 			continue;
1527 		/* is the request already in queue (sent or not) */
1528 		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1529 			return (-1);
1530 		}
1531 		/* is the negative request already in queue, and not sent */
1532 		if (aa->sent == 1)
1533 			continue;
1534 		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1535 			/* add already queued, so remove existing entry */
1536 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1537 			sctp_del_local_addr_restricted(stcb, aa->ifa);
1538 			/* free the entry */
1539 			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1540 			return (-1);
1541 		}
1542 	}			/* for each aa */
1543 
1544 	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1545 	if (stcb) {
1546 		vrf_id = stcb->asoc.vrf_id;
1547 	} else {
1548 		vrf_id = SCTP_DEFAULT_VRFID;
1549 	}
1550 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
1551 
1552 	/* adding new request to the queue */
1553 	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1554 	    SCTP_M_ASC_ADDR);
1555 	if (aa == NULL) {
1556 		/* didn't get memory */
1557 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1558 		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1559 		return (-1);
1560 	}
1561 	/* fill in asconf address parameter fields */
1562 	/* top level elements are "networked" during send */
1563 	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1564 	aa->ifa = ifa;
1565 	if (ifa)
1566 		atomic_add_int(&ifa->refcount, 1);
1567 	/* correlation_id filled in during send routine later... */
1568 	if (sa->sa_family == AF_INET6) {
1569 		/* IPv6 address */
1570 		struct sockaddr_in6 *sin6;
1571 
1572 		sin6 = (struct sockaddr_in6 *)sa;
1573 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1574 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1575 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1576 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1577 		    sizeof(struct in6_addr));
1578 	} else if (sa->sa_family == AF_INET) {
1579 		/* IPv4 address */
1580 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1581 
1582 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1583 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1584 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1585 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1586 		    sizeof(struct in_addr));
1587 	} else {
1588 		/* invalid family! */
1589 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1590 		if (ifa)
1591 			sctp_free_ifa(ifa);
1592 		return (-1);
1593 	}
1594 	aa->sent = 0;		/* clear sent flag */
1595 
1596 	/* delete goes to the back of the queue */
1597 	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1598 
1599 	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1600 	return (0);
1601 }
1602 
1603 /*
1604  * find a specific asconf param on our "sent" queue
1605  */
1606 static struct sctp_asconf_addr *
1607 sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1608 {
1609 	struct sctp_asconf_addr *aa;
1610 
1611 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1612 		if (aa->ap.aph.correlation_id == correlation_id &&
1613 		    aa->sent == 1) {
1614 			/* found it */
1615 			return (aa);
1616 		}
1617 	}
1618 	/* didn't find it */
1619 	return (NULL);
1620 }
1621 
1622 /*
1623  * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1624  * notifications based on the error response
1625  */
1626 static void
1627 sctp_asconf_process_error(struct sctp_tcb *stcb,
1628     struct sctp_asconf_paramhdr *aph)
1629 {
1630 	struct sctp_error_cause *eh;
1631 	struct sctp_paramhdr *ph;
1632 	uint16_t param_type;
1633 	uint16_t error_code;
1634 
1635 	eh = (struct sctp_error_cause *)(aph + 1);
1636 	ph = (struct sctp_paramhdr *)(eh + 1);
1637 	/* validate lengths */
1638 	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1639 	    htons(aph->ph.param_length)) {
1640 		/* invalid error cause length */
1641 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1642 		    "asconf_process_error: cause element too long\n");
1643 		return;
1644 	}
1645 	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1646 	    htons(eh->length)) {
1647 		/* invalid included TLV length */
1648 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1649 		    "asconf_process_error: included TLV too long\n");
1650 		return;
1651 	}
1652 	/* which error code ? */
1653 	error_code = ntohs(eh->code);
1654 	param_type = ntohs(aph->ph.param_type);
1655 	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1656 	switch (error_code) {
1657 	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1658 		/* we allow ourselves to "try again" for this error */
1659 		break;
1660 	default:
1661 		/* peer can't handle it... */
1662 		switch (param_type) {
1663 		case SCTP_ADD_IP_ADDRESS:
1664 		case SCTP_DEL_IP_ADDRESS:
1665 			stcb->asoc.peer_supports_asconf = 0;
1666 			break;
1667 		case SCTP_SET_PRIM_ADDR:
1668 			stcb->asoc.peer_supports_asconf = 0;
1669 			break;
1670 		default:
1671 			break;
1672 		}
1673 	}
1674 }
1675 
1676 /*
1677  * process an asconf queue param.
1678  * aparam: parameter to process, will be removed from the queue.
1679  * flag: 1=success case, 0=failure case
1680  */
1681 static void
1682 sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1683     struct sctp_asconf_addr *aparam, uint32_t flag)
1684 {
1685 	uint16_t param_type;
1686 
1687 	/* process this param */
1688 	param_type = aparam->ap.aph.ph.param_type;
1689 	switch (param_type) {
1690 	case SCTP_ADD_IP_ADDRESS:
1691 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1692 		    "process_param_ack: added IP address\n");
1693 		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1694 		break;
1695 	case SCTP_DEL_IP_ADDRESS:
1696 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1697 		    "process_param_ack: deleted IP address\n");
1698 		/* nothing really to do... lists already updated */
1699 		break;
1700 	case SCTP_SET_PRIM_ADDR:
1701 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1702 		    "process_param_ack: set primary IP address\n");
1703 		/* nothing to do... peer may start using this addr */
1704 		if (flag == 0)
1705 			stcb->asoc.peer_supports_asconf = 0;
1706 		break;
1707 	default:
1708 		/* should NEVER happen */
1709 		break;
1710 	}
1711 
1712 	/* remove the param and free it */
1713 	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1714 	if (aparam->ifa)
1715 		sctp_free_ifa(aparam->ifa);
1716 	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1717 }
1718 
1719 /*
1720  * cleanup from a bad asconf ack parameter
1721  */
1722 static void
1723 sctp_asconf_ack_clear(struct sctp_tcb *stcb)
1724 {
1725 	/* assume peer doesn't really know how to do asconfs */
1726 	stcb->asoc.peer_supports_asconf = 0;
1727 	/* XXX we could free the pending queue here */
1728 }
1729 
1730 void
1731 sctp_handle_asconf_ack(struct mbuf *m, int offset,
1732     struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1733     struct sctp_nets *net, int *abort_no_unlock)
1734 {
1735 	struct sctp_association *asoc;
1736 	uint32_t serial_num;
1737 	uint16_t ack_length;
1738 	struct sctp_asconf_paramhdr *aph;
1739 	struct sctp_asconf_addr *aa, *aa_next;
1740 	uint32_t last_error_id = 0;	/* last error correlation id */
1741 	uint32_t id;
1742 	struct sctp_asconf_addr *ap;
1743 
1744 	/* asconf param buffer */
1745 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1746 
1747 	/* verify minimum length */
1748 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1749 		SCTPDBG(SCTP_DEBUG_ASCONF1,
1750 		    "handle_asconf_ack: chunk too small = %xh\n",
1751 		    ntohs(cp->ch.chunk_length));
1752 		return;
1753 	}
1754 	asoc = &stcb->asoc;
1755 	serial_num = ntohl(cp->serial_number);
1756 
1757 	/*
1758 	 * NOTE: we may want to handle this differently- currently, we will
1759 	 * abort when we get an ack for the expected serial number + 1 (eg.
1760 	 * we didn't send it), process an ack normally if it is the expected
1761 	 * serial number, and re-send the previous ack for *ALL* other
1762 	 * serial numbers
1763 	 */
1764 
1765 	/*
1766 	 * if the serial number is the next expected, but I didn't send it,
1767 	 * abort the asoc, since someone probably just hijacked us...
1768 	 */
1769 	if (serial_num == (asoc->asconf_seq_out + 1)) {
1770 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1771 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1772 		    SCTP_CAUSE_ILLEGAL_ASCONF_ACK, NULL, SCTP_SO_NOT_LOCKED);
1773 		*abort_no_unlock = 1;
1774 		return;
1775 	}
1776 	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1777 		/* got a duplicate/unexpected ASCONF-ACK */
1778 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1779 		    serial_num, asoc->asconf_seq_out_acked + 1);
1780 		return;
1781 	}
1782 	if (serial_num == asoc->asconf_seq_out - 1) {
1783 		/* stop our timer */
1784 		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
1785 		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1786 	}
1787 	/* process the ASCONF-ACK contents */
1788 	ack_length = ntohs(cp->ch.chunk_length) -
1789 	    sizeof(struct sctp_asconf_ack_chunk);
1790 	offset += sizeof(struct sctp_asconf_ack_chunk);
1791 	/* process through all parameters */
1792 	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1793 		unsigned int param_length, param_type;
1794 
1795 		/* get pointer to next asconf parameter */
1796 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1797 		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1798 		if (aph == NULL) {
1799 			/* can't get an asconf paramhdr */
1800 			sctp_asconf_ack_clear(stcb);
1801 			return;
1802 		}
1803 		param_type = ntohs(aph->ph.param_type);
1804 		param_length = ntohs(aph->ph.param_length);
1805 		if (param_length > ack_length) {
1806 			sctp_asconf_ack_clear(stcb);
1807 			return;
1808 		}
1809 		if (param_length < sizeof(struct sctp_paramhdr)) {
1810 			sctp_asconf_ack_clear(stcb);
1811 			return;
1812 		}
1813 		/* get the complete parameter... */
1814 		if (param_length > sizeof(aparam_buf)) {
1815 			SCTPDBG(SCTP_DEBUG_ASCONF1,
1816 			    "param length (%u) larger than buffer size!\n", param_length);
1817 			sctp_asconf_ack_clear(stcb);
1818 			return;
1819 		}
1820 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1821 		if (aph == NULL) {
1822 			sctp_asconf_ack_clear(stcb);
1823 			return;
1824 		}
1825 		/* correlation_id is transparent to peer, no ntohl needed */
1826 		id = aph->correlation_id;
1827 
1828 		switch (param_type) {
1829 		case SCTP_ERROR_CAUSE_IND:
1830 			last_error_id = id;
1831 			/* find the corresponding asconf param in our queue */
1832 			ap = sctp_asconf_find_param(stcb, id);
1833 			if (ap == NULL) {
1834 				/* hmm... can't find this in our queue! */
1835 				break;
1836 			}
1837 			/* process the parameter, failed flag */
1838 			sctp_asconf_process_param_ack(stcb, ap, 0);
1839 			/* process the error response */
1840 			sctp_asconf_process_error(stcb, aph);
1841 			break;
1842 		case SCTP_SUCCESS_REPORT:
1843 			/* find the corresponding asconf param in our queue */
1844 			ap = sctp_asconf_find_param(stcb, id);
1845 			if (ap == NULL) {
1846 				/* hmm... can't find this in our queue! */
1847 				break;
1848 			}
1849 			/* process the parameter, success flag */
1850 			sctp_asconf_process_param_ack(stcb, ap, 1);
1851 			break;
1852 		default:
1853 			break;
1854 		}		/* switch */
1855 
1856 		/* update remaining ASCONF-ACK message length to process */
1857 		ack_length -= SCTP_SIZE32(param_length);
1858 		if (ack_length <= 0) {
1859 			/* no more data in the mbuf chain */
1860 			break;
1861 		}
1862 		offset += SCTP_SIZE32(param_length);
1863 	}			/* while */
1864 
1865 	/*
1866 	 * if there are any "sent" params still on the queue, these are
1867 	 * implicitly "success", or "failed" (if we got an error back) ...
1868 	 * so process these appropriately
1869 	 *
1870 	 * we assume that the correlation_id's are monotonically increasing
1871 	 * beginning from 1 and that we don't have *that* many outstanding
1872 	 * at any given time
1873 	 */
1874 	if (last_error_id == 0)
1875 		last_error_id--;/* set to "max" value */
1876 	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1877 	    aa = aa_next) {
1878 		aa_next = TAILQ_NEXT(aa, next);
1879 		if (aa->sent == 1) {
1880 			/*
1881 			 * implicitly successful or failed if correlation_id
1882 			 * < last_error_id, then success else, failure
1883 			 */
1884 			if (aa->ap.aph.correlation_id < last_error_id)
1885 				sctp_asconf_process_param_ack(stcb, aa, 1);
1886 			else
1887 				sctp_asconf_process_param_ack(stcb, aa, 0);
1888 		} else {
1889 			/*
1890 			 * since we always process in order (FIFO queue) if
1891 			 * we reach one that hasn't been sent, the rest
1892 			 * should not have been sent either. so, we're
1893 			 * done...
1894 			 */
1895 			break;
1896 		}
1897 	}
1898 
1899 	/* update the next sequence number to use */
1900 	asoc->asconf_seq_out_acked++;
1901 	/* remove the old ASCONF on our outbound queue */
1902 	sctp_toss_old_asconf(stcb);
1903 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1904 #ifdef SCTP_TIMER_BASED_ASCONF
1905 		/* we have more params, so restart our timer */
1906 		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1907 		    stcb, net);
1908 #else
1909 		/* we have more params, so send out more */
1910 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1911 #endif
1912 	}
1913 }
1914 
1915 #ifdef INET6
1916 static uint32_t
1917 sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1918 {
1919 	struct sockaddr_in6 *sin6, *net6;
1920 	struct sctp_nets *net;
1921 
1922 	if (sa->sa_family != AF_INET6) {
1923 		/* wrong family */
1924 		return (0);
1925 	}
1926 	sin6 = (struct sockaddr_in6 *)sa;
1927 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1928 		/* not link local address */
1929 		return (0);
1930 	}
1931 	/* hunt through our destination nets list for this scope_id */
1932 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1933 		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1934 		    AF_INET6)
1935 			continue;
1936 		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1937 		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1938 			continue;
1939 		if (sctp_is_same_scope(sin6, net6)) {
1940 			/* found one */
1941 			return (1);
1942 		}
1943 	}
1944 	/* didn't find one */
1945 	return (0);
1946 }
1947 
1948 #endif
1949 
1950 /*
1951  * address management functions
1952  */
1953 static void
1954 sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1955     struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1956 {
1957 	int status;
1958 
1959 
1960 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1961 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1962 		/* subset bound, no ASCONF allowed case, so ignore */
1963 		return;
1964 	}
1965 	/*
1966 	 * note: we know this is not the subset bound, no ASCONF case eg.
1967 	 * this is boundall or subset bound w/ASCONF allowed
1968 	 */
1969 
1970 	/* first, make sure it's a good address family */
1971 	if (ifa->address.sa.sa_family != AF_INET6 &&
1972 	    ifa->address.sa.sa_family != AF_INET) {
1973 		return;
1974 	}
1975 	/* make sure we're "allowed" to add this type of addr */
1976 	if (ifa->address.sa.sa_family == AF_INET6) {
1977 		/* invalid if we're not a v6 endpoint */
1978 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1979 			return;
1980 		/* is the v6 addr really valid ? */
1981 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1982 			return;
1983 		}
1984 	}
1985 	/* put this address on the "pending/do not use yet" list */
1986 	sctp_add_local_addr_restricted(stcb, ifa);
1987 	/*
1988 	 * check address scope if address is out of scope, don't queue
1989 	 * anything... note: this would leave the address on both inp and
1990 	 * asoc lists
1991 	 */
1992 	switch (ifa->address.sa.sa_family) {
1993 #ifdef INET6
1994 	case AF_INET6:
1995 		{
1996 			struct sockaddr_in6 *sin6;
1997 
1998 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1999 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2000 				/* we skip unspecifed addresses */
2001 				return;
2002 			}
2003 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2004 				if (stcb->asoc.local_scope == 0) {
2005 					return;
2006 				}
2007 				/* is it the right link local scope? */
2008 				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2009 					return;
2010 				}
2011 			}
2012 			if (stcb->asoc.site_scope == 0 &&
2013 			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2014 				return;
2015 			}
2016 			break;
2017 		}
2018 #endif
2019 	case AF_INET:
2020 		{
2021 			struct sockaddr_in *sin;
2022 			struct in6pcb *inp6;
2023 
2024 			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2025 			/* invalid if we are a v6 only endpoint */
2026 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2027 			    SCTP_IPV6_V6ONLY(inp6))
2028 				return;
2029 
2030 			sin = (struct sockaddr_in *)&ifa->address.sa;
2031 			if (sin->sin_addr.s_addr == 0) {
2032 				/* we skip unspecifed addresses */
2033 				return;
2034 			}
2035 			if (stcb->asoc.ipv4_local_scope == 0 &&
2036 			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2037 				return;
2038 			}
2039 			break;
2040 		}
2041 	default:
2042 		/* else, not AF_INET or AF_INET6, so skip */
2043 		return;
2044 	}
2045 
2046 	/* queue an asconf for this address add/delete */
2047 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
2048 		/* does the peer do asconf? */
2049 		if (stcb->asoc.peer_supports_asconf) {
2050 			/* queue an asconf for this addr */
2051 			status = sctp_asconf_queue_add(stcb, ifa, type);
2052 
2053 			/*
2054 			 * if queued ok, and in the open state, send out the
2055 			 * ASCONF.  If in the non-open state, these will be
2056 			 * sent when the state goes open.
2057 			 */
2058 			if (status == 0 &&
2059 			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2060 #ifdef SCTP_TIMER_BASED_ASCONF
2061 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
2062 				    stcb, stcb->asoc.primary_destination);
2063 #else
2064 				sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2065 				    addr_locked);
2066 #endif
2067 			}
2068 		}
2069 	}
2070 }
2071 
2072 
2073 int
2074 sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val)
2075 {
2076 	struct sctp_asconf_iterator *asc;
2077 	struct sctp_ifa *ifa;
2078 	struct sctp_laddr *l;
2079 	int type;
2080 	int cnt_invalid = 0;
2081 
2082 	asc = (struct sctp_asconf_iterator *)ptr;
2083 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2084 		ifa = l->ifa;
2085 		type = l->action;
2086 		if (ifa->address.sa.sa_family == AF_INET6) {
2087 			/* invalid if we're not a v6 endpoint */
2088 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2089 				cnt_invalid++;
2090 				if (asc->cnt == cnt_invalid)
2091 					return (1);
2092 				else
2093 					continue;
2094 			}
2095 		} else if (ifa->address.sa.sa_family == AF_INET) {
2096 			/* invalid if we are a v6 only endpoint */
2097 			struct in6pcb *inp6;
2098 
2099 			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2100 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2101 			    SCTP_IPV6_V6ONLY(inp6)) {
2102 				cnt_invalid++;
2103 				if (asc->cnt == cnt_invalid)
2104 					return (1);
2105 				else
2106 					continue;
2107 			}
2108 		} else {
2109 			/* invalid address family */
2110 			cnt_invalid++;
2111 			if (asc->cnt == cnt_invalid)
2112 				return (1);
2113 			else
2114 				continue;
2115 		}
2116 	}
2117 	return (0);
2118 }
2119 
2120 static int
2121 sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val)
2122 {
2123 	struct sctp_ifa *ifa;
2124 	struct sctp_asconf_iterator *asc;
2125 	struct sctp_laddr *laddr, *nladdr, *l;
2126 
2127 	/* Only for specific case not bound all */
2128 	asc = (struct sctp_asconf_iterator *)ptr;
2129 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2130 		ifa = l->ifa;
2131 		if (l->action == SCTP_ADD_IP_ADDRESS) {
2132 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2133 			    sctp_nxt_addr) {
2134 				if (laddr->ifa == ifa) {
2135 					laddr->action = 0;
2136 					break;
2137 				}
2138 			}
2139 		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
2140 			laddr = LIST_FIRST(&inp->sctp_addr_list);
2141 			while (laddr) {
2142 				nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2143 				/* remove only after all guys are done */
2144 				if (laddr->ifa == ifa) {
2145 					sctp_del_local_addr_ep(inp, ifa);
2146 				}
2147 				laddr = nladdr;
2148 			}
2149 		}
2150 	}
2151 	return (0);
2152 }
2153 
2154 void
2155 sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2156     void *ptr, uint32_t val)
2157 {
2158 	struct sctp_asconf_iterator *asc;
2159 	struct sctp_ifa *ifa;
2160 	struct sctp_laddr *l;
2161 	int cnt_invalid = 0;
2162 	int type, status;
2163 	int num_queued = 0;
2164 
2165 	asc = (struct sctp_asconf_iterator *)ptr;
2166 	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2167 		ifa = l->ifa;
2168 		type = l->action;
2169 
2170 		/* address's vrf_id must be the vrf_id of the assoc */
2171 		if (ifa->vrf_id != stcb->asoc.vrf_id) {
2172 			continue;
2173 		}
2174 		/* Same checks again for assoc */
2175 		switch (ifa->address.sa.sa_family) {
2176 #ifdef INET6
2177 		case AF_INET6:
2178 			{
2179 				/* invalid if we're not a v6 endpoint */
2180 				struct sockaddr_in6 *sin6;
2181 
2182 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2183 					cnt_invalid++;
2184 					if (asc->cnt == cnt_invalid)
2185 						return;
2186 					else
2187 						continue;
2188 				}
2189 				sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2190 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2191 					/* we skip unspecifed addresses */
2192 					continue;
2193 				}
2194 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2195 					if (stcb->asoc.local_scope == 0) {
2196 						continue;
2197 					}
2198 					/* is it the right link local scope? */
2199 					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2200 						continue;
2201 					}
2202 				}
2203 				break;
2204 			}
2205 #endif
2206 		case AF_INET:
2207 			{
2208 				/* invalid if we are a v6 only endpoint */
2209 				struct in6pcb *inp6;
2210 				struct sockaddr_in *sin;
2211 
2212 				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2213 				/* invalid if we are a v6 only endpoint */
2214 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2215 				    SCTP_IPV6_V6ONLY(inp6))
2216 					continue;
2217 
2218 				sin = (struct sockaddr_in *)&ifa->address.sa;
2219 				if (sin->sin_addr.s_addr == 0) {
2220 					/* we skip unspecifed addresses */
2221 					continue;
2222 				}
2223 				if (stcb->asoc.ipv4_local_scope == 0 &&
2224 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2225 					continue;;
2226 				}
2227 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2228 				    SCTP_IPV6_V6ONLY(inp6)) {
2229 					cnt_invalid++;
2230 					if (asc->cnt == cnt_invalid)
2231 						return;
2232 					else
2233 						continue;
2234 				}
2235 				break;
2236 			}
2237 		default:
2238 			/* invalid address family */
2239 			cnt_invalid++;
2240 			if (asc->cnt == cnt_invalid)
2241 				return;
2242 			else
2243 				continue;
2244 			break;
2245 		}
2246 
2247 		if (type == SCTP_ADD_IP_ADDRESS) {
2248 			/* prevent this address from being used as a source */
2249 			sctp_add_local_addr_restricted(stcb, ifa);
2250 		} else if (type == SCTP_DEL_IP_ADDRESS) {
2251 			struct sctp_nets *net;
2252 
2253 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2254 				sctp_rtentry_t *rt;
2255 
2256 				/* delete this address if cached */
2257 				if (net->ro._s_addr == ifa) {
2258 					sctp_free_ifa(net->ro._s_addr);
2259 					net->ro._s_addr = NULL;
2260 					net->src_addr_selected = 0;
2261 					rt = net->ro.ro_rt;
2262 					if (rt) {
2263 						RTFREE(rt);
2264 						net->ro.ro_rt = NULL;
2265 					}
2266 					/*
2267 					 * Now we deleted our src address,
2268 					 * should we not also now reset the
2269 					 * cwnd/rto to start as if its a new
2270 					 * address?
2271 					 */
2272 					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2273 					net->RTO = 0;
2274 
2275 				}
2276 			}
2277 		} else if (type == SCTP_SET_PRIM_ADDR) {
2278 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2279 				/* must validate the ifa is in the ep */
2280 				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
2281 					continue;
2282 				}
2283 			} else {
2284 				/* Need to check scopes for this guy */
2285 				if (sctp_is_address_in_scope(ifa,
2286 				    stcb->asoc.ipv4_addr_legal,
2287 				    stcb->asoc.ipv6_addr_legal,
2288 				    stcb->asoc.loopback_scope,
2289 				    stcb->asoc.ipv4_local_scope,
2290 				    stcb->asoc.local_scope,
2291 				    stcb->asoc.site_scope, 0) == 0) {
2292 					continue;
2293 				}
2294 			}
2295 		}
2296 		/* queue an asconf for this address add/delete */
2297 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
2298 		    stcb->asoc.peer_supports_asconf) {
2299 			/* queue an asconf for this addr */
2300 			status = sctp_asconf_queue_add(stcb, ifa, type);
2301 			/*
2302 			 * if queued ok, and in the open state, update the
2303 			 * count of queued params.  If in the non-open
2304 			 * state, these get sent when the assoc goes open.
2305 			 */
2306 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2307 				if (status >= 0) {
2308 					num_queued++;
2309 				}
2310 			}
2311 		}
2312 	}
2313 	/*
2314 	 * If we have queued params in the open state, send out an ASCONF.
2315 	 */
2316 	if (num_queued > 0) {
2317 		sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2318 		    SCTP_ADDR_NOT_LOCKED);
2319 	}
2320 }
2321 
2322 void
2323 sctp_asconf_iterator_end(void *ptr, uint32_t val)
2324 {
2325 	struct sctp_asconf_iterator *asc;
2326 	struct sctp_ifa *ifa;
2327 	struct sctp_laddr *l, *l_next;
2328 
2329 	asc = (struct sctp_asconf_iterator *)ptr;
2330 	l = LIST_FIRST(&asc->list_of_work);
2331 	while (l != NULL) {
2332 		l_next = LIST_NEXT(l, sctp_nxt_addr);
2333 		ifa = l->ifa;
2334 		if (l->action == SCTP_ADD_IP_ADDRESS) {
2335 			/* Clear the defer use flag */
2336 			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2337 		}
2338 		sctp_free_ifa(ifa);
2339 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
2340 		SCTP_DECR_LADDR_COUNT();
2341 		l = l_next;
2342 	}
2343 	SCTP_FREE(asc, SCTP_M_ASC_IT);
2344 }
2345 
2346 /*
2347  * sa is the sockaddr to ask the peer to set primary to.
2348  * returns: 0 = completed, -1 = error
2349  */
2350 int32_t
2351 sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2352 {
2353 	uint32_t vrf_id;
2354 	struct sctp_ifa *ifa;
2355 
2356 	/* find the ifa for the desired set primary */
2357 	vrf_id = stcb->asoc.vrf_id;
2358 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
2359 	if (ifa == NULL) {
2360 		/* Invalid address */
2361 		return (-1);
2362 	}
2363 	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2364 	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2365 		/* set primary queuing succeeded */
2366 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2367 		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2368 		    stcb);
2369 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2370 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2371 #ifdef SCTP_TIMER_BASED_ASCONF
2372 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2373 			    stcb->sctp_ep, stcb,
2374 			    stcb->asoc.primary_destination);
2375 #else
2376 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2377 			    SCTP_ADDR_NOT_LOCKED);
2378 #endif
2379 		}
2380 	} else {
2381 		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2382 		    stcb);
2383 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2384 		return (-1);
2385 	}
2386 	return (0);
2387 }
2388 
2389 void
2390 sctp_set_primary_ip_address(struct sctp_ifa *ifa)
2391 {
2392 	struct sctp_inpcb *inp;
2393 
2394 	/* go through all our PCB's */
2395 	LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
2396 		struct sctp_tcb *stcb;
2397 
2398 		/* process for all associations for this endpoint */
2399 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2400 			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2401 			if (!sctp_asconf_queue_add(stcb, ifa,
2402 			    SCTP_SET_PRIM_ADDR)) {
2403 				/* set primary queuing succeeded */
2404 				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
2405 				    stcb);
2406 				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
2407 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2408 #ifdef SCTP_TIMER_BASED_ASCONF
2409 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2410 					    stcb->sctp_ep, stcb,
2411 					    stcb->asoc.primary_destination);
2412 #else
2413 					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2414 					    SCTP_ADDR_NOT_LOCKED);
2415 #endif
2416 				}
2417 			}
2418 		}		/* for each stcb */
2419 	}			/* for each inp */
2420 }
2421 
2422 int
2423 sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2424 {
2425 	struct sctp_tmit_chunk *chk, *nchk;
2426 	unsigned int offset, asconf_limit;
2427 	struct sctp_asconf_chunk *acp;
2428 	struct sctp_asconf_paramhdr *aph;
2429 	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2430 	struct sctp_ipv6addr_param *p_addr;
2431 	int add_cnt, del_cnt;
2432 	uint16_t last_param_type;
2433 
2434 	add_cnt = del_cnt = 0;
2435 	last_param_type = 0;
2436 	for (chk = TAILQ_FIRST(&stcb->asoc.asconf_send_queue); chk != NULL;
2437 	    chk = nchk) {
2438 		/* get next chk */
2439 		nchk = TAILQ_NEXT(chk, sctp_next);
2440 
2441 		if (chk->data == NULL) {
2442 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2443 			continue;
2444 		}
2445 		offset = 0;
2446 		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2447 		offset += sizeof(struct sctp_asconf_chunk);
2448 		asconf_limit = ntohs(acp->ch.chunk_length);
2449 		p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2450 		if (p_addr == NULL) {
2451 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2452 			continue;
2453 		}
2454 		offset += ntohs(p_addr->ph.param_length);
2455 
2456 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2457 		if (aph == NULL) {
2458 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2459 			continue;
2460 		}
2461 		while (aph != NULL) {
2462 			unsigned int param_length, param_type;
2463 
2464 			param_type = ntohs(aph->ph.param_type);
2465 			param_length = ntohs(aph->ph.param_length);
2466 			if (offset + param_length > asconf_limit) {
2467 				/* parameter goes beyond end of chunk! */
2468 				break;
2469 			}
2470 			if (param_length > sizeof(aparam_buf)) {
2471 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2472 				break;
2473 			}
2474 			if (param_length <= sizeof(struct sctp_paramhdr)) {
2475 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2476 				break;
2477 			}
2478 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2479 			if (aph == NULL) {
2480 				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2481 				break;
2482 			}
2483 			p_addr = (struct sctp_ipv6addr_param *)(aph + 1);
2484 			if (sctp_addr_match(p_addr, &sctp_ifa->address.sa) != 0) {
2485 				switch (param_type) {
2486 				case SCTP_ADD_IP_ADDRESS:
2487 					add_cnt++;
2488 					break;
2489 				case SCTP_DEL_IP_ADDRESS:
2490 					del_cnt++;
2491 					break;
2492 				default:
2493 					break;
2494 				}
2495 				last_param_type = param_type;
2496 			}
2497 			offset += SCTP_SIZE32(param_length);
2498 			if (offset >= asconf_limit) {
2499 				/* no more data in the mbuf chain */
2500 				break;
2501 			}
2502 			/* get pointer to next asconf param */
2503 			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2504 		}
2505 	}
2506 
2507 	/*
2508 	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2509 	 * or DEL -> ADD
2510 	 */
2511 	if (add_cnt > del_cnt ||
2512 	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2513 		return 1;
2514 	}
2515 	return 0;
2516 }
2517 
2518 static struct sockaddr *
2519 sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2520 {
2521 	struct sctp_vrf *vrf = NULL;
2522 	struct sctp_ifn *sctp_ifn;
2523 	struct sctp_ifa *sctp_ifa;
2524 
2525 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2526 		SCTP_IPI_ADDR_RLOCK();
2527 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2528 	if (vrf == NULL) {
2529 		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2530 			SCTP_IPI_ADDR_RUNLOCK();
2531 		return (NULL);
2532 	}
2533 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2534 		if (stcb->asoc.loopback_scope == 0 &&
2535 		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2536 			/* Skip if loopback_scope not set */
2537 			continue;
2538 		}
2539 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2540 			if (sctp_ifa->address.sa.sa_family == AF_INET &&
2541 			    stcb->asoc.ipv4_addr_legal) {
2542 				struct sockaddr_in *sin;
2543 
2544 				sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
2545 				if (sin->sin_addr.s_addr == 0) {
2546 					/* skip unspecifed addresses */
2547 					continue;
2548 				}
2549 				if (stcb->asoc.ipv4_local_scope == 0 &&
2550 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2551 					continue;
2552 
2553 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2554 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2555 					continue;
2556 				/* found a valid local v4 address to use */
2557 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2558 					SCTP_IPI_ADDR_RUNLOCK();
2559 				return (&sctp_ifa->address.sa);
2560 			} else if (sctp_ifa->address.sa.sa_family == AF_INET6 &&
2561 			    stcb->asoc.ipv6_addr_legal) {
2562 				struct sockaddr_in6 *sin6;
2563 
2564 				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2565 					continue;
2566 				}
2567 				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
2568 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2569 					/* we skip unspecifed addresses */
2570 					continue;
2571 				}
2572 				if (stcb->asoc.local_scope == 0 &&
2573 				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2574 					continue;
2575 				if (stcb->asoc.site_scope == 0 &&
2576 				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2577 					continue;
2578 
2579 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2580 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2581 					continue;
2582 				/* found a valid local v6 address to use */
2583 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2584 					SCTP_IPI_ADDR_RUNLOCK();
2585 				return (&sctp_ifa->address.sa);
2586 			}
2587 		}
2588 	}
2589 	/* no valid addresses found */
2590 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2591 		SCTP_IPI_ADDR_RUNLOCK();
2592 	return (NULL);
2593 }
2594 
2595 static struct sockaddr *
2596 sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2597 {
2598 	struct sctp_laddr *laddr;
2599 
2600 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2601 		if (laddr->ifa == NULL) {
2602 			continue;
2603 		}
2604 		/* is the address restricted ? */
2605 		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2606 		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2607 			continue;
2608 
2609 		/* found a valid local address to use */
2610 		return (&laddr->ifa->address.sa);
2611 	}
2612 	/* no valid addresses found */
2613 	return (NULL);
2614 }
2615 
2616 /*
2617  * builds an ASCONF chunk from queued ASCONF params.
2618  * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2619  */
2620 struct mbuf *
2621 sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2622 {
2623 	struct mbuf *m_asconf, *m_asconf_chk;
2624 	struct sctp_asconf_addr *aa;
2625 	struct sctp_asconf_chunk *acp;
2626 	struct sctp_asconf_paramhdr *aph;
2627 	struct sctp_asconf_addr_param *aap;
2628 	uint32_t p_length;
2629 	uint32_t correlation_id = 1;	/* 0 is reserved... */
2630 	caddr_t ptr, lookup_ptr;
2631 	uint8_t lookup_used = 0;
2632 
2633 	/* are there any asconf params to send? */
2634 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2635 		if (aa->sent == 0)
2636 			break;
2637 	}
2638 	if (aa == NULL)
2639 		return (NULL);
2640 
2641 	/*
2642 	 * get a chunk header mbuf and a cluster for the asconf params since
2643 	 * it's simpler to fill in the asconf chunk header lookup address on
2644 	 * the fly
2645 	 */
2646 	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2647 	if (m_asconf_chk == NULL) {
2648 		/* no mbuf's */
2649 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2650 		    "compose_asconf: couldn't get chunk mbuf!\n");
2651 		return (NULL);
2652 	}
2653 	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2654 	if (m_asconf == NULL) {
2655 		/* no mbuf's */
2656 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2657 		    "compose_asconf: couldn't get mbuf!\n");
2658 		sctp_m_freem(m_asconf_chk);
2659 		return (NULL);
2660 	}
2661 	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2662 	SCTP_BUF_LEN(m_asconf) = 0;
2663 	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2664 	bzero(acp, sizeof(struct sctp_asconf_chunk));
2665 	/* save pointers to lookup address and asconf params */
2666 	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2667 	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2668 
2669 	/* fill in chunk header info */
2670 	acp->ch.chunk_type = SCTP_ASCONF;
2671 	acp->ch.chunk_flags = 0;
2672 	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2673 	stcb->asoc.asconf_seq_out++;
2674 
2675 	/* add parameters... up to smallest MTU allowed */
2676 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2677 		if (aa->sent)
2678 			continue;
2679 		/* get the parameter length */
2680 		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2681 		/* will it fit in current chunk? */
2682 		if (SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) {
2683 			/* won't fit, so we're done with this chunk */
2684 			break;
2685 		}
2686 		/* assign (and store) a correlation id */
2687 		aa->ap.aph.correlation_id = correlation_id++;
2688 
2689 		/*
2690 		 * fill in address if we're doing a delete this is a simple
2691 		 * way for us to fill in the correlation address, which
2692 		 * should only be used by the peer if we're deleting our
2693 		 * source address and adding a new address (e.g. renumbering
2694 		 * case)
2695 		 */
2696 		if (lookup_used == 0 &&
2697 		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2698 			struct sctp_ipv6addr_param *lookup;
2699 			uint16_t p_size, addr_size;
2700 
2701 			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2702 			lookup->ph.param_type =
2703 			    htons(aa->ap.addrp.ph.param_type);
2704 			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2705 				/* copy IPv6 address */
2706 				p_size = sizeof(struct sctp_ipv6addr_param);
2707 				addr_size = sizeof(struct in6_addr);
2708 			} else {
2709 				/* copy IPv4 address */
2710 				p_size = sizeof(struct sctp_ipv4addr_param);
2711 				addr_size = sizeof(struct in_addr);
2712 			}
2713 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2714 			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2715 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2716 			lookup_used = 1;
2717 		}
2718 		/* copy into current space */
2719 		memcpy(ptr, &aa->ap, p_length);
2720 
2721 		/* network elements and update lengths */
2722 		aph = (struct sctp_asconf_paramhdr *)ptr;
2723 		aap = (struct sctp_asconf_addr_param *)ptr;
2724 		/* correlation_id is transparent to peer, no htonl needed */
2725 		aph->ph.param_type = htons(aph->ph.param_type);
2726 		aph->ph.param_length = htons(aph->ph.param_length);
2727 		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2728 		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2729 
2730 		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2731 		ptr += SCTP_SIZE32(p_length);
2732 
2733 		/*
2734 		 * these params are removed off the pending list upon
2735 		 * getting an ASCONF-ACK back from the peer, just set flag
2736 		 */
2737 		aa->sent = 1;
2738 	}
2739 	/* check to see if the lookup addr has been populated yet */
2740 	if (lookup_used == 0) {
2741 		/* NOTE: if the address param is optional, can skip this... */
2742 		/* add any valid (existing) address... */
2743 		struct sctp_ipv6addr_param *lookup;
2744 		uint16_t p_size, addr_size;
2745 		struct sockaddr *found_addr;
2746 		caddr_t addr_ptr;
2747 
2748 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2749 			found_addr = sctp_find_valid_localaddr(stcb,
2750 			    addr_locked);
2751 		else
2752 			found_addr = sctp_find_valid_localaddr_ep(stcb);
2753 
2754 		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2755 		if (found_addr != NULL) {
2756 			if (found_addr->sa_family == AF_INET6) {
2757 				/* copy IPv6 address */
2758 				lookup->ph.param_type =
2759 				    htons(SCTP_IPV6_ADDRESS);
2760 				p_size = sizeof(struct sctp_ipv6addr_param);
2761 				addr_size = sizeof(struct in6_addr);
2762 				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2763 				    found_addr)->sin6_addr;
2764 			} else {
2765 				/* copy IPv4 address */
2766 				lookup->ph.param_type =
2767 				    htons(SCTP_IPV4_ADDRESS);
2768 				p_size = sizeof(struct sctp_ipv4addr_param);
2769 				addr_size = sizeof(struct in_addr);
2770 				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2771 				    found_addr)->sin_addr;
2772 			}
2773 			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2774 			memcpy(lookup->addr, addr_ptr, addr_size);
2775 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2776 			lookup_used = 1;
2777 		} else {
2778 			/* uh oh... don't have any address?? */
2779 			SCTPDBG(SCTP_DEBUG_ASCONF1,
2780 			    "compose_asconf: no lookup addr!\n");
2781 			/* for now, we send a IPv4 address of 0.0.0.0 */
2782 			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2783 			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2784 			bzero(lookup->addr, sizeof(struct in_addr));
2785 			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2786 			lookup_used = 1;
2787 		}
2788 	}
2789 	/* chain it all together */
2790 	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2791 	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2792 	acp->ch.chunk_length = ntohs(*retlen);
2793 
2794 	return (m_asconf_chk);
2795 }
2796 
2797 /*
2798  * section to handle address changes before an association is up eg. changes
2799  * during INIT/INIT-ACK/COOKIE-ECHO handshake
2800  */
2801 
2802 /*
2803  * processes the (local) addresses in the INIT-ACK chunk
2804  */
2805 static void
2806 sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2807     unsigned int offset, unsigned int length)
2808 {
2809 	struct sctp_paramhdr tmp_param, *ph;
2810 	uint16_t plen, ptype;
2811 	struct sctp_ifa *sctp_ifa;
2812 	struct sctp_ipv6addr_param addr_store;
2813 	struct sockaddr_in6 sin6;
2814 	struct sockaddr_in sin;
2815 	struct sockaddr *sa;
2816 	uint32_t vrf_id;
2817 
2818 	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2819 	if (stcb == NULL)	/* Un-needed check for SA */
2820 		return;
2821 
2822 	/* convert to upper bound */
2823 	length += offset;
2824 
2825 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2826 		return;
2827 	}
2828 	/* init the addresses */
2829 	bzero(&sin6, sizeof(sin6));
2830 	sin6.sin6_family = AF_INET6;
2831 	sin6.sin6_len = sizeof(sin6);
2832 	sin6.sin6_port = stcb->rport;
2833 
2834 	bzero(&sin, sizeof(sin));
2835 	sin.sin_len = sizeof(sin);
2836 	sin.sin_family = AF_INET;
2837 	sin.sin_port = stcb->rport;
2838 
2839 	/* go through the addresses in the init-ack */
2840 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2841 	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2842 	while (ph != NULL) {
2843 		ptype = ntohs(ph->param_type);
2844 		plen = ntohs(ph->param_length);
2845 		if (ptype == SCTP_IPV6_ADDRESS) {
2846 			struct sctp_ipv6addr_param *a6p;
2847 
2848 			/* get the entire IPv6 address param */
2849 			a6p = (struct sctp_ipv6addr_param *)
2850 			    sctp_m_getptr(m, offset,
2851 			    sizeof(struct sctp_ipv6addr_param),
2852 			    (uint8_t *) & addr_store);
2853 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2854 			    a6p == NULL) {
2855 				return;
2856 			}
2857 			memcpy(&sin6.sin6_addr, a6p->addr,
2858 			    sizeof(struct in6_addr));
2859 			sa = (struct sockaddr *)&sin6;
2860 		} else if (ptype == SCTP_IPV4_ADDRESS) {
2861 			struct sctp_ipv4addr_param *a4p;
2862 
2863 			/* get the entire IPv4 address param */
2864 			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2865 			    sizeof(struct sctp_ipv4addr_param),
2866 			    (uint8_t *) & addr_store);
2867 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2868 			    a4p == NULL) {
2869 				return;
2870 			}
2871 			sin.sin_addr.s_addr = a4p->addr;
2872 			sa = (struct sockaddr *)&sin;
2873 		} else {
2874 			goto next_addr;
2875 		}
2876 
2877 		/* see if this address really (still) exists */
2878 		if (stcb) {
2879 			vrf_id = stcb->asoc.vrf_id;
2880 		} else {
2881 			vrf_id = SCTP_DEFAULT_VRFID;
2882 		}
2883 		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id,
2884 		    SCTP_ADDR_NOT_LOCKED);
2885 		if (sctp_ifa == NULL) {
2886 			/* address doesn't exist anymore */
2887 			int status;
2888 
2889 			/* are ASCONFs allowed ? */
2890 			if ((sctp_is_feature_on(stcb->sctp_ep,
2891 			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2892 			    stcb->asoc.peer_supports_asconf) {
2893 				/* queue an ASCONF DEL_IP_ADDRESS */
2894 				status = sctp_asconf_queue_sa_delete(stcb, sa);
2895 				/*
2896 				 * if queued ok, and in correct state, send
2897 				 * out the ASCONF.
2898 				 */
2899 				if (status == 0 &&
2900 				    SCTP_GET_STATE(&stcb->asoc) ==
2901 				    SCTP_STATE_OPEN) {
2902 #ifdef SCTP_TIMER_BASED_ASCONF
2903 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2904 					    stcb->sctp_ep, stcb,
2905 					    stcb->asoc.primary_destination);
2906 #else
2907 					sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2908 					    SCTP_ADDR_NOT_LOCKED);
2909 #endif
2910 				}
2911 			}
2912 		}
2913 next_addr:
2914 		/*
2915 		 * Sanity check:  Make sure the length isn't 0, otherwise
2916 		 * we'll be stuck in this loop for a long time...
2917 		 */
2918 		if (SCTP_SIZE32(plen) == 0) {
2919 			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2920 			    plen, ptype);
2921 			return;
2922 		}
2923 		/* get next parameter */
2924 		offset += SCTP_SIZE32(plen);
2925 		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2926 			return;
2927 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2928 		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2929 	}			/* while */
2930 }
2931 
2932 /* FIX ME: need to verify return result for v6 address type if v6 disabled */
2933 /*
2934  * checks to see if a specific address is in the initack address list returns
2935  * 1 if found, 0 if not
2936  */
2937 static uint32_t
2938 sctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, uint32_t offset,
2939     uint32_t length, struct sockaddr *sa)
2940 {
2941 	struct sctp_paramhdr tmp_param, *ph;
2942 	uint16_t plen, ptype;
2943 	struct sctp_ipv6addr_param addr_store;
2944 	struct sockaddr_in *sin;
2945 	struct sctp_ipv4addr_param *a4p;
2946 
2947 #ifdef INET6
2948 	struct sockaddr_in6 *sin6;
2949 	struct sctp_ipv6addr_param *a6p;
2950 	struct sockaddr_in6 sin6_tmp;
2951 
2952 #endif				/* INET6 */
2953 
2954 	if (
2955 #ifdef INET6
2956 	    (sa->sa_family != AF_INET6) &&
2957 #endif				/* INET6 */
2958 	    (sa->sa_family != AF_INET))
2959 		return (0);
2960 
2961 	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2962 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2963 	/* convert to upper bound */
2964 	length += offset;
2965 
2966 	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2967 		SCTPDBG(SCTP_DEBUG_ASCONF1,
2968 		    "find_initack_addr: invalid offset?\n");
2969 		return (0);
2970 	}
2971 	/* go through the addresses in the init-ack */
2972 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2973 	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2974 	while (ph != NULL) {
2975 		ptype = ntohs(ph->param_type);
2976 		plen = ntohs(ph->param_length);
2977 #ifdef INET6
2978 		if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2979 			/* get the entire IPv6 address param */
2980 			a6p = (struct sctp_ipv6addr_param *)
2981 			    sctp_m_getptr(m, offset,
2982 			    sizeof(struct sctp_ipv6addr_param),
2983 			    (uint8_t *) & addr_store);
2984 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2985 			    (ph == NULL) ||
2986 			    (a6p == NULL)) {
2987 				return (0);
2988 			}
2989 			sin6 = (struct sockaddr_in6 *)sa;
2990 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2991 				/* create a copy and clear scope */
2992 				memcpy(&sin6_tmp, sin6,
2993 				    sizeof(struct sockaddr_in6));
2994 				sin6 = &sin6_tmp;
2995 				in6_clearscope(&sin6->sin6_addr);
2996 			}
2997 			if (memcmp(&sin6->sin6_addr, a6p->addr,
2998 			    sizeof(struct in6_addr)) == 0) {
2999 				/* found it */
3000 				return (1);
3001 			}
3002 		} else
3003 #endif				/* INET6 */
3004 
3005 			if (ptype == SCTP_IPV4_ADDRESS &&
3006 		    sa->sa_family == AF_INET) {
3007 			/* get the entire IPv4 address param */
3008 			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
3009 			    offset, sizeof(struct sctp_ipv4addr_param),
3010 			    (uint8_t *) & addr_store);
3011 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
3012 			    (ph == NULL) ||
3013 			    (a4p == NULL)) {
3014 				return (0);
3015 			}
3016 			sin = (struct sockaddr_in *)sa;
3017 			if (sin->sin_addr.s_addr == a4p->addr) {
3018 				/* found it */
3019 				return (1);
3020 			}
3021 		}
3022 		/* get next parameter */
3023 		offset += SCTP_SIZE32(plen);
3024 		if (offset + sizeof(struct sctp_paramhdr) > length)
3025 			return (0);
3026 		ph = (struct sctp_paramhdr *)
3027 		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
3028 		    (uint8_t *) & tmp_param);
3029 	}			/* while */
3030 	/* not found! */
3031 	return (0);
3032 }
3033 
3034 /*
3035  * makes sure that the current endpoint local addr list is consistent with
3036  * the new association (eg. subset bound, asconf allowed) adds addresses as
3037  * necessary
3038  */
3039 static void
3040 sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3041     int length, struct sockaddr *init_addr)
3042 {
3043 	struct sctp_laddr *laddr;
3044 
3045 	/* go through the endpoint list */
3046 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3047 		/* be paranoid and validate the laddr */
3048 		if (laddr->ifa == NULL) {
3049 			SCTPDBG(SCTP_DEBUG_ASCONF1,
3050 			    "check_addr_list_ep: laddr->ifa is NULL");
3051 			continue;
3052 		}
3053 		if (laddr->ifa == NULL) {
3054 			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
3055 			continue;
3056 		}
3057 		/* do i have it implicitly? */
3058 		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3059 			continue;
3060 		}
3061 		/* check to see if in the init-ack */
3062 		if (!sctp_addr_in_initack(stcb, m, offset, length,
3063 		    &laddr->ifa->address.sa)) {
3064 			/* try to add it */
3065 			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
3066 			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3067 		}
3068 	}
3069 }
3070 
3071 /*
3072  * makes sure that the current kernel address list is consistent with the new
3073  * association (with all addrs bound) adds addresses as necessary
3074  */
3075 static void
3076 sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3077     int length, struct sockaddr *init_addr,
3078     uint16_t local_scope, uint16_t site_scope,
3079     uint16_t ipv4_scope, uint16_t loopback_scope)
3080 {
3081 	struct sctp_vrf *vrf = NULL;
3082 	struct sctp_ifn *sctp_ifn;
3083 	struct sctp_ifa *sctp_ifa;
3084 	uint32_t vrf_id;
3085 
3086 	if (stcb) {
3087 		vrf_id = stcb->asoc.vrf_id;
3088 	} else {
3089 		return;
3090 	}
3091 	SCTP_IPI_ADDR_RLOCK();
3092 	vrf = sctp_find_vrf(vrf_id);
3093 	if (vrf == NULL) {
3094 		SCTP_IPI_ADDR_RUNLOCK();
3095 		return;
3096 	}
3097 	/* go through all our known interfaces */
3098 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3099 		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3100 			/* skip loopback interface */
3101 			continue;
3102 		}
3103 		/* go through each interface address */
3104 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3105 			/* do i have it implicitly? */
3106 			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3107 				continue;
3108 			}
3109 			/* check to see if in the init-ack */
3110 			if (!sctp_addr_in_initack(stcb, m, offset, length,
3111 			    &sctp_ifa->address.sa)) {
3112 				/* try to add it */
3113 				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
3114 				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
3115 				    SCTP_ADDR_LOCKED);
3116 			}
3117 		}		/* end foreach ifa */
3118 	}			/* end foreach ifn */
3119 	SCTP_IPI_ADDR_RUNLOCK();
3120 }
3121 
3122 /*
3123  * validates an init-ack chunk (from a cookie-echo) with current addresses
3124  * adds addresses from the init-ack into our local address list, if needed
3125  * queues asconf adds/deletes addresses as needed and makes appropriate list
3126  * changes for source address selection m, offset: points to the start of the
3127  * address list in an init-ack chunk length: total length of the address
3128  * params only init_addr: address where my INIT-ACK was sent from
3129  */
3130 void
3131 sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3132     int length, struct sockaddr *init_addr,
3133     uint16_t local_scope, uint16_t site_scope,
3134     uint16_t ipv4_scope, uint16_t loopback_scope)
3135 {
3136 	/* process the local addresses in the initack */
3137 	sctp_process_initack_addresses(stcb, m, offset, length);
3138 
3139 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3140 		/* bound all case */
3141 		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3142 		    local_scope, site_scope, ipv4_scope, loopback_scope);
3143 	} else {
3144 		/* subset bound case */
3145 		if (sctp_is_feature_on(stcb->sctp_ep,
3146 		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3147 			/* asconf's allowed */
3148 			sctp_check_address_list_ep(stcb, m, offset, length,
3149 			    init_addr);
3150 		}
3151 		/* else, no asconfs allowed, so what we sent is what we get */
3152 	}
3153 }
3154 
3155 /*
3156  * sctp_bindx() support
3157  */
3158 uint32_t
3159 sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
3160     uint32_t type, uint32_t vrf_id, struct sctp_ifa *sctp_ifap)
3161 {
3162 	struct sctp_ifa *ifa;
3163 
3164 	if (sa->sa_len == 0) {
3165 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3166 		return (EINVAL);
3167 	}
3168 	if (sctp_ifap) {
3169 		ifa = sctp_ifap;
3170 	} else if (type == SCTP_ADD_IP_ADDRESS) {
3171 		/* For an add the address MUST be on the system */
3172 		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
3173 	} else if (type == SCTP_DEL_IP_ADDRESS) {
3174 		/* For a delete we need to find it in the inp */
3175 		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
3176 	} else {
3177 		ifa = NULL;
3178 	}
3179 	if (ifa != NULL) {
3180 		/* add this address */
3181 		struct sctp_asconf_iterator *asc;
3182 		struct sctp_laddr *wi;
3183 
3184 		SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
3185 		    sizeof(struct sctp_asconf_iterator),
3186 		    SCTP_M_ASC_IT);
3187 		if (asc == NULL) {
3188 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3189 			return (ENOMEM);
3190 		}
3191 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr),
3192 		    struct sctp_laddr);
3193 		if (wi == NULL) {
3194 			SCTP_FREE(asc, SCTP_M_ASC_IT);
3195 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3196 			return (ENOMEM);
3197 		}
3198 		if (type == SCTP_ADD_IP_ADDRESS) {
3199 			sctp_add_local_addr_ep(inp, ifa, type);
3200 		} else if (type == SCTP_DEL_IP_ADDRESS) {
3201 			struct sctp_laddr *laddr;
3202 
3203 			if (inp->laddr_count < 2) {
3204 				/* can't delete the last local address */
3205 				SCTP_FREE(asc, SCTP_M_ASC_IT);
3206 				SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
3207 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3208 				return (EINVAL);
3209 			}
3210 			LIST_FOREACH(laddr, &inp->sctp_addr_list,
3211 			    sctp_nxt_addr) {
3212 				if (ifa == laddr->ifa) {
3213 					/* Mark in the delete */
3214 					laddr->action = type;
3215 				}
3216 			}
3217 		}
3218 		LIST_INIT(&asc->list_of_work);
3219 		asc->cnt = 1;
3220 		SCTP_INCR_LADDR_COUNT();
3221 		wi->ifa = ifa;
3222 		wi->action = type;
3223 		atomic_add_int(&ifa->refcount, 1);
3224 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
3225 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
3226 		    sctp_asconf_iterator_stcb,
3227 		    sctp_asconf_iterator_ep_end,
3228 		    SCTP_PCB_ANY_FLAGS,
3229 		    SCTP_PCB_ANY_FEATURES,
3230 		    SCTP_ASOC_ANY_STATE,
3231 		    (void *)asc, 0,
3232 		    sctp_asconf_iterator_end, inp, 0);
3233 	} else {
3234 		/* invalid address! */
3235 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3236 		return (EADDRNOTAVAIL);
3237 	}
3238 	return (0);
3239 }
3240