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