tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sctp_asconf.c (105158B)


      1 /*-
      2 * SPDX-License-Identifier: BSD-3-Clause
      3 *
      4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
      5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
      6 * Copyright (c) 2008-2012, by Michael Tuexen. 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 are met:
     10 *
     11 * a) Redistributions of source code must retain the above copyright notice,
     12 *    this list of conditions and the following disclaimer.
     13 *
     14 * b) Redistributions in binary form must reproduce the above copyright
     15 *    notice, this list of conditions and the following disclaimer in
     16 *    the documentation and/or other materials provided with the distribution.
     17 *
     18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
     19 *    contributors may be used to endorse or promote products derived
     20 *    from this software without specific prior written permission.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32 * THE POSSIBILITY OF SUCH DAMAGE.
     33 */
     34 
     35 #include <netinet/sctp_os.h>
     36 #include <netinet/sctp_var.h>
     37 #include <netinet/sctp_sysctl.h>
     38 #include <netinet/sctp_pcb.h>
     39 #include <netinet/sctp_header.h>
     40 #include <netinet/sctputil.h>
     41 #include <netinet/sctp_output.h>
     42 #include <netinet/sctp_asconf.h>
     43 #include <netinet/sctp_timer.h>
     44 
     45 /*
     46 * debug flags:
     47 * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
     48 * SCTP_DEBUG_ASCONF2: detailed info
     49 */
     50 
     51 /*
     52 * RFC 5061
     53 *
     54 * An ASCONF parameter queue exists per asoc which holds the pending address
     55 * operations.  Lists are updated upon receipt of ASCONF-ACK.
     56 *
     57 * A restricted_addrs list exists per assoc to hold local addresses that are
     58 * not (yet) usable by the assoc as a source address.  These addresses are
     59 * either pending an ASCONF operation (and exist on the ASCONF parameter
     60 * queue), or they are permanently restricted (the peer has returned an
     61 * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
     62 *
     63 * Deleted addresses are always immediately removed from the lists as they will
     64 * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
     65 * only if allowed.
     66 */
     67 
     68 /*
     69 * ASCONF parameter processing.
     70 * response_required: set if a reply is required (eg. SUCCESS_REPORT).
     71 * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
     72 * FIX: allocating this many mbufs on the fly is pretty inefficient...
     73 */
     74 static struct mbuf *
     75 sctp_asconf_success_response(uint32_t id)
     76 {
     77 struct mbuf *m_reply = NULL;
     78 struct sctp_asconf_paramhdr *aph;
     79 
     80 m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
     81 				0, M_NOWAIT, 1, MT_DATA);
     82 if (m_reply == NULL) {
     83 	SCTPDBG(SCTP_DEBUG_ASCONF1,
     84 		"asconf_success_response: couldn't get mbuf!\n");
     85 	return (NULL);
     86 }
     87 aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
     88 aph->correlation_id = id;
     89 aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
     90 aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
     91 SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
     92 aph->ph.param_length = htons(aph->ph.param_length);
     93 
     94 return (m_reply);
     95 }
     96 
     97 static struct mbuf *
     98 sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
     99                           uint16_t tlv_length)
    100 {
    101 struct mbuf *m_reply = NULL;
    102 struct sctp_asconf_paramhdr *aph;
    103 struct sctp_error_cause *error;
    104 uint32_t buf_len;
    105 uint16_t i, param_length, cause_length, padding_length;
    106 uint8_t *tlv;
    107 
    108 if (error_tlv == NULL) {
    109 	tlv_length = 0;
    110 }
    111 cause_length = sizeof(struct sctp_error_cause) + tlv_length;
    112 param_length = sizeof(struct sctp_asconf_paramhdr) + cause_length;
    113 padding_length = tlv_length % 4;
    114 if (padding_length != 0) {
    115 	padding_length = 4 - padding_length;
    116 }
    117 buf_len = param_length + padding_length;
    118 if (buf_len > MLEN) {
    119 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    120 		"asconf_error_response: tlv_length (%xh) too big\n",
    121 		tlv_length);
    122 	return (NULL);
    123 }
    124 m_reply = sctp_get_mbuf_for_msg(buf_len, 0, M_NOWAIT, 1, MT_DATA);
    125 if (m_reply == NULL) {
    126 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    127 		"asconf_error_response: couldn't get mbuf!\n");
    128 	return (NULL);
    129 }
    130 aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
    131 aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
    132 aph->ph.param_length = htons(param_length);
    133 aph->correlation_id = id;
    134 error = (struct sctp_error_cause *)(aph + 1);
    135 error->code = htons(cause);
    136 error->length = htons(cause_length);
    137 if (error_tlv != NULL) {
    138 	tlv = (uint8_t *) (error + 1);
    139 	memcpy(tlv, error_tlv, tlv_length);
    140 	for (i = 0; i < padding_length; i++) {
    141 		tlv[tlv_length + i] = 0;
    142 	}
    143 }
    144 SCTP_BUF_LEN(m_reply) = buf_len;
    145 return (m_reply);
    146 }
    147 
    148 static struct mbuf *
    149 sctp_process_asconf_add_ip(struct sockaddr *src, struct sctp_asconf_paramhdr *aph,
    150                           struct sctp_tcb *stcb, int send_hb, int response_required)
    151 {
    152 struct sctp_nets *net;
    153 struct mbuf *m_reply = NULL;
    154 union sctp_sockstore store;
    155 struct sctp_paramhdr *ph;
    156 uint16_t param_type, aparam_length;
    157 #if defined(INET) || defined(INET6)
    158 uint16_t param_length;
    159 #endif
    160 struct sockaddr *sa;
    161 int zero_address = 0;
    162 int bad_address = 0;
    163 #ifdef INET
    164 struct sockaddr_in *sin;
    165 struct sctp_ipv4addr_param *v4addr;
    166 #endif
    167 #ifdef INET6
    168 struct sockaddr_in6 *sin6;
    169 struct sctp_ipv6addr_param *v6addr;
    170 #endif
    171 
    172 aparam_length = ntohs(aph->ph.param_length);
    173 if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
    174 	return (NULL);
    175 }
    176 ph = (struct sctp_paramhdr *)(aph + 1);
    177 param_type = ntohs(ph->param_type);
    178 #if defined(INET) || defined(INET6)
    179 param_length = ntohs(ph->param_length);
    180 if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
    181 	return (NULL);
    182 }
    183 #endif
    184 sa = &store.sa;
    185 switch (param_type) {
    186 #ifdef INET
    187 case SCTP_IPV4_ADDRESS:
    188 	if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    189 		/* invalid param size */
    190 		return (NULL);
    191 	}
    192 	v4addr = (struct sctp_ipv4addr_param *)ph;
    193 	sin = &store.sin;
    194 	memset(sin, 0, sizeof(*sin));
    195 	sin->sin_family = AF_INET;
    196 #ifdef HAVE_SIN_LEN
    197 	sin->sin_len = sizeof(struct sockaddr_in);
    198 #endif
    199 	sin->sin_port = stcb->rport;
    200 	sin->sin_addr.s_addr = v4addr->addr;
    201 	if ((sin->sin_addr.s_addr == INADDR_BROADCAST) ||
    202 	    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
    203 		bad_address = 1;
    204 	}
    205 	if (sin->sin_addr.s_addr == INADDR_ANY)
    206 		zero_address = 1;
    207 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
    208 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    209 	break;
    210 #endif
    211 #ifdef INET6
    212 case SCTP_IPV6_ADDRESS:
    213 	if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    214 		/* invalid param size */
    215 		return (NULL);
    216 	}
    217 	v6addr = (struct sctp_ipv6addr_param *)ph;
    218 	sin6 = &store.sin6;
    219 	memset(sin6, 0, sizeof(*sin6));
    220 	sin6->sin6_family = AF_INET6;
    221 #ifdef HAVE_SIN6_LEN
    222 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    223 #endif
    224 	sin6->sin6_port = stcb->rport;
    225 	memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
    226 	    sizeof(struct in6_addr));
    227 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    228 		bad_address = 1;
    229 	}
    230 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
    231 		zero_address = 1;
    232 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
    233 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    234 	break;
    235 #endif
    236 default:
    237 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    238 	    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
    239 	    aparam_length);
    240 	return (m_reply);
    241 }			/* end switch */
    242 
    243 /* if 0.0.0.0/::0, add the source address instead */
    244 if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
    245 	sa = src;
    246 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    247 	        "process_asconf_add_ip: using source addr ");
    248 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
    249 }
    250 net = NULL;
    251 /* add the address */
    252 if (bad_address) {
    253 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    254 	    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
    255 	    aparam_length);
    256 } else if (sctp_add_remote_addr(stcb, sa, &net, stcb->asoc.port,
    257                                 SCTP_DONOT_SETSCOPE,
    258                                 SCTP_ADDR_DYNAMIC_ADDED) != 0) {
    259 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    260 		"process_asconf_add_ip: error adding address\n");
    261 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    262 	    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
    263 	    aparam_length);
    264 } else {
    265 	if (response_required) {
    266 		m_reply =
    267 		    sctp_asconf_success_response(aph->correlation_id);
    268 	}
    269 	if (net != NULL) {
    270 		/* notify upper layer */
    271 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
    272 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
    273 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
    274 				 stcb, net);
    275 		if (send_hb) {
    276 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
    277 		}
    278 	}
    279 }
    280 return (m_reply);
    281 }
    282 
    283 static int
    284 sctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
    285 {
    286 struct sctp_nets *src_net, *net, *nnet;
    287 
    288 /* make sure the source address exists as a destination net */
    289 src_net = sctp_findnet(stcb, src);
    290 if (src_net == NULL) {
    291 	/* not found */
    292 	return (-1);
    293 }
    294 
    295 /* delete all destination addresses except the source */
    296 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
    297 	if (net != src_net) {
    298 		/* delete this address */
    299 		SCTPDBG(SCTP_DEBUG_ASCONF1,
    300 			"asconf_del_remote_addrs_except: deleting ");
    301 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
    302 			     (struct sockaddr *)&net->ro._l_addr);
    303 		/* notify upper layer */
    304 		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
    305 		    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
    306 		sctp_remove_net(stcb, net);
    307 	}
    308 }
    309 return (0);
    310 }
    311 
    312 static struct mbuf *
    313 sctp_process_asconf_delete_ip(struct sockaddr *src,
    314                              struct sctp_asconf_paramhdr *aph,
    315 		      struct sctp_tcb *stcb, int response_required)
    316 {
    317 struct mbuf *m_reply = NULL;
    318 union sctp_sockstore store;
    319 struct sctp_paramhdr *ph;
    320 uint16_t param_type, aparam_length;
    321 #if defined(INET) || defined(INET6)
    322 uint16_t param_length;
    323 #endif
    324 struct sockaddr *sa;
    325 int zero_address = 0;
    326 int result;
    327 #ifdef INET
    328 struct sockaddr_in *sin;
    329 struct sctp_ipv4addr_param *v4addr;
    330 #endif
    331 #ifdef INET6
    332 struct sockaddr_in6 *sin6;
    333 struct sctp_ipv6addr_param *v6addr;
    334 #endif
    335 
    336 aparam_length = ntohs(aph->ph.param_length);
    337 if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
    338 	return (NULL);
    339 }
    340 ph = (struct sctp_paramhdr *)(aph + 1);
    341 param_type = ntohs(ph->param_type);
    342 #if defined(INET) || defined(INET6)
    343 param_length = ntohs(ph->param_length);
    344 if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
    345 	return (NULL);
    346 }
    347 #endif
    348 sa = &store.sa;
    349 switch (param_type) {
    350 #ifdef INET
    351 case SCTP_IPV4_ADDRESS:
    352 	if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    353 		/* invalid param size */
    354 		return (NULL);
    355 	}
    356 	v4addr = (struct sctp_ipv4addr_param *)ph;
    357 	sin = &store.sin;
    358 	memset(sin, 0, sizeof(*sin));
    359 	sin->sin_family = AF_INET;
    360 #ifdef HAVE_SIN_LEN
    361 	sin->sin_len = sizeof(struct sockaddr_in);
    362 #endif
    363 	sin->sin_port = stcb->rport;
    364 	sin->sin_addr.s_addr = v4addr->addr;
    365 	if (sin->sin_addr.s_addr == INADDR_ANY)
    366 		zero_address = 1;
    367 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    368 		"process_asconf_delete_ip: deleting ");
    369 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    370 	break;
    371 #endif
    372 #ifdef INET6
    373 case SCTP_IPV6_ADDRESS:
    374 	if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    375 		/* invalid param size */
    376 		return (NULL);
    377 	}
    378 	v6addr = (struct sctp_ipv6addr_param *)ph;
    379 	sin6 = &store.sin6;
    380 	memset(sin6, 0, sizeof(*sin6));
    381 	sin6->sin6_family = AF_INET6;
    382 #ifdef HAVE_SIN6_LEN
    383 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    384 #endif
    385 	sin6->sin6_port = stcb->rport;
    386 	memcpy(&sin6->sin6_addr, v6addr->addr,
    387 	    sizeof(struct in6_addr));
    388 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
    389 		zero_address = 1;
    390 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    391 		"process_asconf_delete_ip: deleting ");
    392 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    393 	break;
    394 #endif
    395 default:
    396 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    397 	    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
    398 	    aparam_length);
    399 	return (m_reply);
    400 }
    401 
    402 /* make sure the source address is not being deleted */
    403 if (sctp_cmpaddr(sa, src)) {
    404 	/* trying to delete the source address! */
    405 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
    406 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    407 	    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
    408 	    aparam_length);
    409 	return (m_reply);
    410 }
    411 
    412 /* if deleting 0.0.0.0/::0, delete all addresses except src addr */
    413 if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
    414 	result = sctp_asconf_del_remote_addrs_except(stcb, src);
    415 
    416 	if (result) {
    417 		/* src address did not exist? */
    418 		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
    419 		/* what error to reply with?? */
    420 		m_reply =
    421 		    sctp_asconf_error_response(aph->correlation_id,
    422 		    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
    423 		    aparam_length);
    424 	} else if (response_required) {
    425 		m_reply =
    426 		    sctp_asconf_success_response(aph->correlation_id);
    427 	}
    428 	return (m_reply);
    429 }
    430 
    431 /* delete the address */
    432 result = sctp_del_remote_addr(stcb, sa);
    433 /*
    434  * note if result == -2, the address doesn't exist in the asoc but
    435  * since it's being deleted anyways, we just ack the delete -- but
    436  * this probably means something has already gone awry
    437  */
    438 if (result == -1) {
    439 	/* only one address in the asoc */
    440 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
    441 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    442 	    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
    443 	    aparam_length);
    444 } else {
    445 	if (response_required) {
    446 		m_reply = sctp_asconf_success_response(aph->correlation_id);
    447 	}
    448 	/* notify upper layer */
    449 	sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
    450 }
    451 return (m_reply);
    452 }
    453 
    454 static struct mbuf *
    455 sctp_process_asconf_set_primary(struct sockaddr *src,
    456 			struct sctp_asconf_paramhdr *aph,
    457 			struct sctp_tcb *stcb, int response_required)
    458 {
    459 struct mbuf *m_reply = NULL;
    460 union sctp_sockstore store;
    461 struct sctp_paramhdr *ph;
    462 uint16_t param_type, aparam_length;
    463 #if defined(INET) || defined(INET6)
    464 uint16_t param_length;
    465 #endif
    466 struct sockaddr *sa;
    467 int zero_address = 0;
    468 #ifdef INET
    469 struct sockaddr_in *sin;
    470 struct sctp_ipv4addr_param *v4addr;
    471 #endif
    472 #ifdef INET6
    473 struct sockaddr_in6 *sin6;
    474 struct sctp_ipv6addr_param *v6addr;
    475 #endif
    476 
    477 aparam_length = ntohs(aph->ph.param_length);
    478 if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
    479 	return (NULL);
    480 }
    481 ph = (struct sctp_paramhdr *)(aph + 1);
    482 param_type = ntohs(ph->param_type);
    483 #if defined(INET) || defined(INET6)
    484 param_length = ntohs(ph->param_length);
    485 if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
    486 	return (NULL);
    487 }
    488 #endif
    489 sa = &store.sa;
    490 switch (param_type) {
    491 #ifdef INET
    492 case SCTP_IPV4_ADDRESS:
    493 	if (param_length != sizeof(struct sctp_ipv4addr_param)) {
    494 		/* invalid param size */
    495 		return (NULL);
    496 	}
    497 	v4addr = (struct sctp_ipv4addr_param *)ph;
    498 	sin = &store.sin;
    499 	memset(sin, 0, sizeof(*sin));
    500 	sin->sin_family = AF_INET;
    501 #ifdef HAVE_SIN_LEN
    502 	sin->sin_len = sizeof(struct sockaddr_in);
    503 #endif
    504 	sin->sin_addr.s_addr = v4addr->addr;
    505 	if (sin->sin_addr.s_addr == INADDR_ANY)
    506 		zero_address = 1;
    507 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
    508 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    509 	break;
    510 #endif
    511 #ifdef INET6
    512 case SCTP_IPV6_ADDRESS:
    513 	if (param_length != sizeof(struct sctp_ipv6addr_param)) {
    514 		/* invalid param size */
    515 		return (NULL);
    516 	}
    517 	v6addr = (struct sctp_ipv6addr_param *)ph;
    518 	sin6 = &store.sin6;
    519 	memset(sin6, 0, sizeof(*sin6));
    520 	sin6->sin6_family = AF_INET6;
    521 #ifdef HAVE_SIN6_LEN
    522 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    523 #endif
    524 	memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
    525 	    sizeof(struct in6_addr));
    526 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
    527 		zero_address = 1;
    528 	SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
    529 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
    530 	break;
    531 #endif
    532 default:
    533 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    534 	    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
    535 	    aparam_length);
    536 	return (m_reply);
    537 }
    538 
    539 /* if 0.0.0.0/::0, use the source address instead */
    540 if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
    541 	sa = src;
    542 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    543 		"process_asconf_set_primary: using source addr ");
    544 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
    545 }
    546 /* set the primary address */
    547 if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
    548 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    549 		"process_asconf_set_primary: primary address set\n");
    550 	/* notify upper layer */
    551 	sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
    552 	if ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_REACHABLE) &&
    553 	    ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_PF) == 0) &&
    554 	    (stcb->asoc.alternate != NULL)) {
    555 		sctp_free_remote_addr(stcb->asoc.alternate);
    556 		stcb->asoc.alternate = NULL;
    557 	}
    558 	if (response_required) {
    559 		m_reply = sctp_asconf_success_response(aph->correlation_id);
    560 	}
    561 	/* Mobility adaptation.
    562 	   Ideally, when the reception of SET PRIMARY with DELETE IP
    563 	   ADDRESS of the previous primary destination, unacknowledged
    564 	   DATA are retransmitted immediately to the new primary
    565 	   destination for seamless handover.
    566 	   If the destination is UNCONFIRMED and marked to REQ_PRIM,
    567 	   The retransmission occur when reception of the
    568 	   HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
    569 	   sctp_input.c)
    570 	   Also, when change of the primary destination, it is better
    571 	   that all subsequent new DATA containing already queued DATA
    572 	   are transmitted to the new primary destination. (by micchie)
    573 	 */
    574 	if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
    575 	                                 SCTP_MOBILITY_BASE) ||
    576 	    sctp_is_mobility_feature_on(stcb->sctp_ep,
    577 	                                SCTP_MOBILITY_FASTHANDOFF)) &&
    578 	    sctp_is_mobility_feature_on(stcb->sctp_ep,
    579 	                                SCTP_MOBILITY_PRIM_DELETED) &&
    580 	    (stcb->asoc.primary_destination->dest_state &
    581 	     SCTP_ADDR_UNCONFIRMED) == 0) {
    582 		sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
    583 		                stcb->sctp_ep, stcb, NULL,
    584 		                SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
    585 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
    586 				SCTP_MOBILITY_FASTHANDOFF)) {
    587 			sctp_assoc_immediate_retrans(stcb,
    588 					stcb->asoc.primary_destination);
    589 		}
    590 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
    591 				SCTP_MOBILITY_BASE)) {
    592 			sctp_move_chunks_from_net(stcb,
    593 					stcb->asoc.deleted_primary);
    594 		}
    595 		sctp_delete_prim_timer(stcb->sctp_ep, stcb);
    596 	}
    597 } else {
    598 	/* couldn't set the requested primary address! */
    599 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    600 		"process_asconf_set_primary: set primary failed!\n");
    601 	/* must have been an invalid address, so report */
    602 	m_reply = sctp_asconf_error_response(aph->correlation_id,
    603 	    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
    604 	    aparam_length);
    605 }
    606 
    607 return (m_reply);
    608 }
    609 
    610 /*
    611 * handles an ASCONF chunk.
    612 * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
    613 */
    614 void
    615 sctp_handle_asconf(struct mbuf *m, unsigned int offset,
    616                   struct sockaddr *src,
    617 	   struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
    618 	   int first)
    619 {
    620 struct sctp_association *asoc;
    621 uint32_t serial_num;
    622 struct mbuf *n, *m_ack, *m_result, *m_tail;
    623 struct sctp_asconf_ack_chunk *ack_cp;
    624 struct sctp_asconf_paramhdr *aph;
    625 struct sctp_ipv6addr_param *p_addr;
    626 unsigned int asconf_limit, cnt;
    627 int error = 0;		/* did an error occur? */
    628 
    629 /* asconf param buffer */
    630 uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
    631 struct sctp_asconf_ack *ack, *ack_next;
    632 
    633 /* verify minimum length */
    634 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
    635 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    636 		"handle_asconf: chunk too small = %xh\n",
    637 		ntohs(cp->ch.chunk_length));
    638 	return;
    639 }
    640 asoc = &stcb->asoc;
    641 serial_num = ntohl(cp->serial_number);
    642 
    643 if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
    644 	/* got a duplicate ASCONF */
    645 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    646 		"handle_asconf: got duplicate serial number = %xh\n",
    647 		serial_num);
    648 	return;
    649 } else if (serial_num != (asoc->asconf_seq_in + 1)) {
    650 	SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
    651 		serial_num, asoc->asconf_seq_in + 1);
    652 	return;
    653 }
    654 
    655 /* it's the expected "next" sequence number, so process it */
    656 asoc->asconf_seq_in = serial_num;	/* update sequence */
    657 /* get length of all the param's in the ASCONF */
    658 asconf_limit = offset + ntohs(cp->ch.chunk_length);
    659 SCTPDBG(SCTP_DEBUG_ASCONF1,
    660 	"handle_asconf: asconf_limit=%u, sequence=%xh\n",
    661 	asconf_limit, serial_num);
    662 
    663 if (first) {
    664 	/* delete old cache */
    665 	SCTPDBG(SCTP_DEBUG_ASCONF1,"handle_asconf: Now processing first ASCONF. Try to delete old cache\n");
    666 
    667 	TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
    668 		if (ack->serial_number == serial_num)
    669 			break;
    670 		SCTPDBG(SCTP_DEBUG_ASCONF1,"handle_asconf: delete old(%u) < first(%u)\n",
    671 		    ack->serial_number, serial_num);
    672 		TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
    673 		if (ack->data != NULL) {
    674 			sctp_m_freem(ack->data);
    675 		}
    676 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
    677 	}
    678 }
    679 
    680 m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
    681 			      M_NOWAIT, 1, MT_DATA);
    682 if (m_ack == NULL) {
    683 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    684 		"handle_asconf: couldn't get mbuf!\n");
    685 	return;
    686 }
    687 m_tail = m_ack;		/* current reply chain's tail */
    688 
    689 /* fill in ASCONF-ACK header */
    690 ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
    691 ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
    692 ack_cp->ch.chunk_flags = 0;
    693 ack_cp->serial_number = htonl(serial_num);
    694 /* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
    695 SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
    696 ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
    697 
    698 /* skip the lookup address parameter */
    699 offset += sizeof(struct sctp_asconf_chunk);
    700 p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
    701 if (p_addr == NULL) {
    702 	SCTPDBG(SCTP_DEBUG_ASCONF1,
    703 		"handle_asconf: couldn't get lookup addr!\n");
    704 	/* respond with a missing/invalid mandatory parameter error */
    705 	sctp_m_freem(m_ack);
    706 	return;
    707 }
    708 /* skip lookup addr */
    709 offset += SCTP_SIZE32(ntohs(p_addr->ph.param_length));
    710 /* get pointer to first asconf param in ASCONF */
    711 aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
    712 if (aph == NULL) {
    713 	SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
    714 	goto send_reply;
    715 }
    716 /* process through all parameters */
    717 cnt = 0;
    718 while (aph != NULL) {
    719 	unsigned int param_length, param_type;
    720 
    721 	param_type = ntohs(aph->ph.param_type);
    722 	param_length = ntohs(aph->ph.param_length);
    723 	if (offset + param_length > asconf_limit) {
    724 		/* parameter goes beyond end of chunk! */
    725 		sctp_m_freem(m_ack);
    726 		return;
    727 	}
    728 	m_result = NULL;
    729 
    730 	if (param_length > sizeof(aparam_buf)) {
    731 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
    732 		sctp_m_freem(m_ack);
    733 		return;
    734 	}
    735 	if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
    736 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
    737 		sctp_m_freem(m_ack);
    738 		return;
    739 	}
    740 	/* get the entire parameter */
    741 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
    742 	if (aph == NULL) {
    743 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
    744 		sctp_m_freem(m_ack);
    745 		return;
    746 	}
    747 	switch (param_type) {
    748 	case SCTP_ADD_IP_ADDRESS:
    749 		m_result = sctp_process_asconf_add_ip(src, aph, stcb,
    750 		    (cnt < SCTP_BASE_SYSCTL(sctp_hb_maxburst)), error);
    751 		cnt++;
    752 		break;
    753 	case SCTP_DEL_IP_ADDRESS:
    754 		m_result = sctp_process_asconf_delete_ip(src, aph, stcb,
    755 		    error);
    756 		break;
    757 	case SCTP_ERROR_CAUSE_IND:
    758 		/* not valid in an ASCONF chunk */
    759 		break;
    760 	case SCTP_SET_PRIM_ADDR:
    761 		m_result = sctp_process_asconf_set_primary(src, aph,
    762 		    stcb, error);
    763 		break;
    764 	case SCTP_NAT_VTAGS:
    765 	        SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
    766 	        break;
    767 	case SCTP_SUCCESS_REPORT:
    768 		/* not valid in an ASCONF chunk */
    769 		break;
    770 	case SCTP_ULP_ADAPTATION:
    771 		/* FIX */
    772 		break;
    773 	default:
    774 		if ((param_type & 0x8000) == 0) {
    775 			/* Been told to STOP at this param */
    776 			asconf_limit = offset;
    777 			/*
    778 			 * FIX FIX - We need to call
    779 			 * sctp_arethere_unrecognized_parameters()
    780 			 * to get a operr and send it for any
    781 			 * param's with the 0x4000 bit set OR do it
    782 			 * here ourselves... note we still must STOP
    783 			 * if the 0x8000 bit is clear.
    784 			 */
    785 		}
    786 		/* unknown/invalid param type */
    787 		break;
    788 	} /* switch */
    789 
    790 	/* add any (error) result to the reply mbuf chain */
    791 	if (m_result != NULL) {
    792 		SCTP_BUF_NEXT(m_tail) = m_result;
    793 		m_tail = m_result;
    794 		ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
    795 		/* set flag to force success reports */
    796 		error = 1;
    797 	}
    798 	offset += SCTP_SIZE32(param_length);
    799 	/* update remaining ASCONF message length to process */
    800 	if (offset >= asconf_limit) {
    801 		/* no more data in the mbuf chain */
    802 		break;
    803 	}
    804 	/* get pointer to next asconf param */
    805 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
    806 	    sizeof(struct sctp_asconf_paramhdr),
    807 	    (uint8_t *)&aparam_buf);
    808 	if (aph == NULL) {
    809 		/* can't get an asconf paramhdr */
    810 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
    811 		/* FIX ME - add error here... */
    812 	}
    813 }
    814 
    815 send_reply:
    816 ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
    817 /* save the ASCONF-ACK reply */
    818 ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
    819     struct sctp_asconf_ack);
    820 if (ack == NULL) {
    821 	sctp_m_freem(m_ack);
    822 	return;
    823 }
    824 ack->serial_number = serial_num;
    825 ack->last_sent_to = NULL;
    826 ack->data = m_ack;
    827 ack->len = 0;
    828 for (n = m_ack; n != NULL; n = SCTP_BUF_NEXT(n)) {
    829 	ack->len += SCTP_BUF_LEN(n);
    830 }
    831 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
    832 
    833 /* see if last_control_chunk_from is set properly (use IP src addr) */
    834 if (stcb->asoc.last_control_chunk_from == NULL) {
    835 	/*
    836 	 * this could happen if the source address was just newly
    837 	 * added
    838 	 */
    839 	SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
    840 	SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
    841 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
    842 	/* look up the from address */
    843 	stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, src);
    844 #ifdef SCTP_DEBUG
    845 	if (stcb->asoc.last_control_chunk_from == NULL) {
    846 		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
    847 	}
    848 #endif
    849 }
    850 }
    851 
    852 /*
    853 * does the address match? returns 0 if not, 1 if so
    854 */
    855 static uint32_t
    856 sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
    857 {
    858 switch (sa->sa_family) {
    859 #ifdef INET6
    860 case AF_INET6:
    861 {
    862 	/* XXX scopeid */
    863 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    864 
    865 	if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
    866 	    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
    867 	    sizeof(struct in6_addr)) == 0)) {
    868 		return (1);
    869 	}
    870 	break;
    871 }
    872 #endif
    873 #ifdef INET
    874 case AF_INET:
    875 {
    876 	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    877 
    878 	if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
    879 	    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
    880 	    sizeof(struct in_addr)) == 0)) {
    881 		return (1);
    882 	}
    883 	break;
    884 }
    885 #endif
    886 default:
    887 	break;
    888 }
    889 return (0);
    890 }
    891 
    892 /*
    893 * does the address match? returns 0 if not, 1 if so
    894 */
    895 static uint32_t
    896 sctp_addr_match(struct sctp_paramhdr *ph, struct sockaddr *sa)
    897 {
    898 #if defined(INET) || defined(INET6)
    899 uint16_t param_type, param_length;
    900 
    901 param_type = ntohs(ph->param_type);
    902 param_length = ntohs(ph->param_length);
    903 #endif
    904 switch (sa->sa_family) {
    905 #ifdef INET6
    906 case AF_INET6:
    907 {
    908 	/* XXX scopeid */
    909 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    910 	struct sctp_ipv6addr_param *v6addr;
    911 
    912 	v6addr = (struct sctp_ipv6addr_param *)ph;
    913 	if ((param_type == SCTP_IPV6_ADDRESS) &&
    914 	    (param_length == sizeof(struct sctp_ipv6addr_param)) &&
    915 	    (memcmp(&v6addr->addr, &sin6->sin6_addr,
    916 	    sizeof(struct in6_addr)) == 0)) {
    917 		return (1);
    918 	}
    919 	break;
    920 }
    921 #endif
    922 #ifdef INET
    923 case AF_INET:
    924 {
    925 	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    926 	struct sctp_ipv4addr_param *v4addr;
    927 
    928 	v4addr = (struct sctp_ipv4addr_param *)ph;
    929 	if ((param_type == SCTP_IPV4_ADDRESS) &&
    930 	    (param_length == sizeof(struct sctp_ipv4addr_param)) &&
    931 	    (memcmp(&v4addr->addr, &sin->sin_addr,
    932 	    sizeof(struct in_addr)) == 0)) {
    933 		return (1);
    934 	}
    935 	break;
    936 }
    937 #endif
    938 default:
    939 	break;
    940 }
    941 return (0);
    942 }
    943 /*
    944 * Cleanup for non-responded/OP ERR'd ASCONF
    945 */
    946 void
    947 sctp_asconf_cleanup(struct sctp_tcb *stcb)
    948 {
    949 /*
    950  * clear out any existing asconfs going out
    951  */
    952 sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
    953 		SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
    954 stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
    955 /* remove the old ASCONF on our outbound queue */
    956 sctp_toss_old_asconf(stcb);
    957 }
    958 
    959 /*
    960 * cleanup any cached source addresses that may be topologically
    961 * incorrect after a new address has been added to this interface.
    962 */
    963 static void
    964 sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
    965 {
    966 struct sctp_nets *net;
    967 
    968 /*
    969  * Ideally, we want to only clear cached routes and source addresses
    970  * that are topologically incorrect.  But since there is no easy way
    971  * to know whether the newly added address on the ifn would cause a
    972  * routing change (i.e. a new egress interface would be chosen)
    973  * without doing a new routing lookup and source address selection,
    974  * we will (for now) just flush any cached route using a different
    975  * ifn (and cached source addrs) and let output re-choose them during
    976  * the next send on that net.
    977  */
    978 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
    979 	/*
    980 	 * clear any cached route (and cached source address) if the
    981 	 * route's interface is NOT the same as the address change.
    982 	 * If it's the same interface, just clear the cached source
    983 	 * address.
    984 	 */
    985 	if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
    986 	    ((ifn == NULL) ||
    987 	     (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
    988 		/* clear any cached route */
    989 #if defined(__FreeBSD__) && !defined(__Userspace__)
    990 		RO_NHFREE(&net->ro);
    991 #else
    992 		RTFREE(net->ro.ro_rt);
    993 		net->ro.ro_rt = NULL;
    994 #endif
    995 	}
    996 	/* clear any cached source address */
    997 	if (net->src_addr_selected) {
    998 		sctp_free_ifa(net->ro._s_addr);
    999 		net->ro._s_addr = NULL;
   1000 		net->src_addr_selected = 0;
   1001 	}
   1002 }
   1003 }
   1004 
   1005 void
   1006 sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
   1007 {
   1008 int error;
   1009 
   1010 if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
   1011 	return;
   1012 }
   1013 if (stcb->asoc.deleted_primary == NULL) {
   1014 	return;
   1015 }
   1016 
   1017 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
   1018 	SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
   1019 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
   1020 	SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
   1021 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
   1022 	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
   1023 			stcb->asoc.deleted_primary,
   1024 			SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
   1025 	stcb->asoc.num_send_timers_up--;
   1026 	if (stcb->asoc.num_send_timers_up < 0) {
   1027 		stcb->asoc.num_send_timers_up = 0;
   1028 	}
   1029 	SCTP_TCB_LOCK_ASSERT(stcb);
   1030 	error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
   1031 				stcb->asoc.deleted_primary);
   1032 	if (error) {
   1033 		SCTP_INP_DECR_REF(stcb->sctp_ep);
   1034 		return;
   1035 	}
   1036 	SCTP_TCB_LOCK_ASSERT(stcb);
   1037 #ifdef SCTP_AUDITING_ENABLED
   1038 	sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
   1039 #endif
   1040 	sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
   1041 	if ((stcb->asoc.num_send_timers_up == 0) &&
   1042 	    (stcb->asoc.sent_queue_cnt > 0)) {
   1043 		struct sctp_tmit_chunk *chk;
   1044 
   1045 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
   1046 			if (chk->whoTo != NULL) {
   1047 				break;
   1048 			}
   1049 		}
   1050 		if (chk != NULL) {
   1051 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
   1052 		}
   1053 	}
   1054 }
   1055 return;
   1056 }
   1057 
   1058 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
   1059 static int
   1060 sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
   1061 
   1062 void
   1063 sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
   1064 {
   1065 struct sctp_tmit_chunk *chk;
   1066 
   1067 SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
   1068 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
   1069                 SCTP_FROM_SCTP_ASCONF + SCTP_LOC_4);
   1070 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
   1071 net->error_count = 0;
   1072 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
   1073 	if (chk->whoTo == net) {
   1074 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
   1075 			chk->sent = SCTP_DATAGRAM_RESEND;
   1076 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1077 			sctp_flight_size_decrease(chk);
   1078 			sctp_total_flight_decrease(stcb, chk);
   1079 			net->marked_retrans++;
   1080 			stcb->asoc.marked_retrans++;
   1081 		}
   1082 	}
   1083 }
   1084 if (net->marked_retrans) {
   1085 	sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
   1086 }
   1087 }
   1088 
   1089 static void
   1090 sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
   1091 {
   1092 struct sctp_nets *net;
   1093 int addrnum, changed;
   1094 
   1095 /*   If number of local valid addresses is 1, the valid address is
   1096      probably newly added address.
   1097      Several valid addresses in this association.  A source address
   1098      may not be changed.  Additionally, they can be configured on a
   1099      same interface as "alias" addresses.  (by micchie)
   1100  */
   1101 addrnum = sctp_local_addr_count(stcb);
   1102 SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
   1103 	addrnum);
   1104 if (addrnum == 1) {
   1105 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1106 		/* clear any cached route and source address */
   1107 #if defined(__FreeBSD__) && !defined(__Userspace__)
   1108 		RO_NHFREE(&net->ro);
   1109 #else
   1110 		if (net->ro.ro_rt) {
   1111 			RTFREE(net->ro.ro_rt);
   1112 			net->ro.ro_rt = NULL;
   1113 		}
   1114 #endif
   1115 		if (net->src_addr_selected) {
   1116 			sctp_free_ifa(net->ro._s_addr);
   1117 			net->ro._s_addr = NULL;
   1118 			net->src_addr_selected = 0;
   1119 		}
   1120 		/* Retransmit unacknowledged DATA chunks immediately */
   1121 		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
   1122 		                                SCTP_MOBILITY_FASTHANDOFF)) {
   1123 			sctp_net_immediate_retrans(stcb, net);
   1124 		}
   1125 		/* also, SET PRIMARY is maybe already sent */
   1126 	}
   1127 	return;
   1128 }
   1129 
   1130 /* Multiple local addresses exist in the association.  */
   1131 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1132 	/* clear any cached route and source address */
   1133 #if defined(__FreeBSD__) && !defined(__Userspace__)
   1134 	RO_NHFREE(&net->ro);
   1135 #else
   1136 	if (net->ro.ro_rt) {
   1137 		RTFREE(net->ro.ro_rt);
   1138 		net->ro.ro_rt = NULL;
   1139 	}
   1140 #endif
   1141 	if (net->src_addr_selected) {
   1142 		sctp_free_ifa(net->ro._s_addr);
   1143 		net->ro._s_addr = NULL;
   1144 		net->src_addr_selected = 0;
   1145 	}
   1146 	/* Check if the nexthop is corresponding to the new address.
   1147 	   If the new address is corresponding to the current nexthop,
   1148 	   the path will be changed.
   1149 	   If the new address is NOT corresponding to the current
   1150 	   nexthop, the path will not be changed.
   1151 	 */
   1152 	SCTP_RTALLOC((sctp_route_t *)&net->ro,
   1153 		     stcb->sctp_ep->def_vrf_id,
   1154 		     stcb->sctp_ep->fibnum);
   1155 #if defined(__FreeBSD__) && !defined(__Userspace__)
   1156 	if (net->ro.ro_nh == NULL)
   1157 #else
   1158 	if (net->ro.ro_rt == NULL)
   1159 #endif
   1160 		continue;
   1161 
   1162 	changed = 0;
   1163 	switch (net->ro._l_addr.sa.sa_family) {
   1164 #ifdef INET
   1165 	case AF_INET:
   1166 		if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *)&net->ro)) {
   1167 			changed = 1;
   1168 		}
   1169 		break;
   1170 #endif
   1171 #ifdef INET6
   1172 	case AF_INET6:
   1173 		if (sctp_v6src_match_nexthop(
   1174 		    &newifa->address.sin6, (sctp_route_t *)&net->ro)) {
   1175 			changed = 1;
   1176 		}
   1177 		break;
   1178 #endif
   1179 	default:
   1180 		break;
   1181 	}
   1182 	/* if the newly added address does not relate routing
   1183 	   information, we skip.
   1184 	 */
   1185 	if (changed == 0)
   1186 		continue;
   1187 	/* Retransmit unacknowledged DATA chunks immediately */
   1188 	if (sctp_is_mobility_feature_on(stcb->sctp_ep,
   1189 	                                SCTP_MOBILITY_FASTHANDOFF)) {
   1190 		sctp_net_immediate_retrans(stcb, net);
   1191 	}
   1192 	/* Send SET PRIMARY for this new address */
   1193 	if (net == stcb->asoc.primary_destination) {
   1194 		(void)sctp_asconf_queue_mgmt(stcb, newifa,
   1195 					     SCTP_SET_PRIM_ADDR);
   1196 	}
   1197 }
   1198 }
   1199 #endif
   1200 
   1201 /*
   1202 * process an ADD/DELETE IP ack from peer.
   1203 * addr: corresponding sctp_ifa to the address being added/deleted.
   1204 * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
   1205 * flag: 1=success, 0=failure.
   1206 */
   1207 static void
   1208 sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr, uint32_t flag)
   1209 {
   1210 /*
   1211  * do the necessary asoc list work- if we get a failure indication,
   1212  * leave the address on the assoc's restricted list.  If we get a
   1213  * success indication, remove the address from the restricted list.
   1214  */
   1215 /*
   1216  * Note: this will only occur for ADD_IP_ADDRESS, since
   1217  * DEL_IP_ADDRESS is never actually added to the list...
   1218  */
   1219 if (flag) {
   1220 	/* success case, so remove from the restricted list */
   1221 	sctp_del_local_addr_restricted(stcb, addr);
   1222 
   1223 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
   1224 	if (sctp_is_mobility_feature_on(stcb->sctp_ep,
   1225 	                                SCTP_MOBILITY_BASE) ||
   1226 	    sctp_is_mobility_feature_on(stcb->sctp_ep,
   1227 	                                SCTP_MOBILITY_FASTHANDOFF)) {
   1228 		sctp_path_check_and_react(stcb, addr);
   1229 		return;
   1230 	}
   1231 #endif
   1232 	/* clear any cached/topologically incorrect source addresses */
   1233 	sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
   1234 }
   1235 /* else, leave it on the list */
   1236 }
   1237 
   1238 /*
   1239 * add an asconf add/delete/set primary IP address parameter to the queue.
   1240 * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
   1241 * returns 0 if queued, -1 if not queued/removed.
   1242 * NOTE: if adding, but a delete for the same address is already scheduled
   1243 * (and not yet sent out), simply remove it from queue.  Same for deleting
   1244 * an address already scheduled for add.  If a duplicate operation is found,
   1245 * ignore the new one.
   1246 */
   1247 static int
   1248 sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
   1249 	       uint16_t type)
   1250 {
   1251 struct sctp_asconf_addr *aa, *aa_next;
   1252 
   1253 /* make sure the request isn't already in the queue */
   1254 TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
   1255 	/* address match? */
   1256 	if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
   1257 		continue;
   1258 	/* is the request already in queue but not sent?
   1259 	 * pass the request already sent in order to resolve the following case:
   1260 	 *  1. arrival of ADD, then sent
   1261 	 *  2. arrival of DEL. we can't remove the ADD request already sent
   1262 	 *  3. arrival of ADD
   1263 	 */
   1264 	if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
   1265 		return (-1);
   1266 	}
   1267 	/* is the negative request already in queue, and not sent */
   1268 	if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
   1269 	    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
   1270 		/* add requested, delete already queued */
   1271 		TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
   1272 		/* remove the ifa from the restricted list */
   1273 		sctp_del_local_addr_restricted(stcb, ifa);
   1274 		/* free the asconf param */
   1275 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
   1276 		SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
   1277 		return (-1);
   1278 	}
   1279 	if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
   1280 	    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
   1281 		/* delete requested, add already queued */
   1282 		TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
   1283 		/* remove the aa->ifa from the restricted list */
   1284 		sctp_del_local_addr_restricted(stcb, aa->ifa);
   1285 		/* free the asconf param */
   1286 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
   1287 		SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
   1288 		return (-1);
   1289 	}
   1290 } /* for each aa */
   1291 
   1292 /* adding new request to the queue */
   1293 SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
   1294 	    SCTP_M_ASC_ADDR);
   1295 if (aa == NULL) {
   1296 	/* didn't get memory */
   1297 	SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
   1298 	return (-1);
   1299 }
   1300 aa->special_del = 0;
   1301 /* fill in asconf address parameter fields */
   1302 /* top level elements are "networked" during send */
   1303 aa->ap.aph.ph.param_type = type;
   1304 aa->ifa = ifa;
   1305 atomic_add_int(&ifa->refcount, 1);
   1306 /* correlation_id filled in during send routine later... */
   1307 switch (ifa->address.sa.sa_family) {
   1308 #ifdef INET6
   1309 case AF_INET6:
   1310 {
   1311 	struct sockaddr_in6 *sin6;
   1312 
   1313 	sin6 = &ifa->address.sin6;
   1314 	aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
   1315 	aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
   1316 	aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
   1317 	    sizeof(struct sctp_ipv6addr_param);
   1318 	memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
   1319 	       sizeof(struct in6_addr));
   1320 	break;
   1321 }
   1322 #endif
   1323 #ifdef INET
   1324 case AF_INET:
   1325 {
   1326 	struct sockaddr_in *sin;
   1327 
   1328 	sin = &ifa->address.sin;
   1329 	aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
   1330 	aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
   1331 	aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
   1332 	    sizeof(struct sctp_ipv4addr_param);
   1333 	memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
   1334 	       sizeof(struct in_addr));
   1335 	break;
   1336 }
   1337 #endif
   1338 default:
   1339 	/* invalid family! */
   1340 	SCTP_FREE(aa, SCTP_M_ASC_ADDR);
   1341 	sctp_free_ifa(ifa);
   1342 	return (-1);
   1343 }
   1344 aa->sent = 0;		/* clear sent flag */
   1345 
   1346 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
   1347 #ifdef SCTP_DEBUG
   1348 if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
   1349 	if (type == SCTP_ADD_IP_ADDRESS) {
   1350 		SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
   1351 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
   1352 	} else if (type == SCTP_DEL_IP_ADDRESS) {
   1353 		SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
   1354 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
   1355 	} else {
   1356 		SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
   1357 		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
   1358 	}
   1359 }
   1360 #endif
   1361 
   1362 return (0);
   1363 }
   1364 
   1365 /*
   1366 * add an asconf operation for the given ifa and type.
   1367 * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
   1368 * returns 0 if completed, -1 if not completed, 1 if immediate send is
   1369 * advisable.
   1370 */
   1371 static int
   1372 sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
   1373 	      uint16_t type)
   1374 {
   1375 uint32_t status;
   1376 int pending_delete_queued = 0;
   1377 int last;
   1378 
   1379 /* see if peer supports ASCONF */
   1380 if (stcb->asoc.asconf_supported == 0) {
   1381 	return (-1);
   1382 }
   1383 
   1384 /*
   1385  * if this is deleting the last address from the assoc, mark it as
   1386  * pending.
   1387  */
   1388 if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending) {
   1389 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1390 		last = (sctp_local_addr_count(stcb) == 0);
   1391 	} else {
   1392 		last = (sctp_local_addr_count(stcb) == 1);
   1393 	}
   1394 	if (last) {
   1395 		/* set the pending delete info only */
   1396 		stcb->asoc.asconf_del_pending = 1;
   1397 		stcb->asoc.asconf_addr_del_pending = ifa;
   1398 		atomic_add_int(&ifa->refcount, 1);
   1399 		SCTPDBG(SCTP_DEBUG_ASCONF2,
   1400 			"asconf_queue_add: mark delete last address pending\n");
   1401 		return (-1);
   1402 	}
   1403 }
   1404 
   1405 /* queue an asconf parameter */
   1406 status = sctp_asconf_queue_mgmt(stcb, ifa, type);
   1407 
   1408 /*
   1409  * if this is an add, and there is a delete also pending (i.e. the
   1410  * last local address is being changed), queue the pending delete too.
   1411  */
   1412 if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
   1413 	/* queue in the pending delete */
   1414 	if (sctp_asconf_queue_mgmt(stcb,
   1415 				   stcb->asoc.asconf_addr_del_pending,
   1416 				   SCTP_DEL_IP_ADDRESS) == 0) {
   1417 		SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queuing pending delete\n");
   1418 		pending_delete_queued = 1;
   1419 		/* clear out the pending delete info */
   1420 		stcb->asoc.asconf_del_pending = 0;
   1421 		sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
   1422 		stcb->asoc.asconf_addr_del_pending = NULL;
   1423 	}
   1424 }
   1425 
   1426 if (pending_delete_queued) {
   1427 	struct sctp_nets *net;
   1428 	/*
   1429 	 * since we know that the only/last address is now being
   1430 	 * changed in this case, reset the cwnd/rto on all nets to
   1431 	 * start as a new address and path.  Also clear the error
   1432 	 * counts to give the assoc the best chance to complete the
   1433 	 * address change.
   1434 	 */
   1435 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1436 		stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
   1437 								  net);
   1438 		net->RTO = 0;
   1439 		net->error_count = 0;
   1440 	}
   1441 	stcb->asoc.overall_error_count = 0;
   1442 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
   1443 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
   1444 			       stcb->asoc.overall_error_count,
   1445 			       0,
   1446 			       SCTP_FROM_SCTP_ASCONF,
   1447 			       __LINE__);
   1448 	}
   1449 
   1450 	/* queue in an advisory set primary too */
   1451 	(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
   1452 	/* let caller know we should send this out immediately */
   1453 	status = 1;
   1454 }
   1455 return (status);
   1456 }
   1457 
   1458 /*-
   1459 * add an asconf delete IP address parameter to the queue by sockaddr and
   1460 * possibly with no sctp_ifa available.  This is only called by the routine
   1461 * that checks the addresses in an INIT-ACK against the current address list.
   1462 * returns 0 if completed, non-zero if not completed.
   1463 * NOTE: if an add is already scheduled (and not yet sent out), simply
   1464 * remove it from queue.  If a duplicate operation is found, ignore the
   1465 * new one.
   1466 */
   1467 static int
   1468 sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
   1469 {
   1470 struct sctp_ifa *ifa;
   1471 struct sctp_asconf_addr *aa, *aa_next;
   1472 
   1473 if (stcb == NULL) {
   1474 	return (-1);
   1475 }
   1476 /* see if peer supports ASCONF */
   1477 if (stcb->asoc.asconf_supported == 0) {
   1478 	return (-1);
   1479 }
   1480 /* make sure the request isn't already in the queue */
   1481 TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
   1482 	/* address match? */
   1483 	if (sctp_asconf_addr_match(aa, sa) == 0)
   1484 		continue;
   1485 	/* is the request already in queue (sent or not) */
   1486 	if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
   1487 		return (-1);
   1488 	}
   1489 	/* is the negative request already in queue, and not sent */
   1490 	if (aa->sent == 1)
   1491 		continue;
   1492 	if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
   1493 		/* add already queued, so remove existing entry */
   1494 		TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
   1495 		sctp_del_local_addr_restricted(stcb, aa->ifa);
   1496 		/* free the entry */
   1497 		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
   1498 		return (-1);
   1499 	}
   1500 } /* for each aa */
   1501 
   1502 /* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
   1503 ifa = sctp_find_ifa_by_addr(sa, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
   1504 
   1505 /* adding new request to the queue */
   1506 SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
   1507 	    SCTP_M_ASC_ADDR);
   1508 if (aa == NULL) {
   1509 	/* didn't get memory */
   1510 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1511 		"sctp_asconf_queue_sa_delete: failed to get memory!\n");
   1512 	return (-1);
   1513 }
   1514 aa->special_del = 0;
   1515 /* fill in asconf address parameter fields */
   1516 /* top level elements are "networked" during send */
   1517 aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
   1518 aa->ifa = ifa;
   1519 if (ifa)
   1520 	atomic_add_int(&ifa->refcount, 1);
   1521 /* correlation_id filled in during send routine later... */
   1522 switch (sa->sa_family) {
   1523 #ifdef INET6
   1524 case AF_INET6:
   1525 {
   1526 	/* IPv6 address */
   1527 	struct sockaddr_in6 *sin6;
   1528 
   1529 	sin6 = (struct sockaddr_in6 *)sa;
   1530 	aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
   1531 	aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
   1532 	aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
   1533 	memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
   1534 	    sizeof(struct in6_addr));
   1535 	break;
   1536 }
   1537 #endif
   1538 #ifdef INET
   1539 case AF_INET:
   1540 {
   1541 	/* IPv4 address */
   1542 	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
   1543 
   1544 	aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
   1545 	aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
   1546 	aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
   1547 	memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
   1548 	    sizeof(struct in_addr));
   1549 	break;
   1550 }
   1551 #endif
   1552 default:
   1553 	/* invalid family! */
   1554 	SCTP_FREE(aa, SCTP_M_ASC_ADDR);
   1555 	if (ifa)
   1556 		sctp_free_ifa(ifa);
   1557 	return (-1);
   1558 }
   1559 aa->sent = 0;		/* clear sent flag */
   1560 
   1561 /* delete goes to the back of the queue */
   1562 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
   1563 
   1564 /* sa_ignore MEMLEAK {memory is put on the tailq} */
   1565 return (0);
   1566 }
   1567 
   1568 /*
   1569 * find a specific asconf param on our "sent" queue
   1570 */
   1571 static struct sctp_asconf_addr *
   1572 sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
   1573 {
   1574 struct sctp_asconf_addr *aa;
   1575 
   1576 TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
   1577 	if (aa->ap.aph.correlation_id == correlation_id &&
   1578 	    aa->sent == 1) {
   1579 		/* found it */
   1580 		return (aa);
   1581 	}
   1582 }
   1583 /* didn't find it */
   1584 return (NULL);
   1585 }
   1586 
   1587 /*
   1588 * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
   1589 * notifications based on the error response
   1590 */
   1591 static void
   1592 sctp_asconf_process_error(struct sctp_tcb *stcb SCTP_UNUSED,
   1593 		  struct sctp_asconf_paramhdr *aph)
   1594 {
   1595 struct sctp_error_cause *eh;
   1596 struct sctp_paramhdr *ph;
   1597 uint16_t param_type;
   1598 uint16_t error_code;
   1599 
   1600 eh = (struct sctp_error_cause *)(aph + 1);
   1601 ph = (struct sctp_paramhdr *)(eh + 1);
   1602 /* validate lengths */
   1603 if (htons(eh->length) + sizeof(struct sctp_error_cause) >
   1604     htons(aph->ph.param_length)) {
   1605 	/* invalid error cause length */
   1606 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1607 		"asconf_process_error: cause element too long\n");
   1608 	return;
   1609 }
   1610 if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
   1611     htons(eh->length)) {
   1612 	/* invalid included TLV length */
   1613 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1614 		"asconf_process_error: included TLV too long\n");
   1615 	return;
   1616 }
   1617 /* which error code ? */
   1618 error_code = ntohs(eh->code);
   1619 param_type = ntohs(aph->ph.param_type);
   1620 /* FIX: this should go back up the REMOTE_ERROR ULP notify */
   1621 switch (error_code) {
   1622 case SCTP_CAUSE_RESOURCE_SHORTAGE:
   1623 	/* we allow ourselves to "try again" for this error */
   1624 	break;
   1625 default:
   1626 	/* peer can't handle it... */
   1627 	switch (param_type) {
   1628 	case SCTP_ADD_IP_ADDRESS:
   1629 	case SCTP_DEL_IP_ADDRESS:
   1630 	case SCTP_SET_PRIM_ADDR:
   1631 		break;
   1632 	default:
   1633 		break;
   1634 	}
   1635 }
   1636 }
   1637 
   1638 /*
   1639 * process an asconf queue param.
   1640 * aparam: parameter to process, will be removed from the queue.
   1641 * flag: 1=success case, 0=failure case
   1642 */
   1643 static void
   1644 sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
   1645 		      struct sctp_asconf_addr *aparam, uint32_t flag)
   1646 {
   1647 uint16_t param_type;
   1648 
   1649 /* process this param */
   1650 param_type = aparam->ap.aph.ph.param_type;
   1651 switch (param_type) {
   1652 case SCTP_ADD_IP_ADDRESS:
   1653 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1654 		"process_param_ack: added IP address\n");
   1655 	sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, flag);
   1656 	break;
   1657 case SCTP_DEL_IP_ADDRESS:
   1658 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1659 		"process_param_ack: deleted IP address\n");
   1660 	/* nothing really to do... lists already updated */
   1661 	break;
   1662 case SCTP_SET_PRIM_ADDR:
   1663 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1664 		"process_param_ack: set primary IP address\n");
   1665 	/* nothing to do... peer may start using this addr */
   1666 	break;
   1667 default:
   1668 	/* should NEVER happen */
   1669 	break;
   1670 }
   1671 
   1672 /* remove the param and free it */
   1673 TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
   1674 if (aparam->ifa)
   1675 	sctp_free_ifa(aparam->ifa);
   1676 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
   1677 }
   1678 
   1679 /*
   1680 * cleanup from a bad asconf ack parameter
   1681 */
   1682 static void
   1683 sctp_asconf_ack_clear(struct sctp_tcb *stcb SCTP_UNUSED)
   1684 {
   1685 /* assume peer doesn't really know how to do asconfs */
   1686 /* XXX we could free the pending queue here */
   1687 
   1688 }
   1689 
   1690 void
   1691 sctp_handle_asconf_ack(struct mbuf *m, int offset,
   1692 	       struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
   1693 	       struct sctp_nets *net, int *abort_no_unlock)
   1694 {
   1695 struct sctp_association *asoc;
   1696 uint32_t serial_num;
   1697 uint16_t ack_length;
   1698 struct sctp_asconf_paramhdr *aph;
   1699 struct sctp_asconf_addr *aa, *aa_next;
   1700 uint32_t last_error_id = 0;	/* last error correlation id */
   1701 uint32_t id;
   1702 struct sctp_asconf_addr *ap;
   1703 
   1704 /* asconf param buffer */
   1705 uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
   1706 
   1707 /* verify minimum length */
   1708 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
   1709 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   1710 		"handle_asconf_ack: chunk too small = %xh\n",
   1711 		ntohs(cp->ch.chunk_length));
   1712 	return;
   1713 }
   1714 asoc = &stcb->asoc;
   1715 serial_num = ntohl(cp->serial_number);
   1716 
   1717 /*
   1718  * NOTE: we may want to handle this differently- currently, we will
   1719  * abort when we get an ack for the expected serial number + 1 (eg.
   1720  * we didn't send it), process an ack normally if it is the expected
   1721  * serial number, and re-send the previous ack for *ALL* other
   1722  * serial numbers
   1723  */
   1724 
   1725 /*
   1726  * if the serial number is the next expected, but I didn't send it,
   1727  * abort the asoc, since someone probably just hijacked us...
   1728  */
   1729 if (serial_num == (asoc->asconf_seq_out + 1)) {
   1730 	struct mbuf *op_err;
   1731 	char msg[SCTP_DIAG_INFO_LEN];
   1732 
   1733 	SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
   1734 	SCTP_SNPRINTF(msg, sizeof(msg), "Never sent serial number %8.8x", serial_num);
   1735 	op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
   1736 	sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
   1737 	*abort_no_unlock = 1;
   1738 	return;
   1739 }
   1740 if (serial_num != asoc->asconf_seq_out_acked + 1) {
   1741 	/* got a duplicate/unexpected ASCONF-ACK */
   1742 	SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
   1743 		serial_num, asoc->asconf_seq_out_acked + 1);
   1744 	return;
   1745 }
   1746 
   1747 if (serial_num == asoc->asconf_seq_out - 1) {
   1748 	/* stop our timer */
   1749 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
   1750 			SCTP_FROM_SCTP_ASCONF + SCTP_LOC_5);
   1751 }
   1752 
   1753 /* process the ASCONF-ACK contents */
   1754 ack_length = ntohs(cp->ch.chunk_length) -
   1755     sizeof(struct sctp_asconf_ack_chunk);
   1756 offset += sizeof(struct sctp_asconf_ack_chunk);
   1757 /* process through all parameters */
   1758 while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
   1759 	unsigned int param_length, param_type;
   1760 
   1761 	/* get pointer to next asconf parameter */
   1762 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
   1763 	    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
   1764 	if (aph == NULL) {
   1765 		/* can't get an asconf paramhdr */
   1766 		sctp_asconf_ack_clear(stcb);
   1767 		return;
   1768 	}
   1769 	param_type = ntohs(aph->ph.param_type);
   1770 	param_length = ntohs(aph->ph.param_length);
   1771 	if (param_length > ack_length) {
   1772 		sctp_asconf_ack_clear(stcb);
   1773 		return;
   1774 	}
   1775 	if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
   1776 		sctp_asconf_ack_clear(stcb);
   1777 		return;
   1778 	}
   1779 	/* get the complete parameter... */
   1780 	if (param_length > sizeof(aparam_buf)) {
   1781 		SCTPDBG(SCTP_DEBUG_ASCONF1,
   1782 			"param length (%u) larger than buffer size!\n", param_length);
   1783 		sctp_asconf_ack_clear(stcb);
   1784 		return;
   1785 	}
   1786 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
   1787 	if (aph == NULL) {
   1788 		sctp_asconf_ack_clear(stcb);
   1789 		return;
   1790 	}
   1791 	/* correlation_id is transparent to peer, no ntohl needed */
   1792 	id = aph->correlation_id;
   1793 
   1794 	switch (param_type) {
   1795 	case SCTP_ERROR_CAUSE_IND:
   1796 		last_error_id = id;
   1797 		/* find the corresponding asconf param in our queue */
   1798 		ap = sctp_asconf_find_param(stcb, id);
   1799 		if (ap == NULL) {
   1800 			/* hmm... can't find this in our queue! */
   1801 			break;
   1802 		}
   1803 		/* process the parameter, failed flag */
   1804 		sctp_asconf_process_param_ack(stcb, ap, 0);
   1805 		/* process the error response */
   1806 		sctp_asconf_process_error(stcb, aph);
   1807 		break;
   1808 	case SCTP_SUCCESS_REPORT:
   1809 		/* find the corresponding asconf param in our queue */
   1810 		ap = sctp_asconf_find_param(stcb, id);
   1811 		if (ap == NULL) {
   1812 			/* hmm... can't find this in our queue! */
   1813 			break;
   1814 		}
   1815 		/* process the parameter, success flag */
   1816 		sctp_asconf_process_param_ack(stcb, ap, 1);
   1817 		break;
   1818 	default:
   1819 		break;
   1820 	}		/* switch */
   1821 
   1822 	/* update remaining ASCONF-ACK message length to process */
   1823 	if (ack_length > SCTP_SIZE32(param_length)) {
   1824 		ack_length -= SCTP_SIZE32(param_length);
   1825 	} else {
   1826 		break;
   1827 	}
   1828 	offset += SCTP_SIZE32(param_length);
   1829 } /* while */
   1830 
   1831 /*
   1832  * if there are any "sent" params still on the queue, these are
   1833  * implicitly "success", or "failed" (if we got an error back) ...
   1834  * so process these appropriately
   1835  *
   1836  * we assume that the correlation_id's are monotonically increasing
   1837  * beginning from 1 and that we don't have *that* many outstanding
   1838  * at any given time
   1839  */
   1840 if (last_error_id == 0)
   1841 	last_error_id--;	/* set to "max" value */
   1842 TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
   1843 	if (aa->sent == 1) {
   1844 		/*
   1845 		 * implicitly successful or failed if correlation_id
   1846 		 * < last_error_id, then success else, failure
   1847 		 */
   1848 		if (aa->ap.aph.correlation_id < last_error_id)
   1849 			sctp_asconf_process_param_ack(stcb, aa, 1);
   1850 		else
   1851 			sctp_asconf_process_param_ack(stcb, aa, 0);
   1852 	} else {
   1853 		/*
   1854 		 * since we always process in order (FIFO queue) if
   1855 		 * we reach one that hasn't been sent, the rest
   1856 		 * should not have been sent either. so, we're
   1857 		 * done...
   1858 		 */
   1859 		break;
   1860 	}
   1861 }
   1862 
   1863 /* update the next sequence number to use */
   1864 asoc->asconf_seq_out_acked++;
   1865 /* remove the old ASCONF on our outbound queue */
   1866 sctp_toss_old_asconf(stcb);
   1867 if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
   1868 #ifdef SCTP_TIMER_BASED_ASCONF
   1869 	/* we have more params, so restart our timer */
   1870 	sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
   1871 			 stcb, net);
   1872 #else
   1873 	/* we have more params, so send out more */
   1874 	sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
   1875 #endif
   1876 }
   1877 }
   1878 
   1879 #ifdef INET6
   1880 static uint32_t
   1881 sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
   1882 {
   1883 struct sockaddr_in6 *sin6, *net6;
   1884 struct sctp_nets *net;
   1885 
   1886 if (sa->sa_family != AF_INET6) {
   1887 	/* wrong family */
   1888 	return (0);
   1889 }
   1890 sin6 = (struct sockaddr_in6 *)sa;
   1891 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
   1892 	/* not link local address */
   1893 	return (0);
   1894 }
   1895 /* hunt through our destination nets list for this scope_id */
   1896 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1897 	if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
   1898 	    AF_INET6)
   1899 		continue;
   1900 	net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
   1901 	if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
   1902 		continue;
   1903 	if (sctp_is_same_scope(sin6, net6)) {
   1904 		/* found one */
   1905 		return (1);
   1906 	}
   1907 }
   1908 /* didn't find one */
   1909 return (0);
   1910 }
   1911 #endif
   1912 
   1913 /*
   1914 * address management functions
   1915 */
   1916 static void
   1917 sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1918 	     struct sctp_ifa *ifa, uint16_t type, int addr_locked)
   1919 {
   1920 int status;
   1921 
   1922 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 ||
   1923     sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
   1924 	/* subset bound, no ASCONF allowed case, so ignore */
   1925 	return;
   1926 }
   1927 /*
   1928  * note: we know this is not the subset bound, no ASCONF case eg.
   1929  * this is boundall or subset bound w/ASCONF allowed
   1930  */
   1931 
   1932 /* first, make sure that the address is IPv4 or IPv6 and not jailed */
   1933 switch (ifa->address.sa.sa_family) {
   1934 #ifdef INET6
   1935 case AF_INET6:
   1936 #if defined(__FreeBSD__) && !defined(__Userspace__)
   1937 	if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
   1938 	                     &ifa->address.sin6.sin6_addr) != 0) {
   1939 		return;
   1940 	}
   1941 #endif
   1942 	break;
   1943 #endif
   1944 #ifdef INET
   1945 case AF_INET:
   1946 #if defined(__FreeBSD__) && !defined(__Userspace__)
   1947 	if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
   1948 	                     &ifa->address.sin.sin_addr) != 0) {
   1949 		return;
   1950 	}
   1951 #endif
   1952 	break;
   1953 #endif
   1954 default:
   1955 	return;
   1956 }
   1957 #ifdef INET6
   1958 /* make sure we're "allowed" to add this type of addr */
   1959 if (ifa->address.sa.sa_family == AF_INET6) {
   1960 	/* invalid if we're not a v6 endpoint */
   1961 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
   1962 		return;
   1963 	/* is the v6 addr really valid ? */
   1964 	if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
   1965 		return;
   1966 	}
   1967 }
   1968 #endif
   1969 /* put this address on the "pending/do not use yet" list */
   1970 sctp_add_local_addr_restricted(stcb, ifa);
   1971 /*
   1972  * check address scope if address is out of scope, don't queue
   1973  * anything... note: this would leave the address on both inp and
   1974  * asoc lists
   1975  */
   1976 switch (ifa->address.sa.sa_family) {
   1977 #ifdef INET6
   1978 case AF_INET6:
   1979 {
   1980 	struct sockaddr_in6 *sin6;
   1981 
   1982 	sin6 = &ifa->address.sin6;
   1983 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1984 		/* we skip unspecified addresses */
   1985 		return;
   1986 	}
   1987 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   1988 		if (stcb->asoc.scope.local_scope == 0) {
   1989 			return;
   1990 		}
   1991 		/* is it the right link local scope? */
   1992 		if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
   1993 			return;
   1994 		}
   1995 	}
   1996 	if (stcb->asoc.scope.site_scope == 0 &&
   1997 	    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
   1998 		return;
   1999 	}
   2000 	break;
   2001 }
   2002 #endif
   2003 #ifdef INET
   2004 case AF_INET:
   2005 {
   2006 	struct sockaddr_in *sin;
   2007 
   2008 	/* invalid if we are a v6 only endpoint */
   2009 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   2010 	    SCTP_IPV6_V6ONLY(inp))
   2011 		return;
   2012 
   2013 	sin = &ifa->address.sin;
   2014 	if (sin->sin_addr.s_addr == 0) {
   2015 		/* we skip unspecified addresses */
   2016 		return;
   2017 	}
   2018 	if (stcb->asoc.scope.ipv4_local_scope == 0 &&
   2019 	    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
   2020 		return;
   2021 	}
   2022 	break;
   2023 }
   2024 #endif
   2025 default:
   2026 	/* else, not AF_INET or AF_INET6, so skip */
   2027 	return;
   2028 }
   2029 
   2030 /* queue an asconf for this address add/delete */
   2031 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
   2032 	/* does the peer do asconf? */
   2033 	if (stcb->asoc.asconf_supported) {
   2034 		/* queue an asconf for this addr */
   2035 		status = sctp_asconf_queue_add(stcb, ifa, type);
   2036 
   2037 		/*
   2038 		 * if queued ok, and in the open state, send out the
   2039 		 * ASCONF.  If in the non-open state, these will be
   2040 		 * sent when the state goes open.
   2041 		 */
   2042 		if (status == 0 &&
   2043 		    ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
   2044 		     (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED))) {
   2045 #ifdef SCTP_TIMER_BASED_ASCONF
   2046 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
   2047 			    stcb, stcb->asoc.primary_destination);
   2048 #else
   2049 			sctp_send_asconf(stcb, NULL, addr_locked);
   2050 #endif
   2051 		}
   2052 	}
   2053 }
   2054 }
   2055 
   2056 int
   2057 sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
   2058 {
   2059 struct sctp_asconf_iterator *asc;
   2060 struct sctp_ifa *ifa;
   2061 struct sctp_laddr *l;
   2062 int cnt_invalid = 0;
   2063 
   2064 asc = (struct sctp_asconf_iterator *)ptr;
   2065 LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
   2066 	ifa = l->ifa;
   2067 	switch (ifa->address.sa.sa_family) {
   2068 #ifdef INET6
   2069 	case AF_INET6:
   2070 		/* invalid if we're not a v6 endpoint */
   2071 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
   2072 			cnt_invalid++;
   2073 			if (asc->cnt == cnt_invalid)
   2074 				return (1);
   2075 		}
   2076 		break;
   2077 #endif
   2078 #ifdef INET
   2079 	case AF_INET:
   2080 	{
   2081 		/* invalid if we are a v6 only endpoint */
   2082 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   2083 		    SCTP_IPV6_V6ONLY(inp)) {
   2084 			cnt_invalid++;
   2085 			if (asc->cnt == cnt_invalid)
   2086 				return (1);
   2087 		}
   2088 		break;
   2089 	}
   2090 #endif
   2091 	default:
   2092 		/* invalid address family */
   2093 		cnt_invalid++;
   2094 		if (asc->cnt == cnt_invalid)
   2095 			return (1);
   2096 	}
   2097 }
   2098 return (0);
   2099 }
   2100 
   2101 static int
   2102 sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
   2103 {
   2104 struct sctp_ifa *ifa;
   2105 struct sctp_asconf_iterator *asc;
   2106 struct sctp_laddr *laddr, *nladdr, *l;
   2107 
   2108 /* Only for specific case not bound all */
   2109 asc = (struct sctp_asconf_iterator *)ptr;
   2110 LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
   2111 	ifa = l->ifa;
   2112 	if (l->action == SCTP_ADD_IP_ADDRESS) {
   2113 		LIST_FOREACH(laddr, &inp->sctp_addr_list,
   2114 			     sctp_nxt_addr) {
   2115 			if (laddr->ifa == ifa) {
   2116 				laddr->action = 0;
   2117 				break;
   2118 			}
   2119 		}
   2120 	} else if (l->action == SCTP_DEL_IP_ADDRESS) {
   2121 		LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
   2122 			/* remove only after all guys are done */
   2123 			if (laddr->ifa == ifa) {
   2124 				sctp_del_local_addr_ep(inp, ifa);
   2125 			}
   2126 		}
   2127 	}
   2128 }
   2129 return (0);
   2130 }
   2131 
   2132 void
   2133 sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   2134 		  void *ptr, uint32_t val SCTP_UNUSED)
   2135 {
   2136 struct sctp_asconf_iterator *asc;
   2137 struct sctp_ifa *ifa;
   2138 struct sctp_laddr *l;
   2139 int cnt_invalid = 0;
   2140 int type, status;
   2141 int num_queued = 0;
   2142 
   2143 asc = (struct sctp_asconf_iterator *)ptr;
   2144 LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
   2145 	ifa = l->ifa;
   2146 	type = l->action;
   2147 
   2148 	/* address's vrf_id must be the vrf_id of the assoc */
   2149 	if (ifa->vrf_id != stcb->asoc.vrf_id) {
   2150 		continue;
   2151 	}
   2152 
   2153 	/* Same checks again for assoc */
   2154 	switch (ifa->address.sa.sa_family) {
   2155 #ifdef INET6
   2156 	case AF_INET6:
   2157 	{
   2158 		/* invalid if we're not a v6 endpoint */
   2159 		struct sockaddr_in6 *sin6;
   2160 
   2161 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
   2162 			cnt_invalid++;
   2163 			if (asc->cnt == cnt_invalid)
   2164 				return;
   2165 			else
   2166 				continue;
   2167 		}
   2168 		sin6 = &ifa->address.sin6;
   2169 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   2170 			/* we skip unspecified addresses */
   2171 			continue;
   2172 		}
   2173 #if defined(__FreeBSD__) && !defined(__Userspace__)
   2174 		if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
   2175 		                     &sin6->sin6_addr) != 0) {
   2176 			continue;
   2177 		}
   2178 #endif
   2179 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   2180 			if (stcb->asoc.scope.local_scope == 0) {
   2181 				continue;
   2182 			}
   2183 			/* is it the right link local scope? */
   2184 			if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
   2185 				continue;
   2186 			}
   2187 		}
   2188 		break;
   2189 	}
   2190 #endif
   2191 #ifdef INET
   2192 	case AF_INET:
   2193 	{
   2194 		/* invalid if we are a v6 only endpoint */
   2195 		struct sockaddr_in *sin;
   2196 
   2197 		/* invalid if we are a v6 only endpoint */
   2198 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   2199 		    SCTP_IPV6_V6ONLY(inp))
   2200 			continue;
   2201 
   2202 		sin = &ifa->address.sin;
   2203 		if (sin->sin_addr.s_addr == 0) {
   2204 			/* we skip unspecified addresses */
   2205 			continue;
   2206 		}
   2207 #if defined(__FreeBSD__) && !defined(__Userspace__)
   2208 		if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
   2209 		                     &sin->sin_addr) != 0) {
   2210 			continue;
   2211 		}
   2212 #endif
   2213 		if (stcb->asoc.scope.ipv4_local_scope == 0 &&
   2214 		    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
   2215 			continue;
   2216 		}
   2217 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   2218 		    SCTP_IPV6_V6ONLY(inp)) {
   2219 			cnt_invalid++;
   2220 			if (asc->cnt == cnt_invalid)
   2221 				return;
   2222 			else
   2223 				continue;
   2224 		}
   2225 		break;
   2226 	}
   2227 #endif
   2228 	default:
   2229 		/* invalid address family */
   2230 		cnt_invalid++;
   2231 		if (asc->cnt == cnt_invalid)
   2232 			return;
   2233 		else
   2234 			continue;
   2235 	}
   2236 
   2237 	if (type == SCTP_ADD_IP_ADDRESS) {
   2238 		/* prevent this address from being used as a source */
   2239 		sctp_add_local_addr_restricted(stcb, ifa);
   2240 	} else if (type == SCTP_DEL_IP_ADDRESS) {
   2241 		struct sctp_nets *net;
   2242 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   2243 			/* delete this address if cached */
   2244 			if (net->ro._s_addr == ifa) {
   2245 				sctp_free_ifa(net->ro._s_addr);
   2246 				net->ro._s_addr = NULL;
   2247 				net->src_addr_selected = 0;
   2248 #if defined(__FreeBSD__) && !defined(__Userspace__)
   2249 				RO_NHFREE(&net->ro);
   2250 #else
   2251 				if (net->ro.ro_rt) {
   2252 					RTFREE(net->ro.ro_rt);
   2253 					net->ro.ro_rt = NULL;
   2254 				}
   2255 #endif
   2256 				/*
   2257 				 * Now we deleted our src address,
   2258 				 * should we not also now reset the
   2259 				 * cwnd/rto to start as if its a new
   2260 				 * address?
   2261 				 */
   2262 				stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
   2263 				net->RTO = 0;
   2264 			}
   2265 		}
   2266 	} else if (type == SCTP_SET_PRIM_ADDR) {
   2267 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
   2268 			/* must validate the ifa is in the ep */
   2269 			if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
   2270 				continue;
   2271 			}
   2272 		} else {
   2273 			/* Need to check scopes for this guy */
   2274 			if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
   2275 				continue;
   2276 			}
   2277 		}
   2278 	}
   2279 	/* queue an asconf for this address add/delete */
   2280 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
   2281 	    stcb->asoc.asconf_supported == 1) {
   2282 		/* queue an asconf for this addr */
   2283 		status = sctp_asconf_queue_add(stcb, ifa, type);
   2284 		/*
   2285 		 * if queued ok, and in the open state, update the
   2286 		 * count of queued params.  If in the non-open state,
   2287 		 * these get sent when the assoc goes open.
   2288 		 */
   2289 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
   2290 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
   2291 			if (status >= 0) {
   2292 				num_queued++;
   2293 			}
   2294 		}
   2295 	}
   2296 }
   2297 /*
   2298  * If we have queued params in the open state, send out an ASCONF.
   2299  */
   2300 if (num_queued > 0) {
   2301 	sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
   2302 }
   2303 }
   2304 
   2305 void
   2306 sctp_asconf_iterator_end(void *ptr, uint32_t val SCTP_UNUSED)
   2307 {
   2308 struct sctp_asconf_iterator *asc;
   2309 struct sctp_ifa *ifa;
   2310 struct sctp_laddr *l, *nl;
   2311 
   2312 asc = (struct sctp_asconf_iterator *)ptr;
   2313 LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
   2314 	ifa = l->ifa;
   2315 	if (l->action == SCTP_ADD_IP_ADDRESS) {
   2316 		/* Clear the defer use flag */
   2317 		ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
   2318 	}
   2319 	sctp_free_ifa(ifa);
   2320 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
   2321 	SCTP_DECR_LADDR_COUNT();
   2322 }
   2323 SCTP_FREE(asc, SCTP_M_ASC_IT);
   2324 }
   2325 
   2326 /*
   2327 * sa is the sockaddr to ask the peer to set primary to.
   2328 * returns: 0 = completed, -1 = error
   2329 */
   2330 int32_t
   2331 sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
   2332 {
   2333 uint32_t vrf_id;
   2334 struct sctp_ifa *ifa;
   2335 
   2336 /* find the ifa for the desired set primary */
   2337 vrf_id = stcb->asoc.vrf_id;
   2338 ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
   2339 if (ifa == NULL) {
   2340 	/* Invalid address */
   2341 	return (-1);
   2342 }
   2343 
   2344 /* queue an ASCONF:SET_PRIM_ADDR to be sent */
   2345 if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
   2346 	/* set primary queuing succeeded */
   2347 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   2348 		"set_primary_ip_address_sa: queued on tcb=%p, ",
   2349 		(void *)stcb);
   2350 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
   2351 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
   2352 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
   2353 #ifdef SCTP_TIMER_BASED_ASCONF
   2354 		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
   2355 				 stcb->sctp_ep, stcb,
   2356 				 stcb->asoc.primary_destination);
   2357 #else
   2358 		sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
   2359 #endif
   2360 	}
   2361 } else {
   2362 	SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
   2363 		(void *)stcb);
   2364 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
   2365 	return (-1);
   2366 }
   2367 return (0);
   2368 }
   2369 
   2370 int
   2371 sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
   2372 {
   2373 struct sctp_tmit_chunk *chk, *nchk;
   2374 unsigned int offset, asconf_limit;
   2375 struct sctp_asconf_chunk *acp;
   2376 struct sctp_asconf_paramhdr *aph;
   2377 uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
   2378 struct sctp_paramhdr *ph;
   2379 int add_cnt, del_cnt;
   2380 uint16_t last_param_type;
   2381 
   2382 add_cnt = del_cnt = 0;
   2383 last_param_type = 0;
   2384 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
   2385 	if (chk->data == NULL) {
   2386 		SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
   2387 		continue;
   2388 	}
   2389 	offset = 0;
   2390 	acp = mtod(chk->data, struct sctp_asconf_chunk *);
   2391 	offset += sizeof(struct sctp_asconf_chunk);
   2392 	asconf_limit = ntohs(acp->ch.chunk_length);
   2393 	ph = (struct sctp_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
   2394 	if (ph == NULL) {
   2395 		SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
   2396 		continue;
   2397 	}
   2398 	offset += ntohs(ph->param_length);
   2399 
   2400 	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
   2401 	if (aph == NULL) {
   2402 		SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
   2403 		continue;
   2404 	}
   2405 	while (aph != NULL) {
   2406 		unsigned int param_length, param_type;
   2407 
   2408 		param_type = ntohs(aph->ph.param_type);
   2409 		param_length = ntohs(aph->ph.param_length);
   2410 		if (offset + param_length > asconf_limit) {
   2411 			/* parameter goes beyond end of chunk! */
   2412 			break;
   2413 		}
   2414 		if (param_length > sizeof(aparam_buf)) {
   2415 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
   2416 			break;
   2417 		}
   2418 		if (param_length <= sizeof(struct sctp_paramhdr)) {
   2419 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
   2420 			break;
   2421 		}
   2422 
   2423 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
   2424 		if (aph == NULL) {
   2425 			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
   2426 			break;
   2427 		}
   2428 
   2429 		ph = (struct sctp_paramhdr *)(aph + 1);
   2430 		if (sctp_addr_match(ph, &sctp_ifa->address.sa) != 0) {
   2431 			switch (param_type) {
   2432 			case SCTP_ADD_IP_ADDRESS:
   2433 				add_cnt++;
   2434 				break;
   2435 			case SCTP_DEL_IP_ADDRESS:
   2436 				del_cnt++;
   2437 				break;
   2438 			default:
   2439 				break;
   2440 			}
   2441 			last_param_type = param_type;
   2442 		}
   2443 
   2444 		offset += SCTP_SIZE32(param_length);
   2445 		if (offset >= asconf_limit) {
   2446 			/* no more data in the mbuf chain */
   2447 			break;
   2448 		}
   2449 		/* get pointer to next asconf param */
   2450 		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
   2451 	}
   2452 }
   2453 
   2454 /* we want to find the sequences which consist of ADD -> DEL -> ADD or DEL -> ADD */
   2455 if (add_cnt > del_cnt ||
   2456     (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
   2457 	return (1);
   2458 }
   2459 return (0);
   2460 }
   2461 
   2462 static struct sockaddr *
   2463 sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
   2464 {
   2465 struct sctp_vrf *vrf = NULL;
   2466 struct sctp_ifn *sctp_ifn;
   2467 struct sctp_ifa *sctp_ifa;
   2468 
   2469 if (addr_locked == SCTP_ADDR_NOT_LOCKED)
   2470 	SCTP_IPI_ADDR_RLOCK();
   2471 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
   2472 if (vrf == NULL) {
   2473 	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
   2474 		SCTP_IPI_ADDR_RUNLOCK();
   2475 	return (NULL);
   2476 }
   2477 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
   2478 	if (stcb->asoc.scope.loopback_scope == 0 &&
   2479 	    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
   2480 		/* Skip if loopback_scope not set */
   2481 		continue;
   2482 	}
   2483 	LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
   2484 		switch (sctp_ifa->address.sa.sa_family) {
   2485 #ifdef INET
   2486 		case AF_INET:
   2487 			if (stcb->asoc.scope.ipv4_addr_legal) {
   2488 				struct sockaddr_in *sin;
   2489 
   2490 				sin = &sctp_ifa->address.sin;
   2491 				if (sin->sin_addr.s_addr == 0) {
   2492 					/* skip unspecified addresses */
   2493 					continue;
   2494 				}
   2495 #if defined(__FreeBSD__) && !defined(__Userspace__)
   2496 				if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
   2497 				                     &sin->sin_addr) != 0) {
   2498 					continue;
   2499 				}
   2500 #endif
   2501 				if (stcb->asoc.scope.ipv4_local_scope == 0 &&
   2502 				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
   2503 					continue;
   2504 
   2505 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
   2506 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
   2507 					continue;
   2508 				/* found a valid local v4 address to use */
   2509 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
   2510 					SCTP_IPI_ADDR_RUNLOCK();
   2511 				return (&sctp_ifa->address.sa);
   2512 			}
   2513 			break;
   2514 #endif
   2515 #ifdef INET6
   2516 		case AF_INET6:
   2517 			if (stcb->asoc.scope.ipv6_addr_legal) {
   2518 				struct sockaddr_in6 *sin6;
   2519 
   2520 				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
   2521 					continue;
   2522 				}
   2523 
   2524 				sin6 = &sctp_ifa->address.sin6;
   2525 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   2526 					/* we skip unspecified addresses */
   2527 					continue;
   2528 				}
   2529 #if defined(__FreeBSD__) && !defined(__Userspace__)
   2530 				if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
   2531 				                     &sin6->sin6_addr) != 0) {
   2532 					continue;
   2533 				}
   2534 #endif
   2535 				if (stcb->asoc.scope.local_scope == 0 &&
   2536 				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
   2537 					continue;
   2538 				if (stcb->asoc.scope.site_scope == 0 &&
   2539 				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
   2540 					continue;
   2541 
   2542 				if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
   2543 				    (!sctp_is_addr_pending(stcb, sctp_ifa)))
   2544 					continue;
   2545 				/* found a valid local v6 address to use */
   2546 				if (addr_locked == SCTP_ADDR_NOT_LOCKED)
   2547 					SCTP_IPI_ADDR_RUNLOCK();
   2548 				return (&sctp_ifa->address.sa);
   2549 			}
   2550 			break;
   2551 #endif
   2552 		default:
   2553 			break;
   2554 		}
   2555 	}
   2556 }
   2557 /* no valid addresses found */
   2558 if (addr_locked == SCTP_ADDR_NOT_LOCKED)
   2559 	SCTP_IPI_ADDR_RUNLOCK();
   2560 return (NULL);
   2561 }
   2562 
   2563 static struct sockaddr *
   2564 sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
   2565 {
   2566 struct sctp_laddr *laddr;
   2567 
   2568 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
   2569 	if (laddr->ifa == NULL) {
   2570 		continue;
   2571 	}
   2572 	/* is the address restricted ? */
   2573 	if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
   2574 	    (!sctp_is_addr_pending(stcb, laddr->ifa)))
   2575 		continue;
   2576 
   2577 	/* found a valid local address to use */
   2578 	return (&laddr->ifa->address.sa);
   2579 }
   2580 /* no valid addresses found */
   2581 return (NULL);
   2582 }
   2583 
   2584 /*
   2585 * builds an ASCONF chunk from queued ASCONF params.
   2586 * returns NULL on error (no mbuf, no ASCONF params queued, etc).
   2587 */
   2588 struct mbuf *
   2589 sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
   2590 {
   2591 struct mbuf *m_asconf, *m_asconf_chk;
   2592 struct sctp_asconf_addr *aa;
   2593 struct sctp_asconf_chunk *acp;
   2594 struct sctp_asconf_paramhdr *aph;
   2595 struct sctp_asconf_addr_param *aap;
   2596 uint32_t p_length, overhead;
   2597 uint32_t correlation_id = 1;	/* 0 is reserved... */
   2598 caddr_t ptr, lookup_ptr;
   2599 uint8_t lookup_used = 0;
   2600 
   2601 /* are there any asconf params to send? */
   2602 TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
   2603 	if (aa->sent == 0)
   2604 		break;
   2605 }
   2606 if (aa == NULL)
   2607 	return (NULL);
   2608 
   2609 /* Consider IP header and SCTP common header. */
   2610 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   2611 	overhead = SCTP_MIN_OVERHEAD;
   2612 } else {
   2613 	overhead = SCTP_MIN_V4_OVERHEAD;
   2614 }
   2615 /* Consider ASONF chunk. */
   2616 overhead += sizeof(struct sctp_asconf_chunk);
   2617 /* Consider AUTH chunk. */
   2618 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
   2619 if (stcb->asoc.smallest_mtu <= overhead) {
   2620 	/* MTU too small. */
   2621 	return (NULL);
   2622 }
   2623 /*
   2624  * get a chunk header mbuf and a cluster for the asconf params since
   2625  * it's simpler to fill in the asconf chunk header lookup address on
   2626  * the fly
   2627  */
   2628 m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_NOWAIT, 1, MT_DATA);
   2629 if (m_asconf_chk == NULL) {
   2630 	/* no mbuf's */
   2631 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   2632 		"sctp_compose_asconf: couldn't get chunk mbuf!\n");
   2633 	return (NULL);
   2634 }
   2635 m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
   2636 if (m_asconf == NULL) {
   2637 	/* no mbuf's */
   2638 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   2639 		"sctp_compose_asconf: couldn't get mbuf!\n");
   2640 	sctp_m_freem(m_asconf_chk);
   2641 	return (NULL);
   2642 }
   2643 SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
   2644 SCTP_BUF_LEN(m_asconf) = 0;
   2645 acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
   2646 memset(acp, 0, sizeof(struct sctp_asconf_chunk));
   2647 /* save pointers to lookup address and asconf params */
   2648 lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
   2649 ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
   2650 
   2651 /* fill in chunk header info */
   2652 acp->ch.chunk_type = SCTP_ASCONF;
   2653 acp->ch.chunk_flags = 0;
   2654 acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
   2655 stcb->asoc.asconf_seq_out++;
   2656 
   2657 /* add parameters... up to smallest MTU allowed */
   2658 TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
   2659 	if (aa->sent)
   2660 		continue;
   2661 	/* get the parameter length */
   2662 	p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
   2663 	/* will it fit in current chunk? */
   2664 	if ((SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu - overhead) ||
   2665 	    (SCTP_BUF_LEN(m_asconf) + p_length > MCLBYTES)) {
   2666 		/* won't fit, so we're done with this chunk */
   2667 		break;
   2668 	}
   2669 	/* assign (and store) a correlation id */
   2670 	aa->ap.aph.correlation_id = correlation_id++;
   2671 
   2672 	/*
   2673 	 * fill in address if we're doing a delete this is a simple
   2674 	 * way for us to fill in the correlation address, which
   2675 	 * should only be used by the peer if we're deleting our
   2676 	 * source address and adding a new address (e.g. renumbering
   2677 	 * case)
   2678 	 */
   2679 	if (lookup_used == 0 &&
   2680 	    (aa->special_del == 0) &&
   2681 	    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
   2682 		struct sctp_ipv6addr_param *lookup;
   2683 		uint16_t p_size, addr_size;
   2684 
   2685 		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
   2686 		lookup->ph.param_type =
   2687 		    htons(aa->ap.addrp.ph.param_type);
   2688 		if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
   2689 			/* copy IPv6 address */
   2690 			p_size = sizeof(struct sctp_ipv6addr_param);
   2691 			addr_size = sizeof(struct in6_addr);
   2692 		} else {
   2693 			/* copy IPv4 address */
   2694 			p_size = sizeof(struct sctp_ipv4addr_param);
   2695 			addr_size = sizeof(struct in_addr);
   2696 		}
   2697 		lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
   2698 		memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
   2699 		SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
   2700 		lookup_used = 1;
   2701 	}
   2702 	/* copy into current space */
   2703 	memcpy(ptr, &aa->ap, p_length);
   2704 
   2705 	/* network elements and update lengths */
   2706 	aph = (struct sctp_asconf_paramhdr *)ptr;
   2707 	aap = (struct sctp_asconf_addr_param *)ptr;
   2708 	/* correlation_id is transparent to peer, no htonl needed */
   2709 	aph->ph.param_type = htons(aph->ph.param_type);
   2710 	aph->ph.param_length = htons(aph->ph.param_length);
   2711 	aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
   2712 	aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
   2713 
   2714 	SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
   2715 	ptr += SCTP_SIZE32(p_length);
   2716 
   2717 	/*
   2718 	 * these params are removed off the pending list upon
   2719 	 * getting an ASCONF-ACK back from the peer, just set flag
   2720 	 */
   2721 	aa->sent = 1;
   2722 }
   2723 /* check to see if the lookup addr has been populated yet */
   2724 if (lookup_used == 0) {
   2725 	/* NOTE: if the address param is optional, can skip this... */
   2726 	/* add any valid (existing) address... */
   2727 	struct sctp_ipv6addr_param *lookup;
   2728 	uint16_t p_size, addr_size;
   2729 	struct sockaddr *found_addr;
   2730 	caddr_t addr_ptr;
   2731 
   2732 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
   2733 		found_addr = sctp_find_valid_localaddr(stcb,
   2734 						       addr_locked);
   2735 	else
   2736 		found_addr = sctp_find_valid_localaddr_ep(stcb);
   2737 
   2738 	lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
   2739 	if (found_addr != NULL) {
   2740 		switch (found_addr->sa_family) {
   2741 #ifdef INET6
   2742 		case AF_INET6:
   2743 			/* copy IPv6 address */
   2744 			lookup->ph.param_type =
   2745 			    htons(SCTP_IPV6_ADDRESS);
   2746 			p_size = sizeof(struct sctp_ipv6addr_param);
   2747 			addr_size = sizeof(struct in6_addr);
   2748 			addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
   2749 			    found_addr)->sin6_addr;
   2750 			break;
   2751 #endif
   2752 #ifdef INET
   2753 		case AF_INET:
   2754 			/* copy IPv4 address */
   2755 			lookup->ph.param_type =
   2756 			    htons(SCTP_IPV4_ADDRESS);
   2757 			p_size = sizeof(struct sctp_ipv4addr_param);
   2758 			addr_size = sizeof(struct in_addr);
   2759 			addr_ptr = (caddr_t)&((struct sockaddr_in *)
   2760 			    found_addr)->sin_addr;
   2761 			break;
   2762 #endif
   2763 		default:
   2764 			SCTPDBG(SCTP_DEBUG_ASCONF1,
   2765 				"sctp_compose_asconf: no usable lookup addr (family = %d)!\n",
   2766 				found_addr->sa_family);
   2767 			sctp_m_freem(m_asconf_chk);
   2768 			sctp_m_freem(m_asconf);
   2769 			return (NULL);
   2770 		}
   2771 		lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
   2772 		memcpy(lookup->addr, addr_ptr, addr_size);
   2773 		SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
   2774 	} else {
   2775 		/* uh oh... don't have any address?? */
   2776 		SCTPDBG(SCTP_DEBUG_ASCONF1,
   2777 			"sctp_compose_asconf: no lookup addr!\n");
   2778 		sctp_m_freem(m_asconf_chk);
   2779 		sctp_m_freem(m_asconf);
   2780 		return (NULL);
   2781 	}
   2782 }
   2783 /* chain it all together */
   2784 SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
   2785 *retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
   2786 acp->ch.chunk_length = htons(*retlen);
   2787 
   2788 return (m_asconf_chk);
   2789 }
   2790 
   2791 /*
   2792 * section to handle address changes before an association is up eg. changes
   2793 * during INIT/INIT-ACK/COOKIE-ECHO handshake
   2794 */
   2795 
   2796 /*
   2797 * processes the (local) addresses in the INIT-ACK chunk
   2798 */
   2799 static void
   2800 sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
   2801    unsigned int offset, unsigned int length)
   2802 {
   2803 struct sctp_paramhdr tmp_param, *ph;
   2804 uint16_t plen, ptype;
   2805 struct sctp_ifa *sctp_ifa;
   2806 union sctp_sockstore store;
   2807 #ifdef INET6
   2808 struct sctp_ipv6addr_param addr6_store;
   2809 #endif
   2810 #ifdef INET
   2811 struct sctp_ipv4addr_param addr4_store;
   2812 #endif
   2813 
   2814 SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
   2815 if (stcb == NULL) /* Un-needed check for SA */
   2816 	return;
   2817 
   2818 /* convert to upper bound */
   2819 length += offset;
   2820 
   2821 if ((offset + sizeof(struct sctp_paramhdr)) > length) {
   2822 	return;
   2823 }
   2824 /* go through the addresses in the init-ack */
   2825 ph = (struct sctp_paramhdr *)
   2826      sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
   2827                    (uint8_t *)&tmp_param);
   2828 while (ph != NULL) {
   2829 	ptype = ntohs(ph->param_type);
   2830 	plen = ntohs(ph->param_length);
   2831 	switch (ptype) {
   2832 #ifdef INET6
   2833 	case SCTP_IPV6_ADDRESS:
   2834 	{
   2835 		struct sctp_ipv6addr_param *a6p;
   2836 
   2837 		/* get the entire IPv6 address param */
   2838 		a6p = (struct sctp_ipv6addr_param *)
   2839 		    sctp_m_getptr(m, offset,
   2840 		    sizeof(struct sctp_ipv6addr_param),
   2841 		    (uint8_t *)&addr6_store);
   2842 		if (plen != sizeof(struct sctp_ipv6addr_param) ||
   2843 		    a6p == NULL) {
   2844 			return;
   2845 		}
   2846 		memset(&store, 0, sizeof(union sctp_sockstore));
   2847 		store.sin6.sin6_family = AF_INET6;
   2848 #ifdef HAVE_SIN6_LEN
   2849 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
   2850 #endif
   2851 		store.sin6.sin6_port = stcb->rport;
   2852 		memcpy(&store.sin6.sin6_addr, a6p->addr, sizeof(struct in6_addr));
   2853 		break;
   2854 	}
   2855 #endif
   2856 #ifdef INET
   2857 	case SCTP_IPV4_ADDRESS:
   2858 	{
   2859 		struct sctp_ipv4addr_param *a4p;
   2860 
   2861 		/* get the entire IPv4 address param */
   2862 		a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
   2863 								  sizeof(struct sctp_ipv4addr_param),
   2864 								  (uint8_t *)&addr4_store);
   2865 		if (plen != sizeof(struct sctp_ipv4addr_param) ||
   2866 		    a4p == NULL) {
   2867 			return;
   2868 		}
   2869 		memset(&store, 0, sizeof(union sctp_sockstore));
   2870 		store.sin.sin_family = AF_INET;
   2871 #ifdef HAVE_SIN_LEN
   2872 		store.sin.sin_len = sizeof(struct sockaddr_in);
   2873 #endif
   2874 		store.sin.sin_port = stcb->rport;
   2875 		store.sin.sin_addr.s_addr = a4p->addr;
   2876 		break;
   2877 	}
   2878 #endif
   2879 	default:
   2880 		goto next_addr;
   2881 	}
   2882 
   2883 	/* see if this address really (still) exists */
   2884 	sctp_ifa = sctp_find_ifa_by_addr(&store.sa, stcb->asoc.vrf_id,
   2885 					 SCTP_ADDR_NOT_LOCKED);
   2886 	if (sctp_ifa == NULL) {
   2887 		/* address doesn't exist anymore */
   2888 		int status;
   2889 
   2890 		/* are ASCONFs allowed ? */
   2891 		if ((sctp_is_feature_on(stcb->sctp_ep,
   2892 		    SCTP_PCB_FLAGS_DO_ASCONF)) &&
   2893 		    stcb->asoc.asconf_supported) {
   2894 			/* queue an ASCONF DEL_IP_ADDRESS */
   2895 			status = sctp_asconf_queue_sa_delete(stcb, &store.sa);
   2896 			/*
   2897 			 * if queued ok, and in correct state, send
   2898 			 * out the ASCONF.
   2899 			 */
   2900 			if (status == 0 &&
   2901 			    SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
   2902 #ifdef SCTP_TIMER_BASED_ASCONF
   2903 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
   2904 						 stcb->sctp_ep, stcb,
   2905 						 stcb->asoc.primary_destination);
   2906 #else
   2907 				sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
   2908 #endif
   2909 			}
   2910 		}
   2911 	}
   2912 
   2913 next_addr:
   2914 	/*
   2915 	 * Sanity check:  Make sure the length isn't 0, otherwise
   2916 	 * we'll be stuck in this loop for a long time...
   2917 	 */
   2918 	if (SCTP_SIZE32(plen) == 0) {
   2919 		SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
   2920 			    plen, ptype);
   2921 		return;
   2922 	}
   2923 	/* get next parameter */
   2924 	offset += SCTP_SIZE32(plen);
   2925 	if ((offset + sizeof(struct sctp_paramhdr)) > length)
   2926 		return;
   2927 	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
   2928 	    sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
   2929 } /* while */
   2930 }
   2931 
   2932 /* FIX ME: need to verify return result for v6 address type if v6 disabled */
   2933 /*
   2934 * checks to see if a specific address is in the initack address list returns
   2935 * 1 if found, 0 if not
   2936 */
   2937 static uint32_t
   2938 sctp_addr_in_initack(struct mbuf *m, uint32_t offset, uint32_t length, struct sockaddr *sa)
   2939 {
   2940 struct sctp_paramhdr tmp_param, *ph;
   2941 uint16_t plen, ptype;
   2942 #ifdef INET
   2943 struct sockaddr_in *sin;
   2944 struct sctp_ipv4addr_param *a4p;
   2945 struct sctp_ipv6addr_param addr4_store;
   2946 #endif
   2947 #ifdef INET6
   2948 struct sockaddr_in6 *sin6;
   2949 struct sctp_ipv6addr_param *a6p;
   2950 struct sctp_ipv6addr_param addr6_store;
   2951 #ifdef SCTP_EMBEDDED_V6_SCOPE
   2952 struct sockaddr_in6 sin6_tmp;
   2953 #endif
   2954 #endif
   2955 
   2956 switch (sa->sa_family) {
   2957 #ifdef INET
   2958 case AF_INET:
   2959 	break;
   2960 #endif
   2961 #ifdef INET6
   2962 case AF_INET6:
   2963 	break;
   2964 #endif
   2965 default:
   2966 	return (0);
   2967 }
   2968 
   2969 SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
   2970 SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
   2971 /* convert to upper bound */
   2972 length += offset;
   2973 
   2974 if ((offset + sizeof(struct sctp_paramhdr)) > length) {
   2975 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   2976 		"find_initack_addr: invalid offset?\n");
   2977 	return (0);
   2978 }
   2979 /* go through the addresses in the init-ack */
   2980 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
   2981     sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
   2982 while (ph != NULL) {
   2983 	ptype = ntohs(ph->param_type);
   2984 	plen = ntohs(ph->param_length);
   2985 	switch (ptype) {
   2986 #ifdef INET6
   2987 	case SCTP_IPV6_ADDRESS:
   2988 		if (sa->sa_family == AF_INET6) {
   2989 			/* get the entire IPv6 address param */
   2990 			if (plen != sizeof(struct sctp_ipv6addr_param)) {
   2991 				break;
   2992 			}
   2993 			/* get the entire IPv6 address param */
   2994 			a6p = (struct sctp_ipv6addr_param *)
   2995 			      sctp_m_getptr(m, offset,
   2996 			                    sizeof(struct sctp_ipv6addr_param),
   2997 			                    (uint8_t *)&addr6_store);
   2998 			if (a6p == NULL) {
   2999 				return (0);
   3000 			}
   3001 			sin6 = (struct sockaddr_in6 *)sa;
   3002 #ifdef SCTP_EMBEDDED_V6_SCOPE
   3003 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
   3004 				/* create a copy and clear scope */
   3005 				memcpy(&sin6_tmp, sin6,
   3006 				       sizeof(struct sockaddr_in6));
   3007 				sin6 = &sin6_tmp;
   3008 				in6_clearscope(&sin6->sin6_addr);
   3009 			}
   3010 #endif /* SCTP_EMBEDDED_V6_SCOPE */
   3011 			if (memcmp(&sin6->sin6_addr, a6p->addr,
   3012 			           sizeof(struct in6_addr)) == 0) {
   3013 				/* found it */
   3014 				return (1);
   3015 			}
   3016 		}
   3017 		break;
   3018 #endif /* INET6 */
   3019 #ifdef INET
   3020 	case SCTP_IPV4_ADDRESS:
   3021 		if (sa->sa_family == AF_INET) {
   3022 			if (plen != sizeof(struct sctp_ipv4addr_param)) {
   3023 				break;
   3024 			}
   3025 			/* get the entire IPv4 address param */
   3026 			a4p = (struct sctp_ipv4addr_param *)
   3027 			      sctp_m_getptr(m, offset,
   3028 			                    sizeof(struct sctp_ipv4addr_param),
   3029 			                    (uint8_t *)&addr4_store);
   3030 			if (a4p == NULL) {
   3031 				return (0);
   3032 			}
   3033 			sin = (struct sockaddr_in *)sa;
   3034 			if (sin->sin_addr.s_addr == a4p->addr) {
   3035 				/* found it */
   3036 				return (1);
   3037 			}
   3038 		}
   3039 		break;
   3040 #endif
   3041 	default:
   3042 		break;
   3043 	}
   3044 	/* get next parameter */
   3045 	offset += SCTP_SIZE32(plen);
   3046 	if (offset + sizeof(struct sctp_paramhdr) > length) {
   3047 		return (0);
   3048 	}
   3049 	ph = (struct sctp_paramhdr *)
   3050 	    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
   3051 	    (uint8_t *) & tmp_param);
   3052 } /* while */
   3053 /* not found! */
   3054 return (0);
   3055 }
   3056 
   3057 /*
   3058 * makes sure that the current endpoint local addr list is consistent with
   3059 * the new association (eg. subset bound, asconf allowed) adds addresses as
   3060 * necessary
   3061 */
   3062 static void
   3063 sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
   3064    int length, struct sockaddr *init_addr)
   3065 {
   3066 struct sctp_laddr *laddr;
   3067 
   3068 /* go through the endpoint list */
   3069 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
   3070 	/* be paranoid and validate the laddr */
   3071 	if (laddr->ifa == NULL) {
   3072 		SCTPDBG(SCTP_DEBUG_ASCONF1,
   3073 			"check_addr_list_ep: laddr->ifa is NULL");
   3074 		continue;
   3075 	}
   3076 	/* do i have it implicitly? */
   3077 	if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
   3078 		continue;
   3079 	}
   3080 	/* check to see if in the init-ack */
   3081 	if (!sctp_addr_in_initack(m, offset, length, &laddr->ifa->address.sa)) {
   3082 		/* try to add it */
   3083 		sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
   3084 		    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
   3085 	}
   3086 }
   3087 }
   3088 
   3089 /*
   3090 * makes sure that the current kernel address list is consistent with the new
   3091 * association (with all addrs bound) adds addresses as necessary
   3092 */
   3093 static void
   3094 sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
   3095    int length, struct sockaddr *init_addr,
   3096    uint16_t local_scope, uint16_t site_scope,
   3097    uint16_t ipv4_scope, uint16_t loopback_scope)
   3098 {
   3099 struct sctp_vrf *vrf = NULL;
   3100 struct sctp_ifn *sctp_ifn;
   3101 struct sctp_ifa *sctp_ifa;
   3102 uint32_t vrf_id;
   3103 #ifdef INET
   3104 struct sockaddr_in *sin;
   3105 #endif
   3106 #ifdef INET6
   3107 struct sockaddr_in6 *sin6;
   3108 #endif
   3109 
   3110 if (stcb) {
   3111 	vrf_id = stcb->asoc.vrf_id;
   3112 } else {
   3113 	return;
   3114 }
   3115 SCTP_IPI_ADDR_RLOCK();
   3116 vrf = sctp_find_vrf(vrf_id);
   3117 if (vrf == NULL) {
   3118 	SCTP_IPI_ADDR_RUNLOCK();
   3119 	return;
   3120 }
   3121 /* go through all our known interfaces */
   3122 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
   3123 	if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
   3124 		/* skip loopback interface */
   3125 		continue;
   3126 	}
   3127 	/* go through each interface address */
   3128 	LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
   3129 		/* do i have it implicitly? */
   3130 		if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
   3131 			continue;
   3132 		}
   3133 		switch (sctp_ifa->address.sa.sa_family) {
   3134 #ifdef INET
   3135 		case AF_INET:
   3136 			sin = &sctp_ifa->address.sin;
   3137 #if defined(__FreeBSD__) && !defined(__Userspace__)
   3138 			if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
   3139 			                     &sin->sin_addr) != 0) {
   3140 				continue;
   3141 			}
   3142 #endif
   3143 			if ((ipv4_scope == 0) &&
   3144 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
   3145 				/* private address not in scope */
   3146 				continue;
   3147 			}
   3148 			break;
   3149 #endif
   3150 #ifdef INET6
   3151 		case AF_INET6:
   3152 			sin6 = &sctp_ifa->address.sin6;
   3153 #if defined(__FreeBSD__) && !defined(__Userspace__)
   3154 			if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
   3155 			                     &sin6->sin6_addr) != 0) {
   3156 				continue;
   3157 			}
   3158 #endif
   3159 			if ((local_scope == 0) &&
   3160 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
   3161 				continue;
   3162 			}
   3163 			if ((site_scope == 0) &&
   3164 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
   3165 				continue;
   3166 			}
   3167 			break;
   3168 #endif
   3169 		default:
   3170 			break;
   3171 		}
   3172 		/* check to see if in the init-ack */
   3173 		if (!sctp_addr_in_initack(m, offset, length, &sctp_ifa->address.sa)) {
   3174 			/* try to add it */
   3175 			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
   3176 			    sctp_ifa, SCTP_ADD_IP_ADDRESS,
   3177 			    SCTP_ADDR_LOCKED);
   3178 		}
   3179 	} /* end foreach ifa */
   3180 } /* end foreach ifn */
   3181 SCTP_IPI_ADDR_RUNLOCK();
   3182 }
   3183 
   3184 /*
   3185 * validates an init-ack chunk (from a cookie-echo) with current addresses
   3186 * adds addresses from the init-ack into our local address list, if needed
   3187 * queues asconf adds/deletes addresses as needed and makes appropriate list
   3188 * changes for source address selection m, offset: points to the start of the
   3189 * address list in an init-ack chunk length: total length of the address
   3190 * params only init_addr: address where my INIT-ACK was sent from
   3191 */
   3192 void
   3193 sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
   3194    int length, struct sockaddr *init_addr,
   3195    uint16_t local_scope, uint16_t site_scope,
   3196    uint16_t ipv4_scope, uint16_t loopback_scope)
   3197 {
   3198 /* process the local addresses in the initack */
   3199 sctp_process_initack_addresses(stcb, m, offset, length);
   3200 
   3201 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3202 	/* bound all case */
   3203 	sctp_check_address_list_all(stcb, m, offset, length, init_addr,
   3204 	    local_scope, site_scope, ipv4_scope, loopback_scope);
   3205 } else {
   3206 	/* subset bound case */
   3207 	if (sctp_is_feature_on(stcb->sctp_ep,
   3208 	    SCTP_PCB_FLAGS_DO_ASCONF)) {
   3209 		/* asconf's allowed */
   3210 		sctp_check_address_list_ep(stcb, m, offset, length,
   3211 		    init_addr);
   3212 	}
   3213 	/* else, no asconfs allowed, so what we sent is what we get */
   3214 }
   3215 }
   3216 
   3217 /*
   3218 * sctp_bindx() support
   3219 */
   3220 uint32_t
   3221 sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
   3222                     uint32_t type, uint32_t vrf_id)
   3223 {
   3224 struct sctp_ifa *ifa;
   3225 struct sctp_laddr *laddr, *nladdr;
   3226 
   3227 #ifdef HAVE_SA_LEN
   3228 if (sa->sa_len == 0) {
   3229 	SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
   3230 	return (EINVAL);
   3231 }
   3232 #endif
   3233 if (type == SCTP_ADD_IP_ADDRESS) {
   3234 	/* For an add the address MUST be on the system */
   3235 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
   3236 } else if (type == SCTP_DEL_IP_ADDRESS) {
   3237 	/* For a delete we need to find it in the inp */
   3238 	ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
   3239 } else {
   3240 	ifa = NULL;
   3241 }
   3242 if (ifa != NULL) {
   3243 	if (type == SCTP_ADD_IP_ADDRESS) {
   3244 		sctp_add_local_addr_ep(inp, ifa, type);
   3245 	} else if (type == SCTP_DEL_IP_ADDRESS) {
   3246 		if (inp->laddr_count < 2) {
   3247 			/* can't delete the last local address */
   3248 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
   3249 			return (EINVAL);
   3250 		}
   3251 		LIST_FOREACH(laddr, &inp->sctp_addr_list,
   3252 			     sctp_nxt_addr) {
   3253 			if (ifa == laddr->ifa) {
   3254 				/* Mark in the delete */
   3255 				laddr->action = type;
   3256 			}
   3257 		}
   3258 	}
   3259 	if (LIST_EMPTY(&inp->sctp_asoc_list)) {
   3260 		/*
   3261 		 * There is no need to start the iterator if
   3262 		 * the inp has no associations.
   3263 		 */
   3264 		if (type == SCTP_DEL_IP_ADDRESS) {
   3265 			LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
   3266 				if (laddr->ifa == ifa) {
   3267 					sctp_del_local_addr_ep(inp, ifa);
   3268 				}
   3269 			}
   3270 		}
   3271 	} else {
   3272 		struct sctp_asconf_iterator *asc;
   3273 		struct sctp_laddr *wi;
   3274 		int ret;
   3275 
   3276 		SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
   3277 		            sizeof(struct sctp_asconf_iterator),
   3278 		            SCTP_M_ASC_IT);
   3279 		if (asc == NULL) {
   3280 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
   3281 			return (ENOMEM);
   3282 		}
   3283 		wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
   3284 		if (wi == NULL) {
   3285 			SCTP_FREE(asc, SCTP_M_ASC_IT);
   3286 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
   3287 			return (ENOMEM);
   3288 		}
   3289 		LIST_INIT(&asc->list_of_work);
   3290 		asc->cnt = 1;
   3291 		SCTP_INCR_LADDR_COUNT();
   3292 		wi->ifa = ifa;
   3293 		wi->action = type;
   3294 		atomic_add_int(&ifa->refcount, 1);
   3295 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
   3296 		ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
   3297 		                             sctp_asconf_iterator_stcb,
   3298 		                             sctp_asconf_iterator_ep_end,
   3299 		                             SCTP_PCB_ANY_FLAGS,
   3300 		                             SCTP_PCB_ANY_FEATURES,
   3301 		                             SCTP_ASOC_ANY_STATE,
   3302 		                             (void *)asc, 0,
   3303 		                             sctp_asconf_iterator_end, inp, 0);
   3304 		if (ret) {
   3305 			SCTP_PRINTF("Failed to initiate iterator for addr_mgmt_ep_sa\n");
   3306 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EFAULT);
   3307 			                    sctp_asconf_iterator_end(asc, 0);
   3308 			return (EFAULT);
   3309 		}
   3310 	}
   3311 	return (0);
   3312 } else {
   3313 	/* invalid address! */
   3314 	SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
   3315 	return (EADDRNOTAVAIL);
   3316 }
   3317 }
   3318 
   3319 void
   3320 sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb, struct sctp_nets *net)
   3321 {
   3322 struct sctp_asconf_addr *aa_vtag, *aa_add, *aa_del;
   3323 struct sctp_ifa *sctp_ifap;
   3324 struct sctp_asconf_tag_param *vtag;
   3325 #ifdef INET
   3326 struct sockaddr_in *to;
   3327 #endif
   3328 #ifdef INET6
   3329 struct sockaddr_in6 *to6;
   3330 #endif
   3331 
   3332 if (net == NULL) {
   3333 	SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
   3334 	return;
   3335 }
   3336 if (stcb == NULL) {
   3337 	SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
   3338 	return;
   3339 }
   3340 /* Need to have in the ASCONF:
   3341  * - VTAG(my_vtag/peer_vtag)
   3342  * - ADD(wildcard)
   3343  * - DEL(wildcard)
   3344  * - ADD(Any global addresses)
   3345  */
   3346 SCTP_MALLOC(aa_vtag, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
   3347 SCTP_MALLOC(aa_add, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
   3348 SCTP_MALLOC(aa_del, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
   3349 
   3350 if ((aa_vtag == NULL) || (aa_add == NULL) || (aa_del == NULL)) {
   3351 	/* Didn't get memory */
   3352 	SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: failed to get memory!\n");
   3353 out:
   3354 	if (aa_vtag != NULL) {
   3355 		SCTP_FREE(aa_vtag, SCTP_M_ASC_ADDR);
   3356 	}
   3357 	if (aa_add != NULL) {
   3358 		SCTP_FREE(aa_add, SCTP_M_ASC_ADDR);
   3359 	}
   3360 	if (aa_del != NULL) {
   3361 		SCTP_FREE(aa_del, SCTP_M_ASC_ADDR);
   3362 	}
   3363 	return;
   3364 }
   3365 memset(aa_vtag, 0, sizeof(struct sctp_asconf_addr));
   3366 aa_vtag->special_del = 0;
   3367 /* Fill in ASCONF address parameter fields. */
   3368 /* Top level elements are "networked" during send. */
   3369 aa_vtag->ifa = NULL;
   3370 aa_vtag->sent = 0;		/* clear sent flag */
   3371 vtag = (struct sctp_asconf_tag_param *)&aa_vtag->ap.aph;
   3372 vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
   3373 vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
   3374 vtag->local_vtag = htonl(stcb->asoc.my_vtag);
   3375 vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
   3376 
   3377 memset(aa_add, 0, sizeof(struct sctp_asconf_addr));
   3378 memset(aa_del, 0, sizeof(struct sctp_asconf_addr));
   3379 switch (net->ro._l_addr.sa.sa_family) {
   3380 #ifdef INET
   3381 case AF_INET:
   3382 	aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
   3383 	aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
   3384 	aa_add->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
   3385 	aa_add->ap.addrp.ph.param_length = sizeof (struct sctp_ipv4addr_param);
   3386 	/* No need to fill the address, we are using 0.0.0.0 */
   3387 	aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
   3388 	aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
   3389 	aa_del->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
   3390 	aa_del->ap.addrp.ph.param_length = sizeof (struct sctp_ipv4addr_param);
   3391 	/* No need to fill the address, we are using 0.0.0.0 */
   3392 	break;
   3393 #endif
   3394 #ifdef INET6
   3395 case AF_INET6:
   3396 	aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
   3397 	aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
   3398 	aa_add->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
   3399 	aa_add->ap.addrp.ph.param_length = sizeof (struct sctp_ipv6addr_param);
   3400 	/* No need to fill the address, we are using ::0 */
   3401 	aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
   3402 	aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
   3403 	aa_del->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
   3404 	aa_del->ap.addrp.ph.param_length = sizeof (struct sctp_ipv6addr_param);
   3405 	/* No need to fill the address, we are using ::0 */
   3406 	break;
   3407 #endif
   3408 default:
   3409 	SCTPDBG(SCTP_DEBUG_ASCONF1,
   3410 	        "sctp_asconf_send_nat_state_update: unknown address family %d\n",
   3411 	        net->ro._l_addr.sa.sa_family);
   3412 	goto out;
   3413 }
   3414 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_vtag, next);
   3415 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_add, next);
   3416 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_del, next);
   3417 
   3418 /* Now we must hunt the addresses and add all global addresses */
   3419 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3420 	struct sctp_vrf *vrf = NULL;
   3421 	struct sctp_ifn *sctp_ifnp;
   3422 	uint32_t vrf_id;
   3423 
   3424 	vrf_id = stcb->sctp_ep->def_vrf_id;
   3425 	vrf = sctp_find_vrf(vrf_id);
   3426 	if (vrf == NULL) {
   3427 		goto skip_rest;
   3428 	}
   3429 
   3430 	SCTP_IPI_ADDR_RLOCK();
   3431 	LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
   3432 		LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
   3433 			switch (sctp_ifap->address.sa.sa_family) {
   3434 #ifdef INET
   3435 			case AF_INET:
   3436 				to = &sctp_ifap->address.sin;
   3437 #if defined(__FreeBSD__) && !defined(__Userspace__)
   3438 				if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
   3439 				                     &to->sin_addr) != 0) {
   3440 					continue;
   3441 				}
   3442 #endif
   3443 				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
   3444 					continue;
   3445 				}
   3446 				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
   3447 					continue;
   3448 				}
   3449 				break;
   3450 #endif
   3451 #ifdef INET6
   3452 			case AF_INET6:
   3453 				to6 = &sctp_ifap->address.sin6;
   3454 #if defined(__FreeBSD__) && !defined(__Userspace__)
   3455 				if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
   3456 				                     &to6->sin6_addr) != 0) {
   3457 					continue;
   3458 				}
   3459 #endif
   3460 				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
   3461 					continue;
   3462 				}
   3463 				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
   3464 					continue;
   3465 				}
   3466 				break;
   3467 #endif
   3468 			default:
   3469 				continue;
   3470 			}
   3471 			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
   3472 		}
   3473 	}
   3474 	SCTP_IPI_ADDR_RUNLOCK();
   3475 } else {
   3476 	struct sctp_laddr *laddr;
   3477 
   3478 	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
   3479 		if (laddr->ifa == NULL) {
   3480 			continue;
   3481 		}
   3482 		if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
   3483 			/* Address being deleted by the system, dont
   3484 			 * list.
   3485 			 */
   3486 			continue;
   3487 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
   3488 			/* Address being deleted on this ep
   3489 			 * don't list.
   3490 			 */
   3491 			continue;
   3492 		}
   3493 		sctp_ifap = laddr->ifa;
   3494 		switch (sctp_ifap->address.sa.sa_family) {
   3495 #ifdef INET
   3496 		case AF_INET:
   3497 			to = &sctp_ifap->address.sin;
   3498 			if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
   3499 				continue;
   3500 			}
   3501 			if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
   3502 				continue;
   3503 			}
   3504 			break;
   3505 #endif
   3506 #ifdef INET6
   3507 		case AF_INET6:
   3508 			to6 = &sctp_ifap->address.sin6;
   3509 			if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
   3510 				continue;
   3511 			}
   3512 			if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
   3513 				continue;
   3514 			}
   3515 			break;
   3516 #endif
   3517 		default:
   3518 			continue;
   3519 		}
   3520 		sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
   3521 	}
   3522 }
   3523 skip_rest:
   3524 /* Now we must send the asconf into the queue */
   3525 sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
   3526 }