#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sctp_asconf.c,v 1.14 2024/04/11 07:34:37 knakahara Exp $");
#ifdef _KERNEL_OPT
#include "opt_ipsec.h"
#include "opt_inet.h"
#include "opt_sctp.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#ifdef INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.h>
#include <netinet/icmp6.h>
#include <netinet6/nd6.h>
#include <netinet6/scope6_var.h>
#include <netinet6/nd6.h>
#endif
#include <netinet/in_pcb.h>
#include <netinet/sctp_var.h>
#include <netinet/sctp_pcb.h>
#include <netinet/sctp_header.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_output.h>
#include <netinet/sctp_asconf.h>
#include <netinet/sctp_route.h>
#ifdef SCTP_DEBUG
extern u_int32_t sctp_debug_on;
#endif
static struct mbuf *
sctp_asconf_success_response(uint32_t id)
{
struct mbuf *m_reply = NULL;
struct sctp_asconf_paramhdr *aph;
MGET(m_reply, M_DONTWAIT, MT_DATA);
if (m_reply == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_success_response: couldn't get mbuf!\n");
}
#endif
return NULL;
}
aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
aph->correlation_id = id;
aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
m_reply->m_len = aph->ph.param_length;
aph->ph.param_length = htons(aph->ph.param_length);
return m_reply;
}
static struct mbuf *
sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
uint16_t tlv_length)
{
struct mbuf *m_reply = NULL;
struct sctp_asconf_paramhdr *aph;
struct sctp_error_cause *error;
uint8_t *tlv;
MGET(m_reply, M_DONTWAIT, MT_DATA);
if (m_reply == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_error_response: couldn't get mbuf!\n");
}
#endif
return NULL;
}
aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
error = (struct sctp_error_cause *)(aph + 1);
aph->correlation_id = id;
aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
error->code = htons(cause);
error->length = tlv_length + sizeof(struct sctp_error_cause);
aph->ph.param_length = error->length +
sizeof(struct sctp_asconf_paramhdr);
if (aph->ph.param_length > MLEN) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_error_response: tlv_length (%xh) too big\n",
tlv_length);
}
#endif
sctp_m_freem(m_reply);
return NULL;
}
if (error_tlv != NULL) {
tlv = (uint8_t *)(error + 1);
memcpy(tlv, error_tlv, tlv_length);
}
m_reply->m_len = aph->ph.param_length;
error->length = htons(error->length);
aph->ph.param_length = htons(aph->ph.param_length);
return m_reply;
}
static struct mbuf *
sctp_process_asconf_add_ip(struct sctp_asconf_paramhdr *aph,
struct sctp_tcb *stcb, int response_required)
{
struct mbuf *m_reply = NULL;
struct sockaddr_storage sa_store;
struct sctp_ipv4addr_param *v4addr;
uint16_t param_type, param_length, aparam_length;
struct sockaddr *sa;
struct sockaddr_in *sin;
#ifdef INET6
struct sockaddr_in6 *sin6;
struct sctp_ipv6addr_param *v6addr;
#endif
aparam_length = ntohs(aph->ph.param_length);
v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
#ifdef INET6
v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
#endif
param_type = ntohs(v4addr->ph.param_type);
param_length = ntohs(v4addr->ph.param_length);
sa = (struct sockaddr *)&sa_store;
switch (param_type) {
case SCTP_IPV4_ADDRESS:
if (param_length != sizeof(struct sctp_ipv4addr_param)) {
return NULL;
}
sin = (struct sockaddr_in *)&sa_store;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_port = stcb->rport;
sin->sin_addr.s_addr = v4addr->addr;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_add_ip: adding ");
sctp_print_address(sa);
}
#endif
break;
case SCTP_IPV6_ADDRESS:
#ifdef INET6
if (param_length != sizeof(struct sctp_ipv6addr_param)) {
return NULL;
}
sin6 = (struct sockaddr_in6 *)&sa_store;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_port = stcb->rport;
memcpy((void *)&sin6->sin6_addr, v6addr->addr,
sizeof(struct in6_addr));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_add_ip: adding ");
sctp_print_address(sa);
}
#endif
#else
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_INVALID_PARAM, (uint8_t *)aph, aparam_length);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_add_ip: v6 disabled- skipping ");
sctp_print_address(sa);
}
#endif
return m_reply;
#endif
break;
default:
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
aparam_length);
return m_reply;
}
if (sctp_add_remote_addr(stcb, sa, 0, 6) != 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_add_ip: error adding address\n");
}
#endif
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_RESOURCE_SHORTAGE, (uint8_t *)aph,
aparam_length);
} else {
sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa);
if (response_required) {
m_reply =
sctp_asconf_success_response(aph->correlation_id);
}
}
return m_reply;
}
static struct mbuf *
sctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
struct sctp_tcb *stcb, int response_required)
{
struct mbuf *m_reply = NULL;
struct sockaddr_storage sa_store, sa_source;
struct sctp_ipv4addr_param *v4addr;
uint16_t param_type, param_length, aparam_length;
struct sockaddr *sa;
struct sockaddr_in *sin;
struct ip *iph;
int result;
#ifdef INET6
struct sockaddr_in6 *sin6;
struct sctp_ipv6addr_param *v6addr;
#endif
aparam_length = ntohs(aph->ph.param_length);
v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
#ifdef INET6
v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
#endif
param_type = ntohs(v4addr->ph.param_type);
param_length = ntohs(v4addr->ph.param_length);
iph = mtod(m, struct ip *);
if (iph->ip_v == IPVERSION) {
sin = (struct sockaddr_in *)&sa_source;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_port = stcb->rport;
sin->sin_addr.s_addr = iph->ip_src.s_addr;
}
#ifdef INET6
else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6;
sin6 = (struct sockaddr_in6 *)&sa_source;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_port = stcb->rport;
ip6 = mtod(m, struct ip6_hdr *);
sin6->sin6_addr = ip6->ip6_src;
}
#endif
else
return NULL;
sa = (struct sockaddr *)&sa_store;
switch (param_type) {
case SCTP_IPV4_ADDRESS:
if (param_length != sizeof(struct sctp_ipv4addr_param)) {
return NULL;
}
sin = (struct sockaddr_in *)&sa_store;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_port = stcb->rport;
sin->sin_addr.s_addr = v4addr->addr;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_delete_ip: deleting ");
sctp_print_address(sa);
}
#endif
break;
case SCTP_IPV6_ADDRESS:
if (param_length != sizeof(struct sctp_ipv6addr_param)) {
return NULL;
}
#ifdef INET6
sin6 = (struct sockaddr_in6 *)&sa_store;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_port = stcb->rport;
memcpy(&sin6->sin6_addr, v6addr->addr,
sizeof(struct in6_addr));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_delete_ip: deleting ");
sctp_print_address(sa);
}
#endif
#else
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_delete_ip: v6 disabled- ignoring: ");
sctp_print_address(sa);
}
#endif
return NULL;
#endif
break;
default:
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
aparam_length);
return m_reply;
}
if ((memcmp(sa, &sa_source, sizeof(struct sockaddr_in)) == 0)
#ifdef INET6
|| (memcmp(sa, &sa_source, sizeof(struct sockaddr_in6)) == 0)
#endif
) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_delete_ip: tried to delete source addr\n");
}
#endif
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_DELETE_SOURCE_ADDR, (uint8_t *)aph,
aparam_length);
return m_reply;
}
result = sctp_del_remote_addr(stcb, sa);
if (result == -1) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_delete_ip: tried to delete last IP addr!\n");
}
#endif
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_DELETE_LAST_ADDR, (uint8_t *)aph,
aparam_length);
} else {
sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa);
}
if (response_required) {
m_reply = sctp_asconf_success_response(aph->correlation_id);
}
return m_reply;
}
static struct mbuf *
sctp_process_asconf_set_primary(struct sctp_asconf_paramhdr *aph,
struct sctp_tcb *stcb, int response_required)
{
struct mbuf *m_reply = NULL;
struct sockaddr_storage sa_store;
struct sctp_ipv4addr_param *v4addr;
uint16_t param_type, param_length, aparam_length;
struct sockaddr *sa;
struct sockaddr_in *sin;
#ifdef INET6
struct sockaddr_in6 *sin6;
struct sctp_ipv6addr_param *v6addr;
#endif
aparam_length = ntohs(aph->ph.param_length);
v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
#ifdef INET6
v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
#endif
param_type = ntohs(v4addr->ph.param_type);
param_length = ntohs(v4addr->ph.param_length);
sa = (struct sockaddr *)&sa_store;
switch (param_type) {
case SCTP_IPV4_ADDRESS:
if (param_length != sizeof(struct sctp_ipv4addr_param)) {
return NULL;
}
sin = (struct sockaddr_in *)&sa_store;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_addr.s_addr = v4addr->addr;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_set_primary: ");
sctp_print_address(sa);
}
#endif
break;
case SCTP_IPV6_ADDRESS:
if (param_length != sizeof(struct sctp_ipv6addr_param)) {
return NULL;
}
#ifdef INET6
sin6 = (struct sockaddr_in6 *)&sa_store;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
memcpy((void *)&sin6->sin6_addr, v6addr->addr,
sizeof(struct in6_addr));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_set_primary: ");
sctp_print_address(sa);
}
#endif
#else
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_set_primary: v6 disabled- ignoring: ");
sctp_print_address(sa);
}
#endif
return NULL;
#endif
break;
default:
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
aparam_length);
return m_reply;
}
if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_set_primary: primary address set\n");
}
#endif
sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa);
if (response_required) {
m_reply = sctp_asconf_success_response(aph->correlation_id);
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_asconf_set_primary: set primary failed!\n");
}
#endif
m_reply = sctp_asconf_error_response(aph->correlation_id,
SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
aparam_length);
}
return m_reply;
}
void
sctp_handle_asconf(struct mbuf *m, unsigned int offset, struct sctp_asconf_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_association *asoc;
uint32_t serial_num;
struct mbuf *m_ack, *m_result, *m_tail;
struct sctp_asconf_ack_chunk *ack_cp;
struct sctp_asconf_paramhdr *aph, *ack_aph;
struct sctp_ipv6addr_param *p_addr;
unsigned int asconf_limit;
int error = 0;
static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: chunk too small = %xh\n",
ntohs(cp->ch.chunk_length));
}
#endif
return;
}
asoc = &stcb->asoc;
serial_num = ntohl(cp->serial_number);
if (serial_num == asoc->asconf_seq_in) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: got duplicate serial number = %xh\n",
serial_num);
}
#endif
sctp_send_asconf_ack(stcb, 1);
return;
} else if (serial_num != (asoc->asconf_seq_in + 1)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
serial_num, asoc->asconf_seq_in+1);
}
#endif
return;
}
asoc->asconf_seq_in = serial_num;
asconf_limit = offset + ntohs(cp->ch.chunk_length);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: asconf_limit=%u, sequence=%xh\n",
asconf_limit, serial_num);
}
#endif
if (asoc->last_asconf_ack_sent != NULL) {
sctp_m_freem(asoc->last_asconf_ack_sent);
asoc->last_asconf_ack_sent = NULL;
}
MGETHDR(m_ack, M_DONTWAIT, MT_DATA);
if (m_ack == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: couldn't get mbuf!\n");
}
#endif
return;
}
m_tail = m_ack;
ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
ack_cp->ch.chunk_flags = 0;
ack_cp->serial_number = htonl(serial_num);
m_ack->m_len = sizeof(struct sctp_asconf_ack_chunk);
ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
m_ack->m_pkthdr.len = sizeof(struct sctp_asconf_ack_chunk);
offset += sizeof(struct sctp_asconf_chunk);
p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
if (p_addr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: couldn't get lookup addr!\n");
}
#endif
return;
}
offset += ntohs(p_addr->ph.param_length);
aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
if (aph == NULL) {
printf("Gak in asconf\n");
return;
}
ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, vaddr_t) + sizeof(struct sctp_asconf_ack_chunk));
if (ack_aph == NULL) {
printf("Gak in asconf2\n");
return;
}
while (aph != NULL) {
unsigned int param_length, param_type;
param_type = ntohs(aph->ph.param_type);
param_length = ntohs(aph->ph.param_length);
if (offset + param_length > asconf_limit) {
sctp_m_freem(m_ack);
return;
}
m_result = NULL;
if (param_length > sizeof(aparam_buf)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: param length (%u) larger than buffer size!\n", param_length);
}
#endif
sctp_m_freem(m_ack);
return;
}
if (param_length <= sizeof(struct sctp_paramhdr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: param length (%u) too short\n", param_length);
}
#endif
sctp_m_freem(m_ack);
}
aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
if (aph == NULL) {
printf("Gag\n");
sctp_m_freem(m_ack);
return;
}
switch (param_type) {
case SCTP_ADD_IP_ADDRESS:
asoc->peer_supports_asconf = 1;
m_result = sctp_process_asconf_add_ip(aph, stcb, error);
break;
case SCTP_DEL_IP_ADDRESS:
asoc->peer_supports_asconf = 1;
m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
error);
break;
case SCTP_ERROR_CAUSE_IND:
break;
case SCTP_SET_PRIM_ADDR:
asoc->peer_supports_asconf_setprim = 1;
m_result = sctp_process_asconf_set_primary(aph, stcb,
error);
break;
case SCTP_SUCCESS_REPORT:
break;
case SCTP_ULP_ADAPTION:
break;
default:
if ((param_type & 0x8000) == 0) {
asconf_limit = offset;
}
break;
}
if (m_result != NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: adding reply...\n");
}
#endif
m_tail->m_next = m_result;
m_tail = m_result;
m_result->m_len = SCTP_SIZE32(m_result->m_len);
m_ack->m_pkthdr.len += m_result->m_len;
ack_cp->ch.chunk_length += m_result->m_len;
error = 1;
}
offset += SCTP_SIZE32(param_length);
if (offset >= asconf_limit) {
break;
}
aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
sizeof(struct sctp_asconf_paramhdr),
(uint8_t *)&aparam_buf);
if (aph == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf: can't get asconf param hdr!\n");
}
#endif
}
}
ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
asoc->last_asconf_ack_sent = m_ack;
sctp_send_asconf_ack(stcb, 0);
}
static uint32_t
sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
{
#ifdef INET6
if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
(memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
sizeof(struct in6_addr)) == 0)) {
return (1);
}
} else
#endif
if (sa->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
(memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
sizeof(struct in_addr)) == 0)) {
return (1);
}
}
return (0);
}
void
sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
{
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_cleanup: marking peer ASCONF incapable and cleaning up\n");
}
#endif
stcb->asoc.peer_supports_asconf = 0;
stcb->asoc.peer_supports_asconf_setprim = 0;
sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
stcb->asoc.asconf_seq_out++;
sctp_toss_old_asconf(stcb);
}
static void
sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct ifaddr *addr,
uint16_t type, uint32_t flag)
{
if (flag) {
sctp_del_local_addr_assoc(stcb, addr);
}
}
static uint32_t
sctp_asconf_queue_add(struct sctp_tcb *stcb, struct ifaddr *ifa, uint16_t type)
{
struct sctp_asconf_addr *aa, *aa_next;
#ifdef SCTP_DEBUG
char buf[128];
#endif
if (stcb->asoc.peer_supports_asconf == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add: peer doesn't support ASCONF\n");
}
#endif
return (-1);
}
for (aa=TAILQ_FIRST(&stcb->asoc.asconf_queue); aa!=NULL; aa=aa_next) {
aa_next = TAILQ_NEXT(aa, next);
if (sctp_asconf_addr_match(aa, ifa->ifa_addr) == 0)
continue;
if (aa->ap.aph.ph.param_type == type) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add: request already exists\n");
}
#endif
return (-1);
}
if (aa->sent == 0 &&
((type == SCTP_ADD_IP_ADDRESS &&
aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) ||
(type == SCTP_DEL_IP_ADDRESS &&
aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS))) {
TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
free(aa, M_PCB);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add: removing 'opposite' queued request\n");
}
#endif
return (-1);
}
}
aa = malloc(sizeof(*aa), M_PCB, M_NOWAIT);
if (aa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add: failed to get memory!\n");
}
#endif
return (-1);
}
aa->ap.aph.ph.param_type = type;
aa->ifa = ifa;
if (ifa->ifa_addr->sa_family == AF_INET6) {
#ifdef SCTP_DEBUG
char ip6buf[INET6_ADDRSTRLEN];
#endif
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
aa->ap.aph.ph.param_length =
sizeof(struct sctp_asconf_paramhdr) +
sizeof(struct sctp_ipv6addr_param);
memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
sizeof(struct in6_addr));
#ifdef SCTP_DEBUG
strlcpy(buf, IN6_PRINT(ip6buf, &sin6->sin6_addr), sizeof(buf));
#endif
} else if (ifa->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)ifa->ifa_addr;
aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
aa->ap.aph.ph.param_length =
sizeof(struct sctp_asconf_paramhdr) +
sizeof(struct sctp_ipv4addr_param);
memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
sizeof(struct in_addr));
#ifdef SCTP_DEBUG
strlcpy(buf, inet_ntoa(sin->sin_addr), sizeof(buf));
#endif
} else {
return (-1);
}
aa->sent = 0;
if (type == SCTP_ADD_IP_ADDRESS) {
TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add: appended asconf ADD_IP_ADDRESS: %s\n", buf);
}
#endif
} else {
TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
if (type == SCTP_DEL_IP_ADDRESS) {
printf("asconf_queue_add: inserted asconf DEL_IP_ADDRESS: %s\n", buf);
} else {
printf("asconf_queue_add: inserted asconf SET_PRIM_ADDR: %s\n", buf);
}
}
#endif
}
return (0);
}
static uint32_t
sctp_asconf_queue_add_sa(struct sctp_tcb *stcb, struct sockaddr *sa,
uint16_t type)
{
struct sctp_asconf_addr *aa, *aa_next;
if (stcb->asoc.peer_supports_asconf == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: peer doesn't support ASCONF\n");
}
#endif
return (-1);
}
for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
aa = aa_next) {
aa_next = TAILQ_NEXT(aa, next);
if (sctp_asconf_addr_match(aa, sa) == 0)
continue;
if (aa->ap.aph.ph.param_type == type) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: request already exists\n");
}
#endif
return (-1);
}
if (aa->sent == 1)
continue;
if (type == SCTP_ADD_IP_ADDRESS &&
aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
free(aa, M_PCB);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: removing queued delete request\n");
}
#endif
return (-1);
} else if (type == SCTP_DEL_IP_ADDRESS &&
aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
free(aa, M_PCB);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: removing queued add request\n");
}
#endif
return (-1);
}
}
aa = malloc(sizeof(*aa), M_PCB, M_NOWAIT);
if (aa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: failed to get memory!\n");
}
#endif
return (-1);
}
aa->ap.aph.ph.param_type = type;
aa->ifa = sctp_find_ifa_by_addr(sa);
if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)sa;
aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
sizeof(struct in6_addr));
} else if (sa->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
sizeof(struct in_addr));
} else {
return (-1);
}
aa->sent = 0;
if (type == SCTP_ADD_IP_ADDRESS) {
TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_queue_add_sa: appended asconf ADD_IP_ADDRESS\n");
}
#endif
} else {
TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
if (type == SCTP_DEL_IP_ADDRESS) {
printf("asconf_queue_add_sa: inserted asconf DEL_IP_ADDRESS\n");
} else {
printf("asconf_queue_add_sa: inserted asconf SET_PRIM_ADDR\n");
}
}
#endif
}
return (0);
}
static struct sctp_asconf_addr *
sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
{
struct sctp_asconf_addr *aa;
TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
if (aa->ap.aph.correlation_id == correlation_id &&
aa->sent == 1) {
return (aa);
}
}
return (NULL);
}
static void
sctp_asconf_process_error(struct sctp_tcb *stcb,
struct sctp_asconf_paramhdr *aph)
{
struct sctp_error_cause *eh;
struct sctp_paramhdr *ph;
uint16_t param_type;
uint16_t error_code;
eh = (struct sctp_error_cause *)(aph + 1);
ph = (struct sctp_paramhdr *)(eh + 1);
if (htons(eh->length) + sizeof(struct sctp_error_cause) >
htons(aph->ph.param_length)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_process_error: cause element too long\n");
}
#endif
return;
}
if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
htons(eh->length)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("asconf_process_error: included TLV too long\n");
}
#endif
return;
}
error_code = ntohs(eh->code);
param_type = ntohs(aph->ph.param_type);
switch (error_code) {
case SCTP_ERROR_RESOURCE_SHORTAGE:
break;
default:
switch (param_type) {
case SCTP_ADD_IP_ADDRESS:
case SCTP_DEL_IP_ADDRESS:
stcb->asoc.peer_supports_asconf = 0;
break;
case SCTP_SET_PRIM_ADDR:
stcb->asoc.peer_supports_asconf_setprim = 0;
break;
default:
break;
}
}
}
static void
sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
struct sctp_asconf_addr *aparam, uint32_t flag)
{
uint16_t param_type;
param_type = aparam->ap.aph.ph.param_type;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_param_ack: handling asconf parameter type=%xh\n", param_type);
}
#endif
switch (param_type) {
case SCTP_ADD_IP_ADDRESS:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_param_ack: added IP address\n");
}
#endif
sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
break;
case SCTP_DEL_IP_ADDRESS:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_param_ack: deleted IP address\n");
}
#endif
break;
case SCTP_SET_PRIM_ADDR:
if (flag == 0)
stcb->asoc.peer_supports_asconf_setprim = 0;
break;
default:
break;
}
TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
free(aparam, M_PCB);
}
static void
sctp_asconf_ack_clear(struct sctp_tcb *stcb)
{
stcb->asoc.peer_supports_asconf = 0;
stcb->asoc.peer_supports_asconf_setprim = 0;
}
void
sctp_handle_asconf_ack(struct mbuf *m, int offset,
struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct sctp_association *asoc;
uint32_t serial_num;
uint16_t ack_length;
struct sctp_asconf_paramhdr *aph;
struct sctp_asconf_addr *aa, *aa_next;
uint32_t last_error_id = 0;
uint32_t id;
struct sctp_asconf_addr *ap;
static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf_ack: chunk too small = %xh\n",
ntohs(cp->ch.chunk_length));
}
#endif
return;
}
asoc = &stcb->asoc;
serial_num = ntohl(cp->serial_number);
if (serial_num == (asoc->asconf_seq_out + 1)) {
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_ERROR_ILLEGAL_ASCONF_ACK, NULL);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
}
#endif
return;
}
if (serial_num != asoc->asconf_seq_out) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n", serial_num, asoc->asconf_seq_out);
}
#endif
return;
}
sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
ack_length = ntohs(cp->ch.chunk_length) -
sizeof(struct sctp_asconf_ack_chunk);
offset += sizeof(struct sctp_asconf_ack_chunk);
while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
unsigned int param_length, param_type;
aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
sizeof(struct sctp_asconf_paramhdr), aparam_buf);
if (aph == NULL) {
sctp_asconf_ack_clear(stcb);
return;
}
param_type = ntohs(aph->ph.param_type);
param_length = ntohs(aph->ph.param_length);
if (param_length > ack_length) {
sctp_asconf_ack_clear(stcb);
return;
}
if (param_length < sizeof(struct sctp_paramhdr)) {
sctp_asconf_ack_clear(stcb);
return;
}
if (param_length > sizeof(aparam_buf)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("param length (%u) larger than buffer size!\n", param_length);
}
#endif
sctp_asconf_ack_clear(stcb);
return;
}
aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
if (aph == NULL) {
sctp_asconf_ack_clear(stcb);
return;
}
id = aph->correlation_id;
switch (param_type) {
case SCTP_ERROR_CAUSE_IND:
last_error_id = id;
ap = sctp_asconf_find_param(stcb, id);
if (ap == NULL) {
break;
}
sctp_asconf_process_param_ack(stcb, ap, 0);
sctp_asconf_process_error(stcb, aph);
break;
case SCTP_SUCCESS_REPORT:
ap = sctp_asconf_find_param(stcb, id);
if (ap == NULL) {
break;
}
sctp_asconf_process_param_ack(stcb, ap, 1);
break;
default:
break;
}
ack_length -= SCTP_SIZE32(param_length);
if (ack_length <= 0) {
break;
}
offset += SCTP_SIZE32(param_length);
}
if (last_error_id == 0)
last_error_id--;
for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
aa = aa_next) {
aa_next = TAILQ_NEXT(aa, next);
if (aa->sent == 1) {
if (aa->ap.aph.correlation_id < last_error_id)
sctp_asconf_process_param_ack(stcb, aa,
SCTP_SUCCESS_REPORT);
else
sctp_asconf_process_param_ack(stcb, aa,
SCTP_ERROR_CAUSE_IND);
} else {
break;
}
}
asoc->asconf_seq_out++;
sctp_toss_old_asconf(stcb);
asoc->asconf_sent = 0;
if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
stcb, net);
}
}
static uint32_t
sctp_is_desired_interface_type(struct ifaddr *ifa)
{
int result;
switch (ifa->ifa_ifp->if_type) {
case IFT_ETHER:
case IFT_ISO88023:
case IFT_ISO88025:
case IFT_STARLAN:
case IFT_P10:
case IFT_P80:
case IFT_HY:
case IFT_FDDI:
case IFT_PPP:
case IFT_XETHER:
case IFT_SLIP:
case IFT_GIF:
case IFT_IPSEC:
result = 1;
break;
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("ignoring interface type = %u\n",
ifa->ifa_ifp->if_type);
}
#endif
result = 0;
}
return (result);
}
static uint32_t
sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
{
struct sockaddr_in6 *sin6;
const struct sockaddr_in6 *net6;
struct sctp_nets *net;
if (sa->sa_family != AF_INET6) {
return (0);
}
sin6 = (struct sockaddr_in6 *)sa;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
return (0);
}
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if ((rtcache_getdst(&net->ro))->sa_family !=
AF_INET6)
continue;
net6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
continue;
if (sctp_is_same_scope(sin6, net6)) {
return (1);
}
}
return (0);
}
static void
sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
struct ifaddr *ifa, uint16_t type)
{
int status;
#ifdef SCTP_DEBUG
char buf[128];
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
(inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
return;
}
if (ifa->ifa_addr->sa_family != AF_INET6 &&
ifa->ifa_addr->sa_family != AF_INET) {
return;
}
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
return;
ifa6 = (struct in6_ifaddr *)ifa;
if (IFA6_IS_DEPRECATED(ifa6) ||
(ifa6->ia6_flags &
(IN6_IFF_DETACHED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
return;
}
}
if (type == SCTP_ADD_IP_ADDRESS) {
if (sctp_is_scopeid_in_nets(stcb, ifa->ifa_addr) == 0) {
sctp_add_local_addr_assoc(stcb, ifa);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: added to pending list ");
sctp_print_address(ifa->ifa_addr);
}
#endif
}
}
if (ifa->ifa_addr->sa_family == AF_INET6) {
#ifdef SCTP_DEBUG
char ip6buf[INET6_ADDRSTRLEN];
#endif
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
#ifdef SCTP_DEBUG
strlcpy(buf, IN6_PRINT(ip6buf, &sin6->sin6_addr), sizeof(buf));
#endif
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: unspecified IPv6 addr\n");
}
#endif
return;
}
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
if (stcb->asoc.local_scope == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: skipping link local IPv6 addr: %s\n", buf);
}
#endif
return;
}
if (sctp_is_scopeid_in_nets(stcb, ifa->ifa_addr) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: skipping link local IPv6 addr: %s, wrong scope_id\n", buf);
}
#endif
return;
}
}
if (stcb->asoc.site_scope == 0 &&
IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: skipping site local IPv6 addr: %s\n", buf);
}
#endif
return;
}
} else if (ifa->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
struct in6pcb *inp6;
inp6 = (struct in6pcb *)&inp->ip_inp.inp;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(inp6->in6p_flags & IN6P_IPV6_V6ONLY)
)
return;
sin = (struct sockaddr_in *)ifa->ifa_addr;
#ifdef SCTP_DEBUG
strlcpy(buf, inet_ntoa(sin->sin_addr), sizeof(buf));
#endif
if (sin->sin_addr.s_addr == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: unspecified IPv4 addr\n");
}
#endif
return;
}
if (stcb->asoc.ipv4_local_scope == 0 &&
IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: skipping private IPv4 addr: %s\n", buf);
}
#endif
return;
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_assoc: not AF_INET or AF_INET6\n");
}
#endif
return;
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
if (stcb->asoc.peer_supports_asconf) {
status = sctp_asconf_queue_add(stcb, ifa, type);
if (status == 0 &&
SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
stcb, stcb->asoc.primary_destination);
}
}
} else {
#if 0
if (type == SCTP_DEL_IP_ADDRESS) {
sctp_add_local_addr_assoc(stcb, ifa);
}
#endif
}
}
static void
sctp_addr_mgmt_ep(struct sctp_inpcb *inp, struct ifaddr *ifa, uint16_t type)
{
struct sctp_tcb *stcb;
int s;
SCTP_INP_WLOCK(inp);
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
SCTP_INP_WUNLOCK(inp);
return;
}
ifa6 = (struct in6_ifaddr *)ifa;
if (IFA6_IS_DEPRECATED(ifa6) ||
(ifa6->ia6_flags &
(IN6_IFF_DETACHED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
SCTP_INP_WUNLOCK(inp);
return;
}
} else if (ifa->ifa_addr->sa_family == AF_INET) {
struct in6pcb *inp6;
inp6 = (struct in6pcb *)&inp->ip_inp.inp;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(inp6->in6p_flags & IN6P_IPV6_V6ONLY)
) {
SCTP_INP_WUNLOCK(inp);
return;
}
} else {
SCTP_INP_WUNLOCK(inp);
return;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
if (type == SCTP_DEL_IP_ADDRESS) {
sctp_del_local_addr_ep(inp, ifa);
}
SCTP_INP_WUNLOCK(inp);
return;
} else {
if (type == SCTP_ADD_IP_ADDRESS) {
sctp_add_local_addr_ep(inp, ifa);
} else {
sctp_del_local_addr_ep(inp, ifa);
}
}
}
s = splsoftnet();
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_addr_mgmt_assoc(inp, stcb, ifa, type);
SCTP_TCB_UNLOCK(stcb);
}
splx(s);
SCTP_INP_WUNLOCK(inp);
}
static void
sctp_addr_mgmt_restrict_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
{
struct sctp_tcb *stcb;
int s;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
return;
}
s = splsoftnet();
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_add_local_addr_assoc(stcb, ifa);
SCTP_TCB_UNLOCK(stcb);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("restrict_ep: added addr to unusable list\n");
}
#endif
}
splx(s);
SCTP_INP_RUNLOCK(inp);
}
static void
sctp_addr_mgmt(struct ifaddr *ifa, uint16_t type) {
struct sockaddr *sa;
struct sctp_inpcb *inp;
if (!sctp_is_desired_interface_type(ifa)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("sctp_addr_mgmt: ignoring this interface\n");
}
#endif
return;
}
sa = ifa->ifa_addr;
if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
return;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
if (type == SCTP_ADD_IP_ADDRESS)
printf("sctp_addr_mgmt: kernel adds ");
else
printf("sctp_addr_mgmt: kernel deletes ");
sctp_print_address(sa);
}
#endif
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF) {
sctp_addr_mgmt_ep(inp, ifa, type);
} else {
if (type == SCTP_DEL_IP_ADDRESS)
return;
sctp_addr_mgmt_restrict_ep(inp, ifa);
}
}
}
void
sctp_add_ip_address(struct ifaddr *ifa)
{
sctp_addr_mgmt(ifa, SCTP_ADD_IP_ADDRESS);
}
void
sctp_delete_ip_address(struct ifaddr *ifa)
{
struct sctp_inpcb *inp;
sctp_addr_mgmt(ifa, SCTP_DEL_IP_ADDRESS);
if (!sctp_is_desired_interface_type(ifa)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("sctp_delete_ip_address: ignoring this interface\n");
}
#endif
return;
}
SCTP_INP_INFO_RLOCK();
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
struct sctp_tcb *stcb;
struct sctp_laddr *laddr, *laddr_next;
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
struct sctp_nets *net;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
rtcache_free(&net->ro);
}
laddr = LIST_FIRST(&stcb->asoc.sctp_local_addr_list);
while (laddr != NULL) {
laddr_next = LIST_NEXT(laddr, sctp_nxt_addr);
if (laddr->ifa == ifa) {
sctp_remove_laddr(laddr);
}
laddr = laddr_next;
}
}
laddr = LIST_FIRST(&inp->sctp_addr_list);
while (laddr != NULL) {
laddr_next = LIST_NEXT(laddr, sctp_nxt_addr);
if (laddr->ifa == ifa) {
sctp_remove_laddr(laddr);
}
laddr = laddr_next;
}
SCTP_INP_RUNLOCK(inp);
}
SCTP_INP_INFO_RUNLOCK();
}
int32_t
sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
{
if (!sctp_asconf_queue_add_sa(stcb, sa, SCTP_SET_PRIM_ADDR)) {
if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
stcb->sctp_ep, stcb,
stcb->asoc.primary_destination);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("set_primary_ip_address_sa: queued on tcb=%p, ",
stcb);
sctp_print_address(sa);
}
#endif
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
stcb);
sctp_print_address(sa);
}
#endif
return (-1);
}
return (0);
}
void
sctp_set_primary_ip_address(struct ifaddr *ifa)
{
struct sctp_inpcb *inp;
if (!sctp_is_desired_interface_type(ifa)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("set_primary_ip_address: ignoring this interface\n");
}
#endif
return;
}
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
struct sctp_tcb *stcb;
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (!sctp_asconf_queue_add(stcb, ifa,
SCTP_SET_PRIM_ADDR)) {
if (SCTP_GET_STATE(&stcb->asoc) ==
SCTP_STATE_OPEN) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
stcb->sctp_ep, stcb,
stcb->asoc.primary_destination);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("set_primary_ip_address: queued on stcb=%p, ",
stcb);
sctp_print_address(ifa->ifa_addr);
}
#endif
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("set_primary_ip_address: failed to add to queue, ");
sctp_print_address(ifa->ifa_addr);
}
#endif
}
}
}
}
static struct sockaddr *
sctp_find_valid_localaddr(struct sctp_tcb *stcb)
{
struct ifnet *ifn;
struct ifaddr *ifa;
int s;
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if (stcb->asoc.loopback_scope == 0 && ifn->if_type == IFT_LOOP) {
continue;
}
IFADDR_READER_FOREACH(ifa, ifn) {
if (ifa->ifa_addr->sa_family == AF_INET &&
stcb->asoc.ipv4_addr_legal) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (sin->sin_addr.s_addr == 0) {
continue;
}
if (stcb->asoc.ipv4_local_scope == 0 &&
IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
continue;
if (sctp_is_addr_restricted(stcb,
ifa->ifa_addr))
continue;
pserialize_read_exit(s);
return (ifa->ifa_addr);
} else if (ifa->ifa_addr->sa_family == AF_INET6 &&
stcb->asoc.ipv6_addr_legal) {
struct sockaddr_in6 *sin6;
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (IFA6_IS_DEPRECATED(ifa6) ||
(ifa6->ia6_flags & (IN6_IFF_DETACHED |
IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)))
continue;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
continue;
}
if (stcb->asoc.local_scope == 0 &&
IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
continue;
if (stcb->asoc.site_scope == 0 &&
IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
continue;
pserialize_read_exit(s);
return (ifa->ifa_addr);
}
}
}
pserialize_read_exit(s);
return (NULL);
}
static struct sockaddr *
sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
{
struct sctp_laddr *laddr;
LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("find_valid_localaddr_ep: laddr error\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("find_valid_localaddr_ep: laddr->ifa error\n");
}
#endif
continue;
}
if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr))
continue;
return (laddr->ifa->ifa_addr);
}
return (NULL);
}
struct mbuf *
sctp_compose_asconf(struct sctp_tcb *stcb)
{
struct mbuf *m_asconf, *m_asconf_chk;
struct sctp_asconf_addr *aa;
struct sctp_asconf_chunk *acp;
struct sctp_asconf_paramhdr *aph;
struct sctp_asconf_addr_param *aap;
uint32_t p_length;
uint32_t correlation_id = 1;
vaddr_t ptr, lookup_ptr;
uint8_t lookup_used = 0;
if (TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
return (NULL);
}
m_asconf_chk = NULL;
MGETHDR(m_asconf_chk, M_DONTWAIT, MT_DATA);
if (m_asconf_chk == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
printf("compose_asconf: couldn't get chunk mbuf!\n");
#endif
return (NULL);
}
m_asconf = NULL;
MGETHDR(m_asconf, M_DONTWAIT, MT_HEADER);
if (m_asconf == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
printf("compose_asconf: couldn't get mbuf!\n");
#endif
sctp_m_freem(m_asconf_chk);
return (NULL);
}
MCLGET(m_asconf, M_DONTWAIT);
if ((m_asconf->m_flags & M_EXT) != M_EXT) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
printf("compose_asconf: couldn't get cluster!\n");
#endif
sctp_m_freem(m_asconf_chk);
sctp_m_freem(m_asconf);
return (NULL);
}
m_asconf_chk->m_len = sizeof(struct sctp_asconf_chunk);
m_asconf->m_len = 0;
acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
memset(acp, 0, sizeof(struct sctp_asconf_chunk));
lookup_ptr = (vaddr_t)(acp + 1);
ptr = mtod(m_asconf, vaddr_t);
acp->ch.chunk_type = SCTP_ASCONF;
acp->ch.chunk_flags = 0;
acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
if (m_asconf->m_len + p_length > stcb->asoc.smallest_mtu) {
break;
}
aa->ap.aph.correlation_id = correlation_id++;
if (lookup_used == 0 &&
aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
struct sctp_ipv6addr_param *lookup;
uint16_t p_size, addr_size;
lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
lookup->ph.param_type =
htons(aa->ap.addrp.ph.param_type);
if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
p_size = sizeof(struct sctp_ipv6addr_param);
addr_size = sizeof(struct in6_addr);
} else {
p_size = sizeof(struct sctp_ipv4addr_param);
addr_size = sizeof(struct in_addr);
}
lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
m_asconf_chk->m_len += SCTP_SIZE32(p_size);
lookup_used = 1;
}
memcpy((void *)ptr, &aa->ap, p_length);
aph = (struct sctp_asconf_paramhdr *) ptr;
aap = (struct sctp_asconf_addr_param *) ptr;
aph->ph.param_type = htons(aph->ph.param_type);
aph->ph.param_length = htons(aph->ph.param_length);
aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
m_asconf->m_len += SCTP_SIZE32(p_length);
ptr += SCTP_SIZE32(p_length);
aa->sent = 1;
}
if (lookup_used == 0) {
struct sctp_ipv6addr_param *lookup;
uint16_t p_size, addr_size;
struct sockaddr *found_addr;
vaddr_t addr_ptr;
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
found_addr = sctp_find_valid_localaddr(stcb);
else
found_addr = sctp_find_valid_localaddr_ep(stcb);
lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
if (found_addr != NULL) {
if (found_addr->sa_family == AF_INET6) {
lookup->ph.param_type =
htons(SCTP_IPV6_ADDRESS);
p_size = sizeof(struct sctp_ipv6addr_param);
addr_size = sizeof(struct in6_addr);
addr_ptr = (vaddr_t)&((struct sockaddr_in6 *)
found_addr)->sin6_addr;
} else {
lookup->ph.param_type =
htons(SCTP_IPV4_ADDRESS);
p_size = sizeof(struct sctp_ipv4addr_param);
addr_size = sizeof(struct in_addr);
addr_ptr = (vaddr_t)&((struct sockaddr_in *)
found_addr)->sin_addr;
}
lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
memcpy(lookup->addr, (void *)addr_ptr, addr_size);
m_asconf_chk->m_len += SCTP_SIZE32(p_size);
lookup_used = 1;
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
printf("compose_asconf: no lookup addr!\n");
#endif
lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
memset(lookup->addr, 0, sizeof(struct in_addr));
m_asconf_chk->m_len += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
lookup_used = 1;
}
}
m_asconf_chk->m_next = m_asconf;
m_asconf_chk->m_pkthdr.len = m_asconf_chk->m_len + m_asconf->m_len;
acp->ch.chunk_length = ntohs(m_asconf_chk->m_pkthdr.len);
stcb->asoc.asconf_sent++;
return (m_asconf_chk);
}
static void
sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
unsigned int offset, unsigned int length)
{
struct sctp_paramhdr tmp_param, *ph;
uint16_t plen, ptype;
struct sctp_ipv6addr_param addr_store;
struct sockaddr_in6 sin6;
struct sockaddr_in sin;
struct sockaddr *sa;
struct ifaddr *ifa;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("processing init-ack addresses\n");
}
#endif
length += offset;
if ((offset + sizeof(struct sctp_paramhdr)) > length) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_initack_addrs: invalid offset?\n");
}
#endif
return;
}
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(sin6);
sin6.sin6_port = stcb->rport;
memset(&sin, 0, sizeof(sin));
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = stcb->rport;
ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
while (ph != NULL) {
ptype = ntohs(ph->param_type);
plen = ntohs(ph->param_length);
if (ptype == SCTP_IPV6_ADDRESS) {
struct sctp_ipv6addr_param *a6p;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("process_initack_addrs: checking IPv6 param\n");
}
#endif
a6p = (struct sctp_ipv6addr_param *)
sctp_m_getptr(m, offset,
sizeof(struct sctp_ipv6addr_param),
(uint8_t *)&addr_store);
if (plen != sizeof(struct sctp_ipv6addr_param) ||
a6p == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_initack_addrs: invalid IPv6 param length\n");
}
#endif
return;
}
memcpy(&sin6.sin6_addr, a6p->addr,
sizeof(struct in6_addr));
sa = (struct sockaddr *)&sin6;
} else if (ptype == SCTP_IPV4_ADDRESS) {
struct sctp_ipv4addr_param *a4p;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("process_initack_addrs: checking IPv4 param\n");
}
#endif
a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_ipv4addr_param), (uint8_t *)&addr_store);
if (plen != sizeof(struct sctp_ipv4addr_param) ||
a4p == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("process_initack_addrs: invalid IPv4 param length\n");
}
#endif
return;
}
sin.sin_addr.s_addr = a4p->addr;
sa = (struct sockaddr *)&sin;
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("process_initack_addrs: skipping param type=%xh\n", ptype);
}
#endif
goto next_addr;
}
ifa = sctp_find_ifa_by_addr(sa);
if (ifa == NULL) {
int status;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
stcb->asoc.peer_supports_asconf) {
status = sctp_asconf_queue_add_sa(stcb, sa,
SCTP_DEL_IP_ADDRESS);
if (status == 0 &&
SCTP_GET_STATE(&stcb->asoc) ==
SCTP_STATE_OPEN) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
stcb->sctp_ep, stcb,
stcb->asoc.primary_destination);
}
}
} else {
if ((stcb->sctp_ep->sctp_flags &
SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
(stcb->sctp_ep->sctp_flags &
SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("process_initack_addrs: adding local addr to asoc\n");
}
#endif
sctp_add_local_addr_assoc(stcb, ifa);
}
}
next_addr:
offset += SCTP_SIZE32(plen);
if ((offset + sizeof(struct sctp_paramhdr)) > length)
return;
ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
}
}
static uint32_t
sctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, unsigned int offset,
unsigned int length, struct sockaddr *sa)
{
struct sctp_paramhdr tmp_param, *ph;
uint16_t plen, ptype;
struct sctp_ipv6addr_param addr_store;
struct sockaddr_in *sin;
struct sctp_ipv4addr_param *a4p;
#ifdef INET6
struct sockaddr_in6 *sin6, sin6_tmp;
struct sctp_ipv6addr_param *a6p;
#endif
if (
#ifdef INET6
(sa->sa_family != AF_INET6) &&
#endif
(sa->sa_family != AF_INET))
return (0);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("find_initack_addr: starting search for ");
sctp_print_address(sa);
}
#endif
length += offset;
if ((offset + sizeof(struct sctp_paramhdr)) > length) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("find_initack_addr: invalid offset?\n");
}
#endif
return (0);
}
ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
while (ph != NULL) {
ptype = ntohs(ph->param_type);
plen = ntohs(ph->param_length);
#ifdef INET6
if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: checking IPv6 param\n");
}
#endif
a6p = (struct sctp_ipv6addr_param *)
sctp_m_getptr(m, offset,
sizeof(struct sctp_ipv6addr_param),
(uint8_t *)&addr_store);
if (plen != sizeof(struct sctp_ipv6addr_param) ||
ph == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: invalid IPv6 param length\n");
}
#endif
return (0);
}
sin6 = (struct sockaddr_in6 *)sa;
if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
memcpy(&sin6_tmp, sin6,
sizeof(struct sockaddr_in6));
sin6 = &sin6_tmp;
in6_clearscope(&sin6->sin6_addr);
}
if (memcmp(&sin6->sin6_addr, a6p->addr,
sizeof(struct in6_addr)) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: found IPv6 addr\n");
}
#endif
return (1);
}
} else
#endif
if (ptype == SCTP_IPV4_ADDRESS &&
sa->sa_family == AF_INET) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: checking IPv4 param\n");
}
#endif
a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
offset, sizeof(struct sctp_ipv4addr_param),
(uint8_t *)&addr_store);
if (plen != sizeof(struct sctp_ipv4addr_param) ||
ph == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: invalid IPv4 param length\n");
}
#endif
return (0);
}
sin = (struct sockaddr_in *)sa;
if (sin->sin_addr.s_addr == a4p->addr) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: found IPv4 addr\n");
}
#endif
return (1);
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("addr_in_initack: skipping param type=%xh\n", ptype);
}
#endif
}
offset += SCTP_SIZE32(plen);
if (offset + sizeof(struct sctp_paramhdr) > length)
return (0);
ph = (struct sctp_paramhdr *)
sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
(uint8_t *)&tmp_param);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_in_initack: not found!\n");
}
#endif
return (0);
}
static void
sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
int length, struct sockaddr *init_addr)
{
struct sctp_laddr *laddr;
LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("check_addr_list_ep: laddr->ifa is NULL");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
}
#endif
continue;
}
if (sctp_cmpaddr(laddr->ifa->ifa_addr, init_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("check_address_list_all: skipping ");
sctp_print_address(laddr->ifa->ifa_addr);
}
#endif
continue;
}
if (!sctp_addr_in_initack(stcb, m, offset, length,
laddr->ifa->ifa_addr)) {
sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
SCTP_ADD_IP_ADDRESS);
}
}
}
static void
sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
int length, struct sockaddr *init_addr, uint16_t local_scope,
uint16_t site_scope, uint16_t ipv4_scope, uint16_t loopback_scope)
{
struct ifnet *ifn;
struct ifaddr *ifa;
int s;
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if (loopback_scope == 0 && ifn->if_type == IFT_LOOP) {
continue;
}
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_cmpaddr(ifa->ifa_addr, init_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
printf("check_address_list_all: skipping ");
sctp_print_address(ifa->ifa_addr);
}
#endif
continue;
}
if (!sctp_addr_in_initack(stcb, m, offset, length,
ifa->ifa_addr)) {
sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
ifa, SCTP_ADD_IP_ADDRESS);
}
}
}
pserialize_read_exit(s);
}
void
sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
int length, struct sockaddr *init_addr, uint16_t local_scope,
uint16_t site_scope, uint16_t ipv4_scope, uint16_t loopback_scope)
{
sctp_process_initack_addresses(stcb, m, offset, length);
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
sctp_check_address_list_all(stcb, m, offset, length, init_addr,
local_scope, site_scope, ipv4_scope, loopback_scope);
} else {
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
sctp_check_address_list_ep(stcb, m, offset, length,
init_addr);
}
}
}
uint32_t
sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa, uint16_t type)
{
struct ifaddr *ifa;
if (sa->sa_len == 0)
return (EINVAL);
ifa = sctp_find_ifa_by_addr(sa);
if (ifa != NULL) {
#ifdef INET6
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (IFA6_IS_DEPRECATED(ifa6) ||
(ifa6->ia6_flags & (IN6_IFF_DETACHED |
IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
return (EINVAL);
}
}
#endif
sctp_addr_mgmt_ep(inp, ifa, type);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
printf("addr_mgmt_ep_sa: got invalid address!\n");
}
#endif
return (EADDRNOTAVAIL);
}
return (0);
}