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