sctp_input.c revision 235418
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, 10228653Stuexen * 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 14228653Stuexen * 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 235418 2012-05-13 22:27:54Z tuexen $"); 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, 87228653Stuexen int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 88163953Srrs{ 89163953Srrs struct sctp_init *init; 90163953Srrs struct mbuf *op_err; 91163953Srrs 92169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 93169420Srrs stcb); 94172396Srrs if (stcb == NULL) { 95172396Srrs SCTP_INP_RLOCK(inp); 96172396Srrs } 97229774Stuexen /* validate length */ 98163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 99163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 100169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 101179157Srrs vrf_id, port); 102168299Srrs if (stcb) 103168299Srrs *abort_no_unlock = 1; 104172396Srrs goto outnow; 105163953Srrs } 106163953Srrs /* validate parameters */ 107229774Stuexen init = &cp->init; 108163953Srrs if (init->initiate_tag == 0) { 109163953Srrs /* protocol error... send abort */ 110163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 111169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 112179157Srrs vrf_id, port); 113168299Srrs if (stcb) 114168299Srrs *abort_no_unlock = 1; 115172396Srrs goto outnow; 116163953Srrs } 117163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 118163953Srrs /* invalid parameter... send abort */ 119163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 120169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121179157Srrs vrf_id, port); 122171440Srrs if (stcb) 123171440Srrs *abort_no_unlock = 1; 124172396Srrs goto outnow; 125163953Srrs } 126163953Srrs if (init->num_inbound_streams == 0) { 127163953Srrs /* protocol error... send abort */ 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 if (init->num_outbound_streams == 0) { 136163953Srrs /* protocol error... send abort */ 137163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 138169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 139179157Srrs vrf_id, port); 140168299Srrs if (stcb) 141168299Srrs *abort_no_unlock = 1; 142172396Srrs goto outnow; 143163953Srrs } 144163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 145229774Stuexen offset + ntohs(cp->ch.chunk_length))) { 146163953Srrs /* auth parameter(s) error... send abort */ 147179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 148168299Srrs if (stcb) 149168299Srrs *abort_no_unlock = 1; 150172396Srrs goto outnow; 151163953Srrs } 152229774Stuexen /* 153229774Stuexen * We are only accepting if we have a socket with positive 154229774Stuexen * so_qlimit. 155229774Stuexen */ 156229774Stuexen if ((stcb == NULL) && 157229774Stuexen ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 158229774Stuexen (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 159229774Stuexen (inp->sctp_socket == NULL) || 160229774Stuexen (inp->sctp_socket->so_qlimit == 0))) { 161229774Stuexen /* 162229774Stuexen * FIX ME ?? What about TCP model and we have a 163229774Stuexen * match/restart case? Actually no fix is needed. the lookup 164229774Stuexen * will always find the existing assoc so stcb would not be 165229774Stuexen * NULL. It may be questionable to do this since we COULD 166229774Stuexen * just send back the INIT-ACK and hope that the app did 167229774Stuexen * accept()'s by the time the COOKIE was sent. But there is 168229774Stuexen * a price to pay for COOKIE generation and I don't want to 169229774Stuexen * pay it on the chance that the app will actually do some 170229774Stuexen * accepts(). The App just looses and should NOT be in this 171229774Stuexen * state :-) 172229774Stuexen */ 173229805Stuexen if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { 174229805Stuexen sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 175229805Stuexen } 176229774Stuexen goto outnow; 177229774Stuexen } 178229774Stuexen if ((stcb != NULL) && 179229774Stuexen (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { 180229774Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); 181229774Stuexen sctp_send_shutdown_ack(stcb, NULL); 182229774Stuexen sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 183229774Stuexen } else { 184229774Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 185229774Stuexen sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 186229774Stuexen ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 187229774Stuexen } 188172396Srrsoutnow: 189172396Srrs if (stcb == NULL) { 190172396Srrs SCTP_INP_RUNLOCK(inp); 191172396Srrs } 192163953Srrs} 193163953Srrs 194163953Srrs/* 195163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 196163953Srrs */ 197171158Srrs 198171158Srrsint 199221627Stuexensctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked 200221627Stuexen#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 201221627Stuexen SCTP_UNUSED 202221627Stuexen#endif 203221627Stuexen) 204171158Srrs{ 205171158Srrs int unsent_data = 0; 206217760Stuexen unsigned int i; 207217760Stuexen struct sctp_stream_queue_pending *sp; 208171158Srrs struct sctp_association *asoc; 209171158Srrs 210171158Srrs /* 211171158Srrs * This function returns the number of streams that have true unsent 212171158Srrs * data on them. Note that as it looks through it will clean up any 213171158Srrs * places that have old data that has been sent but left at top of 214171158Srrs * stream queue. 215171158Srrs */ 216171158Srrs asoc = &stcb->asoc; 217171158Srrs SCTP_TCB_SEND_LOCK(stcb); 218217760Stuexen if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 219217760Stuexen /* Check to see if some data queued */ 220217760Stuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 221217760Stuexen /* sa_ignore FREED_MEMORY */ 222217760Stuexen sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 223217760Stuexen if (sp == NULL) { 224217760Stuexen continue; 225217760Stuexen } 226171158Srrs if ((sp->msg_is_complete) && 227171158Srrs (sp->length == 0) && 228171158Srrs (sp->sender_all_done)) { 229171158Srrs /* 230171158Srrs * We are doing differed cleanup. Last time 231171158Srrs * through when we took all the data the 232171158Srrs * sender_all_done was not set. 233171158Srrs */ 234171158Srrs if (sp->put_last_out == 0) { 235171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 236171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 237171158Srrs sp->sender_all_done, 238171158Srrs sp->length, 239171158Srrs sp->msg_is_complete, 240171158Srrs sp->put_last_out); 241171158Srrs } 242217760Stuexen atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 243217760Stuexen TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 244212712Stuexen if (sp->net) { 245212712Stuexen sctp_free_remote_addr(sp->net); 246212712Stuexen sp->net = NULL; 247212712Stuexen } 248171158Srrs if (sp->data) { 249171158Srrs sctp_m_freem(sp->data); 250171158Srrs sp->data = NULL; 251171158Srrs } 252221627Stuexen sctp_free_a_strmoq(stcb, sp, so_locked); 253171158Srrs } else { 254171158Srrs unsent_data++; 255216822Stuexen break; 256171158Srrs } 257171158Srrs } 258171158Srrs } 259171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 260171158Srrs return (unsent_data); 261171158Srrs} 262171158Srrs 263163953Srrsstatic int 264228653Stuexensctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) 265163953Srrs{ 266163953Srrs struct sctp_init *init; 267163953Srrs struct sctp_association *asoc; 268163953Srrs struct sctp_nets *lnet; 269163953Srrs unsigned int i; 270163953Srrs 271163953Srrs init = &cp->init; 272163953Srrs asoc = &stcb->asoc; 273163953Srrs /* save off parameters */ 274163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 275163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 276218129Srrs /* init tsn's */ 277218129Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 278218129Srrs 279212712Stuexen if (!TAILQ_EMPTY(&asoc->nets)) { 280163953Srrs /* update any ssthresh's that may have a default */ 281163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 282163953Srrs lnet->ssthresh = asoc->peers_rwnd; 283179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 284170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 285170744Srrs } 286163953Srrs } 287163953Srrs } 288164181Srrs SCTP_TCB_SEND_LOCK(stcb); 289163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 290163953Srrs unsigned int newcnt; 291163953Srrs struct sctp_stream_out *outs; 292216822Stuexen struct sctp_stream_queue_pending *sp, *nsp; 293216822Stuexen struct sctp_tmit_chunk *chk, *nchk; 294163953Srrs 295196260Stuexen /* abandon the upper streams */ 296163953Srrs newcnt = ntohs(init->num_inbound_streams); 297216822Stuexen TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 298216822Stuexen if (chk->rec.data.stream_number >= newcnt) { 299216822Stuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 300216822Stuexen asoc->send_queue_cnt--; 301216822Stuexen if (chk->data != NULL) { 302216822Stuexen sctp_free_bufspace(stcb, asoc, chk, 1); 303235416Stuexen sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 304235416Stuexen 0, chk, SCTP_SO_NOT_LOCKED); 305216822Stuexen if (chk->data) { 306216822Stuexen sctp_m_freem(chk->data); 307216822Stuexen chk->data = NULL; 308196260Stuexen } 309196260Stuexen } 310221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 311216822Stuexen /* sa_ignore FREED_MEMORY */ 312196260Stuexen } 313196260Stuexen } 314163953Srrs if (asoc->strmout) { 315163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 316163953Srrs outs = &asoc->strmout[i]; 317216822Stuexen TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 318196260Stuexen TAILQ_REMOVE(&outs->outqueue, sp, next); 319163953Srrs asoc->stream_queue_cnt--; 320163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 321235416Stuexen stcb, 0, sp, SCTP_SO_NOT_LOCKED); 322163953Srrs if (sp->data) { 323163953Srrs sctp_m_freem(sp->data); 324163953Srrs sp->data = NULL; 325163953Srrs } 326212712Stuexen if (sp->net) { 327212712Stuexen sctp_free_remote_addr(sp->net); 328212712Stuexen sp->net = NULL; 329212712Stuexen } 330163953Srrs /* Free the chunk */ 331221627Stuexen sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 332169655Srrs /* sa_ignore FREED_MEMORY */ 333163953Srrs } 334163953Srrs } 335163953Srrs } 336196260Stuexen /* cut back the count */ 337163953Srrs asoc->pre_open_streams = newcnt; 338163953Srrs } 339164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 340188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 341218129Srrs 342185694Srrs /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 343185694Srrs asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 344179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 345170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 346170744Srrs } 347163953Srrs /* This is the next one we expect */ 348163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 349163953Srrs 350163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 351180955Srrs asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 352218129Srrs 353163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 354163953Srrs /* open the requested streams */ 355170091Srrs 356163953Srrs if (asoc->strmin != NULL) { 357163953Srrs /* Free the old ones */ 358216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 359164181Srrs 360164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 361216822Stuexen TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 362164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 363164181Srrs sctp_free_remote_addr(ctl->whoFrom); 364171158Srrs ctl->whoFrom = NULL; 365164181Srrs sctp_m_freem(ctl->data); 366164181Srrs ctl->data = NULL; 367164181Srrs sctp_free_a_readq(stcb, ctl); 368164181Srrs } 369164181Srrs } 370170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 371163953Srrs } 372164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 373164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 374164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 375164181Srrs } 376163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 377170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 378163953Srrs if (asoc->strmin == NULL) { 379163953Srrs /* we didn't get memory for the streams! */ 380169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 381163953Srrs return (-1); 382163953Srrs } 383163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 384163953Srrs asoc->strmin[i].stream_no = i; 385163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 386163953Srrs /* 387163953Srrs * U-stream ranges will be set when the cookie is unpacked. 388163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 389163953Srrs * supported) when the INIT-ACK arrives. 390163953Srrs */ 391163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 392168943Srrs asoc->strmin[i].delivery_started = 0; 393163953Srrs } 394163953Srrs /* 395163953Srrs * load_address_from_init will put the addresses into the 396163953Srrs * association when the COOKIE is processed or the INIT-ACK is 397163953Srrs * processed. Both types of COOKIE's existing and new call this 398163953Srrs * routine. It will remove addresses that are no longer in the 399163953Srrs * association (for the restarting case where addresses are 400163953Srrs * removed). Up front when the INIT arrives we will discard it if it 401163953Srrs * is a restart and new addresses have been added. 402163953Srrs */ 403169655Srrs /* sa_ignore MEMLEAK */ 404163953Srrs return (0); 405163953Srrs} 406163953Srrs 407163953Srrs/* 408163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 409163953Srrs */ 410163953Srrsstatic int 411163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 412163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 413170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 414163953Srrs{ 415163953Srrs struct sctp_association *asoc; 416163953Srrs struct mbuf *op_err; 417163953Srrs int retval, abort_flag; 418163953Srrs uint32_t initack_limit; 419185694Srrs int nat_friendly = 0; 420163953Srrs 421163953Srrs /* First verify that we have no illegal param's */ 422163953Srrs abort_flag = 0; 423163953Srrs op_err = NULL; 424163953Srrs 425163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 426163953Srrs (offset + sizeof(struct sctp_init_chunk)), 427185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 428163953Srrs if (abort_flag) { 429163953Srrs /* Send an abort and notify peer */ 430235360Stuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 431170056Srrs *abort_no_unlock = 1; 432163953Srrs return (-1); 433163953Srrs } 434163953Srrs asoc = &stcb->asoc; 435185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 436163953Srrs /* process the peer's parameters in the INIT-ACK */ 437228653Stuexen retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); 438163953Srrs if (retval < 0) { 439163953Srrs return (retval); 440163953Srrs } 441163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 442163953Srrs /* load all addresses */ 443228653Stuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 444163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 445163953Srrs NULL))) { 446163953Srrs /* Huh, we should abort */ 447169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 448169420Srrs "Load addresses from INIT causes an abort %d\n", 449169420Srrs retval); 450163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 451179157Srrs NULL, 0, net->port); 452168299Srrs *abort_no_unlock = 1; 453163953Srrs return (-1); 454163953Srrs } 455171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 456171477Srrs if (asoc->peer_supports_asconf == 0) { 457216822Stuexen struct sctp_asconf_addr *param, *nparam; 458171477Srrs 459216822Stuexen TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 460216822Stuexen TAILQ_REMOVE(&asoc->asconf_queue, param, next); 461216822Stuexen SCTP_FREE(param, SCTP_M_ASC_ADDR); 462171477Srrs } 463171477Srrs } 464163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 465163953Srrs stcb->asoc.local_hmacs); 466163953Srrs if (op_err) { 467163953Srrs sctp_queue_op_err(stcb, op_err); 468163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 469163953Srrs op_err = NULL; 470163953Srrs } 471163953Srrs /* extract the cookie and queue it to "echo" it back... */ 472179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 473171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 474171943Srrs stcb->asoc.overall_error_count, 475171943Srrs 0, 476171943Srrs SCTP_FROM_SCTP_INPUT, 477171943Srrs __LINE__); 478171943Srrs } 479163953Srrs stcb->asoc.overall_error_count = 0; 480163953Srrs net->error_count = 0; 481163953Srrs 482163953Srrs /* 483163953Srrs * Cancel the INIT timer, We do this first before queueing the 484163953Srrs * cookie. We always cancel at the primary to assue that we are 485163953Srrs * canceling the timer started by the INIT which always goes to the 486163953Srrs * primary. 487163953Srrs */ 488163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 489165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 490163953Srrs 491165220Srrs /* calculate the RTO */ 492218186Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 493219397Srrs SCTP_RTT_FROM_NON_DATA); 494165220Srrs 495163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 496163953Srrs if (retval < 0) { 497163953Srrs /* 498163953Srrs * No cookie, we probably should send a op error. But in any 499163953Srrs * case if there is no cookie in the INIT-ACK, we can 500163953Srrs * abandon the peer, its broke. 501163953Srrs */ 502163953Srrs if (retval == -3) { 503163953Srrs /* We abort with an error of missing mandatory param */ 504163953Srrs op_err = 505163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 506163953Srrs if (op_err) { 507163953Srrs /* 508163953Srrs * Expand beyond to include the mandatory 509163953Srrs * param cookie 510163953Srrs */ 511163953Srrs struct sctp_inv_mandatory_param *mp; 512163953Srrs 513165647Srrs SCTP_BUF_LEN(op_err) = 514163953Srrs sizeof(struct sctp_inv_mandatory_param); 515163953Srrs mp = mtod(op_err, 516163953Srrs struct sctp_inv_mandatory_param *); 517163953Srrs /* Subtract the reserved param */ 518163953Srrs mp->length = 519163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 520163953Srrs mp->num_param = htonl(1); 521163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 522163953Srrs mp->resv = 0; 523163953Srrs } 524163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 525228653Stuexen sh, op_err, vrf_id, net->port); 526168299Srrs *abort_no_unlock = 1; 527163953Srrs } 528163953Srrs return (retval); 529163953Srrs } 530163953Srrs return (0); 531163953Srrs} 532163953Srrs 533163953Srrsstatic void 534163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 535163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 536163953Srrs{ 537163953Srrs struct sockaddr_storage store; 538212225Srrs struct sctp_nets *r_net, *f_net; 539163953Srrs struct timeval tv; 540172396Srrs int req_prim = 0; 541224641Stuexen uint16_t old_error_counter; 542163953Srrs 543221249Stuexen#ifdef INET 544221249Stuexen struct sockaddr_in *sin; 545221249Stuexen 546221249Stuexen#endif 547221249Stuexen#ifdef INET6 548221249Stuexen struct sockaddr_in6 *sin6; 549221249Stuexen 550221249Stuexen#endif 551221249Stuexen 552163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 553163953Srrs /* Invalid length */ 554163953Srrs return; 555163953Srrs } 556163953Srrs memset(&store, 0, sizeof(store)); 557221249Stuexen switch (cp->heartbeat.hb_info.addr_family) { 558221249Stuexen#ifdef INET 559221249Stuexen case AF_INET: 560221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 561221249Stuexen sin = (struct sockaddr_in *)&store; 562221249Stuexen sin->sin_family = cp->heartbeat.hb_info.addr_family; 563221249Stuexen sin->sin_len = cp->heartbeat.hb_info.addr_len; 564221249Stuexen sin->sin_port = stcb->rport; 565221249Stuexen memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 566221249Stuexen sizeof(sin->sin_addr)); 567221249Stuexen } else { 568221249Stuexen return; 569221249Stuexen } 570221249Stuexen break; 571221249Stuexen#endif 572221249Stuexen#ifdef INET6 573221249Stuexen case AF_INET6: 574221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 575221249Stuexen sin6 = (struct sockaddr_in6 *)&store; 576221249Stuexen sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 577221249Stuexen sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 578221249Stuexen sin6->sin6_port = stcb->rport; 579221249Stuexen memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 580221249Stuexen sizeof(sin6->sin6_addr)); 581221249Stuexen } else { 582221249Stuexen return; 583221249Stuexen } 584221249Stuexen break; 585221249Stuexen#endif 586221249Stuexen default: 587163953Srrs return; 588163953Srrs } 589221249Stuexen r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 590163953Srrs if (r_net == NULL) { 591169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 592163953Srrs return; 593163953Srrs } 594163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 595163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 596163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 597163953Srrs /* 598163953Srrs * If the its a HB and it's random value is correct when can 599163953Srrs * confirm the destination. 600163953Srrs */ 601163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 602167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 603167598Srrs stcb->asoc.primary_destination = r_net; 604167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 605212225Srrs f_net = TAILQ_FIRST(&stcb->asoc.nets); 606212225Srrs if (f_net != r_net) { 607167598Srrs /* 608167598Srrs * first one on the list is NOT the primary 609167598Srrs * sctp_cmpaddr() is much more efficent if 610167598Srrs * the primary is the first on the list, 611167598Srrs * make it so. 612167598Srrs */ 613212225Srrs TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 614212225Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 615167598Srrs } 616172396Srrs req_prim = 1; 617167598Srrs } 618163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 619172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 620224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 621224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 622163953Srrs } 623224641Stuexen old_error_counter = r_net->error_count; 624163953Srrs r_net->error_count = 0; 625163953Srrs r_net->hb_responded = 1; 626163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 627163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 628224641Stuexen /* Now lets do a RTO with this */ 629224641Stuexen r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 630224641Stuexen SCTP_RTT_FROM_NON_DATA); 631224641Stuexen if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 632163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 633163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 634235414Stuexen 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 635163953Srrs } 636224641Stuexen if (r_net->dest_state & SCTP_ADDR_PF) { 637224641Stuexen r_net->dest_state &= ~SCTP_ADDR_PF; 638224641Stuexen stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 639224641Stuexen } 640224641Stuexen if (old_error_counter > 0) { 641224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 642224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 643224641Stuexen } 644224641Stuexen if (r_net == stcb->asoc.primary_destination) { 645224641Stuexen if (stcb->asoc.alternate) { 646224641Stuexen /* release the alternate, primary is good */ 647224641Stuexen sctp_free_remote_addr(stcb->asoc.alternate); 648224641Stuexen stcb->asoc.alternate = NULL; 649171440Srrs } 650171440Srrs } 651172396Srrs /* Mobility adaptation */ 652172396Srrs if (req_prim) { 653172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 654172396Srrs SCTP_MOBILITY_BASE) || 655172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 656172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 657172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 658172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 659172396Srrs 660172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 661172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 662172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 663172396Srrs sctp_assoc_immediate_retrans(stcb, 664172396Srrs stcb->asoc.primary_destination); 665172396Srrs } 666172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 667172396Srrs SCTP_MOBILITY_BASE)) { 668212712Stuexen sctp_move_chunks_from_net(stcb, 669212712Stuexen stcb->asoc.deleted_primary); 670172396Srrs } 671172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 672172396Srrs stcb->asoc.deleted_primary); 673172396Srrs } 674172396Srrs } 675163953Srrs} 676163953Srrs 677185694Srrsstatic int 678185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 679185694Srrs{ 680185694Srrs /* 681185694Srrs * return 0 means we want you to proceed with the abort non-zero 682185694Srrs * means no abort processing 683185694Srrs */ 684185694Srrs struct sctpasochead *head; 685185694Srrs 686185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 687185694Srrs /* generate a new vtag and send init */ 688185694Srrs LIST_REMOVE(stcb, sctp_asocs); 689185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 690185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 691185694Srrs /* 692185694Srrs * put it in the bucket in the vtag hash of assoc's for the 693185694Srrs * system 694185694Srrs */ 695185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 696185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 697185694Srrs return (1); 698185694Srrs } 699185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 700185694Srrs /* 701185694Srrs * treat like a case where the cookie expired i.e.: - dump 702185694Srrs * current cookie. - generate a new vtag. - resend init. 703185694Srrs */ 704185694Srrs /* generate a new vtag and send init */ 705185694Srrs LIST_REMOVE(stcb, sctp_asocs); 706185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 707185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 708185694Srrs sctp_stop_all_cookie_timers(stcb); 709185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 710185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 711185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 712185694Srrs /* 713185694Srrs * put it in the bucket in the vtag hash of assoc's for the 714185694Srrs * system 715185694Srrs */ 716185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 717185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 718185694Srrs return (1); 719185694Srrs } 720185694Srrs return (0); 721185694Srrs} 722185694Srrs 723185694Srrsstatic int 724185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 725185694Srrs struct sctp_nets *net) 726185694Srrs{ 727185694Srrs /* 728185694Srrs * return 0 means we want you to proceed with the abort non-zero 729185694Srrs * means no abort processing 730185694Srrs */ 731185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 732185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 733185694Srrs return (0); 734185694Srrs } 735185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 736185694Srrs return (1); 737185694Srrs} 738185694Srrs 739185694Srrs 740163953Srrsstatic void 741235360Stuexensctp_handle_abort(struct sctp_abort_chunk *abort, 742163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 743163953Srrs{ 744172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 745172090Srrs struct socket *so; 746172090Srrs 747172090Srrs#endif 748185694Srrs uint16_t len; 749235360Stuexen uint16_t error; 750172090Srrs 751169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 752163953Srrs if (stcb == NULL) 753163953Srrs return; 754163953Srrs 755235360Stuexen len = ntohs(abort->ch.chunk_length); 756185694Srrs if (len > sizeof(struct sctp_chunkhdr)) { 757185694Srrs /* 758185694Srrs * Need to check the cause codes for our two magic nat 759185694Srrs * aborts which don't kill the assoc necessarily. 760185694Srrs */ 761185694Srrs struct sctp_missing_nat_state *natc; 762185694Srrs 763235360Stuexen natc = (struct sctp_missing_nat_state *)(abort + 1); 764235360Stuexen error = ntohs(natc->cause); 765235360Stuexen if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) { 766185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 767235360Stuexen abort->ch.chunk_flags); 768185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 769185694Srrs return; 770185694Srrs } 771235360Stuexen } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) { 772185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 773235360Stuexen abort->ch.chunk_flags); 774185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 775185694Srrs return; 776185694Srrs } 777185694Srrs } 778235360Stuexen } else { 779235360Stuexen error = 0; 780185694Srrs } 781163953Srrs /* stop any receive timers */ 782171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 783163953Srrs /* notify user of the abort and clean up... */ 784235403Stuexen sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED); 785163953Srrs /* free the tcb */ 786163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 787163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 788163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 789163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 790163953Srrs } 791168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 792168859Srrs sctp_print_out_track_log(stcb); 793168859Srrs#endif 794172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 795172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 796172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 797172090Srrs SCTP_TCB_UNLOCK(stcb); 798172090Srrs SCTP_SOCKET_LOCK(so, 1); 799172090Srrs SCTP_TCB_LOCK(stcb); 800172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 801172090Srrs#endif 802171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 803171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 804171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 805172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 806172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 807172090Srrs#endif 808169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 809163953Srrs} 810163953Srrs 811163953Srrsstatic void 812224641Stuexensctp_start_net_timers(struct sctp_tcb *stcb) 813224641Stuexen{ 814224641Stuexen uint32_t cnt_hb_sent; 815224641Stuexen struct sctp_nets *net; 816224641Stuexen 817224641Stuexen cnt_hb_sent = 0; 818224641Stuexen TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 819224641Stuexen /* 820224641Stuexen * For each network start: 1) A pmtu timer. 2) A HB timer 3) 821224641Stuexen * If the dest in unconfirmed send a hb as well if under 822224641Stuexen * max_hb_burst have been sent. 823224641Stuexen */ 824224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 825224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 826224641Stuexen if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 827224641Stuexen (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 828224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 829224641Stuexen cnt_hb_sent++; 830224641Stuexen } 831224641Stuexen } 832224641Stuexen if (cnt_hb_sent) { 833224641Stuexen sctp_chunk_output(stcb->sctp_ep, stcb, 834224641Stuexen SCTP_OUTPUT_FROM_COOKIE_ACK, 835224641Stuexen SCTP_SO_NOT_LOCKED); 836224641Stuexen } 837224641Stuexen} 838224641Stuexen 839224641Stuexen 840224641Stuexenstatic void 841163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 842163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 843163953Srrs{ 844163953Srrs struct sctp_association *asoc; 845163953Srrs int some_on_streamwheel; 846163953Srrs 847172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 848172090Srrs struct socket *so; 849172090Srrs 850172090Srrs#endif 851172090Srrs 852169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 853169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 854163953Srrs if (stcb == NULL) 855163953Srrs return; 856165220Srrs asoc = &stcb->asoc; 857165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 858165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 859163953Srrs return; 860163953Srrs } 861163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 862163953Srrs /* Shutdown NOT the expected size */ 863163953Srrs return; 864163953Srrs } else { 865228653Stuexen sctp_update_acked(stcb, cp, abort_flag); 866199477Stuexen if (*abort_flag) { 867199477Stuexen return; 868199477Stuexen } 869163953Srrs } 870165220Srrs if (asoc->control_pdapi) { 871163953Srrs /* 872163953Srrs * With a normal shutdown we assume the end of last record. 873163953Srrs */ 874163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 875165220Srrs asoc->control_pdapi->end_added = 1; 876165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 877165220Srrs asoc->control_pdapi = NULL; 878163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 879172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 880172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 881172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 882172090Srrs SCTP_TCB_UNLOCK(stcb); 883172090Srrs SCTP_SOCKET_LOCK(so, 1); 884172090Srrs SCTP_TCB_LOCK(stcb); 885172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 886172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 887172090Srrs /* assoc was freed while we were unlocked */ 888172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 889172090Srrs return; 890172090Srrs } 891172090Srrs#endif 892163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 893172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 894172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 895172090Srrs#endif 896163953Srrs } 897163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 898163953Srrs if (stcb->sctp_socket) { 899163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 900170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 901163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 902171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 903172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 904163953Srrs /* 905163953Srrs * notify upper layer that peer has initiated a 906163953Srrs * shutdown 907163953Srrs */ 908172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 909163953Srrs 910163953Srrs /* reset time */ 911169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 912163953Srrs } 913163953Srrs } 914163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 915163953Srrs /* 916163953Srrs * stop the shutdown timer, since we WILL move to 917163953Srrs * SHUTDOWN-ACK-SENT. 918163953Srrs */ 919171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 920163953Srrs } 921171158Srrs /* Now is there unsent data on a stream somewhere? */ 922221627Stuexen some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 923163953Srrs 924163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 925163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 926163953Srrs some_on_streamwheel) { 927163953Srrs /* By returning we will push more data out */ 928163953Srrs return; 929163953Srrs } else { 930163953Srrs /* no outstanding data to send, so move on... */ 931163953Srrs /* send SHUTDOWN-ACK */ 932224641Stuexen sctp_send_shutdown_ack(stcb, net); 933163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 934166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 935166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 936163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 937163953Srrs } 938171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 939172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 940214918Stuexen sctp_stop_timers_for_shutdown(stcb); 941163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 942163953Srrs stcb, net); 943163953Srrs } 944163953Srrs} 945163953Srrs 946163953Srrsstatic void 947228653Stuexensctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 948204141Stuexen struct sctp_tcb *stcb, 949204141Stuexen struct sctp_nets *net) 950163953Srrs{ 951163953Srrs struct sctp_association *asoc; 952163953Srrs 953172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 954172090Srrs struct socket *so; 955172090Srrs 956172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 957172090Srrs#endif 958169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 959169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 960163953Srrs if (stcb == NULL) 961163953Srrs return; 962163953Srrs 963163953Srrs asoc = &stcb->asoc; 964163953Srrs /* process according to association state */ 965204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 966204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 967204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 968204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 969204141Stuexen SCTP_TCB_UNLOCK(stcb); 970204141Stuexen return; 971204141Stuexen } 972163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 973163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 974163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 975163953Srrs SCTP_TCB_UNLOCK(stcb); 976163953Srrs return; 977163953Srrs } 978165220Srrs if (asoc->control_pdapi) { 979163953Srrs /* 980163953Srrs * With a normal shutdown we assume the end of last record. 981163953Srrs */ 982163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 983165220Srrs asoc->control_pdapi->end_added = 1; 984165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 985165220Srrs asoc->control_pdapi = NULL; 986163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 987172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 988172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 989172090Srrs SCTP_TCB_UNLOCK(stcb); 990172090Srrs SCTP_SOCKET_LOCK(so, 1); 991172090Srrs SCTP_TCB_LOCK(stcb); 992172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 993172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 994172090Srrs /* assoc was freed while we were unlocked */ 995172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 996172090Srrs return; 997172090Srrs } 998172090Srrs#endif 999163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1000172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1001172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1002172090Srrs#endif 1003163953Srrs } 1004163953Srrs /* are the queues empty? */ 1005163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 1006163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 1007217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1008235416Stuexen sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED); 1009163953Srrs } 1010163953Srrs /* stop the timer */ 1011171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1012163953Srrs /* send SHUTDOWN-COMPLETE */ 1013204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 1014163953Srrs /* notify upper layer protocol */ 1015163953Srrs if (stcb->sctp_socket) { 1016163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1017163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1018229774Stuexen stcb->sctp_socket->so_snd.sb_cc = 0; 1019163953Srrs } 1020230379Stuexen sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1021163953Srrs } 1022163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1023163953Srrs /* free the TCB but first save off the ep */ 1024172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1025172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1026172090Srrs SCTP_TCB_UNLOCK(stcb); 1027172090Srrs SCTP_SOCKET_LOCK(so, 1); 1028172090Srrs SCTP_TCB_LOCK(stcb); 1029172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1030172090Srrs#endif 1031171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1032171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1033172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1034172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1035172090Srrs#endif 1036163953Srrs} 1037163953Srrs 1038163953Srrs/* 1039163953Srrs * Skip past the param header and then we will find the chunk that caused the 1040163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1041163953Srrs * our peer must be broken. 1042163953Srrs */ 1043163953Srrsstatic void 1044163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1045163953Srrs struct sctp_nets *net) 1046163953Srrs{ 1047163953Srrs struct sctp_chunkhdr *chk; 1048163953Srrs 1049163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1050163953Srrs switch (chk->chunk_type) { 1051163953Srrs case SCTP_ASCONF_ACK: 1052163953Srrs case SCTP_ASCONF: 1053163953Srrs sctp_asconf_cleanup(stcb, net); 1054163953Srrs break; 1055163953Srrs case SCTP_FORWARD_CUM_TSN: 1056163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1057163953Srrs break; 1058163953Srrs default: 1059169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1060169420Srrs "Peer does not support chunk type %d(%x)??\n", 1061169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1062163953Srrs break; 1063163953Srrs } 1064163953Srrs} 1065163953Srrs 1066163953Srrs/* 1067163953Srrs * Skip past the param header and then we will find the param that caused the 1068163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1069163953Srrs * these will turn of specific features. 1070163953Srrs */ 1071163953Srrsstatic void 1072163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1073163953Srrs{ 1074163953Srrs struct sctp_paramhdr *pbad; 1075163953Srrs 1076163953Srrs pbad = phdr + 1; 1077163953Srrs switch (ntohs(pbad->param_type)) { 1078163953Srrs /* pr-sctp draft */ 1079163953Srrs case SCTP_PRSCTP_SUPPORTED: 1080163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1081163953Srrs break; 1082163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1083163953Srrs break; 1084163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1085185694Srrs case SCTP_HAS_NAT_SUPPORT: 1086185694Srrs stcb->asoc.peer_supports_nat = 0; 1087185694Srrs break; 1088163953Srrs case SCTP_ADD_IP_ADDRESS: 1089163953Srrs case SCTP_DEL_IP_ADDRESS: 1090163953Srrs case SCTP_SET_PRIM_ADDR: 1091163953Srrs stcb->asoc.peer_supports_asconf = 0; 1092163953Srrs break; 1093163953Srrs case SCTP_SUCCESS_REPORT: 1094163953Srrs case SCTP_ERROR_CAUSE_IND: 1095169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1096169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1097169420Srrs "Turning off ASCONF to this strange peer\n"); 1098163953Srrs stcb->asoc.peer_supports_asconf = 0; 1099163953Srrs break; 1100163953Srrs default: 1101169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1102169420Srrs "Peer does not support param type %d(%x)??\n", 1103169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1104163953Srrs break; 1105163953Srrs } 1106163953Srrs} 1107163953Srrs 1108163953Srrsstatic int 1109163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1110163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1111163953Srrs{ 1112163953Srrs int chklen; 1113163953Srrs struct sctp_paramhdr *phdr; 1114235418Stuexen uint16_t error, error_type; 1115163953Srrs uint16_t error_len; 1116163953Srrs struct sctp_association *asoc; 1117163953Srrs int adjust; 1118163953Srrs 1119172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1120172090Srrs struct socket *so; 1121172090Srrs 1122172090Srrs#endif 1123172090Srrs 1124163953Srrs /* parse through all of the errors and process */ 1125163953Srrs asoc = &stcb->asoc; 1126163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1127163953Srrs sizeof(struct sctp_chunkhdr)); 1128163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1129235418Stuexen error = 0; 1130163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1131163953Srrs /* Process an Error Cause */ 1132163953Srrs error_type = ntohs(phdr->param_type); 1133163953Srrs error_len = ntohs(phdr->param_length); 1134163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1135163953Srrs /* invalid param length for this param */ 1136169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1137169420Srrs chklen, error_len); 1138163953Srrs return (0); 1139163953Srrs } 1140235418Stuexen if (error == 0) { 1141235418Stuexen /* report the first error cause */ 1142235418Stuexen error = error_type; 1143235418Stuexen } 1144163953Srrs switch (error_type) { 1145163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1146163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1147163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1148163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1149169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1150169420Srrs error_type); 1151163953Srrs break; 1152185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1153185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1154185694Srrs ch->chunk_flags); 1155185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1156185694Srrs return (0); 1157185694Srrs } 1158185694Srrs break; 1159185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1160185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1161185694Srrs ch->chunk_flags); 1162185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1163185694Srrs return (0); 1164185694Srrs } 1165185694Srrs break; 1166163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1167163953Srrs /* 1168163953Srrs * We only act if we have echoed a cookie and are 1169163953Srrs * waiting. 1170163953Srrs */ 1171163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1172163953Srrs int *p; 1173163953Srrs 1174163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1175163953Srrs /* Save the time doubled */ 1176163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1177163953Srrs asoc->stale_cookie_count++; 1178163953Srrs if (asoc->stale_cookie_count > 1179163953Srrs asoc->max_init_times) { 1180235403Stuexen sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); 1181163953Srrs /* now free the asoc */ 1182172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1183172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1184172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1185172090Srrs SCTP_TCB_UNLOCK(stcb); 1186172090Srrs SCTP_SOCKET_LOCK(so, 1); 1187172090Srrs SCTP_TCB_LOCK(stcb); 1188172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1189172090Srrs#endif 1190171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1191171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1192172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1193172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1194172090Srrs#endif 1195163953Srrs return (-1); 1196163953Srrs } 1197163953Srrs /* blast back to INIT state */ 1198185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1199163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1200163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1201163953Srrs sctp_stop_all_cookie_timers(stcb); 1202172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1203163953Srrs } 1204163953Srrs break; 1205163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1206163953Srrs /* 1207163953Srrs * Nothing we can do here, we don't do hostname 1208163953Srrs * addresses so if the peer does not like my IPv6 1209163953Srrs * (or IPv4 for that matter) it does not matter. If 1210163953Srrs * they don't support that type of address, they can 1211163953Srrs * NOT possibly get that packet type... i.e. with no 1212163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1213163953Srrs * safely ignore this one. If we ever added support 1214163953Srrs * for HOSTNAME Addresses, then we would need to do 1215163953Srrs * something here. 1216163953Srrs */ 1217163953Srrs break; 1218163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1219163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1220163953Srrs break; 1221163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1222163953Srrs sctp_process_unrecog_param(stcb, phdr); 1223163953Srrs break; 1224163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1225163953Srrs /* 1226163953Srrs * We ignore this since the timer will drive out a 1227163953Srrs * new cookie anyway and there timer will drive us 1228163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1229163953Srrs * here since we don't have their tag. 1230163953Srrs */ 1231163953Srrs break; 1232163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1233163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1234163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1235163953Srrs /* 1236163953Srrs * We should NOT get these here, but in a 1237166086Srrs * ASCONF-ACK. 1238163953Srrs */ 1239169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1240169420Srrs error_type); 1241163953Srrs break; 1242163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1243163953Srrs /* 1244163953Srrs * And what, pray tell do we do with the fact that 1245163953Srrs * the peer is out of resources? Not really sure we 1246166086Srrs * could do anything but abort. I suspect this 1247163953Srrs * should have came WITH an abort instead of in a 1248163953Srrs * OP-ERROR. 1249163953Srrs */ 1250163953Srrs break; 1251163953Srrs default: 1252169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1253169420Srrs error_type); 1254163953Srrs break; 1255163953Srrs } 1256163953Srrs adjust = SCTP_SIZE32(error_len); 1257163953Srrs chklen -= adjust; 1258163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1259163953Srrs } 1260235418Stuexen sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED); 1261163953Srrs return (0); 1262163953Srrs} 1263163953Srrs 1264163953Srrsstatic int 1265169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1266169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1267170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1268163953Srrs{ 1269163953Srrs struct sctp_init_ack *init_ack; 1270163953Srrs struct mbuf *op_err; 1271163953Srrs 1272169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1273169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1274169420Srrs 1275163953Srrs if (stcb == NULL) { 1276169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1277169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1278163953Srrs return (-1); 1279163953Srrs } 1280163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1281163953Srrs /* Invalid length */ 1282163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1283163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1284179157Srrs op_err, 0, net->port); 1285168299Srrs *abort_no_unlock = 1; 1286163953Srrs return (-1); 1287163953Srrs } 1288163953Srrs init_ack = &cp->init; 1289163953Srrs /* validate parameters */ 1290163953Srrs if (init_ack->initiate_tag == 0) { 1291163953Srrs /* protocol error... send an abort */ 1292163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1293163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1294179157Srrs op_err, 0, net->port); 1295168299Srrs *abort_no_unlock = 1; 1296163953Srrs return (-1); 1297163953Srrs } 1298163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1299163953Srrs /* protocol error... send an abort */ 1300163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1301163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1302179157Srrs op_err, 0, net->port); 1303168299Srrs *abort_no_unlock = 1; 1304163953Srrs return (-1); 1305163953Srrs } 1306163953Srrs if (init_ack->num_inbound_streams == 0) { 1307163953Srrs /* protocol error... send an abort */ 1308163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1309163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1310179157Srrs op_err, 0, net->port); 1311168299Srrs *abort_no_unlock = 1; 1312163953Srrs return (-1); 1313163953Srrs } 1314163953Srrs if (init_ack->num_outbound_streams == 0) { 1315163953Srrs /* protocol error... send an abort */ 1316163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1317163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1318179157Srrs op_err, 0, net->port); 1319168299Srrs *abort_no_unlock = 1; 1320163953Srrs return (-1); 1321163953Srrs } 1322163953Srrs /* process according to association state... */ 1323171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1324163953Srrs case SCTP_STATE_COOKIE_WAIT: 1325163953Srrs /* this is the expected state for this chunk */ 1326163953Srrs /* process the INIT-ACK parameters */ 1327163953Srrs if (stcb->asoc.primary_destination->dest_state & 1328163953Srrs SCTP_ADDR_UNCONFIRMED) { 1329163953Srrs /* 1330163953Srrs * The primary is where we sent the INIT, we can 1331163953Srrs * always consider it confirmed when the INIT-ACK is 1332163953Srrs * returned. Do this before we load addresses 1333163953Srrs * though. 1334163953Srrs */ 1335163953Srrs stcb->asoc.primary_destination->dest_state &= 1336163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1337163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1338172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1339163953Srrs } 1340169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1341170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1342163953Srrs /* error in parsing parameters */ 1343163953Srrs return (-1); 1344163953Srrs } 1345163953Srrs /* update our state */ 1346169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1347171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1348163953Srrs 1349163953Srrs /* reset the RTO calc */ 1350179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1351171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1352171943Srrs stcb->asoc.overall_error_count, 1353171943Srrs 0, 1354171943Srrs SCTP_FROM_SCTP_INPUT, 1355171943Srrs __LINE__); 1356171943Srrs } 1357163953Srrs stcb->asoc.overall_error_count = 0; 1358169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1359163953Srrs /* 1360163953Srrs * collapse the init timer back in case of a exponential 1361165220Srrs * backoff 1362163953Srrs */ 1363163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1364163953Srrs stcb, net); 1365163953Srrs /* 1366163953Srrs * the send at the end of the inbound data processing will 1367163953Srrs * cause the cookie to be sent 1368163953Srrs */ 1369163953Srrs break; 1370163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1371163953Srrs /* incorrect state... discard */ 1372163953Srrs break; 1373163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1374163953Srrs /* incorrect state... discard */ 1375163953Srrs break; 1376163953Srrs case SCTP_STATE_OPEN: 1377163953Srrs /* incorrect state... discard */ 1378163953Srrs break; 1379163953Srrs case SCTP_STATE_EMPTY: 1380163953Srrs case SCTP_STATE_INUSE: 1381163953Srrs default: 1382163953Srrs /* incorrect state... discard */ 1383163953Srrs return (-1); 1384163953Srrs break; 1385163953Srrs } 1386169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1387163953Srrs return (0); 1388163953Srrs} 1389163953Srrs 1390185694Srrsstatic struct sctp_tcb * 1391185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1392185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1393185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1394185694Srrs struct sockaddr *init_src, int *notification, 1395185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1396185694Srrs uint32_t vrf_id, uint16_t port); 1397163953Srrs 1398185694Srrs 1399163953Srrs/* 1400163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1401163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1402163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1403163953Srrs * mbuf to the cookie-echo chunk 1404163953Srrs */ 1405163953Srrsstatic struct sctp_tcb * 1406163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1407163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1408185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1409228653Stuexen struct sockaddr *init_src, int *notification, 1410185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1411163953Srrs{ 1412163953Srrs struct sctp_association *asoc; 1413163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1414163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1415185694Srrs struct sctp_nets *net; 1416185694Srrs struct mbuf *op_err; 1417185694Srrs struct sctp_paramhdr *ph; 1418165220Srrs int init_offset, initack_offset, i; 1419163953Srrs int retval; 1420164205Srrs int spec_flag = 0; 1421170140Srrs uint32_t how_indx; 1422163953Srrs 1423185694Srrs net = *netp; 1424163953Srrs /* I know that the TCB is non-NULL from the caller */ 1425163953Srrs asoc = &stcb->asoc; 1426166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1427166023Srrs if (asoc->cookie_how[how_indx] == 0) 1428166023Srrs break; 1429166023Srrs } 1430166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1431166023Srrs asoc->cookie_how[how_indx] = 1; 1432166023Srrs } 1433163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1434163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1435163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1436163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1437165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1438163953Srrs if (op_err == NULL) { 1439163953Srrs /* FOOBAR */ 1440163953Srrs return (NULL); 1441163953Srrs } 1442163953Srrs /* Set the len */ 1443165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1444163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1445163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1446163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1447169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1448179157Srrs vrf_id, net->port); 1449166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1450166023Srrs asoc->cookie_how[how_indx] = 2; 1451163953Srrs return (NULL); 1452163953Srrs } 1453163953Srrs /* 1454163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1455163953Srrs * INIT should start after the cookie-echo header struct (chunk 1456163953Srrs * header, state cookie header struct) 1457163953Srrs */ 1458163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1459163953Srrs 1460163953Srrs init_cp = (struct sctp_init_chunk *) 1461163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1462163953Srrs (uint8_t *) & init_buf); 1463163953Srrs if (init_cp == NULL) { 1464163953Srrs /* could not pull a INIT chunk in cookie */ 1465163953Srrs return (NULL); 1466163953Srrs } 1467163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1468163953Srrs return (NULL); 1469163953Srrs } 1470163953Srrs /* 1471163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1472163953Srrs * INIT-ACK follows the INIT chunk 1473163953Srrs */ 1474228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1475163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1476163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1477163953Srrs (uint8_t *) & initack_buf); 1478163953Srrs if (initack_cp == NULL) { 1479163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1480163953Srrs return (NULL); 1481163953Srrs } 1482163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1483163953Srrs return (NULL); 1484163953Srrs } 1485163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1486163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1487163953Srrs /* 1488163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1489163953Srrs * to get into the OPEN state 1490163953Srrs */ 1491166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1492172091Srrs /*- 1493172091Srrs * Opps, this means that we somehow generated two vtag's 1494172091Srrs * the same. I.e. we did: 1495172091Srrs * Us Peer 1496172091Srrs * <---INIT(tag=a)------ 1497172091Srrs * ----INIT-ACK(tag=t)--> 1498172091Srrs * ----INIT(tag=t)------> *1 1499172091Srrs * <---INIT-ACK(tag=a)--- 1500172091Srrs * <----CE(tag=t)------------- *2 1501172091Srrs * 1502172091Srrs * At point *1 we should be generating a different 1503172091Srrs * tag t'. Which means we would throw away the CE and send 1504172091Srrs * ours instead. Basically this is case C (throw away side). 1505172091Srrs */ 1506172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1507172091Srrs asoc->cookie_how[how_indx] = 17; 1508172091Srrs return (NULL); 1509172091Srrs 1510166023Srrs } 1511163953Srrs switch SCTP_GET_STATE 1512163953Srrs (asoc) { 1513163953Srrs case SCTP_STATE_COOKIE_WAIT: 1514166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1515163953Srrs /* 1516169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1517166023Srrs * correct tags... just accept it...but we must 1518166023Srrs * process the init so that we can make sure we have 1519166023Srrs * the right seq no's. 1520163953Srrs */ 1521163953Srrs /* First we must process the INIT !! */ 1522228653Stuexen retval = sctp_process_init(init_cp, stcb); 1523163953Srrs if (retval < 0) { 1524166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1525166023Srrs asoc->cookie_how[how_indx] = 3; 1526163953Srrs return (NULL); 1527163953Srrs } 1528163953Srrs /* we have already processed the INIT so no problem */ 1529163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1530171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1531171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1532163953Srrs /* update current state */ 1533166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1534166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1535166675Srrs else 1536166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1537171943Srrs 1538171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1539163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1540163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1541163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1542163953Srrs } 1543166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1544165220Srrs sctp_stop_all_cookie_timers(stcb); 1545163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1546163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1547163953Srrs (inp->sctp_socket->so_qlimit == 0) 1548163953Srrs ) { 1549172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1550172090Srrs struct socket *so; 1551172090Srrs 1552172090Srrs#endif 1553163953Srrs /* 1554163953Srrs * Here is where collision would go if we 1555163953Srrs * did a connect() and instead got a 1556163953Srrs * init/init-ack/cookie done before the 1557163953Srrs * init-ack came back.. 1558163953Srrs */ 1559163953Srrs stcb->sctp_ep->sctp_flags |= 1560163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1561172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1562172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1563172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1564172090Srrs SCTP_TCB_UNLOCK(stcb); 1565172090Srrs SCTP_SOCKET_LOCK(so, 1); 1566172090Srrs SCTP_TCB_LOCK(stcb); 1567172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1568172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1569172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1570172090Srrs return (NULL); 1571172090Srrs } 1572172090Srrs#endif 1573172090Srrs soisconnected(stcb->sctp_socket); 1574172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1575172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1576172090Srrs#endif 1577163953Srrs } 1578163953Srrs /* notify upper layer */ 1579163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1580163953Srrs /* 1581163953Srrs * since we did not send a HB make sure we don't 1582163953Srrs * double things 1583163953Srrs */ 1584163953Srrs net->hb_responded = 1; 1585170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1586218186Srrs &cookie->time_entered, 1587218186Srrs sctp_align_unsafe_makecopy, 1588219397Srrs SCTP_RTT_FROM_NON_DATA); 1589163953Srrs 1590163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1591163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1592163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1593163953Srrs inp, stcb, NULL); 1594163953Srrs } 1595163953Srrs break; 1596163953Srrs default: 1597163953Srrs /* 1598163953Srrs * we're in the OPEN state (or beyond), so peer must 1599163953Srrs * have simply lost the COOKIE-ACK 1600163953Srrs */ 1601163953Srrs break; 1602163953Srrs } /* end switch */ 1603165220Srrs sctp_stop_all_cookie_timers(stcb); 1604163953Srrs /* 1605163953Srrs * We ignore the return code here.. not sure if we should 1606163953Srrs * somehow abort.. but we do have an existing asoc. This 1607163953Srrs * really should not fail. 1608163953Srrs */ 1609228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1610163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1611163953Srrs initack_offset, sh, init_src)) { 1612166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1613166023Srrs asoc->cookie_how[how_indx] = 4; 1614163953Srrs return (NULL); 1615163953Srrs } 1616163953Srrs /* respond with a COOKIE-ACK */ 1617165220Srrs sctp_toss_old_cookies(stcb, asoc); 1618163953Srrs sctp_send_cookie_ack(stcb); 1619166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1620166023Srrs asoc->cookie_how[how_indx] = 5; 1621163953Srrs return (stcb); 1622169352Srrs } 1623163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1624163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1625163953Srrs cookie->tie_tag_my_vtag == 0 && 1626163953Srrs cookie->tie_tag_peer_vtag == 0) { 1627163953Srrs /* 1628163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1629163953Srrs */ 1630166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1631166023Srrs asoc->cookie_how[how_indx] = 6; 1632163953Srrs return (NULL); 1633163953Srrs } 1634185694Srrs /* 1635185694Srrs * If nat support, and the below and stcb is established, send back 1636185694Srrs * a ABORT(colliding state) if we are established. 1637185694Srrs */ 1638185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1639185694Srrs (asoc->peer_supports_nat) && 1640185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1641185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1642185694Srrs (asoc->peer_vtag == 0)))) { 1643163953Srrs /* 1644185694Srrs * Special case - Peer's support nat. We may have two init's 1645185694Srrs * that we gave out the same tag on since one was not 1646185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1647185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1648185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1649185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1650185694Srrs * Now we have colliding state. We must send an abort here 1651185694Srrs * with colliding state indication. 1652185694Srrs */ 1653185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1654185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1655185694Srrs if (op_err == NULL) { 1656185694Srrs /* FOOBAR */ 1657185694Srrs return (NULL); 1658185694Srrs } 1659185694Srrs /* pre-reserve some space */ 1660185694Srrs#ifdef INET6 1661185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1662185694Srrs#else 1663185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1664185694Srrs#endif 1665185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1666185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1667185694Srrs /* Set the len */ 1668185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1669185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1670185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1671185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1672185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1673185694Srrs return (NULL); 1674185694Srrs } 1675185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1676185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1677185694Srrs (asoc->peer_vtag == 0))) { 1678185694Srrs /* 1679163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1680163953Srrs * should be ok, re-accept peer info 1681163953Srrs */ 1682166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1683166023Srrs /* 1684166023Srrs * Extension of case C. If we hit this, then the 1685166023Srrs * random number generator returned the same vtag 1686166023Srrs * when we first sent our INIT-ACK and when we later 1687166023Srrs * sent our INIT. The side with the seq numbers that 1688166023Srrs * are different will be the one that normnally 1689166023Srrs * would have hit case C. This in effect "extends" 1690166023Srrs * our vtags in this collision case to be 64 bits. 1691166023Srrs * The same collision could occur aka you get both 1692166023Srrs * vtag and seq number the same twice in a row.. but 1693166023Srrs * is much less likely. If it did happen then we 1694166023Srrs * would proceed through and bring up the assoc.. we 1695166023Srrs * may end up with the wrong stream setup however.. 1696166023Srrs * which would be bad.. but there is no way to 1697166023Srrs * tell.. until we send on a stream that does not 1698166023Srrs * exist :-) 1699166023Srrs */ 1700166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1701166023Srrs asoc->cookie_how[how_indx] = 7; 1702166023Srrs 1703166023Srrs return (NULL); 1704166023Srrs } 1705166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1706166023Srrs asoc->cookie_how[how_indx] = 8; 1707171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1708163953Srrs sctp_stop_all_cookie_timers(stcb); 1709163953Srrs /* 1710163953Srrs * since we did not send a HB make sure we don't double 1711163953Srrs * things 1712163953Srrs */ 1713163953Srrs net->hb_responded = 1; 1714163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1715163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1716163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1717163953Srrs NULL); 1718163953Srrs } 1719163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1720164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1721164205Srrs 1722164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1723164205Srrs /* 1724164205Srrs * Ok the peer probably discarded our data (if we 1725164205Srrs * echoed a cookie+data). So anything on the 1726164205Srrs * sent_queue should be marked for retransmit, we 1727164205Srrs * may not get something to kick us so it COULD 1728164205Srrs * still take a timeout to move these.. but it can't 1729164205Srrs * hurt to mark them. 1730164205Srrs */ 1731164205Srrs struct sctp_tmit_chunk *chk; 1732163953Srrs 1733164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1734164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1735164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1736171440Srrs sctp_flight_size_decrease(chk); 1737171440Srrs sctp_total_flight_decrease(stcb, chk); 1738168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1739164205Srrs spec_flag++; 1740164205Srrs } 1741164205Srrs } 1742164205Srrs 1743164205Srrs } 1744163953Srrs /* process the INIT info (peer's info) */ 1745228653Stuexen retval = sctp_process_init(init_cp, stcb); 1746163953Srrs if (retval < 0) { 1747166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1748166023Srrs asoc->cookie_how[how_indx] = 9; 1749163953Srrs return (NULL); 1750163953Srrs } 1751228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1752163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1753163953Srrs initack_offset, sh, init_src)) { 1754166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1755166023Srrs asoc->cookie_how[how_indx] = 10; 1756163953Srrs return (NULL); 1757163953Srrs } 1758163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1759163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1760163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1761163953Srrs 1762163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1763163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1764163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1765172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1766172090Srrs struct socket *so; 1767172090Srrs 1768172090Srrs#endif 1769163953Srrs stcb->sctp_ep->sctp_flags |= 1770163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1771172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1772172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1773172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1774172090Srrs SCTP_TCB_UNLOCK(stcb); 1775172090Srrs SCTP_SOCKET_LOCK(so, 1); 1776172090Srrs SCTP_TCB_LOCK(stcb); 1777172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1778172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1779172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1780172090Srrs return (NULL); 1781172090Srrs } 1782172090Srrs#endif 1783172090Srrs soisconnected(stcb->sctp_socket); 1784172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1785172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1786172090Srrs#endif 1787163953Srrs } 1788166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1789166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1790166675Srrs else 1791166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1792166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1793166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1794166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1795166675Srrs } else { 1796166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1797163953Srrs } 1798171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1799163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1800163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1801163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1802163953Srrs } 1803163953Srrs sctp_stop_all_cookie_timers(stcb); 1804165220Srrs sctp_toss_old_cookies(stcb, asoc); 1805163953Srrs sctp_send_cookie_ack(stcb); 1806164205Srrs if (spec_flag) { 1807164205Srrs /* 1808164205Srrs * only if we have retrans set do we do this. What 1809164205Srrs * this call does is get only the COOKIE-ACK out and 1810164205Srrs * then when we return the normal call to 1811164205Srrs * sctp_chunk_output will get the retrans out behind 1812164205Srrs * this. 1813164205Srrs */ 1814172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1815164205Srrs } 1816166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1817166023Srrs asoc->cookie_how[how_indx] = 11; 1818166023Srrs 1819163953Srrs return (stcb); 1820163953Srrs } 1821163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1822163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1823163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1824163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1825163953Srrs cookie->tie_tag_peer_vtag != 0) { 1826163953Srrs struct sctpasochead *head; 1827163953Srrs 1828185694Srrs if (asoc->peer_supports_nat) { 1829185694Srrs /* 1830185694Srrs * This is a gross gross hack. just call the 1831185694Srrs * cookie_new code since we are allowing a duplicate 1832185694Srrs * association. I hope this works... 1833185694Srrs */ 1834185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1835185694Srrs inp, netp, init_src, notification, 1836185694Srrs auth_skipped, auth_offset, auth_len, 1837185694Srrs vrf_id, port)); 1838185694Srrs } 1839163953Srrs /* 1840163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1841163953Srrs */ 1842165220Srrs /* temp code */ 1843166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1844166023Srrs asoc->cookie_how[how_indx] = 12; 1845171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1846171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1847165647Srrs 1848163953Srrs /* notify upper layer */ 1849163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1850165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1851166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1852166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1853166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1854166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1855166675Srrs } 1856166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1857166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1858166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1859166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1860166675Srrs } 1861165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1862171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1863165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1864165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1865165647Srrs 1866165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1867165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1868171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1869165647Srrs } 1870165647Srrs asoc->pre_open_streams = 1871165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1872165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1873165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1874179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1875165647Srrs 1876165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1877165647Srrs 1878165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1879165647Srrs 1880165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1881170138Srrs if (asoc->mapping_array) { 1882165647Srrs memset(asoc->mapping_array, 0, 1883165647Srrs asoc->mapping_array_size); 1884170138Srrs } 1885205627Srrs if (asoc->nr_mapping_array) { 1886185694Srrs memset(asoc->nr_mapping_array, 0, 1887206137Stuexen asoc->mapping_array_size); 1888185694Srrs } 1889165220Srrs SCTP_TCB_UNLOCK(stcb); 1890165220Srrs SCTP_INP_INFO_WLOCK(); 1891165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1892165220Srrs SCTP_TCB_LOCK(stcb); 1893165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1894163953Srrs /* send up all the data */ 1895164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1896165220Srrs 1897235416Stuexen sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED); 1898165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1899165220Srrs stcb->asoc.strmout[i].stream_no = i; 1900165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1901165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1902165220Srrs } 1903163953Srrs /* process the INIT-ACK info (my info) */ 1904163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1905163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1906163953Srrs 1907163953Srrs /* pull from vtag hash */ 1908163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1909163953Srrs /* re-insert to new vtag position */ 1910179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1911179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1912163953Srrs /* 1913163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1914163953Srrs * system 1915163953Srrs */ 1916163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1917163953Srrs 1918163953Srrs /* process the INIT info (peer's info) */ 1919164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1920165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1921165220Srrs SCTP_INP_INFO_WUNLOCK(); 1922165220Srrs 1923228653Stuexen retval = sctp_process_init(init_cp, stcb); 1924163953Srrs if (retval < 0) { 1925166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1926166023Srrs asoc->cookie_how[how_indx] = 13; 1927166023Srrs 1928163953Srrs return (NULL); 1929163953Srrs } 1930163953Srrs /* 1931163953Srrs * since we did not send a HB make sure we don't double 1932163953Srrs * things 1933163953Srrs */ 1934163953Srrs net->hb_responded = 1; 1935163953Srrs 1936228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1937163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1938163953Srrs initack_offset, sh, init_src)) { 1939166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1940166023Srrs asoc->cookie_how[how_indx] = 14; 1941166023Srrs 1942163953Srrs return (NULL); 1943163953Srrs } 1944163953Srrs /* respond with a COOKIE-ACK */ 1945163953Srrs sctp_stop_all_cookie_timers(stcb); 1946165220Srrs sctp_toss_old_cookies(stcb, asoc); 1947163953Srrs sctp_send_cookie_ack(stcb); 1948166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1949166023Srrs asoc->cookie_how[how_indx] = 15; 1950163953Srrs 1951163953Srrs return (stcb); 1952163953Srrs } 1953166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1954166023Srrs asoc->cookie_how[how_indx] = 16; 1955163953Srrs /* all other cases... */ 1956163953Srrs return (NULL); 1957163953Srrs} 1958163953Srrs 1959166086Srrs 1960163953Srrs/* 1961163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1962163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1963163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1964163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1965163953Srrs * was from returns a new TCB 1966163953Srrs */ 1967185694Srrsstruct sctp_tcb * 1968163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1969163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1970163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1971163953Srrs struct sockaddr *init_src, int *notification, 1972169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1973179157Srrs uint32_t vrf_id, uint16_t port) 1974163953Srrs{ 1975163953Srrs struct sctp_tcb *stcb; 1976163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1977163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1978163953Srrs struct sockaddr_storage sa_store; 1979163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1980163953Srrs struct sctp_association *asoc; 1981163953Srrs int init_offset, initack_offset, initack_limit; 1982163953Srrs int retval; 1983163953Srrs int error = 0; 1984166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1985163953Srrs 1986221249Stuexen#ifdef INET 1987221249Stuexen struct sockaddr_in *sin; 1988221249Stuexen 1989221249Stuexen#endif 1990221249Stuexen#ifdef INET6 1991221249Stuexen struct sockaddr_in6 *sin6; 1992221249Stuexen 1993221249Stuexen#endif 1994172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1995172090Srrs struct socket *so; 1996172090Srrs 1997172090Srrs so = SCTP_INP_SO(inp); 1998172090Srrs#endif 1999172090Srrs 2000163953Srrs /* 2001163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 2002163953Srrs * INIT should start after the cookie-echo header struct (chunk 2003163953Srrs * header, state cookie header struct) 2004163953Srrs */ 2005163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2006163953Srrs init_cp = (struct sctp_init_chunk *) 2007163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2008163953Srrs (uint8_t *) & init_buf); 2009163953Srrs if (init_cp == NULL) { 2010163953Srrs /* could not pull a INIT chunk in cookie */ 2011169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2012169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 2013163953Srrs return (NULL); 2014163953Srrs } 2015163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2016169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2017163953Srrs return (NULL); 2018163953Srrs } 2019228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2020163953Srrs /* 2021163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 2022163953Srrs * INIT-ACK follows the INIT chunk 2023163953Srrs */ 2024163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2025163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2026163953Srrs (uint8_t *) & initack_buf); 2027163953Srrs if (initack_cp == NULL) { 2028163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2029169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2030163953Srrs return (NULL); 2031163953Srrs } 2032163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2033163953Srrs return (NULL); 2034163953Srrs } 2035163953Srrs /* 2036163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2037163953Srrs * "initack_limit" value. This is because the chk_length field 2038163953Srrs * includes the length of the cookie, but the cookie is omitted when 2039163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2040163953Srrs */ 2041163953Srrs initack_limit = offset + cookie_len; 2042163953Srrs 2043163953Srrs /* 2044163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2045163953Srrs * and popluate 2046163953Srrs */ 2047171531Srrs 2048171531Srrs /* 2049171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2050171531Srrs * We do this since in effect we only use the p argument when the 2051171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2052171531Srrs * getting a cookie, we cannot be unbound. 2053171531Srrs */ 2054206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2055171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2056171531Srrs (struct thread *)NULL 2057171531Srrs ); 2058163953Srrs if (stcb == NULL) { 2059163953Srrs struct mbuf *op_err; 2060163953Srrs 2061163953Srrs /* memory problem? */ 2062169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2063169420Srrs "process_cookie_new: no room for another TCB!\n"); 2064163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2065170140Srrs 2066163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2067179157Srrs sh, op_err, vrf_id, port); 2068163953Srrs return (NULL); 2069163953Srrs } 2070163953Srrs /* get the correct sctp_nets */ 2071169420Srrs if (netp) 2072169420Srrs *netp = sctp_findnet(stcb, init_src); 2073169420Srrs 2074163953Srrs asoc = &stcb->asoc; 2075163953Srrs /* get scope variables out of cookie */ 2076163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2077163953Srrs asoc->site_scope = cookie->site_scope; 2078163953Srrs asoc->local_scope = cookie->local_scope; 2079163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2080163953Srrs 2081163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2082163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2083163953Srrs struct mbuf *op_err; 2084163953Srrs 2085163953Srrs /* 2086163953Srrs * Houston we have a problem. The EP changed while the 2087163953Srrs * cookie was in flight. Only recourse is to abort the 2088163953Srrs * association. 2089163953Srrs */ 2090170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2091163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2092163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2093179157Srrs sh, op_err, vrf_id, port); 2094172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2095172090Srrs SCTP_TCB_UNLOCK(stcb); 2096172090Srrs SCTP_SOCKET_LOCK(so, 1); 2097172090Srrs SCTP_TCB_LOCK(stcb); 2098172090Srrs#endif 2099171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2100171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2101172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2102172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2103172090Srrs#endif 2104172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2105163953Srrs return (NULL); 2106163953Srrs } 2107163953Srrs /* process the INIT-ACK info (my info) */ 2108185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2109163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2110163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2111163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2112163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2113179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2114163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2115163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2116163953Srrs 2117163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2118163953Srrs 2119163953Srrs /* process the INIT info (peer's info) */ 2120169420Srrs if (netp) 2121228653Stuexen retval = sctp_process_init(init_cp, stcb); 2122169420Srrs else 2123169420Srrs retval = 0; 2124163953Srrs if (retval < 0) { 2125170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2126172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2127172090Srrs SCTP_TCB_UNLOCK(stcb); 2128172090Srrs SCTP_SOCKET_LOCK(so, 1); 2129172090Srrs SCTP_TCB_LOCK(stcb); 2130172090Srrs#endif 2131171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2132172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2133172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2134172090Srrs#endif 2135172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2136163953Srrs return (NULL); 2137163953Srrs } 2138163953Srrs /* load all addresses */ 2139228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 2140163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2141163953Srrs init_src)) { 2142170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2143172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2144172090Srrs SCTP_TCB_UNLOCK(stcb); 2145172090Srrs SCTP_SOCKET_LOCK(so, 1); 2146172090Srrs SCTP_TCB_LOCK(stcb); 2147172090Srrs#endif 2148171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2149172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2150172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2151172090Srrs#endif 2152172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2153163953Srrs return (NULL); 2154163953Srrs } 2155163953Srrs /* 2156163953Srrs * verify any preceding AUTH chunk that was skipped 2157163953Srrs */ 2158163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2159163953Srrs sctp_auth_get_cookie_params(stcb, m, 2160163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2161163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2162163953Srrs if (auth_skipped) { 2163163953Srrs struct sctp_auth_chunk *auth; 2164163953Srrs 2165163953Srrs auth = (struct sctp_auth_chunk *) 2166166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2167169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2168163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2169169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2170169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2171171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2172172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2173172090Srrs SCTP_TCB_UNLOCK(stcb); 2174172090Srrs SCTP_SOCKET_LOCK(so, 1); 2175172090Srrs SCTP_TCB_LOCK(stcb); 2176172090Srrs#endif 2177171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2178172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2179172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2180172090Srrs#endif 2181172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2182163953Srrs return (NULL); 2183163953Srrs } else { 2184163953Srrs /* remaining chunks checked... good to go */ 2185163953Srrs stcb->asoc.authenticated = 1; 2186163953Srrs } 2187163953Srrs } 2188163953Srrs /* update current state */ 2189169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2190171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2191163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2192163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2193163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2194163953Srrs } 2195165220Srrs sctp_stop_all_cookie_timers(stcb); 2196163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2197163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2198163953Srrs 2199163953Srrs /* 2200163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2201163953Srrs * addresses that need to get added to the peer (eg. addresses 2202163953Srrs * changed while cookie echo in flight). This needs to be done 2203163953Srrs * after we go to the OPEN state to do the correct asconf 2204163953Srrs * processing. else, make sure we have the correct addresses in our 2205163953Srrs * lists 2206163953Srrs */ 2207163953Srrs 2208163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2209163953Srrs /* pull in local_address (our "from" address) */ 2210221249Stuexen switch (cookie->laddr_type) { 2211221249Stuexen#ifdef INET 2212221249Stuexen case SCTP_IPV4_ADDRESS: 2213163953Srrs /* source addr is IPv4 */ 2214163953Srrs sin = (struct sockaddr_in *)initack_src; 2215163953Srrs memset(sin, 0, sizeof(*sin)); 2216163953Srrs sin->sin_family = AF_INET; 2217163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2218163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2219221249Stuexen break; 2220221249Stuexen#endif 2221221249Stuexen#ifdef INET6 2222221249Stuexen case SCTP_IPV6_ADDRESS: 2223163953Srrs /* source addr is IPv6 */ 2224163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2225163953Srrs memset(sin6, 0, sizeof(*sin6)); 2226163953Srrs sin6->sin6_family = AF_INET6; 2227163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2228163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2229163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2230163953Srrs sizeof(sin6->sin6_addr)); 2231221249Stuexen break; 2232221249Stuexen#endif 2233221249Stuexen default: 2234170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2235172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2236172090Srrs SCTP_TCB_UNLOCK(stcb); 2237172090Srrs SCTP_SOCKET_LOCK(so, 1); 2238172090Srrs SCTP_TCB_LOCK(stcb); 2239172090Srrs#endif 2240171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2241172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2242172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2243172090Srrs#endif 2244172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2245163953Srrs return (NULL); 2246163953Srrs } 2247163953Srrs 2248163953Srrs /* set up to notify upper layer */ 2249163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2250163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2251163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2252163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2253163953Srrs /* 2254163953Srrs * This is an endpoint that called connect() how it got a 2255163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2256163953Srrs * the INIT was sent, but before it got there.. a complete 2257163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2258163953Srrs * should have went to the other code.. not here.. oh well.. 2259163953Srrs * a bit of protection is worth having.. 2260163953Srrs */ 2261163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2262172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2263172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2264172090Srrs SCTP_TCB_UNLOCK(stcb); 2265172090Srrs SCTP_SOCKET_LOCK(so, 1); 2266172090Srrs SCTP_TCB_LOCK(stcb); 2267172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2268172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2269172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2270172090Srrs return (NULL); 2271172090Srrs } 2272172090Srrs#endif 2273172090Srrs soisconnected(stcb->sctp_socket); 2274172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2275172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2276172090Srrs#endif 2277163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2278163953Srrs (inp->sctp_socket->so_qlimit)) { 2279163953Srrs /* 2280163953Srrs * We don't want to do anything with this one. Since it is 2281163953Srrs * the listening guy. The timer will get started for 2282163953Srrs * accepted connections in the caller. 2283163953Srrs */ 2284163953Srrs ; 2285163953Srrs } 2286163953Srrs /* since we did not send a HB make sure we don't double things */ 2287169420Srrs if ((netp) && (*netp)) 2288169420Srrs (*netp)->hb_responded = 1; 2289163953Srrs 2290163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2291163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2292163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2293163953Srrs } 2294165220Srrs /* calculate the RTT */ 2295171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2296170642Srrs if ((netp) && (*netp)) { 2297169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2298218186Srrs &cookie->time_entered, sctp_align_unsafe_makecopy, 2299219397Srrs SCTP_RTT_FROM_NON_DATA); 2300170642Srrs } 2301172190Srrs /* respond with a COOKIE-ACK */ 2302163953Srrs sctp_send_cookie_ack(stcb); 2303172190Srrs 2304172190Srrs /* 2305172190Srrs * check the address lists for any ASCONFs that need to be sent 2306172190Srrs * AFTER the cookie-ack is sent 2307172190Srrs */ 2308172190Srrs sctp_check_address_list(stcb, m, 2309172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2310172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2311172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2312172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2313172190Srrs 2314172190Srrs 2315163953Srrs return (stcb); 2316163953Srrs} 2317163953Srrs 2318185694Srrs/* 2319185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2320185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2321185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2322185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2323185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2324185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2325185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2326185694Srrs -- SEND ABORT - TRY AGAIN -- 2327185694Srrs } 2328185694Srrs } 2329185694Srrs*/ 2330163953Srrs 2331163953Srrs/* 2332163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2333163953Srrs * existing (non-NULL) TCB 2334163953Srrs */ 2335163953Srrsstatic struct mbuf * 2336163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2337163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2338163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2339169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2340179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2341163953Srrs{ 2342163953Srrs struct sctp_state_cookie *cookie; 2343163953Srrs struct sctp_tcb *l_stcb = *stcb; 2344163953Srrs struct sctp_inpcb *l_inp; 2345163953Srrs struct sockaddr *to; 2346163953Srrs struct sctp_pcb *ep; 2347163953Srrs struct mbuf *m_sig; 2348163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2349163953Srrs uint8_t *sig; 2350163953Srrs uint8_t cookie_ok = 0; 2351163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2352163953Srrs unsigned int cookie_len; 2353163953Srrs struct timeval now; 2354163953Srrs struct timeval time_expires; 2355163953Srrs struct sockaddr_storage dest_store; 2356163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2357163953Srrs struct ip *iph; 2358163953Srrs int notification = 0; 2359163953Srrs struct sctp_nets *netl; 2360163953Srrs int had_a_existing_tcb = 0; 2361216887Stuexen int send_int_conf = 0; 2362163953Srrs 2363221249Stuexen#ifdef INET 2364221249Stuexen struct sockaddr_in sin; 2365221249Stuexen 2366221249Stuexen#endif 2367221249Stuexen#ifdef INET6 2368221249Stuexen struct sockaddr_in6 sin6; 2369221249Stuexen 2370221249Stuexen#endif 2371221249Stuexen 2372169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2373169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2374163953Srrs 2375163953Srrs if (inp_p == NULL) { 2376163953Srrs return (NULL); 2377163953Srrs } 2378163953Srrs /* First get the destination address setup too. */ 2379163953Srrs iph = mtod(m, struct ip *); 2380178251Srrs switch (iph->ip_v) { 2381221249Stuexen#ifdef INET 2382178251Srrs case IPVERSION: 2383178251Srrs { 2384178251Srrs /* its IPv4 */ 2385178251Srrs struct sockaddr_in *lsin; 2386163953Srrs 2387178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2388178251Srrs memset(lsin, 0, sizeof(*lsin)); 2389178251Srrs lsin->sin_family = AF_INET; 2390178251Srrs lsin->sin_len = sizeof(*lsin); 2391178251Srrs lsin->sin_port = sh->dest_port; 2392178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2393178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2394178251Srrs break; 2395178251Srrs } 2396221249Stuexen#endif 2397178251Srrs#ifdef INET6 2398178251Srrs case IPV6_VERSION >> 4: 2399178251Srrs { 2400178251Srrs /* its IPv6 */ 2401178251Srrs struct ip6_hdr *ip6; 2402178251Srrs struct sockaddr_in6 *lsin6; 2403163953Srrs 2404178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2405178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2406178251Srrs lsin6->sin6_family = AF_INET6; 2407178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2408178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2409178251Srrs lsin6->sin6_port = sh->dest_port; 2410178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2411178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2412178251Srrs break; 2413178251Srrs } 2414178251Srrs#endif 2415178251Srrs default: 2416163953Srrs return (NULL); 2417163953Srrs } 2418163953Srrs 2419163953Srrs cookie = &cp->cookie; 2420163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2421163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2422163953Srrs 2423163953Srrs if ((cookie->peerport != sh->src_port) && 2424163953Srrs (cookie->myport != sh->dest_port) && 2425163953Srrs (cookie->my_vtag != sh->v_tag)) { 2426163953Srrs /* 2427163953Srrs * invalid ports or bad tag. Note that we always leave the 2428163953Srrs * v_tag in the header in network order and when we stored 2429163953Srrs * it in the my_vtag slot we also left it in network order. 2430169352Srrs * This maintains the match even though it may be in the 2431163953Srrs * opposite byte order of the machine :-> 2432163953Srrs */ 2433163953Srrs return (NULL); 2434163953Srrs } 2435163953Srrs if (cookie_len > size_of_pkt || 2436163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2437163953Srrs sizeof(struct sctp_init_chunk) + 2438163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2439163953Srrs /* cookie too long! or too small */ 2440163953Srrs return (NULL); 2441163953Srrs } 2442163953Srrs /* 2443163953Srrs * split off the signature into its own mbuf (since it should not be 2444163953Srrs * calculated in the sctp_hmac_m() call). 2445163953Srrs */ 2446163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2447163953Srrs if (sig_offset > size_of_pkt) { 2448163953Srrs /* packet not correct size! */ 2449163953Srrs /* XXX this may already be accounted for earlier... */ 2450163953Srrs return (NULL); 2451163953Srrs } 2452163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2453163953Srrs if (m_sig == NULL) { 2454163953Srrs /* out of memory or ?? */ 2455163953Srrs return (NULL); 2456163953Srrs } 2457178202Srrs#ifdef SCTP_MBUF_LOGGING 2458179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2459178202Srrs struct mbuf *mat; 2460178202Srrs 2461234461Stuexen for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) { 2462178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2463178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2464178202Srrs } 2465178202Srrs } 2466178202Srrs } 2467178202Srrs#endif 2468178202Srrs 2469163953Srrs /* 2470163953Srrs * compute the signature/digest for the cookie 2471163953Srrs */ 2472163953Srrs ep = &(*inp_p)->sctp_ep; 2473163953Srrs l_inp = *inp_p; 2474163953Srrs if (l_stcb) { 2475163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2476163953Srrs } 2477163953Srrs SCTP_INP_RLOCK(l_inp); 2478163953Srrs if (l_stcb) { 2479163953Srrs SCTP_TCB_LOCK(l_stcb); 2480163953Srrs } 2481163953Srrs /* which cookie is it? */ 2482163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2483163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2484163953Srrs /* it's the old cookie */ 2485169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2486163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2487170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2488163953Srrs } else { 2489163953Srrs /* it's the current cookie */ 2490169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2491163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2492170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2493163953Srrs } 2494163953Srrs /* get the signature */ 2495163953Srrs SCTP_INP_RUNLOCK(l_inp); 2496163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2497163953Srrs if (sig == NULL) { 2498163953Srrs /* couldn't find signature */ 2499164085Srrs sctp_m_freem(m_sig); 2500163953Srrs return (NULL); 2501163953Srrs } 2502163953Srrs /* compare the received digest with the computed digest */ 2503163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2504163953Srrs /* try the old cookie? */ 2505163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2506163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2507163953Srrs /* compute digest with old */ 2508169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2509163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2510170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2511163953Srrs /* compare */ 2512163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2513163953Srrs cookie_ok = 1; 2514163953Srrs } 2515163953Srrs } else { 2516163953Srrs cookie_ok = 1; 2517163953Srrs } 2518163953Srrs 2519163953Srrs /* 2520163953Srrs * Now before we continue we must reconstruct our mbuf so that 2521163953Srrs * normal processing of any other chunks will work. 2522163953Srrs */ 2523163953Srrs { 2524163953Srrs struct mbuf *m_at; 2525163953Srrs 2526163953Srrs m_at = m; 2527165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2528165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2529163953Srrs } 2530165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2531163953Srrs } 2532163953Srrs 2533163953Srrs if (cookie_ok == 0) { 2534169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2535169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2536169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2537169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2538163953Srrs return (NULL); 2539163953Srrs } 2540163953Srrs /* 2541163953Srrs * check the cookie timestamps to be sure it's not stale 2542163953Srrs */ 2543169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2544163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2545169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2546163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2547180387Srrs /* 2548180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2549180387Srrs * is undefined. 2550180387Srrs */ 2551163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2552163953Srrs /* cookie is stale! */ 2553163953Srrs struct mbuf *op_err; 2554163953Srrs struct sctp_stale_cookie_msg *scm; 2555163953Srrs uint32_t tim; 2556163953Srrs 2557163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2558165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2559163953Srrs if (op_err == NULL) { 2560163953Srrs /* FOOBAR */ 2561163953Srrs return (NULL); 2562163953Srrs } 2563163953Srrs /* Set the len */ 2564165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2565163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2566163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2567163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2568163953Srrs (sizeof(uint32_t)))); 2569163953Srrs /* seconds to usec */ 2570163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2571163953Srrs /* add in usec */ 2572163953Srrs if (tim == 0) 2573163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2574163953Srrs scm->time_usec = htonl(tim); 2575169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2576179157Srrs vrf_id, port); 2577163953Srrs return (NULL); 2578163953Srrs } 2579163953Srrs /* 2580163953Srrs * Now we must see with the lookup address if we have an existing 2581163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2582163953Srrs * and a INIT collided with us and somewhere the peer sent the 2583163953Srrs * cookie on another address besides the single address our assoc 2584163953Srrs * had for him. In this case we will have one of the tie-tags set at 2585163953Srrs * least AND the address field in the cookie can be used to look it 2586163953Srrs * up. 2587163953Srrs */ 2588163953Srrs to = NULL; 2589221249Stuexen switch (cookie->addr_type) { 2590221249Stuexen#ifdef INET6 2591221249Stuexen case SCTP_IPV6_ADDRESS: 2592163953Srrs memset(&sin6, 0, sizeof(sin6)); 2593163953Srrs sin6.sin6_family = AF_INET6; 2594163953Srrs sin6.sin6_len = sizeof(sin6); 2595163953Srrs sin6.sin6_port = sh->src_port; 2596163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2597163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2598163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2599163953Srrs to = (struct sockaddr *)&sin6; 2600221249Stuexen break; 2601221249Stuexen#endif 2602221249Stuexen#ifdef INET 2603221249Stuexen case SCTP_IPV4_ADDRESS: 2604163953Srrs memset(&sin, 0, sizeof(sin)); 2605163953Srrs sin.sin_family = AF_INET; 2606163953Srrs sin.sin_len = sizeof(sin); 2607163953Srrs sin.sin_port = sh->src_port; 2608163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2609163953Srrs to = (struct sockaddr *)&sin; 2610221249Stuexen break; 2611221249Stuexen#endif 2612221249Stuexen default: 2613169420Srrs /* This should not happen */ 2614169420Srrs return (NULL); 2615163953Srrs } 2616163953Srrs if ((*stcb == NULL) && to) { 2617163953Srrs /* Yep, lets check */ 2618163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2619163953Srrs if (*stcb == NULL) { 2620163953Srrs /* 2621163953Srrs * We should have only got back the same inp. If we 2622163953Srrs * got back a different ep we have a problem. The 2623163953Srrs * original findep got back l_inp and now 2624163953Srrs */ 2625163953Srrs if (l_inp != *inp_p) { 2626169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2627163953Srrs } 2628165220Srrs } else { 2629165220Srrs if (*locked_tcb == NULL) { 2630165220Srrs /* 2631165220Srrs * In this case we found the assoc only 2632165220Srrs * after we locked the create lock. This 2633165220Srrs * means we are in a colliding case and we 2634165220Srrs * must make sure that we unlock the tcb if 2635165220Srrs * its one of the cases where we throw away 2636165220Srrs * the incoming packets. 2637165220Srrs */ 2638165220Srrs *locked_tcb = *stcb; 2639165220Srrs 2640165220Srrs /* 2641165220Srrs * We must also increment the inp ref count 2642165220Srrs * since the ref_count flags was set when we 2643165220Srrs * did not find the TCB, now we found it 2644165220Srrs * which reduces the refcount.. we must 2645165220Srrs * raise it back out to balance it all :-) 2646165220Srrs */ 2647165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2648165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2649169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2650165220Srrs (*stcb)->sctp_ep, l_inp); 2651165220Srrs } 2652165220Srrs } 2653163953Srrs } 2654163953Srrs } 2655188067Srrs if (to == NULL) { 2656169420Srrs return (NULL); 2657188067Srrs } 2658163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2659163953Srrs if (*stcb == NULL) { 2660163953Srrs /* this is the "normal" case... get a new TCB */ 2661163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2662163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2663179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2664163953Srrs } else { 2665163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2666163953Srrs had_a_existing_tcb = 1; 2667163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2668185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2669228653Stuexen ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port); 2670163953Srrs } 2671163953Srrs 2672163953Srrs if (*stcb == NULL) { 2673163953Srrs /* still no TCB... must be bad cookie-echo */ 2674163953Srrs return (NULL); 2675163953Srrs } 2676218335Stuexen if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2677218335Stuexen (*netp)->flowid = m->m_pkthdr.flowid; 2678218400Stuexen#ifdef INVARIANTS 2679218392Srrs (*netp)->flowidset = 1; 2680218400Stuexen#endif 2681218335Stuexen } 2682163953Srrs /* 2683163953Srrs * Ok, we built an association so confirm the address we sent the 2684163953Srrs * INIT-ACK to. 2685163953Srrs */ 2686163953Srrs netl = sctp_findnet(*stcb, to); 2687163953Srrs /* 2688163953Srrs * This code should in theory NOT run but 2689163953Srrs */ 2690163953Srrs if (netl == NULL) { 2691163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2692228907Stuexen if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2693228907Stuexen return (NULL); 2694228907Stuexen } 2695163953Srrs netl = sctp_findnet(*stcb, to); 2696163953Srrs } 2697163953Srrs if (netl) { 2698163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2699163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2700169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2701163953Srrs netl); 2702216887Stuexen send_int_conf = 1; 2703163953Srrs } 2704163953Srrs } 2705224641Stuexen sctp_start_net_timers(*stcb); 2706163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2707163953Srrs if (!had_a_existing_tcb || 2708163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2709163953Srrs /* 2710163953Srrs * If we have a NEW cookie or the connect never 2711163953Srrs * reached the connected state during collision we 2712163953Srrs * must do the TCP accept thing. 2713163953Srrs */ 2714163953Srrs struct socket *so, *oso; 2715163953Srrs struct sctp_inpcb *inp; 2716163953Srrs 2717163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2718163953Srrs /* 2719163953Srrs * For a restart we will keep the same 2720163953Srrs * socket, no need to do anything. I THINK!! 2721163953Srrs */ 2722228653Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2723216887Stuexen if (send_int_conf) { 2724216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2725216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2726216887Stuexen } 2727163953Srrs return (m); 2728163953Srrs } 2729163953Srrs oso = (*inp_p)->sctp_socket; 2730171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2731163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2732218757Sbz CURVNET_SET(oso->so_vnet); 2733166086Srrs so = sonewconn(oso, 0 2734163953Srrs ); 2735218757Sbz CURVNET_RESTORE(); 2736163953Srrs SCTP_TCB_LOCK((*stcb)); 2737171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2738171858Srrs 2739163953Srrs if (so == NULL) { 2740163953Srrs struct mbuf *op_err; 2741163953Srrs 2742172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2743172090Srrs struct socket *pcb_so; 2744172090Srrs 2745172090Srrs#endif 2746163953Srrs /* Too many sockets */ 2747169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2748163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2749163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2750179157Srrs sh, op_err, vrf_id, port); 2751172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2752172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2753172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2754172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2755172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2756172090Srrs SCTP_TCB_LOCK((*stcb)); 2757172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2758172090Srrs#endif 2759171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2760172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2761172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2762172090Srrs#endif 2763163953Srrs return (NULL); 2764163953Srrs } 2765163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2766166086Srrs SCTP_INP_INCR_REF(inp); 2767166086Srrs /* 2768166086Srrs * We add the unbound flag here so that if we get an 2769166086Srrs * soabort() before we get the move_pcb done, we 2770166086Srrs * will properly cleanup. 2771166086Srrs */ 2772163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2773163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2774163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2775166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2776163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2777163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2778163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2779172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2780163953Srrs inp->sctp_socket = so; 2781163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2782211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2783218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2784163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2785163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2786233660Srrs inp->local_strreset_support = (*inp_p)->local_strreset_support; 2787163953Srrs inp->inp_starting_point_for_iterator = NULL; 2788163953Srrs /* 2789163953Srrs * copy in the authentication parameters from the 2790163953Srrs * original endpoint 2791163953Srrs */ 2792163953Srrs if (inp->sctp_ep.local_hmacs) 2793163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2794163953Srrs inp->sctp_ep.local_hmacs = 2795163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2796163953Srrs if (inp->sctp_ep.local_auth_chunks) 2797163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2798163953Srrs inp->sctp_ep.local_auth_chunks = 2799163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2800163953Srrs 2801163953Srrs /* 2802163953Srrs * Now we must move it from one hash table to 2803163953Srrs * another and get the tcb in the right place. 2804163953Srrs */ 2805207924Srrs 2806207924Srrs /* 2807207924Srrs * This is where the one-2-one socket is put into 2808207924Srrs * the accept state waiting for the accept! 2809207924Srrs */ 2810207924Srrs if (*stcb) { 2811207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2812207924Srrs } 2813163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2814170056Srrs 2815170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2816170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2817170056Srrs 2818175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2819175845Srwatson 0); 2820170056Srrs SCTP_TCB_LOCK((*stcb)); 2821170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2822163953Srrs 2823170056Srrs 2824166086Srrs /* 2825166086Srrs * now we must check to see if we were aborted while 2826166086Srrs * the move was going on and the lock/unlock 2827166086Srrs * happened. 2828166086Srrs */ 2829166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2830166086Srrs /* 2831166086Srrs * yep it was, we leave the assoc attached 2832166086Srrs * to the socket since the sctp_inpcb_free() 2833166086Srrs * call will send an abort for us. 2834166086Srrs */ 2835166086Srrs SCTP_INP_DECR_REF(inp); 2836166086Srrs return (NULL); 2837166086Srrs } 2838166086Srrs SCTP_INP_DECR_REF(inp); 2839163953Srrs /* Switch over to the new guy */ 2840163953Srrs *inp_p = inp; 2841172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2842216887Stuexen if (send_int_conf) { 2843216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2844216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2845216887Stuexen } 2846166086Srrs /* 2847166086Srrs * Pull it from the incomplete queue and wake the 2848166086Srrs * guy 2849166086Srrs */ 2850172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2851172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2852172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2853172090Srrs SCTP_SOCKET_LOCK(so, 1); 2854172090Srrs#endif 2855166086Srrs soisconnected(so); 2856172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2857172118Srrs SCTP_TCB_LOCK((*stcb)); 2858172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2859172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2860172090Srrs#endif 2861163953Srrs return (m); 2862163953Srrs } 2863163953Srrs } 2864216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2865216887Stuexen if (notification) { 2866216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2867216887Stuexen } 2868216887Stuexen if (send_int_conf) { 2869216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2870216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2871216887Stuexen } 2872163953Srrs } 2873163953Srrs return (m); 2874163953Srrs} 2875163953Srrs 2876163953Srrsstatic void 2877228653Stuexensctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2878163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2879163953Srrs{ 2880163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2881163953Srrs struct sctp_association *asoc; 2882163953Srrs 2883169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2884169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2885163953Srrs if (stcb == NULL) 2886163953Srrs return; 2887163953Srrs 2888163953Srrs asoc = &stcb->asoc; 2889163953Srrs 2890163953Srrs sctp_stop_all_cookie_timers(stcb); 2891163953Srrs /* process according to association state */ 2892163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2893163953Srrs /* state change only needed when I am in right state */ 2894169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2895171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2896224641Stuexen sctp_start_net_timers(stcb); 2897163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2898163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2899163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2900163953Srrs 2901163953Srrs } 2902163953Srrs /* update RTO */ 2903163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2904163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2905163953Srrs if (asoc->overall_error_count == 0) { 2906163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2907218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2908219397Srrs SCTP_RTT_FROM_NON_DATA); 2909163953Srrs } 2910169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2911172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2912163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2913163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2914172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2915172090Srrs struct socket *so; 2916172090Srrs 2917172090Srrs#endif 2918163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2919172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2920172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2921172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2922172090Srrs SCTP_TCB_UNLOCK(stcb); 2923172090Srrs SCTP_SOCKET_LOCK(so, 1); 2924172090Srrs SCTP_TCB_LOCK(stcb); 2925172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2926218641Srrs#endif 2927218641Srrs if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2928218641Srrs soisconnected(stcb->sctp_socket); 2929172090Srrs } 2930172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2931172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2932172090Srrs#endif 2933163953Srrs } 2934163953Srrs /* 2935163953Srrs * since we did not send a HB make sure we don't double 2936163953Srrs * things 2937163953Srrs */ 2938163953Srrs net->hb_responded = 1; 2939163953Srrs 2940218641Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2941218641Srrs /* 2942218641Srrs * We don't need to do the asconf thing, nor hb or 2943218641Srrs * autoclose if the socket is closed. 2944218641Srrs */ 2945218641Srrs goto closed_socket; 2946218641Srrs } 2947218641Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2948218641Srrs stcb, net); 2949218641Srrs 2950218641Srrs 2951163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2952163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2953163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2954163953Srrs stcb->sctp_ep, stcb, NULL); 2955163953Srrs } 2956163953Srrs /* 2957171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2958171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2959171572Srrs * in flight) 2960163953Srrs */ 2961163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2962163953Srrs (stcb->asoc.peer_supports_asconf) && 2963163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2964171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2965163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2966163953Srrs stcb->sctp_ep, stcb, 2967163953Srrs stcb->asoc.primary_destination); 2968171572Srrs#else 2969172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2970172190Srrs SCTP_ADDR_NOT_LOCKED); 2971171572Srrs#endif 2972163953Srrs } 2973163953Srrs } 2974218641Srrsclosed_socket: 2975163953Srrs /* Toss the cookie if I can */ 2976163953Srrs sctp_toss_old_cookies(stcb, asoc); 2977163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2978163953Srrs /* Restart the timer if we have pending data */ 2979163953Srrs struct sctp_tmit_chunk *chk; 2980163953Srrs 2981163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2982216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2983163953Srrs } 2984163953Srrs} 2985163953Srrs 2986163953Srrsstatic void 2987163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2988163953Srrs struct sctp_tcb *stcb) 2989163953Srrs{ 2990163953Srrs struct sctp_nets *net; 2991163953Srrs struct sctp_tmit_chunk *lchk; 2992218072Srrs struct sctp_ecne_chunk bkup; 2993228907Stuexen uint8_t override_bit; 2994218072Srrs uint32_t tsn, window_data_tsn; 2995218072Srrs int len; 2996218232Srrs unsigned int pkt_cnt; 2997163953Srrs 2998218072Srrs len = ntohs(cp->ch.chunk_length); 2999218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 3000218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 3001163953Srrs return; 3002163953Srrs } 3003218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 3004218072Srrs /* Its the old format */ 3005218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3006218072Srrs bkup.num_pkts_since_cwr = htonl(1); 3007218072Srrs cp = &bkup; 3008218072Srrs } 3009163953Srrs SCTP_STAT_INCR(sctps_recvecne); 3010163953Srrs tsn = ntohl(cp->tsn); 3011218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3012218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3013163953Srrs if (lchk == NULL) { 3014218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 3015163953Srrs } else { 3016218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 3017163953Srrs } 3018163953Srrs 3019218072Srrs /* Find where it was sent to if possible. */ 3020163953Srrs net = NULL; 3021216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3022163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 3023163953Srrs net = lchk->whoTo; 3024218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3025163953Srrs break; 3026163953Srrs } 3027216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3028163953Srrs break; 3029216825Stuexen } 3030163953Srrs } 3031218072Srrs if (net == NULL) { 3032163953Srrs /* 3033218072Srrs * What to do. A previous send of a CWR was possibly lost. 3034218072Srrs * See how old it is, we may have it marked on the actual 3035218072Srrs * net. 3036218072Srrs */ 3037218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3038218072Srrs if (tsn == net->last_cwr_tsn) { 3039218072Srrs /* Found him, send it off */ 3040228907Stuexen break; 3041218072Srrs } 3042218072Srrs } 3043228907Stuexen if (net == NULL) { 3044228907Stuexen /* 3045228907Stuexen * If we reach here, we need to send a special CWR 3046228907Stuexen * that says hey, we did this a long time ago and 3047228907Stuexen * you lost the response. 3048228907Stuexen */ 3049228907Stuexen net = TAILQ_FIRST(&stcb->asoc.nets); 3050228907Stuexen if (net == NULL) { 3051228907Stuexen /* TSNH */ 3052228907Stuexen return; 3053228907Stuexen } 3054228907Stuexen override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3055228907Stuexen } else { 3056228907Stuexen override_bit = 0; 3057228907Stuexen } 3058228907Stuexen } else { 3059228907Stuexen override_bit = 0; 3060218072Srrs } 3061218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3062218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3063218072Srrs /* 3064171440Srrs * JRS - Use the congestion control given in the pluggable 3065171440Srrs * CC module 3066171440Srrs */ 3067218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3068171440Srrs /* 3069218072Srrs * We reduce once every RTT. So we will only lower cwnd at 3070218072Srrs * the next sending seq i.e. the window_data_tsn 3071163953Srrs */ 3072218072Srrs net->cwr_window_tsn = window_data_tsn; 3073218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3074218072Srrs net->lost_cnt = pkt_cnt; 3075218072Srrs net->last_cwr_tsn = tsn; 3076218072Srrs } else { 3077218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3078218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3079218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3080218072Srrs /* 3081218129Srrs * Another loss in the same window update how many 3082218129Srrs * marks/packets lost we have had. 3083218072Srrs */ 3084218129Srrs int cnt = 1; 3085218072Srrs 3086218072Srrs if (pkt_cnt > net->lost_cnt) { 3087218072Srrs /* Should be the case */ 3088218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3089218129Srrs net->ecn_ce_pkt_cnt += cnt; 3090218072Srrs } 3091218129Srrs net->lost_cnt = pkt_cnt; 3092218072Srrs net->last_cwr_tsn = tsn; 3093218129Srrs /* 3094218129Srrs * Most CC functions will ignore this call, since we 3095218129Srrs * are in-window yet of the initial CE the peer saw. 3096218129Srrs */ 3097218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3098218072Srrs } 3099163953Srrs } 3100163953Srrs /* 3101163953Srrs * We always send a CWR this way if our previous one was lost our 3102163953Srrs * peer will get an update, or if it is not time again to reduce we 3103218072Srrs * still get the cwr to the peer. Note we set the override when we 3104218072Srrs * could not find the TSN on the chunk or the destination network. 3105163953Srrs */ 3106218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3107163953Srrs} 3108163953Srrs 3109163953Srrsstatic void 3110218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3111163953Srrs{ 3112163953Srrs /* 3113163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3114163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3115163953Srrs * If so remove it. 3116163953Srrs */ 3117163953Srrs struct sctp_tmit_chunk *chk; 3118163953Srrs struct sctp_ecne_chunk *ecne; 3119218072Srrs int override; 3120218072Srrs uint32_t cwr_tsn; 3121163953Srrs 3122218072Srrs cwr_tsn = ntohl(cp->tsn); 3123218072Srrs 3124218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3125163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3126163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3127163953Srrs continue; 3128163953Srrs } 3129218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3130218072Srrs /* Must be from the right src unless override is set */ 3131218072Srrs continue; 3132218072Srrs } 3133163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3134218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3135163953Srrs /* this covers this ECNE, we can remove it */ 3136163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3137163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3138163953Srrs sctp_next); 3139163953Srrs if (chk->data) { 3140163953Srrs sctp_m_freem(chk->data); 3141163953Srrs chk->data = NULL; 3142163953Srrs } 3143163953Srrs stcb->asoc.ctrl_queue_cnt--; 3144221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3145218072Srrs if (override == 0) { 3146218072Srrs break; 3147218072Srrs } 3148163953Srrs } 3149163953Srrs } 3150163953Srrs} 3151163953Srrs 3152163953Srrsstatic void 3153228653Stuexensctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3154163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3155163953Srrs{ 3156163953Srrs struct sctp_association *asoc; 3157163953Srrs 3158172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3159172090Srrs struct socket *so; 3160172090Srrs 3161172090Srrs#endif 3162172090Srrs 3163169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3164169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3165163953Srrs if (stcb == NULL) 3166163953Srrs return; 3167163953Srrs 3168163953Srrs asoc = &stcb->asoc; 3169163953Srrs /* process according to association state */ 3170163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3171163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3172171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3173171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3174163953Srrs SCTP_TCB_UNLOCK(stcb); 3175163953Srrs return; 3176163953Srrs } 3177163953Srrs /* notify upper layer protocol */ 3178163953Srrs if (stcb->sctp_socket) { 3179172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3180163953Srrs /* are the queues empty? they should be */ 3181163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3182163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3183217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3184235416Stuexen sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED); 3185163953Srrs } 3186163953Srrs } 3187163953Srrs /* stop the timer */ 3188178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3189163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3190163953Srrs /* free the TCB */ 3191171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3192171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3193172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3194172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3195172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3196172090Srrs SCTP_TCB_UNLOCK(stcb); 3197172090Srrs SCTP_SOCKET_LOCK(so, 1); 3198172090Srrs SCTP_TCB_LOCK(stcb); 3199172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3200172090Srrs#endif 3201171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3202172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3203172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3204172090Srrs#endif 3205163953Srrs return; 3206163953Srrs} 3207163953Srrs 3208163953Srrsstatic int 3209163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3210163953Srrs struct sctp_nets *net, uint8_t flg) 3211163953Srrs{ 3212163953Srrs switch (desc->chunk_type) { 3213211969Stuexen case SCTP_DATA: 3214163953Srrs /* find the tsn to resend (possibly */ 3215163953Srrs { 3216163953Srrs uint32_t tsn; 3217163953Srrs struct sctp_tmit_chunk *tp1; 3218163953Srrs 3219163953Srrs tsn = ntohl(desc->tsn_ifany); 3220216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3221163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3222163953Srrs /* found it */ 3223163953Srrs break; 3224163953Srrs } 3225216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3226163953Srrs /* not found */ 3227163953Srrs tp1 = NULL; 3228163953Srrs break; 3229163953Srrs } 3230163953Srrs } 3231163953Srrs if (tp1 == NULL) { 3232163953Srrs /* 3233163953Srrs * Do it the other way , aka without paying 3234163953Srrs * attention to queue seq order. 3235163953Srrs */ 3236163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3237216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3238163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3239163953Srrs /* found it */ 3240163953Srrs break; 3241163953Srrs } 3242163953Srrs } 3243163953Srrs } 3244163953Srrs if (tp1 == NULL) { 3245163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3246163953Srrs } 3247163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3248163953Srrs uint8_t *ddp; 3249163953Srrs 3250214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3251214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3252214877Stuexen return (0); 3253214877Stuexen } 3254163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3255163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3256163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3257163953Srrs return (0); 3258163953Srrs } 3259163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3260163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3261163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3262163953Srrs return (0); 3263163953Srrs } 3264163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3265163953Srrs sizeof(struct sctp_data_chunk)); 3266163953Srrs { 3267163953Srrs unsigned int iii; 3268163953Srrs 3269163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3270163953Srrs iii++) { 3271163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3272163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3273163953Srrs return (-1); 3274163953Srrs } 3275163953Srrs } 3276163953Srrs } 3277163953Srrs 3278163953Srrs if (tp1->do_rtt) { 3279163953Srrs /* 3280163953Srrs * this guy had a RTO calculation 3281163953Srrs * pending on it, cancel it 3282163953Srrs */ 3283219397Srrs if (tp1->whoTo->rto_needed == 0) { 3284219397Srrs tp1->whoTo->rto_needed = 1; 3285219397Srrs } 3286163953Srrs tp1->do_rtt = 0; 3287163953Srrs } 3288163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3289163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3290163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3291163953Srrs /* 3292163953Srrs * mark it as if we were doing a FR, since 3293163953Srrs * we will be getting gap ack reports behind 3294163953Srrs * the info from the router. 3295163953Srrs */ 3296163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3297163953Srrs /* 3298163953Srrs * mark the tsn with what sequences can 3299163953Srrs * cause a new FR. 3300163953Srrs */ 3301163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3302163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3303163953Srrs } else { 3304163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3305163953Srrs } 3306163953Srrs 3307163953Srrs /* restart the timer */ 3308163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3309171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3310163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3311163953Srrs stcb, tp1->whoTo); 3312163953Srrs 3313163953Srrs /* fix counts and things */ 3314179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3315170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3316170744Srrs tp1->whoTo->flight_size, 3317170744Srrs tp1->book_size, 3318170744Srrs (uintptr_t) stcb, 3319170744Srrs tp1->rec.data.TSN_seq); 3320170744Srrs } 3321190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3322190689Srrs sctp_flight_size_decrease(tp1); 3323190689Srrs sctp_total_flight_decrease(stcb, tp1); 3324190689Srrs } 3325216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3326163953Srrs } { 3327163953Srrs /* audit code */ 3328163953Srrs unsigned int audit; 3329163953Srrs 3330163953Srrs audit = 0; 3331163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3332163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3333163953Srrs audit++; 3334163953Srrs } 3335163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3336163953Srrs sctp_next) { 3337163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3338163953Srrs audit++; 3339163953Srrs } 3340163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3341169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3342163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3343163953Srrs#ifndef SCTP_AUDITING_ENABLED 3344163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3345163953Srrs#endif 3346163953Srrs } 3347163953Srrs } 3348163953Srrs } 3349163953Srrs break; 3350163953Srrs case SCTP_ASCONF: 3351163953Srrs { 3352163953Srrs struct sctp_tmit_chunk *asconf; 3353163953Srrs 3354163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3355163953Srrs sctp_next) { 3356163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3357163953Srrs break; 3358163953Srrs } 3359163953Srrs } 3360163953Srrs if (asconf) { 3361163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3362163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3363163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3364163953Srrs asconf->snd_count--; 3365163953Srrs } 3366163953Srrs } 3367163953Srrs break; 3368163953Srrs case SCTP_INITIATION: 3369163953Srrs /* resend the INIT */ 3370163953Srrs stcb->asoc.dropped_special_cnt++; 3371163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3372163953Srrs /* 3373163953Srrs * If we can get it in, in a few attempts we do 3374163953Srrs * this, otherwise we let the timer fire. 3375163953Srrs */ 3376163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3377171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3378172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3379163953Srrs } 3380163953Srrs break; 3381163953Srrs case SCTP_SELECTIVE_ACK: 3382206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3383163953Srrs /* resend the sack */ 3384221627Stuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3385163953Srrs break; 3386163953Srrs case SCTP_HEARTBEAT_REQUEST: 3387163953Srrs /* resend a demand HB */ 3388171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3389171440Srrs /* 3390171440Srrs * Only retransmit if we KNOW we wont destroy the 3391171440Srrs * tcb 3392171440Srrs */ 3393224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3394171440Srrs } 3395163953Srrs break; 3396163953Srrs case SCTP_SHUTDOWN: 3397163953Srrs sctp_send_shutdown(stcb, net); 3398163953Srrs break; 3399163953Srrs case SCTP_SHUTDOWN_ACK: 3400163953Srrs sctp_send_shutdown_ack(stcb, net); 3401163953Srrs break; 3402163953Srrs case SCTP_COOKIE_ECHO: 3403163953Srrs { 3404163953Srrs struct sctp_tmit_chunk *cookie; 3405163953Srrs 3406163953Srrs cookie = NULL; 3407163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3408163953Srrs sctp_next) { 3409163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3410163953Srrs break; 3411163953Srrs } 3412163953Srrs } 3413163953Srrs if (cookie) { 3414163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3415163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3416163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3417163953Srrs sctp_stop_all_cookie_timers(stcb); 3418163953Srrs } 3419163953Srrs } 3420163953Srrs break; 3421163953Srrs case SCTP_COOKIE_ACK: 3422163953Srrs sctp_send_cookie_ack(stcb); 3423163953Srrs break; 3424163953Srrs case SCTP_ASCONF_ACK: 3425163953Srrs /* resend last asconf ack */ 3426171990Srrs sctp_send_asconf_ack(stcb); 3427163953Srrs break; 3428163953Srrs case SCTP_FORWARD_CUM_TSN: 3429163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3430163953Srrs break; 3431163953Srrs /* can't do anything with these */ 3432163953Srrs case SCTP_PACKET_DROPPED: 3433163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3434163953Srrs case SCTP_HEARTBEAT_ACK: 3435163953Srrs case SCTP_ABORT_ASSOCIATION: 3436163953Srrs case SCTP_OPERATION_ERROR: 3437163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3438163953Srrs case SCTP_ECN_ECHO: 3439163953Srrs case SCTP_ECN_CWR: 3440163953Srrs default: 3441163953Srrs break; 3442163953Srrs } 3443163953Srrs return (0); 3444163953Srrs} 3445163953Srrs 3446163953Srrsvoid 3447163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3448163953Srrs{ 3449163953Srrs int i; 3450163953Srrs uint16_t temp; 3451163953Srrs 3452163953Srrs /* 3453163953Srrs * We set things to 0xffff since this is the last delivered sequence 3454163953Srrs * and we will be sending in 0 after the reset. 3455163953Srrs */ 3456163953Srrs 3457163953Srrs if (number_entries) { 3458163953Srrs for (i = 0; i < number_entries; i++) { 3459163953Srrs temp = ntohs(list[i]); 3460163953Srrs if (temp >= stcb->asoc.streamincnt) { 3461163953Srrs continue; 3462163953Srrs } 3463163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3464163953Srrs } 3465163953Srrs } else { 3466163953Srrs list = NULL; 3467163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3468163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3469163953Srrs } 3470163953Srrs } 3471172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3472163953Srrs} 3473163953Srrs 3474163953Srrsstatic void 3475163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3476163953Srrs{ 3477163953Srrs int i; 3478163953Srrs 3479163953Srrs if (number_entries == 0) { 3480163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3481163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3482163953Srrs } 3483163953Srrs } else if (number_entries) { 3484163953Srrs for (i = 0; i < number_entries; i++) { 3485163953Srrs uint16_t temp; 3486163953Srrs 3487163953Srrs temp = ntohs(list[i]); 3488163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3489163953Srrs /* no such stream */ 3490163953Srrs continue; 3491163953Srrs } 3492163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3493163953Srrs } 3494163953Srrs } 3495172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3496163953Srrs} 3497163953Srrs 3498163953Srrs 3499163953Srrsstruct sctp_stream_reset_out_request * 3500163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3501163953Srrs{ 3502163953Srrs struct sctp_association *asoc; 3503163953Srrs struct sctp_stream_reset_out_req *req; 3504163953Srrs struct sctp_stream_reset_out_request *r; 3505163953Srrs struct sctp_tmit_chunk *chk; 3506163953Srrs int len, clen; 3507163953Srrs 3508163953Srrs asoc = &stcb->asoc; 3509163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3510169208Srrs asoc->stream_reset_outstanding = 0; 3511163953Srrs return (NULL); 3512163953Srrs } 3513163953Srrs if (stcb->asoc.str_reset == NULL) { 3514169208Srrs asoc->stream_reset_outstanding = 0; 3515163953Srrs return (NULL); 3516163953Srrs } 3517163953Srrs chk = stcb->asoc.str_reset; 3518163953Srrs if (chk->data == NULL) { 3519163953Srrs return (NULL); 3520163953Srrs } 3521163953Srrs if (bchk) { 3522163953Srrs /* he wants a copy of the chk pointer */ 3523163953Srrs *bchk = chk; 3524163953Srrs } 3525163953Srrs clen = chk->send_size; 3526163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3527163953Srrs r = &req->sr_req; 3528163953Srrs if (ntohl(r->request_seq) == seq) { 3529163953Srrs /* found it */ 3530163953Srrs return (r); 3531163953Srrs } 3532163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3533163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3534163953Srrs /* move to the next one, there can only be a max of two */ 3535163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3536163953Srrs if (ntohl(r->request_seq) == seq) { 3537163953Srrs return (r); 3538163953Srrs } 3539163953Srrs } 3540163953Srrs /* that seq is not here */ 3541163953Srrs return (NULL); 3542163953Srrs} 3543163953Srrs 3544163953Srrsstatic void 3545163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3546163953Srrs{ 3547163953Srrs struct sctp_association *asoc; 3548163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3549163953Srrs 3550163953Srrs if (stcb->asoc.str_reset == NULL) { 3551163953Srrs return; 3552163953Srrs } 3553166675Srrs asoc = &stcb->asoc; 3554166675Srrs 3555171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3556163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3557163953Srrs chk, 3558163953Srrs sctp_next); 3559163953Srrs if (chk->data) { 3560163953Srrs sctp_m_freem(chk->data); 3561163953Srrs chk->data = NULL; 3562163953Srrs } 3563163953Srrs asoc->ctrl_queue_cnt--; 3564221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3565172156Srrs /* sa_ignore NO_NULL_CHK */ 3566163953Srrs stcb->asoc.str_reset = NULL; 3567163953Srrs} 3568163953Srrs 3569163953Srrs 3570163953Srrsstatic int 3571163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3572163953Srrs uint32_t seq, uint32_t action, 3573164139Srrs struct sctp_stream_reset_response *respin) 3574163953Srrs{ 3575163953Srrs uint16_t type; 3576163953Srrs int lparm_len; 3577163953Srrs struct sctp_association *asoc = &stcb->asoc; 3578163953Srrs struct sctp_tmit_chunk *chk; 3579163953Srrs struct sctp_stream_reset_out_request *srparam; 3580163953Srrs int number_entries; 3581163953Srrs 3582163953Srrs if (asoc->stream_reset_outstanding == 0) { 3583163953Srrs /* duplicate */ 3584163953Srrs return (0); 3585163953Srrs } 3586163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3587163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3588163953Srrs if (srparam) { 3589163953Srrs stcb->asoc.str_reset_seq_out++; 3590163953Srrs type = ntohs(srparam->ph.param_type); 3591163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3592163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3593164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3594163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3595163953Srrs if (asoc->stream_reset_outstanding) 3596163953Srrs asoc->stream_reset_outstanding--; 3597235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3598163953Srrs /* do it */ 3599163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3600235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3601235066Stuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3602163953Srrs } else { 3603172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3604163953Srrs } 3605163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3606163953Srrs /* Answered my request */ 3607164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3608163953Srrs if (asoc->stream_reset_outstanding) 3609163953Srrs asoc->stream_reset_outstanding--; 3610235066Stuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3611235066Stuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, 3612235066Stuexen number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3613235066Stuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3614233660Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, 3615233660Srrs number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3616163953Srrs } 3617233660Srrs } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { 3618188854Srrs /* Ok we now may have more streams */ 3619233660Srrs int num_stream; 3620233660Srrs 3621233660Srrs num_stream = stcb->asoc.strm_pending_add_size; 3622233660Srrs if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { 3623233660Srrs /* TSNH */ 3624233660Srrs num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; 3625233660Srrs } 3626233660Srrs stcb->asoc.strm_pending_add_size = 0; 3627189121Srrs if (asoc->stream_reset_outstanding) 3628189121Srrs asoc->stream_reset_outstanding--; 3629235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3630188854Srrs /* Put the new streams into effect */ 3631233660Srrs stcb->asoc.streamoutcnt += num_stream; 3632233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3633235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3634235066Stuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3635235066Stuexen SCTP_STREAM_CHANGE_DENIED); 3636188854Srrs } else { 3637233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3638235066Stuexen SCTP_STREAM_CHANGE_FAILED); 3639188854Srrs } 3640233660Srrs } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { 3641233660Srrs if (asoc->stream_reset_outstanding) 3642233660Srrs asoc->stream_reset_outstanding--; 3643235066Stuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3644233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3645235066Stuexen SCTP_STREAM_CHANGE_DENIED); 3646235066Stuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3647235066Stuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3648235066Stuexen SCTP_STREAM_CHANGE_FAILED); 3649233660Srrs } 3650163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3651163953Srrs /** 3652163953Srrs * a) Adopt the new in tsn. 3653163953Srrs * b) reset the map 3654163953Srrs * c) Adopt the new out-tsn 3655163953Srrs */ 3656163953Srrs struct sctp_stream_reset_response_tsn *resp; 3657163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3658163953Srrs int abort_flag = 0; 3659163953Srrs 3660163953Srrs if (respin == NULL) { 3661163953Srrs /* huh ? */ 3662163953Srrs return (0); 3663163953Srrs } 3664235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3665163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3666163953Srrs asoc->stream_reset_outstanding--; 3667163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3668163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3669163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3670170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3671163953Srrs if (abort_flag) { 3672163953Srrs return (1); 3673163953Srrs } 3674163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3675179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3676179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3677179783Srrs } 3678180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3679163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3680163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3681185694Srrs 3682205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3683206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3684205627Srrs 3685163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3686163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3687163953Srrs 3688163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3689163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3690233660Srrs sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); 3691235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3692235066Stuexen sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3693235066Stuexen SCTP_ASSOC_RESET_DENIED); 3694233660Srrs } else { 3695233660Srrs sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3696235066Stuexen SCTP_ASSOC_RESET_FAILED); 3697163953Srrs } 3698163953Srrs } 3699163953Srrs /* get rid of the request and get the request flags */ 3700163953Srrs if (asoc->stream_reset_outstanding == 0) { 3701163953Srrs sctp_clean_up_stream_reset(stcb); 3702163953Srrs } 3703163953Srrs } 3704163953Srrs } 3705163953Srrs return (0); 3706163953Srrs} 3707163953Srrs 3708163953Srrsstatic void 3709163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3710163953Srrs struct sctp_tmit_chunk *chk, 3711170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3712163953Srrs{ 3713163953Srrs uint32_t seq; 3714163953Srrs int len, i; 3715163953Srrs int number_entries; 3716163953Srrs uint16_t temp; 3717163953Srrs 3718163953Srrs /* 3719163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3720163953Srrs * seq_in is right. 3721163953Srrs */ 3722163953Srrs struct sctp_association *asoc = &stcb->asoc; 3723163953Srrs 3724163953Srrs seq = ntohl(req->request_seq); 3725163953Srrs if (asoc->str_reset_seq_in == seq) { 3726235064Stuexen asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3727235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3728235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3729235064Stuexen } else if (trunc) { 3730170992Srrs /* Can't do it, since they exceeded our buffer size */ 3731235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3732170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3733163953Srrs len = ntohs(req->ph.param_length); 3734163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3735163953Srrs for (i = 0; i < number_entries; i++) { 3736163953Srrs temp = ntohs(req->list_of_streams[i]); 3737163953Srrs req->list_of_streams[i] = temp; 3738163953Srrs } 3739235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3740163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3741163953Srrs asoc->str_reset_seq_out, 3742163953Srrs seq, (asoc->sending_seq - 1)); 3743163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3744163953Srrs asoc->str_reset = chk; 3745163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3746163953Srrs stcb->asoc.stream_reset_outstanding++; 3747163953Srrs } else { 3748163953Srrs /* Can't do it, since we have sent one out */ 3749235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3750163953Srrs } 3751233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3752163953Srrs asoc->str_reset_seq_in++; 3753163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3754163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3755163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3756163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3757163953Srrs } else { 3758235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3759163953Srrs } 3760163953Srrs} 3761163953Srrs 3762163953Srrsstatic int 3763163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3764163953Srrs struct sctp_tmit_chunk *chk, 3765163953Srrs struct sctp_stream_reset_tsn_request *req) 3766163953Srrs{ 3767163953Srrs /* reset all in and out and update the tsn */ 3768163953Srrs /* 3769163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3770163953Srrs * and set cum-ack to it. Also process this selected number as a 3771163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3772163953Srrs */ 3773163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3774163953Srrs struct sctp_association *asoc = &stcb->asoc; 3775163953Srrs int abort_flag = 0; 3776163953Srrs uint32_t seq; 3777163953Srrs 3778163953Srrs seq = ntohl(req->request_seq); 3779163953Srrs if (asoc->str_reset_seq_in == seq) { 3780235283Stuexen asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3781235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3782235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3783235283Stuexen } else { 3784235064Stuexen fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3785235283Stuexen fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3786235283Stuexen fwdtsn.ch.chunk_flags = 0; 3787235283Stuexen fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3788235283Stuexen sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3789235283Stuexen if (abort_flag) { 3790235283Stuexen return (1); 3791235283Stuexen } 3792235283Stuexen asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3793235283Stuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3794235283Stuexen sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3795235283Stuexen } 3796235283Stuexen asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; 3797235283Stuexen asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; 3798235283Stuexen memset(asoc->mapping_array, 0, asoc->mapping_array_size); 3799235283Stuexen asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 3800235283Stuexen memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 3801235283Stuexen atomic_add_int(&asoc->sending_seq, 1); 3802235283Stuexen /* save off historical data for retrans */ 3803235283Stuexen asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; 3804235283Stuexen asoc->last_sending_seq[0] = asoc->sending_seq; 3805235283Stuexen asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; 3806235283Stuexen asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; 3807235283Stuexen sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3808235283Stuexen sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3809235283Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3810235283Stuexen sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); 3811163953Srrs } 3812235283Stuexen sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3813235283Stuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3814163953Srrs asoc->str_reset_seq_in++; 3815163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3816163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3817235064Stuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3818163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3819163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3820235064Stuexen asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); 3821163953Srrs } else { 3822235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3823163953Srrs } 3824163953Srrs return (0); 3825163953Srrs} 3826163953Srrs 3827163953Srrsstatic void 3828163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3829163953Srrs struct sctp_tmit_chunk *chk, 3830170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3831163953Srrs{ 3832163953Srrs uint32_t seq, tsn; 3833163953Srrs int number_entries, len; 3834163953Srrs struct sctp_association *asoc = &stcb->asoc; 3835163953Srrs 3836163953Srrs seq = ntohl(req->request_seq); 3837163953Srrs 3838163953Srrs /* now if its not a duplicate we process it */ 3839163953Srrs if (asoc->str_reset_seq_in == seq) { 3840163953Srrs len = ntohs(req->ph.param_length); 3841163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3842163953Srrs /* 3843163953Srrs * the sender is resetting, handle the list issue.. we must 3844163953Srrs * a) verify if we can do the reset, if so no problem b) If 3845163953Srrs * we can't do the reset we must copy the request. c) queue 3846163953Srrs * it, and setup the data in processor to trigger it off 3847163953Srrs * when needed and dequeue all the queued data. 3848163953Srrs */ 3849163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3850163953Srrs 3851163953Srrs /* move the reset action back one */ 3852163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3853235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3854235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3855235064Stuexen } else if (trunc) { 3856235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3857216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3858163953Srrs /* we can do it now */ 3859163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3860235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3861163953Srrs } else { 3862163953Srrs /* 3863163953Srrs * we must queue it up and thus wait for the TSN's 3864163953Srrs * to arrive that are at or before tsn 3865163953Srrs */ 3866163953Srrs struct sctp_stream_reset_list *liste; 3867163953Srrs int siz; 3868163953Srrs 3869163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3870163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3871170091Srrs siz, SCTP_M_STRESET); 3872163953Srrs if (liste == NULL) { 3873163953Srrs /* gak out of memory */ 3874235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3875235064Stuexen sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3876163953Srrs return; 3877163953Srrs } 3878163953Srrs liste->tsn = tsn; 3879163953Srrs liste->number_entries = number_entries; 3880163953Srrs memcpy(&liste->req, req, 3881163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3882163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3883235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3884163953Srrs } 3885233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3886163953Srrs asoc->str_reset_seq_in++; 3887163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3888163953Srrs /* 3889163953Srrs * one seq back, just echo back last action since my 3890163953Srrs * response was lost. 3891163953Srrs */ 3892163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3893163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3894163953Srrs /* 3895163953Srrs * two seq back, just echo back last action since my 3896163953Srrs * response was lost. 3897163953Srrs */ 3898163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3899163953Srrs } else { 3900235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3901163953Srrs } 3902163953Srrs} 3903163953Srrs 3904188854Srrsstatic void 3905188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3906188854Srrs struct sctp_stream_reset_add_strm *str_add) 3907188854Srrs{ 3908188854Srrs /* 3909188854Srrs * Peer is requesting to add more streams. If its within our 3910188854Srrs * max-streams we will allow it. 3911188854Srrs */ 3912233660Srrs uint32_t num_stream, i; 3913188854Srrs uint32_t seq; 3914189121Srrs struct sctp_association *asoc = &stcb->asoc; 3915216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3916188854Srrs 3917188854Srrs /* Get the number. */ 3918188854Srrs seq = ntohl(str_add->request_seq); 3919188854Srrs num_stream = ntohs(str_add->number_of_streams); 3920188854Srrs /* Now what would be the new total? */ 3921189121Srrs if (asoc->str_reset_seq_in == seq) { 3922189121Srrs num_stream += stcb->asoc.streamincnt; 3923235064Stuexen stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3924235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3925235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3926235064Stuexen } else if ((num_stream > stcb->asoc.max_inbound_streams) || 3927233660Srrs (num_stream > 0xffff)) { 3928189121Srrs /* We must reject it they ask for to many */ 3929189121Srrs denied: 3930235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3931189121Srrs } else { 3932189121Srrs /* Ok, we can do that :-) */ 3933189121Srrs struct sctp_stream_in *oldstrm; 3934189121Srrs 3935189121Srrs /* save off the old */ 3936189121Srrs oldstrm = stcb->asoc.strmin; 3937189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3938189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3939189121Srrs SCTP_M_STRMI); 3940189121Srrs if (stcb->asoc.strmin == NULL) { 3941189121Srrs stcb->asoc.strmin = oldstrm; 3942189121Srrs goto denied; 3943189121Srrs } 3944189121Srrs /* copy off the old data */ 3945189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3946189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3947189121Srrs stcb->asoc.strmin[i].stream_no = i; 3948189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3949189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3950189121Srrs /* now anything on those queues? */ 3951216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3952189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3953189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3954189121Srrs } 3955189121Srrs } 3956189121Srrs /* Init the new streams */ 3957189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3958189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3959189121Srrs stcb->asoc.strmin[i].stream_no = i; 3960189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3961189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3962189121Srrs } 3963189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3964189121Srrs /* update the size */ 3965189121Srrs stcb->asoc.streamincnt = num_stream; 3966235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3967233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3968189121Srrs } 3969233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3970233660Srrs asoc->str_reset_seq_in++; 3971189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3972189121Srrs /* 3973189121Srrs * one seq back, just echo back last action since my 3974189121Srrs * response was lost. 3975189121Srrs */ 3976189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3977189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3978189121Srrs /* 3979189121Srrs * two seq back, just echo back last action since my 3980189121Srrs * response was lost. 3981189121Srrs */ 3982189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3983188854Srrs } else { 3984235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3985188854Srrs 3986188854Srrs } 3987188854Srrs} 3988188854Srrs 3989233660Srrsstatic void 3990233660Srrssctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3991233660Srrs struct sctp_stream_reset_add_strm *str_add) 3992233660Srrs{ 3993233660Srrs /* 3994233660Srrs * Peer is requesting to add more streams. If its within our 3995233660Srrs * max-streams we will allow it. 3996233660Srrs */ 3997233660Srrs uint16_t num_stream; 3998233660Srrs uint32_t seq; 3999233660Srrs struct sctp_association *asoc = &stcb->asoc; 4000233660Srrs 4001233660Srrs /* Get the number. */ 4002233660Srrs seq = ntohl(str_add->request_seq); 4003233660Srrs num_stream = ntohs(str_add->number_of_streams); 4004233660Srrs /* Now what would be the new total? */ 4005233660Srrs if (asoc->str_reset_seq_in == seq) { 4006235064Stuexen stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 4007235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 4008235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4009235064Stuexen } else if (stcb->asoc.stream_reset_outstanding) { 4010233660Srrs /* We must reject it we have something pending */ 4011235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 4012233660Srrs } else { 4013233660Srrs /* Ok, we can do that :-) */ 4014233660Srrs int mychk; 4015233660Srrs 4016233660Srrs mychk = stcb->asoc.streamoutcnt; 4017233660Srrs mychk += num_stream; 4018233660Srrs if (mychk < 0x10000) { 4019235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 4020233660Srrs if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) { 4021235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4022233660Srrs } 4023233660Srrs } else { 4024235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4025233660Srrs } 4026233660Srrs } 4027233660Srrs sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); 4028233660Srrs asoc->str_reset_seq_in++; 4029233660Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 4030233660Srrs /* 4031233660Srrs * one seq back, just echo back last action since my 4032233660Srrs * response was lost. 4033233660Srrs */ 4034233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 4035233660Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 4036233660Srrs /* 4037233660Srrs * two seq back, just echo back last action since my 4038233660Srrs * response was lost. 4039233660Srrs */ 4040233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 4041233660Srrs } else { 4042235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 4043233660Srrs } 4044233660Srrs} 4045233660Srrs 4046170992Srrs#ifdef __GNUC__ 4047170992Srrs__attribute__((noinline)) 4048170992Srrs#endif 4049170992Srrs static int 4050170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 4051170992Srrs struct sctp_stream_reset_out_req *sr_req) 4052163953Srrs{ 4053163953Srrs int chk_length, param_len, ptype; 4054170992Srrs struct sctp_paramhdr pstore; 4055170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 4056233660Srrs uint32_t seq = 0; 4057163953Srrs int num_req = 0; 4058170992Srrs int trunc = 0; 4059163953Srrs struct sctp_tmit_chunk *chk; 4060163953Srrs struct sctp_chunkhdr *ch; 4061163953Srrs struct sctp_paramhdr *ph; 4062166675Srrs int ret_code = 0; 4063166675Srrs int num_param = 0; 4064163953Srrs 4065163953Srrs /* now it may be a reset or a reset-response */ 4066163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 4067163953Srrs 4068163953Srrs /* setup for adding the response */ 4069163953Srrs sctp_alloc_a_chunk(stcb, chk); 4070163953Srrs if (chk == NULL) { 4071163953Srrs return (ret_code); 4072163953Srrs } 4073163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 4074169208Srrs chk->rec.chunk_id.can_take_data = 0; 4075163953Srrs chk->asoc = &stcb->asoc; 4076163953Srrs chk->no_fr_allowed = 0; 4077163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4078166023Srrs chk->book_size_scale = 0; 4079165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 4080163953Srrs if (chk->data == NULL) { 4081163953Srrsstrres_nochunk: 4082163953Srrs if (chk->data) { 4083163953Srrs sctp_m_freem(chk->data); 4084163953Srrs chk->data = NULL; 4085163953Srrs } 4086221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4087163953Srrs return (ret_code); 4088163953Srrs } 4089165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4090163953Srrs 4091163953Srrs /* setup chunk parameters */ 4092163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 4093163953Srrs chk->snd_count = 0; 4094224641Stuexen chk->whoTo = NULL; 4095163953Srrs 4096163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 4097163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 4098163953Srrs ch->chunk_flags = 0; 4099163953Srrs ch->chunk_length = htons(chk->send_size); 4100165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4101170992Srrs offset += sizeof(struct sctp_chunkhdr); 4102163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4103170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4104170992Srrs if (ph == NULL) 4105170992Srrs break; 4106163953Srrs param_len = ntohs(ph->param_length); 4107163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4108163953Srrs /* bad param */ 4109163953Srrs break; 4110163953Srrs } 4111170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4112170992Srrs (uint8_t *) & cstore); 4113163953Srrs ptype = ntohs(ph->param_type); 4114163953Srrs num_param++; 4115170994Srrs if (param_len > (int)sizeof(cstore)) { 4116170992Srrs trunc = 1; 4117170992Srrs } else { 4118170992Srrs trunc = 0; 4119170992Srrs } 4120163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 4121163953Srrs /* hit the max of parameters already sorry.. */ 4122163953Srrs break; 4123163953Srrs } 4124163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4125163953Srrs struct sctp_stream_reset_out_request *req_out; 4126163953Srrs 4127163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 4128163953Srrs num_req++; 4129163953Srrs if (stcb->asoc.stream_reset_outstanding) { 4130163953Srrs seq = ntohl(req_out->response_seq); 4131163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 4132163953Srrs /* implicit ack */ 4133235064Stuexen (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); 4134163953Srrs } 4135163953Srrs } 4136170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4137233660Srrs } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { 4138188854Srrs struct sctp_stream_reset_add_strm *str_add; 4139188854Srrs 4140188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4141188854Srrs num_req++; 4142188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4143233660Srrs } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { 4144233660Srrs struct sctp_stream_reset_add_strm *str_add; 4145233660Srrs 4146233660Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4147233660Srrs num_req++; 4148233660Srrs sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); 4149163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4150163953Srrs struct sctp_stream_reset_in_request *req_in; 4151163953Srrs 4152163953Srrs num_req++; 4153163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 4154170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4155163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4156163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 4157163953Srrs 4158163953Srrs num_req++; 4159163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4160163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4161163953Srrs ret_code = 1; 4162163953Srrs goto strres_nochunk; 4163163953Srrs } 4164163953Srrs /* no more */ 4165163953Srrs break; 4166163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4167163953Srrs struct sctp_stream_reset_response *resp; 4168163953Srrs uint32_t result; 4169163953Srrs 4170163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4171163953Srrs seq = ntohl(resp->response_seq); 4172163953Srrs result = ntohl(resp->result); 4173163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4174163953Srrs ret_code = 1; 4175163953Srrs goto strres_nochunk; 4176163953Srrs } 4177163953Srrs } else { 4178163953Srrs break; 4179163953Srrs } 4180170992Srrs offset += SCTP_SIZE32(param_len); 4181163953Srrs chk_length -= SCTP_SIZE32(param_len); 4182163953Srrs } 4183163953Srrs if (num_req == 0) { 4184163953Srrs /* we have no response free the stuff */ 4185163953Srrs goto strres_nochunk; 4186163953Srrs } 4187163953Srrs /* ok we have a chunk to link in */ 4188163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4189163953Srrs chk, 4190163953Srrs sctp_next); 4191163953Srrs stcb->asoc.ctrl_queue_cnt++; 4192163953Srrs return (ret_code); 4193163953Srrs} 4194163953Srrs 4195163953Srrs/* 4196163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4197163953Srrs * to handle this, either we get the whole packet and must disect it 4198163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4199163953Srrs * from a middle box that did the disectting for us. 4200163953Srrs */ 4201163953Srrsstatic void 4202163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4203170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4204163953Srrs{ 4205163953Srrs uint32_t bottle_bw, on_queue; 4206163953Srrs uint16_t trunc_len; 4207163953Srrs unsigned int chlen; 4208163953Srrs unsigned int at; 4209163953Srrs struct sctp_chunk_desc desc; 4210163953Srrs struct sctp_chunkhdr *ch; 4211163953Srrs 4212163953Srrs chlen = ntohs(cp->ch.chunk_length); 4213163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4214163953Srrs /* XXX possible chlen underflow */ 4215163953Srrs if (chlen == 0) { 4216163953Srrs ch = NULL; 4217163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4218163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4219163953Srrs } else { 4220163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4221163953Srrs chlen -= sizeof(struct sctphdr); 4222163953Srrs /* XXX possible chlen underflow */ 4223163953Srrs memset(&desc, 0, sizeof(desc)); 4224163953Srrs } 4225163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4226170781Srrs if (trunc_len > limit) { 4227170781Srrs trunc_len = limit; 4228170781Srrs } 4229163953Srrs /* now the chunks themselves */ 4230163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4231163953Srrs desc.chunk_type = ch->chunk_type; 4232163953Srrs /* get amount we need to move */ 4233163953Srrs at = ntohs(ch->chunk_length); 4234163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4235163953Srrs /* corrupt chunk, maybe at the end? */ 4236163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4237163953Srrs break; 4238163953Srrs } 4239163953Srrs if (trunc_len == 0) { 4240163953Srrs /* we are supposed to have all of it */ 4241163953Srrs if (at > chlen) { 4242163953Srrs /* corrupt skip it */ 4243163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4244163953Srrs break; 4245163953Srrs } 4246163953Srrs } else { 4247163953Srrs /* is there enough of it left ? */ 4248163953Srrs if (desc.chunk_type == SCTP_DATA) { 4249163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4250163953Srrs sizeof(desc.data_bytes))) { 4251163953Srrs break; 4252163953Srrs } 4253163953Srrs } else { 4254163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4255163953Srrs break; 4256163953Srrs } 4257163953Srrs } 4258163953Srrs } 4259163953Srrs if (desc.chunk_type == SCTP_DATA) { 4260163953Srrs /* can we get out the tsn? */ 4261163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4262163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4263163953Srrs 4264163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4265163953Srrs /* yep */ 4266163953Srrs struct sctp_data_chunk *dcp; 4267163953Srrs uint8_t *ddp; 4268163953Srrs unsigned int iii; 4269163953Srrs 4270163953Srrs dcp = (struct sctp_data_chunk *)ch; 4271163953Srrs ddp = (uint8_t *) (dcp + 1); 4272163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4273163953Srrs desc.data_bytes[iii] = ddp[iii]; 4274163953Srrs } 4275163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4276163953Srrs } else { 4277163953Srrs /* nope we are done. */ 4278163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4279163953Srrs break; 4280163953Srrs } 4281163953Srrs } else { 4282163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4283163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4284163953Srrs } 4285163953Srrs 4286163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4287163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4288163953Srrs break; 4289163953Srrs } 4290163953Srrs if (SCTP_SIZE32(at) > chlen) { 4291163953Srrs break; 4292163953Srrs } 4293163953Srrs chlen -= SCTP_SIZE32(at); 4294163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4295163953Srrs /* done, none left */ 4296163953Srrs break; 4297163953Srrs } 4298163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4299163953Srrs } 4300163953Srrs /* Now update any rwnd --- possibly */ 4301163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4302163953Srrs /* From a peer, we get a rwnd report */ 4303163953Srrs uint32_t a_rwnd; 4304163953Srrs 4305163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4306163953Srrs 4307163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4308163953Srrs on_queue = ntohl(cp->current_onq); 4309163953Srrs if (bottle_bw && on_queue) { 4310163953Srrs /* a rwnd report is in here */ 4311163953Srrs if (bottle_bw > on_queue) 4312163953Srrs a_rwnd = bottle_bw - on_queue; 4313163953Srrs else 4314163953Srrs a_rwnd = 0; 4315163953Srrs 4316163953Srrs if (a_rwnd == 0) 4317163953Srrs stcb->asoc.peers_rwnd = 0; 4318163953Srrs else { 4319163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4320163953Srrs stcb->asoc.peers_rwnd = 4321163953Srrs a_rwnd - stcb->asoc.total_flight; 4322163953Srrs } else { 4323163953Srrs stcb->asoc.peers_rwnd = 0; 4324163953Srrs } 4325163953Srrs if (stcb->asoc.peers_rwnd < 4326163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4327163953Srrs /* SWS sender side engages */ 4328163953Srrs stcb->asoc.peers_rwnd = 0; 4329163953Srrs } 4330163953Srrs } 4331163953Srrs } 4332163953Srrs } else { 4333163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4334163953Srrs } 4335163953Srrs 4336163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4337163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4338163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4339163953Srrs (stcb->asoc.sat_network)) { 4340163953Srrs /* 4341163953Srrs * This is debateable but for sat networks it makes sense 4342163953Srrs * Note if a T3 timer has went off, we will prohibit any 4343163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4344163953Srrs */ 4345171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4346171440Srrs net, cp, &bottle_bw, &on_queue); 4347163953Srrs } 4348163953Srrs} 4349163953Srrs 4350163953Srrs/* 4351163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4352163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4353163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4354163953Srrs * length of the complete packet outputs: - length: modified to remaining 4355163953Srrs * length after control processing - netp: modified to new sctp_nets after 4356163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4357163953Srrs * bad packet,...) otherwise return the tcb for this packet 4358163953Srrs */ 4359170099Srrs#ifdef __GNUC__ 4360170099Srrs__attribute__((noinline)) 4361170099Srrs#endif 4362170099Srrs static struct sctp_tcb * 4363170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4364170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4365170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4366179157Srrs uint32_t vrf_id, uint16_t port) 4367163953Srrs{ 4368163953Srrs struct sctp_association *asoc; 4369163953Srrs uint32_t vtag_in; 4370163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4371170140Srrs uint32_t chk_length; 4372163953Srrs int ret; 4373168299Srrs int abort_no_unlock = 0; 4374218186Srrs int ecne_seen = 0; 4375163953Srrs 4376163953Srrs /* 4377163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4378163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4379163953Srrs * until we get into jumbo grams and such.. 4380163953Srrs */ 4381166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4382163953Srrs struct sctp_tcb *locked_tcb = stcb; 4383163953Srrs int got_auth = 0; 4384163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4385163953Srrs int auth_skipped = 0; 4386171990Srrs int asconf_cnt = 0; 4387163953Srrs 4388172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4389172090Srrs struct socket *so; 4390172090Srrs 4391172090Srrs#endif 4392172090Srrs 4393169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4394169420Srrs iphlen, *offset, length, stcb); 4395163953Srrs 4396163953Srrs /* validate chunk header length... */ 4397163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4398170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4399170056Srrs ntohs(ch->chunk_length)); 4400170091Srrs if (locked_tcb) { 4401170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4402170091Srrs } 4403163953Srrs return (NULL); 4404163953Srrs } 4405163953Srrs /* 4406163953Srrs * validate the verification tag 4407163953Srrs */ 4408163953Srrs vtag_in = ntohl(sh->v_tag); 4409163953Srrs 4410165220Srrs if (locked_tcb) { 4411165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4412165220Srrs } 4413163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4414170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4415170056Srrs ntohs(ch->chunk_length), vtag_in); 4416163953Srrs if (vtag_in != 0) { 4417163953Srrs /* protocol error- silently discard... */ 4418163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4419169378Srrs if (locked_tcb) { 4420163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4421169378Srrs } 4422163953Srrs return (NULL); 4423163953Srrs } 4424163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4425163953Srrs /* 4426163953Srrs * If there is no stcb, skip the AUTH chunk and process 4427163953Srrs * later after a stcb is found (to validate the lookup was 4428163953Srrs * valid. 4429163953Srrs */ 4430163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4431179783Srrs (stcb == NULL) && 4432179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4433163953Srrs /* save this chunk for later processing */ 4434163953Srrs auth_skipped = 1; 4435163953Srrs auth_offset = *offset; 4436163953Srrs auth_len = ntohs(ch->chunk_length); 4437163953Srrs 4438163953Srrs /* (temporarily) move past this chunk */ 4439163953Srrs *offset += SCTP_SIZE32(auth_len); 4440163953Srrs if (*offset >= length) { 4441163953Srrs /* no more data left in the mbuf chain */ 4442163953Srrs *offset = length; 4443170091Srrs if (locked_tcb) { 4444170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4445170091Srrs } 4446163953Srrs return (NULL); 4447163953Srrs } 4448163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4449163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4450163953Srrs } 4451169420Srrs if (ch == NULL) { 4452169420Srrs /* Help */ 4453169420Srrs *offset = length; 4454170091Srrs if (locked_tcb) { 4455170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4456170091Srrs } 4457169420Srrs return (NULL); 4458169420Srrs } 4459163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4460163953Srrs goto process_control_chunks; 4461163953Srrs } 4462163953Srrs /* 4463163953Srrs * first check if it's an ASCONF with an unknown src addr we 4464163953Srrs * need to look inside to find the association 4465163953Srrs */ 4466163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4467171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4468171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4469171990Srrs 4470163953Srrs /* inp's refcount may be reduced */ 4471163953Srrs SCTP_INP_INCR_REF(inp); 4472163953Srrs 4473171990Srrs asconf_offset = *offset; 4474171990Srrs do { 4475171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4476171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4477171990Srrs break; 4478228653Stuexen stcb = sctp_findassociation_ep_asconf(m, 4479185694Srrs *offset, sh, &inp, netp, vrf_id); 4480171990Srrs if (stcb != NULL) 4481171990Srrs break; 4482171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4483171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4484171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4485171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4486163953Srrs if (stcb == NULL) { 4487163953Srrs /* 4488163953Srrs * reduce inp's refcount if not reduced in 4489163953Srrs * sctp_findassociation_ep_asconf(). 4490163953Srrs */ 4491163953Srrs SCTP_INP_DECR_REF(inp); 4492171990Srrs } else { 4493171990Srrs locked_tcb = stcb; 4494163953Srrs } 4495171990Srrs 4496163953Srrs /* now go back and verify any auth chunk to be sure */ 4497163953Srrs if (auth_skipped && (stcb != NULL)) { 4498163953Srrs struct sctp_auth_chunk *auth; 4499163953Srrs 4500163953Srrs auth = (struct sctp_auth_chunk *) 4501163953Srrs sctp_m_getptr(m, auth_offset, 4502163953Srrs auth_len, chunk_buf); 4503163953Srrs got_auth = 1; 4504163953Srrs auth_skipped = 0; 4505169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4506163953Srrs auth_offset)) { 4507163953Srrs /* auth HMAC failed so dump it */ 4508163953Srrs *offset = length; 4509170091Srrs if (locked_tcb) { 4510170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4511170091Srrs } 4512163953Srrs return (NULL); 4513163953Srrs } else { 4514163953Srrs /* remaining chunks are HMAC checked */ 4515163953Srrs stcb->asoc.authenticated = 1; 4516163953Srrs } 4517163953Srrs } 4518163953Srrs } 4519163953Srrs if (stcb == NULL) { 4520163953Srrs /* no association, so it's out of the blue... */ 4521169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4522179157Srrs vrf_id, port); 4523163953Srrs *offset = length; 4524169378Srrs if (locked_tcb) { 4525163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4526169378Srrs } 4527163953Srrs return (NULL); 4528163953Srrs } 4529163953Srrs asoc = &stcb->asoc; 4530163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4531163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4532163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4533163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4534163953Srrs if ((vtag_in == asoc->my_vtag) || 4535163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4536163953Srrs (vtag_in == asoc->peer_vtag))) { 4537163953Srrs /* this is valid */ 4538163953Srrs } else { 4539163953Srrs /* drop this packet... */ 4540163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4541169378Srrs if (locked_tcb) { 4542163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4543169378Srrs } 4544163953Srrs return (NULL); 4545163953Srrs } 4546163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4547163953Srrs if (vtag_in != asoc->my_vtag) { 4548163953Srrs /* 4549163953Srrs * this could be a stale SHUTDOWN-ACK or the 4550163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4551163953Srrs * is still hung; we have started a new asoc 4552163953Srrs * but it won't complete until the shutdown 4553163953Srrs * is completed 4554163953Srrs */ 4555169378Srrs if (locked_tcb) { 4556163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4557169378Srrs } 4558163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4559179157Srrs NULL, vrf_id, port); 4560163953Srrs return (NULL); 4561163953Srrs } 4562163953Srrs } else { 4563163953Srrs /* for all other chunks, vtag must match */ 4564163953Srrs if (vtag_in != asoc->my_vtag) { 4565163953Srrs /* invalid vtag... */ 4566169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4567169420Srrs "invalid vtag: %xh, expect %xh\n", 4568169420Srrs vtag_in, asoc->my_vtag); 4569163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4570169378Srrs if (locked_tcb) { 4571163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4572169378Srrs } 4573163953Srrs *offset = length; 4574163953Srrs return (NULL); 4575163953Srrs } 4576163953Srrs } 4577163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4578163953Srrs /* 4579163953Srrs * process all control chunks... 4580163953Srrs */ 4581163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4582185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4583163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4584163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4585163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4586179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4587171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4588171943Srrs stcb->asoc.overall_error_count, 4589171943Srrs 0, 4590171943Srrs SCTP_FROM_SCTP_INPUT, 4591171943Srrs __LINE__); 4592171943Srrs } 4593163953Srrs stcb->asoc.overall_error_count = 0; 4594163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4595163953Srrs *netp); 4596163953Srrs } 4597163953Srrsprocess_control_chunks: 4598163953Srrs while (IS_SCTP_CONTROL(ch)) { 4599163953Srrs /* validate chunk length */ 4600163953Srrs chk_length = ntohs(ch->chunk_length); 4601169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4602169420Srrs ch->chunk_type, chk_length); 4603170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4604170140Srrs if (chk_length < sizeof(*ch) || 4605170140Srrs (*offset + (int)chk_length) > length) { 4606163953Srrs *offset = length; 4607169378Srrs if (locked_tcb) { 4608163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4609169378Srrs } 4610163953Srrs return (NULL); 4611163953Srrs } 4612163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4613163953Srrs /* 4614163953Srrs * INIT-ACK only gets the init ack "header" portion only 4615163953Srrs * because we don't have to process the peer's COOKIE. All 4616163953Srrs * others get a complete chunk. 4617163953Srrs */ 4618169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4619169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4620163953Srrs /* get an init-ack chunk */ 4621163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4622163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4623163953Srrs if (ch == NULL) { 4624163953Srrs *offset = length; 4625169378Srrs if (locked_tcb) { 4626163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4627169378Srrs } 4628163953Srrs return (NULL); 4629163953Srrs } 4630170642Srrs } else { 4631170786Srrs /* For cookies and all other chunks. */ 4632169208Srrs if (chk_length > sizeof(chunk_buf)) { 4633169208Srrs /* 4634169208Srrs * use just the size of the chunk buffer so 4635170642Srrs * the front part of our chunks fit in 4636170642Srrs * contiguous space up to the chunk buffer 4637170642Srrs * size (508 bytes). For chunks that need to 4638170786Srrs * get more than that they must use the 4639170642Srrs * sctp_m_getptr() function or other means 4640170786Srrs * (e.g. know how to parse mbuf chains). 4641170786Srrs * Cookies do this already. 4642169208Srrs */ 4643169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4644169208Srrs (sizeof(chunk_buf) - 4), 4645169208Srrs chunk_buf); 4646169208Srrs if (ch == NULL) { 4647169208Srrs *offset = length; 4648169378Srrs if (locked_tcb) { 4649169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4650169378Srrs } 4651169208Srrs return (NULL); 4652169208Srrs } 4653169208Srrs } else { 4654169208Srrs /* We can fit it all */ 4655170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4656170642Srrs chk_length, chunk_buf); 4657170642Srrs if (ch == NULL) { 4658170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4659170642Srrs *offset = length; 4660170642Srrs if (locked_tcb) { 4661170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4662169208Srrs } 4663170642Srrs return (NULL); 4664163953Srrs } 4665163953Srrs } 4666163953Srrs } 4667163953Srrs num_chunks++; 4668163953Srrs /* Save off the last place we got a control from */ 4669163953Srrs if (stcb != NULL) { 4670169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4671163953Srrs /* 4672163953Srrs * allow last_control to be NULL if 4673163953Srrs * ASCONF... ASCONF processing will find the 4674163953Srrs * right net later 4675163953Srrs */ 4676169420Srrs if ((netp != NULL) && (*netp != NULL)) 4677169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4678163953Srrs } 4679163953Srrs } 4680163953Srrs#ifdef SCTP_AUDITING_ENABLED 4681163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4682163953Srrs#endif 4683163953Srrs 4684163953Srrs /* check to see if this chunk required auth, but isn't */ 4685179783Srrs if ((stcb != NULL) && 4686179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4687179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4688163953Srrs !stcb->asoc.authenticated) { 4689163953Srrs /* "silently" ignore */ 4690163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4691163953Srrs goto next_chunk; 4692163953Srrs } 4693163953Srrs switch (ch->chunk_type) { 4694163953Srrs case SCTP_INITIATION: 4695169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4696229774Stuexen /* The INIT chunk must be the only chunk. */ 4697229774Stuexen if ((num_chunks > 1) || 4698229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4699229774Stuexen sctp_abort_association(inp, stcb, m, 4700229774Stuexen iphlen, sh, NULL, vrf_id, port); 4701163953Srrs *offset = length; 4702163953Srrs return (NULL); 4703163953Srrs } 4704229774Stuexen /* Honor our resource limit. */ 4705229774Stuexen if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4706229774Stuexen struct mbuf *op_err; 4707229774Stuexen 4708229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4709229774Stuexen sctp_abort_association(inp, stcb, m, 4710229774Stuexen iphlen, sh, op_err, vrf_id, port); 4711163953Srrs *offset = length; 4712163953Srrs return (NULL); 4713163953Srrs } 4714229774Stuexen sctp_handle_init(m, iphlen, *offset, sh, 4715229774Stuexen (struct sctp_init_chunk *)ch, inp, 4716229774Stuexen stcb, &abort_no_unlock, vrf_id, port); 4717163953Srrs *offset = length; 4718229774Stuexen if ((!abort_no_unlock) && (locked_tcb)) { 4719163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4720169378Srrs } 4721163953Srrs return (NULL); 4722163953Srrs break; 4723170642Srrs case SCTP_PAD_CHUNK: 4724170642Srrs break; 4725163953Srrs case SCTP_INITIATION_ACK: 4726169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4727163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4728163953Srrs /* We are not interested anymore */ 4729163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4730163953Srrs ; 4731163953Srrs } else { 4732208876Srrs if (locked_tcb != stcb) { 4733208876Srrs /* Very unlikely */ 4734163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4735169378Srrs } 4736163953Srrs *offset = length; 4737163953Srrs if (stcb) { 4738172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4739172090Srrs so = SCTP_INP_SO(inp); 4740172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4741172090Srrs SCTP_TCB_UNLOCK(stcb); 4742172090Srrs SCTP_SOCKET_LOCK(so, 1); 4743172090Srrs SCTP_TCB_LOCK(stcb); 4744172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4745172090Srrs#endif 4746171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4747172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4748172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4749172090Srrs#endif 4750163953Srrs } 4751163953Srrs return (NULL); 4752163953Srrs } 4753163953Srrs } 4754229775Stuexen /* The INIT-ACK chunk must be the only chunk. */ 4755163953Srrs if ((num_chunks > 1) || 4756229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4757163953Srrs *offset = length; 4758169378Srrs if (locked_tcb) { 4759163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4760169378Srrs } 4761163953Srrs return (NULL); 4762163953Srrs } 4763169420Srrs if ((netp) && (*netp)) { 4764169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4765170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4766169420Srrs } else { 4767169420Srrs ret = -1; 4768169420Srrs } 4769229774Stuexen *offset = length; 4770229774Stuexen if (abort_no_unlock) { 4771229774Stuexen return (NULL); 4772229774Stuexen } 4773163953Srrs /* 4774163953Srrs * Special case, I must call the output routine to 4775163953Srrs * get the cookie echoed 4776163953Srrs */ 4777229774Stuexen if ((stcb != NULL) && (ret == 0)) { 4778172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4779229774Stuexen } 4780169378Srrs if (locked_tcb) { 4781163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4782169378Srrs } 4783163953Srrs return (NULL); 4784163953Srrs break; 4785163953Srrs case SCTP_SELECTIVE_ACK: 4786163953Srrs { 4787163953Srrs struct sctp_sack_chunk *sack; 4788163953Srrs int abort_now = 0; 4789163953Srrs uint32_t a_rwnd, cum_ack; 4790202526Stuexen uint16_t num_seg, num_dup; 4791202526Stuexen uint8_t flags; 4792202526Stuexen int offset_seg, offset_dup; 4793163953Srrs 4794211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4795211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4796202526Stuexen if (stcb == NULL) { 4797202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4798202526Stuexen break; 4799169208Srrs } 4800202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4801202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4802202526Stuexen break; 4803202526Stuexen } 4804171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4805171990Srrs /*- 4806171990Srrs * If we have sent a shutdown-ack, we will pay no 4807171990Srrs * attention to a sack sent in to us since 4808171990Srrs * we don't care anymore. 4809171990Srrs */ 4810184883Srrs break; 4811171990Srrs } 4812163953Srrs sack = (struct sctp_sack_chunk *)ch; 4813202526Stuexen flags = ch->chunk_flags; 4814163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4815163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4816202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4817163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4818202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4819202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4820202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4821202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4822202526Stuexen break; 4823202526Stuexen } 4824202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4825202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4826170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4827202526Stuexen cum_ack, num_seg, a_rwnd); 4828163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4829163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4830163953Srrs (num_seg == 0) && 4831216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4832163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4833216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4834163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4835163953Srrs ) { 4836163953Srrs /* 4837163953Srrs * We have a SIMPLE sack having no 4838163953Srrs * prior segments and data on sent 4839163953Srrs * queue to be acked.. Use the 4840163953Srrs * faster path sack processing. We 4841163953Srrs * also allow window update sacks 4842163953Srrs * with no missing segments to go 4843163953Srrs * this way too. 4844163953Srrs */ 4845218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4846163953Srrs } else { 4847169420Srrs if (netp && *netp) 4848228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4849202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4850218186Srrs cum_ack, a_rwnd, ecne_seen); 4851163953Srrs } 4852202526Stuexen if (abort_now) { 4853202526Stuexen /* ABORT signal from sack processing */ 4854202526Stuexen *offset = length; 4855202526Stuexen return (NULL); 4856202526Stuexen } 4857185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4858185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4859185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4860185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4861185694Srrs } 4862163953Srrs } 4863163953Srrs break; 4864185694Srrs /* 4865185694Srrs * EY - nr_sack: If the received chunk is an 4866185694Srrs * nr_sack chunk 4867185694Srrs */ 4868185694Srrs case SCTP_NR_SELECTIVE_ACK: 4869185694Srrs { 4870185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4871185694Srrs int abort_now = 0; 4872185694Srrs uint32_t a_rwnd, cum_ack; 4873202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4874202526Stuexen uint8_t flags; 4875202526Stuexen int offset_seg, offset_dup; 4876185694Srrs 4877211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4878211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4879212225Srrs if (stcb == NULL) { 4880212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4881212225Srrs break; 4882212225Srrs } 4883211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4884211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4885185694Srrs goto unknown_chunk; 4886185694Srrs } 4887202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4888202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4889202526Stuexen break; 4890202526Stuexen } 4891185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4892185694Srrs /*- 4893185694Srrs * If we have sent a shutdown-ack, we will pay no 4894185694Srrs * attention to a sack sent in to us since 4895185694Srrs * we don't care anymore. 4896185694Srrs */ 4897202526Stuexen break; 4898185694Srrs } 4899185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4900202526Stuexen flags = ch->chunk_flags; 4901185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4902185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4903185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4904202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4905185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4906202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4907202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4908202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4909202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4910202526Stuexen break; 4911202526Stuexen } 4912202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4913202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4914185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4915202526Stuexen cum_ack, num_seg, a_rwnd); 4916185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4917185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4918202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4919216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4920185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4921216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4922202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4923185694Srrs /* 4924185694Srrs * We have a SIMPLE sack having no 4925185694Srrs * prior segments and data on sent 4926202526Stuexen * queue to be acked. Use the faster 4927202526Stuexen * path sack processing. We also 4928202526Stuexen * allow window update sacks with no 4929202526Stuexen * missing segments to go this way 4930202526Stuexen * too. 4931185694Srrs */ 4932218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4933218186Srrs &abort_now, ecne_seen); 4934185694Srrs } else { 4935185694Srrs if (netp && *netp) 4936228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4937202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4938218186Srrs cum_ack, a_rwnd, ecne_seen); 4939185694Srrs } 4940202526Stuexen if (abort_now) { 4941202526Stuexen /* ABORT signal from sack processing */ 4942202526Stuexen *offset = length; 4943202526Stuexen return (NULL); 4944202526Stuexen } 4945185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4946185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4947185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4948185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4949185694Srrs } 4950185694Srrs } 4951185694Srrs break; 4952185694Srrs 4953163953Srrs case SCTP_HEARTBEAT_REQUEST: 4954169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4955169420Srrs if ((stcb) && netp && *netp) { 4956169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4957169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4958169420Srrs chk_length, *netp); 4959169420Srrs 4960169420Srrs /* He's alive so give him credit */ 4961179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4962171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4963171943Srrs stcb->asoc.overall_error_count, 4964171943Srrs 0, 4965171943Srrs SCTP_FROM_SCTP_INPUT, 4966171943Srrs __LINE__); 4967171943Srrs } 4968169420Srrs stcb->asoc.overall_error_count = 0; 4969163953Srrs } 4970163953Srrs break; 4971163953Srrs case SCTP_HEARTBEAT_ACK: 4972169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4973169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4974169208Srrs /* Its not ours */ 4975169352Srrs *offset = length; 4976169378Srrs if (locked_tcb) { 4977169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4978169378Srrs } 4979169208Srrs return (NULL); 4980169208Srrs } 4981163953Srrs /* He's alive so give him credit */ 4982179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4983171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4984171943Srrs stcb->asoc.overall_error_count, 4985171943Srrs 0, 4986171943Srrs SCTP_FROM_SCTP_INPUT, 4987171943Srrs __LINE__); 4988171943Srrs } 4989163953Srrs stcb->asoc.overall_error_count = 0; 4990163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4991169420Srrs if (netp && *netp) 4992169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4993169420Srrs stcb, *netp); 4994163953Srrs break; 4995163953Srrs case SCTP_ABORT_ASSOCIATION: 4996170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4997170091Srrs stcb); 4998169420Srrs if ((stcb) && netp && *netp) 4999169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 5000169420Srrs stcb, *netp); 5001163953Srrs *offset = length; 5002163953Srrs return (NULL); 5003163953Srrs break; 5004163953Srrs case SCTP_SHUTDOWN: 5005170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 5006170091Srrs stcb); 5007169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 5008169208Srrs *offset = length; 5009169378Srrs if (locked_tcb) { 5010169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5011169378Srrs } 5012169208Srrs return (NULL); 5013169420Srrs } 5014169420Srrs if (netp && *netp) { 5015163953Srrs int abort_flag = 0; 5016163953Srrs 5017163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 5018163953Srrs stcb, *netp, &abort_flag); 5019163953Srrs if (abort_flag) { 5020163953Srrs *offset = length; 5021163953Srrs return (NULL); 5022163953Srrs } 5023163953Srrs } 5024163953Srrs break; 5025163953Srrs case SCTP_SHUTDOWN_ACK: 5026170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 5027169420Srrs if ((stcb) && (netp) && (*netp)) 5028169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 5029163953Srrs *offset = length; 5030163953Srrs return (NULL); 5031163953Srrs break; 5032169420Srrs 5033163953Srrs case SCTP_OPERATION_ERROR: 5034169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 5035169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 5036163953Srrs *offset = length; 5037163953Srrs return (NULL); 5038163953Srrs } 5039163953Srrs break; 5040163953Srrs case SCTP_COOKIE_ECHO: 5041169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5042170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 5043163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5044163953Srrs ; 5045163953Srrs } else { 5046169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5047163953Srrs /* We are not interested anymore */ 5048208878Srrs abend: 5049208853Srrs if (stcb) { 5050208853Srrs SCTP_TCB_UNLOCK(stcb); 5051208853Srrs } 5052163953Srrs *offset = length; 5053163953Srrs return (NULL); 5054163953Srrs } 5055163953Srrs } 5056163953Srrs /* 5057163953Srrs * First are we accepting? We do this again here 5058207924Srrs * since it is possible that a previous endpoint WAS 5059207924Srrs * listening responded to a INIT-ACK and then 5060163953Srrs * closed. We opened and bound.. and are now no 5061163953Srrs * longer listening. 5062163953Srrs */ 5063172703Srrs 5064172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 5065172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5066179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 5067229774Stuexen struct mbuf *op_err; 5068163953Srrs 5069229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 5070172703Srrs sctp_abort_association(inp, stcb, m, 5071229774Stuexen iphlen, sh, op_err, vrf_id, port); 5072163953Srrs } 5073172703Srrs *offset = length; 5074172703Srrs return (NULL); 5075172703Srrs } else { 5076163953Srrs struct mbuf *ret_buf; 5077165220Srrs struct sctp_inpcb *linp; 5078163953Srrs 5079169420Srrs if (stcb) { 5080165220Srrs linp = NULL; 5081169420Srrs } else { 5082165220Srrs linp = inp; 5083169420Srrs } 5084165220Srrs 5085169420Srrs if (linp) { 5086165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 5087208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5088208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5089208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5090208878Srrs goto abend; 5091208878Srrs } 5092169420Srrs } 5093169420Srrs if (netp) { 5094169420Srrs ret_buf = 5095169420Srrs sctp_handle_cookie_echo(m, iphlen, 5096169420Srrs *offset, sh, 5097169420Srrs (struct sctp_cookie_echo_chunk *)ch, 5098169420Srrs &inp, &stcb, netp, 5099169420Srrs auth_skipped, 5100169420Srrs auth_offset, 5101169420Srrs auth_len, 5102169420Srrs &locked_tcb, 5103179157Srrs vrf_id, 5104179157Srrs port); 5105169420Srrs } else { 5106169420Srrs ret_buf = NULL; 5107169420Srrs } 5108169420Srrs if (linp) { 5109165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5110169420Srrs } 5111163953Srrs if (ret_buf == NULL) { 5112163953Srrs if (locked_tcb) { 5113163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5114163953Srrs } 5115169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5116169420Srrs "GAK, null buffer\n"); 5117163953Srrs *offset = length; 5118163953Srrs return (NULL); 5119163953Srrs } 5120163953Srrs /* if AUTH skipped, see if it verified... */ 5121163953Srrs if (auth_skipped) { 5122163953Srrs got_auth = 1; 5123163953Srrs auth_skipped = 0; 5124163953Srrs } 5125163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5126163953Srrs /* 5127163953Srrs * Restart the timer if we have 5128163953Srrs * pending data 5129163953Srrs */ 5130163953Srrs struct sctp_tmit_chunk *chk; 5131163953Srrs 5132163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5133216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5134163953Srrs } 5135163953Srrs } 5136163953Srrs break; 5137163953Srrs case SCTP_COOKIE_ACK: 5138170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5139169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5140169378Srrs if (locked_tcb) { 5141169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5142169378Srrs } 5143169352Srrs return (NULL); 5144169352Srrs } 5145163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5146163953Srrs /* We are not interested anymore */ 5147163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5148163953Srrs ; 5149169420Srrs } else if (stcb) { 5150172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5151172090Srrs so = SCTP_INP_SO(inp); 5152172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5153172090Srrs SCTP_TCB_UNLOCK(stcb); 5154172090Srrs SCTP_SOCKET_LOCK(so, 1); 5155172090Srrs SCTP_TCB_LOCK(stcb); 5156172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5157172090Srrs#endif 5158171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5159172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5160172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5161172090Srrs#endif 5162163953Srrs *offset = length; 5163163953Srrs return (NULL); 5164163953Srrs } 5165163953Srrs } 5166163953Srrs /* He's alive so give him credit */ 5167169420Srrs if ((stcb) && netp && *netp) { 5168179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5169171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5170171943Srrs stcb->asoc.overall_error_count, 5171171943Srrs 0, 5172171943Srrs SCTP_FROM_SCTP_INPUT, 5173171943Srrs __LINE__); 5174171943Srrs } 5175169378Srrs stcb->asoc.overall_error_count = 0; 5176169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5177169378Srrs } 5178163953Srrs break; 5179163953Srrs case SCTP_ECN_ECHO: 5180169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5181163953Srrs /* He's alive so give him credit */ 5182169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5183169208Srrs /* Its not ours */ 5184169378Srrs if (locked_tcb) { 5185169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5186169378Srrs } 5187169208Srrs *offset = length; 5188169208Srrs return (NULL); 5189169208Srrs } 5190169378Srrs if (stcb) { 5191179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5192171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5193171943Srrs stcb->asoc.overall_error_count, 5194171943Srrs 0, 5195171943Srrs SCTP_FROM_SCTP_INPUT, 5196171943Srrs __LINE__); 5197171943Srrs } 5198169378Srrs stcb->asoc.overall_error_count = 0; 5199169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5200169378Srrs stcb); 5201218186Srrs ecne_seen = 1; 5202169378Srrs } 5203163953Srrs break; 5204163953Srrs case SCTP_ECN_CWR: 5205169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5206163953Srrs /* He's alive so give him credit */ 5207169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5208169208Srrs /* Its not ours */ 5209169378Srrs if (locked_tcb) { 5210169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5211169378Srrs } 5212169208Srrs *offset = length; 5213169208Srrs return (NULL); 5214169208Srrs } 5215169378Srrs if (stcb) { 5216179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5217171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5218171943Srrs stcb->asoc.overall_error_count, 5219171943Srrs 0, 5220171943Srrs SCTP_FROM_SCTP_INPUT, 5221171943Srrs __LINE__); 5222171943Srrs } 5223169378Srrs stcb->asoc.overall_error_count = 0; 5224218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5225169378Srrs } 5226163953Srrs break; 5227163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5228170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5229163953Srrs /* must be first and only chunk */ 5230163953Srrs if ((num_chunks > 1) || 5231170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5232163953Srrs *offset = length; 5233169378Srrs if (locked_tcb) { 5234163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5235169378Srrs } 5236163953Srrs return (NULL); 5237163953Srrs } 5238169420Srrs if ((stcb) && netp && *netp) { 5239169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5240169378Srrs stcb, *netp); 5241169378Srrs } 5242163953Srrs *offset = length; 5243163953Srrs return (NULL); 5244163953Srrs break; 5245163953Srrs case SCTP_ASCONF: 5246169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5247163953Srrs /* He's alive so give him credit */ 5248169378Srrs if (stcb) { 5249179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5250171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5251171943Srrs stcb->asoc.overall_error_count, 5252171943Srrs 0, 5253171943Srrs SCTP_FROM_SCTP_INPUT, 5254171943Srrs __LINE__); 5255171943Srrs } 5256169378Srrs stcb->asoc.overall_error_count = 0; 5257169378Srrs sctp_handle_asconf(m, *offset, 5258171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5259171990Srrs asconf_cnt++; 5260169378Srrs } 5261163953Srrs break; 5262163953Srrs case SCTP_ASCONF_ACK: 5263169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5264169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5265169208Srrs /* Its not ours */ 5266169378Srrs if (locked_tcb) { 5267169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5268169378Srrs } 5269169208Srrs *offset = length; 5270169208Srrs return (NULL); 5271169208Srrs } 5272169420Srrs if ((stcb) && netp && *netp) { 5273169378Srrs /* He's alive so give him credit */ 5274179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5275171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5276171943Srrs stcb->asoc.overall_error_count, 5277171943Srrs 0, 5278171943Srrs SCTP_FROM_SCTP_INPUT, 5279171943Srrs __LINE__); 5280171943Srrs } 5281169378Srrs stcb->asoc.overall_error_count = 0; 5282169378Srrs sctp_handle_asconf_ack(m, *offset, 5283172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5284172190Srrs if (abort_no_unlock) 5285172190Srrs return (NULL); 5286169378Srrs } 5287163953Srrs break; 5288163953Srrs case SCTP_FORWARD_CUM_TSN: 5289169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5290169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5291169208Srrs /* Its not ours */ 5292169378Srrs if (locked_tcb) { 5293169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5294169378Srrs } 5295169208Srrs *offset = length; 5296169208Srrs return (NULL); 5297169208Srrs } 5298163953Srrs /* He's alive so give him credit */ 5299169378Srrs if (stcb) { 5300163953Srrs int abort_flag = 0; 5301163953Srrs 5302163953Srrs stcb->asoc.overall_error_count = 0; 5303179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5304171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5305171943Srrs stcb->asoc.overall_error_count, 5306171943Srrs 0, 5307171943Srrs SCTP_FROM_SCTP_INPUT, 5308171943Srrs __LINE__); 5309171943Srrs } 5310163953Srrs *fwd_tsn_seen = 1; 5311163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5312163953Srrs /* We are not interested anymore */ 5313172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5314172090Srrs so = SCTP_INP_SO(inp); 5315172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5316172090Srrs SCTP_TCB_UNLOCK(stcb); 5317172090Srrs SCTP_SOCKET_LOCK(so, 1); 5318172090Srrs SCTP_TCB_LOCK(stcb); 5319172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5320172090Srrs#endif 5321171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5322172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5323172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5324172090Srrs#endif 5325163953Srrs *offset = length; 5326163953Srrs return (NULL); 5327163953Srrs } 5328163953Srrs sctp_handle_forward_tsn(stcb, 5329170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5330163953Srrs if (abort_flag) { 5331163953Srrs *offset = length; 5332163953Srrs return (NULL); 5333163953Srrs } else { 5334179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5335171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5336171943Srrs stcb->asoc.overall_error_count, 5337171943Srrs 0, 5338171943Srrs SCTP_FROM_SCTP_INPUT, 5339171943Srrs __LINE__); 5340171943Srrs } 5341163953Srrs stcb->asoc.overall_error_count = 0; 5342163953Srrs } 5343163953Srrs 5344163953Srrs } 5345163953Srrs break; 5346163953Srrs case SCTP_STREAM_RESET: 5347169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5348169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5349169208Srrs /* Its not ours */ 5350169378Srrs if (locked_tcb) { 5351169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5352169378Srrs } 5353169208Srrs *offset = length; 5354169208Srrs return (NULL); 5355169208Srrs } 5356163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5357163953Srrs /* We are not interested anymore */ 5358172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5359172090Srrs so = SCTP_INP_SO(inp); 5360172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5361172090Srrs SCTP_TCB_UNLOCK(stcb); 5362172090Srrs SCTP_SOCKET_LOCK(so, 1); 5363172090Srrs SCTP_TCB_LOCK(stcb); 5364172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5365172090Srrs#endif 5366171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5367172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5368172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5369172090Srrs#endif 5370163953Srrs *offset = length; 5371163953Srrs return (NULL); 5372163953Srrs } 5373169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5374169420Srrs /* 5375169420Srrs * hmm, peer should have announced this, but 5376169420Srrs * we will turn it on since he is sending us 5377169420Srrs * a stream reset. 5378169420Srrs */ 5379169420Srrs stcb->asoc.peer_supports_strreset = 1; 5380163953Srrs } 5381170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5382169420Srrs /* stop processing */ 5383169420Srrs *offset = length; 5384169420Srrs return (NULL); 5385169420Srrs } 5386163953Srrs break; 5387163953Srrs case SCTP_PACKET_DROPPED: 5388169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5389163953Srrs /* re-get it all please */ 5390169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5391169208Srrs /* Its not ours */ 5392169378Srrs if (locked_tcb) { 5393169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5394169378Srrs } 5395169208Srrs *offset = length; 5396169208Srrs return (NULL); 5397169208Srrs } 5398169420Srrs if (ch && (stcb) && netp && (*netp)) { 5399169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5400170781Srrs stcb, *netp, 5401170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5402170781Srrs 5403169378Srrs } 5404163953Srrs break; 5405163953Srrs 5406163953Srrs case SCTP_AUTHENTICATION: 5407169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5408179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5409163953Srrs goto unknown_chunk; 5410163953Srrs 5411163953Srrs if (stcb == NULL) { 5412163953Srrs /* save the first AUTH for later processing */ 5413163953Srrs if (auth_skipped == 0) { 5414163953Srrs auth_offset = *offset; 5415163953Srrs auth_len = chk_length; 5416163953Srrs auth_skipped = 1; 5417163953Srrs } 5418163953Srrs /* skip this chunk (temporarily) */ 5419163953Srrs goto next_chunk; 5420163953Srrs } 5421169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5422169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5423169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5424169208Srrs /* Its not ours */ 5425169378Srrs if (locked_tcb) { 5426169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5427169378Srrs } 5428169208Srrs *offset = length; 5429169208Srrs return (NULL); 5430169208Srrs } 5431163953Srrs if (got_auth == 1) { 5432163953Srrs /* skip this chunk... it's already auth'd */ 5433163953Srrs goto next_chunk; 5434163953Srrs } 5435163953Srrs got_auth = 1; 5436169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5437163953Srrs m, *offset)) { 5438163953Srrs /* auth HMAC failed so dump the packet */ 5439163953Srrs *offset = length; 5440163953Srrs return (stcb); 5441163953Srrs } else { 5442163953Srrs /* remaining chunks are HMAC checked */ 5443163953Srrs stcb->asoc.authenticated = 1; 5444163953Srrs } 5445163953Srrs break; 5446163953Srrs 5447163953Srrs default: 5448163953Srrs unknown_chunk: 5449163953Srrs /* it's an unknown chunk! */ 5450163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5451163953Srrs struct mbuf *mm; 5452163953Srrs struct sctp_paramhdr *phd; 5453163953Srrs 5454163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5455165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5456163953Srrs if (mm) { 5457163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5458163953Srrs /* 5459163953Srrs * We cheat and use param type since 5460163953Srrs * we did not bother to define a 5461163953Srrs * error cause struct. They are the 5462163953Srrs * same basic format with different 5463163953Srrs * names. 5464163953Srrs */ 5465163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5466163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5467165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5468234459Stuexen SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT); 5469165647Srrs if (SCTP_BUF_NEXT(mm)) { 5470234459Stuexen if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) { 5471234459Stuexen sctp_m_freem(mm); 5472234459Stuexen } else { 5473178202Srrs#ifdef SCTP_MBUF_LOGGING 5474234459Stuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5475234459Stuexen struct mbuf *mat; 5476178202Srrs 5477234461Stuexen for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) { 5478234459Stuexen if (SCTP_BUF_IS_EXTENDED(mat)) { 5479234459Stuexen sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5480234459Stuexen } 5481178202Srrs } 5482178202Srrs } 5483234459Stuexen#endif 5484234459Stuexen sctp_queue_op_err(stcb, mm); 5485178202Srrs } 5486163953Srrs } else { 5487163953Srrs sctp_m_freem(mm); 5488163953Srrs } 5489163953Srrs } 5490163953Srrs } 5491163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5492163953Srrs /* discard this packet */ 5493163953Srrs *offset = length; 5494163953Srrs return (stcb); 5495163953Srrs } /* else skip this bad chunk and continue... */ 5496163953Srrs break; 5497163953Srrs } /* switch (ch->chunk_type) */ 5498163953Srrs 5499163953Srrs 5500163953Srrsnext_chunk: 5501163953Srrs /* get the next chunk */ 5502163953Srrs *offset += SCTP_SIZE32(chk_length); 5503163953Srrs if (*offset >= length) { 5504163953Srrs /* no more data left in the mbuf chain */ 5505163953Srrs break; 5506163953Srrs } 5507163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5508163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5509163953Srrs if (ch == NULL) { 5510169378Srrs if (locked_tcb) { 5511163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5512169378Srrs } 5513163953Srrs *offset = length; 5514163953Srrs return (NULL); 5515163953Srrs } 5516163953Srrs } /* while */ 5517171990Srrs 5518171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5519171990Srrs sctp_send_asconf_ack(stcb); 5520171990Srrs } 5521163953Srrs return (stcb); 5522163953Srrs} 5523163953Srrs 5524163953Srrs 5525171440Srrs#ifdef INVARIANTS 5526208853Srrs#ifdef __GNUC__ 5527208853Srrs__attribute__((noinline)) 5528208853Srrs#endif 5529208853Srrs void 5530208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5531171440Srrs{ 5532208853Srrs struct sctp_tcb *lstcb; 5533171440Srrs 5534208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5535208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5536171440Srrs panic("Own lock on stcb at return from input"); 5537171440Srrs } 5538171440Srrs } 5539208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5540208876Srrs panic("Own create lock on inp"); 5541208876Srrs } 5542208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5543208876Srrs panic("Own inp lock on inp"); 5544208876Srrs } 5545171440Srrs} 5546171440Srrs 5547171440Srrs#endif 5548171440Srrs 5549163953Srrs/* 5550163953Srrs * common input chunk processing (v4 and v6) 5551163953Srrs */ 5552169378Srrsvoid 5553163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5554163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5555163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5556179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5557163953Srrs{ 5558163953Srrs /* 5559163953Srrs * Control chunk processing 5560163953Srrs */ 5561163953Srrs uint32_t high_tsn; 5562163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5563163953Srrs struct mbuf *m = *mm; 5564163953Srrs int un_sent; 5565218129Srrs int cnt_ctrl_ready = 0; 5566163953Srrs 5567163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5568163953Srrs#ifdef SCTP_AUDITING_ENABLED 5569163953Srrs sctp_audit_log(0xE0, 1); 5570163953Srrs sctp_auditing(0, inp, stcb, net); 5571163953Srrs#endif 5572163953Srrs 5573179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5574179783Srrs m, iphlen, offset, length, stcb); 5575163953Srrs if (stcb) { 5576163953Srrs /* always clear this before beginning a packet */ 5577163953Srrs stcb->asoc.authenticated = 0; 5578163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5579171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5580171990Srrs stcb, stcb->asoc.state); 5581171990Srrs 5582171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5583171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5584171745Srrs /*- 5585171745Srrs * If we hit here, we had a ref count 5586171745Srrs * up when the assoc was aborted and the 5587171745Srrs * timer is clearing out the assoc, we should 5588171745Srrs * NOT respond to any packet.. its OOTB. 5589171745Srrs */ 5590171745Srrs SCTP_TCB_UNLOCK(stcb); 5591171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5592179157Srrs vrf_id, port); 5593171745Srrs goto out_now; 5594171745Srrs } 5595163953Srrs } 5596163953Srrs if (IS_SCTP_CONTROL(ch)) { 5597163953Srrs /* process the control portion of the SCTP packet */ 5598169655Srrs /* sa_ignore NO_NULL_CHK */ 5599163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5600179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5601163953Srrs if (stcb) { 5602163953Srrs /* 5603163953Srrs * This covers us if the cookie-echo was there and 5604163953Srrs * it changes our INP. 5605163953Srrs */ 5606163953Srrs inp = stcb->sctp_ep; 5607179783Srrs if ((net) && (port)) { 5608179783Srrs if (net->port == 0) { 5609228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5610179783Srrs } 5611179783Srrs net->port = port; 5612179783Srrs } 5613163953Srrs } 5614163953Srrs } else { 5615163953Srrs /* 5616163953Srrs * no control chunks, so pre-process DATA chunks (these 5617163953Srrs * checks are taken care of by control processing) 5618163953Srrs */ 5619163953Srrs 5620163953Srrs /* 5621163953Srrs * if DATA only packet, and auth is required, then punt... 5622163953Srrs * can't have authenticated without any AUTH (control) 5623163953Srrs * chunks 5624163953Srrs */ 5625179783Srrs if ((stcb != NULL) && 5626179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5627179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5628163953Srrs /* "silently" ignore */ 5629163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5630163953Srrs SCTP_TCB_UNLOCK(stcb); 5631171440Srrs goto out_now; 5632163953Srrs } 5633163953Srrs if (stcb == NULL) { 5634163953Srrs /* out of the blue DATA chunk */ 5635169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5636179157Srrs vrf_id, port); 5637171440Srrs goto out_now; 5638163953Srrs } 5639163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5640163953Srrs /* v_tag mismatch! */ 5641163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5642163953Srrs SCTP_TCB_UNLOCK(stcb); 5643171440Srrs goto out_now; 5644163953Srrs } 5645163953Srrs } 5646163953Srrs 5647163953Srrs if (stcb == NULL) { 5648163953Srrs /* 5649163953Srrs * no valid TCB for this packet, or we found it's a bad 5650163953Srrs * packet while processing control, or we're done with this 5651163953Srrs * packet (done or skip rest of data), so we drop it... 5652163953Srrs */ 5653171440Srrs goto out_now; 5654163953Srrs } 5655163953Srrs /* 5656163953Srrs * DATA chunk processing 5657163953Srrs */ 5658163953Srrs /* plow through the data chunks while length > offset */ 5659163953Srrs 5660163953Srrs /* 5661163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5662163953Srrs * DATA is required. 5663163953Srrs */ 5664179783Srrs if ((length > offset) && 5665179783Srrs (stcb != NULL) && 5666179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5667179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5668163953Srrs !stcb->asoc.authenticated) { 5669163953Srrs /* "silently" ignore */ 5670163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5671169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5672169420Srrs "Data chunk requires AUTH, skipped\n"); 5673165220Srrs goto trigger_send; 5674163953Srrs } 5675163953Srrs if (length > offset) { 5676163953Srrs int retval; 5677163953Srrs 5678163953Srrs /* 5679163953Srrs * First check to make sure our state is correct. We would 5680163953Srrs * not get here unless we really did have a tag, so we don't 5681163953Srrs * abort if this happens, just dump the chunk silently. 5682163953Srrs */ 5683163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5684163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5685163953Srrs /* 5686163953Srrs * we consider data with valid tags in this state 5687163953Srrs * shows us the cookie-ack was lost. Imply it was 5688163953Srrs * there. 5689163953Srrs */ 5690179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5691171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5692171943Srrs stcb->asoc.overall_error_count, 5693171943Srrs 0, 5694171943Srrs SCTP_FROM_SCTP_INPUT, 5695171943Srrs __LINE__); 5696171943Srrs } 5697163953Srrs stcb->asoc.overall_error_count = 0; 5698163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5699163953Srrs break; 5700163953Srrs case SCTP_STATE_COOKIE_WAIT: 5701163953Srrs /* 5702163953Srrs * We consider OOTB any data sent during asoc setup. 5703163953Srrs */ 5704169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5705179157Srrs vrf_id, port); 5706163953Srrs SCTP_TCB_UNLOCK(stcb); 5707171440Srrs goto out_now; 5708171531Srrs /* sa_ignore NOTREACHED */ 5709163953Srrs break; 5710163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5711163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5712163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5713163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5714163953Srrs default: 5715163953Srrs SCTP_TCB_UNLOCK(stcb); 5716171440Srrs goto out_now; 5717171531Srrs /* sa_ignore NOTREACHED */ 5718163953Srrs break; 5719163953Srrs case SCTP_STATE_OPEN: 5720163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5721163953Srrs break; 5722163953Srrs } 5723163953Srrs /* plow through the data chunks while length > offset */ 5724163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5725163953Srrs inp, stcb, net, &high_tsn); 5726163953Srrs if (retval == 2) { 5727163953Srrs /* 5728163953Srrs * The association aborted, NO UNLOCK needed since 5729163953Srrs * the association is destroyed. 5730163953Srrs */ 5731171440Srrs goto out_now; 5732163953Srrs } 5733163953Srrs data_processed = 1; 5734163953Srrs /* 5735163953Srrs * Anything important needs to have been m_copy'ed in 5736163953Srrs * process_data 5737163953Srrs */ 5738163953Srrs } 5739218129Srrs /* take care of ecn */ 5740228907Stuexen if ((data_processed == 1) && 5741228907Stuexen (stcb->asoc.ecn_allowed == 1) && 5742218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5743218129Srrs /* Yep, we need to add a ECNE */ 5744218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5745218129Srrs } 5746163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5747216495Stuexen int was_a_gap; 5748216495Stuexen uint32_t highest_tsn; 5749163953Srrs 5750216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5751216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5752216495Stuexen } else { 5753216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5754163953Srrs } 5755216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5756190689Srrs stcb->asoc.send_sack = 1; 5757228653Stuexen sctp_sack_check(stcb, was_a_gap); 5758190689Srrs } else if (fwd_tsn_seen) { 5759190689Srrs stcb->asoc.send_sack = 1; 5760163953Srrs } 5761163953Srrs /* trigger send of any chunks in queue... */ 5762165220Srrstrigger_send: 5763163953Srrs#ifdef SCTP_AUDITING_ENABLED 5764163953Srrs sctp_audit_log(0xE0, 2); 5765163953Srrs sctp_auditing(1, inp, stcb, net); 5766163953Srrs#endif 5767169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5768169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5769169420Srrs stcb->asoc.peers_rwnd, 5770169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5771169420Srrs stcb->asoc.total_flight); 5772163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5773218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5774218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5775218129Srrs } 5776218129Srrs if (cnt_ctrl_ready || 5777163953Srrs ((un_sent) && 5778163953Srrs (stcb->asoc.peers_rwnd > 0 || 5779163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5780169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5781172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5782169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5783163953Srrs } 5784163953Srrs#ifdef SCTP_AUDITING_ENABLED 5785163953Srrs sctp_audit_log(0xE0, 3); 5786163953Srrs sctp_auditing(2, inp, stcb, net); 5787163953Srrs#endif 5788163953Srrs SCTP_TCB_UNLOCK(stcb); 5789171440Srrsout_now: 5790171440Srrs#ifdef INVARIANTS 5791171440Srrs sctp_validate_no_locks(inp); 5792171440Srrs#endif 5793169378Srrs return; 5794163953Srrs} 5795163953Srrs 5796185694Srrs#if 0 5797185694Srrsstatic void 5798185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5799185694Srrs{ 5800185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5801234995Stuexen SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5802185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5803234995Stuexen SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5804185694Srrs } 5805185694Srrs} 5806163953Srrs 5807185694Srrs#endif 5808185694Srrs 5809221249Stuexen#ifdef INET 5810163953Srrsvoid 5811189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5812163953Srrs{ 5813165647Srrs struct mbuf *m; 5814163953Srrs int iphlen; 5815170181Srrs uint32_t vrf_id = 0; 5816163953Srrs uint8_t ecn_bits; 5817163953Srrs struct ip *ip; 5818163953Srrs struct sctphdr *sh; 5819163953Srrs struct sctp_inpcb *inp = NULL; 5820163953Srrs struct sctp_nets *net; 5821163953Srrs struct sctp_tcb *stcb = NULL; 5822163953Srrs struct sctp_chunkhdr *ch; 5823163953Srrs int refcount_up = 0; 5824163953Srrs int length, mlen, offset; 5825163953Srrs 5826211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5827211969Stuexen uint32_t check, calc_check; 5828211969Stuexen 5829211969Stuexen#endif 5830211969Stuexen 5831169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5832169352Srrs SCTP_RELEASE_PKT(i_pak); 5833169352Srrs return; 5834169352Srrs } 5835168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5836163953Srrs iphlen = off; 5837165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5838170056Srrs 5839163953Srrs net = NULL; 5840163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5841163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5842163953Srrs 5843170091Srrs 5844163953Srrs#ifdef SCTP_MBUF_LOGGING 5845163953Srrs /* Log in any input mbufs */ 5846179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5847234461Stuexen struct mbuf *mat; 5848234461Stuexen 5849228907Stuexen for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 5850170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5851170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5852170744Srrs } 5853163953Srrs } 5854163953Srrs } 5855163953Srrs#endif 5856170091Srrs#ifdef SCTP_PACKET_LOGGING 5857179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5858170921Srrs sctp_packet_log(m, mlen); 5859170091Srrs#endif 5860170091Srrs /* 5861170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5862170091Srrs * case) 5863170091Srrs */ 5864170091Srrs mlen -= iphlen; 5865168124Srrs 5866163953Srrs /* 5867163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5868163953Srrs */ 5869163953Srrs ip = mtod(m, struct ip *); 5870163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5871165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5872163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5873163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5874163953Srrs return; 5875163953Srrs } 5876163953Srrs ip = mtod(m, struct ip *); 5877163953Srrs } 5878188067Srrs /* validate mbuf chain length with IP payload length */ 5879188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5880188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5881188067Srrs goto bad; 5882188067Srrs } 5883163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5884163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5885170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5886170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5887163953Srrs 5888163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5889163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5890163953Srrs goto bad; 5891163953Srrs } 5892168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5893163953Srrs /* 5894163953Srrs * We only look at broadcast if its a front state, All 5895163953Srrs * others we will not have a tcb for anyway. 5896163953Srrs */ 5897163953Srrs goto bad; 5898163953Srrs } 5899163953Srrs /* validate SCTP checksum */ 5900188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5901188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5902188067Srrs m->m_pkthdr.len, 5903188067Srrs if_name(m->m_pkthdr.rcvif), 5904188067Srrs m->m_pkthdr.csum_flags); 5905211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5906211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5907211969Stuexen#else 5908188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5909188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5910188067Srrs goto sctp_skip_csum_4; 5911188067Srrs } 5912171990Srrs check = sh->checksum; /* save incoming checksum */ 5913171990Srrs sh->checksum = 0; /* prepare for calc */ 5914188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5915203503Stuexen sh->checksum = check; 5916188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5917171990Srrs if (calc_check != check) { 5918171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5919171990Srrs calc_check, check, m, mlen, iphlen); 5920163953Srrs 5921228653Stuexen stcb = sctp_findassociation_addr(m, 5922171990Srrs offset - sizeof(*ch), 5923171990Srrs sh, ch, &inp, &net, 5924171990Srrs vrf_id); 5925179157Srrs if ((net) && (port)) { 5926179157Srrs if (net->port == 0) { 5927228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5928179157Srrs } 5929179157Srrs net->port = port; 5930179157Srrs } 5931218400Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5932218400Stuexen net->flowid = m->m_pkthdr.flowid; 5933218400Stuexen#ifdef INVARIANTS 5934218400Stuexen net->flowidset = 1; 5935218400Stuexen#endif 5936218400Stuexen } 5937171990Srrs if ((inp) && (stcb)) { 5938171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5939172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5940171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5941171990Srrs refcount_up = 1; 5942163953Srrs } 5943171990Srrs SCTP_STAT_INCR(sctps_badsum); 5944171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5945171990Srrs goto bad; 5946168299Srrs } 5947163953Srrssctp_skip_csum_4: 5948211969Stuexen#endif 5949168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5950168124Srrs if (sh->dest_port == 0) { 5951168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5952168124Srrs goto bad; 5953168124Srrs } 5954163953Srrs /* 5955163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5956163953Srrs * IP/SCTP/first chunk header... 5957163953Srrs */ 5958228653Stuexen stcb = sctp_findassociation_addr(m, offset - sizeof(*ch), 5959168299Srrs sh, ch, &inp, &net, vrf_id); 5960179157Srrs if ((net) && (port)) { 5961179157Srrs if (net->port == 0) { 5962228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5963179157Srrs } 5964179157Srrs net->port = port; 5965179157Srrs } 5966218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5967218335Stuexen net->flowid = m->m_pkthdr.flowid; 5968218400Stuexen#ifdef INVARIANTS 5969218392Srrs net->flowidset = 1; 5970218400Stuexen#endif 5971218335Stuexen } 5972163953Srrs /* inp's ref-count increased && stcb locked */ 5973163953Srrs if (inp == NULL) { 5974163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5975163953Srrs 5976163953Srrs SCTP_STAT_INCR(sctps_noport); 5977163953Srrs#ifdef ICMP_BANDLIM 5978163953Srrs /* 5979163953Srrs * we use the bandwidth limiting to protect against sending 5980163953Srrs * too many ABORTS all at once. In this case these count the 5981163953Srrs * same as an ICMP message. 5982163953Srrs */ 5983163953Srrs if (badport_bandlim(0) < 0) 5984163953Srrs goto bad; 5985163953Srrs#endif /* ICMP_BANDLIM */ 5986169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5987169420Srrs "Sending a ABORT from packet entry!\n"); 5988163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5989163953Srrs /* 5990163953Srrs * we do a trick here to get the INIT tag, dig in 5991163953Srrs * and get the tag from the INIT and put it in the 5992163953Srrs * common header. 5993163953Srrs */ 5994163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5995163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5996163953Srrs (uint8_t *) & chunk_buf); 5997163953Srrs if (init_chk != NULL) 5998163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5999163953Srrs } 6000165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 6001228653Stuexen sctp_send_shutdown_complete2(m, sh, vrf_id, port); 6002165220Srrs goto bad; 6003165220Srrs } 6004165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 6005165220Srrs goto bad; 6006165220Srrs } 6007229805Stuexen if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 6008229805Stuexen if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 6009229805Stuexen ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 6010229805Stuexen (ch->chunk_type != SCTP_INIT))) { 6011229805Stuexen sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 6012229805Stuexen } 6013229805Stuexen } 6014163953Srrs goto bad; 6015163953Srrs } else if (stcb == NULL) { 6016163953Srrs refcount_up = 1; 6017163953Srrs } 6018171167Sgnn#ifdef IPSEC 6019163953Srrs /* 6020163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 6021163953Srrs * idea, so I will leave it in place. 6022163953Srrs */ 6023163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 6024197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 6025163953Srrs SCTP_STAT_INCR(sctps_hdrops); 6026163953Srrs goto bad; 6027163953Srrs } 6028163953Srrs#endif /* IPSEC */ 6029163953Srrs 6030163953Srrs /* 6031163953Srrs * common chunk processing 6032163953Srrs */ 6033163953Srrs length = ip->ip_len + iphlen; 6034163953Srrs offset -= sizeof(struct sctp_chunkhdr); 6035163953Srrs 6036163953Srrs ecn_bits = ip->ip_tos; 6037163953Srrs 6038169655Srrs /* sa_ignore NO_NULL_CHK */ 6039163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 6040179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 6041163953Srrs /* inp's ref-count reduced && stcb unlocked */ 6042163953Srrs if (m) { 6043163953Srrs sctp_m_freem(m); 6044163953Srrs } 6045163953Srrs if ((inp) && (refcount_up)) { 6046163953Srrs /* reduce ref-count */ 6047163953Srrs SCTP_INP_DECR_REF(inp); 6048163953Srrs } 6049163953Srrs return; 6050163953Srrsbad: 6051169420Srrs if (stcb) { 6052163953Srrs SCTP_TCB_UNLOCK(stcb); 6053169420Srrs } 6054163953Srrs if ((inp) && (refcount_up)) { 6055163953Srrs /* reduce ref-count */ 6056163953Srrs SCTP_INP_DECR_REF(inp); 6057163953Srrs } 6058163953Srrs if (m) { 6059163953Srrs sctp_m_freem(m); 6060163953Srrs } 6061163953Srrs return; 6062163953Srrs} 6063218211Srrs 6064218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6065218269Srrsextern int *sctp_cpuarry; 6066218211Srrs 6067218269Srrs#endif 6068218269Srrs 6069179157Srrsvoid 6070218211Srrssctp_input(struct mbuf *m, int off) 6071179157Srrs{ 6072218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6073218211Srrs struct ip *ip; 6074218211Srrs struct sctphdr *sh; 6075218211Srrs int offset; 6076218211Srrs int cpu_to_use; 6077218371Srrs uint32_t flowid, tag; 6078218211Srrs 6079218269Srrs if (mp_ncpus > 1) { 6080218371Srrs if (m->m_flags & M_FLOWID) { 6081218371Srrs flowid = m->m_pkthdr.flowid; 6082218269Srrs } else { 6083218269Srrs /* 6084218371Srrs * No flow id built by lower layers fix it so we 6085218371Srrs * create one. 6086218269Srrs */ 6087218371Srrs ip = mtod(m, struct ip *); 6088218371Srrs offset = off + sizeof(*sh); 6089218371Srrs if (SCTP_BUF_LEN(m) < offset) { 6090218371Srrs if ((m = m_pullup(m, offset)) == 0) { 6091218371Srrs SCTP_STAT_INCR(sctps_hdrops); 6092218371Srrs return; 6093218371Srrs } 6094218371Srrs ip = mtod(m, struct ip *); 6095218371Srrs } 6096218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 6097218371Srrs tag = htonl(sh->v_tag); 6098218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6099218371Srrs m->m_pkthdr.flowid = flowid; 6100218371Srrs m->m_flags |= M_FLOWID; 6101218269Srrs } 6102218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6103218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 6104218211Srrs return; 6105218211Srrs } 6106218211Srrs#endif 6107218211Srrs sctp_input_with_port(m, off, 0); 6108179157Srrs} 6109221249Stuexen 6110221249Stuexen#endif 6111