sctp_input.c revision 219397
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 219397 2011-03-08 11:58:25Z 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, 488219397Srrs SCTP_RTT_FROM_NON_DATA); 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, 633219397Srrs SCTP_RTT_FROM_NON_DATA); 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, 1550219397Srrs SCTP_RTT_FROM_NON_DATA); 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, 2254219397Srrs SCTP_RTT_FROM_NON_DATA); 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; 2620218400Stuexen#ifdef INVARIANTS 2621218392Srrs (*netp)->flowidset = 1; 2622218400Stuexen#endif 2623218335Stuexen } 2624163953Srrs /* 2625163953Srrs * Ok, we built an association so confirm the address we sent the 2626163953Srrs * INIT-ACK to. 2627163953Srrs */ 2628163953Srrs netl = sctp_findnet(*stcb, to); 2629163953Srrs /* 2630163953Srrs * This code should in theory NOT run but 2631163953Srrs */ 2632163953Srrs if (netl == NULL) { 2633163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2634163953Srrs int ret; 2635163953Srrs 2636165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2637165220Srrs SCTP_IN_COOKIE_PROC); 2638163953Srrs netl = sctp_findnet(*stcb, to); 2639163953Srrs } 2640163953Srrs if (netl) { 2641163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2642163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2643169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2644163953Srrs netl); 2645216887Stuexen send_int_conf = 1; 2646163953Srrs } 2647163953Srrs } 2648163953Srrs if (*stcb) { 2649163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2650163953Srrs *stcb, NULL); 2651163953Srrs } 2652163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2653163953Srrs if (!had_a_existing_tcb || 2654163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2655163953Srrs /* 2656163953Srrs * If we have a NEW cookie or the connect never 2657163953Srrs * reached the connected state during collision we 2658163953Srrs * must do the TCP accept thing. 2659163953Srrs */ 2660163953Srrs struct socket *so, *oso; 2661163953Srrs struct sctp_inpcb *inp; 2662163953Srrs 2663163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2664163953Srrs /* 2665163953Srrs * For a restart we will keep the same 2666163953Srrs * socket, no need to do anything. I THINK!! 2667163953Srrs */ 2668172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2669216887Stuexen if (send_int_conf) { 2670216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2671216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2672216887Stuexen } 2673163953Srrs return (m); 2674163953Srrs } 2675163953Srrs oso = (*inp_p)->sctp_socket; 2676171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2677163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2678218757Sbz CURVNET_SET(oso->so_vnet); 2679166086Srrs so = sonewconn(oso, 0 2680163953Srrs ); 2681218757Sbz CURVNET_RESTORE(); 2682163953Srrs SCTP_TCB_LOCK((*stcb)); 2683171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2684171858Srrs 2685163953Srrs if (so == NULL) { 2686163953Srrs struct mbuf *op_err; 2687163953Srrs 2688172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2689172090Srrs struct socket *pcb_so; 2690172090Srrs 2691172090Srrs#endif 2692163953Srrs /* Too many sockets */ 2693169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2694163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2695163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2696179157Srrs sh, op_err, vrf_id, port); 2697172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2698172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2699172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2700172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2701172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2702172090Srrs SCTP_TCB_LOCK((*stcb)); 2703172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2704172090Srrs#endif 2705171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2706172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2707172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2708172090Srrs#endif 2709163953Srrs return (NULL); 2710163953Srrs } 2711163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2712166086Srrs SCTP_INP_INCR_REF(inp); 2713166086Srrs /* 2714166086Srrs * We add the unbound flag here so that if we get an 2715166086Srrs * soabort() before we get the move_pcb done, we 2716166086Srrs * will properly cleanup. 2717166086Srrs */ 2718163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2719163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2720163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2721166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2722163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2723163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2724163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2725172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2726163953Srrs inp->sctp_socket = so; 2727163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2728211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2729218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2730163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2731163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2732163953Srrs inp->inp_starting_point_for_iterator = NULL; 2733163953Srrs /* 2734163953Srrs * copy in the authentication parameters from the 2735163953Srrs * original endpoint 2736163953Srrs */ 2737163953Srrs if (inp->sctp_ep.local_hmacs) 2738163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2739163953Srrs inp->sctp_ep.local_hmacs = 2740163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2741163953Srrs if (inp->sctp_ep.local_auth_chunks) 2742163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2743163953Srrs inp->sctp_ep.local_auth_chunks = 2744163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2745163953Srrs 2746163953Srrs /* 2747163953Srrs * Now we must move it from one hash table to 2748163953Srrs * another and get the tcb in the right place. 2749163953Srrs */ 2750207924Srrs 2751207924Srrs /* 2752207924Srrs * This is where the one-2-one socket is put into 2753207924Srrs * the accept state waiting for the accept! 2754207924Srrs */ 2755207924Srrs if (*stcb) { 2756207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2757207924Srrs } 2758163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2759170056Srrs 2760170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2761170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2762170056Srrs 2763175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2764175845Srwatson 0); 2765170056Srrs SCTP_TCB_LOCK((*stcb)); 2766170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2767163953Srrs 2768170056Srrs 2769166086Srrs /* 2770166086Srrs * now we must check to see if we were aborted while 2771166086Srrs * the move was going on and the lock/unlock 2772166086Srrs * happened. 2773166086Srrs */ 2774166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2775166086Srrs /* 2776166086Srrs * yep it was, we leave the assoc attached 2777166086Srrs * to the socket since the sctp_inpcb_free() 2778166086Srrs * call will send an abort for us. 2779166086Srrs */ 2780166086Srrs SCTP_INP_DECR_REF(inp); 2781166086Srrs return (NULL); 2782166086Srrs } 2783166086Srrs SCTP_INP_DECR_REF(inp); 2784163953Srrs /* Switch over to the new guy */ 2785163953Srrs *inp_p = inp; 2786172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2787216887Stuexen if (send_int_conf) { 2788216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2789216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2790216887Stuexen } 2791166086Srrs /* 2792166086Srrs * Pull it from the incomplete queue and wake the 2793166086Srrs * guy 2794166086Srrs */ 2795172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2796172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2797172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2798172090Srrs SCTP_SOCKET_LOCK(so, 1); 2799172090Srrs#endif 2800166086Srrs soisconnected(so); 2801172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2802172118Srrs SCTP_TCB_LOCK((*stcb)); 2803172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2804172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2805172090Srrs#endif 2806163953Srrs return (m); 2807163953Srrs } 2808163953Srrs } 2809216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2810216887Stuexen if (notification) { 2811216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2812216887Stuexen } 2813216887Stuexen if (send_int_conf) { 2814216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2815216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2816216887Stuexen } 2817163953Srrs } 2818163953Srrs return (m); 2819163953Srrs} 2820163953Srrs 2821163953Srrsstatic void 2822163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2823163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2824163953Srrs{ 2825163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2826163953Srrs struct sctp_association *asoc; 2827163953Srrs 2828169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2829169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2830163953Srrs if (stcb == NULL) 2831163953Srrs return; 2832163953Srrs 2833163953Srrs asoc = &stcb->asoc; 2834163953Srrs 2835163953Srrs sctp_stop_all_cookie_timers(stcb); 2836163953Srrs /* process according to association state */ 2837163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2838163953Srrs /* state change only needed when I am in right state */ 2839169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2840171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2841163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2842163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2843163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2844163953Srrs 2845163953Srrs } 2846163953Srrs /* update RTO */ 2847163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2848163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2849163953Srrs if (asoc->overall_error_count == 0) { 2850163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2851218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2852219397Srrs SCTP_RTT_FROM_NON_DATA); 2853163953Srrs } 2854169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2855172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2856163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2857163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2858172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2859172090Srrs struct socket *so; 2860172090Srrs 2861172090Srrs#endif 2862163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2863172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2864172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2865172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2866172090Srrs SCTP_TCB_UNLOCK(stcb); 2867172090Srrs SCTP_SOCKET_LOCK(so, 1); 2868172090Srrs SCTP_TCB_LOCK(stcb); 2869172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2870218641Srrs#endif 2871218641Srrs if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2872218641Srrs soisconnected(stcb->sctp_socket); 2873172090Srrs } 2874172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2875172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2876172090Srrs#endif 2877163953Srrs } 2878163953Srrs /* 2879163953Srrs * since we did not send a HB make sure we don't double 2880163953Srrs * things 2881163953Srrs */ 2882163953Srrs net->hb_responded = 1; 2883163953Srrs 2884218641Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2885218641Srrs /* 2886218641Srrs * We don't need to do the asconf thing, nor hb or 2887218641Srrs * autoclose if the socket is closed. 2888218641Srrs */ 2889218641Srrs goto closed_socket; 2890218641Srrs } 2891218641Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2892218641Srrs stcb, net); 2893218641Srrs 2894218641Srrs 2895163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2896163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2897163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2898163953Srrs stcb->sctp_ep, stcb, NULL); 2899163953Srrs } 2900163953Srrs /* 2901171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2902171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2903171572Srrs * in flight) 2904163953Srrs */ 2905163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2906163953Srrs (stcb->asoc.peer_supports_asconf) && 2907163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2908171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2909163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2910163953Srrs stcb->sctp_ep, stcb, 2911163953Srrs stcb->asoc.primary_destination); 2912171572Srrs#else 2913172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2914172190Srrs SCTP_ADDR_NOT_LOCKED); 2915171572Srrs#endif 2916163953Srrs } 2917163953Srrs } 2918218641Srrsclosed_socket: 2919163953Srrs /* Toss the cookie if I can */ 2920163953Srrs sctp_toss_old_cookies(stcb, asoc); 2921163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2922163953Srrs /* Restart the timer if we have pending data */ 2923163953Srrs struct sctp_tmit_chunk *chk; 2924163953Srrs 2925163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2926216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2927163953Srrs } 2928163953Srrs} 2929163953Srrs 2930163953Srrsstatic void 2931163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2932163953Srrs struct sctp_tcb *stcb) 2933163953Srrs{ 2934163953Srrs struct sctp_nets *net; 2935163953Srrs struct sctp_tmit_chunk *lchk; 2936218072Srrs struct sctp_ecne_chunk bkup; 2937218072Srrs uint8_t override_bit = 0; 2938218072Srrs uint32_t tsn, window_data_tsn; 2939218072Srrs int len; 2940218232Srrs unsigned int pkt_cnt; 2941163953Srrs 2942218072Srrs len = ntohs(cp->ch.chunk_length); 2943218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 2944218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 2945163953Srrs return; 2946163953Srrs } 2947218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 2948218072Srrs /* Its the old format */ 2949218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 2950218072Srrs bkup.num_pkts_since_cwr = htonl(1); 2951218072Srrs cp = &bkup; 2952218072Srrs } 2953163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2954163953Srrs tsn = ntohl(cp->tsn); 2955218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 2956218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 2957163953Srrs if (lchk == NULL) { 2958218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 2959163953Srrs } else { 2960218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 2961163953Srrs } 2962163953Srrs 2963218072Srrs /* Find where it was sent to if possible. */ 2964163953Srrs net = NULL; 2965216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 2966163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2967163953Srrs net = lchk->whoTo; 2968218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 2969163953Srrs break; 2970163953Srrs } 2971216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 2972163953Srrs break; 2973216825Stuexen } 2974163953Srrs } 2975218072Srrs if (net == NULL) { 2976163953Srrs /* 2977218072Srrs * What to do. A previous send of a CWR was possibly lost. 2978218072Srrs * See how old it is, we may have it marked on the actual 2979218072Srrs * net. 2980218072Srrs */ 2981218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2982218072Srrs if (tsn == net->last_cwr_tsn) { 2983218072Srrs /* Found him, send it off */ 2984218072Srrs goto out; 2985218072Srrs } 2986218072Srrs } 2987218072Srrs /* 2988218072Srrs * If we reach here, we need to send a special CWR that says 2989218072Srrs * hey, we did this a long time ago and you lost the 2990218072Srrs * response. 2991218072Srrs */ 2992218072Srrs net = TAILQ_FIRST(&stcb->asoc.nets); 2993218072Srrs override_bit = SCTP_CWR_REDUCE_OVERRIDE; 2994218072Srrs } 2995218072Srrsout: 2996218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 2997218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 2998218072Srrs /* 2999171440Srrs * JRS - Use the congestion control given in the pluggable 3000171440Srrs * CC module 3001171440Srrs */ 3002218072Srrs int ocwnd; 3003218072Srrs 3004218072Srrs ocwnd = net->cwnd; 3005218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3006171440Srrs /* 3007218072Srrs * We reduce once every RTT. So we will only lower cwnd at 3008218072Srrs * the next sending seq i.e. the window_data_tsn 3009163953Srrs */ 3010218072Srrs net->cwr_window_tsn = window_data_tsn; 3011218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3012218072Srrs net->lost_cnt = pkt_cnt; 3013218072Srrs net->last_cwr_tsn = tsn; 3014218072Srrs } else { 3015218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3016218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3017218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3018218072Srrs /* 3019218129Srrs * Another loss in the same window update how many 3020218129Srrs * marks/packets lost we have had. 3021218072Srrs */ 3022218129Srrs int cnt = 1; 3023218072Srrs 3024218072Srrs if (pkt_cnt > net->lost_cnt) { 3025218072Srrs /* Should be the case */ 3026218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3027218129Srrs net->ecn_ce_pkt_cnt += cnt; 3028218072Srrs } 3029218129Srrs net->lost_cnt = pkt_cnt; 3030218072Srrs net->last_cwr_tsn = tsn; 3031218129Srrs /* 3032218129Srrs * Most CC functions will ignore this call, since we 3033218129Srrs * are in-window yet of the initial CE the peer saw. 3034218129Srrs */ 3035218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3036218072Srrs } 3037163953Srrs } 3038163953Srrs /* 3039163953Srrs * We always send a CWR this way if our previous one was lost our 3040163953Srrs * peer will get an update, or if it is not time again to reduce we 3041218072Srrs * still get the cwr to the peer. Note we set the override when we 3042218072Srrs * could not find the TSN on the chunk or the destination network. 3043163953Srrs */ 3044218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3045163953Srrs} 3046163953Srrs 3047163953Srrsstatic void 3048218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3049163953Srrs{ 3050163953Srrs /* 3051163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3052163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3053163953Srrs * If so remove it. 3054163953Srrs */ 3055163953Srrs struct sctp_tmit_chunk *chk; 3056163953Srrs struct sctp_ecne_chunk *ecne; 3057218072Srrs int override; 3058218072Srrs uint32_t cwr_tsn; 3059163953Srrs 3060218072Srrs cwr_tsn = ntohl(cp->tsn); 3061218072Srrs 3062218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3063163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3064163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3065163953Srrs continue; 3066163953Srrs } 3067218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3068218072Srrs /* Must be from the right src unless override is set */ 3069218072Srrs continue; 3070218072Srrs } 3071163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3072218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3073163953Srrs /* this covers this ECNE, we can remove it */ 3074163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3075163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3076163953Srrs sctp_next); 3077163953Srrs if (chk->data) { 3078163953Srrs sctp_m_freem(chk->data); 3079163953Srrs chk->data = NULL; 3080163953Srrs } 3081163953Srrs stcb->asoc.ctrl_queue_cnt--; 3082163953Srrs sctp_free_a_chunk(stcb, chk); 3083218072Srrs if (override == 0) { 3084218072Srrs break; 3085218072Srrs } 3086163953Srrs } 3087163953Srrs } 3088163953Srrs} 3089163953Srrs 3090163953Srrsstatic void 3091163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3092163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3093163953Srrs{ 3094163953Srrs struct sctp_association *asoc; 3095163953Srrs 3096172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3097172090Srrs struct socket *so; 3098172090Srrs 3099172090Srrs#endif 3100172090Srrs 3101169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3102169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3103163953Srrs if (stcb == NULL) 3104163953Srrs return; 3105163953Srrs 3106163953Srrs asoc = &stcb->asoc; 3107163953Srrs /* process according to association state */ 3108163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3109163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3110171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3111171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3112163953Srrs SCTP_TCB_UNLOCK(stcb); 3113163953Srrs return; 3114163953Srrs } 3115163953Srrs /* notify upper layer protocol */ 3116163953Srrs if (stcb->sctp_socket) { 3117172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3118163953Srrs /* are the queues empty? they should be */ 3119163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3120163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3121217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3122172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3123163953Srrs } 3124163953Srrs } 3125163953Srrs /* stop the timer */ 3126178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3127163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3128163953Srrs /* free the TCB */ 3129171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3130171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3131172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3132172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3133172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3134172090Srrs SCTP_TCB_UNLOCK(stcb); 3135172090Srrs SCTP_SOCKET_LOCK(so, 1); 3136172090Srrs SCTP_TCB_LOCK(stcb); 3137172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3138172090Srrs#endif 3139171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3140172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3141172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3142172090Srrs#endif 3143163953Srrs return; 3144163953Srrs} 3145163953Srrs 3146163953Srrsstatic int 3147163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3148163953Srrs struct sctp_nets *net, uint8_t flg) 3149163953Srrs{ 3150163953Srrs switch (desc->chunk_type) { 3151211969Stuexen case SCTP_DATA: 3152163953Srrs /* find the tsn to resend (possibly */ 3153163953Srrs { 3154163953Srrs uint32_t tsn; 3155163953Srrs struct sctp_tmit_chunk *tp1; 3156163953Srrs 3157163953Srrs tsn = ntohl(desc->tsn_ifany); 3158216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3159163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3160163953Srrs /* found it */ 3161163953Srrs break; 3162163953Srrs } 3163216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3164163953Srrs /* not found */ 3165163953Srrs tp1 = NULL; 3166163953Srrs break; 3167163953Srrs } 3168163953Srrs } 3169163953Srrs if (tp1 == NULL) { 3170163953Srrs /* 3171163953Srrs * Do it the other way , aka without paying 3172163953Srrs * attention to queue seq order. 3173163953Srrs */ 3174163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3175216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3176163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3177163953Srrs /* found it */ 3178163953Srrs break; 3179163953Srrs } 3180163953Srrs } 3181163953Srrs } 3182163953Srrs if (tp1 == NULL) { 3183163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3184163953Srrs } 3185163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3186163953Srrs uint8_t *ddp; 3187163953Srrs 3188214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3189214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3190214877Stuexen return (0); 3191214877Stuexen } 3192163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3193163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3194163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3195163953Srrs return (0); 3196163953Srrs } 3197163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3198163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3199163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3200163953Srrs return (0); 3201163953Srrs } 3202163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3203163953Srrs sizeof(struct sctp_data_chunk)); 3204163953Srrs { 3205163953Srrs unsigned int iii; 3206163953Srrs 3207163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3208163953Srrs iii++) { 3209163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3210163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3211163953Srrs return (-1); 3212163953Srrs } 3213163953Srrs } 3214163953Srrs } 3215163953Srrs 3216163953Srrs if (tp1->do_rtt) { 3217163953Srrs /* 3218163953Srrs * this guy had a RTO calculation 3219163953Srrs * pending on it, cancel it 3220163953Srrs */ 3221219397Srrs if (tp1->whoTo->rto_needed == 0) { 3222219397Srrs tp1->whoTo->rto_needed = 1; 3223219397Srrs } 3224163953Srrs tp1->do_rtt = 0; 3225163953Srrs } 3226163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3227163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3228163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3229163953Srrs /* 3230163953Srrs * mark it as if we were doing a FR, since 3231163953Srrs * we will be getting gap ack reports behind 3232163953Srrs * the info from the router. 3233163953Srrs */ 3234163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3235163953Srrs /* 3236163953Srrs * mark the tsn with what sequences can 3237163953Srrs * cause a new FR. 3238163953Srrs */ 3239163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3240163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3241163953Srrs } else { 3242163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3243163953Srrs } 3244163953Srrs 3245163953Srrs /* restart the timer */ 3246163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3247171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3248163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3249163953Srrs stcb, tp1->whoTo); 3250163953Srrs 3251163953Srrs /* fix counts and things */ 3252179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3253170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3254170744Srrs tp1->whoTo->flight_size, 3255170744Srrs tp1->book_size, 3256170744Srrs (uintptr_t) stcb, 3257170744Srrs tp1->rec.data.TSN_seq); 3258170744Srrs } 3259190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3260190689Srrs sctp_flight_size_decrease(tp1); 3261190689Srrs sctp_total_flight_decrease(stcb, tp1); 3262190689Srrs } 3263216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3264163953Srrs } { 3265163953Srrs /* audit code */ 3266163953Srrs unsigned int audit; 3267163953Srrs 3268163953Srrs audit = 0; 3269163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3270163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3271163953Srrs audit++; 3272163953Srrs } 3273163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3274163953Srrs sctp_next) { 3275163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3276163953Srrs audit++; 3277163953Srrs } 3278163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3279169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3280163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3281163953Srrs#ifndef SCTP_AUDITING_ENABLED 3282163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3283163953Srrs#endif 3284163953Srrs } 3285163953Srrs } 3286163953Srrs } 3287163953Srrs break; 3288163953Srrs case SCTP_ASCONF: 3289163953Srrs { 3290163953Srrs struct sctp_tmit_chunk *asconf; 3291163953Srrs 3292163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3293163953Srrs sctp_next) { 3294163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3295163953Srrs break; 3296163953Srrs } 3297163953Srrs } 3298163953Srrs if (asconf) { 3299163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3300163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3301163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3302163953Srrs asconf->snd_count--; 3303163953Srrs } 3304163953Srrs } 3305163953Srrs break; 3306163953Srrs case SCTP_INITIATION: 3307163953Srrs /* resend the INIT */ 3308163953Srrs stcb->asoc.dropped_special_cnt++; 3309163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3310163953Srrs /* 3311163953Srrs * If we can get it in, in a few attempts we do 3312163953Srrs * this, otherwise we let the timer fire. 3313163953Srrs */ 3314163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3315171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3316172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3317163953Srrs } 3318163953Srrs break; 3319163953Srrs case SCTP_SELECTIVE_ACK: 3320206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3321163953Srrs /* resend the sack */ 3322163953Srrs sctp_send_sack(stcb); 3323163953Srrs break; 3324163953Srrs case SCTP_HEARTBEAT_REQUEST: 3325163953Srrs /* resend a demand HB */ 3326171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3327171440Srrs /* 3328171440Srrs * Only retransmit if we KNOW we wont destroy the 3329171440Srrs * tcb 3330171440Srrs */ 3331171440Srrs (void)sctp_send_hb(stcb, 1, net); 3332171440Srrs } 3333163953Srrs break; 3334163953Srrs case SCTP_SHUTDOWN: 3335163953Srrs sctp_send_shutdown(stcb, net); 3336163953Srrs break; 3337163953Srrs case SCTP_SHUTDOWN_ACK: 3338163953Srrs sctp_send_shutdown_ack(stcb, net); 3339163953Srrs break; 3340163953Srrs case SCTP_COOKIE_ECHO: 3341163953Srrs { 3342163953Srrs struct sctp_tmit_chunk *cookie; 3343163953Srrs 3344163953Srrs cookie = NULL; 3345163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3346163953Srrs sctp_next) { 3347163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3348163953Srrs break; 3349163953Srrs } 3350163953Srrs } 3351163953Srrs if (cookie) { 3352163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3353163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3354163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3355163953Srrs sctp_stop_all_cookie_timers(stcb); 3356163953Srrs } 3357163953Srrs } 3358163953Srrs break; 3359163953Srrs case SCTP_COOKIE_ACK: 3360163953Srrs sctp_send_cookie_ack(stcb); 3361163953Srrs break; 3362163953Srrs case SCTP_ASCONF_ACK: 3363163953Srrs /* resend last asconf ack */ 3364171990Srrs sctp_send_asconf_ack(stcb); 3365163953Srrs break; 3366163953Srrs case SCTP_FORWARD_CUM_TSN: 3367163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3368163953Srrs break; 3369163953Srrs /* can't do anything with these */ 3370163953Srrs case SCTP_PACKET_DROPPED: 3371163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3372163953Srrs case SCTP_HEARTBEAT_ACK: 3373163953Srrs case SCTP_ABORT_ASSOCIATION: 3374163953Srrs case SCTP_OPERATION_ERROR: 3375163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3376163953Srrs case SCTP_ECN_ECHO: 3377163953Srrs case SCTP_ECN_CWR: 3378163953Srrs default: 3379163953Srrs break; 3380163953Srrs } 3381163953Srrs return (0); 3382163953Srrs} 3383163953Srrs 3384163953Srrsvoid 3385163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3386163953Srrs{ 3387163953Srrs int i; 3388163953Srrs uint16_t temp; 3389163953Srrs 3390163953Srrs /* 3391163953Srrs * We set things to 0xffff since this is the last delivered sequence 3392163953Srrs * and we will be sending in 0 after the reset. 3393163953Srrs */ 3394163953Srrs 3395163953Srrs if (number_entries) { 3396163953Srrs for (i = 0; i < number_entries; i++) { 3397163953Srrs temp = ntohs(list[i]); 3398163953Srrs if (temp >= stcb->asoc.streamincnt) { 3399163953Srrs continue; 3400163953Srrs } 3401163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3402163953Srrs } 3403163953Srrs } else { 3404163953Srrs list = NULL; 3405163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3406163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3407163953Srrs } 3408163953Srrs } 3409172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3410163953Srrs} 3411163953Srrs 3412163953Srrsstatic void 3413163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3414163953Srrs{ 3415163953Srrs int i; 3416163953Srrs 3417163953Srrs if (number_entries == 0) { 3418163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3419163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3420163953Srrs } 3421163953Srrs } else if (number_entries) { 3422163953Srrs for (i = 0; i < number_entries; i++) { 3423163953Srrs uint16_t temp; 3424163953Srrs 3425163953Srrs temp = ntohs(list[i]); 3426163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3427163953Srrs /* no such stream */ 3428163953Srrs continue; 3429163953Srrs } 3430163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3431163953Srrs } 3432163953Srrs } 3433172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3434163953Srrs} 3435163953Srrs 3436163953Srrs 3437163953Srrsstruct sctp_stream_reset_out_request * 3438163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3439163953Srrs{ 3440163953Srrs struct sctp_association *asoc; 3441163953Srrs struct sctp_stream_reset_out_req *req; 3442163953Srrs struct sctp_stream_reset_out_request *r; 3443163953Srrs struct sctp_tmit_chunk *chk; 3444163953Srrs int len, clen; 3445163953Srrs 3446163953Srrs asoc = &stcb->asoc; 3447163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3448169208Srrs asoc->stream_reset_outstanding = 0; 3449163953Srrs return (NULL); 3450163953Srrs } 3451163953Srrs if (stcb->asoc.str_reset == NULL) { 3452169208Srrs asoc->stream_reset_outstanding = 0; 3453163953Srrs return (NULL); 3454163953Srrs } 3455163953Srrs chk = stcb->asoc.str_reset; 3456163953Srrs if (chk->data == NULL) { 3457163953Srrs return (NULL); 3458163953Srrs } 3459163953Srrs if (bchk) { 3460163953Srrs /* he wants a copy of the chk pointer */ 3461163953Srrs *bchk = chk; 3462163953Srrs } 3463163953Srrs clen = chk->send_size; 3464163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3465163953Srrs r = &req->sr_req; 3466163953Srrs if (ntohl(r->request_seq) == seq) { 3467163953Srrs /* found it */ 3468163953Srrs return (r); 3469163953Srrs } 3470163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3471163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3472163953Srrs /* move to the next one, there can only be a max of two */ 3473163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3474163953Srrs if (ntohl(r->request_seq) == seq) { 3475163953Srrs return (r); 3476163953Srrs } 3477163953Srrs } 3478163953Srrs /* that seq is not here */ 3479163953Srrs return (NULL); 3480163953Srrs} 3481163953Srrs 3482163953Srrsstatic void 3483163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3484163953Srrs{ 3485163953Srrs struct sctp_association *asoc; 3486163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3487163953Srrs 3488163953Srrs if (stcb->asoc.str_reset == NULL) { 3489163953Srrs return; 3490163953Srrs } 3491166675Srrs asoc = &stcb->asoc; 3492166675Srrs 3493171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3494163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3495163953Srrs chk, 3496163953Srrs sctp_next); 3497163953Srrs if (chk->data) { 3498163953Srrs sctp_m_freem(chk->data); 3499163953Srrs chk->data = NULL; 3500163953Srrs } 3501163953Srrs asoc->ctrl_queue_cnt--; 3502163953Srrs sctp_free_a_chunk(stcb, chk); 3503172156Srrs /* sa_ignore NO_NULL_CHK */ 3504163953Srrs stcb->asoc.str_reset = NULL; 3505163953Srrs} 3506163953Srrs 3507163953Srrs 3508163953Srrsstatic int 3509163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3510163953Srrs uint32_t seq, uint32_t action, 3511164139Srrs struct sctp_stream_reset_response *respin) 3512163953Srrs{ 3513163953Srrs uint16_t type; 3514163953Srrs int lparm_len; 3515163953Srrs struct sctp_association *asoc = &stcb->asoc; 3516163953Srrs struct sctp_tmit_chunk *chk; 3517163953Srrs struct sctp_stream_reset_out_request *srparam; 3518163953Srrs int number_entries; 3519163953Srrs 3520163953Srrs if (asoc->stream_reset_outstanding == 0) { 3521163953Srrs /* duplicate */ 3522163953Srrs return (0); 3523163953Srrs } 3524163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3525163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3526163953Srrs if (srparam) { 3527163953Srrs stcb->asoc.str_reset_seq_out++; 3528163953Srrs type = ntohs(srparam->ph.param_type); 3529163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3530163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3531164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3532163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3533163953Srrs if (asoc->stream_reset_outstanding) 3534163953Srrs asoc->stream_reset_outstanding--; 3535163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3536163953Srrs /* do it */ 3537163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3538163953Srrs } else { 3539172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3540163953Srrs } 3541163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3542163953Srrs /* Answered my request */ 3543164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3544163953Srrs if (asoc->stream_reset_outstanding) 3545163953Srrs asoc->stream_reset_outstanding--; 3546163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3547172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3548163953Srrs } 3549188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3550188854Srrs /* Ok we now may have more streams */ 3551189121Srrs if (asoc->stream_reset_outstanding) 3552189121Srrs asoc->stream_reset_outstanding--; 3553188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3554188854Srrs /* Put the new streams into effect */ 3555188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3556188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3557188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3558188854Srrs } else { 3559188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3560188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3561188854Srrs } 3562163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3563163953Srrs /** 3564163953Srrs * a) Adopt the new in tsn. 3565163953Srrs * b) reset the map 3566163953Srrs * c) Adopt the new out-tsn 3567163953Srrs */ 3568163953Srrs struct sctp_stream_reset_response_tsn *resp; 3569163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3570163953Srrs int abort_flag = 0; 3571163953Srrs 3572163953Srrs if (respin == NULL) { 3573163953Srrs /* huh ? */ 3574163953Srrs return (0); 3575163953Srrs } 3576163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3577163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3578163953Srrs asoc->stream_reset_outstanding--; 3579163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3580163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3581163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3582170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3583163953Srrs if (abort_flag) { 3584163953Srrs return (1); 3585163953Srrs } 3586163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3587179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3588179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3589179783Srrs } 3590180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3591163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3592163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3593185694Srrs 3594205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3595206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3596205627Srrs 3597163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3598163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3599163953Srrs 3600163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3601163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3602163953Srrs 3603163953Srrs } 3604163953Srrs } 3605163953Srrs /* get rid of the request and get the request flags */ 3606163953Srrs if (asoc->stream_reset_outstanding == 0) { 3607163953Srrs sctp_clean_up_stream_reset(stcb); 3608163953Srrs } 3609163953Srrs } 3610163953Srrs } 3611163953Srrs return (0); 3612163953Srrs} 3613163953Srrs 3614163953Srrsstatic void 3615163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3616163953Srrs struct sctp_tmit_chunk *chk, 3617170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3618163953Srrs{ 3619163953Srrs uint32_t seq; 3620163953Srrs int len, i; 3621163953Srrs int number_entries; 3622163953Srrs uint16_t temp; 3623163953Srrs 3624163953Srrs /* 3625163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3626163953Srrs * seq_in is right. 3627163953Srrs */ 3628163953Srrs struct sctp_association *asoc = &stcb->asoc; 3629163953Srrs 3630163953Srrs seq = ntohl(req->request_seq); 3631163953Srrs if (asoc->str_reset_seq_in == seq) { 3632170992Srrs if (trunc) { 3633170992Srrs /* Can't do it, since they exceeded our buffer size */ 3634170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3635170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3636170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3637170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3638163953Srrs len = ntohs(req->ph.param_length); 3639163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3640163953Srrs for (i = 0; i < number_entries; i++) { 3641163953Srrs temp = ntohs(req->list_of_streams[i]); 3642163953Srrs req->list_of_streams[i] = temp; 3643163953Srrs } 3644163953Srrs /* move the reset action back one */ 3645163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3646163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3647163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3648163953Srrs asoc->str_reset_seq_out, 3649163953Srrs seq, (asoc->sending_seq - 1)); 3650163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3651163953Srrs asoc->str_reset = chk; 3652163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3653163953Srrs stcb->asoc.stream_reset_outstanding++; 3654163953Srrs } else { 3655163953Srrs /* Can't do it, since we have sent one out */ 3656163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3657163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3658163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3659163953Srrs } 3660163953Srrs asoc->str_reset_seq_in++; 3661163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3662163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3663163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3664163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3665163953Srrs } else { 3666163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3667163953Srrs } 3668163953Srrs} 3669163953Srrs 3670163953Srrsstatic int 3671163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3672163953Srrs struct sctp_tmit_chunk *chk, 3673163953Srrs struct sctp_stream_reset_tsn_request *req) 3674163953Srrs{ 3675163953Srrs /* reset all in and out and update the tsn */ 3676163953Srrs /* 3677163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3678163953Srrs * and set cum-ack to it. Also process this selected number as a 3679163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3680163953Srrs */ 3681163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3682163953Srrs struct sctp_association *asoc = &stcb->asoc; 3683163953Srrs int abort_flag = 0; 3684163953Srrs uint32_t seq; 3685163953Srrs 3686163953Srrs seq = ntohl(req->request_seq); 3687163953Srrs if (asoc->str_reset_seq_in == seq) { 3688163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3689163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3690163953Srrs fwdtsn.ch.chunk_flags = 0; 3691163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3692170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3693163953Srrs if (abort_flag) { 3694163953Srrs return (1); 3695163953Srrs } 3696163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3697179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3698179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3699179783Srrs } 3700180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3701163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3702163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3703205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3704206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3705163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3706163953Srrs /* save off historical data for retrans */ 3707163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3708163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3709163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3710163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3711163953Srrs 3712163953Srrs sctp_add_stream_reset_result_tsn(chk, 3713163953Srrs ntohl(req->request_seq), 3714163953Srrs SCTP_STREAM_RESET_PERFORMED, 3715163953Srrs stcb->asoc.sending_seq, 3716163953Srrs stcb->asoc.mapping_array_base_tsn); 3717163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3718163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3719163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3720163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3721163953Srrs 3722163953Srrs asoc->str_reset_seq_in++; 3723163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3724163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3725163953Srrs stcb->asoc.last_sending_seq[0], 3726163953Srrs stcb->asoc.last_base_tsnsent[0] 3727163953Srrs ); 3728163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3729163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3730163953Srrs stcb->asoc.last_sending_seq[1], 3731163953Srrs stcb->asoc.last_base_tsnsent[1] 3732163953Srrs ); 3733163953Srrs } else { 3734163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3735163953Srrs } 3736163953Srrs return (0); 3737163953Srrs} 3738163953Srrs 3739163953Srrsstatic void 3740163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3741163953Srrs struct sctp_tmit_chunk *chk, 3742170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3743163953Srrs{ 3744163953Srrs uint32_t seq, tsn; 3745163953Srrs int number_entries, len; 3746163953Srrs struct sctp_association *asoc = &stcb->asoc; 3747163953Srrs 3748163953Srrs seq = ntohl(req->request_seq); 3749163953Srrs 3750163953Srrs /* now if its not a duplicate we process it */ 3751163953Srrs if (asoc->str_reset_seq_in == seq) { 3752163953Srrs len = ntohs(req->ph.param_length); 3753163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3754163953Srrs /* 3755163953Srrs * the sender is resetting, handle the list issue.. we must 3756163953Srrs * a) verify if we can do the reset, if so no problem b) If 3757163953Srrs * we can't do the reset we must copy the request. c) queue 3758163953Srrs * it, and setup the data in processor to trigger it off 3759163953Srrs * when needed and dequeue all the queued data. 3760163953Srrs */ 3761163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3762163953Srrs 3763163953Srrs /* move the reset action back one */ 3764163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3765170992Srrs if (trunc) { 3766170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3767170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3768216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3769163953Srrs /* we can do it now */ 3770163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3771163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3772163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3773163953Srrs } else { 3774163953Srrs /* 3775163953Srrs * we must queue it up and thus wait for the TSN's 3776163953Srrs * to arrive that are at or before tsn 3777163953Srrs */ 3778163953Srrs struct sctp_stream_reset_list *liste; 3779163953Srrs int siz; 3780163953Srrs 3781163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3782163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3783170091Srrs siz, SCTP_M_STRESET); 3784163953Srrs if (liste == NULL) { 3785163953Srrs /* gak out of memory */ 3786163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3787163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3788163953Srrs return; 3789163953Srrs } 3790163953Srrs liste->tsn = tsn; 3791163953Srrs liste->number_entries = number_entries; 3792163953Srrs memcpy(&liste->req, req, 3793163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3794163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3795163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3796163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3797163953Srrs } 3798163953Srrs asoc->str_reset_seq_in++; 3799163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3800163953Srrs /* 3801163953Srrs * one seq back, just echo back last action since my 3802163953Srrs * response was lost. 3803163953Srrs */ 3804163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3805163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3806163953Srrs /* 3807163953Srrs * two seq back, just echo back last action since my 3808163953Srrs * response was lost. 3809163953Srrs */ 3810163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3811163953Srrs } else { 3812163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3813163953Srrs } 3814163953Srrs} 3815163953Srrs 3816188854Srrsstatic void 3817188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3818188854Srrs struct sctp_stream_reset_add_strm *str_add) 3819188854Srrs{ 3820188854Srrs /* 3821188854Srrs * Peer is requesting to add more streams. If its within our 3822188854Srrs * max-streams we will allow it. 3823188854Srrs */ 3824188854Srrs uint16_t num_stream, i; 3825188854Srrs uint32_t seq; 3826189121Srrs struct sctp_association *asoc = &stcb->asoc; 3827216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3828188854Srrs 3829188854Srrs /* Get the number. */ 3830188854Srrs seq = ntohl(str_add->request_seq); 3831188854Srrs num_stream = ntohs(str_add->number_of_streams); 3832188854Srrs /* Now what would be the new total? */ 3833189121Srrs if (asoc->str_reset_seq_in == seq) { 3834189121Srrs num_stream += stcb->asoc.streamincnt; 3835189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3836189121Srrs /* We must reject it they ask for to many */ 3837189121Srrs denied: 3838189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3839189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3840189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3841189121Srrs } else { 3842189121Srrs /* Ok, we can do that :-) */ 3843189121Srrs struct sctp_stream_in *oldstrm; 3844189121Srrs 3845189121Srrs /* save off the old */ 3846189121Srrs oldstrm = stcb->asoc.strmin; 3847189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3848189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3849189121Srrs SCTP_M_STRMI); 3850189121Srrs if (stcb->asoc.strmin == NULL) { 3851189121Srrs stcb->asoc.strmin = oldstrm; 3852189121Srrs goto denied; 3853189121Srrs } 3854189121Srrs /* copy off the old data */ 3855189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3856189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3857189121Srrs stcb->asoc.strmin[i].stream_no = i; 3858189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3859189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3860189121Srrs /* now anything on those queues? */ 3861216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3862189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3863189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3864189121Srrs } 3865189121Srrs } 3866189121Srrs /* Init the new streams */ 3867189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3868189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3869189121Srrs stcb->asoc.strmin[i].stream_no = i; 3870189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3871189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3872189121Srrs } 3873189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3874189121Srrs /* update the size */ 3875189121Srrs stcb->asoc.streamincnt = num_stream; 3876189121Srrs /* Send the ack */ 3877189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3878189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3879189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3880189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3881189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3882189121Srrs } 3883189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3884189121Srrs /* 3885189121Srrs * one seq back, just echo back last action since my 3886189121Srrs * response was lost. 3887189121Srrs */ 3888189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3889189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3890189121Srrs /* 3891189121Srrs * two seq back, just echo back last action since my 3892189121Srrs * response was lost. 3893189121Srrs */ 3894189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3895188854Srrs } else { 3896189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3897188854Srrs 3898188854Srrs } 3899188854Srrs} 3900188854Srrs 3901170992Srrs#ifdef __GNUC__ 3902170992Srrs__attribute__((noinline)) 3903170992Srrs#endif 3904170992Srrs static int 3905170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3906170992Srrs struct sctp_stream_reset_out_req *sr_req) 3907163953Srrs{ 3908163953Srrs int chk_length, param_len, ptype; 3909170992Srrs struct sctp_paramhdr pstore; 3910170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3911170992Srrs 3912163953Srrs uint32_t seq; 3913163953Srrs int num_req = 0; 3914170992Srrs int trunc = 0; 3915163953Srrs struct sctp_tmit_chunk *chk; 3916163953Srrs struct sctp_chunkhdr *ch; 3917163953Srrs struct sctp_paramhdr *ph; 3918166675Srrs int ret_code = 0; 3919166675Srrs int num_param = 0; 3920163953Srrs 3921163953Srrs /* now it may be a reset or a reset-response */ 3922163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3923163953Srrs 3924163953Srrs /* setup for adding the response */ 3925163953Srrs sctp_alloc_a_chunk(stcb, chk); 3926163953Srrs if (chk == NULL) { 3927163953Srrs return (ret_code); 3928163953Srrs } 3929163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3930169208Srrs chk->rec.chunk_id.can_take_data = 0; 3931163953Srrs chk->asoc = &stcb->asoc; 3932163953Srrs chk->no_fr_allowed = 0; 3933163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3934166023Srrs chk->book_size_scale = 0; 3935165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3936163953Srrs if (chk->data == NULL) { 3937163953Srrsstrres_nochunk: 3938163953Srrs if (chk->data) { 3939163953Srrs sctp_m_freem(chk->data); 3940163953Srrs chk->data = NULL; 3941163953Srrs } 3942163953Srrs sctp_free_a_chunk(stcb, chk); 3943163953Srrs return (ret_code); 3944163953Srrs } 3945165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3946163953Srrs 3947163953Srrs /* setup chunk parameters */ 3948163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3949163953Srrs chk->snd_count = 0; 3950163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3951163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3952163953Srrs 3953163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3954163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3955163953Srrs ch->chunk_flags = 0; 3956163953Srrs ch->chunk_length = htons(chk->send_size); 3957165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3958170992Srrs offset += sizeof(struct sctp_chunkhdr); 3959163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3960170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3961170992Srrs if (ph == NULL) 3962170992Srrs break; 3963163953Srrs param_len = ntohs(ph->param_length); 3964163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3965163953Srrs /* bad param */ 3966163953Srrs break; 3967163953Srrs } 3968170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3969170992Srrs (uint8_t *) & cstore); 3970163953Srrs ptype = ntohs(ph->param_type); 3971163953Srrs num_param++; 3972170994Srrs if (param_len > (int)sizeof(cstore)) { 3973170992Srrs trunc = 1; 3974170992Srrs } else { 3975170992Srrs trunc = 0; 3976170992Srrs } 3977170992Srrs 3978163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3979163953Srrs /* hit the max of parameters already sorry.. */ 3980163953Srrs break; 3981163953Srrs } 3982163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3983163953Srrs struct sctp_stream_reset_out_request *req_out; 3984163953Srrs 3985163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3986163953Srrs num_req++; 3987163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3988163953Srrs seq = ntohl(req_out->response_seq); 3989163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3990163953Srrs /* implicit ack */ 3991169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3992163953Srrs } 3993163953Srrs } 3994170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3995188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3996188854Srrs struct sctp_stream_reset_add_strm *str_add; 3997188854Srrs 3998188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3999188854Srrs num_req++; 4000188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4001163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4002163953Srrs struct sctp_stream_reset_in_request *req_in; 4003163953Srrs 4004163953Srrs num_req++; 4005170992Srrs 4006163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 4007170992Srrs 4008170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4009163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4010163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 4011163953Srrs 4012163953Srrs num_req++; 4013163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4014170992Srrs 4015163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4016163953Srrs ret_code = 1; 4017163953Srrs goto strres_nochunk; 4018163953Srrs } 4019163953Srrs /* no more */ 4020163953Srrs break; 4021163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4022163953Srrs struct sctp_stream_reset_response *resp; 4023163953Srrs uint32_t result; 4024163953Srrs 4025163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4026163953Srrs seq = ntohl(resp->response_seq); 4027163953Srrs result = ntohl(resp->result); 4028163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4029163953Srrs ret_code = 1; 4030163953Srrs goto strres_nochunk; 4031163953Srrs } 4032163953Srrs } else { 4033163953Srrs break; 4034163953Srrs } 4035170992Srrs offset += SCTP_SIZE32(param_len); 4036163953Srrs chk_length -= SCTP_SIZE32(param_len); 4037163953Srrs } 4038163953Srrs if (num_req == 0) { 4039163953Srrs /* we have no response free the stuff */ 4040163953Srrs goto strres_nochunk; 4041163953Srrs } 4042163953Srrs /* ok we have a chunk to link in */ 4043163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4044163953Srrs chk, 4045163953Srrs sctp_next); 4046163953Srrs stcb->asoc.ctrl_queue_cnt++; 4047163953Srrs return (ret_code); 4048163953Srrs} 4049163953Srrs 4050163953Srrs/* 4051163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4052163953Srrs * to handle this, either we get the whole packet and must disect it 4053163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4054163953Srrs * from a middle box that did the disectting for us. 4055163953Srrs */ 4056163953Srrsstatic void 4057163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4058170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4059163953Srrs{ 4060163953Srrs uint32_t bottle_bw, on_queue; 4061163953Srrs uint16_t trunc_len; 4062163953Srrs unsigned int chlen; 4063163953Srrs unsigned int at; 4064163953Srrs struct sctp_chunk_desc desc; 4065163953Srrs struct sctp_chunkhdr *ch; 4066163953Srrs 4067163953Srrs chlen = ntohs(cp->ch.chunk_length); 4068163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4069163953Srrs /* XXX possible chlen underflow */ 4070163953Srrs if (chlen == 0) { 4071163953Srrs ch = NULL; 4072163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4073163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4074163953Srrs } else { 4075163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4076163953Srrs chlen -= sizeof(struct sctphdr); 4077163953Srrs /* XXX possible chlen underflow */ 4078163953Srrs memset(&desc, 0, sizeof(desc)); 4079163953Srrs } 4080163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4081170781Srrs if (trunc_len > limit) { 4082170781Srrs trunc_len = limit; 4083170781Srrs } 4084163953Srrs /* now the chunks themselves */ 4085163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4086163953Srrs desc.chunk_type = ch->chunk_type; 4087163953Srrs /* get amount we need to move */ 4088163953Srrs at = ntohs(ch->chunk_length); 4089163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4090163953Srrs /* corrupt chunk, maybe at the end? */ 4091163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4092163953Srrs break; 4093163953Srrs } 4094163953Srrs if (trunc_len == 0) { 4095163953Srrs /* we are supposed to have all of it */ 4096163953Srrs if (at > chlen) { 4097163953Srrs /* corrupt skip it */ 4098163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4099163953Srrs break; 4100163953Srrs } 4101163953Srrs } else { 4102163953Srrs /* is there enough of it left ? */ 4103163953Srrs if (desc.chunk_type == SCTP_DATA) { 4104163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4105163953Srrs sizeof(desc.data_bytes))) { 4106163953Srrs break; 4107163953Srrs } 4108163953Srrs } else { 4109163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4110163953Srrs break; 4111163953Srrs } 4112163953Srrs } 4113163953Srrs } 4114163953Srrs if (desc.chunk_type == SCTP_DATA) { 4115163953Srrs /* can we get out the tsn? */ 4116163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4117163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4118163953Srrs 4119163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4120163953Srrs /* yep */ 4121163953Srrs struct sctp_data_chunk *dcp; 4122163953Srrs uint8_t *ddp; 4123163953Srrs unsigned int iii; 4124163953Srrs 4125163953Srrs dcp = (struct sctp_data_chunk *)ch; 4126163953Srrs ddp = (uint8_t *) (dcp + 1); 4127163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4128163953Srrs desc.data_bytes[iii] = ddp[iii]; 4129163953Srrs } 4130163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4131163953Srrs } else { 4132163953Srrs /* nope we are done. */ 4133163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4134163953Srrs break; 4135163953Srrs } 4136163953Srrs } else { 4137163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4138163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4139163953Srrs } 4140163953Srrs 4141163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4142163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4143163953Srrs break; 4144163953Srrs } 4145163953Srrs if (SCTP_SIZE32(at) > chlen) { 4146163953Srrs break; 4147163953Srrs } 4148163953Srrs chlen -= SCTP_SIZE32(at); 4149163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4150163953Srrs /* done, none left */ 4151163953Srrs break; 4152163953Srrs } 4153163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4154163953Srrs } 4155163953Srrs /* Now update any rwnd --- possibly */ 4156163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4157163953Srrs /* From a peer, we get a rwnd report */ 4158163953Srrs uint32_t a_rwnd; 4159163953Srrs 4160163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4161163953Srrs 4162163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4163163953Srrs on_queue = ntohl(cp->current_onq); 4164163953Srrs if (bottle_bw && on_queue) { 4165163953Srrs /* a rwnd report is in here */ 4166163953Srrs if (bottle_bw > on_queue) 4167163953Srrs a_rwnd = bottle_bw - on_queue; 4168163953Srrs else 4169163953Srrs a_rwnd = 0; 4170163953Srrs 4171163953Srrs if (a_rwnd == 0) 4172163953Srrs stcb->asoc.peers_rwnd = 0; 4173163953Srrs else { 4174163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4175163953Srrs stcb->asoc.peers_rwnd = 4176163953Srrs a_rwnd - stcb->asoc.total_flight; 4177163953Srrs } else { 4178163953Srrs stcb->asoc.peers_rwnd = 0; 4179163953Srrs } 4180163953Srrs if (stcb->asoc.peers_rwnd < 4181163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4182163953Srrs /* SWS sender side engages */ 4183163953Srrs stcb->asoc.peers_rwnd = 0; 4184163953Srrs } 4185163953Srrs } 4186163953Srrs } 4187163953Srrs } else { 4188163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4189163953Srrs } 4190163953Srrs 4191163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4192163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4193163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4194163953Srrs (stcb->asoc.sat_network)) { 4195163953Srrs /* 4196163953Srrs * This is debateable but for sat networks it makes sense 4197163953Srrs * Note if a T3 timer has went off, we will prohibit any 4198163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4199163953Srrs */ 4200171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4201171440Srrs net, cp, &bottle_bw, &on_queue); 4202163953Srrs } 4203163953Srrs} 4204163953Srrs 4205163953Srrs/* 4206163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4207163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4208163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4209163953Srrs * length of the complete packet outputs: - length: modified to remaining 4210163953Srrs * length after control processing - netp: modified to new sctp_nets after 4211163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4212163953Srrs * bad packet,...) otherwise return the tcb for this packet 4213163953Srrs */ 4214170099Srrs#ifdef __GNUC__ 4215170099Srrs__attribute__((noinline)) 4216170099Srrs#endif 4217170099Srrs static struct sctp_tcb * 4218170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4219170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4220170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4221179157Srrs uint32_t vrf_id, uint16_t port) 4222163953Srrs{ 4223163953Srrs struct sctp_association *asoc; 4224163953Srrs uint32_t vtag_in; 4225163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4226170140Srrs uint32_t chk_length; 4227163953Srrs int ret; 4228168299Srrs int abort_no_unlock = 0; 4229218186Srrs int ecne_seen = 0; 4230163953Srrs 4231163953Srrs /* 4232163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4233163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4234163953Srrs * until we get into jumbo grams and such.. 4235163953Srrs */ 4236166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4237163953Srrs struct sctp_tcb *locked_tcb = stcb; 4238163953Srrs int got_auth = 0; 4239163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4240163953Srrs int auth_skipped = 0; 4241171990Srrs int asconf_cnt = 0; 4242163953Srrs 4243172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4244172090Srrs struct socket *so; 4245172090Srrs 4246172090Srrs#endif 4247172090Srrs 4248169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4249169420Srrs iphlen, *offset, length, stcb); 4250163953Srrs 4251163953Srrs /* validate chunk header length... */ 4252163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4253170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4254170056Srrs ntohs(ch->chunk_length)); 4255170091Srrs if (locked_tcb) { 4256170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4257170091Srrs } 4258163953Srrs return (NULL); 4259163953Srrs } 4260163953Srrs /* 4261163953Srrs * validate the verification tag 4262163953Srrs */ 4263163953Srrs vtag_in = ntohl(sh->v_tag); 4264163953Srrs 4265165220Srrs if (locked_tcb) { 4266165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4267165220Srrs } 4268163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4269170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4270170056Srrs ntohs(ch->chunk_length), vtag_in); 4271163953Srrs if (vtag_in != 0) { 4272163953Srrs /* protocol error- silently discard... */ 4273163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4274169378Srrs if (locked_tcb) { 4275163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4276169378Srrs } 4277163953Srrs return (NULL); 4278163953Srrs } 4279163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4280163953Srrs /* 4281163953Srrs * If there is no stcb, skip the AUTH chunk and process 4282163953Srrs * later after a stcb is found (to validate the lookup was 4283163953Srrs * valid. 4284163953Srrs */ 4285163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4286179783Srrs (stcb == NULL) && 4287179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4288163953Srrs /* save this chunk for later processing */ 4289163953Srrs auth_skipped = 1; 4290163953Srrs auth_offset = *offset; 4291163953Srrs auth_len = ntohs(ch->chunk_length); 4292163953Srrs 4293163953Srrs /* (temporarily) move past this chunk */ 4294163953Srrs *offset += SCTP_SIZE32(auth_len); 4295163953Srrs if (*offset >= length) { 4296163953Srrs /* no more data left in the mbuf chain */ 4297163953Srrs *offset = length; 4298170091Srrs if (locked_tcb) { 4299170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4300170091Srrs } 4301163953Srrs return (NULL); 4302163953Srrs } 4303163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4304163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4305163953Srrs } 4306169420Srrs if (ch == NULL) { 4307169420Srrs /* Help */ 4308169420Srrs *offset = length; 4309170091Srrs if (locked_tcb) { 4310170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4311170091Srrs } 4312169420Srrs return (NULL); 4313169420Srrs } 4314163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4315163953Srrs goto process_control_chunks; 4316163953Srrs } 4317163953Srrs /* 4318163953Srrs * first check if it's an ASCONF with an unknown src addr we 4319163953Srrs * need to look inside to find the association 4320163953Srrs */ 4321163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4322171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4323171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4324171990Srrs 4325163953Srrs /* inp's refcount may be reduced */ 4326163953Srrs SCTP_INP_INCR_REF(inp); 4327163953Srrs 4328171990Srrs asconf_offset = *offset; 4329171990Srrs do { 4330171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4331171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4332171990Srrs break; 4333171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4334185694Srrs *offset, sh, &inp, netp, vrf_id); 4335171990Srrs if (stcb != NULL) 4336171990Srrs break; 4337171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4338171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4339171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4340171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4341163953Srrs if (stcb == NULL) { 4342163953Srrs /* 4343163953Srrs * reduce inp's refcount if not reduced in 4344163953Srrs * sctp_findassociation_ep_asconf(). 4345163953Srrs */ 4346163953Srrs SCTP_INP_DECR_REF(inp); 4347171990Srrs } else { 4348171990Srrs locked_tcb = stcb; 4349163953Srrs } 4350171990Srrs 4351163953Srrs /* now go back and verify any auth chunk to be sure */ 4352163953Srrs if (auth_skipped && (stcb != NULL)) { 4353163953Srrs struct sctp_auth_chunk *auth; 4354163953Srrs 4355163953Srrs auth = (struct sctp_auth_chunk *) 4356163953Srrs sctp_m_getptr(m, auth_offset, 4357163953Srrs auth_len, chunk_buf); 4358163953Srrs got_auth = 1; 4359163953Srrs auth_skipped = 0; 4360169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4361163953Srrs auth_offset)) { 4362163953Srrs /* auth HMAC failed so dump it */ 4363163953Srrs *offset = length; 4364170091Srrs if (locked_tcb) { 4365170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4366170091Srrs } 4367163953Srrs return (NULL); 4368163953Srrs } else { 4369163953Srrs /* remaining chunks are HMAC checked */ 4370163953Srrs stcb->asoc.authenticated = 1; 4371163953Srrs } 4372163953Srrs } 4373163953Srrs } 4374163953Srrs if (stcb == NULL) { 4375163953Srrs /* no association, so it's out of the blue... */ 4376169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4377179157Srrs vrf_id, port); 4378163953Srrs *offset = length; 4379169378Srrs if (locked_tcb) { 4380163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4381169378Srrs } 4382163953Srrs return (NULL); 4383163953Srrs } 4384163953Srrs asoc = &stcb->asoc; 4385163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4386163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4387163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4388163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4389163953Srrs if ((vtag_in == asoc->my_vtag) || 4390163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4391163953Srrs (vtag_in == asoc->peer_vtag))) { 4392163953Srrs /* this is valid */ 4393163953Srrs } else { 4394163953Srrs /* drop this packet... */ 4395163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4396169378Srrs if (locked_tcb) { 4397163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4398169378Srrs } 4399163953Srrs return (NULL); 4400163953Srrs } 4401163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4402163953Srrs if (vtag_in != asoc->my_vtag) { 4403163953Srrs /* 4404163953Srrs * this could be a stale SHUTDOWN-ACK or the 4405163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4406163953Srrs * is still hung; we have started a new asoc 4407163953Srrs * but it won't complete until the shutdown 4408163953Srrs * is completed 4409163953Srrs */ 4410169378Srrs if (locked_tcb) { 4411163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4412169378Srrs } 4413163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4414179157Srrs NULL, vrf_id, port); 4415163953Srrs return (NULL); 4416163953Srrs } 4417163953Srrs } else { 4418163953Srrs /* for all other chunks, vtag must match */ 4419163953Srrs if (vtag_in != asoc->my_vtag) { 4420163953Srrs /* invalid vtag... */ 4421169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4422169420Srrs "invalid vtag: %xh, expect %xh\n", 4423169420Srrs vtag_in, asoc->my_vtag); 4424163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4425169378Srrs if (locked_tcb) { 4426163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4427169378Srrs } 4428163953Srrs *offset = length; 4429163953Srrs return (NULL); 4430163953Srrs } 4431163953Srrs } 4432163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4433163953Srrs /* 4434163953Srrs * process all control chunks... 4435163953Srrs */ 4436163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4437185694Srrs /* EY */ 4438185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4439163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4440163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4441163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4442179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4443171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4444171943Srrs stcb->asoc.overall_error_count, 4445171943Srrs 0, 4446171943Srrs SCTP_FROM_SCTP_INPUT, 4447171943Srrs __LINE__); 4448171943Srrs } 4449163953Srrs stcb->asoc.overall_error_count = 0; 4450163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4451163953Srrs *netp); 4452163953Srrs } 4453163953Srrsprocess_control_chunks: 4454163953Srrs while (IS_SCTP_CONTROL(ch)) { 4455163953Srrs /* validate chunk length */ 4456163953Srrs chk_length = ntohs(ch->chunk_length); 4457169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4458169420Srrs ch->chunk_type, chk_length); 4459170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4460170140Srrs if (chk_length < sizeof(*ch) || 4461170140Srrs (*offset + (int)chk_length) > length) { 4462163953Srrs *offset = length; 4463169378Srrs if (locked_tcb) { 4464163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4465169378Srrs } 4466163953Srrs return (NULL); 4467163953Srrs } 4468163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4469163953Srrs /* 4470163953Srrs * INIT-ACK only gets the init ack "header" portion only 4471163953Srrs * because we don't have to process the peer's COOKIE. All 4472163953Srrs * others get a complete chunk. 4473163953Srrs */ 4474169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4475169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4476163953Srrs /* get an init-ack chunk */ 4477163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4478163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4479163953Srrs if (ch == NULL) { 4480163953Srrs *offset = length; 4481169378Srrs if (locked_tcb) { 4482163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4483169378Srrs } 4484163953Srrs return (NULL); 4485163953Srrs } 4486170642Srrs } else { 4487170786Srrs /* For cookies and all other chunks. */ 4488169208Srrs if (chk_length > sizeof(chunk_buf)) { 4489169208Srrs /* 4490169208Srrs * use just the size of the chunk buffer so 4491170642Srrs * the front part of our chunks fit in 4492170642Srrs * contiguous space up to the chunk buffer 4493170642Srrs * size (508 bytes). For chunks that need to 4494170786Srrs * get more than that they must use the 4495170642Srrs * sctp_m_getptr() function or other means 4496170786Srrs * (e.g. know how to parse mbuf chains). 4497170786Srrs * Cookies do this already. 4498169208Srrs */ 4499169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4500169208Srrs (sizeof(chunk_buf) - 4), 4501169208Srrs chunk_buf); 4502169208Srrs if (ch == NULL) { 4503169208Srrs *offset = length; 4504169378Srrs if (locked_tcb) { 4505169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4506169378Srrs } 4507169208Srrs return (NULL); 4508169208Srrs } 4509169208Srrs } else { 4510169208Srrs /* We can fit it all */ 4511170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4512170642Srrs chk_length, chunk_buf); 4513170642Srrs if (ch == NULL) { 4514170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4515170642Srrs *offset = length; 4516170642Srrs if (locked_tcb) { 4517170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4518169208Srrs } 4519170642Srrs return (NULL); 4520163953Srrs } 4521163953Srrs } 4522163953Srrs } 4523163953Srrs num_chunks++; 4524163953Srrs /* Save off the last place we got a control from */ 4525163953Srrs if (stcb != NULL) { 4526169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4527163953Srrs /* 4528163953Srrs * allow last_control to be NULL if 4529163953Srrs * ASCONF... ASCONF processing will find the 4530163953Srrs * right net later 4531163953Srrs */ 4532169420Srrs if ((netp != NULL) && (*netp != NULL)) 4533169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4534163953Srrs } 4535163953Srrs } 4536163953Srrs#ifdef SCTP_AUDITING_ENABLED 4537163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4538163953Srrs#endif 4539163953Srrs 4540163953Srrs /* check to see if this chunk required auth, but isn't */ 4541179783Srrs if ((stcb != NULL) && 4542179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4543179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4544163953Srrs !stcb->asoc.authenticated) { 4545163953Srrs /* "silently" ignore */ 4546163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4547163953Srrs goto next_chunk; 4548163953Srrs } 4549163953Srrs switch (ch->chunk_type) { 4550163953Srrs case SCTP_INITIATION: 4551163953Srrs /* must be first and only chunk */ 4552169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4553163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4554163953Srrs /* We are not interested anymore? */ 4555163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4556163953Srrs /* 4557163953Srrs * collision case where we are 4558163953Srrs * sending to them too 4559163953Srrs */ 4560163953Srrs ; 4561163953Srrs } else { 4562169378Srrs if (locked_tcb) { 4563163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4564169378Srrs } 4565163953Srrs *offset = length; 4566163953Srrs return (NULL); 4567163953Srrs } 4568163953Srrs } 4569171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4570171990Srrs (num_chunks > 1) || 4571179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4572163953Srrs *offset = length; 4573169378Srrs if (locked_tcb) { 4574163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4575169378Srrs } 4576163953Srrs return (NULL); 4577163953Srrs } 4578163953Srrs if ((stcb != NULL) && 4579163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4580163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4581163953Srrs sctp_send_shutdown_ack(stcb, 4582163953Srrs stcb->asoc.primary_destination); 4583163953Srrs *offset = length; 4584172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4585169378Srrs if (locked_tcb) { 4586163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4587169378Srrs } 4588163953Srrs return (NULL); 4589163953Srrs } 4590169420Srrs if (netp) { 4591169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4592169420Srrs (struct sctp_init_chunk *)ch, inp, 4593179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4594169420Srrs } 4595168299Srrs if (abort_no_unlock) 4596168299Srrs return (NULL); 4597168299Srrs 4598163953Srrs *offset = length; 4599169378Srrs if (locked_tcb) { 4600163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4601169378Srrs } 4602163953Srrs return (NULL); 4603163953Srrs break; 4604170642Srrs case SCTP_PAD_CHUNK: 4605170642Srrs break; 4606163953Srrs case SCTP_INITIATION_ACK: 4607163953Srrs /* must be first and only chunk */ 4608169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4609163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4610163953Srrs /* We are not interested anymore */ 4611163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4612163953Srrs ; 4613163953Srrs } else { 4614208876Srrs if (locked_tcb != stcb) { 4615208876Srrs /* Very unlikely */ 4616163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4617169378Srrs } 4618163953Srrs *offset = length; 4619163953Srrs if (stcb) { 4620172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4621172090Srrs so = SCTP_INP_SO(inp); 4622172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4623172090Srrs SCTP_TCB_UNLOCK(stcb); 4624172090Srrs SCTP_SOCKET_LOCK(so, 1); 4625172090Srrs SCTP_TCB_LOCK(stcb); 4626172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4627172090Srrs#endif 4628171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4629172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4630172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4631172090Srrs#endif 4632163953Srrs } 4633163953Srrs return (NULL); 4634163953Srrs } 4635163953Srrs } 4636163953Srrs if ((num_chunks > 1) || 4637179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4638163953Srrs *offset = length; 4639169378Srrs if (locked_tcb) { 4640163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4641169378Srrs } 4642163953Srrs return (NULL); 4643163953Srrs } 4644169420Srrs if ((netp) && (*netp)) { 4645169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4646170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4647169420Srrs } else { 4648169420Srrs ret = -1; 4649169420Srrs } 4650163953Srrs /* 4651163953Srrs * Special case, I must call the output routine to 4652163953Srrs * get the cookie echoed 4653163953Srrs */ 4654168299Srrs if (abort_no_unlock) 4655168299Srrs return (NULL); 4656168299Srrs 4657163953Srrs if ((stcb) && ret == 0) 4658172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4659163953Srrs *offset = length; 4660169378Srrs if (locked_tcb) { 4661163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4662169378Srrs } 4663163953Srrs return (NULL); 4664163953Srrs break; 4665163953Srrs case SCTP_SELECTIVE_ACK: 4666163953Srrs { 4667163953Srrs struct sctp_sack_chunk *sack; 4668163953Srrs int abort_now = 0; 4669163953Srrs uint32_t a_rwnd, cum_ack; 4670202526Stuexen uint16_t num_seg, num_dup; 4671202526Stuexen uint8_t flags; 4672202526Stuexen int offset_seg, offset_dup; 4673163953Srrs 4674211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4675211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4676202526Stuexen if (stcb == NULL) { 4677202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4678202526Stuexen break; 4679169208Srrs } 4680202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4681202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4682202526Stuexen break; 4683202526Stuexen } 4684171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4685171990Srrs /*- 4686171990Srrs * If we have sent a shutdown-ack, we will pay no 4687171990Srrs * attention to a sack sent in to us since 4688171990Srrs * we don't care anymore. 4689171990Srrs */ 4690184883Srrs break; 4691171990Srrs } 4692163953Srrs sack = (struct sctp_sack_chunk *)ch; 4693202526Stuexen flags = ch->chunk_flags; 4694163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4695163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4696202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4697163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4698202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4699202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4700202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4701202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4702202526Stuexen break; 4703202526Stuexen } 4704202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4705202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4706170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4707202526Stuexen cum_ack, num_seg, a_rwnd); 4708163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4709163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4710163953Srrs (num_seg == 0) && 4711216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4712163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4713216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4714163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4715163953Srrs ) { 4716163953Srrs /* 4717163953Srrs * We have a SIMPLE sack having no 4718163953Srrs * prior segments and data on sent 4719163953Srrs * queue to be acked.. Use the 4720163953Srrs * faster path sack processing. We 4721163953Srrs * also allow window update sacks 4722163953Srrs * with no missing segments to go 4723163953Srrs * this way too. 4724163953Srrs */ 4725218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4726163953Srrs } else { 4727169420Srrs if (netp && *netp) 4728202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4729202526Stuexen stcb, *netp, 4730202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4731218186Srrs cum_ack, a_rwnd, ecne_seen); 4732163953Srrs } 4733202526Stuexen if (abort_now) { 4734202526Stuexen /* ABORT signal from sack processing */ 4735202526Stuexen *offset = length; 4736202526Stuexen return (NULL); 4737202526Stuexen } 4738185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4739185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4740185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4741185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4742185694Srrs } 4743163953Srrs } 4744163953Srrs break; 4745185694Srrs /* 4746185694Srrs * EY - nr_sack: If the received chunk is an 4747185694Srrs * nr_sack chunk 4748185694Srrs */ 4749185694Srrs case SCTP_NR_SELECTIVE_ACK: 4750185694Srrs { 4751185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4752185694Srrs int abort_now = 0; 4753185694Srrs uint32_t a_rwnd, cum_ack; 4754202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4755202526Stuexen uint8_t flags; 4756202526Stuexen int offset_seg, offset_dup; 4757185694Srrs 4758211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4759211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4760212225Srrs if (stcb == NULL) { 4761212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4762212225Srrs break; 4763212225Srrs } 4764211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4765211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4766185694Srrs goto unknown_chunk; 4767185694Srrs } 4768202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4769202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4770202526Stuexen break; 4771202526Stuexen } 4772185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4773185694Srrs /*- 4774185694Srrs * If we have sent a shutdown-ack, we will pay no 4775185694Srrs * attention to a sack sent in to us since 4776185694Srrs * we don't care anymore. 4777185694Srrs */ 4778202526Stuexen break; 4779185694Srrs } 4780185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4781202526Stuexen flags = ch->chunk_flags; 4782185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4783185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4784185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4785202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4786185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4787202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4788202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4789202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4790202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4791202526Stuexen break; 4792202526Stuexen } 4793202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4794202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4795185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4796202526Stuexen cum_ack, num_seg, a_rwnd); 4797185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4798185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4799202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4800216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4801185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4802216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4803202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4804185694Srrs /* 4805185694Srrs * We have a SIMPLE sack having no 4806185694Srrs * prior segments and data on sent 4807202526Stuexen * queue to be acked. Use the faster 4808202526Stuexen * path sack processing. We also 4809202526Stuexen * allow window update sacks with no 4810202526Stuexen * missing segments to go this way 4811202526Stuexen * too. 4812185694Srrs */ 4813218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4814218186Srrs &abort_now, ecne_seen); 4815185694Srrs } else { 4816185694Srrs if (netp && *netp) 4817202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4818202526Stuexen stcb, *netp, 4819202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4820218186Srrs cum_ack, a_rwnd, ecne_seen); 4821185694Srrs } 4822202526Stuexen if (abort_now) { 4823202526Stuexen /* ABORT signal from sack processing */ 4824202526Stuexen *offset = length; 4825202526Stuexen return (NULL); 4826202526Stuexen } 4827185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4828185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4829185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4830185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4831185694Srrs } 4832185694Srrs } 4833185694Srrs break; 4834185694Srrs 4835163953Srrs case SCTP_HEARTBEAT_REQUEST: 4836169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4837169420Srrs if ((stcb) && netp && *netp) { 4838169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4839169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4840169420Srrs chk_length, *netp); 4841169420Srrs 4842169420Srrs /* He's alive so give him credit */ 4843179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4844171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4845171943Srrs stcb->asoc.overall_error_count, 4846171943Srrs 0, 4847171943Srrs SCTP_FROM_SCTP_INPUT, 4848171943Srrs __LINE__); 4849171943Srrs } 4850169420Srrs stcb->asoc.overall_error_count = 0; 4851163953Srrs } 4852163953Srrs break; 4853163953Srrs case SCTP_HEARTBEAT_ACK: 4854169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4855169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4856169208Srrs /* Its not ours */ 4857169352Srrs *offset = length; 4858169378Srrs if (locked_tcb) { 4859169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4860169378Srrs } 4861169208Srrs return (NULL); 4862169208Srrs } 4863163953Srrs /* He's alive so give him credit */ 4864179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4865171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4866171943Srrs stcb->asoc.overall_error_count, 4867171943Srrs 0, 4868171943Srrs SCTP_FROM_SCTP_INPUT, 4869171943Srrs __LINE__); 4870171943Srrs } 4871163953Srrs stcb->asoc.overall_error_count = 0; 4872163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4873169420Srrs if (netp && *netp) 4874169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4875169420Srrs stcb, *netp); 4876163953Srrs break; 4877163953Srrs case SCTP_ABORT_ASSOCIATION: 4878170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4879170091Srrs stcb); 4880169420Srrs if ((stcb) && netp && *netp) 4881169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4882169420Srrs stcb, *netp); 4883163953Srrs *offset = length; 4884163953Srrs return (NULL); 4885163953Srrs break; 4886163953Srrs case SCTP_SHUTDOWN: 4887170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4888170091Srrs stcb); 4889169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4890169208Srrs *offset = length; 4891169378Srrs if (locked_tcb) { 4892169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4893169378Srrs } 4894169208Srrs return (NULL); 4895169420Srrs } 4896169420Srrs if (netp && *netp) { 4897163953Srrs int abort_flag = 0; 4898163953Srrs 4899163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4900163953Srrs stcb, *netp, &abort_flag); 4901163953Srrs if (abort_flag) { 4902163953Srrs *offset = length; 4903163953Srrs return (NULL); 4904163953Srrs } 4905163953Srrs } 4906163953Srrs break; 4907163953Srrs case SCTP_SHUTDOWN_ACK: 4908170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4909169420Srrs if ((stcb) && (netp) && (*netp)) 4910169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4911163953Srrs *offset = length; 4912163953Srrs return (NULL); 4913163953Srrs break; 4914169420Srrs 4915163953Srrs case SCTP_OPERATION_ERROR: 4916169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4917169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4918169420Srrs 4919163953Srrs *offset = length; 4920163953Srrs return (NULL); 4921163953Srrs } 4922163953Srrs break; 4923163953Srrs case SCTP_COOKIE_ECHO: 4924169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4925170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4926163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4927163953Srrs ; 4928163953Srrs } else { 4929169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4930163953Srrs /* We are not interested anymore */ 4931208878Srrs abend: 4932208853Srrs if (stcb) { 4933208853Srrs SCTP_TCB_UNLOCK(stcb); 4934208853Srrs } 4935163953Srrs *offset = length; 4936163953Srrs return (NULL); 4937163953Srrs } 4938163953Srrs } 4939163953Srrs /* 4940163953Srrs * First are we accepting? We do this again here 4941207924Srrs * since it is possible that a previous endpoint WAS 4942207924Srrs * listening responded to a INIT-ACK and then 4943163953Srrs * closed. We opened and bound.. and are now no 4944163953Srrs * longer listening. 4945163953Srrs */ 4946172703Srrs 4947172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4948172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4949179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4950163953Srrs struct mbuf *oper; 4951163953Srrs struct sctp_paramhdr *phdr; 4952163953Srrs 4953172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4954172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4955172703Srrs if (oper) { 4956172703Srrs SCTP_BUF_LEN(oper) = 4957172703Srrs sizeof(struct sctp_paramhdr); 4958172703Srrs phdr = mtod(oper, 4959172703Srrs struct sctp_paramhdr *); 4960172703Srrs phdr->param_type = 4961172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4962172703Srrs phdr->param_length = 4963172703Srrs htons(sizeof(struct sctp_paramhdr)); 4964163953Srrs } 4965172703Srrs sctp_abort_association(inp, stcb, m, 4966179157Srrs iphlen, sh, oper, vrf_id, port); 4967163953Srrs } 4968172703Srrs *offset = length; 4969172703Srrs return (NULL); 4970172703Srrs } else { 4971163953Srrs struct mbuf *ret_buf; 4972165220Srrs struct sctp_inpcb *linp; 4973163953Srrs 4974169420Srrs if (stcb) { 4975165220Srrs linp = NULL; 4976169420Srrs } else { 4977165220Srrs linp = inp; 4978169420Srrs } 4979165220Srrs 4980169420Srrs if (linp) { 4981165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4982208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 4983208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 4984208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4985208878Srrs goto abend; 4986208878Srrs } 4987169420Srrs } 4988169420Srrs if (netp) { 4989169420Srrs ret_buf = 4990169420Srrs sctp_handle_cookie_echo(m, iphlen, 4991169420Srrs *offset, sh, 4992169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4993169420Srrs &inp, &stcb, netp, 4994169420Srrs auth_skipped, 4995169420Srrs auth_offset, 4996169420Srrs auth_len, 4997169420Srrs &locked_tcb, 4998179157Srrs vrf_id, 4999179157Srrs port); 5000169420Srrs } else { 5001169420Srrs ret_buf = NULL; 5002169420Srrs } 5003169420Srrs if (linp) { 5004165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5005169420Srrs } 5006163953Srrs if (ret_buf == NULL) { 5007163953Srrs if (locked_tcb) { 5008163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5009163953Srrs } 5010169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5011169420Srrs "GAK, null buffer\n"); 5012163953Srrs auth_skipped = 0; 5013163953Srrs *offset = length; 5014163953Srrs return (NULL); 5015163953Srrs } 5016163953Srrs /* if AUTH skipped, see if it verified... */ 5017163953Srrs if (auth_skipped) { 5018163953Srrs got_auth = 1; 5019163953Srrs auth_skipped = 0; 5020163953Srrs } 5021163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5022163953Srrs /* 5023163953Srrs * Restart the timer if we have 5024163953Srrs * pending data 5025163953Srrs */ 5026163953Srrs struct sctp_tmit_chunk *chk; 5027163953Srrs 5028163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5029216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5030163953Srrs } 5031163953Srrs } 5032163953Srrs break; 5033163953Srrs case SCTP_COOKIE_ACK: 5034170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5035169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5036169378Srrs if (locked_tcb) { 5037169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5038169378Srrs } 5039169352Srrs return (NULL); 5040169352Srrs } 5041163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5042163953Srrs /* We are not interested anymore */ 5043163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5044163953Srrs ; 5045169420Srrs } else if (stcb) { 5046172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5047172090Srrs so = SCTP_INP_SO(inp); 5048172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5049172090Srrs SCTP_TCB_UNLOCK(stcb); 5050172090Srrs SCTP_SOCKET_LOCK(so, 1); 5051172090Srrs SCTP_TCB_LOCK(stcb); 5052172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5053172090Srrs#endif 5054171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5055172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5056172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5057172090Srrs#endif 5058163953Srrs *offset = length; 5059163953Srrs return (NULL); 5060163953Srrs } 5061163953Srrs } 5062163953Srrs /* He's alive so give him credit */ 5063169420Srrs if ((stcb) && netp && *netp) { 5064179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5065171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5066171943Srrs stcb->asoc.overall_error_count, 5067171943Srrs 0, 5068171943Srrs SCTP_FROM_SCTP_INPUT, 5069171943Srrs __LINE__); 5070171943Srrs } 5071169378Srrs stcb->asoc.overall_error_count = 0; 5072169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5073169378Srrs } 5074163953Srrs break; 5075163953Srrs case SCTP_ECN_ECHO: 5076169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5077163953Srrs /* He's alive so give him credit */ 5078169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5079169208Srrs /* Its not ours */ 5080169378Srrs if (locked_tcb) { 5081169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5082169378Srrs } 5083169208Srrs *offset = length; 5084169208Srrs return (NULL); 5085169208Srrs } 5086169378Srrs if (stcb) { 5087179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5088171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5089171943Srrs stcb->asoc.overall_error_count, 5090171943Srrs 0, 5091171943Srrs SCTP_FROM_SCTP_INPUT, 5092171943Srrs __LINE__); 5093171943Srrs } 5094169378Srrs stcb->asoc.overall_error_count = 0; 5095169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5096169378Srrs stcb); 5097218186Srrs ecne_seen = 1; 5098169378Srrs } 5099163953Srrs break; 5100163953Srrs case SCTP_ECN_CWR: 5101169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5102163953Srrs /* He's alive so give him credit */ 5103169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5104169208Srrs /* Its not ours */ 5105169378Srrs if (locked_tcb) { 5106169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5107169378Srrs } 5108169208Srrs *offset = length; 5109169208Srrs return (NULL); 5110169208Srrs } 5111169378Srrs if (stcb) { 5112179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5113171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5114171943Srrs stcb->asoc.overall_error_count, 5115171943Srrs 0, 5116171943Srrs SCTP_FROM_SCTP_INPUT, 5117171943Srrs __LINE__); 5118171943Srrs } 5119169378Srrs stcb->asoc.overall_error_count = 0; 5120218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5121169378Srrs } 5122163953Srrs break; 5123163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5124170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5125163953Srrs /* must be first and only chunk */ 5126163953Srrs if ((num_chunks > 1) || 5127170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5128163953Srrs *offset = length; 5129169378Srrs if (locked_tcb) { 5130163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5131169378Srrs } 5132163953Srrs return (NULL); 5133163953Srrs } 5134169420Srrs if ((stcb) && netp && *netp) { 5135169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5136169378Srrs stcb, *netp); 5137169378Srrs } 5138163953Srrs *offset = length; 5139163953Srrs return (NULL); 5140163953Srrs break; 5141163953Srrs case SCTP_ASCONF: 5142169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5143163953Srrs /* He's alive so give him credit */ 5144169378Srrs if (stcb) { 5145179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5146171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5147171943Srrs stcb->asoc.overall_error_count, 5148171943Srrs 0, 5149171943Srrs SCTP_FROM_SCTP_INPUT, 5150171943Srrs __LINE__); 5151171943Srrs } 5152169378Srrs stcb->asoc.overall_error_count = 0; 5153169378Srrs sctp_handle_asconf(m, *offset, 5154171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5155171990Srrs asconf_cnt++; 5156169378Srrs } 5157163953Srrs break; 5158163953Srrs case SCTP_ASCONF_ACK: 5159169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5160169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5161169208Srrs /* Its not ours */ 5162169378Srrs if (locked_tcb) { 5163169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5164169378Srrs } 5165169208Srrs *offset = length; 5166169208Srrs return (NULL); 5167169208Srrs } 5168169420Srrs if ((stcb) && netp && *netp) { 5169169378Srrs /* He's alive so give him credit */ 5170179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5171171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5172171943Srrs stcb->asoc.overall_error_count, 5173171943Srrs 0, 5174171943Srrs SCTP_FROM_SCTP_INPUT, 5175171943Srrs __LINE__); 5176171943Srrs } 5177169378Srrs stcb->asoc.overall_error_count = 0; 5178169378Srrs sctp_handle_asconf_ack(m, *offset, 5179172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5180172190Srrs if (abort_no_unlock) 5181172190Srrs return (NULL); 5182169378Srrs } 5183163953Srrs break; 5184163953Srrs case SCTP_FORWARD_CUM_TSN: 5185169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5186169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5187169208Srrs /* Its not ours */ 5188169378Srrs if (locked_tcb) { 5189169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5190169378Srrs } 5191169208Srrs *offset = length; 5192169208Srrs return (NULL); 5193169208Srrs } 5194163953Srrs /* He's alive so give him credit */ 5195169378Srrs if (stcb) { 5196163953Srrs int abort_flag = 0; 5197163953Srrs 5198163953Srrs stcb->asoc.overall_error_count = 0; 5199179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5200171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5201171943Srrs stcb->asoc.overall_error_count, 5202171943Srrs 0, 5203171943Srrs SCTP_FROM_SCTP_INPUT, 5204171943Srrs __LINE__); 5205171943Srrs } 5206163953Srrs *fwd_tsn_seen = 1; 5207163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5208163953Srrs /* We are not interested anymore */ 5209172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5210172090Srrs so = SCTP_INP_SO(inp); 5211172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5212172090Srrs SCTP_TCB_UNLOCK(stcb); 5213172090Srrs SCTP_SOCKET_LOCK(so, 1); 5214172090Srrs SCTP_TCB_LOCK(stcb); 5215172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5216172090Srrs#endif 5217171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5218172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5219172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5220172090Srrs#endif 5221163953Srrs *offset = length; 5222163953Srrs return (NULL); 5223163953Srrs } 5224163953Srrs sctp_handle_forward_tsn(stcb, 5225170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5226163953Srrs if (abort_flag) { 5227163953Srrs *offset = length; 5228163953Srrs return (NULL); 5229163953Srrs } else { 5230179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5231171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5232171943Srrs stcb->asoc.overall_error_count, 5233171943Srrs 0, 5234171943Srrs SCTP_FROM_SCTP_INPUT, 5235171943Srrs __LINE__); 5236171943Srrs } 5237163953Srrs stcb->asoc.overall_error_count = 0; 5238163953Srrs } 5239163953Srrs 5240163953Srrs } 5241163953Srrs break; 5242163953Srrs case SCTP_STREAM_RESET: 5243169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5244169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5245169208Srrs /* Its not ours */ 5246169378Srrs if (locked_tcb) { 5247169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5248169378Srrs } 5249169208Srrs *offset = length; 5250169208Srrs return (NULL); 5251169208Srrs } 5252163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5253163953Srrs /* We are not interested anymore */ 5254172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5255172090Srrs so = SCTP_INP_SO(inp); 5256172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5257172090Srrs SCTP_TCB_UNLOCK(stcb); 5258172090Srrs SCTP_SOCKET_LOCK(so, 1); 5259172090Srrs SCTP_TCB_LOCK(stcb); 5260172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5261172090Srrs#endif 5262171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5263172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5264172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5265172090Srrs#endif 5266163953Srrs *offset = length; 5267163953Srrs return (NULL); 5268163953Srrs } 5269169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5270169420Srrs /* 5271169420Srrs * hmm, peer should have announced this, but 5272169420Srrs * we will turn it on since he is sending us 5273169420Srrs * a stream reset. 5274169420Srrs */ 5275169420Srrs stcb->asoc.peer_supports_strreset = 1; 5276163953Srrs } 5277170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5278169420Srrs /* stop processing */ 5279169420Srrs *offset = length; 5280169420Srrs return (NULL); 5281169420Srrs } 5282163953Srrs break; 5283163953Srrs case SCTP_PACKET_DROPPED: 5284169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5285163953Srrs /* re-get it all please */ 5286169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5287169208Srrs /* Its not ours */ 5288169378Srrs if (locked_tcb) { 5289169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5290169378Srrs } 5291169208Srrs *offset = length; 5292169208Srrs return (NULL); 5293169208Srrs } 5294169420Srrs if (ch && (stcb) && netp && (*netp)) { 5295169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5296170781Srrs stcb, *netp, 5297170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5298170781Srrs 5299169378Srrs } 5300163953Srrs break; 5301163953Srrs 5302163953Srrs case SCTP_AUTHENTICATION: 5303169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5304179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5305163953Srrs goto unknown_chunk; 5306163953Srrs 5307163953Srrs if (stcb == NULL) { 5308163953Srrs /* save the first AUTH for later processing */ 5309163953Srrs if (auth_skipped == 0) { 5310163953Srrs auth_offset = *offset; 5311163953Srrs auth_len = chk_length; 5312163953Srrs auth_skipped = 1; 5313163953Srrs } 5314163953Srrs /* skip this chunk (temporarily) */ 5315163953Srrs goto next_chunk; 5316163953Srrs } 5317169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5318169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5319169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5320169208Srrs /* Its not ours */ 5321169378Srrs if (locked_tcb) { 5322169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5323169378Srrs } 5324169208Srrs *offset = length; 5325169208Srrs return (NULL); 5326169208Srrs } 5327163953Srrs if (got_auth == 1) { 5328163953Srrs /* skip this chunk... it's already auth'd */ 5329163953Srrs goto next_chunk; 5330163953Srrs } 5331163953Srrs got_auth = 1; 5332169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5333163953Srrs m, *offset)) { 5334163953Srrs /* auth HMAC failed so dump the packet */ 5335163953Srrs *offset = length; 5336163953Srrs return (stcb); 5337163953Srrs } else { 5338163953Srrs /* remaining chunks are HMAC checked */ 5339163953Srrs stcb->asoc.authenticated = 1; 5340163953Srrs } 5341163953Srrs break; 5342163953Srrs 5343163953Srrs default: 5344163953Srrs unknown_chunk: 5345163953Srrs /* it's an unknown chunk! */ 5346163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5347163953Srrs struct mbuf *mm; 5348163953Srrs struct sctp_paramhdr *phd; 5349163953Srrs 5350163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5351165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5352163953Srrs if (mm) { 5353163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5354163953Srrs /* 5355163953Srrs * We cheat and use param type since 5356163953Srrs * we did not bother to define a 5357163953Srrs * error cause struct. They are the 5358163953Srrs * same basic format with different 5359163953Srrs * names. 5360163953Srrs */ 5361163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5362163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5363165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5364166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5365163953Srrs M_DONTWAIT); 5366165647Srrs if (SCTP_BUF_NEXT(mm)) { 5367178202Srrs#ifdef SCTP_MBUF_LOGGING 5368179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5369178202Srrs struct mbuf *mat; 5370178202Srrs 5371178202Srrs mat = SCTP_BUF_NEXT(mm); 5372178202Srrs while (mat) { 5373178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5374178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5375178202Srrs } 5376178202Srrs mat = SCTP_BUF_NEXT(mat); 5377178202Srrs } 5378178202Srrs } 5379178202Srrs#endif 5380163953Srrs sctp_queue_op_err(stcb, mm); 5381163953Srrs } else { 5382163953Srrs sctp_m_freem(mm); 5383163953Srrs } 5384163953Srrs } 5385163953Srrs } 5386163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5387163953Srrs /* discard this packet */ 5388163953Srrs *offset = length; 5389163953Srrs return (stcb); 5390163953Srrs } /* else skip this bad chunk and continue... */ 5391163953Srrs break; 5392163953Srrs } /* switch (ch->chunk_type) */ 5393163953Srrs 5394163953Srrs 5395163953Srrsnext_chunk: 5396163953Srrs /* get the next chunk */ 5397163953Srrs *offset += SCTP_SIZE32(chk_length); 5398163953Srrs if (*offset >= length) { 5399163953Srrs /* no more data left in the mbuf chain */ 5400163953Srrs break; 5401163953Srrs } 5402163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5403163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5404163953Srrs if (ch == NULL) { 5405169378Srrs if (locked_tcb) { 5406163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5407169378Srrs } 5408163953Srrs *offset = length; 5409163953Srrs return (NULL); 5410163953Srrs } 5411163953Srrs } /* while */ 5412171990Srrs 5413171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5414171990Srrs sctp_send_asconf_ack(stcb); 5415171990Srrs } 5416163953Srrs return (stcb); 5417163953Srrs} 5418163953Srrs 5419163953Srrs 5420171440Srrs#ifdef INVARIANTS 5421208853Srrs#ifdef __GNUC__ 5422208853Srrs__attribute__((noinline)) 5423208853Srrs#endif 5424208853Srrs void 5425208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5426171440Srrs{ 5427208853Srrs struct sctp_tcb *lstcb; 5428171440Srrs 5429208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5430208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5431171440Srrs panic("Own lock on stcb at return from input"); 5432171440Srrs } 5433171440Srrs } 5434208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5435208876Srrs panic("Own create lock on inp"); 5436208876Srrs } 5437208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5438208876Srrs panic("Own inp lock on inp"); 5439208876Srrs } 5440171440Srrs} 5441171440Srrs 5442171440Srrs#endif 5443171440Srrs 5444163953Srrs/* 5445163953Srrs * common input chunk processing (v4 and v6) 5446163953Srrs */ 5447169378Srrsvoid 5448163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5449163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5450163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5451179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5452163953Srrs{ 5453163953Srrs /* 5454163953Srrs * Control chunk processing 5455163953Srrs */ 5456163953Srrs uint32_t high_tsn; 5457163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5458163953Srrs struct mbuf *m = *mm; 5459163953Srrs int abort_flag = 0; 5460163953Srrs int un_sent; 5461218129Srrs int cnt_ctrl_ready = 0; 5462163953Srrs 5463163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5464163953Srrs#ifdef SCTP_AUDITING_ENABLED 5465163953Srrs sctp_audit_log(0xE0, 1); 5466163953Srrs sctp_auditing(0, inp, stcb, net); 5467163953Srrs#endif 5468163953Srrs 5469179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5470179783Srrs m, iphlen, offset, length, stcb); 5471163953Srrs if (stcb) { 5472163953Srrs /* always clear this before beginning a packet */ 5473163953Srrs stcb->asoc.authenticated = 0; 5474163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5475171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5476171990Srrs stcb, stcb->asoc.state); 5477171990Srrs 5478171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5479171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5480171745Srrs /*- 5481171745Srrs * If we hit here, we had a ref count 5482171745Srrs * up when the assoc was aborted and the 5483171745Srrs * timer is clearing out the assoc, we should 5484171745Srrs * NOT respond to any packet.. its OOTB. 5485171745Srrs */ 5486171745Srrs SCTP_TCB_UNLOCK(stcb); 5487171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5488179157Srrs vrf_id, port); 5489171745Srrs goto out_now; 5490171745Srrs } 5491163953Srrs } 5492163953Srrs if (IS_SCTP_CONTROL(ch)) { 5493163953Srrs /* process the control portion of the SCTP packet */ 5494169655Srrs /* sa_ignore NO_NULL_CHK */ 5495163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5496179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5497163953Srrs if (stcb) { 5498163953Srrs /* 5499163953Srrs * This covers us if the cookie-echo was there and 5500163953Srrs * it changes our INP. 5501163953Srrs */ 5502163953Srrs inp = stcb->sctp_ep; 5503179783Srrs if ((net) && (port)) { 5504179783Srrs if (net->port == 0) { 5505179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5506179783Srrs } 5507179783Srrs net->port = port; 5508179783Srrs } 5509163953Srrs } 5510163953Srrs } else { 5511163953Srrs /* 5512163953Srrs * no control chunks, so pre-process DATA chunks (these 5513163953Srrs * checks are taken care of by control processing) 5514163953Srrs */ 5515163953Srrs 5516163953Srrs /* 5517163953Srrs * if DATA only packet, and auth is required, then punt... 5518163953Srrs * can't have authenticated without any AUTH (control) 5519163953Srrs * chunks 5520163953Srrs */ 5521179783Srrs if ((stcb != NULL) && 5522179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5523179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5524163953Srrs /* "silently" ignore */ 5525163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5526163953Srrs SCTP_TCB_UNLOCK(stcb); 5527171440Srrs goto out_now; 5528163953Srrs } 5529163953Srrs if (stcb == NULL) { 5530163953Srrs /* out of the blue DATA chunk */ 5531169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5532179157Srrs vrf_id, port); 5533171440Srrs goto out_now; 5534163953Srrs } 5535163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5536163953Srrs /* v_tag mismatch! */ 5537163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5538163953Srrs SCTP_TCB_UNLOCK(stcb); 5539171440Srrs goto out_now; 5540163953Srrs } 5541163953Srrs } 5542163953Srrs 5543163953Srrs if (stcb == NULL) { 5544163953Srrs /* 5545163953Srrs * no valid TCB for this packet, or we found it's a bad 5546163953Srrs * packet while processing control, or we're done with this 5547163953Srrs * packet (done or skip rest of data), so we drop it... 5548163953Srrs */ 5549171440Srrs goto out_now; 5550163953Srrs } 5551163953Srrs /* 5552163953Srrs * DATA chunk processing 5553163953Srrs */ 5554163953Srrs /* plow through the data chunks while length > offset */ 5555163953Srrs 5556163953Srrs /* 5557163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5558163953Srrs * DATA is required. 5559163953Srrs */ 5560179783Srrs if ((length > offset) && 5561179783Srrs (stcb != NULL) && 5562179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5563179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5564163953Srrs !stcb->asoc.authenticated) { 5565163953Srrs /* "silently" ignore */ 5566163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5567169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5568169420Srrs "Data chunk requires AUTH, skipped\n"); 5569165220Srrs goto trigger_send; 5570163953Srrs } 5571163953Srrs if (length > offset) { 5572163953Srrs int retval; 5573163953Srrs 5574163953Srrs /* 5575163953Srrs * First check to make sure our state is correct. We would 5576163953Srrs * not get here unless we really did have a tag, so we don't 5577163953Srrs * abort if this happens, just dump the chunk silently. 5578163953Srrs */ 5579163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5580163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5581163953Srrs /* 5582163953Srrs * we consider data with valid tags in this state 5583163953Srrs * shows us the cookie-ack was lost. Imply it was 5584163953Srrs * there. 5585163953Srrs */ 5586179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5587171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5588171943Srrs stcb->asoc.overall_error_count, 5589171943Srrs 0, 5590171943Srrs SCTP_FROM_SCTP_INPUT, 5591171943Srrs __LINE__); 5592171943Srrs } 5593163953Srrs stcb->asoc.overall_error_count = 0; 5594163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5595163953Srrs break; 5596163953Srrs case SCTP_STATE_COOKIE_WAIT: 5597163953Srrs /* 5598163953Srrs * We consider OOTB any data sent during asoc setup. 5599163953Srrs */ 5600169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5601179157Srrs vrf_id, port); 5602163953Srrs SCTP_TCB_UNLOCK(stcb); 5603171440Srrs goto out_now; 5604171531Srrs /* sa_ignore NOTREACHED */ 5605163953Srrs break; 5606163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5607163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5608163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5609163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5610163953Srrs default: 5611163953Srrs SCTP_TCB_UNLOCK(stcb); 5612171440Srrs goto out_now; 5613171531Srrs /* sa_ignore NOTREACHED */ 5614163953Srrs break; 5615163953Srrs case SCTP_STATE_OPEN: 5616163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5617163953Srrs break; 5618163953Srrs } 5619163953Srrs /* plow through the data chunks while length > offset */ 5620163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5621163953Srrs inp, stcb, net, &high_tsn); 5622163953Srrs if (retval == 2) { 5623163953Srrs /* 5624163953Srrs * The association aborted, NO UNLOCK needed since 5625163953Srrs * the association is destroyed. 5626163953Srrs */ 5627171440Srrs goto out_now; 5628163953Srrs } 5629163953Srrs data_processed = 1; 5630163953Srrs /* 5631163953Srrs * Anything important needs to have been m_copy'ed in 5632163953Srrs * process_data 5633163953Srrs */ 5634163953Srrs } 5635218129Srrs /* take care of ecn */ 5636218235Stuexen if ((stcb->asoc.ecn_allowed == 1) && 5637218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5638218129Srrs /* Yep, we need to add a ECNE */ 5639218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5640218129Srrs } 5641163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5642216495Stuexen int was_a_gap; 5643216495Stuexen uint32_t highest_tsn; 5644163953Srrs 5645216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5646216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5647216495Stuexen } else { 5648216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5649163953Srrs } 5650216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5651190689Srrs stcb->asoc.send_sack = 1; 5652206137Stuexen sctp_sack_check(stcb, was_a_gap, &abort_flag); 5653163953Srrs if (abort_flag) { 5654163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5655171440Srrs goto out_now; 5656163953Srrs } 5657190689Srrs } else if (fwd_tsn_seen) { 5658190689Srrs stcb->asoc.send_sack = 1; 5659163953Srrs } 5660163953Srrs /* trigger send of any chunks in queue... */ 5661165220Srrstrigger_send: 5662163953Srrs#ifdef SCTP_AUDITING_ENABLED 5663163953Srrs sctp_audit_log(0xE0, 2); 5664163953Srrs sctp_auditing(1, inp, stcb, net); 5665163953Srrs#endif 5666169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5667169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5668169420Srrs stcb->asoc.peers_rwnd, 5669169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5670169420Srrs stcb->asoc.total_flight); 5671163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5672218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5673218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5674218129Srrs } 5675218129Srrs if (cnt_ctrl_ready || 5676163953Srrs ((un_sent) && 5677163953Srrs (stcb->asoc.peers_rwnd > 0 || 5678163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5679169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5680172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5681169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5682163953Srrs } 5683163953Srrs#ifdef SCTP_AUDITING_ENABLED 5684163953Srrs sctp_audit_log(0xE0, 3); 5685163953Srrs sctp_auditing(2, inp, stcb, net); 5686163953Srrs#endif 5687163953Srrs SCTP_TCB_UNLOCK(stcb); 5688171440Srrsout_now: 5689171440Srrs#ifdef INVARIANTS 5690171440Srrs sctp_validate_no_locks(inp); 5691171440Srrs#endif 5692169378Srrs return; 5693163953Srrs} 5694163953Srrs 5695185694Srrs#if 0 5696185694Srrsstatic void 5697185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5698185694Srrs{ 5699185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5700185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5701185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5702185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5703185694Srrs } 5704185694Srrs} 5705163953Srrs 5706185694Srrs#endif 5707185694Srrs 5708163953Srrsvoid 5709189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5710163953Srrs{ 5711163953Srrs#ifdef SCTP_MBUF_LOGGING 5712163953Srrs struct mbuf *mat; 5713163953Srrs 5714163953Srrs#endif 5715165647Srrs struct mbuf *m; 5716163953Srrs int iphlen; 5717170181Srrs uint32_t vrf_id = 0; 5718163953Srrs uint8_t ecn_bits; 5719163953Srrs struct ip *ip; 5720163953Srrs struct sctphdr *sh; 5721163953Srrs struct sctp_inpcb *inp = NULL; 5722163953Srrs struct sctp_nets *net; 5723163953Srrs struct sctp_tcb *stcb = NULL; 5724163953Srrs struct sctp_chunkhdr *ch; 5725163953Srrs int refcount_up = 0; 5726163953Srrs int length, mlen, offset; 5727163953Srrs 5728211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5729211969Stuexen uint32_t check, calc_check; 5730211969Stuexen 5731211969Stuexen#endif 5732211969Stuexen 5733169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5734169352Srrs SCTP_RELEASE_PKT(i_pak); 5735169352Srrs return; 5736169352Srrs } 5737168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5738163953Srrs iphlen = off; 5739165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5740170056Srrs 5741163953Srrs net = NULL; 5742163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5743163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5744163953Srrs 5745170091Srrs 5746163953Srrs#ifdef SCTP_MBUF_LOGGING 5747163953Srrs /* Log in any input mbufs */ 5748179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5749170744Srrs mat = m; 5750170744Srrs while (mat) { 5751170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5752170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5753170744Srrs } 5754170744Srrs mat = SCTP_BUF_NEXT(mat); 5755163953Srrs } 5756163953Srrs } 5757163953Srrs#endif 5758170091Srrs#ifdef SCTP_PACKET_LOGGING 5759179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5760170921Srrs sctp_packet_log(m, mlen); 5761170091Srrs#endif 5762170091Srrs /* 5763170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5764170091Srrs * case) 5765170091Srrs */ 5766170091Srrs mlen -= iphlen; 5767168124Srrs 5768163953Srrs /* 5769163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5770163953Srrs */ 5771163953Srrs ip = mtod(m, struct ip *); 5772163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5773165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5774163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5775163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5776163953Srrs return; 5777163953Srrs } 5778163953Srrs ip = mtod(m, struct ip *); 5779163953Srrs } 5780188067Srrs /* validate mbuf chain length with IP payload length */ 5781188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5782188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5783188067Srrs goto bad; 5784188067Srrs } 5785163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5786163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5787170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5788170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5789163953Srrs 5790163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5791163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5792163953Srrs goto bad; 5793163953Srrs } 5794168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5795163953Srrs /* 5796163953Srrs * We only look at broadcast if its a front state, All 5797163953Srrs * others we will not have a tcb for anyway. 5798163953Srrs */ 5799163953Srrs goto bad; 5800163953Srrs } 5801163953Srrs /* validate SCTP checksum */ 5802188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5803188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5804188067Srrs m->m_pkthdr.len, 5805188067Srrs if_name(m->m_pkthdr.rcvif), 5806188067Srrs m->m_pkthdr.csum_flags); 5807211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5808211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5809211969Stuexen#else 5810188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5811188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5812188067Srrs goto sctp_skip_csum_4; 5813188067Srrs } 5814171990Srrs check = sh->checksum; /* save incoming checksum */ 5815171990Srrs sh->checksum = 0; /* prepare for calc */ 5816188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5817203503Stuexen sh->checksum = check; 5818188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5819171990Srrs if (calc_check != check) { 5820171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5821171990Srrs calc_check, check, m, mlen, iphlen); 5822163953Srrs 5823171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5824171990Srrs offset - sizeof(*ch), 5825171990Srrs sh, ch, &inp, &net, 5826171990Srrs vrf_id); 5827179157Srrs if ((net) && (port)) { 5828179157Srrs if (net->port == 0) { 5829179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5830179157Srrs } 5831179157Srrs net->port = port; 5832179157Srrs } 5833218400Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5834218400Stuexen net->flowid = m->m_pkthdr.flowid; 5835218400Stuexen#ifdef INVARIANTS 5836218400Stuexen net->flowidset = 1; 5837218400Stuexen#endif 5838218400Stuexen } 5839171990Srrs if ((inp) && (stcb)) { 5840171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5841172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5842171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5843171990Srrs refcount_up = 1; 5844163953Srrs } 5845171990Srrs SCTP_STAT_INCR(sctps_badsum); 5846171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5847171990Srrs goto bad; 5848168299Srrs } 5849163953Srrssctp_skip_csum_4: 5850211969Stuexen#endif 5851168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5852168124Srrs if (sh->dest_port == 0) { 5853168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5854168124Srrs goto bad; 5855168124Srrs } 5856163953Srrs /* 5857163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5858163953Srrs * IP/SCTP/first chunk header... 5859163953Srrs */ 5860163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5861168299Srrs sh, ch, &inp, &net, vrf_id); 5862179157Srrs if ((net) && (port)) { 5863179157Srrs if (net->port == 0) { 5864179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5865179157Srrs } 5866179157Srrs net->port = port; 5867179157Srrs } 5868218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5869218335Stuexen net->flowid = m->m_pkthdr.flowid; 5870218400Stuexen#ifdef INVARIANTS 5871218392Srrs net->flowidset = 1; 5872218400Stuexen#endif 5873218335Stuexen } 5874163953Srrs /* inp's ref-count increased && stcb locked */ 5875163953Srrs if (inp == NULL) { 5876163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5877163953Srrs 5878163953Srrs SCTP_STAT_INCR(sctps_noport); 5879163953Srrs#ifdef ICMP_BANDLIM 5880163953Srrs /* 5881163953Srrs * we use the bandwidth limiting to protect against sending 5882163953Srrs * too many ABORTS all at once. In this case these count the 5883163953Srrs * same as an ICMP message. 5884163953Srrs */ 5885163953Srrs if (badport_bandlim(0) < 0) 5886163953Srrs goto bad; 5887163953Srrs#endif /* ICMP_BANDLIM */ 5888169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5889169420Srrs "Sending a ABORT from packet entry!\n"); 5890163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5891163953Srrs /* 5892163953Srrs * we do a trick here to get the INIT tag, dig in 5893163953Srrs * and get the tag from the INIT and put it in the 5894163953Srrs * common header. 5895163953Srrs */ 5896163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5897163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5898163953Srrs (uint8_t *) & chunk_buf); 5899163953Srrs if (init_chk != NULL) 5900163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5901163953Srrs } 5902165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5903179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5904165220Srrs goto bad; 5905165220Srrs } 5906165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5907165220Srrs goto bad; 5908165220Srrs } 5909165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5910179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5911163953Srrs goto bad; 5912163953Srrs } else if (stcb == NULL) { 5913163953Srrs refcount_up = 1; 5914163953Srrs } 5915171167Sgnn#ifdef IPSEC 5916163953Srrs /* 5917163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5918163953Srrs * idea, so I will leave it in place. 5919163953Srrs */ 5920163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5921197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5922163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5923163953Srrs goto bad; 5924163953Srrs } 5925163953Srrs#endif /* IPSEC */ 5926163953Srrs 5927163953Srrs /* 5928163953Srrs * common chunk processing 5929163953Srrs */ 5930163953Srrs length = ip->ip_len + iphlen; 5931163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5932163953Srrs 5933163953Srrs ecn_bits = ip->ip_tos; 5934163953Srrs 5935169655Srrs /* sa_ignore NO_NULL_CHK */ 5936163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5937179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5938163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5939163953Srrs if (m) { 5940163953Srrs sctp_m_freem(m); 5941163953Srrs } 5942163953Srrs if ((inp) && (refcount_up)) { 5943163953Srrs /* reduce ref-count */ 5944163953Srrs SCTP_INP_DECR_REF(inp); 5945163953Srrs } 5946163953Srrs return; 5947163953Srrsbad: 5948169420Srrs if (stcb) { 5949163953Srrs SCTP_TCB_UNLOCK(stcb); 5950169420Srrs } 5951163953Srrs if ((inp) && (refcount_up)) { 5952163953Srrs /* reduce ref-count */ 5953163953Srrs SCTP_INP_DECR_REF(inp); 5954163953Srrs } 5955163953Srrs if (m) { 5956163953Srrs sctp_m_freem(m); 5957163953Srrs } 5958163953Srrs return; 5959163953Srrs} 5960218211Srrs 5961218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5962218269Srrsextern int *sctp_cpuarry; 5963218211Srrs 5964218269Srrs#endif 5965218269Srrs 5966179157Srrsvoid 5967218211Srrssctp_input(struct mbuf *m, int off) 5968179157Srrs{ 5969218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5970218211Srrs struct ip *ip; 5971218211Srrs struct sctphdr *sh; 5972218211Srrs int offset; 5973218211Srrs int cpu_to_use; 5974218371Srrs uint32_t flowid, tag; 5975218211Srrs 5976218269Srrs if (mp_ncpus > 1) { 5977218371Srrs if (m->m_flags & M_FLOWID) { 5978218371Srrs flowid = m->m_pkthdr.flowid; 5979218269Srrs } else { 5980218269Srrs /* 5981218371Srrs * No flow id built by lower layers fix it so we 5982218371Srrs * create one. 5983218269Srrs */ 5984218371Srrs ip = mtod(m, struct ip *); 5985218371Srrs offset = off + sizeof(*sh); 5986218371Srrs if (SCTP_BUF_LEN(m) < offset) { 5987218371Srrs if ((m = m_pullup(m, offset)) == 0) { 5988218371Srrs SCTP_STAT_INCR(sctps_hdrops); 5989218371Srrs return; 5990218371Srrs } 5991218371Srrs ip = mtod(m, struct ip *); 5992218371Srrs } 5993218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 5994218371Srrs tag = htonl(sh->v_tag); 5995218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 5996218371Srrs m->m_pkthdr.flowid = flowid; 5997218371Srrs m->m_flags |= M_FLOWID; 5998218269Srrs } 5999218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6000218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 6001218211Srrs return; 6002218211Srrs } 6003218211Srrs#endif 6004218211Srrs sctp_input_with_port(m, off, 0); 6005179157Srrs} 6006