sctp_input.c revision 172156
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by 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 172156 2007-09-13 10:36:43Z 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> 47170091Srrs#include <netinet/sctp_bsd_addr.h> 48172091Srrs#include <netinet/sctp_timer.h> 49163953Srrs 50163953Srrs 51163953Srrs 52163953Srrsstatic void 53163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 54163953Srrs{ 55163953Srrs struct sctp_nets *net; 56163953Srrs 57165220Srrs /* 58165220Srrs * This now not only stops all cookie timers it also stops any INIT 59165220Srrs * timers as well. This will make sure that the timers are stopped 60165220Srrs * in all collision cases. 61165220Srrs */ 62163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 63163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 64165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 65163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 66163953Srrs stcb->sctp_ep, 67163953Srrs stcb, 68165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 69165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 70165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 71165220Srrs stcb->sctp_ep, 72165220Srrs stcb, 73165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 74163953Srrs } 75163953Srrs } 76163953Srrs} 77163953Srrs 78163953Srrs/* INIT handler */ 79163953Srrsstatic void 80163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 81163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 82170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 83163953Srrs{ 84163953Srrs struct sctp_init *init; 85163953Srrs struct mbuf *op_err; 86163953Srrs uint32_t init_limit; 87163953Srrs 88169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 89169420Srrs stcb); 90163953Srrs op_err = NULL; 91163953Srrs init = &cp->init; 92163953Srrs /* First are we accepting? */ 93163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 94169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 95169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 96169420Srrs inp->sctp_socket->so_qlimit); 97163953Srrs /* 98163953Srrs * FIX ME ?? What about TCP model and we have a 99163953Srrs * match/restart case? 100163953Srrs */ 101169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 102170181Srrs vrf_id); 103168299Srrs if (stcb) 104168299Srrs *abort_no_unlock = 1; 105163953Srrs return; 106163953Srrs } 107163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 108163953Srrs /* Invalid length */ 109163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 110169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 111170181Srrs vrf_id); 112168299Srrs if (stcb) 113168299Srrs *abort_no_unlock = 1; 114163953Srrs return; 115163953Srrs } 116163953Srrs /* validate parameters */ 117163953Srrs if (init->initiate_tag == 0) { 118163953Srrs /* protocol error... send abort */ 119163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 120169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121170181Srrs vrf_id); 122168299Srrs if (stcb) 123168299Srrs *abort_no_unlock = 1; 124163953Srrs return; 125163953Srrs } 126163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 127163953Srrs /* invalid parameter... send abort */ 128163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 129169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 130170181Srrs vrf_id); 131171440Srrs if (stcb) 132171440Srrs *abort_no_unlock = 1; 133163953Srrs return; 134163953Srrs } 135163953Srrs if (init->num_inbound_streams == 0) { 136163953Srrs /* protocol error... send abort */ 137163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 138169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 139170181Srrs vrf_id); 140168299Srrs if (stcb) 141168299Srrs *abort_no_unlock = 1; 142163953Srrs return; 143163953Srrs } 144163953Srrs if (init->num_outbound_streams == 0) { 145163953Srrs /* protocol error... send abort */ 146163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 147169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 148170181Srrs vrf_id); 149168299Srrs if (stcb) 150168299Srrs *abort_no_unlock = 1; 151163953Srrs return; 152163953Srrs } 153163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 154163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 155163953Srrs init_limit)) { 156163953Srrs /* auth parameter(s) error... send abort */ 157170181Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id); 158168299Srrs if (stcb) 159168299Srrs *abort_no_unlock = 1; 160163953Srrs return; 161163953Srrs } 162163953Srrs /* send an INIT-ACK w/cookie */ 163169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 164170181Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id); 165163953Srrs} 166163953Srrs 167163953Srrs/* 168163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 169163953Srrs */ 170171158Srrs 171171158Srrsint 172171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 173171158Srrs{ 174171158Srrs int unsent_data = 0; 175171158Srrs struct sctp_stream_queue_pending *sp; 176171158Srrs struct sctp_stream_out *strq; 177171158Srrs struct sctp_association *asoc; 178171158Srrs 179171158Srrs /* 180171158Srrs * This function returns the number of streams that have true unsent 181171158Srrs * data on them. Note that as it looks through it will clean up any 182171158Srrs * places that have old data that has been sent but left at top of 183171158Srrs * stream queue. 184171158Srrs */ 185171158Srrs asoc = &stcb->asoc; 186171158Srrs SCTP_TCB_SEND_LOCK(stcb); 187171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 188171158Srrs /* Check to see if some data queued */ 189171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 190171531Srrs is_there_another: 191171158Srrs /* sa_ignore FREED_MEMORY */ 192171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 193171158Srrs if (sp == NULL) { 194171158Srrs continue; 195171158Srrs } 196171158Srrs if ((sp->msg_is_complete) && 197171158Srrs (sp->length == 0) && 198171158Srrs (sp->sender_all_done)) { 199171158Srrs /* 200171158Srrs * We are doing differed cleanup. Last time 201171158Srrs * through when we took all the data the 202171158Srrs * sender_all_done was not set. 203171158Srrs */ 204171158Srrs if (sp->put_last_out == 0) { 205171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 206171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 207171158Srrs sp->sender_all_done, 208171158Srrs sp->length, 209171158Srrs sp->msg_is_complete, 210171158Srrs sp->put_last_out); 211171158Srrs } 212171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 213171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 214171158Srrs sctp_free_remote_addr(sp->net); 215171158Srrs if (sp->data) { 216171158Srrs sctp_m_freem(sp->data); 217171158Srrs sp->data = NULL; 218171158Srrs } 219171158Srrs sctp_free_a_strmoq(stcb, sp); 220171158Srrs goto is_there_another; 221171158Srrs } else { 222171158Srrs unsent_data++; 223171158Srrs continue; 224171158Srrs } 225171158Srrs } 226171158Srrs } 227171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 228171158Srrs return (unsent_data); 229171158Srrs} 230171158Srrs 231163953Srrsstatic int 232163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 233163953Srrs struct sctp_nets *net) 234163953Srrs{ 235163953Srrs struct sctp_init *init; 236163953Srrs struct sctp_association *asoc; 237163953Srrs struct sctp_nets *lnet; 238163953Srrs unsigned int i; 239163953Srrs 240163953Srrs init = &cp->init; 241163953Srrs asoc = &stcb->asoc; 242163953Srrs /* save off parameters */ 243163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 244163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 245163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 246163953Srrs /* update any ssthresh's that may have a default */ 247163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 248163953Srrs lnet->ssthresh = asoc->peers_rwnd; 249163953Srrs 250170744Srrs if (sctp_logging_level & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 251170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 252170744Srrs } 253163953Srrs } 254163953Srrs } 255164181Srrs SCTP_TCB_SEND_LOCK(stcb); 256163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 257163953Srrs unsigned int newcnt; 258163953Srrs struct sctp_stream_out *outs; 259163953Srrs struct sctp_stream_queue_pending *sp; 260163953Srrs 261163953Srrs /* cut back on number of streams */ 262163953Srrs newcnt = ntohs(init->num_inbound_streams); 263163953Srrs /* This if is probably not needed but I am cautious */ 264163953Srrs if (asoc->strmout) { 265163953Srrs /* First make sure no data chunks are trapped */ 266163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 267163953Srrs outs = &asoc->strmout[i]; 268163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 269163953Srrs while (sp) { 270163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 271163953Srrs next); 272163953Srrs asoc->stream_queue_cnt--; 273163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 274163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 275172090Srrs sp, SCTP_SO_NOT_LOCKED); 276163953Srrs if (sp->data) { 277163953Srrs sctp_m_freem(sp->data); 278163953Srrs sp->data = NULL; 279163953Srrs } 280163953Srrs sctp_free_remote_addr(sp->net); 281163953Srrs sp->net = NULL; 282163953Srrs /* Free the chunk */ 283169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 284163979Sru sp, stcb); 285163953Srrs 286163953Srrs sctp_free_a_strmoq(stcb, sp); 287169655Srrs /* sa_ignore FREED_MEMORY */ 288163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 289163953Srrs } 290163953Srrs } 291163953Srrs } 292163953Srrs /* cut back the count and abandon the upper streams */ 293163953Srrs asoc->pre_open_streams = newcnt; 294163953Srrs } 295164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 296163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 297163953Srrs /* init tsn's */ 298163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 299170744Srrs if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) { 300170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 301170744Srrs } 302163953Srrs /* This is the next one we expect */ 303163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 304163953Srrs 305163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 306163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 307163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 308163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 309163953Srrs /* open the requested streams */ 310170091Srrs 311163953Srrs if (asoc->strmin != NULL) { 312163953Srrs /* Free the old ones */ 313164181Srrs struct sctp_queued_to_read *ctl; 314164181Srrs 315164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 316164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 317164181Srrs while (ctl) { 318164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 319164181Srrs sctp_free_remote_addr(ctl->whoFrom); 320171158Srrs ctl->whoFrom = NULL; 321164181Srrs sctp_m_freem(ctl->data); 322164181Srrs ctl->data = NULL; 323164181Srrs sctp_free_a_readq(stcb, ctl); 324164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 325164181Srrs } 326164181Srrs } 327170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 328163953Srrs } 329164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 330164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 331164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 332164181Srrs } 333163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 334170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 335163953Srrs if (asoc->strmin == NULL) { 336163953Srrs /* we didn't get memory for the streams! */ 337169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 338163953Srrs return (-1); 339163953Srrs } 340163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 341163953Srrs asoc->strmin[i].stream_no = i; 342163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 343163953Srrs /* 344163953Srrs * U-stream ranges will be set when the cookie is unpacked. 345163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 346163953Srrs * supported) when the INIT-ACK arrives. 347163953Srrs */ 348163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 349168943Srrs asoc->strmin[i].delivery_started = 0; 350163953Srrs } 351163953Srrs /* 352163953Srrs * load_address_from_init will put the addresses into the 353163953Srrs * association when the COOKIE is processed or the INIT-ACK is 354163953Srrs * processed. Both types of COOKIE's existing and new call this 355163953Srrs * routine. It will remove addresses that are no longer in the 356163953Srrs * association (for the restarting case where addresses are 357163953Srrs * removed). Up front when the INIT arrives we will discard it if it 358163953Srrs * is a restart and new addresses have been added. 359163953Srrs */ 360169655Srrs /* sa_ignore MEMLEAK */ 361163953Srrs return (0); 362163953Srrs} 363163953Srrs 364163953Srrs/* 365163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 366163953Srrs */ 367163953Srrsstatic int 368163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 369163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 370170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 371163953Srrs{ 372163953Srrs struct sctp_association *asoc; 373163953Srrs struct mbuf *op_err; 374163953Srrs int retval, abort_flag; 375163953Srrs uint32_t initack_limit; 376163953Srrs 377163953Srrs /* First verify that we have no illegal param's */ 378163953Srrs abort_flag = 0; 379163953Srrs op_err = NULL; 380163953Srrs 381163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 382163953Srrs (offset + sizeof(struct sctp_init_chunk)), 383163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 384163953Srrs if (abort_flag) { 385163953Srrs /* Send an abort and notify peer */ 386172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 387170056Srrs *abort_no_unlock = 1; 388163953Srrs return (-1); 389163953Srrs } 390163953Srrs asoc = &stcb->asoc; 391163953Srrs /* process the peer's parameters in the INIT-ACK */ 392163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 393163953Srrs if (retval < 0) { 394163953Srrs return (retval); 395163953Srrs } 396163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 397163953Srrs /* load all addresses */ 398163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 399163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 400163953Srrs NULL))) { 401163953Srrs /* Huh, we should abort */ 402169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 403169420Srrs "Load addresses from INIT causes an abort %d\n", 404169420Srrs retval); 405163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 406170181Srrs NULL, 0); 407168299Srrs *abort_no_unlock = 1; 408163953Srrs return (-1); 409163953Srrs } 410171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 411171477Srrs if (asoc->peer_supports_asconf == 0) { 412171477Srrs struct sctp_asconf_addr *aparam; 413171477Srrs 414171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 415171477Srrs /* sa_ignore FREED_MEMORY */ 416171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 417171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 418171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 419171477Srrs } 420171477Srrs } 421163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 422163953Srrs stcb->asoc.local_hmacs); 423163953Srrs if (op_err) { 424163953Srrs sctp_queue_op_err(stcb, op_err); 425163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 426163953Srrs op_err = NULL; 427163953Srrs } 428163953Srrs /* extract the cookie and queue it to "echo" it back... */ 429171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 430171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 431171943Srrs stcb->asoc.overall_error_count, 432171943Srrs 0, 433171943Srrs SCTP_FROM_SCTP_INPUT, 434171943Srrs __LINE__); 435171943Srrs } 436163953Srrs stcb->asoc.overall_error_count = 0; 437163953Srrs net->error_count = 0; 438163953Srrs 439163953Srrs /* 440163953Srrs * Cancel the INIT timer, We do this first before queueing the 441163953Srrs * cookie. We always cancel at the primary to assue that we are 442163953Srrs * canceling the timer started by the INIT which always goes to the 443163953Srrs * primary. 444163953Srrs */ 445163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 446165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 447163953Srrs 448165220Srrs /* calculate the RTO */ 449171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 450165220Srrs 451163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 452163953Srrs if (retval < 0) { 453163953Srrs /* 454163953Srrs * No cookie, we probably should send a op error. But in any 455163953Srrs * case if there is no cookie in the INIT-ACK, we can 456163953Srrs * abandon the peer, its broke. 457163953Srrs */ 458163953Srrs if (retval == -3) { 459163953Srrs /* We abort with an error of missing mandatory param */ 460163953Srrs op_err = 461163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 462163953Srrs if (op_err) { 463163953Srrs /* 464163953Srrs * Expand beyond to include the mandatory 465163953Srrs * param cookie 466163953Srrs */ 467163953Srrs struct sctp_inv_mandatory_param *mp; 468163953Srrs 469165647Srrs SCTP_BUF_LEN(op_err) = 470163953Srrs sizeof(struct sctp_inv_mandatory_param); 471163953Srrs mp = mtod(op_err, 472163953Srrs struct sctp_inv_mandatory_param *); 473163953Srrs /* Subtract the reserved param */ 474163953Srrs mp->length = 475163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 476163953Srrs mp->num_param = htonl(1); 477163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 478163953Srrs mp->resv = 0; 479163953Srrs } 480163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 481170181Srrs sh, op_err, 0); 482168299Srrs *abort_no_unlock = 1; 483163953Srrs } 484163953Srrs return (retval); 485163953Srrs } 486163953Srrs return (0); 487163953Srrs} 488163953Srrs 489163953Srrsstatic void 490163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 491163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 492163953Srrs{ 493163953Srrs struct sockaddr_storage store; 494163953Srrs struct sockaddr_in *sin; 495163953Srrs struct sockaddr_in6 *sin6; 496163953Srrs struct sctp_nets *r_net; 497163953Srrs struct timeval tv; 498163953Srrs 499163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 500163953Srrs /* Invalid length */ 501163953Srrs return; 502163953Srrs } 503163953Srrs sin = (struct sockaddr_in *)&store; 504163953Srrs sin6 = (struct sockaddr_in6 *)&store; 505163953Srrs 506163953Srrs memset(&store, 0, sizeof(store)); 507163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 508163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 509163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 510163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 511163953Srrs sin->sin_port = stcb->rport; 512163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 513163953Srrs sizeof(sin->sin_addr)); 514163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 515163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 516163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 517163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 518163953Srrs sin6->sin6_port = stcb->rport; 519163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 520163953Srrs sizeof(sin6->sin6_addr)); 521163953Srrs } else { 522163953Srrs return; 523163953Srrs } 524163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 525163953Srrs if (r_net == NULL) { 526169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 527163953Srrs return; 528163953Srrs } 529163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 530163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 531163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 532163953Srrs /* 533163953Srrs * If the its a HB and it's random value is correct when can 534163953Srrs * confirm the destination. 535163953Srrs */ 536163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 537167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 538167598Srrs stcb->asoc.primary_destination = r_net; 539167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 540167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 541167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 542167598Srrs if (r_net != stcb->asoc.primary_destination) { 543167598Srrs /* 544167598Srrs * first one on the list is NOT the primary 545167598Srrs * sctp_cmpaddr() is much more efficent if 546167598Srrs * the primary is the first on the list, 547167598Srrs * make it so. 548167598Srrs */ 549167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 550167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 551167598Srrs } 552172091Srrs /* Mobility adaptation */ 553172091Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 554172091Srrs SCTP_MOBILITY_BASE) || 555172091Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 556172091Srrs SCTP_MOBILITY_FASTHANDOFF)) && 557172091Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 558172091Srrs SCTP_MOBILITY_PRIM_DELETED)) { 559172091Srrs 560172091Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 561172091Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 562172091Srrs SCTP_MOBILITY_FASTHANDOFF)) { 563172091Srrs sctp_assoc_immediate_retrans(stcb, 564172091Srrs stcb->asoc.primary_destination); 565172091Srrs } 566172091Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 567172091Srrs SCTP_MOBILITY_BASE)) { 568172091Srrs sctp_move_chunks_from_deleted_prim(stcb, 569172091Srrs stcb->asoc.primary_destination); 570172091Srrs } 571172091Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 572172091Srrs stcb->asoc.deleted_primary); 573172091Srrs } 574167598Srrs } 575163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 576172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 577163953Srrs } 578163953Srrs r_net->error_count = 0; 579163953Srrs r_net->hb_responded = 1; 580163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 581163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 582163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 583163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 584163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 585163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 586172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 587163953Srrs /* now was it the primary? if so restore */ 588163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 589169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 590163953Srrs } 591163953Srrs } 592171440Srrs /* 593171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 594171440Srrs * set the destination to active state and set the cwnd to one or 595171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 596171440Srrs * timer is running, for the destination, stop the timer because a 597171440Srrs * PF-heartbeat was received. 598171440Srrs */ 599171477Srrs if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == 600171440Srrs SCTP_ADDR_PF) { 601171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 602171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 603171440Srrs stcb, net, 604171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 605171440Srrs } 606171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 607171440Srrs net->cwnd = net->mtu * sctp_cmt_pf; 608171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 609171440Srrs net, net->cwnd); 610171440Srrs } 611163953Srrs /* Now lets do a RTO with this */ 612171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 613163953Srrs} 614163953Srrs 615163953Srrsstatic void 616163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 617163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 618163953Srrs{ 619172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 620172090Srrs struct socket *so; 621172090Srrs 622172090Srrs#endif 623172090Srrs 624169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 625163953Srrs if (stcb == NULL) 626163953Srrs return; 627163953Srrs 628163953Srrs /* stop any receive timers */ 629171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 630163953Srrs /* notify user of the abort and clean up... */ 631172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 632163953Srrs /* free the tcb */ 633163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 634163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 635163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 636163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 637163953Srrs } 638168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 639168859Srrs sctp_print_out_track_log(stcb); 640168859Srrs#endif 641172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 642172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 643172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 644172090Srrs SCTP_TCB_UNLOCK(stcb); 645172090Srrs SCTP_SOCKET_LOCK(so, 1); 646172090Srrs SCTP_TCB_LOCK(stcb); 647172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 648172090Srrs#endif 649171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 650171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 651171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 652172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 653172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 654172090Srrs#endif 655169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 656163953Srrs} 657163953Srrs 658163953Srrsstatic void 659163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 660163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 661163953Srrs{ 662163953Srrs struct sctp_association *asoc; 663163953Srrs int some_on_streamwheel; 664163953Srrs 665172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 666172090Srrs struct socket *so; 667172090Srrs 668172090Srrs#endif 669172090Srrs 670169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 671169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 672163953Srrs if (stcb == NULL) 673163953Srrs return; 674165220Srrs asoc = &stcb->asoc; 675165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 676165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 677163953Srrs return; 678163953Srrs } 679163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 680163953Srrs /* Shutdown NOT the expected size */ 681163953Srrs return; 682163953Srrs } else { 683163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 684163953Srrs } 685165220Srrs if (asoc->control_pdapi) { 686163953Srrs /* 687163953Srrs * With a normal shutdown we assume the end of last record. 688163953Srrs */ 689163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 690165220Srrs asoc->control_pdapi->end_added = 1; 691165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 692165220Srrs asoc->control_pdapi = NULL; 693163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 694172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 695172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 696172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 697172090Srrs SCTP_TCB_UNLOCK(stcb); 698172090Srrs SCTP_SOCKET_LOCK(so, 1); 699172090Srrs SCTP_TCB_LOCK(stcb); 700172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 701172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 702172090Srrs /* assoc was freed while we were unlocked */ 703172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 704172090Srrs return; 705172090Srrs } 706172090Srrs#endif 707163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 708172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 709172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 710172090Srrs#endif 711163953Srrs } 712163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 713163953Srrs if (stcb->sctp_socket) { 714163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 715170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 716163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 717171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 718163953Srrs /* 719163953Srrs * notify upper layer that peer has initiated a 720163953Srrs * shutdown 721163953Srrs */ 722172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 723163953Srrs 724163953Srrs /* reset time */ 725169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 726163953Srrs } 727163953Srrs } 728163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 729163953Srrs /* 730163953Srrs * stop the shutdown timer, since we WILL move to 731163953Srrs * SHUTDOWN-ACK-SENT. 732163953Srrs */ 733171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 734163953Srrs } 735171158Srrs /* Now is there unsent data on a stream somewhere? */ 736171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 737163953Srrs 738163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 739163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 740163953Srrs some_on_streamwheel) { 741163953Srrs /* By returning we will push more data out */ 742163953Srrs return; 743163953Srrs } else { 744163953Srrs /* no outstanding data to send, so move on... */ 745163953Srrs /* send SHUTDOWN-ACK */ 746163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 747163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 748166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 749166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 750163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 751163953Srrs } 752171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 753163953Srrs 754171990Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, 755171990Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 756163953Srrs /* start SHUTDOWN timer */ 757163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 758163953Srrs stcb, net); 759163953Srrs } 760163953Srrs} 761163953Srrs 762163953Srrsstatic void 763163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 764163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 765163953Srrs{ 766163953Srrs struct sctp_association *asoc; 767163953Srrs 768172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 769172090Srrs struct socket *so; 770172090Srrs 771172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 772172090Srrs#endif 773169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 774169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 775163953Srrs if (stcb == NULL) 776163953Srrs return; 777163953Srrs 778163953Srrs asoc = &stcb->asoc; 779163953Srrs /* process according to association state */ 780163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 781163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 782163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 783163953Srrs SCTP_TCB_UNLOCK(stcb); 784163953Srrs return; 785163953Srrs } 786165220Srrs if (asoc->control_pdapi) { 787163953Srrs /* 788163953Srrs * With a normal shutdown we assume the end of last record. 789163953Srrs */ 790163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 791165220Srrs asoc->control_pdapi->end_added = 1; 792165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 793165220Srrs asoc->control_pdapi = NULL; 794163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 795172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 796172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 797172090Srrs SCTP_TCB_UNLOCK(stcb); 798172090Srrs SCTP_SOCKET_LOCK(so, 1); 799172090Srrs SCTP_TCB_LOCK(stcb); 800172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 801172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 802172090Srrs /* assoc was freed while we were unlocked */ 803172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 804172090Srrs return; 805172090Srrs } 806172090Srrs#endif 807163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 808172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 809172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 810172090Srrs#endif 811163953Srrs } 812163953Srrs /* are the queues empty? */ 813163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 814163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 815163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 816172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 817163953Srrs } 818163953Srrs /* stop the timer */ 819171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 820163953Srrs /* send SHUTDOWN-COMPLETE */ 821163953Srrs sctp_send_shutdown_complete(stcb, net); 822163953Srrs /* notify upper layer protocol */ 823163953Srrs if (stcb->sctp_socket) { 824172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 825163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 826163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 827163953Srrs /* Set the connected flag to disconnected */ 828163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 829163953Srrs } 830163953Srrs } 831163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 832163953Srrs /* free the TCB but first save off the ep */ 833172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 834172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 835172090Srrs SCTP_TCB_UNLOCK(stcb); 836172090Srrs SCTP_SOCKET_LOCK(so, 1); 837172090Srrs SCTP_TCB_LOCK(stcb); 838172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 839172090Srrs#endif 840171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 841171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 842172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 843172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 844172090Srrs#endif 845163953Srrs} 846163953Srrs 847163953Srrs/* 848163953Srrs * Skip past the param header and then we will find the chunk that caused the 849163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 850163953Srrs * our peer must be broken. 851163953Srrs */ 852163953Srrsstatic void 853163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 854163953Srrs struct sctp_nets *net) 855163953Srrs{ 856163953Srrs struct sctp_chunkhdr *chk; 857163953Srrs 858163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 859163953Srrs switch (chk->chunk_type) { 860163953Srrs case SCTP_ASCONF_ACK: 861163953Srrs case SCTP_ASCONF: 862163953Srrs sctp_asconf_cleanup(stcb, net); 863163953Srrs break; 864163953Srrs case SCTP_FORWARD_CUM_TSN: 865163953Srrs stcb->asoc.peer_supports_prsctp = 0; 866163953Srrs break; 867163953Srrs default: 868169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 869169420Srrs "Peer does not support chunk type %d(%x)??\n", 870169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 871163953Srrs break; 872163953Srrs } 873163953Srrs} 874163953Srrs 875163953Srrs/* 876163953Srrs * Skip past the param header and then we will find the param that caused the 877163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 878163953Srrs * these will turn of specific features. 879163953Srrs */ 880163953Srrsstatic void 881163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 882163953Srrs{ 883163953Srrs struct sctp_paramhdr *pbad; 884163953Srrs 885163953Srrs pbad = phdr + 1; 886163953Srrs switch (ntohs(pbad->param_type)) { 887163953Srrs /* pr-sctp draft */ 888163953Srrs case SCTP_PRSCTP_SUPPORTED: 889163953Srrs stcb->asoc.peer_supports_prsctp = 0; 890163953Srrs break; 891163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 892163953Srrs break; 893163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 894163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 895163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 896163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 897163953Srrs stcb->asoc.ecn_allowed = 0; 898163953Srrs break; 899163953Srrs case SCTP_ADD_IP_ADDRESS: 900163953Srrs case SCTP_DEL_IP_ADDRESS: 901163953Srrs case SCTP_SET_PRIM_ADDR: 902163953Srrs stcb->asoc.peer_supports_asconf = 0; 903163953Srrs break; 904163953Srrs case SCTP_SUCCESS_REPORT: 905163953Srrs case SCTP_ERROR_CAUSE_IND: 906169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 907169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 908169420Srrs "Turning off ASCONF to this strange peer\n"); 909163953Srrs stcb->asoc.peer_supports_asconf = 0; 910163953Srrs break; 911163953Srrs default: 912169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 913169420Srrs "Peer does not support param type %d(%x)??\n", 914169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 915163953Srrs break; 916163953Srrs } 917163953Srrs} 918163953Srrs 919163953Srrsstatic int 920163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 921163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 922163953Srrs{ 923163953Srrs int chklen; 924163953Srrs struct sctp_paramhdr *phdr; 925163953Srrs uint16_t error_type; 926163953Srrs uint16_t error_len; 927163953Srrs struct sctp_association *asoc; 928163953Srrs int adjust; 929163953Srrs 930172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 931172090Srrs struct socket *so; 932172090Srrs 933172090Srrs#endif 934172090Srrs 935163953Srrs /* parse through all of the errors and process */ 936163953Srrs asoc = &stcb->asoc; 937163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 938163953Srrs sizeof(struct sctp_chunkhdr)); 939163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 940163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 941163953Srrs /* Process an Error Cause */ 942163953Srrs error_type = ntohs(phdr->param_type); 943163953Srrs error_len = ntohs(phdr->param_length); 944163953Srrs if ((error_len > chklen) || (error_len == 0)) { 945163953Srrs /* invalid param length for this param */ 946169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 947169420Srrs chklen, error_len); 948163953Srrs return (0); 949163953Srrs } 950163953Srrs switch (error_type) { 951163953Srrs case SCTP_CAUSE_INVALID_STREAM: 952163953Srrs case SCTP_CAUSE_MISSING_PARAM: 953163953Srrs case SCTP_CAUSE_INVALID_PARAM: 954163953Srrs case SCTP_CAUSE_NO_USER_DATA: 955169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 956169420Srrs error_type); 957163953Srrs break; 958163953Srrs case SCTP_CAUSE_STALE_COOKIE: 959163953Srrs /* 960163953Srrs * We only act if we have echoed a cookie and are 961163953Srrs * waiting. 962163953Srrs */ 963163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 964163953Srrs int *p; 965163953Srrs 966163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 967163953Srrs /* Save the time doubled */ 968163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 969163953Srrs asoc->stale_cookie_count++; 970163953Srrs if (asoc->stale_cookie_count > 971163953Srrs asoc->max_init_times) { 972172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 973163953Srrs /* now free the asoc */ 974172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 975172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 976172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 977172090Srrs SCTP_TCB_UNLOCK(stcb); 978172090Srrs SCTP_SOCKET_LOCK(so, 1); 979172090Srrs SCTP_TCB_LOCK(stcb); 980172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 981172090Srrs#endif 982171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 983171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 984172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 985172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 986172090Srrs#endif 987163953Srrs return (-1); 988163953Srrs } 989163953Srrs /* blast back to INIT state */ 990163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 991163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 992163953Srrs 993163953Srrs sctp_stop_all_cookie_timers(stcb); 994172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 995163953Srrs } 996163953Srrs break; 997163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 998163953Srrs /* 999163953Srrs * Nothing we can do here, we don't do hostname 1000163953Srrs * addresses so if the peer does not like my IPv6 1001163953Srrs * (or IPv4 for that matter) it does not matter. If 1002163953Srrs * they don't support that type of address, they can 1003163953Srrs * NOT possibly get that packet type... i.e. with no 1004163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1005163953Srrs * safely ignore this one. If we ever added support 1006163953Srrs * for HOSTNAME Addresses, then we would need to do 1007163953Srrs * something here. 1008163953Srrs */ 1009163953Srrs break; 1010163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1011163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1012163953Srrs break; 1013163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1014163953Srrs sctp_process_unrecog_param(stcb, phdr); 1015163953Srrs break; 1016163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1017163953Srrs /* 1018163953Srrs * We ignore this since the timer will drive out a 1019163953Srrs * new cookie anyway and there timer will drive us 1020163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1021163953Srrs * here since we don't have their tag. 1022163953Srrs */ 1023163953Srrs break; 1024163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1025163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1026163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1027163953Srrs /* 1028163953Srrs * We should NOT get these here, but in a 1029166086Srrs * ASCONF-ACK. 1030163953Srrs */ 1031169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1032169420Srrs error_type); 1033163953Srrs break; 1034163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1035163953Srrs /* 1036163953Srrs * And what, pray tell do we do with the fact that 1037163953Srrs * the peer is out of resources? Not really sure we 1038166086Srrs * could do anything but abort. I suspect this 1039163953Srrs * should have came WITH an abort instead of in a 1040163953Srrs * OP-ERROR. 1041163953Srrs */ 1042163953Srrs break; 1043163953Srrs default: 1044169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1045169420Srrs error_type); 1046163953Srrs break; 1047163953Srrs } 1048163953Srrs adjust = SCTP_SIZE32(error_len); 1049163953Srrs chklen -= adjust; 1050163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1051163953Srrs } 1052163953Srrs return (0); 1053163953Srrs} 1054163953Srrs 1055163953Srrsstatic int 1056169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1057169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1058170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1059163953Srrs{ 1060163953Srrs struct sctp_init_ack *init_ack; 1061163953Srrs struct mbuf *op_err; 1062163953Srrs 1063169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1064169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1065169420Srrs 1066163953Srrs if (stcb == NULL) { 1067169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1068169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1069163953Srrs return (-1); 1070163953Srrs } 1071163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1072163953Srrs /* Invalid length */ 1073163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1074163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1075170181Srrs op_err, 0); 1076168299Srrs *abort_no_unlock = 1; 1077163953Srrs return (-1); 1078163953Srrs } 1079163953Srrs init_ack = &cp->init; 1080163953Srrs /* validate parameters */ 1081163953Srrs if (init_ack->initiate_tag == 0) { 1082163953Srrs /* protocol error... send an abort */ 1083163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1084163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1085170181Srrs op_err, 0); 1086168299Srrs *abort_no_unlock = 1; 1087163953Srrs return (-1); 1088163953Srrs } 1089163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1090163953Srrs /* protocol error... send an abort */ 1091163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1092163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1093170181Srrs op_err, 0); 1094168299Srrs *abort_no_unlock = 1; 1095163953Srrs return (-1); 1096163953Srrs } 1097163953Srrs if (init_ack->num_inbound_streams == 0) { 1098163953Srrs /* protocol error... send an abort */ 1099163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1100163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1101170181Srrs op_err, 0); 1102168299Srrs *abort_no_unlock = 1; 1103163953Srrs return (-1); 1104163953Srrs } 1105163953Srrs if (init_ack->num_outbound_streams == 0) { 1106163953Srrs /* protocol error... send an abort */ 1107163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1108163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1109170181Srrs op_err, 0); 1110168299Srrs *abort_no_unlock = 1; 1111163953Srrs return (-1); 1112163953Srrs } 1113163953Srrs /* process according to association state... */ 1114171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1115163953Srrs case SCTP_STATE_COOKIE_WAIT: 1116163953Srrs /* this is the expected state for this chunk */ 1117163953Srrs /* process the INIT-ACK parameters */ 1118163953Srrs if (stcb->asoc.primary_destination->dest_state & 1119163953Srrs SCTP_ADDR_UNCONFIRMED) { 1120163953Srrs /* 1121163953Srrs * The primary is where we sent the INIT, we can 1122163953Srrs * always consider it confirmed when the INIT-ACK is 1123163953Srrs * returned. Do this before we load addresses 1124163953Srrs * though. 1125163953Srrs */ 1126163953Srrs stcb->asoc.primary_destination->dest_state &= 1127163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1128163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1129172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1130163953Srrs } 1131169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1132170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1133163953Srrs /* error in parsing parameters */ 1134163953Srrs return (-1); 1135163953Srrs } 1136163953Srrs /* update our state */ 1137169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1138171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1139163953Srrs 1140163953Srrs /* reset the RTO calc */ 1141171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 1142171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1143171943Srrs stcb->asoc.overall_error_count, 1144171943Srrs 0, 1145171943Srrs SCTP_FROM_SCTP_INPUT, 1146171943Srrs __LINE__); 1147171943Srrs } 1148163953Srrs stcb->asoc.overall_error_count = 0; 1149169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1150163953Srrs /* 1151163953Srrs * collapse the init timer back in case of a exponential 1152165220Srrs * backoff 1153163953Srrs */ 1154163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1155163953Srrs stcb, net); 1156163953Srrs /* 1157163953Srrs * the send at the end of the inbound data processing will 1158163953Srrs * cause the cookie to be sent 1159163953Srrs */ 1160163953Srrs break; 1161163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1162163953Srrs /* incorrect state... discard */ 1163163953Srrs break; 1164163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1165163953Srrs /* incorrect state... discard */ 1166163953Srrs break; 1167163953Srrs case SCTP_STATE_OPEN: 1168163953Srrs /* incorrect state... discard */ 1169163953Srrs break; 1170163953Srrs case SCTP_STATE_EMPTY: 1171163953Srrs case SCTP_STATE_INUSE: 1172163953Srrs default: 1173163953Srrs /* incorrect state... discard */ 1174163953Srrs return (-1); 1175163953Srrs break; 1176163953Srrs } 1177169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1178163953Srrs return (0); 1179163953Srrs} 1180163953Srrs 1181163953Srrs 1182163953Srrs/* 1183163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1184163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1185163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1186163953Srrs * mbuf to the cookie-echo chunk 1187163953Srrs */ 1188163953Srrsstatic struct sctp_tcb * 1189163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1190163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1191163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1192169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1193170181Srrs uint32_t vrf_id) 1194163953Srrs{ 1195163953Srrs struct sctp_association *asoc; 1196163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1197163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1198163953Srrs int chk_length; 1199165220Srrs int init_offset, initack_offset, i; 1200163953Srrs int retval; 1201164205Srrs int spec_flag = 0; 1202170140Srrs uint32_t how_indx; 1203163953Srrs 1204163953Srrs /* I know that the TCB is non-NULL from the caller */ 1205163953Srrs asoc = &stcb->asoc; 1206166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1207166023Srrs if (asoc->cookie_how[how_indx] == 0) 1208166023Srrs break; 1209166023Srrs } 1210166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1211166023Srrs asoc->cookie_how[how_indx] = 1; 1212166023Srrs } 1213163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1214163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1215163953Srrs struct mbuf *op_err; 1216163953Srrs struct sctp_paramhdr *ph; 1217163953Srrs 1218163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1219163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1220165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1221163953Srrs if (op_err == NULL) { 1222163953Srrs /* FOOBAR */ 1223163953Srrs return (NULL); 1224163953Srrs } 1225163953Srrs /* pre-reserve some space */ 1226165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1227165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1228165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1229163953Srrs /* Set the len */ 1230165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1231163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1232163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1233163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1234169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1235170181Srrs vrf_id); 1236166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1237166023Srrs asoc->cookie_how[how_indx] = 2; 1238163953Srrs return (NULL); 1239163953Srrs } 1240163953Srrs /* 1241163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1242163953Srrs * INIT should start after the cookie-echo header struct (chunk 1243163953Srrs * header, state cookie header struct) 1244163953Srrs */ 1245163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1246163953Srrs 1247163953Srrs init_cp = (struct sctp_init_chunk *) 1248163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1249163953Srrs (uint8_t *) & init_buf); 1250163953Srrs if (init_cp == NULL) { 1251163953Srrs /* could not pull a INIT chunk in cookie */ 1252163953Srrs return (NULL); 1253163953Srrs } 1254163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1255163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1256163953Srrs return (NULL); 1257163953Srrs } 1258163953Srrs /* 1259163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1260163953Srrs * INIT-ACK follows the INIT chunk 1261163953Srrs */ 1262163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1263163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1264163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1265163953Srrs (uint8_t *) & initack_buf); 1266163953Srrs if (initack_cp == NULL) { 1267163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1268163953Srrs return (NULL); 1269163953Srrs } 1270163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1271163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1272163953Srrs return (NULL); 1273163953Srrs } 1274163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1275163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1276163953Srrs /* 1277163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1278163953Srrs * to get into the OPEN state 1279163953Srrs */ 1280166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1281172091Srrs /*- 1282172091Srrs * Opps, this means that we somehow generated two vtag's 1283172091Srrs * the same. I.e. we did: 1284172091Srrs * Us Peer 1285172091Srrs * <---INIT(tag=a)------ 1286172091Srrs * ----INIT-ACK(tag=t)--> 1287172091Srrs * ----INIT(tag=t)------> *1 1288172091Srrs * <---INIT-ACK(tag=a)--- 1289172091Srrs * <----CE(tag=t)------------- *2 1290172091Srrs * 1291172091Srrs * At point *1 we should be generating a different 1292172091Srrs * tag t'. Which means we would throw away the CE and send 1293172091Srrs * ours instead. Basically this is case C (throw away side). 1294172091Srrs */ 1295172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1296172091Srrs asoc->cookie_how[how_indx] = 17; 1297172091Srrs return (NULL); 1298172091Srrs 1299166023Srrs } 1300163953Srrs switch SCTP_GET_STATE 1301163953Srrs (asoc) { 1302163953Srrs case SCTP_STATE_COOKIE_WAIT: 1303166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1304163953Srrs /* 1305169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1306166023Srrs * correct tags... just accept it...but we must 1307166023Srrs * process the init so that we can make sure we have 1308166023Srrs * the right seq no's. 1309163953Srrs */ 1310163953Srrs /* First we must process the INIT !! */ 1311163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1312163953Srrs if (retval < 0) { 1313166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1314166023Srrs asoc->cookie_how[how_indx] = 3; 1315163953Srrs return (NULL); 1316163953Srrs } 1317163953Srrs /* we have already processed the INIT so no problem */ 1318163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1319171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1320171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1321163953Srrs /* update current state */ 1322166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1323166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1324166675Srrs else 1325166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1326171943Srrs 1327171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1328163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1329163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1330163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1331163953Srrs } 1332166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1333165220Srrs sctp_stop_all_cookie_timers(stcb); 1334163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1335163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1336163953Srrs (inp->sctp_socket->so_qlimit == 0) 1337163953Srrs ) { 1338172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1339172090Srrs struct socket *so; 1340172090Srrs 1341172090Srrs#endif 1342163953Srrs /* 1343163953Srrs * Here is where collision would go if we 1344163953Srrs * did a connect() and instead got a 1345163953Srrs * init/init-ack/cookie done before the 1346163953Srrs * init-ack came back.. 1347163953Srrs */ 1348163953Srrs stcb->sctp_ep->sctp_flags |= 1349163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1350172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1351172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1352172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1353172090Srrs SCTP_TCB_UNLOCK(stcb); 1354172090Srrs SCTP_SOCKET_LOCK(so, 1); 1355172090Srrs SCTP_TCB_LOCK(stcb); 1356172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1357172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1358172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1359172090Srrs return (NULL); 1360172090Srrs } 1361172090Srrs#endif 1362172090Srrs soisconnected(stcb->sctp_socket); 1363172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1364172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1365172090Srrs#endif 1366163953Srrs } 1367163953Srrs /* notify upper layer */ 1368163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1369163953Srrs /* 1370163953Srrs * since we did not send a HB make sure we don't 1371163953Srrs * double things 1372163953Srrs */ 1373163953Srrs net->hb_responded = 1; 1374170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1375171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1376163953Srrs 1377163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1378163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1379163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1380163953Srrs inp, stcb, NULL); 1381163953Srrs } 1382163953Srrs break; 1383163953Srrs default: 1384163953Srrs /* 1385163953Srrs * we're in the OPEN state (or beyond), so peer must 1386163953Srrs * have simply lost the COOKIE-ACK 1387163953Srrs */ 1388163953Srrs break; 1389163953Srrs } /* end switch */ 1390165220Srrs sctp_stop_all_cookie_timers(stcb); 1391163953Srrs /* 1392163953Srrs * We ignore the return code here.. not sure if we should 1393163953Srrs * somehow abort.. but we do have an existing asoc. This 1394163953Srrs * really should not fail. 1395163953Srrs */ 1396163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1397163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1398163953Srrs initack_offset, sh, init_src)) { 1399166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1400166023Srrs asoc->cookie_how[how_indx] = 4; 1401163953Srrs return (NULL); 1402163953Srrs } 1403163953Srrs /* respond with a COOKIE-ACK */ 1404165220Srrs sctp_toss_old_cookies(stcb, asoc); 1405163953Srrs sctp_send_cookie_ack(stcb); 1406166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1407166023Srrs asoc->cookie_how[how_indx] = 5; 1408163953Srrs return (stcb); 1409169352Srrs } 1410163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1411163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1412163953Srrs cookie->tie_tag_my_vtag == 0 && 1413163953Srrs cookie->tie_tag_peer_vtag == 0) { 1414163953Srrs /* 1415163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1416163953Srrs */ 1417166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1418166023Srrs asoc->cookie_how[how_indx] = 6; 1419163953Srrs return (NULL); 1420163953Srrs } 1421163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1422163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1423163953Srrs init_cp->init.initiate_tag == 0)) { 1424163953Srrs /* 1425163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1426163953Srrs * should be ok, re-accept peer info 1427163953Srrs */ 1428166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1429166023Srrs /* 1430166023Srrs * Extension of case C. If we hit this, then the 1431166023Srrs * random number generator returned the same vtag 1432166023Srrs * when we first sent our INIT-ACK and when we later 1433166023Srrs * sent our INIT. The side with the seq numbers that 1434166023Srrs * are different will be the one that normnally 1435166023Srrs * would have hit case C. This in effect "extends" 1436166023Srrs * our vtags in this collision case to be 64 bits. 1437166023Srrs * The same collision could occur aka you get both 1438166023Srrs * vtag and seq number the same twice in a row.. but 1439166023Srrs * is much less likely. If it did happen then we 1440166023Srrs * would proceed through and bring up the assoc.. we 1441166023Srrs * may end up with the wrong stream setup however.. 1442166023Srrs * which would be bad.. but there is no way to 1443166023Srrs * tell.. until we send on a stream that does not 1444166023Srrs * exist :-) 1445166023Srrs */ 1446166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1447166023Srrs asoc->cookie_how[how_indx] = 7; 1448166023Srrs 1449166023Srrs return (NULL); 1450166023Srrs } 1451166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1452166023Srrs asoc->cookie_how[how_indx] = 8; 1453171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1454163953Srrs sctp_stop_all_cookie_timers(stcb); 1455163953Srrs /* 1456163953Srrs * since we did not send a HB make sure we don't double 1457163953Srrs * things 1458163953Srrs */ 1459163953Srrs net->hb_responded = 1; 1460163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1461163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1462163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1463163953Srrs NULL); 1464163953Srrs } 1465163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1466164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1467164205Srrs 1468164205Srrs /* Note last_cwr_tsn? where is this used? */ 1469163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1470164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1471164205Srrs /* 1472164205Srrs * Ok the peer probably discarded our data (if we 1473164205Srrs * echoed a cookie+data). So anything on the 1474164205Srrs * sent_queue should be marked for retransmit, we 1475164205Srrs * may not get something to kick us so it COULD 1476164205Srrs * still take a timeout to move these.. but it can't 1477164205Srrs * hurt to mark them. 1478164205Srrs */ 1479164205Srrs struct sctp_tmit_chunk *chk; 1480163953Srrs 1481164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1482164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1483164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1484171440Srrs sctp_flight_size_decrease(chk); 1485171440Srrs sctp_total_flight_decrease(stcb, chk); 1486168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1487164205Srrs spec_flag++; 1488164205Srrs } 1489164205Srrs } 1490164205Srrs 1491164205Srrs } 1492163953Srrs /* process the INIT info (peer's info) */ 1493163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1494163953Srrs if (retval < 0) { 1495166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1496166023Srrs asoc->cookie_how[how_indx] = 9; 1497163953Srrs return (NULL); 1498163953Srrs } 1499163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1500163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1501163953Srrs initack_offset, sh, init_src)) { 1502166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1503166023Srrs asoc->cookie_how[how_indx] = 10; 1504163953Srrs return (NULL); 1505163953Srrs } 1506163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1507163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1508163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1509163953Srrs 1510163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1511163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1512163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1513172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1514172090Srrs struct socket *so; 1515172090Srrs 1516172090Srrs#endif 1517163953Srrs stcb->sctp_ep->sctp_flags |= 1518163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1519172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1520172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1521172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1522172090Srrs SCTP_TCB_UNLOCK(stcb); 1523172090Srrs SCTP_SOCKET_LOCK(so, 1); 1524172090Srrs SCTP_TCB_LOCK(stcb); 1525172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1526172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1527172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1528172090Srrs return (NULL); 1529172090Srrs } 1530172090Srrs#endif 1531172090Srrs soisconnected(stcb->sctp_socket); 1532172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1533172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1534172090Srrs#endif 1535163953Srrs } 1536166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1537166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1538166675Srrs else 1539166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1540166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1541166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1542166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1543166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1544166675Srrs } else { 1545166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1546163953Srrs } 1547171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1548163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1549163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1550163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1551163953Srrs 1552163953Srrs } 1553163953Srrs sctp_stop_all_cookie_timers(stcb); 1554165220Srrs sctp_toss_old_cookies(stcb, asoc); 1555163953Srrs sctp_send_cookie_ack(stcb); 1556164205Srrs if (spec_flag) { 1557164205Srrs /* 1558164205Srrs * only if we have retrans set do we do this. What 1559164205Srrs * this call does is get only the COOKIE-ACK out and 1560164205Srrs * then when we return the normal call to 1561164205Srrs * sctp_chunk_output will get the retrans out behind 1562164205Srrs * this. 1563164205Srrs */ 1564172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1565164205Srrs } 1566166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1567166023Srrs asoc->cookie_how[how_indx] = 11; 1568166023Srrs 1569163953Srrs return (stcb); 1570163953Srrs } 1571163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1572163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1573163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1574163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1575163953Srrs cookie->tie_tag_peer_vtag != 0) { 1576163953Srrs struct sctpasochead *head; 1577163953Srrs 1578163953Srrs /* 1579163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1580163953Srrs */ 1581165220Srrs /* temp code */ 1582166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1583166023Srrs asoc->cookie_how[how_indx] = 12; 1584171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1585171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1586165647Srrs 1587163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1588163953Srrs /* notify upper layer */ 1589163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1590165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1591166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1592166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1593166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1594166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1595166675Srrs } 1596166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1597166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1598166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1599166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1600166675Srrs } 1601165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1602171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1603165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1604165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1605165647Srrs 1606165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1607165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1608171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1609165647Srrs } 1610165647Srrs asoc->pre_open_streams = 1611165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1612165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1613165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1614165647Srrs 1615165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1616165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1617165647Srrs 1618165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1619165647Srrs 1620165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1621170138Srrs if (asoc->mapping_array) { 1622165647Srrs memset(asoc->mapping_array, 0, 1623165647Srrs asoc->mapping_array_size); 1624170138Srrs } 1625165220Srrs SCTP_TCB_UNLOCK(stcb); 1626165220Srrs SCTP_INP_INFO_WLOCK(); 1627165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1628165220Srrs SCTP_TCB_LOCK(stcb); 1629165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1630163953Srrs /* send up all the data */ 1631164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1632165220Srrs 1633172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1634165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1635165220Srrs stcb->asoc.strmout[i].stream_no = i; 1636165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1637165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1638165220Srrs } 1639163953Srrs /* process the INIT-ACK info (my info) */ 1640163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1641163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1642163953Srrs 1643163953Srrs /* pull from vtag hash */ 1644163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1645163953Srrs /* re-insert to new vtag position */ 1646163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1647163953Srrs sctppcbinfo.hashasocmark)]; 1648163953Srrs /* 1649163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1650163953Srrs * system 1651163953Srrs */ 1652163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1653163953Srrs 1654163953Srrs /* Is this the first restart? */ 1655163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1656163953Srrs /* Ok add it to assoc_id vtag hash */ 1657163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1658163953Srrs sctppcbinfo.hashrestartmark)]; 1659163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1660163953Srrs stcb->asoc.in_restart_hash = 1; 1661163953Srrs } 1662163953Srrs /* process the INIT info (peer's info) */ 1663164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1664165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1665165220Srrs SCTP_INP_INFO_WUNLOCK(); 1666165220Srrs 1667163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1668163953Srrs if (retval < 0) { 1669166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1670166023Srrs asoc->cookie_how[how_indx] = 13; 1671166023Srrs 1672163953Srrs return (NULL); 1673163953Srrs } 1674163953Srrs /* 1675163953Srrs * since we did not send a HB make sure we don't double 1676163953Srrs * things 1677163953Srrs */ 1678163953Srrs net->hb_responded = 1; 1679163953Srrs 1680163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1681163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1682163953Srrs initack_offset, sh, init_src)) { 1683166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1684166023Srrs asoc->cookie_how[how_indx] = 14; 1685166023Srrs 1686163953Srrs return (NULL); 1687163953Srrs } 1688163953Srrs /* respond with a COOKIE-ACK */ 1689163953Srrs sctp_stop_all_cookie_timers(stcb); 1690165220Srrs sctp_toss_old_cookies(stcb, asoc); 1691163953Srrs sctp_send_cookie_ack(stcb); 1692166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1693166023Srrs asoc->cookie_how[how_indx] = 15; 1694163953Srrs 1695163953Srrs return (stcb); 1696163953Srrs } 1697166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1698166023Srrs asoc->cookie_how[how_indx] = 16; 1699163953Srrs /* all other cases... */ 1700163953Srrs return (NULL); 1701163953Srrs} 1702163953Srrs 1703166086Srrs 1704163953Srrs/* 1705163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1706163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1707163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1708163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1709163953Srrs * was from returns a new TCB 1710163953Srrs */ 1711163953Srrsstatic struct sctp_tcb * 1712163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1713163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1714163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1715163953Srrs struct sockaddr *init_src, int *notification, 1716169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1717170181Srrs uint32_t vrf_id) 1718163953Srrs{ 1719163953Srrs struct sctp_tcb *stcb; 1720163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1721163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1722163953Srrs struct sockaddr_storage sa_store; 1723163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1724163953Srrs struct sockaddr_in *sin; 1725163953Srrs struct sockaddr_in6 *sin6; 1726163953Srrs struct sctp_association *asoc; 1727163953Srrs int chk_length; 1728163953Srrs int init_offset, initack_offset, initack_limit; 1729163953Srrs int retval; 1730163953Srrs int error = 0; 1731163953Srrs uint32_t old_tag; 1732166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1733163953Srrs 1734172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1735172090Srrs struct socket *so; 1736172090Srrs 1737172090Srrs so = SCTP_INP_SO(inp); 1738172090Srrs#endif 1739172090Srrs 1740163953Srrs /* 1741163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1742163953Srrs * INIT should start after the cookie-echo header struct (chunk 1743163953Srrs * header, state cookie header struct) 1744163953Srrs */ 1745163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1746163953Srrs init_cp = (struct sctp_init_chunk *) 1747163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1748163953Srrs (uint8_t *) & init_buf); 1749163953Srrs if (init_cp == NULL) { 1750163953Srrs /* could not pull a INIT chunk in cookie */ 1751169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1752169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1753163953Srrs return (NULL); 1754163953Srrs } 1755163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1756163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1757169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1758163953Srrs return (NULL); 1759163953Srrs } 1760163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1761163953Srrs /* 1762163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1763163953Srrs * INIT-ACK follows the INIT chunk 1764163953Srrs */ 1765163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1766163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1767163953Srrs (uint8_t *) & initack_buf); 1768163953Srrs if (initack_cp == NULL) { 1769163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1770169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1771163953Srrs return (NULL); 1772163953Srrs } 1773163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1774163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1775163953Srrs return (NULL); 1776163953Srrs } 1777163953Srrs /* 1778163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1779163953Srrs * "initack_limit" value. This is because the chk_length field 1780163953Srrs * includes the length of the cookie, but the cookie is omitted when 1781163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1782163953Srrs */ 1783163953Srrs initack_limit = offset + cookie_len; 1784163953Srrs 1785163953Srrs /* 1786163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1787163953Srrs * and popluate 1788163953Srrs */ 1789171531Srrs 1790171531Srrs /* 1791171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 1792171531Srrs * We do this since in effect we only use the p argument when the 1793171531Srrs * socket is unbound and we must do an implicit bind. Since we are 1794171531Srrs * getting a cookie, we cannot be unbound. 1795171531Srrs */ 1796163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1797171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 1798171531Srrs (struct thread *)NULL 1799171531Srrs ); 1800163953Srrs if (stcb == NULL) { 1801163953Srrs struct mbuf *op_err; 1802163953Srrs 1803163953Srrs /* memory problem? */ 1804169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1805169420Srrs "process_cookie_new: no room for another TCB!\n"); 1806163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1807170140Srrs 1808163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1809170181Srrs sh, op_err, vrf_id); 1810163953Srrs return (NULL); 1811163953Srrs } 1812163953Srrs /* get the correct sctp_nets */ 1813169420Srrs if (netp) 1814169420Srrs *netp = sctp_findnet(stcb, init_src); 1815169420Srrs 1816163953Srrs asoc = &stcb->asoc; 1817163953Srrs /* get scope variables out of cookie */ 1818163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1819163953Srrs asoc->site_scope = cookie->site_scope; 1820163953Srrs asoc->local_scope = cookie->local_scope; 1821163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1822163953Srrs 1823163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1824163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1825163953Srrs struct mbuf *op_err; 1826163953Srrs 1827163953Srrs /* 1828163953Srrs * Houston we have a problem. The EP changed while the 1829163953Srrs * cookie was in flight. Only recourse is to abort the 1830163953Srrs * association. 1831163953Srrs */ 1832170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1833163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1834163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1835170181Srrs sh, op_err, vrf_id); 1836172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1837172090Srrs SCTP_TCB_UNLOCK(stcb); 1838172090Srrs SCTP_SOCKET_LOCK(so, 1); 1839172090Srrs SCTP_TCB_LOCK(stcb); 1840172090Srrs#endif 1841171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 1842171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1843172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1844172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1845172090Srrs#endif 1846172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1847163953Srrs return (NULL); 1848163953Srrs } 1849163953Srrs /* process the INIT-ACK info (my info) */ 1850163953Srrs old_tag = asoc->my_vtag; 1851163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1852163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1853163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1854163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1855163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1856163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1857163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1858163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1859163953Srrs 1860163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1861163953Srrs 1862163953Srrs /* process the INIT info (peer's info) */ 1863169420Srrs if (netp) 1864169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1865169420Srrs else 1866169420Srrs retval = 0; 1867163953Srrs if (retval < 0) { 1868170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1869172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1870172090Srrs SCTP_TCB_UNLOCK(stcb); 1871172090Srrs SCTP_SOCKET_LOCK(so, 1); 1872172090Srrs SCTP_TCB_LOCK(stcb); 1873172090Srrs#endif 1874171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1875172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1876172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1877172090Srrs#endif 1878172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1879163953Srrs return (NULL); 1880163953Srrs } 1881163953Srrs /* load all addresses */ 1882163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1883163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1884163953Srrs init_src)) { 1885170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1886172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1887172090Srrs SCTP_TCB_UNLOCK(stcb); 1888172090Srrs SCTP_SOCKET_LOCK(so, 1); 1889172090Srrs SCTP_TCB_LOCK(stcb); 1890172090Srrs#endif 1891171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1892172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1893172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1894172090Srrs#endif 1895172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1896163953Srrs return (NULL); 1897163953Srrs } 1898163953Srrs /* 1899163953Srrs * verify any preceding AUTH chunk that was skipped 1900163953Srrs */ 1901163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1902163953Srrs sctp_auth_get_cookie_params(stcb, m, 1903163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1904163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1905163953Srrs if (auth_skipped) { 1906163953Srrs struct sctp_auth_chunk *auth; 1907163953Srrs 1908163953Srrs auth = (struct sctp_auth_chunk *) 1909166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1910169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1911163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1912169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1913169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1914171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1915172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1916172090Srrs SCTP_TCB_UNLOCK(stcb); 1917172090Srrs SCTP_SOCKET_LOCK(so, 1); 1918172090Srrs SCTP_TCB_LOCK(stcb); 1919172090Srrs#endif 1920171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1921172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1922172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1923172090Srrs#endif 1924172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1925163953Srrs return (NULL); 1926163953Srrs } else { 1927163953Srrs /* remaining chunks checked... good to go */ 1928163953Srrs stcb->asoc.authenticated = 1; 1929163953Srrs } 1930163953Srrs } 1931163953Srrs /* update current state */ 1932169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1933171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1934163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1935163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1936163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1937163953Srrs } 1938165220Srrs sctp_stop_all_cookie_timers(stcb); 1939163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1940163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1941163953Srrs 1942163953Srrs /* 1943163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1944163953Srrs * addresses that need to get added to the peer (eg. addresses 1945163953Srrs * changed while cookie echo in flight). This needs to be done 1946163953Srrs * after we go to the OPEN state to do the correct asconf 1947163953Srrs * processing. else, make sure we have the correct addresses in our 1948163953Srrs * lists 1949163953Srrs */ 1950163953Srrs 1951163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1952163953Srrs /* pull in local_address (our "from" address) */ 1953163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1954163953Srrs /* source addr is IPv4 */ 1955163953Srrs sin = (struct sockaddr_in *)initack_src; 1956163953Srrs memset(sin, 0, sizeof(*sin)); 1957163953Srrs sin->sin_family = AF_INET; 1958163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1959163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1960163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1961163953Srrs /* source addr is IPv6 */ 1962163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1963163953Srrs memset(sin6, 0, sizeof(*sin6)); 1964163953Srrs sin6->sin6_family = AF_INET6; 1965163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1966163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1967163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1968163953Srrs sizeof(sin6->sin6_addr)); 1969163953Srrs } else { 1970170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1971172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1972172090Srrs SCTP_TCB_UNLOCK(stcb); 1973172090Srrs SCTP_SOCKET_LOCK(so, 1); 1974172090Srrs SCTP_TCB_LOCK(stcb); 1975172090Srrs#endif 1976171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1977172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1978172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1979172090Srrs#endif 1980172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1981163953Srrs return (NULL); 1982163953Srrs } 1983163953Srrs 1984163953Srrs sctp_check_address_list(stcb, m, 1985163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1986163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1987163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1988163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1989163953Srrs 1990163953Srrs 1991163953Srrs /* set up to notify upper layer */ 1992163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1993163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1994163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1995163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1996163953Srrs /* 1997163953Srrs * This is an endpoint that called connect() how it got a 1998163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1999163953Srrs * the INIT was sent, but before it got there.. a complete 2000163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2001163953Srrs * should have went to the other code.. not here.. oh well.. 2002163953Srrs * a bit of protection is worth having.. 2003163953Srrs */ 2004163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2005172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2006172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2007172090Srrs SCTP_TCB_UNLOCK(stcb); 2008172090Srrs SCTP_SOCKET_LOCK(so, 1); 2009172090Srrs SCTP_TCB_LOCK(stcb); 2010172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2011172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2012172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2013172090Srrs return (NULL); 2014172090Srrs } 2015172090Srrs#endif 2016172090Srrs soisconnected(stcb->sctp_socket); 2017172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2018172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2019172090Srrs#endif 2020163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2021163953Srrs (inp->sctp_socket->so_qlimit)) { 2022163953Srrs /* 2023163953Srrs * We don't want to do anything with this one. Since it is 2024163953Srrs * the listening guy. The timer will get started for 2025163953Srrs * accepted connections in the caller. 2026163953Srrs */ 2027163953Srrs ; 2028163953Srrs } 2029163953Srrs /* since we did not send a HB make sure we don't double things */ 2030169420Srrs if ((netp) && (*netp)) 2031169420Srrs (*netp)->hb_responded = 1; 2032163953Srrs 2033163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2034163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2035163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2036163953Srrs } 2037163953Srrs /* respond with a COOKIE-ACK */ 2038165220Srrs /* calculate the RTT */ 2039171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2040170642Srrs if ((netp) && (*netp)) { 2041169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2042171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2043170642Srrs } 2044163953Srrs sctp_send_cookie_ack(stcb); 2045163953Srrs return (stcb); 2046163953Srrs} 2047163953Srrs 2048163953Srrs 2049163953Srrs/* 2050163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2051163953Srrs * existing (non-NULL) TCB 2052163953Srrs */ 2053163953Srrsstatic struct mbuf * 2054163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2055163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2056163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2057169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2058170181Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id) 2059163953Srrs{ 2060163953Srrs struct sctp_state_cookie *cookie; 2061163953Srrs struct sockaddr_in6 sin6; 2062163953Srrs struct sockaddr_in sin; 2063163953Srrs struct sctp_tcb *l_stcb = *stcb; 2064163953Srrs struct sctp_inpcb *l_inp; 2065163953Srrs struct sockaddr *to; 2066163953Srrs sctp_assoc_t sac_restart_id; 2067163953Srrs struct sctp_pcb *ep; 2068163953Srrs struct mbuf *m_sig; 2069163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2070163953Srrs uint8_t *sig; 2071163953Srrs uint8_t cookie_ok = 0; 2072163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2073163953Srrs unsigned int cookie_len; 2074163953Srrs struct timeval now; 2075163953Srrs struct timeval time_expires; 2076163953Srrs struct sockaddr_storage dest_store; 2077163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2078163953Srrs struct ip *iph; 2079163953Srrs int notification = 0; 2080163953Srrs struct sctp_nets *netl; 2081163953Srrs int had_a_existing_tcb = 0; 2082163953Srrs 2083169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2084169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2085163953Srrs 2086163953Srrs if (inp_p == NULL) { 2087163953Srrs return (NULL); 2088163953Srrs } 2089163953Srrs /* First get the destination address setup too. */ 2090163953Srrs iph = mtod(m, struct ip *); 2091163953Srrs if (iph->ip_v == IPVERSION) { 2092163953Srrs /* its IPv4 */ 2093170140Srrs struct sockaddr_in *lsin; 2094163953Srrs 2095170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 2096170140Srrs memset(lsin, 0, sizeof(*lsin)); 2097170140Srrs lsin->sin_family = AF_INET; 2098170140Srrs lsin->sin_len = sizeof(*lsin); 2099170140Srrs lsin->sin_port = sh->dest_port; 2100170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2101165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2102163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 2103163953Srrs /* its IPv6 */ 2104163953Srrs struct ip6_hdr *ip6; 2105170140Srrs struct sockaddr_in6 *lsin6; 2106163953Srrs 2107170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2108170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 2109170140Srrs lsin6->sin6_family = AF_INET6; 2110170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2111163953Srrs ip6 = mtod(m, struct ip6_hdr *); 2112170140Srrs lsin6->sin6_port = sh->dest_port; 2113170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 2114166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2115163953Srrs } else { 2116163953Srrs return (NULL); 2117163953Srrs } 2118163953Srrs 2119163953Srrs cookie = &cp->cookie; 2120163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2121163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2122163953Srrs 2123163953Srrs if ((cookie->peerport != sh->src_port) && 2124163953Srrs (cookie->myport != sh->dest_port) && 2125163953Srrs (cookie->my_vtag != sh->v_tag)) { 2126163953Srrs /* 2127163953Srrs * invalid ports or bad tag. Note that we always leave the 2128163953Srrs * v_tag in the header in network order and when we stored 2129163953Srrs * it in the my_vtag slot we also left it in network order. 2130169352Srrs * This maintains the match even though it may be in the 2131163953Srrs * opposite byte order of the machine :-> 2132163953Srrs */ 2133163953Srrs return (NULL); 2134163953Srrs } 2135163953Srrs if (cookie_len > size_of_pkt || 2136163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2137163953Srrs sizeof(struct sctp_init_chunk) + 2138163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2139163953Srrs /* cookie too long! or too small */ 2140163953Srrs return (NULL); 2141163953Srrs } 2142163953Srrs /* 2143163953Srrs * split off the signature into its own mbuf (since it should not be 2144163953Srrs * calculated in the sctp_hmac_m() call). 2145163953Srrs */ 2146163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2147163953Srrs if (sig_offset > size_of_pkt) { 2148163953Srrs /* packet not correct size! */ 2149163953Srrs /* XXX this may already be accounted for earlier... */ 2150163953Srrs return (NULL); 2151163953Srrs } 2152163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2153163953Srrs if (m_sig == NULL) { 2154163953Srrs /* out of memory or ?? */ 2155163953Srrs return (NULL); 2156163953Srrs } 2157163953Srrs /* 2158163953Srrs * compute the signature/digest for the cookie 2159163953Srrs */ 2160163953Srrs ep = &(*inp_p)->sctp_ep; 2161163953Srrs l_inp = *inp_p; 2162163953Srrs if (l_stcb) { 2163163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2164163953Srrs } 2165163953Srrs SCTP_INP_RLOCK(l_inp); 2166163953Srrs if (l_stcb) { 2167163953Srrs SCTP_TCB_LOCK(l_stcb); 2168163953Srrs } 2169163953Srrs /* which cookie is it? */ 2170163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2171163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2172163953Srrs /* it's the old cookie */ 2173169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2174163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2175170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2176163953Srrs } else { 2177163953Srrs /* it's the current cookie */ 2178169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2179163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2180170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2181163953Srrs } 2182163953Srrs /* get the signature */ 2183163953Srrs SCTP_INP_RUNLOCK(l_inp); 2184163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2185163953Srrs if (sig == NULL) { 2186163953Srrs /* couldn't find signature */ 2187164085Srrs sctp_m_freem(m_sig); 2188163953Srrs return (NULL); 2189163953Srrs } 2190163953Srrs /* compare the received digest with the computed digest */ 2191163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2192163953Srrs /* try the old cookie? */ 2193163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2194163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2195163953Srrs /* compute digest with old */ 2196169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2197163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2198170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2199163953Srrs /* compare */ 2200163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2201163953Srrs cookie_ok = 1; 2202163953Srrs } 2203163953Srrs } else { 2204163953Srrs cookie_ok = 1; 2205163953Srrs } 2206163953Srrs 2207163953Srrs /* 2208163953Srrs * Now before we continue we must reconstruct our mbuf so that 2209163953Srrs * normal processing of any other chunks will work. 2210163953Srrs */ 2211163953Srrs { 2212163953Srrs struct mbuf *m_at; 2213163953Srrs 2214163953Srrs m_at = m; 2215165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2216165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2217163953Srrs } 2218165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2219163953Srrs } 2220163953Srrs 2221163953Srrs if (cookie_ok == 0) { 2222169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2223169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2224169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2225169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2226163953Srrs return (NULL); 2227163953Srrs } 2228163953Srrs /* 2229163953Srrs * check the cookie timestamps to be sure it's not stale 2230163953Srrs */ 2231169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2232163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2233169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2234163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2235163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2236163953Srrs /* cookie is stale! */ 2237163953Srrs struct mbuf *op_err; 2238163953Srrs struct sctp_stale_cookie_msg *scm; 2239163953Srrs uint32_t tim; 2240163953Srrs 2241163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2242165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2243163953Srrs if (op_err == NULL) { 2244163953Srrs /* FOOBAR */ 2245163953Srrs return (NULL); 2246163953Srrs } 2247163953Srrs /* pre-reserve some space */ 2248165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2249165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2250165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2251163953Srrs 2252163953Srrs /* Set the len */ 2253165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2254163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2255163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2256163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2257163953Srrs (sizeof(uint32_t)))); 2258163953Srrs /* seconds to usec */ 2259163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2260163953Srrs /* add in usec */ 2261163953Srrs if (tim == 0) 2262163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2263163953Srrs scm->time_usec = htonl(tim); 2264169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2265170181Srrs vrf_id); 2266163953Srrs return (NULL); 2267163953Srrs } 2268163953Srrs /* 2269163953Srrs * Now we must see with the lookup address if we have an existing 2270163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2271163953Srrs * and a INIT collided with us and somewhere the peer sent the 2272163953Srrs * cookie on another address besides the single address our assoc 2273163953Srrs * had for him. In this case we will have one of the tie-tags set at 2274163953Srrs * least AND the address field in the cookie can be used to look it 2275163953Srrs * up. 2276163953Srrs */ 2277163953Srrs to = NULL; 2278163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2279163953Srrs memset(&sin6, 0, sizeof(sin6)); 2280163953Srrs sin6.sin6_family = AF_INET6; 2281163953Srrs sin6.sin6_len = sizeof(sin6); 2282163953Srrs sin6.sin6_port = sh->src_port; 2283163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2284163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2285163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2286163953Srrs to = (struct sockaddr *)&sin6; 2287163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2288163953Srrs memset(&sin, 0, sizeof(sin)); 2289163953Srrs sin.sin_family = AF_INET; 2290163953Srrs sin.sin_len = sizeof(sin); 2291163953Srrs sin.sin_port = sh->src_port; 2292163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2293163953Srrs to = (struct sockaddr *)&sin; 2294169420Srrs } else { 2295169420Srrs /* This should not happen */ 2296169420Srrs return (NULL); 2297163953Srrs } 2298163953Srrs if ((*stcb == NULL) && to) { 2299163953Srrs /* Yep, lets check */ 2300163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2301163953Srrs if (*stcb == NULL) { 2302163953Srrs /* 2303163953Srrs * We should have only got back the same inp. If we 2304163953Srrs * got back a different ep we have a problem. The 2305163953Srrs * original findep got back l_inp and now 2306163953Srrs */ 2307163953Srrs if (l_inp != *inp_p) { 2308169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2309163953Srrs } 2310165220Srrs } else { 2311165220Srrs if (*locked_tcb == NULL) { 2312165220Srrs /* 2313165220Srrs * In this case we found the assoc only 2314165220Srrs * after we locked the create lock. This 2315165220Srrs * means we are in a colliding case and we 2316165220Srrs * must make sure that we unlock the tcb if 2317165220Srrs * its one of the cases where we throw away 2318165220Srrs * the incoming packets. 2319165220Srrs */ 2320165220Srrs *locked_tcb = *stcb; 2321165220Srrs 2322165220Srrs /* 2323165220Srrs * We must also increment the inp ref count 2324165220Srrs * since the ref_count flags was set when we 2325165220Srrs * did not find the TCB, now we found it 2326165220Srrs * which reduces the refcount.. we must 2327165220Srrs * raise it back out to balance it all :-) 2328165220Srrs */ 2329165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2330165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2331169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2332165220Srrs (*stcb)->sctp_ep, l_inp); 2333165220Srrs } 2334165220Srrs } 2335163953Srrs } 2336163953Srrs } 2337169420Srrs if (to == NULL) 2338169420Srrs return (NULL); 2339169420Srrs 2340163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2341163953Srrs if (*stcb == NULL) { 2342163953Srrs /* this is the "normal" case... get a new TCB */ 2343163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2344163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2345170181Srrs auth_skipped, auth_offset, auth_len, vrf_id); 2346163953Srrs } else { 2347163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2348163953Srrs had_a_existing_tcb = 1; 2349163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2350170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2351170181Srrs ¬ification, &sac_restart_id, vrf_id); 2352163953Srrs } 2353163953Srrs 2354163953Srrs if (*stcb == NULL) { 2355163953Srrs /* still no TCB... must be bad cookie-echo */ 2356163953Srrs return (NULL); 2357163953Srrs } 2358163953Srrs /* 2359163953Srrs * Ok, we built an association so confirm the address we sent the 2360163953Srrs * INIT-ACK to. 2361163953Srrs */ 2362163953Srrs netl = sctp_findnet(*stcb, to); 2363163953Srrs /* 2364163953Srrs * This code should in theory NOT run but 2365163953Srrs */ 2366163953Srrs if (netl == NULL) { 2367163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2368163953Srrs int ret; 2369163953Srrs 2370165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2371165220Srrs SCTP_IN_COOKIE_PROC); 2372163953Srrs netl = sctp_findnet(*stcb, to); 2373163953Srrs } 2374163953Srrs if (netl) { 2375163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2376163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2377169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2378163953Srrs netl); 2379163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2380172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2381163953Srrs } 2382163953Srrs } 2383163953Srrs if (*stcb) { 2384163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2385163953Srrs *stcb, NULL); 2386163953Srrs } 2387163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2388163953Srrs if (!had_a_existing_tcb || 2389163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2390163953Srrs /* 2391163953Srrs * If we have a NEW cookie or the connect never 2392163953Srrs * reached the connected state during collision we 2393163953Srrs * must do the TCP accept thing. 2394163953Srrs */ 2395163953Srrs struct socket *so, *oso; 2396163953Srrs struct sctp_inpcb *inp; 2397163953Srrs 2398163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2399163953Srrs /* 2400163953Srrs * For a restart we will keep the same 2401163953Srrs * socket, no need to do anything. I THINK!! 2402163953Srrs */ 2403172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2404163953Srrs return (m); 2405163953Srrs } 2406163953Srrs oso = (*inp_p)->sctp_socket; 2407171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2408163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2409166086Srrs so = sonewconn(oso, 0 2410163953Srrs ); 2411163953Srrs SCTP_TCB_LOCK((*stcb)); 2412171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2413171858Srrs 2414163953Srrs if (so == NULL) { 2415163953Srrs struct mbuf *op_err; 2416163953Srrs 2417172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2418172090Srrs struct socket *pcb_so; 2419172090Srrs 2420172090Srrs#endif 2421163953Srrs /* Too many sockets */ 2422169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2423163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2424163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2425170181Srrs sh, op_err, vrf_id); 2426172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2427172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2428172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2429172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2430172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2431172090Srrs SCTP_TCB_LOCK((*stcb)); 2432172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2433172090Srrs#endif 2434171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2435172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2436172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2437172090Srrs#endif 2438163953Srrs return (NULL); 2439163953Srrs } 2440163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2441166086Srrs SCTP_INP_INCR_REF(inp); 2442166086Srrs /* 2443166086Srrs * We add the unbound flag here so that if we get an 2444166086Srrs * soabort() before we get the move_pcb done, we 2445166086Srrs * will properly cleanup. 2446166086Srrs */ 2447163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2448163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2449163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2450166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2451163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2452163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2453163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2454172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2455163953Srrs inp->sctp_socket = so; 2456163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2457163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2458163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2459163953Srrs inp->inp_starting_point_for_iterator = NULL; 2460163953Srrs /* 2461163953Srrs * copy in the authentication parameters from the 2462163953Srrs * original endpoint 2463163953Srrs */ 2464163953Srrs if (inp->sctp_ep.local_hmacs) 2465163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2466163953Srrs inp->sctp_ep.local_hmacs = 2467163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2468163953Srrs if (inp->sctp_ep.local_auth_chunks) 2469163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2470163953Srrs inp->sctp_ep.local_auth_chunks = 2471163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2472163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2473163953Srrs &inp->sctp_ep.shared_keys); 2474163953Srrs 2475163953Srrs /* 2476163953Srrs * Now we must move it from one hash table to 2477163953Srrs * another and get the tcb in the right place. 2478163953Srrs */ 2479163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2480170056Srrs 2481170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2482170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2483170056Srrs 2484169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2485170056Srrs SCTP_TCB_LOCK((*stcb)); 2486170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2487163953Srrs 2488170056Srrs 2489166086Srrs /* 2490166086Srrs * now we must check to see if we were aborted while 2491166086Srrs * the move was going on and the lock/unlock 2492166086Srrs * happened. 2493166086Srrs */ 2494166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2495166086Srrs /* 2496166086Srrs * yep it was, we leave the assoc attached 2497166086Srrs * to the socket since the sctp_inpcb_free() 2498166086Srrs * call will send an abort for us. 2499166086Srrs */ 2500166086Srrs SCTP_INP_DECR_REF(inp); 2501166086Srrs return (NULL); 2502166086Srrs } 2503166086Srrs SCTP_INP_DECR_REF(inp); 2504163953Srrs /* Switch over to the new guy */ 2505163953Srrs *inp_p = inp; 2506172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2507163953Srrs 2508166086Srrs /* 2509166086Srrs * Pull it from the incomplete queue and wake the 2510166086Srrs * guy 2511166086Srrs */ 2512172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2513172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2514172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2515172090Srrs SCTP_SOCKET_LOCK(so, 1); 2516172090Srrs#endif 2517166086Srrs soisconnected(so); 2518172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2519172118Srrs SCTP_TCB_LOCK((*stcb)); 2520172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2521172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2522172090Srrs#endif 2523163953Srrs return (m); 2524163953Srrs } 2525163953Srrs } 2526163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2527172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2528163953Srrs } 2529163953Srrs return (m); 2530163953Srrs} 2531163953Srrs 2532163953Srrsstatic void 2533163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2534163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2535163953Srrs{ 2536163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2537163953Srrs struct sctp_association *asoc; 2538163953Srrs 2539169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2540169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2541163953Srrs if (stcb == NULL) 2542163953Srrs return; 2543163953Srrs 2544163953Srrs asoc = &stcb->asoc; 2545163953Srrs 2546163953Srrs sctp_stop_all_cookie_timers(stcb); 2547163953Srrs /* process according to association state */ 2548163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2549163953Srrs /* state change only needed when I am in right state */ 2550169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2551171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2552163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2553163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2554163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2555163953Srrs 2556163953Srrs } 2557163953Srrs /* update RTO */ 2558163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2559163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2560163953Srrs if (asoc->overall_error_count == 0) { 2561163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2562171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2563163953Srrs } 2564169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2565172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2566163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2567163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2568172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2569172090Srrs struct socket *so; 2570172090Srrs 2571172090Srrs#endif 2572163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2573172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2574172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2575172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2576172090Srrs SCTP_TCB_UNLOCK(stcb); 2577172090Srrs SCTP_SOCKET_LOCK(so, 1); 2578172090Srrs SCTP_TCB_LOCK(stcb); 2579172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2580172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2581172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2582172090Srrs return; 2583172090Srrs } 2584172090Srrs#endif 2585172090Srrs soisconnected(stcb->sctp_socket); 2586172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2587172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2588172090Srrs#endif 2589163953Srrs } 2590163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2591163953Srrs stcb, net); 2592163953Srrs /* 2593163953Srrs * since we did not send a HB make sure we don't double 2594163953Srrs * things 2595163953Srrs */ 2596163953Srrs net->hb_responded = 1; 2597163953Srrs 2598163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2599163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2600163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2601163953Srrs stcb->sctp_ep, stcb, NULL); 2602163953Srrs } 2603163953Srrs /* 2604171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2605171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2606171572Srrs * in flight) 2607163953Srrs */ 2608163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2609163953Srrs (stcb->asoc.peer_supports_asconf) && 2610163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2611171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2612163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2613163953Srrs stcb->sctp_ep, stcb, 2614163953Srrs stcb->asoc.primary_destination); 2615171572Srrs#else 2616171572Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination); 2617171572Srrs#endif 2618163953Srrs } 2619163953Srrs } 2620163953Srrs /* Toss the cookie if I can */ 2621163953Srrs sctp_toss_old_cookies(stcb, asoc); 2622163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2623163953Srrs /* Restart the timer if we have pending data */ 2624163953Srrs struct sctp_tmit_chunk *chk; 2625163953Srrs 2626163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2627163953Srrs if (chk) { 2628163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2629163953Srrs stcb, chk->whoTo); 2630163953Srrs } 2631163953Srrs } 2632163953Srrs} 2633163953Srrs 2634163953Srrsstatic void 2635163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2636163953Srrs struct sctp_tcb *stcb) 2637163953Srrs{ 2638163953Srrs struct sctp_nets *net; 2639163953Srrs struct sctp_tmit_chunk *lchk; 2640163953Srrs uint32_t tsn; 2641163953Srrs 2642163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2643163953Srrs return; 2644163953Srrs } 2645163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2646163953Srrs tsn = ntohl(cp->tsn); 2647163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2648163953Srrs /* Also we make sure we disable the nonce_wait */ 2649163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2650163953Srrs if (lchk == NULL) { 2651163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2652163953Srrs } else { 2653163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2654163953Srrs } 2655163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2656163953Srrs stcb->asoc.nonce_sum_check = 0; 2657163953Srrs 2658163953Srrs /* Find where it was sent, if possible */ 2659163953Srrs net = NULL; 2660163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2661163953Srrs while (lchk) { 2662163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2663163953Srrs net = lchk->whoTo; 2664163953Srrs break; 2665163953Srrs } 2666163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2667163953Srrs break; 2668163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2669163953Srrs } 2670163953Srrs if (net == NULL) 2671163953Srrs /* default is we use the primary */ 2672163953Srrs net = stcb->asoc.primary_destination; 2673163953Srrs 2674163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2675163953Srrs /* 2676171440Srrs * JRS - Use the congestion control given in the pluggable 2677171440Srrs * CC module 2678171440Srrs */ 2679171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2680171440Srrs /* 2681163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2682163953Srrs * the next sending seq i.e. the resync_tsn. 2683163953Srrs */ 2684163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2685163953Srrs } 2686163953Srrs /* 2687163953Srrs * We always send a CWR this way if our previous one was lost our 2688163953Srrs * peer will get an update, or if it is not time again to reduce we 2689163953Srrs * still get the cwr to the peer. 2690163953Srrs */ 2691163953Srrs sctp_send_cwr(stcb, net, tsn); 2692163953Srrs} 2693163953Srrs 2694163953Srrsstatic void 2695163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2696163953Srrs{ 2697163953Srrs /* 2698163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2699163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2700163953Srrs * If so remove it. 2701163953Srrs */ 2702163953Srrs struct sctp_tmit_chunk *chk; 2703163953Srrs struct sctp_ecne_chunk *ecne; 2704163953Srrs 2705163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2706163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2707163953Srrs continue; 2708163953Srrs } 2709163953Srrs /* 2710163953Srrs * Look for and remove if it is the right TSN. Since there 2711163953Srrs * is only ONE ECNE on the control queue at any one time we 2712163953Srrs * don't need to worry about more than one! 2713163953Srrs */ 2714163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2715163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2716163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2717163953Srrs /* this covers this ECNE, we can remove it */ 2718163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2719163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2720163953Srrs sctp_next); 2721163953Srrs if (chk->data) { 2722163953Srrs sctp_m_freem(chk->data); 2723163953Srrs chk->data = NULL; 2724163953Srrs } 2725163953Srrs stcb->asoc.ctrl_queue_cnt--; 2726163953Srrs sctp_free_a_chunk(stcb, chk); 2727163953Srrs break; 2728163953Srrs } 2729163953Srrs } 2730163953Srrs} 2731163953Srrs 2732163953Srrsstatic void 2733163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2734163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2735163953Srrs{ 2736163953Srrs struct sctp_association *asoc; 2737163953Srrs 2738172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2739172090Srrs struct socket *so; 2740172090Srrs 2741172090Srrs#endif 2742172090Srrs 2743169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2744169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2745163953Srrs if (stcb == NULL) 2746163953Srrs return; 2747163953Srrs 2748163953Srrs asoc = &stcb->asoc; 2749163953Srrs /* process according to association state */ 2750163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2751163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2752171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2753171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 2754163953Srrs SCTP_TCB_UNLOCK(stcb); 2755163953Srrs return; 2756163953Srrs } 2757163953Srrs /* notify upper layer protocol */ 2758163953Srrs if (stcb->sctp_socket) { 2759172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2760163953Srrs /* are the queues empty? they should be */ 2761163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2762163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2763163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2764172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 2765163953Srrs } 2766163953Srrs } 2767163953Srrs /* stop the timer */ 2768171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2769163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2770163953Srrs /* free the TCB */ 2771171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2772171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 2773172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2774172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2775172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2776172090Srrs SCTP_TCB_UNLOCK(stcb); 2777172090Srrs SCTP_SOCKET_LOCK(so, 1); 2778172090Srrs SCTP_TCB_LOCK(stcb); 2779172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2780172090Srrs#endif 2781171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2782172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2783172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2784172090Srrs#endif 2785163953Srrs return; 2786163953Srrs} 2787163953Srrs 2788163953Srrsstatic int 2789163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2790163953Srrs struct sctp_nets *net, uint8_t flg) 2791163953Srrs{ 2792163953Srrs switch (desc->chunk_type) { 2793163953Srrs case SCTP_DATA: 2794163953Srrs /* find the tsn to resend (possibly */ 2795163953Srrs { 2796163953Srrs uint32_t tsn; 2797163953Srrs struct sctp_tmit_chunk *tp1; 2798163953Srrs 2799163953Srrs tsn = ntohl(desc->tsn_ifany); 2800163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2801163953Srrs while (tp1) { 2802163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2803163953Srrs /* found it */ 2804163953Srrs break; 2805163953Srrs } 2806163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2807163953Srrs MAX_TSN)) { 2808163953Srrs /* not found */ 2809163953Srrs tp1 = NULL; 2810163953Srrs break; 2811163953Srrs } 2812163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2813163953Srrs } 2814163953Srrs if (tp1 == NULL) { 2815163953Srrs /* 2816163953Srrs * Do it the other way , aka without paying 2817163953Srrs * attention to queue seq order. 2818163953Srrs */ 2819163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2820163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2821163953Srrs while (tp1) { 2822163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2823163953Srrs /* found it */ 2824163953Srrs break; 2825163953Srrs } 2826163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2827163953Srrs } 2828163953Srrs } 2829163953Srrs if (tp1 == NULL) { 2830163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2831163953Srrs } 2832163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2833163953Srrs uint8_t *ddp; 2834163953Srrs 2835163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2836163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2837163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2838163953Srrs return (0); 2839163953Srrs } 2840163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2841163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2842163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2843163953Srrs return (0); 2844163953Srrs } 2845163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2846163953Srrs sizeof(struct sctp_data_chunk)); 2847163953Srrs { 2848163953Srrs unsigned int iii; 2849163953Srrs 2850163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2851163953Srrs iii++) { 2852163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2853163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2854163953Srrs return (-1); 2855163953Srrs } 2856163953Srrs } 2857163953Srrs } 2858163953Srrs /* 2859163953Srrs * We zero out the nonce so resync not 2860163953Srrs * needed 2861163953Srrs */ 2862163953Srrs tp1->rec.data.ect_nonce = 0; 2863163953Srrs 2864163953Srrs if (tp1->do_rtt) { 2865163953Srrs /* 2866163953Srrs * this guy had a RTO calculation 2867163953Srrs * pending on it, cancel it 2868163953Srrs */ 2869163953Srrs tp1->do_rtt = 0; 2870163953Srrs } 2871163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2872163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2873163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2874163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2875163953Srrs /* 2876163953Srrs * mark it as if we were doing a FR, since 2877163953Srrs * we will be getting gap ack reports behind 2878163953Srrs * the info from the router. 2879163953Srrs */ 2880163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2881163953Srrs /* 2882163953Srrs * mark the tsn with what sequences can 2883163953Srrs * cause a new FR. 2884163953Srrs */ 2885163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2886163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2887163953Srrs } else { 2888163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2889163953Srrs } 2890163953Srrs 2891163953Srrs /* restart the timer */ 2892163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2893171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2894163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2895163953Srrs stcb, tp1->whoTo); 2896163953Srrs 2897163953Srrs /* fix counts and things */ 2898170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 2899170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2900170744Srrs tp1->whoTo->flight_size, 2901170744Srrs tp1->book_size, 2902170744Srrs (uintptr_t) stcb, 2903170744Srrs tp1->rec.data.TSN_seq); 2904170744Srrs } 2905168709Srrs sctp_flight_size_decrease(tp1); 2906168709Srrs sctp_total_flight_decrease(stcb, tp1); 2907163953Srrs } { 2908163953Srrs /* audit code */ 2909163953Srrs unsigned int audit; 2910163953Srrs 2911163953Srrs audit = 0; 2912163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2913163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2914163953Srrs audit++; 2915163953Srrs } 2916163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2917163953Srrs sctp_next) { 2918163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2919163953Srrs audit++; 2920163953Srrs } 2921163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2922169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2923163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2924163953Srrs#ifndef SCTP_AUDITING_ENABLED 2925163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2926163953Srrs#endif 2927163953Srrs } 2928163953Srrs } 2929163953Srrs } 2930163953Srrs break; 2931163953Srrs case SCTP_ASCONF: 2932163953Srrs { 2933163953Srrs struct sctp_tmit_chunk *asconf; 2934163953Srrs 2935163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2936163953Srrs sctp_next) { 2937163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2938163953Srrs break; 2939163953Srrs } 2940163953Srrs } 2941163953Srrs if (asconf) { 2942163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2943163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2944163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2945163953Srrs asconf->snd_count--; 2946163953Srrs } 2947163953Srrs } 2948163953Srrs break; 2949163953Srrs case SCTP_INITIATION: 2950163953Srrs /* resend the INIT */ 2951163953Srrs stcb->asoc.dropped_special_cnt++; 2952163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2953163953Srrs /* 2954163953Srrs * If we can get it in, in a few attempts we do 2955163953Srrs * this, otherwise we let the timer fire. 2956163953Srrs */ 2957163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2958171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2959172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 2960163953Srrs } 2961163953Srrs break; 2962163953Srrs case SCTP_SELECTIVE_ACK: 2963163953Srrs /* resend the sack */ 2964163953Srrs sctp_send_sack(stcb); 2965163953Srrs break; 2966163953Srrs case SCTP_HEARTBEAT_REQUEST: 2967163953Srrs /* resend a demand HB */ 2968171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 2969171440Srrs /* 2970171440Srrs * Only retransmit if we KNOW we wont destroy the 2971171440Srrs * tcb 2972171440Srrs */ 2973171440Srrs (void)sctp_send_hb(stcb, 1, net); 2974171440Srrs } 2975163953Srrs break; 2976163953Srrs case SCTP_SHUTDOWN: 2977163953Srrs sctp_send_shutdown(stcb, net); 2978163953Srrs break; 2979163953Srrs case SCTP_SHUTDOWN_ACK: 2980163953Srrs sctp_send_shutdown_ack(stcb, net); 2981163953Srrs break; 2982163953Srrs case SCTP_COOKIE_ECHO: 2983163953Srrs { 2984163953Srrs struct sctp_tmit_chunk *cookie; 2985163953Srrs 2986163953Srrs cookie = NULL; 2987163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2988163953Srrs sctp_next) { 2989163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2990163953Srrs break; 2991163953Srrs } 2992163953Srrs } 2993163953Srrs if (cookie) { 2994163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2995163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2996163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2997163953Srrs sctp_stop_all_cookie_timers(stcb); 2998163953Srrs } 2999163953Srrs } 3000163953Srrs break; 3001163953Srrs case SCTP_COOKIE_ACK: 3002163953Srrs sctp_send_cookie_ack(stcb); 3003163953Srrs break; 3004163953Srrs case SCTP_ASCONF_ACK: 3005163953Srrs /* resend last asconf ack */ 3006171990Srrs sctp_send_asconf_ack(stcb); 3007163953Srrs break; 3008163953Srrs case SCTP_FORWARD_CUM_TSN: 3009163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3010163953Srrs break; 3011163953Srrs /* can't do anything with these */ 3012163953Srrs case SCTP_PACKET_DROPPED: 3013163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3014163953Srrs case SCTP_HEARTBEAT_ACK: 3015163953Srrs case SCTP_ABORT_ASSOCIATION: 3016163953Srrs case SCTP_OPERATION_ERROR: 3017163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3018163953Srrs case SCTP_ECN_ECHO: 3019163953Srrs case SCTP_ECN_CWR: 3020163953Srrs default: 3021163953Srrs break; 3022163953Srrs } 3023163953Srrs return (0); 3024163953Srrs} 3025163953Srrs 3026163953Srrsvoid 3027163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3028163953Srrs{ 3029163953Srrs int i; 3030163953Srrs uint16_t temp; 3031163953Srrs 3032163953Srrs /* 3033163953Srrs * We set things to 0xffff since this is the last delivered sequence 3034163953Srrs * and we will be sending in 0 after the reset. 3035163953Srrs */ 3036163953Srrs 3037163953Srrs if (number_entries) { 3038163953Srrs for (i = 0; i < number_entries; i++) { 3039163953Srrs temp = ntohs(list[i]); 3040163953Srrs if (temp >= stcb->asoc.streamincnt) { 3041163953Srrs continue; 3042163953Srrs } 3043163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3044163953Srrs } 3045163953Srrs } else { 3046163953Srrs list = NULL; 3047163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3048163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3049163953Srrs } 3050163953Srrs } 3051172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3052163953Srrs} 3053163953Srrs 3054163953Srrsstatic void 3055163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3056163953Srrs{ 3057163953Srrs int i; 3058163953Srrs 3059163953Srrs if (number_entries == 0) { 3060163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3061163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3062163953Srrs } 3063163953Srrs } else if (number_entries) { 3064163953Srrs for (i = 0; i < number_entries; i++) { 3065163953Srrs uint16_t temp; 3066163953Srrs 3067163953Srrs temp = ntohs(list[i]); 3068163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3069163953Srrs /* no such stream */ 3070163953Srrs continue; 3071163953Srrs } 3072163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3073163953Srrs } 3074163953Srrs } 3075172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3076163953Srrs} 3077163953Srrs 3078163953Srrs 3079163953Srrsstruct sctp_stream_reset_out_request * 3080163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3081163953Srrs{ 3082163953Srrs struct sctp_association *asoc; 3083163953Srrs struct sctp_stream_reset_out_req *req; 3084163953Srrs struct sctp_stream_reset_out_request *r; 3085163953Srrs struct sctp_tmit_chunk *chk; 3086163953Srrs int len, clen; 3087163953Srrs 3088163953Srrs asoc = &stcb->asoc; 3089163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3090169208Srrs asoc->stream_reset_outstanding = 0; 3091163953Srrs return (NULL); 3092163953Srrs } 3093163953Srrs if (stcb->asoc.str_reset == NULL) { 3094169208Srrs asoc->stream_reset_outstanding = 0; 3095163953Srrs return (NULL); 3096163953Srrs } 3097163953Srrs chk = stcb->asoc.str_reset; 3098163953Srrs if (chk->data == NULL) { 3099163953Srrs return (NULL); 3100163953Srrs } 3101163953Srrs if (bchk) { 3102163953Srrs /* he wants a copy of the chk pointer */ 3103163953Srrs *bchk = chk; 3104163953Srrs } 3105163953Srrs clen = chk->send_size; 3106163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3107163953Srrs r = &req->sr_req; 3108163953Srrs if (ntohl(r->request_seq) == seq) { 3109163953Srrs /* found it */ 3110163953Srrs return (r); 3111163953Srrs } 3112163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3113163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3114163953Srrs /* move to the next one, there can only be a max of two */ 3115163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3116163953Srrs if (ntohl(r->request_seq) == seq) { 3117163953Srrs return (r); 3118163953Srrs } 3119163953Srrs } 3120163953Srrs /* that seq is not here */ 3121163953Srrs return (NULL); 3122163953Srrs} 3123163953Srrs 3124163953Srrsstatic void 3125163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3126163953Srrs{ 3127163953Srrs struct sctp_association *asoc; 3128163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3129163953Srrs 3130163953Srrs if (stcb->asoc.str_reset == NULL) { 3131163953Srrs return; 3132163953Srrs } 3133166675Srrs asoc = &stcb->asoc; 3134166675Srrs 3135171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3136163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3137163953Srrs chk, 3138163953Srrs sctp_next); 3139163953Srrs if (chk->data) { 3140163953Srrs sctp_m_freem(chk->data); 3141163953Srrs chk->data = NULL; 3142163953Srrs } 3143163953Srrs asoc->ctrl_queue_cnt--; 3144163953Srrs sctp_free_a_chunk(stcb, chk); 3145172156Srrs /* sa_ignore NO_NULL_CHK */ 3146163953Srrs stcb->asoc.str_reset = NULL; 3147163953Srrs} 3148163953Srrs 3149163953Srrs 3150163953Srrsstatic int 3151163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3152163953Srrs uint32_t seq, uint32_t action, 3153164139Srrs struct sctp_stream_reset_response *respin) 3154163953Srrs{ 3155163953Srrs uint16_t type; 3156163953Srrs int lparm_len; 3157163953Srrs struct sctp_association *asoc = &stcb->asoc; 3158163953Srrs struct sctp_tmit_chunk *chk; 3159163953Srrs struct sctp_stream_reset_out_request *srparam; 3160163953Srrs int number_entries; 3161163953Srrs 3162163953Srrs if (asoc->stream_reset_outstanding == 0) { 3163163953Srrs /* duplicate */ 3164163953Srrs return (0); 3165163953Srrs } 3166163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3167163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3168163953Srrs if (srparam) { 3169163953Srrs stcb->asoc.str_reset_seq_out++; 3170163953Srrs type = ntohs(srparam->ph.param_type); 3171163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3172163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3173164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3174163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3175163953Srrs if (asoc->stream_reset_outstanding) 3176163953Srrs asoc->stream_reset_outstanding--; 3177163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3178163953Srrs /* do it */ 3179163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3180163953Srrs } else { 3181172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3182163953Srrs } 3183163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3184163953Srrs /* Answered my request */ 3185164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3186163953Srrs if (asoc->stream_reset_outstanding) 3187163953Srrs asoc->stream_reset_outstanding--; 3188163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3189172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3190163953Srrs } 3191163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3192163953Srrs /** 3193163953Srrs * a) Adopt the new in tsn. 3194163953Srrs * b) reset the map 3195163953Srrs * c) Adopt the new out-tsn 3196163953Srrs */ 3197163953Srrs struct sctp_stream_reset_response_tsn *resp; 3198163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3199163953Srrs int abort_flag = 0; 3200163953Srrs 3201163953Srrs if (respin == NULL) { 3202163953Srrs /* huh ? */ 3203163953Srrs return (0); 3204163953Srrs } 3205163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3206163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3207163953Srrs asoc->stream_reset_outstanding--; 3208163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3209163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3210163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3211170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3212163953Srrs if (abort_flag) { 3213163953Srrs return (1); 3214163953Srrs } 3215163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3216163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3217163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3218163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3219163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3220163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3221163953Srrs 3222163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3223163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3224163953Srrs 3225163953Srrs } 3226163953Srrs } 3227163953Srrs /* get rid of the request and get the request flags */ 3228163953Srrs if (asoc->stream_reset_outstanding == 0) { 3229163953Srrs sctp_clean_up_stream_reset(stcb); 3230163953Srrs } 3231163953Srrs } 3232163953Srrs } 3233163953Srrs return (0); 3234163953Srrs} 3235163953Srrs 3236163953Srrsstatic void 3237163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3238163953Srrs struct sctp_tmit_chunk *chk, 3239170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3240163953Srrs{ 3241163953Srrs uint32_t seq; 3242163953Srrs int len, i; 3243163953Srrs int number_entries; 3244163953Srrs uint16_t temp; 3245163953Srrs 3246163953Srrs /* 3247163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3248163953Srrs * seq_in is right. 3249163953Srrs */ 3250163953Srrs struct sctp_association *asoc = &stcb->asoc; 3251163953Srrs 3252163953Srrs seq = ntohl(req->request_seq); 3253163953Srrs if (asoc->str_reset_seq_in == seq) { 3254170992Srrs if (trunc) { 3255170992Srrs /* Can't do it, since they exceeded our buffer size */ 3256170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3257170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3258170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3259170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3260163953Srrs len = ntohs(req->ph.param_length); 3261163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3262163953Srrs for (i = 0; i < number_entries; i++) { 3263163953Srrs temp = ntohs(req->list_of_streams[i]); 3264163953Srrs req->list_of_streams[i] = temp; 3265163953Srrs } 3266163953Srrs /* move the reset action back one */ 3267163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3268163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3269163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3270163953Srrs asoc->str_reset_seq_out, 3271163953Srrs seq, (asoc->sending_seq - 1)); 3272163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3273163953Srrs asoc->str_reset = chk; 3274163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3275163953Srrs stcb->asoc.stream_reset_outstanding++; 3276163953Srrs } else { 3277163953Srrs /* Can't do it, since we have sent one out */ 3278163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3279163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3280163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3281163953Srrs } 3282163953Srrs asoc->str_reset_seq_in++; 3283163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3284163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3285163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3286163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3287163953Srrs } else { 3288163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3289163953Srrs } 3290163953Srrs} 3291163953Srrs 3292163953Srrsstatic int 3293163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3294163953Srrs struct sctp_tmit_chunk *chk, 3295163953Srrs struct sctp_stream_reset_tsn_request *req) 3296163953Srrs{ 3297163953Srrs /* reset all in and out and update the tsn */ 3298163953Srrs /* 3299163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3300163953Srrs * and set cum-ack to it. Also process this selected number as a 3301163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3302163953Srrs */ 3303163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3304163953Srrs struct sctp_association *asoc = &stcb->asoc; 3305163953Srrs int abort_flag = 0; 3306163953Srrs uint32_t seq; 3307163953Srrs 3308163953Srrs seq = ntohl(req->request_seq); 3309163953Srrs if (asoc->str_reset_seq_in == seq) { 3310163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3311163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3312163953Srrs fwdtsn.ch.chunk_flags = 0; 3313163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3314170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3315163953Srrs if (abort_flag) { 3316163953Srrs return (1); 3317163953Srrs } 3318163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3319163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3320163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3321163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3322163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3323163953Srrs /* save off historical data for retrans */ 3324163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3325163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3326163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3327163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3328163953Srrs 3329163953Srrs sctp_add_stream_reset_result_tsn(chk, 3330163953Srrs ntohl(req->request_seq), 3331163953Srrs SCTP_STREAM_RESET_PERFORMED, 3332163953Srrs stcb->asoc.sending_seq, 3333163953Srrs stcb->asoc.mapping_array_base_tsn); 3334163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3335163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3336163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3337163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3338163953Srrs 3339163953Srrs asoc->str_reset_seq_in++; 3340163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3341163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3342163953Srrs stcb->asoc.last_sending_seq[0], 3343163953Srrs stcb->asoc.last_base_tsnsent[0] 3344163953Srrs ); 3345163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3346163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3347163953Srrs stcb->asoc.last_sending_seq[1], 3348163953Srrs stcb->asoc.last_base_tsnsent[1] 3349163953Srrs ); 3350163953Srrs } else { 3351163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3352163953Srrs } 3353163953Srrs return (0); 3354163953Srrs} 3355163953Srrs 3356163953Srrsstatic void 3357163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3358163953Srrs struct sctp_tmit_chunk *chk, 3359170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3360163953Srrs{ 3361163953Srrs uint32_t seq, tsn; 3362163953Srrs int number_entries, len; 3363163953Srrs struct sctp_association *asoc = &stcb->asoc; 3364163953Srrs 3365163953Srrs seq = ntohl(req->request_seq); 3366163953Srrs 3367163953Srrs /* now if its not a duplicate we process it */ 3368163953Srrs if (asoc->str_reset_seq_in == seq) { 3369163953Srrs len = ntohs(req->ph.param_length); 3370163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3371163953Srrs /* 3372163953Srrs * the sender is resetting, handle the list issue.. we must 3373163953Srrs * a) verify if we can do the reset, if so no problem b) If 3374163953Srrs * we can't do the reset we must copy the request. c) queue 3375163953Srrs * it, and setup the data in processor to trigger it off 3376163953Srrs * when needed and dequeue all the queued data. 3377163953Srrs */ 3378163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3379163953Srrs 3380163953Srrs /* move the reset action back one */ 3381163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3382170992Srrs if (trunc) { 3383170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3384170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3385170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3386163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3387163953Srrs /* we can do it now */ 3388163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3389163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3390163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3391163953Srrs } else { 3392163953Srrs /* 3393163953Srrs * we must queue it up and thus wait for the TSN's 3394163953Srrs * to arrive that are at or before tsn 3395163953Srrs */ 3396163953Srrs struct sctp_stream_reset_list *liste; 3397163953Srrs int siz; 3398163953Srrs 3399163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3400163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3401170091Srrs siz, SCTP_M_STRESET); 3402163953Srrs if (liste == NULL) { 3403163953Srrs /* gak out of memory */ 3404163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3405163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3406163953Srrs return; 3407163953Srrs } 3408163953Srrs liste->tsn = tsn; 3409163953Srrs liste->number_entries = number_entries; 3410163953Srrs memcpy(&liste->req, req, 3411163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3412163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3413163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3414163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3415163953Srrs } 3416163953Srrs asoc->str_reset_seq_in++; 3417163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3418163953Srrs /* 3419163953Srrs * one seq back, just echo back last action since my 3420163953Srrs * response was lost. 3421163953Srrs */ 3422163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3423163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3424163953Srrs /* 3425163953Srrs * two seq back, just echo back last action since my 3426163953Srrs * response was lost. 3427163953Srrs */ 3428163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3429163953Srrs } else { 3430163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3431163953Srrs } 3432163953Srrs} 3433163953Srrs 3434170992Srrs#ifdef __GNUC__ 3435170992Srrs__attribute__((noinline)) 3436170992Srrs#endif 3437170992Srrs static int 3438170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3439170992Srrs struct sctp_stream_reset_out_req *sr_req) 3440163953Srrs{ 3441163953Srrs int chk_length, param_len, ptype; 3442170992Srrs struct sctp_paramhdr pstore; 3443170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3444170992Srrs 3445163953Srrs uint32_t seq; 3446163953Srrs int num_req = 0; 3447170992Srrs int trunc = 0; 3448163953Srrs struct sctp_tmit_chunk *chk; 3449163953Srrs struct sctp_chunkhdr *ch; 3450163953Srrs struct sctp_paramhdr *ph; 3451166675Srrs int ret_code = 0; 3452166675Srrs int num_param = 0; 3453163953Srrs 3454163953Srrs /* now it may be a reset or a reset-response */ 3455163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3456163953Srrs 3457163953Srrs /* setup for adding the response */ 3458163953Srrs sctp_alloc_a_chunk(stcb, chk); 3459163953Srrs if (chk == NULL) { 3460163953Srrs return (ret_code); 3461163953Srrs } 3462163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3463169208Srrs chk->rec.chunk_id.can_take_data = 0; 3464163953Srrs chk->asoc = &stcb->asoc; 3465163953Srrs chk->no_fr_allowed = 0; 3466163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3467166023Srrs chk->book_size_scale = 0; 3468165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3469163953Srrs if (chk->data == NULL) { 3470163953Srrsstrres_nochunk: 3471163953Srrs if (chk->data) { 3472163953Srrs sctp_m_freem(chk->data); 3473163953Srrs chk->data = NULL; 3474163953Srrs } 3475163953Srrs sctp_free_a_chunk(stcb, chk); 3476163953Srrs return (ret_code); 3477163953Srrs } 3478165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3479163953Srrs 3480163953Srrs /* setup chunk parameters */ 3481163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3482163953Srrs chk->snd_count = 0; 3483163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3484163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3485163953Srrs 3486163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3487163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3488163953Srrs ch->chunk_flags = 0; 3489163953Srrs ch->chunk_length = htons(chk->send_size); 3490165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3491170992Srrs offset += sizeof(struct sctp_chunkhdr); 3492163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3493170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3494170992Srrs if (ph == NULL) 3495170992Srrs break; 3496163953Srrs param_len = ntohs(ph->param_length); 3497163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3498163953Srrs /* bad param */ 3499163953Srrs break; 3500163953Srrs } 3501170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3502170992Srrs (uint8_t *) & cstore); 3503163953Srrs ptype = ntohs(ph->param_type); 3504163953Srrs num_param++; 3505170994Srrs if (param_len > (int)sizeof(cstore)) { 3506170992Srrs trunc = 1; 3507170992Srrs } else { 3508170992Srrs trunc = 0; 3509170992Srrs } 3510170992Srrs 3511163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3512163953Srrs /* hit the max of parameters already sorry.. */ 3513163953Srrs break; 3514163953Srrs } 3515163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3516163953Srrs struct sctp_stream_reset_out_request *req_out; 3517163953Srrs 3518163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3519163953Srrs num_req++; 3520163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3521163953Srrs seq = ntohl(req_out->response_seq); 3522163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3523163953Srrs /* implicit ack */ 3524169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3525163953Srrs } 3526163953Srrs } 3527170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3528163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3529163953Srrs struct sctp_stream_reset_in_request *req_in; 3530163953Srrs 3531163953Srrs num_req++; 3532170992Srrs 3533163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3534170992Srrs 3535170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3536163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3537163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3538163953Srrs 3539163953Srrs num_req++; 3540163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3541170992Srrs 3542163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3543163953Srrs ret_code = 1; 3544163953Srrs goto strres_nochunk; 3545163953Srrs } 3546163953Srrs /* no more */ 3547163953Srrs break; 3548163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3549163953Srrs struct sctp_stream_reset_response *resp; 3550163953Srrs uint32_t result; 3551163953Srrs 3552163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3553163953Srrs seq = ntohl(resp->response_seq); 3554163953Srrs result = ntohl(resp->result); 3555163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3556163953Srrs ret_code = 1; 3557163953Srrs goto strres_nochunk; 3558163953Srrs } 3559163953Srrs } else { 3560163953Srrs break; 3561163953Srrs } 3562170992Srrs offset += SCTP_SIZE32(param_len); 3563163953Srrs chk_length -= SCTP_SIZE32(param_len); 3564163953Srrs } 3565163953Srrs if (num_req == 0) { 3566163953Srrs /* we have no response free the stuff */ 3567163953Srrs goto strres_nochunk; 3568163953Srrs } 3569163953Srrs /* ok we have a chunk to link in */ 3570163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3571163953Srrs chk, 3572163953Srrs sctp_next); 3573163953Srrs stcb->asoc.ctrl_queue_cnt++; 3574163953Srrs return (ret_code); 3575163953Srrs} 3576163953Srrs 3577163953Srrs/* 3578163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3579163953Srrs * to handle this, either we get the whole packet and must disect it 3580163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3581163953Srrs * from a middle box that did the disectting for us. 3582163953Srrs */ 3583163953Srrsstatic void 3584163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3585170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3586163953Srrs{ 3587163953Srrs uint32_t bottle_bw, on_queue; 3588163953Srrs uint16_t trunc_len; 3589163953Srrs unsigned int chlen; 3590163953Srrs unsigned int at; 3591163953Srrs struct sctp_chunk_desc desc; 3592163953Srrs struct sctp_chunkhdr *ch; 3593163953Srrs 3594163953Srrs chlen = ntohs(cp->ch.chunk_length); 3595163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3596163953Srrs /* XXX possible chlen underflow */ 3597163953Srrs if (chlen == 0) { 3598163953Srrs ch = NULL; 3599163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3600163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3601163953Srrs } else { 3602163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3603163953Srrs chlen -= sizeof(struct sctphdr); 3604163953Srrs /* XXX possible chlen underflow */ 3605163953Srrs memset(&desc, 0, sizeof(desc)); 3606163953Srrs } 3607163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3608170781Srrs if (trunc_len > limit) { 3609170781Srrs trunc_len = limit; 3610170781Srrs } 3611163953Srrs /* now the chunks themselves */ 3612163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3613163953Srrs desc.chunk_type = ch->chunk_type; 3614163953Srrs /* get amount we need to move */ 3615163953Srrs at = ntohs(ch->chunk_length); 3616163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3617163953Srrs /* corrupt chunk, maybe at the end? */ 3618163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3619163953Srrs break; 3620163953Srrs } 3621163953Srrs if (trunc_len == 0) { 3622163953Srrs /* we are supposed to have all of it */ 3623163953Srrs if (at > chlen) { 3624163953Srrs /* corrupt skip it */ 3625163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3626163953Srrs break; 3627163953Srrs } 3628163953Srrs } else { 3629163953Srrs /* is there enough of it left ? */ 3630163953Srrs if (desc.chunk_type == SCTP_DATA) { 3631163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3632163953Srrs sizeof(desc.data_bytes))) { 3633163953Srrs break; 3634163953Srrs } 3635163953Srrs } else { 3636163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3637163953Srrs break; 3638163953Srrs } 3639163953Srrs } 3640163953Srrs } 3641163953Srrs if (desc.chunk_type == SCTP_DATA) { 3642163953Srrs /* can we get out the tsn? */ 3643163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3644163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3645163953Srrs 3646163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3647163953Srrs /* yep */ 3648163953Srrs struct sctp_data_chunk *dcp; 3649163953Srrs uint8_t *ddp; 3650163953Srrs unsigned int iii; 3651163953Srrs 3652163953Srrs dcp = (struct sctp_data_chunk *)ch; 3653163953Srrs ddp = (uint8_t *) (dcp + 1); 3654163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3655163953Srrs desc.data_bytes[iii] = ddp[iii]; 3656163953Srrs } 3657163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3658163953Srrs } else { 3659163953Srrs /* nope we are done. */ 3660163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3661163953Srrs break; 3662163953Srrs } 3663163953Srrs } else { 3664163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3665163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3666163953Srrs } 3667163953Srrs 3668163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3669163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3670163953Srrs break; 3671163953Srrs } 3672163953Srrs if (SCTP_SIZE32(at) > chlen) { 3673163953Srrs break; 3674163953Srrs } 3675163953Srrs chlen -= SCTP_SIZE32(at); 3676163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3677163953Srrs /* done, none left */ 3678163953Srrs break; 3679163953Srrs } 3680163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3681163953Srrs } 3682163953Srrs /* Now update any rwnd --- possibly */ 3683163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3684163953Srrs /* From a peer, we get a rwnd report */ 3685163953Srrs uint32_t a_rwnd; 3686163953Srrs 3687163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3688163953Srrs 3689163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3690163953Srrs on_queue = ntohl(cp->current_onq); 3691163953Srrs if (bottle_bw && on_queue) { 3692163953Srrs /* a rwnd report is in here */ 3693163953Srrs if (bottle_bw > on_queue) 3694163953Srrs a_rwnd = bottle_bw - on_queue; 3695163953Srrs else 3696163953Srrs a_rwnd = 0; 3697163953Srrs 3698163953Srrs if (a_rwnd == 0) 3699163953Srrs stcb->asoc.peers_rwnd = 0; 3700163953Srrs else { 3701163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3702163953Srrs stcb->asoc.peers_rwnd = 3703163953Srrs a_rwnd - stcb->asoc.total_flight; 3704163953Srrs } else { 3705163953Srrs stcb->asoc.peers_rwnd = 0; 3706163953Srrs } 3707163953Srrs if (stcb->asoc.peers_rwnd < 3708163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3709163953Srrs /* SWS sender side engages */ 3710163953Srrs stcb->asoc.peers_rwnd = 0; 3711163953Srrs } 3712163953Srrs } 3713163953Srrs } 3714163953Srrs } else { 3715163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3716163953Srrs } 3717163953Srrs 3718163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3719163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3720163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3721163953Srrs (stcb->asoc.sat_network)) { 3722163953Srrs /* 3723163953Srrs * This is debateable but for sat networks it makes sense 3724163953Srrs * Note if a T3 timer has went off, we will prohibit any 3725163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3726163953Srrs */ 3727171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 3728171440Srrs net, cp, &bottle_bw, &on_queue); 3729163953Srrs } 3730163953Srrs} 3731163953Srrs 3732163953Srrs/* 3733163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3734163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3735163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3736163953Srrs * length of the complete packet outputs: - length: modified to remaining 3737163953Srrs * length after control processing - netp: modified to new sctp_nets after 3738163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3739163953Srrs * bad packet,...) otherwise return the tcb for this packet 3740163953Srrs */ 3741170099Srrs#ifdef __GNUC__ 3742170099Srrs__attribute__((noinline)) 3743170099Srrs#endif 3744170099Srrs static struct sctp_tcb * 3745170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3746170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3747170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3748170181Srrs uint32_t vrf_id) 3749163953Srrs{ 3750163953Srrs struct sctp_association *asoc; 3751163953Srrs uint32_t vtag_in; 3752163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3753170140Srrs uint32_t chk_length; 3754163953Srrs int ret; 3755168299Srrs int abort_no_unlock = 0; 3756163953Srrs 3757163953Srrs /* 3758163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3759163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3760163953Srrs * until we get into jumbo grams and such.. 3761163953Srrs */ 3762166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3763163953Srrs struct sctp_tcb *locked_tcb = stcb; 3764163953Srrs int got_auth = 0; 3765163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3766163953Srrs int auth_skipped = 0; 3767171990Srrs int asconf_cnt = 0; 3768163953Srrs 3769172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3770172090Srrs struct socket *so; 3771172090Srrs 3772172090Srrs#endif 3773172090Srrs 3774169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3775169420Srrs iphlen, *offset, length, stcb); 3776163953Srrs 3777163953Srrs /* validate chunk header length... */ 3778163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3779170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3780170056Srrs ntohs(ch->chunk_length)); 3781170091Srrs if (locked_tcb) { 3782170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3783170091Srrs } 3784163953Srrs return (NULL); 3785163953Srrs } 3786163953Srrs /* 3787163953Srrs * validate the verification tag 3788163953Srrs */ 3789163953Srrs vtag_in = ntohl(sh->v_tag); 3790163953Srrs 3791165220Srrs if (locked_tcb) { 3792165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3793165220Srrs } 3794163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3795170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3796170056Srrs ntohs(ch->chunk_length), vtag_in); 3797163953Srrs if (vtag_in != 0) { 3798163953Srrs /* protocol error- silently discard... */ 3799163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3800169378Srrs if (locked_tcb) { 3801163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3802169378Srrs } 3803163953Srrs return (NULL); 3804163953Srrs } 3805163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3806163953Srrs /* 3807163953Srrs * If there is no stcb, skip the AUTH chunk and process 3808163953Srrs * later after a stcb is found (to validate the lookup was 3809163953Srrs * valid. 3810163953Srrs */ 3811163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3812163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3813163953Srrs /* save this chunk for later processing */ 3814163953Srrs auth_skipped = 1; 3815163953Srrs auth_offset = *offset; 3816163953Srrs auth_len = ntohs(ch->chunk_length); 3817163953Srrs 3818163953Srrs /* (temporarily) move past this chunk */ 3819163953Srrs *offset += SCTP_SIZE32(auth_len); 3820163953Srrs if (*offset >= length) { 3821163953Srrs /* no more data left in the mbuf chain */ 3822163953Srrs *offset = length; 3823170091Srrs if (locked_tcb) { 3824170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3825170091Srrs } 3826163953Srrs return (NULL); 3827163953Srrs } 3828163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3829163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3830163953Srrs } 3831169420Srrs if (ch == NULL) { 3832169420Srrs /* Help */ 3833169420Srrs *offset = length; 3834170091Srrs if (locked_tcb) { 3835170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3836170091Srrs } 3837169420Srrs return (NULL); 3838169420Srrs } 3839163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3840163953Srrs goto process_control_chunks; 3841163953Srrs } 3842163953Srrs /* 3843163953Srrs * first check if it's an ASCONF with an unknown src addr we 3844163953Srrs * need to look inside to find the association 3845163953Srrs */ 3846163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3847171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 3848171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 3849171990Srrs 3850163953Srrs /* inp's refcount may be reduced */ 3851163953Srrs SCTP_INP_INCR_REF(inp); 3852163953Srrs 3853171990Srrs asconf_offset = *offset; 3854171990Srrs do { 3855171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 3856171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 3857171990Srrs break; 3858171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3859171990Srrs *offset, sh, &inp, netp); 3860171990Srrs if (stcb != NULL) 3861171990Srrs break; 3862171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 3863171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 3864171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3865171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 3866163953Srrs if (stcb == NULL) { 3867163953Srrs /* 3868163953Srrs * reduce inp's refcount if not reduced in 3869163953Srrs * sctp_findassociation_ep_asconf(). 3870163953Srrs */ 3871163953Srrs SCTP_INP_DECR_REF(inp); 3872171990Srrs } else { 3873171990Srrs locked_tcb = stcb; 3874163953Srrs } 3875171990Srrs 3876163953Srrs /* now go back and verify any auth chunk to be sure */ 3877163953Srrs if (auth_skipped && (stcb != NULL)) { 3878163953Srrs struct sctp_auth_chunk *auth; 3879163953Srrs 3880163953Srrs auth = (struct sctp_auth_chunk *) 3881163953Srrs sctp_m_getptr(m, auth_offset, 3882163953Srrs auth_len, chunk_buf); 3883163953Srrs got_auth = 1; 3884163953Srrs auth_skipped = 0; 3885169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3886163953Srrs auth_offset)) { 3887163953Srrs /* auth HMAC failed so dump it */ 3888163953Srrs *offset = length; 3889170091Srrs if (locked_tcb) { 3890170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3891170091Srrs } 3892163953Srrs return (NULL); 3893163953Srrs } else { 3894163953Srrs /* remaining chunks are HMAC checked */ 3895163953Srrs stcb->asoc.authenticated = 1; 3896163953Srrs } 3897163953Srrs } 3898163953Srrs } 3899163953Srrs if (stcb == NULL) { 3900163953Srrs /* no association, so it's out of the blue... */ 3901169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3902170181Srrs vrf_id); 3903163953Srrs *offset = length; 3904169378Srrs if (locked_tcb) { 3905163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3906169378Srrs } 3907163953Srrs return (NULL); 3908163953Srrs } 3909163953Srrs asoc = &stcb->asoc; 3910163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3911163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3912163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3913163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3914163953Srrs if ((vtag_in == asoc->my_vtag) || 3915163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3916163953Srrs (vtag_in == asoc->peer_vtag))) { 3917163953Srrs /* this is valid */ 3918163953Srrs } else { 3919163953Srrs /* drop this packet... */ 3920163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3921169378Srrs if (locked_tcb) { 3922163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3923169378Srrs } 3924163953Srrs return (NULL); 3925163953Srrs } 3926163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3927163953Srrs if (vtag_in != asoc->my_vtag) { 3928163953Srrs /* 3929163953Srrs * this could be a stale SHUTDOWN-ACK or the 3930163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3931163953Srrs * is still hung; we have started a new asoc 3932163953Srrs * but it won't complete until the shutdown 3933163953Srrs * is completed 3934163953Srrs */ 3935169378Srrs if (locked_tcb) { 3936163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3937169378Srrs } 3938163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3939170181Srrs NULL, vrf_id); 3940163953Srrs return (NULL); 3941163953Srrs } 3942163953Srrs } else { 3943163953Srrs /* for all other chunks, vtag must match */ 3944163953Srrs if (vtag_in != asoc->my_vtag) { 3945163953Srrs /* invalid vtag... */ 3946169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3947169420Srrs "invalid vtag: %xh, expect %xh\n", 3948169420Srrs vtag_in, asoc->my_vtag); 3949163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3950169378Srrs if (locked_tcb) { 3951163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3952169378Srrs } 3953163953Srrs *offset = length; 3954163953Srrs return (NULL); 3955163953Srrs } 3956163953Srrs } 3957163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3958163953Srrs /* 3959163953Srrs * process all control chunks... 3960163953Srrs */ 3961163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3962163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3963163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3964163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3965171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 3966171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3967171943Srrs stcb->asoc.overall_error_count, 3968171943Srrs 0, 3969171943Srrs SCTP_FROM_SCTP_INPUT, 3970171943Srrs __LINE__); 3971171943Srrs } 3972163953Srrs stcb->asoc.overall_error_count = 0; 3973163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3974163953Srrs *netp); 3975163953Srrs } 3976163953Srrsprocess_control_chunks: 3977163953Srrs while (IS_SCTP_CONTROL(ch)) { 3978163953Srrs /* validate chunk length */ 3979163953Srrs chk_length = ntohs(ch->chunk_length); 3980169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 3981169420Srrs ch->chunk_type, chk_length); 3982170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 3983170140Srrs if (chk_length < sizeof(*ch) || 3984170140Srrs (*offset + (int)chk_length) > length) { 3985163953Srrs *offset = length; 3986169378Srrs if (locked_tcb) { 3987163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3988169378Srrs } 3989163953Srrs return (NULL); 3990163953Srrs } 3991163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3992163953Srrs /* 3993163953Srrs * INIT-ACK only gets the init ack "header" portion only 3994163953Srrs * because we don't have to process the peer's COOKIE. All 3995163953Srrs * others get a complete chunk. 3996163953Srrs */ 3997169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3998169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3999163953Srrs /* get an init-ack chunk */ 4000163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4001163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4002163953Srrs if (ch == NULL) { 4003163953Srrs *offset = length; 4004169378Srrs if (locked_tcb) { 4005163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4006169378Srrs } 4007163953Srrs return (NULL); 4008163953Srrs } 4009170642Srrs } else { 4010170786Srrs /* For cookies and all other chunks. */ 4011169208Srrs if (chk_length > sizeof(chunk_buf)) { 4012169208Srrs /* 4013169208Srrs * use just the size of the chunk buffer so 4014170642Srrs * the front part of our chunks fit in 4015170642Srrs * contiguous space up to the chunk buffer 4016170642Srrs * size (508 bytes). For chunks that need to 4017170786Srrs * get more than that they must use the 4018170642Srrs * sctp_m_getptr() function or other means 4019170786Srrs * (e.g. know how to parse mbuf chains). 4020170786Srrs * Cookies do this already. 4021169208Srrs */ 4022169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4023169208Srrs (sizeof(chunk_buf) - 4), 4024169208Srrs chunk_buf); 4025169208Srrs if (ch == NULL) { 4026169208Srrs *offset = length; 4027169378Srrs if (locked_tcb) { 4028169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4029169378Srrs } 4030169208Srrs return (NULL); 4031169208Srrs } 4032169208Srrs } else { 4033169208Srrs /* We can fit it all */ 4034170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4035170642Srrs chk_length, chunk_buf); 4036170642Srrs if (ch == NULL) { 4037170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4038170642Srrs *offset = length; 4039170642Srrs if (locked_tcb) { 4040170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4041169208Srrs } 4042170642Srrs return (NULL); 4043163953Srrs } 4044163953Srrs } 4045163953Srrs } 4046163953Srrs num_chunks++; 4047163953Srrs /* Save off the last place we got a control from */ 4048163953Srrs if (stcb != NULL) { 4049169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4050163953Srrs /* 4051163953Srrs * allow last_control to be NULL if 4052163953Srrs * ASCONF... ASCONF processing will find the 4053163953Srrs * right net later 4054163953Srrs */ 4055169420Srrs if ((netp != NULL) && (*netp != NULL)) 4056169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4057163953Srrs } 4058163953Srrs } 4059163953Srrs#ifdef SCTP_AUDITING_ENABLED 4060163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4061163953Srrs#endif 4062163953Srrs 4063163953Srrs /* check to see if this chunk required auth, but isn't */ 4064163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4065163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 4066163953Srrs stcb->asoc.local_auth_chunks) && 4067163953Srrs !stcb->asoc.authenticated) { 4068163953Srrs /* "silently" ignore */ 4069163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4070163953Srrs goto next_chunk; 4071163953Srrs } 4072163953Srrs switch (ch->chunk_type) { 4073163953Srrs case SCTP_INITIATION: 4074163953Srrs /* must be first and only chunk */ 4075169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4076163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4077163953Srrs /* We are not interested anymore? */ 4078163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4079163953Srrs /* 4080163953Srrs * collision case where we are 4081163953Srrs * sending to them too 4082163953Srrs */ 4083163953Srrs ; 4084163953Srrs } else { 4085169378Srrs if (locked_tcb) { 4086163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4087169378Srrs } 4088163953Srrs *offset = length; 4089163953Srrs return (NULL); 4090163953Srrs } 4091163953Srrs } 4092171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4093171990Srrs (num_chunks > 1) || 4094170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4095163953Srrs *offset = length; 4096169378Srrs if (locked_tcb) { 4097163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4098169378Srrs } 4099163953Srrs return (NULL); 4100163953Srrs } 4101163953Srrs if ((stcb != NULL) && 4102163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4103163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4104163953Srrs sctp_send_shutdown_ack(stcb, 4105163953Srrs stcb->asoc.primary_destination); 4106163953Srrs *offset = length; 4107172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4108169378Srrs if (locked_tcb) { 4109163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4110169378Srrs } 4111163953Srrs return (NULL); 4112163953Srrs } 4113169420Srrs if (netp) { 4114169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4115169420Srrs (struct sctp_init_chunk *)ch, inp, 4116170181Srrs stcb, *netp, &abort_no_unlock, vrf_id); 4117169420Srrs } 4118168299Srrs if (abort_no_unlock) 4119168299Srrs return (NULL); 4120168299Srrs 4121163953Srrs *offset = length; 4122169378Srrs if (locked_tcb) { 4123163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4124169378Srrs } 4125163953Srrs return (NULL); 4126163953Srrs break; 4127170642Srrs case SCTP_PAD_CHUNK: 4128170642Srrs break; 4129163953Srrs case SCTP_INITIATION_ACK: 4130163953Srrs /* must be first and only chunk */ 4131169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 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 { 4137169378Srrs if (locked_tcb) { 4138163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4139169378Srrs } 4140163953Srrs *offset = length; 4141163953Srrs if (stcb) { 4142172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4143172090Srrs so = SCTP_INP_SO(inp); 4144172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4145172090Srrs SCTP_TCB_UNLOCK(stcb); 4146172090Srrs SCTP_SOCKET_LOCK(so, 1); 4147172090Srrs SCTP_TCB_LOCK(stcb); 4148172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4149172090Srrs#endif 4150171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4151172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4152172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4153172090Srrs#endif 4154163953Srrs } 4155163953Srrs return (NULL); 4156163953Srrs } 4157163953Srrs } 4158163953Srrs if ((num_chunks > 1) || 4159170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4160163953Srrs *offset = length; 4161169378Srrs if (locked_tcb) { 4162163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4163169378Srrs } 4164163953Srrs return (NULL); 4165163953Srrs } 4166169420Srrs if ((netp) && (*netp)) { 4167169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4168170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4169169420Srrs } else { 4170169420Srrs ret = -1; 4171169420Srrs } 4172163953Srrs /* 4173163953Srrs * Special case, I must call the output routine to 4174163953Srrs * get the cookie echoed 4175163953Srrs */ 4176168299Srrs if (abort_no_unlock) 4177168299Srrs return (NULL); 4178168299Srrs 4179163953Srrs if ((stcb) && ret == 0) 4180172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4181163953Srrs *offset = length; 4182169378Srrs if (locked_tcb) { 4183163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4184169378Srrs } 4185163953Srrs return (NULL); 4186163953Srrs break; 4187163953Srrs case SCTP_SELECTIVE_ACK: 4188169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4189163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4190163953Srrs { 4191163953Srrs struct sctp_sack_chunk *sack; 4192163953Srrs int abort_now = 0; 4193163953Srrs uint32_t a_rwnd, cum_ack; 4194163953Srrs uint16_t num_seg; 4195163953Srrs int nonce_sum_flag; 4196163953Srrs 4197169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 4198169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 4199171990Srrs ignore_sack: 4200169208Srrs *offset = length; 4201169378Srrs if (locked_tcb) { 4202169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4203169378Srrs } 4204169208Srrs return (NULL); 4205169208Srrs } 4206171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4207171990Srrs /*- 4208171990Srrs * If we have sent a shutdown-ack, we will pay no 4209171990Srrs * attention to a sack sent in to us since 4210171990Srrs * we don't care anymore. 4211171990Srrs */ 4212171990Srrs goto ignore_sack; 4213171990Srrs } 4214163953Srrs sack = (struct sctp_sack_chunk *)ch; 4215163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 4216163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4217163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4218163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4219170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4220170606Srrs cum_ack, 4221170606Srrs num_seg, 4222170606Srrs a_rwnd 4223170606Srrs ); 4224163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4225163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4226163953Srrs (num_seg == 0) && 4227163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4228163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4229163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4230163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4231163953Srrs ) { 4232163953Srrs /* 4233163953Srrs * We have a SIMPLE sack having no 4234163953Srrs * prior segments and data on sent 4235163953Srrs * queue to be acked.. Use the 4236163953Srrs * faster path sack processing. We 4237163953Srrs * also allow window update sacks 4238163953Srrs * with no missing segments to go 4239163953Srrs * this way too. 4240163953Srrs */ 4241169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4242169208Srrs &abort_now); 4243163953Srrs } else { 4244169420Srrs if (netp && *netp) 4245170781Srrs sctp_handle_sack(m, *offset, 4246170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 4247163953Srrs } 4248163953Srrs if (abort_now) { 4249163953Srrs /* ABORT signal from sack processing */ 4250163953Srrs *offset = length; 4251163953Srrs return (NULL); 4252163953Srrs } 4253163953Srrs } 4254163953Srrs break; 4255163953Srrs case SCTP_HEARTBEAT_REQUEST: 4256169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4257169420Srrs if ((stcb) && netp && *netp) { 4258169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4259169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4260169420Srrs chk_length, *netp); 4261169420Srrs 4262169420Srrs /* He's alive so give him credit */ 4263171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4264171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4265171943Srrs stcb->asoc.overall_error_count, 4266171943Srrs 0, 4267171943Srrs SCTP_FROM_SCTP_INPUT, 4268171943Srrs __LINE__); 4269171943Srrs } 4270169420Srrs stcb->asoc.overall_error_count = 0; 4271163953Srrs } 4272163953Srrs break; 4273163953Srrs case SCTP_HEARTBEAT_ACK: 4274169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4275169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4276169208Srrs /* Its not ours */ 4277169352Srrs *offset = length; 4278169378Srrs if (locked_tcb) { 4279169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4280169378Srrs } 4281169208Srrs return (NULL); 4282169208Srrs } 4283163953Srrs /* He's alive so give him credit */ 4284171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4285171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4286171943Srrs stcb->asoc.overall_error_count, 4287171943Srrs 0, 4288171943Srrs SCTP_FROM_SCTP_INPUT, 4289171943Srrs __LINE__); 4290171943Srrs } 4291163953Srrs stcb->asoc.overall_error_count = 0; 4292163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4293169420Srrs if (netp && *netp) 4294169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4295169420Srrs stcb, *netp); 4296163953Srrs break; 4297163953Srrs case SCTP_ABORT_ASSOCIATION: 4298170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4299170091Srrs stcb); 4300169420Srrs if ((stcb) && netp && *netp) 4301169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4302169420Srrs stcb, *netp); 4303163953Srrs *offset = length; 4304163953Srrs return (NULL); 4305163953Srrs break; 4306163953Srrs case SCTP_SHUTDOWN: 4307170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4308170091Srrs stcb); 4309169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4310169208Srrs *offset = length; 4311169378Srrs if (locked_tcb) { 4312169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4313169378Srrs } 4314169208Srrs return (NULL); 4315169420Srrs } 4316169420Srrs if (netp && *netp) { 4317163953Srrs int abort_flag = 0; 4318163953Srrs 4319163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4320163953Srrs stcb, *netp, &abort_flag); 4321163953Srrs if (abort_flag) { 4322163953Srrs *offset = length; 4323163953Srrs return (NULL); 4324163953Srrs } 4325163953Srrs } 4326163953Srrs break; 4327163953Srrs case SCTP_SHUTDOWN_ACK: 4328170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4329169420Srrs if ((stcb) && (netp) && (*netp)) 4330169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4331163953Srrs *offset = length; 4332163953Srrs return (NULL); 4333163953Srrs break; 4334169420Srrs 4335163953Srrs case SCTP_OPERATION_ERROR: 4336169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4337169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4338169420Srrs 4339163953Srrs *offset = length; 4340163953Srrs return (NULL); 4341163953Srrs } 4342163953Srrs break; 4343163953Srrs case SCTP_COOKIE_ECHO: 4344169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4345170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4346163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4347163953Srrs ; 4348163953Srrs } else { 4349169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4350163953Srrs /* We are not interested anymore */ 4351163953Srrs *offset = length; 4352163953Srrs return (NULL); 4353163953Srrs } 4354163953Srrs } 4355163953Srrs /* 4356163953Srrs * First are we accepting? We do this again here 4357169420Srrs * sincen it is possible that a previous endpoint 4358169420Srrs * WAS listening responded to a INIT-ACK and then 4359163953Srrs * closed. We opened and bound.. and are now no 4360163953Srrs * longer listening. 4361163953Srrs */ 4362163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4363163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4364163953Srrs /* 4365163953Srrs * special case, is this a retran'd 4366163953Srrs * COOKIE-ECHO or a restarting assoc 4367163953Srrs * that is a peeled off or 4368163953Srrs * one-to-one style socket. 4369163953Srrs */ 4370163953Srrs goto process_cookie_anyway; 4371163953Srrs } 4372169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4373170181Srrs sh, NULL, vrf_id); 4374163953Srrs *offset = length; 4375163953Srrs return (NULL); 4376163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4377163953Srrs /* we are accepting so check limits like TCP */ 4378163953Srrs if (inp->sctp_socket->so_qlen > 4379163953Srrs inp->sctp_socket->so_qlimit) { 4380163953Srrs /* no space */ 4381163953Srrs struct mbuf *oper; 4382163953Srrs struct sctp_paramhdr *phdr; 4383163953Srrs 4384163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4385163953Srrs oper = NULL; 4386163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4387165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4388163953Srrs if (oper) { 4389165647Srrs SCTP_BUF_LEN(oper) = 4390163953Srrs sizeof(struct sctp_paramhdr); 4391163953Srrs phdr = mtod(oper, 4392163953Srrs struct sctp_paramhdr *); 4393163953Srrs phdr->param_type = 4394163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4395163953Srrs phdr->param_length = 4396163953Srrs htons(sizeof(struct sctp_paramhdr)); 4397163953Srrs } 4398163953Srrs sctp_abort_association(inp, stcb, m, 4399170181Srrs iphlen, sh, oper, vrf_id); 4400163953Srrs } 4401163953Srrs *offset = length; 4402163953Srrs return (NULL); 4403163953Srrs } 4404163953Srrs } 4405163953Srrs process_cookie_anyway: 4406163953Srrs { 4407163953Srrs struct mbuf *ret_buf; 4408165220Srrs struct sctp_inpcb *linp; 4409163953Srrs 4410169420Srrs if (stcb) { 4411165220Srrs linp = NULL; 4412169420Srrs } else { 4413165220Srrs linp = inp; 4414169420Srrs } 4415165220Srrs 4416169420Srrs if (linp) { 4417165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4418169420Srrs } 4419169420Srrs if (netp) { 4420169420Srrs ret_buf = 4421169420Srrs sctp_handle_cookie_echo(m, iphlen, 4422169420Srrs *offset, sh, 4423169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4424169420Srrs &inp, &stcb, netp, 4425169420Srrs auth_skipped, 4426169420Srrs auth_offset, 4427169420Srrs auth_len, 4428169420Srrs &locked_tcb, 4429170181Srrs vrf_id); 4430169420Srrs } else { 4431169420Srrs ret_buf = NULL; 4432169420Srrs } 4433169420Srrs if (linp) { 4434165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4435169420Srrs } 4436163953Srrs if (ret_buf == NULL) { 4437163953Srrs if (locked_tcb) { 4438163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4439163953Srrs } 4440169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4441169420Srrs "GAK, null buffer\n"); 4442163953Srrs auth_skipped = 0; 4443163953Srrs *offset = length; 4444163953Srrs return (NULL); 4445163953Srrs } 4446163953Srrs /* if AUTH skipped, see if it verified... */ 4447163953Srrs if (auth_skipped) { 4448163953Srrs got_auth = 1; 4449163953Srrs auth_skipped = 0; 4450163953Srrs } 4451163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4452163953Srrs /* 4453163953Srrs * Restart the timer if we have 4454163953Srrs * pending data 4455163953Srrs */ 4456163953Srrs struct sctp_tmit_chunk *chk; 4457163953Srrs 4458163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4459163953Srrs if (chk) { 4460163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4461163953Srrs stcb->sctp_ep, stcb, 4462163953Srrs chk->whoTo); 4463163953Srrs } 4464163953Srrs } 4465163953Srrs } 4466163953Srrs break; 4467163953Srrs case SCTP_COOKIE_ACK: 4468170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4469169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4470169378Srrs if (locked_tcb) { 4471169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4472169378Srrs } 4473169352Srrs return (NULL); 4474169352Srrs } 4475163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4476163953Srrs /* We are not interested anymore */ 4477163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4478163953Srrs ; 4479169420Srrs } else if (stcb) { 4480172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4481172090Srrs so = SCTP_INP_SO(inp); 4482172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4483172090Srrs SCTP_TCB_UNLOCK(stcb); 4484172090Srrs SCTP_SOCKET_LOCK(so, 1); 4485172090Srrs SCTP_TCB_LOCK(stcb); 4486172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4487172090Srrs#endif 4488171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4489172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4490172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4491172090Srrs#endif 4492163953Srrs *offset = length; 4493163953Srrs return (NULL); 4494163953Srrs } 4495163953Srrs } 4496163953Srrs /* He's alive so give him credit */ 4497169420Srrs if ((stcb) && netp && *netp) { 4498171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4499171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4500171943Srrs stcb->asoc.overall_error_count, 4501171943Srrs 0, 4502171943Srrs SCTP_FROM_SCTP_INPUT, 4503171943Srrs __LINE__); 4504171943Srrs } 4505169378Srrs stcb->asoc.overall_error_count = 0; 4506169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4507169378Srrs } 4508163953Srrs break; 4509163953Srrs case SCTP_ECN_ECHO: 4510169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4511163953Srrs /* He's alive so give him credit */ 4512169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4513169208Srrs /* Its not ours */ 4514169378Srrs if (locked_tcb) { 4515169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4516169378Srrs } 4517169208Srrs *offset = length; 4518169208Srrs return (NULL); 4519169208Srrs } 4520169378Srrs if (stcb) { 4521171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4522171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4523171943Srrs stcb->asoc.overall_error_count, 4524171943Srrs 0, 4525171943Srrs SCTP_FROM_SCTP_INPUT, 4526171943Srrs __LINE__); 4527171943Srrs } 4528169378Srrs stcb->asoc.overall_error_count = 0; 4529169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4530169378Srrs stcb); 4531169378Srrs } 4532163953Srrs break; 4533163953Srrs case SCTP_ECN_CWR: 4534169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4535163953Srrs /* He's alive so give him credit */ 4536169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4537169208Srrs /* Its not ours */ 4538169378Srrs if (locked_tcb) { 4539169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4540169378Srrs } 4541169208Srrs *offset = length; 4542169208Srrs return (NULL); 4543169208Srrs } 4544169378Srrs if (stcb) { 4545171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4546171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4547171943Srrs stcb->asoc.overall_error_count, 4548171943Srrs 0, 4549171943Srrs SCTP_FROM_SCTP_INPUT, 4550171943Srrs __LINE__); 4551171943Srrs } 4552169378Srrs stcb->asoc.overall_error_count = 0; 4553169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4554169378Srrs } 4555163953Srrs break; 4556163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4557170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4558163953Srrs /* must be first and only chunk */ 4559163953Srrs if ((num_chunks > 1) || 4560170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4561163953Srrs *offset = length; 4562169378Srrs if (locked_tcb) { 4563163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4564169378Srrs } 4565163953Srrs return (NULL); 4566163953Srrs } 4567169420Srrs if ((stcb) && netp && *netp) { 4568169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4569169378Srrs stcb, *netp); 4570169378Srrs } 4571163953Srrs *offset = length; 4572163953Srrs return (NULL); 4573163953Srrs break; 4574163953Srrs case SCTP_ASCONF: 4575169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4576163953Srrs /* He's alive so give him credit */ 4577169378Srrs if (stcb) { 4578171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4579171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4580171943Srrs stcb->asoc.overall_error_count, 4581171943Srrs 0, 4582171943Srrs SCTP_FROM_SCTP_INPUT, 4583171943Srrs __LINE__); 4584171943Srrs } 4585169378Srrs stcb->asoc.overall_error_count = 0; 4586169378Srrs sctp_handle_asconf(m, *offset, 4587171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 4588171990Srrs asconf_cnt++; 4589169378Srrs } 4590163953Srrs break; 4591163953Srrs case SCTP_ASCONF_ACK: 4592169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4593169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4594169208Srrs /* Its not ours */ 4595169378Srrs if (locked_tcb) { 4596169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4597169378Srrs } 4598169208Srrs *offset = length; 4599169208Srrs return (NULL); 4600169208Srrs } 4601169420Srrs if ((stcb) && netp && *netp) { 4602169378Srrs /* He's alive so give him credit */ 4603171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4604171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4605171943Srrs stcb->asoc.overall_error_count, 4606171943Srrs 0, 4607171943Srrs SCTP_FROM_SCTP_INPUT, 4608171943Srrs __LINE__); 4609171943Srrs } 4610169378Srrs stcb->asoc.overall_error_count = 0; 4611169378Srrs sctp_handle_asconf_ack(m, *offset, 4612169378Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4613169378Srrs } 4614163953Srrs break; 4615163953Srrs case SCTP_FORWARD_CUM_TSN: 4616169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4617169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4618169208Srrs /* Its not ours */ 4619169378Srrs if (locked_tcb) { 4620169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4621169378Srrs } 4622169208Srrs *offset = length; 4623169208Srrs return (NULL); 4624169208Srrs } 4625163953Srrs /* He's alive so give him credit */ 4626169378Srrs if (stcb) { 4627163953Srrs int abort_flag = 0; 4628163953Srrs 4629163953Srrs stcb->asoc.overall_error_count = 0; 4630171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4631171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4632171943Srrs stcb->asoc.overall_error_count, 4633171943Srrs 0, 4634171943Srrs SCTP_FROM_SCTP_INPUT, 4635171943Srrs __LINE__); 4636171943Srrs } 4637163953Srrs *fwd_tsn_seen = 1; 4638163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4639163953Srrs /* We are not interested anymore */ 4640172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4641172090Srrs so = SCTP_INP_SO(inp); 4642172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4643172090Srrs SCTP_TCB_UNLOCK(stcb); 4644172090Srrs SCTP_SOCKET_LOCK(so, 1); 4645172090Srrs SCTP_TCB_LOCK(stcb); 4646172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4647172090Srrs#endif 4648171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4649172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4650172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4651172090Srrs#endif 4652163953Srrs *offset = length; 4653163953Srrs return (NULL); 4654163953Srrs } 4655163953Srrs sctp_handle_forward_tsn(stcb, 4656170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 4657163953Srrs if (abort_flag) { 4658163953Srrs *offset = length; 4659163953Srrs return (NULL); 4660163953Srrs } else { 4661171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4662171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4663171943Srrs stcb->asoc.overall_error_count, 4664171943Srrs 0, 4665171943Srrs SCTP_FROM_SCTP_INPUT, 4666171943Srrs __LINE__); 4667171943Srrs } 4668163953Srrs stcb->asoc.overall_error_count = 0; 4669163953Srrs } 4670163953Srrs 4671163953Srrs } 4672163953Srrs break; 4673163953Srrs case SCTP_STREAM_RESET: 4674169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4675169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4676169208Srrs /* Its not ours */ 4677169378Srrs if (locked_tcb) { 4678169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4679169378Srrs } 4680169208Srrs *offset = length; 4681169208Srrs return (NULL); 4682169208Srrs } 4683163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4684163953Srrs /* We are not interested anymore */ 4685172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4686172090Srrs so = SCTP_INP_SO(inp); 4687172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4688172090Srrs SCTP_TCB_UNLOCK(stcb); 4689172090Srrs SCTP_SOCKET_LOCK(so, 1); 4690172090Srrs SCTP_TCB_LOCK(stcb); 4691172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4692172090Srrs#endif 4693171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4694172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4695172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4696172090Srrs#endif 4697163953Srrs *offset = length; 4698163953Srrs return (NULL); 4699163953Srrs } 4700169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4701169420Srrs /* 4702169420Srrs * hmm, peer should have announced this, but 4703169420Srrs * we will turn it on since he is sending us 4704169420Srrs * a stream reset. 4705169420Srrs */ 4706169420Srrs stcb->asoc.peer_supports_strreset = 1; 4707163953Srrs } 4708170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 4709169420Srrs /* stop processing */ 4710169420Srrs *offset = length; 4711169420Srrs return (NULL); 4712169420Srrs } 4713163953Srrs break; 4714163953Srrs case SCTP_PACKET_DROPPED: 4715169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4716163953Srrs /* re-get it all please */ 4717169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4718169208Srrs /* Its not ours */ 4719169378Srrs if (locked_tcb) { 4720169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4721169378Srrs } 4722169208Srrs *offset = length; 4723169208Srrs return (NULL); 4724169208Srrs } 4725169420Srrs if (ch && (stcb) && netp && (*netp)) { 4726169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4727170781Srrs stcb, *netp, 4728170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4729170781Srrs 4730169378Srrs } 4731163953Srrs break; 4732163953Srrs 4733163953Srrs case SCTP_AUTHENTICATION: 4734169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4735163953Srrs if (sctp_auth_disable) 4736163953Srrs goto unknown_chunk; 4737163953Srrs 4738163953Srrs if (stcb == NULL) { 4739163953Srrs /* save the first AUTH for later processing */ 4740163953Srrs if (auth_skipped == 0) { 4741163953Srrs auth_offset = *offset; 4742163953Srrs auth_len = chk_length; 4743163953Srrs auth_skipped = 1; 4744163953Srrs } 4745163953Srrs /* skip this chunk (temporarily) */ 4746163953Srrs goto next_chunk; 4747163953Srrs } 4748169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4749169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4750169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4751169208Srrs /* Its not ours */ 4752169378Srrs if (locked_tcb) { 4753169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4754169378Srrs } 4755169208Srrs *offset = length; 4756169208Srrs return (NULL); 4757169208Srrs } 4758163953Srrs if (got_auth == 1) { 4759163953Srrs /* skip this chunk... it's already auth'd */ 4760163953Srrs goto next_chunk; 4761163953Srrs } 4762163953Srrs got_auth = 1; 4763169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4764163953Srrs m, *offset)) { 4765163953Srrs /* auth HMAC failed so dump the packet */ 4766163953Srrs *offset = length; 4767163953Srrs return (stcb); 4768163953Srrs } else { 4769163953Srrs /* remaining chunks are HMAC checked */ 4770163953Srrs stcb->asoc.authenticated = 1; 4771163953Srrs } 4772163953Srrs break; 4773163953Srrs 4774163953Srrs default: 4775163953Srrs unknown_chunk: 4776163953Srrs /* it's an unknown chunk! */ 4777163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4778163953Srrs struct mbuf *mm; 4779163953Srrs struct sctp_paramhdr *phd; 4780163953Srrs 4781163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4782165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4783163953Srrs if (mm) { 4784163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4785163953Srrs /* 4786163953Srrs * We cheat and use param type since 4787163953Srrs * we did not bother to define a 4788163953Srrs * error cause struct. They are the 4789163953Srrs * same basic format with different 4790163953Srrs * names. 4791163953Srrs */ 4792163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4793163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4794165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4795166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4796163953Srrs M_DONTWAIT); 4797165647Srrs if (SCTP_BUF_NEXT(mm)) { 4798163953Srrs sctp_queue_op_err(stcb, mm); 4799163953Srrs } else { 4800163953Srrs sctp_m_freem(mm); 4801163953Srrs } 4802163953Srrs } 4803163953Srrs } 4804163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4805163953Srrs /* discard this packet */ 4806163953Srrs *offset = length; 4807163953Srrs return (stcb); 4808163953Srrs } /* else skip this bad chunk and continue... */ 4809163953Srrs break; 4810163953Srrs } /* switch (ch->chunk_type) */ 4811163953Srrs 4812163953Srrs 4813163953Srrsnext_chunk: 4814163953Srrs /* get the next chunk */ 4815163953Srrs *offset += SCTP_SIZE32(chk_length); 4816163953Srrs if (*offset >= length) { 4817163953Srrs /* no more data left in the mbuf chain */ 4818163953Srrs break; 4819163953Srrs } 4820163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4821163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4822163953Srrs if (ch == NULL) { 4823169378Srrs if (locked_tcb) { 4824163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4825169378Srrs } 4826163953Srrs *offset = length; 4827163953Srrs return (NULL); 4828163953Srrs } 4829163953Srrs } /* while */ 4830171990Srrs 4831171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 4832171990Srrs sctp_send_asconf_ack(stcb); 4833171990Srrs } 4834163953Srrs return (stcb); 4835163953Srrs} 4836163953Srrs 4837163953Srrs 4838163953Srrs/* 4839163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4840163953Srrs * ECN(0) or ECN(1) or CE 4841163953Srrs */ 4842170806Srrsstatic void 4843163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4844163953Srrs uint8_t ecn_bits) 4845163953Srrs{ 4846163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4847163953Srrs ; 4848163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4849163953Srrs /* 4850163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4851163953Srrs * change the NS bit (that we have yet to find a way to send 4852163953Srrs * it yet). 4853163953Srrs */ 4854163953Srrs 4855163953Srrs /* ECN Nonce stuff */ 4856163953Srrs stcb->asoc.receiver_nonce_sum++; 4857163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4858163953Srrs 4859163953Srrs /* 4860163953Srrs * Drag up the last_echo point if cumack is larger since we 4861163953Srrs * don't want the point falling way behind by more than 4862163953Srrs * 2^^31 and then having it be incorrect. 4863163953Srrs */ 4864163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4865163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4866163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4867163953Srrs } 4868163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4869163953Srrs /* 4870163953Srrs * Drag up the last_echo point if cumack is larger since we 4871163953Srrs * don't want the point falling way behind by more than 4872163953Srrs * 2^^31 and then having it be incorrect. 4873163953Srrs */ 4874163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4875163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4876163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4877163953Srrs } 4878163953Srrs } 4879163953Srrs} 4880163953Srrs 4881170806Srrsstatic void 4882163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4883163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4884163953Srrs{ 4885163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4886163953Srrs /* 4887163953Srrs * we possibly must notify the sender that a congestion 4888163953Srrs * window reduction is in order. We do this by adding a ECNE 4889163953Srrs * chunk to the output chunk queue. The incoming CWR will 4890163953Srrs * remove this chunk. 4891163953Srrs */ 4892163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4893163953Srrs MAX_TSN)) { 4894163953Srrs /* Yep, we need to add a ECNE */ 4895163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4896163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4897163953Srrs } 4898163953Srrs } 4899163953Srrs} 4900163953Srrs 4901171440Srrs#ifdef INVARIANTS 4902171440Srrsstatic void 4903171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 4904171440Srrs{ 4905171440Srrs struct sctp_tcb *stcb; 4906171440Srrs 4907171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4908171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 4909171440Srrs panic("Own lock on stcb at return from input"); 4910171440Srrs } 4911171440Srrs } 4912171440Srrs} 4913171440Srrs 4914171440Srrs#endif 4915171440Srrs 4916163953Srrs/* 4917163953Srrs * common input chunk processing (v4 and v6) 4918163953Srrs */ 4919169378Srrsvoid 4920163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4921163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4922163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4923170181Srrs uint8_t ecn_bits, uint32_t vrf_id) 4924163953Srrs{ 4925163953Srrs /* 4926163953Srrs * Control chunk processing 4927163953Srrs */ 4928163953Srrs uint32_t high_tsn; 4929163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4930163953Srrs struct mbuf *m = *mm; 4931163953Srrs int abort_flag = 0; 4932163953Srrs int un_sent; 4933163953Srrs 4934163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4935163953Srrs#ifdef SCTP_AUDITING_ENABLED 4936163953Srrs sctp_audit_log(0xE0, 1); 4937163953Srrs sctp_auditing(0, inp, stcb, net); 4938163953Srrs#endif 4939163953Srrs 4940171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d stcb:%p\n", 4941171990Srrs m, iphlen, offset, stcb); 4942163953Srrs if (stcb) { 4943163953Srrs /* always clear this before beginning a packet */ 4944163953Srrs stcb->asoc.authenticated = 0; 4945163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4946171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 4947171990Srrs stcb, stcb->asoc.state); 4948171990Srrs 4949171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 4950171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 4951171745Srrs /*- 4952171745Srrs * If we hit here, we had a ref count 4953171745Srrs * up when the assoc was aborted and the 4954171745Srrs * timer is clearing out the assoc, we should 4955171745Srrs * NOT respond to any packet.. its OOTB. 4956171745Srrs */ 4957171745Srrs SCTP_TCB_UNLOCK(stcb); 4958171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4959171745Srrs vrf_id); 4960171745Srrs goto out_now; 4961171745Srrs } 4962163953Srrs } 4963163953Srrs if (IS_SCTP_CONTROL(ch)) { 4964163953Srrs /* process the control portion of the SCTP packet */ 4965169655Srrs /* sa_ignore NO_NULL_CHK */ 4966163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4967170181Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id); 4968163953Srrs if (stcb) { 4969163953Srrs /* 4970163953Srrs * This covers us if the cookie-echo was there and 4971163953Srrs * it changes our INP. 4972163953Srrs */ 4973163953Srrs inp = stcb->sctp_ep; 4974163953Srrs } 4975163953Srrs } else { 4976163953Srrs /* 4977163953Srrs * no control chunks, so pre-process DATA chunks (these 4978163953Srrs * checks are taken care of by control processing) 4979163953Srrs */ 4980163953Srrs 4981163953Srrs /* 4982163953Srrs * if DATA only packet, and auth is required, then punt... 4983163953Srrs * can't have authenticated without any AUTH (control) 4984163953Srrs * chunks 4985163953Srrs */ 4986163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4987163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4988163953Srrs stcb->asoc.local_auth_chunks)) { 4989163953Srrs /* "silently" ignore */ 4990163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4991163953Srrs SCTP_TCB_UNLOCK(stcb); 4992171440Srrs goto out_now; 4993163953Srrs } 4994163953Srrs if (stcb == NULL) { 4995163953Srrs /* out of the blue DATA chunk */ 4996169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4997170181Srrs vrf_id); 4998171440Srrs goto out_now; 4999163953Srrs } 5000163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5001163953Srrs /* v_tag mismatch! */ 5002163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5003163953Srrs SCTP_TCB_UNLOCK(stcb); 5004171440Srrs goto out_now; 5005163953Srrs } 5006163953Srrs } 5007163953Srrs 5008163953Srrs if (stcb == NULL) { 5009163953Srrs /* 5010163953Srrs * no valid TCB for this packet, or we found it's a bad 5011163953Srrs * packet while processing control, or we're done with this 5012163953Srrs * packet (done or skip rest of data), so we drop it... 5013163953Srrs */ 5014171440Srrs goto out_now; 5015163953Srrs } 5016163953Srrs /* 5017163953Srrs * DATA chunk processing 5018163953Srrs */ 5019163953Srrs /* plow through the data chunks while length > offset */ 5020163953Srrs 5021163953Srrs /* 5022163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5023163953Srrs * DATA is required. 5024163953Srrs */ 5025165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 5026163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 5027163953Srrs stcb->asoc.local_auth_chunks) && 5028163953Srrs !stcb->asoc.authenticated) { 5029163953Srrs /* "silently" ignore */ 5030163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5031169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5032169420Srrs "Data chunk requires AUTH, skipped\n"); 5033165220Srrs goto trigger_send; 5034163953Srrs } 5035163953Srrs if (length > offset) { 5036163953Srrs int retval; 5037163953Srrs 5038163953Srrs /* 5039163953Srrs * First check to make sure our state is correct. We would 5040163953Srrs * not get here unless we really did have a tag, so we don't 5041163953Srrs * abort if this happens, just dump the chunk silently. 5042163953Srrs */ 5043163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5044163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5045163953Srrs /* 5046163953Srrs * we consider data with valid tags in this state 5047163953Srrs * shows us the cookie-ack was lost. Imply it was 5048163953Srrs * there. 5049163953Srrs */ 5050171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 5051171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5052171943Srrs stcb->asoc.overall_error_count, 5053171943Srrs 0, 5054171943Srrs SCTP_FROM_SCTP_INPUT, 5055171943Srrs __LINE__); 5056171943Srrs } 5057163953Srrs stcb->asoc.overall_error_count = 0; 5058163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5059163953Srrs break; 5060163953Srrs case SCTP_STATE_COOKIE_WAIT: 5061163953Srrs /* 5062163953Srrs * We consider OOTB any data sent during asoc setup. 5063163953Srrs */ 5064169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5065170181Srrs vrf_id); 5066163953Srrs SCTP_TCB_UNLOCK(stcb); 5067171440Srrs goto out_now; 5068171531Srrs /* sa_ignore NOTREACHED */ 5069163953Srrs break; 5070163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5071163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5072163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5073163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5074163953Srrs default: 5075163953Srrs SCTP_TCB_UNLOCK(stcb); 5076171440Srrs goto out_now; 5077171531Srrs /* sa_ignore NOTREACHED */ 5078163953Srrs break; 5079163953Srrs case SCTP_STATE_OPEN: 5080163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5081163953Srrs break; 5082163953Srrs } 5083163953Srrs /* take care of ECN, part 1. */ 5084163953Srrs if (stcb->asoc.ecn_allowed && 5085163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5086163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5087163953Srrs } 5088163953Srrs /* plow through the data chunks while length > offset */ 5089163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5090163953Srrs inp, stcb, net, &high_tsn); 5091163953Srrs if (retval == 2) { 5092163953Srrs /* 5093163953Srrs * The association aborted, NO UNLOCK needed since 5094163953Srrs * the association is destroyed. 5095163953Srrs */ 5096171440Srrs goto out_now; 5097163953Srrs } 5098163953Srrs data_processed = 1; 5099163953Srrs if (retval == 0) { 5100163953Srrs /* take care of ecn part 2. */ 5101163953Srrs if (stcb->asoc.ecn_allowed && 5102163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5103163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5104163953Srrs ecn_bits); 5105163953Srrs } 5106163953Srrs } 5107163953Srrs /* 5108163953Srrs * Anything important needs to have been m_copy'ed in 5109163953Srrs * process_data 5110163953Srrs */ 5111163953Srrs } 5112163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5113163953Srrs int was_a_gap = 0; 5114163953Srrs 5115163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5116163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5117163953Srrs /* there was a gap before this data was processed */ 5118163953Srrs was_a_gap = 1; 5119163953Srrs } 5120163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 5121163953Srrs if (abort_flag) { 5122163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5123171440Srrs goto out_now; 5124163953Srrs } 5125163953Srrs } 5126163953Srrs /* trigger send of any chunks in queue... */ 5127165220Srrstrigger_send: 5128163953Srrs#ifdef SCTP_AUDITING_ENABLED 5129163953Srrs sctp_audit_log(0xE0, 2); 5130163953Srrs sctp_auditing(1, inp, stcb, net); 5131163953Srrs#endif 5132169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5133169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5134169420Srrs stcb->asoc.peers_rwnd, 5135169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5136169420Srrs stcb->asoc.total_flight); 5137163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5138163953Srrs 5139163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5140163953Srrs ((un_sent) && 5141163953Srrs (stcb->asoc.peers_rwnd > 0 || 5142163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5143169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5144172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5145169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5146163953Srrs } 5147163953Srrs#ifdef SCTP_AUDITING_ENABLED 5148163953Srrs sctp_audit_log(0xE0, 3); 5149163953Srrs sctp_auditing(2, inp, stcb, net); 5150163953Srrs#endif 5151163953Srrs SCTP_TCB_UNLOCK(stcb); 5152171440Srrsout_now: 5153171440Srrs#ifdef INVARIANTS 5154171440Srrs sctp_validate_no_locks(inp); 5155171440Srrs#endif 5156169378Srrs return; 5157163953Srrs} 5158163953Srrs 5159163953Srrs 5160163953Srrs 5161163953Srrsvoid 5162165647Srrssctp_input(i_pak, off) 5163165647Srrs struct mbuf *i_pak; 5164163953Srrs int off; 5165163953Srrs 5166163953Srrs{ 5167163953Srrs#ifdef SCTP_MBUF_LOGGING 5168163953Srrs struct mbuf *mat; 5169163953Srrs 5170163953Srrs#endif 5171165647Srrs struct mbuf *m; 5172163953Srrs int iphlen; 5173170181Srrs uint32_t vrf_id = 0; 5174163953Srrs uint8_t ecn_bits; 5175163953Srrs struct ip *ip; 5176163953Srrs struct sctphdr *sh; 5177163953Srrs struct sctp_inpcb *inp = NULL; 5178163953Srrs 5179163953Srrs uint32_t check, calc_check; 5180163953Srrs struct sctp_nets *net; 5181163953Srrs struct sctp_tcb *stcb = NULL; 5182163953Srrs struct sctp_chunkhdr *ch; 5183163953Srrs int refcount_up = 0; 5184163953Srrs int length, mlen, offset; 5185163953Srrs 5186169352Srrs 5187169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5188169352Srrs SCTP_RELEASE_PKT(i_pak); 5189169352Srrs return; 5190169352Srrs } 5191168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5192163953Srrs iphlen = off; 5193165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5194170056Srrs 5195163953Srrs net = NULL; 5196163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5197163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5198163953Srrs 5199170091Srrs 5200163953Srrs#ifdef SCTP_MBUF_LOGGING 5201163953Srrs /* Log in any input mbufs */ 5202170744Srrs if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) { 5203170744Srrs mat = m; 5204170744Srrs while (mat) { 5205170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5206170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5207170744Srrs } 5208170744Srrs mat = SCTP_BUF_NEXT(mat); 5209163953Srrs } 5210163953Srrs } 5211163953Srrs#endif 5212170091Srrs#ifdef SCTP_PACKET_LOGGING 5213170921Srrs if (sctp_logging_level & SCTP_LAST_PACKET_TRACING) 5214170921Srrs sctp_packet_log(m, mlen); 5215170091Srrs#endif 5216170091Srrs /* 5217170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5218170091Srrs * case) 5219170091Srrs */ 5220170091Srrs mlen -= iphlen; 5221168124Srrs 5222163953Srrs /* 5223163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5224163953Srrs */ 5225163953Srrs ip = mtod(m, struct ip *); 5226163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5227165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5228163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5229163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5230163953Srrs return; 5231163953Srrs } 5232163953Srrs ip = mtod(m, struct ip *); 5233163953Srrs } 5234163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5235163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5236170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5237170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5238163953Srrs 5239163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5240163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5241163953Srrs goto bad; 5242163953Srrs } 5243168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5244163953Srrs /* 5245163953Srrs * We only look at broadcast if its a front state, All 5246163953Srrs * others we will not have a tcb for anyway. 5247163953Srrs */ 5248163953Srrs goto bad; 5249163953Srrs } 5250163953Srrs /* validate SCTP checksum */ 5251171990Srrs check = sh->checksum; /* save incoming checksum */ 5252171990Srrs if ((check == 0) && (sctp_no_csum_on_loopback) && 5253171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5254171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5255171990Srrs ) { 5256171990Srrs goto sctp_skip_csum_4; 5257171990Srrs } 5258171990Srrs sh->checksum = 0; /* prepare for calc */ 5259171990Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 5260171990Srrs if (calc_check != check) { 5261171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5262171990Srrs calc_check, check, m, mlen, iphlen); 5263163953Srrs 5264171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5265171990Srrs offset - sizeof(*ch), 5266171990Srrs sh, ch, &inp, &net, 5267171990Srrs vrf_id); 5268171990Srrs if ((inp) && (stcb)) { 5269171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5270172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5271171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5272171990Srrs refcount_up = 1; 5273163953Srrs } 5274171990Srrs SCTP_STAT_INCR(sctps_badsum); 5275171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5276171990Srrs goto bad; 5277168299Srrs } 5278171990Srrs sh->checksum = calc_check; 5279163953Srrssctp_skip_csum_4: 5280168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5281168124Srrs if (sh->dest_port == 0) { 5282168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5283168124Srrs goto bad; 5284168124Srrs } 5285163953Srrs /* validate mbuf chain length with IP payload length */ 5286163953Srrs if (mlen < (ip->ip_len - iphlen)) { 5287163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5288163953Srrs goto bad; 5289163953Srrs } 5290163953Srrs /* 5291163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5292163953Srrs * IP/SCTP/first chunk header... 5293163953Srrs */ 5294163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5295168299Srrs sh, ch, &inp, &net, vrf_id); 5296163953Srrs /* inp's ref-count increased && stcb locked */ 5297163953Srrs if (inp == NULL) { 5298163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5299163953Srrs 5300163953Srrs SCTP_STAT_INCR(sctps_noport); 5301163953Srrs#ifdef ICMP_BANDLIM 5302163953Srrs /* 5303163953Srrs * we use the bandwidth limiting to protect against sending 5304163953Srrs * too many ABORTS all at once. In this case these count the 5305163953Srrs * same as an ICMP message. 5306163953Srrs */ 5307163953Srrs if (badport_bandlim(0) < 0) 5308163953Srrs goto bad; 5309163953Srrs#endif /* ICMP_BANDLIM */ 5310169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5311169420Srrs "Sending a ABORT from packet entry!\n"); 5312163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5313163953Srrs /* 5314163953Srrs * we do a trick here to get the INIT tag, dig in 5315163953Srrs * and get the tag from the INIT and put it in the 5316163953Srrs * common header. 5317163953Srrs */ 5318163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5319163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5320163953Srrs (uint8_t *) & chunk_buf); 5321163953Srrs if (init_chk != NULL) 5322163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5323163953Srrs } 5324165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5325170181Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id); 5326165220Srrs goto bad; 5327165220Srrs } 5328165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5329165220Srrs goto bad; 5330165220Srrs } 5331165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5332170181Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id); 5333163953Srrs goto bad; 5334163953Srrs } else if (stcb == NULL) { 5335163953Srrs refcount_up = 1; 5336163953Srrs } 5337171167Sgnn#ifdef IPSEC 5338163953Srrs /* 5339163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5340163953Srrs * idea, so I will leave it in place. 5341163953Srrs */ 5342163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5343171133Sgnn ipsec4stat.in_polvio++; 5344163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5345163953Srrs goto bad; 5346163953Srrs } 5347163953Srrs#endif /* IPSEC */ 5348163953Srrs 5349163953Srrs /* 5350163953Srrs * common chunk processing 5351163953Srrs */ 5352163953Srrs length = ip->ip_len + iphlen; 5353163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5354163953Srrs 5355163953Srrs ecn_bits = ip->ip_tos; 5356163953Srrs 5357169655Srrs /* sa_ignore NO_NULL_CHK */ 5358163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5359170181Srrs inp, stcb, net, ecn_bits, vrf_id); 5360163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5361163953Srrs if (m) { 5362163953Srrs sctp_m_freem(m); 5363163953Srrs } 5364163953Srrs if ((inp) && (refcount_up)) { 5365163953Srrs /* reduce ref-count */ 5366163953Srrs SCTP_INP_DECR_REF(inp); 5367163953Srrs } 5368163953Srrs return; 5369163953Srrsbad: 5370169420Srrs if (stcb) { 5371163953Srrs SCTP_TCB_UNLOCK(stcb); 5372169420Srrs } 5373163953Srrs if ((inp) && (refcount_up)) { 5374163953Srrs /* reduce ref-count */ 5375163953Srrs SCTP_INP_DECR_REF(inp); 5376163953Srrs } 5377163953Srrs if (m) { 5378163953Srrs sctp_m_freem(m); 5379163953Srrs } 5380163953Srrs return; 5381163953Srrs} 5382