sctp_input.c revision 218392
1163953Srrs/*- 2185694Srrs * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3218269Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. 4218269Srrs * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. 5163953Srrs * 6163953Srrs * Redistribution and use in source and binary forms, with or without 7163953Srrs * modification, are permitted provided that the following conditions are met: 8163953Srrs * 9163953Srrs * a) Redistributions of source code must retain the above copyright notice, 10163953Srrs * this list of conditions and the following disclaimer. 11163953Srrs * 12163953Srrs * b) Redistributions in binary form must reproduce the above copyright 13163953Srrs * notice, this list of conditions and the following disclaimer in 14163953Srrs * the documentation and/or other materials provided with the distribution. 15163953Srrs * 16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 17163953Srrs * contributors may be used to endorse or promote products derived 18163953Srrs * from this software without specific prior written permission. 19163953Srrs * 20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 31163953Srrs */ 32163953Srrs 33163953Srrs/* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */ 34163953Srrs 35163953Srrs#include <sys/cdefs.h> 36163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 218392 2011-02-07 08:10:29Z rrs $"); 37163953Srrs 38163953Srrs#include <netinet/sctp_os.h> 39163953Srrs#include <netinet/sctp_var.h> 40167598Srrs#include <netinet/sctp_sysctl.h> 41163953Srrs#include <netinet/sctp_pcb.h> 42163953Srrs#include <netinet/sctp_header.h> 43163953Srrs#include <netinet/sctputil.h> 44163953Srrs#include <netinet/sctp_output.h> 45163953Srrs#include <netinet/sctp_input.h> 46163953Srrs#include <netinet/sctp_auth.h> 47163953Srrs#include <netinet/sctp_indata.h> 48163953Srrs#include <netinet/sctp_asconf.h> 49170091Srrs#include <netinet/sctp_bsd_addr.h> 50172091Srrs#include <netinet/sctp_timer.h> 51188067Srrs#include <netinet/sctp_crc32.h> 52179157Srrs#include <netinet/udp.h> 53218211Srrs#include <sys/smp.h> 54163953Srrs 55163953Srrs 56163953Srrs 57163953Srrsstatic void 58163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 59163953Srrs{ 60163953Srrs struct sctp_nets *net; 61163953Srrs 62165220Srrs /* 63165220Srrs * This now not only stops all cookie timers it also stops any INIT 64165220Srrs * timers as well. This will make sure that the timers are stopped 65165220Srrs * in all collision cases. 66165220Srrs */ 67163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 68163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 69165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 70163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 71163953Srrs stcb->sctp_ep, 72163953Srrs stcb, 73165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 74165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 75165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 76165220Srrs stcb->sctp_ep, 77165220Srrs stcb, 78165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 79163953Srrs } 80163953Srrs } 81163953Srrs} 82163953Srrs 83163953Srrs/* INIT handler */ 84163953Srrsstatic void 85163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 86163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 87179157Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 88163953Srrs{ 89163953Srrs struct sctp_init *init; 90163953Srrs struct mbuf *op_err; 91163953Srrs uint32_t init_limit; 92163953Srrs 93169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 94169420Srrs stcb); 95172396Srrs if (stcb == NULL) { 96172396Srrs SCTP_INP_RLOCK(inp); 97172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 98172396Srrs goto outnow; 99172396Srrs } 100172396Srrs } 101163953Srrs op_err = NULL; 102163953Srrs init = &cp->init; 103163953Srrs /* First are we accepting? */ 104163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 105169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 106169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 107169420Srrs inp->sctp_socket->so_qlimit); 108163953Srrs /* 109163953Srrs * FIX ME ?? What about TCP model and we have a 110179141Srrs * match/restart case? Actually no fix is needed. the lookup 111179141Srrs * will always find the existing assoc so stcb would not be 112179141Srrs * NULL. It may be questionable to do this since we COULD 113179141Srrs * just send back the INIT-ACK and hope that the app did 114179141Srrs * accept()'s by the time the COOKIE was sent. But there is 115179141Srrs * a price to pay for COOKIE generation and I don't want to 116179141Srrs * pay it on the chance that the app will actually do some 117179141Srrs * accepts(). The App just looses and should NOT be in this 118179141Srrs * state :-) 119163953Srrs */ 120169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121179157Srrs vrf_id, port); 122168299Srrs if (stcb) 123168299Srrs *abort_no_unlock = 1; 124172396Srrs goto outnow; 125163953Srrs } 126163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 127163953Srrs /* Invalid length */ 128163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 129169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 130179157Srrs vrf_id, port); 131168299Srrs if (stcb) 132168299Srrs *abort_no_unlock = 1; 133172396Srrs goto outnow; 134163953Srrs } 135163953Srrs /* validate parameters */ 136163953Srrs if (init->initiate_tag == 0) { 137163953Srrs /* protocol error... send abort */ 138163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 139169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 140179157Srrs vrf_id, port); 141168299Srrs if (stcb) 142168299Srrs *abort_no_unlock = 1; 143172396Srrs goto outnow; 144163953Srrs } 145163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 146163953Srrs /* invalid parameter... send abort */ 147163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 148169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 149179157Srrs vrf_id, port); 150171440Srrs if (stcb) 151171440Srrs *abort_no_unlock = 1; 152172396Srrs goto outnow; 153163953Srrs } 154163953Srrs if (init->num_inbound_streams == 0) { 155163953Srrs /* protocol error... send abort */ 156163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 157169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 158179157Srrs vrf_id, port); 159168299Srrs if (stcb) 160168299Srrs *abort_no_unlock = 1; 161172396Srrs goto outnow; 162163953Srrs } 163163953Srrs if (init->num_outbound_streams == 0) { 164163953Srrs /* protocol error... send abort */ 165163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 166169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 167179157Srrs vrf_id, port); 168168299Srrs if (stcb) 169168299Srrs *abort_no_unlock = 1; 170172396Srrs goto outnow; 171163953Srrs } 172163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 173163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 174163953Srrs init_limit)) { 175163953Srrs /* auth parameter(s) error... send abort */ 176179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 177168299Srrs if (stcb) 178168299Srrs *abort_no_unlock = 1; 179172396Srrs goto outnow; 180163953Srrs } 181163953Srrs /* send an INIT-ACK w/cookie */ 182169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 183179157Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 184172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 185172396Srrsoutnow: 186172396Srrs if (stcb == NULL) { 187172396Srrs SCTP_INP_RUNLOCK(inp); 188172396Srrs } 189163953Srrs} 190163953Srrs 191163953Srrs/* 192163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 193163953Srrs */ 194171158Srrs 195171158Srrsint 196171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 197171158Srrs{ 198171158Srrs int unsent_data = 0; 199217760Stuexen unsigned int i; 200217760Stuexen struct sctp_stream_queue_pending *sp; 201171158Srrs struct sctp_association *asoc; 202171158Srrs 203171158Srrs /* 204171158Srrs * This function returns the number of streams that have true unsent 205171158Srrs * data on them. Note that as it looks through it will clean up any 206171158Srrs * places that have old data that has been sent but left at top of 207171158Srrs * stream queue. 208171158Srrs */ 209171158Srrs asoc = &stcb->asoc; 210171158Srrs SCTP_TCB_SEND_LOCK(stcb); 211217760Stuexen if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 212217760Stuexen /* Check to see if some data queued */ 213217760Stuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 214217760Stuexen /* sa_ignore FREED_MEMORY */ 215217760Stuexen sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 216217760Stuexen if (sp == NULL) { 217217760Stuexen continue; 218217760Stuexen } 219171158Srrs if ((sp->msg_is_complete) && 220171158Srrs (sp->length == 0) && 221171158Srrs (sp->sender_all_done)) { 222171158Srrs /* 223171158Srrs * We are doing differed cleanup. Last time 224171158Srrs * through when we took all the data the 225171158Srrs * sender_all_done was not set. 226171158Srrs */ 227171158Srrs if (sp->put_last_out == 0) { 228171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 229171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 230171158Srrs sp->sender_all_done, 231171158Srrs sp->length, 232171158Srrs sp->msg_is_complete, 233171158Srrs sp->put_last_out); 234171158Srrs } 235217760Stuexen atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 236217760Stuexen TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 237212712Stuexen if (sp->net) { 238212712Stuexen sctp_free_remote_addr(sp->net); 239212712Stuexen sp->net = NULL; 240212712Stuexen } 241171158Srrs if (sp->data) { 242171158Srrs sctp_m_freem(sp->data); 243171158Srrs sp->data = NULL; 244171158Srrs } 245171158Srrs sctp_free_a_strmoq(stcb, sp); 246171158Srrs } else { 247171158Srrs unsent_data++; 248216822Stuexen break; 249171158Srrs } 250171158Srrs } 251171158Srrs } 252171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 253171158Srrs return (unsent_data); 254171158Srrs} 255171158Srrs 256163953Srrsstatic int 257163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 258163953Srrs struct sctp_nets *net) 259163953Srrs{ 260163953Srrs struct sctp_init *init; 261163953Srrs struct sctp_association *asoc; 262163953Srrs struct sctp_nets *lnet; 263163953Srrs unsigned int i; 264163953Srrs 265163953Srrs init = &cp->init; 266163953Srrs asoc = &stcb->asoc; 267163953Srrs /* save off parameters */ 268163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 269163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 270218129Srrs /* init tsn's */ 271218129Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 272218129Srrs 273212712Stuexen if (!TAILQ_EMPTY(&asoc->nets)) { 274163953Srrs /* update any ssthresh's that may have a default */ 275163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 276163953Srrs lnet->ssthresh = asoc->peers_rwnd; 277179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 278170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 279170744Srrs } 280163953Srrs } 281163953Srrs } 282164181Srrs SCTP_TCB_SEND_LOCK(stcb); 283163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 284163953Srrs unsigned int newcnt; 285163953Srrs struct sctp_stream_out *outs; 286216822Stuexen struct sctp_stream_queue_pending *sp, *nsp; 287216822Stuexen struct sctp_tmit_chunk *chk, *nchk; 288163953Srrs 289196260Stuexen /* abandon the upper streams */ 290163953Srrs newcnt = ntohs(init->num_inbound_streams); 291216822Stuexen TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 292216822Stuexen if (chk->rec.data.stream_number >= newcnt) { 293216822Stuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 294216822Stuexen asoc->send_queue_cnt--; 295216822Stuexen if (chk->data != NULL) { 296216822Stuexen sctp_free_bufspace(stcb, asoc, chk, 1); 297216822Stuexen sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 298216822Stuexen SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 299216822Stuexen if (chk->data) { 300216822Stuexen sctp_m_freem(chk->data); 301216822Stuexen chk->data = NULL; 302196260Stuexen } 303196260Stuexen } 304216822Stuexen sctp_free_a_chunk(stcb, chk); 305216822Stuexen /* sa_ignore FREED_MEMORY */ 306196260Stuexen } 307196260Stuexen } 308163953Srrs if (asoc->strmout) { 309163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 310163953Srrs outs = &asoc->strmout[i]; 311216822Stuexen TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 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 } 321212712Stuexen if (sp->net) { 322212712Stuexen sctp_free_remote_addr(sp->net); 323212712Stuexen sp->net = NULL; 324212712Stuexen } 325163953Srrs /* Free the chunk */ 326163953Srrs sctp_free_a_strmoq(stcb, sp); 327169655Srrs /* sa_ignore FREED_MEMORY */ 328163953Srrs } 329163953Srrs } 330163953Srrs } 331196260Stuexen /* cut back the count */ 332163953Srrs asoc->pre_open_streams = newcnt; 333163953Srrs } 334164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 335188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 336218129Srrs 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; 347218129Srrs 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 */ 353216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 354164181Srrs 355164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 356216822Stuexen TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 357164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 358164181Srrs sctp_free_remote_addr(ctl->whoFrom); 359171158Srrs ctl->whoFrom = NULL; 360164181Srrs sctp_m_freem(ctl->data); 361164181Srrs ctl->data = NULL; 362164181Srrs sctp_free_a_readq(stcb, ctl); 363164181Srrs } 364164181Srrs } 365170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 366163953Srrs } 367164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 368164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 369164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 370164181Srrs } 371163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 372170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 373163953Srrs if (asoc->strmin == NULL) { 374163953Srrs /* we didn't get memory for the streams! */ 375169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 376163953Srrs return (-1); 377163953Srrs } 378163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 379163953Srrs asoc->strmin[i].stream_no = i; 380163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 381163953Srrs /* 382163953Srrs * U-stream ranges will be set when the cookie is unpacked. 383163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 384163953Srrs * supported) when the INIT-ACK arrives. 385163953Srrs */ 386163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 387168943Srrs asoc->strmin[i].delivery_started = 0; 388163953Srrs } 389163953Srrs /* 390163953Srrs * load_address_from_init will put the addresses into the 391163953Srrs * association when the COOKIE is processed or the INIT-ACK is 392163953Srrs * processed. Both types of COOKIE's existing and new call this 393163953Srrs * routine. It will remove addresses that are no longer in the 394163953Srrs * association (for the restarting case where addresses are 395163953Srrs * removed). Up front when the INIT arrives we will discard it if it 396163953Srrs * is a restart and new addresses have been added. 397163953Srrs */ 398169655Srrs /* sa_ignore MEMLEAK */ 399163953Srrs return (0); 400163953Srrs} 401163953Srrs 402163953Srrs/* 403163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 404163953Srrs */ 405163953Srrsstatic int 406163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 407163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 408170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 409163953Srrs{ 410163953Srrs struct sctp_association *asoc; 411163953Srrs struct mbuf *op_err; 412163953Srrs int retval, abort_flag; 413163953Srrs uint32_t initack_limit; 414185694Srrs int nat_friendly = 0; 415163953Srrs 416163953Srrs /* First verify that we have no illegal param's */ 417163953Srrs abort_flag = 0; 418163953Srrs op_err = NULL; 419163953Srrs 420163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 421163953Srrs (offset + sizeof(struct sctp_init_chunk)), 422185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 423163953Srrs if (abort_flag) { 424163953Srrs /* Send an abort and notify peer */ 425172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 426170056Srrs *abort_no_unlock = 1; 427163953Srrs return (-1); 428163953Srrs } 429163953Srrs asoc = &stcb->asoc; 430185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 431163953Srrs /* process the peer's parameters in the INIT-ACK */ 432163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 433163953Srrs if (retval < 0) { 434163953Srrs return (retval); 435163953Srrs } 436163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 437163953Srrs /* load all addresses */ 438163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 439163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 440163953Srrs NULL))) { 441163953Srrs /* Huh, we should abort */ 442169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 443169420Srrs "Load addresses from INIT causes an abort %d\n", 444169420Srrs retval); 445163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 446179157Srrs NULL, 0, net->port); 447168299Srrs *abort_no_unlock = 1; 448163953Srrs return (-1); 449163953Srrs } 450171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 451171477Srrs if (asoc->peer_supports_asconf == 0) { 452216822Stuexen struct sctp_asconf_addr *param, *nparam; 453171477Srrs 454216822Stuexen TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 455216822Stuexen TAILQ_REMOVE(&asoc->asconf_queue, param, next); 456216822Stuexen SCTP_FREE(param, SCTP_M_ASC_ADDR); 457171477Srrs } 458171477Srrs } 459163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 460163953Srrs stcb->asoc.local_hmacs); 461163953Srrs if (op_err) { 462163953Srrs sctp_queue_op_err(stcb, op_err); 463163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 464163953Srrs op_err = NULL; 465163953Srrs } 466163953Srrs /* extract the cookie and queue it to "echo" it back... */ 467179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 468171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 469171943Srrs stcb->asoc.overall_error_count, 470171943Srrs 0, 471171943Srrs SCTP_FROM_SCTP_INPUT, 472171943Srrs __LINE__); 473171943Srrs } 474163953Srrs stcb->asoc.overall_error_count = 0; 475163953Srrs net->error_count = 0; 476163953Srrs 477163953Srrs /* 478163953Srrs * Cancel the INIT timer, We do this first before queueing the 479163953Srrs * cookie. We always cancel at the primary to assue that we are 480163953Srrs * canceling the timer started by the INIT which always goes to the 481163953Srrs * primary. 482163953Srrs */ 483163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 484165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 485163953Srrs 486165220Srrs /* calculate the RTO */ 487218186Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 488218186Srrs SCTP_DETERMINE_LL_NOTOK); 489165220Srrs 490163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 491163953Srrs if (retval < 0) { 492163953Srrs /* 493163953Srrs * No cookie, we probably should send a op error. But in any 494163953Srrs * case if there is no cookie in the INIT-ACK, we can 495163953Srrs * abandon the peer, its broke. 496163953Srrs */ 497163953Srrs if (retval == -3) { 498163953Srrs /* We abort with an error of missing mandatory param */ 499163953Srrs op_err = 500163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 501163953Srrs if (op_err) { 502163953Srrs /* 503163953Srrs * Expand beyond to include the mandatory 504163953Srrs * param cookie 505163953Srrs */ 506163953Srrs struct sctp_inv_mandatory_param *mp; 507163953Srrs 508165647Srrs SCTP_BUF_LEN(op_err) = 509163953Srrs sizeof(struct sctp_inv_mandatory_param); 510163953Srrs mp = mtod(op_err, 511163953Srrs struct sctp_inv_mandatory_param *); 512163953Srrs /* Subtract the reserved param */ 513163953Srrs mp->length = 514163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 515163953Srrs mp->num_param = htonl(1); 516163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 517163953Srrs mp->resv = 0; 518163953Srrs } 519163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 520179157Srrs sh, op_err, 0, net->port); 521168299Srrs *abort_no_unlock = 1; 522163953Srrs } 523163953Srrs return (retval); 524163953Srrs } 525163953Srrs return (0); 526163953Srrs} 527163953Srrs 528163953Srrsstatic void 529163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 530163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 531163953Srrs{ 532163953Srrs struct sockaddr_storage store; 533163953Srrs struct sockaddr_in *sin; 534163953Srrs struct sockaddr_in6 *sin6; 535212225Srrs struct sctp_nets *r_net, *f_net; 536163953Srrs struct timeval tv; 537172396Srrs int req_prim = 0; 538163953Srrs 539163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 540163953Srrs /* Invalid length */ 541163953Srrs return; 542163953Srrs } 543163953Srrs sin = (struct sockaddr_in *)&store; 544163953Srrs sin6 = (struct sockaddr_in6 *)&store; 545163953Srrs 546163953Srrs memset(&store, 0, sizeof(store)); 547163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 548163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 549163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 550163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 551163953Srrs sin->sin_port = stcb->rport; 552163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 553163953Srrs sizeof(sin->sin_addr)); 554163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 555163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 556163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 557163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 558163953Srrs sin6->sin6_port = stcb->rport; 559163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 560163953Srrs sizeof(sin6->sin6_addr)); 561163953Srrs } else { 562163953Srrs return; 563163953Srrs } 564163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 565163953Srrs if (r_net == NULL) { 566169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 567163953Srrs return; 568163953Srrs } 569163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 570163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 571163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 572163953Srrs /* 573163953Srrs * If the its a HB and it's random value is correct when can 574163953Srrs * confirm the destination. 575163953Srrs */ 576163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 577167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 578167598Srrs stcb->asoc.primary_destination = r_net; 579167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 580167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 581212225Srrs f_net = TAILQ_FIRST(&stcb->asoc.nets); 582212225Srrs if (f_net != r_net) { 583167598Srrs /* 584167598Srrs * first one on the list is NOT the primary 585167598Srrs * sctp_cmpaddr() is much more efficent if 586167598Srrs * the primary is the first on the list, 587167598Srrs * make it so. 588167598Srrs */ 589212225Srrs TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 590212225Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 591167598Srrs } 592172396Srrs req_prim = 1; 593167598Srrs } 594163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 595172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 596163953Srrs } 597163953Srrs r_net->error_count = 0; 598163953Srrs r_net->hb_responded = 1; 599163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 600163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 601163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 602163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 603163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 604163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 605172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 606163953Srrs /* now was it the primary? if so restore */ 607163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 608169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 609163953Srrs } 610163953Srrs } 611171440Srrs /* 612171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 613171440Srrs * set the destination to active state and set the cwnd to one or 614171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 615171440Srrs * timer is running, for the destination, stop the timer because a 616171440Srrs * PF-heartbeat was received. 617171440Srrs */ 618216669Stuexen if ((stcb->asoc.sctp_cmt_on_off > 0) && 619211944Stuexen (stcb->asoc.sctp_cmt_pf > 0) && 620211944Stuexen ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) { 621171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 622171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 623171440Srrs stcb, net, 624171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 625171440Srrs } 626171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 627211944Stuexen net->cwnd = net->mtu * stcb->asoc.sctp_cmt_pf; 628171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 629171440Srrs net, net->cwnd); 630171440Srrs } 631163953Srrs /* Now lets do a RTO with this */ 632218186Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 633218186Srrs SCTP_DETERMINE_LL_OK); 634172396Srrs /* Mobility adaptation */ 635172396Srrs if (req_prim) { 636172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 637172396Srrs SCTP_MOBILITY_BASE) || 638172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 639172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 640172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 641172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 642172396Srrs 643172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 644172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 645172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 646172396Srrs sctp_assoc_immediate_retrans(stcb, 647172396Srrs stcb->asoc.primary_destination); 648172396Srrs } 649172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 650172396Srrs SCTP_MOBILITY_BASE)) { 651212712Stuexen sctp_move_chunks_from_net(stcb, 652212712Stuexen stcb->asoc.deleted_primary); 653172396Srrs } 654172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 655172396Srrs stcb->asoc.deleted_primary); 656172396Srrs } 657172396Srrs } 658163953Srrs} 659163953Srrs 660185694Srrsstatic int 661185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 662185694Srrs{ 663185694Srrs /* 664185694Srrs * return 0 means we want you to proceed with the abort non-zero 665185694Srrs * means no abort processing 666185694Srrs */ 667185694Srrs struct sctpasochead *head; 668185694Srrs 669185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 670185694Srrs /* generate a new vtag and send init */ 671185694Srrs LIST_REMOVE(stcb, sctp_asocs); 672185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 673185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 674185694Srrs /* 675185694Srrs * put it in the bucket in the vtag hash of assoc's for the 676185694Srrs * system 677185694Srrs */ 678185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 679185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 680185694Srrs return (1); 681185694Srrs } 682185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 683185694Srrs /* 684185694Srrs * treat like a case where the cookie expired i.e.: - dump 685185694Srrs * current cookie. - generate a new vtag. - resend init. 686185694Srrs */ 687185694Srrs /* generate a new vtag and send init */ 688185694Srrs LIST_REMOVE(stcb, sctp_asocs); 689185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 690185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 691185694Srrs sctp_stop_all_cookie_timers(stcb); 692185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 693185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 694185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 695185694Srrs /* 696185694Srrs * put it in the bucket in the vtag hash of assoc's for the 697185694Srrs * system 698185694Srrs */ 699185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 700185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 701185694Srrs return (1); 702185694Srrs } 703185694Srrs return (0); 704185694Srrs} 705185694Srrs 706185694Srrsstatic int 707185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 708185694Srrs struct sctp_nets *net) 709185694Srrs{ 710185694Srrs /* 711185694Srrs * return 0 means we want you to proceed with the abort non-zero 712185694Srrs * means no abort processing 713185694Srrs */ 714185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 715185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 716185694Srrs return (0); 717185694Srrs } 718185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 719185694Srrs return (1); 720185694Srrs} 721185694Srrs 722185694Srrs 723163953Srrsstatic void 724163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 725163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 726163953Srrs{ 727172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 728172090Srrs struct socket *so; 729172090Srrs 730172090Srrs#endif 731185694Srrs uint16_t len; 732172090Srrs 733169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 734163953Srrs if (stcb == NULL) 735163953Srrs return; 736163953Srrs 737185694Srrs len = ntohs(cp->ch.chunk_length); 738185694Srrs if (len > sizeof(struct sctp_chunkhdr)) { 739185694Srrs /* 740185694Srrs * Need to check the cause codes for our two magic nat 741185694Srrs * aborts which don't kill the assoc necessarily. 742185694Srrs */ 743185694Srrs struct sctp_abort_chunk *cpnext; 744185694Srrs struct sctp_missing_nat_state *natc; 745185694Srrs uint16_t cause; 746185694Srrs 747185694Srrs cpnext = cp; 748185694Srrs cpnext++; 749185694Srrs natc = (struct sctp_missing_nat_state *)cpnext; 750185694Srrs cause = ntohs(natc->cause); 751185694Srrs if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 752185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 753185694Srrs cp->ch.chunk_flags); 754185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 755185694Srrs return; 756185694Srrs } 757185694Srrs } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 758185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 759185694Srrs cp->ch.chunk_flags); 760185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 761185694Srrs return; 762185694Srrs } 763185694Srrs } 764185694Srrs } 765163953Srrs /* stop any receive timers */ 766171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 767163953Srrs /* notify user of the abort and clean up... */ 768172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 769163953Srrs /* free the tcb */ 770172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 771172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 772172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 773172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 774172396Srrs panic("Received an ABORT"); 775172396Srrs } else { 776172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 777172396Srrs } 778172396Srrs#endif 779163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 780163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 781163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 782163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 783163953Srrs } 784168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 785168859Srrs sctp_print_out_track_log(stcb); 786168859Srrs#endif 787172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 788172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 789172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 790172090Srrs SCTP_TCB_UNLOCK(stcb); 791172090Srrs SCTP_SOCKET_LOCK(so, 1); 792172090Srrs SCTP_TCB_LOCK(stcb); 793172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 794172090Srrs#endif 795171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 796171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 797171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 798172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 799172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 800172090Srrs#endif 801169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 802163953Srrs} 803163953Srrs 804163953Srrsstatic void 805163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 806163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 807163953Srrs{ 808163953Srrs struct sctp_association *asoc; 809163953Srrs int some_on_streamwheel; 810163953Srrs 811172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 812172090Srrs struct socket *so; 813172090Srrs 814172090Srrs#endif 815172090Srrs 816169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 817169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 818163953Srrs if (stcb == NULL) 819163953Srrs return; 820165220Srrs asoc = &stcb->asoc; 821165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 822165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 823163953Srrs return; 824163953Srrs } 825163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 826163953Srrs /* Shutdown NOT the expected size */ 827163953Srrs return; 828163953Srrs } else { 829163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 830199477Stuexen if (*abort_flag) { 831199477Stuexen return; 832199477Stuexen } 833163953Srrs } 834165220Srrs if (asoc->control_pdapi) { 835163953Srrs /* 836163953Srrs * With a normal shutdown we assume the end of last record. 837163953Srrs */ 838163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 839165220Srrs asoc->control_pdapi->end_added = 1; 840165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 841165220Srrs asoc->control_pdapi = NULL; 842163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 843172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 844172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 845172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 846172090Srrs SCTP_TCB_UNLOCK(stcb); 847172090Srrs SCTP_SOCKET_LOCK(so, 1); 848172090Srrs SCTP_TCB_LOCK(stcb); 849172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 850172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 851172090Srrs /* assoc was freed while we were unlocked */ 852172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 853172090Srrs return; 854172090Srrs } 855172090Srrs#endif 856163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 857172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 858172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 859172090Srrs#endif 860163953Srrs } 861163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 862163953Srrs if (stcb->sctp_socket) { 863163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 864170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 865163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 866171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 867172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 868163953Srrs /* 869163953Srrs * notify upper layer that peer has initiated a 870163953Srrs * shutdown 871163953Srrs */ 872172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 873163953Srrs 874163953Srrs /* reset time */ 875169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 876163953Srrs } 877163953Srrs } 878163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 879163953Srrs /* 880163953Srrs * stop the shutdown timer, since we WILL move to 881163953Srrs * SHUTDOWN-ACK-SENT. 882163953Srrs */ 883171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 884163953Srrs } 885171158Srrs /* Now is there unsent data on a stream somewhere? */ 886171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 887163953Srrs 888163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 889163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 890163953Srrs some_on_streamwheel) { 891163953Srrs /* By returning we will push more data out */ 892163953Srrs return; 893163953Srrs } else { 894163953Srrs /* no outstanding data to send, so move on... */ 895163953Srrs /* send SHUTDOWN-ACK */ 896163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 897163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 898166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 899166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 900163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 901163953Srrs } 902171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 903172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 904214918Stuexen sctp_stop_timers_for_shutdown(stcb); 905163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 906163953Srrs stcb, net); 907163953Srrs } 908163953Srrs} 909163953Srrs 910163953Srrsstatic void 911163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 912204141Stuexen struct sctp_tcb *stcb, 913204141Stuexen struct sctp_nets *net) 914163953Srrs{ 915163953Srrs struct sctp_association *asoc; 916163953Srrs 917172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 918172090Srrs struct socket *so; 919172090Srrs 920172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 921172090Srrs#endif 922169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 923169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 924163953Srrs if (stcb == NULL) 925163953Srrs return; 926163953Srrs 927163953Srrs asoc = &stcb->asoc; 928163953Srrs /* process according to association state */ 929204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 930204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 931204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 932204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 933204141Stuexen SCTP_TCB_UNLOCK(stcb); 934204141Stuexen return; 935204141Stuexen } 936163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 937163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 938163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 939163953Srrs SCTP_TCB_UNLOCK(stcb); 940163953Srrs return; 941163953Srrs } 942165220Srrs if (asoc->control_pdapi) { 943163953Srrs /* 944163953Srrs * With a normal shutdown we assume the end of last record. 945163953Srrs */ 946163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 947165220Srrs asoc->control_pdapi->end_added = 1; 948165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 949165220Srrs asoc->control_pdapi = NULL; 950163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 951172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 952172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 953172090Srrs SCTP_TCB_UNLOCK(stcb); 954172090Srrs SCTP_SOCKET_LOCK(so, 1); 955172090Srrs SCTP_TCB_LOCK(stcb); 956172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 957172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 958172090Srrs /* assoc was freed while we were unlocked */ 959172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 960172090Srrs return; 961172090Srrs } 962172090Srrs#endif 963163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 964172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 965172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 966172090Srrs#endif 967163953Srrs } 968163953Srrs /* are the queues empty? */ 969163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 970163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 971217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 972172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 973163953Srrs } 974163953Srrs /* stop the timer */ 975171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 976163953Srrs /* send SHUTDOWN-COMPLETE */ 977204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 978163953Srrs /* notify upper layer protocol */ 979163953Srrs if (stcb->sctp_socket) { 980172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 981163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 982163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 983163953Srrs /* Set the connected flag to disconnected */ 984163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 985163953Srrs } 986163953Srrs } 987163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 988163953Srrs /* free the TCB but first save off the ep */ 989172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 990172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 991172090Srrs SCTP_TCB_UNLOCK(stcb); 992172090Srrs SCTP_SOCKET_LOCK(so, 1); 993172090Srrs SCTP_TCB_LOCK(stcb); 994172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 995172090Srrs#endif 996171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 997171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 998172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 999172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1000172090Srrs#endif 1001163953Srrs} 1002163953Srrs 1003163953Srrs/* 1004163953Srrs * Skip past the param header and then we will find the chunk that caused the 1005163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1006163953Srrs * our peer must be broken. 1007163953Srrs */ 1008163953Srrsstatic void 1009163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1010163953Srrs struct sctp_nets *net) 1011163953Srrs{ 1012163953Srrs struct sctp_chunkhdr *chk; 1013163953Srrs 1014163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1015163953Srrs switch (chk->chunk_type) { 1016163953Srrs case SCTP_ASCONF_ACK: 1017163953Srrs case SCTP_ASCONF: 1018163953Srrs sctp_asconf_cleanup(stcb, net); 1019163953Srrs break; 1020163953Srrs case SCTP_FORWARD_CUM_TSN: 1021163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1022163953Srrs break; 1023163953Srrs default: 1024169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1025169420Srrs "Peer does not support chunk type %d(%x)??\n", 1026169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1027163953Srrs break; 1028163953Srrs } 1029163953Srrs} 1030163953Srrs 1031163953Srrs/* 1032163953Srrs * Skip past the param header and then we will find the param that caused the 1033163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1034163953Srrs * these will turn of specific features. 1035163953Srrs */ 1036163953Srrsstatic void 1037163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1038163953Srrs{ 1039163953Srrs struct sctp_paramhdr *pbad; 1040163953Srrs 1041163953Srrs pbad = phdr + 1; 1042163953Srrs switch (ntohs(pbad->param_type)) { 1043163953Srrs /* pr-sctp draft */ 1044163953Srrs case SCTP_PRSCTP_SUPPORTED: 1045163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1046163953Srrs break; 1047163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1048163953Srrs break; 1049163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1050185694Srrs case SCTP_HAS_NAT_SUPPORT: 1051185694Srrs stcb->asoc.peer_supports_nat = 0; 1052185694Srrs break; 1053163953Srrs case SCTP_ADD_IP_ADDRESS: 1054163953Srrs case SCTP_DEL_IP_ADDRESS: 1055163953Srrs case SCTP_SET_PRIM_ADDR: 1056163953Srrs stcb->asoc.peer_supports_asconf = 0; 1057163953Srrs break; 1058163953Srrs case SCTP_SUCCESS_REPORT: 1059163953Srrs case SCTP_ERROR_CAUSE_IND: 1060169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1061169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1062169420Srrs "Turning off ASCONF to this strange peer\n"); 1063163953Srrs stcb->asoc.peer_supports_asconf = 0; 1064163953Srrs break; 1065163953Srrs default: 1066169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1067169420Srrs "Peer does not support param type %d(%x)??\n", 1068169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1069163953Srrs break; 1070163953Srrs } 1071163953Srrs} 1072163953Srrs 1073163953Srrsstatic int 1074163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1075163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1076163953Srrs{ 1077163953Srrs int chklen; 1078163953Srrs struct sctp_paramhdr *phdr; 1079163953Srrs uint16_t error_type; 1080163953Srrs uint16_t error_len; 1081163953Srrs struct sctp_association *asoc; 1082163953Srrs int adjust; 1083163953Srrs 1084172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1085172090Srrs struct socket *so; 1086172090Srrs 1087172090Srrs#endif 1088172090Srrs 1089163953Srrs /* parse through all of the errors and process */ 1090163953Srrs asoc = &stcb->asoc; 1091163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1092163953Srrs sizeof(struct sctp_chunkhdr)); 1093163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1094163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1095163953Srrs /* Process an Error Cause */ 1096163953Srrs error_type = ntohs(phdr->param_type); 1097163953Srrs error_len = ntohs(phdr->param_length); 1098163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1099163953Srrs /* invalid param length for this param */ 1100169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1101169420Srrs chklen, error_len); 1102163953Srrs return (0); 1103163953Srrs } 1104163953Srrs switch (error_type) { 1105163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1106163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1107163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1108163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1109169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1110169420Srrs error_type); 1111163953Srrs break; 1112185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1113185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1114185694Srrs ch->chunk_flags); 1115185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1116185694Srrs return (0); 1117185694Srrs } 1118185694Srrs break; 1119185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1120185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1121185694Srrs ch->chunk_flags); 1122185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1123185694Srrs return (0); 1124185694Srrs } 1125185694Srrs break; 1126163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1127163953Srrs /* 1128163953Srrs * We only act if we have echoed a cookie and are 1129163953Srrs * waiting. 1130163953Srrs */ 1131163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1132163953Srrs int *p; 1133163953Srrs 1134163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1135163953Srrs /* Save the time doubled */ 1136163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1137163953Srrs asoc->stale_cookie_count++; 1138163953Srrs if (asoc->stale_cookie_count > 1139163953Srrs asoc->max_init_times) { 1140172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1141163953Srrs /* now free the asoc */ 1142172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1143172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1144172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1145172090Srrs SCTP_TCB_UNLOCK(stcb); 1146172090Srrs SCTP_SOCKET_LOCK(so, 1); 1147172090Srrs SCTP_TCB_LOCK(stcb); 1148172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1149172090Srrs#endif 1150171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1151171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1152172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1153172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1154172090Srrs#endif 1155163953Srrs return (-1); 1156163953Srrs } 1157163953Srrs /* blast back to INIT state */ 1158185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1159163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1160163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1161163953Srrs sctp_stop_all_cookie_timers(stcb); 1162172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1163163953Srrs } 1164163953Srrs break; 1165163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1166163953Srrs /* 1167163953Srrs * Nothing we can do here, we don't do hostname 1168163953Srrs * addresses so if the peer does not like my IPv6 1169163953Srrs * (or IPv4 for that matter) it does not matter. If 1170163953Srrs * they don't support that type of address, they can 1171163953Srrs * NOT possibly get that packet type... i.e. with no 1172163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1173163953Srrs * safely ignore this one. If we ever added support 1174163953Srrs * for HOSTNAME Addresses, then we would need to do 1175163953Srrs * something here. 1176163953Srrs */ 1177163953Srrs break; 1178163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1179163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1180163953Srrs break; 1181163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1182163953Srrs sctp_process_unrecog_param(stcb, phdr); 1183163953Srrs break; 1184163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1185163953Srrs /* 1186163953Srrs * We ignore this since the timer will drive out a 1187163953Srrs * new cookie anyway and there timer will drive us 1188163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1189163953Srrs * here since we don't have their tag. 1190163953Srrs */ 1191163953Srrs break; 1192163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1193163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1194163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1195163953Srrs /* 1196163953Srrs * We should NOT get these here, but in a 1197166086Srrs * ASCONF-ACK. 1198163953Srrs */ 1199169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1200169420Srrs error_type); 1201163953Srrs break; 1202163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1203163953Srrs /* 1204163953Srrs * And what, pray tell do we do with the fact that 1205163953Srrs * the peer is out of resources? Not really sure we 1206166086Srrs * could do anything but abort. I suspect this 1207163953Srrs * should have came WITH an abort instead of in a 1208163953Srrs * OP-ERROR. 1209163953Srrs */ 1210163953Srrs break; 1211163953Srrs default: 1212169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1213169420Srrs error_type); 1214163953Srrs break; 1215163953Srrs } 1216163953Srrs adjust = SCTP_SIZE32(error_len); 1217163953Srrs chklen -= adjust; 1218163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1219163953Srrs } 1220163953Srrs return (0); 1221163953Srrs} 1222163953Srrs 1223163953Srrsstatic int 1224169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1225169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1226170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1227163953Srrs{ 1228163953Srrs struct sctp_init_ack *init_ack; 1229163953Srrs struct mbuf *op_err; 1230163953Srrs 1231169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1232169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1233169420Srrs 1234163953Srrs if (stcb == NULL) { 1235169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1236169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1237163953Srrs return (-1); 1238163953Srrs } 1239163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1240163953Srrs /* Invalid length */ 1241163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1242163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1243179157Srrs op_err, 0, net->port); 1244168299Srrs *abort_no_unlock = 1; 1245163953Srrs return (-1); 1246163953Srrs } 1247163953Srrs init_ack = &cp->init; 1248163953Srrs /* validate parameters */ 1249163953Srrs if (init_ack->initiate_tag == 0) { 1250163953Srrs /* protocol error... send an abort */ 1251163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1252163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1253179157Srrs op_err, 0, net->port); 1254168299Srrs *abort_no_unlock = 1; 1255163953Srrs return (-1); 1256163953Srrs } 1257163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1258163953Srrs /* protocol error... send an abort */ 1259163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1260163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1261179157Srrs op_err, 0, net->port); 1262168299Srrs *abort_no_unlock = 1; 1263163953Srrs return (-1); 1264163953Srrs } 1265163953Srrs if (init_ack->num_inbound_streams == 0) { 1266163953Srrs /* protocol error... send an abort */ 1267163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1268163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1269179157Srrs op_err, 0, net->port); 1270168299Srrs *abort_no_unlock = 1; 1271163953Srrs return (-1); 1272163953Srrs } 1273163953Srrs if (init_ack->num_outbound_streams == 0) { 1274163953Srrs /* protocol error... send an abort */ 1275163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1276163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1277179157Srrs op_err, 0, net->port); 1278168299Srrs *abort_no_unlock = 1; 1279163953Srrs return (-1); 1280163953Srrs } 1281163953Srrs /* process according to association state... */ 1282171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1283163953Srrs case SCTP_STATE_COOKIE_WAIT: 1284163953Srrs /* this is the expected state for this chunk */ 1285163953Srrs /* process the INIT-ACK parameters */ 1286163953Srrs if (stcb->asoc.primary_destination->dest_state & 1287163953Srrs SCTP_ADDR_UNCONFIRMED) { 1288163953Srrs /* 1289163953Srrs * The primary is where we sent the INIT, we can 1290163953Srrs * always consider it confirmed when the INIT-ACK is 1291163953Srrs * returned. Do this before we load addresses 1292163953Srrs * though. 1293163953Srrs */ 1294163953Srrs stcb->asoc.primary_destination->dest_state &= 1295163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1296163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1297172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1298163953Srrs } 1299169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1300170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1301163953Srrs /* error in parsing parameters */ 1302163953Srrs return (-1); 1303163953Srrs } 1304163953Srrs /* update our state */ 1305169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1306171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1307163953Srrs 1308163953Srrs /* reset the RTO calc */ 1309179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1310171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1311171943Srrs stcb->asoc.overall_error_count, 1312171943Srrs 0, 1313171943Srrs SCTP_FROM_SCTP_INPUT, 1314171943Srrs __LINE__); 1315171943Srrs } 1316163953Srrs stcb->asoc.overall_error_count = 0; 1317169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1318163953Srrs /* 1319163953Srrs * collapse the init timer back in case of a exponential 1320165220Srrs * backoff 1321163953Srrs */ 1322163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1323163953Srrs stcb, net); 1324163953Srrs /* 1325163953Srrs * the send at the end of the inbound data processing will 1326163953Srrs * cause the cookie to be sent 1327163953Srrs */ 1328163953Srrs break; 1329163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1330163953Srrs /* incorrect state... discard */ 1331163953Srrs break; 1332163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1333163953Srrs /* incorrect state... discard */ 1334163953Srrs break; 1335163953Srrs case SCTP_STATE_OPEN: 1336163953Srrs /* incorrect state... discard */ 1337163953Srrs break; 1338163953Srrs case SCTP_STATE_EMPTY: 1339163953Srrs case SCTP_STATE_INUSE: 1340163953Srrs default: 1341163953Srrs /* incorrect state... discard */ 1342163953Srrs return (-1); 1343163953Srrs break; 1344163953Srrs } 1345169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1346163953Srrs return (0); 1347163953Srrs} 1348163953Srrs 1349185694Srrsstatic struct sctp_tcb * 1350185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1351185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1352185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1353185694Srrs struct sockaddr *init_src, int *notification, 1354185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1355185694Srrs uint32_t vrf_id, uint16_t port); 1356163953Srrs 1357185694Srrs 1358163953Srrs/* 1359163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1360163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1361163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1362163953Srrs * mbuf to the cookie-echo chunk 1363163953Srrs */ 1364163953Srrsstatic struct sctp_tcb * 1365163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1366163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1367185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1368169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1369185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1370163953Srrs{ 1371163953Srrs struct sctp_association *asoc; 1372163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1373163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1374185694Srrs struct sctp_nets *net; 1375185694Srrs struct mbuf *op_err; 1376185694Srrs struct sctp_paramhdr *ph; 1377163953Srrs int chk_length; 1378165220Srrs int init_offset, initack_offset, i; 1379163953Srrs int retval; 1380164205Srrs int spec_flag = 0; 1381170140Srrs uint32_t how_indx; 1382163953Srrs 1383185694Srrs net = *netp; 1384163953Srrs /* I know that the TCB is non-NULL from the caller */ 1385163953Srrs asoc = &stcb->asoc; 1386166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1387166023Srrs if (asoc->cookie_how[how_indx] == 0) 1388166023Srrs break; 1389166023Srrs } 1390166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1391166023Srrs asoc->cookie_how[how_indx] = 1; 1392166023Srrs } 1393163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1394163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1395163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1396163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1397165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1398163953Srrs if (op_err == NULL) { 1399163953Srrs /* FOOBAR */ 1400163953Srrs return (NULL); 1401163953Srrs } 1402163953Srrs /* Set the len */ 1403165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1404163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1405163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1406163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1407169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1408179157Srrs vrf_id, net->port); 1409166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1410166023Srrs asoc->cookie_how[how_indx] = 2; 1411163953Srrs return (NULL); 1412163953Srrs } 1413163953Srrs /* 1414163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1415163953Srrs * INIT should start after the cookie-echo header struct (chunk 1416163953Srrs * header, state cookie header struct) 1417163953Srrs */ 1418163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1419163953Srrs 1420163953Srrs init_cp = (struct sctp_init_chunk *) 1421163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1422163953Srrs (uint8_t *) & init_buf); 1423163953Srrs if (init_cp == NULL) { 1424163953Srrs /* could not pull a INIT chunk in cookie */ 1425163953Srrs return (NULL); 1426163953Srrs } 1427163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1428163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1429163953Srrs return (NULL); 1430163953Srrs } 1431163953Srrs /* 1432163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1433163953Srrs * INIT-ACK follows the INIT chunk 1434163953Srrs */ 1435163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1436163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1437163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1438163953Srrs (uint8_t *) & initack_buf); 1439163953Srrs if (initack_cp == NULL) { 1440163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1441163953Srrs return (NULL); 1442163953Srrs } 1443163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1444163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1445163953Srrs return (NULL); 1446163953Srrs } 1447163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1448163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1449163953Srrs /* 1450163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1451163953Srrs * to get into the OPEN state 1452163953Srrs */ 1453166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1454172091Srrs /*- 1455172091Srrs * Opps, this means that we somehow generated two vtag's 1456172091Srrs * the same. I.e. we did: 1457172091Srrs * Us Peer 1458172091Srrs * <---INIT(tag=a)------ 1459172091Srrs * ----INIT-ACK(tag=t)--> 1460172091Srrs * ----INIT(tag=t)------> *1 1461172091Srrs * <---INIT-ACK(tag=a)--- 1462172091Srrs * <----CE(tag=t)------------- *2 1463172091Srrs * 1464172091Srrs * At point *1 we should be generating a different 1465172091Srrs * tag t'. Which means we would throw away the CE and send 1466172091Srrs * ours instead. Basically this is case C (throw away side). 1467172091Srrs */ 1468172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1469172091Srrs asoc->cookie_how[how_indx] = 17; 1470172091Srrs return (NULL); 1471172091Srrs 1472166023Srrs } 1473163953Srrs switch SCTP_GET_STATE 1474163953Srrs (asoc) { 1475163953Srrs case SCTP_STATE_COOKIE_WAIT: 1476166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1477163953Srrs /* 1478169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1479166023Srrs * correct tags... just accept it...but we must 1480166023Srrs * process the init so that we can make sure we have 1481166023Srrs * the right seq no's. 1482163953Srrs */ 1483163953Srrs /* First we must process the INIT !! */ 1484163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1485163953Srrs if (retval < 0) { 1486166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1487166023Srrs asoc->cookie_how[how_indx] = 3; 1488163953Srrs return (NULL); 1489163953Srrs } 1490163953Srrs /* we have already processed the INIT so no problem */ 1491163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1492171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1493171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1494163953Srrs /* update current state */ 1495166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1496166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1497166675Srrs else 1498166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1499171943Srrs 1500171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1501163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1502163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1503163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1504163953Srrs } 1505166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1506165220Srrs sctp_stop_all_cookie_timers(stcb); 1507163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1508163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1509163953Srrs (inp->sctp_socket->so_qlimit == 0) 1510163953Srrs ) { 1511172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1512172090Srrs struct socket *so; 1513172090Srrs 1514172090Srrs#endif 1515163953Srrs /* 1516163953Srrs * Here is where collision would go if we 1517163953Srrs * did a connect() and instead got a 1518163953Srrs * init/init-ack/cookie done before the 1519163953Srrs * init-ack came back.. 1520163953Srrs */ 1521163953Srrs stcb->sctp_ep->sctp_flags |= 1522163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1523172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1524172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1525172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1526172090Srrs SCTP_TCB_UNLOCK(stcb); 1527172090Srrs SCTP_SOCKET_LOCK(so, 1); 1528172090Srrs SCTP_TCB_LOCK(stcb); 1529172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1530172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1531172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1532172090Srrs return (NULL); 1533172090Srrs } 1534172090Srrs#endif 1535172090Srrs soisconnected(stcb->sctp_socket); 1536172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1537172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1538172090Srrs#endif 1539163953Srrs } 1540163953Srrs /* notify upper layer */ 1541163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1542163953Srrs /* 1543163953Srrs * since we did not send a HB make sure we don't 1544163953Srrs * double things 1545163953Srrs */ 1546163953Srrs net->hb_responded = 1; 1547170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1548218186Srrs &cookie->time_entered, 1549218186Srrs sctp_align_unsafe_makecopy, 1550218186Srrs SCTP_DETERMINE_LL_NOTOK); 1551163953Srrs 1552163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1553163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1554163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1555163953Srrs inp, stcb, NULL); 1556163953Srrs } 1557163953Srrs break; 1558163953Srrs default: 1559163953Srrs /* 1560163953Srrs * we're in the OPEN state (or beyond), so peer must 1561163953Srrs * have simply lost the COOKIE-ACK 1562163953Srrs */ 1563163953Srrs break; 1564163953Srrs } /* end switch */ 1565165220Srrs sctp_stop_all_cookie_timers(stcb); 1566163953Srrs /* 1567163953Srrs * We ignore the return code here.. not sure if we should 1568163953Srrs * somehow abort.. but we do have an existing asoc. This 1569163953Srrs * really should not fail. 1570163953Srrs */ 1571163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1572163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1573163953Srrs initack_offset, sh, init_src)) { 1574166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1575166023Srrs asoc->cookie_how[how_indx] = 4; 1576163953Srrs return (NULL); 1577163953Srrs } 1578163953Srrs /* respond with a COOKIE-ACK */ 1579165220Srrs sctp_toss_old_cookies(stcb, asoc); 1580163953Srrs sctp_send_cookie_ack(stcb); 1581166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1582166023Srrs asoc->cookie_how[how_indx] = 5; 1583163953Srrs return (stcb); 1584169352Srrs } 1585163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1586163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1587163953Srrs cookie->tie_tag_my_vtag == 0 && 1588163953Srrs cookie->tie_tag_peer_vtag == 0) { 1589163953Srrs /* 1590163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1591163953Srrs */ 1592166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1593166023Srrs asoc->cookie_how[how_indx] = 6; 1594163953Srrs return (NULL); 1595163953Srrs } 1596185694Srrs /* 1597185694Srrs * If nat support, and the below and stcb is established, send back 1598185694Srrs * a ABORT(colliding state) if we are established. 1599185694Srrs */ 1600185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1601185694Srrs (asoc->peer_supports_nat) && 1602185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1603185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1604185694Srrs (asoc->peer_vtag == 0)))) { 1605163953Srrs /* 1606185694Srrs * Special case - Peer's support nat. We may have two init's 1607185694Srrs * that we gave out the same tag on since one was not 1608185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1609185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1610185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1611185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1612185694Srrs * Now we have colliding state. We must send an abort here 1613185694Srrs * with colliding state indication. 1614185694Srrs */ 1615185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1616185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1617185694Srrs if (op_err == NULL) { 1618185694Srrs /* FOOBAR */ 1619185694Srrs return (NULL); 1620185694Srrs } 1621185694Srrs /* pre-reserve some space */ 1622185694Srrs#ifdef INET6 1623185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1624185694Srrs#else 1625185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1626185694Srrs#endif 1627185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1628185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1629185694Srrs /* Set the len */ 1630185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1631185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1632185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1633185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1634185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1635185694Srrs return (NULL); 1636185694Srrs } 1637185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1638185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1639185694Srrs (asoc->peer_vtag == 0))) { 1640185694Srrs /* 1641163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1642163953Srrs * should be ok, re-accept peer info 1643163953Srrs */ 1644166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1645166023Srrs /* 1646166023Srrs * Extension of case C. If we hit this, then the 1647166023Srrs * random number generator returned the same vtag 1648166023Srrs * when we first sent our INIT-ACK and when we later 1649166023Srrs * sent our INIT. The side with the seq numbers that 1650166023Srrs * are different will be the one that normnally 1651166023Srrs * would have hit case C. This in effect "extends" 1652166023Srrs * our vtags in this collision case to be 64 bits. 1653166023Srrs * The same collision could occur aka you get both 1654166023Srrs * vtag and seq number the same twice in a row.. but 1655166023Srrs * is much less likely. If it did happen then we 1656166023Srrs * would proceed through and bring up the assoc.. we 1657166023Srrs * may end up with the wrong stream setup however.. 1658166023Srrs * which would be bad.. but there is no way to 1659166023Srrs * tell.. until we send on a stream that does not 1660166023Srrs * exist :-) 1661166023Srrs */ 1662166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1663166023Srrs asoc->cookie_how[how_indx] = 7; 1664166023Srrs 1665166023Srrs return (NULL); 1666166023Srrs } 1667166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1668166023Srrs asoc->cookie_how[how_indx] = 8; 1669171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1670163953Srrs sctp_stop_all_cookie_timers(stcb); 1671163953Srrs /* 1672163953Srrs * since we did not send a HB make sure we don't double 1673163953Srrs * things 1674163953Srrs */ 1675163953Srrs net->hb_responded = 1; 1676163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1677163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1678163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1679163953Srrs NULL); 1680163953Srrs } 1681163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1682164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1683164205Srrs 1684164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1685164205Srrs /* 1686164205Srrs * Ok the peer probably discarded our data (if we 1687164205Srrs * echoed a cookie+data). So anything on the 1688164205Srrs * sent_queue should be marked for retransmit, we 1689164205Srrs * may not get something to kick us so it COULD 1690164205Srrs * still take a timeout to move these.. but it can't 1691164205Srrs * hurt to mark them. 1692164205Srrs */ 1693164205Srrs struct sctp_tmit_chunk *chk; 1694163953Srrs 1695164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1696164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1697164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1698171440Srrs sctp_flight_size_decrease(chk); 1699171440Srrs sctp_total_flight_decrease(stcb, chk); 1700168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1701164205Srrs spec_flag++; 1702164205Srrs } 1703164205Srrs } 1704164205Srrs 1705164205Srrs } 1706163953Srrs /* process the INIT info (peer's info) */ 1707163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1708163953Srrs if (retval < 0) { 1709166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1710166023Srrs asoc->cookie_how[how_indx] = 9; 1711163953Srrs return (NULL); 1712163953Srrs } 1713163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1714163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1715163953Srrs initack_offset, sh, init_src)) { 1716166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1717166023Srrs asoc->cookie_how[how_indx] = 10; 1718163953Srrs return (NULL); 1719163953Srrs } 1720163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1721163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1722163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1723163953Srrs 1724163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1725163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1726163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1727172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1728172090Srrs struct socket *so; 1729172090Srrs 1730172090Srrs#endif 1731163953Srrs stcb->sctp_ep->sctp_flags |= 1732163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1733172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1734172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1735172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1736172090Srrs SCTP_TCB_UNLOCK(stcb); 1737172090Srrs SCTP_SOCKET_LOCK(so, 1); 1738172090Srrs SCTP_TCB_LOCK(stcb); 1739172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1740172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1741172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1742172090Srrs return (NULL); 1743172090Srrs } 1744172090Srrs#endif 1745172090Srrs soisconnected(stcb->sctp_socket); 1746172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1747172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1748172090Srrs#endif 1749163953Srrs } 1750166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1751166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1752166675Srrs else 1753166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1754166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1755166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1756166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1757166675Srrs } else { 1758166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1759163953Srrs } 1760171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1761163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1762163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1763163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1764163953Srrs } 1765163953Srrs sctp_stop_all_cookie_timers(stcb); 1766165220Srrs sctp_toss_old_cookies(stcb, asoc); 1767163953Srrs sctp_send_cookie_ack(stcb); 1768164205Srrs if (spec_flag) { 1769164205Srrs /* 1770164205Srrs * only if we have retrans set do we do this. What 1771164205Srrs * this call does is get only the COOKIE-ACK out and 1772164205Srrs * then when we return the normal call to 1773164205Srrs * sctp_chunk_output will get the retrans out behind 1774164205Srrs * this. 1775164205Srrs */ 1776172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1777164205Srrs } 1778166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1779166023Srrs asoc->cookie_how[how_indx] = 11; 1780166023Srrs 1781163953Srrs return (stcb); 1782163953Srrs } 1783163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1784163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1785163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1786163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1787163953Srrs cookie->tie_tag_peer_vtag != 0) { 1788163953Srrs struct sctpasochead *head; 1789163953Srrs 1790185694Srrs if (asoc->peer_supports_nat) { 1791185694Srrs /* 1792185694Srrs * This is a gross gross hack. just call the 1793185694Srrs * cookie_new code since we are allowing a duplicate 1794185694Srrs * association. I hope this works... 1795185694Srrs */ 1796185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1797185694Srrs inp, netp, init_src, notification, 1798185694Srrs auth_skipped, auth_offset, auth_len, 1799185694Srrs vrf_id, port)); 1800185694Srrs } 1801163953Srrs /* 1802163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1803163953Srrs */ 1804165220Srrs /* temp code */ 1805166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1806166023Srrs asoc->cookie_how[how_indx] = 12; 1807171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1808171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1809165647Srrs 1810163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1811163953Srrs /* notify upper layer */ 1812163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1813165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1814166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1815166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1816166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1817166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1818166675Srrs } 1819166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1820166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1821166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1822166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1823166675Srrs } 1824165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1825171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1826165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1827165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1828165647Srrs 1829165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1830165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1831171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1832165647Srrs } 1833165647Srrs asoc->pre_open_streams = 1834165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1835165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1836165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1837179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1838165647Srrs 1839165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1840165647Srrs 1841165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1842165647Srrs 1843165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1844170138Srrs if (asoc->mapping_array) { 1845165647Srrs memset(asoc->mapping_array, 0, 1846165647Srrs asoc->mapping_array_size); 1847170138Srrs } 1848205627Srrs if (asoc->nr_mapping_array) { 1849185694Srrs memset(asoc->nr_mapping_array, 0, 1850206137Stuexen asoc->mapping_array_size); 1851185694Srrs } 1852165220Srrs SCTP_TCB_UNLOCK(stcb); 1853165220Srrs SCTP_INP_INFO_WLOCK(); 1854165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1855165220Srrs SCTP_TCB_LOCK(stcb); 1856165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1857163953Srrs /* send up all the data */ 1858164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1859165220Srrs 1860172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1861165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1862165220Srrs stcb->asoc.strmout[i].stream_no = i; 1863165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1864165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1865165220Srrs } 1866163953Srrs /* process the INIT-ACK info (my info) */ 1867163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1868163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1869163953Srrs 1870163953Srrs /* pull from vtag hash */ 1871163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1872163953Srrs /* re-insert to new vtag position */ 1873179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1874179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1875163953Srrs /* 1876163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1877163953Srrs * system 1878163953Srrs */ 1879163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1880163953Srrs 1881163953Srrs /* process the INIT info (peer's info) */ 1882164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1883165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1884165220Srrs SCTP_INP_INFO_WUNLOCK(); 1885165220Srrs 1886163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1887163953Srrs if (retval < 0) { 1888166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1889166023Srrs asoc->cookie_how[how_indx] = 13; 1890166023Srrs 1891163953Srrs return (NULL); 1892163953Srrs } 1893163953Srrs /* 1894163953Srrs * since we did not send a HB make sure we don't double 1895163953Srrs * things 1896163953Srrs */ 1897163953Srrs net->hb_responded = 1; 1898163953Srrs 1899163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1900163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1901163953Srrs initack_offset, sh, init_src)) { 1902166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1903166023Srrs asoc->cookie_how[how_indx] = 14; 1904166023Srrs 1905163953Srrs return (NULL); 1906163953Srrs } 1907163953Srrs /* respond with a COOKIE-ACK */ 1908163953Srrs sctp_stop_all_cookie_timers(stcb); 1909165220Srrs sctp_toss_old_cookies(stcb, asoc); 1910163953Srrs sctp_send_cookie_ack(stcb); 1911166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1912166023Srrs asoc->cookie_how[how_indx] = 15; 1913163953Srrs 1914163953Srrs return (stcb); 1915163953Srrs } 1916166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1917166023Srrs asoc->cookie_how[how_indx] = 16; 1918163953Srrs /* all other cases... */ 1919163953Srrs return (NULL); 1920163953Srrs} 1921163953Srrs 1922166086Srrs 1923163953Srrs/* 1924163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1925163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1926163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1927163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1928163953Srrs * was from returns a new TCB 1929163953Srrs */ 1930185694Srrsstruct sctp_tcb * 1931163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1932163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1933163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1934163953Srrs struct sockaddr *init_src, int *notification, 1935169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1936179157Srrs uint32_t vrf_id, uint16_t port) 1937163953Srrs{ 1938163953Srrs struct sctp_tcb *stcb; 1939163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1940163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1941163953Srrs struct sockaddr_storage sa_store; 1942163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1943163953Srrs struct sockaddr_in *sin; 1944163953Srrs struct sockaddr_in6 *sin6; 1945163953Srrs struct sctp_association *asoc; 1946163953Srrs int chk_length; 1947163953Srrs int init_offset, initack_offset, initack_limit; 1948163953Srrs int retval; 1949163953Srrs int error = 0; 1950163953Srrs uint32_t old_tag; 1951166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1952163953Srrs 1953172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1954172090Srrs struct socket *so; 1955172090Srrs 1956172090Srrs so = SCTP_INP_SO(inp); 1957172090Srrs#endif 1958172090Srrs 1959163953Srrs /* 1960163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1961163953Srrs * INIT should start after the cookie-echo header struct (chunk 1962163953Srrs * header, state cookie header struct) 1963163953Srrs */ 1964163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1965163953Srrs init_cp = (struct sctp_init_chunk *) 1966163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1967163953Srrs (uint8_t *) & init_buf); 1968163953Srrs if (init_cp == NULL) { 1969163953Srrs /* could not pull a INIT chunk in cookie */ 1970169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1971169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1972163953Srrs return (NULL); 1973163953Srrs } 1974163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1975163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1976169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1977163953Srrs return (NULL); 1978163953Srrs } 1979163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1980163953Srrs /* 1981163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1982163953Srrs * INIT-ACK follows the INIT chunk 1983163953Srrs */ 1984163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1985163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1986163953Srrs (uint8_t *) & initack_buf); 1987163953Srrs if (initack_cp == NULL) { 1988163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1989169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1990163953Srrs return (NULL); 1991163953Srrs } 1992163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1993163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1994163953Srrs return (NULL); 1995163953Srrs } 1996163953Srrs /* 1997163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1998163953Srrs * "initack_limit" value. This is because the chk_length field 1999163953Srrs * includes the length of the cookie, but the cookie is omitted when 2000163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2001163953Srrs */ 2002163953Srrs initack_limit = offset + cookie_len; 2003163953Srrs 2004163953Srrs /* 2005163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2006163953Srrs * and popluate 2007163953Srrs */ 2008171531Srrs 2009171531Srrs /* 2010171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2011171531Srrs * We do this since in effect we only use the p argument when the 2012171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2013171531Srrs * getting a cookie, we cannot be unbound. 2014171531Srrs */ 2015206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2016171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2017171531Srrs (struct thread *)NULL 2018171531Srrs ); 2019163953Srrs if (stcb == NULL) { 2020163953Srrs struct mbuf *op_err; 2021163953Srrs 2022163953Srrs /* memory problem? */ 2023169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2024169420Srrs "process_cookie_new: no room for another TCB!\n"); 2025163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2026170140Srrs 2027163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2028179157Srrs sh, op_err, vrf_id, port); 2029163953Srrs return (NULL); 2030163953Srrs } 2031163953Srrs /* get the correct sctp_nets */ 2032169420Srrs if (netp) 2033169420Srrs *netp = sctp_findnet(stcb, init_src); 2034169420Srrs 2035163953Srrs asoc = &stcb->asoc; 2036163953Srrs /* get scope variables out of cookie */ 2037163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2038163953Srrs asoc->site_scope = cookie->site_scope; 2039163953Srrs asoc->local_scope = cookie->local_scope; 2040163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2041163953Srrs 2042163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2043163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2044163953Srrs struct mbuf *op_err; 2045163953Srrs 2046163953Srrs /* 2047163953Srrs * Houston we have a problem. The EP changed while the 2048163953Srrs * cookie was in flight. Only recourse is to abort the 2049163953Srrs * association. 2050163953Srrs */ 2051170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2052163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2053163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2054179157Srrs sh, op_err, vrf_id, port); 2055172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2056172090Srrs SCTP_TCB_UNLOCK(stcb); 2057172090Srrs SCTP_SOCKET_LOCK(so, 1); 2058172090Srrs SCTP_TCB_LOCK(stcb); 2059172090Srrs#endif 2060171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2061171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2062172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2063172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2064172090Srrs#endif 2065172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2066163953Srrs return (NULL); 2067163953Srrs } 2068163953Srrs /* process the INIT-ACK info (my info) */ 2069163953Srrs old_tag = asoc->my_vtag; 2070185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2071163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2072163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2073163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2074163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2075179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2076163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2077163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2078163953Srrs 2079163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2080163953Srrs 2081163953Srrs /* process the INIT info (peer's info) */ 2082169420Srrs if (netp) 2083169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 2084169420Srrs else 2085169420Srrs retval = 0; 2086163953Srrs if (retval < 0) { 2087170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2088172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2089172090Srrs SCTP_TCB_UNLOCK(stcb); 2090172090Srrs SCTP_SOCKET_LOCK(so, 1); 2091172090Srrs SCTP_TCB_LOCK(stcb); 2092172090Srrs#endif 2093171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2094172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2095172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2096172090Srrs#endif 2097172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2098163953Srrs return (NULL); 2099163953Srrs } 2100163953Srrs /* load all addresses */ 2101163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 2102163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2103163953Srrs init_src)) { 2104170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2105172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2106172090Srrs SCTP_TCB_UNLOCK(stcb); 2107172090Srrs SCTP_SOCKET_LOCK(so, 1); 2108172090Srrs SCTP_TCB_LOCK(stcb); 2109172090Srrs#endif 2110171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2111172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2112172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2113172090Srrs#endif 2114172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2115163953Srrs return (NULL); 2116163953Srrs } 2117163953Srrs /* 2118163953Srrs * verify any preceding AUTH chunk that was skipped 2119163953Srrs */ 2120163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2121163953Srrs sctp_auth_get_cookie_params(stcb, m, 2122163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2123163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2124163953Srrs if (auth_skipped) { 2125163953Srrs struct sctp_auth_chunk *auth; 2126163953Srrs 2127163953Srrs auth = (struct sctp_auth_chunk *) 2128166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2129169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2130163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2131169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2132169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2133171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2134172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2135172090Srrs SCTP_TCB_UNLOCK(stcb); 2136172090Srrs SCTP_SOCKET_LOCK(so, 1); 2137172090Srrs SCTP_TCB_LOCK(stcb); 2138172090Srrs#endif 2139171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2140172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2141172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2142172090Srrs#endif 2143172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2144163953Srrs return (NULL); 2145163953Srrs } else { 2146163953Srrs /* remaining chunks checked... good to go */ 2147163953Srrs stcb->asoc.authenticated = 1; 2148163953Srrs } 2149163953Srrs } 2150163953Srrs /* update current state */ 2151169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2152171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2153163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2154163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2155163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2156163953Srrs } 2157165220Srrs sctp_stop_all_cookie_timers(stcb); 2158163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2159163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2160163953Srrs 2161163953Srrs /* 2162163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2163163953Srrs * addresses that need to get added to the peer (eg. addresses 2164163953Srrs * changed while cookie echo in flight). This needs to be done 2165163953Srrs * after we go to the OPEN state to do the correct asconf 2166163953Srrs * processing. else, make sure we have the correct addresses in our 2167163953Srrs * lists 2168163953Srrs */ 2169163953Srrs 2170163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2171163953Srrs /* pull in local_address (our "from" address) */ 2172163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 2173163953Srrs /* source addr is IPv4 */ 2174163953Srrs sin = (struct sockaddr_in *)initack_src; 2175163953Srrs memset(sin, 0, sizeof(*sin)); 2176163953Srrs sin->sin_family = AF_INET; 2177163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2178163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2179163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 2180163953Srrs /* source addr is IPv6 */ 2181163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2182163953Srrs memset(sin6, 0, sizeof(*sin6)); 2183163953Srrs sin6->sin6_family = AF_INET6; 2184163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2185163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2186163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2187163953Srrs sizeof(sin6->sin6_addr)); 2188163953Srrs } else { 2189170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2190172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2191172090Srrs SCTP_TCB_UNLOCK(stcb); 2192172090Srrs SCTP_SOCKET_LOCK(so, 1); 2193172090Srrs SCTP_TCB_LOCK(stcb); 2194172090Srrs#endif 2195171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2196172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2197172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2198172090Srrs#endif 2199172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2200163953Srrs return (NULL); 2201163953Srrs } 2202163953Srrs 2203163953Srrs /* set up to notify upper layer */ 2204163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2205163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2206163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2207163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2208163953Srrs /* 2209163953Srrs * This is an endpoint that called connect() how it got a 2210163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2211163953Srrs * the INIT was sent, but before it got there.. a complete 2212163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2213163953Srrs * should have went to the other code.. not here.. oh well.. 2214163953Srrs * a bit of protection is worth having.. 2215163953Srrs */ 2216163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2217172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2218172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2219172090Srrs SCTP_TCB_UNLOCK(stcb); 2220172090Srrs SCTP_SOCKET_LOCK(so, 1); 2221172090Srrs SCTP_TCB_LOCK(stcb); 2222172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2223172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2224172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2225172090Srrs return (NULL); 2226172090Srrs } 2227172090Srrs#endif 2228172090Srrs soisconnected(stcb->sctp_socket); 2229172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2230172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2231172090Srrs#endif 2232163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2233163953Srrs (inp->sctp_socket->so_qlimit)) { 2234163953Srrs /* 2235163953Srrs * We don't want to do anything with this one. Since it is 2236163953Srrs * the listening guy. The timer will get started for 2237163953Srrs * accepted connections in the caller. 2238163953Srrs */ 2239163953Srrs ; 2240163953Srrs } 2241163953Srrs /* since we did not send a HB make sure we don't double things */ 2242169420Srrs if ((netp) && (*netp)) 2243169420Srrs (*netp)->hb_responded = 1; 2244163953Srrs 2245163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2246163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2247163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2248163953Srrs } 2249165220Srrs /* calculate the RTT */ 2250171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2251170642Srrs if ((netp) && (*netp)) { 2252169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2253218186Srrs &cookie->time_entered, sctp_align_unsafe_makecopy, 2254218186Srrs SCTP_DETERMINE_LL_NOTOK); 2255170642Srrs } 2256172190Srrs /* respond with a COOKIE-ACK */ 2257163953Srrs sctp_send_cookie_ack(stcb); 2258172190Srrs 2259172190Srrs /* 2260172190Srrs * check the address lists for any ASCONFs that need to be sent 2261172190Srrs * AFTER the cookie-ack is sent 2262172190Srrs */ 2263172190Srrs sctp_check_address_list(stcb, m, 2264172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2265172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2266172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2267172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2268172190Srrs 2269172190Srrs 2270163953Srrs return (stcb); 2271163953Srrs} 2272163953Srrs 2273185694Srrs/* 2274185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2275185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2276185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2277185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2278185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2279185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2280185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2281185694Srrs -- SEND ABORT - TRY AGAIN -- 2282185694Srrs } 2283185694Srrs } 2284185694Srrs*/ 2285163953Srrs 2286163953Srrs/* 2287163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2288163953Srrs * existing (non-NULL) TCB 2289163953Srrs */ 2290163953Srrsstatic struct mbuf * 2291163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2292163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2293163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2294169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2295179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2296163953Srrs{ 2297163953Srrs struct sctp_state_cookie *cookie; 2298163953Srrs struct sockaddr_in6 sin6; 2299163953Srrs struct sockaddr_in sin; 2300163953Srrs struct sctp_tcb *l_stcb = *stcb; 2301163953Srrs struct sctp_inpcb *l_inp; 2302163953Srrs struct sockaddr *to; 2303163953Srrs sctp_assoc_t sac_restart_id; 2304163953Srrs struct sctp_pcb *ep; 2305163953Srrs struct mbuf *m_sig; 2306163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2307163953Srrs uint8_t *sig; 2308163953Srrs uint8_t cookie_ok = 0; 2309163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2310163953Srrs unsigned int cookie_len; 2311163953Srrs struct timeval now; 2312163953Srrs struct timeval time_expires; 2313163953Srrs struct sockaddr_storage dest_store; 2314163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2315163953Srrs struct ip *iph; 2316163953Srrs int notification = 0; 2317163953Srrs struct sctp_nets *netl; 2318163953Srrs int had_a_existing_tcb = 0; 2319216887Stuexen int send_int_conf = 0; 2320163953Srrs 2321169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2322169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2323163953Srrs 2324163953Srrs if (inp_p == NULL) { 2325163953Srrs return (NULL); 2326163953Srrs } 2327163953Srrs /* First get the destination address setup too. */ 2328163953Srrs iph = mtod(m, struct ip *); 2329178251Srrs switch (iph->ip_v) { 2330178251Srrs case IPVERSION: 2331178251Srrs { 2332178251Srrs /* its IPv4 */ 2333178251Srrs struct sockaddr_in *lsin; 2334163953Srrs 2335178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2336178251Srrs memset(lsin, 0, sizeof(*lsin)); 2337178251Srrs lsin->sin_family = AF_INET; 2338178251Srrs lsin->sin_len = sizeof(*lsin); 2339178251Srrs lsin->sin_port = sh->dest_port; 2340178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2341178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2342178251Srrs break; 2343178251Srrs } 2344178251Srrs#ifdef INET6 2345178251Srrs case IPV6_VERSION >> 4: 2346178251Srrs { 2347178251Srrs /* its IPv6 */ 2348178251Srrs struct ip6_hdr *ip6; 2349178251Srrs struct sockaddr_in6 *lsin6; 2350163953Srrs 2351178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2352178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2353178251Srrs lsin6->sin6_family = AF_INET6; 2354178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2355178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2356178251Srrs lsin6->sin6_port = sh->dest_port; 2357178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2358178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2359178251Srrs break; 2360178251Srrs } 2361178251Srrs#endif 2362178251Srrs default: 2363163953Srrs return (NULL); 2364163953Srrs } 2365163953Srrs 2366163953Srrs cookie = &cp->cookie; 2367163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2368163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2369163953Srrs 2370163953Srrs if ((cookie->peerport != sh->src_port) && 2371163953Srrs (cookie->myport != sh->dest_port) && 2372163953Srrs (cookie->my_vtag != sh->v_tag)) { 2373163953Srrs /* 2374163953Srrs * invalid ports or bad tag. Note that we always leave the 2375163953Srrs * v_tag in the header in network order and when we stored 2376163953Srrs * it in the my_vtag slot we also left it in network order. 2377169352Srrs * This maintains the match even though it may be in the 2378163953Srrs * opposite byte order of the machine :-> 2379163953Srrs */ 2380163953Srrs return (NULL); 2381163953Srrs } 2382163953Srrs if (cookie_len > size_of_pkt || 2383163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2384163953Srrs sizeof(struct sctp_init_chunk) + 2385163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2386163953Srrs /* cookie too long! or too small */ 2387163953Srrs return (NULL); 2388163953Srrs } 2389163953Srrs /* 2390163953Srrs * split off the signature into its own mbuf (since it should not be 2391163953Srrs * calculated in the sctp_hmac_m() call). 2392163953Srrs */ 2393163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2394163953Srrs if (sig_offset > size_of_pkt) { 2395163953Srrs /* packet not correct size! */ 2396163953Srrs /* XXX this may already be accounted for earlier... */ 2397163953Srrs return (NULL); 2398163953Srrs } 2399163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2400163953Srrs if (m_sig == NULL) { 2401163953Srrs /* out of memory or ?? */ 2402163953Srrs return (NULL); 2403163953Srrs } 2404178202Srrs#ifdef SCTP_MBUF_LOGGING 2405179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2406178202Srrs struct mbuf *mat; 2407178202Srrs 2408178202Srrs mat = m_sig; 2409178202Srrs while (mat) { 2410178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2411178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2412178202Srrs } 2413178202Srrs mat = SCTP_BUF_NEXT(mat); 2414178202Srrs } 2415178202Srrs } 2416178202Srrs#endif 2417178202Srrs 2418163953Srrs /* 2419163953Srrs * compute the signature/digest for the cookie 2420163953Srrs */ 2421163953Srrs ep = &(*inp_p)->sctp_ep; 2422163953Srrs l_inp = *inp_p; 2423163953Srrs if (l_stcb) { 2424163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2425163953Srrs } 2426163953Srrs SCTP_INP_RLOCK(l_inp); 2427163953Srrs if (l_stcb) { 2428163953Srrs SCTP_TCB_LOCK(l_stcb); 2429163953Srrs } 2430163953Srrs /* which cookie is it? */ 2431163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2432163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2433163953Srrs /* it's the old cookie */ 2434169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2435163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2436170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2437163953Srrs } else { 2438163953Srrs /* it's the current cookie */ 2439169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2440163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2441170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2442163953Srrs } 2443163953Srrs /* get the signature */ 2444163953Srrs SCTP_INP_RUNLOCK(l_inp); 2445163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2446163953Srrs if (sig == NULL) { 2447163953Srrs /* couldn't find signature */ 2448164085Srrs sctp_m_freem(m_sig); 2449163953Srrs return (NULL); 2450163953Srrs } 2451163953Srrs /* compare the received digest with the computed digest */ 2452163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2453163953Srrs /* try the old cookie? */ 2454163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2455163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2456163953Srrs /* compute digest with old */ 2457169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2458163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2459170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2460163953Srrs /* compare */ 2461163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2462163953Srrs cookie_ok = 1; 2463163953Srrs } 2464163953Srrs } else { 2465163953Srrs cookie_ok = 1; 2466163953Srrs } 2467163953Srrs 2468163953Srrs /* 2469163953Srrs * Now before we continue we must reconstruct our mbuf so that 2470163953Srrs * normal processing of any other chunks will work. 2471163953Srrs */ 2472163953Srrs { 2473163953Srrs struct mbuf *m_at; 2474163953Srrs 2475163953Srrs m_at = m; 2476165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2477165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2478163953Srrs } 2479165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2480163953Srrs } 2481163953Srrs 2482163953Srrs if (cookie_ok == 0) { 2483169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2484169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2485169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2486169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2487163953Srrs return (NULL); 2488163953Srrs } 2489163953Srrs /* 2490163953Srrs * check the cookie timestamps to be sure it's not stale 2491163953Srrs */ 2492169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2493163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2494169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2495163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2496180387Srrs /* 2497180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2498180387Srrs * is undefined. 2499180387Srrs */ 2500163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2501163953Srrs /* cookie is stale! */ 2502163953Srrs struct mbuf *op_err; 2503163953Srrs struct sctp_stale_cookie_msg *scm; 2504163953Srrs uint32_t tim; 2505163953Srrs 2506163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2507165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2508163953Srrs if (op_err == NULL) { 2509163953Srrs /* FOOBAR */ 2510163953Srrs return (NULL); 2511163953Srrs } 2512163953Srrs /* Set the len */ 2513165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2514163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2515163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2516163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2517163953Srrs (sizeof(uint32_t)))); 2518163953Srrs /* seconds to usec */ 2519163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2520163953Srrs /* add in usec */ 2521163953Srrs if (tim == 0) 2522163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2523163953Srrs scm->time_usec = htonl(tim); 2524169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2525179157Srrs vrf_id, port); 2526163953Srrs return (NULL); 2527163953Srrs } 2528163953Srrs /* 2529163953Srrs * Now we must see with the lookup address if we have an existing 2530163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2531163953Srrs * and a INIT collided with us and somewhere the peer sent the 2532163953Srrs * cookie on another address besides the single address our assoc 2533163953Srrs * had for him. In this case we will have one of the tie-tags set at 2534163953Srrs * least AND the address field in the cookie can be used to look it 2535163953Srrs * up. 2536163953Srrs */ 2537163953Srrs to = NULL; 2538163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2539163953Srrs memset(&sin6, 0, sizeof(sin6)); 2540163953Srrs sin6.sin6_family = AF_INET6; 2541163953Srrs sin6.sin6_len = sizeof(sin6); 2542163953Srrs sin6.sin6_port = sh->src_port; 2543163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2544163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2545163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2546163953Srrs to = (struct sockaddr *)&sin6; 2547163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2548163953Srrs memset(&sin, 0, sizeof(sin)); 2549163953Srrs sin.sin_family = AF_INET; 2550163953Srrs sin.sin_len = sizeof(sin); 2551163953Srrs sin.sin_port = sh->src_port; 2552163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2553163953Srrs to = (struct sockaddr *)&sin; 2554169420Srrs } else { 2555169420Srrs /* This should not happen */ 2556169420Srrs return (NULL); 2557163953Srrs } 2558163953Srrs if ((*stcb == NULL) && to) { 2559163953Srrs /* Yep, lets check */ 2560163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2561163953Srrs if (*stcb == NULL) { 2562163953Srrs /* 2563163953Srrs * We should have only got back the same inp. If we 2564163953Srrs * got back a different ep we have a problem. The 2565163953Srrs * original findep got back l_inp and now 2566163953Srrs */ 2567163953Srrs if (l_inp != *inp_p) { 2568169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2569163953Srrs } 2570165220Srrs } else { 2571165220Srrs if (*locked_tcb == NULL) { 2572165220Srrs /* 2573165220Srrs * In this case we found the assoc only 2574165220Srrs * after we locked the create lock. This 2575165220Srrs * means we are in a colliding case and we 2576165220Srrs * must make sure that we unlock the tcb if 2577165220Srrs * its one of the cases where we throw away 2578165220Srrs * the incoming packets. 2579165220Srrs */ 2580165220Srrs *locked_tcb = *stcb; 2581165220Srrs 2582165220Srrs /* 2583165220Srrs * We must also increment the inp ref count 2584165220Srrs * since the ref_count flags was set when we 2585165220Srrs * did not find the TCB, now we found it 2586165220Srrs * which reduces the refcount.. we must 2587165220Srrs * raise it back out to balance it all :-) 2588165220Srrs */ 2589165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2590165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2591169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2592165220Srrs (*stcb)->sctp_ep, l_inp); 2593165220Srrs } 2594165220Srrs } 2595163953Srrs } 2596163953Srrs } 2597188067Srrs if (to == NULL) { 2598169420Srrs return (NULL); 2599188067Srrs } 2600163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2601163953Srrs if (*stcb == NULL) { 2602163953Srrs /* this is the "normal" case... get a new TCB */ 2603163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2604163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2605179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2606163953Srrs } else { 2607163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2608163953Srrs had_a_existing_tcb = 1; 2609163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2610185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2611185694Srrs ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port); 2612163953Srrs } 2613163953Srrs 2614163953Srrs if (*stcb == NULL) { 2615163953Srrs /* still no TCB... must be bad cookie-echo */ 2616163953Srrs return (NULL); 2617163953Srrs } 2618218335Stuexen if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2619218335Stuexen (*netp)->flowid = m->m_pkthdr.flowid; 2620218392Srrs (*netp)->flowidset = 1; 2621218335Stuexen } 2622163953Srrs /* 2623163953Srrs * Ok, we built an association so confirm the address we sent the 2624163953Srrs * INIT-ACK to. 2625163953Srrs */ 2626163953Srrs netl = sctp_findnet(*stcb, to); 2627163953Srrs /* 2628163953Srrs * This code should in theory NOT run but 2629163953Srrs */ 2630163953Srrs if (netl == NULL) { 2631163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2632163953Srrs int ret; 2633163953Srrs 2634165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2635165220Srrs SCTP_IN_COOKIE_PROC); 2636163953Srrs netl = sctp_findnet(*stcb, to); 2637163953Srrs } 2638163953Srrs if (netl) { 2639163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2640163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2641169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2642163953Srrs netl); 2643216887Stuexen send_int_conf = 1; 2644163953Srrs } 2645163953Srrs } 2646163953Srrs if (*stcb) { 2647163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2648163953Srrs *stcb, NULL); 2649163953Srrs } 2650163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2651163953Srrs if (!had_a_existing_tcb || 2652163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2653163953Srrs /* 2654163953Srrs * If we have a NEW cookie or the connect never 2655163953Srrs * reached the connected state during collision we 2656163953Srrs * must do the TCP accept thing. 2657163953Srrs */ 2658163953Srrs struct socket *so, *oso; 2659163953Srrs struct sctp_inpcb *inp; 2660163953Srrs 2661163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2662163953Srrs /* 2663163953Srrs * For a restart we will keep the same 2664163953Srrs * socket, no need to do anything. I THINK!! 2665163953Srrs */ 2666172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2667216887Stuexen if (send_int_conf) { 2668216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2669216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2670216887Stuexen } 2671163953Srrs return (m); 2672163953Srrs } 2673163953Srrs oso = (*inp_p)->sctp_socket; 2674171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2675163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2676166086Srrs so = sonewconn(oso, 0 2677163953Srrs ); 2678163953Srrs SCTP_TCB_LOCK((*stcb)); 2679171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2680171858Srrs 2681163953Srrs if (so == NULL) { 2682163953Srrs struct mbuf *op_err; 2683163953Srrs 2684172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2685172090Srrs struct socket *pcb_so; 2686172090Srrs 2687172090Srrs#endif 2688163953Srrs /* Too many sockets */ 2689169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2690163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2691163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2692179157Srrs sh, op_err, vrf_id, port); 2693172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2694172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2695172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2696172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2697172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2698172090Srrs SCTP_TCB_LOCK((*stcb)); 2699172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2700172090Srrs#endif 2701171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2702172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2703172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2704172090Srrs#endif 2705163953Srrs return (NULL); 2706163953Srrs } 2707163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2708166086Srrs SCTP_INP_INCR_REF(inp); 2709166086Srrs /* 2710166086Srrs * We add the unbound flag here so that if we get an 2711166086Srrs * soabort() before we get the move_pcb done, we 2712166086Srrs * will properly cleanup. 2713166086Srrs */ 2714163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2715163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2716163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2717166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2718163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2719163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2720163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2721172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2722163953Srrs inp->sctp_socket = so; 2723163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2724211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2725218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2726163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2727163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2728163953Srrs inp->inp_starting_point_for_iterator = NULL; 2729163953Srrs /* 2730163953Srrs * copy in the authentication parameters from the 2731163953Srrs * original endpoint 2732163953Srrs */ 2733163953Srrs if (inp->sctp_ep.local_hmacs) 2734163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2735163953Srrs inp->sctp_ep.local_hmacs = 2736163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2737163953Srrs if (inp->sctp_ep.local_auth_chunks) 2738163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2739163953Srrs inp->sctp_ep.local_auth_chunks = 2740163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2741163953Srrs 2742163953Srrs /* 2743163953Srrs * Now we must move it from one hash table to 2744163953Srrs * another and get the tcb in the right place. 2745163953Srrs */ 2746207924Srrs 2747207924Srrs /* 2748207924Srrs * This is where the one-2-one socket is put into 2749207924Srrs * the accept state waiting for the accept! 2750207924Srrs */ 2751207924Srrs if (*stcb) { 2752207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2753207924Srrs } 2754163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2755170056Srrs 2756170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2757170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2758170056Srrs 2759175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2760175845Srwatson 0); 2761170056Srrs SCTP_TCB_LOCK((*stcb)); 2762170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2763163953Srrs 2764170056Srrs 2765166086Srrs /* 2766166086Srrs * now we must check to see if we were aborted while 2767166086Srrs * the move was going on and the lock/unlock 2768166086Srrs * happened. 2769166086Srrs */ 2770166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2771166086Srrs /* 2772166086Srrs * yep it was, we leave the assoc attached 2773166086Srrs * to the socket since the sctp_inpcb_free() 2774166086Srrs * call will send an abort for us. 2775166086Srrs */ 2776166086Srrs SCTP_INP_DECR_REF(inp); 2777166086Srrs return (NULL); 2778166086Srrs } 2779166086Srrs SCTP_INP_DECR_REF(inp); 2780163953Srrs /* Switch over to the new guy */ 2781163953Srrs *inp_p = inp; 2782172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2783216887Stuexen if (send_int_conf) { 2784216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2785216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2786216887Stuexen } 2787166086Srrs /* 2788166086Srrs * Pull it from the incomplete queue and wake the 2789166086Srrs * guy 2790166086Srrs */ 2791172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2792172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2793172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2794172090Srrs SCTP_SOCKET_LOCK(so, 1); 2795172090Srrs#endif 2796166086Srrs soisconnected(so); 2797172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2798172118Srrs SCTP_TCB_LOCK((*stcb)); 2799172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2800172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2801172090Srrs#endif 2802163953Srrs return (m); 2803163953Srrs } 2804163953Srrs } 2805216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2806216887Stuexen if (notification) { 2807216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2808216887Stuexen } 2809216887Stuexen if (send_int_conf) { 2810216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2811216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2812216887Stuexen } 2813163953Srrs } 2814163953Srrs return (m); 2815163953Srrs} 2816163953Srrs 2817163953Srrsstatic void 2818163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2819163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2820163953Srrs{ 2821163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2822163953Srrs struct sctp_association *asoc; 2823163953Srrs 2824169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2825169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2826163953Srrs if (stcb == NULL) 2827163953Srrs return; 2828163953Srrs 2829163953Srrs asoc = &stcb->asoc; 2830163953Srrs 2831163953Srrs sctp_stop_all_cookie_timers(stcb); 2832163953Srrs /* process according to association state */ 2833163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2834163953Srrs /* state change only needed when I am in right state */ 2835169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2836171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2837163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2838163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2839163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2840163953Srrs 2841163953Srrs } 2842163953Srrs /* update RTO */ 2843163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2844163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2845163953Srrs if (asoc->overall_error_count == 0) { 2846163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2847218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2848218186Srrs SCTP_DETERMINE_LL_NOTOK); 2849163953Srrs } 2850169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2851172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2852163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2853163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2854172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2855172090Srrs struct socket *so; 2856172090Srrs 2857172090Srrs#endif 2858163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2859172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2860172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2861172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2862172090Srrs SCTP_TCB_UNLOCK(stcb); 2863172090Srrs SCTP_SOCKET_LOCK(so, 1); 2864172090Srrs SCTP_TCB_LOCK(stcb); 2865172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2866172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2867172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2868172090Srrs return; 2869172090Srrs } 2870172090Srrs#endif 2871172090Srrs soisconnected(stcb->sctp_socket); 2872172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2873172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2874172090Srrs#endif 2875163953Srrs } 2876163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2877163953Srrs stcb, net); 2878163953Srrs /* 2879163953Srrs * since we did not send a HB make sure we don't double 2880163953Srrs * things 2881163953Srrs */ 2882163953Srrs net->hb_responded = 1; 2883163953Srrs 2884163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2885163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2886163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2887163953Srrs stcb->sctp_ep, stcb, NULL); 2888163953Srrs } 2889163953Srrs /* 2890171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2891171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2892171572Srrs * in flight) 2893163953Srrs */ 2894163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2895163953Srrs (stcb->asoc.peer_supports_asconf) && 2896163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2897171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2898163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2899163953Srrs stcb->sctp_ep, stcb, 2900163953Srrs stcb->asoc.primary_destination); 2901171572Srrs#else 2902172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2903172190Srrs SCTP_ADDR_NOT_LOCKED); 2904171572Srrs#endif 2905163953Srrs } 2906163953Srrs } 2907163953Srrs /* Toss the cookie if I can */ 2908163953Srrs sctp_toss_old_cookies(stcb, asoc); 2909163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2910163953Srrs /* Restart the timer if we have pending data */ 2911163953Srrs struct sctp_tmit_chunk *chk; 2912163953Srrs 2913163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2914216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2915163953Srrs } 2916163953Srrs} 2917163953Srrs 2918163953Srrsstatic void 2919163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2920163953Srrs struct sctp_tcb *stcb) 2921163953Srrs{ 2922163953Srrs struct sctp_nets *net; 2923163953Srrs struct sctp_tmit_chunk *lchk; 2924218072Srrs struct sctp_ecne_chunk bkup; 2925218072Srrs uint8_t override_bit = 0; 2926218072Srrs uint32_t tsn, window_data_tsn; 2927218072Srrs int len; 2928218232Srrs unsigned int pkt_cnt; 2929163953Srrs 2930218072Srrs len = ntohs(cp->ch.chunk_length); 2931218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 2932218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 2933163953Srrs return; 2934163953Srrs } 2935218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 2936218072Srrs /* Its the old format */ 2937218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 2938218072Srrs bkup.num_pkts_since_cwr = htonl(1); 2939218072Srrs cp = &bkup; 2940218072Srrs } 2941163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2942163953Srrs tsn = ntohl(cp->tsn); 2943218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 2944218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 2945163953Srrs if (lchk == NULL) { 2946218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 2947163953Srrs } else { 2948218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 2949163953Srrs } 2950163953Srrs 2951218072Srrs /* Find where it was sent to if possible. */ 2952163953Srrs net = NULL; 2953216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 2954163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2955163953Srrs net = lchk->whoTo; 2956218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 2957163953Srrs break; 2958163953Srrs } 2959216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 2960163953Srrs break; 2961216825Stuexen } 2962163953Srrs } 2963218072Srrs if (net == NULL) { 2964163953Srrs /* 2965218072Srrs * What to do. A previous send of a CWR was possibly lost. 2966218072Srrs * See how old it is, we may have it marked on the actual 2967218072Srrs * net. 2968218072Srrs */ 2969218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2970218072Srrs if (tsn == net->last_cwr_tsn) { 2971218072Srrs /* Found him, send it off */ 2972218072Srrs goto out; 2973218072Srrs } 2974218072Srrs } 2975218072Srrs /* 2976218072Srrs * If we reach here, we need to send a special CWR that says 2977218072Srrs * hey, we did this a long time ago and you lost the 2978218072Srrs * response. 2979218072Srrs */ 2980218072Srrs net = TAILQ_FIRST(&stcb->asoc.nets); 2981218072Srrs override_bit = SCTP_CWR_REDUCE_OVERRIDE; 2982218072Srrs } 2983218072Srrsout: 2984218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 2985218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 2986218072Srrs /* 2987171440Srrs * JRS - Use the congestion control given in the pluggable 2988171440Srrs * CC module 2989171440Srrs */ 2990218072Srrs int ocwnd; 2991218072Srrs 2992218072Srrs ocwnd = net->cwnd; 2993218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 2994171440Srrs /* 2995218072Srrs * We reduce once every RTT. So we will only lower cwnd at 2996218072Srrs * the next sending seq i.e. the window_data_tsn 2997163953Srrs */ 2998218072Srrs net->cwr_window_tsn = window_data_tsn; 2999218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3000218072Srrs net->lost_cnt = pkt_cnt; 3001218072Srrs net->last_cwr_tsn = tsn; 3002218072Srrs } else { 3003218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3004218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3005218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3006218072Srrs /* 3007218129Srrs * Another loss in the same window update how many 3008218129Srrs * marks/packets lost we have had. 3009218072Srrs */ 3010218129Srrs int cnt = 1; 3011218072Srrs 3012218072Srrs if (pkt_cnt > net->lost_cnt) { 3013218072Srrs /* Should be the case */ 3014218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3015218129Srrs net->ecn_ce_pkt_cnt += cnt; 3016218072Srrs } 3017218129Srrs net->lost_cnt = pkt_cnt; 3018218072Srrs net->last_cwr_tsn = tsn; 3019218129Srrs /* 3020218129Srrs * Most CC functions will ignore this call, since we 3021218129Srrs * are in-window yet of the initial CE the peer saw. 3022218129Srrs */ 3023218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3024218072Srrs } 3025163953Srrs } 3026163953Srrs /* 3027163953Srrs * We always send a CWR this way if our previous one was lost our 3028163953Srrs * peer will get an update, or if it is not time again to reduce we 3029218072Srrs * still get the cwr to the peer. Note we set the override when we 3030218072Srrs * could not find the TSN on the chunk or the destination network. 3031163953Srrs */ 3032218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3033163953Srrs} 3034163953Srrs 3035163953Srrsstatic void 3036218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3037163953Srrs{ 3038163953Srrs /* 3039163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3040163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3041163953Srrs * If so remove it. 3042163953Srrs */ 3043163953Srrs struct sctp_tmit_chunk *chk; 3044163953Srrs struct sctp_ecne_chunk *ecne; 3045218072Srrs int override; 3046218072Srrs uint32_t cwr_tsn; 3047163953Srrs 3048218072Srrs cwr_tsn = ntohl(cp->tsn); 3049218072Srrs 3050218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3051163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3052163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3053163953Srrs continue; 3054163953Srrs } 3055218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3056218072Srrs /* Must be from the right src unless override is set */ 3057218072Srrs continue; 3058218072Srrs } 3059163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3060218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3061163953Srrs /* this covers this ECNE, we can remove it */ 3062163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3063163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3064163953Srrs sctp_next); 3065163953Srrs if (chk->data) { 3066163953Srrs sctp_m_freem(chk->data); 3067163953Srrs chk->data = NULL; 3068163953Srrs } 3069163953Srrs stcb->asoc.ctrl_queue_cnt--; 3070163953Srrs sctp_free_a_chunk(stcb, chk); 3071218072Srrs if (override == 0) { 3072218072Srrs break; 3073218072Srrs } 3074163953Srrs } 3075163953Srrs } 3076163953Srrs} 3077163953Srrs 3078163953Srrsstatic void 3079163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3080163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3081163953Srrs{ 3082163953Srrs struct sctp_association *asoc; 3083163953Srrs 3084172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3085172090Srrs struct socket *so; 3086172090Srrs 3087172090Srrs#endif 3088172090Srrs 3089169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3090169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3091163953Srrs if (stcb == NULL) 3092163953Srrs return; 3093163953Srrs 3094163953Srrs asoc = &stcb->asoc; 3095163953Srrs /* process according to association state */ 3096163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3097163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3098171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3099171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3100163953Srrs SCTP_TCB_UNLOCK(stcb); 3101163953Srrs return; 3102163953Srrs } 3103163953Srrs /* notify upper layer protocol */ 3104163953Srrs if (stcb->sctp_socket) { 3105172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3106163953Srrs /* are the queues empty? they should be */ 3107163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3108163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3109217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3110172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3111163953Srrs } 3112163953Srrs } 3113163953Srrs /* stop the timer */ 3114178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3115163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3116163953Srrs /* free the TCB */ 3117171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3118171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3119172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3120172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3121172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3122172090Srrs SCTP_TCB_UNLOCK(stcb); 3123172090Srrs SCTP_SOCKET_LOCK(so, 1); 3124172090Srrs SCTP_TCB_LOCK(stcb); 3125172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3126172090Srrs#endif 3127171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3128172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3129172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3130172090Srrs#endif 3131163953Srrs return; 3132163953Srrs} 3133163953Srrs 3134163953Srrsstatic int 3135163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3136163953Srrs struct sctp_nets *net, uint8_t flg) 3137163953Srrs{ 3138163953Srrs switch (desc->chunk_type) { 3139211969Stuexen case SCTP_DATA: 3140163953Srrs /* find the tsn to resend (possibly */ 3141163953Srrs { 3142163953Srrs uint32_t tsn; 3143163953Srrs struct sctp_tmit_chunk *tp1; 3144163953Srrs 3145163953Srrs tsn = ntohl(desc->tsn_ifany); 3146216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3147163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3148163953Srrs /* found it */ 3149163953Srrs break; 3150163953Srrs } 3151216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3152163953Srrs /* not found */ 3153163953Srrs tp1 = NULL; 3154163953Srrs break; 3155163953Srrs } 3156163953Srrs } 3157163953Srrs if (tp1 == NULL) { 3158163953Srrs /* 3159163953Srrs * Do it the other way , aka without paying 3160163953Srrs * attention to queue seq order. 3161163953Srrs */ 3162163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3163216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3164163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3165163953Srrs /* found it */ 3166163953Srrs break; 3167163953Srrs } 3168163953Srrs } 3169163953Srrs } 3170163953Srrs if (tp1 == NULL) { 3171163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3172163953Srrs } 3173163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3174163953Srrs uint8_t *ddp; 3175163953Srrs 3176214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3177214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3178214877Stuexen return (0); 3179214877Stuexen } 3180163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3181163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3182163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3183163953Srrs return (0); 3184163953Srrs } 3185163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3186163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3187163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3188163953Srrs return (0); 3189163953Srrs } 3190163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3191163953Srrs sizeof(struct sctp_data_chunk)); 3192163953Srrs { 3193163953Srrs unsigned int iii; 3194163953Srrs 3195163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3196163953Srrs iii++) { 3197163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3198163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3199163953Srrs return (-1); 3200163953Srrs } 3201163953Srrs } 3202163953Srrs } 3203163953Srrs 3204163953Srrs if (tp1->do_rtt) { 3205163953Srrs /* 3206163953Srrs * this guy had a RTO calculation 3207163953Srrs * pending on it, cancel it 3208163953Srrs */ 3209163953Srrs tp1->do_rtt = 0; 3210163953Srrs } 3211163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3212163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3213163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3214163953Srrs /* 3215163953Srrs * mark it as if we were doing a FR, since 3216163953Srrs * we will be getting gap ack reports behind 3217163953Srrs * the info from the router. 3218163953Srrs */ 3219163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3220163953Srrs /* 3221163953Srrs * mark the tsn with what sequences can 3222163953Srrs * cause a new FR. 3223163953Srrs */ 3224163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3225163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3226163953Srrs } else { 3227163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3228163953Srrs } 3229163953Srrs 3230163953Srrs /* restart the timer */ 3231163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3232171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3233163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3234163953Srrs stcb, tp1->whoTo); 3235163953Srrs 3236163953Srrs /* fix counts and things */ 3237179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3238170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3239170744Srrs tp1->whoTo->flight_size, 3240170744Srrs tp1->book_size, 3241170744Srrs (uintptr_t) stcb, 3242170744Srrs tp1->rec.data.TSN_seq); 3243170744Srrs } 3244190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3245190689Srrs sctp_flight_size_decrease(tp1); 3246190689Srrs sctp_total_flight_decrease(stcb, tp1); 3247190689Srrs } 3248216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3249163953Srrs } { 3250163953Srrs /* audit code */ 3251163953Srrs unsigned int audit; 3252163953Srrs 3253163953Srrs audit = 0; 3254163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3255163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3256163953Srrs audit++; 3257163953Srrs } 3258163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3259163953Srrs sctp_next) { 3260163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3261163953Srrs audit++; 3262163953Srrs } 3263163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3264169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3265163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3266163953Srrs#ifndef SCTP_AUDITING_ENABLED 3267163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3268163953Srrs#endif 3269163953Srrs } 3270163953Srrs } 3271163953Srrs } 3272163953Srrs break; 3273163953Srrs case SCTP_ASCONF: 3274163953Srrs { 3275163953Srrs struct sctp_tmit_chunk *asconf; 3276163953Srrs 3277163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3278163953Srrs sctp_next) { 3279163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3280163953Srrs break; 3281163953Srrs } 3282163953Srrs } 3283163953Srrs if (asconf) { 3284163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3285163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3286163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3287163953Srrs asconf->snd_count--; 3288163953Srrs } 3289163953Srrs } 3290163953Srrs break; 3291163953Srrs case SCTP_INITIATION: 3292163953Srrs /* resend the INIT */ 3293163953Srrs stcb->asoc.dropped_special_cnt++; 3294163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3295163953Srrs /* 3296163953Srrs * If we can get it in, in a few attempts we do 3297163953Srrs * this, otherwise we let the timer fire. 3298163953Srrs */ 3299163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3300171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3301172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3302163953Srrs } 3303163953Srrs break; 3304163953Srrs case SCTP_SELECTIVE_ACK: 3305206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3306163953Srrs /* resend the sack */ 3307163953Srrs sctp_send_sack(stcb); 3308163953Srrs break; 3309163953Srrs case SCTP_HEARTBEAT_REQUEST: 3310163953Srrs /* resend a demand HB */ 3311171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3312171440Srrs /* 3313171440Srrs * Only retransmit if we KNOW we wont destroy the 3314171440Srrs * tcb 3315171440Srrs */ 3316171440Srrs (void)sctp_send_hb(stcb, 1, net); 3317171440Srrs } 3318163953Srrs break; 3319163953Srrs case SCTP_SHUTDOWN: 3320163953Srrs sctp_send_shutdown(stcb, net); 3321163953Srrs break; 3322163953Srrs case SCTP_SHUTDOWN_ACK: 3323163953Srrs sctp_send_shutdown_ack(stcb, net); 3324163953Srrs break; 3325163953Srrs case SCTP_COOKIE_ECHO: 3326163953Srrs { 3327163953Srrs struct sctp_tmit_chunk *cookie; 3328163953Srrs 3329163953Srrs cookie = NULL; 3330163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3331163953Srrs sctp_next) { 3332163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3333163953Srrs break; 3334163953Srrs } 3335163953Srrs } 3336163953Srrs if (cookie) { 3337163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3338163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3339163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3340163953Srrs sctp_stop_all_cookie_timers(stcb); 3341163953Srrs } 3342163953Srrs } 3343163953Srrs break; 3344163953Srrs case SCTP_COOKIE_ACK: 3345163953Srrs sctp_send_cookie_ack(stcb); 3346163953Srrs break; 3347163953Srrs case SCTP_ASCONF_ACK: 3348163953Srrs /* resend last asconf ack */ 3349171990Srrs sctp_send_asconf_ack(stcb); 3350163953Srrs break; 3351163953Srrs case SCTP_FORWARD_CUM_TSN: 3352163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3353163953Srrs break; 3354163953Srrs /* can't do anything with these */ 3355163953Srrs case SCTP_PACKET_DROPPED: 3356163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3357163953Srrs case SCTP_HEARTBEAT_ACK: 3358163953Srrs case SCTP_ABORT_ASSOCIATION: 3359163953Srrs case SCTP_OPERATION_ERROR: 3360163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3361163953Srrs case SCTP_ECN_ECHO: 3362163953Srrs case SCTP_ECN_CWR: 3363163953Srrs default: 3364163953Srrs break; 3365163953Srrs } 3366163953Srrs return (0); 3367163953Srrs} 3368163953Srrs 3369163953Srrsvoid 3370163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3371163953Srrs{ 3372163953Srrs int i; 3373163953Srrs uint16_t temp; 3374163953Srrs 3375163953Srrs /* 3376163953Srrs * We set things to 0xffff since this is the last delivered sequence 3377163953Srrs * and we will be sending in 0 after the reset. 3378163953Srrs */ 3379163953Srrs 3380163953Srrs if (number_entries) { 3381163953Srrs for (i = 0; i < number_entries; i++) { 3382163953Srrs temp = ntohs(list[i]); 3383163953Srrs if (temp >= stcb->asoc.streamincnt) { 3384163953Srrs continue; 3385163953Srrs } 3386163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3387163953Srrs } 3388163953Srrs } else { 3389163953Srrs list = NULL; 3390163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3391163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3392163953Srrs } 3393163953Srrs } 3394172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3395163953Srrs} 3396163953Srrs 3397163953Srrsstatic void 3398163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3399163953Srrs{ 3400163953Srrs int i; 3401163953Srrs 3402163953Srrs if (number_entries == 0) { 3403163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3404163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3405163953Srrs } 3406163953Srrs } else if (number_entries) { 3407163953Srrs for (i = 0; i < number_entries; i++) { 3408163953Srrs uint16_t temp; 3409163953Srrs 3410163953Srrs temp = ntohs(list[i]); 3411163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3412163953Srrs /* no such stream */ 3413163953Srrs continue; 3414163953Srrs } 3415163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3416163953Srrs } 3417163953Srrs } 3418172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3419163953Srrs} 3420163953Srrs 3421163953Srrs 3422163953Srrsstruct sctp_stream_reset_out_request * 3423163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3424163953Srrs{ 3425163953Srrs struct sctp_association *asoc; 3426163953Srrs struct sctp_stream_reset_out_req *req; 3427163953Srrs struct sctp_stream_reset_out_request *r; 3428163953Srrs struct sctp_tmit_chunk *chk; 3429163953Srrs int len, clen; 3430163953Srrs 3431163953Srrs asoc = &stcb->asoc; 3432163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3433169208Srrs asoc->stream_reset_outstanding = 0; 3434163953Srrs return (NULL); 3435163953Srrs } 3436163953Srrs if (stcb->asoc.str_reset == NULL) { 3437169208Srrs asoc->stream_reset_outstanding = 0; 3438163953Srrs return (NULL); 3439163953Srrs } 3440163953Srrs chk = stcb->asoc.str_reset; 3441163953Srrs if (chk->data == NULL) { 3442163953Srrs return (NULL); 3443163953Srrs } 3444163953Srrs if (bchk) { 3445163953Srrs /* he wants a copy of the chk pointer */ 3446163953Srrs *bchk = chk; 3447163953Srrs } 3448163953Srrs clen = chk->send_size; 3449163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3450163953Srrs r = &req->sr_req; 3451163953Srrs if (ntohl(r->request_seq) == seq) { 3452163953Srrs /* found it */ 3453163953Srrs return (r); 3454163953Srrs } 3455163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3456163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3457163953Srrs /* move to the next one, there can only be a max of two */ 3458163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3459163953Srrs if (ntohl(r->request_seq) == seq) { 3460163953Srrs return (r); 3461163953Srrs } 3462163953Srrs } 3463163953Srrs /* that seq is not here */ 3464163953Srrs return (NULL); 3465163953Srrs} 3466163953Srrs 3467163953Srrsstatic void 3468163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3469163953Srrs{ 3470163953Srrs struct sctp_association *asoc; 3471163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3472163953Srrs 3473163953Srrs if (stcb->asoc.str_reset == NULL) { 3474163953Srrs return; 3475163953Srrs } 3476166675Srrs asoc = &stcb->asoc; 3477166675Srrs 3478171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3479163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3480163953Srrs chk, 3481163953Srrs sctp_next); 3482163953Srrs if (chk->data) { 3483163953Srrs sctp_m_freem(chk->data); 3484163953Srrs chk->data = NULL; 3485163953Srrs } 3486163953Srrs asoc->ctrl_queue_cnt--; 3487163953Srrs sctp_free_a_chunk(stcb, chk); 3488172156Srrs /* sa_ignore NO_NULL_CHK */ 3489163953Srrs stcb->asoc.str_reset = NULL; 3490163953Srrs} 3491163953Srrs 3492163953Srrs 3493163953Srrsstatic int 3494163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3495163953Srrs uint32_t seq, uint32_t action, 3496164139Srrs struct sctp_stream_reset_response *respin) 3497163953Srrs{ 3498163953Srrs uint16_t type; 3499163953Srrs int lparm_len; 3500163953Srrs struct sctp_association *asoc = &stcb->asoc; 3501163953Srrs struct sctp_tmit_chunk *chk; 3502163953Srrs struct sctp_stream_reset_out_request *srparam; 3503163953Srrs int number_entries; 3504163953Srrs 3505163953Srrs if (asoc->stream_reset_outstanding == 0) { 3506163953Srrs /* duplicate */ 3507163953Srrs return (0); 3508163953Srrs } 3509163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3510163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3511163953Srrs if (srparam) { 3512163953Srrs stcb->asoc.str_reset_seq_out++; 3513163953Srrs type = ntohs(srparam->ph.param_type); 3514163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3515163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3516164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3517163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3518163953Srrs if (asoc->stream_reset_outstanding) 3519163953Srrs asoc->stream_reset_outstanding--; 3520163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3521163953Srrs /* do it */ 3522163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3523163953Srrs } else { 3524172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3525163953Srrs } 3526163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3527163953Srrs /* Answered my request */ 3528164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3529163953Srrs if (asoc->stream_reset_outstanding) 3530163953Srrs asoc->stream_reset_outstanding--; 3531163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3532172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3533163953Srrs } 3534188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3535188854Srrs /* Ok we now may have more streams */ 3536189121Srrs if (asoc->stream_reset_outstanding) 3537189121Srrs asoc->stream_reset_outstanding--; 3538188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3539188854Srrs /* Put the new streams into effect */ 3540188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3541188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3542188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3543188854Srrs } else { 3544188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3545188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3546188854Srrs } 3547163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3548163953Srrs /** 3549163953Srrs * a) Adopt the new in tsn. 3550163953Srrs * b) reset the map 3551163953Srrs * c) Adopt the new out-tsn 3552163953Srrs */ 3553163953Srrs struct sctp_stream_reset_response_tsn *resp; 3554163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3555163953Srrs int abort_flag = 0; 3556163953Srrs 3557163953Srrs if (respin == NULL) { 3558163953Srrs /* huh ? */ 3559163953Srrs return (0); 3560163953Srrs } 3561163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3562163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3563163953Srrs asoc->stream_reset_outstanding--; 3564163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3565163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3566163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3567170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3568163953Srrs if (abort_flag) { 3569163953Srrs return (1); 3570163953Srrs } 3571163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3572179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3573179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3574179783Srrs } 3575180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3576163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3577163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3578185694Srrs 3579205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3580206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3581205627Srrs 3582163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3583163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3584163953Srrs 3585163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3586163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3587163953Srrs 3588163953Srrs } 3589163953Srrs } 3590163953Srrs /* get rid of the request and get the request flags */ 3591163953Srrs if (asoc->stream_reset_outstanding == 0) { 3592163953Srrs sctp_clean_up_stream_reset(stcb); 3593163953Srrs } 3594163953Srrs } 3595163953Srrs } 3596163953Srrs return (0); 3597163953Srrs} 3598163953Srrs 3599163953Srrsstatic void 3600163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3601163953Srrs struct sctp_tmit_chunk *chk, 3602170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3603163953Srrs{ 3604163953Srrs uint32_t seq; 3605163953Srrs int len, i; 3606163953Srrs int number_entries; 3607163953Srrs uint16_t temp; 3608163953Srrs 3609163953Srrs /* 3610163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3611163953Srrs * seq_in is right. 3612163953Srrs */ 3613163953Srrs struct sctp_association *asoc = &stcb->asoc; 3614163953Srrs 3615163953Srrs seq = ntohl(req->request_seq); 3616163953Srrs if (asoc->str_reset_seq_in == seq) { 3617170992Srrs if (trunc) { 3618170992Srrs /* Can't do it, since they exceeded our buffer size */ 3619170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3620170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3621170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3622170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3623163953Srrs len = ntohs(req->ph.param_length); 3624163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3625163953Srrs for (i = 0; i < number_entries; i++) { 3626163953Srrs temp = ntohs(req->list_of_streams[i]); 3627163953Srrs req->list_of_streams[i] = temp; 3628163953Srrs } 3629163953Srrs /* move the reset action back one */ 3630163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3631163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3632163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3633163953Srrs asoc->str_reset_seq_out, 3634163953Srrs seq, (asoc->sending_seq - 1)); 3635163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3636163953Srrs asoc->str_reset = chk; 3637163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3638163953Srrs stcb->asoc.stream_reset_outstanding++; 3639163953Srrs } else { 3640163953Srrs /* Can't do it, since we have sent one out */ 3641163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3642163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3643163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3644163953Srrs } 3645163953Srrs asoc->str_reset_seq_in++; 3646163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3647163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3648163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3649163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3650163953Srrs } else { 3651163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3652163953Srrs } 3653163953Srrs} 3654163953Srrs 3655163953Srrsstatic int 3656163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3657163953Srrs struct sctp_tmit_chunk *chk, 3658163953Srrs struct sctp_stream_reset_tsn_request *req) 3659163953Srrs{ 3660163953Srrs /* reset all in and out and update the tsn */ 3661163953Srrs /* 3662163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3663163953Srrs * and set cum-ack to it. Also process this selected number as a 3664163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3665163953Srrs */ 3666163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3667163953Srrs struct sctp_association *asoc = &stcb->asoc; 3668163953Srrs int abort_flag = 0; 3669163953Srrs uint32_t seq; 3670163953Srrs 3671163953Srrs seq = ntohl(req->request_seq); 3672163953Srrs if (asoc->str_reset_seq_in == seq) { 3673163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3674163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3675163953Srrs fwdtsn.ch.chunk_flags = 0; 3676163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3677170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3678163953Srrs if (abort_flag) { 3679163953Srrs return (1); 3680163953Srrs } 3681163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3682179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3683179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3684179783Srrs } 3685180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3686163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3687163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3688205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3689206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3690163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3691163953Srrs /* save off historical data for retrans */ 3692163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3693163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3694163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3695163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3696163953Srrs 3697163953Srrs sctp_add_stream_reset_result_tsn(chk, 3698163953Srrs ntohl(req->request_seq), 3699163953Srrs SCTP_STREAM_RESET_PERFORMED, 3700163953Srrs stcb->asoc.sending_seq, 3701163953Srrs stcb->asoc.mapping_array_base_tsn); 3702163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3703163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3704163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3705163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3706163953Srrs 3707163953Srrs asoc->str_reset_seq_in++; 3708163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3709163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3710163953Srrs stcb->asoc.last_sending_seq[0], 3711163953Srrs stcb->asoc.last_base_tsnsent[0] 3712163953Srrs ); 3713163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3714163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3715163953Srrs stcb->asoc.last_sending_seq[1], 3716163953Srrs stcb->asoc.last_base_tsnsent[1] 3717163953Srrs ); 3718163953Srrs } else { 3719163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3720163953Srrs } 3721163953Srrs return (0); 3722163953Srrs} 3723163953Srrs 3724163953Srrsstatic void 3725163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3726163953Srrs struct sctp_tmit_chunk *chk, 3727170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3728163953Srrs{ 3729163953Srrs uint32_t seq, tsn; 3730163953Srrs int number_entries, len; 3731163953Srrs struct sctp_association *asoc = &stcb->asoc; 3732163953Srrs 3733163953Srrs seq = ntohl(req->request_seq); 3734163953Srrs 3735163953Srrs /* now if its not a duplicate we process it */ 3736163953Srrs if (asoc->str_reset_seq_in == seq) { 3737163953Srrs len = ntohs(req->ph.param_length); 3738163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3739163953Srrs /* 3740163953Srrs * the sender is resetting, handle the list issue.. we must 3741163953Srrs * a) verify if we can do the reset, if so no problem b) If 3742163953Srrs * we can't do the reset we must copy the request. c) queue 3743163953Srrs * it, and setup the data in processor to trigger it off 3744163953Srrs * when needed and dequeue all the queued data. 3745163953Srrs */ 3746163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3747163953Srrs 3748163953Srrs /* move the reset action back one */ 3749163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3750170992Srrs if (trunc) { 3751170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3752170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3753216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3754163953Srrs /* we can do it now */ 3755163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3756163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3757163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3758163953Srrs } else { 3759163953Srrs /* 3760163953Srrs * we must queue it up and thus wait for the TSN's 3761163953Srrs * to arrive that are at or before tsn 3762163953Srrs */ 3763163953Srrs struct sctp_stream_reset_list *liste; 3764163953Srrs int siz; 3765163953Srrs 3766163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3767163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3768170091Srrs siz, SCTP_M_STRESET); 3769163953Srrs if (liste == NULL) { 3770163953Srrs /* gak out of memory */ 3771163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3772163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3773163953Srrs return; 3774163953Srrs } 3775163953Srrs liste->tsn = tsn; 3776163953Srrs liste->number_entries = number_entries; 3777163953Srrs memcpy(&liste->req, req, 3778163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3779163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3780163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3781163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3782163953Srrs } 3783163953Srrs asoc->str_reset_seq_in++; 3784163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3785163953Srrs /* 3786163953Srrs * one seq back, just echo back last action since my 3787163953Srrs * response was lost. 3788163953Srrs */ 3789163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3790163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3791163953Srrs /* 3792163953Srrs * two seq back, just echo back last action since my 3793163953Srrs * response was lost. 3794163953Srrs */ 3795163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3796163953Srrs } else { 3797163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3798163953Srrs } 3799163953Srrs} 3800163953Srrs 3801188854Srrsstatic void 3802188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3803188854Srrs struct sctp_stream_reset_add_strm *str_add) 3804188854Srrs{ 3805188854Srrs /* 3806188854Srrs * Peer is requesting to add more streams. If its within our 3807188854Srrs * max-streams we will allow it. 3808188854Srrs */ 3809188854Srrs uint16_t num_stream, i; 3810188854Srrs uint32_t seq; 3811189121Srrs struct sctp_association *asoc = &stcb->asoc; 3812216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3813188854Srrs 3814188854Srrs /* Get the number. */ 3815188854Srrs seq = ntohl(str_add->request_seq); 3816188854Srrs num_stream = ntohs(str_add->number_of_streams); 3817188854Srrs /* Now what would be the new total? */ 3818189121Srrs if (asoc->str_reset_seq_in == seq) { 3819189121Srrs num_stream += stcb->asoc.streamincnt; 3820189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3821189121Srrs /* We must reject it they ask for to many */ 3822189121Srrs denied: 3823189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3824189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3825189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3826189121Srrs } else { 3827189121Srrs /* Ok, we can do that :-) */ 3828189121Srrs struct sctp_stream_in *oldstrm; 3829189121Srrs 3830189121Srrs /* save off the old */ 3831189121Srrs oldstrm = stcb->asoc.strmin; 3832189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3833189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3834189121Srrs SCTP_M_STRMI); 3835189121Srrs if (stcb->asoc.strmin == NULL) { 3836189121Srrs stcb->asoc.strmin = oldstrm; 3837189121Srrs goto denied; 3838189121Srrs } 3839189121Srrs /* copy off the old data */ 3840189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3841189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3842189121Srrs stcb->asoc.strmin[i].stream_no = i; 3843189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3844189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3845189121Srrs /* now anything on those queues? */ 3846216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3847189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3848189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3849189121Srrs } 3850189121Srrs } 3851189121Srrs /* Init the new streams */ 3852189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3853189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3854189121Srrs stcb->asoc.strmin[i].stream_no = i; 3855189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3856189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3857189121Srrs } 3858189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3859189121Srrs /* update the size */ 3860189121Srrs stcb->asoc.streamincnt = num_stream; 3861189121Srrs /* Send the ack */ 3862189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3863189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3864189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3865189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3866189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3867189121Srrs } 3868189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3869189121Srrs /* 3870189121Srrs * one seq back, just echo back last action since my 3871189121Srrs * response was lost. 3872189121Srrs */ 3873189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3874189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3875189121Srrs /* 3876189121Srrs * two seq back, just echo back last action since my 3877189121Srrs * response was lost. 3878189121Srrs */ 3879189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3880188854Srrs } else { 3881189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3882188854Srrs 3883188854Srrs } 3884188854Srrs} 3885188854Srrs 3886170992Srrs#ifdef __GNUC__ 3887170992Srrs__attribute__((noinline)) 3888170992Srrs#endif 3889170992Srrs static int 3890170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3891170992Srrs struct sctp_stream_reset_out_req *sr_req) 3892163953Srrs{ 3893163953Srrs int chk_length, param_len, ptype; 3894170992Srrs struct sctp_paramhdr pstore; 3895170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3896170992Srrs 3897163953Srrs uint32_t seq; 3898163953Srrs int num_req = 0; 3899170992Srrs int trunc = 0; 3900163953Srrs struct sctp_tmit_chunk *chk; 3901163953Srrs struct sctp_chunkhdr *ch; 3902163953Srrs struct sctp_paramhdr *ph; 3903166675Srrs int ret_code = 0; 3904166675Srrs int num_param = 0; 3905163953Srrs 3906163953Srrs /* now it may be a reset or a reset-response */ 3907163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3908163953Srrs 3909163953Srrs /* setup for adding the response */ 3910163953Srrs sctp_alloc_a_chunk(stcb, chk); 3911163953Srrs if (chk == NULL) { 3912163953Srrs return (ret_code); 3913163953Srrs } 3914163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3915169208Srrs chk->rec.chunk_id.can_take_data = 0; 3916163953Srrs chk->asoc = &stcb->asoc; 3917163953Srrs chk->no_fr_allowed = 0; 3918163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3919166023Srrs chk->book_size_scale = 0; 3920165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3921163953Srrs if (chk->data == NULL) { 3922163953Srrsstrres_nochunk: 3923163953Srrs if (chk->data) { 3924163953Srrs sctp_m_freem(chk->data); 3925163953Srrs chk->data = NULL; 3926163953Srrs } 3927163953Srrs sctp_free_a_chunk(stcb, chk); 3928163953Srrs return (ret_code); 3929163953Srrs } 3930165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3931163953Srrs 3932163953Srrs /* setup chunk parameters */ 3933163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3934163953Srrs chk->snd_count = 0; 3935163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3936163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3937163953Srrs 3938163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3939163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3940163953Srrs ch->chunk_flags = 0; 3941163953Srrs ch->chunk_length = htons(chk->send_size); 3942165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3943170992Srrs offset += sizeof(struct sctp_chunkhdr); 3944163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3945170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3946170992Srrs if (ph == NULL) 3947170992Srrs break; 3948163953Srrs param_len = ntohs(ph->param_length); 3949163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3950163953Srrs /* bad param */ 3951163953Srrs break; 3952163953Srrs } 3953170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3954170992Srrs (uint8_t *) & cstore); 3955163953Srrs ptype = ntohs(ph->param_type); 3956163953Srrs num_param++; 3957170994Srrs if (param_len > (int)sizeof(cstore)) { 3958170992Srrs trunc = 1; 3959170992Srrs } else { 3960170992Srrs trunc = 0; 3961170992Srrs } 3962170992Srrs 3963163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3964163953Srrs /* hit the max of parameters already sorry.. */ 3965163953Srrs break; 3966163953Srrs } 3967163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3968163953Srrs struct sctp_stream_reset_out_request *req_out; 3969163953Srrs 3970163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3971163953Srrs num_req++; 3972163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3973163953Srrs seq = ntohl(req_out->response_seq); 3974163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3975163953Srrs /* implicit ack */ 3976169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3977163953Srrs } 3978163953Srrs } 3979170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3980188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3981188854Srrs struct sctp_stream_reset_add_strm *str_add; 3982188854Srrs 3983188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3984188854Srrs num_req++; 3985188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 3986163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3987163953Srrs struct sctp_stream_reset_in_request *req_in; 3988163953Srrs 3989163953Srrs num_req++; 3990170992Srrs 3991163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3992170992Srrs 3993170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3994163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3995163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3996163953Srrs 3997163953Srrs num_req++; 3998163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3999170992Srrs 4000163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4001163953Srrs ret_code = 1; 4002163953Srrs goto strres_nochunk; 4003163953Srrs } 4004163953Srrs /* no more */ 4005163953Srrs break; 4006163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4007163953Srrs struct sctp_stream_reset_response *resp; 4008163953Srrs uint32_t result; 4009163953Srrs 4010163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4011163953Srrs seq = ntohl(resp->response_seq); 4012163953Srrs result = ntohl(resp->result); 4013163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4014163953Srrs ret_code = 1; 4015163953Srrs goto strres_nochunk; 4016163953Srrs } 4017163953Srrs } else { 4018163953Srrs break; 4019163953Srrs } 4020170992Srrs offset += SCTP_SIZE32(param_len); 4021163953Srrs chk_length -= SCTP_SIZE32(param_len); 4022163953Srrs } 4023163953Srrs if (num_req == 0) { 4024163953Srrs /* we have no response free the stuff */ 4025163953Srrs goto strres_nochunk; 4026163953Srrs } 4027163953Srrs /* ok we have a chunk to link in */ 4028163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4029163953Srrs chk, 4030163953Srrs sctp_next); 4031163953Srrs stcb->asoc.ctrl_queue_cnt++; 4032163953Srrs return (ret_code); 4033163953Srrs} 4034163953Srrs 4035163953Srrs/* 4036163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4037163953Srrs * to handle this, either we get the whole packet and must disect it 4038163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4039163953Srrs * from a middle box that did the disectting for us. 4040163953Srrs */ 4041163953Srrsstatic void 4042163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4043170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4044163953Srrs{ 4045163953Srrs uint32_t bottle_bw, on_queue; 4046163953Srrs uint16_t trunc_len; 4047163953Srrs unsigned int chlen; 4048163953Srrs unsigned int at; 4049163953Srrs struct sctp_chunk_desc desc; 4050163953Srrs struct sctp_chunkhdr *ch; 4051163953Srrs 4052163953Srrs chlen = ntohs(cp->ch.chunk_length); 4053163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4054163953Srrs /* XXX possible chlen underflow */ 4055163953Srrs if (chlen == 0) { 4056163953Srrs ch = NULL; 4057163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4058163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4059163953Srrs } else { 4060163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4061163953Srrs chlen -= sizeof(struct sctphdr); 4062163953Srrs /* XXX possible chlen underflow */ 4063163953Srrs memset(&desc, 0, sizeof(desc)); 4064163953Srrs } 4065163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4066170781Srrs if (trunc_len > limit) { 4067170781Srrs trunc_len = limit; 4068170781Srrs } 4069163953Srrs /* now the chunks themselves */ 4070163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4071163953Srrs desc.chunk_type = ch->chunk_type; 4072163953Srrs /* get amount we need to move */ 4073163953Srrs at = ntohs(ch->chunk_length); 4074163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4075163953Srrs /* corrupt chunk, maybe at the end? */ 4076163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4077163953Srrs break; 4078163953Srrs } 4079163953Srrs if (trunc_len == 0) { 4080163953Srrs /* we are supposed to have all of it */ 4081163953Srrs if (at > chlen) { 4082163953Srrs /* corrupt skip it */ 4083163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4084163953Srrs break; 4085163953Srrs } 4086163953Srrs } else { 4087163953Srrs /* is there enough of it left ? */ 4088163953Srrs if (desc.chunk_type == SCTP_DATA) { 4089163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4090163953Srrs sizeof(desc.data_bytes))) { 4091163953Srrs break; 4092163953Srrs } 4093163953Srrs } else { 4094163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4095163953Srrs break; 4096163953Srrs } 4097163953Srrs } 4098163953Srrs } 4099163953Srrs if (desc.chunk_type == SCTP_DATA) { 4100163953Srrs /* can we get out the tsn? */ 4101163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4102163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4103163953Srrs 4104163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4105163953Srrs /* yep */ 4106163953Srrs struct sctp_data_chunk *dcp; 4107163953Srrs uint8_t *ddp; 4108163953Srrs unsigned int iii; 4109163953Srrs 4110163953Srrs dcp = (struct sctp_data_chunk *)ch; 4111163953Srrs ddp = (uint8_t *) (dcp + 1); 4112163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4113163953Srrs desc.data_bytes[iii] = ddp[iii]; 4114163953Srrs } 4115163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4116163953Srrs } else { 4117163953Srrs /* nope we are done. */ 4118163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4119163953Srrs break; 4120163953Srrs } 4121163953Srrs } else { 4122163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4123163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4124163953Srrs } 4125163953Srrs 4126163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4127163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4128163953Srrs break; 4129163953Srrs } 4130163953Srrs if (SCTP_SIZE32(at) > chlen) { 4131163953Srrs break; 4132163953Srrs } 4133163953Srrs chlen -= SCTP_SIZE32(at); 4134163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4135163953Srrs /* done, none left */ 4136163953Srrs break; 4137163953Srrs } 4138163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4139163953Srrs } 4140163953Srrs /* Now update any rwnd --- possibly */ 4141163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4142163953Srrs /* From a peer, we get a rwnd report */ 4143163953Srrs uint32_t a_rwnd; 4144163953Srrs 4145163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4146163953Srrs 4147163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4148163953Srrs on_queue = ntohl(cp->current_onq); 4149163953Srrs if (bottle_bw && on_queue) { 4150163953Srrs /* a rwnd report is in here */ 4151163953Srrs if (bottle_bw > on_queue) 4152163953Srrs a_rwnd = bottle_bw - on_queue; 4153163953Srrs else 4154163953Srrs a_rwnd = 0; 4155163953Srrs 4156163953Srrs if (a_rwnd == 0) 4157163953Srrs stcb->asoc.peers_rwnd = 0; 4158163953Srrs else { 4159163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4160163953Srrs stcb->asoc.peers_rwnd = 4161163953Srrs a_rwnd - stcb->asoc.total_flight; 4162163953Srrs } else { 4163163953Srrs stcb->asoc.peers_rwnd = 0; 4164163953Srrs } 4165163953Srrs if (stcb->asoc.peers_rwnd < 4166163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4167163953Srrs /* SWS sender side engages */ 4168163953Srrs stcb->asoc.peers_rwnd = 0; 4169163953Srrs } 4170163953Srrs } 4171163953Srrs } 4172163953Srrs } else { 4173163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4174163953Srrs } 4175163953Srrs 4176163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4177163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4178163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4179163953Srrs (stcb->asoc.sat_network)) { 4180163953Srrs /* 4181163953Srrs * This is debateable but for sat networks it makes sense 4182163953Srrs * Note if a T3 timer has went off, we will prohibit any 4183163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4184163953Srrs */ 4185171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4186171440Srrs net, cp, &bottle_bw, &on_queue); 4187163953Srrs } 4188163953Srrs} 4189163953Srrs 4190163953Srrs/* 4191163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4192163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4193163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4194163953Srrs * length of the complete packet outputs: - length: modified to remaining 4195163953Srrs * length after control processing - netp: modified to new sctp_nets after 4196163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4197163953Srrs * bad packet,...) otherwise return the tcb for this packet 4198163953Srrs */ 4199170099Srrs#ifdef __GNUC__ 4200170099Srrs__attribute__((noinline)) 4201170099Srrs#endif 4202170099Srrs static struct sctp_tcb * 4203170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4204170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4205170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4206179157Srrs uint32_t vrf_id, uint16_t port) 4207163953Srrs{ 4208163953Srrs struct sctp_association *asoc; 4209163953Srrs uint32_t vtag_in; 4210163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4211170140Srrs uint32_t chk_length; 4212163953Srrs int ret; 4213168299Srrs int abort_no_unlock = 0; 4214218186Srrs int ecne_seen = 0; 4215163953Srrs 4216163953Srrs /* 4217163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4218163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4219163953Srrs * until we get into jumbo grams and such.. 4220163953Srrs */ 4221166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4222163953Srrs struct sctp_tcb *locked_tcb = stcb; 4223163953Srrs int got_auth = 0; 4224163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4225163953Srrs int auth_skipped = 0; 4226171990Srrs int asconf_cnt = 0; 4227163953Srrs 4228172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4229172090Srrs struct socket *so; 4230172090Srrs 4231172090Srrs#endif 4232172090Srrs 4233169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4234169420Srrs iphlen, *offset, length, stcb); 4235163953Srrs 4236163953Srrs /* validate chunk header length... */ 4237163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4238170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4239170056Srrs ntohs(ch->chunk_length)); 4240170091Srrs if (locked_tcb) { 4241170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4242170091Srrs } 4243163953Srrs return (NULL); 4244163953Srrs } 4245163953Srrs /* 4246163953Srrs * validate the verification tag 4247163953Srrs */ 4248163953Srrs vtag_in = ntohl(sh->v_tag); 4249163953Srrs 4250165220Srrs if (locked_tcb) { 4251165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4252165220Srrs } 4253163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4254170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4255170056Srrs ntohs(ch->chunk_length), vtag_in); 4256163953Srrs if (vtag_in != 0) { 4257163953Srrs /* protocol error- silently discard... */ 4258163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4259169378Srrs if (locked_tcb) { 4260163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4261169378Srrs } 4262163953Srrs return (NULL); 4263163953Srrs } 4264163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4265163953Srrs /* 4266163953Srrs * If there is no stcb, skip the AUTH chunk and process 4267163953Srrs * later after a stcb is found (to validate the lookup was 4268163953Srrs * valid. 4269163953Srrs */ 4270163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4271179783Srrs (stcb == NULL) && 4272179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4273163953Srrs /* save this chunk for later processing */ 4274163953Srrs auth_skipped = 1; 4275163953Srrs auth_offset = *offset; 4276163953Srrs auth_len = ntohs(ch->chunk_length); 4277163953Srrs 4278163953Srrs /* (temporarily) move past this chunk */ 4279163953Srrs *offset += SCTP_SIZE32(auth_len); 4280163953Srrs if (*offset >= length) { 4281163953Srrs /* no more data left in the mbuf chain */ 4282163953Srrs *offset = length; 4283170091Srrs if (locked_tcb) { 4284170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4285170091Srrs } 4286163953Srrs return (NULL); 4287163953Srrs } 4288163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4289163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4290163953Srrs } 4291169420Srrs if (ch == NULL) { 4292169420Srrs /* Help */ 4293169420Srrs *offset = length; 4294170091Srrs if (locked_tcb) { 4295170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4296170091Srrs } 4297169420Srrs return (NULL); 4298169420Srrs } 4299163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4300163953Srrs goto process_control_chunks; 4301163953Srrs } 4302163953Srrs /* 4303163953Srrs * first check if it's an ASCONF with an unknown src addr we 4304163953Srrs * need to look inside to find the association 4305163953Srrs */ 4306163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4307171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4308171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4309171990Srrs 4310163953Srrs /* inp's refcount may be reduced */ 4311163953Srrs SCTP_INP_INCR_REF(inp); 4312163953Srrs 4313171990Srrs asconf_offset = *offset; 4314171990Srrs do { 4315171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4316171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4317171990Srrs break; 4318171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4319185694Srrs *offset, sh, &inp, netp, vrf_id); 4320171990Srrs if (stcb != NULL) 4321171990Srrs break; 4322171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4323171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4324171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4325171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4326163953Srrs if (stcb == NULL) { 4327163953Srrs /* 4328163953Srrs * reduce inp's refcount if not reduced in 4329163953Srrs * sctp_findassociation_ep_asconf(). 4330163953Srrs */ 4331163953Srrs SCTP_INP_DECR_REF(inp); 4332171990Srrs } else { 4333171990Srrs locked_tcb = stcb; 4334163953Srrs } 4335171990Srrs 4336163953Srrs /* now go back and verify any auth chunk to be sure */ 4337163953Srrs if (auth_skipped && (stcb != NULL)) { 4338163953Srrs struct sctp_auth_chunk *auth; 4339163953Srrs 4340163953Srrs auth = (struct sctp_auth_chunk *) 4341163953Srrs sctp_m_getptr(m, auth_offset, 4342163953Srrs auth_len, chunk_buf); 4343163953Srrs got_auth = 1; 4344163953Srrs auth_skipped = 0; 4345169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4346163953Srrs auth_offset)) { 4347163953Srrs /* auth HMAC failed so dump it */ 4348163953Srrs *offset = length; 4349170091Srrs if (locked_tcb) { 4350170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4351170091Srrs } 4352163953Srrs return (NULL); 4353163953Srrs } else { 4354163953Srrs /* remaining chunks are HMAC checked */ 4355163953Srrs stcb->asoc.authenticated = 1; 4356163953Srrs } 4357163953Srrs } 4358163953Srrs } 4359163953Srrs if (stcb == NULL) { 4360163953Srrs /* no association, so it's out of the blue... */ 4361169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4362179157Srrs vrf_id, port); 4363163953Srrs *offset = length; 4364169378Srrs if (locked_tcb) { 4365163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4366169378Srrs } 4367163953Srrs return (NULL); 4368163953Srrs } 4369163953Srrs asoc = &stcb->asoc; 4370163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4371163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4372163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4373163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4374163953Srrs if ((vtag_in == asoc->my_vtag) || 4375163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4376163953Srrs (vtag_in == asoc->peer_vtag))) { 4377163953Srrs /* this is valid */ 4378163953Srrs } else { 4379163953Srrs /* drop this packet... */ 4380163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4381169378Srrs if (locked_tcb) { 4382163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4383169378Srrs } 4384163953Srrs return (NULL); 4385163953Srrs } 4386163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4387163953Srrs if (vtag_in != asoc->my_vtag) { 4388163953Srrs /* 4389163953Srrs * this could be a stale SHUTDOWN-ACK or the 4390163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4391163953Srrs * is still hung; we have started a new asoc 4392163953Srrs * but it won't complete until the shutdown 4393163953Srrs * is completed 4394163953Srrs */ 4395169378Srrs if (locked_tcb) { 4396163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4397169378Srrs } 4398163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4399179157Srrs NULL, vrf_id, port); 4400163953Srrs return (NULL); 4401163953Srrs } 4402163953Srrs } else { 4403163953Srrs /* for all other chunks, vtag must match */ 4404163953Srrs if (vtag_in != asoc->my_vtag) { 4405163953Srrs /* invalid vtag... */ 4406169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4407169420Srrs "invalid vtag: %xh, expect %xh\n", 4408169420Srrs vtag_in, asoc->my_vtag); 4409163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4410169378Srrs if (locked_tcb) { 4411163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4412169378Srrs } 4413163953Srrs *offset = length; 4414163953Srrs return (NULL); 4415163953Srrs } 4416163953Srrs } 4417163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4418163953Srrs /* 4419163953Srrs * process all control chunks... 4420163953Srrs */ 4421163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4422185694Srrs /* EY */ 4423185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4424163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4425163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4426163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4427179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4428171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4429171943Srrs stcb->asoc.overall_error_count, 4430171943Srrs 0, 4431171943Srrs SCTP_FROM_SCTP_INPUT, 4432171943Srrs __LINE__); 4433171943Srrs } 4434163953Srrs stcb->asoc.overall_error_count = 0; 4435163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4436163953Srrs *netp); 4437163953Srrs } 4438163953Srrsprocess_control_chunks: 4439163953Srrs while (IS_SCTP_CONTROL(ch)) { 4440163953Srrs /* validate chunk length */ 4441163953Srrs chk_length = ntohs(ch->chunk_length); 4442169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4443169420Srrs ch->chunk_type, chk_length); 4444170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4445170140Srrs if (chk_length < sizeof(*ch) || 4446170140Srrs (*offset + (int)chk_length) > length) { 4447163953Srrs *offset = length; 4448169378Srrs if (locked_tcb) { 4449163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4450169378Srrs } 4451163953Srrs return (NULL); 4452163953Srrs } 4453163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4454163953Srrs /* 4455163953Srrs * INIT-ACK only gets the init ack "header" portion only 4456163953Srrs * because we don't have to process the peer's COOKIE. All 4457163953Srrs * others get a complete chunk. 4458163953Srrs */ 4459169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4460169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4461163953Srrs /* get an init-ack chunk */ 4462163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4463163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4464163953Srrs if (ch == NULL) { 4465163953Srrs *offset = length; 4466169378Srrs if (locked_tcb) { 4467163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4468169378Srrs } 4469163953Srrs return (NULL); 4470163953Srrs } 4471170642Srrs } else { 4472170786Srrs /* For cookies and all other chunks. */ 4473169208Srrs if (chk_length > sizeof(chunk_buf)) { 4474169208Srrs /* 4475169208Srrs * use just the size of the chunk buffer so 4476170642Srrs * the front part of our chunks fit in 4477170642Srrs * contiguous space up to the chunk buffer 4478170642Srrs * size (508 bytes). For chunks that need to 4479170786Srrs * get more than that they must use the 4480170642Srrs * sctp_m_getptr() function or other means 4481170786Srrs * (e.g. know how to parse mbuf chains). 4482170786Srrs * Cookies do this already. 4483169208Srrs */ 4484169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4485169208Srrs (sizeof(chunk_buf) - 4), 4486169208Srrs chunk_buf); 4487169208Srrs if (ch == NULL) { 4488169208Srrs *offset = length; 4489169378Srrs if (locked_tcb) { 4490169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4491169378Srrs } 4492169208Srrs return (NULL); 4493169208Srrs } 4494169208Srrs } else { 4495169208Srrs /* We can fit it all */ 4496170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4497170642Srrs chk_length, chunk_buf); 4498170642Srrs if (ch == NULL) { 4499170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4500170642Srrs *offset = length; 4501170642Srrs if (locked_tcb) { 4502170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4503169208Srrs } 4504170642Srrs return (NULL); 4505163953Srrs } 4506163953Srrs } 4507163953Srrs } 4508163953Srrs num_chunks++; 4509163953Srrs /* Save off the last place we got a control from */ 4510163953Srrs if (stcb != NULL) { 4511169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4512163953Srrs /* 4513163953Srrs * allow last_control to be NULL if 4514163953Srrs * ASCONF... ASCONF processing will find the 4515163953Srrs * right net later 4516163953Srrs */ 4517169420Srrs if ((netp != NULL) && (*netp != NULL)) 4518169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4519163953Srrs } 4520163953Srrs } 4521163953Srrs#ifdef SCTP_AUDITING_ENABLED 4522163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4523163953Srrs#endif 4524163953Srrs 4525163953Srrs /* check to see if this chunk required auth, but isn't */ 4526179783Srrs if ((stcb != NULL) && 4527179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4528179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4529163953Srrs !stcb->asoc.authenticated) { 4530163953Srrs /* "silently" ignore */ 4531163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4532163953Srrs goto next_chunk; 4533163953Srrs } 4534163953Srrs switch (ch->chunk_type) { 4535163953Srrs case SCTP_INITIATION: 4536163953Srrs /* must be first and only chunk */ 4537169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4538163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4539163953Srrs /* We are not interested anymore? */ 4540163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4541163953Srrs /* 4542163953Srrs * collision case where we are 4543163953Srrs * sending to them too 4544163953Srrs */ 4545163953Srrs ; 4546163953Srrs } else { 4547169378Srrs if (locked_tcb) { 4548163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4549169378Srrs } 4550163953Srrs *offset = length; 4551163953Srrs return (NULL); 4552163953Srrs } 4553163953Srrs } 4554171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4555171990Srrs (num_chunks > 1) || 4556179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4557163953Srrs *offset = length; 4558169378Srrs if (locked_tcb) { 4559163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4560169378Srrs } 4561163953Srrs return (NULL); 4562163953Srrs } 4563163953Srrs if ((stcb != NULL) && 4564163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4565163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4566163953Srrs sctp_send_shutdown_ack(stcb, 4567163953Srrs stcb->asoc.primary_destination); 4568163953Srrs *offset = length; 4569172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4570169378Srrs if (locked_tcb) { 4571163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4572169378Srrs } 4573163953Srrs return (NULL); 4574163953Srrs } 4575169420Srrs if (netp) { 4576169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4577169420Srrs (struct sctp_init_chunk *)ch, inp, 4578179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4579169420Srrs } 4580168299Srrs if (abort_no_unlock) 4581168299Srrs return (NULL); 4582168299Srrs 4583163953Srrs *offset = length; 4584169378Srrs if (locked_tcb) { 4585163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4586169378Srrs } 4587163953Srrs return (NULL); 4588163953Srrs break; 4589170642Srrs case SCTP_PAD_CHUNK: 4590170642Srrs break; 4591163953Srrs case SCTP_INITIATION_ACK: 4592163953Srrs /* must be first and only chunk */ 4593169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4594163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4595163953Srrs /* We are not interested anymore */ 4596163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4597163953Srrs ; 4598163953Srrs } else { 4599208876Srrs if (locked_tcb != stcb) { 4600208876Srrs /* Very unlikely */ 4601163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4602169378Srrs } 4603163953Srrs *offset = length; 4604163953Srrs if (stcb) { 4605172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4606172090Srrs so = SCTP_INP_SO(inp); 4607172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4608172090Srrs SCTP_TCB_UNLOCK(stcb); 4609172090Srrs SCTP_SOCKET_LOCK(so, 1); 4610172090Srrs SCTP_TCB_LOCK(stcb); 4611172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4612172090Srrs#endif 4613171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4614172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4615172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4616172090Srrs#endif 4617163953Srrs } 4618163953Srrs return (NULL); 4619163953Srrs } 4620163953Srrs } 4621163953Srrs if ((num_chunks > 1) || 4622179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4623163953Srrs *offset = length; 4624169378Srrs if (locked_tcb) { 4625163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4626169378Srrs } 4627163953Srrs return (NULL); 4628163953Srrs } 4629169420Srrs if ((netp) && (*netp)) { 4630169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4631170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4632169420Srrs } else { 4633169420Srrs ret = -1; 4634169420Srrs } 4635163953Srrs /* 4636163953Srrs * Special case, I must call the output routine to 4637163953Srrs * get the cookie echoed 4638163953Srrs */ 4639168299Srrs if (abort_no_unlock) 4640168299Srrs return (NULL); 4641168299Srrs 4642163953Srrs if ((stcb) && ret == 0) 4643172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4644163953Srrs *offset = length; 4645169378Srrs if (locked_tcb) { 4646163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4647169378Srrs } 4648163953Srrs return (NULL); 4649163953Srrs break; 4650163953Srrs case SCTP_SELECTIVE_ACK: 4651163953Srrs { 4652163953Srrs struct sctp_sack_chunk *sack; 4653163953Srrs int abort_now = 0; 4654163953Srrs uint32_t a_rwnd, cum_ack; 4655202526Stuexen uint16_t num_seg, num_dup; 4656202526Stuexen uint8_t flags; 4657202526Stuexen int offset_seg, offset_dup; 4658163953Srrs 4659211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4660211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4661202526Stuexen if (stcb == NULL) { 4662202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4663202526Stuexen break; 4664169208Srrs } 4665202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4666202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4667202526Stuexen break; 4668202526Stuexen } 4669171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4670171990Srrs /*- 4671171990Srrs * If we have sent a shutdown-ack, we will pay no 4672171990Srrs * attention to a sack sent in to us since 4673171990Srrs * we don't care anymore. 4674171990Srrs */ 4675184883Srrs break; 4676171990Srrs } 4677163953Srrs sack = (struct sctp_sack_chunk *)ch; 4678202526Stuexen flags = ch->chunk_flags; 4679163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4680163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4681202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4682163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4683202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4684202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4685202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4686202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4687202526Stuexen break; 4688202526Stuexen } 4689202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4690202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4691170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4692202526Stuexen cum_ack, num_seg, a_rwnd); 4693163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4694163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4695163953Srrs (num_seg == 0) && 4696216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4697163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4698216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4699163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4700163953Srrs ) { 4701163953Srrs /* 4702163953Srrs * We have a SIMPLE sack having no 4703163953Srrs * prior segments and data on sent 4704163953Srrs * queue to be acked.. Use the 4705163953Srrs * faster path sack processing. We 4706163953Srrs * also allow window update sacks 4707163953Srrs * with no missing segments to go 4708163953Srrs * this way too. 4709163953Srrs */ 4710218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4711163953Srrs } else { 4712169420Srrs if (netp && *netp) 4713202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4714202526Stuexen stcb, *netp, 4715202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4716218186Srrs cum_ack, a_rwnd, ecne_seen); 4717163953Srrs } 4718202526Stuexen if (abort_now) { 4719202526Stuexen /* ABORT signal from sack processing */ 4720202526Stuexen *offset = length; 4721202526Stuexen return (NULL); 4722202526Stuexen } 4723185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4724185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4725185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4726185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4727185694Srrs } 4728163953Srrs } 4729163953Srrs break; 4730185694Srrs /* 4731185694Srrs * EY - nr_sack: If the received chunk is an 4732185694Srrs * nr_sack chunk 4733185694Srrs */ 4734185694Srrs case SCTP_NR_SELECTIVE_ACK: 4735185694Srrs { 4736185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4737185694Srrs int abort_now = 0; 4738185694Srrs uint32_t a_rwnd, cum_ack; 4739202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4740202526Stuexen uint8_t flags; 4741202526Stuexen int offset_seg, offset_dup; 4742185694Srrs 4743211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4744211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4745212225Srrs if (stcb == NULL) { 4746212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4747212225Srrs break; 4748212225Srrs } 4749211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4750211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4751185694Srrs goto unknown_chunk; 4752185694Srrs } 4753202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4754202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4755202526Stuexen break; 4756202526Stuexen } 4757185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4758185694Srrs /*- 4759185694Srrs * If we have sent a shutdown-ack, we will pay no 4760185694Srrs * attention to a sack sent in to us since 4761185694Srrs * we don't care anymore. 4762185694Srrs */ 4763202526Stuexen break; 4764185694Srrs } 4765185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4766202526Stuexen flags = ch->chunk_flags; 4767185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4768185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4769185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4770202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4771185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4772202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4773202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4774202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4775202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4776202526Stuexen break; 4777202526Stuexen } 4778202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4779202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4780185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4781202526Stuexen cum_ack, num_seg, a_rwnd); 4782185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4783185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4784202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4785216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4786185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4787216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4788202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4789185694Srrs /* 4790185694Srrs * We have a SIMPLE sack having no 4791185694Srrs * prior segments and data on sent 4792202526Stuexen * queue to be acked. Use the faster 4793202526Stuexen * path sack processing. We also 4794202526Stuexen * allow window update sacks with no 4795202526Stuexen * missing segments to go this way 4796202526Stuexen * too. 4797185694Srrs */ 4798218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4799218186Srrs &abort_now, ecne_seen); 4800185694Srrs } else { 4801185694Srrs if (netp && *netp) 4802202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4803202526Stuexen stcb, *netp, 4804202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4805218186Srrs cum_ack, a_rwnd, ecne_seen); 4806185694Srrs } 4807202526Stuexen if (abort_now) { 4808202526Stuexen /* ABORT signal from sack processing */ 4809202526Stuexen *offset = length; 4810202526Stuexen return (NULL); 4811202526Stuexen } 4812185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4813185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4814185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4815185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4816185694Srrs } 4817185694Srrs } 4818185694Srrs break; 4819185694Srrs 4820163953Srrs case SCTP_HEARTBEAT_REQUEST: 4821169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4822169420Srrs if ((stcb) && netp && *netp) { 4823169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4824169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4825169420Srrs chk_length, *netp); 4826169420Srrs 4827169420Srrs /* He's alive so give him credit */ 4828179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4829171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4830171943Srrs stcb->asoc.overall_error_count, 4831171943Srrs 0, 4832171943Srrs SCTP_FROM_SCTP_INPUT, 4833171943Srrs __LINE__); 4834171943Srrs } 4835169420Srrs stcb->asoc.overall_error_count = 0; 4836163953Srrs } 4837163953Srrs break; 4838163953Srrs case SCTP_HEARTBEAT_ACK: 4839169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4840169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4841169208Srrs /* Its not ours */ 4842169352Srrs *offset = length; 4843169378Srrs if (locked_tcb) { 4844169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4845169378Srrs } 4846169208Srrs return (NULL); 4847169208Srrs } 4848163953Srrs /* He's alive so give him credit */ 4849179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4850171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4851171943Srrs stcb->asoc.overall_error_count, 4852171943Srrs 0, 4853171943Srrs SCTP_FROM_SCTP_INPUT, 4854171943Srrs __LINE__); 4855171943Srrs } 4856163953Srrs stcb->asoc.overall_error_count = 0; 4857163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4858169420Srrs if (netp && *netp) 4859169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4860169420Srrs stcb, *netp); 4861163953Srrs break; 4862163953Srrs case SCTP_ABORT_ASSOCIATION: 4863170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4864170091Srrs stcb); 4865169420Srrs if ((stcb) && netp && *netp) 4866169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4867169420Srrs stcb, *netp); 4868163953Srrs *offset = length; 4869163953Srrs return (NULL); 4870163953Srrs break; 4871163953Srrs case SCTP_SHUTDOWN: 4872170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4873170091Srrs stcb); 4874169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4875169208Srrs *offset = length; 4876169378Srrs if (locked_tcb) { 4877169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4878169378Srrs } 4879169208Srrs return (NULL); 4880169420Srrs } 4881169420Srrs if (netp && *netp) { 4882163953Srrs int abort_flag = 0; 4883163953Srrs 4884163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4885163953Srrs stcb, *netp, &abort_flag); 4886163953Srrs if (abort_flag) { 4887163953Srrs *offset = length; 4888163953Srrs return (NULL); 4889163953Srrs } 4890163953Srrs } 4891163953Srrs break; 4892163953Srrs case SCTP_SHUTDOWN_ACK: 4893170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4894169420Srrs if ((stcb) && (netp) && (*netp)) 4895169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4896163953Srrs *offset = length; 4897163953Srrs return (NULL); 4898163953Srrs break; 4899169420Srrs 4900163953Srrs case SCTP_OPERATION_ERROR: 4901169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4902169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4903169420Srrs 4904163953Srrs *offset = length; 4905163953Srrs return (NULL); 4906163953Srrs } 4907163953Srrs break; 4908163953Srrs case SCTP_COOKIE_ECHO: 4909169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4910170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4911163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4912163953Srrs ; 4913163953Srrs } else { 4914169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4915163953Srrs /* We are not interested anymore */ 4916208878Srrs abend: 4917208853Srrs if (stcb) { 4918208853Srrs SCTP_TCB_UNLOCK(stcb); 4919208853Srrs } 4920163953Srrs *offset = length; 4921163953Srrs return (NULL); 4922163953Srrs } 4923163953Srrs } 4924163953Srrs /* 4925163953Srrs * First are we accepting? We do this again here 4926207924Srrs * since it is possible that a previous endpoint WAS 4927207924Srrs * listening responded to a INIT-ACK and then 4928163953Srrs * closed. We opened and bound.. and are now no 4929163953Srrs * longer listening. 4930163953Srrs */ 4931172703Srrs 4932172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4933172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4934179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4935163953Srrs struct mbuf *oper; 4936163953Srrs struct sctp_paramhdr *phdr; 4937163953Srrs 4938172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4939172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4940172703Srrs if (oper) { 4941172703Srrs SCTP_BUF_LEN(oper) = 4942172703Srrs sizeof(struct sctp_paramhdr); 4943172703Srrs phdr = mtod(oper, 4944172703Srrs struct sctp_paramhdr *); 4945172703Srrs phdr->param_type = 4946172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4947172703Srrs phdr->param_length = 4948172703Srrs htons(sizeof(struct sctp_paramhdr)); 4949163953Srrs } 4950172703Srrs sctp_abort_association(inp, stcb, m, 4951179157Srrs iphlen, sh, oper, vrf_id, port); 4952163953Srrs } 4953172703Srrs *offset = length; 4954172703Srrs return (NULL); 4955172703Srrs } else { 4956163953Srrs struct mbuf *ret_buf; 4957165220Srrs struct sctp_inpcb *linp; 4958163953Srrs 4959169420Srrs if (stcb) { 4960165220Srrs linp = NULL; 4961169420Srrs } else { 4962165220Srrs linp = inp; 4963169420Srrs } 4964165220Srrs 4965169420Srrs if (linp) { 4966165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4967208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 4968208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 4969208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4970208878Srrs goto abend; 4971208878Srrs } 4972169420Srrs } 4973169420Srrs if (netp) { 4974169420Srrs ret_buf = 4975169420Srrs sctp_handle_cookie_echo(m, iphlen, 4976169420Srrs *offset, sh, 4977169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4978169420Srrs &inp, &stcb, netp, 4979169420Srrs auth_skipped, 4980169420Srrs auth_offset, 4981169420Srrs auth_len, 4982169420Srrs &locked_tcb, 4983179157Srrs vrf_id, 4984179157Srrs port); 4985169420Srrs } else { 4986169420Srrs ret_buf = NULL; 4987169420Srrs } 4988169420Srrs if (linp) { 4989165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4990169420Srrs } 4991163953Srrs if (ret_buf == NULL) { 4992163953Srrs if (locked_tcb) { 4993163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4994163953Srrs } 4995169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4996169420Srrs "GAK, null buffer\n"); 4997163953Srrs auth_skipped = 0; 4998163953Srrs *offset = length; 4999163953Srrs return (NULL); 5000163953Srrs } 5001163953Srrs /* if AUTH skipped, see if it verified... */ 5002163953Srrs if (auth_skipped) { 5003163953Srrs got_auth = 1; 5004163953Srrs auth_skipped = 0; 5005163953Srrs } 5006163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5007163953Srrs /* 5008163953Srrs * Restart the timer if we have 5009163953Srrs * pending data 5010163953Srrs */ 5011163953Srrs struct sctp_tmit_chunk *chk; 5012163953Srrs 5013163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5014216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5015163953Srrs } 5016163953Srrs } 5017163953Srrs break; 5018163953Srrs case SCTP_COOKIE_ACK: 5019170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5020169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5021169378Srrs if (locked_tcb) { 5022169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5023169378Srrs } 5024169352Srrs return (NULL); 5025169352Srrs } 5026163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5027163953Srrs /* We are not interested anymore */ 5028163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5029163953Srrs ; 5030169420Srrs } else if (stcb) { 5031172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5032172090Srrs so = SCTP_INP_SO(inp); 5033172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5034172090Srrs SCTP_TCB_UNLOCK(stcb); 5035172090Srrs SCTP_SOCKET_LOCK(so, 1); 5036172090Srrs SCTP_TCB_LOCK(stcb); 5037172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5038172090Srrs#endif 5039171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5040172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5041172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5042172090Srrs#endif 5043163953Srrs *offset = length; 5044163953Srrs return (NULL); 5045163953Srrs } 5046163953Srrs } 5047163953Srrs /* He's alive so give him credit */ 5048169420Srrs if ((stcb) && netp && *netp) { 5049179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5050171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5051171943Srrs stcb->asoc.overall_error_count, 5052171943Srrs 0, 5053171943Srrs SCTP_FROM_SCTP_INPUT, 5054171943Srrs __LINE__); 5055171943Srrs } 5056169378Srrs stcb->asoc.overall_error_count = 0; 5057169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5058169378Srrs } 5059163953Srrs break; 5060163953Srrs case SCTP_ECN_ECHO: 5061169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5062163953Srrs /* He's alive so give him credit */ 5063169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5064169208Srrs /* Its not ours */ 5065169378Srrs if (locked_tcb) { 5066169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5067169378Srrs } 5068169208Srrs *offset = length; 5069169208Srrs return (NULL); 5070169208Srrs } 5071169378Srrs if (stcb) { 5072179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5073171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5074171943Srrs stcb->asoc.overall_error_count, 5075171943Srrs 0, 5076171943Srrs SCTP_FROM_SCTP_INPUT, 5077171943Srrs __LINE__); 5078171943Srrs } 5079169378Srrs stcb->asoc.overall_error_count = 0; 5080169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5081169378Srrs stcb); 5082218186Srrs ecne_seen = 1; 5083169378Srrs } 5084163953Srrs break; 5085163953Srrs case SCTP_ECN_CWR: 5086169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5087163953Srrs /* He's alive so give him credit */ 5088169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5089169208Srrs /* Its not ours */ 5090169378Srrs if (locked_tcb) { 5091169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5092169378Srrs } 5093169208Srrs *offset = length; 5094169208Srrs return (NULL); 5095169208Srrs } 5096169378Srrs if (stcb) { 5097179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5098171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5099171943Srrs stcb->asoc.overall_error_count, 5100171943Srrs 0, 5101171943Srrs SCTP_FROM_SCTP_INPUT, 5102171943Srrs __LINE__); 5103171943Srrs } 5104169378Srrs stcb->asoc.overall_error_count = 0; 5105218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5106169378Srrs } 5107163953Srrs break; 5108163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5109170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5110163953Srrs /* must be first and only chunk */ 5111163953Srrs if ((num_chunks > 1) || 5112170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5113163953Srrs *offset = length; 5114169378Srrs if (locked_tcb) { 5115163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5116169378Srrs } 5117163953Srrs return (NULL); 5118163953Srrs } 5119169420Srrs if ((stcb) && netp && *netp) { 5120169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5121169378Srrs stcb, *netp); 5122169378Srrs } 5123163953Srrs *offset = length; 5124163953Srrs return (NULL); 5125163953Srrs break; 5126163953Srrs case SCTP_ASCONF: 5127169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5128163953Srrs /* He's alive so give him credit */ 5129169378Srrs if (stcb) { 5130179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5131171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5132171943Srrs stcb->asoc.overall_error_count, 5133171943Srrs 0, 5134171943Srrs SCTP_FROM_SCTP_INPUT, 5135171943Srrs __LINE__); 5136171943Srrs } 5137169378Srrs stcb->asoc.overall_error_count = 0; 5138169378Srrs sctp_handle_asconf(m, *offset, 5139171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5140171990Srrs asconf_cnt++; 5141169378Srrs } 5142163953Srrs break; 5143163953Srrs case SCTP_ASCONF_ACK: 5144169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5145169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5146169208Srrs /* Its not ours */ 5147169378Srrs if (locked_tcb) { 5148169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5149169378Srrs } 5150169208Srrs *offset = length; 5151169208Srrs return (NULL); 5152169208Srrs } 5153169420Srrs if ((stcb) && netp && *netp) { 5154169378Srrs /* He's alive so give him credit */ 5155179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5156171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5157171943Srrs stcb->asoc.overall_error_count, 5158171943Srrs 0, 5159171943Srrs SCTP_FROM_SCTP_INPUT, 5160171943Srrs __LINE__); 5161171943Srrs } 5162169378Srrs stcb->asoc.overall_error_count = 0; 5163169378Srrs sctp_handle_asconf_ack(m, *offset, 5164172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5165172190Srrs if (abort_no_unlock) 5166172190Srrs return (NULL); 5167169378Srrs } 5168163953Srrs break; 5169163953Srrs case SCTP_FORWARD_CUM_TSN: 5170169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5171169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5172169208Srrs /* Its not ours */ 5173169378Srrs if (locked_tcb) { 5174169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5175169378Srrs } 5176169208Srrs *offset = length; 5177169208Srrs return (NULL); 5178169208Srrs } 5179163953Srrs /* He's alive so give him credit */ 5180169378Srrs if (stcb) { 5181163953Srrs int abort_flag = 0; 5182163953Srrs 5183163953Srrs stcb->asoc.overall_error_count = 0; 5184179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5185171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5186171943Srrs stcb->asoc.overall_error_count, 5187171943Srrs 0, 5188171943Srrs SCTP_FROM_SCTP_INPUT, 5189171943Srrs __LINE__); 5190171943Srrs } 5191163953Srrs *fwd_tsn_seen = 1; 5192163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5193163953Srrs /* We are not interested anymore */ 5194172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5195172090Srrs so = SCTP_INP_SO(inp); 5196172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5197172090Srrs SCTP_TCB_UNLOCK(stcb); 5198172090Srrs SCTP_SOCKET_LOCK(so, 1); 5199172090Srrs SCTP_TCB_LOCK(stcb); 5200172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5201172090Srrs#endif 5202171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5203172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5204172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5205172090Srrs#endif 5206163953Srrs *offset = length; 5207163953Srrs return (NULL); 5208163953Srrs } 5209163953Srrs sctp_handle_forward_tsn(stcb, 5210170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5211163953Srrs if (abort_flag) { 5212163953Srrs *offset = length; 5213163953Srrs return (NULL); 5214163953Srrs } else { 5215179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5216171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5217171943Srrs stcb->asoc.overall_error_count, 5218171943Srrs 0, 5219171943Srrs SCTP_FROM_SCTP_INPUT, 5220171943Srrs __LINE__); 5221171943Srrs } 5222163953Srrs stcb->asoc.overall_error_count = 0; 5223163953Srrs } 5224163953Srrs 5225163953Srrs } 5226163953Srrs break; 5227163953Srrs case SCTP_STREAM_RESET: 5228169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5229169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5230169208Srrs /* Its not ours */ 5231169378Srrs if (locked_tcb) { 5232169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5233169378Srrs } 5234169208Srrs *offset = length; 5235169208Srrs return (NULL); 5236169208Srrs } 5237163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5238163953Srrs /* We are not interested anymore */ 5239172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5240172090Srrs so = SCTP_INP_SO(inp); 5241172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5242172090Srrs SCTP_TCB_UNLOCK(stcb); 5243172090Srrs SCTP_SOCKET_LOCK(so, 1); 5244172090Srrs SCTP_TCB_LOCK(stcb); 5245172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5246172090Srrs#endif 5247171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5248172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5249172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5250172090Srrs#endif 5251163953Srrs *offset = length; 5252163953Srrs return (NULL); 5253163953Srrs } 5254169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5255169420Srrs /* 5256169420Srrs * hmm, peer should have announced this, but 5257169420Srrs * we will turn it on since he is sending us 5258169420Srrs * a stream reset. 5259169420Srrs */ 5260169420Srrs stcb->asoc.peer_supports_strreset = 1; 5261163953Srrs } 5262170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5263169420Srrs /* stop processing */ 5264169420Srrs *offset = length; 5265169420Srrs return (NULL); 5266169420Srrs } 5267163953Srrs break; 5268163953Srrs case SCTP_PACKET_DROPPED: 5269169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5270163953Srrs /* re-get it all please */ 5271169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5272169208Srrs /* Its not ours */ 5273169378Srrs if (locked_tcb) { 5274169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5275169378Srrs } 5276169208Srrs *offset = length; 5277169208Srrs return (NULL); 5278169208Srrs } 5279169420Srrs if (ch && (stcb) && netp && (*netp)) { 5280169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5281170781Srrs stcb, *netp, 5282170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5283170781Srrs 5284169378Srrs } 5285163953Srrs break; 5286163953Srrs 5287163953Srrs case SCTP_AUTHENTICATION: 5288169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5289179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5290163953Srrs goto unknown_chunk; 5291163953Srrs 5292163953Srrs if (stcb == NULL) { 5293163953Srrs /* save the first AUTH for later processing */ 5294163953Srrs if (auth_skipped == 0) { 5295163953Srrs auth_offset = *offset; 5296163953Srrs auth_len = chk_length; 5297163953Srrs auth_skipped = 1; 5298163953Srrs } 5299163953Srrs /* skip this chunk (temporarily) */ 5300163953Srrs goto next_chunk; 5301163953Srrs } 5302169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5303169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5304169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5305169208Srrs /* Its not ours */ 5306169378Srrs if (locked_tcb) { 5307169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5308169378Srrs } 5309169208Srrs *offset = length; 5310169208Srrs return (NULL); 5311169208Srrs } 5312163953Srrs if (got_auth == 1) { 5313163953Srrs /* skip this chunk... it's already auth'd */ 5314163953Srrs goto next_chunk; 5315163953Srrs } 5316163953Srrs got_auth = 1; 5317169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5318163953Srrs m, *offset)) { 5319163953Srrs /* auth HMAC failed so dump the packet */ 5320163953Srrs *offset = length; 5321163953Srrs return (stcb); 5322163953Srrs } else { 5323163953Srrs /* remaining chunks are HMAC checked */ 5324163953Srrs stcb->asoc.authenticated = 1; 5325163953Srrs } 5326163953Srrs break; 5327163953Srrs 5328163953Srrs default: 5329163953Srrs unknown_chunk: 5330163953Srrs /* it's an unknown chunk! */ 5331163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5332163953Srrs struct mbuf *mm; 5333163953Srrs struct sctp_paramhdr *phd; 5334163953Srrs 5335163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5336165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5337163953Srrs if (mm) { 5338163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5339163953Srrs /* 5340163953Srrs * We cheat and use param type since 5341163953Srrs * we did not bother to define a 5342163953Srrs * error cause struct. They are the 5343163953Srrs * same basic format with different 5344163953Srrs * names. 5345163953Srrs */ 5346163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5347163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5348165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5349166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5350163953Srrs M_DONTWAIT); 5351165647Srrs if (SCTP_BUF_NEXT(mm)) { 5352178202Srrs#ifdef SCTP_MBUF_LOGGING 5353179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5354178202Srrs struct mbuf *mat; 5355178202Srrs 5356178202Srrs mat = SCTP_BUF_NEXT(mm); 5357178202Srrs while (mat) { 5358178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5359178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5360178202Srrs } 5361178202Srrs mat = SCTP_BUF_NEXT(mat); 5362178202Srrs } 5363178202Srrs } 5364178202Srrs#endif 5365163953Srrs sctp_queue_op_err(stcb, mm); 5366163953Srrs } else { 5367163953Srrs sctp_m_freem(mm); 5368163953Srrs } 5369163953Srrs } 5370163953Srrs } 5371163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5372163953Srrs /* discard this packet */ 5373163953Srrs *offset = length; 5374163953Srrs return (stcb); 5375163953Srrs } /* else skip this bad chunk and continue... */ 5376163953Srrs break; 5377163953Srrs } /* switch (ch->chunk_type) */ 5378163953Srrs 5379163953Srrs 5380163953Srrsnext_chunk: 5381163953Srrs /* get the next chunk */ 5382163953Srrs *offset += SCTP_SIZE32(chk_length); 5383163953Srrs if (*offset >= length) { 5384163953Srrs /* no more data left in the mbuf chain */ 5385163953Srrs break; 5386163953Srrs } 5387163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5388163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5389163953Srrs if (ch == NULL) { 5390169378Srrs if (locked_tcb) { 5391163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5392169378Srrs } 5393163953Srrs *offset = length; 5394163953Srrs return (NULL); 5395163953Srrs } 5396163953Srrs } /* while */ 5397171990Srrs 5398171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5399171990Srrs sctp_send_asconf_ack(stcb); 5400171990Srrs } 5401163953Srrs return (stcb); 5402163953Srrs} 5403163953Srrs 5404163953Srrs 5405171440Srrs#ifdef INVARIANTS 5406208853Srrs#ifdef __GNUC__ 5407208853Srrs__attribute__((noinline)) 5408208853Srrs#endif 5409208853Srrs void 5410208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5411171440Srrs{ 5412208853Srrs struct sctp_tcb *lstcb; 5413171440Srrs 5414208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5415208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5416171440Srrs panic("Own lock on stcb at return from input"); 5417171440Srrs } 5418171440Srrs } 5419208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5420208876Srrs panic("Own create lock on inp"); 5421208876Srrs } 5422208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5423208876Srrs panic("Own inp lock on inp"); 5424208876Srrs } 5425171440Srrs} 5426171440Srrs 5427171440Srrs#endif 5428171440Srrs 5429163953Srrs/* 5430163953Srrs * common input chunk processing (v4 and v6) 5431163953Srrs */ 5432169378Srrsvoid 5433163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5434163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5435163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5436179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5437163953Srrs{ 5438163953Srrs /* 5439163953Srrs * Control chunk processing 5440163953Srrs */ 5441163953Srrs uint32_t high_tsn; 5442163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5443163953Srrs struct mbuf *m = *mm; 5444163953Srrs int abort_flag = 0; 5445163953Srrs int un_sent; 5446218129Srrs int cnt_ctrl_ready = 0; 5447163953Srrs 5448163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5449163953Srrs#ifdef SCTP_AUDITING_ENABLED 5450163953Srrs sctp_audit_log(0xE0, 1); 5451163953Srrs sctp_auditing(0, inp, stcb, net); 5452163953Srrs#endif 5453163953Srrs 5454179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5455179783Srrs m, iphlen, offset, length, stcb); 5456163953Srrs if (stcb) { 5457163953Srrs /* always clear this before beginning a packet */ 5458163953Srrs stcb->asoc.authenticated = 0; 5459163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5460171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5461171990Srrs stcb, stcb->asoc.state); 5462171990Srrs 5463171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5464171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5465171745Srrs /*- 5466171745Srrs * If we hit here, we had a ref count 5467171745Srrs * up when the assoc was aborted and the 5468171745Srrs * timer is clearing out the assoc, we should 5469171745Srrs * NOT respond to any packet.. its OOTB. 5470171745Srrs */ 5471171745Srrs SCTP_TCB_UNLOCK(stcb); 5472171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5473179157Srrs vrf_id, port); 5474171745Srrs goto out_now; 5475171745Srrs } 5476163953Srrs } 5477163953Srrs if (IS_SCTP_CONTROL(ch)) { 5478163953Srrs /* process the control portion of the SCTP packet */ 5479169655Srrs /* sa_ignore NO_NULL_CHK */ 5480163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5481179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5482163953Srrs if (stcb) { 5483163953Srrs /* 5484163953Srrs * This covers us if the cookie-echo was there and 5485163953Srrs * it changes our INP. 5486163953Srrs */ 5487163953Srrs inp = stcb->sctp_ep; 5488179783Srrs if ((net) && (port)) { 5489179783Srrs if (net->port == 0) { 5490179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5491179783Srrs } 5492179783Srrs net->port = port; 5493179783Srrs } 5494163953Srrs } 5495163953Srrs } else { 5496163953Srrs /* 5497163953Srrs * no control chunks, so pre-process DATA chunks (these 5498163953Srrs * checks are taken care of by control processing) 5499163953Srrs */ 5500163953Srrs 5501163953Srrs /* 5502163953Srrs * if DATA only packet, and auth is required, then punt... 5503163953Srrs * can't have authenticated without any AUTH (control) 5504163953Srrs * chunks 5505163953Srrs */ 5506179783Srrs if ((stcb != NULL) && 5507179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5508179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5509163953Srrs /* "silently" ignore */ 5510163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5511163953Srrs SCTP_TCB_UNLOCK(stcb); 5512171440Srrs goto out_now; 5513163953Srrs } 5514163953Srrs if (stcb == NULL) { 5515163953Srrs /* out of the blue DATA chunk */ 5516169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5517179157Srrs vrf_id, port); 5518171440Srrs goto out_now; 5519163953Srrs } 5520163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5521163953Srrs /* v_tag mismatch! */ 5522163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5523163953Srrs SCTP_TCB_UNLOCK(stcb); 5524171440Srrs goto out_now; 5525163953Srrs } 5526163953Srrs } 5527163953Srrs 5528163953Srrs if (stcb == NULL) { 5529163953Srrs /* 5530163953Srrs * no valid TCB for this packet, or we found it's a bad 5531163953Srrs * packet while processing control, or we're done with this 5532163953Srrs * packet (done or skip rest of data), so we drop it... 5533163953Srrs */ 5534171440Srrs goto out_now; 5535163953Srrs } 5536163953Srrs /* 5537163953Srrs * DATA chunk processing 5538163953Srrs */ 5539163953Srrs /* plow through the data chunks while length > offset */ 5540163953Srrs 5541163953Srrs /* 5542163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5543163953Srrs * DATA is required. 5544163953Srrs */ 5545179783Srrs if ((length > offset) && 5546179783Srrs (stcb != NULL) && 5547179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5548179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5549163953Srrs !stcb->asoc.authenticated) { 5550163953Srrs /* "silently" ignore */ 5551163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5552169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5553169420Srrs "Data chunk requires AUTH, skipped\n"); 5554165220Srrs goto trigger_send; 5555163953Srrs } 5556163953Srrs if (length > offset) { 5557163953Srrs int retval; 5558163953Srrs 5559163953Srrs /* 5560163953Srrs * First check to make sure our state is correct. We would 5561163953Srrs * not get here unless we really did have a tag, so we don't 5562163953Srrs * abort if this happens, just dump the chunk silently. 5563163953Srrs */ 5564163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5565163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5566163953Srrs /* 5567163953Srrs * we consider data with valid tags in this state 5568163953Srrs * shows us the cookie-ack was lost. Imply it was 5569163953Srrs * there. 5570163953Srrs */ 5571179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5572171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5573171943Srrs stcb->asoc.overall_error_count, 5574171943Srrs 0, 5575171943Srrs SCTP_FROM_SCTP_INPUT, 5576171943Srrs __LINE__); 5577171943Srrs } 5578163953Srrs stcb->asoc.overall_error_count = 0; 5579163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5580163953Srrs break; 5581163953Srrs case SCTP_STATE_COOKIE_WAIT: 5582163953Srrs /* 5583163953Srrs * We consider OOTB any data sent during asoc setup. 5584163953Srrs */ 5585169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5586179157Srrs vrf_id, port); 5587163953Srrs SCTP_TCB_UNLOCK(stcb); 5588171440Srrs goto out_now; 5589171531Srrs /* sa_ignore NOTREACHED */ 5590163953Srrs break; 5591163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5592163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5593163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5594163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5595163953Srrs default: 5596163953Srrs SCTP_TCB_UNLOCK(stcb); 5597171440Srrs goto out_now; 5598171531Srrs /* sa_ignore NOTREACHED */ 5599163953Srrs break; 5600163953Srrs case SCTP_STATE_OPEN: 5601163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5602163953Srrs break; 5603163953Srrs } 5604163953Srrs /* plow through the data chunks while length > offset */ 5605163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5606163953Srrs inp, stcb, net, &high_tsn); 5607163953Srrs if (retval == 2) { 5608163953Srrs /* 5609163953Srrs * The association aborted, NO UNLOCK needed since 5610163953Srrs * the association is destroyed. 5611163953Srrs */ 5612171440Srrs goto out_now; 5613163953Srrs } 5614163953Srrs data_processed = 1; 5615163953Srrs /* 5616163953Srrs * Anything important needs to have been m_copy'ed in 5617163953Srrs * process_data 5618163953Srrs */ 5619163953Srrs } 5620218129Srrs /* take care of ecn */ 5621218235Stuexen if ((stcb->asoc.ecn_allowed == 1) && 5622218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5623218129Srrs /* Yep, we need to add a ECNE */ 5624218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5625218129Srrs } 5626163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5627216495Stuexen int was_a_gap; 5628216495Stuexen uint32_t highest_tsn; 5629163953Srrs 5630216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5631216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5632216495Stuexen } else { 5633216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5634163953Srrs } 5635216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5636190689Srrs stcb->asoc.send_sack = 1; 5637206137Stuexen sctp_sack_check(stcb, was_a_gap, &abort_flag); 5638163953Srrs if (abort_flag) { 5639163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5640171440Srrs goto out_now; 5641163953Srrs } 5642190689Srrs } else if (fwd_tsn_seen) { 5643190689Srrs stcb->asoc.send_sack = 1; 5644163953Srrs } 5645163953Srrs /* trigger send of any chunks in queue... */ 5646165220Srrstrigger_send: 5647163953Srrs#ifdef SCTP_AUDITING_ENABLED 5648163953Srrs sctp_audit_log(0xE0, 2); 5649163953Srrs sctp_auditing(1, inp, stcb, net); 5650163953Srrs#endif 5651169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5652169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5653169420Srrs stcb->asoc.peers_rwnd, 5654169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5655169420Srrs stcb->asoc.total_flight); 5656163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5657218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5658218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5659218129Srrs } 5660218129Srrs if (cnt_ctrl_ready || 5661163953Srrs ((un_sent) && 5662163953Srrs (stcb->asoc.peers_rwnd > 0 || 5663163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5664169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5665172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5666169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5667163953Srrs } 5668163953Srrs#ifdef SCTP_AUDITING_ENABLED 5669163953Srrs sctp_audit_log(0xE0, 3); 5670163953Srrs sctp_auditing(2, inp, stcb, net); 5671163953Srrs#endif 5672163953Srrs SCTP_TCB_UNLOCK(stcb); 5673171440Srrsout_now: 5674171440Srrs#ifdef INVARIANTS 5675171440Srrs sctp_validate_no_locks(inp); 5676171440Srrs#endif 5677169378Srrs return; 5678163953Srrs} 5679163953Srrs 5680185694Srrs#if 0 5681185694Srrsstatic void 5682185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5683185694Srrs{ 5684185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5685185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5686185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5687185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5688185694Srrs } 5689185694Srrs} 5690163953Srrs 5691185694Srrs#endif 5692185694Srrs 5693163953Srrsvoid 5694189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5695163953Srrs{ 5696163953Srrs#ifdef SCTP_MBUF_LOGGING 5697163953Srrs struct mbuf *mat; 5698163953Srrs 5699163953Srrs#endif 5700165647Srrs struct mbuf *m; 5701163953Srrs int iphlen; 5702170181Srrs uint32_t vrf_id = 0; 5703163953Srrs uint8_t ecn_bits; 5704163953Srrs struct ip *ip; 5705163953Srrs struct sctphdr *sh; 5706163953Srrs struct sctp_inpcb *inp = NULL; 5707163953Srrs struct sctp_nets *net; 5708163953Srrs struct sctp_tcb *stcb = NULL; 5709163953Srrs struct sctp_chunkhdr *ch; 5710163953Srrs int refcount_up = 0; 5711163953Srrs int length, mlen, offset; 5712163953Srrs 5713211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5714211969Stuexen uint32_t check, calc_check; 5715211969Stuexen 5716211969Stuexen#endif 5717211969Stuexen 5718169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5719169352Srrs SCTP_RELEASE_PKT(i_pak); 5720169352Srrs return; 5721169352Srrs } 5722168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5723163953Srrs iphlen = off; 5724165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5725170056Srrs 5726163953Srrs net = NULL; 5727163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5728163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5729163953Srrs 5730170091Srrs 5731163953Srrs#ifdef SCTP_MBUF_LOGGING 5732163953Srrs /* Log in any input mbufs */ 5733179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5734170744Srrs mat = m; 5735170744Srrs while (mat) { 5736170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5737170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5738170744Srrs } 5739170744Srrs mat = SCTP_BUF_NEXT(mat); 5740163953Srrs } 5741163953Srrs } 5742163953Srrs#endif 5743170091Srrs#ifdef SCTP_PACKET_LOGGING 5744179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5745170921Srrs sctp_packet_log(m, mlen); 5746170091Srrs#endif 5747170091Srrs /* 5748170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5749170091Srrs * case) 5750170091Srrs */ 5751170091Srrs mlen -= iphlen; 5752168124Srrs 5753163953Srrs /* 5754163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5755163953Srrs */ 5756163953Srrs ip = mtod(m, struct ip *); 5757163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5758165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5759163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5760163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5761163953Srrs return; 5762163953Srrs } 5763163953Srrs ip = mtod(m, struct ip *); 5764163953Srrs } 5765188067Srrs /* validate mbuf chain length with IP payload length */ 5766188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5767188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5768188067Srrs goto bad; 5769188067Srrs } 5770163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5771163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5772170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5773170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5774163953Srrs 5775163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5776163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5777163953Srrs goto bad; 5778163953Srrs } 5779168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5780163953Srrs /* 5781163953Srrs * We only look at broadcast if its a front state, All 5782163953Srrs * others we will not have a tcb for anyway. 5783163953Srrs */ 5784163953Srrs goto bad; 5785163953Srrs } 5786163953Srrs /* validate SCTP checksum */ 5787188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5788188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5789188067Srrs m->m_pkthdr.len, 5790188067Srrs if_name(m->m_pkthdr.rcvif), 5791188067Srrs m->m_pkthdr.csum_flags); 5792211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5793211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5794211969Stuexen#else 5795188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5796188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5797188067Srrs goto sctp_skip_csum_4; 5798188067Srrs } 5799171990Srrs check = sh->checksum; /* save incoming checksum */ 5800171990Srrs sh->checksum = 0; /* prepare for calc */ 5801188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5802203503Stuexen sh->checksum = check; 5803188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5804171990Srrs if (calc_check != check) { 5805171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5806171990Srrs calc_check, check, m, mlen, iphlen); 5807163953Srrs 5808171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5809171990Srrs offset - sizeof(*ch), 5810171990Srrs sh, ch, &inp, &net, 5811171990Srrs vrf_id); 5812179157Srrs if ((net) && (port)) { 5813179157Srrs if (net->port == 0) { 5814179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5815179157Srrs } 5816179157Srrs net->port = port; 5817179157Srrs } 5818171990Srrs if ((inp) && (stcb)) { 5819171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5820172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5821171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5822171990Srrs refcount_up = 1; 5823163953Srrs } 5824171990Srrs SCTP_STAT_INCR(sctps_badsum); 5825171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5826171990Srrs goto bad; 5827168299Srrs } 5828163953Srrssctp_skip_csum_4: 5829211969Stuexen#endif 5830168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5831168124Srrs if (sh->dest_port == 0) { 5832168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5833168124Srrs goto bad; 5834168124Srrs } 5835163953Srrs /* 5836163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5837163953Srrs * IP/SCTP/first chunk header... 5838163953Srrs */ 5839163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5840168299Srrs sh, ch, &inp, &net, vrf_id); 5841179157Srrs if ((net) && (port)) { 5842179157Srrs if (net->port == 0) { 5843179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5844179157Srrs } 5845179157Srrs net->port = port; 5846179157Srrs } 5847218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5848218335Stuexen net->flowid = m->m_pkthdr.flowid; 5849218392Srrs net->flowidset = 1; 5850218335Stuexen } 5851163953Srrs /* inp's ref-count increased && stcb locked */ 5852163953Srrs if (inp == NULL) { 5853163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5854163953Srrs 5855163953Srrs SCTP_STAT_INCR(sctps_noport); 5856163953Srrs#ifdef ICMP_BANDLIM 5857163953Srrs /* 5858163953Srrs * we use the bandwidth limiting to protect against sending 5859163953Srrs * too many ABORTS all at once. In this case these count the 5860163953Srrs * same as an ICMP message. 5861163953Srrs */ 5862163953Srrs if (badport_bandlim(0) < 0) 5863163953Srrs goto bad; 5864163953Srrs#endif /* ICMP_BANDLIM */ 5865169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5866169420Srrs "Sending a ABORT from packet entry!\n"); 5867163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5868163953Srrs /* 5869163953Srrs * we do a trick here to get the INIT tag, dig in 5870163953Srrs * and get the tag from the INIT and put it in the 5871163953Srrs * common header. 5872163953Srrs */ 5873163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5874163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5875163953Srrs (uint8_t *) & chunk_buf); 5876163953Srrs if (init_chk != NULL) 5877163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5878163953Srrs } 5879165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5880179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5881165220Srrs goto bad; 5882165220Srrs } 5883165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5884165220Srrs goto bad; 5885165220Srrs } 5886165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5887179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5888163953Srrs goto bad; 5889163953Srrs } else if (stcb == NULL) { 5890163953Srrs refcount_up = 1; 5891163953Srrs } 5892171167Sgnn#ifdef IPSEC 5893163953Srrs /* 5894163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5895163953Srrs * idea, so I will leave it in place. 5896163953Srrs */ 5897163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5898197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5899163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5900163953Srrs goto bad; 5901163953Srrs } 5902163953Srrs#endif /* IPSEC */ 5903163953Srrs 5904163953Srrs /* 5905163953Srrs * common chunk processing 5906163953Srrs */ 5907163953Srrs length = ip->ip_len + iphlen; 5908163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5909163953Srrs 5910163953Srrs ecn_bits = ip->ip_tos; 5911163953Srrs 5912169655Srrs /* sa_ignore NO_NULL_CHK */ 5913163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5914179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5915163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5916163953Srrs if (m) { 5917163953Srrs sctp_m_freem(m); 5918163953Srrs } 5919163953Srrs if ((inp) && (refcount_up)) { 5920163953Srrs /* reduce ref-count */ 5921163953Srrs SCTP_INP_DECR_REF(inp); 5922163953Srrs } 5923163953Srrs return; 5924163953Srrsbad: 5925169420Srrs if (stcb) { 5926163953Srrs SCTP_TCB_UNLOCK(stcb); 5927169420Srrs } 5928163953Srrs if ((inp) && (refcount_up)) { 5929163953Srrs /* reduce ref-count */ 5930163953Srrs SCTP_INP_DECR_REF(inp); 5931163953Srrs } 5932163953Srrs if (m) { 5933163953Srrs sctp_m_freem(m); 5934163953Srrs } 5935163953Srrs return; 5936163953Srrs} 5937218211Srrs 5938218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5939218269Srrsextern int *sctp_cpuarry; 5940218211Srrs 5941218269Srrs#endif 5942218269Srrs 5943179157Srrsvoid 5944218211Srrssctp_input(struct mbuf *m, int off) 5945179157Srrs{ 5946218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5947218211Srrs struct ip *ip; 5948218211Srrs struct sctphdr *sh; 5949218211Srrs int offset; 5950218211Srrs int cpu_to_use; 5951218371Srrs uint32_t flowid, tag; 5952218211Srrs 5953218269Srrs if (mp_ncpus > 1) { 5954218371Srrs if (m->m_flags & M_FLOWID) { 5955218371Srrs flowid = m->m_pkthdr.flowid; 5956218269Srrs } else { 5957218269Srrs /* 5958218371Srrs * No flow id built by lower layers fix it so we 5959218371Srrs * create one. 5960218269Srrs */ 5961218371Srrs ip = mtod(m, struct ip *); 5962218371Srrs offset = off + sizeof(*sh); 5963218371Srrs if (SCTP_BUF_LEN(m) < offset) { 5964218371Srrs if ((m = m_pullup(m, offset)) == 0) { 5965218371Srrs SCTP_STAT_INCR(sctps_hdrops); 5966218371Srrs return; 5967218371Srrs } 5968218371Srrs ip = mtod(m, struct ip *); 5969218371Srrs } 5970218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 5971218371Srrs tag = htonl(sh->v_tag); 5972218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 5973218371Srrs m->m_pkthdr.flowid = flowid; 5974218371Srrs m->m_flags |= M_FLOWID; 5975218269Srrs } 5976218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 5977218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 5978218211Srrs return; 5979218211Srrs } 5980218211Srrs#endif 5981218211Srrs sctp_input_with_port(m, off, 0); 5982179157Srrs} 5983