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