#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sctp_indata.c,v 1.19 2025/11/19 22:31:51 andvar 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/mbuf.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/route.h>
#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
#include <sys/limits.h>
#else
#include <machine/limits.h>
#endif
#include <machine/cpu.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifdef INET6
#include <netinet/ip6.h>
#endif
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#ifdef INET6
#include <netinet6/ip6_var.h>
#endif
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/sctp_var.h>
#include <netinet/sctp_pcb.h>
#include <netinet/sctp_header.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_output.h>
#include <netinet/sctp_input.h>
#include <netinet/sctp_hashdriver.h>
#include <netinet/sctp_indata.h>
#include <netinet/sctp_uio.h>
#include <netinet/sctp_timer.h>
#ifdef IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#endif
#ifdef SCTP_DEBUG
extern u_int32_t sctp_debug_on;
#endif
extern int sctp_strict_sacks;
void
sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
{
u_int32_t calc, calc_w_oh;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
printf("cc:%lu hiwat:%lu lowat:%lu mbcnt:%lu mbmax:%lu\n",
(u_long)stcb->sctp_socket->so_rcv.sb_cc,
(u_long)stcb->sctp_socket->so_rcv.sb_hiwat,
(u_long)stcb->sctp_socket->so_rcv.sb_lowat,
(u_long)stcb->sctp_socket->so_rcv.sb_mbcnt,
(u_long)stcb->sctp_socket->so_rcv.sb_mbmax);
printf("Setting rwnd to: sb:%ld - (del:%d + reasm:%d str:%d)\n",
sctp_sbspace(&stcb->sctp_socket->so_rcv),
asoc->size_on_delivery_queue,
asoc->size_on_reasm_queue,
asoc->size_on_all_streams);
}
#endif
if (stcb->sctp_socket->so_rcv.sb_cc == 0 &&
asoc->size_on_delivery_queue == 0 &&
asoc->size_on_reasm_queue == 0 &&
asoc->size_on_all_streams == 0) {
asoc->my_rwnd = uimax(stcb->sctp_socket->so_rcv.sb_hiwat,
SCTP_MINIMAL_RWND);
return;
}
calc = (u_int32_t)sctp_sbspace(&stcb->sctp_socket->so_rcv);
calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_delivery_queue);
calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_reasm_queue);
calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_all_streams);
calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
asoc->my_rwnd = calc;
if (calc_w_oh == 0) {
asoc->my_rwnd = 1;
} else {
if (asoc->my_rwnd &&
(asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
asoc->my_rwnd = 1;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
printf(" - SWS zeros\n");
}
} else {
if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
printf("\n");
}
#endif
}
}
}
static struct mbuf *
sctp_build_ctl_nchunk(struct sctp_tcb *stcb, uint32_t tsn, uint32_t ppid,
uint32_t context, uint16_t stream_no, uint16_t stream_seq, uint8_t flags)
{
struct sctp_sndrcvinfo *outinfo;
struct cmsghdr *cmh;
struct mbuf *ret;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) == 0) {
return (NULL);
}
MGETHDR(ret, M_DONTWAIT, MT_CONTROL);
if (ret == NULL) {
return (ret);
}
cmh = mtod(ret, struct cmsghdr *);
outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
cmh->cmsg_level = IPPROTO_SCTP;
cmh->cmsg_type = SCTP_SNDRCV;
cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
outinfo->sinfo_stream = stream_no;
outinfo->sinfo_ssn = stream_seq;
if (flags & SCTP_DATA_UNORDERED) {
outinfo->sinfo_flags = SCTP_UNORDERED;
} else {
outinfo->sinfo_flags = 0;
}
outinfo->sinfo_ppid = ppid;
outinfo->sinfo_context = context;
outinfo->sinfo_assoc_id = sctp_get_associd(stcb);
outinfo->sinfo_tsn = tsn;
outinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
ret->m_len = cmh->cmsg_len;
ret->m_pkthdr.len = ret->m_len;
stcb->asoc.my_rwnd_control_len +=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
return (ret);
}
static
struct mbuf *
sctp_build_ctl(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk)
{
struct sctp_sndrcvinfo *outinfo;
struct cmsghdr *cmh;
struct mbuf *ret;
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) == 0) {
return (NULL);
}
MGET(ret, M_DONTWAIT, MT_CONTROL);
if (ret == NULL) {
return (ret);
}
cmh = mtod(ret, struct cmsghdr *);
outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
cmh->cmsg_level = IPPROTO_SCTP;
cmh->cmsg_type = SCTP_SNDRCV;
cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
outinfo->sinfo_stream = chk->rec.data.stream_number;
outinfo->sinfo_ssn = chk->rec.data.stream_seq;
if (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
outinfo->sinfo_flags = SCTP_UNORDERED;
} else {
outinfo->sinfo_flags = 0;
}
outinfo->sinfo_ppid = chk->rec.data.payloadtype;
outinfo->sinfo_context = chk->rec.data.context;
outinfo->sinfo_assoc_id = sctp_get_associd(stcb);
outinfo->sinfo_tsn = chk->rec.data.TSN_seq;
outinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
ret->m_len = cmh->cmsg_len;
stcb->asoc.my_rwnd_control_len +=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
return (ret);
}
int
sctp_deliver_data(struct sctp_tcb *stcb, struct sctp_association *asoc,
struct sctp_tmit_chunk *chk, int hold_locks)
{
struct mbuf *control, *m;
int free_it;
struct sockaddr_in6 sin6;
const struct sockaddr *to;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("I am now in Deliver data! (%p)\n", chk);
}
#endif
if (hold_locks == 0) {
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_WLOCK(stcb->sctp_ep);
SCTP_TCB_LOCK(stcb);
}
free_it = 0;
if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("gone is gone!\n");
}
#endif
if (chk != NULL) {
sctp_m_freem(chk->data);
chk->data = NULL;
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++;
}
TAILQ_FOREACH(chk, &asoc->delivery_queue, sctp_next) {
asoc->size_on_delivery_queue -= chk->send_size;
asoc->cnt_on_delivery_queue--;
sctp_m_freem(chk->data);
chk->data = NULL;
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++;
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return (0);
}
if (chk != NULL) {
TAILQ_INSERT_TAIL(&asoc->delivery_queue, chk, sctp_next);
asoc->size_on_delivery_queue += chk->send_size;
asoc->cnt_on_delivery_queue++;
}
if (asoc->fragmented_delivery_inprogress) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Fragmented delivery in progress?\n");
}
#endif
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return (0);
}
chk = TAILQ_FIRST(&asoc->delivery_queue);
if (chk == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Nothing in queue?\n");
}
#endif
asoc->size_on_delivery_queue = 0;
asoc->cnt_on_delivery_queue = 0;
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return (0);
}
if (stcb->sctp_socket->so_rcv.sb_cc >= stcb->sctp_socket->so_rcv.sb_hiwat) {
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return (0);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Now to the delivery with chk(%p)!\n", chk);
}
#endif
if ((chk->data->m_flags & M_PKTHDR) == 0) {
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return (0);
}
m->m_pkthdr.len = chk->send_size;
m->m_len = 0;
m->m_next = chk->data;
chk->data = m;
}
if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
if (chk->data->m_next == NULL) {
chk->data->m_flags |= M_EOR;
} else {
m = chk->data;
while (m->m_next != NULL) {
m = m->m_next;
}
m->m_flags |= M_EOR;
}
}
if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
struct sockaddr_in6 lsa6;
control = sctp_build_ctl(stcb, chk);
to = rtcache_getdst(&chk->whoTo->ro);
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
to->sa_family == AF_INET) {
const struct sockaddr_in *sin;
sin = (const struct sockaddr_in *)to;
in6_sin_2_v4mapsin6(sin, &sin6);
to = (struct sockaddr *)&sin6;
}
to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
&lsa6);
if (((const struct sockaddr_in *)to)->sin_port == 0) {
printf("Huh a, port is %d not net:%p %d?\n",
((const struct sockaddr_in *)to)->sin_port,
chk->whoTo,
(int)(ntohs(stcb->rport)));
}
if (sctp_sbspace(&stcb->sctp_socket->so_rcv) < (long)chk->send_size) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
goto skip;
}
if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv,
to, chk->data, control, stcb->asoc.my_vtag,
stcb->sctp_ep)) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
} else {
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
stcb->asoc.my_rwnd_control_len +=
sizeof(struct mbuf);
}
} else {
stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
}
free_it = 1;
}
} else {
if (sctp_sbspace(&stcb->sctp_socket->so_rcv) >=
(long)chk->send_size) {
sbappend(&stcb->sctp_socket->so_rcv, chk->data);
free_it = 1;
}
}
skip:
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
if (free_it) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Free_it true, doing tickle wakeup\n");
}
#endif
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
asoc->size_on_delivery_queue -= chk->send_size;
asoc->cnt_on_delivery_queue--;
chk->data = NULL;
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++;
}
return (free_it);
}
static void
sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc, int hold_locks)
{
const struct sockaddr *to;
struct sockaddr_in6 sin6;
struct sctp_tmit_chunk *chk, *at;
struct mbuf *control, *m;
u_int16_t nxt_todel;
u_int16_t stream_no;
int cntDel;
cntDel = stream_no = 0;
if (hold_locks == 0) {
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_WLOCK(stcb->sctp_ep);
SCTP_TCB_LOCK(stcb);
}
if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
asoc->fragmented_delivery_inprogress = 0;
TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
asoc->size_on_delivery_queue -= chk->send_size;
asoc->cnt_on_delivery_queue--;
sctp_m_freem(chk->data);
chk->data = NULL;
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++;
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
do {
if (stcb->sctp_socket->so_rcv.sb_cc >=
stcb->sctp_socket->so_rcv.sb_hiwat) {
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
chk = TAILQ_FIRST(&asoc->reasmqueue);
if (chk == NULL) {
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
stream_no = chk->rec.data.stream_number;
nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
if (nxt_todel != chk->rec.data.stream_seq &&
(chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
if ((chk->data->m_flags & M_PKTHDR) == 0) {
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
m->m_pkthdr.len = chk->send_size;
m->m_len = 0;
m->m_next = chk->data;
chk->data = m;
}
if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
if (chk->data->m_next == NULL) {
chk->data->m_flags |= M_EOR;
} else {
m = chk->data;
while (m->m_next != NULL) {
m = m->m_next;
}
m->m_flags |= M_EOR;
}
}
if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
struct sockaddr_in6 lsa6;
control = sctp_build_ctl(stcb, chk);
to = rtcache_getdst(&chk->whoTo->ro);
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
to->sa_family == AF_INET) {
const struct sockaddr_in *sin;
sin = satocsin(to);
in6_sin_2_v4mapsin6(sin, &sin6);
to = (struct sockaddr *)&sin6;
}
to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
&lsa6);
if (((const struct sockaddr_in *)to)->sin_port == 0) {
printf("Huh b, port is %d not net:%p %d?\n",
((const struct sockaddr_in *)to)->sin_port,
chk->whoTo,
(int)(ntohs(stcb->rport)));
}
if (sctp_sbspace(&stcb->sctp_socket->so_rcv) <
(long)chk->send_size) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv,
to, chk->data, control, stcb->asoc.my_vtag,
stcb->sctp_ep)) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
stcb->asoc.my_rwnd_control_len +=
sizeof(struct mbuf);
}
} else {
stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
}
cntDel++;
} else {
if (sctp_sbspace(&stcb->sctp_socket->so_rcv) >=
(long)chk->send_size) {
sbappend(&stcb->sctp_socket->so_rcv, chk->data);
cntDel++;
} else {
sctp_sorwakeup(stcb->sctp_ep,
stcb->sctp_socket);
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
}
TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
asoc->fragmented_delivery_inprogress = 0;
if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
asoc->strmin[stream_no].last_sequence_delivered++;
}
}
asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
asoc->size_on_reasm_queue -= chk->send_size;
asoc->cnt_on_reasm_queue--;
sctp_free_remote_addr(chk->whoTo);
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++;
if (asoc->fragmented_delivery_inprogress == 0) {
struct sctp_stream_in *strm;
strm = &asoc->strmin[stream_no];
nxt_todel = strm->last_sequence_delivered + 1;
chk = TAILQ_FIRST(&strm->inqueue);
if (chk && (nxt_todel == chk->rec.data.stream_seq)) {
while (chk != NULL) {
if (nxt_todel ==
chk->rec.data.stream_seq) {
at = TAILQ_NEXT(chk, sctp_next);
TAILQ_REMOVE(&strm->inqueue,
chk, sctp_next);
asoc->size_on_all_streams -=
chk->send_size;
asoc->cnt_on_all_streams--;
strm->last_sequence_delivered++;
sctp_deliver_data(stcb, asoc, chk, 1);
chk = at;
} else {
break;
}
nxt_todel =
strm->last_sequence_delivered + 1;
}
}
if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
if (sctp_deliver_data(stcb, asoc, NULL, 1) == 0)
break;
}
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
return;
}
chk = TAILQ_FIRST(&asoc->reasmqueue);
} while (chk);
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
}
if (hold_locks == 0) {
SCTP_INP_WUNLOCK(stcb->sctp_ep);
}
}
static void
sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
struct sctp_tmit_chunk *chk, int *abort_flag)
{
struct sctp_stream_in *strm;
struct sctp_tmit_chunk *at;
int queue_needed;
u_int16_t nxt_todel;
struct mbuf *oper;
queue_needed = 1;
asoc->size_on_all_streams += chk->send_size;
asoc->cnt_on_all_streams++;
strm = &asoc->strmin[chk->rec.data.stream_number];
nxt_todel = strm->last_sequence_delivered + 1;
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
(u_int)chk->rec.data.stream_seq,
(u_int)strm->last_sequence_delivered, (u_int)nxt_todel);
}
#endif
if (compare_with_wrap(strm->last_sequence_delivered,
chk->rec.data.stream_seq, MAX_SEQ) ||
(strm->last_sequence_delivered == chk->rec.data.stream_seq)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
chk->rec.data.stream_seq,
strm->last_sequence_delivered);
}
#endif
TAILQ_INSERT_HEAD(&strm->inqueue, chk, sctp_next);
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len = sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x00000001);
}
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
if (nxt_todel == chk->rec.data.stream_seq) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("It's NEXT!\n");
}
#endif
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
#endif
queue_needed = 0;
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
strm->last_sequence_delivered++;
sctp_deliver_data(stcb, asoc, chk, 0);
chk = TAILQ_FIRST(&strm->inqueue);
while (chk != NULL) {
nxt_todel = strm->last_sequence_delivered + 1;
if (nxt_todel == chk->rec.data.stream_seq) {
at = TAILQ_NEXT(chk, sctp_next);
TAILQ_REMOVE(&strm->inqueue, chk, sctp_next);
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
strm->last_sequence_delivered++;
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, NULL,
SCTP_STR_LOG_FROM_IMMED_DEL);
#endif
sctp_deliver_data(stcb, asoc, chk, 0);
chk = at;
continue;
}
break;
}
}
if (queue_needed) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Queue Needed!\n");
}
#endif
if (TAILQ_EMPTY(&strm->inqueue)) {
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
#endif
TAILQ_INSERT_HEAD(&strm->inqueue, chk, sctp_next);
} else {
TAILQ_FOREACH(at, &strm->inqueue, sctp_next) {
if (compare_with_wrap(at->rec.data.stream_seq,
chk->rec.data.stream_seq, MAX_SEQ)) {
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, at,
SCTP_STR_LOG_FROM_INSERT_MD);
#endif
TAILQ_INSERT_BEFORE(at, chk, sctp_next);
break;
} else if (at->rec.data.stream_seq ==
chk->rec.data.stream_seq) {
sctp_m_freem(chk->data);
chk->data = NULL;
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
sctp_pegs[SCTP_DUP_SSN_RCVD]++;
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++;
return;
} else {
if (TAILQ_NEXT(at, sctp_next) == NULL) {
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del(chk, at,
SCTP_STR_LOG_FROM_INSERT_TL);
#endif
TAILQ_INSERT_AFTER(&strm->inqueue,
at, chk, sctp_next);
break;
}
}
}
}
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Doing WAKEUP!\n");
}
#endif
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
}
}
static int
sctp_is_all_msg_on_reasm(struct sctp_association *asoc, int *t_size)
{
struct sctp_tmit_chunk *chk;
u_int32_t tsn;
*t_size = 0;
chk = TAILQ_FIRST(&asoc->reasmqueue);
if (chk == NULL) {
return (0);
}
if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
return (0);
}
tsn = chk->rec.data.TSN_seq;
while (chk) {
if (tsn != chk->rec.data.TSN_seq) {
return (0);
}
*t_size += chk->send_size;
if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
return (1);
}
tsn++;
chk = TAILQ_NEXT(chk, sctp_next);
}
return (0);
}
static void
sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
struct sctp_tmit_chunk *chk, int *abort_flag)
{
struct mbuf *oper;
u_int16_t nxt_todel;
u_int32_t cum_ackp1, prev_tsn, post_tsn;
int tsize;
struct sctp_tmit_chunk *at, *prev, *next;
prev = next = NULL;
cum_ackp1 = asoc->tsn_last_delivered + 1;
if (TAILQ_EMPTY(&asoc->reasmqueue)) {
TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
asoc->size_on_reasm_queue = chk->send_size;
asoc->cnt_on_reasm_queue++;
if (chk->rec.data.TSN_seq == cum_ackp1) {
if (asoc->fragmented_delivery_inprogress == 0 &&
(chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
SCTP_DATA_FIRST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Gak, Evil plot, its not first, no fragmented delivery in progress\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000001);
}
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
} else if (asoc->fragmented_delivery_inprogress &&
(chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000002);
}
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
} else if (asoc->fragmented_delivery_inprogress) {
if (chk->rec.data.stream_number !=
asoc->str_of_pdapi) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Gak, Evil plot, it IS not same stream number %d vs %d\n",
chk->rec.data.stream_number,
asoc->str_of_pdapi);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000003);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
SCTP_DATA_UNORDERED &&
chk->rec.data.stream_seq !=
asoc->ssn_of_pdapi) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Gak, Evil plot, it IS not same stream seq %d vs %d\n",
chk->rec.data.stream_seq,
asoc->ssn_of_pdapi);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000004);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
}
}
}
return;
}
at = TAILQ_FIRST(&asoc->reasmqueue);
TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
if (compare_with_wrap(at->rec.data.TSN_seq,
chk->rec.data.TSN_seq, MAX_TSN)) {
asoc->size_on_reasm_queue += chk->send_size;
asoc->cnt_on_reasm_queue++;
next = at;
TAILQ_INSERT_BEFORE(at, chk, sctp_next);
break;
} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
sctp_m_freem(chk->data);
chk->data = NULL;
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++;
return;
} else {
prev = at;
if (TAILQ_NEXT(at, sctp_next) == NULL) {
asoc->size_on_reasm_queue += chk->send_size;
asoc->cnt_on_reasm_queue++;
TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
break;
}
}
}
if (prev) {
prev_tsn = chk->rec.data.TSN_seq - 1;
if (prev_tsn == prev->rec.data.TSN_seq) {
if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_FIRST_FRAG ||
(prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_MIDDLE_FRAG) {
if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_FIRST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Prev check - It can be a midlle or last but not a first\n");
printf("Gak, Evil plot, it's a FIRST!\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000005);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
if (chk->rec.data.stream_number !=
prev->rec.data.stream_number) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
chk->rec.data.stream_number,
prev->rec.data.stream_number);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000006);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
chk->rec.data.stream_seq !=
prev->rec.data.stream_seq) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
chk->rec.data.stream_seq,
prev->rec.data.stream_seq);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000007);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_LAST_FRAG) {
if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
SCTP_DATA_FIRST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Prev check - Gak, evil plot, its not FIRST and it must be!\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000008);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
}
}
}
if (next) {
post_tsn = chk->rec.data.TSN_seq + 1;
if (post_tsn == next->rec.data.TSN_seq) {
if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
if ((chk->rec.data.rcv_flags&SCTP_DATA_FRAG_MASK)
!= SCTP_DATA_LAST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Next chk - Next is FIRST, we must be LAST\n");
printf("Gak, Evil plot, its not a last!\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x10000009);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_MIDDLE_FRAG ||
(next->rec.data.rcv_flags&SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_LAST_FRAG) {
if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
SCTP_DATA_LAST_FRAG) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Next chk - Next is a MIDDLE/LAST\n");
printf("Gak, Evil plot, new prev chunk is a LAST\n");
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x1000000a);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
if (chk->rec.data.stream_number !=
next->rec.data.stream_number) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
chk->rec.data.stream_number,
next->rec.data.stream_number);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x1000000b);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
chk->rec.data.stream_seq !=
next->rec.data.stream_seq) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
chk->rec.data.stream_seq,
next->rec.data.stream_seq);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x1000000c);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
return;
}
}
}
}
chk = TAILQ_FIRST(&asoc->reasmqueue);
if (chk == NULL) {
asoc->size_on_reasm_queue = 0;
asoc->cnt_on_reasm_queue = 0;
return;
}
if (asoc->fragmented_delivery_inprogress == 0) {
nxt_todel =
asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
(nxt_todel == chk->rec.data.stream_seq ||
(chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
if (TAILQ_EMPTY(&asoc->delivery_queue) &&
(sctp_is_all_msg_on_reasm(asoc, &tsize) ||
(asoc->size_on_reasm_queue >=
(stcb->sctp_socket->so_rcv.sb_hiwat >> 2) &&
tsize))) {
asoc->fragmented_delivery_inprogress = 1;
asoc->tsn_last_delivered =
chk->rec.data.TSN_seq - 1;
asoc->str_of_pdapi =
chk->rec.data.stream_number;
asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
asoc->fragment_flags = chk->rec.data.rcv_flags;
sctp_service_reassembly(stcb, asoc, 0);
}
}
} else {
sctp_service_reassembly(stcb, asoc, 0);
}
}
static int
sctp_does_chk_belong_to_reasm(struct sctp_association *asoc,
struct sctp_tmit_chunk *chk)
{
struct sctp_tmit_chunk *at;
u_int32_t tsn_est;
TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
if (compare_with_wrap(chk->rec.data.TSN_seq,
at->rec.data.TSN_seq, MAX_TSN)) {
tsn_est = at->rec.data.TSN_seq + 1;
if (tsn_est == chk->rec.data.TSN_seq) {
if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
SCTP_DATA_LAST_FRAG) {
return (1);
} else {
return (0);
}
}
} else if (chk->rec.data.TSN_seq == at->rec.data.TSN_seq) {
return (1);
} else {
tsn_est = chk->rec.data.TSN_seq + 1;
if (tsn_est == at->rec.data.TSN_seq) {
if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
SCTP_DATA_FIRST_FRAG) {
return (1);
} else {
return (0);
}
}
}
}
return (0);
}
extern unsigned int sctp_max_chunks_on_queue;
static int
sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
struct sctp_nets *net, u_int32_t *high_tsn, int *abort_flag,
int *break_flag, int last_chunk)
{
struct sctp_tmit_chunk *chk;
u_int32_t tsn, gap;
struct mbuf *dmbuf;
int the_len;
u_int16_t strmno, strmseq;
struct mbuf *oper;
chk = NULL;
tsn = ntohl(ch->dp.tsn);
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, tsn, asoc->cumulative_tsn, SCTP_MAP_PREPARE_SLIDE);
#endif
if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
asoc->cumulative_tsn == tsn) {
sctp_pegs[SCTP_DUPTSN_RECVD]++;
if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
asoc->dup_tsns[asoc->numduptsns] = tsn;
asoc->numduptsns++;
}
return (0);
}
if (tsn >= asoc->mapping_array_base_tsn) {
gap = tsn - asoc->mapping_array_base_tsn;
} else {
gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
}
if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
return (0);
}
if (gap >= (uint32_t)(asoc->mapping_array_size << 3)) {
if (sctp_expand_mapping_array(asoc)) {
return (0);
}
}
if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
*high_tsn = tsn;
}
if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
sctp_pegs[SCTP_DUPTSN_RECVD]++;
if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
asoc->dup_tsns[asoc->numduptsns] = tsn;
asoc->numduptsns++;
}
if (!callout_pending(&asoc->dack_timer.timer)) {
sctp_timer_start(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep,
stcb, NULL);
}
return (0);
}
if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
struct mbuf *op_err;
op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
*abort_flag = 1;
return (0);
}
if (((asoc->cnt_on_all_streams +
asoc->cnt_on_delivery_queue +
asoc->cnt_on_reasm_queue +
asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
(((int)asoc->my_rwnd) <= 0)) {
if (stcb->sctp_socket->so_rcv.sb_cc) {
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
}
if (compare_with_wrap(tsn,
asoc->highest_tsn_inside_map, MAX_TSN)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("My rwnd overrun1:tsn:%lx rwnd %lu sbspace:%ld delq:%d!\n",
(u_long)tsn, (u_long)asoc->my_rwnd,
sctp_sbspace(&stcb->sctp_socket->so_rcv),
stcb->asoc.cnt_on_delivery_queue);
}
#endif
sctp_set_rwnd(stcb, asoc);
if ((asoc->cnt_on_all_streams +
asoc->cnt_on_delivery_queue +
asoc->cnt_on_reasm_queue +
asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
sctp_pegs[SCTP_MSGC_DROP]++;
} else {
sctp_pegs[SCTP_RWND_DROPS]++;
}
*break_flag = 1;
return (0);
}
}
strmno = ntohs(ch->dp.stream_id);
if (strmno >= asoc->streamincnt) {
struct sctp_paramhdr *phdr;
struct mbuf *mb;
MGETHDR(mb, M_DONTWAIT, MT_DATA);
if (mb != NULL) {
mb->m_data += sizeof(struct sctp_chunkhdr);
phdr = mtod(mb, struct sctp_paramhdr *);
mb->m_pkthdr.len = mb->m_len =
(sizeof(struct sctp_paramhdr) * 2);
phdr->param_type = htons(SCTP_CAUSE_INV_STRM);
phdr->param_length =
htons(sizeof(struct sctp_paramhdr) * 2);
phdr++;
phdr->param_type = ch->dp.stream_id;
phdr->param_length = 0;
sctp_queue_op_err(stcb, mb);
}
sctp_pegs[SCTP_BAD_STRMNO]++;
return (0);
}
strmseq = ntohs(ch->dp.stream_sequence);
if ((ch->ch.chunk_flags & SCTP_DATA_FIRST_FRAG) &&
(ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
(compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
strmseq, MAX_SEQ) ||
asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
strmseq,
asoc->strmin[strmno].last_sequence_delivered);
}
#endif
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len = sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x20000001);
}
sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY,
oper);
sctp_pegs[SCTP_BAD_SSN_WRAP]++;
*abort_flag = 1;
return (0);
}
the_len = (chk_length-sizeof(struct sctp_data_chunk));
if (last_chunk == 0) {
dmbuf = sctp_m_copym(*m,
(offset + sizeof(struct sctp_data_chunk)),
the_len, M_DONTWAIT);
} else {
dmbuf = *m;
m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
if (dmbuf->m_pkthdr.len > the_len) {
m_adj(dmbuf, -(dmbuf->m_pkthdr.len-the_len));
}
sctp_pegs[SCTP_NO_COPY_IN]++;
}
if (dmbuf == NULL) {
sctp_pegs[SCTP_DROP_NOMEMORY]++;
return (0);
}
if ((ch->ch.chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
asoc->fragmented_delivery_inprogress == 0 &&
TAILQ_EMPTY(&asoc->delivery_queue) &&
((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) ||
((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
TAILQ_EMPTY(&asoc->strmin[strmno].inqueue))) &&
((long)(stcb->sctp_socket->so_rcv.sb_hiwat -
stcb->sctp_socket->so_rcv.sb_cc) >= (long)the_len)) {
struct mbuf *control, *mmm;
struct sockaddr_in6 sin6;
struct sockaddr_in6 lsa6;
const struct sockaddr *to;
control = sctp_build_ctl_nchunk(stcb, tsn,
ch->dp.protocol_id, 0, strmno, strmseq,
ch->ch.chunk_flags);
if ((dmbuf->m_flags & M_PKTHDR) == 0) {
struct mbuf *tmp;
MGETHDR(tmp, M_DONTWAIT, MT_DATA);
if (tmp == NULL) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
goto failed_express_del;
}
tmp->m_pkthdr.len = the_len;
tmp->m_len = 0;
tmp->m_next = dmbuf;
dmbuf = tmp;
}
to = rtcache_getdst(&net->ro);
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
to->sa_family == AF_INET) {
const struct sockaddr_in *sin;
sin = satocsin(to);
in6_sin_2_v4mapsin6(sin, &sin6);
to = (struct sockaddr *)&sin6;
}
to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
&lsa6);
if (((const struct sockaddr_in *)to)->sin_port == 0) {
printf("Huh c, port is %d not net:%p %d?\n",
((const struct sockaddr_in *)to)->sin_port,
net,
(int)(ntohs(stcb->rport)));
}
mmm = dmbuf;
while (mmm->m_next != NULL) {
mmm = mmm->m_next;
}
mmm->m_flags |= M_EOR;
if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
asoc->highest_tsn_inside_map = tsn;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 1, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
}
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_WLOCK(stcb->sctp_ep);
SCTP_TCB_LOCK(stcb);
if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv, to, dmbuf,
control, stcb->asoc.my_vtag, stcb->sctp_ep)) {
if (control) {
sctp_m_freem(control);
stcb->asoc.my_rwnd_control_len -=
CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
}
sctp_m_freem(dmbuf);
goto failed_express_del;
}
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
}
} else {
stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
}
SCTP_INP_WUNLOCK(stcb->sctp_ep);
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
if ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0) {
asoc->strmin[strmno].last_sequence_delivered++;
}
sctp_pegs[SCTP_EXPRESS_ROUTE]++;
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del_alt(tsn, strmseq,
SCTP_STR_LOG_FROM_EXPRS_DEL);
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Express Delivery succeeds\n");
}
#endif
goto finish_express_del;
}
failed_express_del:
chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
if (chk == NULL) {
sctp_pegs[SCTP_DROP_NOMEMORY]++;
if (last_chunk == 0) {
sctp_m_freem(dmbuf);
}
return (0);
}
sctppcbinfo.ipi_count_chunk++;
sctppcbinfo.ipi_gencnt_chunk++;
chk->rec.data.TSN_seq = tsn;
chk->rec.data.stream_seq = strmseq;
chk->rec.data.stream_number = strmno;
chk->rec.data.payloadtype = ch->dp.protocol_id;
chk->rec.data.context = 0;
chk->rec.data.doing_fast_retransmit = 0;
chk->rec.data.rcv_flags = ch->ch.chunk_flags;
chk->asoc = asoc;
chk->send_size = the_len;
chk->whoTo = net;
net->ref_count++;
chk->data = dmbuf;
if ((chk->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
SCTP_DATA_NOT_FRAG) {
if (asoc->fragmented_delivery_inprogress) {
u_int32_t estimate_tsn;
estimate_tsn = asoc->tsn_last_delivered + 1;
if (TAILQ_EMPTY(&asoc->reasmqueue) &&
(estimate_tsn == chk->rec.data.TSN_seq)) {
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x20000002);
}
sctp_abort_an_association(stcb->sctp_ep, stcb,
SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
sctp_pegs[SCTP_DROP_FRAG]++;
return (0);
} else {
if (sctp_does_chk_belong_to_reasm(asoc, chk)) {
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x20000003);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
sctp_pegs[SCTP_DROP_FRAG]++;
return (0);
}
}
} else {
if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
if (sctp_does_chk_belong_to_reasm(asoc, chk)) {
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len =
sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper,
struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length =
htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x20000004);
}
sctp_abort_an_association(stcb->sctp_ep,
stcb, SCTP_PEER_FAULTY, oper);
*abort_flag = 1;
sctp_pegs[SCTP_DROP_FRAG]++;
return (0);
}
}
}
if (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
sctp_deliver_data(stcb, asoc, chk, 0);
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
} else {
if ((asoc->pending_reply) &&
((compare_with_wrap(tsn, ntohl(asoc->pending_reply->reset_at_tsn), MAX_TSN)) ||
(tsn == ntohl(asoc->pending_reply->reset_at_tsn)))
) {
TAILQ_INSERT_TAIL(&asoc->pending_reply_queue , chk, sctp_next);
} else {
sctp_queue_data_to_stream(stcb, asoc, chk, abort_flag);
}
}
} else {
sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
if (*abort_flag) {
sctp_pegs[SCTP_DROP_FRAG]++;
return (0);
}
}
if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
asoc->highest_tsn_inside_map = tsn;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
}
finish_express_del:
if (last_chunk) {
*m = NULL;
}
sctp_pegs[SCTP_PEG_TSNS_RCVD]++;
#ifdef SCTP_STR_LOGGING
sctp_log_strm_del_alt(tsn, strmseq, SCTP_STR_LOG_FROM_MARK_TSN);
#endif
#ifdef SCTP_MAP_LOGGING
sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
#endif
SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
return (1);
}
void
sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
{
struct sctp_association *asoc;
int i, at;
int m_size, all_ones;
int slide_from, slide_end, lgap, distance;
#ifdef SCTP_MAP_LOGGING
uint32_t old_cumack, old_base, old_highest;
unsigned char aux_array[64];
#endif
asoc = &stcb->asoc;
at = 0;
#ifdef SCTP_MAP_LOGGING
old_cumack = asoc->cumulative_tsn;
old_base = asoc->mapping_array_base_tsn;
old_highest = asoc->highest_tsn_inside_map;
if (asoc->mapping_array_size < 64)
memcpy(aux_array, asoc->mapping_array,
asoc->mapping_array_size);
else
memcpy(aux_array, asoc->mapping_array, 64);
#endif
all_ones = 1;
m_size = stcb->asoc.mapping_array_size << 3;
for (i = 0; i < m_size; i++) {
if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
at = i;
all_ones = 0;
asoc->cumulative_tsn = asoc->mapping_array_base_tsn +
(i - 1);
break;
}
}
if (compare_with_wrap(asoc->cumulative_tsn,
asoc->highest_tsn_inside_map,
MAX_TSN)) {
panic("huh, cumack greater than high-tsn in map");
}
if (all_ones ||
(asoc->cumulative_tsn == asoc->highest_tsn_inside_map && at >= 8)) {
int clr;
asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
if (all_ones)
clr = asoc->mapping_array_size;
else {
clr = (at >> 3) + 1;
if (clr > asoc->mapping_array_size)
clr = asoc->mapping_array_size;
}
memset(asoc->mapping_array, 0, clr);
asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(old_base, old_cumack, old_highest,
SCTP_MAP_PREPARE_SLIDE);
sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
#endif
} else if (at >= 8) {
slide_from = at >> 3;
if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
lgap = asoc->highest_tsn_inside_map -
asoc->mapping_array_base_tsn;
} else {
lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
asoc->highest_tsn_inside_map + 1;
}
slide_end = lgap >> 3;
if (slide_end < slide_from) {
panic("impossible slide");
}
distance = (slide_end-slide_from) + 1;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(old_base, old_cumack, old_highest,
SCTP_MAP_PREPARE_SLIDE);
sctp_log_map((uint32_t)slide_from, (uint32_t)slide_end,
(uint32_t)lgap, SCTP_MAP_SLIDE_FROM);
#endif
if (distance + slide_from > asoc->mapping_array_size ||
distance < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Ugh bad addition.. you can't hrumpp!\n");
}
#endif
#ifdef SCTP_MAP_LOGGING
sctp_log_map((uint32_t)distance, (uint32_t)slide_from,
(uint32_t)asoc->mapping_array_size,
SCTP_MAP_SLIDE_NONE);
#endif
} else {
int ii;
for (ii = 0; ii < distance; ii++) {
asoc->mapping_array[ii] =
asoc->mapping_array[slide_from + ii];
}
for (ii = distance;ii <= slide_end; ii++) {
asoc->mapping_array[ii] = 0;
}
asoc->mapping_array_base_tsn += (slide_from << 3);
#ifdef SCTP_MAP_LOGGING
sctp_log_map(asoc->mapping_array_base_tsn,
asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
SCTP_MAP_SLIDE_RESULT);
#endif
}
}
if ((asoc->pending_reply) &&
((compare_with_wrap((asoc->cumulative_tsn+1), ntohl(asoc->pending_reply->reset_at_tsn), MAX_TSN)) ||
((asoc->cumulative_tsn+1) == ntohl(asoc->pending_reply->reset_at_tsn)))
) {
struct sctp_tmit_chunk *chk;
sctp_handle_stream_reset_response(stcb, asoc->pending_reply);
free(asoc->pending_reply, M_PCB);
asoc->pending_reply = NULL;
chk = TAILQ_FIRST(&asoc->pending_reply_queue);
while (chk) {
TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
sctp_queue_data_to_stream(stcb, asoc, chk, abort_flag);
if (*abort_flag) {
return;
}
chk = TAILQ_FIRST(&asoc->pending_reply_queue);
}
}
if (ok_to_sack) {
if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
if (callout_pending(&stcb->asoc.dack_timer.timer)) {
sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
}
#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);
sctp_send_sack(stcb);
} else {
int is_a_gap;
is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
stcb->asoc.cumulative_tsn, MAX_TSN);
if ((stcb->asoc.first_ack_sent == 0) ||
((was_a_gap) && (is_a_gap == 0)) ||
(stcb->asoc.numduptsns) ||
(is_a_gap) ||
(callout_pending(&stcb->asoc.dack_timer.timer))
) {
stcb->asoc.first_ack_sent = 1;
sctp_send_sack(stcb);
} else {
sctp_timer_start(SCTP_TIMER_TYPE_RECV,
stcb->sctp_ep, stcb, NULL);
}
}
}
}
void
sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc, int hold_locks)
{
struct sctp_tmit_chunk *chk;
int tsize, cntDel;
u_int16_t nxt_todel;
cntDel = 0;
if (asoc->fragmented_delivery_inprogress) {
sctp_service_reassembly(stcb, asoc, hold_locks);
}
if (asoc->fragmented_delivery_inprogress) {
return;
}
do {
if (stcb->sctp_socket->so_rcv.sb_cc >= stcb->sctp_socket->so_rcv.sb_hiwat) {
if (cntDel == 0)
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
break;
}
if (sctp_deliver_data(stcb, asoc, (struct sctp_tmit_chunk *)NULL, hold_locks) == 0)
break;
cntDel++;
chk = TAILQ_FIRST(&asoc->delivery_queue);
} while (chk);
if (cntDel) {
sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
}
chk = TAILQ_FIRST(&asoc->reasmqueue);
if (chk == NULL) {
asoc->size_on_reasm_queue = 0;
asoc->cnt_on_reasm_queue = 0;
return;
}
nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
((nxt_todel == chk->rec.data.stream_seq) ||
(chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
if (TAILQ_EMPTY(&asoc->delivery_queue) &&
(sctp_is_all_msg_on_reasm(asoc, &tsize) ||
(asoc->size_on_reasm_queue >=
(stcb->sctp_socket->so_rcv.sb_hiwat >> 2) && tsize))) {
asoc->fragmented_delivery_inprogress = 1;
asoc->tsn_last_delivered = chk->rec.data.TSN_seq-1;
asoc->str_of_pdapi = chk->rec.data.stream_number;
asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
asoc->fragment_flags = chk->rec.data.rcv_flags;
sctp_service_reassembly(stcb, asoc, hold_locks);
}
}
}
int
sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
struct sctp_nets *net, u_int32_t *high_tsn)
{
struct sctp_data_chunk *ch, chunk_buf;
struct sctp_association *asoc;
int num_chunks = 0;
int chk_length, break_flag, last_chunk;
int abort_flag = 0, was_a_gap = 0;
struct mbuf *m;
sctp_set_rwnd(stcb, &stcb->asoc);
m = *mm;
asoc = &stcb->asoc;
if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
stcb->asoc.cumulative_tsn, MAX_TSN)) {
was_a_gap = 1;
}
asoc->last_data_chunk_from = net;
if (m->m_len < (long)MHLEN && m->m_next == NULL) {
MGET(m, M_DONTWAIT, MT_DATA);
if (m) {
vaddr_t *from, *to;
if ((*mm)->m_flags & M_PKTHDR) {
#ifdef __APPLE__
M_COPY_PKTHDR(m, (*mm));
#else
m_move_pkthdr(m, (*mm));
#endif
}
to = mtod(m, vaddr_t *);
from = mtod((*mm), vaddr_t *);
memcpy(to, from, (*mm)->m_len);
m->m_len = (*mm)->m_len;
sctp_m_freem(*mm);
*mm = m;
} else {
m = *mm;
}
}
ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
sizeof(chunk_buf), (u_int8_t *)&chunk_buf);
if (ch == NULL) {
printf(" ... its short\n");
return (1);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("In process data off:%d length:%d iphlen:%d ch->type:%d\n",
*offset, length, iphlen, (int)ch->ch.chunk_type);
}
#endif
*high_tsn = asoc->cumulative_tsn;
break_flag = 0;
while (ch->ch.chunk_type == SCTP_DATA) {
chk_length = ntohs(ch->ch.chunk_length);
if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1 ||
length - *offset < chk_length) {
struct mbuf *op_err;
MGET(op_err, M_DONTWAIT, MT_DATA);
if (op_err) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
op_err->m_len = sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(op_err, struct sctp_paramhdr *);
ph->param_type =
htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(op_err->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x30000001);
}
sctp_abort_association(inp, stcb, m, iphlen, sh,
op_err);
return (2);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("A chunk of len:%d to process (tot:%d)\n",
chk_length, length - *offset);
}
#endif
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xB1, 0);
#endif
if (SCTP_SIZE32(chk_length) == *offset - length) {
last_chunk = 1;
} else {
last_chunk = 0;
}
if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
chk_length, net, high_tsn, &abort_flag, &break_flag,
last_chunk)) {
num_chunks++;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Now incr num_chunks to %d\n",
num_chunks);
}
#endif
}
if (abort_flag)
return (2);
if (break_flag) {
break;
}
*offset += SCTP_SIZE32(chk_length);
if (*offset >= length) {
break;
}
ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
sizeof(chunk_buf), (u_int8_t *)&chunk_buf);
if (ch == NULL) {
*offset = length;
break;
}
}
if (break_flag) {
sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
}
if (num_chunks) {
sctp_pegs[SCTP_DATA_DG_RECV]++;
stcb->asoc.overall_error_count = 0;
SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
}
sctp_service_queues(stcb, asoc, 0);
if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
sctp_timer_start(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb,
net);
}
sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
if (abort_flag)
return (2);
return (0);
}
static void
sctp_handle_segments(struct sctp_tcb *stcb, struct sctp_association *asoc,
struct sctp_sack_chunk *ch, u_long last_tsn, u_long *biggest_tsn_acked,
u_long *biggest_newly_acked_tsn, int num_seg, int *ecn_seg_sums)
{
struct sctp_sack *sack;
struct sctp_gap_ack_block *frag;
struct sctp_tmit_chunk *tp1;
int i;
unsigned int j;
#ifdef SCTP_FR_LOGGING
int num_frs=0;
#endif
uint16_t frag_strt, frag_end, primary_flag_set;
u_long last_frag_high;
if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
primary_flag_set = 1;
} else {
primary_flag_set = 0;
}
sack = &ch->sack;
frag = (struct sctp_gap_ack_block *)((vaddr_t)sack +
sizeof(struct sctp_sack));
tp1 = NULL;
last_frag_high = 0;
for (i = 0; i < num_seg; i++) {
frag_strt = ntohs(frag->start);
frag_end = ntohs(frag->end);
if (frag_strt > frag_end) {
frag++;
continue;
}
if (compare_with_wrap((frag_end+last_tsn), *biggest_tsn_acked,
MAX_TSN))
*biggest_tsn_acked = frag_end+last_tsn;
if (tp1 == NULL) {
tp1 = TAILQ_FIRST(&asoc->sent_queue);
last_frag_high = frag_end + last_tsn;
} else {
if (compare_with_wrap(frag_strt+last_tsn,
last_frag_high, MAX_TSN)) {
;
} else {
tp1 = TAILQ_FIRST(&asoc->sent_queue);
}
last_frag_high = frag_end + last_tsn;
}
for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
while (tp1) {
#ifdef SCTP_FR_LOGGING
if (tp1->rec.data.doing_fast_retransmit)
num_frs++;
#endif
if (tp1->rec.data.TSN_seq == j) {
if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
if (tp1->sent < SCTP_DATAGRAM_ACKED) {
if (compare_with_wrap(tp1->rec.data.TSN_seq,
*biggest_newly_acked_tsn,
MAX_TSN)) {
*biggest_newly_acked_tsn =
tp1->rec.data.TSN_seq;
}
sctp_flight_size_decrease(tp1);
sctp_total_flight_decrease(stcb, tp1);
if (tp1->snd_count < 2) {
tp1->whoTo->net_ack2 +=
tp1->send_size;
if (tp1->do_rtt) {
tp1->whoTo->RTO =
sctp_calculate_rto(stcb,
asoc,
tp1->whoTo,
&tp1->sent_rcv_time);
tp1->whoTo->rto_pending = 0;
tp1->do_rtt = 0;
}
}
}
if (tp1->sent <= SCTP_DATAGRAM_RESEND &&
tp1->sent != SCTP_DATAGRAM_UNSENT &&
compare_with_wrap(tp1->rec.data.TSN_seq,
asoc->this_sack_highest_gap,
MAX_TSN)) {
asoc->this_sack_highest_gap =
tp1->rec.data.TSN_seq;
if (primary_flag_set) {
tp1->whoTo->cacc_saw_newack = 1;
}
}
if (tp1->sent == SCTP_DATAGRAM_RESEND) {
#ifdef SCTP_DEBUG
if (sctp_debug_on &
SCTP_DEBUG_INDATA3) {
printf("Hmm. one that is in RESEND that is now ACKED\n");
}
#endif
sctp_ucount_decr(asoc->sent_queue_retran_cnt);
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xB2,
(asoc->sent_queue_retran_cnt & 0x000000ff));
#endif
}
(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
tp1->sent = SCTP_DATAGRAM_MARKED;
}
break;
}
if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
MAX_TSN))
break;
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
}
frag++;
}
#ifdef SCTP_FR_LOGGING
if (num_frs)
sctp_log_fr(*biggest_tsn_acked, *biggest_newly_acked_tsn,
last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
#endif
}
static void
sctp_check_for_revoked(struct sctp_association *asoc, u_long cum_ack,
u_long biggest_tsn_acked)
{
struct sctp_tmit_chunk *tp1;
int tot_revoked=0;
tp1 = TAILQ_FIRST(&asoc->sent_queue);
while (tp1) {
if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
MAX_TSN)) {
if (tp1->sent == SCTP_DATAGRAM_ACKED) {
tp1->sent = SCTP_DATAGRAM_SENT;
tot_revoked++;
} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
tp1->sent = SCTP_DATAGRAM_ACKED;
}
}
if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
MAX_TSN)) {
break;
}
if (tp1->sent == SCTP_DATAGRAM_UNSENT)
break;
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
if (tot_revoked > 0) {
tp1 = TAILQ_FIRST(&asoc->send_queue);
if (tp1 == NULL) {
asoc->nonce_resync_tsn = asoc->sending_seq;
} else {
asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
}
asoc->nonce_wait_for_ecne = 0;
asoc->nonce_sum_check = 0;
}
}
extern int sctp_peer_chunk_oh;
static void
sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
u_long biggest_tsn_acked, int strike_enabled,
u_long biggest_tsn_newly_acked, int accum_moved)
{
struct sctp_tmit_chunk *tp1;
int strike_flag=0;
struct timeval now;
int tot_retrans=0;
u_int32_t sending_seq;
int primary_switch_active = 0;
int double_switch_active = 0;
tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
if (tp1 == NULL) {
sending_seq = asoc->sending_seq;
} else {
sending_seq = tp1->rec.data.TSN_seq;
}
if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
primary_switch_active = 1;
}
if (asoc->primary_destination->dest_state & SCTP_ADDR_DOUBLE_SWITCH) {
double_switch_active = 1;
}
if (stcb->asoc.peer_supports_prsctp ) {
SCTP_GETTIME_TIMEVAL(&now);
}
tp1 = TAILQ_FIRST(&asoc->sent_queue);
while (tp1) {
strike_flag=0;
if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
MAX_TSN) ||
tp1->sent == SCTP_DATAGRAM_UNSENT) {
break;
}
if ((tp1->flags & (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) ==
SCTP_PR_SCTP_ENABLED &&
tp1->sent < SCTP_DATAGRAM_ACKED) {
#ifndef __FreeBSD__
if (timercmp(&now, &tp1->rec.data.timetodrop, >))
#else
if (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
#endif
{
if (tp1->data != NULL) {
sctp_release_pr_sctp_chunk(stcb, tp1,
(SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
&asoc->sent_queue);
}
tp1 = TAILQ_NEXT(tp1, sctp_next);
continue;
}
}
if (compare_with_wrap(tp1->rec.data.TSN_seq,
asoc->this_sack_highest_gap, MAX_TSN)) {
break;
}
if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
tp1 = TAILQ_NEXT(tp1, sctp_next);
continue;
}
if (primary_switch_active && (strike_enabled == 0)) {
if (tp1->whoTo != asoc->primary_destination) {
tp1 = TAILQ_NEXT(tp1, sctp_next);
continue;
}
} else if (primary_switch_active) {
if (tp1->whoTo->cacc_saw_newack == 0) {
tp1 = TAILQ_NEXT(tp1, sctp_next);
continue;
}
}
if (double_switch_active &&
(compare_with_wrap(asoc->primary_destination->next_tsn_at_change,
tp1->rec.data.TSN_seq, MAX_TSN))) {
tp1 = TAILQ_NEXT(tp1, sctp_next);
continue;
}
if (accum_moved && asoc->fast_retran_loss_recovery) {
tp1->sent++;
} else if (tp1->rec.data.doing_fast_retransmit) {
#ifdef SCTP_FR_TO_ALTERNATE
if (asoc->numnets < 2)
#else
if (1)
#endif
{
if ((compare_with_wrap(biggest_tsn_newly_acked,
tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
(biggest_tsn_newly_acked ==
tp1->rec.data.fast_retran_tsn)) {
#ifdef SCTP_FR_LOGGING
sctp_log_fr(biggest_tsn_newly_acked,
tp1->rec.data.TSN_seq,
tp1->rec.data.fast_retran_tsn,
SCTP_FR_LOG_STRIKE_CHUNK);
#endif
tp1->sent++;
strike_flag=1;
}
}
} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
biggest_tsn_newly_acked, MAX_TSN)) {
;
} else {
tp1->sent++;
}
if (tp1->sent == SCTP_DATAGRAM_RESEND) {
struct sctp_nets *alt;
#ifdef SCTP_FR_LOGGING
sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
0, SCTP_FR_MARKED);
#endif
if (strike_flag) {
sctp_pegs[SCTP_DUP_FR]++;
}
asoc->sent_queue_retran_cnt++;
#ifdef SCTP_FR_TO_ALTERNATE
alt = sctp_find_alternate_net(stcb, tp1->whoTo);
#else
alt = tp1->whoTo;
#endif
tp1->rec.data.doing_fast_retransmit = 1;
tot_retrans++;
if (TAILQ_EMPTY(&asoc->send_queue)) {
tp1->rec.data.fast_retran_tsn = sending_seq - 1;
} else {
struct sctp_tmit_chunk *ttt;
ttt = TAILQ_FIRST(&asoc->send_queue);
tp1->rec.data.fast_retran_tsn =
ttt->rec.data.TSN_seq - 1;
}
if (tp1->do_rtt) {
tp1->whoTo->rto_pending = 0;
tp1->do_rtt = 0;
}
tp1->whoTo->net_ack++;
sctp_flight_size_decrease(tp1);
#ifdef SCTP_LOG_RWND
sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
asoc->peers_rwnd , tp1->send_size, sctp_peer_chunk_oh);
#endif
asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
sctp_total_flight_decrease(stcb, tp1);
if (alt != tp1->whoTo) {
sctp_free_remote_addr(tp1->whoTo);
tp1->whoTo = alt;
alt->ref_count++;
}
}
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
if (tot_retrans > 0) {
asoc->nonce_resync_tsn = sending_seq;
asoc->nonce_wait_for_ecne = 0;
asoc->nonce_sum_check = 0;
}
}
struct sctp_tmit_chunk *
sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
struct sctp_association *asoc)
{
struct sctp_tmit_chunk *tp1, *tp2, *a_adv=NULL;
struct timeval now;
int now_filled=0;
if (asoc->peer_supports_prsctp == 0) {
return (NULL);
}
tp1 = TAILQ_FIRST(&asoc->sent_queue);
while (tp1) {
if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
tp1->sent != SCTP_DATAGRAM_RESEND) {
break;
}
if ((tp1->flags & SCTP_PR_SCTP_ENABLED) == 0) {
break;
}
if (!now_filled) {
SCTP_GETTIME_TIMEVAL(&now);
now_filled = 1;
}
tp2 = TAILQ_NEXT(tp1, sctp_next);
if (tp1->sent == SCTP_DATAGRAM_RESEND &&
(tp1->flags & SCTP_PR_SCTP_BUFFER) == 0) {
#ifndef __FreeBSD__
if (timercmp(&now, &tp1->rec.data.timetodrop, >))
#else
if (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
#endif
{
if (tp1->data) {
sctp_release_pr_sctp_chunk(stcb, tp1,
(SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
&asoc->sent_queue);
}
} else {
break;
}
}
if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
a_adv = tp1;
if (tp1->data) {
sctp_free_bufspace(stcb, asoc, tp1);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("--total out:%lu total_mbuf_out:%lu\n",
(u_long)asoc->total_output_queue_size,
(u_long)asoc->total_output_mbuf_queue_size);
}
#endif
sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
(SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
tp1);
sctp_m_freem(tp1->data);
tp1->data = NULL;
sctp_sowwakeup(stcb->sctp_ep,
stcb->sctp_socket);
}
} else {
break;
}
tp1 = tp2;
}
return (a_adv);
}
#ifdef SCTP_HIGH_SPEED
struct sctp_hs_raise_drop {
int32_t cwnd;
int32_t increase;
int32_t drop_percent;
};
#define SCTP_HS_TABLE_SIZE 73
struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
{38,1,50},
{118,2,44},
{221,3,41},
{347,4,38},
{495,5,37},
{663,6,35},
{851,7,34},
{1058,8,33},
{1284,9,32},
{1529,10,31},
{1793,11,30},
{2076,12,29},
{2378,13,28},
{2699,14,28},
{3039,15,27},
{3399,16,27},
{3778,17,26},
{4177,18,26},
{4596,19,25},
{5036,20,25},
{5497,21,24},
{5979,22,24},
{6483,23,23},
{7009,24,23},
{7558,25,22},
{8130,26,22},
{8726,27,22},
{9346,28,21},
{9991,29,21},
{10661,30,21},
{11358,31,20},
{12082,32,20},
{12834,33,20},
{13614,34,19},
{14424,35,19},
{15265,36,19},
{16137,37,19},
{17042,38,18},
{17981,39,18},
{18955,40,18},
{19965,41,17},
{21013,42,17},
{22101,43,17},
{23230,44,17},
{24402,45,16},
{25618,46,16},
{26881,47,16},
{28193,48,16},
{29557,49,15},
{30975,50,15},
{32450,51,15},
{33986,52,15},
{35586,53,14},
{37253,54,14},
{38992,55,14},
{40808,56,14},
{42707,57,13},
{44694,58,13},
{46776,59,13},
{48961,60,13},
{51258,61,13},
{53677,62,12},
{56230,63,12},
{58932,64,12},
{61799,65,12},
{64851,66,11},
{68113,67,11},
{71617,68,11},
{75401,69,10},
{79517,70,10},
{84035,71,10},
{89053,72,10},
{94717,73,9}
};
static void
sctp_hs_cwnd_increase(struct sctp_nets *net)
{
int cur_val, i, indx, incr;
cur_val = net->cwnd >> 10;
indx = SCTP_HS_TABLE_SIZE - 1;
if (cur_val < sctp_cwnd_adjust[0].cwnd) {
if (net->net_ack > net->mtu) {
net->cwnd += net->mtu;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, net->mtu, SCTP_CWND_LOG_FROM_SS);
#endif
} else {
net->cwnd += net->net_ack;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
#endif
}
} else {
for (i=net->last_hs_used; i<SCTP_HS_TABLE_SIZE; i++) {
if (cur_val < sctp_cwnd_adjust[i].cwnd) {
indx = i;
break;
}
}
net->last_hs_used = indx;
incr = ((sctp_cwnd_adjust[indx].increase) << 10);
net->cwnd += incr;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, incr, SCTP_CWND_LOG_FROM_SS);
#endif
}
}
static void
sctp_hs_cwnd_decrease(struct sctp_nets *net)
{
int cur_val, i, indx;
#ifdef SCTP_CWND_LOGGING
int old_cwnd = net->cwnd;
#endif
cur_val = net->cwnd >> 10;
indx = net->last_hs_used;
if (cur_val < sctp_cwnd_adjust[0].cwnd) {
net->ssthresh = net->cwnd / 2;
if (net->ssthresh < (net->mtu*2)) {
net->ssthresh = 2 * net->mtu;
}
net->cwnd = net->ssthresh;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_FR);
#endif
} else {
net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
sctp_cwnd_adjust[net->last_hs_used].drop_percent);
net->cwnd = net->ssthresh;
indx = net->last_hs_used;
cur_val = net->cwnd >> 10;
if (cur_val < sctp_cwnd_adjust[0].cwnd) {
net->last_hs_used = 0;
} else {
for (i = indx; i >= 1; i--) {
if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
break;
}
}
net->last_hs_used = indx;
}
}
}
#endif
void
sctp_handle_sack(struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
struct sctp_nets *net_from, int *abort_now)
{
struct sctp_association *asoc;
struct sctp_sack *sack;
struct sctp_tmit_chunk *tp1, *tp2;
u_long cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked;
uint16_t num_seg;
unsigned int sack_length;
uint32_t send_s;
int some_on_streamwheel;
int strike_enabled = 0, cnt_of_cacc = 0;
int accum_moved = 0;
int marking_allowed = 1;
int will_exit_fast_recovery=0;
u_int32_t a_rwnd;
struct sctp_nets *net = NULL;
int nonce_sum_flag, ecn_seg_sums=0;
asoc = &stcb->asoc;
asoc->overall_error_count = 0;
if (asoc->sent_queue_retran_cnt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Handling SACK for asoc:%p retran:%d\n",
asoc, asoc->sent_queue_retran_cnt);
}
#endif
}
sctp_service_queues(stcb, asoc, 0);
sack_length = ntohs(ch->ch.chunk_length);
if (sack_length < sizeof(struct sctp_sack_chunk)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Bad size on sack chunk .. to small\n");
}
#endif
return;
}
nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
sack = &ch->sack;
cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
num_seg = ntohs(sack->num_gap_ack_blks);
if (TAILQ_EMPTY(&asoc->send_queue)) {
send_s = asoc->sending_seq;
} else {
tp1 = TAILQ_FIRST(&asoc->send_queue);
send_s = tp1->rec.data.TSN_seq;
}
if (sctp_strict_sacks) {
if (cum_ack == send_s ||
compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
struct mbuf *oper;
hopeless_peer:
*abort_now = 1;
MGET(oper, M_DONTWAIT, MT_DATA);
if (oper) {
struct sctp_paramhdr *ph;
u_int32_t *ippp;
oper->m_len = sizeof(struct sctp_paramhdr) +
sizeof(*ippp);
ph = mtod(oper, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
ph->param_length = htons(oper->m_len);
ippp = (u_int32_t *)(ph + 1);
*ippp = htonl(0x30000002);
}
sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
return;
}
}
a_rwnd = (u_int32_t)ntohl(sack->a_rwnd);
if (asoc->sent_queue_retran_cnt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("cum_ack:%lx num_seg:%u last_acked_seq:%x\n",
cum_ack, (u_int)num_seg, asoc->last_acked_seq);
}
#endif
}
if (compare_with_wrap(asoc->t3timeout_highest_marked, cum_ack, MAX_TSN)) {
marking_allowed = 0;
}
if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
if (asoc->sent_queue_retran_cnt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("The cum-ack is behind us\n");
}
#endif
}
return;
}
if (TAILQ_EMPTY(&asoc->sent_queue)) {
#ifdef SCTP_LOG_RWND
sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
asoc->peers_rwnd, 0, 0, a_rwnd);
#endif
asoc->peers_rwnd = a_rwnd;
if (asoc->sent_queue_retran_cnt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Huh? retran set but none on queue\n");
}
#endif
asoc->sent_queue_retran_cnt = 0;
}
if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
asoc->peers_rwnd = 0;
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, net);
net->partial_bytes_acked = 0;
net->flight_size = 0;
}
asoc->total_flight = 0;
asoc->total_flight_count = 0;
return;
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
net->prev_cwnd = net->cwnd;
net->net_ack = 0;
net->net_ack2 = 0;
}
tp1 = TAILQ_FIRST(&asoc->sent_queue);
while (tp1) {
if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
MAX_TSN) ||
last_tsn == tp1->rec.data.TSN_seq) {
if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
accum_moved = 1;
if (tp1->sent < SCTP_DATAGRAM_ACKED) {
if ((tp1->whoTo->dest_state &
SCTP_ADDR_UNCONFIRMED) &&
(tp1->snd_count < 2) ) {
tp1->whoTo->dest_state &=
~SCTP_ADDR_UNCONFIRMED;
}
sctp_flight_size_decrease(tp1);
sctp_total_flight_decrease(stcb, tp1);
tp1->whoTo->net_ack += tp1->send_size;
if (tp1->snd_count < 2) {
tp1->whoTo->net_ack2 +=
tp1->send_size;
if (tp1->do_rtt) {
tp1->whoTo->RTO =
sctp_calculate_rto(stcb,
asoc, tp1->whoTo,
&tp1->sent_rcv_time);
tp1->whoTo->rto_pending = 0;
tp1->do_rtt = 0;
}
}
}
if (tp1->sent == SCTP_DATAGRAM_RESEND) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA3) {
printf("Hmm. one that is in RESEND that is now ACKED\n");
}
#endif
sctp_ucount_decr(asoc->sent_queue_retran_cnt);
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xB3,
(asoc->sent_queue_retran_cnt & 0x000000ff));
#endif
}
tp1->sent = SCTP_DATAGRAM_ACKED;
}
} else {
break;
}
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
if (accum_moved) {
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, net);
}
}
biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
asoc->this_sack_highest_gap = last_tsn;
if (num_seg * sizeof(struct sctp_gap_ack_block) + sizeof(struct sctp_sack_chunk) > sack_length) {
strike_enabled = 0;
goto skip_segments;
}
if (num_seg > 0) {
if (asoc->primary_destination->dest_state &
SCTP_ADDR_SWITCH_PRIMARY) {
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
net->cacc_saw_newack = 0;
}
}
sctp_handle_segments(stcb, asoc, ch, last_tsn,
&biggest_tsn_acked, &biggest_tsn_newly_acked,
num_seg, &ecn_seg_sums);
if (sctp_strict_sacks) {
if ((biggest_tsn_acked == send_s) ||
(compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
goto hopeless_peer;
}
}
if (asoc->primary_destination->dest_state &
SCTP_ADDR_SWITCH_PRIMARY) {
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
if (net->cacc_saw_newack) {
cnt_of_cacc++;
}
}
}
}
if (cnt_of_cacc < 2) {
strike_enabled = 1;
} else {
strike_enabled = 0;
}
skip_segments:
asoc->last_acked_seq = cum_ack;
if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
if ((cum_ack == asoc->primary_destination->next_tsn_at_change) ||
(compare_with_wrap(cum_ack,
asoc->primary_destination->next_tsn_at_change, MAX_TSN))) {
struct sctp_nets *lnet;
TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
asoc->primary_destination->dest_state &=
~(SCTP_ADDR_SWITCH_PRIMARY|SCTP_ADDR_DOUBLE_SWITCH);
}
}
}
if (marking_allowed) {
asoc->t3timeout_highest_marked = cum_ack;
}
tp1 = TAILQ_FIRST(&asoc->sent_queue);
do {
if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
MAX_TSN)) {
break;
}
if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
break;
}
tp2 = TAILQ_NEXT(tp1, sctp_next);
TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
if (tp1->data) {
sctp_free_bufspace(stcb, asoc, tp1);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
printf("--total out:%lu total_mbuf_out:%lu\n",
(u_long)asoc->total_output_queue_size,
(u_long)asoc->total_output_mbuf_queue_size);
}
#endif
sctp_m_freem(tp1->data);
if (tp1->flags & SCTP_PR_SCTP_BUFFER) {
asoc->sent_queue_cnt_removeable--;
}
}
tp1->data = NULL;
asoc->sent_queue_cnt--;
sctp_free_remote_addr(tp1->whoTo);
sctppcbinfo.ipi_count_chunk--;
asoc->chunks_on_out_queue--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is going negative");
}
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, tp1);
sctppcbinfo.ipi_gencnt_chunk++;
sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
tp1 = tp2;
} while (tp1 != NULL);
if (asoc->fast_retran_loss_recovery && accum_moved) {
if (compare_with_wrap(asoc->last_acked_seq,
asoc->fast_recovery_tsn, MAX_TSN) ||
asoc->last_acked_seq == asoc->fast_recovery_tsn) {
will_exit_fast_recovery = 1;
}
}
if (asoc->saw_sack_with_frags)
sctp_check_for_revoked(asoc, cum_ack, biggest_tsn_acked);
if (num_seg)
asoc->saw_sack_with_frags = 1;
else
asoc->saw_sack_with_frags = 0;
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
if (net->net_ack == 0)
continue;
if (net->net_ack2 > 0) {
net->error_count = 0;
if ((net->dest_state&SCTP_ADDR_NOT_REACHABLE) ==
SCTP_ADDR_NOT_REACHABLE) {
net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
net->dest_state |= SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
SCTP_RECEIVED_SACK, (void *)net);
if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
}
}
}
if (asoc->fast_retran_loss_recovery &&
will_exit_fast_recovery == 0) {
sctp_pegs[SCTP_CWND_SKIP]++;
goto skip_cwnd_update;
}
if (accum_moved) {
if (net->cwnd <= net->ssthresh) {
if (net->flight_size + net->net_ack >=
net->cwnd ) {
#ifdef SCTP_HIGH_SPEED
sctp_hs_cwnd_increase(net);
#else
if (net->net_ack > net->mtu) {
net->cwnd += net->mtu;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, net->mtu,
SCTP_CWND_LOG_FROM_SS);
#endif
} else {
net->cwnd += net->net_ack;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, net->net_ack,
SCTP_CWND_LOG_FROM_SS);
#endif
}
#endif
sctp_pegs[SCTP_CWND_SS]++;
} else {
unsigned int dif;
sctp_pegs[SCTP_CWND_NOUSE_SS]++;
dif = net->cwnd - (net->flight_size +
net->net_ack);
#ifdef SCTP_CWND_LOGGING
#endif
if (dif > sctp_pegs[SCTP_CWND_DIFF_SA]) {
sctp_pegs[SCTP_CWND_DIFF_SA] =
dif;
sctp_pegs[SCTP_OQS_AT_SS] =
asoc->total_output_queue_size;
sctp_pegs[SCTP_SQQ_AT_SS] =
asoc->sent_queue_cnt;
sctp_pegs[SCTP_SQC_AT_SS] =
asoc->send_queue_cnt;
}
}
} else {
if (net->flight_size + net->net_ack >=
net->cwnd) {
net->partial_bytes_acked +=
net->net_ack;
if (net->partial_bytes_acked >=
net->cwnd) {
if (net->cwnd <
net->partial_bytes_acked) {
net->partial_bytes_acked -=
net->cwnd;
} else {
net->partial_bytes_acked =
0;
}
net->cwnd += net->mtu;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, net->mtu,
SCTP_CWND_LOG_FROM_CA);
#endif
sctp_pegs[SCTP_CWND_CA]++;
}
} else {
unsigned int dif;
sctp_pegs[SCTP_CWND_NOUSE_CA]++;
#ifdef SCTP_CWND_LOGGING
#endif
dif = net->cwnd - (net->flight_size +
net->net_ack);
if (dif > sctp_pegs[SCTP_CWND_DIFF_CA]) {
sctp_pegs[SCTP_CWND_DIFF_CA] =
dif;
sctp_pegs[SCTP_OQS_AT_CA] =
asoc->total_output_queue_size;
sctp_pegs[SCTP_SQQ_AT_CA] =
asoc->sent_queue_cnt;
sctp_pegs[SCTP_SQC_AT_CA] =
asoc->send_queue_cnt;
}
}
}
} else {
sctp_pegs[SCTP_CWND_NOCUM]++;
}
skip_cwnd_update:
if (net->net_ack2) {
net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
if (net->RTO < stcb->asoc.minrto) {
net->RTO = stcb->asoc.minrto;
}
if (net->RTO > stcb->asoc.maxrto) {
net->RTO = stcb->asoc.maxrto;
}
}
if (net->cwnd > sctp_pegs[SCTP_MAX_CWND]) {
sctp_pegs[SCTP_MAX_CWND] = net->cwnd;
}
}
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) {
#ifdef SCTP_LOG_RWND
sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
asoc->peers_rwnd, 0, 0, a_rwnd);
#endif
asoc->peers_rwnd = a_rwnd;
if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
asoc->peers_rwnd = 0;
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, net);
net->flight_size = 0;
net->partial_bytes_acked = 0;
}
asoc->total_flight = 0;
asoc->total_flight_count = 0;
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_SHUTDOWN_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);
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 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) {
asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
sctp_send_shutdown_ack(stcb,
stcb->asoc.primary_destination);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
stcb->sctp_ep, stcb, asoc->primary_destination);
}
return;
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
net->net_ack = 0;
}
if ((num_seg > 0) && marking_allowed) {
sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
strike_enabled, biggest_tsn_newly_acked, accum_moved);
}
if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
asoc->advanced_peer_ack_point = cum_ack;
}
if (asoc->peer_supports_prsctp) {
struct sctp_tmit_chunk *lchk;
lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
MAX_TSN)) {
send_forward_tsn(stcb, asoc);
asoc->nonce_sum_check = 0;
asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
if (lchk) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND,
stcb->sctp_ep, stcb, lchk->whoTo);
}
}
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
if (asoc->fast_retran_loss_recovery == 0) {
if (net->net_ack > 0) {
struct sctp_tmit_chunk *lchk;
#ifdef SCTP_HIGH_SPEED
sctp_hs_cwnd_decrease(net);
#else
#ifdef SCTP_CWND_LOGGING
int old_cwnd = net->cwnd;
#endif
net->ssthresh = net->cwnd / 2;
if (net->ssthresh < (net->mtu*2)) {
net->ssthresh = 2 * net->mtu;
}
net->cwnd = net->ssthresh;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, (net->cwnd-old_cwnd),
SCTP_CWND_LOG_FROM_FR);
#endif
#endif
lchk = TAILQ_FIRST(&asoc->send_queue);
net->partial_bytes_acked = 0;
asoc->fast_retran_loss_recovery = 1;
if (lchk == NULL) {
asoc->fast_recovery_tsn = asoc->sending_seq - 1;
} else {
asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
}
asoc->nonce_sum_check = 0;
asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
stcb->sctp_ep, stcb, net);
sctp_timer_start(SCTP_TIMER_TYPE_SEND,
stcb->sctp_ep, stcb, net);
}
} else if (net->net_ack > 0) {
sctp_pegs[SCTP_FR_INAWINDOW]++;
}
}
if (asoc->ecn_nonce_allowed) {
if (asoc->nonce_sum_check) {
if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
if (asoc->nonce_wait_for_ecne == 0) {
struct sctp_tmit_chunk *lchk;
lchk = TAILQ_FIRST(&asoc->send_queue);
asoc->nonce_wait_for_ecne = 1;
if (lchk) {
asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
} else {
asoc->nonce_wait_tsn = asoc->sending_seq;
}
} else {
if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
(asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
printf("Mis-behaving peer detected\n");
asoc->ecn_allowed = 0;
asoc->ecn_nonce_allowed = 0;
}
}
}
} else {
if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
asoc->nonce_sum_check = 1;
asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
}
}
}
if (will_exit_fast_recovery) {
asoc->fast_retran_loss_recovery = 0;
}
if ((asoc->sat_t3_loss_recovery) &&
((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
MAX_TSN) ||
(asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
asoc->sat_t3_loss_recovery = 0;
}
#ifdef SCTP_LOG_RWND
sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
#endif
asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
(u_int32_t)(asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
asoc->peers_rwnd = 0;
}
TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
struct sctp_tmit_chunk *chk;
TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
if (chk->whoTo == net &&
(chk->sent < SCTP_DATAGRAM_ACKED ||
chk->sent == SCTP_FORWARD_TSN_SKIP)) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND,
stcb->sctp_ep, stcb, net);
break;
}
}
}
}
void
sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
struct sctp_nets *netp, int *abort_flag)
{
struct sctp_sack_chunk sack;
sack.sack.cum_tsn_ack = cp->cumulative_tsn_ack;
sack.ch.chunk_type = SCTP_SELECTIVE_ACK;
sack.ch.chunk_flags = 0;
sack.ch.chunk_length = ntohs(sizeof(struct sctp_sack_chunk));
sack.sack.a_rwnd =
htonl(stcb->asoc.peers_rwnd + stcb->asoc.total_flight);
sack.sack.num_gap_ack_blks = 0;
sack.sack.num_dup_tsns = 0;
sctp_handle_sack(&sack, stcb, netp, abort_flag);
}
static void
sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
struct sctp_stream_in *strmin)
{
struct sctp_tmit_chunk *chk, *nchk;
struct sctp_association *asoc;
int tt;
asoc = &stcb->asoc;
tt = strmin->last_sequence_delivered;
chk = TAILQ_FIRST(&strmin->inqueue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (compare_with_wrap(tt, chk->rec.data.stream_seq, MAX_SEQ) ||
(tt == chk->rec.data.stream_seq)) {
TAILQ_REMOVE(&strmin->inqueue, chk, sctp_next);
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
sctp_deliver_data(stcb, &stcb->asoc, chk, 0);
} else {
break;
}
chk = nchk;
}
tt = strmin->last_sequence_delivered + 1;
chk = TAILQ_FIRST(&strmin->inqueue);
while (chk) {
nchk = TAILQ_NEXT(chk, sctp_next);
if (tt == chk->rec.data.stream_seq) {
TAILQ_REMOVE(&strmin->inqueue, chk, sctp_next);
asoc->size_on_all_streams -= chk->send_size;
asoc->cnt_on_all_streams--;
strmin->last_sequence_delivered =
chk->rec.data.stream_seq;
sctp_deliver_data(stcb, &stcb->asoc, chk, 0);
tt = strmin->last_sequence_delivered + 1;
} else {
break;
}
chk = nchk;
}
}
void
sctp_handle_forward_tsn(struct sctp_tcb *stcb,
struct sctp_forward_tsn_chunk *fwd, int *abort_flag)
{
struct sctp_strseq *stseq;
struct sctp_association *asoc;
u_int32_t new_cum_tsn, gap, back_out_htsn;
unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
struct sctp_stream_in *strm;
struct sctp_tmit_chunk *chk, *at;
cumack_set_flag = 0;
asoc = &stcb->asoc;
cnt_gone = 0;
if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Bad size too small/big fwd-tsn\n");
}
#endif
return;
}
m_size = (stcb->asoc.mapping_array_size << 3);
new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
asoc->cumulative_tsn == new_cum_tsn) {
return;
}
back_out_htsn = asoc->highest_tsn_inside_map;
if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
MAX_TSN)) {
asoc->highest_tsn_inside_map = new_cum_tsn;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
}
if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
MAX_TSN)) ||
(new_cum_tsn == asoc->mapping_array_base_tsn)) {
gap = new_cum_tsn - asoc->mapping_array_base_tsn;
} else {
gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
}
if (gap >= m_size) {
asoc->highest_tsn_inside_map = back_out_htsn;
if ((long)gap > sctp_sbspace(&stcb->sctp_socket->so_rcv)) {
return;
}
if (asoc->highest_tsn_inside_map >
asoc->mapping_array_base_tsn) {
gap = asoc->highest_tsn_inside_map -
asoc->mapping_array_base_tsn;
} else {
gap = asoc->highest_tsn_inside_map +
(MAX_TSN - asoc->mapping_array_base_tsn) + 1;
}
cumack_set_flag = 1;
}
for (i = 0; i <= gap; i++) {
SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
}
sctp_sack_check(stcb, 0, 0, abort_flag);
if (*abort_flag)
return;
if (cumack_set_flag) {
asoc->highest_tsn_inside_map = new_cum_tsn - 1;
asoc->mapping_array_base_tsn = new_cum_tsn;
asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
}
if (asoc->fragmented_delivery_inprogress) {
sctp_service_reassembly(stcb, asoc, 0);
}
if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
chk = TAILQ_FIRST(&asoc->reasmqueue);
while (chk) {
at = TAILQ_NEXT(chk, sctp_next);
if (compare_with_wrap(asoc->cumulative_tsn,
chk->rec.data.TSN_seq, MAX_TSN) ||
asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
if (compare_with_wrap(chk->rec.data.TSN_seq,
asoc->tsn_last_delivered, MAX_TSN)) {
asoc->tsn_last_delivered =
chk->rec.data.TSN_seq;
asoc->str_of_pdapi =
chk->rec.data.stream_number;
asoc->ssn_of_pdapi =
chk->rec.data.stream_seq;
asoc->fragment_flags =
chk->rec.data.rcv_flags;
}
asoc->size_on_reasm_queue -= chk->send_size;
asoc->cnt_on_reasm_queue--;
cnt_gone++;
if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
SCTP_DATA_UNORDERED &&
(compare_with_wrap(chk->rec.data.stream_seq,
asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
MAX_SEQ))) {
asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
chk->rec.data.stream_seq;
}
sctp_m_freem(chk->data);
chk->data = NULL;
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 ((asoc->fragmented_delivery_inprogress) &&
(chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
asoc->fragmented_delivery_inprogress = 0;
sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)NULL);
}
break;
}
chk = at;
}
}
if (asoc->fragmented_delivery_inprogress) {
sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)NULL);
asoc->fragmented_delivery_inprogress = 0;
}
stseq = (struct sctp_strseq *)((vaddr_t)fwd + sizeof(*fwd));
fwd_sz -= sizeof(*fwd);
{
int num_str;
num_str = fwd_sz/sizeof(struct sctp_strseq);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Using NEW method, %d strseq's reported in FWD-TSN\n",
num_str);
}
#endif
for (i = 0; i < num_str; i++) {
u_int16_t st;
#if 0
unsigned char *xx;
xx = (unsigned char *)&stseq[i];
#endif
st = ntohs(stseq[i].stream);
stseq[i].stream = st;
st = ntohs(stseq[i].sequence);
stseq[i].sequence = st;
if (stseq[i].stream > asoc->streamincnt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
printf("Bogus stream number %d "
"streamincnt is %d\n",
stseq[i].stream, asoc->streamincnt);
}
#endif
continue;
}
strm = &asoc->strmin[stseq[i].stream];
if (compare_with_wrap(stseq[i].sequence,
strm->last_sequence_delivered, MAX_SEQ)) {
strm->last_sequence_delivered =
stseq[i].sequence;
}
sctp_kick_prsctp_reorder_queue(stcb, strm);
}
}
}