sctp_input.c revision 224641
1163953Srrs/*- 2185694Srrs * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3218269Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. 4218269Srrs * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. 5163953Srrs * 6163953Srrs * Redistribution and use in source and binary forms, with or without 7163953Srrs * modification, are permitted provided that the following conditions are met: 8163953Srrs * 9163953Srrs * a) Redistributions of source code must retain the above copyright notice, 10163953Srrs * this list of conditions and the following disclaimer. 11163953Srrs * 12163953Srrs * b) Redistributions in binary form must reproduce the above copyright 13163953Srrs * notice, this list of conditions and the following disclaimer in 14163953Srrs * the documentation and/or other materials provided with the distribution. 15163953Srrs * 16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 17163953Srrs * contributors may be used to endorse or promote products derived 18163953Srrs * from this software without specific prior written permission. 19163953Srrs * 20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 31163953Srrs */ 32163953Srrs 33163953Srrs/* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */ 34163953Srrs 35163953Srrs#include <sys/cdefs.h> 36163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 224641 2011-08-03 20:21:00Z 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, 87179157Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 88163953Srrs{ 89163953Srrs struct sctp_init *init; 90163953Srrs struct mbuf *op_err; 91163953Srrs uint32_t init_limit; 92163953Srrs 93169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 94169420Srrs stcb); 95172396Srrs if (stcb == NULL) { 96172396Srrs SCTP_INP_RLOCK(inp); 97172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 98172396Srrs goto outnow; 99172396Srrs } 100172396Srrs } 101163953Srrs op_err = NULL; 102163953Srrs init = &cp->init; 103163953Srrs /* First are we accepting? */ 104163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 105169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 106169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 107169420Srrs inp->sctp_socket->so_qlimit); 108163953Srrs /* 109163953Srrs * FIX ME ?? What about TCP model and we have a 110179141Srrs * match/restart case? Actually no fix is needed. the lookup 111179141Srrs * will always find the existing assoc so stcb would not be 112179141Srrs * NULL. It may be questionable to do this since we COULD 113179141Srrs * just send back the INIT-ACK and hope that the app did 114179141Srrs * accept()'s by the time the COOKIE was sent. But there is 115179141Srrs * a price to pay for COOKIE generation and I don't want to 116179141Srrs * pay it on the chance that the app will actually do some 117179141Srrs * accepts(). The App just looses and should NOT be in this 118179141Srrs * state :-) 119163953Srrs */ 120169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121179157Srrs vrf_id, port); 122168299Srrs if (stcb) 123168299Srrs *abort_no_unlock = 1; 124172396Srrs goto outnow; 125163953Srrs } 126163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 127163953Srrs /* Invalid length */ 128163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 129169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 130179157Srrs vrf_id, port); 131168299Srrs if (stcb) 132168299Srrs *abort_no_unlock = 1; 133172396Srrs goto outnow; 134163953Srrs } 135163953Srrs /* validate parameters */ 136163953Srrs if (init->initiate_tag == 0) { 137163953Srrs /* protocol error... send abort */ 138163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 139169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 140179157Srrs vrf_id, port); 141168299Srrs if (stcb) 142168299Srrs *abort_no_unlock = 1; 143172396Srrs goto outnow; 144163953Srrs } 145163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 146163953Srrs /* invalid parameter... send abort */ 147163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 148169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 149179157Srrs vrf_id, port); 150171440Srrs if (stcb) 151171440Srrs *abort_no_unlock = 1; 152172396Srrs goto outnow; 153163953Srrs } 154163953Srrs if (init->num_inbound_streams == 0) { 155163953Srrs /* protocol error... send abort */ 156163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 157169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 158179157Srrs vrf_id, port); 159168299Srrs if (stcb) 160168299Srrs *abort_no_unlock = 1; 161172396Srrs goto outnow; 162163953Srrs } 163163953Srrs if (init->num_outbound_streams == 0) { 164163953Srrs /* protocol error... send abort */ 165163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 166169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 167179157Srrs vrf_id, port); 168168299Srrs if (stcb) 169168299Srrs *abort_no_unlock = 1; 170172396Srrs goto outnow; 171163953Srrs } 172163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 173163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 174163953Srrs init_limit)) { 175163953Srrs /* auth parameter(s) error... send abort */ 176179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 177168299Srrs if (stcb) 178168299Srrs *abort_no_unlock = 1; 179172396Srrs goto outnow; 180163953Srrs } 181163953Srrs /* send an INIT-ACK w/cookie */ 182169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 183179157Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 184172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 185172396Srrsoutnow: 186172396Srrs if (stcb == NULL) { 187172396Srrs SCTP_INP_RUNLOCK(inp); 188172396Srrs } 189163953Srrs} 190163953Srrs 191163953Srrs/* 192163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 193163953Srrs */ 194171158Srrs 195171158Srrsint 196221627Stuexensctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked 197221627Stuexen#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 198221627Stuexen SCTP_UNUSED 199221627Stuexen#endif 200221627Stuexen) 201171158Srrs{ 202171158Srrs int unsent_data = 0; 203217760Stuexen unsigned int i; 204217760Stuexen struct sctp_stream_queue_pending *sp; 205171158Srrs struct sctp_association *asoc; 206171158Srrs 207171158Srrs /* 208171158Srrs * This function returns the number of streams that have true unsent 209171158Srrs * data on them. Note that as it looks through it will clean up any 210171158Srrs * places that have old data that has been sent but left at top of 211171158Srrs * stream queue. 212171158Srrs */ 213171158Srrs asoc = &stcb->asoc; 214171158Srrs SCTP_TCB_SEND_LOCK(stcb); 215217760Stuexen if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 216217760Stuexen /* Check to see if some data queued */ 217217760Stuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 218217760Stuexen /* sa_ignore FREED_MEMORY */ 219217760Stuexen sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 220217760Stuexen if (sp == NULL) { 221217760Stuexen continue; 222217760Stuexen } 223171158Srrs if ((sp->msg_is_complete) && 224171158Srrs (sp->length == 0) && 225171158Srrs (sp->sender_all_done)) { 226171158Srrs /* 227171158Srrs * We are doing differed cleanup. Last time 228171158Srrs * through when we took all the data the 229171158Srrs * sender_all_done was not set. 230171158Srrs */ 231171158Srrs if (sp->put_last_out == 0) { 232171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 233171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 234171158Srrs sp->sender_all_done, 235171158Srrs sp->length, 236171158Srrs sp->msg_is_complete, 237171158Srrs sp->put_last_out); 238171158Srrs } 239217760Stuexen atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 240217760Stuexen TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 241212712Stuexen if (sp->net) { 242212712Stuexen sctp_free_remote_addr(sp->net); 243212712Stuexen sp->net = NULL; 244212712Stuexen } 245171158Srrs if (sp->data) { 246171158Srrs sctp_m_freem(sp->data); 247171158Srrs sp->data = NULL; 248171158Srrs } 249221627Stuexen sctp_free_a_strmoq(stcb, sp, so_locked); 250171158Srrs } else { 251171158Srrs unsent_data++; 252216822Stuexen break; 253171158Srrs } 254171158Srrs } 255171158Srrs } 256171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 257171158Srrs return (unsent_data); 258171158Srrs} 259171158Srrs 260163953Srrsstatic int 261163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 262163953Srrs struct sctp_nets *net) 263163953Srrs{ 264163953Srrs struct sctp_init *init; 265163953Srrs struct sctp_association *asoc; 266163953Srrs struct sctp_nets *lnet; 267163953Srrs unsigned int i; 268163953Srrs 269163953Srrs init = &cp->init; 270163953Srrs asoc = &stcb->asoc; 271163953Srrs /* save off parameters */ 272163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 273163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 274218129Srrs /* init tsn's */ 275218129Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 276218129Srrs 277212712Stuexen if (!TAILQ_EMPTY(&asoc->nets)) { 278163953Srrs /* update any ssthresh's that may have a default */ 279163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 280163953Srrs lnet->ssthresh = asoc->peers_rwnd; 281179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 282170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 283170744Srrs } 284163953Srrs } 285163953Srrs } 286164181Srrs SCTP_TCB_SEND_LOCK(stcb); 287163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 288163953Srrs unsigned int newcnt; 289163953Srrs struct sctp_stream_out *outs; 290216822Stuexen struct sctp_stream_queue_pending *sp, *nsp; 291216822Stuexen struct sctp_tmit_chunk *chk, *nchk; 292163953Srrs 293196260Stuexen /* abandon the upper streams */ 294163953Srrs newcnt = ntohs(init->num_inbound_streams); 295216822Stuexen TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 296216822Stuexen if (chk->rec.data.stream_number >= newcnt) { 297216822Stuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 298216822Stuexen asoc->send_queue_cnt--; 299216822Stuexen if (chk->data != NULL) { 300216822Stuexen sctp_free_bufspace(stcb, asoc, chk, 1); 301216822Stuexen sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 302216822Stuexen SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 303216822Stuexen if (chk->data) { 304216822Stuexen sctp_m_freem(chk->data); 305216822Stuexen chk->data = NULL; 306196260Stuexen } 307196260Stuexen } 308221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 309216822Stuexen /* sa_ignore FREED_MEMORY */ 310196260Stuexen } 311196260Stuexen } 312163953Srrs if (asoc->strmout) { 313163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 314163953Srrs outs = &asoc->strmout[i]; 315216822Stuexen TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 316196260Stuexen TAILQ_REMOVE(&outs->outqueue, sp, next); 317163953Srrs asoc->stream_queue_cnt--; 318163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 319163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 320172090Srrs sp, SCTP_SO_NOT_LOCKED); 321163953Srrs if (sp->data) { 322163953Srrs sctp_m_freem(sp->data); 323163953Srrs sp->data = NULL; 324163953Srrs } 325212712Stuexen if (sp->net) { 326212712Stuexen sctp_free_remote_addr(sp->net); 327212712Stuexen sp->net = NULL; 328212712Stuexen } 329163953Srrs /* Free the chunk */ 330221627Stuexen sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 331169655Srrs /* sa_ignore FREED_MEMORY */ 332163953Srrs } 333163953Srrs } 334163953Srrs } 335196260Stuexen /* cut back the count */ 336163953Srrs asoc->pre_open_streams = newcnt; 337163953Srrs } 338164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 339188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 340218129Srrs 341185694Srrs /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 342185694Srrs asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 343179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 344170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 345170744Srrs } 346163953Srrs /* This is the next one we expect */ 347163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 348163953Srrs 349163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 350180955Srrs asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 351218129Srrs 352163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 353163953Srrs /* open the requested streams */ 354170091Srrs 355163953Srrs if (asoc->strmin != NULL) { 356163953Srrs /* Free the old ones */ 357216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 358164181Srrs 359164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 360216822Stuexen TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 361164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 362164181Srrs sctp_free_remote_addr(ctl->whoFrom); 363171158Srrs ctl->whoFrom = NULL; 364164181Srrs sctp_m_freem(ctl->data); 365164181Srrs ctl->data = NULL; 366164181Srrs sctp_free_a_readq(stcb, ctl); 367164181Srrs } 368164181Srrs } 369170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 370163953Srrs } 371164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 372164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 373164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 374164181Srrs } 375163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 376170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 377163953Srrs if (asoc->strmin == NULL) { 378163953Srrs /* we didn't get memory for the streams! */ 379169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 380163953Srrs return (-1); 381163953Srrs } 382163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 383163953Srrs asoc->strmin[i].stream_no = i; 384163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 385163953Srrs /* 386163953Srrs * U-stream ranges will be set when the cookie is unpacked. 387163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 388163953Srrs * supported) when the INIT-ACK arrives. 389163953Srrs */ 390163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 391168943Srrs asoc->strmin[i].delivery_started = 0; 392163953Srrs } 393163953Srrs /* 394163953Srrs * load_address_from_init will put the addresses into the 395163953Srrs * association when the COOKIE is processed or the INIT-ACK is 396163953Srrs * processed. Both types of COOKIE's existing and new call this 397163953Srrs * routine. It will remove addresses that are no longer in the 398163953Srrs * association (for the restarting case where addresses are 399163953Srrs * removed). Up front when the INIT arrives we will discard it if it 400163953Srrs * is a restart and new addresses have been added. 401163953Srrs */ 402169655Srrs /* sa_ignore MEMLEAK */ 403163953Srrs return (0); 404163953Srrs} 405163953Srrs 406163953Srrs/* 407163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 408163953Srrs */ 409163953Srrsstatic int 410163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 411163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 412170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 413163953Srrs{ 414163953Srrs struct sctp_association *asoc; 415163953Srrs struct mbuf *op_err; 416163953Srrs int retval, abort_flag; 417163953Srrs uint32_t initack_limit; 418185694Srrs int nat_friendly = 0; 419163953Srrs 420163953Srrs /* First verify that we have no illegal param's */ 421163953Srrs abort_flag = 0; 422163953Srrs op_err = NULL; 423163953Srrs 424163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 425163953Srrs (offset + sizeof(struct sctp_init_chunk)), 426185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 427163953Srrs if (abort_flag) { 428163953Srrs /* Send an abort and notify peer */ 429172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 430170056Srrs *abort_no_unlock = 1; 431163953Srrs return (-1); 432163953Srrs } 433163953Srrs asoc = &stcb->asoc; 434185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 435163953Srrs /* process the peer's parameters in the INIT-ACK */ 436163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 437163953Srrs if (retval < 0) { 438163953Srrs return (retval); 439163953Srrs } 440163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 441163953Srrs /* load all addresses */ 442163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 443163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 444163953Srrs NULL))) { 445163953Srrs /* Huh, we should abort */ 446169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 447169420Srrs "Load addresses from INIT causes an abort %d\n", 448169420Srrs retval); 449163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 450179157Srrs NULL, 0, net->port); 451168299Srrs *abort_no_unlock = 1; 452163953Srrs return (-1); 453163953Srrs } 454171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 455171477Srrs if (asoc->peer_supports_asconf == 0) { 456216822Stuexen struct sctp_asconf_addr *param, *nparam; 457171477Srrs 458216822Stuexen TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 459216822Stuexen TAILQ_REMOVE(&asoc->asconf_queue, param, next); 460216822Stuexen SCTP_FREE(param, SCTP_M_ASC_ADDR); 461171477Srrs } 462171477Srrs } 463163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 464163953Srrs stcb->asoc.local_hmacs); 465163953Srrs if (op_err) { 466163953Srrs sctp_queue_op_err(stcb, op_err); 467163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 468163953Srrs op_err = NULL; 469163953Srrs } 470163953Srrs /* extract the cookie and queue it to "echo" it back... */ 471179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 472171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 473171943Srrs stcb->asoc.overall_error_count, 474171943Srrs 0, 475171943Srrs SCTP_FROM_SCTP_INPUT, 476171943Srrs __LINE__); 477171943Srrs } 478163953Srrs stcb->asoc.overall_error_count = 0; 479163953Srrs net->error_count = 0; 480163953Srrs 481163953Srrs /* 482163953Srrs * Cancel the INIT timer, We do this first before queueing the 483163953Srrs * cookie. We always cancel at the primary to assue that we are 484163953Srrs * canceling the timer started by the INIT which always goes to the 485163953Srrs * primary. 486163953Srrs */ 487163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 488165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 489163953Srrs 490165220Srrs /* calculate the RTO */ 491218186Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 492219397Srrs SCTP_RTT_FROM_NON_DATA); 493165220Srrs 494163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 495163953Srrs if (retval < 0) { 496163953Srrs /* 497163953Srrs * No cookie, we probably should send a op error. But in any 498163953Srrs * case if there is no cookie in the INIT-ACK, we can 499163953Srrs * abandon the peer, its broke. 500163953Srrs */ 501163953Srrs if (retval == -3) { 502163953Srrs /* We abort with an error of missing mandatory param */ 503163953Srrs op_err = 504163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 505163953Srrs if (op_err) { 506163953Srrs /* 507163953Srrs * Expand beyond to include the mandatory 508163953Srrs * param cookie 509163953Srrs */ 510163953Srrs struct sctp_inv_mandatory_param *mp; 511163953Srrs 512165647Srrs SCTP_BUF_LEN(op_err) = 513163953Srrs sizeof(struct sctp_inv_mandatory_param); 514163953Srrs mp = mtod(op_err, 515163953Srrs struct sctp_inv_mandatory_param *); 516163953Srrs /* Subtract the reserved param */ 517163953Srrs mp->length = 518163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 519163953Srrs mp->num_param = htonl(1); 520163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 521163953Srrs mp->resv = 0; 522163953Srrs } 523163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 524179157Srrs sh, op_err, 0, net->port); 525168299Srrs *abort_no_unlock = 1; 526163953Srrs } 527163953Srrs return (retval); 528163953Srrs } 529163953Srrs return (0); 530163953Srrs} 531163953Srrs 532163953Srrsstatic void 533163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 534163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 535163953Srrs{ 536163953Srrs struct sockaddr_storage store; 537212225Srrs struct sctp_nets *r_net, *f_net; 538163953Srrs struct timeval tv; 539172396Srrs int req_prim = 0; 540224641Stuexen uint16_t old_error_counter; 541163953Srrs 542221249Stuexen#ifdef INET 543221249Stuexen struct sockaddr_in *sin; 544221249Stuexen 545221249Stuexen#endif 546221249Stuexen#ifdef INET6 547221249Stuexen struct sockaddr_in6 *sin6; 548221249Stuexen 549221249Stuexen#endif 550221249Stuexen 551163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 552163953Srrs /* Invalid length */ 553163953Srrs return; 554163953Srrs } 555163953Srrs memset(&store, 0, sizeof(store)); 556221249Stuexen switch (cp->heartbeat.hb_info.addr_family) { 557221249Stuexen#ifdef INET 558221249Stuexen case AF_INET: 559221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 560221249Stuexen sin = (struct sockaddr_in *)&store; 561221249Stuexen sin->sin_family = cp->heartbeat.hb_info.addr_family; 562221249Stuexen sin->sin_len = cp->heartbeat.hb_info.addr_len; 563221249Stuexen sin->sin_port = stcb->rport; 564221249Stuexen memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 565221249Stuexen sizeof(sin->sin_addr)); 566221249Stuexen } else { 567221249Stuexen return; 568221249Stuexen } 569221249Stuexen break; 570221249Stuexen#endif 571221249Stuexen#ifdef INET6 572221249Stuexen case AF_INET6: 573221249Stuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 574221249Stuexen sin6 = (struct sockaddr_in6 *)&store; 575221249Stuexen sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 576221249Stuexen sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 577221249Stuexen sin6->sin6_port = stcb->rport; 578221249Stuexen memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 579221249Stuexen sizeof(sin6->sin6_addr)); 580221249Stuexen } else { 581221249Stuexen return; 582221249Stuexen } 583221249Stuexen break; 584221249Stuexen#endif 585221249Stuexen default: 586163953Srrs return; 587163953Srrs } 588221249Stuexen r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 589163953Srrs if (r_net == NULL) { 590169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 591163953Srrs return; 592163953Srrs } 593163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 594163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 595163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 596163953Srrs /* 597163953Srrs * If the its a HB and it's random value is correct when can 598163953Srrs * confirm the destination. 599163953Srrs */ 600163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 601167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 602167598Srrs stcb->asoc.primary_destination = r_net; 603167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 604212225Srrs f_net = TAILQ_FIRST(&stcb->asoc.nets); 605212225Srrs if (f_net != r_net) { 606167598Srrs /* 607167598Srrs * first one on the list is NOT the primary 608167598Srrs * sctp_cmpaddr() is much more efficent if 609167598Srrs * the primary is the first on the list, 610167598Srrs * make it so. 611167598Srrs */ 612212225Srrs TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 613212225Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 614167598Srrs } 615172396Srrs req_prim = 1; 616167598Srrs } 617163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 618172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 619224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 620224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 621163953Srrs } 622224641Stuexen old_error_counter = r_net->error_count; 623163953Srrs r_net->error_count = 0; 624163953Srrs r_net->hb_responded = 1; 625163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 626163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 627224641Stuexen /* Now lets do a RTO with this */ 628224641Stuexen r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 629224641Stuexen SCTP_RTT_FROM_NON_DATA); 630224641Stuexen if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 631163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 632163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 633172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 634163953Srrs } 635224641Stuexen if (r_net->dest_state & SCTP_ADDR_PF) { 636224641Stuexen r_net->dest_state &= ~SCTP_ADDR_PF; 637224641Stuexen stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 638224641Stuexen } 639224641Stuexen if (old_error_counter > 0) { 640224641Stuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 641224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 642224641Stuexen } 643224641Stuexen if (r_net == stcb->asoc.primary_destination) { 644224641Stuexen if (stcb->asoc.alternate) { 645224641Stuexen /* release the alternate, primary is good */ 646224641Stuexen sctp_free_remote_addr(stcb->asoc.alternate); 647224641Stuexen stcb->asoc.alternate = NULL; 648171440Srrs } 649171440Srrs } 650172396Srrs /* Mobility adaptation */ 651172396Srrs if (req_prim) { 652172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 653172396Srrs SCTP_MOBILITY_BASE) || 654172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 655172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 656172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 657172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 658172396Srrs 659172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 660172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 661172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 662172396Srrs sctp_assoc_immediate_retrans(stcb, 663172396Srrs stcb->asoc.primary_destination); 664172396Srrs } 665172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 666172396Srrs SCTP_MOBILITY_BASE)) { 667212712Stuexen sctp_move_chunks_from_net(stcb, 668212712Stuexen stcb->asoc.deleted_primary); 669172396Srrs } 670172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 671172396Srrs stcb->asoc.deleted_primary); 672172396Srrs } 673172396Srrs } 674163953Srrs} 675163953Srrs 676185694Srrsstatic int 677185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 678185694Srrs{ 679185694Srrs /* 680185694Srrs * return 0 means we want you to proceed with the abort non-zero 681185694Srrs * means no abort processing 682185694Srrs */ 683185694Srrs struct sctpasochead *head; 684185694Srrs 685185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 686185694Srrs /* generate a new vtag and send init */ 687185694Srrs LIST_REMOVE(stcb, sctp_asocs); 688185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 689185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 690185694Srrs /* 691185694Srrs * put it in the bucket in the vtag hash of assoc's for the 692185694Srrs * system 693185694Srrs */ 694185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 695185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 696185694Srrs return (1); 697185694Srrs } 698185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 699185694Srrs /* 700185694Srrs * treat like a case where the cookie expired i.e.: - dump 701185694Srrs * current cookie. - generate a new vtag. - resend init. 702185694Srrs */ 703185694Srrs /* generate a new vtag and send init */ 704185694Srrs LIST_REMOVE(stcb, sctp_asocs); 705185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 706185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 707185694Srrs sctp_stop_all_cookie_timers(stcb); 708185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 709185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 710185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 711185694Srrs /* 712185694Srrs * put it in the bucket in the vtag hash of assoc's for the 713185694Srrs * system 714185694Srrs */ 715185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 716185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 717185694Srrs return (1); 718185694Srrs } 719185694Srrs return (0); 720185694Srrs} 721185694Srrs 722185694Srrsstatic int 723185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 724185694Srrs struct sctp_nets *net) 725185694Srrs{ 726185694Srrs /* 727185694Srrs * return 0 means we want you to proceed with the abort non-zero 728185694Srrs * means no abort processing 729185694Srrs */ 730185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 731185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 732185694Srrs return (0); 733185694Srrs } 734185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 735185694Srrs return (1); 736185694Srrs} 737185694Srrs 738185694Srrs 739163953Srrsstatic void 740163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 741163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 742163953Srrs{ 743172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 744172090Srrs struct socket *so; 745172090Srrs 746172090Srrs#endif 747185694Srrs uint16_t len; 748172090Srrs 749169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 750163953Srrs if (stcb == NULL) 751163953Srrs return; 752163953Srrs 753185694Srrs len = ntohs(cp->ch.chunk_length); 754185694Srrs if (len > sizeof(struct sctp_chunkhdr)) { 755185694Srrs /* 756185694Srrs * Need to check the cause codes for our two magic nat 757185694Srrs * aborts which don't kill the assoc necessarily. 758185694Srrs */ 759185694Srrs struct sctp_abort_chunk *cpnext; 760185694Srrs struct sctp_missing_nat_state *natc; 761185694Srrs uint16_t cause; 762185694Srrs 763185694Srrs cpnext = cp; 764185694Srrs cpnext++; 765185694Srrs natc = (struct sctp_missing_nat_state *)cpnext; 766185694Srrs cause = ntohs(natc->cause); 767185694Srrs if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 768185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 769185694Srrs cp->ch.chunk_flags); 770185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 771185694Srrs return; 772185694Srrs } 773185694Srrs } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 774185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 775185694Srrs cp->ch.chunk_flags); 776185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 777185694Srrs return; 778185694Srrs } 779185694Srrs } 780185694Srrs } 781163953Srrs /* stop any receive timers */ 782171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 783163953Srrs /* notify user of the abort and clean up... */ 784172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 785163953Srrs /* free the tcb */ 786172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 787172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 788172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 789172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 790172396Srrs panic("Received an ABORT"); 791172396Srrs } else { 792172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 793172396Srrs } 794172396Srrs#endif 795163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 796163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 797163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 798163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 799163953Srrs } 800168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 801168859Srrs sctp_print_out_track_log(stcb); 802168859Srrs#endif 803172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 804172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 805172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 806172090Srrs SCTP_TCB_UNLOCK(stcb); 807172090Srrs SCTP_SOCKET_LOCK(so, 1); 808172090Srrs SCTP_TCB_LOCK(stcb); 809172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 810172090Srrs#endif 811171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 812171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 813171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 814172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 815172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 816172090Srrs#endif 817169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 818163953Srrs} 819163953Srrs 820163953Srrsstatic void 821224641Stuexensctp_start_net_timers(struct sctp_tcb *stcb) 822224641Stuexen{ 823224641Stuexen uint32_t cnt_hb_sent; 824224641Stuexen struct sctp_nets *net; 825224641Stuexen 826224641Stuexen cnt_hb_sent = 0; 827224641Stuexen TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 828224641Stuexen /* 829224641Stuexen * For each network start: 1) A pmtu timer. 2) A HB timer 3) 830224641Stuexen * If the dest in unconfirmed send a hb as well if under 831224641Stuexen * max_hb_burst have been sent. 832224641Stuexen */ 833224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 834224641Stuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 835224641Stuexen if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 836224641Stuexen (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 837224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 838224641Stuexen cnt_hb_sent++; 839224641Stuexen } 840224641Stuexen } 841224641Stuexen if (cnt_hb_sent) { 842224641Stuexen sctp_chunk_output(stcb->sctp_ep, stcb, 843224641Stuexen SCTP_OUTPUT_FROM_COOKIE_ACK, 844224641Stuexen SCTP_SO_NOT_LOCKED); 845224641Stuexen } 846224641Stuexen} 847224641Stuexen 848224641Stuexen 849224641Stuexenstatic void 850163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 851163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 852163953Srrs{ 853163953Srrs struct sctp_association *asoc; 854163953Srrs int some_on_streamwheel; 855163953Srrs 856172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 857172090Srrs struct socket *so; 858172090Srrs 859172090Srrs#endif 860172090Srrs 861169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 862169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 863163953Srrs if (stcb == NULL) 864163953Srrs return; 865165220Srrs asoc = &stcb->asoc; 866165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 867165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 868163953Srrs return; 869163953Srrs } 870163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 871163953Srrs /* Shutdown NOT the expected size */ 872163953Srrs return; 873163953Srrs } else { 874163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 875199477Stuexen if (*abort_flag) { 876199477Stuexen return; 877199477Stuexen } 878163953Srrs } 879165220Srrs if (asoc->control_pdapi) { 880163953Srrs /* 881163953Srrs * With a normal shutdown we assume the end of last record. 882163953Srrs */ 883163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 884165220Srrs asoc->control_pdapi->end_added = 1; 885165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 886165220Srrs asoc->control_pdapi = NULL; 887163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 888172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 889172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 890172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 891172090Srrs SCTP_TCB_UNLOCK(stcb); 892172090Srrs SCTP_SOCKET_LOCK(so, 1); 893172090Srrs SCTP_TCB_LOCK(stcb); 894172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 895172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 896172090Srrs /* assoc was freed while we were unlocked */ 897172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 898172090Srrs return; 899172090Srrs } 900172090Srrs#endif 901163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 902172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 903172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 904172090Srrs#endif 905163953Srrs } 906163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 907163953Srrs if (stcb->sctp_socket) { 908163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 909170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 910163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 911171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 912172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 913163953Srrs /* 914163953Srrs * notify upper layer that peer has initiated a 915163953Srrs * shutdown 916163953Srrs */ 917172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 918163953Srrs 919163953Srrs /* reset time */ 920169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 921163953Srrs } 922163953Srrs } 923163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 924163953Srrs /* 925163953Srrs * stop the shutdown timer, since we WILL move to 926163953Srrs * SHUTDOWN-ACK-SENT. 927163953Srrs */ 928171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 929163953Srrs } 930171158Srrs /* Now is there unsent data on a stream somewhere? */ 931221627Stuexen some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 932163953Srrs 933163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 934163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 935163953Srrs some_on_streamwheel) { 936163953Srrs /* By returning we will push more data out */ 937163953Srrs return; 938163953Srrs } else { 939163953Srrs /* no outstanding data to send, so move on... */ 940163953Srrs /* send SHUTDOWN-ACK */ 941224641Stuexen sctp_send_shutdown_ack(stcb, net); 942163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 943166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 944166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 945163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 946163953Srrs } 947171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 948172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 949214918Stuexen sctp_stop_timers_for_shutdown(stcb); 950163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 951163953Srrs stcb, net); 952163953Srrs } 953163953Srrs} 954163953Srrs 955163953Srrsstatic void 956163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 957204141Stuexen struct sctp_tcb *stcb, 958204141Stuexen struct sctp_nets *net) 959163953Srrs{ 960163953Srrs struct sctp_association *asoc; 961163953Srrs 962172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 963172090Srrs struct socket *so; 964172090Srrs 965172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 966172090Srrs#endif 967169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 968169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 969163953Srrs if (stcb == NULL) 970163953Srrs return; 971163953Srrs 972163953Srrs asoc = &stcb->asoc; 973163953Srrs /* process according to association state */ 974204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 975204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 976204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 977204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 978204141Stuexen SCTP_TCB_UNLOCK(stcb); 979204141Stuexen return; 980204141Stuexen } 981163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 982163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 983163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 984163953Srrs SCTP_TCB_UNLOCK(stcb); 985163953Srrs return; 986163953Srrs } 987165220Srrs if (asoc->control_pdapi) { 988163953Srrs /* 989163953Srrs * With a normal shutdown we assume the end of last record. 990163953Srrs */ 991163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 992165220Srrs asoc->control_pdapi->end_added = 1; 993165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 994165220Srrs asoc->control_pdapi = NULL; 995163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 996172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 997172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 998172090Srrs SCTP_TCB_UNLOCK(stcb); 999172090Srrs SCTP_SOCKET_LOCK(so, 1); 1000172090Srrs SCTP_TCB_LOCK(stcb); 1001172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1002172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1003172090Srrs /* assoc was freed while we were unlocked */ 1004172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1005172090Srrs return; 1006172090Srrs } 1007172090Srrs#endif 1008163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1009172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1010172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1011172090Srrs#endif 1012163953Srrs } 1013163953Srrs /* are the queues empty? */ 1014163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 1015163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 1016217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1017172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 1018163953Srrs } 1019163953Srrs /* stop the timer */ 1020171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1021163953Srrs /* send SHUTDOWN-COMPLETE */ 1022204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 1023163953Srrs /* notify upper layer protocol */ 1024163953Srrs if (stcb->sctp_socket) { 1025172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1026163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1027163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1028163953Srrs /* Set the connected flag to disconnected */ 1029163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 1030163953Srrs } 1031163953Srrs } 1032163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1033163953Srrs /* free the TCB but first save off the ep */ 1034172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1035172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1036172090Srrs SCTP_TCB_UNLOCK(stcb); 1037172090Srrs SCTP_SOCKET_LOCK(so, 1); 1038172090Srrs SCTP_TCB_LOCK(stcb); 1039172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1040172090Srrs#endif 1041171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1042171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1043172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1044172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1045172090Srrs#endif 1046163953Srrs} 1047163953Srrs 1048163953Srrs/* 1049163953Srrs * Skip past the param header and then we will find the chunk that caused the 1050163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1051163953Srrs * our peer must be broken. 1052163953Srrs */ 1053163953Srrsstatic void 1054163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1055163953Srrs struct sctp_nets *net) 1056163953Srrs{ 1057163953Srrs struct sctp_chunkhdr *chk; 1058163953Srrs 1059163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1060163953Srrs switch (chk->chunk_type) { 1061163953Srrs case SCTP_ASCONF_ACK: 1062163953Srrs case SCTP_ASCONF: 1063163953Srrs sctp_asconf_cleanup(stcb, net); 1064163953Srrs break; 1065163953Srrs case SCTP_FORWARD_CUM_TSN: 1066163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1067163953Srrs break; 1068163953Srrs default: 1069169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1070169420Srrs "Peer does not support chunk type %d(%x)??\n", 1071169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1072163953Srrs break; 1073163953Srrs } 1074163953Srrs} 1075163953Srrs 1076163953Srrs/* 1077163953Srrs * Skip past the param header and then we will find the param that caused the 1078163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1079163953Srrs * these will turn of specific features. 1080163953Srrs */ 1081163953Srrsstatic void 1082163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1083163953Srrs{ 1084163953Srrs struct sctp_paramhdr *pbad; 1085163953Srrs 1086163953Srrs pbad = phdr + 1; 1087163953Srrs switch (ntohs(pbad->param_type)) { 1088163953Srrs /* pr-sctp draft */ 1089163953Srrs case SCTP_PRSCTP_SUPPORTED: 1090163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1091163953Srrs break; 1092163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1093163953Srrs break; 1094163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1095185694Srrs case SCTP_HAS_NAT_SUPPORT: 1096185694Srrs stcb->asoc.peer_supports_nat = 0; 1097185694Srrs break; 1098163953Srrs case SCTP_ADD_IP_ADDRESS: 1099163953Srrs case SCTP_DEL_IP_ADDRESS: 1100163953Srrs case SCTP_SET_PRIM_ADDR: 1101163953Srrs stcb->asoc.peer_supports_asconf = 0; 1102163953Srrs break; 1103163953Srrs case SCTP_SUCCESS_REPORT: 1104163953Srrs case SCTP_ERROR_CAUSE_IND: 1105169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1106169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1107169420Srrs "Turning off ASCONF to this strange peer\n"); 1108163953Srrs stcb->asoc.peer_supports_asconf = 0; 1109163953Srrs break; 1110163953Srrs default: 1111169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1112169420Srrs "Peer does not support param type %d(%x)??\n", 1113169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1114163953Srrs break; 1115163953Srrs } 1116163953Srrs} 1117163953Srrs 1118163953Srrsstatic int 1119163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1120163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1121163953Srrs{ 1122163953Srrs int chklen; 1123163953Srrs struct sctp_paramhdr *phdr; 1124163953Srrs uint16_t error_type; 1125163953Srrs uint16_t error_len; 1126163953Srrs struct sctp_association *asoc; 1127163953Srrs int adjust; 1128163953Srrs 1129172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1130172090Srrs struct socket *so; 1131172090Srrs 1132172090Srrs#endif 1133172090Srrs 1134163953Srrs /* parse through all of the errors and process */ 1135163953Srrs asoc = &stcb->asoc; 1136163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1137163953Srrs sizeof(struct sctp_chunkhdr)); 1138163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1139163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1140163953Srrs /* Process an Error Cause */ 1141163953Srrs error_type = ntohs(phdr->param_type); 1142163953Srrs error_len = ntohs(phdr->param_length); 1143163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1144163953Srrs /* invalid param length for this param */ 1145169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1146169420Srrs chklen, error_len); 1147163953Srrs return (0); 1148163953Srrs } 1149163953Srrs switch (error_type) { 1150163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1151163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1152163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1153163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1154169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1155169420Srrs error_type); 1156163953Srrs break; 1157185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1158185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1159185694Srrs ch->chunk_flags); 1160185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1161185694Srrs return (0); 1162185694Srrs } 1163185694Srrs break; 1164185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1165185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1166185694Srrs ch->chunk_flags); 1167185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1168185694Srrs return (0); 1169185694Srrs } 1170185694Srrs break; 1171163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1172163953Srrs /* 1173163953Srrs * We only act if we have echoed a cookie and are 1174163953Srrs * waiting. 1175163953Srrs */ 1176163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1177163953Srrs int *p; 1178163953Srrs 1179163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1180163953Srrs /* Save the time doubled */ 1181163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1182163953Srrs asoc->stale_cookie_count++; 1183163953Srrs if (asoc->stale_cookie_count > 1184163953Srrs asoc->max_init_times) { 1185172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1186163953Srrs /* now free the asoc */ 1187172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1188172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1189172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1190172090Srrs SCTP_TCB_UNLOCK(stcb); 1191172090Srrs SCTP_SOCKET_LOCK(so, 1); 1192172090Srrs SCTP_TCB_LOCK(stcb); 1193172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1194172090Srrs#endif 1195171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1196171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1197172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1198172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1199172090Srrs#endif 1200163953Srrs return (-1); 1201163953Srrs } 1202163953Srrs /* blast back to INIT state */ 1203185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1204163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1205163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1206163953Srrs sctp_stop_all_cookie_timers(stcb); 1207172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1208163953Srrs } 1209163953Srrs break; 1210163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1211163953Srrs /* 1212163953Srrs * Nothing we can do here, we don't do hostname 1213163953Srrs * addresses so if the peer does not like my IPv6 1214163953Srrs * (or IPv4 for that matter) it does not matter. If 1215163953Srrs * they don't support that type of address, they can 1216163953Srrs * NOT possibly get that packet type... i.e. with no 1217163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1218163953Srrs * safely ignore this one. If we ever added support 1219163953Srrs * for HOSTNAME Addresses, then we would need to do 1220163953Srrs * something here. 1221163953Srrs */ 1222163953Srrs break; 1223163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1224163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1225163953Srrs break; 1226163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1227163953Srrs sctp_process_unrecog_param(stcb, phdr); 1228163953Srrs break; 1229163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1230163953Srrs /* 1231163953Srrs * We ignore this since the timer will drive out a 1232163953Srrs * new cookie anyway and there timer will drive us 1233163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1234163953Srrs * here since we don't have their tag. 1235163953Srrs */ 1236163953Srrs break; 1237163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1238163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1239163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1240163953Srrs /* 1241163953Srrs * We should NOT get these here, but in a 1242166086Srrs * ASCONF-ACK. 1243163953Srrs */ 1244169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1245169420Srrs error_type); 1246163953Srrs break; 1247163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1248163953Srrs /* 1249163953Srrs * And what, pray tell do we do with the fact that 1250163953Srrs * the peer is out of resources? Not really sure we 1251166086Srrs * could do anything but abort. I suspect this 1252163953Srrs * should have came WITH an abort instead of in a 1253163953Srrs * OP-ERROR. 1254163953Srrs */ 1255163953Srrs break; 1256163953Srrs default: 1257169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1258169420Srrs error_type); 1259163953Srrs break; 1260163953Srrs } 1261163953Srrs adjust = SCTP_SIZE32(error_len); 1262163953Srrs chklen -= adjust; 1263163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1264163953Srrs } 1265163953Srrs return (0); 1266163953Srrs} 1267163953Srrs 1268163953Srrsstatic int 1269169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1270169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1271170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1272163953Srrs{ 1273163953Srrs struct sctp_init_ack *init_ack; 1274163953Srrs struct mbuf *op_err; 1275163953Srrs 1276169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1277169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1278169420Srrs 1279163953Srrs if (stcb == NULL) { 1280169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1281169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1282163953Srrs return (-1); 1283163953Srrs } 1284163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1285163953Srrs /* Invalid length */ 1286163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1287163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1288179157Srrs op_err, 0, net->port); 1289168299Srrs *abort_no_unlock = 1; 1290163953Srrs return (-1); 1291163953Srrs } 1292163953Srrs init_ack = &cp->init; 1293163953Srrs /* validate parameters */ 1294163953Srrs if (init_ack->initiate_tag == 0) { 1295163953Srrs /* protocol error... send an abort */ 1296163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1297163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1298179157Srrs op_err, 0, net->port); 1299168299Srrs *abort_no_unlock = 1; 1300163953Srrs return (-1); 1301163953Srrs } 1302163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1303163953Srrs /* protocol error... send an abort */ 1304163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1305163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1306179157Srrs op_err, 0, net->port); 1307168299Srrs *abort_no_unlock = 1; 1308163953Srrs return (-1); 1309163953Srrs } 1310163953Srrs if (init_ack->num_inbound_streams == 0) { 1311163953Srrs /* protocol error... send an abort */ 1312163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1313163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1314179157Srrs op_err, 0, net->port); 1315168299Srrs *abort_no_unlock = 1; 1316163953Srrs return (-1); 1317163953Srrs } 1318163953Srrs if (init_ack->num_outbound_streams == 0) { 1319163953Srrs /* protocol error... send an abort */ 1320163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1321163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1322179157Srrs op_err, 0, net->port); 1323168299Srrs *abort_no_unlock = 1; 1324163953Srrs return (-1); 1325163953Srrs } 1326163953Srrs /* process according to association state... */ 1327171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1328163953Srrs case SCTP_STATE_COOKIE_WAIT: 1329163953Srrs /* this is the expected state for this chunk */ 1330163953Srrs /* process the INIT-ACK parameters */ 1331163953Srrs if (stcb->asoc.primary_destination->dest_state & 1332163953Srrs SCTP_ADDR_UNCONFIRMED) { 1333163953Srrs /* 1334163953Srrs * The primary is where we sent the INIT, we can 1335163953Srrs * always consider it confirmed when the INIT-ACK is 1336163953Srrs * returned. Do this before we load addresses 1337163953Srrs * though. 1338163953Srrs */ 1339163953Srrs stcb->asoc.primary_destination->dest_state &= 1340163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1341163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1342172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1343163953Srrs } 1344169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1345170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1346163953Srrs /* error in parsing parameters */ 1347163953Srrs return (-1); 1348163953Srrs } 1349163953Srrs /* update our state */ 1350169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1351171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1352163953Srrs 1353163953Srrs /* reset the RTO calc */ 1354179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1355171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1356171943Srrs stcb->asoc.overall_error_count, 1357171943Srrs 0, 1358171943Srrs SCTP_FROM_SCTP_INPUT, 1359171943Srrs __LINE__); 1360171943Srrs } 1361163953Srrs stcb->asoc.overall_error_count = 0; 1362169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1363163953Srrs /* 1364163953Srrs * collapse the init timer back in case of a exponential 1365165220Srrs * backoff 1366163953Srrs */ 1367163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1368163953Srrs stcb, net); 1369163953Srrs /* 1370163953Srrs * the send at the end of the inbound data processing will 1371163953Srrs * cause the cookie to be sent 1372163953Srrs */ 1373163953Srrs break; 1374163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1375163953Srrs /* incorrect state... discard */ 1376163953Srrs break; 1377163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1378163953Srrs /* incorrect state... discard */ 1379163953Srrs break; 1380163953Srrs case SCTP_STATE_OPEN: 1381163953Srrs /* incorrect state... discard */ 1382163953Srrs break; 1383163953Srrs case SCTP_STATE_EMPTY: 1384163953Srrs case SCTP_STATE_INUSE: 1385163953Srrs default: 1386163953Srrs /* incorrect state... discard */ 1387163953Srrs return (-1); 1388163953Srrs break; 1389163953Srrs } 1390169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1391163953Srrs return (0); 1392163953Srrs} 1393163953Srrs 1394185694Srrsstatic struct sctp_tcb * 1395185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1396185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1397185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1398185694Srrs struct sockaddr *init_src, int *notification, 1399185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1400185694Srrs uint32_t vrf_id, uint16_t port); 1401163953Srrs 1402185694Srrs 1403163953Srrs/* 1404163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1405163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1406163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1407163953Srrs * mbuf to the cookie-echo chunk 1408163953Srrs */ 1409163953Srrsstatic struct sctp_tcb * 1410163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1411163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1412185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1413169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1414185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1415163953Srrs{ 1416163953Srrs struct sctp_association *asoc; 1417163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1418163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1419185694Srrs struct sctp_nets *net; 1420185694Srrs struct mbuf *op_err; 1421185694Srrs struct sctp_paramhdr *ph; 1422163953Srrs int chk_length; 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 chk_length = ntohs(init_cp->ch.chunk_length); 1473163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1474163953Srrs return (NULL); 1475163953Srrs } 1476163953Srrs /* 1477163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1478163953Srrs * INIT-ACK follows the INIT chunk 1479163953Srrs */ 1480163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1481163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1482163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1483163953Srrs (uint8_t *) & initack_buf); 1484163953Srrs if (initack_cp == NULL) { 1485163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1486163953Srrs return (NULL); 1487163953Srrs } 1488163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1489163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1490163953Srrs return (NULL); 1491163953Srrs } 1492163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1493163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1494163953Srrs /* 1495163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1496163953Srrs * to get into the OPEN state 1497163953Srrs */ 1498166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1499172091Srrs /*- 1500172091Srrs * Opps, this means that we somehow generated two vtag's 1501172091Srrs * the same. I.e. we did: 1502172091Srrs * Us Peer 1503172091Srrs * <---INIT(tag=a)------ 1504172091Srrs * ----INIT-ACK(tag=t)--> 1505172091Srrs * ----INIT(tag=t)------> *1 1506172091Srrs * <---INIT-ACK(tag=a)--- 1507172091Srrs * <----CE(tag=t)------------- *2 1508172091Srrs * 1509172091Srrs * At point *1 we should be generating a different 1510172091Srrs * tag t'. Which means we would throw away the CE and send 1511172091Srrs * ours instead. Basically this is case C (throw away side). 1512172091Srrs */ 1513172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1514172091Srrs asoc->cookie_how[how_indx] = 17; 1515172091Srrs return (NULL); 1516172091Srrs 1517166023Srrs } 1518163953Srrs switch SCTP_GET_STATE 1519163953Srrs (asoc) { 1520163953Srrs case SCTP_STATE_COOKIE_WAIT: 1521166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1522163953Srrs /* 1523169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1524166023Srrs * correct tags... just accept it...but we must 1525166023Srrs * process the init so that we can make sure we have 1526166023Srrs * the right seq no's. 1527163953Srrs */ 1528163953Srrs /* First we must process the INIT !! */ 1529163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1530163953Srrs if (retval < 0) { 1531166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1532166023Srrs asoc->cookie_how[how_indx] = 3; 1533163953Srrs return (NULL); 1534163953Srrs } 1535163953Srrs /* we have already processed the INIT so no problem */ 1536163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1537171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1538171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1539163953Srrs /* update current state */ 1540166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1541166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1542166675Srrs else 1543166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1544171943Srrs 1545171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1546163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1547163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1548163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1549163953Srrs } 1550166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1551165220Srrs sctp_stop_all_cookie_timers(stcb); 1552163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1553163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1554163953Srrs (inp->sctp_socket->so_qlimit == 0) 1555163953Srrs ) { 1556172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1557172090Srrs struct socket *so; 1558172090Srrs 1559172090Srrs#endif 1560163953Srrs /* 1561163953Srrs * Here is where collision would go if we 1562163953Srrs * did a connect() and instead got a 1563163953Srrs * init/init-ack/cookie done before the 1564163953Srrs * init-ack came back.. 1565163953Srrs */ 1566163953Srrs stcb->sctp_ep->sctp_flags |= 1567163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1568172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1569172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1570172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1571172090Srrs SCTP_TCB_UNLOCK(stcb); 1572172090Srrs SCTP_SOCKET_LOCK(so, 1); 1573172090Srrs SCTP_TCB_LOCK(stcb); 1574172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1575172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1576172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1577172090Srrs return (NULL); 1578172090Srrs } 1579172090Srrs#endif 1580172090Srrs soisconnected(stcb->sctp_socket); 1581172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1582172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1583172090Srrs#endif 1584163953Srrs } 1585163953Srrs /* notify upper layer */ 1586163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1587163953Srrs /* 1588163953Srrs * since we did not send a HB make sure we don't 1589163953Srrs * double things 1590163953Srrs */ 1591163953Srrs net->hb_responded = 1; 1592170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1593218186Srrs &cookie->time_entered, 1594218186Srrs sctp_align_unsafe_makecopy, 1595219397Srrs SCTP_RTT_FROM_NON_DATA); 1596163953Srrs 1597163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1598163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1599163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1600163953Srrs inp, stcb, NULL); 1601163953Srrs } 1602163953Srrs break; 1603163953Srrs default: 1604163953Srrs /* 1605163953Srrs * we're in the OPEN state (or beyond), so peer must 1606163953Srrs * have simply lost the COOKIE-ACK 1607163953Srrs */ 1608163953Srrs break; 1609163953Srrs } /* end switch */ 1610165220Srrs sctp_stop_all_cookie_timers(stcb); 1611163953Srrs /* 1612163953Srrs * We ignore the return code here.. not sure if we should 1613163953Srrs * somehow abort.. but we do have an existing asoc. This 1614163953Srrs * really should not fail. 1615163953Srrs */ 1616163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1617163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1618163953Srrs initack_offset, sh, init_src)) { 1619166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1620166023Srrs asoc->cookie_how[how_indx] = 4; 1621163953Srrs return (NULL); 1622163953Srrs } 1623163953Srrs /* respond with a COOKIE-ACK */ 1624165220Srrs sctp_toss_old_cookies(stcb, asoc); 1625163953Srrs sctp_send_cookie_ack(stcb); 1626166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1627166023Srrs asoc->cookie_how[how_indx] = 5; 1628163953Srrs return (stcb); 1629169352Srrs } 1630163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1631163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1632163953Srrs cookie->tie_tag_my_vtag == 0 && 1633163953Srrs cookie->tie_tag_peer_vtag == 0) { 1634163953Srrs /* 1635163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1636163953Srrs */ 1637166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1638166023Srrs asoc->cookie_how[how_indx] = 6; 1639163953Srrs return (NULL); 1640163953Srrs } 1641185694Srrs /* 1642185694Srrs * If nat support, and the below and stcb is established, send back 1643185694Srrs * a ABORT(colliding state) if we are established. 1644185694Srrs */ 1645185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1646185694Srrs (asoc->peer_supports_nat) && 1647185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1648185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1649185694Srrs (asoc->peer_vtag == 0)))) { 1650163953Srrs /* 1651185694Srrs * Special case - Peer's support nat. We may have two init's 1652185694Srrs * that we gave out the same tag on since one was not 1653185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1654185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1655185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1656185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1657185694Srrs * Now we have colliding state. We must send an abort here 1658185694Srrs * with colliding state indication. 1659185694Srrs */ 1660185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1661185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1662185694Srrs if (op_err == NULL) { 1663185694Srrs /* FOOBAR */ 1664185694Srrs return (NULL); 1665185694Srrs } 1666185694Srrs /* pre-reserve some space */ 1667185694Srrs#ifdef INET6 1668185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1669185694Srrs#else 1670185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1671185694Srrs#endif 1672185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1673185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1674185694Srrs /* Set the len */ 1675185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1676185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1677185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1678185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1679185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1680185694Srrs return (NULL); 1681185694Srrs } 1682185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1683185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1684185694Srrs (asoc->peer_vtag == 0))) { 1685185694Srrs /* 1686163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1687163953Srrs * should be ok, re-accept peer info 1688163953Srrs */ 1689166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1690166023Srrs /* 1691166023Srrs * Extension of case C. If we hit this, then the 1692166023Srrs * random number generator returned the same vtag 1693166023Srrs * when we first sent our INIT-ACK and when we later 1694166023Srrs * sent our INIT. The side with the seq numbers that 1695166023Srrs * are different will be the one that normnally 1696166023Srrs * would have hit case C. This in effect "extends" 1697166023Srrs * our vtags in this collision case to be 64 bits. 1698166023Srrs * The same collision could occur aka you get both 1699166023Srrs * vtag and seq number the same twice in a row.. but 1700166023Srrs * is much less likely. If it did happen then we 1701166023Srrs * would proceed through and bring up the assoc.. we 1702166023Srrs * may end up with the wrong stream setup however.. 1703166023Srrs * which would be bad.. but there is no way to 1704166023Srrs * tell.. until we send on a stream that does not 1705166023Srrs * exist :-) 1706166023Srrs */ 1707166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1708166023Srrs asoc->cookie_how[how_indx] = 7; 1709166023Srrs 1710166023Srrs return (NULL); 1711166023Srrs } 1712166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1713166023Srrs asoc->cookie_how[how_indx] = 8; 1714171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1715163953Srrs sctp_stop_all_cookie_timers(stcb); 1716163953Srrs /* 1717163953Srrs * since we did not send a HB make sure we don't double 1718163953Srrs * things 1719163953Srrs */ 1720163953Srrs net->hb_responded = 1; 1721163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1722163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1723163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1724163953Srrs NULL); 1725163953Srrs } 1726163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1727164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1728164205Srrs 1729164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1730164205Srrs /* 1731164205Srrs * Ok the peer probably discarded our data (if we 1732164205Srrs * echoed a cookie+data). So anything on the 1733164205Srrs * sent_queue should be marked for retransmit, we 1734164205Srrs * may not get something to kick us so it COULD 1735164205Srrs * still take a timeout to move these.. but it can't 1736164205Srrs * hurt to mark them. 1737164205Srrs */ 1738164205Srrs struct sctp_tmit_chunk *chk; 1739163953Srrs 1740164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1741164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1742164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1743171440Srrs sctp_flight_size_decrease(chk); 1744171440Srrs sctp_total_flight_decrease(stcb, chk); 1745168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1746164205Srrs spec_flag++; 1747164205Srrs } 1748164205Srrs } 1749164205Srrs 1750164205Srrs } 1751163953Srrs /* process the INIT info (peer's info) */ 1752163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1753163953Srrs if (retval < 0) { 1754166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1755166023Srrs asoc->cookie_how[how_indx] = 9; 1756163953Srrs return (NULL); 1757163953Srrs } 1758163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1759163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1760163953Srrs initack_offset, sh, init_src)) { 1761166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1762166023Srrs asoc->cookie_how[how_indx] = 10; 1763163953Srrs return (NULL); 1764163953Srrs } 1765163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1766163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1767163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1768163953Srrs 1769163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1770163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1771163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1772172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1773172090Srrs struct socket *so; 1774172090Srrs 1775172090Srrs#endif 1776163953Srrs stcb->sctp_ep->sctp_flags |= 1777163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1778172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1779172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1780172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1781172090Srrs SCTP_TCB_UNLOCK(stcb); 1782172090Srrs SCTP_SOCKET_LOCK(so, 1); 1783172090Srrs SCTP_TCB_LOCK(stcb); 1784172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1785172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1786172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1787172090Srrs return (NULL); 1788172090Srrs } 1789172090Srrs#endif 1790172090Srrs soisconnected(stcb->sctp_socket); 1791172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1792172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1793172090Srrs#endif 1794163953Srrs } 1795166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1796166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1797166675Srrs else 1798166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1799166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1800166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1801166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1802166675Srrs } else { 1803166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1804163953Srrs } 1805171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1806163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1807163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1808163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1809163953Srrs } 1810163953Srrs sctp_stop_all_cookie_timers(stcb); 1811165220Srrs sctp_toss_old_cookies(stcb, asoc); 1812163953Srrs sctp_send_cookie_ack(stcb); 1813164205Srrs if (spec_flag) { 1814164205Srrs /* 1815164205Srrs * only if we have retrans set do we do this. What 1816164205Srrs * this call does is get only the COOKIE-ACK out and 1817164205Srrs * then when we return the normal call to 1818164205Srrs * sctp_chunk_output will get the retrans out behind 1819164205Srrs * this. 1820164205Srrs */ 1821172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1822164205Srrs } 1823166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1824166023Srrs asoc->cookie_how[how_indx] = 11; 1825166023Srrs 1826163953Srrs return (stcb); 1827163953Srrs } 1828163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1829163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1830163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1831163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1832163953Srrs cookie->tie_tag_peer_vtag != 0) { 1833163953Srrs struct sctpasochead *head; 1834163953Srrs 1835185694Srrs if (asoc->peer_supports_nat) { 1836185694Srrs /* 1837185694Srrs * This is a gross gross hack. just call the 1838185694Srrs * cookie_new code since we are allowing a duplicate 1839185694Srrs * association. I hope this works... 1840185694Srrs */ 1841185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1842185694Srrs inp, netp, init_src, notification, 1843185694Srrs auth_skipped, auth_offset, auth_len, 1844185694Srrs vrf_id, port)); 1845185694Srrs } 1846163953Srrs /* 1847163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1848163953Srrs */ 1849165220Srrs /* temp code */ 1850166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1851166023Srrs asoc->cookie_how[how_indx] = 12; 1852171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1853171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1854165647Srrs 1855163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1856163953Srrs /* notify upper layer */ 1857163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1858165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1859166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1860166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1861166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1862166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1863166675Srrs } 1864166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1865166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1866166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1867166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1868166675Srrs } 1869165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1870171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1871165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1872165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1873165647Srrs 1874165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1875165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1876171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1877165647Srrs } 1878165647Srrs asoc->pre_open_streams = 1879165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1880165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1881165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1882179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1883165647Srrs 1884165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1885165647Srrs 1886165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1887165647Srrs 1888165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1889170138Srrs if (asoc->mapping_array) { 1890165647Srrs memset(asoc->mapping_array, 0, 1891165647Srrs asoc->mapping_array_size); 1892170138Srrs } 1893205627Srrs if (asoc->nr_mapping_array) { 1894185694Srrs memset(asoc->nr_mapping_array, 0, 1895206137Stuexen asoc->mapping_array_size); 1896185694Srrs } 1897165220Srrs SCTP_TCB_UNLOCK(stcb); 1898165220Srrs SCTP_INP_INFO_WLOCK(); 1899165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1900165220Srrs SCTP_TCB_LOCK(stcb); 1901165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1902163953Srrs /* send up all the data */ 1903164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1904165220Srrs 1905172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1906165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1907165220Srrs stcb->asoc.strmout[i].stream_no = i; 1908165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1909165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1910165220Srrs } 1911163953Srrs /* process the INIT-ACK info (my info) */ 1912163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1913163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1914163953Srrs 1915163953Srrs /* pull from vtag hash */ 1916163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1917163953Srrs /* re-insert to new vtag position */ 1918179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1919179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1920163953Srrs /* 1921163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1922163953Srrs * system 1923163953Srrs */ 1924163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1925163953Srrs 1926163953Srrs /* process the INIT info (peer's info) */ 1927164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1928165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1929165220Srrs SCTP_INP_INFO_WUNLOCK(); 1930165220Srrs 1931163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1932163953Srrs if (retval < 0) { 1933166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1934166023Srrs asoc->cookie_how[how_indx] = 13; 1935166023Srrs 1936163953Srrs return (NULL); 1937163953Srrs } 1938163953Srrs /* 1939163953Srrs * since we did not send a HB make sure we don't double 1940163953Srrs * things 1941163953Srrs */ 1942163953Srrs net->hb_responded = 1; 1943163953Srrs 1944163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1945163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1946163953Srrs initack_offset, sh, init_src)) { 1947166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1948166023Srrs asoc->cookie_how[how_indx] = 14; 1949166023Srrs 1950163953Srrs return (NULL); 1951163953Srrs } 1952163953Srrs /* respond with a COOKIE-ACK */ 1953163953Srrs sctp_stop_all_cookie_timers(stcb); 1954165220Srrs sctp_toss_old_cookies(stcb, asoc); 1955163953Srrs sctp_send_cookie_ack(stcb); 1956166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1957166023Srrs asoc->cookie_how[how_indx] = 15; 1958163953Srrs 1959163953Srrs return (stcb); 1960163953Srrs } 1961166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1962166023Srrs asoc->cookie_how[how_indx] = 16; 1963163953Srrs /* all other cases... */ 1964163953Srrs return (NULL); 1965163953Srrs} 1966163953Srrs 1967166086Srrs 1968163953Srrs/* 1969163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1970163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1971163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1972163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1973163953Srrs * was from returns a new TCB 1974163953Srrs */ 1975185694Srrsstruct sctp_tcb * 1976163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1977163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1978163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1979163953Srrs struct sockaddr *init_src, int *notification, 1980169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1981179157Srrs uint32_t vrf_id, uint16_t port) 1982163953Srrs{ 1983163953Srrs struct sctp_tcb *stcb; 1984163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1985163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1986163953Srrs struct sockaddr_storage sa_store; 1987163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1988163953Srrs struct sctp_association *asoc; 1989163953Srrs int chk_length; 1990163953Srrs int init_offset, initack_offset, initack_limit; 1991163953Srrs int retval; 1992163953Srrs int error = 0; 1993163953Srrs uint32_t old_tag; 1994166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1995163953Srrs 1996221249Stuexen#ifdef INET 1997221249Stuexen struct sockaddr_in *sin; 1998221249Stuexen 1999221249Stuexen#endif 2000221249Stuexen#ifdef INET6 2001221249Stuexen struct sockaddr_in6 *sin6; 2002221249Stuexen 2003221249Stuexen#endif 2004172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2005172090Srrs struct socket *so; 2006172090Srrs 2007172090Srrs so = SCTP_INP_SO(inp); 2008172090Srrs#endif 2009172090Srrs 2010163953Srrs /* 2011163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 2012163953Srrs * INIT should start after the cookie-echo header struct (chunk 2013163953Srrs * header, state cookie header struct) 2014163953Srrs */ 2015163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2016163953Srrs init_cp = (struct sctp_init_chunk *) 2017163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2018163953Srrs (uint8_t *) & init_buf); 2019163953Srrs if (init_cp == NULL) { 2020163953Srrs /* could not pull a INIT chunk in cookie */ 2021169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2022169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 2023163953Srrs return (NULL); 2024163953Srrs } 2025163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 2026163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2027169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2028163953Srrs return (NULL); 2029163953Srrs } 2030163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 2031163953Srrs /* 2032163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 2033163953Srrs * INIT-ACK follows the INIT chunk 2034163953Srrs */ 2035163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2036163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2037163953Srrs (uint8_t *) & initack_buf); 2038163953Srrs if (initack_cp == NULL) { 2039163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2040169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2041163953Srrs return (NULL); 2042163953Srrs } 2043163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 2044163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2045163953Srrs return (NULL); 2046163953Srrs } 2047163953Srrs /* 2048163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2049163953Srrs * "initack_limit" value. This is because the chk_length field 2050163953Srrs * includes the length of the cookie, but the cookie is omitted when 2051163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2052163953Srrs */ 2053163953Srrs initack_limit = offset + cookie_len; 2054163953Srrs 2055163953Srrs /* 2056163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2057163953Srrs * and popluate 2058163953Srrs */ 2059171531Srrs 2060171531Srrs /* 2061171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2062171531Srrs * We do this since in effect we only use the p argument when the 2063171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2064171531Srrs * getting a cookie, we cannot be unbound. 2065171531Srrs */ 2066206137Stuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2067171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2068171531Srrs (struct thread *)NULL 2069171531Srrs ); 2070163953Srrs if (stcb == NULL) { 2071163953Srrs struct mbuf *op_err; 2072163953Srrs 2073163953Srrs /* memory problem? */ 2074169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2075169420Srrs "process_cookie_new: no room for another TCB!\n"); 2076163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2077170140Srrs 2078163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2079179157Srrs sh, op_err, vrf_id, port); 2080163953Srrs return (NULL); 2081163953Srrs } 2082163953Srrs /* get the correct sctp_nets */ 2083169420Srrs if (netp) 2084169420Srrs *netp = sctp_findnet(stcb, init_src); 2085169420Srrs 2086163953Srrs asoc = &stcb->asoc; 2087163953Srrs /* get scope variables out of cookie */ 2088163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2089163953Srrs asoc->site_scope = cookie->site_scope; 2090163953Srrs asoc->local_scope = cookie->local_scope; 2091163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2092163953Srrs 2093163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2094163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2095163953Srrs struct mbuf *op_err; 2096163953Srrs 2097163953Srrs /* 2098163953Srrs * Houston we have a problem. The EP changed while the 2099163953Srrs * cookie was in flight. Only recourse is to abort the 2100163953Srrs * association. 2101163953Srrs */ 2102170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2103163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2104163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2105179157Srrs sh, op_err, vrf_id, port); 2106172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2107172090Srrs SCTP_TCB_UNLOCK(stcb); 2108172090Srrs SCTP_SOCKET_LOCK(so, 1); 2109172090Srrs SCTP_TCB_LOCK(stcb); 2110172090Srrs#endif 2111171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2112171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2113172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2114172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2115172090Srrs#endif 2116172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2117163953Srrs return (NULL); 2118163953Srrs } 2119163953Srrs /* process the INIT-ACK info (my info) */ 2120163953Srrs old_tag = asoc->my_vtag; 2121185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2122163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2123163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2124163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2125163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2126179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2127163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2128163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2129163953Srrs 2130163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2131163953Srrs 2132163953Srrs /* process the INIT info (peer's info) */ 2133169420Srrs if (netp) 2134169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 2135169420Srrs else 2136169420Srrs retval = 0; 2137163953Srrs if (retval < 0) { 2138170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2139172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2140172090Srrs SCTP_TCB_UNLOCK(stcb); 2141172090Srrs SCTP_SOCKET_LOCK(so, 1); 2142172090Srrs SCTP_TCB_LOCK(stcb); 2143172090Srrs#endif 2144171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2145172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2146172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2147172090Srrs#endif 2148172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2149163953Srrs return (NULL); 2150163953Srrs } 2151163953Srrs /* load all addresses */ 2152163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 2153163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2154163953Srrs init_src)) { 2155170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2156172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2157172090Srrs SCTP_TCB_UNLOCK(stcb); 2158172090Srrs SCTP_SOCKET_LOCK(so, 1); 2159172090Srrs SCTP_TCB_LOCK(stcb); 2160172090Srrs#endif 2161171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2162172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2163172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2164172090Srrs#endif 2165172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2166163953Srrs return (NULL); 2167163953Srrs } 2168163953Srrs /* 2169163953Srrs * verify any preceding AUTH chunk that was skipped 2170163953Srrs */ 2171163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2172163953Srrs sctp_auth_get_cookie_params(stcb, m, 2173163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2174163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2175163953Srrs if (auth_skipped) { 2176163953Srrs struct sctp_auth_chunk *auth; 2177163953Srrs 2178163953Srrs auth = (struct sctp_auth_chunk *) 2179166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2180169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2181163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2182169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2183169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2184171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2185172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2186172090Srrs SCTP_TCB_UNLOCK(stcb); 2187172090Srrs SCTP_SOCKET_LOCK(so, 1); 2188172090Srrs SCTP_TCB_LOCK(stcb); 2189172090Srrs#endif 2190171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2191172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2192172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2193172090Srrs#endif 2194172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2195163953Srrs return (NULL); 2196163953Srrs } else { 2197163953Srrs /* remaining chunks checked... good to go */ 2198163953Srrs stcb->asoc.authenticated = 1; 2199163953Srrs } 2200163953Srrs } 2201163953Srrs /* update current state */ 2202169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2203171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2204163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2205163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2206163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2207163953Srrs } 2208165220Srrs sctp_stop_all_cookie_timers(stcb); 2209163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2210163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2211163953Srrs 2212163953Srrs /* 2213163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2214163953Srrs * addresses that need to get added to the peer (eg. addresses 2215163953Srrs * changed while cookie echo in flight). This needs to be done 2216163953Srrs * after we go to the OPEN state to do the correct asconf 2217163953Srrs * processing. else, make sure we have the correct addresses in our 2218163953Srrs * lists 2219163953Srrs */ 2220163953Srrs 2221163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2222163953Srrs /* pull in local_address (our "from" address) */ 2223221249Stuexen switch (cookie->laddr_type) { 2224221249Stuexen#ifdef INET 2225221249Stuexen case SCTP_IPV4_ADDRESS: 2226163953Srrs /* source addr is IPv4 */ 2227163953Srrs sin = (struct sockaddr_in *)initack_src; 2228163953Srrs memset(sin, 0, sizeof(*sin)); 2229163953Srrs sin->sin_family = AF_INET; 2230163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2231163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2232221249Stuexen break; 2233221249Stuexen#endif 2234221249Stuexen#ifdef INET6 2235221249Stuexen case SCTP_IPV6_ADDRESS: 2236163953Srrs /* source addr is IPv6 */ 2237163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2238163953Srrs memset(sin6, 0, sizeof(*sin6)); 2239163953Srrs sin6->sin6_family = AF_INET6; 2240163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2241163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2242163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2243163953Srrs sizeof(sin6->sin6_addr)); 2244221249Stuexen break; 2245221249Stuexen#endif 2246221249Stuexen default: 2247170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2248172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2249172090Srrs SCTP_TCB_UNLOCK(stcb); 2250172090Srrs SCTP_SOCKET_LOCK(so, 1); 2251172090Srrs SCTP_TCB_LOCK(stcb); 2252172090Srrs#endif 2253171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2254172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2255172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2256172090Srrs#endif 2257172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2258163953Srrs return (NULL); 2259163953Srrs } 2260163953Srrs 2261163953Srrs /* set up to notify upper layer */ 2262163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2263163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2264163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2265163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2266163953Srrs /* 2267163953Srrs * This is an endpoint that called connect() how it got a 2268163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2269163953Srrs * the INIT was sent, but before it got there.. a complete 2270163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2271163953Srrs * should have went to the other code.. not here.. oh well.. 2272163953Srrs * a bit of protection is worth having.. 2273163953Srrs */ 2274163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2275172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2276172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2277172090Srrs SCTP_TCB_UNLOCK(stcb); 2278172090Srrs SCTP_SOCKET_LOCK(so, 1); 2279172090Srrs SCTP_TCB_LOCK(stcb); 2280172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2281172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2282172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2283172090Srrs return (NULL); 2284172090Srrs } 2285172090Srrs#endif 2286172090Srrs soisconnected(stcb->sctp_socket); 2287172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2288172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2289172090Srrs#endif 2290163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2291163953Srrs (inp->sctp_socket->so_qlimit)) { 2292163953Srrs /* 2293163953Srrs * We don't want to do anything with this one. Since it is 2294163953Srrs * the listening guy. The timer will get started for 2295163953Srrs * accepted connections in the caller. 2296163953Srrs */ 2297163953Srrs ; 2298163953Srrs } 2299163953Srrs /* since we did not send a HB make sure we don't double things */ 2300169420Srrs if ((netp) && (*netp)) 2301169420Srrs (*netp)->hb_responded = 1; 2302163953Srrs 2303163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2304163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2305163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2306163953Srrs } 2307165220Srrs /* calculate the RTT */ 2308171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2309170642Srrs if ((netp) && (*netp)) { 2310169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2311218186Srrs &cookie->time_entered, sctp_align_unsafe_makecopy, 2312219397Srrs SCTP_RTT_FROM_NON_DATA); 2313170642Srrs } 2314172190Srrs /* respond with a COOKIE-ACK */ 2315163953Srrs sctp_send_cookie_ack(stcb); 2316172190Srrs 2317172190Srrs /* 2318172190Srrs * check the address lists for any ASCONFs that need to be sent 2319172190Srrs * AFTER the cookie-ack is sent 2320172190Srrs */ 2321172190Srrs sctp_check_address_list(stcb, m, 2322172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2323172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2324172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2325172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2326172190Srrs 2327172190Srrs 2328163953Srrs return (stcb); 2329163953Srrs} 2330163953Srrs 2331185694Srrs/* 2332185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2333185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2334185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2335185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2336185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2337185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2338185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2339185694Srrs -- SEND ABORT - TRY AGAIN -- 2340185694Srrs } 2341185694Srrs } 2342185694Srrs*/ 2343163953Srrs 2344163953Srrs/* 2345163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2346163953Srrs * existing (non-NULL) TCB 2347163953Srrs */ 2348163953Srrsstatic struct mbuf * 2349163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2350163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2351163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2352169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2353179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2354163953Srrs{ 2355163953Srrs struct sctp_state_cookie *cookie; 2356163953Srrs struct sctp_tcb *l_stcb = *stcb; 2357163953Srrs struct sctp_inpcb *l_inp; 2358163953Srrs struct sockaddr *to; 2359163953Srrs sctp_assoc_t sac_restart_id; 2360163953Srrs struct sctp_pcb *ep; 2361163953Srrs struct mbuf *m_sig; 2362163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2363163953Srrs uint8_t *sig; 2364163953Srrs uint8_t cookie_ok = 0; 2365163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2366163953Srrs unsigned int cookie_len; 2367163953Srrs struct timeval now; 2368163953Srrs struct timeval time_expires; 2369163953Srrs struct sockaddr_storage dest_store; 2370163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2371163953Srrs struct ip *iph; 2372163953Srrs int notification = 0; 2373163953Srrs struct sctp_nets *netl; 2374163953Srrs int had_a_existing_tcb = 0; 2375216887Stuexen int send_int_conf = 0; 2376163953Srrs 2377221249Stuexen#ifdef INET 2378221249Stuexen struct sockaddr_in sin; 2379221249Stuexen 2380221249Stuexen#endif 2381221249Stuexen#ifdef INET6 2382221249Stuexen struct sockaddr_in6 sin6; 2383221249Stuexen 2384221249Stuexen#endif 2385221249Stuexen 2386169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2387169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2388163953Srrs 2389163953Srrs if (inp_p == NULL) { 2390163953Srrs return (NULL); 2391163953Srrs } 2392163953Srrs /* First get the destination address setup too. */ 2393163953Srrs iph = mtod(m, struct ip *); 2394178251Srrs switch (iph->ip_v) { 2395221249Stuexen#ifdef INET 2396178251Srrs case IPVERSION: 2397178251Srrs { 2398178251Srrs /* its IPv4 */ 2399178251Srrs struct sockaddr_in *lsin; 2400163953Srrs 2401178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2402178251Srrs memset(lsin, 0, sizeof(*lsin)); 2403178251Srrs lsin->sin_family = AF_INET; 2404178251Srrs lsin->sin_len = sizeof(*lsin); 2405178251Srrs lsin->sin_port = sh->dest_port; 2406178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2407178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2408178251Srrs break; 2409178251Srrs } 2410221249Stuexen#endif 2411178251Srrs#ifdef INET6 2412178251Srrs case IPV6_VERSION >> 4: 2413178251Srrs { 2414178251Srrs /* its IPv6 */ 2415178251Srrs struct ip6_hdr *ip6; 2416178251Srrs struct sockaddr_in6 *lsin6; 2417163953Srrs 2418178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2419178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2420178251Srrs lsin6->sin6_family = AF_INET6; 2421178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2422178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2423178251Srrs lsin6->sin6_port = sh->dest_port; 2424178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2425178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2426178251Srrs break; 2427178251Srrs } 2428178251Srrs#endif 2429178251Srrs default: 2430163953Srrs return (NULL); 2431163953Srrs } 2432163953Srrs 2433163953Srrs cookie = &cp->cookie; 2434163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2435163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2436163953Srrs 2437163953Srrs if ((cookie->peerport != sh->src_port) && 2438163953Srrs (cookie->myport != sh->dest_port) && 2439163953Srrs (cookie->my_vtag != sh->v_tag)) { 2440163953Srrs /* 2441163953Srrs * invalid ports or bad tag. Note that we always leave the 2442163953Srrs * v_tag in the header in network order and when we stored 2443163953Srrs * it in the my_vtag slot we also left it in network order. 2444169352Srrs * This maintains the match even though it may be in the 2445163953Srrs * opposite byte order of the machine :-> 2446163953Srrs */ 2447163953Srrs return (NULL); 2448163953Srrs } 2449163953Srrs if (cookie_len > size_of_pkt || 2450163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2451163953Srrs sizeof(struct sctp_init_chunk) + 2452163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2453163953Srrs /* cookie too long! or too small */ 2454163953Srrs return (NULL); 2455163953Srrs } 2456163953Srrs /* 2457163953Srrs * split off the signature into its own mbuf (since it should not be 2458163953Srrs * calculated in the sctp_hmac_m() call). 2459163953Srrs */ 2460163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2461163953Srrs if (sig_offset > size_of_pkt) { 2462163953Srrs /* packet not correct size! */ 2463163953Srrs /* XXX this may already be accounted for earlier... */ 2464163953Srrs return (NULL); 2465163953Srrs } 2466163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2467163953Srrs if (m_sig == NULL) { 2468163953Srrs /* out of memory or ?? */ 2469163953Srrs return (NULL); 2470163953Srrs } 2471178202Srrs#ifdef SCTP_MBUF_LOGGING 2472179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2473178202Srrs struct mbuf *mat; 2474178202Srrs 2475178202Srrs mat = m_sig; 2476178202Srrs while (mat) { 2477178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2478178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2479178202Srrs } 2480178202Srrs mat = SCTP_BUF_NEXT(mat); 2481178202Srrs } 2482178202Srrs } 2483178202Srrs#endif 2484178202Srrs 2485163953Srrs /* 2486163953Srrs * compute the signature/digest for the cookie 2487163953Srrs */ 2488163953Srrs ep = &(*inp_p)->sctp_ep; 2489163953Srrs l_inp = *inp_p; 2490163953Srrs if (l_stcb) { 2491163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2492163953Srrs } 2493163953Srrs SCTP_INP_RLOCK(l_inp); 2494163953Srrs if (l_stcb) { 2495163953Srrs SCTP_TCB_LOCK(l_stcb); 2496163953Srrs } 2497163953Srrs /* which cookie is it? */ 2498163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2499163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2500163953Srrs /* it's the old cookie */ 2501169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2502163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2503170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2504163953Srrs } else { 2505163953Srrs /* it's the current cookie */ 2506169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2507163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2508170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2509163953Srrs } 2510163953Srrs /* get the signature */ 2511163953Srrs SCTP_INP_RUNLOCK(l_inp); 2512163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2513163953Srrs if (sig == NULL) { 2514163953Srrs /* couldn't find signature */ 2515164085Srrs sctp_m_freem(m_sig); 2516163953Srrs return (NULL); 2517163953Srrs } 2518163953Srrs /* compare the received digest with the computed digest */ 2519163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2520163953Srrs /* try the old cookie? */ 2521163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2522163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2523163953Srrs /* compute digest with old */ 2524169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2525163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2526170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2527163953Srrs /* compare */ 2528163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2529163953Srrs cookie_ok = 1; 2530163953Srrs } 2531163953Srrs } else { 2532163953Srrs cookie_ok = 1; 2533163953Srrs } 2534163953Srrs 2535163953Srrs /* 2536163953Srrs * Now before we continue we must reconstruct our mbuf so that 2537163953Srrs * normal processing of any other chunks will work. 2538163953Srrs */ 2539163953Srrs { 2540163953Srrs struct mbuf *m_at; 2541163953Srrs 2542163953Srrs m_at = m; 2543165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2544165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2545163953Srrs } 2546165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2547163953Srrs } 2548163953Srrs 2549163953Srrs if (cookie_ok == 0) { 2550169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2551169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2552169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2553169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2554163953Srrs return (NULL); 2555163953Srrs } 2556163953Srrs /* 2557163953Srrs * check the cookie timestamps to be sure it's not stale 2558163953Srrs */ 2559169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2560163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2561169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2562163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2563180387Srrs /* 2564180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2565180387Srrs * is undefined. 2566180387Srrs */ 2567163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2568163953Srrs /* cookie is stale! */ 2569163953Srrs struct mbuf *op_err; 2570163953Srrs struct sctp_stale_cookie_msg *scm; 2571163953Srrs uint32_t tim; 2572163953Srrs 2573163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2574165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2575163953Srrs if (op_err == NULL) { 2576163953Srrs /* FOOBAR */ 2577163953Srrs return (NULL); 2578163953Srrs } 2579163953Srrs /* Set the len */ 2580165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2581163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2582163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2583163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2584163953Srrs (sizeof(uint32_t)))); 2585163953Srrs /* seconds to usec */ 2586163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2587163953Srrs /* add in usec */ 2588163953Srrs if (tim == 0) 2589163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2590163953Srrs scm->time_usec = htonl(tim); 2591169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2592179157Srrs vrf_id, port); 2593163953Srrs return (NULL); 2594163953Srrs } 2595163953Srrs /* 2596163953Srrs * Now we must see with the lookup address if we have an existing 2597163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2598163953Srrs * and a INIT collided with us and somewhere the peer sent the 2599163953Srrs * cookie on another address besides the single address our assoc 2600163953Srrs * had for him. In this case we will have one of the tie-tags set at 2601163953Srrs * least AND the address field in the cookie can be used to look it 2602163953Srrs * up. 2603163953Srrs */ 2604163953Srrs to = NULL; 2605221249Stuexen switch (cookie->addr_type) { 2606221249Stuexen#ifdef INET6 2607221249Stuexen case SCTP_IPV6_ADDRESS: 2608163953Srrs memset(&sin6, 0, sizeof(sin6)); 2609163953Srrs sin6.sin6_family = AF_INET6; 2610163953Srrs sin6.sin6_len = sizeof(sin6); 2611163953Srrs sin6.sin6_port = sh->src_port; 2612163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2613163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2614163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2615163953Srrs to = (struct sockaddr *)&sin6; 2616221249Stuexen break; 2617221249Stuexen#endif 2618221249Stuexen#ifdef INET 2619221249Stuexen case SCTP_IPV4_ADDRESS: 2620163953Srrs memset(&sin, 0, sizeof(sin)); 2621163953Srrs sin.sin_family = AF_INET; 2622163953Srrs sin.sin_len = sizeof(sin); 2623163953Srrs sin.sin_port = sh->src_port; 2624163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2625163953Srrs to = (struct sockaddr *)&sin; 2626221249Stuexen break; 2627221249Stuexen#endif 2628221249Stuexen default: 2629169420Srrs /* This should not happen */ 2630169420Srrs return (NULL); 2631163953Srrs } 2632163953Srrs if ((*stcb == NULL) && to) { 2633163953Srrs /* Yep, lets check */ 2634163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2635163953Srrs if (*stcb == NULL) { 2636163953Srrs /* 2637163953Srrs * We should have only got back the same inp. If we 2638163953Srrs * got back a different ep we have a problem. The 2639163953Srrs * original findep got back l_inp and now 2640163953Srrs */ 2641163953Srrs if (l_inp != *inp_p) { 2642169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2643163953Srrs } 2644165220Srrs } else { 2645165220Srrs if (*locked_tcb == NULL) { 2646165220Srrs /* 2647165220Srrs * In this case we found the assoc only 2648165220Srrs * after we locked the create lock. This 2649165220Srrs * means we are in a colliding case and we 2650165220Srrs * must make sure that we unlock the tcb if 2651165220Srrs * its one of the cases where we throw away 2652165220Srrs * the incoming packets. 2653165220Srrs */ 2654165220Srrs *locked_tcb = *stcb; 2655165220Srrs 2656165220Srrs /* 2657165220Srrs * We must also increment the inp ref count 2658165220Srrs * since the ref_count flags was set when we 2659165220Srrs * did not find the TCB, now we found it 2660165220Srrs * which reduces the refcount.. we must 2661165220Srrs * raise it back out to balance it all :-) 2662165220Srrs */ 2663165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2664165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2665169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2666165220Srrs (*stcb)->sctp_ep, l_inp); 2667165220Srrs } 2668165220Srrs } 2669163953Srrs } 2670163953Srrs } 2671188067Srrs if (to == NULL) { 2672169420Srrs return (NULL); 2673188067Srrs } 2674163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2675163953Srrs if (*stcb == NULL) { 2676163953Srrs /* this is the "normal" case... get a new TCB */ 2677163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2678163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2679179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2680163953Srrs } else { 2681163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2682163953Srrs had_a_existing_tcb = 1; 2683163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2684185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2685185694Srrs ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port); 2686163953Srrs } 2687163953Srrs 2688163953Srrs if (*stcb == NULL) { 2689163953Srrs /* still no TCB... must be bad cookie-echo */ 2690163953Srrs return (NULL); 2691163953Srrs } 2692218335Stuexen if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2693218335Stuexen (*netp)->flowid = m->m_pkthdr.flowid; 2694218400Stuexen#ifdef INVARIANTS 2695218392Srrs (*netp)->flowidset = 1; 2696218400Stuexen#endif 2697218335Stuexen } 2698163953Srrs /* 2699163953Srrs * Ok, we built an association so confirm the address we sent the 2700163953Srrs * INIT-ACK to. 2701163953Srrs */ 2702163953Srrs netl = sctp_findnet(*stcb, to); 2703163953Srrs /* 2704163953Srrs * This code should in theory NOT run but 2705163953Srrs */ 2706163953Srrs if (netl == NULL) { 2707163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2708163953Srrs int ret; 2709163953Srrs 2710224641Stuexen ret = sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, 2711165220Srrs SCTP_IN_COOKIE_PROC); 2712163953Srrs netl = sctp_findnet(*stcb, to); 2713163953Srrs } 2714163953Srrs if (netl) { 2715163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2716163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2717169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2718163953Srrs netl); 2719216887Stuexen send_int_conf = 1; 2720163953Srrs } 2721163953Srrs } 2722224641Stuexen sctp_start_net_timers(*stcb); 2723163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2724163953Srrs if (!had_a_existing_tcb || 2725163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2726163953Srrs /* 2727163953Srrs * If we have a NEW cookie or the connect never 2728163953Srrs * reached the connected state during collision we 2729163953Srrs * must do the TCP accept thing. 2730163953Srrs */ 2731163953Srrs struct socket *so, *oso; 2732163953Srrs struct sctp_inpcb *inp; 2733163953Srrs 2734163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2735163953Srrs /* 2736163953Srrs * For a restart we will keep the same 2737163953Srrs * socket, no need to do anything. I THINK!! 2738163953Srrs */ 2739172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2740216887Stuexen if (send_int_conf) { 2741216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2742216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2743216887Stuexen } 2744163953Srrs return (m); 2745163953Srrs } 2746163953Srrs oso = (*inp_p)->sctp_socket; 2747171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2748163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2749218757Sbz CURVNET_SET(oso->so_vnet); 2750166086Srrs so = sonewconn(oso, 0 2751163953Srrs ); 2752218757Sbz CURVNET_RESTORE(); 2753163953Srrs SCTP_TCB_LOCK((*stcb)); 2754171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2755171858Srrs 2756163953Srrs if (so == NULL) { 2757163953Srrs struct mbuf *op_err; 2758163953Srrs 2759172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2760172090Srrs struct socket *pcb_so; 2761172090Srrs 2762172090Srrs#endif 2763163953Srrs /* Too many sockets */ 2764169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2765163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2766163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2767179157Srrs sh, op_err, vrf_id, port); 2768172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2769172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2770172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2771172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2772172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2773172090Srrs SCTP_TCB_LOCK((*stcb)); 2774172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2775172090Srrs#endif 2776171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2777172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2778172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2779172090Srrs#endif 2780163953Srrs return (NULL); 2781163953Srrs } 2782163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2783166086Srrs SCTP_INP_INCR_REF(inp); 2784166086Srrs /* 2785166086Srrs * We add the unbound flag here so that if we get an 2786166086Srrs * soabort() before we get the move_pcb done, we 2787166086Srrs * will properly cleanup. 2788166086Srrs */ 2789163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2790163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2791163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2792166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2793163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2794163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2795163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2796172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2797163953Srrs inp->sctp_socket = so; 2798163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2799211944Stuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2800218235Stuexen inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2801163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2802163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2803163953Srrs inp->inp_starting_point_for_iterator = NULL; 2804163953Srrs /* 2805163953Srrs * copy in the authentication parameters from the 2806163953Srrs * original endpoint 2807163953Srrs */ 2808163953Srrs if (inp->sctp_ep.local_hmacs) 2809163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2810163953Srrs inp->sctp_ep.local_hmacs = 2811163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2812163953Srrs if (inp->sctp_ep.local_auth_chunks) 2813163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2814163953Srrs inp->sctp_ep.local_auth_chunks = 2815163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2816163953Srrs 2817163953Srrs /* 2818163953Srrs * Now we must move it from one hash table to 2819163953Srrs * another and get the tcb in the right place. 2820163953Srrs */ 2821207924Srrs 2822207924Srrs /* 2823207924Srrs * This is where the one-2-one socket is put into 2824207924Srrs * the accept state waiting for the accept! 2825207924Srrs */ 2826207924Srrs if (*stcb) { 2827207924Srrs (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2828207924Srrs } 2829163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2830170056Srrs 2831170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2832170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2833170056Srrs 2834175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2835175845Srwatson 0); 2836170056Srrs SCTP_TCB_LOCK((*stcb)); 2837170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2838163953Srrs 2839170056Srrs 2840166086Srrs /* 2841166086Srrs * now we must check to see if we were aborted while 2842166086Srrs * the move was going on and the lock/unlock 2843166086Srrs * happened. 2844166086Srrs */ 2845166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2846166086Srrs /* 2847166086Srrs * yep it was, we leave the assoc attached 2848166086Srrs * to the socket since the sctp_inpcb_free() 2849166086Srrs * call will send an abort for us. 2850166086Srrs */ 2851166086Srrs SCTP_INP_DECR_REF(inp); 2852166086Srrs return (NULL); 2853166086Srrs } 2854166086Srrs SCTP_INP_DECR_REF(inp); 2855163953Srrs /* Switch over to the new guy */ 2856163953Srrs *inp_p = inp; 2857172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2858216887Stuexen if (send_int_conf) { 2859216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2860216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2861216887Stuexen } 2862166086Srrs /* 2863166086Srrs * Pull it from the incomplete queue and wake the 2864166086Srrs * guy 2865166086Srrs */ 2866172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2867172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2868172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2869172090Srrs SCTP_SOCKET_LOCK(so, 1); 2870172090Srrs#endif 2871166086Srrs soisconnected(so); 2872172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2873172118Srrs SCTP_TCB_LOCK((*stcb)); 2874172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2875172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2876172090Srrs#endif 2877163953Srrs return (m); 2878163953Srrs } 2879163953Srrs } 2880216887Stuexen if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2881216887Stuexen if (notification) { 2882216887Stuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2883216887Stuexen } 2884216887Stuexen if (send_int_conf) { 2885216887Stuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2886216887Stuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2887216887Stuexen } 2888163953Srrs } 2889163953Srrs return (m); 2890163953Srrs} 2891163953Srrs 2892163953Srrsstatic void 2893163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2894163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2895163953Srrs{ 2896163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2897163953Srrs struct sctp_association *asoc; 2898163953Srrs 2899169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2900169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2901163953Srrs if (stcb == NULL) 2902163953Srrs return; 2903163953Srrs 2904163953Srrs asoc = &stcb->asoc; 2905163953Srrs 2906163953Srrs sctp_stop_all_cookie_timers(stcb); 2907163953Srrs /* process according to association state */ 2908163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2909163953Srrs /* state change only needed when I am in right state */ 2910169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2911171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2912224641Stuexen sctp_start_net_timers(stcb); 2913163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2914163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2915163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2916163953Srrs 2917163953Srrs } 2918163953Srrs /* update RTO */ 2919163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2920163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2921163953Srrs if (asoc->overall_error_count == 0) { 2922163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2923218186Srrs &asoc->time_entered, sctp_align_safe_nocopy, 2924219397Srrs SCTP_RTT_FROM_NON_DATA); 2925163953Srrs } 2926169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2927172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2928163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2929163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2930172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2931172090Srrs struct socket *so; 2932172090Srrs 2933172090Srrs#endif 2934163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2935172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2936172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2937172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2938172090Srrs SCTP_TCB_UNLOCK(stcb); 2939172090Srrs SCTP_SOCKET_LOCK(so, 1); 2940172090Srrs SCTP_TCB_LOCK(stcb); 2941172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2942218641Srrs#endif 2943218641Srrs if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2944218641Srrs soisconnected(stcb->sctp_socket); 2945172090Srrs } 2946172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2947172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2948172090Srrs#endif 2949163953Srrs } 2950163953Srrs /* 2951163953Srrs * since we did not send a HB make sure we don't double 2952163953Srrs * things 2953163953Srrs */ 2954163953Srrs net->hb_responded = 1; 2955163953Srrs 2956218641Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2957218641Srrs /* 2958218641Srrs * We don't need to do the asconf thing, nor hb or 2959218641Srrs * autoclose if the socket is closed. 2960218641Srrs */ 2961218641Srrs goto closed_socket; 2962218641Srrs } 2963218641Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2964218641Srrs stcb, net); 2965218641Srrs 2966218641Srrs 2967163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2968163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2969163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2970163953Srrs stcb->sctp_ep, stcb, NULL); 2971163953Srrs } 2972163953Srrs /* 2973171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2974171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2975171572Srrs * in flight) 2976163953Srrs */ 2977163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2978163953Srrs (stcb->asoc.peer_supports_asconf) && 2979163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2980171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2981163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2982163953Srrs stcb->sctp_ep, stcb, 2983163953Srrs stcb->asoc.primary_destination); 2984171572Srrs#else 2985172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2986172190Srrs SCTP_ADDR_NOT_LOCKED); 2987171572Srrs#endif 2988163953Srrs } 2989163953Srrs } 2990218641Srrsclosed_socket: 2991163953Srrs /* Toss the cookie if I can */ 2992163953Srrs sctp_toss_old_cookies(stcb, asoc); 2993163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2994163953Srrs /* Restart the timer if we have pending data */ 2995163953Srrs struct sctp_tmit_chunk *chk; 2996163953Srrs 2997163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2998216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2999163953Srrs } 3000163953Srrs} 3001163953Srrs 3002163953Srrsstatic void 3003163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 3004163953Srrs struct sctp_tcb *stcb) 3005163953Srrs{ 3006163953Srrs struct sctp_nets *net; 3007163953Srrs struct sctp_tmit_chunk *lchk; 3008218072Srrs struct sctp_ecne_chunk bkup; 3009218072Srrs uint8_t override_bit = 0; 3010218072Srrs uint32_t tsn, window_data_tsn; 3011218072Srrs int len; 3012218232Srrs unsigned int pkt_cnt; 3013163953Srrs 3014218072Srrs len = ntohs(cp->ch.chunk_length); 3015218072Srrs if ((len != sizeof(struct sctp_ecne_chunk)) && 3016218072Srrs (len != sizeof(struct old_sctp_ecne_chunk))) { 3017163953Srrs return; 3018163953Srrs } 3019218072Srrs if (len == sizeof(struct old_sctp_ecne_chunk)) { 3020218072Srrs /* Its the old format */ 3021218072Srrs memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3022218072Srrs bkup.num_pkts_since_cwr = htonl(1); 3023218072Srrs cp = &bkup; 3024218072Srrs } 3025163953Srrs SCTP_STAT_INCR(sctps_recvecne); 3026163953Srrs tsn = ntohl(cp->tsn); 3027218072Srrs pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3028218072Srrs lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3029163953Srrs if (lchk == NULL) { 3030218129Srrs window_data_tsn = stcb->asoc.sending_seq - 1; 3031163953Srrs } else { 3032218129Srrs window_data_tsn = lchk->rec.data.TSN_seq; 3033163953Srrs } 3034163953Srrs 3035218072Srrs /* Find where it was sent to if possible. */ 3036163953Srrs net = NULL; 3037216822Stuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3038163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 3039163953Srrs net = lchk->whoTo; 3040218186Srrs net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3041163953Srrs break; 3042163953Srrs } 3043216825Stuexen if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3044163953Srrs break; 3045216825Stuexen } 3046163953Srrs } 3047218072Srrs if (net == NULL) { 3048163953Srrs /* 3049218072Srrs * What to do. A previous send of a CWR was possibly lost. 3050218072Srrs * See how old it is, we may have it marked on the actual 3051218072Srrs * net. 3052218072Srrs */ 3053218072Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3054218072Srrs if (tsn == net->last_cwr_tsn) { 3055218072Srrs /* Found him, send it off */ 3056218072Srrs goto out; 3057218072Srrs } 3058218072Srrs } 3059218072Srrs /* 3060218072Srrs * If we reach here, we need to send a special CWR that says 3061218072Srrs * hey, we did this a long time ago and you lost the 3062218072Srrs * response. 3063218072Srrs */ 3064218072Srrs net = TAILQ_FIRST(&stcb->asoc.nets); 3065218072Srrs override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3066218072Srrs } 3067218072Srrsout: 3068218129Srrs if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3069218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3070218072Srrs /* 3071171440Srrs * JRS - Use the congestion control given in the pluggable 3072171440Srrs * CC module 3073171440Srrs */ 3074218072Srrs int ocwnd; 3075218072Srrs 3076218072Srrs ocwnd = net->cwnd; 3077218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3078171440Srrs /* 3079218072Srrs * We reduce once every RTT. So we will only lower cwnd at 3080218072Srrs * the next sending seq i.e. the window_data_tsn 3081163953Srrs */ 3082218072Srrs net->cwr_window_tsn = window_data_tsn; 3083218072Srrs net->ecn_ce_pkt_cnt += pkt_cnt; 3084218072Srrs net->lost_cnt = pkt_cnt; 3085218072Srrs net->last_cwr_tsn = tsn; 3086218072Srrs } else { 3087218072Srrs override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3088218129Srrs if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3089218129Srrs ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3090218072Srrs /* 3091218129Srrs * Another loss in the same window update how many 3092218129Srrs * marks/packets lost we have had. 3093218072Srrs */ 3094218129Srrs int cnt = 1; 3095218072Srrs 3096218072Srrs if (pkt_cnt > net->lost_cnt) { 3097218072Srrs /* Should be the case */ 3098218129Srrs cnt = (pkt_cnt - net->lost_cnt); 3099218129Srrs net->ecn_ce_pkt_cnt += cnt; 3100218072Srrs } 3101218129Srrs net->lost_cnt = pkt_cnt; 3102218072Srrs net->last_cwr_tsn = tsn; 3103218129Srrs /* 3104218129Srrs * Most CC functions will ignore this call, since we 3105218129Srrs * are in-window yet of the initial CE the peer saw. 3106218129Srrs */ 3107218129Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3108218072Srrs } 3109163953Srrs } 3110163953Srrs /* 3111163953Srrs * We always send a CWR this way if our previous one was lost our 3112163953Srrs * peer will get an update, or if it is not time again to reduce we 3113218072Srrs * still get the cwr to the peer. Note we set the override when we 3114218072Srrs * could not find the TSN on the chunk or the destination network. 3115163953Srrs */ 3116218072Srrs sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3117163953Srrs} 3118163953Srrs 3119163953Srrsstatic void 3120218072Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3121163953Srrs{ 3122163953Srrs /* 3123163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 3124163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 3125163953Srrs * If so remove it. 3126163953Srrs */ 3127163953Srrs struct sctp_tmit_chunk *chk; 3128163953Srrs struct sctp_ecne_chunk *ecne; 3129218072Srrs int override; 3130218072Srrs uint32_t cwr_tsn; 3131163953Srrs 3132218072Srrs cwr_tsn = ntohl(cp->tsn); 3133218072Srrs 3134218072Srrs override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3135163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3136163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3137163953Srrs continue; 3138163953Srrs } 3139218072Srrs if ((override == 0) && (chk->whoTo != net)) { 3140218072Srrs /* Must be from the right src unless override is set */ 3141218072Srrs continue; 3142218072Srrs } 3143163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3144218072Srrs if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3145163953Srrs /* this covers this ECNE, we can remove it */ 3146163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 3147163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3148163953Srrs sctp_next); 3149163953Srrs if (chk->data) { 3150163953Srrs sctp_m_freem(chk->data); 3151163953Srrs chk->data = NULL; 3152163953Srrs } 3153163953Srrs stcb->asoc.ctrl_queue_cnt--; 3154221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3155218072Srrs if (override == 0) { 3156218072Srrs break; 3157218072Srrs } 3158163953Srrs } 3159163953Srrs } 3160163953Srrs} 3161163953Srrs 3162163953Srrsstatic void 3163163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3164163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3165163953Srrs{ 3166163953Srrs struct sctp_association *asoc; 3167163953Srrs 3168172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3169172090Srrs struct socket *so; 3170172090Srrs 3171172090Srrs#endif 3172172090Srrs 3173169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3174169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3175163953Srrs if (stcb == NULL) 3176163953Srrs return; 3177163953Srrs 3178163953Srrs asoc = &stcb->asoc; 3179163953Srrs /* process according to association state */ 3180163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3181163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3182171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3183171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3184163953Srrs SCTP_TCB_UNLOCK(stcb); 3185163953Srrs return; 3186163953Srrs } 3187163953Srrs /* notify upper layer protocol */ 3188163953Srrs if (stcb->sctp_socket) { 3189172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3190163953Srrs /* are the queues empty? they should be */ 3191163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3192163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3193217760Stuexen !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3194172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3195163953Srrs } 3196163953Srrs } 3197163953Srrs /* stop the timer */ 3198178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3199163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3200163953Srrs /* free the TCB */ 3201171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3202171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3203172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3204172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3205172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3206172090Srrs SCTP_TCB_UNLOCK(stcb); 3207172090Srrs SCTP_SOCKET_LOCK(so, 1); 3208172090Srrs SCTP_TCB_LOCK(stcb); 3209172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3210172090Srrs#endif 3211171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3212172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3213172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3214172090Srrs#endif 3215163953Srrs return; 3216163953Srrs} 3217163953Srrs 3218163953Srrsstatic int 3219163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3220163953Srrs struct sctp_nets *net, uint8_t flg) 3221163953Srrs{ 3222163953Srrs switch (desc->chunk_type) { 3223211969Stuexen case SCTP_DATA: 3224163953Srrs /* find the tsn to resend (possibly */ 3225163953Srrs { 3226163953Srrs uint32_t tsn; 3227163953Srrs struct sctp_tmit_chunk *tp1; 3228163953Srrs 3229163953Srrs tsn = ntohl(desc->tsn_ifany); 3230216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3231163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3232163953Srrs /* found it */ 3233163953Srrs break; 3234163953Srrs } 3235216825Stuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3236163953Srrs /* not found */ 3237163953Srrs tp1 = NULL; 3238163953Srrs break; 3239163953Srrs } 3240163953Srrs } 3241163953Srrs if (tp1 == NULL) { 3242163953Srrs /* 3243163953Srrs * Do it the other way , aka without paying 3244163953Srrs * attention to queue seq order. 3245163953Srrs */ 3246163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3247216822Stuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3248163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3249163953Srrs /* found it */ 3250163953Srrs break; 3251163953Srrs } 3252163953Srrs } 3253163953Srrs } 3254163953Srrs if (tp1 == NULL) { 3255163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3256163953Srrs } 3257163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3258163953Srrs uint8_t *ddp; 3259163953Srrs 3260214877Stuexen if (((flg & SCTP_BADCRC) == 0) && 3261214877Stuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3262214877Stuexen return (0); 3263214877Stuexen } 3264163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3265163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3266163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3267163953Srrs return (0); 3268163953Srrs } 3269163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3270163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3271163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3272163953Srrs return (0); 3273163953Srrs } 3274163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3275163953Srrs sizeof(struct sctp_data_chunk)); 3276163953Srrs { 3277163953Srrs unsigned int iii; 3278163953Srrs 3279163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3280163953Srrs iii++) { 3281163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3282163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3283163953Srrs return (-1); 3284163953Srrs } 3285163953Srrs } 3286163953Srrs } 3287163953Srrs 3288163953Srrs if (tp1->do_rtt) { 3289163953Srrs /* 3290163953Srrs * this guy had a RTO calculation 3291163953Srrs * pending on it, cancel it 3292163953Srrs */ 3293219397Srrs if (tp1->whoTo->rto_needed == 0) { 3294219397Srrs tp1->whoTo->rto_needed = 1; 3295219397Srrs } 3296163953Srrs tp1->do_rtt = 0; 3297163953Srrs } 3298163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3299163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3300163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3301163953Srrs /* 3302163953Srrs * mark it as if we were doing a FR, since 3303163953Srrs * we will be getting gap ack reports behind 3304163953Srrs * the info from the router. 3305163953Srrs */ 3306163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3307163953Srrs /* 3308163953Srrs * mark the tsn with what sequences can 3309163953Srrs * cause a new FR. 3310163953Srrs */ 3311163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3312163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3313163953Srrs } else { 3314163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3315163953Srrs } 3316163953Srrs 3317163953Srrs /* restart the timer */ 3318163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3319171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3320163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3321163953Srrs stcb, tp1->whoTo); 3322163953Srrs 3323163953Srrs /* fix counts and things */ 3324179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3325170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3326170744Srrs tp1->whoTo->flight_size, 3327170744Srrs tp1->book_size, 3328170744Srrs (uintptr_t) stcb, 3329170744Srrs tp1->rec.data.TSN_seq); 3330170744Srrs } 3331190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3332190689Srrs sctp_flight_size_decrease(tp1); 3333190689Srrs sctp_total_flight_decrease(stcb, tp1); 3334190689Srrs } 3335216502Stuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3336163953Srrs } { 3337163953Srrs /* audit code */ 3338163953Srrs unsigned int audit; 3339163953Srrs 3340163953Srrs audit = 0; 3341163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3342163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3343163953Srrs audit++; 3344163953Srrs } 3345163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3346163953Srrs sctp_next) { 3347163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3348163953Srrs audit++; 3349163953Srrs } 3350163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3351169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3352163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3353163953Srrs#ifndef SCTP_AUDITING_ENABLED 3354163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3355163953Srrs#endif 3356163953Srrs } 3357163953Srrs } 3358163953Srrs } 3359163953Srrs break; 3360163953Srrs case SCTP_ASCONF: 3361163953Srrs { 3362163953Srrs struct sctp_tmit_chunk *asconf; 3363163953Srrs 3364163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3365163953Srrs sctp_next) { 3366163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3367163953Srrs break; 3368163953Srrs } 3369163953Srrs } 3370163953Srrs if (asconf) { 3371163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3372163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3373163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3374163953Srrs asconf->snd_count--; 3375163953Srrs } 3376163953Srrs } 3377163953Srrs break; 3378163953Srrs case SCTP_INITIATION: 3379163953Srrs /* resend the INIT */ 3380163953Srrs stcb->asoc.dropped_special_cnt++; 3381163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3382163953Srrs /* 3383163953Srrs * If we can get it in, in a few attempts we do 3384163953Srrs * this, otherwise we let the timer fire. 3385163953Srrs */ 3386163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3387171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3388172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3389163953Srrs } 3390163953Srrs break; 3391163953Srrs case SCTP_SELECTIVE_ACK: 3392206137Stuexen case SCTP_NR_SELECTIVE_ACK: 3393163953Srrs /* resend the sack */ 3394221627Stuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3395163953Srrs break; 3396163953Srrs case SCTP_HEARTBEAT_REQUEST: 3397163953Srrs /* resend a demand HB */ 3398171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3399171440Srrs /* 3400171440Srrs * Only retransmit if we KNOW we wont destroy the 3401171440Srrs * tcb 3402171440Srrs */ 3403224641Stuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3404171440Srrs } 3405163953Srrs break; 3406163953Srrs case SCTP_SHUTDOWN: 3407163953Srrs sctp_send_shutdown(stcb, net); 3408163953Srrs break; 3409163953Srrs case SCTP_SHUTDOWN_ACK: 3410163953Srrs sctp_send_shutdown_ack(stcb, net); 3411163953Srrs break; 3412163953Srrs case SCTP_COOKIE_ECHO: 3413163953Srrs { 3414163953Srrs struct sctp_tmit_chunk *cookie; 3415163953Srrs 3416163953Srrs cookie = NULL; 3417163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3418163953Srrs sctp_next) { 3419163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3420163953Srrs break; 3421163953Srrs } 3422163953Srrs } 3423163953Srrs if (cookie) { 3424163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3425163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3426163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3427163953Srrs sctp_stop_all_cookie_timers(stcb); 3428163953Srrs } 3429163953Srrs } 3430163953Srrs break; 3431163953Srrs case SCTP_COOKIE_ACK: 3432163953Srrs sctp_send_cookie_ack(stcb); 3433163953Srrs break; 3434163953Srrs case SCTP_ASCONF_ACK: 3435163953Srrs /* resend last asconf ack */ 3436171990Srrs sctp_send_asconf_ack(stcb); 3437163953Srrs break; 3438163953Srrs case SCTP_FORWARD_CUM_TSN: 3439163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3440163953Srrs break; 3441163953Srrs /* can't do anything with these */ 3442163953Srrs case SCTP_PACKET_DROPPED: 3443163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3444163953Srrs case SCTP_HEARTBEAT_ACK: 3445163953Srrs case SCTP_ABORT_ASSOCIATION: 3446163953Srrs case SCTP_OPERATION_ERROR: 3447163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3448163953Srrs case SCTP_ECN_ECHO: 3449163953Srrs case SCTP_ECN_CWR: 3450163953Srrs default: 3451163953Srrs break; 3452163953Srrs } 3453163953Srrs return (0); 3454163953Srrs} 3455163953Srrs 3456163953Srrsvoid 3457163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3458163953Srrs{ 3459163953Srrs int i; 3460163953Srrs uint16_t temp; 3461163953Srrs 3462163953Srrs /* 3463163953Srrs * We set things to 0xffff since this is the last delivered sequence 3464163953Srrs * and we will be sending in 0 after the reset. 3465163953Srrs */ 3466163953Srrs 3467163953Srrs if (number_entries) { 3468163953Srrs for (i = 0; i < number_entries; i++) { 3469163953Srrs temp = ntohs(list[i]); 3470163953Srrs if (temp >= stcb->asoc.streamincnt) { 3471163953Srrs continue; 3472163953Srrs } 3473163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3474163953Srrs } 3475163953Srrs } else { 3476163953Srrs list = NULL; 3477163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3478163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3479163953Srrs } 3480163953Srrs } 3481172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3482163953Srrs} 3483163953Srrs 3484163953Srrsstatic void 3485163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3486163953Srrs{ 3487163953Srrs int i; 3488163953Srrs 3489163953Srrs if (number_entries == 0) { 3490163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3491163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3492163953Srrs } 3493163953Srrs } else if (number_entries) { 3494163953Srrs for (i = 0; i < number_entries; i++) { 3495163953Srrs uint16_t temp; 3496163953Srrs 3497163953Srrs temp = ntohs(list[i]); 3498163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3499163953Srrs /* no such stream */ 3500163953Srrs continue; 3501163953Srrs } 3502163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3503163953Srrs } 3504163953Srrs } 3505172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3506163953Srrs} 3507163953Srrs 3508163953Srrs 3509163953Srrsstruct sctp_stream_reset_out_request * 3510163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3511163953Srrs{ 3512163953Srrs struct sctp_association *asoc; 3513163953Srrs struct sctp_stream_reset_out_req *req; 3514163953Srrs struct sctp_stream_reset_out_request *r; 3515163953Srrs struct sctp_tmit_chunk *chk; 3516163953Srrs int len, clen; 3517163953Srrs 3518163953Srrs asoc = &stcb->asoc; 3519163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3520169208Srrs asoc->stream_reset_outstanding = 0; 3521163953Srrs return (NULL); 3522163953Srrs } 3523163953Srrs if (stcb->asoc.str_reset == NULL) { 3524169208Srrs asoc->stream_reset_outstanding = 0; 3525163953Srrs return (NULL); 3526163953Srrs } 3527163953Srrs chk = stcb->asoc.str_reset; 3528163953Srrs if (chk->data == NULL) { 3529163953Srrs return (NULL); 3530163953Srrs } 3531163953Srrs if (bchk) { 3532163953Srrs /* he wants a copy of the chk pointer */ 3533163953Srrs *bchk = chk; 3534163953Srrs } 3535163953Srrs clen = chk->send_size; 3536163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3537163953Srrs r = &req->sr_req; 3538163953Srrs if (ntohl(r->request_seq) == seq) { 3539163953Srrs /* found it */ 3540163953Srrs return (r); 3541163953Srrs } 3542163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3543163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3544163953Srrs /* move to the next one, there can only be a max of two */ 3545163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3546163953Srrs if (ntohl(r->request_seq) == seq) { 3547163953Srrs return (r); 3548163953Srrs } 3549163953Srrs } 3550163953Srrs /* that seq is not here */ 3551163953Srrs return (NULL); 3552163953Srrs} 3553163953Srrs 3554163953Srrsstatic void 3555163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3556163953Srrs{ 3557163953Srrs struct sctp_association *asoc; 3558163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3559163953Srrs 3560163953Srrs if (stcb->asoc.str_reset == NULL) { 3561163953Srrs return; 3562163953Srrs } 3563166675Srrs asoc = &stcb->asoc; 3564166675Srrs 3565171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3566163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3567163953Srrs chk, 3568163953Srrs sctp_next); 3569163953Srrs if (chk->data) { 3570163953Srrs sctp_m_freem(chk->data); 3571163953Srrs chk->data = NULL; 3572163953Srrs } 3573163953Srrs asoc->ctrl_queue_cnt--; 3574221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3575172156Srrs /* sa_ignore NO_NULL_CHK */ 3576163953Srrs stcb->asoc.str_reset = NULL; 3577163953Srrs} 3578163953Srrs 3579163953Srrs 3580163953Srrsstatic int 3581163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3582163953Srrs uint32_t seq, uint32_t action, 3583164139Srrs struct sctp_stream_reset_response *respin) 3584163953Srrs{ 3585163953Srrs uint16_t type; 3586163953Srrs int lparm_len; 3587163953Srrs struct sctp_association *asoc = &stcb->asoc; 3588163953Srrs struct sctp_tmit_chunk *chk; 3589163953Srrs struct sctp_stream_reset_out_request *srparam; 3590163953Srrs int number_entries; 3591163953Srrs 3592163953Srrs if (asoc->stream_reset_outstanding == 0) { 3593163953Srrs /* duplicate */ 3594163953Srrs return (0); 3595163953Srrs } 3596163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3597163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3598163953Srrs if (srparam) { 3599163953Srrs stcb->asoc.str_reset_seq_out++; 3600163953Srrs type = ntohs(srparam->ph.param_type); 3601163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3602163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3603164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3604163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3605163953Srrs if (asoc->stream_reset_outstanding) 3606163953Srrs asoc->stream_reset_outstanding--; 3607163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3608163953Srrs /* do it */ 3609163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3610163953Srrs } else { 3611172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3612163953Srrs } 3613163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3614163953Srrs /* Answered my request */ 3615164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3616163953Srrs if (asoc->stream_reset_outstanding) 3617163953Srrs asoc->stream_reset_outstanding--; 3618163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3619172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3620163953Srrs } 3621188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3622188854Srrs /* Ok we now may have more streams */ 3623189121Srrs if (asoc->stream_reset_outstanding) 3624189121Srrs asoc->stream_reset_outstanding--; 3625188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3626188854Srrs /* Put the new streams into effect */ 3627188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3628188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3629188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3630188854Srrs } else { 3631188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3632188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3633188854Srrs } 3634163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3635163953Srrs /** 3636163953Srrs * a) Adopt the new in tsn. 3637163953Srrs * b) reset the map 3638163953Srrs * c) Adopt the new out-tsn 3639163953Srrs */ 3640163953Srrs struct sctp_stream_reset_response_tsn *resp; 3641163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3642163953Srrs int abort_flag = 0; 3643163953Srrs 3644163953Srrs if (respin == NULL) { 3645163953Srrs /* huh ? */ 3646163953Srrs return (0); 3647163953Srrs } 3648163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3649163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3650163953Srrs asoc->stream_reset_outstanding--; 3651163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3652163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3653163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3654170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3655163953Srrs if (abort_flag) { 3656163953Srrs return (1); 3657163953Srrs } 3658163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3659179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3660179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3661179783Srrs } 3662180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3663163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3664163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3665185694Srrs 3666205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3667206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3668205627Srrs 3669163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3670163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3671163953Srrs 3672163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3673163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3674163953Srrs 3675163953Srrs } 3676163953Srrs } 3677163953Srrs /* get rid of the request and get the request flags */ 3678163953Srrs if (asoc->stream_reset_outstanding == 0) { 3679163953Srrs sctp_clean_up_stream_reset(stcb); 3680163953Srrs } 3681163953Srrs } 3682163953Srrs } 3683163953Srrs return (0); 3684163953Srrs} 3685163953Srrs 3686163953Srrsstatic void 3687163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3688163953Srrs struct sctp_tmit_chunk *chk, 3689170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3690163953Srrs{ 3691163953Srrs uint32_t seq; 3692163953Srrs int len, i; 3693163953Srrs int number_entries; 3694163953Srrs uint16_t temp; 3695163953Srrs 3696163953Srrs /* 3697163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3698163953Srrs * seq_in is right. 3699163953Srrs */ 3700163953Srrs struct sctp_association *asoc = &stcb->asoc; 3701163953Srrs 3702163953Srrs seq = ntohl(req->request_seq); 3703163953Srrs if (asoc->str_reset_seq_in == seq) { 3704170992Srrs if (trunc) { 3705170992Srrs /* Can't do it, since they exceeded our buffer size */ 3706170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3707170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3708170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3709170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3710163953Srrs len = ntohs(req->ph.param_length); 3711163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3712163953Srrs for (i = 0; i < number_entries; i++) { 3713163953Srrs temp = ntohs(req->list_of_streams[i]); 3714163953Srrs req->list_of_streams[i] = temp; 3715163953Srrs } 3716163953Srrs /* move the reset action back one */ 3717163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3718163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3719163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3720163953Srrs asoc->str_reset_seq_out, 3721163953Srrs seq, (asoc->sending_seq - 1)); 3722163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3723163953Srrs asoc->str_reset = chk; 3724163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3725163953Srrs stcb->asoc.stream_reset_outstanding++; 3726163953Srrs } else { 3727163953Srrs /* Can't do it, since we have sent one out */ 3728163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3729163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3730163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3731163953Srrs } 3732163953Srrs asoc->str_reset_seq_in++; 3733163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3734163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3735163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3736163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3737163953Srrs } else { 3738163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3739163953Srrs } 3740163953Srrs} 3741163953Srrs 3742163953Srrsstatic int 3743163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3744163953Srrs struct sctp_tmit_chunk *chk, 3745163953Srrs struct sctp_stream_reset_tsn_request *req) 3746163953Srrs{ 3747163953Srrs /* reset all in and out and update the tsn */ 3748163953Srrs /* 3749163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3750163953Srrs * and set cum-ack to it. Also process this selected number as a 3751163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3752163953Srrs */ 3753163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3754163953Srrs struct sctp_association *asoc = &stcb->asoc; 3755163953Srrs int abort_flag = 0; 3756163953Srrs uint32_t seq; 3757163953Srrs 3758163953Srrs seq = ntohl(req->request_seq); 3759163953Srrs if (asoc->str_reset_seq_in == seq) { 3760163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3761163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3762163953Srrs fwdtsn.ch.chunk_flags = 0; 3763163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3764170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3765163953Srrs if (abort_flag) { 3766163953Srrs return (1); 3767163953Srrs } 3768163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3769179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3770179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3771179783Srrs } 3772180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3773163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3774163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3775205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3776206137Stuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3777163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3778163953Srrs /* save off historical data for retrans */ 3779163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3780163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3781163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3782163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3783163953Srrs 3784163953Srrs sctp_add_stream_reset_result_tsn(chk, 3785163953Srrs ntohl(req->request_seq), 3786163953Srrs SCTP_STREAM_RESET_PERFORMED, 3787163953Srrs stcb->asoc.sending_seq, 3788163953Srrs stcb->asoc.mapping_array_base_tsn); 3789163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3790163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3791163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3792163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3793163953Srrs 3794163953Srrs asoc->str_reset_seq_in++; 3795163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3796163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3797163953Srrs stcb->asoc.last_sending_seq[0], 3798163953Srrs stcb->asoc.last_base_tsnsent[0] 3799163953Srrs ); 3800163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3801163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3802163953Srrs stcb->asoc.last_sending_seq[1], 3803163953Srrs stcb->asoc.last_base_tsnsent[1] 3804163953Srrs ); 3805163953Srrs } else { 3806163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3807163953Srrs } 3808163953Srrs return (0); 3809163953Srrs} 3810163953Srrs 3811163953Srrsstatic void 3812163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3813163953Srrs struct sctp_tmit_chunk *chk, 3814170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3815163953Srrs{ 3816163953Srrs uint32_t seq, tsn; 3817163953Srrs int number_entries, len; 3818163953Srrs struct sctp_association *asoc = &stcb->asoc; 3819163953Srrs 3820163953Srrs seq = ntohl(req->request_seq); 3821163953Srrs 3822163953Srrs /* now if its not a duplicate we process it */ 3823163953Srrs if (asoc->str_reset_seq_in == seq) { 3824163953Srrs len = ntohs(req->ph.param_length); 3825163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3826163953Srrs /* 3827163953Srrs * the sender is resetting, handle the list issue.. we must 3828163953Srrs * a) verify if we can do the reset, if so no problem b) If 3829163953Srrs * we can't do the reset we must copy the request. c) queue 3830163953Srrs * it, and setup the data in processor to trigger it off 3831163953Srrs * when needed and dequeue all the queued data. 3832163953Srrs */ 3833163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3834163953Srrs 3835163953Srrs /* move the reset action back one */ 3836163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3837170992Srrs if (trunc) { 3838170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3839170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3840216825Stuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3841163953Srrs /* we can do it now */ 3842163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3843163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3844163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3845163953Srrs } else { 3846163953Srrs /* 3847163953Srrs * we must queue it up and thus wait for the TSN's 3848163953Srrs * to arrive that are at or before tsn 3849163953Srrs */ 3850163953Srrs struct sctp_stream_reset_list *liste; 3851163953Srrs int siz; 3852163953Srrs 3853163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3854163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3855170091Srrs siz, SCTP_M_STRESET); 3856163953Srrs if (liste == NULL) { 3857163953Srrs /* gak out of memory */ 3858163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3859163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3860163953Srrs return; 3861163953Srrs } 3862163953Srrs liste->tsn = tsn; 3863163953Srrs liste->number_entries = number_entries; 3864163953Srrs memcpy(&liste->req, req, 3865163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3866163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3867163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3868163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3869163953Srrs } 3870163953Srrs asoc->str_reset_seq_in++; 3871163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3872163953Srrs /* 3873163953Srrs * one seq back, just echo back last action since my 3874163953Srrs * response was lost. 3875163953Srrs */ 3876163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3877163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3878163953Srrs /* 3879163953Srrs * two seq back, just echo back last action since my 3880163953Srrs * response was lost. 3881163953Srrs */ 3882163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3883163953Srrs } else { 3884163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3885163953Srrs } 3886163953Srrs} 3887163953Srrs 3888188854Srrsstatic void 3889188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3890188854Srrs struct sctp_stream_reset_add_strm *str_add) 3891188854Srrs{ 3892188854Srrs /* 3893188854Srrs * Peer is requesting to add more streams. If its within our 3894188854Srrs * max-streams we will allow it. 3895188854Srrs */ 3896188854Srrs uint16_t num_stream, i; 3897188854Srrs uint32_t seq; 3898189121Srrs struct sctp_association *asoc = &stcb->asoc; 3899216822Stuexen struct sctp_queued_to_read *ctl, *nctl; 3900188854Srrs 3901188854Srrs /* Get the number. */ 3902188854Srrs seq = ntohl(str_add->request_seq); 3903188854Srrs num_stream = ntohs(str_add->number_of_streams); 3904188854Srrs /* Now what would be the new total? */ 3905189121Srrs if (asoc->str_reset_seq_in == seq) { 3906189121Srrs num_stream += stcb->asoc.streamincnt; 3907189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3908189121Srrs /* We must reject it they ask for to many */ 3909189121Srrs denied: 3910189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3911189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3912189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3913189121Srrs } else { 3914189121Srrs /* Ok, we can do that :-) */ 3915189121Srrs struct sctp_stream_in *oldstrm; 3916189121Srrs 3917189121Srrs /* save off the old */ 3918189121Srrs oldstrm = stcb->asoc.strmin; 3919189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3920189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3921189121Srrs SCTP_M_STRMI); 3922189121Srrs if (stcb->asoc.strmin == NULL) { 3923189121Srrs stcb->asoc.strmin = oldstrm; 3924189121Srrs goto denied; 3925189121Srrs } 3926189121Srrs /* copy off the old data */ 3927189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3928189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3929189121Srrs stcb->asoc.strmin[i].stream_no = i; 3930189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3931189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3932189121Srrs /* now anything on those queues? */ 3933216822Stuexen TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3934189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3935189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3936189121Srrs } 3937189121Srrs } 3938189121Srrs /* Init the new streams */ 3939189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3940189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3941189121Srrs stcb->asoc.strmin[i].stream_no = i; 3942189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3943189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3944189121Srrs } 3945189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3946189121Srrs /* update the size */ 3947189121Srrs stcb->asoc.streamincnt = num_stream; 3948189121Srrs /* Send the ack */ 3949189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3950189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3951189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3952189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3953189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3954189121Srrs } 3955189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3956189121Srrs /* 3957189121Srrs * one seq back, just echo back last action since my 3958189121Srrs * response was lost. 3959189121Srrs */ 3960189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3961189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3962189121Srrs /* 3963189121Srrs * two seq back, just echo back last action since my 3964189121Srrs * response was lost. 3965189121Srrs */ 3966189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3967188854Srrs } else { 3968189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3969188854Srrs 3970188854Srrs } 3971188854Srrs} 3972188854Srrs 3973170992Srrs#ifdef __GNUC__ 3974170992Srrs__attribute__((noinline)) 3975170992Srrs#endif 3976170992Srrs static int 3977170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3978170992Srrs struct sctp_stream_reset_out_req *sr_req) 3979163953Srrs{ 3980163953Srrs int chk_length, param_len, ptype; 3981170992Srrs struct sctp_paramhdr pstore; 3982170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3983170992Srrs 3984163953Srrs uint32_t seq; 3985163953Srrs int num_req = 0; 3986170992Srrs int trunc = 0; 3987163953Srrs struct sctp_tmit_chunk *chk; 3988163953Srrs struct sctp_chunkhdr *ch; 3989163953Srrs struct sctp_paramhdr *ph; 3990166675Srrs int ret_code = 0; 3991166675Srrs int num_param = 0; 3992163953Srrs 3993163953Srrs /* now it may be a reset or a reset-response */ 3994163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3995163953Srrs 3996163953Srrs /* setup for adding the response */ 3997163953Srrs sctp_alloc_a_chunk(stcb, chk); 3998163953Srrs if (chk == NULL) { 3999163953Srrs return (ret_code); 4000163953Srrs } 4001163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 4002169208Srrs chk->rec.chunk_id.can_take_data = 0; 4003163953Srrs chk->asoc = &stcb->asoc; 4004163953Srrs chk->no_fr_allowed = 0; 4005163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4006166023Srrs chk->book_size_scale = 0; 4007165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 4008163953Srrs if (chk->data == NULL) { 4009163953Srrsstrres_nochunk: 4010163953Srrs if (chk->data) { 4011163953Srrs sctp_m_freem(chk->data); 4012163953Srrs chk->data = NULL; 4013163953Srrs } 4014221627Stuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4015163953Srrs return (ret_code); 4016163953Srrs } 4017165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4018163953Srrs 4019163953Srrs /* setup chunk parameters */ 4020163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 4021163953Srrs chk->snd_count = 0; 4022224641Stuexen chk->whoTo = NULL; 4023163953Srrs 4024163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 4025163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 4026163953Srrs ch->chunk_flags = 0; 4027163953Srrs ch->chunk_length = htons(chk->send_size); 4028165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4029170992Srrs offset += sizeof(struct sctp_chunkhdr); 4030163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4031170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4032170992Srrs if (ph == NULL) 4033170992Srrs break; 4034163953Srrs param_len = ntohs(ph->param_length); 4035163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4036163953Srrs /* bad param */ 4037163953Srrs break; 4038163953Srrs } 4039170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4040170992Srrs (uint8_t *) & cstore); 4041163953Srrs ptype = ntohs(ph->param_type); 4042163953Srrs num_param++; 4043170994Srrs if (param_len > (int)sizeof(cstore)) { 4044170992Srrs trunc = 1; 4045170992Srrs } else { 4046170992Srrs trunc = 0; 4047170992Srrs } 4048170992Srrs 4049163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 4050163953Srrs /* hit the max of parameters already sorry.. */ 4051163953Srrs break; 4052163953Srrs } 4053163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4054163953Srrs struct sctp_stream_reset_out_request *req_out; 4055163953Srrs 4056163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 4057163953Srrs num_req++; 4058163953Srrs if (stcb->asoc.stream_reset_outstanding) { 4059163953Srrs seq = ntohl(req_out->response_seq); 4060163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 4061163953Srrs /* implicit ack */ 4062169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 4063163953Srrs } 4064163953Srrs } 4065170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4066188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 4067188854Srrs struct sctp_stream_reset_add_strm *str_add; 4068188854Srrs 4069188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 4070188854Srrs num_req++; 4071188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4072163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4073163953Srrs struct sctp_stream_reset_in_request *req_in; 4074163953Srrs 4075163953Srrs num_req++; 4076170992Srrs 4077163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 4078170992Srrs 4079170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4080163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4081163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 4082163953Srrs 4083163953Srrs num_req++; 4084163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4085170992Srrs 4086163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4087163953Srrs ret_code = 1; 4088163953Srrs goto strres_nochunk; 4089163953Srrs } 4090163953Srrs /* no more */ 4091163953Srrs break; 4092163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4093163953Srrs struct sctp_stream_reset_response *resp; 4094163953Srrs uint32_t result; 4095163953Srrs 4096163953Srrs resp = (struct sctp_stream_reset_response *)ph; 4097163953Srrs seq = ntohl(resp->response_seq); 4098163953Srrs result = ntohl(resp->result); 4099163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4100163953Srrs ret_code = 1; 4101163953Srrs goto strres_nochunk; 4102163953Srrs } 4103163953Srrs } else { 4104163953Srrs break; 4105163953Srrs } 4106170992Srrs offset += SCTP_SIZE32(param_len); 4107163953Srrs chk_length -= SCTP_SIZE32(param_len); 4108163953Srrs } 4109163953Srrs if (num_req == 0) { 4110163953Srrs /* we have no response free the stuff */ 4111163953Srrs goto strres_nochunk; 4112163953Srrs } 4113163953Srrs /* ok we have a chunk to link in */ 4114163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4115163953Srrs chk, 4116163953Srrs sctp_next); 4117163953Srrs stcb->asoc.ctrl_queue_cnt++; 4118163953Srrs return (ret_code); 4119163953Srrs} 4120163953Srrs 4121163953Srrs/* 4122163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 4123163953Srrs * to handle this, either we get the whole packet and must disect it 4124163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 4125163953Srrs * from a middle box that did the disectting for us. 4126163953Srrs */ 4127163953Srrsstatic void 4128163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4129170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4130163953Srrs{ 4131163953Srrs uint32_t bottle_bw, on_queue; 4132163953Srrs uint16_t trunc_len; 4133163953Srrs unsigned int chlen; 4134163953Srrs unsigned int at; 4135163953Srrs struct sctp_chunk_desc desc; 4136163953Srrs struct sctp_chunkhdr *ch; 4137163953Srrs 4138163953Srrs chlen = ntohs(cp->ch.chunk_length); 4139163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4140163953Srrs /* XXX possible chlen underflow */ 4141163953Srrs if (chlen == 0) { 4142163953Srrs ch = NULL; 4143163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4144163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4145163953Srrs } else { 4146163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4147163953Srrs chlen -= sizeof(struct sctphdr); 4148163953Srrs /* XXX possible chlen underflow */ 4149163953Srrs memset(&desc, 0, sizeof(desc)); 4150163953Srrs } 4151163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4152170781Srrs if (trunc_len > limit) { 4153170781Srrs trunc_len = limit; 4154170781Srrs } 4155163953Srrs /* now the chunks themselves */ 4156163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4157163953Srrs desc.chunk_type = ch->chunk_type; 4158163953Srrs /* get amount we need to move */ 4159163953Srrs at = ntohs(ch->chunk_length); 4160163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4161163953Srrs /* corrupt chunk, maybe at the end? */ 4162163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4163163953Srrs break; 4164163953Srrs } 4165163953Srrs if (trunc_len == 0) { 4166163953Srrs /* we are supposed to have all of it */ 4167163953Srrs if (at > chlen) { 4168163953Srrs /* corrupt skip it */ 4169163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4170163953Srrs break; 4171163953Srrs } 4172163953Srrs } else { 4173163953Srrs /* is there enough of it left ? */ 4174163953Srrs if (desc.chunk_type == SCTP_DATA) { 4175163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4176163953Srrs sizeof(desc.data_bytes))) { 4177163953Srrs break; 4178163953Srrs } 4179163953Srrs } else { 4180163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4181163953Srrs break; 4182163953Srrs } 4183163953Srrs } 4184163953Srrs } 4185163953Srrs if (desc.chunk_type == SCTP_DATA) { 4186163953Srrs /* can we get out the tsn? */ 4187163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4188163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4189163953Srrs 4190163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4191163953Srrs /* yep */ 4192163953Srrs struct sctp_data_chunk *dcp; 4193163953Srrs uint8_t *ddp; 4194163953Srrs unsigned int iii; 4195163953Srrs 4196163953Srrs dcp = (struct sctp_data_chunk *)ch; 4197163953Srrs ddp = (uint8_t *) (dcp + 1); 4198163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4199163953Srrs desc.data_bytes[iii] = ddp[iii]; 4200163953Srrs } 4201163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4202163953Srrs } else { 4203163953Srrs /* nope we are done. */ 4204163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4205163953Srrs break; 4206163953Srrs } 4207163953Srrs } else { 4208163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4209163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4210163953Srrs } 4211163953Srrs 4212163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4213163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4214163953Srrs break; 4215163953Srrs } 4216163953Srrs if (SCTP_SIZE32(at) > chlen) { 4217163953Srrs break; 4218163953Srrs } 4219163953Srrs chlen -= SCTP_SIZE32(at); 4220163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4221163953Srrs /* done, none left */ 4222163953Srrs break; 4223163953Srrs } 4224163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4225163953Srrs } 4226163953Srrs /* Now update any rwnd --- possibly */ 4227163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4228163953Srrs /* From a peer, we get a rwnd report */ 4229163953Srrs uint32_t a_rwnd; 4230163953Srrs 4231163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4232163953Srrs 4233163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4234163953Srrs on_queue = ntohl(cp->current_onq); 4235163953Srrs if (bottle_bw && on_queue) { 4236163953Srrs /* a rwnd report is in here */ 4237163953Srrs if (bottle_bw > on_queue) 4238163953Srrs a_rwnd = bottle_bw - on_queue; 4239163953Srrs else 4240163953Srrs a_rwnd = 0; 4241163953Srrs 4242163953Srrs if (a_rwnd == 0) 4243163953Srrs stcb->asoc.peers_rwnd = 0; 4244163953Srrs else { 4245163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4246163953Srrs stcb->asoc.peers_rwnd = 4247163953Srrs a_rwnd - stcb->asoc.total_flight; 4248163953Srrs } else { 4249163953Srrs stcb->asoc.peers_rwnd = 0; 4250163953Srrs } 4251163953Srrs if (stcb->asoc.peers_rwnd < 4252163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4253163953Srrs /* SWS sender side engages */ 4254163953Srrs stcb->asoc.peers_rwnd = 0; 4255163953Srrs } 4256163953Srrs } 4257163953Srrs } 4258163953Srrs } else { 4259163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4260163953Srrs } 4261163953Srrs 4262163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4263163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4264163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4265163953Srrs (stcb->asoc.sat_network)) { 4266163953Srrs /* 4267163953Srrs * This is debateable but for sat networks it makes sense 4268163953Srrs * Note if a T3 timer has went off, we will prohibit any 4269163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4270163953Srrs */ 4271171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4272171440Srrs net, cp, &bottle_bw, &on_queue); 4273163953Srrs } 4274163953Srrs} 4275163953Srrs 4276163953Srrs/* 4277163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4278163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4279163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4280163953Srrs * length of the complete packet outputs: - length: modified to remaining 4281163953Srrs * length after control processing - netp: modified to new sctp_nets after 4282163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4283163953Srrs * bad packet,...) otherwise return the tcb for this packet 4284163953Srrs */ 4285170099Srrs#ifdef __GNUC__ 4286170099Srrs__attribute__((noinline)) 4287170099Srrs#endif 4288170099Srrs static struct sctp_tcb * 4289170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4290170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4291170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4292179157Srrs uint32_t vrf_id, uint16_t port) 4293163953Srrs{ 4294163953Srrs struct sctp_association *asoc; 4295163953Srrs uint32_t vtag_in; 4296163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4297170140Srrs uint32_t chk_length; 4298163953Srrs int ret; 4299168299Srrs int abort_no_unlock = 0; 4300218186Srrs int ecne_seen = 0; 4301163953Srrs 4302163953Srrs /* 4303163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4304163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4305163953Srrs * until we get into jumbo grams and such.. 4306163953Srrs */ 4307166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4308163953Srrs struct sctp_tcb *locked_tcb = stcb; 4309163953Srrs int got_auth = 0; 4310163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4311163953Srrs int auth_skipped = 0; 4312171990Srrs int asconf_cnt = 0; 4313163953Srrs 4314172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4315172090Srrs struct socket *so; 4316172090Srrs 4317172090Srrs#endif 4318172090Srrs 4319169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4320169420Srrs iphlen, *offset, length, stcb); 4321163953Srrs 4322163953Srrs /* validate chunk header length... */ 4323163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4324170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4325170056Srrs ntohs(ch->chunk_length)); 4326170091Srrs if (locked_tcb) { 4327170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4328170091Srrs } 4329163953Srrs return (NULL); 4330163953Srrs } 4331163953Srrs /* 4332163953Srrs * validate the verification tag 4333163953Srrs */ 4334163953Srrs vtag_in = ntohl(sh->v_tag); 4335163953Srrs 4336165220Srrs if (locked_tcb) { 4337165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4338165220Srrs } 4339163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4340170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4341170056Srrs ntohs(ch->chunk_length), vtag_in); 4342163953Srrs if (vtag_in != 0) { 4343163953Srrs /* protocol error- silently discard... */ 4344163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4345169378Srrs if (locked_tcb) { 4346163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4347169378Srrs } 4348163953Srrs return (NULL); 4349163953Srrs } 4350163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4351163953Srrs /* 4352163953Srrs * If there is no stcb, skip the AUTH chunk and process 4353163953Srrs * later after a stcb is found (to validate the lookup was 4354163953Srrs * valid. 4355163953Srrs */ 4356163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4357179783Srrs (stcb == NULL) && 4358179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4359163953Srrs /* save this chunk for later processing */ 4360163953Srrs auth_skipped = 1; 4361163953Srrs auth_offset = *offset; 4362163953Srrs auth_len = ntohs(ch->chunk_length); 4363163953Srrs 4364163953Srrs /* (temporarily) move past this chunk */ 4365163953Srrs *offset += SCTP_SIZE32(auth_len); 4366163953Srrs if (*offset >= length) { 4367163953Srrs /* no more data left in the mbuf chain */ 4368163953Srrs *offset = length; 4369170091Srrs if (locked_tcb) { 4370170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4371170091Srrs } 4372163953Srrs return (NULL); 4373163953Srrs } 4374163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4375163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4376163953Srrs } 4377169420Srrs if (ch == NULL) { 4378169420Srrs /* Help */ 4379169420Srrs *offset = length; 4380170091Srrs if (locked_tcb) { 4381170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4382170091Srrs } 4383169420Srrs return (NULL); 4384169420Srrs } 4385163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4386163953Srrs goto process_control_chunks; 4387163953Srrs } 4388163953Srrs /* 4389163953Srrs * first check if it's an ASCONF with an unknown src addr we 4390163953Srrs * need to look inside to find the association 4391163953Srrs */ 4392163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4393171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4394171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4395171990Srrs 4396163953Srrs /* inp's refcount may be reduced */ 4397163953Srrs SCTP_INP_INCR_REF(inp); 4398163953Srrs 4399171990Srrs asconf_offset = *offset; 4400171990Srrs do { 4401171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4402171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4403171990Srrs break; 4404171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4405185694Srrs *offset, sh, &inp, netp, vrf_id); 4406171990Srrs if (stcb != NULL) 4407171990Srrs break; 4408171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4409171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4410171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4411171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4412163953Srrs if (stcb == NULL) { 4413163953Srrs /* 4414163953Srrs * reduce inp's refcount if not reduced in 4415163953Srrs * sctp_findassociation_ep_asconf(). 4416163953Srrs */ 4417163953Srrs SCTP_INP_DECR_REF(inp); 4418171990Srrs } else { 4419171990Srrs locked_tcb = stcb; 4420163953Srrs } 4421171990Srrs 4422163953Srrs /* now go back and verify any auth chunk to be sure */ 4423163953Srrs if (auth_skipped && (stcb != NULL)) { 4424163953Srrs struct sctp_auth_chunk *auth; 4425163953Srrs 4426163953Srrs auth = (struct sctp_auth_chunk *) 4427163953Srrs sctp_m_getptr(m, auth_offset, 4428163953Srrs auth_len, chunk_buf); 4429163953Srrs got_auth = 1; 4430163953Srrs auth_skipped = 0; 4431169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4432163953Srrs auth_offset)) { 4433163953Srrs /* auth HMAC failed so dump it */ 4434163953Srrs *offset = length; 4435170091Srrs if (locked_tcb) { 4436170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4437170091Srrs } 4438163953Srrs return (NULL); 4439163953Srrs } else { 4440163953Srrs /* remaining chunks are HMAC checked */ 4441163953Srrs stcb->asoc.authenticated = 1; 4442163953Srrs } 4443163953Srrs } 4444163953Srrs } 4445163953Srrs if (stcb == NULL) { 4446163953Srrs /* no association, so it's out of the blue... */ 4447169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4448179157Srrs vrf_id, port); 4449163953Srrs *offset = length; 4450169378Srrs if (locked_tcb) { 4451163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4452169378Srrs } 4453163953Srrs return (NULL); 4454163953Srrs } 4455163953Srrs asoc = &stcb->asoc; 4456163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4457163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4458163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4459163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4460163953Srrs if ((vtag_in == asoc->my_vtag) || 4461163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4462163953Srrs (vtag_in == asoc->peer_vtag))) { 4463163953Srrs /* this is valid */ 4464163953Srrs } else { 4465163953Srrs /* drop this packet... */ 4466163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4467169378Srrs if (locked_tcb) { 4468163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4469169378Srrs } 4470163953Srrs return (NULL); 4471163953Srrs } 4472163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4473163953Srrs if (vtag_in != asoc->my_vtag) { 4474163953Srrs /* 4475163953Srrs * this could be a stale SHUTDOWN-ACK or the 4476163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4477163953Srrs * is still hung; we have started a new asoc 4478163953Srrs * but it won't complete until the shutdown 4479163953Srrs * is completed 4480163953Srrs */ 4481169378Srrs if (locked_tcb) { 4482163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4483169378Srrs } 4484163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4485179157Srrs NULL, vrf_id, port); 4486163953Srrs return (NULL); 4487163953Srrs } 4488163953Srrs } else { 4489163953Srrs /* for all other chunks, vtag must match */ 4490163953Srrs if (vtag_in != asoc->my_vtag) { 4491163953Srrs /* invalid vtag... */ 4492169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4493169420Srrs "invalid vtag: %xh, expect %xh\n", 4494169420Srrs vtag_in, asoc->my_vtag); 4495163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4496169378Srrs if (locked_tcb) { 4497163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4498169378Srrs } 4499163953Srrs *offset = length; 4500163953Srrs return (NULL); 4501163953Srrs } 4502163953Srrs } 4503163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4504163953Srrs /* 4505163953Srrs * process all control chunks... 4506163953Srrs */ 4507163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4508185694Srrs /* EY */ 4509185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4510163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4511163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4512163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4513179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4514171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4515171943Srrs stcb->asoc.overall_error_count, 4516171943Srrs 0, 4517171943Srrs SCTP_FROM_SCTP_INPUT, 4518171943Srrs __LINE__); 4519171943Srrs } 4520163953Srrs stcb->asoc.overall_error_count = 0; 4521163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4522163953Srrs *netp); 4523163953Srrs } 4524163953Srrsprocess_control_chunks: 4525163953Srrs while (IS_SCTP_CONTROL(ch)) { 4526163953Srrs /* validate chunk length */ 4527163953Srrs chk_length = ntohs(ch->chunk_length); 4528169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4529169420Srrs ch->chunk_type, chk_length); 4530170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4531170140Srrs if (chk_length < sizeof(*ch) || 4532170140Srrs (*offset + (int)chk_length) > length) { 4533163953Srrs *offset = length; 4534169378Srrs if (locked_tcb) { 4535163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4536169378Srrs } 4537163953Srrs return (NULL); 4538163953Srrs } 4539163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4540163953Srrs /* 4541163953Srrs * INIT-ACK only gets the init ack "header" portion only 4542163953Srrs * because we don't have to process the peer's COOKIE. All 4543163953Srrs * others get a complete chunk. 4544163953Srrs */ 4545169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4546169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4547163953Srrs /* get an init-ack chunk */ 4548163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4549163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4550163953Srrs if (ch == NULL) { 4551163953Srrs *offset = length; 4552169378Srrs if (locked_tcb) { 4553163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4554169378Srrs } 4555163953Srrs return (NULL); 4556163953Srrs } 4557170642Srrs } else { 4558170786Srrs /* For cookies and all other chunks. */ 4559169208Srrs if (chk_length > sizeof(chunk_buf)) { 4560169208Srrs /* 4561169208Srrs * use just the size of the chunk buffer so 4562170642Srrs * the front part of our chunks fit in 4563170642Srrs * contiguous space up to the chunk buffer 4564170642Srrs * size (508 bytes). For chunks that need to 4565170786Srrs * get more than that they must use the 4566170642Srrs * sctp_m_getptr() function or other means 4567170786Srrs * (e.g. know how to parse mbuf chains). 4568170786Srrs * Cookies do this already. 4569169208Srrs */ 4570169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4571169208Srrs (sizeof(chunk_buf) - 4), 4572169208Srrs chunk_buf); 4573169208Srrs if (ch == NULL) { 4574169208Srrs *offset = length; 4575169378Srrs if (locked_tcb) { 4576169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4577169378Srrs } 4578169208Srrs return (NULL); 4579169208Srrs } 4580169208Srrs } else { 4581169208Srrs /* We can fit it all */ 4582170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4583170642Srrs chk_length, chunk_buf); 4584170642Srrs if (ch == NULL) { 4585170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4586170642Srrs *offset = length; 4587170642Srrs if (locked_tcb) { 4588170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4589169208Srrs } 4590170642Srrs return (NULL); 4591163953Srrs } 4592163953Srrs } 4593163953Srrs } 4594163953Srrs num_chunks++; 4595163953Srrs /* Save off the last place we got a control from */ 4596163953Srrs if (stcb != NULL) { 4597169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4598163953Srrs /* 4599163953Srrs * allow last_control to be NULL if 4600163953Srrs * ASCONF... ASCONF processing will find the 4601163953Srrs * right net later 4602163953Srrs */ 4603169420Srrs if ((netp != NULL) && (*netp != NULL)) 4604169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4605163953Srrs } 4606163953Srrs } 4607163953Srrs#ifdef SCTP_AUDITING_ENABLED 4608163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4609163953Srrs#endif 4610163953Srrs 4611163953Srrs /* check to see if this chunk required auth, but isn't */ 4612179783Srrs if ((stcb != NULL) && 4613179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4614179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4615163953Srrs !stcb->asoc.authenticated) { 4616163953Srrs /* "silently" ignore */ 4617163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4618163953Srrs goto next_chunk; 4619163953Srrs } 4620163953Srrs switch (ch->chunk_type) { 4621163953Srrs case SCTP_INITIATION: 4622163953Srrs /* must be first and only chunk */ 4623169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4624163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4625163953Srrs /* We are not interested anymore? */ 4626163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4627163953Srrs /* 4628163953Srrs * collision case where we are 4629163953Srrs * sending to them too 4630163953Srrs */ 4631163953Srrs ; 4632163953Srrs } else { 4633169378Srrs if (locked_tcb) { 4634163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4635169378Srrs } 4636163953Srrs *offset = length; 4637163953Srrs return (NULL); 4638163953Srrs } 4639163953Srrs } 4640171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4641171990Srrs (num_chunks > 1) || 4642179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4643163953Srrs *offset = length; 4644169378Srrs if (locked_tcb) { 4645163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4646169378Srrs } 4647163953Srrs return (NULL); 4648163953Srrs } 4649163953Srrs if ((stcb != NULL) && 4650163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4651163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4652224641Stuexen sctp_send_shutdown_ack(stcb, NULL); 4653163953Srrs *offset = length; 4654172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4655169378Srrs if (locked_tcb) { 4656163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4657169378Srrs } 4658163953Srrs return (NULL); 4659163953Srrs } 4660169420Srrs if (netp) { 4661169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4662169420Srrs (struct sctp_init_chunk *)ch, inp, 4663179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4664169420Srrs } 4665168299Srrs if (abort_no_unlock) 4666168299Srrs return (NULL); 4667168299Srrs 4668163953Srrs *offset = length; 4669169378Srrs if (locked_tcb) { 4670163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4671169378Srrs } 4672163953Srrs return (NULL); 4673163953Srrs break; 4674170642Srrs case SCTP_PAD_CHUNK: 4675170642Srrs break; 4676163953Srrs case SCTP_INITIATION_ACK: 4677163953Srrs /* must be first and only chunk */ 4678169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4679163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4680163953Srrs /* We are not interested anymore */ 4681163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4682163953Srrs ; 4683163953Srrs } else { 4684208876Srrs if (locked_tcb != stcb) { 4685208876Srrs /* Very unlikely */ 4686163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4687169378Srrs } 4688163953Srrs *offset = length; 4689163953Srrs if (stcb) { 4690172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4691172090Srrs so = SCTP_INP_SO(inp); 4692172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4693172090Srrs SCTP_TCB_UNLOCK(stcb); 4694172090Srrs SCTP_SOCKET_LOCK(so, 1); 4695172090Srrs SCTP_TCB_LOCK(stcb); 4696172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4697172090Srrs#endif 4698171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4699172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4700172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4701172090Srrs#endif 4702163953Srrs } 4703163953Srrs return (NULL); 4704163953Srrs } 4705163953Srrs } 4706163953Srrs if ((num_chunks > 1) || 4707179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4708163953Srrs *offset = length; 4709169378Srrs if (locked_tcb) { 4710163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4711169378Srrs } 4712163953Srrs return (NULL); 4713163953Srrs } 4714169420Srrs if ((netp) && (*netp)) { 4715169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4716170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4717169420Srrs } else { 4718169420Srrs ret = -1; 4719169420Srrs } 4720163953Srrs /* 4721163953Srrs * Special case, I must call the output routine to 4722163953Srrs * get the cookie echoed 4723163953Srrs */ 4724168299Srrs if (abort_no_unlock) 4725168299Srrs return (NULL); 4726168299Srrs 4727163953Srrs if ((stcb) && ret == 0) 4728172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4729163953Srrs *offset = length; 4730169378Srrs if (locked_tcb) { 4731163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4732169378Srrs } 4733163953Srrs return (NULL); 4734163953Srrs break; 4735163953Srrs case SCTP_SELECTIVE_ACK: 4736163953Srrs { 4737163953Srrs struct sctp_sack_chunk *sack; 4738163953Srrs int abort_now = 0; 4739163953Srrs uint32_t a_rwnd, cum_ack; 4740202526Stuexen uint16_t num_seg, num_dup; 4741202526Stuexen uint8_t flags; 4742202526Stuexen int offset_seg, offset_dup; 4743163953Srrs 4744211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4745211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4746202526Stuexen if (stcb == NULL) { 4747202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4748202526Stuexen break; 4749169208Srrs } 4750202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4751202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4752202526Stuexen break; 4753202526Stuexen } 4754171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4755171990Srrs /*- 4756171990Srrs * If we have sent a shutdown-ack, we will pay no 4757171990Srrs * attention to a sack sent in to us since 4758171990Srrs * we don't care anymore. 4759171990Srrs */ 4760184883Srrs break; 4761171990Srrs } 4762163953Srrs sack = (struct sctp_sack_chunk *)ch; 4763202526Stuexen flags = ch->chunk_flags; 4764163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4765163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4766202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4767163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4768202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4769202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4770202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4771202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4772202526Stuexen break; 4773202526Stuexen } 4774202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4775202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4776170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4777202526Stuexen cum_ack, num_seg, a_rwnd); 4778163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4779163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4780163953Srrs (num_seg == 0) && 4781216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4782163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4783216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4784163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4785163953Srrs ) { 4786163953Srrs /* 4787163953Srrs * We have a SIMPLE sack having no 4788163953Srrs * prior segments and data on sent 4789163953Srrs * queue to be acked.. Use the 4790163953Srrs * faster path sack processing. We 4791163953Srrs * also allow window update sacks 4792163953Srrs * with no missing segments to go 4793163953Srrs * this way too. 4794163953Srrs */ 4795218186Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4796163953Srrs } else { 4797169420Srrs if (netp && *netp) 4798202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4799202526Stuexen stcb, *netp, 4800202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4801218186Srrs cum_ack, a_rwnd, ecne_seen); 4802163953Srrs } 4803202526Stuexen if (abort_now) { 4804202526Stuexen /* ABORT signal from sack processing */ 4805202526Stuexen *offset = length; 4806202526Stuexen return (NULL); 4807202526Stuexen } 4808185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4809185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4810185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4811185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4812185694Srrs } 4813163953Srrs } 4814163953Srrs break; 4815185694Srrs /* 4816185694Srrs * EY - nr_sack: If the received chunk is an 4817185694Srrs * nr_sack chunk 4818185694Srrs */ 4819185694Srrs case SCTP_NR_SELECTIVE_ACK: 4820185694Srrs { 4821185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4822185694Srrs int abort_now = 0; 4823185694Srrs uint32_t a_rwnd, cum_ack; 4824202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4825202526Stuexen uint8_t flags; 4826202526Stuexen int offset_seg, offset_dup; 4827185694Srrs 4828211944Stuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4829211944Stuexen SCTP_STAT_INCR(sctps_recvsacks); 4830212225Srrs if (stcb == NULL) { 4831212225Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4832212225Srrs break; 4833212225Srrs } 4834211944Stuexen if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4835211944Stuexen (stcb->asoc.peer_supports_nr_sack == 0)) { 4836185694Srrs goto unknown_chunk; 4837185694Srrs } 4838202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4839202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4840202526Stuexen break; 4841202526Stuexen } 4842185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4843185694Srrs /*- 4844185694Srrs * If we have sent a shutdown-ack, we will pay no 4845185694Srrs * attention to a sack sent in to us since 4846185694Srrs * we don't care anymore. 4847185694Srrs */ 4848202526Stuexen break; 4849185694Srrs } 4850185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4851202526Stuexen flags = ch->chunk_flags; 4852185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4853185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4854185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4855202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4856185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4857202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4858202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4859202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4860202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4861202526Stuexen break; 4862202526Stuexen } 4863202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4864202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4865185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4866202526Stuexen cum_ack, num_seg, a_rwnd); 4867185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4868185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4869202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4870216825Stuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4871185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4872216188Stuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4873202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4874185694Srrs /* 4875185694Srrs * We have a SIMPLE sack having no 4876185694Srrs * prior segments and data on sent 4877202526Stuexen * queue to be acked. Use the faster 4878202526Stuexen * path sack processing. We also 4879202526Stuexen * allow window update sacks with no 4880202526Stuexen * missing segments to go this way 4881202526Stuexen * too. 4882185694Srrs */ 4883218129Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4884218186Srrs &abort_now, ecne_seen); 4885185694Srrs } else { 4886185694Srrs if (netp && *netp) 4887202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4888202526Stuexen stcb, *netp, 4889202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4890218186Srrs cum_ack, a_rwnd, ecne_seen); 4891185694Srrs } 4892202526Stuexen if (abort_now) { 4893202526Stuexen /* ABORT signal from sack processing */ 4894202526Stuexen *offset = length; 4895202526Stuexen return (NULL); 4896202526Stuexen } 4897185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4898185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4899185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4900185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4901185694Srrs } 4902185694Srrs } 4903185694Srrs break; 4904185694Srrs 4905163953Srrs case SCTP_HEARTBEAT_REQUEST: 4906169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4907169420Srrs if ((stcb) && netp && *netp) { 4908169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4909169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4910169420Srrs chk_length, *netp); 4911169420Srrs 4912169420Srrs /* He's alive so give him credit */ 4913179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4914171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4915171943Srrs stcb->asoc.overall_error_count, 4916171943Srrs 0, 4917171943Srrs SCTP_FROM_SCTP_INPUT, 4918171943Srrs __LINE__); 4919171943Srrs } 4920169420Srrs stcb->asoc.overall_error_count = 0; 4921163953Srrs } 4922163953Srrs break; 4923163953Srrs case SCTP_HEARTBEAT_ACK: 4924169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4925169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4926169208Srrs /* Its not ours */ 4927169352Srrs *offset = length; 4928169378Srrs if (locked_tcb) { 4929169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4930169378Srrs } 4931169208Srrs return (NULL); 4932169208Srrs } 4933163953Srrs /* He's alive so give him credit */ 4934179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4935171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4936171943Srrs stcb->asoc.overall_error_count, 4937171943Srrs 0, 4938171943Srrs SCTP_FROM_SCTP_INPUT, 4939171943Srrs __LINE__); 4940171943Srrs } 4941163953Srrs stcb->asoc.overall_error_count = 0; 4942163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4943169420Srrs if (netp && *netp) 4944169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4945169420Srrs stcb, *netp); 4946163953Srrs break; 4947163953Srrs case SCTP_ABORT_ASSOCIATION: 4948170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4949170091Srrs stcb); 4950169420Srrs if ((stcb) && netp && *netp) 4951169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4952169420Srrs stcb, *netp); 4953163953Srrs *offset = length; 4954163953Srrs return (NULL); 4955163953Srrs break; 4956163953Srrs case SCTP_SHUTDOWN: 4957170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4958170091Srrs stcb); 4959169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4960169208Srrs *offset = length; 4961169378Srrs if (locked_tcb) { 4962169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4963169378Srrs } 4964169208Srrs return (NULL); 4965169420Srrs } 4966169420Srrs if (netp && *netp) { 4967163953Srrs int abort_flag = 0; 4968163953Srrs 4969163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4970163953Srrs stcb, *netp, &abort_flag); 4971163953Srrs if (abort_flag) { 4972163953Srrs *offset = length; 4973163953Srrs return (NULL); 4974163953Srrs } 4975163953Srrs } 4976163953Srrs break; 4977163953Srrs case SCTP_SHUTDOWN_ACK: 4978170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4979169420Srrs if ((stcb) && (netp) && (*netp)) 4980169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4981163953Srrs *offset = length; 4982163953Srrs return (NULL); 4983163953Srrs break; 4984169420Srrs 4985163953Srrs case SCTP_OPERATION_ERROR: 4986169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4987169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4988169420Srrs 4989163953Srrs *offset = length; 4990163953Srrs return (NULL); 4991163953Srrs } 4992163953Srrs break; 4993163953Srrs case SCTP_COOKIE_ECHO: 4994169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4995170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4996163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4997163953Srrs ; 4998163953Srrs } else { 4999169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5000163953Srrs /* We are not interested anymore */ 5001208878Srrs abend: 5002208853Srrs if (stcb) { 5003208853Srrs SCTP_TCB_UNLOCK(stcb); 5004208853Srrs } 5005163953Srrs *offset = length; 5006163953Srrs return (NULL); 5007163953Srrs } 5008163953Srrs } 5009163953Srrs /* 5010163953Srrs * First are we accepting? We do this again here 5011207924Srrs * since it is possible that a previous endpoint WAS 5012207924Srrs * listening responded to a INIT-ACK and then 5013163953Srrs * closed. We opened and bound.. and are now no 5014163953Srrs * longer listening. 5015163953Srrs */ 5016172703Srrs 5017172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 5018172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5019179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 5020163953Srrs struct mbuf *oper; 5021163953Srrs struct sctp_paramhdr *phdr; 5022163953Srrs 5023172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5024172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 5025172703Srrs if (oper) { 5026172703Srrs SCTP_BUF_LEN(oper) = 5027172703Srrs sizeof(struct sctp_paramhdr); 5028172703Srrs phdr = mtod(oper, 5029172703Srrs struct sctp_paramhdr *); 5030172703Srrs phdr->param_type = 5031172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 5032172703Srrs phdr->param_length = 5033172703Srrs htons(sizeof(struct sctp_paramhdr)); 5034163953Srrs } 5035172703Srrs sctp_abort_association(inp, stcb, m, 5036179157Srrs iphlen, sh, oper, vrf_id, port); 5037163953Srrs } 5038172703Srrs *offset = length; 5039172703Srrs return (NULL); 5040172703Srrs } else { 5041163953Srrs struct mbuf *ret_buf; 5042165220Srrs struct sctp_inpcb *linp; 5043163953Srrs 5044169420Srrs if (stcb) { 5045165220Srrs linp = NULL; 5046169420Srrs } else { 5047165220Srrs linp = inp; 5048169420Srrs } 5049165220Srrs 5050169420Srrs if (linp) { 5051165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 5052208878Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5053208878Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5054208878Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5055208878Srrs goto abend; 5056208878Srrs } 5057169420Srrs } 5058169420Srrs if (netp) { 5059169420Srrs ret_buf = 5060169420Srrs sctp_handle_cookie_echo(m, iphlen, 5061169420Srrs *offset, sh, 5062169420Srrs (struct sctp_cookie_echo_chunk *)ch, 5063169420Srrs &inp, &stcb, netp, 5064169420Srrs auth_skipped, 5065169420Srrs auth_offset, 5066169420Srrs auth_len, 5067169420Srrs &locked_tcb, 5068179157Srrs vrf_id, 5069179157Srrs port); 5070169420Srrs } else { 5071169420Srrs ret_buf = NULL; 5072169420Srrs } 5073169420Srrs if (linp) { 5074165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 5075169420Srrs } 5076163953Srrs if (ret_buf == NULL) { 5077163953Srrs if (locked_tcb) { 5078163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5079163953Srrs } 5080169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 5081169420Srrs "GAK, null buffer\n"); 5082163953Srrs auth_skipped = 0; 5083163953Srrs *offset = length; 5084163953Srrs return (NULL); 5085163953Srrs } 5086163953Srrs /* if AUTH skipped, see if it verified... */ 5087163953Srrs if (auth_skipped) { 5088163953Srrs got_auth = 1; 5089163953Srrs auth_skipped = 0; 5090163953Srrs } 5091163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5092163953Srrs /* 5093163953Srrs * Restart the timer if we have 5094163953Srrs * pending data 5095163953Srrs */ 5096163953Srrs struct sctp_tmit_chunk *chk; 5097163953Srrs 5098163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5099216822Stuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5100163953Srrs } 5101163953Srrs } 5102163953Srrs break; 5103163953Srrs case SCTP_COOKIE_ACK: 5104170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5105169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5106169378Srrs if (locked_tcb) { 5107169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 5108169378Srrs } 5109169352Srrs return (NULL); 5110169352Srrs } 5111163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5112163953Srrs /* We are not interested anymore */ 5113163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5114163953Srrs ; 5115169420Srrs } else if (stcb) { 5116172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5117172090Srrs so = SCTP_INP_SO(inp); 5118172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5119172090Srrs SCTP_TCB_UNLOCK(stcb); 5120172090Srrs SCTP_SOCKET_LOCK(so, 1); 5121172090Srrs SCTP_TCB_LOCK(stcb); 5122172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5123172090Srrs#endif 5124171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5125172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5126172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5127172090Srrs#endif 5128163953Srrs *offset = length; 5129163953Srrs return (NULL); 5130163953Srrs } 5131163953Srrs } 5132163953Srrs /* He's alive so give him credit */ 5133169420Srrs if ((stcb) && netp && *netp) { 5134179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5135171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5136171943Srrs stcb->asoc.overall_error_count, 5137171943Srrs 0, 5138171943Srrs SCTP_FROM_SCTP_INPUT, 5139171943Srrs __LINE__); 5140171943Srrs } 5141169378Srrs stcb->asoc.overall_error_count = 0; 5142169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5143169378Srrs } 5144163953Srrs break; 5145163953Srrs case SCTP_ECN_ECHO: 5146169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5147163953Srrs /* He's alive so give him credit */ 5148169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5149169208Srrs /* Its not ours */ 5150169378Srrs if (locked_tcb) { 5151169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5152169378Srrs } 5153169208Srrs *offset = length; 5154169208Srrs return (NULL); 5155169208Srrs } 5156169378Srrs if (stcb) { 5157179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5158171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5159171943Srrs stcb->asoc.overall_error_count, 5160171943Srrs 0, 5161171943Srrs SCTP_FROM_SCTP_INPUT, 5162171943Srrs __LINE__); 5163171943Srrs } 5164169378Srrs stcb->asoc.overall_error_count = 0; 5165169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5166169378Srrs stcb); 5167218186Srrs ecne_seen = 1; 5168169378Srrs } 5169163953Srrs break; 5170163953Srrs case SCTP_ECN_CWR: 5171169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5172163953Srrs /* He's alive so give him credit */ 5173169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5174169208Srrs /* Its not ours */ 5175169378Srrs if (locked_tcb) { 5176169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5177169378Srrs } 5178169208Srrs *offset = length; 5179169208Srrs return (NULL); 5180169208Srrs } 5181169378Srrs if (stcb) { 5182179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5183171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5184171943Srrs stcb->asoc.overall_error_count, 5185171943Srrs 0, 5186171943Srrs SCTP_FROM_SCTP_INPUT, 5187171943Srrs __LINE__); 5188171943Srrs } 5189169378Srrs stcb->asoc.overall_error_count = 0; 5190218072Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5191169378Srrs } 5192163953Srrs break; 5193163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5194170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5195163953Srrs /* must be first and only chunk */ 5196163953Srrs if ((num_chunks > 1) || 5197170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5198163953Srrs *offset = length; 5199169378Srrs if (locked_tcb) { 5200163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5201169378Srrs } 5202163953Srrs return (NULL); 5203163953Srrs } 5204169420Srrs if ((stcb) && netp && *netp) { 5205169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5206169378Srrs stcb, *netp); 5207169378Srrs } 5208163953Srrs *offset = length; 5209163953Srrs return (NULL); 5210163953Srrs break; 5211163953Srrs case SCTP_ASCONF: 5212169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5213163953Srrs /* He's alive so give him credit */ 5214169378Srrs if (stcb) { 5215179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5216171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5217171943Srrs stcb->asoc.overall_error_count, 5218171943Srrs 0, 5219171943Srrs SCTP_FROM_SCTP_INPUT, 5220171943Srrs __LINE__); 5221171943Srrs } 5222169378Srrs stcb->asoc.overall_error_count = 0; 5223169378Srrs sctp_handle_asconf(m, *offset, 5224171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5225171990Srrs asconf_cnt++; 5226169378Srrs } 5227163953Srrs break; 5228163953Srrs case SCTP_ASCONF_ACK: 5229169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5230169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5231169208Srrs /* Its not ours */ 5232169378Srrs if (locked_tcb) { 5233169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5234169378Srrs } 5235169208Srrs *offset = length; 5236169208Srrs return (NULL); 5237169208Srrs } 5238169420Srrs if ((stcb) && netp && *netp) { 5239169378Srrs /* He's alive so give him credit */ 5240179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5241171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5242171943Srrs stcb->asoc.overall_error_count, 5243171943Srrs 0, 5244171943Srrs SCTP_FROM_SCTP_INPUT, 5245171943Srrs __LINE__); 5246171943Srrs } 5247169378Srrs stcb->asoc.overall_error_count = 0; 5248169378Srrs sctp_handle_asconf_ack(m, *offset, 5249172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5250172190Srrs if (abort_no_unlock) 5251172190Srrs return (NULL); 5252169378Srrs } 5253163953Srrs break; 5254163953Srrs case SCTP_FORWARD_CUM_TSN: 5255169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5256169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5257169208Srrs /* Its not ours */ 5258169378Srrs if (locked_tcb) { 5259169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5260169378Srrs } 5261169208Srrs *offset = length; 5262169208Srrs return (NULL); 5263169208Srrs } 5264163953Srrs /* He's alive so give him credit */ 5265169378Srrs if (stcb) { 5266163953Srrs int abort_flag = 0; 5267163953Srrs 5268163953Srrs stcb->asoc.overall_error_count = 0; 5269179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5270171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5271171943Srrs stcb->asoc.overall_error_count, 5272171943Srrs 0, 5273171943Srrs SCTP_FROM_SCTP_INPUT, 5274171943Srrs __LINE__); 5275171943Srrs } 5276163953Srrs *fwd_tsn_seen = 1; 5277163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5278163953Srrs /* We are not interested anymore */ 5279172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5280172090Srrs so = SCTP_INP_SO(inp); 5281172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5282172090Srrs SCTP_TCB_UNLOCK(stcb); 5283172090Srrs SCTP_SOCKET_LOCK(so, 1); 5284172090Srrs SCTP_TCB_LOCK(stcb); 5285172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5286172090Srrs#endif 5287171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5288172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5289172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5290172090Srrs#endif 5291163953Srrs *offset = length; 5292163953Srrs return (NULL); 5293163953Srrs } 5294163953Srrs sctp_handle_forward_tsn(stcb, 5295170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5296163953Srrs if (abort_flag) { 5297163953Srrs *offset = length; 5298163953Srrs return (NULL); 5299163953Srrs } else { 5300179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5301171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5302171943Srrs stcb->asoc.overall_error_count, 5303171943Srrs 0, 5304171943Srrs SCTP_FROM_SCTP_INPUT, 5305171943Srrs __LINE__); 5306171943Srrs } 5307163953Srrs stcb->asoc.overall_error_count = 0; 5308163953Srrs } 5309163953Srrs 5310163953Srrs } 5311163953Srrs break; 5312163953Srrs case SCTP_STREAM_RESET: 5313169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5314169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5315169208Srrs /* Its not ours */ 5316169378Srrs if (locked_tcb) { 5317169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5318169378Srrs } 5319169208Srrs *offset = length; 5320169208Srrs return (NULL); 5321169208Srrs } 5322163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5323163953Srrs /* We are not interested anymore */ 5324172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5325172090Srrs so = SCTP_INP_SO(inp); 5326172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5327172090Srrs SCTP_TCB_UNLOCK(stcb); 5328172090Srrs SCTP_SOCKET_LOCK(so, 1); 5329172090Srrs SCTP_TCB_LOCK(stcb); 5330172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5331172090Srrs#endif 5332171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5333172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5334172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5335172090Srrs#endif 5336163953Srrs *offset = length; 5337163953Srrs return (NULL); 5338163953Srrs } 5339169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5340169420Srrs /* 5341169420Srrs * hmm, peer should have announced this, but 5342169420Srrs * we will turn it on since he is sending us 5343169420Srrs * a stream reset. 5344169420Srrs */ 5345169420Srrs stcb->asoc.peer_supports_strreset = 1; 5346163953Srrs } 5347170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5348169420Srrs /* stop processing */ 5349169420Srrs *offset = length; 5350169420Srrs return (NULL); 5351169420Srrs } 5352163953Srrs break; 5353163953Srrs case SCTP_PACKET_DROPPED: 5354169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5355163953Srrs /* re-get it all please */ 5356169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5357169208Srrs /* Its not ours */ 5358169378Srrs if (locked_tcb) { 5359169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5360169378Srrs } 5361169208Srrs *offset = length; 5362169208Srrs return (NULL); 5363169208Srrs } 5364169420Srrs if (ch && (stcb) && netp && (*netp)) { 5365169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5366170781Srrs stcb, *netp, 5367170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5368170781Srrs 5369169378Srrs } 5370163953Srrs break; 5371163953Srrs 5372163953Srrs case SCTP_AUTHENTICATION: 5373169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5374179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5375163953Srrs goto unknown_chunk; 5376163953Srrs 5377163953Srrs if (stcb == NULL) { 5378163953Srrs /* save the first AUTH for later processing */ 5379163953Srrs if (auth_skipped == 0) { 5380163953Srrs auth_offset = *offset; 5381163953Srrs auth_len = chk_length; 5382163953Srrs auth_skipped = 1; 5383163953Srrs } 5384163953Srrs /* skip this chunk (temporarily) */ 5385163953Srrs goto next_chunk; 5386163953Srrs } 5387169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5388169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5389169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5390169208Srrs /* Its not ours */ 5391169378Srrs if (locked_tcb) { 5392169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5393169378Srrs } 5394169208Srrs *offset = length; 5395169208Srrs return (NULL); 5396169208Srrs } 5397163953Srrs if (got_auth == 1) { 5398163953Srrs /* skip this chunk... it's already auth'd */ 5399163953Srrs goto next_chunk; 5400163953Srrs } 5401163953Srrs got_auth = 1; 5402169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5403163953Srrs m, *offset)) { 5404163953Srrs /* auth HMAC failed so dump the packet */ 5405163953Srrs *offset = length; 5406163953Srrs return (stcb); 5407163953Srrs } else { 5408163953Srrs /* remaining chunks are HMAC checked */ 5409163953Srrs stcb->asoc.authenticated = 1; 5410163953Srrs } 5411163953Srrs break; 5412163953Srrs 5413163953Srrs default: 5414163953Srrs unknown_chunk: 5415163953Srrs /* it's an unknown chunk! */ 5416163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5417163953Srrs struct mbuf *mm; 5418163953Srrs struct sctp_paramhdr *phd; 5419163953Srrs 5420163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5421165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5422163953Srrs if (mm) { 5423163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5424163953Srrs /* 5425163953Srrs * We cheat and use param type since 5426163953Srrs * we did not bother to define a 5427163953Srrs * error cause struct. They are the 5428163953Srrs * same basic format with different 5429163953Srrs * names. 5430163953Srrs */ 5431163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5432163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5433165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5434166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5435163953Srrs M_DONTWAIT); 5436165647Srrs if (SCTP_BUF_NEXT(mm)) { 5437178202Srrs#ifdef SCTP_MBUF_LOGGING 5438179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5439178202Srrs struct mbuf *mat; 5440178202Srrs 5441178202Srrs mat = SCTP_BUF_NEXT(mm); 5442178202Srrs while (mat) { 5443178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5444178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5445178202Srrs } 5446178202Srrs mat = SCTP_BUF_NEXT(mat); 5447178202Srrs } 5448178202Srrs } 5449178202Srrs#endif 5450163953Srrs sctp_queue_op_err(stcb, mm); 5451163953Srrs } else { 5452163953Srrs sctp_m_freem(mm); 5453163953Srrs } 5454163953Srrs } 5455163953Srrs } 5456163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5457163953Srrs /* discard this packet */ 5458163953Srrs *offset = length; 5459163953Srrs return (stcb); 5460163953Srrs } /* else skip this bad chunk and continue... */ 5461163953Srrs break; 5462163953Srrs } /* switch (ch->chunk_type) */ 5463163953Srrs 5464163953Srrs 5465163953Srrsnext_chunk: 5466163953Srrs /* get the next chunk */ 5467163953Srrs *offset += SCTP_SIZE32(chk_length); 5468163953Srrs if (*offset >= length) { 5469163953Srrs /* no more data left in the mbuf chain */ 5470163953Srrs break; 5471163953Srrs } 5472163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5473163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5474163953Srrs if (ch == NULL) { 5475169378Srrs if (locked_tcb) { 5476163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5477169378Srrs } 5478163953Srrs *offset = length; 5479163953Srrs return (NULL); 5480163953Srrs } 5481163953Srrs } /* while */ 5482171990Srrs 5483171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5484171990Srrs sctp_send_asconf_ack(stcb); 5485171990Srrs } 5486163953Srrs return (stcb); 5487163953Srrs} 5488163953Srrs 5489163953Srrs 5490171440Srrs#ifdef INVARIANTS 5491208853Srrs#ifdef __GNUC__ 5492208853Srrs__attribute__((noinline)) 5493208853Srrs#endif 5494208853Srrs void 5495208853Srrs sctp_validate_no_locks(struct sctp_inpcb *inp) 5496171440Srrs{ 5497208853Srrs struct sctp_tcb *lstcb; 5498171440Srrs 5499208853Srrs LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5500208853Srrs if (mtx_owned(&lstcb->tcb_mtx)) { 5501171440Srrs panic("Own lock on stcb at return from input"); 5502171440Srrs } 5503171440Srrs } 5504208876Srrs if (mtx_owned(&inp->inp_create_mtx)) { 5505208876Srrs panic("Own create lock on inp"); 5506208876Srrs } 5507208876Srrs if (mtx_owned(&inp->inp_mtx)) { 5508208876Srrs panic("Own inp lock on inp"); 5509208876Srrs } 5510171440Srrs} 5511171440Srrs 5512171440Srrs#endif 5513171440Srrs 5514163953Srrs/* 5515163953Srrs * common input chunk processing (v4 and v6) 5516163953Srrs */ 5517169378Srrsvoid 5518163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5519163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5520163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5521179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5522163953Srrs{ 5523163953Srrs /* 5524163953Srrs * Control chunk processing 5525163953Srrs */ 5526163953Srrs uint32_t high_tsn; 5527163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5528163953Srrs struct mbuf *m = *mm; 5529163953Srrs int abort_flag = 0; 5530163953Srrs int un_sent; 5531218129Srrs int cnt_ctrl_ready = 0; 5532163953Srrs 5533163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5534163953Srrs#ifdef SCTP_AUDITING_ENABLED 5535163953Srrs sctp_audit_log(0xE0, 1); 5536163953Srrs sctp_auditing(0, inp, stcb, net); 5537163953Srrs#endif 5538163953Srrs 5539179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5540179783Srrs m, iphlen, offset, length, stcb); 5541163953Srrs if (stcb) { 5542163953Srrs /* always clear this before beginning a packet */ 5543163953Srrs stcb->asoc.authenticated = 0; 5544163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5545171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5546171990Srrs stcb, stcb->asoc.state); 5547171990Srrs 5548171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5549171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5550171745Srrs /*- 5551171745Srrs * If we hit here, we had a ref count 5552171745Srrs * up when the assoc was aborted and the 5553171745Srrs * timer is clearing out the assoc, we should 5554171745Srrs * NOT respond to any packet.. its OOTB. 5555171745Srrs */ 5556171745Srrs SCTP_TCB_UNLOCK(stcb); 5557171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5558179157Srrs vrf_id, port); 5559171745Srrs goto out_now; 5560171745Srrs } 5561163953Srrs } 5562163953Srrs if (IS_SCTP_CONTROL(ch)) { 5563163953Srrs /* process the control portion of the SCTP packet */ 5564169655Srrs /* sa_ignore NO_NULL_CHK */ 5565163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5566179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5567163953Srrs if (stcb) { 5568163953Srrs /* 5569163953Srrs * This covers us if the cookie-echo was there and 5570163953Srrs * it changes our INP. 5571163953Srrs */ 5572163953Srrs inp = stcb->sctp_ep; 5573179783Srrs if ((net) && (port)) { 5574179783Srrs if (net->port == 0) { 5575179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5576179783Srrs } 5577179783Srrs net->port = port; 5578179783Srrs } 5579163953Srrs } 5580163953Srrs } else { 5581163953Srrs /* 5582163953Srrs * no control chunks, so pre-process DATA chunks (these 5583163953Srrs * checks are taken care of by control processing) 5584163953Srrs */ 5585163953Srrs 5586163953Srrs /* 5587163953Srrs * if DATA only packet, and auth is required, then punt... 5588163953Srrs * can't have authenticated without any AUTH (control) 5589163953Srrs * chunks 5590163953Srrs */ 5591179783Srrs if ((stcb != NULL) && 5592179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5593179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5594163953Srrs /* "silently" ignore */ 5595163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5596163953Srrs SCTP_TCB_UNLOCK(stcb); 5597171440Srrs goto out_now; 5598163953Srrs } 5599163953Srrs if (stcb == NULL) { 5600163953Srrs /* out of the blue DATA chunk */ 5601169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5602179157Srrs vrf_id, port); 5603171440Srrs goto out_now; 5604163953Srrs } 5605163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5606163953Srrs /* v_tag mismatch! */ 5607163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5608163953Srrs SCTP_TCB_UNLOCK(stcb); 5609171440Srrs goto out_now; 5610163953Srrs } 5611163953Srrs } 5612163953Srrs 5613163953Srrs if (stcb == NULL) { 5614163953Srrs /* 5615163953Srrs * no valid TCB for this packet, or we found it's a bad 5616163953Srrs * packet while processing control, or we're done with this 5617163953Srrs * packet (done or skip rest of data), so we drop it... 5618163953Srrs */ 5619171440Srrs goto out_now; 5620163953Srrs } 5621163953Srrs /* 5622163953Srrs * DATA chunk processing 5623163953Srrs */ 5624163953Srrs /* plow through the data chunks while length > offset */ 5625163953Srrs 5626163953Srrs /* 5627163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5628163953Srrs * DATA is required. 5629163953Srrs */ 5630179783Srrs if ((length > offset) && 5631179783Srrs (stcb != NULL) && 5632179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5633179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5634163953Srrs !stcb->asoc.authenticated) { 5635163953Srrs /* "silently" ignore */ 5636163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5637169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5638169420Srrs "Data chunk requires AUTH, skipped\n"); 5639165220Srrs goto trigger_send; 5640163953Srrs } 5641163953Srrs if (length > offset) { 5642163953Srrs int retval; 5643163953Srrs 5644163953Srrs /* 5645163953Srrs * First check to make sure our state is correct. We would 5646163953Srrs * not get here unless we really did have a tag, so we don't 5647163953Srrs * abort if this happens, just dump the chunk silently. 5648163953Srrs */ 5649163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5650163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5651163953Srrs /* 5652163953Srrs * we consider data with valid tags in this state 5653163953Srrs * shows us the cookie-ack was lost. Imply it was 5654163953Srrs * there. 5655163953Srrs */ 5656179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5657171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5658171943Srrs stcb->asoc.overall_error_count, 5659171943Srrs 0, 5660171943Srrs SCTP_FROM_SCTP_INPUT, 5661171943Srrs __LINE__); 5662171943Srrs } 5663163953Srrs stcb->asoc.overall_error_count = 0; 5664163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5665163953Srrs break; 5666163953Srrs case SCTP_STATE_COOKIE_WAIT: 5667163953Srrs /* 5668163953Srrs * We consider OOTB any data sent during asoc setup. 5669163953Srrs */ 5670169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5671179157Srrs vrf_id, port); 5672163953Srrs SCTP_TCB_UNLOCK(stcb); 5673171440Srrs goto out_now; 5674171531Srrs /* sa_ignore NOTREACHED */ 5675163953Srrs break; 5676163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5677163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5678163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5679163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5680163953Srrs default: 5681163953Srrs SCTP_TCB_UNLOCK(stcb); 5682171440Srrs goto out_now; 5683171531Srrs /* sa_ignore NOTREACHED */ 5684163953Srrs break; 5685163953Srrs case SCTP_STATE_OPEN: 5686163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5687163953Srrs break; 5688163953Srrs } 5689163953Srrs /* plow through the data chunks while length > offset */ 5690163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5691163953Srrs inp, stcb, net, &high_tsn); 5692163953Srrs if (retval == 2) { 5693163953Srrs /* 5694163953Srrs * The association aborted, NO UNLOCK needed since 5695163953Srrs * the association is destroyed. 5696163953Srrs */ 5697171440Srrs goto out_now; 5698163953Srrs } 5699163953Srrs data_processed = 1; 5700163953Srrs /* 5701163953Srrs * Anything important needs to have been m_copy'ed in 5702163953Srrs * process_data 5703163953Srrs */ 5704163953Srrs } 5705218129Srrs /* take care of ecn */ 5706218235Stuexen if ((stcb->asoc.ecn_allowed == 1) && 5707218235Stuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5708218129Srrs /* Yep, we need to add a ECNE */ 5709218129Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5710218129Srrs } 5711163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5712216495Stuexen int was_a_gap; 5713216495Stuexen uint32_t highest_tsn; 5714163953Srrs 5715216825Stuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5716216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5717216495Stuexen } else { 5718216495Stuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5719163953Srrs } 5720216825Stuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5721190689Srrs stcb->asoc.send_sack = 1; 5722206137Stuexen sctp_sack_check(stcb, was_a_gap, &abort_flag); 5723163953Srrs if (abort_flag) { 5724163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5725171440Srrs goto out_now; 5726163953Srrs } 5727190689Srrs } else if (fwd_tsn_seen) { 5728190689Srrs stcb->asoc.send_sack = 1; 5729163953Srrs } 5730163953Srrs /* trigger send of any chunks in queue... */ 5731165220Srrstrigger_send: 5732163953Srrs#ifdef SCTP_AUDITING_ENABLED 5733163953Srrs sctp_audit_log(0xE0, 2); 5734163953Srrs sctp_auditing(1, inp, stcb, net); 5735163953Srrs#endif 5736169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5737169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5738169420Srrs stcb->asoc.peers_rwnd, 5739169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5740169420Srrs stcb->asoc.total_flight); 5741163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5742218129Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5743218129Srrs cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5744218129Srrs } 5745218129Srrs if (cnt_ctrl_ready || 5746163953Srrs ((un_sent) && 5747163953Srrs (stcb->asoc.peers_rwnd > 0 || 5748163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5749169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5750172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5751169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5752163953Srrs } 5753163953Srrs#ifdef SCTP_AUDITING_ENABLED 5754163953Srrs sctp_audit_log(0xE0, 3); 5755163953Srrs sctp_auditing(2, inp, stcb, net); 5756163953Srrs#endif 5757163953Srrs SCTP_TCB_UNLOCK(stcb); 5758171440Srrsout_now: 5759171440Srrs#ifdef INVARIANTS 5760171440Srrs sctp_validate_no_locks(inp); 5761171440Srrs#endif 5762169378Srrs return; 5763163953Srrs} 5764163953Srrs 5765185694Srrs#if 0 5766185694Srrsstatic void 5767185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5768185694Srrs{ 5769185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5770185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5771185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5772185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5773185694Srrs } 5774185694Srrs} 5775163953Srrs 5776185694Srrs#endif 5777185694Srrs 5778221249Stuexen#ifdef INET 5779163953Srrsvoid 5780189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5781163953Srrs{ 5782163953Srrs#ifdef SCTP_MBUF_LOGGING 5783163953Srrs struct mbuf *mat; 5784163953Srrs 5785163953Srrs#endif 5786165647Srrs struct mbuf *m; 5787163953Srrs int iphlen; 5788170181Srrs uint32_t vrf_id = 0; 5789163953Srrs uint8_t ecn_bits; 5790163953Srrs struct ip *ip; 5791163953Srrs struct sctphdr *sh; 5792163953Srrs struct sctp_inpcb *inp = NULL; 5793163953Srrs struct sctp_nets *net; 5794163953Srrs struct sctp_tcb *stcb = NULL; 5795163953Srrs struct sctp_chunkhdr *ch; 5796163953Srrs int refcount_up = 0; 5797163953Srrs int length, mlen, offset; 5798163953Srrs 5799211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 5800211969Stuexen uint32_t check, calc_check; 5801211969Stuexen 5802211969Stuexen#endif 5803211969Stuexen 5804169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5805169352Srrs SCTP_RELEASE_PKT(i_pak); 5806169352Srrs return; 5807169352Srrs } 5808168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5809163953Srrs iphlen = off; 5810165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5811170056Srrs 5812163953Srrs net = NULL; 5813163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5814163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5815163953Srrs 5816170091Srrs 5817163953Srrs#ifdef SCTP_MBUF_LOGGING 5818163953Srrs /* Log in any input mbufs */ 5819179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5820170744Srrs mat = m; 5821170744Srrs while (mat) { 5822170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5823170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5824170744Srrs } 5825170744Srrs mat = SCTP_BUF_NEXT(mat); 5826163953Srrs } 5827163953Srrs } 5828163953Srrs#endif 5829170091Srrs#ifdef SCTP_PACKET_LOGGING 5830179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5831170921Srrs sctp_packet_log(m, mlen); 5832170091Srrs#endif 5833170091Srrs /* 5834170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5835170091Srrs * case) 5836170091Srrs */ 5837170091Srrs mlen -= iphlen; 5838168124Srrs 5839163953Srrs /* 5840163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5841163953Srrs */ 5842163953Srrs ip = mtod(m, struct ip *); 5843163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5844165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5845163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5846163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5847163953Srrs return; 5848163953Srrs } 5849163953Srrs ip = mtod(m, struct ip *); 5850163953Srrs } 5851188067Srrs /* validate mbuf chain length with IP payload length */ 5852188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5853188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5854188067Srrs goto bad; 5855188067Srrs } 5856163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5857163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5858170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5859170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5860163953Srrs 5861163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5862163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5863163953Srrs goto bad; 5864163953Srrs } 5865168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5866163953Srrs /* 5867163953Srrs * We only look at broadcast if its a front state, All 5868163953Srrs * others we will not have a tcb for anyway. 5869163953Srrs */ 5870163953Srrs goto bad; 5871163953Srrs } 5872163953Srrs /* validate SCTP checksum */ 5873188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5874188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5875188067Srrs m->m_pkthdr.len, 5876188067Srrs if_name(m->m_pkthdr.rcvif), 5877188067Srrs m->m_pkthdr.csum_flags); 5878211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 5879211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 5880211969Stuexen#else 5881188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5882188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5883188067Srrs goto sctp_skip_csum_4; 5884188067Srrs } 5885171990Srrs check = sh->checksum; /* save incoming checksum */ 5886171990Srrs sh->checksum = 0; /* prepare for calc */ 5887188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5888203503Stuexen sh->checksum = check; 5889188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5890171990Srrs if (calc_check != check) { 5891171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5892171990Srrs calc_check, check, m, mlen, iphlen); 5893163953Srrs 5894171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5895171990Srrs offset - sizeof(*ch), 5896171990Srrs sh, ch, &inp, &net, 5897171990Srrs vrf_id); 5898179157Srrs if ((net) && (port)) { 5899179157Srrs if (net->port == 0) { 5900179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5901179157Srrs } 5902179157Srrs net->port = port; 5903179157Srrs } 5904218400Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5905218400Stuexen net->flowid = m->m_pkthdr.flowid; 5906218400Stuexen#ifdef INVARIANTS 5907218400Stuexen net->flowidset = 1; 5908218400Stuexen#endif 5909218400Stuexen } 5910171990Srrs if ((inp) && (stcb)) { 5911171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5912172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5913171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5914171990Srrs refcount_up = 1; 5915163953Srrs } 5916171990Srrs SCTP_STAT_INCR(sctps_badsum); 5917171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5918171990Srrs goto bad; 5919168299Srrs } 5920163953Srrssctp_skip_csum_4: 5921211969Stuexen#endif 5922168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5923168124Srrs if (sh->dest_port == 0) { 5924168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5925168124Srrs goto bad; 5926168124Srrs } 5927163953Srrs /* 5928163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5929163953Srrs * IP/SCTP/first chunk header... 5930163953Srrs */ 5931163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5932168299Srrs sh, ch, &inp, &net, vrf_id); 5933179157Srrs if ((net) && (port)) { 5934179157Srrs if (net->port == 0) { 5935179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5936179157Srrs } 5937179157Srrs net->port = port; 5938179157Srrs } 5939218335Stuexen if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5940218335Stuexen net->flowid = m->m_pkthdr.flowid; 5941218400Stuexen#ifdef INVARIANTS 5942218392Srrs net->flowidset = 1; 5943218400Stuexen#endif 5944218335Stuexen } 5945163953Srrs /* inp's ref-count increased && stcb locked */ 5946163953Srrs if (inp == NULL) { 5947163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5948163953Srrs 5949163953Srrs SCTP_STAT_INCR(sctps_noport); 5950163953Srrs#ifdef ICMP_BANDLIM 5951163953Srrs /* 5952163953Srrs * we use the bandwidth limiting to protect against sending 5953163953Srrs * too many ABORTS all at once. In this case these count the 5954163953Srrs * same as an ICMP message. 5955163953Srrs */ 5956163953Srrs if (badport_bandlim(0) < 0) 5957163953Srrs goto bad; 5958163953Srrs#endif /* ICMP_BANDLIM */ 5959169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5960169420Srrs "Sending a ABORT from packet entry!\n"); 5961163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5962163953Srrs /* 5963163953Srrs * we do a trick here to get the INIT tag, dig in 5964163953Srrs * and get the tag from the INIT and put it in the 5965163953Srrs * common header. 5966163953Srrs */ 5967163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5968163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5969163953Srrs (uint8_t *) & chunk_buf); 5970163953Srrs if (init_chk != NULL) 5971163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5972163953Srrs } 5973165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5974179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5975165220Srrs goto bad; 5976165220Srrs } 5977165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5978165220Srrs goto bad; 5979165220Srrs } 5980165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5981179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5982163953Srrs goto bad; 5983163953Srrs } else if (stcb == NULL) { 5984163953Srrs refcount_up = 1; 5985163953Srrs } 5986171167Sgnn#ifdef IPSEC 5987163953Srrs /* 5988163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5989163953Srrs * idea, so I will leave it in place. 5990163953Srrs */ 5991163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5992197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5993163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5994163953Srrs goto bad; 5995163953Srrs } 5996163953Srrs#endif /* IPSEC */ 5997163953Srrs 5998163953Srrs /* 5999163953Srrs * common chunk processing 6000163953Srrs */ 6001163953Srrs length = ip->ip_len + iphlen; 6002163953Srrs offset -= sizeof(struct sctp_chunkhdr); 6003163953Srrs 6004163953Srrs ecn_bits = ip->ip_tos; 6005163953Srrs 6006169655Srrs /* sa_ignore NO_NULL_CHK */ 6007163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 6008179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 6009163953Srrs /* inp's ref-count reduced && stcb unlocked */ 6010163953Srrs if (m) { 6011163953Srrs sctp_m_freem(m); 6012163953Srrs } 6013163953Srrs if ((inp) && (refcount_up)) { 6014163953Srrs /* reduce ref-count */ 6015163953Srrs SCTP_INP_DECR_REF(inp); 6016163953Srrs } 6017163953Srrs return; 6018163953Srrsbad: 6019169420Srrs if (stcb) { 6020163953Srrs SCTP_TCB_UNLOCK(stcb); 6021169420Srrs } 6022163953Srrs if ((inp) && (refcount_up)) { 6023163953Srrs /* reduce ref-count */ 6024163953Srrs SCTP_INP_DECR_REF(inp); 6025163953Srrs } 6026163953Srrs if (m) { 6027163953Srrs sctp_m_freem(m); 6028163953Srrs } 6029163953Srrs return; 6030163953Srrs} 6031218211Srrs 6032218269Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6033218269Srrsextern int *sctp_cpuarry; 6034218211Srrs 6035218269Srrs#endif 6036218269Srrs 6037179157Srrsvoid 6038218211Srrssctp_input(struct mbuf *m, int off) 6039179157Srrs{ 6040218211Srrs#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6041218211Srrs struct ip *ip; 6042218211Srrs struct sctphdr *sh; 6043218211Srrs int offset; 6044218211Srrs int cpu_to_use; 6045218371Srrs uint32_t flowid, tag; 6046218211Srrs 6047218269Srrs if (mp_ncpus > 1) { 6048218371Srrs if (m->m_flags & M_FLOWID) { 6049218371Srrs flowid = m->m_pkthdr.flowid; 6050218269Srrs } else { 6051218269Srrs /* 6052218371Srrs * No flow id built by lower layers fix it so we 6053218371Srrs * create one. 6054218269Srrs */ 6055218371Srrs ip = mtod(m, struct ip *); 6056218371Srrs offset = off + sizeof(*sh); 6057218371Srrs if (SCTP_BUF_LEN(m) < offset) { 6058218371Srrs if ((m = m_pullup(m, offset)) == 0) { 6059218371Srrs SCTP_STAT_INCR(sctps_hdrops); 6060218371Srrs return; 6061218371Srrs } 6062218371Srrs ip = mtod(m, struct ip *); 6063218371Srrs } 6064218371Srrs sh = (struct sctphdr *)((caddr_t)ip + off); 6065218371Srrs tag = htonl(sh->v_tag); 6066218371Srrs flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6067218371Srrs m->m_pkthdr.flowid = flowid; 6068218371Srrs m->m_flags |= M_FLOWID; 6069218269Srrs } 6070218371Srrs cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6071218211Srrs sctp_queue_to_mcore(m, off, cpu_to_use); 6072218211Srrs return; 6073218211Srrs } 6074218211Srrs#endif 6075218211Srrs sctp_input_with_port(m, off, 0); 6076179157Srrs} 6077221249Stuexen 6078221249Stuexen#endif 6079