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