sctp_input.c revision 230379
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 230379 2012-01-20 13:26:11Z 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); 303216822Stuexen sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 304216822Stuexen SCTP_NOTIFY_DATAGRAM_UNSENT, 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, 321163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 322172090Srrs sp, SCTP_SO_NOT_LOCKED); 323163953Srrs if (sp->data) { 324163953Srrs sctp_m_freem(sp->data); 325163953Srrs sp->data = NULL; 326163953Srrs } 327212712Stuexen if (sp->net) { 328212712Stuexen sctp_free_remote_addr(sp->net); 329212712Stuexen sp->net = NULL; 330212712Stuexen } 331163953Srrs /* Free the chunk */ 332221627Stuexen sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 333169655Srrs /* sa_ignore FREED_MEMORY */ 334163953Srrs } 335163953Srrs } 336163953Srrs } 337196260Stuexen /* cut back the count */ 338163953Srrs asoc->pre_open_streams = newcnt; 339163953Srrs } 340164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 341188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 342218129Srrs 343185694Srrs /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 344185694Srrs asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 345179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 346170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 347170744Srrs } 348163953Srrs /* This is the next one we expect */ 349163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 350163953Srrs 351163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 352180955Srrs asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 353218129Srrs 354163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 355163953Srrs /* open the requested streams */ 356170091Srrs 357163953Srrs if (asoc->strmin != NULL) { 358163953Srrs /* Free the old ones */ 359216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 360164181Srrs 361164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 362216822Stuexen TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 363164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 364164181Srrs sctp_free_remote_addr(ctl->whoFrom); 365171158Srrs ctl->whoFrom = NULL; 366164181Srrs sctp_m_freem(ctl->data); 367164181Srrs ctl->data = NULL; 368164181Srrs sctp_free_a_readq(stcb, ctl); 369164181Srrs } 370164181Srrs } 371170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 372163953Srrs } 373164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 374164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 375164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 376164181Srrs } 377163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 378170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 379163953Srrs if (asoc->strmin == NULL) { 380163953Srrs /* we didn't get memory for the streams! */ 381169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 382163953Srrs return (-1); 383163953Srrs } 384163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 385163953Srrs asoc->strmin[i].stream_no = i; 386163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 387163953Srrs /* 388163953Srrs * U-stream ranges will be set when the cookie is unpacked. 389163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 390163953Srrs * supported) when the INIT-ACK arrives. 391163953Srrs */ 392163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 393168943Srrs asoc->strmin[i].delivery_started = 0; 394163953Srrs } 395163953Srrs /* 396163953Srrs * load_address_from_init will put the addresses into the 397163953Srrs * association when the COOKIE is processed or the INIT-ACK is 398163953Srrs * processed. Both types of COOKIE's existing and new call this 399163953Srrs * routine. It will remove addresses that are no longer in the 400163953Srrs * association (for the restarting case where addresses are 401163953Srrs * removed). Up front when the INIT arrives we will discard it if it 402163953Srrs * is a restart and new addresses have been added. 403163953Srrs */ 404169655Srrs /* sa_ignore MEMLEAK */ 405163953Srrs return (0); 406163953Srrs} 407163953Srrs 408163953Srrs/* 409163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 410163953Srrs */ 411163953Srrsstatic int 412163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 413163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 414170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 415163953Srrs{ 416163953Srrs struct sctp_association *asoc; 417163953Srrs struct mbuf *op_err; 418163953Srrs int retval, abort_flag; 419163953Srrs uint32_t initack_limit; 420185694Srrs int nat_friendly = 0; 421163953Srrs 422163953Srrs /* First verify that we have no illegal param's */ 423163953Srrs abort_flag = 0; 424163953Srrs op_err = NULL; 425163953Srrs 426163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 427163953Srrs (offset + sizeof(struct sctp_init_chunk)), 428185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 429163953Srrs if (abort_flag) { 430163953Srrs /* Send an abort and notify peer */ 431172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 432170056Srrs *abort_no_unlock = 1; 433163953Srrs return (-1); 434163953Srrs } 435163953Srrs asoc = &stcb->asoc; 436185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 437163953Srrs /* process the peer's parameters in the INIT-ACK */ 438228653Stuexen retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); 439163953Srrs if (retval < 0) { 440163953Srrs return (retval); 441163953Srrs } 442163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 443163953Srrs /* load all addresses */ 444228653Stuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 445163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 446163953Srrs NULL))) { 447163953Srrs /* Huh, we should abort */ 448169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 449169420Srrs "Load addresses from INIT causes an abort %d\n", 450169420Srrs retval); 451163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 452179157Srrs NULL, 0, net->port); 453168299Srrs *abort_no_unlock = 1; 454163953Srrs return (-1); 455163953Srrs } 456171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 457171477Srrs if (asoc->peer_supports_asconf == 0) { 458216822Stuexen struct sctp_asconf_addr *param, *nparam; 459171477Srrs 460216822Stuexen TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 461216822Stuexen TAILQ_REMOVE(&asoc->asconf_queue, param, next); 462216822Stuexen SCTP_FREE(param, SCTP_M_ASC_ADDR); 463171477Srrs } 464171477Srrs } 465163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 466163953Srrs stcb->asoc.local_hmacs); 467163953Srrs if (op_err) { 468163953Srrs sctp_queue_op_err(stcb, op_err); 469163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 470163953Srrs op_err = NULL; 471163953Srrs } 472163953Srrs /* extract the cookie and queue it to "echo" it back... */ 473179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 474171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 475171943Srrs stcb->asoc.overall_error_count, 476171943Srrs 0, 477171943Srrs SCTP_FROM_SCTP_INPUT, 478171943Srrs __LINE__); 479171943Srrs } 480163953Srrs stcb->asoc.overall_error_count = 0; 481163953Srrs net->error_count = 0; 482163953Srrs 483163953Srrs /* 484163953Srrs * Cancel the INIT timer, We do this first before queueing the 485163953Srrs * cookie. We always cancel at the primary to assue that we are 486163953Srrs * canceling the timer started by the INIT which always goes to the 487163953Srrs * primary. 488163953Srrs */ 489163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 490165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 491163953Srrs 492165220Srrs /* calculate the RTO */ 493218186Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 494219397Srrs SCTP_RTT_FROM_NON_DATA); 495165220Srrs 496163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 497163953Srrs if (retval < 0) { 498163953Srrs /* 499163953Srrs * No cookie, we probably should send a op error. But in any 500163953Srrs * case if there is no cookie in the INIT-ACK, we can 501163953Srrs * abandon the peer, its broke. 502163953Srrs */ 503163953Srrs if (retval == -3) { 504163953Srrs /* We abort with an error of missing mandatory param */ 505163953Srrs op_err = 506163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 507163953Srrs if (op_err) { 508163953Srrs /* 509163953Srrs * Expand beyond to include the mandatory 510163953Srrs * param cookie 511163953Srrs */ 512163953Srrs struct sctp_inv_mandatory_param *mp; 513163953Srrs 514165647Srrs SCTP_BUF_LEN(op_err) = 515163953Srrs sizeof(struct sctp_inv_mandatory_param); 516163953Srrs mp = mtod(op_err, 517163953Srrs struct sctp_inv_mandatory_param *); 518163953Srrs /* Subtract the reserved param */ 519163953Srrs mp->length = 520163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 521163953Srrs mp->num_param = htonl(1); 522163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 523163953Srrs mp->resv = 0; 524163953Srrs } 525163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 526228653Stuexen sh, op_err, vrf_id, net->port); 527168299Srrs *abort_no_unlock = 1; 528163953Srrs } 529163953Srrs return (retval); 530163953Srrs } 531163953Srrs return (0); 532163953Srrs} 533163953Srrs 534163953Srrsstatic void 535163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 536163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 537163953Srrs{ 538163953Srrs struct sockaddr_storage store; 539212225Srrs struct sctp_nets *r_net, *f_net; 540163953Srrs struct timeval tv; 541172396Srrs int req_prim = 0; 542224641Stuexen uint16_t old_error_counter; 543163953Srrs 544221249Stuexen#ifdef INET 545221249Stuexen struct sockaddr_in *sin; 546221249Stuexen 547221249Stuexen#endif 548221249Stuexen#ifdef INET6 549221249Stuexen struct sockaddr_in6 *sin6; 550221249Stuexen 551221249Stuexen#endif 552221249Stuexen 553163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 554163953Srrs /* Invalid length */ 555163953Srrs return; 556163953Srrs } 557163953Srrs memset(&store, 0, sizeof(store)); 558221249Stuexen switch (cp->heartbeat.hb_info.addr_family) { 559221249Stuexen#ifdef INET 560221249Stuexen case AF_INET: 561221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 562221249Stuexen sin = (struct sockaddr_in *)&store; 563221249Stuexen sin->sin_family = cp->heartbeat.hb_info.addr_family; 564221249Stuexen sin->sin_len = cp->heartbeat.hb_info.addr_len; 565221249Stuexen sin->sin_port = stcb->rport; 566221249Stuexen memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 567221249Stuexen sizeof(sin->sin_addr)); 568221249Stuexen } else { 569221249Stuexen return; 570221249Stuexen } 571221249Stuexen break; 572221249Stuexen#endif 573221249Stuexen#ifdef INET6 574221249Stuexen case AF_INET6: 575221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 576221249Stuexen sin6 = (struct sockaddr_in6 *)&store; 577221249Stuexen sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 578221249Stuexen sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 579221249Stuexen sin6->sin6_port = stcb->rport; 580221249Stuexen memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 581221249Stuexen sizeof(sin6->sin6_addr)); 582221249Stuexen } else { 583221249Stuexen return; 584221249Stuexen } 585221249Stuexen break; 586221249Stuexen#endif 587221249Stuexen default: 588163953Srrs return; 589163953Srrs } 590221249Stuexen r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 591163953Srrs if (r_net == NULL) { 592169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 593163953Srrs return; 594163953Srrs } 595163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 596163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 597163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 598163953Srrs /* 599163953Srrs * If the its a HB and it's random value is correct when can 600163953Srrs * confirm the destination. 601163953Srrs */ 602163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 603167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 604167598Srrs stcb->asoc.primary_destination = r_net; 605167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 606212225Srrs f_net = TAILQ_FIRST(&stcb->asoc.nets); 607212225Srrs if (f_net != r_net) { 608167598Srrs /* 609167598Srrs * first one on the list is NOT the primary 610167598Srrs * sctp_cmpaddr() is much more efficent if 611167598Srrs * the primary is the first on the list, 612167598Srrs * make it so. 613167598Srrs */ 614212225Srrs TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 615212225Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 616167598Srrs } 617172396Srrs req_prim = 1; 618167598Srrs } 619163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 620172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 621224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 622224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 623163953Srrs } 624224641Stuexen old_error_counter = r_net->error_count; 625163953Srrs r_net->error_count = 0; 626163953Srrs r_net->hb_responded = 1; 627163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 628163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 629224641Stuexen /* Now lets do a RTO with this */ 630224641Stuexen r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 631224641Stuexen SCTP_RTT_FROM_NON_DATA); 632224641Stuexen if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 633163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 634163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 635172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 636163953Srrs } 637224641Stuexen if (r_net->dest_state & SCTP_ADDR_PF) { 638224641Stuexen r_net->dest_state &= ~SCTP_ADDR_PF; 639224641Stuexen stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 640224641Stuexen } 641224641Stuexen if (old_error_counter > 0) { 642224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 643224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 644224641Stuexen } 645224641Stuexen if (r_net == stcb->asoc.primary_destination) { 646224641Stuexen if (stcb->asoc.alternate) { 647224641Stuexen /* release the alternate, primary is good */ 648224641Stuexen sctp_free_remote_addr(stcb->asoc.alternate); 649224641Stuexen stcb->asoc.alternate = NULL; 650171440Srrs } 651171440Srrs } 652172396Srrs /* Mobility adaptation */ 653172396Srrs if (req_prim) { 654172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 655172396Srrs SCTP_MOBILITY_BASE) || 656172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 657172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 658172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 659172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 660172396Srrs 661172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 662172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 663172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 664172396Srrs sctp_assoc_immediate_retrans(stcb, 665172396Srrs stcb->asoc.primary_destination); 666172396Srrs } 667172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 668172396Srrs SCTP_MOBILITY_BASE)) { 669212712Stuexen sctp_move_chunks_from_net(stcb, 670212712Stuexen stcb->asoc.deleted_primary); 671172396Srrs } 672172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 673172396Srrs stcb->asoc.deleted_primary); 674172396Srrs } 675172396Srrs } 676163953Srrs} 677163953Srrs 678185694Srrsstatic int 679185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 680185694Srrs{ 681185694Srrs /* 682185694Srrs * return 0 means we want you to proceed with the abort non-zero 683185694Srrs * means no abort processing 684185694Srrs */ 685185694Srrs struct sctpasochead *head; 686185694Srrs 687185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 688185694Srrs /* generate a new vtag and send init */ 689185694Srrs LIST_REMOVE(stcb, sctp_asocs); 690185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 691185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 692185694Srrs /* 693185694Srrs * put it in the bucket in the vtag hash of assoc's for the 694185694Srrs * system 695185694Srrs */ 696185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 697185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 698185694Srrs return (1); 699185694Srrs } 700185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 701185694Srrs /* 702185694Srrs * treat like a case where the cookie expired i.e.: - dump 703185694Srrs * current cookie. - generate a new vtag. - resend init. 704185694Srrs */ 705185694Srrs /* generate a new vtag and send init */ 706185694Srrs LIST_REMOVE(stcb, sctp_asocs); 707185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 708185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 709185694Srrs sctp_stop_all_cookie_timers(stcb); 710185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 711185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 712185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 713185694Srrs /* 714185694Srrs * put it in the bucket in the vtag hash of assoc's for the 715185694Srrs * system 716185694Srrs */ 717185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 718185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 719185694Srrs return (1); 720185694Srrs } 721185694Srrs return (0); 722185694Srrs} 723185694Srrs 724185694Srrsstatic int 725185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 726185694Srrs struct sctp_nets *net) 727185694Srrs{ 728185694Srrs /* 729185694Srrs * return 0 means we want you to proceed with the abort non-zero 730185694Srrs * means no abort processing 731185694Srrs */ 732185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 733185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 734185694Srrs return (0); 735185694Srrs } 736185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 737185694Srrs return (1); 738185694Srrs} 739185694Srrs 740185694Srrs 741163953Srrsstatic void 742163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 743163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 744163953Srrs{ 745172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 746172090Srrs struct socket *so; 747172090Srrs 748172090Srrs#endif 749185694Srrs uint16_t len; 750172090Srrs 751169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 752163953Srrs if (stcb == NULL) 753163953Srrs return; 754163953Srrs 755185694Srrs len = ntohs(cp->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_abort_chunk *cpnext; 762185694Srrs struct sctp_missing_nat_state *natc; 763185694Srrs uint16_t cause; 764185694Srrs 765185694Srrs cpnext = cp; 766185694Srrs cpnext++; 767185694Srrs natc = (struct sctp_missing_nat_state *)cpnext; 768185694Srrs cause = ntohs(natc->cause); 769185694Srrs if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 770185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 771185694Srrs cp->ch.chunk_flags); 772185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 773185694Srrs return; 774185694Srrs } 775185694Srrs } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 776185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 777185694Srrs cp->ch.chunk_flags); 778185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 779185694Srrs return; 780185694Srrs } 781185694Srrs } 782185694Srrs } 783163953Srrs /* stop any receive timers */ 784171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 785163953Srrs /* notify user of the abort and clean up... */ 786172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 787163953Srrs /* free the tcb */ 788172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 789172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 790172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 791172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 792172396Srrs panic("Received an ABORT"); 793172396Srrs } else { 794172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 795172396Srrs } 796172396Srrs#endif 797163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 798163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 799163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 800163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 801163953Srrs } 802168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 803168859Srrs sctp_print_out_track_log(stcb); 804168859Srrs#endif 805172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 806172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 807172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 808172090Srrs SCTP_TCB_UNLOCK(stcb); 809172090Srrs SCTP_SOCKET_LOCK(so, 1); 810172090Srrs SCTP_TCB_LOCK(stcb); 811172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 812172090Srrs#endif 813171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 814171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 815171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 816172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 817172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 818172090Srrs#endif 819169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 820163953Srrs} 821163953Srrs 822163953Srrsstatic void 823224641Stuexensctp_start_net_timers(struct sctp_tcb *stcb) 824224641Stuexen{ 825224641Stuexen uint32_t cnt_hb_sent; 826224641Stuexen struct sctp_nets *net; 827224641Stuexen 828224641Stuexen cnt_hb_sent = 0; 829224641Stuexen TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 830224641Stuexen /* 831224641Stuexen * For each network start: 1) A pmtu timer. 2) A HB timer 3) 832224641Stuexen * If the dest in unconfirmed send a hb as well if under 833224641Stuexen * max_hb_burst have been sent. 834224641Stuexen */ 835224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 836224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 837224641Stuexen if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 838224641Stuexen (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 839224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 840224641Stuexen cnt_hb_sent++; 841224641Stuexen } 842224641Stuexen } 843224641Stuexen if (cnt_hb_sent) { 844224641Stuexen sctp_chunk_output(stcb->sctp_ep, stcb, 845224641Stuexen SCTP_OUTPUT_FROM_COOKIE_ACK, 846224641Stuexen SCTP_SO_NOT_LOCKED); 847224641Stuexen } 848224641Stuexen} 849224641Stuexen 850224641Stuexen 851224641Stuexenstatic void 852163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 853163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 854163953Srrs{ 855163953Srrs struct sctp_association *asoc; 856163953Srrs int some_on_streamwheel; 857163953Srrs 858172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 859172090Srrs struct socket *so; 860172090Srrs 861172090Srrs#endif 862172090Srrs 863169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 864169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 865163953Srrs if (stcb == NULL) 866163953Srrs return; 867165220Srrs asoc = &stcb->asoc; 868165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 869165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 870163953Srrs return; 871163953Srrs } 872163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 873163953Srrs /* Shutdown NOT the expected size */ 874163953Srrs return; 875163953Srrs } else { 876228653Stuexen sctp_update_acked(stcb, cp, abort_flag); 877199477Stuexen if (*abort_flag) { 878199477Stuexen return; 879199477Stuexen } 880163953Srrs } 881165220Srrs if (asoc->control_pdapi) { 882163953Srrs /* 883163953Srrs * With a normal shutdown we assume the end of last record. 884163953Srrs */ 885163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 886165220Srrs asoc->control_pdapi->end_added = 1; 887165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 888165220Srrs asoc->control_pdapi = NULL; 889163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 890172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 891172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 892172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 893172090Srrs SCTP_TCB_UNLOCK(stcb); 894172090Srrs SCTP_SOCKET_LOCK(so, 1); 895172090Srrs SCTP_TCB_LOCK(stcb); 896172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 897172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 898172090Srrs /* assoc was freed while we were unlocked */ 899172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 900172090Srrs return; 901172090Srrs } 902172090Srrs#endif 903163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 904172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 905172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 906172090Srrs#endif 907163953Srrs } 908163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 909163953Srrs if (stcb->sctp_socket) { 910163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 911170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 912163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 913171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 914172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 915163953Srrs /* 916163953Srrs * notify upper layer that peer has initiated a 917163953Srrs * shutdown 918163953Srrs */ 919172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 920163953Srrs 921163953Srrs /* reset time */ 922169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 923163953Srrs } 924163953Srrs } 925163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 926163953Srrs /* 927163953Srrs * stop the shutdown timer, since we WILL move to 928163953Srrs * SHUTDOWN-ACK-SENT. 929163953Srrs */ 930171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 931163953Srrs } 932171158Srrs /* Now is there unsent data on a stream somewhere? */ 933221627Stuexen some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 934163953Srrs 935163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 936163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 937163953Srrs some_on_streamwheel) { 938163953Srrs /* By returning we will push more data out */ 939163953Srrs return; 940163953Srrs } else { 941163953Srrs /* no outstanding data to send, so move on... */ 942163953Srrs /* send SHUTDOWN-ACK */ 943224641Stuexen sctp_send_shutdown_ack(stcb, net); 944163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 945166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 946166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 947163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 948163953Srrs } 949171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 950172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 951214918Stuexen sctp_stop_timers_for_shutdown(stcb); 952163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 953163953Srrs stcb, net); 954163953Srrs } 955163953Srrs} 956163953Srrs 957163953Srrsstatic void 958228653Stuexensctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 959204141Stuexen struct sctp_tcb *stcb, 960204141Stuexen struct sctp_nets *net) 961163953Srrs{ 962163953Srrs struct sctp_association *asoc; 963163953Srrs 964172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 965172090Srrs struct socket *so; 966172090Srrs 967172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 968172090Srrs#endif 969169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 970169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 971163953Srrs if (stcb == NULL) 972163953Srrs return; 973163953Srrs 974163953Srrs asoc = &stcb->asoc; 975163953Srrs /* process according to association state */ 976204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 977204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 978204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 979204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 980204141Stuexen SCTP_TCB_UNLOCK(stcb); 981204141Stuexen return; 982204141Stuexen } 983163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 984163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 985163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 986163953Srrs SCTP_TCB_UNLOCK(stcb); 987163953Srrs return; 988163953Srrs } 989165220Srrs if (asoc->control_pdapi) { 990163953Srrs /* 991163953Srrs * With a normal shutdown we assume the end of last record. 992163953Srrs */ 993163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 994165220Srrs asoc->control_pdapi->end_added = 1; 995165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 996165220Srrs asoc->control_pdapi = NULL; 997163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 998172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 999172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1000172090Srrs SCTP_TCB_UNLOCK(stcb); 1001172090Srrs SCTP_SOCKET_LOCK(so, 1); 1002172090Srrs SCTP_TCB_LOCK(stcb); 1003172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1004172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1005172090Srrs /* assoc was freed while we were unlocked */ 1006172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1007172090Srrs return; 1008172090Srrs } 1009172090Srrs#endif 1010163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1011172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1012172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1013172090Srrs#endif 1014163953Srrs } 1015163953Srrs /* are the queues empty? */ 1016163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 1017163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 1018217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1019172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 1020163953Srrs } 1021163953Srrs /* stop the timer */ 1022171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1023163953Srrs /* send SHUTDOWN-COMPLETE */ 1024204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 1025163953Srrs /* notify upper layer protocol */ 1026163953Srrs if (stcb->sctp_socket) { 1027163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1028163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1029229774Stuexen stcb->sctp_socket->so_snd.sb_cc = 0; 1030163953Srrs } 1031230379Stuexen sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1032163953Srrs } 1033163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1034163953Srrs /* free the TCB but first save off the ep */ 1035172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1036172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1037172090Srrs SCTP_TCB_UNLOCK(stcb); 1038172090Srrs SCTP_SOCKET_LOCK(so, 1); 1039172090Srrs SCTP_TCB_LOCK(stcb); 1040172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1041172090Srrs#endif 1042171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1043171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1044172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1045172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1046172090Srrs#endif 1047163953Srrs} 1048163953Srrs 1049163953Srrs/* 1050163953Srrs * Skip past the param header and then we will find the chunk that caused the 1051163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1052163953Srrs * our peer must be broken. 1053163953Srrs */ 1054163953Srrsstatic void 1055163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1056163953Srrs struct sctp_nets *net) 1057163953Srrs{ 1058163953Srrs struct sctp_chunkhdr *chk; 1059163953Srrs 1060163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1061163953Srrs switch (chk->chunk_type) { 1062163953Srrs case SCTP_ASCONF_ACK: 1063163953Srrs case SCTP_ASCONF: 1064163953Srrs sctp_asconf_cleanup(stcb, net); 1065163953Srrs break; 1066163953Srrs case SCTP_FORWARD_CUM_TSN: 1067163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1068163953Srrs break; 1069163953Srrs default: 1070169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1071169420Srrs "Peer does not support chunk type %d(%x)??\n", 1072169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1073163953Srrs break; 1074163953Srrs } 1075163953Srrs} 1076163953Srrs 1077163953Srrs/* 1078163953Srrs * Skip past the param header and then we will find the param that caused the 1079163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1080163953Srrs * these will turn of specific features. 1081163953Srrs */ 1082163953Srrsstatic void 1083163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1084163953Srrs{ 1085163953Srrs struct sctp_paramhdr *pbad; 1086163953Srrs 1087163953Srrs pbad = phdr + 1; 1088163953Srrs switch (ntohs(pbad->param_type)) { 1089163953Srrs /* pr-sctp draft */ 1090163953Srrs case SCTP_PRSCTP_SUPPORTED: 1091163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1092163953Srrs break; 1093163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1094163953Srrs break; 1095163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1096185694Srrs case SCTP_HAS_NAT_SUPPORT: 1097185694Srrs stcb->asoc.peer_supports_nat = 0; 1098185694Srrs break; 1099163953Srrs case SCTP_ADD_IP_ADDRESS: 1100163953Srrs case SCTP_DEL_IP_ADDRESS: 1101163953Srrs case SCTP_SET_PRIM_ADDR: 1102163953Srrs stcb->asoc.peer_supports_asconf = 0; 1103163953Srrs break; 1104163953Srrs case SCTP_SUCCESS_REPORT: 1105163953Srrs case SCTP_ERROR_CAUSE_IND: 1106169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1107169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1108169420Srrs "Turning off ASCONF to this strange peer\n"); 1109163953Srrs stcb->asoc.peer_supports_asconf = 0; 1110163953Srrs break; 1111163953Srrs default: 1112169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1113169420Srrs "Peer does not support param type %d(%x)??\n", 1114169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1115163953Srrs break; 1116163953Srrs } 1117163953Srrs} 1118163953Srrs 1119163953Srrsstatic int 1120163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1121163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1122163953Srrs{ 1123163953Srrs int chklen; 1124163953Srrs struct sctp_paramhdr *phdr; 1125163953Srrs uint16_t error_type; 1126163953Srrs uint16_t error_len; 1127163953Srrs struct sctp_association *asoc; 1128163953Srrs int adjust; 1129163953Srrs 1130172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1131172090Srrs struct socket *so; 1132172090Srrs 1133172090Srrs#endif 1134172090Srrs 1135163953Srrs /* parse through all of the errors and process */ 1136163953Srrs asoc = &stcb->asoc; 1137163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1138163953Srrs sizeof(struct sctp_chunkhdr)); 1139163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1140163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1141163953Srrs /* Process an Error Cause */ 1142163953Srrs error_type = ntohs(phdr->param_type); 1143163953Srrs error_len = ntohs(phdr->param_length); 1144163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1145163953Srrs /* invalid param length for this param */ 1146169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1147169420Srrs chklen, error_len); 1148163953Srrs return (0); 1149163953Srrs } 1150163953Srrs switch (error_type) { 1151163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1152163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1153163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1154163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1155169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1156169420Srrs error_type); 1157163953Srrs break; 1158185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1159185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1160185694Srrs ch->chunk_flags); 1161185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1162185694Srrs return (0); 1163185694Srrs } 1164185694Srrs break; 1165185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1166185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1167185694Srrs ch->chunk_flags); 1168185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1169185694Srrs return (0); 1170185694Srrs } 1171185694Srrs break; 1172163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1173163953Srrs /* 1174163953Srrs * We only act if we have echoed a cookie and are 1175163953Srrs * waiting. 1176163953Srrs */ 1177163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1178163953Srrs int *p; 1179163953Srrs 1180163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1181163953Srrs /* Save the time doubled */ 1182163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1183163953Srrs asoc->stale_cookie_count++; 1184163953Srrs if (asoc->stale_cookie_count > 1185163953Srrs asoc->max_init_times) { 1186172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1187163953Srrs /* now free the asoc */ 1188172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1189172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1190172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1191172090Srrs SCTP_TCB_UNLOCK(stcb); 1192172090Srrs SCTP_SOCKET_LOCK(so, 1); 1193172090Srrs SCTP_TCB_LOCK(stcb); 1194172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1195172090Srrs#endif 1196171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1197171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1198172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1199172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1200172090Srrs#endif 1201163953Srrs return (-1); 1202163953Srrs } 1203163953Srrs /* blast back to INIT state */ 1204185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1205163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1206163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1207163953Srrs sctp_stop_all_cookie_timers(stcb); 1208172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1209163953Srrs } 1210163953Srrs break; 1211163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1212163953Srrs /* 1213163953Srrs * Nothing we can do here, we don't do hostname 1214163953Srrs * addresses so if the peer does not like my IPv6 1215163953Srrs * (or IPv4 for that matter) it does not matter. If 1216163953Srrs * they don't support that type of address, they can 1217163953Srrs * NOT possibly get that packet type... i.e. with no 1218163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1219163953Srrs * safely ignore this one. If we ever added support 1220163953Srrs * for HOSTNAME Addresses, then we would need to do 1221163953Srrs * something here. 1222163953Srrs */ 1223163953Srrs break; 1224163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1225163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1226163953Srrs break; 1227163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1228163953Srrs sctp_process_unrecog_param(stcb, phdr); 1229163953Srrs break; 1230163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1231163953Srrs /* 1232163953Srrs * We ignore this since the timer will drive out a 1233163953Srrs * new cookie anyway and there timer will drive us 1234163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1235163953Srrs * here since we don't have their tag. 1236163953Srrs */ 1237163953Srrs break; 1238163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1239163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1240163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1241163953Srrs /* 1242163953Srrs * We should NOT get these here, but in a 1243166086Srrs * ASCONF-ACK. 1244163953Srrs */ 1245169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1246169420Srrs error_type); 1247163953Srrs break; 1248163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1249163953Srrs /* 1250163953Srrs * And what, pray tell do we do with the fact that 1251163953Srrs * the peer is out of resources? Not really sure we 1252166086Srrs * could do anything but abort. I suspect this 1253163953Srrs * should have came WITH an abort instead of in a 1254163953Srrs * OP-ERROR. 1255163953Srrs */ 1256163953Srrs break; 1257163953Srrs default: 1258169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1259169420Srrs error_type); 1260163953Srrs break; 1261163953Srrs } 1262163953Srrs adjust = SCTP_SIZE32(error_len); 1263163953Srrs chklen -= adjust; 1264163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1265163953Srrs } 1266163953Srrs return (0); 1267163953Srrs} 1268163953Srrs 1269163953Srrsstatic int 1270169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1271169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1272170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1273163953Srrs{ 1274163953Srrs struct sctp_init_ack *init_ack; 1275163953Srrs struct mbuf *op_err; 1276163953Srrs 1277169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1278169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1279169420Srrs 1280163953Srrs if (stcb == NULL) { 1281169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1282169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1283163953Srrs return (-1); 1284163953Srrs } 1285163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1286163953Srrs /* Invalid length */ 1287163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1288163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1289179157Srrs op_err, 0, net->port); 1290168299Srrs *abort_no_unlock = 1; 1291163953Srrs return (-1); 1292163953Srrs } 1293163953Srrs init_ack = &cp->init; 1294163953Srrs /* validate parameters */ 1295163953Srrs if (init_ack->initiate_tag == 0) { 1296163953Srrs /* protocol error... send an abort */ 1297163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1298163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1299179157Srrs op_err, 0, net->port); 1300168299Srrs *abort_no_unlock = 1; 1301163953Srrs return (-1); 1302163953Srrs } 1303163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1304163953Srrs /* protocol error... send an abort */ 1305163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1306163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1307179157Srrs op_err, 0, net->port); 1308168299Srrs *abort_no_unlock = 1; 1309163953Srrs return (-1); 1310163953Srrs } 1311163953Srrs if (init_ack->num_inbound_streams == 0) { 1312163953Srrs /* protocol error... send an abort */ 1313163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1314163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1315179157Srrs op_err, 0, net->port); 1316168299Srrs *abort_no_unlock = 1; 1317163953Srrs return (-1); 1318163953Srrs } 1319163953Srrs if (init_ack->num_outbound_streams == 0) { 1320163953Srrs /* protocol error... send an abort */ 1321163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1322163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1323179157Srrs op_err, 0, net->port); 1324168299Srrs *abort_no_unlock = 1; 1325163953Srrs return (-1); 1326163953Srrs } 1327163953Srrs /* process according to association state... */ 1328171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1329163953Srrs case SCTP_STATE_COOKIE_WAIT: 1330163953Srrs /* this is the expected state for this chunk */ 1331163953Srrs /* process the INIT-ACK parameters */ 1332163953Srrs if (stcb->asoc.primary_destination->dest_state & 1333163953Srrs SCTP_ADDR_UNCONFIRMED) { 1334163953Srrs /* 1335163953Srrs * The primary is where we sent the INIT, we can 1336163953Srrs * always consider it confirmed when the INIT-ACK is 1337163953Srrs * returned. Do this before we load addresses 1338163953Srrs * though. 1339163953Srrs */ 1340163953Srrs stcb->asoc.primary_destination->dest_state &= 1341163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1342163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1343172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1344163953Srrs } 1345169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1346170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1347163953Srrs /* error in parsing parameters */ 1348163953Srrs return (-1); 1349163953Srrs } 1350163953Srrs /* update our state */ 1351169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1352171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1353163953Srrs 1354163953Srrs /* reset the RTO calc */ 1355179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1356171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1357171943Srrs stcb->asoc.overall_error_count, 1358171943Srrs 0, 1359171943Srrs SCTP_FROM_SCTP_INPUT, 1360171943Srrs __LINE__); 1361171943Srrs } 1362163953Srrs stcb->asoc.overall_error_count = 0; 1363169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1364163953Srrs /* 1365163953Srrs * collapse the init timer back in case of a exponential 1366165220Srrs * backoff 1367163953Srrs */ 1368163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1369163953Srrs stcb, net); 1370163953Srrs /* 1371163953Srrs * the send at the end of the inbound data processing will 1372163953Srrs * cause the cookie to be sent 1373163953Srrs */ 1374163953Srrs break; 1375163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1376163953Srrs /* incorrect state... discard */ 1377163953Srrs break; 1378163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1379163953Srrs /* incorrect state... discard */ 1380163953Srrs break; 1381163953Srrs case SCTP_STATE_OPEN: 1382163953Srrs /* incorrect state... discard */ 1383163953Srrs break; 1384163953Srrs case SCTP_STATE_EMPTY: 1385163953Srrs case SCTP_STATE_INUSE: 1386163953Srrs default: 1387163953Srrs /* incorrect state... discard */ 1388163953Srrs return (-1); 1389163953Srrs break; 1390163953Srrs } 1391169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1392163953Srrs return (0); 1393163953Srrs} 1394163953Srrs 1395185694Srrsstatic struct sctp_tcb * 1396185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1397185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1398185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1399185694Srrs struct sockaddr *init_src, int *notification, 1400185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1401185694Srrs uint32_t vrf_id, uint16_t port); 1402163953Srrs 1403185694Srrs 1404163953Srrs/* 1405163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1406163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1407163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1408163953Srrs * mbuf to the cookie-echo chunk 1409163953Srrs */ 1410163953Srrsstatic struct sctp_tcb * 1411163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1412163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1413185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1414228653Stuexen struct sockaddr *init_src, int *notification, 1415185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1416163953Srrs{ 1417163953Srrs struct sctp_association *asoc; 1418163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1419163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1420185694Srrs struct sctp_nets *net; 1421185694Srrs struct mbuf *op_err; 1422185694Srrs struct sctp_paramhdr *ph; 1423165220Srrs int init_offset, initack_offset, i; 1424163953Srrs int retval; 1425164205Srrs int spec_flag = 0; 1426170140Srrs uint32_t how_indx; 1427163953Srrs 1428185694Srrs net = *netp; 1429163953Srrs /* I know that the TCB is non-NULL from the caller */ 1430163953Srrs asoc = &stcb->asoc; 1431166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1432166023Srrs if (asoc->cookie_how[how_indx] == 0) 1433166023Srrs break; 1434166023Srrs } 1435166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1436166023Srrs asoc->cookie_how[how_indx] = 1; 1437166023Srrs } 1438163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1439163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1440163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1441163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1442165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1443163953Srrs if (op_err == NULL) { 1444163953Srrs /* FOOBAR */ 1445163953Srrs return (NULL); 1446163953Srrs } 1447163953Srrs /* Set the len */ 1448165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1449163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1450163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1451163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1452169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1453179157Srrs vrf_id, net->port); 1454166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1455166023Srrs asoc->cookie_how[how_indx] = 2; 1456163953Srrs return (NULL); 1457163953Srrs } 1458163953Srrs /* 1459163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1460163953Srrs * INIT should start after the cookie-echo header struct (chunk 1461163953Srrs * header, state cookie header struct) 1462163953Srrs */ 1463163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1464163953Srrs 1465163953Srrs init_cp = (struct sctp_init_chunk *) 1466163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1467163953Srrs (uint8_t *) & init_buf); 1468163953Srrs if (init_cp == NULL) { 1469163953Srrs /* could not pull a INIT chunk in cookie */ 1470163953Srrs return (NULL); 1471163953Srrs } 1472163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1473163953Srrs return (NULL); 1474163953Srrs } 1475163953Srrs /* 1476163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1477163953Srrs * INIT-ACK follows the INIT chunk 1478163953Srrs */ 1479228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1480163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1481163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1482163953Srrs (uint8_t *) & initack_buf); 1483163953Srrs if (initack_cp == NULL) { 1484163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1485163953Srrs return (NULL); 1486163953Srrs } 1487163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1488163953Srrs return (NULL); 1489163953Srrs } 1490163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1491163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1492163953Srrs /* 1493163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1494163953Srrs * to get into the OPEN state 1495163953Srrs */ 1496166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1497172091Srrs /*- 1498172091Srrs * Opps, this means that we somehow generated two vtag's 1499172091Srrs * the same. I.e. we did: 1500172091Srrs * Us Peer 1501172091Srrs * <---INIT(tag=a)------ 1502172091Srrs * ----INIT-ACK(tag=t)--> 1503172091Srrs * ----INIT(tag=t)------> *1 1504172091Srrs * <---INIT-ACK(tag=a)--- 1505172091Srrs * <----CE(tag=t)------------- *2 1506172091Srrs * 1507172091Srrs * At point *1 we should be generating a different 1508172091Srrs * tag t'. Which means we would throw away the CE and send 1509172091Srrs * ours instead. Basically this is case C (throw away side). 1510172091Srrs */ 1511172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1512172091Srrs asoc->cookie_how[how_indx] = 17; 1513172091Srrs return (NULL); 1514172091Srrs 1515166023Srrs } 1516163953Srrs switch SCTP_GET_STATE 1517163953Srrs (asoc) { 1518163953Srrs case SCTP_STATE_COOKIE_WAIT: 1519166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1520163953Srrs /* 1521169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1522166023Srrs * correct tags... just accept it...but we must 1523166023Srrs * process the init so that we can make sure we have 1524166023Srrs * the right seq no's. 1525163953Srrs */ 1526163953Srrs /* First we must process the INIT !! */ 1527228653Stuexen retval = sctp_process_init(init_cp, stcb); 1528163953Srrs if (retval < 0) { 1529166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1530166023Srrs asoc->cookie_how[how_indx] = 3; 1531163953Srrs return (NULL); 1532163953Srrs } 1533163953Srrs /* we have already processed the INIT so no problem */ 1534163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1535171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1536171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1537163953Srrs /* update current state */ 1538166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1539166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1540166675Srrs else 1541166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1542171943Srrs 1543171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1544163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1545163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1546163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1547163953Srrs } 1548166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1549165220Srrs sctp_stop_all_cookie_timers(stcb); 1550163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1551163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1552163953Srrs (inp->sctp_socket->so_qlimit == 0) 1553163953Srrs ) { 1554172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1555172090Srrs struct socket *so; 1556172090Srrs 1557172090Srrs#endif 1558163953Srrs /* 1559163953Srrs * Here is where collision would go if we 1560163953Srrs * did a connect() and instead got a 1561163953Srrs * init/init-ack/cookie done before the 1562163953Srrs * init-ack came back.. 1563163953Srrs */ 1564163953Srrs stcb->sctp_ep->sctp_flags |= 1565163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1566172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1567172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1568172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1569172090Srrs SCTP_TCB_UNLOCK(stcb); 1570172090Srrs SCTP_SOCKET_LOCK(so, 1); 1571172090Srrs SCTP_TCB_LOCK(stcb); 1572172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1573172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1574172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1575172090Srrs return (NULL); 1576172090Srrs } 1577172090Srrs#endif 1578172090Srrs soisconnected(stcb->sctp_socket); 1579172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1580172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1581172090Srrs#endif 1582163953Srrs } 1583163953Srrs /* notify upper layer */ 1584163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1585163953Srrs /* 1586163953Srrs * since we did not send a HB make sure we don't 1587163953Srrs * double things 1588163953Srrs */ 1589163953Srrs net->hb_responded = 1; 1590170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1591218186Srrs &cookie->time_entered, 1592218186Srrs sctp_align_unsafe_makecopy, 1593219397Srrs SCTP_RTT_FROM_NON_DATA); 1594163953Srrs 1595163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1596163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1597163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1598163953Srrs inp, stcb, NULL); 1599163953Srrs } 1600163953Srrs break; 1601163953Srrs default: 1602163953Srrs /* 1603163953Srrs * we're in the OPEN state (or beyond), so peer must 1604163953Srrs * have simply lost the COOKIE-ACK 1605163953Srrs */ 1606163953Srrs break; 1607163953Srrs } /* end switch */ 1608165220Srrs sctp_stop_all_cookie_timers(stcb); 1609163953Srrs /* 1610163953Srrs * We ignore the return code here.. not sure if we should 1611163953Srrs * somehow abort.. but we do have an existing asoc. This 1612163953Srrs * really should not fail. 1613163953Srrs */ 1614228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1615163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1616163953Srrs initack_offset, sh, init_src)) { 1617166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1618166023Srrs asoc->cookie_how[how_indx] = 4; 1619163953Srrs return (NULL); 1620163953Srrs } 1621163953Srrs /* respond with a COOKIE-ACK */ 1622165220Srrs sctp_toss_old_cookies(stcb, asoc); 1623163953Srrs sctp_send_cookie_ack(stcb); 1624166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1625166023Srrs asoc->cookie_how[how_indx] = 5; 1626163953Srrs return (stcb); 1627169352Srrs } 1628163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1629163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1630163953Srrs cookie->tie_tag_my_vtag == 0 && 1631163953Srrs cookie->tie_tag_peer_vtag == 0) { 1632163953Srrs /* 1633163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1634163953Srrs */ 1635166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1636166023Srrs asoc->cookie_how[how_indx] = 6; 1637163953Srrs return (NULL); 1638163953Srrs } 1639185694Srrs /* 1640185694Srrs * If nat support, and the below and stcb is established, send back 1641185694Srrs * a ABORT(colliding state) if we are established. 1642185694Srrs */ 1643185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1644185694Srrs (asoc->peer_supports_nat) && 1645185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1646185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1647185694Srrs (asoc->peer_vtag == 0)))) { 1648163953Srrs /* 1649185694Srrs * Special case - Peer's support nat. We may have two init's 1650185694Srrs * that we gave out the same tag on since one was not 1651185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1652185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1653185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1654185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1655185694Srrs * Now we have colliding state. We must send an abort here 1656185694Srrs * with colliding state indication. 1657185694Srrs */ 1658185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1659185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1660185694Srrs if (op_err == NULL) { 1661185694Srrs /* FOOBAR */ 1662185694Srrs return (NULL); 1663185694Srrs } 1664185694Srrs /* pre-reserve some space */ 1665185694Srrs#ifdef INET6 1666185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1667185694Srrs#else 1668185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1669185694Srrs#endif 1670185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1671185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1672185694Srrs /* Set the len */ 1673185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1674185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1675185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1676185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1677185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1678185694Srrs return (NULL); 1679185694Srrs } 1680185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1681185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1682185694Srrs (asoc->peer_vtag == 0))) { 1683185694Srrs /* 1684163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1685163953Srrs * should be ok, re-accept peer info 1686163953Srrs */ 1687166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1688166023Srrs /* 1689166023Srrs * Extension of case C. If we hit this, then the 1690166023Srrs * random number generator returned the same vtag 1691166023Srrs * when we first sent our INIT-ACK and when we later 1692166023Srrs * sent our INIT. The side with the seq numbers that 1693166023Srrs * are different will be the one that normnally 1694166023Srrs * would have hit case C. This in effect "extends" 1695166023Srrs * our vtags in this collision case to be 64 bits. 1696166023Srrs * The same collision could occur aka you get both 1697166023Srrs * vtag and seq number the same twice in a row.. but 1698166023Srrs * is much less likely. If it did happen then we 1699166023Srrs * would proceed through and bring up the assoc.. we 1700166023Srrs * may end up with the wrong stream setup however.. 1701166023Srrs * which would be bad.. but there is no way to 1702166023Srrs * tell.. until we send on a stream that does not 1703166023Srrs * exist :-) 1704166023Srrs */ 1705166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1706166023Srrs asoc->cookie_how[how_indx] = 7; 1707166023Srrs 1708166023Srrs return (NULL); 1709166023Srrs } 1710166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1711166023Srrs asoc->cookie_how[how_indx] = 8; 1712171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1713163953Srrs sctp_stop_all_cookie_timers(stcb); 1714163953Srrs /* 1715163953Srrs * since we did not send a HB make sure we don't double 1716163953Srrs * things 1717163953Srrs */ 1718163953Srrs net->hb_responded = 1; 1719163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1720163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1721163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1722163953Srrs NULL); 1723163953Srrs } 1724163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1725164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1726164205Srrs 1727164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1728164205Srrs /* 1729164205Srrs * Ok the peer probably discarded our data (if we 1730164205Srrs * echoed a cookie+data). So anything on the 1731164205Srrs * sent_queue should be marked for retransmit, we 1732164205Srrs * may not get something to kick us so it COULD 1733164205Srrs * still take a timeout to move these.. but it can't 1734164205Srrs * hurt to mark them. 1735164205Srrs */ 1736164205Srrs struct sctp_tmit_chunk *chk; 1737163953Srrs 1738164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1739164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1740164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1741171440Srrs sctp_flight_size_decrease(chk); 1742171440Srrs sctp_total_flight_decrease(stcb, chk); 1743168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1744164205Srrs spec_flag++; 1745164205Srrs } 1746164205Srrs } 1747164205Srrs 1748164205Srrs } 1749163953Srrs /* process the INIT info (peer's info) */ 1750228653Stuexen retval = sctp_process_init(init_cp, stcb); 1751163953Srrs if (retval < 0) { 1752166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1753166023Srrs asoc->cookie_how[how_indx] = 9; 1754163953Srrs return (NULL); 1755163953Srrs } 1756228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1757163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1758163953Srrs initack_offset, sh, init_src)) { 1759166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1760166023Srrs asoc->cookie_how[how_indx] = 10; 1761163953Srrs return (NULL); 1762163953Srrs } 1763163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1764163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1765163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1766163953Srrs 1767163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1768163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1769163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1770172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1771172090Srrs struct socket *so; 1772172090Srrs 1773172090Srrs#endif 1774163953Srrs stcb->sctp_ep->sctp_flags |= 1775163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1776172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1777172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1778172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1779172090Srrs SCTP_TCB_UNLOCK(stcb); 1780172090Srrs SCTP_SOCKET_LOCK(so, 1); 1781172090Srrs SCTP_TCB_LOCK(stcb); 1782172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1783172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1784172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1785172090Srrs return (NULL); 1786172090Srrs } 1787172090Srrs#endif 1788172090Srrs soisconnected(stcb->sctp_socket); 1789172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1790172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1791172090Srrs#endif 1792163953Srrs } 1793166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1794166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1795166675Srrs else 1796166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1797166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1798166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1799166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1800166675Srrs } else { 1801166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1802163953Srrs } 1803171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1804163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1805163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1806163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1807163953Srrs } 1808163953Srrs sctp_stop_all_cookie_timers(stcb); 1809165220Srrs sctp_toss_old_cookies(stcb, asoc); 1810163953Srrs sctp_send_cookie_ack(stcb); 1811164205Srrs if (spec_flag) { 1812164205Srrs /* 1813164205Srrs * only if we have retrans set do we do this. What 1814164205Srrs * this call does is get only the COOKIE-ACK out and 1815164205Srrs * then when we return the normal call to 1816164205Srrs * sctp_chunk_output will get the retrans out behind 1817164205Srrs * this. 1818164205Srrs */ 1819172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1820164205Srrs } 1821166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1822166023Srrs asoc->cookie_how[how_indx] = 11; 1823166023Srrs 1824163953Srrs return (stcb); 1825163953Srrs } 1826163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1827163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1828163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1829163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1830163953Srrs cookie->tie_tag_peer_vtag != 0) { 1831163953Srrs struct sctpasochead *head; 1832163953Srrs 1833185694Srrs if (asoc->peer_supports_nat) { 1834185694Srrs /* 1835185694Srrs * This is a gross gross hack. just call the 1836185694Srrs * cookie_new code since we are allowing a duplicate 1837185694Srrs * association. I hope this works... 1838185694Srrs */ 1839185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1840185694Srrs inp, netp, init_src, notification, 1841185694Srrs auth_skipped, auth_offset, auth_len, 1842185694Srrs vrf_id, port)); 1843185694Srrs } 1844163953Srrs /* 1845163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1846163953Srrs */ 1847165220Srrs /* temp code */ 1848166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1849166023Srrs asoc->cookie_how[how_indx] = 12; 1850171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1851171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1852165647Srrs 1853163953Srrs /* notify upper layer */ 1854163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1855165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1856166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1857166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1858166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1859166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1860166675Srrs } 1861166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1862166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1863166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1864166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1865166675Srrs } 1866165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1867171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1868165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1869165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1870165647Srrs 1871165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1872165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1873171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1874165647Srrs } 1875165647Srrs asoc->pre_open_streams = 1876165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1877165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1878165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1879179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1880165647Srrs 1881165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1882165647Srrs 1883165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1884165647Srrs 1885165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1886170138Srrs if (asoc->mapping_array) { 1887165647Srrs memset(asoc->mapping_array, 0, 1888165647Srrs asoc->mapping_array_size); 1889170138Srrs } 1890205627Srrs if (asoc->nr_mapping_array) { 1891185694Srrs memset(asoc->nr_mapping_array, 0, 1892206137Stuexen asoc->mapping_array_size); 1893185694Srrs } 1894165220Srrs SCTP_TCB_UNLOCK(stcb); 1895165220Srrs SCTP_INP_INFO_WLOCK(); 1896165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1897165220Srrs SCTP_TCB_LOCK(stcb); 1898165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1899163953Srrs /* send up all the data */ 1900164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1901165220Srrs 1902172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1903165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1904165220Srrs stcb->asoc.strmout[i].stream_no = i; 1905165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1906165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1907165220Srrs } 1908163953Srrs /* process the INIT-ACK info (my info) */ 1909163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1910163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1911163953Srrs 1912163953Srrs /* pull from vtag hash */ 1913163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1914163953Srrs /* re-insert to new vtag position */ 1915179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1916179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1917163953Srrs /* 1918163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1919163953Srrs * system 1920163953Srrs */ 1921163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1922163953Srrs 1923163953Srrs /* process the INIT info (peer's info) */ 1924164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1925165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1926165220Srrs SCTP_INP_INFO_WUNLOCK(); 1927165220Srrs 1928228653Stuexen retval = sctp_process_init(init_cp, stcb); 1929163953Srrs if (retval < 0) { 1930166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1931166023Srrs asoc->cookie_how[how_indx] = 13; 1932166023Srrs 1933163953Srrs return (NULL); 1934163953Srrs } 1935163953Srrs /* 1936163953Srrs * since we did not send a HB make sure we don't double 1937163953Srrs * things 1938163953Srrs */ 1939163953Srrs net->hb_responded = 1; 1940163953Srrs 1941228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 1942163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1943163953Srrs initack_offset, sh, init_src)) { 1944166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1945166023Srrs asoc->cookie_how[how_indx] = 14; 1946166023Srrs 1947163953Srrs return (NULL); 1948163953Srrs } 1949163953Srrs /* respond with a COOKIE-ACK */ 1950163953Srrs sctp_stop_all_cookie_timers(stcb); 1951165220Srrs sctp_toss_old_cookies(stcb, asoc); 1952163953Srrs sctp_send_cookie_ack(stcb); 1953166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1954166023Srrs asoc->cookie_how[how_indx] = 15; 1955163953Srrs 1956163953Srrs return (stcb); 1957163953Srrs } 1958166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1959166023Srrs asoc->cookie_how[how_indx] = 16; 1960163953Srrs /* all other cases... */ 1961163953Srrs return (NULL); 1962163953Srrs} 1963163953Srrs 1964166086Srrs 1965163953Srrs/* 1966163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1967163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1968163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1969163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1970163953Srrs * was from returns a new TCB 1971163953Srrs */ 1972185694Srrsstruct sctp_tcb * 1973163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1974163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1975163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1976163953Srrs struct sockaddr *init_src, int *notification, 1977169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1978179157Srrs uint32_t vrf_id, uint16_t port) 1979163953Srrs{ 1980163953Srrs struct sctp_tcb *stcb; 1981163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1982163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1983163953Srrs struct sockaddr_storage sa_store; 1984163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1985163953Srrs struct sctp_association *asoc; 1986163953Srrs int init_offset, initack_offset, initack_limit; 1987163953Srrs int retval; 1988163953Srrs int error = 0; 1989166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1990163953Srrs 1991221249Stuexen#ifdef INET 1992221249Stuexen struct sockaddr_in *sin; 1993221249Stuexen 1994221249Stuexen#endif 1995221249Stuexen#ifdef INET6 1996221249Stuexen struct sockaddr_in6 *sin6; 1997221249Stuexen 1998221249Stuexen#endif 1999172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2000172090Srrs struct socket *so; 2001172090Srrs 2002172090Srrs so = SCTP_INP_SO(inp); 2003172090Srrs#endif 2004172090Srrs 2005163953Srrs /* 2006163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 2007163953Srrs * INIT should start after the cookie-echo header struct (chunk 2008163953Srrs * header, state cookie header struct) 2009163953Srrs */ 2010163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2011163953Srrs init_cp = (struct sctp_init_chunk *) 2012163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2013163953Srrs (uint8_t *) & init_buf); 2014163953Srrs if (init_cp == NULL) { 2015163953Srrs /* could not pull a INIT chunk in cookie */ 2016169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2017169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 2018163953Srrs return (NULL); 2019163953Srrs } 2020163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2021169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2022163953Srrs return (NULL); 2023163953Srrs } 2024228907Stuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2025163953Srrs /* 2026163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 2027163953Srrs * INIT-ACK follows the INIT chunk 2028163953Srrs */ 2029163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2030163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2031163953Srrs (uint8_t *) & initack_buf); 2032163953Srrs if (initack_cp == NULL) { 2033163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2034169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2035163953Srrs return (NULL); 2036163953Srrs } 2037163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2038163953Srrs return (NULL); 2039163953Srrs } 2040163953Srrs /* 2041163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2042163953Srrs * "initack_limit" value. This is because the chk_length field 2043163953Srrs * includes the length of the cookie, but the cookie is omitted when 2044163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2045163953Srrs */ 2046163953Srrs initack_limit = offset + cookie_len; 2047163953Srrs 2048163953Srrs /* 2049163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2050163953Srrs * and popluate 2051163953Srrs */ 2052171531Srrs 2053171531Srrs /* 2054171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2055171531Srrs * We do this since in effect we only use the p argument when the 2056171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2057171531Srrs * getting a cookie, we cannot be unbound. 2058171531Srrs */ 2059206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2060171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2061171531Srrs (struct thread *)NULL 2062171531Srrs ); 2063163953Srrs if (stcb == NULL) { 2064163953Srrs struct mbuf *op_err; 2065163953Srrs 2066163953Srrs /* memory problem? */ 2067169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2068169420Srrs "process_cookie_new: no room for another TCB!\n"); 2069163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2070170140Srrs 2071163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2072179157Srrs sh, op_err, vrf_id, port); 2073163953Srrs return (NULL); 2074163953Srrs } 2075163953Srrs /* get the correct sctp_nets */ 2076169420Srrs if (netp) 2077169420Srrs *netp = sctp_findnet(stcb, init_src); 2078169420Srrs 2079163953Srrs asoc = &stcb->asoc; 2080163953Srrs /* get scope variables out of cookie */ 2081163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2082163953Srrs asoc->site_scope = cookie->site_scope; 2083163953Srrs asoc->local_scope = cookie->local_scope; 2084163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2085163953Srrs 2086163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2087163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2088163953Srrs struct mbuf *op_err; 2089163953Srrs 2090163953Srrs /* 2091163953Srrs * Houston we have a problem. The EP changed while the 2092163953Srrs * cookie was in flight. Only recourse is to abort the 2093163953Srrs * association. 2094163953Srrs */ 2095170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2096163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2097163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2098179157Srrs sh, op_err, vrf_id, port); 2099172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2100172090Srrs SCTP_TCB_UNLOCK(stcb); 2101172090Srrs SCTP_SOCKET_LOCK(so, 1); 2102172090Srrs SCTP_TCB_LOCK(stcb); 2103172090Srrs#endif 2104171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2105171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2106172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2107172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2108172090Srrs#endif 2109172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2110163953Srrs return (NULL); 2111163953Srrs } 2112163953Srrs /* process the INIT-ACK info (my info) */ 2113185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2114163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2115163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2116163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2117163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2118179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2119163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2120163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2121163953Srrs 2122163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2123163953Srrs 2124163953Srrs /* process the INIT info (peer's info) */ 2125169420Srrs if (netp) 2126228653Stuexen retval = sctp_process_init(init_cp, stcb); 2127169420Srrs else 2128169420Srrs retval = 0; 2129163953Srrs if (retval < 0) { 2130170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2131172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2132172090Srrs SCTP_TCB_UNLOCK(stcb); 2133172090Srrs SCTP_SOCKET_LOCK(so, 1); 2134172090Srrs SCTP_TCB_LOCK(stcb); 2135172090Srrs#endif 2136171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2137172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2138172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2139172090Srrs#endif 2140172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2141163953Srrs return (NULL); 2142163953Srrs } 2143163953Srrs /* load all addresses */ 2144228653Stuexen if (sctp_load_addresses_from_init(stcb, m, 2145163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2146163953Srrs init_src)) { 2147170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2148172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2149172090Srrs SCTP_TCB_UNLOCK(stcb); 2150172090Srrs SCTP_SOCKET_LOCK(so, 1); 2151172090Srrs SCTP_TCB_LOCK(stcb); 2152172090Srrs#endif 2153171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2154172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2155172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2156172090Srrs#endif 2157172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2158163953Srrs return (NULL); 2159163953Srrs } 2160163953Srrs /* 2161163953Srrs * verify any preceding AUTH chunk that was skipped 2162163953Srrs */ 2163163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2164163953Srrs sctp_auth_get_cookie_params(stcb, m, 2165163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2166163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2167163953Srrs if (auth_skipped) { 2168163953Srrs struct sctp_auth_chunk *auth; 2169163953Srrs 2170163953Srrs auth = (struct sctp_auth_chunk *) 2171166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2172169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2173163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2174169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2175169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2176171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2177172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2178172090Srrs SCTP_TCB_UNLOCK(stcb); 2179172090Srrs SCTP_SOCKET_LOCK(so, 1); 2180172090Srrs SCTP_TCB_LOCK(stcb); 2181172090Srrs#endif 2182171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2183172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2184172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2185172090Srrs#endif 2186172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2187163953Srrs return (NULL); 2188163953Srrs } else { 2189163953Srrs /* remaining chunks checked... good to go */ 2190163953Srrs stcb->asoc.authenticated = 1; 2191163953Srrs } 2192163953Srrs } 2193163953Srrs /* update current state */ 2194169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2195171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2196163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2197163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2198163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2199163953Srrs } 2200165220Srrs sctp_stop_all_cookie_timers(stcb); 2201163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2202163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2203163953Srrs 2204163953Srrs /* 2205163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2206163953Srrs * addresses that need to get added to the peer (eg. addresses 2207163953Srrs * changed while cookie echo in flight). This needs to be done 2208163953Srrs * after we go to the OPEN state to do the correct asconf 2209163953Srrs * processing. else, make sure we have the correct addresses in our 2210163953Srrs * lists 2211163953Srrs */ 2212163953Srrs 2213163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2214163953Srrs /* pull in local_address (our "from" address) */ 2215221249Stuexen switch (cookie->laddr_type) { 2216221249Stuexen#ifdef INET 2217221249Stuexen case SCTP_IPV4_ADDRESS: 2218163953Srrs /* source addr is IPv4 */ 2219163953Srrs sin = (struct sockaddr_in *)initack_src; 2220163953Srrs memset(sin, 0, sizeof(*sin)); 2221163953Srrs sin->sin_family = AF_INET; 2222163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2223163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2224221249Stuexen break; 2225221249Stuexen#endif 2226221249Stuexen#ifdef INET6 2227221249Stuexen case SCTP_IPV6_ADDRESS: 2228163953Srrs /* source addr is IPv6 */ 2229163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2230163953Srrs memset(sin6, 0, sizeof(*sin6)); 2231163953Srrs sin6->sin6_family = AF_INET6; 2232163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2233163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2234163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2235163953Srrs sizeof(sin6->sin6_addr)); 2236221249Stuexen break; 2237221249Stuexen#endif 2238221249Stuexen default: 2239170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2240172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2241172090Srrs SCTP_TCB_UNLOCK(stcb); 2242172090Srrs SCTP_SOCKET_LOCK(so, 1); 2243172090Srrs SCTP_TCB_LOCK(stcb); 2244172090Srrs#endif 2245171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2246172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2247172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2248172090Srrs#endif 2249172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2250163953Srrs return (NULL); 2251163953Srrs } 2252163953Srrs 2253163953Srrs /* set up to notify upper layer */ 2254163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2255163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2256163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2257163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2258163953Srrs /* 2259163953Srrs * This is an endpoint that called connect() how it got a 2260163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2261163953Srrs * the INIT was sent, but before it got there.. a complete 2262163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2263163953Srrs * should have went to the other code.. not here.. oh well.. 2264163953Srrs * a bit of protection is worth having.. 2265163953Srrs */ 2266163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2267172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2268172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2269172090Srrs SCTP_TCB_UNLOCK(stcb); 2270172090Srrs SCTP_SOCKET_LOCK(so, 1); 2271172090Srrs SCTP_TCB_LOCK(stcb); 2272172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2273172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2274172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2275172090Srrs return (NULL); 2276172090Srrs } 2277172090Srrs#endif 2278172090Srrs soisconnected(stcb->sctp_socket); 2279172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2280172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2281172090Srrs#endif 2282163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2283163953Srrs (inp->sctp_socket->so_qlimit)) { 2284163953Srrs /* 2285163953Srrs * We don't want to do anything with this one. Since it is 2286163953Srrs * the listening guy. The timer will get started for 2287163953Srrs * accepted connections in the caller. 2288163953Srrs */ 2289163953Srrs ; 2290163953Srrs } 2291163953Srrs /* since we did not send a HB make sure we don't double things */ 2292169420Srrs if ((netp) && (*netp)) 2293169420Srrs (*netp)->hb_responded = 1; 2294163953Srrs 2295163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2296163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2297163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2298163953Srrs } 2299165220Srrs /* calculate the RTT */ 2300171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2301170642Srrs if ((netp) && (*netp)) { 2302169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2303218186Srrs &cookie->time_entered, sctp_align_unsafe_makecopy, 2304219397Srrs SCTP_RTT_FROM_NON_DATA); 2305170642Srrs } 2306172190Srrs /* respond with a COOKIE-ACK */ 2307163953Srrs sctp_send_cookie_ack(stcb); 2308172190Srrs 2309172190Srrs /* 2310172190Srrs * check the address lists for any ASCONFs that need to be sent 2311172190Srrs * AFTER the cookie-ack is sent 2312172190Srrs */ 2313172190Srrs sctp_check_address_list(stcb, m, 2314172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2315172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2316172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2317172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2318172190Srrs 2319172190Srrs 2320163953Srrs return (stcb); 2321163953Srrs} 2322163953Srrs 2323185694Srrs/* 2324185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2325185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2326185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2327185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2328185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2329185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2330185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2331185694Srrs -- SEND ABORT - TRY AGAIN -- 2332185694Srrs } 2333185694Srrs } 2334185694Srrs*/ 2335163953Srrs 2336163953Srrs/* 2337163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2338163953Srrs * existing (non-NULL) TCB 2339163953Srrs */ 2340163953Srrsstatic struct mbuf * 2341163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2342163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2343163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2344169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2345179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2346163953Srrs{ 2347163953Srrs struct sctp_state_cookie *cookie; 2348163953Srrs struct sctp_tcb *l_stcb = *stcb; 2349163953Srrs struct sctp_inpcb *l_inp; 2350163953Srrs struct sockaddr *to; 2351163953Srrs struct sctp_pcb *ep; 2352163953Srrs struct mbuf *m_sig; 2353163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2354163953Srrs uint8_t *sig; 2355163953Srrs uint8_t cookie_ok = 0; 2356163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2357163953Srrs unsigned int cookie_len; 2358163953Srrs struct timeval now; 2359163953Srrs struct timeval time_expires; 2360163953Srrs struct sockaddr_storage dest_store; 2361163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2362163953Srrs struct ip *iph; 2363163953Srrs int notification = 0; 2364163953Srrs struct sctp_nets *netl; 2365163953Srrs int had_a_existing_tcb = 0; 2366216887Stuexen int send_int_conf = 0; 2367163953Srrs 2368221249Stuexen#ifdef INET 2369221249Stuexen struct sockaddr_in sin; 2370221249Stuexen 2371221249Stuexen#endif 2372221249Stuexen#ifdef INET6 2373221249Stuexen struct sockaddr_in6 sin6; 2374221249Stuexen 2375221249Stuexen#endif 2376221249Stuexen 2377169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2378169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2379163953Srrs 2380163953Srrs if (inp_p == NULL) { 2381163953Srrs return (NULL); 2382163953Srrs } 2383163953Srrs /* First get the destination address setup too. */ 2384163953Srrs iph = mtod(m, struct ip *); 2385178251Srrs switch (iph->ip_v) { 2386221249Stuexen#ifdef INET 2387178251Srrs case IPVERSION: 2388178251Srrs { 2389178251Srrs /* its IPv4 */ 2390178251Srrs struct sockaddr_in *lsin; 2391163953Srrs 2392178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2393178251Srrs memset(lsin, 0, sizeof(*lsin)); 2394178251Srrs lsin->sin_family = AF_INET; 2395178251Srrs lsin->sin_len = sizeof(*lsin); 2396178251Srrs lsin->sin_port = sh->dest_port; 2397178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2398178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2399178251Srrs break; 2400178251Srrs } 2401221249Stuexen#endif 2402178251Srrs#ifdef INET6 2403178251Srrs case IPV6_VERSION >> 4: 2404178251Srrs { 2405178251Srrs /* its IPv6 */ 2406178251Srrs struct ip6_hdr *ip6; 2407178251Srrs struct sockaddr_in6 *lsin6; 2408163953Srrs 2409178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2410178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2411178251Srrs lsin6->sin6_family = AF_INET6; 2412178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2413178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2414178251Srrs lsin6->sin6_port = sh->dest_port; 2415178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2416178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2417178251Srrs break; 2418178251Srrs } 2419178251Srrs#endif 2420178251Srrs default: 2421163953Srrs return (NULL); 2422163953Srrs } 2423163953Srrs 2424163953Srrs cookie = &cp->cookie; 2425163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2426163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2427163953Srrs 2428163953Srrs if ((cookie->peerport != sh->src_port) && 2429163953Srrs (cookie->myport != sh->dest_port) && 2430163953Srrs (cookie->my_vtag != sh->v_tag)) { 2431163953Srrs /* 2432163953Srrs * invalid ports or bad tag. Note that we always leave the 2433163953Srrs * v_tag in the header in network order and when we stored 2434163953Srrs * it in the my_vtag slot we also left it in network order. 2435169352Srrs * This maintains the match even though it may be in the 2436163953Srrs * opposite byte order of the machine :-> 2437163953Srrs */ 2438163953Srrs return (NULL); 2439163953Srrs } 2440163953Srrs if (cookie_len > size_of_pkt || 2441163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2442163953Srrs sizeof(struct sctp_init_chunk) + 2443163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2444163953Srrs /* cookie too long! or too small */ 2445163953Srrs return (NULL); 2446163953Srrs } 2447163953Srrs /* 2448163953Srrs * split off the signature into its own mbuf (since it should not be 2449163953Srrs * calculated in the sctp_hmac_m() call). 2450163953Srrs */ 2451163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2452163953Srrs if (sig_offset > size_of_pkt) { 2453163953Srrs /* packet not correct size! */ 2454163953Srrs /* XXX this may already be accounted for earlier... */ 2455163953Srrs return (NULL); 2456163953Srrs } 2457163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2458163953Srrs if (m_sig == NULL) { 2459163953Srrs /* out of memory or ?? */ 2460163953Srrs return (NULL); 2461163953Srrs } 2462178202Srrs#ifdef SCTP_MBUF_LOGGING 2463179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2464178202Srrs struct mbuf *mat; 2465178202Srrs 2466178202Srrs mat = m_sig; 2467178202Srrs while (mat) { 2468178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2469178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2470178202Srrs } 2471178202Srrs mat = SCTP_BUF_NEXT(mat); 2472178202Srrs } 2473178202Srrs } 2474178202Srrs#endif 2475178202Srrs 2476163953Srrs /* 2477163953Srrs * compute the signature/digest for the cookie 2478163953Srrs */ 2479163953Srrs ep = &(*inp_p)->sctp_ep; 2480163953Srrs l_inp = *inp_p; 2481163953Srrs if (l_stcb) { 2482163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2483163953Srrs } 2484163953Srrs SCTP_INP_RLOCK(l_inp); 2485163953Srrs if (l_stcb) { 2486163953Srrs SCTP_TCB_LOCK(l_stcb); 2487163953Srrs } 2488163953Srrs /* which cookie is it? */ 2489163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2490163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2491163953Srrs /* it's the old cookie */ 2492169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2493163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2494170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2495163953Srrs } else { 2496163953Srrs /* it's the current cookie */ 2497169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2498163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2499170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2500163953Srrs } 2501163953Srrs /* get the signature */ 2502163953Srrs SCTP_INP_RUNLOCK(l_inp); 2503163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2504163953Srrs if (sig == NULL) { 2505163953Srrs /* couldn't find signature */ 2506164085Srrs sctp_m_freem(m_sig); 2507163953Srrs return (NULL); 2508163953Srrs } 2509163953Srrs /* compare the received digest with the computed digest */ 2510163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2511163953Srrs /* try the old cookie? */ 2512163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2513163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2514163953Srrs /* compute digest with old */ 2515169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2516163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2517170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2518163953Srrs /* compare */ 2519163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2520163953Srrs cookie_ok = 1; 2521163953Srrs } 2522163953Srrs } else { 2523163953Srrs cookie_ok = 1; 2524163953Srrs } 2525163953Srrs 2526163953Srrs /* 2527163953Srrs * Now before we continue we must reconstruct our mbuf so that 2528163953Srrs * normal processing of any other chunks will work. 2529163953Srrs */ 2530163953Srrs { 2531163953Srrs struct mbuf *m_at; 2532163953Srrs 2533163953Srrs m_at = m; 2534165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2535165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2536163953Srrs } 2537165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2538163953Srrs } 2539163953Srrs 2540163953Srrs if (cookie_ok == 0) { 2541169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2542169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2543169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2544169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2545163953Srrs return (NULL); 2546163953Srrs } 2547163953Srrs /* 2548163953Srrs * check the cookie timestamps to be sure it's not stale 2549163953Srrs */ 2550169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2551163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2552169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2553163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2554180387Srrs /* 2555180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2556180387Srrs * is undefined. 2557180387Srrs */ 2558163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2559163953Srrs /* cookie is stale! */ 2560163953Srrs struct mbuf *op_err; 2561163953Srrs struct sctp_stale_cookie_msg *scm; 2562163953Srrs uint32_t tim; 2563163953Srrs 2564163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2565165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2566163953Srrs if (op_err == NULL) { 2567163953Srrs /* FOOBAR */ 2568163953Srrs return (NULL); 2569163953Srrs } 2570163953Srrs /* Set the len */ 2571165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2572163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2573163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2574163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2575163953Srrs (sizeof(uint32_t)))); 2576163953Srrs /* seconds to usec */ 2577163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2578163953Srrs /* add in usec */ 2579163953Srrs if (tim == 0) 2580163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2581163953Srrs scm->time_usec = htonl(tim); 2582169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2583179157Srrs vrf_id, port); 2584163953Srrs return (NULL); 2585163953Srrs } 2586163953Srrs /* 2587163953Srrs * Now we must see with the lookup address if we have an existing 2588163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2589163953Srrs * and a INIT collided with us and somewhere the peer sent the 2590163953Srrs * cookie on another address besides the single address our assoc 2591163953Srrs * had for him. In this case we will have one of the tie-tags set at 2592163953Srrs * least AND the address field in the cookie can be used to look it 2593163953Srrs * up. 2594163953Srrs */ 2595163953Srrs to = NULL; 2596221249Stuexen switch (cookie->addr_type) { 2597221249Stuexen#ifdef INET6 2598221249Stuexen case SCTP_IPV6_ADDRESS: 2599163953Srrs memset(&sin6, 0, sizeof(sin6)); 2600163953Srrs sin6.sin6_family = AF_INET6; 2601163953Srrs sin6.sin6_len = sizeof(sin6); 2602163953Srrs sin6.sin6_port = sh->src_port; 2603163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2604163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2605163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2606163953Srrs to = (struct sockaddr *)&sin6; 2607221249Stuexen break; 2608221249Stuexen#endif 2609221249Stuexen#ifdef INET 2610221249Stuexen case SCTP_IPV4_ADDRESS: 2611163953Srrs memset(&sin, 0, sizeof(sin)); 2612163953Srrs sin.sin_family = AF_INET; 2613163953Srrs sin.sin_len = sizeof(sin); 2614163953Srrs sin.sin_port = sh->src_port; 2615163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2616163953Srrs to = (struct sockaddr *)&sin; 2617221249Stuexen break; 2618221249Stuexen#endif 2619221249Stuexen default: 2620169420Srrs /* This should not happen */ 2621169420Srrs return (NULL); 2622163953Srrs } 2623163953Srrs if ((*stcb == NULL) && to) { 2624163953Srrs /* Yep, lets check */ 2625163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2626163953Srrs if (*stcb == NULL) { 2627163953Srrs /* 2628163953Srrs * We should have only got back the same inp. If we 2629163953Srrs * got back a different ep we have a problem. The 2630163953Srrs * original findep got back l_inp and now 2631163953Srrs */ 2632163953Srrs if (l_inp != *inp_p) { 2633169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2634163953Srrs } 2635165220Srrs } else { 2636165220Srrs if (*locked_tcb == NULL) { 2637165220Srrs /* 2638165220Srrs * In this case we found the assoc only 2639165220Srrs * after we locked the create lock. This 2640165220Srrs * means we are in a colliding case and we 2641165220Srrs * must make sure that we unlock the tcb if 2642165220Srrs * its one of the cases where we throw away 2643165220Srrs * the incoming packets. 2644165220Srrs */ 2645165220Srrs *locked_tcb = *stcb; 2646165220Srrs 2647165220Srrs /* 2648165220Srrs * We must also increment the inp ref count 2649165220Srrs * since the ref_count flags was set when we 2650165220Srrs * did not find the TCB, now we found it 2651165220Srrs * which reduces the refcount.. we must 2652165220Srrs * raise it back out to balance it all :-) 2653165220Srrs */ 2654165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2655165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2656169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2657165220Srrs (*stcb)->sctp_ep, l_inp); 2658165220Srrs } 2659165220Srrs } 2660163953Srrs } 2661163953Srrs } 2662188067Srrs if (to == NULL) { 2663169420Srrs return (NULL); 2664188067Srrs } 2665163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2666163953Srrs if (*stcb == NULL) { 2667163953Srrs /* this is the "normal" case... get a new TCB */ 2668163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2669163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2670179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2671163953Srrs } else { 2672163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2673163953Srrs had_a_existing_tcb = 1; 2674163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2675185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2676228653Stuexen ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port); 2677163953Srrs } 2678163953Srrs 2679163953Srrs if (*stcb == NULL) { 2680163953Srrs /* still no TCB... must be bad cookie-echo */ 2681163953Srrs return (NULL); 2682163953Srrs } 2683218335Stuexen if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2684218335Stuexen (*netp)->flowid = m->m_pkthdr.flowid; 2685218400Stuexen#ifdef INVARIANTS 2686218392Srrs (*netp)->flowidset = 1; 2687218400Stuexen#endif 2688218335Stuexen } 2689163953Srrs /* 2690163953Srrs * Ok, we built an association so confirm the address we sent the 2691163953Srrs * INIT-ACK to. 2692163953Srrs */ 2693163953Srrs netl = sctp_findnet(*stcb, to); 2694163953Srrs /* 2695163953Srrs * This code should in theory NOT run but 2696163953Srrs */ 2697163953Srrs if (netl == NULL) { 2698163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2699228907Stuexen if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2700228907Stuexen return (NULL); 2701228907Stuexen } 2702163953Srrs netl = sctp_findnet(*stcb, to); 2703163953Srrs } 2704163953Srrs if (netl) { 2705163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2706163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2707169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2708163953Srrs netl); 2709216887Stuexen send_int_conf = 1; 2710163953Srrs } 2711163953Srrs } 2712224641Stuexen sctp_start_net_timers(*stcb); 2713163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2714163953Srrs if (!had_a_existing_tcb || 2715163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2716163953Srrs /* 2717163953Srrs * If we have a NEW cookie or the connect never 2718163953Srrs * reached the connected state during collision we 2719163953Srrs * must do the TCP accept thing. 2720163953Srrs */ 2721163953Srrs struct socket *so, *oso; 2722163953Srrs struct sctp_inpcb *inp; 2723163953Srrs 2724163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2725163953Srrs /* 2726163953Srrs * For a restart we will keep the same 2727163953Srrs * socket, no need to do anything. I THINK!! 2728163953Srrs */ 2729228653Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2730216887Stuexen if (send_int_conf) { 2731216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2732216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2733216887Stuexen } 2734163953Srrs return (m); 2735163953Srrs } 2736163953Srrs oso = (*inp_p)->sctp_socket; 2737171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2738163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2739218757Sbz CURVNET_SET(oso->so_vnet); 2740166086Srrs so = sonewconn(oso, 0 2741163953Srrs ); 2742218757Sbz CURVNET_RESTORE(); 2743163953Srrs SCTP_TCB_LOCK((*stcb)); 2744171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2745171858Srrs 2746163953Srrs if (so == NULL) { 2747163953Srrs struct mbuf *op_err; 2748163953Srrs 2749172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2750172090Srrs struct socket *pcb_so; 2751172090Srrs 2752172090Srrs#endif 2753163953Srrs /* Too many sockets */ 2754169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2755163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2756163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2757179157Srrs sh, op_err, vrf_id, port); 2758172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2759172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2760172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2761172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2762172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2763172090Srrs SCTP_TCB_LOCK((*stcb)); 2764172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2765172090Srrs#endif 2766171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2767172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2768172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2769172090Srrs#endif 2770163953Srrs return (NULL); 2771163953Srrs } 2772163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2773166086Srrs SCTP_INP_INCR_REF(inp); 2774166086Srrs /* 2775166086Srrs * We add the unbound flag here so that if we get an 2776166086Srrs * soabort() before we get the move_pcb done, we 2777166086Srrs * will properly cleanup. 2778166086Srrs */ 2779163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2780163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2781163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2782166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2783163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2784163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2785163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2786172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2787163953Srrs inp->sctp_socket = so; 2788163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2789211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2790218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2791163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2792163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2793163953Srrs inp->inp_starting_point_for_iterator = NULL; 2794163953Srrs /* 2795163953Srrs * copy in the authentication parameters from the 2796163953Srrs * original endpoint 2797163953Srrs */ 2798163953Srrs if (inp->sctp_ep.local_hmacs) 2799163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2800163953Srrs inp->sctp_ep.local_hmacs = 2801163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2802163953Srrs if (inp->sctp_ep.local_auth_chunks) 2803163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2804163953Srrs inp->sctp_ep.local_auth_chunks = 2805163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2806163953Srrs 2807163953Srrs /* 2808163953Srrs * Now we must move it from one hash table to 2809163953Srrs * another and get the tcb in the right place. 2810163953Srrs */ 2811207924Srrs 2812207924Srrs /* 2813207924Srrs * This is where the one-2-one socket is put into 2814207924Srrs * the accept state waiting for the accept! 2815207924Srrs */ 2816207924Srrs if (*stcb) { 2817207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2818207924Srrs } 2819163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2820170056Srrs 2821170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2822170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2823170056Srrs 2824175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2825175845Srwatson 0); 2826170056Srrs SCTP_TCB_LOCK((*stcb)); 2827170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2828163953Srrs 2829170056Srrs 2830166086Srrs /* 2831166086Srrs * now we must check to see if we were aborted while 2832166086Srrs * the move was going on and the lock/unlock 2833166086Srrs * happened. 2834166086Srrs */ 2835166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2836166086Srrs /* 2837166086Srrs * yep it was, we leave the assoc attached 2838166086Srrs * to the socket since the sctp_inpcb_free() 2839166086Srrs * call will send an abort for us. 2840166086Srrs */ 2841166086Srrs SCTP_INP_DECR_REF(inp); 2842166086Srrs return (NULL); 2843166086Srrs } 2844166086Srrs SCTP_INP_DECR_REF(inp); 2845163953Srrs /* Switch over to the new guy */ 2846163953Srrs *inp_p = inp; 2847172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2848216887Stuexen if (send_int_conf) { 2849216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2850216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2851216887Stuexen } 2852166086Srrs /* 2853166086Srrs * Pull it from the incomplete queue and wake the 2854166086Srrs * guy 2855166086Srrs */ 2856172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2857172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2858172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2859172090Srrs SCTP_SOCKET_LOCK(so, 1); 2860172090Srrs#endif 2861166086Srrs soisconnected(so); 2862172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2863172118Srrs SCTP_TCB_LOCK((*stcb)); 2864172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2865172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2866172090Srrs#endif 2867163953Srrs return (m); 2868163953Srrs } 2869163953Srrs } 2870216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2871216887Stuexen if (notification) { 2872216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2873216887Stuexen } 2874216887Stuexen if (send_int_conf) { 2875216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2876216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2877216887Stuexen } 2878163953Srrs } 2879163953Srrs return (m); 2880163953Srrs} 2881163953Srrs 2882163953Srrsstatic void 2883228653Stuexensctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2884163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2885163953Srrs{ 2886163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2887163953Srrs struct sctp_association *asoc; 2888163953Srrs 2889169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2890169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2891163953Srrs if (stcb == NULL) 2892163953Srrs return; 2893163953Srrs 2894163953Srrs asoc = &stcb->asoc; 2895163953Srrs 2896163953Srrs sctp_stop_all_cookie_timers(stcb); 2897163953Srrs /* process according to association state */ 2898163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2899163953Srrs /* state change only needed when I am in right state */ 2900169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2901171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2902224641Stuexen sctp_start_net_timers(stcb); 2903163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2904163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2905163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2906163953Srrs 2907163953Srrs } 2908163953Srrs /* update RTO */ 2909163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2910163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2911163953Srrs if (asoc->overall_error_count == 0) { 2912163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2913218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2914219397Srrs SCTP_RTT_FROM_NON_DATA); 2915163953Srrs } 2916169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2917172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2918163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2919163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2920172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2921172090Srrs struct socket *so; 2922172090Srrs 2923172090Srrs#endif 2924163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2925172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2926172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2927172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2928172090Srrs SCTP_TCB_UNLOCK(stcb); 2929172090Srrs SCTP_SOCKET_LOCK(so, 1); 2930172090Srrs SCTP_TCB_LOCK(stcb); 2931172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2932218641Srrs#endif 2933218641Srrs if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2934218641Srrs soisconnected(stcb->sctp_socket); 2935172090Srrs } 2936172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2937172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2938172090Srrs#endif 2939163953Srrs } 2940163953Srrs /* 2941163953Srrs * since we did not send a HB make sure we don't double 2942163953Srrs * things 2943163953Srrs */ 2944163953Srrs net->hb_responded = 1; 2945163953Srrs 2946218641Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2947218641Srrs /* 2948218641Srrs * We don't need to do the asconf thing, nor hb or 2949218641Srrs * autoclose if the socket is closed. 2950218641Srrs */ 2951218641Srrs goto closed_socket; 2952218641Srrs } 2953218641Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2954218641Srrs stcb, net); 2955218641Srrs 2956218641Srrs 2957163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2958163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2959163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2960163953Srrs stcb->sctp_ep, stcb, NULL); 2961163953Srrs } 2962163953Srrs /* 2963171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2964171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2965171572Srrs * in flight) 2966163953Srrs */ 2967163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2968163953Srrs (stcb->asoc.peer_supports_asconf) && 2969163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2970171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2971163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2972163953Srrs stcb->sctp_ep, stcb, 2973163953Srrs stcb->asoc.primary_destination); 2974171572Srrs#else 2975172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2976172190Srrs SCTP_ADDR_NOT_LOCKED); 2977171572Srrs#endif 2978163953Srrs } 2979163953Srrs } 2980218641Srrsclosed_socket: 2981163953Srrs /* Toss the cookie if I can */ 2982163953Srrs sctp_toss_old_cookies(stcb, asoc); 2983163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2984163953Srrs /* Restart the timer if we have pending data */ 2985163953Srrs struct sctp_tmit_chunk *chk; 2986163953Srrs 2987163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2988216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2989163953Srrs } 2990163953Srrs} 2991163953Srrs 2992163953Srrsstatic void 2993163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2994163953Srrs struct sctp_tcb *stcb) 2995163953Srrs{ 2996163953Srrs struct sctp_nets *net; 2997163953Srrs struct sctp_tmit_chunk *lchk; 2998218072Srrs struct sctp_ecne_chunk bkup; 2999228907Stuexen uint8_t override_bit; 3000218072Srrs uint32_t tsn, window_data_tsn; 3001218072Srrs int len; 3002218232Srrs unsigned int pkt_cnt; 3003163953Srrs 3004218072Srrs len = ntohs(cp->ch.chunk_length); 3005218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 3006218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 3007163953Srrs return; 3008163953Srrs } 3009218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 3010218072Srrs /* Its the old format */ 3011218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3012218072Srrs bkup.num_pkts_since_cwr = htonl(1); 3013218072Srrs cp = &bkup; 3014218072Srrs } 3015163953Srrs SCTP_STAT_INCR(sctps_recvecne); 3016163953Srrs tsn = ntohl(cp->tsn); 3017218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3018218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3019163953Srrs if (lchk == NULL) { 3020218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 3021163953Srrs } else { 3022218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 3023163953Srrs } 3024163953Srrs 3025218072Srrs /* Find where it was sent to if possible. */ 3026163953Srrs net = NULL; 3027216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3028163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 3029163953Srrs net = lchk->whoTo; 3030218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3031163953Srrs break; 3032163953Srrs } 3033216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3034163953Srrs break; 3035216825Stuexen } 3036163953Srrs } 3037218072Srrs if (net == NULL) { 3038163953Srrs /* 3039218072Srrs * What to do. A previous send of a CWR was possibly lost. 3040218072Srrs * See how old it is, we may have it marked on the actual 3041218072Srrs * net. 3042218072Srrs */ 3043218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3044218072Srrs if (tsn == net->last_cwr_tsn) { 3045218072Srrs /* Found him, send it off */ 3046228907Stuexen break; 3047218072Srrs } 3048218072Srrs } 3049228907Stuexen if (net == NULL) { 3050228907Stuexen /* 3051228907Stuexen * If we reach here, we need to send a special CWR 3052228907Stuexen * that says hey, we did this a long time ago and 3053228907Stuexen * you lost the response. 3054228907Stuexen */ 3055228907Stuexen net = TAILQ_FIRST(&stcb->asoc.nets); 3056228907Stuexen if (net == NULL) { 3057228907Stuexen /* TSNH */ 3058228907Stuexen return; 3059228907Stuexen } 3060228907Stuexen override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3061228907Stuexen } else { 3062228907Stuexen override_bit = 0; 3063228907Stuexen } 3064228907Stuexen } else { 3065228907Stuexen override_bit = 0; 3066218072Srrs } 3067218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3068218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3069218072Srrs /* 3070171440Srrs * JRS - Use the congestion control given in the pluggable 3071171440Srrs * CC module 3072171440Srrs */ 3073218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3074171440Srrs /* 3075218072Srrs * We reduce once every RTT. So we will only lower cwnd at 3076218072Srrs * the next sending seq i.e. the window_data_tsn 3077163953Srrs */ 3078218072Srrs net->cwr_window_tsn = window_data_tsn; 3079218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3080218072Srrs net->lost_cnt = pkt_cnt; 3081218072Srrs net->last_cwr_tsn = tsn; 3082218072Srrs } else { 3083218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3084218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3085218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3086218072Srrs /* 3087218129Srrs * Another loss in the same window update how many 3088218129Srrs * marks/packets lost we have had. 3089218072Srrs */ 3090218129Srrs int cnt = 1; 3091218072Srrs 3092218072Srrs if (pkt_cnt > net->lost_cnt) { 3093218072Srrs /* Should be the case */ 3094218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3095218129Srrs net->ecn_ce_pkt_cnt += cnt; 3096218072Srrs } 3097218129Srrs net->lost_cnt = pkt_cnt; 3098218072Srrs net->last_cwr_tsn = tsn; 3099218129Srrs /* 3100218129Srrs * Most CC functions will ignore this call, since we 3101218129Srrs * are in-window yet of the initial CE the peer saw. 3102218129Srrs */ 3103218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3104218072Srrs } 3105163953Srrs } 3106163953Srrs /* 3107163953Srrs * We always send a CWR this way if our previous one was lost our 3108163953Srrs * peer will get an update, or if it is not time again to reduce we 3109218072Srrs * still get the cwr to the peer. Note we set the override when we 3110218072Srrs * could not find the TSN on the chunk or the destination network. 3111163953Srrs */ 3112218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3113163953Srrs} 3114163953Srrs 3115163953Srrsstatic void 3116218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3117163953Srrs{ 3118163953Srrs /* 3119163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3120163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3121163953Srrs * If so remove it. 3122163953Srrs */ 3123163953Srrs struct sctp_tmit_chunk *chk; 3124163953Srrs struct sctp_ecne_chunk *ecne; 3125218072Srrs int override; 3126218072Srrs uint32_t cwr_tsn; 3127163953Srrs 3128218072Srrs cwr_tsn = ntohl(cp->tsn); 3129218072Srrs 3130218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3131163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3132163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3133163953Srrs continue; 3134163953Srrs } 3135218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3136218072Srrs /* Must be from the right src unless override is set */ 3137218072Srrs continue; 3138218072Srrs } 3139163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3140218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3141163953Srrs /* this covers this ECNE, we can remove it */ 3142163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3143163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3144163953Srrs sctp_next); 3145163953Srrs if (chk->data) { 3146163953Srrs sctp_m_freem(chk->data); 3147163953Srrs chk->data = NULL; 3148163953Srrs } 3149163953Srrs stcb->asoc.ctrl_queue_cnt--; 3150221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3151218072Srrs if (override == 0) { 3152218072Srrs break; 3153218072Srrs } 3154163953Srrs } 3155163953Srrs } 3156163953Srrs} 3157163953Srrs 3158163953Srrsstatic void 3159228653Stuexensctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3160163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3161163953Srrs{ 3162163953Srrs struct sctp_association *asoc; 3163163953Srrs 3164172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3165172090Srrs struct socket *so; 3166172090Srrs 3167172090Srrs#endif 3168172090Srrs 3169169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3170169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3171163953Srrs if (stcb == NULL) 3172163953Srrs return; 3173163953Srrs 3174163953Srrs asoc = &stcb->asoc; 3175163953Srrs /* process according to association state */ 3176163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3177163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3178171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3179171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3180163953Srrs SCTP_TCB_UNLOCK(stcb); 3181163953Srrs return; 3182163953Srrs } 3183163953Srrs /* notify upper layer protocol */ 3184163953Srrs if (stcb->sctp_socket) { 3185172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3186163953Srrs /* are the queues empty? they should be */ 3187163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3188163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3189217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3190172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3191163953Srrs } 3192163953Srrs } 3193163953Srrs /* stop the timer */ 3194178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3195163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3196163953Srrs /* free the TCB */ 3197171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3198171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3199172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3200172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3201172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3202172090Srrs SCTP_TCB_UNLOCK(stcb); 3203172090Srrs SCTP_SOCKET_LOCK(so, 1); 3204172090Srrs SCTP_TCB_LOCK(stcb); 3205172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3206172090Srrs#endif 3207171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3208172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3209172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3210172090Srrs#endif 3211163953Srrs return; 3212163953Srrs} 3213163953Srrs 3214163953Srrsstatic int 3215163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3216163953Srrs struct sctp_nets *net, uint8_t flg) 3217163953Srrs{ 3218163953Srrs switch (desc->chunk_type) { 3219211969Stuexen case SCTP_DATA: 3220163953Srrs /* find the tsn to resend (possibly */ 3221163953Srrs { 3222163953Srrs uint32_t tsn; 3223163953Srrs struct sctp_tmit_chunk *tp1; 3224163953Srrs 3225163953Srrs tsn = ntohl(desc->tsn_ifany); 3226216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3227163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3228163953Srrs /* found it */ 3229163953Srrs break; 3230163953Srrs } 3231216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3232163953Srrs /* not found */ 3233163953Srrs tp1 = NULL; 3234163953Srrs break; 3235163953Srrs } 3236163953Srrs } 3237163953Srrs if (tp1 == NULL) { 3238163953Srrs /* 3239163953Srrs * Do it the other way , aka without paying 3240163953Srrs * attention to queue seq order. 3241163953Srrs */ 3242163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3243216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3244163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3245163953Srrs /* found it */ 3246163953Srrs break; 3247163953Srrs } 3248163953Srrs } 3249163953Srrs } 3250163953Srrs if (tp1 == NULL) { 3251163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3252163953Srrs } 3253163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3254163953Srrs uint8_t *ddp; 3255163953Srrs 3256214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3257214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3258214877Stuexen return (0); 3259214877Stuexen } 3260163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3261163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3262163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3263163953Srrs return (0); 3264163953Srrs } 3265163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3266163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3267163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3268163953Srrs return (0); 3269163953Srrs } 3270163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3271163953Srrs sizeof(struct sctp_data_chunk)); 3272163953Srrs { 3273163953Srrs unsigned int iii; 3274163953Srrs 3275163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3276163953Srrs iii++) { 3277163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3278163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3279163953Srrs return (-1); 3280163953Srrs } 3281163953Srrs } 3282163953Srrs } 3283163953Srrs 3284163953Srrs if (tp1->do_rtt) { 3285163953Srrs /* 3286163953Srrs * this guy had a RTO calculation 3287163953Srrs * pending on it, cancel it 3288163953Srrs */ 3289219397Srrs if (tp1->whoTo->rto_needed == 0) { 3290219397Srrs tp1->whoTo->rto_needed = 1; 3291219397Srrs } 3292163953Srrs tp1->do_rtt = 0; 3293163953Srrs } 3294163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3295163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3296163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3297163953Srrs /* 3298163953Srrs * mark it as if we were doing a FR, since 3299163953Srrs * we will be getting gap ack reports behind 3300163953Srrs * the info from the router. 3301163953Srrs */ 3302163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3303163953Srrs /* 3304163953Srrs * mark the tsn with what sequences can 3305163953Srrs * cause a new FR. 3306163953Srrs */ 3307163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3308163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3309163953Srrs } else { 3310163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3311163953Srrs } 3312163953Srrs 3313163953Srrs /* restart the timer */ 3314163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3315171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3316163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3317163953Srrs stcb, tp1->whoTo); 3318163953Srrs 3319163953Srrs /* fix counts and things */ 3320179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3321170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3322170744Srrs tp1->whoTo->flight_size, 3323170744Srrs tp1->book_size, 3324170744Srrs (uintptr_t) stcb, 3325170744Srrs tp1->rec.data.TSN_seq); 3326170744Srrs } 3327190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3328190689Srrs sctp_flight_size_decrease(tp1); 3329190689Srrs sctp_total_flight_decrease(stcb, tp1); 3330190689Srrs } 3331216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3332163953Srrs } { 3333163953Srrs /* audit code */ 3334163953Srrs unsigned int audit; 3335163953Srrs 3336163953Srrs audit = 0; 3337163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3338163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3339163953Srrs audit++; 3340163953Srrs } 3341163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3342163953Srrs sctp_next) { 3343163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3344163953Srrs audit++; 3345163953Srrs } 3346163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3347169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3348163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3349163953Srrs#ifndef SCTP_AUDITING_ENABLED 3350163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3351163953Srrs#endif 3352163953Srrs } 3353163953Srrs } 3354163953Srrs } 3355163953Srrs break; 3356163953Srrs case SCTP_ASCONF: 3357163953Srrs { 3358163953Srrs struct sctp_tmit_chunk *asconf; 3359163953Srrs 3360163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3361163953Srrs sctp_next) { 3362163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3363163953Srrs break; 3364163953Srrs } 3365163953Srrs } 3366163953Srrs if (asconf) { 3367163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3368163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3369163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3370163953Srrs asconf->snd_count--; 3371163953Srrs } 3372163953Srrs } 3373163953Srrs break; 3374163953Srrs case SCTP_INITIATION: 3375163953Srrs /* resend the INIT */ 3376163953Srrs stcb->asoc.dropped_special_cnt++; 3377163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3378163953Srrs /* 3379163953Srrs * If we can get it in, in a few attempts we do 3380163953Srrs * this, otherwise we let the timer fire. 3381163953Srrs */ 3382163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3383171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3384172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3385163953Srrs } 3386163953Srrs break; 3387163953Srrs case SCTP_SELECTIVE_ACK: 3388206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3389163953Srrs /* resend the sack */ 3390221627Stuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3391163953Srrs break; 3392163953Srrs case SCTP_HEARTBEAT_REQUEST: 3393163953Srrs /* resend a demand HB */ 3394171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3395171440Srrs /* 3396171440Srrs * Only retransmit if we KNOW we wont destroy the 3397171440Srrs * tcb 3398171440Srrs */ 3399224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3400171440Srrs } 3401163953Srrs break; 3402163953Srrs case SCTP_SHUTDOWN: 3403163953Srrs sctp_send_shutdown(stcb, net); 3404163953Srrs break; 3405163953Srrs case SCTP_SHUTDOWN_ACK: 3406163953Srrs sctp_send_shutdown_ack(stcb, net); 3407163953Srrs break; 3408163953Srrs case SCTP_COOKIE_ECHO: 3409163953Srrs { 3410163953Srrs struct sctp_tmit_chunk *cookie; 3411163953Srrs 3412163953Srrs cookie = NULL; 3413163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3414163953Srrs sctp_next) { 3415163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3416163953Srrs break; 3417163953Srrs } 3418163953Srrs } 3419163953Srrs if (cookie) { 3420163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3421163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3422163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3423163953Srrs sctp_stop_all_cookie_timers(stcb); 3424163953Srrs } 3425163953Srrs } 3426163953Srrs break; 3427163953Srrs case SCTP_COOKIE_ACK: 3428163953Srrs sctp_send_cookie_ack(stcb); 3429163953Srrs break; 3430163953Srrs case SCTP_ASCONF_ACK: 3431163953Srrs /* resend last asconf ack */ 3432171990Srrs sctp_send_asconf_ack(stcb); 3433163953Srrs break; 3434163953Srrs case SCTP_FORWARD_CUM_TSN: 3435163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3436163953Srrs break; 3437163953Srrs /* can't do anything with these */ 3438163953Srrs case SCTP_PACKET_DROPPED: 3439163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3440163953Srrs case SCTP_HEARTBEAT_ACK: 3441163953Srrs case SCTP_ABORT_ASSOCIATION: 3442163953Srrs case SCTP_OPERATION_ERROR: 3443163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3444163953Srrs case SCTP_ECN_ECHO: 3445163953Srrs case SCTP_ECN_CWR: 3446163953Srrs default: 3447163953Srrs break; 3448163953Srrs } 3449163953Srrs return (0); 3450163953Srrs} 3451163953Srrs 3452163953Srrsvoid 3453163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3454163953Srrs{ 3455163953Srrs int i; 3456163953Srrs uint16_t temp; 3457163953Srrs 3458163953Srrs /* 3459163953Srrs * We set things to 0xffff since this is the last delivered sequence 3460163953Srrs * and we will be sending in 0 after the reset. 3461163953Srrs */ 3462163953Srrs 3463163953Srrs if (number_entries) { 3464163953Srrs for (i = 0; i < number_entries; i++) { 3465163953Srrs temp = ntohs(list[i]); 3466163953Srrs if (temp >= stcb->asoc.streamincnt) { 3467163953Srrs continue; 3468163953Srrs } 3469163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3470163953Srrs } 3471163953Srrs } else { 3472163953Srrs list = NULL; 3473163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3474163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3475163953Srrs } 3476163953Srrs } 3477172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3478163953Srrs} 3479163953Srrs 3480163953Srrsstatic void 3481163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3482163953Srrs{ 3483163953Srrs int i; 3484163953Srrs 3485163953Srrs if (number_entries == 0) { 3486163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3487163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3488163953Srrs } 3489163953Srrs } else if (number_entries) { 3490163953Srrs for (i = 0; i < number_entries; i++) { 3491163953Srrs uint16_t temp; 3492163953Srrs 3493163953Srrs temp = ntohs(list[i]); 3494163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3495163953Srrs /* no such stream */ 3496163953Srrs continue; 3497163953Srrs } 3498163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3499163953Srrs } 3500163953Srrs } 3501172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3502163953Srrs} 3503163953Srrs 3504163953Srrs 3505163953Srrsstruct sctp_stream_reset_out_request * 3506163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3507163953Srrs{ 3508163953Srrs struct sctp_association *asoc; 3509163953Srrs struct sctp_stream_reset_out_req *req; 3510163953Srrs struct sctp_stream_reset_out_request *r; 3511163953Srrs struct sctp_tmit_chunk *chk; 3512163953Srrs int len, clen; 3513163953Srrs 3514163953Srrs asoc = &stcb->asoc; 3515163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3516169208Srrs asoc->stream_reset_outstanding = 0; 3517163953Srrs return (NULL); 3518163953Srrs } 3519163953Srrs if (stcb->asoc.str_reset == NULL) { 3520169208Srrs asoc->stream_reset_outstanding = 0; 3521163953Srrs return (NULL); 3522163953Srrs } 3523163953Srrs chk = stcb->asoc.str_reset; 3524163953Srrs if (chk->data == NULL) { 3525163953Srrs return (NULL); 3526163953Srrs } 3527163953Srrs if (bchk) { 3528163953Srrs /* he wants a copy of the chk pointer */ 3529163953Srrs *bchk = chk; 3530163953Srrs } 3531163953Srrs clen = chk->send_size; 3532163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3533163953Srrs r = &req->sr_req; 3534163953Srrs if (ntohl(r->request_seq) == seq) { 3535163953Srrs /* found it */ 3536163953Srrs return (r); 3537163953Srrs } 3538163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3539163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3540163953Srrs /* move to the next one, there can only be a max of two */ 3541163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3542163953Srrs if (ntohl(r->request_seq) == seq) { 3543163953Srrs return (r); 3544163953Srrs } 3545163953Srrs } 3546163953Srrs /* that seq is not here */ 3547163953Srrs return (NULL); 3548163953Srrs} 3549163953Srrs 3550163953Srrsstatic void 3551163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3552163953Srrs{ 3553163953Srrs struct sctp_association *asoc; 3554163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3555163953Srrs 3556163953Srrs if (stcb->asoc.str_reset == NULL) { 3557163953Srrs return; 3558163953Srrs } 3559166675Srrs asoc = &stcb->asoc; 3560166675Srrs 3561171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3562163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3563163953Srrs chk, 3564163953Srrs sctp_next); 3565163953Srrs if (chk->data) { 3566163953Srrs sctp_m_freem(chk->data); 3567163953Srrs chk->data = NULL; 3568163953Srrs } 3569163953Srrs asoc->ctrl_queue_cnt--; 3570221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3571172156Srrs /* sa_ignore NO_NULL_CHK */ 3572163953Srrs stcb->asoc.str_reset = NULL; 3573163953Srrs} 3574163953Srrs 3575163953Srrs 3576163953Srrsstatic int 3577163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3578163953Srrs uint32_t seq, uint32_t action, 3579164139Srrs struct sctp_stream_reset_response *respin) 3580163953Srrs{ 3581163953Srrs uint16_t type; 3582163953Srrs int lparm_len; 3583163953Srrs struct sctp_association *asoc = &stcb->asoc; 3584163953Srrs struct sctp_tmit_chunk *chk; 3585163953Srrs struct sctp_stream_reset_out_request *srparam; 3586163953Srrs int number_entries; 3587163953Srrs 3588163953Srrs if (asoc->stream_reset_outstanding == 0) { 3589163953Srrs /* duplicate */ 3590163953Srrs return (0); 3591163953Srrs } 3592163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3593163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3594163953Srrs if (srparam) { 3595163953Srrs stcb->asoc.str_reset_seq_out++; 3596163953Srrs type = ntohs(srparam->ph.param_type); 3597163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3598163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3599164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3600163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3601163953Srrs if (asoc->stream_reset_outstanding) 3602163953Srrs asoc->stream_reset_outstanding--; 3603163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3604163953Srrs /* do it */ 3605163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3606163953Srrs } else { 3607172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3608163953Srrs } 3609163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3610163953Srrs /* Answered my request */ 3611164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3612163953Srrs if (asoc->stream_reset_outstanding) 3613163953Srrs asoc->stream_reset_outstanding--; 3614163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3615172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3616163953Srrs } 3617188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3618188854Srrs /* Ok we now may have more streams */ 3619189121Srrs if (asoc->stream_reset_outstanding) 3620189121Srrs asoc->stream_reset_outstanding--; 3621188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3622188854Srrs /* Put the new streams into effect */ 3623188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3624188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3625188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3626188854Srrs } else { 3627188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3628188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3629188854Srrs } 3630163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3631163953Srrs /** 3632163953Srrs * a) Adopt the new in tsn. 3633163953Srrs * b) reset the map 3634163953Srrs * c) Adopt the new out-tsn 3635163953Srrs */ 3636163953Srrs struct sctp_stream_reset_response_tsn *resp; 3637163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3638163953Srrs int abort_flag = 0; 3639163953Srrs 3640163953Srrs if (respin == NULL) { 3641163953Srrs /* huh ? */ 3642163953Srrs return (0); 3643163953Srrs } 3644163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3645163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3646163953Srrs asoc->stream_reset_outstanding--; 3647163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3648163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3649163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3650170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3651163953Srrs if (abort_flag) { 3652163953Srrs return (1); 3653163953Srrs } 3654163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3655179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3656179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3657179783Srrs } 3658180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3659163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3660163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3661185694Srrs 3662205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3663206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3664205627Srrs 3665163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3666163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3667163953Srrs 3668163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3669163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3670163953Srrs 3671163953Srrs } 3672163953Srrs } 3673163953Srrs /* get rid of the request and get the request flags */ 3674163953Srrs if (asoc->stream_reset_outstanding == 0) { 3675163953Srrs sctp_clean_up_stream_reset(stcb); 3676163953Srrs } 3677163953Srrs } 3678163953Srrs } 3679163953Srrs return (0); 3680163953Srrs} 3681163953Srrs 3682163953Srrsstatic void 3683163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3684163953Srrs struct sctp_tmit_chunk *chk, 3685170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3686163953Srrs{ 3687163953Srrs uint32_t seq; 3688163953Srrs int len, i; 3689163953Srrs int number_entries; 3690163953Srrs uint16_t temp; 3691163953Srrs 3692163953Srrs /* 3693163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3694163953Srrs * seq_in is right. 3695163953Srrs */ 3696163953Srrs struct sctp_association *asoc = &stcb->asoc; 3697163953Srrs 3698163953Srrs seq = ntohl(req->request_seq); 3699163953Srrs if (asoc->str_reset_seq_in == seq) { 3700170992Srrs if (trunc) { 3701170992Srrs /* Can't do it, since they exceeded our buffer size */ 3702170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3703170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3704170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3705170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3706163953Srrs len = ntohs(req->ph.param_length); 3707163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3708163953Srrs for (i = 0; i < number_entries; i++) { 3709163953Srrs temp = ntohs(req->list_of_streams[i]); 3710163953Srrs req->list_of_streams[i] = temp; 3711163953Srrs } 3712163953Srrs /* move the reset action back one */ 3713163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3714163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3715163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3716163953Srrs asoc->str_reset_seq_out, 3717163953Srrs seq, (asoc->sending_seq - 1)); 3718163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3719163953Srrs asoc->str_reset = chk; 3720163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3721163953Srrs stcb->asoc.stream_reset_outstanding++; 3722163953Srrs } else { 3723163953Srrs /* Can't do it, since we have sent one out */ 3724163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3725163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3726163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3727163953Srrs } 3728163953Srrs asoc->str_reset_seq_in++; 3729163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3730163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3731163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3732163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3733163953Srrs } else { 3734163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3735163953Srrs } 3736163953Srrs} 3737163953Srrs 3738163953Srrsstatic int 3739163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3740163953Srrs struct sctp_tmit_chunk *chk, 3741163953Srrs struct sctp_stream_reset_tsn_request *req) 3742163953Srrs{ 3743163953Srrs /* reset all in and out and update the tsn */ 3744163953Srrs /* 3745163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3746163953Srrs * and set cum-ack to it. Also process this selected number as a 3747163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3748163953Srrs */ 3749163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3750163953Srrs struct sctp_association *asoc = &stcb->asoc; 3751163953Srrs int abort_flag = 0; 3752163953Srrs uint32_t seq; 3753163953Srrs 3754163953Srrs seq = ntohl(req->request_seq); 3755163953Srrs if (asoc->str_reset_seq_in == seq) { 3756163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3757163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3758163953Srrs fwdtsn.ch.chunk_flags = 0; 3759163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3760170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3761163953Srrs if (abort_flag) { 3762163953Srrs return (1); 3763163953Srrs } 3764163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3765179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3766179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3767179783Srrs } 3768180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3769163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3770163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3771205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3772206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3773163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3774163953Srrs /* save off historical data for retrans */ 3775163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3776163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3777163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3778163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3779163953Srrs 3780163953Srrs sctp_add_stream_reset_result_tsn(chk, 3781163953Srrs ntohl(req->request_seq), 3782163953Srrs SCTP_STREAM_RESET_PERFORMED, 3783163953Srrs stcb->asoc.sending_seq, 3784163953Srrs stcb->asoc.mapping_array_base_tsn); 3785163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3786163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3787163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3788163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3789163953Srrs 3790163953Srrs asoc->str_reset_seq_in++; 3791163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3792163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3793163953Srrs stcb->asoc.last_sending_seq[0], 3794163953Srrs stcb->asoc.last_base_tsnsent[0] 3795163953Srrs ); 3796163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3797163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3798163953Srrs stcb->asoc.last_sending_seq[1], 3799163953Srrs stcb->asoc.last_base_tsnsent[1] 3800163953Srrs ); 3801163953Srrs } else { 3802163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3803163953Srrs } 3804163953Srrs return (0); 3805163953Srrs} 3806163953Srrs 3807163953Srrsstatic void 3808163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3809163953Srrs struct sctp_tmit_chunk *chk, 3810170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3811163953Srrs{ 3812163953Srrs uint32_t seq, tsn; 3813163953Srrs int number_entries, len; 3814163953Srrs struct sctp_association *asoc = &stcb->asoc; 3815163953Srrs 3816163953Srrs seq = ntohl(req->request_seq); 3817163953Srrs 3818163953Srrs /* now if its not a duplicate we process it */ 3819163953Srrs if (asoc->str_reset_seq_in == seq) { 3820163953Srrs len = ntohs(req->ph.param_length); 3821163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3822163953Srrs /* 3823163953Srrs * the sender is resetting, handle the list issue.. we must 3824163953Srrs * a) verify if we can do the reset, if so no problem b) If 3825163953Srrs * we can't do the reset we must copy the request. c) queue 3826163953Srrs * it, and setup the data in processor to trigger it off 3827163953Srrs * when needed and dequeue all the queued data. 3828163953Srrs */ 3829163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3830163953Srrs 3831163953Srrs /* move the reset action back one */ 3832163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3833170992Srrs if (trunc) { 3834170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3835170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3836216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3837163953Srrs /* we can do it now */ 3838163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3839163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3840163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3841163953Srrs } else { 3842163953Srrs /* 3843163953Srrs * we must queue it up and thus wait for the TSN's 3844163953Srrs * to arrive that are at or before tsn 3845163953Srrs */ 3846163953Srrs struct sctp_stream_reset_list *liste; 3847163953Srrs int siz; 3848163953Srrs 3849163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3850163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3851170091Srrs siz, SCTP_M_STRESET); 3852163953Srrs if (liste == NULL) { 3853163953Srrs /* gak out of memory */ 3854163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3855163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3856163953Srrs return; 3857163953Srrs } 3858163953Srrs liste->tsn = tsn; 3859163953Srrs liste->number_entries = number_entries; 3860163953Srrs memcpy(&liste->req, req, 3861163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3862163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3863163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3864163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3865163953Srrs } 3866163953Srrs asoc->str_reset_seq_in++; 3867163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3868163953Srrs /* 3869163953Srrs * one seq back, just echo back last action since my 3870163953Srrs * response was lost. 3871163953Srrs */ 3872163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3873163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3874163953Srrs /* 3875163953Srrs * two seq back, just echo back last action since my 3876163953Srrs * response was lost. 3877163953Srrs */ 3878163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3879163953Srrs } else { 3880163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3881163953Srrs } 3882163953Srrs} 3883163953Srrs 3884188854Srrsstatic void 3885188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3886188854Srrs struct sctp_stream_reset_add_strm *str_add) 3887188854Srrs{ 3888188854Srrs /* 3889188854Srrs * Peer is requesting to add more streams. If its within our 3890188854Srrs * max-streams we will allow it. 3891188854Srrs */ 3892188854Srrs uint16_t num_stream, i; 3893188854Srrs uint32_t seq; 3894189121Srrs struct sctp_association *asoc = &stcb->asoc; 3895216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3896188854Srrs 3897188854Srrs /* Get the number. */ 3898188854Srrs seq = ntohl(str_add->request_seq); 3899188854Srrs num_stream = ntohs(str_add->number_of_streams); 3900188854Srrs /* Now what would be the new total? */ 3901189121Srrs if (asoc->str_reset_seq_in == seq) { 3902189121Srrs num_stream += stcb->asoc.streamincnt; 3903189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3904189121Srrs /* We must reject it they ask for to many */ 3905189121Srrs denied: 3906189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3907189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3908189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3909189121Srrs } else { 3910189121Srrs /* Ok, we can do that :-) */ 3911189121Srrs struct sctp_stream_in *oldstrm; 3912189121Srrs 3913189121Srrs /* save off the old */ 3914189121Srrs oldstrm = stcb->asoc.strmin; 3915189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3916189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3917189121Srrs SCTP_M_STRMI); 3918189121Srrs if (stcb->asoc.strmin == NULL) { 3919189121Srrs stcb->asoc.strmin = oldstrm; 3920189121Srrs goto denied; 3921189121Srrs } 3922189121Srrs /* copy off the old data */ 3923189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3924189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3925189121Srrs stcb->asoc.strmin[i].stream_no = i; 3926189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3927189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3928189121Srrs /* now anything on those queues? */ 3929216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3930189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3931189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3932189121Srrs } 3933189121Srrs } 3934189121Srrs /* Init the new streams */ 3935189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3936189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3937189121Srrs stcb->asoc.strmin[i].stream_no = i; 3938189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3939189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3940189121Srrs } 3941189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3942189121Srrs /* update the size */ 3943189121Srrs stcb->asoc.streamincnt = num_stream; 3944189121Srrs /* Send the ack */ 3945189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3946189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3947189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3948189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3949189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3950189121Srrs } 3951189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3952189121Srrs /* 3953189121Srrs * one seq back, just echo back last action since my 3954189121Srrs * response was lost. 3955189121Srrs */ 3956189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3957189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3958189121Srrs /* 3959189121Srrs * two seq back, just echo back last action since my 3960189121Srrs * response was lost. 3961189121Srrs */ 3962189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3963188854Srrs } else { 3964189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3965188854Srrs 3966188854Srrs } 3967188854Srrs} 3968188854Srrs 3969170992Srrs#ifdef __GNUC__ 3970170992Srrs__attribute__((noinline)) 3971170992Srrs#endif 3972170992Srrs static int 3973170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3974170992Srrs struct sctp_stream_reset_out_req *sr_req) 3975163953Srrs{ 3976163953Srrs int chk_length, param_len, ptype; 3977170992Srrs struct sctp_paramhdr pstore; 3978170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3979170992Srrs 3980163953Srrs uint32_t seq; 3981163953Srrs int num_req = 0; 3982170992Srrs int trunc = 0; 3983163953Srrs struct sctp_tmit_chunk *chk; 3984163953Srrs struct sctp_chunkhdr *ch; 3985163953Srrs struct sctp_paramhdr *ph; 3986166675Srrs int ret_code = 0; 3987166675Srrs int num_param = 0; 3988163953Srrs 3989163953Srrs /* now it may be a reset or a reset-response */ 3990163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3991163953Srrs 3992163953Srrs /* setup for adding the response */ 3993163953Srrs sctp_alloc_a_chunk(stcb, chk); 3994163953Srrs if (chk == NULL) { 3995163953Srrs return (ret_code); 3996163953Srrs } 3997163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3998169208Srrs chk->rec.chunk_id.can_take_data = 0; 3999163953Srrs chk->asoc = &stcb->asoc; 4000163953Srrs chk->no_fr_allowed = 0; 4001163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4002166023Srrs chk->book_size_scale = 0; 4003165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 4004163953Srrs if (chk->data == NULL) { 4005163953Srrsstrres_nochunk: 4006163953Srrs if (chk->data) { 4007163953Srrs sctp_m_freem(chk->data); 4008163953Srrs chk->data = NULL; 4009163953Srrs } 4010221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4011163953Srrs return (ret_code); 4012163953Srrs } 4013165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4014163953Srrs 4015163953Srrs /* setup chunk parameters */ 4016163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 4017163953Srrs chk->snd_count = 0; 4018224641Stuexen chk->whoTo = NULL; 4019163953Srrs 4020163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 4021163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 4022163953Srrs ch->chunk_flags = 0; 4023163953Srrs ch->chunk_length = htons(chk->send_size); 4024165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4025170992Srrs offset += sizeof(struct sctp_chunkhdr); 4026163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4027170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4028170992Srrs if (ph == NULL) 4029170992Srrs break; 4030163953Srrs param_len = ntohs(ph->param_length); 4031163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4032163953Srrs /* bad param */ 4033163953Srrs break; 4034163953Srrs } 4035170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4036170992Srrs (uint8_t *) & cstore); 4037163953Srrs ptype = ntohs(ph->param_type); 4038163953Srrs num_param++; 4039170994Srrs if (param_len > (int)sizeof(cstore)) { 4040170992Srrs trunc = 1; 4041170992Srrs } else { 4042170992Srrs trunc = 0; 4043170992Srrs } 4044170992Srrs 4045163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 4046163953Srrs /* hit the max of parameters already sorry.. */ 4047163953Srrs break; 4048163953Srrs } 4049163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4050163953Srrs struct sctp_stream_reset_out_request *req_out; 4051163953Srrs 4052163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 4053163953Srrs num_req++; 4054163953Srrs if (stcb->asoc.stream_reset_outstanding) { 4055163953Srrs seq = ntohl(req_out->response_seq); 4056163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 4057163953Srrs /* implicit ack */ 4058169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 4059163953Srrs } 4060163953Srrs } 4061170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4062188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 4063188854Srrs struct sctp_stream_reset_add_strm *str_add; 4064188854Srrs 4065188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4066188854Srrs num_req++; 4067188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4068163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4069163953Srrs struct sctp_stream_reset_in_request *req_in; 4070163953Srrs 4071163953Srrs num_req++; 4072170992Srrs 4073163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 4074170992Srrs 4075170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4076163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4077163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 4078163953Srrs 4079163953Srrs num_req++; 4080163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4081170992Srrs 4082163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4083163953Srrs ret_code = 1; 4084163953Srrs goto strres_nochunk; 4085163953Srrs } 4086163953Srrs /* no more */ 4087163953Srrs break; 4088163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4089163953Srrs struct sctp_stream_reset_response *resp; 4090163953Srrs uint32_t result; 4091163953Srrs 4092163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4093163953Srrs seq = ntohl(resp->response_seq); 4094163953Srrs result = ntohl(resp->result); 4095163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4096163953Srrs ret_code = 1; 4097163953Srrs goto strres_nochunk; 4098163953Srrs } 4099163953Srrs } else { 4100163953Srrs break; 4101163953Srrs } 4102170992Srrs offset += SCTP_SIZE32(param_len); 4103163953Srrs chk_length -= SCTP_SIZE32(param_len); 4104163953Srrs } 4105163953Srrs if (num_req == 0) { 4106163953Srrs /* we have no response free the stuff */ 4107163953Srrs goto strres_nochunk; 4108163953Srrs } 4109163953Srrs /* ok we have a chunk to link in */ 4110163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4111163953Srrs chk, 4112163953Srrs sctp_next); 4113163953Srrs stcb->asoc.ctrl_queue_cnt++; 4114163953Srrs return (ret_code); 4115163953Srrs} 4116163953Srrs 4117163953Srrs/* 4118163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4119163953Srrs * to handle this, either we get the whole packet and must disect it 4120163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4121163953Srrs * from a middle box that did the disectting for us. 4122163953Srrs */ 4123163953Srrsstatic void 4124163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4125170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4126163953Srrs{ 4127163953Srrs uint32_t bottle_bw, on_queue; 4128163953Srrs uint16_t trunc_len; 4129163953Srrs unsigned int chlen; 4130163953Srrs unsigned int at; 4131163953Srrs struct sctp_chunk_desc desc; 4132163953Srrs struct sctp_chunkhdr *ch; 4133163953Srrs 4134163953Srrs chlen = ntohs(cp->ch.chunk_length); 4135163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4136163953Srrs /* XXX possible chlen underflow */ 4137163953Srrs if (chlen == 0) { 4138163953Srrs ch = NULL; 4139163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4140163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4141163953Srrs } else { 4142163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4143163953Srrs chlen -= sizeof(struct sctphdr); 4144163953Srrs /* XXX possible chlen underflow */ 4145163953Srrs memset(&desc, 0, sizeof(desc)); 4146163953Srrs } 4147163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4148170781Srrs if (trunc_len > limit) { 4149170781Srrs trunc_len = limit; 4150170781Srrs } 4151163953Srrs /* now the chunks themselves */ 4152163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4153163953Srrs desc.chunk_type = ch->chunk_type; 4154163953Srrs /* get amount we need to move */ 4155163953Srrs at = ntohs(ch->chunk_length); 4156163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4157163953Srrs /* corrupt chunk, maybe at the end? */ 4158163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4159163953Srrs break; 4160163953Srrs } 4161163953Srrs if (trunc_len == 0) { 4162163953Srrs /* we are supposed to have all of it */ 4163163953Srrs if (at > chlen) { 4164163953Srrs /* corrupt skip it */ 4165163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4166163953Srrs break; 4167163953Srrs } 4168163953Srrs } else { 4169163953Srrs /* is there enough of it left ? */ 4170163953Srrs if (desc.chunk_type == SCTP_DATA) { 4171163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4172163953Srrs sizeof(desc.data_bytes))) { 4173163953Srrs break; 4174163953Srrs } 4175163953Srrs } else { 4176163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4177163953Srrs break; 4178163953Srrs } 4179163953Srrs } 4180163953Srrs } 4181163953Srrs if (desc.chunk_type == SCTP_DATA) { 4182163953Srrs /* can we get out the tsn? */ 4183163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4184163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4185163953Srrs 4186163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4187163953Srrs /* yep */ 4188163953Srrs struct sctp_data_chunk *dcp; 4189163953Srrs uint8_t *ddp; 4190163953Srrs unsigned int iii; 4191163953Srrs 4192163953Srrs dcp = (struct sctp_data_chunk *)ch; 4193163953Srrs ddp = (uint8_t *) (dcp + 1); 4194163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4195163953Srrs desc.data_bytes[iii] = ddp[iii]; 4196163953Srrs } 4197163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4198163953Srrs } else { 4199163953Srrs /* nope we are done. */ 4200163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4201163953Srrs break; 4202163953Srrs } 4203163953Srrs } else { 4204163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4205163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4206163953Srrs } 4207163953Srrs 4208163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4209163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4210163953Srrs break; 4211163953Srrs } 4212163953Srrs if (SCTP_SIZE32(at) > chlen) { 4213163953Srrs break; 4214163953Srrs } 4215163953Srrs chlen -= SCTP_SIZE32(at); 4216163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4217163953Srrs /* done, none left */ 4218163953Srrs break; 4219163953Srrs } 4220163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4221163953Srrs } 4222163953Srrs /* Now update any rwnd --- possibly */ 4223163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4224163953Srrs /* From a peer, we get a rwnd report */ 4225163953Srrs uint32_t a_rwnd; 4226163953Srrs 4227163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4228163953Srrs 4229163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4230163953Srrs on_queue = ntohl(cp->current_onq); 4231163953Srrs if (bottle_bw && on_queue) { 4232163953Srrs /* a rwnd report is in here */ 4233163953Srrs if (bottle_bw > on_queue) 4234163953Srrs a_rwnd = bottle_bw - on_queue; 4235163953Srrs else 4236163953Srrs a_rwnd = 0; 4237163953Srrs 4238163953Srrs if (a_rwnd == 0) 4239163953Srrs stcb->asoc.peers_rwnd = 0; 4240163953Srrs else { 4241163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4242163953Srrs stcb->asoc.peers_rwnd = 4243163953Srrs a_rwnd - stcb->asoc.total_flight; 4244163953Srrs } else { 4245163953Srrs stcb->asoc.peers_rwnd = 0; 4246163953Srrs } 4247163953Srrs if (stcb->asoc.peers_rwnd < 4248163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4249163953Srrs /* SWS sender side engages */ 4250163953Srrs stcb->asoc.peers_rwnd = 0; 4251163953Srrs } 4252163953Srrs } 4253163953Srrs } 4254163953Srrs } else { 4255163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4256163953Srrs } 4257163953Srrs 4258163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4259163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4260163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4261163953Srrs (stcb->asoc.sat_network)) { 4262163953Srrs /* 4263163953Srrs * This is debateable but for sat networks it makes sense 4264163953Srrs * Note if a T3 timer has went off, we will prohibit any 4265163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4266163953Srrs */ 4267171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4268171440Srrs net, cp, &bottle_bw, &on_queue); 4269163953Srrs } 4270163953Srrs} 4271163953Srrs 4272163953Srrs/* 4273163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4274163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4275163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4276163953Srrs * length of the complete packet outputs: - length: modified to remaining 4277163953Srrs * length after control processing - netp: modified to new sctp_nets after 4278163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4279163953Srrs * bad packet,...) otherwise return the tcb for this packet 4280163953Srrs */ 4281170099Srrs#ifdef __GNUC__ 4282170099Srrs__attribute__((noinline)) 4283170099Srrs#endif 4284170099Srrs static struct sctp_tcb * 4285170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4286170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4287170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4288179157Srrs uint32_t vrf_id, uint16_t port) 4289163953Srrs{ 4290163953Srrs struct sctp_association *asoc; 4291163953Srrs uint32_t vtag_in; 4292163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4293170140Srrs uint32_t chk_length; 4294163953Srrs int ret; 4295168299Srrs int abort_no_unlock = 0; 4296218186Srrs int ecne_seen = 0; 4297163953Srrs 4298163953Srrs /* 4299163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4300163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4301163953Srrs * until we get into jumbo grams and such.. 4302163953Srrs */ 4303166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4304163953Srrs struct sctp_tcb *locked_tcb = stcb; 4305163953Srrs int got_auth = 0; 4306163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4307163953Srrs int auth_skipped = 0; 4308171990Srrs int asconf_cnt = 0; 4309163953Srrs 4310172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4311172090Srrs struct socket *so; 4312172090Srrs 4313172090Srrs#endif 4314172090Srrs 4315169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4316169420Srrs iphlen, *offset, length, stcb); 4317163953Srrs 4318163953Srrs /* validate chunk header length... */ 4319163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4320170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4321170056Srrs ntohs(ch->chunk_length)); 4322170091Srrs if (locked_tcb) { 4323170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4324170091Srrs } 4325163953Srrs return (NULL); 4326163953Srrs } 4327163953Srrs /* 4328163953Srrs * validate the verification tag 4329163953Srrs */ 4330163953Srrs vtag_in = ntohl(sh->v_tag); 4331163953Srrs 4332165220Srrs if (locked_tcb) { 4333165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4334165220Srrs } 4335163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4336170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4337170056Srrs ntohs(ch->chunk_length), vtag_in); 4338163953Srrs if (vtag_in != 0) { 4339163953Srrs /* protocol error- silently discard... */ 4340163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4341169378Srrs if (locked_tcb) { 4342163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4343169378Srrs } 4344163953Srrs return (NULL); 4345163953Srrs } 4346163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4347163953Srrs /* 4348163953Srrs * If there is no stcb, skip the AUTH chunk and process 4349163953Srrs * later after a stcb is found (to validate the lookup was 4350163953Srrs * valid. 4351163953Srrs */ 4352163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4353179783Srrs (stcb == NULL) && 4354179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4355163953Srrs /* save this chunk for later processing */ 4356163953Srrs auth_skipped = 1; 4357163953Srrs auth_offset = *offset; 4358163953Srrs auth_len = ntohs(ch->chunk_length); 4359163953Srrs 4360163953Srrs /* (temporarily) move past this chunk */ 4361163953Srrs *offset += SCTP_SIZE32(auth_len); 4362163953Srrs if (*offset >= length) { 4363163953Srrs /* no more data left in the mbuf chain */ 4364163953Srrs *offset = length; 4365170091Srrs if (locked_tcb) { 4366170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4367170091Srrs } 4368163953Srrs return (NULL); 4369163953Srrs } 4370163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4371163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4372163953Srrs } 4373169420Srrs if (ch == NULL) { 4374169420Srrs /* Help */ 4375169420Srrs *offset = length; 4376170091Srrs if (locked_tcb) { 4377170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4378170091Srrs } 4379169420Srrs return (NULL); 4380169420Srrs } 4381163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4382163953Srrs goto process_control_chunks; 4383163953Srrs } 4384163953Srrs /* 4385163953Srrs * first check if it's an ASCONF with an unknown src addr we 4386163953Srrs * need to look inside to find the association 4387163953Srrs */ 4388163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4389171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4390171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4391171990Srrs 4392163953Srrs /* inp's refcount may be reduced */ 4393163953Srrs SCTP_INP_INCR_REF(inp); 4394163953Srrs 4395171990Srrs asconf_offset = *offset; 4396171990Srrs do { 4397171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4398171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4399171990Srrs break; 4400228653Stuexen stcb = sctp_findassociation_ep_asconf(m, 4401185694Srrs *offset, sh, &inp, netp, vrf_id); 4402171990Srrs if (stcb != NULL) 4403171990Srrs break; 4404171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4405171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4406171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4407171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4408163953Srrs if (stcb == NULL) { 4409163953Srrs /* 4410163953Srrs * reduce inp's refcount if not reduced in 4411163953Srrs * sctp_findassociation_ep_asconf(). 4412163953Srrs */ 4413163953Srrs SCTP_INP_DECR_REF(inp); 4414171990Srrs } else { 4415171990Srrs locked_tcb = stcb; 4416163953Srrs } 4417171990Srrs 4418163953Srrs /* now go back and verify any auth chunk to be sure */ 4419163953Srrs if (auth_skipped && (stcb != NULL)) { 4420163953Srrs struct sctp_auth_chunk *auth; 4421163953Srrs 4422163953Srrs auth = (struct sctp_auth_chunk *) 4423163953Srrs sctp_m_getptr(m, auth_offset, 4424163953Srrs auth_len, chunk_buf); 4425163953Srrs got_auth = 1; 4426163953Srrs auth_skipped = 0; 4427169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4428163953Srrs auth_offset)) { 4429163953Srrs /* auth HMAC failed so dump it */ 4430163953Srrs *offset = length; 4431170091Srrs if (locked_tcb) { 4432170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4433170091Srrs } 4434163953Srrs return (NULL); 4435163953Srrs } else { 4436163953Srrs /* remaining chunks are HMAC checked */ 4437163953Srrs stcb->asoc.authenticated = 1; 4438163953Srrs } 4439163953Srrs } 4440163953Srrs } 4441163953Srrs if (stcb == NULL) { 4442163953Srrs /* no association, so it's out of the blue... */ 4443169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4444179157Srrs vrf_id, port); 4445163953Srrs *offset = length; 4446169378Srrs if (locked_tcb) { 4447163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4448169378Srrs } 4449163953Srrs return (NULL); 4450163953Srrs } 4451163953Srrs asoc = &stcb->asoc; 4452163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4453163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4454163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4455163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4456163953Srrs if ((vtag_in == asoc->my_vtag) || 4457163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4458163953Srrs (vtag_in == asoc->peer_vtag))) { 4459163953Srrs /* this is valid */ 4460163953Srrs } else { 4461163953Srrs /* drop this packet... */ 4462163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4463169378Srrs if (locked_tcb) { 4464163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4465169378Srrs } 4466163953Srrs return (NULL); 4467163953Srrs } 4468163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4469163953Srrs if (vtag_in != asoc->my_vtag) { 4470163953Srrs /* 4471163953Srrs * this could be a stale SHUTDOWN-ACK or the 4472163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4473163953Srrs * is still hung; we have started a new asoc 4474163953Srrs * but it won't complete until the shutdown 4475163953Srrs * is completed 4476163953Srrs */ 4477169378Srrs if (locked_tcb) { 4478163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4479169378Srrs } 4480163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4481179157Srrs NULL, vrf_id, port); 4482163953Srrs return (NULL); 4483163953Srrs } 4484163953Srrs } else { 4485163953Srrs /* for all other chunks, vtag must match */ 4486163953Srrs if (vtag_in != asoc->my_vtag) { 4487163953Srrs /* invalid vtag... */ 4488169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4489169420Srrs "invalid vtag: %xh, expect %xh\n", 4490169420Srrs vtag_in, asoc->my_vtag); 4491163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4492169378Srrs if (locked_tcb) { 4493163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4494169378Srrs } 4495163953Srrs *offset = length; 4496163953Srrs return (NULL); 4497163953Srrs } 4498163953Srrs } 4499163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4500163953Srrs /* 4501163953Srrs * process all control chunks... 4502163953Srrs */ 4503163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4504185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4505163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4506163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4507163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4508179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4509171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4510171943Srrs stcb->asoc.overall_error_count, 4511171943Srrs 0, 4512171943Srrs SCTP_FROM_SCTP_INPUT, 4513171943Srrs __LINE__); 4514171943Srrs } 4515163953Srrs stcb->asoc.overall_error_count = 0; 4516163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4517163953Srrs *netp); 4518163953Srrs } 4519163953Srrsprocess_control_chunks: 4520163953Srrs while (IS_SCTP_CONTROL(ch)) { 4521163953Srrs /* validate chunk length */ 4522163953Srrs chk_length = ntohs(ch->chunk_length); 4523169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4524169420Srrs ch->chunk_type, chk_length); 4525170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4526170140Srrs if (chk_length < sizeof(*ch) || 4527170140Srrs (*offset + (int)chk_length) > length) { 4528163953Srrs *offset = length; 4529169378Srrs if (locked_tcb) { 4530163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4531169378Srrs } 4532163953Srrs return (NULL); 4533163953Srrs } 4534163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4535163953Srrs /* 4536163953Srrs * INIT-ACK only gets the init ack "header" portion only 4537163953Srrs * because we don't have to process the peer's COOKIE. All 4538163953Srrs * others get a complete chunk. 4539163953Srrs */ 4540169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4541169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4542163953Srrs /* get an init-ack chunk */ 4543163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4544163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4545163953Srrs if (ch == NULL) { 4546163953Srrs *offset = length; 4547169378Srrs if (locked_tcb) { 4548163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4549169378Srrs } 4550163953Srrs return (NULL); 4551163953Srrs } 4552170642Srrs } else { 4553170786Srrs /* For cookies and all other chunks. */ 4554169208Srrs if (chk_length > sizeof(chunk_buf)) { 4555169208Srrs /* 4556169208Srrs * use just the size of the chunk buffer so 4557170642Srrs * the front part of our chunks fit in 4558170642Srrs * contiguous space up to the chunk buffer 4559170642Srrs * size (508 bytes). For chunks that need to 4560170786Srrs * get more than that they must use the 4561170642Srrs * sctp_m_getptr() function or other means 4562170786Srrs * (e.g. know how to parse mbuf chains). 4563170786Srrs * Cookies do this already. 4564169208Srrs */ 4565169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4566169208Srrs (sizeof(chunk_buf) - 4), 4567169208Srrs chunk_buf); 4568169208Srrs if (ch == NULL) { 4569169208Srrs *offset = length; 4570169378Srrs if (locked_tcb) { 4571169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4572169378Srrs } 4573169208Srrs return (NULL); 4574169208Srrs } 4575169208Srrs } else { 4576169208Srrs /* We can fit it all */ 4577170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4578170642Srrs chk_length, chunk_buf); 4579170642Srrs if (ch == NULL) { 4580170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4581170642Srrs *offset = length; 4582170642Srrs if (locked_tcb) { 4583170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4584169208Srrs } 4585170642Srrs return (NULL); 4586163953Srrs } 4587163953Srrs } 4588163953Srrs } 4589163953Srrs num_chunks++; 4590163953Srrs /* Save off the last place we got a control from */ 4591163953Srrs if (stcb != NULL) { 4592169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4593163953Srrs /* 4594163953Srrs * allow last_control to be NULL if 4595163953Srrs * ASCONF... ASCONF processing will find the 4596163953Srrs * right net later 4597163953Srrs */ 4598169420Srrs if ((netp != NULL) && (*netp != NULL)) 4599169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4600163953Srrs } 4601163953Srrs } 4602163953Srrs#ifdef SCTP_AUDITING_ENABLED 4603163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4604163953Srrs#endif 4605163953Srrs 4606163953Srrs /* check to see if this chunk required auth, but isn't */ 4607179783Srrs if ((stcb != NULL) && 4608179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4609179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4610163953Srrs !stcb->asoc.authenticated) { 4611163953Srrs /* "silently" ignore */ 4612163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4613163953Srrs goto next_chunk; 4614163953Srrs } 4615163953Srrs switch (ch->chunk_type) { 4616163953Srrs case SCTP_INITIATION: 4617169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4618229774Stuexen /* The INIT chunk must be the only chunk. */ 4619229774Stuexen if ((num_chunks > 1) || 4620229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4621229774Stuexen sctp_abort_association(inp, stcb, m, 4622229774Stuexen iphlen, sh, NULL, vrf_id, port); 4623163953Srrs *offset = length; 4624163953Srrs return (NULL); 4625163953Srrs } 4626229774Stuexen /* Honor our resource limit. */ 4627229774Stuexen if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4628229774Stuexen struct mbuf *op_err; 4629229774Stuexen 4630229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4631229774Stuexen sctp_abort_association(inp, stcb, m, 4632229774Stuexen iphlen, sh, op_err, vrf_id, port); 4633163953Srrs *offset = length; 4634163953Srrs return (NULL); 4635163953Srrs } 4636229774Stuexen sctp_handle_init(m, iphlen, *offset, sh, 4637229774Stuexen (struct sctp_init_chunk *)ch, inp, 4638229774Stuexen stcb, &abort_no_unlock, vrf_id, port); 4639163953Srrs *offset = length; 4640229774Stuexen if ((!abort_no_unlock) && (locked_tcb)) { 4641163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4642169378Srrs } 4643163953Srrs return (NULL); 4644163953Srrs break; 4645170642Srrs case SCTP_PAD_CHUNK: 4646170642Srrs break; 4647163953Srrs case SCTP_INITIATION_ACK: 4648169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4649163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4650163953Srrs /* We are not interested anymore */ 4651163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4652163953Srrs ; 4653163953Srrs } else { 4654208876Srrs if (locked_tcb != stcb) { 4655208876Srrs /* Very unlikely */ 4656163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4657169378Srrs } 4658163953Srrs *offset = length; 4659163953Srrs if (stcb) { 4660172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4661172090Srrs so = SCTP_INP_SO(inp); 4662172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4663172090Srrs SCTP_TCB_UNLOCK(stcb); 4664172090Srrs SCTP_SOCKET_LOCK(so, 1); 4665172090Srrs SCTP_TCB_LOCK(stcb); 4666172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4667172090Srrs#endif 4668171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4669172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4670172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4671172090Srrs#endif 4672163953Srrs } 4673163953Srrs return (NULL); 4674163953Srrs } 4675163953Srrs } 4676229775Stuexen /* The INIT-ACK chunk must be the only chunk. */ 4677163953Srrs if ((num_chunks > 1) || 4678229775Stuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4679163953Srrs *offset = length; 4680169378Srrs if (locked_tcb) { 4681163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4682169378Srrs } 4683163953Srrs return (NULL); 4684163953Srrs } 4685169420Srrs if ((netp) && (*netp)) { 4686169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4687170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4688169420Srrs } else { 4689169420Srrs ret = -1; 4690169420Srrs } 4691229774Stuexen *offset = length; 4692229774Stuexen if (abort_no_unlock) { 4693229774Stuexen return (NULL); 4694229774Stuexen } 4695163953Srrs /* 4696163953Srrs * Special case, I must call the output routine to 4697163953Srrs * get the cookie echoed 4698163953Srrs */ 4699229774Stuexen if ((stcb != NULL) && (ret == 0)) { 4700172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4701229774Stuexen } 4702169378Srrs if (locked_tcb) { 4703163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4704169378Srrs } 4705163953Srrs return (NULL); 4706163953Srrs break; 4707163953Srrs case SCTP_SELECTIVE_ACK: 4708163953Srrs { 4709163953Srrs struct sctp_sack_chunk *sack; 4710163953Srrs int abort_now = 0; 4711163953Srrs uint32_t a_rwnd, cum_ack; 4712202526Stuexen uint16_t num_seg, num_dup; 4713202526Stuexen uint8_t flags; 4714202526Stuexen int offset_seg, offset_dup; 4715163953Srrs 4716211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4717211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4718202526Stuexen if (stcb == NULL) { 4719202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4720202526Stuexen break; 4721169208Srrs } 4722202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4723202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4724202526Stuexen break; 4725202526Stuexen } 4726171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4727171990Srrs /*- 4728171990Srrs * If we have sent a shutdown-ack, we will pay no 4729171990Srrs * attention to a sack sent in to us since 4730171990Srrs * we don't care anymore. 4731171990Srrs */ 4732184883Srrs break; 4733171990Srrs } 4734163953Srrs sack = (struct sctp_sack_chunk *)ch; 4735202526Stuexen flags = ch->chunk_flags; 4736163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4737163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4738202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4739163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4740202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4741202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4742202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4743202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4744202526Stuexen break; 4745202526Stuexen } 4746202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4747202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4748170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4749202526Stuexen cum_ack, num_seg, a_rwnd); 4750163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4751163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4752163953Srrs (num_seg == 0) && 4753216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4754163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4755216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4756163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4757163953Srrs ) { 4758163953Srrs /* 4759163953Srrs * We have a SIMPLE sack having no 4760163953Srrs * prior segments and data on sent 4761163953Srrs * queue to be acked.. Use the 4762163953Srrs * faster path sack processing. We 4763163953Srrs * also allow window update sacks 4764163953Srrs * with no missing segments to go 4765163953Srrs * this way too. 4766163953Srrs */ 4767218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4768163953Srrs } else { 4769169420Srrs if (netp && *netp) 4770228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4771202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4772218186Srrs cum_ack, a_rwnd, ecne_seen); 4773163953Srrs } 4774202526Stuexen if (abort_now) { 4775202526Stuexen /* ABORT signal from sack processing */ 4776202526Stuexen *offset = length; 4777202526Stuexen return (NULL); 4778202526Stuexen } 4779185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4780185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4781185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4782185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4783185694Srrs } 4784163953Srrs } 4785163953Srrs break; 4786185694Srrs /* 4787185694Srrs * EY - nr_sack: If the received chunk is an 4788185694Srrs * nr_sack chunk 4789185694Srrs */ 4790185694Srrs case SCTP_NR_SELECTIVE_ACK: 4791185694Srrs { 4792185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4793185694Srrs int abort_now = 0; 4794185694Srrs uint32_t a_rwnd, cum_ack; 4795202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4796202526Stuexen uint8_t flags; 4797202526Stuexen int offset_seg, offset_dup; 4798185694Srrs 4799211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4800211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4801212225Srrs if (stcb == NULL) { 4802212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4803212225Srrs break; 4804212225Srrs } 4805211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4806211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4807185694Srrs goto unknown_chunk; 4808185694Srrs } 4809202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4810202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4811202526Stuexen break; 4812202526Stuexen } 4813185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4814185694Srrs /*- 4815185694Srrs * If we have sent a shutdown-ack, we will pay no 4816185694Srrs * attention to a sack sent in to us since 4817185694Srrs * we don't care anymore. 4818185694Srrs */ 4819202526Stuexen break; 4820185694Srrs } 4821185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4822202526Stuexen flags = ch->chunk_flags; 4823185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4824185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4825185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4826202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4827185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4828202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4829202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4830202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4831202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4832202526Stuexen break; 4833202526Stuexen } 4834202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4835202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4836185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4837202526Stuexen cum_ack, num_seg, a_rwnd); 4838185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4839185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4840202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4841216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4842185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4843216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4844202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4845185694Srrs /* 4846185694Srrs * We have a SIMPLE sack having no 4847185694Srrs * prior segments and data on sent 4848202526Stuexen * queue to be acked. Use the faster 4849202526Stuexen * path sack processing. We also 4850202526Stuexen * allow window update sacks with no 4851202526Stuexen * missing segments to go this way 4852202526Stuexen * too. 4853185694Srrs */ 4854218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4855218186Srrs &abort_now, ecne_seen); 4856185694Srrs } else { 4857185694Srrs if (netp && *netp) 4858228653Stuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4859202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4860218186Srrs cum_ack, a_rwnd, ecne_seen); 4861185694Srrs } 4862202526Stuexen if (abort_now) { 4863202526Stuexen /* ABORT signal from sack processing */ 4864202526Stuexen *offset = length; 4865202526Stuexen return (NULL); 4866202526Stuexen } 4867185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4868185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4869185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4870185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4871185694Srrs } 4872185694Srrs } 4873185694Srrs break; 4874185694Srrs 4875163953Srrs case SCTP_HEARTBEAT_REQUEST: 4876169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4877169420Srrs if ((stcb) && netp && *netp) { 4878169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4879169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4880169420Srrs chk_length, *netp); 4881169420Srrs 4882169420Srrs /* He's alive so give him credit */ 4883179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4884171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4885171943Srrs stcb->asoc.overall_error_count, 4886171943Srrs 0, 4887171943Srrs SCTP_FROM_SCTP_INPUT, 4888171943Srrs __LINE__); 4889171943Srrs } 4890169420Srrs stcb->asoc.overall_error_count = 0; 4891163953Srrs } 4892163953Srrs break; 4893163953Srrs case SCTP_HEARTBEAT_ACK: 4894169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4895169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4896169208Srrs /* Its not ours */ 4897169352Srrs *offset = length; 4898169378Srrs if (locked_tcb) { 4899169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4900169378Srrs } 4901169208Srrs return (NULL); 4902169208Srrs } 4903163953Srrs /* He's alive so give him credit */ 4904179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4905171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4906171943Srrs stcb->asoc.overall_error_count, 4907171943Srrs 0, 4908171943Srrs SCTP_FROM_SCTP_INPUT, 4909171943Srrs __LINE__); 4910171943Srrs } 4911163953Srrs stcb->asoc.overall_error_count = 0; 4912163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4913169420Srrs if (netp && *netp) 4914169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4915169420Srrs stcb, *netp); 4916163953Srrs break; 4917163953Srrs case SCTP_ABORT_ASSOCIATION: 4918170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4919170091Srrs stcb); 4920169420Srrs if ((stcb) && netp && *netp) 4921169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4922169420Srrs stcb, *netp); 4923163953Srrs *offset = length; 4924163953Srrs return (NULL); 4925163953Srrs break; 4926163953Srrs case SCTP_SHUTDOWN: 4927170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4928170091Srrs stcb); 4929169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4930169208Srrs *offset = length; 4931169378Srrs if (locked_tcb) { 4932169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4933169378Srrs } 4934169208Srrs return (NULL); 4935169420Srrs } 4936169420Srrs if (netp && *netp) { 4937163953Srrs int abort_flag = 0; 4938163953Srrs 4939163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4940163953Srrs stcb, *netp, &abort_flag); 4941163953Srrs if (abort_flag) { 4942163953Srrs *offset = length; 4943163953Srrs return (NULL); 4944163953Srrs } 4945163953Srrs } 4946163953Srrs break; 4947163953Srrs case SCTP_SHUTDOWN_ACK: 4948170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4949169420Srrs if ((stcb) && (netp) && (*netp)) 4950169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4951163953Srrs *offset = length; 4952163953Srrs return (NULL); 4953163953Srrs break; 4954169420Srrs 4955163953Srrs case SCTP_OPERATION_ERROR: 4956169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4957169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4958163953Srrs *offset = length; 4959163953Srrs return (NULL); 4960163953Srrs } 4961163953Srrs break; 4962163953Srrs case SCTP_COOKIE_ECHO: 4963169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4964170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4965163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4966163953Srrs ; 4967163953Srrs } else { 4968169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4969163953Srrs /* We are not interested anymore */ 4970208878Srrs abend: 4971208853Srrs if (stcb) { 4972208853Srrs SCTP_TCB_UNLOCK(stcb); 4973208853Srrs } 4974163953Srrs *offset = length; 4975163953Srrs return (NULL); 4976163953Srrs } 4977163953Srrs } 4978163953Srrs /* 4979163953Srrs * First are we accepting? We do this again here 4980207924Srrs * since it is possible that a previous endpoint WAS 4981207924Srrs * listening responded to a INIT-ACK and then 4982163953Srrs * closed. We opened and bound.. and are now no 4983163953Srrs * longer listening. 4984163953Srrs */ 4985172703Srrs 4986172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4987172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4988179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4989229774Stuexen struct mbuf *op_err; 4990163953Srrs 4991229774Stuexen op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4992172703Srrs sctp_abort_association(inp, stcb, m, 4993229774Stuexen iphlen, sh, op_err, vrf_id, port); 4994163953Srrs } 4995172703Srrs *offset = length; 4996172703Srrs return (NULL); 4997172703Srrs } else { 4998163953Srrs struct mbuf *ret_buf; 4999165220Srrs struct sctp_inpcb *linp; 5000163953Srrs 5001169420Srrs if (stcb) { 5002165220Srrs linp = NULL; 5003169420Srrs } else { 5004165220Srrs linp = inp; 5005169420Srrs } 5006165220Srrs 5007169420Srrs if (linp) { 5008165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 5009208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5010208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5011208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5012208878Srrs goto abend; 5013208878Srrs } 5014169420Srrs } 5015169420Srrs if (netp) { 5016169420Srrs ret_buf = 5017169420Srrs sctp_handle_cookie_echo(m, iphlen, 5018169420Srrs *offset, sh, 5019169420Srrs (struct sctp_cookie_echo_chunk *)ch, 5020169420Srrs &inp, &stcb, netp, 5021169420Srrs auth_skipped, 5022169420Srrs auth_offset, 5023169420Srrs auth_len, 5024169420Srrs &locked_tcb, 5025179157Srrs vrf_id, 5026179157Srrs port); 5027169420Srrs } else { 5028169420Srrs ret_buf = NULL; 5029169420Srrs } 5030169420Srrs if (linp) { 5031165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5032169420Srrs } 5033163953Srrs if (ret_buf == NULL) { 5034163953Srrs if (locked_tcb) { 5035163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5036163953Srrs } 5037169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5038169420Srrs "GAK, null buffer\n"); 5039163953Srrs *offset = length; 5040163953Srrs return (NULL); 5041163953Srrs } 5042163953Srrs /* if AUTH skipped, see if it verified... */ 5043163953Srrs if (auth_skipped) { 5044163953Srrs got_auth = 1; 5045163953Srrs auth_skipped = 0; 5046163953Srrs } 5047163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5048163953Srrs /* 5049163953Srrs * Restart the timer if we have 5050163953Srrs * pending data 5051163953Srrs */ 5052163953Srrs struct sctp_tmit_chunk *chk; 5053163953Srrs 5054163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5055216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5056163953Srrs } 5057163953Srrs } 5058163953Srrs break; 5059163953Srrs case SCTP_COOKIE_ACK: 5060170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5061169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5062169378Srrs if (locked_tcb) { 5063169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5064169378Srrs } 5065169352Srrs return (NULL); 5066169352Srrs } 5067163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5068163953Srrs /* We are not interested anymore */ 5069163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5070163953Srrs ; 5071169420Srrs } else if (stcb) { 5072172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5073172090Srrs so = SCTP_INP_SO(inp); 5074172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5075172090Srrs SCTP_TCB_UNLOCK(stcb); 5076172090Srrs SCTP_SOCKET_LOCK(so, 1); 5077172090Srrs SCTP_TCB_LOCK(stcb); 5078172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5079172090Srrs#endif 5080171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5081172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5082172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5083172090Srrs#endif 5084163953Srrs *offset = length; 5085163953Srrs return (NULL); 5086163953Srrs } 5087163953Srrs } 5088163953Srrs /* He's alive so give him credit */ 5089169420Srrs if ((stcb) && netp && *netp) { 5090179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5091171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5092171943Srrs stcb->asoc.overall_error_count, 5093171943Srrs 0, 5094171943Srrs SCTP_FROM_SCTP_INPUT, 5095171943Srrs __LINE__); 5096171943Srrs } 5097169378Srrs stcb->asoc.overall_error_count = 0; 5098169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5099169378Srrs } 5100163953Srrs break; 5101163953Srrs case SCTP_ECN_ECHO: 5102169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5103163953Srrs /* He's alive so give him credit */ 5104169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5105169208Srrs /* Its not ours */ 5106169378Srrs if (locked_tcb) { 5107169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5108169378Srrs } 5109169208Srrs *offset = length; 5110169208Srrs return (NULL); 5111169208Srrs } 5112169378Srrs if (stcb) { 5113179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5114171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5115171943Srrs stcb->asoc.overall_error_count, 5116171943Srrs 0, 5117171943Srrs SCTP_FROM_SCTP_INPUT, 5118171943Srrs __LINE__); 5119171943Srrs } 5120169378Srrs stcb->asoc.overall_error_count = 0; 5121169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5122169378Srrs stcb); 5123218186Srrs ecne_seen = 1; 5124169378Srrs } 5125163953Srrs break; 5126163953Srrs case SCTP_ECN_CWR: 5127169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5128163953Srrs /* He's alive so give him credit */ 5129169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5130169208Srrs /* Its not ours */ 5131169378Srrs if (locked_tcb) { 5132169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5133169378Srrs } 5134169208Srrs *offset = length; 5135169208Srrs return (NULL); 5136169208Srrs } 5137169378Srrs if (stcb) { 5138179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5139171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5140171943Srrs stcb->asoc.overall_error_count, 5141171943Srrs 0, 5142171943Srrs SCTP_FROM_SCTP_INPUT, 5143171943Srrs __LINE__); 5144171943Srrs } 5145169378Srrs stcb->asoc.overall_error_count = 0; 5146218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5147169378Srrs } 5148163953Srrs break; 5149163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5150170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5151163953Srrs /* must be first and only chunk */ 5152163953Srrs if ((num_chunks > 1) || 5153170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5154163953Srrs *offset = length; 5155169378Srrs if (locked_tcb) { 5156163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5157169378Srrs } 5158163953Srrs return (NULL); 5159163953Srrs } 5160169420Srrs if ((stcb) && netp && *netp) { 5161169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5162169378Srrs stcb, *netp); 5163169378Srrs } 5164163953Srrs *offset = length; 5165163953Srrs return (NULL); 5166163953Srrs break; 5167163953Srrs case SCTP_ASCONF: 5168169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5169163953Srrs /* He's alive so give him credit */ 5170169378Srrs if (stcb) { 5171179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5172171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5173171943Srrs stcb->asoc.overall_error_count, 5174171943Srrs 0, 5175171943Srrs SCTP_FROM_SCTP_INPUT, 5176171943Srrs __LINE__); 5177171943Srrs } 5178169378Srrs stcb->asoc.overall_error_count = 0; 5179169378Srrs sctp_handle_asconf(m, *offset, 5180171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5181171990Srrs asconf_cnt++; 5182169378Srrs } 5183163953Srrs break; 5184163953Srrs case SCTP_ASCONF_ACK: 5185169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5186169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5187169208Srrs /* Its not ours */ 5188169378Srrs if (locked_tcb) { 5189169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5190169378Srrs } 5191169208Srrs *offset = length; 5192169208Srrs return (NULL); 5193169208Srrs } 5194169420Srrs if ((stcb) && netp && *netp) { 5195169378Srrs /* He's alive so give him credit */ 5196179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5197171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5198171943Srrs stcb->asoc.overall_error_count, 5199171943Srrs 0, 5200171943Srrs SCTP_FROM_SCTP_INPUT, 5201171943Srrs __LINE__); 5202171943Srrs } 5203169378Srrs stcb->asoc.overall_error_count = 0; 5204169378Srrs sctp_handle_asconf_ack(m, *offset, 5205172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5206172190Srrs if (abort_no_unlock) 5207172190Srrs return (NULL); 5208169378Srrs } 5209163953Srrs break; 5210163953Srrs case SCTP_FORWARD_CUM_TSN: 5211169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5212169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5213169208Srrs /* Its not ours */ 5214169378Srrs if (locked_tcb) { 5215169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5216169378Srrs } 5217169208Srrs *offset = length; 5218169208Srrs return (NULL); 5219169208Srrs } 5220163953Srrs /* He's alive so give him credit */ 5221169378Srrs if (stcb) { 5222163953Srrs int abort_flag = 0; 5223163953Srrs 5224163953Srrs stcb->asoc.overall_error_count = 0; 5225179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5226171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5227171943Srrs stcb->asoc.overall_error_count, 5228171943Srrs 0, 5229171943Srrs SCTP_FROM_SCTP_INPUT, 5230171943Srrs __LINE__); 5231171943Srrs } 5232163953Srrs *fwd_tsn_seen = 1; 5233163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5234163953Srrs /* We are not interested anymore */ 5235172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5236172090Srrs so = SCTP_INP_SO(inp); 5237172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5238172090Srrs SCTP_TCB_UNLOCK(stcb); 5239172090Srrs SCTP_SOCKET_LOCK(so, 1); 5240172090Srrs SCTP_TCB_LOCK(stcb); 5241172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5242172090Srrs#endif 5243171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5244172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5245172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5246172090Srrs#endif 5247163953Srrs *offset = length; 5248163953Srrs return (NULL); 5249163953Srrs } 5250163953Srrs sctp_handle_forward_tsn(stcb, 5251170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5252163953Srrs if (abort_flag) { 5253163953Srrs *offset = length; 5254163953Srrs return (NULL); 5255163953Srrs } else { 5256179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5257171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5258171943Srrs stcb->asoc.overall_error_count, 5259171943Srrs 0, 5260171943Srrs SCTP_FROM_SCTP_INPUT, 5261171943Srrs __LINE__); 5262171943Srrs } 5263163953Srrs stcb->asoc.overall_error_count = 0; 5264163953Srrs } 5265163953Srrs 5266163953Srrs } 5267163953Srrs break; 5268163953Srrs case SCTP_STREAM_RESET: 5269169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5270169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5271169208Srrs /* Its not ours */ 5272169378Srrs if (locked_tcb) { 5273169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5274169378Srrs } 5275169208Srrs *offset = length; 5276169208Srrs return (NULL); 5277169208Srrs } 5278163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5279163953Srrs /* We are not interested anymore */ 5280172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5281172090Srrs so = SCTP_INP_SO(inp); 5282172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5283172090Srrs SCTP_TCB_UNLOCK(stcb); 5284172090Srrs SCTP_SOCKET_LOCK(so, 1); 5285172090Srrs SCTP_TCB_LOCK(stcb); 5286172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5287172090Srrs#endif 5288171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5289172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5290172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5291172090Srrs#endif 5292163953Srrs *offset = length; 5293163953Srrs return (NULL); 5294163953Srrs } 5295169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5296169420Srrs /* 5297169420Srrs * hmm, peer should have announced this, but 5298169420Srrs * we will turn it on since he is sending us 5299169420Srrs * a stream reset. 5300169420Srrs */ 5301169420Srrs stcb->asoc.peer_supports_strreset = 1; 5302163953Srrs } 5303170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5304169420Srrs /* stop processing */ 5305169420Srrs *offset = length; 5306169420Srrs return (NULL); 5307169420Srrs } 5308163953Srrs break; 5309163953Srrs case SCTP_PACKET_DROPPED: 5310169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5311163953Srrs /* re-get it all please */ 5312169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5313169208Srrs /* Its not ours */ 5314169378Srrs if (locked_tcb) { 5315169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5316169378Srrs } 5317169208Srrs *offset = length; 5318169208Srrs return (NULL); 5319169208Srrs } 5320169420Srrs if (ch && (stcb) && netp && (*netp)) { 5321169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5322170781Srrs stcb, *netp, 5323170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5324170781Srrs 5325169378Srrs } 5326163953Srrs break; 5327163953Srrs 5328163953Srrs case SCTP_AUTHENTICATION: 5329169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5330179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5331163953Srrs goto unknown_chunk; 5332163953Srrs 5333163953Srrs if (stcb == NULL) { 5334163953Srrs /* save the first AUTH for later processing */ 5335163953Srrs if (auth_skipped == 0) { 5336163953Srrs auth_offset = *offset; 5337163953Srrs auth_len = chk_length; 5338163953Srrs auth_skipped = 1; 5339163953Srrs } 5340163953Srrs /* skip this chunk (temporarily) */ 5341163953Srrs goto next_chunk; 5342163953Srrs } 5343169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5344169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5345169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5346169208Srrs /* Its not ours */ 5347169378Srrs if (locked_tcb) { 5348169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5349169378Srrs } 5350169208Srrs *offset = length; 5351169208Srrs return (NULL); 5352169208Srrs } 5353163953Srrs if (got_auth == 1) { 5354163953Srrs /* skip this chunk... it's already auth'd */ 5355163953Srrs goto next_chunk; 5356163953Srrs } 5357163953Srrs got_auth = 1; 5358169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5359163953Srrs m, *offset)) { 5360163953Srrs /* auth HMAC failed so dump the packet */ 5361163953Srrs *offset = length; 5362163953Srrs return (stcb); 5363163953Srrs } else { 5364163953Srrs /* remaining chunks are HMAC checked */ 5365163953Srrs stcb->asoc.authenticated = 1; 5366163953Srrs } 5367163953Srrs break; 5368163953Srrs 5369163953Srrs default: 5370163953Srrs unknown_chunk: 5371163953Srrs /* it's an unknown chunk! */ 5372163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5373163953Srrs struct mbuf *mm; 5374163953Srrs struct sctp_paramhdr *phd; 5375163953Srrs 5376163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5377165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5378163953Srrs if (mm) { 5379163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5380163953Srrs /* 5381163953Srrs * We cheat and use param type since 5382163953Srrs * we did not bother to define a 5383163953Srrs * error cause struct. They are the 5384163953Srrs * same basic format with different 5385163953Srrs * names. 5386163953Srrs */ 5387163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5388163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5389165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5390166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5391163953Srrs M_DONTWAIT); 5392165647Srrs if (SCTP_BUF_NEXT(mm)) { 5393178202Srrs#ifdef SCTP_MBUF_LOGGING 5394179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5395178202Srrs struct mbuf *mat; 5396178202Srrs 5397178202Srrs mat = SCTP_BUF_NEXT(mm); 5398178202Srrs while (mat) { 5399178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5400178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5401178202Srrs } 5402178202Srrs mat = SCTP_BUF_NEXT(mat); 5403178202Srrs } 5404178202Srrs } 5405178202Srrs#endif 5406163953Srrs sctp_queue_op_err(stcb, mm); 5407163953Srrs } else { 5408163953Srrs sctp_m_freem(mm); 5409163953Srrs } 5410163953Srrs } 5411163953Srrs } 5412163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5413163953Srrs /* discard this packet */ 5414163953Srrs *offset = length; 5415163953Srrs return (stcb); 5416163953Srrs } /* else skip this bad chunk and continue... */ 5417163953Srrs break; 5418163953Srrs } /* switch (ch->chunk_type) */ 5419163953Srrs 5420163953Srrs 5421163953Srrsnext_chunk: 5422163953Srrs /* get the next chunk */ 5423163953Srrs *offset += SCTP_SIZE32(chk_length); 5424163953Srrs if (*offset >= length) { 5425163953Srrs /* no more data left in the mbuf chain */ 5426163953Srrs break; 5427163953Srrs } 5428163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5429163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5430163953Srrs if (ch == NULL) { 5431169378Srrs if (locked_tcb) { 5432163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5433169378Srrs } 5434163953Srrs *offset = length; 5435163953Srrs return (NULL); 5436163953Srrs } 5437163953Srrs } /* while */ 5438171990Srrs 5439171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5440171990Srrs sctp_send_asconf_ack(stcb); 5441171990Srrs } 5442163953Srrs return (stcb); 5443163953Srrs} 5444163953Srrs 5445163953Srrs 5446171440Srrs#ifdef INVARIANTS 5447208853Srrs#ifdef __GNUC__ 5448208853Srrs__attribute__((noinline)) 5449208853Srrs#endif 5450208853Srrs void 5451208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5452171440Srrs{ 5453208853Srrs struct sctp_tcb *lstcb; 5454171440Srrs 5455208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5456208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5457171440Srrs panic("Own lock on stcb at return from input"); 5458171440Srrs } 5459171440Srrs } 5460208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5461208876Srrs panic("Own create lock on inp"); 5462208876Srrs } 5463208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5464208876Srrs panic("Own inp lock on inp"); 5465208876Srrs } 5466171440Srrs} 5467171440Srrs 5468171440Srrs#endif 5469171440Srrs 5470163953Srrs/* 5471163953Srrs * common input chunk processing (v4 and v6) 5472163953Srrs */ 5473169378Srrsvoid 5474163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5475163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5476163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5477179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5478163953Srrs{ 5479163953Srrs /* 5480163953Srrs * Control chunk processing 5481163953Srrs */ 5482163953Srrs uint32_t high_tsn; 5483163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5484163953Srrs struct mbuf *m = *mm; 5485163953Srrs int un_sent; 5486218129Srrs int cnt_ctrl_ready = 0; 5487163953Srrs 5488163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5489163953Srrs#ifdef SCTP_AUDITING_ENABLED 5490163953Srrs sctp_audit_log(0xE0, 1); 5491163953Srrs sctp_auditing(0, inp, stcb, net); 5492163953Srrs#endif 5493163953Srrs 5494179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5495179783Srrs m, iphlen, offset, length, stcb); 5496163953Srrs if (stcb) { 5497163953Srrs /* always clear this before beginning a packet */ 5498163953Srrs stcb->asoc.authenticated = 0; 5499163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5500171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5501171990Srrs stcb, stcb->asoc.state); 5502171990Srrs 5503171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5504171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5505171745Srrs /*- 5506171745Srrs * If we hit here, we had a ref count 5507171745Srrs * up when the assoc was aborted and the 5508171745Srrs * timer is clearing out the assoc, we should 5509171745Srrs * NOT respond to any packet.. its OOTB. 5510171745Srrs */ 5511171745Srrs SCTP_TCB_UNLOCK(stcb); 5512171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5513179157Srrs vrf_id, port); 5514171745Srrs goto out_now; 5515171745Srrs } 5516163953Srrs } 5517163953Srrs if (IS_SCTP_CONTROL(ch)) { 5518163953Srrs /* process the control portion of the SCTP packet */ 5519169655Srrs /* sa_ignore NO_NULL_CHK */ 5520163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5521179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5522163953Srrs if (stcb) { 5523163953Srrs /* 5524163953Srrs * This covers us if the cookie-echo was there and 5525163953Srrs * it changes our INP. 5526163953Srrs */ 5527163953Srrs inp = stcb->sctp_ep; 5528179783Srrs if ((net) && (port)) { 5529179783Srrs if (net->port == 0) { 5530228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5531179783Srrs } 5532179783Srrs net->port = port; 5533179783Srrs } 5534163953Srrs } 5535163953Srrs } else { 5536163953Srrs /* 5537163953Srrs * no control chunks, so pre-process DATA chunks (these 5538163953Srrs * checks are taken care of by control processing) 5539163953Srrs */ 5540163953Srrs 5541163953Srrs /* 5542163953Srrs * if DATA only packet, and auth is required, then punt... 5543163953Srrs * can't have authenticated without any AUTH (control) 5544163953Srrs * chunks 5545163953Srrs */ 5546179783Srrs if ((stcb != NULL) && 5547179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5548179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5549163953Srrs /* "silently" ignore */ 5550163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5551163953Srrs SCTP_TCB_UNLOCK(stcb); 5552171440Srrs goto out_now; 5553163953Srrs } 5554163953Srrs if (stcb == NULL) { 5555163953Srrs /* out of the blue DATA chunk */ 5556169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5557179157Srrs vrf_id, port); 5558171440Srrs goto out_now; 5559163953Srrs } 5560163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5561163953Srrs /* v_tag mismatch! */ 5562163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5563163953Srrs SCTP_TCB_UNLOCK(stcb); 5564171440Srrs goto out_now; 5565163953Srrs } 5566163953Srrs } 5567163953Srrs 5568163953Srrs if (stcb == NULL) { 5569163953Srrs /* 5570163953Srrs * no valid TCB for this packet, or we found it's a bad 5571163953Srrs * packet while processing control, or we're done with this 5572163953Srrs * packet (done or skip rest of data), so we drop it... 5573163953Srrs */ 5574171440Srrs goto out_now; 5575163953Srrs } 5576163953Srrs /* 5577163953Srrs * DATA chunk processing 5578163953Srrs */ 5579163953Srrs /* plow through the data chunks while length > offset */ 5580163953Srrs 5581163953Srrs /* 5582163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5583163953Srrs * DATA is required. 5584163953Srrs */ 5585179783Srrs if ((length > offset) && 5586179783Srrs (stcb != NULL) && 5587179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5588179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5589163953Srrs !stcb->asoc.authenticated) { 5590163953Srrs /* "silently" ignore */ 5591163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5592169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5593169420Srrs "Data chunk requires AUTH, skipped\n"); 5594165220Srrs goto trigger_send; 5595163953Srrs } 5596163953Srrs if (length > offset) { 5597163953Srrs int retval; 5598163953Srrs 5599163953Srrs /* 5600163953Srrs * First check to make sure our state is correct. We would 5601163953Srrs * not get here unless we really did have a tag, so we don't 5602163953Srrs * abort if this happens, just dump the chunk silently. 5603163953Srrs */ 5604163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5605163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5606163953Srrs /* 5607163953Srrs * we consider data with valid tags in this state 5608163953Srrs * shows us the cookie-ack was lost. Imply it was 5609163953Srrs * there. 5610163953Srrs */ 5611179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5612171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5613171943Srrs stcb->asoc.overall_error_count, 5614171943Srrs 0, 5615171943Srrs SCTP_FROM_SCTP_INPUT, 5616171943Srrs __LINE__); 5617171943Srrs } 5618163953Srrs stcb->asoc.overall_error_count = 0; 5619163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5620163953Srrs break; 5621163953Srrs case SCTP_STATE_COOKIE_WAIT: 5622163953Srrs /* 5623163953Srrs * We consider OOTB any data sent during asoc setup. 5624163953Srrs */ 5625169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5626179157Srrs vrf_id, port); 5627163953Srrs SCTP_TCB_UNLOCK(stcb); 5628171440Srrs goto out_now; 5629171531Srrs /* sa_ignore NOTREACHED */ 5630163953Srrs break; 5631163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5632163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5633163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5634163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5635163953Srrs default: 5636163953Srrs SCTP_TCB_UNLOCK(stcb); 5637171440Srrs goto out_now; 5638171531Srrs /* sa_ignore NOTREACHED */ 5639163953Srrs break; 5640163953Srrs case SCTP_STATE_OPEN: 5641163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5642163953Srrs break; 5643163953Srrs } 5644163953Srrs /* plow through the data chunks while length > offset */ 5645163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5646163953Srrs inp, stcb, net, &high_tsn); 5647163953Srrs if (retval == 2) { 5648163953Srrs /* 5649163953Srrs * The association aborted, NO UNLOCK needed since 5650163953Srrs * the association is destroyed. 5651163953Srrs */ 5652171440Srrs goto out_now; 5653163953Srrs } 5654163953Srrs data_processed = 1; 5655163953Srrs /* 5656163953Srrs * Anything important needs to have been m_copy'ed in 5657163953Srrs * process_data 5658163953Srrs */ 5659163953Srrs } 5660218129Srrs /* take care of ecn */ 5661228907Stuexen if ((data_processed == 1) && 5662228907Stuexen (stcb->asoc.ecn_allowed == 1) && 5663218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5664218129Srrs /* Yep, we need to add a ECNE */ 5665218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5666218129Srrs } 5667163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5668216495Stuexen int was_a_gap; 5669216495Stuexen uint32_t highest_tsn; 5670163953Srrs 5671216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5672216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5673216495Stuexen } else { 5674216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5675163953Srrs } 5676216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5677190689Srrs stcb->asoc.send_sack = 1; 5678228653Stuexen sctp_sack_check(stcb, was_a_gap); 5679190689Srrs } else if (fwd_tsn_seen) { 5680190689Srrs stcb->asoc.send_sack = 1; 5681163953Srrs } 5682163953Srrs /* trigger send of any chunks in queue... */ 5683165220Srrstrigger_send: 5684163953Srrs#ifdef SCTP_AUDITING_ENABLED 5685163953Srrs sctp_audit_log(0xE0, 2); 5686163953Srrs sctp_auditing(1, inp, stcb, net); 5687163953Srrs#endif 5688169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5689169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5690169420Srrs stcb->asoc.peers_rwnd, 5691169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5692169420Srrs stcb->asoc.total_flight); 5693163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5694218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5695218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5696218129Srrs } 5697218129Srrs if (cnt_ctrl_ready || 5698163953Srrs ((un_sent) && 5699163953Srrs (stcb->asoc.peers_rwnd > 0 || 5700163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5701169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5702172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5703169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5704163953Srrs } 5705163953Srrs#ifdef SCTP_AUDITING_ENABLED 5706163953Srrs sctp_audit_log(0xE0, 3); 5707163953Srrs sctp_auditing(2, inp, stcb, net); 5708163953Srrs#endif 5709163953Srrs SCTP_TCB_UNLOCK(stcb); 5710171440Srrsout_now: 5711171440Srrs#ifdef INVARIANTS 5712171440Srrs sctp_validate_no_locks(inp); 5713171440Srrs#endif 5714169378Srrs return; 5715163953Srrs} 5716163953Srrs 5717185694Srrs#if 0 5718185694Srrsstatic void 5719185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5720185694Srrs{ 5721185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5722185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5723185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5724185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5725185694Srrs } 5726185694Srrs} 5727163953Srrs 5728185694Srrs#endif 5729185694Srrs 5730221249Stuexen#ifdef INET 5731163953Srrsvoid 5732189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5733163953Srrs{ 5734163953Srrs#ifdef SCTP_MBUF_LOGGING 5735163953Srrs struct mbuf *mat; 5736163953Srrs 5737163953Srrs#endif 5738165647Srrs struct mbuf *m; 5739163953Srrs int iphlen; 5740170181Srrs uint32_t vrf_id = 0; 5741163953Srrs uint8_t ecn_bits; 5742163953Srrs struct ip *ip; 5743163953Srrs struct sctphdr *sh; 5744163953Srrs struct sctp_inpcb *inp = NULL; 5745163953Srrs struct sctp_nets *net; 5746163953Srrs struct sctp_tcb *stcb = NULL; 5747163953Srrs struct sctp_chunkhdr *ch; 5748163953Srrs int refcount_up = 0; 5749163953Srrs int length, mlen, offset; 5750163953Srrs 5751211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5752211969Stuexen uint32_t check, calc_check; 5753211969Stuexen 5754211969Stuexen#endif 5755211969Stuexen 5756169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5757169352Srrs SCTP_RELEASE_PKT(i_pak); 5758169352Srrs return; 5759169352Srrs } 5760168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5761163953Srrs iphlen = off; 5762165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5763170056Srrs 5764163953Srrs net = NULL; 5765163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5766163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5767163953Srrs 5768170091Srrs 5769163953Srrs#ifdef SCTP_MBUF_LOGGING 5770163953Srrs /* Log in any input mbufs */ 5771179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5772228907Stuexen for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 5773170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5774170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5775170744Srrs } 5776163953Srrs } 5777163953Srrs } 5778163953Srrs#endif 5779170091Srrs#ifdef SCTP_PACKET_LOGGING 5780179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5781170921Srrs sctp_packet_log(m, mlen); 5782170091Srrs#endif 5783170091Srrs /* 5784170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5785170091Srrs * case) 5786170091Srrs */ 5787170091Srrs mlen -= iphlen; 5788168124Srrs 5789163953Srrs /* 5790163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5791163953Srrs */ 5792163953Srrs ip = mtod(m, struct ip *); 5793163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5794165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5795163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5796163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5797163953Srrs return; 5798163953Srrs } 5799163953Srrs ip = mtod(m, struct ip *); 5800163953Srrs } 5801188067Srrs /* validate mbuf chain length with IP payload length */ 5802188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5803188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5804188067Srrs goto bad; 5805188067Srrs } 5806163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5807163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5808170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5809170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5810163953Srrs 5811163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5812163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5813163953Srrs goto bad; 5814163953Srrs } 5815168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5816163953Srrs /* 5817163953Srrs * We only look at broadcast if its a front state, All 5818163953Srrs * others we will not have a tcb for anyway. 5819163953Srrs */ 5820163953Srrs goto bad; 5821163953Srrs } 5822163953Srrs /* validate SCTP checksum */ 5823188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5824188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5825188067Srrs m->m_pkthdr.len, 5826188067Srrs if_name(m->m_pkthdr.rcvif), 5827188067Srrs m->m_pkthdr.csum_flags); 5828211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5829211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5830211969Stuexen#else 5831188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5832188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5833188067Srrs goto sctp_skip_csum_4; 5834188067Srrs } 5835171990Srrs check = sh->checksum; /* save incoming checksum */ 5836171990Srrs sh->checksum = 0; /* prepare for calc */ 5837188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5838203503Stuexen sh->checksum = check; 5839188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5840171990Srrs if (calc_check != check) { 5841171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5842171990Srrs calc_check, check, m, mlen, iphlen); 5843163953Srrs 5844228653Stuexen stcb = sctp_findassociation_addr(m, 5845171990Srrs offset - sizeof(*ch), 5846171990Srrs sh, ch, &inp, &net, 5847171990Srrs vrf_id); 5848179157Srrs if ((net) && (port)) { 5849179157Srrs if (net->port == 0) { 5850228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5851179157Srrs } 5852179157Srrs net->port = port; 5853179157Srrs } 5854218400Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5855218400Stuexen net->flowid = m->m_pkthdr.flowid; 5856218400Stuexen#ifdef INVARIANTS 5857218400Stuexen net->flowidset = 1; 5858218400Stuexen#endif 5859218400Stuexen } 5860171990Srrs if ((inp) && (stcb)) { 5861171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5862172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5863171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5864171990Srrs refcount_up = 1; 5865163953Srrs } 5866171990Srrs SCTP_STAT_INCR(sctps_badsum); 5867171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5868171990Srrs goto bad; 5869168299Srrs } 5870163953Srrssctp_skip_csum_4: 5871211969Stuexen#endif 5872168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5873168124Srrs if (sh->dest_port == 0) { 5874168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5875168124Srrs goto bad; 5876168124Srrs } 5877163953Srrs /* 5878163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5879163953Srrs * IP/SCTP/first chunk header... 5880163953Srrs */ 5881228653Stuexen stcb = sctp_findassociation_addr(m, offset - sizeof(*ch), 5882168299Srrs sh, ch, &inp, &net, vrf_id); 5883179157Srrs if ((net) && (port)) { 5884179157Srrs if (net->port == 0) { 5885228653Stuexen sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5886179157Srrs } 5887179157Srrs net->port = port; 5888179157Srrs } 5889218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5890218335Stuexen net->flowid = m->m_pkthdr.flowid; 5891218400Stuexen#ifdef INVARIANTS 5892218392Srrs net->flowidset = 1; 5893218400Stuexen#endif 5894218335Stuexen } 5895163953Srrs /* inp's ref-count increased && stcb locked */ 5896163953Srrs if (inp == NULL) { 5897163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5898163953Srrs 5899163953Srrs SCTP_STAT_INCR(sctps_noport); 5900163953Srrs#ifdef ICMP_BANDLIM 5901163953Srrs /* 5902163953Srrs * we use the bandwidth limiting to protect against sending 5903163953Srrs * too many ABORTS all at once. In this case these count the 5904163953Srrs * same as an ICMP message. 5905163953Srrs */ 5906163953Srrs if (badport_bandlim(0) < 0) 5907163953Srrs goto bad; 5908163953Srrs#endif /* ICMP_BANDLIM */ 5909169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5910169420Srrs "Sending a ABORT from packet entry!\n"); 5911163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5912163953Srrs /* 5913163953Srrs * we do a trick here to get the INIT tag, dig in 5914163953Srrs * and get the tag from the INIT and put it in the 5915163953Srrs * common header. 5916163953Srrs */ 5917163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5918163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5919163953Srrs (uint8_t *) & chunk_buf); 5920163953Srrs if (init_chk != NULL) 5921163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5922163953Srrs } 5923165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5924228653Stuexen sctp_send_shutdown_complete2(m, sh, vrf_id, port); 5925165220Srrs goto bad; 5926165220Srrs } 5927165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5928165220Srrs goto bad; 5929165220Srrs } 5930229805Stuexen if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 5931229805Stuexen if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 5932229805Stuexen ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 5933229805Stuexen (ch->chunk_type != SCTP_INIT))) { 5934229805Stuexen sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5935229805Stuexen } 5936229805Stuexen } 5937163953Srrs goto bad; 5938163953Srrs } else if (stcb == NULL) { 5939163953Srrs refcount_up = 1; 5940163953Srrs } 5941171167Sgnn#ifdef IPSEC 5942163953Srrs /* 5943163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5944163953Srrs * idea, so I will leave it in place. 5945163953Srrs */ 5946163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5947197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5948163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5949163953Srrs goto bad; 5950163953Srrs } 5951163953Srrs#endif /* IPSEC */ 5952163953Srrs 5953163953Srrs /* 5954163953Srrs * common chunk processing 5955163953Srrs */ 5956163953Srrs length = ip->ip_len + iphlen; 5957163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5958163953Srrs 5959163953Srrs ecn_bits = ip->ip_tos; 5960163953Srrs 5961169655Srrs /* sa_ignore NO_NULL_CHK */ 5962163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5963179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5964163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5965163953Srrs if (m) { 5966163953Srrs sctp_m_freem(m); 5967163953Srrs } 5968163953Srrs if ((inp) && (refcount_up)) { 5969163953Srrs /* reduce ref-count */ 5970163953Srrs SCTP_INP_DECR_REF(inp); 5971163953Srrs } 5972163953Srrs return; 5973163953Srrsbad: 5974169420Srrs if (stcb) { 5975163953Srrs SCTP_TCB_UNLOCK(stcb); 5976169420Srrs } 5977163953Srrs if ((inp) && (refcount_up)) { 5978163953Srrs /* reduce ref-count */ 5979163953Srrs SCTP_INP_DECR_REF(inp); 5980163953Srrs } 5981163953Srrs if (m) { 5982163953Srrs sctp_m_freem(m); 5983163953Srrs } 5984163953Srrs return; 5985163953Srrs} 5986218211Srrs 5987218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5988218269Srrsextern int *sctp_cpuarry; 5989218211Srrs 5990218269Srrs#endif 5991218269Srrs 5992179157Srrsvoid 5993218211Srrssctp_input(struct mbuf *m, int off) 5994179157Srrs{ 5995218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 5996218211Srrs struct ip *ip; 5997218211Srrs struct sctphdr *sh; 5998218211Srrs int offset; 5999218211Srrs int cpu_to_use; 6000218371Srrs uint32_t flowid, tag; 6001218211Srrs 6002218269Srrs if (mp_ncpus > 1) { 6003218371Srrs if (m->m_flags & M_FLOWID) { 6004218371Srrs flowid = m->m_pkthdr.flowid; 6005218269Srrs } else { 6006218269Srrs /* 6007218371Srrs * No flow id built by lower layers fix it so we 6008218371Srrs * create one. 6009218269Srrs */ 6010218371Srrs ip = mtod(m, struct ip *); 6011218371Srrs offset = off + sizeof(*sh); 6012218371Srrs if (SCTP_BUF_LEN(m) < offset) { 6013218371Srrs if ((m = m_pullup(m, offset)) == 0) { 6014218371Srrs SCTP_STAT_INCR(sctps_hdrops); 6015218371Srrs return; 6016218371Srrs } 6017218371Srrs ip = mtod(m, struct ip *); 6018218371Srrs } 6019218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 6020218371Srrs tag = htonl(sh->v_tag); 6021218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6022218371Srrs m->m_pkthdr.flowid = flowid; 6023218371Srrs m->m_flags |= M_FLOWID; 6024218269Srrs } 6025218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6026218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 6027218211Srrs return; 6028218211Srrs } 6029218211Srrs#endif 6030218211Srrs sctp_input_with_port(m, off, 0); 6031179157Srrs} 6032221249Stuexen 6033221249Stuexen#endif 6034