sctp_input.c revision 172703
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 172703 2007-10-16 14:05:51Z rrs $"); 35163953Srrs 36163953Srrs#include <netinet/sctp_os.h> 37163953Srrs#include <netinet/sctp_var.h> 38167598Srrs#include <netinet/sctp_sysctl.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctputil.h> 42163953Srrs#include <netinet/sctp_output.h> 43163953Srrs#include <netinet/sctp_input.h> 44163953Srrs#include <netinet/sctp_auth.h> 45163953Srrs#include <netinet/sctp_indata.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47170091Srrs#include <netinet/sctp_bsd_addr.h> 48172091Srrs#include <netinet/sctp_timer.h> 49163953Srrs 50163953Srrs 51163953Srrs 52163953Srrsstatic void 53163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 54163953Srrs{ 55163953Srrs struct sctp_nets *net; 56163953Srrs 57165220Srrs /* 58165220Srrs * This now not only stops all cookie timers it also stops any INIT 59165220Srrs * timers as well. This will make sure that the timers are stopped 60165220Srrs * in all collision cases. 61165220Srrs */ 62163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 63163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 64165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 65163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 66163953Srrs stcb->sctp_ep, 67163953Srrs stcb, 68165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 69165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 70165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 71165220Srrs stcb->sctp_ep, 72165220Srrs stcb, 73165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 74163953Srrs } 75163953Srrs } 76163953Srrs} 77163953Srrs 78163953Srrs/* INIT handler */ 79163953Srrsstatic void 80163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 81163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 82170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 83163953Srrs{ 84163953Srrs struct sctp_init *init; 85163953Srrs struct mbuf *op_err; 86163953Srrs uint32_t init_limit; 87163953Srrs 88169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 89169420Srrs stcb); 90172396Srrs if (stcb == NULL) { 91172396Srrs SCTP_INP_RLOCK(inp); 92172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 93172396Srrs goto outnow; 94172396Srrs } 95172396Srrs } 96163953Srrs op_err = NULL; 97163953Srrs init = &cp->init; 98163953Srrs /* First are we accepting? */ 99163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 100169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 101169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 102169420Srrs inp->sctp_socket->so_qlimit); 103163953Srrs /* 104163953Srrs * FIX ME ?? What about TCP model and we have a 105163953Srrs * match/restart case? 106163953Srrs */ 107169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 108170181Srrs vrf_id); 109168299Srrs if (stcb) 110168299Srrs *abort_no_unlock = 1; 111172396Srrs goto outnow; 112163953Srrs } 113163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 114163953Srrs /* Invalid length */ 115163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 116169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 117170181Srrs vrf_id); 118168299Srrs if (stcb) 119168299Srrs *abort_no_unlock = 1; 120172396Srrs goto outnow; 121163953Srrs } 122163953Srrs /* validate parameters */ 123163953Srrs if (init->initiate_tag == 0) { 124163953Srrs /* protocol error... send abort */ 125163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 126169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 127170181Srrs vrf_id); 128168299Srrs if (stcb) 129168299Srrs *abort_no_unlock = 1; 130172396Srrs goto outnow; 131163953Srrs } 132163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 133163953Srrs /* invalid parameter... send abort */ 134163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 135169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 136170181Srrs vrf_id); 137171440Srrs if (stcb) 138171440Srrs *abort_no_unlock = 1; 139172396Srrs goto outnow; 140163953Srrs } 141163953Srrs if (init->num_inbound_streams == 0) { 142163953Srrs /* protocol error... send abort */ 143163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 144169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 145170181Srrs vrf_id); 146168299Srrs if (stcb) 147168299Srrs *abort_no_unlock = 1; 148172396Srrs goto outnow; 149163953Srrs } 150163953Srrs if (init->num_outbound_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, 154170181Srrs vrf_id); 155168299Srrs if (stcb) 156168299Srrs *abort_no_unlock = 1; 157172396Srrs goto outnow; 158163953Srrs } 159163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 160163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 161163953Srrs init_limit)) { 162163953Srrs /* auth parameter(s) error... send abort */ 163170181Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id); 164168299Srrs if (stcb) 165168299Srrs *abort_no_unlock = 1; 166172396Srrs goto outnow; 167163953Srrs } 168163953Srrs /* send an INIT-ACK w/cookie */ 169169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 170172396Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, 171172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 172172396Srrsoutnow: 173172396Srrs if (stcb == NULL) { 174172396Srrs SCTP_INP_RUNLOCK(inp); 175172396Srrs } 176163953Srrs} 177163953Srrs 178163953Srrs/* 179163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 180163953Srrs */ 181171158Srrs 182171158Srrsint 183171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 184171158Srrs{ 185171158Srrs int unsent_data = 0; 186171158Srrs struct sctp_stream_queue_pending *sp; 187171158Srrs struct sctp_stream_out *strq; 188171158Srrs struct sctp_association *asoc; 189171158Srrs 190171158Srrs /* 191171158Srrs * This function returns the number of streams that have true unsent 192171158Srrs * data on them. Note that as it looks through it will clean up any 193171158Srrs * places that have old data that has been sent but left at top of 194171158Srrs * stream queue. 195171158Srrs */ 196171158Srrs asoc = &stcb->asoc; 197171158Srrs SCTP_TCB_SEND_LOCK(stcb); 198171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 199171158Srrs /* Check to see if some data queued */ 200171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 201171531Srrs is_there_another: 202171158Srrs /* sa_ignore FREED_MEMORY */ 203171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 204171158Srrs if (sp == NULL) { 205171158Srrs continue; 206171158Srrs } 207171158Srrs if ((sp->msg_is_complete) && 208171158Srrs (sp->length == 0) && 209171158Srrs (sp->sender_all_done)) { 210171158Srrs /* 211171158Srrs * We are doing differed cleanup. Last time 212171158Srrs * through when we took all the data the 213171158Srrs * sender_all_done was not set. 214171158Srrs */ 215171158Srrs if (sp->put_last_out == 0) { 216171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 217171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 218171158Srrs sp->sender_all_done, 219171158Srrs sp->length, 220171158Srrs sp->msg_is_complete, 221171158Srrs sp->put_last_out); 222171158Srrs } 223171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 224171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 225171158Srrs sctp_free_remote_addr(sp->net); 226171158Srrs if (sp->data) { 227171158Srrs sctp_m_freem(sp->data); 228171158Srrs sp->data = NULL; 229171158Srrs } 230171158Srrs sctp_free_a_strmoq(stcb, sp); 231171158Srrs goto is_there_another; 232171158Srrs } else { 233171158Srrs unsent_data++; 234171158Srrs continue; 235171158Srrs } 236171158Srrs } 237171158Srrs } 238171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 239171158Srrs return (unsent_data); 240171158Srrs} 241171158Srrs 242163953Srrsstatic int 243163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 244163953Srrs struct sctp_nets *net) 245163953Srrs{ 246163953Srrs struct sctp_init *init; 247163953Srrs struct sctp_association *asoc; 248163953Srrs struct sctp_nets *lnet; 249163953Srrs unsigned int i; 250163953Srrs 251163953Srrs init = &cp->init; 252163953Srrs asoc = &stcb->asoc; 253163953Srrs /* save off parameters */ 254163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 255163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 256163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 257163953Srrs /* update any ssthresh's that may have a default */ 258163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 259163953Srrs lnet->ssthresh = asoc->peers_rwnd; 260163953Srrs 261170744Srrs if (sctp_logging_level & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 262170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 263170744Srrs } 264163953Srrs } 265163953Srrs } 266164181Srrs SCTP_TCB_SEND_LOCK(stcb); 267163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 268163953Srrs unsigned int newcnt; 269163953Srrs struct sctp_stream_out *outs; 270163953Srrs struct sctp_stream_queue_pending *sp; 271163953Srrs 272163953Srrs /* cut back on number of streams */ 273163953Srrs newcnt = ntohs(init->num_inbound_streams); 274163953Srrs /* This if is probably not needed but I am cautious */ 275163953Srrs if (asoc->strmout) { 276163953Srrs /* First make sure no data chunks are trapped */ 277163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 278163953Srrs outs = &asoc->strmout[i]; 279163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 280163953Srrs while (sp) { 281163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 282163953Srrs next); 283163953Srrs asoc->stream_queue_cnt--; 284163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 285163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 286172090Srrs sp, SCTP_SO_NOT_LOCKED); 287163953Srrs if (sp->data) { 288163953Srrs sctp_m_freem(sp->data); 289163953Srrs sp->data = NULL; 290163953Srrs } 291163953Srrs sctp_free_remote_addr(sp->net); 292163953Srrs sp->net = NULL; 293163953Srrs /* Free the chunk */ 294169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 295163979Sru sp, stcb); 296163953Srrs 297163953Srrs sctp_free_a_strmoq(stcb, sp); 298169655Srrs /* sa_ignore FREED_MEMORY */ 299163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 300163953Srrs } 301163953Srrs } 302163953Srrs } 303163953Srrs /* cut back the count and abandon the upper streams */ 304163953Srrs asoc->pre_open_streams = newcnt; 305163953Srrs } 306164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 307163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 308163953Srrs /* init tsn's */ 309163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 310170744Srrs if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) { 311170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 312170744Srrs } 313163953Srrs /* This is the next one we expect */ 314163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 315163953Srrs 316163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 317163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 318163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 319163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 320163953Srrs /* open the requested streams */ 321170091Srrs 322163953Srrs if (asoc->strmin != NULL) { 323163953Srrs /* Free the old ones */ 324164181Srrs struct sctp_queued_to_read *ctl; 325164181Srrs 326164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 327164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 328164181Srrs while (ctl) { 329164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 330164181Srrs sctp_free_remote_addr(ctl->whoFrom); 331171158Srrs ctl->whoFrom = NULL; 332164181Srrs sctp_m_freem(ctl->data); 333164181Srrs ctl->data = NULL; 334164181Srrs sctp_free_a_readq(stcb, ctl); 335164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 336164181Srrs } 337164181Srrs } 338170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 339163953Srrs } 340164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 341164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 342164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 343164181Srrs } 344163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 345170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 346163953Srrs if (asoc->strmin == NULL) { 347163953Srrs /* we didn't get memory for the streams! */ 348169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 349163953Srrs return (-1); 350163953Srrs } 351163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 352163953Srrs asoc->strmin[i].stream_no = i; 353163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 354163953Srrs /* 355163953Srrs * U-stream ranges will be set when the cookie is unpacked. 356163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 357163953Srrs * supported) when the INIT-ACK arrives. 358163953Srrs */ 359163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 360168943Srrs asoc->strmin[i].delivery_started = 0; 361163953Srrs } 362163953Srrs /* 363163953Srrs * load_address_from_init will put the addresses into the 364163953Srrs * association when the COOKIE is processed or the INIT-ACK is 365163953Srrs * processed. Both types of COOKIE's existing and new call this 366163953Srrs * routine. It will remove addresses that are no longer in the 367163953Srrs * association (for the restarting case where addresses are 368163953Srrs * removed). Up front when the INIT arrives we will discard it if it 369163953Srrs * is a restart and new addresses have been added. 370163953Srrs */ 371169655Srrs /* sa_ignore MEMLEAK */ 372163953Srrs return (0); 373163953Srrs} 374163953Srrs 375163953Srrs/* 376163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 377163953Srrs */ 378163953Srrsstatic int 379163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 380163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 381170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 382163953Srrs{ 383163953Srrs struct sctp_association *asoc; 384163953Srrs struct mbuf *op_err; 385163953Srrs int retval, abort_flag; 386163953Srrs uint32_t initack_limit; 387163953Srrs 388163953Srrs /* First verify that we have no illegal param's */ 389163953Srrs abort_flag = 0; 390163953Srrs op_err = NULL; 391163953Srrs 392163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 393163953Srrs (offset + sizeof(struct sctp_init_chunk)), 394163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 395163953Srrs if (abort_flag) { 396163953Srrs /* Send an abort and notify peer */ 397172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 398170056Srrs *abort_no_unlock = 1; 399163953Srrs return (-1); 400163953Srrs } 401163953Srrs asoc = &stcb->asoc; 402163953Srrs /* process the peer's parameters in the INIT-ACK */ 403163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 404163953Srrs if (retval < 0) { 405163953Srrs return (retval); 406163953Srrs } 407163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 408163953Srrs /* load all addresses */ 409163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 410163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 411163953Srrs NULL))) { 412163953Srrs /* Huh, we should abort */ 413169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 414169420Srrs "Load addresses from INIT causes an abort %d\n", 415169420Srrs retval); 416163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 417170181Srrs NULL, 0); 418168299Srrs *abort_no_unlock = 1; 419163953Srrs return (-1); 420163953Srrs } 421171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 422171477Srrs if (asoc->peer_supports_asconf == 0) { 423171477Srrs struct sctp_asconf_addr *aparam; 424171477Srrs 425171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 426171477Srrs /* sa_ignore FREED_MEMORY */ 427171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 428171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 429171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 430171477Srrs } 431171477Srrs } 432163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 433163953Srrs stcb->asoc.local_hmacs); 434163953Srrs if (op_err) { 435163953Srrs sctp_queue_op_err(stcb, op_err); 436163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 437163953Srrs op_err = NULL; 438163953Srrs } 439163953Srrs /* extract the cookie and queue it to "echo" it back... */ 440171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 441171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 442171943Srrs stcb->asoc.overall_error_count, 443171943Srrs 0, 444171943Srrs SCTP_FROM_SCTP_INPUT, 445171943Srrs __LINE__); 446171943Srrs } 447163953Srrs stcb->asoc.overall_error_count = 0; 448163953Srrs net->error_count = 0; 449163953Srrs 450163953Srrs /* 451163953Srrs * Cancel the INIT timer, We do this first before queueing the 452163953Srrs * cookie. We always cancel at the primary to assue that we are 453163953Srrs * canceling the timer started by the INIT which always goes to the 454163953Srrs * primary. 455163953Srrs */ 456163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 457165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 458163953Srrs 459165220Srrs /* calculate the RTO */ 460171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 461165220Srrs 462163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 463163953Srrs if (retval < 0) { 464163953Srrs /* 465163953Srrs * No cookie, we probably should send a op error. But in any 466163953Srrs * case if there is no cookie in the INIT-ACK, we can 467163953Srrs * abandon the peer, its broke. 468163953Srrs */ 469163953Srrs if (retval == -3) { 470163953Srrs /* We abort with an error of missing mandatory param */ 471163953Srrs op_err = 472163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 473163953Srrs if (op_err) { 474163953Srrs /* 475163953Srrs * Expand beyond to include the mandatory 476163953Srrs * param cookie 477163953Srrs */ 478163953Srrs struct sctp_inv_mandatory_param *mp; 479163953Srrs 480165647Srrs SCTP_BUF_LEN(op_err) = 481163953Srrs sizeof(struct sctp_inv_mandatory_param); 482163953Srrs mp = mtod(op_err, 483163953Srrs struct sctp_inv_mandatory_param *); 484163953Srrs /* Subtract the reserved param */ 485163953Srrs mp->length = 486163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 487163953Srrs mp->num_param = htonl(1); 488163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 489163953Srrs mp->resv = 0; 490163953Srrs } 491163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 492170181Srrs sh, op_err, 0); 493168299Srrs *abort_no_unlock = 1; 494163953Srrs } 495163953Srrs return (retval); 496163953Srrs } 497163953Srrs return (0); 498163953Srrs} 499163953Srrs 500163953Srrsstatic void 501163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 502163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 503163953Srrs{ 504163953Srrs struct sockaddr_storage store; 505163953Srrs struct sockaddr_in *sin; 506163953Srrs struct sockaddr_in6 *sin6; 507163953Srrs struct sctp_nets *r_net; 508163953Srrs struct timeval tv; 509172396Srrs int req_prim = 0; 510163953Srrs 511163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 512163953Srrs /* Invalid length */ 513163953Srrs return; 514163953Srrs } 515163953Srrs sin = (struct sockaddr_in *)&store; 516163953Srrs sin6 = (struct sockaddr_in6 *)&store; 517163953Srrs 518163953Srrs memset(&store, 0, sizeof(store)); 519163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 520163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 521163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 522163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 523163953Srrs sin->sin_port = stcb->rport; 524163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 525163953Srrs sizeof(sin->sin_addr)); 526163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 527163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 528163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 529163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 530163953Srrs sin6->sin6_port = stcb->rport; 531163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 532163953Srrs sizeof(sin6->sin6_addr)); 533163953Srrs } else { 534163953Srrs return; 535163953Srrs } 536163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 537163953Srrs if (r_net == NULL) { 538169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 539163953Srrs return; 540163953Srrs } 541163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 542163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 543163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 544163953Srrs /* 545163953Srrs * If the its a HB and it's random value is correct when can 546163953Srrs * confirm the destination. 547163953Srrs */ 548163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 549167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 550167598Srrs stcb->asoc.primary_destination = r_net; 551167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 552167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 553167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 554167598Srrs if (r_net != stcb->asoc.primary_destination) { 555167598Srrs /* 556167598Srrs * first one on the list is NOT the primary 557167598Srrs * sctp_cmpaddr() is much more efficent if 558167598Srrs * the primary is the first on the list, 559167598Srrs * make it so. 560167598Srrs */ 561167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 562167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 563167598Srrs } 564172396Srrs req_prim = 1; 565167598Srrs } 566163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 567172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 568163953Srrs } 569163953Srrs r_net->error_count = 0; 570163953Srrs r_net->hb_responded = 1; 571163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 572163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 573163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 574163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 575163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 576163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 577172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 578163953Srrs /* now was it the primary? if so restore */ 579163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 580169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 581163953Srrs } 582163953Srrs } 583171440Srrs /* 584171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 585171440Srrs * set the destination to active state and set the cwnd to one or 586171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 587171440Srrs * timer is running, for the destination, stop the timer because a 588171440Srrs * PF-heartbeat was received. 589171440Srrs */ 590171477Srrs if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == 591171440Srrs SCTP_ADDR_PF) { 592171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 593171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 594171440Srrs stcb, net, 595171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 596171440Srrs } 597171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 598171440Srrs net->cwnd = net->mtu * sctp_cmt_pf; 599171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 600171440Srrs net, net->cwnd); 601171440Srrs } 602163953Srrs /* Now lets do a RTO with this */ 603171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 604172396Srrs /* Mobility adaptation */ 605172396Srrs if (req_prim) { 606172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 607172396Srrs SCTP_MOBILITY_BASE) || 608172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 609172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 610172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 611172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 612172396Srrs 613172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 614172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 615172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 616172396Srrs sctp_assoc_immediate_retrans(stcb, 617172396Srrs stcb->asoc.primary_destination); 618172396Srrs } 619172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 620172396Srrs SCTP_MOBILITY_BASE)) { 621172396Srrs sctp_move_chunks_from_deleted_prim(stcb, 622172396Srrs stcb->asoc.primary_destination); 623172396Srrs } 624172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 625172396Srrs stcb->asoc.deleted_primary); 626172396Srrs } 627172396Srrs } 628163953Srrs} 629163953Srrs 630163953Srrsstatic void 631163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 632163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 633163953Srrs{ 634172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 635172090Srrs struct socket *so; 636172090Srrs 637172090Srrs#endif 638172090Srrs 639169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 640163953Srrs if (stcb == NULL) 641163953Srrs return; 642163953Srrs 643163953Srrs /* stop any receive timers */ 644171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 645163953Srrs /* notify user of the abort and clean up... */ 646172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 647163953Srrs /* free the tcb */ 648172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 649172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 650172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 651172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 652172396Srrs panic("Received an ABORT"); 653172396Srrs } else { 654172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 655172396Srrs } 656172396Srrs#endif 657163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 658163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 659163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 660163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 661163953Srrs } 662168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 663168859Srrs sctp_print_out_track_log(stcb); 664168859Srrs#endif 665172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 666172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 667172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 668172090Srrs SCTP_TCB_UNLOCK(stcb); 669172090Srrs SCTP_SOCKET_LOCK(so, 1); 670172090Srrs SCTP_TCB_LOCK(stcb); 671172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 672172090Srrs#endif 673171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 674171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 675171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 676172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 677172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 678172090Srrs#endif 679169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 680163953Srrs} 681163953Srrs 682163953Srrsstatic void 683163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 684163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 685163953Srrs{ 686163953Srrs struct sctp_association *asoc; 687163953Srrs int some_on_streamwheel; 688163953Srrs 689172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 690172090Srrs struct socket *so; 691172090Srrs 692172090Srrs#endif 693172090Srrs 694169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 695169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 696163953Srrs if (stcb == NULL) 697163953Srrs return; 698165220Srrs asoc = &stcb->asoc; 699165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 700165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 701163953Srrs return; 702163953Srrs } 703163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 704163953Srrs /* Shutdown NOT the expected size */ 705163953Srrs return; 706163953Srrs } else { 707163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 708163953Srrs } 709165220Srrs if (asoc->control_pdapi) { 710163953Srrs /* 711163953Srrs * With a normal shutdown we assume the end of last record. 712163953Srrs */ 713163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 714165220Srrs asoc->control_pdapi->end_added = 1; 715165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 716165220Srrs asoc->control_pdapi = NULL; 717163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 718172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 719172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 720172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 721172090Srrs SCTP_TCB_UNLOCK(stcb); 722172090Srrs SCTP_SOCKET_LOCK(so, 1); 723172090Srrs SCTP_TCB_LOCK(stcb); 724172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 725172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 726172090Srrs /* assoc was freed while we were unlocked */ 727172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 728172090Srrs return; 729172090Srrs } 730172090Srrs#endif 731163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 732172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 733172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 734172090Srrs#endif 735163953Srrs } 736163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 737163953Srrs if (stcb->sctp_socket) { 738163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 739170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 740163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 741171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 742172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 743163953Srrs /* 744163953Srrs * notify upper layer that peer has initiated a 745163953Srrs * shutdown 746163953Srrs */ 747172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 748163953Srrs 749163953Srrs /* reset time */ 750169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 751163953Srrs } 752163953Srrs } 753163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 754163953Srrs /* 755163953Srrs * stop the shutdown timer, since we WILL move to 756163953Srrs * SHUTDOWN-ACK-SENT. 757163953Srrs */ 758171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 759163953Srrs } 760171158Srrs /* Now is there unsent data on a stream somewhere? */ 761171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 762163953Srrs 763163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 764163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 765163953Srrs some_on_streamwheel) { 766163953Srrs /* By returning we will push more data out */ 767163953Srrs return; 768163953Srrs } else { 769163953Srrs /* no outstanding data to send, so move on... */ 770163953Srrs /* send SHUTDOWN-ACK */ 771163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 772163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 773166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 774166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 775163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 776163953Srrs } 777171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 778172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 779171990Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, 780171990Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 781163953Srrs /* start SHUTDOWN timer */ 782163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 783163953Srrs stcb, net); 784163953Srrs } 785163953Srrs} 786163953Srrs 787163953Srrsstatic void 788163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 789163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 790163953Srrs{ 791163953Srrs struct sctp_association *asoc; 792163953Srrs 793172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 794172090Srrs struct socket *so; 795172090Srrs 796172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 797172090Srrs#endif 798169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 799169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 800163953Srrs if (stcb == NULL) 801163953Srrs return; 802163953Srrs 803163953Srrs asoc = &stcb->asoc; 804163953Srrs /* process according to association state */ 805163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 806163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 807163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 808163953Srrs SCTP_TCB_UNLOCK(stcb); 809163953Srrs return; 810163953Srrs } 811165220Srrs if (asoc->control_pdapi) { 812163953Srrs /* 813163953Srrs * With a normal shutdown we assume the end of last record. 814163953Srrs */ 815163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 816165220Srrs asoc->control_pdapi->end_added = 1; 817165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 818165220Srrs asoc->control_pdapi = NULL; 819163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 820172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 821172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 822172090Srrs SCTP_TCB_UNLOCK(stcb); 823172090Srrs SCTP_SOCKET_LOCK(so, 1); 824172090Srrs SCTP_TCB_LOCK(stcb); 825172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 826172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 827172090Srrs /* assoc was freed while we were unlocked */ 828172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 829172090Srrs return; 830172090Srrs } 831172090Srrs#endif 832163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 833172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 834172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 835172090Srrs#endif 836163953Srrs } 837163953Srrs /* are the queues empty? */ 838163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 839163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 840163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 841172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 842163953Srrs } 843163953Srrs /* stop the timer */ 844171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 845163953Srrs /* send SHUTDOWN-COMPLETE */ 846163953Srrs sctp_send_shutdown_complete(stcb, net); 847163953Srrs /* notify upper layer protocol */ 848163953Srrs if (stcb->sctp_socket) { 849172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 850163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 851163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 852163953Srrs /* Set the connected flag to disconnected */ 853163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 854163953Srrs } 855163953Srrs } 856163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 857163953Srrs /* free the TCB but first save off the ep */ 858172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 859172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 860172090Srrs SCTP_TCB_UNLOCK(stcb); 861172090Srrs SCTP_SOCKET_LOCK(so, 1); 862172090Srrs SCTP_TCB_LOCK(stcb); 863172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 864172090Srrs#endif 865171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 866171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 867172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 868172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 869172090Srrs#endif 870163953Srrs} 871163953Srrs 872163953Srrs/* 873163953Srrs * Skip past the param header and then we will find the chunk that caused the 874163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 875163953Srrs * our peer must be broken. 876163953Srrs */ 877163953Srrsstatic void 878163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 879163953Srrs struct sctp_nets *net) 880163953Srrs{ 881163953Srrs struct sctp_chunkhdr *chk; 882163953Srrs 883163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 884163953Srrs switch (chk->chunk_type) { 885163953Srrs case SCTP_ASCONF_ACK: 886163953Srrs case SCTP_ASCONF: 887163953Srrs sctp_asconf_cleanup(stcb, net); 888163953Srrs break; 889163953Srrs case SCTP_FORWARD_CUM_TSN: 890163953Srrs stcb->asoc.peer_supports_prsctp = 0; 891163953Srrs break; 892163953Srrs default: 893169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 894169420Srrs "Peer does not support chunk type %d(%x)??\n", 895169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 896163953Srrs break; 897163953Srrs } 898163953Srrs} 899163953Srrs 900163953Srrs/* 901163953Srrs * Skip past the param header and then we will find the param that caused the 902163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 903163953Srrs * these will turn of specific features. 904163953Srrs */ 905163953Srrsstatic void 906163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 907163953Srrs{ 908163953Srrs struct sctp_paramhdr *pbad; 909163953Srrs 910163953Srrs pbad = phdr + 1; 911163953Srrs switch (ntohs(pbad->param_type)) { 912163953Srrs /* pr-sctp draft */ 913163953Srrs case SCTP_PRSCTP_SUPPORTED: 914163953Srrs stcb->asoc.peer_supports_prsctp = 0; 915163953Srrs break; 916163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 917163953Srrs break; 918163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 919163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 920163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 921163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 922163953Srrs stcb->asoc.ecn_allowed = 0; 923163953Srrs break; 924163953Srrs case SCTP_ADD_IP_ADDRESS: 925163953Srrs case SCTP_DEL_IP_ADDRESS: 926163953Srrs case SCTP_SET_PRIM_ADDR: 927163953Srrs stcb->asoc.peer_supports_asconf = 0; 928163953Srrs break; 929163953Srrs case SCTP_SUCCESS_REPORT: 930163953Srrs case SCTP_ERROR_CAUSE_IND: 931169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 932169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 933169420Srrs "Turning off ASCONF to this strange peer\n"); 934163953Srrs stcb->asoc.peer_supports_asconf = 0; 935163953Srrs break; 936163953Srrs default: 937169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 938169420Srrs "Peer does not support param type %d(%x)??\n", 939169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 940163953Srrs break; 941163953Srrs } 942163953Srrs} 943163953Srrs 944163953Srrsstatic int 945163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 946163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 947163953Srrs{ 948163953Srrs int chklen; 949163953Srrs struct sctp_paramhdr *phdr; 950163953Srrs uint16_t error_type; 951163953Srrs uint16_t error_len; 952163953Srrs struct sctp_association *asoc; 953163953Srrs int adjust; 954163953Srrs 955172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 956172090Srrs struct socket *so; 957172090Srrs 958172090Srrs#endif 959172090Srrs 960163953Srrs /* parse through all of the errors and process */ 961163953Srrs asoc = &stcb->asoc; 962163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 963163953Srrs sizeof(struct sctp_chunkhdr)); 964163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 965163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 966163953Srrs /* Process an Error Cause */ 967163953Srrs error_type = ntohs(phdr->param_type); 968163953Srrs error_len = ntohs(phdr->param_length); 969163953Srrs if ((error_len > chklen) || (error_len == 0)) { 970163953Srrs /* invalid param length for this param */ 971169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 972169420Srrs chklen, error_len); 973163953Srrs return (0); 974163953Srrs } 975163953Srrs switch (error_type) { 976163953Srrs case SCTP_CAUSE_INVALID_STREAM: 977163953Srrs case SCTP_CAUSE_MISSING_PARAM: 978163953Srrs case SCTP_CAUSE_INVALID_PARAM: 979163953Srrs case SCTP_CAUSE_NO_USER_DATA: 980169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 981169420Srrs error_type); 982163953Srrs break; 983163953Srrs case SCTP_CAUSE_STALE_COOKIE: 984163953Srrs /* 985163953Srrs * We only act if we have echoed a cookie and are 986163953Srrs * waiting. 987163953Srrs */ 988163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 989163953Srrs int *p; 990163953Srrs 991163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 992163953Srrs /* Save the time doubled */ 993163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 994163953Srrs asoc->stale_cookie_count++; 995163953Srrs if (asoc->stale_cookie_count > 996163953Srrs asoc->max_init_times) { 997172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 998163953Srrs /* now free the asoc */ 999172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1000172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1001172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1002172090Srrs SCTP_TCB_UNLOCK(stcb); 1003172090Srrs SCTP_SOCKET_LOCK(so, 1); 1004172090Srrs SCTP_TCB_LOCK(stcb); 1005172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1006172090Srrs#endif 1007171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1008171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1009172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1010172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1011172090Srrs#endif 1012163953Srrs return (-1); 1013163953Srrs } 1014163953Srrs /* blast back to INIT state */ 1015163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1016163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1017163953Srrs 1018163953Srrs sctp_stop_all_cookie_timers(stcb); 1019172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1020163953Srrs } 1021163953Srrs break; 1022163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1023163953Srrs /* 1024163953Srrs * Nothing we can do here, we don't do hostname 1025163953Srrs * addresses so if the peer does not like my IPv6 1026163953Srrs * (or IPv4 for that matter) it does not matter. If 1027163953Srrs * they don't support that type of address, they can 1028163953Srrs * NOT possibly get that packet type... i.e. with no 1029163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1030163953Srrs * safely ignore this one. If we ever added support 1031163953Srrs * for HOSTNAME Addresses, then we would need to do 1032163953Srrs * something here. 1033163953Srrs */ 1034163953Srrs break; 1035163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1036163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1037163953Srrs break; 1038163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1039163953Srrs sctp_process_unrecog_param(stcb, phdr); 1040163953Srrs break; 1041163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1042163953Srrs /* 1043163953Srrs * We ignore this since the timer will drive out a 1044163953Srrs * new cookie anyway and there timer will drive us 1045163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1046163953Srrs * here since we don't have their tag. 1047163953Srrs */ 1048163953Srrs break; 1049163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1050163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1051163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1052163953Srrs /* 1053163953Srrs * We should NOT get these here, but in a 1054166086Srrs * ASCONF-ACK. 1055163953Srrs */ 1056169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1057169420Srrs error_type); 1058163953Srrs break; 1059163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1060163953Srrs /* 1061163953Srrs * And what, pray tell do we do with the fact that 1062163953Srrs * the peer is out of resources? Not really sure we 1063166086Srrs * could do anything but abort. I suspect this 1064163953Srrs * should have came WITH an abort instead of in a 1065163953Srrs * OP-ERROR. 1066163953Srrs */ 1067163953Srrs break; 1068163953Srrs default: 1069169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1070169420Srrs error_type); 1071163953Srrs break; 1072163953Srrs } 1073163953Srrs adjust = SCTP_SIZE32(error_len); 1074163953Srrs chklen -= adjust; 1075163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1076163953Srrs } 1077163953Srrs return (0); 1078163953Srrs} 1079163953Srrs 1080163953Srrsstatic int 1081169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1082169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1083170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1084163953Srrs{ 1085163953Srrs struct sctp_init_ack *init_ack; 1086163953Srrs struct mbuf *op_err; 1087163953Srrs 1088169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1089169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1090169420Srrs 1091163953Srrs if (stcb == NULL) { 1092169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1093169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1094163953Srrs return (-1); 1095163953Srrs } 1096163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1097163953Srrs /* Invalid length */ 1098163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1099163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1100170181Srrs op_err, 0); 1101168299Srrs *abort_no_unlock = 1; 1102163953Srrs return (-1); 1103163953Srrs } 1104163953Srrs init_ack = &cp->init; 1105163953Srrs /* validate parameters */ 1106163953Srrs if (init_ack->initiate_tag == 0) { 1107163953Srrs /* protocol error... send an abort */ 1108163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1109163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1110170181Srrs op_err, 0); 1111168299Srrs *abort_no_unlock = 1; 1112163953Srrs return (-1); 1113163953Srrs } 1114163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1115163953Srrs /* protocol error... send an abort */ 1116163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1117163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1118170181Srrs op_err, 0); 1119168299Srrs *abort_no_unlock = 1; 1120163953Srrs return (-1); 1121163953Srrs } 1122163953Srrs if (init_ack->num_inbound_streams == 0) { 1123163953Srrs /* protocol error... send an abort */ 1124163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1125163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1126170181Srrs op_err, 0); 1127168299Srrs *abort_no_unlock = 1; 1128163953Srrs return (-1); 1129163953Srrs } 1130163953Srrs if (init_ack->num_outbound_streams == 0) { 1131163953Srrs /* protocol error... send an abort */ 1132163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1133163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1134170181Srrs op_err, 0); 1135168299Srrs *abort_no_unlock = 1; 1136163953Srrs return (-1); 1137163953Srrs } 1138163953Srrs /* process according to association state... */ 1139171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1140163953Srrs case SCTP_STATE_COOKIE_WAIT: 1141163953Srrs /* this is the expected state for this chunk */ 1142163953Srrs /* process the INIT-ACK parameters */ 1143163953Srrs if (stcb->asoc.primary_destination->dest_state & 1144163953Srrs SCTP_ADDR_UNCONFIRMED) { 1145163953Srrs /* 1146163953Srrs * The primary is where we sent the INIT, we can 1147163953Srrs * always consider it confirmed when the INIT-ACK is 1148163953Srrs * returned. Do this before we load addresses 1149163953Srrs * though. 1150163953Srrs */ 1151163953Srrs stcb->asoc.primary_destination->dest_state &= 1152163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1153163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1154172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1155163953Srrs } 1156169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1157170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1158163953Srrs /* error in parsing parameters */ 1159163953Srrs return (-1); 1160163953Srrs } 1161163953Srrs /* update our state */ 1162169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1163171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1164163953Srrs 1165163953Srrs /* reset the RTO calc */ 1166171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 1167171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1168171943Srrs stcb->asoc.overall_error_count, 1169171943Srrs 0, 1170171943Srrs SCTP_FROM_SCTP_INPUT, 1171171943Srrs __LINE__); 1172171943Srrs } 1173163953Srrs stcb->asoc.overall_error_count = 0; 1174169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1175163953Srrs /* 1176163953Srrs * collapse the init timer back in case of a exponential 1177165220Srrs * backoff 1178163953Srrs */ 1179163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1180163953Srrs stcb, net); 1181163953Srrs /* 1182163953Srrs * the send at the end of the inbound data processing will 1183163953Srrs * cause the cookie to be sent 1184163953Srrs */ 1185163953Srrs break; 1186163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1187163953Srrs /* incorrect state... discard */ 1188163953Srrs break; 1189163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1190163953Srrs /* incorrect state... discard */ 1191163953Srrs break; 1192163953Srrs case SCTP_STATE_OPEN: 1193163953Srrs /* incorrect state... discard */ 1194163953Srrs break; 1195163953Srrs case SCTP_STATE_EMPTY: 1196163953Srrs case SCTP_STATE_INUSE: 1197163953Srrs default: 1198163953Srrs /* incorrect state... discard */ 1199163953Srrs return (-1); 1200163953Srrs break; 1201163953Srrs } 1202169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1203163953Srrs return (0); 1204163953Srrs} 1205163953Srrs 1206163953Srrs 1207163953Srrs/* 1208163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1209163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1210163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1211163953Srrs * mbuf to the cookie-echo chunk 1212163953Srrs */ 1213163953Srrsstatic struct sctp_tcb * 1214163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1215163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1216163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1217169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1218170181Srrs uint32_t vrf_id) 1219163953Srrs{ 1220163953Srrs struct sctp_association *asoc; 1221163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1222163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1223163953Srrs int chk_length; 1224165220Srrs int init_offset, initack_offset, i; 1225163953Srrs int retval; 1226164205Srrs int spec_flag = 0; 1227170140Srrs uint32_t how_indx; 1228163953Srrs 1229163953Srrs /* I know that the TCB is non-NULL from the caller */ 1230163953Srrs asoc = &stcb->asoc; 1231166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1232166023Srrs if (asoc->cookie_how[how_indx] == 0) 1233166023Srrs break; 1234166023Srrs } 1235166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1236166023Srrs asoc->cookie_how[how_indx] = 1; 1237166023Srrs } 1238163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1239163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1240163953Srrs struct mbuf *op_err; 1241163953Srrs struct sctp_paramhdr *ph; 1242163953Srrs 1243163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1244163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1245165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1246163953Srrs if (op_err == NULL) { 1247163953Srrs /* FOOBAR */ 1248163953Srrs return (NULL); 1249163953Srrs } 1250163953Srrs /* pre-reserve some space */ 1251165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1252165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1253165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1254163953Srrs /* Set the len */ 1255165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1256163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1257163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1258163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1259169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1260170181Srrs vrf_id); 1261166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1262166023Srrs asoc->cookie_how[how_indx] = 2; 1263163953Srrs return (NULL); 1264163953Srrs } 1265163953Srrs /* 1266163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1267163953Srrs * INIT should start after the cookie-echo header struct (chunk 1268163953Srrs * header, state cookie header struct) 1269163953Srrs */ 1270163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1271163953Srrs 1272163953Srrs init_cp = (struct sctp_init_chunk *) 1273163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1274163953Srrs (uint8_t *) & init_buf); 1275163953Srrs if (init_cp == NULL) { 1276163953Srrs /* could not pull a INIT chunk in cookie */ 1277163953Srrs return (NULL); 1278163953Srrs } 1279163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1280163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1281163953Srrs return (NULL); 1282163953Srrs } 1283163953Srrs /* 1284163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1285163953Srrs * INIT-ACK follows the INIT chunk 1286163953Srrs */ 1287163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1288163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1289163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1290163953Srrs (uint8_t *) & initack_buf); 1291163953Srrs if (initack_cp == NULL) { 1292163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1293163953Srrs return (NULL); 1294163953Srrs } 1295163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1296163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1297163953Srrs return (NULL); 1298163953Srrs } 1299163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1300163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1301163953Srrs /* 1302163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1303163953Srrs * to get into the OPEN state 1304163953Srrs */ 1305166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1306172091Srrs /*- 1307172091Srrs * Opps, this means that we somehow generated two vtag's 1308172091Srrs * the same. I.e. we did: 1309172091Srrs * Us Peer 1310172091Srrs * <---INIT(tag=a)------ 1311172091Srrs * ----INIT-ACK(tag=t)--> 1312172091Srrs * ----INIT(tag=t)------> *1 1313172091Srrs * <---INIT-ACK(tag=a)--- 1314172091Srrs * <----CE(tag=t)------------- *2 1315172091Srrs * 1316172091Srrs * At point *1 we should be generating a different 1317172091Srrs * tag t'. Which means we would throw away the CE and send 1318172091Srrs * ours instead. Basically this is case C (throw away side). 1319172091Srrs */ 1320172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1321172091Srrs asoc->cookie_how[how_indx] = 17; 1322172091Srrs return (NULL); 1323172091Srrs 1324166023Srrs } 1325163953Srrs switch SCTP_GET_STATE 1326163953Srrs (asoc) { 1327163953Srrs case SCTP_STATE_COOKIE_WAIT: 1328166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1329163953Srrs /* 1330169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1331166023Srrs * correct tags... just accept it...but we must 1332166023Srrs * process the init so that we can make sure we have 1333166023Srrs * the right seq no's. 1334163953Srrs */ 1335163953Srrs /* First we must process the INIT !! */ 1336163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1337163953Srrs if (retval < 0) { 1338166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1339166023Srrs asoc->cookie_how[how_indx] = 3; 1340163953Srrs return (NULL); 1341163953Srrs } 1342163953Srrs /* we have already processed the INIT so no problem */ 1343163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1344171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1345171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1346163953Srrs /* update current state */ 1347166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1348166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1349166675Srrs else 1350166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1351171943Srrs 1352171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1353163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1354163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1355163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1356163953Srrs } 1357166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1358165220Srrs sctp_stop_all_cookie_timers(stcb); 1359163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1360163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1361163953Srrs (inp->sctp_socket->so_qlimit == 0) 1362163953Srrs ) { 1363172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1364172090Srrs struct socket *so; 1365172090Srrs 1366172090Srrs#endif 1367163953Srrs /* 1368163953Srrs * Here is where collision would go if we 1369163953Srrs * did a connect() and instead got a 1370163953Srrs * init/init-ack/cookie done before the 1371163953Srrs * init-ack came back.. 1372163953Srrs */ 1373163953Srrs stcb->sctp_ep->sctp_flags |= 1374163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1375172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1376172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1377172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1378172090Srrs SCTP_TCB_UNLOCK(stcb); 1379172090Srrs SCTP_SOCKET_LOCK(so, 1); 1380172090Srrs SCTP_TCB_LOCK(stcb); 1381172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1382172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1383172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1384172090Srrs return (NULL); 1385172090Srrs } 1386172090Srrs#endif 1387172090Srrs soisconnected(stcb->sctp_socket); 1388172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1389172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1390172090Srrs#endif 1391163953Srrs } 1392163953Srrs /* notify upper layer */ 1393163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1394163953Srrs /* 1395163953Srrs * since we did not send a HB make sure we don't 1396163953Srrs * double things 1397163953Srrs */ 1398163953Srrs net->hb_responded = 1; 1399170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1400171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1401163953Srrs 1402163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1403163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1404163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1405163953Srrs inp, stcb, NULL); 1406163953Srrs } 1407163953Srrs break; 1408163953Srrs default: 1409163953Srrs /* 1410163953Srrs * we're in the OPEN state (or beyond), so peer must 1411163953Srrs * have simply lost the COOKIE-ACK 1412163953Srrs */ 1413163953Srrs break; 1414163953Srrs } /* end switch */ 1415165220Srrs sctp_stop_all_cookie_timers(stcb); 1416163953Srrs /* 1417163953Srrs * We ignore the return code here.. not sure if we should 1418163953Srrs * somehow abort.. but we do have an existing asoc. This 1419163953Srrs * really should not fail. 1420163953Srrs */ 1421163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1422163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1423163953Srrs initack_offset, sh, init_src)) { 1424166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1425166023Srrs asoc->cookie_how[how_indx] = 4; 1426163953Srrs return (NULL); 1427163953Srrs } 1428163953Srrs /* respond with a COOKIE-ACK */ 1429165220Srrs sctp_toss_old_cookies(stcb, asoc); 1430163953Srrs sctp_send_cookie_ack(stcb); 1431166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1432166023Srrs asoc->cookie_how[how_indx] = 5; 1433163953Srrs return (stcb); 1434169352Srrs } 1435163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1436163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1437163953Srrs cookie->tie_tag_my_vtag == 0 && 1438163953Srrs cookie->tie_tag_peer_vtag == 0) { 1439163953Srrs /* 1440163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1441163953Srrs */ 1442166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1443166023Srrs asoc->cookie_how[how_indx] = 6; 1444163953Srrs return (NULL); 1445163953Srrs } 1446163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1447163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1448163953Srrs init_cp->init.initiate_tag == 0)) { 1449163953Srrs /* 1450163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1451163953Srrs * should be ok, re-accept peer info 1452163953Srrs */ 1453166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1454166023Srrs /* 1455166023Srrs * Extension of case C. If we hit this, then the 1456166023Srrs * random number generator returned the same vtag 1457166023Srrs * when we first sent our INIT-ACK and when we later 1458166023Srrs * sent our INIT. The side with the seq numbers that 1459166023Srrs * are different will be the one that normnally 1460166023Srrs * would have hit case C. This in effect "extends" 1461166023Srrs * our vtags in this collision case to be 64 bits. 1462166023Srrs * The same collision could occur aka you get both 1463166023Srrs * vtag and seq number the same twice in a row.. but 1464166023Srrs * is much less likely. If it did happen then we 1465166023Srrs * would proceed through and bring up the assoc.. we 1466166023Srrs * may end up with the wrong stream setup however.. 1467166023Srrs * which would be bad.. but there is no way to 1468166023Srrs * tell.. until we send on a stream that does not 1469166023Srrs * exist :-) 1470166023Srrs */ 1471166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1472166023Srrs asoc->cookie_how[how_indx] = 7; 1473166023Srrs 1474166023Srrs return (NULL); 1475166023Srrs } 1476166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1477166023Srrs asoc->cookie_how[how_indx] = 8; 1478171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1479163953Srrs sctp_stop_all_cookie_timers(stcb); 1480163953Srrs /* 1481163953Srrs * since we did not send a HB make sure we don't double 1482163953Srrs * things 1483163953Srrs */ 1484163953Srrs net->hb_responded = 1; 1485163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1486163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1487163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1488163953Srrs NULL); 1489163953Srrs } 1490163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1491164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1492164205Srrs 1493164205Srrs /* Note last_cwr_tsn? where is this used? */ 1494163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1495164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1496164205Srrs /* 1497164205Srrs * Ok the peer probably discarded our data (if we 1498164205Srrs * echoed a cookie+data). So anything on the 1499164205Srrs * sent_queue should be marked for retransmit, we 1500164205Srrs * may not get something to kick us so it COULD 1501164205Srrs * still take a timeout to move these.. but it can't 1502164205Srrs * hurt to mark them. 1503164205Srrs */ 1504164205Srrs struct sctp_tmit_chunk *chk; 1505163953Srrs 1506164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1507164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1508164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1509171440Srrs sctp_flight_size_decrease(chk); 1510171440Srrs sctp_total_flight_decrease(stcb, chk); 1511168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1512164205Srrs spec_flag++; 1513164205Srrs } 1514164205Srrs } 1515164205Srrs 1516164205Srrs } 1517163953Srrs /* process the INIT info (peer's info) */ 1518163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1519163953Srrs if (retval < 0) { 1520166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1521166023Srrs asoc->cookie_how[how_indx] = 9; 1522163953Srrs return (NULL); 1523163953Srrs } 1524163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1525163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1526163953Srrs initack_offset, sh, init_src)) { 1527166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1528166023Srrs asoc->cookie_how[how_indx] = 10; 1529163953Srrs return (NULL); 1530163953Srrs } 1531163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1532163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1533163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1534163953Srrs 1535163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1536163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1537163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1538172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1539172090Srrs struct socket *so; 1540172090Srrs 1541172090Srrs#endif 1542163953Srrs stcb->sctp_ep->sctp_flags |= 1543163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1544172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1545172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1546172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1547172090Srrs SCTP_TCB_UNLOCK(stcb); 1548172090Srrs SCTP_SOCKET_LOCK(so, 1); 1549172090Srrs SCTP_TCB_LOCK(stcb); 1550172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1551172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1552172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1553172090Srrs return (NULL); 1554172090Srrs } 1555172090Srrs#endif 1556172090Srrs soisconnected(stcb->sctp_socket); 1557172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1558172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1559172090Srrs#endif 1560163953Srrs } 1561166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1562166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1563166675Srrs else 1564166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1565166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1566166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1567166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1568166675Srrs } else { 1569166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1570163953Srrs } 1571171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1572163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1573163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1574163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1575163953Srrs } 1576163953Srrs sctp_stop_all_cookie_timers(stcb); 1577165220Srrs sctp_toss_old_cookies(stcb, asoc); 1578163953Srrs sctp_send_cookie_ack(stcb); 1579164205Srrs if (spec_flag) { 1580164205Srrs /* 1581164205Srrs * only if we have retrans set do we do this. What 1582164205Srrs * this call does is get only the COOKIE-ACK out and 1583164205Srrs * then when we return the normal call to 1584164205Srrs * sctp_chunk_output will get the retrans out behind 1585164205Srrs * this. 1586164205Srrs */ 1587172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1588164205Srrs } 1589166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1590166023Srrs asoc->cookie_how[how_indx] = 11; 1591166023Srrs 1592163953Srrs return (stcb); 1593163953Srrs } 1594163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1595163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1596163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1597163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1598163953Srrs cookie->tie_tag_peer_vtag != 0) { 1599163953Srrs struct sctpasochead *head; 1600163953Srrs 1601163953Srrs /* 1602163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1603163953Srrs */ 1604165220Srrs /* temp code */ 1605166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1606166023Srrs asoc->cookie_how[how_indx] = 12; 1607171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1608171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1609165647Srrs 1610163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1611163953Srrs /* notify upper layer */ 1612163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1613165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1614166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1615166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1616166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1617166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1618166675Srrs } 1619166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1620166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1621166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1622166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1623166675Srrs } 1624165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1625171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1626165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1627165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1628165647Srrs 1629165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1630165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1631171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1632165647Srrs } 1633165647Srrs asoc->pre_open_streams = 1634165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1635165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1636165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1637165647Srrs 1638165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1639165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1640165647Srrs 1641165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1642165647Srrs 1643165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1644170138Srrs if (asoc->mapping_array) { 1645165647Srrs memset(asoc->mapping_array, 0, 1646165647Srrs asoc->mapping_array_size); 1647170138Srrs } 1648165220Srrs SCTP_TCB_UNLOCK(stcb); 1649165220Srrs SCTP_INP_INFO_WLOCK(); 1650165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1651165220Srrs SCTP_TCB_LOCK(stcb); 1652165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1653163953Srrs /* send up all the data */ 1654164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1655165220Srrs 1656172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1657165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1658165220Srrs stcb->asoc.strmout[i].stream_no = i; 1659165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1660165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1661165220Srrs } 1662163953Srrs /* process the INIT-ACK info (my info) */ 1663163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1664163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1665163953Srrs 1666163953Srrs /* pull from vtag hash */ 1667163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1668163953Srrs /* re-insert to new vtag position */ 1669163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1670163953Srrs sctppcbinfo.hashasocmark)]; 1671163953Srrs /* 1672163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1673163953Srrs * system 1674163953Srrs */ 1675163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1676163953Srrs 1677163953Srrs /* Is this the first restart? */ 1678163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1679163953Srrs /* Ok add it to assoc_id vtag hash */ 1680163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1681163953Srrs sctppcbinfo.hashrestartmark)]; 1682163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1683163953Srrs stcb->asoc.in_restart_hash = 1; 1684163953Srrs } 1685163953Srrs /* process the INIT info (peer's info) */ 1686164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1687165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1688165220Srrs SCTP_INP_INFO_WUNLOCK(); 1689165220Srrs 1690163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1691163953Srrs if (retval < 0) { 1692166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1693166023Srrs asoc->cookie_how[how_indx] = 13; 1694166023Srrs 1695163953Srrs return (NULL); 1696163953Srrs } 1697163953Srrs /* 1698163953Srrs * since we did not send a HB make sure we don't double 1699163953Srrs * things 1700163953Srrs */ 1701163953Srrs net->hb_responded = 1; 1702163953Srrs 1703163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1704163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1705163953Srrs initack_offset, sh, init_src)) { 1706166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1707166023Srrs asoc->cookie_how[how_indx] = 14; 1708166023Srrs 1709163953Srrs return (NULL); 1710163953Srrs } 1711163953Srrs /* respond with a COOKIE-ACK */ 1712163953Srrs sctp_stop_all_cookie_timers(stcb); 1713165220Srrs sctp_toss_old_cookies(stcb, asoc); 1714163953Srrs sctp_send_cookie_ack(stcb); 1715166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1716166023Srrs asoc->cookie_how[how_indx] = 15; 1717163953Srrs 1718163953Srrs return (stcb); 1719163953Srrs } 1720166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1721166023Srrs asoc->cookie_how[how_indx] = 16; 1722163953Srrs /* all other cases... */ 1723163953Srrs return (NULL); 1724163953Srrs} 1725163953Srrs 1726166086Srrs 1727163953Srrs/* 1728163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1729163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1730163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1731163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1732163953Srrs * was from returns a new TCB 1733163953Srrs */ 1734163953Srrsstatic struct sctp_tcb * 1735163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1736163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1737163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1738163953Srrs struct sockaddr *init_src, int *notification, 1739169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1740170181Srrs uint32_t vrf_id) 1741163953Srrs{ 1742163953Srrs struct sctp_tcb *stcb; 1743163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1744163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1745163953Srrs struct sockaddr_storage sa_store; 1746163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1747163953Srrs struct sockaddr_in *sin; 1748163953Srrs struct sockaddr_in6 *sin6; 1749163953Srrs struct sctp_association *asoc; 1750163953Srrs int chk_length; 1751163953Srrs int init_offset, initack_offset, initack_limit; 1752163953Srrs int retval; 1753163953Srrs int error = 0; 1754163953Srrs uint32_t old_tag; 1755166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1756163953Srrs 1757172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1758172090Srrs struct socket *so; 1759172090Srrs 1760172090Srrs so = SCTP_INP_SO(inp); 1761172090Srrs#endif 1762172090Srrs 1763163953Srrs /* 1764163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1765163953Srrs * INIT should start after the cookie-echo header struct (chunk 1766163953Srrs * header, state cookie header struct) 1767163953Srrs */ 1768163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1769163953Srrs init_cp = (struct sctp_init_chunk *) 1770163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1771163953Srrs (uint8_t *) & init_buf); 1772163953Srrs if (init_cp == NULL) { 1773163953Srrs /* could not pull a INIT chunk in cookie */ 1774169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1775169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1776163953Srrs return (NULL); 1777163953Srrs } 1778163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1779163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1780169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1781163953Srrs return (NULL); 1782163953Srrs } 1783163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1784163953Srrs /* 1785163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1786163953Srrs * INIT-ACK follows the INIT chunk 1787163953Srrs */ 1788163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1789163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1790163953Srrs (uint8_t *) & initack_buf); 1791163953Srrs if (initack_cp == NULL) { 1792163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1793169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1794163953Srrs return (NULL); 1795163953Srrs } 1796163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1797163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1798163953Srrs return (NULL); 1799163953Srrs } 1800163953Srrs /* 1801163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1802163953Srrs * "initack_limit" value. This is because the chk_length field 1803163953Srrs * includes the length of the cookie, but the cookie is omitted when 1804163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1805163953Srrs */ 1806163953Srrs initack_limit = offset + cookie_len; 1807163953Srrs 1808163953Srrs /* 1809163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1810163953Srrs * and popluate 1811163953Srrs */ 1812171531Srrs 1813171531Srrs /* 1814171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 1815171531Srrs * We do this since in effect we only use the p argument when the 1816171531Srrs * socket is unbound and we must do an implicit bind. Since we are 1817171531Srrs * getting a cookie, we cannot be unbound. 1818171531Srrs */ 1819163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1820171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 1821171531Srrs (struct thread *)NULL 1822171531Srrs ); 1823163953Srrs if (stcb == NULL) { 1824163953Srrs struct mbuf *op_err; 1825163953Srrs 1826163953Srrs /* memory problem? */ 1827169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1828169420Srrs "process_cookie_new: no room for another TCB!\n"); 1829163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1830170140Srrs 1831163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1832170181Srrs sh, op_err, vrf_id); 1833163953Srrs return (NULL); 1834163953Srrs } 1835163953Srrs /* get the correct sctp_nets */ 1836169420Srrs if (netp) 1837169420Srrs *netp = sctp_findnet(stcb, init_src); 1838169420Srrs 1839163953Srrs asoc = &stcb->asoc; 1840163953Srrs /* get scope variables out of cookie */ 1841163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1842163953Srrs asoc->site_scope = cookie->site_scope; 1843163953Srrs asoc->local_scope = cookie->local_scope; 1844163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1845163953Srrs 1846163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1847163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1848163953Srrs struct mbuf *op_err; 1849163953Srrs 1850163953Srrs /* 1851163953Srrs * Houston we have a problem. The EP changed while the 1852163953Srrs * cookie was in flight. Only recourse is to abort the 1853163953Srrs * association. 1854163953Srrs */ 1855170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1856163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1857163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1858170181Srrs sh, op_err, vrf_id); 1859172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1860172090Srrs SCTP_TCB_UNLOCK(stcb); 1861172090Srrs SCTP_SOCKET_LOCK(so, 1); 1862172090Srrs SCTP_TCB_LOCK(stcb); 1863172090Srrs#endif 1864171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 1865171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1866172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1867172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1868172090Srrs#endif 1869172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1870163953Srrs return (NULL); 1871163953Srrs } 1872163953Srrs /* process the INIT-ACK info (my info) */ 1873163953Srrs old_tag = asoc->my_vtag; 1874163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1875163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1876163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1877163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1878163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1879163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1880163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1881163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1882163953Srrs 1883163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1884163953Srrs 1885163953Srrs /* process the INIT info (peer's info) */ 1886169420Srrs if (netp) 1887169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1888169420Srrs else 1889169420Srrs retval = 0; 1890163953Srrs if (retval < 0) { 1891170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1892172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1893172090Srrs SCTP_TCB_UNLOCK(stcb); 1894172090Srrs SCTP_SOCKET_LOCK(so, 1); 1895172090Srrs SCTP_TCB_LOCK(stcb); 1896172090Srrs#endif 1897171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1898172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1899172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1900172090Srrs#endif 1901172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1902163953Srrs return (NULL); 1903163953Srrs } 1904163953Srrs /* load all addresses */ 1905163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1906163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1907163953Srrs init_src)) { 1908170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1909172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1910172090Srrs SCTP_TCB_UNLOCK(stcb); 1911172090Srrs SCTP_SOCKET_LOCK(so, 1); 1912172090Srrs SCTP_TCB_LOCK(stcb); 1913172090Srrs#endif 1914171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1915172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1916172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1917172090Srrs#endif 1918172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1919163953Srrs return (NULL); 1920163953Srrs } 1921163953Srrs /* 1922163953Srrs * verify any preceding AUTH chunk that was skipped 1923163953Srrs */ 1924163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1925163953Srrs sctp_auth_get_cookie_params(stcb, m, 1926163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1927163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1928163953Srrs if (auth_skipped) { 1929163953Srrs struct sctp_auth_chunk *auth; 1930163953Srrs 1931163953Srrs auth = (struct sctp_auth_chunk *) 1932166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1933169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1934163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1935169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1936169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1937171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1938172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1939172090Srrs SCTP_TCB_UNLOCK(stcb); 1940172090Srrs SCTP_SOCKET_LOCK(so, 1); 1941172090Srrs SCTP_TCB_LOCK(stcb); 1942172090Srrs#endif 1943171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1944172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1945172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1946172090Srrs#endif 1947172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1948163953Srrs return (NULL); 1949163953Srrs } else { 1950163953Srrs /* remaining chunks checked... good to go */ 1951163953Srrs stcb->asoc.authenticated = 1; 1952163953Srrs } 1953163953Srrs } 1954163953Srrs /* update current state */ 1955169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1956171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1957163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1958163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1959163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1960163953Srrs } 1961165220Srrs sctp_stop_all_cookie_timers(stcb); 1962163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1963163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1964163953Srrs 1965163953Srrs /* 1966163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1967163953Srrs * addresses that need to get added to the peer (eg. addresses 1968163953Srrs * changed while cookie echo in flight). This needs to be done 1969163953Srrs * after we go to the OPEN state to do the correct asconf 1970163953Srrs * processing. else, make sure we have the correct addresses in our 1971163953Srrs * lists 1972163953Srrs */ 1973163953Srrs 1974163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1975163953Srrs /* pull in local_address (our "from" address) */ 1976163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1977163953Srrs /* source addr is IPv4 */ 1978163953Srrs sin = (struct sockaddr_in *)initack_src; 1979163953Srrs memset(sin, 0, sizeof(*sin)); 1980163953Srrs sin->sin_family = AF_INET; 1981163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1982163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1983163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1984163953Srrs /* source addr is IPv6 */ 1985163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1986163953Srrs memset(sin6, 0, sizeof(*sin6)); 1987163953Srrs sin6->sin6_family = AF_INET6; 1988163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1989163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1990163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1991163953Srrs sizeof(sin6->sin6_addr)); 1992163953Srrs } else { 1993170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1994172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1995172090Srrs SCTP_TCB_UNLOCK(stcb); 1996172090Srrs SCTP_SOCKET_LOCK(so, 1); 1997172090Srrs SCTP_TCB_LOCK(stcb); 1998172090Srrs#endif 1999171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2000172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2001172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2002172090Srrs#endif 2003172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2004163953Srrs return (NULL); 2005163953Srrs } 2006163953Srrs 2007163953Srrs /* set up to notify upper layer */ 2008163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2009163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2010163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2011163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2012163953Srrs /* 2013163953Srrs * This is an endpoint that called connect() how it got a 2014163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2015163953Srrs * the INIT was sent, but before it got there.. a complete 2016163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2017163953Srrs * should have went to the other code.. not here.. oh well.. 2018163953Srrs * a bit of protection is worth having.. 2019163953Srrs */ 2020163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2021172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2022172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2023172090Srrs SCTP_TCB_UNLOCK(stcb); 2024172090Srrs SCTP_SOCKET_LOCK(so, 1); 2025172090Srrs SCTP_TCB_LOCK(stcb); 2026172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2027172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2028172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2029172090Srrs return (NULL); 2030172090Srrs } 2031172090Srrs#endif 2032172090Srrs soisconnected(stcb->sctp_socket); 2033172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2034172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2035172090Srrs#endif 2036163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2037163953Srrs (inp->sctp_socket->so_qlimit)) { 2038163953Srrs /* 2039163953Srrs * We don't want to do anything with this one. Since it is 2040163953Srrs * the listening guy. The timer will get started for 2041163953Srrs * accepted connections in the caller. 2042163953Srrs */ 2043163953Srrs ; 2044163953Srrs } 2045163953Srrs /* since we did not send a HB make sure we don't double things */ 2046169420Srrs if ((netp) && (*netp)) 2047169420Srrs (*netp)->hb_responded = 1; 2048163953Srrs 2049163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2050163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2051163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2052163953Srrs } 2053165220Srrs /* calculate the RTT */ 2054171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2055170642Srrs if ((netp) && (*netp)) { 2056169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2057171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2058170642Srrs } 2059172190Srrs /* respond with a COOKIE-ACK */ 2060163953Srrs sctp_send_cookie_ack(stcb); 2061172190Srrs 2062172190Srrs /* 2063172190Srrs * check the address lists for any ASCONFs that need to be sent 2064172190Srrs * AFTER the cookie-ack is sent 2065172190Srrs */ 2066172190Srrs sctp_check_address_list(stcb, m, 2067172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2068172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2069172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2070172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2071172190Srrs 2072172190Srrs 2073163953Srrs return (stcb); 2074163953Srrs} 2075163953Srrs 2076163953Srrs 2077163953Srrs/* 2078163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2079163953Srrs * existing (non-NULL) TCB 2080163953Srrs */ 2081163953Srrsstatic struct mbuf * 2082163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2083163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2084163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2085169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2086170181Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id) 2087163953Srrs{ 2088163953Srrs struct sctp_state_cookie *cookie; 2089163953Srrs struct sockaddr_in6 sin6; 2090163953Srrs struct sockaddr_in sin; 2091163953Srrs struct sctp_tcb *l_stcb = *stcb; 2092163953Srrs struct sctp_inpcb *l_inp; 2093163953Srrs struct sockaddr *to; 2094163953Srrs sctp_assoc_t sac_restart_id; 2095163953Srrs struct sctp_pcb *ep; 2096163953Srrs struct mbuf *m_sig; 2097163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2098163953Srrs uint8_t *sig; 2099163953Srrs uint8_t cookie_ok = 0; 2100163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2101163953Srrs unsigned int cookie_len; 2102163953Srrs struct timeval now; 2103163953Srrs struct timeval time_expires; 2104163953Srrs struct sockaddr_storage dest_store; 2105163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2106163953Srrs struct ip *iph; 2107163953Srrs int notification = 0; 2108163953Srrs struct sctp_nets *netl; 2109163953Srrs int had_a_existing_tcb = 0; 2110163953Srrs 2111169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2112169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2113163953Srrs 2114163953Srrs if (inp_p == NULL) { 2115163953Srrs return (NULL); 2116163953Srrs } 2117163953Srrs /* First get the destination address setup too. */ 2118163953Srrs iph = mtod(m, struct ip *); 2119163953Srrs if (iph->ip_v == IPVERSION) { 2120163953Srrs /* its IPv4 */ 2121170140Srrs struct sockaddr_in *lsin; 2122163953Srrs 2123170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 2124170140Srrs memset(lsin, 0, sizeof(*lsin)); 2125170140Srrs lsin->sin_family = AF_INET; 2126170140Srrs lsin->sin_len = sizeof(*lsin); 2127170140Srrs lsin->sin_port = sh->dest_port; 2128170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2129165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2130163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 2131163953Srrs /* its IPv6 */ 2132163953Srrs struct ip6_hdr *ip6; 2133170140Srrs struct sockaddr_in6 *lsin6; 2134163953Srrs 2135170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2136170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 2137170140Srrs lsin6->sin6_family = AF_INET6; 2138170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2139163953Srrs ip6 = mtod(m, struct ip6_hdr *); 2140170140Srrs lsin6->sin6_port = sh->dest_port; 2141170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 2142166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2143163953Srrs } else { 2144163953Srrs return (NULL); 2145163953Srrs } 2146163953Srrs 2147163953Srrs cookie = &cp->cookie; 2148163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2149163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2150163953Srrs 2151163953Srrs if ((cookie->peerport != sh->src_port) && 2152163953Srrs (cookie->myport != sh->dest_port) && 2153163953Srrs (cookie->my_vtag != sh->v_tag)) { 2154163953Srrs /* 2155163953Srrs * invalid ports or bad tag. Note that we always leave the 2156163953Srrs * v_tag in the header in network order and when we stored 2157163953Srrs * it in the my_vtag slot we also left it in network order. 2158169352Srrs * This maintains the match even though it may be in the 2159163953Srrs * opposite byte order of the machine :-> 2160163953Srrs */ 2161163953Srrs return (NULL); 2162163953Srrs } 2163163953Srrs if (cookie_len > size_of_pkt || 2164163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2165163953Srrs sizeof(struct sctp_init_chunk) + 2166163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2167163953Srrs /* cookie too long! or too small */ 2168163953Srrs return (NULL); 2169163953Srrs } 2170163953Srrs /* 2171163953Srrs * split off the signature into its own mbuf (since it should not be 2172163953Srrs * calculated in the sctp_hmac_m() call). 2173163953Srrs */ 2174163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2175163953Srrs if (sig_offset > size_of_pkt) { 2176163953Srrs /* packet not correct size! */ 2177163953Srrs /* XXX this may already be accounted for earlier... */ 2178163953Srrs return (NULL); 2179163953Srrs } 2180163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2181163953Srrs if (m_sig == NULL) { 2182163953Srrs /* out of memory or ?? */ 2183163953Srrs return (NULL); 2184163953Srrs } 2185163953Srrs /* 2186163953Srrs * compute the signature/digest for the cookie 2187163953Srrs */ 2188163953Srrs ep = &(*inp_p)->sctp_ep; 2189163953Srrs l_inp = *inp_p; 2190163953Srrs if (l_stcb) { 2191163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2192163953Srrs } 2193163953Srrs SCTP_INP_RLOCK(l_inp); 2194163953Srrs if (l_stcb) { 2195163953Srrs SCTP_TCB_LOCK(l_stcb); 2196163953Srrs } 2197163953Srrs /* which cookie is it? */ 2198163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2199163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2200163953Srrs /* it's the old cookie */ 2201169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2202163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2203170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2204163953Srrs } else { 2205163953Srrs /* it's the current cookie */ 2206169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2207163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2208170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2209163953Srrs } 2210163953Srrs /* get the signature */ 2211163953Srrs SCTP_INP_RUNLOCK(l_inp); 2212163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2213163953Srrs if (sig == NULL) { 2214163953Srrs /* couldn't find signature */ 2215164085Srrs sctp_m_freem(m_sig); 2216163953Srrs return (NULL); 2217163953Srrs } 2218163953Srrs /* compare the received digest with the computed digest */ 2219163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2220163953Srrs /* try the old cookie? */ 2221163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2222163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2223163953Srrs /* compute digest with old */ 2224169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2225163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2226170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2227163953Srrs /* compare */ 2228163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2229163953Srrs cookie_ok = 1; 2230163953Srrs } 2231163953Srrs } else { 2232163953Srrs cookie_ok = 1; 2233163953Srrs } 2234163953Srrs 2235163953Srrs /* 2236163953Srrs * Now before we continue we must reconstruct our mbuf so that 2237163953Srrs * normal processing of any other chunks will work. 2238163953Srrs */ 2239163953Srrs { 2240163953Srrs struct mbuf *m_at; 2241163953Srrs 2242163953Srrs m_at = m; 2243165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2244165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2245163953Srrs } 2246165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2247163953Srrs } 2248163953Srrs 2249163953Srrs if (cookie_ok == 0) { 2250169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2251169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2252169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2253169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2254163953Srrs return (NULL); 2255163953Srrs } 2256163953Srrs /* 2257163953Srrs * check the cookie timestamps to be sure it's not stale 2258163953Srrs */ 2259169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2260163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2261169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2262163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2263163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2264163953Srrs /* cookie is stale! */ 2265163953Srrs struct mbuf *op_err; 2266163953Srrs struct sctp_stale_cookie_msg *scm; 2267163953Srrs uint32_t tim; 2268163953Srrs 2269163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2270165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2271163953Srrs if (op_err == NULL) { 2272163953Srrs /* FOOBAR */ 2273163953Srrs return (NULL); 2274163953Srrs } 2275163953Srrs /* pre-reserve some space */ 2276165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2277165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2278165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2279163953Srrs 2280163953Srrs /* Set the len */ 2281165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2282163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2283163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2284163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2285163953Srrs (sizeof(uint32_t)))); 2286163953Srrs /* seconds to usec */ 2287163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2288163953Srrs /* add in usec */ 2289163953Srrs if (tim == 0) 2290163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2291163953Srrs scm->time_usec = htonl(tim); 2292169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2293170181Srrs vrf_id); 2294163953Srrs return (NULL); 2295163953Srrs } 2296163953Srrs /* 2297163953Srrs * Now we must see with the lookup address if we have an existing 2298163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2299163953Srrs * and a INIT collided with us and somewhere the peer sent the 2300163953Srrs * cookie on another address besides the single address our assoc 2301163953Srrs * had for him. In this case we will have one of the tie-tags set at 2302163953Srrs * least AND the address field in the cookie can be used to look it 2303163953Srrs * up. 2304163953Srrs */ 2305163953Srrs to = NULL; 2306163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2307163953Srrs memset(&sin6, 0, sizeof(sin6)); 2308163953Srrs sin6.sin6_family = AF_INET6; 2309163953Srrs sin6.sin6_len = sizeof(sin6); 2310163953Srrs sin6.sin6_port = sh->src_port; 2311163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2312163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2313163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2314163953Srrs to = (struct sockaddr *)&sin6; 2315163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2316163953Srrs memset(&sin, 0, sizeof(sin)); 2317163953Srrs sin.sin_family = AF_INET; 2318163953Srrs sin.sin_len = sizeof(sin); 2319163953Srrs sin.sin_port = sh->src_port; 2320163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2321163953Srrs to = (struct sockaddr *)&sin; 2322169420Srrs } else { 2323169420Srrs /* This should not happen */ 2324169420Srrs return (NULL); 2325163953Srrs } 2326163953Srrs if ((*stcb == NULL) && to) { 2327163953Srrs /* Yep, lets check */ 2328163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2329163953Srrs if (*stcb == NULL) { 2330163953Srrs /* 2331163953Srrs * We should have only got back the same inp. If we 2332163953Srrs * got back a different ep we have a problem. The 2333163953Srrs * original findep got back l_inp and now 2334163953Srrs */ 2335163953Srrs if (l_inp != *inp_p) { 2336169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2337163953Srrs } 2338165220Srrs } else { 2339165220Srrs if (*locked_tcb == NULL) { 2340165220Srrs /* 2341165220Srrs * In this case we found the assoc only 2342165220Srrs * after we locked the create lock. This 2343165220Srrs * means we are in a colliding case and we 2344165220Srrs * must make sure that we unlock the tcb if 2345165220Srrs * its one of the cases where we throw away 2346165220Srrs * the incoming packets. 2347165220Srrs */ 2348165220Srrs *locked_tcb = *stcb; 2349165220Srrs 2350165220Srrs /* 2351165220Srrs * We must also increment the inp ref count 2352165220Srrs * since the ref_count flags was set when we 2353165220Srrs * did not find the TCB, now we found it 2354165220Srrs * which reduces the refcount.. we must 2355165220Srrs * raise it back out to balance it all :-) 2356165220Srrs */ 2357165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2358165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2359169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2360165220Srrs (*stcb)->sctp_ep, l_inp); 2361165220Srrs } 2362165220Srrs } 2363163953Srrs } 2364163953Srrs } 2365169420Srrs if (to == NULL) 2366169420Srrs return (NULL); 2367169420Srrs 2368163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2369163953Srrs if (*stcb == NULL) { 2370163953Srrs /* this is the "normal" case... get a new TCB */ 2371163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2372163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2373170181Srrs auth_skipped, auth_offset, auth_len, vrf_id); 2374163953Srrs } else { 2375163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2376163953Srrs had_a_existing_tcb = 1; 2377163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2378170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2379170181Srrs ¬ification, &sac_restart_id, vrf_id); 2380163953Srrs } 2381163953Srrs 2382163953Srrs if (*stcb == NULL) { 2383163953Srrs /* still no TCB... must be bad cookie-echo */ 2384163953Srrs return (NULL); 2385163953Srrs } 2386163953Srrs /* 2387163953Srrs * Ok, we built an association so confirm the address we sent the 2388163953Srrs * INIT-ACK to. 2389163953Srrs */ 2390163953Srrs netl = sctp_findnet(*stcb, to); 2391163953Srrs /* 2392163953Srrs * This code should in theory NOT run but 2393163953Srrs */ 2394163953Srrs if (netl == NULL) { 2395163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2396163953Srrs int ret; 2397163953Srrs 2398165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2399165220Srrs SCTP_IN_COOKIE_PROC); 2400163953Srrs netl = sctp_findnet(*stcb, to); 2401163953Srrs } 2402163953Srrs if (netl) { 2403163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2404163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2405169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2406163953Srrs netl); 2407163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2408172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2409163953Srrs } 2410163953Srrs } 2411163953Srrs if (*stcb) { 2412163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2413163953Srrs *stcb, NULL); 2414163953Srrs } 2415163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2416163953Srrs if (!had_a_existing_tcb || 2417163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2418163953Srrs /* 2419163953Srrs * If we have a NEW cookie or the connect never 2420163953Srrs * reached the connected state during collision we 2421163953Srrs * must do the TCP accept thing. 2422163953Srrs */ 2423163953Srrs struct socket *so, *oso; 2424163953Srrs struct sctp_inpcb *inp; 2425163953Srrs 2426163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2427163953Srrs /* 2428163953Srrs * For a restart we will keep the same 2429163953Srrs * socket, no need to do anything. I THINK!! 2430163953Srrs */ 2431172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2432163953Srrs return (m); 2433163953Srrs } 2434163953Srrs oso = (*inp_p)->sctp_socket; 2435171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2436163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2437166086Srrs so = sonewconn(oso, 0 2438163953Srrs ); 2439163953Srrs SCTP_TCB_LOCK((*stcb)); 2440171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2441171858Srrs 2442163953Srrs if (so == NULL) { 2443163953Srrs struct mbuf *op_err; 2444163953Srrs 2445172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2446172090Srrs struct socket *pcb_so; 2447172090Srrs 2448172090Srrs#endif 2449163953Srrs /* Too many sockets */ 2450169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2451163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2452163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2453170181Srrs sh, op_err, vrf_id); 2454172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2455172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2456172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2457172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2458172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2459172090Srrs SCTP_TCB_LOCK((*stcb)); 2460172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2461172090Srrs#endif 2462171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2463172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2464172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2465172090Srrs#endif 2466163953Srrs return (NULL); 2467163953Srrs } 2468163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2469166086Srrs SCTP_INP_INCR_REF(inp); 2470166086Srrs /* 2471166086Srrs * We add the unbound flag here so that if we get an 2472166086Srrs * soabort() before we get the move_pcb done, we 2473166086Srrs * will properly cleanup. 2474166086Srrs */ 2475163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2476163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2477163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2478166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2479163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2480163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2481163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2482172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2483163953Srrs inp->sctp_socket = so; 2484163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2485163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2486163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2487163953Srrs inp->inp_starting_point_for_iterator = NULL; 2488163953Srrs /* 2489163953Srrs * copy in the authentication parameters from the 2490163953Srrs * original endpoint 2491163953Srrs */ 2492163953Srrs if (inp->sctp_ep.local_hmacs) 2493163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2494163953Srrs inp->sctp_ep.local_hmacs = 2495163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2496163953Srrs if (inp->sctp_ep.local_auth_chunks) 2497163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2498163953Srrs inp->sctp_ep.local_auth_chunks = 2499163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2500163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2501163953Srrs &inp->sctp_ep.shared_keys); 2502163953Srrs 2503163953Srrs /* 2504163953Srrs * Now we must move it from one hash table to 2505163953Srrs * another and get the tcb in the right place. 2506163953Srrs */ 2507163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2508170056Srrs 2509170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2510170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2511170056Srrs 2512169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2513170056Srrs SCTP_TCB_LOCK((*stcb)); 2514170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2515163953Srrs 2516170056Srrs 2517166086Srrs /* 2518166086Srrs * now we must check to see if we were aborted while 2519166086Srrs * the move was going on and the lock/unlock 2520166086Srrs * happened. 2521166086Srrs */ 2522166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2523166086Srrs /* 2524166086Srrs * yep it was, we leave the assoc attached 2525166086Srrs * to the socket since the sctp_inpcb_free() 2526166086Srrs * call will send an abort for us. 2527166086Srrs */ 2528166086Srrs SCTP_INP_DECR_REF(inp); 2529166086Srrs return (NULL); 2530166086Srrs } 2531166086Srrs SCTP_INP_DECR_REF(inp); 2532163953Srrs /* Switch over to the new guy */ 2533163953Srrs *inp_p = inp; 2534172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2535163953Srrs 2536166086Srrs /* 2537166086Srrs * Pull it from the incomplete queue and wake the 2538166086Srrs * guy 2539166086Srrs */ 2540172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2541172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2542172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2543172090Srrs SCTP_SOCKET_LOCK(so, 1); 2544172090Srrs#endif 2545166086Srrs soisconnected(so); 2546172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2547172118Srrs SCTP_TCB_LOCK((*stcb)); 2548172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2549172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2550172090Srrs#endif 2551163953Srrs return (m); 2552163953Srrs } 2553163953Srrs } 2554163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2555172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2556163953Srrs } 2557163953Srrs return (m); 2558163953Srrs} 2559163953Srrs 2560163953Srrsstatic void 2561163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2562163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2563163953Srrs{ 2564163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2565163953Srrs struct sctp_association *asoc; 2566163953Srrs 2567169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2568169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2569163953Srrs if (stcb == NULL) 2570163953Srrs return; 2571163953Srrs 2572163953Srrs asoc = &stcb->asoc; 2573163953Srrs 2574163953Srrs sctp_stop_all_cookie_timers(stcb); 2575163953Srrs /* process according to association state */ 2576163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2577163953Srrs /* state change only needed when I am in right state */ 2578169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2579171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2580163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2581163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2582163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2583163953Srrs 2584163953Srrs } 2585163953Srrs /* update RTO */ 2586163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2587163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2588163953Srrs if (asoc->overall_error_count == 0) { 2589163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2590171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2591163953Srrs } 2592169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2593172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2594163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2595163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2596172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2597172090Srrs struct socket *so; 2598172090Srrs 2599172090Srrs#endif 2600163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2601172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2602172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2603172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2604172090Srrs SCTP_TCB_UNLOCK(stcb); 2605172090Srrs SCTP_SOCKET_LOCK(so, 1); 2606172090Srrs SCTP_TCB_LOCK(stcb); 2607172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2608172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2609172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2610172090Srrs return; 2611172090Srrs } 2612172090Srrs#endif 2613172090Srrs soisconnected(stcb->sctp_socket); 2614172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2615172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2616172090Srrs#endif 2617163953Srrs } 2618163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2619163953Srrs stcb, net); 2620163953Srrs /* 2621163953Srrs * since we did not send a HB make sure we don't double 2622163953Srrs * things 2623163953Srrs */ 2624163953Srrs net->hb_responded = 1; 2625163953Srrs 2626163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2627163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2628163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2629163953Srrs stcb->sctp_ep, stcb, NULL); 2630163953Srrs } 2631163953Srrs /* 2632171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2633171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2634171572Srrs * in flight) 2635163953Srrs */ 2636163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2637163953Srrs (stcb->asoc.peer_supports_asconf) && 2638163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2639171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2640163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2641163953Srrs stcb->sctp_ep, stcb, 2642163953Srrs stcb->asoc.primary_destination); 2643171572Srrs#else 2644172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2645172190Srrs SCTP_ADDR_NOT_LOCKED); 2646171572Srrs#endif 2647163953Srrs } 2648163953Srrs } 2649163953Srrs /* Toss the cookie if I can */ 2650163953Srrs sctp_toss_old_cookies(stcb, asoc); 2651163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2652163953Srrs /* Restart the timer if we have pending data */ 2653163953Srrs struct sctp_tmit_chunk *chk; 2654163953Srrs 2655163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2656163953Srrs if (chk) { 2657163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2658163953Srrs stcb, chk->whoTo); 2659163953Srrs } 2660163953Srrs } 2661163953Srrs} 2662163953Srrs 2663163953Srrsstatic void 2664163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2665163953Srrs struct sctp_tcb *stcb) 2666163953Srrs{ 2667163953Srrs struct sctp_nets *net; 2668163953Srrs struct sctp_tmit_chunk *lchk; 2669163953Srrs uint32_t tsn; 2670163953Srrs 2671163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2672163953Srrs return; 2673163953Srrs } 2674163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2675163953Srrs tsn = ntohl(cp->tsn); 2676163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2677163953Srrs /* Also we make sure we disable the nonce_wait */ 2678163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2679163953Srrs if (lchk == NULL) { 2680163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2681163953Srrs } else { 2682163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2683163953Srrs } 2684163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2685163953Srrs stcb->asoc.nonce_sum_check = 0; 2686163953Srrs 2687163953Srrs /* Find where it was sent, if possible */ 2688163953Srrs net = NULL; 2689163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2690163953Srrs while (lchk) { 2691163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2692163953Srrs net = lchk->whoTo; 2693163953Srrs break; 2694163953Srrs } 2695163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2696163953Srrs break; 2697163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2698163953Srrs } 2699163953Srrs if (net == NULL) 2700163953Srrs /* default is we use the primary */ 2701163953Srrs net = stcb->asoc.primary_destination; 2702163953Srrs 2703163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2704163953Srrs /* 2705171440Srrs * JRS - Use the congestion control given in the pluggable 2706171440Srrs * CC module 2707171440Srrs */ 2708171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2709171440Srrs /* 2710163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2711163953Srrs * the next sending seq i.e. the resync_tsn. 2712163953Srrs */ 2713163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2714163953Srrs } 2715163953Srrs /* 2716163953Srrs * We always send a CWR this way if our previous one was lost our 2717163953Srrs * peer will get an update, or if it is not time again to reduce we 2718163953Srrs * still get the cwr to the peer. 2719163953Srrs */ 2720163953Srrs sctp_send_cwr(stcb, net, tsn); 2721163953Srrs} 2722163953Srrs 2723163953Srrsstatic void 2724163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2725163953Srrs{ 2726163953Srrs /* 2727163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2728163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2729163953Srrs * If so remove it. 2730163953Srrs */ 2731163953Srrs struct sctp_tmit_chunk *chk; 2732163953Srrs struct sctp_ecne_chunk *ecne; 2733163953Srrs 2734163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2735163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2736163953Srrs continue; 2737163953Srrs } 2738163953Srrs /* 2739163953Srrs * Look for and remove if it is the right TSN. Since there 2740163953Srrs * is only ONE ECNE on the control queue at any one time we 2741163953Srrs * don't need to worry about more than one! 2742163953Srrs */ 2743163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2744163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2745163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2746163953Srrs /* this covers this ECNE, we can remove it */ 2747163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2748163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2749163953Srrs sctp_next); 2750163953Srrs if (chk->data) { 2751163953Srrs sctp_m_freem(chk->data); 2752163953Srrs chk->data = NULL; 2753163953Srrs } 2754163953Srrs stcb->asoc.ctrl_queue_cnt--; 2755163953Srrs sctp_free_a_chunk(stcb, chk); 2756163953Srrs break; 2757163953Srrs } 2758163953Srrs } 2759163953Srrs} 2760163953Srrs 2761163953Srrsstatic void 2762163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2763163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2764163953Srrs{ 2765163953Srrs struct sctp_association *asoc; 2766163953Srrs 2767172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2768172090Srrs struct socket *so; 2769172090Srrs 2770172090Srrs#endif 2771172090Srrs 2772169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2773169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2774163953Srrs if (stcb == NULL) 2775163953Srrs return; 2776163953Srrs 2777163953Srrs asoc = &stcb->asoc; 2778163953Srrs /* process according to association state */ 2779163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2780163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2781171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2782171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 2783163953Srrs SCTP_TCB_UNLOCK(stcb); 2784163953Srrs return; 2785163953Srrs } 2786163953Srrs /* notify upper layer protocol */ 2787163953Srrs if (stcb->sctp_socket) { 2788172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2789163953Srrs /* are the queues empty? they should be */ 2790163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2791163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2792163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2793172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 2794163953Srrs } 2795163953Srrs } 2796163953Srrs /* stop the timer */ 2797171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2798163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2799163953Srrs /* free the TCB */ 2800171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2801171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 2802172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2803172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2804172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2805172090Srrs SCTP_TCB_UNLOCK(stcb); 2806172090Srrs SCTP_SOCKET_LOCK(so, 1); 2807172090Srrs SCTP_TCB_LOCK(stcb); 2808172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2809172090Srrs#endif 2810171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2811172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2812172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2813172090Srrs#endif 2814163953Srrs return; 2815163953Srrs} 2816163953Srrs 2817163953Srrsstatic int 2818163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2819163953Srrs struct sctp_nets *net, uint8_t flg) 2820163953Srrs{ 2821163953Srrs switch (desc->chunk_type) { 2822163953Srrs case SCTP_DATA: 2823163953Srrs /* find the tsn to resend (possibly */ 2824163953Srrs { 2825163953Srrs uint32_t tsn; 2826163953Srrs struct sctp_tmit_chunk *tp1; 2827163953Srrs 2828163953Srrs tsn = ntohl(desc->tsn_ifany); 2829163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2830163953Srrs while (tp1) { 2831163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2832163953Srrs /* found it */ 2833163953Srrs break; 2834163953Srrs } 2835163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2836163953Srrs MAX_TSN)) { 2837163953Srrs /* not found */ 2838163953Srrs tp1 = NULL; 2839163953Srrs break; 2840163953Srrs } 2841163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2842163953Srrs } 2843163953Srrs if (tp1 == NULL) { 2844163953Srrs /* 2845163953Srrs * Do it the other way , aka without paying 2846163953Srrs * attention to queue seq order. 2847163953Srrs */ 2848163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2849163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2850163953Srrs while (tp1) { 2851163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2852163953Srrs /* found it */ 2853163953Srrs break; 2854163953Srrs } 2855163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2856163953Srrs } 2857163953Srrs } 2858163953Srrs if (tp1 == NULL) { 2859163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2860163953Srrs } 2861163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2862163953Srrs uint8_t *ddp; 2863163953Srrs 2864163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2865163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2866163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2867163953Srrs return (0); 2868163953Srrs } 2869163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2870163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2871163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2872163953Srrs return (0); 2873163953Srrs } 2874163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2875163953Srrs sizeof(struct sctp_data_chunk)); 2876163953Srrs { 2877163953Srrs unsigned int iii; 2878163953Srrs 2879163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2880163953Srrs iii++) { 2881163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2882163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2883163953Srrs return (-1); 2884163953Srrs } 2885163953Srrs } 2886163953Srrs } 2887163953Srrs /* 2888163953Srrs * We zero out the nonce so resync not 2889163953Srrs * needed 2890163953Srrs */ 2891163953Srrs tp1->rec.data.ect_nonce = 0; 2892163953Srrs 2893163953Srrs if (tp1->do_rtt) { 2894163953Srrs /* 2895163953Srrs * this guy had a RTO calculation 2896163953Srrs * pending on it, cancel it 2897163953Srrs */ 2898163953Srrs tp1->do_rtt = 0; 2899163953Srrs } 2900163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2901163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2902163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2903163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2904163953Srrs /* 2905163953Srrs * mark it as if we were doing a FR, since 2906163953Srrs * we will be getting gap ack reports behind 2907163953Srrs * the info from the router. 2908163953Srrs */ 2909163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2910163953Srrs /* 2911163953Srrs * mark the tsn with what sequences can 2912163953Srrs * cause a new FR. 2913163953Srrs */ 2914163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2915163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2916163953Srrs } else { 2917163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2918163953Srrs } 2919163953Srrs 2920163953Srrs /* restart the timer */ 2921163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2922171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2923163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2924163953Srrs stcb, tp1->whoTo); 2925163953Srrs 2926163953Srrs /* fix counts and things */ 2927170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 2928170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2929170744Srrs tp1->whoTo->flight_size, 2930170744Srrs tp1->book_size, 2931170744Srrs (uintptr_t) stcb, 2932170744Srrs tp1->rec.data.TSN_seq); 2933170744Srrs } 2934168709Srrs sctp_flight_size_decrease(tp1); 2935168709Srrs sctp_total_flight_decrease(stcb, tp1); 2936163953Srrs } { 2937163953Srrs /* audit code */ 2938163953Srrs unsigned int audit; 2939163953Srrs 2940163953Srrs audit = 0; 2941163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2942163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2943163953Srrs audit++; 2944163953Srrs } 2945163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2946163953Srrs sctp_next) { 2947163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2948163953Srrs audit++; 2949163953Srrs } 2950163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2951169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2952163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2953163953Srrs#ifndef SCTP_AUDITING_ENABLED 2954163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2955163953Srrs#endif 2956163953Srrs } 2957163953Srrs } 2958163953Srrs } 2959163953Srrs break; 2960163953Srrs case SCTP_ASCONF: 2961163953Srrs { 2962163953Srrs struct sctp_tmit_chunk *asconf; 2963163953Srrs 2964163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2965163953Srrs sctp_next) { 2966163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2967163953Srrs break; 2968163953Srrs } 2969163953Srrs } 2970163953Srrs if (asconf) { 2971163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2972163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2973163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2974163953Srrs asconf->snd_count--; 2975163953Srrs } 2976163953Srrs } 2977163953Srrs break; 2978163953Srrs case SCTP_INITIATION: 2979163953Srrs /* resend the INIT */ 2980163953Srrs stcb->asoc.dropped_special_cnt++; 2981163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2982163953Srrs /* 2983163953Srrs * If we can get it in, in a few attempts we do 2984163953Srrs * this, otherwise we let the timer fire. 2985163953Srrs */ 2986163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2987171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2988172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 2989163953Srrs } 2990163953Srrs break; 2991163953Srrs case SCTP_SELECTIVE_ACK: 2992163953Srrs /* resend the sack */ 2993163953Srrs sctp_send_sack(stcb); 2994163953Srrs break; 2995163953Srrs case SCTP_HEARTBEAT_REQUEST: 2996163953Srrs /* resend a demand HB */ 2997171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 2998171440Srrs /* 2999171440Srrs * Only retransmit if we KNOW we wont destroy the 3000171440Srrs * tcb 3001171440Srrs */ 3002171440Srrs (void)sctp_send_hb(stcb, 1, net); 3003171440Srrs } 3004163953Srrs break; 3005163953Srrs case SCTP_SHUTDOWN: 3006163953Srrs sctp_send_shutdown(stcb, net); 3007163953Srrs break; 3008163953Srrs case SCTP_SHUTDOWN_ACK: 3009163953Srrs sctp_send_shutdown_ack(stcb, net); 3010163953Srrs break; 3011163953Srrs case SCTP_COOKIE_ECHO: 3012163953Srrs { 3013163953Srrs struct sctp_tmit_chunk *cookie; 3014163953Srrs 3015163953Srrs cookie = NULL; 3016163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3017163953Srrs sctp_next) { 3018163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3019163953Srrs break; 3020163953Srrs } 3021163953Srrs } 3022163953Srrs if (cookie) { 3023163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3024163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3025163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3026163953Srrs sctp_stop_all_cookie_timers(stcb); 3027163953Srrs } 3028163953Srrs } 3029163953Srrs break; 3030163953Srrs case SCTP_COOKIE_ACK: 3031163953Srrs sctp_send_cookie_ack(stcb); 3032163953Srrs break; 3033163953Srrs case SCTP_ASCONF_ACK: 3034163953Srrs /* resend last asconf ack */ 3035171990Srrs sctp_send_asconf_ack(stcb); 3036163953Srrs break; 3037163953Srrs case SCTP_FORWARD_CUM_TSN: 3038163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3039163953Srrs break; 3040163953Srrs /* can't do anything with these */ 3041163953Srrs case SCTP_PACKET_DROPPED: 3042163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3043163953Srrs case SCTP_HEARTBEAT_ACK: 3044163953Srrs case SCTP_ABORT_ASSOCIATION: 3045163953Srrs case SCTP_OPERATION_ERROR: 3046163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3047163953Srrs case SCTP_ECN_ECHO: 3048163953Srrs case SCTP_ECN_CWR: 3049163953Srrs default: 3050163953Srrs break; 3051163953Srrs } 3052163953Srrs return (0); 3053163953Srrs} 3054163953Srrs 3055163953Srrsvoid 3056163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3057163953Srrs{ 3058163953Srrs int i; 3059163953Srrs uint16_t temp; 3060163953Srrs 3061163953Srrs /* 3062163953Srrs * We set things to 0xffff since this is the last delivered sequence 3063163953Srrs * and we will be sending in 0 after the reset. 3064163953Srrs */ 3065163953Srrs 3066163953Srrs if (number_entries) { 3067163953Srrs for (i = 0; i < number_entries; i++) { 3068163953Srrs temp = ntohs(list[i]); 3069163953Srrs if (temp >= stcb->asoc.streamincnt) { 3070163953Srrs continue; 3071163953Srrs } 3072163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3073163953Srrs } 3074163953Srrs } else { 3075163953Srrs list = NULL; 3076163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3077163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3078163953Srrs } 3079163953Srrs } 3080172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3081163953Srrs} 3082163953Srrs 3083163953Srrsstatic void 3084163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3085163953Srrs{ 3086163953Srrs int i; 3087163953Srrs 3088163953Srrs if (number_entries == 0) { 3089163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3090163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3091163953Srrs } 3092163953Srrs } else if (number_entries) { 3093163953Srrs for (i = 0; i < number_entries; i++) { 3094163953Srrs uint16_t temp; 3095163953Srrs 3096163953Srrs temp = ntohs(list[i]); 3097163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3098163953Srrs /* no such stream */ 3099163953Srrs continue; 3100163953Srrs } 3101163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3102163953Srrs } 3103163953Srrs } 3104172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3105163953Srrs} 3106163953Srrs 3107163953Srrs 3108163953Srrsstruct sctp_stream_reset_out_request * 3109163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3110163953Srrs{ 3111163953Srrs struct sctp_association *asoc; 3112163953Srrs struct sctp_stream_reset_out_req *req; 3113163953Srrs struct sctp_stream_reset_out_request *r; 3114163953Srrs struct sctp_tmit_chunk *chk; 3115163953Srrs int len, clen; 3116163953Srrs 3117163953Srrs asoc = &stcb->asoc; 3118163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3119169208Srrs asoc->stream_reset_outstanding = 0; 3120163953Srrs return (NULL); 3121163953Srrs } 3122163953Srrs if (stcb->asoc.str_reset == NULL) { 3123169208Srrs asoc->stream_reset_outstanding = 0; 3124163953Srrs return (NULL); 3125163953Srrs } 3126163953Srrs chk = stcb->asoc.str_reset; 3127163953Srrs if (chk->data == NULL) { 3128163953Srrs return (NULL); 3129163953Srrs } 3130163953Srrs if (bchk) { 3131163953Srrs /* he wants a copy of the chk pointer */ 3132163953Srrs *bchk = chk; 3133163953Srrs } 3134163953Srrs clen = chk->send_size; 3135163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3136163953Srrs r = &req->sr_req; 3137163953Srrs if (ntohl(r->request_seq) == seq) { 3138163953Srrs /* found it */ 3139163953Srrs return (r); 3140163953Srrs } 3141163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3142163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3143163953Srrs /* move to the next one, there can only be a max of two */ 3144163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3145163953Srrs if (ntohl(r->request_seq) == seq) { 3146163953Srrs return (r); 3147163953Srrs } 3148163953Srrs } 3149163953Srrs /* that seq is not here */ 3150163953Srrs return (NULL); 3151163953Srrs} 3152163953Srrs 3153163953Srrsstatic void 3154163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3155163953Srrs{ 3156163953Srrs struct sctp_association *asoc; 3157163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3158163953Srrs 3159163953Srrs if (stcb->asoc.str_reset == NULL) { 3160163953Srrs return; 3161163953Srrs } 3162166675Srrs asoc = &stcb->asoc; 3163166675Srrs 3164171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3165163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3166163953Srrs chk, 3167163953Srrs sctp_next); 3168163953Srrs if (chk->data) { 3169163953Srrs sctp_m_freem(chk->data); 3170163953Srrs chk->data = NULL; 3171163953Srrs } 3172163953Srrs asoc->ctrl_queue_cnt--; 3173163953Srrs sctp_free_a_chunk(stcb, chk); 3174172156Srrs /* sa_ignore NO_NULL_CHK */ 3175163953Srrs stcb->asoc.str_reset = NULL; 3176163953Srrs} 3177163953Srrs 3178163953Srrs 3179163953Srrsstatic int 3180163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3181163953Srrs uint32_t seq, uint32_t action, 3182164139Srrs struct sctp_stream_reset_response *respin) 3183163953Srrs{ 3184163953Srrs uint16_t type; 3185163953Srrs int lparm_len; 3186163953Srrs struct sctp_association *asoc = &stcb->asoc; 3187163953Srrs struct sctp_tmit_chunk *chk; 3188163953Srrs struct sctp_stream_reset_out_request *srparam; 3189163953Srrs int number_entries; 3190163953Srrs 3191163953Srrs if (asoc->stream_reset_outstanding == 0) { 3192163953Srrs /* duplicate */ 3193163953Srrs return (0); 3194163953Srrs } 3195163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3196163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3197163953Srrs if (srparam) { 3198163953Srrs stcb->asoc.str_reset_seq_out++; 3199163953Srrs type = ntohs(srparam->ph.param_type); 3200163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3201163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3202164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3203163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3204163953Srrs if (asoc->stream_reset_outstanding) 3205163953Srrs asoc->stream_reset_outstanding--; 3206163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3207163953Srrs /* do it */ 3208163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3209163953Srrs } else { 3210172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3211163953Srrs } 3212163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3213163953Srrs /* Answered my request */ 3214164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3215163953Srrs if (asoc->stream_reset_outstanding) 3216163953Srrs asoc->stream_reset_outstanding--; 3217163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3218172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3219163953Srrs } 3220163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3221163953Srrs /** 3222163953Srrs * a) Adopt the new in tsn. 3223163953Srrs * b) reset the map 3224163953Srrs * c) Adopt the new out-tsn 3225163953Srrs */ 3226163953Srrs struct sctp_stream_reset_response_tsn *resp; 3227163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3228163953Srrs int abort_flag = 0; 3229163953Srrs 3230163953Srrs if (respin == NULL) { 3231163953Srrs /* huh ? */ 3232163953Srrs return (0); 3233163953Srrs } 3234163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3235163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3236163953Srrs asoc->stream_reset_outstanding--; 3237163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3238163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3239163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3240170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3241163953Srrs if (abort_flag) { 3242163953Srrs return (1); 3243163953Srrs } 3244163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3245163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3246163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3247163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3248163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3249163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3250163953Srrs 3251163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3252163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3253163953Srrs 3254163953Srrs } 3255163953Srrs } 3256163953Srrs /* get rid of the request and get the request flags */ 3257163953Srrs if (asoc->stream_reset_outstanding == 0) { 3258163953Srrs sctp_clean_up_stream_reset(stcb); 3259163953Srrs } 3260163953Srrs } 3261163953Srrs } 3262163953Srrs return (0); 3263163953Srrs} 3264163953Srrs 3265163953Srrsstatic void 3266163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3267163953Srrs struct sctp_tmit_chunk *chk, 3268170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3269163953Srrs{ 3270163953Srrs uint32_t seq; 3271163953Srrs int len, i; 3272163953Srrs int number_entries; 3273163953Srrs uint16_t temp; 3274163953Srrs 3275163953Srrs /* 3276163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3277163953Srrs * seq_in is right. 3278163953Srrs */ 3279163953Srrs struct sctp_association *asoc = &stcb->asoc; 3280163953Srrs 3281163953Srrs seq = ntohl(req->request_seq); 3282163953Srrs if (asoc->str_reset_seq_in == seq) { 3283170992Srrs if (trunc) { 3284170992Srrs /* Can't do it, since they exceeded our buffer size */ 3285170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3286170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3287170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3288170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3289163953Srrs len = ntohs(req->ph.param_length); 3290163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3291163953Srrs for (i = 0; i < number_entries; i++) { 3292163953Srrs temp = ntohs(req->list_of_streams[i]); 3293163953Srrs req->list_of_streams[i] = temp; 3294163953Srrs } 3295163953Srrs /* move the reset action back one */ 3296163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3297163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3298163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3299163953Srrs asoc->str_reset_seq_out, 3300163953Srrs seq, (asoc->sending_seq - 1)); 3301163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3302163953Srrs asoc->str_reset = chk; 3303163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3304163953Srrs stcb->asoc.stream_reset_outstanding++; 3305163953Srrs } else { 3306163953Srrs /* Can't do it, since we have sent one out */ 3307163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3308163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3309163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3310163953Srrs } 3311163953Srrs asoc->str_reset_seq_in++; 3312163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3313163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3314163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3315163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3316163953Srrs } else { 3317163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3318163953Srrs } 3319163953Srrs} 3320163953Srrs 3321163953Srrsstatic int 3322163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3323163953Srrs struct sctp_tmit_chunk *chk, 3324163953Srrs struct sctp_stream_reset_tsn_request *req) 3325163953Srrs{ 3326163953Srrs /* reset all in and out and update the tsn */ 3327163953Srrs /* 3328163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3329163953Srrs * and set cum-ack to it. Also process this selected number as a 3330163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3331163953Srrs */ 3332163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3333163953Srrs struct sctp_association *asoc = &stcb->asoc; 3334163953Srrs int abort_flag = 0; 3335163953Srrs uint32_t seq; 3336163953Srrs 3337163953Srrs seq = ntohl(req->request_seq); 3338163953Srrs if (asoc->str_reset_seq_in == seq) { 3339163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3340163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3341163953Srrs fwdtsn.ch.chunk_flags = 0; 3342163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3343170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3344163953Srrs if (abort_flag) { 3345163953Srrs return (1); 3346163953Srrs } 3347163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3348163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3349163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3350163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3351163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3352163953Srrs /* save off historical data for retrans */ 3353163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3354163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3355163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3356163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3357163953Srrs 3358163953Srrs sctp_add_stream_reset_result_tsn(chk, 3359163953Srrs ntohl(req->request_seq), 3360163953Srrs SCTP_STREAM_RESET_PERFORMED, 3361163953Srrs stcb->asoc.sending_seq, 3362163953Srrs stcb->asoc.mapping_array_base_tsn); 3363163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3364163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3365163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3366163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3367163953Srrs 3368163953Srrs asoc->str_reset_seq_in++; 3369163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3370163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3371163953Srrs stcb->asoc.last_sending_seq[0], 3372163953Srrs stcb->asoc.last_base_tsnsent[0] 3373163953Srrs ); 3374163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3375163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3376163953Srrs stcb->asoc.last_sending_seq[1], 3377163953Srrs stcb->asoc.last_base_tsnsent[1] 3378163953Srrs ); 3379163953Srrs } else { 3380163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3381163953Srrs } 3382163953Srrs return (0); 3383163953Srrs} 3384163953Srrs 3385163953Srrsstatic void 3386163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3387163953Srrs struct sctp_tmit_chunk *chk, 3388170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3389163953Srrs{ 3390163953Srrs uint32_t seq, tsn; 3391163953Srrs int number_entries, len; 3392163953Srrs struct sctp_association *asoc = &stcb->asoc; 3393163953Srrs 3394163953Srrs seq = ntohl(req->request_seq); 3395163953Srrs 3396163953Srrs /* now if its not a duplicate we process it */ 3397163953Srrs if (asoc->str_reset_seq_in == seq) { 3398163953Srrs len = ntohs(req->ph.param_length); 3399163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3400163953Srrs /* 3401163953Srrs * the sender is resetting, handle the list issue.. we must 3402163953Srrs * a) verify if we can do the reset, if so no problem b) If 3403163953Srrs * we can't do the reset we must copy the request. c) queue 3404163953Srrs * it, and setup the data in processor to trigger it off 3405163953Srrs * when needed and dequeue all the queued data. 3406163953Srrs */ 3407163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3408163953Srrs 3409163953Srrs /* move the reset action back one */ 3410163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3411170992Srrs if (trunc) { 3412170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3413170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3414170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3415163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3416163953Srrs /* we can do it now */ 3417163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3418163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3419163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3420163953Srrs } else { 3421163953Srrs /* 3422163953Srrs * we must queue it up and thus wait for the TSN's 3423163953Srrs * to arrive that are at or before tsn 3424163953Srrs */ 3425163953Srrs struct sctp_stream_reset_list *liste; 3426163953Srrs int siz; 3427163953Srrs 3428163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3429163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3430170091Srrs siz, SCTP_M_STRESET); 3431163953Srrs if (liste == NULL) { 3432163953Srrs /* gak out of memory */ 3433163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3434163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3435163953Srrs return; 3436163953Srrs } 3437163953Srrs liste->tsn = tsn; 3438163953Srrs liste->number_entries = number_entries; 3439163953Srrs memcpy(&liste->req, req, 3440163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3441163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3442163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3443163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3444163953Srrs } 3445163953Srrs asoc->str_reset_seq_in++; 3446163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3447163953Srrs /* 3448163953Srrs * one seq back, just echo back last action since my 3449163953Srrs * response was lost. 3450163953Srrs */ 3451163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3452163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3453163953Srrs /* 3454163953Srrs * two seq back, just echo back last action since my 3455163953Srrs * response was lost. 3456163953Srrs */ 3457163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3458163953Srrs } else { 3459163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3460163953Srrs } 3461163953Srrs} 3462163953Srrs 3463170992Srrs#ifdef __GNUC__ 3464170992Srrs__attribute__((noinline)) 3465170992Srrs#endif 3466170992Srrs static int 3467170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3468170992Srrs struct sctp_stream_reset_out_req *sr_req) 3469163953Srrs{ 3470163953Srrs int chk_length, param_len, ptype; 3471170992Srrs struct sctp_paramhdr pstore; 3472170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3473170992Srrs 3474163953Srrs uint32_t seq; 3475163953Srrs int num_req = 0; 3476170992Srrs int trunc = 0; 3477163953Srrs struct sctp_tmit_chunk *chk; 3478163953Srrs struct sctp_chunkhdr *ch; 3479163953Srrs struct sctp_paramhdr *ph; 3480166675Srrs int ret_code = 0; 3481166675Srrs int num_param = 0; 3482163953Srrs 3483163953Srrs /* now it may be a reset or a reset-response */ 3484163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3485163953Srrs 3486163953Srrs /* setup for adding the response */ 3487163953Srrs sctp_alloc_a_chunk(stcb, chk); 3488163953Srrs if (chk == NULL) { 3489163953Srrs return (ret_code); 3490163953Srrs } 3491163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3492169208Srrs chk->rec.chunk_id.can_take_data = 0; 3493163953Srrs chk->asoc = &stcb->asoc; 3494163953Srrs chk->no_fr_allowed = 0; 3495163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3496166023Srrs chk->book_size_scale = 0; 3497165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3498163953Srrs if (chk->data == NULL) { 3499163953Srrsstrres_nochunk: 3500163953Srrs if (chk->data) { 3501163953Srrs sctp_m_freem(chk->data); 3502163953Srrs chk->data = NULL; 3503163953Srrs } 3504163953Srrs sctp_free_a_chunk(stcb, chk); 3505163953Srrs return (ret_code); 3506163953Srrs } 3507165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3508163953Srrs 3509163953Srrs /* setup chunk parameters */ 3510163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3511163953Srrs chk->snd_count = 0; 3512163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3513163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3514163953Srrs 3515163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3516163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3517163953Srrs ch->chunk_flags = 0; 3518163953Srrs ch->chunk_length = htons(chk->send_size); 3519165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3520170992Srrs offset += sizeof(struct sctp_chunkhdr); 3521163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3522170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3523170992Srrs if (ph == NULL) 3524170992Srrs break; 3525163953Srrs param_len = ntohs(ph->param_length); 3526163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3527163953Srrs /* bad param */ 3528163953Srrs break; 3529163953Srrs } 3530170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3531170992Srrs (uint8_t *) & cstore); 3532163953Srrs ptype = ntohs(ph->param_type); 3533163953Srrs num_param++; 3534170994Srrs if (param_len > (int)sizeof(cstore)) { 3535170992Srrs trunc = 1; 3536170992Srrs } else { 3537170992Srrs trunc = 0; 3538170992Srrs } 3539170992Srrs 3540163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3541163953Srrs /* hit the max of parameters already sorry.. */ 3542163953Srrs break; 3543163953Srrs } 3544163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3545163953Srrs struct sctp_stream_reset_out_request *req_out; 3546163953Srrs 3547163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3548163953Srrs num_req++; 3549163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3550163953Srrs seq = ntohl(req_out->response_seq); 3551163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3552163953Srrs /* implicit ack */ 3553169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3554163953Srrs } 3555163953Srrs } 3556170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3557163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3558163953Srrs struct sctp_stream_reset_in_request *req_in; 3559163953Srrs 3560163953Srrs num_req++; 3561170992Srrs 3562163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3563170992Srrs 3564170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3565163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3566163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3567163953Srrs 3568163953Srrs num_req++; 3569163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3570170992Srrs 3571163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3572163953Srrs ret_code = 1; 3573163953Srrs goto strres_nochunk; 3574163953Srrs } 3575163953Srrs /* no more */ 3576163953Srrs break; 3577163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3578163953Srrs struct sctp_stream_reset_response *resp; 3579163953Srrs uint32_t result; 3580163953Srrs 3581163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3582163953Srrs seq = ntohl(resp->response_seq); 3583163953Srrs result = ntohl(resp->result); 3584163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3585163953Srrs ret_code = 1; 3586163953Srrs goto strres_nochunk; 3587163953Srrs } 3588163953Srrs } else { 3589163953Srrs break; 3590163953Srrs } 3591170992Srrs offset += SCTP_SIZE32(param_len); 3592163953Srrs chk_length -= SCTP_SIZE32(param_len); 3593163953Srrs } 3594163953Srrs if (num_req == 0) { 3595163953Srrs /* we have no response free the stuff */ 3596163953Srrs goto strres_nochunk; 3597163953Srrs } 3598163953Srrs /* ok we have a chunk to link in */ 3599163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3600163953Srrs chk, 3601163953Srrs sctp_next); 3602163953Srrs stcb->asoc.ctrl_queue_cnt++; 3603163953Srrs return (ret_code); 3604163953Srrs} 3605163953Srrs 3606163953Srrs/* 3607163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3608163953Srrs * to handle this, either we get the whole packet and must disect it 3609163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3610163953Srrs * from a middle box that did the disectting for us. 3611163953Srrs */ 3612163953Srrsstatic void 3613163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3614170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3615163953Srrs{ 3616163953Srrs uint32_t bottle_bw, on_queue; 3617163953Srrs uint16_t trunc_len; 3618163953Srrs unsigned int chlen; 3619163953Srrs unsigned int at; 3620163953Srrs struct sctp_chunk_desc desc; 3621163953Srrs struct sctp_chunkhdr *ch; 3622163953Srrs 3623163953Srrs chlen = ntohs(cp->ch.chunk_length); 3624163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3625163953Srrs /* XXX possible chlen underflow */ 3626163953Srrs if (chlen == 0) { 3627163953Srrs ch = NULL; 3628163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3629163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3630163953Srrs } else { 3631163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3632163953Srrs chlen -= sizeof(struct sctphdr); 3633163953Srrs /* XXX possible chlen underflow */ 3634163953Srrs memset(&desc, 0, sizeof(desc)); 3635163953Srrs } 3636163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3637170781Srrs if (trunc_len > limit) { 3638170781Srrs trunc_len = limit; 3639170781Srrs } 3640163953Srrs /* now the chunks themselves */ 3641163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3642163953Srrs desc.chunk_type = ch->chunk_type; 3643163953Srrs /* get amount we need to move */ 3644163953Srrs at = ntohs(ch->chunk_length); 3645163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3646163953Srrs /* corrupt chunk, maybe at the end? */ 3647163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3648163953Srrs break; 3649163953Srrs } 3650163953Srrs if (trunc_len == 0) { 3651163953Srrs /* we are supposed to have all of it */ 3652163953Srrs if (at > chlen) { 3653163953Srrs /* corrupt skip it */ 3654163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3655163953Srrs break; 3656163953Srrs } 3657163953Srrs } else { 3658163953Srrs /* is there enough of it left ? */ 3659163953Srrs if (desc.chunk_type == SCTP_DATA) { 3660163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3661163953Srrs sizeof(desc.data_bytes))) { 3662163953Srrs break; 3663163953Srrs } 3664163953Srrs } else { 3665163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3666163953Srrs break; 3667163953Srrs } 3668163953Srrs } 3669163953Srrs } 3670163953Srrs if (desc.chunk_type == SCTP_DATA) { 3671163953Srrs /* can we get out the tsn? */ 3672163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3673163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3674163953Srrs 3675163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3676163953Srrs /* yep */ 3677163953Srrs struct sctp_data_chunk *dcp; 3678163953Srrs uint8_t *ddp; 3679163953Srrs unsigned int iii; 3680163953Srrs 3681163953Srrs dcp = (struct sctp_data_chunk *)ch; 3682163953Srrs ddp = (uint8_t *) (dcp + 1); 3683163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3684163953Srrs desc.data_bytes[iii] = ddp[iii]; 3685163953Srrs } 3686163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3687163953Srrs } else { 3688163953Srrs /* nope we are done. */ 3689163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3690163953Srrs break; 3691163953Srrs } 3692163953Srrs } else { 3693163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3694163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3695163953Srrs } 3696163953Srrs 3697163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3698163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3699163953Srrs break; 3700163953Srrs } 3701163953Srrs if (SCTP_SIZE32(at) > chlen) { 3702163953Srrs break; 3703163953Srrs } 3704163953Srrs chlen -= SCTP_SIZE32(at); 3705163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3706163953Srrs /* done, none left */ 3707163953Srrs break; 3708163953Srrs } 3709163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3710163953Srrs } 3711163953Srrs /* Now update any rwnd --- possibly */ 3712163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3713163953Srrs /* From a peer, we get a rwnd report */ 3714163953Srrs uint32_t a_rwnd; 3715163953Srrs 3716163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3717163953Srrs 3718163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3719163953Srrs on_queue = ntohl(cp->current_onq); 3720163953Srrs if (bottle_bw && on_queue) { 3721163953Srrs /* a rwnd report is in here */ 3722163953Srrs if (bottle_bw > on_queue) 3723163953Srrs a_rwnd = bottle_bw - on_queue; 3724163953Srrs else 3725163953Srrs a_rwnd = 0; 3726163953Srrs 3727163953Srrs if (a_rwnd == 0) 3728163953Srrs stcb->asoc.peers_rwnd = 0; 3729163953Srrs else { 3730163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3731163953Srrs stcb->asoc.peers_rwnd = 3732163953Srrs a_rwnd - stcb->asoc.total_flight; 3733163953Srrs } else { 3734163953Srrs stcb->asoc.peers_rwnd = 0; 3735163953Srrs } 3736163953Srrs if (stcb->asoc.peers_rwnd < 3737163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3738163953Srrs /* SWS sender side engages */ 3739163953Srrs stcb->asoc.peers_rwnd = 0; 3740163953Srrs } 3741163953Srrs } 3742163953Srrs } 3743163953Srrs } else { 3744163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3745163953Srrs } 3746163953Srrs 3747163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3748163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3749163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3750163953Srrs (stcb->asoc.sat_network)) { 3751163953Srrs /* 3752163953Srrs * This is debateable but for sat networks it makes sense 3753163953Srrs * Note if a T3 timer has went off, we will prohibit any 3754163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3755163953Srrs */ 3756171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 3757171440Srrs net, cp, &bottle_bw, &on_queue); 3758163953Srrs } 3759163953Srrs} 3760163953Srrs 3761163953Srrs/* 3762163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3763163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3764163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3765163953Srrs * length of the complete packet outputs: - length: modified to remaining 3766163953Srrs * length after control processing - netp: modified to new sctp_nets after 3767163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3768163953Srrs * bad packet,...) otherwise return the tcb for this packet 3769163953Srrs */ 3770170099Srrs#ifdef __GNUC__ 3771170099Srrs__attribute__((noinline)) 3772170099Srrs#endif 3773170099Srrs static struct sctp_tcb * 3774170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3775170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3776170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3777170181Srrs uint32_t vrf_id) 3778163953Srrs{ 3779163953Srrs struct sctp_association *asoc; 3780163953Srrs uint32_t vtag_in; 3781163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3782170140Srrs uint32_t chk_length; 3783163953Srrs int ret; 3784168299Srrs int abort_no_unlock = 0; 3785163953Srrs 3786163953Srrs /* 3787163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3788163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3789163953Srrs * until we get into jumbo grams and such.. 3790163953Srrs */ 3791166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3792163953Srrs struct sctp_tcb *locked_tcb = stcb; 3793163953Srrs int got_auth = 0; 3794163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3795163953Srrs int auth_skipped = 0; 3796171990Srrs int asconf_cnt = 0; 3797163953Srrs 3798172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3799172090Srrs struct socket *so; 3800172090Srrs 3801172090Srrs#endif 3802172090Srrs 3803169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3804169420Srrs iphlen, *offset, length, stcb); 3805163953Srrs 3806163953Srrs /* validate chunk header length... */ 3807163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3808170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3809170056Srrs ntohs(ch->chunk_length)); 3810170091Srrs if (locked_tcb) { 3811170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3812170091Srrs } 3813163953Srrs return (NULL); 3814163953Srrs } 3815163953Srrs /* 3816163953Srrs * validate the verification tag 3817163953Srrs */ 3818163953Srrs vtag_in = ntohl(sh->v_tag); 3819163953Srrs 3820165220Srrs if (locked_tcb) { 3821165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3822165220Srrs } 3823163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3824170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3825170056Srrs ntohs(ch->chunk_length), vtag_in); 3826163953Srrs if (vtag_in != 0) { 3827163953Srrs /* protocol error- silently discard... */ 3828163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3829169378Srrs if (locked_tcb) { 3830163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3831169378Srrs } 3832163953Srrs return (NULL); 3833163953Srrs } 3834163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3835163953Srrs /* 3836163953Srrs * If there is no stcb, skip the AUTH chunk and process 3837163953Srrs * later after a stcb is found (to validate the lookup was 3838163953Srrs * valid. 3839163953Srrs */ 3840163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3841163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3842163953Srrs /* save this chunk for later processing */ 3843163953Srrs auth_skipped = 1; 3844163953Srrs auth_offset = *offset; 3845163953Srrs auth_len = ntohs(ch->chunk_length); 3846163953Srrs 3847163953Srrs /* (temporarily) move past this chunk */ 3848163953Srrs *offset += SCTP_SIZE32(auth_len); 3849163953Srrs if (*offset >= length) { 3850163953Srrs /* no more data left in the mbuf chain */ 3851163953Srrs *offset = length; 3852170091Srrs if (locked_tcb) { 3853170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3854170091Srrs } 3855163953Srrs return (NULL); 3856163953Srrs } 3857163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3858163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3859163953Srrs } 3860169420Srrs if (ch == NULL) { 3861169420Srrs /* Help */ 3862169420Srrs *offset = length; 3863170091Srrs if (locked_tcb) { 3864170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3865170091Srrs } 3866169420Srrs return (NULL); 3867169420Srrs } 3868163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3869163953Srrs goto process_control_chunks; 3870163953Srrs } 3871163953Srrs /* 3872163953Srrs * first check if it's an ASCONF with an unknown src addr we 3873163953Srrs * need to look inside to find the association 3874163953Srrs */ 3875163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3876171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 3877171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 3878171990Srrs 3879163953Srrs /* inp's refcount may be reduced */ 3880163953Srrs SCTP_INP_INCR_REF(inp); 3881163953Srrs 3882171990Srrs asconf_offset = *offset; 3883171990Srrs do { 3884171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 3885171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 3886171990Srrs break; 3887171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3888171990Srrs *offset, sh, &inp, netp); 3889171990Srrs if (stcb != NULL) 3890171990Srrs break; 3891171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 3892171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 3893171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3894171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 3895163953Srrs if (stcb == NULL) { 3896163953Srrs /* 3897163953Srrs * reduce inp's refcount if not reduced in 3898163953Srrs * sctp_findassociation_ep_asconf(). 3899163953Srrs */ 3900163953Srrs SCTP_INP_DECR_REF(inp); 3901171990Srrs } else { 3902171990Srrs locked_tcb = stcb; 3903163953Srrs } 3904171990Srrs 3905163953Srrs /* now go back and verify any auth chunk to be sure */ 3906163953Srrs if (auth_skipped && (stcb != NULL)) { 3907163953Srrs struct sctp_auth_chunk *auth; 3908163953Srrs 3909163953Srrs auth = (struct sctp_auth_chunk *) 3910163953Srrs sctp_m_getptr(m, auth_offset, 3911163953Srrs auth_len, chunk_buf); 3912163953Srrs got_auth = 1; 3913163953Srrs auth_skipped = 0; 3914169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3915163953Srrs auth_offset)) { 3916163953Srrs /* auth HMAC failed so dump it */ 3917163953Srrs *offset = length; 3918170091Srrs if (locked_tcb) { 3919170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3920170091Srrs } 3921163953Srrs return (NULL); 3922163953Srrs } else { 3923163953Srrs /* remaining chunks are HMAC checked */ 3924163953Srrs stcb->asoc.authenticated = 1; 3925163953Srrs } 3926163953Srrs } 3927163953Srrs } 3928163953Srrs if (stcb == NULL) { 3929163953Srrs /* no association, so it's out of the blue... */ 3930169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3931170181Srrs vrf_id); 3932163953Srrs *offset = length; 3933169378Srrs if (locked_tcb) { 3934163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3935169378Srrs } 3936163953Srrs return (NULL); 3937163953Srrs } 3938163953Srrs asoc = &stcb->asoc; 3939163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3940163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3941163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3942163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3943163953Srrs if ((vtag_in == asoc->my_vtag) || 3944163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3945163953Srrs (vtag_in == asoc->peer_vtag))) { 3946163953Srrs /* this is valid */ 3947163953Srrs } else { 3948163953Srrs /* drop this packet... */ 3949163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3950169378Srrs if (locked_tcb) { 3951163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3952169378Srrs } 3953163953Srrs return (NULL); 3954163953Srrs } 3955163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3956163953Srrs if (vtag_in != asoc->my_vtag) { 3957163953Srrs /* 3958163953Srrs * this could be a stale SHUTDOWN-ACK or the 3959163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3960163953Srrs * is still hung; we have started a new asoc 3961163953Srrs * but it won't complete until the shutdown 3962163953Srrs * is completed 3963163953Srrs */ 3964169378Srrs if (locked_tcb) { 3965163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3966169378Srrs } 3967163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3968170181Srrs NULL, vrf_id); 3969163953Srrs return (NULL); 3970163953Srrs } 3971163953Srrs } else { 3972163953Srrs /* for all other chunks, vtag must match */ 3973163953Srrs if (vtag_in != asoc->my_vtag) { 3974163953Srrs /* invalid vtag... */ 3975169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3976169420Srrs "invalid vtag: %xh, expect %xh\n", 3977169420Srrs vtag_in, asoc->my_vtag); 3978163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3979169378Srrs if (locked_tcb) { 3980163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3981169378Srrs } 3982163953Srrs *offset = length; 3983163953Srrs return (NULL); 3984163953Srrs } 3985163953Srrs } 3986163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3987163953Srrs /* 3988163953Srrs * process all control chunks... 3989163953Srrs */ 3990163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3991163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3992163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3993163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3994171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 3995171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3996171943Srrs stcb->asoc.overall_error_count, 3997171943Srrs 0, 3998171943Srrs SCTP_FROM_SCTP_INPUT, 3999171943Srrs __LINE__); 4000171943Srrs } 4001163953Srrs stcb->asoc.overall_error_count = 0; 4002163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4003163953Srrs *netp); 4004163953Srrs } 4005163953Srrsprocess_control_chunks: 4006163953Srrs while (IS_SCTP_CONTROL(ch)) { 4007163953Srrs /* validate chunk length */ 4008163953Srrs chk_length = ntohs(ch->chunk_length); 4009169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4010169420Srrs ch->chunk_type, chk_length); 4011170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4012170140Srrs if (chk_length < sizeof(*ch) || 4013170140Srrs (*offset + (int)chk_length) > length) { 4014163953Srrs *offset = length; 4015169378Srrs if (locked_tcb) { 4016163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4017169378Srrs } 4018163953Srrs return (NULL); 4019163953Srrs } 4020163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4021163953Srrs /* 4022163953Srrs * INIT-ACK only gets the init ack "header" portion only 4023163953Srrs * because we don't have to process the peer's COOKIE. All 4024163953Srrs * others get a complete chunk. 4025163953Srrs */ 4026169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4027169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4028163953Srrs /* get an init-ack chunk */ 4029163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4030163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4031163953Srrs if (ch == NULL) { 4032163953Srrs *offset = length; 4033169378Srrs if (locked_tcb) { 4034163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4035169378Srrs } 4036163953Srrs return (NULL); 4037163953Srrs } 4038170642Srrs } else { 4039170786Srrs /* For cookies and all other chunks. */ 4040169208Srrs if (chk_length > sizeof(chunk_buf)) { 4041169208Srrs /* 4042169208Srrs * use just the size of the chunk buffer so 4043170642Srrs * the front part of our chunks fit in 4044170642Srrs * contiguous space up to the chunk buffer 4045170642Srrs * size (508 bytes). For chunks that need to 4046170786Srrs * get more than that they must use the 4047170642Srrs * sctp_m_getptr() function or other means 4048170786Srrs * (e.g. know how to parse mbuf chains). 4049170786Srrs * Cookies do this already. 4050169208Srrs */ 4051169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4052169208Srrs (sizeof(chunk_buf) - 4), 4053169208Srrs chunk_buf); 4054169208Srrs if (ch == NULL) { 4055169208Srrs *offset = length; 4056169378Srrs if (locked_tcb) { 4057169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4058169378Srrs } 4059169208Srrs return (NULL); 4060169208Srrs } 4061169208Srrs } else { 4062169208Srrs /* We can fit it all */ 4063170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4064170642Srrs chk_length, chunk_buf); 4065170642Srrs if (ch == NULL) { 4066170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4067170642Srrs *offset = length; 4068170642Srrs if (locked_tcb) { 4069170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4070169208Srrs } 4071170642Srrs return (NULL); 4072163953Srrs } 4073163953Srrs } 4074163953Srrs } 4075163953Srrs num_chunks++; 4076163953Srrs /* Save off the last place we got a control from */ 4077163953Srrs if (stcb != NULL) { 4078169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4079163953Srrs /* 4080163953Srrs * allow last_control to be NULL if 4081163953Srrs * ASCONF... ASCONF processing will find the 4082163953Srrs * right net later 4083163953Srrs */ 4084169420Srrs if ((netp != NULL) && (*netp != NULL)) 4085169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4086163953Srrs } 4087163953Srrs } 4088163953Srrs#ifdef SCTP_AUDITING_ENABLED 4089163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4090163953Srrs#endif 4091163953Srrs 4092163953Srrs /* check to see if this chunk required auth, but isn't */ 4093163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4094163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 4095163953Srrs stcb->asoc.local_auth_chunks) && 4096163953Srrs !stcb->asoc.authenticated) { 4097163953Srrs /* "silently" ignore */ 4098163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4099163953Srrs goto next_chunk; 4100163953Srrs } 4101163953Srrs switch (ch->chunk_type) { 4102163953Srrs case SCTP_INITIATION: 4103163953Srrs /* must be first and only chunk */ 4104169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4105163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4106163953Srrs /* We are not interested anymore? */ 4107163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4108163953Srrs /* 4109163953Srrs * collision case where we are 4110163953Srrs * sending to them too 4111163953Srrs */ 4112163953Srrs ; 4113163953Srrs } else { 4114169378Srrs if (locked_tcb) { 4115163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4116169378Srrs } 4117163953Srrs *offset = length; 4118163953Srrs return (NULL); 4119163953Srrs } 4120163953Srrs } 4121171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4122171990Srrs (num_chunks > 1) || 4123170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4124163953Srrs *offset = length; 4125169378Srrs if (locked_tcb) { 4126163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4127169378Srrs } 4128163953Srrs return (NULL); 4129163953Srrs } 4130163953Srrs if ((stcb != NULL) && 4131163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4132163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4133163953Srrs sctp_send_shutdown_ack(stcb, 4134163953Srrs stcb->asoc.primary_destination); 4135163953Srrs *offset = length; 4136172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4137169378Srrs if (locked_tcb) { 4138163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4139169378Srrs } 4140163953Srrs return (NULL); 4141163953Srrs } 4142169420Srrs if (netp) { 4143169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4144169420Srrs (struct sctp_init_chunk *)ch, inp, 4145170181Srrs stcb, *netp, &abort_no_unlock, vrf_id); 4146169420Srrs } 4147168299Srrs if (abort_no_unlock) 4148168299Srrs return (NULL); 4149168299Srrs 4150163953Srrs *offset = length; 4151169378Srrs if (locked_tcb) { 4152163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4153169378Srrs } 4154163953Srrs return (NULL); 4155163953Srrs break; 4156170642Srrs case SCTP_PAD_CHUNK: 4157170642Srrs break; 4158163953Srrs case SCTP_INITIATION_ACK: 4159163953Srrs /* must be first and only chunk */ 4160169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4161163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4162163953Srrs /* We are not interested anymore */ 4163163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4164163953Srrs ; 4165163953Srrs } else { 4166169378Srrs if (locked_tcb) { 4167163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4168169378Srrs } 4169163953Srrs *offset = length; 4170163953Srrs if (stcb) { 4171172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4172172090Srrs so = SCTP_INP_SO(inp); 4173172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4174172090Srrs SCTP_TCB_UNLOCK(stcb); 4175172090Srrs SCTP_SOCKET_LOCK(so, 1); 4176172090Srrs SCTP_TCB_LOCK(stcb); 4177172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4178172090Srrs#endif 4179171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4180172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4181172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4182172090Srrs#endif 4183163953Srrs } 4184163953Srrs return (NULL); 4185163953Srrs } 4186163953Srrs } 4187163953Srrs if ((num_chunks > 1) || 4188170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4189163953Srrs *offset = length; 4190169378Srrs if (locked_tcb) { 4191163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4192169378Srrs } 4193163953Srrs return (NULL); 4194163953Srrs } 4195169420Srrs if ((netp) && (*netp)) { 4196169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4197170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4198169420Srrs } else { 4199169420Srrs ret = -1; 4200169420Srrs } 4201163953Srrs /* 4202163953Srrs * Special case, I must call the output routine to 4203163953Srrs * get the cookie echoed 4204163953Srrs */ 4205168299Srrs if (abort_no_unlock) 4206168299Srrs return (NULL); 4207168299Srrs 4208163953Srrs if ((stcb) && ret == 0) 4209172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4210163953Srrs *offset = length; 4211169378Srrs if (locked_tcb) { 4212163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4213169378Srrs } 4214163953Srrs return (NULL); 4215163953Srrs break; 4216163953Srrs case SCTP_SELECTIVE_ACK: 4217169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4218163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4219163953Srrs { 4220163953Srrs struct sctp_sack_chunk *sack; 4221163953Srrs int abort_now = 0; 4222163953Srrs uint32_t a_rwnd, cum_ack; 4223163953Srrs uint16_t num_seg; 4224163953Srrs int nonce_sum_flag; 4225163953Srrs 4226169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 4227169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 4228171990Srrs ignore_sack: 4229169208Srrs *offset = length; 4230169378Srrs if (locked_tcb) { 4231169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4232169378Srrs } 4233169208Srrs return (NULL); 4234169208Srrs } 4235171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4236171990Srrs /*- 4237171990Srrs * If we have sent a shutdown-ack, we will pay no 4238171990Srrs * attention to a sack sent in to us since 4239171990Srrs * we don't care anymore. 4240171990Srrs */ 4241171990Srrs goto ignore_sack; 4242171990Srrs } 4243163953Srrs sack = (struct sctp_sack_chunk *)ch; 4244163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 4245163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4246163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4247163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4248170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4249170606Srrs cum_ack, 4250170606Srrs num_seg, 4251170606Srrs a_rwnd 4252170606Srrs ); 4253163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4254163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4255163953Srrs (num_seg == 0) && 4256163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4257163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4258163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4259163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4260163953Srrs ) { 4261163953Srrs /* 4262163953Srrs * We have a SIMPLE sack having no 4263163953Srrs * prior segments and data on sent 4264163953Srrs * queue to be acked.. Use the 4265163953Srrs * faster path sack processing. We 4266163953Srrs * also allow window update sacks 4267163953Srrs * with no missing segments to go 4268163953Srrs * this way too. 4269163953Srrs */ 4270169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4271169208Srrs &abort_now); 4272163953Srrs } else { 4273169420Srrs if (netp && *netp) 4274170781Srrs sctp_handle_sack(m, *offset, 4275170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 4276163953Srrs } 4277163953Srrs if (abort_now) { 4278163953Srrs /* ABORT signal from sack processing */ 4279163953Srrs *offset = length; 4280163953Srrs return (NULL); 4281163953Srrs } 4282163953Srrs } 4283163953Srrs break; 4284163953Srrs case SCTP_HEARTBEAT_REQUEST: 4285169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4286169420Srrs if ((stcb) && netp && *netp) { 4287169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4288169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4289169420Srrs chk_length, *netp); 4290169420Srrs 4291169420Srrs /* He's alive so give him credit */ 4292171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4293171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4294171943Srrs stcb->asoc.overall_error_count, 4295171943Srrs 0, 4296171943Srrs SCTP_FROM_SCTP_INPUT, 4297171943Srrs __LINE__); 4298171943Srrs } 4299169420Srrs stcb->asoc.overall_error_count = 0; 4300163953Srrs } 4301163953Srrs break; 4302163953Srrs case SCTP_HEARTBEAT_ACK: 4303169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4304169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4305169208Srrs /* Its not ours */ 4306169352Srrs *offset = length; 4307169378Srrs if (locked_tcb) { 4308169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4309169378Srrs } 4310169208Srrs return (NULL); 4311169208Srrs } 4312163953Srrs /* He's alive so give him credit */ 4313171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4314171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4315171943Srrs stcb->asoc.overall_error_count, 4316171943Srrs 0, 4317171943Srrs SCTP_FROM_SCTP_INPUT, 4318171943Srrs __LINE__); 4319171943Srrs } 4320163953Srrs stcb->asoc.overall_error_count = 0; 4321163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4322169420Srrs if (netp && *netp) 4323169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4324169420Srrs stcb, *netp); 4325163953Srrs break; 4326163953Srrs case SCTP_ABORT_ASSOCIATION: 4327170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4328170091Srrs stcb); 4329169420Srrs if ((stcb) && netp && *netp) 4330169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4331169420Srrs stcb, *netp); 4332163953Srrs *offset = length; 4333163953Srrs return (NULL); 4334163953Srrs break; 4335163953Srrs case SCTP_SHUTDOWN: 4336170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4337170091Srrs stcb); 4338169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4339169208Srrs *offset = length; 4340169378Srrs if (locked_tcb) { 4341169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4342169378Srrs } 4343169208Srrs return (NULL); 4344169420Srrs } 4345169420Srrs if (netp && *netp) { 4346163953Srrs int abort_flag = 0; 4347163953Srrs 4348163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4349163953Srrs stcb, *netp, &abort_flag); 4350163953Srrs if (abort_flag) { 4351163953Srrs *offset = length; 4352163953Srrs return (NULL); 4353163953Srrs } 4354163953Srrs } 4355163953Srrs break; 4356163953Srrs case SCTP_SHUTDOWN_ACK: 4357170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4358169420Srrs if ((stcb) && (netp) && (*netp)) 4359169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4360163953Srrs *offset = length; 4361163953Srrs return (NULL); 4362163953Srrs break; 4363169420Srrs 4364163953Srrs case SCTP_OPERATION_ERROR: 4365169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4366169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4367169420Srrs 4368163953Srrs *offset = length; 4369163953Srrs return (NULL); 4370163953Srrs } 4371163953Srrs break; 4372163953Srrs case SCTP_COOKIE_ECHO: 4373169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4374170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4375163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4376163953Srrs ; 4377163953Srrs } else { 4378169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4379163953Srrs /* We are not interested anymore */ 4380163953Srrs *offset = length; 4381163953Srrs return (NULL); 4382163953Srrs } 4383163953Srrs } 4384163953Srrs /* 4385163953Srrs * First are we accepting? We do this again here 4386169420Srrs * sincen it is possible that a previous endpoint 4387169420Srrs * WAS listening responded to a INIT-ACK and then 4388163953Srrs * closed. We opened and bound.. and are now no 4389163953Srrs * longer listening. 4390163953Srrs */ 4391172703Srrs 4392172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4393172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4394172703Srrs (sctp_abort_if_one_2_one_hits_limit)) { 4395163953Srrs struct mbuf *oper; 4396163953Srrs struct sctp_paramhdr *phdr; 4397163953Srrs 4398172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4399172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4400172703Srrs if (oper) { 4401172703Srrs SCTP_BUF_LEN(oper) = 4402172703Srrs sizeof(struct sctp_paramhdr); 4403172703Srrs phdr = mtod(oper, 4404172703Srrs struct sctp_paramhdr *); 4405172703Srrs phdr->param_type = 4406172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4407172703Srrs phdr->param_length = 4408172703Srrs htons(sizeof(struct sctp_paramhdr)); 4409163953Srrs } 4410172703Srrs sctp_abort_association(inp, stcb, m, 4411172703Srrs iphlen, sh, oper, vrf_id); 4412163953Srrs } 4413172703Srrs *offset = length; 4414172703Srrs return (NULL); 4415172703Srrs } else { 4416163953Srrs struct mbuf *ret_buf; 4417165220Srrs struct sctp_inpcb *linp; 4418163953Srrs 4419169420Srrs if (stcb) { 4420165220Srrs linp = NULL; 4421169420Srrs } else { 4422165220Srrs linp = inp; 4423169420Srrs } 4424165220Srrs 4425169420Srrs if (linp) { 4426165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4427169420Srrs } 4428169420Srrs if (netp) { 4429169420Srrs ret_buf = 4430169420Srrs sctp_handle_cookie_echo(m, iphlen, 4431169420Srrs *offset, sh, 4432169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4433169420Srrs &inp, &stcb, netp, 4434169420Srrs auth_skipped, 4435169420Srrs auth_offset, 4436169420Srrs auth_len, 4437169420Srrs &locked_tcb, 4438170181Srrs vrf_id); 4439169420Srrs } else { 4440169420Srrs ret_buf = NULL; 4441169420Srrs } 4442169420Srrs if (linp) { 4443165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4444169420Srrs } 4445163953Srrs if (ret_buf == NULL) { 4446163953Srrs if (locked_tcb) { 4447163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4448163953Srrs } 4449169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4450169420Srrs "GAK, null buffer\n"); 4451163953Srrs auth_skipped = 0; 4452163953Srrs *offset = length; 4453163953Srrs return (NULL); 4454163953Srrs } 4455163953Srrs /* if AUTH skipped, see if it verified... */ 4456163953Srrs if (auth_skipped) { 4457163953Srrs got_auth = 1; 4458163953Srrs auth_skipped = 0; 4459163953Srrs } 4460163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4461163953Srrs /* 4462163953Srrs * Restart the timer if we have 4463163953Srrs * pending data 4464163953Srrs */ 4465163953Srrs struct sctp_tmit_chunk *chk; 4466163953Srrs 4467163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4468163953Srrs if (chk) { 4469163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4470163953Srrs stcb->sctp_ep, stcb, 4471163953Srrs chk->whoTo); 4472163953Srrs } 4473163953Srrs } 4474163953Srrs } 4475163953Srrs break; 4476163953Srrs case SCTP_COOKIE_ACK: 4477170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4478169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4479169378Srrs if (locked_tcb) { 4480169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4481169378Srrs } 4482169352Srrs return (NULL); 4483169352Srrs } 4484163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4485163953Srrs /* We are not interested anymore */ 4486163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4487163953Srrs ; 4488169420Srrs } else if (stcb) { 4489172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4490172090Srrs so = SCTP_INP_SO(inp); 4491172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4492172090Srrs SCTP_TCB_UNLOCK(stcb); 4493172090Srrs SCTP_SOCKET_LOCK(so, 1); 4494172090Srrs SCTP_TCB_LOCK(stcb); 4495172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4496172090Srrs#endif 4497171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4498172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4499172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4500172090Srrs#endif 4501163953Srrs *offset = length; 4502163953Srrs return (NULL); 4503163953Srrs } 4504163953Srrs } 4505163953Srrs /* He's alive so give him credit */ 4506169420Srrs if ((stcb) && netp && *netp) { 4507171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4508171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4509171943Srrs stcb->asoc.overall_error_count, 4510171943Srrs 0, 4511171943Srrs SCTP_FROM_SCTP_INPUT, 4512171943Srrs __LINE__); 4513171943Srrs } 4514169378Srrs stcb->asoc.overall_error_count = 0; 4515169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4516169378Srrs } 4517163953Srrs break; 4518163953Srrs case SCTP_ECN_ECHO: 4519169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4520163953Srrs /* He's alive so give him credit */ 4521169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4522169208Srrs /* Its not ours */ 4523169378Srrs if (locked_tcb) { 4524169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4525169378Srrs } 4526169208Srrs *offset = length; 4527169208Srrs return (NULL); 4528169208Srrs } 4529169378Srrs if (stcb) { 4530171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4531171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4532171943Srrs stcb->asoc.overall_error_count, 4533171943Srrs 0, 4534171943Srrs SCTP_FROM_SCTP_INPUT, 4535171943Srrs __LINE__); 4536171943Srrs } 4537169378Srrs stcb->asoc.overall_error_count = 0; 4538169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4539169378Srrs stcb); 4540169378Srrs } 4541163953Srrs break; 4542163953Srrs case SCTP_ECN_CWR: 4543169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4544163953Srrs /* He's alive so give him credit */ 4545169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4546169208Srrs /* Its not ours */ 4547169378Srrs if (locked_tcb) { 4548169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4549169378Srrs } 4550169208Srrs *offset = length; 4551169208Srrs return (NULL); 4552169208Srrs } 4553169378Srrs if (stcb) { 4554171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4555171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4556171943Srrs stcb->asoc.overall_error_count, 4557171943Srrs 0, 4558171943Srrs SCTP_FROM_SCTP_INPUT, 4559171943Srrs __LINE__); 4560171943Srrs } 4561169378Srrs stcb->asoc.overall_error_count = 0; 4562169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4563169378Srrs } 4564163953Srrs break; 4565163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4566170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4567163953Srrs /* must be first and only chunk */ 4568163953Srrs if ((num_chunks > 1) || 4569170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4570163953Srrs *offset = length; 4571169378Srrs if (locked_tcb) { 4572163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4573169378Srrs } 4574163953Srrs return (NULL); 4575163953Srrs } 4576169420Srrs if ((stcb) && netp && *netp) { 4577169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4578169378Srrs stcb, *netp); 4579169378Srrs } 4580163953Srrs *offset = length; 4581163953Srrs return (NULL); 4582163953Srrs break; 4583163953Srrs case SCTP_ASCONF: 4584169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4585163953Srrs /* He's alive so give him credit */ 4586169378Srrs if (stcb) { 4587171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4588171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4589171943Srrs stcb->asoc.overall_error_count, 4590171943Srrs 0, 4591171943Srrs SCTP_FROM_SCTP_INPUT, 4592171943Srrs __LINE__); 4593171943Srrs } 4594169378Srrs stcb->asoc.overall_error_count = 0; 4595169378Srrs sctp_handle_asconf(m, *offset, 4596171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 4597171990Srrs asconf_cnt++; 4598169378Srrs } 4599163953Srrs break; 4600163953Srrs case SCTP_ASCONF_ACK: 4601169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4602169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4603169208Srrs /* Its not ours */ 4604169378Srrs if (locked_tcb) { 4605169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4606169378Srrs } 4607169208Srrs *offset = length; 4608169208Srrs return (NULL); 4609169208Srrs } 4610169420Srrs if ((stcb) && netp && *netp) { 4611169378Srrs /* He's alive so give him credit */ 4612171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4613171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4614171943Srrs stcb->asoc.overall_error_count, 4615171943Srrs 0, 4616171943Srrs SCTP_FROM_SCTP_INPUT, 4617171943Srrs __LINE__); 4618171943Srrs } 4619169378Srrs stcb->asoc.overall_error_count = 0; 4620169378Srrs sctp_handle_asconf_ack(m, *offset, 4621172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 4622172190Srrs if (abort_no_unlock) 4623172190Srrs return (NULL); 4624169378Srrs } 4625163953Srrs break; 4626163953Srrs case SCTP_FORWARD_CUM_TSN: 4627169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4628169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4629169208Srrs /* Its not ours */ 4630169378Srrs if (locked_tcb) { 4631169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4632169378Srrs } 4633169208Srrs *offset = length; 4634169208Srrs return (NULL); 4635169208Srrs } 4636163953Srrs /* He's alive so give him credit */ 4637169378Srrs if (stcb) { 4638163953Srrs int abort_flag = 0; 4639163953Srrs 4640163953Srrs stcb->asoc.overall_error_count = 0; 4641171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4642171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4643171943Srrs stcb->asoc.overall_error_count, 4644171943Srrs 0, 4645171943Srrs SCTP_FROM_SCTP_INPUT, 4646171943Srrs __LINE__); 4647171943Srrs } 4648163953Srrs *fwd_tsn_seen = 1; 4649163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4650163953Srrs /* We are not interested anymore */ 4651172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4652172090Srrs so = SCTP_INP_SO(inp); 4653172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4654172090Srrs SCTP_TCB_UNLOCK(stcb); 4655172090Srrs SCTP_SOCKET_LOCK(so, 1); 4656172090Srrs SCTP_TCB_LOCK(stcb); 4657172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4658172090Srrs#endif 4659171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4660172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4661172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4662172090Srrs#endif 4663163953Srrs *offset = length; 4664163953Srrs return (NULL); 4665163953Srrs } 4666163953Srrs sctp_handle_forward_tsn(stcb, 4667170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 4668163953Srrs if (abort_flag) { 4669163953Srrs *offset = length; 4670163953Srrs return (NULL); 4671163953Srrs } else { 4672171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4673171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4674171943Srrs stcb->asoc.overall_error_count, 4675171943Srrs 0, 4676171943Srrs SCTP_FROM_SCTP_INPUT, 4677171943Srrs __LINE__); 4678171943Srrs } 4679163953Srrs stcb->asoc.overall_error_count = 0; 4680163953Srrs } 4681163953Srrs 4682163953Srrs } 4683163953Srrs break; 4684163953Srrs case SCTP_STREAM_RESET: 4685169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4686169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4687169208Srrs /* Its not ours */ 4688169378Srrs if (locked_tcb) { 4689169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4690169378Srrs } 4691169208Srrs *offset = length; 4692169208Srrs return (NULL); 4693169208Srrs } 4694163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4695163953Srrs /* We are not interested anymore */ 4696172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4697172090Srrs so = SCTP_INP_SO(inp); 4698172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4699172090Srrs SCTP_TCB_UNLOCK(stcb); 4700172090Srrs SCTP_SOCKET_LOCK(so, 1); 4701172090Srrs SCTP_TCB_LOCK(stcb); 4702172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4703172090Srrs#endif 4704171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4705172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4706172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4707172090Srrs#endif 4708163953Srrs *offset = length; 4709163953Srrs return (NULL); 4710163953Srrs } 4711169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4712169420Srrs /* 4713169420Srrs * hmm, peer should have announced this, but 4714169420Srrs * we will turn it on since he is sending us 4715169420Srrs * a stream reset. 4716169420Srrs */ 4717169420Srrs stcb->asoc.peer_supports_strreset = 1; 4718163953Srrs } 4719170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 4720169420Srrs /* stop processing */ 4721169420Srrs *offset = length; 4722169420Srrs return (NULL); 4723169420Srrs } 4724163953Srrs break; 4725163953Srrs case SCTP_PACKET_DROPPED: 4726169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4727163953Srrs /* re-get it all please */ 4728169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4729169208Srrs /* Its not ours */ 4730169378Srrs if (locked_tcb) { 4731169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4732169378Srrs } 4733169208Srrs *offset = length; 4734169208Srrs return (NULL); 4735169208Srrs } 4736169420Srrs if (ch && (stcb) && netp && (*netp)) { 4737169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4738170781Srrs stcb, *netp, 4739170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4740170781Srrs 4741169378Srrs } 4742163953Srrs break; 4743163953Srrs 4744163953Srrs case SCTP_AUTHENTICATION: 4745169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4746163953Srrs if (sctp_auth_disable) 4747163953Srrs goto unknown_chunk; 4748163953Srrs 4749163953Srrs if (stcb == NULL) { 4750163953Srrs /* save the first AUTH for later processing */ 4751163953Srrs if (auth_skipped == 0) { 4752163953Srrs auth_offset = *offset; 4753163953Srrs auth_len = chk_length; 4754163953Srrs auth_skipped = 1; 4755163953Srrs } 4756163953Srrs /* skip this chunk (temporarily) */ 4757163953Srrs goto next_chunk; 4758163953Srrs } 4759169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4760169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4761169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4762169208Srrs /* Its not ours */ 4763169378Srrs if (locked_tcb) { 4764169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4765169378Srrs } 4766169208Srrs *offset = length; 4767169208Srrs return (NULL); 4768169208Srrs } 4769163953Srrs if (got_auth == 1) { 4770163953Srrs /* skip this chunk... it's already auth'd */ 4771163953Srrs goto next_chunk; 4772163953Srrs } 4773163953Srrs got_auth = 1; 4774169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4775163953Srrs m, *offset)) { 4776163953Srrs /* auth HMAC failed so dump the packet */ 4777163953Srrs *offset = length; 4778163953Srrs return (stcb); 4779163953Srrs } else { 4780163953Srrs /* remaining chunks are HMAC checked */ 4781163953Srrs stcb->asoc.authenticated = 1; 4782163953Srrs } 4783163953Srrs break; 4784163953Srrs 4785163953Srrs default: 4786163953Srrs unknown_chunk: 4787163953Srrs /* it's an unknown chunk! */ 4788163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4789163953Srrs struct mbuf *mm; 4790163953Srrs struct sctp_paramhdr *phd; 4791163953Srrs 4792163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4793165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4794163953Srrs if (mm) { 4795163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4796163953Srrs /* 4797163953Srrs * We cheat and use param type since 4798163953Srrs * we did not bother to define a 4799163953Srrs * error cause struct. They are the 4800163953Srrs * same basic format with different 4801163953Srrs * names. 4802163953Srrs */ 4803163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4804163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4805165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4806166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4807163953Srrs M_DONTWAIT); 4808165647Srrs if (SCTP_BUF_NEXT(mm)) { 4809163953Srrs sctp_queue_op_err(stcb, mm); 4810163953Srrs } else { 4811163953Srrs sctp_m_freem(mm); 4812163953Srrs } 4813163953Srrs } 4814163953Srrs } 4815163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4816163953Srrs /* discard this packet */ 4817163953Srrs *offset = length; 4818163953Srrs return (stcb); 4819163953Srrs } /* else skip this bad chunk and continue... */ 4820163953Srrs break; 4821163953Srrs } /* switch (ch->chunk_type) */ 4822163953Srrs 4823163953Srrs 4824163953Srrsnext_chunk: 4825163953Srrs /* get the next chunk */ 4826163953Srrs *offset += SCTP_SIZE32(chk_length); 4827163953Srrs if (*offset >= length) { 4828163953Srrs /* no more data left in the mbuf chain */ 4829163953Srrs break; 4830163953Srrs } 4831163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4832163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4833163953Srrs if (ch == NULL) { 4834169378Srrs if (locked_tcb) { 4835163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4836169378Srrs } 4837163953Srrs *offset = length; 4838163953Srrs return (NULL); 4839163953Srrs } 4840163953Srrs } /* while */ 4841171990Srrs 4842171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 4843171990Srrs sctp_send_asconf_ack(stcb); 4844171990Srrs } 4845163953Srrs return (stcb); 4846163953Srrs} 4847163953Srrs 4848163953Srrs 4849163953Srrs/* 4850163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4851163953Srrs * ECN(0) or ECN(1) or CE 4852163953Srrs */ 4853170806Srrsstatic void 4854163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4855163953Srrs uint8_t ecn_bits) 4856163953Srrs{ 4857163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4858163953Srrs ; 4859163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4860163953Srrs /* 4861163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4862163953Srrs * change the NS bit (that we have yet to find a way to send 4863163953Srrs * it yet). 4864163953Srrs */ 4865163953Srrs 4866163953Srrs /* ECN Nonce stuff */ 4867163953Srrs stcb->asoc.receiver_nonce_sum++; 4868163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4869163953Srrs 4870163953Srrs /* 4871163953Srrs * Drag up the last_echo point if cumack is larger since we 4872163953Srrs * don't want the point falling way behind by more than 4873163953Srrs * 2^^31 and then having it be incorrect. 4874163953Srrs */ 4875163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4876163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4877163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4878163953Srrs } 4879163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4880163953Srrs /* 4881163953Srrs * Drag up the last_echo point if cumack is larger since we 4882163953Srrs * don't want the point falling way behind by more than 4883163953Srrs * 2^^31 and then having it be incorrect. 4884163953Srrs */ 4885163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4886163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4887163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4888163953Srrs } 4889163953Srrs } 4890163953Srrs} 4891163953Srrs 4892170806Srrsstatic void 4893163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4894163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4895163953Srrs{ 4896163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4897163953Srrs /* 4898163953Srrs * we possibly must notify the sender that a congestion 4899163953Srrs * window reduction is in order. We do this by adding a ECNE 4900163953Srrs * chunk to the output chunk queue. The incoming CWR will 4901163953Srrs * remove this chunk. 4902163953Srrs */ 4903163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4904163953Srrs MAX_TSN)) { 4905163953Srrs /* Yep, we need to add a ECNE */ 4906163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4907163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4908163953Srrs } 4909163953Srrs } 4910163953Srrs} 4911163953Srrs 4912171440Srrs#ifdef INVARIANTS 4913171440Srrsstatic void 4914171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 4915171440Srrs{ 4916171440Srrs struct sctp_tcb *stcb; 4917171440Srrs 4918171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4919171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 4920171440Srrs panic("Own lock on stcb at return from input"); 4921171440Srrs } 4922171440Srrs } 4923171440Srrs} 4924171440Srrs 4925171440Srrs#endif 4926171440Srrs 4927163953Srrs/* 4928163953Srrs * common input chunk processing (v4 and v6) 4929163953Srrs */ 4930169378Srrsvoid 4931163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4932163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4933163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4934170181Srrs uint8_t ecn_bits, uint32_t vrf_id) 4935163953Srrs{ 4936163953Srrs /* 4937163953Srrs * Control chunk processing 4938163953Srrs */ 4939163953Srrs uint32_t high_tsn; 4940163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4941163953Srrs struct mbuf *m = *mm; 4942163953Srrs int abort_flag = 0; 4943163953Srrs int un_sent; 4944163953Srrs 4945163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4946163953Srrs#ifdef SCTP_AUDITING_ENABLED 4947163953Srrs sctp_audit_log(0xE0, 1); 4948163953Srrs sctp_auditing(0, inp, stcb, net); 4949163953Srrs#endif 4950163953Srrs 4951171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d stcb:%p\n", 4952171990Srrs m, iphlen, offset, stcb); 4953163953Srrs if (stcb) { 4954163953Srrs /* always clear this before beginning a packet */ 4955163953Srrs stcb->asoc.authenticated = 0; 4956163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4957171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 4958171990Srrs stcb, stcb->asoc.state); 4959171990Srrs 4960171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 4961171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 4962171745Srrs /*- 4963171745Srrs * If we hit here, we had a ref count 4964171745Srrs * up when the assoc was aborted and the 4965171745Srrs * timer is clearing out the assoc, we should 4966171745Srrs * NOT respond to any packet.. its OOTB. 4967171745Srrs */ 4968171745Srrs SCTP_TCB_UNLOCK(stcb); 4969171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4970171745Srrs vrf_id); 4971171745Srrs goto out_now; 4972171745Srrs } 4973163953Srrs } 4974163953Srrs if (IS_SCTP_CONTROL(ch)) { 4975163953Srrs /* process the control portion of the SCTP packet */ 4976169655Srrs /* sa_ignore NO_NULL_CHK */ 4977163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4978170181Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id); 4979163953Srrs if (stcb) { 4980163953Srrs /* 4981163953Srrs * This covers us if the cookie-echo was there and 4982163953Srrs * it changes our INP. 4983163953Srrs */ 4984163953Srrs inp = stcb->sctp_ep; 4985163953Srrs } 4986163953Srrs } else { 4987163953Srrs /* 4988163953Srrs * no control chunks, so pre-process DATA chunks (these 4989163953Srrs * checks are taken care of by control processing) 4990163953Srrs */ 4991163953Srrs 4992163953Srrs /* 4993163953Srrs * if DATA only packet, and auth is required, then punt... 4994163953Srrs * can't have authenticated without any AUTH (control) 4995163953Srrs * chunks 4996163953Srrs */ 4997163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4998163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4999163953Srrs stcb->asoc.local_auth_chunks)) { 5000163953Srrs /* "silently" ignore */ 5001163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5002163953Srrs SCTP_TCB_UNLOCK(stcb); 5003171440Srrs goto out_now; 5004163953Srrs } 5005163953Srrs if (stcb == NULL) { 5006163953Srrs /* out of the blue DATA chunk */ 5007169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5008170181Srrs vrf_id); 5009171440Srrs goto out_now; 5010163953Srrs } 5011163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5012163953Srrs /* v_tag mismatch! */ 5013163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5014163953Srrs SCTP_TCB_UNLOCK(stcb); 5015171440Srrs goto out_now; 5016163953Srrs } 5017163953Srrs } 5018163953Srrs 5019163953Srrs if (stcb == NULL) { 5020163953Srrs /* 5021163953Srrs * no valid TCB for this packet, or we found it's a bad 5022163953Srrs * packet while processing control, or we're done with this 5023163953Srrs * packet (done or skip rest of data), so we drop it... 5024163953Srrs */ 5025171440Srrs goto out_now; 5026163953Srrs } 5027163953Srrs /* 5028163953Srrs * DATA chunk processing 5029163953Srrs */ 5030163953Srrs /* plow through the data chunks while length > offset */ 5031163953Srrs 5032163953Srrs /* 5033163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5034163953Srrs * DATA is required. 5035163953Srrs */ 5036165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 5037163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 5038163953Srrs stcb->asoc.local_auth_chunks) && 5039163953Srrs !stcb->asoc.authenticated) { 5040163953Srrs /* "silently" ignore */ 5041163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5042169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5043169420Srrs "Data chunk requires AUTH, skipped\n"); 5044165220Srrs goto trigger_send; 5045163953Srrs } 5046163953Srrs if (length > offset) { 5047163953Srrs int retval; 5048163953Srrs 5049163953Srrs /* 5050163953Srrs * First check to make sure our state is correct. We would 5051163953Srrs * not get here unless we really did have a tag, so we don't 5052163953Srrs * abort if this happens, just dump the chunk silently. 5053163953Srrs */ 5054163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5055163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5056163953Srrs /* 5057163953Srrs * we consider data with valid tags in this state 5058163953Srrs * shows us the cookie-ack was lost. Imply it was 5059163953Srrs * there. 5060163953Srrs */ 5061171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 5062171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5063171943Srrs stcb->asoc.overall_error_count, 5064171943Srrs 0, 5065171943Srrs SCTP_FROM_SCTP_INPUT, 5066171943Srrs __LINE__); 5067171943Srrs } 5068163953Srrs stcb->asoc.overall_error_count = 0; 5069163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5070163953Srrs break; 5071163953Srrs case SCTP_STATE_COOKIE_WAIT: 5072163953Srrs /* 5073163953Srrs * We consider OOTB any data sent during asoc setup. 5074163953Srrs */ 5075169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5076170181Srrs vrf_id); 5077163953Srrs SCTP_TCB_UNLOCK(stcb); 5078171440Srrs goto out_now; 5079171531Srrs /* sa_ignore NOTREACHED */ 5080163953Srrs break; 5081163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5082163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5083163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5084163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5085163953Srrs default: 5086163953Srrs SCTP_TCB_UNLOCK(stcb); 5087171440Srrs goto out_now; 5088171531Srrs /* sa_ignore NOTREACHED */ 5089163953Srrs break; 5090163953Srrs case SCTP_STATE_OPEN: 5091163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5092163953Srrs break; 5093163953Srrs } 5094163953Srrs /* take care of ECN, part 1. */ 5095163953Srrs if (stcb->asoc.ecn_allowed && 5096163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5097163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5098163953Srrs } 5099163953Srrs /* plow through the data chunks while length > offset */ 5100163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5101163953Srrs inp, stcb, net, &high_tsn); 5102163953Srrs if (retval == 2) { 5103163953Srrs /* 5104163953Srrs * The association aborted, NO UNLOCK needed since 5105163953Srrs * the association is destroyed. 5106163953Srrs */ 5107171440Srrs goto out_now; 5108163953Srrs } 5109163953Srrs data_processed = 1; 5110163953Srrs if (retval == 0) { 5111163953Srrs /* take care of ecn part 2. */ 5112163953Srrs if (stcb->asoc.ecn_allowed && 5113163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5114163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5115163953Srrs ecn_bits); 5116163953Srrs } 5117163953Srrs } 5118163953Srrs /* 5119163953Srrs * Anything important needs to have been m_copy'ed in 5120163953Srrs * process_data 5121163953Srrs */ 5122163953Srrs } 5123163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5124163953Srrs int was_a_gap = 0; 5125163953Srrs 5126163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5127163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5128163953Srrs /* there was a gap before this data was processed */ 5129163953Srrs was_a_gap = 1; 5130163953Srrs } 5131163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 5132163953Srrs if (abort_flag) { 5133163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5134171440Srrs goto out_now; 5135163953Srrs } 5136163953Srrs } 5137163953Srrs /* trigger send of any chunks in queue... */ 5138165220Srrstrigger_send: 5139163953Srrs#ifdef SCTP_AUDITING_ENABLED 5140163953Srrs sctp_audit_log(0xE0, 2); 5141163953Srrs sctp_auditing(1, inp, stcb, net); 5142163953Srrs#endif 5143169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5144169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5145169420Srrs stcb->asoc.peers_rwnd, 5146169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5147169420Srrs stcb->asoc.total_flight); 5148163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5149163953Srrs 5150163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5151163953Srrs ((un_sent) && 5152163953Srrs (stcb->asoc.peers_rwnd > 0 || 5153163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5154169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5155172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5156169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5157163953Srrs } 5158163953Srrs#ifdef SCTP_AUDITING_ENABLED 5159163953Srrs sctp_audit_log(0xE0, 3); 5160163953Srrs sctp_auditing(2, inp, stcb, net); 5161163953Srrs#endif 5162163953Srrs SCTP_TCB_UNLOCK(stcb); 5163171440Srrsout_now: 5164171440Srrs#ifdef INVARIANTS 5165171440Srrs sctp_validate_no_locks(inp); 5166171440Srrs#endif 5167169378Srrs return; 5168163953Srrs} 5169163953Srrs 5170163953Srrs 5171163953Srrs 5172163953Srrsvoid 5173165647Srrssctp_input(i_pak, off) 5174165647Srrs struct mbuf *i_pak; 5175163953Srrs int off; 5176163953Srrs 5177163953Srrs{ 5178163953Srrs#ifdef SCTP_MBUF_LOGGING 5179163953Srrs struct mbuf *mat; 5180163953Srrs 5181163953Srrs#endif 5182165647Srrs struct mbuf *m; 5183163953Srrs int iphlen; 5184170181Srrs uint32_t vrf_id = 0; 5185163953Srrs uint8_t ecn_bits; 5186163953Srrs struct ip *ip; 5187163953Srrs struct sctphdr *sh; 5188163953Srrs struct sctp_inpcb *inp = NULL; 5189163953Srrs 5190163953Srrs uint32_t check, calc_check; 5191163953Srrs struct sctp_nets *net; 5192163953Srrs struct sctp_tcb *stcb = NULL; 5193163953Srrs struct sctp_chunkhdr *ch; 5194163953Srrs int refcount_up = 0; 5195163953Srrs int length, mlen, offset; 5196163953Srrs 5197169352Srrs 5198169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5199169352Srrs SCTP_RELEASE_PKT(i_pak); 5200169352Srrs return; 5201169352Srrs } 5202168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5203163953Srrs iphlen = off; 5204165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5205170056Srrs 5206163953Srrs net = NULL; 5207163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5208163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5209163953Srrs 5210170091Srrs 5211163953Srrs#ifdef SCTP_MBUF_LOGGING 5212163953Srrs /* Log in any input mbufs */ 5213170744Srrs if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) { 5214170744Srrs mat = m; 5215170744Srrs while (mat) { 5216170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5217170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5218170744Srrs } 5219170744Srrs mat = SCTP_BUF_NEXT(mat); 5220163953Srrs } 5221163953Srrs } 5222163953Srrs#endif 5223170091Srrs#ifdef SCTP_PACKET_LOGGING 5224170921Srrs if (sctp_logging_level & SCTP_LAST_PACKET_TRACING) 5225170921Srrs sctp_packet_log(m, mlen); 5226170091Srrs#endif 5227170091Srrs /* 5228170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5229170091Srrs * case) 5230170091Srrs */ 5231170091Srrs mlen -= iphlen; 5232168124Srrs 5233163953Srrs /* 5234163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5235163953Srrs */ 5236163953Srrs ip = mtod(m, struct ip *); 5237163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5238165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5239163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5240163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5241163953Srrs return; 5242163953Srrs } 5243163953Srrs ip = mtod(m, struct ip *); 5244163953Srrs } 5245163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5246163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5247170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5248170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5249163953Srrs 5250163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5251163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5252163953Srrs goto bad; 5253163953Srrs } 5254168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5255163953Srrs /* 5256163953Srrs * We only look at broadcast if its a front state, All 5257163953Srrs * others we will not have a tcb for anyway. 5258163953Srrs */ 5259163953Srrs goto bad; 5260163953Srrs } 5261163953Srrs /* validate SCTP checksum */ 5262171990Srrs check = sh->checksum; /* save incoming checksum */ 5263171990Srrs if ((check == 0) && (sctp_no_csum_on_loopback) && 5264171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5265171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5266171990Srrs ) { 5267171990Srrs goto sctp_skip_csum_4; 5268171990Srrs } 5269171990Srrs sh->checksum = 0; /* prepare for calc */ 5270171990Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 5271171990Srrs if (calc_check != check) { 5272171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5273171990Srrs calc_check, check, m, mlen, iphlen); 5274163953Srrs 5275171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5276171990Srrs offset - sizeof(*ch), 5277171990Srrs sh, ch, &inp, &net, 5278171990Srrs vrf_id); 5279171990Srrs if ((inp) && (stcb)) { 5280171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5281172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5282171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5283171990Srrs refcount_up = 1; 5284163953Srrs } 5285171990Srrs SCTP_STAT_INCR(sctps_badsum); 5286171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5287171990Srrs goto bad; 5288168299Srrs } 5289171990Srrs sh->checksum = calc_check; 5290163953Srrssctp_skip_csum_4: 5291168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5292168124Srrs if (sh->dest_port == 0) { 5293168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5294168124Srrs goto bad; 5295168124Srrs } 5296163953Srrs /* validate mbuf chain length with IP payload length */ 5297163953Srrs if (mlen < (ip->ip_len - iphlen)) { 5298163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5299163953Srrs goto bad; 5300163953Srrs } 5301163953Srrs /* 5302163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5303163953Srrs * IP/SCTP/first chunk header... 5304163953Srrs */ 5305163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5306168299Srrs sh, ch, &inp, &net, vrf_id); 5307163953Srrs /* inp's ref-count increased && stcb locked */ 5308163953Srrs if (inp == NULL) { 5309163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5310163953Srrs 5311163953Srrs SCTP_STAT_INCR(sctps_noport); 5312163953Srrs#ifdef ICMP_BANDLIM 5313163953Srrs /* 5314163953Srrs * we use the bandwidth limiting to protect against sending 5315163953Srrs * too many ABORTS all at once. In this case these count the 5316163953Srrs * same as an ICMP message. 5317163953Srrs */ 5318163953Srrs if (badport_bandlim(0) < 0) 5319163953Srrs goto bad; 5320163953Srrs#endif /* ICMP_BANDLIM */ 5321169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5322169420Srrs "Sending a ABORT from packet entry!\n"); 5323163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5324163953Srrs /* 5325163953Srrs * we do a trick here to get the INIT tag, dig in 5326163953Srrs * and get the tag from the INIT and put it in the 5327163953Srrs * common header. 5328163953Srrs */ 5329163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5330163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5331163953Srrs (uint8_t *) & chunk_buf); 5332163953Srrs if (init_chk != NULL) 5333163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5334163953Srrs } 5335165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5336170181Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id); 5337165220Srrs goto bad; 5338165220Srrs } 5339165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5340165220Srrs goto bad; 5341165220Srrs } 5342165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5343170181Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id); 5344163953Srrs goto bad; 5345163953Srrs } else if (stcb == NULL) { 5346163953Srrs refcount_up = 1; 5347163953Srrs } 5348171167Sgnn#ifdef IPSEC 5349163953Srrs /* 5350163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5351163953Srrs * idea, so I will leave it in place. 5352163953Srrs */ 5353163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5354171133Sgnn ipsec4stat.in_polvio++; 5355163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5356163953Srrs goto bad; 5357163953Srrs } 5358163953Srrs#endif /* IPSEC */ 5359163953Srrs 5360163953Srrs /* 5361163953Srrs * common chunk processing 5362163953Srrs */ 5363163953Srrs length = ip->ip_len + iphlen; 5364163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5365163953Srrs 5366163953Srrs ecn_bits = ip->ip_tos; 5367163953Srrs 5368169655Srrs /* sa_ignore NO_NULL_CHK */ 5369163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5370170181Srrs inp, stcb, net, ecn_bits, vrf_id); 5371163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5372163953Srrs if (m) { 5373163953Srrs sctp_m_freem(m); 5374163953Srrs } 5375163953Srrs if ((inp) && (refcount_up)) { 5376163953Srrs /* reduce ref-count */ 5377163953Srrs SCTP_INP_DECR_REF(inp); 5378163953Srrs } 5379163953Srrs return; 5380163953Srrsbad: 5381169420Srrs if (stcb) { 5382163953Srrs SCTP_TCB_UNLOCK(stcb); 5383169420Srrs } 5384163953Srrs if ((inp) && (refcount_up)) { 5385163953Srrs /* reduce ref-count */ 5386163953Srrs SCTP_INP_DECR_REF(inp); 5387163953Srrs } 5388163953Srrs if (m) { 5389163953Srrs sctp_m_freem(m); 5390163953Srrs } 5391163953Srrs return; 5392163953Srrs} 5393