#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sctp_input.c,v 1.19 2025/07/11 22:19:54 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/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <machine/limits.h>
#include <machine/cpu.h>
#include <net/if.h>
#include <net/route.h>
#include <net/if_types.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>
#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_asconf.h>
#ifdef IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#endif
#ifdef SCTP_DEBUG
extern u_int32_t sctp_debug_on;
#endif
static void
sctp_handle_init(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_init *init;
struct mbuf *op_err;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
}
#endif
op_err = NULL;
init = &cp->init;
if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
(inp->sctp_socket->so_qlimit == 0)) {
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
if (init->initiate_tag == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
if (init->num_inbound_streams == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
if (init->num_outbound_streams == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
return;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_handle_init: sending INIT-ACK\n");
}
#endif
sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
}
static int
sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct sctp_init *init;
struct sctp_association *asoc;
struct sctp_nets *lnet;
unsigned int i;
init = &cp->init;
asoc = &stcb->asoc;
asoc->peer_vtag = ntohl(init->initiate_tag);
asoc->peers_rwnd = ntohl(init->a_rwnd);
if (TAILQ_FIRST(&asoc->nets)) {
TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
lnet->ssthresh = asoc->peers_rwnd;
}
}
if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
unsigned int newcnt;
struct sctp_stream_out *outs;
struct sctp_tmit_chunk *chk;
newcnt = ntohs(init->num_inbound_streams);
if (asoc->strmout) {
for (i=newcnt; i < asoc->pre_open_streams; i++) {
outs = &asoc->strmout[i];
chk = TAILQ_FIRST(&outs->outqueue);
while (chk) {
TAILQ_REMOVE(&outs->outqueue, chk,
sctp_next);
asoc->stream_queue_cnt--;
sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
chk);
sctp_m_freem(chk->data);
chk->data = NULL;
sctp_free_remote_addr(chk->whoTo);
chk->whoTo = NULL;
chk->asoc = NULL;
SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
sctppcbinfo.ipi_count_chunk--;
if ((int)sctppcbinfo.ipi_count_chunk < 0) {
panic("Chunk count is negative");
}
sctppcbinfo.ipi_gencnt_chunk++;
chk = TAILQ_FIRST(&outs->outqueue);
}
}
}
asoc->pre_open_streams = newcnt;
}
asoc->streamincnt = ntohs(init->num_outbound_streams);
if (asoc->streamincnt > MAX_SCTP_STREAMS) {
asoc->streamincnt = MAX_SCTP_STREAMS;
}
asoc->streamoutcnt = asoc->pre_open_streams;
asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
#ifdef SCTP_MAP_LOGGING
sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
#endif
asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
asoc->cumulative_tsn = asoc->asconf_seq_in;
asoc->last_echo_tsn = asoc->asconf_seq_in;
asoc->advanced_peer_ack_point = asoc->last_acked_seq;
if (asoc->strmin != NULL) {
free(asoc->strmin, M_PCB);
}
asoc->strmin = malloc(asoc->streamincnt * sizeof(struct sctp_stream_in),
M_PCB, M_NOWAIT);
if (asoc->strmin == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("process_init: couldn't get memory for the streams!\n");
}
#endif
return (-1);
}
for (i = 0; i < asoc->streamincnt; i++) {
asoc->strmin[i].stream_no = i;
asoc->strmin[i].last_sequence_delivered = 0xffff;
TAILQ_INIT(&asoc->strmin[i].inqueue);
asoc->strmin[i].next_spoke.tqe_next = 0;
asoc->strmin[i].next_spoke.tqe_prev = 0;
}
return (0);
}
static int
sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct sctp_association *asoc;
struct mbuf *op_err;
int retval, abort_flag;
uint32_t initack_limit;
abort_flag = 0;
op_err = NULL;
op_err = sctp_arethere_unrecognized_parameters(m,
(offset+sizeof(struct sctp_init_chunk)) ,
&abort_flag, (struct sctp_chunkhdr *)cp);
if (abort_flag) {
if (op_err != NULL) {
sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
} else {
sctp_abort_notification(stcb, 0);
sctp_free_assoc(stcb->sctp_ep, stcb);
}
return (-1);
}
asoc = &stcb->asoc;
retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
if (retval < 0) {
return (retval);
}
initack_limit = offset + ntohs(cp->ch.chunk_length);
if (sctp_load_addresses_from_init(stcb, m, iphlen,
(offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
NULL)) {
sctp_abort_notification(stcb, 0);
sctp_free_assoc(stcb->sctp_ep, stcb);
return (-1);
}
if (op_err) {
sctp_queue_op_err(stcb, op_err);
op_err = NULL;
}
stcb->asoc.overall_error_count = 0;
net->error_count = 0;
retval = sctp_send_cookie_echo(m, offset, stcb, net);
if (retval < 0) {
if (retval == -3) {
op_err =
sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
if (op_err) {
struct sctp_inv_mandatory_param *mp;
op_err->m_len =
sizeof(struct sctp_inv_mandatory_param);
mp = mtod(op_err,
struct sctp_inv_mandatory_param *);
mp->length =
htons(sizeof(struct sctp_inv_mandatory_param) - 2);
mp->num_param = htonl(1);
mp->param = htons(SCTP_STATE_COOKIE);
mp->resv = 0;
}
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
sh, op_err);
}
return (retval);
}
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
asoc->primary_destination);
net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
return (0);
}
static void
sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sockaddr_storage store;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct sctp_nets *r_net;
struct timeval tv;
if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
return;
}
sin = (struct sockaddr_in *)&store;
sin6 = (struct sockaddr_in6 *)&store;
memset(&store, 0, sizeof(store));
if (cp->heartbeat.hb_info.addr_family == AF_INET &&
cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
sin->sin_family = cp->heartbeat.hb_info.addr_family;
sin->sin_len = cp->heartbeat.hb_info.addr_len;
sin->sin_port = stcb->rport;
memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
sizeof(sin->sin_addr));
} else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
sin6->sin6_port = stcb->rport;
memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
sizeof(sin6->sin6_addr));
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("unsupported address family");
}
#endif
return;
}
r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
if (r_net == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Huh? I can't find the address I sent it to, discard\n");
}
#endif
return;
}
if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
(r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
(r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
stcb, 0, (void *)r_net);
}
r_net->error_count = 0;
r_net->hb_responded = 1;
tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
r_net->dest_state = SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
}
}
r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
}
static void
sctp_handle_abort(struct sctp_abort_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_abort: handling ABORT\n");
}
#endif
if (stcb == NULL)
return;
sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
sctp_abort_notification(stcb, 0);
sctp_free_assoc(stcb->sctp_ep, stcb);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_abort: finished\n");
}
#endif
}
static void
sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
{
struct sctp_association *asoc;
int some_on_streamwheel;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_shutdown: handling SHUTDOWN\n");
}
#endif
if (stcb == NULL)
return;
if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
(SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
return;
}
if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
ntohs(cp->ch.chunk_length),
(int)sizeof(struct sctp_shutdown_chunk));
}
#endif
return;
} else {
sctp_update_acked(stcb, cp, net, abort_flag);
}
asoc = &stcb->asoc;
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Moving to SHUTDOWN-RECEIVED state\n");
}
#endif
sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
#if defined(__FreeBSD__) && __FreeBSD_version >= 502115
stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
#else
stcb->sctp_ep->sctp_socket->so_state |= SS_CANTSENDMORE;
#endif
}
SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
}
if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
}
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;
}
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
some_on_streamwheel,
!TAILQ_EMPTY(&asoc->send_queue),
!TAILQ_EMPTY(&asoc->sent_queue));
}
#endif
if (!TAILQ_EMPTY(&asoc->send_queue) ||
!TAILQ_EMPTY(&asoc->sent_queue) ||
some_on_streamwheel) {
return;
} else {
sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("moving to SHUTDOWN_ACK state\n");
}
#endif
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
stcb, net);
}
}
static void
sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_association *asoc;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
}
#endif
if (stcb == NULL)
return;
asoc = &stcb->asoc;
if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
(SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
return;
}
if (!TAILQ_EMPTY(&asoc->send_queue) ||
!TAILQ_EMPTY(&asoc->sent_queue) ||
!TAILQ_EMPTY(&asoc->out_wheel)) {
sctp_report_all_outbound(stcb);
}
sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
sctp_send_shutdown_complete(stcb, net);
sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
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;
stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
soisdisconnected(stcb->sctp_ep->sctp_socket);
}
sctp_free_assoc(stcb->sctp_ep, stcb);
}
static void
sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
struct sctp_nets *net)
{
struct sctp_chunkhdr *chk;
chk = (struct sctp_chunkhdr *)((vaddr_t)phdr + sizeof(*phdr));
switch (chk->chunk_type) {
case SCTP_ASCONF_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Strange peer, snds ASCONF but does not recongnize asconf-ack?\n");
}
#endif
case SCTP_ASCONF:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
}
#endif
sctp_asconf_cleanup(stcb, net);
break;
case SCTP_FORWARD_CUM_TSN:
stcb->asoc.peer_supports_prsctp = 0;
break;
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Peer does not support chunk type %d(%x)??\n",
chk->chunk_type, (u_int)chk->chunk_type);
}
#endif
break;
}
}
static void
sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
{
struct sctp_paramhdr *pbad;
pbad = phdr + 1;
switch (ntohs(pbad->param_type)) {
case SCTP_PRSCTP_SUPPORTED:
stcb->asoc.peer_supports_prsctp = 0;
break;
case SCTP_SUPPORTED_CHUNK_EXT:
break;
case SCTP_ECN_NONCE_SUPPORTED:
stcb->asoc.peer_supports_ecn_nonce = 0;
stcb->asoc.ecn_nonce_allowed = 0;
stcb->asoc.ecn_allowed = 0;
break;
case SCTP_ADD_IP_ADDRESS:
case SCTP_DEL_IP_ADDRESS:
stcb->asoc.peer_supports_asconf = 0;
break;
case SCTP_SET_PRIM_ADDR:
stcb->asoc.peer_supports_asconf_setprim = 0;
break;
case SCTP_SUCCESS_REPORT:
case SCTP_ERROR_CAUSE_IND:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Huh, the peer does not support success? or error cause?\n");
printf("Turning off ASCONF to this strange peer\n");
}
#endif
stcb->asoc.peer_supports_asconf = 0;
stcb->asoc.peer_supports_asconf_setprim = 0;
break;
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Peer does not support base param type %d(%x)??\n",
pbad->param_type, (u_int)pbad->param_type);
}
#endif
break;
}
}
static int
sctp_handle_error(struct sctp_chunkhdr *ch,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
int chklen;
struct sctp_paramhdr *phdr;
uint16_t error_type;
uint16_t error_len;
struct sctp_association *asoc;
int adjust;
asoc = &stcb->asoc;
phdr = (struct sctp_paramhdr *)((vaddr_t)ch +
sizeof(struct sctp_chunkhdr));
chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
error_type = ntohs(phdr->param_type);
error_len = ntohs(phdr->param_length);
if ((error_len > chklen) || (error_len == 0)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Bogus length in error param- chunk left:%d errorlen:%d\n",
chklen, error_len);
}
#endif
return (0);
}
switch (error_type) {
case SCTP_CAUSE_INV_STRM:
case SCTP_CAUSE_MISS_PARAM:
case SCTP_CAUSE_INVALID_PARAM:
case SCTP_CAUSE_NOUSER_DATA:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
error_type);
}
#endif
break;
case SCTP_CAUSE_STALE_COOKIE:
if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
int *p;
p = (int *)((vaddr_t)phdr + sizeof(*phdr));
asoc->cookie_preserve_req = ntohl(*p) << 1;
asoc->stale_cookie_count++;
if (asoc->stale_cookie_count >
asoc->max_init_times) {
sctp_abort_notification(stcb, 0);
sctp_free_assoc(stcb->sctp_ep, stcb);
return (-1);
}
asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
asoc->state |= SCTP_STATE_COOKIE_WAIT;
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
stcb->sctp_ep, stcb, net);
sctp_send_initiate(stcb->sctp_ep, stcb);
}
break;
case SCTP_CAUSE_UNRESOLV_ADDR:
break;
case SCTP_CAUSE_UNRECOG_CHUNK:
sctp_process_unrecog_chunk(stcb, phdr, net);
break;
case SCTP_CAUSE_UNRECOG_PARAM:
sctp_process_unrecog_param(stcb, phdr);
break;
case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
break;
case SCTP_CAUSE_DELETEING_LAST_ADDR:
case SCTP_CAUSE_OPERATION_REFUSED:
case SCTP_CAUSE_DELETING_SRC_ADDR:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
error_type);
}
#endif
break;
case SCTP_CAUSE_OUT_OF_RESC:
break;
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("sctp_handle_error: unknown error type = 0x%xh\n",
error_type);
}
#endif
break;
}
adjust = SCTP_SIZE32(error_len);
chklen -= adjust;
phdr = (struct sctp_paramhdr *)((vaddr_t)phdr + adjust);
}
return (0);
}
static int
sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
struct sctp_nets *net)
{
struct sctp_init_ack *init_ack;
int *state;
struct mbuf *op_err;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_init_ack: handling INIT-ACK\n");
}
#endif
if (stcb == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_init_ack: TCB is null\n");
}
#endif
return (-1);
}
if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
op_err);
return (-1);
}
init_ack = &cp->init;
if (init_ack->initiate_tag == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
op_err);
return (-1);
}
if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
op_err);
return (-1);
}
if (init_ack->num_inbound_streams == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
op_err);
return (-1);
}
if (init_ack->num_outbound_streams == 0) {
op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
op_err);
return (-1);
}
state = &stcb->asoc.state;
switch (*state & SCTP_STATE_MASK) {
case SCTP_STATE_COOKIE_WAIT:
if (stcb->asoc.primary_destination->dest_state &
SCTP_ADDR_UNCONFIRMED) {
stcb->asoc.primary_destination->dest_state &=
~SCTP_ADDR_UNCONFIRMED;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
stcb, 0, (void *)stcb->asoc.primary_destination);
}
if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
) < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_process_init_ack: error in msg, discarding\n");
}
#endif
return (-1);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("moving to COOKIE-ECHOED state\n");
}
#endif
if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
*state = SCTP_STATE_COOKIE_ECHOED |
SCTP_STATE_SHUTDOWN_PENDING;
} else {
*state = SCTP_STATE_COOKIE_ECHOED;
}
stcb->asoc.overall_error_count = 0;
SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
stcb, net);
break;
case SCTP_STATE_SHUTDOWN_SENT:
break;
case SCTP_STATE_COOKIE_ECHOED:
break;
case SCTP_STATE_OPEN:
break;
case SCTP_STATE_EMPTY:
case SCTP_STATE_INUSE:
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Leaving handle-init-ack default\n");
}
#endif
return (-1);
break;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Leaving handle-init-ack end\n");
}
#endif
return (0);
}
static struct sctp_tcb *
sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
struct sockaddr *init_src, int *notification)
{
struct sctp_association *asoc;
struct sctp_init_chunk *init_cp, init_buf;
struct sctp_init_ack_chunk *initack_cp, initack_buf;
int chk_length;
int init_offset, initack_offset;
int retval;
asoc = &stcb->asoc;
if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
struct mbuf *op_err;
struct sctp_paramhdr *ph;
sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: got a cookie, while shutting down!\n");
}
#endif
MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
if (op_err == NULL) {
return (NULL);
}
op_err->m_data += sizeof(struct ip6_hdr);
op_err->m_data += sizeof(struct sctphdr);
op_err->m_data += sizeof(struct sctp_chunkhdr);
op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
ph = mtod(op_err, struct sctp_paramhdr *);
ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
ph->param_length = htons(sizeof(struct sctp_paramhdr));
sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
return (NULL);
}
init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
init_cp = (struct sctp_init_chunk *)
sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
(u_int8_t *)&init_buf);
if (init_cp == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("process_cookie_existing: could not pull INIT chunk hdr\n");
}
#endif
return (NULL);
}
chk_length = ntohs(init_cp->ch.chunk_length);
if (init_cp->ch.chunk_type != SCTP_INITIATION) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("process_cookie_existing: could not find INIT chunk!\n");
}
#endif
return (NULL);
}
initack_offset = init_offset + SCTP_SIZE32(chk_length);
initack_cp = (struct sctp_init_ack_chunk *)
sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
(u_int8_t *)&initack_buf);
if (initack_cp == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
}
#endif
return (NULL);
}
chk_length = ntohs(initack_cp->ch.chunk_length);
if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("process_cookie_existing: could not find INIT-ACK chunk!\n");
}
#endif
return (NULL);
}
if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
(ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
switch SCTP_GET_STATE(asoc) {
case SCTP_STATE_COOKIE_WAIT:
retval = sctp_process_init(init_cp, stcb, net);
if (retval < 0) {
#ifdef SCTP_DEBUG
printf("process_cookie_existing: INIT processing failed\n");
#endif
return (NULL);
}
case SCTP_STATE_COOKIE_ECHOED:
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
net);
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
net);
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_OPEN |
SCTP_STATE_SHUTDOWN_PENDING;
} else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
asoc->state = SCTP_STATE_OPEN;
}
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_ACCEPTING))) {
stcb->sctp_ep->sctp_flags |=
SCTP_PCB_FLAGS_CONNECTED;
soisconnected(stcb->sctp_ep->sctp_socket);
}
*notification = SCTP_NOTIFY_ASSOC_UP;
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
net);
net->hb_responded = 1;
if (stcb->asoc.sctp_autoclose_ticks &&
(inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
inp, stcb, NULL);
}
break;
default:
break;
}
if (sctp_load_addresses_from_init(stcb, m, iphlen,
init_offset + sizeof(struct sctp_init_chunk),
initack_offset, sh, init_src)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Weird cookie load_address failure on cookie existing - 1\n");
}
#endif
return (NULL);
}
sctp_send_cookie_ack(stcb);
return (stcb);
}
if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
cookie->tie_tag_my_vtag == 0 &&
cookie->tie_tag_peer_vtag == 0) {
return (NULL);
}
if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
(ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
init_cp->init.initiate_tag == 0)) {
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
net->hb_responded = 1;
if (stcb->asoc.sctp_autoclose_ticks &&
(inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
NULL);
}
asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
asoc->pre_open_streams =
ntohs(initack_cp->init.num_outbound_streams);
asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
asoc->init_seq_number;
asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
asoc->last_cwr_tsn = asoc->init_seq_number - 1;
asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
asoc->str_reset_seq_in = asoc->init_seq_number;
asoc->advanced_peer_ack_point = asoc->last_acked_seq;
retval = sctp_process_init(init_cp, stcb, net);
if (retval < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_existing: INIT processing failed\n");
}
#endif
return (NULL);
}
if (sctp_load_addresses_from_init(stcb, m, iphlen,
init_offset + sizeof(struct sctp_init_chunk),
initack_offset, sh, init_src)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Weird cookie load_address failure on cookie existing - 2\n");
}
#endif
return (NULL);
}
if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
(asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
*notification = SCTP_NOTIFY_ASSOC_UP;
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_ACCEPTING)) {
stcb->sctp_ep->sctp_flags |=
SCTP_PCB_FLAGS_CONNECTED;
soisconnected(stcb->sctp_ep->sctp_socket);
}
}
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_OPEN |
SCTP_STATE_SHUTDOWN_PENDING;
} else {
asoc->state = SCTP_STATE_OPEN;
}
sctp_send_cookie_ack(stcb);
return (stcb);
}
if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
cookie->tie_tag_peer_vtag != 0) {
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
*notification = SCTP_NOTIFY_ASSOC_RESTART;
sctp_report_all_outbound(stcb);
asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
asoc->pre_open_streams =
ntohs(initack_cp->init.num_outbound_streams);
asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
asoc->init_seq_number;
asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
asoc->last_cwr_tsn = asoc->init_seq_number - 1;
asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
asoc->str_reset_seq_in = asoc->init_seq_number;
asoc->advanced_peer_ack_point = asoc->last_acked_seq;
if (asoc->mapping_array)
memset(asoc->mapping_array, 0,
asoc->mapping_array_size);
retval = sctp_process_init(init_cp, stcb, net);
if (retval < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_existing: INIT processing failed\n");
}
#endif
return (NULL);
}
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
net->hb_responded = 1;
if (sctp_load_addresses_from_init(stcb, m, iphlen,
init_offset + sizeof(struct sctp_init_chunk),
initack_offset, sh, init_src)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Weird cookie load_address failure on cookie existing - 3\n");
}
#endif
return (NULL);
}
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_OPEN |
SCTP_STATE_SHUTDOWN_PENDING;
} else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
asoc->state = SCTP_STATE_OPEN;
}
sctp_send_cookie_ack(stcb);
return (stcb);
}
return (NULL);
}
static struct sctp_tcb *
sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
struct sctp_inpcb *inp, struct sctp_nets **netp,
struct sockaddr *init_src, int *notification)
{
struct sctp_tcb *stcb;
struct sctp_init_chunk *init_cp, init_buf;
struct sctp_init_ack_chunk *initack_cp, initack_buf;
struct sockaddr_storage sa_store;
struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct sctp_association *asoc;
int chk_length;
int init_offset, initack_offset, initack_limit;
int retval;
int error = 0;
init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
init_cp = (struct sctp_init_chunk *)
sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
(u_int8_t *)&init_buf);
if (init_cp == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_new: could not pull INIT chunk hdr\n");
}
#endif
return (NULL);
}
chk_length = ntohs(init_cp->ch.chunk_length);
if (init_cp->ch.chunk_type != SCTP_INITIATION) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("HUH? process_cookie_new: could not find INIT chunk!\n");
}
#endif
return (NULL);
}
initack_offset = init_offset + SCTP_SIZE32(chk_length);
initack_cp = (struct sctp_init_ack_chunk *)
sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
(u_int8_t *)&initack_buf);
if (initack_cp == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
}
#endif
return (NULL);
}
chk_length = ntohs(initack_cp->ch.chunk_length);
if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
u_int8_t *pp;
pp = (u_int8_t *)initack_cp;
printf("process_cookie_new: could not find INIT-ACK chunk!\n");
printf("Found bytes %x %x %x %x at position %d\n",
(u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
(u_int)pp[3], initack_offset);
}
#endif
return (NULL);
}
initack_limit = initack_offset + SCTP_SIZE32(chk_length);
stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
if (stcb == NULL) {
struct mbuf *op_err;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_new: no room for another TCB!\n");
}
#endif
op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
sh, op_err);
return (NULL);
}
*netp = sctp_findnet(stcb, init_src);
asoc = &stcb->asoc;
asoc->ipv4_local_scope = cookie->ipv4_scope;
asoc->site_scope = cookie->site_scope;
asoc->local_scope = cookie->local_scope;
asoc->loopback_scope = cookie->loopback_scope;
if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
(asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
struct mbuf *op_err;
op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
sh, op_err);
return (NULL);
}
asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
asoc->last_cwr_tsn = asoc->init_seq_number - 1;
asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
asoc->str_reset_seq_in = asoc->init_seq_number;
asoc->advanced_peer_ack_point = asoc->last_acked_seq;
retval = sctp_process_init(init_cp, stcb, *netp);
if (retval < 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_new: INIT processing failed\n");
}
#endif
sctp_free_assoc(inp, stcb);
return (NULL);
}
if (sctp_load_addresses_from_init(stcb, m, iphlen,
init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
init_src)) {
sctp_free_assoc(inp, stcb);
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("moving to OPEN state\n");
}
#endif
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
} else {
asoc->state = SCTP_STATE_OPEN;
}
(*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
&cookie->time_entered);
if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
sin = (struct sockaddr_in *)initack_src;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_addr.s_addr = cookie->laddress[0];
} else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
sin6 = (struct sockaddr_in6 *)initack_src;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_scope_id = cookie->scope_id;
memcpy(&sin6->sin6_addr, cookie->laddress,
sizeof(sin6->sin6_addr));
} else {
sctp_free_assoc(inp, stcb);
return (NULL);
}
sctp_check_address_list(stcb, m, initack_offset +
sizeof(struct sctp_init_ack_chunk), initack_limit,
initack_src, cookie->local_scope, cookie->site_scope,
cookie->ipv4_scope, cookie->loopback_scope);
*notification = SCTP_NOTIFY_ASSOC_UP;
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_ACCEPTING)) {
stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
soisconnected(stcb->sctp_ep->sctp_socket);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
;
} else {
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
}
(*netp)->hb_responded = 1;
if (stcb->asoc.sctp_autoclose_ticks &&
(inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
}
sctp_send_cookie_ack(stcb);
return (stcb);
}
static struct mbuf *
sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
{
struct sctp_state_cookie *cookie;
struct sockaddr_in6 sin6;
struct sockaddr_in sin;
struct sctp_tcb *l_stcb=*stcb;
struct sctp_inpcb *l_inp;
struct sockaddr *to;
struct sctp_pcb *ep;
struct mbuf *m_sig;
uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
uint8_t *sig;
uint8_t cookie_ok = 0;
unsigned int size_of_pkt, sig_offset, cookie_offset;
unsigned int cookie_len;
struct timeval now;
struct timeval time_expires;
struct sockaddr_storage dest_store;
struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
struct ip *iph;
int notification = 0;
struct sctp_nets *netl;
int had_a_existing_tcb = 0;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: handling COOKIE-ECHO\n");
}
#endif
if (inp_p == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("sctp_handle_cookie: null inp_p!\n");
}
#endif
return (NULL);
}
iph = mtod(m, struct ip *);
if (iph->ip_v == IPVERSION) {
struct sockaddr_in *sin_d;
sin_d = (struct sockaddr_in *)(localep_sa);
memset(sin_d, 0, sizeof(*sin_d));
sin_d->sin_family = AF_INET;
sin_d->sin_len = sizeof(*sin_d);
sin_d->sin_port = sh->dest_port;
sin_d->sin_addr.s_addr = iph->ip_dst.s_addr ;
} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
struct ip6_hdr *ip6;
struct sockaddr_in6 *sin6_d;
sin6_d = (struct sockaddr_in6 *)(localep_sa);
memset(sin6_d, 0, sizeof(*sin6_d));
sin6_d->sin6_family = AF_INET6;
sin6_d->sin6_len = sizeof(struct sockaddr_in6);
ip6 = mtod(m, struct ip6_hdr *);
sin6_d->sin6_port = sh->dest_port;
sin6_d->sin6_addr = ip6->ip6_dst;
} else {
return (NULL);
}
cookie = &cp->cookie;
cookie_offset = offset + sizeof(struct sctp_chunkhdr);
cookie_len = ntohs(cp->ch.chunk_length);
if (m->m_flags & M_PKTHDR) {
size_of_pkt = m->m_pkthdr.len;
} else {
struct mbuf *mat;
mat = m;
size_of_pkt = 0;
while (mat != NULL) {
size_of_pkt += mat->m_len;
mat = mat->m_next;
}
}
if (cookie_len > size_of_pkt ||
cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
sizeof(struct sctp_init_chunk) +
sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
}
#endif
return (NULL);
}
if ((cookie->peerport != sh->src_port) &&
(cookie->myport != sh->dest_port) &&
(cookie->my_vtag != sh->v_tag)) {
return (NULL);
}
sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
if (sig_offset > size_of_pkt) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
}
#endif
return (NULL);
}
m_sig = m_split(m, sig_offset, M_DONTWAIT);
if (m_sig == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("sctp_handle_cookie: couldn't m_split the signature\n");
}
#endif
return (NULL);
}
ep = &(*inp_p)->sctp_ep;
l_inp = *inp_p;
if (l_stcb) {
SCTP_TCB_UNLOCK(l_stcb);
}
SCTP_INP_RLOCK(l_inp);
if (l_stcb) {
SCTP_TCB_LOCK(l_stcb);
}
if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
(ep->current_secret_number != ep->last_secret_number)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: old cookie sig\n");
}
#endif
sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: current cookie sig\n");
}
#endif
sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
}
SCTP_INP_RUNLOCK(l_inp);
sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
if (sig == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("sctp_handle_cookie: couldn't pull the signature\n");
}
#endif
return (NULL);
}
if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
(ep->current_secret_number != ep->last_secret_number)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: old cookie sig\n");
}
#endif
sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
cookie_ok = 1;
}
} else {
cookie_ok = 1;
}
{
struct mbuf *m_at;
m_at = m;
while (m_at->m_next != NULL) {
m_at = m_at->m_next;
}
m_at->m_next = m_sig;
if (m->m_flags & M_PKTHDR) {
if (m_sig->m_flags & M_PKTHDR) {
m->m_pkthdr.len += m_sig->m_len;
} else {
int mmlen = 0;
struct mbuf *lat;
printf("Warning: Hitting m_split join TSNH code - fixed\n");
lat = m_sig;
while (lat) {
mmlen += lat->m_len;
lat = lat->m_next;
}
m->m_pkthdr.len += mmlen;
}
}
}
if (cookie_ok == 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("handle_cookie_echo: cookie signature validation failed!\n");
printf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
(u_int32_t)offset, cookie_offset, sig_offset);
}
#endif
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("handle_cookie_echo: cookie signature validation passed\n");
}
#endif
SCTP_GETTIME_TIMEVAL(&now);
time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
time_expires.tv_usec = cookie->time_entered.tv_usec;
#ifndef __FreeBSD__
if (timercmp(&now, &time_expires, >))
#else
if (timevalcmp(&now, &time_expires, >))
#endif
{
struct mbuf *op_err;
struct sctp_stale_cookie_msg *scm;
u_int32_t tim;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_handle_cookie: got a STALE cookie!\n");
}
#endif
MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
if (op_err == NULL) {
return (NULL);
}
op_err->m_data += sizeof(struct ip6_hdr);
op_err->m_data += sizeof(struct sctphdr);
op_err->m_data += sizeof(struct sctp_chunkhdr);
op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
scm = mtod(op_err, struct sctp_stale_cookie_msg *);
scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
(sizeof(u_int32_t))));
tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
if (tim == 0)
tim = now.tv_usec - cookie->time_entered.tv_usec;
scm->time_usec = htonl(tim);
sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
return (NULL);
}
to = NULL;
if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(sin6);
sin6.sin6_port = sh->src_port;
sin6.sin6_scope_id = cookie->scope_id;
memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
sizeof(sin6.sin6_addr.s6_addr));
to = (struct sockaddr *)&sin6;
} else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
sin.sin_port = sh->src_port;
sin.sin_addr.s_addr = cookie->address[0];
to = (struct sockaddr *)&sin;
}
if ((*stcb == NULL) && to) {
*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
if (*stcb == NULL) {
if (l_inp != *inp_p) {
printf("Bad problem find_ep got a diff inp then special_locate?\n");
}
}
}
cookie_len -= SCTP_SIGNATURE_SIZE;
if (*stcb == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: processing NEW cookie\n");
}
#endif
*stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
cookie_len, *inp_p, netp, to, ¬ification);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie: processing EXISTING cookie\n");
}
#endif
had_a_existing_tcb = 1;
*stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification);
}
if (*stcb == NULL) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("handle_cookie_echo: ACK! don't have a TCB!\n");
}
#endif
return (NULL);
}
netl = sctp_findnet(*stcb, to);
if (netl == NULL) {
#ifdef SCTP_DEBUG
printf("TSNH! Huh, why do I need to add this address here?\n");
#endif
sctp_add_remote_addr(*stcb, to, 0, 100);
netl = sctp_findnet(*stcb, to);
}
if (netl) {
if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
netl);
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
(*stcb), 0, (void *)netl);
}
}
#ifdef SCTP_DEBUG
else {
printf("Could not add source address for some reason\n");
}
#endif
if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
if (!had_a_existing_tcb ||
(((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
struct socket *so, *oso;
struct sctp_inpcb *inp;
if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
sctp_ulp_notify(notification, *stcb, 0, NULL);
return (m);
}
oso = (*inp_p)->sctp_socket;
SCTP_TCB_UNLOCK((*stcb));
so = sonewconn(oso, SS_ISCONNECTED);
SCTP_INP_WLOCK((*stcb)->sctp_ep);
SCTP_TCB_LOCK((*stcb));
SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
if (so == NULL) {
struct mbuf *op_err;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("process_cookie_new: no room for another socket!\n");
}
#endif
op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
sctp_abort_association(*inp_p, NULL, m, iphlen,
sh, op_err);
sctp_free_assoc(*inp_p, *stcb);
return (NULL);
}
inp = (struct sctp_inpcb *)so->so_pcb;
inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
SCTP_PCB_FLAGS_CONNECTED |
SCTP_PCB_FLAGS_IN_TCPPOOL |
(SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
SCTP_PCB_FLAGS_DONT_WAKE);
inp->sctp_socket = so;
sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
*inp_p = inp;
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
*stcb, *netp);
sctp_ulp_notify(notification, *stcb, 0, NULL);
return (m);
}
}
if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
sctp_ulp_notify(notification, *stcb, 0, NULL);
}
return (m);
}
static void
sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_association *asoc;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
}
#endif
if (stcb == NULL)
return;
asoc = &stcb->asoc;
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("moving to OPEN state\n");
}
#endif
if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
} else {
asoc->state = SCTP_STATE_OPEN;
}
if (asoc->overall_error_count == 0) {
net->RTO = sctp_calculate_rto(stcb, asoc, net,
&asoc->time_entered);
}
SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
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;
soisconnected(stcb->sctp_ep->sctp_socket);
}
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
stcb, net);
net->hb_responded = 1;
if (stcb->asoc.sctp_autoclose_ticks &&
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
stcb->sctp_ep, stcb, NULL);
}
if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
(stcb->asoc.peer_supports_asconf) &&
(!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
stcb->sctp_ep, stcb,
stcb->asoc.primary_destination);
}
}
sctp_toss_old_cookies(asoc);
if (!TAILQ_EMPTY(&asoc->sent_queue)) {
struct sctp_tmit_chunk *chk;
chk = TAILQ_FIRST(&asoc->sent_queue);
if (chk) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, chk->whoTo);
}
}
}
static void
sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
struct sctp_tcb *stcb)
{
struct sctp_nets *net;
struct sctp_tmit_chunk *lchk;
u_int32_t tsn;
if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
return;
}
sctp_pegs[SCTP_ECNE_RCVD]++;
tsn = ntohl(cp->tsn);
lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
if (lchk == NULL) {
stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
} else {
stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
}
stcb->asoc.nonce_wait_for_ecne = 0;
stcb->asoc.nonce_sum_check = 0;
net = NULL;
lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
while (lchk) {
if (lchk->rec.data.TSN_seq == tsn) {
net = lchk->whoTo;
break;
}
if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
break;
lchk = TAILQ_NEXT(lchk, sctp_next);
}
if (net == NULL)
net = stcb->asoc.primary_destination;
if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
#ifdef SCTP_CWND_LOGGING
int old_cwnd;
#endif
#ifdef SCTP_CWND_LOGGING
old_cwnd = net->cwnd;
#endif
sctp_pegs[SCTP_CWR_PERFO]++;
net->ssthresh = net->cwnd / 2;
if (net->ssthresh < net->mtu) {
net->ssthresh = net->mtu;
net->RTO <<= 2;
}
net->cwnd = net->ssthresh;
#ifdef SCTP_CWND_LOGGING
sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
#endif
stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
}
sctp_send_cwr(stcb, net, tsn);
}
static void
sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
{
struct sctp_tmit_chunk *chk;
struct sctp_ecne_chunk *ecne;
TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
continue;
}
ecne = mtod(chk->data, struct sctp_ecne_chunk *);
if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
MAX_TSN) || (cp->tsn == ecne->tsn)) {
TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
sctp_next);
sctp_m_freem(chk->data);
chk->data = NULL;
stcb->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++;
break;
}
}
}
static void
sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
struct sctp_association *asoc;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
}
#endif
if (stcb == NULL)
return;
asoc = &stcb->asoc;
if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
return;
}
sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
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;
stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
soisdisconnected(stcb->sctp_ep->sctp_socket);
}
if (!TAILQ_EMPTY(&asoc->send_queue) ||
!TAILQ_EMPTY(&asoc->sent_queue) ||
!TAILQ_EMPTY(&asoc->out_wheel)) {
sctp_report_all_outbound(stcb);
}
sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
sctp_free_assoc(stcb->sctp_ep, stcb);
return;
}
static int
process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
struct sctp_nets *net, u_int8_t flg)
{
switch (desc->chunk_type) {
case SCTP_DATA:
{
u_int32_t tsn;
struct sctp_tmit_chunk *tp1;
tsn = ntohl(desc->tsn_ifany);
tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
while (tp1) {
if (tp1->rec.data.TSN_seq == tsn) {
break;
}
if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
MAX_TSN)) {
tp1 = NULL;
break;
}
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
if (tp1 == NULL) {
sctp_pegs[SCTP_PDRP_DNFND]++;
tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
while (tp1) {
if (tp1->rec.data.TSN_seq == tsn) {
break;
}
tp1 = TAILQ_NEXT(tp1, sctp_next);
}
}
if (tp1 == NULL) {
sctp_pegs[SCTP_PDRP_TSNNF]++;
}
if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
u_int8_t *ddp;
if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
sctp_pegs[SCTP_PDRP_DIWNP]++;
return (0);
}
if (stcb->asoc.peers_rwnd == 0 &&
(flg & SCTP_FROM_MIDDLE_BOX)) {
sctp_pegs[SCTP_PDRP_DIZRW]++;
return (0);
}
ddp = (u_int8_t *)(mtod(tp1->data, vaddr_t) +
sizeof(struct sctp_data_chunk));
{
unsigned int iii;
for (iii = 0; iii < sizeof(desc->data_bytes);
iii++) {
if (ddp[iii] != desc->data_bytes[iii]) {
sctp_pegs[SCTP_PDRP_BADD]++;
return (-1);
}
}
}
if (tp1->sent != SCTP_DATAGRAM_RESEND) {
stcb->asoc.sent_queue_retran_cnt++;
}
tp1->rec.data.ect_nonce = 0;
if (tp1->do_rtt) {
tp1->whoTo->rto_pending = 0;
tp1->do_rtt = 0;
}
sctp_pegs[SCTP_PDRP_MARK]++;
tp1->sent = SCTP_DATAGRAM_RESEND;
tp1->rec.data.doing_fast_retransmit = 1;
if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
} else {
tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
}
sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, tp1->whoTo);
sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
stcb, tp1->whoTo);
sctp_flight_size_decrease(tp1);
sctp_total_flight_decrease(stcb, tp1);
tp1->snd_count--;
}
{
unsigned int audit;
audit = 0;
TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
if (tp1->sent == SCTP_DATAGRAM_RESEND)
audit++;
}
TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
sctp_next) {
if (tp1->sent == SCTP_DATAGRAM_RESEND)
audit++;
}
if (audit != stcb->asoc.sent_queue_retran_cnt) {
printf("**Local Audit finds cnt:%d asoc cnt:%d\n",
audit, stcb->asoc.sent_queue_retran_cnt);
#ifndef SCTP_AUDITING_ENABLED
stcb->asoc.sent_queue_retran_cnt = audit;
#endif
}
}
}
break;
case SCTP_ASCONF:
{
struct sctp_tmit_chunk *asconf;
TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
sctp_next) {
if (asconf->rec.chunk_id == SCTP_ASCONF) {
break;
}
}
if (asconf) {
if (asconf->sent != SCTP_DATAGRAM_RESEND)
stcb->asoc.sent_queue_retran_cnt++;
asconf->sent = SCTP_DATAGRAM_RESEND;
asconf->snd_count--;
}
}
break;
case SCTP_INITIATION:
stcb->asoc.dropped_special_cnt++;
if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
stcb, net);
sctp_send_initiate(stcb->sctp_ep, stcb);
}
break;
case SCTP_SELECTIVE_ACK:
sctp_send_sack(stcb);
break;
case SCTP_HEARTBEAT_REQUEST:
sctp_send_hb(stcb, 1, net);
break;
case SCTP_SHUTDOWN:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
printf("%s:%d sends a shutdown\n",
__FILE__,
__LINE__
);
}
#endif
sctp_send_shutdown(stcb, net);
break;
case SCTP_SHUTDOWN_ACK:
sctp_send_shutdown_ack(stcb, net);
break;
case SCTP_COOKIE_ECHO:
{
struct sctp_tmit_chunk *cookie;
cookie = NULL;
TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
sctp_next) {
if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
break;
}
}
if (cookie) {
if (cookie->sent != SCTP_DATAGRAM_RESEND)
stcb->asoc.sent_queue_retran_cnt++;
cookie->sent = SCTP_DATAGRAM_RESEND;
sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
}
}
break;
case SCTP_COOKIE_ACK:
sctp_send_cookie_ack(stcb);
break;
case SCTP_ASCONF_ACK:
sctp_send_asconf_ack(stcb, 1);
break;
case SCTP_FORWARD_CUM_TSN:
send_forward_tsn(stcb, &stcb->asoc);
break;
case SCTP_PACKET_DROPPED:
case SCTP_INITIATION_ACK:
case SCTP_HEARTBEAT_ACK:
case SCTP_ABORT_ASSOCIATION:
case SCTP_OPERATION_ERROR:
case SCTP_SHUTDOWN_COMPLETE:
case SCTP_ECN_ECHO:
case SCTP_ECN_CWR:
default:
break;
}
return (0);
}
static void
sctp_reset_in_stream(struct sctp_tcb *stcb,
struct sctp_stream_reset_response *resp, int number_entries)
{
int i;
uint16_t *list, temp;
if (resp->reset_flags & SCTP_RESET_PERFORMED) {
if (number_entries) {
list = resp->list_of_streams;
for (i = 0; i < number_entries; i++) {
temp = ntohs(list[i]);
list[i] = temp;
if (list[i] >= stcb->asoc.streamincnt) {
printf("Invalid stream in-stream reset %d\n", list[i]);
continue;
}
stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
}
} else {
list = NULL;
for (i = 0; i < stcb->asoc.streamincnt; i++) {
stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
}
}
sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
}
}
static void
sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
{
struct sctp_tmit_chunk *chk, *nchk;
struct sctp_association *asoc;
asoc = &stcb->asoc;
for (chk = TAILQ_FIRST(&asoc->control_send_queue);
chk; chk = nchk) {
nchk = TAILQ_NEXT(chk, sctp_next);
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)) {
continue;
} else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
printf("TSNH, an unknown stream reset request is in queue %x\n",
(u_int)ntohs(strreq->sr_req.ph.param_type));
continue;
}
sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
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++;
break;
}
}
}
void
sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
struct sctp_stream_reset_response *resp)
{
uint32_t seq, tsn;
int number_entries, param_length;
param_length = ntohs(resp->ph.param_length);
seq = ntohl(resp->reset_req_seq_resp);
if (seq == stcb->asoc.str_reset_seq_out) {
sctp_clean_up_stream_reset(stcb);
stcb->asoc.str_reset_seq_out++;
stcb->asoc.stream_reset_outstanding = 0;
tsn = ntohl(resp->reset_at_tsn);
number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
tsn--;
if ((tsn == stcb->asoc.cumulative_tsn) ||
(compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
sctp_reset_in_stream(stcb, resp, number_entries);
} else {
if (stcb->asoc.pending_reply != NULL) {
free(stcb->asoc.pending_reply, M_PCB);
}
stcb->asoc.pending_reply = malloc(param_length,
M_PCB, M_NOWAIT);
memcpy(stcb->asoc.pending_reply, resp, param_length);
}
} else {
#ifdef SCTP_DEBUG
printf("Duplicate old stream reset resp next:%x this one:%x\n",
stcb->asoc.str_reset_seq_out, seq);
#endif
}
}
static void
sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
{
int chk_length, param_len;
struct sctp_paramhdr *ph;
struct sctp_stream_reset_request *req;
struct sctp_stream_reset_response *resp;
chk_length = ntohs(sr_req->ch.chunk_length);
ph = (struct sctp_paramhdr *)&sr_req->sr_req;
while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
param_len = ntohs(ph->param_length);
if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
req = (struct sctp_stream_reset_request *)ph;
sctp_send_str_reset_ack(stcb, req);
} else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
resp = (struct sctp_stream_reset_response *)ph;
sctp_handle_stream_reset_response(stcb, resp);
}
ph = (struct sctp_paramhdr *)((vaddr_t)ph + SCTP_SIZE32(param_len));
chk_length -= SCTP_SIZE32(param_len);
}
}
static void
sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
struct sctp_tcb *stcb, struct sctp_nets *net)
{
u_int32_t bottle_bw, on_queue;
u_int16_t trunc_len;
unsigned int chlen;
unsigned int at;
struct sctp_chunk_desc desc;
struct sctp_chunkhdr *ch;
chlen = ntohs(cp->ch.chunk_length);
chlen -= sizeof(struct sctp_pktdrop_chunk);
if (chlen == 0) {
ch = NULL;
if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
sctp_pegs[SCTP_PDRP_BWRPT]++;
} else {
ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
chlen -= sizeof(struct sctphdr);
memset(&desc, 0, sizeof(desc));
}
if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
u_int32_t a_rwnd;
sctp_pegs[SCTP_PDRP_FEHOS]++;
bottle_bw = ntohl(cp->bottle_bw);
on_queue = ntohl(cp->current_onq);
if (bottle_bw && on_queue) {
if (bottle_bw > on_queue)
a_rwnd = bottle_bw - on_queue;
else
a_rwnd = 0;
if (a_rwnd <= 0)
stcb->asoc.peers_rwnd = 0;
else {
if (a_rwnd > stcb->asoc.total_flight) {
stcb->asoc.peers_rwnd =
a_rwnd - stcb->asoc.total_flight;
} else {
stcb->asoc.peers_rwnd = 0;
}
if (stcb->asoc.peers_rwnd <
stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
stcb->asoc.peers_rwnd = 0;
}
}
}
} else {
sctp_pegs[SCTP_PDRP_FMBOX]++;
}
trunc_len = (u_int16_t)ntohs(cp->trunc_len);
while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
desc.chunk_type = ch->chunk_type;
at = ntohs(ch->chunk_length);
if (at < sizeof(struct sctp_chunkhdr)) {
sctp_pegs[SCTP_PDRP_CRUPT]++;
break;
}
if (trunc_len == 0) {
if (at > chlen) {
sctp_pegs[SCTP_PDRP_CRUPT]++;
break;
}
} else {
if (desc.chunk_type == SCTP_DATA) {
if (chlen < (sizeof(struct sctp_data_chunk) +
sizeof(desc.data_bytes))) {
break;
}
} else {
if (chlen < sizeof(struct sctp_chunkhdr)) {
break;
}
}
}
if (desc.chunk_type == SCTP_DATA) {
if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
sctp_pegs[SCTP_PDRP_MB_DA]++;
if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
struct sctp_data_chunk *dcp;
u_int8_t *ddp;
unsigned int iii;
dcp = (struct sctp_data_chunk *)ch;
ddp = (u_int8_t *)(dcp + 1);
for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
desc.data_bytes[iii] = ddp[iii];
}
desc.tsn_ifany = dcp->dp.tsn;
} else {
sctp_pegs[SCTP_PDRP_NEDAT]++;
break;
}
} else {
if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
sctp_pegs[SCTP_PDRP_MB_CT]++;
}
if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
sctp_pegs[SCTP_PDRP_PDBRK]++;
break;
}
if (SCTP_SIZE32(at) > chlen) {
break;
}
chlen -= SCTP_SIZE32(at);
if (chlen < sizeof(struct sctp_chunkhdr)) {
break;
}
ch = (struct sctp_chunkhdr *)((vaddr_t)ch + SCTP_SIZE32(at));
}
if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
(stcb->asoc.sat_t3_loss_recovery == 0) &&
(stcb->asoc.sat_network)) {
u_int32_t bw_avail;
int rtt, incr;
#ifdef SCTP_CWND_LOGGING
int old_cwnd=net->cwnd;
#endif
rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
bottle_bw = ntohl(cp->bottle_bw);
on_queue = ntohl(cp->current_onq);
if (on_queue < net->flight_size)
on_queue = net->flight_size;
bw_avail = (bottle_bw*rtt)/1000;
if (bw_avail > bottle_bw) {
bw_avail = bottle_bw;
}
if (on_queue > bw_avail) {
int seg_inflight, seg_onqueue, my_portion;
net->partial_bytes_acked = 0;
incr = on_queue - bw_avail;
if (stcb->asoc.seen_a_sack_this_pkt) {
net->cwnd = net->prev_cwnd;
}
seg_inflight = net->flight_size / net->mtu;
seg_onqueue = on_queue / net->mtu;
my_portion = (incr * seg_inflight)/seg_onqueue;
if (net->cwnd > net->flight_size) {
int diff_adj;
diff_adj = net->cwnd - net->flight_size;
if (diff_adj > my_portion)
my_portion = 0;
else
my_portion -= diff_adj;
}
net->cwnd -= my_portion;
if (net->cwnd <= net->mtu) {
net->cwnd = net->mtu;
}
net->ssthresh = net->cwnd - 1;
} else {
incr = uimin((bw_avail - on_queue) >> 2,
(int)stcb->asoc.max_burst * (int)net->mtu);
net->cwnd += incr;
}
if (net->cwnd > bw_avail) {
net->cwnd = bw_avail;
}
if (net->cwnd < net->mtu) {
net->cwnd = net->mtu;
}
#ifdef SCTP_CWND_LOGGING
if (net->cwnd - old_cwnd != 0) {
sctp_log_cwnd(net, (net->cwnd - old_cwnd),
SCTP_CWND_LOG_FROM_SAT);
}
#endif
}
}
extern int sctp_strict_init;
static struct sctp_tcb *
sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
{
struct sctp_association *asoc;
u_int32_t vtag_in;
int num_chunks = 0;
int chk_length;
int ret;
u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
struct sctp_tcb *locked_tcb = stcb;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
iphlen, *offset, length, stcb);
}
#endif
if (ntohs(ch->chunk_length) < sizeof(*ch)) {
return (NULL);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: validating vtags\n");
}
#endif
vtag_in = ntohl(sh->v_tag);
if (ch->chunk_type == SCTP_INITIATION) {
if (vtag_in != 0) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: INIT with vtag != 0\n");
}
#endif
sctp_pegs[SCTP_BAD_VTAGS]++;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
stcb = sctp_findassociation_ep_asconf(m, iphlen,
*offset, sh, &inp, netp);
}
if (stcb == NULL) {
sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
ch->chunk_type);
}
#endif
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
asoc = &stcb->asoc;
if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
(ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
(ch->chunk_type == SCTP_PACKET_DROPPED)) {
if ((vtag_in == asoc->my_vtag) ||
((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
(vtag_in == asoc->peer_vtag))) {
} else {
sctp_pegs[SCTP_BAD_VTAGS]++;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
if (vtag_in != asoc->my_vtag) {
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
sctp_handle_ootb(m, iphlen, *offset, sh, inp,
NULL);
return (NULL);
}
} else {
if (vtag_in != asoc->my_vtag) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
}
#endif
sctp_pegs[SCTP_BAD_VTAGS]++;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
*offset = length;
return (NULL);
}
}
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: vtags ok, processing ctrl chunks\n");
}
#endif
if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
(ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
(SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
stcb->asoc.overall_error_count = 0;
sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
}
while (IS_SCTP_CONTROL(ch)) {
chk_length = ntohs(ch->chunk_length);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
printf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
}
#endif
if ((size_t)chk_length < sizeof(*ch) ||
(*offset + chk_length) > length) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
*offset, chk_length, length);
}
#endif
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
if (ch->chunk_type == SCTP_INITIATION_ACK) {
ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
sizeof(struct sctp_init_ack), chunk_buf);
if (ch == NULL) {
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
} else {
if ((size_t)chk_length > sizeof(chunk_buf)) {
struct mbuf *oper;
struct sctp_paramhdr *phdr;
oper = NULL;
MGETHDR(oper, M_DONTWAIT, MT_HEADER);
if (oper) {
oper->m_data +=
sizeof(struct sctp_chunkhdr);
phdr =
mtod(oper, struct sctp_paramhdr *);
phdr->param_type =
htons(SCTP_CAUSE_OUT_OF_RESC);
phdr->param_length =
htons(sizeof(struct sctp_paramhdr));
sctp_queue_op_err(stcb, oper);
}
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
chk_length, chunk_buf);
if (ch == NULL) {
printf("sctp_process_control: Can't get the all data....\n");
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
}
num_chunks++;
if ((*netp) && stcb) {
stcb->asoc.last_control_chunk_from = *netp;
}
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xB0, ch->chunk_type);
#endif
switch (ch->chunk_type) {
case SCTP_INITIATION:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_INIT\n");
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
sctp_inpcb_free(inp, 1);
}
*offset = length;
return (NULL);
}
if ((num_chunks > 1) ||
(sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
if ((stcb != NULL) &&
(SCTP_GET_STATE(&stcb->asoc) ==
SCTP_STATE_SHUTDOWN_ACK_SENT)) {
sctp_send_shutdown_ack(stcb,
stcb->asoc.primary_destination);
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
sctp_handle_init(m, iphlen, *offset, sh,
(struct sctp_init_chunk *)ch, inp, stcb, *netp);
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
break;
case SCTP_INITIATION_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_INIT-ACK\n");
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
*offset = length;
if (stcb) {
sctp_free_assoc(inp, stcb);
} else {
if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
sctp_inpcb_free(inp, 1);
}
}
return (NULL);
}
if ((num_chunks > 1) ||
(sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("Length is %d rounded chk_length:%d .. dropping\n",
length - *offset,
SCTP_SIZE32(chk_length));
}
#endif
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
(struct sctp_init_ack_chunk *)ch, stcb, *netp);
if ((stcb) && ret == 0)
sctp_chunk_output(stcb->sctp_ep, stcb, 2);
*offset = length;
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("All done INIT-ACK processing\n");
}
#endif
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
break;
case SCTP_SELECTIVE_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_SACK\n");
}
#endif
sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
{
int abort_now = 0;
stcb->asoc.seen_a_sack_this_pkt = 1;
sctp_handle_sack((struct sctp_sack_chunk *)ch,
stcb, *netp, &abort_now);
if (abort_now) {
*offset = length;
return (NULL);
}
}
break;
case SCTP_HEARTBEAT_REQUEST:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_HEARTBEAT\n");
}
#endif
sctp_pegs[SCTP_HB_RECV]++;
sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
*netp);
stcb->asoc.overall_error_count = 0;
break;
case SCTP_HEARTBEAT_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_HEARTBEAT-ACK\n");
}
#endif
stcb->asoc.overall_error_count = 0;
sctp_pegs[SCTP_HB_ACK_RECV]++;
sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
stcb, *netp);
break;
case SCTP_ABORT_ASSOCIATION:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_ABORT\n");
}
#endif
sctp_handle_abort((struct sctp_abort_chunk *)ch,
stcb, *netp);
*offset = length;
return (NULL);
break;
case SCTP_SHUTDOWN:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_SHUTDOWN\n");
}
#endif
{
int abort_flag = 0;
sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
stcb, *netp, &abort_flag);
if (abort_flag) {
*offset = length;
return (NULL);
}
}
break;
case SCTP_SHUTDOWN_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_SHUTDOWN-ACK\n");
}
#endif
sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
*offset = length;
return (NULL);
break;
case SCTP_OPERATION_ERROR:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_OP-ERR\n");
}
#endif
if (sctp_handle_error(ch, stcb, *netp) < 0) {
*offset = length;
return (NULL);
}
break;
case SCTP_COOKIE_ECHO:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
*offset = length;
if (stcb) {
sctp_free_assoc(inp, stcb);
} else {
if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
sctp_inpcb_free(inp, 1);
}
}
return (NULL);
}
if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
(inp->sctp_socket->so_qlimit == 0)) {
sctp_abort_association(inp, stcb, m, iphlen, sh,
NULL);
*offset = length;
return (NULL);
} else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
if (inp->sctp_socket->so_qlen >
inp->sctp_socket->so_qlimit) {
struct mbuf *oper;
struct sctp_paramhdr *phdr;
oper = NULL;
MGETHDR(oper, M_DONTWAIT, MT_HEADER);
if (oper) {
oper->m_len =
oper->m_pkthdr.len =
sizeof(struct sctp_paramhdr);
phdr = mtod(oper,
struct sctp_paramhdr *);
phdr->param_type =
htons(SCTP_CAUSE_OUT_OF_RESC);
phdr->param_length =
htons(sizeof(struct sctp_paramhdr));
}
sctp_abort_association(inp, stcb, m,
iphlen, sh, oper);
*offset = length;
return (NULL);
}
}
{
struct mbuf *ret_buf;
ret_buf = sctp_handle_cookie_echo(m, iphlen,
*offset, sh,
(struct sctp_cookie_echo_chunk *)ch, &inp,
&stcb, netp);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("ret_buf:%p length:%d off:%d\n",
ret_buf, length, *offset);
}
#endif
if (ret_buf == NULL) {
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("GAK, null buffer\n");
}
#endif
*offset = length;
return (NULL);
}
if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
struct sctp_tmit_chunk *chk;
chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
if (chk) {
sctp_timer_start(SCTP_TIMER_TYPE_SEND,
stcb->sctp_ep, stcb,
chk->whoTo);
}
}
}
break;
case SCTP_COOKIE_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_COOKIE-ACK\n");
}
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
sctp_free_assoc(inp, stcb);
*offset = length;
return (NULL);
}
stcb->asoc.overall_error_count = 0;
sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
stcb, *netp);
break;
case SCTP_ECN_ECHO:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_ECN-ECHO\n");
}
#endif
stcb->asoc.overall_error_count = 0;
sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
stcb);
break;
case SCTP_ECN_CWR:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_ECN-CWR\n");
}
#endif
stcb->asoc.overall_error_count = 0;
sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
break;
case SCTP_SHUTDOWN_COMPLETE:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_SHUTDOWN-COMPLETE\n");
}
#endif
if ((num_chunks > 1) ||
(length - *offset > SCTP_SIZE32(chk_length))) {
*offset = length;
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
return (NULL);
}
sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
stcb, *netp);
*offset = length;
return (NULL);
break;
case SCTP_ASCONF:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_ASCONF\n");
}
#endif
stcb->asoc.overall_error_count = 0;
sctp_handle_asconf(m, *offset,
(struct sctp_asconf_chunk *)ch, stcb, *netp);
break;
case SCTP_ASCONF_ACK:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_ASCONF-ACK\n");
}
#endif
stcb->asoc.overall_error_count = 0;
sctp_handle_asconf_ack(m, *offset,
(struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
break;
case SCTP_FORWARD_CUM_TSN:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_FWD-TSN\n");
}
#endif
{
int abort_flag = 0;
stcb->asoc.overall_error_count = 0;
*fwd_tsn_seen = 1;
sctp_handle_forward_tsn(stcb,
(struct sctp_forward_tsn_chunk *)ch, &abort_flag);
if (abort_flag) {
*offset = length;
return (NULL);
} else {
stcb->asoc.overall_error_count = 0;
}
}
break;
case SCTP_STREAM_RESET:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_STREAM_RESET\n");
}
#endif
ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
chk_length, chunk_buf);
if (stcb->asoc.peer_supports_strreset == 0) {
stcb->asoc.peer_supports_strreset = 1;
}
sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
break;
case SCTP_PACKET_DROPPED:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("SCTP_PACKET_DROPPED\n");
}
#endif
ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
chk_length, chunk_buf);
sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
stcb, *netp);
break;
default:
if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
struct mbuf *mm;
struct sctp_paramhdr *phd;
MGETHDR(mm, M_DONTWAIT, MT_HEADER);
if (mm) {
phd = mtod(mm, struct sctp_paramhdr *);
phd->param_type =
htons(SCTP_CAUSE_UNRECOG_CHUNK);
phd->param_length =
htons(chk_length + sizeof(*phd));
mm->m_len = sizeof(*phd);
mm->m_next = sctp_m_copym(m, *offset,
SCTP_SIZE32(chk_length),
M_DONTWAIT);
if (mm->m_next) {
mm->m_pkthdr.len =
SCTP_SIZE32(chk_length) +
sizeof(*phd);
sctp_queue_op_err(stcb, mm);
} else {
sctp_m_freem(mm);
#ifdef SCTP_DEBUG
if (sctp_debug_on &
SCTP_DEBUG_INPUT1) {
printf("Gak can't copy the chunk into operr %d bytes\n",
chk_length);
}
#endif
}
}
#ifdef SCTP_DEBUG
else {
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("Gak can't mgethdr for op-err of unrec chunk\n");
}
}
#endif
}
if ((ch->chunk_type & 0x80) == 0) {
*offset = length;
return (stcb);
}
break;
}
*offset += SCTP_SIZE32(chk_length);
if (*offset >= length) {
break;
}
ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
sizeof(struct sctp_chunkhdr), chunk_buf);
if (ch == NULL) {
if (locked_tcb) {
SCTP_TCB_UNLOCK(locked_tcb);
}
*offset = length;
return (NULL);
}
}
return (stcb);
}
static void
sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
u_int8_t ecn_bits)
{
if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
;
} else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
stcb->asoc.receiver_nonce_sum++;
stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
if (compare_with_wrap(stcb->asoc.cumulative_tsn,
stcb->asoc.last_echo_tsn, MAX_TSN)) {
stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
}
} else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
if (compare_with_wrap(stcb->asoc.cumulative_tsn,
stcb->asoc.last_echo_tsn, MAX_TSN)) {
stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
}
}
}
static void
sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
u_int32_t high_tsn, u_int8_t ecn_bits)
{
if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
MAX_TSN)) {
sctp_send_ecn_echo(stcb, net, high_tsn);
stcb->asoc.last_echo_tsn = high_tsn;
}
}
}
int
sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
u_int8_t ecn_bits)
{
u_int32_t high_tsn;
int fwd_tsn_seen = 0, data_processed = 0;
struct mbuf *m = *mm;
int abort_flag = 0;
sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xE0, 1);
sctp_auditing(0, inp, stcb, net);
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d\n",
m, iphlen, offset, length);
}
#endif
if (IS_SCTP_CONTROL(ch)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Processing control\n");
}
#endif
stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
inp, stcb, &net, &fwd_tsn_seen);
} else {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("No control present\n");
}
#endif
if (stcb == NULL) {
sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
return (1);
}
if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
sctp_pegs[SCTP_BAD_VTAGS]++;
SCTP_TCB_UNLOCK(stcb);
return (1);
}
}
if (stcb == NULL) {
return (1);
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Ok, control finished time to look for data (%d) offset:%d\n",
length, offset);
}
#endif
stcb->asoc.seen_a_sack_this_pkt = 0;
if (length > offset) {
int retval;
switch (SCTP_GET_STATE(&stcb->asoc)) {
case SCTP_STATE_COOKIE_ECHOED:
stcb->asoc.overall_error_count = 0;
sctp_handle_cookie_ack(
(struct sctp_cookie_ack_chunk *)ch, stcb, net);
break;
case SCTP_STATE_COOKIE_WAIT:
sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
SCTP_TCB_UNLOCK(stcb);
return (1);
break;
case SCTP_STATE_EMPTY:
case SCTP_STATE_INUSE:
case SCTP_STATE_SHUTDOWN_RECEIVED:
case SCTP_STATE_SHUTDOWN_ACK_SENT:
default:
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
}
#endif
SCTP_TCB_UNLOCK(stcb);
return (1);
break;
case SCTP_STATE_OPEN:
case SCTP_STATE_SHUTDOWN_SENT:
break;
}
if (stcb->asoc.ecn_allowed &&
(ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
sctp_process_ecn_marked_a(stcb, net, ecn_bits);
}
retval = sctp_process_data(mm, iphlen, &offset, length, sh,
inp, stcb, net, &high_tsn);
if (retval == 2) {
return (0);
}
data_processed = 1;
if (retval == 0) {
if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
}
}
}
if ((data_processed == 0) && (fwd_tsn_seen)) {
int was_a_gap = 0;
if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
stcb->asoc.cumulative_tsn, MAX_TSN)) {
was_a_gap = 1;
}
sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
if (abort_flag) {
return (0);
}
}
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xE0, 2);
sctp_auditing(1, inp, stcb, net);
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Check for chunk output prw:%d tqe:%d tf=%d\n",
stcb->asoc.peers_rwnd,
TAILQ_EMPTY(&stcb->asoc.control_send_queue),
stcb->asoc.total_flight);
}
#endif
if (stcb->asoc.peers_rwnd > 0 ||
!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
(stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("Calling chunk OUTPUT\n");
}
#endif
sctp_chunk_output(inp, stcb, 3);
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
printf("chunk OUTPUT returns\n");
}
#endif
}
#ifdef SCTP_AUDITING_ENABLED
sctp_audit_log(0xE0, 3);
sctp_auditing(2, inp, stcb, net);
#endif
SCTP_TCB_UNLOCK(stcb);
return (0);
}
#if defined(__OpenBSD__)
static void
sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
struct mbuf *m)
{
if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
*mp = sbcreatecontrol((vaddr_t) &ip->ip_dst,
sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
if (*mp)
mp = &(*mp)->m_next;
}
}
#endif
extern int sctp_no_csum_on_loopback;
void
sctp_input(struct mbuf *m, int off, int proto)
{
int iphlen;
u_int8_t ecn_bits;
struct ip *ip;
struct sctphdr *sh;
struct sctp_inpcb *inp = NULL;
struct mbuf *opts = 0;
u_int32_t check, calc_check;
struct sctp_nets *net;
struct sctp_tcb *stcb = NULL;
struct sctp_chunkhdr *ch;
int refcount_up = 0;
int length, mlen, offset;
iphlen = off;
net = NULL;
sctp_pegs[SCTP_INPKTS]++;
#ifdef SCTP_DEBUG
printf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
#endif
if ((size_t)iphlen > sizeof(struct ip)) {
printf("sctp_input: got options\n");
#if 0
ip_stripoptions(m, (struct mbuf *)0);
#endif
iphlen = sizeof(struct ip);
}
ip = mtod(m, struct ip *);
offset = iphlen + sizeof(*sh) + sizeof(*ch);
if (m->m_len < offset) {
if ((m = m_pullup(m, offset)) == 0) {
sctp_pegs[SCTP_HDR_DROPS]++;
return;
}
ip = mtod(m, struct ip *);
}
sh = (struct sctphdr *)((vaddr_t)ip + iphlen);
ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(*sh));
if (IN_MULTICAST(ip->ip_dst.s_addr))
{
sctp_pegs[SCTP_IN_MCAST]++;
goto bad;
}
if (in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m))) {
sctp_pegs[SCTP_IN_MCAST]++;
goto bad;
}
if (sh->dest_port == 0) {
sctp_pegs[SCTP_HDR_DROPS]++;
goto bad;
}
if ((sctp_no_csum_on_loopback == 0) ||
(m_get_rcvif_NOMPSAFE(m) == NULL) ||
(m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
check = sh->checksum;
if ((check == 0) && (sctp_no_csum_on_loopback)) {
if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
goto sctp_skip_csum_4;
}
sh->checksum = 0;
calc_check = sctp_calculate_sum(m, &mlen, iphlen);
if (calc_check != check) {
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
calc_check, check, m, mlen, iphlen);
}
#endif
stcb = sctp_findassociation_addr(m, iphlen,
offset - sizeof(*ch),
sh, ch, &inp, &net);
if ((inp) && (stcb)) {
sctp_send_packet_dropped(stcb, net, m, iphlen,
1);
sctp_chunk_output(inp, stcb, 2);
} else if ((inp != NULL) && (stcb == NULL)) {
refcount_up = 1;
}
sctp_pegs[SCTP_BAD_CSUM]++;
goto bad;
}
sh->checksum = calc_check;
} else {
sctp_skip_csum_4:
mlen = m->m_pkthdr.len;
}
#if defined(__NetBSD__) || defined(__OpenBSD__)
NTOHS(ip->ip_len);
#endif
if (mlen < (ip->ip_len - iphlen)) {
sctp_pegs[SCTP_HDR_DROPS]++;
goto bad;
}
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("V4 find association\n");
}
#endif
stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
sh, ch, &inp, &net);
if (inp == NULL) {
struct sctp_init_chunk *init_chk, chunk_buf;
sctp_pegs[SCTP_NOPORTS]++;
#ifdef ICMP_BANDLIM
if (badport_bandlim(0) < 0)
goto bad;
#endif
#ifdef SCTP_DEBUG
if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
printf("Sending a ABORT from packet entry!\n");
}
#endif
if (ch->chunk_type == SCTP_INITIATION) {
init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
iphlen + sizeof(*sh), sizeof(*init_chk),
(u_int8_t *)&chunk_buf);
if (init_chk != NULL)
sh->v_tag = init_chk->init.initiate_tag;
}
sctp_send_abort(m, iphlen, sh, 0, NULL);
goto bad;
} else if (stcb == NULL) {
refcount_up = 1;
}
#ifdef IPSEC
if (ipsec_used && ipsec_in_reject(m, (struct inpcb *)inp)) {
#if 0
ipsecstat.in_polvio++;
#endif
sctp_pegs[SCTP_HDR_DROPS]++;
goto bad;
}
#endif
if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
|| (inp->sctp_socket->so_options & SO_TIMESTAMP)
) {
ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
}
length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
offset -= sizeof(struct sctp_chunkhdr);
ecn_bits = ip->ip_tos;
sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
inp, stcb, net, ecn_bits);
sctp_m_freem(m);
sctp_m_freem(opts);
if ((inp) && (refcount_up)) {
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
return;
bad:
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
if ((inp) && (refcount_up)) {
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
sctp_m_freem(m);
sctp_m_freem(opts);
return;
}