sctp_input.c revision 208853
1163953Srrs/*- 2185694Srrs * Copyright (c) 2001-2008, 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 208853 2010-06-05 21:20:28Z 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> 49188067Srrs#include <netinet/sctp_crc32.h> 50179157Srrs#include <netinet/udp.h> 51163953Srrs 52163953Srrs 53163953Srrs 54163953Srrsstatic void 55163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 56163953Srrs{ 57163953Srrs struct sctp_nets *net; 58163953Srrs 59165220Srrs /* 60165220Srrs * This now not only stops all cookie timers it also stops any INIT 61165220Srrs * timers as well. This will make sure that the timers are stopped 62165220Srrs * in all collision cases. 63165220Srrs */ 64163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 65163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 66165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 67163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 68163953Srrs stcb->sctp_ep, 69163953Srrs stcb, 70165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 71165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 72165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 73165220Srrs stcb->sctp_ep, 74165220Srrs stcb, 75165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 76163953Srrs } 77163953Srrs } 78163953Srrs} 79163953Srrs 80163953Srrs/* INIT handler */ 81163953Srrsstatic void 82163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 83163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 84179157Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 85163953Srrs{ 86163953Srrs struct sctp_init *init; 87163953Srrs struct mbuf *op_err; 88163953Srrs uint32_t init_limit; 89163953Srrs 90169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 91169420Srrs stcb); 92172396Srrs if (stcb == NULL) { 93172396Srrs SCTP_INP_RLOCK(inp); 94172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 95172396Srrs goto outnow; 96172396Srrs } 97172396Srrs } 98163953Srrs op_err = NULL; 99163953Srrs init = &cp->init; 100163953Srrs /* First are we accepting? */ 101163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 102169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 103169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 104169420Srrs inp->sctp_socket->so_qlimit); 105163953Srrs /* 106163953Srrs * FIX ME ?? What about TCP model and we have a 107179141Srrs * match/restart case? Actually no fix is needed. the lookup 108179141Srrs * will always find the existing assoc so stcb would not be 109179141Srrs * NULL. It may be questionable to do this since we COULD 110179141Srrs * just send back the INIT-ACK and hope that the app did 111179141Srrs * accept()'s by the time the COOKIE was sent. But there is 112179141Srrs * a price to pay for COOKIE generation and I don't want to 113179141Srrs * pay it on the chance that the app will actually do some 114179141Srrs * accepts(). The App just looses and should NOT be in this 115179141Srrs * state :-) 116163953Srrs */ 117169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 118179157Srrs vrf_id, port); 119168299Srrs if (stcb) 120168299Srrs *abort_no_unlock = 1; 121172396Srrs goto outnow; 122163953Srrs } 123163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 124163953Srrs /* Invalid length */ 125163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 126169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 127179157Srrs vrf_id, port); 128168299Srrs if (stcb) 129168299Srrs *abort_no_unlock = 1; 130172396Srrs goto outnow; 131163953Srrs } 132163953Srrs /* validate parameters */ 133163953Srrs if (init->initiate_tag == 0) { 134163953Srrs /* protocol error... send abort */ 135163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 136169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 137179157Srrs vrf_id, port); 138168299Srrs if (stcb) 139168299Srrs *abort_no_unlock = 1; 140172396Srrs goto outnow; 141163953Srrs } 142163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 143163953Srrs /* invalid parameter... send abort */ 144163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 145169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 146179157Srrs vrf_id, port); 147171440Srrs if (stcb) 148171440Srrs *abort_no_unlock = 1; 149172396Srrs goto outnow; 150163953Srrs } 151163953Srrs if (init->num_inbound_streams == 0) { 152163953Srrs /* protocol error... send abort */ 153163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 154169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 155179157Srrs vrf_id, port); 156168299Srrs if (stcb) 157168299Srrs *abort_no_unlock = 1; 158172396Srrs goto outnow; 159163953Srrs } 160163953Srrs if (init->num_outbound_streams == 0) { 161163953Srrs /* protocol error... send abort */ 162163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 163169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 164179157Srrs vrf_id, port); 165168299Srrs if (stcb) 166168299Srrs *abort_no_unlock = 1; 167172396Srrs goto outnow; 168163953Srrs } 169163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 170163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 171163953Srrs init_limit)) { 172163953Srrs /* auth parameter(s) error... send abort */ 173179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 174168299Srrs if (stcb) 175168299Srrs *abort_no_unlock = 1; 176172396Srrs goto outnow; 177163953Srrs } 178163953Srrs /* send an INIT-ACK w/cookie */ 179169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 180179157Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 181172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 182172396Srrsoutnow: 183172396Srrs if (stcb == NULL) { 184172396Srrs SCTP_INP_RUNLOCK(inp); 185172396Srrs } 186163953Srrs} 187163953Srrs 188163953Srrs/* 189163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 190163953Srrs */ 191171158Srrs 192171158Srrsint 193171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 194171158Srrs{ 195171158Srrs int unsent_data = 0; 196171158Srrs struct sctp_stream_queue_pending *sp; 197171158Srrs struct sctp_stream_out *strq; 198171158Srrs struct sctp_association *asoc; 199171158Srrs 200171158Srrs /* 201171158Srrs * This function returns the number of streams that have true unsent 202171158Srrs * data on them. Note that as it looks through it will clean up any 203171158Srrs * places that have old data that has been sent but left at top of 204171158Srrs * stream queue. 205171158Srrs */ 206171158Srrs asoc = &stcb->asoc; 207171158Srrs SCTP_TCB_SEND_LOCK(stcb); 208171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 209171158Srrs /* Check to see if some data queued */ 210171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 211171531Srrs is_there_another: 212171158Srrs /* sa_ignore FREED_MEMORY */ 213171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 214171158Srrs if (sp == NULL) { 215171158Srrs continue; 216171158Srrs } 217171158Srrs if ((sp->msg_is_complete) && 218171158Srrs (sp->length == 0) && 219171158Srrs (sp->sender_all_done)) { 220171158Srrs /* 221171158Srrs * We are doing differed cleanup. Last time 222171158Srrs * through when we took all the data the 223171158Srrs * sender_all_done was not set. 224171158Srrs */ 225171158Srrs if (sp->put_last_out == 0) { 226171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 227171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 228171158Srrs sp->sender_all_done, 229171158Srrs sp->length, 230171158Srrs sp->msg_is_complete, 231171158Srrs sp->put_last_out); 232171158Srrs } 233171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 234171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 235171158Srrs sctp_free_remote_addr(sp->net); 236171158Srrs if (sp->data) { 237171158Srrs sctp_m_freem(sp->data); 238171158Srrs sp->data = NULL; 239171158Srrs } 240171158Srrs sctp_free_a_strmoq(stcb, sp); 241171158Srrs goto is_there_another; 242171158Srrs } else { 243171158Srrs unsent_data++; 244171158Srrs continue; 245171158Srrs } 246171158Srrs } 247171158Srrs } 248171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 249171158Srrs return (unsent_data); 250171158Srrs} 251171158Srrs 252163953Srrsstatic int 253163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 254163953Srrs struct sctp_nets *net) 255163953Srrs{ 256163953Srrs struct sctp_init *init; 257163953Srrs struct sctp_association *asoc; 258163953Srrs struct sctp_nets *lnet; 259163953Srrs unsigned int i; 260163953Srrs 261163953Srrs init = &cp->init; 262163953Srrs asoc = &stcb->asoc; 263163953Srrs /* save off parameters */ 264163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 265163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 266163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 267163953Srrs /* update any ssthresh's that may have a default */ 268163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 269163953Srrs lnet->ssthresh = asoc->peers_rwnd; 270163953Srrs 271179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 272170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 273170744Srrs } 274163953Srrs } 275163953Srrs } 276164181Srrs SCTP_TCB_SEND_LOCK(stcb); 277163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 278163953Srrs unsigned int newcnt; 279163953Srrs struct sctp_stream_out *outs; 280163953Srrs struct sctp_stream_queue_pending *sp; 281196260Stuexen struct sctp_tmit_chunk *chk, *chk_next; 282163953Srrs 283196260Stuexen /* abandon the upper streams */ 284163953Srrs newcnt = ntohs(init->num_inbound_streams); 285196260Stuexen if (!TAILQ_EMPTY(&asoc->send_queue)) { 286196260Stuexen chk = TAILQ_FIRST(&asoc->send_queue); 287196260Stuexen while (chk) { 288196260Stuexen chk_next = TAILQ_NEXT(chk, sctp_next); 289196260Stuexen if (chk->rec.data.stream_number >= newcnt) { 290196260Stuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 291196260Stuexen asoc->send_queue_cnt--; 292196260Stuexen if (chk->data != NULL) { 293196260Stuexen sctp_free_bufspace(stcb, asoc, chk, 1); 294196260Stuexen sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 295196260Stuexen SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 296196260Stuexen if (chk->data) { 297196260Stuexen sctp_m_freem(chk->data); 298196260Stuexen chk->data = NULL; 299196260Stuexen } 300196260Stuexen } 301196260Stuexen sctp_free_a_chunk(stcb, chk); 302196260Stuexen /* sa_ignore FREED_MEMORY */ 303196260Stuexen } 304196260Stuexen chk = chk_next; 305196260Stuexen } 306196260Stuexen } 307163953Srrs if (asoc->strmout) { 308163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 309163953Srrs outs = &asoc->strmout[i]; 310163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 311163953Srrs while (sp) { 312196260Stuexen TAILQ_REMOVE(&outs->outqueue, sp, next); 313163953Srrs asoc->stream_queue_cnt--; 314163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 315163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 316172090Srrs sp, SCTP_SO_NOT_LOCKED); 317163953Srrs if (sp->data) { 318163953Srrs sctp_m_freem(sp->data); 319163953Srrs sp->data = NULL; 320163953Srrs } 321163953Srrs sctp_free_remote_addr(sp->net); 322163953Srrs sp->net = NULL; 323163953Srrs /* Free the chunk */ 324163953Srrs sctp_free_a_strmoq(stcb, sp); 325169655Srrs /* sa_ignore FREED_MEMORY */ 326163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 327163953Srrs } 328163953Srrs } 329163953Srrs } 330196260Stuexen /* cut back the count */ 331163953Srrs asoc->pre_open_streams = newcnt; 332163953Srrs } 333164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 334188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 335163953Srrs /* init tsn's */ 336163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 337185694Srrs /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 338185694Srrs asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 339179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 340170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 341170744Srrs } 342163953Srrs /* This is the next one we expect */ 343163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 344163953Srrs 345163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 346180955Srrs asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 347163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 348163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 349163953Srrs /* open the requested streams */ 350170091Srrs 351163953Srrs if (asoc->strmin != NULL) { 352163953Srrs /* Free the old ones */ 353164181Srrs struct sctp_queued_to_read *ctl; 354164181Srrs 355164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 356164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 357164181Srrs while (ctl) { 358164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 359164181Srrs sctp_free_remote_addr(ctl->whoFrom); 360171158Srrs ctl->whoFrom = NULL; 361164181Srrs sctp_m_freem(ctl->data); 362164181Srrs ctl->data = NULL; 363164181Srrs sctp_free_a_readq(stcb, ctl); 364164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 365164181Srrs } 366164181Srrs } 367170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 368163953Srrs } 369164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 370164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 371164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 372164181Srrs } 373163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 374170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 375163953Srrs if (asoc->strmin == NULL) { 376163953Srrs /* we didn't get memory for the streams! */ 377169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 378163953Srrs return (-1); 379163953Srrs } 380163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 381163953Srrs asoc->strmin[i].stream_no = i; 382163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 383163953Srrs /* 384163953Srrs * U-stream ranges will be set when the cookie is unpacked. 385163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 386163953Srrs * supported) when the INIT-ACK arrives. 387163953Srrs */ 388163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 389168943Srrs asoc->strmin[i].delivery_started = 0; 390163953Srrs } 391163953Srrs /* 392163953Srrs * load_address_from_init will put the addresses into the 393163953Srrs * association when the COOKIE is processed or the INIT-ACK is 394163953Srrs * processed. Both types of COOKIE's existing and new call this 395163953Srrs * routine. It will remove addresses that are no longer in the 396163953Srrs * association (for the restarting case where addresses are 397163953Srrs * removed). Up front when the INIT arrives we will discard it if it 398163953Srrs * is a restart and new addresses have been added. 399163953Srrs */ 400169655Srrs /* sa_ignore MEMLEAK */ 401163953Srrs return (0); 402163953Srrs} 403163953Srrs 404163953Srrs/* 405163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 406163953Srrs */ 407163953Srrsstatic int 408163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 409163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 410170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 411163953Srrs{ 412163953Srrs struct sctp_association *asoc; 413163953Srrs struct mbuf *op_err; 414163953Srrs int retval, abort_flag; 415163953Srrs uint32_t initack_limit; 416185694Srrs int nat_friendly = 0; 417163953Srrs 418163953Srrs /* First verify that we have no illegal param's */ 419163953Srrs abort_flag = 0; 420163953Srrs op_err = NULL; 421163953Srrs 422163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 423163953Srrs (offset + sizeof(struct sctp_init_chunk)), 424185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 425163953Srrs if (abort_flag) { 426163953Srrs /* Send an abort and notify peer */ 427172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 428170056Srrs *abort_no_unlock = 1; 429163953Srrs return (-1); 430163953Srrs } 431163953Srrs asoc = &stcb->asoc; 432185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 433163953Srrs /* process the peer's parameters in the INIT-ACK */ 434163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 435163953Srrs if (retval < 0) { 436163953Srrs return (retval); 437163953Srrs } 438163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 439163953Srrs /* load all addresses */ 440163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 441163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 442163953Srrs NULL))) { 443163953Srrs /* Huh, we should abort */ 444169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 445169420Srrs "Load addresses from INIT causes an abort %d\n", 446169420Srrs retval); 447163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 448179157Srrs NULL, 0, net->port); 449168299Srrs *abort_no_unlock = 1; 450163953Srrs return (-1); 451163953Srrs } 452171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 453171477Srrs if (asoc->peer_supports_asconf == 0) { 454171477Srrs struct sctp_asconf_addr *aparam; 455171477Srrs 456171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 457171477Srrs /* sa_ignore FREED_MEMORY */ 458171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 459171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 460171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 461171477Srrs } 462171477Srrs } 463163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 464163953Srrs stcb->asoc.local_hmacs); 465163953Srrs if (op_err) { 466163953Srrs sctp_queue_op_err(stcb, op_err); 467163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 468163953Srrs op_err = NULL; 469163953Srrs } 470163953Srrs /* extract the cookie and queue it to "echo" it back... */ 471179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 472171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 473171943Srrs stcb->asoc.overall_error_count, 474171943Srrs 0, 475171943Srrs SCTP_FROM_SCTP_INPUT, 476171943Srrs __LINE__); 477171943Srrs } 478163953Srrs stcb->asoc.overall_error_count = 0; 479163953Srrs net->error_count = 0; 480163953Srrs 481163953Srrs /* 482163953Srrs * Cancel the INIT timer, We do this first before queueing the 483163953Srrs * cookie. We always cancel at the primary to assue that we are 484163953Srrs * canceling the timer started by the INIT which always goes to the 485163953Srrs * primary. 486163953Srrs */ 487163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 488165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 489163953Srrs 490165220Srrs /* calculate the RTO */ 491171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 492165220Srrs 493163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 494163953Srrs if (retval < 0) { 495163953Srrs /* 496163953Srrs * No cookie, we probably should send a op error. But in any 497163953Srrs * case if there is no cookie in the INIT-ACK, we can 498163953Srrs * abandon the peer, its broke. 499163953Srrs */ 500163953Srrs if (retval == -3) { 501163953Srrs /* We abort with an error of missing mandatory param */ 502163953Srrs op_err = 503163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 504163953Srrs if (op_err) { 505163953Srrs /* 506163953Srrs * Expand beyond to include the mandatory 507163953Srrs * param cookie 508163953Srrs */ 509163953Srrs struct sctp_inv_mandatory_param *mp; 510163953Srrs 511165647Srrs SCTP_BUF_LEN(op_err) = 512163953Srrs sizeof(struct sctp_inv_mandatory_param); 513163953Srrs mp = mtod(op_err, 514163953Srrs struct sctp_inv_mandatory_param *); 515163953Srrs /* Subtract the reserved param */ 516163953Srrs mp->length = 517163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 518163953Srrs mp->num_param = htonl(1); 519163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 520163953Srrs mp->resv = 0; 521163953Srrs } 522163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 523179157Srrs sh, op_err, 0, net->port); 524168299Srrs *abort_no_unlock = 1; 525163953Srrs } 526163953Srrs return (retval); 527163953Srrs } 528163953Srrs return (0); 529163953Srrs} 530163953Srrs 531163953Srrsstatic void 532163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 533163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 534163953Srrs{ 535163953Srrs struct sockaddr_storage store; 536163953Srrs struct sockaddr_in *sin; 537163953Srrs struct sockaddr_in6 *sin6; 538163953Srrs struct sctp_nets *r_net; 539163953Srrs struct timeval tv; 540172396Srrs int req_prim = 0; 541163953Srrs 542163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 543163953Srrs /* Invalid length */ 544163953Srrs return; 545163953Srrs } 546163953Srrs sin = (struct sockaddr_in *)&store; 547163953Srrs sin6 = (struct sockaddr_in6 *)&store; 548163953Srrs 549163953Srrs memset(&store, 0, sizeof(store)); 550163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 551163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 552163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 553163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 554163953Srrs sin->sin_port = stcb->rport; 555163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 556163953Srrs sizeof(sin->sin_addr)); 557163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 558163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 559163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 560163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 561163953Srrs sin6->sin6_port = stcb->rport; 562163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 563163953Srrs sizeof(sin6->sin6_addr)); 564163953Srrs } else { 565163953Srrs return; 566163953Srrs } 567163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 568163953Srrs if (r_net == NULL) { 569169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 570163953Srrs return; 571163953Srrs } 572163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 573163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 574163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 575163953Srrs /* 576163953Srrs * If the its a HB and it's random value is correct when can 577163953Srrs * confirm the destination. 578163953Srrs */ 579163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 580167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 581167598Srrs stcb->asoc.primary_destination = r_net; 582167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 583167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 584167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 585167598Srrs if (r_net != stcb->asoc.primary_destination) { 586167598Srrs /* 587167598Srrs * first one on the list is NOT the primary 588167598Srrs * sctp_cmpaddr() is much more efficent if 589167598Srrs * the primary is the first on the list, 590167598Srrs * make it so. 591167598Srrs */ 592167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 593167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 594167598Srrs } 595172396Srrs req_prim = 1; 596167598Srrs } 597163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 598172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 599163953Srrs } 600163953Srrs r_net->error_count = 0; 601163953Srrs r_net->hb_responded = 1; 602163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 603163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 604163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 605163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 606163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 607163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 608172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 609163953Srrs /* now was it the primary? if so restore */ 610163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 611169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 612163953Srrs } 613163953Srrs } 614171440Srrs /* 615171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 616171440Srrs * set the destination to active state and set the cwnd to one or 617171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 618171440Srrs * timer is running, for the destination, stop the timer because a 619171440Srrs * PF-heartbeat was received. 620171440Srrs */ 621179783Srrs if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && 622179783Srrs SCTP_BASE_SYSCTL(sctp_cmt_pf) && 623179783Srrs (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) { 624171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 625171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 626171440Srrs stcb, net, 627171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 628171440Srrs } 629171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 630179783Srrs net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf); 631171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 632171440Srrs net, net->cwnd); 633171440Srrs } 634163953Srrs /* Now lets do a RTO with this */ 635171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 636172396Srrs /* Mobility adaptation */ 637172396Srrs if (req_prim) { 638172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 639172396Srrs SCTP_MOBILITY_BASE) || 640172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 641172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 642172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 643172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 644172396Srrs 645172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 646172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 647172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 648172396Srrs sctp_assoc_immediate_retrans(stcb, 649172396Srrs stcb->asoc.primary_destination); 650172396Srrs } 651172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 652172396Srrs SCTP_MOBILITY_BASE)) { 653172396Srrs sctp_move_chunks_from_deleted_prim(stcb, 654172396Srrs stcb->asoc.primary_destination); 655172396Srrs } 656172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 657172396Srrs stcb->asoc.deleted_primary); 658172396Srrs } 659172396Srrs } 660163953Srrs} 661163953Srrs 662185694Srrsstatic int 663185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 664185694Srrs{ 665185694Srrs /* 666185694Srrs * return 0 means we want you to proceed with the abort non-zero 667185694Srrs * means no abort processing 668185694Srrs */ 669185694Srrs struct sctpasochead *head; 670185694Srrs 671185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 672185694Srrs /* generate a new vtag and send init */ 673185694Srrs LIST_REMOVE(stcb, sctp_asocs); 674185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 675185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 676185694Srrs /* 677185694Srrs * put it in the bucket in the vtag hash of assoc's for the 678185694Srrs * system 679185694Srrs */ 680185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 681185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 682185694Srrs return (1); 683185694Srrs } 684185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 685185694Srrs /* 686185694Srrs * treat like a case where the cookie expired i.e.: - dump 687185694Srrs * current cookie. - generate a new vtag. - resend init. 688185694Srrs */ 689185694Srrs /* generate a new vtag and send init */ 690185694Srrs LIST_REMOVE(stcb, sctp_asocs); 691185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 692185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 693185694Srrs sctp_stop_all_cookie_timers(stcb); 694185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 695185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 696185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 697185694Srrs /* 698185694Srrs * put it in the bucket in the vtag hash of assoc's for the 699185694Srrs * system 700185694Srrs */ 701185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 702185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 703185694Srrs return (1); 704185694Srrs } 705185694Srrs return (0); 706185694Srrs} 707185694Srrs 708185694Srrsstatic int 709185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 710185694Srrs struct sctp_nets *net) 711185694Srrs{ 712185694Srrs /* 713185694Srrs * return 0 means we want you to proceed with the abort non-zero 714185694Srrs * means no abort processing 715185694Srrs */ 716185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 717185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 718185694Srrs return (0); 719185694Srrs } 720185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 721185694Srrs return (1); 722185694Srrs} 723185694Srrs 724185694Srrs 725163953Srrsstatic void 726163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 727163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 728163953Srrs{ 729172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 730172090Srrs struct socket *so; 731172090Srrs 732172090Srrs#endif 733185694Srrs uint16_t len; 734172090Srrs 735169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 736163953Srrs if (stcb == NULL) 737163953Srrs return; 738163953Srrs 739185694Srrs len = ntohs(cp->ch.chunk_length); 740185694Srrs if (len > sizeof(struct sctp_chunkhdr)) { 741185694Srrs /* 742185694Srrs * Need to check the cause codes for our two magic nat 743185694Srrs * aborts which don't kill the assoc necessarily. 744185694Srrs */ 745185694Srrs struct sctp_abort_chunk *cpnext; 746185694Srrs struct sctp_missing_nat_state *natc; 747185694Srrs uint16_t cause; 748185694Srrs 749185694Srrs cpnext = cp; 750185694Srrs cpnext++; 751185694Srrs natc = (struct sctp_missing_nat_state *)cpnext; 752185694Srrs cause = ntohs(natc->cause); 753185694Srrs if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 754185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 755185694Srrs cp->ch.chunk_flags); 756185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 757185694Srrs return; 758185694Srrs } 759185694Srrs } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 760185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 761185694Srrs cp->ch.chunk_flags); 762185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 763185694Srrs return; 764185694Srrs } 765185694Srrs } 766185694Srrs } 767163953Srrs /* stop any receive timers */ 768171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 769163953Srrs /* notify user of the abort and clean up... */ 770172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 771163953Srrs /* free the tcb */ 772172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 773172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 774172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 775172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 776172396Srrs panic("Received an ABORT"); 777172396Srrs } else { 778172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 779172396Srrs } 780172396Srrs#endif 781163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 782163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 783163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 784163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 785163953Srrs } 786168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 787168859Srrs sctp_print_out_track_log(stcb); 788168859Srrs#endif 789172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 790172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 791172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 792172090Srrs SCTP_TCB_UNLOCK(stcb); 793172090Srrs SCTP_SOCKET_LOCK(so, 1); 794172090Srrs SCTP_TCB_LOCK(stcb); 795172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 796172090Srrs#endif 797171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 798171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 799171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 800172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 801172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 802172090Srrs#endif 803169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 804163953Srrs} 805163953Srrs 806163953Srrsstatic void 807163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 808163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 809163953Srrs{ 810163953Srrs struct sctp_association *asoc; 811163953Srrs int some_on_streamwheel; 812163953Srrs 813172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 814172090Srrs struct socket *so; 815172090Srrs 816172090Srrs#endif 817172090Srrs 818169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 819169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 820163953Srrs if (stcb == NULL) 821163953Srrs return; 822165220Srrs asoc = &stcb->asoc; 823165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 824165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 825163953Srrs return; 826163953Srrs } 827163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 828163953Srrs /* Shutdown NOT the expected size */ 829163953Srrs return; 830163953Srrs } else { 831163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 832199477Stuexen if (*abort_flag) { 833199477Stuexen return; 834199477Stuexen } 835163953Srrs } 836165220Srrs if (asoc->control_pdapi) { 837163953Srrs /* 838163953Srrs * With a normal shutdown we assume the end of last record. 839163953Srrs */ 840163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 841165220Srrs asoc->control_pdapi->end_added = 1; 842165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 843165220Srrs asoc->control_pdapi = NULL; 844163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 845172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 846172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 847172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 848172090Srrs SCTP_TCB_UNLOCK(stcb); 849172090Srrs SCTP_SOCKET_LOCK(so, 1); 850172090Srrs SCTP_TCB_LOCK(stcb); 851172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 852172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 853172090Srrs /* assoc was freed while we were unlocked */ 854172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 855172090Srrs return; 856172090Srrs } 857172090Srrs#endif 858163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 859172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 860172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 861172090Srrs#endif 862163953Srrs } 863163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 864163953Srrs if (stcb->sctp_socket) { 865163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 866170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 867163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 868171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 869172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 870163953Srrs /* 871163953Srrs * notify upper layer that peer has initiated a 872163953Srrs * shutdown 873163953Srrs */ 874172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 875163953Srrs 876163953Srrs /* reset time */ 877169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 878163953Srrs } 879163953Srrs } 880163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 881163953Srrs /* 882163953Srrs * stop the shutdown timer, since we WILL move to 883163953Srrs * SHUTDOWN-ACK-SENT. 884163953Srrs */ 885171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 886163953Srrs } 887171158Srrs /* Now is there unsent data on a stream somewhere? */ 888171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 889163953Srrs 890163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 891163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 892163953Srrs some_on_streamwheel) { 893163953Srrs /* By returning we will push more data out */ 894163953Srrs return; 895163953Srrs } else { 896163953Srrs /* no outstanding data to send, so move on... */ 897163953Srrs /* send SHUTDOWN-ACK */ 898163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 899163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 900166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 901166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 902163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 903163953Srrs } 904171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 905172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 906171990Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, 907171990Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 908163953Srrs /* start SHUTDOWN timer */ 909163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 910163953Srrs stcb, net); 911163953Srrs } 912163953Srrs} 913163953Srrs 914163953Srrsstatic void 915163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 916204141Stuexen struct sctp_tcb *stcb, 917204141Stuexen struct sctp_nets *net) 918163953Srrs{ 919163953Srrs struct sctp_association *asoc; 920163953Srrs 921172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 922172090Srrs struct socket *so; 923172090Srrs 924172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 925172090Srrs#endif 926169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 927169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 928163953Srrs if (stcb == NULL) 929163953Srrs return; 930163953Srrs 931163953Srrs asoc = &stcb->asoc; 932163953Srrs /* process according to association state */ 933204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 934204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 935204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 936204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 937204141Stuexen SCTP_TCB_UNLOCK(stcb); 938204141Stuexen return; 939204141Stuexen } 940163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 941163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 942163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 943163953Srrs SCTP_TCB_UNLOCK(stcb); 944163953Srrs return; 945163953Srrs } 946165220Srrs if (asoc->control_pdapi) { 947163953Srrs /* 948163953Srrs * With a normal shutdown we assume the end of last record. 949163953Srrs */ 950163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 951165220Srrs asoc->control_pdapi->end_added = 1; 952165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 953165220Srrs asoc->control_pdapi = NULL; 954163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 955172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 956172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 957172090Srrs SCTP_TCB_UNLOCK(stcb); 958172090Srrs SCTP_SOCKET_LOCK(so, 1); 959172090Srrs SCTP_TCB_LOCK(stcb); 960172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 961172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 962172090Srrs /* assoc was freed while we were unlocked */ 963172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 964172090Srrs return; 965172090Srrs } 966172090Srrs#endif 967163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 968172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 969172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 970172090Srrs#endif 971163953Srrs } 972163953Srrs /* are the queues empty? */ 973163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 974163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 975163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 976172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 977163953Srrs } 978163953Srrs /* stop the timer */ 979171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 980163953Srrs /* send SHUTDOWN-COMPLETE */ 981204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 982163953Srrs /* notify upper layer protocol */ 983163953Srrs if (stcb->sctp_socket) { 984172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 985163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 986163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 987163953Srrs /* Set the connected flag to disconnected */ 988163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 989163953Srrs } 990163953Srrs } 991163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 992163953Srrs /* free the TCB but first save off the ep */ 993172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 994172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 995172090Srrs SCTP_TCB_UNLOCK(stcb); 996172090Srrs SCTP_SOCKET_LOCK(so, 1); 997172090Srrs SCTP_TCB_LOCK(stcb); 998172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 999172090Srrs#endif 1000171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1001171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1002172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1003172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1004172090Srrs#endif 1005163953Srrs} 1006163953Srrs 1007163953Srrs/* 1008163953Srrs * Skip past the param header and then we will find the chunk that caused the 1009163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1010163953Srrs * our peer must be broken. 1011163953Srrs */ 1012163953Srrsstatic void 1013163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1014163953Srrs struct sctp_nets *net) 1015163953Srrs{ 1016163953Srrs struct sctp_chunkhdr *chk; 1017163953Srrs 1018163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1019163953Srrs switch (chk->chunk_type) { 1020163953Srrs case SCTP_ASCONF_ACK: 1021163953Srrs case SCTP_ASCONF: 1022163953Srrs sctp_asconf_cleanup(stcb, net); 1023163953Srrs break; 1024163953Srrs case SCTP_FORWARD_CUM_TSN: 1025163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1026163953Srrs break; 1027163953Srrs default: 1028169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1029169420Srrs "Peer does not support chunk type %d(%x)??\n", 1030169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1031163953Srrs break; 1032163953Srrs } 1033163953Srrs} 1034163953Srrs 1035163953Srrs/* 1036163953Srrs * Skip past the param header and then we will find the param that caused the 1037163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1038163953Srrs * these will turn of specific features. 1039163953Srrs */ 1040163953Srrsstatic void 1041163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1042163953Srrs{ 1043163953Srrs struct sctp_paramhdr *pbad; 1044163953Srrs 1045163953Srrs pbad = phdr + 1; 1046163953Srrs switch (ntohs(pbad->param_type)) { 1047163953Srrs /* pr-sctp draft */ 1048163953Srrs case SCTP_PRSCTP_SUPPORTED: 1049163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1050163953Srrs break; 1051163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1052163953Srrs break; 1053163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1054185694Srrs case SCTP_HAS_NAT_SUPPORT: 1055185694Srrs stcb->asoc.peer_supports_nat = 0; 1056185694Srrs break; 1057163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 1058163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 1059163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 1060163953Srrs stcb->asoc.ecn_allowed = 0; 1061163953Srrs break; 1062163953Srrs case SCTP_ADD_IP_ADDRESS: 1063163953Srrs case SCTP_DEL_IP_ADDRESS: 1064163953Srrs case SCTP_SET_PRIM_ADDR: 1065163953Srrs stcb->asoc.peer_supports_asconf = 0; 1066163953Srrs break; 1067163953Srrs case SCTP_SUCCESS_REPORT: 1068163953Srrs case SCTP_ERROR_CAUSE_IND: 1069169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1070169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1071169420Srrs "Turning off ASCONF to this strange peer\n"); 1072163953Srrs stcb->asoc.peer_supports_asconf = 0; 1073163953Srrs break; 1074163953Srrs default: 1075169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1076169420Srrs "Peer does not support param type %d(%x)??\n", 1077169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1078163953Srrs break; 1079163953Srrs } 1080163953Srrs} 1081163953Srrs 1082163953Srrsstatic int 1083163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1084163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1085163953Srrs{ 1086163953Srrs int chklen; 1087163953Srrs struct sctp_paramhdr *phdr; 1088163953Srrs uint16_t error_type; 1089163953Srrs uint16_t error_len; 1090163953Srrs struct sctp_association *asoc; 1091163953Srrs int adjust; 1092163953Srrs 1093172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1094172090Srrs struct socket *so; 1095172090Srrs 1096172090Srrs#endif 1097172090Srrs 1098163953Srrs /* parse through all of the errors and process */ 1099163953Srrs asoc = &stcb->asoc; 1100163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1101163953Srrs sizeof(struct sctp_chunkhdr)); 1102163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1103163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1104163953Srrs /* Process an Error Cause */ 1105163953Srrs error_type = ntohs(phdr->param_type); 1106163953Srrs error_len = ntohs(phdr->param_length); 1107163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1108163953Srrs /* invalid param length for this param */ 1109169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1110169420Srrs chklen, error_len); 1111163953Srrs return (0); 1112163953Srrs } 1113163953Srrs switch (error_type) { 1114163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1115163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1116163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1117163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1118169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1119169420Srrs error_type); 1120163953Srrs break; 1121185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1122185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1123185694Srrs ch->chunk_flags); 1124185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1125185694Srrs return (0); 1126185694Srrs } 1127185694Srrs break; 1128185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1129185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1130185694Srrs ch->chunk_flags); 1131185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1132185694Srrs return (0); 1133185694Srrs } 1134185694Srrs break; 1135163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1136163953Srrs /* 1137163953Srrs * We only act if we have echoed a cookie and are 1138163953Srrs * waiting. 1139163953Srrs */ 1140163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1141163953Srrs int *p; 1142163953Srrs 1143163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1144163953Srrs /* Save the time doubled */ 1145163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1146163953Srrs asoc->stale_cookie_count++; 1147163953Srrs if (asoc->stale_cookie_count > 1148163953Srrs asoc->max_init_times) { 1149172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1150163953Srrs /* now free the asoc */ 1151172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1152172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1153172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1154172090Srrs SCTP_TCB_UNLOCK(stcb); 1155172090Srrs SCTP_SOCKET_LOCK(so, 1); 1156172090Srrs SCTP_TCB_LOCK(stcb); 1157172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1158172090Srrs#endif 1159171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1160171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1161172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1162172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1163172090Srrs#endif 1164163953Srrs return (-1); 1165163953Srrs } 1166163953Srrs /* blast back to INIT state */ 1167185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1168163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1169163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1170163953Srrs sctp_stop_all_cookie_timers(stcb); 1171172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1172163953Srrs } 1173163953Srrs break; 1174163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1175163953Srrs /* 1176163953Srrs * Nothing we can do here, we don't do hostname 1177163953Srrs * addresses so if the peer does not like my IPv6 1178163953Srrs * (or IPv4 for that matter) it does not matter. If 1179163953Srrs * they don't support that type of address, they can 1180163953Srrs * NOT possibly get that packet type... i.e. with no 1181163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1182163953Srrs * safely ignore this one. If we ever added support 1183163953Srrs * for HOSTNAME Addresses, then we would need to do 1184163953Srrs * something here. 1185163953Srrs */ 1186163953Srrs break; 1187163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1188163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1189163953Srrs break; 1190163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1191163953Srrs sctp_process_unrecog_param(stcb, phdr); 1192163953Srrs break; 1193163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1194163953Srrs /* 1195163953Srrs * We ignore this since the timer will drive out a 1196163953Srrs * new cookie anyway and there timer will drive us 1197163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1198163953Srrs * here since we don't have their tag. 1199163953Srrs */ 1200163953Srrs break; 1201163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1202163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1203163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1204163953Srrs /* 1205163953Srrs * We should NOT get these here, but in a 1206166086Srrs * ASCONF-ACK. 1207163953Srrs */ 1208169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1209169420Srrs error_type); 1210163953Srrs break; 1211163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1212163953Srrs /* 1213163953Srrs * And what, pray tell do we do with the fact that 1214163953Srrs * the peer is out of resources? Not really sure we 1215166086Srrs * could do anything but abort. I suspect this 1216163953Srrs * should have came WITH an abort instead of in a 1217163953Srrs * OP-ERROR. 1218163953Srrs */ 1219163953Srrs break; 1220163953Srrs default: 1221169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1222169420Srrs error_type); 1223163953Srrs break; 1224163953Srrs } 1225163953Srrs adjust = SCTP_SIZE32(error_len); 1226163953Srrs chklen -= adjust; 1227163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1228163953Srrs } 1229163953Srrs return (0); 1230163953Srrs} 1231163953Srrs 1232163953Srrsstatic int 1233169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1234169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1235170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1236163953Srrs{ 1237163953Srrs struct sctp_init_ack *init_ack; 1238163953Srrs struct mbuf *op_err; 1239163953Srrs 1240169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1241169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1242169420Srrs 1243163953Srrs if (stcb == NULL) { 1244169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1245169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1246163953Srrs return (-1); 1247163953Srrs } 1248163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1249163953Srrs /* Invalid length */ 1250163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1251163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1252179157Srrs op_err, 0, net->port); 1253168299Srrs *abort_no_unlock = 1; 1254163953Srrs return (-1); 1255163953Srrs } 1256163953Srrs init_ack = &cp->init; 1257163953Srrs /* validate parameters */ 1258163953Srrs if (init_ack->initiate_tag == 0) { 1259163953Srrs /* protocol error... send an abort */ 1260163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1261163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1262179157Srrs op_err, 0, net->port); 1263168299Srrs *abort_no_unlock = 1; 1264163953Srrs return (-1); 1265163953Srrs } 1266163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1267163953Srrs /* protocol error... send an abort */ 1268163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1269163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1270179157Srrs op_err, 0, net->port); 1271168299Srrs *abort_no_unlock = 1; 1272163953Srrs return (-1); 1273163953Srrs } 1274163953Srrs if (init_ack->num_inbound_streams == 0) { 1275163953Srrs /* protocol error... send an abort */ 1276163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1277163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1278179157Srrs op_err, 0, net->port); 1279168299Srrs *abort_no_unlock = 1; 1280163953Srrs return (-1); 1281163953Srrs } 1282163953Srrs if (init_ack->num_outbound_streams == 0) { 1283163953Srrs /* protocol error... send an abort */ 1284163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1285163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1286179157Srrs op_err, 0, net->port); 1287168299Srrs *abort_no_unlock = 1; 1288163953Srrs return (-1); 1289163953Srrs } 1290163953Srrs /* process according to association state... */ 1291171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1292163953Srrs case SCTP_STATE_COOKIE_WAIT: 1293163953Srrs /* this is the expected state for this chunk */ 1294163953Srrs /* process the INIT-ACK parameters */ 1295163953Srrs if (stcb->asoc.primary_destination->dest_state & 1296163953Srrs SCTP_ADDR_UNCONFIRMED) { 1297163953Srrs /* 1298163953Srrs * The primary is where we sent the INIT, we can 1299163953Srrs * always consider it confirmed when the INIT-ACK is 1300163953Srrs * returned. Do this before we load addresses 1301163953Srrs * though. 1302163953Srrs */ 1303163953Srrs stcb->asoc.primary_destination->dest_state &= 1304163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1305163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1306172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1307163953Srrs } 1308169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1309170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1310163953Srrs /* error in parsing parameters */ 1311163953Srrs return (-1); 1312163953Srrs } 1313163953Srrs /* update our state */ 1314169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1315171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1316163953Srrs 1317163953Srrs /* reset the RTO calc */ 1318179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1319171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1320171943Srrs stcb->asoc.overall_error_count, 1321171943Srrs 0, 1322171943Srrs SCTP_FROM_SCTP_INPUT, 1323171943Srrs __LINE__); 1324171943Srrs } 1325163953Srrs stcb->asoc.overall_error_count = 0; 1326169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1327163953Srrs /* 1328163953Srrs * collapse the init timer back in case of a exponential 1329165220Srrs * backoff 1330163953Srrs */ 1331163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1332163953Srrs stcb, net); 1333163953Srrs /* 1334163953Srrs * the send at the end of the inbound data processing will 1335163953Srrs * cause the cookie to be sent 1336163953Srrs */ 1337163953Srrs break; 1338163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1339163953Srrs /* incorrect state... discard */ 1340163953Srrs break; 1341163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1342163953Srrs /* incorrect state... discard */ 1343163953Srrs break; 1344163953Srrs case SCTP_STATE_OPEN: 1345163953Srrs /* incorrect state... discard */ 1346163953Srrs break; 1347163953Srrs case SCTP_STATE_EMPTY: 1348163953Srrs case SCTP_STATE_INUSE: 1349163953Srrs default: 1350163953Srrs /* incorrect state... discard */ 1351163953Srrs return (-1); 1352163953Srrs break; 1353163953Srrs } 1354169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1355163953Srrs return (0); 1356163953Srrs} 1357163953Srrs 1358185694Srrsstatic struct sctp_tcb * 1359185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1360185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1361185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1362185694Srrs struct sockaddr *init_src, int *notification, 1363185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1364185694Srrs uint32_t vrf_id, uint16_t port); 1365163953Srrs 1366185694Srrs 1367163953Srrs/* 1368163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1369163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1370163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1371163953Srrs * mbuf to the cookie-echo chunk 1372163953Srrs */ 1373163953Srrsstatic struct sctp_tcb * 1374163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1375163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1376185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1377169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1378185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1379163953Srrs{ 1380163953Srrs struct sctp_association *asoc; 1381163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1382163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1383185694Srrs struct sctp_nets *net; 1384185694Srrs struct mbuf *op_err; 1385185694Srrs struct sctp_paramhdr *ph; 1386163953Srrs int chk_length; 1387165220Srrs int init_offset, initack_offset, i; 1388163953Srrs int retval; 1389164205Srrs int spec_flag = 0; 1390170140Srrs uint32_t how_indx; 1391163953Srrs 1392185694Srrs net = *netp; 1393163953Srrs /* I know that the TCB is non-NULL from the caller */ 1394163953Srrs asoc = &stcb->asoc; 1395166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1396166023Srrs if (asoc->cookie_how[how_indx] == 0) 1397166023Srrs break; 1398166023Srrs } 1399166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1400166023Srrs asoc->cookie_how[how_indx] = 1; 1401166023Srrs } 1402163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1403163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1404163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1405163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1406165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1407163953Srrs if (op_err == NULL) { 1408163953Srrs /* FOOBAR */ 1409163953Srrs return (NULL); 1410163953Srrs } 1411163953Srrs /* Set the len */ 1412165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1413163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1414163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1415163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1416169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1417179157Srrs vrf_id, net->port); 1418166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1419166023Srrs asoc->cookie_how[how_indx] = 2; 1420163953Srrs return (NULL); 1421163953Srrs } 1422163953Srrs /* 1423163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1424163953Srrs * INIT should start after the cookie-echo header struct (chunk 1425163953Srrs * header, state cookie header struct) 1426163953Srrs */ 1427163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1428163953Srrs 1429163953Srrs init_cp = (struct sctp_init_chunk *) 1430163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1431163953Srrs (uint8_t *) & init_buf); 1432163953Srrs if (init_cp == NULL) { 1433163953Srrs /* could not pull a INIT chunk in cookie */ 1434163953Srrs return (NULL); 1435163953Srrs } 1436163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1437163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1438163953Srrs return (NULL); 1439163953Srrs } 1440163953Srrs /* 1441163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1442163953Srrs * INIT-ACK follows the INIT chunk 1443163953Srrs */ 1444163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1445163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1446163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1447163953Srrs (uint8_t *) & initack_buf); 1448163953Srrs if (initack_cp == NULL) { 1449163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1450163953Srrs return (NULL); 1451163953Srrs } 1452163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1453163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1454163953Srrs return (NULL); 1455163953Srrs } 1456163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1457163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1458163953Srrs /* 1459163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1460163953Srrs * to get into the OPEN state 1461163953Srrs */ 1462166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1463172091Srrs /*- 1464172091Srrs * Opps, this means that we somehow generated two vtag's 1465172091Srrs * the same. I.e. we did: 1466172091Srrs * Us Peer 1467172091Srrs * <---INIT(tag=a)------ 1468172091Srrs * ----INIT-ACK(tag=t)--> 1469172091Srrs * ----INIT(tag=t)------> *1 1470172091Srrs * <---INIT-ACK(tag=a)--- 1471172091Srrs * <----CE(tag=t)------------- *2 1472172091Srrs * 1473172091Srrs * At point *1 we should be generating a different 1474172091Srrs * tag t'. Which means we would throw away the CE and send 1475172091Srrs * ours instead. Basically this is case C (throw away side). 1476172091Srrs */ 1477172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1478172091Srrs asoc->cookie_how[how_indx] = 17; 1479172091Srrs return (NULL); 1480172091Srrs 1481166023Srrs } 1482163953Srrs switch SCTP_GET_STATE 1483163953Srrs (asoc) { 1484163953Srrs case SCTP_STATE_COOKIE_WAIT: 1485166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1486163953Srrs /* 1487169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1488166023Srrs * correct tags... just accept it...but we must 1489166023Srrs * process the init so that we can make sure we have 1490166023Srrs * the right seq no's. 1491163953Srrs */ 1492163953Srrs /* First we must process the INIT !! */ 1493163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1494163953Srrs if (retval < 0) { 1495166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1496166023Srrs asoc->cookie_how[how_indx] = 3; 1497163953Srrs return (NULL); 1498163953Srrs } 1499163953Srrs /* we have already processed the INIT so no problem */ 1500163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1501171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1502171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1503163953Srrs /* update current state */ 1504166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1505166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1506166675Srrs else 1507166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1508171943Srrs 1509171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1510163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1511163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1512163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1513163953Srrs } 1514166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1515165220Srrs sctp_stop_all_cookie_timers(stcb); 1516163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1517163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1518163953Srrs (inp->sctp_socket->so_qlimit == 0) 1519163953Srrs ) { 1520172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1521172090Srrs struct socket *so; 1522172090Srrs 1523172090Srrs#endif 1524163953Srrs /* 1525163953Srrs * Here is where collision would go if we 1526163953Srrs * did a connect() and instead got a 1527163953Srrs * init/init-ack/cookie done before the 1528163953Srrs * init-ack came back.. 1529163953Srrs */ 1530163953Srrs stcb->sctp_ep->sctp_flags |= 1531163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1532172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1533172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1534172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1535172090Srrs SCTP_TCB_UNLOCK(stcb); 1536172090Srrs SCTP_SOCKET_LOCK(so, 1); 1537172090Srrs SCTP_TCB_LOCK(stcb); 1538172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1539172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1540172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1541172090Srrs return (NULL); 1542172090Srrs } 1543172090Srrs#endif 1544172090Srrs soisconnected(stcb->sctp_socket); 1545172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1546172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1547172090Srrs#endif 1548163953Srrs } 1549163953Srrs /* notify upper layer */ 1550163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1551163953Srrs /* 1552163953Srrs * since we did not send a HB make sure we don't 1553163953Srrs * double things 1554163953Srrs */ 1555163953Srrs net->hb_responded = 1; 1556170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1557171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1558163953Srrs 1559163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1560163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1561163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1562163953Srrs inp, stcb, NULL); 1563163953Srrs } 1564163953Srrs break; 1565163953Srrs default: 1566163953Srrs /* 1567163953Srrs * we're in the OPEN state (or beyond), so peer must 1568163953Srrs * have simply lost the COOKIE-ACK 1569163953Srrs */ 1570163953Srrs break; 1571163953Srrs } /* end switch */ 1572165220Srrs sctp_stop_all_cookie_timers(stcb); 1573163953Srrs /* 1574163953Srrs * We ignore the return code here.. not sure if we should 1575163953Srrs * somehow abort.. but we do have an existing asoc. This 1576163953Srrs * really should not fail. 1577163953Srrs */ 1578163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1579163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1580163953Srrs initack_offset, sh, init_src)) { 1581166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1582166023Srrs asoc->cookie_how[how_indx] = 4; 1583163953Srrs return (NULL); 1584163953Srrs } 1585163953Srrs /* respond with a COOKIE-ACK */ 1586165220Srrs sctp_toss_old_cookies(stcb, asoc); 1587163953Srrs sctp_send_cookie_ack(stcb); 1588166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1589166023Srrs asoc->cookie_how[how_indx] = 5; 1590163953Srrs return (stcb); 1591169352Srrs } 1592163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1593163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1594163953Srrs cookie->tie_tag_my_vtag == 0 && 1595163953Srrs cookie->tie_tag_peer_vtag == 0) { 1596163953Srrs /* 1597163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1598163953Srrs */ 1599166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1600166023Srrs asoc->cookie_how[how_indx] = 6; 1601163953Srrs return (NULL); 1602163953Srrs } 1603185694Srrs /* 1604185694Srrs * If nat support, and the below and stcb is established, send back 1605185694Srrs * a ABORT(colliding state) if we are established. 1606185694Srrs */ 1607185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1608185694Srrs (asoc->peer_supports_nat) && 1609185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1610185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1611185694Srrs (asoc->peer_vtag == 0)))) { 1612163953Srrs /* 1613185694Srrs * Special case - Peer's support nat. We may have two init's 1614185694Srrs * that we gave out the same tag on since one was not 1615185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1616185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1617185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1618185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1619185694Srrs * Now we have colliding state. We must send an abort here 1620185694Srrs * with colliding state indication. 1621185694Srrs */ 1622185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1623185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1624185694Srrs if (op_err == NULL) { 1625185694Srrs /* FOOBAR */ 1626185694Srrs return (NULL); 1627185694Srrs } 1628185694Srrs /* pre-reserve some space */ 1629185694Srrs#ifdef INET6 1630185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1631185694Srrs#else 1632185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1633185694Srrs#endif 1634185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1635185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1636185694Srrs /* Set the len */ 1637185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1638185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1639185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1640185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1641185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1642185694Srrs return (NULL); 1643185694Srrs } 1644185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1645185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1646185694Srrs (asoc->peer_vtag == 0))) { 1647185694Srrs /* 1648163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1649163953Srrs * should be ok, re-accept peer info 1650163953Srrs */ 1651166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1652166023Srrs /* 1653166023Srrs * Extension of case C. If we hit this, then the 1654166023Srrs * random number generator returned the same vtag 1655166023Srrs * when we first sent our INIT-ACK and when we later 1656166023Srrs * sent our INIT. The side with the seq numbers that 1657166023Srrs * are different will be the one that normnally 1658166023Srrs * would have hit case C. This in effect "extends" 1659166023Srrs * our vtags in this collision case to be 64 bits. 1660166023Srrs * The same collision could occur aka you get both 1661166023Srrs * vtag and seq number the same twice in a row.. but 1662166023Srrs * is much less likely. If it did happen then we 1663166023Srrs * would proceed through and bring up the assoc.. we 1664166023Srrs * may end up with the wrong stream setup however.. 1665166023Srrs * which would be bad.. but there is no way to 1666166023Srrs * tell.. until we send on a stream that does not 1667166023Srrs * exist :-) 1668166023Srrs */ 1669166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1670166023Srrs asoc->cookie_how[how_indx] = 7; 1671166023Srrs 1672166023Srrs return (NULL); 1673166023Srrs } 1674166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1675166023Srrs asoc->cookie_how[how_indx] = 8; 1676171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1677163953Srrs sctp_stop_all_cookie_timers(stcb); 1678163953Srrs /* 1679163953Srrs * since we did not send a HB make sure we don't double 1680163953Srrs * things 1681163953Srrs */ 1682163953Srrs net->hb_responded = 1; 1683163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1684163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1685163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1686163953Srrs NULL); 1687163953Srrs } 1688163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1689164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1690164205Srrs 1691164205Srrs /* Note last_cwr_tsn? where is this used? */ 1692163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1693164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1694164205Srrs /* 1695164205Srrs * Ok the peer probably discarded our data (if we 1696164205Srrs * echoed a cookie+data). So anything on the 1697164205Srrs * sent_queue should be marked for retransmit, we 1698164205Srrs * may not get something to kick us so it COULD 1699164205Srrs * still take a timeout to move these.. but it can't 1700164205Srrs * hurt to mark them. 1701164205Srrs */ 1702164205Srrs struct sctp_tmit_chunk *chk; 1703163953Srrs 1704164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1705164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1706164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1707171440Srrs sctp_flight_size_decrease(chk); 1708171440Srrs sctp_total_flight_decrease(stcb, chk); 1709168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1710164205Srrs spec_flag++; 1711164205Srrs } 1712164205Srrs } 1713164205Srrs 1714164205Srrs } 1715163953Srrs /* process the INIT info (peer's info) */ 1716163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1717163953Srrs if (retval < 0) { 1718166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1719166023Srrs asoc->cookie_how[how_indx] = 9; 1720163953Srrs return (NULL); 1721163953Srrs } 1722163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1723163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1724163953Srrs initack_offset, sh, init_src)) { 1725166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1726166023Srrs asoc->cookie_how[how_indx] = 10; 1727163953Srrs return (NULL); 1728163953Srrs } 1729163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1730163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1731163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1732163953Srrs 1733163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1734163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1735163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1736172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1737172090Srrs struct socket *so; 1738172090Srrs 1739172090Srrs#endif 1740163953Srrs stcb->sctp_ep->sctp_flags |= 1741163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1742172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1743172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1744172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1745172090Srrs SCTP_TCB_UNLOCK(stcb); 1746172090Srrs SCTP_SOCKET_LOCK(so, 1); 1747172090Srrs SCTP_TCB_LOCK(stcb); 1748172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1749172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1750172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1751172090Srrs return (NULL); 1752172090Srrs } 1753172090Srrs#endif 1754172090Srrs soisconnected(stcb->sctp_socket); 1755172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1756172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1757172090Srrs#endif 1758163953Srrs } 1759166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1760166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1761166675Srrs else 1762166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1763166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1764166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1765166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1766166675Srrs } else { 1767166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1768163953Srrs } 1769171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1770163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1771163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1772163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1773163953Srrs } 1774163953Srrs sctp_stop_all_cookie_timers(stcb); 1775165220Srrs sctp_toss_old_cookies(stcb, asoc); 1776163953Srrs sctp_send_cookie_ack(stcb); 1777164205Srrs if (spec_flag) { 1778164205Srrs /* 1779164205Srrs * only if we have retrans set do we do this. What 1780164205Srrs * this call does is get only the COOKIE-ACK out and 1781164205Srrs * then when we return the normal call to 1782164205Srrs * sctp_chunk_output will get the retrans out behind 1783164205Srrs * this. 1784164205Srrs */ 1785172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1786164205Srrs } 1787166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1788166023Srrs asoc->cookie_how[how_indx] = 11; 1789166023Srrs 1790163953Srrs return (stcb); 1791163953Srrs } 1792163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1793163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1794163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1795163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1796163953Srrs cookie->tie_tag_peer_vtag != 0) { 1797163953Srrs struct sctpasochead *head; 1798163953Srrs 1799185694Srrs if (asoc->peer_supports_nat) { 1800185694Srrs /* 1801185694Srrs * This is a gross gross hack. just call the 1802185694Srrs * cookie_new code since we are allowing a duplicate 1803185694Srrs * association. I hope this works... 1804185694Srrs */ 1805185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1806185694Srrs inp, netp, init_src, notification, 1807185694Srrs auth_skipped, auth_offset, auth_len, 1808185694Srrs vrf_id, port)); 1809185694Srrs } 1810163953Srrs /* 1811163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1812163953Srrs */ 1813165220Srrs /* temp code */ 1814166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1815166023Srrs asoc->cookie_how[how_indx] = 12; 1816171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1817171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1818165647Srrs 1819163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1820163953Srrs /* notify upper layer */ 1821163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1822165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1823166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1824166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1825166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1826166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1827166675Srrs } 1828166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1829166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1830166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1831166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1832166675Srrs } 1833165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1834171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1835165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1836165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1837165647Srrs 1838165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1839165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1840171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1841165647Srrs } 1842165647Srrs asoc->pre_open_streams = 1843165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1844165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1845165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1846179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1847165647Srrs 1848165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1849165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1850165647Srrs 1851165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1852165647Srrs 1853165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1854170138Srrs if (asoc->mapping_array) { 1855165647Srrs memset(asoc->mapping_array, 0, 1856165647Srrs asoc->mapping_array_size); 1857170138Srrs } 1858205627Srrs if (asoc->nr_mapping_array) { 1859185694Srrs memset(asoc->nr_mapping_array, 0, 1860206137Stuexen asoc->mapping_array_size); 1861185694Srrs } 1862165220Srrs SCTP_TCB_UNLOCK(stcb); 1863165220Srrs SCTP_INP_INFO_WLOCK(); 1864165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1865165220Srrs SCTP_TCB_LOCK(stcb); 1866165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1867163953Srrs /* send up all the data */ 1868164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1869165220Srrs 1870172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1871165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1872165220Srrs stcb->asoc.strmout[i].stream_no = i; 1873165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1874165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1875165220Srrs } 1876163953Srrs /* process the INIT-ACK info (my info) */ 1877163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1878163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1879163953Srrs 1880163953Srrs /* pull from vtag hash */ 1881163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1882163953Srrs /* re-insert to new vtag position */ 1883179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1884179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1885163953Srrs /* 1886163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1887163953Srrs * system 1888163953Srrs */ 1889163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1890163953Srrs 1891163953Srrs /* process the INIT info (peer's info) */ 1892164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1893165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1894165220Srrs SCTP_INP_INFO_WUNLOCK(); 1895165220Srrs 1896163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1897163953Srrs if (retval < 0) { 1898166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1899166023Srrs asoc->cookie_how[how_indx] = 13; 1900166023Srrs 1901163953Srrs return (NULL); 1902163953Srrs } 1903163953Srrs /* 1904163953Srrs * since we did not send a HB make sure we don't double 1905163953Srrs * things 1906163953Srrs */ 1907163953Srrs net->hb_responded = 1; 1908163953Srrs 1909163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1910163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1911163953Srrs initack_offset, sh, init_src)) { 1912166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1913166023Srrs asoc->cookie_how[how_indx] = 14; 1914166023Srrs 1915163953Srrs return (NULL); 1916163953Srrs } 1917163953Srrs /* respond with a COOKIE-ACK */ 1918163953Srrs sctp_stop_all_cookie_timers(stcb); 1919165220Srrs sctp_toss_old_cookies(stcb, asoc); 1920163953Srrs sctp_send_cookie_ack(stcb); 1921166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1922166023Srrs asoc->cookie_how[how_indx] = 15; 1923163953Srrs 1924163953Srrs return (stcb); 1925163953Srrs } 1926166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1927166023Srrs asoc->cookie_how[how_indx] = 16; 1928163953Srrs /* all other cases... */ 1929163953Srrs return (NULL); 1930163953Srrs} 1931163953Srrs 1932166086Srrs 1933163953Srrs/* 1934163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1935163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1936163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1937163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1938163953Srrs * was from returns a new TCB 1939163953Srrs */ 1940185694Srrsstruct sctp_tcb * 1941163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1942163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1943163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1944163953Srrs struct sockaddr *init_src, int *notification, 1945169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1946179157Srrs uint32_t vrf_id, uint16_t port) 1947163953Srrs{ 1948163953Srrs struct sctp_tcb *stcb; 1949163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1950163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1951163953Srrs struct sockaddr_storage sa_store; 1952163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1953163953Srrs struct sockaddr_in *sin; 1954163953Srrs struct sockaddr_in6 *sin6; 1955163953Srrs struct sctp_association *asoc; 1956163953Srrs int chk_length; 1957163953Srrs int init_offset, initack_offset, initack_limit; 1958163953Srrs int retval; 1959163953Srrs int error = 0; 1960163953Srrs uint32_t old_tag; 1961166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1962163953Srrs 1963172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1964172090Srrs struct socket *so; 1965172090Srrs 1966172090Srrs so = SCTP_INP_SO(inp); 1967172090Srrs#endif 1968172090Srrs 1969163953Srrs /* 1970163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1971163953Srrs * INIT should start after the cookie-echo header struct (chunk 1972163953Srrs * header, state cookie header struct) 1973163953Srrs */ 1974163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1975163953Srrs init_cp = (struct sctp_init_chunk *) 1976163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1977163953Srrs (uint8_t *) & init_buf); 1978163953Srrs if (init_cp == NULL) { 1979163953Srrs /* could not pull a INIT chunk in cookie */ 1980169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1981169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1982163953Srrs return (NULL); 1983163953Srrs } 1984163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1985163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1986169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1987163953Srrs return (NULL); 1988163953Srrs } 1989163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1990163953Srrs /* 1991163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1992163953Srrs * INIT-ACK follows the INIT chunk 1993163953Srrs */ 1994163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1995163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1996163953Srrs (uint8_t *) & initack_buf); 1997163953Srrs if (initack_cp == NULL) { 1998163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1999169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2000163953Srrs return (NULL); 2001163953Srrs } 2002163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 2003163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2004163953Srrs return (NULL); 2005163953Srrs } 2006163953Srrs /* 2007163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2008163953Srrs * "initack_limit" value. This is because the chk_length field 2009163953Srrs * includes the length of the cookie, but the cookie is omitted when 2010163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2011163953Srrs */ 2012163953Srrs initack_limit = offset + cookie_len; 2013163953Srrs 2014163953Srrs /* 2015163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2016163953Srrs * and popluate 2017163953Srrs */ 2018171531Srrs 2019171531Srrs /* 2020171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2021171531Srrs * We do this since in effect we only use the p argument when the 2022171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2023171531Srrs * getting a cookie, we cannot be unbound. 2024171531Srrs */ 2025206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2026171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2027171531Srrs (struct thread *)NULL 2028171531Srrs ); 2029163953Srrs if (stcb == NULL) { 2030163953Srrs struct mbuf *op_err; 2031163953Srrs 2032163953Srrs /* memory problem? */ 2033169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2034169420Srrs "process_cookie_new: no room for another TCB!\n"); 2035163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2036170140Srrs 2037163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2038179157Srrs sh, op_err, vrf_id, port); 2039163953Srrs return (NULL); 2040163953Srrs } 2041163953Srrs /* get the correct sctp_nets */ 2042169420Srrs if (netp) 2043169420Srrs *netp = sctp_findnet(stcb, init_src); 2044169420Srrs 2045163953Srrs asoc = &stcb->asoc; 2046163953Srrs /* get scope variables out of cookie */ 2047163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2048163953Srrs asoc->site_scope = cookie->site_scope; 2049163953Srrs asoc->local_scope = cookie->local_scope; 2050163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2051163953Srrs 2052163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2053163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2054163953Srrs struct mbuf *op_err; 2055163953Srrs 2056163953Srrs /* 2057163953Srrs * Houston we have a problem. The EP changed while the 2058163953Srrs * cookie was in flight. Only recourse is to abort the 2059163953Srrs * association. 2060163953Srrs */ 2061170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2062163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2063163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2064179157Srrs sh, op_err, vrf_id, port); 2065172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2066172090Srrs SCTP_TCB_UNLOCK(stcb); 2067172090Srrs SCTP_SOCKET_LOCK(so, 1); 2068172090Srrs SCTP_TCB_LOCK(stcb); 2069172090Srrs#endif 2070171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2071171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2072172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2073172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2074172090Srrs#endif 2075172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2076163953Srrs return (NULL); 2077163953Srrs } 2078163953Srrs /* process the INIT-ACK info (my info) */ 2079163953Srrs old_tag = asoc->my_vtag; 2080185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2081163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2082163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2083163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2084163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2085179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2086163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 2087163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2088163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2089163953Srrs 2090163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2091163953Srrs 2092163953Srrs /* process the INIT info (peer's info) */ 2093169420Srrs if (netp) 2094169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 2095169420Srrs else 2096169420Srrs retval = 0; 2097163953Srrs if (retval < 0) { 2098170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2099172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2100172090Srrs SCTP_TCB_UNLOCK(stcb); 2101172090Srrs SCTP_SOCKET_LOCK(so, 1); 2102172090Srrs SCTP_TCB_LOCK(stcb); 2103172090Srrs#endif 2104171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2105172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2106172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2107172090Srrs#endif 2108172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2109163953Srrs return (NULL); 2110163953Srrs } 2111163953Srrs /* load all addresses */ 2112163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 2113163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2114163953Srrs init_src)) { 2115170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2116172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2117172090Srrs SCTP_TCB_UNLOCK(stcb); 2118172090Srrs SCTP_SOCKET_LOCK(so, 1); 2119172090Srrs SCTP_TCB_LOCK(stcb); 2120172090Srrs#endif 2121171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2122172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2123172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2124172090Srrs#endif 2125172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2126163953Srrs return (NULL); 2127163953Srrs } 2128163953Srrs /* 2129163953Srrs * verify any preceding AUTH chunk that was skipped 2130163953Srrs */ 2131163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2132163953Srrs sctp_auth_get_cookie_params(stcb, m, 2133163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2134163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2135163953Srrs if (auth_skipped) { 2136163953Srrs struct sctp_auth_chunk *auth; 2137163953Srrs 2138163953Srrs auth = (struct sctp_auth_chunk *) 2139166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2140169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2141163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2142169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2143169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2144171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2145172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2146172090Srrs SCTP_TCB_UNLOCK(stcb); 2147172090Srrs SCTP_SOCKET_LOCK(so, 1); 2148172090Srrs SCTP_TCB_LOCK(stcb); 2149172090Srrs#endif 2150171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2151172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2152172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2153172090Srrs#endif 2154172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2155163953Srrs return (NULL); 2156163953Srrs } else { 2157163953Srrs /* remaining chunks checked... good to go */ 2158163953Srrs stcb->asoc.authenticated = 1; 2159163953Srrs } 2160163953Srrs } 2161163953Srrs /* update current state */ 2162169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2163171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2164163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2165163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2166163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2167163953Srrs } 2168165220Srrs sctp_stop_all_cookie_timers(stcb); 2169163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2170163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2171163953Srrs 2172163953Srrs /* 2173163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2174163953Srrs * addresses that need to get added to the peer (eg. addresses 2175163953Srrs * changed while cookie echo in flight). This needs to be done 2176163953Srrs * after we go to the OPEN state to do the correct asconf 2177163953Srrs * processing. else, make sure we have the correct addresses in our 2178163953Srrs * lists 2179163953Srrs */ 2180163953Srrs 2181163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2182163953Srrs /* pull in local_address (our "from" address) */ 2183163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 2184163953Srrs /* source addr is IPv4 */ 2185163953Srrs sin = (struct sockaddr_in *)initack_src; 2186163953Srrs memset(sin, 0, sizeof(*sin)); 2187163953Srrs sin->sin_family = AF_INET; 2188163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2189163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2190163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 2191163953Srrs /* source addr is IPv6 */ 2192163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2193163953Srrs memset(sin6, 0, sizeof(*sin6)); 2194163953Srrs sin6->sin6_family = AF_INET6; 2195163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2196163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2197163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2198163953Srrs sizeof(sin6->sin6_addr)); 2199163953Srrs } else { 2200170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2201172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2202172090Srrs SCTP_TCB_UNLOCK(stcb); 2203172090Srrs SCTP_SOCKET_LOCK(so, 1); 2204172090Srrs SCTP_TCB_LOCK(stcb); 2205172090Srrs#endif 2206171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2207172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2208172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2209172090Srrs#endif 2210172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2211163953Srrs return (NULL); 2212163953Srrs } 2213163953Srrs 2214163953Srrs /* set up to notify upper layer */ 2215163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2216163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2217163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2218163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2219163953Srrs /* 2220163953Srrs * This is an endpoint that called connect() how it got a 2221163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2222163953Srrs * the INIT was sent, but before it got there.. a complete 2223163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2224163953Srrs * should have went to the other code.. not here.. oh well.. 2225163953Srrs * a bit of protection is worth having.. 2226163953Srrs */ 2227163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2228172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2229172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2230172090Srrs SCTP_TCB_UNLOCK(stcb); 2231172090Srrs SCTP_SOCKET_LOCK(so, 1); 2232172090Srrs SCTP_TCB_LOCK(stcb); 2233172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2234172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2235172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2236172090Srrs return (NULL); 2237172090Srrs } 2238172090Srrs#endif 2239172090Srrs soisconnected(stcb->sctp_socket); 2240172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2241172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2242172090Srrs#endif 2243163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2244163953Srrs (inp->sctp_socket->so_qlimit)) { 2245163953Srrs /* 2246163953Srrs * We don't want to do anything with this one. Since it is 2247163953Srrs * the listening guy. The timer will get started for 2248163953Srrs * accepted connections in the caller. 2249163953Srrs */ 2250163953Srrs ; 2251163953Srrs } 2252163953Srrs /* since we did not send a HB make sure we don't double things */ 2253169420Srrs if ((netp) && (*netp)) 2254169420Srrs (*netp)->hb_responded = 1; 2255163953Srrs 2256163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2257163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2258163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2259163953Srrs } 2260165220Srrs /* calculate the RTT */ 2261171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2262170642Srrs if ((netp) && (*netp)) { 2263169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2264171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2265170642Srrs } 2266172190Srrs /* respond with a COOKIE-ACK */ 2267163953Srrs sctp_send_cookie_ack(stcb); 2268172190Srrs 2269172190Srrs /* 2270172190Srrs * check the address lists for any ASCONFs that need to be sent 2271172190Srrs * AFTER the cookie-ack is sent 2272172190Srrs */ 2273172190Srrs sctp_check_address_list(stcb, m, 2274172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2275172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2276172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2277172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2278172190Srrs 2279172190Srrs 2280163953Srrs return (stcb); 2281163953Srrs} 2282163953Srrs 2283185694Srrs/* 2284185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2285185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2286185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2287185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2288185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2289185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2290185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2291185694Srrs -- SEND ABORT - TRY AGAIN -- 2292185694Srrs } 2293185694Srrs } 2294185694Srrs*/ 2295163953Srrs 2296163953Srrs/* 2297163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2298163953Srrs * existing (non-NULL) TCB 2299163953Srrs */ 2300163953Srrsstatic struct mbuf * 2301163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2302163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2303163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2304169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2305179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2306163953Srrs{ 2307163953Srrs struct sctp_state_cookie *cookie; 2308163953Srrs struct sockaddr_in6 sin6; 2309163953Srrs struct sockaddr_in sin; 2310163953Srrs struct sctp_tcb *l_stcb = *stcb; 2311163953Srrs struct sctp_inpcb *l_inp; 2312163953Srrs struct sockaddr *to; 2313163953Srrs sctp_assoc_t sac_restart_id; 2314163953Srrs struct sctp_pcb *ep; 2315163953Srrs struct mbuf *m_sig; 2316163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2317163953Srrs uint8_t *sig; 2318163953Srrs uint8_t cookie_ok = 0; 2319163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2320163953Srrs unsigned int cookie_len; 2321163953Srrs struct timeval now; 2322163953Srrs struct timeval time_expires; 2323163953Srrs struct sockaddr_storage dest_store; 2324163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2325163953Srrs struct ip *iph; 2326163953Srrs int notification = 0; 2327163953Srrs struct sctp_nets *netl; 2328163953Srrs int had_a_existing_tcb = 0; 2329163953Srrs 2330169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2331169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2332163953Srrs 2333163953Srrs if (inp_p == NULL) { 2334163953Srrs return (NULL); 2335163953Srrs } 2336163953Srrs /* First get the destination address setup too. */ 2337163953Srrs iph = mtod(m, struct ip *); 2338178251Srrs switch (iph->ip_v) { 2339178251Srrs case IPVERSION: 2340178251Srrs { 2341178251Srrs /* its IPv4 */ 2342178251Srrs struct sockaddr_in *lsin; 2343163953Srrs 2344178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2345178251Srrs memset(lsin, 0, sizeof(*lsin)); 2346178251Srrs lsin->sin_family = AF_INET; 2347178251Srrs lsin->sin_len = sizeof(*lsin); 2348178251Srrs lsin->sin_port = sh->dest_port; 2349178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2350178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2351178251Srrs break; 2352178251Srrs } 2353178251Srrs#ifdef INET6 2354178251Srrs case IPV6_VERSION >> 4: 2355178251Srrs { 2356178251Srrs /* its IPv6 */ 2357178251Srrs struct ip6_hdr *ip6; 2358178251Srrs struct sockaddr_in6 *lsin6; 2359163953Srrs 2360178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2361178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2362178251Srrs lsin6->sin6_family = AF_INET6; 2363178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2364178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2365178251Srrs lsin6->sin6_port = sh->dest_port; 2366178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2367178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2368178251Srrs break; 2369178251Srrs } 2370178251Srrs#endif 2371178251Srrs default: 2372163953Srrs return (NULL); 2373163953Srrs } 2374163953Srrs 2375163953Srrs cookie = &cp->cookie; 2376163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2377163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2378163953Srrs 2379163953Srrs if ((cookie->peerport != sh->src_port) && 2380163953Srrs (cookie->myport != sh->dest_port) && 2381163953Srrs (cookie->my_vtag != sh->v_tag)) { 2382163953Srrs /* 2383163953Srrs * invalid ports or bad tag. Note that we always leave the 2384163953Srrs * v_tag in the header in network order and when we stored 2385163953Srrs * it in the my_vtag slot we also left it in network order. 2386169352Srrs * This maintains the match even though it may be in the 2387163953Srrs * opposite byte order of the machine :-> 2388163953Srrs */ 2389163953Srrs return (NULL); 2390163953Srrs } 2391163953Srrs if (cookie_len > size_of_pkt || 2392163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2393163953Srrs sizeof(struct sctp_init_chunk) + 2394163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2395163953Srrs /* cookie too long! or too small */ 2396163953Srrs return (NULL); 2397163953Srrs } 2398163953Srrs /* 2399163953Srrs * split off the signature into its own mbuf (since it should not be 2400163953Srrs * calculated in the sctp_hmac_m() call). 2401163953Srrs */ 2402163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2403163953Srrs if (sig_offset > size_of_pkt) { 2404163953Srrs /* packet not correct size! */ 2405163953Srrs /* XXX this may already be accounted for earlier... */ 2406163953Srrs return (NULL); 2407163953Srrs } 2408163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2409163953Srrs if (m_sig == NULL) { 2410163953Srrs /* out of memory or ?? */ 2411163953Srrs return (NULL); 2412163953Srrs } 2413178202Srrs#ifdef SCTP_MBUF_LOGGING 2414179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2415178202Srrs struct mbuf *mat; 2416178202Srrs 2417178202Srrs mat = m_sig; 2418178202Srrs while (mat) { 2419178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2420178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2421178202Srrs } 2422178202Srrs mat = SCTP_BUF_NEXT(mat); 2423178202Srrs } 2424178202Srrs } 2425178202Srrs#endif 2426178202Srrs 2427163953Srrs /* 2428163953Srrs * compute the signature/digest for the cookie 2429163953Srrs */ 2430163953Srrs ep = &(*inp_p)->sctp_ep; 2431163953Srrs l_inp = *inp_p; 2432163953Srrs if (l_stcb) { 2433163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2434163953Srrs } 2435163953Srrs SCTP_INP_RLOCK(l_inp); 2436163953Srrs if (l_stcb) { 2437163953Srrs SCTP_TCB_LOCK(l_stcb); 2438163953Srrs } 2439163953Srrs /* which cookie is it? */ 2440163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2441163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2442163953Srrs /* it's the old cookie */ 2443169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2444163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2445170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2446163953Srrs } else { 2447163953Srrs /* it's the current cookie */ 2448169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2449163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2450170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2451163953Srrs } 2452163953Srrs /* get the signature */ 2453163953Srrs SCTP_INP_RUNLOCK(l_inp); 2454163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2455163953Srrs if (sig == NULL) { 2456163953Srrs /* couldn't find signature */ 2457164085Srrs sctp_m_freem(m_sig); 2458163953Srrs return (NULL); 2459163953Srrs } 2460163953Srrs /* compare the received digest with the computed digest */ 2461163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2462163953Srrs /* try the old cookie? */ 2463163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2464163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2465163953Srrs /* compute digest with old */ 2466169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2467163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2468170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2469163953Srrs /* compare */ 2470163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2471163953Srrs cookie_ok = 1; 2472163953Srrs } 2473163953Srrs } else { 2474163953Srrs cookie_ok = 1; 2475163953Srrs } 2476163953Srrs 2477163953Srrs /* 2478163953Srrs * Now before we continue we must reconstruct our mbuf so that 2479163953Srrs * normal processing of any other chunks will work. 2480163953Srrs */ 2481163953Srrs { 2482163953Srrs struct mbuf *m_at; 2483163953Srrs 2484163953Srrs m_at = m; 2485165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2486165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2487163953Srrs } 2488165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2489163953Srrs } 2490163953Srrs 2491163953Srrs if (cookie_ok == 0) { 2492169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2493169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2494169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2495169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2496163953Srrs return (NULL); 2497163953Srrs } 2498163953Srrs /* 2499163953Srrs * check the cookie timestamps to be sure it's not stale 2500163953Srrs */ 2501169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2502163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2503169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2504163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2505180387Srrs /* 2506180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2507180387Srrs * is undefined. 2508180387Srrs */ 2509163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2510163953Srrs /* cookie is stale! */ 2511163953Srrs struct mbuf *op_err; 2512163953Srrs struct sctp_stale_cookie_msg *scm; 2513163953Srrs uint32_t tim; 2514163953Srrs 2515163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2516165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2517163953Srrs if (op_err == NULL) { 2518163953Srrs /* FOOBAR */ 2519163953Srrs return (NULL); 2520163953Srrs } 2521163953Srrs /* Set the len */ 2522165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2523163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2524163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2525163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2526163953Srrs (sizeof(uint32_t)))); 2527163953Srrs /* seconds to usec */ 2528163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2529163953Srrs /* add in usec */ 2530163953Srrs if (tim == 0) 2531163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2532163953Srrs scm->time_usec = htonl(tim); 2533169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2534179157Srrs vrf_id, port); 2535163953Srrs return (NULL); 2536163953Srrs } 2537163953Srrs /* 2538163953Srrs * Now we must see with the lookup address if we have an existing 2539163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2540163953Srrs * and a INIT collided with us and somewhere the peer sent the 2541163953Srrs * cookie on another address besides the single address our assoc 2542163953Srrs * had for him. In this case we will have one of the tie-tags set at 2543163953Srrs * least AND the address field in the cookie can be used to look it 2544163953Srrs * up. 2545163953Srrs */ 2546163953Srrs to = NULL; 2547163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2548163953Srrs memset(&sin6, 0, sizeof(sin6)); 2549163953Srrs sin6.sin6_family = AF_INET6; 2550163953Srrs sin6.sin6_len = sizeof(sin6); 2551163953Srrs sin6.sin6_port = sh->src_port; 2552163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2553163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2554163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2555163953Srrs to = (struct sockaddr *)&sin6; 2556163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2557163953Srrs memset(&sin, 0, sizeof(sin)); 2558163953Srrs sin.sin_family = AF_INET; 2559163953Srrs sin.sin_len = sizeof(sin); 2560163953Srrs sin.sin_port = sh->src_port; 2561163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2562163953Srrs to = (struct sockaddr *)&sin; 2563169420Srrs } else { 2564169420Srrs /* This should not happen */ 2565169420Srrs return (NULL); 2566163953Srrs } 2567163953Srrs if ((*stcb == NULL) && to) { 2568163953Srrs /* Yep, lets check */ 2569163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2570163953Srrs if (*stcb == NULL) { 2571163953Srrs /* 2572163953Srrs * We should have only got back the same inp. If we 2573163953Srrs * got back a different ep we have a problem. The 2574163953Srrs * original findep got back l_inp and now 2575163953Srrs */ 2576163953Srrs if (l_inp != *inp_p) { 2577169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2578163953Srrs } 2579165220Srrs } else { 2580165220Srrs if (*locked_tcb == NULL) { 2581165220Srrs /* 2582165220Srrs * In this case we found the assoc only 2583165220Srrs * after we locked the create lock. This 2584165220Srrs * means we are in a colliding case and we 2585165220Srrs * must make sure that we unlock the tcb if 2586165220Srrs * its one of the cases where we throw away 2587165220Srrs * the incoming packets. 2588165220Srrs */ 2589165220Srrs *locked_tcb = *stcb; 2590165220Srrs 2591165220Srrs /* 2592165220Srrs * We must also increment the inp ref count 2593165220Srrs * since the ref_count flags was set when we 2594165220Srrs * did not find the TCB, now we found it 2595165220Srrs * which reduces the refcount.. we must 2596165220Srrs * raise it back out to balance it all :-) 2597165220Srrs */ 2598165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2599165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2600169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2601165220Srrs (*stcb)->sctp_ep, l_inp); 2602165220Srrs } 2603165220Srrs } 2604163953Srrs } 2605163953Srrs } 2606188067Srrs if (to == NULL) { 2607169420Srrs return (NULL); 2608188067Srrs } 2609163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2610163953Srrs if (*stcb == NULL) { 2611163953Srrs /* this is the "normal" case... get a new TCB */ 2612163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2613163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2614179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2615163953Srrs } else { 2616163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2617163953Srrs had_a_existing_tcb = 1; 2618163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2619185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2620185694Srrs ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port); 2621163953Srrs } 2622163953Srrs 2623163953Srrs if (*stcb == NULL) { 2624163953Srrs /* still no TCB... must be bad cookie-echo */ 2625163953Srrs return (NULL); 2626163953Srrs } 2627163953Srrs /* 2628163953Srrs * Ok, we built an association so confirm the address we sent the 2629163953Srrs * INIT-ACK to. 2630163953Srrs */ 2631163953Srrs netl = sctp_findnet(*stcb, to); 2632163953Srrs /* 2633163953Srrs * This code should in theory NOT run but 2634163953Srrs */ 2635163953Srrs if (netl == NULL) { 2636163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2637163953Srrs int ret; 2638163953Srrs 2639165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2640165220Srrs SCTP_IN_COOKIE_PROC); 2641163953Srrs netl = sctp_findnet(*stcb, to); 2642163953Srrs } 2643163953Srrs if (netl) { 2644163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2645163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2646169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2647163953Srrs netl); 2648163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2649172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2650163953Srrs } 2651163953Srrs } 2652163953Srrs if (*stcb) { 2653163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2654163953Srrs *stcb, NULL); 2655163953Srrs } 2656163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2657163953Srrs if (!had_a_existing_tcb || 2658163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2659163953Srrs /* 2660163953Srrs * If we have a NEW cookie or the connect never 2661163953Srrs * reached the connected state during collision we 2662163953Srrs * must do the TCP accept thing. 2663163953Srrs */ 2664163953Srrs struct socket *so, *oso; 2665163953Srrs struct sctp_inpcb *inp; 2666163953Srrs 2667163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2668163953Srrs /* 2669163953Srrs * For a restart we will keep the same 2670163953Srrs * socket, no need to do anything. I THINK!! 2671163953Srrs */ 2672172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2673163953Srrs return (m); 2674163953Srrs } 2675163953Srrs oso = (*inp_p)->sctp_socket; 2676171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2677163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2678166086Srrs so = sonewconn(oso, 0 2679163953Srrs ); 2680163953Srrs SCTP_TCB_LOCK((*stcb)); 2681171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2682171858Srrs 2683163953Srrs if (so == NULL) { 2684163953Srrs struct mbuf *op_err; 2685163953Srrs 2686172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2687172090Srrs struct socket *pcb_so; 2688172090Srrs 2689172090Srrs#endif 2690163953Srrs /* Too many sockets */ 2691169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2692163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2693163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2694179157Srrs sh, op_err, vrf_id, port); 2695172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2696172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2697172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2698172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2699172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2700172090Srrs SCTP_TCB_LOCK((*stcb)); 2701172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2702172090Srrs#endif 2703171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2704172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2705172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2706172090Srrs#endif 2707163953Srrs return (NULL); 2708163953Srrs } 2709163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2710166086Srrs SCTP_INP_INCR_REF(inp); 2711166086Srrs /* 2712166086Srrs * We add the unbound flag here so that if we get an 2713166086Srrs * soabort() before we get the move_pcb done, we 2714166086Srrs * will properly cleanup. 2715166086Srrs */ 2716163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2717163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2718163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2719166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2720163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2721163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2722163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2723172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2724163953Srrs inp->sctp_socket = so; 2725163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2726163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2727163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2728163953Srrs inp->inp_starting_point_for_iterator = NULL; 2729163953Srrs /* 2730163953Srrs * copy in the authentication parameters from the 2731163953Srrs * original endpoint 2732163953Srrs */ 2733163953Srrs if (inp->sctp_ep.local_hmacs) 2734163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2735163953Srrs inp->sctp_ep.local_hmacs = 2736163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2737163953Srrs if (inp->sctp_ep.local_auth_chunks) 2738163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2739163953Srrs inp->sctp_ep.local_auth_chunks = 2740163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2741163953Srrs 2742163953Srrs /* 2743163953Srrs * Now we must move it from one hash table to 2744163953Srrs * another and get the tcb in the right place. 2745163953Srrs */ 2746207924Srrs 2747207924Srrs /* 2748207924Srrs * This is where the one-2-one socket is put into 2749207924Srrs * the accept state waiting for the accept! 2750207924Srrs */ 2751207924Srrs if (*stcb) { 2752207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2753207924Srrs } 2754163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2755170056Srrs 2756170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2757170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2758170056Srrs 2759175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2760175845Srwatson 0); 2761170056Srrs SCTP_TCB_LOCK((*stcb)); 2762170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2763163953Srrs 2764170056Srrs 2765166086Srrs /* 2766166086Srrs * now we must check to see if we were aborted while 2767166086Srrs * the move was going on and the lock/unlock 2768166086Srrs * happened. 2769166086Srrs */ 2770166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2771166086Srrs /* 2772166086Srrs * yep it was, we leave the assoc attached 2773166086Srrs * to the socket since the sctp_inpcb_free() 2774166086Srrs * call will send an abort for us. 2775166086Srrs */ 2776166086Srrs SCTP_INP_DECR_REF(inp); 2777166086Srrs return (NULL); 2778166086Srrs } 2779166086Srrs SCTP_INP_DECR_REF(inp); 2780163953Srrs /* Switch over to the new guy */ 2781163953Srrs *inp_p = inp; 2782172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2783163953Srrs 2784166086Srrs /* 2785166086Srrs * Pull it from the incomplete queue and wake the 2786166086Srrs * guy 2787166086Srrs */ 2788172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2789172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2790172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2791172090Srrs SCTP_SOCKET_LOCK(so, 1); 2792172090Srrs#endif 2793166086Srrs soisconnected(so); 2794172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2795172118Srrs SCTP_TCB_LOCK((*stcb)); 2796172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2797172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2798172090Srrs#endif 2799163953Srrs return (m); 2800163953Srrs } 2801163953Srrs } 2802163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2803172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2804163953Srrs } 2805163953Srrs return (m); 2806163953Srrs} 2807163953Srrs 2808163953Srrsstatic void 2809163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2810163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2811163953Srrs{ 2812163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2813163953Srrs struct sctp_association *asoc; 2814163953Srrs 2815169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2816169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2817163953Srrs if (stcb == NULL) 2818163953Srrs return; 2819163953Srrs 2820163953Srrs asoc = &stcb->asoc; 2821163953Srrs 2822163953Srrs sctp_stop_all_cookie_timers(stcb); 2823163953Srrs /* process according to association state */ 2824163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2825163953Srrs /* state change only needed when I am in right state */ 2826169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2827171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2828163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2829163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2830163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2831163953Srrs 2832163953Srrs } 2833163953Srrs /* update RTO */ 2834163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2835163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2836163953Srrs if (asoc->overall_error_count == 0) { 2837163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2838171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2839163953Srrs } 2840169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2841172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2842163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2843163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2844172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2845172090Srrs struct socket *so; 2846172090Srrs 2847172090Srrs#endif 2848163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2849172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2850172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2851172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2852172090Srrs SCTP_TCB_UNLOCK(stcb); 2853172090Srrs SCTP_SOCKET_LOCK(so, 1); 2854172090Srrs SCTP_TCB_LOCK(stcb); 2855172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2856172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2857172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2858172090Srrs return; 2859172090Srrs } 2860172090Srrs#endif 2861172090Srrs soisconnected(stcb->sctp_socket); 2862172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2863172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2864172090Srrs#endif 2865163953Srrs } 2866163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2867163953Srrs stcb, net); 2868163953Srrs /* 2869163953Srrs * since we did not send a HB make sure we don't double 2870163953Srrs * things 2871163953Srrs */ 2872163953Srrs net->hb_responded = 1; 2873163953Srrs 2874163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2875163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2876163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2877163953Srrs stcb->sctp_ep, stcb, NULL); 2878163953Srrs } 2879163953Srrs /* 2880171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2881171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2882171572Srrs * in flight) 2883163953Srrs */ 2884163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2885163953Srrs (stcb->asoc.peer_supports_asconf) && 2886163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2887171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2888163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2889163953Srrs stcb->sctp_ep, stcb, 2890163953Srrs stcb->asoc.primary_destination); 2891171572Srrs#else 2892172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2893172190Srrs SCTP_ADDR_NOT_LOCKED); 2894171572Srrs#endif 2895163953Srrs } 2896163953Srrs } 2897163953Srrs /* Toss the cookie if I can */ 2898163953Srrs sctp_toss_old_cookies(stcb, asoc); 2899163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2900163953Srrs /* Restart the timer if we have pending data */ 2901163953Srrs struct sctp_tmit_chunk *chk; 2902163953Srrs 2903163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2904163953Srrs if (chk) { 2905163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2906163953Srrs stcb, chk->whoTo); 2907163953Srrs } 2908163953Srrs } 2909163953Srrs} 2910163953Srrs 2911163953Srrsstatic void 2912163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2913163953Srrs struct sctp_tcb *stcb) 2914163953Srrs{ 2915163953Srrs struct sctp_nets *net; 2916163953Srrs struct sctp_tmit_chunk *lchk; 2917163953Srrs uint32_t tsn; 2918163953Srrs 2919163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2920163953Srrs return; 2921163953Srrs } 2922163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2923163953Srrs tsn = ntohl(cp->tsn); 2924163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2925163953Srrs /* Also we make sure we disable the nonce_wait */ 2926163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2927163953Srrs if (lchk == NULL) { 2928163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2929163953Srrs } else { 2930163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2931163953Srrs } 2932163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2933163953Srrs stcb->asoc.nonce_sum_check = 0; 2934163953Srrs 2935163953Srrs /* Find where it was sent, if possible */ 2936163953Srrs net = NULL; 2937163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2938163953Srrs while (lchk) { 2939163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2940163953Srrs net = lchk->whoTo; 2941163953Srrs break; 2942163953Srrs } 2943163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2944163953Srrs break; 2945163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2946163953Srrs } 2947163953Srrs if (net == NULL) 2948163953Srrs /* default is we use the primary */ 2949163953Srrs net = stcb->asoc.primary_destination; 2950163953Srrs 2951163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2952163953Srrs /* 2953171440Srrs * JRS - Use the congestion control given in the pluggable 2954171440Srrs * CC module 2955171440Srrs */ 2956171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2957171440Srrs /* 2958163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2959163953Srrs * the next sending seq i.e. the resync_tsn. 2960163953Srrs */ 2961163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2962163953Srrs } 2963163953Srrs /* 2964163953Srrs * We always send a CWR this way if our previous one was lost our 2965163953Srrs * peer will get an update, or if it is not time again to reduce we 2966163953Srrs * still get the cwr to the peer. 2967163953Srrs */ 2968163953Srrs sctp_send_cwr(stcb, net, tsn); 2969163953Srrs} 2970163953Srrs 2971163953Srrsstatic void 2972163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2973163953Srrs{ 2974163953Srrs /* 2975163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2976163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2977163953Srrs * If so remove it. 2978163953Srrs */ 2979163953Srrs struct sctp_tmit_chunk *chk; 2980163953Srrs struct sctp_ecne_chunk *ecne; 2981163953Srrs 2982163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2983163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2984163953Srrs continue; 2985163953Srrs } 2986163953Srrs /* 2987163953Srrs * Look for and remove if it is the right TSN. Since there 2988163953Srrs * is only ONE ECNE on the control queue at any one time we 2989163953Srrs * don't need to worry about more than one! 2990163953Srrs */ 2991163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2992163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2993163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2994163953Srrs /* this covers this ECNE, we can remove it */ 2995163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2996163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2997163953Srrs sctp_next); 2998163953Srrs if (chk->data) { 2999163953Srrs sctp_m_freem(chk->data); 3000163953Srrs chk->data = NULL; 3001163953Srrs } 3002163953Srrs stcb->asoc.ctrl_queue_cnt--; 3003163953Srrs sctp_free_a_chunk(stcb, chk); 3004163953Srrs break; 3005163953Srrs } 3006163953Srrs } 3007163953Srrs} 3008163953Srrs 3009163953Srrsstatic void 3010163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3011163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3012163953Srrs{ 3013163953Srrs struct sctp_association *asoc; 3014163953Srrs 3015172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3016172090Srrs struct socket *so; 3017172090Srrs 3018172090Srrs#endif 3019172090Srrs 3020169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3021169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3022163953Srrs if (stcb == NULL) 3023163953Srrs return; 3024163953Srrs 3025163953Srrs asoc = &stcb->asoc; 3026163953Srrs /* process according to association state */ 3027163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3028163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3029171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3030171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3031163953Srrs SCTP_TCB_UNLOCK(stcb); 3032163953Srrs return; 3033163953Srrs } 3034163953Srrs /* notify upper layer protocol */ 3035163953Srrs if (stcb->sctp_socket) { 3036172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3037163953Srrs /* are the queues empty? they should be */ 3038163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3039163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3040163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 3041172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3042163953Srrs } 3043163953Srrs } 3044163953Srrs /* stop the timer */ 3045178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3046163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3047163953Srrs /* free the TCB */ 3048171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3049171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3050172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3051172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3052172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3053172090Srrs SCTP_TCB_UNLOCK(stcb); 3054172090Srrs SCTP_SOCKET_LOCK(so, 1); 3055172090Srrs SCTP_TCB_LOCK(stcb); 3056172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3057172090Srrs#endif 3058171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3059172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3060172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3061172090Srrs#endif 3062163953Srrs return; 3063163953Srrs} 3064163953Srrs 3065163953Srrsstatic int 3066163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3067163953Srrs struct sctp_nets *net, uint8_t flg) 3068163953Srrs{ 3069163953Srrs switch (desc->chunk_type) { 3070208853Srrs case SCTP_DATA: 3071163953Srrs /* find the tsn to resend (possibly */ 3072163953Srrs { 3073163953Srrs uint32_t tsn; 3074163953Srrs struct sctp_tmit_chunk *tp1; 3075163953Srrs 3076163953Srrs tsn = ntohl(desc->tsn_ifany); 3077163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3078163953Srrs while (tp1) { 3079163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3080163953Srrs /* found it */ 3081163953Srrs break; 3082163953Srrs } 3083163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 3084163953Srrs MAX_TSN)) { 3085163953Srrs /* not found */ 3086163953Srrs tp1 = NULL; 3087163953Srrs break; 3088163953Srrs } 3089163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3090163953Srrs } 3091163953Srrs if (tp1 == NULL) { 3092163953Srrs /* 3093163953Srrs * Do it the other way , aka without paying 3094163953Srrs * attention to queue seq order. 3095163953Srrs */ 3096163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3097163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3098163953Srrs while (tp1) { 3099163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3100163953Srrs /* found it */ 3101163953Srrs break; 3102163953Srrs } 3103163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3104163953Srrs } 3105163953Srrs } 3106163953Srrs if (tp1 == NULL) { 3107163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3108163953Srrs } 3109163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3110163953Srrs uint8_t *ddp; 3111163953Srrs 3112163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3113163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3114163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3115163953Srrs return (0); 3116163953Srrs } 3117163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3118163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3119163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3120163953Srrs return (0); 3121163953Srrs } 3122163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3123163953Srrs sizeof(struct sctp_data_chunk)); 3124163953Srrs { 3125163953Srrs unsigned int iii; 3126163953Srrs 3127163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3128163953Srrs iii++) { 3129163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3130163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3131163953Srrs return (-1); 3132163953Srrs } 3133163953Srrs } 3134163953Srrs } 3135163953Srrs /* 3136163953Srrs * We zero out the nonce so resync not 3137163953Srrs * needed 3138163953Srrs */ 3139163953Srrs tp1->rec.data.ect_nonce = 0; 3140163953Srrs 3141163953Srrs if (tp1->do_rtt) { 3142163953Srrs /* 3143163953Srrs * this guy had a RTO calculation 3144163953Srrs * pending on it, cancel it 3145163953Srrs */ 3146163953Srrs tp1->do_rtt = 0; 3147163953Srrs } 3148163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3149163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3150163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3151163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 3152163953Srrs /* 3153163953Srrs * mark it as if we were doing a FR, since 3154163953Srrs * we will be getting gap ack reports behind 3155163953Srrs * the info from the router. 3156163953Srrs */ 3157163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3158163953Srrs /* 3159163953Srrs * mark the tsn with what sequences can 3160163953Srrs * cause a new FR. 3161163953Srrs */ 3162163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3163163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3164163953Srrs } else { 3165163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3166163953Srrs } 3167163953Srrs 3168163953Srrs /* restart the timer */ 3169163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3170171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3171163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3172163953Srrs stcb, tp1->whoTo); 3173163953Srrs 3174163953Srrs /* fix counts and things */ 3175179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3176170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3177170744Srrs tp1->whoTo->flight_size, 3178170744Srrs tp1->book_size, 3179170744Srrs (uintptr_t) stcb, 3180170744Srrs tp1->rec.data.TSN_seq); 3181170744Srrs } 3182190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3183190689Srrs sctp_flight_size_decrease(tp1); 3184190689Srrs sctp_total_flight_decrease(stcb, tp1); 3185190689Srrs } 3186163953Srrs } { 3187163953Srrs /* audit code */ 3188163953Srrs unsigned int audit; 3189163953Srrs 3190163953Srrs audit = 0; 3191163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3192163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3193163953Srrs audit++; 3194163953Srrs } 3195163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3196163953Srrs sctp_next) { 3197163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3198163953Srrs audit++; 3199163953Srrs } 3200163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3201169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3202163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3203163953Srrs#ifndef SCTP_AUDITING_ENABLED 3204163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3205163953Srrs#endif 3206163953Srrs } 3207163953Srrs } 3208163953Srrs } 3209163953Srrs break; 3210163953Srrs case SCTP_ASCONF: 3211163953Srrs { 3212163953Srrs struct sctp_tmit_chunk *asconf; 3213163953Srrs 3214163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3215163953Srrs sctp_next) { 3216163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3217163953Srrs break; 3218163953Srrs } 3219163953Srrs } 3220163953Srrs if (asconf) { 3221163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3222163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3223163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3224163953Srrs asconf->snd_count--; 3225163953Srrs } 3226163953Srrs } 3227163953Srrs break; 3228163953Srrs case SCTP_INITIATION: 3229163953Srrs /* resend the INIT */ 3230163953Srrs stcb->asoc.dropped_special_cnt++; 3231163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3232163953Srrs /* 3233163953Srrs * If we can get it in, in a few attempts we do 3234163953Srrs * this, otherwise we let the timer fire. 3235163953Srrs */ 3236163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3237171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3238172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3239163953Srrs } 3240163953Srrs break; 3241163953Srrs case SCTP_SELECTIVE_ACK: 3242206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3243163953Srrs /* resend the sack */ 3244163953Srrs sctp_send_sack(stcb); 3245163953Srrs break; 3246163953Srrs case SCTP_HEARTBEAT_REQUEST: 3247163953Srrs /* resend a demand HB */ 3248171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3249171440Srrs /* 3250171440Srrs * Only retransmit if we KNOW we wont destroy the 3251171440Srrs * tcb 3252171440Srrs */ 3253171440Srrs (void)sctp_send_hb(stcb, 1, net); 3254171440Srrs } 3255163953Srrs break; 3256163953Srrs case SCTP_SHUTDOWN: 3257163953Srrs sctp_send_shutdown(stcb, net); 3258163953Srrs break; 3259163953Srrs case SCTP_SHUTDOWN_ACK: 3260163953Srrs sctp_send_shutdown_ack(stcb, net); 3261163953Srrs break; 3262163953Srrs case SCTP_COOKIE_ECHO: 3263163953Srrs { 3264163953Srrs struct sctp_tmit_chunk *cookie; 3265163953Srrs 3266163953Srrs cookie = NULL; 3267163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3268163953Srrs sctp_next) { 3269163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3270163953Srrs break; 3271163953Srrs } 3272163953Srrs } 3273163953Srrs if (cookie) { 3274163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3275163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3276163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3277163953Srrs sctp_stop_all_cookie_timers(stcb); 3278163953Srrs } 3279163953Srrs } 3280163953Srrs break; 3281163953Srrs case SCTP_COOKIE_ACK: 3282163953Srrs sctp_send_cookie_ack(stcb); 3283163953Srrs break; 3284163953Srrs case SCTP_ASCONF_ACK: 3285163953Srrs /* resend last asconf ack */ 3286171990Srrs sctp_send_asconf_ack(stcb); 3287163953Srrs break; 3288163953Srrs case SCTP_FORWARD_CUM_TSN: 3289163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3290163953Srrs break; 3291163953Srrs /* can't do anything with these */ 3292163953Srrs case SCTP_PACKET_DROPPED: 3293163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3294163953Srrs case SCTP_HEARTBEAT_ACK: 3295163953Srrs case SCTP_ABORT_ASSOCIATION: 3296163953Srrs case SCTP_OPERATION_ERROR: 3297163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3298163953Srrs case SCTP_ECN_ECHO: 3299163953Srrs case SCTP_ECN_CWR: 3300163953Srrs default: 3301163953Srrs break; 3302163953Srrs } 3303163953Srrs return (0); 3304163953Srrs} 3305163953Srrs 3306163953Srrsvoid 3307163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3308163953Srrs{ 3309163953Srrs int i; 3310163953Srrs uint16_t temp; 3311163953Srrs 3312163953Srrs /* 3313163953Srrs * We set things to 0xffff since this is the last delivered sequence 3314163953Srrs * and we will be sending in 0 after the reset. 3315163953Srrs */ 3316163953Srrs 3317163953Srrs if (number_entries) { 3318163953Srrs for (i = 0; i < number_entries; i++) { 3319163953Srrs temp = ntohs(list[i]); 3320163953Srrs if (temp >= stcb->asoc.streamincnt) { 3321163953Srrs continue; 3322163953Srrs } 3323163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3324163953Srrs } 3325163953Srrs } else { 3326163953Srrs list = NULL; 3327163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3328163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3329163953Srrs } 3330163953Srrs } 3331172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3332163953Srrs} 3333163953Srrs 3334163953Srrsstatic void 3335163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3336163953Srrs{ 3337163953Srrs int i; 3338163953Srrs 3339163953Srrs if (number_entries == 0) { 3340163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3341163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3342163953Srrs } 3343163953Srrs } else if (number_entries) { 3344163953Srrs for (i = 0; i < number_entries; i++) { 3345163953Srrs uint16_t temp; 3346163953Srrs 3347163953Srrs temp = ntohs(list[i]); 3348163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3349163953Srrs /* no such stream */ 3350163953Srrs continue; 3351163953Srrs } 3352163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3353163953Srrs } 3354163953Srrs } 3355172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3356163953Srrs} 3357163953Srrs 3358163953Srrs 3359163953Srrsstruct sctp_stream_reset_out_request * 3360163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3361163953Srrs{ 3362163953Srrs struct sctp_association *asoc; 3363163953Srrs struct sctp_stream_reset_out_req *req; 3364163953Srrs struct sctp_stream_reset_out_request *r; 3365163953Srrs struct sctp_tmit_chunk *chk; 3366163953Srrs int len, clen; 3367163953Srrs 3368163953Srrs asoc = &stcb->asoc; 3369163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3370169208Srrs asoc->stream_reset_outstanding = 0; 3371163953Srrs return (NULL); 3372163953Srrs } 3373163953Srrs if (stcb->asoc.str_reset == NULL) { 3374169208Srrs asoc->stream_reset_outstanding = 0; 3375163953Srrs return (NULL); 3376163953Srrs } 3377163953Srrs chk = stcb->asoc.str_reset; 3378163953Srrs if (chk->data == NULL) { 3379163953Srrs return (NULL); 3380163953Srrs } 3381163953Srrs if (bchk) { 3382163953Srrs /* he wants a copy of the chk pointer */ 3383163953Srrs *bchk = chk; 3384163953Srrs } 3385163953Srrs clen = chk->send_size; 3386163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3387163953Srrs r = &req->sr_req; 3388163953Srrs if (ntohl(r->request_seq) == seq) { 3389163953Srrs /* found it */ 3390163953Srrs return (r); 3391163953Srrs } 3392163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3393163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3394163953Srrs /* move to the next one, there can only be a max of two */ 3395163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3396163953Srrs if (ntohl(r->request_seq) == seq) { 3397163953Srrs return (r); 3398163953Srrs } 3399163953Srrs } 3400163953Srrs /* that seq is not here */ 3401163953Srrs return (NULL); 3402163953Srrs} 3403163953Srrs 3404163953Srrsstatic void 3405163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3406163953Srrs{ 3407163953Srrs struct sctp_association *asoc; 3408163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3409163953Srrs 3410163953Srrs if (stcb->asoc.str_reset == NULL) { 3411163953Srrs return; 3412163953Srrs } 3413166675Srrs asoc = &stcb->asoc; 3414166675Srrs 3415171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3416163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3417163953Srrs chk, 3418163953Srrs sctp_next); 3419163953Srrs if (chk->data) { 3420163953Srrs sctp_m_freem(chk->data); 3421163953Srrs chk->data = NULL; 3422163953Srrs } 3423163953Srrs asoc->ctrl_queue_cnt--; 3424163953Srrs sctp_free_a_chunk(stcb, chk); 3425172156Srrs /* sa_ignore NO_NULL_CHK */ 3426163953Srrs stcb->asoc.str_reset = NULL; 3427163953Srrs} 3428163953Srrs 3429163953Srrs 3430163953Srrsstatic int 3431163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3432163953Srrs uint32_t seq, uint32_t action, 3433164139Srrs struct sctp_stream_reset_response *respin) 3434163953Srrs{ 3435163953Srrs uint16_t type; 3436163953Srrs int lparm_len; 3437163953Srrs struct sctp_association *asoc = &stcb->asoc; 3438163953Srrs struct sctp_tmit_chunk *chk; 3439163953Srrs struct sctp_stream_reset_out_request *srparam; 3440163953Srrs int number_entries; 3441163953Srrs 3442163953Srrs if (asoc->stream_reset_outstanding == 0) { 3443163953Srrs /* duplicate */ 3444163953Srrs return (0); 3445163953Srrs } 3446163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3447163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3448163953Srrs if (srparam) { 3449163953Srrs stcb->asoc.str_reset_seq_out++; 3450163953Srrs type = ntohs(srparam->ph.param_type); 3451163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3452163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3453164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3454163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3455163953Srrs if (asoc->stream_reset_outstanding) 3456163953Srrs asoc->stream_reset_outstanding--; 3457163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3458163953Srrs /* do it */ 3459163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3460163953Srrs } else { 3461172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3462163953Srrs } 3463163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3464163953Srrs /* Answered my request */ 3465164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3466163953Srrs if (asoc->stream_reset_outstanding) 3467163953Srrs asoc->stream_reset_outstanding--; 3468163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3469172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3470163953Srrs } 3471188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3472188854Srrs /* Ok we now may have more streams */ 3473189121Srrs if (asoc->stream_reset_outstanding) 3474189121Srrs asoc->stream_reset_outstanding--; 3475188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3476188854Srrs /* Put the new streams into effect */ 3477188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3478188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3479188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3480188854Srrs } else { 3481188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3482188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3483188854Srrs } 3484163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3485163953Srrs /** 3486163953Srrs * a) Adopt the new in tsn. 3487163953Srrs * b) reset the map 3488163953Srrs * c) Adopt the new out-tsn 3489163953Srrs */ 3490163953Srrs struct sctp_stream_reset_response_tsn *resp; 3491163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3492163953Srrs int abort_flag = 0; 3493163953Srrs 3494163953Srrs if (respin == NULL) { 3495163953Srrs /* huh ? */ 3496163953Srrs return (0); 3497163953Srrs } 3498163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3499163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3500163953Srrs asoc->stream_reset_outstanding--; 3501163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3502163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3503163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3504170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3505163953Srrs if (abort_flag) { 3506163953Srrs return (1); 3507163953Srrs } 3508163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3509179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3510179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3511179783Srrs } 3512180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3513163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3514163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3515185694Srrs 3516205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3517206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3518205627Srrs 3519163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3520163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3521163953Srrs 3522163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3523163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3524163953Srrs 3525163953Srrs } 3526163953Srrs } 3527163953Srrs /* get rid of the request and get the request flags */ 3528163953Srrs if (asoc->stream_reset_outstanding == 0) { 3529163953Srrs sctp_clean_up_stream_reset(stcb); 3530163953Srrs } 3531163953Srrs } 3532163953Srrs } 3533163953Srrs return (0); 3534163953Srrs} 3535163953Srrs 3536163953Srrsstatic void 3537163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3538163953Srrs struct sctp_tmit_chunk *chk, 3539170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3540163953Srrs{ 3541163953Srrs uint32_t seq; 3542163953Srrs int len, i; 3543163953Srrs int number_entries; 3544163953Srrs uint16_t temp; 3545163953Srrs 3546163953Srrs /* 3547163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3548163953Srrs * seq_in is right. 3549163953Srrs */ 3550163953Srrs struct sctp_association *asoc = &stcb->asoc; 3551163953Srrs 3552163953Srrs seq = ntohl(req->request_seq); 3553163953Srrs if (asoc->str_reset_seq_in == seq) { 3554170992Srrs if (trunc) { 3555170992Srrs /* Can't do it, since they exceeded our buffer size */ 3556170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3557170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3558170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3559170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3560163953Srrs len = ntohs(req->ph.param_length); 3561163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3562163953Srrs for (i = 0; i < number_entries; i++) { 3563163953Srrs temp = ntohs(req->list_of_streams[i]); 3564163953Srrs req->list_of_streams[i] = temp; 3565163953Srrs } 3566163953Srrs /* move the reset action back one */ 3567163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3568163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3569163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3570163953Srrs asoc->str_reset_seq_out, 3571163953Srrs seq, (asoc->sending_seq - 1)); 3572163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3573163953Srrs asoc->str_reset = chk; 3574163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3575163953Srrs stcb->asoc.stream_reset_outstanding++; 3576163953Srrs } else { 3577163953Srrs /* Can't do it, since we have sent one out */ 3578163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3579163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3580163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3581163953Srrs } 3582163953Srrs asoc->str_reset_seq_in++; 3583163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3584163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3585163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3586163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3587163953Srrs } else { 3588163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3589163953Srrs } 3590163953Srrs} 3591163953Srrs 3592163953Srrsstatic int 3593163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3594163953Srrs struct sctp_tmit_chunk *chk, 3595163953Srrs struct sctp_stream_reset_tsn_request *req) 3596163953Srrs{ 3597163953Srrs /* reset all in and out and update the tsn */ 3598163953Srrs /* 3599163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3600163953Srrs * and set cum-ack to it. Also process this selected number as a 3601163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3602163953Srrs */ 3603163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3604163953Srrs struct sctp_association *asoc = &stcb->asoc; 3605163953Srrs int abort_flag = 0; 3606163953Srrs uint32_t seq; 3607163953Srrs 3608163953Srrs seq = ntohl(req->request_seq); 3609163953Srrs if (asoc->str_reset_seq_in == seq) { 3610163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3611163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3612163953Srrs fwdtsn.ch.chunk_flags = 0; 3613163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3614170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3615163953Srrs if (abort_flag) { 3616163953Srrs return (1); 3617163953Srrs } 3618163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3619179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3620179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3621179783Srrs } 3622180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3623163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3624163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3625205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3626206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3627163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3628163953Srrs /* save off historical data for retrans */ 3629163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3630163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3631163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3632163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3633163953Srrs 3634163953Srrs sctp_add_stream_reset_result_tsn(chk, 3635163953Srrs ntohl(req->request_seq), 3636163953Srrs SCTP_STREAM_RESET_PERFORMED, 3637163953Srrs stcb->asoc.sending_seq, 3638163953Srrs stcb->asoc.mapping_array_base_tsn); 3639163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3640163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3641163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3642163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3643163953Srrs 3644163953Srrs asoc->str_reset_seq_in++; 3645163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3646163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3647163953Srrs stcb->asoc.last_sending_seq[0], 3648163953Srrs stcb->asoc.last_base_tsnsent[0] 3649163953Srrs ); 3650163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3651163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3652163953Srrs stcb->asoc.last_sending_seq[1], 3653163953Srrs stcb->asoc.last_base_tsnsent[1] 3654163953Srrs ); 3655163953Srrs } else { 3656163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3657163953Srrs } 3658163953Srrs return (0); 3659163953Srrs} 3660163953Srrs 3661163953Srrsstatic void 3662163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3663163953Srrs struct sctp_tmit_chunk *chk, 3664170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3665163953Srrs{ 3666163953Srrs uint32_t seq, tsn; 3667163953Srrs int number_entries, len; 3668163953Srrs struct sctp_association *asoc = &stcb->asoc; 3669163953Srrs 3670163953Srrs seq = ntohl(req->request_seq); 3671163953Srrs 3672163953Srrs /* now if its not a duplicate we process it */ 3673163953Srrs if (asoc->str_reset_seq_in == seq) { 3674163953Srrs len = ntohs(req->ph.param_length); 3675163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3676163953Srrs /* 3677163953Srrs * the sender is resetting, handle the list issue.. we must 3678163953Srrs * a) verify if we can do the reset, if so no problem b) If 3679163953Srrs * we can't do the reset we must copy the request. c) queue 3680163953Srrs * it, and setup the data in processor to trigger it off 3681163953Srrs * when needed and dequeue all the queued data. 3682163953Srrs */ 3683163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3684163953Srrs 3685163953Srrs /* move the reset action back one */ 3686163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3687170992Srrs if (trunc) { 3688170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3689170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3690170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3691163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3692163953Srrs /* we can do it now */ 3693163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3694163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3695163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3696163953Srrs } else { 3697163953Srrs /* 3698163953Srrs * we must queue it up and thus wait for the TSN's 3699163953Srrs * to arrive that are at or before tsn 3700163953Srrs */ 3701163953Srrs struct sctp_stream_reset_list *liste; 3702163953Srrs int siz; 3703163953Srrs 3704163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3705163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3706170091Srrs siz, SCTP_M_STRESET); 3707163953Srrs if (liste == NULL) { 3708163953Srrs /* gak out of memory */ 3709163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3710163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3711163953Srrs return; 3712163953Srrs } 3713163953Srrs liste->tsn = tsn; 3714163953Srrs liste->number_entries = number_entries; 3715163953Srrs memcpy(&liste->req, req, 3716163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3717163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3718163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3719163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3720163953Srrs } 3721163953Srrs asoc->str_reset_seq_in++; 3722163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3723163953Srrs /* 3724163953Srrs * one seq back, just echo back last action since my 3725163953Srrs * response was lost. 3726163953Srrs */ 3727163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3728163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3729163953Srrs /* 3730163953Srrs * two seq back, just echo back last action since my 3731163953Srrs * response was lost. 3732163953Srrs */ 3733163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3734163953Srrs } else { 3735163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3736163953Srrs } 3737163953Srrs} 3738163953Srrs 3739188854Srrsstatic void 3740188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3741188854Srrs struct sctp_stream_reset_add_strm *str_add) 3742188854Srrs{ 3743188854Srrs /* 3744188854Srrs * Peer is requesting to add more streams. If its within our 3745188854Srrs * max-streams we will allow it. 3746188854Srrs */ 3747188854Srrs uint16_t num_stream, i; 3748188854Srrs uint32_t seq; 3749189121Srrs struct sctp_association *asoc = &stcb->asoc; 3750189121Srrs struct sctp_queued_to_read *ctl; 3751188854Srrs 3752188854Srrs /* Get the number. */ 3753188854Srrs seq = ntohl(str_add->request_seq); 3754188854Srrs num_stream = ntohs(str_add->number_of_streams); 3755188854Srrs /* Now what would be the new total? */ 3756189121Srrs if (asoc->str_reset_seq_in == seq) { 3757189121Srrs num_stream += stcb->asoc.streamincnt; 3758189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3759189121Srrs /* We must reject it they ask for to many */ 3760189121Srrs denied: 3761189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3762189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3763189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3764189121Srrs } else { 3765189121Srrs /* Ok, we can do that :-) */ 3766189121Srrs struct sctp_stream_in *oldstrm; 3767189121Srrs 3768189121Srrs /* save off the old */ 3769189121Srrs oldstrm = stcb->asoc.strmin; 3770189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3771189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3772189121Srrs SCTP_M_STRMI); 3773189121Srrs if (stcb->asoc.strmin == NULL) { 3774189121Srrs stcb->asoc.strmin = oldstrm; 3775189121Srrs goto denied; 3776189121Srrs } 3777189121Srrs /* copy off the old data */ 3778189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3779189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3780189121Srrs stcb->asoc.strmin[i].stream_no = i; 3781189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3782189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3783189121Srrs /* now anything on those queues? */ 3784189121Srrs while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) { 3785189121Srrs ctl = TAILQ_FIRST(&oldstrm[i].inqueue); 3786189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3787189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3788189121Srrs } 3789189121Srrs } 3790189121Srrs /* Init the new streams */ 3791189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3792189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3793189121Srrs stcb->asoc.strmin[i].stream_no = i; 3794189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3795189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3796189121Srrs } 3797189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3798189121Srrs /* update the size */ 3799189121Srrs stcb->asoc.streamincnt = num_stream; 3800189121Srrs /* Send the ack */ 3801189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3802189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3803189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3804189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3805189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3806189121Srrs } 3807189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3808189121Srrs /* 3809189121Srrs * one seq back, just echo back last action since my 3810189121Srrs * response was lost. 3811189121Srrs */ 3812189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3813189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3814189121Srrs /* 3815189121Srrs * two seq back, just echo back last action since my 3816189121Srrs * response was lost. 3817189121Srrs */ 3818189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3819188854Srrs } else { 3820189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3821188854Srrs 3822188854Srrs } 3823188854Srrs} 3824188854Srrs 3825170992Srrs#ifdef __GNUC__ 3826170992Srrs__attribute__((noinline)) 3827170992Srrs#endif 3828170992Srrs static int 3829170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3830170992Srrs struct sctp_stream_reset_out_req *sr_req) 3831163953Srrs{ 3832163953Srrs int chk_length, param_len, ptype; 3833170992Srrs struct sctp_paramhdr pstore; 3834170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3835170992Srrs 3836163953Srrs uint32_t seq; 3837163953Srrs int num_req = 0; 3838170992Srrs int trunc = 0; 3839163953Srrs struct sctp_tmit_chunk *chk; 3840163953Srrs struct sctp_chunkhdr *ch; 3841163953Srrs struct sctp_paramhdr *ph; 3842166675Srrs int ret_code = 0; 3843166675Srrs int num_param = 0; 3844163953Srrs 3845163953Srrs /* now it may be a reset or a reset-response */ 3846163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3847163953Srrs 3848163953Srrs /* setup for adding the response */ 3849163953Srrs sctp_alloc_a_chunk(stcb, chk); 3850163953Srrs if (chk == NULL) { 3851163953Srrs return (ret_code); 3852163953Srrs } 3853163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3854169208Srrs chk->rec.chunk_id.can_take_data = 0; 3855163953Srrs chk->asoc = &stcb->asoc; 3856163953Srrs chk->no_fr_allowed = 0; 3857163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3858166023Srrs chk->book_size_scale = 0; 3859165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3860163953Srrs if (chk->data == NULL) { 3861163953Srrsstrres_nochunk: 3862163953Srrs if (chk->data) { 3863163953Srrs sctp_m_freem(chk->data); 3864163953Srrs chk->data = NULL; 3865163953Srrs } 3866163953Srrs sctp_free_a_chunk(stcb, chk); 3867163953Srrs return (ret_code); 3868163953Srrs } 3869165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3870163953Srrs 3871163953Srrs /* setup chunk parameters */ 3872163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3873163953Srrs chk->snd_count = 0; 3874163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3875163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3876163953Srrs 3877163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3878163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3879163953Srrs ch->chunk_flags = 0; 3880163953Srrs ch->chunk_length = htons(chk->send_size); 3881165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3882170992Srrs offset += sizeof(struct sctp_chunkhdr); 3883163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3884170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3885170992Srrs if (ph == NULL) 3886170992Srrs break; 3887163953Srrs param_len = ntohs(ph->param_length); 3888163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3889163953Srrs /* bad param */ 3890163953Srrs break; 3891163953Srrs } 3892170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3893170992Srrs (uint8_t *) & cstore); 3894163953Srrs ptype = ntohs(ph->param_type); 3895163953Srrs num_param++; 3896170994Srrs if (param_len > (int)sizeof(cstore)) { 3897170992Srrs trunc = 1; 3898170992Srrs } else { 3899170992Srrs trunc = 0; 3900170992Srrs } 3901170992Srrs 3902163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3903163953Srrs /* hit the max of parameters already sorry.. */ 3904163953Srrs break; 3905163953Srrs } 3906163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3907163953Srrs struct sctp_stream_reset_out_request *req_out; 3908163953Srrs 3909163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3910163953Srrs num_req++; 3911163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3912163953Srrs seq = ntohl(req_out->response_seq); 3913163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3914163953Srrs /* implicit ack */ 3915169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3916163953Srrs } 3917163953Srrs } 3918170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3919188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3920188854Srrs struct sctp_stream_reset_add_strm *str_add; 3921188854Srrs 3922188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3923188854Srrs num_req++; 3924188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 3925163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3926163953Srrs struct sctp_stream_reset_in_request *req_in; 3927163953Srrs 3928163953Srrs num_req++; 3929170992Srrs 3930163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3931170992Srrs 3932170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3933163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3934163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3935163953Srrs 3936163953Srrs num_req++; 3937163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3938170992Srrs 3939163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3940163953Srrs ret_code = 1; 3941163953Srrs goto strres_nochunk; 3942163953Srrs } 3943163953Srrs /* no more */ 3944163953Srrs break; 3945163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3946163953Srrs struct sctp_stream_reset_response *resp; 3947163953Srrs uint32_t result; 3948163953Srrs 3949163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3950163953Srrs seq = ntohl(resp->response_seq); 3951163953Srrs result = ntohl(resp->result); 3952163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3953163953Srrs ret_code = 1; 3954163953Srrs goto strres_nochunk; 3955163953Srrs } 3956163953Srrs } else { 3957163953Srrs break; 3958163953Srrs } 3959170992Srrs offset += SCTP_SIZE32(param_len); 3960163953Srrs chk_length -= SCTP_SIZE32(param_len); 3961163953Srrs } 3962163953Srrs if (num_req == 0) { 3963163953Srrs /* we have no response free the stuff */ 3964163953Srrs goto strres_nochunk; 3965163953Srrs } 3966163953Srrs /* ok we have a chunk to link in */ 3967163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3968163953Srrs chk, 3969163953Srrs sctp_next); 3970163953Srrs stcb->asoc.ctrl_queue_cnt++; 3971163953Srrs return (ret_code); 3972163953Srrs} 3973163953Srrs 3974163953Srrs/* 3975163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3976163953Srrs * to handle this, either we get the whole packet and must disect it 3977163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3978163953Srrs * from a middle box that did the disectting for us. 3979163953Srrs */ 3980163953Srrsstatic void 3981163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3982170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3983163953Srrs{ 3984163953Srrs uint32_t bottle_bw, on_queue; 3985163953Srrs uint16_t trunc_len; 3986163953Srrs unsigned int chlen; 3987163953Srrs unsigned int at; 3988163953Srrs struct sctp_chunk_desc desc; 3989163953Srrs struct sctp_chunkhdr *ch; 3990163953Srrs 3991163953Srrs chlen = ntohs(cp->ch.chunk_length); 3992163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3993163953Srrs /* XXX possible chlen underflow */ 3994163953Srrs if (chlen == 0) { 3995163953Srrs ch = NULL; 3996163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3997163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3998163953Srrs } else { 3999163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4000163953Srrs chlen -= sizeof(struct sctphdr); 4001163953Srrs /* XXX possible chlen underflow */ 4002163953Srrs memset(&desc, 0, sizeof(desc)); 4003163953Srrs } 4004163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4005170781Srrs if (trunc_len > limit) { 4006170781Srrs trunc_len = limit; 4007170781Srrs } 4008163953Srrs /* now the chunks themselves */ 4009163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4010163953Srrs desc.chunk_type = ch->chunk_type; 4011163953Srrs /* get amount we need to move */ 4012163953Srrs at = ntohs(ch->chunk_length); 4013163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4014163953Srrs /* corrupt chunk, maybe at the end? */ 4015163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4016163953Srrs break; 4017163953Srrs } 4018163953Srrs if (trunc_len == 0) { 4019163953Srrs /* we are supposed to have all of it */ 4020163953Srrs if (at > chlen) { 4021163953Srrs /* corrupt skip it */ 4022163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4023163953Srrs break; 4024163953Srrs } 4025163953Srrs } else { 4026163953Srrs /* is there enough of it left ? */ 4027163953Srrs if (desc.chunk_type == SCTP_DATA) { 4028163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4029163953Srrs sizeof(desc.data_bytes))) { 4030163953Srrs break; 4031163953Srrs } 4032163953Srrs } else { 4033163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4034163953Srrs break; 4035163953Srrs } 4036163953Srrs } 4037163953Srrs } 4038163953Srrs if (desc.chunk_type == SCTP_DATA) { 4039163953Srrs /* can we get out the tsn? */ 4040163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4041163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4042163953Srrs 4043163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4044163953Srrs /* yep */ 4045163953Srrs struct sctp_data_chunk *dcp; 4046163953Srrs uint8_t *ddp; 4047163953Srrs unsigned int iii; 4048163953Srrs 4049163953Srrs dcp = (struct sctp_data_chunk *)ch; 4050163953Srrs ddp = (uint8_t *) (dcp + 1); 4051163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4052163953Srrs desc.data_bytes[iii] = ddp[iii]; 4053163953Srrs } 4054163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4055163953Srrs } else { 4056163953Srrs /* nope we are done. */ 4057163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4058163953Srrs break; 4059163953Srrs } 4060163953Srrs } else { 4061163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4062163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4063163953Srrs } 4064163953Srrs 4065163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4066163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4067163953Srrs break; 4068163953Srrs } 4069163953Srrs if (SCTP_SIZE32(at) > chlen) { 4070163953Srrs break; 4071163953Srrs } 4072163953Srrs chlen -= SCTP_SIZE32(at); 4073163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4074163953Srrs /* done, none left */ 4075163953Srrs break; 4076163953Srrs } 4077163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4078163953Srrs } 4079163953Srrs /* Now update any rwnd --- possibly */ 4080163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4081163953Srrs /* From a peer, we get a rwnd report */ 4082163953Srrs uint32_t a_rwnd; 4083163953Srrs 4084163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4085163953Srrs 4086163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4087163953Srrs on_queue = ntohl(cp->current_onq); 4088163953Srrs if (bottle_bw && on_queue) { 4089163953Srrs /* a rwnd report is in here */ 4090163953Srrs if (bottle_bw > on_queue) 4091163953Srrs a_rwnd = bottle_bw - on_queue; 4092163953Srrs else 4093163953Srrs a_rwnd = 0; 4094163953Srrs 4095163953Srrs if (a_rwnd == 0) 4096163953Srrs stcb->asoc.peers_rwnd = 0; 4097163953Srrs else { 4098163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4099163953Srrs stcb->asoc.peers_rwnd = 4100163953Srrs a_rwnd - stcb->asoc.total_flight; 4101163953Srrs } else { 4102163953Srrs stcb->asoc.peers_rwnd = 0; 4103163953Srrs } 4104163953Srrs if (stcb->asoc.peers_rwnd < 4105163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4106163953Srrs /* SWS sender side engages */ 4107163953Srrs stcb->asoc.peers_rwnd = 0; 4108163953Srrs } 4109163953Srrs } 4110163953Srrs } 4111163953Srrs } else { 4112163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4113163953Srrs } 4114163953Srrs 4115163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4116163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4117163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4118163953Srrs (stcb->asoc.sat_network)) { 4119163953Srrs /* 4120163953Srrs * This is debateable but for sat networks it makes sense 4121163953Srrs * Note if a T3 timer has went off, we will prohibit any 4122163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4123163953Srrs */ 4124171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4125171440Srrs net, cp, &bottle_bw, &on_queue); 4126163953Srrs } 4127163953Srrs} 4128163953Srrs 4129163953Srrs/* 4130163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4131163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4132163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4133163953Srrs * length of the complete packet outputs: - length: modified to remaining 4134163953Srrs * length after control processing - netp: modified to new sctp_nets after 4135163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4136163953Srrs * bad packet,...) otherwise return the tcb for this packet 4137163953Srrs */ 4138170099Srrs#ifdef __GNUC__ 4139170099Srrs__attribute__((noinline)) 4140170099Srrs#endif 4141170099Srrs static struct sctp_tcb * 4142170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4143170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4144170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4145179157Srrs uint32_t vrf_id, uint16_t port) 4146163953Srrs{ 4147163953Srrs struct sctp_association *asoc; 4148163953Srrs uint32_t vtag_in; 4149163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4150170140Srrs uint32_t chk_length; 4151163953Srrs int ret; 4152168299Srrs int abort_no_unlock = 0; 4153163953Srrs 4154163953Srrs /* 4155163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4156163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4157163953Srrs * until we get into jumbo grams and such.. 4158163953Srrs */ 4159166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4160163953Srrs struct sctp_tcb *locked_tcb = stcb; 4161163953Srrs int got_auth = 0; 4162163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4163163953Srrs int auth_skipped = 0; 4164171990Srrs int asconf_cnt = 0; 4165163953Srrs 4166172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4167172090Srrs struct socket *so; 4168172090Srrs 4169172090Srrs#endif 4170172090Srrs 4171169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4172169420Srrs iphlen, *offset, length, stcb); 4173163953Srrs 4174163953Srrs /* validate chunk header length... */ 4175163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4176170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4177170056Srrs ntohs(ch->chunk_length)); 4178170091Srrs if (locked_tcb) { 4179170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4180170091Srrs } 4181163953Srrs return (NULL); 4182163953Srrs } 4183163953Srrs /* 4184163953Srrs * validate the verification tag 4185163953Srrs */ 4186163953Srrs vtag_in = ntohl(sh->v_tag); 4187163953Srrs 4188165220Srrs if (locked_tcb) { 4189165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4190165220Srrs } 4191163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4192170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4193170056Srrs ntohs(ch->chunk_length), vtag_in); 4194163953Srrs if (vtag_in != 0) { 4195163953Srrs /* protocol error- silently discard... */ 4196163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4197169378Srrs if (locked_tcb) { 4198163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4199169378Srrs } 4200163953Srrs return (NULL); 4201163953Srrs } 4202163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4203163953Srrs /* 4204163953Srrs * If there is no stcb, skip the AUTH chunk and process 4205163953Srrs * later after a stcb is found (to validate the lookup was 4206163953Srrs * valid. 4207163953Srrs */ 4208163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4209179783Srrs (stcb == NULL) && 4210179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4211163953Srrs /* save this chunk for later processing */ 4212163953Srrs auth_skipped = 1; 4213163953Srrs auth_offset = *offset; 4214163953Srrs auth_len = ntohs(ch->chunk_length); 4215163953Srrs 4216163953Srrs /* (temporarily) move past this chunk */ 4217163953Srrs *offset += SCTP_SIZE32(auth_len); 4218163953Srrs if (*offset >= length) { 4219163953Srrs /* no more data left in the mbuf chain */ 4220163953Srrs *offset = length; 4221170091Srrs if (locked_tcb) { 4222170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4223170091Srrs } 4224163953Srrs return (NULL); 4225163953Srrs } 4226163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4227163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4228163953Srrs } 4229169420Srrs if (ch == NULL) { 4230169420Srrs /* Help */ 4231169420Srrs *offset = length; 4232170091Srrs if (locked_tcb) { 4233170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4234170091Srrs } 4235169420Srrs return (NULL); 4236169420Srrs } 4237163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4238163953Srrs goto process_control_chunks; 4239163953Srrs } 4240163953Srrs /* 4241163953Srrs * first check if it's an ASCONF with an unknown src addr we 4242163953Srrs * need to look inside to find the association 4243163953Srrs */ 4244163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4245171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4246171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4247171990Srrs 4248163953Srrs /* inp's refcount may be reduced */ 4249163953Srrs SCTP_INP_INCR_REF(inp); 4250163953Srrs 4251171990Srrs asconf_offset = *offset; 4252171990Srrs do { 4253171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4254171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4255171990Srrs break; 4256171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4257185694Srrs *offset, sh, &inp, netp, vrf_id); 4258171990Srrs if (stcb != NULL) 4259171990Srrs break; 4260171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4261171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4262171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4263171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4264163953Srrs if (stcb == NULL) { 4265163953Srrs /* 4266163953Srrs * reduce inp's refcount if not reduced in 4267163953Srrs * sctp_findassociation_ep_asconf(). 4268163953Srrs */ 4269163953Srrs SCTP_INP_DECR_REF(inp); 4270171990Srrs } else { 4271171990Srrs locked_tcb = stcb; 4272163953Srrs } 4273171990Srrs 4274163953Srrs /* now go back and verify any auth chunk to be sure */ 4275163953Srrs if (auth_skipped && (stcb != NULL)) { 4276163953Srrs struct sctp_auth_chunk *auth; 4277163953Srrs 4278163953Srrs auth = (struct sctp_auth_chunk *) 4279163953Srrs sctp_m_getptr(m, auth_offset, 4280163953Srrs auth_len, chunk_buf); 4281163953Srrs got_auth = 1; 4282163953Srrs auth_skipped = 0; 4283169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4284163953Srrs auth_offset)) { 4285163953Srrs /* auth HMAC failed so dump it */ 4286163953Srrs *offset = length; 4287170091Srrs if (locked_tcb) { 4288170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4289170091Srrs } 4290163953Srrs return (NULL); 4291163953Srrs } else { 4292163953Srrs /* remaining chunks are HMAC checked */ 4293163953Srrs stcb->asoc.authenticated = 1; 4294163953Srrs } 4295163953Srrs } 4296163953Srrs } 4297163953Srrs if (stcb == NULL) { 4298163953Srrs /* no association, so it's out of the blue... */ 4299169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4300179157Srrs vrf_id, port); 4301163953Srrs *offset = length; 4302169378Srrs if (locked_tcb) { 4303163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4304169378Srrs } 4305163953Srrs return (NULL); 4306163953Srrs } 4307163953Srrs asoc = &stcb->asoc; 4308163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4309163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4310163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4311163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4312163953Srrs if ((vtag_in == asoc->my_vtag) || 4313163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4314163953Srrs (vtag_in == asoc->peer_vtag))) { 4315163953Srrs /* this is valid */ 4316163953Srrs } else { 4317163953Srrs /* drop this packet... */ 4318163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4319169378Srrs if (locked_tcb) { 4320163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4321169378Srrs } 4322163953Srrs return (NULL); 4323163953Srrs } 4324163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4325163953Srrs if (vtag_in != asoc->my_vtag) { 4326163953Srrs /* 4327163953Srrs * this could be a stale SHUTDOWN-ACK or the 4328163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4329163953Srrs * is still hung; we have started a new asoc 4330163953Srrs * but it won't complete until the shutdown 4331163953Srrs * is completed 4332163953Srrs */ 4333169378Srrs if (locked_tcb) { 4334163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4335169378Srrs } 4336163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4337179157Srrs NULL, vrf_id, port); 4338163953Srrs return (NULL); 4339163953Srrs } 4340163953Srrs } else { 4341163953Srrs /* for all other chunks, vtag must match */ 4342163953Srrs if (vtag_in != asoc->my_vtag) { 4343163953Srrs /* invalid vtag... */ 4344169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4345169420Srrs "invalid vtag: %xh, expect %xh\n", 4346169420Srrs vtag_in, asoc->my_vtag); 4347163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4348169378Srrs if (locked_tcb) { 4349163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4350169378Srrs } 4351163953Srrs *offset = length; 4352163953Srrs return (NULL); 4353163953Srrs } 4354163953Srrs } 4355163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4356163953Srrs /* 4357163953Srrs * process all control chunks... 4358163953Srrs */ 4359163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4360185694Srrs /* EY */ 4361185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4362163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4363163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4364163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4365179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4366171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4367171943Srrs stcb->asoc.overall_error_count, 4368171943Srrs 0, 4369171943Srrs SCTP_FROM_SCTP_INPUT, 4370171943Srrs __LINE__); 4371171943Srrs } 4372163953Srrs stcb->asoc.overall_error_count = 0; 4373163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4374163953Srrs *netp); 4375163953Srrs } 4376163953Srrsprocess_control_chunks: 4377163953Srrs while (IS_SCTP_CONTROL(ch)) { 4378163953Srrs /* validate chunk length */ 4379163953Srrs chk_length = ntohs(ch->chunk_length); 4380169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4381169420Srrs ch->chunk_type, chk_length); 4382170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4383170140Srrs if (chk_length < sizeof(*ch) || 4384170140Srrs (*offset + (int)chk_length) > length) { 4385163953Srrs *offset = length; 4386169378Srrs if (locked_tcb) { 4387163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4388169378Srrs } 4389163953Srrs return (NULL); 4390163953Srrs } 4391163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4392163953Srrs /* 4393163953Srrs * INIT-ACK only gets the init ack "header" portion only 4394163953Srrs * because we don't have to process the peer's COOKIE. All 4395163953Srrs * others get a complete chunk. 4396163953Srrs */ 4397169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4398169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4399163953Srrs /* get an init-ack chunk */ 4400163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4401163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4402163953Srrs if (ch == NULL) { 4403163953Srrs *offset = length; 4404169378Srrs if (locked_tcb) { 4405163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4406169378Srrs } 4407163953Srrs return (NULL); 4408163953Srrs } 4409170642Srrs } else { 4410170786Srrs /* For cookies and all other chunks. */ 4411169208Srrs if (chk_length > sizeof(chunk_buf)) { 4412169208Srrs /* 4413169208Srrs * use just the size of the chunk buffer so 4414170642Srrs * the front part of our chunks fit in 4415170642Srrs * contiguous space up to the chunk buffer 4416170642Srrs * size (508 bytes). For chunks that need to 4417170786Srrs * get more than that they must use the 4418170642Srrs * sctp_m_getptr() function or other means 4419170786Srrs * (e.g. know how to parse mbuf chains). 4420170786Srrs * Cookies do this already. 4421169208Srrs */ 4422169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4423169208Srrs (sizeof(chunk_buf) - 4), 4424169208Srrs chunk_buf); 4425169208Srrs if (ch == NULL) { 4426169208Srrs *offset = length; 4427169378Srrs if (locked_tcb) { 4428169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4429169378Srrs } 4430169208Srrs return (NULL); 4431169208Srrs } 4432169208Srrs } else { 4433169208Srrs /* We can fit it all */ 4434170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4435170642Srrs chk_length, chunk_buf); 4436170642Srrs if (ch == NULL) { 4437170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4438170642Srrs *offset = length; 4439170642Srrs if (locked_tcb) { 4440170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4441169208Srrs } 4442170642Srrs return (NULL); 4443163953Srrs } 4444163953Srrs } 4445163953Srrs } 4446163953Srrs num_chunks++; 4447163953Srrs /* Save off the last place we got a control from */ 4448163953Srrs if (stcb != NULL) { 4449169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4450163953Srrs /* 4451163953Srrs * allow last_control to be NULL if 4452163953Srrs * ASCONF... ASCONF processing will find the 4453163953Srrs * right net later 4454163953Srrs */ 4455169420Srrs if ((netp != NULL) && (*netp != NULL)) 4456169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4457163953Srrs } 4458163953Srrs } 4459163953Srrs#ifdef SCTP_AUDITING_ENABLED 4460163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4461163953Srrs#endif 4462163953Srrs 4463163953Srrs /* check to see if this chunk required auth, but isn't */ 4464179783Srrs if ((stcb != NULL) && 4465179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4466179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4467163953Srrs !stcb->asoc.authenticated) { 4468163953Srrs /* "silently" ignore */ 4469163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4470163953Srrs goto next_chunk; 4471163953Srrs } 4472163953Srrs switch (ch->chunk_type) { 4473163953Srrs case SCTP_INITIATION: 4474163953Srrs /* must be first and only chunk */ 4475169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4476163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4477163953Srrs /* We are not interested anymore? */ 4478163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4479163953Srrs /* 4480163953Srrs * collision case where we are 4481163953Srrs * sending to them too 4482163953Srrs */ 4483163953Srrs ; 4484163953Srrs } else { 4485169378Srrs if (locked_tcb) { 4486163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4487169378Srrs } 4488163953Srrs *offset = length; 4489163953Srrs return (NULL); 4490163953Srrs } 4491163953Srrs } 4492171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4493171990Srrs (num_chunks > 1) || 4494179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4495163953Srrs *offset = length; 4496169378Srrs if (locked_tcb) { 4497163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4498169378Srrs } 4499163953Srrs return (NULL); 4500163953Srrs } 4501163953Srrs if ((stcb != NULL) && 4502163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4503163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4504163953Srrs sctp_send_shutdown_ack(stcb, 4505163953Srrs stcb->asoc.primary_destination); 4506163953Srrs *offset = length; 4507172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4508169378Srrs if (locked_tcb) { 4509163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4510169378Srrs } 4511163953Srrs return (NULL); 4512163953Srrs } 4513169420Srrs if (netp) { 4514169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4515169420Srrs (struct sctp_init_chunk *)ch, inp, 4516179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4517169420Srrs } 4518168299Srrs if (abort_no_unlock) 4519168299Srrs return (NULL); 4520168299Srrs 4521163953Srrs *offset = length; 4522169378Srrs if (locked_tcb) { 4523163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4524169378Srrs } 4525163953Srrs return (NULL); 4526163953Srrs break; 4527170642Srrs case SCTP_PAD_CHUNK: 4528170642Srrs break; 4529163953Srrs case SCTP_INITIATION_ACK: 4530163953Srrs /* must be first and only chunk */ 4531169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4532163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4533163953Srrs /* We are not interested anymore */ 4534163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4535163953Srrs ; 4536163953Srrs } else { 4537169378Srrs if (locked_tcb) { 4538163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4539169378Srrs } 4540163953Srrs *offset = length; 4541163953Srrs if (stcb) { 4542172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4543172090Srrs so = SCTP_INP_SO(inp); 4544172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4545172090Srrs SCTP_TCB_UNLOCK(stcb); 4546172090Srrs SCTP_SOCKET_LOCK(so, 1); 4547172090Srrs SCTP_TCB_LOCK(stcb); 4548172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4549172090Srrs#endif 4550171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4551172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4552172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4553172090Srrs#endif 4554163953Srrs } 4555163953Srrs return (NULL); 4556163953Srrs } 4557163953Srrs } 4558163953Srrs if ((num_chunks > 1) || 4559179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4560163953Srrs *offset = length; 4561169378Srrs if (locked_tcb) { 4562163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4563169378Srrs } 4564163953Srrs return (NULL); 4565163953Srrs } 4566169420Srrs if ((netp) && (*netp)) { 4567169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4568170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4569169420Srrs } else { 4570169420Srrs ret = -1; 4571169420Srrs } 4572163953Srrs /* 4573163953Srrs * Special case, I must call the output routine to 4574163953Srrs * get the cookie echoed 4575163953Srrs */ 4576168299Srrs if (abort_no_unlock) 4577168299Srrs return (NULL); 4578168299Srrs 4579163953Srrs if ((stcb) && ret == 0) 4580172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4581163953Srrs *offset = length; 4582169378Srrs if (locked_tcb) { 4583163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4584169378Srrs } 4585163953Srrs return (NULL); 4586163953Srrs break; 4587163953Srrs case SCTP_SELECTIVE_ACK: 4588169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4589163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4590163953Srrs { 4591163953Srrs struct sctp_sack_chunk *sack; 4592163953Srrs int abort_now = 0; 4593163953Srrs uint32_t a_rwnd, cum_ack; 4594202526Stuexen uint16_t num_seg, num_dup; 4595202526Stuexen uint8_t flags; 4596202526Stuexen int offset_seg, offset_dup; 4597163953Srrs int nonce_sum_flag; 4598163953Srrs 4599202526Stuexen if (stcb == NULL) { 4600202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4601202526Stuexen break; 4602169208Srrs } 4603202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4604202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4605202526Stuexen break; 4606202526Stuexen } 4607171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4608171990Srrs /*- 4609171990Srrs * If we have sent a shutdown-ack, we will pay no 4610171990Srrs * attention to a sack sent in to us since 4611171990Srrs * we don't care anymore. 4612171990Srrs */ 4613184883Srrs break; 4614171990Srrs } 4615163953Srrs sack = (struct sctp_sack_chunk *)ch; 4616202526Stuexen flags = ch->chunk_flags; 4617202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4618163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4619163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4620202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4621163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4622202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4623202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4624202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4625202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4626202526Stuexen break; 4627202526Stuexen } 4628202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4629202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4630170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4631202526Stuexen cum_ack, num_seg, a_rwnd); 4632163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4633163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4634163953Srrs (num_seg == 0) && 4635163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4636163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4637163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4638163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4639163953Srrs ) { 4640163953Srrs /* 4641163953Srrs * We have a SIMPLE sack having no 4642163953Srrs * prior segments and data on sent 4643163953Srrs * queue to be acked.. Use the 4644163953Srrs * faster path sack processing. We 4645163953Srrs * also allow window update sacks 4646163953Srrs * with no missing segments to go 4647163953Srrs * this way too. 4648163953Srrs */ 4649169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4650169208Srrs &abort_now); 4651163953Srrs } else { 4652169420Srrs if (netp && *netp) 4653202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4654202526Stuexen stcb, *netp, 4655202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4656202526Stuexen cum_ack, a_rwnd); 4657163953Srrs } 4658202526Stuexen if (abort_now) { 4659202526Stuexen /* ABORT signal from sack processing */ 4660202526Stuexen *offset = length; 4661202526Stuexen return (NULL); 4662202526Stuexen } 4663185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4664185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4665185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4666185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4667185694Srrs } 4668163953Srrs } 4669163953Srrs break; 4670185694Srrs /* 4671185694Srrs * EY - nr_sack: If the received chunk is an 4672185694Srrs * nr_sack chunk 4673185694Srrs */ 4674185694Srrs case SCTP_NR_SELECTIVE_ACK: 4675185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4676185694Srrs SCTP_STAT_INCR(sctps_recvsacks); 4677185694Srrs { 4678185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4679185694Srrs int abort_now = 0; 4680185694Srrs uint32_t a_rwnd, cum_ack; 4681202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4682202526Stuexen uint8_t flags; 4683202526Stuexen int offset_seg, offset_dup; 4684194355Srrs int nonce_sum_flag; 4685185694Srrs 4686185694Srrs /* 4687185694Srrs * EY nr_sacks have not been negotiated but 4688185694Srrs * the peer end sent an nr_sack, silently 4689185694Srrs * discard the chunk 4690185694Srrs */ 4691202526Stuexen if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && 4692202526Stuexen stcb->asoc.peer_supports_nr_sack)) { 4693185694Srrs goto unknown_chunk; 4694185694Srrs } 4695202526Stuexen if (stcb == NULL) { 4696202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4697202526Stuexen break; 4698202526Stuexen } 4699202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4700202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4701202526Stuexen break; 4702202526Stuexen } 4703185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4704185694Srrs /*- 4705185694Srrs * If we have sent a shutdown-ack, we will pay no 4706185694Srrs * attention to a sack sent in to us since 4707185694Srrs * we don't care anymore. 4708185694Srrs */ 4709202526Stuexen break; 4710185694Srrs } 4711185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4712202526Stuexen flags = ch->chunk_flags; 4713202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4714185694Srrs 4715185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4716185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4717185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4718202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4719185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4720202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4721202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4722202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4723202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4724202526Stuexen break; 4725202526Stuexen } 4726202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4727202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4728185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4729202526Stuexen cum_ack, num_seg, a_rwnd); 4730185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4731185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4732202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4733185694Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4734185694Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4735185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4736202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4737185694Srrs /* 4738185694Srrs * We have a SIMPLE sack having no 4739185694Srrs * prior segments and data on sent 4740202526Stuexen * queue to be acked. Use the faster 4741202526Stuexen * path sack processing. We also 4742202526Stuexen * allow window update sacks with no 4743202526Stuexen * missing segments to go this way 4744202526Stuexen * too. 4745185694Srrs */ 4746202526Stuexen sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4747185694Srrs &abort_now); 4748185694Srrs } else { 4749185694Srrs if (netp && *netp) 4750202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4751202526Stuexen stcb, *netp, 4752202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4753202526Stuexen cum_ack, a_rwnd); 4754185694Srrs } 4755202526Stuexen if (abort_now) { 4756202526Stuexen /* ABORT signal from sack processing */ 4757202526Stuexen *offset = length; 4758202526Stuexen return (NULL); 4759202526Stuexen } 4760185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4761185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4762185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4763185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4764185694Srrs } 4765185694Srrs } 4766185694Srrs break; 4767185694Srrs 4768163953Srrs case SCTP_HEARTBEAT_REQUEST: 4769169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4770169420Srrs if ((stcb) && netp && *netp) { 4771169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4772169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4773169420Srrs chk_length, *netp); 4774169420Srrs 4775169420Srrs /* He's alive so give him credit */ 4776179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4777171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4778171943Srrs stcb->asoc.overall_error_count, 4779171943Srrs 0, 4780171943Srrs SCTP_FROM_SCTP_INPUT, 4781171943Srrs __LINE__); 4782171943Srrs } 4783169420Srrs stcb->asoc.overall_error_count = 0; 4784163953Srrs } 4785163953Srrs break; 4786163953Srrs case SCTP_HEARTBEAT_ACK: 4787169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4788169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4789169208Srrs /* Its not ours */ 4790169352Srrs *offset = length; 4791169378Srrs if (locked_tcb) { 4792169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4793169378Srrs } 4794169208Srrs return (NULL); 4795169208Srrs } 4796163953Srrs /* He's alive so give him credit */ 4797179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4798171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4799171943Srrs stcb->asoc.overall_error_count, 4800171943Srrs 0, 4801171943Srrs SCTP_FROM_SCTP_INPUT, 4802171943Srrs __LINE__); 4803171943Srrs } 4804163953Srrs stcb->asoc.overall_error_count = 0; 4805163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4806169420Srrs if (netp && *netp) 4807169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4808169420Srrs stcb, *netp); 4809163953Srrs break; 4810163953Srrs case SCTP_ABORT_ASSOCIATION: 4811170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4812170091Srrs stcb); 4813169420Srrs if ((stcb) && netp && *netp) 4814169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4815169420Srrs stcb, *netp); 4816163953Srrs *offset = length; 4817163953Srrs return (NULL); 4818163953Srrs break; 4819163953Srrs case SCTP_SHUTDOWN: 4820170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4821170091Srrs stcb); 4822169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4823169208Srrs *offset = length; 4824169378Srrs if (locked_tcb) { 4825169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4826169378Srrs } 4827169208Srrs return (NULL); 4828169420Srrs } 4829169420Srrs if (netp && *netp) { 4830163953Srrs int abort_flag = 0; 4831163953Srrs 4832163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4833163953Srrs stcb, *netp, &abort_flag); 4834163953Srrs if (abort_flag) { 4835163953Srrs *offset = length; 4836163953Srrs return (NULL); 4837163953Srrs } 4838163953Srrs } 4839163953Srrs break; 4840163953Srrs case SCTP_SHUTDOWN_ACK: 4841170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4842169420Srrs if ((stcb) && (netp) && (*netp)) 4843169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4844163953Srrs *offset = length; 4845163953Srrs return (NULL); 4846163953Srrs break; 4847169420Srrs 4848163953Srrs case SCTP_OPERATION_ERROR: 4849169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4850169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4851169420Srrs 4852163953Srrs *offset = length; 4853163953Srrs return (NULL); 4854163953Srrs } 4855163953Srrs break; 4856163953Srrs case SCTP_COOKIE_ECHO: 4857169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4858170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4859163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4860163953Srrs ; 4861163953Srrs } else { 4862169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4863163953Srrs /* We are not interested anymore */ 4864208853Srrs if (stcb) { 4865208853Srrs SCTP_TCB_UNLOCK(stcb); 4866208853Srrs } 4867163953Srrs *offset = length; 4868163953Srrs return (NULL); 4869163953Srrs } 4870163953Srrs } 4871163953Srrs /* 4872163953Srrs * First are we accepting? We do this again here 4873207924Srrs * since it is possible that a previous endpoint WAS 4874207924Srrs * listening responded to a INIT-ACK and then 4875163953Srrs * closed. We opened and bound.. and are now no 4876163953Srrs * longer listening. 4877163953Srrs */ 4878172703Srrs 4879172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4880172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4881179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4882163953Srrs struct mbuf *oper; 4883163953Srrs struct sctp_paramhdr *phdr; 4884163953Srrs 4885172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4886172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4887172703Srrs if (oper) { 4888172703Srrs SCTP_BUF_LEN(oper) = 4889172703Srrs sizeof(struct sctp_paramhdr); 4890172703Srrs phdr = mtod(oper, 4891172703Srrs struct sctp_paramhdr *); 4892172703Srrs phdr->param_type = 4893172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4894172703Srrs phdr->param_length = 4895172703Srrs htons(sizeof(struct sctp_paramhdr)); 4896163953Srrs } 4897172703Srrs sctp_abort_association(inp, stcb, m, 4898179157Srrs iphlen, sh, oper, vrf_id, port); 4899163953Srrs } 4900172703Srrs *offset = length; 4901172703Srrs return (NULL); 4902172703Srrs } else { 4903163953Srrs struct mbuf *ret_buf; 4904165220Srrs struct sctp_inpcb *linp; 4905163953Srrs 4906169420Srrs if (stcb) { 4907165220Srrs linp = NULL; 4908169420Srrs } else { 4909165220Srrs linp = inp; 4910169420Srrs } 4911165220Srrs 4912169420Srrs if (linp) { 4913165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4914169420Srrs } 4915169420Srrs if (netp) { 4916169420Srrs ret_buf = 4917169420Srrs sctp_handle_cookie_echo(m, iphlen, 4918169420Srrs *offset, sh, 4919169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4920169420Srrs &inp, &stcb, netp, 4921169420Srrs auth_skipped, 4922169420Srrs auth_offset, 4923169420Srrs auth_len, 4924169420Srrs &locked_tcb, 4925179157Srrs vrf_id, 4926179157Srrs port); 4927169420Srrs } else { 4928169420Srrs ret_buf = NULL; 4929169420Srrs } 4930169420Srrs if (linp) { 4931165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4932169420Srrs } 4933163953Srrs if (ret_buf == NULL) { 4934163953Srrs if (locked_tcb) { 4935163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4936163953Srrs } 4937169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4938169420Srrs "GAK, null buffer\n"); 4939163953Srrs auth_skipped = 0; 4940163953Srrs *offset = length; 4941163953Srrs return (NULL); 4942163953Srrs } 4943163953Srrs /* if AUTH skipped, see if it verified... */ 4944163953Srrs if (auth_skipped) { 4945163953Srrs got_auth = 1; 4946163953Srrs auth_skipped = 0; 4947163953Srrs } 4948163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4949163953Srrs /* 4950163953Srrs * Restart the timer if we have 4951163953Srrs * pending data 4952163953Srrs */ 4953163953Srrs struct sctp_tmit_chunk *chk; 4954163953Srrs 4955163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4956163953Srrs if (chk) { 4957163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4958163953Srrs stcb->sctp_ep, stcb, 4959163953Srrs chk->whoTo); 4960163953Srrs } 4961163953Srrs } 4962163953Srrs } 4963163953Srrs break; 4964163953Srrs case SCTP_COOKIE_ACK: 4965170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4966169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4967169378Srrs if (locked_tcb) { 4968169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4969169378Srrs } 4970169352Srrs return (NULL); 4971169352Srrs } 4972163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4973163953Srrs /* We are not interested anymore */ 4974163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4975163953Srrs ; 4976169420Srrs } else if (stcb) { 4977172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4978172090Srrs so = SCTP_INP_SO(inp); 4979172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4980172090Srrs SCTP_TCB_UNLOCK(stcb); 4981172090Srrs SCTP_SOCKET_LOCK(so, 1); 4982172090Srrs SCTP_TCB_LOCK(stcb); 4983172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4984172090Srrs#endif 4985171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4986172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4987172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4988172090Srrs#endif 4989163953Srrs *offset = length; 4990163953Srrs return (NULL); 4991163953Srrs } 4992163953Srrs } 4993163953Srrs /* He's alive so give him credit */ 4994169420Srrs if ((stcb) && netp && *netp) { 4995179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4996171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4997171943Srrs stcb->asoc.overall_error_count, 4998171943Srrs 0, 4999171943Srrs SCTP_FROM_SCTP_INPUT, 5000171943Srrs __LINE__); 5001171943Srrs } 5002169378Srrs stcb->asoc.overall_error_count = 0; 5003169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5004169378Srrs } 5005163953Srrs break; 5006163953Srrs case SCTP_ECN_ECHO: 5007169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5008163953Srrs /* He's alive so give him credit */ 5009169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5010169208Srrs /* Its not ours */ 5011169378Srrs if (locked_tcb) { 5012169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5013169378Srrs } 5014169208Srrs *offset = length; 5015169208Srrs return (NULL); 5016169208Srrs } 5017169378Srrs if (stcb) { 5018179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5019171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5020171943Srrs stcb->asoc.overall_error_count, 5021171943Srrs 0, 5022171943Srrs SCTP_FROM_SCTP_INPUT, 5023171943Srrs __LINE__); 5024171943Srrs } 5025169378Srrs stcb->asoc.overall_error_count = 0; 5026169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5027169378Srrs stcb); 5028169378Srrs } 5029163953Srrs break; 5030163953Srrs case SCTP_ECN_CWR: 5031169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5032163953Srrs /* He's alive so give him credit */ 5033169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5034169208Srrs /* Its not ours */ 5035169378Srrs if (locked_tcb) { 5036169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5037169378Srrs } 5038169208Srrs *offset = length; 5039169208Srrs return (NULL); 5040169208Srrs } 5041169378Srrs if (stcb) { 5042179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5043171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5044171943Srrs stcb->asoc.overall_error_count, 5045171943Srrs 0, 5046171943Srrs SCTP_FROM_SCTP_INPUT, 5047171943Srrs __LINE__); 5048171943Srrs } 5049169378Srrs stcb->asoc.overall_error_count = 0; 5050169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 5051169378Srrs } 5052163953Srrs break; 5053163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5054170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5055163953Srrs /* must be first and only chunk */ 5056163953Srrs if ((num_chunks > 1) || 5057170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5058163953Srrs *offset = length; 5059169378Srrs if (locked_tcb) { 5060163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5061169378Srrs } 5062163953Srrs return (NULL); 5063163953Srrs } 5064169420Srrs if ((stcb) && netp && *netp) { 5065169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5066169378Srrs stcb, *netp); 5067169378Srrs } 5068163953Srrs *offset = length; 5069163953Srrs return (NULL); 5070163953Srrs break; 5071163953Srrs case SCTP_ASCONF: 5072169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5073163953Srrs /* He's alive so give him credit */ 5074169378Srrs if (stcb) { 5075179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5076171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5077171943Srrs stcb->asoc.overall_error_count, 5078171943Srrs 0, 5079171943Srrs SCTP_FROM_SCTP_INPUT, 5080171943Srrs __LINE__); 5081171943Srrs } 5082169378Srrs stcb->asoc.overall_error_count = 0; 5083169378Srrs sctp_handle_asconf(m, *offset, 5084171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5085171990Srrs asconf_cnt++; 5086169378Srrs } 5087163953Srrs break; 5088163953Srrs case SCTP_ASCONF_ACK: 5089169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5090169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5091169208Srrs /* Its not ours */ 5092169378Srrs if (locked_tcb) { 5093169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5094169378Srrs } 5095169208Srrs *offset = length; 5096169208Srrs return (NULL); 5097169208Srrs } 5098169420Srrs if ((stcb) && netp && *netp) { 5099169378Srrs /* He's alive so give him credit */ 5100179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5101171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5102171943Srrs stcb->asoc.overall_error_count, 5103171943Srrs 0, 5104171943Srrs SCTP_FROM_SCTP_INPUT, 5105171943Srrs __LINE__); 5106171943Srrs } 5107169378Srrs stcb->asoc.overall_error_count = 0; 5108169378Srrs sctp_handle_asconf_ack(m, *offset, 5109172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5110172190Srrs if (abort_no_unlock) 5111172190Srrs return (NULL); 5112169378Srrs } 5113163953Srrs break; 5114163953Srrs case SCTP_FORWARD_CUM_TSN: 5115169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5116169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5117169208Srrs /* Its not ours */ 5118169378Srrs if (locked_tcb) { 5119169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5120169378Srrs } 5121169208Srrs *offset = length; 5122169208Srrs return (NULL); 5123169208Srrs } 5124163953Srrs /* He's alive so give him credit */ 5125169378Srrs if (stcb) { 5126163953Srrs int abort_flag = 0; 5127163953Srrs 5128163953Srrs stcb->asoc.overall_error_count = 0; 5129179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5130171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5131171943Srrs stcb->asoc.overall_error_count, 5132171943Srrs 0, 5133171943Srrs SCTP_FROM_SCTP_INPUT, 5134171943Srrs __LINE__); 5135171943Srrs } 5136163953Srrs *fwd_tsn_seen = 1; 5137163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5138163953Srrs /* We are not interested anymore */ 5139172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5140172090Srrs so = SCTP_INP_SO(inp); 5141172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5142172090Srrs SCTP_TCB_UNLOCK(stcb); 5143172090Srrs SCTP_SOCKET_LOCK(so, 1); 5144172090Srrs SCTP_TCB_LOCK(stcb); 5145172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5146172090Srrs#endif 5147171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5148172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5149172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5150172090Srrs#endif 5151163953Srrs *offset = length; 5152163953Srrs return (NULL); 5153163953Srrs } 5154163953Srrs sctp_handle_forward_tsn(stcb, 5155170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5156163953Srrs if (abort_flag) { 5157163953Srrs *offset = length; 5158163953Srrs return (NULL); 5159163953Srrs } else { 5160179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5161171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5162171943Srrs stcb->asoc.overall_error_count, 5163171943Srrs 0, 5164171943Srrs SCTP_FROM_SCTP_INPUT, 5165171943Srrs __LINE__); 5166171943Srrs } 5167163953Srrs stcb->asoc.overall_error_count = 0; 5168163953Srrs } 5169163953Srrs 5170163953Srrs } 5171163953Srrs break; 5172163953Srrs case SCTP_STREAM_RESET: 5173169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5174169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5175169208Srrs /* Its not ours */ 5176169378Srrs if (locked_tcb) { 5177169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5178169378Srrs } 5179169208Srrs *offset = length; 5180169208Srrs return (NULL); 5181169208Srrs } 5182163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5183163953Srrs /* We are not interested anymore */ 5184172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5185172090Srrs so = SCTP_INP_SO(inp); 5186172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5187172090Srrs SCTP_TCB_UNLOCK(stcb); 5188172090Srrs SCTP_SOCKET_LOCK(so, 1); 5189172090Srrs SCTP_TCB_LOCK(stcb); 5190172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5191172090Srrs#endif 5192171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5193172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5194172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5195172090Srrs#endif 5196163953Srrs *offset = length; 5197163953Srrs return (NULL); 5198163953Srrs } 5199169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5200169420Srrs /* 5201169420Srrs * hmm, peer should have announced this, but 5202169420Srrs * we will turn it on since he is sending us 5203169420Srrs * a stream reset. 5204169420Srrs */ 5205169420Srrs stcb->asoc.peer_supports_strreset = 1; 5206163953Srrs } 5207170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5208169420Srrs /* stop processing */ 5209169420Srrs *offset = length; 5210169420Srrs return (NULL); 5211169420Srrs } 5212163953Srrs break; 5213163953Srrs case SCTP_PACKET_DROPPED: 5214169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5215163953Srrs /* re-get it all please */ 5216169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5217169208Srrs /* Its not ours */ 5218169378Srrs if (locked_tcb) { 5219169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5220169378Srrs } 5221169208Srrs *offset = length; 5222169208Srrs return (NULL); 5223169208Srrs } 5224169420Srrs if (ch && (stcb) && netp && (*netp)) { 5225169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5226170781Srrs stcb, *netp, 5227170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5228170781Srrs 5229169378Srrs } 5230163953Srrs break; 5231163953Srrs 5232163953Srrs case SCTP_AUTHENTICATION: 5233169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5234179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5235163953Srrs goto unknown_chunk; 5236163953Srrs 5237163953Srrs if (stcb == NULL) { 5238163953Srrs /* save the first AUTH for later processing */ 5239163953Srrs if (auth_skipped == 0) { 5240163953Srrs auth_offset = *offset; 5241163953Srrs auth_len = chk_length; 5242163953Srrs auth_skipped = 1; 5243163953Srrs } 5244163953Srrs /* skip this chunk (temporarily) */ 5245163953Srrs goto next_chunk; 5246163953Srrs } 5247169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5248169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5249169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5250169208Srrs /* Its not ours */ 5251169378Srrs if (locked_tcb) { 5252169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5253169378Srrs } 5254169208Srrs *offset = length; 5255169208Srrs return (NULL); 5256169208Srrs } 5257163953Srrs if (got_auth == 1) { 5258163953Srrs /* skip this chunk... it's already auth'd */ 5259163953Srrs goto next_chunk; 5260163953Srrs } 5261163953Srrs got_auth = 1; 5262169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5263163953Srrs m, *offset)) { 5264163953Srrs /* auth HMAC failed so dump the packet */ 5265163953Srrs *offset = length; 5266163953Srrs return (stcb); 5267163953Srrs } else { 5268163953Srrs /* remaining chunks are HMAC checked */ 5269163953Srrs stcb->asoc.authenticated = 1; 5270163953Srrs } 5271163953Srrs break; 5272163953Srrs 5273163953Srrs default: 5274163953Srrs unknown_chunk: 5275163953Srrs /* it's an unknown chunk! */ 5276163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5277163953Srrs struct mbuf *mm; 5278163953Srrs struct sctp_paramhdr *phd; 5279163953Srrs 5280163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5281165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5282163953Srrs if (mm) { 5283163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5284163953Srrs /* 5285163953Srrs * We cheat and use param type since 5286163953Srrs * we did not bother to define a 5287163953Srrs * error cause struct. They are the 5288163953Srrs * same basic format with different 5289163953Srrs * names. 5290163953Srrs */ 5291163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5292163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5293165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5294166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5295163953Srrs M_DONTWAIT); 5296165647Srrs if (SCTP_BUF_NEXT(mm)) { 5297178202Srrs#ifdef SCTP_MBUF_LOGGING 5298179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5299178202Srrs struct mbuf *mat; 5300178202Srrs 5301178202Srrs mat = SCTP_BUF_NEXT(mm); 5302178202Srrs while (mat) { 5303178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5304178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5305178202Srrs } 5306178202Srrs mat = SCTP_BUF_NEXT(mat); 5307178202Srrs } 5308178202Srrs } 5309178202Srrs#endif 5310163953Srrs sctp_queue_op_err(stcb, mm); 5311163953Srrs } else { 5312163953Srrs sctp_m_freem(mm); 5313163953Srrs } 5314163953Srrs } 5315163953Srrs } 5316163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5317163953Srrs /* discard this packet */ 5318163953Srrs *offset = length; 5319163953Srrs return (stcb); 5320163953Srrs } /* else skip this bad chunk and continue... */ 5321163953Srrs break; 5322163953Srrs } /* switch (ch->chunk_type) */ 5323163953Srrs 5324163953Srrs 5325163953Srrsnext_chunk: 5326163953Srrs /* get the next chunk */ 5327163953Srrs *offset += SCTP_SIZE32(chk_length); 5328163953Srrs if (*offset >= length) { 5329163953Srrs /* no more data left in the mbuf chain */ 5330163953Srrs break; 5331163953Srrs } 5332163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5333163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5334163953Srrs if (ch == NULL) { 5335169378Srrs if (locked_tcb) { 5336163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5337169378Srrs } 5338163953Srrs *offset = length; 5339163953Srrs return (NULL); 5340163953Srrs } 5341163953Srrs } /* while */ 5342171990Srrs 5343171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5344171990Srrs sctp_send_asconf_ack(stcb); 5345171990Srrs } 5346163953Srrs return (stcb); 5347163953Srrs} 5348163953Srrs 5349163953Srrs 5350163953Srrs/* 5351163953Srrs * Process the ECN bits we have something set so we must look to see if it is 5352163953Srrs * ECN(0) or ECN(1) or CE 5353163953Srrs */ 5354170806Srrsstatic void 5355163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 5356163953Srrs uint8_t ecn_bits) 5357163953Srrs{ 5358163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5359163953Srrs ; 5360163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 5361163953Srrs /* 5362163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 5363163953Srrs * change the NS bit (that we have yet to find a way to send 5364163953Srrs * it yet). 5365163953Srrs */ 5366163953Srrs 5367163953Srrs /* ECN Nonce stuff */ 5368163953Srrs stcb->asoc.receiver_nonce_sum++; 5369163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 5370163953Srrs 5371163953Srrs /* 5372163953Srrs * Drag up the last_echo point if cumack is larger since we 5373163953Srrs * don't want the point falling way behind by more than 5374163953Srrs * 2^^31 and then having it be incorrect. 5375163953Srrs */ 5376163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5377163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5378163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5379163953Srrs } 5380163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 5381163953Srrs /* 5382163953Srrs * Drag up the last_echo point if cumack is larger since we 5383163953Srrs * don't want the point falling way behind by more than 5384163953Srrs * 2^^31 and then having it be incorrect. 5385163953Srrs */ 5386163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5387163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5388163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5389163953Srrs } 5390163953Srrs } 5391163953Srrs} 5392163953Srrs 5393170806Srrsstatic void 5394163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 5395163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 5396163953Srrs{ 5397163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5398163953Srrs /* 5399163953Srrs * we possibly must notify the sender that a congestion 5400163953Srrs * window reduction is in order. We do this by adding a ECNE 5401163953Srrs * chunk to the output chunk queue. The incoming CWR will 5402163953Srrs * remove this chunk. 5403163953Srrs */ 5404163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 5405163953Srrs MAX_TSN)) { 5406163953Srrs /* Yep, we need to add a ECNE */ 5407163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5408163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 5409163953Srrs } 5410163953Srrs } 5411163953Srrs} 5412163953Srrs 5413171440Srrs#ifdef INVARIANTS 5414208853Srrs#ifdef __GNUC__ 5415208853Srrs__attribute__((noinline)) 5416208853Srrs#endif 5417208853Srrs void 5418208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5419171440Srrs{ 5420208853Srrs struct sctp_tcb *lstcb; 5421171440Srrs 5422208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5423208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5424171440Srrs panic("Own lock on stcb at return from input"); 5425171440Srrs } 5426171440Srrs } 5427171440Srrs} 5428171440Srrs 5429171440Srrs#endif 5430171440Srrs 5431163953Srrs/* 5432163953Srrs * common input chunk processing (v4 and v6) 5433163953Srrs */ 5434169378Srrsvoid 5435163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5436163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5437163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5438179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5439163953Srrs{ 5440163953Srrs /* 5441163953Srrs * Control chunk processing 5442163953Srrs */ 5443163953Srrs uint32_t high_tsn; 5444163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5445163953Srrs struct mbuf *m = *mm; 5446163953Srrs int abort_flag = 0; 5447163953Srrs int un_sent; 5448163953Srrs 5449163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5450163953Srrs#ifdef SCTP_AUDITING_ENABLED 5451163953Srrs sctp_audit_log(0xE0, 1); 5452163953Srrs sctp_auditing(0, inp, stcb, net); 5453163953Srrs#endif 5454163953Srrs 5455179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5456179783Srrs m, iphlen, offset, length, stcb); 5457163953Srrs if (stcb) { 5458163953Srrs /* always clear this before beginning a packet */ 5459163953Srrs stcb->asoc.authenticated = 0; 5460163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5461171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5462171990Srrs stcb, stcb->asoc.state); 5463171990Srrs 5464171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5465171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5466171745Srrs /*- 5467171745Srrs * If we hit here, we had a ref count 5468171745Srrs * up when the assoc was aborted and the 5469171745Srrs * timer is clearing out the assoc, we should 5470171745Srrs * NOT respond to any packet.. its OOTB. 5471171745Srrs */ 5472171745Srrs SCTP_TCB_UNLOCK(stcb); 5473171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5474179157Srrs vrf_id, port); 5475171745Srrs goto out_now; 5476171745Srrs } 5477163953Srrs } 5478163953Srrs if (IS_SCTP_CONTROL(ch)) { 5479163953Srrs /* process the control portion of the SCTP packet */ 5480169655Srrs /* sa_ignore NO_NULL_CHK */ 5481163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5482179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5483163953Srrs if (stcb) { 5484163953Srrs /* 5485163953Srrs * This covers us if the cookie-echo was there and 5486163953Srrs * it changes our INP. 5487163953Srrs */ 5488163953Srrs inp = stcb->sctp_ep; 5489179783Srrs if ((net) && (port)) { 5490179783Srrs if (net->port == 0) { 5491179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5492179783Srrs } 5493179783Srrs net->port = port; 5494179783Srrs } 5495163953Srrs } 5496163953Srrs } else { 5497163953Srrs /* 5498163953Srrs * no control chunks, so pre-process DATA chunks (these 5499163953Srrs * checks are taken care of by control processing) 5500163953Srrs */ 5501163953Srrs 5502163953Srrs /* 5503163953Srrs * if DATA only packet, and auth is required, then punt... 5504163953Srrs * can't have authenticated without any AUTH (control) 5505163953Srrs * chunks 5506163953Srrs */ 5507179783Srrs if ((stcb != NULL) && 5508179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5509179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5510163953Srrs /* "silently" ignore */ 5511163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5512163953Srrs SCTP_TCB_UNLOCK(stcb); 5513171440Srrs goto out_now; 5514163953Srrs } 5515163953Srrs if (stcb == NULL) { 5516163953Srrs /* out of the blue DATA chunk */ 5517169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5518179157Srrs vrf_id, port); 5519171440Srrs goto out_now; 5520163953Srrs } 5521163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5522163953Srrs /* v_tag mismatch! */ 5523163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5524163953Srrs SCTP_TCB_UNLOCK(stcb); 5525171440Srrs goto out_now; 5526163953Srrs } 5527163953Srrs } 5528163953Srrs 5529163953Srrs if (stcb == NULL) { 5530163953Srrs /* 5531163953Srrs * no valid TCB for this packet, or we found it's a bad 5532163953Srrs * packet while processing control, or we're done with this 5533163953Srrs * packet (done or skip rest of data), so we drop it... 5534163953Srrs */ 5535171440Srrs goto out_now; 5536163953Srrs } 5537163953Srrs /* 5538163953Srrs * DATA chunk processing 5539163953Srrs */ 5540163953Srrs /* plow through the data chunks while length > offset */ 5541163953Srrs 5542163953Srrs /* 5543163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5544163953Srrs * DATA is required. 5545163953Srrs */ 5546179783Srrs if ((length > offset) && 5547179783Srrs (stcb != NULL) && 5548179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5549179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5550163953Srrs !stcb->asoc.authenticated) { 5551163953Srrs /* "silently" ignore */ 5552163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5553169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5554169420Srrs "Data chunk requires AUTH, skipped\n"); 5555165220Srrs goto trigger_send; 5556163953Srrs } 5557163953Srrs if (length > offset) { 5558163953Srrs int retval; 5559163953Srrs 5560163953Srrs /* 5561163953Srrs * First check to make sure our state is correct. We would 5562163953Srrs * not get here unless we really did have a tag, so we don't 5563163953Srrs * abort if this happens, just dump the chunk silently. 5564163953Srrs */ 5565163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5566163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5567163953Srrs /* 5568163953Srrs * we consider data with valid tags in this state 5569163953Srrs * shows us the cookie-ack was lost. Imply it was 5570163953Srrs * there. 5571163953Srrs */ 5572179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5573171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5574171943Srrs stcb->asoc.overall_error_count, 5575171943Srrs 0, 5576171943Srrs SCTP_FROM_SCTP_INPUT, 5577171943Srrs __LINE__); 5578171943Srrs } 5579163953Srrs stcb->asoc.overall_error_count = 0; 5580163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5581163953Srrs break; 5582163953Srrs case SCTP_STATE_COOKIE_WAIT: 5583163953Srrs /* 5584163953Srrs * We consider OOTB any data sent during asoc setup. 5585163953Srrs */ 5586169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5587179157Srrs vrf_id, port); 5588163953Srrs SCTP_TCB_UNLOCK(stcb); 5589171440Srrs goto out_now; 5590171531Srrs /* sa_ignore NOTREACHED */ 5591163953Srrs break; 5592163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5593163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5594163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5595163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5596163953Srrs default: 5597163953Srrs SCTP_TCB_UNLOCK(stcb); 5598171440Srrs goto out_now; 5599171531Srrs /* sa_ignore NOTREACHED */ 5600163953Srrs break; 5601163953Srrs case SCTP_STATE_OPEN: 5602163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5603163953Srrs break; 5604163953Srrs } 5605163953Srrs /* take care of ECN, part 1. */ 5606163953Srrs if (stcb->asoc.ecn_allowed && 5607163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5608163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5609163953Srrs } 5610163953Srrs /* plow through the data chunks while length > offset */ 5611163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5612163953Srrs inp, stcb, net, &high_tsn); 5613163953Srrs if (retval == 2) { 5614163953Srrs /* 5615163953Srrs * The association aborted, NO UNLOCK needed since 5616163953Srrs * the association is destroyed. 5617163953Srrs */ 5618171440Srrs goto out_now; 5619163953Srrs } 5620163953Srrs data_processed = 1; 5621163953Srrs if (retval == 0) { 5622163953Srrs /* take care of ecn part 2. */ 5623163953Srrs if (stcb->asoc.ecn_allowed && 5624163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5625163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5626163953Srrs ecn_bits); 5627163953Srrs } 5628163953Srrs } 5629163953Srrs /* 5630163953Srrs * Anything important needs to have been m_copy'ed in 5631163953Srrs * process_data 5632163953Srrs */ 5633163953Srrs } 5634163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5635163953Srrs int was_a_gap = 0; 5636163953Srrs 5637163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5638163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5639163953Srrs /* there was a gap before this data was processed */ 5640163953Srrs was_a_gap = 1; 5641163953Srrs } 5642190689Srrs stcb->asoc.send_sack = 1; 5643206137Stuexen sctp_sack_check(stcb, was_a_gap, &abort_flag); 5644163953Srrs if (abort_flag) { 5645163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5646171440Srrs goto out_now; 5647163953Srrs } 5648190689Srrs } else if (fwd_tsn_seen) { 5649190689Srrs stcb->asoc.send_sack = 1; 5650163953Srrs } 5651163953Srrs /* trigger send of any chunks in queue... */ 5652165220Srrstrigger_send: 5653163953Srrs#ifdef SCTP_AUDITING_ENABLED 5654163953Srrs sctp_audit_log(0xE0, 2); 5655163953Srrs sctp_auditing(1, inp, stcb, net); 5656163953Srrs#endif 5657169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5658169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5659169420Srrs stcb->asoc.peers_rwnd, 5660169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5661169420Srrs stcb->asoc.total_flight); 5662163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5663163953Srrs 5664163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5665163953Srrs ((un_sent) && 5666163953Srrs (stcb->asoc.peers_rwnd > 0 || 5667163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5668169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5669172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5670169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5671163953Srrs } 5672163953Srrs#ifdef SCTP_AUDITING_ENABLED 5673163953Srrs sctp_audit_log(0xE0, 3); 5674163953Srrs sctp_auditing(2, inp, stcb, net); 5675163953Srrs#endif 5676163953Srrs SCTP_TCB_UNLOCK(stcb); 5677171440Srrsout_now: 5678171440Srrs#ifdef INVARIANTS 5679171440Srrs sctp_validate_no_locks(inp); 5680171440Srrs#endif 5681169378Srrs return; 5682163953Srrs} 5683163953Srrs 5684185694Srrs#if 0 5685185694Srrsstatic void 5686185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5687185694Srrs{ 5688185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5689185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5690185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5691185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5692185694Srrs } 5693185694Srrs} 5694163953Srrs 5695185694Srrs#endif 5696185694Srrs 5697163953Srrsvoid 5698189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5699163953Srrs{ 5700163953Srrs#ifdef SCTP_MBUF_LOGGING 5701163953Srrs struct mbuf *mat; 5702163953Srrs 5703163953Srrs#endif 5704165647Srrs struct mbuf *m; 5705163953Srrs int iphlen; 5706170181Srrs uint32_t vrf_id = 0; 5707163953Srrs uint8_t ecn_bits; 5708163953Srrs struct ip *ip; 5709163953Srrs struct sctphdr *sh; 5710163953Srrs struct sctp_inpcb *inp = NULL; 5711163953Srrs 5712163953Srrs uint32_t check, calc_check; 5713163953Srrs struct sctp_nets *net; 5714163953Srrs struct sctp_tcb *stcb = NULL; 5715163953Srrs struct sctp_chunkhdr *ch; 5716163953Srrs int refcount_up = 0; 5717163953Srrs int length, mlen, offset; 5718163953Srrs 5719169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5720169352Srrs SCTP_RELEASE_PKT(i_pak); 5721169352Srrs return; 5722169352Srrs } 5723168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5724163953Srrs iphlen = off; 5725165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5726170056Srrs 5727163953Srrs net = NULL; 5728163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5729163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5730163953Srrs 5731170091Srrs 5732163953Srrs#ifdef SCTP_MBUF_LOGGING 5733163953Srrs /* Log in any input mbufs */ 5734179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5735170744Srrs mat = m; 5736170744Srrs while (mat) { 5737170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5738170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5739170744Srrs } 5740170744Srrs mat = SCTP_BUF_NEXT(mat); 5741163953Srrs } 5742163953Srrs } 5743163953Srrs#endif 5744170091Srrs#ifdef SCTP_PACKET_LOGGING 5745179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5746170921Srrs sctp_packet_log(m, mlen); 5747170091Srrs#endif 5748170091Srrs /* 5749170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5750170091Srrs * case) 5751170091Srrs */ 5752170091Srrs mlen -= iphlen; 5753168124Srrs 5754163953Srrs /* 5755163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5756163953Srrs */ 5757163953Srrs ip = mtod(m, struct ip *); 5758163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5759165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5760163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5761163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5762163953Srrs return; 5763163953Srrs } 5764163953Srrs ip = mtod(m, struct ip *); 5765163953Srrs } 5766188067Srrs /* validate mbuf chain length with IP payload length */ 5767188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5768188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5769188067Srrs goto bad; 5770188067Srrs } 5771163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5772163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5773170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5774170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5775163953Srrs 5776163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5777163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5778163953Srrs goto bad; 5779163953Srrs } 5780168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5781163953Srrs /* 5782163953Srrs * We only look at broadcast if its a front state, All 5783163953Srrs * others we will not have a tcb for anyway. 5784163953Srrs */ 5785163953Srrs goto bad; 5786163953Srrs } 5787163953Srrs /* validate SCTP checksum */ 5788188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5789188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5790188067Srrs m->m_pkthdr.len, 5791188067Srrs if_name(m->m_pkthdr.rcvif), 5792188067Srrs m->m_pkthdr.csum_flags); 5793188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5794188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5795188067Srrs goto sctp_skip_csum_4; 5796188067Srrs } 5797171990Srrs check = sh->checksum; /* save incoming checksum */ 5798179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 5799171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5800171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5801171990Srrs ) { 5802188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 5803171990Srrs goto sctp_skip_csum_4; 5804171990Srrs } 5805171990Srrs sh->checksum = 0; /* prepare for calc */ 5806188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5807203503Stuexen sh->checksum = check; 5808188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5809171990Srrs if (calc_check != check) { 5810171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5811171990Srrs calc_check, check, m, mlen, iphlen); 5812163953Srrs 5813171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5814171990Srrs offset - sizeof(*ch), 5815171990Srrs sh, ch, &inp, &net, 5816171990Srrs vrf_id); 5817179157Srrs if ((net) && (port)) { 5818179157Srrs if (net->port == 0) { 5819179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5820179157Srrs } 5821179157Srrs net->port = port; 5822179157Srrs } 5823171990Srrs if ((inp) && (stcb)) { 5824171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5825172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5826171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5827171990Srrs refcount_up = 1; 5828163953Srrs } 5829171990Srrs SCTP_STAT_INCR(sctps_badsum); 5830171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5831171990Srrs goto bad; 5832168299Srrs } 5833163953Srrssctp_skip_csum_4: 5834168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5835168124Srrs if (sh->dest_port == 0) { 5836168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5837168124Srrs goto bad; 5838168124Srrs } 5839163953Srrs /* 5840163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5841163953Srrs * IP/SCTP/first chunk header... 5842163953Srrs */ 5843163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5844168299Srrs sh, ch, &inp, &net, vrf_id); 5845179157Srrs if ((net) && (port)) { 5846179157Srrs if (net->port == 0) { 5847179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5848179157Srrs } 5849179157Srrs net->port = port; 5850179157Srrs } 5851163953Srrs /* inp's ref-count increased && stcb locked */ 5852163953Srrs if (inp == NULL) { 5853163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5854163953Srrs 5855163953Srrs SCTP_STAT_INCR(sctps_noport); 5856163953Srrs#ifdef ICMP_BANDLIM 5857163953Srrs /* 5858163953Srrs * we use the bandwidth limiting to protect against sending 5859163953Srrs * too many ABORTS all at once. In this case these count the 5860163953Srrs * same as an ICMP message. 5861163953Srrs */ 5862163953Srrs if (badport_bandlim(0) < 0) 5863163953Srrs goto bad; 5864163953Srrs#endif /* ICMP_BANDLIM */ 5865169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5866169420Srrs "Sending a ABORT from packet entry!\n"); 5867163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5868163953Srrs /* 5869163953Srrs * we do a trick here to get the INIT tag, dig in 5870163953Srrs * and get the tag from the INIT and put it in the 5871163953Srrs * common header. 5872163953Srrs */ 5873163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5874163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5875163953Srrs (uint8_t *) & chunk_buf); 5876163953Srrs if (init_chk != NULL) 5877163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5878163953Srrs } 5879165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5880179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5881165220Srrs goto bad; 5882165220Srrs } 5883165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5884165220Srrs goto bad; 5885165220Srrs } 5886165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5887179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5888163953Srrs goto bad; 5889163953Srrs } else if (stcb == NULL) { 5890163953Srrs refcount_up = 1; 5891163953Srrs } 5892171167Sgnn#ifdef IPSEC 5893163953Srrs /* 5894163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5895163953Srrs * idea, so I will leave it in place. 5896163953Srrs */ 5897163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5898197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5899163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5900163953Srrs goto bad; 5901163953Srrs } 5902163953Srrs#endif /* IPSEC */ 5903163953Srrs 5904163953Srrs /* 5905163953Srrs * common chunk processing 5906163953Srrs */ 5907163953Srrs length = ip->ip_len + iphlen; 5908163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5909163953Srrs 5910163953Srrs ecn_bits = ip->ip_tos; 5911163953Srrs 5912169655Srrs /* sa_ignore NO_NULL_CHK */ 5913163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5914179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5915163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5916163953Srrs if (m) { 5917163953Srrs sctp_m_freem(m); 5918163953Srrs } 5919163953Srrs if ((inp) && (refcount_up)) { 5920163953Srrs /* reduce ref-count */ 5921163953Srrs SCTP_INP_DECR_REF(inp); 5922163953Srrs } 5923163953Srrs return; 5924163953Srrsbad: 5925169420Srrs if (stcb) { 5926163953Srrs SCTP_TCB_UNLOCK(stcb); 5927169420Srrs } 5928163953Srrs if ((inp) && (refcount_up)) { 5929163953Srrs /* reduce ref-count */ 5930163953Srrs SCTP_INP_DECR_REF(inp); 5931163953Srrs } 5932163953Srrs if (m) { 5933163953Srrs sctp_m_freem(m); 5934163953Srrs } 5935163953Srrs return; 5936163953Srrs} 5937179157Srrsvoid 5938179157Srrssctp_input(i_pak, off) 5939179157Srrs struct mbuf *i_pak; 5940179157Srrs int off; 5941179157Srrs{ 5942179157Srrs sctp_input_with_port(i_pak, off, 0); 5943179157Srrs} 5944