sctp_input.c revision 235416
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 235416 2012-05-13 19:32:49Z 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; 1114163953Srrs uint16_t 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); 1129163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1130163953Srrs /* Process an Error Cause */ 1131163953Srrs error_type = ntohs(phdr->param_type); 1132163953Srrs error_len = ntohs(phdr->param_length); 1133163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1134163953Srrs /* invalid param length for this param */ 1135169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1136169420Srrs chklen, error_len); 1137163953Srrs return (0); 1138163953Srrs } 1139163953Srrs switch (error_type) { 1140163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1141163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1142163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1143163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1144169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1145169420Srrs error_type); 1146163953Srrs break; 1147185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1148185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1149185694Srrs ch->chunk_flags); 1150185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1151185694Srrs return (0); 1152185694Srrs } 1153185694Srrs break; 1154185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1155185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1156185694Srrs ch->chunk_flags); 1157185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1158185694Srrs return (0); 1159185694Srrs } 1160185694Srrs break; 1161163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1162163953Srrs /* 1163163953Srrs * We only act if we have echoed a cookie and are 1164163953Srrs * waiting. 1165163953Srrs */ 1166163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1167163953Srrs int *p; 1168163953Srrs 1169163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1170163953Srrs /* Save the time doubled */ 1171163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1172163953Srrs asoc->stale_cookie_count++; 1173163953Srrs if (asoc->stale_cookie_count > 1174163953Srrs asoc->max_init_times) { 1175235403Stuexen sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); 1176163953Srrs /* now free the asoc */ 1177172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1178172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1179172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1180172090Srrs SCTP_TCB_UNLOCK(stcb); 1181172090Srrs SCTP_SOCKET_LOCK(so, 1); 1182172090Srrs SCTP_TCB_LOCK(stcb); 1183172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1184172090Srrs#endif 1185171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1186171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1187172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1188172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1189172090Srrs#endif 1190163953Srrs return (-1); 1191163953Srrs } 1192163953Srrs /* blast back to INIT state */ 1193185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1194163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1195163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1196163953Srrs sctp_stop_all_cookie_timers(stcb); 1197172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1198163953Srrs } 1199163953Srrs break; 1200163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1201163953Srrs /* 1202163953Srrs * Nothing we can do here, we don't do hostname 1203163953Srrs * addresses so if the peer does not like my IPv6 1204163953Srrs * (or IPv4 for that matter) it does not matter. If 1205163953Srrs * they don't support that type of address, they can 1206163953Srrs * NOT possibly get that packet type... i.e. with no 1207163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1208163953Srrs * safely ignore this one. If we ever added support 1209163953Srrs * for HOSTNAME Addresses, then we would need to do 1210163953Srrs * something here. 1211163953Srrs */ 1212163953Srrs break; 1213163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1214163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1215163953Srrs break; 1216163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1217163953Srrs sctp_process_unrecog_param(stcb, phdr); 1218163953Srrs break; 1219163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1220163953Srrs /* 1221163953Srrs * We ignore this since the timer will drive out a 1222163953Srrs * new cookie anyway and there timer will drive us 1223163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1224163953Srrs * here since we don't have their tag. 1225163953Srrs */ 1226163953Srrs break; 1227163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1228163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1229163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1230163953Srrs /* 1231163953Srrs * We should NOT get these here, but in a 1232166086Srrs * ASCONF-ACK. 1233163953Srrs */ 1234169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1235169420Srrs error_type); 1236163953Srrs break; 1237163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1238163953Srrs /* 1239163953Srrs * And what, pray tell do we do with the fact that 1240163953Srrs * the peer is out of resources? Not really sure we 1241166086Srrs * could do anything but abort. I suspect this 1242163953Srrs * should have came WITH an abort instead of in a 1243163953Srrs * OP-ERROR. 1244163953Srrs */ 1245163953Srrs break; 1246163953Srrs default: 1247169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1248169420Srrs error_type); 1249163953Srrs break; 1250163953Srrs } 1251163953Srrs adjust = SCTP_SIZE32(error_len); 1252163953Srrs chklen -= adjust; 1253163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1254163953Srrs } 1255163953Srrs return (0); 1256163953Srrs} 1257163953Srrs 1258163953Srrsstatic int 1259169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1260169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1261170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1262163953Srrs{ 1263163953Srrs struct sctp_init_ack *init_ack; 1264163953Srrs struct mbuf *op_err; 1265163953Srrs 1266169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1267169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1268169420Srrs 1269163953Srrs if (stcb == NULL) { 1270169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1271169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1272163953Srrs return (-1); 1273163953Srrs } 1274163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1275163953Srrs /* Invalid length */ 1276163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1277163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1278179157Srrs op_err, 0, net->port); 1279168299Srrs *abort_no_unlock = 1; 1280163953Srrs return (-1); 1281163953Srrs } 1282163953Srrs init_ack = &cp->init; 1283163953Srrs /* validate parameters */ 1284163953Srrs if (init_ack->initiate_tag == 0) { 1285163953Srrs /* protocol error... send an abort */ 1286163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1287163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1288179157Srrs op_err, 0, net->port); 1289168299Srrs *abort_no_unlock = 1; 1290163953Srrs return (-1); 1291163953Srrs } 1292163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1293163953Srrs /* protocol error... send an abort */ 1294163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1295163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1296179157Srrs op_err, 0, net->port); 1297168299Srrs *abort_no_unlock = 1; 1298163953Srrs return (-1); 1299163953Srrs } 1300163953Srrs if (init_ack->num_inbound_streams == 0) { 1301163953Srrs /* protocol error... send an abort */ 1302163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1303163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1304179157Srrs op_err, 0, net->port); 1305168299Srrs *abort_no_unlock = 1; 1306163953Srrs return (-1); 1307163953Srrs } 1308163953Srrs if (init_ack->num_outbound_streams == 0) { 1309163953Srrs /* protocol error... send an abort */ 1310163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1311163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1312179157Srrs op_err, 0, net->port); 1313168299Srrs *abort_no_unlock = 1; 1314163953Srrs return (-1); 1315163953Srrs } 1316163953Srrs /* process according to association state... */ 1317171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1318163953Srrs case SCTP_STATE_COOKIE_WAIT: 1319163953Srrs /* this is the expected state for this chunk */ 1320163953Srrs /* process the INIT-ACK parameters */ 1321163953Srrs if (stcb->asoc.primary_destination->dest_state & 1322163953Srrs SCTP_ADDR_UNCONFIRMED) { 1323163953Srrs /* 1324163953Srrs * The primary is where we sent the INIT, we can 1325163953Srrs * always consider it confirmed when the INIT-ACK is 1326163953Srrs * returned. Do this before we load addresses 1327163953Srrs * though. 1328163953Srrs */ 1329163953Srrs stcb->asoc.primary_destination->dest_state &= 1330163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1331163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1332172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1333163953Srrs } 1334169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1335170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1336163953Srrs /* error in parsing parameters */ 1337163953Srrs return (-1); 1338163953Srrs } 1339163953Srrs /* update our state */ 1340169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1341171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1342163953Srrs 1343163953Srrs /* reset the RTO calc */ 1344179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1345171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1346171943Srrs stcb->asoc.overall_error_count, 1347171943Srrs 0, 1348171943Srrs SCTP_FROM_SCTP_INPUT, 1349171943Srrs __LINE__); 1350171943Srrs } 1351163953Srrs stcb->asoc.overall_error_count = 0; 1352169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1353163953Srrs /* 1354163953Srrs * collapse the init timer back in case of a exponential 1355165220Srrs * backoff 1356163953Srrs */ 1357163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1358163953Srrs stcb, net); 1359163953Srrs /* 1360163953Srrs * the send at the end of the inbound data processing will 1361163953Srrs * cause the cookie to be sent 1362163953Srrs */ 1363163953Srrs break; 1364163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1365163953Srrs /* incorrect state... discard */ 1366163953Srrs break; 1367163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1368163953Srrs /* incorrect state... discard */ 1369163953Srrs break; 1370163953Srrs case SCTP_STATE_OPEN: 1371163953Srrs /* incorrect state... discard */ 1372163953Srrs break; 1373163953Srrs case SCTP_STATE_EMPTY: 1374163953Srrs case SCTP_STATE_INUSE: 1375163953Srrs default: 1376163953Srrs /* incorrect state... discard */ 1377163953Srrs return (-1); 1378163953Srrs break; 1379163953Srrs } 1380169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1381163953Srrs return (0); 1382163953Srrs} 1383163953Srrs 1384185694Srrsstatic struct sctp_tcb * 1385185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1386185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1387185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1388185694Srrs struct sockaddr *init_src, int *notification, 1389185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1390185694Srrs uint32_t vrf_id, uint16_t port); 1391163953Srrs 1392185694Srrs 1393163953Srrs/* 1394163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1395163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1396163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1397163953Srrs * mbuf to the cookie-echo chunk 1398163953Srrs */ 1399163953Srrsstatic struct sctp_tcb * 1400163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1401163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1402185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1403228653Stuexen struct sockaddr *init_src, int *notification, 1404185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1405163953Srrs{ 1406163953Srrs struct sctp_association *asoc; 1407163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1408163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1409185694Srrs struct sctp_nets *net; 1410185694Srrs struct mbuf *op_err; 1411185694Srrs struct sctp_paramhdr *ph; 1412165220Srrs int init_offset, initack_offset, i; 1413163953Srrs int retval; 1414164205Srrs int spec_flag = 0; 1415170140Srrs uint32_t how_indx; 1416163953Srrs 1417185694Srrs net = *netp; 1418163953Srrs /* I know that the TCB is non-NULL from the caller */ 1419163953Srrs asoc = &stcb->asoc; 1420166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1421166023Srrs if (asoc->cookie_how[how_indx] == 0) 1422166023Srrs break; 1423166023Srrs } 1424166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1425166023Srrs asoc->cookie_how[how_indx] = 1; 1426166023Srrs } 1427163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1428163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1429163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1430163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1431165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1432163953Srrs if (op_err == NULL) { 1433163953Srrs /* FOOBAR */ 1434163953Srrs return (NULL); 1435163953Srrs } 1436163953Srrs /* Set the len */ 1437165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1438163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1439163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1440163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1441169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1442179157Srrs vrf_id, net->port); 1443166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1444166023Srrs asoc->cookie_how[how_indx] = 2; 1445163953Srrs return (NULL); 1446163953Srrs } 1447163953Srrs /* 1448163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1449163953Srrs * INIT should start after the cookie-echo header struct (chunk 1450163953Srrs * header, state cookie header struct) 1451163953Srrs */ 1452163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1453163953Srrs 1454163953Srrs init_cp = (struct sctp_init_chunk *) 1455163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1456163953Srrs (uint8_t *) & init_buf); 1457163953Srrs if (init_cp == NULL) { 1458163953Srrs /* could not pull a INIT chunk in cookie */ 1459163953Srrs return (NULL); 1460163953Srrs } 1461163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1462163953Srrs return (NULL); 1463163953Srrs } 1464163953Srrs /* 1465163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1466163953Srrs * INIT-ACK follows the INIT chunk 1467163953Srrs */ 1468228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1469163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1470163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1471163953Srrs (uint8_t *) & initack_buf); 1472163953Srrs if (initack_cp == NULL) { 1473163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1474163953Srrs return (NULL); 1475163953Srrs } 1476163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1477163953Srrs return (NULL); 1478163953Srrs } 1479163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1480163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1481163953Srrs /* 1482163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1483163953Srrs * to get into the OPEN state 1484163953Srrs */ 1485166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1486172091Srrs /*- 1487172091Srrs * Opps, this means that we somehow generated two vtag's 1488172091Srrs * the same. I.e. we did: 1489172091Srrs * Us Peer 1490172091Srrs * <---INIT(tag=a)------ 1491172091Srrs * ----INIT-ACK(tag=t)--> 1492172091Srrs * ----INIT(tag=t)------> *1 1493172091Srrs * <---INIT-ACK(tag=a)--- 1494172091Srrs * <----CE(tag=t)------------- *2 1495172091Srrs * 1496172091Srrs * At point *1 we should be generating a different 1497172091Srrs * tag t'. Which means we would throw away the CE and send 1498172091Srrs * ours instead. Basically this is case C (throw away side). 1499172091Srrs */ 1500172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1501172091Srrs asoc->cookie_how[how_indx] = 17; 1502172091Srrs return (NULL); 1503172091Srrs 1504166023Srrs } 1505163953Srrs switch SCTP_GET_STATE 1506163953Srrs (asoc) { 1507163953Srrs case SCTP_STATE_COOKIE_WAIT: 1508166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1509163953Srrs /* 1510169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1511166023Srrs * correct tags... just accept it...but we must 1512166023Srrs * process the init so that we can make sure we have 1513166023Srrs * the right seq no's. 1514163953Srrs */ 1515163953Srrs /* First we must process the INIT !! */ 1516228653Stuexen retval = sctp_process_init(init_cp, stcb); 1517163953Srrs if (retval < 0) { 1518166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1519166023Srrs asoc->cookie_how[how_indx] = 3; 1520163953Srrs return (NULL); 1521163953Srrs } 1522163953Srrs /* we have already processed the INIT so no problem */ 1523163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1524171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1525171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1526163953Srrs /* update current state */ 1527166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1528166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1529166675Srrs else 1530166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1531171943Srrs 1532171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1533163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1534163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1535163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1536163953Srrs } 1537166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1538165220Srrs sctp_stop_all_cookie_timers(stcb); 1539163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1540163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1541163953Srrs (inp->sctp_socket->so_qlimit == 0) 1542163953Srrs ) { 1543172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1544172090Srrs struct socket *so; 1545172090Srrs 1546172090Srrs#endif 1547163953Srrs /* 1548163953Srrs * Here is where collision would go if we 1549163953Srrs * did a connect() and instead got a 1550163953Srrs * init/init-ack/cookie done before the 1551163953Srrs * init-ack came back.. 1552163953Srrs */ 1553163953Srrs stcb->sctp_ep->sctp_flags |= 1554163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1555172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1556172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1557172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1558172090Srrs SCTP_TCB_UNLOCK(stcb); 1559172090Srrs SCTP_SOCKET_LOCK(so, 1); 1560172090Srrs SCTP_TCB_LOCK(stcb); 1561172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1562172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1563172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1564172090Srrs return (NULL); 1565172090Srrs } 1566172090Srrs#endif 1567172090Srrs soisconnected(stcb->sctp_socket); 1568172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1569172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1570172090Srrs#endif 1571163953Srrs } 1572163953Srrs /* notify upper layer */ 1573163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1574163953Srrs /* 1575163953Srrs * since we did not send a HB make sure we don't 1576163953Srrs * double things 1577163953Srrs */ 1578163953Srrs net->hb_responded = 1; 1579170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1580218186Srrs &cookie->time_entered, 1581218186Srrs sctp_align_unsafe_makecopy, 1582219397Srrs SCTP_RTT_FROM_NON_DATA); 1583163953Srrs 1584163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1585163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1586163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1587163953Srrs inp, stcb, NULL); 1588163953Srrs } 1589163953Srrs break; 1590163953Srrs default: 1591163953Srrs /* 1592163953Srrs * we're in the OPEN state (or beyond), so peer must 1593163953Srrs * have simply lost the COOKIE-ACK 1594163953Srrs */ 1595163953Srrs break; 1596163953Srrs } /* end switch */ 1597165220Srrs sctp_stop_all_cookie_timers(stcb); 1598163953Srrs /* 1599163953Srrs * We ignore the return code here.. not sure if we should 1600163953Srrs * somehow abort.. but we do have an existing asoc. This 1601163953Srrs * really should not fail. 1602163953Srrs */ 1603228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1604163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1605163953Srrs initack_offset, sh, init_src)) { 1606166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1607166023Srrs asoc->cookie_how[how_indx] = 4; 1608163953Srrs return (NULL); 1609163953Srrs } 1610163953Srrs /* respond with a COOKIE-ACK */ 1611165220Srrs sctp_toss_old_cookies(stcb, asoc); 1612163953Srrs sctp_send_cookie_ack(stcb); 1613166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1614166023Srrs asoc->cookie_how[how_indx] = 5; 1615163953Srrs return (stcb); 1616169352Srrs } 1617163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1618163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1619163953Srrs cookie->tie_tag_my_vtag == 0 && 1620163953Srrs cookie->tie_tag_peer_vtag == 0) { 1621163953Srrs /* 1622163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1623163953Srrs */ 1624166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1625166023Srrs asoc->cookie_how[how_indx] = 6; 1626163953Srrs return (NULL); 1627163953Srrs } 1628185694Srrs /* 1629185694Srrs * If nat support, and the below and stcb is established, send back 1630185694Srrs * a ABORT(colliding state) if we are established. 1631185694Srrs */ 1632185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1633185694Srrs (asoc->peer_supports_nat) && 1634185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1635185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1636185694Srrs (asoc->peer_vtag == 0)))) { 1637163953Srrs /* 1638185694Srrs * Special case - Peer's support nat. We may have two init's 1639185694Srrs * that we gave out the same tag on since one was not 1640185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1641185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1642185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1643185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1644185694Srrs * Now we have colliding state. We must send an abort here 1645185694Srrs * with colliding state indication. 1646185694Srrs */ 1647185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1648185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1649185694Srrs if (op_err == NULL) { 1650185694Srrs /* FOOBAR */ 1651185694Srrs return (NULL); 1652185694Srrs } 1653185694Srrs /* pre-reserve some space */ 1654185694Srrs#ifdef INET6 1655185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1656185694Srrs#else 1657185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1658185694Srrs#endif 1659185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1660185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1661185694Srrs /* Set the len */ 1662185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1663185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1664185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1665185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1666185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1667185694Srrs return (NULL); 1668185694Srrs } 1669185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1670185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1671185694Srrs (asoc->peer_vtag == 0))) { 1672185694Srrs /* 1673163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1674163953Srrs * should be ok, re-accept peer info 1675163953Srrs */ 1676166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1677166023Srrs /* 1678166023Srrs * Extension of case C. If we hit this, then the 1679166023Srrs * random number generator returned the same vtag 1680166023Srrs * when we first sent our INIT-ACK and when we later 1681166023Srrs * sent our INIT. The side with the seq numbers that 1682166023Srrs * are different will be the one that normnally 1683166023Srrs * would have hit case C. This in effect "extends" 1684166023Srrs * our vtags in this collision case to be 64 bits. 1685166023Srrs * The same collision could occur aka you get both 1686166023Srrs * vtag and seq number the same twice in a row.. but 1687166023Srrs * is much less likely. If it did happen then we 1688166023Srrs * would proceed through and bring up the assoc.. we 1689166023Srrs * may end up with the wrong stream setup however.. 1690166023Srrs * which would be bad.. but there is no way to 1691166023Srrs * tell.. until we send on a stream that does not 1692166023Srrs * exist :-) 1693166023Srrs */ 1694166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1695166023Srrs asoc->cookie_how[how_indx] = 7; 1696166023Srrs 1697166023Srrs return (NULL); 1698166023Srrs } 1699166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1700166023Srrs asoc->cookie_how[how_indx] = 8; 1701171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1702163953Srrs sctp_stop_all_cookie_timers(stcb); 1703163953Srrs /* 1704163953Srrs * since we did not send a HB make sure we don't double 1705163953Srrs * things 1706163953Srrs */ 1707163953Srrs net->hb_responded = 1; 1708163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1709163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1710163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1711163953Srrs NULL); 1712163953Srrs } 1713163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1714164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1715164205Srrs 1716164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1717164205Srrs /* 1718164205Srrs * Ok the peer probably discarded our data (if we 1719164205Srrs * echoed a cookie+data). So anything on the 1720164205Srrs * sent_queue should be marked for retransmit, we 1721164205Srrs * may not get something to kick us so it COULD 1722164205Srrs * still take a timeout to move these.. but it can't 1723164205Srrs * hurt to mark them. 1724164205Srrs */ 1725164205Srrs struct sctp_tmit_chunk *chk; 1726163953Srrs 1727164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1728164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1729164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1730171440Srrs sctp_flight_size_decrease(chk); 1731171440Srrs sctp_total_flight_decrease(stcb, chk); 1732168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1733164205Srrs spec_flag++; 1734164205Srrs } 1735164205Srrs } 1736164205Srrs 1737164205Srrs } 1738163953Srrs /* process the INIT info (peer's info) */ 1739228653Stuexen retval = sctp_process_init(init_cp, stcb); 1740163953Srrs if (retval < 0) { 1741166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1742166023Srrs asoc->cookie_how[how_indx] = 9; 1743163953Srrs return (NULL); 1744163953Srrs } 1745228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1746163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1747163953Srrs initack_offset, sh, init_src)) { 1748166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1749166023Srrs asoc->cookie_how[how_indx] = 10; 1750163953Srrs return (NULL); 1751163953Srrs } 1752163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1753163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1754163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1755163953Srrs 1756163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1757163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1758163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1759172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1760172090Srrs struct socket *so; 1761172090Srrs 1762172090Srrs#endif 1763163953Srrs stcb->sctp_ep->sctp_flags |= 1764163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1765172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1766172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1767172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1768172090Srrs SCTP_TCB_UNLOCK(stcb); 1769172090Srrs SCTP_SOCKET_LOCK(so, 1); 1770172090Srrs SCTP_TCB_LOCK(stcb); 1771172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1772172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1773172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1774172090Srrs return (NULL); 1775172090Srrs } 1776172090Srrs#endif 1777172090Srrs soisconnected(stcb->sctp_socket); 1778172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1779172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1780172090Srrs#endif 1781163953Srrs } 1782166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1783166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1784166675Srrs else 1785166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1786166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1787166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1788166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1789166675Srrs } else { 1790166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1791163953Srrs } 1792171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1793163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1794163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1795163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1796163953Srrs } 1797163953Srrs sctp_stop_all_cookie_timers(stcb); 1798165220Srrs sctp_toss_old_cookies(stcb, asoc); 1799163953Srrs sctp_send_cookie_ack(stcb); 1800164205Srrs if (spec_flag) { 1801164205Srrs /* 1802164205Srrs * only if we have retrans set do we do this. What 1803164205Srrs * this call does is get only the COOKIE-ACK out and 1804164205Srrs * then when we return the normal call to 1805164205Srrs * sctp_chunk_output will get the retrans out behind 1806164205Srrs * this. 1807164205Srrs */ 1808172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1809164205Srrs } 1810166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1811166023Srrs asoc->cookie_how[how_indx] = 11; 1812166023Srrs 1813163953Srrs return (stcb); 1814163953Srrs } 1815163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1816163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1817163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1818163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1819163953Srrs cookie->tie_tag_peer_vtag != 0) { 1820163953Srrs struct sctpasochead *head; 1821163953Srrs 1822185694Srrs if (asoc->peer_supports_nat) { 1823185694Srrs /* 1824185694Srrs * This is a gross gross hack. just call the 1825185694Srrs * cookie_new code since we are allowing a duplicate 1826185694Srrs * association. I hope this works... 1827185694Srrs */ 1828185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1829185694Srrs inp, netp, init_src, notification, 1830185694Srrs auth_skipped, auth_offset, auth_len, 1831185694Srrs vrf_id, port)); 1832185694Srrs } 1833163953Srrs /* 1834163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1835163953Srrs */ 1836165220Srrs /* temp code */ 1837166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1838166023Srrs asoc->cookie_how[how_indx] = 12; 1839171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1840171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1841165647Srrs 1842163953Srrs /* notify upper layer */ 1843163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1844165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1845166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1846166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1847166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1848166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1849166675Srrs } 1850166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1851166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1852166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1853166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1854166675Srrs } 1855165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1856171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1857165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1858165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1859165647Srrs 1860165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1861165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1862171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1863165647Srrs } 1864165647Srrs asoc->pre_open_streams = 1865165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1866165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1867165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1868179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1869165647Srrs 1870165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1871165647Srrs 1872165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1873165647Srrs 1874165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1875170138Srrs if (asoc->mapping_array) { 1876165647Srrs memset(asoc->mapping_array, 0, 1877165647Srrs asoc->mapping_array_size); 1878170138Srrs } 1879205627Srrs if (asoc->nr_mapping_array) { 1880185694Srrs memset(asoc->nr_mapping_array, 0, 1881206137Stuexen asoc->mapping_array_size); 1882185694Srrs } 1883165220Srrs SCTP_TCB_UNLOCK(stcb); 1884165220Srrs SCTP_INP_INFO_WLOCK(); 1885165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1886165220Srrs SCTP_TCB_LOCK(stcb); 1887165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1888163953Srrs /* send up all the data */ 1889164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1890165220Srrs 1891235416Stuexen sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED); 1892165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1893165220Srrs stcb->asoc.strmout[i].stream_no = i; 1894165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1895165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1896165220Srrs } 1897163953Srrs /* process the INIT-ACK info (my info) */ 1898163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1899163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1900163953Srrs 1901163953Srrs /* pull from vtag hash */ 1902163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1903163953Srrs /* re-insert to new vtag position */ 1904179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1905179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1906163953Srrs /* 1907163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1908163953Srrs * system 1909163953Srrs */ 1910163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1911163953Srrs 1912163953Srrs /* process the INIT info (peer's info) */ 1913164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1914165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1915165220Srrs SCTP_INP_INFO_WUNLOCK(); 1916165220Srrs 1917228653Stuexen retval = sctp_process_init(init_cp, stcb); 1918163953Srrs if (retval < 0) { 1919166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1920166023Srrs asoc->cookie_how[how_indx] = 13; 1921166023Srrs 1922163953Srrs return (NULL); 1923163953Srrs } 1924163953Srrs /* 1925163953Srrs * since we did not send a HB make sure we don't double 1926163953Srrs * things 1927163953Srrs */ 1928163953Srrs net->hb_responded = 1; 1929163953Srrs 1930228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1931163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1932163953Srrs initack_offset, sh, init_src)) { 1933166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1934166023Srrs asoc->cookie_how[how_indx] = 14; 1935166023Srrs 1936163953Srrs return (NULL); 1937163953Srrs } 1938163953Srrs /* respond with a COOKIE-ACK */ 1939163953Srrs sctp_stop_all_cookie_timers(stcb); 1940165220Srrs sctp_toss_old_cookies(stcb, asoc); 1941163953Srrs sctp_send_cookie_ack(stcb); 1942166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1943166023Srrs asoc->cookie_how[how_indx] = 15; 1944163953Srrs 1945163953Srrs return (stcb); 1946163953Srrs } 1947166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1948166023Srrs asoc->cookie_how[how_indx] = 16; 1949163953Srrs /* all other cases... */ 1950163953Srrs return (NULL); 1951163953Srrs} 1952163953Srrs 1953166086Srrs 1954163953Srrs/* 1955163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1956163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1957163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1958163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1959163953Srrs * was from returns a new TCB 1960163953Srrs */ 1961185694Srrsstruct sctp_tcb * 1962163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1963163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1964163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1965163953Srrs struct sockaddr *init_src, int *notification, 1966169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1967179157Srrs uint32_t vrf_id, uint16_t port) 1968163953Srrs{ 1969163953Srrs struct sctp_tcb *stcb; 1970163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1971163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1972163953Srrs struct sockaddr_storage sa_store; 1973163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1974163953Srrs struct sctp_association *asoc; 1975163953Srrs int init_offset, initack_offset, initack_limit; 1976163953Srrs int retval; 1977163953Srrs int error = 0; 1978166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1979163953Srrs 1980221249Stuexen#ifdef INET 1981221249Stuexen struct sockaddr_in *sin; 1982221249Stuexen 1983221249Stuexen#endif 1984221249Stuexen#ifdef INET6 1985221249Stuexen struct sockaddr_in6 *sin6; 1986221249Stuexen 1987221249Stuexen#endif 1988172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1989172090Srrs struct socket *so; 1990172090Srrs 1991172090Srrs so = SCTP_INP_SO(inp); 1992172090Srrs#endif 1993172090Srrs 1994163953Srrs /* 1995163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1996163953Srrs * INIT should start after the cookie-echo header struct (chunk 1997163953Srrs * header, state cookie header struct) 1998163953Srrs */ 1999163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2000163953Srrs init_cp = (struct sctp_init_chunk *) 2001163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2002163953Srrs (uint8_t *) & init_buf); 2003163953Srrs if (init_cp == NULL) { 2004163953Srrs /* could not pull a INIT chunk in cookie */ 2005169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2006169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 2007163953Srrs return (NULL); 2008163953Srrs } 2009163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2010169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2011163953Srrs return (NULL); 2012163953Srrs } 2013228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2014163953Srrs /* 2015163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 2016163953Srrs * INIT-ACK follows the INIT chunk 2017163953Srrs */ 2018163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2019163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2020163953Srrs (uint8_t *) & initack_buf); 2021163953Srrs if (initack_cp == NULL) { 2022163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2023169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2024163953Srrs return (NULL); 2025163953Srrs } 2026163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2027163953Srrs return (NULL); 2028163953Srrs } 2029163953Srrs /* 2030163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2031163953Srrs * "initack_limit" value. This is because the chk_length field 2032163953Srrs * includes the length of the cookie, but the cookie is omitted when 2033163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2034163953Srrs */ 2035163953Srrs initack_limit = offset + cookie_len; 2036163953Srrs 2037163953Srrs /* 2038163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2039163953Srrs * and popluate 2040163953Srrs */ 2041171531Srrs 2042171531Srrs /* 2043171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2044171531Srrs * We do this since in effect we only use the p argument when the 2045171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2046171531Srrs * getting a cookie, we cannot be unbound. 2047171531Srrs */ 2048206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2049171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2050171531Srrs (struct thread *)NULL 2051171531Srrs ); 2052163953Srrs if (stcb == NULL) { 2053163953Srrs struct mbuf *op_err; 2054163953Srrs 2055163953Srrs /* memory problem? */ 2056169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2057169420Srrs "process_cookie_new: no room for another TCB!\n"); 2058163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2059170140Srrs 2060163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2061179157Srrs sh, op_err, vrf_id, port); 2062163953Srrs return (NULL); 2063163953Srrs } 2064163953Srrs /* get the correct sctp_nets */ 2065169420Srrs if (netp) 2066169420Srrs *netp = sctp_findnet(stcb, init_src); 2067169420Srrs 2068163953Srrs asoc = &stcb->asoc; 2069163953Srrs /* get scope variables out of cookie */ 2070163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2071163953Srrs asoc->site_scope = cookie->site_scope; 2072163953Srrs asoc->local_scope = cookie->local_scope; 2073163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2074163953Srrs 2075163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2076163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2077163953Srrs struct mbuf *op_err; 2078163953Srrs 2079163953Srrs /* 2080163953Srrs * Houston we have a problem. The EP changed while the 2081163953Srrs * cookie was in flight. Only recourse is to abort the 2082163953Srrs * association. 2083163953Srrs */ 2084170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2085163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2086163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2087179157Srrs sh, op_err, vrf_id, port); 2088172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2089172090Srrs SCTP_TCB_UNLOCK(stcb); 2090172090Srrs SCTP_SOCKET_LOCK(so, 1); 2091172090Srrs SCTP_TCB_LOCK(stcb); 2092172090Srrs#endif 2093171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2094171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2095172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2096172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2097172090Srrs#endif 2098172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2099163953Srrs return (NULL); 2100163953Srrs } 2101163953Srrs /* process the INIT-ACK info (my info) */ 2102185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2103163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2104163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2105163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2106163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2107179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2108163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2109163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2110163953Srrs 2111163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2112163953Srrs 2113163953Srrs /* process the INIT info (peer's info) */ 2114169420Srrs if (netp) 2115228653Stuexen retval = sctp_process_init(init_cp, stcb); 2116169420Srrs else 2117169420Srrs retval = 0; 2118163953Srrs if (retval < 0) { 2119170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2120172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2121172090Srrs SCTP_TCB_UNLOCK(stcb); 2122172090Srrs SCTP_SOCKET_LOCK(so, 1); 2123172090Srrs SCTP_TCB_LOCK(stcb); 2124172090Srrs#endif 2125171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2126172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2127172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2128172090Srrs#endif 2129172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2130163953Srrs return (NULL); 2131163953Srrs } 2132163953Srrs /* load all addresses */ 2133228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 2134163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2135163953Srrs init_src)) { 2136170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2137172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2138172090Srrs SCTP_TCB_UNLOCK(stcb); 2139172090Srrs SCTP_SOCKET_LOCK(so, 1); 2140172090Srrs SCTP_TCB_LOCK(stcb); 2141172090Srrs#endif 2142171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2143172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2144172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2145172090Srrs#endif 2146172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2147163953Srrs return (NULL); 2148163953Srrs } 2149163953Srrs /* 2150163953Srrs * verify any preceding AUTH chunk that was skipped 2151163953Srrs */ 2152163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2153163953Srrs sctp_auth_get_cookie_params(stcb, m, 2154163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2155163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2156163953Srrs if (auth_skipped) { 2157163953Srrs struct sctp_auth_chunk *auth; 2158163953Srrs 2159163953Srrs auth = (struct sctp_auth_chunk *) 2160166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2161169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2162163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2163169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2164169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2165171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2166172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2167172090Srrs SCTP_TCB_UNLOCK(stcb); 2168172090Srrs SCTP_SOCKET_LOCK(so, 1); 2169172090Srrs SCTP_TCB_LOCK(stcb); 2170172090Srrs#endif 2171171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2172172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2173172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2174172090Srrs#endif 2175172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2176163953Srrs return (NULL); 2177163953Srrs } else { 2178163953Srrs /* remaining chunks checked... good to go */ 2179163953Srrs stcb->asoc.authenticated = 1; 2180163953Srrs } 2181163953Srrs } 2182163953Srrs /* update current state */ 2183169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2184171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2185163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2186163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2187163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2188163953Srrs } 2189165220Srrs sctp_stop_all_cookie_timers(stcb); 2190163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2191163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2192163953Srrs 2193163953Srrs /* 2194163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2195163953Srrs * addresses that need to get added to the peer (eg. addresses 2196163953Srrs * changed while cookie echo in flight). This needs to be done 2197163953Srrs * after we go to the OPEN state to do the correct asconf 2198163953Srrs * processing. else, make sure we have the correct addresses in our 2199163953Srrs * lists 2200163953Srrs */ 2201163953Srrs 2202163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2203163953Srrs /* pull in local_address (our "from" address) */ 2204221249Stuexen switch (cookie->laddr_type) { 2205221249Stuexen#ifdef INET 2206221249Stuexen case SCTP_IPV4_ADDRESS: 2207163953Srrs /* source addr is IPv4 */ 2208163953Srrs sin = (struct sockaddr_in *)initack_src; 2209163953Srrs memset(sin, 0, sizeof(*sin)); 2210163953Srrs sin->sin_family = AF_INET; 2211163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2212163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2213221249Stuexen break; 2214221249Stuexen#endif 2215221249Stuexen#ifdef INET6 2216221249Stuexen case SCTP_IPV6_ADDRESS: 2217163953Srrs /* source addr is IPv6 */ 2218163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2219163953Srrs memset(sin6, 0, sizeof(*sin6)); 2220163953Srrs sin6->sin6_family = AF_INET6; 2221163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2222163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2223163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2224163953Srrs sizeof(sin6->sin6_addr)); 2225221249Stuexen break; 2226221249Stuexen#endif 2227221249Stuexen default: 2228170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2229172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2230172090Srrs SCTP_TCB_UNLOCK(stcb); 2231172090Srrs SCTP_SOCKET_LOCK(so, 1); 2232172090Srrs SCTP_TCB_LOCK(stcb); 2233172090Srrs#endif 2234171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2235172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2236172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2237172090Srrs#endif 2238172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2239163953Srrs return (NULL); 2240163953Srrs } 2241163953Srrs 2242163953Srrs /* set up to notify upper layer */ 2243163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2244163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2245163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2246163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2247163953Srrs /* 2248163953Srrs * This is an endpoint that called connect() how it got a 2249163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2250163953Srrs * the INIT was sent, but before it got there.. a complete 2251163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2252163953Srrs * should have went to the other code.. not here.. oh well.. 2253163953Srrs * a bit of protection is worth having.. 2254163953Srrs */ 2255163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2256172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2257172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2258172090Srrs SCTP_TCB_UNLOCK(stcb); 2259172090Srrs SCTP_SOCKET_LOCK(so, 1); 2260172090Srrs SCTP_TCB_LOCK(stcb); 2261172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2262172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2263172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2264172090Srrs return (NULL); 2265172090Srrs } 2266172090Srrs#endif 2267172090Srrs soisconnected(stcb->sctp_socket); 2268172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2269172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2270172090Srrs#endif 2271163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2272163953Srrs (inp->sctp_socket->so_qlimit)) { 2273163953Srrs /* 2274163953Srrs * We don't want to do anything with this one. Since it is 2275163953Srrs * the listening guy. The timer will get started for 2276163953Srrs * accepted connections in the caller. 2277163953Srrs */ 2278163953Srrs ; 2279163953Srrs } 2280163953Srrs /* since we did not send a HB make sure we don't double things */ 2281169420Srrs if ((netp) && (*netp)) 2282169420Srrs (*netp)->hb_responded = 1; 2283163953Srrs 2284163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2285163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2286163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2287163953Srrs } 2288165220Srrs /* calculate the RTT */ 2289171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2290170642Srrs if ((netp) && (*netp)) { 2291169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2292218186Srrs &cookie->time_entered, sctp_align_unsafe_makecopy, 2293219397Srrs SCTP_RTT_FROM_NON_DATA); 2294170642Srrs } 2295172190Srrs /* respond with a COOKIE-ACK */ 2296163953Srrs sctp_send_cookie_ack(stcb); 2297172190Srrs 2298172190Srrs /* 2299172190Srrs * check the address lists for any ASCONFs that need to be sent 2300172190Srrs * AFTER the cookie-ack is sent 2301172190Srrs */ 2302172190Srrs sctp_check_address_list(stcb, m, 2303172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2304172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2305172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2306172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2307172190Srrs 2308172190Srrs 2309163953Srrs return (stcb); 2310163953Srrs} 2311163953Srrs 2312185694Srrs/* 2313185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2314185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2315185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2316185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2317185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2318185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2319185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2320185694Srrs -- SEND ABORT - TRY AGAIN -- 2321185694Srrs } 2322185694Srrs } 2323185694Srrs*/ 2324163953Srrs 2325163953Srrs/* 2326163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2327163953Srrs * existing (non-NULL) TCB 2328163953Srrs */ 2329163953Srrsstatic struct mbuf * 2330163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2331163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2332163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2333169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2334179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2335163953Srrs{ 2336163953Srrs struct sctp_state_cookie *cookie; 2337163953Srrs struct sctp_tcb *l_stcb = *stcb; 2338163953Srrs struct sctp_inpcb *l_inp; 2339163953Srrs struct sockaddr *to; 2340163953Srrs struct sctp_pcb *ep; 2341163953Srrs struct mbuf *m_sig; 2342163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2343163953Srrs uint8_t *sig; 2344163953Srrs uint8_t cookie_ok = 0; 2345163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2346163953Srrs unsigned int cookie_len; 2347163953Srrs struct timeval now; 2348163953Srrs struct timeval time_expires; 2349163953Srrs struct sockaddr_storage dest_store; 2350163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2351163953Srrs struct ip *iph; 2352163953Srrs int notification = 0; 2353163953Srrs struct sctp_nets *netl; 2354163953Srrs int had_a_existing_tcb = 0; 2355216887Stuexen int send_int_conf = 0; 2356163953Srrs 2357221249Stuexen#ifdef INET 2358221249Stuexen struct sockaddr_in sin; 2359221249Stuexen 2360221249Stuexen#endif 2361221249Stuexen#ifdef INET6 2362221249Stuexen struct sockaddr_in6 sin6; 2363221249Stuexen 2364221249Stuexen#endif 2365221249Stuexen 2366169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2367169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2368163953Srrs 2369163953Srrs if (inp_p == NULL) { 2370163953Srrs return (NULL); 2371163953Srrs } 2372163953Srrs /* First get the destination address setup too. */ 2373163953Srrs iph = mtod(m, struct ip *); 2374178251Srrs switch (iph->ip_v) { 2375221249Stuexen#ifdef INET 2376178251Srrs case IPVERSION: 2377178251Srrs { 2378178251Srrs /* its IPv4 */ 2379178251Srrs struct sockaddr_in *lsin; 2380163953Srrs 2381178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2382178251Srrs memset(lsin, 0, sizeof(*lsin)); 2383178251Srrs lsin->sin_family = AF_INET; 2384178251Srrs lsin->sin_len = sizeof(*lsin); 2385178251Srrs lsin->sin_port = sh->dest_port; 2386178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2387178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2388178251Srrs break; 2389178251Srrs } 2390221249Stuexen#endif 2391178251Srrs#ifdef INET6 2392178251Srrs case IPV6_VERSION >> 4: 2393178251Srrs { 2394178251Srrs /* its IPv6 */ 2395178251Srrs struct ip6_hdr *ip6; 2396178251Srrs struct sockaddr_in6 *lsin6; 2397163953Srrs 2398178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2399178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2400178251Srrs lsin6->sin6_family = AF_INET6; 2401178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2402178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2403178251Srrs lsin6->sin6_port = sh->dest_port; 2404178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2405178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2406178251Srrs break; 2407178251Srrs } 2408178251Srrs#endif 2409178251Srrs default: 2410163953Srrs return (NULL); 2411163953Srrs } 2412163953Srrs 2413163953Srrs cookie = &cp->cookie; 2414163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2415163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2416163953Srrs 2417163953Srrs if ((cookie->peerport != sh->src_port) && 2418163953Srrs (cookie->myport != sh->dest_port) && 2419163953Srrs (cookie->my_vtag != sh->v_tag)) { 2420163953Srrs /* 2421163953Srrs * invalid ports or bad tag. Note that we always leave the 2422163953Srrs * v_tag in the header in network order and when we stored 2423163953Srrs * it in the my_vtag slot we also left it in network order. 2424169352Srrs * This maintains the match even though it may be in the 2425163953Srrs * opposite byte order of the machine :-> 2426163953Srrs */ 2427163953Srrs return (NULL); 2428163953Srrs } 2429163953Srrs if (cookie_len > size_of_pkt || 2430163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2431163953Srrs sizeof(struct sctp_init_chunk) + 2432163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2433163953Srrs /* cookie too long! or too small */ 2434163953Srrs return (NULL); 2435163953Srrs } 2436163953Srrs /* 2437163953Srrs * split off the signature into its own mbuf (since it should not be 2438163953Srrs * calculated in the sctp_hmac_m() call). 2439163953Srrs */ 2440163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2441163953Srrs if (sig_offset > size_of_pkt) { 2442163953Srrs /* packet not correct size! */ 2443163953Srrs /* XXX this may already be accounted for earlier... */ 2444163953Srrs return (NULL); 2445163953Srrs } 2446163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2447163953Srrs if (m_sig == NULL) { 2448163953Srrs /* out of memory or ?? */ 2449163953Srrs return (NULL); 2450163953Srrs } 2451178202Srrs#ifdef SCTP_MBUF_LOGGING 2452179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2453178202Srrs struct mbuf *mat; 2454178202Srrs 2455234461Stuexen for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) { 2456178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2457178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2458178202Srrs } 2459178202Srrs } 2460178202Srrs } 2461178202Srrs#endif 2462178202Srrs 2463163953Srrs /* 2464163953Srrs * compute the signature/digest for the cookie 2465163953Srrs */ 2466163953Srrs ep = &(*inp_p)->sctp_ep; 2467163953Srrs l_inp = *inp_p; 2468163953Srrs if (l_stcb) { 2469163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2470163953Srrs } 2471163953Srrs SCTP_INP_RLOCK(l_inp); 2472163953Srrs if (l_stcb) { 2473163953Srrs SCTP_TCB_LOCK(l_stcb); 2474163953Srrs } 2475163953Srrs /* which cookie is it? */ 2476163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2477163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2478163953Srrs /* it's the old cookie */ 2479169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2480163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2481170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2482163953Srrs } else { 2483163953Srrs /* it's the current cookie */ 2484169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2485163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2486170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2487163953Srrs } 2488163953Srrs /* get the signature */ 2489163953Srrs SCTP_INP_RUNLOCK(l_inp); 2490163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2491163953Srrs if (sig == NULL) { 2492163953Srrs /* couldn't find signature */ 2493164085Srrs sctp_m_freem(m_sig); 2494163953Srrs return (NULL); 2495163953Srrs } 2496163953Srrs /* compare the received digest with the computed digest */ 2497163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2498163953Srrs /* try the old cookie? */ 2499163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2500163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2501163953Srrs /* compute digest with old */ 2502169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2503163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2504170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2505163953Srrs /* compare */ 2506163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2507163953Srrs cookie_ok = 1; 2508163953Srrs } 2509163953Srrs } else { 2510163953Srrs cookie_ok = 1; 2511163953Srrs } 2512163953Srrs 2513163953Srrs /* 2514163953Srrs * Now before we continue we must reconstruct our mbuf so that 2515163953Srrs * normal processing of any other chunks will work. 2516163953Srrs */ 2517163953Srrs { 2518163953Srrs struct mbuf *m_at; 2519163953Srrs 2520163953Srrs m_at = m; 2521165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2522165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2523163953Srrs } 2524165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2525163953Srrs } 2526163953Srrs 2527163953Srrs if (cookie_ok == 0) { 2528169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2529169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2530169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2531169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2532163953Srrs return (NULL); 2533163953Srrs } 2534163953Srrs /* 2535163953Srrs * check the cookie timestamps to be sure it's not stale 2536163953Srrs */ 2537169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2538163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2539169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2540163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2541180387Srrs /* 2542180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2543180387Srrs * is undefined. 2544180387Srrs */ 2545163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2546163953Srrs /* cookie is stale! */ 2547163953Srrs struct mbuf *op_err; 2548163953Srrs struct sctp_stale_cookie_msg *scm; 2549163953Srrs uint32_t tim; 2550163953Srrs 2551163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2552165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2553163953Srrs if (op_err == NULL) { 2554163953Srrs /* FOOBAR */ 2555163953Srrs return (NULL); 2556163953Srrs } 2557163953Srrs /* Set the len */ 2558165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2559163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2560163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2561163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2562163953Srrs (sizeof(uint32_t)))); 2563163953Srrs /* seconds to usec */ 2564163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2565163953Srrs /* add in usec */ 2566163953Srrs if (tim == 0) 2567163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2568163953Srrs scm->time_usec = htonl(tim); 2569169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2570179157Srrs vrf_id, port); 2571163953Srrs return (NULL); 2572163953Srrs } 2573163953Srrs /* 2574163953Srrs * Now we must see with the lookup address if we have an existing 2575163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2576163953Srrs * and a INIT collided with us and somewhere the peer sent the 2577163953Srrs * cookie on another address besides the single address our assoc 2578163953Srrs * had for him. In this case we will have one of the tie-tags set at 2579163953Srrs * least AND the address field in the cookie can be used to look it 2580163953Srrs * up. 2581163953Srrs */ 2582163953Srrs to = NULL; 2583221249Stuexen switch (cookie->addr_type) { 2584221249Stuexen#ifdef INET6 2585221249Stuexen case SCTP_IPV6_ADDRESS: 2586163953Srrs memset(&sin6, 0, sizeof(sin6)); 2587163953Srrs sin6.sin6_family = AF_INET6; 2588163953Srrs sin6.sin6_len = sizeof(sin6); 2589163953Srrs sin6.sin6_port = sh->src_port; 2590163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2591163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2592163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2593163953Srrs to = (struct sockaddr *)&sin6; 2594221249Stuexen break; 2595221249Stuexen#endif 2596221249Stuexen#ifdef INET 2597221249Stuexen case SCTP_IPV4_ADDRESS: 2598163953Srrs memset(&sin, 0, sizeof(sin)); 2599163953Srrs sin.sin_family = AF_INET; 2600163953Srrs sin.sin_len = sizeof(sin); 2601163953Srrs sin.sin_port = sh->src_port; 2602163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2603163953Srrs to = (struct sockaddr *)&sin; 2604221249Stuexen break; 2605221249Stuexen#endif 2606221249Stuexen default: 2607169420Srrs /* This should not happen */ 2608169420Srrs return (NULL); 2609163953Srrs } 2610163953Srrs if ((*stcb == NULL) && to) { 2611163953Srrs /* Yep, lets check */ 2612163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2613163953Srrs if (*stcb == NULL) { 2614163953Srrs /* 2615163953Srrs * We should have only got back the same inp. If we 2616163953Srrs * got back a different ep we have a problem. The 2617163953Srrs * original findep got back l_inp and now 2618163953Srrs */ 2619163953Srrs if (l_inp != *inp_p) { 2620169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2621163953Srrs } 2622165220Srrs } else { 2623165220Srrs if (*locked_tcb == NULL) { 2624165220Srrs /* 2625165220Srrs * In this case we found the assoc only 2626165220Srrs * after we locked the create lock. This 2627165220Srrs * means we are in a colliding case and we 2628165220Srrs * must make sure that we unlock the tcb if 2629165220Srrs * its one of the cases where we throw away 2630165220Srrs * the incoming packets. 2631165220Srrs */ 2632165220Srrs *locked_tcb = *stcb; 2633165220Srrs 2634165220Srrs /* 2635165220Srrs * We must also increment the inp ref count 2636165220Srrs * since the ref_count flags was set when we 2637165220Srrs * did not find the TCB, now we found it 2638165220Srrs * which reduces the refcount.. we must 2639165220Srrs * raise it back out to balance it all :-) 2640165220Srrs */ 2641165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2642165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2643169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2644165220Srrs (*stcb)->sctp_ep, l_inp); 2645165220Srrs } 2646165220Srrs } 2647163953Srrs } 2648163953Srrs } 2649188067Srrs if (to == NULL) { 2650169420Srrs return (NULL); 2651188067Srrs } 2652163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2653163953Srrs if (*stcb == NULL) { 2654163953Srrs /* this is the "normal" case... get a new TCB */ 2655163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2656163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2657179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2658163953Srrs } else { 2659163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2660163953Srrs had_a_existing_tcb = 1; 2661163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2662185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2663228653Stuexen ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port); 2664163953Srrs } 2665163953Srrs 2666163953Srrs if (*stcb == NULL) { 2667163953Srrs /* still no TCB... must be bad cookie-echo */ 2668163953Srrs return (NULL); 2669163953Srrs } 2670218335Stuexen if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2671218335Stuexen (*netp)->flowid = m->m_pkthdr.flowid; 2672218400Stuexen#ifdef INVARIANTS 2673218392Srrs (*netp)->flowidset = 1; 2674218400Stuexen#endif 2675218335Stuexen } 2676163953Srrs /* 2677163953Srrs * Ok, we built an association so confirm the address we sent the 2678163953Srrs * INIT-ACK to. 2679163953Srrs */ 2680163953Srrs netl = sctp_findnet(*stcb, to); 2681163953Srrs /* 2682163953Srrs * This code should in theory NOT run but 2683163953Srrs */ 2684163953Srrs if (netl == NULL) { 2685163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2686228907Stuexen if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2687228907Stuexen return (NULL); 2688228907Stuexen } 2689163953Srrs netl = sctp_findnet(*stcb, to); 2690163953Srrs } 2691163953Srrs if (netl) { 2692163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2693163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2694169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2695163953Srrs netl); 2696216887Stuexen send_int_conf = 1; 2697163953Srrs } 2698163953Srrs } 2699224641Stuexen sctp_start_net_timers(*stcb); 2700163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2701163953Srrs if (!had_a_existing_tcb || 2702163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2703163953Srrs /* 2704163953Srrs * If we have a NEW cookie or the connect never 2705163953Srrs * reached the connected state during collision we 2706163953Srrs * must do the TCP accept thing. 2707163953Srrs */ 2708163953Srrs struct socket *so, *oso; 2709163953Srrs struct sctp_inpcb *inp; 2710163953Srrs 2711163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2712163953Srrs /* 2713163953Srrs * For a restart we will keep the same 2714163953Srrs * socket, no need to do anything. I THINK!! 2715163953Srrs */ 2716228653Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2717216887Stuexen if (send_int_conf) { 2718216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2719216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2720216887Stuexen } 2721163953Srrs return (m); 2722163953Srrs } 2723163953Srrs oso = (*inp_p)->sctp_socket; 2724171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2725163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2726218757Sbz CURVNET_SET(oso->so_vnet); 2727166086Srrs so = sonewconn(oso, 0 2728163953Srrs ); 2729218757Sbz CURVNET_RESTORE(); 2730163953Srrs SCTP_TCB_LOCK((*stcb)); 2731171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2732171858Srrs 2733163953Srrs if (so == NULL) { 2734163953Srrs struct mbuf *op_err; 2735163953Srrs 2736172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2737172090Srrs struct socket *pcb_so; 2738172090Srrs 2739172090Srrs#endif 2740163953Srrs /* Too many sockets */ 2741169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2742163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2743163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2744179157Srrs sh, op_err, vrf_id, port); 2745172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2746172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2747172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2748172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2749172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2750172090Srrs SCTP_TCB_LOCK((*stcb)); 2751172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2752172090Srrs#endif 2753171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2754172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2755172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2756172090Srrs#endif 2757163953Srrs return (NULL); 2758163953Srrs } 2759163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2760166086Srrs SCTP_INP_INCR_REF(inp); 2761166086Srrs /* 2762166086Srrs * We add the unbound flag here so that if we get an 2763166086Srrs * soabort() before we get the move_pcb done, we 2764166086Srrs * will properly cleanup. 2765166086Srrs */ 2766163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2767163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2768163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2769166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2770163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2771163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2772163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2773172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2774163953Srrs inp->sctp_socket = so; 2775163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2776211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2777218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2778163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2779163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2780233660Srrs inp->local_strreset_support = (*inp_p)->local_strreset_support; 2781163953Srrs inp->inp_starting_point_for_iterator = NULL; 2782163953Srrs /* 2783163953Srrs * copy in the authentication parameters from the 2784163953Srrs * original endpoint 2785163953Srrs */ 2786163953Srrs if (inp->sctp_ep.local_hmacs) 2787163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2788163953Srrs inp->sctp_ep.local_hmacs = 2789163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2790163953Srrs if (inp->sctp_ep.local_auth_chunks) 2791163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2792163953Srrs inp->sctp_ep.local_auth_chunks = 2793163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2794163953Srrs 2795163953Srrs /* 2796163953Srrs * Now we must move it from one hash table to 2797163953Srrs * another and get the tcb in the right place. 2798163953Srrs */ 2799207924Srrs 2800207924Srrs /* 2801207924Srrs * This is where the one-2-one socket is put into 2802207924Srrs * the accept state waiting for the accept! 2803207924Srrs */ 2804207924Srrs if (*stcb) { 2805207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2806207924Srrs } 2807163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2808170056Srrs 2809170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2810170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2811170056Srrs 2812175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2813175845Srwatson 0); 2814170056Srrs SCTP_TCB_LOCK((*stcb)); 2815170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2816163953Srrs 2817170056Srrs 2818166086Srrs /* 2819166086Srrs * now we must check to see if we were aborted while 2820166086Srrs * the move was going on and the lock/unlock 2821166086Srrs * happened. 2822166086Srrs */ 2823166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2824166086Srrs /* 2825166086Srrs * yep it was, we leave the assoc attached 2826166086Srrs * to the socket since the sctp_inpcb_free() 2827166086Srrs * call will send an abort for us. 2828166086Srrs */ 2829166086Srrs SCTP_INP_DECR_REF(inp); 2830166086Srrs return (NULL); 2831166086Srrs } 2832166086Srrs SCTP_INP_DECR_REF(inp); 2833163953Srrs /* Switch over to the new guy */ 2834163953Srrs *inp_p = inp; 2835172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2836216887Stuexen if (send_int_conf) { 2837216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2838216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2839216887Stuexen } 2840166086Srrs /* 2841166086Srrs * Pull it from the incomplete queue and wake the 2842166086Srrs * guy 2843166086Srrs */ 2844172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2845172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2846172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2847172090Srrs SCTP_SOCKET_LOCK(so, 1); 2848172090Srrs#endif 2849166086Srrs soisconnected(so); 2850172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2851172118Srrs SCTP_TCB_LOCK((*stcb)); 2852172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2853172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2854172090Srrs#endif 2855163953Srrs return (m); 2856163953Srrs } 2857163953Srrs } 2858216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2859216887Stuexen if (notification) { 2860216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2861216887Stuexen } 2862216887Stuexen if (send_int_conf) { 2863216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2864216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2865216887Stuexen } 2866163953Srrs } 2867163953Srrs return (m); 2868163953Srrs} 2869163953Srrs 2870163953Srrsstatic void 2871228653Stuexensctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2872163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2873163953Srrs{ 2874163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2875163953Srrs struct sctp_association *asoc; 2876163953Srrs 2877169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2878169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2879163953Srrs if (stcb == NULL) 2880163953Srrs return; 2881163953Srrs 2882163953Srrs asoc = &stcb->asoc; 2883163953Srrs 2884163953Srrs sctp_stop_all_cookie_timers(stcb); 2885163953Srrs /* process according to association state */ 2886163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2887163953Srrs /* state change only needed when I am in right state */ 2888169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2889171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2890224641Stuexen sctp_start_net_timers(stcb); 2891163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2892163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2893163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2894163953Srrs 2895163953Srrs } 2896163953Srrs /* update RTO */ 2897163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2898163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2899163953Srrs if (asoc->overall_error_count == 0) { 2900163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2901218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2902219397Srrs SCTP_RTT_FROM_NON_DATA); 2903163953Srrs } 2904169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2905172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2906163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2907163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2908172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2909172090Srrs struct socket *so; 2910172090Srrs 2911172090Srrs#endif 2912163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2913172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2914172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2915172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2916172090Srrs SCTP_TCB_UNLOCK(stcb); 2917172090Srrs SCTP_SOCKET_LOCK(so, 1); 2918172090Srrs SCTP_TCB_LOCK(stcb); 2919172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2920218641Srrs#endif 2921218641Srrs if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2922218641Srrs soisconnected(stcb->sctp_socket); 2923172090Srrs } 2924172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2925172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2926172090Srrs#endif 2927163953Srrs } 2928163953Srrs /* 2929163953Srrs * since we did not send a HB make sure we don't double 2930163953Srrs * things 2931163953Srrs */ 2932163953Srrs net->hb_responded = 1; 2933163953Srrs 2934218641Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2935218641Srrs /* 2936218641Srrs * We don't need to do the asconf thing, nor hb or 2937218641Srrs * autoclose if the socket is closed. 2938218641Srrs */ 2939218641Srrs goto closed_socket; 2940218641Srrs } 2941218641Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2942218641Srrs stcb, net); 2943218641Srrs 2944218641Srrs 2945163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2946163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2947163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2948163953Srrs stcb->sctp_ep, stcb, NULL); 2949163953Srrs } 2950163953Srrs /* 2951171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2952171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2953171572Srrs * in flight) 2954163953Srrs */ 2955163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2956163953Srrs (stcb->asoc.peer_supports_asconf) && 2957163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2958171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2959163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2960163953Srrs stcb->sctp_ep, stcb, 2961163953Srrs stcb->asoc.primary_destination); 2962171572Srrs#else 2963172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2964172190Srrs SCTP_ADDR_NOT_LOCKED); 2965171572Srrs#endif 2966163953Srrs } 2967163953Srrs } 2968218641Srrsclosed_socket: 2969163953Srrs /* Toss the cookie if I can */ 2970163953Srrs sctp_toss_old_cookies(stcb, asoc); 2971163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2972163953Srrs /* Restart the timer if we have pending data */ 2973163953Srrs struct sctp_tmit_chunk *chk; 2974163953Srrs 2975163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2976216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2977163953Srrs } 2978163953Srrs} 2979163953Srrs 2980163953Srrsstatic void 2981163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2982163953Srrs struct sctp_tcb *stcb) 2983163953Srrs{ 2984163953Srrs struct sctp_nets *net; 2985163953Srrs struct sctp_tmit_chunk *lchk; 2986218072Srrs struct sctp_ecne_chunk bkup; 2987228907Stuexen uint8_t override_bit; 2988218072Srrs uint32_t tsn, window_data_tsn; 2989218072Srrs int len; 2990218232Srrs unsigned int pkt_cnt; 2991163953Srrs 2992218072Srrs len = ntohs(cp->ch.chunk_length); 2993218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 2994218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 2995163953Srrs return; 2996163953Srrs } 2997218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 2998218072Srrs /* Its the old format */ 2999218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3000218072Srrs bkup.num_pkts_since_cwr = htonl(1); 3001218072Srrs cp = &bkup; 3002218072Srrs } 3003163953Srrs SCTP_STAT_INCR(sctps_recvecne); 3004163953Srrs tsn = ntohl(cp->tsn); 3005218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3006218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3007163953Srrs if (lchk == NULL) { 3008218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 3009163953Srrs } else { 3010218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 3011163953Srrs } 3012163953Srrs 3013218072Srrs /* Find where it was sent to if possible. */ 3014163953Srrs net = NULL; 3015216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3016163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 3017163953Srrs net = lchk->whoTo; 3018218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3019163953Srrs break; 3020163953Srrs } 3021216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3022163953Srrs break; 3023216825Stuexen } 3024163953Srrs } 3025218072Srrs if (net == NULL) { 3026163953Srrs /* 3027218072Srrs * What to do. A previous send of a CWR was possibly lost. 3028218072Srrs * See how old it is, we may have it marked on the actual 3029218072Srrs * net. 3030218072Srrs */ 3031218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3032218072Srrs if (tsn == net->last_cwr_tsn) { 3033218072Srrs /* Found him, send it off */ 3034228907Stuexen break; 3035218072Srrs } 3036218072Srrs } 3037228907Stuexen if (net == NULL) { 3038228907Stuexen /* 3039228907Stuexen * If we reach here, we need to send a special CWR 3040228907Stuexen * that says hey, we did this a long time ago and 3041228907Stuexen * you lost the response. 3042228907Stuexen */ 3043228907Stuexen net = TAILQ_FIRST(&stcb->asoc.nets); 3044228907Stuexen if (net == NULL) { 3045228907Stuexen /* TSNH */ 3046228907Stuexen return; 3047228907Stuexen } 3048228907Stuexen override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3049228907Stuexen } else { 3050228907Stuexen override_bit = 0; 3051228907Stuexen } 3052228907Stuexen } else { 3053228907Stuexen override_bit = 0; 3054218072Srrs } 3055218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3056218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3057218072Srrs /* 3058171440Srrs * JRS - Use the congestion control given in the pluggable 3059171440Srrs * CC module 3060171440Srrs */ 3061218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3062171440Srrs /* 3063218072Srrs * We reduce once every RTT. So we will only lower cwnd at 3064218072Srrs * the next sending seq i.e. the window_data_tsn 3065163953Srrs */ 3066218072Srrs net->cwr_window_tsn = window_data_tsn; 3067218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3068218072Srrs net->lost_cnt = pkt_cnt; 3069218072Srrs net->last_cwr_tsn = tsn; 3070218072Srrs } else { 3071218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3072218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3073218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3074218072Srrs /* 3075218129Srrs * Another loss in the same window update how many 3076218129Srrs * marks/packets lost we have had. 3077218072Srrs */ 3078218129Srrs int cnt = 1; 3079218072Srrs 3080218072Srrs if (pkt_cnt > net->lost_cnt) { 3081218072Srrs /* Should be the case */ 3082218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3083218129Srrs net->ecn_ce_pkt_cnt += cnt; 3084218072Srrs } 3085218129Srrs net->lost_cnt = pkt_cnt; 3086218072Srrs net->last_cwr_tsn = tsn; 3087218129Srrs /* 3088218129Srrs * Most CC functions will ignore this call, since we 3089218129Srrs * are in-window yet of the initial CE the peer saw. 3090218129Srrs */ 3091218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3092218072Srrs } 3093163953Srrs } 3094163953Srrs /* 3095163953Srrs * We always send a CWR this way if our previous one was lost our 3096163953Srrs * peer will get an update, or if it is not time again to reduce we 3097218072Srrs * still get the cwr to the peer. Note we set the override when we 3098218072Srrs * could not find the TSN on the chunk or the destination network. 3099163953Srrs */ 3100218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3101163953Srrs} 3102163953Srrs 3103163953Srrsstatic void 3104218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3105163953Srrs{ 3106163953Srrs /* 3107163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3108163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3109163953Srrs * If so remove it. 3110163953Srrs */ 3111163953Srrs struct sctp_tmit_chunk *chk; 3112163953Srrs struct sctp_ecne_chunk *ecne; 3113218072Srrs int override; 3114218072Srrs uint32_t cwr_tsn; 3115163953Srrs 3116218072Srrs cwr_tsn = ntohl(cp->tsn); 3117218072Srrs 3118218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3119163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3120163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3121163953Srrs continue; 3122163953Srrs } 3123218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3124218072Srrs /* Must be from the right src unless override is set */ 3125218072Srrs continue; 3126218072Srrs } 3127163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3128218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3129163953Srrs /* this covers this ECNE, we can remove it */ 3130163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3131163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3132163953Srrs sctp_next); 3133163953Srrs if (chk->data) { 3134163953Srrs sctp_m_freem(chk->data); 3135163953Srrs chk->data = NULL; 3136163953Srrs } 3137163953Srrs stcb->asoc.ctrl_queue_cnt--; 3138221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3139218072Srrs if (override == 0) { 3140218072Srrs break; 3141218072Srrs } 3142163953Srrs } 3143163953Srrs } 3144163953Srrs} 3145163953Srrs 3146163953Srrsstatic void 3147228653Stuexensctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3148163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3149163953Srrs{ 3150163953Srrs struct sctp_association *asoc; 3151163953Srrs 3152172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3153172090Srrs struct socket *so; 3154172090Srrs 3155172090Srrs#endif 3156172090Srrs 3157169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3158169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3159163953Srrs if (stcb == NULL) 3160163953Srrs return; 3161163953Srrs 3162163953Srrs asoc = &stcb->asoc; 3163163953Srrs /* process according to association state */ 3164163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3165163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3166171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3167171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3168163953Srrs SCTP_TCB_UNLOCK(stcb); 3169163953Srrs return; 3170163953Srrs } 3171163953Srrs /* notify upper layer protocol */ 3172163953Srrs if (stcb->sctp_socket) { 3173172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3174163953Srrs /* are the queues empty? they should be */ 3175163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3176163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3177217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3178235416Stuexen sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED); 3179163953Srrs } 3180163953Srrs } 3181163953Srrs /* stop the timer */ 3182178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3183163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3184163953Srrs /* free the TCB */ 3185171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3186171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3187172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3188172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3189172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3190172090Srrs SCTP_TCB_UNLOCK(stcb); 3191172090Srrs SCTP_SOCKET_LOCK(so, 1); 3192172090Srrs SCTP_TCB_LOCK(stcb); 3193172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3194172090Srrs#endif 3195171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3196172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3197172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3198172090Srrs#endif 3199163953Srrs return; 3200163953Srrs} 3201163953Srrs 3202163953Srrsstatic int 3203163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3204163953Srrs struct sctp_nets *net, uint8_t flg) 3205163953Srrs{ 3206163953Srrs switch (desc->chunk_type) { 3207211969Stuexen case SCTP_DATA: 3208163953Srrs /* find the tsn to resend (possibly */ 3209163953Srrs { 3210163953Srrs uint32_t tsn; 3211163953Srrs struct sctp_tmit_chunk *tp1; 3212163953Srrs 3213163953Srrs tsn = ntohl(desc->tsn_ifany); 3214216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3215163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3216163953Srrs /* found it */ 3217163953Srrs break; 3218163953Srrs } 3219216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3220163953Srrs /* not found */ 3221163953Srrs tp1 = NULL; 3222163953Srrs break; 3223163953Srrs } 3224163953Srrs } 3225163953Srrs if (tp1 == NULL) { 3226163953Srrs /* 3227163953Srrs * Do it the other way , aka without paying 3228163953Srrs * attention to queue seq order. 3229163953Srrs */ 3230163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3231216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3232163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3233163953Srrs /* found it */ 3234163953Srrs break; 3235163953Srrs } 3236163953Srrs } 3237163953Srrs } 3238163953Srrs if (tp1 == NULL) { 3239163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3240163953Srrs } 3241163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3242163953Srrs uint8_t *ddp; 3243163953Srrs 3244214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3245214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3246214877Stuexen return (0); 3247214877Stuexen } 3248163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3249163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3250163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3251163953Srrs return (0); 3252163953Srrs } 3253163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3254163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3255163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3256163953Srrs return (0); 3257163953Srrs } 3258163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3259163953Srrs sizeof(struct sctp_data_chunk)); 3260163953Srrs { 3261163953Srrs unsigned int iii; 3262163953Srrs 3263163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3264163953Srrs iii++) { 3265163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3266163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3267163953Srrs return (-1); 3268163953Srrs } 3269163953Srrs } 3270163953Srrs } 3271163953Srrs 3272163953Srrs if (tp1->do_rtt) { 3273163953Srrs /* 3274163953Srrs * this guy had a RTO calculation 3275163953Srrs * pending on it, cancel it 3276163953Srrs */ 3277219397Srrs if (tp1->whoTo->rto_needed == 0) { 3278219397Srrs tp1->whoTo->rto_needed = 1; 3279219397Srrs } 3280163953Srrs tp1->do_rtt = 0; 3281163953Srrs } 3282163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3283163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3284163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3285163953Srrs /* 3286163953Srrs * mark it as if we were doing a FR, since 3287163953Srrs * we will be getting gap ack reports behind 3288163953Srrs * the info from the router. 3289163953Srrs */ 3290163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3291163953Srrs /* 3292163953Srrs * mark the tsn with what sequences can 3293163953Srrs * cause a new FR. 3294163953Srrs */ 3295163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3296163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3297163953Srrs } else { 3298163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3299163953Srrs } 3300163953Srrs 3301163953Srrs /* restart the timer */ 3302163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3303171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3304163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3305163953Srrs stcb, tp1->whoTo); 3306163953Srrs 3307163953Srrs /* fix counts and things */ 3308179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3309170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3310170744Srrs tp1->whoTo->flight_size, 3311170744Srrs tp1->book_size, 3312170744Srrs (uintptr_t) stcb, 3313170744Srrs tp1->rec.data.TSN_seq); 3314170744Srrs } 3315190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3316190689Srrs sctp_flight_size_decrease(tp1); 3317190689Srrs sctp_total_flight_decrease(stcb, tp1); 3318190689Srrs } 3319216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3320163953Srrs } { 3321163953Srrs /* audit code */ 3322163953Srrs unsigned int audit; 3323163953Srrs 3324163953Srrs audit = 0; 3325163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3326163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3327163953Srrs audit++; 3328163953Srrs } 3329163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3330163953Srrs sctp_next) { 3331163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3332163953Srrs audit++; 3333163953Srrs } 3334163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3335169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3336163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3337163953Srrs#ifndef SCTP_AUDITING_ENABLED 3338163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3339163953Srrs#endif 3340163953Srrs } 3341163953Srrs } 3342163953Srrs } 3343163953Srrs break; 3344163953Srrs case SCTP_ASCONF: 3345163953Srrs { 3346163953Srrs struct sctp_tmit_chunk *asconf; 3347163953Srrs 3348163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3349163953Srrs sctp_next) { 3350163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3351163953Srrs break; 3352163953Srrs } 3353163953Srrs } 3354163953Srrs if (asconf) { 3355163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3356163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3357163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3358163953Srrs asconf->snd_count--; 3359163953Srrs } 3360163953Srrs } 3361163953Srrs break; 3362163953Srrs case SCTP_INITIATION: 3363163953Srrs /* resend the INIT */ 3364163953Srrs stcb->asoc.dropped_special_cnt++; 3365163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3366163953Srrs /* 3367163953Srrs * If we can get it in, in a few attempts we do 3368163953Srrs * this, otherwise we let the timer fire. 3369163953Srrs */ 3370163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3371171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3372172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3373163953Srrs } 3374163953Srrs break; 3375163953Srrs case SCTP_SELECTIVE_ACK: 3376206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3377163953Srrs /* resend the sack */ 3378221627Stuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3379163953Srrs break; 3380163953Srrs case SCTP_HEARTBEAT_REQUEST: 3381163953Srrs /* resend a demand HB */ 3382171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3383171440Srrs /* 3384171440Srrs * Only retransmit if we KNOW we wont destroy the 3385171440Srrs * tcb 3386171440Srrs */ 3387224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3388171440Srrs } 3389163953Srrs break; 3390163953Srrs case SCTP_SHUTDOWN: 3391163953Srrs sctp_send_shutdown(stcb, net); 3392163953Srrs break; 3393163953Srrs case SCTP_SHUTDOWN_ACK: 3394163953Srrs sctp_send_shutdown_ack(stcb, net); 3395163953Srrs break; 3396163953Srrs case SCTP_COOKIE_ECHO: 3397163953Srrs { 3398163953Srrs struct sctp_tmit_chunk *cookie; 3399163953Srrs 3400163953Srrs cookie = NULL; 3401163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3402163953Srrs sctp_next) { 3403163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3404163953Srrs break; 3405163953Srrs } 3406163953Srrs } 3407163953Srrs if (cookie) { 3408163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3409163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3410163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3411163953Srrs sctp_stop_all_cookie_timers(stcb); 3412163953Srrs } 3413163953Srrs } 3414163953Srrs break; 3415163953Srrs case SCTP_COOKIE_ACK: 3416163953Srrs sctp_send_cookie_ack(stcb); 3417163953Srrs break; 3418163953Srrs case SCTP_ASCONF_ACK: 3419163953Srrs /* resend last asconf ack */ 3420171990Srrs sctp_send_asconf_ack(stcb); 3421163953Srrs break; 3422163953Srrs case SCTP_FORWARD_CUM_TSN: 3423163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3424163953Srrs break; 3425163953Srrs /* can't do anything with these */ 3426163953Srrs case SCTP_PACKET_DROPPED: 3427163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3428163953Srrs case SCTP_HEARTBEAT_ACK: 3429163953Srrs case SCTP_ABORT_ASSOCIATION: 3430163953Srrs case SCTP_OPERATION_ERROR: 3431163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3432163953Srrs case SCTP_ECN_ECHO: 3433163953Srrs case SCTP_ECN_CWR: 3434163953Srrs default: 3435163953Srrs break; 3436163953Srrs } 3437163953Srrs return (0); 3438163953Srrs} 3439163953Srrs 3440163953Srrsvoid 3441163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3442163953Srrs{ 3443163953Srrs int i; 3444163953Srrs uint16_t temp; 3445163953Srrs 3446163953Srrs /* 3447163953Srrs * We set things to 0xffff since this is the last delivered sequence 3448163953Srrs * and we will be sending in 0 after the reset. 3449163953Srrs */ 3450163953Srrs 3451163953Srrs if (number_entries) { 3452163953Srrs for (i = 0; i < number_entries; i++) { 3453163953Srrs temp = ntohs(list[i]); 3454163953Srrs if (temp >= stcb->asoc.streamincnt) { 3455163953Srrs continue; 3456163953Srrs } 3457163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3458163953Srrs } 3459163953Srrs } else { 3460163953Srrs list = NULL; 3461163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3462163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3463163953Srrs } 3464163953Srrs } 3465172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3466163953Srrs} 3467163953Srrs 3468163953Srrsstatic void 3469163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3470163953Srrs{ 3471163953Srrs int i; 3472163953Srrs 3473163953Srrs if (number_entries == 0) { 3474163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3475163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3476163953Srrs } 3477163953Srrs } else if (number_entries) { 3478163953Srrs for (i = 0; i < number_entries; i++) { 3479163953Srrs uint16_t temp; 3480163953Srrs 3481163953Srrs temp = ntohs(list[i]); 3482163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3483163953Srrs /* no such stream */ 3484163953Srrs continue; 3485163953Srrs } 3486163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3487163953Srrs } 3488163953Srrs } 3489172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3490163953Srrs} 3491163953Srrs 3492163953Srrs 3493163953Srrsstruct sctp_stream_reset_out_request * 3494163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3495163953Srrs{ 3496163953Srrs struct sctp_association *asoc; 3497163953Srrs struct sctp_stream_reset_out_req *req; 3498163953Srrs struct sctp_stream_reset_out_request *r; 3499163953Srrs struct sctp_tmit_chunk *chk; 3500163953Srrs int len, clen; 3501163953Srrs 3502163953Srrs asoc = &stcb->asoc; 3503163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3504169208Srrs asoc->stream_reset_outstanding = 0; 3505163953Srrs return (NULL); 3506163953Srrs } 3507163953Srrs if (stcb->asoc.str_reset == NULL) { 3508169208Srrs asoc->stream_reset_outstanding = 0; 3509163953Srrs return (NULL); 3510163953Srrs } 3511163953Srrs chk = stcb->asoc.str_reset; 3512163953Srrs if (chk->data == NULL) { 3513163953Srrs return (NULL); 3514163953Srrs } 3515163953Srrs if (bchk) { 3516163953Srrs /* he wants a copy of the chk pointer */ 3517163953Srrs *bchk = chk; 3518163953Srrs } 3519163953Srrs clen = chk->send_size; 3520163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3521163953Srrs r = &req->sr_req; 3522163953Srrs if (ntohl(r->request_seq) == seq) { 3523163953Srrs /* found it */ 3524163953Srrs return (r); 3525163953Srrs } 3526163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3527163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3528163953Srrs /* move to the next one, there can only be a max of two */ 3529163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3530163953Srrs if (ntohl(r->request_seq) == seq) { 3531163953Srrs return (r); 3532163953Srrs } 3533163953Srrs } 3534163953Srrs /* that seq is not here */ 3535163953Srrs return (NULL); 3536163953Srrs} 3537163953Srrs 3538163953Srrsstatic void 3539163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3540163953Srrs{ 3541163953Srrs struct sctp_association *asoc; 3542163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3543163953Srrs 3544163953Srrs if (stcb->asoc.str_reset == NULL) { 3545163953Srrs return; 3546163953Srrs } 3547166675Srrs asoc = &stcb->asoc; 3548166675Srrs 3549171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3550163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3551163953Srrs chk, 3552163953Srrs sctp_next); 3553163953Srrs if (chk->data) { 3554163953Srrs sctp_m_freem(chk->data); 3555163953Srrs chk->data = NULL; 3556163953Srrs } 3557163953Srrs asoc->ctrl_queue_cnt--; 3558221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3559172156Srrs /* sa_ignore NO_NULL_CHK */ 3560163953Srrs stcb->asoc.str_reset = NULL; 3561163953Srrs} 3562163953Srrs 3563163953Srrs 3564163953Srrsstatic int 3565163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3566163953Srrs uint32_t seq, uint32_t action, 3567164139Srrs struct sctp_stream_reset_response *respin) 3568163953Srrs{ 3569163953Srrs uint16_t type; 3570163953Srrs int lparm_len; 3571163953Srrs struct sctp_association *asoc = &stcb->asoc; 3572163953Srrs struct sctp_tmit_chunk *chk; 3573163953Srrs struct sctp_stream_reset_out_request *srparam; 3574163953Srrs int number_entries; 3575163953Srrs 3576163953Srrs if (asoc->stream_reset_outstanding == 0) { 3577163953Srrs /* duplicate */ 3578163953Srrs return (0); 3579163953Srrs } 3580163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3581163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3582163953Srrs if (srparam) { 3583163953Srrs stcb->asoc.str_reset_seq_out++; 3584163953Srrs type = ntohs(srparam->ph.param_type); 3585163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3586163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3587164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3588163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3589163953Srrs if (asoc->stream_reset_outstanding) 3590163953Srrs asoc->stream_reset_outstanding--; 3591235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3592163953Srrs /* do it */ 3593163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3594235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3595235066Stuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3596163953Srrs } else { 3597172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3598163953Srrs } 3599163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3600163953Srrs /* Answered my request */ 3601164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3602163953Srrs if (asoc->stream_reset_outstanding) 3603163953Srrs asoc->stream_reset_outstanding--; 3604235066Stuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3605235066Stuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, 3606235066Stuexen number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3607235066Stuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3608233660Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, 3609233660Srrs number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3610163953Srrs } 3611233660Srrs } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { 3612188854Srrs /* Ok we now may have more streams */ 3613233660Srrs int num_stream; 3614233660Srrs 3615233660Srrs num_stream = stcb->asoc.strm_pending_add_size; 3616233660Srrs if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { 3617233660Srrs /* TSNH */ 3618233660Srrs num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; 3619233660Srrs } 3620233660Srrs stcb->asoc.strm_pending_add_size = 0; 3621189121Srrs if (asoc->stream_reset_outstanding) 3622189121Srrs asoc->stream_reset_outstanding--; 3623235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3624188854Srrs /* Put the new streams into effect */ 3625233660Srrs stcb->asoc.streamoutcnt += num_stream; 3626233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3627235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3628235066Stuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3629235066Stuexen SCTP_STREAM_CHANGE_DENIED); 3630188854Srrs } else { 3631233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3632235066Stuexen SCTP_STREAM_CHANGE_FAILED); 3633188854Srrs } 3634233660Srrs } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { 3635233660Srrs if (asoc->stream_reset_outstanding) 3636233660Srrs asoc->stream_reset_outstanding--; 3637235066Stuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3638233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3639235066Stuexen SCTP_STREAM_CHANGE_DENIED); 3640235066Stuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3641235066Stuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3642235066Stuexen SCTP_STREAM_CHANGE_FAILED); 3643233660Srrs } 3644163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3645163953Srrs /** 3646163953Srrs * a) Adopt the new in tsn. 3647163953Srrs * b) reset the map 3648163953Srrs * c) Adopt the new out-tsn 3649163953Srrs */ 3650163953Srrs struct sctp_stream_reset_response_tsn *resp; 3651163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3652163953Srrs int abort_flag = 0; 3653163953Srrs 3654163953Srrs if (respin == NULL) { 3655163953Srrs /* huh ? */ 3656163953Srrs return (0); 3657163953Srrs } 3658235064Stuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3659163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3660163953Srrs asoc->stream_reset_outstanding--; 3661163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3662163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3663163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3664170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3665163953Srrs if (abort_flag) { 3666163953Srrs return (1); 3667163953Srrs } 3668163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3669179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3670179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3671179783Srrs } 3672180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3673163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3674163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3675185694Srrs 3676205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3677206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3678205627Srrs 3679163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3680163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3681163953Srrs 3682163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3683163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3684233660Srrs sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); 3685235066Stuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3686235066Stuexen sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3687235066Stuexen SCTP_ASSOC_RESET_DENIED); 3688233660Srrs } else { 3689233660Srrs sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3690235066Stuexen SCTP_ASSOC_RESET_FAILED); 3691163953Srrs } 3692163953Srrs } 3693163953Srrs /* get rid of the request and get the request flags */ 3694163953Srrs if (asoc->stream_reset_outstanding == 0) { 3695163953Srrs sctp_clean_up_stream_reset(stcb); 3696163953Srrs } 3697163953Srrs } 3698163953Srrs } 3699163953Srrs return (0); 3700163953Srrs} 3701163953Srrs 3702163953Srrsstatic void 3703163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3704163953Srrs struct sctp_tmit_chunk *chk, 3705170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3706163953Srrs{ 3707163953Srrs uint32_t seq; 3708163953Srrs int len, i; 3709163953Srrs int number_entries; 3710163953Srrs uint16_t temp; 3711163953Srrs 3712163953Srrs /* 3713163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3714163953Srrs * seq_in is right. 3715163953Srrs */ 3716163953Srrs struct sctp_association *asoc = &stcb->asoc; 3717163953Srrs 3718163953Srrs seq = ntohl(req->request_seq); 3719163953Srrs if (asoc->str_reset_seq_in == seq) { 3720235064Stuexen asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3721235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3722235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3723235064Stuexen } else if (trunc) { 3724170992Srrs /* Can't do it, since they exceeded our buffer size */ 3725235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3726170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3727163953Srrs len = ntohs(req->ph.param_length); 3728163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3729163953Srrs for (i = 0; i < number_entries; i++) { 3730163953Srrs temp = ntohs(req->list_of_streams[i]); 3731163953Srrs req->list_of_streams[i] = temp; 3732163953Srrs } 3733235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3734163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3735163953Srrs asoc->str_reset_seq_out, 3736163953Srrs seq, (asoc->sending_seq - 1)); 3737163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3738163953Srrs asoc->str_reset = chk; 3739163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3740163953Srrs stcb->asoc.stream_reset_outstanding++; 3741163953Srrs } else { 3742163953Srrs /* Can't do it, since we have sent one out */ 3743235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3744163953Srrs } 3745233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3746163953Srrs asoc->str_reset_seq_in++; 3747163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3748163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3749163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3750163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3751163953Srrs } else { 3752235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3753163953Srrs } 3754163953Srrs} 3755163953Srrs 3756163953Srrsstatic int 3757163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3758163953Srrs struct sctp_tmit_chunk *chk, 3759163953Srrs struct sctp_stream_reset_tsn_request *req) 3760163953Srrs{ 3761163953Srrs /* reset all in and out and update the tsn */ 3762163953Srrs /* 3763163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3764163953Srrs * and set cum-ack to it. Also process this selected number as a 3765163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3766163953Srrs */ 3767163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3768163953Srrs struct sctp_association *asoc = &stcb->asoc; 3769163953Srrs int abort_flag = 0; 3770163953Srrs uint32_t seq; 3771163953Srrs 3772163953Srrs seq = ntohl(req->request_seq); 3773163953Srrs if (asoc->str_reset_seq_in == seq) { 3774235283Stuexen asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3775235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3776235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3777235283Stuexen } else { 3778235064Stuexen fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3779235283Stuexen fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3780235283Stuexen fwdtsn.ch.chunk_flags = 0; 3781235283Stuexen fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3782235283Stuexen sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3783235283Stuexen if (abort_flag) { 3784235283Stuexen return (1); 3785235283Stuexen } 3786235283Stuexen asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3787235283Stuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3788235283Stuexen sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3789235283Stuexen } 3790235283Stuexen asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; 3791235283Stuexen asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; 3792235283Stuexen memset(asoc->mapping_array, 0, asoc->mapping_array_size); 3793235283Stuexen asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 3794235283Stuexen memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 3795235283Stuexen atomic_add_int(&asoc->sending_seq, 1); 3796235283Stuexen /* save off historical data for retrans */ 3797235283Stuexen asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; 3798235283Stuexen asoc->last_sending_seq[0] = asoc->sending_seq; 3799235283Stuexen asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; 3800235283Stuexen asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; 3801235283Stuexen sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3802235283Stuexen sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3803235283Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3804235283Stuexen sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); 3805163953Srrs } 3806235283Stuexen sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3807235283Stuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3808163953Srrs asoc->str_reset_seq_in++; 3809163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3810163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3811235064Stuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3812163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3813163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3814235064Stuexen asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); 3815163953Srrs } else { 3816235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3817163953Srrs } 3818163953Srrs return (0); 3819163953Srrs} 3820163953Srrs 3821163953Srrsstatic void 3822163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3823163953Srrs struct sctp_tmit_chunk *chk, 3824170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3825163953Srrs{ 3826163953Srrs uint32_t seq, tsn; 3827163953Srrs int number_entries, len; 3828163953Srrs struct sctp_association *asoc = &stcb->asoc; 3829163953Srrs 3830163953Srrs seq = ntohl(req->request_seq); 3831163953Srrs 3832163953Srrs /* now if its not a duplicate we process it */ 3833163953Srrs if (asoc->str_reset_seq_in == seq) { 3834163953Srrs len = ntohs(req->ph.param_length); 3835163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3836163953Srrs /* 3837163953Srrs * the sender is resetting, handle the list issue.. we must 3838163953Srrs * a) verify if we can do the reset, if so no problem b) If 3839163953Srrs * we can't do the reset we must copy the request. c) queue 3840163953Srrs * it, and setup the data in processor to trigger it off 3841163953Srrs * when needed and dequeue all the queued data. 3842163953Srrs */ 3843163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3844163953Srrs 3845163953Srrs /* move the reset action back one */ 3846163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3847235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3848235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3849235064Stuexen } else if (trunc) { 3850235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3851216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3852163953Srrs /* we can do it now */ 3853163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3854235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3855163953Srrs } else { 3856163953Srrs /* 3857163953Srrs * we must queue it up and thus wait for the TSN's 3858163953Srrs * to arrive that are at or before tsn 3859163953Srrs */ 3860163953Srrs struct sctp_stream_reset_list *liste; 3861163953Srrs int siz; 3862163953Srrs 3863163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3864163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3865170091Srrs siz, SCTP_M_STRESET); 3866163953Srrs if (liste == NULL) { 3867163953Srrs /* gak out of memory */ 3868235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3869235064Stuexen sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3870163953Srrs return; 3871163953Srrs } 3872163953Srrs liste->tsn = tsn; 3873163953Srrs liste->number_entries = number_entries; 3874163953Srrs memcpy(&liste->req, req, 3875163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3876163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3877235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3878163953Srrs } 3879233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3880163953Srrs asoc->str_reset_seq_in++; 3881163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3882163953Srrs /* 3883163953Srrs * one seq back, just echo back last action since my 3884163953Srrs * response was lost. 3885163953Srrs */ 3886163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3887163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3888163953Srrs /* 3889163953Srrs * two 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[1]); 3893163953Srrs } else { 3894235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3895163953Srrs } 3896163953Srrs} 3897163953Srrs 3898188854Srrsstatic void 3899188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3900188854Srrs struct sctp_stream_reset_add_strm *str_add) 3901188854Srrs{ 3902188854Srrs /* 3903188854Srrs * Peer is requesting to add more streams. If its within our 3904188854Srrs * max-streams we will allow it. 3905188854Srrs */ 3906233660Srrs uint32_t num_stream, i; 3907188854Srrs uint32_t seq; 3908189121Srrs struct sctp_association *asoc = &stcb->asoc; 3909216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3910188854Srrs 3911188854Srrs /* Get the number. */ 3912188854Srrs seq = ntohl(str_add->request_seq); 3913188854Srrs num_stream = ntohs(str_add->number_of_streams); 3914188854Srrs /* Now what would be the new total? */ 3915189121Srrs if (asoc->str_reset_seq_in == seq) { 3916189121Srrs num_stream += stcb->asoc.streamincnt; 3917235064Stuexen stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3918235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3919235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3920235064Stuexen } else if ((num_stream > stcb->asoc.max_inbound_streams) || 3921233660Srrs (num_stream > 0xffff)) { 3922189121Srrs /* We must reject it they ask for to many */ 3923189121Srrs denied: 3924235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3925189121Srrs } else { 3926189121Srrs /* Ok, we can do that :-) */ 3927189121Srrs struct sctp_stream_in *oldstrm; 3928189121Srrs 3929189121Srrs /* save off the old */ 3930189121Srrs oldstrm = stcb->asoc.strmin; 3931189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3932189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3933189121Srrs SCTP_M_STRMI); 3934189121Srrs if (stcb->asoc.strmin == NULL) { 3935189121Srrs stcb->asoc.strmin = oldstrm; 3936189121Srrs goto denied; 3937189121Srrs } 3938189121Srrs /* copy off the old data */ 3939189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3940189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3941189121Srrs stcb->asoc.strmin[i].stream_no = i; 3942189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3943189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3944189121Srrs /* now anything on those queues? */ 3945216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3946189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3947189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3948189121Srrs } 3949189121Srrs } 3950189121Srrs /* Init the new streams */ 3951189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3952189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3953189121Srrs stcb->asoc.strmin[i].stream_no = i; 3954189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3955189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3956189121Srrs } 3957189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3958189121Srrs /* update the size */ 3959189121Srrs stcb->asoc.streamincnt = num_stream; 3960235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3961233660Srrs sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3962189121Srrs } 3963233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3964233660Srrs asoc->str_reset_seq_in++; 3965189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3966189121Srrs /* 3967189121Srrs * one seq back, just echo back last action since my 3968189121Srrs * response was lost. 3969189121Srrs */ 3970189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3971189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3972189121Srrs /* 3973189121Srrs * two 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[1]); 3977188854Srrs } else { 3978235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3979188854Srrs 3980188854Srrs } 3981188854Srrs} 3982188854Srrs 3983233660Srrsstatic void 3984233660Srrssctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3985233660Srrs struct sctp_stream_reset_add_strm *str_add) 3986233660Srrs{ 3987233660Srrs /* 3988233660Srrs * Peer is requesting to add more streams. If its within our 3989233660Srrs * max-streams we will allow it. 3990233660Srrs */ 3991233660Srrs uint16_t num_stream; 3992233660Srrs uint32_t seq; 3993233660Srrs struct sctp_association *asoc = &stcb->asoc; 3994233660Srrs 3995233660Srrs /* Get the number. */ 3996233660Srrs seq = ntohl(str_add->request_seq); 3997233660Srrs num_stream = ntohs(str_add->number_of_streams); 3998233660Srrs /* Now what would be the new total? */ 3999233660Srrs if (asoc->str_reset_seq_in == seq) { 4000235064Stuexen stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 4001235064Stuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 4002235064Stuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4003235064Stuexen } else if (stcb->asoc.stream_reset_outstanding) { 4004233660Srrs /* We must reject it we have something pending */ 4005235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 4006233660Srrs } else { 4007233660Srrs /* Ok, we can do that :-) */ 4008233660Srrs int mychk; 4009233660Srrs 4010233660Srrs mychk = stcb->asoc.streamoutcnt; 4011233660Srrs mychk += num_stream; 4012233660Srrs if (mychk < 0x10000) { 4013235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 4014233660Srrs if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) { 4015235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4016233660Srrs } 4017233660Srrs } else { 4018235064Stuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4019233660Srrs } 4020233660Srrs } 4021233660Srrs sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); 4022233660Srrs asoc->str_reset_seq_in++; 4023233660Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 4024233660Srrs /* 4025233660Srrs * one seq back, just echo back last action since my 4026233660Srrs * response was lost. 4027233660Srrs */ 4028233660Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 4029233660Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 4030233660Srrs /* 4031233660Srrs * two 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[1]); 4035233660Srrs } else { 4036235064Stuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 4037233660Srrs } 4038233660Srrs} 4039233660Srrs 4040170992Srrs#ifdef __GNUC__ 4041170992Srrs__attribute__((noinline)) 4042170992Srrs#endif 4043170992Srrs static int 4044170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 4045170992Srrs struct sctp_stream_reset_out_req *sr_req) 4046163953Srrs{ 4047163953Srrs int chk_length, param_len, ptype; 4048170992Srrs struct sctp_paramhdr pstore; 4049170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 4050233660Srrs uint32_t seq = 0; 4051163953Srrs int num_req = 0; 4052170992Srrs int trunc = 0; 4053163953Srrs struct sctp_tmit_chunk *chk; 4054163953Srrs struct sctp_chunkhdr *ch; 4055163953Srrs struct sctp_paramhdr *ph; 4056166675Srrs int ret_code = 0; 4057166675Srrs int num_param = 0; 4058163953Srrs 4059163953Srrs /* now it may be a reset or a reset-response */ 4060163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 4061163953Srrs 4062163953Srrs /* setup for adding the response */ 4063163953Srrs sctp_alloc_a_chunk(stcb, chk); 4064163953Srrs if (chk == NULL) { 4065163953Srrs return (ret_code); 4066163953Srrs } 4067163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 4068169208Srrs chk->rec.chunk_id.can_take_data = 0; 4069163953Srrs chk->asoc = &stcb->asoc; 4070163953Srrs chk->no_fr_allowed = 0; 4071163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4072166023Srrs chk->book_size_scale = 0; 4073165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 4074163953Srrs if (chk->data == NULL) { 4075163953Srrsstrres_nochunk: 4076163953Srrs if (chk->data) { 4077163953Srrs sctp_m_freem(chk->data); 4078163953Srrs chk->data = NULL; 4079163953Srrs } 4080221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4081163953Srrs return (ret_code); 4082163953Srrs } 4083165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4084163953Srrs 4085163953Srrs /* setup chunk parameters */ 4086163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 4087163953Srrs chk->snd_count = 0; 4088224641Stuexen chk->whoTo = NULL; 4089163953Srrs 4090163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 4091163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 4092163953Srrs ch->chunk_flags = 0; 4093163953Srrs ch->chunk_length = htons(chk->send_size); 4094165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4095170992Srrs offset += sizeof(struct sctp_chunkhdr); 4096163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4097170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4098170992Srrs if (ph == NULL) 4099170992Srrs break; 4100163953Srrs param_len = ntohs(ph->param_length); 4101163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4102163953Srrs /* bad param */ 4103163953Srrs break; 4104163953Srrs } 4105170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4106170992Srrs (uint8_t *) & cstore); 4107163953Srrs ptype = ntohs(ph->param_type); 4108163953Srrs num_param++; 4109170994Srrs if (param_len > (int)sizeof(cstore)) { 4110170992Srrs trunc = 1; 4111170992Srrs } else { 4112170992Srrs trunc = 0; 4113170992Srrs } 4114163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 4115163953Srrs /* hit the max of parameters already sorry.. */ 4116163953Srrs break; 4117163953Srrs } 4118163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4119163953Srrs struct sctp_stream_reset_out_request *req_out; 4120163953Srrs 4121163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 4122163953Srrs num_req++; 4123163953Srrs if (stcb->asoc.stream_reset_outstanding) { 4124163953Srrs seq = ntohl(req_out->response_seq); 4125163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 4126163953Srrs /* implicit ack */ 4127235064Stuexen (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); 4128163953Srrs } 4129163953Srrs } 4130170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4131233660Srrs } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { 4132188854Srrs struct sctp_stream_reset_add_strm *str_add; 4133188854Srrs 4134188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4135188854Srrs num_req++; 4136188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4137233660Srrs } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { 4138233660Srrs struct sctp_stream_reset_add_strm *str_add; 4139233660Srrs 4140233660Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4141233660Srrs num_req++; 4142233660Srrs sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); 4143163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4144163953Srrs struct sctp_stream_reset_in_request *req_in; 4145163953Srrs 4146163953Srrs num_req++; 4147163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 4148170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4149163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4150163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 4151163953Srrs 4152163953Srrs num_req++; 4153163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4154163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4155163953Srrs ret_code = 1; 4156163953Srrs goto strres_nochunk; 4157163953Srrs } 4158163953Srrs /* no more */ 4159163953Srrs break; 4160163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4161163953Srrs struct sctp_stream_reset_response *resp; 4162163953Srrs uint32_t result; 4163163953Srrs 4164163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4165163953Srrs seq = ntohl(resp->response_seq); 4166163953Srrs result = ntohl(resp->result); 4167163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4168163953Srrs ret_code = 1; 4169163953Srrs goto strres_nochunk; 4170163953Srrs } 4171163953Srrs } else { 4172163953Srrs break; 4173163953Srrs } 4174170992Srrs offset += SCTP_SIZE32(param_len); 4175163953Srrs chk_length -= SCTP_SIZE32(param_len); 4176163953Srrs } 4177163953Srrs if (num_req == 0) { 4178163953Srrs /* we have no response free the stuff */ 4179163953Srrs goto strres_nochunk; 4180163953Srrs } 4181163953Srrs /* ok we have a chunk to link in */ 4182163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4183163953Srrs chk, 4184163953Srrs sctp_next); 4185163953Srrs stcb->asoc.ctrl_queue_cnt++; 4186163953Srrs return (ret_code); 4187163953Srrs} 4188163953Srrs 4189163953Srrs/* 4190163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4191163953Srrs * to handle this, either we get the whole packet and must disect it 4192163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4193163953Srrs * from a middle box that did the disectting for us. 4194163953Srrs */ 4195163953Srrsstatic void 4196163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4197170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4198163953Srrs{ 4199163953Srrs uint32_t bottle_bw, on_queue; 4200163953Srrs uint16_t trunc_len; 4201163953Srrs unsigned int chlen; 4202163953Srrs unsigned int at; 4203163953Srrs struct sctp_chunk_desc desc; 4204163953Srrs struct sctp_chunkhdr *ch; 4205163953Srrs 4206163953Srrs chlen = ntohs(cp->ch.chunk_length); 4207163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4208163953Srrs /* XXX possible chlen underflow */ 4209163953Srrs if (chlen == 0) { 4210163953Srrs ch = NULL; 4211163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4212163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4213163953Srrs } else { 4214163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4215163953Srrs chlen -= sizeof(struct sctphdr); 4216163953Srrs /* XXX possible chlen underflow */ 4217163953Srrs memset(&desc, 0, sizeof(desc)); 4218163953Srrs } 4219163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4220170781Srrs if (trunc_len > limit) { 4221170781Srrs trunc_len = limit; 4222170781Srrs } 4223163953Srrs /* now the chunks themselves */ 4224163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4225163953Srrs desc.chunk_type = ch->chunk_type; 4226163953Srrs /* get amount we need to move */ 4227163953Srrs at = ntohs(ch->chunk_length); 4228163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4229163953Srrs /* corrupt chunk, maybe at the end? */ 4230163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4231163953Srrs break; 4232163953Srrs } 4233163953Srrs if (trunc_len == 0) { 4234163953Srrs /* we are supposed to have all of it */ 4235163953Srrs if (at > chlen) { 4236163953Srrs /* corrupt skip it */ 4237163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4238163953Srrs break; 4239163953Srrs } 4240163953Srrs } else { 4241163953Srrs /* is there enough of it left ? */ 4242163953Srrs if (desc.chunk_type == SCTP_DATA) { 4243163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4244163953Srrs sizeof(desc.data_bytes))) { 4245163953Srrs break; 4246163953Srrs } 4247163953Srrs } else { 4248163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4249163953Srrs break; 4250163953Srrs } 4251163953Srrs } 4252163953Srrs } 4253163953Srrs if (desc.chunk_type == SCTP_DATA) { 4254163953Srrs /* can we get out the tsn? */ 4255163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4256163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4257163953Srrs 4258163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4259163953Srrs /* yep */ 4260163953Srrs struct sctp_data_chunk *dcp; 4261163953Srrs uint8_t *ddp; 4262163953Srrs unsigned int iii; 4263163953Srrs 4264163953Srrs dcp = (struct sctp_data_chunk *)ch; 4265163953Srrs ddp = (uint8_t *) (dcp + 1); 4266163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4267163953Srrs desc.data_bytes[iii] = ddp[iii]; 4268163953Srrs } 4269163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4270163953Srrs } else { 4271163953Srrs /* nope we are done. */ 4272163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4273163953Srrs break; 4274163953Srrs } 4275163953Srrs } else { 4276163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4277163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4278163953Srrs } 4279163953Srrs 4280163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4281163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4282163953Srrs break; 4283163953Srrs } 4284163953Srrs if (SCTP_SIZE32(at) > chlen) { 4285163953Srrs break; 4286163953Srrs } 4287163953Srrs chlen -= SCTP_SIZE32(at); 4288163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4289163953Srrs /* done, none left */ 4290163953Srrs break; 4291163953Srrs } 4292163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4293163953Srrs } 4294163953Srrs /* Now update any rwnd --- possibly */ 4295163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4296163953Srrs /* From a peer, we get a rwnd report */ 4297163953Srrs uint32_t a_rwnd; 4298163953Srrs 4299163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4300163953Srrs 4301163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4302163953Srrs on_queue = ntohl(cp->current_onq); 4303163953Srrs if (bottle_bw && on_queue) { 4304163953Srrs /* a rwnd report is in here */ 4305163953Srrs if (bottle_bw > on_queue) 4306163953Srrs a_rwnd = bottle_bw - on_queue; 4307163953Srrs else 4308163953Srrs a_rwnd = 0; 4309163953Srrs 4310163953Srrs if (a_rwnd == 0) 4311163953Srrs stcb->asoc.peers_rwnd = 0; 4312163953Srrs else { 4313163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4314163953Srrs stcb->asoc.peers_rwnd = 4315163953Srrs a_rwnd - stcb->asoc.total_flight; 4316163953Srrs } else { 4317163953Srrs stcb->asoc.peers_rwnd = 0; 4318163953Srrs } 4319163953Srrs if (stcb->asoc.peers_rwnd < 4320163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4321163953Srrs /* SWS sender side engages */ 4322163953Srrs stcb->asoc.peers_rwnd = 0; 4323163953Srrs } 4324163953Srrs } 4325163953Srrs } 4326163953Srrs } else { 4327163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4328163953Srrs } 4329163953Srrs 4330163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4331163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4332163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4333163953Srrs (stcb->asoc.sat_network)) { 4334163953Srrs /* 4335163953Srrs * This is debateable but for sat networks it makes sense 4336163953Srrs * Note if a T3 timer has went off, we will prohibit any 4337163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4338163953Srrs */ 4339171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4340171440Srrs net, cp, &bottle_bw, &on_queue); 4341163953Srrs } 4342163953Srrs} 4343163953Srrs 4344163953Srrs/* 4345163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4346163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4347163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4348163953Srrs * length of the complete packet outputs: - length: modified to remaining 4349163953Srrs * length after control processing - netp: modified to new sctp_nets after 4350163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4351163953Srrs * bad packet,...) otherwise return the tcb for this packet 4352163953Srrs */ 4353170099Srrs#ifdef __GNUC__ 4354170099Srrs__attribute__((noinline)) 4355170099Srrs#endif 4356170099Srrs static struct sctp_tcb * 4357170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4358170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4359170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4360179157Srrs uint32_t vrf_id, uint16_t port) 4361163953Srrs{ 4362163953Srrs struct sctp_association *asoc; 4363163953Srrs uint32_t vtag_in; 4364163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4365170140Srrs uint32_t chk_length; 4366163953Srrs int ret; 4367168299Srrs int abort_no_unlock = 0; 4368218186Srrs int ecne_seen = 0; 4369163953Srrs 4370163953Srrs /* 4371163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4372163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4373163953Srrs * until we get into jumbo grams and such.. 4374163953Srrs */ 4375166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4376163953Srrs struct sctp_tcb *locked_tcb = stcb; 4377163953Srrs int got_auth = 0; 4378163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4379163953Srrs int auth_skipped = 0; 4380171990Srrs int asconf_cnt = 0; 4381163953Srrs 4382172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4383172090Srrs struct socket *so; 4384172090Srrs 4385172090Srrs#endif 4386172090Srrs 4387169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4388169420Srrs iphlen, *offset, length, stcb); 4389163953Srrs 4390163953Srrs /* validate chunk header length... */ 4391163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4392170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4393170056Srrs ntohs(ch->chunk_length)); 4394170091Srrs if (locked_tcb) { 4395170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4396170091Srrs } 4397163953Srrs return (NULL); 4398163953Srrs } 4399163953Srrs /* 4400163953Srrs * validate the verification tag 4401163953Srrs */ 4402163953Srrs vtag_in = ntohl(sh->v_tag); 4403163953Srrs 4404165220Srrs if (locked_tcb) { 4405165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4406165220Srrs } 4407163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4408170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4409170056Srrs ntohs(ch->chunk_length), vtag_in); 4410163953Srrs if (vtag_in != 0) { 4411163953Srrs /* protocol error- silently discard... */ 4412163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4413169378Srrs if (locked_tcb) { 4414163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4415169378Srrs } 4416163953Srrs return (NULL); 4417163953Srrs } 4418163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4419163953Srrs /* 4420163953Srrs * If there is no stcb, skip the AUTH chunk and process 4421163953Srrs * later after a stcb is found (to validate the lookup was 4422163953Srrs * valid. 4423163953Srrs */ 4424163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4425179783Srrs (stcb == NULL) && 4426179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4427163953Srrs /* save this chunk for later processing */ 4428163953Srrs auth_skipped = 1; 4429163953Srrs auth_offset = *offset; 4430163953Srrs auth_len = ntohs(ch->chunk_length); 4431163953Srrs 4432163953Srrs /* (temporarily) move past this chunk */ 4433163953Srrs *offset += SCTP_SIZE32(auth_len); 4434163953Srrs if (*offset >= length) { 4435163953Srrs /* no more data left in the mbuf chain */ 4436163953Srrs *offset = length; 4437170091Srrs if (locked_tcb) { 4438170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4439170091Srrs } 4440163953Srrs return (NULL); 4441163953Srrs } 4442163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4443163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4444163953Srrs } 4445169420Srrs if (ch == NULL) { 4446169420Srrs /* Help */ 4447169420Srrs *offset = length; 4448170091Srrs if (locked_tcb) { 4449170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4450170091Srrs } 4451169420Srrs return (NULL); 4452169420Srrs } 4453163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4454163953Srrs goto process_control_chunks; 4455163953Srrs } 4456163953Srrs /* 4457163953Srrs * first check if it's an ASCONF with an unknown src addr we 4458163953Srrs * need to look inside to find the association 4459163953Srrs */ 4460163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4461171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4462171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4463171990Srrs 4464163953Srrs /* inp's refcount may be reduced */ 4465163953Srrs SCTP_INP_INCR_REF(inp); 4466163953Srrs 4467171990Srrs asconf_offset = *offset; 4468171990Srrs do { 4469171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4470171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4471171990Srrs break; 4472228653Stuexen stcb = sctp_findassociation_ep_asconf(m, 4473185694Srrs *offset, sh, &inp, netp, vrf_id); 4474171990Srrs if (stcb != NULL) 4475171990Srrs break; 4476171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4477171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4478171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4479171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4480163953Srrs if (stcb == NULL) { 4481163953Srrs /* 4482163953Srrs * reduce inp's refcount if not reduced in 4483163953Srrs * sctp_findassociation_ep_asconf(). 4484163953Srrs */ 4485163953Srrs SCTP_INP_DECR_REF(inp); 4486171990Srrs } else { 4487171990Srrs locked_tcb = stcb; 4488163953Srrs } 4489171990Srrs 4490163953Srrs /* now go back and verify any auth chunk to be sure */ 4491163953Srrs if (auth_skipped && (stcb != NULL)) { 4492163953Srrs struct sctp_auth_chunk *auth; 4493163953Srrs 4494163953Srrs auth = (struct sctp_auth_chunk *) 4495163953Srrs sctp_m_getptr(m, auth_offset, 4496163953Srrs auth_len, chunk_buf); 4497163953Srrs got_auth = 1; 4498163953Srrs auth_skipped = 0; 4499169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4500163953Srrs auth_offset)) { 4501163953Srrs /* auth HMAC failed so dump it */ 4502163953Srrs *offset = length; 4503170091Srrs if (locked_tcb) { 4504170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4505170091Srrs } 4506163953Srrs return (NULL); 4507163953Srrs } else { 4508163953Srrs /* remaining chunks are HMAC checked */ 4509163953Srrs stcb->asoc.authenticated = 1; 4510163953Srrs } 4511163953Srrs } 4512163953Srrs } 4513163953Srrs if (stcb == NULL) { 4514163953Srrs /* no association, so it's out of the blue... */ 4515169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4516179157Srrs vrf_id, port); 4517163953Srrs *offset = length; 4518169378Srrs if (locked_tcb) { 4519163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4520169378Srrs } 4521163953Srrs return (NULL); 4522163953Srrs } 4523163953Srrs asoc = &stcb->asoc; 4524163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4525163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4526163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4527163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4528163953Srrs if ((vtag_in == asoc->my_vtag) || 4529163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4530163953Srrs (vtag_in == asoc->peer_vtag))) { 4531163953Srrs /* this is valid */ 4532163953Srrs } else { 4533163953Srrs /* drop this packet... */ 4534163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4535169378Srrs if (locked_tcb) { 4536163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4537169378Srrs } 4538163953Srrs return (NULL); 4539163953Srrs } 4540163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4541163953Srrs if (vtag_in != asoc->my_vtag) { 4542163953Srrs /* 4543163953Srrs * this could be a stale SHUTDOWN-ACK or the 4544163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4545163953Srrs * is still hung; we have started a new asoc 4546163953Srrs * but it won't complete until the shutdown 4547163953Srrs * is completed 4548163953Srrs */ 4549169378Srrs if (locked_tcb) { 4550163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4551169378Srrs } 4552163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4553179157Srrs NULL, vrf_id, port); 4554163953Srrs return (NULL); 4555163953Srrs } 4556163953Srrs } else { 4557163953Srrs /* for all other chunks, vtag must match */ 4558163953Srrs if (vtag_in != asoc->my_vtag) { 4559163953Srrs /* invalid vtag... */ 4560169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4561169420Srrs "invalid vtag: %xh, expect %xh\n", 4562169420Srrs vtag_in, asoc->my_vtag); 4563163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4564169378Srrs if (locked_tcb) { 4565163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4566169378Srrs } 4567163953Srrs *offset = length; 4568163953Srrs return (NULL); 4569163953Srrs } 4570163953Srrs } 4571163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4572163953Srrs /* 4573163953Srrs * process all control chunks... 4574163953Srrs */ 4575163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4576185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4577163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4578163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4579163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4580179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4581171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4582171943Srrs stcb->asoc.overall_error_count, 4583171943Srrs 0, 4584171943Srrs SCTP_FROM_SCTP_INPUT, 4585171943Srrs __LINE__); 4586171943Srrs } 4587163953Srrs stcb->asoc.overall_error_count = 0; 4588163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4589163953Srrs *netp); 4590163953Srrs } 4591163953Srrsprocess_control_chunks: 4592163953Srrs while (IS_SCTP_CONTROL(ch)) { 4593163953Srrs /* validate chunk length */ 4594163953Srrs chk_length = ntohs(ch->chunk_length); 4595169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4596169420Srrs ch->chunk_type, chk_length); 4597170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4598170140Srrs if (chk_length < sizeof(*ch) || 4599170140Srrs (*offset + (int)chk_length) > length) { 4600163953Srrs *offset = length; 4601169378Srrs if (locked_tcb) { 4602163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4603169378Srrs } 4604163953Srrs return (NULL); 4605163953Srrs } 4606163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4607163953Srrs /* 4608163953Srrs * INIT-ACK only gets the init ack "header" portion only 4609163953Srrs * because we don't have to process the peer's COOKIE. All 4610163953Srrs * others get a complete chunk. 4611163953Srrs */ 4612169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4613169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4614163953Srrs /* get an init-ack chunk */ 4615163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4616163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4617163953Srrs if (ch == NULL) { 4618163953Srrs *offset = length; 4619169378Srrs if (locked_tcb) { 4620163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4621169378Srrs } 4622163953Srrs return (NULL); 4623163953Srrs } 4624170642Srrs } else { 4625170786Srrs /* For cookies and all other chunks. */ 4626169208Srrs if (chk_length > sizeof(chunk_buf)) { 4627169208Srrs /* 4628169208Srrs * use just the size of the chunk buffer so 4629170642Srrs * the front part of our chunks fit in 4630170642Srrs * contiguous space up to the chunk buffer 4631170642Srrs * size (508 bytes). For chunks that need to 4632170786Srrs * get more than that they must use the 4633170642Srrs * sctp_m_getptr() function or other means 4634170786Srrs * (e.g. know how to parse mbuf chains). 4635170786Srrs * Cookies do this already. 4636169208Srrs */ 4637169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4638169208Srrs (sizeof(chunk_buf) - 4), 4639169208Srrs chunk_buf); 4640169208Srrs if (ch == NULL) { 4641169208Srrs *offset = length; 4642169378Srrs if (locked_tcb) { 4643169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4644169378Srrs } 4645169208Srrs return (NULL); 4646169208Srrs } 4647169208Srrs } else { 4648169208Srrs /* We can fit it all */ 4649170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4650170642Srrs chk_length, chunk_buf); 4651170642Srrs if (ch == NULL) { 4652170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4653170642Srrs *offset = length; 4654170642Srrs if (locked_tcb) { 4655170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4656169208Srrs } 4657170642Srrs return (NULL); 4658163953Srrs } 4659163953Srrs } 4660163953Srrs } 4661163953Srrs num_chunks++; 4662163953Srrs /* Save off the last place we got a control from */ 4663163953Srrs if (stcb != NULL) { 4664169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4665163953Srrs /* 4666163953Srrs * allow last_control to be NULL if 4667163953Srrs * ASCONF... ASCONF processing will find the 4668163953Srrs * right net later 4669163953Srrs */ 4670169420Srrs if ((netp != NULL) && (*netp != NULL)) 4671169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4672163953Srrs } 4673163953Srrs } 4674163953Srrs#ifdef SCTP_AUDITING_ENABLED 4675163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4676163953Srrs#endif 4677163953Srrs 4678163953Srrs /* check to see if this chunk required auth, but isn't */ 4679179783Srrs if ((stcb != NULL) && 4680179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4681179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4682163953Srrs !stcb->asoc.authenticated) { 4683163953Srrs /* "silently" ignore */ 4684163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4685163953Srrs goto next_chunk; 4686163953Srrs } 4687163953Srrs switch (ch->chunk_type) { 4688163953Srrs case SCTP_INITIATION: 4689169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4690229774Stuexen /* The INIT chunk must be the only chunk. */ 4691229774Stuexen if ((num_chunks > 1) || 4692229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4693229774Stuexen sctp_abort_association(inp, stcb, m, 4694229774Stuexen iphlen, sh, NULL, vrf_id, port); 4695163953Srrs *offset = length; 4696163953Srrs return (NULL); 4697163953Srrs } 4698229774Stuexen /* Honor our resource limit. */ 4699229774Stuexen if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4700229774Stuexen struct mbuf *op_err; 4701229774Stuexen 4702229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4703229774Stuexen sctp_abort_association(inp, stcb, m, 4704229774Stuexen iphlen, sh, op_err, vrf_id, port); 4705163953Srrs *offset = length; 4706163953Srrs return (NULL); 4707163953Srrs } 4708229774Stuexen sctp_handle_init(m, iphlen, *offset, sh, 4709229774Stuexen (struct sctp_init_chunk *)ch, inp, 4710229774Stuexen stcb, &abort_no_unlock, vrf_id, port); 4711163953Srrs *offset = length; 4712229774Stuexen if ((!abort_no_unlock) && (locked_tcb)) { 4713163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4714169378Srrs } 4715163953Srrs return (NULL); 4716163953Srrs break; 4717170642Srrs case SCTP_PAD_CHUNK: 4718170642Srrs break; 4719163953Srrs case SCTP_INITIATION_ACK: 4720169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4721163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4722163953Srrs /* We are not interested anymore */ 4723163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4724163953Srrs ; 4725163953Srrs } else { 4726208876Srrs if (locked_tcb != stcb) { 4727208876Srrs /* Very unlikely */ 4728163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4729169378Srrs } 4730163953Srrs *offset = length; 4731163953Srrs if (stcb) { 4732172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4733172090Srrs so = SCTP_INP_SO(inp); 4734172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4735172090Srrs SCTP_TCB_UNLOCK(stcb); 4736172090Srrs SCTP_SOCKET_LOCK(so, 1); 4737172090Srrs SCTP_TCB_LOCK(stcb); 4738172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4739172090Srrs#endif 4740171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4741172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4742172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4743172090Srrs#endif 4744163953Srrs } 4745163953Srrs return (NULL); 4746163953Srrs } 4747163953Srrs } 4748229775Stuexen /* The INIT-ACK chunk must be the only chunk. */ 4749163953Srrs if ((num_chunks > 1) || 4750229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4751163953Srrs *offset = length; 4752169378Srrs if (locked_tcb) { 4753163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4754169378Srrs } 4755163953Srrs return (NULL); 4756163953Srrs } 4757169420Srrs if ((netp) && (*netp)) { 4758169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4759170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4760169420Srrs } else { 4761169420Srrs ret = -1; 4762169420Srrs } 4763229774Stuexen *offset = length; 4764229774Stuexen if (abort_no_unlock) { 4765229774Stuexen return (NULL); 4766229774Stuexen } 4767163953Srrs /* 4768163953Srrs * Special case, I must call the output routine to 4769163953Srrs * get the cookie echoed 4770163953Srrs */ 4771229774Stuexen if ((stcb != NULL) && (ret == 0)) { 4772172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4773229774Stuexen } 4774169378Srrs if (locked_tcb) { 4775163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4776169378Srrs } 4777163953Srrs return (NULL); 4778163953Srrs break; 4779163953Srrs case SCTP_SELECTIVE_ACK: 4780163953Srrs { 4781163953Srrs struct sctp_sack_chunk *sack; 4782163953Srrs int abort_now = 0; 4783163953Srrs uint32_t a_rwnd, cum_ack; 4784202526Stuexen uint16_t num_seg, num_dup; 4785202526Stuexen uint8_t flags; 4786202526Stuexen int offset_seg, offset_dup; 4787163953Srrs 4788211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4789211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4790202526Stuexen if (stcb == NULL) { 4791202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4792202526Stuexen break; 4793169208Srrs } 4794202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4795202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4796202526Stuexen break; 4797202526Stuexen } 4798171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4799171990Srrs /*- 4800171990Srrs * If we have sent a shutdown-ack, we will pay no 4801171990Srrs * attention to a sack sent in to us since 4802171990Srrs * we don't care anymore. 4803171990Srrs */ 4804184883Srrs break; 4805171990Srrs } 4806163953Srrs sack = (struct sctp_sack_chunk *)ch; 4807202526Stuexen flags = ch->chunk_flags; 4808163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4809163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4810202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4811163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4812202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4813202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4814202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4815202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4816202526Stuexen break; 4817202526Stuexen } 4818202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4819202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4820170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4821202526Stuexen cum_ack, num_seg, a_rwnd); 4822163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4823163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4824163953Srrs (num_seg == 0) && 4825216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4826163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4827216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4828163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4829163953Srrs ) { 4830163953Srrs /* 4831163953Srrs * We have a SIMPLE sack having no 4832163953Srrs * prior segments and data on sent 4833163953Srrs * queue to be acked.. Use the 4834163953Srrs * faster path sack processing. We 4835163953Srrs * also allow window update sacks 4836163953Srrs * with no missing segments to go 4837163953Srrs * this way too. 4838163953Srrs */ 4839218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4840163953Srrs } else { 4841169420Srrs if (netp && *netp) 4842228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4843202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4844218186Srrs cum_ack, a_rwnd, ecne_seen); 4845163953Srrs } 4846202526Stuexen if (abort_now) { 4847202526Stuexen /* ABORT signal from sack processing */ 4848202526Stuexen *offset = length; 4849202526Stuexen return (NULL); 4850202526Stuexen } 4851185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4852185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4853185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4854185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4855185694Srrs } 4856163953Srrs } 4857163953Srrs break; 4858185694Srrs /* 4859185694Srrs * EY - nr_sack: If the received chunk is an 4860185694Srrs * nr_sack chunk 4861185694Srrs */ 4862185694Srrs case SCTP_NR_SELECTIVE_ACK: 4863185694Srrs { 4864185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4865185694Srrs int abort_now = 0; 4866185694Srrs uint32_t a_rwnd, cum_ack; 4867202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4868202526Stuexen uint8_t flags; 4869202526Stuexen int offset_seg, offset_dup; 4870185694Srrs 4871211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4872211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4873212225Srrs if (stcb == NULL) { 4874212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4875212225Srrs break; 4876212225Srrs } 4877211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4878211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4879185694Srrs goto unknown_chunk; 4880185694Srrs } 4881202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4882202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4883202526Stuexen break; 4884202526Stuexen } 4885185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4886185694Srrs /*- 4887185694Srrs * If we have sent a shutdown-ack, we will pay no 4888185694Srrs * attention to a sack sent in to us since 4889185694Srrs * we don't care anymore. 4890185694Srrs */ 4891202526Stuexen break; 4892185694Srrs } 4893185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4894202526Stuexen flags = ch->chunk_flags; 4895185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4896185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4897185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4898202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4899185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4900202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4901202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4902202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4903202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4904202526Stuexen break; 4905202526Stuexen } 4906202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4907202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4908185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4909202526Stuexen cum_ack, num_seg, a_rwnd); 4910185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4911185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4912202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4913216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4914185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4915216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4916202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4917185694Srrs /* 4918185694Srrs * We have a SIMPLE sack having no 4919185694Srrs * prior segments and data on sent 4920202526Stuexen * queue to be acked. Use the faster 4921202526Stuexen * path sack processing. We also 4922202526Stuexen * allow window update sacks with no 4923202526Stuexen * missing segments to go this way 4924202526Stuexen * too. 4925185694Srrs */ 4926218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4927218186Srrs &abort_now, ecne_seen); 4928185694Srrs } else { 4929185694Srrs if (netp && *netp) 4930228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4931202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4932218186Srrs cum_ack, a_rwnd, ecne_seen); 4933185694Srrs } 4934202526Stuexen if (abort_now) { 4935202526Stuexen /* ABORT signal from sack processing */ 4936202526Stuexen *offset = length; 4937202526Stuexen return (NULL); 4938202526Stuexen } 4939185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4940185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4941185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4942185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4943185694Srrs } 4944185694Srrs } 4945185694Srrs break; 4946185694Srrs 4947163953Srrs case SCTP_HEARTBEAT_REQUEST: 4948169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4949169420Srrs if ((stcb) && netp && *netp) { 4950169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4951169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4952169420Srrs chk_length, *netp); 4953169420Srrs 4954169420Srrs /* He's alive so give him credit */ 4955179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4956171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4957171943Srrs stcb->asoc.overall_error_count, 4958171943Srrs 0, 4959171943Srrs SCTP_FROM_SCTP_INPUT, 4960171943Srrs __LINE__); 4961171943Srrs } 4962169420Srrs stcb->asoc.overall_error_count = 0; 4963163953Srrs } 4964163953Srrs break; 4965163953Srrs case SCTP_HEARTBEAT_ACK: 4966169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4967169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4968169208Srrs /* Its not ours */ 4969169352Srrs *offset = length; 4970169378Srrs if (locked_tcb) { 4971169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4972169378Srrs } 4973169208Srrs return (NULL); 4974169208Srrs } 4975163953Srrs /* He's alive so give him credit */ 4976179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4977171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4978171943Srrs stcb->asoc.overall_error_count, 4979171943Srrs 0, 4980171943Srrs SCTP_FROM_SCTP_INPUT, 4981171943Srrs __LINE__); 4982171943Srrs } 4983163953Srrs stcb->asoc.overall_error_count = 0; 4984163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4985169420Srrs if (netp && *netp) 4986169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4987169420Srrs stcb, *netp); 4988163953Srrs break; 4989163953Srrs case SCTP_ABORT_ASSOCIATION: 4990170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4991170091Srrs stcb); 4992169420Srrs if ((stcb) && netp && *netp) 4993169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4994169420Srrs stcb, *netp); 4995163953Srrs *offset = length; 4996163953Srrs return (NULL); 4997163953Srrs break; 4998163953Srrs case SCTP_SHUTDOWN: 4999170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 5000170091Srrs stcb); 5001169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 5002169208Srrs *offset = length; 5003169378Srrs if (locked_tcb) { 5004169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5005169378Srrs } 5006169208Srrs return (NULL); 5007169420Srrs } 5008169420Srrs if (netp && *netp) { 5009163953Srrs int abort_flag = 0; 5010163953Srrs 5011163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 5012163953Srrs stcb, *netp, &abort_flag); 5013163953Srrs if (abort_flag) { 5014163953Srrs *offset = length; 5015163953Srrs return (NULL); 5016163953Srrs } 5017163953Srrs } 5018163953Srrs break; 5019163953Srrs case SCTP_SHUTDOWN_ACK: 5020170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 5021169420Srrs if ((stcb) && (netp) && (*netp)) 5022169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 5023163953Srrs *offset = length; 5024163953Srrs return (NULL); 5025163953Srrs break; 5026169420Srrs 5027163953Srrs case SCTP_OPERATION_ERROR: 5028169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 5029169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 5030163953Srrs *offset = length; 5031163953Srrs return (NULL); 5032163953Srrs } 5033163953Srrs break; 5034163953Srrs case SCTP_COOKIE_ECHO: 5035169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5036170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 5037163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5038163953Srrs ; 5039163953Srrs } else { 5040169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5041163953Srrs /* We are not interested anymore */ 5042208878Srrs abend: 5043208853Srrs if (stcb) { 5044208853Srrs SCTP_TCB_UNLOCK(stcb); 5045208853Srrs } 5046163953Srrs *offset = length; 5047163953Srrs return (NULL); 5048163953Srrs } 5049163953Srrs } 5050163953Srrs /* 5051163953Srrs * First are we accepting? We do this again here 5052207924Srrs * since it is possible that a previous endpoint WAS 5053207924Srrs * listening responded to a INIT-ACK and then 5054163953Srrs * closed. We opened and bound.. and are now no 5055163953Srrs * longer listening. 5056163953Srrs */ 5057172703Srrs 5058172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 5059172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5060179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 5061229774Stuexen struct mbuf *op_err; 5062163953Srrs 5063229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 5064172703Srrs sctp_abort_association(inp, stcb, m, 5065229774Stuexen iphlen, sh, op_err, vrf_id, port); 5066163953Srrs } 5067172703Srrs *offset = length; 5068172703Srrs return (NULL); 5069172703Srrs } else { 5070163953Srrs struct mbuf *ret_buf; 5071165220Srrs struct sctp_inpcb *linp; 5072163953Srrs 5073169420Srrs if (stcb) { 5074165220Srrs linp = NULL; 5075169420Srrs } else { 5076165220Srrs linp = inp; 5077169420Srrs } 5078165220Srrs 5079169420Srrs if (linp) { 5080165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 5081208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5082208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5083208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5084208878Srrs goto abend; 5085208878Srrs } 5086169420Srrs } 5087169420Srrs if (netp) { 5088169420Srrs ret_buf = 5089169420Srrs sctp_handle_cookie_echo(m, iphlen, 5090169420Srrs *offset, sh, 5091169420Srrs (struct sctp_cookie_echo_chunk *)ch, 5092169420Srrs &inp, &stcb, netp, 5093169420Srrs auth_skipped, 5094169420Srrs auth_offset, 5095169420Srrs auth_len, 5096169420Srrs &locked_tcb, 5097179157Srrs vrf_id, 5098179157Srrs port); 5099169420Srrs } else { 5100169420Srrs ret_buf = NULL; 5101169420Srrs } 5102169420Srrs if (linp) { 5103165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5104169420Srrs } 5105163953Srrs if (ret_buf == NULL) { 5106163953Srrs if (locked_tcb) { 5107163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5108163953Srrs } 5109169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5110169420Srrs "GAK, null buffer\n"); 5111163953Srrs *offset = length; 5112163953Srrs return (NULL); 5113163953Srrs } 5114163953Srrs /* if AUTH skipped, see if it verified... */ 5115163953Srrs if (auth_skipped) { 5116163953Srrs got_auth = 1; 5117163953Srrs auth_skipped = 0; 5118163953Srrs } 5119163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5120163953Srrs /* 5121163953Srrs * Restart the timer if we have 5122163953Srrs * pending data 5123163953Srrs */ 5124163953Srrs struct sctp_tmit_chunk *chk; 5125163953Srrs 5126163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5127216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5128163953Srrs } 5129163953Srrs } 5130163953Srrs break; 5131163953Srrs case SCTP_COOKIE_ACK: 5132170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5133169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5134169378Srrs if (locked_tcb) { 5135169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5136169378Srrs } 5137169352Srrs return (NULL); 5138169352Srrs } 5139163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5140163953Srrs /* We are not interested anymore */ 5141163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5142163953Srrs ; 5143169420Srrs } else if (stcb) { 5144172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5145172090Srrs so = SCTP_INP_SO(inp); 5146172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5147172090Srrs SCTP_TCB_UNLOCK(stcb); 5148172090Srrs SCTP_SOCKET_LOCK(so, 1); 5149172090Srrs SCTP_TCB_LOCK(stcb); 5150172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5151172090Srrs#endif 5152171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5153172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5154172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5155172090Srrs#endif 5156163953Srrs *offset = length; 5157163953Srrs return (NULL); 5158163953Srrs } 5159163953Srrs } 5160163953Srrs /* He's alive so give him credit */ 5161169420Srrs if ((stcb) && netp && *netp) { 5162179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5163171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5164171943Srrs stcb->asoc.overall_error_count, 5165171943Srrs 0, 5166171943Srrs SCTP_FROM_SCTP_INPUT, 5167171943Srrs __LINE__); 5168171943Srrs } 5169169378Srrs stcb->asoc.overall_error_count = 0; 5170169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5171169378Srrs } 5172163953Srrs break; 5173163953Srrs case SCTP_ECN_ECHO: 5174169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5175163953Srrs /* He's alive so give him credit */ 5176169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5177169208Srrs /* Its not ours */ 5178169378Srrs if (locked_tcb) { 5179169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5180169378Srrs } 5181169208Srrs *offset = length; 5182169208Srrs return (NULL); 5183169208Srrs } 5184169378Srrs if (stcb) { 5185179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5186171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5187171943Srrs stcb->asoc.overall_error_count, 5188171943Srrs 0, 5189171943Srrs SCTP_FROM_SCTP_INPUT, 5190171943Srrs __LINE__); 5191171943Srrs } 5192169378Srrs stcb->asoc.overall_error_count = 0; 5193169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5194169378Srrs stcb); 5195218186Srrs ecne_seen = 1; 5196169378Srrs } 5197163953Srrs break; 5198163953Srrs case SCTP_ECN_CWR: 5199169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5200163953Srrs /* He's alive so give him credit */ 5201169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5202169208Srrs /* Its not ours */ 5203169378Srrs if (locked_tcb) { 5204169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5205169378Srrs } 5206169208Srrs *offset = length; 5207169208Srrs return (NULL); 5208169208Srrs } 5209169378Srrs if (stcb) { 5210179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5211171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5212171943Srrs stcb->asoc.overall_error_count, 5213171943Srrs 0, 5214171943Srrs SCTP_FROM_SCTP_INPUT, 5215171943Srrs __LINE__); 5216171943Srrs } 5217169378Srrs stcb->asoc.overall_error_count = 0; 5218218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5219169378Srrs } 5220163953Srrs break; 5221163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5222170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5223163953Srrs /* must be first and only chunk */ 5224163953Srrs if ((num_chunks > 1) || 5225170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5226163953Srrs *offset = length; 5227169378Srrs if (locked_tcb) { 5228163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5229169378Srrs } 5230163953Srrs return (NULL); 5231163953Srrs } 5232169420Srrs if ((stcb) && netp && *netp) { 5233169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5234169378Srrs stcb, *netp); 5235169378Srrs } 5236163953Srrs *offset = length; 5237163953Srrs return (NULL); 5238163953Srrs break; 5239163953Srrs case SCTP_ASCONF: 5240169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5241163953Srrs /* He's alive so give him credit */ 5242169378Srrs if (stcb) { 5243179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5244171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5245171943Srrs stcb->asoc.overall_error_count, 5246171943Srrs 0, 5247171943Srrs SCTP_FROM_SCTP_INPUT, 5248171943Srrs __LINE__); 5249171943Srrs } 5250169378Srrs stcb->asoc.overall_error_count = 0; 5251169378Srrs sctp_handle_asconf(m, *offset, 5252171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5253171990Srrs asconf_cnt++; 5254169378Srrs } 5255163953Srrs break; 5256163953Srrs case SCTP_ASCONF_ACK: 5257169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5258169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5259169208Srrs /* Its not ours */ 5260169378Srrs if (locked_tcb) { 5261169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5262169378Srrs } 5263169208Srrs *offset = length; 5264169208Srrs return (NULL); 5265169208Srrs } 5266169420Srrs if ((stcb) && netp && *netp) { 5267169378Srrs /* He's alive so give him credit */ 5268179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5269171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5270171943Srrs stcb->asoc.overall_error_count, 5271171943Srrs 0, 5272171943Srrs SCTP_FROM_SCTP_INPUT, 5273171943Srrs __LINE__); 5274171943Srrs } 5275169378Srrs stcb->asoc.overall_error_count = 0; 5276169378Srrs sctp_handle_asconf_ack(m, *offset, 5277172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5278172190Srrs if (abort_no_unlock) 5279172190Srrs return (NULL); 5280169378Srrs } 5281163953Srrs break; 5282163953Srrs case SCTP_FORWARD_CUM_TSN: 5283169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5284169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5285169208Srrs /* Its not ours */ 5286169378Srrs if (locked_tcb) { 5287169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5288169378Srrs } 5289169208Srrs *offset = length; 5290169208Srrs return (NULL); 5291169208Srrs } 5292163953Srrs /* He's alive so give him credit */ 5293169378Srrs if (stcb) { 5294163953Srrs int abort_flag = 0; 5295163953Srrs 5296163953Srrs stcb->asoc.overall_error_count = 0; 5297179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5298171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5299171943Srrs stcb->asoc.overall_error_count, 5300171943Srrs 0, 5301171943Srrs SCTP_FROM_SCTP_INPUT, 5302171943Srrs __LINE__); 5303171943Srrs } 5304163953Srrs *fwd_tsn_seen = 1; 5305163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5306163953Srrs /* We are not interested anymore */ 5307172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5308172090Srrs so = SCTP_INP_SO(inp); 5309172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5310172090Srrs SCTP_TCB_UNLOCK(stcb); 5311172090Srrs SCTP_SOCKET_LOCK(so, 1); 5312172090Srrs SCTP_TCB_LOCK(stcb); 5313172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5314172090Srrs#endif 5315171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5316172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5317172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5318172090Srrs#endif 5319163953Srrs *offset = length; 5320163953Srrs return (NULL); 5321163953Srrs } 5322163953Srrs sctp_handle_forward_tsn(stcb, 5323170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5324163953Srrs if (abort_flag) { 5325163953Srrs *offset = length; 5326163953Srrs return (NULL); 5327163953Srrs } else { 5328179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5329171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5330171943Srrs stcb->asoc.overall_error_count, 5331171943Srrs 0, 5332171943Srrs SCTP_FROM_SCTP_INPUT, 5333171943Srrs __LINE__); 5334171943Srrs } 5335163953Srrs stcb->asoc.overall_error_count = 0; 5336163953Srrs } 5337163953Srrs 5338163953Srrs } 5339163953Srrs break; 5340163953Srrs case SCTP_STREAM_RESET: 5341169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5342169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5343169208Srrs /* Its not ours */ 5344169378Srrs if (locked_tcb) { 5345169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5346169378Srrs } 5347169208Srrs *offset = length; 5348169208Srrs return (NULL); 5349169208Srrs } 5350163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5351163953Srrs /* We are not interested anymore */ 5352172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5353172090Srrs so = SCTP_INP_SO(inp); 5354172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5355172090Srrs SCTP_TCB_UNLOCK(stcb); 5356172090Srrs SCTP_SOCKET_LOCK(so, 1); 5357172090Srrs SCTP_TCB_LOCK(stcb); 5358172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5359172090Srrs#endif 5360171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5361172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5362172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5363172090Srrs#endif 5364163953Srrs *offset = length; 5365163953Srrs return (NULL); 5366163953Srrs } 5367169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5368169420Srrs /* 5369169420Srrs * hmm, peer should have announced this, but 5370169420Srrs * we will turn it on since he is sending us 5371169420Srrs * a stream reset. 5372169420Srrs */ 5373169420Srrs stcb->asoc.peer_supports_strreset = 1; 5374163953Srrs } 5375170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5376169420Srrs /* stop processing */ 5377169420Srrs *offset = length; 5378169420Srrs return (NULL); 5379169420Srrs } 5380163953Srrs break; 5381163953Srrs case SCTP_PACKET_DROPPED: 5382169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5383163953Srrs /* re-get it all please */ 5384169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5385169208Srrs /* Its not ours */ 5386169378Srrs if (locked_tcb) { 5387169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5388169378Srrs } 5389169208Srrs *offset = length; 5390169208Srrs return (NULL); 5391169208Srrs } 5392169420Srrs if (ch && (stcb) && netp && (*netp)) { 5393169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5394170781Srrs stcb, *netp, 5395170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5396170781Srrs 5397169378Srrs } 5398163953Srrs break; 5399163953Srrs 5400163953Srrs case SCTP_AUTHENTICATION: 5401169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5402179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5403163953Srrs goto unknown_chunk; 5404163953Srrs 5405163953Srrs if (stcb == NULL) { 5406163953Srrs /* save the first AUTH for later processing */ 5407163953Srrs if (auth_skipped == 0) { 5408163953Srrs auth_offset = *offset; 5409163953Srrs auth_len = chk_length; 5410163953Srrs auth_skipped = 1; 5411163953Srrs } 5412163953Srrs /* skip this chunk (temporarily) */ 5413163953Srrs goto next_chunk; 5414163953Srrs } 5415169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5416169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5417169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5418169208Srrs /* Its not ours */ 5419169378Srrs if (locked_tcb) { 5420169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5421169378Srrs } 5422169208Srrs *offset = length; 5423169208Srrs return (NULL); 5424169208Srrs } 5425163953Srrs if (got_auth == 1) { 5426163953Srrs /* skip this chunk... it's already auth'd */ 5427163953Srrs goto next_chunk; 5428163953Srrs } 5429163953Srrs got_auth = 1; 5430169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5431163953Srrs m, *offset)) { 5432163953Srrs /* auth HMAC failed so dump the packet */ 5433163953Srrs *offset = length; 5434163953Srrs return (stcb); 5435163953Srrs } else { 5436163953Srrs /* remaining chunks are HMAC checked */ 5437163953Srrs stcb->asoc.authenticated = 1; 5438163953Srrs } 5439163953Srrs break; 5440163953Srrs 5441163953Srrs default: 5442163953Srrs unknown_chunk: 5443163953Srrs /* it's an unknown chunk! */ 5444163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5445163953Srrs struct mbuf *mm; 5446163953Srrs struct sctp_paramhdr *phd; 5447163953Srrs 5448163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5449165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5450163953Srrs if (mm) { 5451163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5452163953Srrs /* 5453163953Srrs * We cheat and use param type since 5454163953Srrs * we did not bother to define a 5455163953Srrs * error cause struct. They are the 5456163953Srrs * same basic format with different 5457163953Srrs * names. 5458163953Srrs */ 5459163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5460163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5461165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5462234459Stuexen SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT); 5463165647Srrs if (SCTP_BUF_NEXT(mm)) { 5464234459Stuexen if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) { 5465234459Stuexen sctp_m_freem(mm); 5466234459Stuexen } else { 5467178202Srrs#ifdef SCTP_MBUF_LOGGING 5468234459Stuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5469234459Stuexen struct mbuf *mat; 5470178202Srrs 5471234461Stuexen for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) { 5472234459Stuexen if (SCTP_BUF_IS_EXTENDED(mat)) { 5473234459Stuexen sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5474234459Stuexen } 5475178202Srrs } 5476178202Srrs } 5477234459Stuexen#endif 5478234459Stuexen sctp_queue_op_err(stcb, mm); 5479178202Srrs } 5480163953Srrs } else { 5481163953Srrs sctp_m_freem(mm); 5482163953Srrs } 5483163953Srrs } 5484163953Srrs } 5485163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5486163953Srrs /* discard this packet */ 5487163953Srrs *offset = length; 5488163953Srrs return (stcb); 5489163953Srrs } /* else skip this bad chunk and continue... */ 5490163953Srrs break; 5491163953Srrs } /* switch (ch->chunk_type) */ 5492163953Srrs 5493163953Srrs 5494163953Srrsnext_chunk: 5495163953Srrs /* get the next chunk */ 5496163953Srrs *offset += SCTP_SIZE32(chk_length); 5497163953Srrs if (*offset >= length) { 5498163953Srrs /* no more data left in the mbuf chain */ 5499163953Srrs break; 5500163953Srrs } 5501163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5502163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5503163953Srrs if (ch == NULL) { 5504169378Srrs if (locked_tcb) { 5505163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5506169378Srrs } 5507163953Srrs *offset = length; 5508163953Srrs return (NULL); 5509163953Srrs } 5510163953Srrs } /* while */ 5511171990Srrs 5512171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5513171990Srrs sctp_send_asconf_ack(stcb); 5514171990Srrs } 5515163953Srrs return (stcb); 5516163953Srrs} 5517163953Srrs 5518163953Srrs 5519171440Srrs#ifdef INVARIANTS 5520208853Srrs#ifdef __GNUC__ 5521208853Srrs__attribute__((noinline)) 5522208853Srrs#endif 5523208853Srrs void 5524208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5525171440Srrs{ 5526208853Srrs struct sctp_tcb *lstcb; 5527171440Srrs 5528208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5529208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5530171440Srrs panic("Own lock on stcb at return from input"); 5531171440Srrs } 5532171440Srrs } 5533208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5534208876Srrs panic("Own create lock on inp"); 5535208876Srrs } 5536208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5537208876Srrs panic("Own inp lock on inp"); 5538208876Srrs } 5539171440Srrs} 5540171440Srrs 5541171440Srrs#endif 5542171440Srrs 5543163953Srrs/* 5544163953Srrs * common input chunk processing (v4 and v6) 5545163953Srrs */ 5546169378Srrsvoid 5547163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5548163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5549163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5550179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5551163953Srrs{ 5552163953Srrs /* 5553163953Srrs * Control chunk processing 5554163953Srrs */ 5555163953Srrs uint32_t high_tsn; 5556163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5557163953Srrs struct mbuf *m = *mm; 5558163953Srrs int un_sent; 5559218129Srrs int cnt_ctrl_ready = 0; 5560163953Srrs 5561163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5562163953Srrs#ifdef SCTP_AUDITING_ENABLED 5563163953Srrs sctp_audit_log(0xE0, 1); 5564163953Srrs sctp_auditing(0, inp, stcb, net); 5565163953Srrs#endif 5566163953Srrs 5567179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5568179783Srrs m, iphlen, offset, length, stcb); 5569163953Srrs if (stcb) { 5570163953Srrs /* always clear this before beginning a packet */ 5571163953Srrs stcb->asoc.authenticated = 0; 5572163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5573171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5574171990Srrs stcb, stcb->asoc.state); 5575171990Srrs 5576171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5577171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5578171745Srrs /*- 5579171745Srrs * If we hit here, we had a ref count 5580171745Srrs * up when the assoc was aborted and the 5581171745Srrs * timer is clearing out the assoc, we should 5582171745Srrs * NOT respond to any packet.. its OOTB. 5583171745Srrs */ 5584171745Srrs SCTP_TCB_UNLOCK(stcb); 5585171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5586179157Srrs vrf_id, port); 5587171745Srrs goto out_now; 5588171745Srrs } 5589163953Srrs } 5590163953Srrs if (IS_SCTP_CONTROL(ch)) { 5591163953Srrs /* process the control portion of the SCTP packet */ 5592169655Srrs /* sa_ignore NO_NULL_CHK */ 5593163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5594179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5595163953Srrs if (stcb) { 5596163953Srrs /* 5597163953Srrs * This covers us if the cookie-echo was there and 5598163953Srrs * it changes our INP. 5599163953Srrs */ 5600163953Srrs inp = stcb->sctp_ep; 5601179783Srrs if ((net) && (port)) { 5602179783Srrs if (net->port == 0) { 5603228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5604179783Srrs } 5605179783Srrs net->port = port; 5606179783Srrs } 5607163953Srrs } 5608163953Srrs } else { 5609163953Srrs /* 5610163953Srrs * no control chunks, so pre-process DATA chunks (these 5611163953Srrs * checks are taken care of by control processing) 5612163953Srrs */ 5613163953Srrs 5614163953Srrs /* 5615163953Srrs * if DATA only packet, and auth is required, then punt... 5616163953Srrs * can't have authenticated without any AUTH (control) 5617163953Srrs * chunks 5618163953Srrs */ 5619179783Srrs if ((stcb != NULL) && 5620179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5621179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5622163953Srrs /* "silently" ignore */ 5623163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5624163953Srrs SCTP_TCB_UNLOCK(stcb); 5625171440Srrs goto out_now; 5626163953Srrs } 5627163953Srrs if (stcb == NULL) { 5628163953Srrs /* out of the blue DATA chunk */ 5629169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5630179157Srrs vrf_id, port); 5631171440Srrs goto out_now; 5632163953Srrs } 5633163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5634163953Srrs /* v_tag mismatch! */ 5635163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5636163953Srrs SCTP_TCB_UNLOCK(stcb); 5637171440Srrs goto out_now; 5638163953Srrs } 5639163953Srrs } 5640163953Srrs 5641163953Srrs if (stcb == NULL) { 5642163953Srrs /* 5643163953Srrs * no valid TCB for this packet, or we found it's a bad 5644163953Srrs * packet while processing control, or we're done with this 5645163953Srrs * packet (done or skip rest of data), so we drop it... 5646163953Srrs */ 5647171440Srrs goto out_now; 5648163953Srrs } 5649163953Srrs /* 5650163953Srrs * DATA chunk processing 5651163953Srrs */ 5652163953Srrs /* plow through the data chunks while length > offset */ 5653163953Srrs 5654163953Srrs /* 5655163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5656163953Srrs * DATA is required. 5657163953Srrs */ 5658179783Srrs if ((length > offset) && 5659179783Srrs (stcb != NULL) && 5660179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5661179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5662163953Srrs !stcb->asoc.authenticated) { 5663163953Srrs /* "silently" ignore */ 5664163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5665169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5666169420Srrs "Data chunk requires AUTH, skipped\n"); 5667165220Srrs goto trigger_send; 5668163953Srrs } 5669163953Srrs if (length > offset) { 5670163953Srrs int retval; 5671163953Srrs 5672163953Srrs /* 5673163953Srrs * First check to make sure our state is correct. We would 5674163953Srrs * not get here unless we really did have a tag, so we don't 5675163953Srrs * abort if this happens, just dump the chunk silently. 5676163953Srrs */ 5677163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5678163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5679163953Srrs /* 5680163953Srrs * we consider data with valid tags in this state 5681163953Srrs * shows us the cookie-ack was lost. Imply it was 5682163953Srrs * there. 5683163953Srrs */ 5684179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5685171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5686171943Srrs stcb->asoc.overall_error_count, 5687171943Srrs 0, 5688171943Srrs SCTP_FROM_SCTP_INPUT, 5689171943Srrs __LINE__); 5690171943Srrs } 5691163953Srrs stcb->asoc.overall_error_count = 0; 5692163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5693163953Srrs break; 5694163953Srrs case SCTP_STATE_COOKIE_WAIT: 5695163953Srrs /* 5696163953Srrs * We consider OOTB any data sent during asoc setup. 5697163953Srrs */ 5698169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5699179157Srrs vrf_id, port); 5700163953Srrs SCTP_TCB_UNLOCK(stcb); 5701171440Srrs goto out_now; 5702171531Srrs /* sa_ignore NOTREACHED */ 5703163953Srrs break; 5704163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5705163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5706163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5707163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5708163953Srrs default: 5709163953Srrs SCTP_TCB_UNLOCK(stcb); 5710171440Srrs goto out_now; 5711171531Srrs /* sa_ignore NOTREACHED */ 5712163953Srrs break; 5713163953Srrs case SCTP_STATE_OPEN: 5714163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5715163953Srrs break; 5716163953Srrs } 5717163953Srrs /* plow through the data chunks while length > offset */ 5718163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5719163953Srrs inp, stcb, net, &high_tsn); 5720163953Srrs if (retval == 2) { 5721163953Srrs /* 5722163953Srrs * The association aborted, NO UNLOCK needed since 5723163953Srrs * the association is destroyed. 5724163953Srrs */ 5725171440Srrs goto out_now; 5726163953Srrs } 5727163953Srrs data_processed = 1; 5728163953Srrs /* 5729163953Srrs * Anything important needs to have been m_copy'ed in 5730163953Srrs * process_data 5731163953Srrs */ 5732163953Srrs } 5733218129Srrs /* take care of ecn */ 5734228907Stuexen if ((data_processed == 1) && 5735228907Stuexen (stcb->asoc.ecn_allowed == 1) && 5736218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5737218129Srrs /* Yep, we need to add a ECNE */ 5738218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5739218129Srrs } 5740163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5741216495Stuexen int was_a_gap; 5742216495Stuexen uint32_t highest_tsn; 5743163953Srrs 5744216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5745216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5746216495Stuexen } else { 5747216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5748163953Srrs } 5749216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5750190689Srrs stcb->asoc.send_sack = 1; 5751228653Stuexen sctp_sack_check(stcb, was_a_gap); 5752190689Srrs } else if (fwd_tsn_seen) { 5753190689Srrs stcb->asoc.send_sack = 1; 5754163953Srrs } 5755163953Srrs /* trigger send of any chunks in queue... */ 5756165220Srrstrigger_send: 5757163953Srrs#ifdef SCTP_AUDITING_ENABLED 5758163953Srrs sctp_audit_log(0xE0, 2); 5759163953Srrs sctp_auditing(1, inp, stcb, net); 5760163953Srrs#endif 5761169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5762169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5763169420Srrs stcb->asoc.peers_rwnd, 5764169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5765169420Srrs stcb->asoc.total_flight); 5766163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5767218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5768218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5769218129Srrs } 5770218129Srrs if (cnt_ctrl_ready || 5771163953Srrs ((un_sent) && 5772163953Srrs (stcb->asoc.peers_rwnd > 0 || 5773163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5774169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5775172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5776169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5777163953Srrs } 5778163953Srrs#ifdef SCTP_AUDITING_ENABLED 5779163953Srrs sctp_audit_log(0xE0, 3); 5780163953Srrs sctp_auditing(2, inp, stcb, net); 5781163953Srrs#endif 5782163953Srrs SCTP_TCB_UNLOCK(stcb); 5783171440Srrsout_now: 5784171440Srrs#ifdef INVARIANTS 5785171440Srrs sctp_validate_no_locks(inp); 5786171440Srrs#endif 5787169378Srrs return; 5788163953Srrs} 5789163953Srrs 5790185694Srrs#if 0 5791185694Srrsstatic void 5792185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5793185694Srrs{ 5794185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5795234995Stuexen SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5796185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5797234995Stuexen SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5798185694Srrs } 5799185694Srrs} 5800163953Srrs 5801185694Srrs#endif 5802185694Srrs 5803221249Stuexen#ifdef INET 5804163953Srrsvoid 5805189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5806163953Srrs{ 5807165647Srrs struct mbuf *m; 5808163953Srrs int iphlen; 5809170181Srrs uint32_t vrf_id = 0; 5810163953Srrs uint8_t ecn_bits; 5811163953Srrs struct ip *ip; 5812163953Srrs struct sctphdr *sh; 5813163953Srrs struct sctp_inpcb *inp = NULL; 5814163953Srrs struct sctp_nets *net; 5815163953Srrs struct sctp_tcb *stcb = NULL; 5816163953Srrs struct sctp_chunkhdr *ch; 5817163953Srrs int refcount_up = 0; 5818163953Srrs int length, mlen, offset; 5819163953Srrs 5820211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5821211969Stuexen uint32_t check, calc_check; 5822211969Stuexen 5823211969Stuexen#endif 5824211969Stuexen 5825169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5826169352Srrs SCTP_RELEASE_PKT(i_pak); 5827169352Srrs return; 5828169352Srrs } 5829168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5830163953Srrs iphlen = off; 5831165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5832170056Srrs 5833163953Srrs net = NULL; 5834163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5835163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5836163953Srrs 5837170091Srrs 5838163953Srrs#ifdef SCTP_MBUF_LOGGING 5839163953Srrs /* Log in any input mbufs */ 5840179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5841234461Stuexen struct mbuf *mat; 5842234461Stuexen 5843228907Stuexen for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 5844170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5845170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5846170744Srrs } 5847163953Srrs } 5848163953Srrs } 5849163953Srrs#endif 5850170091Srrs#ifdef SCTP_PACKET_LOGGING 5851179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5852170921Srrs sctp_packet_log(m, mlen); 5853170091Srrs#endif 5854170091Srrs /* 5855170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5856170091Srrs * case) 5857170091Srrs */ 5858170091Srrs mlen -= iphlen; 5859168124Srrs 5860163953Srrs /* 5861163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5862163953Srrs */ 5863163953Srrs ip = mtod(m, struct ip *); 5864163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5865165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5866163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5867163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5868163953Srrs return; 5869163953Srrs } 5870163953Srrs ip = mtod(m, struct ip *); 5871163953Srrs } 5872188067Srrs /* validate mbuf chain length with IP payload length */ 5873188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5874188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5875188067Srrs goto bad; 5876188067Srrs } 5877163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5878163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5879170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5880170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5881163953Srrs 5882163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5883163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5884163953Srrs goto bad; 5885163953Srrs } 5886168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5887163953Srrs /* 5888163953Srrs * We only look at broadcast if its a front state, All 5889163953Srrs * others we will not have a tcb for anyway. 5890163953Srrs */ 5891163953Srrs goto bad; 5892163953Srrs } 5893163953Srrs /* validate SCTP checksum */ 5894188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5895188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5896188067Srrs m->m_pkthdr.len, 5897188067Srrs if_name(m->m_pkthdr.rcvif), 5898188067Srrs m->m_pkthdr.csum_flags); 5899211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5900211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5901211969Stuexen#else 5902188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5903188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5904188067Srrs goto sctp_skip_csum_4; 5905188067Srrs } 5906171990Srrs check = sh->checksum; /* save incoming checksum */ 5907171990Srrs sh->checksum = 0; /* prepare for calc */ 5908188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5909203503Stuexen sh->checksum = check; 5910188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5911171990Srrs if (calc_check != check) { 5912171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5913171990Srrs calc_check, check, m, mlen, iphlen); 5914163953Srrs 5915228653Stuexen stcb = sctp_findassociation_addr(m, 5916171990Srrs offset - sizeof(*ch), 5917171990Srrs sh, ch, &inp, &net, 5918171990Srrs vrf_id); 5919179157Srrs if ((net) && (port)) { 5920179157Srrs if (net->port == 0) { 5921228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5922179157Srrs } 5923179157Srrs net->port = port; 5924179157Srrs } 5925218400Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5926218400Stuexen net->flowid = m->m_pkthdr.flowid; 5927218400Stuexen#ifdef INVARIANTS 5928218400Stuexen net->flowidset = 1; 5929218400Stuexen#endif 5930218400Stuexen } 5931171990Srrs if ((inp) && (stcb)) { 5932171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5933172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5934171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5935171990Srrs refcount_up = 1; 5936163953Srrs } 5937171990Srrs SCTP_STAT_INCR(sctps_badsum); 5938171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5939171990Srrs goto bad; 5940168299Srrs } 5941163953Srrssctp_skip_csum_4: 5942211969Stuexen#endif 5943168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5944168124Srrs if (sh->dest_port == 0) { 5945168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5946168124Srrs goto bad; 5947168124Srrs } 5948163953Srrs /* 5949163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5950163953Srrs * IP/SCTP/first chunk header... 5951163953Srrs */ 5952228653Stuexen stcb = sctp_findassociation_addr(m, offset - sizeof(*ch), 5953168299Srrs sh, ch, &inp, &net, vrf_id); 5954179157Srrs if ((net) && (port)) { 5955179157Srrs if (net->port == 0) { 5956228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5957179157Srrs } 5958179157Srrs net->port = port; 5959179157Srrs } 5960218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5961218335Stuexen net->flowid = m->m_pkthdr.flowid; 5962218400Stuexen#ifdef INVARIANTS 5963218392Srrs net->flowidset = 1; 5964218400Stuexen#endif 5965218335Stuexen } 5966163953Srrs /* inp's ref-count increased && stcb locked */ 5967163953Srrs if (inp == NULL) { 5968163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5969163953Srrs 5970163953Srrs SCTP_STAT_INCR(sctps_noport); 5971163953Srrs#ifdef ICMP_BANDLIM 5972163953Srrs /* 5973163953Srrs * we use the bandwidth limiting to protect against sending 5974163953Srrs * too many ABORTS all at once. In this case these count the 5975163953Srrs * same as an ICMP message. 5976163953Srrs */ 5977163953Srrs if (badport_bandlim(0) < 0) 5978163953Srrs goto bad; 5979163953Srrs#endif /* ICMP_BANDLIM */ 5980169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5981169420Srrs "Sending a ABORT from packet entry!\n"); 5982163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5983163953Srrs /* 5984163953Srrs * we do a trick here to get the INIT tag, dig in 5985163953Srrs * and get the tag from the INIT and put it in the 5986163953Srrs * common header. 5987163953Srrs */ 5988163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5989163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5990163953Srrs (uint8_t *) & chunk_buf); 5991163953Srrs if (init_chk != NULL) 5992163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5993163953Srrs } 5994165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5995228653Stuexen sctp_send_shutdown_complete2(m, sh, vrf_id, port); 5996165220Srrs goto bad; 5997165220Srrs } 5998165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5999165220Srrs goto bad; 6000165220Srrs } 6001229805Stuexen if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 6002229805Stuexen if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 6003229805Stuexen ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 6004229805Stuexen (ch->chunk_type != SCTP_INIT))) { 6005229805Stuexen sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 6006229805Stuexen } 6007229805Stuexen } 6008163953Srrs goto bad; 6009163953Srrs } else if (stcb == NULL) { 6010163953Srrs refcount_up = 1; 6011163953Srrs } 6012171167Sgnn#ifdef IPSEC 6013163953Srrs /* 6014163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 6015163953Srrs * idea, so I will leave it in place. 6016163953Srrs */ 6017163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 6018197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 6019163953Srrs SCTP_STAT_INCR(sctps_hdrops); 6020163953Srrs goto bad; 6021163953Srrs } 6022163953Srrs#endif /* IPSEC */ 6023163953Srrs 6024163953Srrs /* 6025163953Srrs * common chunk processing 6026163953Srrs */ 6027163953Srrs length = ip->ip_len + iphlen; 6028163953Srrs offset -= sizeof(struct sctp_chunkhdr); 6029163953Srrs 6030163953Srrs ecn_bits = ip->ip_tos; 6031163953Srrs 6032169655Srrs /* sa_ignore NO_NULL_CHK */ 6033163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 6034179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 6035163953Srrs /* inp's ref-count reduced && stcb unlocked */ 6036163953Srrs if (m) { 6037163953Srrs sctp_m_freem(m); 6038163953Srrs } 6039163953Srrs if ((inp) && (refcount_up)) { 6040163953Srrs /* reduce ref-count */ 6041163953Srrs SCTP_INP_DECR_REF(inp); 6042163953Srrs } 6043163953Srrs return; 6044163953Srrsbad: 6045169420Srrs if (stcb) { 6046163953Srrs SCTP_TCB_UNLOCK(stcb); 6047169420Srrs } 6048163953Srrs if ((inp) && (refcount_up)) { 6049163953Srrs /* reduce ref-count */ 6050163953Srrs SCTP_INP_DECR_REF(inp); 6051163953Srrs } 6052163953Srrs if (m) { 6053163953Srrs sctp_m_freem(m); 6054163953Srrs } 6055163953Srrs return; 6056163953Srrs} 6057218211Srrs 6058218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6059218269Srrsextern int *sctp_cpuarry; 6060218211Srrs 6061218269Srrs#endif 6062218269Srrs 6063179157Srrsvoid 6064218211Srrssctp_input(struct mbuf *m, int off) 6065179157Srrs{ 6066218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6067218211Srrs struct ip *ip; 6068218211Srrs struct sctphdr *sh; 6069218211Srrs int offset; 6070218211Srrs int cpu_to_use; 6071218371Srrs uint32_t flowid, tag; 6072218211Srrs 6073218269Srrs if (mp_ncpus > 1) { 6074218371Srrs if (m->m_flags & M_FLOWID) { 6075218371Srrs flowid = m->m_pkthdr.flowid; 6076218269Srrs } else { 6077218269Srrs /* 6078218371Srrs * No flow id built by lower layers fix it so we 6079218371Srrs * create one. 6080218269Srrs */ 6081218371Srrs ip = mtod(m, struct ip *); 6082218371Srrs offset = off + sizeof(*sh); 6083218371Srrs if (SCTP_BUF_LEN(m) < offset) { 6084218371Srrs if ((m = m_pullup(m, offset)) == 0) { 6085218371Srrs SCTP_STAT_INCR(sctps_hdrops); 6086218371Srrs return; 6087218371Srrs } 6088218371Srrs ip = mtod(m, struct ip *); 6089218371Srrs } 6090218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 6091218371Srrs tag = htonl(sh->v_tag); 6092218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6093218371Srrs m->m_pkthdr.flowid = flowid; 6094218371Srrs m->m_flags |= M_FLOWID; 6095218269Srrs } 6096218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6097218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 6098218211Srrs return; 6099218211Srrs } 6100218211Srrs#endif 6101218211Srrs sctp_input_with_port(m, off, 0); 6102179157Srrs} 6103221249Stuexen 6104221249Stuexen#endif 6105