Home | History | Annotate | Line # | Download | only in netinet
sctp_asconf.c revision 1.11
      1 /*	$NetBSD: sctp_asconf.c,v 1.11 2017/06/28 14:38:18 rjs 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.11 2017/06/28 14:38:18 rjs 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/nd6.h>
     67 #include <netinet6/scope6_var.h>
     68 #include <netinet6/nd6.h>
     69 #endif /* INET6 */
     70 
     71 #include <netinet/in_pcb.h>
     72 
     73 #include <netinet/sctp_var.h>
     74 #include <netinet/sctp_pcb.h>
     75 #include <netinet/sctp_header.h>
     76 #include <netinet/sctputil.h>
     77 #include <netinet/sctp_output.h>
     78 #include <netinet/sctp_asconf.h>
     79 
     80 /*
     81  * debug flags:
     82  *   SCTP_DEBUG_ASCONF1: protocol info, general info and errors
     83  *   SCTP_DEBUG_ASCONF2: detailed info
     84  */
     85 #ifdef SCTP_DEBUG
     86 extern u_int32_t sctp_debug_on;
     87 #endif /* SCTP_DEBUG */
     88 
     89 /*
     90  * draft-ietf-tsvwg-addip-sctp
     91  *
     92  * Address management only currently supported
     93  * For the bound all case:
     94  *	the asoc local addr list is always a "DO NOT USE" list
     95  * For the subset bound case:
     96  *	If ASCONFs are allowed:
     97  *		the endpoint local addr list is the usable address list
     98  *		the asoc local addr list is the "DO NOT USE" list
     99  *	If ASCONFs are not allowed:
    100  *		the endpoint local addr list is the default usable list
    101  *		the asoc local addr list is the usable address list
    102  *
    103  * An ASCONF parameter queue exists per asoc which holds the pending
    104  * address operations.  Lists are updated upon receipt of ASCONF-ACK.
    105  *
    106  * Deleted addresses are always immediately removed from the lists as
    107  * they will (shortly) no longer exist in the kernel.  We send ASCONFs
    108  * as a courtesy, only if allowed.
    109  */
    110 
    111 /*
    112  * ASCONF parameter processing
    113  * response_required: set if a reply is required (eg. SUCCESS_REPORT)
    114  * returns a mbuf to an "error" response parameter or NULL/"success" if ok
    115  * FIX: allocating this many mbufs on the fly is pretty inefficient...
    116  */
    117 
    118 static struct mbuf *
    119 sctp_asconf_success_response(uint32_t id)
    120 {
    121 	struct mbuf *m_reply = NULL;
    122 	struct sctp_asconf_paramhdr *aph;
    123 
    124 	MGET(m_reply, M_DONTWAIT, MT_DATA);
    125 	if (m_reply == NULL) {
    126 #ifdef SCTP_DEBUG
    127 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    128 			printf("asconf_success_response: couldn't get mbuf!\n");
    129 		}
    130 #endif /* SCTP_DEBUG */
    131 		return NULL;
    132 	}
    133 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
    134 	aph->correlation_id = id;
    135 	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
    136 	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
    137 	m_reply->m_len = aph->ph.param_length;
    138 	aph->ph.param_length = htons(aph->ph.param_length);
    139 
    140 	return m_reply;
    141 }
    142 
    143 static struct mbuf *
    144 sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
    145     uint16_t tlv_length)
    146 {
    147 	struct mbuf *m_reply = NULL;
    148 	struct sctp_asconf_paramhdr *aph;
    149 	struct sctp_error_cause *error;
    150 	uint8_t *tlv;
    151 
    152 	MGET(m_reply, M_DONTWAIT, MT_DATA);
    153 	if (m_reply == NULL) {
    154 #ifdef SCTP_DEBUG
    155 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    156 			printf("asconf_error_response: couldn't get mbuf!\n");
    157 		}
    158 #endif /* SCTP_DEBUG */
    159 		return NULL;
    160 	}
    161 	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
    162 	error = (struct sctp_error_cause *)(aph + 1);
    163 
    164 	aph->correlation_id = id;
    165 	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
    166 	error->code = htons(cause);
    167 	error->length = tlv_length + sizeof(struct sctp_error_cause);
    168 	aph->ph.param_length = error->length +
    169 	    sizeof(struct sctp_asconf_paramhdr);
    170 
    171 	if (aph->ph.param_length > MLEN) {
    172 #ifdef SCTP_DEBUG
    173 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    174 			printf("asconf_error_response: tlv_length (%xh) too big\n",
    175 			    tlv_length);
    176 		}
    177 #endif /* SCTP_DEBUG */
    178 		sctp_m_freem(m_reply);	/* discard */
    179 		return NULL;
    180 	}
    181 
    182 	if (error_tlv != NULL) {
    183 		tlv = (uint8_t *)(error + 1);
    184 		memcpy(tlv, error_tlv, tlv_length);
    185 	}
    186 
    187 	m_reply->m_len = aph->ph.param_length;
    188 	error->length = htons(error->length);
    189 	aph->ph.param_length = htons(aph->ph.param_length);
    190 
    191 	return m_reply;
    192 }
    193 
    194 static struct mbuf *
    195 sctp_process_asconf_add_ip(struct sctp_asconf_paramhdr *aph,
    196     struct sctp_tcb *stcb, int response_required)
    197 {
    198 	struct mbuf *m_reply = NULL;
    199 	struct sockaddr_storage sa_store;
    200 	struct sctp_ipv4addr_param *v4addr;
    201 	uint16_t param_type, param_length, aparam_length;
    202 	struct sockaddr *sa;
    203 	struct sockaddr_in *sin;
    204 #ifdef INET6
    205 	struct sockaddr_in6 *sin6;
    206 	struct sctp_ipv6addr_param *v6addr;
    207 #endif /* INET6 */
    208 
    209 	aparam_length = ntohs(aph->ph.param_length);
    210 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
    211 #ifdef INET6
    212 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
    213 #endif /* INET6 */
    214 	param_type = ntohs(v4addr->ph.param_type);
    215 	param_length = ntohs(v4addr->ph.param_length);
    216 
    217 	sa = (struct sockaddr *)&sa_store;
    218 	switch (param_type) {
    219 	case SCTP_IPV4_ADDRESS:
    220 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    221 			/* invalid param size */
    222 			return NULL;
    223 		}
    224 		sin = (struct sockaddr_in *)&sa_store;
    225 		memset(sin, 0, sizeof(*sin));
    226 		sin->sin_family = AF_INET;
    227 		sin->sin_len = sizeof(struct sockaddr_in);
    228 		sin->sin_port = stcb->rport;
    229 		sin->sin_addr.s_addr = v4addr->addr;
    230 #ifdef SCTP_DEBUG
    231 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    232 			printf("process_asconf_add_ip: adding ");
    233 			sctp_print_address(sa);
    234 		}
    235 #endif /* SCTP_DEBUG */
    236 		break;
    237 	case SCTP_IPV6_ADDRESS:
    238 #ifdef INET6
    239 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    240 			/* invalid param size */
    241 			return NULL;
    242 		}
    243 		sin6 = (struct sockaddr_in6 *)&sa_store;
    244 		memset(sin6, 0, sizeof(*sin6));
    245 		sin6->sin6_family = AF_INET6;
    246 		sin6->sin6_len = sizeof(struct sockaddr_in6);
    247 		sin6->sin6_port = stcb->rport;
    248 		memcpy((void *)&sin6->sin6_addr, v6addr->addr,
    249 		    sizeof(struct in6_addr));
    250 #ifdef SCTP_DEBUG
    251 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    252 			printf("process_asconf_add_ip: adding ");
    253 			sctp_print_address(sa);
    254 		}
    255 #endif /* SCTP_DEBUG */
    256 #else
    257 		/* IPv6 not enabled! */
    258 		/* FIX ME: currently sends back an invalid param error */
    259 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    260 		    SCTP_ERROR_INVALID_PARAM, (uint8_t *)aph, aparam_length);
    261 #ifdef SCTP_DEBUG
    262 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    263 			printf("process_asconf_add_ip: v6 disabled- skipping ");
    264 			sctp_print_address(sa);
    265 		}
    266 #endif /* SCTP_DEBUG */
    267 		return m_reply;
    268 #endif /* INET6 */
    269 		break;
    270 	default:
    271 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    272 		    SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
    273 		    aparam_length);
    274 		return m_reply;
    275 	} /* end switch */
    276 
    277 	/* add the address */
    278 	if (sctp_add_remote_addr(stcb, sa, 0, 6) != 0) {
    279 #ifdef SCTP_DEBUG
    280 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    281 			printf("process_asconf_add_ip: error adding address\n");
    282 		}
    283 #endif /* SCTP_DEBUG */
    284 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    285 		    SCTP_ERROR_RESOURCE_SHORTAGE, (uint8_t *)aph,
    286 		    aparam_length);
    287 	} else {
    288 		/* notify upper layer */
    289 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa);
    290 		if (response_required) {
    291 			m_reply =
    292 			    sctp_asconf_success_response(aph->correlation_id);
    293 		}
    294 	}
    295 
    296 	return m_reply;
    297 }
    298 
    299 static struct mbuf *
    300 sctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
    301     struct sctp_tcb *stcb, int response_required)
    302 {
    303 	struct mbuf *m_reply = NULL;
    304 	struct sockaddr_storage sa_store, sa_source;
    305 	struct sctp_ipv4addr_param *v4addr;
    306 	uint16_t param_type, param_length, aparam_length;
    307 	struct sockaddr *sa;
    308 	struct sockaddr_in *sin;
    309 	struct ip *iph;
    310 	int result;
    311 #ifdef INET6
    312 	struct sockaddr_in6 *sin6;
    313 	struct sctp_ipv6addr_param *v6addr;
    314 #endif /* INET6 */
    315 
    316 	aparam_length = ntohs(aph->ph.param_length);
    317 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
    318 #ifdef INET6
    319 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
    320 #endif /* INET6 */
    321 	param_type = ntohs(v4addr->ph.param_type);
    322 	param_length = ntohs(v4addr->ph.param_length);
    323 
    324 	/* get the source IP address for deletion check */
    325 	iph = mtod(m, struct ip *);
    326 	if (iph->ip_v == IPVERSION) {
    327 		/* IPv4 source */
    328 		sin = (struct sockaddr_in *)&sa_source;
    329 		memset(sin, 0, sizeof(*sin));
    330 		sin->sin_family = AF_INET;
    331 		sin->sin_len = sizeof(struct sockaddr_in);
    332 		sin->sin_port = stcb->rport;
    333 		sin->sin_addr.s_addr = iph->ip_src.s_addr;
    334 	}
    335 #ifdef INET6
    336 	else if (iph->ip_v == (IPV6_VERSION >> 4)) {
    337 		/* IPv6 source */
    338 		struct ip6_hdr *ip6;
    339 
    340 		sin6 = (struct sockaddr_in6 *)&sa_source;
    341 		memset(sin6, 0, sizeof(*sin6));
    342 		sin6->sin6_family = AF_INET6;
    343 		sin6->sin6_len = sizeof(struct sockaddr_in6);
    344 		sin6->sin6_port = stcb->rport;
    345 		ip6 = mtod(m, struct ip6_hdr *);
    346 		sin6->sin6_addr = ip6->ip6_src;
    347 	}
    348 #endif /* INET6 */
    349 	else
    350 		return NULL;
    351 
    352 	sa = (struct sockaddr *)&sa_store;
    353 	switch (param_type) {
    354 	case SCTP_IPV4_ADDRESS:
    355 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    356 			/* invalid param size */
    357 			return NULL;
    358 		}
    359 		sin = (struct sockaddr_in *)&sa_store;
    360 		memset(sin, 0, sizeof(*sin));
    361 		sin->sin_family = AF_INET;
    362 		sin->sin_len = sizeof(struct sockaddr_in);
    363 		sin->sin_port = stcb->rport;
    364 		sin->sin_addr.s_addr = v4addr->addr;
    365 #ifdef SCTP_DEBUG
    366 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    367 			printf("process_asconf_delete_ip: deleting ");
    368 			sctp_print_address(sa);
    369 		}
    370 #endif /* SCTP_DEBUG */
    371 		break;
    372 	case SCTP_IPV6_ADDRESS:
    373 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    374 			/* invalid param size */
    375 			return NULL;
    376 		}
    377 #ifdef INET6
    378 		sin6 = (struct sockaddr_in6 *)&sa_store;
    379 		memset(sin6, 0, sizeof(*sin6));
    380 		sin6->sin6_family = AF_INET6;
    381 		sin6->sin6_len = sizeof(struct sockaddr_in6);
    382 		sin6->sin6_port = stcb->rport;
    383 		memcpy(&sin6->sin6_addr, v6addr->addr,
    384 		    sizeof(struct in6_addr));
    385 #ifdef SCTP_DEBUG
    386 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    387 			printf("process_asconf_delete_ip: deleting ");
    388 			sctp_print_address(sa);
    389 		}
    390 #endif /* SCTP_DEBUG */
    391 #else
    392 		/* IPv6 not enabled!  No "action" needed; just ack it */
    393 #ifdef SCTP_DEBUG
    394 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    395 			printf("process_asconf_delete_ip: v6 disabled- ignoring: ");
    396 			sctp_print_address(sa);
    397 		}
    398 #endif /* SCTP_DEBUG */
    399 		/* just respond with a "success" ASCONF-ACK */
    400 		return NULL;
    401 #endif /* INET6 */
    402 		break;
    403 	default:
    404 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    405 		    SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
    406 		    aparam_length);
    407 		return m_reply;
    408 	} /* end switch */
    409 
    410 	/* make sure the source address is not being deleted */
    411 	if ((memcmp(sa, &sa_source, sizeof(struct sockaddr_in)) == 0)
    412 #ifdef INET6
    413 	    || (memcmp(sa, &sa_source, sizeof(struct sockaddr_in6)) == 0)
    414 #endif /* INET6 */
    415 		) {
    416 		/* trying to delete the source address! */
    417 #ifdef SCTP_DEBUG
    418 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    419 			printf("process_asconf_delete_ip: tried to delete source addr\n");
    420 		}
    421 #endif /* SCTP_DEBUG */
    422 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    423 		    SCTP_ERROR_DELETE_SOURCE_ADDR, (uint8_t *)aph,
    424 		    aparam_length);
    425 		return m_reply;
    426 	}
    427 
    428 	/* delete the address */
    429 	result = sctp_del_remote_addr(stcb, sa);
    430 	/*
    431 	 * note if result == -2, the address doesn't exist in the asoc
    432 	 * but since it's being deleted anyways, we just ack the delete
    433 	 * -- but this probably means something has already gone awry
    434 	 */
    435 	if (result == -1) {
    436 		/* only one address in the asoc */
    437 #ifdef SCTP_DEBUG
    438 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    439 			printf("process_asconf_delete_ip: tried to delete last IP addr!\n");
    440 		}
    441 #endif /* SCTP_DEBUG */
    442 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    443 		    SCTP_ERROR_DELETE_LAST_ADDR, (uint8_t *)aph,
    444 		    aparam_length);
    445 	} else {
    446 		/* notify upper layer */
    447 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa);
    448 	}
    449 
    450 	if (response_required) {
    451 		m_reply = sctp_asconf_success_response(aph->correlation_id);
    452 	}
    453 	return m_reply;
    454 }
    455 
    456 static struct mbuf *
    457 sctp_process_asconf_set_primary(struct sctp_asconf_paramhdr *aph,
    458     struct sctp_tcb *stcb, int response_required)
    459 {
    460 	struct mbuf *m_reply = NULL;
    461 	struct sockaddr_storage sa_store;
    462 	struct sctp_ipv4addr_param *v4addr;
    463 	uint16_t param_type, param_length, aparam_length;
    464 	struct sockaddr *sa;
    465 	struct sockaddr_in *sin;
    466 #ifdef INET6
    467 	struct sockaddr_in6 *sin6;
    468 	struct sctp_ipv6addr_param *v6addr;
    469 #endif /* INET6 */
    470 
    471 	aparam_length = ntohs(aph->ph.param_length);
    472 	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
    473 #ifdef INET6
    474 	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
    475 #endif /* INET6 */
    476 	param_type = ntohs(v4addr->ph.param_type);
    477 	param_length = ntohs(v4addr->ph.param_length);
    478 
    479 	sa = (struct sockaddr *)&sa_store;
    480 	switch (param_type) {
    481 	case SCTP_IPV4_ADDRESS:
    482 		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    483 			/* invalid param size */
    484 			return NULL;
    485 		}
    486 		sin = (struct sockaddr_in *)&sa_store;
    487 		memset(sin, 0, sizeof(*sin));
    488 		sin->sin_family = AF_INET;
    489 		sin->sin_len = sizeof(struct sockaddr_in);
    490 		sin->sin_addr.s_addr = v4addr->addr;
    491 #ifdef SCTP_DEBUG
    492 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    493 			printf("process_asconf_set_primary: ");
    494 			sctp_print_address(sa);
    495 		}
    496 #endif /* SCTP_DEBUG */
    497 		break;
    498 	case SCTP_IPV6_ADDRESS:
    499 		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    500 			/* invalid param size */
    501 			return NULL;
    502 		}
    503 #ifdef INET6
    504 		sin6 = (struct sockaddr_in6 *)&sa_store;
    505 		memset(sin6, 0, sizeof(*sin6));
    506 		sin6->sin6_family = AF_INET6;
    507 		sin6->sin6_len = sizeof(struct sockaddr_in6);
    508 		memcpy((void *)&sin6->sin6_addr, v6addr->addr,
    509 		    sizeof(struct in6_addr));
    510 #ifdef SCTP_DEBUG
    511 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    512 			printf("process_asconf_set_primary: ");
    513 			sctp_print_address(sa);
    514 		}
    515 #endif /* SCTP_DEBUG */
    516 #else
    517 		/* IPv6 not enabled!  No "action" needed; just ack it */
    518 #ifdef SCTP_DEBUG
    519 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    520 			printf("process_asconf_set_primary: v6 disabled- ignoring: ");
    521 			sctp_print_address(sa);
    522 		}
    523 #endif /* SCTP_DEBUG */
    524 		/* just respond with a "success" ASCONF-ACK */
    525 		return NULL;
    526 #endif /* INET6 */
    527 		break;
    528 	default:
    529 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    530 		    SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
    531 		    aparam_length);
    532 		return m_reply;
    533 	} /* end switch */
    534 
    535 	/* set the primary address */
    536 	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
    537 #ifdef SCTP_DEBUG
    538 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    539 			printf("process_asconf_set_primary: primary address set\n");
    540 		}
    541 #endif /* SCTP_DEBUG */
    542 		/* notify upper layer */
    543 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa);
    544 
    545 		if (response_required) {
    546 			m_reply = sctp_asconf_success_response(aph->correlation_id);
    547 		}
    548 	} else {
    549 		/* couldn't set the requested primary address! */
    550 #ifdef SCTP_DEBUG
    551 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    552 			printf("process_asconf_set_primary: set primary failed!\n");
    553 		}
    554 #endif /* SCTP_DEBUG */
    555 		/* must have been an invalid address, so report */
    556 		m_reply = sctp_asconf_error_response(aph->correlation_id,
    557 		    SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
    558 		    aparam_length);
    559 	}
    560 
    561 	return m_reply;
    562 }
    563 
    564 /*
    565  * handles an ASCONF chunk
    566  * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
    567  */
    568 void
    569 sctp_handle_asconf(struct mbuf *m, unsigned int offset, struct sctp_asconf_chunk *cp,
    570     struct sctp_tcb *stcb, struct sctp_nets *net)
    571 {
    572 	struct sctp_association *asoc;
    573 	uint32_t serial_num;
    574 	struct mbuf *m_ack, *m_result, *m_tail;
    575 	struct sctp_asconf_ack_chunk *ack_cp;
    576 	struct sctp_asconf_paramhdr *aph, *ack_aph;
    577 	struct sctp_ipv6addr_param *p_addr;
    578 	unsigned int asconf_limit;
    579 	int error = 0;		/* did an error occur? */
    580 	/* asconf param buffer */
    581 	static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
    582 
    583 	/* verify minimum length */
    584 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
    585 #ifdef SCTP_DEBUG
    586 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    587 			printf("handle_asconf: chunk too small = %xh\n",
    588 			    ntohs(cp->ch.chunk_length));
    589 		}
    590 #endif /* SCTP_DEBUG */
    591 		return;
    592 	}
    593 
    594 	asoc = &stcb->asoc;
    595 	serial_num = ntohl(cp->serial_number);
    596 
    597 	if (serial_num == asoc->asconf_seq_in) {
    598 		/* got a duplicate ASCONF */
    599 #ifdef SCTP_DEBUG
    600 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    601 			printf("handle_asconf: got duplicate serial number = %xh\n",
    602 			       serial_num);
    603 		}
    604 #endif /* SCTP_DEBUG */
    605 		/* resend last ASCONF-ACK... */
    606 		sctp_send_asconf_ack(stcb, 1);
    607 		return;
    608 	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
    609 #ifdef SCTP_DEBUG
    610 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    611 			printf("handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
    612 			    serial_num, asoc->asconf_seq_in+1);
    613 		}
    614 #endif /* SCTP_DEBUG */
    615 		return;
    616 	}
    617 
    618 	/* it's the expected "next" sequence number, so process it */
    619 	asoc->asconf_seq_in = serial_num;	/* update sequence */
    620 	/* get length of all the param's in the ASCONF */
    621 	asconf_limit = offset + ntohs(cp->ch.chunk_length);
    622 #ifdef SCTP_DEBUG
    623 	if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    624 		printf("handle_asconf: asconf_limit=%u, sequence=%xh\n",
    625 		    asconf_limit, serial_num);
    626 	}
    627 #endif /* SCTP_DEBUG */
    628 	if (asoc->last_asconf_ack_sent != NULL) {
    629 		/* free last ASCONF-ACK message sent */
    630 		sctp_m_freem(asoc->last_asconf_ack_sent);
    631 		asoc->last_asconf_ack_sent = NULL;
    632 	}
    633 	MGETHDR(m_ack, M_DONTWAIT, MT_DATA);
    634 	if (m_ack == NULL) {
    635 #ifdef SCTP_DEBUG
    636 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    637 			printf("handle_asconf: couldn't get mbuf!\n");
    638 		}
    639 #endif /* SCTP_DEBUG */
    640 		return;
    641 	}
    642 	m_tail = m_ack;		/* current reply chain's tail */
    643 
    644 	/* fill in ASCONF-ACK header */
    645 	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
    646 	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
    647 	ack_cp->ch.chunk_flags = 0;
    648 	ack_cp->serial_number = htonl(serial_num);
    649 	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
    650 	m_ack->m_len = sizeof(struct sctp_asconf_ack_chunk);
    651 	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
    652 	m_ack->m_pkthdr.len = sizeof(struct sctp_asconf_ack_chunk);
    653 
    654 	/* skip the lookup address parameter */
    655 	offset += sizeof(struct sctp_asconf_chunk);
    656 	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
    657 	if (p_addr == NULL) {
    658 #ifdef SCTP_DEBUG
    659 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    660 			printf("handle_asconf: couldn't get lookup addr!\n");
    661 		}
    662 #endif /* SCTP_DEBUG */
    663 
    664 		/* respond with a missing/invalid mandatory parameter error */
    665 		return;
    666 	}
    667 	/* param_length is already validated in process_control... */
    668 	offset += ntohs(p_addr->ph.param_length);   /* skip lookup addr */
    669 
    670 	/* get pointer to first asconf param in ASCONF */
    671 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
    672 	/* get pointer to first asconf param in ASCONF-ACK */
    673 	if (aph == NULL) {
    674 		printf("Gak in asconf\n");
    675 		return;
    676 	}
    677 	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, vaddr_t) + sizeof(struct sctp_asconf_ack_chunk));
    678 	if (ack_aph == NULL) {
    679 		printf("Gak in asconf2\n");
    680 		return;
    681 	}
    682 
    683 	/* process through all parameters */
    684 	while (aph != NULL) {
    685 		unsigned int param_length, param_type;
    686 
    687 		param_type = ntohs(aph->ph.param_type);
    688 		param_length = ntohs(aph->ph.param_length);
    689 		if (offset + param_length > asconf_limit) {
    690 			/* parameter goes beyond end of chunk! */
    691 			sctp_m_freem(m_ack);
    692 			return;
    693 		}
    694 		m_result = NULL;
    695 
    696 		if (param_length > sizeof(aparam_buf)) {
    697 #ifdef SCTP_DEBUG
    698 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    699 				printf("handle_asconf: param length (%u) larger than buffer size!\n", param_length);
    700 			}
    701 #endif /* SCTP_DEBUG */
    702 			sctp_m_freem(m_ack);
    703 			return;
    704 		}
    705 		if (param_length <= sizeof(struct sctp_paramhdr)) {
    706 #ifdef SCTP_DEBUG
    707 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    708 				printf("handle_asconf: param length (%u) too short\n", param_length);
    709 			}
    710 #endif /* SCTP_DEBUG */
    711 			sctp_m_freem(m_ack);
    712 		}
    713 
    714 		/* get the entire parameter */
    715 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
    716 		if (aph == NULL) {
    717 			printf("Gag\n");
    718 			sctp_m_freem(m_ack);
    719 			return;
    720 		}
    721 		switch (param_type) {
    722 		case SCTP_ADD_IP_ADDRESS:
    723 			asoc->peer_supports_asconf = 1;
    724 			m_result = sctp_process_asconf_add_ip(aph, stcb, error);
    725 			break;
    726 		case SCTP_DEL_IP_ADDRESS:
    727 			asoc->peer_supports_asconf = 1;
    728 			m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
    729 			    error);
    730 			break;
    731 		case SCTP_ERROR_CAUSE_IND:
    732 			/* not valid in an ASCONF chunk */
    733 			break;
    734 		case SCTP_SET_PRIM_ADDR:
    735 			asoc->peer_supports_asconf_setprim = 1;
    736 			m_result = sctp_process_asconf_set_primary(aph, stcb,
    737 			    error);
    738 			break;
    739 		case SCTP_SUCCESS_REPORT:
    740 			/* not valid in an ASCONF chunk */
    741 			break;
    742 		case SCTP_ULP_ADAPTION:
    743 			/* FIX */
    744 			break;
    745 		default:
    746 			if ((param_type & 0x8000) == 0) {
    747 				/* Been told to STOP at this param */
    748 				asconf_limit = offset;
    749 				/* FIX FIX - We need to call sctp_arethere_unrecognized_parameters()
    750 				 * to get a operr and send it for any param's with the
    751 				 * 0x4000 bit set OR do it here ourselves... note we still
    752 				 * must STOP if the 0x8000 bit is clear.
    753 				 */
    754 			}
    755 			/* unknown/invalid param type */
    756 			break;
    757 		} /* switch */
    758 
    759 		/* add any (error) result to the reply mbuf chain */
    760 		if (m_result != NULL) {
    761 #ifdef SCTP_DEBUG
    762 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    763 				printf("handle_asconf: adding reply...\n");
    764 			}
    765 #endif /* SCTP_DEBUG */
    766 			m_tail->m_next = m_result;
    767 			m_tail = m_result;
    768 			/* update lengths, make sure it's aligned too */
    769 			m_result->m_len = SCTP_SIZE32(m_result->m_len);
    770 			m_ack->m_pkthdr.len += m_result->m_len;
    771 			ack_cp->ch.chunk_length += m_result->m_len;
    772 			/* set flag to force success reports */
    773 			error = 1;
    774 		}
    775 
    776 		offset += SCTP_SIZE32(param_length);
    777 		/* update remaining ASCONF message length to process */
    778 		if (offset >= asconf_limit) {
    779 			/* no more data in the mbuf chain */
    780 			break;
    781 		}
    782 		/* get pointer to next asconf param */
    783 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
    784 		    sizeof(struct sctp_asconf_paramhdr),
    785 		    (uint8_t *)&aparam_buf);
    786 		if (aph == NULL) {
    787 			/* can't get an asconf paramhdr */
    788 #ifdef SCTP_DEBUG
    789 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    790 				printf("handle_asconf: can't get asconf param hdr!\n");
    791 			}
    792 #endif /* SCTP_DEBUG */
    793 			/* FIX ME - add error here... */
    794 		}
    795 	} /* while */
    796 
    797 	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
    798 	/* save the ASCONF-ACK reply */
    799 	asoc->last_asconf_ack_sent = m_ack;
    800 	/* and send (a new one) it out... */
    801 	sctp_send_asconf_ack(stcb, 0);
    802 }
    803 
    804 /*
    805  * does the address match?
    806  * returns 0 if not, 1 if so
    807  */
    808 static uint32_t
    809 sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
    810 {
    811 #ifdef INET6
    812 	if (sa->sa_family == AF_INET6) {
    813 		/* IPv6 sa address */
    814 		/* XXX scopeid */
    815 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    816 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
    817 		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
    818 			    sizeof(struct in6_addr)) == 0)) {
    819 			return (1);
    820 		}
    821 	} else
    822 #endif /* INET6 */
    823 	if (sa->sa_family == AF_INET) {
    824 		/* IPv4 sa address */
    825 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    826 		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
    827 		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
    828 			    sizeof(struct in_addr)) == 0)) {
    829 			return (1);
    830 		}
    831 	}
    832 	return (0);
    833 }
    834 
    835 /*
    836  * Cleanup for non-responded/OP ERR'd ASCONF
    837  */
    838 void
    839 sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
    840 {
    841 #ifdef SCTP_DEBUG
    842 	if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    843 		printf("asconf_cleanup: marking peer ASCONF incapable and cleaning up\n");
    844 	}
    845 #endif /* SCTP_DEBUG */
    846 	/* mark peer as ASCONF incapable */
    847 	stcb->asoc.peer_supports_asconf = 0;
    848 	stcb->asoc.peer_supports_asconf_setprim = 0;
    849 	/*
    850 	 * clear out any existing asconfs going out
    851 	 */
    852 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
    853 	stcb->asoc.asconf_seq_out++;
    854 	/* remove the old ASCONF on our outbound queue */
    855 	sctp_toss_old_asconf(stcb);
    856 }
    857 
    858 /*
    859  * process an ADD/DELETE IP ack from peer
    860  * ifa:  corresponding ifaddr to the address being added/deleted
    861  * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS
    862  * flag: 1=success, 0=failure
    863  */
    864 static void
    865 sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct ifaddr *addr,
    866     uint16_t type, uint32_t flag)
    867 {
    868 
    869 	/*
    870 	 * do the necessary asoc list work-
    871 	 * if we get a failure indication, leave the address on the
    872 	 *   "do not use" asoc list
    873 	 * if we get a success indication, remove the address from
    874 	 *   the list
    875 	 */
    876 	/*
    877 	 * Note: this will only occur for ADD_IP_ADDRESS, since
    878 	 * DEL_IP_ADDRESS is never actually added to the list...
    879 	 */
    880 	if (flag) {
    881 		/* success case, so remove from the list */
    882 		sctp_del_local_addr_assoc(stcb, addr);
    883 	}
    884 	/* else, leave it on the list */
    885 }
    886 
    887 /*
    888  * add an asconf add/delete IP address parameter to the queue
    889  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR
    890  * returns 0 if completed, non-zero if not completed
    891  * NOTE: if adding, but delete already scheduled (and not yet
    892  * 	sent out), simply remove from queue.  Same for deleting
    893  *	an address already scheduled for add.  If a duplicate
    894  *	operation is found, ignore the new one.
    895  */
    896 static uint32_t
    897 sctp_asconf_queue_add(struct sctp_tcb *stcb, struct ifaddr *ifa, uint16_t type)
    898 {
    899 	struct sctp_asconf_addr *aa, *aa_next;
    900 #ifdef SCTP_DEBUG
    901 	char buf[128];	/* for address in string format */
    902 #endif /* SCTP_DEBUG */
    903 
    904 	/* see if peer supports ASCONF */
    905 	if (stcb->asoc.peer_supports_asconf == 0) {
    906 #ifdef SCTP_DEBUG
    907 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    908 			printf("asconf_queue_add: peer doesn't support ASCONF\n");
    909 		}
    910 #endif /* SCTP_DEBUG */
    911 		return (-1);
    912 	}
    913 
    914 	/* make sure the request isn't already in the queue */
    915 	for (aa=TAILQ_FIRST(&stcb->asoc.asconf_queue); aa!=NULL; aa=aa_next) {
    916 		aa_next = TAILQ_NEXT(aa, next);
    917 		/* address match? */
    918 		if (sctp_asconf_addr_match(aa, ifa->ifa_addr) == 0)
    919 			continue;
    920 		/* is the request already in queue (sent or not) */
    921 		if (aa->ap.aph.ph.param_type == type) {
    922 #ifdef SCTP_DEBUG
    923 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    924 				printf("asconf_queue_add: request already exists\n");
    925 			}
    926 #endif /* SCTP_DEBUG */
    927 			return (-1);
    928 		}
    929 		/* is the negative request already in queue, and not sent */
    930 		if (aa->sent == 0 &&
    931 		    /* add requested, delete already queued */
    932 		    ((type == SCTP_ADD_IP_ADDRESS &&
    933 		      aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) ||
    934 		     /* delete requested, add already queued */
    935 		     (type == SCTP_DEL_IP_ADDRESS &&
    936 		      aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS))) {
    937 			/* delete the existing entry in the queue */
    938 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
    939 			/* take the entry off the appropriate list */
    940 			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
    941 			/* free the entry */
    942 			free(aa, M_PCB);
    943 
    944 #ifdef SCTP_DEBUG
    945 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    946 				printf("asconf_queue_add: removing 'opposite' queued request\n");
    947 			}
    948 #endif /* SCTP_DEBUG */
    949 			return (-1);
    950 		}
    951 	} /* for each aa */
    952 
    953 	/* adding new request to the queue */
    954 	aa = malloc(sizeof(*aa), M_PCB, M_NOWAIT);
    955 	if (aa == NULL) {
    956 		/* didn't get memory */
    957 #ifdef SCTP_DEBUG
    958 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
    959 			printf("asconf_queue_add: failed to get memory!\n");
    960 		}
    961 #endif /* SCTP_DEBUG */
    962 		return (-1);
    963 	}
    964 	/* fill in asconf address parameter fields */
    965 	/* top level elements are "networked" during send */
    966 	aa->ap.aph.ph.param_type = type;
    967 	aa->ifa = ifa;
    968 	/* correlation_id filled in during send routine later... */
    969 	if (ifa->ifa_addr->sa_family == AF_INET6) {
    970 		/* IPv6 address */
    971 #ifdef SCTP_DEBUG
    972 		char ip6buf[INET6_ADDRSTRLEN];
    973 #endif
    974 		struct sockaddr_in6 *sin6;
    975 
    976 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    977 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
    978 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
    979 		aa->ap.aph.ph.param_length =
    980 		    sizeof(struct sctp_asconf_paramhdr) +
    981 		    sizeof(struct sctp_ipv6addr_param);
    982 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
    983 		    sizeof(struct in6_addr));
    984 #ifdef SCTP_DEBUG
    985 		strlcpy(buf, IN6_PRINT(ip6buf, &sin6->sin6_addr), sizeof(buf));
    986 #endif /* SCTP_DEBUG */
    987 
    988 	} else if (ifa->ifa_addr->sa_family == AF_INET) {
    989 		/* IPv4 address */
    990 		struct sockaddr_in *sin = (struct sockaddr_in *)ifa->ifa_addr;
    991 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
    992 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
    993 		aa->ap.aph.ph.param_length =
    994 		    sizeof(struct sctp_asconf_paramhdr) +
    995 		    sizeof(struct sctp_ipv4addr_param);
    996 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
    997 		    sizeof(struct in_addr));
    998 #ifdef SCTP_DEBUG
    999 		strlcpy(buf, inet_ntoa(sin->sin_addr), sizeof(buf));
   1000 #endif /* SCTP_DEBUG */
   1001 	} else {
   1002 		/* invalid family! */
   1003 		return (-1);
   1004 	}
   1005 	aa->sent = 0;			/* clear sent flag */
   1006 
   1007 	/*
   1008 	 * if we are deleting an address it should go out last
   1009 	 * otherwise, add it to front of the pending queue
   1010 	 */
   1011 	if (type == SCTP_ADD_IP_ADDRESS) {
   1012 		/* add goes to the front of the queue */
   1013 		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
   1014 #ifdef SCTP_DEBUG
   1015 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1016 			printf("asconf_queue_add: appended asconf ADD_IP_ADDRESS: %s\n", buf);
   1017 		}
   1018 #endif /* SCTP_DEBUG */
   1019 	} else {
   1020 		/* delete and set primary goes to the back of the queue */
   1021 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
   1022 #ifdef SCTP_DEBUG
   1023 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1024 			if (type == SCTP_DEL_IP_ADDRESS) {
   1025 				printf("asconf_queue_add: inserted asconf DEL_IP_ADDRESS: %s\n", buf);
   1026 			} else {
   1027 				printf("asconf_queue_add: inserted asconf SET_PRIM_ADDR: %s\n", buf);
   1028 			}
   1029 		}
   1030 #endif /* SCTP_DEBUG */
   1031 	}
   1032 
   1033 	return (0);
   1034 }
   1035 
   1036 /*
   1037  * add an asconf add/delete IP address parameter to the queue by addr
   1038  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR
   1039  * returns 0 if completed, non-zero if not completed
   1040  * NOTE: if adding, but delete already scheduled (and not yet
   1041  * 	sent out), simply remove from queue.  Same for deleting
   1042  *	an address already scheduled for add.  If a duplicate
   1043  *	operation is found, ignore the new one.
   1044  */
   1045 static uint32_t
   1046 sctp_asconf_queue_add_sa(struct sctp_tcb *stcb, struct sockaddr *sa,
   1047     uint16_t type)
   1048 {
   1049 	struct sctp_asconf_addr *aa, *aa_next;
   1050 
   1051 	/* see if peer supports ASCONF */
   1052 	if (stcb->asoc.peer_supports_asconf == 0) {
   1053 #ifdef SCTP_DEBUG
   1054 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1055 			printf("asconf_queue_add_sa: peer doesn't support ASCONF\n");
   1056 		}
   1057 #endif /* SCTP_DEBUG */
   1058 		return (-1);
   1059 	}
   1060 
   1061 	/* make sure the request isn't already in the queue */
   1062 	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
   1063 	    aa = aa_next) {
   1064 		aa_next = TAILQ_NEXT(aa, next);
   1065 		/* address match? */
   1066 		if (sctp_asconf_addr_match(aa, sa) == 0)
   1067 			continue;
   1068 		/* is the request already in queue (sent or not) */
   1069 		if (aa->ap.aph.ph.param_type == type) {
   1070 #ifdef SCTP_DEBUG
   1071 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1072 				printf("asconf_queue_add_sa: request already exists\n");
   1073 			}
   1074 #endif /* SCTP_DEBUG */
   1075 			return (-1);
   1076 		}
   1077 
   1078 		/* is the negative request already in queue, and not sent */
   1079 		if (aa->sent == 1)
   1080 			continue;
   1081 		if (type == SCTP_ADD_IP_ADDRESS &&
   1082 		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
   1083 			/* add requested, delete already queued */
   1084 
   1085 			/* delete the existing entry in the queue */
   1086 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
   1087 			/* free the entry */
   1088 			free(aa, M_PCB);
   1089 #ifdef SCTP_DEBUG
   1090 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1091 				printf("asconf_queue_add_sa: removing queued delete request\n");
   1092 			}
   1093 #endif /* SCTP_DEBUG */
   1094 			return (-1);
   1095 		} else if (type == SCTP_DEL_IP_ADDRESS &&
   1096 			   aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
   1097 			/* delete requested, add already queued */
   1098 
   1099 			/* delete the existing entry in the queue */
   1100 			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
   1101 			/* take the entry off the appropriate list */
   1102 			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
   1103 			/* free the entry */
   1104 			free(aa, M_PCB);
   1105 #ifdef SCTP_DEBUG
   1106 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1107 				printf("asconf_queue_add_sa: removing queued add request\n");
   1108 			}
   1109 #endif /* SCTP_DEBUG */
   1110 			return (-1);
   1111 		}
   1112 	} /* for each aa */
   1113 
   1114 	/* adding new request to the queue */
   1115 	aa = malloc(sizeof(*aa), M_PCB, M_NOWAIT);
   1116 	if (aa == NULL) {
   1117 		/* didn't get memory */
   1118 #ifdef SCTP_DEBUG
   1119 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1120 			printf("asconf_queue_add_sa: failed to get memory!\n");
   1121 		}
   1122 #endif /* SCTP_DEBUG */
   1123 		return (-1);
   1124 	}
   1125 	/* fill in asconf address parameter fields */
   1126 	/* top level elements are "networked" during send */
   1127 	aa->ap.aph.ph.param_type = type;
   1128 	aa->ifa = sctp_find_ifa_by_addr(sa);
   1129 	/* correlation_id filled in during send routine later... */
   1130 	if (sa->sa_family == AF_INET6) {
   1131 		/* IPv6 address */
   1132 		struct sockaddr_in6 *sin6;
   1133 
   1134 		sin6 = (struct sockaddr_in6 *)sa;
   1135 		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
   1136 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
   1137 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
   1138 		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
   1139 		    sizeof(struct in6_addr));
   1140 	} else if (sa->sa_family == AF_INET) {
   1141 		/* IPv4 address */
   1142 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
   1143 		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
   1144 		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
   1145 		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
   1146 		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
   1147 		    sizeof(struct in_addr));
   1148 	} else {
   1149 		/* invalid family! */
   1150 		return (-1);
   1151 	}
   1152 	aa->sent = 0;			/* clear sent flag */
   1153 
   1154 	/*
   1155 	 * if we are deleting an address it should go out last
   1156 	 * otherwise, add it to front of the pending queue
   1157 	 */
   1158 	if (type == SCTP_ADD_IP_ADDRESS) {
   1159 		/* add goes to the front of the queue */
   1160 		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
   1161 #ifdef SCTP_DEBUG
   1162 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1163 			printf("asconf_queue_add_sa: appended asconf ADD_IP_ADDRESS\n");
   1164 		}
   1165 #endif /* SCTP_DEBUG */
   1166 	} else {
   1167 		/* delete and set primary goes to the back of the queue */
   1168 		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
   1169 #ifdef SCTP_DEBUG
   1170 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1171 			if (type == SCTP_DEL_IP_ADDRESS) {
   1172 				printf("asconf_queue_add_sa: inserted asconf DEL_IP_ADDRESS\n");
   1173 			} else {
   1174 				printf("asconf_queue_add_sa: inserted asconf SET_PRIM_ADDR\n");
   1175 			}
   1176 		}
   1177 #endif /* SCTP_DEBUG */
   1178 	}
   1179 
   1180 	return (0);
   1181 }
   1182 
   1183 /*
   1184  * find a specific asconf param on our "sent" queue
   1185  */
   1186 static struct sctp_asconf_addr *
   1187 sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
   1188 {
   1189 	struct sctp_asconf_addr *aa;
   1190 
   1191 	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
   1192 		if (aa->ap.aph.correlation_id == correlation_id &&
   1193 		    aa->sent == 1) {
   1194 			/* found it */
   1195 			return (aa);
   1196 		}
   1197 	}
   1198 	/* didn't find it */
   1199 	return (NULL);
   1200 }
   1201 
   1202 /*
   1203  * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter
   1204  * and do notifications based on the error response
   1205  */
   1206 static void
   1207 sctp_asconf_process_error(struct sctp_tcb *stcb,
   1208     struct sctp_asconf_paramhdr *aph)
   1209 {
   1210 	struct sctp_error_cause *eh;
   1211 	struct sctp_paramhdr *ph;
   1212 	uint16_t param_type;
   1213 	uint16_t error_code;
   1214 
   1215 	eh = (struct sctp_error_cause *)(aph + 1);
   1216 	ph = (struct sctp_paramhdr *)(eh + 1);
   1217 	/* validate lengths */
   1218 	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
   1219 	    htons(aph->ph.param_length)) {
   1220 		/* invalid error cause length */
   1221 #ifdef SCTP_DEBUG
   1222 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1223 			printf("asconf_process_error: cause element too long\n");
   1224 		}
   1225 #endif /* SCTP_DEBUG */
   1226 		return;
   1227 	}
   1228 	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
   1229 	    htons(eh->length)) {
   1230 		/* invalid included TLV length */
   1231 #ifdef SCTP_DEBUG
   1232 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1233 			printf("asconf_process_error: included TLV too long\n");
   1234 		}
   1235 #endif /* SCTP_DEBUG */
   1236 		return;
   1237 	}
   1238 
   1239 	/* which error code ? */
   1240 	error_code = ntohs(eh->code);
   1241 	param_type = ntohs(aph->ph.param_type);
   1242 	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
   1243 	switch (error_code) {
   1244 	case SCTP_ERROR_RESOURCE_SHORTAGE:
   1245 		/* we allow ourselves to "try again" for this error */
   1246 		break;
   1247 	default:
   1248 		/* peer can't handle it... */
   1249 		switch (param_type) {
   1250 		case SCTP_ADD_IP_ADDRESS:
   1251 		case SCTP_DEL_IP_ADDRESS:
   1252 			stcb->asoc.peer_supports_asconf = 0;
   1253 			break;
   1254 		case SCTP_SET_PRIM_ADDR:
   1255 			stcb->asoc.peer_supports_asconf_setprim = 0;
   1256 			break;
   1257 		default:
   1258 			break;
   1259 		}
   1260 	}
   1261 }
   1262 
   1263 /*
   1264  * process an asconf queue param
   1265  * aparam: parameter to process, will be removed from the queue
   1266  * flag: 1=success, 0=failure
   1267  */
   1268 static void
   1269 sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
   1270     struct sctp_asconf_addr *aparam, uint32_t flag)
   1271 {
   1272 	uint16_t param_type;
   1273 
   1274 	/* process this param */
   1275 	param_type = aparam->ap.aph.ph.param_type;
   1276 #ifdef SCTP_DEBUG
   1277 	if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1278 		printf("process_param_ack: handling asconf parameter type=%xh\n", param_type);
   1279 	}
   1280 #endif /* SCTP_DEBUG */
   1281 	switch (param_type) {
   1282 	case SCTP_ADD_IP_ADDRESS:
   1283 #ifdef SCTP_DEBUG
   1284 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1285 			printf("process_param_ack: added IP address\n");
   1286 		}
   1287 #endif /* SCTP_DEBUG */
   1288 		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
   1289 		break;
   1290 	case SCTP_DEL_IP_ADDRESS:
   1291 #ifdef SCTP_DEBUG
   1292 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1293 			printf("process_param_ack: deleted IP address\n");
   1294 		}
   1295 #endif /* SCTP_DEBUG */
   1296 		/* nothing really to do... lists already updated */
   1297 		break;
   1298 	case SCTP_SET_PRIM_ADDR:
   1299 		/* nothing to do... peer may start using this addr */
   1300 		if (flag == 0)
   1301 			stcb->asoc.peer_supports_asconf_setprim = 0;
   1302 		break;
   1303 	default:
   1304 		/* should NEVER happen */
   1305 		break;
   1306 	} /* switch */
   1307 
   1308 	/* remove the param and free it */
   1309 	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
   1310 	free(aparam, M_PCB);
   1311 }
   1312 
   1313 /*
   1314  * cleanup from a bad asconf ack parameter
   1315  */
   1316 static void
   1317 sctp_asconf_ack_clear(struct sctp_tcb *stcb)
   1318 {
   1319 	/* assume peer doesn't really know how to do asconfs */
   1320 	stcb->asoc.peer_supports_asconf = 0;
   1321 	stcb->asoc.peer_supports_asconf_setprim = 0;
   1322 	/* XXX we could free the pending queue here */
   1323 }
   1324 
   1325 void
   1326 sctp_handle_asconf_ack(struct mbuf *m, int offset,
   1327     struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
   1328     struct sctp_nets *net)
   1329 {
   1330 	struct sctp_association *asoc;
   1331 	uint32_t serial_num;
   1332 	uint16_t ack_length;
   1333 	struct sctp_asconf_paramhdr *aph;
   1334 	struct sctp_asconf_addr *aa, *aa_next;
   1335 	uint32_t last_error_id = 0;		/* last error correlation id */
   1336 	uint32_t id;
   1337 	struct sctp_asconf_addr *ap;
   1338 	/* asconf param buffer */
   1339 	static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
   1340 
   1341 	/* verify minimum length */
   1342 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
   1343 #ifdef SCTP_DEBUG
   1344 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1345 			printf("handle_asconf_ack: chunk too small = %xh\n",
   1346 			       ntohs(cp->ch.chunk_length));
   1347 		}
   1348 #endif /* SCTP_DEBUG */
   1349 		return;
   1350 	}
   1351 
   1352 	asoc = &stcb->asoc;
   1353 	serial_num = ntohl(cp->serial_number);
   1354 
   1355 	/*
   1356 	 * NOTE: we may want to handle this differently- currently, we
   1357 	 * will abort when we get an ack for the expected serial number + 1
   1358 	 * (eg. we didn't send it), process an ack normally if it is the
   1359 	 * expected serial number, and re-send the previous ack for *ALL*
   1360 	 * other serial numbers
   1361 	 */
   1362 
   1363 	/*
   1364 	 * if the serial number is the next expected, but I didn't send it,
   1365 	 * abort the asoc, since someone probably just hijacked us...
   1366 	 */
   1367 	if (serial_num == (asoc->asconf_seq_out + 1)) {
   1368 		sctp_abort_an_association(stcb->sctp_ep, stcb,
   1369 		    SCTP_ERROR_ILLEGAL_ASCONF_ACK, NULL);
   1370 #ifdef SCTP_DEBUG
   1371 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1372 			printf("handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
   1373 		}
   1374 #endif /* SCTP_DEBUG */
   1375 		return;
   1376 	}
   1377 
   1378 	if (serial_num != asoc->asconf_seq_out) {
   1379 		/* got a duplicate/unexpected ASCONF-ACK */
   1380 #ifdef SCTP_DEBUG
   1381 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1382 			printf("handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n", serial_num, asoc->asconf_seq_out);
   1383 		}
   1384 #endif /* SCTP_DEBUG */
   1385 		return;
   1386 	}
   1387 	/* stop our timer */
   1388 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
   1389 
   1390 	/* process the ASCONF-ACK contents */
   1391 	ack_length = ntohs(cp->ch.chunk_length) -
   1392 	    sizeof(struct sctp_asconf_ack_chunk);
   1393 	offset += sizeof(struct sctp_asconf_ack_chunk);
   1394 	/* process through all parameters */
   1395 	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
   1396 		unsigned int param_length, param_type;
   1397 
   1398 		/* get pointer to next asconf parameter */
   1399 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
   1400 		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
   1401 		if (aph == NULL) {
   1402 			/* can't get an asconf paramhdr */
   1403 			sctp_asconf_ack_clear(stcb);
   1404 			return;
   1405 		}
   1406 		param_type = ntohs(aph->ph.param_type);
   1407 		param_length = ntohs(aph->ph.param_length);
   1408 		if (param_length > ack_length) {
   1409 			sctp_asconf_ack_clear(stcb);
   1410 			return;
   1411 		}
   1412 		if (param_length < sizeof(struct sctp_paramhdr)) {
   1413 			sctp_asconf_ack_clear(stcb);
   1414 			return;
   1415 		}
   1416 
   1417 		/* get the complete parameter... */
   1418 		if (param_length > sizeof(aparam_buf)) {
   1419 #ifdef SCTP_DEBUG
   1420 			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1421 				printf("param length (%u) larger than buffer size!\n", param_length);
   1422 			}
   1423 #endif /* SCTP_DEBUG */
   1424 			sctp_asconf_ack_clear(stcb);
   1425 			return;
   1426 		}
   1427 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
   1428 		if (aph == NULL) {
   1429 			sctp_asconf_ack_clear(stcb);
   1430 			return;
   1431 		}
   1432 		/* correlation_id is transparent to peer, no ntohl needed */
   1433 		id = aph->correlation_id;
   1434 
   1435 		switch (param_type) {
   1436 		case SCTP_ERROR_CAUSE_IND:
   1437 			last_error_id = id;
   1438 			/* find the corresponding asconf param in our queue */
   1439 			ap = sctp_asconf_find_param(stcb, id);
   1440 			if (ap == NULL) {
   1441 				/* hmm... can't find this in our queue! */
   1442 				break;
   1443 			}
   1444 			/* process the parameter, failed flag */
   1445 			sctp_asconf_process_param_ack(stcb, ap, 0);
   1446 			/* process the error response */
   1447 			sctp_asconf_process_error(stcb, aph);
   1448 			break;
   1449 		case SCTP_SUCCESS_REPORT:
   1450 			/* find the corresponding asconf param in our queue */
   1451 			ap = sctp_asconf_find_param(stcb, id);
   1452 			if (ap == NULL) {
   1453 				/* hmm... can't find this in our queue! */
   1454 				break;
   1455 			}
   1456 			/* process the parameter, success flag */
   1457 			sctp_asconf_process_param_ack(stcb, ap, 1);
   1458 			break;
   1459 		default:
   1460 			break;
   1461 		} /* switch */
   1462 
   1463 		/* update remaining ASCONF-ACK message length to process */
   1464 		ack_length -= SCTP_SIZE32(param_length);
   1465 		if (ack_length <= 0) {
   1466 			/* no more data in the mbuf chain */
   1467 			break;
   1468 		}
   1469 		offset += SCTP_SIZE32(param_length);
   1470 	} /* while */
   1471 
   1472 	/*
   1473 	 * if there are any "sent" params still on the queue, these are
   1474 	 * implicitly "success", or "failed" (if we got an error back)
   1475 	 * ... so process these appropriately
   1476 	 *
   1477 	 * we assume that the correlation_id's are monotonically increasing
   1478 	 * beginning from 1 and that we don't have *that* many outstanding
   1479 	 * at any given time
   1480 	 */
   1481 	if (last_error_id == 0)
   1482 		last_error_id--;	/* set to "max" value */
   1483 	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
   1484 	    aa = aa_next) {
   1485 		aa_next = TAILQ_NEXT(aa, next);
   1486 		if (aa->sent == 1) {
   1487 			/*
   1488 			 * implicitly successful or failed
   1489 			 * if correlation_id < last_error_id, then success
   1490 			 * else, failure
   1491 			 */
   1492 			if (aa->ap.aph.correlation_id < last_error_id)
   1493 				sctp_asconf_process_param_ack(stcb, aa,
   1494 				    SCTP_SUCCESS_REPORT);
   1495 			else
   1496 				sctp_asconf_process_param_ack(stcb, aa,
   1497 				    SCTP_ERROR_CAUSE_IND);
   1498 		} else {
   1499 			/*
   1500 			 * since we always process in order (FIFO queue)
   1501 			 * if we reach one that hasn't been sent, the
   1502 			 * rest should not have been sent either.
   1503 			 * so, we're done...
   1504 			 */
   1505 			break;
   1506 		}
   1507 	}
   1508 
   1509 	/* update the next sequence number to use */
   1510 	asoc->asconf_seq_out++;
   1511 	/* remove the old ASCONF on our outbound queue */
   1512 	sctp_toss_old_asconf(stcb);
   1513 	/* clear the sent flag to allow new ASCONFs */
   1514 	asoc->asconf_sent = 0;
   1515 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
   1516 		/* we have more params, so restart our timer */
   1517 		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
   1518 		    stcb, net);
   1519 	}
   1520 }
   1521 
   1522 /* is this an interface that we care about at all? */
   1523 static uint32_t
   1524 sctp_is_desired_interface_type(struct ifaddr *ifa)
   1525 {
   1526 	int result;
   1527 
   1528 	/* check the interface type to see if it's one we care about */
   1529 	switch (ifa->ifa_ifp->if_type) {
   1530 	case IFT_ETHER:
   1531 	case IFT_ISO88023:
   1532 	case IFT_ISO88025:
   1533 	case IFT_STARLAN:
   1534 	case IFT_P10:
   1535 	case IFT_P80:
   1536 	case IFT_HY:
   1537 	case IFT_FDDI:
   1538 	case IFT_PPP:
   1539 	case IFT_XETHER:
   1540 	case IFT_SLIP:
   1541 	case IFT_GIF:
   1542 		result = 1;
   1543 		break;
   1544 	default:
   1545 #ifdef SCTP_DEBUG
   1546 		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
   1547 			printf("ignoring interface type = %u\n",
   1548 			       ifa->ifa_ifp->if_type);
   1549 		}
   1550 #endif /* SCTP_DEBUG */
   1551 		result = 0;
   1552 	} /* end switch */
   1553 
   1554 	return (result);
   1555 }
   1556 
   1557 static uint32_t
   1558 sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
   1559 {
   1560 	struct sockaddr_in6 *sin6;
   1561 	const struct sockaddr_in6 *net6;
   1562 	struct sctp_nets *net;
   1563 
   1564 	if (sa->sa_family != AF_INET6) {
   1565 		/* wrong family */
   1566 		return (0);
   1567 	}
   1568 
   1569 	sin6 = (struct sockaddr_in6 *)sa;
   1570 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
   1571 		/* not link local address */
   1572 		return (0);
   1573 	}
   1574 	/* hunt through our destination nets list for this scope_id */
   1575 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1576 		if ((rtcache_getdst(&net->ro))->sa_family !=
   1577 		    AF_INET6)
   1578 			continue;
   1579 		net6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1580 		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
   1581 			continue;
   1582 		if (sctp_is_same_scope(sin6, net6)) {
   1583 			/* found one */
   1584 			return (1);
   1585 		}
   1586 	}
   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