sctp_input.c revision 180387
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 180387 2008-07-09 16:45:30Z rrs $"); 35163953Srrs 36163953Srrs#include <netinet/sctp_os.h> 37163953Srrs#include <netinet/sctp_var.h> 38167598Srrs#include <netinet/sctp_sysctl.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctputil.h> 42163953Srrs#include <netinet/sctp_output.h> 43163953Srrs#include <netinet/sctp_input.h> 44163953Srrs#include <netinet/sctp_auth.h> 45163953Srrs#include <netinet/sctp_indata.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47170091Srrs#include <netinet/sctp_bsd_addr.h> 48172091Srrs#include <netinet/sctp_timer.h> 49179157Srrs#include <netinet/udp.h> 50163953Srrs 51163953Srrs 52163953Srrs 53163953Srrsstatic void 54163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 55163953Srrs{ 56163953Srrs struct sctp_nets *net; 57163953Srrs 58165220Srrs /* 59165220Srrs * This now not only stops all cookie timers it also stops any INIT 60165220Srrs * timers as well. This will make sure that the timers are stopped 61165220Srrs * in all collision cases. 62165220Srrs */ 63163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 64163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 65165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 66163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 67163953Srrs stcb->sctp_ep, 68163953Srrs stcb, 69165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 70165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 71165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 72165220Srrs stcb->sctp_ep, 73165220Srrs stcb, 74165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 75163953Srrs } 76163953Srrs } 77163953Srrs} 78163953Srrs 79163953Srrs/* INIT handler */ 80163953Srrsstatic void 81163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 82163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 83179157Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 84163953Srrs{ 85163953Srrs struct sctp_init *init; 86163953Srrs struct mbuf *op_err; 87163953Srrs uint32_t init_limit; 88163953Srrs 89169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 90169420Srrs stcb); 91172396Srrs if (stcb == NULL) { 92172396Srrs SCTP_INP_RLOCK(inp); 93172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 94172396Srrs goto outnow; 95172396Srrs } 96172396Srrs } 97163953Srrs op_err = NULL; 98163953Srrs init = &cp->init; 99163953Srrs /* First are we accepting? */ 100163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 101169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 102169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 103169420Srrs inp->sctp_socket->so_qlimit); 104163953Srrs /* 105163953Srrs * FIX ME ?? What about TCP model and we have a 106179141Srrs * match/restart case? Actually no fix is needed. the lookup 107179141Srrs * will always find the existing assoc so stcb would not be 108179141Srrs * NULL. It may be questionable to do this since we COULD 109179141Srrs * just send back the INIT-ACK and hope that the app did 110179141Srrs * accept()'s by the time the COOKIE was sent. But there is 111179141Srrs * a price to pay for COOKIE generation and I don't want to 112179141Srrs * pay it on the chance that the app will actually do some 113179141Srrs * accepts(). The App just looses and should NOT be in this 114179141Srrs * state :-) 115163953Srrs */ 116169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 117179157Srrs vrf_id, port); 118168299Srrs if (stcb) 119168299Srrs *abort_no_unlock = 1; 120172396Srrs goto outnow; 121163953Srrs } 122163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 123163953Srrs /* Invalid length */ 124163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 125169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 126179157Srrs vrf_id, port); 127168299Srrs if (stcb) 128168299Srrs *abort_no_unlock = 1; 129172396Srrs goto outnow; 130163953Srrs } 131163953Srrs /* validate parameters */ 132163953Srrs if (init->initiate_tag == 0) { 133163953Srrs /* protocol error... send abort */ 134163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 135169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 136179157Srrs vrf_id, port); 137168299Srrs if (stcb) 138168299Srrs *abort_no_unlock = 1; 139172396Srrs goto outnow; 140163953Srrs } 141163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 142163953Srrs /* invalid parameter... send abort */ 143163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 144169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 145179157Srrs vrf_id, port); 146171440Srrs if (stcb) 147171440Srrs *abort_no_unlock = 1; 148172396Srrs goto outnow; 149163953Srrs } 150163953Srrs if (init->num_inbound_streams == 0) { 151163953Srrs /* protocol error... send abort */ 152163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 153169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 154179157Srrs vrf_id, port); 155168299Srrs if (stcb) 156168299Srrs *abort_no_unlock = 1; 157172396Srrs goto outnow; 158163953Srrs } 159163953Srrs if (init->num_outbound_streams == 0) { 160163953Srrs /* protocol error... send abort */ 161163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 162169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 163179157Srrs vrf_id, port); 164168299Srrs if (stcb) 165168299Srrs *abort_no_unlock = 1; 166172396Srrs goto outnow; 167163953Srrs } 168163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 169163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 170163953Srrs init_limit)) { 171163953Srrs /* auth parameter(s) error... send abort */ 172179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 173168299Srrs if (stcb) 174168299Srrs *abort_no_unlock = 1; 175172396Srrs goto outnow; 176163953Srrs } 177163953Srrs /* send an INIT-ACK w/cookie */ 178169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 179179157Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 180172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 181172396Srrsoutnow: 182172396Srrs if (stcb == NULL) { 183172396Srrs SCTP_INP_RUNLOCK(inp); 184172396Srrs } 185163953Srrs} 186163953Srrs 187163953Srrs/* 188163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 189163953Srrs */ 190171158Srrs 191171158Srrsint 192171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 193171158Srrs{ 194171158Srrs int unsent_data = 0; 195171158Srrs struct sctp_stream_queue_pending *sp; 196171158Srrs struct sctp_stream_out *strq; 197171158Srrs struct sctp_association *asoc; 198171158Srrs 199171158Srrs /* 200171158Srrs * This function returns the number of streams that have true unsent 201171158Srrs * data on them. Note that as it looks through it will clean up any 202171158Srrs * places that have old data that has been sent but left at top of 203171158Srrs * stream queue. 204171158Srrs */ 205171158Srrs asoc = &stcb->asoc; 206171158Srrs SCTP_TCB_SEND_LOCK(stcb); 207171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 208171158Srrs /* Check to see if some data queued */ 209171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 210171531Srrs is_there_another: 211171158Srrs /* sa_ignore FREED_MEMORY */ 212171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 213171158Srrs if (sp == NULL) { 214171158Srrs continue; 215171158Srrs } 216171158Srrs if ((sp->msg_is_complete) && 217171158Srrs (sp->length == 0) && 218171158Srrs (sp->sender_all_done)) { 219171158Srrs /* 220171158Srrs * We are doing differed cleanup. Last time 221171158Srrs * through when we took all the data the 222171158Srrs * sender_all_done was not set. 223171158Srrs */ 224171158Srrs if (sp->put_last_out == 0) { 225171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 226171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 227171158Srrs sp->sender_all_done, 228171158Srrs sp->length, 229171158Srrs sp->msg_is_complete, 230171158Srrs sp->put_last_out); 231171158Srrs } 232171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 233171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 234171158Srrs sctp_free_remote_addr(sp->net); 235171158Srrs if (sp->data) { 236171158Srrs sctp_m_freem(sp->data); 237171158Srrs sp->data = NULL; 238171158Srrs } 239171158Srrs sctp_free_a_strmoq(stcb, sp); 240171158Srrs goto is_there_another; 241171158Srrs } else { 242171158Srrs unsent_data++; 243171158Srrs continue; 244171158Srrs } 245171158Srrs } 246171158Srrs } 247171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 248171158Srrs return (unsent_data); 249171158Srrs} 250171158Srrs 251163953Srrsstatic int 252163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 253163953Srrs struct sctp_nets *net) 254163953Srrs{ 255163953Srrs struct sctp_init *init; 256163953Srrs struct sctp_association *asoc; 257163953Srrs struct sctp_nets *lnet; 258163953Srrs unsigned int i; 259163953Srrs 260163953Srrs init = &cp->init; 261163953Srrs asoc = &stcb->asoc; 262163953Srrs /* save off parameters */ 263163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 264163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 265163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 266163953Srrs /* update any ssthresh's that may have a default */ 267163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 268163953Srrs lnet->ssthresh = asoc->peers_rwnd; 269163953Srrs 270179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 271170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 272170744Srrs } 273163953Srrs } 274163953Srrs } 275164181Srrs SCTP_TCB_SEND_LOCK(stcb); 276163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 277163953Srrs unsigned int newcnt; 278163953Srrs struct sctp_stream_out *outs; 279163953Srrs struct sctp_stream_queue_pending *sp; 280163953Srrs 281163953Srrs /* cut back on number of streams */ 282163953Srrs newcnt = ntohs(init->num_inbound_streams); 283163953Srrs /* This if is probably not needed but I am cautious */ 284163953Srrs if (asoc->strmout) { 285163953Srrs /* First make sure no data chunks are trapped */ 286163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 287163953Srrs outs = &asoc->strmout[i]; 288163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 289163953Srrs while (sp) { 290163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 291163953Srrs next); 292163953Srrs asoc->stream_queue_cnt--; 293163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 294163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 295172090Srrs sp, SCTP_SO_NOT_LOCKED); 296163953Srrs if (sp->data) { 297163953Srrs sctp_m_freem(sp->data); 298163953Srrs sp->data = NULL; 299163953Srrs } 300163953Srrs sctp_free_remote_addr(sp->net); 301163953Srrs sp->net = NULL; 302163953Srrs /* Free the chunk */ 303169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 304163979Sru sp, stcb); 305163953Srrs 306163953Srrs sctp_free_a_strmoq(stcb, sp); 307169655Srrs /* sa_ignore FREED_MEMORY */ 308163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 309163953Srrs } 310163953Srrs } 311163953Srrs } 312163953Srrs /* cut back the count and abandon the upper streams */ 313163953Srrs asoc->pre_open_streams = newcnt; 314163953Srrs } 315164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 316163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 317163953Srrs /* init tsn's */ 318163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 319179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 320170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 321170744Srrs } 322163953Srrs /* This is the next one we expect */ 323163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 324163953Srrs 325163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 326163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 327163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 328163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 329163953Srrs /* open the requested streams */ 330170091Srrs 331163953Srrs if (asoc->strmin != NULL) { 332163953Srrs /* Free the old ones */ 333164181Srrs struct sctp_queued_to_read *ctl; 334164181Srrs 335164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 336164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 337164181Srrs while (ctl) { 338164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 339164181Srrs sctp_free_remote_addr(ctl->whoFrom); 340171158Srrs ctl->whoFrom = NULL; 341164181Srrs sctp_m_freem(ctl->data); 342164181Srrs ctl->data = NULL; 343164181Srrs sctp_free_a_readq(stcb, ctl); 344164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 345164181Srrs } 346164181Srrs } 347170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 348163953Srrs } 349164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 350164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 351164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 352164181Srrs } 353163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 354170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 355163953Srrs if (asoc->strmin == NULL) { 356163953Srrs /* we didn't get memory for the streams! */ 357169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 358163953Srrs return (-1); 359163953Srrs } 360163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 361163953Srrs asoc->strmin[i].stream_no = i; 362163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 363163953Srrs /* 364163953Srrs * U-stream ranges will be set when the cookie is unpacked. 365163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 366163953Srrs * supported) when the INIT-ACK arrives. 367163953Srrs */ 368163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 369168943Srrs asoc->strmin[i].delivery_started = 0; 370163953Srrs } 371163953Srrs /* 372163953Srrs * load_address_from_init will put the addresses into the 373163953Srrs * association when the COOKIE is processed or the INIT-ACK is 374163953Srrs * processed. Both types of COOKIE's existing and new call this 375163953Srrs * routine. It will remove addresses that are no longer in the 376163953Srrs * association (for the restarting case where addresses are 377163953Srrs * removed). Up front when the INIT arrives we will discard it if it 378163953Srrs * is a restart and new addresses have been added. 379163953Srrs */ 380169655Srrs /* sa_ignore MEMLEAK */ 381163953Srrs return (0); 382163953Srrs} 383163953Srrs 384163953Srrs/* 385163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 386163953Srrs */ 387163953Srrsstatic int 388163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 389163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 390170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 391163953Srrs{ 392163953Srrs struct sctp_association *asoc; 393163953Srrs struct mbuf *op_err; 394163953Srrs int retval, abort_flag; 395163953Srrs uint32_t initack_limit; 396163953Srrs 397163953Srrs /* First verify that we have no illegal param's */ 398163953Srrs abort_flag = 0; 399163953Srrs op_err = NULL; 400163953Srrs 401163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 402163953Srrs (offset + sizeof(struct sctp_init_chunk)), 403163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 404163953Srrs if (abort_flag) { 405163953Srrs /* Send an abort and notify peer */ 406172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 407170056Srrs *abort_no_unlock = 1; 408163953Srrs return (-1); 409163953Srrs } 410163953Srrs asoc = &stcb->asoc; 411163953Srrs /* process the peer's parameters in the INIT-ACK */ 412163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 413163953Srrs if (retval < 0) { 414163953Srrs return (retval); 415163953Srrs } 416163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 417163953Srrs /* load all addresses */ 418163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 419163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 420163953Srrs NULL))) { 421163953Srrs /* Huh, we should abort */ 422169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 423169420Srrs "Load addresses from INIT causes an abort %d\n", 424169420Srrs retval); 425163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 426179157Srrs NULL, 0, net->port); 427168299Srrs *abort_no_unlock = 1; 428163953Srrs return (-1); 429163953Srrs } 430171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 431171477Srrs if (asoc->peer_supports_asconf == 0) { 432171477Srrs struct sctp_asconf_addr *aparam; 433171477Srrs 434171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 435171477Srrs /* sa_ignore FREED_MEMORY */ 436171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 437171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 438171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 439171477Srrs } 440171477Srrs } 441163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 442163953Srrs stcb->asoc.local_hmacs); 443163953Srrs if (op_err) { 444163953Srrs sctp_queue_op_err(stcb, op_err); 445163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 446163953Srrs op_err = NULL; 447163953Srrs } 448163953Srrs /* extract the cookie and queue it to "echo" it back... */ 449179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 450171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 451171943Srrs stcb->asoc.overall_error_count, 452171943Srrs 0, 453171943Srrs SCTP_FROM_SCTP_INPUT, 454171943Srrs __LINE__); 455171943Srrs } 456163953Srrs stcb->asoc.overall_error_count = 0; 457163953Srrs net->error_count = 0; 458163953Srrs 459163953Srrs /* 460163953Srrs * Cancel the INIT timer, We do this first before queueing the 461163953Srrs * cookie. We always cancel at the primary to assue that we are 462163953Srrs * canceling the timer started by the INIT which always goes to the 463163953Srrs * primary. 464163953Srrs */ 465163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 466165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 467163953Srrs 468165220Srrs /* calculate the RTO */ 469171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 470165220Srrs 471163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 472163953Srrs if (retval < 0) { 473163953Srrs /* 474163953Srrs * No cookie, we probably should send a op error. But in any 475163953Srrs * case if there is no cookie in the INIT-ACK, we can 476163953Srrs * abandon the peer, its broke. 477163953Srrs */ 478163953Srrs if (retval == -3) { 479163953Srrs /* We abort with an error of missing mandatory param */ 480163953Srrs op_err = 481163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 482163953Srrs if (op_err) { 483163953Srrs /* 484163953Srrs * Expand beyond to include the mandatory 485163953Srrs * param cookie 486163953Srrs */ 487163953Srrs struct sctp_inv_mandatory_param *mp; 488163953Srrs 489165647Srrs SCTP_BUF_LEN(op_err) = 490163953Srrs sizeof(struct sctp_inv_mandatory_param); 491163953Srrs mp = mtod(op_err, 492163953Srrs struct sctp_inv_mandatory_param *); 493163953Srrs /* Subtract the reserved param */ 494163953Srrs mp->length = 495163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 496163953Srrs mp->num_param = htonl(1); 497163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 498163953Srrs mp->resv = 0; 499163953Srrs } 500163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 501179157Srrs sh, op_err, 0, net->port); 502168299Srrs *abort_no_unlock = 1; 503163953Srrs } 504163953Srrs return (retval); 505163953Srrs } 506163953Srrs return (0); 507163953Srrs} 508163953Srrs 509163953Srrsstatic void 510163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 511163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 512163953Srrs{ 513163953Srrs struct sockaddr_storage store; 514163953Srrs struct sockaddr_in *sin; 515163953Srrs struct sockaddr_in6 *sin6; 516163953Srrs struct sctp_nets *r_net; 517163953Srrs struct timeval tv; 518172396Srrs int req_prim = 0; 519163953Srrs 520163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 521163953Srrs /* Invalid length */ 522163953Srrs return; 523163953Srrs } 524163953Srrs sin = (struct sockaddr_in *)&store; 525163953Srrs sin6 = (struct sockaddr_in6 *)&store; 526163953Srrs 527163953Srrs memset(&store, 0, sizeof(store)); 528163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 529163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 530163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 531163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 532163953Srrs sin->sin_port = stcb->rport; 533163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 534163953Srrs sizeof(sin->sin_addr)); 535163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 536163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 537163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 538163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 539163953Srrs sin6->sin6_port = stcb->rport; 540163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 541163953Srrs sizeof(sin6->sin6_addr)); 542163953Srrs } else { 543163953Srrs return; 544163953Srrs } 545163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 546163953Srrs if (r_net == NULL) { 547169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 548163953Srrs return; 549163953Srrs } 550163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 551163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 552163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 553163953Srrs /* 554163953Srrs * If the its a HB and it's random value is correct when can 555163953Srrs * confirm the destination. 556163953Srrs */ 557163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 558167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 559167598Srrs stcb->asoc.primary_destination = r_net; 560167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 561167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 562167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 563167598Srrs if (r_net != stcb->asoc.primary_destination) { 564167598Srrs /* 565167598Srrs * first one on the list is NOT the primary 566167598Srrs * sctp_cmpaddr() is much more efficent if 567167598Srrs * the primary is the first on the list, 568167598Srrs * make it so. 569167598Srrs */ 570167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 571167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 572167598Srrs } 573172396Srrs req_prim = 1; 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 */ 599179783Srrs if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && 600179783Srrs SCTP_BASE_SYSCTL(sctp_cmt_pf) && 601179783Srrs (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) { 602171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 603171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 604171440Srrs stcb, net, 605171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 606171440Srrs } 607171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 608179783Srrs net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf); 609171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 610171440Srrs net, net->cwnd); 611171440Srrs } 612163953Srrs /* Now lets do a RTO with this */ 613171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 614172396Srrs /* Mobility adaptation */ 615172396Srrs if (req_prim) { 616172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 617172396Srrs SCTP_MOBILITY_BASE) || 618172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 619172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 620172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 621172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 622172396Srrs 623172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 624172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 625172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 626172396Srrs sctp_assoc_immediate_retrans(stcb, 627172396Srrs stcb->asoc.primary_destination); 628172396Srrs } 629172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 630172396Srrs SCTP_MOBILITY_BASE)) { 631172396Srrs sctp_move_chunks_from_deleted_prim(stcb, 632172396Srrs stcb->asoc.primary_destination); 633172396Srrs } 634172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 635172396Srrs stcb->asoc.deleted_primary); 636172396Srrs } 637172396Srrs } 638163953Srrs} 639163953Srrs 640163953Srrsstatic void 641163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 642163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 643163953Srrs{ 644172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 645172090Srrs struct socket *so; 646172090Srrs 647172090Srrs#endif 648172090Srrs 649169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 650163953Srrs if (stcb == NULL) 651163953Srrs return; 652163953Srrs 653163953Srrs /* stop any receive timers */ 654171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 655163953Srrs /* notify user of the abort and clean up... */ 656172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 657163953Srrs /* free the tcb */ 658172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 659172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 660172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 661172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 662172396Srrs panic("Received an ABORT"); 663172396Srrs } else { 664172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 665172396Srrs } 666172396Srrs#endif 667163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 668163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 669163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 670163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 671163953Srrs } 672168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 673168859Srrs sctp_print_out_track_log(stcb); 674168859Srrs#endif 675172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 676172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 677172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 678172090Srrs SCTP_TCB_UNLOCK(stcb); 679172090Srrs SCTP_SOCKET_LOCK(so, 1); 680172090Srrs SCTP_TCB_LOCK(stcb); 681172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 682172090Srrs#endif 683171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 684171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 685171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 686172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 687172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 688172090Srrs#endif 689169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 690163953Srrs} 691163953Srrs 692163953Srrsstatic void 693163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 694163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 695163953Srrs{ 696163953Srrs struct sctp_association *asoc; 697163953Srrs int some_on_streamwheel; 698163953Srrs 699172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 700172090Srrs struct socket *so; 701172090Srrs 702172090Srrs#endif 703172090Srrs 704169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 705169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 706163953Srrs if (stcb == NULL) 707163953Srrs return; 708165220Srrs asoc = &stcb->asoc; 709165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 710165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 711163953Srrs return; 712163953Srrs } 713163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 714163953Srrs /* Shutdown NOT the expected size */ 715163953Srrs return; 716163953Srrs } else { 717163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 718163953Srrs } 719165220Srrs if (asoc->control_pdapi) { 720163953Srrs /* 721163953Srrs * With a normal shutdown we assume the end of last record. 722163953Srrs */ 723163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 724165220Srrs asoc->control_pdapi->end_added = 1; 725165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 726165220Srrs asoc->control_pdapi = NULL; 727163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 728172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 729172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 730172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 731172090Srrs SCTP_TCB_UNLOCK(stcb); 732172090Srrs SCTP_SOCKET_LOCK(so, 1); 733172090Srrs SCTP_TCB_LOCK(stcb); 734172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 735172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 736172090Srrs /* assoc was freed while we were unlocked */ 737172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 738172090Srrs return; 739172090Srrs } 740172090Srrs#endif 741163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 742172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 743172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 744172090Srrs#endif 745163953Srrs } 746163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 747163953Srrs if (stcb->sctp_socket) { 748163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 749170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 750163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 751171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 752172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 753163953Srrs /* 754163953Srrs * notify upper layer that peer has initiated a 755163953Srrs * shutdown 756163953Srrs */ 757172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 758163953Srrs 759163953Srrs /* reset time */ 760169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 761163953Srrs } 762163953Srrs } 763163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 764163953Srrs /* 765163953Srrs * stop the shutdown timer, since we WILL move to 766163953Srrs * SHUTDOWN-ACK-SENT. 767163953Srrs */ 768171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 769163953Srrs } 770171158Srrs /* Now is there unsent data on a stream somewhere? */ 771171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 772163953Srrs 773163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 774163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 775163953Srrs some_on_streamwheel) { 776163953Srrs /* By returning we will push more data out */ 777163953Srrs return; 778163953Srrs } else { 779163953Srrs /* no outstanding data to send, so move on... */ 780163953Srrs /* send SHUTDOWN-ACK */ 781163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 782163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 783166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 784166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 785163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 786163953Srrs } 787171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 788172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 789171990Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, 790171990Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 791163953Srrs /* start SHUTDOWN timer */ 792163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 793163953Srrs stcb, net); 794163953Srrs } 795163953Srrs} 796163953Srrs 797163953Srrsstatic void 798163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 799163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 800163953Srrs{ 801163953Srrs struct sctp_association *asoc; 802163953Srrs 803172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 804172090Srrs struct socket *so; 805172090Srrs 806172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 807172090Srrs#endif 808169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 809169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 810163953Srrs if (stcb == NULL) 811163953Srrs return; 812163953Srrs 813163953Srrs asoc = &stcb->asoc; 814163953Srrs /* process according to association state */ 815163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 816163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 817163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 818163953Srrs SCTP_TCB_UNLOCK(stcb); 819163953Srrs return; 820163953Srrs } 821165220Srrs if (asoc->control_pdapi) { 822163953Srrs /* 823163953Srrs * With a normal shutdown we assume the end of last record. 824163953Srrs */ 825163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 826165220Srrs asoc->control_pdapi->end_added = 1; 827165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 828165220Srrs asoc->control_pdapi = NULL; 829163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 830172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 831172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 832172090Srrs SCTP_TCB_UNLOCK(stcb); 833172090Srrs SCTP_SOCKET_LOCK(so, 1); 834172090Srrs SCTP_TCB_LOCK(stcb); 835172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 836172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 837172090Srrs /* assoc was freed while we were unlocked */ 838172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 839172090Srrs return; 840172090Srrs } 841172090Srrs#endif 842163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 843172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 844172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 845172090Srrs#endif 846163953Srrs } 847163953Srrs /* are the queues empty? */ 848163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 849163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 850163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 851172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 852163953Srrs } 853163953Srrs /* stop the timer */ 854171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 855163953Srrs /* send SHUTDOWN-COMPLETE */ 856163953Srrs sctp_send_shutdown_complete(stcb, net); 857163953Srrs /* notify upper layer protocol */ 858163953Srrs if (stcb->sctp_socket) { 859172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 860163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 861163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 862163953Srrs /* Set the connected flag to disconnected */ 863163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 864163953Srrs } 865163953Srrs } 866163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 867163953Srrs /* free the TCB but first save off the ep */ 868172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 869172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 870172090Srrs SCTP_TCB_UNLOCK(stcb); 871172090Srrs SCTP_SOCKET_LOCK(so, 1); 872172090Srrs SCTP_TCB_LOCK(stcb); 873172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 874172090Srrs#endif 875171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 876171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 877172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 878172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 879172090Srrs#endif 880163953Srrs} 881163953Srrs 882163953Srrs/* 883163953Srrs * Skip past the param header and then we will find the chunk that caused the 884163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 885163953Srrs * our peer must be broken. 886163953Srrs */ 887163953Srrsstatic void 888163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 889163953Srrs struct sctp_nets *net) 890163953Srrs{ 891163953Srrs struct sctp_chunkhdr *chk; 892163953Srrs 893163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 894163953Srrs switch (chk->chunk_type) { 895163953Srrs case SCTP_ASCONF_ACK: 896163953Srrs case SCTP_ASCONF: 897163953Srrs sctp_asconf_cleanup(stcb, net); 898163953Srrs break; 899163953Srrs case SCTP_FORWARD_CUM_TSN: 900163953Srrs stcb->asoc.peer_supports_prsctp = 0; 901163953Srrs break; 902163953Srrs default: 903169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 904169420Srrs "Peer does not support chunk type %d(%x)??\n", 905169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 906163953Srrs break; 907163953Srrs } 908163953Srrs} 909163953Srrs 910163953Srrs/* 911163953Srrs * Skip past the param header and then we will find the param that caused the 912163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 913163953Srrs * these will turn of specific features. 914163953Srrs */ 915163953Srrsstatic void 916163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 917163953Srrs{ 918163953Srrs struct sctp_paramhdr *pbad; 919163953Srrs 920163953Srrs pbad = phdr + 1; 921163953Srrs switch (ntohs(pbad->param_type)) { 922163953Srrs /* pr-sctp draft */ 923163953Srrs case SCTP_PRSCTP_SUPPORTED: 924163953Srrs stcb->asoc.peer_supports_prsctp = 0; 925163953Srrs break; 926163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 927163953Srrs break; 928163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 929163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 930163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 931163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 932163953Srrs stcb->asoc.ecn_allowed = 0; 933163953Srrs break; 934163953Srrs case SCTP_ADD_IP_ADDRESS: 935163953Srrs case SCTP_DEL_IP_ADDRESS: 936163953Srrs case SCTP_SET_PRIM_ADDR: 937163953Srrs stcb->asoc.peer_supports_asconf = 0; 938163953Srrs break; 939163953Srrs case SCTP_SUCCESS_REPORT: 940163953Srrs case SCTP_ERROR_CAUSE_IND: 941169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 942169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 943169420Srrs "Turning off ASCONF to this strange peer\n"); 944163953Srrs stcb->asoc.peer_supports_asconf = 0; 945163953Srrs break; 946163953Srrs default: 947169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 948169420Srrs "Peer does not support param type %d(%x)??\n", 949169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 950163953Srrs break; 951163953Srrs } 952163953Srrs} 953163953Srrs 954163953Srrsstatic int 955163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 956163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 957163953Srrs{ 958163953Srrs int chklen; 959163953Srrs struct sctp_paramhdr *phdr; 960163953Srrs uint16_t error_type; 961163953Srrs uint16_t error_len; 962163953Srrs struct sctp_association *asoc; 963163953Srrs int adjust; 964163953Srrs 965172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 966172090Srrs struct socket *so; 967172090Srrs 968172090Srrs#endif 969172090Srrs 970163953Srrs /* parse through all of the errors and process */ 971163953Srrs asoc = &stcb->asoc; 972163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 973163953Srrs sizeof(struct sctp_chunkhdr)); 974163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 975163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 976163953Srrs /* Process an Error Cause */ 977163953Srrs error_type = ntohs(phdr->param_type); 978163953Srrs error_len = ntohs(phdr->param_length); 979163953Srrs if ((error_len > chklen) || (error_len == 0)) { 980163953Srrs /* invalid param length for this param */ 981169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 982169420Srrs chklen, error_len); 983163953Srrs return (0); 984163953Srrs } 985163953Srrs switch (error_type) { 986163953Srrs case SCTP_CAUSE_INVALID_STREAM: 987163953Srrs case SCTP_CAUSE_MISSING_PARAM: 988163953Srrs case SCTP_CAUSE_INVALID_PARAM: 989163953Srrs case SCTP_CAUSE_NO_USER_DATA: 990169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 991169420Srrs error_type); 992163953Srrs break; 993163953Srrs case SCTP_CAUSE_STALE_COOKIE: 994163953Srrs /* 995163953Srrs * We only act if we have echoed a cookie and are 996163953Srrs * waiting. 997163953Srrs */ 998163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 999163953Srrs int *p; 1000163953Srrs 1001163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1002163953Srrs /* Save the time doubled */ 1003163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1004163953Srrs asoc->stale_cookie_count++; 1005163953Srrs if (asoc->stale_cookie_count > 1006163953Srrs asoc->max_init_times) { 1007172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1008163953Srrs /* now free the asoc */ 1009172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1010172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1011172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1012172090Srrs SCTP_TCB_UNLOCK(stcb); 1013172090Srrs SCTP_SOCKET_LOCK(so, 1); 1014172090Srrs SCTP_TCB_LOCK(stcb); 1015172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1016172090Srrs#endif 1017171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1018171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1019172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1020172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1021172090Srrs#endif 1022163953Srrs return (-1); 1023163953Srrs } 1024163953Srrs /* blast back to INIT state */ 1025163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1026163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1027163953Srrs 1028163953Srrs sctp_stop_all_cookie_timers(stcb); 1029172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1030163953Srrs } 1031163953Srrs break; 1032163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1033163953Srrs /* 1034163953Srrs * Nothing we can do here, we don't do hostname 1035163953Srrs * addresses so if the peer does not like my IPv6 1036163953Srrs * (or IPv4 for that matter) it does not matter. If 1037163953Srrs * they don't support that type of address, they can 1038163953Srrs * NOT possibly get that packet type... i.e. with no 1039163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1040163953Srrs * safely ignore this one. If we ever added support 1041163953Srrs * for HOSTNAME Addresses, then we would need to do 1042163953Srrs * something here. 1043163953Srrs */ 1044163953Srrs break; 1045163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1046163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1047163953Srrs break; 1048163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1049163953Srrs sctp_process_unrecog_param(stcb, phdr); 1050163953Srrs break; 1051163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1052163953Srrs /* 1053163953Srrs * We ignore this since the timer will drive out a 1054163953Srrs * new cookie anyway and there timer will drive us 1055163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1056163953Srrs * here since we don't have their tag. 1057163953Srrs */ 1058163953Srrs break; 1059163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1060163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1061163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1062163953Srrs /* 1063163953Srrs * We should NOT get these here, but in a 1064166086Srrs * ASCONF-ACK. 1065163953Srrs */ 1066169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1067169420Srrs error_type); 1068163953Srrs break; 1069163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1070163953Srrs /* 1071163953Srrs * And what, pray tell do we do with the fact that 1072163953Srrs * the peer is out of resources? Not really sure we 1073166086Srrs * could do anything but abort. I suspect this 1074163953Srrs * should have came WITH an abort instead of in a 1075163953Srrs * OP-ERROR. 1076163953Srrs */ 1077163953Srrs break; 1078163953Srrs default: 1079169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1080169420Srrs error_type); 1081163953Srrs break; 1082163953Srrs } 1083163953Srrs adjust = SCTP_SIZE32(error_len); 1084163953Srrs chklen -= adjust; 1085163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1086163953Srrs } 1087163953Srrs return (0); 1088163953Srrs} 1089163953Srrs 1090163953Srrsstatic int 1091169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1092169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1093170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1094163953Srrs{ 1095163953Srrs struct sctp_init_ack *init_ack; 1096163953Srrs struct mbuf *op_err; 1097163953Srrs 1098169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1099169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1100169420Srrs 1101163953Srrs if (stcb == NULL) { 1102169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1103169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1104163953Srrs return (-1); 1105163953Srrs } 1106163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1107163953Srrs /* Invalid length */ 1108163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1109163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1110179157Srrs op_err, 0, net->port); 1111168299Srrs *abort_no_unlock = 1; 1112163953Srrs return (-1); 1113163953Srrs } 1114163953Srrs init_ack = &cp->init; 1115163953Srrs /* validate parameters */ 1116163953Srrs if (init_ack->initiate_tag == 0) { 1117163953Srrs /* protocol error... send an abort */ 1118163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1119163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1120179157Srrs op_err, 0, net->port); 1121168299Srrs *abort_no_unlock = 1; 1122163953Srrs return (-1); 1123163953Srrs } 1124163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1125163953Srrs /* protocol error... send an abort */ 1126163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1127163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1128179157Srrs op_err, 0, net->port); 1129168299Srrs *abort_no_unlock = 1; 1130163953Srrs return (-1); 1131163953Srrs } 1132163953Srrs if (init_ack->num_inbound_streams == 0) { 1133163953Srrs /* protocol error... send an abort */ 1134163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1135163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1136179157Srrs op_err, 0, net->port); 1137168299Srrs *abort_no_unlock = 1; 1138163953Srrs return (-1); 1139163953Srrs } 1140163953Srrs if (init_ack->num_outbound_streams == 0) { 1141163953Srrs /* protocol error... send an abort */ 1142163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1143163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1144179157Srrs op_err, 0, net->port); 1145168299Srrs *abort_no_unlock = 1; 1146163953Srrs return (-1); 1147163953Srrs } 1148163953Srrs /* process according to association state... */ 1149171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1150163953Srrs case SCTP_STATE_COOKIE_WAIT: 1151163953Srrs /* this is the expected state for this chunk */ 1152163953Srrs /* process the INIT-ACK parameters */ 1153163953Srrs if (stcb->asoc.primary_destination->dest_state & 1154163953Srrs SCTP_ADDR_UNCONFIRMED) { 1155163953Srrs /* 1156163953Srrs * The primary is where we sent the INIT, we can 1157163953Srrs * always consider it confirmed when the INIT-ACK is 1158163953Srrs * returned. Do this before we load addresses 1159163953Srrs * though. 1160163953Srrs */ 1161163953Srrs stcb->asoc.primary_destination->dest_state &= 1162163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1163163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1164172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1165163953Srrs } 1166169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1167170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1168163953Srrs /* error in parsing parameters */ 1169163953Srrs return (-1); 1170163953Srrs } 1171163953Srrs /* update our state */ 1172169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1173171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1174163953Srrs 1175163953Srrs /* reset the RTO calc */ 1176179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1177171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1178171943Srrs stcb->asoc.overall_error_count, 1179171943Srrs 0, 1180171943Srrs SCTP_FROM_SCTP_INPUT, 1181171943Srrs __LINE__); 1182171943Srrs } 1183163953Srrs stcb->asoc.overall_error_count = 0; 1184169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1185163953Srrs /* 1186163953Srrs * collapse the init timer back in case of a exponential 1187165220Srrs * backoff 1188163953Srrs */ 1189163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1190163953Srrs stcb, net); 1191163953Srrs /* 1192163953Srrs * the send at the end of the inbound data processing will 1193163953Srrs * cause the cookie to be sent 1194163953Srrs */ 1195163953Srrs break; 1196163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1197163953Srrs /* incorrect state... discard */ 1198163953Srrs break; 1199163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1200163953Srrs /* incorrect state... discard */ 1201163953Srrs break; 1202163953Srrs case SCTP_STATE_OPEN: 1203163953Srrs /* incorrect state... discard */ 1204163953Srrs break; 1205163953Srrs case SCTP_STATE_EMPTY: 1206163953Srrs case SCTP_STATE_INUSE: 1207163953Srrs default: 1208163953Srrs /* incorrect state... discard */ 1209163953Srrs return (-1); 1210163953Srrs break; 1211163953Srrs } 1212169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1213163953Srrs return (0); 1214163953Srrs} 1215163953Srrs 1216163953Srrs 1217163953Srrs/* 1218163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1219163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1220163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1221163953Srrs * mbuf to the cookie-echo chunk 1222163953Srrs */ 1223163953Srrsstatic struct sctp_tcb * 1224163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1225163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1226163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1227169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1228170181Srrs uint32_t vrf_id) 1229163953Srrs{ 1230163953Srrs struct sctp_association *asoc; 1231163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1232163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1233163953Srrs int chk_length; 1234165220Srrs int init_offset, initack_offset, i; 1235163953Srrs int retval; 1236164205Srrs int spec_flag = 0; 1237170140Srrs uint32_t how_indx; 1238163953Srrs 1239163953Srrs /* I know that the TCB is non-NULL from the caller */ 1240163953Srrs asoc = &stcb->asoc; 1241166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1242166023Srrs if (asoc->cookie_how[how_indx] == 0) 1243166023Srrs break; 1244166023Srrs } 1245166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1246166023Srrs asoc->cookie_how[how_indx] = 1; 1247166023Srrs } 1248163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1249163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1250163953Srrs struct mbuf *op_err; 1251163953Srrs struct sctp_paramhdr *ph; 1252163953Srrs 1253163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1254163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1255165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1256163953Srrs if (op_err == NULL) { 1257163953Srrs /* FOOBAR */ 1258163953Srrs return (NULL); 1259163953Srrs } 1260163953Srrs /* pre-reserve some space */ 1261178251Srrs#ifdef INET6 1262165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1263178251Srrs#else 1264178251Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1265178251Srrs#endif 1266165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1267165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1268163953Srrs /* Set the len */ 1269165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1270163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1271163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1272163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1273169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1274179157Srrs vrf_id, net->port); 1275166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1276166023Srrs asoc->cookie_how[how_indx] = 2; 1277163953Srrs return (NULL); 1278163953Srrs } 1279163953Srrs /* 1280163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1281163953Srrs * INIT should start after the cookie-echo header struct (chunk 1282163953Srrs * header, state cookie header struct) 1283163953Srrs */ 1284163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1285163953Srrs 1286163953Srrs init_cp = (struct sctp_init_chunk *) 1287163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1288163953Srrs (uint8_t *) & init_buf); 1289163953Srrs if (init_cp == NULL) { 1290163953Srrs /* could not pull a INIT chunk in cookie */ 1291163953Srrs return (NULL); 1292163953Srrs } 1293163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1294163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1295163953Srrs return (NULL); 1296163953Srrs } 1297163953Srrs /* 1298163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1299163953Srrs * INIT-ACK follows the INIT chunk 1300163953Srrs */ 1301163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1302163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1303163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1304163953Srrs (uint8_t *) & initack_buf); 1305163953Srrs if (initack_cp == NULL) { 1306163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1307163953Srrs return (NULL); 1308163953Srrs } 1309163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1310163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1311163953Srrs return (NULL); 1312163953Srrs } 1313163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1314163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1315163953Srrs /* 1316163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1317163953Srrs * to get into the OPEN state 1318163953Srrs */ 1319166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1320172091Srrs /*- 1321172091Srrs * Opps, this means that we somehow generated two vtag's 1322172091Srrs * the same. I.e. we did: 1323172091Srrs * Us Peer 1324172091Srrs * <---INIT(tag=a)------ 1325172091Srrs * ----INIT-ACK(tag=t)--> 1326172091Srrs * ----INIT(tag=t)------> *1 1327172091Srrs * <---INIT-ACK(tag=a)--- 1328172091Srrs * <----CE(tag=t)------------- *2 1329172091Srrs * 1330172091Srrs * At point *1 we should be generating a different 1331172091Srrs * tag t'. Which means we would throw away the CE and send 1332172091Srrs * ours instead. Basically this is case C (throw away side). 1333172091Srrs */ 1334172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1335172091Srrs asoc->cookie_how[how_indx] = 17; 1336172091Srrs return (NULL); 1337172091Srrs 1338166023Srrs } 1339163953Srrs switch SCTP_GET_STATE 1340163953Srrs (asoc) { 1341163953Srrs case SCTP_STATE_COOKIE_WAIT: 1342166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1343163953Srrs /* 1344169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1345166023Srrs * correct tags... just accept it...but we must 1346166023Srrs * process the init so that we can make sure we have 1347166023Srrs * the right seq no's. 1348163953Srrs */ 1349163953Srrs /* First we must process the INIT !! */ 1350163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1351163953Srrs if (retval < 0) { 1352166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1353166023Srrs asoc->cookie_how[how_indx] = 3; 1354163953Srrs return (NULL); 1355163953Srrs } 1356163953Srrs /* we have already processed the INIT so no problem */ 1357163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1358171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1359171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1360163953Srrs /* update current state */ 1361166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1362166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1363166675Srrs else 1364166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1365171943Srrs 1366171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1367163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1368163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1369163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1370163953Srrs } 1371166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1372165220Srrs sctp_stop_all_cookie_timers(stcb); 1373163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1374163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1375163953Srrs (inp->sctp_socket->so_qlimit == 0) 1376163953Srrs ) { 1377172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1378172090Srrs struct socket *so; 1379172090Srrs 1380172090Srrs#endif 1381163953Srrs /* 1382163953Srrs * Here is where collision would go if we 1383163953Srrs * did a connect() and instead got a 1384163953Srrs * init/init-ack/cookie done before the 1385163953Srrs * init-ack came back.. 1386163953Srrs */ 1387163953Srrs stcb->sctp_ep->sctp_flags |= 1388163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1389172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1390172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1391172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1392172090Srrs SCTP_TCB_UNLOCK(stcb); 1393172090Srrs SCTP_SOCKET_LOCK(so, 1); 1394172090Srrs SCTP_TCB_LOCK(stcb); 1395172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1396172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1397172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1398172090Srrs return (NULL); 1399172090Srrs } 1400172090Srrs#endif 1401172090Srrs soisconnected(stcb->sctp_socket); 1402172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1403172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1404172090Srrs#endif 1405163953Srrs } 1406163953Srrs /* notify upper layer */ 1407163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1408163953Srrs /* 1409163953Srrs * since we did not send a HB make sure we don't 1410163953Srrs * double things 1411163953Srrs */ 1412163953Srrs net->hb_responded = 1; 1413170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1414171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1415163953Srrs 1416163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1417163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1418163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1419163953Srrs inp, stcb, NULL); 1420163953Srrs } 1421163953Srrs break; 1422163953Srrs default: 1423163953Srrs /* 1424163953Srrs * we're in the OPEN state (or beyond), so peer must 1425163953Srrs * have simply lost the COOKIE-ACK 1426163953Srrs */ 1427163953Srrs break; 1428163953Srrs } /* end switch */ 1429165220Srrs sctp_stop_all_cookie_timers(stcb); 1430163953Srrs /* 1431163953Srrs * We ignore the return code here.. not sure if we should 1432163953Srrs * somehow abort.. but we do have an existing asoc. This 1433163953Srrs * really should not fail. 1434163953Srrs */ 1435163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1436163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1437163953Srrs initack_offset, sh, init_src)) { 1438166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1439166023Srrs asoc->cookie_how[how_indx] = 4; 1440163953Srrs return (NULL); 1441163953Srrs } 1442163953Srrs /* respond with a COOKIE-ACK */ 1443165220Srrs sctp_toss_old_cookies(stcb, asoc); 1444163953Srrs sctp_send_cookie_ack(stcb); 1445166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1446166023Srrs asoc->cookie_how[how_indx] = 5; 1447163953Srrs return (stcb); 1448169352Srrs } 1449163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1450163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1451163953Srrs cookie->tie_tag_my_vtag == 0 && 1452163953Srrs cookie->tie_tag_peer_vtag == 0) { 1453163953Srrs /* 1454163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1455163953Srrs */ 1456166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1457166023Srrs asoc->cookie_how[how_indx] = 6; 1458163953Srrs return (NULL); 1459163953Srrs } 1460163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1461163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1462163953Srrs init_cp->init.initiate_tag == 0)) { 1463163953Srrs /* 1464163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1465163953Srrs * should be ok, re-accept peer info 1466163953Srrs */ 1467166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1468166023Srrs /* 1469166023Srrs * Extension of case C. If we hit this, then the 1470166023Srrs * random number generator returned the same vtag 1471166023Srrs * when we first sent our INIT-ACK and when we later 1472166023Srrs * sent our INIT. The side with the seq numbers that 1473166023Srrs * are different will be the one that normnally 1474166023Srrs * would have hit case C. This in effect "extends" 1475166023Srrs * our vtags in this collision case to be 64 bits. 1476166023Srrs * The same collision could occur aka you get both 1477166023Srrs * vtag and seq number the same twice in a row.. but 1478166023Srrs * is much less likely. If it did happen then we 1479166023Srrs * would proceed through and bring up the assoc.. we 1480166023Srrs * may end up with the wrong stream setup however.. 1481166023Srrs * which would be bad.. but there is no way to 1482166023Srrs * tell.. until we send on a stream that does not 1483166023Srrs * exist :-) 1484166023Srrs */ 1485166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1486166023Srrs asoc->cookie_how[how_indx] = 7; 1487166023Srrs 1488166023Srrs return (NULL); 1489166023Srrs } 1490166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1491166023Srrs asoc->cookie_how[how_indx] = 8; 1492171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1493163953Srrs sctp_stop_all_cookie_timers(stcb); 1494163953Srrs /* 1495163953Srrs * since we did not send a HB make sure we don't double 1496163953Srrs * things 1497163953Srrs */ 1498163953Srrs net->hb_responded = 1; 1499163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1500163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1501163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1502163953Srrs NULL); 1503163953Srrs } 1504163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1505164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1506164205Srrs 1507164205Srrs /* Note last_cwr_tsn? where is this used? */ 1508163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1509164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1510164205Srrs /* 1511164205Srrs * Ok the peer probably discarded our data (if we 1512164205Srrs * echoed a cookie+data). So anything on the 1513164205Srrs * sent_queue should be marked for retransmit, we 1514164205Srrs * may not get something to kick us so it COULD 1515164205Srrs * still take a timeout to move these.. but it can't 1516164205Srrs * hurt to mark them. 1517164205Srrs */ 1518164205Srrs struct sctp_tmit_chunk *chk; 1519163953Srrs 1520164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1521164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1522164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1523171440Srrs sctp_flight_size_decrease(chk); 1524171440Srrs sctp_total_flight_decrease(stcb, chk); 1525168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1526164205Srrs spec_flag++; 1527164205Srrs } 1528164205Srrs } 1529164205Srrs 1530164205Srrs } 1531163953Srrs /* process the INIT info (peer's info) */ 1532163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1533163953Srrs if (retval < 0) { 1534166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1535166023Srrs asoc->cookie_how[how_indx] = 9; 1536163953Srrs return (NULL); 1537163953Srrs } 1538163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1539163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1540163953Srrs initack_offset, sh, init_src)) { 1541166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1542166023Srrs asoc->cookie_how[how_indx] = 10; 1543163953Srrs return (NULL); 1544163953Srrs } 1545163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1546163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1547163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1548163953Srrs 1549163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1550163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1551163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1552172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1553172090Srrs struct socket *so; 1554172090Srrs 1555172090Srrs#endif 1556163953Srrs stcb->sctp_ep->sctp_flags |= 1557163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1558172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1559172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1560172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1561172090Srrs SCTP_TCB_UNLOCK(stcb); 1562172090Srrs SCTP_SOCKET_LOCK(so, 1); 1563172090Srrs SCTP_TCB_LOCK(stcb); 1564172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1565172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1566172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1567172090Srrs return (NULL); 1568172090Srrs } 1569172090Srrs#endif 1570172090Srrs soisconnected(stcb->sctp_socket); 1571172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1572172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1573172090Srrs#endif 1574163953Srrs } 1575166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1576166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1577166675Srrs else 1578166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1579166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1580166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1581166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1582166675Srrs } else { 1583166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1584163953Srrs } 1585171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1586163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1587163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1588163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1589163953Srrs } 1590163953Srrs sctp_stop_all_cookie_timers(stcb); 1591165220Srrs sctp_toss_old_cookies(stcb, asoc); 1592163953Srrs sctp_send_cookie_ack(stcb); 1593164205Srrs if (spec_flag) { 1594164205Srrs /* 1595164205Srrs * only if we have retrans set do we do this. What 1596164205Srrs * this call does is get only the COOKIE-ACK out and 1597164205Srrs * then when we return the normal call to 1598164205Srrs * sctp_chunk_output will get the retrans out behind 1599164205Srrs * this. 1600164205Srrs */ 1601172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1602164205Srrs } 1603166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1604166023Srrs asoc->cookie_how[how_indx] = 11; 1605166023Srrs 1606163953Srrs return (stcb); 1607163953Srrs } 1608163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1609163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1610163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1611163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1612163953Srrs cookie->tie_tag_peer_vtag != 0) { 1613163953Srrs struct sctpasochead *head; 1614163953Srrs 1615163953Srrs /* 1616163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1617163953Srrs */ 1618165220Srrs /* temp code */ 1619166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1620166023Srrs asoc->cookie_how[how_indx] = 12; 1621171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1622171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1623165647Srrs 1624163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1625163953Srrs /* notify upper layer */ 1626163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1627165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1628166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1629166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1630166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1631166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1632166675Srrs } 1633166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1634166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1635166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1636166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1637166675Srrs } 1638165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1639171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1640165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1641165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1642165647Srrs 1643165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1644165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1645171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1646165647Srrs } 1647165647Srrs asoc->pre_open_streams = 1648165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1649165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1650165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1651179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1652165647Srrs 1653165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1654165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1655165647Srrs 1656165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1657165647Srrs 1658165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1659170138Srrs if (asoc->mapping_array) { 1660165647Srrs memset(asoc->mapping_array, 0, 1661165647Srrs asoc->mapping_array_size); 1662170138Srrs } 1663165220Srrs SCTP_TCB_UNLOCK(stcb); 1664165220Srrs SCTP_INP_INFO_WLOCK(); 1665165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1666165220Srrs SCTP_TCB_LOCK(stcb); 1667165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1668163953Srrs /* send up all the data */ 1669164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1670165220Srrs 1671172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1672165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1673165220Srrs stcb->asoc.strmout[i].stream_no = i; 1674165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1675165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1676165220Srrs } 1677163953Srrs /* process the INIT-ACK info (my info) */ 1678163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1679163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1680163953Srrs 1681163953Srrs /* pull from vtag hash */ 1682163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1683163953Srrs /* re-insert to new vtag position */ 1684179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1685179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1686163953Srrs /* 1687163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1688163953Srrs * system 1689163953Srrs */ 1690163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1691163953Srrs 1692163953Srrs /* Is this the first restart? */ 1693163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1694163953Srrs /* Ok add it to assoc_id vtag hash */ 1695179783Srrs head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1696179783Srrs SCTP_BASE_INFO(hashrestartmark))]; 1697163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1698163953Srrs stcb->asoc.in_restart_hash = 1; 1699163953Srrs } 1700163953Srrs /* process the INIT info (peer's info) */ 1701164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1702165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1703165220Srrs SCTP_INP_INFO_WUNLOCK(); 1704165220Srrs 1705163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1706163953Srrs if (retval < 0) { 1707166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1708166023Srrs asoc->cookie_how[how_indx] = 13; 1709166023Srrs 1710163953Srrs return (NULL); 1711163953Srrs } 1712163953Srrs /* 1713163953Srrs * since we did not send a HB make sure we don't double 1714163953Srrs * things 1715163953Srrs */ 1716163953Srrs net->hb_responded = 1; 1717163953Srrs 1718163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1719163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1720163953Srrs initack_offset, sh, init_src)) { 1721166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1722166023Srrs asoc->cookie_how[how_indx] = 14; 1723166023Srrs 1724163953Srrs return (NULL); 1725163953Srrs } 1726163953Srrs /* respond with a COOKIE-ACK */ 1727163953Srrs sctp_stop_all_cookie_timers(stcb); 1728165220Srrs sctp_toss_old_cookies(stcb, asoc); 1729163953Srrs sctp_send_cookie_ack(stcb); 1730166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1731166023Srrs asoc->cookie_how[how_indx] = 15; 1732163953Srrs 1733163953Srrs return (stcb); 1734163953Srrs } 1735166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1736166023Srrs asoc->cookie_how[how_indx] = 16; 1737163953Srrs /* all other cases... */ 1738163953Srrs return (NULL); 1739163953Srrs} 1740163953Srrs 1741166086Srrs 1742163953Srrs/* 1743163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1744163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1745163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1746163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1747163953Srrs * was from returns a new TCB 1748163953Srrs */ 1749163953Srrsstatic struct sctp_tcb * 1750163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1751163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1752163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1753163953Srrs struct sockaddr *init_src, int *notification, 1754169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1755179157Srrs uint32_t vrf_id, uint16_t port) 1756163953Srrs{ 1757163953Srrs struct sctp_tcb *stcb; 1758163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1759163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1760163953Srrs struct sockaddr_storage sa_store; 1761163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1762163953Srrs struct sockaddr_in *sin; 1763163953Srrs struct sockaddr_in6 *sin6; 1764163953Srrs struct sctp_association *asoc; 1765163953Srrs int chk_length; 1766163953Srrs int init_offset, initack_offset, initack_limit; 1767163953Srrs int retval; 1768163953Srrs int error = 0; 1769163953Srrs uint32_t old_tag; 1770166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1771163953Srrs 1772172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1773172090Srrs struct socket *so; 1774172090Srrs 1775172090Srrs so = SCTP_INP_SO(inp); 1776172090Srrs#endif 1777172090Srrs 1778163953Srrs /* 1779163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1780163953Srrs * INIT should start after the cookie-echo header struct (chunk 1781163953Srrs * header, state cookie header struct) 1782163953Srrs */ 1783163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1784163953Srrs init_cp = (struct sctp_init_chunk *) 1785163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1786163953Srrs (uint8_t *) & init_buf); 1787163953Srrs if (init_cp == NULL) { 1788163953Srrs /* could not pull a INIT chunk in cookie */ 1789169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1790169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1791163953Srrs return (NULL); 1792163953Srrs } 1793163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1794163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1795169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1796163953Srrs return (NULL); 1797163953Srrs } 1798163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1799163953Srrs /* 1800163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1801163953Srrs * INIT-ACK follows the INIT chunk 1802163953Srrs */ 1803163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1804163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1805163953Srrs (uint8_t *) & initack_buf); 1806163953Srrs if (initack_cp == NULL) { 1807163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1808169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1809163953Srrs return (NULL); 1810163953Srrs } 1811163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1812163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1813163953Srrs return (NULL); 1814163953Srrs } 1815163953Srrs /* 1816163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1817163953Srrs * "initack_limit" value. This is because the chk_length field 1818163953Srrs * includes the length of the cookie, but the cookie is omitted when 1819163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1820163953Srrs */ 1821163953Srrs initack_limit = offset + cookie_len; 1822163953Srrs 1823163953Srrs /* 1824163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1825163953Srrs * and popluate 1826163953Srrs */ 1827171531Srrs 1828171531Srrs /* 1829171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 1830171531Srrs * We do this since in effect we only use the p argument when the 1831171531Srrs * socket is unbound and we must do an implicit bind. Since we are 1832171531Srrs * getting a cookie, we cannot be unbound. 1833171531Srrs */ 1834163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1835171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 1836171531Srrs (struct thread *)NULL 1837171531Srrs ); 1838163953Srrs if (stcb == NULL) { 1839163953Srrs struct mbuf *op_err; 1840163953Srrs 1841163953Srrs /* memory problem? */ 1842169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1843169420Srrs "process_cookie_new: no room for another TCB!\n"); 1844163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1845170140Srrs 1846163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1847179157Srrs sh, op_err, vrf_id, port); 1848163953Srrs return (NULL); 1849163953Srrs } 1850163953Srrs /* get the correct sctp_nets */ 1851169420Srrs if (netp) 1852169420Srrs *netp = sctp_findnet(stcb, init_src); 1853169420Srrs 1854163953Srrs asoc = &stcb->asoc; 1855163953Srrs /* get scope variables out of cookie */ 1856163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1857163953Srrs asoc->site_scope = cookie->site_scope; 1858163953Srrs asoc->local_scope = cookie->local_scope; 1859163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1860163953Srrs 1861163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1862163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1863163953Srrs struct mbuf *op_err; 1864163953Srrs 1865163953Srrs /* 1866163953Srrs * Houston we have a problem. The EP changed while the 1867163953Srrs * cookie was in flight. Only recourse is to abort the 1868163953Srrs * association. 1869163953Srrs */ 1870170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1871163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1872163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1873179157Srrs sh, op_err, vrf_id, port); 1874172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1875172090Srrs SCTP_TCB_UNLOCK(stcb); 1876172090Srrs SCTP_SOCKET_LOCK(so, 1); 1877172090Srrs SCTP_TCB_LOCK(stcb); 1878172090Srrs#endif 1879171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 1880171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1881172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1882172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1883172090Srrs#endif 1884172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1885163953Srrs return (NULL); 1886163953Srrs } 1887163953Srrs /* process the INIT-ACK info (my info) */ 1888163953Srrs old_tag = asoc->my_vtag; 1889163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1890163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1891163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1892163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1893163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1894179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1895163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1896163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1897163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1898163953Srrs 1899163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1900163953Srrs 1901163953Srrs /* process the INIT info (peer's info) */ 1902169420Srrs if (netp) 1903169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1904169420Srrs else 1905169420Srrs retval = 0; 1906163953Srrs if (retval < 0) { 1907170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1908172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1909172090Srrs SCTP_TCB_UNLOCK(stcb); 1910172090Srrs SCTP_SOCKET_LOCK(so, 1); 1911172090Srrs SCTP_TCB_LOCK(stcb); 1912172090Srrs#endif 1913171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1914172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1915172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1916172090Srrs#endif 1917172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1918163953Srrs return (NULL); 1919163953Srrs } 1920163953Srrs /* load all addresses */ 1921163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1922163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1923163953Srrs init_src)) { 1924170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1925172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1926172090Srrs SCTP_TCB_UNLOCK(stcb); 1927172090Srrs SCTP_SOCKET_LOCK(so, 1); 1928172090Srrs SCTP_TCB_LOCK(stcb); 1929172090Srrs#endif 1930171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1931172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1932172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1933172090Srrs#endif 1934172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1935163953Srrs return (NULL); 1936163953Srrs } 1937163953Srrs /* 1938163953Srrs * verify any preceding AUTH chunk that was skipped 1939163953Srrs */ 1940163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1941163953Srrs sctp_auth_get_cookie_params(stcb, m, 1942163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1943163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1944163953Srrs if (auth_skipped) { 1945163953Srrs struct sctp_auth_chunk *auth; 1946163953Srrs 1947163953Srrs auth = (struct sctp_auth_chunk *) 1948166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1949169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1950163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1951169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1952169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1953171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1954172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1955172090Srrs SCTP_TCB_UNLOCK(stcb); 1956172090Srrs SCTP_SOCKET_LOCK(so, 1); 1957172090Srrs SCTP_TCB_LOCK(stcb); 1958172090Srrs#endif 1959171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1960172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1961172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1962172090Srrs#endif 1963172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1964163953Srrs return (NULL); 1965163953Srrs } else { 1966163953Srrs /* remaining chunks checked... good to go */ 1967163953Srrs stcb->asoc.authenticated = 1; 1968163953Srrs } 1969163953Srrs } 1970163953Srrs /* update current state */ 1971169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1972171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1973163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1974163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1975163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1976163953Srrs } 1977165220Srrs sctp_stop_all_cookie_timers(stcb); 1978163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1979163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1980163953Srrs 1981163953Srrs /* 1982163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1983163953Srrs * addresses that need to get added to the peer (eg. addresses 1984163953Srrs * changed while cookie echo in flight). This needs to be done 1985163953Srrs * after we go to the OPEN state to do the correct asconf 1986163953Srrs * processing. else, make sure we have the correct addresses in our 1987163953Srrs * lists 1988163953Srrs */ 1989163953Srrs 1990163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1991163953Srrs /* pull in local_address (our "from" address) */ 1992163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1993163953Srrs /* source addr is IPv4 */ 1994163953Srrs sin = (struct sockaddr_in *)initack_src; 1995163953Srrs memset(sin, 0, sizeof(*sin)); 1996163953Srrs sin->sin_family = AF_INET; 1997163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1998163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1999163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 2000163953Srrs /* source addr is IPv6 */ 2001163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2002163953Srrs memset(sin6, 0, sizeof(*sin6)); 2003163953Srrs sin6->sin6_family = AF_INET6; 2004163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2005163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2006163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2007163953Srrs sizeof(sin6->sin6_addr)); 2008163953Srrs } else { 2009170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2010172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2011172090Srrs SCTP_TCB_UNLOCK(stcb); 2012172090Srrs SCTP_SOCKET_LOCK(so, 1); 2013172090Srrs SCTP_TCB_LOCK(stcb); 2014172090Srrs#endif 2015171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2016172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2017172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2018172090Srrs#endif 2019172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2020163953Srrs return (NULL); 2021163953Srrs } 2022163953Srrs 2023163953Srrs /* set up to notify upper layer */ 2024163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2025163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2026163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2027163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2028163953Srrs /* 2029163953Srrs * This is an endpoint that called connect() how it got a 2030163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2031163953Srrs * the INIT was sent, but before it got there.. a complete 2032163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2033163953Srrs * should have went to the other code.. not here.. oh well.. 2034163953Srrs * a bit of protection is worth having.. 2035163953Srrs */ 2036163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2037172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2038172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2039172090Srrs SCTP_TCB_UNLOCK(stcb); 2040172090Srrs SCTP_SOCKET_LOCK(so, 1); 2041172090Srrs SCTP_TCB_LOCK(stcb); 2042172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2043172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2044172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2045172090Srrs return (NULL); 2046172090Srrs } 2047172090Srrs#endif 2048172090Srrs soisconnected(stcb->sctp_socket); 2049172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2050172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2051172090Srrs#endif 2052163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2053163953Srrs (inp->sctp_socket->so_qlimit)) { 2054163953Srrs /* 2055163953Srrs * We don't want to do anything with this one. Since it is 2056163953Srrs * the listening guy. The timer will get started for 2057163953Srrs * accepted connections in the caller. 2058163953Srrs */ 2059163953Srrs ; 2060163953Srrs } 2061163953Srrs /* since we did not send a HB make sure we don't double things */ 2062169420Srrs if ((netp) && (*netp)) 2063169420Srrs (*netp)->hb_responded = 1; 2064163953Srrs 2065163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2066163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2067163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2068163953Srrs } 2069165220Srrs /* calculate the RTT */ 2070171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2071170642Srrs if ((netp) && (*netp)) { 2072169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2073171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2074170642Srrs } 2075172190Srrs /* respond with a COOKIE-ACK */ 2076163953Srrs sctp_send_cookie_ack(stcb); 2077172190Srrs 2078172190Srrs /* 2079172190Srrs * check the address lists for any ASCONFs that need to be sent 2080172190Srrs * AFTER the cookie-ack is sent 2081172190Srrs */ 2082172190Srrs sctp_check_address_list(stcb, m, 2083172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2084172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2085172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2086172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2087172190Srrs 2088172190Srrs 2089163953Srrs return (stcb); 2090163953Srrs} 2091163953Srrs 2092163953Srrs 2093163953Srrs/* 2094163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2095163953Srrs * existing (non-NULL) TCB 2096163953Srrs */ 2097163953Srrsstatic struct mbuf * 2098163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2099163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2100163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2101169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2102179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2103163953Srrs{ 2104163953Srrs struct sctp_state_cookie *cookie; 2105163953Srrs struct sockaddr_in6 sin6; 2106163953Srrs struct sockaddr_in sin; 2107163953Srrs struct sctp_tcb *l_stcb = *stcb; 2108163953Srrs struct sctp_inpcb *l_inp; 2109163953Srrs struct sockaddr *to; 2110163953Srrs sctp_assoc_t sac_restart_id; 2111163953Srrs struct sctp_pcb *ep; 2112163953Srrs struct mbuf *m_sig; 2113163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2114163953Srrs uint8_t *sig; 2115163953Srrs uint8_t cookie_ok = 0; 2116163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2117163953Srrs unsigned int cookie_len; 2118163953Srrs struct timeval now; 2119163953Srrs struct timeval time_expires; 2120163953Srrs struct sockaddr_storage dest_store; 2121163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2122163953Srrs struct ip *iph; 2123163953Srrs int notification = 0; 2124163953Srrs struct sctp_nets *netl; 2125163953Srrs int had_a_existing_tcb = 0; 2126163953Srrs 2127169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2128169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2129163953Srrs 2130163953Srrs if (inp_p == NULL) { 2131163953Srrs return (NULL); 2132163953Srrs } 2133163953Srrs /* First get the destination address setup too. */ 2134163953Srrs iph = mtod(m, struct ip *); 2135178251Srrs switch (iph->ip_v) { 2136178251Srrs case IPVERSION: 2137178251Srrs { 2138178251Srrs /* its IPv4 */ 2139178251Srrs struct sockaddr_in *lsin; 2140163953Srrs 2141178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2142178251Srrs memset(lsin, 0, sizeof(*lsin)); 2143178251Srrs lsin->sin_family = AF_INET; 2144178251Srrs lsin->sin_len = sizeof(*lsin); 2145178251Srrs lsin->sin_port = sh->dest_port; 2146178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2147178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2148178251Srrs break; 2149178251Srrs } 2150178251Srrs#ifdef INET6 2151178251Srrs case IPV6_VERSION >> 4: 2152178251Srrs { 2153178251Srrs /* its IPv6 */ 2154178251Srrs struct ip6_hdr *ip6; 2155178251Srrs struct sockaddr_in6 *lsin6; 2156163953Srrs 2157178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2158178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2159178251Srrs lsin6->sin6_family = AF_INET6; 2160178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2161178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2162178251Srrs lsin6->sin6_port = sh->dest_port; 2163178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2164178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2165178251Srrs break; 2166178251Srrs } 2167178251Srrs#endif 2168178251Srrs default: 2169163953Srrs return (NULL); 2170163953Srrs } 2171163953Srrs 2172163953Srrs cookie = &cp->cookie; 2173163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2174163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2175163953Srrs 2176163953Srrs if ((cookie->peerport != sh->src_port) && 2177163953Srrs (cookie->myport != sh->dest_port) && 2178163953Srrs (cookie->my_vtag != sh->v_tag)) { 2179163953Srrs /* 2180163953Srrs * invalid ports or bad tag. Note that we always leave the 2181163953Srrs * v_tag in the header in network order and when we stored 2182163953Srrs * it in the my_vtag slot we also left it in network order. 2183169352Srrs * This maintains the match even though it may be in the 2184163953Srrs * opposite byte order of the machine :-> 2185163953Srrs */ 2186163953Srrs return (NULL); 2187163953Srrs } 2188163953Srrs if (cookie_len > size_of_pkt || 2189163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2190163953Srrs sizeof(struct sctp_init_chunk) + 2191163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2192163953Srrs /* cookie too long! or too small */ 2193163953Srrs return (NULL); 2194163953Srrs } 2195163953Srrs /* 2196163953Srrs * split off the signature into its own mbuf (since it should not be 2197163953Srrs * calculated in the sctp_hmac_m() call). 2198163953Srrs */ 2199163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2200163953Srrs if (sig_offset > size_of_pkt) { 2201163953Srrs /* packet not correct size! */ 2202163953Srrs /* XXX this may already be accounted for earlier... */ 2203163953Srrs return (NULL); 2204163953Srrs } 2205163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2206163953Srrs if (m_sig == NULL) { 2207163953Srrs /* out of memory or ?? */ 2208163953Srrs return (NULL); 2209163953Srrs } 2210178202Srrs#ifdef SCTP_MBUF_LOGGING 2211179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2212178202Srrs struct mbuf *mat; 2213178202Srrs 2214178202Srrs mat = m_sig; 2215178202Srrs while (mat) { 2216178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2217178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2218178202Srrs } 2219178202Srrs mat = SCTP_BUF_NEXT(mat); 2220178202Srrs } 2221178202Srrs } 2222178202Srrs#endif 2223178202Srrs 2224163953Srrs /* 2225163953Srrs * compute the signature/digest for the cookie 2226163953Srrs */ 2227163953Srrs ep = &(*inp_p)->sctp_ep; 2228163953Srrs l_inp = *inp_p; 2229163953Srrs if (l_stcb) { 2230163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2231163953Srrs } 2232163953Srrs SCTP_INP_RLOCK(l_inp); 2233163953Srrs if (l_stcb) { 2234163953Srrs SCTP_TCB_LOCK(l_stcb); 2235163953Srrs } 2236163953Srrs /* which cookie is it? */ 2237163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2238163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2239163953Srrs /* it's the old cookie */ 2240169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2241163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2242170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2243163953Srrs } else { 2244163953Srrs /* it's the current cookie */ 2245169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2246163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2247170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2248163953Srrs } 2249163953Srrs /* get the signature */ 2250163953Srrs SCTP_INP_RUNLOCK(l_inp); 2251163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2252163953Srrs if (sig == NULL) { 2253163953Srrs /* couldn't find signature */ 2254164085Srrs sctp_m_freem(m_sig); 2255163953Srrs return (NULL); 2256163953Srrs } 2257163953Srrs /* compare the received digest with the computed digest */ 2258163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2259163953Srrs /* try the old cookie? */ 2260163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2261163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2262163953Srrs /* compute digest with old */ 2263169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2264163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2265170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2266163953Srrs /* compare */ 2267163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2268163953Srrs cookie_ok = 1; 2269163953Srrs } 2270163953Srrs } else { 2271163953Srrs cookie_ok = 1; 2272163953Srrs } 2273163953Srrs 2274163953Srrs /* 2275163953Srrs * Now before we continue we must reconstruct our mbuf so that 2276163953Srrs * normal processing of any other chunks will work. 2277163953Srrs */ 2278163953Srrs { 2279163953Srrs struct mbuf *m_at; 2280163953Srrs 2281163953Srrs m_at = m; 2282165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2283165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2284163953Srrs } 2285165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2286163953Srrs } 2287163953Srrs 2288163953Srrs if (cookie_ok == 0) { 2289169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2290169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2291169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2292169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2293163953Srrs return (NULL); 2294163953Srrs } 2295163953Srrs /* 2296163953Srrs * check the cookie timestamps to be sure it's not stale 2297163953Srrs */ 2298169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2299163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2300169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2301163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2302180387Srrs /* 2303180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2304180387Srrs * is undefined. 2305180387Srrs */ 2306163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2307163953Srrs /* cookie is stale! */ 2308163953Srrs struct mbuf *op_err; 2309163953Srrs struct sctp_stale_cookie_msg *scm; 2310163953Srrs uint32_t tim; 2311163953Srrs 2312163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2313165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2314163953Srrs if (op_err == NULL) { 2315163953Srrs /* FOOBAR */ 2316163953Srrs return (NULL); 2317163953Srrs } 2318163953Srrs /* pre-reserve some space */ 2319178251Srrs#ifdef INET6 2320165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2321178251Srrs#else 2322178251Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 2323178251Srrs#endif 2324165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2325165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2326163953Srrs 2327163953Srrs /* Set the len */ 2328165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2329163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2330163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2331163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2332163953Srrs (sizeof(uint32_t)))); 2333163953Srrs /* seconds to usec */ 2334163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2335163953Srrs /* add in usec */ 2336163953Srrs if (tim == 0) 2337163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2338163953Srrs scm->time_usec = htonl(tim); 2339169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2340179157Srrs vrf_id, port); 2341163953Srrs return (NULL); 2342163953Srrs } 2343163953Srrs /* 2344163953Srrs * Now we must see with the lookup address if we have an existing 2345163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2346163953Srrs * and a INIT collided with us and somewhere the peer sent the 2347163953Srrs * cookie on another address besides the single address our assoc 2348163953Srrs * had for him. In this case we will have one of the tie-tags set at 2349163953Srrs * least AND the address field in the cookie can be used to look it 2350163953Srrs * up. 2351163953Srrs */ 2352163953Srrs to = NULL; 2353163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2354163953Srrs memset(&sin6, 0, sizeof(sin6)); 2355163953Srrs sin6.sin6_family = AF_INET6; 2356163953Srrs sin6.sin6_len = sizeof(sin6); 2357163953Srrs sin6.sin6_port = sh->src_port; 2358163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2359163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2360163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2361163953Srrs to = (struct sockaddr *)&sin6; 2362163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2363163953Srrs memset(&sin, 0, sizeof(sin)); 2364163953Srrs sin.sin_family = AF_INET; 2365163953Srrs sin.sin_len = sizeof(sin); 2366163953Srrs sin.sin_port = sh->src_port; 2367163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2368163953Srrs to = (struct sockaddr *)&sin; 2369169420Srrs } else { 2370169420Srrs /* This should not happen */ 2371169420Srrs return (NULL); 2372163953Srrs } 2373163953Srrs if ((*stcb == NULL) && to) { 2374163953Srrs /* Yep, lets check */ 2375163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2376163953Srrs if (*stcb == NULL) { 2377163953Srrs /* 2378163953Srrs * We should have only got back the same inp. If we 2379163953Srrs * got back a different ep we have a problem. The 2380163953Srrs * original findep got back l_inp and now 2381163953Srrs */ 2382163953Srrs if (l_inp != *inp_p) { 2383169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2384163953Srrs } 2385165220Srrs } else { 2386165220Srrs if (*locked_tcb == NULL) { 2387165220Srrs /* 2388165220Srrs * In this case we found the assoc only 2389165220Srrs * after we locked the create lock. This 2390165220Srrs * means we are in a colliding case and we 2391165220Srrs * must make sure that we unlock the tcb if 2392165220Srrs * its one of the cases where we throw away 2393165220Srrs * the incoming packets. 2394165220Srrs */ 2395165220Srrs *locked_tcb = *stcb; 2396165220Srrs 2397165220Srrs /* 2398165220Srrs * We must also increment the inp ref count 2399165220Srrs * since the ref_count flags was set when we 2400165220Srrs * did not find the TCB, now we found it 2401165220Srrs * which reduces the refcount.. we must 2402165220Srrs * raise it back out to balance it all :-) 2403165220Srrs */ 2404165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2405165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2406169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2407165220Srrs (*stcb)->sctp_ep, l_inp); 2408165220Srrs } 2409165220Srrs } 2410163953Srrs } 2411163953Srrs } 2412169420Srrs if (to == NULL) 2413169420Srrs return (NULL); 2414169420Srrs 2415163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2416163953Srrs if (*stcb == NULL) { 2417163953Srrs /* this is the "normal" case... get a new TCB */ 2418163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2419163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2420179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2421163953Srrs } else { 2422163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2423163953Srrs had_a_existing_tcb = 1; 2424163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2425170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2426170181Srrs ¬ification, &sac_restart_id, vrf_id); 2427163953Srrs } 2428163953Srrs 2429163953Srrs if (*stcb == NULL) { 2430163953Srrs /* still no TCB... must be bad cookie-echo */ 2431163953Srrs return (NULL); 2432163953Srrs } 2433163953Srrs /* 2434163953Srrs * Ok, we built an association so confirm the address we sent the 2435163953Srrs * INIT-ACK to. 2436163953Srrs */ 2437163953Srrs netl = sctp_findnet(*stcb, to); 2438163953Srrs /* 2439163953Srrs * This code should in theory NOT run but 2440163953Srrs */ 2441163953Srrs if (netl == NULL) { 2442163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2443163953Srrs int ret; 2444163953Srrs 2445165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2446165220Srrs SCTP_IN_COOKIE_PROC); 2447163953Srrs netl = sctp_findnet(*stcb, to); 2448163953Srrs } 2449163953Srrs if (netl) { 2450163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2451163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2452169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2453163953Srrs netl); 2454163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2455172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2456163953Srrs } 2457163953Srrs } 2458163953Srrs if (*stcb) { 2459163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2460163953Srrs *stcb, NULL); 2461163953Srrs } 2462163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2463163953Srrs if (!had_a_existing_tcb || 2464163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2465163953Srrs /* 2466163953Srrs * If we have a NEW cookie or the connect never 2467163953Srrs * reached the connected state during collision we 2468163953Srrs * must do the TCP accept thing. 2469163953Srrs */ 2470163953Srrs struct socket *so, *oso; 2471163953Srrs struct sctp_inpcb *inp; 2472163953Srrs 2473163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2474163953Srrs /* 2475163953Srrs * For a restart we will keep the same 2476163953Srrs * socket, no need to do anything. I THINK!! 2477163953Srrs */ 2478172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2479163953Srrs return (m); 2480163953Srrs } 2481163953Srrs oso = (*inp_p)->sctp_socket; 2482171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2483163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2484166086Srrs so = sonewconn(oso, 0 2485163953Srrs ); 2486163953Srrs SCTP_TCB_LOCK((*stcb)); 2487171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2488171858Srrs 2489163953Srrs if (so == NULL) { 2490163953Srrs struct mbuf *op_err; 2491163953Srrs 2492172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2493172090Srrs struct socket *pcb_so; 2494172090Srrs 2495172090Srrs#endif 2496163953Srrs /* Too many sockets */ 2497169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2498163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2499163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2500179157Srrs sh, op_err, vrf_id, port); 2501172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2502172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2503172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2504172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2505172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2506172090Srrs SCTP_TCB_LOCK((*stcb)); 2507172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2508172090Srrs#endif 2509171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2510172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2511172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2512172090Srrs#endif 2513163953Srrs return (NULL); 2514163953Srrs } 2515163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2516166086Srrs SCTP_INP_INCR_REF(inp); 2517166086Srrs /* 2518166086Srrs * We add the unbound flag here so that if we get an 2519166086Srrs * soabort() before we get the move_pcb done, we 2520166086Srrs * will properly cleanup. 2521166086Srrs */ 2522163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2523163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2524163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2525166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2526163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2527163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2528163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2529172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2530163953Srrs inp->sctp_socket = so; 2531163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2532163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2533163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2534163953Srrs inp->inp_starting_point_for_iterator = NULL; 2535163953Srrs /* 2536163953Srrs * copy in the authentication parameters from the 2537163953Srrs * original endpoint 2538163953Srrs */ 2539163953Srrs if (inp->sctp_ep.local_hmacs) 2540163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2541163953Srrs inp->sctp_ep.local_hmacs = 2542163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2543163953Srrs if (inp->sctp_ep.local_auth_chunks) 2544163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2545163953Srrs inp->sctp_ep.local_auth_chunks = 2546163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2547163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2548163953Srrs &inp->sctp_ep.shared_keys); 2549163953Srrs 2550163953Srrs /* 2551163953Srrs * Now we must move it from one hash table to 2552163953Srrs * another and get the tcb in the right place. 2553163953Srrs */ 2554163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2555170056Srrs 2556170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2557170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2558170056Srrs 2559175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2560175845Srwatson 0); 2561170056Srrs SCTP_TCB_LOCK((*stcb)); 2562170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2563163953Srrs 2564170056Srrs 2565166086Srrs /* 2566166086Srrs * now we must check to see if we were aborted while 2567166086Srrs * the move was going on and the lock/unlock 2568166086Srrs * happened. 2569166086Srrs */ 2570166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2571166086Srrs /* 2572166086Srrs * yep it was, we leave the assoc attached 2573166086Srrs * to the socket since the sctp_inpcb_free() 2574166086Srrs * call will send an abort for us. 2575166086Srrs */ 2576166086Srrs SCTP_INP_DECR_REF(inp); 2577166086Srrs return (NULL); 2578166086Srrs } 2579166086Srrs SCTP_INP_DECR_REF(inp); 2580163953Srrs /* Switch over to the new guy */ 2581163953Srrs *inp_p = inp; 2582172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2583163953Srrs 2584166086Srrs /* 2585166086Srrs * Pull it from the incomplete queue and wake the 2586166086Srrs * guy 2587166086Srrs */ 2588172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2589172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2590172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2591172090Srrs SCTP_SOCKET_LOCK(so, 1); 2592172090Srrs#endif 2593166086Srrs soisconnected(so); 2594172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2595172118Srrs SCTP_TCB_LOCK((*stcb)); 2596172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2597172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2598172090Srrs#endif 2599163953Srrs return (m); 2600163953Srrs } 2601163953Srrs } 2602163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2603172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2604163953Srrs } 2605163953Srrs return (m); 2606163953Srrs} 2607163953Srrs 2608163953Srrsstatic void 2609163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2610163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2611163953Srrs{ 2612163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2613163953Srrs struct sctp_association *asoc; 2614163953Srrs 2615169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2616169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2617163953Srrs if (stcb == NULL) 2618163953Srrs return; 2619163953Srrs 2620163953Srrs asoc = &stcb->asoc; 2621163953Srrs 2622163953Srrs sctp_stop_all_cookie_timers(stcb); 2623163953Srrs /* process according to association state */ 2624163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2625163953Srrs /* state change only needed when I am in right state */ 2626169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2627171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2628163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2629163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2630163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2631163953Srrs 2632163953Srrs } 2633163953Srrs /* update RTO */ 2634163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2635163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2636163953Srrs if (asoc->overall_error_count == 0) { 2637163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2638171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2639163953Srrs } 2640169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2641172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2642163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2643163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2644172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2645172090Srrs struct socket *so; 2646172090Srrs 2647172090Srrs#endif 2648163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2649172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2650172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2651172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2652172090Srrs SCTP_TCB_UNLOCK(stcb); 2653172090Srrs SCTP_SOCKET_LOCK(so, 1); 2654172090Srrs SCTP_TCB_LOCK(stcb); 2655172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2656172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2657172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2658172090Srrs return; 2659172090Srrs } 2660172090Srrs#endif 2661172090Srrs soisconnected(stcb->sctp_socket); 2662172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2663172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2664172090Srrs#endif 2665163953Srrs } 2666163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2667163953Srrs stcb, net); 2668163953Srrs /* 2669163953Srrs * since we did not send a HB make sure we don't double 2670163953Srrs * things 2671163953Srrs */ 2672163953Srrs net->hb_responded = 1; 2673163953Srrs 2674163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2675163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2676163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2677163953Srrs stcb->sctp_ep, stcb, NULL); 2678163953Srrs } 2679163953Srrs /* 2680171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2681171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2682171572Srrs * in flight) 2683163953Srrs */ 2684163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2685163953Srrs (stcb->asoc.peer_supports_asconf) && 2686163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2687171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2688163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2689163953Srrs stcb->sctp_ep, stcb, 2690163953Srrs stcb->asoc.primary_destination); 2691171572Srrs#else 2692172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2693172190Srrs SCTP_ADDR_NOT_LOCKED); 2694171572Srrs#endif 2695163953Srrs } 2696163953Srrs } 2697163953Srrs /* Toss the cookie if I can */ 2698163953Srrs sctp_toss_old_cookies(stcb, asoc); 2699163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2700163953Srrs /* Restart the timer if we have pending data */ 2701163953Srrs struct sctp_tmit_chunk *chk; 2702163953Srrs 2703163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2704163953Srrs if (chk) { 2705163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2706163953Srrs stcb, chk->whoTo); 2707163953Srrs } 2708163953Srrs } 2709163953Srrs} 2710163953Srrs 2711163953Srrsstatic void 2712163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2713163953Srrs struct sctp_tcb *stcb) 2714163953Srrs{ 2715163953Srrs struct sctp_nets *net; 2716163953Srrs struct sctp_tmit_chunk *lchk; 2717163953Srrs uint32_t tsn; 2718163953Srrs 2719163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2720163953Srrs return; 2721163953Srrs } 2722163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2723163953Srrs tsn = ntohl(cp->tsn); 2724163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2725163953Srrs /* Also we make sure we disable the nonce_wait */ 2726163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2727163953Srrs if (lchk == NULL) { 2728163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2729163953Srrs } else { 2730163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2731163953Srrs } 2732163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2733163953Srrs stcb->asoc.nonce_sum_check = 0; 2734163953Srrs 2735163953Srrs /* Find where it was sent, if possible */ 2736163953Srrs net = NULL; 2737163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2738163953Srrs while (lchk) { 2739163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2740163953Srrs net = lchk->whoTo; 2741163953Srrs break; 2742163953Srrs } 2743163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2744163953Srrs break; 2745163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2746163953Srrs } 2747163953Srrs if (net == NULL) 2748163953Srrs /* default is we use the primary */ 2749163953Srrs net = stcb->asoc.primary_destination; 2750163953Srrs 2751163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2752163953Srrs /* 2753171440Srrs * JRS - Use the congestion control given in the pluggable 2754171440Srrs * CC module 2755171440Srrs */ 2756171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2757171440Srrs /* 2758163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2759163953Srrs * the next sending seq i.e. the resync_tsn. 2760163953Srrs */ 2761163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2762163953Srrs } 2763163953Srrs /* 2764163953Srrs * We always send a CWR this way if our previous one was lost our 2765163953Srrs * peer will get an update, or if it is not time again to reduce we 2766163953Srrs * still get the cwr to the peer. 2767163953Srrs */ 2768163953Srrs sctp_send_cwr(stcb, net, tsn); 2769163953Srrs} 2770163953Srrs 2771163953Srrsstatic void 2772163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2773163953Srrs{ 2774163953Srrs /* 2775163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2776163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2777163953Srrs * If so remove it. 2778163953Srrs */ 2779163953Srrs struct sctp_tmit_chunk *chk; 2780163953Srrs struct sctp_ecne_chunk *ecne; 2781163953Srrs 2782163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2783163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2784163953Srrs continue; 2785163953Srrs } 2786163953Srrs /* 2787163953Srrs * Look for and remove if it is the right TSN. Since there 2788163953Srrs * is only ONE ECNE on the control queue at any one time we 2789163953Srrs * don't need to worry about more than one! 2790163953Srrs */ 2791163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2792163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2793163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2794163953Srrs /* this covers this ECNE, we can remove it */ 2795163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2796163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2797163953Srrs sctp_next); 2798163953Srrs if (chk->data) { 2799163953Srrs sctp_m_freem(chk->data); 2800163953Srrs chk->data = NULL; 2801163953Srrs } 2802163953Srrs stcb->asoc.ctrl_queue_cnt--; 2803163953Srrs sctp_free_a_chunk(stcb, chk); 2804163953Srrs break; 2805163953Srrs } 2806163953Srrs } 2807163953Srrs} 2808163953Srrs 2809163953Srrsstatic void 2810163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2811163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2812163953Srrs{ 2813163953Srrs struct sctp_association *asoc; 2814163953Srrs 2815172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2816172090Srrs struct socket *so; 2817172090Srrs 2818172090Srrs#endif 2819172090Srrs 2820169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2821169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2822163953Srrs if (stcb == NULL) 2823163953Srrs return; 2824163953Srrs 2825163953Srrs asoc = &stcb->asoc; 2826163953Srrs /* process according to association state */ 2827163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2828163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2829171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2830171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 2831163953Srrs SCTP_TCB_UNLOCK(stcb); 2832163953Srrs return; 2833163953Srrs } 2834163953Srrs /* notify upper layer protocol */ 2835163953Srrs if (stcb->sctp_socket) { 2836172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2837163953Srrs /* are the queues empty? they should be */ 2838163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2839163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2840163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2841172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 2842163953Srrs } 2843163953Srrs } 2844163953Srrs /* stop the timer */ 2845178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2846163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2847163953Srrs /* free the TCB */ 2848171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2849171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 2850172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2851172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2852172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2853172090Srrs SCTP_TCB_UNLOCK(stcb); 2854172090Srrs SCTP_SOCKET_LOCK(so, 1); 2855172090Srrs SCTP_TCB_LOCK(stcb); 2856172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2857172090Srrs#endif 2858171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2859172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2860172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2861172090Srrs#endif 2862163953Srrs return; 2863163953Srrs} 2864163953Srrs 2865163953Srrsstatic int 2866163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2867163953Srrs struct sctp_nets *net, uint8_t flg) 2868163953Srrs{ 2869163953Srrs switch (desc->chunk_type) { 2870163953Srrs case SCTP_DATA: 2871163953Srrs /* find the tsn to resend (possibly */ 2872163953Srrs { 2873163953Srrs uint32_t tsn; 2874163953Srrs struct sctp_tmit_chunk *tp1; 2875163953Srrs 2876163953Srrs tsn = ntohl(desc->tsn_ifany); 2877163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2878163953Srrs while (tp1) { 2879163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2880163953Srrs /* found it */ 2881163953Srrs break; 2882163953Srrs } 2883163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2884163953Srrs MAX_TSN)) { 2885163953Srrs /* not found */ 2886163953Srrs tp1 = NULL; 2887163953Srrs break; 2888163953Srrs } 2889163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2890163953Srrs } 2891163953Srrs if (tp1 == NULL) { 2892163953Srrs /* 2893163953Srrs * Do it the other way , aka without paying 2894163953Srrs * attention to queue seq order. 2895163953Srrs */ 2896163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2897163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2898163953Srrs while (tp1) { 2899163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2900163953Srrs /* found it */ 2901163953Srrs break; 2902163953Srrs } 2903163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2904163953Srrs } 2905163953Srrs } 2906163953Srrs if (tp1 == NULL) { 2907163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2908163953Srrs } 2909163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2910163953Srrs uint8_t *ddp; 2911163953Srrs 2912163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2913163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2914163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2915163953Srrs return (0); 2916163953Srrs } 2917163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2918163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2919163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2920163953Srrs return (0); 2921163953Srrs } 2922163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2923163953Srrs sizeof(struct sctp_data_chunk)); 2924163953Srrs { 2925163953Srrs unsigned int iii; 2926163953Srrs 2927163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2928163953Srrs iii++) { 2929163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2930163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2931163953Srrs return (-1); 2932163953Srrs } 2933163953Srrs } 2934163953Srrs } 2935163953Srrs /* 2936163953Srrs * We zero out the nonce so resync not 2937163953Srrs * needed 2938163953Srrs */ 2939163953Srrs tp1->rec.data.ect_nonce = 0; 2940163953Srrs 2941163953Srrs if (tp1->do_rtt) { 2942163953Srrs /* 2943163953Srrs * this guy had a RTO calculation 2944163953Srrs * pending on it, cancel it 2945163953Srrs */ 2946163953Srrs tp1->do_rtt = 0; 2947163953Srrs } 2948163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2949163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2950163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2951163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2952163953Srrs /* 2953163953Srrs * mark it as if we were doing a FR, since 2954163953Srrs * we will be getting gap ack reports behind 2955163953Srrs * the info from the router. 2956163953Srrs */ 2957163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2958163953Srrs /* 2959163953Srrs * mark the tsn with what sequences can 2960163953Srrs * cause a new FR. 2961163953Srrs */ 2962163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2963163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2964163953Srrs } else { 2965163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2966163953Srrs } 2967163953Srrs 2968163953Srrs /* restart the timer */ 2969163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2970171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2971163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2972163953Srrs stcb, tp1->whoTo); 2973163953Srrs 2974163953Srrs /* fix counts and things */ 2975179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 2976170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2977170744Srrs tp1->whoTo->flight_size, 2978170744Srrs tp1->book_size, 2979170744Srrs (uintptr_t) stcb, 2980170744Srrs tp1->rec.data.TSN_seq); 2981170744Srrs } 2982168709Srrs sctp_flight_size_decrease(tp1); 2983168709Srrs sctp_total_flight_decrease(stcb, tp1); 2984163953Srrs } { 2985163953Srrs /* audit code */ 2986163953Srrs unsigned int audit; 2987163953Srrs 2988163953Srrs audit = 0; 2989163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2990163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2991163953Srrs audit++; 2992163953Srrs } 2993163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2994163953Srrs sctp_next) { 2995163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2996163953Srrs audit++; 2997163953Srrs } 2998163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2999169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3000163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3001163953Srrs#ifndef SCTP_AUDITING_ENABLED 3002163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3003163953Srrs#endif 3004163953Srrs } 3005163953Srrs } 3006163953Srrs } 3007163953Srrs break; 3008163953Srrs case SCTP_ASCONF: 3009163953Srrs { 3010163953Srrs struct sctp_tmit_chunk *asconf; 3011163953Srrs 3012163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3013163953Srrs sctp_next) { 3014163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3015163953Srrs break; 3016163953Srrs } 3017163953Srrs } 3018163953Srrs if (asconf) { 3019163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3020163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3021163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3022163953Srrs asconf->snd_count--; 3023163953Srrs } 3024163953Srrs } 3025163953Srrs break; 3026163953Srrs case SCTP_INITIATION: 3027163953Srrs /* resend the INIT */ 3028163953Srrs stcb->asoc.dropped_special_cnt++; 3029163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3030163953Srrs /* 3031163953Srrs * If we can get it in, in a few attempts we do 3032163953Srrs * this, otherwise we let the timer fire. 3033163953Srrs */ 3034163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3035171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3036172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3037163953Srrs } 3038163953Srrs break; 3039163953Srrs case SCTP_SELECTIVE_ACK: 3040163953Srrs /* resend the sack */ 3041163953Srrs sctp_send_sack(stcb); 3042163953Srrs break; 3043163953Srrs case SCTP_HEARTBEAT_REQUEST: 3044163953Srrs /* resend a demand HB */ 3045171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3046171440Srrs /* 3047171440Srrs * Only retransmit if we KNOW we wont destroy the 3048171440Srrs * tcb 3049171440Srrs */ 3050171440Srrs (void)sctp_send_hb(stcb, 1, net); 3051171440Srrs } 3052163953Srrs break; 3053163953Srrs case SCTP_SHUTDOWN: 3054163953Srrs sctp_send_shutdown(stcb, net); 3055163953Srrs break; 3056163953Srrs case SCTP_SHUTDOWN_ACK: 3057163953Srrs sctp_send_shutdown_ack(stcb, net); 3058163953Srrs break; 3059163953Srrs case SCTP_COOKIE_ECHO: 3060163953Srrs { 3061163953Srrs struct sctp_tmit_chunk *cookie; 3062163953Srrs 3063163953Srrs cookie = NULL; 3064163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3065163953Srrs sctp_next) { 3066163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3067163953Srrs break; 3068163953Srrs } 3069163953Srrs } 3070163953Srrs if (cookie) { 3071163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3072163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3073163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3074163953Srrs sctp_stop_all_cookie_timers(stcb); 3075163953Srrs } 3076163953Srrs } 3077163953Srrs break; 3078163953Srrs case SCTP_COOKIE_ACK: 3079163953Srrs sctp_send_cookie_ack(stcb); 3080163953Srrs break; 3081163953Srrs case SCTP_ASCONF_ACK: 3082163953Srrs /* resend last asconf ack */ 3083171990Srrs sctp_send_asconf_ack(stcb); 3084163953Srrs break; 3085163953Srrs case SCTP_FORWARD_CUM_TSN: 3086163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3087163953Srrs break; 3088163953Srrs /* can't do anything with these */ 3089163953Srrs case SCTP_PACKET_DROPPED: 3090163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3091163953Srrs case SCTP_HEARTBEAT_ACK: 3092163953Srrs case SCTP_ABORT_ASSOCIATION: 3093163953Srrs case SCTP_OPERATION_ERROR: 3094163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3095163953Srrs case SCTP_ECN_ECHO: 3096163953Srrs case SCTP_ECN_CWR: 3097163953Srrs default: 3098163953Srrs break; 3099163953Srrs } 3100163953Srrs return (0); 3101163953Srrs} 3102163953Srrs 3103163953Srrsvoid 3104163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3105163953Srrs{ 3106163953Srrs int i; 3107163953Srrs uint16_t temp; 3108163953Srrs 3109163953Srrs /* 3110163953Srrs * We set things to 0xffff since this is the last delivered sequence 3111163953Srrs * and we will be sending in 0 after the reset. 3112163953Srrs */ 3113163953Srrs 3114163953Srrs if (number_entries) { 3115163953Srrs for (i = 0; i < number_entries; i++) { 3116163953Srrs temp = ntohs(list[i]); 3117163953Srrs if (temp >= stcb->asoc.streamincnt) { 3118163953Srrs continue; 3119163953Srrs } 3120163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3121163953Srrs } 3122163953Srrs } else { 3123163953Srrs list = NULL; 3124163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3125163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3126163953Srrs } 3127163953Srrs } 3128172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3129163953Srrs} 3130163953Srrs 3131163953Srrsstatic void 3132163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3133163953Srrs{ 3134163953Srrs int i; 3135163953Srrs 3136163953Srrs if (number_entries == 0) { 3137163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3138163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3139163953Srrs } 3140163953Srrs } else if (number_entries) { 3141163953Srrs for (i = 0; i < number_entries; i++) { 3142163953Srrs uint16_t temp; 3143163953Srrs 3144163953Srrs temp = ntohs(list[i]); 3145163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3146163953Srrs /* no such stream */ 3147163953Srrs continue; 3148163953Srrs } 3149163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3150163953Srrs } 3151163953Srrs } 3152172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3153163953Srrs} 3154163953Srrs 3155163953Srrs 3156163953Srrsstruct sctp_stream_reset_out_request * 3157163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3158163953Srrs{ 3159163953Srrs struct sctp_association *asoc; 3160163953Srrs struct sctp_stream_reset_out_req *req; 3161163953Srrs struct sctp_stream_reset_out_request *r; 3162163953Srrs struct sctp_tmit_chunk *chk; 3163163953Srrs int len, clen; 3164163953Srrs 3165163953Srrs asoc = &stcb->asoc; 3166163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3167169208Srrs asoc->stream_reset_outstanding = 0; 3168163953Srrs return (NULL); 3169163953Srrs } 3170163953Srrs if (stcb->asoc.str_reset == NULL) { 3171169208Srrs asoc->stream_reset_outstanding = 0; 3172163953Srrs return (NULL); 3173163953Srrs } 3174163953Srrs chk = stcb->asoc.str_reset; 3175163953Srrs if (chk->data == NULL) { 3176163953Srrs return (NULL); 3177163953Srrs } 3178163953Srrs if (bchk) { 3179163953Srrs /* he wants a copy of the chk pointer */ 3180163953Srrs *bchk = chk; 3181163953Srrs } 3182163953Srrs clen = chk->send_size; 3183163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3184163953Srrs r = &req->sr_req; 3185163953Srrs if (ntohl(r->request_seq) == seq) { 3186163953Srrs /* found it */ 3187163953Srrs return (r); 3188163953Srrs } 3189163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3190163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3191163953Srrs /* move to the next one, there can only be a max of two */ 3192163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3193163953Srrs if (ntohl(r->request_seq) == seq) { 3194163953Srrs return (r); 3195163953Srrs } 3196163953Srrs } 3197163953Srrs /* that seq is not here */ 3198163953Srrs return (NULL); 3199163953Srrs} 3200163953Srrs 3201163953Srrsstatic void 3202163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3203163953Srrs{ 3204163953Srrs struct sctp_association *asoc; 3205163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3206163953Srrs 3207163953Srrs if (stcb->asoc.str_reset == NULL) { 3208163953Srrs return; 3209163953Srrs } 3210166675Srrs asoc = &stcb->asoc; 3211166675Srrs 3212171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3213163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3214163953Srrs chk, 3215163953Srrs sctp_next); 3216163953Srrs if (chk->data) { 3217163953Srrs sctp_m_freem(chk->data); 3218163953Srrs chk->data = NULL; 3219163953Srrs } 3220163953Srrs asoc->ctrl_queue_cnt--; 3221163953Srrs sctp_free_a_chunk(stcb, chk); 3222172156Srrs /* sa_ignore NO_NULL_CHK */ 3223163953Srrs stcb->asoc.str_reset = NULL; 3224163953Srrs} 3225163953Srrs 3226163953Srrs 3227163953Srrsstatic int 3228163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3229163953Srrs uint32_t seq, uint32_t action, 3230164139Srrs struct sctp_stream_reset_response *respin) 3231163953Srrs{ 3232163953Srrs uint16_t type; 3233163953Srrs int lparm_len; 3234163953Srrs struct sctp_association *asoc = &stcb->asoc; 3235163953Srrs struct sctp_tmit_chunk *chk; 3236163953Srrs struct sctp_stream_reset_out_request *srparam; 3237163953Srrs int number_entries; 3238163953Srrs 3239163953Srrs if (asoc->stream_reset_outstanding == 0) { 3240163953Srrs /* duplicate */ 3241163953Srrs return (0); 3242163953Srrs } 3243163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3244163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3245163953Srrs if (srparam) { 3246163953Srrs stcb->asoc.str_reset_seq_out++; 3247163953Srrs type = ntohs(srparam->ph.param_type); 3248163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3249163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3250164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3251163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3252163953Srrs if (asoc->stream_reset_outstanding) 3253163953Srrs asoc->stream_reset_outstanding--; 3254163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3255163953Srrs /* do it */ 3256163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3257163953Srrs } else { 3258172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3259163953Srrs } 3260163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3261163953Srrs /* Answered my request */ 3262164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3263163953Srrs if (asoc->stream_reset_outstanding) 3264163953Srrs asoc->stream_reset_outstanding--; 3265163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3266172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3267163953Srrs } 3268163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3269163953Srrs /** 3270163953Srrs * a) Adopt the new in tsn. 3271163953Srrs * b) reset the map 3272163953Srrs * c) Adopt the new out-tsn 3273163953Srrs */ 3274163953Srrs struct sctp_stream_reset_response_tsn *resp; 3275163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3276163953Srrs int abort_flag = 0; 3277163953Srrs 3278163953Srrs if (respin == NULL) { 3279163953Srrs /* huh ? */ 3280163953Srrs return (0); 3281163953Srrs } 3282163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3283163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3284163953Srrs asoc->stream_reset_outstanding--; 3285163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3286163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3287163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3288170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3289163953Srrs if (abort_flag) { 3290163953Srrs return (1); 3291163953Srrs } 3292163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3293179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3294179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3295179783Srrs } 3296163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3297163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3298163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3299163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3300163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3301163953Srrs 3302163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3303163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3304163953Srrs 3305163953Srrs } 3306163953Srrs } 3307163953Srrs /* get rid of the request and get the request flags */ 3308163953Srrs if (asoc->stream_reset_outstanding == 0) { 3309163953Srrs sctp_clean_up_stream_reset(stcb); 3310163953Srrs } 3311163953Srrs } 3312163953Srrs } 3313163953Srrs return (0); 3314163953Srrs} 3315163953Srrs 3316163953Srrsstatic void 3317163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3318163953Srrs struct sctp_tmit_chunk *chk, 3319170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3320163953Srrs{ 3321163953Srrs uint32_t seq; 3322163953Srrs int len, i; 3323163953Srrs int number_entries; 3324163953Srrs uint16_t temp; 3325163953Srrs 3326163953Srrs /* 3327163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3328163953Srrs * seq_in is right. 3329163953Srrs */ 3330163953Srrs struct sctp_association *asoc = &stcb->asoc; 3331163953Srrs 3332163953Srrs seq = ntohl(req->request_seq); 3333163953Srrs if (asoc->str_reset_seq_in == seq) { 3334170992Srrs if (trunc) { 3335170992Srrs /* Can't do it, since they exceeded our buffer size */ 3336170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3337170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3338170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3339170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3340163953Srrs len = ntohs(req->ph.param_length); 3341163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3342163953Srrs for (i = 0; i < number_entries; i++) { 3343163953Srrs temp = ntohs(req->list_of_streams[i]); 3344163953Srrs req->list_of_streams[i] = temp; 3345163953Srrs } 3346163953Srrs /* move the reset action back one */ 3347163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3348163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3349163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3350163953Srrs asoc->str_reset_seq_out, 3351163953Srrs seq, (asoc->sending_seq - 1)); 3352163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3353163953Srrs asoc->str_reset = chk; 3354163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3355163953Srrs stcb->asoc.stream_reset_outstanding++; 3356163953Srrs } else { 3357163953Srrs /* Can't do it, since we have sent one out */ 3358163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3359163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3360163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3361163953Srrs } 3362163953Srrs asoc->str_reset_seq_in++; 3363163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3364163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3365163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3366163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3367163953Srrs } else { 3368163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3369163953Srrs } 3370163953Srrs} 3371163953Srrs 3372163953Srrsstatic int 3373163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3374163953Srrs struct sctp_tmit_chunk *chk, 3375163953Srrs struct sctp_stream_reset_tsn_request *req) 3376163953Srrs{ 3377163953Srrs /* reset all in and out and update the tsn */ 3378163953Srrs /* 3379163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3380163953Srrs * and set cum-ack to it. Also process this selected number as a 3381163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3382163953Srrs */ 3383163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3384163953Srrs struct sctp_association *asoc = &stcb->asoc; 3385163953Srrs int abort_flag = 0; 3386163953Srrs uint32_t seq; 3387163953Srrs 3388163953Srrs seq = ntohl(req->request_seq); 3389163953Srrs if (asoc->str_reset_seq_in == seq) { 3390163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3391163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3392163953Srrs fwdtsn.ch.chunk_flags = 0; 3393163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3394170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3395163953Srrs if (abort_flag) { 3396163953Srrs return (1); 3397163953Srrs } 3398163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3399179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3400179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3401179783Srrs } 3402163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3403163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3404163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3405163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3406163953Srrs /* save off historical data for retrans */ 3407163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3408163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3409163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3410163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3411163953Srrs 3412163953Srrs sctp_add_stream_reset_result_tsn(chk, 3413163953Srrs ntohl(req->request_seq), 3414163953Srrs SCTP_STREAM_RESET_PERFORMED, 3415163953Srrs stcb->asoc.sending_seq, 3416163953Srrs stcb->asoc.mapping_array_base_tsn); 3417163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3418163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3419163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3420163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3421163953Srrs 3422163953Srrs asoc->str_reset_seq_in++; 3423163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3424163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3425163953Srrs stcb->asoc.last_sending_seq[0], 3426163953Srrs stcb->asoc.last_base_tsnsent[0] 3427163953Srrs ); 3428163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3429163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3430163953Srrs stcb->asoc.last_sending_seq[1], 3431163953Srrs stcb->asoc.last_base_tsnsent[1] 3432163953Srrs ); 3433163953Srrs } else { 3434163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3435163953Srrs } 3436163953Srrs return (0); 3437163953Srrs} 3438163953Srrs 3439163953Srrsstatic void 3440163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3441163953Srrs struct sctp_tmit_chunk *chk, 3442170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3443163953Srrs{ 3444163953Srrs uint32_t seq, tsn; 3445163953Srrs int number_entries, len; 3446163953Srrs struct sctp_association *asoc = &stcb->asoc; 3447163953Srrs 3448163953Srrs seq = ntohl(req->request_seq); 3449163953Srrs 3450163953Srrs /* now if its not a duplicate we process it */ 3451163953Srrs if (asoc->str_reset_seq_in == seq) { 3452163953Srrs len = ntohs(req->ph.param_length); 3453163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3454163953Srrs /* 3455163953Srrs * the sender is resetting, handle the list issue.. we must 3456163953Srrs * a) verify if we can do the reset, if so no problem b) If 3457163953Srrs * we can't do the reset we must copy the request. c) queue 3458163953Srrs * it, and setup the data in processor to trigger it off 3459163953Srrs * when needed and dequeue all the queued data. 3460163953Srrs */ 3461163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3462163953Srrs 3463163953Srrs /* move the reset action back one */ 3464163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3465170992Srrs if (trunc) { 3466170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3467170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3468170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3469163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3470163953Srrs /* we can do it now */ 3471163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3472163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3473163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3474163953Srrs } else { 3475163953Srrs /* 3476163953Srrs * we must queue it up and thus wait for the TSN's 3477163953Srrs * to arrive that are at or before tsn 3478163953Srrs */ 3479163953Srrs struct sctp_stream_reset_list *liste; 3480163953Srrs int siz; 3481163953Srrs 3482163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3483163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3484170091Srrs siz, SCTP_M_STRESET); 3485163953Srrs if (liste == NULL) { 3486163953Srrs /* gak out of memory */ 3487163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3488163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3489163953Srrs return; 3490163953Srrs } 3491163953Srrs liste->tsn = tsn; 3492163953Srrs liste->number_entries = number_entries; 3493163953Srrs memcpy(&liste->req, req, 3494163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3495163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3496163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3497163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3498163953Srrs } 3499163953Srrs asoc->str_reset_seq_in++; 3500163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3501163953Srrs /* 3502163953Srrs * one seq back, just echo back last action since my 3503163953Srrs * response was lost. 3504163953Srrs */ 3505163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3506163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3507163953Srrs /* 3508163953Srrs * two seq back, just echo back last action since my 3509163953Srrs * response was lost. 3510163953Srrs */ 3511163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3512163953Srrs } else { 3513163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3514163953Srrs } 3515163953Srrs} 3516163953Srrs 3517170992Srrs#ifdef __GNUC__ 3518170992Srrs__attribute__((noinline)) 3519170992Srrs#endif 3520170992Srrs static int 3521170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3522170992Srrs struct sctp_stream_reset_out_req *sr_req) 3523163953Srrs{ 3524163953Srrs int chk_length, param_len, ptype; 3525170992Srrs struct sctp_paramhdr pstore; 3526170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3527170992Srrs 3528163953Srrs uint32_t seq; 3529163953Srrs int num_req = 0; 3530170992Srrs int trunc = 0; 3531163953Srrs struct sctp_tmit_chunk *chk; 3532163953Srrs struct sctp_chunkhdr *ch; 3533163953Srrs struct sctp_paramhdr *ph; 3534166675Srrs int ret_code = 0; 3535166675Srrs int num_param = 0; 3536163953Srrs 3537163953Srrs /* now it may be a reset or a reset-response */ 3538163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3539163953Srrs 3540163953Srrs /* setup for adding the response */ 3541163953Srrs sctp_alloc_a_chunk(stcb, chk); 3542163953Srrs if (chk == NULL) { 3543163953Srrs return (ret_code); 3544163953Srrs } 3545163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3546169208Srrs chk->rec.chunk_id.can_take_data = 0; 3547163953Srrs chk->asoc = &stcb->asoc; 3548163953Srrs chk->no_fr_allowed = 0; 3549163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3550166023Srrs chk->book_size_scale = 0; 3551165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3552163953Srrs if (chk->data == NULL) { 3553163953Srrsstrres_nochunk: 3554163953Srrs if (chk->data) { 3555163953Srrs sctp_m_freem(chk->data); 3556163953Srrs chk->data = NULL; 3557163953Srrs } 3558163953Srrs sctp_free_a_chunk(stcb, chk); 3559163953Srrs return (ret_code); 3560163953Srrs } 3561165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3562163953Srrs 3563163953Srrs /* setup chunk parameters */ 3564163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3565163953Srrs chk->snd_count = 0; 3566163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3567163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3568163953Srrs 3569163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3570163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3571163953Srrs ch->chunk_flags = 0; 3572163953Srrs ch->chunk_length = htons(chk->send_size); 3573165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3574170992Srrs offset += sizeof(struct sctp_chunkhdr); 3575163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3576170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3577170992Srrs if (ph == NULL) 3578170992Srrs break; 3579163953Srrs param_len = ntohs(ph->param_length); 3580163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3581163953Srrs /* bad param */ 3582163953Srrs break; 3583163953Srrs } 3584170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3585170992Srrs (uint8_t *) & cstore); 3586163953Srrs ptype = ntohs(ph->param_type); 3587163953Srrs num_param++; 3588170994Srrs if (param_len > (int)sizeof(cstore)) { 3589170992Srrs trunc = 1; 3590170992Srrs } else { 3591170992Srrs trunc = 0; 3592170992Srrs } 3593170992Srrs 3594163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3595163953Srrs /* hit the max of parameters already sorry.. */ 3596163953Srrs break; 3597163953Srrs } 3598163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3599163953Srrs struct sctp_stream_reset_out_request *req_out; 3600163953Srrs 3601163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3602163953Srrs num_req++; 3603163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3604163953Srrs seq = ntohl(req_out->response_seq); 3605163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3606163953Srrs /* implicit ack */ 3607169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3608163953Srrs } 3609163953Srrs } 3610170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3611163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3612163953Srrs struct sctp_stream_reset_in_request *req_in; 3613163953Srrs 3614163953Srrs num_req++; 3615170992Srrs 3616163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3617170992Srrs 3618170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3619163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3620163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3621163953Srrs 3622163953Srrs num_req++; 3623163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3624170992Srrs 3625163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3626163953Srrs ret_code = 1; 3627163953Srrs goto strres_nochunk; 3628163953Srrs } 3629163953Srrs /* no more */ 3630163953Srrs break; 3631163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3632163953Srrs struct sctp_stream_reset_response *resp; 3633163953Srrs uint32_t result; 3634163953Srrs 3635163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3636163953Srrs seq = ntohl(resp->response_seq); 3637163953Srrs result = ntohl(resp->result); 3638163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3639163953Srrs ret_code = 1; 3640163953Srrs goto strres_nochunk; 3641163953Srrs } 3642163953Srrs } else { 3643163953Srrs break; 3644163953Srrs } 3645170992Srrs offset += SCTP_SIZE32(param_len); 3646163953Srrs chk_length -= SCTP_SIZE32(param_len); 3647163953Srrs } 3648163953Srrs if (num_req == 0) { 3649163953Srrs /* we have no response free the stuff */ 3650163953Srrs goto strres_nochunk; 3651163953Srrs } 3652163953Srrs /* ok we have a chunk to link in */ 3653163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3654163953Srrs chk, 3655163953Srrs sctp_next); 3656163953Srrs stcb->asoc.ctrl_queue_cnt++; 3657163953Srrs return (ret_code); 3658163953Srrs} 3659163953Srrs 3660163953Srrs/* 3661163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3662163953Srrs * to handle this, either we get the whole packet and must disect it 3663163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3664163953Srrs * from a middle box that did the disectting for us. 3665163953Srrs */ 3666163953Srrsstatic void 3667163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3668170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3669163953Srrs{ 3670163953Srrs uint32_t bottle_bw, on_queue; 3671163953Srrs uint16_t trunc_len; 3672163953Srrs unsigned int chlen; 3673163953Srrs unsigned int at; 3674163953Srrs struct sctp_chunk_desc desc; 3675163953Srrs struct sctp_chunkhdr *ch; 3676163953Srrs 3677163953Srrs chlen = ntohs(cp->ch.chunk_length); 3678163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3679163953Srrs /* XXX possible chlen underflow */ 3680163953Srrs if (chlen == 0) { 3681163953Srrs ch = NULL; 3682163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3683163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3684163953Srrs } else { 3685163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3686163953Srrs chlen -= sizeof(struct sctphdr); 3687163953Srrs /* XXX possible chlen underflow */ 3688163953Srrs memset(&desc, 0, sizeof(desc)); 3689163953Srrs } 3690163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3691170781Srrs if (trunc_len > limit) { 3692170781Srrs trunc_len = limit; 3693170781Srrs } 3694163953Srrs /* now the chunks themselves */ 3695163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3696163953Srrs desc.chunk_type = ch->chunk_type; 3697163953Srrs /* get amount we need to move */ 3698163953Srrs at = ntohs(ch->chunk_length); 3699163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3700163953Srrs /* corrupt chunk, maybe at the end? */ 3701163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3702163953Srrs break; 3703163953Srrs } 3704163953Srrs if (trunc_len == 0) { 3705163953Srrs /* we are supposed to have all of it */ 3706163953Srrs if (at > chlen) { 3707163953Srrs /* corrupt skip it */ 3708163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3709163953Srrs break; 3710163953Srrs } 3711163953Srrs } else { 3712163953Srrs /* is there enough of it left ? */ 3713163953Srrs if (desc.chunk_type == SCTP_DATA) { 3714163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3715163953Srrs sizeof(desc.data_bytes))) { 3716163953Srrs break; 3717163953Srrs } 3718163953Srrs } else { 3719163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3720163953Srrs break; 3721163953Srrs } 3722163953Srrs } 3723163953Srrs } 3724163953Srrs if (desc.chunk_type == SCTP_DATA) { 3725163953Srrs /* can we get out the tsn? */ 3726163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3727163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3728163953Srrs 3729163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3730163953Srrs /* yep */ 3731163953Srrs struct sctp_data_chunk *dcp; 3732163953Srrs uint8_t *ddp; 3733163953Srrs unsigned int iii; 3734163953Srrs 3735163953Srrs dcp = (struct sctp_data_chunk *)ch; 3736163953Srrs ddp = (uint8_t *) (dcp + 1); 3737163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3738163953Srrs desc.data_bytes[iii] = ddp[iii]; 3739163953Srrs } 3740163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3741163953Srrs } else { 3742163953Srrs /* nope we are done. */ 3743163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3744163953Srrs break; 3745163953Srrs } 3746163953Srrs } else { 3747163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3748163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3749163953Srrs } 3750163953Srrs 3751163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3752163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3753163953Srrs break; 3754163953Srrs } 3755163953Srrs if (SCTP_SIZE32(at) > chlen) { 3756163953Srrs break; 3757163953Srrs } 3758163953Srrs chlen -= SCTP_SIZE32(at); 3759163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3760163953Srrs /* done, none left */ 3761163953Srrs break; 3762163953Srrs } 3763163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3764163953Srrs } 3765163953Srrs /* Now update any rwnd --- possibly */ 3766163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3767163953Srrs /* From a peer, we get a rwnd report */ 3768163953Srrs uint32_t a_rwnd; 3769163953Srrs 3770163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3771163953Srrs 3772163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3773163953Srrs on_queue = ntohl(cp->current_onq); 3774163953Srrs if (bottle_bw && on_queue) { 3775163953Srrs /* a rwnd report is in here */ 3776163953Srrs if (bottle_bw > on_queue) 3777163953Srrs a_rwnd = bottle_bw - on_queue; 3778163953Srrs else 3779163953Srrs a_rwnd = 0; 3780163953Srrs 3781163953Srrs if (a_rwnd == 0) 3782163953Srrs stcb->asoc.peers_rwnd = 0; 3783163953Srrs else { 3784163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3785163953Srrs stcb->asoc.peers_rwnd = 3786163953Srrs a_rwnd - stcb->asoc.total_flight; 3787163953Srrs } else { 3788163953Srrs stcb->asoc.peers_rwnd = 0; 3789163953Srrs } 3790163953Srrs if (stcb->asoc.peers_rwnd < 3791163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3792163953Srrs /* SWS sender side engages */ 3793163953Srrs stcb->asoc.peers_rwnd = 0; 3794163953Srrs } 3795163953Srrs } 3796163953Srrs } 3797163953Srrs } else { 3798163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3799163953Srrs } 3800163953Srrs 3801163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3802163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3803163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3804163953Srrs (stcb->asoc.sat_network)) { 3805163953Srrs /* 3806163953Srrs * This is debateable but for sat networks it makes sense 3807163953Srrs * Note if a T3 timer has went off, we will prohibit any 3808163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3809163953Srrs */ 3810171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 3811171440Srrs net, cp, &bottle_bw, &on_queue); 3812163953Srrs } 3813163953Srrs} 3814163953Srrs 3815163953Srrs/* 3816163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3817163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3818163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3819163953Srrs * length of the complete packet outputs: - length: modified to remaining 3820163953Srrs * length after control processing - netp: modified to new sctp_nets after 3821163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3822163953Srrs * bad packet,...) otherwise return the tcb for this packet 3823163953Srrs */ 3824170099Srrs#ifdef __GNUC__ 3825170099Srrs__attribute__((noinline)) 3826170099Srrs#endif 3827170099Srrs static struct sctp_tcb * 3828170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3829170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3830170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3831179157Srrs uint32_t vrf_id, uint16_t port) 3832163953Srrs{ 3833163953Srrs struct sctp_association *asoc; 3834163953Srrs uint32_t vtag_in; 3835163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3836170140Srrs uint32_t chk_length; 3837163953Srrs int ret; 3838168299Srrs int abort_no_unlock = 0; 3839163953Srrs 3840163953Srrs /* 3841163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3842163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3843163953Srrs * until we get into jumbo grams and such.. 3844163953Srrs */ 3845166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3846163953Srrs struct sctp_tcb *locked_tcb = stcb; 3847163953Srrs int got_auth = 0; 3848163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3849163953Srrs int auth_skipped = 0; 3850171990Srrs int asconf_cnt = 0; 3851163953Srrs 3852172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3853172090Srrs struct socket *so; 3854172090Srrs 3855172090Srrs#endif 3856172090Srrs 3857169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3858169420Srrs iphlen, *offset, length, stcb); 3859163953Srrs 3860163953Srrs /* validate chunk header length... */ 3861163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3862170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3863170056Srrs ntohs(ch->chunk_length)); 3864170091Srrs if (locked_tcb) { 3865170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3866170091Srrs } 3867163953Srrs return (NULL); 3868163953Srrs } 3869163953Srrs /* 3870163953Srrs * validate the verification tag 3871163953Srrs */ 3872163953Srrs vtag_in = ntohl(sh->v_tag); 3873163953Srrs 3874165220Srrs if (locked_tcb) { 3875165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3876165220Srrs } 3877163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3878170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3879170056Srrs ntohs(ch->chunk_length), vtag_in); 3880163953Srrs if (vtag_in != 0) { 3881163953Srrs /* protocol error- silently discard... */ 3882163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3883169378Srrs if (locked_tcb) { 3884163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3885169378Srrs } 3886163953Srrs return (NULL); 3887163953Srrs } 3888163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3889163953Srrs /* 3890163953Srrs * If there is no stcb, skip the AUTH chunk and process 3891163953Srrs * later after a stcb is found (to validate the lookup was 3892163953Srrs * valid. 3893163953Srrs */ 3894163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3895179783Srrs (stcb == NULL) && 3896179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 3897163953Srrs /* save this chunk for later processing */ 3898163953Srrs auth_skipped = 1; 3899163953Srrs auth_offset = *offset; 3900163953Srrs auth_len = ntohs(ch->chunk_length); 3901163953Srrs 3902163953Srrs /* (temporarily) move past this chunk */ 3903163953Srrs *offset += SCTP_SIZE32(auth_len); 3904163953Srrs if (*offset >= length) { 3905163953Srrs /* no more data left in the mbuf chain */ 3906163953Srrs *offset = length; 3907170091Srrs if (locked_tcb) { 3908170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3909170091Srrs } 3910163953Srrs return (NULL); 3911163953Srrs } 3912163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3913163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3914163953Srrs } 3915169420Srrs if (ch == NULL) { 3916169420Srrs /* Help */ 3917169420Srrs *offset = length; 3918170091Srrs if (locked_tcb) { 3919170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3920170091Srrs } 3921169420Srrs return (NULL); 3922169420Srrs } 3923163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3924163953Srrs goto process_control_chunks; 3925163953Srrs } 3926163953Srrs /* 3927163953Srrs * first check if it's an ASCONF with an unknown src addr we 3928163953Srrs * need to look inside to find the association 3929163953Srrs */ 3930163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3931171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 3932171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 3933171990Srrs 3934163953Srrs /* inp's refcount may be reduced */ 3935163953Srrs SCTP_INP_INCR_REF(inp); 3936163953Srrs 3937171990Srrs asconf_offset = *offset; 3938171990Srrs do { 3939171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 3940171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 3941171990Srrs break; 3942171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3943171990Srrs *offset, sh, &inp, netp); 3944171990Srrs if (stcb != NULL) 3945171990Srrs break; 3946171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 3947171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 3948171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3949171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 3950163953Srrs if (stcb == NULL) { 3951163953Srrs /* 3952163953Srrs * reduce inp's refcount if not reduced in 3953163953Srrs * sctp_findassociation_ep_asconf(). 3954163953Srrs */ 3955163953Srrs SCTP_INP_DECR_REF(inp); 3956171990Srrs } else { 3957171990Srrs locked_tcb = stcb; 3958163953Srrs } 3959171990Srrs 3960163953Srrs /* now go back and verify any auth chunk to be sure */ 3961163953Srrs if (auth_skipped && (stcb != NULL)) { 3962163953Srrs struct sctp_auth_chunk *auth; 3963163953Srrs 3964163953Srrs auth = (struct sctp_auth_chunk *) 3965163953Srrs sctp_m_getptr(m, auth_offset, 3966163953Srrs auth_len, chunk_buf); 3967163953Srrs got_auth = 1; 3968163953Srrs auth_skipped = 0; 3969169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3970163953Srrs auth_offset)) { 3971163953Srrs /* auth HMAC failed so dump it */ 3972163953Srrs *offset = length; 3973170091Srrs if (locked_tcb) { 3974170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3975170091Srrs } 3976163953Srrs return (NULL); 3977163953Srrs } else { 3978163953Srrs /* remaining chunks are HMAC checked */ 3979163953Srrs stcb->asoc.authenticated = 1; 3980163953Srrs } 3981163953Srrs } 3982163953Srrs } 3983163953Srrs if (stcb == NULL) { 3984163953Srrs /* no association, so it's out of the blue... */ 3985169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3986179157Srrs vrf_id, port); 3987163953Srrs *offset = length; 3988169378Srrs if (locked_tcb) { 3989163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3990169378Srrs } 3991163953Srrs return (NULL); 3992163953Srrs } 3993163953Srrs asoc = &stcb->asoc; 3994163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3995163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3996163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3997163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3998163953Srrs if ((vtag_in == asoc->my_vtag) || 3999163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4000163953Srrs (vtag_in == asoc->peer_vtag))) { 4001163953Srrs /* this is valid */ 4002163953Srrs } else { 4003163953Srrs /* drop this packet... */ 4004163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4005169378Srrs if (locked_tcb) { 4006163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4007169378Srrs } 4008163953Srrs return (NULL); 4009163953Srrs } 4010163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4011163953Srrs if (vtag_in != asoc->my_vtag) { 4012163953Srrs /* 4013163953Srrs * this could be a stale SHUTDOWN-ACK or the 4014163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4015163953Srrs * is still hung; we have started a new asoc 4016163953Srrs * but it won't complete until the shutdown 4017163953Srrs * is completed 4018163953Srrs */ 4019169378Srrs if (locked_tcb) { 4020163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4021169378Srrs } 4022163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4023179157Srrs NULL, vrf_id, port); 4024163953Srrs return (NULL); 4025163953Srrs } 4026163953Srrs } else { 4027163953Srrs /* for all other chunks, vtag must match */ 4028163953Srrs if (vtag_in != asoc->my_vtag) { 4029163953Srrs /* invalid vtag... */ 4030169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4031169420Srrs "invalid vtag: %xh, expect %xh\n", 4032169420Srrs vtag_in, asoc->my_vtag); 4033163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4034169378Srrs if (locked_tcb) { 4035163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4036169378Srrs } 4037163953Srrs *offset = length; 4038163953Srrs return (NULL); 4039163953Srrs } 4040163953Srrs } 4041163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4042163953Srrs /* 4043163953Srrs * process all control chunks... 4044163953Srrs */ 4045163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4046163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4047163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4048163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4049179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4050171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4051171943Srrs stcb->asoc.overall_error_count, 4052171943Srrs 0, 4053171943Srrs SCTP_FROM_SCTP_INPUT, 4054171943Srrs __LINE__); 4055171943Srrs } 4056163953Srrs stcb->asoc.overall_error_count = 0; 4057163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4058163953Srrs *netp); 4059163953Srrs } 4060163953Srrsprocess_control_chunks: 4061163953Srrs while (IS_SCTP_CONTROL(ch)) { 4062163953Srrs /* validate chunk length */ 4063163953Srrs chk_length = ntohs(ch->chunk_length); 4064169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4065169420Srrs ch->chunk_type, chk_length); 4066170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4067170140Srrs if (chk_length < sizeof(*ch) || 4068170140Srrs (*offset + (int)chk_length) > length) { 4069163953Srrs *offset = length; 4070169378Srrs if (locked_tcb) { 4071163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4072169378Srrs } 4073163953Srrs return (NULL); 4074163953Srrs } 4075163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4076163953Srrs /* 4077163953Srrs * INIT-ACK only gets the init ack "header" portion only 4078163953Srrs * because we don't have to process the peer's COOKIE. All 4079163953Srrs * others get a complete chunk. 4080163953Srrs */ 4081169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4082169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4083163953Srrs /* get an init-ack chunk */ 4084163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4085163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4086163953Srrs if (ch == NULL) { 4087163953Srrs *offset = length; 4088169378Srrs if (locked_tcb) { 4089163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4090169378Srrs } 4091163953Srrs return (NULL); 4092163953Srrs } 4093170642Srrs } else { 4094170786Srrs /* For cookies and all other chunks. */ 4095169208Srrs if (chk_length > sizeof(chunk_buf)) { 4096169208Srrs /* 4097169208Srrs * use just the size of the chunk buffer so 4098170642Srrs * the front part of our chunks fit in 4099170642Srrs * contiguous space up to the chunk buffer 4100170642Srrs * size (508 bytes). For chunks that need to 4101170786Srrs * get more than that they must use the 4102170642Srrs * sctp_m_getptr() function or other means 4103170786Srrs * (e.g. know how to parse mbuf chains). 4104170786Srrs * Cookies do this already. 4105169208Srrs */ 4106169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4107169208Srrs (sizeof(chunk_buf) - 4), 4108169208Srrs chunk_buf); 4109169208Srrs if (ch == NULL) { 4110169208Srrs *offset = length; 4111169378Srrs if (locked_tcb) { 4112169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4113169378Srrs } 4114169208Srrs return (NULL); 4115169208Srrs } 4116169208Srrs } else { 4117169208Srrs /* We can fit it all */ 4118170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4119170642Srrs chk_length, chunk_buf); 4120170642Srrs if (ch == NULL) { 4121170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4122170642Srrs *offset = length; 4123170642Srrs if (locked_tcb) { 4124170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4125169208Srrs } 4126170642Srrs return (NULL); 4127163953Srrs } 4128163953Srrs } 4129163953Srrs } 4130163953Srrs num_chunks++; 4131163953Srrs /* Save off the last place we got a control from */ 4132163953Srrs if (stcb != NULL) { 4133169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4134163953Srrs /* 4135163953Srrs * allow last_control to be NULL if 4136163953Srrs * ASCONF... ASCONF processing will find the 4137163953Srrs * right net later 4138163953Srrs */ 4139169420Srrs if ((netp != NULL) && (*netp != NULL)) 4140169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4141163953Srrs } 4142163953Srrs } 4143163953Srrs#ifdef SCTP_AUDITING_ENABLED 4144163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4145163953Srrs#endif 4146163953Srrs 4147163953Srrs /* check to see if this chunk required auth, but isn't */ 4148179783Srrs if ((stcb != NULL) && 4149179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4150179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4151163953Srrs !stcb->asoc.authenticated) { 4152163953Srrs /* "silently" ignore */ 4153163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4154163953Srrs goto next_chunk; 4155163953Srrs } 4156163953Srrs switch (ch->chunk_type) { 4157163953Srrs case SCTP_INITIATION: 4158163953Srrs /* must be first and only chunk */ 4159169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4160163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4161163953Srrs /* We are not interested anymore? */ 4162163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4163163953Srrs /* 4164163953Srrs * collision case where we are 4165163953Srrs * sending to them too 4166163953Srrs */ 4167163953Srrs ; 4168163953Srrs } else { 4169169378Srrs if (locked_tcb) { 4170163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4171169378Srrs } 4172163953Srrs *offset = length; 4173163953Srrs return (NULL); 4174163953Srrs } 4175163953Srrs } 4176171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4177171990Srrs (num_chunks > 1) || 4178179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4179163953Srrs *offset = length; 4180169378Srrs if (locked_tcb) { 4181163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4182169378Srrs } 4183163953Srrs return (NULL); 4184163953Srrs } 4185163953Srrs if ((stcb != NULL) && 4186163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4187163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4188163953Srrs sctp_send_shutdown_ack(stcb, 4189163953Srrs stcb->asoc.primary_destination); 4190163953Srrs *offset = length; 4191172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4192169378Srrs if (locked_tcb) { 4193163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4194169378Srrs } 4195163953Srrs return (NULL); 4196163953Srrs } 4197169420Srrs if (netp) { 4198169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4199169420Srrs (struct sctp_init_chunk *)ch, inp, 4200179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4201169420Srrs } 4202168299Srrs if (abort_no_unlock) 4203168299Srrs return (NULL); 4204168299Srrs 4205163953Srrs *offset = length; 4206169378Srrs if (locked_tcb) { 4207163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4208169378Srrs } 4209163953Srrs return (NULL); 4210163953Srrs break; 4211170642Srrs case SCTP_PAD_CHUNK: 4212170642Srrs break; 4213163953Srrs case SCTP_INITIATION_ACK: 4214163953Srrs /* must be first and only chunk */ 4215169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4216163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4217163953Srrs /* We are not interested anymore */ 4218163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4219163953Srrs ; 4220163953Srrs } else { 4221169378Srrs if (locked_tcb) { 4222163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4223169378Srrs } 4224163953Srrs *offset = length; 4225163953Srrs if (stcb) { 4226172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4227172090Srrs so = SCTP_INP_SO(inp); 4228172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4229172090Srrs SCTP_TCB_UNLOCK(stcb); 4230172090Srrs SCTP_SOCKET_LOCK(so, 1); 4231172090Srrs SCTP_TCB_LOCK(stcb); 4232172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4233172090Srrs#endif 4234171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4235172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4236172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4237172090Srrs#endif 4238163953Srrs } 4239163953Srrs return (NULL); 4240163953Srrs } 4241163953Srrs } 4242163953Srrs if ((num_chunks > 1) || 4243179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4244163953Srrs *offset = length; 4245169378Srrs if (locked_tcb) { 4246163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4247169378Srrs } 4248163953Srrs return (NULL); 4249163953Srrs } 4250169420Srrs if ((netp) && (*netp)) { 4251169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4252170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4253169420Srrs } else { 4254169420Srrs ret = -1; 4255169420Srrs } 4256163953Srrs /* 4257163953Srrs * Special case, I must call the output routine to 4258163953Srrs * get the cookie echoed 4259163953Srrs */ 4260168299Srrs if (abort_no_unlock) 4261168299Srrs return (NULL); 4262168299Srrs 4263163953Srrs if ((stcb) && ret == 0) 4264172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4265163953Srrs *offset = length; 4266169378Srrs if (locked_tcb) { 4267163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4268169378Srrs } 4269163953Srrs return (NULL); 4270163953Srrs break; 4271163953Srrs case SCTP_SELECTIVE_ACK: 4272169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4273163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4274163953Srrs { 4275163953Srrs struct sctp_sack_chunk *sack; 4276163953Srrs int abort_now = 0; 4277163953Srrs uint32_t a_rwnd, cum_ack; 4278163953Srrs uint16_t num_seg; 4279163953Srrs int nonce_sum_flag; 4280163953Srrs 4281169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 4282169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 4283171990Srrs ignore_sack: 4284169208Srrs *offset = length; 4285169378Srrs if (locked_tcb) { 4286169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4287169378Srrs } 4288169208Srrs return (NULL); 4289169208Srrs } 4290171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4291171990Srrs /*- 4292171990Srrs * If we have sent a shutdown-ack, we will pay no 4293171990Srrs * attention to a sack sent in to us since 4294171990Srrs * we don't care anymore. 4295171990Srrs */ 4296171990Srrs goto ignore_sack; 4297171990Srrs } 4298163953Srrs sack = (struct sctp_sack_chunk *)ch; 4299163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 4300163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4301163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4302163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4303170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4304170606Srrs cum_ack, 4305170606Srrs num_seg, 4306170606Srrs a_rwnd 4307170606Srrs ); 4308163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4309163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4310163953Srrs (num_seg == 0) && 4311163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4312163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4313163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4314163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4315163953Srrs ) { 4316163953Srrs /* 4317163953Srrs * We have a SIMPLE sack having no 4318163953Srrs * prior segments and data on sent 4319163953Srrs * queue to be acked.. Use the 4320163953Srrs * faster path sack processing. We 4321163953Srrs * also allow window update sacks 4322163953Srrs * with no missing segments to go 4323163953Srrs * this way too. 4324163953Srrs */ 4325169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4326169208Srrs &abort_now); 4327163953Srrs } else { 4328169420Srrs if (netp && *netp) 4329170781Srrs sctp_handle_sack(m, *offset, 4330170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 4331163953Srrs } 4332163953Srrs if (abort_now) { 4333163953Srrs /* ABORT signal from sack processing */ 4334163953Srrs *offset = length; 4335163953Srrs return (NULL); 4336163953Srrs } 4337163953Srrs } 4338163953Srrs break; 4339163953Srrs case SCTP_HEARTBEAT_REQUEST: 4340169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4341169420Srrs if ((stcb) && netp && *netp) { 4342169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4343169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4344169420Srrs chk_length, *netp); 4345169420Srrs 4346169420Srrs /* He's alive so give him credit */ 4347179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4348171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4349171943Srrs stcb->asoc.overall_error_count, 4350171943Srrs 0, 4351171943Srrs SCTP_FROM_SCTP_INPUT, 4352171943Srrs __LINE__); 4353171943Srrs } 4354169420Srrs stcb->asoc.overall_error_count = 0; 4355163953Srrs } 4356163953Srrs break; 4357163953Srrs case SCTP_HEARTBEAT_ACK: 4358169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4359169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4360169208Srrs /* Its not ours */ 4361169352Srrs *offset = length; 4362169378Srrs if (locked_tcb) { 4363169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4364169378Srrs } 4365169208Srrs return (NULL); 4366169208Srrs } 4367163953Srrs /* He's alive so give him credit */ 4368179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4369171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4370171943Srrs stcb->asoc.overall_error_count, 4371171943Srrs 0, 4372171943Srrs SCTP_FROM_SCTP_INPUT, 4373171943Srrs __LINE__); 4374171943Srrs } 4375163953Srrs stcb->asoc.overall_error_count = 0; 4376163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4377169420Srrs if (netp && *netp) 4378169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4379169420Srrs stcb, *netp); 4380163953Srrs break; 4381163953Srrs case SCTP_ABORT_ASSOCIATION: 4382170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4383170091Srrs stcb); 4384169420Srrs if ((stcb) && netp && *netp) 4385169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4386169420Srrs stcb, *netp); 4387163953Srrs *offset = length; 4388163953Srrs return (NULL); 4389163953Srrs break; 4390163953Srrs case SCTP_SHUTDOWN: 4391170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4392170091Srrs stcb); 4393169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4394169208Srrs *offset = length; 4395169378Srrs if (locked_tcb) { 4396169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4397169378Srrs } 4398169208Srrs return (NULL); 4399169420Srrs } 4400169420Srrs if (netp && *netp) { 4401163953Srrs int abort_flag = 0; 4402163953Srrs 4403163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4404163953Srrs stcb, *netp, &abort_flag); 4405163953Srrs if (abort_flag) { 4406163953Srrs *offset = length; 4407163953Srrs return (NULL); 4408163953Srrs } 4409163953Srrs } 4410163953Srrs break; 4411163953Srrs case SCTP_SHUTDOWN_ACK: 4412170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4413169420Srrs if ((stcb) && (netp) && (*netp)) 4414169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4415163953Srrs *offset = length; 4416163953Srrs return (NULL); 4417163953Srrs break; 4418169420Srrs 4419163953Srrs case SCTP_OPERATION_ERROR: 4420169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4421169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4422169420Srrs 4423163953Srrs *offset = length; 4424163953Srrs return (NULL); 4425163953Srrs } 4426163953Srrs break; 4427163953Srrs case SCTP_COOKIE_ECHO: 4428169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4429170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4430163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4431163953Srrs ; 4432163953Srrs } else { 4433169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4434163953Srrs /* We are not interested anymore */ 4435163953Srrs *offset = length; 4436163953Srrs return (NULL); 4437163953Srrs } 4438163953Srrs } 4439163953Srrs /* 4440163953Srrs * First are we accepting? We do this again here 4441169420Srrs * sincen it is possible that a previous endpoint 4442169420Srrs * WAS listening responded to a INIT-ACK and then 4443163953Srrs * closed. We opened and bound.. and are now no 4444163953Srrs * longer listening. 4445163953Srrs */ 4446172703Srrs 4447172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4448172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4449179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4450163953Srrs struct mbuf *oper; 4451163953Srrs struct sctp_paramhdr *phdr; 4452163953Srrs 4453172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4454172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4455172703Srrs if (oper) { 4456172703Srrs SCTP_BUF_LEN(oper) = 4457172703Srrs sizeof(struct sctp_paramhdr); 4458172703Srrs phdr = mtod(oper, 4459172703Srrs struct sctp_paramhdr *); 4460172703Srrs phdr->param_type = 4461172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4462172703Srrs phdr->param_length = 4463172703Srrs htons(sizeof(struct sctp_paramhdr)); 4464163953Srrs } 4465172703Srrs sctp_abort_association(inp, stcb, m, 4466179157Srrs iphlen, sh, oper, vrf_id, port); 4467163953Srrs } 4468172703Srrs *offset = length; 4469172703Srrs return (NULL); 4470172703Srrs } else { 4471163953Srrs struct mbuf *ret_buf; 4472165220Srrs struct sctp_inpcb *linp; 4473163953Srrs 4474169420Srrs if (stcb) { 4475165220Srrs linp = NULL; 4476169420Srrs } else { 4477165220Srrs linp = inp; 4478169420Srrs } 4479165220Srrs 4480169420Srrs if (linp) { 4481165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4482169420Srrs } 4483169420Srrs if (netp) { 4484169420Srrs ret_buf = 4485169420Srrs sctp_handle_cookie_echo(m, iphlen, 4486169420Srrs *offset, sh, 4487169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4488169420Srrs &inp, &stcb, netp, 4489169420Srrs auth_skipped, 4490169420Srrs auth_offset, 4491169420Srrs auth_len, 4492169420Srrs &locked_tcb, 4493179157Srrs vrf_id, 4494179157Srrs port); 4495169420Srrs } else { 4496169420Srrs ret_buf = NULL; 4497169420Srrs } 4498169420Srrs if (linp) { 4499165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4500169420Srrs } 4501163953Srrs if (ret_buf == NULL) { 4502163953Srrs if (locked_tcb) { 4503163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4504163953Srrs } 4505169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4506169420Srrs "GAK, null buffer\n"); 4507163953Srrs auth_skipped = 0; 4508163953Srrs *offset = length; 4509163953Srrs return (NULL); 4510163953Srrs } 4511163953Srrs /* if AUTH skipped, see if it verified... */ 4512163953Srrs if (auth_skipped) { 4513163953Srrs got_auth = 1; 4514163953Srrs auth_skipped = 0; 4515163953Srrs } 4516163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4517163953Srrs /* 4518163953Srrs * Restart the timer if we have 4519163953Srrs * pending data 4520163953Srrs */ 4521163953Srrs struct sctp_tmit_chunk *chk; 4522163953Srrs 4523163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4524163953Srrs if (chk) { 4525163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4526163953Srrs stcb->sctp_ep, stcb, 4527163953Srrs chk->whoTo); 4528163953Srrs } 4529163953Srrs } 4530163953Srrs } 4531163953Srrs break; 4532163953Srrs case SCTP_COOKIE_ACK: 4533170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4534169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4535169378Srrs if (locked_tcb) { 4536169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4537169378Srrs } 4538169352Srrs return (NULL); 4539169352Srrs } 4540163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4541163953Srrs /* We are not interested anymore */ 4542163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4543163953Srrs ; 4544169420Srrs } else if (stcb) { 4545172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4546172090Srrs so = SCTP_INP_SO(inp); 4547172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4548172090Srrs SCTP_TCB_UNLOCK(stcb); 4549172090Srrs SCTP_SOCKET_LOCK(so, 1); 4550172090Srrs SCTP_TCB_LOCK(stcb); 4551172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4552172090Srrs#endif 4553171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4554172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4555172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4556172090Srrs#endif 4557163953Srrs *offset = length; 4558163953Srrs return (NULL); 4559163953Srrs } 4560163953Srrs } 4561163953Srrs /* He's alive so give him credit */ 4562169420Srrs if ((stcb) && netp && *netp) { 4563179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4564171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4565171943Srrs stcb->asoc.overall_error_count, 4566171943Srrs 0, 4567171943Srrs SCTP_FROM_SCTP_INPUT, 4568171943Srrs __LINE__); 4569171943Srrs } 4570169378Srrs stcb->asoc.overall_error_count = 0; 4571169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4572169378Srrs } 4573163953Srrs break; 4574163953Srrs case SCTP_ECN_ECHO: 4575169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4576163953Srrs /* He's alive so give him credit */ 4577169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4578169208Srrs /* Its not ours */ 4579169378Srrs if (locked_tcb) { 4580169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4581169378Srrs } 4582169208Srrs *offset = length; 4583169208Srrs return (NULL); 4584169208Srrs } 4585169378Srrs if (stcb) { 4586179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4587171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4588171943Srrs stcb->asoc.overall_error_count, 4589171943Srrs 0, 4590171943Srrs SCTP_FROM_SCTP_INPUT, 4591171943Srrs __LINE__); 4592171943Srrs } 4593169378Srrs stcb->asoc.overall_error_count = 0; 4594169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4595169378Srrs stcb); 4596169378Srrs } 4597163953Srrs break; 4598163953Srrs case SCTP_ECN_CWR: 4599169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4600163953Srrs /* He's alive so give him credit */ 4601169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4602169208Srrs /* Its not ours */ 4603169378Srrs if (locked_tcb) { 4604169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4605169378Srrs } 4606169208Srrs *offset = length; 4607169208Srrs return (NULL); 4608169208Srrs } 4609169378Srrs if (stcb) { 4610179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4611171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4612171943Srrs stcb->asoc.overall_error_count, 4613171943Srrs 0, 4614171943Srrs SCTP_FROM_SCTP_INPUT, 4615171943Srrs __LINE__); 4616171943Srrs } 4617169378Srrs stcb->asoc.overall_error_count = 0; 4618169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4619169378Srrs } 4620163953Srrs break; 4621163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4622170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4623163953Srrs /* must be first and only chunk */ 4624163953Srrs if ((num_chunks > 1) || 4625170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4626163953Srrs *offset = length; 4627169378Srrs if (locked_tcb) { 4628163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4629169378Srrs } 4630163953Srrs return (NULL); 4631163953Srrs } 4632169420Srrs if ((stcb) && netp && *netp) { 4633169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4634169378Srrs stcb, *netp); 4635169378Srrs } 4636163953Srrs *offset = length; 4637163953Srrs return (NULL); 4638163953Srrs break; 4639163953Srrs case SCTP_ASCONF: 4640169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4641163953Srrs /* He's alive so give him credit */ 4642169378Srrs if (stcb) { 4643179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4644171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4645171943Srrs stcb->asoc.overall_error_count, 4646171943Srrs 0, 4647171943Srrs SCTP_FROM_SCTP_INPUT, 4648171943Srrs __LINE__); 4649171943Srrs } 4650169378Srrs stcb->asoc.overall_error_count = 0; 4651169378Srrs sctp_handle_asconf(m, *offset, 4652171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 4653171990Srrs asconf_cnt++; 4654169378Srrs } 4655163953Srrs break; 4656163953Srrs case SCTP_ASCONF_ACK: 4657169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4658169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4659169208Srrs /* Its not ours */ 4660169378Srrs if (locked_tcb) { 4661169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4662169378Srrs } 4663169208Srrs *offset = length; 4664169208Srrs return (NULL); 4665169208Srrs } 4666169420Srrs if ((stcb) && netp && *netp) { 4667169378Srrs /* He's alive so give him credit */ 4668179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4669171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4670171943Srrs stcb->asoc.overall_error_count, 4671171943Srrs 0, 4672171943Srrs SCTP_FROM_SCTP_INPUT, 4673171943Srrs __LINE__); 4674171943Srrs } 4675169378Srrs stcb->asoc.overall_error_count = 0; 4676169378Srrs sctp_handle_asconf_ack(m, *offset, 4677172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 4678172190Srrs if (abort_no_unlock) 4679172190Srrs return (NULL); 4680169378Srrs } 4681163953Srrs break; 4682163953Srrs case SCTP_FORWARD_CUM_TSN: 4683169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4684169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4685169208Srrs /* Its not ours */ 4686169378Srrs if (locked_tcb) { 4687169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4688169378Srrs } 4689169208Srrs *offset = length; 4690169208Srrs return (NULL); 4691169208Srrs } 4692163953Srrs /* He's alive so give him credit */ 4693169378Srrs if (stcb) { 4694163953Srrs int abort_flag = 0; 4695163953Srrs 4696163953Srrs stcb->asoc.overall_error_count = 0; 4697179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4698171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4699171943Srrs stcb->asoc.overall_error_count, 4700171943Srrs 0, 4701171943Srrs SCTP_FROM_SCTP_INPUT, 4702171943Srrs __LINE__); 4703171943Srrs } 4704163953Srrs *fwd_tsn_seen = 1; 4705163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4706163953Srrs /* We are not interested anymore */ 4707172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4708172090Srrs so = SCTP_INP_SO(inp); 4709172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4710172090Srrs SCTP_TCB_UNLOCK(stcb); 4711172090Srrs SCTP_SOCKET_LOCK(so, 1); 4712172090Srrs SCTP_TCB_LOCK(stcb); 4713172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4714172090Srrs#endif 4715171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4716172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4717172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4718172090Srrs#endif 4719163953Srrs *offset = length; 4720163953Srrs return (NULL); 4721163953Srrs } 4722163953Srrs sctp_handle_forward_tsn(stcb, 4723170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 4724163953Srrs if (abort_flag) { 4725163953Srrs *offset = length; 4726163953Srrs return (NULL); 4727163953Srrs } else { 4728179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4729171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4730171943Srrs stcb->asoc.overall_error_count, 4731171943Srrs 0, 4732171943Srrs SCTP_FROM_SCTP_INPUT, 4733171943Srrs __LINE__); 4734171943Srrs } 4735163953Srrs stcb->asoc.overall_error_count = 0; 4736163953Srrs } 4737163953Srrs 4738163953Srrs } 4739163953Srrs break; 4740163953Srrs case SCTP_STREAM_RESET: 4741169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4742169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4743169208Srrs /* Its not ours */ 4744169378Srrs if (locked_tcb) { 4745169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4746169378Srrs } 4747169208Srrs *offset = length; 4748169208Srrs return (NULL); 4749169208Srrs } 4750163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4751163953Srrs /* We are not interested anymore */ 4752172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4753172090Srrs so = SCTP_INP_SO(inp); 4754172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4755172090Srrs SCTP_TCB_UNLOCK(stcb); 4756172090Srrs SCTP_SOCKET_LOCK(so, 1); 4757172090Srrs SCTP_TCB_LOCK(stcb); 4758172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4759172090Srrs#endif 4760171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4761172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4762172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4763172090Srrs#endif 4764163953Srrs *offset = length; 4765163953Srrs return (NULL); 4766163953Srrs } 4767169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4768169420Srrs /* 4769169420Srrs * hmm, peer should have announced this, but 4770169420Srrs * we will turn it on since he is sending us 4771169420Srrs * a stream reset. 4772169420Srrs */ 4773169420Srrs stcb->asoc.peer_supports_strreset = 1; 4774163953Srrs } 4775170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 4776169420Srrs /* stop processing */ 4777169420Srrs *offset = length; 4778169420Srrs return (NULL); 4779169420Srrs } 4780163953Srrs break; 4781163953Srrs case SCTP_PACKET_DROPPED: 4782169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4783163953Srrs /* re-get it all please */ 4784169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4785169208Srrs /* Its not ours */ 4786169378Srrs if (locked_tcb) { 4787169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4788169378Srrs } 4789169208Srrs *offset = length; 4790169208Srrs return (NULL); 4791169208Srrs } 4792169420Srrs if (ch && (stcb) && netp && (*netp)) { 4793169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4794170781Srrs stcb, *netp, 4795170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4796170781Srrs 4797169378Srrs } 4798163953Srrs break; 4799163953Srrs 4800163953Srrs case SCTP_AUTHENTICATION: 4801169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4802179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 4803163953Srrs goto unknown_chunk; 4804163953Srrs 4805163953Srrs if (stcb == NULL) { 4806163953Srrs /* save the first AUTH for later processing */ 4807163953Srrs if (auth_skipped == 0) { 4808163953Srrs auth_offset = *offset; 4809163953Srrs auth_len = chk_length; 4810163953Srrs auth_skipped = 1; 4811163953Srrs } 4812163953Srrs /* skip this chunk (temporarily) */ 4813163953Srrs goto next_chunk; 4814163953Srrs } 4815169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4816169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4817169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4818169208Srrs /* Its not ours */ 4819169378Srrs if (locked_tcb) { 4820169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4821169378Srrs } 4822169208Srrs *offset = length; 4823169208Srrs return (NULL); 4824169208Srrs } 4825163953Srrs if (got_auth == 1) { 4826163953Srrs /* skip this chunk... it's already auth'd */ 4827163953Srrs goto next_chunk; 4828163953Srrs } 4829163953Srrs got_auth = 1; 4830169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4831163953Srrs m, *offset)) { 4832163953Srrs /* auth HMAC failed so dump the packet */ 4833163953Srrs *offset = length; 4834163953Srrs return (stcb); 4835163953Srrs } else { 4836163953Srrs /* remaining chunks are HMAC checked */ 4837163953Srrs stcb->asoc.authenticated = 1; 4838163953Srrs } 4839163953Srrs break; 4840163953Srrs 4841163953Srrs default: 4842163953Srrs unknown_chunk: 4843163953Srrs /* it's an unknown chunk! */ 4844163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4845163953Srrs struct mbuf *mm; 4846163953Srrs struct sctp_paramhdr *phd; 4847163953Srrs 4848163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4849165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4850163953Srrs if (mm) { 4851163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4852163953Srrs /* 4853163953Srrs * We cheat and use param type since 4854163953Srrs * we did not bother to define a 4855163953Srrs * error cause struct. They are the 4856163953Srrs * same basic format with different 4857163953Srrs * names. 4858163953Srrs */ 4859163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4860163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4861165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4862166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4863163953Srrs M_DONTWAIT); 4864165647Srrs if (SCTP_BUF_NEXT(mm)) { 4865178202Srrs#ifdef SCTP_MBUF_LOGGING 4866179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 4867178202Srrs struct mbuf *mat; 4868178202Srrs 4869178202Srrs mat = SCTP_BUF_NEXT(mm); 4870178202Srrs while (mat) { 4871178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4872178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 4873178202Srrs } 4874178202Srrs mat = SCTP_BUF_NEXT(mat); 4875178202Srrs } 4876178202Srrs } 4877178202Srrs#endif 4878163953Srrs sctp_queue_op_err(stcb, mm); 4879163953Srrs } else { 4880163953Srrs sctp_m_freem(mm); 4881163953Srrs } 4882163953Srrs } 4883163953Srrs } 4884163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4885163953Srrs /* discard this packet */ 4886163953Srrs *offset = length; 4887163953Srrs return (stcb); 4888163953Srrs } /* else skip this bad chunk and continue... */ 4889163953Srrs break; 4890163953Srrs } /* switch (ch->chunk_type) */ 4891163953Srrs 4892163953Srrs 4893163953Srrsnext_chunk: 4894163953Srrs /* get the next chunk */ 4895163953Srrs *offset += SCTP_SIZE32(chk_length); 4896163953Srrs if (*offset >= length) { 4897163953Srrs /* no more data left in the mbuf chain */ 4898163953Srrs break; 4899163953Srrs } 4900163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4901163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4902163953Srrs if (ch == NULL) { 4903169378Srrs if (locked_tcb) { 4904163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4905169378Srrs } 4906163953Srrs *offset = length; 4907163953Srrs return (NULL); 4908163953Srrs } 4909163953Srrs } /* while */ 4910171990Srrs 4911171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 4912171990Srrs sctp_send_asconf_ack(stcb); 4913171990Srrs } 4914163953Srrs return (stcb); 4915163953Srrs} 4916163953Srrs 4917163953Srrs 4918163953Srrs/* 4919163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4920163953Srrs * ECN(0) or ECN(1) or CE 4921163953Srrs */ 4922170806Srrsstatic void 4923163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4924163953Srrs uint8_t ecn_bits) 4925163953Srrs{ 4926163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4927163953Srrs ; 4928163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4929163953Srrs /* 4930163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4931163953Srrs * change the NS bit (that we have yet to find a way to send 4932163953Srrs * it yet). 4933163953Srrs */ 4934163953Srrs 4935163953Srrs /* ECN Nonce stuff */ 4936163953Srrs stcb->asoc.receiver_nonce_sum++; 4937163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4938163953Srrs 4939163953Srrs /* 4940163953Srrs * Drag up the last_echo point if cumack is larger since we 4941163953Srrs * don't want the point falling way behind by more than 4942163953Srrs * 2^^31 and then having it be incorrect. 4943163953Srrs */ 4944163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4945163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4946163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4947163953Srrs } 4948163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4949163953Srrs /* 4950163953Srrs * Drag up the last_echo point if cumack is larger since we 4951163953Srrs * don't want the point falling way behind by more than 4952163953Srrs * 2^^31 and then having it be incorrect. 4953163953Srrs */ 4954163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4955163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4956163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4957163953Srrs } 4958163953Srrs } 4959163953Srrs} 4960163953Srrs 4961170806Srrsstatic void 4962163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4963163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4964163953Srrs{ 4965163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4966163953Srrs /* 4967163953Srrs * we possibly must notify the sender that a congestion 4968163953Srrs * window reduction is in order. We do this by adding a ECNE 4969163953Srrs * chunk to the output chunk queue. The incoming CWR will 4970163953Srrs * remove this chunk. 4971163953Srrs */ 4972163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4973163953Srrs MAX_TSN)) { 4974163953Srrs /* Yep, we need to add a ECNE */ 4975163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4976163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4977163953Srrs } 4978163953Srrs } 4979163953Srrs} 4980163953Srrs 4981171440Srrs#ifdef INVARIANTS 4982171440Srrsstatic void 4983171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 4984171440Srrs{ 4985171440Srrs struct sctp_tcb *stcb; 4986171440Srrs 4987171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4988171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 4989171440Srrs panic("Own lock on stcb at return from input"); 4990171440Srrs } 4991171440Srrs } 4992171440Srrs} 4993171440Srrs 4994171440Srrs#endif 4995171440Srrs 4996163953Srrs/* 4997163953Srrs * common input chunk processing (v4 and v6) 4998163953Srrs */ 4999169378Srrsvoid 5000163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5001163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5002163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5003179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5004163953Srrs{ 5005163953Srrs /* 5006163953Srrs * Control chunk processing 5007163953Srrs */ 5008163953Srrs uint32_t high_tsn; 5009163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5010163953Srrs struct mbuf *m = *mm; 5011163953Srrs int abort_flag = 0; 5012163953Srrs int un_sent; 5013163953Srrs 5014163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5015163953Srrs#ifdef SCTP_AUDITING_ENABLED 5016163953Srrs sctp_audit_log(0xE0, 1); 5017163953Srrs sctp_auditing(0, inp, stcb, net); 5018163953Srrs#endif 5019163953Srrs 5020179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5021179783Srrs m, iphlen, offset, length, stcb); 5022163953Srrs if (stcb) { 5023163953Srrs /* always clear this before beginning a packet */ 5024163953Srrs stcb->asoc.authenticated = 0; 5025163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5026171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5027171990Srrs stcb, stcb->asoc.state); 5028171990Srrs 5029171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5030171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5031171745Srrs /*- 5032171745Srrs * If we hit here, we had a ref count 5033171745Srrs * up when the assoc was aborted and the 5034171745Srrs * timer is clearing out the assoc, we should 5035171745Srrs * NOT respond to any packet.. its OOTB. 5036171745Srrs */ 5037171745Srrs SCTP_TCB_UNLOCK(stcb); 5038171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5039179157Srrs vrf_id, port); 5040171745Srrs goto out_now; 5041171745Srrs } 5042163953Srrs } 5043163953Srrs if (IS_SCTP_CONTROL(ch)) { 5044163953Srrs /* process the control portion of the SCTP packet */ 5045169655Srrs /* sa_ignore NO_NULL_CHK */ 5046163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5047179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5048163953Srrs if (stcb) { 5049163953Srrs /* 5050163953Srrs * This covers us if the cookie-echo was there and 5051163953Srrs * it changes our INP. 5052163953Srrs */ 5053163953Srrs inp = stcb->sctp_ep; 5054179783Srrs if ((net) && (port)) { 5055179783Srrs if (net->port == 0) { 5056179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5057179783Srrs } 5058179783Srrs net->port = port; 5059179783Srrs } 5060163953Srrs } 5061163953Srrs } else { 5062163953Srrs /* 5063163953Srrs * no control chunks, so pre-process DATA chunks (these 5064163953Srrs * checks are taken care of by control processing) 5065163953Srrs */ 5066163953Srrs 5067163953Srrs /* 5068163953Srrs * if DATA only packet, and auth is required, then punt... 5069163953Srrs * can't have authenticated without any AUTH (control) 5070163953Srrs * chunks 5071163953Srrs */ 5072179783Srrs if ((stcb != NULL) && 5073179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5074179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5075163953Srrs /* "silently" ignore */ 5076163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5077163953Srrs SCTP_TCB_UNLOCK(stcb); 5078171440Srrs goto out_now; 5079163953Srrs } 5080163953Srrs if (stcb == NULL) { 5081163953Srrs /* out of the blue DATA chunk */ 5082169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5083179157Srrs vrf_id, port); 5084171440Srrs goto out_now; 5085163953Srrs } 5086163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5087163953Srrs /* v_tag mismatch! */ 5088163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5089163953Srrs SCTP_TCB_UNLOCK(stcb); 5090171440Srrs goto out_now; 5091163953Srrs } 5092163953Srrs } 5093163953Srrs 5094163953Srrs if (stcb == NULL) { 5095163953Srrs /* 5096163953Srrs * no valid TCB for this packet, or we found it's a bad 5097163953Srrs * packet while processing control, or we're done with this 5098163953Srrs * packet (done or skip rest of data), so we drop it... 5099163953Srrs */ 5100171440Srrs goto out_now; 5101163953Srrs } 5102163953Srrs /* 5103163953Srrs * DATA chunk processing 5104163953Srrs */ 5105163953Srrs /* plow through the data chunks while length > offset */ 5106163953Srrs 5107163953Srrs /* 5108163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5109163953Srrs * DATA is required. 5110163953Srrs */ 5111179783Srrs if ((length > offset) && 5112179783Srrs (stcb != NULL) && 5113179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5114179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5115163953Srrs !stcb->asoc.authenticated) { 5116163953Srrs /* "silently" ignore */ 5117163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5118169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5119169420Srrs "Data chunk requires AUTH, skipped\n"); 5120165220Srrs goto trigger_send; 5121163953Srrs } 5122163953Srrs if (length > offset) { 5123163953Srrs int retval; 5124163953Srrs 5125163953Srrs /* 5126163953Srrs * First check to make sure our state is correct. We would 5127163953Srrs * not get here unless we really did have a tag, so we don't 5128163953Srrs * abort if this happens, just dump the chunk silently. 5129163953Srrs */ 5130163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5131163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5132163953Srrs /* 5133163953Srrs * we consider data with valid tags in this state 5134163953Srrs * shows us the cookie-ack was lost. Imply it was 5135163953Srrs * there. 5136163953Srrs */ 5137179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5138171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5139171943Srrs stcb->asoc.overall_error_count, 5140171943Srrs 0, 5141171943Srrs SCTP_FROM_SCTP_INPUT, 5142171943Srrs __LINE__); 5143171943Srrs } 5144163953Srrs stcb->asoc.overall_error_count = 0; 5145163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5146163953Srrs break; 5147163953Srrs case SCTP_STATE_COOKIE_WAIT: 5148163953Srrs /* 5149163953Srrs * We consider OOTB any data sent during asoc setup. 5150163953Srrs */ 5151169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5152179157Srrs vrf_id, port); 5153163953Srrs SCTP_TCB_UNLOCK(stcb); 5154171440Srrs goto out_now; 5155171531Srrs /* sa_ignore NOTREACHED */ 5156163953Srrs break; 5157163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5158163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5159163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5160163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5161163953Srrs default: 5162163953Srrs SCTP_TCB_UNLOCK(stcb); 5163171440Srrs goto out_now; 5164171531Srrs /* sa_ignore NOTREACHED */ 5165163953Srrs break; 5166163953Srrs case SCTP_STATE_OPEN: 5167163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5168163953Srrs break; 5169163953Srrs } 5170163953Srrs /* take care of ECN, part 1. */ 5171163953Srrs if (stcb->asoc.ecn_allowed && 5172163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5173163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5174163953Srrs } 5175163953Srrs /* plow through the data chunks while length > offset */ 5176163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5177163953Srrs inp, stcb, net, &high_tsn); 5178163953Srrs if (retval == 2) { 5179163953Srrs /* 5180163953Srrs * The association aborted, NO UNLOCK needed since 5181163953Srrs * the association is destroyed. 5182163953Srrs */ 5183171440Srrs goto out_now; 5184163953Srrs } 5185163953Srrs data_processed = 1; 5186163953Srrs if (retval == 0) { 5187163953Srrs /* take care of ecn part 2. */ 5188163953Srrs if (stcb->asoc.ecn_allowed && 5189163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5190163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5191163953Srrs ecn_bits); 5192163953Srrs } 5193163953Srrs } 5194163953Srrs /* 5195163953Srrs * Anything important needs to have been m_copy'ed in 5196163953Srrs * process_data 5197163953Srrs */ 5198163953Srrs } 5199163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5200163953Srrs int was_a_gap = 0; 5201163953Srrs 5202163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5203163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5204163953Srrs /* there was a gap before this data was processed */ 5205163953Srrs was_a_gap = 1; 5206163953Srrs } 5207163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 5208163953Srrs if (abort_flag) { 5209163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5210171440Srrs goto out_now; 5211163953Srrs } 5212163953Srrs } 5213163953Srrs /* trigger send of any chunks in queue... */ 5214165220Srrstrigger_send: 5215163953Srrs#ifdef SCTP_AUDITING_ENABLED 5216163953Srrs sctp_audit_log(0xE0, 2); 5217163953Srrs sctp_auditing(1, inp, stcb, net); 5218163953Srrs#endif 5219169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5220169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5221169420Srrs stcb->asoc.peers_rwnd, 5222169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5223169420Srrs stcb->asoc.total_flight); 5224163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5225163953Srrs 5226163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5227163953Srrs ((un_sent) && 5228163953Srrs (stcb->asoc.peers_rwnd > 0 || 5229163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5230169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5231172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5232169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5233163953Srrs } 5234163953Srrs#ifdef SCTP_AUDITING_ENABLED 5235163953Srrs sctp_audit_log(0xE0, 3); 5236163953Srrs sctp_auditing(2, inp, stcb, net); 5237163953Srrs#endif 5238163953Srrs SCTP_TCB_UNLOCK(stcb); 5239171440Srrsout_now: 5240171440Srrs#ifdef INVARIANTS 5241171440Srrs sctp_validate_no_locks(inp); 5242171440Srrs#endif 5243169378Srrs return; 5244163953Srrs} 5245163953Srrs 5246163953Srrs 5247163953Srrsvoid 5248179157Srrssctp_input_with_port(i_pak, off, port) 5249165647Srrs struct mbuf *i_pak; 5250163953Srrs int off; 5251179157Srrs uint16_t port; 5252163953Srrs{ 5253163953Srrs#ifdef SCTP_MBUF_LOGGING 5254163953Srrs struct mbuf *mat; 5255163953Srrs 5256163953Srrs#endif 5257165647Srrs struct mbuf *m; 5258163953Srrs int iphlen; 5259170181Srrs uint32_t vrf_id = 0; 5260163953Srrs uint8_t ecn_bits; 5261163953Srrs struct ip *ip; 5262163953Srrs struct sctphdr *sh; 5263163953Srrs struct sctp_inpcb *inp = NULL; 5264163953Srrs 5265163953Srrs uint32_t check, calc_check; 5266163953Srrs struct sctp_nets *net; 5267163953Srrs struct sctp_tcb *stcb = NULL; 5268163953Srrs struct sctp_chunkhdr *ch; 5269163953Srrs int refcount_up = 0; 5270163953Srrs int length, mlen, offset; 5271163953Srrs 5272169352Srrs 5273169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5274169352Srrs SCTP_RELEASE_PKT(i_pak); 5275169352Srrs return; 5276169352Srrs } 5277168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5278163953Srrs iphlen = off; 5279165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5280170056Srrs 5281163953Srrs net = NULL; 5282163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5283163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5284163953Srrs 5285170091Srrs 5286163953Srrs#ifdef SCTP_MBUF_LOGGING 5287163953Srrs /* Log in any input mbufs */ 5288179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5289170744Srrs mat = m; 5290170744Srrs while (mat) { 5291170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5292170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5293170744Srrs } 5294170744Srrs mat = SCTP_BUF_NEXT(mat); 5295163953Srrs } 5296163953Srrs } 5297163953Srrs#endif 5298170091Srrs#ifdef SCTP_PACKET_LOGGING 5299179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5300170921Srrs sctp_packet_log(m, mlen); 5301170091Srrs#endif 5302170091Srrs /* 5303170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5304170091Srrs * case) 5305170091Srrs */ 5306170091Srrs mlen -= iphlen; 5307168124Srrs 5308163953Srrs /* 5309163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5310163953Srrs */ 5311163953Srrs ip = mtod(m, struct ip *); 5312163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5313165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5314163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5315163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5316163953Srrs return; 5317163953Srrs } 5318163953Srrs ip = mtod(m, struct ip *); 5319163953Srrs } 5320163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5321163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5322170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5323170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5324163953Srrs 5325163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5326163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5327163953Srrs goto bad; 5328163953Srrs } 5329168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5330163953Srrs /* 5331163953Srrs * We only look at broadcast if its a front state, All 5332163953Srrs * others we will not have a tcb for anyway. 5333163953Srrs */ 5334163953Srrs goto bad; 5335163953Srrs } 5336163953Srrs /* validate SCTP checksum */ 5337171990Srrs check = sh->checksum; /* save incoming checksum */ 5338179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 5339171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5340171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5341171990Srrs ) { 5342171990Srrs goto sctp_skip_csum_4; 5343171990Srrs } 5344171990Srrs sh->checksum = 0; /* prepare for calc */ 5345171990Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 5346171990Srrs if (calc_check != check) { 5347171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5348171990Srrs calc_check, check, m, mlen, iphlen); 5349163953Srrs 5350171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5351171990Srrs offset - sizeof(*ch), 5352171990Srrs sh, ch, &inp, &net, 5353171990Srrs vrf_id); 5354179157Srrs if ((net) && (port)) { 5355179157Srrs if (net->port == 0) { 5356179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5357179157Srrs } 5358179157Srrs net->port = port; 5359179157Srrs } 5360171990Srrs if ((inp) && (stcb)) { 5361171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5362172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5363171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5364171990Srrs refcount_up = 1; 5365163953Srrs } 5366171990Srrs SCTP_STAT_INCR(sctps_badsum); 5367171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5368171990Srrs goto bad; 5369168299Srrs } 5370171990Srrs sh->checksum = calc_check; 5371163953Srrssctp_skip_csum_4: 5372168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5373168124Srrs if (sh->dest_port == 0) { 5374168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5375168124Srrs goto bad; 5376168124Srrs } 5377163953Srrs /* validate mbuf chain length with IP payload length */ 5378180387Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5379163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5380163953Srrs goto bad; 5381163953Srrs } 5382163953Srrs /* 5383163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5384163953Srrs * IP/SCTP/first chunk header... 5385163953Srrs */ 5386163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5387168299Srrs sh, ch, &inp, &net, vrf_id); 5388179157Srrs if ((net) && (port)) { 5389179157Srrs if (net->port == 0) { 5390179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5391179157Srrs } 5392179157Srrs net->port = port; 5393179157Srrs } 5394163953Srrs /* inp's ref-count increased && stcb locked */ 5395163953Srrs if (inp == NULL) { 5396163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5397163953Srrs 5398163953Srrs SCTP_STAT_INCR(sctps_noport); 5399163953Srrs#ifdef ICMP_BANDLIM 5400163953Srrs /* 5401163953Srrs * we use the bandwidth limiting to protect against sending 5402163953Srrs * too many ABORTS all at once. In this case these count the 5403163953Srrs * same as an ICMP message. 5404163953Srrs */ 5405163953Srrs if (badport_bandlim(0) < 0) 5406163953Srrs goto bad; 5407163953Srrs#endif /* ICMP_BANDLIM */ 5408169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5409169420Srrs "Sending a ABORT from packet entry!\n"); 5410163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5411163953Srrs /* 5412163953Srrs * we do a trick here to get the INIT tag, dig in 5413163953Srrs * and get the tag from the INIT and put it in the 5414163953Srrs * common header. 5415163953Srrs */ 5416163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5417163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5418163953Srrs (uint8_t *) & chunk_buf); 5419163953Srrs if (init_chk != NULL) 5420163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5421163953Srrs } 5422165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5423179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5424165220Srrs goto bad; 5425165220Srrs } 5426165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5427165220Srrs goto bad; 5428165220Srrs } 5429165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5430179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5431163953Srrs goto bad; 5432163953Srrs } else if (stcb == NULL) { 5433163953Srrs refcount_up = 1; 5434163953Srrs } 5435171167Sgnn#ifdef IPSEC 5436163953Srrs /* 5437163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5438163953Srrs * idea, so I will leave it in place. 5439163953Srrs */ 5440163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5441180387Srrs MODULE_GLOBAL(MOD_IPSEC, ipsec4stat).in_polvio++; 5442163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5443163953Srrs goto bad; 5444163953Srrs } 5445163953Srrs#endif /* IPSEC */ 5446163953Srrs 5447163953Srrs /* 5448163953Srrs * common chunk processing 5449163953Srrs */ 5450163953Srrs length = ip->ip_len + iphlen; 5451163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5452163953Srrs 5453163953Srrs ecn_bits = ip->ip_tos; 5454163953Srrs 5455169655Srrs /* sa_ignore NO_NULL_CHK */ 5456163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5457179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5458163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5459163953Srrs if (m) { 5460163953Srrs sctp_m_freem(m); 5461163953Srrs } 5462163953Srrs if ((inp) && (refcount_up)) { 5463163953Srrs /* reduce ref-count */ 5464163953Srrs SCTP_INP_DECR_REF(inp); 5465163953Srrs } 5466163953Srrs return; 5467163953Srrsbad: 5468169420Srrs if (stcb) { 5469163953Srrs SCTP_TCB_UNLOCK(stcb); 5470169420Srrs } 5471163953Srrs if ((inp) && (refcount_up)) { 5472163953Srrs /* reduce ref-count */ 5473163953Srrs SCTP_INP_DECR_REF(inp); 5474163953Srrs } 5475163953Srrs if (m) { 5476163953Srrs sctp_m_freem(m); 5477163953Srrs } 5478163953Srrs return; 5479163953Srrs} 5480179157Srrsvoid 5481179157Srrssctp_input(i_pak, off) 5482179157Srrs struct mbuf *i_pak; 5483179157Srrs int off; 5484179157Srrs{ 5485179157Srrs sctp_input_with_port(i_pak, off, 0); 5486179157Srrs} 5487