sctp_input.c revision 168124
1163953Srrs/*- 2166675Srrs * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved. 3163953Srrs * 4163953Srrs * Redistribution and use in source and binary forms, with or without 5163953Srrs * modification, are permitted provided that the following conditions are met: 6163953Srrs * 7163953Srrs * a) Redistributions of source code must retain the above copyright notice, 8163953Srrs * this list of conditions and the following disclaimer. 9163953Srrs * 10163953Srrs * b) Redistributions in binary form must reproduce the above copyright 11163953Srrs * notice, this list of conditions and the following disclaimer in 12163953Srrs * the documentation and/or other materials provided with the distribution. 13163953Srrs * 14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 15163953Srrs * contributors may be used to endorse or promote products derived 16163953Srrs * from this software without specific prior written permission. 17163953Srrs * 18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 29163953Srrs */ 30163953Srrs 31163953Srrs/* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */ 32163953Srrs 33163953Srrs#include <sys/cdefs.h> 34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 168124 2007-03-31 11:47:30Z rrs $"); 35163953Srrs 36163953Srrs#include <netinet/sctp_os.h> 37163953Srrs#include <netinet/sctp_var.h> 38167598Srrs#include <netinet/sctp_sysctl.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctputil.h> 42163953Srrs#include <netinet/sctp_output.h> 43163953Srrs#include <netinet/sctp_input.h> 44163953Srrs#include <netinet/sctp_auth.h> 45163953Srrs#include <netinet/sctp_indata.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47163953Srrs 48163953Srrs 49163953Srrs 50163953Srrs 51163953Srrsstatic void 52163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 53163953Srrs{ 54163953Srrs struct sctp_nets *net; 55163953Srrs 56165220Srrs /* 57165220Srrs * This now not only stops all cookie timers it also stops any INIT 58165220Srrs * timers as well. This will make sure that the timers are stopped 59165220Srrs * in all collision cases. 60165220Srrs */ 61163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 62163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 63165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 64163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 65163953Srrs stcb->sctp_ep, 66163953Srrs stcb, 67165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 68165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 69165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 70165220Srrs stcb->sctp_ep, 71165220Srrs stcb, 72165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 73163953Srrs } 74163953Srrs } 75163953Srrs} 76163953Srrs 77163953Srrs/* INIT handler */ 78163953Srrsstatic void 79163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 80163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 81163953Srrs struct sctp_nets *net) 82163953Srrs{ 83163953Srrs struct sctp_init *init; 84163953Srrs struct mbuf *op_err; 85163953Srrs uint32_t init_limit; 86163953Srrs 87163953Srrs#ifdef SCTP_DEBUG 88163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 89163953Srrs printf("sctp_handle_init: handling INIT tcb:%p\n", stcb); 90163953Srrs } 91163953Srrs#endif 92163953Srrs op_err = NULL; 93163953Srrs init = &cp->init; 94163953Srrs /* First are we accepting? */ 95163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 96163953Srrs#ifdef SCTP_DEBUG 97163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 98163953Srrs printf("sctp_handle_init: Abort, so_qlimit:%d\n", inp->sctp_socket->so_qlimit); 99163953Srrs } 100163953Srrs#endif 101163953Srrs /* 102163953Srrs * FIX ME ?? What about TCP model and we have a 103163953Srrs * match/restart case? 104163953Srrs */ 105163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 106163953Srrs return; 107163953Srrs } 108163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 109163953Srrs /* Invalid length */ 110163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 111163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 112163953Srrs return; 113163953Srrs } 114163953Srrs /* validate parameters */ 115163953Srrs if (init->initiate_tag == 0) { 116163953Srrs /* protocol error... send abort */ 117163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 118163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 119163953Srrs return; 120163953Srrs } 121163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 122163953Srrs /* invalid parameter... send abort */ 123163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 124163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 125163953Srrs return; 126163953Srrs } 127163953Srrs if (init->num_inbound_streams == 0) { 128163953Srrs /* protocol error... send abort */ 129163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 130163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 131163953Srrs return; 132163953Srrs } 133163953Srrs if (init->num_outbound_streams == 0) { 134163953Srrs /* protocol error... send abort */ 135163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 136163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err); 137163953Srrs return; 138163953Srrs } 139163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 140163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 141163953Srrs init_limit)) { 142163953Srrs /* auth parameter(s) error... send abort */ 143163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL); 144163953Srrs return; 145163953Srrs } 146163953Srrs /* send an INIT-ACK w/cookie */ 147163953Srrs#ifdef SCTP_DEBUG 148163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 149163953Srrs printf("sctp_handle_init: sending INIT-ACK\n"); 150163953Srrs } 151163953Srrs#endif 152163953Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp); 153163953Srrs} 154163953Srrs 155163953Srrs/* 156163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 157163953Srrs */ 158163953Srrsstatic int 159163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 160163953Srrs struct sctp_nets *net) 161163953Srrs{ 162163953Srrs struct sctp_init *init; 163163953Srrs struct sctp_association *asoc; 164163953Srrs struct sctp_nets *lnet; 165163953Srrs unsigned int i; 166163953Srrs 167163953Srrs init = &cp->init; 168163953Srrs asoc = &stcb->asoc; 169163953Srrs /* save off parameters */ 170163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 171163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 172163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 173163953Srrs /* update any ssthresh's that may have a default */ 174163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 175163953Srrs lnet->ssthresh = asoc->peers_rwnd; 176163953Srrs 177163953Srrs#if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING) 178163953Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 179163953Srrs#endif 180163953Srrs 181163953Srrs } 182163953Srrs } 183164181Srrs SCTP_TCB_SEND_LOCK(stcb); 184163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 185163953Srrs unsigned int newcnt; 186163953Srrs struct sctp_stream_out *outs; 187163953Srrs struct sctp_stream_queue_pending *sp; 188163953Srrs 189163953Srrs /* cut back on number of streams */ 190163953Srrs newcnt = ntohs(init->num_inbound_streams); 191163953Srrs /* This if is probably not needed but I am cautious */ 192163953Srrs if (asoc->strmout) { 193163953Srrs /* First make sure no data chunks are trapped */ 194163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 195163953Srrs outs = &asoc->strmout[i]; 196163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 197163953Srrs while (sp) { 198163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 199163953Srrs next); 200163953Srrs asoc->stream_queue_cnt--; 201163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 202163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 203163953Srrs sp); 204163953Srrs if (sp->data) { 205163953Srrs sctp_m_freem(sp->data); 206163953Srrs sp->data = NULL; 207163953Srrs } 208163953Srrs sctp_free_remote_addr(sp->net); 209163953Srrs sp->net = NULL; 210163953Srrs /* Free the chunk */ 211163964Srrs printf("sp:%p tcb:%p weird free case\n", 212163979Sru sp, stcb); 213163953Srrs 214163953Srrs sctp_free_a_strmoq(stcb, sp); 215163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 216163953Srrs } 217163953Srrs } 218163953Srrs } 219163953Srrs /* cut back the count and abandon the upper streams */ 220163953Srrs asoc->pre_open_streams = newcnt; 221163953Srrs } 222164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 223163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 224163953Srrs /* init tsn's */ 225163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 226163953Srrs#ifdef SCTP_MAP_LOGGING 227163953Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 228163953Srrs#endif 229163953Srrs /* This is the next one we expect */ 230163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 231163953Srrs 232163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 233163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 234163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 235163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 236163953Srrs /* open the requested streams */ 237163953Srrs if (asoc->strmin != NULL) { 238163953Srrs /* Free the old ones */ 239164181Srrs struct sctp_queued_to_read *ctl; 240164181Srrs 241164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 242164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 243164181Srrs while (ctl) { 244164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 245164181Srrs sctp_free_remote_addr(ctl->whoFrom); 246164181Srrs sctp_m_freem(ctl->data); 247164181Srrs ctl->data = NULL; 248164181Srrs sctp_free_a_readq(stcb, ctl); 249164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 250164181Srrs } 251164181Srrs } 252163953Srrs SCTP_FREE(asoc->strmin); 253163953Srrs } 254164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 255164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 256164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 257164181Srrs } 258163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 259163953Srrs sizeof(struct sctp_stream_in), "StreamsIn"); 260163953Srrs if (asoc->strmin == NULL) { 261163953Srrs /* we didn't get memory for the streams! */ 262163953Srrs#ifdef SCTP_DEBUG 263163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 264163953Srrs printf("process_init: couldn't get memory for the streams!\n"); 265163953Srrs } 266163953Srrs#endif 267163953Srrs return (-1); 268163953Srrs } 269163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 270163953Srrs asoc->strmin[i].stream_no = i; 271163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 272163953Srrs /* 273163953Srrs * U-stream ranges will be set when the cookie is unpacked. 274163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 275163953Srrs * supported) when the INIT-ACK arrives. 276163953Srrs */ 277163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 278163953Srrs /* 279163953Srrs * we are not on any wheel, pr-sctp streams will go on the 280163953Srrs * wheel when they have data waiting for reorder. 281163953Srrs */ 282163953Srrs asoc->strmin[i].next_spoke.tqe_next = 0; 283163953Srrs asoc->strmin[i].next_spoke.tqe_prev = 0; 284163953Srrs } 285163953Srrs 286163953Srrs /* 287163953Srrs * load_address_from_init will put the addresses into the 288163953Srrs * association when the COOKIE is processed or the INIT-ACK is 289163953Srrs * processed. Both types of COOKIE's existing and new call this 290163953Srrs * routine. It will remove addresses that are no longer in the 291163953Srrs * association (for the restarting case where addresses are 292163953Srrs * removed). Up front when the INIT arrives we will discard it if it 293163953Srrs * is a restart and new addresses have been added. 294163953Srrs */ 295163953Srrs return (0); 296163953Srrs} 297163953Srrs 298163953Srrs/* 299163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 300163953Srrs */ 301163953Srrsstatic int 302163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 303163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 304163953Srrs struct sctp_nets *net) 305163953Srrs{ 306163953Srrs struct sctp_association *asoc; 307163953Srrs struct mbuf *op_err; 308163953Srrs int retval, abort_flag; 309163953Srrs uint32_t initack_limit; 310163953Srrs 311163953Srrs /* First verify that we have no illegal param's */ 312163953Srrs abort_flag = 0; 313163953Srrs op_err = NULL; 314163953Srrs 315163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 316163953Srrs (offset + sizeof(struct sctp_init_chunk)), 317163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 318163953Srrs if (abort_flag) { 319163953Srrs /* Send an abort and notify peer */ 320163953Srrs if (op_err != NULL) { 321163953Srrs sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag); 322163953Srrs } else { 323163953Srrs /* 324163953Srrs * Just notify (abort_assoc does this if we send an 325163953Srrs * abort). 326163953Srrs */ 327163953Srrs sctp_abort_notification(stcb, 0); 328163953Srrs /* 329163953Srrs * No sense in further INIT's since we will get the 330163953Srrs * same param back 331163953Srrs */ 332165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 333163953Srrs } 334163953Srrs return (-1); 335163953Srrs } 336163953Srrs asoc = &stcb->asoc; 337163953Srrs /* process the peer's parameters in the INIT-ACK */ 338163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 339163953Srrs if (retval < 0) { 340163953Srrs return (retval); 341163953Srrs } 342163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 343163953Srrs /* load all addresses */ 344163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 345163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 346163953Srrs NULL))) { 347163953Srrs /* Huh, we should abort */ 348163953Srrs#ifdef SCTP_DEBUG 349163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 350163953Srrs printf("Load addresses from INIT causes an abort %d\n", retval); 351163953Srrs } 352163953Srrs#endif 353163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 354163953Srrs NULL); 355163953Srrs return (-1); 356163953Srrs } 357163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 358163953Srrs stcb->asoc.local_hmacs); 359163953Srrs if (op_err) { 360163953Srrs sctp_queue_op_err(stcb, op_err); 361163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 362163953Srrs op_err = NULL; 363163953Srrs } 364163953Srrs /* extract the cookie and queue it to "echo" it back... */ 365163953Srrs stcb->asoc.overall_error_count = 0; 366163953Srrs net->error_count = 0; 367163953Srrs 368163953Srrs /* 369163953Srrs * Cancel the INIT timer, We do this first before queueing the 370163953Srrs * cookie. We always cancel at the primary to assue that we are 371163953Srrs * canceling the timer started by the INIT which always goes to the 372163953Srrs * primary. 373163953Srrs */ 374163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 375165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 376163953Srrs 377165220Srrs /* calculate the RTO */ 378165220Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered); 379165220Srrs 380163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 381163953Srrs if (retval < 0) { 382163953Srrs /* 383163953Srrs * No cookie, we probably should send a op error. But in any 384163953Srrs * case if there is no cookie in the INIT-ACK, we can 385163953Srrs * abandon the peer, its broke. 386163953Srrs */ 387163953Srrs if (retval == -3) { 388163953Srrs /* We abort with an error of missing mandatory param */ 389163953Srrs struct mbuf *op_err; 390163953Srrs 391163953Srrs op_err = 392163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 393163953Srrs if (op_err) { 394163953Srrs /* 395163953Srrs * Expand beyond to include the mandatory 396163953Srrs * param cookie 397163953Srrs */ 398163953Srrs struct sctp_inv_mandatory_param *mp; 399163953Srrs 400165647Srrs SCTP_BUF_LEN(op_err) = 401163953Srrs sizeof(struct sctp_inv_mandatory_param); 402163953Srrs mp = mtod(op_err, 403163953Srrs struct sctp_inv_mandatory_param *); 404163953Srrs /* Subtract the reserved param */ 405163953Srrs mp->length = 406163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 407163953Srrs mp->num_param = htonl(1); 408163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 409163953Srrs mp->resv = 0; 410163953Srrs } 411163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 412163953Srrs sh, op_err); 413163953Srrs } 414163953Srrs return (retval); 415163953Srrs } 416163953Srrs return (0); 417163953Srrs} 418163953Srrs 419163953Srrsstatic void 420163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 421163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 422163953Srrs{ 423163953Srrs struct sockaddr_storage store; 424163953Srrs struct sockaddr_in *sin; 425163953Srrs struct sockaddr_in6 *sin6; 426163953Srrs struct sctp_nets *r_net; 427163953Srrs struct timeval tv; 428163953Srrs 429163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 430163953Srrs /* Invalid length */ 431163953Srrs return; 432163953Srrs } 433163953Srrs sin = (struct sockaddr_in *)&store; 434163953Srrs sin6 = (struct sockaddr_in6 *)&store; 435163953Srrs 436163953Srrs memset(&store, 0, sizeof(store)); 437163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 438163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 439163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 440163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 441163953Srrs sin->sin_port = stcb->rport; 442163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 443163953Srrs sizeof(sin->sin_addr)); 444163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 445163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 446163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 447163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 448163953Srrs sin6->sin6_port = stcb->rport; 449163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 450163953Srrs sizeof(sin6->sin6_addr)); 451163953Srrs } else { 452163953Srrs return; 453163953Srrs } 454163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 455163953Srrs if (r_net == NULL) { 456163953Srrs#ifdef SCTP_DEBUG 457163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 458163953Srrs printf("Huh? I can't find the address I sent it to, discard\n"); 459163953Srrs } 460163953Srrs#endif 461163953Srrs return; 462163953Srrs } 463163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 464163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 465163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 466163953Srrs /* 467163953Srrs * If the its a HB and it's random value is correct when can 468163953Srrs * confirm the destination. 469163953Srrs */ 470163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 471167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 472167598Srrs stcb->asoc.primary_destination = r_net; 473167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 474167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 475167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 476167598Srrs if (r_net != stcb->asoc.primary_destination) { 477167598Srrs /* 478167598Srrs * first one on the list is NOT the primary 479167598Srrs * sctp_cmpaddr() is much more efficent if 480167598Srrs * the primary is the first on the list, 481167598Srrs * make it so. 482167598Srrs */ 483167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 484167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 485167598Srrs } 486167598Srrs } 487163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 488163953Srrs stcb, 0, (void *)r_net); 489163953Srrs } 490163953Srrs r_net->error_count = 0; 491163953Srrs r_net->hb_responded = 1; 492163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 493163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 494163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 495163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 496163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 497163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 498163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 499163953Srrs /* now was it the primary? if so restore */ 500163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 501163953Srrs sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 502163953Srrs } 503163953Srrs } 504163953Srrs /* Now lets do a RTO with this */ 505163953Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv); 506163953Srrs} 507163953Srrs 508163953Srrsstatic void 509163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 510163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 511163953Srrs{ 512163953Srrs 513163953Srrs#ifdef SCTP_DEBUG 514163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 515163953Srrs printf("sctp_handle_abort: handling ABORT\n"); 516163953Srrs } 517163953Srrs#endif 518163953Srrs if (stcb == NULL) 519163953Srrs return; 520163953Srrs /* verify that the destination addr is in the association */ 521163953Srrs /* ignore abort for addresses being deleted */ 522163953Srrs 523163953Srrs /* stop any receive timers */ 524165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 525163953Srrs /* notify user of the abort and clean up... */ 526163953Srrs sctp_abort_notification(stcb, 0); 527163953Srrs /* free the tcb */ 528163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 529163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 530163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 531163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 532163953Srrs } 533165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 534163953Srrs#ifdef SCTP_DEBUG 535163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 536163953Srrs printf("sctp_handle_abort: finished\n"); 537163953Srrs } 538163953Srrs#endif 539163953Srrs} 540163953Srrs 541163953Srrsstatic void 542163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 543163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 544163953Srrs{ 545163953Srrs struct sctp_association *asoc; 546163953Srrs int some_on_streamwheel; 547163953Srrs 548163953Srrs#ifdef SCTP_DEBUG 549163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 550163953Srrs printf("sctp_handle_shutdown: handling SHUTDOWN\n"); 551163953Srrs } 552163953Srrs#endif 553163953Srrs if (stcb == NULL) 554163953Srrs return; 555165220Srrs asoc = &stcb->asoc; 556165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 557165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 558163953Srrs return; 559163953Srrs } 560163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 561163953Srrs /* Shutdown NOT the expected size */ 562163953Srrs return; 563163953Srrs } else { 564163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 565163953Srrs } 566165220Srrs if (asoc->control_pdapi) { 567163953Srrs /* 568163953Srrs * With a normal shutdown we assume the end of last record. 569163953Srrs */ 570163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 571165220Srrs asoc->control_pdapi->end_added = 1; 572165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 573165220Srrs asoc->control_pdapi = NULL; 574163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 575163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 576163953Srrs } 577163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 578163953Srrs if (stcb->sctp_socket) { 579163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 580163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 581163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED; 582163953Srrs /* 583163953Srrs * notify upper layer that peer has initiated a 584163953Srrs * shutdown 585163953Srrs */ 586163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 587163953Srrs 588163953Srrs /* reset time */ 589163953Srrs SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 590163953Srrs } 591163953Srrs } 592163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 593163953Srrs /* 594163953Srrs * stop the shutdown timer, since we WILL move to 595163953Srrs * SHUTDOWN-ACK-SENT. 596163953Srrs */ 597165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 598163953Srrs } 599163953Srrs /* Now are we there yet? */ 600163953Srrs some_on_streamwheel = 0; 601163953Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 602163953Srrs /* Check to see if some data queued */ 603163953Srrs struct sctp_stream_out *outs; 604163953Srrs 605163953Srrs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) { 606163953Srrs if (!TAILQ_EMPTY(&outs->outqueue)) { 607163953Srrs some_on_streamwheel = 1; 608163953Srrs break; 609163953Srrs } 610163953Srrs } 611163953Srrs } 612163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 613163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 614163953Srrs some_on_streamwheel) { 615163953Srrs /* By returning we will push more data out */ 616163953Srrs return; 617163953Srrs } else { 618163953Srrs /* no outstanding data to send, so move on... */ 619163953Srrs /* send SHUTDOWN-ACK */ 620163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 621163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 622166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 623166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 624163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 625163953Srrs } 626163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT; 627163953Srrs 628163953Srrs /* start SHUTDOWN timer */ 629163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 630163953Srrs stcb, net); 631163953Srrs } 632163953Srrs} 633163953Srrs 634163953Srrsstatic void 635163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 636163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 637163953Srrs{ 638163953Srrs struct sctp_association *asoc; 639163953Srrs 640163953Srrs#ifdef SCTP_DEBUG 641163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 642163953Srrs printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 643163953Srrs } 644163953Srrs#endif 645163953Srrs if (stcb == NULL) 646163953Srrs return; 647163953Srrs 648163953Srrs asoc = &stcb->asoc; 649163953Srrs /* process according to association state */ 650163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 651163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 652163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 653163953Srrs SCTP_TCB_UNLOCK(stcb); 654163953Srrs return; 655163953Srrs } 656165220Srrs if (asoc->control_pdapi) { 657163953Srrs /* 658163953Srrs * With a normal shutdown we assume the end of last record. 659163953Srrs */ 660163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 661165220Srrs asoc->control_pdapi->end_added = 1; 662165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 663165220Srrs asoc->control_pdapi = NULL; 664163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 665163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 666163953Srrs } 667163953Srrs /* are the queues empty? */ 668163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 669163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 670163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 671164205Srrs sctp_report_all_outbound(stcb, 0); 672163953Srrs } 673163953Srrs /* stop the timer */ 674165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 675163953Srrs /* send SHUTDOWN-COMPLETE */ 676163953Srrs sctp_send_shutdown_complete(stcb, net); 677163953Srrs /* notify upper layer protocol */ 678163953Srrs if (stcb->sctp_socket) { 679163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 680163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 681163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 682163953Srrs /* Set the connected flag to disconnected */ 683163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 684163953Srrs } 685163953Srrs } 686163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 687163953Srrs /* free the TCB but first save off the ep */ 688165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 689165220Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 690163953Srrs} 691163953Srrs 692163953Srrs/* 693163953Srrs * Skip past the param header and then we will find the chunk that caused the 694163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 695163953Srrs * our peer must be broken. 696163953Srrs */ 697163953Srrsstatic void 698163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 699163953Srrs struct sctp_nets *net) 700163953Srrs{ 701163953Srrs struct sctp_chunkhdr *chk; 702163953Srrs 703163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 704163953Srrs switch (chk->chunk_type) { 705163953Srrs case SCTP_ASCONF_ACK: 706163953Srrs case SCTP_ASCONF: 707163953Srrs sctp_asconf_cleanup(stcb, net); 708163953Srrs break; 709163953Srrs case SCTP_FORWARD_CUM_TSN: 710163953Srrs stcb->asoc.peer_supports_prsctp = 0; 711163953Srrs break; 712163953Srrs default: 713163953Srrs#ifdef SCTP_DEBUG 714163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 715163953Srrs printf("Peer does not support chunk type %d(%x)??\n", 716163953Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 717163953Srrs } 718163953Srrs#endif 719163953Srrs break; 720163953Srrs } 721163953Srrs} 722163953Srrs 723163953Srrs/* 724163953Srrs * Skip past the param header and then we will find the param that caused the 725163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 726163953Srrs * these will turn of specific features. 727163953Srrs */ 728163953Srrsstatic void 729163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 730163953Srrs{ 731163953Srrs struct sctp_paramhdr *pbad; 732163953Srrs 733163953Srrs pbad = phdr + 1; 734163953Srrs switch (ntohs(pbad->param_type)) { 735163953Srrs /* pr-sctp draft */ 736163953Srrs case SCTP_PRSCTP_SUPPORTED: 737163953Srrs stcb->asoc.peer_supports_prsctp = 0; 738163953Srrs break; 739163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 740163953Srrs break; 741163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 742163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 743163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 744163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 745163953Srrs stcb->asoc.ecn_allowed = 0; 746163953Srrs break; 747163953Srrs case SCTP_ADD_IP_ADDRESS: 748163953Srrs case SCTP_DEL_IP_ADDRESS: 749163953Srrs case SCTP_SET_PRIM_ADDR: 750163953Srrs stcb->asoc.peer_supports_asconf = 0; 751163953Srrs break; 752163953Srrs case SCTP_SUCCESS_REPORT: 753163953Srrs case SCTP_ERROR_CAUSE_IND: 754163953Srrs#ifdef SCTP_DEBUG 755163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 756163953Srrs printf("Huh, the peer does not support success? or error cause?\n"); 757163953Srrs printf("Turning off ASCONF to this strange peer\n"); 758163953Srrs } 759163953Srrs#endif 760163953Srrs stcb->asoc.peer_supports_asconf = 0; 761163953Srrs break; 762163953Srrs default: 763163953Srrs#ifdef SCTP_DEBUG 764163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 765163953Srrs printf("Peer does not support param type %d(%x)??\n", 766163953Srrs pbad->param_type, (uint32_t) pbad->param_type); 767163953Srrs } 768163953Srrs#endif 769163953Srrs break; 770163953Srrs } 771163953Srrs} 772163953Srrs 773163953Srrsstatic int 774163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 775163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 776163953Srrs{ 777163953Srrs int chklen; 778163953Srrs struct sctp_paramhdr *phdr; 779163953Srrs uint16_t error_type; 780163953Srrs uint16_t error_len; 781163953Srrs struct sctp_association *asoc; 782163953Srrs 783163953Srrs int adjust; 784163953Srrs 785163953Srrs /* parse through all of the errors and process */ 786163953Srrs asoc = &stcb->asoc; 787163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 788163953Srrs sizeof(struct sctp_chunkhdr)); 789163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 790163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 791163953Srrs /* Process an Error Cause */ 792163953Srrs error_type = ntohs(phdr->param_type); 793163953Srrs error_len = ntohs(phdr->param_length); 794163953Srrs if ((error_len > chklen) || (error_len == 0)) { 795163953Srrs /* invalid param length for this param */ 796163953Srrs#ifdef SCTP_DEBUG 797163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 798163953Srrs printf("Bogus length in error param- chunk left:%d errorlen:%d\n", 799163953Srrs chklen, error_len); 800163953Srrs } 801163953Srrs#endif /* SCTP_DEBUG */ 802163953Srrs return (0); 803163953Srrs } 804163953Srrs switch (error_type) { 805163953Srrs case SCTP_CAUSE_INVALID_STREAM: 806163953Srrs case SCTP_CAUSE_MISSING_PARAM: 807163953Srrs case SCTP_CAUSE_INVALID_PARAM: 808163953Srrs case SCTP_CAUSE_NO_USER_DATA: 809163953Srrs#ifdef SCTP_DEBUG 810163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 811163953Srrs printf("Software error we got a %d back? We have a bug :/ (or do they?)\n", 812163953Srrs error_type); 813163953Srrs } 814163953Srrs#endif 815163953Srrs break; 816163953Srrs case SCTP_CAUSE_STALE_COOKIE: 817163953Srrs /* 818163953Srrs * We only act if we have echoed a cookie and are 819163953Srrs * waiting. 820163953Srrs */ 821163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 822163953Srrs int *p; 823163953Srrs 824163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 825163953Srrs /* Save the time doubled */ 826163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 827163953Srrs asoc->stale_cookie_count++; 828163953Srrs if (asoc->stale_cookie_count > 829163953Srrs asoc->max_init_times) { 830163953Srrs sctp_abort_notification(stcb, 0); 831163953Srrs /* now free the asoc */ 832165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 833163953Srrs return (-1); 834163953Srrs } 835163953Srrs /* blast back to INIT state */ 836163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 837163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 838163953Srrs 839163953Srrs sctp_stop_all_cookie_timers(stcb); 840163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 841163953Srrs } 842163953Srrs break; 843163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 844163953Srrs /* 845163953Srrs * Nothing we can do here, we don't do hostname 846163953Srrs * addresses so if the peer does not like my IPv6 847163953Srrs * (or IPv4 for that matter) it does not matter. If 848163953Srrs * they don't support that type of address, they can 849163953Srrs * NOT possibly get that packet type... i.e. with no 850163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 851163953Srrs * safely ignore this one. If we ever added support 852163953Srrs * for HOSTNAME Addresses, then we would need to do 853163953Srrs * something here. 854163953Srrs */ 855163953Srrs break; 856163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 857163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 858163953Srrs break; 859163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 860163953Srrs sctp_process_unrecog_param(stcb, phdr); 861163953Srrs break; 862163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 863163953Srrs /* 864163953Srrs * We ignore this since the timer will drive out a 865163953Srrs * new cookie anyway and there timer will drive us 866163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 867163953Srrs * here since we don't have their tag. 868163953Srrs */ 869163953Srrs break; 870163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 871163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 872163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 873163953Srrs /* 874163953Srrs * We should NOT get these here, but in a 875166086Srrs * ASCONF-ACK. 876163953Srrs */ 877163953Srrs#ifdef SCTP_DEBUG 878163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 879163953Srrs printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n", 880163953Srrs error_type); 881163953Srrs } 882163953Srrs#endif 883163953Srrs break; 884163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 885163953Srrs /* 886163953Srrs * And what, pray tell do we do with the fact that 887163953Srrs * the peer is out of resources? Not really sure we 888166086Srrs * could do anything but abort. I suspect this 889163953Srrs * should have came WITH an abort instead of in a 890163953Srrs * OP-ERROR. 891163953Srrs */ 892163953Srrs break; 893163953Srrs default: 894163953Srrs#ifdef SCTP_DEBUG 895163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 896163953Srrs /* don't know what this error cause is... */ 897163953Srrs printf("sctp_handle_error: unknown error type = 0x%xh\n", 898163953Srrs error_type); 899163953Srrs } 900163953Srrs#endif /* SCTP_DEBUG */ 901163953Srrs break; 902163953Srrs } 903163953Srrs adjust = SCTP_SIZE32(error_len); 904163953Srrs chklen -= adjust; 905163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 906163953Srrs } 907163953Srrs return (0); 908163953Srrs} 909163953Srrs 910163953Srrsstatic int 911163953Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 912163953Srrs struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 913163953Srrs struct sctp_nets *net) 914163953Srrs{ 915163953Srrs struct sctp_init_ack *init_ack; 916163953Srrs int *state; 917163953Srrs struct mbuf *op_err; 918163953Srrs 919163953Srrs#ifdef SCTP_DEBUG 920163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 921163953Srrs printf("sctp_handle_init_ack: handling INIT-ACK\n"); 922163953Srrs } 923163953Srrs#endif 924163953Srrs if (stcb == NULL) { 925163953Srrs#ifdef SCTP_DEBUG 926163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 927163953Srrs printf("sctp_handle_init_ack: TCB is null\n"); 928163953Srrs } 929163953Srrs#endif 930163953Srrs return (-1); 931163953Srrs } 932163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 933163953Srrs /* Invalid length */ 934163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 935163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 936163953Srrs op_err); 937163953Srrs return (-1); 938163953Srrs } 939163953Srrs init_ack = &cp->init; 940163953Srrs /* validate parameters */ 941163953Srrs if (init_ack->initiate_tag == 0) { 942163953Srrs /* protocol error... send an abort */ 943163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 944163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 945163953Srrs op_err); 946163953Srrs return (-1); 947163953Srrs } 948163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 949163953Srrs /* protocol error... send an abort */ 950163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 951163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 952163953Srrs op_err); 953163953Srrs return (-1); 954163953Srrs } 955163953Srrs if (init_ack->num_inbound_streams == 0) { 956163953Srrs /* protocol error... send an abort */ 957163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 958163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 959163953Srrs op_err); 960163953Srrs return (-1); 961163953Srrs } 962163953Srrs if (init_ack->num_outbound_streams == 0) { 963163953Srrs /* protocol error... send an abort */ 964163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 965163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 966163953Srrs op_err); 967163953Srrs return (-1); 968163953Srrs } 969163953Srrs /* process according to association state... */ 970163953Srrs state = &stcb->asoc.state; 971163953Srrs switch (*state & SCTP_STATE_MASK) { 972163953Srrs case SCTP_STATE_COOKIE_WAIT: 973163953Srrs /* this is the expected state for this chunk */ 974163953Srrs /* process the INIT-ACK parameters */ 975163953Srrs if (stcb->asoc.primary_destination->dest_state & 976163953Srrs SCTP_ADDR_UNCONFIRMED) { 977163953Srrs /* 978163953Srrs * The primary is where we sent the INIT, we can 979163953Srrs * always consider it confirmed when the INIT-ACK is 980163953Srrs * returned. Do this before we load addresses 981163953Srrs * though. 982163953Srrs */ 983163953Srrs stcb->asoc.primary_destination->dest_state &= 984163953Srrs ~SCTP_ADDR_UNCONFIRMED; 985163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 986163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 987163953Srrs } 988163953Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net 989163953Srrs ) < 0) { 990163953Srrs /* error in parsing parameters */ 991163953Srrs return (-1); 992163953Srrs } 993163953Srrs /* update our state */ 994163953Srrs#ifdef SCTP_DEBUG 995163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 996163953Srrs printf("moving to COOKIE-ECHOED state\n"); 997163953Srrs } 998163953Srrs#endif 999163953Srrs if (*state & SCTP_STATE_SHUTDOWN_PENDING) { 1000163953Srrs *state = SCTP_STATE_COOKIE_ECHOED | 1001163953Srrs SCTP_STATE_SHUTDOWN_PENDING; 1002163953Srrs } else { 1003163953Srrs *state = SCTP_STATE_COOKIE_ECHOED; 1004163953Srrs } 1005163953Srrs 1006163953Srrs /* reset the RTO calc */ 1007163953Srrs stcb->asoc.overall_error_count = 0; 1008163953Srrs SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1009163953Srrs /* 1010163953Srrs * collapse the init timer back in case of a exponential 1011165220Srrs * backoff 1012163953Srrs */ 1013163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1014163953Srrs stcb, net); 1015163953Srrs /* 1016163953Srrs * the send at the end of the inbound data processing will 1017163953Srrs * cause the cookie to be sent 1018163953Srrs */ 1019163953Srrs break; 1020163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1021163953Srrs /* incorrect state... discard */ 1022163953Srrs break; 1023163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1024163953Srrs /* incorrect state... discard */ 1025163953Srrs break; 1026163953Srrs case SCTP_STATE_OPEN: 1027163953Srrs /* incorrect state... discard */ 1028163953Srrs break; 1029163953Srrs case SCTP_STATE_EMPTY: 1030163953Srrs case SCTP_STATE_INUSE: 1031163953Srrs default: 1032163953Srrs /* incorrect state... discard */ 1033163953Srrs return (-1); 1034163953Srrs break; 1035163953Srrs } /* end switch asoc state */ 1036163953Srrs#ifdef SCTP_DEBUG 1037163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1038163953Srrs printf("Leaving handle-init-ack end\n"); 1039163953Srrs } 1040163953Srrs#endif 1041163953Srrs return (0); 1042163953Srrs} 1043163953Srrs 1044163953Srrs 1045163953Srrs/* 1046163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1047163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1048163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1049163953Srrs * mbuf to the cookie-echo chunk 1050163953Srrs */ 1051163953Srrsstatic struct sctp_tcb * 1052163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1053163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1054163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1055163953Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id) 1056163953Srrs{ 1057163953Srrs struct sctp_association *asoc; 1058163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1059163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1060163953Srrs int chk_length; 1061165220Srrs int init_offset, initack_offset, i; 1062163953Srrs int retval; 1063164205Srrs int spec_flag = 0; 1064166023Srrs int how_indx; 1065163953Srrs 1066163953Srrs /* I know that the TCB is non-NULL from the caller */ 1067163953Srrs asoc = &stcb->asoc; 1068166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1069166023Srrs if (asoc->cookie_how[how_indx] == 0) 1070166023Srrs break; 1071166023Srrs } 1072166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1073166023Srrs asoc->cookie_how[how_indx] = 1; 1074166023Srrs } 1075163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1076163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1077163953Srrs struct mbuf *op_err; 1078163953Srrs struct sctp_paramhdr *ph; 1079163953Srrs 1080163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1081163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1082165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1083163953Srrs if (op_err == NULL) { 1084163953Srrs /* FOOBAR */ 1085163953Srrs return (NULL); 1086163953Srrs } 1087163953Srrs /* pre-reserve some space */ 1088165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1089165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1090165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1091163953Srrs /* Set the len */ 1092165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1093163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1094163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1095163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1096163953Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag); 1097166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1098166023Srrs asoc->cookie_how[how_indx] = 2; 1099163953Srrs return (NULL); 1100163953Srrs } 1101163953Srrs /* 1102163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1103163953Srrs * INIT should start after the cookie-echo header struct (chunk 1104163953Srrs * header, state cookie header struct) 1105163953Srrs */ 1106163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1107163953Srrs 1108163953Srrs init_cp = (struct sctp_init_chunk *) 1109163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1110163953Srrs (uint8_t *) & init_buf); 1111163953Srrs if (init_cp == NULL) { 1112163953Srrs /* could not pull a INIT chunk in cookie */ 1113163953Srrs return (NULL); 1114163953Srrs } 1115163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1116163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1117163953Srrs return (NULL); 1118163953Srrs } 1119163953Srrs /* 1120163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1121163953Srrs * INIT-ACK follows the INIT chunk 1122163953Srrs */ 1123163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1124163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1125163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1126163953Srrs (uint8_t *) & initack_buf); 1127163953Srrs if (initack_cp == NULL) { 1128163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1129163953Srrs return (NULL); 1130163953Srrs } 1131163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1132163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1133163953Srrs return (NULL); 1134163953Srrs } 1135163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1136163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1137163953Srrs /* 1138163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1139163953Srrs * to get into the OPEN state 1140163953Srrs */ 1141166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1142166086Srrs#ifdef INVARIANTS 1143166023Srrs panic("Case D and non-match seq?"); 1144166086Srrs#else 1145166086Srrs printf("Case D, seq non-match %x vs %x?\n", 1146166086Srrs ntohl(initack_cp->init.initial_tsn), asoc->init_seq_number); 1147166086Srrs#endif 1148166023Srrs } 1149163953Srrs switch SCTP_GET_STATE 1150163953Srrs (asoc) { 1151163953Srrs case SCTP_STATE_COOKIE_WAIT: 1152166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1153163953Srrs /* 1154163953Srrs * INIT was sent, but got got a COOKIE_ECHO with the 1155166023Srrs * correct tags... just accept it...but we must 1156166023Srrs * process the init so that we can make sure we have 1157166023Srrs * the right seq no's. 1158163953Srrs */ 1159163953Srrs /* First we must process the INIT !! */ 1160163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1161163953Srrs if (retval < 0) { 1162166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1163166023Srrs asoc->cookie_how[how_indx] = 3; 1164163953Srrs return (NULL); 1165163953Srrs } 1166163953Srrs /* we have already processed the INIT so no problem */ 1167163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1168165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1169165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1170163953Srrs /* update current state */ 1171166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1172166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1173166675Srrs else 1174166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1175163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1176166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1177163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1178163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1179163953Srrs 1180166675Srrs } else { 1181163953Srrs /* if ok, move to OPEN state */ 1182163953Srrs asoc->state = SCTP_STATE_OPEN; 1183163953Srrs } 1184166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1185165220Srrs sctp_stop_all_cookie_timers(stcb); 1186163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1187163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1188163953Srrs (inp->sctp_socket->so_qlimit == 0) 1189163953Srrs ) { 1190163953Srrs /* 1191163953Srrs * Here is where collision would go if we 1192163953Srrs * did a connect() and instead got a 1193163953Srrs * init/init-ack/cookie done before the 1194163953Srrs * init-ack came back.. 1195163953Srrs */ 1196163953Srrs stcb->sctp_ep->sctp_flags |= 1197163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1198163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1199163953Srrs } 1200163953Srrs /* notify upper layer */ 1201163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1202163953Srrs /* 1203163953Srrs * since we did not send a HB make sure we don't 1204163953Srrs * double things 1205163953Srrs */ 1206163953Srrs net->hb_responded = 1; 1207163953Srrs 1208163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1209163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1210163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1211163953Srrs inp, stcb, NULL); 1212163953Srrs } 1213163953Srrs break; 1214163953Srrs default: 1215163953Srrs /* 1216163953Srrs * we're in the OPEN state (or beyond), so peer must 1217163953Srrs * have simply lost the COOKIE-ACK 1218163953Srrs */ 1219166675Srrs 1220163953Srrs break; 1221163953Srrs } /* end switch */ 1222165220Srrs sctp_stop_all_cookie_timers(stcb); 1223163953Srrs /* 1224163953Srrs * We ignore the return code here.. not sure if we should 1225163953Srrs * somehow abort.. but we do have an existing asoc. This 1226163953Srrs * really should not fail. 1227163953Srrs */ 1228163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1229163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1230163953Srrs initack_offset, sh, init_src)) { 1231166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1232166023Srrs asoc->cookie_how[how_indx] = 4; 1233163953Srrs return (NULL); 1234163953Srrs } 1235163953Srrs /* respond with a COOKIE-ACK */ 1236165220Srrs sctp_toss_old_cookies(stcb, asoc); 1237163953Srrs sctp_send_cookie_ack(stcb); 1238166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1239166023Srrs asoc->cookie_how[how_indx] = 5; 1240163953Srrs return (stcb); 1241163953Srrs } /* end if */ 1242163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1243163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1244163953Srrs cookie->tie_tag_my_vtag == 0 && 1245163953Srrs cookie->tie_tag_peer_vtag == 0) { 1246163953Srrs /* 1247163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1248163953Srrs */ 1249166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1250166023Srrs asoc->cookie_how[how_indx] = 6; 1251163953Srrs return (NULL); 1252163953Srrs } 1253163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1254163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1255163953Srrs init_cp->init.initiate_tag == 0)) { 1256163953Srrs /* 1257163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1258163953Srrs * should be ok, re-accept peer info 1259163953Srrs */ 1260166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1261166023Srrs /* 1262166023Srrs * Extension of case C. If we hit this, then the 1263166023Srrs * random number generator returned the same vtag 1264166023Srrs * when we first sent our INIT-ACK and when we later 1265166023Srrs * sent our INIT. The side with the seq numbers that 1266166023Srrs * are different will be the one that normnally 1267166023Srrs * would have hit case C. This in effect "extends" 1268166023Srrs * our vtags in this collision case to be 64 bits. 1269166023Srrs * The same collision could occur aka you get both 1270166023Srrs * vtag and seq number the same twice in a row.. but 1271166023Srrs * is much less likely. If it did happen then we 1272166023Srrs * would proceed through and bring up the assoc.. we 1273166023Srrs * may end up with the wrong stream setup however.. 1274166023Srrs * which would be bad.. but there is no way to 1275166023Srrs * tell.. until we send on a stream that does not 1276166023Srrs * exist :-) 1277166023Srrs */ 1278166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1279166023Srrs asoc->cookie_how[how_indx] = 7; 1280166023Srrs 1281166023Srrs return (NULL); 1282166023Srrs } 1283166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1284166023Srrs asoc->cookie_how[how_indx] = 8; 1285165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1286163953Srrs sctp_stop_all_cookie_timers(stcb); 1287163953Srrs /* 1288163953Srrs * since we did not send a HB make sure we don't double 1289163953Srrs * things 1290163953Srrs */ 1291163953Srrs net->hb_responded = 1; 1292163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1293163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1294163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1295163953Srrs NULL); 1296163953Srrs } 1297163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1298164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1299164205Srrs 1300164205Srrs /* Note last_cwr_tsn? where is this used? */ 1301163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1302164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1303164205Srrs /* 1304164205Srrs * Ok the peer probably discarded our data (if we 1305164205Srrs * echoed a cookie+data). So anything on the 1306164205Srrs * sent_queue should be marked for retransmit, we 1307164205Srrs * may not get something to kick us so it COULD 1308164205Srrs * still take a timeout to move these.. but it can't 1309164205Srrs * hurt to mark them. 1310164205Srrs */ 1311164205Srrs struct sctp_tmit_chunk *chk; 1312163953Srrs 1313164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1314164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1315164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1316168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1317164205Srrs spec_flag++; 1318164205Srrs } 1319164205Srrs } 1320164205Srrs 1321164205Srrs } 1322163953Srrs /* process the INIT info (peer's info) */ 1323163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1324163953Srrs if (retval < 0) { 1325166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1326166023Srrs asoc->cookie_how[how_indx] = 9; 1327163953Srrs return (NULL); 1328163953Srrs } 1329163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1330163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1331163953Srrs initack_offset, sh, init_src)) { 1332166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1333166023Srrs asoc->cookie_how[how_indx] = 10; 1334163953Srrs return (NULL); 1335163953Srrs } 1336163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1337163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1338163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1339163953Srrs 1340163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1341163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1342163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1343163953Srrs stcb->sctp_ep->sctp_flags |= 1344163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1345163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1346163953Srrs } 1347166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1348166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1349166675Srrs else 1350166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1351166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1352166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1353166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1354166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1355166675Srrs } else { 1356166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1357163953Srrs } 1358163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1359166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1360163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1361163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1362163953Srrs 1363163953Srrs } else { 1364163953Srrs asoc->state = SCTP_STATE_OPEN; 1365163953Srrs } 1366163953Srrs sctp_stop_all_cookie_timers(stcb); 1367165220Srrs sctp_toss_old_cookies(stcb, asoc); 1368163953Srrs sctp_send_cookie_ack(stcb); 1369164205Srrs if (spec_flag) { 1370164205Srrs /* 1371164205Srrs * only if we have retrans set do we do this. What 1372164205Srrs * this call does is get only the COOKIE-ACK out and 1373164205Srrs * then when we return the normal call to 1374164205Srrs * sctp_chunk_output will get the retrans out behind 1375164205Srrs * this. 1376164205Srrs */ 1377164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1378164205Srrs } 1379166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1380166023Srrs asoc->cookie_how[how_indx] = 11; 1381166023Srrs 1382163953Srrs return (stcb); 1383163953Srrs } 1384163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1385163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1386163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1387163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1388163953Srrs cookie->tie_tag_peer_vtag != 0) { 1389163953Srrs struct sctpasochead *head; 1390163953Srrs 1391163953Srrs /* 1392163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1393163953Srrs */ 1394165220Srrs /* temp code */ 1395166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1396166023Srrs asoc->cookie_how[how_indx] = 12; 1397165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1398165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1399165647Srrs 1400163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1401163953Srrs /* notify upper layer */ 1402163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1403165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1404166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1405166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1406166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1407166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1408166675Srrs } 1409166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1410166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1411166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1412166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1413166675Srrs } 1414165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1415165647Srrs asoc->state = SCTP_STATE_OPEN | 1416165647Srrs SCTP_STATE_SHUTDOWN_PENDING; 1417165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1418165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1419165647Srrs 1420165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1421165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1422165647Srrs asoc->state = SCTP_STATE_OPEN; 1423165647Srrs } 1424165647Srrs asoc->pre_open_streams = 1425165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1426165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1427165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1428165647Srrs 1429165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1430165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1431165647Srrs 1432165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1433165647Srrs 1434165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1435165647Srrs if (asoc->mapping_array) 1436165647Srrs memset(asoc->mapping_array, 0, 1437165647Srrs asoc->mapping_array_size); 1438165220Srrs SCTP_TCB_UNLOCK(stcb); 1439165220Srrs SCTP_INP_INFO_WLOCK(); 1440165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1441165220Srrs SCTP_TCB_LOCK(stcb); 1442165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1443163953Srrs /* send up all the data */ 1444164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1445165220Srrs 1446164205Srrs sctp_report_all_outbound(stcb, 1); 1447165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1448165220Srrs stcb->asoc.strmout[i].stream_no = i; 1449165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1450165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1451165220Srrs } 1452163953Srrs /* process the INIT-ACK info (my info) */ 1453163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1454163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1455163953Srrs 1456163953Srrs /* pull from vtag hash */ 1457163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1458163953Srrs /* re-insert to new vtag position */ 1459163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1460163953Srrs sctppcbinfo.hashasocmark)]; 1461163953Srrs /* 1462163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1463163953Srrs * system 1464163953Srrs */ 1465163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1466163953Srrs 1467163953Srrs /* Is this the first restart? */ 1468163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1469163953Srrs /* Ok add it to assoc_id vtag hash */ 1470163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1471163953Srrs sctppcbinfo.hashrestartmark)]; 1472163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1473163953Srrs stcb->asoc.in_restart_hash = 1; 1474163953Srrs } 1475163953Srrs /* process the INIT info (peer's info) */ 1476164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1477165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1478165220Srrs SCTP_INP_INFO_WUNLOCK(); 1479165220Srrs 1480163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1481163953Srrs if (retval < 0) { 1482166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1483166023Srrs asoc->cookie_how[how_indx] = 13; 1484166023Srrs 1485163953Srrs return (NULL); 1486163953Srrs } 1487163953Srrs /* 1488163953Srrs * since we did not send a HB make sure we don't double 1489163953Srrs * things 1490163953Srrs */ 1491163953Srrs net->hb_responded = 1; 1492163953Srrs 1493163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1494163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1495163953Srrs initack_offset, sh, init_src)) { 1496166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1497166023Srrs asoc->cookie_how[how_indx] = 14; 1498166023Srrs 1499163953Srrs return (NULL); 1500163953Srrs } 1501163953Srrs /* respond with a COOKIE-ACK */ 1502163953Srrs sctp_stop_all_cookie_timers(stcb); 1503165220Srrs sctp_toss_old_cookies(stcb, asoc); 1504163953Srrs sctp_send_cookie_ack(stcb); 1505166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1506166023Srrs asoc->cookie_how[how_indx] = 15; 1507163953Srrs 1508163953Srrs return (stcb); 1509163953Srrs } 1510166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1511166023Srrs asoc->cookie_how[how_indx] = 16; 1512163953Srrs /* all other cases... */ 1513163953Srrs return (NULL); 1514163953Srrs} 1515163953Srrs 1516166086Srrs 1517163953Srrs/* 1518163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1519163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1520163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1521163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1522163953Srrs * was from returns a new TCB 1523163953Srrs */ 1524163953Srrsstatic struct sctp_tcb * 1525163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1526163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1527163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1528163953Srrs struct sockaddr *init_src, int *notification, 1529163953Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len) 1530163953Srrs{ 1531163953Srrs struct sctp_tcb *stcb; 1532163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1533163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1534163953Srrs struct sockaddr_storage sa_store; 1535163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1536163953Srrs struct sockaddr_in *sin; 1537163953Srrs struct sockaddr_in6 *sin6; 1538163953Srrs struct sctp_association *asoc; 1539167598Srrs uint32_t vrf; 1540163953Srrs int chk_length; 1541163953Srrs int init_offset, initack_offset, initack_limit; 1542163953Srrs int retval; 1543163953Srrs int error = 0; 1544163953Srrs uint32_t old_tag; 1545166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1546163953Srrs 1547167598Srrs vrf = SCTP_DEFAULT_VRFID; 1548167598Srrs 1549163953Srrs /* 1550163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1551163953Srrs * INIT should start after the cookie-echo header struct (chunk 1552163953Srrs * header, state cookie header struct) 1553163953Srrs */ 1554163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1555163953Srrs init_cp = (struct sctp_init_chunk *) 1556163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1557163953Srrs (uint8_t *) & init_buf); 1558163953Srrs if (init_cp == NULL) { 1559163953Srrs /* could not pull a INIT chunk in cookie */ 1560163953Srrs#ifdef SCTP_DEBUG 1561163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1562163953Srrs printf("process_cookie_new: could not pull INIT chunk hdr\n"); 1563163953Srrs } 1564163953Srrs#endif /* SCTP_DEBUG */ 1565163953Srrs return (NULL); 1566163953Srrs } 1567163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1568163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1569163953Srrs#ifdef SCTP_DEBUG 1570163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1571163953Srrs printf("HUH? process_cookie_new: could not find INIT chunk!\n"); 1572163953Srrs } 1573163953Srrs#endif /* SCTP_DEBUG */ 1574163953Srrs return (NULL); 1575163953Srrs } 1576163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1577163953Srrs /* 1578163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1579163953Srrs * INIT-ACK follows the INIT chunk 1580163953Srrs */ 1581163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1582163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1583163953Srrs (uint8_t *) & initack_buf); 1584163953Srrs if (initack_cp == NULL) { 1585163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1586163953Srrs#ifdef SCTP_DEBUG 1587163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1588163953Srrs printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1589163953Srrs } 1590163953Srrs#endif /* SCTP_DEBUG */ 1591163953Srrs return (NULL); 1592163953Srrs } 1593163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1594163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1595163953Srrs return (NULL); 1596163953Srrs } 1597163953Srrs /* 1598163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1599163953Srrs * "initack_limit" value. This is because the chk_length field 1600163953Srrs * includes the length of the cookie, but the cookie is omitted when 1601163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1602163953Srrs */ 1603163953Srrs initack_limit = offset + cookie_len; 1604163953Srrs 1605163953Srrs /* 1606163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1607163953Srrs * and popluate 1608163953Srrs */ 1609163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1610167598Srrs ntohl(initack_cp->init.initiate_tag), vrf); 1611163953Srrs if (stcb == NULL) { 1612163953Srrs struct mbuf *op_err; 1613163953Srrs 1614163953Srrs /* memory problem? */ 1615163953Srrs#ifdef SCTP_DEBUG 1616163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1617163953Srrs printf("process_cookie_new: no room for another TCB!\n"); 1618163953Srrs } 1619163953Srrs#endif /* SCTP_DEBUG */ 1620163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1621163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1622163953Srrs sh, op_err); 1623163953Srrs return (NULL); 1624163953Srrs } 1625163953Srrs /* get the correct sctp_nets */ 1626163953Srrs *netp = sctp_findnet(stcb, init_src); 1627163953Srrs asoc = &stcb->asoc; 1628163953Srrs /* get scope variables out of cookie */ 1629163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1630163953Srrs asoc->site_scope = cookie->site_scope; 1631163953Srrs asoc->local_scope = cookie->local_scope; 1632163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1633163953Srrs 1634163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1635163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1636163953Srrs struct mbuf *op_err; 1637163953Srrs 1638163953Srrs /* 1639163953Srrs * Houston we have a problem. The EP changed while the 1640163953Srrs * cookie was in flight. Only recourse is to abort the 1641163953Srrs * association. 1642163953Srrs */ 1643163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1644163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1645163953Srrs sh, op_err); 1646163953Srrs return (NULL); 1647163953Srrs } 1648163953Srrs /* process the INIT-ACK info (my info) */ 1649163953Srrs old_tag = asoc->my_vtag; 1650163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1651163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1652163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1653163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1654163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1655163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1656163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1657163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1658163953Srrs 1659163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1660163953Srrs 1661163953Srrs /* process the INIT info (peer's info) */ 1662163953Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1663163953Srrs if (retval < 0) { 1664165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1665163953Srrs return (NULL); 1666163953Srrs } 1667163953Srrs /* load all addresses */ 1668163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1669163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1670163953Srrs init_src)) { 1671165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1672163953Srrs return (NULL); 1673163953Srrs } 1674163953Srrs /* 1675163953Srrs * verify any preceding AUTH chunk that was skipped 1676163953Srrs */ 1677163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1678163953Srrs sctp_auth_get_cookie_params(stcb, m, 1679163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1680163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1681163953Srrs if (auth_skipped) { 1682163953Srrs struct sctp_auth_chunk *auth; 1683163953Srrs 1684163953Srrs auth = (struct sctp_auth_chunk *) 1685166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1686163953Srrs if (sctp_handle_auth(stcb, auth, m, auth_offset)) { 1687163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1688163953Srrs#ifdef SCTP_DEBUG 1689163953Srrs if (sctp_debug_on & SCTP_DEBUG_AUTH1) 1690163953Srrs printf("COOKIE-ECHO: AUTH failed\n"); 1691163953Srrs#endif /* SCTP_DEBUG */ 1692165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1693163953Srrs return (NULL); 1694163953Srrs } else { 1695163953Srrs /* remaining chunks checked... good to go */ 1696163953Srrs stcb->asoc.authenticated = 1; 1697163953Srrs } 1698163953Srrs } 1699163953Srrs /* update current state */ 1700163953Srrs#ifdef SCTP_DEBUG 1701163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1702163953Srrs printf("moving to OPEN state\n"); 1703163953Srrs } 1704163953Srrs#endif 1705163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1706163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1707163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1708163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1709163953Srrs } else { 1710163953Srrs asoc->state = SCTP_STATE_OPEN; 1711163953Srrs } 1712165220Srrs sctp_stop_all_cookie_timers(stcb); 1713163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1714163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1715163953Srrs 1716163953Srrs /* 1717163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1718163953Srrs * addresses that need to get added to the peer (eg. addresses 1719163953Srrs * changed while cookie echo in flight). This needs to be done 1720163953Srrs * after we go to the OPEN state to do the correct asconf 1721163953Srrs * processing. else, make sure we have the correct addresses in our 1722163953Srrs * lists 1723163953Srrs */ 1724163953Srrs 1725163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1726163953Srrs /* pull in local_address (our "from" address) */ 1727163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1728163953Srrs /* source addr is IPv4 */ 1729163953Srrs sin = (struct sockaddr_in *)initack_src; 1730163953Srrs memset(sin, 0, sizeof(*sin)); 1731163953Srrs sin->sin_family = AF_INET; 1732163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1733163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1734163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1735163953Srrs /* source addr is IPv6 */ 1736163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1737163953Srrs memset(sin6, 0, sizeof(*sin6)); 1738163953Srrs sin6->sin6_family = AF_INET6; 1739163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1740163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1741163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1742163953Srrs sizeof(sin6->sin6_addr)); 1743163953Srrs } else { 1744165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1745163953Srrs return (NULL); 1746163953Srrs } 1747163953Srrs 1748163953Srrs sctp_check_address_list(stcb, m, 1749163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1750163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1751163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1752163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1753163953Srrs 1754163953Srrs 1755163953Srrs /* set up to notify upper layer */ 1756163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1757163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1758163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1759163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1760163953Srrs /* 1761163953Srrs * This is an endpoint that called connect() how it got a 1762163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1763163953Srrs * the INIT was sent, but before it got there.. a complete 1764163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1765163953Srrs * should have went to the other code.. not here.. oh well.. 1766163953Srrs * a bit of protection is worth having.. 1767163953Srrs */ 1768163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1769163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1770163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1771163953Srrs (inp->sctp_socket->so_qlimit)) { 1772163953Srrs /* 1773163953Srrs * We don't want to do anything with this one. Since it is 1774163953Srrs * the listening guy. The timer will get started for 1775163953Srrs * accepted connections in the caller. 1776163953Srrs */ 1777163953Srrs ; 1778163953Srrs } 1779163953Srrs /* since we did not send a HB make sure we don't double things */ 1780163953Srrs (*netp)->hb_responded = 1; 1781163953Srrs 1782163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1783163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1784163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1785163953Srrs } 1786163953Srrs /* respond with a COOKIE-ACK */ 1787165220Srrs /* calculate the RTT */ 1788165220Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1789165220Srrs &cookie->time_entered); 1790163953Srrs sctp_send_cookie_ack(stcb); 1791163953Srrs return (stcb); 1792163953Srrs} 1793163953Srrs 1794163953Srrs 1795163953Srrs/* 1796163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1797163953Srrs * existing (non-NULL) TCB 1798163953Srrs */ 1799163953Srrsstatic struct mbuf * 1800163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1801163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1802163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1803165220Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, struct sctp_tcb **locked_tcb) 1804163953Srrs{ 1805163953Srrs struct sctp_state_cookie *cookie; 1806163953Srrs struct sockaddr_in6 sin6; 1807163953Srrs struct sockaddr_in sin; 1808163953Srrs struct sctp_tcb *l_stcb = *stcb; 1809163953Srrs struct sctp_inpcb *l_inp; 1810163953Srrs struct sockaddr *to; 1811163953Srrs sctp_assoc_t sac_restart_id; 1812163953Srrs struct sctp_pcb *ep; 1813163953Srrs struct mbuf *m_sig; 1814163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1815163953Srrs uint8_t *sig; 1816163953Srrs uint8_t cookie_ok = 0; 1817163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1818163953Srrs unsigned int cookie_len; 1819163953Srrs struct timeval now; 1820163953Srrs struct timeval time_expires; 1821163953Srrs struct sockaddr_storage dest_store; 1822163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1823163953Srrs struct ip *iph; 1824163953Srrs int notification = 0; 1825163953Srrs struct sctp_nets *netl; 1826163953Srrs int had_a_existing_tcb = 0; 1827163953Srrs 1828163953Srrs#ifdef SCTP_DEBUG 1829163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 1830163953Srrs printf("sctp_handle_cookie: handling COOKIE-ECHO\n"); 1831163953Srrs } 1832163953Srrs#endif 1833163953Srrs 1834163953Srrs if (inp_p == NULL) { 1835163953Srrs return (NULL); 1836163953Srrs } 1837163953Srrs /* First get the destination address setup too. */ 1838163953Srrs iph = mtod(m, struct ip *); 1839163953Srrs if (iph->ip_v == IPVERSION) { 1840163953Srrs /* its IPv4 */ 1841163953Srrs struct sockaddr_in *sin; 1842163953Srrs 1843163953Srrs sin = (struct sockaddr_in *)(localep_sa); 1844163953Srrs memset(sin, 0, sizeof(*sin)); 1845163953Srrs sin->sin_family = AF_INET; 1846163953Srrs sin->sin_len = sizeof(*sin); 1847163953Srrs sin->sin_port = sh->dest_port; 1848163953Srrs sin->sin_addr.s_addr = iph->ip_dst.s_addr; 1849165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1850163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1851163953Srrs /* its IPv6 */ 1852163953Srrs struct ip6_hdr *ip6; 1853163953Srrs struct sockaddr_in6 *sin6; 1854163953Srrs 1855163953Srrs sin6 = (struct sockaddr_in6 *)(localep_sa); 1856163953Srrs memset(sin6, 0, sizeof(*sin6)); 1857163953Srrs sin6->sin6_family = AF_INET6; 1858163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1859163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1860163953Srrs sin6->sin6_port = sh->dest_port; 1861163953Srrs sin6->sin6_addr = ip6->ip6_dst; 1862166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1863163953Srrs } else { 1864163953Srrs return (NULL); 1865163953Srrs } 1866163953Srrs 1867163953Srrs cookie = &cp->cookie; 1868163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1869163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1870163953Srrs 1871163953Srrs if ((cookie->peerport != sh->src_port) && 1872163953Srrs (cookie->myport != sh->dest_port) && 1873163953Srrs (cookie->my_vtag != sh->v_tag)) { 1874163953Srrs /* 1875163953Srrs * invalid ports or bad tag. Note that we always leave the 1876163953Srrs * v_tag in the header in network order and when we stored 1877163953Srrs * it in the my_vtag slot we also left it in network order. 1878163953Srrs * This maintians the match even though it may be in the 1879163953Srrs * opposite byte order of the machine :-> 1880163953Srrs */ 1881163953Srrs return (NULL); 1882163953Srrs } 1883163953Srrs if (cookie_len > size_of_pkt || 1884163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1885163953Srrs sizeof(struct sctp_init_chunk) + 1886163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1887163953Srrs /* cookie too long! or too small */ 1888163953Srrs return (NULL); 1889163953Srrs } 1890163953Srrs /* 1891163953Srrs * split off the signature into its own mbuf (since it should not be 1892163953Srrs * calculated in the sctp_hmac_m() call). 1893163953Srrs */ 1894163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1895163953Srrs if (sig_offset > size_of_pkt) { 1896163953Srrs /* packet not correct size! */ 1897163953Srrs /* XXX this may already be accounted for earlier... */ 1898163953Srrs return (NULL); 1899163953Srrs } 1900163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1901163953Srrs if (m_sig == NULL) { 1902163953Srrs /* out of memory or ?? */ 1903163953Srrs return (NULL); 1904163953Srrs } 1905163953Srrs /* 1906163953Srrs * compute the signature/digest for the cookie 1907163953Srrs */ 1908163953Srrs ep = &(*inp_p)->sctp_ep; 1909163953Srrs l_inp = *inp_p; 1910163953Srrs if (l_stcb) { 1911163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1912163953Srrs } 1913163953Srrs SCTP_INP_RLOCK(l_inp); 1914163953Srrs if (l_stcb) { 1915163953Srrs SCTP_TCB_LOCK(l_stcb); 1916163953Srrs } 1917163953Srrs /* which cookie is it? */ 1918163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1919163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1920163953Srrs /* it's the old cookie */ 1921163953Srrs sctp_hmac_m(SCTP_HMAC, 1922163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1923163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1924163953Srrs } else { 1925163953Srrs /* it's the current cookie */ 1926163953Srrs sctp_hmac_m(SCTP_HMAC, 1927163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1928163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1929163953Srrs } 1930163953Srrs /* get the signature */ 1931163953Srrs SCTP_INP_RUNLOCK(l_inp); 1932163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1933163953Srrs if (sig == NULL) { 1934163953Srrs /* couldn't find signature */ 1935164085Srrs sctp_m_freem(m_sig); 1936163953Srrs return (NULL); 1937163953Srrs } 1938163953Srrs /* compare the received digest with the computed digest */ 1939163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1940163953Srrs /* try the old cookie? */ 1941163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1942163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1943163953Srrs /* compute digest with old */ 1944163953Srrs sctp_hmac_m(SCTP_HMAC, 1945163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1946163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1947163953Srrs /* compare */ 1948163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1949163953Srrs cookie_ok = 1; 1950163953Srrs } 1951163953Srrs } else { 1952163953Srrs cookie_ok = 1; 1953163953Srrs } 1954163953Srrs 1955163953Srrs /* 1956163953Srrs * Now before we continue we must reconstruct our mbuf so that 1957163953Srrs * normal processing of any other chunks will work. 1958163953Srrs */ 1959163953Srrs { 1960163953Srrs struct mbuf *m_at; 1961163953Srrs 1962163953Srrs m_at = m; 1963165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1964165647Srrs m_at = SCTP_BUF_NEXT(m_at); 1965163953Srrs } 1966165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 1967163953Srrs } 1968163953Srrs 1969163953Srrs if (cookie_ok == 0) { 1970163953Srrs#ifdef SCTP_DEBUG 1971163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 1972163953Srrs printf("handle_cookie_echo: cookie signature validation failed!\n"); 1973163953Srrs printf("offset = %u, cookie_offset = %u, sig_offset = %u\n", 1974163953Srrs (uint32_t) offset, cookie_offset, sig_offset); 1975163953Srrs } 1976163953Srrs#endif 1977163953Srrs return (NULL); 1978163953Srrs } 1979163953Srrs /* 1980163953Srrs * check the cookie timestamps to be sure it's not stale 1981163953Srrs */ 1982163953Srrs SCTP_GETTIME_TIMEVAL(&now); 1983163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 1984163953Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life; 1985163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 1986163953Srrs if (timevalcmp(&now, &time_expires, >)) { 1987163953Srrs /* cookie is stale! */ 1988163953Srrs struct mbuf *op_err; 1989163953Srrs struct sctp_stale_cookie_msg *scm; 1990163953Srrs uint32_t tim; 1991163953Srrs 1992163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 1993165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1994163953Srrs if (op_err == NULL) { 1995163953Srrs /* FOOBAR */ 1996163953Srrs return (NULL); 1997163953Srrs } 1998163953Srrs /* pre-reserve some space */ 1999165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2000165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2001165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2002163953Srrs 2003163953Srrs /* Set the len */ 2004165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2005163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2006163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2007163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2008163953Srrs (sizeof(uint32_t)))); 2009163953Srrs /* seconds to usec */ 2010163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2011163953Srrs /* add in usec */ 2012163953Srrs if (tim == 0) 2013163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2014163953Srrs scm->time_usec = htonl(tim); 2015163953Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag); 2016163953Srrs return (NULL); 2017163953Srrs } 2018163953Srrs /* 2019163953Srrs * Now we must see with the lookup address if we have an existing 2020163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2021163953Srrs * and a INIT collided with us and somewhere the peer sent the 2022163953Srrs * cookie on another address besides the single address our assoc 2023163953Srrs * had for him. In this case we will have one of the tie-tags set at 2024163953Srrs * least AND the address field in the cookie can be used to look it 2025163953Srrs * up. 2026163953Srrs */ 2027163953Srrs to = NULL; 2028163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2029163953Srrs memset(&sin6, 0, sizeof(sin6)); 2030163953Srrs sin6.sin6_family = AF_INET6; 2031163953Srrs sin6.sin6_len = sizeof(sin6); 2032163953Srrs sin6.sin6_port = sh->src_port; 2033163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2034163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2035163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2036163953Srrs to = (struct sockaddr *)&sin6; 2037163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2038163953Srrs memset(&sin, 0, sizeof(sin)); 2039163953Srrs sin.sin_family = AF_INET; 2040163953Srrs sin.sin_len = sizeof(sin); 2041163953Srrs sin.sin_port = sh->src_port; 2042163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2043163953Srrs to = (struct sockaddr *)&sin; 2044163953Srrs } 2045163953Srrs if ((*stcb == NULL) && to) { 2046163953Srrs /* Yep, lets check */ 2047163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2048163953Srrs if (*stcb == NULL) { 2049163953Srrs /* 2050163953Srrs * We should have only got back the same inp. If we 2051163953Srrs * got back a different ep we have a problem. The 2052163953Srrs * original findep got back l_inp and now 2053163953Srrs */ 2054163953Srrs if (l_inp != *inp_p) { 2055163953Srrs printf("Bad problem find_ep got a diff inp then special_locate?\n"); 2056163953Srrs } 2057165220Srrs } else { 2058165220Srrs if (*locked_tcb == NULL) { 2059165220Srrs /* 2060165220Srrs * In this case we found the assoc only 2061165220Srrs * after we locked the create lock. This 2062165220Srrs * means we are in a colliding case and we 2063165220Srrs * must make sure that we unlock the tcb if 2064165220Srrs * its one of the cases where we throw away 2065165220Srrs * the incoming packets. 2066165220Srrs */ 2067165220Srrs *locked_tcb = *stcb; 2068165220Srrs 2069165220Srrs /* 2070165220Srrs * We must also increment the inp ref count 2071165220Srrs * since the ref_count flags was set when we 2072165220Srrs * did not find the TCB, now we found it 2073165220Srrs * which reduces the refcount.. we must 2074165220Srrs * raise it back out to balance it all :-) 2075165220Srrs */ 2076165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2077165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2078165220Srrs printf("Huh? ep:%p diff then l_inp:%p?\n", 2079165220Srrs (*stcb)->sctp_ep, l_inp); 2080165220Srrs } 2081165220Srrs } 2082163953Srrs } 2083163953Srrs } 2084163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2085163953Srrs if (*stcb == NULL) { 2086163953Srrs /* this is the "normal" case... get a new TCB */ 2087163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2088163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2089163953Srrs auth_skipped, auth_offset, auth_len); 2090163953Srrs } else { 2091163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2092163953Srrs had_a_existing_tcb = 1; 2093163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2094163953Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification, 2095163953Srrs &sac_restart_id); 2096163953Srrs } 2097163953Srrs 2098163953Srrs if (*stcb == NULL) { 2099163953Srrs /* still no TCB... must be bad cookie-echo */ 2100163953Srrs return (NULL); 2101163953Srrs } 2102163953Srrs /* 2103163953Srrs * Ok, we built an association so confirm the address we sent the 2104163953Srrs * INIT-ACK to. 2105163953Srrs */ 2106163953Srrs netl = sctp_findnet(*stcb, to); 2107163953Srrs /* 2108163953Srrs * This code should in theory NOT run but 2109163953Srrs */ 2110163953Srrs if (netl == NULL) { 2111163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2112163953Srrs int ret; 2113163953Srrs 2114165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2115165220Srrs SCTP_IN_COOKIE_PROC); 2116163953Srrs netl = sctp_findnet(*stcb, to); 2117163953Srrs } 2118163953Srrs if (netl) { 2119163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2120163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2121163953Srrs sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2122163953Srrs netl); 2123163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2124163953Srrs (*stcb), 0, (void *)netl); 2125163953Srrs } 2126163953Srrs } 2127163953Srrs if (*stcb) { 2128163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2129163953Srrs *stcb, NULL); 2130163953Srrs } 2131163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2132163953Srrs if (!had_a_existing_tcb || 2133163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2134163953Srrs /* 2135163953Srrs * If we have a NEW cookie or the connect never 2136163953Srrs * reached the connected state during collision we 2137163953Srrs * must do the TCP accept thing. 2138163953Srrs */ 2139163953Srrs struct socket *so, *oso; 2140163953Srrs struct sctp_inpcb *inp; 2141163953Srrs 2142163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2143163953Srrs /* 2144163953Srrs * For a restart we will keep the same 2145163953Srrs * socket, no need to do anything. I THINK!! 2146163953Srrs */ 2147163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2148163953Srrs return (m); 2149163953Srrs } 2150163953Srrs oso = (*inp_p)->sctp_socket; 2151163953Srrs /* 2152163953Srrs * We do this to keep the sockets side happy durin 2153163953Srrs * the sonewcon ONLY. 2154163953Srrs */ 2155163953Srrs NET_LOCK_GIANT(); 2156163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2157166086Srrs so = sonewconn(oso, 0 2158163953Srrs ); 2159163953Srrs NET_UNLOCK_GIANT(); 2160163953Srrs SCTP_INP_WLOCK((*stcb)->sctp_ep); 2161163953Srrs SCTP_TCB_LOCK((*stcb)); 2162163953Srrs SCTP_INP_WUNLOCK((*stcb)->sctp_ep); 2163163953Srrs if (so == NULL) { 2164163953Srrs struct mbuf *op_err; 2165163953Srrs 2166163953Srrs /* Too many sockets */ 2167163953Srrs#ifdef SCTP_DEBUG 2168163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 2169163953Srrs printf("process_cookie_new: no room for another socket!\n"); 2170163953Srrs } 2171163953Srrs#endif /* SCTP_DEBUG */ 2172163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2173163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2174163953Srrs sh, op_err); 2175165220Srrs sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2176163953Srrs return (NULL); 2177163953Srrs } 2178163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2179166086Srrs SCTP_INP_INCR_REF(inp); 2180166086Srrs /* 2181166086Srrs * We add the unbound flag here so that if we get an 2182166086Srrs * soabort() before we get the move_pcb done, we 2183166086Srrs * will properly cleanup. 2184166086Srrs */ 2185163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2186163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2187163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2188166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2189163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2190163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2191163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2192163953Srrs inp->sctp_socket = so; 2193163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2194163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2195163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2196163953Srrs inp->inp_starting_point_for_iterator = NULL; 2197163953Srrs /* 2198163953Srrs * copy in the authentication parameters from the 2199163953Srrs * original endpoint 2200163953Srrs */ 2201163953Srrs if (inp->sctp_ep.local_hmacs) 2202163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2203163953Srrs inp->sctp_ep.local_hmacs = 2204163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2205163953Srrs if (inp->sctp_ep.local_auth_chunks) 2206163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2207163953Srrs inp->sctp_ep.local_auth_chunks = 2208163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2209163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2210163953Srrs &inp->sctp_ep.shared_keys); 2211163953Srrs 2212163953Srrs /* 2213163953Srrs * Now we must move it from one hash table to 2214163953Srrs * another and get the tcb in the right place. 2215163953Srrs */ 2216163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2217163953Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb); 2218163953Srrs 2219166086Srrs /* 2220166086Srrs * now we must check to see if we were aborted while 2221166086Srrs * the move was going on and the lock/unlock 2222166086Srrs * happened. 2223166086Srrs */ 2224166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2225166086Srrs /* 2226166086Srrs * yep it was, we leave the assoc attached 2227166086Srrs * to the socket since the sctp_inpcb_free() 2228166086Srrs * call will send an abort for us. 2229166086Srrs */ 2230166086Srrs SCTP_INP_DECR_REF(inp); 2231166086Srrs return (NULL); 2232166086Srrs } 2233166086Srrs SCTP_INP_DECR_REF(inp); 2234163953Srrs /* Switch over to the new guy */ 2235163953Srrs *inp_p = inp; 2236166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2237163953Srrs 2238166086Srrs /* 2239166086Srrs * Pull it from the incomplete queue and wake the 2240166086Srrs * guy 2241166086Srrs */ 2242166086Srrs soisconnected(so); 2243163953Srrs return (m); 2244163953Srrs } 2245163953Srrs } 2246163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2247163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2248163953Srrs } 2249163953Srrs return (m); 2250163953Srrs} 2251163953Srrs 2252163953Srrsstatic void 2253163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2254163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2255163953Srrs{ 2256163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2257163953Srrs struct sctp_association *asoc; 2258163953Srrs 2259163953Srrs#ifdef SCTP_DEBUG 2260163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2261163953Srrs printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2262163953Srrs } 2263163953Srrs#endif 2264163953Srrs if (stcb == NULL) 2265163953Srrs return; 2266163953Srrs 2267163953Srrs asoc = &stcb->asoc; 2268163953Srrs 2269163953Srrs sctp_stop_all_cookie_timers(stcb); 2270163953Srrs /* process according to association state */ 2271163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2272163953Srrs /* state change only needed when I am in right state */ 2273163953Srrs#ifdef SCTP_DEBUG 2274163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2275163953Srrs printf("moving to OPEN state\n"); 2276163953Srrs } 2277163953Srrs#endif 2278163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2279163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 2280163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2281163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2282163953Srrs 2283163953Srrs } else { 2284163953Srrs asoc->state = SCTP_STATE_OPEN; 2285163953Srrs } 2286163953Srrs /* update RTO */ 2287163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2288163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2289163953Srrs if (asoc->overall_error_count == 0) { 2290163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2291163953Srrs &asoc->time_entered); 2292163953Srrs } 2293163953Srrs SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2294163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2295163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2296163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2297163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2298163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2299163953Srrs } 2300163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2301163953Srrs stcb, net); 2302163953Srrs /* 2303163953Srrs * since we did not send a HB make sure we don't double 2304163953Srrs * things 2305163953Srrs */ 2306163953Srrs net->hb_responded = 1; 2307163953Srrs 2308163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2309163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2310163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2311163953Srrs stcb->sctp_ep, stcb, NULL); 2312163953Srrs } 2313163953Srrs /* 2314163953Srrs * set ASCONF timer if ASCONFs are pending and allowed (eg. 2315163953Srrs * addresses changed when init/cookie echo in flight) 2316163953Srrs */ 2317163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2318163953Srrs (stcb->asoc.peer_supports_asconf) && 2319163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2320163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2321163953Srrs stcb->sctp_ep, stcb, 2322163953Srrs stcb->asoc.primary_destination); 2323163953Srrs } 2324163953Srrs } 2325163953Srrs /* Toss the cookie if I can */ 2326163953Srrs sctp_toss_old_cookies(stcb, asoc); 2327163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2328163953Srrs /* Restart the timer if we have pending data */ 2329163953Srrs struct sctp_tmit_chunk *chk; 2330163953Srrs 2331163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2332163953Srrs if (chk) { 2333163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2334163953Srrs stcb, chk->whoTo); 2335163953Srrs } 2336163953Srrs } 2337163953Srrs} 2338163953Srrs 2339163953Srrsstatic void 2340163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2341163953Srrs struct sctp_tcb *stcb) 2342163953Srrs{ 2343163953Srrs struct sctp_nets *net; 2344163953Srrs struct sctp_tmit_chunk *lchk; 2345163953Srrs uint32_t tsn; 2346163953Srrs 2347163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2348163953Srrs return; 2349163953Srrs } 2350163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2351163953Srrs tsn = ntohl(cp->tsn); 2352163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2353163953Srrs /* Also we make sure we disable the nonce_wait */ 2354163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2355163953Srrs if (lchk == NULL) { 2356163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2357163953Srrs } else { 2358163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2359163953Srrs } 2360163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2361163953Srrs stcb->asoc.nonce_sum_check = 0; 2362163953Srrs 2363163953Srrs /* Find where it was sent, if possible */ 2364163953Srrs net = NULL; 2365163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2366163953Srrs while (lchk) { 2367163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2368163953Srrs net = lchk->whoTo; 2369163953Srrs break; 2370163953Srrs } 2371163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2372163953Srrs break; 2373163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2374163953Srrs } 2375163953Srrs if (net == NULL) 2376163953Srrs /* default is we use the primary */ 2377163953Srrs net = stcb->asoc.primary_destination; 2378163953Srrs 2379163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2380163953Srrs#ifdef SCTP_CWND_MONITOR 2381163953Srrs int old_cwnd; 2382163953Srrs 2383163953Srrs old_cwnd = net->cwnd; 2384163953Srrs#endif 2385163953Srrs SCTP_STAT_INCR(sctps_ecnereducedcwnd); 2386163953Srrs net->ssthresh = net->cwnd / 2; 2387163953Srrs if (net->ssthresh < net->mtu) { 2388163953Srrs net->ssthresh = net->mtu; 2389163953Srrs /* here back off the timer as well, to slow us down */ 2390163953Srrs net->RTO <<= 2; 2391163953Srrs } 2392163953Srrs net->cwnd = net->ssthresh; 2393163953Srrs#ifdef SCTP_CWND_MONITOR 2394163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT); 2395163953Srrs#endif 2396163953Srrs /* 2397163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2398163953Srrs * the next sending seq i.e. the resync_tsn. 2399163953Srrs */ 2400163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2401163953Srrs } 2402163953Srrs /* 2403163953Srrs * We always send a CWR this way if our previous one was lost our 2404163953Srrs * peer will get an update, or if it is not time again to reduce we 2405163953Srrs * still get the cwr to the peer. 2406163953Srrs */ 2407163953Srrs sctp_send_cwr(stcb, net, tsn); 2408163953Srrs} 2409163953Srrs 2410163953Srrsstatic void 2411163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2412163953Srrs{ 2413163953Srrs /* 2414163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2415163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2416163953Srrs * If so remove it. 2417163953Srrs */ 2418163953Srrs struct sctp_tmit_chunk *chk; 2419163953Srrs struct sctp_ecne_chunk *ecne; 2420163953Srrs 2421163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2422163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2423163953Srrs continue; 2424163953Srrs } 2425163953Srrs /* 2426163953Srrs * Look for and remove if it is the right TSN. Since there 2427163953Srrs * is only ONE ECNE on the control queue at any one time we 2428163953Srrs * don't need to worry about more than one! 2429163953Srrs */ 2430163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2431163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2432163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2433163953Srrs /* this covers this ECNE, we can remove it */ 2434163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2435163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2436163953Srrs sctp_next); 2437163953Srrs if (chk->data) { 2438163953Srrs sctp_m_freem(chk->data); 2439163953Srrs chk->data = NULL; 2440163953Srrs } 2441163953Srrs stcb->asoc.ctrl_queue_cnt--; 2442163953Srrs sctp_free_remote_addr(chk->whoTo); 2443163953Srrs sctp_free_a_chunk(stcb, chk); 2444163953Srrs break; 2445163953Srrs } 2446163953Srrs } 2447163953Srrs} 2448163953Srrs 2449163953Srrsstatic void 2450163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2451163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2452163953Srrs{ 2453163953Srrs struct sctp_association *asoc; 2454163953Srrs 2455163953Srrs#ifdef SCTP_DEBUG 2456163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2457163953Srrs printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2458163953Srrs } 2459163953Srrs#endif 2460163953Srrs if (stcb == NULL) 2461163953Srrs return; 2462163953Srrs 2463163953Srrs asoc = &stcb->asoc; 2464163953Srrs /* process according to association state */ 2465163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2466163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2467163953Srrs SCTP_TCB_UNLOCK(stcb); 2468163953Srrs return; 2469163953Srrs } 2470163953Srrs /* notify upper layer protocol */ 2471163953Srrs if (stcb->sctp_socket) { 2472163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2473163953Srrs /* are the queues empty? they should be */ 2474163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2475163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2476163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2477164205Srrs sctp_report_all_outbound(stcb, 0); 2478163953Srrs } 2479163953Srrs } 2480163953Srrs /* stop the timer */ 2481165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_21); 2482163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2483163953Srrs /* free the TCB */ 2484165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2485163953Srrs return; 2486163953Srrs} 2487163953Srrs 2488163953Srrsstatic int 2489163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2490163953Srrs struct sctp_nets *net, uint8_t flg) 2491163953Srrs{ 2492163953Srrs switch (desc->chunk_type) { 2493163953Srrs case SCTP_DATA: 2494163953Srrs /* find the tsn to resend (possibly */ 2495163953Srrs { 2496163953Srrs uint32_t tsn; 2497163953Srrs struct sctp_tmit_chunk *tp1; 2498163953Srrs 2499163953Srrs tsn = ntohl(desc->tsn_ifany); 2500163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2501163953Srrs while (tp1) { 2502163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2503163953Srrs /* found it */ 2504163953Srrs break; 2505163953Srrs } 2506163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2507163953Srrs MAX_TSN)) { 2508163953Srrs /* not found */ 2509163953Srrs tp1 = NULL; 2510163953Srrs break; 2511163953Srrs } 2512163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2513163953Srrs } 2514163953Srrs if (tp1 == NULL) { 2515163953Srrs /* 2516163953Srrs * Do it the other way , aka without paying 2517163953Srrs * attention to queue seq order. 2518163953Srrs */ 2519163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2520163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2521163953Srrs while (tp1) { 2522163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2523163953Srrs /* found it */ 2524163953Srrs break; 2525163953Srrs } 2526163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2527163953Srrs } 2528163953Srrs } 2529163953Srrs if (tp1 == NULL) { 2530163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2531163953Srrs } 2532163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2533163953Srrs uint8_t *ddp; 2534163953Srrs 2535163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2536163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2537163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2538163953Srrs return (0); 2539163953Srrs } 2540163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2541163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2542163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2543163953Srrs return (0); 2544163953Srrs } 2545163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2546163953Srrs sizeof(struct sctp_data_chunk)); 2547163953Srrs { 2548163953Srrs unsigned int iii; 2549163953Srrs 2550163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2551163953Srrs iii++) { 2552163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2553163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2554163953Srrs return (-1); 2555163953Srrs } 2556163953Srrs } 2557163953Srrs } 2558163953Srrs /* 2559163953Srrs * We zero out the nonce so resync not 2560163953Srrs * needed 2561163953Srrs */ 2562163953Srrs tp1->rec.data.ect_nonce = 0; 2563163953Srrs 2564163953Srrs if (tp1->do_rtt) { 2565163953Srrs /* 2566163953Srrs * this guy had a RTO calculation 2567163953Srrs * pending on it, cancel it 2568163953Srrs */ 2569163953Srrs tp1->do_rtt = 0; 2570163953Srrs } 2571163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2572163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2573163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2574163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2575163953Srrs /* 2576163953Srrs * mark it as if we were doing a FR, since 2577163953Srrs * we will be getting gap ack reports behind 2578163953Srrs * the info from the router. 2579163953Srrs */ 2580163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2581163953Srrs /* 2582163953Srrs * mark the tsn with what sequences can 2583163953Srrs * cause a new FR. 2584163953Srrs */ 2585163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2586163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2587163953Srrs } else { 2588163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2589163953Srrs } 2590163953Srrs 2591163953Srrs /* restart the timer */ 2592163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2593165220Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2594163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2595163953Srrs stcb, tp1->whoTo); 2596163953Srrs 2597163953Srrs /* fix counts and things */ 2598165220Srrs#ifdef SCTP_FLIGHT_LOGGING 2599165220Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN, 2600165220Srrs tp1->whoTo->flight_size, 2601165220Srrs tp1->book_size, 2602165220Srrs (uintptr_t) stcb, 2603165220Srrs tp1->rec.data.TSN_seq); 2604165220Srrs#endif 2605163953Srrs if (tp1->whoTo->flight_size >= tp1->book_size) 2606163953Srrs tp1->whoTo->flight_size -= tp1->book_size; 2607163953Srrs else 2608163953Srrs tp1->whoTo->flight_size = 0; 2609163953Srrs 2610163953Srrs if (stcb->asoc.total_flight >= tp1->book_size) { 2611163953Srrs stcb->asoc.total_flight -= tp1->book_size; 2612163953Srrs if (stcb->asoc.total_flight_count > 0) 2613163953Srrs stcb->asoc.total_flight_count--; 2614163953Srrs } else { 2615163953Srrs stcb->asoc.total_flight = 0; 2616163953Srrs stcb->asoc.total_flight_count = 0; 2617163953Srrs } 2618163953Srrs tp1->snd_count--; 2619163953Srrs } { 2620163953Srrs /* audit code */ 2621163953Srrs unsigned int audit; 2622163953Srrs 2623163953Srrs audit = 0; 2624163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2625163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2626163953Srrs audit++; 2627163953Srrs } 2628163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2629163953Srrs sctp_next) { 2630163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2631163953Srrs audit++; 2632163953Srrs } 2633163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2634163953Srrs printf("**Local Audit finds cnt:%d asoc cnt:%d\n", 2635163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2636163953Srrs#ifndef SCTP_AUDITING_ENABLED 2637163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2638163953Srrs#endif 2639163953Srrs } 2640163953Srrs } 2641163953Srrs } 2642163953Srrs break; 2643163953Srrs case SCTP_ASCONF: 2644163953Srrs { 2645163953Srrs struct sctp_tmit_chunk *asconf; 2646163953Srrs 2647163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2648163953Srrs sctp_next) { 2649163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2650163953Srrs break; 2651163953Srrs } 2652163953Srrs } 2653163953Srrs if (asconf) { 2654163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2655163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2656163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2657163953Srrs asconf->snd_count--; 2658163953Srrs } 2659163953Srrs } 2660163953Srrs break; 2661163953Srrs case SCTP_INITIATION: 2662163953Srrs /* resend the INIT */ 2663163953Srrs stcb->asoc.dropped_special_cnt++; 2664163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2665163953Srrs /* 2666163953Srrs * If we can get it in, in a few attempts we do 2667163953Srrs * this, otherwise we let the timer fire. 2668163953Srrs */ 2669163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2670165220Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2671163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2672163953Srrs } 2673163953Srrs break; 2674163953Srrs case SCTP_SELECTIVE_ACK: 2675163953Srrs /* resend the sack */ 2676163953Srrs sctp_send_sack(stcb); 2677163953Srrs break; 2678163953Srrs case SCTP_HEARTBEAT_REQUEST: 2679163953Srrs /* resend a demand HB */ 2680163953Srrs sctp_send_hb(stcb, 1, net); 2681163953Srrs break; 2682163953Srrs case SCTP_SHUTDOWN: 2683163953Srrs sctp_send_shutdown(stcb, net); 2684163953Srrs break; 2685163953Srrs case SCTP_SHUTDOWN_ACK: 2686163953Srrs sctp_send_shutdown_ack(stcb, net); 2687163953Srrs break; 2688163953Srrs case SCTP_COOKIE_ECHO: 2689163953Srrs { 2690163953Srrs struct sctp_tmit_chunk *cookie; 2691163953Srrs 2692163953Srrs cookie = NULL; 2693163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2694163953Srrs sctp_next) { 2695163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2696163953Srrs break; 2697163953Srrs } 2698163953Srrs } 2699163953Srrs if (cookie) { 2700163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2701163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2702163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2703163953Srrs sctp_stop_all_cookie_timers(stcb); 2704163953Srrs } 2705163953Srrs } 2706163953Srrs break; 2707163953Srrs case SCTP_COOKIE_ACK: 2708163953Srrs sctp_send_cookie_ack(stcb); 2709163953Srrs break; 2710163953Srrs case SCTP_ASCONF_ACK: 2711163953Srrs /* resend last asconf ack */ 2712163953Srrs sctp_send_asconf_ack(stcb, 1); 2713163953Srrs break; 2714163953Srrs case SCTP_FORWARD_CUM_TSN: 2715163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2716163953Srrs break; 2717163953Srrs /* can't do anything with these */ 2718163953Srrs case SCTP_PACKET_DROPPED: 2719163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2720163953Srrs case SCTP_HEARTBEAT_ACK: 2721163953Srrs case SCTP_ABORT_ASSOCIATION: 2722163953Srrs case SCTP_OPERATION_ERROR: 2723163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2724163953Srrs case SCTP_ECN_ECHO: 2725163953Srrs case SCTP_ECN_CWR: 2726163953Srrs default: 2727163953Srrs break; 2728163953Srrs } 2729163953Srrs return (0); 2730163953Srrs} 2731163953Srrs 2732163953Srrsvoid 2733163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2734163953Srrs{ 2735163953Srrs int i; 2736163953Srrs uint16_t temp; 2737163953Srrs 2738163953Srrs /* 2739163953Srrs * We set things to 0xffff since this is the last delivered sequence 2740163953Srrs * and we will be sending in 0 after the reset. 2741163953Srrs */ 2742163953Srrs 2743163953Srrs if (number_entries) { 2744163953Srrs for (i = 0; i < number_entries; i++) { 2745163953Srrs temp = ntohs(list[i]); 2746163953Srrs if (temp >= stcb->asoc.streamincnt) { 2747163953Srrs continue; 2748163953Srrs } 2749163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2750163953Srrs } 2751163953Srrs } else { 2752163953Srrs list = NULL; 2753163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2754163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2755163953Srrs } 2756163953Srrs } 2757163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2758163953Srrs} 2759163953Srrs 2760163953Srrsstatic void 2761163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2762163953Srrs{ 2763163953Srrs int i; 2764163953Srrs 2765163953Srrs if (number_entries == 0) { 2766163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2767163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2768163953Srrs } 2769163953Srrs } else if (number_entries) { 2770163953Srrs for (i = 0; i < number_entries; i++) { 2771163953Srrs uint16_t temp; 2772163953Srrs 2773163953Srrs temp = ntohs(list[i]); 2774163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2775163953Srrs /* no such stream */ 2776163953Srrs continue; 2777163953Srrs } 2778163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2779163953Srrs } 2780163953Srrs } 2781163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2782163953Srrs} 2783163953Srrs 2784163953Srrs 2785163953Srrsstruct sctp_stream_reset_out_request * 2786163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2787163953Srrs{ 2788163953Srrs struct sctp_association *asoc; 2789163953Srrs struct sctp_stream_reset_out_req *req; 2790163953Srrs struct sctp_stream_reset_out_request *r; 2791163953Srrs struct sctp_tmit_chunk *chk; 2792163953Srrs int len, clen; 2793163953Srrs 2794163953Srrs asoc = &stcb->asoc; 2795163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2796163953Srrs return (NULL); 2797163953Srrs } 2798163953Srrs if (stcb->asoc.str_reset == NULL) { 2799163953Srrs return (NULL); 2800163953Srrs } 2801163953Srrs chk = stcb->asoc.str_reset; 2802163953Srrs if (chk->data == NULL) { 2803163953Srrs return (NULL); 2804163953Srrs } 2805163953Srrs if (bchk) { 2806163953Srrs /* he wants a copy of the chk pointer */ 2807163953Srrs *bchk = chk; 2808163953Srrs } 2809163953Srrs clen = chk->send_size; 2810163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2811163953Srrs r = &req->sr_req; 2812163953Srrs if (ntohl(r->request_seq) == seq) { 2813163953Srrs /* found it */ 2814163953Srrs return (r); 2815163953Srrs } 2816163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2817163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2818163953Srrs /* move to the next one, there can only be a max of two */ 2819163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2820163953Srrs if (ntohl(r->request_seq) == seq) { 2821163953Srrs return (r); 2822163953Srrs } 2823163953Srrs } 2824163953Srrs /* that seq is not here */ 2825163953Srrs return (NULL); 2826163953Srrs} 2827163953Srrs 2828163953Srrsstatic void 2829163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2830163953Srrs{ 2831163953Srrs struct sctp_association *asoc; 2832163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2833163953Srrs 2834163953Srrs if (stcb->asoc.str_reset == NULL) { 2835163953Srrs return; 2836163953Srrs } 2837166675Srrs asoc = &stcb->asoc; 2838166675Srrs 2839165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2840163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2841163953Srrs chk, 2842163953Srrs sctp_next); 2843163953Srrs if (chk->data) { 2844163953Srrs sctp_m_freem(chk->data); 2845163953Srrs chk->data = NULL; 2846163953Srrs } 2847163953Srrs asoc->ctrl_queue_cnt--; 2848163953Srrs sctp_free_remote_addr(chk->whoTo); 2849163953Srrs 2850163953Srrs sctp_free_a_chunk(stcb, chk); 2851163953Srrs stcb->asoc.str_reset = NULL; 2852163953Srrs} 2853163953Srrs 2854163953Srrs 2855163953Srrsstatic int 2856163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2857163953Srrs uint32_t seq, uint32_t action, 2858164139Srrs struct sctp_stream_reset_response *respin) 2859163953Srrs{ 2860163953Srrs uint16_t type; 2861163953Srrs int lparm_len; 2862163953Srrs struct sctp_association *asoc = &stcb->asoc; 2863163953Srrs struct sctp_tmit_chunk *chk; 2864163953Srrs struct sctp_stream_reset_out_request *srparam; 2865163953Srrs int number_entries; 2866163953Srrs 2867163953Srrs if (asoc->stream_reset_outstanding == 0) { 2868163953Srrs /* duplicate */ 2869163953Srrs return (0); 2870163953Srrs } 2871163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2872163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2873163953Srrs if (srparam) { 2874163953Srrs stcb->asoc.str_reset_seq_out++; 2875163953Srrs type = ntohs(srparam->ph.param_type); 2876163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2877163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2878164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2879163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2880163953Srrs if (asoc->stream_reset_outstanding) 2881163953Srrs asoc->stream_reset_outstanding--; 2882163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2883163953Srrs /* do it */ 2884163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2885163953Srrs } else { 2886163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2887163953Srrs } 2888163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2889163953Srrs /* Answered my request */ 2890164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2891163953Srrs if (asoc->stream_reset_outstanding) 2892163953Srrs asoc->stream_reset_outstanding--; 2893163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2894163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2895163953Srrs } 2896163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2897163953Srrs /** 2898163953Srrs * a) Adopt the new in tsn. 2899163953Srrs * b) reset the map 2900163953Srrs * c) Adopt the new out-tsn 2901163953Srrs */ 2902163953Srrs struct sctp_stream_reset_response_tsn *resp; 2903163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2904163953Srrs int abort_flag = 0; 2905163953Srrs 2906163953Srrs if (respin == NULL) { 2907163953Srrs /* huh ? */ 2908163953Srrs return (0); 2909163953Srrs } 2910163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2911163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2912163953Srrs asoc->stream_reset_outstanding--; 2913163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2914163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2915163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2916163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2917163953Srrs if (abort_flag) { 2918163953Srrs return (1); 2919163953Srrs } 2920163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2921163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2922163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2923163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2924163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2925163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2926163953Srrs 2927163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2928163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2929163953Srrs 2930163953Srrs } 2931163953Srrs } 2932163953Srrs /* get rid of the request and get the request flags */ 2933163953Srrs if (asoc->stream_reset_outstanding == 0) { 2934163953Srrs sctp_clean_up_stream_reset(stcb); 2935163953Srrs } 2936163953Srrs } 2937163953Srrs } 2938163953Srrs return (0); 2939163953Srrs} 2940163953Srrs 2941163953Srrsstatic void 2942163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2943163953Srrs struct sctp_tmit_chunk *chk, 2944163953Srrs struct sctp_stream_reset_in_request *req) 2945163953Srrs{ 2946163953Srrs uint32_t seq; 2947163953Srrs int len, i; 2948163953Srrs int number_entries; 2949163953Srrs uint16_t temp; 2950163953Srrs 2951163953Srrs /* 2952163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2953163953Srrs * seq_in is right. 2954163953Srrs */ 2955163953Srrs struct sctp_association *asoc = &stcb->asoc; 2956163953Srrs 2957163953Srrs seq = ntohl(req->request_seq); 2958163953Srrs if (asoc->str_reset_seq_in == seq) { 2959163953Srrs if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2960163953Srrs len = ntohs(req->ph.param_length); 2961163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2962163953Srrs for (i = 0; i < number_entries; i++) { 2963163953Srrs temp = ntohs(req->list_of_streams[i]); 2964163953Srrs req->list_of_streams[i] = temp; 2965163953Srrs } 2966163953Srrs /* move the reset action back one */ 2967163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2968163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2969163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2970163953Srrs asoc->str_reset_seq_out, 2971163953Srrs seq, (asoc->sending_seq - 1)); 2972163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2973163953Srrs asoc->str_reset = chk; 2974163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 2975163953Srrs stcb->asoc.stream_reset_outstanding++; 2976163953Srrs } else { 2977163953Srrs /* Can't do it, since we have sent one out */ 2978163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2979163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 2980163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2981163953Srrs } 2982163953Srrs asoc->str_reset_seq_in++; 2983163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 2984163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2985163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 2986163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 2987163953Srrs } else { 2988163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 2989163953Srrs } 2990163953Srrs} 2991163953Srrs 2992163953Srrsstatic int 2993163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 2994163953Srrs struct sctp_tmit_chunk *chk, 2995163953Srrs struct sctp_stream_reset_tsn_request *req) 2996163953Srrs{ 2997163953Srrs /* reset all in and out and update the tsn */ 2998163953Srrs /* 2999163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3000163953Srrs * and set cum-ack to it. Also process this selected number as a 3001163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3002163953Srrs */ 3003163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3004163953Srrs struct sctp_association *asoc = &stcb->asoc; 3005163953Srrs int abort_flag = 0; 3006163953Srrs uint32_t seq; 3007163953Srrs 3008163953Srrs seq = ntohl(req->request_seq); 3009163953Srrs if (asoc->str_reset_seq_in == seq) { 3010163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3011163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3012163953Srrs fwdtsn.ch.chunk_flags = 0; 3013163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3014163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 3015163953Srrs if (abort_flag) { 3016163953Srrs return (1); 3017163953Srrs } 3018163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3019163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3020163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3021163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3022163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3023163953Srrs /* save off historical data for retrans */ 3024163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3025163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3026163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3027163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3028163953Srrs 3029163953Srrs sctp_add_stream_reset_result_tsn(chk, 3030163953Srrs ntohl(req->request_seq), 3031163953Srrs SCTP_STREAM_RESET_PERFORMED, 3032163953Srrs stcb->asoc.sending_seq, 3033163953Srrs stcb->asoc.mapping_array_base_tsn); 3034163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3035163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3036163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3037163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3038163953Srrs 3039163953Srrs asoc->str_reset_seq_in++; 3040163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3041163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3042163953Srrs stcb->asoc.last_sending_seq[0], 3043163953Srrs stcb->asoc.last_base_tsnsent[0] 3044163953Srrs ); 3045163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3046163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3047163953Srrs stcb->asoc.last_sending_seq[1], 3048163953Srrs stcb->asoc.last_base_tsnsent[1] 3049163953Srrs ); 3050163953Srrs } else { 3051163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3052163953Srrs } 3053163953Srrs return (0); 3054163953Srrs} 3055163953Srrs 3056163953Srrsstatic void 3057163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3058163953Srrs struct sctp_tmit_chunk *chk, 3059163953Srrs struct sctp_stream_reset_out_request *req) 3060163953Srrs{ 3061163953Srrs uint32_t seq, tsn; 3062163953Srrs int number_entries, len; 3063163953Srrs struct sctp_association *asoc = &stcb->asoc; 3064163953Srrs 3065163953Srrs seq = ntohl(req->request_seq); 3066163953Srrs 3067163953Srrs /* now if its not a duplicate we process it */ 3068163953Srrs if (asoc->str_reset_seq_in == seq) { 3069163953Srrs len = ntohs(req->ph.param_length); 3070163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3071163953Srrs /* 3072163953Srrs * the sender is resetting, handle the list issue.. we must 3073163953Srrs * a) verify if we can do the reset, if so no problem b) If 3074163953Srrs * we can't do the reset we must copy the request. c) queue 3075163953Srrs * it, and setup the data in processor to trigger it off 3076163953Srrs * when needed and dequeue all the queued data. 3077163953Srrs */ 3078163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3079163953Srrs 3080163953Srrs /* move the reset action back one */ 3081163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3082163953Srrs if ((tsn == asoc->cumulative_tsn) || 3083163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3084163953Srrs /* we can do it now */ 3085163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3086163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3087163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3088163953Srrs } else { 3089163953Srrs /* 3090163953Srrs * we must queue it up and thus wait for the TSN's 3091163953Srrs * to arrive that are at or before tsn 3092163953Srrs */ 3093163953Srrs struct sctp_stream_reset_list *liste; 3094163953Srrs int siz; 3095163953Srrs 3096163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3097163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3098163953Srrs siz, "StrRstList"); 3099163953Srrs if (liste == NULL) { 3100163953Srrs /* gak out of memory */ 3101163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3102163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3103163953Srrs return; 3104163953Srrs } 3105163953Srrs liste->tsn = tsn; 3106163953Srrs liste->number_entries = number_entries; 3107163953Srrs memcpy(&liste->req, req, 3108163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3109163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3110163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3111163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3112163953Srrs } 3113163953Srrs asoc->str_reset_seq_in++; 3114163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3115163953Srrs /* 3116163953Srrs * one seq back, just echo back last action since my 3117163953Srrs * response was lost. 3118163953Srrs */ 3119163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3120163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3121163953Srrs /* 3122163953Srrs * two seq back, just echo back last action since my 3123163953Srrs * response was lost. 3124163953Srrs */ 3125163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3126163953Srrs } else { 3127163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3128163953Srrs } 3129163953Srrs} 3130163953Srrs 3131163953Srrsstatic int 3132163953Srrssctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_out_req *sr_req) 3133163953Srrs{ 3134163953Srrs int chk_length, param_len, ptype; 3135163953Srrs uint32_t seq; 3136163953Srrs int num_req = 0; 3137163953Srrs struct sctp_tmit_chunk *chk; 3138163953Srrs struct sctp_chunkhdr *ch; 3139163953Srrs struct sctp_paramhdr *ph; 3140166675Srrs int ret_code = 0; 3141166675Srrs int num_param = 0; 3142163953Srrs 3143163953Srrs /* now it may be a reset or a reset-response */ 3144163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3145163953Srrs 3146163953Srrs /* setup for adding the response */ 3147163953Srrs sctp_alloc_a_chunk(stcb, chk); 3148163953Srrs if (chk == NULL) { 3149163953Srrs return (ret_code); 3150163953Srrs } 3151163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3152163953Srrs chk->asoc = &stcb->asoc; 3153163953Srrs chk->no_fr_allowed = 0; 3154163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3155166023Srrs chk->book_size_scale = 0; 3156165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3157163953Srrs if (chk->data == NULL) { 3158163953Srrsstrres_nochunk: 3159163953Srrs if (chk->data) { 3160163953Srrs sctp_m_freem(chk->data); 3161163953Srrs chk->data = NULL; 3162163953Srrs } 3163163953Srrs sctp_free_a_chunk(stcb, chk); 3164163953Srrs return (ret_code); 3165163953Srrs } 3166165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3167163953Srrs 3168163953Srrs /* setup chunk parameters */ 3169163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3170163953Srrs chk->snd_count = 0; 3171163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3172163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3173163953Srrs 3174163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3175163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3176163953Srrs ch->chunk_flags = 0; 3177163953Srrs ch->chunk_length = htons(chk->send_size); 3178165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3179163953Srrs 3180163953Srrs ph = (struct sctp_paramhdr *)&sr_req->sr_req; 3181163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3182163953Srrs param_len = ntohs(ph->param_length); 3183163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3184163953Srrs /* bad param */ 3185163953Srrs break; 3186163953Srrs } 3187163953Srrs ptype = ntohs(ph->param_type); 3188163953Srrs num_param++; 3189163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3190163953Srrs /* hit the max of parameters already sorry.. */ 3191163953Srrs break; 3192163953Srrs } 3193163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3194163953Srrs struct sctp_stream_reset_out_request *req_out; 3195163953Srrs 3196163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3197163953Srrs num_req++; 3198163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3199163953Srrs seq = ntohl(req_out->response_seq); 3200163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3201163953Srrs /* implicit ack */ 3202163953Srrs sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3203163953Srrs } 3204163953Srrs } 3205163953Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out); 3206163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3207163953Srrs struct sctp_stream_reset_in_request *req_in; 3208163953Srrs 3209163953Srrs num_req++; 3210163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3211163953Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in); 3212163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3213163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3214163953Srrs 3215163953Srrs num_req++; 3216163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3217163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3218163953Srrs ret_code = 1; 3219163953Srrs goto strres_nochunk; 3220163953Srrs } 3221163953Srrs /* no more */ 3222163953Srrs break; 3223163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3224163953Srrs struct sctp_stream_reset_response *resp; 3225163953Srrs uint32_t result; 3226163953Srrs 3227163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3228163953Srrs seq = ntohl(resp->response_seq); 3229163953Srrs result = ntohl(resp->result); 3230163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3231163953Srrs ret_code = 1; 3232163953Srrs goto strres_nochunk; 3233163953Srrs } 3234163953Srrs } else { 3235163953Srrs break; 3236163953Srrs } 3237163953Srrs 3238163953Srrs ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len)); 3239163953Srrs chk_length -= SCTP_SIZE32(param_len); 3240163953Srrs } 3241163953Srrs if (num_req == 0) { 3242163953Srrs /* we have no response free the stuff */ 3243163953Srrs goto strres_nochunk; 3244163953Srrs } 3245163953Srrs /* ok we have a chunk to link in */ 3246163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3247163953Srrs chk, 3248163953Srrs sctp_next); 3249163953Srrs stcb->asoc.ctrl_queue_cnt++; 3250163953Srrs return (ret_code); 3251163953Srrs} 3252163953Srrs 3253163953Srrs/* 3254163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3255163953Srrs * to handle this, either we get the whole packet and must disect it 3256163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3257163953Srrs * from a middle box that did the disectting for us. 3258163953Srrs */ 3259163953Srrsstatic void 3260163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3261163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3262163953Srrs{ 3263163953Srrs uint32_t bottle_bw, on_queue; 3264163953Srrs uint16_t trunc_len; 3265163953Srrs unsigned int chlen; 3266163953Srrs unsigned int at; 3267163953Srrs struct sctp_chunk_desc desc; 3268163953Srrs struct sctp_chunkhdr *ch; 3269163953Srrs 3270163953Srrs chlen = ntohs(cp->ch.chunk_length); 3271163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3272163953Srrs /* XXX possible chlen underflow */ 3273163953Srrs if (chlen == 0) { 3274163953Srrs ch = NULL; 3275163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3276163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3277163953Srrs } else { 3278163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3279163953Srrs chlen -= sizeof(struct sctphdr); 3280163953Srrs /* XXX possible chlen underflow */ 3281163953Srrs memset(&desc, 0, sizeof(desc)); 3282163953Srrs } 3283163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3284163953Srrs /* now the chunks themselves */ 3285163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3286163953Srrs desc.chunk_type = ch->chunk_type; 3287163953Srrs /* get amount we need to move */ 3288163953Srrs at = ntohs(ch->chunk_length); 3289163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3290163953Srrs /* corrupt chunk, maybe at the end? */ 3291163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3292163953Srrs break; 3293163953Srrs } 3294163953Srrs if (trunc_len == 0) { 3295163953Srrs /* we are supposed to have all of it */ 3296163953Srrs if (at > chlen) { 3297163953Srrs /* corrupt skip it */ 3298163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3299163953Srrs break; 3300163953Srrs } 3301163953Srrs } else { 3302163953Srrs /* is there enough of it left ? */ 3303163953Srrs if (desc.chunk_type == SCTP_DATA) { 3304163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3305163953Srrs sizeof(desc.data_bytes))) { 3306163953Srrs break; 3307163953Srrs } 3308163953Srrs } else { 3309163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3310163953Srrs break; 3311163953Srrs } 3312163953Srrs } 3313163953Srrs } 3314163953Srrs if (desc.chunk_type == SCTP_DATA) { 3315163953Srrs /* can we get out the tsn? */ 3316163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3317163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3318163953Srrs 3319163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3320163953Srrs /* yep */ 3321163953Srrs struct sctp_data_chunk *dcp; 3322163953Srrs uint8_t *ddp; 3323163953Srrs unsigned int iii; 3324163953Srrs 3325163953Srrs dcp = (struct sctp_data_chunk *)ch; 3326163953Srrs ddp = (uint8_t *) (dcp + 1); 3327163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3328163953Srrs desc.data_bytes[iii] = ddp[iii]; 3329163953Srrs } 3330163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3331163953Srrs } else { 3332163953Srrs /* nope we are done. */ 3333163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3334163953Srrs break; 3335163953Srrs } 3336163953Srrs } else { 3337163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3338163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3339163953Srrs } 3340163953Srrs 3341163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3342163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3343163953Srrs break; 3344163953Srrs } 3345163953Srrs if (SCTP_SIZE32(at) > chlen) { 3346163953Srrs break; 3347163953Srrs } 3348163953Srrs chlen -= SCTP_SIZE32(at); 3349163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3350163953Srrs /* done, none left */ 3351163953Srrs break; 3352163953Srrs } 3353163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3354163953Srrs } 3355163953Srrs /* Now update any rwnd --- possibly */ 3356163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3357163953Srrs /* From a peer, we get a rwnd report */ 3358163953Srrs uint32_t a_rwnd; 3359163953Srrs 3360163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3361163953Srrs 3362163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3363163953Srrs on_queue = ntohl(cp->current_onq); 3364163953Srrs if (bottle_bw && on_queue) { 3365163953Srrs /* a rwnd report is in here */ 3366163953Srrs if (bottle_bw > on_queue) 3367163953Srrs a_rwnd = bottle_bw - on_queue; 3368163953Srrs else 3369163953Srrs a_rwnd = 0; 3370163953Srrs 3371163953Srrs if (a_rwnd == 0) 3372163953Srrs stcb->asoc.peers_rwnd = 0; 3373163953Srrs else { 3374163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3375163953Srrs stcb->asoc.peers_rwnd = 3376163953Srrs a_rwnd - stcb->asoc.total_flight; 3377163953Srrs } else { 3378163953Srrs stcb->asoc.peers_rwnd = 0; 3379163953Srrs } 3380163953Srrs if (stcb->asoc.peers_rwnd < 3381163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3382163953Srrs /* SWS sender side engages */ 3383163953Srrs stcb->asoc.peers_rwnd = 0; 3384163953Srrs } 3385163953Srrs } 3386163953Srrs } 3387163953Srrs } else { 3388163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3389163953Srrs } 3390163953Srrs 3391163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3392163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3393163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3394163953Srrs (stcb->asoc.sat_network)) { 3395163953Srrs /* 3396163953Srrs * This is debateable but for sat networks it makes sense 3397163953Srrs * Note if a T3 timer has went off, we will prohibit any 3398163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3399163953Srrs */ 3400163953Srrs uint32_t bw_avail; 3401163953Srrs int rtt, incr; 3402163953Srrs 3403163953Srrs#ifdef SCTP_CWND_MONITOR 3404163953Srrs int old_cwnd = net->cwnd; 3405163953Srrs 3406163953Srrs#endif 3407163953Srrs /* need real RTT for this calc */ 3408163953Srrs rtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 3409163953Srrs /* get bottle neck bw */ 3410163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3411163953Srrs /* and whats on queue */ 3412163953Srrs on_queue = ntohl(cp->current_onq); 3413163953Srrs /* 3414163953Srrs * adjust the on-queue if our flight is more it could be 3415163953Srrs * that the router has not yet gotten data "in-flight" to it 3416163953Srrs */ 3417163953Srrs if (on_queue < net->flight_size) 3418163953Srrs on_queue = net->flight_size; 3419163953Srrs 3420163953Srrs /* calculate the available space */ 3421163953Srrs bw_avail = (bottle_bw * rtt) / 1000; 3422163953Srrs if (bw_avail > bottle_bw) { 3423163953Srrs /* 3424163953Srrs * Cap the growth to no more than the bottle neck. 3425163953Srrs * This can happen as RTT slides up due to queues. 3426163953Srrs * It also means if you have more than a 1 second 3427163953Srrs * RTT with a empty queue you will be limited to the 3428163953Srrs * bottle_bw per second no matter if other points 3429163953Srrs * have 1/2 the RTT and you could get more out... 3430163953Srrs */ 3431163953Srrs bw_avail = bottle_bw; 3432163953Srrs } 3433163953Srrs if (on_queue > bw_avail) { 3434163953Srrs /* 3435163953Srrs * No room for anything else don't allow anything 3436163953Srrs * else to be "added to the fire". 3437163953Srrs */ 3438163953Srrs int seg_inflight, seg_onqueue, my_portion; 3439163953Srrs 3440163953Srrs net->partial_bytes_acked = 0; 3441163953Srrs 3442163953Srrs /* how much are we over queue size? */ 3443163953Srrs incr = on_queue - bw_avail; 3444163953Srrs if (stcb->asoc.seen_a_sack_this_pkt) { 3445163953Srrs /* 3446163953Srrs * undo any cwnd adjustment that the sack 3447163953Srrs * might have made 3448163953Srrs */ 3449163953Srrs net->cwnd = net->prev_cwnd; 3450163953Srrs } 3451163953Srrs /* Now how much of that is mine? */ 3452163953Srrs seg_inflight = net->flight_size / net->mtu; 3453163953Srrs seg_onqueue = on_queue / net->mtu; 3454163953Srrs my_portion = (incr * seg_inflight) / seg_onqueue; 3455163953Srrs 3456163953Srrs /* Have I made an adjustment already */ 3457163953Srrs if (net->cwnd > net->flight_size) { 3458163953Srrs /* 3459163953Srrs * for this flight I made an adjustment we 3460163953Srrs * need to decrease the portion by a share 3461163953Srrs * our previous adjustment. 3462163953Srrs */ 3463163953Srrs int diff_adj; 3464163953Srrs 3465163953Srrs diff_adj = net->cwnd - net->flight_size; 3466163953Srrs if (diff_adj > my_portion) 3467163953Srrs my_portion = 0; 3468163953Srrs else 3469163953Srrs my_portion -= diff_adj; 3470163953Srrs } 3471163953Srrs /* 3472163953Srrs * back down to the previous cwnd (assume we have 3473163953Srrs * had a sack before this packet). minus what ever 3474163953Srrs * portion of the overage is my fault. 3475163953Srrs */ 3476163953Srrs net->cwnd -= my_portion; 3477163953Srrs 3478163953Srrs /* we will NOT back down more than 1 MTU */ 3479163953Srrs if (net->cwnd <= net->mtu) { 3480163953Srrs net->cwnd = net->mtu; 3481163953Srrs } 3482163953Srrs /* force into CA */ 3483163953Srrs net->ssthresh = net->cwnd - 1; 3484163953Srrs } else { 3485163953Srrs /* 3486163953Srrs * Take 1/4 of the space left or max burst up .. 3487163953Srrs * whichever is less. 3488163953Srrs */ 3489163953Srrs incr = min((bw_avail - on_queue) >> 2, 3490163953Srrs (int)stcb->asoc.max_burst * (int)net->mtu); 3491163953Srrs net->cwnd += incr; 3492163953Srrs } 3493163953Srrs if (net->cwnd > bw_avail) { 3494163953Srrs /* We can't exceed the pipe size */ 3495163953Srrs net->cwnd = bw_avail; 3496163953Srrs } 3497163953Srrs if (net->cwnd < net->mtu) { 3498163953Srrs /* We always have 1 MTU */ 3499163953Srrs net->cwnd = net->mtu; 3500163953Srrs } 3501163953Srrs#ifdef SCTP_CWND_MONITOR 3502163953Srrs if (net->cwnd - old_cwnd != 0) { 3503163953Srrs /* log only changes */ 3504163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), 3505163953Srrs SCTP_CWND_LOG_FROM_SAT); 3506163953Srrs } 3507163953Srrs#endif 3508163953Srrs } 3509163953Srrs} 3510163953Srrs 3511163953Srrs/* 3512163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3513163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3514163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3515163953Srrs * length of the complete packet outputs: - length: modified to remaining 3516163953Srrs * length after control processing - netp: modified to new sctp_nets after 3517163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3518163953Srrs * bad packet,...) otherwise return the tcb for this packet 3519163953Srrs */ 3520163953Srrsstatic struct sctp_tcb * 3521163953Srrssctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3522163953Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3523163953Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen) 3524163953Srrs{ 3525163953Srrs struct sctp_association *asoc; 3526163953Srrs uint32_t vtag_in; 3527163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3528163953Srrs int chk_length; 3529163953Srrs int ret; 3530163953Srrs 3531163953Srrs /* 3532163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3533163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3534163953Srrs * until we get into jumbo grams and such.. 3535163953Srrs */ 3536166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3537163953Srrs struct sctp_tcb *locked_tcb = stcb; 3538163953Srrs int got_auth = 0; 3539163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3540163953Srrs int auth_skipped = 0; 3541163953Srrs 3542163953Srrs#ifdef SCTP_DEBUG 3543163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 3544163953Srrs printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3545163953Srrs iphlen, *offset, length, stcb); 3546163953Srrs } 3547163953Srrs#endif /* SCTP_DEBUG */ 3548163953Srrs 3549163953Srrs /* validate chunk header length... */ 3550163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3551163953Srrs return (NULL); 3552163953Srrs } 3553163953Srrs /* 3554163953Srrs * validate the verification tag 3555163953Srrs */ 3556163953Srrs vtag_in = ntohl(sh->v_tag); 3557163953Srrs 3558165220Srrs if (locked_tcb) { 3559165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3560165220Srrs } 3561163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3562163953Srrs if (vtag_in != 0) { 3563163953Srrs /* protocol error- silently discard... */ 3564163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3565163953Srrs if (locked_tcb) 3566163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3567163953Srrs return (NULL); 3568163953Srrs } 3569163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3570163953Srrs /* 3571163953Srrs * If there is no stcb, skip the AUTH chunk and process 3572163953Srrs * later after a stcb is found (to validate the lookup was 3573163953Srrs * valid. 3574163953Srrs */ 3575163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3576163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3577163953Srrs /* save this chunk for later processing */ 3578163953Srrs auth_skipped = 1; 3579163953Srrs auth_offset = *offset; 3580163953Srrs auth_len = ntohs(ch->chunk_length); 3581163953Srrs 3582163953Srrs /* (temporarily) move past this chunk */ 3583163953Srrs *offset += SCTP_SIZE32(auth_len); 3584163953Srrs if (*offset >= length) { 3585163953Srrs /* no more data left in the mbuf chain */ 3586163953Srrs *offset = length; 3587163953Srrs return (NULL); 3588163953Srrs } 3589163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3590163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3591163953Srrs } 3592163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3593163953Srrs goto process_control_chunks; 3594163953Srrs } 3595163953Srrs /* 3596163953Srrs * first check if it's an ASCONF with an unknown src addr we 3597163953Srrs * need to look inside to find the association 3598163953Srrs */ 3599163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3600163953Srrs /* inp's refcount may be reduced */ 3601163953Srrs SCTP_INP_INCR_REF(inp); 3602163953Srrs 3603163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3604163953Srrs *offset, sh, &inp, netp); 3605163953Srrs if (stcb == NULL) { 3606163953Srrs /* 3607163953Srrs * reduce inp's refcount if not reduced in 3608163953Srrs * sctp_findassociation_ep_asconf(). 3609163953Srrs */ 3610163953Srrs SCTP_INP_DECR_REF(inp); 3611163953Srrs } 3612163953Srrs /* now go back and verify any auth chunk to be sure */ 3613163953Srrs if (auth_skipped && (stcb != NULL)) { 3614163953Srrs struct sctp_auth_chunk *auth; 3615163953Srrs 3616163953Srrs auth = (struct sctp_auth_chunk *) 3617163953Srrs sctp_m_getptr(m, auth_offset, 3618163953Srrs auth_len, chunk_buf); 3619163953Srrs got_auth = 1; 3620163953Srrs auth_skipped = 0; 3621163953Srrs if (sctp_handle_auth(stcb, auth, m, 3622163953Srrs auth_offset)) { 3623163953Srrs /* auth HMAC failed so dump it */ 3624163953Srrs *offset = length; 3625163953Srrs return (NULL); 3626163953Srrs } else { 3627163953Srrs /* remaining chunks are HMAC checked */ 3628163953Srrs stcb->asoc.authenticated = 1; 3629163953Srrs } 3630163953Srrs } 3631163953Srrs } 3632163953Srrs if (stcb == NULL) { 3633163953Srrs /* no association, so it's out of the blue... */ 3634163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL); 3635163953Srrs *offset = length; 3636163953Srrs if (locked_tcb) 3637163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3638163953Srrs return (NULL); 3639163953Srrs } 3640163953Srrs asoc = &stcb->asoc; 3641163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3642163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3643163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3644163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3645163953Srrs if ((vtag_in == asoc->my_vtag) || 3646163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3647163953Srrs (vtag_in == asoc->peer_vtag))) { 3648163953Srrs /* this is valid */ 3649163953Srrs } else { 3650163953Srrs /* drop this packet... */ 3651163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3652163953Srrs if (locked_tcb) 3653163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3654163953Srrs return (NULL); 3655163953Srrs } 3656163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3657163953Srrs if (vtag_in != asoc->my_vtag) { 3658163953Srrs /* 3659163953Srrs * this could be a stale SHUTDOWN-ACK or the 3660163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3661163953Srrs * is still hung; we have started a new asoc 3662163953Srrs * but it won't complete until the shutdown 3663163953Srrs * is completed 3664163953Srrs */ 3665163953Srrs if (locked_tcb) 3666163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3667163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3668163953Srrs NULL); 3669163953Srrs return (NULL); 3670163953Srrs } 3671163953Srrs } else { 3672163953Srrs /* for all other chunks, vtag must match */ 3673163953Srrs if (vtag_in != asoc->my_vtag) { 3674163953Srrs /* invalid vtag... */ 3675163953Srrs#ifdef SCTP_DEBUG 3676163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3677163953Srrs printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag); 3678163953Srrs } 3679163953Srrs#endif /* SCTP_DEBUG */ 3680163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3681163953Srrs if (locked_tcb) 3682163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3683163953Srrs *offset = length; 3684163953Srrs return (NULL); 3685163953Srrs } 3686163953Srrs } 3687163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3688163953Srrs /* 3689163953Srrs * process all control chunks... 3690163953Srrs */ 3691163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3692163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3693163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3694163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3695163953Srrs stcb->asoc.overall_error_count = 0; 3696163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3697163953Srrs *netp); 3698163953Srrs } 3699163953Srrsprocess_control_chunks: 3700163953Srrs 3701163953Srrs while (IS_SCTP_CONTROL(ch)) { 3702163953Srrs /* validate chunk length */ 3703163953Srrs chk_length = ntohs(ch->chunk_length); 3704163953Srrs#ifdef SCTP_DEBUG 3705163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 3706163953Srrs printf("sctp_process_control: processing a chunk type=%u, len=%u\n", 3707163953Srrs ch->chunk_type, chk_length); 3708163953Srrs } 3709163953Srrs#endif /* SCTP_DEBUG */ 3710163953Srrs if ((size_t)chk_length < sizeof(*ch) || 3711163953Srrs (*offset + chk_length) > length) { 3712163953Srrs *offset = length; 3713163953Srrs if (locked_tcb) 3714163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3715163953Srrs return (NULL); 3716163953Srrs } 3717163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3718163953Srrs /* 3719163953Srrs * INIT-ACK only gets the init ack "header" portion only 3720163953Srrs * because we don't have to process the peer's COOKIE. All 3721163953Srrs * others get a complete chunk. 3722163953Srrs */ 3723163953Srrs if (ch->chunk_type == SCTP_INITIATION_ACK) { 3724163953Srrs /* get an init-ack chunk */ 3725163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3726163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3727163953Srrs if (ch == NULL) { 3728163953Srrs *offset = length; 3729163953Srrs if (locked_tcb) 3730163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3731163953Srrs return (NULL); 3732163953Srrs } 3733163953Srrs } else { 3734163953Srrs /* get a complete chunk... */ 3735163953Srrs if ((size_t)chk_length > sizeof(chunk_buf)) { 3736163953Srrs struct mbuf *oper; 3737163953Srrs struct sctp_paramhdr *phdr; 3738163953Srrs 3739163953Srrs oper = NULL; 3740163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 3741165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 3742163953Srrs if (oper) { 3743163953Srrs /* pre-reserve some space */ 3744165647Srrs SCTP_BUF_RESV_UF(oper, sizeof(struct sctp_chunkhdr)); 3745165647Srrs SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr); 3746163953Srrs phdr = mtod(oper, struct sctp_paramhdr *); 3747163953Srrs phdr->param_type = htons(SCTP_CAUSE_OUT_OF_RESC); 3748163953Srrs phdr->param_length = htons(sizeof(struct sctp_paramhdr)); 3749163953Srrs sctp_queue_op_err(stcb, oper); 3750163953Srrs } 3751163953Srrs if (locked_tcb) 3752163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3753163953Srrs return (NULL); 3754163953Srrs } 3755163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3756163953Srrs chk_length, chunk_buf); 3757163953Srrs if (ch == NULL) { 3758163953Srrs printf("sctp_process_control: Can't get the all data....\n"); 3759163953Srrs *offset = length; 3760163953Srrs if (locked_tcb) 3761163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3762163953Srrs return (NULL); 3763163953Srrs } 3764163953Srrs } 3765163953Srrs num_chunks++; 3766163953Srrs /* Save off the last place we got a control from */ 3767163953Srrs if (stcb != NULL) { 3768163953Srrs if ((*netp != NULL) || (ch->chunk_type == SCTP_ASCONF)) { 3769163953Srrs /* 3770163953Srrs * allow last_control to be NULL if 3771163953Srrs * ASCONF... ASCONF processing will find the 3772163953Srrs * right net later 3773163953Srrs */ 3774163953Srrs stcb->asoc.last_control_chunk_from = *netp; 3775163953Srrs } 3776163953Srrs } 3777163953Srrs#ifdef SCTP_AUDITING_ENABLED 3778163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3779163953Srrs#endif 3780163953Srrs 3781163953Srrs /* check to see if this chunk required auth, but isn't */ 3782163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3783163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3784163953Srrs stcb->asoc.local_auth_chunks) && 3785163953Srrs !stcb->asoc.authenticated) { 3786163953Srrs /* "silently" ignore */ 3787163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3788163953Srrs goto next_chunk; 3789163953Srrs } 3790163953Srrs switch (ch->chunk_type) { 3791163953Srrs case SCTP_INITIATION: 3792163953Srrs /* must be first and only chunk */ 3793163953Srrs#ifdef SCTP_DEBUG 3794163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3795163953Srrs printf("SCTP_INIT\n"); 3796163953Srrs } 3797163953Srrs#endif /* SCTP_DEBUG */ 3798163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3799163953Srrs /* We are not interested anymore? */ 3800163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3801163953Srrs /* 3802163953Srrs * collision case where we are 3803163953Srrs * sending to them too 3804163953Srrs */ 3805163953Srrs ; 3806163953Srrs } else { 3807163953Srrs if (locked_tcb) 3808163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3809163953Srrs *offset = length; 3810163953Srrs return (NULL); 3811163953Srrs } 3812163953Srrs } 3813163953Srrs if ((num_chunks > 1) || 3814163953Srrs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) { 3815163953Srrs *offset = length; 3816163953Srrs if (locked_tcb) 3817163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3818163953Srrs return (NULL); 3819163953Srrs } 3820163953Srrs if ((stcb != NULL) && 3821163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3822163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3823163953Srrs sctp_send_shutdown_ack(stcb, 3824163953Srrs stcb->asoc.primary_destination); 3825163953Srrs *offset = length; 3826163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3827163953Srrs if (locked_tcb) 3828163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3829163953Srrs return (NULL); 3830163953Srrs } 3831163953Srrs sctp_handle_init(m, iphlen, *offset, sh, 3832163953Srrs (struct sctp_init_chunk *)ch, inp, stcb, *netp); 3833163953Srrs *offset = length; 3834163953Srrs if (locked_tcb) 3835163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3836163953Srrs return (NULL); 3837163953Srrs break; 3838163953Srrs case SCTP_INITIATION_ACK: 3839163953Srrs /* must be first and only chunk */ 3840163953Srrs#ifdef SCTP_DEBUG 3841163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3842163953Srrs printf("SCTP_INIT-ACK\n"); 3843163953Srrs } 3844163953Srrs#endif /* SCTP_DEBUG */ 3845163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3846163953Srrs /* We are not interested anymore */ 3847163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3848163953Srrs ; 3849163953Srrs } else { 3850163953Srrs if (locked_tcb) 3851163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3852163953Srrs *offset = length; 3853163953Srrs if (stcb) { 3854165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3855163953Srrs } 3856163953Srrs return (NULL); 3857163953Srrs } 3858163953Srrs } 3859163953Srrs if ((num_chunks > 1) || 3860163953Srrs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) { 3861163953Srrs *offset = length; 3862163953Srrs if (locked_tcb) 3863163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3864163953Srrs return (NULL); 3865163953Srrs } 3866163953Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3867163953Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp); 3868163953Srrs /* 3869163953Srrs * Special case, I must call the output routine to 3870163953Srrs * get the cookie echoed 3871163953Srrs */ 3872163953Srrs if ((stcb) && ret == 0) 3873163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3874163953Srrs *offset = length; 3875163953Srrs if (locked_tcb) 3876163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3877163953Srrs return (NULL); 3878163953Srrs break; 3879163953Srrs case SCTP_SELECTIVE_ACK: 3880163953Srrs#ifdef SCTP_DEBUG 3881163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3882163953Srrs printf("SCTP_SACK\n"); 3883163953Srrs } 3884163953Srrs#endif /* SCTP_DEBUG */ 3885163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3886163953Srrs { 3887163953Srrs struct sctp_sack_chunk *sack; 3888163953Srrs int abort_now = 0; 3889163953Srrs uint32_t a_rwnd, cum_ack; 3890163953Srrs uint16_t num_seg; 3891163953Srrs int nonce_sum_flag; 3892163953Srrs 3893163953Srrs sack = (struct sctp_sack_chunk *)ch; 3894163953Srrs 3895163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3896163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3897163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3898163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3899163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3900163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3901163953Srrs (num_seg == 0) && 3902163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3903163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3904163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3905163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3906163953Srrs ) { 3907163953Srrs /* 3908163953Srrs * We have a SIMPLE sack having no 3909163953Srrs * prior segments and data on sent 3910163953Srrs * queue to be acked.. Use the 3911163953Srrs * faster path sack processing. We 3912163953Srrs * also allow window update sacks 3913163953Srrs * with no missing segments to go 3914163953Srrs * this way too. 3915163953Srrs */ 3916163953Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, &abort_now); 3917163953Srrs } else { 3918163953Srrs sctp_handle_sack(sack, stcb, *netp, &abort_now); 3919163953Srrs } 3920163953Srrs if (abort_now) { 3921163953Srrs /* ABORT signal from sack processing */ 3922163953Srrs *offset = length; 3923163953Srrs return (NULL); 3924163953Srrs } 3925163953Srrs } 3926163953Srrs break; 3927163953Srrs case SCTP_HEARTBEAT_REQUEST: 3928163953Srrs#ifdef SCTP_DEBUG 3929163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3930163953Srrs printf("SCTP_HEARTBEAT\n"); 3931163953Srrs } 3932163953Srrs#endif /* SCTP_DEBUG */ 3933163953Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 3934163953Srrs sctp_send_heartbeat_ack(stcb, m, *offset, chk_length, 3935163953Srrs *netp); 3936163953Srrs 3937163953Srrs /* He's alive so give him credit */ 3938163953Srrs stcb->asoc.overall_error_count = 0; 3939163953Srrs break; 3940163953Srrs case SCTP_HEARTBEAT_ACK: 3941163953Srrs#ifdef SCTP_DEBUG 3942163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3943163953Srrs printf("SCTP_HEARTBEAT-ACK\n"); 3944163953Srrs } 3945163953Srrs#endif /* SCTP_DEBUG */ 3946163953Srrs 3947163953Srrs /* He's alive so give him credit */ 3948163953Srrs stcb->asoc.overall_error_count = 0; 3949163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 3950163953Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 3951163953Srrs stcb, *netp); 3952163953Srrs break; 3953163953Srrs case SCTP_ABORT_ASSOCIATION: 3954163953Srrs#ifdef SCTP_DEBUG 3955163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3956163953Srrs printf("SCTP_ABORT\n"); 3957163953Srrs } 3958163953Srrs#endif /* SCTP_DEBUG */ 3959163953Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 3960163953Srrs stcb, *netp); 3961163953Srrs *offset = length; 3962163953Srrs return (NULL); 3963163953Srrs break; 3964163953Srrs case SCTP_SHUTDOWN: 3965163953Srrs#ifdef SCTP_DEBUG 3966163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3967163953Srrs printf("SCTP_SHUTDOWN\n"); 3968163953Srrs } 3969163953Srrs#endif /* SCTP_DEBUG */ 3970163953Srrs { 3971163953Srrs int abort_flag = 0; 3972163953Srrs 3973163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 3974163953Srrs stcb, *netp, &abort_flag); 3975163953Srrs if (abort_flag) { 3976163953Srrs *offset = length; 3977163953Srrs return (NULL); 3978163953Srrs } 3979163953Srrs } 3980163953Srrs break; 3981163953Srrs case SCTP_SHUTDOWN_ACK: 3982163953Srrs#ifdef SCTP_DEBUG 3983163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3984163953Srrs printf("SCTP_SHUTDOWN-ACK\n"); 3985163953Srrs } 3986163953Srrs#endif /* SCTP_DEBUG */ 3987163953Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 3988163953Srrs *offset = length; 3989163953Srrs return (NULL); 3990163953Srrs break; 3991163953Srrs case SCTP_OPERATION_ERROR: 3992163953Srrs#ifdef SCTP_DEBUG 3993163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3994163953Srrs printf("SCTP_OP-ERR\n"); 3995163953Srrs } 3996163953Srrs#endif /* SCTP_DEBUG */ 3997163953Srrs if (sctp_handle_error(ch, stcb, *netp) < 0) { 3998163953Srrs *offset = length; 3999163953Srrs return (NULL); 4000163953Srrs } 4001163953Srrs break; 4002163953Srrs case SCTP_COOKIE_ECHO: 4003163953Srrs#ifdef SCTP_DEBUG 4004163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4005163953Srrs printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb); 4006163953Srrs } 4007163953Srrs#endif /* SCTP_DEBUG */ 4008163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4009163953Srrs ; 4010163953Srrs } else { 4011163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) && 4012163953Srrs (stcb == NULL)) { 4013163953Srrs /* We are not interested anymore */ 4014163953Srrs *offset = length; 4015163953Srrs return (NULL); 4016163953Srrs } 4017163953Srrs } 4018163953Srrs /* 4019163953Srrs * First are we accepting? We do this again here 4020163953Srrs * since it is possible that a previous endpoint WAS 4021163953Srrs * listening responded to a INIT-ACK and then 4022163953Srrs * closed. We opened and bound.. and are now no 4023163953Srrs * longer listening. 4024163953Srrs */ 4025163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4026163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4027163953Srrs /* 4028163953Srrs * special case, is this a retran'd 4029163953Srrs * COOKIE-ECHO or a restarting assoc 4030163953Srrs * that is a peeled off or 4031163953Srrs * one-to-one style socket. 4032163953Srrs */ 4033163953Srrs goto process_cookie_anyway; 4034163953Srrs } 4035163953Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, 4036163953Srrs NULL); 4037163953Srrs *offset = length; 4038163953Srrs return (NULL); 4039163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4040163953Srrs /* we are accepting so check limits like TCP */ 4041163953Srrs if (inp->sctp_socket->so_qlen > 4042163953Srrs inp->sctp_socket->so_qlimit) { 4043163953Srrs /* no space */ 4044163953Srrs struct mbuf *oper; 4045163953Srrs struct sctp_paramhdr *phdr; 4046163953Srrs 4047163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4048163953Srrs oper = NULL; 4049163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4050165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4051163953Srrs if (oper) { 4052165647Srrs SCTP_BUF_LEN(oper) = 4053163953Srrs sizeof(struct sctp_paramhdr); 4054163953Srrs phdr = mtod(oper, 4055163953Srrs struct sctp_paramhdr *); 4056163953Srrs phdr->param_type = 4057163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4058163953Srrs phdr->param_length = 4059163953Srrs htons(sizeof(struct sctp_paramhdr)); 4060163953Srrs } 4061163953Srrs sctp_abort_association(inp, stcb, m, 4062163953Srrs iphlen, sh, oper); 4063163953Srrs } 4064163953Srrs *offset = length; 4065163953Srrs return (NULL); 4066163953Srrs } 4067163953Srrs } 4068163953Srrs process_cookie_anyway: 4069163953Srrs { 4070163953Srrs struct mbuf *ret_buf; 4071165220Srrs struct sctp_inpcb *linp; 4072163953Srrs 4073165220Srrs if (stcb) 4074165220Srrs linp = NULL; 4075165220Srrs else 4076165220Srrs linp = inp; 4077165220Srrs 4078165220Srrs if (linp) 4079165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4080163953Srrs ret_buf = 4081163953Srrs sctp_handle_cookie_echo(m, iphlen, 4082163953Srrs *offset, sh, 4083163953Srrs (struct sctp_cookie_echo_chunk *)ch, 4084163953Srrs &inp, &stcb, netp, 4085163953Srrs auth_skipped, 4086163953Srrs auth_offset, 4087165220Srrs auth_len, 4088165220Srrs &locked_tcb); 4089165220Srrs if (linp) 4090165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4091163953Srrs if (ret_buf == NULL) { 4092163953Srrs if (locked_tcb) { 4093163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4094163953Srrs } 4095163953Srrs#ifdef SCTP_DEBUG 4096163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4097163953Srrs printf("GAK, null buffer\n"); 4098163953Srrs } 4099163953Srrs#endif /* SCTP_DEBUG */ 4100163953Srrs auth_skipped = 0; 4101163953Srrs *offset = length; 4102163953Srrs return (NULL); 4103163953Srrs } 4104163953Srrs /* if AUTH skipped, see if it verified... */ 4105163953Srrs if (auth_skipped) { 4106163953Srrs got_auth = 1; 4107163953Srrs auth_skipped = 0; 4108163953Srrs } 4109163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4110163953Srrs /* 4111163953Srrs * Restart the timer if we have 4112163953Srrs * pending data 4113163953Srrs */ 4114163953Srrs struct sctp_tmit_chunk *chk; 4115163953Srrs 4116163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4117163953Srrs if (chk) { 4118163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4119163953Srrs stcb->sctp_ep, stcb, 4120163953Srrs chk->whoTo); 4121163953Srrs } 4122163953Srrs } 4123163953Srrs } 4124163953Srrs break; 4125163953Srrs case SCTP_COOKIE_ACK: 4126163953Srrs#ifdef SCTP_DEBUG 4127163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4128163953Srrs printf("SCTP_COOKIE-ACK\n"); 4129163953Srrs } 4130163953Srrs#endif /* SCTP_DEBUG */ 4131163953Srrs 4132163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4133163953Srrs /* We are not interested anymore */ 4134163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4135163953Srrs ; 4136163953Srrs } else { 4137165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4138163953Srrs *offset = length; 4139163953Srrs return (NULL); 4140163953Srrs } 4141163953Srrs } 4142163953Srrs /* He's alive so give him credit */ 4143163953Srrs stcb->asoc.overall_error_count = 0; 4144163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4145163953Srrs break; 4146163953Srrs case SCTP_ECN_ECHO: 4147163953Srrs#ifdef SCTP_DEBUG 4148163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4149163953Srrs printf("SCTP_ECN-ECHO\n"); 4150163953Srrs } 4151163953Srrs#endif /* SCTP_DEBUG */ 4152163953Srrs /* He's alive so give him credit */ 4153163953Srrs stcb->asoc.overall_error_count = 0; 4154163953Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4155163953Srrs stcb); 4156163953Srrs break; 4157163953Srrs case SCTP_ECN_CWR: 4158163953Srrs#ifdef SCTP_DEBUG 4159163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4160163953Srrs printf("SCTP_ECN-CWR\n"); 4161163953Srrs } 4162163953Srrs#endif /* SCTP_DEBUG */ 4163163953Srrs /* He's alive so give him credit */ 4164163953Srrs stcb->asoc.overall_error_count = 0; 4165163953Srrs 4166163953Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4167163953Srrs break; 4168163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4169163953Srrs#ifdef SCTP_DEBUG 4170163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4171163953Srrs printf("SCTP_SHUTDOWN-COMPLETE\n"); 4172163953Srrs } 4173163953Srrs#endif /* SCTP_DEBUG */ 4174163953Srrs /* must be first and only chunk */ 4175163953Srrs if ((num_chunks > 1) || 4176163953Srrs (length - *offset > SCTP_SIZE32(chk_length))) { 4177163953Srrs *offset = length; 4178163953Srrs if (locked_tcb) 4179163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4180163953Srrs 4181163953Srrs return (NULL); 4182163953Srrs } 4183163953Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4184163953Srrs stcb, *netp); 4185163953Srrs *offset = length; 4186163953Srrs return (NULL); 4187163953Srrs break; 4188163953Srrs case SCTP_ASCONF: 4189163953Srrs#ifdef SCTP_DEBUG 4190163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4191163953Srrs printf("SCTP_ASCONF\n"); 4192163953Srrs } 4193163953Srrs#endif /* SCTP_DEBUG */ 4194163953Srrs /* He's alive so give him credit */ 4195163953Srrs stcb->asoc.overall_error_count = 0; 4196163953Srrs 4197163953Srrs sctp_handle_asconf(m, *offset, 4198163953Srrs (struct sctp_asconf_chunk *)ch, stcb); 4199163953Srrs break; 4200163953Srrs case SCTP_ASCONF_ACK: 4201163953Srrs#ifdef SCTP_DEBUG 4202163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4203163953Srrs printf("SCTP_ASCONF-ACK\n"); 4204163953Srrs } 4205163953Srrs#endif /* SCTP_DEBUG */ 4206163953Srrs /* He's alive so give him credit */ 4207163953Srrs stcb->asoc.overall_error_count = 0; 4208163953Srrs 4209163953Srrs sctp_handle_asconf_ack(m, *offset, 4210163953Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4211163953Srrs break; 4212163953Srrs case SCTP_FORWARD_CUM_TSN: 4213163953Srrs#ifdef SCTP_DEBUG 4214163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4215163953Srrs printf("SCTP_FWD-TSN\n"); 4216163953Srrs } 4217163953Srrs#endif /* SCTP_DEBUG */ 4218163953Srrs /* He's alive so give him credit */ 4219163953Srrs { 4220163953Srrs int abort_flag = 0; 4221163953Srrs 4222163953Srrs stcb->asoc.overall_error_count = 0; 4223163953Srrs *fwd_tsn_seen = 1; 4224163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4225163953Srrs /* We are not interested anymore */ 4226165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28); 4227163953Srrs *offset = length; 4228163953Srrs return (NULL); 4229163953Srrs } 4230163953Srrs sctp_handle_forward_tsn(stcb, 4231163953Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag); 4232163953Srrs if (abort_flag) { 4233163953Srrs *offset = length; 4234163953Srrs return (NULL); 4235163953Srrs } else { 4236163953Srrs stcb->asoc.overall_error_count = 0; 4237163953Srrs } 4238163953Srrs 4239163953Srrs } 4240163953Srrs break; 4241163953Srrs case SCTP_STREAM_RESET: 4242163953Srrs#ifdef SCTP_DEBUG 4243163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4244163953Srrs printf("SCTP_STREAM_RESET\n"); 4245163953Srrs } 4246163953Srrs#endif /* SCTP_DEBUG */ 4247163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4248163953Srrs chk_length, chunk_buf); 4249163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4250163953Srrs /* We are not interested anymore */ 4251165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4252163953Srrs *offset = length; 4253163953Srrs return (NULL); 4254163953Srrs } 4255163953Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4256163953Srrs /* 4257163953Srrs * hmm, peer should have announced this, but 4258163953Srrs * we will turn it on since he is sending us 4259163953Srrs * a stream reset. 4260163953Srrs */ 4261163953Srrs stcb->asoc.peer_supports_strreset = 1; 4262163953Srrs } 4263163953Srrs if (sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_out_req *)ch)) { 4264163953Srrs /* stop processing */ 4265163953Srrs *offset = length; 4266163953Srrs return (NULL); 4267163953Srrs } 4268163953Srrs break; 4269163953Srrs case SCTP_PACKET_DROPPED: 4270163953Srrs#ifdef SCTP_DEBUG 4271163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4272163953Srrs printf("SCTP_PACKET_DROPPED\n"); 4273163953Srrs } 4274163953Srrs#endif /* SCTP_DEBUG */ 4275163953Srrs /* re-get it all please */ 4276163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4277163953Srrs chk_length, chunk_buf); 4278163953Srrs 4279163953Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4280163953Srrs stcb, *netp); 4281163953Srrs 4282163953Srrs break; 4283163953Srrs 4284163953Srrs case SCTP_AUTHENTICATION: 4285163953Srrs#ifdef SCTP_DEBUG 4286163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4287163953Srrs printf("SCTP_AUTHENTICATION\n"); 4288163953Srrs } 4289163953Srrs#endif /* SCTP_DEBUG */ 4290163953Srrs if (sctp_auth_disable) 4291163953Srrs goto unknown_chunk; 4292163953Srrs 4293163953Srrs if (stcb == NULL) { 4294163953Srrs /* save the first AUTH for later processing */ 4295163953Srrs if (auth_skipped == 0) { 4296163953Srrs auth_offset = *offset; 4297163953Srrs auth_len = chk_length; 4298163953Srrs auth_skipped = 1; 4299163953Srrs } 4300163953Srrs /* skip this chunk (temporarily) */ 4301163953Srrs goto next_chunk; 4302163953Srrs } 4303163953Srrs if (got_auth == 1) { 4304163953Srrs /* skip this chunk... it's already auth'd */ 4305163953Srrs goto next_chunk; 4306163953Srrs } 4307163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4308163953Srrs chk_length, chunk_buf); 4309163953Srrs got_auth = 1; 4310163953Srrs if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4311163953Srrs m, *offset)) { 4312163953Srrs /* auth HMAC failed so dump the packet */ 4313163953Srrs *offset = length; 4314163953Srrs return (stcb); 4315163953Srrs } else { 4316163953Srrs /* remaining chunks are HMAC checked */ 4317163953Srrs stcb->asoc.authenticated = 1; 4318163953Srrs } 4319163953Srrs break; 4320163953Srrs 4321163953Srrs default: 4322163953Srrs unknown_chunk: 4323163953Srrs /* it's an unknown chunk! */ 4324163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4325163953Srrs struct mbuf *mm; 4326163953Srrs struct sctp_paramhdr *phd; 4327163953Srrs 4328163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4329165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4330163953Srrs if (mm) { 4331163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4332163953Srrs /* 4333163953Srrs * We cheat and use param type since 4334163953Srrs * we did not bother to define a 4335163953Srrs * error cause struct. They are the 4336163953Srrs * same basic format with different 4337163953Srrs * names. 4338163953Srrs */ 4339163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4340163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4341165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4342166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4343163953Srrs M_DONTWAIT); 4344165647Srrs if (SCTP_BUF_NEXT(mm)) { 4345163953Srrs sctp_queue_op_err(stcb, mm); 4346163953Srrs } else { 4347163953Srrs sctp_m_freem(mm); 4348163953Srrs } 4349163953Srrs } 4350163953Srrs } 4351163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4352163953Srrs /* discard this packet */ 4353163953Srrs *offset = length; 4354163953Srrs return (stcb); 4355163953Srrs } /* else skip this bad chunk and continue... */ 4356163953Srrs break; 4357163953Srrs } /* switch (ch->chunk_type) */ 4358163953Srrs 4359163953Srrs 4360163953Srrsnext_chunk: 4361163953Srrs /* get the next chunk */ 4362163953Srrs *offset += SCTP_SIZE32(chk_length); 4363163953Srrs if (*offset >= length) { 4364163953Srrs /* no more data left in the mbuf chain */ 4365163953Srrs break; 4366163953Srrs } 4367163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4368163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4369163953Srrs if (ch == NULL) { 4370163953Srrs if (locked_tcb) 4371163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4372163953Srrs *offset = length; 4373163953Srrs return (NULL); 4374163953Srrs } 4375163953Srrs } /* while */ 4376163953Srrs return (stcb); 4377163953Srrs} 4378163953Srrs 4379163953Srrs 4380163953Srrs/* 4381163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4382163953Srrs * ECN(0) or ECN(1) or CE 4383163953Srrs */ 4384163953Srrsstatic __inline void 4385163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4386163953Srrs uint8_t ecn_bits) 4387163953Srrs{ 4388163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4389163953Srrs ; 4390163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4391163953Srrs /* 4392163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4393163953Srrs * change the NS bit (that we have yet to find a way to send 4394163953Srrs * it yet). 4395163953Srrs */ 4396163953Srrs 4397163953Srrs /* ECN Nonce stuff */ 4398163953Srrs stcb->asoc.receiver_nonce_sum++; 4399163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4400163953Srrs 4401163953Srrs /* 4402163953Srrs * Drag up the last_echo point if cumack is larger since we 4403163953Srrs * don't want the point falling way behind by more than 4404163953Srrs * 2^^31 and then having it be incorrect. 4405163953Srrs */ 4406163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4407163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4408163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4409163953Srrs } 4410163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4411163953Srrs /* 4412163953Srrs * Drag up the last_echo point if cumack is larger since we 4413163953Srrs * don't want the point falling way behind by more than 4414163953Srrs * 2^^31 and then having it be incorrect. 4415163953Srrs */ 4416163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4417163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4418163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4419163953Srrs } 4420163953Srrs } 4421163953Srrs} 4422163953Srrs 4423163953Srrsstatic __inline void 4424163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4425163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4426163953Srrs{ 4427163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4428163953Srrs /* 4429163953Srrs * we possibly must notify the sender that a congestion 4430163953Srrs * window reduction is in order. We do this by adding a ECNE 4431163953Srrs * chunk to the output chunk queue. The incoming CWR will 4432163953Srrs * remove this chunk. 4433163953Srrs */ 4434163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4435163953Srrs MAX_TSN)) { 4436163953Srrs /* Yep, we need to add a ECNE */ 4437163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4438163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4439163953Srrs } 4440163953Srrs } 4441163953Srrs} 4442163953Srrs 4443163953Srrs/* 4444163953Srrs * common input chunk processing (v4 and v6) 4445163953Srrs */ 4446163953Srrsint 4447163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4448163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4449163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4450163953Srrs uint8_t ecn_bits) 4451163953Srrs{ 4452163953Srrs /* 4453163953Srrs * Control chunk processing 4454163953Srrs */ 4455163953Srrs uint32_t high_tsn; 4456163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4457163953Srrs struct mbuf *m = *mm; 4458163953Srrs int abort_flag = 0; 4459163953Srrs int un_sent; 4460163953Srrs 4461163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4462163953Srrs#ifdef SCTP_AUDITING_ENABLED 4463163953Srrs sctp_audit_log(0xE0, 1); 4464163953Srrs sctp_auditing(0, inp, stcb, net); 4465163953Srrs#endif 4466163953Srrs 4467163953Srrs#ifdef SCTP_DEBUG 4468163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4469163996Srrs printf("Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4470163996Srrs m, iphlen, offset); 4471163953Srrs } 4472163953Srrs#endif /* SCTP_DEBUG */ 4473163953Srrs 4474163953Srrs if (stcb) { 4475163953Srrs /* always clear this before beginning a packet */ 4476163953Srrs stcb->asoc.authenticated = 0; 4477163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4478163953Srrs } 4479163953Srrs if (IS_SCTP_CONTROL(ch)) { 4480163953Srrs /* process the control portion of the SCTP packet */ 4481163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4482163953Srrs inp, stcb, &net, &fwd_tsn_seen); 4483163953Srrs if (stcb) { 4484163953Srrs /* 4485163953Srrs * This covers us if the cookie-echo was there and 4486163953Srrs * it changes our INP. 4487163953Srrs */ 4488163953Srrs inp = stcb->sctp_ep; 4489163953Srrs } 4490163953Srrs } else { 4491163953Srrs /* 4492163953Srrs * no control chunks, so pre-process DATA chunks (these 4493163953Srrs * checks are taken care of by control processing) 4494163953Srrs */ 4495163953Srrs 4496163953Srrs /* 4497163953Srrs * if DATA only packet, and auth is required, then punt... 4498163953Srrs * can't have authenticated without any AUTH (control) 4499163953Srrs * chunks 4500163953Srrs */ 4501163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4502163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4503163953Srrs stcb->asoc.local_auth_chunks)) { 4504163953Srrs /* "silently" ignore */ 4505163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4506163953Srrs SCTP_TCB_UNLOCK(stcb); 4507163953Srrs return (1); 4508163953Srrs } 4509163953Srrs if (stcb == NULL) { 4510163953Srrs /* out of the blue DATA chunk */ 4511163953Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL); 4512163953Srrs return (1); 4513163953Srrs } 4514163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4515163953Srrs /* v_tag mismatch! */ 4516163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4517163953Srrs SCTP_TCB_UNLOCK(stcb); 4518163953Srrs return (1); 4519163953Srrs } 4520163953Srrs } 4521163953Srrs 4522163953Srrs if (stcb == NULL) { 4523163953Srrs /* 4524163953Srrs * no valid TCB for this packet, or we found it's a bad 4525163953Srrs * packet while processing control, or we're done with this 4526163953Srrs * packet (done or skip rest of data), so we drop it... 4527163953Srrs */ 4528163953Srrs return (1); 4529163953Srrs } 4530163953Srrs /* 4531163953Srrs * DATA chunk processing 4532163953Srrs */ 4533163953Srrs /* plow through the data chunks while length > offset */ 4534163953Srrs 4535163953Srrs /* 4536163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4537163953Srrs * DATA is required. 4538163953Srrs */ 4539165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4540163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4541163953Srrs stcb->asoc.local_auth_chunks) && 4542163953Srrs !stcb->asoc.authenticated) { 4543163953Srrs /* "silently" ignore */ 4544163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4545163953Srrs#ifdef SCTP_DEBUG 4546163953Srrs if (sctp_debug_on & SCTP_DEBUG_AUTH1) 4547163953Srrs printf("Data chunk requires AUTH, skipped\n"); 4548163953Srrs#endif 4549165220Srrs goto trigger_send; 4550163953Srrs } 4551163953Srrs if (length > offset) { 4552163953Srrs int retval; 4553163953Srrs 4554163953Srrs /* 4555163953Srrs * First check to make sure our state is correct. We would 4556163953Srrs * not get here unless we really did have a tag, so we don't 4557163953Srrs * abort if this happens, just dump the chunk silently. 4558163953Srrs */ 4559163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4560163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4561163953Srrs /* 4562163953Srrs * we consider data with valid tags in this state 4563163953Srrs * shows us the cookie-ack was lost. Imply it was 4564163953Srrs * there. 4565163953Srrs */ 4566163953Srrs stcb->asoc.overall_error_count = 0; 4567163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4568163953Srrs break; 4569163953Srrs case SCTP_STATE_COOKIE_WAIT: 4570163953Srrs /* 4571163953Srrs * We consider OOTB any data sent during asoc setup. 4572163953Srrs */ 4573163953Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL); 4574163953Srrs SCTP_TCB_UNLOCK(stcb); 4575163953Srrs return (1); 4576163953Srrs break; 4577163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4578163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4579163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4580163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4581163953Srrs default: 4582163953Srrs SCTP_TCB_UNLOCK(stcb); 4583163953Srrs return (1); 4584163953Srrs break; 4585163953Srrs case SCTP_STATE_OPEN: 4586163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4587163953Srrs break; 4588163953Srrs } 4589163953Srrs /* take care of ECN, part 1. */ 4590163953Srrs if (stcb->asoc.ecn_allowed && 4591163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4592163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4593163953Srrs } 4594163953Srrs /* plow through the data chunks while length > offset */ 4595163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4596163953Srrs inp, stcb, net, &high_tsn); 4597163953Srrs if (retval == 2) { 4598163953Srrs /* 4599163953Srrs * The association aborted, NO UNLOCK needed since 4600163953Srrs * the association is destroyed. 4601163953Srrs */ 4602163953Srrs return (0); 4603163953Srrs } 4604163953Srrs data_processed = 1; 4605163953Srrs if (retval == 0) { 4606163953Srrs /* take care of ecn part 2. */ 4607163953Srrs if (stcb->asoc.ecn_allowed && 4608163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4609163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4610163953Srrs ecn_bits); 4611163953Srrs } 4612163953Srrs } 4613163953Srrs /* 4614163953Srrs * Anything important needs to have been m_copy'ed in 4615163953Srrs * process_data 4616163953Srrs */ 4617163953Srrs } 4618163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4619163953Srrs int was_a_gap = 0; 4620163953Srrs 4621163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4622163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4623163953Srrs /* there was a gap before this data was processed */ 4624163953Srrs was_a_gap = 1; 4625163953Srrs } 4626163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4627163953Srrs if (abort_flag) { 4628163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4629163953Srrs return (0); 4630163953Srrs } 4631163953Srrs } 4632163953Srrs /* trigger send of any chunks in queue... */ 4633165220Srrstrigger_send: 4634163953Srrs#ifdef SCTP_AUDITING_ENABLED 4635163953Srrs sctp_audit_log(0xE0, 2); 4636163953Srrs sctp_auditing(1, inp, stcb, net); 4637163953Srrs#endif 4638163953Srrs#ifdef SCTP_DEBUG 4639163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4640163953Srrs printf("Check for chunk output prw:%d tqe:%d tf=%d\n", 4641163953Srrs stcb->asoc.peers_rwnd, 4642163953Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4643163953Srrs stcb->asoc.total_flight); 4644163953Srrs } 4645163953Srrs#endif 4646163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4647163953Srrs 4648163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4649163953Srrs ((un_sent) && 4650163953Srrs (stcb->asoc.peers_rwnd > 0 || 4651163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4652163953Srrs#ifdef SCTP_DEBUG 4653163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4654163953Srrs printf("Calling chunk OUTPUT\n"); 4655163953Srrs } 4656163953Srrs#endif 4657163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4658163953Srrs#ifdef SCTP_DEBUG 4659163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4660163953Srrs printf("chunk OUTPUT returns\n"); 4661163953Srrs } 4662163953Srrs#endif 4663163953Srrs } 4664163953Srrs#ifdef SCTP_AUDITING_ENABLED 4665163953Srrs sctp_audit_log(0xE0, 3); 4666163953Srrs sctp_auditing(2, inp, stcb, net); 4667163953Srrs#endif 4668163953Srrs SCTP_TCB_UNLOCK(stcb); 4669163953Srrs return (0); 4670163953Srrs} 4671163953Srrs 4672163953Srrs 4673163953Srrs 4674163953Srrsvoid 4675165647Srrssctp_input(i_pak, off) 4676165647Srrs struct mbuf *i_pak; 4677163953Srrs int off; 4678163953Srrs 4679163953Srrs{ 4680163953Srrs#ifdef SCTP_MBUF_LOGGING 4681163953Srrs struct mbuf *mat; 4682163953Srrs 4683163953Srrs#endif 4684165647Srrs struct mbuf *m; 4685163953Srrs int iphlen; 4686163953Srrs uint8_t ecn_bits; 4687163953Srrs struct ip *ip; 4688163953Srrs struct sctphdr *sh; 4689163953Srrs struct sctp_inpcb *inp = NULL; 4690163953Srrs 4691163953Srrs uint32_t check, calc_check; 4692163953Srrs struct sctp_nets *net; 4693163953Srrs struct sctp_tcb *stcb = NULL; 4694163953Srrs struct sctp_chunkhdr *ch; 4695163953Srrs int refcount_up = 0; 4696163953Srrs int length, mlen, offset; 4697163953Srrs 4698163953Srrs 4699163953Srrs iphlen = off; 4700165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4701163953Srrs net = NULL; 4702163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4703163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4704163953Srrs 4705163953Srrs#ifdef SCTP_MBUF_LOGGING 4706163953Srrs /* Log in any input mbufs */ 4707163953Srrs mat = m; 4708163953Srrs while (mat) { 4709165647Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4710163953Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4711163953Srrs } 4712165647Srrs mat = SCTP_BUF_NEXT(mat); 4713163953Srrs } 4714163953Srrs#endif 4715168124Srrs 4716163953Srrs /* 4717163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4718163953Srrs */ 4719163953Srrs ip = mtod(m, struct ip *); 4720163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4721165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4722163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4723163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4724163953Srrs return; 4725163953Srrs } 4726163953Srrs ip = mtod(m, struct ip *); 4727163953Srrs } 4728163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4729163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4730163953Srrs 4731163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4732163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4733163953Srrs goto bad; 4734163953Srrs } 4735168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4736163953Srrs /* 4737163953Srrs * We only look at broadcast if its a front state, All 4738163953Srrs * others we will not have a tcb for anyway. 4739163953Srrs */ 4740163953Srrs goto bad; 4741163953Srrs } 4742163953Srrs /* validate SCTP checksum */ 4743168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4744163953Srrs /* 4745163953Srrs * we do NOT validate things from the loopback if the sysctl 4746163953Srrs * is set to 1. 4747163953Srrs */ 4748163953Srrs check = sh->checksum; /* save incoming checksum */ 4749163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4750163953Srrs /* 4751163953Srrs * special hook for where we got a local address 4752163953Srrs * somehow routed across a non IFT_LOOP type 4753163953Srrs * interface 4754163953Srrs */ 4755163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4756163953Srrs goto sctp_skip_csum_4; 4757163953Srrs } 4758163953Srrs sh->checksum = 0; /* prepare for calc */ 4759163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4760163953Srrs if (calc_check != check) { 4761163953Srrs#ifdef SCTP_DEBUG 4762163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4763163996Srrs printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4764163996Srrs calc_check, check, m, mlen, iphlen); 4765163953Srrs } 4766163953Srrs#endif 4767163953Srrs 4768163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4769163953Srrs offset - sizeof(*ch), 4770163953Srrs sh, ch, &inp, &net); 4771163953Srrs if ((inp) && (stcb)) { 4772163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4773163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4774163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4775163953Srrs refcount_up = 1; 4776163953Srrs } 4777163953Srrs SCTP_STAT_INCR(sctps_badsum); 4778163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4779163953Srrs goto bad; 4780163953Srrs } 4781163953Srrs sh->checksum = calc_check; 4782163953Srrs } else { 4783163953Srrssctp_skip_csum_4: 4784168124Srrs mlen = SCTP_HEADER_LEN(m); 4785163953Srrs } 4786168124Srrs 4787168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4788168124Srrs if (sh->dest_port == 0) { 4789168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4790168124Srrs goto bad; 4791168124Srrs } 4792163953Srrs /* validate mbuf chain length with IP payload length */ 4793163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4794163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4795163953Srrs goto bad; 4796163953Srrs } 4797163953Srrs /* 4798163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4799163953Srrs * IP/SCTP/first chunk header... 4800163953Srrs */ 4801163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4802163953Srrs sh, ch, &inp, &net); 4803163953Srrs /* inp's ref-count increased && stcb locked */ 4804163953Srrs if (inp == NULL) { 4805163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4806163953Srrs 4807163953Srrs SCTP_STAT_INCR(sctps_noport); 4808163953Srrs#ifdef ICMP_BANDLIM 4809163953Srrs /* 4810163953Srrs * we use the bandwidth limiting to protect against sending 4811163953Srrs * too many ABORTS all at once. In this case these count the 4812163953Srrs * same as an ICMP message. 4813163953Srrs */ 4814163953Srrs if (badport_bandlim(0) < 0) 4815163953Srrs goto bad; 4816163953Srrs#endif /* ICMP_BANDLIM */ 4817163953Srrs#ifdef SCTP_DEBUG 4818163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4819163953Srrs printf("Sending a ABORT from packet entry!\n"); 4820163953Srrs } 4821163953Srrs#endif 4822163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4823163953Srrs /* 4824163953Srrs * we do a trick here to get the INIT tag, dig in 4825163953Srrs * and get the tag from the INIT and put it in the 4826163953Srrs * common header. 4827163953Srrs */ 4828163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4829163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4830163953Srrs (uint8_t *) & chunk_buf); 4831163953Srrs if (init_chk != NULL) 4832163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4833163953Srrs } 4834165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4835165220Srrs sctp_send_shutdown_complete2(m, iphlen, sh); 4836165220Srrs goto bad; 4837165220Srrs } 4838165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4839165220Srrs goto bad; 4840165220Srrs } 4841165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4842165220Srrs sctp_send_abort(m, iphlen, sh, 0, NULL); 4843163953Srrs goto bad; 4844163953Srrs } else if (stcb == NULL) { 4845163953Srrs refcount_up = 1; 4846163953Srrs } 4847163953Srrs#ifdef IPSEC 4848163953Srrs /* 4849163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 4850163953Srrs * idea, so I will leave it in place. 4851163953Srrs */ 4852163953Srrs 4853163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 4854163953Srrs ipsecstat.in_polvio++; 4855163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4856163953Srrs goto bad; 4857163953Srrs } 4858163953Srrs#endif /* IPSEC */ 4859163953Srrs 4860163953Srrs 4861163953Srrs 4862163953Srrs /* 4863163953Srrs * common chunk processing 4864163953Srrs */ 4865163953Srrs length = ip->ip_len + iphlen; 4866163953Srrs offset -= sizeof(struct sctp_chunkhdr); 4867163953Srrs 4868163953Srrs ecn_bits = ip->ip_tos; 4869163953Srrs 4870163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 4871163953Srrs inp, stcb, net, ecn_bits); 4872163953Srrs /* inp's ref-count reduced && stcb unlocked */ 4873163953Srrs if (m) { 4874163953Srrs sctp_m_freem(m); 4875163953Srrs } 4876163953Srrs if ((inp) && (refcount_up)) { 4877163953Srrs /* reduce ref-count */ 4878163953Srrs SCTP_INP_WLOCK(inp); 4879163953Srrs SCTP_INP_DECR_REF(inp); 4880163953Srrs SCTP_INP_WUNLOCK(inp); 4881163953Srrs } 4882163953Srrs return; 4883163953Srrsbad: 4884163953Srrs if (stcb) 4885163953Srrs SCTP_TCB_UNLOCK(stcb); 4886163953Srrs 4887163953Srrs if ((inp) && (refcount_up)) { 4888163953Srrs /* reduce ref-count */ 4889163953Srrs SCTP_INP_WLOCK(inp); 4890163953Srrs SCTP_INP_DECR_REF(inp); 4891163953Srrs SCTP_INP_WUNLOCK(inp); 4892163953Srrs } 4893163953Srrs if (m) { 4894163953Srrs sctp_m_freem(m); 4895163953Srrs } 4896163953Srrs return; 4897163953Srrs} 4898