#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sctp_pcb.c,v 1.28 2025/12/10 21:33:03 andvar Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_sctp.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/callout.h>
#include <machine/limits.h>
#include <machine/cpu.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/scope6_var.h>
#include <netinet6/in6_pcb.h>
#endif
#ifdef IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#endif
#include <netinet/sctp_var.h>
#include <netinet/sctp_pcb.h>
#include <netinet/sctputil.h>
#include <netinet/sctp.h>
#include <netinet/sctp_header.h>
#include <netinet/sctp_asconf.h>
#include <netinet/sctp_output.h>
#include <netinet/sctp_timer.h>
#ifndef SCTP_PCBHASHSIZE
#define SCTP_PCBHASHSIZE 256
#endif
#ifdef SCTP_DEBUG
u_int32_t sctp_debug_on = SCTP_DEBUG_ALL;
#endif
u_int32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
int sctp_pcbtblsize = SCTP_PCBHASHSIZE;
struct sctp_epinfo sctppcbinfo;
int
SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b)
{
struct in6_addr tmp_a, tmp_b;
tmp_a = *a;
tmp_b = *b;
in6_clearscope(&tmp_a);
in6_clearscope(&tmp_b);
return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
}
#if defined(__FreeBSD__) && __FreeBSD_version > 500000
#ifndef xyzzy
void sctp_validate_no_locks(void);
void
SCTP_INP_RLOCK(struct sctp_inpcb *inp)
{
struct sctp_tcb *stcb;
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (mtx_owned(&(stcb)->tcb_mtx))
panic("I own TCB lock?");
}
if (mtx_owned(&(inp)->inp_mtx))
panic("INP Recursive Lock-R");
mtx_lock(&(inp)->inp_mtx);
}
void
SCTP_INP_WLOCK(struct sctp_inpcb *inp)
{
SCTP_INP_RLOCK(inp);
}
void
SCTP_INP_INFO_RLOCK()
{
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
if (mtx_owned(&(inp)->inp_mtx))
panic("info-lock and own inp lock?");
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (mtx_owned(&(stcb)->tcb_mtx))
panic("Info lock and own a tcb lock?");
}
}
if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
panic("INP INFO Recursive Lock-R");
mtx_lock(&sctppcbinfo.ipi_ep_mtx);
}
void
SCTP_INP_INFO_WLOCK()
{
SCTP_INP_INFO_RLOCK();
}
void sctp_validate_no_locks()
{
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
panic("INP INFO lock is owned?");
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
if (mtx_owned(&(inp)->inp_mtx))
panic("You own an INP lock?");
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (mtx_owned(&(stcb)->tcb_mtx))
panic("You own a TCB lock?");
}
}
}
#endif
#endif
void
sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
{
SCTP_INP_INFO_RLOCK();
spcb->ep_count = sctppcbinfo.ipi_count_ep;
spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
spcb->chk_count = sctppcbinfo.ipi_count_chunk;
spcb->sockq_count = sctppcbinfo.ipi_count_sockq;
spcb->mbuf_track = sctppcbinfo.mbuf_track;
SCTP_INP_INFO_RUNLOCK();
}
static struct sctp_tcb *
sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
struct sockaddr *to, struct sctp_nets **netp)
{
uint16_t lport, rport;
struct sctppcbhead *ephead;
struct sctp_inpcb *inp;
struct sctp_laddr *laddr;
struct sctp_tcb *stcb;
struct sctp_nets *net;
if ((to == NULL) || (from == NULL)) {
return (NULL);
}
if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
lport = ((struct sockaddr_in *)to)->sin_port;
rport = ((struct sockaddr_in *)from)->sin_port;
} else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
lport = ((struct sockaddr_in6 *)to)->sin6_port;
rport = ((struct sockaddr_in6 *)from)->sin6_port;
} else {
return NULL;
}
ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
(lport + rport), sctppcbinfo.hashtcpmark)];
LIST_FOREACH(inp, ephead, sctp_hash) {
if (lport != inp->sctp_lport) {
continue;
}
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
int match = 0;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("An ounce of prevention is worth a pound of cure\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("ifa with a NULL address\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr->sa_family ==
to->sa_family) {
struct sockaddr_in *intf_addr, *sin;
intf_addr = (struct sockaddr_in *)
laddr->ifa->ifa_addr;
sin = (struct sockaddr_in *)to;
if (from->sa_family == AF_INET) {
if (sin->sin_addr.s_addr ==
intf_addr->sin_addr.s_addr) {
match = 1;
SCTP_INP_RUNLOCK(inp);
break;
}
} else {
struct sockaddr_in6 *intf_addr6;
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)
to;
intf_addr6 = (struct sockaddr_in6 *)
laddr->ifa->ifa_addr;
if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
&intf_addr6->sin6_addr)) {
match = 1;
SCTP_INP_RUNLOCK(inp);
break;
}
}
}
}
if (match == 0) {
SCTP_INP_RUNLOCK(inp);
continue;
}
}
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
SCTP_INP_RUNLOCK(inp);
continue;
}
SCTP_TCB_LOCK(stcb);
if (stcb->rport != rport) {
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
continue;
}
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (sctp_cmpaddr(from, rtcache_getdst(&net->ro))) {
if (netp != NULL) {
*netp = net;
}
*inp_p = inp;
SCTP_INP_RUNLOCK(inp);
return (stcb);
}
}
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
}
return (NULL);
}
struct sctp_tcb *
sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
{
struct sctp_tcb *stcb;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct sockaddr_storage local_store, remote_store;
struct ip *iph;
struct sctp_paramhdr parm_buf, *phdr;
int ptype;
memset(&local_store, 0, sizeof(local_store));
memset(&remote_store, 0, sizeof(remote_store));
iph = mtod(m, struct ip *);
if (iph->ip_v == IPVERSION) {
sin = (struct sockaddr_in *)&local_store;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_port = sh->dest_port;
sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6;
ip6 = mtod(m, struct ip6_hdr *);
sin6 = (struct sockaddr_in6 *)&local_store;
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_port = sh->dest_port;
sin6->sin6_addr = ip6->ip6_dst;
} else {
return NULL;
}
phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
&parm_buf, sizeof(struct sctp_paramhdr));
if (phdr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: failed to get asconf lookup addr\n");
}
#endif
return NULL;
}
ptype = (int)((u_int)ntohs(phdr->param_type));
if (ptype == SCTP_IPV6_ADDRESS) {
struct sctp_ipv6addr_param *p6, p6_buf;
if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
return NULL;
}
p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
offset + sizeof(struct sctp_asconf_chunk),
&p6_buf.ph, sizeof(*p6));
if (p6 == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: failed to get asconf v6 lookup addr\n");
}
#endif
return (NULL);
}
sin6 = (struct sockaddr_in6 *)&remote_store;
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_port = sh->src_port;
memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
} else if (ptype == SCTP_IPV4_ADDRESS) {
struct sctp_ipv4addr_param *p4, p4_buf;
if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
return NULL;
}
p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
offset + sizeof(struct sctp_asconf_chunk),
&p4_buf.ph, sizeof(*p4));
if (p4 == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: failed to get asconf v4 lookup addr\n");
}
#endif
return (NULL);
}
sin = (struct sockaddr_in *)&remote_store;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_port = sh->src_port;
memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
} else {
return NULL;
}
stcb = sctp_findassociation_ep_addr(inp_p,
(struct sockaddr *)&remote_store, netp,
(struct sockaddr *)&local_store, NULL);
return (stcb);
}
struct sctp_tcb *
sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
{
struct sctpasochead *head;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
struct sctp_nets *net;
uint16_t rport;
inp = *inp_p;
if (remote->sa_family == AF_INET) {
rport = (((struct sockaddr_in *)remote)->sin_port);
} else if (remote->sa_family == AF_INET6) {
rport = (((struct sockaddr_in6 *)remote)->sin6_port);
} else {
return (NULL);
}
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
SCTP_INP_INFO_RLOCK();
if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
stcb = sctp_tcb_special_locate(inp_p, remote, local,
netp);
if ((stcb != NULL) && (locked_tcb == NULL)){
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
if (locked_tcb != NULL) {
SCTP_INP_RLOCK(locked_tcb->sctp_ep);
SCTP_TCB_LOCK(locked_tcb);
SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
}
SCTP_INP_INFO_RUNLOCK();
return (stcb);
} else {
SCTP_INP_WLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
goto null_return;
}
SCTP_TCB_LOCK(stcb);
if (stcb->rport != rport) {
SCTP_TCB_UNLOCK(stcb);
goto null_return;
}
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
if (netp != NULL) {
*netp = net;
}
if (locked_tcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_RUNLOCK();
return (stcb);
}
}
SCTP_TCB_UNLOCK(stcb);
}
} else {
SCTP_INP_WLOCK(inp);
head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
inp->sctp_hashmark)];
if (head == NULL) {
goto null_return;
}
LIST_FOREACH(stcb, head, sctp_tcbhash) {
if (stcb->rport != rport) {
continue;
}
SCTP_TCB_LOCK(stcb);
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
if (netp != NULL) {
*netp = net;
}
if (locked_tcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_RUNLOCK();
return (stcb);
}
}
SCTP_TCB_UNLOCK(stcb);
}
}
null_return:
if (locked_tcb){
if (locked_tcb->sctp_ep != inp) {
SCTP_INP_RLOCK(locked_tcb->sctp_ep);
SCTP_TCB_LOCK(locked_tcb);
SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
} else {
SCTP_TCB_LOCK(locked_tcb);
}
}
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_RUNLOCK();
return (NULL);
}
struct sctp_tcb *
sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, vaddr_t asoc_id)
{
struct sctpasochead *head;
struct sctp_tcb *stcb;
u_int32_t vtag;
if (asoc_id == 0 || inp == NULL) {
return (NULL);
}
SCTP_INP_INFO_RLOCK();
vtag = (u_int32_t)asoc_id;
head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
sctppcbinfo.hashasocmark)];
if (head == NULL) {
SCTP_INP_INFO_RUNLOCK();
return (NULL);
}
LIST_FOREACH(stcb, head, sctp_asocs) {
SCTP_INP_RLOCK(stcb->sctp_ep);
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(stcb->sctp_ep);
if (stcb->asoc.my_vtag == vtag) {
if (inp != stcb->sctp_ep) {
sctp_pegs[SCTP_VTAG_BOGUS]++;
SCTP_TCB_UNLOCK(stcb);
continue;
}
sctp_pegs[SCTP_VTAG_EXPR]++;
SCTP_INP_INFO_RUNLOCK();
return (stcb);
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_INFO_RUNLOCK();
return (NULL);
}
static struct sctp_inpcb *
sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
uint16_t lport)
{
struct sctp_inpcb *inp;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct sctp_laddr *laddr;
if (nam->sa_family == AF_INET) {
sin = (struct sockaddr_in *)nam;
sin6 = NULL;
} else if (nam->sa_family == AF_INET6) {
sin6 = (struct sockaddr_in6 *)nam;
sin = NULL;
} else {
return (NULL);
}
if (head == NULL)
return (NULL);
LIST_FOREACH(inp, head, sctp_hash) {
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
(inp->sctp_lport == lport)) {
if ((nam->sa_family == AF_INET) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
) {
SCTP_INP_RUNLOCK(inp);
continue;
}
if (nam->sa_family == AF_INET6 &&
(inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
SCTP_INP_RUNLOCK(inp);
continue;
}
SCTP_INP_RUNLOCK(inp);
return (inp);
}
SCTP_INP_RUNLOCK(inp);
}
if ((nam->sa_family == AF_INET) &&
(sin->sin_addr.s_addr == INADDR_ANY)) {
return (NULL);
} else if ((nam->sa_family == AF_INET6) &&
(IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Ok, there is NO bound-all available for port:%x\n", ntohs(lport));
}
#endif
LIST_FOREACH(inp, head, sctp_hash) {
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
SCTP_INP_RUNLOCK(inp);
continue;
}
if (inp->sctp_lport != lport) {
SCTP_INP_RUNLOCK(inp);
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Ok, found matching local port\n");
}
#endif
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("An ounce of prevention is worth a pound of cure\n");
}
#endif
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Ok laddr->ifa:%p is possible, ",
laddr->ifa);
}
#endif
if (laddr->ifa->ifa_addr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Huh IFA as an ifa_addr=NULL, ");
}
#endif
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Ok laddr->ifa:%p is possible, ",
laddr->ifa->ifa_addr);
sctp_print_address(laddr->ifa->ifa_addr);
printf("looking for ");
sctp_print_address(nam);
}
#endif
if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
struct sockaddr_in *intf_addr;
intf_addr = (struct sockaddr_in *)
laddr->ifa->ifa_addr;
if (nam->sa_family == AF_INET) {
if (sin->sin_addr.s_addr ==
intf_addr->sin_addr.s_addr) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("YES, return ep:%p\n", inp);
}
#endif
SCTP_INP_RUNLOCK(inp);
return (inp);
}
} else if (nam->sa_family == AF_INET6) {
struct sockaddr_in6 *intf_addr6;
intf_addr6 = (struct sockaddr_in6 *)
laddr->ifa->ifa_addr;
if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
&intf_addr6->sin6_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("YES, return ep:%p\n", inp);
}
#endif
SCTP_INP_RUNLOCK(inp);
return (inp);
}
}
}
SCTP_INP_RUNLOCK(inp);
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("NO, Falls out to NULL\n");
}
#endif
return (NULL);
}
struct sctp_inpcb *
sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
{
struct sctp_inpcb *inp;
struct sctppcbhead *head;
int lport;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Looking for endpoint %d :",
ntohs(((struct sockaddr_in *)nam)->sin_port));
sctp_print_address(nam);
}
#endif
if (nam->sa_family == AF_INET) {
lport = ((struct sockaddr_in *)nam)->sin_port;
} else if (nam->sa_family == AF_INET6) {
lport = ((struct sockaddr_in6 *)nam)->sin6_port;
} else {
return (NULL);
}
if (have_lock == 0) {
SCTP_INP_INFO_RLOCK();
}
head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
sctppcbinfo.hashmark)];
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Main hash to lookup at head:%p\n", head);
}
#endif
inp = sctp_endpoint_probe(nam, head, lport);
if (inp == NULL && find_tcp_pool) {
unsigned int i;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("EP was NULL and TCP model is supported\n");
}
#endif
for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
head = &sctppcbinfo.sctp_tcpephash[i];
if (LIST_FIRST(head)) {
inp = sctp_endpoint_probe(nam, head, lport);
if (inp) {
break;
}
}
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("EP to return is %p\n", inp);
}
#endif
if (have_lock == 0) {
if (inp) {
SCTP_INP_WLOCK(inp);
SCTP_INP_INCR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
SCTP_INP_INFO_RUNLOCK();
} else {
if (inp) {
SCTP_INP_WLOCK(inp);
SCTP_INP_INCR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
}
return (inp);
}
struct sctp_tcb *
sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
{
struct sctp_inpcb *inp;
struct sctp_tcb *retval;
SCTP_INP_INFO_RLOCK();
if (find_tcp_pool) {
if (inp_p != NULL) {
retval = sctp_tcb_special_locate(inp_p, from, to, netp);
} else {
retval = sctp_tcb_special_locate(&inp, from, to, netp);
}
if (retval != NULL) {
SCTP_INP_INFO_RUNLOCK();
return (retval);
}
}
inp = sctp_pcb_findep(to, 0, 1);
if (inp_p != NULL) {
*inp_p = inp;
}
SCTP_INP_INFO_RUNLOCK();
if (inp == NULL) {
return (NULL);
}
if (inp_p != NULL) {
return (sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL));
} else {
return (sctp_findassociation_ep_addr(&inp, from, netp, to, NULL));
}
}
static struct sctp_tcb *
sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
struct sockaddr *dest)
{
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
struct sctp_paramhdr *phdr, parm_buf;
struct sctp_tcb *retval;
u_int32_t ptype, plen;
memset(&sin4, 0, sizeof(sin4));
memset(&sin6, 0, sizeof(sin6));
sin4.sin_len = sizeof(sin4);
sin4.sin_family = AF_INET;
sin4.sin_port = sh->src_port;
sin6.sin6_len = sizeof(sin6);
sin6.sin6_family = AF_INET6;
sin6.sin6_port = sh->src_port;
retval = NULL;
offset += sizeof(struct sctp_init_chunk);
phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
while (phdr != NULL) {
ptype = ntohs(phdr->param_type);
plen = ntohs(phdr->param_length);
if (plen == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("sctp_findassociation_special_addr: Impossible length in parameter\n");
}
#endif
break;
}
if (ptype == SCTP_IPV4_ADDRESS &&
plen == sizeof(struct sctp_ipv4addr_param)) {
struct sctp_ipv4addr_param ip4_parm, *p4;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&ip4_parm, plen);
if (phdr == NULL) {
return (NULL);
}
p4 = (struct sctp_ipv4addr_param *)phdr;
memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
retval = sctp_findassociation_ep_addr(inp_p,
(struct sockaddr *)&sin4, netp, dest, NULL);
if (retval != NULL) {
return (retval);
}
} else if (ptype == SCTP_IPV6_ADDRESS &&
plen == sizeof(struct sctp_ipv6addr_param)) {
struct sctp_ipv6addr_param ip6_parm, *p6;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&ip6_parm, plen);
if (phdr == NULL) {
return (NULL);
}
p6 = (struct sctp_ipv6addr_param *)phdr;
memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
retval = sctp_findassociation_ep_addr(inp_p,
(struct sockaddr *)&sin6, netp, dest, NULL);
if (retval != NULL) {
return (retval);
}
}
offset += SCTP_SIZE32(plen);
phdr = sctp_get_next_param(m, offset, &parm_buf,
sizeof(parm_buf));
}
return (NULL);
}
static struct sctp_tcb *
sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
uint16_t lport)
{
struct sctpasochead *head;
struct sctp_nets *net;
struct sctp_tcb *stcb;
SCTP_INP_INFO_RLOCK();
head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
sctppcbinfo.hashasocmark)];
if (head == NULL) {
SCTP_INP_INFO_RUNLOCK();
return (NULL);
}
LIST_FOREACH(stcb, head, sctp_asocs) {
SCTP_INP_RLOCK(stcb->sctp_ep);
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(stcb->sctp_ep);
if (stcb->asoc.my_vtag == vtag) {
if (stcb->rport != rport) {
SCTP_TCB_UNLOCK(stcb);
continue;
}
if (stcb->sctp_ep->sctp_lport != lport) {
SCTP_TCB_UNLOCK(stcb);
continue;
}
net = sctp_findnet(stcb, from);
if (net) {
*netp = net;
sctp_pegs[SCTP_VTAG_EXPR]++;
*inp_p = stcb->sctp_ep;
SCTP_INP_INFO_RUNLOCK();
return (stcb);
} else {
sctp_pegs[SCTP_VTAG_BOGUS]++;
}
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_INFO_RUNLOCK();
return (NULL);
}
struct sctp_tcb *
sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_chunkhdr *ch,
struct sctp_inpcb **inp_p, struct sctp_nets **netp)
{
int find_tcp_pool;
struct ip *iph;
struct sctp_tcb *retval;
struct sockaddr_storage to_store, from_store;
struct sockaddr *to = (struct sockaddr *)&to_store;
struct sockaddr *from = (struct sockaddr *)&from_store;
struct sctp_inpcb *inp;
iph = mtod(m, struct ip *);
if (iph->ip_v == IPVERSION) {
struct sockaddr_in *to4, *from4;
to4 = (struct sockaddr_in *)&to_store;
from4 = (struct sockaddr_in *)&from_store;
memset(to4, 0, sizeof(*to4));
memset(from4, 0, sizeof(*from4));
from4->sin_family = to4->sin_family = AF_INET;
from4->sin_len = to4->sin_len = sizeof(struct sockaddr_in);
from4->sin_addr.s_addr = iph->ip_src.s_addr;
to4->sin_addr.s_addr = iph->ip_dst.s_addr ;
from4->sin_port = sh->src_port;
to4->sin_port = sh->dest_port;
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6;
struct sockaddr_in6 *to6, *from6;
ip6 = mtod(m, struct ip6_hdr *);
to6 = (struct sockaddr_in6 *)&to_store;
from6 = (struct sockaddr_in6 *)&from_store;
memset(to6, 0, sizeof(*to6));
memset(from6, 0, sizeof(*from6));
from6->sin6_family = to6->sin6_family = AF_INET6;
from6->sin6_len = to6->sin6_len = sizeof(struct sockaddr_in6);
from6->sin6_addr = ip6->ip6_src;
to6->sin6_addr = ip6->ip6_dst;
from6->sin6_port = sh->src_port;
to6->sin6_port = sh->dest_port;
#if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
(void)in6_recoverscope(to6, &to6->sin6_addr, NULL);
(void)in6_embedscope(&to6->sin6_addr, to6, NULL, NULL);
(void)in6_recoverscope(from6, &from6->sin6_addr, NULL);
(void)in6_embedscope(&from6->sin6_addr, from6, NULL, NULL);
#endif
} else {
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Looking for port %d address :",
ntohs(((struct sockaddr_in *)to)->sin_port));
sctp_print_address(to);
printf("From for port %d address :",
ntohs(((struct sockaddr_in *)from)->sin_port));
sctp_print_address(from);
}
#endif
if (sh->v_tag) {
retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
inp_p, netp, sh->src_port, sh->dest_port);
if (retval) {
return (retval);
}
}
find_tcp_pool = 0;
if ((ch->chunk_type != SCTP_INITIATION) &&
(ch->chunk_type != SCTP_INITIATION_ACK) &&
(ch->chunk_type != SCTP_COOKIE_ACK) &&
(ch->chunk_type != SCTP_COOKIE_ECHO)) {
find_tcp_pool = 1;
}
if (inp_p) {
retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
find_tcp_pool);
inp = *inp_p;
} else {
retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
find_tcp_pool);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("retval:%p inp:%p\n", retval, inp);
}
#endif
if (retval == NULL && inp) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Found endpoint %p but no asoc - ep state:%x\n",
inp, inp->sctp_flags);
}
#endif
if ((ch->chunk_type == SCTP_INITIATION) ||
(ch->chunk_type == SCTP_INITIATION_ACK)) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Gak, its in the TCP pool... return NULL");
}
#endif
if (inp_p) {
*inp_p = NULL;
}
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Now doing SPECIAL find\n");
}
#endif
retval = sctp_findassociation_special_addr(m, iphlen,
offset, sh, inp_p, netp, to);
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("retval is %p\n", retval);
}
#endif
return (retval);
}
extern int sctp_max_burst_default;
extern unsigned int sctp_delayed_sack_time_default;
extern unsigned int sctp_heartbeat_interval_default;
extern unsigned int sctp_pmtu_raise_time_default;
extern unsigned int sctp_shutdown_guard_time_default;
extern unsigned int sctp_secret_lifetime_default;
extern unsigned int sctp_rto_max_default;
extern unsigned int sctp_rto_min_default;
extern unsigned int sctp_rto_initial_default;
extern unsigned int sctp_init_rto_max_default;
extern unsigned int sctp_valid_cookie_life_default;
extern unsigned int sctp_init_rtx_max_default;
extern unsigned int sctp_assoc_rtx_max_default;
extern unsigned int sctp_path_rtx_max_default;
extern unsigned int sctp_nr_outgoing_streams_default;
int
sctp_inpcb_alloc(struct socket *so)
{
int i, error;
struct sctp_inpcb *inp;
#ifdef DEBUG
struct sctp_inpcb *n_inp;
#endif
#ifdef IPSEC
struct inpcbpolicy *pcb_sp = NULL;
#endif
struct sctp_pcb *m;
struct timeval time;
error = 0;
#ifdef DEBUG
SCTP_INP_INFO_RLOCK();
inp = LIST_FIRST(&sctppcbinfo.listhead);
while (inp) {
n_inp = LIST_NEXT(inp, sctp_list);
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
printf("Found a GONE on list\n");
SCTP_INP_INFO_RUNLOCK();
sctp_inpcb_free(inp, 1);
SCTP_INP_INFO_RLOCK();
}
}
inp = n_inp;
}
SCTP_INP_INFO_RUNLOCK();
#endif
SCTP_INP_INFO_WLOCK();
inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
if (inp == NULL) {
printf("Out of SCTP-INPCB structures - no resources\n");
SCTP_INP_INFO_WUNLOCK();
return (ENOBUFS);
}
memset(inp, 0, sizeof(*inp));
inp->sctp_socket = so;
inp->ip_inp.inp.inp_socket = so;
inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
#ifdef IPSEC
if (ipsec_enabled) {
error = ipsec_init_pcbpolicy(so, &pcb_sp);
if (error != 0) {
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
SCTP_INP_INFO_WUNLOCK();
return error;
}
inp->ip_inp.inp.inp_sp = pcb_sp;
pcb_sp->sp_inph = (struct inpcb_hdr *)inp;
}
#endif
sctppcbinfo.ipi_count_ep++;
inp->inp_ip_ttl = ip_defttl;
inp->inp_ip_tos = 0;
so->so_pcb = (void *)inp;
if ((so->so_type == SOCK_DGRAM) ||
(so->so_type == SOCK_SEQPACKET)) {
inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
SCTP_PCB_FLAGS_UNBOUND);
inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
} else if (so->so_type == SOCK_STREAM) {
inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
SCTP_PCB_FLAGS_UNBOUND);
inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
so->so_state &= ~SS_NBIO;
} else {
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
SCTP_INP_INFO_WUNLOCK();
return (EOPNOTSUPP);
}
inp->sctp_tcbhash = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_hash);
if (inp->sctp_tcbhash == NULL) {
printf("Out of SCTP-INPCB->hashinit - no resources\n");
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
SCTP_INP_INFO_WUNLOCK();
return (ENOBUFS);
} else {
for (i = 0; i < sctp_pcbtblsize; i++)
LIST_INIT(&inp->sctp_tcbhash[i]);
for (i = 1; i < sctp_pcbtblsize; i <<= 1)
continue;
inp->sctp_hashmark = i - 1;
}
SCTP_INP_LOCK_INIT(inp);
SCTP_ASOC_CREATE_LOCK_INIT(inp);
SCTP_INP_WLOCK(inp);
LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
SCTP_INP_INFO_WUNLOCK();
LIST_INIT(&inp->sctp_addr_list);
LIST_INIT(&inp->sctp_asoc_list);
TAILQ_INIT(&inp->sctp_queue_list);
callout_init(&inp->sctp_ep.signature_change.timer, 0);
inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
m = &inp->sctp_ep;
m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC);
m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC);
m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_heartbeat_interval_default;
m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
m->sctp_maxrto = sctp_rto_max_default;
m->sctp_minrto = sctp_rto_min_default;
m->initial_rto = sctp_rto_initial_default;
m->initial_init_rto_max = sctp_init_rto_max_default;
m->max_open_streams_intome = MAX_SCTP_STREAMS;
m->max_init_times = sctp_init_rtx_max_default;
m->max_send_times = sctp_assoc_rtx_max_default;
m->def_net_failure = sctp_path_rtx_max_default;
m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
m->max_burst = sctp_max_burst_default;
m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
m->adaption_layer_indicator = 0x504C5253;
m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
sizeof(struct sctp_state_cookie);
m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
SCTP_GETTIME_TIMEVAL(&time);
m->time_of_secret_change = time.tv_sec;
for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
m->secret_key[0][i] = sctp_select_initial_TSN(m);
}
sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
m->def_cookie_life = sctp_valid_cookie_life_default;
SCTP_INP_WUNLOCK(inp);
return (error);
}
void
sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
struct sctp_tcb *stcb)
{
uint16_t lport, rport;
struct sctppcbhead *head;
struct sctp_laddr *laddr, *oladdr;
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_INFO_WLOCK();
SCTP_INP_WLOCK(old_inp);
SCTP_INP_WLOCK(new_inp);
SCTP_TCB_LOCK(stcb);
new_inp->sctp_ep.time_of_secret_change =
old_inp->sctp_ep.time_of_secret_change;
memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
sizeof(old_inp->sctp_ep.secret_key));
new_inp->sctp_ep.current_secret_number =
old_inp->sctp_ep.current_secret_number;
new_inp->sctp_ep.last_secret_number =
old_inp->sctp_ep.last_secret_number;
new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
lport = new_inp->sctp_lport = old_inp->sctp_lport;
rport = stcb->rport;
LIST_REMOVE(stcb, sctp_tcbhash);
LIST_REMOVE(stcb, sctp_tcblist);
head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
sctppcbinfo.hashtcpmark)];
LIST_INSERT_HEAD(head, new_inp, sctp_hash);
LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
SCTP_INP_INFO_WUNLOCK();
stcb->sctp_socket = new_inp->sctp_socket;
stcb->sctp_ep = new_inp;
if (new_inp->sctp_tcbhash != NULL) {
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash,
new_inp->sctp_tcbhash);
new_inp->sctp_tcbhash = NULL;
}
if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
sctppcbinfo.ipi_zone_laddr);
if (laddr == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Association hosed in TCP model, out of laddr memory\n");
}
#endif
continue;
}
sctppcbinfo.ipi_count_laddr++;
sctppcbinfo.ipi_gencnt_laddr++;
memset(laddr, 0, sizeof(*laddr));
laddr->ifa = oladdr->ifa;
LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
sctp_nxt_addr);
new_inp->laddr_count++;
}
}
SCTP_INP_WUNLOCK(new_inp);
SCTP_INP_WUNLOCK(old_inp);
}
static int
sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
{
struct sctppcbhead *head;
struct sctp_inpcb *t_inp;
head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
sctppcbinfo.hashmark)];
LIST_FOREACH(t_inp, head, sctp_hash) {
if (t_inp->sctp_lport != lport) {
continue;
}
if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(((struct in6pcb *)t_inp)->in6p_flags & IN6P_IPV6_V6ONLY)) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
return (1);
} else {
continue;
}
} else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
return (1);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
) {
continue;
}
}
return (1);
}
return (0);
}
int
sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct lwp *l)
{
struct sctppcbhead *head;
struct sctp_inpcb *inp, *inp_tmp;
int bindall;
uint16_t lport;
int error;
lport = 0;
error = 0;
bindall = 1;
inp = (struct sctp_inpcb *)so->so_pcb;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
if (addr) {
printf("Bind called port:%d\n",
ntohs(((struct sockaddr_in *)addr)->sin_port));
printf("Addr :");
sctp_print_address(addr);
}
}
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
return (EINVAL);
}
if (addr != NULL) {
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
if (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY) {
return (EINVAL);
}
if (addr->sa_len != sizeof(*sin))
return (EINVAL);
sin = (struct sockaddr_in *)addr;
lport = sin->sin_port;
if (sin->sin_addr.s_addr != INADDR_ANY) {
bindall = 0;
}
#ifdef IPSEC
inp->ip_inp.inp.inp_af = AF_INET;
#endif
} else if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (addr->sa_len != sizeof(*sin6))
return (EINVAL);
lport = sin6->sin6_port;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
bindall = 0;
error = sa6_embedscope(sin6, ip6_use_defzone);
if (error != 0)
return (error);
}
#ifndef SCOPEDROUTING
sin6->sin6_scope_id = 0;
#endif
#ifdef IPSEC
inp->ip_inp.inp.inp_af = AF_INET6;
#endif
} else {
return (EAFNOSUPPORT);
}
#ifdef IPSEC
if (ipsec_enabled) {
inp->ip_inp.inp.inp_socket = so;
error = ipsec_init_pcbpolicy(so, &inp->ip_inp.inp.inp_sp);
if (error != 0)
return (error);
inp->ip_inp.inp.inp_sp->sp_inph = (struct inpcb_hdr *)inp;
}
#endif
}
SCTP_INP_INFO_WLOCK();
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("sctp_inpcb_bind: after SCTP_INP_INFO_WLOCK\n");
}
#endif
SCTP_INP_WLOCK(inp);
SCTP_INP_INCR_REF(inp);
if (lport) {
enum kauth_network_req req;
if (ntohs(lport) < IPPORT_RESERVED)
req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
else
req = KAUTH_REQ_NETWORK_BIND_PORT;
error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
req, so, addr, NULL);
if (error) {
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (EACCES);
}
SCTP_INP_WUNLOCK(inp);
inp_tmp = sctp_pcb_findep(addr, 0, 1);
if (inp_tmp != NULL) {
SCTP_INP_WLOCK(inp_tmp);
SCTP_INP_DECR_REF(inp_tmp);
SCTP_INP_WUNLOCK(inp_tmp);
SCTP_INP_INFO_WUNLOCK();
return EADDRINUSE;
}
SCTP_INP_WLOCK(inp);
if (bindall) {
if (sctp_isport_inuse(inp, lport)) {
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return EADDRINUSE;
}
}
} else {
uint32_t port_guess;
uint16_t port_attempt;
int not_done=1;
while (not_done) {
port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
port_attempt = (port_guess & 0x0000ffff);
if (port_attempt == 0) {
goto next_half;
}
if (port_attempt < IPPORT_RESERVED) {
port_attempt += IPPORT_RESERVED;
}
if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
not_done = 0;
continue;
}
next_half:
port_attempt = ((port_guess >> 16) & 0x0000ffff);
if (port_attempt == 0) {
goto last_try;
}
if (port_attempt < IPPORT_RESERVED) {
port_attempt += IPPORT_RESERVED;
}
if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
not_done = 0;
continue;
}
last_try:
port_attempt = (((port_guess >> 16) & 0x0000ffff) + (port_guess & 0x0000ffff));
if (port_attempt == 0) {
continue;
}
if (port_attempt < IPPORT_RESERVED) {
port_attempt += IPPORT_RESERVED;
}
if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
not_done = 0;
continue;
}
}
lport = htons(port_attempt);
}
SCTP_INP_DECR_REF(inp);
if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (EINVAL);
}
if (bindall) {
inp->sctp_flags |= (SCTP_PCB_FLAGS_BOUNDALL |
SCTP_PCB_FLAGS_DO_ASCONF);
if (sctp_auto_asconf == 0) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
} else {
inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
}
} else {
struct ifaddr *ifa;
struct sockaddr_storage store_sa;
memset(&store_sa, 0, sizeof(store_sa));
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)&store_sa;
memcpy(sin, addr, sizeof(struct sockaddr_in));
sin->sin_port = 0;
} else if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&store_sa;
memcpy(sin6, addr, sizeof(struct sockaddr_in6));
sin6->sin6_port = 0;
}
ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
if (ifa == NULL) {
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (EADDRNOTAVAIL);
}
if (addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
IN6_IFF_ANYCAST |
IN6_IFF_NOTREADY)) {
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (EINVAL);
}
}
inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
#if 0
inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
#endif
if (sctp_auto_asconf == 0) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
} else {
inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
}
inp->sctp_flags |= SCTP_PCB_FLAGS_DO_ASCONF;
error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
if (error != 0) {
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (error);
}
inp->laddr_count++;
}
head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
sctppcbinfo.hashmark)];
LIST_INSERT_HEAD(head, inp, sctp_hash);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
}
#endif
inp->sctp_lport = lport;
inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (0);
}
static void
sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
{
struct sctp_iterator *it;
LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
if (it == inp->inp_starting_point_for_iterator)
continue;
if (it->inp == inp) {
if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
it->inp = NULL;
it->stcb = NULL;
} else {
it->inp = inp_next;
it->stcb = NULL;
}
}
}
it = inp->inp_starting_point_for_iterator;
if (it) {
if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
it->inp = NULL;
} else {
it->inp = inp_next;
}
it->stcb = NULL;
}
}
void
sctp_inpcb_free(struct sctp_inpcb *inp, int immediate)
{
struct sctp_inpcb *inp_save;
struct sctp_tcb *asoc, *nasoc;
struct sctp_laddr *laddr, *nladdr;
struct inpcb *ip_pcb;
struct socket *so;
struct sctp_socket_q_list *sq;
int s, cnt;
struct rtentry *rt;
s = splsoftnet();
SCTP_ASOC_CREATE_LOCK(inp);
SCTP_INP_WLOCK(inp);
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
splx(s);
printf("Endpoint was all gone (dup free)?\n");
SCTP_INP_WUNLOCK(inp);
SCTP_ASOC_CREATE_UNLOCK(inp);
return;
}
sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
sctp_m_freem(inp->control);
inp->control = NULL;
sctp_m_freem(inp->pkt);
inp->pkt = NULL;
so = inp->sctp_socket;
ip_pcb = &inp->ip_inp.inp;
if (immediate == 0) {
int cnt_in_sd;
cnt_in_sd = 0;
for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
asoc = nasoc) {
nasoc = LIST_NEXT(asoc, sctp_tcblist);
if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
SCTP_TCB_LOCK(asoc);
SCTP_INP_WUNLOCK(inp);
sctp_free_assoc(inp, asoc);
SCTP_INP_WLOCK(inp);
continue;
} else {
asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
}
if ((asoc->asoc.size_on_delivery_queue > 0) ||
(asoc->asoc.size_on_reasm_queue > 0) ||
(asoc->asoc.size_on_all_streams > 0) ||
(so && (so->so_rcv.sb_cc > 0))
) {
struct mbuf *op_err;
MGET(op_err, M_DONTWAIT, MT_DATA);
if (op_err) {
struct sctp_paramhdr *ph;
op_err->m_len =
sizeof(struct sctp_paramhdr);
ph = mtod(op_err,
struct sctp_paramhdr *);
ph->param_type = htons(
SCTP_CAUSE_USER_INITIATED_ABT);
ph->param_length = htons(op_err->m_len);
}
SCTP_TCB_LOCK(asoc);
sctp_send_abort_tcb(asoc, op_err);
SCTP_INP_WUNLOCK(inp);
sctp_free_assoc(inp, asoc);
SCTP_INP_WLOCK(inp);
continue;
} else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
TAILQ_EMPTY(&asoc->asoc.sent_queue)) {
if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
(SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
SCTP_TCB_LOCK(asoc);
sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
asoc->asoc.primary_destination);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
asoc->asoc.primary_destination);
sctp_chunk_output(inp, asoc, 1);
SCTP_TCB_UNLOCK(asoc);
}
} else {
asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
}
cnt_in_sd++;
}
if (cnt_in_sd) {
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE;
splx(s);
SCTP_INP_WUNLOCK(inp);
SCTP_ASOC_CREATE_UNLOCK(inp);
return;
}
}
#if defined(__FreeBSD__) && __FreeBSD_version >= 503000
if (inp->refcount) {
sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
SCTP_INP_WUNLOCK(inp);
SCTP_ASOC_CREATE_UNLOCK(inp);
return;
}
#endif
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
rt = rtcache_validate(&ip_pcb->inp_route);
rtcache_unref(rt, &ip_pcb->inp_route);
callout_stop(&inp->sctp_ep.signature_change.timer);
callout_destroy(&inp->sctp_ep.signature_change.timer);
if (so) {
#ifdef IPSEC
if (ipsec_enabled)
ipsec_delete_pcbpolicy(ip_pcb);
#endif
so->so_pcb = 0;
}
if (ip_pcb->inp_options) {
(void)m_free(ip_pcb->inp_options);
ip_pcb->inp_options = 0;
}
rtcache_free(&ip_pcb->inp_route);
if (ip_pcb->inp_moptions) {
ip_freemoptions(ip_pcb->inp_moptions);
ip_pcb->inp_moptions = 0;
}
inp->inp_vflag = 0;
cnt = 0;
for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
asoc = nasoc) {
nasoc = LIST_NEXT(asoc, sctp_tcblist);
SCTP_TCB_LOCK(asoc);
if (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) {
struct mbuf *op_err;
MGET(op_err, M_DONTWAIT, MT_DATA);
if (op_err) {
struct sctp_paramhdr *ph;
op_err->m_len = sizeof(struct sctp_paramhdr);
ph = mtod(op_err, struct sctp_paramhdr *);
ph->param_type = htons(
SCTP_CAUSE_USER_INITIATED_ABT);
ph->param_length = htons(op_err->m_len);
}
sctp_send_abort_tcb(asoc, op_err);
}
cnt++;
inp->sctp_flags &= ~SCTP_PCB_FLAGS_SOCKET_GONE;
SCTP_INP_WUNLOCK(inp);
sctp_free_assoc(inp, asoc);
SCTP_INP_WLOCK(inp);
}
while ((sq = TAILQ_FIRST(&inp->sctp_queue_list)) != NULL) {
TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
sctppcbinfo.ipi_count_sockq--;
sctppcbinfo.ipi_gencnt_sockq++;
}
inp->sctp_socket = 0;
SCTP_INP_WUNLOCK(inp);
SCTP_ITERATOR_LOCK();
SCTP_INP_INFO_WLOCK();
SCTP_INP_WLOCK(inp);
inp_save = LIST_NEXT(inp, sctp_list);
LIST_REMOVE(inp, sctp_list);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
SCTP_PCB_FLAGS_UNBOUND) {
LIST_REMOVE(inp, sctp_hash);
}
sctp_iterator_inp_being_freed(inp, inp_save);
SCTP_ITERATOR_UNLOCK();
for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
laddr = nladdr) {
nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
LIST_REMOVE(laddr, sctp_nxt_addr);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
sctppcbinfo.ipi_gencnt_laddr++;
sctppcbinfo.ipi_count_laddr--;
}
if (inp->sctp_tcbhash != NULL) {
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash, inp->sctp_tcbhash);
inp->sctp_tcbhash = NULL;
}
SCTP_INP_WUNLOCK(inp);
SCTP_ASOC_CREATE_UNLOCK(inp);
SCTP_INP_LOCK_DESTROY(inp);
SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
sctppcbinfo.ipi_count_ep--;
SCTP_INP_INFO_WUNLOCK();
splx(s);
sofree(so);
mutex_enter(softnet_lock);
}
struct sctp_nets *
sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
{
struct sctp_nets *net;
#if 0
if (stcb->rport != sin->sin_port) {
return (NULL);
}
#endif
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (sctp_cmpaddr(addr, rtcache_getdst(&net->ro)))
return (net);
}
return (NULL);
}
int
sctp_is_address_on_local_host(struct sockaddr *addr)
{
struct ifnet *ifn;
struct ifaddr *ifa;
int s;
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (addr->sa_family == ifa->ifa_addr->sa_family) {
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin, *sin_c;
sin = (struct sockaddr_in *)addr;
sin_c = (struct sockaddr_in *)
ifa->ifa_addr;
if (sin->sin_addr.s_addr ==
sin_c->sin_addr.s_addr) {
pserialize_read_exit(s);
return (1);
}
} else if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6, *sin_c6;
sin6 = (struct sockaddr_in6 *)addr;
sin_c6 = (struct sockaddr_in6 *)
ifa->ifa_addr;
if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
&sin_c6->sin6_addr)) {
pserialize_read_exit(s);
return (1);
}
}
}
}
}
pserialize_read_exit(s);
return (0);
}
int
sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
int set_scope, int from)
{
struct sctp_nets *net, *netfirst;
struct rtentry *rt, *netfirst_rt;
int addr_inscope;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Adding an address (from:%d) to the peer: ", from);
sctp_print_address(newaddr);
}
#endif
netfirst = sctp_findnet(stcb, newaddr);
if (netfirst) {
if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
netfirst->dest_state = (SCTP_ADDR_REACHABLE|
SCTP_ADDR_UNCONFIRMED);
} else {
netfirst->dest_state = SCTP_ADDR_REACHABLE;
}
return (0);
}
addr_inscope = 1;
if (newaddr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)newaddr;
if (sin->sin_addr.s_addr == 0) {
return (-1);
}
memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
sin->sin_len = sizeof(struct sockaddr_in);
if (set_scope) {
#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
stcb->ipv4_local_scope = 1;
#else
if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
stcb->asoc.ipv4_local_scope = 1;
}
#endif
if (sctp_is_address_on_local_host(newaddr)) {
stcb->asoc.loopback_scope = 1;
stcb->asoc.ipv4_local_scope = 1;
stcb->asoc.local_scope = 1;
stcb->asoc.site_scope = 1;
}
} else {
if (from == 8) {
if (sctp_is_address_on_local_host(newaddr)) {
stcb->asoc.loopback_scope = 1;
stcb->asoc.ipv4_local_scope = 1;
stcb->asoc.local_scope = 1;
stcb->asoc.site_scope = 1;
}
}
if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
(stcb->asoc.ipv4_local_scope == 0)) {
addr_inscope = 0;
}
}
} else if (newaddr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)newaddr;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
return (-1);
}
sin6->sin6_len = sizeof(struct sockaddr_in6);
if (set_scope) {
if (sctp_is_address_on_local_host(newaddr)) {
stcb->asoc.loopback_scope = 1;
stcb->asoc.local_scope = 1;
stcb->asoc.ipv4_local_scope = 1;
stcb->asoc.site_scope = 1;
} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
stcb->asoc.ipv4_local_scope = 1;
stcb->asoc.site_scope = 1;
} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
stcb->asoc.site_scope = 1;
}
} else {
if (from == 8) {
if (sctp_is_address_on_local_host(newaddr)) {
stcb->asoc.loopback_scope = 1;
stcb->asoc.ipv4_local_scope = 1;
stcb->asoc.local_scope = 1;
stcb->asoc.site_scope = 1;
}
}
if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
(stcb->asoc.loopback_scope == 0)) {
addr_inscope = 0;
} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
(stcb->asoc.local_scope == 0)) {
addr_inscope = 0;
} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
(stcb->asoc.site_scope == 0)) {
addr_inscope = 0;
}
}
} else {
return (-1);
}
net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
if (net == NULL) {
return (-1);
}
sctppcbinfo.ipi_count_raddr++;
sctppcbinfo.ipi_gencnt_raddr++;
memset(net, 0, sizeof(*net));
if (newaddr->sa_family == AF_INET) {
((struct sockaddr_in *)newaddr)->sin_port = stcb->rport;
} else if (newaddr->sa_family == AF_INET6) {
((struct sockaddr_in6 *)newaddr)->sin6_port = stcb->rport;
}
net->addr_is_local = sctp_is_address_on_local_host(newaddr);
net->failure_threshold = stcb->asoc.def_net_failure;
if (addr_inscope == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Adding an address which is OUT OF SCOPE\n");
}
#endif
net->dest_state = (SCTP_ADDR_REACHABLE |
SCTP_ADDR_OUT_OF_SCOPE);
} else {
if (from == 8)
net->dest_state = SCTP_ADDR_REACHABLE;
else
net->dest_state = SCTP_ADDR_REACHABLE |
SCTP_ADDR_UNCONFIRMED;
}
net->RTO = stcb->asoc.initial_rto;
stcb->asoc.numnets++;
net->ref_count = 1;
callout_init(&net->rxt_timer.timer, 0);
callout_init(&net->pmtu_timer.timer, 0);
if (newaddr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)newaddr;
if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
return (-1);
}
rt = rtcache_lookup(&net->ro, newaddr);
if (rt) {
net->mtu = rt->rt_ifp->if_mtu;
if (from == 1) {
stcb->asoc.smallest_mtu = net->mtu;
}
rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
} else {
net->mtu = stcb->asoc.smallest_mtu;
}
#ifdef SCTP_DEBUG
printf("After lookup\n");
#endif
if (stcb->asoc.smallest_mtu > net->mtu) {
stcb->asoc.smallest_mtu = net->mtu;
}
net->cwnd = uimin((net->mtu * 4), uimax((stcb->asoc.max_burst * net->mtu), SCTP_INITIAL_CWND));
if (net->cwnd < (2 * net->mtu)) {
net->cwnd = 2 * net->mtu;
}
net->ssthresh = stcb->asoc.peers_rwnd;
net->src_addr_selected = 0;
netfirst = TAILQ_FIRST(&stcb->asoc.nets);
if (rt == NULL) {
TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
} else if (netfirst == NULL) {
TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
} else if ((netfirst_rt = rtcache_validate(&netfirst->ro)) == NULL) {
TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
} else if (rt->rt_ifp != netfirst_rt->rt_ifp) {
rtcache_unref(netfirst_rt, &netfirst->ro);
TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
} else {
struct sctp_nets *netlook;
struct rtentry *netlook_rt;
do {
netlook = TAILQ_NEXT(netfirst, sctp_next);
if (netlook == NULL) {
TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
sctp_next);
break;
} else if ((netlook_rt = rtcache_validate(&netlook->ro)) == NULL) {
TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
break;
} else if (netlook_rt->rt_ifp != rt->rt_ifp) {
rtcache_unref(netlook_rt, &netlook->ro);
TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
net, sctp_next);
break;
}
rtcache_unref(netlook_rt, &netlook->ro);
netfirst = netlook;
} while (netlook != NULL);
rtcache_unref(netfirst_rt, &netfirst->ro);
}
if (stcb->asoc.primary_destination == 0) {
stcb->asoc.primary_destination = net;
} else if (!rtcache_validate(&stcb->asoc.primary_destination->ro)) {
stcb->asoc.primary_destination = net;
}
sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
net);
return (0);
}
struct sctp_tcb *
sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
int for_a_init, int *error, uint32_t override_tag)
{
struct sctp_tcb *stcb;
struct sctp_association *asoc;
struct sctpasochead *head;
uint16_t rport;
int err;
if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
*error = ENOBUFS;
return (NULL);
}
SCTP_INP_RLOCK(inp);
if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
SCTP_INP_RUNLOCK(inp);
*error = EINVAL;
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("Allocate an association for peer:");
if (firstaddr)
sctp_print_address(firstaddr);
else
printf("None\n");
printf("Port:%d\n",
ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
}
#endif
if (firstaddr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)firstaddr;
if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("peer address invalid\n");
}
#endif
SCTP_INP_RUNLOCK(inp);
*error = EINVAL;
return (NULL);
}
rport = sin->sin_port;
} else if (firstaddr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)firstaddr;
if ((sin6->sin6_port == 0) ||
(IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("peer address invalid\n");
}
#endif
SCTP_INP_RUNLOCK(inp);
*error = EINVAL;
return (NULL);
}
rport = sin6->sin6_port;
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("BAD family %d\n", firstaddr->sa_family);
}
#endif
SCTP_INP_RUNLOCK(inp);
*error = EINVAL;
return (NULL);
}
SCTP_INP_RUNLOCK(inp);
if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("Doing implicit BIND\n");
}
#endif
if ((err = sctp_inpcb_bind(inp->sctp_socket,
(struct sockaddr *)NULL, (struct lwp *)NULL))){
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("BIND FAILS ret:%d\n", err);
}
#endif
*error = err;
return (NULL);
}
}
stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
if (stcb == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("aloc_assoc: no assoc mem left, stcb=NULL\n");
}
#endif
*error = ENOMEM;
return (NULL);
}
sctppcbinfo.ipi_count_asoc++;
sctppcbinfo.ipi_gencnt_asoc++;
memset(stcb, 0, sizeof(*stcb));
asoc = &stcb->asoc;
SCTP_TCB_LOCK_INIT(stcb);
#ifdef SCTP_DEBUG
printf("Before back pointers\n");
#endif
stcb->sctp_ep = inp;
stcb->sctp_socket = inp->sctp_socket;
if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
SCTP_TCB_LOCK_DESTROY (stcb);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
sctppcbinfo.ipi_count_asoc--;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
}
#endif
*error = err;
return (NULL);
}
stcb->rport = rport;
SCTP_INP_INFO_WLOCK();
SCTP_INP_WLOCK(inp);
if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
SCTP_TCB_LOCK_DESTROY (stcb);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
sctppcbinfo.ipi_count_asoc--;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
}
#endif
*error = EINVAL;
return (NULL);
}
SCTP_TCB_LOCK(stcb);
head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
sctppcbinfo.hashasocmark)];
LIST_INSERT_HEAD(head, stcb, sctp_asocs);
SCTP_INP_INFO_WUNLOCK();
if ((err = sctp_add_remote_addr(stcb, firstaddr, 1, 1))) {
if (asoc->strmout)
free(asoc->strmout, M_PCB);
if (asoc->mapping_array)
free(asoc->mapping_array, M_PCB);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
sctppcbinfo.ipi_count_asoc--;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB3) {
printf("aloc_assoc: couldn't add remote addr!\n");
}
#endif
SCTP_TCB_LOCK_DESTROY (stcb);
*error = ENOBUFS;
return (NULL);
}
callout_init(&asoc->hb_timer.timer, 0);
callout_init(&asoc->dack_timer.timer, 0);
callout_init(&asoc->asconf_timer.timer, 0);
callout_init(&asoc->shut_guard_timer.timer, 0);
callout_init(&asoc->autoclose_timer.timer, 0);
callout_init(&asoc->delayed_event_timer.timer, 0);
LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
#ifdef SCTP_DEBUG
printf("Before hashing %ld size %d\n",
inp->sctp_hashmark, sctp_pcbtblsize);
#endif
if (inp->sctp_tcbhash != NULL) {
head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
inp->sctp_hashmark)];
LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
}
#ifdef SCTP_DEBUG
printf("After hashing\n");
#endif
SCTP_INP_WUNLOCK(inp);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("Association %p now allocated\n", stcb);
}
#endif
return (stcb);
}
void
sctp_free_remote_addr(struct sctp_nets *net)
{
if (net == NULL)
return;
net->ref_count--;
if (net->ref_count <= 0) {
callout_stop(&net->rxt_timer.timer);
callout_stop(&net->pmtu_timer.timer);
callout_destroy(&net->rxt_timer.timer);
callout_destroy(&net->pmtu_timer.timer);
net->dest_state = SCTP_ADDR_NOT_REACHABLE;
rtcache_free(&net->ro);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
sctppcbinfo.ipi_count_raddr--;
}
}
int
sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
{
struct sctp_association *asoc;
struct sctp_nets *net, *net_tmp;
asoc = &stcb->asoc;
if (asoc->numnets < 2) {
return (-1);
}
for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
net_tmp = TAILQ_NEXT(net, sctp_next);
if (rtcache_getdst(&net->ro)->sa_family != remaddr->sa_family) {
continue;
}
if (sctp_cmpaddr(rtcache_getdst(&net->ro), remaddr)) {
asoc->numnets--;
TAILQ_REMOVE(&asoc->nets, net, sctp_next);
sctp_free_remote_addr(net);
if (net == asoc->primary_destination) {
struct sctp_nets *lnet;
lnet = TAILQ_FIRST(&asoc->nets);
asoc->primary_destination =
sctp_find_alternate_net(stcb, lnet);
}
if (net == asoc->last_data_chunk_from) {
asoc->last_data_chunk_from =
TAILQ_FIRST(&asoc->nets);
}
if (net == asoc->last_control_chunk_from) {
asoc->last_control_chunk_from =
TAILQ_FIRST(&asoc->nets);
}
if (net == asoc->asconf_last_sent_to) {
asoc->asconf_last_sent_to =
TAILQ_FIRST(&asoc->nets);
}
return (0);
}
}
return (-2);
}
static void
sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, u_int32_t tag)
{
struct sctpvtaghead *chain;
struct sctp_tagblock *twait_block;
struct timeval now;
int set, i;
SCTP_GETTIME_TIMEVAL(&now);
chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
set = 0;
if (!LIST_EMPTY(chain)) {
LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
if ((twait_block->vtag_block[i].v_tag == 0) &&
!set) {
twait_block->vtag_block[0].tv_sec_at_expire =
now.tv_sec + SCTP_TIME_WAIT;
twait_block->vtag_block[0].v_tag = tag;
set = 1;
} else if ((twait_block->vtag_block[i].v_tag) &&
((long)twait_block->vtag_block[i].tv_sec_at_expire >
now.tv_sec)) {
twait_block->vtag_block[i].tv_sec_at_expire = 0;
twait_block->vtag_block[i].v_tag = 0;
if (set == 0) {
twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
twait_block->vtag_block[0].v_tag = tag;
set = 1;
}
}
}
if (set) {
break;
}
}
}
if (!set) {
twait_block = malloc(sizeof(struct sctp_tagblock), M_PCB, M_NOWAIT);
if (twait_block == NULL) {
return;
}
memset(twait_block, 0, sizeof(struct sctp_timewait));
LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
SCTP_TIME_WAIT;
twait_block->vtag_block[0].v_tag = tag;
}
}
static void
sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
{
struct sctp_iterator *it;
SCTP_ITERATOR_LOCK();
SCTP_INP_INFO_WLOCK();
SCTP_INP_WLOCK(inp);
SCTP_TCB_LOCK(stcb);
it = stcb->asoc.stcb_starting_point_for_iterator;
if (it == NULL) {
return;
}
if (it->inp != stcb->sctp_ep) {
return;
}
if (it->stcb != stcb) {
return;
}
it->stcb = LIST_NEXT(stcb, sctp_tcblist);
if (it->stcb == NULL) {
if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
it->inp = NULL;
} else {
it->inp = LIST_NEXT(inp, sctp_list);
}
}
}
void
sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
{
struct sctp_association *asoc;
struct sctp_nets *net, *prev;
struct sctp_laddr *laddr;
struct sctp_tmit_chunk *chk;
struct sctp_asconf_addr *aparam;
struct sctp_socket_q_list *sq;
int s;
s = splsoftnet();
if (stcb->asoc.state == 0) {
printf("Freeing already free association:%p - huh??\n",
stcb);
splx(s);
return;
}
asoc = &stcb->asoc;
asoc->state = 0;
callout_stop(&asoc->hb_timer.timer);
callout_destroy(&asoc->hb_timer.timer);
callout_stop(&asoc->dack_timer.timer);
callout_destroy(&asoc->dack_timer.timer);
callout_stop(&asoc->asconf_timer.timer);
callout_destroy(&asoc->asconf_timer.timer);
callout_stop(&asoc->shut_guard_timer.timer);
callout_destroy(&asoc->shut_guard_timer.timer);
callout_stop(&asoc->autoclose_timer.timer);
callout_destroy(&asoc->autoclose_timer.timer);
callout_stop(&asoc->delayed_event_timer.timer);
callout_destroy(&asoc->delayed_event_timer.timer);
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
callout_stop(&net->rxt_timer.timer);
callout_stop(&net->pmtu_timer.timer);
callout_destroy(&net->rxt_timer.timer);
callout_destroy(&net->pmtu_timer.timer);
}
SCTP_TCB_UNLOCK(stcb);
sctp_iterator_asoc_being_freed(inp, stcb);
TAILQ_FOREACH(sq, &inp->sctp_queue_list, next_sq) {
if (sq->tcb == stcb) {
sq->tcb = NULL;
}
}
if (inp->sctp_tcb_at_block == (void *)stcb) {
inp->error_on_block = ECONNRESET;
}
if (inp->sctp_tcbhash) {
LIST_REMOVE(stcb, sctp_tcbhash);
}
LIST_REMOVE(stcb, sctp_tcblist);
SCTP_INP_WUNLOCK(inp);
SCTP_ITERATOR_UNLOCK();
LIST_REMOVE(stcb, sctp_asocs);
sctp_add_vtag_to_timewait(inp, asoc->my_vtag);
SCTP_INP_INFO_WUNLOCK();
prev = NULL;
while (!TAILQ_EMPTY(&asoc->nets)) {
net = TAILQ_FIRST(&asoc->nets);
if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
break;
}
prev = net;
TAILQ_REMOVE(&asoc->nets, net, sctp_next);
rtcache_free(&net->ro);
net->ref_count = 0;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
sctppcbinfo.ipi_count_raddr--;
}
while (!TAILQ_EMPTY(&asoc->out_wheel)) {
struct sctp_stream_out *outs;
outs = TAILQ_FIRST(&asoc->out_wheel);
TAILQ_REMOVE(&asoc->out_wheel, outs, next_spoke);
chk = TAILQ_FIRST(&outs->outqueue);
while (chk) {
TAILQ_REMOVE(&outs->outqueue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
chk->whoTo = NULL;
chk->asoc = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
sctppcbinfo.ipi_gencnt_chunk++;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
chk = TAILQ_FIRST(&outs->outqueue);
}
outs = TAILQ_FIRST(&asoc->out_wheel);
}
if (asoc->pending_reply) {
free(asoc->pending_reply, M_PCB);
asoc->pending_reply = NULL;
}
chk = TAILQ_FIRST(&asoc->pending_reply_queue);
while (chk) {
TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
chk->whoTo = NULL;
chk->asoc = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
sctppcbinfo.ipi_gencnt_chunk++;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
chk = TAILQ_FIRST(&asoc->pending_reply_queue);
}
if (!TAILQ_EMPTY(&asoc->send_queue)) {
chk = TAILQ_FIRST(&asoc->send_queue);
while (chk) {
TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->send_queue);
}
}
if (!TAILQ_EMPTY(&asoc->sent_queue)) {
chk = TAILQ_FIRST(&asoc->sent_queue);
while (chk) {
TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->sent_queue);
}
}
if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
chk = TAILQ_FIRST(&asoc->control_send_queue);
while (chk) {
TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->control_send_queue);
}
}
if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
chk = TAILQ_FIRST(&asoc->reasmqueue);
while (chk) {
TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->reasmqueue);
}
}
if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
chk = TAILQ_FIRST(&asoc->delivery_queue);
while (chk) {
TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->delivery_queue);
}
}
if (asoc->mapping_array) {
free(asoc->mapping_array, M_PCB);
asoc->mapping_array = NULL;
}
if (asoc->strmout) {
free(asoc->strmout, M_PCB);
asoc->strmout = NULL;
}
asoc->streamoutcnt = 0;
if (asoc->strmin) {
int i;
for (i = 0; i < asoc->streamincnt; i++) {
if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
while (chk) {
TAILQ_REMOVE(&asoc->strmin[i].inqueue,
chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk,
chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
}
}
}
free(asoc->strmin, M_PCB);
asoc->strmin = NULL;
}
asoc->streamincnt = 0;
while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
LIST_REMOVE(laddr, sctp_nxt_addr);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
sctppcbinfo.ipi_count_laddr--;
}
while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
aparam = TAILQ_FIRST(&asoc->asconf_queue);
TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
free(aparam, M_PCB);
}
sctp_m_freem(asoc->last_asconf_ack_sent);
asoc->last_asconf_ack_sent = NULL;
SCTP_TCB_LOCK_DESTROY(stcb);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
sctppcbinfo.ipi_count_asoc--;
if ((inp->sctp_socket->so_snd.sb_cc) ||
(inp->sctp_socket->so_snd.sb_mbcnt)) {
inp->sctp_socket->so_snd.sb_cc = 0;
inp->sctp_socket->so_snd.sb_mbcnt = 0;
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
soisdisconnected(inp->sctp_socket);
}
}
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
sctp_inpcb_free(inp, 0);
}
splx(s);
}
int
sctp_destination_is_reachable(struct sctp_tcb *stcb, const struct sockaddr *destaddr)
{
struct sctp_inpcb *inp;
int answer;
inp = stcb->sctp_ep;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
return (1);
}
if (destaddr->sa_family == AF_INET6) {
answer = inp->inp_vflag & INP_IPV6;
} else if (destaddr->sa_family == AF_INET) {
answer = inp->inp_vflag & INP_IPV4;
} else {
answer = 0;
}
return (answer);
}
static void
sctp_update_ep_vflag(struct sctp_inpcb *inp) {
struct sctp_laddr *laddr;
inp->inp_vflag = 0;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
printf("An ounce of prevention is worth a pound of cure\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr) {
continue;
}
if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
inp->inp_vflag |= INP_IPV6;
} else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
inp->inp_vflag |= INP_IPV4;
}
}
}
int
sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
{
struct sctp_laddr *laddr;
int fnd, error;
fnd = 0;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
return (0);
}
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
return (-1);
}
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == ifa) {
fnd = 1;
break;
}
}
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
if (error != 0)
return (error);
inp->laddr_count++;
if (ifa->ifa_addr->sa_family == AF_INET6) {
inp->inp_vflag |= INP_IPV6;
} else if (ifa->ifa_addr->sa_family == AF_INET) {
inp->inp_vflag |= INP_IPV4;
}
}
return (0);
}
static void
sctp_select_primary_destination(struct sctp_tcb *stcb)
{
struct sctp_nets *net;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
continue;
if (sctp_destination_is_reachable(stcb,
rtcache_getdst(&net->ro))) {
stcb->asoc.primary_destination = net;
}
}
}
int
sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
{
struct sctp_laddr *laddr;
int fnd;
fnd = 0;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
return (EINVAL);
}
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == ifa) {
fnd = 1;
break;
}
}
if (fnd && (inp->laddr_count < 2)) {
return (-1);
}
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
struct sctp_tcb *stcb;
if (inp->next_addr_touse == laddr)
inp->next_addr_touse = NULL;
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (stcb->asoc.last_used_address == laddr)
stcb->asoc.last_used_address = NULL;
}
sctp_remove_laddr(laddr);
inp->laddr_count--;
sctp_update_ep_vflag(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (sctp_destination_is_reachable(stcb,
rtcache_getdst(&stcb->asoc.primary_destination->ro)) == 0) {
sctp_select_primary_destination(stcb);
}
SCTP_TCB_UNLOCK(stcb);
}
}
return (0);
}
int
sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
{
struct sctp_laddr *laddr;
int error;
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
IN6_IFF_ANYCAST |
IN6_IFF_NOTREADY))
return (-1);
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
if (laddr->ifa == ifa) {
return (-1);
}
}
error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
if (error != 0)
return (error);
return (0);
}
int
sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa) {
struct sctp_laddr *laddr;
int s;
s = splsoftnet();
laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
if (laddr == NULL) {
splx(s);
return (EINVAL);
}
sctppcbinfo.ipi_count_laddr++;
sctppcbinfo.ipi_gencnt_laddr++;
memset(laddr, 0, sizeof(*laddr));
laddr->ifa = ifa;
LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
splx(s);
return (0);
}
void
sctp_remove_laddr(struct sctp_laddr *laddr)
{
int s;
s = splsoftnet();
LIST_REMOVE(laddr, sctp_nxt_addr);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
sctppcbinfo.ipi_count_laddr--;
sctppcbinfo.ipi_gencnt_laddr++;
splx(s);
}
int
sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
{
struct sctp_inpcb *inp;
struct sctp_laddr *laddr;
inp = stcb->sctp_ep;
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
if (stcb->asoc.numnets < 2) {
return (-1);
}
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL)
continue;
if (laddr->ifa == ifa) {
sctp_remove_laddr(laddr);
return (0);
}
}
return (-1);
}
int
sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
{
struct sctp_inpcb *inp;
struct sctp_laddr *laddr;
struct sockaddr *l_sa;
inp = stcb->sctp_ep;
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
if (stcb->asoc.numnets < 2) {
return (-1);
}
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL)
continue;
if (laddr->ifa->ifa_addr == NULL)
continue;
l_sa = laddr->ifa->ifa_addr;
if (l_sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin1, *sin2;
sin1 = (struct sockaddr_in6 *)l_sa;
sin2 = (struct sockaddr_in6 *)sa;
if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
sizeof(struct in6_addr)) == 0) {
sctp_remove_laddr(laddr);
return (0);
}
} else if (l_sa->sa_family == AF_INET) {
struct sockaddr_in *sin1, *sin2;
sin1 = (struct sockaddr_in *)l_sa;
sin2 = (struct sockaddr_in *)sa;
if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
sctp_remove_laddr(laddr);
return (0);
}
} else {
return (-1);
}
}
return (-1);
}
static char sctp_pcb_initialized = 0;
#if defined(__FreeBSD__) || defined(__APPLE__)
static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
#endif
#ifndef SCTP_TCBHASHSIZE
#define SCTP_TCBHASHSIZE 1024
#endif
#ifndef SCTP_CHUNKQUEUE_SCALE
#define SCTP_CHUNKQUEUE_SCALE 10
#endif
void
sctp_pcb_init(void)
{
int i;
int hashtblsize = SCTP_TCBHASHSIZE;
#if defined(__FreeBSD__) || defined(__APPLE__)
int sctp_chunkscale = SCTP_CHUNKQUEUE_SCALE;
#endif
if (sctp_pcb_initialized != 0) {
return;
}
sctp_pcb_initialized = 1;
for (i = 0; i < SCTP_NUMBER_OF_PEGS; i++) {
sctp_pegs[i] = 0;
}
LIST_INIT(&sctppcbinfo.listhead);
LIST_INIT(&sctppcbinfo.iteratorhead);
#if defined(__FreeBSD__)
#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &hashtblsize);
TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
#else
TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
hashtblsize);
TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
sctp_pcbtblsize);
TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
sctp_chunkscale);
#endif
#endif
sctppcbinfo.sctp_asochash = hashinit((hashtblsize * 31), HASH_LIST,
M_WAITOK, &sctppcbinfo.hashasocmark);
sctppcbinfo.sctp_ephash = hashinit(hashtblsize, HASH_LIST,
M_WAITOK, &sctppcbinfo.hashmark);
sctppcbinfo.sctp_tcpephash = hashinit(hashtblsize, HASH_LIST,
M_WAITOK, &sctppcbinfo.hashtcpmark);
sctppcbinfo.hashtblsize = hashtblsize;
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
sizeof(struct sctp_inpcb), maxsockets);
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
sizeof(struct sctp_laddr),
(sctp_max_number_of_assoc * sctp_scale_up_for_address));
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
sizeof(struct sctp_nets),
(sctp_max_number_of_assoc * sctp_scale_up_for_address));
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
sizeof(struct sctp_tmit_chunk),
(sctp_max_number_of_assoc * sctp_scale_up_for_address *
sctp_chunkscale));
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_sockq, "sctp_sockq",
sizeof(struct sctp_socket_q_list),
(sctp_max_number_of_assoc * sctp_scale_up_for_address *
sctp_chunkscale));
SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_hash, "sctp_hash",
sizeof(void *) * sctp_pcbtblsize, maxsockets);
SCTP_INP_INFO_LOCK_INIT();
SCTP_ITERATOR_LOCK_INIT();
sctppcbinfo.ipi_count_ep = 0;
sctppcbinfo.ipi_gencnt_ep = 0;
sctppcbinfo.ipi_count_asoc = 0;
sctppcbinfo.ipi_gencnt_asoc = 0;
sctppcbinfo.ipi_count_laddr = 0;
sctppcbinfo.ipi_gencnt_laddr = 0;
sctppcbinfo.ipi_count_raddr = 0;
sctppcbinfo.ipi_gencnt_raddr = 0;
sctppcbinfo.ipi_count_chunk = 0;
sctppcbinfo.ipi_gencnt_chunk = 0;
sctppcbinfo.ipi_count_sockq = 0;
sctppcbinfo.ipi_gencnt_sockq = 0;
sctppcbinfo.mbuf_track = 0;
sctppcbinfo.lastlow = anonportmin;
for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
}
#if defined(_SCTP_NEEDS_CALLOUT_) && !defined(__APPLE__)
TAILQ_INIT(&sctppcbinfo.callqueue);
#endif
}
int
sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
int iphlen, int offset, int limit, struct sctphdr *sh,
struct sockaddr *altsa)
{
struct sctp_inpcb *inp, *l_inp;
struct sctp_nets *net, *net_tmp;
struct ip *iph;
struct sctp_paramhdr *phdr, parm_buf;
struct sctp_tcb *stcb_tmp;
u_int16_t ptype, plen;
struct sockaddr *sa;
struct sockaddr_storage dest_store;
struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
memset(&sin, 0, sizeof(sin));
memset(&sin6, 0, sizeof(sin6));
sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
sin.sin_port = stcb->rport;
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_port = stcb->rport;
if (altsa == NULL) {
iph = mtod(m, struct ip *);
if (iph->ip_v == IPVERSION) {
struct sockaddr_in *sin_2;
sin_2 = (struct sockaddr_in *)(local_sa);
memset(sin_2, 0, sizeof(sin));
sin_2->sin_family = AF_INET;
sin_2->sin_len = sizeof(sin);
sin_2->sin_port = sh->dest_port;
sin_2->sin_addr.s_addr = iph->ip_dst.s_addr ;
sin.sin_addr = iph->ip_src;
sa = (struct sockaddr *)&sin;
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6;
struct sockaddr_in6 *sin6_2;
ip6 = mtod(m, struct ip6_hdr *);
sin6_2 = (struct sockaddr_in6 *)(local_sa);
memset(sin6_2, 0, sizeof(sin6));
sin6_2->sin6_family = AF_INET6;
sin6_2->sin6_len = sizeof(struct sockaddr_in6);
sin6_2->sin6_port = sh->dest_port;
sin6.sin6_addr = ip6->ip6_src;
sa = (struct sockaddr *)&sin6;
} else {
sa = NULL;
}
} else {
sa = altsa;
}
stcb->asoc.ecn_allowed = 0;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
}
l_inp = inp = stcb->sctp_ep;
stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
if ((sa->sa_family == AF_INET) &&
(stcb->asoc.ipv4_addr_legal)) {
if (sctp_add_remote_addr(stcb, sa, 0, 2)) {
return (-1);
}
} else if ((sa->sa_family == AF_INET6) &&
(stcb->asoc.ipv6_addr_legal)) {
if (sctp_add_remote_addr(stcb, sa, 0, 3)) {
return (-1);
}
}
} else {
if (net_tmp != NULL && stcb_tmp == stcb) {
net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
} else if (stcb_tmp != stcb) {
return (-1);
}
}
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
return (-1);
}
if (stcb->asoc.state == 0) {
return (-1);
}
phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
while (phdr) {
ptype = ntohs(phdr->param_type);
plen = ntohs(phdr->param_length);
if (offset + plen > limit) {
break;
}
if (plen == 0) {
break;
}
if ((ptype == SCTP_IPV4_ADDRESS) &&
(stcb->asoc.ipv4_addr_legal)) {
struct sctp_ipv4addr_param *p4, p4_buf;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
if (plen != sizeof(struct sctp_ipv4addr_param) ||
phdr == NULL) {
return (-1);
}
p4 = (struct sctp_ipv4addr_param *)phdr;
sin.sin_addr.s_addr = p4->addr;
sa = (struct sockaddr *)&sin;
inp = stcb->sctp_ep;
stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
local_sa, stcb);
if ((stcb_tmp== NULL && inp == stcb->sctp_ep) ||
inp == NULL) {
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
return (-1);
}
if (stcb->asoc.state == 0) {
return (-1);
}
if (sctp_add_remote_addr(stcb, sa, 0, 4)) {
return (-1);
}
} else if (stcb_tmp == stcb) {
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
return (-1);
}
if (stcb->asoc.state == 0) {
return (-1);
}
if (net != NULL) {
net->dest_state &=
~SCTP_ADDR_NOT_IN_ASSOC;
}
} else {
SCTP_TCB_UNLOCK(stcb_tmp);
SCTP_INP_RLOCK(inp);
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
SCTP_INP_RUNLOCK(l_inp);
return (-1);
}
if (stcb->asoc.state == 0) {
SCTP_INP_RUNLOCK(l_inp);
return (-1);
}
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(stcb->sctp_ep);
return (-1);
}
} else if ((ptype == SCTP_IPV6_ADDRESS) &&
(stcb->asoc.ipv6_addr_legal)) {
struct sctp_ipv6addr_param *p6, p6_buf;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
if (plen != sizeof(struct sctp_ipv6addr_param) ||
phdr == NULL) {
return (-1);
}
p6 = (struct sctp_ipv6addr_param *)phdr;
memcpy((void *)&sin6.sin6_addr, p6->addr,
sizeof(p6->addr));
sa = (struct sockaddr *)&sin6;
inp = stcb->sctp_ep;
stcb_tmp= sctp_findassociation_ep_addr(&inp, sa, &net,
local_sa, stcb);
if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
inp == NULL)) {
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
return (-1);
}
if (stcb->asoc.state == 0) {
return (-1);
}
if (sctp_add_remote_addr(stcb, sa, 0, 5)) {
return (-1);
}
} else if (stcb_tmp == stcb) {
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
return (-1);
}
if (stcb->asoc.state == 0) {
return (-1);
}
if (net != NULL) {
net->dest_state &=
~SCTP_ADDR_NOT_IN_ASSOC;
}
} else {
SCTP_TCB_UNLOCK(stcb_tmp);
SCTP_INP_RLOCK(l_inp);
if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
SCTP_INP_RUNLOCK(l_inp);
return (-1);
}
if (stcb->asoc.state == 0) {
SCTP_INP_RUNLOCK(l_inp);
return (-1);
}
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(l_inp);
return (-1);
}
} else if (ptype == SCTP_ECN_CAPABLE) {
stcb->asoc.ecn_allowed = 1;
} else if (ptype == SCTP_ULP_ADAPTION) {
if (stcb->asoc.state != SCTP_STATE_OPEN) {
struct sctp_adaption_layer_indication ai, *aip;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&ai, sizeof(ai));
aip = (struct sctp_adaption_layer_indication *)phdr;
sctp_ulp_notify(SCTP_NOTIFY_ADAPTION_INDICATION,
stcb, ntohl(aip->indication), NULL);
}
} else if (ptype == SCTP_SET_PRIM_ADDR) {
struct sctp_asconf_addr_param lstore, *fee;
struct sctp_asconf_addrv4_param *fii;
int lptype;
struct sockaddr *lsa = NULL;
stcb->asoc.peer_supports_asconf = 1;
stcb->asoc.peer_supports_asconf_setprim = 1;
if (plen > sizeof(lstore)) {
return (-1);
}
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&lstore, plen);
if (phdr == NULL) {
return (-1);
}
fee = (struct sctp_asconf_addr_param *)phdr;
lptype = ntohs(fee->addrp.ph.param_type);
if (lptype == SCTP_IPV4_ADDRESS) {
if (plen !=
sizeof(struct sctp_asconf_addrv4_param)) {
printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
(int)sizeof(struct sctp_asconf_addrv4_param),
plen);
} else {
fii = (struct sctp_asconf_addrv4_param *)fee;
sin.sin_addr.s_addr = fii->addrp.addr;
lsa = (struct sockaddr *)&sin;
}
} else if (lptype == SCTP_IPV6_ADDRESS) {
if (plen !=
sizeof(struct sctp_asconf_addr_param)) {
printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
(int)sizeof(struct sctp_asconf_addr_param),
plen);
} else {
memcpy(sin6.sin6_addr.s6_addr,
fee->addrp.addr,
sizeof(fee->addrp.addr));
lsa = (struct sockaddr *)&sin6;
}
}
if (lsa) {
sctp_set_primary_addr(stcb, sa, NULL);
}
} else if (ptype == SCTP_PRSCTP_SUPPORTED) {
stcb->asoc.peer_supports_prsctp = 1;
} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
struct sctp_supported_chunk_types_param *pr_supported;
uint8_t local_store[128];
int num_ent, i;
phdr = sctp_get_next_param(m, offset,
(struct sctp_paramhdr *)&local_store, plen);
if (phdr == NULL) {
return (-1);
}
stcb->asoc.peer_supports_asconf = 0;
stcb->asoc.peer_supports_asconf_setprim = 0;
stcb->asoc.peer_supports_prsctp = 0;
stcb->asoc.peer_supports_pktdrop = 0;
stcb->asoc.peer_supports_strreset = 0;
pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
num_ent = plen - sizeof(struct sctp_paramhdr);
for (i=0; i<num_ent; i++) {
switch (pr_supported->chunk_types[i]) {
case SCTP_ASCONF:
stcb->asoc.peer_supports_asconf = 1;
stcb->asoc.peer_supports_asconf_setprim = 1;
break;
case SCTP_ASCONF_ACK:
stcb->asoc.peer_supports_asconf = 1;
stcb->asoc.peer_supports_asconf_setprim = 1;
break;
case SCTP_FORWARD_CUM_TSN:
stcb->asoc.peer_supports_prsctp = 1;
break;
case SCTP_PACKET_DROPPED:
stcb->asoc.peer_supports_pktdrop = 1;
break;
case SCTP_STREAM_RESET:
stcb->asoc.peer_supports_strreset = 1;
break;
default:
break;
}
}
} else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
stcb->asoc.peer_supports_ecn_nonce = 1;
stcb->asoc.ecn_nonce_allowed = 1;
} else if ((ptype == SCTP_HEARTBEAT_INFO) ||
(ptype == SCTP_STATE_COOKIE) ||
(ptype == SCTP_UNRECOG_PARAM) ||
(ptype == SCTP_COOKIE_PRESERVE) ||
(ptype == SCTP_SUPPORTED_ADDRTYPE) ||
(ptype == SCTP_ADD_IP_ADDRESS) ||
(ptype == SCTP_DEL_IP_ADDRESS) ||
(ptype == SCTP_ERROR_CAUSE_IND) ||
(ptype == SCTP_SUCCESS_REPORT)) {
;
} else {
if ((ptype & 0x8000) == 0x0000) {
break;
}
}
offset += SCTP_SIZE32(plen);
if (offset >= limit) {
break;
}
phdr = sctp_get_next_param(m, offset, &parm_buf,
sizeof(parm_buf));
}
for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
net_tmp = TAILQ_NEXT(net, sctp_next);
if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
SCTP_ADDR_NOT_IN_ASSOC) {
stcb->asoc.numnets--;
TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
sctp_free_remote_addr(net);
if (net == stcb->asoc.primary_destination) {
stcb->asoc.primary_destination = NULL;
sctp_select_primary_destination(stcb);
}
}
}
return (0);
}
int
sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
struct sctp_nets *net)
{
if (net == NULL && sa)
net = sctp_findnet(stcb, sa);
if (net == NULL) {
return (-1);
} else {
if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
return (-1);
}
stcb->asoc.primary_destination = net;
net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
return (0);
}
}
int
sctp_is_vtag_good(struct sctp_inpcb *inp, u_int32_t tag, struct timeval *now)
{
struct sctpasochead *head;
struct sctpvtaghead *chain;
struct sctp_tagblock *twait_block;
struct sctp_tcb *stcb;
int i;
SCTP_INP_INFO_WLOCK();
chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
sctppcbinfo.hashasocmark)];
if (head == NULL) {
SCTP_INP_INFO_WUNLOCK();
return (0);
}
LIST_FOREACH(stcb, head, sctp_asocs) {
if (stcb->asoc.my_vtag == tag) {
if (inp == stcb->sctp_ep) {
SCTP_INP_INFO_WUNLOCK();
return (0);
}
}
}
if (!LIST_EMPTY(chain)) {
LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
if (twait_block->vtag_block[i].v_tag == 0) {
continue;
} else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
now->tv_sec) {
twait_block->vtag_block[i].tv_sec_at_expire = 0;
twait_block->vtag_block[i].v_tag = 0;
} else if (twait_block->vtag_block[i].v_tag ==
tag) {
SCTP_INP_INFO_WUNLOCK();
return (0);
}
}
}
}
SCTP_INP_INFO_WUNLOCK();
return (1);
}
int
sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
{
struct sctp_laddr *laddr;
struct sockaddr *l_sa;
int found = 0;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
return (EINVAL);
}
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL)
continue;
if (laddr->ifa->ifa_addr == NULL)
continue;
l_sa = laddr->ifa->ifa_addr;
if (l_sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin1, *sin2;
sin1 = (struct sockaddr_in6 *)l_sa;
sin2 = (struct sockaddr_in6 *)sa;
if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
sizeof(struct in6_addr)) == 0) {
found = 1;
break;
}
} else if (l_sa->sa_family == AF_INET) {
struct sockaddr_in *sin1, *sin2;
sin1 = (struct sockaddr_in *)l_sa;
sin2 = (struct sockaddr_in *)sa;
if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
found = 1;
break;
}
} else {
return (-1);
}
}
if (found && inp->laddr_count < 2) {
return (-1);
}
if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
sctp_remove_laddr(laddr);
return (0);
} else {
return (-1);
}
}
static void
sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
{
struct sctp_association *asoc;
struct sctp_tmit_chunk *chk, *nchk;
u_int32_t cumulative_tsn_p1, tsn;
int cnt, strmat, gap;
asoc = &stcb->asoc;
cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
cnt = 0;
chk = TAILQ_FIRST(&asoc->reasmqueue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (compare_with_wrap(chk->rec.data.TSN_seq,
cumulative_tsn_p1, MAX_TSN)) {
cnt++;
tsn = chk->rec.data.TSN_seq;
if (tsn >= asoc->mapping_array_base_tsn) {
gap = tsn - asoc->mapping_array_base_tsn;
} else {
gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
tsn + 1;
}
asoc->size_on_reasm_queue -= chk->send_size;
asoc->cnt_on_reasm_queue--;
SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
}
chk = nchk;
}
for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
chk = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (compare_with_wrap(chk->rec.data.TSN_seq,
cumulative_tsn_p1, MAX_TSN)) {
cnt++;
tsn = chk->rec.data.TSN_seq;
if (tsn >= asoc->mapping_array_base_tsn) {
gap = tsn -
asoc->mapping_array_base_tsn;
} else {
gap = (MAX_TSN -
asoc->mapping_array_base_tsn) +
tsn + 1;
}
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
gap);
TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
}
chk = nchk;
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_PCB1) {
if (cnt) {
printf("Freed %d chunks from reneg harvest\n", cnt);
}
}
#endif
}
void
sctp_drain(void)
{
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
SCTP_INP_INFO_RLOCK();
LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_drain_mbufs(inp, stcb);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
SCTP_INP_INFO_RUNLOCK();
}
int
sctp_add_to_socket_q(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
{
struct sctp_socket_q_list *sq;
if ((inp == NULL) || (stcb == NULL)) {
return (0);
}
sq = (struct sctp_socket_q_list *)SCTP_ZONE_GET(
sctppcbinfo.ipi_zone_sockq);
if (sq == NULL) {
return (0);
}
sctppcbinfo.ipi_count_sockq++;
sctppcbinfo.ipi_gencnt_sockq++;
if (stcb)
stcb->asoc.cnt_msg_on_sb++;
sq->tcb = stcb;
TAILQ_INSERT_TAIL(&inp->sctp_queue_list, sq, next_sq);
return (1);
}
struct sctp_tcb *
sctp_remove_from_socket_q(struct sctp_inpcb *inp)
{
struct sctp_tcb *stcb = NULL;
struct sctp_socket_q_list *sq;
sq = TAILQ_FIRST(&inp->sctp_queue_list);
if (sq == NULL)
return (NULL);
stcb = sq->tcb;
TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
sctppcbinfo.ipi_count_sockq--;
sctppcbinfo.ipi_gencnt_sockq++;
if (stcb) {
stcb->asoc.cnt_msg_on_sb--;
}
return (stcb);
}
int
sctp_initiate_iterator(asoc_func af, uint32_t pcb_state, uint32_t asoc_state,
void *argp, uint32_t argi, end_func ef,
struct sctp_inpcb *s_inp)
{
struct sctp_iterator *it=NULL;
int s;
if (af == NULL) {
return (-1);
}
it = malloc(sizeof(struct sctp_iterator), M_PCB, M_WAITOK);
if (it == NULL) {
return (ENOMEM);
}
memset(it, 0, sizeof(*it));
it->function_toapply = af;
it->function_atend = ef;
it->pointer = argp;
it->val = argi;
it->pcb_flags = pcb_state;
it->asoc_state = asoc_state;
if (s_inp) {
it->inp = s_inp;
it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
} else {
SCTP_INP_INFO_RLOCK();
it->inp = LIST_FIRST(&sctppcbinfo.listhead);
SCTP_INP_INFO_RUNLOCK();
it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
}
callout_init(&it->tmr.timer, 0);
SCTP_INP_INFO_WLOCK();
LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
SCTP_INP_INFO_WUNLOCK();
s = splsoftnet();
sctp_iterator_timer(it);
splx(s);
return (0);
}