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