sctp_input.c revision 211944
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 211944 2010-08-28 17:59:51Z tuexen $"); 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 */ 621211944Stuexen if ((stcb->asoc.sctp_cmt_on_off == 1) && 622211944Stuexen (stcb->asoc.sctp_cmt_pf > 0) && 623211944Stuexen ((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; 630211944Stuexen net->cwnd = net->mtu * stcb->asoc.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; 2726211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2727163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2728163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2729163953Srrs inp->inp_starting_point_for_iterator = NULL; 2730163953Srrs /* 2731163953Srrs * copy in the authentication parameters from the 2732163953Srrs * original endpoint 2733163953Srrs */ 2734163953Srrs if (inp->sctp_ep.local_hmacs) 2735163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2736163953Srrs inp->sctp_ep.local_hmacs = 2737163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2738163953Srrs if (inp->sctp_ep.local_auth_chunks) 2739163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2740163953Srrs inp->sctp_ep.local_auth_chunks = 2741163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2742163953Srrs 2743163953Srrs /* 2744163953Srrs * Now we must move it from one hash table to 2745163953Srrs * another and get the tcb in the right place. 2746163953Srrs */ 2747207924Srrs 2748207924Srrs /* 2749207924Srrs * This is where the one-2-one socket is put into 2750207924Srrs * the accept state waiting for the accept! 2751207924Srrs */ 2752207924Srrs if (*stcb) { 2753207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2754207924Srrs } 2755163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2756170056Srrs 2757170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2758170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2759170056Srrs 2760175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2761175845Srwatson 0); 2762170056Srrs SCTP_TCB_LOCK((*stcb)); 2763170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2764163953Srrs 2765170056Srrs 2766166086Srrs /* 2767166086Srrs * now we must check to see if we were aborted while 2768166086Srrs * the move was going on and the lock/unlock 2769166086Srrs * happened. 2770166086Srrs */ 2771166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2772166086Srrs /* 2773166086Srrs * yep it was, we leave the assoc attached 2774166086Srrs * to the socket since the sctp_inpcb_free() 2775166086Srrs * call will send an abort for us. 2776166086Srrs */ 2777166086Srrs SCTP_INP_DECR_REF(inp); 2778166086Srrs return (NULL); 2779166086Srrs } 2780166086Srrs SCTP_INP_DECR_REF(inp); 2781163953Srrs /* Switch over to the new guy */ 2782163953Srrs *inp_p = inp; 2783172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2784163953Srrs 2785166086Srrs /* 2786166086Srrs * Pull it from the incomplete queue and wake the 2787166086Srrs * guy 2788166086Srrs */ 2789172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2790172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2791172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2792172090Srrs SCTP_SOCKET_LOCK(so, 1); 2793172090Srrs#endif 2794166086Srrs soisconnected(so); 2795172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2796172118Srrs SCTP_TCB_LOCK((*stcb)); 2797172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2798172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2799172090Srrs#endif 2800163953Srrs return (m); 2801163953Srrs } 2802163953Srrs } 2803163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2804172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2805163953Srrs } 2806163953Srrs return (m); 2807163953Srrs} 2808163953Srrs 2809163953Srrsstatic void 2810163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2811163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2812163953Srrs{ 2813163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2814163953Srrs struct sctp_association *asoc; 2815163953Srrs 2816169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2817169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2818163953Srrs if (stcb == NULL) 2819163953Srrs return; 2820163953Srrs 2821163953Srrs asoc = &stcb->asoc; 2822163953Srrs 2823163953Srrs sctp_stop_all_cookie_timers(stcb); 2824163953Srrs /* process according to association state */ 2825163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2826163953Srrs /* state change only needed when I am in right state */ 2827169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2828171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2829163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2830163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2831163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2832163953Srrs 2833163953Srrs } 2834163953Srrs /* update RTO */ 2835163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2836163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2837163953Srrs if (asoc->overall_error_count == 0) { 2838163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2839171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2840163953Srrs } 2841169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2842172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2843163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2844163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2845172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2846172090Srrs struct socket *so; 2847172090Srrs 2848172090Srrs#endif 2849163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2850172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2851172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2852172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2853172090Srrs SCTP_TCB_UNLOCK(stcb); 2854172090Srrs SCTP_SOCKET_LOCK(so, 1); 2855172090Srrs SCTP_TCB_LOCK(stcb); 2856172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2857172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2858172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2859172090Srrs return; 2860172090Srrs } 2861172090Srrs#endif 2862172090Srrs soisconnected(stcb->sctp_socket); 2863172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2864172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2865172090Srrs#endif 2866163953Srrs } 2867163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2868163953Srrs stcb, net); 2869163953Srrs /* 2870163953Srrs * since we did not send a HB make sure we don't double 2871163953Srrs * things 2872163953Srrs */ 2873163953Srrs net->hb_responded = 1; 2874163953Srrs 2875163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2876163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2877163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2878163953Srrs stcb->sctp_ep, stcb, NULL); 2879163953Srrs } 2880163953Srrs /* 2881171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2882171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2883171572Srrs * in flight) 2884163953Srrs */ 2885163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2886163953Srrs (stcb->asoc.peer_supports_asconf) && 2887163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2888171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2889163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2890163953Srrs stcb->sctp_ep, stcb, 2891163953Srrs stcb->asoc.primary_destination); 2892171572Srrs#else 2893172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2894172190Srrs SCTP_ADDR_NOT_LOCKED); 2895171572Srrs#endif 2896163953Srrs } 2897163953Srrs } 2898163953Srrs /* Toss the cookie if I can */ 2899163953Srrs sctp_toss_old_cookies(stcb, asoc); 2900163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2901163953Srrs /* Restart the timer if we have pending data */ 2902163953Srrs struct sctp_tmit_chunk *chk; 2903163953Srrs 2904163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2905163953Srrs if (chk) { 2906163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2907163953Srrs stcb, chk->whoTo); 2908163953Srrs } 2909163953Srrs } 2910163953Srrs} 2911163953Srrs 2912163953Srrsstatic void 2913163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2914163953Srrs struct sctp_tcb *stcb) 2915163953Srrs{ 2916163953Srrs struct sctp_nets *net; 2917163953Srrs struct sctp_tmit_chunk *lchk; 2918163953Srrs uint32_t tsn; 2919163953Srrs 2920163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2921163953Srrs return; 2922163953Srrs } 2923163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2924163953Srrs tsn = ntohl(cp->tsn); 2925163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2926163953Srrs /* Also we make sure we disable the nonce_wait */ 2927163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2928163953Srrs if (lchk == NULL) { 2929163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2930163953Srrs } else { 2931163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2932163953Srrs } 2933163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2934163953Srrs stcb->asoc.nonce_sum_check = 0; 2935163953Srrs 2936163953Srrs /* Find where it was sent, if possible */ 2937163953Srrs net = NULL; 2938163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2939163953Srrs while (lchk) { 2940163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2941163953Srrs net = lchk->whoTo; 2942163953Srrs break; 2943163953Srrs } 2944163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2945163953Srrs break; 2946163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2947163953Srrs } 2948163953Srrs if (net == NULL) 2949163953Srrs /* default is we use the primary */ 2950163953Srrs net = stcb->asoc.primary_destination; 2951163953Srrs 2952163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2953163953Srrs /* 2954171440Srrs * JRS - Use the congestion control given in the pluggable 2955171440Srrs * CC module 2956171440Srrs */ 2957171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2958171440Srrs /* 2959163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2960163953Srrs * the next sending seq i.e. the resync_tsn. 2961163953Srrs */ 2962163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2963163953Srrs } 2964163953Srrs /* 2965163953Srrs * We always send a CWR this way if our previous one was lost our 2966163953Srrs * peer will get an update, or if it is not time again to reduce we 2967163953Srrs * still get the cwr to the peer. 2968163953Srrs */ 2969163953Srrs sctp_send_cwr(stcb, net, tsn); 2970163953Srrs} 2971163953Srrs 2972163953Srrsstatic void 2973163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2974163953Srrs{ 2975163953Srrs /* 2976163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2977163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2978163953Srrs * If so remove it. 2979163953Srrs */ 2980163953Srrs struct sctp_tmit_chunk *chk; 2981163953Srrs struct sctp_ecne_chunk *ecne; 2982163953Srrs 2983163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2984163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2985163953Srrs continue; 2986163953Srrs } 2987163953Srrs /* 2988163953Srrs * Look for and remove if it is the right TSN. Since there 2989163953Srrs * is only ONE ECNE on the control queue at any one time we 2990163953Srrs * don't need to worry about more than one! 2991163953Srrs */ 2992163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2993163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2994163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2995163953Srrs /* this covers this ECNE, we can remove it */ 2996163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2997163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2998163953Srrs sctp_next); 2999163953Srrs if (chk->data) { 3000163953Srrs sctp_m_freem(chk->data); 3001163953Srrs chk->data = NULL; 3002163953Srrs } 3003163953Srrs stcb->asoc.ctrl_queue_cnt--; 3004163953Srrs sctp_free_a_chunk(stcb, chk); 3005163953Srrs break; 3006163953Srrs } 3007163953Srrs } 3008163953Srrs} 3009163953Srrs 3010163953Srrsstatic void 3011163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3012163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3013163953Srrs{ 3014163953Srrs struct sctp_association *asoc; 3015163953Srrs 3016172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3017172090Srrs struct socket *so; 3018172090Srrs 3019172090Srrs#endif 3020172090Srrs 3021169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3022169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3023163953Srrs if (stcb == NULL) 3024163953Srrs return; 3025163953Srrs 3026163953Srrs asoc = &stcb->asoc; 3027163953Srrs /* process according to association state */ 3028163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3029163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3030171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3031171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3032163953Srrs SCTP_TCB_UNLOCK(stcb); 3033163953Srrs return; 3034163953Srrs } 3035163953Srrs /* notify upper layer protocol */ 3036163953Srrs if (stcb->sctp_socket) { 3037172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3038163953Srrs /* are the queues empty? they should be */ 3039163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3040163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3041163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 3042172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3043163953Srrs } 3044163953Srrs } 3045163953Srrs /* stop the timer */ 3046178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3047163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3048163953Srrs /* free the TCB */ 3049171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3050171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3051172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3052172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3053172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3054172090Srrs SCTP_TCB_UNLOCK(stcb); 3055172090Srrs SCTP_SOCKET_LOCK(so, 1); 3056172090Srrs SCTP_TCB_LOCK(stcb); 3057172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3058172090Srrs#endif 3059171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3060172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3061172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3062172090Srrs#endif 3063163953Srrs return; 3064163953Srrs} 3065163953Srrs 3066163953Srrsstatic int 3067163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3068163953Srrs struct sctp_nets *net, uint8_t flg) 3069163953Srrs{ 3070163953Srrs switch (desc->chunk_type) { 3071211944Stuexen case SCTP_DATA: 3072163953Srrs /* find the tsn to resend (possibly */ 3073163953Srrs { 3074163953Srrs uint32_t tsn; 3075163953Srrs struct sctp_tmit_chunk *tp1; 3076163953Srrs 3077163953Srrs tsn = ntohl(desc->tsn_ifany); 3078163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3079163953Srrs while (tp1) { 3080163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3081163953Srrs /* found it */ 3082163953Srrs break; 3083163953Srrs } 3084163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 3085163953Srrs MAX_TSN)) { 3086163953Srrs /* not found */ 3087163953Srrs tp1 = NULL; 3088163953Srrs break; 3089163953Srrs } 3090163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3091163953Srrs } 3092163953Srrs if (tp1 == NULL) { 3093163953Srrs /* 3094163953Srrs * Do it the other way , aka without paying 3095163953Srrs * attention to queue seq order. 3096163953Srrs */ 3097163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3098163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3099163953Srrs while (tp1) { 3100163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3101163953Srrs /* found it */ 3102163953Srrs break; 3103163953Srrs } 3104163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3105163953Srrs } 3106163953Srrs } 3107163953Srrs if (tp1 == NULL) { 3108163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3109163953Srrs } 3110163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3111163953Srrs uint8_t *ddp; 3112163953Srrs 3113163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3114163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3115163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3116163953Srrs return (0); 3117163953Srrs } 3118163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3119163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3120163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3121163953Srrs return (0); 3122163953Srrs } 3123163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3124163953Srrs sizeof(struct sctp_data_chunk)); 3125163953Srrs { 3126163953Srrs unsigned int iii; 3127163953Srrs 3128163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3129163953Srrs iii++) { 3130163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3131163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3132163953Srrs return (-1); 3133163953Srrs } 3134163953Srrs } 3135163953Srrs } 3136163953Srrs /* 3137163953Srrs * We zero out the nonce so resync not 3138163953Srrs * needed 3139163953Srrs */ 3140163953Srrs tp1->rec.data.ect_nonce = 0; 3141163953Srrs 3142163953Srrs if (tp1->do_rtt) { 3143163953Srrs /* 3144163953Srrs * this guy had a RTO calculation 3145163953Srrs * pending on it, cancel it 3146163953Srrs */ 3147163953Srrs tp1->do_rtt = 0; 3148163953Srrs } 3149163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3150163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3151163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3152163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 3153163953Srrs /* 3154163953Srrs * mark it as if we were doing a FR, since 3155163953Srrs * we will be getting gap ack reports behind 3156163953Srrs * the info from the router. 3157163953Srrs */ 3158163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3159163953Srrs /* 3160163953Srrs * mark the tsn with what sequences can 3161163953Srrs * cause a new FR. 3162163953Srrs */ 3163163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3164163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3165163953Srrs } else { 3166163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3167163953Srrs } 3168163953Srrs 3169163953Srrs /* restart the timer */ 3170163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3171171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3172163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3173163953Srrs stcb, tp1->whoTo); 3174163953Srrs 3175163953Srrs /* fix counts and things */ 3176179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3177170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3178170744Srrs tp1->whoTo->flight_size, 3179170744Srrs tp1->book_size, 3180170744Srrs (uintptr_t) stcb, 3181170744Srrs tp1->rec.data.TSN_seq); 3182170744Srrs } 3183190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3184190689Srrs sctp_flight_size_decrease(tp1); 3185190689Srrs sctp_total_flight_decrease(stcb, tp1); 3186190689Srrs } 3187163953Srrs } { 3188163953Srrs /* audit code */ 3189163953Srrs unsigned int audit; 3190163953Srrs 3191163953Srrs audit = 0; 3192163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3193163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3194163953Srrs audit++; 3195163953Srrs } 3196163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3197163953Srrs sctp_next) { 3198163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3199163953Srrs audit++; 3200163953Srrs } 3201163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3202169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3203163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3204163953Srrs#ifndef SCTP_AUDITING_ENABLED 3205163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3206163953Srrs#endif 3207163953Srrs } 3208163953Srrs } 3209163953Srrs } 3210163953Srrs break; 3211163953Srrs case SCTP_ASCONF: 3212163953Srrs { 3213163953Srrs struct sctp_tmit_chunk *asconf; 3214163953Srrs 3215163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3216163953Srrs sctp_next) { 3217163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3218163953Srrs break; 3219163953Srrs } 3220163953Srrs } 3221163953Srrs if (asconf) { 3222163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3223163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3224163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3225163953Srrs asconf->snd_count--; 3226163953Srrs } 3227163953Srrs } 3228163953Srrs break; 3229163953Srrs case SCTP_INITIATION: 3230163953Srrs /* resend the INIT */ 3231163953Srrs stcb->asoc.dropped_special_cnt++; 3232163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3233163953Srrs /* 3234163953Srrs * If we can get it in, in a few attempts we do 3235163953Srrs * this, otherwise we let the timer fire. 3236163953Srrs */ 3237163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3238171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3239172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3240163953Srrs } 3241163953Srrs break; 3242163953Srrs case SCTP_SELECTIVE_ACK: 3243206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3244163953Srrs /* resend the sack */ 3245163953Srrs sctp_send_sack(stcb); 3246163953Srrs break; 3247163953Srrs case SCTP_HEARTBEAT_REQUEST: 3248163953Srrs /* resend a demand HB */ 3249171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3250171440Srrs /* 3251171440Srrs * Only retransmit if we KNOW we wont destroy the 3252171440Srrs * tcb 3253171440Srrs */ 3254171440Srrs (void)sctp_send_hb(stcb, 1, net); 3255171440Srrs } 3256163953Srrs break; 3257163953Srrs case SCTP_SHUTDOWN: 3258163953Srrs sctp_send_shutdown(stcb, net); 3259163953Srrs break; 3260163953Srrs case SCTP_SHUTDOWN_ACK: 3261163953Srrs sctp_send_shutdown_ack(stcb, net); 3262163953Srrs break; 3263163953Srrs case SCTP_COOKIE_ECHO: 3264163953Srrs { 3265163953Srrs struct sctp_tmit_chunk *cookie; 3266163953Srrs 3267163953Srrs cookie = NULL; 3268163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3269163953Srrs sctp_next) { 3270163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3271163953Srrs break; 3272163953Srrs } 3273163953Srrs } 3274163953Srrs if (cookie) { 3275163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3276163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3277163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3278163953Srrs sctp_stop_all_cookie_timers(stcb); 3279163953Srrs } 3280163953Srrs } 3281163953Srrs break; 3282163953Srrs case SCTP_COOKIE_ACK: 3283163953Srrs sctp_send_cookie_ack(stcb); 3284163953Srrs break; 3285163953Srrs case SCTP_ASCONF_ACK: 3286163953Srrs /* resend last asconf ack */ 3287171990Srrs sctp_send_asconf_ack(stcb); 3288163953Srrs break; 3289163953Srrs case SCTP_FORWARD_CUM_TSN: 3290163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3291163953Srrs break; 3292163953Srrs /* can't do anything with these */ 3293163953Srrs case SCTP_PACKET_DROPPED: 3294163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3295163953Srrs case SCTP_HEARTBEAT_ACK: 3296163953Srrs case SCTP_ABORT_ASSOCIATION: 3297163953Srrs case SCTP_OPERATION_ERROR: 3298163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3299163953Srrs case SCTP_ECN_ECHO: 3300163953Srrs case SCTP_ECN_CWR: 3301163953Srrs default: 3302163953Srrs break; 3303163953Srrs } 3304163953Srrs return (0); 3305163953Srrs} 3306163953Srrs 3307163953Srrsvoid 3308163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3309163953Srrs{ 3310163953Srrs int i; 3311163953Srrs uint16_t temp; 3312163953Srrs 3313163953Srrs /* 3314163953Srrs * We set things to 0xffff since this is the last delivered sequence 3315163953Srrs * and we will be sending in 0 after the reset. 3316163953Srrs */ 3317163953Srrs 3318163953Srrs if (number_entries) { 3319163953Srrs for (i = 0; i < number_entries; i++) { 3320163953Srrs temp = ntohs(list[i]); 3321163953Srrs if (temp >= stcb->asoc.streamincnt) { 3322163953Srrs continue; 3323163953Srrs } 3324163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3325163953Srrs } 3326163953Srrs } else { 3327163953Srrs list = NULL; 3328163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3329163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3330163953Srrs } 3331163953Srrs } 3332172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3333163953Srrs} 3334163953Srrs 3335163953Srrsstatic void 3336163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3337163953Srrs{ 3338163953Srrs int i; 3339163953Srrs 3340163953Srrs if (number_entries == 0) { 3341163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3342163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3343163953Srrs } 3344163953Srrs } else if (number_entries) { 3345163953Srrs for (i = 0; i < number_entries; i++) { 3346163953Srrs uint16_t temp; 3347163953Srrs 3348163953Srrs temp = ntohs(list[i]); 3349163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3350163953Srrs /* no such stream */ 3351163953Srrs continue; 3352163953Srrs } 3353163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3354163953Srrs } 3355163953Srrs } 3356172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3357163953Srrs} 3358163953Srrs 3359163953Srrs 3360163953Srrsstruct sctp_stream_reset_out_request * 3361163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3362163953Srrs{ 3363163953Srrs struct sctp_association *asoc; 3364163953Srrs struct sctp_stream_reset_out_req *req; 3365163953Srrs struct sctp_stream_reset_out_request *r; 3366163953Srrs struct sctp_tmit_chunk *chk; 3367163953Srrs int len, clen; 3368163953Srrs 3369163953Srrs asoc = &stcb->asoc; 3370163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3371169208Srrs asoc->stream_reset_outstanding = 0; 3372163953Srrs return (NULL); 3373163953Srrs } 3374163953Srrs if (stcb->asoc.str_reset == NULL) { 3375169208Srrs asoc->stream_reset_outstanding = 0; 3376163953Srrs return (NULL); 3377163953Srrs } 3378163953Srrs chk = stcb->asoc.str_reset; 3379163953Srrs if (chk->data == NULL) { 3380163953Srrs return (NULL); 3381163953Srrs } 3382163953Srrs if (bchk) { 3383163953Srrs /* he wants a copy of the chk pointer */ 3384163953Srrs *bchk = chk; 3385163953Srrs } 3386163953Srrs clen = chk->send_size; 3387163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3388163953Srrs r = &req->sr_req; 3389163953Srrs if (ntohl(r->request_seq) == seq) { 3390163953Srrs /* found it */ 3391163953Srrs return (r); 3392163953Srrs } 3393163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3394163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3395163953Srrs /* move to the next one, there can only be a max of two */ 3396163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3397163953Srrs if (ntohl(r->request_seq) == seq) { 3398163953Srrs return (r); 3399163953Srrs } 3400163953Srrs } 3401163953Srrs /* that seq is not here */ 3402163953Srrs return (NULL); 3403163953Srrs} 3404163953Srrs 3405163953Srrsstatic void 3406163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3407163953Srrs{ 3408163953Srrs struct sctp_association *asoc; 3409163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3410163953Srrs 3411163953Srrs if (stcb->asoc.str_reset == NULL) { 3412163953Srrs return; 3413163953Srrs } 3414166675Srrs asoc = &stcb->asoc; 3415166675Srrs 3416171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3417163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3418163953Srrs chk, 3419163953Srrs sctp_next); 3420163953Srrs if (chk->data) { 3421163953Srrs sctp_m_freem(chk->data); 3422163953Srrs chk->data = NULL; 3423163953Srrs } 3424163953Srrs asoc->ctrl_queue_cnt--; 3425163953Srrs sctp_free_a_chunk(stcb, chk); 3426172156Srrs /* sa_ignore NO_NULL_CHK */ 3427163953Srrs stcb->asoc.str_reset = NULL; 3428163953Srrs} 3429163953Srrs 3430163953Srrs 3431163953Srrsstatic int 3432163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3433163953Srrs uint32_t seq, uint32_t action, 3434164139Srrs struct sctp_stream_reset_response *respin) 3435163953Srrs{ 3436163953Srrs uint16_t type; 3437163953Srrs int lparm_len; 3438163953Srrs struct sctp_association *asoc = &stcb->asoc; 3439163953Srrs struct sctp_tmit_chunk *chk; 3440163953Srrs struct sctp_stream_reset_out_request *srparam; 3441163953Srrs int number_entries; 3442163953Srrs 3443163953Srrs if (asoc->stream_reset_outstanding == 0) { 3444163953Srrs /* duplicate */ 3445163953Srrs return (0); 3446163953Srrs } 3447163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3448163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3449163953Srrs if (srparam) { 3450163953Srrs stcb->asoc.str_reset_seq_out++; 3451163953Srrs type = ntohs(srparam->ph.param_type); 3452163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3453163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3454164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3455163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3456163953Srrs if (asoc->stream_reset_outstanding) 3457163953Srrs asoc->stream_reset_outstanding--; 3458163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3459163953Srrs /* do it */ 3460163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3461163953Srrs } else { 3462172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3463163953Srrs } 3464163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3465163953Srrs /* Answered my request */ 3466164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3467163953Srrs if (asoc->stream_reset_outstanding) 3468163953Srrs asoc->stream_reset_outstanding--; 3469163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3470172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3471163953Srrs } 3472188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3473188854Srrs /* Ok we now may have more streams */ 3474189121Srrs if (asoc->stream_reset_outstanding) 3475189121Srrs asoc->stream_reset_outstanding--; 3476188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3477188854Srrs /* Put the new streams into effect */ 3478188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3479188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3480188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3481188854Srrs } else { 3482188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3483188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3484188854Srrs } 3485163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3486163953Srrs /** 3487163953Srrs * a) Adopt the new in tsn. 3488163953Srrs * b) reset the map 3489163953Srrs * c) Adopt the new out-tsn 3490163953Srrs */ 3491163953Srrs struct sctp_stream_reset_response_tsn *resp; 3492163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3493163953Srrs int abort_flag = 0; 3494163953Srrs 3495163953Srrs if (respin == NULL) { 3496163953Srrs /* huh ? */ 3497163953Srrs return (0); 3498163953Srrs } 3499163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3500163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3501163953Srrs asoc->stream_reset_outstanding--; 3502163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3503163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3504163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3505170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3506163953Srrs if (abort_flag) { 3507163953Srrs return (1); 3508163953Srrs } 3509163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3510179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3511179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3512179783Srrs } 3513180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3514163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3515163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3516185694Srrs 3517205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3518206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3519205627Srrs 3520163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3521163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3522163953Srrs 3523163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3524163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3525163953Srrs 3526163953Srrs } 3527163953Srrs } 3528163953Srrs /* get rid of the request and get the request flags */ 3529163953Srrs if (asoc->stream_reset_outstanding == 0) { 3530163953Srrs sctp_clean_up_stream_reset(stcb); 3531163953Srrs } 3532163953Srrs } 3533163953Srrs } 3534163953Srrs return (0); 3535163953Srrs} 3536163953Srrs 3537163953Srrsstatic void 3538163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3539163953Srrs struct sctp_tmit_chunk *chk, 3540170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3541163953Srrs{ 3542163953Srrs uint32_t seq; 3543163953Srrs int len, i; 3544163953Srrs int number_entries; 3545163953Srrs uint16_t temp; 3546163953Srrs 3547163953Srrs /* 3548163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3549163953Srrs * seq_in is right. 3550163953Srrs */ 3551163953Srrs struct sctp_association *asoc = &stcb->asoc; 3552163953Srrs 3553163953Srrs seq = ntohl(req->request_seq); 3554163953Srrs if (asoc->str_reset_seq_in == seq) { 3555170992Srrs if (trunc) { 3556170992Srrs /* Can't do it, since they exceeded our buffer size */ 3557170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3558170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3559170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3560170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3561163953Srrs len = ntohs(req->ph.param_length); 3562163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3563163953Srrs for (i = 0; i < number_entries; i++) { 3564163953Srrs temp = ntohs(req->list_of_streams[i]); 3565163953Srrs req->list_of_streams[i] = temp; 3566163953Srrs } 3567163953Srrs /* move the reset action back one */ 3568163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3569163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3570163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3571163953Srrs asoc->str_reset_seq_out, 3572163953Srrs seq, (asoc->sending_seq - 1)); 3573163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3574163953Srrs asoc->str_reset = chk; 3575163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3576163953Srrs stcb->asoc.stream_reset_outstanding++; 3577163953Srrs } else { 3578163953Srrs /* Can't do it, since we have sent one out */ 3579163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3580163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3581163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3582163953Srrs } 3583163953Srrs asoc->str_reset_seq_in++; 3584163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3585163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3586163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3587163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3588163953Srrs } else { 3589163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3590163953Srrs } 3591163953Srrs} 3592163953Srrs 3593163953Srrsstatic int 3594163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3595163953Srrs struct sctp_tmit_chunk *chk, 3596163953Srrs struct sctp_stream_reset_tsn_request *req) 3597163953Srrs{ 3598163953Srrs /* reset all in and out and update the tsn */ 3599163953Srrs /* 3600163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3601163953Srrs * and set cum-ack to it. Also process this selected number as a 3602163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3603163953Srrs */ 3604163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3605163953Srrs struct sctp_association *asoc = &stcb->asoc; 3606163953Srrs int abort_flag = 0; 3607163953Srrs uint32_t seq; 3608163953Srrs 3609163953Srrs seq = ntohl(req->request_seq); 3610163953Srrs if (asoc->str_reset_seq_in == seq) { 3611163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3612163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3613163953Srrs fwdtsn.ch.chunk_flags = 0; 3614163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3615170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3616163953Srrs if (abort_flag) { 3617163953Srrs return (1); 3618163953Srrs } 3619163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3620179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3621179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3622179783Srrs } 3623180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3624163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3625163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3626205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3627206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3628163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3629163953Srrs /* save off historical data for retrans */ 3630163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3631163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3632163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3633163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3634163953Srrs 3635163953Srrs sctp_add_stream_reset_result_tsn(chk, 3636163953Srrs ntohl(req->request_seq), 3637163953Srrs SCTP_STREAM_RESET_PERFORMED, 3638163953Srrs stcb->asoc.sending_seq, 3639163953Srrs stcb->asoc.mapping_array_base_tsn); 3640163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3641163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3642163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3643163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3644163953Srrs 3645163953Srrs asoc->str_reset_seq_in++; 3646163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3647163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3648163953Srrs stcb->asoc.last_sending_seq[0], 3649163953Srrs stcb->asoc.last_base_tsnsent[0] 3650163953Srrs ); 3651163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3652163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3653163953Srrs stcb->asoc.last_sending_seq[1], 3654163953Srrs stcb->asoc.last_base_tsnsent[1] 3655163953Srrs ); 3656163953Srrs } else { 3657163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3658163953Srrs } 3659163953Srrs return (0); 3660163953Srrs} 3661163953Srrs 3662163953Srrsstatic void 3663163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3664163953Srrs struct sctp_tmit_chunk *chk, 3665170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3666163953Srrs{ 3667163953Srrs uint32_t seq, tsn; 3668163953Srrs int number_entries, len; 3669163953Srrs struct sctp_association *asoc = &stcb->asoc; 3670163953Srrs 3671163953Srrs seq = ntohl(req->request_seq); 3672163953Srrs 3673163953Srrs /* now if its not a duplicate we process it */ 3674163953Srrs if (asoc->str_reset_seq_in == seq) { 3675163953Srrs len = ntohs(req->ph.param_length); 3676163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3677163953Srrs /* 3678163953Srrs * the sender is resetting, handle the list issue.. we must 3679163953Srrs * a) verify if we can do the reset, if so no problem b) If 3680163953Srrs * we can't do the reset we must copy the request. c) queue 3681163953Srrs * it, and setup the data in processor to trigger it off 3682163953Srrs * when needed and dequeue all the queued data. 3683163953Srrs */ 3684163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3685163953Srrs 3686163953Srrs /* move the reset action back one */ 3687163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3688170992Srrs if (trunc) { 3689170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3690170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3691170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3692163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3693163953Srrs /* we can do it now */ 3694163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3695163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3696163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3697163953Srrs } else { 3698163953Srrs /* 3699163953Srrs * we must queue it up and thus wait for the TSN's 3700163953Srrs * to arrive that are at or before tsn 3701163953Srrs */ 3702163953Srrs struct sctp_stream_reset_list *liste; 3703163953Srrs int siz; 3704163953Srrs 3705163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3706163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3707170091Srrs siz, SCTP_M_STRESET); 3708163953Srrs if (liste == NULL) { 3709163953Srrs /* gak out of memory */ 3710163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3711163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3712163953Srrs return; 3713163953Srrs } 3714163953Srrs liste->tsn = tsn; 3715163953Srrs liste->number_entries = number_entries; 3716163953Srrs memcpy(&liste->req, req, 3717163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3718163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3719163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3720163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3721163953Srrs } 3722163953Srrs asoc->str_reset_seq_in++; 3723163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3724163953Srrs /* 3725163953Srrs * one seq back, just echo back last action since my 3726163953Srrs * response was lost. 3727163953Srrs */ 3728163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3729163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3730163953Srrs /* 3731163953Srrs * two seq back, just echo back last action since my 3732163953Srrs * response was lost. 3733163953Srrs */ 3734163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3735163953Srrs } else { 3736163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3737163953Srrs } 3738163953Srrs} 3739163953Srrs 3740188854Srrsstatic void 3741188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3742188854Srrs struct sctp_stream_reset_add_strm *str_add) 3743188854Srrs{ 3744188854Srrs /* 3745188854Srrs * Peer is requesting to add more streams. If its within our 3746188854Srrs * max-streams we will allow it. 3747188854Srrs */ 3748188854Srrs uint16_t num_stream, i; 3749188854Srrs uint32_t seq; 3750189121Srrs struct sctp_association *asoc = &stcb->asoc; 3751189121Srrs struct sctp_queued_to_read *ctl; 3752188854Srrs 3753188854Srrs /* Get the number. */ 3754188854Srrs seq = ntohl(str_add->request_seq); 3755188854Srrs num_stream = ntohs(str_add->number_of_streams); 3756188854Srrs /* Now what would be the new total? */ 3757189121Srrs if (asoc->str_reset_seq_in == seq) { 3758189121Srrs num_stream += stcb->asoc.streamincnt; 3759189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3760189121Srrs /* We must reject it they ask for to many */ 3761189121Srrs denied: 3762189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3763189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3764189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3765189121Srrs } else { 3766189121Srrs /* Ok, we can do that :-) */ 3767189121Srrs struct sctp_stream_in *oldstrm; 3768189121Srrs 3769189121Srrs /* save off the old */ 3770189121Srrs oldstrm = stcb->asoc.strmin; 3771189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3772189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3773189121Srrs SCTP_M_STRMI); 3774189121Srrs if (stcb->asoc.strmin == NULL) { 3775189121Srrs stcb->asoc.strmin = oldstrm; 3776189121Srrs goto denied; 3777189121Srrs } 3778189121Srrs /* copy off the old data */ 3779189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3780189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3781189121Srrs stcb->asoc.strmin[i].stream_no = i; 3782189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3783189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3784189121Srrs /* now anything on those queues? */ 3785189121Srrs while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) { 3786189121Srrs ctl = TAILQ_FIRST(&oldstrm[i].inqueue); 3787189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3788189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3789189121Srrs } 3790189121Srrs } 3791189121Srrs /* Init the new streams */ 3792189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3793189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3794189121Srrs stcb->asoc.strmin[i].stream_no = i; 3795189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3796189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3797189121Srrs } 3798189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3799189121Srrs /* update the size */ 3800189121Srrs stcb->asoc.streamincnt = num_stream; 3801189121Srrs /* Send the ack */ 3802189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3803189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3804189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3805189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3806189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3807189121Srrs } 3808189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3809189121Srrs /* 3810189121Srrs * one seq back, just echo back last action since my 3811189121Srrs * response was lost. 3812189121Srrs */ 3813189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3814189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3815189121Srrs /* 3816189121Srrs * two seq back, just echo back last action since my 3817189121Srrs * response was lost. 3818189121Srrs */ 3819189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3820188854Srrs } else { 3821189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3822188854Srrs 3823188854Srrs } 3824188854Srrs} 3825188854Srrs 3826170992Srrs#ifdef __GNUC__ 3827170992Srrs__attribute__((noinline)) 3828170992Srrs#endif 3829170992Srrs static int 3830170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3831170992Srrs struct sctp_stream_reset_out_req *sr_req) 3832163953Srrs{ 3833163953Srrs int chk_length, param_len, ptype; 3834170992Srrs struct sctp_paramhdr pstore; 3835170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3836170992Srrs 3837163953Srrs uint32_t seq; 3838163953Srrs int num_req = 0; 3839170992Srrs int trunc = 0; 3840163953Srrs struct sctp_tmit_chunk *chk; 3841163953Srrs struct sctp_chunkhdr *ch; 3842163953Srrs struct sctp_paramhdr *ph; 3843166675Srrs int ret_code = 0; 3844166675Srrs int num_param = 0; 3845163953Srrs 3846163953Srrs /* now it may be a reset or a reset-response */ 3847163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3848163953Srrs 3849163953Srrs /* setup for adding the response */ 3850163953Srrs sctp_alloc_a_chunk(stcb, chk); 3851163953Srrs if (chk == NULL) { 3852163953Srrs return (ret_code); 3853163953Srrs } 3854163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3855169208Srrs chk->rec.chunk_id.can_take_data = 0; 3856163953Srrs chk->asoc = &stcb->asoc; 3857163953Srrs chk->no_fr_allowed = 0; 3858163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3859166023Srrs chk->book_size_scale = 0; 3860165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3861163953Srrs if (chk->data == NULL) { 3862163953Srrsstrres_nochunk: 3863163953Srrs if (chk->data) { 3864163953Srrs sctp_m_freem(chk->data); 3865163953Srrs chk->data = NULL; 3866163953Srrs } 3867163953Srrs sctp_free_a_chunk(stcb, chk); 3868163953Srrs return (ret_code); 3869163953Srrs } 3870165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3871163953Srrs 3872163953Srrs /* setup chunk parameters */ 3873163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3874163953Srrs chk->snd_count = 0; 3875163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3876163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3877163953Srrs 3878163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3879163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3880163953Srrs ch->chunk_flags = 0; 3881163953Srrs ch->chunk_length = htons(chk->send_size); 3882165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3883170992Srrs offset += sizeof(struct sctp_chunkhdr); 3884163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3885170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3886170992Srrs if (ph == NULL) 3887170992Srrs break; 3888163953Srrs param_len = ntohs(ph->param_length); 3889163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3890163953Srrs /* bad param */ 3891163953Srrs break; 3892163953Srrs } 3893170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3894170992Srrs (uint8_t *) & cstore); 3895163953Srrs ptype = ntohs(ph->param_type); 3896163953Srrs num_param++; 3897170994Srrs if (param_len > (int)sizeof(cstore)) { 3898170992Srrs trunc = 1; 3899170992Srrs } else { 3900170992Srrs trunc = 0; 3901170992Srrs } 3902170992Srrs 3903163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3904163953Srrs /* hit the max of parameters already sorry.. */ 3905163953Srrs break; 3906163953Srrs } 3907163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3908163953Srrs struct sctp_stream_reset_out_request *req_out; 3909163953Srrs 3910163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3911163953Srrs num_req++; 3912163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3913163953Srrs seq = ntohl(req_out->response_seq); 3914163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3915163953Srrs /* implicit ack */ 3916169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3917163953Srrs } 3918163953Srrs } 3919170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3920188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3921188854Srrs struct sctp_stream_reset_add_strm *str_add; 3922188854Srrs 3923188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3924188854Srrs num_req++; 3925188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 3926163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3927163953Srrs struct sctp_stream_reset_in_request *req_in; 3928163953Srrs 3929163953Srrs num_req++; 3930170992Srrs 3931163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3932170992Srrs 3933170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3934163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3935163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3936163953Srrs 3937163953Srrs num_req++; 3938163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3939170992Srrs 3940163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3941163953Srrs ret_code = 1; 3942163953Srrs goto strres_nochunk; 3943163953Srrs } 3944163953Srrs /* no more */ 3945163953Srrs break; 3946163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3947163953Srrs struct sctp_stream_reset_response *resp; 3948163953Srrs uint32_t result; 3949163953Srrs 3950163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3951163953Srrs seq = ntohl(resp->response_seq); 3952163953Srrs result = ntohl(resp->result); 3953163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3954163953Srrs ret_code = 1; 3955163953Srrs goto strres_nochunk; 3956163953Srrs } 3957163953Srrs } else { 3958163953Srrs break; 3959163953Srrs } 3960170992Srrs offset += SCTP_SIZE32(param_len); 3961163953Srrs chk_length -= SCTP_SIZE32(param_len); 3962163953Srrs } 3963163953Srrs if (num_req == 0) { 3964163953Srrs /* we have no response free the stuff */ 3965163953Srrs goto strres_nochunk; 3966163953Srrs } 3967163953Srrs /* ok we have a chunk to link in */ 3968163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3969163953Srrs chk, 3970163953Srrs sctp_next); 3971163953Srrs stcb->asoc.ctrl_queue_cnt++; 3972163953Srrs return (ret_code); 3973163953Srrs} 3974163953Srrs 3975163953Srrs/* 3976163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3977163953Srrs * to handle this, either we get the whole packet and must disect it 3978163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3979163953Srrs * from a middle box that did the disectting for us. 3980163953Srrs */ 3981163953Srrsstatic void 3982163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3983170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3984163953Srrs{ 3985163953Srrs uint32_t bottle_bw, on_queue; 3986163953Srrs uint16_t trunc_len; 3987163953Srrs unsigned int chlen; 3988163953Srrs unsigned int at; 3989163953Srrs struct sctp_chunk_desc desc; 3990163953Srrs struct sctp_chunkhdr *ch; 3991163953Srrs 3992163953Srrs chlen = ntohs(cp->ch.chunk_length); 3993163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3994163953Srrs /* XXX possible chlen underflow */ 3995163953Srrs if (chlen == 0) { 3996163953Srrs ch = NULL; 3997163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3998163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3999163953Srrs } else { 4000163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4001163953Srrs chlen -= sizeof(struct sctphdr); 4002163953Srrs /* XXX possible chlen underflow */ 4003163953Srrs memset(&desc, 0, sizeof(desc)); 4004163953Srrs } 4005163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4006170781Srrs if (trunc_len > limit) { 4007170781Srrs trunc_len = limit; 4008170781Srrs } 4009163953Srrs /* now the chunks themselves */ 4010163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4011163953Srrs desc.chunk_type = ch->chunk_type; 4012163953Srrs /* get amount we need to move */ 4013163953Srrs at = ntohs(ch->chunk_length); 4014163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4015163953Srrs /* corrupt chunk, maybe at the end? */ 4016163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4017163953Srrs break; 4018163953Srrs } 4019163953Srrs if (trunc_len == 0) { 4020163953Srrs /* we are supposed to have all of it */ 4021163953Srrs if (at > chlen) { 4022163953Srrs /* corrupt skip it */ 4023163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4024163953Srrs break; 4025163953Srrs } 4026163953Srrs } else { 4027163953Srrs /* is there enough of it left ? */ 4028163953Srrs if (desc.chunk_type == SCTP_DATA) { 4029163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4030163953Srrs sizeof(desc.data_bytes))) { 4031163953Srrs break; 4032163953Srrs } 4033163953Srrs } else { 4034163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4035163953Srrs break; 4036163953Srrs } 4037163953Srrs } 4038163953Srrs } 4039163953Srrs if (desc.chunk_type == SCTP_DATA) { 4040163953Srrs /* can we get out the tsn? */ 4041163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4042163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4043163953Srrs 4044163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4045163953Srrs /* yep */ 4046163953Srrs struct sctp_data_chunk *dcp; 4047163953Srrs uint8_t *ddp; 4048163953Srrs unsigned int iii; 4049163953Srrs 4050163953Srrs dcp = (struct sctp_data_chunk *)ch; 4051163953Srrs ddp = (uint8_t *) (dcp + 1); 4052163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4053163953Srrs desc.data_bytes[iii] = ddp[iii]; 4054163953Srrs } 4055163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4056163953Srrs } else { 4057163953Srrs /* nope we are done. */ 4058163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4059163953Srrs break; 4060163953Srrs } 4061163953Srrs } else { 4062163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4063163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4064163953Srrs } 4065163953Srrs 4066163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4067163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4068163953Srrs break; 4069163953Srrs } 4070163953Srrs if (SCTP_SIZE32(at) > chlen) { 4071163953Srrs break; 4072163953Srrs } 4073163953Srrs chlen -= SCTP_SIZE32(at); 4074163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4075163953Srrs /* done, none left */ 4076163953Srrs break; 4077163953Srrs } 4078163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4079163953Srrs } 4080163953Srrs /* Now update any rwnd --- possibly */ 4081163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4082163953Srrs /* From a peer, we get a rwnd report */ 4083163953Srrs uint32_t a_rwnd; 4084163953Srrs 4085163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4086163953Srrs 4087163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4088163953Srrs on_queue = ntohl(cp->current_onq); 4089163953Srrs if (bottle_bw && on_queue) { 4090163953Srrs /* a rwnd report is in here */ 4091163953Srrs if (bottle_bw > on_queue) 4092163953Srrs a_rwnd = bottle_bw - on_queue; 4093163953Srrs else 4094163953Srrs a_rwnd = 0; 4095163953Srrs 4096163953Srrs if (a_rwnd == 0) 4097163953Srrs stcb->asoc.peers_rwnd = 0; 4098163953Srrs else { 4099163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4100163953Srrs stcb->asoc.peers_rwnd = 4101163953Srrs a_rwnd - stcb->asoc.total_flight; 4102163953Srrs } else { 4103163953Srrs stcb->asoc.peers_rwnd = 0; 4104163953Srrs } 4105163953Srrs if (stcb->asoc.peers_rwnd < 4106163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4107163953Srrs /* SWS sender side engages */ 4108163953Srrs stcb->asoc.peers_rwnd = 0; 4109163953Srrs } 4110163953Srrs } 4111163953Srrs } 4112163953Srrs } else { 4113163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4114163953Srrs } 4115163953Srrs 4116163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4117163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4118163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4119163953Srrs (stcb->asoc.sat_network)) { 4120163953Srrs /* 4121163953Srrs * This is debateable but for sat networks it makes sense 4122163953Srrs * Note if a T3 timer has went off, we will prohibit any 4123163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4124163953Srrs */ 4125171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4126171440Srrs net, cp, &bottle_bw, &on_queue); 4127163953Srrs } 4128163953Srrs} 4129163953Srrs 4130163953Srrs/* 4131163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4132163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4133163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4134163953Srrs * length of the complete packet outputs: - length: modified to remaining 4135163953Srrs * length after control processing - netp: modified to new sctp_nets after 4136163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4137163953Srrs * bad packet,...) otherwise return the tcb for this packet 4138163953Srrs */ 4139170099Srrs#ifdef __GNUC__ 4140170099Srrs__attribute__((noinline)) 4141170099Srrs#endif 4142170099Srrs static struct sctp_tcb * 4143170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4144170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4145170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4146179157Srrs uint32_t vrf_id, uint16_t port) 4147163953Srrs{ 4148163953Srrs struct sctp_association *asoc; 4149163953Srrs uint32_t vtag_in; 4150163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4151170140Srrs uint32_t chk_length; 4152163953Srrs int ret; 4153168299Srrs int abort_no_unlock = 0; 4154163953Srrs 4155163953Srrs /* 4156163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4157163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4158163953Srrs * until we get into jumbo grams and such.. 4159163953Srrs */ 4160166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4161163953Srrs struct sctp_tcb *locked_tcb = stcb; 4162163953Srrs int got_auth = 0; 4163163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4164163953Srrs int auth_skipped = 0; 4165171990Srrs int asconf_cnt = 0; 4166163953Srrs 4167172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4168172090Srrs struct socket *so; 4169172090Srrs 4170172090Srrs#endif 4171172090Srrs 4172169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4173169420Srrs iphlen, *offset, length, stcb); 4174163953Srrs 4175163953Srrs /* validate chunk header length... */ 4176163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4177170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4178170056Srrs ntohs(ch->chunk_length)); 4179170091Srrs if (locked_tcb) { 4180170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4181170091Srrs } 4182163953Srrs return (NULL); 4183163953Srrs } 4184163953Srrs /* 4185163953Srrs * validate the verification tag 4186163953Srrs */ 4187163953Srrs vtag_in = ntohl(sh->v_tag); 4188163953Srrs 4189165220Srrs if (locked_tcb) { 4190165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4191165220Srrs } 4192163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4193170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4194170056Srrs ntohs(ch->chunk_length), vtag_in); 4195163953Srrs if (vtag_in != 0) { 4196163953Srrs /* protocol error- silently discard... */ 4197163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4198169378Srrs if (locked_tcb) { 4199163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4200169378Srrs } 4201163953Srrs return (NULL); 4202163953Srrs } 4203163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4204163953Srrs /* 4205163953Srrs * If there is no stcb, skip the AUTH chunk and process 4206163953Srrs * later after a stcb is found (to validate the lookup was 4207163953Srrs * valid. 4208163953Srrs */ 4209163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4210179783Srrs (stcb == NULL) && 4211179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4212163953Srrs /* save this chunk for later processing */ 4213163953Srrs auth_skipped = 1; 4214163953Srrs auth_offset = *offset; 4215163953Srrs auth_len = ntohs(ch->chunk_length); 4216163953Srrs 4217163953Srrs /* (temporarily) move past this chunk */ 4218163953Srrs *offset += SCTP_SIZE32(auth_len); 4219163953Srrs if (*offset >= length) { 4220163953Srrs /* no more data left in the mbuf chain */ 4221163953Srrs *offset = length; 4222170091Srrs if (locked_tcb) { 4223170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4224170091Srrs } 4225163953Srrs return (NULL); 4226163953Srrs } 4227163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4228163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4229163953Srrs } 4230169420Srrs if (ch == NULL) { 4231169420Srrs /* Help */ 4232169420Srrs *offset = length; 4233170091Srrs if (locked_tcb) { 4234170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4235170091Srrs } 4236169420Srrs return (NULL); 4237169420Srrs } 4238163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4239163953Srrs goto process_control_chunks; 4240163953Srrs } 4241163953Srrs /* 4242163953Srrs * first check if it's an ASCONF with an unknown src addr we 4243163953Srrs * need to look inside to find the association 4244163953Srrs */ 4245163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4246171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4247171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4248171990Srrs 4249163953Srrs /* inp's refcount may be reduced */ 4250163953Srrs SCTP_INP_INCR_REF(inp); 4251163953Srrs 4252171990Srrs asconf_offset = *offset; 4253171990Srrs do { 4254171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4255171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4256171990Srrs break; 4257171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4258185694Srrs *offset, sh, &inp, netp, vrf_id); 4259171990Srrs if (stcb != NULL) 4260171990Srrs break; 4261171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4262171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4263171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4264171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4265163953Srrs if (stcb == NULL) { 4266163953Srrs /* 4267163953Srrs * reduce inp's refcount if not reduced in 4268163953Srrs * sctp_findassociation_ep_asconf(). 4269163953Srrs */ 4270163953Srrs SCTP_INP_DECR_REF(inp); 4271171990Srrs } else { 4272171990Srrs locked_tcb = stcb; 4273163953Srrs } 4274171990Srrs 4275163953Srrs /* now go back and verify any auth chunk to be sure */ 4276163953Srrs if (auth_skipped && (stcb != NULL)) { 4277163953Srrs struct sctp_auth_chunk *auth; 4278163953Srrs 4279163953Srrs auth = (struct sctp_auth_chunk *) 4280163953Srrs sctp_m_getptr(m, auth_offset, 4281163953Srrs auth_len, chunk_buf); 4282163953Srrs got_auth = 1; 4283163953Srrs auth_skipped = 0; 4284169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4285163953Srrs auth_offset)) { 4286163953Srrs /* auth HMAC failed so dump it */ 4287163953Srrs *offset = length; 4288170091Srrs if (locked_tcb) { 4289170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4290170091Srrs } 4291163953Srrs return (NULL); 4292163953Srrs } else { 4293163953Srrs /* remaining chunks are HMAC checked */ 4294163953Srrs stcb->asoc.authenticated = 1; 4295163953Srrs } 4296163953Srrs } 4297163953Srrs } 4298163953Srrs if (stcb == NULL) { 4299163953Srrs /* no association, so it's out of the blue... */ 4300169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4301179157Srrs vrf_id, port); 4302163953Srrs *offset = length; 4303169378Srrs if (locked_tcb) { 4304163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4305169378Srrs } 4306163953Srrs return (NULL); 4307163953Srrs } 4308163953Srrs asoc = &stcb->asoc; 4309163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4310163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4311163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4312163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4313163953Srrs if ((vtag_in == asoc->my_vtag) || 4314163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4315163953Srrs (vtag_in == asoc->peer_vtag))) { 4316163953Srrs /* this is valid */ 4317163953Srrs } else { 4318163953Srrs /* drop this packet... */ 4319163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4320169378Srrs if (locked_tcb) { 4321163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4322169378Srrs } 4323163953Srrs return (NULL); 4324163953Srrs } 4325163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4326163953Srrs if (vtag_in != asoc->my_vtag) { 4327163953Srrs /* 4328163953Srrs * this could be a stale SHUTDOWN-ACK or the 4329163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4330163953Srrs * is still hung; we have started a new asoc 4331163953Srrs * but it won't complete until the shutdown 4332163953Srrs * is completed 4333163953Srrs */ 4334169378Srrs if (locked_tcb) { 4335163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4336169378Srrs } 4337163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4338179157Srrs NULL, vrf_id, port); 4339163953Srrs return (NULL); 4340163953Srrs } 4341163953Srrs } else { 4342163953Srrs /* for all other chunks, vtag must match */ 4343163953Srrs if (vtag_in != asoc->my_vtag) { 4344163953Srrs /* invalid vtag... */ 4345169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4346169420Srrs "invalid vtag: %xh, expect %xh\n", 4347169420Srrs vtag_in, asoc->my_vtag); 4348163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4349169378Srrs if (locked_tcb) { 4350163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4351169378Srrs } 4352163953Srrs *offset = length; 4353163953Srrs return (NULL); 4354163953Srrs } 4355163953Srrs } 4356163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4357163953Srrs /* 4358163953Srrs * process all control chunks... 4359163953Srrs */ 4360163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4361185694Srrs /* EY */ 4362185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4363163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4364163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4365163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4366179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4367171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4368171943Srrs stcb->asoc.overall_error_count, 4369171943Srrs 0, 4370171943Srrs SCTP_FROM_SCTP_INPUT, 4371171943Srrs __LINE__); 4372171943Srrs } 4373163953Srrs stcb->asoc.overall_error_count = 0; 4374163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4375163953Srrs *netp); 4376163953Srrs } 4377163953Srrsprocess_control_chunks: 4378163953Srrs while (IS_SCTP_CONTROL(ch)) { 4379163953Srrs /* validate chunk length */ 4380163953Srrs chk_length = ntohs(ch->chunk_length); 4381169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4382169420Srrs ch->chunk_type, chk_length); 4383170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4384170140Srrs if (chk_length < sizeof(*ch) || 4385170140Srrs (*offset + (int)chk_length) > length) { 4386163953Srrs *offset = length; 4387169378Srrs if (locked_tcb) { 4388163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4389169378Srrs } 4390163953Srrs return (NULL); 4391163953Srrs } 4392163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4393163953Srrs /* 4394163953Srrs * INIT-ACK only gets the init ack "header" portion only 4395163953Srrs * because we don't have to process the peer's COOKIE. All 4396163953Srrs * others get a complete chunk. 4397163953Srrs */ 4398169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4399169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4400163953Srrs /* get an init-ack chunk */ 4401163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4402163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4403163953Srrs if (ch == NULL) { 4404163953Srrs *offset = length; 4405169378Srrs if (locked_tcb) { 4406163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4407169378Srrs } 4408163953Srrs return (NULL); 4409163953Srrs } 4410170642Srrs } else { 4411170786Srrs /* For cookies and all other chunks. */ 4412169208Srrs if (chk_length > sizeof(chunk_buf)) { 4413169208Srrs /* 4414169208Srrs * use just the size of the chunk buffer so 4415170642Srrs * the front part of our chunks fit in 4416170642Srrs * contiguous space up to the chunk buffer 4417170642Srrs * size (508 bytes). For chunks that need to 4418170786Srrs * get more than that they must use the 4419170642Srrs * sctp_m_getptr() function or other means 4420170786Srrs * (e.g. know how to parse mbuf chains). 4421170786Srrs * Cookies do this already. 4422169208Srrs */ 4423169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4424169208Srrs (sizeof(chunk_buf) - 4), 4425169208Srrs chunk_buf); 4426169208Srrs if (ch == NULL) { 4427169208Srrs *offset = length; 4428169378Srrs if (locked_tcb) { 4429169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4430169378Srrs } 4431169208Srrs return (NULL); 4432169208Srrs } 4433169208Srrs } else { 4434169208Srrs /* We can fit it all */ 4435170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4436170642Srrs chk_length, chunk_buf); 4437170642Srrs if (ch == NULL) { 4438170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4439170642Srrs *offset = length; 4440170642Srrs if (locked_tcb) { 4441170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4442169208Srrs } 4443170642Srrs return (NULL); 4444163953Srrs } 4445163953Srrs } 4446163953Srrs } 4447163953Srrs num_chunks++; 4448163953Srrs /* Save off the last place we got a control from */ 4449163953Srrs if (stcb != NULL) { 4450169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4451163953Srrs /* 4452163953Srrs * allow last_control to be NULL if 4453163953Srrs * ASCONF... ASCONF processing will find the 4454163953Srrs * right net later 4455163953Srrs */ 4456169420Srrs if ((netp != NULL) && (*netp != NULL)) 4457169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4458163953Srrs } 4459163953Srrs } 4460163953Srrs#ifdef SCTP_AUDITING_ENABLED 4461163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4462163953Srrs#endif 4463163953Srrs 4464163953Srrs /* check to see if this chunk required auth, but isn't */ 4465179783Srrs if ((stcb != NULL) && 4466179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4467179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4468163953Srrs !stcb->asoc.authenticated) { 4469163953Srrs /* "silently" ignore */ 4470163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4471163953Srrs goto next_chunk; 4472163953Srrs } 4473163953Srrs switch (ch->chunk_type) { 4474163953Srrs case SCTP_INITIATION: 4475163953Srrs /* must be first and only chunk */ 4476169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4477163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4478163953Srrs /* We are not interested anymore? */ 4479163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4480163953Srrs /* 4481163953Srrs * collision case where we are 4482163953Srrs * sending to them too 4483163953Srrs */ 4484163953Srrs ; 4485163953Srrs } else { 4486169378Srrs if (locked_tcb) { 4487163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4488169378Srrs } 4489163953Srrs *offset = length; 4490163953Srrs return (NULL); 4491163953Srrs } 4492163953Srrs } 4493171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4494171990Srrs (num_chunks > 1) || 4495179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4496163953Srrs *offset = length; 4497169378Srrs if (locked_tcb) { 4498163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4499169378Srrs } 4500163953Srrs return (NULL); 4501163953Srrs } 4502163953Srrs if ((stcb != NULL) && 4503163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4504163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4505163953Srrs sctp_send_shutdown_ack(stcb, 4506163953Srrs stcb->asoc.primary_destination); 4507163953Srrs *offset = length; 4508172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4509169378Srrs if (locked_tcb) { 4510163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4511169378Srrs } 4512163953Srrs return (NULL); 4513163953Srrs } 4514169420Srrs if (netp) { 4515169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4516169420Srrs (struct sctp_init_chunk *)ch, inp, 4517179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4518169420Srrs } 4519168299Srrs if (abort_no_unlock) 4520168299Srrs return (NULL); 4521168299Srrs 4522163953Srrs *offset = length; 4523169378Srrs if (locked_tcb) { 4524163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4525169378Srrs } 4526163953Srrs return (NULL); 4527163953Srrs break; 4528170642Srrs case SCTP_PAD_CHUNK: 4529170642Srrs break; 4530163953Srrs case SCTP_INITIATION_ACK: 4531163953Srrs /* must be first and only chunk */ 4532169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4533163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4534163953Srrs /* We are not interested anymore */ 4535163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4536163953Srrs ; 4537163953Srrs } else { 4538208876Srrs if (locked_tcb != stcb) { 4539208876Srrs /* Very unlikely */ 4540163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4541169378Srrs } 4542163953Srrs *offset = length; 4543163953Srrs if (stcb) { 4544172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4545172090Srrs so = SCTP_INP_SO(inp); 4546172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4547172090Srrs SCTP_TCB_UNLOCK(stcb); 4548172090Srrs SCTP_SOCKET_LOCK(so, 1); 4549172090Srrs SCTP_TCB_LOCK(stcb); 4550172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4551172090Srrs#endif 4552171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4553172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4554172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4555172090Srrs#endif 4556163953Srrs } 4557163953Srrs return (NULL); 4558163953Srrs } 4559163953Srrs } 4560163953Srrs if ((num_chunks > 1) || 4561179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4562163953Srrs *offset = length; 4563169378Srrs if (locked_tcb) { 4564163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4565169378Srrs } 4566163953Srrs return (NULL); 4567163953Srrs } 4568169420Srrs if ((netp) && (*netp)) { 4569169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4570170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4571169420Srrs } else { 4572169420Srrs ret = -1; 4573169420Srrs } 4574163953Srrs /* 4575163953Srrs * Special case, I must call the output routine to 4576163953Srrs * get the cookie echoed 4577163953Srrs */ 4578168299Srrs if (abort_no_unlock) 4579168299Srrs return (NULL); 4580168299Srrs 4581163953Srrs if ((stcb) && ret == 0) 4582172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4583163953Srrs *offset = length; 4584169378Srrs if (locked_tcb) { 4585163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4586169378Srrs } 4587163953Srrs return (NULL); 4588163953Srrs break; 4589163953Srrs case SCTP_SELECTIVE_ACK: 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 4599211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4600211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4601202526Stuexen if (stcb == NULL) { 4602202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4603202526Stuexen break; 4604169208Srrs } 4605202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4606202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4607202526Stuexen break; 4608202526Stuexen } 4609171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4610171990Srrs /*- 4611171990Srrs * If we have sent a shutdown-ack, we will pay no 4612171990Srrs * attention to a sack sent in to us since 4613171990Srrs * we don't care anymore. 4614171990Srrs */ 4615184883Srrs break; 4616171990Srrs } 4617163953Srrs sack = (struct sctp_sack_chunk *)ch; 4618202526Stuexen flags = ch->chunk_flags; 4619202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4620163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4621163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4622202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4623163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4624202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4625202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4626202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4627202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4628202526Stuexen break; 4629202526Stuexen } 4630202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4631202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4632170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4633202526Stuexen cum_ack, num_seg, a_rwnd); 4634163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4635163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4636163953Srrs (num_seg == 0) && 4637163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4638163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4639163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4640163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4641163953Srrs ) { 4642163953Srrs /* 4643163953Srrs * We have a SIMPLE sack having no 4644163953Srrs * prior segments and data on sent 4645163953Srrs * queue to be acked.. Use the 4646163953Srrs * faster path sack processing. We 4647163953Srrs * also allow window update sacks 4648163953Srrs * with no missing segments to go 4649163953Srrs * this way too. 4650163953Srrs */ 4651169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4652169208Srrs &abort_now); 4653163953Srrs } else { 4654169420Srrs if (netp && *netp) 4655202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4656202526Stuexen stcb, *netp, 4657202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4658202526Stuexen cum_ack, a_rwnd); 4659163953Srrs } 4660202526Stuexen if (abort_now) { 4661202526Stuexen /* ABORT signal from sack processing */ 4662202526Stuexen *offset = length; 4663202526Stuexen return (NULL); 4664202526Stuexen } 4665185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4666185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4667185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4668185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4669185694Srrs } 4670163953Srrs } 4671163953Srrs break; 4672185694Srrs /* 4673185694Srrs * EY - nr_sack: If the received chunk is an 4674185694Srrs * nr_sack chunk 4675185694Srrs */ 4676185694Srrs case SCTP_NR_SELECTIVE_ACK: 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 4686211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4687211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4688211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4689211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4690185694Srrs goto unknown_chunk; 4691185694Srrs } 4692202526Stuexen if (stcb == NULL) { 4693202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4694202526Stuexen break; 4695202526Stuexen } 4696202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4697202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4698202526Stuexen break; 4699202526Stuexen } 4700185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4701185694Srrs /*- 4702185694Srrs * If we have sent a shutdown-ack, we will pay no 4703185694Srrs * attention to a sack sent in to us since 4704185694Srrs * we don't care anymore. 4705185694Srrs */ 4706202526Stuexen break; 4707185694Srrs } 4708185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4709202526Stuexen flags = ch->chunk_flags; 4710202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4711185694Srrs 4712185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4713185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4714185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4715202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4716185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4717202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4718202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4719202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4720202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4721202526Stuexen break; 4722202526Stuexen } 4723202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4724202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4725185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4726202526Stuexen cum_ack, num_seg, a_rwnd); 4727185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4728185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4729202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4730185694Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4731185694Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4732185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4733202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4734185694Srrs /* 4735185694Srrs * We have a SIMPLE sack having no 4736185694Srrs * prior segments and data on sent 4737202526Stuexen * queue to be acked. Use the faster 4738202526Stuexen * path sack processing. We also 4739202526Stuexen * allow window update sacks with no 4740202526Stuexen * missing segments to go this way 4741202526Stuexen * too. 4742185694Srrs */ 4743202526Stuexen sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4744185694Srrs &abort_now); 4745185694Srrs } else { 4746185694Srrs if (netp && *netp) 4747202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4748202526Stuexen stcb, *netp, 4749202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4750202526Stuexen cum_ack, a_rwnd); 4751185694Srrs } 4752202526Stuexen if (abort_now) { 4753202526Stuexen /* ABORT signal from sack processing */ 4754202526Stuexen *offset = length; 4755202526Stuexen return (NULL); 4756202526Stuexen } 4757185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4758185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4759185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4760185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4761185694Srrs } 4762185694Srrs } 4763185694Srrs break; 4764185694Srrs 4765163953Srrs case SCTP_HEARTBEAT_REQUEST: 4766169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4767169420Srrs if ((stcb) && netp && *netp) { 4768169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4769169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4770169420Srrs chk_length, *netp); 4771169420Srrs 4772169420Srrs /* He's alive so give him credit */ 4773179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4774171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4775171943Srrs stcb->asoc.overall_error_count, 4776171943Srrs 0, 4777171943Srrs SCTP_FROM_SCTP_INPUT, 4778171943Srrs __LINE__); 4779171943Srrs } 4780169420Srrs stcb->asoc.overall_error_count = 0; 4781163953Srrs } 4782163953Srrs break; 4783163953Srrs case SCTP_HEARTBEAT_ACK: 4784169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4785169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4786169208Srrs /* Its not ours */ 4787169352Srrs *offset = length; 4788169378Srrs if (locked_tcb) { 4789169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4790169378Srrs } 4791169208Srrs return (NULL); 4792169208Srrs } 4793163953Srrs /* He's alive so give him credit */ 4794179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4795171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4796171943Srrs stcb->asoc.overall_error_count, 4797171943Srrs 0, 4798171943Srrs SCTP_FROM_SCTP_INPUT, 4799171943Srrs __LINE__); 4800171943Srrs } 4801163953Srrs stcb->asoc.overall_error_count = 0; 4802163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4803169420Srrs if (netp && *netp) 4804169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4805169420Srrs stcb, *netp); 4806163953Srrs break; 4807163953Srrs case SCTP_ABORT_ASSOCIATION: 4808170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4809170091Srrs stcb); 4810169420Srrs if ((stcb) && netp && *netp) 4811169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4812169420Srrs stcb, *netp); 4813163953Srrs *offset = length; 4814163953Srrs return (NULL); 4815163953Srrs break; 4816163953Srrs case SCTP_SHUTDOWN: 4817170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4818170091Srrs stcb); 4819169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4820169208Srrs *offset = length; 4821169378Srrs if (locked_tcb) { 4822169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4823169378Srrs } 4824169208Srrs return (NULL); 4825169420Srrs } 4826169420Srrs if (netp && *netp) { 4827163953Srrs int abort_flag = 0; 4828163953Srrs 4829163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4830163953Srrs stcb, *netp, &abort_flag); 4831163953Srrs if (abort_flag) { 4832163953Srrs *offset = length; 4833163953Srrs return (NULL); 4834163953Srrs } 4835163953Srrs } 4836163953Srrs break; 4837163953Srrs case SCTP_SHUTDOWN_ACK: 4838170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4839169420Srrs if ((stcb) && (netp) && (*netp)) 4840169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4841163953Srrs *offset = length; 4842163953Srrs return (NULL); 4843163953Srrs break; 4844169420Srrs 4845163953Srrs case SCTP_OPERATION_ERROR: 4846169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4847169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4848169420Srrs 4849163953Srrs *offset = length; 4850163953Srrs return (NULL); 4851163953Srrs } 4852163953Srrs break; 4853163953Srrs case SCTP_COOKIE_ECHO: 4854169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4855170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4856163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4857163953Srrs ; 4858163953Srrs } else { 4859169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4860163953Srrs /* We are not interested anymore */ 4861208878Srrs abend: 4862208853Srrs if (stcb) { 4863208853Srrs SCTP_TCB_UNLOCK(stcb); 4864208853Srrs } 4865163953Srrs *offset = length; 4866163953Srrs return (NULL); 4867163953Srrs } 4868163953Srrs } 4869163953Srrs /* 4870163953Srrs * First are we accepting? We do this again here 4871207924Srrs * since it is possible that a previous endpoint WAS 4872207924Srrs * listening responded to a INIT-ACK and then 4873163953Srrs * closed. We opened and bound.. and are now no 4874163953Srrs * longer listening. 4875163953Srrs */ 4876172703Srrs 4877172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4878172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4879179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4880163953Srrs struct mbuf *oper; 4881163953Srrs struct sctp_paramhdr *phdr; 4882163953Srrs 4883172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4884172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4885172703Srrs if (oper) { 4886172703Srrs SCTP_BUF_LEN(oper) = 4887172703Srrs sizeof(struct sctp_paramhdr); 4888172703Srrs phdr = mtod(oper, 4889172703Srrs struct sctp_paramhdr *); 4890172703Srrs phdr->param_type = 4891172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4892172703Srrs phdr->param_length = 4893172703Srrs htons(sizeof(struct sctp_paramhdr)); 4894163953Srrs } 4895172703Srrs sctp_abort_association(inp, stcb, m, 4896179157Srrs iphlen, sh, oper, vrf_id, port); 4897163953Srrs } 4898172703Srrs *offset = length; 4899172703Srrs return (NULL); 4900172703Srrs } else { 4901163953Srrs struct mbuf *ret_buf; 4902165220Srrs struct sctp_inpcb *linp; 4903163953Srrs 4904169420Srrs if (stcb) { 4905165220Srrs linp = NULL; 4906169420Srrs } else { 4907165220Srrs linp = inp; 4908169420Srrs } 4909165220Srrs 4910169420Srrs if (linp) { 4911165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4912208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 4913208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 4914208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4915208878Srrs goto abend; 4916208878Srrs } 4917169420Srrs } 4918169420Srrs if (netp) { 4919169420Srrs ret_buf = 4920169420Srrs sctp_handle_cookie_echo(m, iphlen, 4921169420Srrs *offset, sh, 4922169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4923169420Srrs &inp, &stcb, netp, 4924169420Srrs auth_skipped, 4925169420Srrs auth_offset, 4926169420Srrs auth_len, 4927169420Srrs &locked_tcb, 4928179157Srrs vrf_id, 4929179157Srrs port); 4930169420Srrs } else { 4931169420Srrs ret_buf = NULL; 4932169420Srrs } 4933169420Srrs if (linp) { 4934165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4935169420Srrs } 4936163953Srrs if (ret_buf == NULL) { 4937163953Srrs if (locked_tcb) { 4938163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4939163953Srrs } 4940169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4941169420Srrs "GAK, null buffer\n"); 4942163953Srrs auth_skipped = 0; 4943163953Srrs *offset = length; 4944163953Srrs return (NULL); 4945163953Srrs } 4946163953Srrs /* if AUTH skipped, see if it verified... */ 4947163953Srrs if (auth_skipped) { 4948163953Srrs got_auth = 1; 4949163953Srrs auth_skipped = 0; 4950163953Srrs } 4951163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4952163953Srrs /* 4953163953Srrs * Restart the timer if we have 4954163953Srrs * pending data 4955163953Srrs */ 4956163953Srrs struct sctp_tmit_chunk *chk; 4957163953Srrs 4958163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4959163953Srrs if (chk) { 4960163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4961163953Srrs stcb->sctp_ep, stcb, 4962163953Srrs chk->whoTo); 4963163953Srrs } 4964163953Srrs } 4965163953Srrs } 4966163953Srrs break; 4967163953Srrs case SCTP_COOKIE_ACK: 4968170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4969169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4970169378Srrs if (locked_tcb) { 4971169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4972169378Srrs } 4973169352Srrs return (NULL); 4974169352Srrs } 4975163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4976163953Srrs /* We are not interested anymore */ 4977163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4978163953Srrs ; 4979169420Srrs } else if (stcb) { 4980172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4981172090Srrs so = SCTP_INP_SO(inp); 4982172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4983172090Srrs SCTP_TCB_UNLOCK(stcb); 4984172090Srrs SCTP_SOCKET_LOCK(so, 1); 4985172090Srrs SCTP_TCB_LOCK(stcb); 4986172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4987172090Srrs#endif 4988171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4989172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4990172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4991172090Srrs#endif 4992163953Srrs *offset = length; 4993163953Srrs return (NULL); 4994163953Srrs } 4995163953Srrs } 4996163953Srrs /* He's alive so give him credit */ 4997169420Srrs if ((stcb) && netp && *netp) { 4998179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4999171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5000171943Srrs stcb->asoc.overall_error_count, 5001171943Srrs 0, 5002171943Srrs SCTP_FROM_SCTP_INPUT, 5003171943Srrs __LINE__); 5004171943Srrs } 5005169378Srrs stcb->asoc.overall_error_count = 0; 5006169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5007169378Srrs } 5008163953Srrs break; 5009163953Srrs case SCTP_ECN_ECHO: 5010169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5011163953Srrs /* He's alive so give him credit */ 5012169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5013169208Srrs /* Its not ours */ 5014169378Srrs if (locked_tcb) { 5015169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5016169378Srrs } 5017169208Srrs *offset = length; 5018169208Srrs return (NULL); 5019169208Srrs } 5020169378Srrs if (stcb) { 5021179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5022171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5023171943Srrs stcb->asoc.overall_error_count, 5024171943Srrs 0, 5025171943Srrs SCTP_FROM_SCTP_INPUT, 5026171943Srrs __LINE__); 5027171943Srrs } 5028169378Srrs stcb->asoc.overall_error_count = 0; 5029169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5030169378Srrs stcb); 5031169378Srrs } 5032163953Srrs break; 5033163953Srrs case SCTP_ECN_CWR: 5034169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5035163953Srrs /* He's alive so give him credit */ 5036169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5037169208Srrs /* Its not ours */ 5038169378Srrs if (locked_tcb) { 5039169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5040169378Srrs } 5041169208Srrs *offset = length; 5042169208Srrs return (NULL); 5043169208Srrs } 5044169378Srrs if (stcb) { 5045179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5046171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5047171943Srrs stcb->asoc.overall_error_count, 5048171943Srrs 0, 5049171943Srrs SCTP_FROM_SCTP_INPUT, 5050171943Srrs __LINE__); 5051171943Srrs } 5052169378Srrs stcb->asoc.overall_error_count = 0; 5053169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 5054169378Srrs } 5055163953Srrs break; 5056163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5057170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5058163953Srrs /* must be first and only chunk */ 5059163953Srrs if ((num_chunks > 1) || 5060170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5061163953Srrs *offset = length; 5062169378Srrs if (locked_tcb) { 5063163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5064169378Srrs } 5065163953Srrs return (NULL); 5066163953Srrs } 5067169420Srrs if ((stcb) && netp && *netp) { 5068169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5069169378Srrs stcb, *netp); 5070169378Srrs } 5071163953Srrs *offset = length; 5072163953Srrs return (NULL); 5073163953Srrs break; 5074163953Srrs case SCTP_ASCONF: 5075169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5076163953Srrs /* He's alive so give him credit */ 5077169378Srrs if (stcb) { 5078179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5079171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5080171943Srrs stcb->asoc.overall_error_count, 5081171943Srrs 0, 5082171943Srrs SCTP_FROM_SCTP_INPUT, 5083171943Srrs __LINE__); 5084171943Srrs } 5085169378Srrs stcb->asoc.overall_error_count = 0; 5086169378Srrs sctp_handle_asconf(m, *offset, 5087171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5088171990Srrs asconf_cnt++; 5089169378Srrs } 5090163953Srrs break; 5091163953Srrs case SCTP_ASCONF_ACK: 5092169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5093169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5094169208Srrs /* Its not ours */ 5095169378Srrs if (locked_tcb) { 5096169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5097169378Srrs } 5098169208Srrs *offset = length; 5099169208Srrs return (NULL); 5100169208Srrs } 5101169420Srrs if ((stcb) && netp && *netp) { 5102169378Srrs /* He's alive so give him credit */ 5103179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5104171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5105171943Srrs stcb->asoc.overall_error_count, 5106171943Srrs 0, 5107171943Srrs SCTP_FROM_SCTP_INPUT, 5108171943Srrs __LINE__); 5109171943Srrs } 5110169378Srrs stcb->asoc.overall_error_count = 0; 5111169378Srrs sctp_handle_asconf_ack(m, *offset, 5112172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5113172190Srrs if (abort_no_unlock) 5114172190Srrs return (NULL); 5115169378Srrs } 5116163953Srrs break; 5117163953Srrs case SCTP_FORWARD_CUM_TSN: 5118169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5119169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5120169208Srrs /* Its not ours */ 5121169378Srrs if (locked_tcb) { 5122169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5123169378Srrs } 5124169208Srrs *offset = length; 5125169208Srrs return (NULL); 5126169208Srrs } 5127163953Srrs /* He's alive so give him credit */ 5128169378Srrs if (stcb) { 5129163953Srrs int abort_flag = 0; 5130163953Srrs 5131163953Srrs stcb->asoc.overall_error_count = 0; 5132179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5133171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5134171943Srrs stcb->asoc.overall_error_count, 5135171943Srrs 0, 5136171943Srrs SCTP_FROM_SCTP_INPUT, 5137171943Srrs __LINE__); 5138171943Srrs } 5139163953Srrs *fwd_tsn_seen = 1; 5140163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5141163953Srrs /* We are not interested anymore */ 5142172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5143172090Srrs so = SCTP_INP_SO(inp); 5144172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5145172090Srrs SCTP_TCB_UNLOCK(stcb); 5146172090Srrs SCTP_SOCKET_LOCK(so, 1); 5147172090Srrs SCTP_TCB_LOCK(stcb); 5148172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5149172090Srrs#endif 5150171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5151172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5152172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5153172090Srrs#endif 5154163953Srrs *offset = length; 5155163953Srrs return (NULL); 5156163953Srrs } 5157163953Srrs sctp_handle_forward_tsn(stcb, 5158170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5159163953Srrs if (abort_flag) { 5160163953Srrs *offset = length; 5161163953Srrs return (NULL); 5162163953Srrs } else { 5163179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5164171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5165171943Srrs stcb->asoc.overall_error_count, 5166171943Srrs 0, 5167171943Srrs SCTP_FROM_SCTP_INPUT, 5168171943Srrs __LINE__); 5169171943Srrs } 5170163953Srrs stcb->asoc.overall_error_count = 0; 5171163953Srrs } 5172163953Srrs 5173163953Srrs } 5174163953Srrs break; 5175163953Srrs case SCTP_STREAM_RESET: 5176169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5177169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5178169208Srrs /* Its not ours */ 5179169378Srrs if (locked_tcb) { 5180169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5181169378Srrs } 5182169208Srrs *offset = length; 5183169208Srrs return (NULL); 5184169208Srrs } 5185163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5186163953Srrs /* We are not interested anymore */ 5187172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5188172090Srrs so = SCTP_INP_SO(inp); 5189172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5190172090Srrs SCTP_TCB_UNLOCK(stcb); 5191172090Srrs SCTP_SOCKET_LOCK(so, 1); 5192172090Srrs SCTP_TCB_LOCK(stcb); 5193172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5194172090Srrs#endif 5195171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5196172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5197172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5198172090Srrs#endif 5199163953Srrs *offset = length; 5200163953Srrs return (NULL); 5201163953Srrs } 5202169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5203169420Srrs /* 5204169420Srrs * hmm, peer should have announced this, but 5205169420Srrs * we will turn it on since he is sending us 5206169420Srrs * a stream reset. 5207169420Srrs */ 5208169420Srrs stcb->asoc.peer_supports_strreset = 1; 5209163953Srrs } 5210170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5211169420Srrs /* stop processing */ 5212169420Srrs *offset = length; 5213169420Srrs return (NULL); 5214169420Srrs } 5215163953Srrs break; 5216163953Srrs case SCTP_PACKET_DROPPED: 5217169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5218163953Srrs /* re-get it all please */ 5219169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5220169208Srrs /* Its not ours */ 5221169378Srrs if (locked_tcb) { 5222169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5223169378Srrs } 5224169208Srrs *offset = length; 5225169208Srrs return (NULL); 5226169208Srrs } 5227169420Srrs if (ch && (stcb) && netp && (*netp)) { 5228169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5229170781Srrs stcb, *netp, 5230170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5231170781Srrs 5232169378Srrs } 5233163953Srrs break; 5234163953Srrs 5235163953Srrs case SCTP_AUTHENTICATION: 5236169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5237179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5238163953Srrs goto unknown_chunk; 5239163953Srrs 5240163953Srrs if (stcb == NULL) { 5241163953Srrs /* save the first AUTH for later processing */ 5242163953Srrs if (auth_skipped == 0) { 5243163953Srrs auth_offset = *offset; 5244163953Srrs auth_len = chk_length; 5245163953Srrs auth_skipped = 1; 5246163953Srrs } 5247163953Srrs /* skip this chunk (temporarily) */ 5248163953Srrs goto next_chunk; 5249163953Srrs } 5250169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5251169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5252169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5253169208Srrs /* Its not ours */ 5254169378Srrs if (locked_tcb) { 5255169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5256169378Srrs } 5257169208Srrs *offset = length; 5258169208Srrs return (NULL); 5259169208Srrs } 5260163953Srrs if (got_auth == 1) { 5261163953Srrs /* skip this chunk... it's already auth'd */ 5262163953Srrs goto next_chunk; 5263163953Srrs } 5264163953Srrs got_auth = 1; 5265169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5266163953Srrs m, *offset)) { 5267163953Srrs /* auth HMAC failed so dump the packet */ 5268163953Srrs *offset = length; 5269163953Srrs return (stcb); 5270163953Srrs } else { 5271163953Srrs /* remaining chunks are HMAC checked */ 5272163953Srrs stcb->asoc.authenticated = 1; 5273163953Srrs } 5274163953Srrs break; 5275163953Srrs 5276163953Srrs default: 5277163953Srrs unknown_chunk: 5278163953Srrs /* it's an unknown chunk! */ 5279163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5280163953Srrs struct mbuf *mm; 5281163953Srrs struct sctp_paramhdr *phd; 5282163953Srrs 5283163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5284165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5285163953Srrs if (mm) { 5286163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5287163953Srrs /* 5288163953Srrs * We cheat and use param type since 5289163953Srrs * we did not bother to define a 5290163953Srrs * error cause struct. They are the 5291163953Srrs * same basic format with different 5292163953Srrs * names. 5293163953Srrs */ 5294163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5295163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5296165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5297166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5298163953Srrs M_DONTWAIT); 5299165647Srrs if (SCTP_BUF_NEXT(mm)) { 5300178202Srrs#ifdef SCTP_MBUF_LOGGING 5301179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5302178202Srrs struct mbuf *mat; 5303178202Srrs 5304178202Srrs mat = SCTP_BUF_NEXT(mm); 5305178202Srrs while (mat) { 5306178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5307178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5308178202Srrs } 5309178202Srrs mat = SCTP_BUF_NEXT(mat); 5310178202Srrs } 5311178202Srrs } 5312178202Srrs#endif 5313163953Srrs sctp_queue_op_err(stcb, mm); 5314163953Srrs } else { 5315163953Srrs sctp_m_freem(mm); 5316163953Srrs } 5317163953Srrs } 5318163953Srrs } 5319163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5320163953Srrs /* discard this packet */ 5321163953Srrs *offset = length; 5322163953Srrs return (stcb); 5323163953Srrs } /* else skip this bad chunk and continue... */ 5324163953Srrs break; 5325163953Srrs } /* switch (ch->chunk_type) */ 5326163953Srrs 5327163953Srrs 5328163953Srrsnext_chunk: 5329163953Srrs /* get the next chunk */ 5330163953Srrs *offset += SCTP_SIZE32(chk_length); 5331163953Srrs if (*offset >= length) { 5332163953Srrs /* no more data left in the mbuf chain */ 5333163953Srrs break; 5334163953Srrs } 5335163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5336163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5337163953Srrs if (ch == NULL) { 5338169378Srrs if (locked_tcb) { 5339163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5340169378Srrs } 5341163953Srrs *offset = length; 5342163953Srrs return (NULL); 5343163953Srrs } 5344163953Srrs } /* while */ 5345171990Srrs 5346171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5347171990Srrs sctp_send_asconf_ack(stcb); 5348171990Srrs } 5349163953Srrs return (stcb); 5350163953Srrs} 5351163953Srrs 5352163953Srrs 5353163953Srrs/* 5354163953Srrs * Process the ECN bits we have something set so we must look to see if it is 5355163953Srrs * ECN(0) or ECN(1) or CE 5356163953Srrs */ 5357170806Srrsstatic void 5358163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 5359163953Srrs uint8_t ecn_bits) 5360163953Srrs{ 5361163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5362163953Srrs ; 5363163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 5364163953Srrs /* 5365163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 5366163953Srrs * change the NS bit (that we have yet to find a way to send 5367163953Srrs * it yet). 5368163953Srrs */ 5369163953Srrs 5370163953Srrs /* ECN Nonce stuff */ 5371163953Srrs stcb->asoc.receiver_nonce_sum++; 5372163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 5373163953Srrs 5374163953Srrs /* 5375163953Srrs * Drag up the last_echo point if cumack is larger since we 5376163953Srrs * don't want the point falling way behind by more than 5377163953Srrs * 2^^31 and then having it be incorrect. 5378163953Srrs */ 5379163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5380163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5381163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5382163953Srrs } 5383163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 5384163953Srrs /* 5385163953Srrs * Drag up the last_echo point if cumack is larger since we 5386163953Srrs * don't want the point falling way behind by more than 5387163953Srrs * 2^^31 and then having it be incorrect. 5388163953Srrs */ 5389163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5390163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5391163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5392163953Srrs } 5393163953Srrs } 5394163953Srrs} 5395163953Srrs 5396170806Srrsstatic void 5397163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 5398163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 5399163953Srrs{ 5400163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5401163953Srrs /* 5402163953Srrs * we possibly must notify the sender that a congestion 5403163953Srrs * window reduction is in order. We do this by adding a ECNE 5404163953Srrs * chunk to the output chunk queue. The incoming CWR will 5405163953Srrs * remove this chunk. 5406163953Srrs */ 5407163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 5408163953Srrs MAX_TSN)) { 5409163953Srrs /* Yep, we need to add a ECNE */ 5410163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5411163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 5412163953Srrs } 5413163953Srrs } 5414163953Srrs} 5415163953Srrs 5416171440Srrs#ifdef INVARIANTS 5417208853Srrs#ifdef __GNUC__ 5418208853Srrs__attribute__((noinline)) 5419208853Srrs#endif 5420208853Srrs void 5421208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5422171440Srrs{ 5423208853Srrs struct sctp_tcb *lstcb; 5424171440Srrs 5425208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5426208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5427171440Srrs panic("Own lock on stcb at return from input"); 5428171440Srrs } 5429171440Srrs } 5430208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5431208876Srrs panic("Own create lock on inp"); 5432208876Srrs } 5433208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5434208876Srrs panic("Own inp lock on inp"); 5435208876Srrs } 5436171440Srrs} 5437171440Srrs 5438171440Srrs#endif 5439171440Srrs 5440163953Srrs/* 5441163953Srrs * common input chunk processing (v4 and v6) 5442163953Srrs */ 5443169378Srrsvoid 5444163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5445163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5446163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5447179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5448163953Srrs{ 5449163953Srrs /* 5450163953Srrs * Control chunk processing 5451163953Srrs */ 5452163953Srrs uint32_t high_tsn; 5453163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5454163953Srrs struct mbuf *m = *mm; 5455163953Srrs int abort_flag = 0; 5456163953Srrs int un_sent; 5457163953Srrs 5458163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5459163953Srrs#ifdef SCTP_AUDITING_ENABLED 5460163953Srrs sctp_audit_log(0xE0, 1); 5461163953Srrs sctp_auditing(0, inp, stcb, net); 5462163953Srrs#endif 5463163953Srrs 5464179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5465179783Srrs m, iphlen, offset, length, stcb); 5466163953Srrs if (stcb) { 5467163953Srrs /* always clear this before beginning a packet */ 5468163953Srrs stcb->asoc.authenticated = 0; 5469163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5470171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5471171990Srrs stcb, stcb->asoc.state); 5472171990Srrs 5473171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5474171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5475171745Srrs /*- 5476171745Srrs * If we hit here, we had a ref count 5477171745Srrs * up when the assoc was aborted and the 5478171745Srrs * timer is clearing out the assoc, we should 5479171745Srrs * NOT respond to any packet.. its OOTB. 5480171745Srrs */ 5481171745Srrs SCTP_TCB_UNLOCK(stcb); 5482171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5483179157Srrs vrf_id, port); 5484171745Srrs goto out_now; 5485171745Srrs } 5486163953Srrs } 5487163953Srrs if (IS_SCTP_CONTROL(ch)) { 5488163953Srrs /* process the control portion of the SCTP packet */ 5489169655Srrs /* sa_ignore NO_NULL_CHK */ 5490163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5491179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5492163953Srrs if (stcb) { 5493163953Srrs /* 5494163953Srrs * This covers us if the cookie-echo was there and 5495163953Srrs * it changes our INP. 5496163953Srrs */ 5497163953Srrs inp = stcb->sctp_ep; 5498179783Srrs if ((net) && (port)) { 5499179783Srrs if (net->port == 0) { 5500179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5501179783Srrs } 5502179783Srrs net->port = port; 5503179783Srrs } 5504163953Srrs } 5505163953Srrs } else { 5506163953Srrs /* 5507163953Srrs * no control chunks, so pre-process DATA chunks (these 5508163953Srrs * checks are taken care of by control processing) 5509163953Srrs */ 5510163953Srrs 5511163953Srrs /* 5512163953Srrs * if DATA only packet, and auth is required, then punt... 5513163953Srrs * can't have authenticated without any AUTH (control) 5514163953Srrs * chunks 5515163953Srrs */ 5516179783Srrs if ((stcb != NULL) && 5517179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5518179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5519163953Srrs /* "silently" ignore */ 5520163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5521163953Srrs SCTP_TCB_UNLOCK(stcb); 5522171440Srrs goto out_now; 5523163953Srrs } 5524163953Srrs if (stcb == NULL) { 5525163953Srrs /* out of the blue DATA chunk */ 5526169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5527179157Srrs vrf_id, port); 5528171440Srrs goto out_now; 5529163953Srrs } 5530163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5531163953Srrs /* v_tag mismatch! */ 5532163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5533163953Srrs SCTP_TCB_UNLOCK(stcb); 5534171440Srrs goto out_now; 5535163953Srrs } 5536163953Srrs } 5537163953Srrs 5538163953Srrs if (stcb == NULL) { 5539163953Srrs /* 5540163953Srrs * no valid TCB for this packet, or we found it's a bad 5541163953Srrs * packet while processing control, or we're done with this 5542163953Srrs * packet (done or skip rest of data), so we drop it... 5543163953Srrs */ 5544171440Srrs goto out_now; 5545163953Srrs } 5546163953Srrs /* 5547163953Srrs * DATA chunk processing 5548163953Srrs */ 5549163953Srrs /* plow through the data chunks while length > offset */ 5550163953Srrs 5551163953Srrs /* 5552163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5553163953Srrs * DATA is required. 5554163953Srrs */ 5555179783Srrs if ((length > offset) && 5556179783Srrs (stcb != NULL) && 5557179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5558179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5559163953Srrs !stcb->asoc.authenticated) { 5560163953Srrs /* "silently" ignore */ 5561163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5562169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5563169420Srrs "Data chunk requires AUTH, skipped\n"); 5564165220Srrs goto trigger_send; 5565163953Srrs } 5566163953Srrs if (length > offset) { 5567163953Srrs int retval; 5568163953Srrs 5569163953Srrs /* 5570163953Srrs * First check to make sure our state is correct. We would 5571163953Srrs * not get here unless we really did have a tag, so we don't 5572163953Srrs * abort if this happens, just dump the chunk silently. 5573163953Srrs */ 5574163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5575163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5576163953Srrs /* 5577163953Srrs * we consider data with valid tags in this state 5578163953Srrs * shows us the cookie-ack was lost. Imply it was 5579163953Srrs * there. 5580163953Srrs */ 5581179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5582171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5583171943Srrs stcb->asoc.overall_error_count, 5584171943Srrs 0, 5585171943Srrs SCTP_FROM_SCTP_INPUT, 5586171943Srrs __LINE__); 5587171943Srrs } 5588163953Srrs stcb->asoc.overall_error_count = 0; 5589163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5590163953Srrs break; 5591163953Srrs case SCTP_STATE_COOKIE_WAIT: 5592163953Srrs /* 5593163953Srrs * We consider OOTB any data sent during asoc setup. 5594163953Srrs */ 5595169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5596179157Srrs vrf_id, port); 5597163953Srrs SCTP_TCB_UNLOCK(stcb); 5598171440Srrs goto out_now; 5599171531Srrs /* sa_ignore NOTREACHED */ 5600163953Srrs break; 5601163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5602163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5603163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5604163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5605163953Srrs default: 5606163953Srrs SCTP_TCB_UNLOCK(stcb); 5607171440Srrs goto out_now; 5608171531Srrs /* sa_ignore NOTREACHED */ 5609163953Srrs break; 5610163953Srrs case SCTP_STATE_OPEN: 5611163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5612163953Srrs break; 5613163953Srrs } 5614163953Srrs /* take care of ECN, part 1. */ 5615163953Srrs if (stcb->asoc.ecn_allowed && 5616163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5617163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5618163953Srrs } 5619163953Srrs /* plow through the data chunks while length > offset */ 5620163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5621163953Srrs inp, stcb, net, &high_tsn); 5622163953Srrs if (retval == 2) { 5623163953Srrs /* 5624163953Srrs * The association aborted, NO UNLOCK needed since 5625163953Srrs * the association is destroyed. 5626163953Srrs */ 5627171440Srrs goto out_now; 5628163953Srrs } 5629163953Srrs data_processed = 1; 5630163953Srrs if (retval == 0) { 5631163953Srrs /* take care of ecn part 2. */ 5632163953Srrs if (stcb->asoc.ecn_allowed && 5633163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5634163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5635163953Srrs ecn_bits); 5636163953Srrs } 5637163953Srrs } 5638163953Srrs /* 5639163953Srrs * Anything important needs to have been m_copy'ed in 5640163953Srrs * process_data 5641163953Srrs */ 5642163953Srrs } 5643163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5644163953Srrs int was_a_gap = 0; 5645163953Srrs 5646163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5647163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5648163953Srrs /* there was a gap before this data was processed */ 5649163953Srrs was_a_gap = 1; 5650163953Srrs } 5651190689Srrs stcb->asoc.send_sack = 1; 5652206137Stuexen sctp_sack_check(stcb, was_a_gap, &abort_flag); 5653163953Srrs if (abort_flag) { 5654163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5655171440Srrs goto out_now; 5656163953Srrs } 5657190689Srrs } else if (fwd_tsn_seen) { 5658190689Srrs stcb->asoc.send_sack = 1; 5659163953Srrs } 5660163953Srrs /* trigger send of any chunks in queue... */ 5661165220Srrstrigger_send: 5662163953Srrs#ifdef SCTP_AUDITING_ENABLED 5663163953Srrs sctp_audit_log(0xE0, 2); 5664163953Srrs sctp_auditing(1, inp, stcb, net); 5665163953Srrs#endif 5666169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5667169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5668169420Srrs stcb->asoc.peers_rwnd, 5669169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5670169420Srrs stcb->asoc.total_flight); 5671163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5672163953Srrs 5673163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5674163953Srrs ((un_sent) && 5675163953Srrs (stcb->asoc.peers_rwnd > 0 || 5676163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5677169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5678172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5679169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5680163953Srrs } 5681163953Srrs#ifdef SCTP_AUDITING_ENABLED 5682163953Srrs sctp_audit_log(0xE0, 3); 5683163953Srrs sctp_auditing(2, inp, stcb, net); 5684163953Srrs#endif 5685163953Srrs SCTP_TCB_UNLOCK(stcb); 5686171440Srrsout_now: 5687171440Srrs#ifdef INVARIANTS 5688171440Srrs sctp_validate_no_locks(inp); 5689171440Srrs#endif 5690169378Srrs return; 5691163953Srrs} 5692163953Srrs 5693185694Srrs#if 0 5694185694Srrsstatic void 5695185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5696185694Srrs{ 5697185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5698185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5699185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5700185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5701185694Srrs } 5702185694Srrs} 5703163953Srrs 5704185694Srrs#endif 5705185694Srrs 5706163953Srrsvoid 5707189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5708163953Srrs{ 5709163953Srrs#ifdef SCTP_MBUF_LOGGING 5710163953Srrs struct mbuf *mat; 5711163953Srrs 5712163953Srrs#endif 5713165647Srrs struct mbuf *m; 5714163953Srrs int iphlen; 5715170181Srrs uint32_t vrf_id = 0; 5716163953Srrs uint8_t ecn_bits; 5717163953Srrs struct ip *ip; 5718163953Srrs struct sctphdr *sh; 5719163953Srrs struct sctp_inpcb *inp = NULL; 5720163953Srrs 5721163953Srrs uint32_t check, calc_check; 5722163953Srrs struct sctp_nets *net; 5723163953Srrs struct sctp_tcb *stcb = NULL; 5724163953Srrs struct sctp_chunkhdr *ch; 5725163953Srrs int refcount_up = 0; 5726163953Srrs int length, mlen, offset; 5727163953Srrs 5728169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5729169352Srrs SCTP_RELEASE_PKT(i_pak); 5730169352Srrs return; 5731169352Srrs } 5732168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5733163953Srrs iphlen = off; 5734165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5735170056Srrs 5736163953Srrs net = NULL; 5737163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5738163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5739163953Srrs 5740170091Srrs 5741163953Srrs#ifdef SCTP_MBUF_LOGGING 5742163953Srrs /* Log in any input mbufs */ 5743179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5744170744Srrs mat = m; 5745170744Srrs while (mat) { 5746170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5747170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5748170744Srrs } 5749170744Srrs mat = SCTP_BUF_NEXT(mat); 5750163953Srrs } 5751163953Srrs } 5752163953Srrs#endif 5753170091Srrs#ifdef SCTP_PACKET_LOGGING 5754179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5755170921Srrs sctp_packet_log(m, mlen); 5756170091Srrs#endif 5757170091Srrs /* 5758170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5759170091Srrs * case) 5760170091Srrs */ 5761170091Srrs mlen -= iphlen; 5762168124Srrs 5763163953Srrs /* 5764163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5765163953Srrs */ 5766163953Srrs ip = mtod(m, struct ip *); 5767163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5768165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5769163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5770163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5771163953Srrs return; 5772163953Srrs } 5773163953Srrs ip = mtod(m, struct ip *); 5774163953Srrs } 5775188067Srrs /* validate mbuf chain length with IP payload length */ 5776188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5777188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5778188067Srrs goto bad; 5779188067Srrs } 5780163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5781163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5782170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5783170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5784163953Srrs 5785163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5786163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5787163953Srrs goto bad; 5788163953Srrs } 5789168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5790163953Srrs /* 5791163953Srrs * We only look at broadcast if its a front state, All 5792163953Srrs * others we will not have a tcb for anyway. 5793163953Srrs */ 5794163953Srrs goto bad; 5795163953Srrs } 5796163953Srrs /* validate SCTP checksum */ 5797188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5798188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5799188067Srrs m->m_pkthdr.len, 5800188067Srrs if_name(m->m_pkthdr.rcvif), 5801188067Srrs m->m_pkthdr.csum_flags); 5802188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5803188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5804188067Srrs goto sctp_skip_csum_4; 5805188067Srrs } 5806171990Srrs check = sh->checksum; /* save incoming checksum */ 5807179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 5808171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5809171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5810171990Srrs ) { 5811188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 5812171990Srrs goto sctp_skip_csum_4; 5813171990Srrs } 5814171990Srrs sh->checksum = 0; /* prepare for calc */ 5815188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5816203503Stuexen sh->checksum = check; 5817188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5818171990Srrs if (calc_check != check) { 5819171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5820171990Srrs calc_check, check, m, mlen, iphlen); 5821163953Srrs 5822171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5823171990Srrs offset - sizeof(*ch), 5824171990Srrs sh, ch, &inp, &net, 5825171990Srrs vrf_id); 5826179157Srrs if ((net) && (port)) { 5827179157Srrs if (net->port == 0) { 5828179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5829179157Srrs } 5830179157Srrs net->port = port; 5831179157Srrs } 5832171990Srrs if ((inp) && (stcb)) { 5833171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5834172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5835171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5836171990Srrs refcount_up = 1; 5837163953Srrs } 5838171990Srrs SCTP_STAT_INCR(sctps_badsum); 5839171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5840171990Srrs goto bad; 5841168299Srrs } 5842163953Srrssctp_skip_csum_4: 5843168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5844168124Srrs if (sh->dest_port == 0) { 5845168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5846168124Srrs goto bad; 5847168124Srrs } 5848163953Srrs /* 5849163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5850163953Srrs * IP/SCTP/first chunk header... 5851163953Srrs */ 5852163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5853168299Srrs sh, ch, &inp, &net, vrf_id); 5854179157Srrs if ((net) && (port)) { 5855179157Srrs if (net->port == 0) { 5856179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5857179157Srrs } 5858179157Srrs net->port = port; 5859179157Srrs } 5860163953Srrs /* inp's ref-count increased && stcb locked */ 5861163953Srrs if (inp == NULL) { 5862163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5863163953Srrs 5864163953Srrs SCTP_STAT_INCR(sctps_noport); 5865163953Srrs#ifdef ICMP_BANDLIM 5866163953Srrs /* 5867163953Srrs * we use the bandwidth limiting to protect against sending 5868163953Srrs * too many ABORTS all at once. In this case these count the 5869163953Srrs * same as an ICMP message. 5870163953Srrs */ 5871163953Srrs if (badport_bandlim(0) < 0) 5872163953Srrs goto bad; 5873163953Srrs#endif /* ICMP_BANDLIM */ 5874169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5875169420Srrs "Sending a ABORT from packet entry!\n"); 5876163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5877163953Srrs /* 5878163953Srrs * we do a trick here to get the INIT tag, dig in 5879163953Srrs * and get the tag from the INIT and put it in the 5880163953Srrs * common header. 5881163953Srrs */ 5882163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5883163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5884163953Srrs (uint8_t *) & chunk_buf); 5885163953Srrs if (init_chk != NULL) 5886163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5887163953Srrs } 5888165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5889179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5890165220Srrs goto bad; 5891165220Srrs } 5892165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5893165220Srrs goto bad; 5894165220Srrs } 5895165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5896179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5897163953Srrs goto bad; 5898163953Srrs } else if (stcb == NULL) { 5899163953Srrs refcount_up = 1; 5900163953Srrs } 5901171167Sgnn#ifdef IPSEC 5902163953Srrs /* 5903163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5904163953Srrs * idea, so I will leave it in place. 5905163953Srrs */ 5906163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5907197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5908163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5909163953Srrs goto bad; 5910163953Srrs } 5911163953Srrs#endif /* IPSEC */ 5912163953Srrs 5913163953Srrs /* 5914163953Srrs * common chunk processing 5915163953Srrs */ 5916163953Srrs length = ip->ip_len + iphlen; 5917163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5918163953Srrs 5919163953Srrs ecn_bits = ip->ip_tos; 5920163953Srrs 5921169655Srrs /* sa_ignore NO_NULL_CHK */ 5922163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5923179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5924163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5925163953Srrs if (m) { 5926163953Srrs sctp_m_freem(m); 5927163953Srrs } 5928163953Srrs if ((inp) && (refcount_up)) { 5929163953Srrs /* reduce ref-count */ 5930163953Srrs SCTP_INP_DECR_REF(inp); 5931163953Srrs } 5932163953Srrs return; 5933163953Srrsbad: 5934169420Srrs if (stcb) { 5935163953Srrs SCTP_TCB_UNLOCK(stcb); 5936169420Srrs } 5937163953Srrs if ((inp) && (refcount_up)) { 5938163953Srrs /* reduce ref-count */ 5939163953Srrs SCTP_INP_DECR_REF(inp); 5940163953Srrs } 5941163953Srrs if (m) { 5942163953Srrs sctp_m_freem(m); 5943163953Srrs } 5944163953Srrs return; 5945163953Srrs} 5946179157Srrsvoid 5947179157Srrssctp_input(i_pak, off) 5948179157Srrs struct mbuf *i_pak; 5949179157Srrs int off; 5950179157Srrs{ 5951179157Srrs sctp_input_with_port(i_pak, off, 0); 5952179157Srrs} 5953