#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sctp_output.c,v 1.39 2025/06/12 06:08:10 ozaki-r 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/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/resourcevar.h>
#include <sys/uio.h>
#ifdef INET6
#include <sys/domain.h>
#endif
#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/nd6.h>
#include <netinet6/in6_pcb.h>
#include <netinet/icmp6.h>
#endif
#if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
#ifndef in6pcb
#define in6pcb inpcb
#endif
#endif
#include <netinet/sctp_pcb.h>
#ifdef IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#endif
#include <netinet/sctp_var.h>
#include <netinet/sctp_header.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_pcb.h>
#include <netinet/sctp_output.h>
#include <netinet/sctp_uio.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_hashdriver.h>
#include <netinet/sctp_timer.h>
#include <netinet/sctp_asconf.h>
#include <netinet/sctp_indata.h>
#ifdef SCTP_DEBUG
extern uint32_t sctp_debug_on;
#endif
extern int sctp_peer_chunk_oh;
static int
sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
{
struct cmsghdr cmh;
int tlen, at;
tlen = control->m_len;
at = 0;
while (at < tlen) {
if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
return (0);
}
m_copydata(control, at, sizeof(cmh), (void *)&cmh);
if ((cmh.cmsg_len + at) > tlen) {
return (0);
}
if ((cmh.cmsg_level == IPPROTO_SCTP) &&
(c_type == cmh.cmsg_type)) {
at += CMSG_ALIGN(sizeof(struct cmsghdr));
if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
return (0);
}
m_copydata(control, at, cpsize, data);
return (1);
} else {
at += CMSG_ALIGN(cmh.cmsg_len);
if (cmh.cmsg_len == 0) {
break;
}
}
}
return (0);
}
static struct mbuf *
sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
{
struct sctp_paramhdr *parmh;
struct mbuf *mret;
int len;
if (ifa->ifa_addr->sa_family == AF_INET) {
len = sizeof(struct sctp_ipv4addr_param);
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
len = sizeof(struct sctp_ipv6addr_param);
} else {
return (m);
}
if (M_TRAILINGSPACE(m) >= len) {
parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
mret = m;
} else {
mret = m;
while (mret->m_next != NULL) {
mret = mret->m_next;
}
MGET(mret->m_next, M_DONTWAIT, MT_DATA);
if (mret->m_next == NULL) {
return (m);
}
mret = mret->m_next;
parmh = mtod(mret, struct sctp_paramhdr *);
}
if (ifa->ifa_addr->sa_family == AF_INET) {
struct sctp_ipv4addr_param *ipv4p;
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)ifa->ifa_addr;
ipv4p = (struct sctp_ipv4addr_param *)parmh;
parmh->param_type = htons(SCTP_IPV4_ADDRESS);
parmh->param_length = htons(len);
ipv4p->addr = sin->sin_addr.s_addr;
mret->m_len += len;
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
struct sctp_ipv6addr_param *ipv6p;
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
ipv6p = (struct sctp_ipv6addr_param *)parmh;
parmh->param_type = htons(SCTP_IPV6_ADDRESS);
parmh->param_length = htons(len);
memcpy(ipv6p->addr, &sin6->sin6_addr,
sizeof(ipv6p->addr));
in6_clearscope((struct in6_addr *)ipv6p->addr);
mret->m_len += len;
} else {
return (m);
}
return (mret);
}
static struct mbuf *
sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
{
struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
struct sctp_state_cookie *stc;
struct sctp_paramhdr *ph;
uint8_t *signature;
int sig_offset;
uint16_t cookie_sz;
mret = NULL;
MGET(mret, M_DONTWAIT, MT_DATA);
if (mret == NULL) {
return (NULL);
}
copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
if (copy_init == NULL) {
sctp_m_freem(mret);
return (NULL);
}
copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
M_DONTWAIT);
if (copy_initack == NULL) {
sctp_m_freem(mret);
sctp_m_freem(copy_init);
return (NULL);
}
ph = mtod(mret, struct sctp_paramhdr *);
mret->m_len = sizeof(struct sctp_state_cookie) +
sizeof(struct sctp_paramhdr);
stc = (struct sctp_state_cookie *)((vaddr_t)ph +
sizeof(struct sctp_paramhdr));
ph->param_type = htons(SCTP_STATE_COOKIE);
ph->param_length = 0;
*stc = *stc_in;
cookie_sz = 0;
m_at = mret;
for (m_at = mret; m_at; m_at = m_at->m_next) {
cookie_sz += m_at->m_len;
if (m_at->m_next == NULL) {
m_at->m_next = copy_init;
break;
}
}
for (m_at = copy_init; m_at; m_at = m_at->m_next) {
cookie_sz += m_at->m_len;
if (m_at->m_next == NULL) {
m_at->m_next = copy_initack;
break;
}
}
for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
cookie_sz += m_at->m_len;
if (m_at->m_next == NULL) {
break;
}
}
MGET(sig, M_DONTWAIT, MT_DATA);
if (sig == NULL) {
sctp_m_freem(mret);
sctp_m_freem(copy_init);
sctp_m_freem(copy_initack);
return (NULL);
}
sig->m_len = 0;
m_at->m_next = sig;
sig_offset = 0;
signature = (uint8_t *)(mtod(sig, vaddr_t) + sig_offset);
sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
(int)(inp->sctp_ep.current_secret_number)],
SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
(uint8_t *)signature);
sig->m_len += SCTP_SIGNATURE_SIZE;
cookie_sz += SCTP_SIGNATURE_SIZE;
ph->param_length = htons(cookie_sz);
return (mret);
}
static struct sockaddr_in *
sctp_is_v4_ifa_addr_prefered (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
{
struct sockaddr_in *sin;
if (ifa->ifa_addr->sa_family != AF_INET) {
return (NULL);
}
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (sin->sin_addr.s_addr == 0) {
return (NULL);
}
*sin_local = *sin_loop = 0;
if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
(IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
*sin_loop = 1;
*sin_local = 1;
}
if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
*sin_local = 1;
}
if (!loopscope && *sin_loop) {
return (NULL);
}
if (!ipv4_scope && *sin_local) {
return (NULL);
}
if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
return (NULL);
}
return (sin);
}
static struct sockaddr_in *
sctp_is_v4_ifa_addr_acceptable (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
{
struct sockaddr_in *sin;
if (ifa->ifa_addr->sa_family != AF_INET) {
return (NULL);
}
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (sin->sin_addr.s_addr == 0) {
return (NULL);
}
*sin_local = *sin_loop = 0;
if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
(IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
*sin_loop = 1;
*sin_local = 1;
}
if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
*sin_local = 1;
}
if (!loopscope && *sin_loop) {
return (NULL);
}
return (sin);
}
int
sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
{
struct sctp_laddr *laddr;
#ifdef SCTP_DEBUG
int cnt=0;
#endif
if (stcb == NULL) {
return (0);
}
#ifdef SCTP_DEBUG
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
cnt++;
}
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("There are %d addresses on the restricted list\n", cnt);
}
cnt = 0;
#endif
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Help I have fallen and I can't get up!\n");
}
#endif
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
cnt++;
printf("Restricted address[%d]:", cnt);
sctp_print_address(laddr->ifa->ifa_addr);
}
#endif
if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
return (1);
}
}
return (0);
}
static int
sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
{
struct sctp_laddr *laddr;
if (ifa == NULL)
return (0);
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Help I have fallen and I can't get up!\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr == NULL)
continue;
if (laddr->ifa == ifa)
return (1);
if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
continue;
}
if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
return (1);
}
}
return (0);
}
static struct in_addr
sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
struct rtentry *rt,
uint8_t ipv4_scope,
uint8_t loopscope)
{
struct in_addr ans;
struct sctp_laddr *laddr;
struct sockaddr_in *sin;
struct ifnet *ifn;
struct ifaddr *ifa;
uint8_t sin_loop, sin_local;
ifn = rt->rt_ifp;
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (sctp_is_addr_in_ep(inp, ifa)) {
return (sin->sin_addr);
}
}
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (sctp_is_addr_in_ep(inp, ifa)) {
return (sin->sin_addr);
}
}
}
for (laddr = LIST_FIRST(&inp->sctp_addr_list);
laddr && (laddr != inp->next_addr_touse);
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
return (sin->sin_addr);
}
for (laddr = LIST_FIRST(&inp->sctp_addr_list);
laddr && (laddr != inp->next_addr_touse);
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
return (sin->sin_addr);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Src address selection for EP, no acceptable src address found for address\n");
}
#endif
memset(&ans, 0, sizeof(ans));
return (ans);
}
static struct in_addr
sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
struct rtentry *rt,
uint8_t ipv4_scope,
uint8_t loopscope,
int non_asoc_addr_ok)
{
struct sctp_laddr *laddr, *starting_point;
struct in_addr ans;
struct ifnet *ifn;
struct ifaddr *ifa;
uint8_t sin_loop, sin_local, start_at_beginning=0;
struct sockaddr_in *sin;
ifn = rt->rt_ifp;
if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
}
#endif
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_is_addr_in_ep(inp, ifa)) {
sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if ((non_asoc_addr_ok == 0) &&
(sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
continue;
}
return (sin->sin_addr);
}
}
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_is_addr_in_ep(inp, ifa)) {
sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if ((non_asoc_addr_ok == 0) &&
(sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
continue;
}
return (sin->sin_addr);
}
}
}
starting_point = stcb->asoc.last_used_address;
sctpv4_from_the_top:
if (stcb->asoc.last_used_address == NULL) {
start_at_beginning=1;
stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
}
for (laddr = stcb->asoc.last_used_address; laddr;
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if ((non_asoc_addr_ok == 0) &&
(sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
continue;
}
return (sin->sin_addr);
}
if (start_at_beginning == 0) {
stcb->asoc.last_used_address = NULL;
goto sctpv4_from_the_top;
}
stcb->asoc.last_used_address = starting_point;
start_at_beginning = 0;
sctpv4_from_the_top2:
if (stcb->asoc.last_used_address == NULL) {
start_at_beginning=1;
stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
}
for (laddr = stcb->asoc.last_used_address; laddr;
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if ((non_asoc_addr_ok == 0) &&
(sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
continue;
}
return (sin->sin_addr);
}
if (start_at_beginning == 0) {
stcb->asoc.last_used_address = NULL;
goto sctpv4_from_the_top2;
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
}
#endif
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (laddr->ifa == ifa) {
sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
return (sin->sin_addr);
}
if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
return (sin->sin_addr);
}
}
}
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (laddr->ifa == ifa) {
sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
return (sin->sin_addr);
}
if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
return (sin->sin_addr);
}
}
}
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
return (sin->sin_addr);
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
return (sin->sin_addr);
}
}
memset(&ans, 0, sizeof(ans));
return (ans);
}
static struct sockaddr_in *
sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
{
struct ifaddr *ifa;
struct sockaddr_in *sin;
uint8_t sin_loop, sin_local;
int num_eligible_addr = 0;
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
continue;
}
}
if (cur_addr_num == num_eligible_addr) {
return (sin);
}
}
return (NULL);
}
static int
sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
{
struct ifaddr *ifa;
struct sockaddr_in *sin;
int num_eligible_addr = 0;
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
if (sin == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
continue;
}
}
num_eligible_addr++;
}
return (num_eligible_addr);
}
static struct in_addr
sctp_choose_v4_boundall(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
struct rtentry *rt,
uint8_t ipv4_scope,
uint8_t loopscope,
int non_asoc_addr_ok)
{
int cur_addr_num=0, num_prefered=0;
uint8_t sin_loop, sin_local;
struct ifnet *ifn;
struct sockaddr_in *sin;
struct in_addr ans;
struct ifaddr *ifa;
int s;
ifn = rt->rt_ifp;
if (net) {
cur_addr_num = net->indx_of_eligible_next_to_use;
}
if (ifn == NULL) {
goto bound_all_v4_plan_c;
}
num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Found %d prefered source addresses\n", num_prefered);
}
#endif
if (num_prefered == 0) {
goto bound_all_v4_plan_b;
}
if (cur_addr_num >= num_prefered) {
cur_addr_num = 0;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("cur_addr_num:%d\n", cur_addr_num);
}
#endif
sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
ipv4_scope, cur_addr_num);
if (sin) {
return (sin->sin_addr);
}
bound_all_v4_plan_b:
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
continue;
}
}
return (sin->sin_addr);
}
bound_all_v4_plan_c:
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if (ifn == inp->next_ifn_touse)
break;
if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
continue;
}
if (ifn == rt->rt_ifp)
continue;
num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
loopscope, ipv4_scope, &sin_loop, &sin_local);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Found ifn:%p %d prefered source addresses\n", ifn, num_prefered);
}
#endif
if (num_prefered == 0) {
continue;
}
if (cur_addr_num >= num_prefered) {
cur_addr_num = 0;
}
sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
ipv4_scope, cur_addr_num);
if (sin == NULL)
continue;
pserialize_read_exit(s);
return (sin->sin_addr);
}
pserialize_read_exit(s);
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if (ifn == inp->next_ifn_touse)
break;
if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
continue;
}
if (ifn == rt->rt_ifp)
continue;
IFADDR_READER_FOREACH(ifa, ifn) {
sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
if (sin == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
continue;
}
}
pserialize_read_exit(s);
return (sin->sin_addr);
}
}
pserialize_read_exit(s);
if (non_asoc_addr_ok) {
return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
} else {
memset(&ans, 0, sizeof(ans));
return (ans);
}
}
struct in_addr
sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
int non_asoc_addr_ok)
{
struct in_addr ans;
const struct sockaddr_in *to;
struct rtentry *rt;
uint8_t ipv4_scope, loopscope;
rt = rtcache_validate(ro);
if (rt == NULL) {
memset(&ans, 0, sizeof(ans));
return (ans);
} else {
to = satocsin(rtcache_getdst(ro));
}
if (stcb) {
ipv4_scope = stcb->asoc.ipv4_local_scope;
loopscope = stcb->asoc.loopback_scope;
} else {
if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
ipv4_scope = 1;
loopscope = 0;
} else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
ipv4_scope = 1;
loopscope = 1;
} else {
ipv4_scope = 0;
loopscope = 0;
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Scope setup loop:%d ipv4_scope:%d\n",
loopscope, ipv4_scope);
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
ans = sctp_choose_v4_boundall(inp, stcb, net, rt,
ipv4_scope, loopscope, non_asoc_addr_ok);
goto out;
}
if (stcb) {
ans = sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
rt, ipv4_scope, loopscope, non_asoc_addr_ok);
goto out;
} else {
ans = sctp_choose_v4_boundspecific_inp(inp, rt,
ipv4_scope, loopscope);
goto out;
}
memset(&ans, 0, sizeof(ans));
out:
rtcache_unref(rt, ro);
return ans;
}
static struct sockaddr_in6 *
sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
{
struct in6_ifaddr *ifa6;
struct sockaddr_in6 *sin6;
if (ifa->ifa_addr->sa_family != AF_INET6) {
return (NULL);
}
ifa6 = (struct in6_ifaddr *)ifa;
if (!ip6_use_deprecated) {
if (IFA6_IS_DEPRECATED(ifa6)) {
return (NULL);
}
}
if (ifa6->ia6_flags &
(IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
return (NULL);
}
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
*sin_local = *sin_loop = 0;
if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
(IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
*sin_loop = 1;
}
if (!loopscope && *sin_loop) {
return (NULL);
}
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
return (NULL);
}
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
*sin_local = 1;
}
if (!loc_scope && *sin_local) {
return (NULL);
}
return (sin6);
}
static struct sockaddr_in6 *
sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
struct rtentry *rt,
uint8_t loc_scope,
uint8_t loopscope,
int non_asoc_addr_ok)
{
struct sctp_laddr *laddr, *starting_point;
struct sockaddr_in6 *sin6;
int sin_loop, sin_local;
int start_at_beginning=0;
struct ifnet *ifn;
struct ifaddr *ifa;
ifn = rt->rt_ifp;
if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
}
#endif
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_is_addr_in_ep(inp, ifa)) {
sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if ((non_asoc_addr_ok == 0) &&
(sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
continue;
}
return (sin6);
}
}
}
starting_point = stcb->asoc.last_used_address;
sctp_from_the_top:
if (stcb->asoc.last_used_address == NULL) {
start_at_beginning=1;
stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
}
for (laddr = stcb->asoc.last_used_address; laddr;
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
continue;
}
if ((loopscope == 0) &&
(loc_scope == 0) &&
(sin_loop == 0) &&
(sin_local == 0)) {
return (sin6);
}
if (loopscope && sin_loop)
return (sin6);
if (loc_scope && sin_local)
return (sin6);
}
if (start_at_beginning == 0) {
stcb->asoc.last_used_address = NULL;
goto sctp_from_the_top;
}
stcb->asoc.last_used_address = starting_point;
start_at_beginning = 0;
sctp_from_the_top2:
if (stcb->asoc.last_used_address == NULL) {
start_at_beginning=1;
stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
}
for (laddr = stcb->asoc.last_used_address; laddr;
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
continue;
}
return (sin6);
}
if (start_at_beginning == 0) {
stcb->asoc.last_used_address = NULL;
goto sctp_from_the_top2;
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
}
#endif
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
if (laddr->ifa == ifa) {
sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
return (sin6);
}
if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
return (sin6);
}
}
}
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if ((loopscope == 0) &&
(loc_scope == 0) &&
(sin_loop == 0) &&
(sin_local == 0)) {
return (sin6);
}
if (loopscope && sin_loop)
return (sin6);
if (loc_scope && sin_local)
return (sin6);
}
LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
return (sin6);
}
}
return (NULL);
}
static struct sockaddr_in6 *
sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
struct rtentry *rt,
uint8_t loc_scope,
uint8_t loopscope)
{
struct sctp_laddr *laddr;
struct sockaddr_in6 *sin6;
struct ifnet *ifn;
struct ifaddr *ifa;
int sin_loop, sin_local;
ifn = rt->rt_ifp;
if (ifn) {
IFADDR_READER_FOREACH(ifa, ifn) {
sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if (sctp_is_addr_in_ep(inp, ifa)) {
return (sin6);
}
}
}
for (laddr = LIST_FIRST(&inp->sctp_addr_list);
laddr && (laddr != inp->next_addr_touse);
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if ((loopscope == 0) &&
(loc_scope == 0) &&
(sin_loop == 0) &&
(sin_local == 0)) {
return (sin6);
}
if (loopscope && sin_loop)
return (sin6);
if (loc_scope && sin_local)
return (sin6);
}
for (laddr = LIST_FIRST(&inp->sctp_addr_list);
laddr && (laddr != inp->next_addr_touse);
laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
if (laddr->ifa == NULL) {
continue;
}
sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
return (sin6);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Src address selection for EP, no acceptable src address found for address\n");
}
#endif
return (NULL);
}
static struct sockaddr_in6 *
sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
uint8_t loc_scope, int cur_addr_num, int match_scope)
{
struct ifaddr *ifa;
struct sockaddr_in6 *sin6;
int sin_loop, sin_local;
int num_eligible_addr = 0;
IFADDR_READER_FOREACH(ifa, ifn) {
sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
continue;
}
}
if (match_scope) {
if (loopscope && sin_loop)
return (sin6);
if (loc_scope && sin_local)
return (sin6);
if ((loopscope == 0) &&
(loc_scope == 0) &&
(sin_loop == 0) &&
(sin_local == 0)) {
return (sin6);
}
continue;
}
if (num_eligible_addr == cur_addr_num) {
return (sin6);
}
num_eligible_addr++;
}
return (NULL);
}
static int
sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
{
struct ifaddr *ifa;
struct sockaddr_in6 *sin6;
int num_eligible_addr = 0;
int sin_loop, sin_local;
IFADDR_READER_FOREACH(ifa, ifn) {
sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
if (sin6 == NULL)
continue;
if (stcb) {
if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
continue;
}
}
num_eligible_addr++;
}
return (num_eligible_addr);
}
static struct sockaddr_in6 *
sctp_choose_v6_boundall(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
struct rtentry *rt,
uint8_t loc_scope,
uint8_t loopscope,
int non_asoc_addr_ok)
{
int num_eligible_addr;
int cur_addr_num=0;
int started_at_beginning=0;
int match_scope_prefered;
struct ifnet *ifn;
struct sockaddr_in6 *sin6;
int s;
ifn = rt->rt_ifp;
if (net) {
cur_addr_num = net->indx_of_eligible_next_to_use;
}
if (cur_addr_num == 0) {
match_scope_prefered = 1;
} else {
match_scope_prefered = 0;
}
num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Found %d eligible source addresses\n", num_eligible_addr);
}
#endif
if (num_eligible_addr == 0) {
goto bound_all_v6_plan_b;
}
if (cur_addr_num >= num_eligible_addr) {
cur_addr_num = 0;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("cur_addr_num:%d match_scope_prefered:%d select it\n",
cur_addr_num, match_scope_prefered);
}
#endif
sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
loc_scope, cur_addr_num, match_scope_prefered);
if (match_scope_prefered && (sin6 == NULL)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("retry with no match_scope_prefered\n");
}
#endif
sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
loc_scope, cur_addr_num, 0);
}
if (sin6) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
}
#endif
if (net) {
if (cur_addr_num < 255)
net->indx_of_eligible_next_to_use = cur_addr_num + 1;
else
net->indx_of_eligible_next_to_use = 0;
}
return (sin6);
}
num_eligible_addr = 0;
bound_all_v6_plan_b:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("bound-all Plan B\n");
}
#endif
if (inp->next_ifn_touse == NULL) {
started_at_beginning=1;
inp->next_ifn_touse = IFNET_READER_FIRST();
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Start at first IFN:%p\n", inp->next_ifn_touse);
}
#endif
} else {
inp->next_ifn_touse = IFNET_READER_NEXT(inp->next_ifn_touse);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Resume at IFN:%p\n", inp->next_ifn_touse);
}
#endif
if (inp->next_ifn_touse == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("IFN Resets\n");
}
#endif
started_at_beginning=1;
inp->next_ifn_touse = IFNET_READER_FIRST();
}
}
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
continue;
}
if (loc_scope && (ifn->if_index != loc_scope)) {
continue;
}
if (ifn == rt->rt_ifp) {
continue;
}
num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
}
#endif
if (num_eligible_addr == 0) {
continue;
}
inp->next_ifn_touse = ifn;
sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
if (sin6 == NULL) {
sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
if (sin6 == NULL)
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Selected the %d'th address of ifn:%p\n",
cur_addr_num, ifn);
}
#endif
pserialize_read_exit(s);
return (sin6);
}
pserialize_read_exit(s);
if (started_at_beginning == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Force a recycle\n");
}
#endif
inp->next_ifn_touse = NULL;
goto bound_all_v6_plan_b;
}
return (NULL);
}
struct in6_addr
sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
int non_asoc_addr_ok)
{
struct in6_addr ans;
struct sockaddr_in6 *rt_addr;
uint8_t loc_scope, loopscope;
struct sockaddr_in6 to;
struct rtentry *rt;
rt = rtcache_validate(ro);
if (rt == NULL) {
int scope_save;
memcpy(&to, rtcache_getdst(ro), sizeof(struct sockaddr));
scope_save = to.sin6_scope_id;
to.sin6_scope_id = 0;
rt = rtcache_lookup(ro, (struct sockaddr *)&to);
to.sin6_scope_id = scope_save;
}
if (rt == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("No route to host, this packet cannot be sent!\n");
}
#endif
memset(&ans, 0, sizeof(ans));
return (ans);
}
loc_scope = loopscope = 0;
if (IN6_IS_ADDR_LOOPBACK(&to.sin6_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Loopback scope is set\n");
}
#endif
loc_scope = 0;
loopscope = 1;
if (net != NULL) {
net->addr_is_local = 1;
}
} else if (IN6_IS_ADDR_LINKLOCAL(&to.sin6_addr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Link local scope is set, id:%d\n", to.sin6_scope_id);
}
#endif
if (to.sin6_scope_id)
loc_scope = to.sin6_scope_id;
else {
loc_scope = 1;
}
loopscope = 0;
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Global scope is set\n");
}
#endif
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Destination address:");
sctp_print_address((struct sockaddr *)&to);
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Calling bound-all src addr selection for v6\n");
}
#endif
rt_addr = sctp_choose_v6_boundall(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Calling bound-specific src addr selection for v6\n");
}
#endif
if (stcb)
rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
else
rt_addr = sctp_choose_v6_boundspecific_inp(inp, rt, loc_scope, loopscope);
}
rtcache_unref(rt, ro);
if (rt_addr == NULL) {
struct in6_addr in6;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("V6 packet will reach dead-end no suitable src address\n");
}
#endif
memset(&in6, 0, sizeof(in6));
return (in6);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Source address selected is:");
sctp_print_address((struct sockaddr *)rt_addr);
}
#endif
return (rt_addr->sin6_addr);
}
static uint8_t
sctp_get_ect(struct sctp_tcb *stcb,
struct sctp_tmit_chunk *chk)
{
uint8_t this_random;
if (sctp_ecn == 0)
return (0);
if (sctp_ecn_nonce == 0)
return (SCTP_ECT0_BIT);
if (stcb->asoc.peer_supports_ecn_nonce == 0) {
return (SCTP_ECT0_BIT);
}
if (chk == NULL)
return (SCTP_ECT0_BIT);
if (((stcb->asoc.hb_random_idx == 3) &&
(stcb->asoc.hb_ect_randombit > 7)) ||
(stcb->asoc.hb_random_idx > 3)) {
uint32_t rndval;
rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
memcpy(stcb->asoc.hb_random_values, &rndval,
sizeof(stcb->asoc.hb_random_values));
this_random = stcb->asoc.hb_random_values[0];
stcb->asoc.hb_random_idx = 0;
stcb->asoc.hb_ect_randombit = 0;
} else {
if (stcb->asoc.hb_ect_randombit > 7) {
stcb->asoc.hb_ect_randombit = 0;
stcb->asoc.hb_random_idx++;
}
this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
}
if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
if (chk != NULL)
chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
stcb->asoc.hb_ect_randombit++;
return (SCTP_ECT1_BIT);
} else {
stcb->asoc.hb_ect_randombit++;
return (SCTP_ECT0_BIT);
}
}
extern int sctp_no_csum_on_loopback;
static int
sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
const struct sockaddr *to,
struct mbuf *m,
int nofragment_flag,
int ecn_ok,
struct sctp_tmit_chunk *chk,
int out_of_asoc_ok)
{
struct sctphdr *sctphdr;
int o_flgs;
uint32_t csum;
int ret;
unsigned int have_mtu;
struct route *ro;
struct rtentry *rt;
if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
sctp_m_freem(m);
return (EFAULT);
}
if ((m->m_flags & M_PKTHDR) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
}
#endif
sctp_m_freem(m);
return (EFAULT);
}
sctphdr = mtod(m, struct sctphdr *);
have_mtu = 0;
if (sctp_no_csum_on_loopback &&
(stcb) &&
(stcb->asoc.loopback_scope)) {
sctphdr->checksum = 0;
m->m_pkthdr.len = sctp_calculate_len(m);
} else {
sctphdr->checksum = 0;
csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
sctphdr->checksum = csum;
}
if (to->sa_family == AF_INET) {
struct ip *ip;
static struct route iproute;
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
if (m == NULL) {
return (ENOMEM);
}
ip = mtod(m, struct ip *);
ip->ip_v = IPVERSION;
ip->ip_hl = (sizeof(struct ip) >> 2);
if (nofragment_flag) {
ip->ip_off = htons(IP_DF);
} else
ip->ip_off = 0;
ip->ip_id = htons(ip_newid());
ip->ip_ttl = inp->inp_ip_ttl;
ip->ip_len = htons(m->m_pkthdr.len);
if (stcb) {
if ((stcb->asoc.ecn_allowed) && ecn_ok) {
ip->ip_tos = (u_char)((in4p_ip(&inp->ip_inp.inp).ip_tos & 0x000000fc) |
sctp_get_ect(stcb, chk));
} else {
ip->ip_tos = in4p_ip(&inp->ip_inp.inp).ip_tos;
}
} else {
ip->ip_tos = inp->inp_ip_tos;
}
ip->ip_p = IPPROTO_SCTP;
ip->ip_sum = 0;
#ifdef SCTP_DEBUG
printf("chunk_output: net %p\n", net);
#endif
if (net == NULL) {
ro = &iproute;
memset(&iproute, 0, sizeof(iproute));
rt = rtcache_lookup(ro, to);
rtcache_unref(rt, ro);
} else {
ro = (struct route *)&net->ro;
}
ip->ip_dst.s_addr = satocsin(to)->sin_addr.s_addr;
if (net) {
if (net->src_addr_selected == 0) {
((struct sockaddr_in *)&net->_s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
stcb,
ro, net, out_of_asoc_ok);
rt = rtcache_validate(ro);
if (rt != NULL) {
net->src_addr_selected = 1;
}
rtcache_unref(rt, ro);
}
ip->ip_src = ((struct sockaddr_in *)&net->_s_addr)->sin_addr;
} else {
ip->ip_src = sctp_ipv4_source_address_selection(inp,
stcb, ro, net, out_of_asoc_ok);
}
#ifdef SCTP_DEBUG
printf("src addr %x\n", ip->ip_src.s_addr);
#endif
rt = rtcache_validate(ro);
if (rt == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("low_level_output: dropped v4 packet- no valid source addr\n");
printf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
}
#endif
if (net) {
if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
stcb,
SCTP_FAILED_THRESHOLD,
(void *)net);
net->dest_state &= ~SCTP_ADDR_REACHABLE;
net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
if (stcb) {
if (net == stcb->asoc.primary_destination) {
struct sctp_nets *alt;
alt = sctp_find_alternate_net(stcb, net);
if (alt != net) {
if (sctp_set_primary_addr(stcb,
(struct sockaddr *)NULL,
alt) == 0) {
net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
net->src_addr_selected = 0;
}
}
}
}
}
sctp_m_freem(m);
return (EHOSTUNREACH);
} else {
have_mtu = rt->rt_ifp->if_mtu;
}
o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Calling ipv4 output routine from low level src addr:%x\n",
(u_int)(ntohl(ip->ip_src.s_addr)));
printf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
printf("RTP route is %p through\n", rt);
printf("length %d\n", ntohs(ip->ip_len));
}
#endif
if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
rt->rt_ifp->if_mtu = net->mtu;
}
ret = ip_output(m, inp->ip_inp.inp.inp_options,
ro, o_flgs, inp->ip_inp.inp.inp_moptions,
&inp->ip_inp.inp);
if ((rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
rt->rt_ifp->if_mtu = have_mtu;
}
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Ip output returns %d\n", ret);
}
#endif
if (net == NULL) {
} else {
if (rt != NULL) {
if (rt->rt_rmx.rmx_mtu &&
(stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
sctp_mtu_size_reset(inp, &stcb->asoc,
rt->rt_rmx.rmx_mtu);
}
} else {
net->src_addr_selected = 0;
}
}
rtcache_unref(rt, ro);
return (ret);
}
#ifdef INET6
else if (to->sa_family == AF_INET6) {
struct ip6_hdr *ip6h;
static struct route ip6route;
struct ifnet *ifp;
u_char flowTop;
uint16_t flowBottom;
u_char tosBottom, tosTop;
struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
int prev_scope=0;
u_short prev_port=0;
M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
if (m == NULL) {
return (ENOMEM);
}
ip6h = mtod(m, struct ip6_hdr *);
flowBottom = in6p_flowinfo(inp) & 0x0000ffff;
flowTop = ((in6p_flowinfo(inp) & 0x000f0000) >> 16);
tosTop = (((in6p_flowinfo(inp) & 0xf0) >> 4) | IPV6_VERSION);
memcpy(&tmp, to, sizeof(struct sockaddr_in6));
sin6 = &tmp;
#if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
#else
if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
#endif
return (EINVAL);
if (net == NULL) {
memset(&ip6route, 0, sizeof(ip6route));
ro = (struct route *)&ip6route;
rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
rtcache_unref(rt, ro);
} else {
ro = (struct route *)&net->ro;
}
if (stcb != NULL) {
if ((stcb->asoc.ecn_allowed) && ecn_ok) {
tosBottom = (((in6p_flowinfo(inp) & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
} else {
tosBottom = ((in6p_flowinfo(inp) & 0x0c) << 4);
}
} else {
tosBottom = ((in6p_flowinfo(inp) & 0x0c) << 4);
}
ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
ip6h->ip6_nxt = IPPROTO_SCTP;
ip6h->ip6_plen = m->m_pkthdr.len;
ip6h->ip6_dst = sin6->sin6_addr;
memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
lsa6_tmp.sin6_family = AF_INET6;
lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
lsa6 = &lsa6_tmp;
rt = rtcache_validate(ro);
if (net) {
if (net->src_addr_selected == 0) {
((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
stcb, ro, net, out_of_asoc_ok);
if (rt != NULL) {
net->src_addr_selected = 1;
}
}
lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr;
} else {
lsa6->sin6_addr = sctp_ipv6_source_address_selection(
inp, stcb, ro, net, out_of_asoc_ok);
}
lsa6->sin6_port = inp->sctp_lport;
if (rt == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("low_level_output: dropped v6 pkt- no valid source addr\n");
}
#endif
sctp_m_freem(m);
if (net) {
if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
stcb,
SCTP_FAILED_THRESHOLD,
(void *)net);
net->dest_state &= ~SCTP_ADDR_REACHABLE;
net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
if (stcb) {
if (net == stcb->asoc.primary_destination) {
struct sctp_nets *alt;
alt = sctp_find_alternate_net(stcb, net);
if (alt != net) {
if (sctp_set_primary_addr(stcb,
(struct sockaddr *)NULL,
alt) == 0) {
net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
net->src_addr_selected = 0;
}
}
}
}
}
return (EHOSTUNREACH);
}
ip6h->ip6_src = lsa6->sin6_addr;
ip6h->ip6_hlim = in6pcb_selecthlim(&inp->ip_inp.inp,
(ro ?
(rt ? (rt->rt_ifp) : (NULL)) :
(NULL)));
o_flgs = 0;
ifp = rt->rt_ifp;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
sin6->sin6_addr = ip6h->ip6_dst;
lsa6->sin6_addr = ip6h->ip6_src;
printf("Calling ipv6 output routine from low level\n");
printf("src: ");
sctp_print_address((struct sockaddr *)lsa6);
printf("dst: ");
sctp_print_address((struct sockaddr *)sin6);
}
#endif
if (net) {
sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
prev_scope = sin6->sin6_scope_id;
prev_port = sin6->sin6_port;
}
rtcache_unref(rt, ro);
ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
ro,
o_flgs,
((struct in6pcb *)inp)->in6p_moptions,
(struct inpcb *)inp,
&ifp);
if (net) {
sin6->sin6_scope_id = prev_scope;
sin6->sin6_port = prev_port;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("return from send is %d\n", ret);
}
#endif
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
if (net) {
rt = rtcache_validate(ro);
if (rt == NULL) {
net->src_addr_selected = 0;
}
if (rt != NULL) {
if (rt->rt_rmx.rmx_mtu &&
(stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
sctp_mtu_size_reset(inp,
&stcb->asoc,
rt->rt_rmx.rmx_mtu);
}
rtcache_unref(rt, ro);
} else if (ifp) {
if (ifp->if_mtu &&
(stcb->asoc.smallest_mtu > ifp->if_mtu)) {
sctp_mtu_size_reset(inp,
&stcb->asoc,
ifp->if_mtu);
}
}
}
return (ret);
}
#endif
else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Unknown protocol (TSNH) type %d\n", ((const struct sockaddr *)to)->sa_family);
}
#endif
sctp_m_freem(m);
return (EFAULT);
}
}
static
int sctp_is_address_in_scope(struct ifaddr *ifa,
int ipv4_addr_legal,
int ipv6_addr_legal,
int loopback_scope,
int ipv4_local_scope,
int local_scope,
int site_scope)
{
if ((loopback_scope == 0) && (ifa->ifa_ifp->if_type == IFT_LOOP)) {
return (0);
}
if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (sin->sin_addr.s_addr == 0) {
return (0);
}
if ((ipv4_local_scope == 0) &&
(IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
return (0);
}
} else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
struct sockaddr_in6 *sin6;
struct in6_ifaddr *ifa6;
ifa6 = (struct in6_ifaddr *)ifa;
if (!ip6_use_deprecated) {
if (ifa6->ia6_flags &
IN6_IFF_DEPRECATED) {
return (0);
}
}
if (ifa6->ia6_flags &
(IN6_IFF_DETACHED |
IN6_IFF_ANYCAST |
IN6_IFF_NOTREADY)) {
return (0);
}
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
return (0);
}
if (
(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
return (0);
}
if ((site_scope == 0) &&
(IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
return (0);
}
} else {
return (0);
}
return (1);
}
void
sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
{
struct mbuf *m, *m_at, *m_last;
struct sctp_nets *net;
struct sctp_init_msg *initm;
struct sctp_supported_addr_param *sup_addr;
struct sctp_ecn_supported_param *ecn;
struct sctp_prsctp_supported_param *prsctp;
struct sctp_ecn_nonce_supported_param *ecn_nonce;
struct sctp_supported_chunk_types_param *pr_supported;
int cnt_inits_to=0;
int padval, ret;
m_last = NULL;
net = stcb->asoc.primary_destination;
if (net == NULL) {
net = TAILQ_FIRST(&stcb->asoc.nets);
if (net == NULL) {
return;
}
net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
sctp_set_primary_addr(stcb, NULL, net);
} else {
net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Sending INIT to ");
sctp_print_address (rtcache_getdst(&net->ro));
}
#endif
if (rtcache_getdst(&net->ro)->sa_family == AF_INET6) {
if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&net->ro)->sa_data))
cnt_inits_to = 1;
}
if (callout_pending(&net->rxt_timer.timer)) {
return;
}
if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
return;
}
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m == NULL) {
return;
}
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) != M_EXT) {
sctp_m_freem(m);
return;
}
m->m_data += SCTP_MIN_OVERHEAD;
m->m_len = sizeof(struct sctp_init_msg);
initm = mtod(m, struct sctp_init_msg *);
initm->sh.src_port = inp->sctp_lport;
initm->sh.dest_port = stcb->rport;
initm->sh.v_tag = 0;
initm->sh.checksum = 0;
initm->msg.ch.chunk_type = SCTP_INITIATION;
initm->msg.ch.chunk_flags = 0;
initm->msg.ch.chunk_length = 0;
initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
initm->msg.init.a_rwnd = htonl(uimax(inp->sctp_socket->so_rcv.sb_hiwat,
SCTP_MINIMAL_RWND));
initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
sup_addr = (struct sctp_supported_addr_param *)((vaddr_t)initm +
sizeof(*initm));
sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
sizeof(uint16_t));
sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
if (inp->sctp_ep.adaption_layer_indicator) {
struct sctp_adaption_layer_indication *ali;
ali = (struct sctp_adaption_layer_indication *)(
(vaddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
ali->ph.param_length = htons(sizeof(*ali));
ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
m->m_len += sizeof(*ali);
ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
sizeof(*ali));
} else {
ecn = (struct sctp_ecn_supported_param *)((vaddr_t)sup_addr +
sizeof(*sup_addr) + sizeof(uint16_t));
}
if (stcb->asoc.cookie_preserve_req) {
struct sctp_cookie_perserve_param *cookie_preserve;
cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
cookie_preserve->ph.param_length = htons(
sizeof(*cookie_preserve));
cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
m->m_len += sizeof(*cookie_preserve);
ecn = (struct sctp_ecn_supported_param *)(
(vaddr_t)cookie_preserve + sizeof(*cookie_preserve));
stcb->asoc.cookie_preserve_req = 0;
}
if (sctp_ecn == 1) {
ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
ecn->ph.param_length = htons(sizeof(*ecn));
m->m_len += sizeof(*ecn);
prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
sizeof(*ecn));
} else {
prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
}
prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
prsctp->ph.param_length = htons(sizeof(*prsctp));
m->m_len += sizeof(*prsctp);
pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
sizeof(*prsctp));
pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
pr_supported->chunk_types[0] = SCTP_ASCONF;
pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
pr_supported->chunk_types[5] = 0;
pr_supported->chunk_types[6] = 0;
pr_supported->chunk_types[7] = 0;
m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
if (sctp_ecn_nonce) {
ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
m->m_len += sizeof(*ecn_nonce);
}
m_at = m;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
struct ifnet *ifn;
struct ifaddr *ifa;
int cnt;
int s;
cnt = cnt_inits_to;
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 (sctp_is_address_in_scope(ifa,
stcb->asoc.ipv4_addr_legal,
stcb->asoc.ipv6_addr_legal,
stcb->asoc.loopback_scope,
stcb->asoc.ipv4_local_scope,
stcb->asoc.local_scope,
stcb->asoc.site_scope) == 0) {
continue;
}
cnt++;
}
}
pserialize_read_exit(s);
if (cnt > 1) {
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 (sctp_is_address_in_scope(ifa,
stcb->asoc.ipv4_addr_legal,
stcb->asoc.ipv6_addr_legal,
stcb->asoc.loopback_scope,
stcb->asoc.ipv4_local_scope,
stcb->asoc.local_scope,
stcb->asoc.site_scope) == 0) {
continue;
}
m_at = sctp_add_addr_to_mbuf(m_at, ifa);
}
}
pserialize_read_exit(s);
}
} else {
struct sctp_laddr *laddr;
int cnt;
cnt = cnt_inits_to;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
if (laddr->ifa->ifa_addr == NULL)
continue;
if (sctp_is_address_in_scope(laddr->ifa,
stcb->asoc.ipv4_addr_legal,
stcb->asoc.ipv6_addr_legal,
stcb->asoc.loopback_scope,
stcb->asoc.ipv4_local_scope,
stcb->asoc.local_scope,
stcb->asoc.site_scope) == 0) {
continue;
}
cnt++;
}
if (cnt > 1) {
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
if (laddr->ifa->ifa_addr == NULL) {
continue;
}
if (sctp_is_address_in_scope(laddr->ifa,
stcb->asoc.ipv4_addr_legal,
stcb->asoc.ipv6_addr_legal,
stcb->asoc.loopback_scope,
stcb->asoc.ipv4_local_scope,
stcb->asoc.local_scope,
stcb->asoc.site_scope) == 0) {
continue;
}
m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
}
}
}
m->m_pkthdr.len = 0;
for (m_at = m; m_at; m_at = m_at->m_next) {
if (m_at->m_next == NULL)
m_last = m_at;
m->m_pkthdr.len += m_at->m_len;
}
initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
sizeof(struct sctphdr)));
#ifdef SCTP_DEBUG
printf("chunk_length %d\n", ntohs(initm->msg.ch.chunk_length));
#endif
padval = m->m_pkthdr.len % 4;
if ((padval) && (m_last)) {
ret = sctp_add_pad_tombuf(m_last, (4-padval));
if (ret) {
sctp_m_freem(m);
return;
}
m->m_pkthdr.len += padval;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Calling lowlevel output stcb:%p net:%p\n",
stcb, net);
}
#endif
ret = sctp_lowlevel_chunk_output(inp, stcb, net,
rtcache_getdst(&net->ro), m, 0, 0, NULL, 0);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Low level output returns %d\n", ret);
}
#endif
sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
}
struct mbuf *
sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
{
struct sctp_paramhdr *phdr, params;
struct mbuf *mat, *op_err;
char tempbuf[2048];
int at, limit, pad_needed;
uint16_t ptype, plen;
int err_at;
*abort_processing = 0;
mat = in_initpkt;
err_at = 0;
limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Limit is %d bytes\n", limit);
}
#endif
at = param_offset;
op_err = NULL;
phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
ptype = ntohs(phdr->param_type);
plen = ntohs(phdr->param_length);
limit -= SCTP_SIZE32(plen);
if (plen < sizeof(struct sctp_paramhdr)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
}
#endif
*abort_processing = 1;
break;
}
if ((ptype == SCTP_HEARTBEAT_INFO) ||
(ptype == SCTP_IPV4_ADDRESS) ||
(ptype == SCTP_IPV6_ADDRESS) ||
(ptype == SCTP_STATE_COOKIE) ||
(ptype == SCTP_UNRECOG_PARAM) ||
(ptype == SCTP_COOKIE_PRESERVE) ||
(ptype == SCTP_SUPPORTED_ADDRTYPE) ||
(ptype == SCTP_PRSCTP_SUPPORTED) ||
(ptype == SCTP_ADD_IP_ADDRESS) ||
(ptype == SCTP_DEL_IP_ADDRESS) ||
(ptype == SCTP_ECN_CAPABLE) ||
(ptype == SCTP_ULP_ADAPTION) ||
(ptype == SCTP_ERROR_CAUSE_IND) ||
(ptype == SCTP_SET_PRIM_ADDR) ||
(ptype == SCTP_SUCCESS_REPORT) ||
(ptype == SCTP_ULP_ADAPTION) ||
(ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
(ptype == SCTP_ECN_NONCE_SUPPORTED)
) {
at += SCTP_SIZE32(plen);
} else if (ptype == SCTP_HOSTNAME_ADDRESS) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Can't handle hostname addresses.. abort processing\n");
}
#endif
*abort_processing = 1;
if (op_err == NULL) {
MGETHDR(op_err, M_DONTWAIT, MT_DATA);
if (op_err) {
op_err->m_len = 0;
op_err->m_pkthdr.len = 0;
op_err->m_data += sizeof(struct ip6_hdr);
op_err->m_data += sizeof(struct sctphdr);
op_err->m_data += sizeof(struct sctp_chunkhdr);
}
}
if (op_err) {
struct sctp_paramhdr s;
if (err_at % 4) {
u_int32_t cpthis=0;
pad_needed = 4 - (err_at % 4);
m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
err_at += pad_needed;
}
s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
s.param_length = htons(sizeof(s) + plen);
m_copyback(op_err, err_at, sizeof(s), (void *)&s);
err_at += sizeof(s);
phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
if (phdr == NULL) {
sctp_m_freem(op_err);
return (NULL);
}
m_copyback(op_err, err_at, plen, (void *)phdr);
err_at += plen;
}
return (op_err);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Got parameter type %x - unknown\n",
(u_int)ptype);
}
#endif
if ((ptype & 0x4000) == 0x4000) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Report bit is set\n");
}
#endif
if (op_err == NULL) {
MGETHDR(op_err, M_DONTWAIT, MT_DATA);
if (op_err) {
op_err->m_len = 0;
op_err->m_pkthdr.len = 0;
op_err->m_data += sizeof(struct ip6_hdr);
op_err->m_data += sizeof(struct sctphdr);
op_err->m_data += sizeof(struct sctp_chunkhdr);
}
}
if (op_err) {
struct sctp_paramhdr s;
if (err_at % 4) {
u_int32_t cpthis=0;
pad_needed = 4 - (err_at % 4);
m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
err_at += pad_needed;
}
s.param_type = htons(SCTP_UNRECOG_PARAM);
s.param_length = htons(sizeof(s) + plen);
m_copyback(op_err, err_at, sizeof(s), (void *)&s);
err_at += sizeof(s);
if (plen > sizeof(tempbuf)) {
plen = sizeof(tempbuf);
}
phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
if (phdr == NULL) {
sctp_m_freem(op_err);
goto more_processing;
}
m_copyback(op_err, err_at, plen, (void *)phdr);
err_at += plen;
}
}
more_processing:
if ((ptype & 0x8000) == 0x0000) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Abort bit is now setting1\n");
}
#endif
return (op_err);
} else {
at += SCTP_SIZE32(plen);
}
}
phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
}
return (op_err);
}
static int
sctp_are_there_new_addresses(struct sctp_association *asoc,
struct mbuf *in_initpkt, int iphlen, int offset)
{
struct sockaddr_in sin4, *sa4;
struct sockaddr_in6 sin6, *sa6;
struct sockaddr *sa_touse;
struct sockaddr *sa;
struct sctp_paramhdr *phdr, params;
struct ip *iph;
struct mbuf *mat;
uint16_t ptype, plen;
uint8_t fnd;
struct sctp_nets *net;
memset(&sin4, 0, sizeof(sin4));
memset(&sin6, 0, sizeof(sin6));
sin4.sin_family = AF_INET;
sin4.sin_len = sizeof(sin4);
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(sin6);
sa_touse = NULL;
iph = mtod(in_initpkt, struct ip *);
if (iph->ip_v == IPVERSION) {
sin4.sin_addr = iph->ip_src;
sa_touse = (struct sockaddr *)&sin4;
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6h;
ip6h = mtod(in_initpkt, struct ip6_hdr *);
sin6.sin6_addr = ip6h->ip6_src;
sa_touse = (struct sockaddr *)&sin6;
} else {
return (1);
}
fnd = 0;
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
sa = (struct sockaddr *)&net->ro.ro_sa;
if (sa->sa_family == sa_touse->sa_family) {
if (sa->sa_family == AF_INET) {
sa4 = (struct sockaddr_in *)sa;
if (sa4->sin_addr.s_addr ==
sin4.sin_addr.s_addr) {
fnd = 1;
break;
}
} else if (sa->sa_family == AF_INET6) {
sa6 = (struct sockaddr_in6 *)sa;
if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
&sin6.sin6_addr)) {
fnd = 1;
break;
}
}
}
}
if (fnd == 0) {
return (1);
}
mat = in_initpkt;
sa_touse = NULL;
offset += sizeof(struct sctp_init_chunk);
phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
while (phdr) {
ptype = ntohs(phdr->param_type);
plen = ntohs(phdr->param_length);
if (ptype == SCTP_IPV4_ADDRESS) {
struct sctp_ipv4addr_param *p4, p4_buf;
phdr = sctp_get_next_param(mat, 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;
sin4.sin_addr.s_addr = p4->addr;
sa_touse = (struct sockaddr *)&sin4;
} else if (ptype == SCTP_IPV6_ADDRESS) {
struct sctp_ipv6addr_param *p6, p6_buf;
phdr = sctp_get_next_param(mat, 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_touse = (struct sockaddr *)&sin4;
}
if (sa_touse) {
fnd = 0;
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
sa = (struct sockaddr *)&net->ro.ro_sa;
if (sa->sa_family != sa_touse->sa_family) {
continue;
}
if (sa->sa_family == AF_INET) {
sa4 = (struct sockaddr_in *)sa;
if (sa4->sin_addr.s_addr ==
sin4.sin_addr.s_addr) {
fnd = 1;
break;
}
} else if (sa->sa_family == AF_INET6) {
sa6 = (struct sockaddr_in6 *)sa;
if (SCTP6_ARE_ADDR_EQUAL(
&sa6->sin6_addr, &sin6.sin6_addr)) {
fnd = 1;
break;
}
}
}
if (!fnd) {
return (1);
}
}
offset += SCTP_SIZE32(plen);
phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
}
return (0);
}
void
sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
struct sctp_init_chunk *init_chk)
{
struct sctp_association *asoc;
struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
struct sctp_init_msg *initackm_out;
struct sctp_ecn_supported_param *ecn;
struct sctp_prsctp_supported_param *prsctp;
struct sctp_ecn_nonce_supported_param *ecn_nonce;
struct sctp_supported_chunk_types_param *pr_supported;
struct sockaddr_storage store;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct route *ro;
struct ip *iph;
struct ip6_hdr *ip6;
const struct sockaddr *to;
struct sctp_state_cookie stc;
struct sctp_nets *net=NULL;
int cnt_inits_to=0;
uint16_t his_limit, i_want;
int abort_flag, padval, sz_of;
struct rtentry *rt;
if (stcb) {
asoc = &stcb->asoc;
} else {
asoc = NULL;
}
m_last = NULL;
if ((asoc != NULL) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
(sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
return;
}
abort_flag = 0;
op_err = sctp_arethere_unrecognized_parameters(init_pkt,
(offset+sizeof(struct sctp_init_chunk)),
&abort_flag, (struct sctp_chunkhdr *)init_chk);
if (abort_flag) {
sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
return;
}
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m == NULL) {
sctp_m_freem(op_err);
return;
}
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) != M_EXT) {
sctp_m_freem(op_err);
sctp_m_freem(m);
return;
}
m->m_data += SCTP_MIN_OVERHEAD;
m_reset_rcvif(m);
m->m_len = sizeof(struct sctp_init_msg);
SCTP_GETTIME_TIMEVAL(&stc.time_entered);
if (asoc != NULL) {
SCTP_TCB_UNLOCK(stcb);
if (asoc->my_vtag_nonce == 0)
asoc->my_vtag_nonce = sctp_select_a_tag(inp);
stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
if (asoc->peer_vtag_nonce == 0)
asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
stc.cookie_life = asoc->cookie_life;
net = asoc->primary_destination;
SCTP_INP_RLOCK(inp);
if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
sctp_m_freem(op_err);
sctp_m_freem(m);
sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
return;
}
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(stcb->sctp_ep);
} else {
stc.tie_tag_my_vtag = 0;
stc.tie_tag_peer_vtag = 0;
stc.cookie_life = inp->sctp_ep.def_cookie_life;
}
stc.myport = sh->dest_port;
stc.peerport = sh->src_port;
stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
struct inpcb *in_inp;
in_inp = (struct inpcb *)inp;
stc.ipv6_addr_legal = 1;
if (
#if defined(__FreeBSD__) || defined(__APPLE__)
(in_inp->inp_flags & IN6P_IPV6_V6ONLY)
#elif defined(__OpenBSD__)
(0)
#else
(((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
#endif
== 0) {
stc.ipv4_addr_legal = 1;
} else {
stc.ipv4_addr_legal = 0;
}
} else {
stc.ipv4_addr_legal = 1;
stc.ipv6_addr_legal = 0;
}
#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
stc.ipv4_scope = 1;
#else
stc.ipv4_scope = 0;
#endif
memset((void *)&store, 0, sizeof(store));
sin = (struct sockaddr_in *)&store;
sin6 = (struct sockaddr_in6 *)&store;
if (net == NULL) {
to = (struct sockaddr *)&store;
iph = mtod(init_pkt, struct ip *);
if (iph->ip_v == IPVERSION) {
struct in_addr addr;
static struct route iproute;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_port = sh->src_port;
sin->sin_addr = iph->ip_src;
stc.address[0] = sin->sin_addr.s_addr;
stc.address[1] = 0;
stc.address[2] = 0;
stc.address[3] = 0;
stc.addr_type = SCTP_IPV4_ADDRESS;
memset(&iproute, 0, sizeof(iproute));
ro = &iproute;
rt = rtcache_lookup(ro, (struct sockaddr *) sin);
rtcache_unref(rt, ro);
addr = sctp_ipv4_source_address_selection(inp, NULL,
ro, NULL, 0);
stc.laddress[0] = addr.s_addr;
stc.laddress[1] = 0;
stc.laddress[2] = 0;
stc.laddress[3] = 0;
stc.laddr_type = SCTP_IPV4_ADDRESS;
stc.scope_id = 0;
#ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
stc.ipv4_scope = 1;
}
#else
stc.ipv4_scope = 1;
#endif
if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
stc.loopback_scope = 1;
stc.ipv4_scope = 1;
stc.site_scope = 1;
stc.local_scope = 1;
}
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct in6_addr addr;
static struct route iproute6;
ip6 = mtod(init_pkt, struct ip6_hdr *);
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_port = sh->src_port;
sin6->sin6_addr = ip6->ip6_src;
memcpy(&stc.address, &sin6->sin6_addr,
sizeof(struct in6_addr));
sin6->sin6_scope_id = 0;
stc.addr_type = SCTP_IPV6_ADDRESS;
stc.scope_id = 0;
if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
stc.loopback_scope = 1;
stc.local_scope = 1;
stc.site_scope = 1;
stc.ipv4_scope = 1;
} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
stc.local_scope = 0;
stc.site_scope = 1;
stc.ipv4_scope = 1;
cnt_inits_to=1;
#if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
(void)in6_recoverscope(sin6, &in6_src,
m_get_rcvif_NOMPSAFE(init_pkt));
in6_embedscope(&sin6->sin6_addr, sin6, NULL,
NULL);
#else
(void)sa6_recoverscope(sin6);
#endif
stc.scope_id = sin6->sin6_scope_id;
} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
stc.site_scope = 1;
}
memset(&iproute6, 0, sizeof(iproute6));
ro = (struct route *)&iproute6;
rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
rtcache_unref(rt, ro);
addr = sctp_ipv6_source_address_selection(inp, NULL,
ro, NULL, 0);
memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
stc.laddr_type = SCTP_IPV6_ADDRESS;
}
} else {
struct sctp_nets *lnet;
stc.loopback_scope = asoc->loopback_scope;
stc.ipv4_scope = asoc->ipv4_local_scope;
stc.site_scope = asoc->site_scope;
stc.local_scope = asoc->local_scope;
TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
if (rtcache_getdst(&lnet->ro)->sa_family == AF_INET6) {
if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&lnet->ro)->sa_data)) {
cnt_inits_to = 1;
}
}
}
to = rtcache_getdst(&net->ro);
if (to->sa_family == AF_INET) {
memcpy(&stc.address[0], to, sizeof(struct in_addr));
stc.address[1] = 0;
stc.address[2] = 0;
stc.address[3] = 0;
stc.addr_type = SCTP_IPV4_ADDRESS;
if (net->src_addr_selected == 0) {
net->_s_addr.sin.sin_addr =
sctp_ipv4_source_address_selection(inp,
stcb, &net->ro, net, 0);
net->src_addr_selected = 1;
}
stc.laddress[0] = net->_s_addr.sin.sin_addr.s_addr;
stc.laddress[1] = 0;
stc.laddress[2] = 0;
stc.laddress[3] = 0;
stc.laddr_type = SCTP_IPV4_ADDRESS;
} else if (to->sa_family == AF_INET6) {
memcpy(&stc.address, &to->sa_data,
sizeof(struct in6_addr));
stc.addr_type = SCTP_IPV6_ADDRESS;
if (net->src_addr_selected == 0) {
net->_s_addr.sin6.sin6_addr =
sctp_ipv6_source_address_selection(inp,
stcb, &net->ro, net, 0);
net->src_addr_selected = 1;
}
memcpy(&stc.laddress, &net->_s_addr.sin6.sin6_addr,
sizeof(struct in6_addr));
stc.laddr_type = SCTP_IPV6_ADDRESS;
}
}
initackm_out = mtod(m, struct sctp_init_msg *);
initackm_out->sh.src_port = inp->sctp_lport;
initackm_out->sh.dest_port = sh->src_port;
initackm_out->sh.v_tag = init_chk->init.initiate_tag;
stc.peers_vtag = init_chk->init.initiate_tag;
initackm_out->sh.checksum = 0;
strncpy(stc.identification, SCTP_VERSION_STRING,
uimin(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
initackm_out->msg.ch.chunk_flags = 0;
initackm_out->msg.ch.chunk_length = 0;
if ((asoc != NULL) &&
((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
} else {
initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
}
stc.my_vtag = initackm_out->msg.init.initiate_tag;
initackm_out->msg.init.a_rwnd = htonl(uimax(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
his_limit = ntohs(init_chk->init.num_inbound_streams);
if (asoc != NULL) {
if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
i_want = asoc->streamoutcnt;
} else {
i_want = inp->sctp_ep.pre_open_stream_count;
}
} else {
i_want = inp->sctp_ep.pre_open_stream_count;
}
if (his_limit < i_want) {
initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
} else {
initackm_out->msg.init.num_outbound_streams = htons(i_want);
}
initackm_out->msg.init.num_inbound_streams =
htons(inp->sctp_ep.max_open_streams_intome);
if (inp->sctp_ep.adaption_layer_indicator) {
struct sctp_adaption_layer_indication *ali;
ali = (struct sctp_adaption_layer_indication *)(
(vaddr_t)initackm_out + sizeof(*initackm_out));
ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
ali->ph.param_length = htons(sizeof(*ali));
ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
m->m_len += sizeof(*ali);
ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
sizeof(*ali));
} else {
ecn = (struct sctp_ecn_supported_param*)(
(vaddr_t)initackm_out + sizeof(*initackm_out));
}
if (sctp_ecn == 1) {
ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
ecn->ph.param_length = htons(sizeof(*ecn));
m->m_len += sizeof(*ecn);
prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
sizeof(*ecn));
} else {
prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
}
prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
prsctp->ph.param_length = htons(sizeof(*prsctp));
m->m_len += sizeof(*prsctp);
pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
sizeof(*prsctp));
pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
pr_supported->chunk_types[0] = SCTP_ASCONF;
pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
pr_supported->chunk_types[5] = 0;
pr_supported->chunk_types[6] = 0;
pr_supported->chunk_types[7] = 0;
m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
if (sctp_ecn_nonce) {
ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
m->m_len += sizeof(*ecn_nonce);
}
m_at = m;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
struct ifnet *ifn;
struct ifaddr *ifa;
int cnt = cnt_inits_to;
int s;
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if ((stc.loopback_scope == 0) &&
(ifn->if_type == IFT_LOOP)) {
continue;
}
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_is_address_in_scope(ifa,
stc.ipv4_addr_legal, stc.ipv6_addr_legal,
stc.loopback_scope, stc.ipv4_scope,
stc.local_scope, stc.site_scope) == 0) {
continue;
}
cnt++;
}
}
pserialize_read_exit(s);
if (cnt > 1) {
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifn) {
if ((stc.loopback_scope == 0) &&
(ifn->if_type == IFT_LOOP)) {
continue;
}
IFADDR_READER_FOREACH(ifa, ifn) {
if (sctp_is_address_in_scope(ifa,
stc.ipv4_addr_legal,
stc.ipv6_addr_legal,
stc.loopback_scope, stc.ipv4_scope,
stc.local_scope, stc.site_scope) == 0) {
continue;
}
m_at = sctp_add_addr_to_mbuf(m_at, ifa);
}
}
pserialize_read_exit(s);
}
} else {
struct sctp_laddr *laddr;
int cnt;
cnt = cnt_inits_to;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
continue;
}
if (laddr->ifa->ifa_addr == NULL)
continue;
if (sctp_is_address_in_scope(laddr->ifa,
stc.ipv4_addr_legal, stc.ipv6_addr_legal,
stc.loopback_scope, stc.ipv4_scope,
stc.local_scope, stc.site_scope) == 0) {
continue;
}
cnt++;
}
if (cnt > 1) {
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Help I have fallen and I can't get up!\n");
}
#endif
continue;
}
if (laddr->ifa->ifa_addr == NULL)
continue;
if (sctp_is_address_in_scope(laddr->ifa,
stc.ipv4_addr_legal, stc.ipv6_addr_legal,
stc.loopback_scope, stc.ipv4_scope,
stc.local_scope, stc.site_scope) == 0) {
continue;
}
m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
}
}
}
if (op_err) {
if (op_err->m_pkthdr.len % 4) {
u_int32_t cpthis=0;
int padlen;
padlen = 4 - (op_err->m_pkthdr.len % 4);
m_copyback(op_err, op_err->m_pkthdr.len, padlen, (void *)&cpthis);
}
while (m_at->m_next != NULL) {
m_at = m_at->m_next;
}
m_at->m_next = op_err;
while (m_at->m_next != NULL) {
m_at = m_at->m_next;
}
}
sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
m->m_pkthdr.len = 0;
for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
m->m_pkthdr.len += m_tmp->m_len;
if (m_tmp->m_next == NULL) {
break;
}
}
sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
sz_of += sizeof(struct sctp_state_cookie_param);
sz_of += SCTP_SIGNATURE_SIZE;
initackm_out->msg.ch.chunk_length = htons(sz_of);
m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
sizeof(struct sctphdr), &stc);
if (m_cookie == NULL) {
sctp_m_freem(m);
return;
}
m_tmp->m_next = m_cookie;
padval = m->m_pkthdr.len % 4;
if ((padval) && (m_last)) {
int ret;
ret = sctp_add_pad_tombuf(m_last, (4-padval));
if (ret) {
sctp_m_freem(m);
return;
}
m->m_pkthdr.len += padval;
}
sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
}
static void
sctp_insert_on_wheel(struct sctp_association *asoc,
struct sctp_stream_out *strq)
{
struct sctp_stream_out *stre, *strn;
stre = TAILQ_FIRST(&asoc->out_wheel);
if (stre == NULL) {
TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
return;
}
for (; stre; stre = strn) {
strn = TAILQ_NEXT(stre, next_spoke);
if (stre->stream_no > strq->stream_no) {
TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
return;
} else if (stre->stream_no == strq->stream_no) {
return;
} else if (strn == NULL) {
TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
next_spoke);
}
}
}
static void
sctp_remove_from_wheel(struct sctp_association *asoc,
struct sctp_stream_out *strq)
{
TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
strq->next_spoke.tqe_next = NULL;
strq->next_spoke.tqe_prev = NULL;
}
static void
sctp_prune_prsctp(struct sctp_tcb *stcb,
struct sctp_association *asoc,
struct sctp_sndrcvinfo *srcv,
int dataout
)
{
int freed_spc=0;
struct sctp_tmit_chunk *chk, *nchk;
if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
SCTP_PR_SCTP_BUFFER)) ==
(SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
if (chk->data) {
int ret_spc;
int cause;
if (chk->sent > SCTP_DATAGRAM_UNSENT)
cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
else
cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
cause,
&asoc->sent_queue);
freed_spc += ret_spc;
if (freed_spc >= dataout) {
return;
}
}
}
}
}
chk = TAILQ_FIRST(&asoc->send_queue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
SCTP_PR_SCTP_BUFFER)) ==
(SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
if (chk->data) {
int ret_spc;
ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
&asoc->send_queue);
freed_spc += ret_spc;
if (freed_spc >= dataout) {
return;
}
}
}
}
chk = nchk;
}
}
}
static void
sctp_prepare_chunk(struct sctp_tmit_chunk *template,
struct sctp_tcb *stcb,
struct sctp_sndrcvinfo *srcv,
struct sctp_stream_out *strq,
struct sctp_nets *net)
{
memset(template, 0, sizeof(struct sctp_tmit_chunk));
template->sent = SCTP_DATAGRAM_UNSENT;
if ((stcb->asoc.peer_supports_prsctp) &&
(srcv->sinfo_flags & (SCTP_PR_SCTP_TTL|SCTP_PR_SCTP_BUF)) &&
(srcv->sinfo_timetolive > 0)
) {
if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
} else {
struct timeval tv;
SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
tv.tv_sec = srcv->sinfo_timetolive / 1000;
tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
#ifndef __FreeBSD__
timeradd(&template->rec.data.timetodrop, &tv,
&template->rec.data.timetodrop);
#else
timevaladd(&template->rec.data.timetodrop, &tv);
#endif
}
}
if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
template->rec.data.stream_seq = strq->next_sequence_sent;
} else {
template->rec.data.stream_seq = 0;
}
template->rec.data.TSN_seq = 0;
template->rec.data.stream_number = srcv->sinfo_stream;
template->rec.data.payloadtype = srcv->sinfo_ppid;
template->rec.data.context = srcv->sinfo_context;
template->rec.data.doing_fast_retransmit = 0;
template->rec.data.ect_nonce = 0;
if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
template->whoTo = net;
} else {
if (stcb->asoc.primary_destination)
template->whoTo = stcb->asoc.primary_destination;
else {
template->whoTo = net;
}
}
if (srcv->sinfo_flags & SCTP_UNORDERED) {
template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
} else {
template->rec.data.rcv_flags = 0;
}
template->flags = 0;
if (stcb->asoc.peer_supports_prsctp) {
if (srcv->sinfo_timetolive > 0) {
if (srcv->sinfo_flags & SCTP_PR_SCTP_TTL) {
template->flags |= SCTP_PR_SCTP_ENABLED;
}
if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
template->flags |= SCTP_PR_SCTP_BUFFER;
}
}
}
template->asoc = &stcb->asoc;
}
int
sctp_get_frag_point(struct sctp_tcb *stcb,
struct sctp_association *asoc)
{
int siz, ovh;
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
ovh = SCTP_MED_OVERHEAD;
} else {
ovh = SCTP_MED_V4_OVERHEAD;
}
if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
siz = asoc->smallest_mtu - ovh;
else
siz = (stcb->sctp_ep->sctp_frag_point - ovh);
if (siz % 4) {
siz -= (siz % 4);
}
return (siz);
}
extern unsigned int sctp_max_chunks_on_queue;
#define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
static int
sctp_msg_append(struct sctp_tcb *stcb,
struct sctp_nets *net,
struct mbuf *m,
struct sctp_sndrcvinfo *srcv,
int flags)
{
struct socket *so;
struct sctp_association *asoc;
struct sctp_stream_out *strq;
struct sctp_tmit_chunk *chk;
struct sctpchunk_listhead tmp;
struct sctp_tmit_chunk template;
struct mbuf *n, *mnext;
struct mbuf *mm;
unsigned int dataout, siz;
int mbcnt = 0;
int mbcnt_e = 0;
int error = 0;
if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("software error in sctp_msg_append:1\n");
printf("stcb:%p net:%p m:%p srcv:%p\n",
stcb, net, m, srcv);
}
#endif
sctp_m_freem(m);
return (EFAULT);
}
so = stcb->sctp_socket;
asoc = &stcb->asoc;
if (srcv->sinfo_flags & SCTP_ABORT) {
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
dataout = m->m_pkthdr.len;
} else {
dataout = 0;
for (n = m; n; n = n->m_next) {
dataout += n->m_len;
}
}
M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
if (m) {
struct sctp_paramhdr *ph;
m->m_len = sizeof(struct sctp_paramhdr) + dataout;
ph = mtod(m, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
ph->param_length = htons(m->m_len);
}
sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
m = NULL;
} else {
;
}
goto out;
}
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
(asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
error = ECONNRESET;
goto out;
}
if (srcv->sinfo_stream >= asoc->streamoutcnt) {
error = EINVAL;
goto out;
}
if (asoc->strmout == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("software error in sctp_msg_append:2\n");
}
#endif
error = EFAULT;
goto out;
}
strq = &asoc->strmout[srcv->sinfo_stream];
if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
dataout = m->m_pkthdr.len;
} else {
dataout = 0;
for (n = m; n; n = n->m_next) {
dataout += n->m_len;
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Attempt to send out %d bytes\n",
dataout);
}
#endif
error = sblock(&so->so_snd, SBLOCKWAIT(flags));
if (error)
goto out_locked;
if (dataout > so->so_snd.sb_hiwat) {
error = EMSGSIZE;
goto release;
}
if ((srcv->sinfo_flags & SCTP_EOF) &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(dataout == 0)
) {
goto zap_by_it_all;
}
if ((so->so_snd.sb_hiwat <
(dataout + asoc->total_output_queue_size)) ||
(asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
(asoc->total_output_mbuf_queue_size >
so->so_snd.sb_mbmax)
) {
if (asoc->peer_supports_prsctp) {
sctp_prune_prsctp(stcb, asoc, srcv, dataout);
}
while ((so->so_snd.sb_hiwat <
(dataout + asoc->total_output_queue_size)) ||
(asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
(asoc->total_output_mbuf_queue_size >
so->so_snd.sb_mbmax)) {
struct sctp_inpcb *inp;
if (so->so_state & SS_NBIO) {
error = EWOULDBLOCK;
goto release;
}
inp = stcb->sctp_ep;
inp->sctp_tcb_at_block = (void *)stcb;
inp->error_on_block = 0;
sbunlock(&so->so_snd);
error = sbwait(&so->so_snd);
inp->sctp_tcb_at_block = 0;
if (inp->error_on_block)
error = inp->error_on_block;
if (so->so_error)
error = so->so_error;
if (error) {
goto out_locked;
}
error = sblock(&so->so_snd, M_WAITOK);
if (error)
goto out_locked;
#if defined(__FreeBSD__) && __FreeBSD_version >= 502115
if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
#else
if (so->so_state & SS_CANTSENDMORE) {
#endif
error = EPIPE;
goto release;
}
if (so->so_error) {
error = so->so_error;
goto release;
}
}
}
if (m->m_flags & M_PKTHDR) {
m->m_pkthdr.len = dataout;
}
siz = sctp_get_frag_point(stcb, asoc);
if ((dataout) && (dataout <= siz)) {
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
error = ENOMEM;
goto release;
}
sctp_prepare_chunk(chk, stcb, srcv, strq, net);
chk->whoTo->ref_count++;
chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
asoc->chunks_on_out_queue++;
chk->data = m;
m = NULL;
for (mm = chk->data; mm; mm = mm->m_next) {
mbcnt += MSIZE;
if (mm->m_flags & M_EXT) {
mbcnt += chk->data->m_ext.ext_size;
}
}
chk->send_size = dataout;
chk->book_size = chk->send_size;
chk->mbcnt = mbcnt;
if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
strq->next_sequence_sent++;
}
chk->data->m_nextpkt = 0;
asoc->stream_queue_cnt++;
TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
if ((strq->next_spoke.tqe_next == NULL) &&
(strq->next_spoke.tqe_prev == NULL)) {
sctp_insert_on_wheel(asoc, strq);
}
} else if ((dataout) && (dataout > siz)) {
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
(dataout > siz)) {
error = EMSGSIZE;
goto release;
}
sctp_prepare_chunk(&template, stcb, srcv, strq, net);
n = m;
while (dataout > siz) {
n->m_nextpkt = m_split(n, siz, M_WAIT);
if (n->m_nextpkt == NULL) {
error = EFAULT;
goto release;
}
dataout -= siz;
n = n->m_nextpkt;
}
n = m;
TAILQ_INIT(&tmp);
while (n) {
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
chk = TAILQ_FIRST(&tmp);
while (chk) {
TAILQ_REMOVE(&tmp, chk, sctp_next);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
asoc->chunks_on_out_queue--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&tmp);
}
error = ENOMEM;
goto release;
}
sctppcbinfo.ipi_count_chunk++;
asoc->chunks_on_out_queue++;
sctppcbinfo.ipi_gencnt_chunk++;
*chk = template;
chk->whoTo->ref_count++;
chk->data = n;
mbcnt_e = 0;
for (mm = chk->data; mm; mm = mm->m_next) {
mbcnt_e += MSIZE;
if (mm->m_flags & M_EXT) {
mbcnt_e += chk->data->m_ext.ext_size;
}
}
if (chk->data->m_flags & M_PKTHDR) {
chk->send_size = chk->data->m_pkthdr.len;
} else {
struct mbuf *nn;
chk->send_size = 0;
for (nn = chk->data; nn; nn = nn->m_next) {
chk->send_size += nn->m_len;
}
}
chk->book_size = chk->send_size;
chk->mbcnt = mbcnt_e;
mbcnt += mbcnt_e;
if (chk->flags & SCTP_PR_SCTP_BUFFER) {
asoc->sent_queue_cnt_removeable++;
}
n = n->m_nextpkt;
TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
}
m = NULL;
if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
strq->next_sequence_sent++;
}
chk = TAILQ_FIRST(&tmp);
chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
chk = TAILQ_FIRST(&tmp);
while (chk) {
chk->data->m_nextpkt = 0;
TAILQ_REMOVE(&tmp, chk, sctp_next);
asoc->stream_queue_cnt++;
TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
chk = TAILQ_FIRST(&tmp);
}
if ((strq->next_spoke.tqe_next == NULL) &&
(strq->next_spoke.tqe_prev == NULL)) {
sctp_insert_on_wheel(asoc, strq);
}
}
zap_by_it_all:
if ((srcv->sinfo_flags & SCTP_EOF) &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
int some_on_streamwheel = 0;
if (!TAILQ_EMPTY(&asoc->out_wheel)) {
struct sctp_stream_out *outs;
TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
if (!TAILQ_EMPTY(&outs->outqueue)) {
some_on_streamwheel = 1;
break;
}
}
}
if (TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(some_on_streamwheel == 0)) {
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("%s:%d sends a shutdown\n",
__FILE__,
__LINE__
);
}
#endif
sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
asoc->state = SCTP_STATE_SHUTDOWN_SENT;
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
asoc->primary_destination);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
asoc->primary_destination);
}
} else {
asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
}
}
#ifdef SCTP_MBCNT_LOGGING
sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
asoc->total_output_queue_size,
dataout,
asoc->total_output_mbuf_queue_size,
mbcnt);
#endif
asoc->total_output_queue_size += dataout;
asoc->total_output_mbuf_queue_size += mbcnt;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
so->so_snd.sb_cc += dataout;
so->so_snd.sb_mbcnt += mbcnt;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("++total out:%d total_mbuf_out:%d\n",
(int)asoc->total_output_queue_size,
(int)asoc->total_output_mbuf_queue_size);
}
#endif
release:
sbunlock(&so->so_snd);
out_locked:
out:
if (m && m->m_nextpkt) {
n = m;
while (n) {
mnext = n->m_nextpkt;
n->m_nextpkt = NULL;
sctp_m_freem(n);
n = mnext;
}
} else {
sctp_m_freem(m);
}
return (error);
}
static struct mbuf *
sctp_copy_mbufchain(struct mbuf *clonechain,
struct mbuf *outchain)
{
struct mbuf *appendchain;
#if defined(__FreeBSD__) || defined(__NetBSD__)
if (clonechain->m_flags & M_PKTHDR) {
appendchain = m_copypacket(clonechain, M_DONTWAIT);
sctp_pegs[SCTP_CACHED_SRC]++;
} else
appendchain = m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
#elif defined(__APPLE__)
appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
#else
appendchain = m_copy(clonechain, 0, M_COPYALL);
#endif
if (appendchain == NULL) {
sctp_m_freem(outchain);
return (NULL);
}
if (outchain) {
struct mbuf *m;
m = outchain;
while (m) {
if (m->m_next == NULL) {
m->m_next = appendchain;
break;
}
m = m->m_next;
}
if (outchain->m_flags & M_PKTHDR) {
int append_tot;
struct mbuf *t;
t = appendchain;
append_tot = 0;
while (t) {
append_tot += t->m_len;
t = t->m_next;
}
outchain->m_pkthdr.len += append_tot;
}
return (outchain);
} else {
return (appendchain);
}
}
static void
sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
{
struct sctp_copy_all *ca;
struct mbuf *m;
int turned_on_nonblock=0, ret;
ca = (struct sctp_copy_all *)ptr;
if (ca->m == NULL) {
return;
}
if (ca->inp != inp) {
return;
}
m = sctp_copy_mbufchain(ca->m, NULL);
if (m == NULL) {
ca->cnt_failed++;
return;
}
if ((stcb->sctp_socket->so_state & SS_NBIO) == 0) {
turned_on_nonblock = 1;
stcb->sctp_socket->so_state |= SS_NBIO;
}
ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m, &ca->sndrcv, 0);
if (turned_on_nonblock) {
stcb->sctp_socket->so_state &= ~SS_NBIO;
}
if (ret) {
ca->cnt_failed++;
} else {
ca->cnt_sent++;
}
}
static void
sctp_sendall_completes(void *ptr, u_int32_t val)
{
struct sctp_copy_all *ca;
ca = (struct sctp_copy_all *)ptr;
m_freem(ca->m);
free(ca, M_PCB);
}
#define MC_ALIGN(m, len) do { \
(m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
} while (0)
static struct mbuf *
sctp_copy_out_all(struct uio *uio, int len)
{
struct mbuf *ret, *at;
int left, willcpy, cancpy, error;
MGETHDR(ret, M_WAIT, MT_HEADER);
if (ret == NULL) {
return (NULL);
}
left = len;
ret->m_len = 0;
ret->m_pkthdr.len = len;
MCLGET(ret, M_WAIT);
if (ret == NULL) {
return (NULL);
}
if ((ret->m_flags & M_EXT) == 0) {
m_freem (ret);
return (NULL);
}
cancpy = M_TRAILINGSPACE(ret);
willcpy = uimin(cancpy, left);
at = ret;
while (left > 0) {
MC_ALIGN(at, willcpy);
error = uiomove(mtod(at, void *), willcpy, uio);
if (error) {
err_out_now:
m_freem(ret);
return (NULL);
}
at->m_len = willcpy;
at->m_nextpkt = at->m_next = 0;
left -= willcpy;
if (left > 0) {
MGET(at->m_next, M_WAIT, MT_DATA);
if (at->m_next == NULL) {
goto err_out_now;
}
at = at->m_next;
at->m_len = 0;
MCLGET(at, M_WAIT);
if (at == NULL) {
goto err_out_now;
}
if ((at->m_flags & M_EXT) == 0) {
goto err_out_now;
}
cancpy = M_TRAILINGSPACE(at);
willcpy = uimin(cancpy, left);
}
}
return (ret);
}
static int
sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
{
int ret;
struct sctp_copy_all *ca;
ca = malloc(sizeof(struct sctp_copy_all), M_PCB, M_WAIT);
if (ca == NULL) {
m_freem(m);
return (ENOMEM);
}
memset (ca, 0, sizeof(struct sctp_copy_all));
ca->inp = inp;
ca->sndrcv = *srcv;
ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
if (uio) {
ca->sndlen = uio->uio_resid;
ca->m = sctp_copy_out_all(uio, ca->sndlen);
if (ca->m == NULL) {
free(ca, M_PCB);
return (ENOMEM);
}
} else {
if ((m->m_flags & M_PKTHDR) == 0) {
ca->sndlen = 0;
while(m) {
ca->sndlen += m->m_len;
m = m->m_next;
}
} else {
ca->sndlen = m->m_pkthdr.len;
}
ca->m = m;
}
ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
(void *)ca, 0, sctp_sendall_completes, inp);
if (ret) {
#ifdef SCTP_DEBUG
printf("Failed to initiate iterator to takeover associations\n");
#endif
free(ca, M_PCB);
return (EFAULT);
}
return (0);
}
void
sctp_toss_old_cookies(struct sctp_association *asoc)
{
struct sctp_tmit_chunk *chk, *nchk;
chk = TAILQ_FIRST(&asoc->control_send_queue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
asoc->ctrl_queue_cnt--;
if (chk->whoTo)
sctp_free_remote_addr(chk->whoTo);
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;
}
}
void
sctp_toss_old_asconf(struct sctp_tcb *stcb)
{
struct sctp_association *asoc;
struct sctp_tmit_chunk *chk, *chk_tmp;
asoc = &stcb->asoc;
for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
chk = chk_tmp) {
chk_tmp = TAILQ_NEXT(chk, sctp_next);
if (chk->rec.chunk_id == SCTP_ASCONF) {
TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
asoc->ctrl_queue_cnt--;
if (chk->whoTo)
sctp_free_remote_addr(chk->whoTo);
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++;
}
}
}
static void
sctp_clean_up_datalist(struct sctp_tcb *stcb,
struct sctp_association *asoc,
struct sctp_tmit_chunk **data_list,
int bundle_at,
struct sctp_nets *net)
{
int i;
for (i = 0; i < bundle_at; i++) {
if (i) {
data_list[i]->do_rtt = 0;
}
data_list[i]->sent_rcv_time = net->last_sent_time;
TAILQ_REMOVE(&asoc->send_queue,
data_list[i],
sctp_next);
TAILQ_INSERT_TAIL(&asoc->sent_queue,
data_list[i],
sctp_next);
asoc->sent_queue_cnt++;
asoc->send_queue_cnt--;
if ((asoc->peers_rwnd <= 0) &&
(asoc->total_flight == 0) &&
(bundle_at == 1)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("WINDOW PROBE SET\n");
}
#endif
sctp_pegs[SCTP_WINDOW_PROBES]++;
data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
} else {
data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
}
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC2, 3);
#endif
data_list[i]->sent = SCTP_DATAGRAM_SENT;
data_list[i]->snd_count = 1;
net->flight_size += data_list[i]->book_size;
asoc->total_flight += data_list[i]->book_size;
asoc->total_flight_count++;
#ifdef SCTP_LOG_RWND
sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
#endif
asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
(u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
asoc->peers_rwnd = 0;
}
}
}
static void
sctp_clean_up_ctl(struct sctp_association *asoc)
{
struct sctp_tmit_chunk *chk, *nchk;
for (chk = TAILQ_FIRST(&asoc->control_send_queue);
chk; chk = nchk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
(chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
(chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
(chk->rec.chunk_id == SCTP_SHUTDOWN) ||
(chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
(chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
(chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
(chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
(chk->rec.chunk_id == SCTP_ECN_CWR) ||
(chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
clean_up_anyway:
TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
asoc->ctrl_queue_cnt--;
sctp_free_remote_addr(chk->whoTo);
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++;
} else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
struct sctp_stream_reset_req *strreq;
strreq = mtod(chk->data, struct sctp_stream_reset_req *);
if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
goto clean_up_anyway;
}
}
}
}
static int
sctp_move_to_outqueue(struct sctp_tcb *stcb,
struct sctp_stream_out *strq)
{
struct sctp_association *asoc;
int tot_moved = 0;
int failed = 0;
int padval;
struct sctp_tmit_chunk *chk, *nchk;
struct sctp_data_chunk *dchkh;
struct sctpchunk_listhead tmp;
struct mbuf *orig;
asoc = &stcb->asoc;
TAILQ_INIT(&tmp);
chk = TAILQ_FIRST(&strq->outqueue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
orig = chk->data;
M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
if (chk->data == NULL) {
failed++;
break;
}
if (orig != chk->data) {
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
stcb->sctp_socket->so_snd.sb_mbcnt += MSIZE;
}
#ifdef SCTP_MBCNT_LOGGING
sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
asoc->total_output_queue_size,
0,
asoc->total_output_mbuf_queue_size,
MSIZE);
#endif
stcb->asoc.total_output_mbuf_queue_size += MSIZE;
chk->mbcnt += MSIZE;
}
chk->send_size += sizeof(struct sctp_data_chunk);
if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
m_pullup(chk->data, sizeof(struct sctp_data_chunk));
if (chk->data == NULL) {
failed++;
break;
}
}
dchkh = mtod(chk->data, struct sctp_data_chunk *);
dchkh->ch.chunk_length = htons(chk->send_size);
padval = chk->send_size % 4;
if (padval) {
if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
failed++;
break;
}
chk->send_size += (4 - padval);
}
TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
asoc->stream_queue_cnt--;
TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
break;
}
chk = nchk;
}
if (failed) {
chk = TAILQ_FIRST(&tmp);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
TAILQ_REMOVE(&tmp, chk, sctp_next);
sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
(SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
chk);
sctp_m_freem(chk->data);
chk->data = NULL;
if (chk->whoTo) {
sctp_free_remote_addr(chk->whoTo);
chk->whoTo = 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;
}
return (0);
}
chk = TAILQ_FIRST(&tmp);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
TAILQ_REMOVE(&tmp, chk, sctp_next);
TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
asoc->send_queue_cnt++;
chk->rec.data.TSN_seq = asoc->sending_seq++;
dchkh = mtod(chk->data, struct sctp_data_chunk *);
dchkh->ch.chunk_type = SCTP_DATA;
dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
dchkh->dp.stream_id = htons(strq->stream_no);
dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
dchkh->dp.protocol_id = chk->rec.data.payloadtype;
tot_moved += chk->send_size;
chk = nchk;
}
return (tot_moved);
}
static void
sctp_fill_outqueue(struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct sctp_association *asoc;
struct sctp_tmit_chunk *chk;
struct sctp_stream_out *strq, *strqn;
int mtu_fromwheel, goal_mtu;
unsigned int moved, seenend, cnt_mvd=0;
asoc = &stcb->asoc;
goal_mtu = net->cwnd - net->flight_size;
if ((unsigned int)goal_mtu < net->mtu) {
goal_mtu = net->mtu;
}
if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
}
seenend = moved = mtu_fromwheel = 0;
if (asoc->last_out_stream == NULL) {
strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
if (asoc->last_out_stream == NULL) {
return;
}
goto done_it;
}
strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
done_it:
if (strq == NULL) {
asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
}
while (mtu_fromwheel < goal_mtu) {
if (strq == NULL) {
if (seenend == 0) {
seenend = 1;
strq = TAILQ_FIRST(&asoc->out_wheel);
} else if ((moved == 0) && (seenend)) {
sctp_pegs[SCTP_MOVED_NLEF]++;
return;
} else if (moved) {
moved = 0;
seenend = 0;
strq = TAILQ_FIRST(&asoc->out_wheel);
}
if (strq == NULL)
break;
continue;
}
strqn = TAILQ_NEXT(strq, next_spoke);
if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
sctp_remove_from_wheel(asoc, strq);
if (strq == asoc->last_out_stream) {
asoc->last_out_stream = NULL;
}
strq = strqn;
continue;
}
if (chk->whoTo != net) {
strq = strqn;
continue;
}
mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
cnt_mvd++;
moved++;
asoc->last_out_stream = strq;
strq = strqn;
}
sctp_pegs[SCTP_MOVED_MAX]++;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Ok we moved %d chunks to send queue\n",
moved);
}
#endif
if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
}
}
void
sctp_fix_ecn_echo(struct sctp_association *asoc)
{
struct sctp_tmit_chunk *chk;
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
chk->sent = SCTP_DATAGRAM_UNSENT;
}
}
}
static void
sctp_move_to_an_alt(struct sctp_tcb *stcb,
struct sctp_association *asoc,
struct sctp_nets *net)
{
struct sctp_tmit_chunk *chk;
struct sctp_nets *a_net;
a_net = sctp_find_alternate_net(stcb, net);
if ((a_net != net) &&
((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
if (chk->whoTo == net) {
sctp_free_remote_addr(chk->whoTo);
chk->whoTo = a_net;
a_net->ref_count++;
}
}
}
}
static int sctp_from_user_send=0;
static int
sctp_med_chunk_output(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_association *asoc,
int *num_out,
int *reason_code,
int control_only, int *cwnd_full, int from_where,
struct timeval *now, int *now_filled)
{
struct sctp_nets *net;
struct mbuf *outchain;
struct sctp_tmit_chunk *chk, *nchk;
struct sctphdr *shdr;
struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
int no_fragmentflg, error;
int one_chunk, hbflag;
int asconf, cookie, no_out_cnt;
int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
unsigned int mtu, r_mtu, omtu;
*num_out = 0;
cwnd_full_ind = 0;
ctl_cnt = no_out_cnt = asconf = cookie = 0;
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC2, 2);
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("***********************\n");
}
#endif
hbflag = 0;
if (control_only)
no_data_chunks = 1;
else
no_data_chunks = 0;
if (TAILQ_EMPTY(&asoc->control_send_queue) &&
TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->out_wheel)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("All wheels empty\n");
}
#endif
return (0);
}
if (asoc->peers_rwnd <= 0) {
*cwnd_full = 1;
*reason_code = 1;
if (asoc->total_flight > 0) {
no_data_chunks = 1;
sctp_pegs[SCTP_RWND_BLOCKED]++;
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
(int)no_data_chunks,
(int)asoc->total_flight, (int)asoc->peers_rwnd);
}
#endif
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("net:%p fs:%d cwnd:%d\n",
net, net->flight_size, net->cwnd);
}
#endif
if (net->flight_size >= net->cwnd) {
cwnd_full_ind++;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Ok skip fillup->fs:%d > cwnd:%d\n",
net->flight_size,
net->cwnd);
}
#endif
sctp_pegs[SCTP_CWND_NOFILL]++;
continue;
}
sctp_fill_outqueue(stcb, net);
}
*cwnd_full = cwnd_full_ind;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
int chk_cnt = 0;
TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
chk_cnt++;
}
printf("We have %d chunks on the send_queue\n", chk_cnt);
chk_cnt = 0;
TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
chk_cnt++;
}
printf("We have %d chunks on the sent_queue\n", chk_cnt);
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
chk_cnt++;
}
printf("We have %d chunks on the control_queue\n", chk_cnt);
}
#endif
if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
(no_data_chunks == 0)) {
if (callout_pending(&stcb->asoc.dack_timer.timer)) {
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep,
stcb, NULL);
sctp_send_sack(stcb);
}
}
if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
(TAILQ_FIRST(&asoc->send_queue) == NULL)) {
return (0);
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
struct rtentry *rt;
if (net->ref_count < 2) {
continue;
}
ctl_cnt = bundle_at = 0;
outchain = NULL;
no_fragmentflg = 1;
one_chunk = 0;
rt = rtcache_validate(&net->ro);
if (rt != NULL) {
struct ifnet *ifp;
ifp = net->ro._ro_rt->rt_ifp;
if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
#ifdef SCTP_LOG_MAXBURST
sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
#endif
rtcache_unref(rt, &net->ro);
continue;
}
rtcache_unref(rt, &net->ro);
}
if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
} else {
mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
}
if (mtu > asoc->peers_rwnd) {
if (asoc->total_flight > 0) {
r_mtu = asoc->peers_rwnd;
} else {
one_chunk = 1;
r_mtu = mtu;
}
} else {
r_mtu = mtu;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
r_mtu, mtu, net, one_chunk);
}
#endif
for (chk = TAILQ_FIRST(&asoc->control_send_queue);
chk; chk = nchk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (chk->whoTo != net) {
continue;
}
if (chk->data == NULL) {
continue;
}
if ((chk->data->m_flags & M_PKTHDR) == 0) {
continue;
}
if (chk->sent != SCTP_DATAGRAM_UNSENT) {
continue;
}
if ((chk->data->m_pkthdr.len < (int)mtu) ||
(chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
outchain = sctp_copy_mbufchain(chk->data,
outchain);
if (outchain == NULL) {
return (ENOMEM);
}
if (mtu > chk->data->m_pkthdr.len)
mtu -= chk->data->m_pkthdr.len;
else
mtu = 0;
if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
no_fragmentflg = 0;
}
if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
(chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
(chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
(chk->rec.chunk_id == SCTP_SHUTDOWN) ||
(chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
(chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
(chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
(chk->rec.chunk_id == SCTP_ECN_CWR) ||
(chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
(chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
hbflag = 1;
if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
if (callout_pending(&stcb->asoc.dack_timer.timer)) {
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
inp, stcb, net);
}
}
ctl_cnt++;
} else {
ctl_cnt++;
if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
cookie = 1;
no_out_cnt = 1;
} else if (chk->rec.chunk_id == SCTP_ASCONF) {
hbflag = 1;
asconf = 1;
}
chk->sent = SCTP_DATAGRAM_SENT;
chk->snd_count++;
}
if (mtu == 0) {
if (asconf) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
asconf = 0;
}
if (cookie) {
sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
cookie = 0;
}
if (outchain->m_len == 0) {
outchain->m_len = sizeof(struct sctphdr);
} else {
M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
if (outchain == NULL) {
error = ENOBUFS;
goto error_out_again;
}
}
shdr = mtod(outchain, struct sctphdr *);
shdr->src_port = inp->sctp_lport;
shdr->dest_port = stcb->rport;
shdr->v_tag = htonl(stcb->asoc.peer_vtag);
shdr->checksum = 0;
if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
rtcache_getdst(&net->ro),
outchain,
no_fragmentflg, 0, NULL, asconf))) {
if (error == ENOBUFS) {
asoc->ifp_had_enobuf = 1;
}
sctp_pegs[SCTP_DATA_OUT_ERR]++;
if (from_where == 0) {
sctp_pegs[SCTP_ERROUT_FRM_USR]++;
}
error_out_again:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("Gak got ctrl error %d\n", error);
}
#endif
if (hbflag) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Update HB anyway\n");
}
#endif
if (*now_filled == 0) {
SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
*now_filled = 1;
*now = net->last_sent_time;
} else {
net->last_sent_time = *now;
}
hbflag = 0;
}
if (error == EHOSTUNREACH ||
error == EHOSTDOWN) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Moving data to an alterante\n");
}
#endif
sctp_move_to_an_alt(stcb, asoc, net);
}
sctp_clean_up_ctl (asoc);
return (error);
} else
asoc->ifp_had_enobuf = 0;
if (hbflag) {
if (*now_filled == 0) {
SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
*now_filled = 1;
*now = net->last_sent_time;
} else {
net->last_sent_time = *now;
}
hbflag = 0;
}
if (!no_out_cnt)
*num_out += ctl_cnt;
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
mtu = (net->mtu - SCTP_MIN_OVERHEAD);
} else {
mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
}
no_fragmentflg = 1;
}
}
}
if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
} else {
omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Now to data transmission\n");
}
#endif
if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
(cookie)) {
for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
if (no_data_chunks) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Either nothing to send or we are full\n");
}
#endif
break;
}
if (net->flight_size >= net->cwnd) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("fs:%d > cwnd:%d\n",
net->flight_size, net->cwnd);
}
#endif
sctp_pegs[SCTP_CWND_BLOCKED]++;
*reason_code = 2;
break;
}
nchk = TAILQ_NEXT(chk, sctp_next);
if (chk->whoTo != net) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("chk->whoTo:%p not %p\n",
chk->whoTo, net);
}
#endif
continue;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Can we pick up a chunk?\n");
}
#endif
if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
chk->send_size, mtu);
chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
}
if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Picking up the chunk\n");
}
#endif
outchain = sctp_copy_mbufchain(chk->data, outchain);
if (outchain == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Gakk no memory\n");
}
#endif
if (!callout_pending(&net->rxt_timer.timer)) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
}
return (ENOMEM);
}
if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
no_fragmentflg = 0;
}
mtu -= chk->send_size;
r_mtu -= chk->send_size;
data_list[bundle_at++] = chk;
if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
mtu = 0;
break;
}
if (mtu <= 0) {
mtu = 0;
break;
}
if ((r_mtu <= 0) || one_chunk) {
r_mtu = 0;
break;
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
chk->send_size, mtu, r_mtu);
}
#endif
break;
}
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("ok now is chain assembled? %p\n",
outchain);
}
#endif
if (outchain) {
if (asconf) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
asconf = 0;
}
if (cookie) {
sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
cookie = 0;
}
if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("ok lets start a send timer .. we will transmit %p\n",
outchain);
}
#endif
sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
}
if ((outchain->m_flags & M_PKTHDR) == 0) {
struct mbuf *t;
MGETHDR(t, M_DONTWAIT, MT_HEADER);
if (t == NULL) {
sctp_m_freem(outchain);
return (ENOMEM);
}
t->m_next = outchain;
t->m_pkthdr.len = 0;
m_reset_rcvif(t);
t->m_len = 0;
outchain = t;
while (t) {
outchain->m_pkthdr.len += t->m_len;
t = t->m_next;
}
}
if (outchain->m_len == 0) {
m_align(outchain, sizeof(struct sctphdr));
outchain->m_len = sizeof(struct sctphdr);
} else {
M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
if (outchain == NULL) {
error = ENOBUFS;
goto errored_send;
}
}
shdr = mtod(outchain, struct sctphdr *);
shdr->src_port = inp->sctp_lport;
shdr->dest_port = stcb->rport;
shdr->v_tag = htonl(stcb->asoc.peer_vtag);
shdr->checksum = 0;
if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
rtcache_getdst(&net->ro),
outchain,
no_fragmentflg, bundle_at, data_list[0], asconf))) {
if (error == ENOBUFS) {
asoc->ifp_had_enobuf = 1;
}
sctp_pegs[SCTP_DATA_OUT_ERR]++;
if (from_where == 0) {
sctp_pegs[SCTP_ERROUT_FRM_USR]++;
}
errored_send:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Gak send error %d\n", error);
}
#endif
if (hbflag) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Update HB time anyway\n");
}
#endif
if (*now_filled == 0) {
SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
*now_filled = 1;
*now = net->last_sent_time;
} else {
net->last_sent_time = *now;
}
hbflag = 0;
}
if (error == EHOSTUNREACH ||
error == EHOSTDOWN) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Calling the movement routine\n");
}
#endif
sctp_move_to_an_alt(stcb, asoc, net);
}
sctp_clean_up_ctl (asoc);
return (error);
} else {
asoc->ifp_had_enobuf = 0;
}
if (bundle_at || hbflag) {
if (*now_filled == 0) {
SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
*now_filled = 1;
*now = net->last_sent_time;
} else {
net->last_sent_time = *now;
}
}
if (!no_out_cnt) {
*num_out += (ctl_cnt + bundle_at);
}
if (bundle_at) {
if (!net->rto_pending) {
net->rto_pending = 1;
data_list[0]->do_rtt = 1;
} else {
data_list[0]->do_rtt = 0;
}
sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
}
if (one_chunk) {
break;
}
}
}
if ((*num_out == 0) && (*reason_code == 0)) {
*reason_code = 3;
}
sctp_clean_up_ctl (asoc);
return (0);
}
void
sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
{
struct sctp_chunkhdr *hdr;
struct sctp_tmit_chunk *chk;
struct mbuf *mat;
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(op_err);
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
if (op_err == 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++;
return;
}
chk->send_size = 0;
mat = op_err;
while (mat != NULL) {
chk->send_size += mat->m_len;
mat = mat->m_next;
}
chk->rec.chunk_id = SCTP_OPERATION_ERROR;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = op_err;
chk->whoTo = chk->asoc->primary_destination;
chk->whoTo->ref_count++;
hdr = mtod(op_err, struct sctp_chunkhdr *);
hdr->chunk_type = SCTP_OPERATION_ERROR;
hdr->chunk_flags = 0;
hdr->chunk_length = htons(chk->send_size);
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
chk,
sctp_next);
chk->asoc->ctrl_queue_cnt++;
}
int
sctp_send_cookie_echo(struct mbuf *m,
int offset,
struct sctp_tcb *stcb,
struct sctp_nets *net)
{
int at;
struct mbuf *cookie, *mat;
struct sctp_paramhdr parm, *phdr;
struct sctp_chunkhdr *hdr;
struct sctp_tmit_chunk *chk;
uint16_t ptype, plen;
cookie = NULL;
at = offset + sizeof(struct sctp_init_chunk);
do {
phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
if (phdr == NULL) {
return (-3);
}
ptype = ntohs(phdr->param_type);
plen = ntohs(phdr->param_length);
if (ptype == SCTP_STATE_COOKIE) {
int pad;
if ((pad = (plen % 4))) {
plen += 4 - pad;
}
cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
if (cookie == NULL) {
return (-2);
}
break;
}
at += SCTP_SIZE32(plen);
} while (phdr);
if (cookie == NULL) {
return (-3);
}
hdr = mtod(cookie, struct sctp_chunkhdr *);
hdr->chunk_type = SCTP_COOKIE_ECHO;
hdr->chunk_flags = 0;
if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
MGETHDR(mat, M_DONTWAIT, MT_HEADER);
if (mat == NULL) {
sctp_m_freem(cookie);
return (-4);
}
mat->m_len = 0;
m_reset_rcvif(mat);
mat->m_next = cookie;
cookie = mat;
}
cookie->m_pkthdr.len = plen;
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(cookie);
return (-5);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->send_size = cookie->m_pkthdr.len;
chk->rec.chunk_id = SCTP_COOKIE_ECHO;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = cookie;
chk->whoTo = chk->asoc->primary_destination;
chk->whoTo->ref_count++;
TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
return (0);
}
void
sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
struct mbuf *m,
int offset,
int chk_length,
struct sctp_nets *net)
{
struct mbuf *outchain;
struct sctp_chunkhdr *chdr;
struct sctp_tmit_chunk *chk;
if (net == NULL)
return;
outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
if (outchain == NULL) {
return;
}
chdr = mtod(outchain, struct sctp_chunkhdr *);
chdr->chunk_type = SCTP_HEARTBEAT_ACK;
chdr->chunk_flags = 0;
if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
struct mbuf *tmp;
MGETHDR(tmp, M_DONTWAIT, MT_HEADER);
if (tmp == NULL) {
return;
}
tmp->m_len = 0;
m_reset_rcvif(tmp);
tmp->m_next = outchain;
outchain = tmp;
}
outchain->m_pkthdr.len = chk_length;
if (chk_length % 4) {
u_int32_t cpthis=0;
int padlen;
padlen = 4 - (outchain->m_pkthdr.len % 4);
m_copyback(outchain, outchain->m_pkthdr.len, padlen, (void *)&cpthis);
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(outchain);
return ;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->send_size = chk_length;
chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = outchain;
chk->whoTo = net;
chk->whoTo->ref_count++;
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
}
int
sctp_send_cookie_ack(struct sctp_tcb *stcb) {
struct mbuf *cookie_ack;
struct sctp_chunkhdr *hdr;
struct sctp_tmit_chunk *chk;
cookie_ack = NULL;
MGETHDR(cookie_ack, M_DONTWAIT, MT_HEADER);
if (cookie_ack == NULL) {
return (-1);
}
cookie_ack->m_data += SCTP_MIN_OVERHEAD;
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(cookie_ack);
return (-1);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->send_size = sizeof(struct sctp_chunkhdr);
chk->rec.chunk_id = SCTP_COOKIE_ACK;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = cookie_ack;
if (chk->asoc->last_control_chunk_from != NULL) {
chk->whoTo = chk->asoc->last_control_chunk_from;
} else {
chk->whoTo = chk->asoc->primary_destination;
}
chk->whoTo->ref_count++;
hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
hdr->chunk_type = SCTP_COOKIE_ACK;
hdr->chunk_flags = 0;
hdr->chunk_length = htons(chk->send_size);
cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
m_reset_rcvif(cookie_ack);
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
return (0);
}
int
sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct mbuf *m_shutdown_ack;
struct sctp_shutdown_ack_chunk *ack_cp;
struct sctp_tmit_chunk *chk;
m_shutdown_ack = NULL;
MGETHDR(m_shutdown_ack, M_DONTWAIT, MT_HEADER);
if (m_shutdown_ack == NULL) {
return (-1);
}
m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(m_shutdown_ack);
return (-1);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->send_size = sizeof(struct sctp_chunkhdr);
chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = m_shutdown_ack;
chk->whoTo = net;
net->ref_count++;
ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
ack_cp->ch.chunk_flags = 0;
ack_cp->ch.chunk_length = htons(chk->send_size);
m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
m_reset_rcvif(m_shutdown_ack);
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
return (0);
}
int
sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct mbuf *m_shutdown;
struct sctp_shutdown_chunk *shutdown_cp;
struct sctp_tmit_chunk *chk;
m_shutdown = NULL;
MGETHDR(m_shutdown, M_DONTWAIT, MT_HEADER);
if (m_shutdown == NULL) {
return (-1);
}
m_shutdown->m_data += SCTP_MIN_OVERHEAD;
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(m_shutdown);
return (-1);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->send_size = sizeof(struct sctp_shutdown_chunk);
chk->rec.chunk_id = SCTP_SHUTDOWN;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->data = m_shutdown;
chk->whoTo = net;
net->ref_count++;
shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
shutdown_cp->ch.chunk_flags = 0;
shutdown_cp->ch.chunk_length = htons(chk->send_size);
shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
m_reset_rcvif(m_shutdown);
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
soisdisconnecting(stcb->sctp_ep->sctp_socket);
}
return (0);
}
int
sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_tmit_chunk *chk;
struct mbuf *m_asconf;
m_asconf = sctp_compose_asconf(stcb);
if (m_asconf == NULL) {
return (-1);
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(m_asconf);
return (-1);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->data = m_asconf;
chk->send_size = m_asconf->m_pkthdr.len;
chk->rec.chunk_id = SCTP_ASCONF;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->whoTo = chk->asoc->primary_destination;
chk->whoTo->ref_count++;
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
return (0);
}
int
sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
{
struct sctp_tmit_chunk *chk;
struct mbuf *m_ack;
if (stcb->asoc.last_asconf_ack_sent == NULL) {
return (-1);
}
#if defined(__FreeBSD__) || defined(__NetBSD__)
if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
sctp_pegs[SCTP_CACHED_SRC]++;
} else
m_ack = m_copym(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL, M_DONTWAIT);
#else
m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
#endif
if (m_ack == NULL) {
return (-1);
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_m_freem(m_ack);
return (-1);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
if (retrans) {
if (stcb->asoc.used_alt_asconfack > 2) {
chk->whoTo = NULL;
} else {
chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
stcb->asoc.used_alt_asconfack++;
}
if (chk->whoTo == NULL) {
if (stcb->asoc.last_control_chunk_from == NULL)
chk->whoTo = stcb->asoc.primary_destination;
else
chk->whoTo = stcb->asoc.last_control_chunk_from;
stcb->asoc.used_alt_asconfack = 0;
}
} else {
if (stcb->asoc.last_control_chunk_from == NULL)
chk->whoTo = stcb->asoc.primary_destination;
else
chk->whoTo = stcb->asoc.last_control_chunk_from;
stcb->asoc.used_alt_asconfack = 0;
}
chk->data = m_ack;
chk->send_size = m_ack->m_pkthdr.len;
chk->rec.chunk_id = SCTP_ASCONF_ACK;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->flags = 0;
chk->asoc = &stcb->asoc;
chk->whoTo->ref_count++;
TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
chk->asoc->ctrl_queue_cnt++;
return (0);
}
static int
sctp_chunk_retransmission(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_association *asoc,
int *cnt_out, struct timeval *now, int *now_filled)
{
struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
struct sctp_tmit_chunk *chk, *fwd;
struct mbuf *m;
struct sctphdr *shdr;
int asconf;
struct sctp_nets *net;
int no_fragmentflg, bundle_at, cnt_thru;
unsigned int mtu;
int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
tmr_started = ctl_cnt = bundle_at = error = 0;
no_fragmentflg = 1;
asconf = 0;
fwd_tsn = 0;
*cnt_out = 0;
fwd = NULL;
m = NULL;
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC3, 1);
#endif
if (TAILQ_EMPTY(&asoc->sent_queue)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("SCTP hits empty queue with cnt set to %d?\n",
asoc->sent_queue_retran_cnt);
}
#endif
asoc->sent_queue_cnt = 0;
asoc->sent_queue_cnt_removeable = 0;
}
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->sent != SCTP_DATAGRAM_RESEND) {
continue;
}
if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
(chk->rec.chunk_id == SCTP_ASCONF) ||
(chk->rec.chunk_id == SCTP_STREAM_RESET) ||
(chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
struct sctp_stream_reset_req *strreq;
strreq = mtod(chk->data, struct sctp_stream_reset_req *);
if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
continue;
}
}
ctl_cnt++;
if (chk->rec.chunk_id == SCTP_ASCONF) {
no_fragmentflg = 1;
asconf = 1;
}
if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
fwd_tsn = 1;
fwd = chk;
}
m = sctp_copy_mbufchain(chk->data, m);
break;
}
}
one_chunk = 0;
cnt_thru = 0;
if (m != NULL) {
if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
} else if (chk->rec.chunk_id == SCTP_ASCONF)
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
if (m->m_len == 0) {
m->m_len = sizeof(struct sctphdr);
} else {
M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
if (m == NULL) {
return (ENOBUFS);
}
}
shdr = mtod(m, struct sctphdr *);
shdr->src_port = inp->sctp_lport;
shdr->dest_port = stcb->rport;
shdr->v_tag = htonl(stcb->asoc.peer_vtag);
shdr->checksum = 0;
chk->snd_count++;
if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
rtcache_getdst(&chk->whoTo->ro), m,
no_fragmentflg, 0, NULL, asconf))) {
sctp_pegs[SCTP_DATA_OUT_ERR]++;
return (error);
}
*cnt_out += 1;
chk->sent = SCTP_DATAGRAM_SENT;
sctp_ucount_decr(asoc->sent_queue_retran_cnt);
if (fwd_tsn == 0) {
return (0);
} else {
sctp_clean_up_ctl (asoc);
return (0);
}
}
if (TAILQ_EMPTY(&asoc->sent_queue)) {
return (-1);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Normal chunk retransmission cnt:%d\n",
asoc->sent_queue_retran_cnt);
}
#endif
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
return (1);
}
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(20, inp, stcb, NULL);
#endif
TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
if (chk->sent != SCTP_DATAGRAM_RESEND) {
continue;
}
net = chk->whoTo;
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
mtu = (net->mtu - SCTP_MIN_OVERHEAD);
} else {
mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
}
if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
uint32_t tsn;
tsn = asoc->last_acked_seq + 1;
if (tsn == chk->rec.data.TSN_seq) {
goto one_chunk_around;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("blocked-peers_rwnd:%d tf:%d\n",
(int)asoc->peers_rwnd,
(int)asoc->total_flight);
}
#endif
sctp_pegs[SCTP_RWND_BLOCKED]++;
return (1);
}
one_chunk_around:
if (asoc->peers_rwnd < mtu) {
one_chunk = 1;
}
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC3, 2);
#endif
bundle_at = 0;
m = NULL;
net->fast_retran_ip = 0;
if (chk->rec.data.doing_fast_retransmit == 0) {
if (net->flight_size >= net->cwnd) {
sctp_pegs[SCTP_CWND_BLOCKED]++;
continue;
}
} else {
net->fast_retran_ip = 1;
}
if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
m = sctp_copy_mbufchain(chk->data, m);
if (m == NULL) {
return (ENOMEM);
}
if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
no_fragmentflg = 0;
}
mtu -= chk->send_size;
data_list[bundle_at++] = chk;
if (one_chunk && (asoc->total_flight <= 0)) {
sctp_pegs[SCTP_WINDOW_PROBES]++;
chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
}
}
if (one_chunk == 0) {
fwd = TAILQ_NEXT(chk, sctp_next);
while (fwd) {
if (fwd->sent != SCTP_DATAGRAM_RESEND) {
fwd = TAILQ_NEXT(fwd, sctp_next);
continue;
}
if (fwd->whoTo != net) {
fwd = TAILQ_NEXT(fwd, sctp_next);
continue;
}
if (fwd->send_size <= mtu) {
m = sctp_copy_mbufchain(fwd->data, m);
if (m == NULL) {
return (ENOMEM);
}
if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
no_fragmentflg = 0;
}
mtu -= fwd->send_size;
data_list[bundle_at++] = fwd;
if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
break;
}
fwd = TAILQ_NEXT(fwd, sctp_next);
} else {
break;
}
}
}
if (m) {
if (!callout_pending(&net->rxt_timer.timer)) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
tmr_started = 1;
}
if (m->m_len == 0) {
m->m_len = sizeof(struct sctphdr);
} else {
M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
if (m == NULL) {
return (ENOBUFS);
}
}
shdr = mtod(m, struct sctphdr *);
shdr->src_port = inp->sctp_lport;
shdr->dest_port = stcb->rport;
shdr->v_tag = htonl(stcb->asoc.peer_vtag);
shdr->checksum = 0;
if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
rtcache_getdst(&net->ro),
m,
no_fragmentflg, 0, NULL, asconf))) {
sctp_pegs[SCTP_DATA_OUT_ERR]++;
return (error);
}
cnt_thru++;
if (*now_filled == 0) {
SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
*now = asoc->time_last_sent;
*now_filled = 1;
} else {
asoc->time_last_sent = *now;
}
*cnt_out += bundle_at;
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC4, bundle_at);
#endif
for (i = 0; i < bundle_at; i++) {
sctp_pegs[SCTP_RETRANTSN_SENT]++;
data_list[i]->sent = SCTP_DATAGRAM_SENT;
data_list[i]->snd_count++;
sctp_ucount_decr(asoc->sent_queue_retran_cnt);
data_list[i]->sent_rcv_time = asoc->time_last_sent;
net->flight_size += data_list[i]->book_size;
asoc->total_flight += data_list[i]->book_size;
asoc->total_flight_count++;
#ifdef SCTP_LOG_RWND
sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
#endif
asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
(u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
asoc->peers_rwnd = 0;
}
if ((i == 0) &&
(data_list[i]->rec.data.doing_fast_retransmit)) {
sctp_pegs[SCTP_FAST_RETRAN]++;
if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
(tmr_started == 0)) {
sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
}
}
}
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(21, inp, stcb, NULL);
#endif
} else {
return (1);
}
if (asoc->sent_queue_retran_cnt <= 0) {
asoc->sent_queue_retran_cnt = 0;
break;
}
if (one_chunk) {
return (1);
}
break;
}
return (0);
}
static int
sctp_timer_validation(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_association *asoc,
int ret)
{
struct sctp_nets *net;
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
if (callout_pending(&net->rxt_timer.timer)) {
return (ret);
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Deadlock avoided starting timer on a dest at retran\n");
}
#endif
sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
return (ret);
}
int
sctp_chunk_output(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
int from_where)
{
struct sctp_association *asoc;
struct sctp_nets *net;
int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
struct timeval now;
int now_filled=0;
int cwnd_full=0;
asoc = &stcb->asoc;
tot_out = 0;
num_out = 0;
reason_code = 0;
sctp_pegs[SCTP_CALLS_TO_CO]++;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
}
#endif
while (asoc->sent_queue_retran_cnt) {
ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
if (ret > 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("retransmission ret:%d -- full\n", ret);
}
#endif
(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
&cwnd_full, from_where,
&now, &now_filled);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Control send outputs:%d@full\n", num_out);
}
#endif
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(8, inp, stcb, NULL);
#endif
return (sctp_timer_validation(inp, stcb, asoc, ret));
}
if (ret < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Done with retrans, none left fill up window\n");
}
#endif
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(9, inp, stcb, NULL);
#endif
break;
}
if (from_where == 1) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Only one packet allowed out\n");
}
#endif
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(10, inp, stcb, NULL);
#endif
(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
&now, &now_filled);
return (ret);
}
if ((num_out == 0) && (ret == 0)) {
break;
}
}
#ifdef SCTP_AUDITING_ENABLED
sctp_auditing(12, inp, stcb, NULL);
#endif
burst_limit = asoc->max_burst;
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
SCTP_ADDR_NOT_REACHABLE) {
sctp_move_to_an_alt(stcb, asoc, net);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
}
#endif
#ifdef SCTP_USE_ALLMAN_BURST
if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
if (net->ssthresh < net->cwnd)
net->ssthresh = net->cwnd;
net->cwnd = (net->flight_size+(burst_limit*net->mtu));
#ifdef SCTP_LOG_MAXBURST
sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
#endif
sctp_pegs[SCTP_MAX_BURST_APL]++;
}
net->fast_retran_ip = 0;
#endif
}
}
burst_cnt = 0;
cwnd_full = 0;
do {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("Burst count:%d - call m-c-o\n", burst_cnt);
}
#endif
error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
&reason_code, 0, &cwnd_full, from_where,
&now, &now_filled);
if (error) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Error %d was returned from med-c-op\n", error);
}
#endif
#ifdef SCTP_LOG_MAXBURST
sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
#endif
break;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
printf("m-c-o put out %d\n", num_out);
}
#endif
tot_out += num_out;
burst_cnt++;
} while (num_out
#ifndef SCTP_USE_ALLMAN_BURST
&& (burst_cnt < burst_limit)
#endif
);
#ifndef SCTP_USE_ALLMAN_BURST
if (burst_cnt >= burst_limit) {
sctp_pegs[SCTP_MAX_BURST_APL]++;
asoc->burst_limit_applied = 1;
#ifdef SCTP_LOG_MAXBURST
sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
#endif
} else {
asoc->burst_limit_applied = 0;
}
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Ok, we have put out %d chunks\n", tot_out);
}
#endif
if (tot_out == 0) {
sctp_pegs[SCTP_CO_NODATASNT]++;
if (asoc->stream_queue_cnt > 0) {
sctp_pegs[SCTP_SOS_NOSNT]++;
} else {
sctp_pegs[SCTP_NOS_NOSNT]++;
}
if (asoc->send_queue_cnt > 0) {
sctp_pegs[SCTP_SOSE_NOSNT]++;
} else {
sctp_pegs[SCTP_NOSE_NOSNT]++;
}
}
sctp_fix_ecn_echo(asoc);
return (error);
}
int
sctp_output(struct sctp_inpcb *inp, struct mbuf *m,
struct sockaddr *addr, struct mbuf *control, struct lwp *l, int flags)
{
struct sctp_inpcb *t_inp;
struct sctp_tcb *stcb;
struct sctp_nets *net;
struct sctp_association *asoc;
int create_lock_applied = 0;
int queue_only, error = 0;
struct sctp_sndrcvinfo srcv;
int un_sent = 0;
int use_rcvinfo = 0;
t_inp = inp;
queue_only = 0;
stcb = NULL;
asoc = NULL;
net = NULL;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("USR Send BEGINS\n");
}
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
sctp_m_freem(m);
return (EFAULT);
}
if (addr) {
SCTP_ASOC_CREATE_LOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
SCTP_ASOC_CREATE_UNLOCK(inp);
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
sctp_m_freem(m);
return (EFAULT);
}
create_lock_applied = 1;
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
(addr->sa_family == AF_INET6)) {
SCTP_ASOC_CREATE_UNLOCK(inp);
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
sctp_m_freem(m);
return (EINVAL);
}
}
if (control) {
sctppcbinfo.mbuf_track++;
if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
sizeof(srcv))) {
if (srcv.sinfo_flags & SCTP_SENDALL) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
return (sctp_sendall(inp, NULL, m, &srcv));
}
if (srcv.sinfo_assoc_id) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb) {
SCTP_TCB_LOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
if (stcb == NULL) {
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
sctp_m_freem(m);
return (ENOTCONN);
}
net = stcb->asoc.primary_destination;
} else {
stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
}
if ((stcb) &&
(addr != NULL)) {
if (addr)
net = sctp_findnet(stcb, addr);
}
if (net == NULL)
net = stcb->asoc.primary_destination;
}
use_rcvinfo = 1;
}
}
if (stcb == NULL) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb) {
SCTP_TCB_LOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
if (stcb == NULL) {
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
sctp_m_freem(m);
return (ENOTCONN);
}
if (addr == NULL) {
net = stcb->asoc.primary_destination;
} else {
net = sctp_findnet(stcb, addr);
if (net == NULL) {
net = stcb->asoc.primary_destination;
}
}
} else {
if (addr != NULL) {
SCTP_INP_WLOCK(inp);
SCTP_INP_INCR_REF(inp);
SCTP_INP_WUNLOCK(inp);
stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
}
}
}
if ((stcb == NULL) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
sctp_m_freem(m);
return (ENOTCONN);
} else if ((stcb == NULL) &&
(addr == NULL)) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
sctp_m_freem(m);
return (ENOENT);
} else if (stcb == NULL) {
if ((use_rcvinfo) && (srcv.sinfo_flags & SCTP_ABORT)) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
sctp_m_freem(m);
return (ENOENT);
}
stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
if (stcb == NULL) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
sctp_m_freem(m);
return (error);
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
} else {
printf("Huh-1, create lock should have been applied!\n");
}
queue_only = 1;
asoc = &stcb->asoc;
asoc->state = SCTP_STATE_COOKIE_WAIT;
SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
if (control) {
struct sctp_initmsg initm;
int i;
if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
sizeof(initm))) {
if (initm.sinit_max_attempts)
asoc->max_init_times = initm.sinit_max_attempts;
if (initm.sinit_num_ostreams)
asoc->pre_open_streams = initm.sinit_num_ostreams;
if (initm.sinit_max_instreams)
asoc->max_inbound_streams = initm.sinit_max_instreams;
if (initm.sinit_max_init_timeo)
asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
}
if (asoc->streamoutcnt < asoc->pre_open_streams) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Ok, defout:%d pre_open:%d\n",
asoc->streamoutcnt, asoc->pre_open_streams);
}
#endif
free(asoc->strmout, M_PCB);
asoc->strmout = NULL;
asoc->streamoutcnt = asoc->pre_open_streams;
asoc->strmout = malloc(asoc->streamoutcnt *
sizeof(struct sctp_stream_out), M_PCB,
M_WAIT);
for (i = 0; i < asoc->streamoutcnt; i++) {
asoc->strmout[i].next_sequence_sent = 0x0;
TAILQ_INIT(&asoc->strmout[i].outqueue);
asoc->strmout[i].stream_no = i;
asoc->strmout[i].next_spoke.tqe_next = 0;
asoc->strmout[i].next_spoke.tqe_prev = 0;
}
}
}
sctp_send_initiate(inp, stcb);
net = stcb->asoc.primary_destination;
} else {
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
asoc = &stcb->asoc;
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
queue_only = 1;
}
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
(asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if ((use_rcvinfo) &&
(srcv.sinfo_flags & SCTP_ABORT)) {
sctp_msg_append(stcb, net, m, &srcv, flags);
error = 0;
} else {
sctp_m_freem(m);
error = ECONNRESET;
}
SCTP_TCB_UNLOCK(stcb);
return (error);
}
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
if (use_rcvinfo == 0) {
srcv = stcb->asoc.def_send;
}
#ifdef SCTP_DEBUG
else {
if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
printf("stream:%d\n", srcv.sinfo_stream);
printf("flags:%x\n", (u_int)srcv.sinfo_flags);
printf("ppid:%d\n", srcv.sinfo_ppid);
printf("context:%d\n", srcv.sinfo_context);
}
}
#endif
if (control) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
control = NULL;
}
if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
;
} else {
net = stcb->asoc.primary_destination;
}
if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
SCTP_TCB_UNLOCK(stcb);
return (error);
}
if (net->flight_size > net->cwnd) {
sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
queue_only = 1;
} else if (asoc->ifp_had_enobuf) {
sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
queue_only = 1;
} else {
un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
SCTP_MED_OVERHEAD);
if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
(stcb->asoc.total_flight > 0) &&
(un_sent < (int)stcb->asoc.smallest_mtu)
) {
sctp_pegs[SCTP_NAGLE_NOQ]++;
queue_only = 1;
} else {
sctp_pegs[SCTP_NAGLE_OFF]++;
}
}
if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("USR Send calls sctp_chunk_output\n");
}
#endif
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC0, 1);
sctp_auditing(6, inp, stcb, net);
#endif
sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
sctp_chunk_output(inp, stcb, 0);
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xC0, 2);
sctp_auditing(7, inp, stcb, net);
#endif
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
}
#endif
SCTP_TCB_UNLOCK(stcb);
return (0);
}
void
send_forward_tsn(struct sctp_tcb *stcb,
struct sctp_association *asoc)
{
struct sctp_tmit_chunk *chk;
struct sctp_forward_tsn_chunk *fwdtsn;
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
if (chk->whoTo != asoc->primary_destination) {
sctp_free_remote_addr(chk->whoTo);
chk->whoTo = asoc->primary_destination;
chk->whoTo->ref_count++;
}
goto sctp_fill_in_rest;
}
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
chk->asoc = asoc;
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (chk->data == NULL) {
chk->whoTo->ref_count--;
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++;
return;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = asoc->primary_destination;
chk->whoTo->ref_count++;
TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
asoc->ctrl_queue_cnt++;
sctp_fill_in_rest:
chk->data->m_pkthdr.len = chk->data->m_len = 0;
{
struct sctp_tmit_chunk *at, *tp1, *last;
struct sctp_strseq *strseq;
unsigned int cnt_of_space, i, ovh;
unsigned int space_needed;
unsigned int cnt_of_skipped = 0;
TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
if (at->sent != SCTP_FORWARD_TSN_SKIP) {
break;
}
if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
continue;
}
cnt_of_skipped++;
}
space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
(cnt_of_skipped * sizeof(struct sctp_strseq)));
if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
((chk->data->m_flags & M_EXT) == 0)) {
MCLGET(chk->data, M_DONTWAIT);
}
cnt_of_space = M_TRAILINGSPACE(chk->data);
if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
ovh = SCTP_MIN_OVERHEAD;
} else {
ovh = SCTP_MIN_V4_OVERHEAD;
}
if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
cnt_of_space = asoc->smallest_mtu - ovh;
}
if (cnt_of_space < space_needed) {
cnt_of_skipped = (cnt_of_space-
((sizeof(struct sctp_forward_tsn_chunk))/
sizeof(struct sctp_strseq)));
at = TAILQ_FIRST(&asoc->sent_queue);
for (i = 0; i < cnt_of_skipped; i++) {
tp1 = TAILQ_NEXT(at, sctp_next);
at = tp1;
}
last = at;
asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
}
chk->send_size = space_needed;
fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
fwdtsn->ch.chunk_length = htons(chk->send_size);
fwdtsn->ch.chunk_flags = 0;
fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
(cnt_of_skipped * sizeof(struct sctp_strseq)));
chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
fwdtsn++;
strseq = (struct sctp_strseq *)fwdtsn;
at = TAILQ_FIRST(&asoc->sent_queue);
for (i = 0; i < cnt_of_skipped; i++) {
tp1 = TAILQ_NEXT(at, sctp_next);
if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
i--;
at = tp1;
continue;
}
strseq->stream = ntohs(at->rec.data.stream_number);
strseq->sequence = ntohs(at->rec.data.stream_seq);
strseq++;
at = tp1;
}
}
return;
}
void
sctp_send_sack(struct sctp_tcb *stcb)
{
struct sctp_association *asoc;
struct sctp_tmit_chunk *chk, *a_chk;
struct sctp_sack_chunk *sack;
struct sctp_gap_ack_block *gap_descriptor;
uint32_t *dup;
int start;
unsigned int i, maxi, seeing_ones, m_size;
unsigned int num_gap_blocks, space;
start = maxi = 0;
seeing_ones = 1;
a_chk = NULL;
asoc = &stcb->asoc;
if (asoc->last_data_chunk_from == NULL) {
return;
}
sctp_set_rwnd(stcb, asoc);
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
asoc->ctrl_queue_cnt++;
a_chk = chk;
sctp_m_freem(a_chk->data);
a_chk->data = NULL;
sctp_free_remote_addr(a_chk->whoTo);
a_chk->whoTo = NULL;
break;
}
}
if (a_chk == NULL) {
a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (a_chk == NULL) {
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
sctp_timer_start(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
}
a_chk->asoc = asoc;
a_chk->snd_count = 0;
a_chk->send_size = 0;
a_chk->sent = SCTP_DATAGRAM_UNSENT;
m_size = (asoc->mapping_array_size << 3);
if ((asoc->numduptsns) ||
(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
) {
if ((!(asoc->last_data_chunk_from->dest_state &
SCTP_ADDR_NOT_REACHABLE)) &&
(asoc->used_alt_onsack > 2)) {
a_chk->whoTo = NULL;
} else {
asoc->used_alt_onsack++;
a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
}
if (a_chk->whoTo == NULL) {
a_chk->whoTo = asoc->last_data_chunk_from;
asoc->used_alt_onsack = 0;
}
} else {
#ifdef SCTP_DEBUG
if (asoc->last_data_chunk_from == NULL) {
printf("Huh, last_data_chunk_from is null when we want to sack??\n");
}
#endif
asoc->used_alt_onsack = 0;
a_chk->whoTo = asoc->last_data_chunk_from;
}
if (a_chk->whoTo)
a_chk->whoTo->ref_count++;
MGETHDR(a_chk->data, M_DONTWAIT, MT_DATA);
if ((a_chk->data == NULL) ||
(a_chk->whoTo == NULL)) {
if (a_chk->data) {
sctp_m_freem(a_chk->data);
a_chk->data = NULL;
}
a_chk->whoTo->ref_count--;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
sctp_timer_start(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
return;
}
if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
num_gap_blocks = 0;
} else {
num_gap_blocks = 0;
if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
} else {
maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
}
if (maxi > m_size) {
#ifdef SCTP_DEBUG
printf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
maxi,
m_size,
asoc->highest_tsn_inside_map,
asoc->mapping_array_base_tsn,
asoc->cumulative_tsn
);
#endif
num_gap_blocks = 0;
goto no_gaps_now;
}
if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
} else {
start = -1;
}
start++;
for (i = start; i <= maxi; i++) {
if (seeing_ones) {
if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
seeing_ones = 0;
}
} else {
if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
seeing_ones = 1;
num_gap_blocks++;
}
}
}
no_gaps_now:
if (num_gap_blocks == 0) {
if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
}
}
}
space = (sizeof(struct sctp_sack_chunk) +
(num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
(asoc->numduptsns * sizeof(int32_t))
);
if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
int calc, fit;
calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
calc -= sizeof(struct sctp_gap_ack_block);
fit = calc/sizeof(struct sctp_gap_ack_block);
if (fit > (int)num_gap_blocks) {
asoc->numduptsns = (fit - num_gap_blocks);
} else {
num_gap_blocks = fit;
asoc->numduptsns = 0;
}
space = (sizeof(struct sctp_sack_chunk) +
(num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
(asoc->numduptsns * sizeof(int32_t))
);
}
if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
MCLGET(a_chk->data, M_DONTWAIT);
if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
sctp_m_freem(a_chk->data);
a_chk->data = NULL;
a_chk->whoTo->ref_count--;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
sctp_timer_start(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
return;
}
}
a_chk->data->m_data += SCTP_MIN_OVERHEAD;
sack = mtod(a_chk->data, struct sctp_sack_chunk *);
sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
sack->sack.a_rwnd = htonl(asoc->my_rwnd);
asoc->my_last_reported_rwnd = asoc->my_rwnd;
sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
sack->sack.num_dup_tsns = htons(asoc->numduptsns);
a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
(num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
(asoc->numduptsns * sizeof(int32_t)));
a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
sack->ch.chunk_length = htons(a_chk->send_size);
gap_descriptor = (struct sctp_gap_ack_block *)((vaddr_t)sack + sizeof(struct sctp_sack_chunk));
seeing_ones = 0;
for (i = start; i <= maxi; i++) {
if (num_gap_blocks == 0) {
break;
}
if (seeing_ones) {
if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
gap_descriptor->end = htons(((uint16_t)(i-start)));
gap_descriptor++;
seeing_ones = 0;
num_gap_blocks--;
}
} else {
if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
gap_descriptor->start = htons(((uint16_t)(i+1-start)));
seeing_ones = 1;
}
}
}
if (num_gap_blocks) {
gap_descriptor->end = htons(((uint16_t)((i-start))));
gap_descriptor++;
}
if (asoc->numduptsns) {
dup = (uint32_t *)gap_descriptor;
for (i = 0; i < asoc->numduptsns; i++) {
*dup = htonl(asoc->dup_tsns[i]);
dup++;
}
asoc->numduptsns = 0;
}
TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
asoc->ctrl_queue_cnt++;
sctp_pegs[SCTP_PEG_SACKS_SENT]++;
return;
}
void
sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
{
struct mbuf *m_abort;
struct sctp_abort_msg *abort_m;
int sz;
abort_m = NULL;
MGETHDR(m_abort, M_DONTWAIT, MT_HEADER);
if (m_abort == NULL) {
return;
}
m_abort->m_data += SCTP_MIN_OVERHEAD;
abort_m = mtod(m_abort, struct sctp_abort_msg *);
m_abort->m_len = sizeof(struct sctp_abort_msg);
m_abort->m_next = operr;
sz = 0;
if (operr) {
struct mbuf *n;
n = operr;
while (n) {
sz += n->m_len;
n = n->m_next;
}
}
abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
abort_m->msg.ch.chunk_flags = 0;
abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
sz);
abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
abort_m->sh.dest_port = stcb->rport;
abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
abort_m->sh.checksum = 0;
m_abort->m_pkthdr.len = m_abort->m_len + sz;
m_reset_rcvif(m_abort);
sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
stcb->asoc.primary_destination,
rtcache_getdst(&stcb->asoc.primary_destination->ro),
m_abort, 1, 0, NULL, 0);
}
int
sctp_send_shutdown_complete(struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct mbuf *m_shutdown_comp;
struct sctp_shutdown_complete_msg *comp_cp;
m_shutdown_comp = NULL;
MGETHDR(m_shutdown_comp, M_DONTWAIT, MT_HEADER);
if (m_shutdown_comp == NULL) {
return (-1);
}
m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
comp_cp->shut_cmp.ch.chunk_flags = 0;
comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
comp_cp->sh.dest_port = stcb->rport;
comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
comp_cp->sh.checksum = 0;
m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
m_reset_rcvif(m_shutdown_comp);
sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
rtcache_getdst(&net->ro), m_shutdown_comp,
1, 0, NULL, 0);
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
soisdisconnected(stcb->sctp_ep->sctp_socket);
}
return (0);
}
int
sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
{
struct mbuf *mout;
struct ip *iph, *iph_out;
struct ip6_hdr *ip6, *ip6_out;
int offset_out;
struct sctp_shutdown_complete_msg *comp_cp;
MGETHDR(mout, M_DONTWAIT, MT_HEADER);
if (mout == NULL) {
return (-1);
}
iph = mtod(m, struct ip *);
iph_out = NULL;
ip6_out = NULL;
offset_out = 0;
if (iph->ip_v == IPVERSION) {
mout->m_len = sizeof(struct ip) +
sizeof(struct sctp_shutdown_complete_msg);
mout->m_next = NULL;
iph_out = mtod(mout, struct ip *);
iph_out->ip_v = IPVERSION;
iph_out->ip_hl = (sizeof(struct ip)/4);
iph_out->ip_tos = (u_char)0;
iph_out->ip_id = 0;
iph_out->ip_off = 0;
iph_out->ip_ttl = MAXTTL;
iph_out->ip_p = IPPROTO_SCTP;
iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
iph_out->ip_sum = 0;
offset_out += sizeof(*iph_out);
comp_cp = (struct sctp_shutdown_complete_msg *)(
(vaddr_t)iph_out + offset_out);
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
ip6 = (struct ip6_hdr *)iph;
mout->m_len = sizeof(struct ip6_hdr) +
sizeof(struct sctp_shutdown_complete_msg);
mout->m_next = NULL;
ip6_out = mtod(mout, struct ip6_hdr *);
ip6_out->ip6_flow = ip6->ip6_flow;
ip6_out->ip6_hlim = ip6_defhlim;
ip6_out->ip6_nxt = IPPROTO_SCTP;
ip6_out->ip6_src = ip6->ip6_dst;
ip6_out->ip6_dst = ip6->ip6_src;
ip6_out->ip6_plen = mout->m_len;
offset_out += sizeof(*ip6_out);
comp_cp = (struct sctp_shutdown_complete_msg *)(
(vaddr_t)ip6_out + offset_out);
} else {
return (-1);
}
comp_cp->sh.src_port = sh->dest_port;
comp_cp->sh.dest_port = sh->src_port;
comp_cp->sh.checksum = 0;
comp_cp->sh.v_tag = sh->v_tag;
comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
mout->m_pkthdr.len = mout->m_len;
if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
comp_cp->sh.checksum = 0;
} else {
comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
}
m_reset_rcvif(mout);
if (iph_out != NULL) {
struct route ro;
memset(&ro, 0, sizeof ro);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("sctp_shutdown_complete2 calling ip_output:\n");
sctp_print_address_pkt(iph_out, &comp_cp->sh);
}
#endif
iph_out->ip_len = htons(mout->m_pkthdr.len);
ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
} else if (ip6_out != NULL) {
struct route ro;
memset(&ro, 0, sizeof(ro));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("sctp_shutdown_complete2 calling ip6_output:\n");
sctp_print_address_pkt((struct ip *)ip6_out,
&comp_cp->sh);
}
#endif
ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
}
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
return (0);
}
static struct sctp_nets *
sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
{
struct sctp_nets *net, *hnet;
int ms_goneby, highest_ms, state_override=0;
SCTP_GETTIME_TIMEVAL(now);
highest_ms = 0;
hnet = NULL;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (
((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
(net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Skipping net:%p state:%d nohb/out-of-scope\n",
net, net->dest_state);
}
#endif
continue;
}
if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro.ro_sa) == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Skipping net:%p reachable NOT\n",
net);
}
#endif
continue;
}
if (net->last_sent_time.tv_sec) {
ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
} else
ms_goneby = 0x7fffffff;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("net:%p ms_goneby:%d\n",
net, ms_goneby);
}
#endif
if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
state_override = 1;
} else {
state_override = 0;
}
if ((((unsigned int)ms_goneby >= net->RTO) || (state_override)) &&
(ms_goneby > highest_ms)) {
highest_ms = ms_goneby;
hnet = net;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("net:%p is the new high\n",
net);
}
#endif
}
}
if (hnet &&
((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
state_override = 1;
} else {
state_override = 0;
}
if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_override)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("net:%p is the hb winner -",
hnet);
if (hnet)
sctp_print_address((struct sockaddr *)&hnet->ro.ro_sa);
else
printf(" none\n");
}
#endif
hnet->last_sent_time = *now;
return (hnet);
}
return (NULL);
}
int
sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
{
struct sctp_tmit_chunk *chk;
struct sctp_nets *net;
struct sctp_heartbeat_chunk *hb;
struct timeval now;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
if (user_req == 0) {
net = sctp_select_hb_destination(stcb, &now);
if (net == NULL) {
if (stcb->asoc.state == 0) {
return (0);
}
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
stcb->sctp_ep,
stcb,
net);
return (0);
}
#ifndef SCTP_USE_ALLMAN_BURST
else {
if (net->flight_size == 0) {
net->cwnd /= 2;
if (net->addr_is_local) {
if (net->cwnd < (net->mtu *4)) {
net->cwnd = net->mtu * 4;
}
} else {
if (net->cwnd < (net->mtu * 2)) {
net->cwnd = net->mtu * 2;
}
}
}
}
#endif
} else {
net = u_net;
if (net == NULL) {
return (0);
}
SCTP_GETTIME_TIMEVAL(&now);
}
sin = (struct sockaddr_in *)&net->ro.ro_sa;
if (sin->sin_family != AF_INET) {
if (sin->sin_family != AF_INET6) {
return (0);
}
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Gak, can't get a chunk for hb\n");
}
#endif
return (0);
}
sctppcbinfo.ipi_gencnt_chunk++;
sctppcbinfo.ipi_count_chunk++;
chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
chk->asoc = &stcb->asoc;
chk->send_size = sizeof(struct sctp_heartbeat_chunk);
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (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++;
return (0);
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = net;
chk->whoTo->ref_count++;
hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
hb->ch.chunk_flags = 0;
hb->ch.chunk_length = htons(chk->send_size);
hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
hb->heartbeat.hb_info.user_req = user_req;
hb->heartbeat.hb_info.addr_family = sin->sin_family;
hb->heartbeat.hb_info.addr_len = sin->sin_len;
if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
} else {
net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
}
if (sin->sin_family == AF_INET) {
memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
} else if (sin->sin_family == AF_INET6) {
sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Compiler bug bleeds a mbuf and a chunk\n");
}
#endif
return (0);
}
if (user_req == 0) {
if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
stcb->asoc.max_send_times)) {
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++;
return (-1);
}
}
net->hb_responded = 0;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("Inserting chunk for HB\n");
}
#endif
TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
stcb->asoc.ctrl_queue_cnt++;
sctp_pegs[SCTP_HB_SENT]++;
if (user_req == 0) {
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
stcb, net);
}
return (1);
}
void
sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
uint32_t high_tsn)
{
struct sctp_association *asoc;
struct sctp_ecne_chunk *ecne;
struct sctp_tmit_chunk *chk;
asoc = &stcb->asoc;
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
ecne = mtod(chk->data, struct sctp_ecne_chunk *);
ecne->tsn = htonl(high_tsn);
return;
}
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctp_pegs[SCTP_ECNE_SENT]++;
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.chunk_id = SCTP_ECN_ECHO;
chk->asoc = &stcb->asoc;
chk->send_size = sizeof(struct sctp_ecne_chunk);
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (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++;
return;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = net;
chk->whoTo->ref_count++;
ecne = mtod(chk->data, struct sctp_ecne_chunk *);
ecne->ch.chunk_type = SCTP_ECN_ECHO;
ecne->ch.chunk_flags = 0;
ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
ecne->tsn = htonl(high_tsn);
TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
asoc->ctrl_queue_cnt++;
}
void
sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
struct mbuf *m, int iphlen, int bad_crc)
{
struct sctp_association *asoc;
struct sctp_pktdrop_chunk *drp;
struct sctp_tmit_chunk *chk;
uint8_t *datap;
int len;
unsigned int small_one;
struct ip *iph;
long spc;
asoc = &stcb->asoc;
if (asoc->peer_supports_pktdrop == 0) {
return;
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
iph = mtod(m, struct ip *);
if (iph == NULL) {
return;
}
if (iph->ip_v == IPVERSION) {
#if defined(__FreeBSD__)
len = chk->send_size = iph->ip_len;
#else
len = chk->send_size = (iph->ip_len - iphlen);
#endif
} else {
struct ip6_hdr *ip6h;
ip6h = mtod(m, struct ip6_hdr *);
len = chk->send_size = htons(ip6h->ip6_plen);
}
if ((len+iphlen) > m->m_pkthdr.len) {
chk->send_size = len = m->m_pkthdr.len - iphlen;
}
chk->asoc = &stcb->asoc;
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (chk->data == NULL) {
jump_out:
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++;
return;
}
if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
MCLGET(chk->data, M_DONTWAIT);
if ((chk->data->m_flags & M_EXT) == 0) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto jump_out;
}
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
if (drp == NULL) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto jump_out;
}
small_one = asoc->smallest_mtu;
if (small_one > MCLBYTES) {
small_one = MCLBYTES;
}
chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
if (chk->book_size > small_one) {
drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
drp->trunc_len = htons(chk->send_size);
chk->send_size = small_one - (SCTP_MED_OVERHEAD +
sizeof(struct sctp_pktdrop_chunk) +
sizeof(struct sctphdr));
len = chk->send_size;
} else {
drp->ch.chunk_flags = 0;
drp->trunc_len = htons(0);
}
if (bad_crc) {
drp->ch.chunk_flags |= SCTP_BADCRC;
}
chk->send_size += sizeof(struct sctp_pktdrop_chunk);
chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
if (net) {
chk->whoTo = net;
} else {
chk->whoTo = asoc->primary_destination;
}
chk->whoTo->ref_count++;
chk->rec.chunk_id = SCTP_PACKET_DROPPED;
drp->ch.chunk_type = SCTP_PACKET_DROPPED;
drp->ch.chunk_length = htons(chk->send_size);
spc = stcb->sctp_socket->so_rcv.sb_hiwat;
if (spc < 0) {
spc = 0;
}
drp->bottle_bw = htonl(spc);
drp->current_onq = htonl(asoc->size_on_delivery_queue +
asoc->size_on_reasm_queue +
asoc->size_on_all_streams +
asoc->my_rwnd_control_len +
stcb->sctp_socket->so_rcv.sb_cc);
drp->reserved = 0;
datap = drp->data;
m_copydata(m, iphlen, len, datap);
TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
asoc->ctrl_queue_cnt++;
}
void
sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
{
struct sctp_association *asoc;
struct sctp_cwr_chunk *cwr;
struct sctp_tmit_chunk *chk;
asoc = &stcb->asoc;
TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
if (chk->rec.chunk_id == SCTP_ECN_CWR) {
cwr = mtod(chk->data, struct sctp_cwr_chunk *);
if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
MAX_TSN)) {
cwr->tsn = htonl(high_tsn);
}
return;
}
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.chunk_id = SCTP_ECN_CWR;
chk->asoc = &stcb->asoc;
chk->send_size = sizeof(struct sctp_cwr_chunk);
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (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++;
return;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = net;
chk->whoTo->ref_count++;
cwr = mtod(chk->data, struct sctp_cwr_chunk *);
cwr->ch.chunk_type = SCTP_ECN_CWR;
cwr->ch.chunk_flags = 0;
cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
cwr->tsn = htonl(high_tsn);
TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
asoc->ctrl_queue_cnt++;
}
static void
sctp_reset_the_streams(struct sctp_tcb *stcb,
struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
{
int i;
if (req->reset_flags & SCTP_RESET_ALL) {
for (i=0; i<stcb->asoc.streamoutcnt; i++) {
stcb->asoc.strmout[i].next_sequence_sent = 0;
}
} else if (number_entries) {
for (i=0; i<number_entries; i++) {
if (list[i] >= stcb->asoc.streamoutcnt) {
continue;
}
stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
}
}
sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
}
void
sctp_send_str_reset_ack(struct sctp_tcb *stcb,
struct sctp_stream_reset_request *req)
{
struct sctp_association *asoc;
struct sctp_stream_reset_resp *strack;
struct sctp_tmit_chunk *chk;
uint32_t seq;
int number_entries, i;
uint8_t two_way=0, not_peer=0;
uint16_t *list=NULL;
asoc = &stcb->asoc;
if (req->reset_flags & SCTP_RESET_ALL)
number_entries = 0;
else
number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.chunk_id = SCTP_STREAM_RESET;
chk->asoc = &stcb->asoc;
chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (chk->data == NULL) {
strresp_jump_out:
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++;
return;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
MCLGET(chk->data, M_DONTWAIT);
if ((chk->data->m_flags & M_EXT) == 0) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto strresp_jump_out;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
}
if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto strresp_jump_out;
}
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = asoc->primary_destination;
chk->whoTo->ref_count++;
strack = mtod(chk->data, struct sctp_stream_reset_resp *);
strack->ch.chunk_type = SCTP_STREAM_RESET;
strack->ch.chunk_flags = 0;
strack->ch.chunk_length = htons(chk->send_size);
memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
if (chk->send_size % 4) {
int pad;
uint8_t *end;
end = (uint8_t *)((vaddr_t)strack + chk->send_size);
pad = chk->send_size % 4;
for (i = 0; i < pad; i++) {
end[i] = 0;
}
chk->send_size += pad;
}
if (req->reset_flags & SCTP_RESET_YOUR) {
strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
} else {
strack->sr_resp.reset_flags = 0;
}
strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
seq = ntohl(req->reset_req_seq);
list = req->list_of_streams;
for (i=0; i<number_entries; i++) {
strack->sr_resp.list_of_streams[i] = list[i];
}
if (asoc->str_reset_seq_in == seq) {
asoc->str_reset_seq_in++;
strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
asoc->str_reset_sending_seq = asoc->sending_seq;
if (number_entries) {
uint16_t temp;
for (i=0 ; i<number_entries; i++) {
temp = ntohs(list[i]);
list[i] = temp;
}
}
if (req->reset_flags & SCTP_RESET_YOUR) {
sctp_reset_the_streams(stcb, req , number_entries, list);
}
if (req->reset_flags & SCTP_RECIPRICAL) {
sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
}
} else {
strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
}
strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
TAILQ_INSERT_TAIL(&asoc->control_send_queue,
chk,
sctp_next);
asoc->ctrl_queue_cnt++;
}
void
sctp_send_str_reset_req(struct sctp_tcb *stcb,
int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
{
struct sctp_association *asoc;
struct sctp_stream_reset_req *strreq;
struct sctp_tmit_chunk *chk;
asoc = &stcb->asoc;
if (asoc->stream_reset_outstanding) {
return;
}
if ((two_way == 0) && (not_peer == 1)) {
return;
}
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
return;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.chunk_id = SCTP_STREAM_RESET;
chk->asoc = &stcb->asoc;
chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
if (chk->data == NULL) {
strreq_jump_out:
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++;
return;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
MCLGET(chk->data, M_DONTWAIT);
if ((chk->data->m_flags & M_EXT) == 0) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto strreq_jump_out;
}
chk->data->m_data += SCTP_MIN_OVERHEAD;
}
if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
sctp_m_freem(chk->data);
chk->data = NULL;
goto strreq_jump_out;
}
chk->sent = SCTP_DATAGRAM_UNSENT;
chk->snd_count = 0;
chk->whoTo = asoc->primary_destination;
chk->whoTo->ref_count++;
strreq = mtod(chk->data, struct sctp_stream_reset_req *);
strreq->ch.chunk_type = SCTP_STREAM_RESET;
strreq->ch.chunk_flags = 0;
strreq->ch.chunk_length = htons(chk->send_size);
strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
if (chk->send_size % 4) {
int pad, i;
uint8_t *end;
end = (uint8_t *)((vaddr_t)strreq + chk->send_size);
pad = chk->send_size % 4;
for (i=0; i<pad; i++) {
end[i] = 0;
}
chk->send_size += pad;
}
strreq->sr_req.reset_flags = 0;
if (number_entrys == 0) {
strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
}
if (two_way == 0) {
strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
} else {
if (not_peer == 0) {
strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
} else {
strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
}
}
memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
if (number_entrys) {
int i;
for (i=0; i < number_entrys; i++) {
strreq->sr_req.list_of_streams[i] = htons(list[i]);
}
}
TAILQ_INSERT_TAIL(&asoc->control_send_queue,
chk,
sctp_next);
asoc->ctrl_queue_cnt++;
sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
asoc->stream_reset_outstanding = 1;
}
void
sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
struct mbuf *err_cause)
{
struct mbuf *mout;
struct sctp_abort_msg *abm;
struct ip *iph, *iph_out;
struct ip6_hdr *ip6, *ip6_out;
int iphlen_out;
if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
sctp_m_freem(err_cause);
return;
}
MGETHDR(mout, M_DONTWAIT, MT_HEADER);
if (mout == NULL) {
sctp_m_freem(err_cause);
return;
}
iph = mtod(m, struct ip *);
iph_out = NULL;
ip6_out = NULL;
if (iph->ip_v == IPVERSION) {
iph_out = mtod(mout, struct ip *);
mout->m_len = sizeof(*iph_out) + sizeof(*abm);
mout->m_next = err_cause;
iph_out->ip_v = IPVERSION;
iph_out->ip_hl = (sizeof(struct ip) / 4);
iph_out->ip_tos = (u_char)0;
iph_out->ip_id = 0;
iph_out->ip_off = 0;
iph_out->ip_ttl = MAXTTL;
iph_out->ip_p = IPPROTO_SCTP;
iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
iph_out->ip_sum = 0;
iphlen_out = sizeof(*iph_out);
abm = (struct sctp_abort_msg *)((vaddr_t)iph_out + iphlen_out);
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
ip6 = (struct ip6_hdr *)iph;
ip6_out = mtod(mout, struct ip6_hdr *);
mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
mout->m_next = err_cause;
ip6_out->ip6_flow = ip6->ip6_flow;
ip6_out->ip6_hlim = ip6_defhlim;
ip6_out->ip6_nxt = IPPROTO_SCTP;
ip6_out->ip6_src = ip6->ip6_dst;
ip6_out->ip6_dst = ip6->ip6_src;
iphlen_out = sizeof(*ip6_out);
abm = (struct sctp_abort_msg *)((vaddr_t)ip6_out + iphlen_out);
} else {
return;
}
abm->sh.src_port = sh->dest_port;
abm->sh.dest_port = sh->src_port;
abm->sh.checksum = 0;
if (vtag == 0) {
abm->sh.v_tag = sh->v_tag;
abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
} else {
abm->sh.v_tag = htonl(vtag);
abm->msg.ch.chunk_flags = 0;
}
abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
if (err_cause) {
struct mbuf *m_tmp = err_cause;
int err_len = 0;
while (m_tmp != NULL) {
err_len += m_tmp->m_len;
m_tmp = m_tmp->m_next;
}
mout->m_pkthdr.len = mout->m_len + err_len;
if (err_len % 4) {
u_int32_t cpthis=0;
int padlen;
padlen = 4 - (mout->m_pkthdr.len % 4);
m_copyback(mout, mout->m_pkthdr.len, padlen, (void *)&cpthis);
}
abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
} else {
mout->m_pkthdr.len = mout->m_len;
abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
}
if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
abm->sh.checksum = 0;
} else {
abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
}
m_reset_rcvif(mout);
if (iph_out != NULL) {
struct route ro;
memset(&ro, 0, sizeof ro);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("sctp_send_abort calling ip_output:\n");
sctp_print_address_pkt(iph_out, &abm->sh);
}
#endif
iph_out->ip_len = htons(mout->m_pkthdr.len);
(void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
} else if (ip6_out != NULL) {
struct route ro;
memset(&ro, 0, sizeof(ro));
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("sctp_send_abort calling ip6_output:\n");
sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
}
#endif
ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
}
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
}
void
sctp_send_operr_to(struct mbuf *m, int iphlen,
struct mbuf *scm,
uint32_t vtag)
{
struct sctphdr *ihdr;
struct sctphdr *ohdr;
struct sctp_chunkhdr *ophdr;
struct ip *iph;
#ifdef SCTP_DEBUG
struct sockaddr_in6 lsa6, fsa6;
#endif
uint32_t val;
iph = mtod(m, struct ip *);
ihdr = (struct sctphdr *)((vaddr_t)iph + iphlen);
if (!(scm->m_flags & M_PKTHDR)) {
printf("Huh, not a packet header in send_operr\n");
m_freem(scm);
return;
}
M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
if (scm == NULL) {
return;
}
ohdr = mtod(scm, struct sctphdr *);
ohdr->src_port = ihdr->dest_port;
ohdr->dest_port = ihdr->src_port;
ohdr->v_tag = vtag;
ohdr->checksum = 0;
ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
ophdr->chunk_type = SCTP_OPERATION_ERROR;
ophdr->chunk_flags = 0;
ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
if (scm->m_pkthdr.len % 4) {
u_int32_t cpthis=0;
int padlen;
padlen = 4 - (scm->m_pkthdr.len % 4);
m_copyback(scm, scm->m_pkthdr.len, padlen, (void *)&cpthis);
}
if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
val = 0;
} else {
val = sctp_calculate_sum(scm, NULL, 0);
}
ohdr->checksum = val;
if (iph->ip_v == IPVERSION) {
struct ip *out;
struct route ro;
M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
if (scm == NULL)
return;
memset(&ro, 0, sizeof ro);
out = mtod(scm, struct ip *);
out->ip_v = iph->ip_v;
out->ip_hl = (sizeof(struct ip)/4);
out->ip_tos = iph->ip_tos;
out->ip_id = iph->ip_id;
out->ip_off = 0;
out->ip_ttl = MAXTTL;
out->ip_p = IPPROTO_SCTP;
out->ip_sum = 0;
out->ip_src = iph->ip_dst;
out->ip_dst = iph->ip_src;
out->ip_len = htons(scm->m_pkthdr.len);
ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
} else {
struct route ro;
struct ip6_hdr *out6, *in6;
M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
if (scm == NULL)
return;
memset(&ro, 0, sizeof ro);
in6 = mtod(m, struct ip6_hdr *);
out6 = mtod(scm, struct ip6_hdr *);
out6->ip6_flow = in6->ip6_flow;
out6->ip6_hlim = ip6_defhlim;
out6->ip6_nxt = IPPROTO_SCTP;
out6->ip6_src = in6->ip6_dst;
out6->ip6_dst = in6->ip6_src;
#ifdef SCTP_DEBUG
memset(&lsa6, 0, sizeof(lsa6));
lsa6.sin6_len = sizeof(lsa6);
lsa6.sin6_family = AF_INET6;
lsa6.sin6_addr = out6->ip6_src;
memset(&fsa6, 0, sizeof(fsa6));
fsa6.sin6_len = sizeof(fsa6);
fsa6.sin6_family = AF_INET6;
fsa6.sin6_addr = out6->ip6_dst;
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("sctp_operr_to calling ipv6 output:\n");
printf("src: ");
sctp_print_address((struct sockaddr *)&lsa6);
printf("dst ");
sctp_print_address((struct sockaddr *)&fsa6);
}
#endif
ip6_output(scm, NULL, &ro, 0, NULL, NULL, NULL);
sctp_pegs[SCTP_DATAGRAMS_SENT]++;
}
}
static int
sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
{
int left, cancpy, willcpy, error;
left = cpsz;
if (m == NULL) {
*mbcnt = 0;
return (ENOMEM);
}
m->m_len = 0;
if ((left+resv_upfront) > (int)MHLEN) {
MCLGET(m, M_WAIT);
if (m == NULL) {
*mbcnt = 0;
return (ENOMEM);
}
if ((m->m_flags & M_EXT) == 0) {
*mbcnt = 0;
return (ENOMEM);
}
*mbcnt += m->m_ext.ext_size;
}
*mbcnt += MSIZE;
cancpy = M_TRAILINGSPACE(m);
willcpy = uimin(cancpy, left);
if ((willcpy + resv_upfront) > cancpy) {
willcpy -= resv_upfront;
}
while (left > 0) {
if ((m->m_flags & M_EXT) == 0) {
m_align(m, willcpy);
} else {
MC_ALIGN(m, willcpy);
}
error = uiomove(mtod(m, void *), willcpy, uio);
if (error) {
return (error);
}
m->m_len = willcpy;
m->m_nextpkt = 0;
left -= willcpy;
if (left > 0) {
MGET(m->m_next, M_WAIT, MT_DATA);
if (m->m_next == NULL) {
*mbcnt = 0;
return (ENOMEM);
}
m = m->m_next;
m->m_len = 0;
*mbcnt += MSIZE;
if (left > (int)MHLEN) {
MCLGET(m, M_WAIT);
if (m == NULL) {
*mbcnt = 0;
return (ENOMEM);
}
if ((m->m_flags & M_EXT) == 0) {
*mbcnt = 0;
return (ENOMEM);
}
*mbcnt += m->m_ext.ext_size;
}
cancpy = M_TRAILINGSPACE(m);
willcpy = uimin(cancpy, left);
}
}
return (0);
}
static int
sctp_copy_it_in(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_association *asoc,
struct sctp_nets *net,
struct sctp_sndrcvinfo *srcv,
struct uio *uio,
int flags)
{
struct socket *so;
int error = 0;
int frag_size, mbcnt = 0, mbcnt_e = 0;
unsigned int sndlen;
unsigned int tot_demand;
int tot_out, dataout;
struct sctp_tmit_chunk *chk;
struct mbuf *mm;
struct sctp_stream_out *strq;
uint32_t my_vtag;
int resv_in_first;
so = stcb->sctp_socket;
solock(so);
chk = NULL;
mm = NULL;
sndlen = uio->uio_resid;
error = sblock(&so->so_snd, SBLOCKWAIT(flags));
if (error)
goto out_locked;
#ifdef SCTP_DEBUG
printf("sctp_copy_it_in: %d\n", sndlen);
#endif
if (sndlen > so->so_snd.sb_hiwat) {
error = EMSGSIZE;
goto release;
}
if ((so->so_snd.sb_hiwat <
(sndlen + asoc->total_output_queue_size)) ||
(asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
(asoc->total_output_mbuf_queue_size >
so->so_snd.sb_mbmax)
) {
if (asoc->peer_supports_prsctp) {
sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
}
while ((so->so_snd.sb_hiwat <
(sndlen + asoc->total_output_queue_size)) ||
(asoc->chunks_on_out_queue >
sctp_max_chunks_on_queue) ||
(asoc->total_output_mbuf_queue_size >
so->so_snd.sb_mbmax)
) {
if ((so->so_state & SS_NBIO)
#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
|| (flags & MSG_NBIO)
#endif
) {
error = EWOULDBLOCK;
goto release;
}
inp->sctp_tcb_at_block = (void *)stcb;
inp->error_on_block = 0;
#ifdef SCTP_BLK_LOGGING
sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
so, asoc);
#endif
sbunlock(&so->so_snd);
SCTP_TCB_UNLOCK(stcb);
error = sbwait(&so->so_snd);
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
SCTP_INP_RUNLOCK(inp);
error = EFAULT;
goto out_locked;
}
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(inp);
inp->sctp_tcb_at_block = 0;
#ifdef SCTP_BLK_LOGGING
sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
so, asoc);
#endif
if (inp->error_on_block) {
error = inp->error_on_block;
goto out_locked;
}
if (error) {
goto out_locked;
}
if (so->so_error) {
error = so->so_error;
goto out_locked;
}
error = sblock(&so->so_snd, M_WAITOK);
if (error) {
goto out_locked;
}
#if defined(__FreeBSD__) && __FreeBSD_version >= 502115
if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
#else
if (so->so_state & SS_CANTSENDMORE) {
#endif
error = EPIPE;
goto release;
}
if (so->so_error) {
error = so->so_error;
goto release;
}
if (asoc->peer_supports_prsctp) {
sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
}
}
}
dataout = tot_out = uio->uio_resid;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
resv_in_first = SCTP_MED_OVERHEAD;
} else {
resv_in_first = SCTP_MED_V4_OVERHEAD;
}
if (srcv->sinfo_flags & SCTP_ABORT) {
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
MGETHDR(mm, M_WAIT, MT_DATA);
if (mm) {
struct sctp_paramhdr *ph;
tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
if (tot_demand > MHLEN) {
if (tot_demand > MCLBYTES) {
tot_demand = MCLBYTES;
tot_out = tot_demand - sizeof(struct sctp_paramhdr);
}
MCLGET(mm, M_WAIT);
if ((mm->m_flags & M_EXT) == 0) {
tot_demand = MHLEN;
tot_out = tot_demand - sizeof(struct sctp_paramhdr);
}
}
ph = mtod(mm, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
ph++;
mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
mm->m_len = mm->m_pkthdr.len;
error = uiomove((void *)ph, (int)tot_out, uio);
if (error) {
sctp_m_freem(mm);
mm = NULL;
}
}
sbunlock(&so->so_snd);
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_RESPONSE_TO_USER_REQ,
mm);
mm = NULL;
goto out_locked;
}
goto release;
}
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
(asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
error = ECONNRESET;
goto release;
}
if (srcv->sinfo_stream >= asoc->streamoutcnt) {
error = EINVAL;
goto release;
}
if (asoc->strmout == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("software error in sctp_copy_it_in\n");
}
#endif
error = EFAULT;
goto release;
}
if ((srcv->sinfo_flags & SCTP_EOF) &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(tot_out == 0)) {
sounlock(so);
goto zap_by_it_now;
}
if (tot_out == 0) {
error = EMSGSIZE;
goto release;
}
my_vtag = asoc->my_vtag;
strq = &asoc->strmout[srcv->sinfo_stream];
frag_size = sctp_get_frag_point(stcb, asoc);
sounlock(so);
if (tot_out <= frag_size) {
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
error = ENOMEM;
goto release;
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
asoc->chunks_on_out_queue++;
MGETHDR(mm, M_WAIT, MT_DATA);
if (mm == NULL) {
error = ENOMEM;
goto clean_up;
}
error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
if (error)
goto clean_up;
sctp_prepare_chunk(chk, stcb, srcv, strq, net);
chk->mbcnt = mbcnt_e;
mbcnt += mbcnt_e;
mbcnt_e = 0;
mm->m_pkthdr.len = tot_out;
chk->data = mm;
mm = NULL;
chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
chk->whoTo->ref_count++;
chk->send_size = tot_out;
chk->book_size = chk->send_size;
if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
strq->next_sequence_sent++;
}
if (chk->flags & SCTP_PR_SCTP_BUFFER) {
asoc->sent_queue_cnt_removeable++;
}
solock(so);
if ((asoc->state == 0) ||
(my_vtag != asoc->my_vtag) ||
(so != inp->sctp_socket) ||
(inp->sctp_socket == 0)) {
sounlock(so);
error = ECONNRESET;
goto clean_up;
}
asoc->stream_queue_cnt++;
TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
if ((strq->next_spoke.tqe_next == NULL) &&
(strq->next_spoke.tqe_prev == NULL)) {
sctp_insert_on_wheel(asoc, strq);
}
sounlock(so);
clean_up:
if (error) {
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
goto release;
}
} else {
struct sctp_tmit_chunk template;
struct sctpchunk_listhead tmp;
sctp_prepare_chunk(&template, stcb, srcv, strq, net);
TAILQ_INIT(&tmp);
while (tot_out > 0) {
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
error = ENOMEM;
}
sctppcbinfo.ipi_count_chunk++;
asoc->chunks_on_out_queue++;
sctppcbinfo.ipi_gencnt_chunk++;
*chk = template;
chk->whoTo->ref_count++;
MGETHDR(chk->data, M_WAIT, MT_DATA);
if (chk->data == NULL) {
error = ENOMEM;
goto temp_clean_up;
}
tot_demand = uimin(tot_out, frag_size);
error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
if (error)
goto temp_clean_up;
chk->mbcnt = mbcnt_e;
mbcnt += mbcnt_e;
mbcnt_e = 0;
chk->send_size = tot_demand;
chk->data->m_pkthdr.len = tot_demand;
chk->book_size = chk->send_size;
if (chk->flags & SCTP_PR_SCTP_BUFFER) {
asoc->sent_queue_cnt_removeable++;
}
TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
tot_out -= tot_demand;
}
if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
strq->next_sequence_sent++;
}
chk = TAILQ_FIRST(&tmp);
chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
mutex_enter(softnet_lock);
if ((asoc->state == 0) ||
(my_vtag != asoc->my_vtag) ||
(so != inp->sctp_socket) ||
(inp->sctp_socket == 0)) {
mutex_exit(softnet_lock);
error = ECONNRESET;
goto temp_clean_up;
}
chk = TAILQ_FIRST(&tmp);
while (chk) {
chk->data->m_nextpkt = 0;
TAILQ_REMOVE(&tmp, chk, sctp_next);
asoc->stream_queue_cnt++;
TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
chk = TAILQ_FIRST(&tmp);
}
if ((strq->next_spoke.tqe_next == NULL) &&
(strq->next_spoke.tqe_prev == NULL)) {
sctp_insert_on_wheel(asoc, strq);
}
mutex_exit(softnet_lock);
temp_clean_up:
if (error) {
chk = TAILQ_FIRST(&tmp);
while (chk) {
sctp_m_freem(chk->data);
chk->data = NULL;
TAILQ_REMOVE(&tmp, chk, sctp_next);
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
asoc->chunks_on_out_queue--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&tmp);
}
goto release;
}
}
zap_by_it_now:
#ifdef SCTP_MBCNT_LOGGING
sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
asoc->total_output_queue_size,
dataout,
asoc->total_output_mbuf_queue_size,
mbcnt);
#endif
solock(so);
asoc->total_output_queue_size += dataout;
asoc->total_output_mbuf_queue_size += mbcnt;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
so->so_snd.sb_cc += dataout;
so->so_snd.sb_mbcnt += mbcnt;
}
if ((srcv->sinfo_flags & SCTP_EOF) &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
) {
int some_on_streamwheel = 0;
error = 0;
if (!TAILQ_EMPTY(&asoc->out_wheel)) {
struct sctp_stream_out *outs;
TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
if (!TAILQ_EMPTY(&outs->outqueue)) {
some_on_streamwheel = 1;
break;
}
}
}
if (TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(some_on_streamwheel == 0)) {
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("%s:%d sends a shutdown\n",
__FILE__,
__LINE__
);
}
#endif
sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
asoc->state = SCTP_STATE_SHUTDOWN_SENT;
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
asoc->primary_destination);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
asoc->primary_destination);
}
} else {
asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("++total out:%d total_mbuf_out:%d\n",
(int)asoc->total_output_queue_size,
(int)asoc->total_output_mbuf_queue_size);
}
#endif
release:
sbunlock(&so->so_snd);
out_locked:
sounlock(so);
sctp_m_freem(mm);
return (error);
}
int
sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
struct mbuf *top, struct mbuf *control, int flags, struct lwp *p)
{
int error, use_rcvinfo;
int queue_only = 0, queue_only_for_init=0;
int un_sent = 0;
int now_filled=0;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb=NULL;
struct sctp_sndrcvinfo srcv;
struct timeval now;
struct sctp_nets *net;
struct sctp_association *asoc;
struct sctp_inpcb *t_inp;
int create_lock_applied = 0;
error = use_rcvinfo = 0;
net = NULL;
stcb = NULL;
asoc = NULL;
t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
solock(so);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
error = EFAULT;
sounlock(so);
goto out;
}
if (addr) {
SCTP_ASOC_CREATE_LOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
error = EFAULT;
sounlock(so);
goto out;
}
create_lock_applied = 1;
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
(addr->sa_family == AF_INET6)) {
error = EINVAL;
sounlock(so);
goto out;
}
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
SCTP_INP_RUNLOCK(inp);
error = ENOTCONN;
sounlock(so);
goto out;
}
SCTP_TCB_LOCK(stcb);
SCTP_INP_RUNLOCK(inp);
net = stcb->asoc.primary_destination;
}
#ifdef SCTP_DEBUG
printf("sctp_sosend: get control\n");
#endif
if (control) {
if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
sizeof(srcv))) {
if (srcv.sinfo_flags & SCTP_SENDALL) {
sctppcbinfo.mbuf_track--;
sctp_m_freem(control);
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
return (sctp_sendall(inp, uio, top, &srcv));
}
use_rcvinfo = 1;
}
}
#ifdef SCTP_DEBUG
printf("sctp_sosend: doing lookup\n");
#endif
if (stcb == NULL) {
if (use_rcvinfo && srcv.sinfo_assoc_id) {
stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
if ((stcb) &&
(addr != NULL)) {
net = sctp_findnet(stcb, addr);
}
}
if (stcb == NULL) {
if (addr != NULL) {
SCTP_INP_WLOCK(inp);
SCTP_INP_INCR_REF(inp);
SCTP_INP_WUNLOCK(inp);
stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
}
}
}
if ((stcb == NULL) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
error = ENOTCONN;
sounlock(so);
goto out;
} else if ((stcb == NULL) && (addr == NULL)) {
error = ENOENT;
sounlock(so);
goto out;
} else if (stcb == NULL) {
if ((use_rcvinfo) &&
(srcv.sinfo_flags & SCTP_ABORT)) {
error = ENOENT;
sounlock(so);
goto out;
}
stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
if (stcb == NULL) {
sounlock(so);
goto out;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
} else {
printf("Huh-3? create lock should have been on??\n");
}
queue_only = 1;
asoc = &stcb->asoc;
asoc->state = SCTP_STATE_COOKIE_WAIT;
SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
if (control) {
struct sctp_initmsg initm;
int i;
if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
if (initm.sinit_max_attempts)
asoc->max_init_times = initm.sinit_max_attempts;
if (initm.sinit_num_ostreams)
asoc->pre_open_streams = initm.sinit_num_ostreams;
if (initm.sinit_max_instreams)
asoc->max_inbound_streams = initm.sinit_max_instreams;
if (initm.sinit_max_init_timeo)
asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
if (asoc->streamoutcnt < asoc->pre_open_streams) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("Ok, defout:%d pre_open:%d\n",
asoc->streamoutcnt, asoc->pre_open_streams);
}
#endif
free(asoc->strmout, M_PCB);
asoc->strmout = NULL;
asoc->streamoutcnt = asoc->pre_open_streams;
asoc->strmout = malloc(
asoc->streamoutcnt *
sizeof(struct sctp_stream_out),
M_PCB, M_WAIT);
for (i = 0; i < asoc->streamoutcnt; i++) {
asoc->strmout[i].next_sequence_sent = 0x0;
TAILQ_INIT(&asoc->strmout[i].outqueue);
asoc->strmout[i].stream_no = i;
asoc->strmout[i].next_spoke.tqe_next = 0;
asoc->strmout[i].next_spoke.tqe_prev = 0;
}
}
}
}
queue_only_for_init = 1;
sctp_send_initiate(inp, stcb);
net = stcb->asoc.primary_destination;
asoc = &stcb->asoc;
} else {
asoc = &stcb->asoc;
}
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
queue_only = 1;
}
if (use_rcvinfo == 0) {
srcv = stcb->asoc.def_send;
}
sctp_m_freem(control);
control = NULL;
if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
(SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
(asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
if ((use_rcvinfo) &&
(srcv.sinfo_flags & SCTP_ABORT)) {
;
} else {
error = ECONNRESET;
sounlock(so);
goto out;
}
}
#if 0
if (p)
p->p_stats->p_ru.ru_msgsnd++;
#endif
if (stcb) {
if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
;
} else {
net = stcb->asoc.primary_destination;
}
}
#ifdef SCTP_DEBUG
printf("sctp_sosend: before copying in %p\n", top);
#endif
if (top == NULL) {
sounlock(so);
#ifdef SCTP_DEBUG
printf("sctp_sosend: before cii\n");
#endif
error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
#ifdef SCTP_DEBUG
printf("sctp_sosend: after cii\n");
#endif
if (error)
goto out;
} else {
error = sctp_msg_append(stcb, net, top, &srcv, flags);
sounlock(so);
if (error)
goto out;
top = 0;
}
#ifdef SCTP_DEBUG
printf("sctp_sosend: after copying in\n");
#endif
if (net->flight_size > net->cwnd) {
sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
queue_only = 1;
} else if (asoc->ifp_had_enobuf) {
sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
queue_only = 1;
} else {
un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
SCTP_MED_OVERHEAD);
if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
(stcb->asoc.total_flight > 0) &&
(un_sent < (int)stcb->asoc.smallest_mtu)) {
sctp_pegs[SCTP_NAGLE_NOQ]++;
queue_only = 1;
} else {
sctp_pegs[SCTP_NAGLE_OFF]++;
}
}
if (queue_only_for_init) {
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
queue_only = 0;
}
}
#ifdef SCTP_DEBUG
printf("sctp_sosend: before sending chunk\n");
#endif
if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("USR Send calls sctp_chunk_output\n");
}
#endif
solock(so);
sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
sctp_chunk_output(inp, stcb, 0);
sounlock(so);
} else if ((queue_only == 0) &&
(stcb->asoc.peers_rwnd == 0) &&
(stcb->asoc.total_flight == 0)) {
solock(so);
sctp_from_user_send = 1;
sctp_chunk_output(inp, stcb, 0);
sctp_from_user_send = 0;
sounlock(so);
} else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
int num_out, reason, cwnd_full;
solock(so);
sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
&reason, 1, &cwnd_full, 1, &now, &now_filled);
sounlock(so);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
queue_only, stcb->asoc.peers_rwnd, un_sent,
stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
stcb->asoc.total_output_queue_size);
}
#endif
out:
if (create_lock_applied) {
SCTP_ASOC_CREATE_UNLOCK(inp);
create_lock_applied = 0;
}
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
sctp_m_freem(top);
sctp_m_freem(control);
return (error);
}