sctp_input.c revision 205627
1163953Srrs/*- 2185694Srrs * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3163953Srrs * 4163953Srrs * Redistribution and use in source and binary forms, with or without 5163953Srrs * modification, are permitted provided that the following conditions are met: 6163953Srrs * 7163953Srrs * a) Redistributions of source code must retain the above copyright notice, 8163953Srrs * this list of conditions and the following disclaimer. 9163953Srrs * 10163953Srrs * b) Redistributions in binary form must reproduce the above copyright 11163953Srrs * notice, this list of conditions and the following disclaimer in 12163953Srrs * the documentation and/or other materials provided with the distribution. 13163953Srrs * 14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 15163953Srrs * contributors may be used to endorse or promote products derived 16163953Srrs * from this software without specific prior written permission. 17163953Srrs * 18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 29163953Srrs */ 30163953Srrs 31163953Srrs/* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */ 32163953Srrs 33163953Srrs#include <sys/cdefs.h> 34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 205627 2010-03-24 19:45:36Z rrs $"); 35163953Srrs 36163953Srrs#include <netinet/sctp_os.h> 37163953Srrs#include <netinet/sctp_var.h> 38167598Srrs#include <netinet/sctp_sysctl.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctputil.h> 42163953Srrs#include <netinet/sctp_output.h> 43163953Srrs#include <netinet/sctp_input.h> 44163953Srrs#include <netinet/sctp_auth.h> 45163953Srrs#include <netinet/sctp_indata.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47170091Srrs#include <netinet/sctp_bsd_addr.h> 48172091Srrs#include <netinet/sctp_timer.h> 49188067Srrs#include <netinet/sctp_crc32.h> 50179157Srrs#include <netinet/udp.h> 51163953Srrs 52163953Srrs 53163953Srrs 54163953Srrsstatic void 55163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 56163953Srrs{ 57163953Srrs struct sctp_nets *net; 58163953Srrs 59165220Srrs /* 60165220Srrs * This now not only stops all cookie timers it also stops any INIT 61165220Srrs * timers as well. This will make sure that the timers are stopped 62165220Srrs * in all collision cases. 63165220Srrs */ 64163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 65163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 66165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 67163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 68163953Srrs stcb->sctp_ep, 69163953Srrs stcb, 70165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 71165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 72165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 73165220Srrs stcb->sctp_ep, 74165220Srrs stcb, 75165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 76163953Srrs } 77163953Srrs } 78163953Srrs} 79163953Srrs 80163953Srrs/* INIT handler */ 81163953Srrsstatic void 82163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 83163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 84179157Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 85163953Srrs{ 86163953Srrs struct sctp_init *init; 87163953Srrs struct mbuf *op_err; 88163953Srrs uint32_t init_limit; 89163953Srrs 90169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 91169420Srrs stcb); 92172396Srrs if (stcb == NULL) { 93172396Srrs SCTP_INP_RLOCK(inp); 94172396Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 95172396Srrs goto outnow; 96172396Srrs } 97172396Srrs } 98163953Srrs op_err = NULL; 99163953Srrs init = &cp->init; 100163953Srrs /* First are we accepting? */ 101163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 102169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 103169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 104169420Srrs inp->sctp_socket->so_qlimit); 105163953Srrs /* 106163953Srrs * FIX ME ?? What about TCP model and we have a 107179141Srrs * match/restart case? Actually no fix is needed. the lookup 108179141Srrs * will always find the existing assoc so stcb would not be 109179141Srrs * NULL. It may be questionable to do this since we COULD 110179141Srrs * just send back the INIT-ACK and hope that the app did 111179141Srrs * accept()'s by the time the COOKIE was sent. But there is 112179141Srrs * a price to pay for COOKIE generation and I don't want to 113179141Srrs * pay it on the chance that the app will actually do some 114179141Srrs * accepts(). The App just looses and should NOT be in this 115179141Srrs * state :-) 116163953Srrs */ 117169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 118179157Srrs vrf_id, port); 119168299Srrs if (stcb) 120168299Srrs *abort_no_unlock = 1; 121172396Srrs goto outnow; 122163953Srrs } 123163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 124163953Srrs /* Invalid length */ 125163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 126169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 127179157Srrs vrf_id, port); 128168299Srrs if (stcb) 129168299Srrs *abort_no_unlock = 1; 130172396Srrs goto outnow; 131163953Srrs } 132163953Srrs /* validate parameters */ 133163953Srrs if (init->initiate_tag == 0) { 134163953Srrs /* protocol error... send abort */ 135163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 136169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 137179157Srrs vrf_id, port); 138168299Srrs if (stcb) 139168299Srrs *abort_no_unlock = 1; 140172396Srrs goto outnow; 141163953Srrs } 142163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 143163953Srrs /* invalid parameter... send abort */ 144163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 145169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 146179157Srrs vrf_id, port); 147171440Srrs if (stcb) 148171440Srrs *abort_no_unlock = 1; 149172396Srrs goto outnow; 150163953Srrs } 151163953Srrs if (init->num_inbound_streams == 0) { 152163953Srrs /* protocol error... send abort */ 153163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 154169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 155179157Srrs vrf_id, port); 156168299Srrs if (stcb) 157168299Srrs *abort_no_unlock = 1; 158172396Srrs goto outnow; 159163953Srrs } 160163953Srrs if (init->num_outbound_streams == 0) { 161163953Srrs /* protocol error... send abort */ 162163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 163169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 164179157Srrs vrf_id, port); 165168299Srrs if (stcb) 166168299Srrs *abort_no_unlock = 1; 167172396Srrs goto outnow; 168163953Srrs } 169163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 170163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 171163953Srrs init_limit)) { 172163953Srrs /* auth parameter(s) error... send abort */ 173179157Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 174168299Srrs if (stcb) 175168299Srrs *abort_no_unlock = 1; 176172396Srrs goto outnow; 177163953Srrs } 178163953Srrs /* send an INIT-ACK w/cookie */ 179169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 180179157Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 181172703Srrs ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 182172396Srrsoutnow: 183172396Srrs if (stcb == NULL) { 184172396Srrs SCTP_INP_RUNLOCK(inp); 185172396Srrs } 186163953Srrs} 187163953Srrs 188163953Srrs/* 189163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 190163953Srrs */ 191171158Srrs 192171158Srrsint 193171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 194171158Srrs{ 195171158Srrs int unsent_data = 0; 196171158Srrs struct sctp_stream_queue_pending *sp; 197171158Srrs struct sctp_stream_out *strq; 198171158Srrs struct sctp_association *asoc; 199171158Srrs 200171158Srrs /* 201171158Srrs * This function returns the number of streams that have true unsent 202171158Srrs * data on them. Note that as it looks through it will clean up any 203171158Srrs * places that have old data that has been sent but left at top of 204171158Srrs * stream queue. 205171158Srrs */ 206171158Srrs asoc = &stcb->asoc; 207171158Srrs SCTP_TCB_SEND_LOCK(stcb); 208171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 209171158Srrs /* Check to see if some data queued */ 210171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 211171531Srrs is_there_another: 212171158Srrs /* sa_ignore FREED_MEMORY */ 213171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 214171158Srrs if (sp == NULL) { 215171158Srrs continue; 216171158Srrs } 217171158Srrs if ((sp->msg_is_complete) && 218171158Srrs (sp->length == 0) && 219171158Srrs (sp->sender_all_done)) { 220171158Srrs /* 221171158Srrs * We are doing differed cleanup. Last time 222171158Srrs * through when we took all the data the 223171158Srrs * sender_all_done was not set. 224171158Srrs */ 225171158Srrs if (sp->put_last_out == 0) { 226171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 227171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 228171158Srrs sp->sender_all_done, 229171158Srrs sp->length, 230171158Srrs sp->msg_is_complete, 231171158Srrs sp->put_last_out); 232171158Srrs } 233171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 234171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 235171158Srrs sctp_free_remote_addr(sp->net); 236171158Srrs if (sp->data) { 237171158Srrs sctp_m_freem(sp->data); 238171158Srrs sp->data = NULL; 239171158Srrs } 240171158Srrs sctp_free_a_strmoq(stcb, sp); 241171158Srrs goto is_there_another; 242171158Srrs } else { 243171158Srrs unsent_data++; 244171158Srrs continue; 245171158Srrs } 246171158Srrs } 247171158Srrs } 248171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 249171158Srrs return (unsent_data); 250171158Srrs} 251171158Srrs 252163953Srrsstatic int 253163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 254163953Srrs struct sctp_nets *net) 255163953Srrs{ 256163953Srrs struct sctp_init *init; 257163953Srrs struct sctp_association *asoc; 258163953Srrs struct sctp_nets *lnet; 259163953Srrs unsigned int i; 260163953Srrs 261163953Srrs init = &cp->init; 262163953Srrs asoc = &stcb->asoc; 263163953Srrs /* save off parameters */ 264163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 265163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 266163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 267163953Srrs /* update any ssthresh's that may have a default */ 268163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 269163953Srrs lnet->ssthresh = asoc->peers_rwnd; 270163953Srrs 271179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 272170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 273170744Srrs } 274163953Srrs } 275163953Srrs } 276164181Srrs SCTP_TCB_SEND_LOCK(stcb); 277163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 278163953Srrs unsigned int newcnt; 279163953Srrs struct sctp_stream_out *outs; 280163953Srrs struct sctp_stream_queue_pending *sp; 281196260Stuexen struct sctp_tmit_chunk *chk, *chk_next; 282163953Srrs 283196260Stuexen /* abandon the upper streams */ 284163953Srrs newcnt = ntohs(init->num_inbound_streams); 285196260Stuexen if (!TAILQ_EMPTY(&asoc->send_queue)) { 286196260Stuexen chk = TAILQ_FIRST(&asoc->send_queue); 287196260Stuexen while (chk) { 288196260Stuexen chk_next = TAILQ_NEXT(chk, sctp_next); 289196260Stuexen if (chk->rec.data.stream_number >= newcnt) { 290196260Stuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 291196260Stuexen asoc->send_queue_cnt--; 292196260Stuexen if (chk->data != NULL) { 293196260Stuexen sctp_free_bufspace(stcb, asoc, chk, 1); 294196260Stuexen sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 295196260Stuexen SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 296196260Stuexen if (chk->data) { 297196260Stuexen sctp_m_freem(chk->data); 298196260Stuexen chk->data = NULL; 299196260Stuexen } 300196260Stuexen } 301196260Stuexen sctp_free_a_chunk(stcb, chk); 302196260Stuexen /* sa_ignore FREED_MEMORY */ 303196260Stuexen } 304196260Stuexen chk = chk_next; 305196260Stuexen } 306196260Stuexen } 307163953Srrs if (asoc->strmout) { 308163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 309163953Srrs outs = &asoc->strmout[i]; 310163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 311163953Srrs while (sp) { 312196260Stuexen TAILQ_REMOVE(&outs->outqueue, sp, next); 313163953Srrs asoc->stream_queue_cnt--; 314163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 315163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 316172090Srrs sp, SCTP_SO_NOT_LOCKED); 317163953Srrs if (sp->data) { 318163953Srrs sctp_m_freem(sp->data); 319163953Srrs sp->data = NULL; 320163953Srrs } 321163953Srrs sctp_free_remote_addr(sp->net); 322163953Srrs sp->net = NULL; 323163953Srrs /* Free the chunk */ 324163953Srrs sctp_free_a_strmoq(stcb, sp); 325169655Srrs /* sa_ignore FREED_MEMORY */ 326163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 327163953Srrs } 328163953Srrs } 329163953Srrs } 330196260Stuexen /* cut back the count */ 331163953Srrs asoc->pre_open_streams = newcnt; 332163953Srrs } 333164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 334188854Srrs asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 335163953Srrs /* init tsn's */ 336163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 337185694Srrs /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 338185694Srrs asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 339179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 340170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 341170744Srrs } 342163953Srrs /* This is the next one we expect */ 343163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 344163953Srrs 345163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 346185694Srrs /* 347185694Srrs * EY 05/13/08 - nr_sack: initialize nr_mapping array's base tsn 348185694Srrs * like above 349185694Srrs */ 350185694Srrs asoc->nr_mapping_array_base_tsn = ntohl(init->initial_tsn); 351180955Srrs asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 352163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 353163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 354163953Srrs /* open the requested streams */ 355170091Srrs 356163953Srrs if (asoc->strmin != NULL) { 357163953Srrs /* Free the old ones */ 358164181Srrs struct sctp_queued_to_read *ctl; 359164181Srrs 360164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 361164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 362164181Srrs while (ctl) { 363164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 364164181Srrs sctp_free_remote_addr(ctl->whoFrom); 365171158Srrs ctl->whoFrom = NULL; 366164181Srrs sctp_m_freem(ctl->data); 367164181Srrs ctl->data = NULL; 368164181Srrs sctp_free_a_readq(stcb, ctl); 369164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 370164181Srrs } 371164181Srrs } 372170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 373163953Srrs } 374164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 375164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 376164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 377164181Srrs } 378163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 379170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 380163953Srrs if (asoc->strmin == NULL) { 381163953Srrs /* we didn't get memory for the streams! */ 382169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 383163953Srrs return (-1); 384163953Srrs } 385163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 386163953Srrs asoc->strmin[i].stream_no = i; 387163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 388163953Srrs /* 389163953Srrs * U-stream ranges will be set when the cookie is unpacked. 390163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 391163953Srrs * supported) when the INIT-ACK arrives. 392163953Srrs */ 393163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 394168943Srrs asoc->strmin[i].delivery_started = 0; 395163953Srrs } 396163953Srrs /* 397163953Srrs * load_address_from_init will put the addresses into the 398163953Srrs * association when the COOKIE is processed or the INIT-ACK is 399163953Srrs * processed. Both types of COOKIE's existing and new call this 400163953Srrs * routine. It will remove addresses that are no longer in the 401163953Srrs * association (for the restarting case where addresses are 402163953Srrs * removed). Up front when the INIT arrives we will discard it if it 403163953Srrs * is a restart and new addresses have been added. 404163953Srrs */ 405169655Srrs /* sa_ignore MEMLEAK */ 406163953Srrs return (0); 407163953Srrs} 408163953Srrs 409163953Srrs/* 410163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 411163953Srrs */ 412163953Srrsstatic int 413163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 414163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 415170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 416163953Srrs{ 417163953Srrs struct sctp_association *asoc; 418163953Srrs struct mbuf *op_err; 419163953Srrs int retval, abort_flag; 420163953Srrs uint32_t initack_limit; 421185694Srrs int nat_friendly = 0; 422163953Srrs 423163953Srrs /* First verify that we have no illegal param's */ 424163953Srrs abort_flag = 0; 425163953Srrs op_err = NULL; 426163953Srrs 427163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 428163953Srrs (offset + sizeof(struct sctp_init_chunk)), 429185694Srrs &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 430163953Srrs if (abort_flag) { 431163953Srrs /* Send an abort and notify peer */ 432172090Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 433170056Srrs *abort_no_unlock = 1; 434163953Srrs return (-1); 435163953Srrs } 436163953Srrs asoc = &stcb->asoc; 437185694Srrs asoc->peer_supports_nat = (uint8_t) nat_friendly; 438163953Srrs /* process the peer's parameters in the INIT-ACK */ 439163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 440163953Srrs if (retval < 0) { 441163953Srrs return (retval); 442163953Srrs } 443163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 444163953Srrs /* load all addresses */ 445163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 446163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 447163953Srrs NULL))) { 448163953Srrs /* Huh, we should abort */ 449169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 450169420Srrs "Load addresses from INIT causes an abort %d\n", 451169420Srrs retval); 452163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 453179157Srrs NULL, 0, net->port); 454168299Srrs *abort_no_unlock = 1; 455163953Srrs return (-1); 456163953Srrs } 457171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 458171477Srrs if (asoc->peer_supports_asconf == 0) { 459171477Srrs struct sctp_asconf_addr *aparam; 460171477Srrs 461171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 462171477Srrs /* sa_ignore FREED_MEMORY */ 463171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 464171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 465171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 466171477Srrs } 467171477Srrs } 468163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 469163953Srrs stcb->asoc.local_hmacs); 470163953Srrs if (op_err) { 471163953Srrs sctp_queue_op_err(stcb, op_err); 472163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 473163953Srrs op_err = NULL; 474163953Srrs } 475163953Srrs /* extract the cookie and queue it to "echo" it back... */ 476179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 477171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 478171943Srrs stcb->asoc.overall_error_count, 479171943Srrs 0, 480171943Srrs SCTP_FROM_SCTP_INPUT, 481171943Srrs __LINE__); 482171943Srrs } 483163953Srrs stcb->asoc.overall_error_count = 0; 484163953Srrs net->error_count = 0; 485163953Srrs 486163953Srrs /* 487163953Srrs * Cancel the INIT timer, We do this first before queueing the 488163953Srrs * cookie. We always cancel at the primary to assue that we are 489163953Srrs * canceling the timer started by the INIT which always goes to the 490163953Srrs * primary. 491163953Srrs */ 492163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 493165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 494163953Srrs 495165220Srrs /* calculate the RTO */ 496171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 497165220Srrs 498163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 499163953Srrs if (retval < 0) { 500163953Srrs /* 501163953Srrs * No cookie, we probably should send a op error. But in any 502163953Srrs * case if there is no cookie in the INIT-ACK, we can 503163953Srrs * abandon the peer, its broke. 504163953Srrs */ 505163953Srrs if (retval == -3) { 506163953Srrs /* We abort with an error of missing mandatory param */ 507163953Srrs op_err = 508163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 509163953Srrs if (op_err) { 510163953Srrs /* 511163953Srrs * Expand beyond to include the mandatory 512163953Srrs * param cookie 513163953Srrs */ 514163953Srrs struct sctp_inv_mandatory_param *mp; 515163953Srrs 516165647Srrs SCTP_BUF_LEN(op_err) = 517163953Srrs sizeof(struct sctp_inv_mandatory_param); 518163953Srrs mp = mtod(op_err, 519163953Srrs struct sctp_inv_mandatory_param *); 520163953Srrs /* Subtract the reserved param */ 521163953Srrs mp->length = 522163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 523163953Srrs mp->num_param = htonl(1); 524163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 525163953Srrs mp->resv = 0; 526163953Srrs } 527163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 528179157Srrs sh, op_err, 0, net->port); 529168299Srrs *abort_no_unlock = 1; 530163953Srrs } 531163953Srrs return (retval); 532163953Srrs } 533163953Srrs return (0); 534163953Srrs} 535163953Srrs 536163953Srrsstatic void 537163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 538163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 539163953Srrs{ 540163953Srrs struct sockaddr_storage store; 541163953Srrs struct sockaddr_in *sin; 542163953Srrs struct sockaddr_in6 *sin6; 543163953Srrs struct sctp_nets *r_net; 544163953Srrs struct timeval tv; 545172396Srrs int req_prim = 0; 546163953Srrs 547163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 548163953Srrs /* Invalid length */ 549163953Srrs return; 550163953Srrs } 551163953Srrs sin = (struct sockaddr_in *)&store; 552163953Srrs sin6 = (struct sockaddr_in6 *)&store; 553163953Srrs 554163953Srrs memset(&store, 0, sizeof(store)); 555163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 556163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 557163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 558163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 559163953Srrs sin->sin_port = stcb->rport; 560163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 561163953Srrs sizeof(sin->sin_addr)); 562163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 563163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 564163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 565163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 566163953Srrs sin6->sin6_port = stcb->rport; 567163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 568163953Srrs sizeof(sin6->sin6_addr)); 569163953Srrs } else { 570163953Srrs return; 571163953Srrs } 572163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 573163953Srrs if (r_net == NULL) { 574169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 575163953Srrs return; 576163953Srrs } 577163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 578163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 579163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 580163953Srrs /* 581163953Srrs * If the its a HB and it's random value is correct when can 582163953Srrs * confirm the destination. 583163953Srrs */ 584163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 585167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 586167598Srrs stcb->asoc.primary_destination = r_net; 587167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 588167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 589167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 590167598Srrs if (r_net != stcb->asoc.primary_destination) { 591167598Srrs /* 592167598Srrs * first one on the list is NOT the primary 593167598Srrs * sctp_cmpaddr() is much more efficent if 594167598Srrs * the primary is the first on the list, 595167598Srrs * make it so. 596167598Srrs */ 597167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 598167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 599167598Srrs } 600172396Srrs req_prim = 1; 601167598Srrs } 602163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 603172090Srrs stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 604163953Srrs } 605163953Srrs r_net->error_count = 0; 606163953Srrs r_net->hb_responded = 1; 607163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 608163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 609163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 610163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 611163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 612163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 613172090Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 614163953Srrs /* now was it the primary? if so restore */ 615163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 616169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 617163953Srrs } 618163953Srrs } 619171440Srrs /* 620171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 621171440Srrs * set the destination to active state and set the cwnd to one or 622171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 623171440Srrs * timer is running, for the destination, stop the timer because a 624171440Srrs * PF-heartbeat was received. 625171440Srrs */ 626179783Srrs if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && 627179783Srrs SCTP_BASE_SYSCTL(sctp_cmt_pf) && 628179783Srrs (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) { 629171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 630171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 631171440Srrs stcb, net, 632171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 633171440Srrs } 634171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 635179783Srrs net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf); 636171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 637171440Srrs net, net->cwnd); 638171440Srrs } 639163953Srrs /* Now lets do a RTO with this */ 640171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 641172396Srrs /* Mobility adaptation */ 642172396Srrs if (req_prim) { 643172396Srrs if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 644172396Srrs SCTP_MOBILITY_BASE) || 645172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 646172396Srrs SCTP_MOBILITY_FASTHANDOFF)) && 647172396Srrs sctp_is_mobility_feature_on(stcb->sctp_ep, 648172396Srrs SCTP_MOBILITY_PRIM_DELETED)) { 649172396Srrs 650172396Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 651172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 652172396Srrs SCTP_MOBILITY_FASTHANDOFF)) { 653172396Srrs sctp_assoc_immediate_retrans(stcb, 654172396Srrs stcb->asoc.primary_destination); 655172396Srrs } 656172396Srrs if (sctp_is_mobility_feature_on(stcb->sctp_ep, 657172396Srrs SCTP_MOBILITY_BASE)) { 658172396Srrs sctp_move_chunks_from_deleted_prim(stcb, 659172396Srrs stcb->asoc.primary_destination); 660172396Srrs } 661172396Srrs sctp_delete_prim_timer(stcb->sctp_ep, stcb, 662172396Srrs stcb->asoc.deleted_primary); 663172396Srrs } 664172396Srrs } 665163953Srrs} 666163953Srrs 667185694Srrsstatic int 668185694Srrssctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 669185694Srrs{ 670185694Srrs /* 671185694Srrs * return 0 means we want you to proceed with the abort non-zero 672185694Srrs * means no abort processing 673185694Srrs */ 674185694Srrs struct sctpasochead *head; 675185694Srrs 676185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 677185694Srrs /* generate a new vtag and send init */ 678185694Srrs LIST_REMOVE(stcb, sctp_asocs); 679185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 680185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 681185694Srrs /* 682185694Srrs * put it in the bucket in the vtag hash of assoc's for the 683185694Srrs * system 684185694Srrs */ 685185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 686185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 687185694Srrs return (1); 688185694Srrs } 689185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 690185694Srrs /* 691185694Srrs * treat like a case where the cookie expired i.e.: - dump 692185694Srrs * current cookie. - generate a new vtag. - resend init. 693185694Srrs */ 694185694Srrs /* generate a new vtag and send init */ 695185694Srrs LIST_REMOVE(stcb, sctp_asocs); 696185694Srrs stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 697185694Srrs stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 698185694Srrs sctp_stop_all_cookie_timers(stcb); 699185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 700185694Srrs stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 701185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 702185694Srrs /* 703185694Srrs * put it in the bucket in the vtag hash of assoc's for the 704185694Srrs * system 705185694Srrs */ 706185694Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 707185694Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 708185694Srrs return (1); 709185694Srrs } 710185694Srrs return (0); 711185694Srrs} 712185694Srrs 713185694Srrsstatic int 714185694Srrssctp_handle_nat_missing_state(struct sctp_tcb *stcb, 715185694Srrs struct sctp_nets *net) 716185694Srrs{ 717185694Srrs /* 718185694Srrs * return 0 means we want you to proceed with the abort non-zero 719185694Srrs * means no abort processing 720185694Srrs */ 721185694Srrs if (stcb->asoc.peer_supports_auth == 0) { 722185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 723185694Srrs return (0); 724185694Srrs } 725185694Srrs sctp_asconf_send_nat_state_update(stcb, net); 726185694Srrs return (1); 727185694Srrs} 728185694Srrs 729185694Srrs 730163953Srrsstatic void 731163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 732163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 733163953Srrs{ 734172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 735172090Srrs struct socket *so; 736172090Srrs 737172090Srrs#endif 738185694Srrs uint16_t len; 739172090Srrs 740169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 741163953Srrs if (stcb == NULL) 742163953Srrs return; 743163953Srrs 744185694Srrs len = ntohs(cp->ch.chunk_length); 745185694Srrs if (len > sizeof(struct sctp_chunkhdr)) { 746185694Srrs /* 747185694Srrs * Need to check the cause codes for our two magic nat 748185694Srrs * aborts which don't kill the assoc necessarily. 749185694Srrs */ 750185694Srrs struct sctp_abort_chunk *cpnext; 751185694Srrs struct sctp_missing_nat_state *natc; 752185694Srrs uint16_t cause; 753185694Srrs 754185694Srrs cpnext = cp; 755185694Srrs cpnext++; 756185694Srrs natc = (struct sctp_missing_nat_state *)cpnext; 757185694Srrs cause = ntohs(natc->cause); 758185694Srrs if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 759185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 760185694Srrs cp->ch.chunk_flags); 761185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 762185694Srrs return; 763185694Srrs } 764185694Srrs } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 765185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 766185694Srrs cp->ch.chunk_flags); 767185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 768185694Srrs return; 769185694Srrs } 770185694Srrs } 771185694Srrs } 772163953Srrs /* stop any receive timers */ 773171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 774163953Srrs /* notify user of the abort and clean up... */ 775172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 776163953Srrs /* free the tcb */ 777172396Srrs#if defined(SCTP_PANIC_ON_ABORT) 778172396Srrs printf("stcb:%p state:%d rport:%d net:%p\n", 779172396Srrs stcb, stcb->asoc.state, stcb->rport, net); 780172396Srrs if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 781172396Srrs panic("Received an ABORT"); 782172396Srrs } else { 783172396Srrs printf("No panic its in state %x closed\n", stcb->asoc.state); 784172396Srrs } 785172396Srrs#endif 786163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 787163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 788163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 789163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 790163953Srrs } 791168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 792168859Srrs sctp_print_out_track_log(stcb); 793168859Srrs#endif 794172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 795172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 796172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 797172090Srrs SCTP_TCB_UNLOCK(stcb); 798172090Srrs SCTP_SOCKET_LOCK(so, 1); 799172090Srrs SCTP_TCB_LOCK(stcb); 800172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 801172090Srrs#endif 802171990Srrs stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 803171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 804171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 805172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 806172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 807172090Srrs#endif 808169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 809163953Srrs} 810163953Srrs 811163953Srrsstatic void 812163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 813163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 814163953Srrs{ 815163953Srrs struct sctp_association *asoc; 816163953Srrs int some_on_streamwheel; 817163953Srrs 818172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 819172090Srrs struct socket *so; 820172090Srrs 821172090Srrs#endif 822172090Srrs 823169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 824169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 825163953Srrs if (stcb == NULL) 826163953Srrs return; 827165220Srrs asoc = &stcb->asoc; 828165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 829165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 830163953Srrs return; 831163953Srrs } 832163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 833163953Srrs /* Shutdown NOT the expected size */ 834163953Srrs return; 835163953Srrs } else { 836163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 837199477Stuexen if (*abort_flag) { 838199477Stuexen return; 839199477Stuexen } 840163953Srrs } 841165220Srrs if (asoc->control_pdapi) { 842163953Srrs /* 843163953Srrs * With a normal shutdown we assume the end of last record. 844163953Srrs */ 845163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 846165220Srrs asoc->control_pdapi->end_added = 1; 847165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 848165220Srrs asoc->control_pdapi = NULL; 849163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 850172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 851172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 852172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 853172090Srrs SCTP_TCB_UNLOCK(stcb); 854172090Srrs SCTP_SOCKET_LOCK(so, 1); 855172090Srrs SCTP_TCB_LOCK(stcb); 856172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 857172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 858172090Srrs /* assoc was freed while we were unlocked */ 859172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 860172090Srrs return; 861172090Srrs } 862172090Srrs#endif 863163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 864172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 865172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 866172090Srrs#endif 867163953Srrs } 868163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 869163953Srrs if (stcb->sctp_socket) { 870163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 871170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 872163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 873171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 874172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 875163953Srrs /* 876163953Srrs * notify upper layer that peer has initiated a 877163953Srrs * shutdown 878163953Srrs */ 879172090Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 880163953Srrs 881163953Srrs /* reset time */ 882169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 883163953Srrs } 884163953Srrs } 885163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 886163953Srrs /* 887163953Srrs * stop the shutdown timer, since we WILL move to 888163953Srrs * SHUTDOWN-ACK-SENT. 889163953Srrs */ 890171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 891163953Srrs } 892171158Srrs /* Now is there unsent data on a stream somewhere? */ 893171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 894163953Srrs 895163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 896163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 897163953Srrs some_on_streamwheel) { 898163953Srrs /* By returning we will push more data out */ 899163953Srrs return; 900163953Srrs } else { 901163953Srrs /* no outstanding data to send, so move on... */ 902163953Srrs /* send SHUTDOWN-ACK */ 903163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 904163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 905166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 906166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 907163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 908163953Srrs } 909171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 910172703Srrs SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 911171990Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, 912171990Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 913163953Srrs /* start SHUTDOWN timer */ 914163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 915163953Srrs stcb, net); 916163953Srrs } 917163953Srrs} 918163953Srrs 919163953Srrsstatic void 920163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 921204141Stuexen struct sctp_tcb *stcb, 922204141Stuexen struct sctp_nets *net) 923163953Srrs{ 924163953Srrs struct sctp_association *asoc; 925163953Srrs 926172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 927172090Srrs struct socket *so; 928172090Srrs 929172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 930172090Srrs#endif 931169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 932169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 933163953Srrs if (stcb == NULL) 934163953Srrs return; 935163953Srrs 936163953Srrs asoc = &stcb->asoc; 937163953Srrs /* process according to association state */ 938204141Stuexen if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 939204141Stuexen (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 940204141Stuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 941204141Stuexen sctp_send_shutdown_complete(stcb, net, 1); 942204141Stuexen SCTP_TCB_UNLOCK(stcb); 943204141Stuexen return; 944204141Stuexen } 945163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 946163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 947163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 948163953Srrs SCTP_TCB_UNLOCK(stcb); 949163953Srrs return; 950163953Srrs } 951165220Srrs if (asoc->control_pdapi) { 952163953Srrs /* 953163953Srrs * With a normal shutdown we assume the end of last record. 954163953Srrs */ 955163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 956165220Srrs asoc->control_pdapi->end_added = 1; 957165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 958165220Srrs asoc->control_pdapi = NULL; 959163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 960172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 961172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 962172090Srrs SCTP_TCB_UNLOCK(stcb); 963172090Srrs SCTP_SOCKET_LOCK(so, 1); 964172090Srrs SCTP_TCB_LOCK(stcb); 965172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 966172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 967172090Srrs /* assoc was freed while we were unlocked */ 968172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 969172090Srrs return; 970172090Srrs } 971172090Srrs#endif 972163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 973172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 974172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 975172090Srrs#endif 976163953Srrs } 977163953Srrs /* are the queues empty? */ 978163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 979163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 980163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 981172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 982163953Srrs } 983163953Srrs /* stop the timer */ 984171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 985163953Srrs /* send SHUTDOWN-COMPLETE */ 986204141Stuexen sctp_send_shutdown_complete(stcb, net, 0); 987163953Srrs /* notify upper layer protocol */ 988163953Srrs if (stcb->sctp_socket) { 989172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 990163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 991163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 992163953Srrs /* Set the connected flag to disconnected */ 993163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 994163953Srrs } 995163953Srrs } 996163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 997163953Srrs /* free the TCB but first save off the ep */ 998172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 999172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1000172090Srrs SCTP_TCB_UNLOCK(stcb); 1001172090Srrs SCTP_SOCKET_LOCK(so, 1); 1002172090Srrs SCTP_TCB_LOCK(stcb); 1003172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1004172090Srrs#endif 1005171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1006171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1007172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1008172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1009172090Srrs#endif 1010163953Srrs} 1011163953Srrs 1012163953Srrs/* 1013163953Srrs * Skip past the param header and then we will find the chunk that caused the 1014163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1015163953Srrs * our peer must be broken. 1016163953Srrs */ 1017163953Srrsstatic void 1018163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1019163953Srrs struct sctp_nets *net) 1020163953Srrs{ 1021163953Srrs struct sctp_chunkhdr *chk; 1022163953Srrs 1023163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1024163953Srrs switch (chk->chunk_type) { 1025163953Srrs case SCTP_ASCONF_ACK: 1026163953Srrs case SCTP_ASCONF: 1027163953Srrs sctp_asconf_cleanup(stcb, net); 1028163953Srrs break; 1029163953Srrs case SCTP_FORWARD_CUM_TSN: 1030163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1031163953Srrs break; 1032163953Srrs default: 1033169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1034169420Srrs "Peer does not support chunk type %d(%x)??\n", 1035169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 1036163953Srrs break; 1037163953Srrs } 1038163953Srrs} 1039163953Srrs 1040163953Srrs/* 1041163953Srrs * Skip past the param header and then we will find the param that caused the 1042163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 1043163953Srrs * these will turn of specific features. 1044163953Srrs */ 1045163953Srrsstatic void 1046163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1047163953Srrs{ 1048163953Srrs struct sctp_paramhdr *pbad; 1049163953Srrs 1050163953Srrs pbad = phdr + 1; 1051163953Srrs switch (ntohs(pbad->param_type)) { 1052163953Srrs /* pr-sctp draft */ 1053163953Srrs case SCTP_PRSCTP_SUPPORTED: 1054163953Srrs stcb->asoc.peer_supports_prsctp = 0; 1055163953Srrs break; 1056163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 1057163953Srrs break; 1058163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 1059185694Srrs case SCTP_HAS_NAT_SUPPORT: 1060185694Srrs stcb->asoc.peer_supports_nat = 0; 1061185694Srrs break; 1062163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 1063163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 1064163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 1065163953Srrs stcb->asoc.ecn_allowed = 0; 1066163953Srrs break; 1067163953Srrs case SCTP_ADD_IP_ADDRESS: 1068163953Srrs case SCTP_DEL_IP_ADDRESS: 1069163953Srrs case SCTP_SET_PRIM_ADDR: 1070163953Srrs stcb->asoc.peer_supports_asconf = 0; 1071163953Srrs break; 1072163953Srrs case SCTP_SUCCESS_REPORT: 1073163953Srrs case SCTP_ERROR_CAUSE_IND: 1074169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1075169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1076169420Srrs "Turning off ASCONF to this strange peer\n"); 1077163953Srrs stcb->asoc.peer_supports_asconf = 0; 1078163953Srrs break; 1079163953Srrs default: 1080169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1081169420Srrs "Peer does not support param type %d(%x)??\n", 1082169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 1083163953Srrs break; 1084163953Srrs } 1085163953Srrs} 1086163953Srrs 1087163953Srrsstatic int 1088163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 1089163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 1090163953Srrs{ 1091163953Srrs int chklen; 1092163953Srrs struct sctp_paramhdr *phdr; 1093163953Srrs uint16_t error_type; 1094163953Srrs uint16_t error_len; 1095163953Srrs struct sctp_association *asoc; 1096163953Srrs int adjust; 1097163953Srrs 1098172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1099172090Srrs struct socket *so; 1100172090Srrs 1101172090Srrs#endif 1102172090Srrs 1103163953Srrs /* parse through all of the errors and process */ 1104163953Srrs asoc = &stcb->asoc; 1105163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1106163953Srrs sizeof(struct sctp_chunkhdr)); 1107163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1108163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1109163953Srrs /* Process an Error Cause */ 1110163953Srrs error_type = ntohs(phdr->param_type); 1111163953Srrs error_len = ntohs(phdr->param_length); 1112163953Srrs if ((error_len > chklen) || (error_len == 0)) { 1113163953Srrs /* invalid param length for this param */ 1114169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1115169420Srrs chklen, error_len); 1116163953Srrs return (0); 1117163953Srrs } 1118163953Srrs switch (error_type) { 1119163953Srrs case SCTP_CAUSE_INVALID_STREAM: 1120163953Srrs case SCTP_CAUSE_MISSING_PARAM: 1121163953Srrs case SCTP_CAUSE_INVALID_PARAM: 1122163953Srrs case SCTP_CAUSE_NO_USER_DATA: 1123169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1124169420Srrs error_type); 1125163953Srrs break; 1126185694Srrs case SCTP_CAUSE_NAT_COLLIDING_STATE: 1127185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1128185694Srrs ch->chunk_flags); 1129185694Srrs if (sctp_handle_nat_colliding_state(stcb)) { 1130185694Srrs return (0); 1131185694Srrs } 1132185694Srrs break; 1133185694Srrs case SCTP_CAUSE_NAT_MISSING_STATE: 1134185694Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1135185694Srrs ch->chunk_flags); 1136185694Srrs if (sctp_handle_nat_missing_state(stcb, net)) { 1137185694Srrs return (0); 1138185694Srrs } 1139185694Srrs break; 1140163953Srrs case SCTP_CAUSE_STALE_COOKIE: 1141163953Srrs /* 1142163953Srrs * We only act if we have echoed a cookie and are 1143163953Srrs * waiting. 1144163953Srrs */ 1145163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1146163953Srrs int *p; 1147163953Srrs 1148163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1149163953Srrs /* Save the time doubled */ 1150163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 1151163953Srrs asoc->stale_cookie_count++; 1152163953Srrs if (asoc->stale_cookie_count > 1153163953Srrs asoc->max_init_times) { 1154172090Srrs sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1155163953Srrs /* now free the asoc */ 1156172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1157172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1158172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1159172090Srrs SCTP_TCB_UNLOCK(stcb); 1160172090Srrs SCTP_SOCKET_LOCK(so, 1); 1161172090Srrs SCTP_TCB_LOCK(stcb); 1162172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 1163172090Srrs#endif 1164171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1165171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1166172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1167172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1168172090Srrs#endif 1169163953Srrs return (-1); 1170163953Srrs } 1171163953Srrs /* blast back to INIT state */ 1172185694Srrs sctp_toss_old_cookies(stcb, &stcb->asoc); 1173163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1174163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 1175163953Srrs sctp_stop_all_cookie_timers(stcb); 1176172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1177163953Srrs } 1178163953Srrs break; 1179163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1180163953Srrs /* 1181163953Srrs * Nothing we can do here, we don't do hostname 1182163953Srrs * addresses so if the peer does not like my IPv6 1183163953Srrs * (or IPv4 for that matter) it does not matter. If 1184163953Srrs * they don't support that type of address, they can 1185163953Srrs * NOT possibly get that packet type... i.e. with no 1186163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 1187163953Srrs * safely ignore this one. If we ever added support 1188163953Srrs * for HOSTNAME Addresses, then we would need to do 1189163953Srrs * something here. 1190163953Srrs */ 1191163953Srrs break; 1192163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 1193163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 1194163953Srrs break; 1195163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 1196163953Srrs sctp_process_unrecog_param(stcb, phdr); 1197163953Srrs break; 1198163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1199163953Srrs /* 1200163953Srrs * We ignore this since the timer will drive out a 1201163953Srrs * new cookie anyway and there timer will drive us 1202163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 1203163953Srrs * here since we don't have their tag. 1204163953Srrs */ 1205163953Srrs break; 1206163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 1207163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 1208163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 1209163953Srrs /* 1210163953Srrs * We should NOT get these here, but in a 1211166086Srrs * ASCONF-ACK. 1212163953Srrs */ 1213169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1214169420Srrs error_type); 1215163953Srrs break; 1216163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 1217163953Srrs /* 1218163953Srrs * And what, pray tell do we do with the fact that 1219163953Srrs * the peer is out of resources? Not really sure we 1220166086Srrs * could do anything but abort. I suspect this 1221163953Srrs * should have came WITH an abort instead of in a 1222163953Srrs * OP-ERROR. 1223163953Srrs */ 1224163953Srrs break; 1225163953Srrs default: 1226169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1227169420Srrs error_type); 1228163953Srrs break; 1229163953Srrs } 1230163953Srrs adjust = SCTP_SIZE32(error_len); 1231163953Srrs chklen -= adjust; 1232163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1233163953Srrs } 1234163953Srrs return (0); 1235163953Srrs} 1236163953Srrs 1237163953Srrsstatic int 1238169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1239169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1240170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1241163953Srrs{ 1242163953Srrs struct sctp_init_ack *init_ack; 1243163953Srrs struct mbuf *op_err; 1244163953Srrs 1245169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1246169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 1247169420Srrs 1248163953Srrs if (stcb == NULL) { 1249169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1250169420Srrs "sctp_handle_init_ack: TCB is null\n"); 1251163953Srrs return (-1); 1252163953Srrs } 1253163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1254163953Srrs /* Invalid length */ 1255163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1256163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1257179157Srrs op_err, 0, net->port); 1258168299Srrs *abort_no_unlock = 1; 1259163953Srrs return (-1); 1260163953Srrs } 1261163953Srrs init_ack = &cp->init; 1262163953Srrs /* validate parameters */ 1263163953Srrs if (init_ack->initiate_tag == 0) { 1264163953Srrs /* protocol error... send an abort */ 1265163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1266163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1267179157Srrs op_err, 0, net->port); 1268168299Srrs *abort_no_unlock = 1; 1269163953Srrs return (-1); 1270163953Srrs } 1271163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1272163953Srrs /* protocol error... send an abort */ 1273163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1274163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1275179157Srrs op_err, 0, net->port); 1276168299Srrs *abort_no_unlock = 1; 1277163953Srrs return (-1); 1278163953Srrs } 1279163953Srrs if (init_ack->num_inbound_streams == 0) { 1280163953Srrs /* protocol error... send an abort */ 1281163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1282163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1283179157Srrs op_err, 0, net->port); 1284168299Srrs *abort_no_unlock = 1; 1285163953Srrs return (-1); 1286163953Srrs } 1287163953Srrs if (init_ack->num_outbound_streams == 0) { 1288163953Srrs /* protocol error... send an abort */ 1289163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1290163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1291179157Srrs op_err, 0, net->port); 1292168299Srrs *abort_no_unlock = 1; 1293163953Srrs return (-1); 1294163953Srrs } 1295163953Srrs /* process according to association state... */ 1296171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1297163953Srrs case SCTP_STATE_COOKIE_WAIT: 1298163953Srrs /* this is the expected state for this chunk */ 1299163953Srrs /* process the INIT-ACK parameters */ 1300163953Srrs if (stcb->asoc.primary_destination->dest_state & 1301163953Srrs SCTP_ADDR_UNCONFIRMED) { 1302163953Srrs /* 1303163953Srrs * The primary is where we sent the INIT, we can 1304163953Srrs * always consider it confirmed when the INIT-ACK is 1305163953Srrs * returned. Do this before we load addresses 1306163953Srrs * though. 1307163953Srrs */ 1308163953Srrs stcb->asoc.primary_destination->dest_state &= 1309163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1310163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1311172090Srrs stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1312163953Srrs } 1313169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1314170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1315163953Srrs /* error in parsing parameters */ 1316163953Srrs return (-1); 1317163953Srrs } 1318163953Srrs /* update our state */ 1319169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1320171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1321163953Srrs 1322163953Srrs /* reset the RTO calc */ 1323179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1324171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1325171943Srrs stcb->asoc.overall_error_count, 1326171943Srrs 0, 1327171943Srrs SCTP_FROM_SCTP_INPUT, 1328171943Srrs __LINE__); 1329171943Srrs } 1330163953Srrs stcb->asoc.overall_error_count = 0; 1331169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1332163953Srrs /* 1333163953Srrs * collapse the init timer back in case of a exponential 1334165220Srrs * backoff 1335163953Srrs */ 1336163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1337163953Srrs stcb, net); 1338163953Srrs /* 1339163953Srrs * the send at the end of the inbound data processing will 1340163953Srrs * cause the cookie to be sent 1341163953Srrs */ 1342163953Srrs break; 1343163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1344163953Srrs /* incorrect state... discard */ 1345163953Srrs break; 1346163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1347163953Srrs /* incorrect state... discard */ 1348163953Srrs break; 1349163953Srrs case SCTP_STATE_OPEN: 1350163953Srrs /* incorrect state... discard */ 1351163953Srrs break; 1352163953Srrs case SCTP_STATE_EMPTY: 1353163953Srrs case SCTP_STATE_INUSE: 1354163953Srrs default: 1355163953Srrs /* incorrect state... discard */ 1356163953Srrs return (-1); 1357163953Srrs break; 1358163953Srrs } 1359169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1360163953Srrs return (0); 1361163953Srrs} 1362163953Srrs 1363185694Srrsstatic struct sctp_tcb * 1364185694Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1365185694Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1366185694Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1367185694Srrs struct sockaddr *init_src, int *notification, 1368185694Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1369185694Srrs uint32_t vrf_id, uint16_t port); 1370163953Srrs 1371185694Srrs 1372163953Srrs/* 1373163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1374163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1375163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1376163953Srrs * mbuf to the cookie-echo chunk 1377163953Srrs */ 1378163953Srrsstatic struct sctp_tcb * 1379163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1380163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1381185694Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1382169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1383185694Srrs uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1384163953Srrs{ 1385163953Srrs struct sctp_association *asoc; 1386163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1387163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1388185694Srrs struct sctp_nets *net; 1389185694Srrs struct mbuf *op_err; 1390185694Srrs struct sctp_paramhdr *ph; 1391163953Srrs int chk_length; 1392165220Srrs int init_offset, initack_offset, i; 1393163953Srrs int retval; 1394164205Srrs int spec_flag = 0; 1395170140Srrs uint32_t how_indx; 1396163953Srrs 1397185694Srrs net = *netp; 1398163953Srrs /* I know that the TCB is non-NULL from the caller */ 1399163953Srrs asoc = &stcb->asoc; 1400166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1401166023Srrs if (asoc->cookie_how[how_indx] == 0) 1402166023Srrs break; 1403166023Srrs } 1404166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1405166023Srrs asoc->cookie_how[how_indx] = 1; 1406166023Srrs } 1407163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1408163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1409163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1410163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1411165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1412163953Srrs if (op_err == NULL) { 1413163953Srrs /* FOOBAR */ 1414163953Srrs return (NULL); 1415163953Srrs } 1416163953Srrs /* Set the len */ 1417165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1418163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1419163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1420163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1421169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1422179157Srrs vrf_id, net->port); 1423166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1424166023Srrs asoc->cookie_how[how_indx] = 2; 1425163953Srrs return (NULL); 1426163953Srrs } 1427163953Srrs /* 1428163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1429163953Srrs * INIT should start after the cookie-echo header struct (chunk 1430163953Srrs * header, state cookie header struct) 1431163953Srrs */ 1432163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1433163953Srrs 1434163953Srrs init_cp = (struct sctp_init_chunk *) 1435163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1436163953Srrs (uint8_t *) & init_buf); 1437163953Srrs if (init_cp == NULL) { 1438163953Srrs /* could not pull a INIT chunk in cookie */ 1439163953Srrs return (NULL); 1440163953Srrs } 1441163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1442163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1443163953Srrs return (NULL); 1444163953Srrs } 1445163953Srrs /* 1446163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1447163953Srrs * INIT-ACK follows the INIT chunk 1448163953Srrs */ 1449163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1450163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1451163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1452163953Srrs (uint8_t *) & initack_buf); 1453163953Srrs if (initack_cp == NULL) { 1454163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1455163953Srrs return (NULL); 1456163953Srrs } 1457163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1458163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1459163953Srrs return (NULL); 1460163953Srrs } 1461163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1462163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1463163953Srrs /* 1464163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1465163953Srrs * to get into the OPEN state 1466163953Srrs */ 1467166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1468172091Srrs /*- 1469172091Srrs * Opps, this means that we somehow generated two vtag's 1470172091Srrs * the same. I.e. we did: 1471172091Srrs * Us Peer 1472172091Srrs * <---INIT(tag=a)------ 1473172091Srrs * ----INIT-ACK(tag=t)--> 1474172091Srrs * ----INIT(tag=t)------> *1 1475172091Srrs * <---INIT-ACK(tag=a)--- 1476172091Srrs * <----CE(tag=t)------------- *2 1477172091Srrs * 1478172091Srrs * At point *1 we should be generating a different 1479172091Srrs * tag t'. Which means we would throw away the CE and send 1480172091Srrs * ours instead. Basically this is case C (throw away side). 1481172091Srrs */ 1482172091Srrs if (how_indx < sizeof(asoc->cookie_how)) 1483172091Srrs asoc->cookie_how[how_indx] = 17; 1484172091Srrs return (NULL); 1485172091Srrs 1486166023Srrs } 1487163953Srrs switch SCTP_GET_STATE 1488163953Srrs (asoc) { 1489163953Srrs case SCTP_STATE_COOKIE_WAIT: 1490166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1491163953Srrs /* 1492169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1493166023Srrs * correct tags... just accept it...but we must 1494166023Srrs * process the init so that we can make sure we have 1495166023Srrs * the right seq no's. 1496163953Srrs */ 1497163953Srrs /* First we must process the INIT !! */ 1498163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1499163953Srrs if (retval < 0) { 1500166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1501166023Srrs asoc->cookie_how[how_indx] = 3; 1502163953Srrs return (NULL); 1503163953Srrs } 1504163953Srrs /* we have already processed the INIT so no problem */ 1505163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1506171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1507171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1508163953Srrs /* update current state */ 1509166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1510166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1511166675Srrs else 1512166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1513171943Srrs 1514171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1515163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1516163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1517163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1518163953Srrs } 1519166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1520165220Srrs sctp_stop_all_cookie_timers(stcb); 1521163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1522163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1523163953Srrs (inp->sctp_socket->so_qlimit == 0) 1524163953Srrs ) { 1525172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1526172090Srrs struct socket *so; 1527172090Srrs 1528172090Srrs#endif 1529163953Srrs /* 1530163953Srrs * Here is where collision would go if we 1531163953Srrs * did a connect() and instead got a 1532163953Srrs * init/init-ack/cookie done before the 1533163953Srrs * init-ack came back.. 1534163953Srrs */ 1535163953Srrs stcb->sctp_ep->sctp_flags |= 1536163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1537172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1538172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1539172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1540172090Srrs SCTP_TCB_UNLOCK(stcb); 1541172090Srrs SCTP_SOCKET_LOCK(so, 1); 1542172090Srrs SCTP_TCB_LOCK(stcb); 1543172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1544172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1545172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1546172090Srrs return (NULL); 1547172090Srrs } 1548172090Srrs#endif 1549172090Srrs soisconnected(stcb->sctp_socket); 1550172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1551172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1552172090Srrs#endif 1553163953Srrs } 1554163953Srrs /* notify upper layer */ 1555163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1556163953Srrs /* 1557163953Srrs * since we did not send a HB make sure we don't 1558163953Srrs * double things 1559163953Srrs */ 1560163953Srrs net->hb_responded = 1; 1561170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1562171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1563163953Srrs 1564163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1565163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1566163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1567163953Srrs inp, stcb, NULL); 1568163953Srrs } 1569163953Srrs break; 1570163953Srrs default: 1571163953Srrs /* 1572163953Srrs * we're in the OPEN state (or beyond), so peer must 1573163953Srrs * have simply lost the COOKIE-ACK 1574163953Srrs */ 1575163953Srrs break; 1576163953Srrs } /* end switch */ 1577165220Srrs sctp_stop_all_cookie_timers(stcb); 1578163953Srrs /* 1579163953Srrs * We ignore the return code here.. not sure if we should 1580163953Srrs * somehow abort.. but we do have an existing asoc. This 1581163953Srrs * really should not fail. 1582163953Srrs */ 1583163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1584163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1585163953Srrs initack_offset, sh, init_src)) { 1586166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1587166023Srrs asoc->cookie_how[how_indx] = 4; 1588163953Srrs return (NULL); 1589163953Srrs } 1590163953Srrs /* respond with a COOKIE-ACK */ 1591165220Srrs sctp_toss_old_cookies(stcb, asoc); 1592163953Srrs sctp_send_cookie_ack(stcb); 1593166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1594166023Srrs asoc->cookie_how[how_indx] = 5; 1595163953Srrs return (stcb); 1596169352Srrs } 1597163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1598163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1599163953Srrs cookie->tie_tag_my_vtag == 0 && 1600163953Srrs cookie->tie_tag_peer_vtag == 0) { 1601163953Srrs /* 1602163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1603163953Srrs */ 1604166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1605166023Srrs asoc->cookie_how[how_indx] = 6; 1606163953Srrs return (NULL); 1607163953Srrs } 1608185694Srrs /* 1609185694Srrs * If nat support, and the below and stcb is established, send back 1610185694Srrs * a ABORT(colliding state) if we are established. 1611185694Srrs */ 1612185694Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1613185694Srrs (asoc->peer_supports_nat) && 1614185694Srrs ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1615185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1616185694Srrs (asoc->peer_vtag == 0)))) { 1617163953Srrs /* 1618185694Srrs * Special case - Peer's support nat. We may have two init's 1619185694Srrs * that we gave out the same tag on since one was not 1620185694Srrs * established.. i.e. we get INIT from host-1 behind the nat 1621185694Srrs * and we respond tag-a, we get a INIT from host-2 behind 1622185694Srrs * the nat and we get tag-a again. Then we bring up host-1 1623185694Srrs * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1624185694Srrs * Now we have colliding state. We must send an abort here 1625185694Srrs * with colliding state indication. 1626185694Srrs */ 1627185694Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1628185694Srrs 0, M_DONTWAIT, 1, MT_DATA); 1629185694Srrs if (op_err == NULL) { 1630185694Srrs /* FOOBAR */ 1631185694Srrs return (NULL); 1632185694Srrs } 1633185694Srrs /* pre-reserve some space */ 1634185694Srrs#ifdef INET6 1635185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1636185694Srrs#else 1637185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1638185694Srrs#endif 1639185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1640185694Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1641185694Srrs /* Set the len */ 1642185694Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1643185694Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1644185694Srrs ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1645185694Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1646185694Srrs sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1647185694Srrs return (NULL); 1648185694Srrs } 1649185694Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1650185694Srrs ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1651185694Srrs (asoc->peer_vtag == 0))) { 1652185694Srrs /* 1653163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1654163953Srrs * should be ok, re-accept peer info 1655163953Srrs */ 1656166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1657166023Srrs /* 1658166023Srrs * Extension of case C. If we hit this, then the 1659166023Srrs * random number generator returned the same vtag 1660166023Srrs * when we first sent our INIT-ACK and when we later 1661166023Srrs * sent our INIT. The side with the seq numbers that 1662166023Srrs * are different will be the one that normnally 1663166023Srrs * would have hit case C. This in effect "extends" 1664166023Srrs * our vtags in this collision case to be 64 bits. 1665166023Srrs * The same collision could occur aka you get both 1666166023Srrs * vtag and seq number the same twice in a row.. but 1667166023Srrs * is much less likely. If it did happen then we 1668166023Srrs * would proceed through and bring up the assoc.. we 1669166023Srrs * may end up with the wrong stream setup however.. 1670166023Srrs * which would be bad.. but there is no way to 1671166023Srrs * tell.. until we send on a stream that does not 1672166023Srrs * exist :-) 1673166023Srrs */ 1674166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1675166023Srrs asoc->cookie_how[how_indx] = 7; 1676166023Srrs 1677166023Srrs return (NULL); 1678166023Srrs } 1679166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1680166023Srrs asoc->cookie_how[how_indx] = 8; 1681171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1682163953Srrs sctp_stop_all_cookie_timers(stcb); 1683163953Srrs /* 1684163953Srrs * since we did not send a HB make sure we don't double 1685163953Srrs * things 1686163953Srrs */ 1687163953Srrs net->hb_responded = 1; 1688163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1689163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1690163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1691163953Srrs NULL); 1692163953Srrs } 1693163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1694164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1695164205Srrs 1696164205Srrs /* Note last_cwr_tsn? where is this used? */ 1697163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1698164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1699164205Srrs /* 1700164205Srrs * Ok the peer probably discarded our data (if we 1701164205Srrs * echoed a cookie+data). So anything on the 1702164205Srrs * sent_queue should be marked for retransmit, we 1703164205Srrs * may not get something to kick us so it COULD 1704164205Srrs * still take a timeout to move these.. but it can't 1705164205Srrs * hurt to mark them. 1706164205Srrs */ 1707164205Srrs struct sctp_tmit_chunk *chk; 1708163953Srrs 1709164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1710164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1711164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1712171440Srrs sctp_flight_size_decrease(chk); 1713171440Srrs sctp_total_flight_decrease(stcb, chk); 1714168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1715164205Srrs spec_flag++; 1716164205Srrs } 1717164205Srrs } 1718164205Srrs 1719164205Srrs } 1720163953Srrs /* process the INIT info (peer's info) */ 1721163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1722163953Srrs if (retval < 0) { 1723166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1724166023Srrs asoc->cookie_how[how_indx] = 9; 1725163953Srrs return (NULL); 1726163953Srrs } 1727163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1728163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1729163953Srrs initack_offset, sh, init_src)) { 1730166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1731166023Srrs asoc->cookie_how[how_indx] = 10; 1732163953Srrs return (NULL); 1733163953Srrs } 1734163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1735163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1736163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1737163953Srrs 1738163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1739163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1740163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1741172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1742172090Srrs struct socket *so; 1743172090Srrs 1744172090Srrs#endif 1745163953Srrs stcb->sctp_ep->sctp_flags |= 1746163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1747172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1748172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 1749172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1750172090Srrs SCTP_TCB_UNLOCK(stcb); 1751172090Srrs SCTP_SOCKET_LOCK(so, 1); 1752172090Srrs SCTP_TCB_LOCK(stcb); 1753172090Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1754172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1755172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1756172090Srrs return (NULL); 1757172090Srrs } 1758172090Srrs#endif 1759172090Srrs soisconnected(stcb->sctp_socket); 1760172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1761172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 1762172090Srrs#endif 1763163953Srrs } 1764166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1765166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1766166675Srrs else 1767166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1768166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1769166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1770166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1771166675Srrs } else { 1772166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1773163953Srrs } 1774171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1775163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1776163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1777163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1778163953Srrs } 1779163953Srrs sctp_stop_all_cookie_timers(stcb); 1780165220Srrs sctp_toss_old_cookies(stcb, asoc); 1781163953Srrs sctp_send_cookie_ack(stcb); 1782164205Srrs if (spec_flag) { 1783164205Srrs /* 1784164205Srrs * only if we have retrans set do we do this. What 1785164205Srrs * this call does is get only the COOKIE-ACK out and 1786164205Srrs * then when we return the normal call to 1787164205Srrs * sctp_chunk_output will get the retrans out behind 1788164205Srrs * this. 1789164205Srrs */ 1790172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1791164205Srrs } 1792166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1793166023Srrs asoc->cookie_how[how_indx] = 11; 1794166023Srrs 1795163953Srrs return (stcb); 1796163953Srrs } 1797163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1798163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1799163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1800163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1801163953Srrs cookie->tie_tag_peer_vtag != 0) { 1802163953Srrs struct sctpasochead *head; 1803163953Srrs 1804185694Srrs if (asoc->peer_supports_nat) { 1805185694Srrs /* 1806185694Srrs * This is a gross gross hack. just call the 1807185694Srrs * cookie_new code since we are allowing a duplicate 1808185694Srrs * association. I hope this works... 1809185694Srrs */ 1810185694Srrs return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1811185694Srrs inp, netp, init_src, notification, 1812185694Srrs auth_skipped, auth_offset, auth_len, 1813185694Srrs vrf_id, port)); 1814185694Srrs } 1815163953Srrs /* 1816163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1817163953Srrs */ 1818165220Srrs /* temp code */ 1819166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1820166023Srrs asoc->cookie_how[how_indx] = 12; 1821171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1822171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1823165647Srrs 1824163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1825163953Srrs /* notify upper layer */ 1826163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1827165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1828166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1829166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1830166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1831166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1832166675Srrs } 1833166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1834166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1835166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1836166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1837166675Srrs } 1838165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1839171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1840165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1841165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1842165647Srrs 1843165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1844165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1845171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1846165647Srrs } 1847165647Srrs asoc->pre_open_streams = 1848165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1849165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1850165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1851179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1852165647Srrs 1853165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1854165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1855165647Srrs 1856165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1857165647Srrs 1858165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1859170138Srrs if (asoc->mapping_array) { 1860165647Srrs memset(asoc->mapping_array, 0, 1861165647Srrs asoc->mapping_array_size); 1862170138Srrs } 1863205627Srrs if (asoc->nr_mapping_array) { 1864185694Srrs memset(asoc->nr_mapping_array, 0, 1865185694Srrs asoc->nr_mapping_array_size); 1866185694Srrs } 1867165220Srrs SCTP_TCB_UNLOCK(stcb); 1868165220Srrs SCTP_INP_INFO_WLOCK(); 1869165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1870165220Srrs SCTP_TCB_LOCK(stcb); 1871165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1872163953Srrs /* send up all the data */ 1873164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1874165220Srrs 1875172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1876165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1877165220Srrs stcb->asoc.strmout[i].stream_no = i; 1878165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1879165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1880165220Srrs } 1881163953Srrs /* process the INIT-ACK info (my info) */ 1882163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1883163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1884163953Srrs 1885163953Srrs /* pull from vtag hash */ 1886163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1887163953Srrs /* re-insert to new vtag position */ 1888179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1889179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1890163953Srrs /* 1891163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1892163953Srrs * system 1893163953Srrs */ 1894163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1895163953Srrs 1896163953Srrs /* process the INIT info (peer's info) */ 1897164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1898165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1899165220Srrs SCTP_INP_INFO_WUNLOCK(); 1900165220Srrs 1901163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1902163953Srrs if (retval < 0) { 1903166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1904166023Srrs asoc->cookie_how[how_indx] = 13; 1905166023Srrs 1906163953Srrs return (NULL); 1907163953Srrs } 1908163953Srrs /* 1909163953Srrs * since we did not send a HB make sure we don't double 1910163953Srrs * things 1911163953Srrs */ 1912163953Srrs net->hb_responded = 1; 1913163953Srrs 1914163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1915163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1916163953Srrs initack_offset, sh, init_src)) { 1917166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1918166023Srrs asoc->cookie_how[how_indx] = 14; 1919166023Srrs 1920163953Srrs return (NULL); 1921163953Srrs } 1922163953Srrs /* respond with a COOKIE-ACK */ 1923163953Srrs sctp_stop_all_cookie_timers(stcb); 1924165220Srrs sctp_toss_old_cookies(stcb, asoc); 1925163953Srrs sctp_send_cookie_ack(stcb); 1926166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1927166023Srrs asoc->cookie_how[how_indx] = 15; 1928163953Srrs 1929163953Srrs return (stcb); 1930163953Srrs } 1931166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1932166023Srrs asoc->cookie_how[how_indx] = 16; 1933163953Srrs /* all other cases... */ 1934163953Srrs return (NULL); 1935163953Srrs} 1936163953Srrs 1937166086Srrs 1938163953Srrs/* 1939163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1940163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1941163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1942163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1943163953Srrs * was from returns a new TCB 1944163953Srrs */ 1945185694Srrsstruct sctp_tcb * 1946163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1947163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1948163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1949163953Srrs struct sockaddr *init_src, int *notification, 1950169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1951179157Srrs uint32_t vrf_id, uint16_t port) 1952163953Srrs{ 1953163953Srrs struct sctp_tcb *stcb; 1954163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1955163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1956163953Srrs struct sockaddr_storage sa_store; 1957163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1958163953Srrs struct sockaddr_in *sin; 1959163953Srrs struct sockaddr_in6 *sin6; 1960163953Srrs struct sctp_association *asoc; 1961163953Srrs int chk_length; 1962163953Srrs int init_offset, initack_offset, initack_limit; 1963163953Srrs int retval; 1964163953Srrs int error = 0; 1965163953Srrs uint32_t old_tag; 1966166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1967163953Srrs 1968172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1969172090Srrs struct socket *so; 1970172090Srrs 1971172090Srrs so = SCTP_INP_SO(inp); 1972172090Srrs#endif 1973172090Srrs 1974163953Srrs /* 1975163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1976163953Srrs * INIT should start after the cookie-echo header struct (chunk 1977163953Srrs * header, state cookie header struct) 1978163953Srrs */ 1979163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1980163953Srrs init_cp = (struct sctp_init_chunk *) 1981163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1982163953Srrs (uint8_t *) & init_buf); 1983163953Srrs if (init_cp == NULL) { 1984163953Srrs /* could not pull a INIT chunk in cookie */ 1985169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1986169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1987163953Srrs return (NULL); 1988163953Srrs } 1989163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1990163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1991169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1992163953Srrs return (NULL); 1993163953Srrs } 1994163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1995163953Srrs /* 1996163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1997163953Srrs * INIT-ACK follows the INIT chunk 1998163953Srrs */ 1999163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2000163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2001163953Srrs (uint8_t *) & initack_buf); 2002163953Srrs if (initack_cp == NULL) { 2003163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2004169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2005163953Srrs return (NULL); 2006163953Srrs } 2007163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 2008163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2009163953Srrs return (NULL); 2010163953Srrs } 2011163953Srrs /* 2012163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2013163953Srrs * "initack_limit" value. This is because the chk_length field 2014163953Srrs * includes the length of the cookie, but the cookie is omitted when 2015163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2016163953Srrs */ 2017163953Srrs initack_limit = offset + cookie_len; 2018163953Srrs 2019163953Srrs /* 2020163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2021163953Srrs * and popluate 2022163953Srrs */ 2023171531Srrs 2024171531Srrs /* 2025171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2026171531Srrs * We do this since in effect we only use the p argument when the 2027171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2028171531Srrs * getting a cookie, we cannot be unbound. 2029171531Srrs */ 2030163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 2031171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2032171531Srrs (struct thread *)NULL 2033171531Srrs ); 2034163953Srrs if (stcb == NULL) { 2035163953Srrs struct mbuf *op_err; 2036163953Srrs 2037163953Srrs /* memory problem? */ 2038169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2039169420Srrs "process_cookie_new: no room for another TCB!\n"); 2040163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2041170140Srrs 2042163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2043179157Srrs sh, op_err, vrf_id, port); 2044163953Srrs return (NULL); 2045163953Srrs } 2046163953Srrs /* get the correct sctp_nets */ 2047169420Srrs if (netp) 2048169420Srrs *netp = sctp_findnet(stcb, init_src); 2049169420Srrs 2050163953Srrs asoc = &stcb->asoc; 2051163953Srrs /* get scope variables out of cookie */ 2052163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2053163953Srrs asoc->site_scope = cookie->site_scope; 2054163953Srrs asoc->local_scope = cookie->local_scope; 2055163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2056163953Srrs 2057163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2058163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2059163953Srrs struct mbuf *op_err; 2060163953Srrs 2061163953Srrs /* 2062163953Srrs * Houston we have a problem. The EP changed while the 2063163953Srrs * cookie was in flight. Only recourse is to abort the 2064163953Srrs * association. 2065163953Srrs */ 2066170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2067163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2068163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2069179157Srrs sh, op_err, vrf_id, port); 2070172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2071172090Srrs SCTP_TCB_UNLOCK(stcb); 2072172090Srrs SCTP_SOCKET_LOCK(so, 1); 2073172090Srrs SCTP_TCB_LOCK(stcb); 2074172090Srrs#endif 2075171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2076171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2077172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2078172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2079172090Srrs#endif 2080172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2081163953Srrs return (NULL); 2082163953Srrs } 2083163953Srrs /* process the INIT-ACK info (my info) */ 2084163953Srrs old_tag = asoc->my_vtag; 2085185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2086163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2087163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2088163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2089163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2090179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2091163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 2092163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2093163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2094163953Srrs 2095163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2096163953Srrs 2097163953Srrs /* process the INIT info (peer's info) */ 2098169420Srrs if (netp) 2099169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 2100169420Srrs else 2101169420Srrs retval = 0; 2102163953Srrs if (retval < 0) { 2103170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2104172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2105172090Srrs SCTP_TCB_UNLOCK(stcb); 2106172090Srrs SCTP_SOCKET_LOCK(so, 1); 2107172090Srrs SCTP_TCB_LOCK(stcb); 2108172090Srrs#endif 2109171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2110172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2111172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2112172090Srrs#endif 2113172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2114163953Srrs return (NULL); 2115163953Srrs } 2116163953Srrs /* load all addresses */ 2117163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 2118163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2119163953Srrs init_src)) { 2120170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2121172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2122172090Srrs SCTP_TCB_UNLOCK(stcb); 2123172090Srrs SCTP_SOCKET_LOCK(so, 1); 2124172090Srrs SCTP_TCB_LOCK(stcb); 2125172090Srrs#endif 2126171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2127172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2128172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2129172090Srrs#endif 2130172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2131163953Srrs return (NULL); 2132163953Srrs } 2133163953Srrs /* 2134163953Srrs * verify any preceding AUTH chunk that was skipped 2135163953Srrs */ 2136163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2137163953Srrs sctp_auth_get_cookie_params(stcb, m, 2138163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2139163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2140163953Srrs if (auth_skipped) { 2141163953Srrs struct sctp_auth_chunk *auth; 2142163953Srrs 2143163953Srrs auth = (struct sctp_auth_chunk *) 2144166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2145169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2146163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2147169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2148169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2149171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2150172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2151172090Srrs SCTP_TCB_UNLOCK(stcb); 2152172090Srrs SCTP_SOCKET_LOCK(so, 1); 2153172090Srrs SCTP_TCB_LOCK(stcb); 2154172090Srrs#endif 2155171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2156172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2157172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2158172090Srrs#endif 2159172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2160163953Srrs return (NULL); 2161163953Srrs } else { 2162163953Srrs /* remaining chunks checked... good to go */ 2163163953Srrs stcb->asoc.authenticated = 1; 2164163953Srrs } 2165163953Srrs } 2166163953Srrs /* update current state */ 2167169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2168171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2169163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2170163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2171163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2172163953Srrs } 2173165220Srrs sctp_stop_all_cookie_timers(stcb); 2174163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2175163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2176163953Srrs 2177163953Srrs /* 2178163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2179163953Srrs * addresses that need to get added to the peer (eg. addresses 2180163953Srrs * changed while cookie echo in flight). This needs to be done 2181163953Srrs * after we go to the OPEN state to do the correct asconf 2182163953Srrs * processing. else, make sure we have the correct addresses in our 2183163953Srrs * lists 2184163953Srrs */ 2185163953Srrs 2186163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2187163953Srrs /* pull in local_address (our "from" address) */ 2188163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 2189163953Srrs /* source addr is IPv4 */ 2190163953Srrs sin = (struct sockaddr_in *)initack_src; 2191163953Srrs memset(sin, 0, sizeof(*sin)); 2192163953Srrs sin->sin_family = AF_INET; 2193163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2194163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2195163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 2196163953Srrs /* source addr is IPv6 */ 2197163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2198163953Srrs memset(sin6, 0, sizeof(*sin6)); 2199163953Srrs sin6->sin6_family = AF_INET6; 2200163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2201163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2202163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2203163953Srrs sizeof(sin6->sin6_addr)); 2204163953Srrs } else { 2205170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2206172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2207172090Srrs SCTP_TCB_UNLOCK(stcb); 2208172090Srrs SCTP_SOCKET_LOCK(so, 1); 2209172090Srrs SCTP_TCB_LOCK(stcb); 2210172090Srrs#endif 2211171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2212172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2213172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2214172090Srrs#endif 2215172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2216163953Srrs return (NULL); 2217163953Srrs } 2218163953Srrs 2219163953Srrs /* set up to notify upper layer */ 2220163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2221163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2222163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2223163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2224163953Srrs /* 2225163953Srrs * This is an endpoint that called connect() how it got a 2226163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2227163953Srrs * the INIT was sent, but before it got there.. a complete 2228163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2229163953Srrs * should have went to the other code.. not here.. oh well.. 2230163953Srrs * a bit of protection is worth having.. 2231163953Srrs */ 2232163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2233172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2234172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2235172090Srrs SCTP_TCB_UNLOCK(stcb); 2236172090Srrs SCTP_SOCKET_LOCK(so, 1); 2237172090Srrs SCTP_TCB_LOCK(stcb); 2238172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2239172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2240172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2241172090Srrs return (NULL); 2242172090Srrs } 2243172090Srrs#endif 2244172090Srrs soisconnected(stcb->sctp_socket); 2245172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2246172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2247172090Srrs#endif 2248163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2249163953Srrs (inp->sctp_socket->so_qlimit)) { 2250163953Srrs /* 2251163953Srrs * We don't want to do anything with this one. Since it is 2252163953Srrs * the listening guy. The timer will get started for 2253163953Srrs * accepted connections in the caller. 2254163953Srrs */ 2255163953Srrs ; 2256163953Srrs } 2257163953Srrs /* since we did not send a HB make sure we don't double things */ 2258169420Srrs if ((netp) && (*netp)) 2259169420Srrs (*netp)->hb_responded = 1; 2260163953Srrs 2261163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2262163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2263163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2264163953Srrs } 2265165220Srrs /* calculate the RTT */ 2266171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2267170642Srrs if ((netp) && (*netp)) { 2268169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2269171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2270170642Srrs } 2271172190Srrs /* respond with a COOKIE-ACK */ 2272163953Srrs sctp_send_cookie_ack(stcb); 2273172190Srrs 2274172190Srrs /* 2275172190Srrs * check the address lists for any ASCONFs that need to be sent 2276172190Srrs * AFTER the cookie-ack is sent 2277172190Srrs */ 2278172190Srrs sctp_check_address_list(stcb, m, 2279172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2280172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2281172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2282172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2283172190Srrs 2284172190Srrs 2285163953Srrs return (stcb); 2286163953Srrs} 2287163953Srrs 2288185694Srrs/* 2289185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2290185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2291185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2292185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2293185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2294185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2295185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2296185694Srrs -- SEND ABORT - TRY AGAIN -- 2297185694Srrs } 2298185694Srrs } 2299185694Srrs*/ 2300163953Srrs 2301163953Srrs/* 2302163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2303163953Srrs * existing (non-NULL) TCB 2304163953Srrs */ 2305163953Srrsstatic struct mbuf * 2306163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2307163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2308163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2309169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2310179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2311163953Srrs{ 2312163953Srrs struct sctp_state_cookie *cookie; 2313163953Srrs struct sockaddr_in6 sin6; 2314163953Srrs struct sockaddr_in sin; 2315163953Srrs struct sctp_tcb *l_stcb = *stcb; 2316163953Srrs struct sctp_inpcb *l_inp; 2317163953Srrs struct sockaddr *to; 2318163953Srrs sctp_assoc_t sac_restart_id; 2319163953Srrs struct sctp_pcb *ep; 2320163953Srrs struct mbuf *m_sig; 2321163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2322163953Srrs uint8_t *sig; 2323163953Srrs uint8_t cookie_ok = 0; 2324163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2325163953Srrs unsigned int cookie_len; 2326163953Srrs struct timeval now; 2327163953Srrs struct timeval time_expires; 2328163953Srrs struct sockaddr_storage dest_store; 2329163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2330163953Srrs struct ip *iph; 2331163953Srrs int notification = 0; 2332163953Srrs struct sctp_nets *netl; 2333163953Srrs int had_a_existing_tcb = 0; 2334163953Srrs 2335169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2336169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2337163953Srrs 2338163953Srrs if (inp_p == NULL) { 2339163953Srrs return (NULL); 2340163953Srrs } 2341163953Srrs /* First get the destination address setup too. */ 2342163953Srrs iph = mtod(m, struct ip *); 2343178251Srrs switch (iph->ip_v) { 2344178251Srrs case IPVERSION: 2345178251Srrs { 2346178251Srrs /* its IPv4 */ 2347178251Srrs struct sockaddr_in *lsin; 2348163953Srrs 2349178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2350178251Srrs memset(lsin, 0, sizeof(*lsin)); 2351178251Srrs lsin->sin_family = AF_INET; 2352178251Srrs lsin->sin_len = sizeof(*lsin); 2353178251Srrs lsin->sin_port = sh->dest_port; 2354178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2355178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2356178251Srrs break; 2357178251Srrs } 2358178251Srrs#ifdef INET6 2359178251Srrs case IPV6_VERSION >> 4: 2360178251Srrs { 2361178251Srrs /* its IPv6 */ 2362178251Srrs struct ip6_hdr *ip6; 2363178251Srrs struct sockaddr_in6 *lsin6; 2364163953Srrs 2365178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2366178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2367178251Srrs lsin6->sin6_family = AF_INET6; 2368178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2369178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2370178251Srrs lsin6->sin6_port = sh->dest_port; 2371178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2372178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2373178251Srrs break; 2374178251Srrs } 2375178251Srrs#endif 2376178251Srrs default: 2377163953Srrs return (NULL); 2378163953Srrs } 2379163953Srrs 2380163953Srrs cookie = &cp->cookie; 2381163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2382163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2383163953Srrs 2384163953Srrs if ((cookie->peerport != sh->src_port) && 2385163953Srrs (cookie->myport != sh->dest_port) && 2386163953Srrs (cookie->my_vtag != sh->v_tag)) { 2387163953Srrs /* 2388163953Srrs * invalid ports or bad tag. Note that we always leave the 2389163953Srrs * v_tag in the header in network order and when we stored 2390163953Srrs * it in the my_vtag slot we also left it in network order. 2391169352Srrs * This maintains the match even though it may be in the 2392163953Srrs * opposite byte order of the machine :-> 2393163953Srrs */ 2394163953Srrs return (NULL); 2395163953Srrs } 2396163953Srrs if (cookie_len > size_of_pkt || 2397163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2398163953Srrs sizeof(struct sctp_init_chunk) + 2399163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2400163953Srrs /* cookie too long! or too small */ 2401163953Srrs return (NULL); 2402163953Srrs } 2403163953Srrs /* 2404163953Srrs * split off the signature into its own mbuf (since it should not be 2405163953Srrs * calculated in the sctp_hmac_m() call). 2406163953Srrs */ 2407163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2408163953Srrs if (sig_offset > size_of_pkt) { 2409163953Srrs /* packet not correct size! */ 2410163953Srrs /* XXX this may already be accounted for earlier... */ 2411163953Srrs return (NULL); 2412163953Srrs } 2413163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2414163953Srrs if (m_sig == NULL) { 2415163953Srrs /* out of memory or ?? */ 2416163953Srrs return (NULL); 2417163953Srrs } 2418178202Srrs#ifdef SCTP_MBUF_LOGGING 2419179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2420178202Srrs struct mbuf *mat; 2421178202Srrs 2422178202Srrs mat = m_sig; 2423178202Srrs while (mat) { 2424178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2425178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2426178202Srrs } 2427178202Srrs mat = SCTP_BUF_NEXT(mat); 2428178202Srrs } 2429178202Srrs } 2430178202Srrs#endif 2431178202Srrs 2432163953Srrs /* 2433163953Srrs * compute the signature/digest for the cookie 2434163953Srrs */ 2435163953Srrs ep = &(*inp_p)->sctp_ep; 2436163953Srrs l_inp = *inp_p; 2437163953Srrs if (l_stcb) { 2438163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2439163953Srrs } 2440163953Srrs SCTP_INP_RLOCK(l_inp); 2441163953Srrs if (l_stcb) { 2442163953Srrs SCTP_TCB_LOCK(l_stcb); 2443163953Srrs } 2444163953Srrs /* which cookie is it? */ 2445163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2446163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2447163953Srrs /* it's the old cookie */ 2448169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2449163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2450170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2451163953Srrs } else { 2452163953Srrs /* it's the current cookie */ 2453169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2454163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2455170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2456163953Srrs } 2457163953Srrs /* get the signature */ 2458163953Srrs SCTP_INP_RUNLOCK(l_inp); 2459163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2460163953Srrs if (sig == NULL) { 2461163953Srrs /* couldn't find signature */ 2462164085Srrs sctp_m_freem(m_sig); 2463163953Srrs return (NULL); 2464163953Srrs } 2465163953Srrs /* compare the received digest with the computed digest */ 2466163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2467163953Srrs /* try the old cookie? */ 2468163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2469163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2470163953Srrs /* compute digest with old */ 2471169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2472163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2473170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2474163953Srrs /* compare */ 2475163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2476163953Srrs cookie_ok = 1; 2477163953Srrs } 2478163953Srrs } else { 2479163953Srrs cookie_ok = 1; 2480163953Srrs } 2481163953Srrs 2482163953Srrs /* 2483163953Srrs * Now before we continue we must reconstruct our mbuf so that 2484163953Srrs * normal processing of any other chunks will work. 2485163953Srrs */ 2486163953Srrs { 2487163953Srrs struct mbuf *m_at; 2488163953Srrs 2489163953Srrs m_at = m; 2490165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2491165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2492163953Srrs } 2493165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2494163953Srrs } 2495163953Srrs 2496163953Srrs if (cookie_ok == 0) { 2497169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2498169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2499169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2500169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2501163953Srrs return (NULL); 2502163953Srrs } 2503163953Srrs /* 2504163953Srrs * check the cookie timestamps to be sure it's not stale 2505163953Srrs */ 2506169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2507163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2508169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2509163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2510180387Srrs /* 2511180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2512180387Srrs * is undefined. 2513180387Srrs */ 2514163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2515163953Srrs /* cookie is stale! */ 2516163953Srrs struct mbuf *op_err; 2517163953Srrs struct sctp_stale_cookie_msg *scm; 2518163953Srrs uint32_t tim; 2519163953Srrs 2520163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2521165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2522163953Srrs if (op_err == NULL) { 2523163953Srrs /* FOOBAR */ 2524163953Srrs return (NULL); 2525163953Srrs } 2526163953Srrs /* Set the len */ 2527165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2528163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2529163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2530163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2531163953Srrs (sizeof(uint32_t)))); 2532163953Srrs /* seconds to usec */ 2533163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2534163953Srrs /* add in usec */ 2535163953Srrs if (tim == 0) 2536163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2537163953Srrs scm->time_usec = htonl(tim); 2538169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2539179157Srrs vrf_id, port); 2540163953Srrs return (NULL); 2541163953Srrs } 2542163953Srrs /* 2543163953Srrs * Now we must see with the lookup address if we have an existing 2544163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2545163953Srrs * and a INIT collided with us and somewhere the peer sent the 2546163953Srrs * cookie on another address besides the single address our assoc 2547163953Srrs * had for him. In this case we will have one of the tie-tags set at 2548163953Srrs * least AND the address field in the cookie can be used to look it 2549163953Srrs * up. 2550163953Srrs */ 2551163953Srrs to = NULL; 2552163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2553163953Srrs memset(&sin6, 0, sizeof(sin6)); 2554163953Srrs sin6.sin6_family = AF_INET6; 2555163953Srrs sin6.sin6_len = sizeof(sin6); 2556163953Srrs sin6.sin6_port = sh->src_port; 2557163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2558163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2559163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2560163953Srrs to = (struct sockaddr *)&sin6; 2561163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2562163953Srrs memset(&sin, 0, sizeof(sin)); 2563163953Srrs sin.sin_family = AF_INET; 2564163953Srrs sin.sin_len = sizeof(sin); 2565163953Srrs sin.sin_port = sh->src_port; 2566163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2567163953Srrs to = (struct sockaddr *)&sin; 2568169420Srrs } else { 2569169420Srrs /* This should not happen */ 2570169420Srrs return (NULL); 2571163953Srrs } 2572163953Srrs if ((*stcb == NULL) && to) { 2573163953Srrs /* Yep, lets check */ 2574163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2575163953Srrs if (*stcb == NULL) { 2576163953Srrs /* 2577163953Srrs * We should have only got back the same inp. If we 2578163953Srrs * got back a different ep we have a problem. The 2579163953Srrs * original findep got back l_inp and now 2580163953Srrs */ 2581163953Srrs if (l_inp != *inp_p) { 2582169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2583163953Srrs } 2584165220Srrs } else { 2585165220Srrs if (*locked_tcb == NULL) { 2586165220Srrs /* 2587165220Srrs * In this case we found the assoc only 2588165220Srrs * after we locked the create lock. This 2589165220Srrs * means we are in a colliding case and we 2590165220Srrs * must make sure that we unlock the tcb if 2591165220Srrs * its one of the cases where we throw away 2592165220Srrs * the incoming packets. 2593165220Srrs */ 2594165220Srrs *locked_tcb = *stcb; 2595165220Srrs 2596165220Srrs /* 2597165220Srrs * We must also increment the inp ref count 2598165220Srrs * since the ref_count flags was set when we 2599165220Srrs * did not find the TCB, now we found it 2600165220Srrs * which reduces the refcount.. we must 2601165220Srrs * raise it back out to balance it all :-) 2602165220Srrs */ 2603165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2604165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2605169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2606165220Srrs (*stcb)->sctp_ep, l_inp); 2607165220Srrs } 2608165220Srrs } 2609163953Srrs } 2610163953Srrs } 2611188067Srrs if (to == NULL) { 2612169420Srrs return (NULL); 2613188067Srrs } 2614163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2615163953Srrs if (*stcb == NULL) { 2616163953Srrs /* this is the "normal" case... get a new TCB */ 2617163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2618163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2619179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2620163953Srrs } else { 2621163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2622163953Srrs had_a_existing_tcb = 1; 2623163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2624185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2625185694Srrs ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port); 2626163953Srrs } 2627163953Srrs 2628163953Srrs if (*stcb == NULL) { 2629163953Srrs /* still no TCB... must be bad cookie-echo */ 2630163953Srrs return (NULL); 2631163953Srrs } 2632163953Srrs /* 2633163953Srrs * Ok, we built an association so confirm the address we sent the 2634163953Srrs * INIT-ACK to. 2635163953Srrs */ 2636163953Srrs netl = sctp_findnet(*stcb, to); 2637163953Srrs /* 2638163953Srrs * This code should in theory NOT run but 2639163953Srrs */ 2640163953Srrs if (netl == NULL) { 2641163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2642163953Srrs int ret; 2643163953Srrs 2644165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2645165220Srrs SCTP_IN_COOKIE_PROC); 2646163953Srrs netl = sctp_findnet(*stcb, to); 2647163953Srrs } 2648163953Srrs if (netl) { 2649163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2650163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2651169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2652163953Srrs netl); 2653163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2654172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2655163953Srrs } 2656163953Srrs } 2657163953Srrs if (*stcb) { 2658163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2659163953Srrs *stcb, NULL); 2660163953Srrs } 2661163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2662163953Srrs if (!had_a_existing_tcb || 2663163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2664163953Srrs /* 2665163953Srrs * If we have a NEW cookie or the connect never 2666163953Srrs * reached the connected state during collision we 2667163953Srrs * must do the TCP accept thing. 2668163953Srrs */ 2669163953Srrs struct socket *so, *oso; 2670163953Srrs struct sctp_inpcb *inp; 2671163953Srrs 2672163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2673163953Srrs /* 2674163953Srrs * For a restart we will keep the same 2675163953Srrs * socket, no need to do anything. I THINK!! 2676163953Srrs */ 2677172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2678163953Srrs return (m); 2679163953Srrs } 2680163953Srrs oso = (*inp_p)->sctp_socket; 2681171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2682163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2683166086Srrs so = sonewconn(oso, 0 2684163953Srrs ); 2685163953Srrs SCTP_TCB_LOCK((*stcb)); 2686171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2687171858Srrs 2688163953Srrs if (so == NULL) { 2689163953Srrs struct mbuf *op_err; 2690163953Srrs 2691172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2692172090Srrs struct socket *pcb_so; 2693172090Srrs 2694172090Srrs#endif 2695163953Srrs /* Too many sockets */ 2696169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2697163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2698163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2699179157Srrs sh, op_err, vrf_id, port); 2700172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2701172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2702172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2703172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2704172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2705172090Srrs SCTP_TCB_LOCK((*stcb)); 2706172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2707172090Srrs#endif 2708171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2709172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2710172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2711172090Srrs#endif 2712163953Srrs return (NULL); 2713163953Srrs } 2714163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2715166086Srrs SCTP_INP_INCR_REF(inp); 2716166086Srrs /* 2717166086Srrs * We add the unbound flag here so that if we get an 2718166086Srrs * soabort() before we get the move_pcb done, we 2719166086Srrs * will properly cleanup. 2720166086Srrs */ 2721163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2722163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2723163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2724166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2725163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2726163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2727163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2728172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2729163953Srrs inp->sctp_socket = so; 2730163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2731163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2732163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2733163953Srrs inp->inp_starting_point_for_iterator = NULL; 2734163953Srrs /* 2735163953Srrs * copy in the authentication parameters from the 2736163953Srrs * original endpoint 2737163953Srrs */ 2738163953Srrs if (inp->sctp_ep.local_hmacs) 2739163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2740163953Srrs inp->sctp_ep.local_hmacs = 2741163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2742163953Srrs if (inp->sctp_ep.local_auth_chunks) 2743163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2744163953Srrs inp->sctp_ep.local_auth_chunks = 2745163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2746163953Srrs 2747163953Srrs /* 2748163953Srrs * Now we must move it from one hash table to 2749163953Srrs * another and get the tcb in the right place. 2750163953Srrs */ 2751163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2752170056Srrs 2753170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2754170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2755170056Srrs 2756175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2757175845Srwatson 0); 2758170056Srrs SCTP_TCB_LOCK((*stcb)); 2759170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2760163953Srrs 2761170056Srrs 2762166086Srrs /* 2763166086Srrs * now we must check to see if we were aborted while 2764166086Srrs * the move was going on and the lock/unlock 2765166086Srrs * happened. 2766166086Srrs */ 2767166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2768166086Srrs /* 2769166086Srrs * yep it was, we leave the assoc attached 2770166086Srrs * to the socket since the sctp_inpcb_free() 2771166086Srrs * call will send an abort for us. 2772166086Srrs */ 2773166086Srrs SCTP_INP_DECR_REF(inp); 2774166086Srrs return (NULL); 2775166086Srrs } 2776166086Srrs SCTP_INP_DECR_REF(inp); 2777163953Srrs /* Switch over to the new guy */ 2778163953Srrs *inp_p = inp; 2779172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2780163953Srrs 2781166086Srrs /* 2782166086Srrs * Pull it from the incomplete queue and wake the 2783166086Srrs * guy 2784166086Srrs */ 2785172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2786172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2787172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2788172090Srrs SCTP_SOCKET_LOCK(so, 1); 2789172090Srrs#endif 2790166086Srrs soisconnected(so); 2791172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2792172118Srrs SCTP_TCB_LOCK((*stcb)); 2793172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2794172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2795172090Srrs#endif 2796163953Srrs return (m); 2797163953Srrs } 2798163953Srrs } 2799163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2800172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2801163953Srrs } 2802163953Srrs return (m); 2803163953Srrs} 2804163953Srrs 2805163953Srrsstatic void 2806163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2807163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2808163953Srrs{ 2809163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2810163953Srrs struct sctp_association *asoc; 2811163953Srrs 2812169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2813169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2814163953Srrs if (stcb == NULL) 2815163953Srrs return; 2816163953Srrs 2817163953Srrs asoc = &stcb->asoc; 2818163953Srrs 2819163953Srrs sctp_stop_all_cookie_timers(stcb); 2820163953Srrs /* process according to association state */ 2821163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2822163953Srrs /* state change only needed when I am in right state */ 2823169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2824171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2825163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2826163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2827163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2828163953Srrs 2829163953Srrs } 2830163953Srrs /* update RTO */ 2831163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2832163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2833163953Srrs if (asoc->overall_error_count == 0) { 2834163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2835171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2836163953Srrs } 2837169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2838172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2839163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2840163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2841172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2842172090Srrs struct socket *so; 2843172090Srrs 2844172090Srrs#endif 2845163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2846172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2847172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2848172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2849172090Srrs SCTP_TCB_UNLOCK(stcb); 2850172090Srrs SCTP_SOCKET_LOCK(so, 1); 2851172090Srrs SCTP_TCB_LOCK(stcb); 2852172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2853172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2854172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2855172090Srrs return; 2856172090Srrs } 2857172090Srrs#endif 2858172090Srrs soisconnected(stcb->sctp_socket); 2859172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2860172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2861172090Srrs#endif 2862163953Srrs } 2863163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2864163953Srrs stcb, net); 2865163953Srrs /* 2866163953Srrs * since we did not send a HB make sure we don't double 2867163953Srrs * things 2868163953Srrs */ 2869163953Srrs net->hb_responded = 1; 2870163953Srrs 2871163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2872163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2873163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2874163953Srrs stcb->sctp_ep, stcb, NULL); 2875163953Srrs } 2876163953Srrs /* 2877171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2878171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2879171572Srrs * in flight) 2880163953Srrs */ 2881163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2882163953Srrs (stcb->asoc.peer_supports_asconf) && 2883163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2884171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2885163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2886163953Srrs stcb->sctp_ep, stcb, 2887163953Srrs stcb->asoc.primary_destination); 2888171572Srrs#else 2889172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2890172190Srrs SCTP_ADDR_NOT_LOCKED); 2891171572Srrs#endif 2892163953Srrs } 2893163953Srrs } 2894163953Srrs /* Toss the cookie if I can */ 2895163953Srrs sctp_toss_old_cookies(stcb, asoc); 2896163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2897163953Srrs /* Restart the timer if we have pending data */ 2898163953Srrs struct sctp_tmit_chunk *chk; 2899163953Srrs 2900163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2901163953Srrs if (chk) { 2902163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2903163953Srrs stcb, chk->whoTo); 2904163953Srrs } 2905163953Srrs } 2906163953Srrs} 2907163953Srrs 2908163953Srrsstatic void 2909163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2910163953Srrs struct sctp_tcb *stcb) 2911163953Srrs{ 2912163953Srrs struct sctp_nets *net; 2913163953Srrs struct sctp_tmit_chunk *lchk; 2914163953Srrs uint32_t tsn; 2915163953Srrs 2916163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2917163953Srrs return; 2918163953Srrs } 2919163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2920163953Srrs tsn = ntohl(cp->tsn); 2921163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2922163953Srrs /* Also we make sure we disable the nonce_wait */ 2923163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2924163953Srrs if (lchk == NULL) { 2925163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2926163953Srrs } else { 2927163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2928163953Srrs } 2929163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2930163953Srrs stcb->asoc.nonce_sum_check = 0; 2931163953Srrs 2932163953Srrs /* Find where it was sent, if possible */ 2933163953Srrs net = NULL; 2934163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2935163953Srrs while (lchk) { 2936163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2937163953Srrs net = lchk->whoTo; 2938163953Srrs break; 2939163953Srrs } 2940163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2941163953Srrs break; 2942163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2943163953Srrs } 2944163953Srrs if (net == NULL) 2945163953Srrs /* default is we use the primary */ 2946163953Srrs net = stcb->asoc.primary_destination; 2947163953Srrs 2948163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2949163953Srrs /* 2950171440Srrs * JRS - Use the congestion control given in the pluggable 2951171440Srrs * CC module 2952171440Srrs */ 2953171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2954171440Srrs /* 2955163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2956163953Srrs * the next sending seq i.e. the resync_tsn. 2957163953Srrs */ 2958163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2959163953Srrs } 2960163953Srrs /* 2961163953Srrs * We always send a CWR this way if our previous one was lost our 2962163953Srrs * peer will get an update, or if it is not time again to reduce we 2963163953Srrs * still get the cwr to the peer. 2964163953Srrs */ 2965163953Srrs sctp_send_cwr(stcb, net, tsn); 2966163953Srrs} 2967163953Srrs 2968163953Srrsstatic void 2969163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2970163953Srrs{ 2971163953Srrs /* 2972163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2973163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2974163953Srrs * If so remove it. 2975163953Srrs */ 2976163953Srrs struct sctp_tmit_chunk *chk; 2977163953Srrs struct sctp_ecne_chunk *ecne; 2978163953Srrs 2979163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2980163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2981163953Srrs continue; 2982163953Srrs } 2983163953Srrs /* 2984163953Srrs * Look for and remove if it is the right TSN. Since there 2985163953Srrs * is only ONE ECNE on the control queue at any one time we 2986163953Srrs * don't need to worry about more than one! 2987163953Srrs */ 2988163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2989163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2990163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2991163953Srrs /* this covers this ECNE, we can remove it */ 2992163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2993163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2994163953Srrs sctp_next); 2995163953Srrs if (chk->data) { 2996163953Srrs sctp_m_freem(chk->data); 2997163953Srrs chk->data = NULL; 2998163953Srrs } 2999163953Srrs stcb->asoc.ctrl_queue_cnt--; 3000163953Srrs sctp_free_a_chunk(stcb, chk); 3001163953Srrs break; 3002163953Srrs } 3003163953Srrs } 3004163953Srrs} 3005163953Srrs 3006163953Srrsstatic void 3007163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3008163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3009163953Srrs{ 3010163953Srrs struct sctp_association *asoc; 3011163953Srrs 3012172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3013172090Srrs struct socket *so; 3014172090Srrs 3015172090Srrs#endif 3016172090Srrs 3017169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3018169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3019163953Srrs if (stcb == NULL) 3020163953Srrs return; 3021163953Srrs 3022163953Srrs asoc = &stcb->asoc; 3023163953Srrs /* process according to association state */ 3024163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3025163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3026171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3027171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3028163953Srrs SCTP_TCB_UNLOCK(stcb); 3029163953Srrs return; 3030163953Srrs } 3031163953Srrs /* notify upper layer protocol */ 3032163953Srrs if (stcb->sctp_socket) { 3033172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3034163953Srrs /* are the queues empty? they should be */ 3035163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3036163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3037163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 3038172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3039163953Srrs } 3040163953Srrs } 3041163953Srrs /* stop the timer */ 3042178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3043163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3044163953Srrs /* free the TCB */ 3045171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3046171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3047172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3048172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3049172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3050172090Srrs SCTP_TCB_UNLOCK(stcb); 3051172090Srrs SCTP_SOCKET_LOCK(so, 1); 3052172090Srrs SCTP_TCB_LOCK(stcb); 3053172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3054172090Srrs#endif 3055171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3056172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3057172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3058172090Srrs#endif 3059163953Srrs return; 3060163953Srrs} 3061163953Srrs 3062163953Srrsstatic int 3063163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3064163953Srrs struct sctp_nets *net, uint8_t flg) 3065163953Srrs{ 3066163953Srrs switch (desc->chunk_type) { 3067163953Srrs case SCTP_DATA: 3068163953Srrs /* find the tsn to resend (possibly */ 3069163953Srrs { 3070163953Srrs uint32_t tsn; 3071163953Srrs struct sctp_tmit_chunk *tp1; 3072163953Srrs 3073163953Srrs tsn = ntohl(desc->tsn_ifany); 3074163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3075163953Srrs while (tp1) { 3076163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3077163953Srrs /* found it */ 3078163953Srrs break; 3079163953Srrs } 3080163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 3081163953Srrs MAX_TSN)) { 3082163953Srrs /* not found */ 3083163953Srrs tp1 = NULL; 3084163953Srrs break; 3085163953Srrs } 3086163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3087163953Srrs } 3088163953Srrs if (tp1 == NULL) { 3089163953Srrs /* 3090163953Srrs * Do it the other way , aka without paying 3091163953Srrs * attention to queue seq order. 3092163953Srrs */ 3093163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3094163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3095163953Srrs while (tp1) { 3096163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3097163953Srrs /* found it */ 3098163953Srrs break; 3099163953Srrs } 3100163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3101163953Srrs } 3102163953Srrs } 3103163953Srrs if (tp1 == NULL) { 3104163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3105163953Srrs } 3106163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3107163953Srrs uint8_t *ddp; 3108163953Srrs 3109163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3110163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3111163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3112163953Srrs return (0); 3113163953Srrs } 3114163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3115163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3116163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3117163953Srrs return (0); 3118163953Srrs } 3119163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3120163953Srrs sizeof(struct sctp_data_chunk)); 3121163953Srrs { 3122163953Srrs unsigned int iii; 3123163953Srrs 3124163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3125163953Srrs iii++) { 3126163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3127163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3128163953Srrs return (-1); 3129163953Srrs } 3130163953Srrs } 3131163953Srrs } 3132163953Srrs /* 3133163953Srrs * We zero out the nonce so resync not 3134163953Srrs * needed 3135163953Srrs */ 3136163953Srrs tp1->rec.data.ect_nonce = 0; 3137163953Srrs 3138163953Srrs if (tp1->do_rtt) { 3139163953Srrs /* 3140163953Srrs * this guy had a RTO calculation 3141163953Srrs * pending on it, cancel it 3142163953Srrs */ 3143163953Srrs tp1->do_rtt = 0; 3144163953Srrs } 3145163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3146163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3147163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3148163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 3149163953Srrs /* 3150163953Srrs * mark it as if we were doing a FR, since 3151163953Srrs * we will be getting gap ack reports behind 3152163953Srrs * the info from the router. 3153163953Srrs */ 3154163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3155163953Srrs /* 3156163953Srrs * mark the tsn with what sequences can 3157163953Srrs * cause a new FR. 3158163953Srrs */ 3159163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3160163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3161163953Srrs } else { 3162163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3163163953Srrs } 3164163953Srrs 3165163953Srrs /* restart the timer */ 3166163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3167171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3168163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3169163953Srrs stcb, tp1->whoTo); 3170163953Srrs 3171163953Srrs /* fix counts and things */ 3172179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3173170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3174170744Srrs tp1->whoTo->flight_size, 3175170744Srrs tp1->book_size, 3176170744Srrs (uintptr_t) stcb, 3177170744Srrs tp1->rec.data.TSN_seq); 3178170744Srrs } 3179190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3180190689Srrs sctp_flight_size_decrease(tp1); 3181190689Srrs sctp_total_flight_decrease(stcb, tp1); 3182190689Srrs } 3183163953Srrs } { 3184163953Srrs /* audit code */ 3185163953Srrs unsigned int audit; 3186163953Srrs 3187163953Srrs audit = 0; 3188163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3189163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3190163953Srrs audit++; 3191163953Srrs } 3192163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3193163953Srrs sctp_next) { 3194163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3195163953Srrs audit++; 3196163953Srrs } 3197163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3198169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3199163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3200163953Srrs#ifndef SCTP_AUDITING_ENABLED 3201163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3202163953Srrs#endif 3203163953Srrs } 3204163953Srrs } 3205163953Srrs } 3206163953Srrs break; 3207163953Srrs case SCTP_ASCONF: 3208163953Srrs { 3209163953Srrs struct sctp_tmit_chunk *asconf; 3210163953Srrs 3211163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3212163953Srrs sctp_next) { 3213163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3214163953Srrs break; 3215163953Srrs } 3216163953Srrs } 3217163953Srrs if (asconf) { 3218163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3219163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3220163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3221163953Srrs asconf->snd_count--; 3222163953Srrs } 3223163953Srrs } 3224163953Srrs break; 3225163953Srrs case SCTP_INITIATION: 3226163953Srrs /* resend the INIT */ 3227163953Srrs stcb->asoc.dropped_special_cnt++; 3228163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3229163953Srrs /* 3230163953Srrs * If we can get it in, in a few attempts we do 3231163953Srrs * this, otherwise we let the timer fire. 3232163953Srrs */ 3233163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3234171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3235172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3236163953Srrs } 3237163953Srrs break; 3238163953Srrs case SCTP_SELECTIVE_ACK: 3239163953Srrs /* resend the sack */ 3240163953Srrs sctp_send_sack(stcb); 3241163953Srrs break; 3242185694Srrs /* EY for nr_sacks */ 3243185694Srrs case SCTP_NR_SELECTIVE_ACK: 3244185694Srrs sctp_send_nr_sack(stcb); /* EY resend the nr-sack */ 3245185694Srrs break; 3246163953Srrs case SCTP_HEARTBEAT_REQUEST: 3247163953Srrs /* resend a demand HB */ 3248171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3249171440Srrs /* 3250171440Srrs * Only retransmit if we KNOW we wont destroy the 3251171440Srrs * tcb 3252171440Srrs */ 3253171440Srrs (void)sctp_send_hb(stcb, 1, net); 3254171440Srrs } 3255163953Srrs break; 3256163953Srrs case SCTP_SHUTDOWN: 3257163953Srrs sctp_send_shutdown(stcb, net); 3258163953Srrs break; 3259163953Srrs case SCTP_SHUTDOWN_ACK: 3260163953Srrs sctp_send_shutdown_ack(stcb, net); 3261163953Srrs break; 3262163953Srrs case SCTP_COOKIE_ECHO: 3263163953Srrs { 3264163953Srrs struct sctp_tmit_chunk *cookie; 3265163953Srrs 3266163953Srrs cookie = NULL; 3267163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3268163953Srrs sctp_next) { 3269163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3270163953Srrs break; 3271163953Srrs } 3272163953Srrs } 3273163953Srrs if (cookie) { 3274163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3275163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3276163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3277163953Srrs sctp_stop_all_cookie_timers(stcb); 3278163953Srrs } 3279163953Srrs } 3280163953Srrs break; 3281163953Srrs case SCTP_COOKIE_ACK: 3282163953Srrs sctp_send_cookie_ack(stcb); 3283163953Srrs break; 3284163953Srrs case SCTP_ASCONF_ACK: 3285163953Srrs /* resend last asconf ack */ 3286171990Srrs sctp_send_asconf_ack(stcb); 3287163953Srrs break; 3288163953Srrs case SCTP_FORWARD_CUM_TSN: 3289163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3290163953Srrs break; 3291163953Srrs /* can't do anything with these */ 3292163953Srrs case SCTP_PACKET_DROPPED: 3293163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3294163953Srrs case SCTP_HEARTBEAT_ACK: 3295163953Srrs case SCTP_ABORT_ASSOCIATION: 3296163953Srrs case SCTP_OPERATION_ERROR: 3297163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3298163953Srrs case SCTP_ECN_ECHO: 3299163953Srrs case SCTP_ECN_CWR: 3300163953Srrs default: 3301163953Srrs break; 3302163953Srrs } 3303163953Srrs return (0); 3304163953Srrs} 3305163953Srrs 3306163953Srrsvoid 3307163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3308163953Srrs{ 3309163953Srrs int i; 3310163953Srrs uint16_t temp; 3311163953Srrs 3312163953Srrs /* 3313163953Srrs * We set things to 0xffff since this is the last delivered sequence 3314163953Srrs * and we will be sending in 0 after the reset. 3315163953Srrs */ 3316163953Srrs 3317163953Srrs if (number_entries) { 3318163953Srrs for (i = 0; i < number_entries; i++) { 3319163953Srrs temp = ntohs(list[i]); 3320163953Srrs if (temp >= stcb->asoc.streamincnt) { 3321163953Srrs continue; 3322163953Srrs } 3323163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3324163953Srrs } 3325163953Srrs } else { 3326163953Srrs list = NULL; 3327163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3328163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3329163953Srrs } 3330163953Srrs } 3331172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3332163953Srrs} 3333163953Srrs 3334163953Srrsstatic void 3335163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3336163953Srrs{ 3337163953Srrs int i; 3338163953Srrs 3339163953Srrs if (number_entries == 0) { 3340163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3341163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3342163953Srrs } 3343163953Srrs } else if (number_entries) { 3344163953Srrs for (i = 0; i < number_entries; i++) { 3345163953Srrs uint16_t temp; 3346163953Srrs 3347163953Srrs temp = ntohs(list[i]); 3348163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3349163953Srrs /* no such stream */ 3350163953Srrs continue; 3351163953Srrs } 3352163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3353163953Srrs } 3354163953Srrs } 3355172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3356163953Srrs} 3357163953Srrs 3358163953Srrs 3359163953Srrsstruct sctp_stream_reset_out_request * 3360163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3361163953Srrs{ 3362163953Srrs struct sctp_association *asoc; 3363163953Srrs struct sctp_stream_reset_out_req *req; 3364163953Srrs struct sctp_stream_reset_out_request *r; 3365163953Srrs struct sctp_tmit_chunk *chk; 3366163953Srrs int len, clen; 3367163953Srrs 3368163953Srrs asoc = &stcb->asoc; 3369163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3370169208Srrs asoc->stream_reset_outstanding = 0; 3371163953Srrs return (NULL); 3372163953Srrs } 3373163953Srrs if (stcb->asoc.str_reset == NULL) { 3374169208Srrs asoc->stream_reset_outstanding = 0; 3375163953Srrs return (NULL); 3376163953Srrs } 3377163953Srrs chk = stcb->asoc.str_reset; 3378163953Srrs if (chk->data == NULL) { 3379163953Srrs return (NULL); 3380163953Srrs } 3381163953Srrs if (bchk) { 3382163953Srrs /* he wants a copy of the chk pointer */ 3383163953Srrs *bchk = chk; 3384163953Srrs } 3385163953Srrs clen = chk->send_size; 3386163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3387163953Srrs r = &req->sr_req; 3388163953Srrs if (ntohl(r->request_seq) == seq) { 3389163953Srrs /* found it */ 3390163953Srrs return (r); 3391163953Srrs } 3392163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3393163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3394163953Srrs /* move to the next one, there can only be a max of two */ 3395163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3396163953Srrs if (ntohl(r->request_seq) == seq) { 3397163953Srrs return (r); 3398163953Srrs } 3399163953Srrs } 3400163953Srrs /* that seq is not here */ 3401163953Srrs return (NULL); 3402163953Srrs} 3403163953Srrs 3404163953Srrsstatic void 3405163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3406163953Srrs{ 3407163953Srrs struct sctp_association *asoc; 3408163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3409163953Srrs 3410163953Srrs if (stcb->asoc.str_reset == NULL) { 3411163953Srrs return; 3412163953Srrs } 3413166675Srrs asoc = &stcb->asoc; 3414166675Srrs 3415171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3416163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3417163953Srrs chk, 3418163953Srrs sctp_next); 3419163953Srrs if (chk->data) { 3420163953Srrs sctp_m_freem(chk->data); 3421163953Srrs chk->data = NULL; 3422163953Srrs } 3423163953Srrs asoc->ctrl_queue_cnt--; 3424163953Srrs sctp_free_a_chunk(stcb, chk); 3425172156Srrs /* sa_ignore NO_NULL_CHK */ 3426163953Srrs stcb->asoc.str_reset = NULL; 3427163953Srrs} 3428163953Srrs 3429163953Srrs 3430163953Srrsstatic int 3431163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3432163953Srrs uint32_t seq, uint32_t action, 3433164139Srrs struct sctp_stream_reset_response *respin) 3434163953Srrs{ 3435163953Srrs uint16_t type; 3436163953Srrs int lparm_len; 3437163953Srrs struct sctp_association *asoc = &stcb->asoc; 3438163953Srrs struct sctp_tmit_chunk *chk; 3439163953Srrs struct sctp_stream_reset_out_request *srparam; 3440163953Srrs int number_entries; 3441163953Srrs 3442163953Srrs if (asoc->stream_reset_outstanding == 0) { 3443163953Srrs /* duplicate */ 3444163953Srrs return (0); 3445163953Srrs } 3446163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3447163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3448163953Srrs if (srparam) { 3449163953Srrs stcb->asoc.str_reset_seq_out++; 3450163953Srrs type = ntohs(srparam->ph.param_type); 3451163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3452163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3453164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3454163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3455163953Srrs if (asoc->stream_reset_outstanding) 3456163953Srrs asoc->stream_reset_outstanding--; 3457163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3458163953Srrs /* do it */ 3459163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3460163953Srrs } else { 3461172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3462163953Srrs } 3463163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3464163953Srrs /* Answered my request */ 3465164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3466163953Srrs if (asoc->stream_reset_outstanding) 3467163953Srrs asoc->stream_reset_outstanding--; 3468163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3469172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3470163953Srrs } 3471188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3472188854Srrs /* Ok we now may have more streams */ 3473189121Srrs if (asoc->stream_reset_outstanding) 3474189121Srrs asoc->stream_reset_outstanding--; 3475188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3476188854Srrs /* Put the new streams into effect */ 3477188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3478188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3479188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3480188854Srrs } else { 3481188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3482188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3483188854Srrs } 3484163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3485163953Srrs /** 3486163953Srrs * a) Adopt the new in tsn. 3487163953Srrs * b) reset the map 3488163953Srrs * c) Adopt the new out-tsn 3489163953Srrs */ 3490163953Srrs struct sctp_stream_reset_response_tsn *resp; 3491163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3492163953Srrs int abort_flag = 0; 3493163953Srrs 3494163953Srrs if (respin == NULL) { 3495163953Srrs /* huh ? */ 3496163953Srrs return (0); 3497163953Srrs } 3498163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3499163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3500163953Srrs asoc->stream_reset_outstanding--; 3501163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3502163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3503163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3504170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3505163953Srrs if (abort_flag) { 3506163953Srrs return (1); 3507163953Srrs } 3508163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3509179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3510179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3511179783Srrs } 3512180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3513163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3514163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3515185694Srrs 3516205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3517205627Srrs stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.mapping_array_base_tsn; 3518205627Srrs memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size); 3519205627Srrs 3520163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3521163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3522163953Srrs 3523163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3524163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3525163953Srrs 3526163953Srrs } 3527163953Srrs } 3528163953Srrs /* get rid of the request and get the request flags */ 3529163953Srrs if (asoc->stream_reset_outstanding == 0) { 3530163953Srrs sctp_clean_up_stream_reset(stcb); 3531163953Srrs } 3532163953Srrs } 3533163953Srrs } 3534163953Srrs return (0); 3535163953Srrs} 3536163953Srrs 3537163953Srrsstatic void 3538163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3539163953Srrs struct sctp_tmit_chunk *chk, 3540170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3541163953Srrs{ 3542163953Srrs uint32_t seq; 3543163953Srrs int len, i; 3544163953Srrs int number_entries; 3545163953Srrs uint16_t temp; 3546163953Srrs 3547163953Srrs /* 3548163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3549163953Srrs * seq_in is right. 3550163953Srrs */ 3551163953Srrs struct sctp_association *asoc = &stcb->asoc; 3552163953Srrs 3553163953Srrs seq = ntohl(req->request_seq); 3554163953Srrs if (asoc->str_reset_seq_in == seq) { 3555170992Srrs if (trunc) { 3556170992Srrs /* Can't do it, since they exceeded our buffer size */ 3557170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3558170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3559170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3560170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3561163953Srrs len = ntohs(req->ph.param_length); 3562163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3563163953Srrs for (i = 0; i < number_entries; i++) { 3564163953Srrs temp = ntohs(req->list_of_streams[i]); 3565163953Srrs req->list_of_streams[i] = temp; 3566163953Srrs } 3567163953Srrs /* move the reset action back one */ 3568163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3569163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3570163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3571163953Srrs asoc->str_reset_seq_out, 3572163953Srrs seq, (asoc->sending_seq - 1)); 3573163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3574163953Srrs asoc->str_reset = chk; 3575163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3576163953Srrs stcb->asoc.stream_reset_outstanding++; 3577163953Srrs } else { 3578163953Srrs /* Can't do it, since we have sent one out */ 3579163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3580163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3581163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3582163953Srrs } 3583163953Srrs asoc->str_reset_seq_in++; 3584163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3585163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3586163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3587163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3588163953Srrs } else { 3589163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3590163953Srrs } 3591163953Srrs} 3592163953Srrs 3593163953Srrsstatic int 3594163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3595163953Srrs struct sctp_tmit_chunk *chk, 3596163953Srrs struct sctp_stream_reset_tsn_request *req) 3597163953Srrs{ 3598163953Srrs /* reset all in and out and update the tsn */ 3599163953Srrs /* 3600163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3601163953Srrs * and set cum-ack to it. Also process this selected number as a 3602163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3603163953Srrs */ 3604163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3605163953Srrs struct sctp_association *asoc = &stcb->asoc; 3606163953Srrs int abort_flag = 0; 3607163953Srrs uint32_t seq; 3608163953Srrs 3609163953Srrs seq = ntohl(req->request_seq); 3610163953Srrs if (asoc->str_reset_seq_in == seq) { 3611163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3612163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3613163953Srrs fwdtsn.ch.chunk_flags = 0; 3614163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3615170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3616163953Srrs if (abort_flag) { 3617163953Srrs return (1); 3618163953Srrs } 3619163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3620179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3621179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3622179783Srrs } 3623180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3624163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3625163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3626205627Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3627205627Srrs stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3628205627Srrs memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size); 3629163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3630163953Srrs /* save off historical data for retrans */ 3631163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3632163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3633163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3634163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3635163953Srrs 3636163953Srrs sctp_add_stream_reset_result_tsn(chk, 3637163953Srrs ntohl(req->request_seq), 3638163953Srrs SCTP_STREAM_RESET_PERFORMED, 3639163953Srrs stcb->asoc.sending_seq, 3640163953Srrs stcb->asoc.mapping_array_base_tsn); 3641163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3642163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3643163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3644163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3645163953Srrs 3646163953Srrs asoc->str_reset_seq_in++; 3647163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3648163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3649163953Srrs stcb->asoc.last_sending_seq[0], 3650163953Srrs stcb->asoc.last_base_tsnsent[0] 3651163953Srrs ); 3652163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3653163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3654163953Srrs stcb->asoc.last_sending_seq[1], 3655163953Srrs stcb->asoc.last_base_tsnsent[1] 3656163953Srrs ); 3657163953Srrs } else { 3658163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3659163953Srrs } 3660163953Srrs return (0); 3661163953Srrs} 3662163953Srrs 3663163953Srrsstatic void 3664163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3665163953Srrs struct sctp_tmit_chunk *chk, 3666170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3667163953Srrs{ 3668163953Srrs uint32_t seq, tsn; 3669163953Srrs int number_entries, len; 3670163953Srrs struct sctp_association *asoc = &stcb->asoc; 3671163953Srrs 3672163953Srrs seq = ntohl(req->request_seq); 3673163953Srrs 3674163953Srrs /* now if its not a duplicate we process it */ 3675163953Srrs if (asoc->str_reset_seq_in == seq) { 3676163953Srrs len = ntohs(req->ph.param_length); 3677163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3678163953Srrs /* 3679163953Srrs * the sender is resetting, handle the list issue.. we must 3680163953Srrs * a) verify if we can do the reset, if so no problem b) If 3681163953Srrs * we can't do the reset we must copy the request. c) queue 3682163953Srrs * it, and setup the data in processor to trigger it off 3683163953Srrs * when needed and dequeue all the queued data. 3684163953Srrs */ 3685163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3686163953Srrs 3687163953Srrs /* move the reset action back one */ 3688163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3689170992Srrs if (trunc) { 3690170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3691170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3692170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3693163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3694163953Srrs /* we can do it now */ 3695163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3696163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3697163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3698163953Srrs } else { 3699163953Srrs /* 3700163953Srrs * we must queue it up and thus wait for the TSN's 3701163953Srrs * to arrive that are at or before tsn 3702163953Srrs */ 3703163953Srrs struct sctp_stream_reset_list *liste; 3704163953Srrs int siz; 3705163953Srrs 3706163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3707163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3708170091Srrs siz, SCTP_M_STRESET); 3709163953Srrs if (liste == NULL) { 3710163953Srrs /* gak out of memory */ 3711163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3712163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3713163953Srrs return; 3714163953Srrs } 3715163953Srrs liste->tsn = tsn; 3716163953Srrs liste->number_entries = number_entries; 3717163953Srrs memcpy(&liste->req, req, 3718163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3719163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3720163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3721163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3722163953Srrs } 3723163953Srrs asoc->str_reset_seq_in++; 3724163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3725163953Srrs /* 3726163953Srrs * one seq back, just echo back last action since my 3727163953Srrs * response was lost. 3728163953Srrs */ 3729163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3730163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3731163953Srrs /* 3732163953Srrs * two seq back, just echo back last action since my 3733163953Srrs * response was lost. 3734163953Srrs */ 3735163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3736163953Srrs } else { 3737163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3738163953Srrs } 3739163953Srrs} 3740163953Srrs 3741188854Srrsstatic void 3742188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3743188854Srrs struct sctp_stream_reset_add_strm *str_add) 3744188854Srrs{ 3745188854Srrs /* 3746188854Srrs * Peer is requesting to add more streams. If its within our 3747188854Srrs * max-streams we will allow it. 3748188854Srrs */ 3749188854Srrs uint16_t num_stream, i; 3750188854Srrs uint32_t seq; 3751189121Srrs struct sctp_association *asoc = &stcb->asoc; 3752189121Srrs struct sctp_queued_to_read *ctl; 3753188854Srrs 3754188854Srrs /* Get the number. */ 3755188854Srrs seq = ntohl(str_add->request_seq); 3756188854Srrs num_stream = ntohs(str_add->number_of_streams); 3757188854Srrs /* Now what would be the new total? */ 3758189121Srrs if (asoc->str_reset_seq_in == seq) { 3759189121Srrs num_stream += stcb->asoc.streamincnt; 3760189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3761189121Srrs /* We must reject it they ask for to many */ 3762189121Srrs denied: 3763189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3764189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3765189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3766189121Srrs } else { 3767189121Srrs /* Ok, we can do that :-) */ 3768189121Srrs struct sctp_stream_in *oldstrm; 3769189121Srrs 3770189121Srrs /* save off the old */ 3771189121Srrs oldstrm = stcb->asoc.strmin; 3772189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3773189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3774189121Srrs SCTP_M_STRMI); 3775189121Srrs if (stcb->asoc.strmin == NULL) { 3776189121Srrs stcb->asoc.strmin = oldstrm; 3777189121Srrs goto denied; 3778189121Srrs } 3779189121Srrs /* copy off the old data */ 3780189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3781189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3782189121Srrs stcb->asoc.strmin[i].stream_no = i; 3783189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3784189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3785189121Srrs /* now anything on those queues? */ 3786189121Srrs while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) { 3787189121Srrs ctl = TAILQ_FIRST(&oldstrm[i].inqueue); 3788189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3789189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3790189121Srrs } 3791189121Srrs } 3792189121Srrs /* Init the new streams */ 3793189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3794189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3795189121Srrs stcb->asoc.strmin[i].stream_no = i; 3796189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3797189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3798189121Srrs } 3799189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3800189121Srrs /* update the size */ 3801189121Srrs stcb->asoc.streamincnt = num_stream; 3802189121Srrs /* Send the ack */ 3803189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3804189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3805189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3806189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3807189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3808189121Srrs } 3809189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3810189121Srrs /* 3811189121Srrs * one seq back, just echo back last action since my 3812189121Srrs * response was lost. 3813189121Srrs */ 3814189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3815189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3816189121Srrs /* 3817189121Srrs * two seq back, just echo back last action since my 3818189121Srrs * response was lost. 3819189121Srrs */ 3820189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3821188854Srrs } else { 3822189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3823188854Srrs 3824188854Srrs } 3825188854Srrs} 3826188854Srrs 3827170992Srrs#ifdef __GNUC__ 3828170992Srrs__attribute__((noinline)) 3829170992Srrs#endif 3830170992Srrs static int 3831170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3832170992Srrs struct sctp_stream_reset_out_req *sr_req) 3833163953Srrs{ 3834163953Srrs int chk_length, param_len, ptype; 3835170992Srrs struct sctp_paramhdr pstore; 3836170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3837170992Srrs 3838163953Srrs uint32_t seq; 3839163953Srrs int num_req = 0; 3840170992Srrs int trunc = 0; 3841163953Srrs struct sctp_tmit_chunk *chk; 3842163953Srrs struct sctp_chunkhdr *ch; 3843163953Srrs struct sctp_paramhdr *ph; 3844166675Srrs int ret_code = 0; 3845166675Srrs int num_param = 0; 3846163953Srrs 3847163953Srrs /* now it may be a reset or a reset-response */ 3848163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3849163953Srrs 3850163953Srrs /* setup for adding the response */ 3851163953Srrs sctp_alloc_a_chunk(stcb, chk); 3852163953Srrs if (chk == NULL) { 3853163953Srrs return (ret_code); 3854163953Srrs } 3855163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3856169208Srrs chk->rec.chunk_id.can_take_data = 0; 3857163953Srrs chk->asoc = &stcb->asoc; 3858163953Srrs chk->no_fr_allowed = 0; 3859163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3860166023Srrs chk->book_size_scale = 0; 3861165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3862163953Srrs if (chk->data == NULL) { 3863163953Srrsstrres_nochunk: 3864163953Srrs if (chk->data) { 3865163953Srrs sctp_m_freem(chk->data); 3866163953Srrs chk->data = NULL; 3867163953Srrs } 3868163953Srrs sctp_free_a_chunk(stcb, chk); 3869163953Srrs return (ret_code); 3870163953Srrs } 3871165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3872163953Srrs 3873163953Srrs /* setup chunk parameters */ 3874163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3875163953Srrs chk->snd_count = 0; 3876163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3877163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3878163953Srrs 3879163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3880163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3881163953Srrs ch->chunk_flags = 0; 3882163953Srrs ch->chunk_length = htons(chk->send_size); 3883165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3884170992Srrs offset += sizeof(struct sctp_chunkhdr); 3885163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3886170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3887170992Srrs if (ph == NULL) 3888170992Srrs break; 3889163953Srrs param_len = ntohs(ph->param_length); 3890163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3891163953Srrs /* bad param */ 3892163953Srrs break; 3893163953Srrs } 3894170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3895170992Srrs (uint8_t *) & cstore); 3896163953Srrs ptype = ntohs(ph->param_type); 3897163953Srrs num_param++; 3898170994Srrs if (param_len > (int)sizeof(cstore)) { 3899170992Srrs trunc = 1; 3900170992Srrs } else { 3901170992Srrs trunc = 0; 3902170992Srrs } 3903170992Srrs 3904163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3905163953Srrs /* hit the max of parameters already sorry.. */ 3906163953Srrs break; 3907163953Srrs } 3908163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3909163953Srrs struct sctp_stream_reset_out_request *req_out; 3910163953Srrs 3911163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3912163953Srrs num_req++; 3913163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3914163953Srrs seq = ntohl(req_out->response_seq); 3915163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3916163953Srrs /* implicit ack */ 3917169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3918163953Srrs } 3919163953Srrs } 3920170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3921188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3922188854Srrs struct sctp_stream_reset_add_strm *str_add; 3923188854Srrs 3924188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3925188854Srrs num_req++; 3926188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 3927163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3928163953Srrs struct sctp_stream_reset_in_request *req_in; 3929163953Srrs 3930163953Srrs num_req++; 3931170992Srrs 3932163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3933170992Srrs 3934170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3935163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3936163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3937163953Srrs 3938163953Srrs num_req++; 3939163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3940170992Srrs 3941163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3942163953Srrs ret_code = 1; 3943163953Srrs goto strres_nochunk; 3944163953Srrs } 3945163953Srrs /* no more */ 3946163953Srrs break; 3947163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3948163953Srrs struct sctp_stream_reset_response *resp; 3949163953Srrs uint32_t result; 3950163953Srrs 3951163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3952163953Srrs seq = ntohl(resp->response_seq); 3953163953Srrs result = ntohl(resp->result); 3954163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3955163953Srrs ret_code = 1; 3956163953Srrs goto strres_nochunk; 3957163953Srrs } 3958163953Srrs } else { 3959163953Srrs break; 3960163953Srrs } 3961170992Srrs offset += SCTP_SIZE32(param_len); 3962163953Srrs chk_length -= SCTP_SIZE32(param_len); 3963163953Srrs } 3964163953Srrs if (num_req == 0) { 3965163953Srrs /* we have no response free the stuff */ 3966163953Srrs goto strres_nochunk; 3967163953Srrs } 3968163953Srrs /* ok we have a chunk to link in */ 3969163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3970163953Srrs chk, 3971163953Srrs sctp_next); 3972163953Srrs stcb->asoc.ctrl_queue_cnt++; 3973163953Srrs return (ret_code); 3974163953Srrs} 3975163953Srrs 3976163953Srrs/* 3977163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3978163953Srrs * to handle this, either we get the whole packet and must disect it 3979163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3980163953Srrs * from a middle box that did the disectting for us. 3981163953Srrs */ 3982163953Srrsstatic void 3983163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3984170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3985163953Srrs{ 3986163953Srrs uint32_t bottle_bw, on_queue; 3987163953Srrs uint16_t trunc_len; 3988163953Srrs unsigned int chlen; 3989163953Srrs unsigned int at; 3990163953Srrs struct sctp_chunk_desc desc; 3991163953Srrs struct sctp_chunkhdr *ch; 3992163953Srrs 3993163953Srrs chlen = ntohs(cp->ch.chunk_length); 3994163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3995163953Srrs /* XXX possible chlen underflow */ 3996163953Srrs if (chlen == 0) { 3997163953Srrs ch = NULL; 3998163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3999163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4000163953Srrs } else { 4001163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4002163953Srrs chlen -= sizeof(struct sctphdr); 4003163953Srrs /* XXX possible chlen underflow */ 4004163953Srrs memset(&desc, 0, sizeof(desc)); 4005163953Srrs } 4006163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4007170781Srrs if (trunc_len > limit) { 4008170781Srrs trunc_len = limit; 4009170781Srrs } 4010163953Srrs /* now the chunks themselves */ 4011163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4012163953Srrs desc.chunk_type = ch->chunk_type; 4013163953Srrs /* get amount we need to move */ 4014163953Srrs at = ntohs(ch->chunk_length); 4015163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4016163953Srrs /* corrupt chunk, maybe at the end? */ 4017163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4018163953Srrs break; 4019163953Srrs } 4020163953Srrs if (trunc_len == 0) { 4021163953Srrs /* we are supposed to have all of it */ 4022163953Srrs if (at > chlen) { 4023163953Srrs /* corrupt skip it */ 4024163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4025163953Srrs break; 4026163953Srrs } 4027163953Srrs } else { 4028163953Srrs /* is there enough of it left ? */ 4029163953Srrs if (desc.chunk_type == SCTP_DATA) { 4030163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4031163953Srrs sizeof(desc.data_bytes))) { 4032163953Srrs break; 4033163953Srrs } 4034163953Srrs } else { 4035163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4036163953Srrs break; 4037163953Srrs } 4038163953Srrs } 4039163953Srrs } 4040163953Srrs if (desc.chunk_type == SCTP_DATA) { 4041163953Srrs /* can we get out the tsn? */ 4042163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4043163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4044163953Srrs 4045163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4046163953Srrs /* yep */ 4047163953Srrs struct sctp_data_chunk *dcp; 4048163953Srrs uint8_t *ddp; 4049163953Srrs unsigned int iii; 4050163953Srrs 4051163953Srrs dcp = (struct sctp_data_chunk *)ch; 4052163953Srrs ddp = (uint8_t *) (dcp + 1); 4053163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4054163953Srrs desc.data_bytes[iii] = ddp[iii]; 4055163953Srrs } 4056163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4057163953Srrs } else { 4058163953Srrs /* nope we are done. */ 4059163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4060163953Srrs break; 4061163953Srrs } 4062163953Srrs } else { 4063163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4064163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4065163953Srrs } 4066163953Srrs 4067163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4068163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4069163953Srrs break; 4070163953Srrs } 4071163953Srrs if (SCTP_SIZE32(at) > chlen) { 4072163953Srrs break; 4073163953Srrs } 4074163953Srrs chlen -= SCTP_SIZE32(at); 4075163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4076163953Srrs /* done, none left */ 4077163953Srrs break; 4078163953Srrs } 4079163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4080163953Srrs } 4081163953Srrs /* Now update any rwnd --- possibly */ 4082163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4083163953Srrs /* From a peer, we get a rwnd report */ 4084163953Srrs uint32_t a_rwnd; 4085163953Srrs 4086163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4087163953Srrs 4088163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4089163953Srrs on_queue = ntohl(cp->current_onq); 4090163953Srrs if (bottle_bw && on_queue) { 4091163953Srrs /* a rwnd report is in here */ 4092163953Srrs if (bottle_bw > on_queue) 4093163953Srrs a_rwnd = bottle_bw - on_queue; 4094163953Srrs else 4095163953Srrs a_rwnd = 0; 4096163953Srrs 4097163953Srrs if (a_rwnd == 0) 4098163953Srrs stcb->asoc.peers_rwnd = 0; 4099163953Srrs else { 4100163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4101163953Srrs stcb->asoc.peers_rwnd = 4102163953Srrs a_rwnd - stcb->asoc.total_flight; 4103163953Srrs } else { 4104163953Srrs stcb->asoc.peers_rwnd = 0; 4105163953Srrs } 4106163953Srrs if (stcb->asoc.peers_rwnd < 4107163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4108163953Srrs /* SWS sender side engages */ 4109163953Srrs stcb->asoc.peers_rwnd = 0; 4110163953Srrs } 4111163953Srrs } 4112163953Srrs } 4113163953Srrs } else { 4114163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4115163953Srrs } 4116163953Srrs 4117163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4118163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4119163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4120163953Srrs (stcb->asoc.sat_network)) { 4121163953Srrs /* 4122163953Srrs * This is debateable but for sat networks it makes sense 4123163953Srrs * Note if a T3 timer has went off, we will prohibit any 4124163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4125163953Srrs */ 4126171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4127171440Srrs net, cp, &bottle_bw, &on_queue); 4128163953Srrs } 4129163953Srrs} 4130163953Srrs 4131163953Srrs/* 4132163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4133163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4134163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4135163953Srrs * length of the complete packet outputs: - length: modified to remaining 4136163953Srrs * length after control processing - netp: modified to new sctp_nets after 4137163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4138163953Srrs * bad packet,...) otherwise return the tcb for this packet 4139163953Srrs */ 4140170099Srrs#ifdef __GNUC__ 4141170099Srrs__attribute__((noinline)) 4142170099Srrs#endif 4143170099Srrs static struct sctp_tcb * 4144170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4145170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4146170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4147179157Srrs uint32_t vrf_id, uint16_t port) 4148163953Srrs{ 4149163953Srrs struct sctp_association *asoc; 4150163953Srrs uint32_t vtag_in; 4151163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4152170140Srrs uint32_t chk_length; 4153163953Srrs int ret; 4154168299Srrs int abort_no_unlock = 0; 4155163953Srrs 4156163953Srrs /* 4157163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4158163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4159163953Srrs * until we get into jumbo grams and such.. 4160163953Srrs */ 4161166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4162163953Srrs struct sctp_tcb *locked_tcb = stcb; 4163163953Srrs int got_auth = 0; 4164163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4165163953Srrs int auth_skipped = 0; 4166171990Srrs int asconf_cnt = 0; 4167163953Srrs 4168172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4169172090Srrs struct socket *so; 4170172090Srrs 4171172090Srrs#endif 4172172090Srrs 4173169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4174169420Srrs iphlen, *offset, length, stcb); 4175163953Srrs 4176163953Srrs /* validate chunk header length... */ 4177163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4178170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4179170056Srrs ntohs(ch->chunk_length)); 4180170091Srrs if (locked_tcb) { 4181170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4182170091Srrs } 4183163953Srrs return (NULL); 4184163953Srrs } 4185163953Srrs /* 4186163953Srrs * validate the verification tag 4187163953Srrs */ 4188163953Srrs vtag_in = ntohl(sh->v_tag); 4189163953Srrs 4190165220Srrs if (locked_tcb) { 4191165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4192165220Srrs } 4193163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4194170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4195170056Srrs ntohs(ch->chunk_length), vtag_in); 4196163953Srrs if (vtag_in != 0) { 4197163953Srrs /* protocol error- silently discard... */ 4198163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4199169378Srrs if (locked_tcb) { 4200163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4201169378Srrs } 4202163953Srrs return (NULL); 4203163953Srrs } 4204163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4205163953Srrs /* 4206163953Srrs * If there is no stcb, skip the AUTH chunk and process 4207163953Srrs * later after a stcb is found (to validate the lookup was 4208163953Srrs * valid. 4209163953Srrs */ 4210163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4211179783Srrs (stcb == NULL) && 4212179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4213163953Srrs /* save this chunk for later processing */ 4214163953Srrs auth_skipped = 1; 4215163953Srrs auth_offset = *offset; 4216163953Srrs auth_len = ntohs(ch->chunk_length); 4217163953Srrs 4218163953Srrs /* (temporarily) move past this chunk */ 4219163953Srrs *offset += SCTP_SIZE32(auth_len); 4220163953Srrs if (*offset >= length) { 4221163953Srrs /* no more data left in the mbuf chain */ 4222163953Srrs *offset = length; 4223170091Srrs if (locked_tcb) { 4224170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4225170091Srrs } 4226163953Srrs return (NULL); 4227163953Srrs } 4228163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4229163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4230163953Srrs } 4231169420Srrs if (ch == NULL) { 4232169420Srrs /* Help */ 4233169420Srrs *offset = length; 4234170091Srrs if (locked_tcb) { 4235170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4236170091Srrs } 4237169420Srrs return (NULL); 4238169420Srrs } 4239163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4240163953Srrs goto process_control_chunks; 4241163953Srrs } 4242163953Srrs /* 4243163953Srrs * first check if it's an ASCONF with an unknown src addr we 4244163953Srrs * need to look inside to find the association 4245163953Srrs */ 4246163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4247171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4248171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4249171990Srrs 4250163953Srrs /* inp's refcount may be reduced */ 4251163953Srrs SCTP_INP_INCR_REF(inp); 4252163953Srrs 4253171990Srrs asconf_offset = *offset; 4254171990Srrs do { 4255171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4256171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4257171990Srrs break; 4258171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4259185694Srrs *offset, sh, &inp, netp, vrf_id); 4260171990Srrs if (stcb != NULL) 4261171990Srrs break; 4262171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4263171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4264171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4265171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4266163953Srrs if (stcb == NULL) { 4267163953Srrs /* 4268163953Srrs * reduce inp's refcount if not reduced in 4269163953Srrs * sctp_findassociation_ep_asconf(). 4270163953Srrs */ 4271163953Srrs SCTP_INP_DECR_REF(inp); 4272171990Srrs } else { 4273171990Srrs locked_tcb = stcb; 4274163953Srrs } 4275171990Srrs 4276163953Srrs /* now go back and verify any auth chunk to be sure */ 4277163953Srrs if (auth_skipped && (stcb != NULL)) { 4278163953Srrs struct sctp_auth_chunk *auth; 4279163953Srrs 4280163953Srrs auth = (struct sctp_auth_chunk *) 4281163953Srrs sctp_m_getptr(m, auth_offset, 4282163953Srrs auth_len, chunk_buf); 4283163953Srrs got_auth = 1; 4284163953Srrs auth_skipped = 0; 4285169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4286163953Srrs auth_offset)) { 4287163953Srrs /* auth HMAC failed so dump it */ 4288163953Srrs *offset = length; 4289170091Srrs if (locked_tcb) { 4290170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4291170091Srrs } 4292163953Srrs return (NULL); 4293163953Srrs } else { 4294163953Srrs /* remaining chunks are HMAC checked */ 4295163953Srrs stcb->asoc.authenticated = 1; 4296163953Srrs } 4297163953Srrs } 4298163953Srrs } 4299163953Srrs if (stcb == NULL) { 4300163953Srrs /* no association, so it's out of the blue... */ 4301169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4302179157Srrs vrf_id, port); 4303163953Srrs *offset = length; 4304169378Srrs if (locked_tcb) { 4305163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4306169378Srrs } 4307163953Srrs return (NULL); 4308163953Srrs } 4309163953Srrs asoc = &stcb->asoc; 4310163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4311163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4312163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4313163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4314163953Srrs if ((vtag_in == asoc->my_vtag) || 4315163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4316163953Srrs (vtag_in == asoc->peer_vtag))) { 4317163953Srrs /* this is valid */ 4318163953Srrs } else { 4319163953Srrs /* drop this packet... */ 4320163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4321169378Srrs if (locked_tcb) { 4322163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4323169378Srrs } 4324163953Srrs return (NULL); 4325163953Srrs } 4326163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4327163953Srrs if (vtag_in != asoc->my_vtag) { 4328163953Srrs /* 4329163953Srrs * this could be a stale SHUTDOWN-ACK or the 4330163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4331163953Srrs * is still hung; we have started a new asoc 4332163953Srrs * but it won't complete until the shutdown 4333163953Srrs * is completed 4334163953Srrs */ 4335169378Srrs if (locked_tcb) { 4336163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4337169378Srrs } 4338163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4339179157Srrs NULL, vrf_id, port); 4340163953Srrs return (NULL); 4341163953Srrs } 4342163953Srrs } else { 4343163953Srrs /* for all other chunks, vtag must match */ 4344163953Srrs if (vtag_in != asoc->my_vtag) { 4345163953Srrs /* invalid vtag... */ 4346169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4347169420Srrs "invalid vtag: %xh, expect %xh\n", 4348169420Srrs vtag_in, asoc->my_vtag); 4349163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4350169378Srrs if (locked_tcb) { 4351163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4352169378Srrs } 4353163953Srrs *offset = length; 4354163953Srrs return (NULL); 4355163953Srrs } 4356163953Srrs } 4357163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4358163953Srrs /* 4359163953Srrs * process all control chunks... 4360163953Srrs */ 4361163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4362185694Srrs /* EY */ 4363185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4364163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4365163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4366163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4367179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4368171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4369171943Srrs stcb->asoc.overall_error_count, 4370171943Srrs 0, 4371171943Srrs SCTP_FROM_SCTP_INPUT, 4372171943Srrs __LINE__); 4373171943Srrs } 4374163953Srrs stcb->asoc.overall_error_count = 0; 4375163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4376163953Srrs *netp); 4377163953Srrs } 4378163953Srrsprocess_control_chunks: 4379163953Srrs while (IS_SCTP_CONTROL(ch)) { 4380163953Srrs /* validate chunk length */ 4381163953Srrs chk_length = ntohs(ch->chunk_length); 4382169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4383169420Srrs ch->chunk_type, chk_length); 4384170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4385170140Srrs if (chk_length < sizeof(*ch) || 4386170140Srrs (*offset + (int)chk_length) > length) { 4387163953Srrs *offset = length; 4388169378Srrs if (locked_tcb) { 4389163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4390169378Srrs } 4391163953Srrs return (NULL); 4392163953Srrs } 4393163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4394163953Srrs /* 4395163953Srrs * INIT-ACK only gets the init ack "header" portion only 4396163953Srrs * because we don't have to process the peer's COOKIE. All 4397163953Srrs * others get a complete chunk. 4398163953Srrs */ 4399169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4400169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4401163953Srrs /* get an init-ack chunk */ 4402163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4403163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4404163953Srrs if (ch == NULL) { 4405163953Srrs *offset = length; 4406169378Srrs if (locked_tcb) { 4407163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4408169378Srrs } 4409163953Srrs return (NULL); 4410163953Srrs } 4411170642Srrs } else { 4412170786Srrs /* For cookies and all other chunks. */ 4413169208Srrs if (chk_length > sizeof(chunk_buf)) { 4414169208Srrs /* 4415169208Srrs * use just the size of the chunk buffer so 4416170642Srrs * the front part of our chunks fit in 4417170642Srrs * contiguous space up to the chunk buffer 4418170642Srrs * size (508 bytes). For chunks that need to 4419170786Srrs * get more than that they must use the 4420170642Srrs * sctp_m_getptr() function or other means 4421170786Srrs * (e.g. know how to parse mbuf chains). 4422170786Srrs * Cookies do this already. 4423169208Srrs */ 4424169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4425169208Srrs (sizeof(chunk_buf) - 4), 4426169208Srrs chunk_buf); 4427169208Srrs if (ch == NULL) { 4428169208Srrs *offset = length; 4429169378Srrs if (locked_tcb) { 4430169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4431169378Srrs } 4432169208Srrs return (NULL); 4433169208Srrs } 4434169208Srrs } else { 4435169208Srrs /* We can fit it all */ 4436170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4437170642Srrs chk_length, chunk_buf); 4438170642Srrs if (ch == NULL) { 4439170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4440170642Srrs *offset = length; 4441170642Srrs if (locked_tcb) { 4442170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4443169208Srrs } 4444170642Srrs return (NULL); 4445163953Srrs } 4446163953Srrs } 4447163953Srrs } 4448163953Srrs num_chunks++; 4449163953Srrs /* Save off the last place we got a control from */ 4450163953Srrs if (stcb != NULL) { 4451169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4452163953Srrs /* 4453163953Srrs * allow last_control to be NULL if 4454163953Srrs * ASCONF... ASCONF processing will find the 4455163953Srrs * right net later 4456163953Srrs */ 4457169420Srrs if ((netp != NULL) && (*netp != NULL)) 4458169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4459163953Srrs } 4460163953Srrs } 4461163953Srrs#ifdef SCTP_AUDITING_ENABLED 4462163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4463163953Srrs#endif 4464163953Srrs 4465163953Srrs /* check to see if this chunk required auth, but isn't */ 4466179783Srrs if ((stcb != NULL) && 4467179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4468179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4469163953Srrs !stcb->asoc.authenticated) { 4470163953Srrs /* "silently" ignore */ 4471163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4472163953Srrs goto next_chunk; 4473163953Srrs } 4474163953Srrs switch (ch->chunk_type) { 4475163953Srrs case SCTP_INITIATION: 4476163953Srrs /* must be first and only chunk */ 4477169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4478163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4479163953Srrs /* We are not interested anymore? */ 4480163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4481163953Srrs /* 4482163953Srrs * collision case where we are 4483163953Srrs * sending to them too 4484163953Srrs */ 4485163953Srrs ; 4486163953Srrs } else { 4487169378Srrs if (locked_tcb) { 4488163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4489169378Srrs } 4490163953Srrs *offset = length; 4491163953Srrs return (NULL); 4492163953Srrs } 4493163953Srrs } 4494171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4495171990Srrs (num_chunks > 1) || 4496179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4497163953Srrs *offset = length; 4498169378Srrs if (locked_tcb) { 4499163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4500169378Srrs } 4501163953Srrs return (NULL); 4502163953Srrs } 4503163953Srrs if ((stcb != NULL) && 4504163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4505163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4506163953Srrs sctp_send_shutdown_ack(stcb, 4507163953Srrs stcb->asoc.primary_destination); 4508163953Srrs *offset = length; 4509172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4510169378Srrs if (locked_tcb) { 4511163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4512169378Srrs } 4513163953Srrs return (NULL); 4514163953Srrs } 4515169420Srrs if (netp) { 4516169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4517169420Srrs (struct sctp_init_chunk *)ch, inp, 4518179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4519169420Srrs } 4520168299Srrs if (abort_no_unlock) 4521168299Srrs return (NULL); 4522168299Srrs 4523163953Srrs *offset = length; 4524169378Srrs if (locked_tcb) { 4525163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4526169378Srrs } 4527163953Srrs return (NULL); 4528163953Srrs break; 4529170642Srrs case SCTP_PAD_CHUNK: 4530170642Srrs break; 4531163953Srrs case SCTP_INITIATION_ACK: 4532163953Srrs /* must be first and only chunk */ 4533169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4534163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4535163953Srrs /* We are not interested anymore */ 4536163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4537163953Srrs ; 4538163953Srrs } else { 4539169378Srrs if (locked_tcb) { 4540163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4541169378Srrs } 4542163953Srrs *offset = length; 4543163953Srrs if (stcb) { 4544172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4545172090Srrs so = SCTP_INP_SO(inp); 4546172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4547172090Srrs SCTP_TCB_UNLOCK(stcb); 4548172090Srrs SCTP_SOCKET_LOCK(so, 1); 4549172090Srrs SCTP_TCB_LOCK(stcb); 4550172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4551172090Srrs#endif 4552171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4553172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4554172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4555172090Srrs#endif 4556163953Srrs } 4557163953Srrs return (NULL); 4558163953Srrs } 4559163953Srrs } 4560163953Srrs if ((num_chunks > 1) || 4561179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4562163953Srrs *offset = length; 4563169378Srrs if (locked_tcb) { 4564163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4565169378Srrs } 4566163953Srrs return (NULL); 4567163953Srrs } 4568169420Srrs if ((netp) && (*netp)) { 4569169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4570170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4571169420Srrs } else { 4572169420Srrs ret = -1; 4573169420Srrs } 4574163953Srrs /* 4575163953Srrs * Special case, I must call the output routine to 4576163953Srrs * get the cookie echoed 4577163953Srrs */ 4578168299Srrs if (abort_no_unlock) 4579168299Srrs return (NULL); 4580168299Srrs 4581163953Srrs if ((stcb) && ret == 0) 4582172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4583163953Srrs *offset = length; 4584169378Srrs if (locked_tcb) { 4585163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4586169378Srrs } 4587163953Srrs return (NULL); 4588163953Srrs break; 4589163953Srrs case SCTP_SELECTIVE_ACK: 4590169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4591163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4592163953Srrs { 4593163953Srrs struct sctp_sack_chunk *sack; 4594163953Srrs int abort_now = 0; 4595163953Srrs uint32_t a_rwnd, cum_ack; 4596202526Stuexen uint16_t num_seg, num_dup; 4597202526Stuexen uint8_t flags; 4598202526Stuexen int offset_seg, offset_dup; 4599163953Srrs int nonce_sum_flag; 4600163953Srrs 4601202526Stuexen if (stcb == NULL) { 4602202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4603202526Stuexen break; 4604169208Srrs } 4605202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4606202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4607202526Stuexen break; 4608202526Stuexen } 4609171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4610171990Srrs /*- 4611171990Srrs * If we have sent a shutdown-ack, we will pay no 4612171990Srrs * attention to a sack sent in to us since 4613171990Srrs * we don't care anymore. 4614171990Srrs */ 4615184883Srrs break; 4616171990Srrs } 4617163953Srrs sack = (struct sctp_sack_chunk *)ch; 4618202526Stuexen flags = ch->chunk_flags; 4619202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4620163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4621163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4622202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4623163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4624202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4625202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4626202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4627202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4628202526Stuexen break; 4629202526Stuexen } 4630202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4631202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4632170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4633202526Stuexen cum_ack, num_seg, a_rwnd); 4634163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4635163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4636163953Srrs (num_seg == 0) && 4637163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4638163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4639163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4640163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4641163953Srrs ) { 4642163953Srrs /* 4643163953Srrs * We have a SIMPLE sack having no 4644163953Srrs * prior segments and data on sent 4645163953Srrs * queue to be acked.. Use the 4646163953Srrs * faster path sack processing. We 4647163953Srrs * also allow window update sacks 4648163953Srrs * with no missing segments to go 4649163953Srrs * this way too. 4650163953Srrs */ 4651169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4652169208Srrs &abort_now); 4653163953Srrs } else { 4654169420Srrs if (netp && *netp) 4655202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4656202526Stuexen stcb, *netp, 4657202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4658202526Stuexen cum_ack, a_rwnd); 4659163953Srrs } 4660202526Stuexen if (abort_now) { 4661202526Stuexen /* ABORT signal from sack processing */ 4662202526Stuexen *offset = length; 4663202526Stuexen return (NULL); 4664202526Stuexen } 4665185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4666185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4667185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4668185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4669185694Srrs } 4670163953Srrs } 4671163953Srrs break; 4672185694Srrs /* 4673185694Srrs * EY - nr_sack: If the received chunk is an 4674185694Srrs * nr_sack chunk 4675185694Srrs */ 4676185694Srrs case SCTP_NR_SELECTIVE_ACK: 4677185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4678185694Srrs SCTP_STAT_INCR(sctps_recvsacks); 4679185694Srrs { 4680185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4681185694Srrs int abort_now = 0; 4682185694Srrs uint32_t a_rwnd, cum_ack; 4683202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4684202526Stuexen uint8_t flags; 4685202526Stuexen int offset_seg, offset_dup; 4686194355Srrs int nonce_sum_flag; 4687185694Srrs 4688185694Srrs /* 4689185694Srrs * EY nr_sacks have not been negotiated but 4690185694Srrs * the peer end sent an nr_sack, silently 4691185694Srrs * discard the chunk 4692185694Srrs */ 4693202526Stuexen if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && 4694202526Stuexen stcb->asoc.peer_supports_nr_sack)) { 4695185694Srrs goto unknown_chunk; 4696185694Srrs } 4697202526Stuexen if (stcb == NULL) { 4698202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4699202526Stuexen break; 4700202526Stuexen } 4701202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4702202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4703202526Stuexen break; 4704202526Stuexen } 4705185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4706185694Srrs /*- 4707185694Srrs * If we have sent a shutdown-ack, we will pay no 4708185694Srrs * attention to a sack sent in to us since 4709185694Srrs * we don't care anymore. 4710185694Srrs */ 4711202526Stuexen break; 4712185694Srrs } 4713185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4714202526Stuexen flags = ch->chunk_flags; 4715202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4716185694Srrs 4717185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4718185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4719185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4720202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4721185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4722202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4723202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4724202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4725202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4726202526Stuexen break; 4727202526Stuexen } 4728202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4729202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4730185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4731202526Stuexen cum_ack, num_seg, a_rwnd); 4732185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4733185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4734202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4735185694Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4736185694Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4737185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4738202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4739185694Srrs /* 4740185694Srrs * We have a SIMPLE sack having no 4741185694Srrs * prior segments and data on sent 4742202526Stuexen * queue to be acked. Use the faster 4743202526Stuexen * path sack processing. We also 4744202526Stuexen * allow window update sacks with no 4745202526Stuexen * missing segments to go this way 4746202526Stuexen * too. 4747185694Srrs */ 4748202526Stuexen sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4749185694Srrs &abort_now); 4750185694Srrs } else { 4751185694Srrs if (netp && *netp) 4752202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4753202526Stuexen stcb, *netp, 4754202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4755202526Stuexen cum_ack, a_rwnd); 4756185694Srrs } 4757202526Stuexen if (abort_now) { 4758202526Stuexen /* ABORT signal from sack processing */ 4759202526Stuexen *offset = length; 4760202526Stuexen return (NULL); 4761202526Stuexen } 4762185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4763185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4764185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4765185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4766185694Srrs } 4767185694Srrs } 4768185694Srrs break; 4769185694Srrs 4770163953Srrs case SCTP_HEARTBEAT_REQUEST: 4771169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4772169420Srrs if ((stcb) && netp && *netp) { 4773169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4774169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4775169420Srrs chk_length, *netp); 4776169420Srrs 4777169420Srrs /* He's alive so give him credit */ 4778179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4779171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4780171943Srrs stcb->asoc.overall_error_count, 4781171943Srrs 0, 4782171943Srrs SCTP_FROM_SCTP_INPUT, 4783171943Srrs __LINE__); 4784171943Srrs } 4785169420Srrs stcb->asoc.overall_error_count = 0; 4786163953Srrs } 4787163953Srrs break; 4788163953Srrs case SCTP_HEARTBEAT_ACK: 4789169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4790169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4791169208Srrs /* Its not ours */ 4792169352Srrs *offset = length; 4793169378Srrs if (locked_tcb) { 4794169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4795169378Srrs } 4796169208Srrs return (NULL); 4797169208Srrs } 4798163953Srrs /* He's alive so give him credit */ 4799179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4800171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4801171943Srrs stcb->asoc.overall_error_count, 4802171943Srrs 0, 4803171943Srrs SCTP_FROM_SCTP_INPUT, 4804171943Srrs __LINE__); 4805171943Srrs } 4806163953Srrs stcb->asoc.overall_error_count = 0; 4807163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4808169420Srrs if (netp && *netp) 4809169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4810169420Srrs stcb, *netp); 4811163953Srrs break; 4812163953Srrs case SCTP_ABORT_ASSOCIATION: 4813170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4814170091Srrs stcb); 4815169420Srrs if ((stcb) && netp && *netp) 4816169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4817169420Srrs stcb, *netp); 4818163953Srrs *offset = length; 4819163953Srrs return (NULL); 4820163953Srrs break; 4821163953Srrs case SCTP_SHUTDOWN: 4822170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4823170091Srrs stcb); 4824169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4825169208Srrs *offset = length; 4826169378Srrs if (locked_tcb) { 4827169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4828169378Srrs } 4829169208Srrs return (NULL); 4830169420Srrs } 4831169420Srrs if (netp && *netp) { 4832163953Srrs int abort_flag = 0; 4833163953Srrs 4834163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4835163953Srrs stcb, *netp, &abort_flag); 4836163953Srrs if (abort_flag) { 4837163953Srrs *offset = length; 4838163953Srrs return (NULL); 4839163953Srrs } 4840163953Srrs } 4841163953Srrs break; 4842163953Srrs case SCTP_SHUTDOWN_ACK: 4843170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4844169420Srrs if ((stcb) && (netp) && (*netp)) 4845169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4846163953Srrs *offset = length; 4847163953Srrs return (NULL); 4848163953Srrs break; 4849169420Srrs 4850163953Srrs case SCTP_OPERATION_ERROR: 4851169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4852169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4853169420Srrs 4854163953Srrs *offset = length; 4855163953Srrs return (NULL); 4856163953Srrs } 4857163953Srrs break; 4858163953Srrs case SCTP_COOKIE_ECHO: 4859169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4860170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4861163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4862163953Srrs ; 4863163953Srrs } else { 4864169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4865163953Srrs /* We are not interested anymore */ 4866163953Srrs *offset = length; 4867163953Srrs return (NULL); 4868163953Srrs } 4869163953Srrs } 4870163953Srrs /* 4871163953Srrs * First are we accepting? We do this again here 4872169420Srrs * sincen it is possible that a previous endpoint 4873169420Srrs * WAS listening responded to a INIT-ACK and then 4874163953Srrs * closed. We opened and bound.. and are now no 4875163953Srrs * longer listening. 4876163953Srrs */ 4877172703Srrs 4878172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4879172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4880179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4881163953Srrs struct mbuf *oper; 4882163953Srrs struct sctp_paramhdr *phdr; 4883163953Srrs 4884172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4885172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4886172703Srrs if (oper) { 4887172703Srrs SCTP_BUF_LEN(oper) = 4888172703Srrs sizeof(struct sctp_paramhdr); 4889172703Srrs phdr = mtod(oper, 4890172703Srrs struct sctp_paramhdr *); 4891172703Srrs phdr->param_type = 4892172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4893172703Srrs phdr->param_length = 4894172703Srrs htons(sizeof(struct sctp_paramhdr)); 4895163953Srrs } 4896172703Srrs sctp_abort_association(inp, stcb, m, 4897179157Srrs iphlen, sh, oper, vrf_id, port); 4898163953Srrs } 4899172703Srrs *offset = length; 4900172703Srrs return (NULL); 4901172703Srrs } else { 4902163953Srrs struct mbuf *ret_buf; 4903165220Srrs struct sctp_inpcb *linp; 4904163953Srrs 4905169420Srrs if (stcb) { 4906165220Srrs linp = NULL; 4907169420Srrs } else { 4908165220Srrs linp = inp; 4909169420Srrs } 4910165220Srrs 4911169420Srrs if (linp) { 4912165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4913169420Srrs } 4914169420Srrs if (netp) { 4915169420Srrs ret_buf = 4916169420Srrs sctp_handle_cookie_echo(m, iphlen, 4917169420Srrs *offset, sh, 4918169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4919169420Srrs &inp, &stcb, netp, 4920169420Srrs auth_skipped, 4921169420Srrs auth_offset, 4922169420Srrs auth_len, 4923169420Srrs &locked_tcb, 4924179157Srrs vrf_id, 4925179157Srrs port); 4926169420Srrs } else { 4927169420Srrs ret_buf = NULL; 4928169420Srrs } 4929169420Srrs if (linp) { 4930165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4931169420Srrs } 4932163953Srrs if (ret_buf == NULL) { 4933163953Srrs if (locked_tcb) { 4934163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4935163953Srrs } 4936169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4937169420Srrs "GAK, null buffer\n"); 4938163953Srrs auth_skipped = 0; 4939163953Srrs *offset = length; 4940163953Srrs return (NULL); 4941163953Srrs } 4942163953Srrs /* if AUTH skipped, see if it verified... */ 4943163953Srrs if (auth_skipped) { 4944163953Srrs got_auth = 1; 4945163953Srrs auth_skipped = 0; 4946163953Srrs } 4947163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4948163953Srrs /* 4949163953Srrs * Restart the timer if we have 4950163953Srrs * pending data 4951163953Srrs */ 4952163953Srrs struct sctp_tmit_chunk *chk; 4953163953Srrs 4954163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4955163953Srrs if (chk) { 4956163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4957163953Srrs stcb->sctp_ep, stcb, 4958163953Srrs chk->whoTo); 4959163953Srrs } 4960163953Srrs } 4961163953Srrs } 4962163953Srrs break; 4963163953Srrs case SCTP_COOKIE_ACK: 4964170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4965169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4966169378Srrs if (locked_tcb) { 4967169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4968169378Srrs } 4969169352Srrs return (NULL); 4970169352Srrs } 4971163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4972163953Srrs /* We are not interested anymore */ 4973163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4974163953Srrs ; 4975169420Srrs } else if (stcb) { 4976172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4977172090Srrs so = SCTP_INP_SO(inp); 4978172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4979172090Srrs SCTP_TCB_UNLOCK(stcb); 4980172090Srrs SCTP_SOCKET_LOCK(so, 1); 4981172090Srrs SCTP_TCB_LOCK(stcb); 4982172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4983172090Srrs#endif 4984171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4985172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4986172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4987172090Srrs#endif 4988163953Srrs *offset = length; 4989163953Srrs return (NULL); 4990163953Srrs } 4991163953Srrs } 4992163953Srrs /* He's alive so give him credit */ 4993169420Srrs if ((stcb) && netp && *netp) { 4994179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4995171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4996171943Srrs stcb->asoc.overall_error_count, 4997171943Srrs 0, 4998171943Srrs SCTP_FROM_SCTP_INPUT, 4999171943Srrs __LINE__); 5000171943Srrs } 5001169378Srrs stcb->asoc.overall_error_count = 0; 5002169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5003169378Srrs } 5004163953Srrs break; 5005163953Srrs case SCTP_ECN_ECHO: 5006169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5007163953Srrs /* He's alive so give him credit */ 5008169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5009169208Srrs /* Its not ours */ 5010169378Srrs if (locked_tcb) { 5011169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5012169378Srrs } 5013169208Srrs *offset = length; 5014169208Srrs return (NULL); 5015169208Srrs } 5016169378Srrs if (stcb) { 5017179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5018171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5019171943Srrs stcb->asoc.overall_error_count, 5020171943Srrs 0, 5021171943Srrs SCTP_FROM_SCTP_INPUT, 5022171943Srrs __LINE__); 5023171943Srrs } 5024169378Srrs stcb->asoc.overall_error_count = 0; 5025169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5026169378Srrs stcb); 5027169378Srrs } 5028163953Srrs break; 5029163953Srrs case SCTP_ECN_CWR: 5030169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5031163953Srrs /* He's alive so give him credit */ 5032169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5033169208Srrs /* Its not ours */ 5034169378Srrs if (locked_tcb) { 5035169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5036169378Srrs } 5037169208Srrs *offset = length; 5038169208Srrs return (NULL); 5039169208Srrs } 5040169378Srrs if (stcb) { 5041179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5042171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5043171943Srrs stcb->asoc.overall_error_count, 5044171943Srrs 0, 5045171943Srrs SCTP_FROM_SCTP_INPUT, 5046171943Srrs __LINE__); 5047171943Srrs } 5048169378Srrs stcb->asoc.overall_error_count = 0; 5049169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 5050169378Srrs } 5051163953Srrs break; 5052163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5053170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5054163953Srrs /* must be first and only chunk */ 5055163953Srrs if ((num_chunks > 1) || 5056170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5057163953Srrs *offset = length; 5058169378Srrs if (locked_tcb) { 5059163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5060169378Srrs } 5061163953Srrs return (NULL); 5062163953Srrs } 5063169420Srrs if ((stcb) && netp && *netp) { 5064169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5065169378Srrs stcb, *netp); 5066169378Srrs } 5067163953Srrs *offset = length; 5068163953Srrs return (NULL); 5069163953Srrs break; 5070163953Srrs case SCTP_ASCONF: 5071169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5072163953Srrs /* He's alive so give him credit */ 5073169378Srrs if (stcb) { 5074179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5075171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5076171943Srrs stcb->asoc.overall_error_count, 5077171943Srrs 0, 5078171943Srrs SCTP_FROM_SCTP_INPUT, 5079171943Srrs __LINE__); 5080171943Srrs } 5081169378Srrs stcb->asoc.overall_error_count = 0; 5082169378Srrs sctp_handle_asconf(m, *offset, 5083171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5084171990Srrs asconf_cnt++; 5085169378Srrs } 5086163953Srrs break; 5087163953Srrs case SCTP_ASCONF_ACK: 5088169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5089169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5090169208Srrs /* Its not ours */ 5091169378Srrs if (locked_tcb) { 5092169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5093169378Srrs } 5094169208Srrs *offset = length; 5095169208Srrs return (NULL); 5096169208Srrs } 5097169420Srrs if ((stcb) && netp && *netp) { 5098169378Srrs /* He's alive so give him credit */ 5099179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5100171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5101171943Srrs stcb->asoc.overall_error_count, 5102171943Srrs 0, 5103171943Srrs SCTP_FROM_SCTP_INPUT, 5104171943Srrs __LINE__); 5105171943Srrs } 5106169378Srrs stcb->asoc.overall_error_count = 0; 5107169378Srrs sctp_handle_asconf_ack(m, *offset, 5108172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5109172190Srrs if (abort_no_unlock) 5110172190Srrs return (NULL); 5111169378Srrs } 5112163953Srrs break; 5113163953Srrs case SCTP_FORWARD_CUM_TSN: 5114169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5115169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5116169208Srrs /* Its not ours */ 5117169378Srrs if (locked_tcb) { 5118169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5119169378Srrs } 5120169208Srrs *offset = length; 5121169208Srrs return (NULL); 5122169208Srrs } 5123163953Srrs /* He's alive so give him credit */ 5124169378Srrs if (stcb) { 5125163953Srrs int abort_flag = 0; 5126163953Srrs 5127163953Srrs stcb->asoc.overall_error_count = 0; 5128179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5129171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5130171943Srrs stcb->asoc.overall_error_count, 5131171943Srrs 0, 5132171943Srrs SCTP_FROM_SCTP_INPUT, 5133171943Srrs __LINE__); 5134171943Srrs } 5135163953Srrs *fwd_tsn_seen = 1; 5136163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5137163953Srrs /* We are not interested anymore */ 5138172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5139172090Srrs so = SCTP_INP_SO(inp); 5140172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5141172090Srrs SCTP_TCB_UNLOCK(stcb); 5142172090Srrs SCTP_SOCKET_LOCK(so, 1); 5143172090Srrs SCTP_TCB_LOCK(stcb); 5144172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5145172090Srrs#endif 5146171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5147172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5148172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5149172090Srrs#endif 5150163953Srrs *offset = length; 5151163953Srrs return (NULL); 5152163953Srrs } 5153163953Srrs sctp_handle_forward_tsn(stcb, 5154170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5155163953Srrs if (abort_flag) { 5156163953Srrs *offset = length; 5157163953Srrs return (NULL); 5158163953Srrs } else { 5159179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5160171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5161171943Srrs stcb->asoc.overall_error_count, 5162171943Srrs 0, 5163171943Srrs SCTP_FROM_SCTP_INPUT, 5164171943Srrs __LINE__); 5165171943Srrs } 5166163953Srrs stcb->asoc.overall_error_count = 0; 5167163953Srrs } 5168163953Srrs 5169163953Srrs } 5170163953Srrs break; 5171163953Srrs case SCTP_STREAM_RESET: 5172169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5173169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5174169208Srrs /* Its not ours */ 5175169378Srrs if (locked_tcb) { 5176169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5177169378Srrs } 5178169208Srrs *offset = length; 5179169208Srrs return (NULL); 5180169208Srrs } 5181163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5182163953Srrs /* We are not interested anymore */ 5183172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5184172090Srrs so = SCTP_INP_SO(inp); 5185172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5186172090Srrs SCTP_TCB_UNLOCK(stcb); 5187172090Srrs SCTP_SOCKET_LOCK(so, 1); 5188172090Srrs SCTP_TCB_LOCK(stcb); 5189172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5190172090Srrs#endif 5191171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5192172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5193172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5194172090Srrs#endif 5195163953Srrs *offset = length; 5196163953Srrs return (NULL); 5197163953Srrs } 5198169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5199169420Srrs /* 5200169420Srrs * hmm, peer should have announced this, but 5201169420Srrs * we will turn it on since he is sending us 5202169420Srrs * a stream reset. 5203169420Srrs */ 5204169420Srrs stcb->asoc.peer_supports_strreset = 1; 5205163953Srrs } 5206170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5207169420Srrs /* stop processing */ 5208169420Srrs *offset = length; 5209169420Srrs return (NULL); 5210169420Srrs } 5211163953Srrs break; 5212163953Srrs case SCTP_PACKET_DROPPED: 5213169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5214163953Srrs /* re-get it all please */ 5215169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5216169208Srrs /* Its not ours */ 5217169378Srrs if (locked_tcb) { 5218169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5219169378Srrs } 5220169208Srrs *offset = length; 5221169208Srrs return (NULL); 5222169208Srrs } 5223169420Srrs if (ch && (stcb) && netp && (*netp)) { 5224169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5225170781Srrs stcb, *netp, 5226170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5227170781Srrs 5228169378Srrs } 5229163953Srrs break; 5230163953Srrs 5231163953Srrs case SCTP_AUTHENTICATION: 5232169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5233179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5234163953Srrs goto unknown_chunk; 5235163953Srrs 5236163953Srrs if (stcb == NULL) { 5237163953Srrs /* save the first AUTH for later processing */ 5238163953Srrs if (auth_skipped == 0) { 5239163953Srrs auth_offset = *offset; 5240163953Srrs auth_len = chk_length; 5241163953Srrs auth_skipped = 1; 5242163953Srrs } 5243163953Srrs /* skip this chunk (temporarily) */ 5244163953Srrs goto next_chunk; 5245163953Srrs } 5246169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5247169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5248169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5249169208Srrs /* Its not ours */ 5250169378Srrs if (locked_tcb) { 5251169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5252169378Srrs } 5253169208Srrs *offset = length; 5254169208Srrs return (NULL); 5255169208Srrs } 5256163953Srrs if (got_auth == 1) { 5257163953Srrs /* skip this chunk... it's already auth'd */ 5258163953Srrs goto next_chunk; 5259163953Srrs } 5260163953Srrs got_auth = 1; 5261169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5262163953Srrs m, *offset)) { 5263163953Srrs /* auth HMAC failed so dump the packet */ 5264163953Srrs *offset = length; 5265163953Srrs return (stcb); 5266163953Srrs } else { 5267163953Srrs /* remaining chunks are HMAC checked */ 5268163953Srrs stcb->asoc.authenticated = 1; 5269163953Srrs } 5270163953Srrs break; 5271163953Srrs 5272163953Srrs default: 5273163953Srrs unknown_chunk: 5274163953Srrs /* it's an unknown chunk! */ 5275163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5276163953Srrs struct mbuf *mm; 5277163953Srrs struct sctp_paramhdr *phd; 5278163953Srrs 5279163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5280165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5281163953Srrs if (mm) { 5282163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5283163953Srrs /* 5284163953Srrs * We cheat and use param type since 5285163953Srrs * we did not bother to define a 5286163953Srrs * error cause struct. They are the 5287163953Srrs * same basic format with different 5288163953Srrs * names. 5289163953Srrs */ 5290163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5291163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5292165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5293166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5294163953Srrs M_DONTWAIT); 5295165647Srrs if (SCTP_BUF_NEXT(mm)) { 5296178202Srrs#ifdef SCTP_MBUF_LOGGING 5297179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5298178202Srrs struct mbuf *mat; 5299178202Srrs 5300178202Srrs mat = SCTP_BUF_NEXT(mm); 5301178202Srrs while (mat) { 5302178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5303178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5304178202Srrs } 5305178202Srrs mat = SCTP_BUF_NEXT(mat); 5306178202Srrs } 5307178202Srrs } 5308178202Srrs#endif 5309163953Srrs sctp_queue_op_err(stcb, mm); 5310163953Srrs } else { 5311163953Srrs sctp_m_freem(mm); 5312163953Srrs } 5313163953Srrs } 5314163953Srrs } 5315163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5316163953Srrs /* discard this packet */ 5317163953Srrs *offset = length; 5318163953Srrs return (stcb); 5319163953Srrs } /* else skip this bad chunk and continue... */ 5320163953Srrs break; 5321163953Srrs } /* switch (ch->chunk_type) */ 5322163953Srrs 5323163953Srrs 5324163953Srrsnext_chunk: 5325163953Srrs /* get the next chunk */ 5326163953Srrs *offset += SCTP_SIZE32(chk_length); 5327163953Srrs if (*offset >= length) { 5328163953Srrs /* no more data left in the mbuf chain */ 5329163953Srrs break; 5330163953Srrs } 5331163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5332163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5333163953Srrs if (ch == NULL) { 5334169378Srrs if (locked_tcb) { 5335163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5336169378Srrs } 5337163953Srrs *offset = length; 5338163953Srrs return (NULL); 5339163953Srrs } 5340163953Srrs } /* while */ 5341171990Srrs 5342171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5343171990Srrs sctp_send_asconf_ack(stcb); 5344171990Srrs } 5345163953Srrs return (stcb); 5346163953Srrs} 5347163953Srrs 5348163953Srrs 5349163953Srrs/* 5350163953Srrs * Process the ECN bits we have something set so we must look to see if it is 5351163953Srrs * ECN(0) or ECN(1) or CE 5352163953Srrs */ 5353170806Srrsstatic void 5354163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 5355163953Srrs uint8_t ecn_bits) 5356163953Srrs{ 5357163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5358163953Srrs ; 5359163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 5360163953Srrs /* 5361163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 5362163953Srrs * change the NS bit (that we have yet to find a way to send 5363163953Srrs * it yet). 5364163953Srrs */ 5365163953Srrs 5366163953Srrs /* ECN Nonce stuff */ 5367163953Srrs stcb->asoc.receiver_nonce_sum++; 5368163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 5369163953Srrs 5370163953Srrs /* 5371163953Srrs * Drag up the last_echo point if cumack is larger since we 5372163953Srrs * don't want the point falling way behind by more than 5373163953Srrs * 2^^31 and then having it be incorrect. 5374163953Srrs */ 5375163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5376163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5377163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5378163953Srrs } 5379163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 5380163953Srrs /* 5381163953Srrs * Drag up the last_echo point if cumack is larger since we 5382163953Srrs * don't want the point falling way behind by more than 5383163953Srrs * 2^^31 and then having it be incorrect. 5384163953Srrs */ 5385163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5386163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5387163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5388163953Srrs } 5389163953Srrs } 5390163953Srrs} 5391163953Srrs 5392170806Srrsstatic void 5393163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 5394163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 5395163953Srrs{ 5396163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5397163953Srrs /* 5398163953Srrs * we possibly must notify the sender that a congestion 5399163953Srrs * window reduction is in order. We do this by adding a ECNE 5400163953Srrs * chunk to the output chunk queue. The incoming CWR will 5401163953Srrs * remove this chunk. 5402163953Srrs */ 5403163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 5404163953Srrs MAX_TSN)) { 5405163953Srrs /* Yep, we need to add a ECNE */ 5406163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5407163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 5408163953Srrs } 5409163953Srrs } 5410163953Srrs} 5411163953Srrs 5412171440Srrs#ifdef INVARIANTS 5413171440Srrsstatic void 5414171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 5415171440Srrs{ 5416171440Srrs struct sctp_tcb *stcb; 5417171440Srrs 5418171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5419171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 5420171440Srrs panic("Own lock on stcb at return from input"); 5421171440Srrs } 5422171440Srrs } 5423171440Srrs} 5424171440Srrs 5425171440Srrs#endif 5426171440Srrs 5427163953Srrs/* 5428163953Srrs * common input chunk processing (v4 and v6) 5429163953Srrs */ 5430169378Srrsvoid 5431163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5432163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5433163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5434179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5435163953Srrs{ 5436163953Srrs /* 5437163953Srrs * Control chunk processing 5438163953Srrs */ 5439163953Srrs uint32_t high_tsn; 5440163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5441163953Srrs struct mbuf *m = *mm; 5442163953Srrs int abort_flag = 0; 5443163953Srrs int un_sent; 5444163953Srrs 5445163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5446163953Srrs#ifdef SCTP_AUDITING_ENABLED 5447163953Srrs sctp_audit_log(0xE0, 1); 5448163953Srrs sctp_auditing(0, inp, stcb, net); 5449163953Srrs#endif 5450163953Srrs 5451179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5452179783Srrs m, iphlen, offset, length, stcb); 5453163953Srrs if (stcb) { 5454163953Srrs /* always clear this before beginning a packet */ 5455163953Srrs stcb->asoc.authenticated = 0; 5456163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5457171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5458171990Srrs stcb, stcb->asoc.state); 5459171990Srrs 5460171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5461171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5462171745Srrs /*- 5463171745Srrs * If we hit here, we had a ref count 5464171745Srrs * up when the assoc was aborted and the 5465171745Srrs * timer is clearing out the assoc, we should 5466171745Srrs * NOT respond to any packet.. its OOTB. 5467171745Srrs */ 5468171745Srrs SCTP_TCB_UNLOCK(stcb); 5469171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5470179157Srrs vrf_id, port); 5471171745Srrs goto out_now; 5472171745Srrs } 5473163953Srrs } 5474163953Srrs if (IS_SCTP_CONTROL(ch)) { 5475163953Srrs /* process the control portion of the SCTP packet */ 5476169655Srrs /* sa_ignore NO_NULL_CHK */ 5477163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5478179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5479163953Srrs if (stcb) { 5480163953Srrs /* 5481163953Srrs * This covers us if the cookie-echo was there and 5482163953Srrs * it changes our INP. 5483163953Srrs */ 5484163953Srrs inp = stcb->sctp_ep; 5485179783Srrs if ((net) && (port)) { 5486179783Srrs if (net->port == 0) { 5487179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5488179783Srrs } 5489179783Srrs net->port = port; 5490179783Srrs } 5491163953Srrs } 5492163953Srrs } else { 5493163953Srrs /* 5494163953Srrs * no control chunks, so pre-process DATA chunks (these 5495163953Srrs * checks are taken care of by control processing) 5496163953Srrs */ 5497163953Srrs 5498163953Srrs /* 5499163953Srrs * if DATA only packet, and auth is required, then punt... 5500163953Srrs * can't have authenticated without any AUTH (control) 5501163953Srrs * chunks 5502163953Srrs */ 5503179783Srrs if ((stcb != NULL) && 5504179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5505179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5506163953Srrs /* "silently" ignore */ 5507163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5508163953Srrs SCTP_TCB_UNLOCK(stcb); 5509171440Srrs goto out_now; 5510163953Srrs } 5511163953Srrs if (stcb == NULL) { 5512163953Srrs /* out of the blue DATA chunk */ 5513169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5514179157Srrs vrf_id, port); 5515171440Srrs goto out_now; 5516163953Srrs } 5517163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5518163953Srrs /* v_tag mismatch! */ 5519163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5520163953Srrs SCTP_TCB_UNLOCK(stcb); 5521171440Srrs goto out_now; 5522163953Srrs } 5523163953Srrs } 5524163953Srrs 5525163953Srrs if (stcb == NULL) { 5526163953Srrs /* 5527163953Srrs * no valid TCB for this packet, or we found it's a bad 5528163953Srrs * packet while processing control, or we're done with this 5529163953Srrs * packet (done or skip rest of data), so we drop it... 5530163953Srrs */ 5531171440Srrs goto out_now; 5532163953Srrs } 5533163953Srrs /* 5534163953Srrs * DATA chunk processing 5535163953Srrs */ 5536163953Srrs /* plow through the data chunks while length > offset */ 5537163953Srrs 5538163953Srrs /* 5539163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5540163953Srrs * DATA is required. 5541163953Srrs */ 5542179783Srrs if ((length > offset) && 5543179783Srrs (stcb != NULL) && 5544179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5545179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5546163953Srrs !stcb->asoc.authenticated) { 5547163953Srrs /* "silently" ignore */ 5548163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5549169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5550169420Srrs "Data chunk requires AUTH, skipped\n"); 5551165220Srrs goto trigger_send; 5552163953Srrs } 5553163953Srrs if (length > offset) { 5554163953Srrs int retval; 5555163953Srrs 5556163953Srrs /* 5557163953Srrs * First check to make sure our state is correct. We would 5558163953Srrs * not get here unless we really did have a tag, so we don't 5559163953Srrs * abort if this happens, just dump the chunk silently. 5560163953Srrs */ 5561163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5562163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5563163953Srrs /* 5564163953Srrs * we consider data with valid tags in this state 5565163953Srrs * shows us the cookie-ack was lost. Imply it was 5566163953Srrs * there. 5567163953Srrs */ 5568179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5569171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5570171943Srrs stcb->asoc.overall_error_count, 5571171943Srrs 0, 5572171943Srrs SCTP_FROM_SCTP_INPUT, 5573171943Srrs __LINE__); 5574171943Srrs } 5575163953Srrs stcb->asoc.overall_error_count = 0; 5576163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5577163953Srrs break; 5578163953Srrs case SCTP_STATE_COOKIE_WAIT: 5579163953Srrs /* 5580163953Srrs * We consider OOTB any data sent during asoc setup. 5581163953Srrs */ 5582169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5583179157Srrs vrf_id, port); 5584163953Srrs SCTP_TCB_UNLOCK(stcb); 5585171440Srrs goto out_now; 5586171531Srrs /* sa_ignore NOTREACHED */ 5587163953Srrs break; 5588163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5589163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5590163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5591163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5592163953Srrs default: 5593163953Srrs SCTP_TCB_UNLOCK(stcb); 5594171440Srrs goto out_now; 5595171531Srrs /* sa_ignore NOTREACHED */ 5596163953Srrs break; 5597163953Srrs case SCTP_STATE_OPEN: 5598163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5599163953Srrs break; 5600163953Srrs } 5601163953Srrs /* take care of ECN, part 1. */ 5602163953Srrs if (stcb->asoc.ecn_allowed && 5603163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5604163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5605163953Srrs } 5606163953Srrs /* plow through the data chunks while length > offset */ 5607163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5608163953Srrs inp, stcb, net, &high_tsn); 5609163953Srrs if (retval == 2) { 5610163953Srrs /* 5611163953Srrs * The association aborted, NO UNLOCK needed since 5612163953Srrs * the association is destroyed. 5613163953Srrs */ 5614171440Srrs goto out_now; 5615163953Srrs } 5616163953Srrs data_processed = 1; 5617163953Srrs if (retval == 0) { 5618163953Srrs /* take care of ecn part 2. */ 5619163953Srrs if (stcb->asoc.ecn_allowed && 5620163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5621163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5622163953Srrs ecn_bits); 5623163953Srrs } 5624163953Srrs } 5625163953Srrs /* 5626163953Srrs * Anything important needs to have been m_copy'ed in 5627163953Srrs * process_data 5628163953Srrs */ 5629163953Srrs } 5630163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5631163953Srrs int was_a_gap = 0; 5632163953Srrs 5633163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5634163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5635163953Srrs /* there was a gap before this data was processed */ 5636163953Srrs was_a_gap = 1; 5637163953Srrs } 5638190689Srrs stcb->asoc.send_sack = 1; 5639163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 5640163953Srrs if (abort_flag) { 5641163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5642171440Srrs goto out_now; 5643163953Srrs } 5644190689Srrs } else if (fwd_tsn_seen) { 5645190689Srrs stcb->asoc.send_sack = 1; 5646163953Srrs } 5647163953Srrs /* trigger send of any chunks in queue... */ 5648165220Srrstrigger_send: 5649163953Srrs#ifdef SCTP_AUDITING_ENABLED 5650163953Srrs sctp_audit_log(0xE0, 2); 5651163953Srrs sctp_auditing(1, inp, stcb, net); 5652163953Srrs#endif 5653169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5654169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5655169420Srrs stcb->asoc.peers_rwnd, 5656169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5657169420Srrs stcb->asoc.total_flight); 5658163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5659163953Srrs 5660163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5661163953Srrs ((un_sent) && 5662163953Srrs (stcb->asoc.peers_rwnd > 0 || 5663163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5664169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5665172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5666169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5667163953Srrs } 5668163953Srrs#ifdef SCTP_AUDITING_ENABLED 5669163953Srrs sctp_audit_log(0xE0, 3); 5670163953Srrs sctp_auditing(2, inp, stcb, net); 5671163953Srrs#endif 5672163953Srrs SCTP_TCB_UNLOCK(stcb); 5673171440Srrsout_now: 5674171440Srrs#ifdef INVARIANTS 5675171440Srrs sctp_validate_no_locks(inp); 5676171440Srrs#endif 5677169378Srrs return; 5678163953Srrs} 5679163953Srrs 5680185694Srrs#if 0 5681185694Srrsstatic void 5682185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5683185694Srrs{ 5684185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5685185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5686185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5687185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5688185694Srrs } 5689185694Srrs} 5690163953Srrs 5691185694Srrs#endif 5692185694Srrs 5693163953Srrsvoid 5694189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5695163953Srrs{ 5696163953Srrs#ifdef SCTP_MBUF_LOGGING 5697163953Srrs struct mbuf *mat; 5698163953Srrs 5699163953Srrs#endif 5700165647Srrs struct mbuf *m; 5701163953Srrs int iphlen; 5702170181Srrs uint32_t vrf_id = 0; 5703163953Srrs uint8_t ecn_bits; 5704163953Srrs struct ip *ip; 5705163953Srrs struct sctphdr *sh; 5706163953Srrs struct sctp_inpcb *inp = NULL; 5707163953Srrs 5708163953Srrs uint32_t check, calc_check; 5709163953Srrs struct sctp_nets *net; 5710163953Srrs struct sctp_tcb *stcb = NULL; 5711163953Srrs struct sctp_chunkhdr *ch; 5712163953Srrs int refcount_up = 0; 5713163953Srrs int length, mlen, offset; 5714163953Srrs 5715169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5716169352Srrs SCTP_RELEASE_PKT(i_pak); 5717169352Srrs return; 5718169352Srrs } 5719168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5720163953Srrs iphlen = off; 5721165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5722170056Srrs 5723163953Srrs net = NULL; 5724163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5725163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5726163953Srrs 5727170091Srrs 5728163953Srrs#ifdef SCTP_MBUF_LOGGING 5729163953Srrs /* Log in any input mbufs */ 5730179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5731170744Srrs mat = m; 5732170744Srrs while (mat) { 5733170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5734170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5735170744Srrs } 5736170744Srrs mat = SCTP_BUF_NEXT(mat); 5737163953Srrs } 5738163953Srrs } 5739163953Srrs#endif 5740170091Srrs#ifdef SCTP_PACKET_LOGGING 5741179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5742170921Srrs sctp_packet_log(m, mlen); 5743170091Srrs#endif 5744170091Srrs /* 5745170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5746170091Srrs * case) 5747170091Srrs */ 5748170091Srrs mlen -= iphlen; 5749168124Srrs 5750163953Srrs /* 5751163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5752163953Srrs */ 5753163953Srrs ip = mtod(m, struct ip *); 5754163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5755165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5756163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5757163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5758163953Srrs return; 5759163953Srrs } 5760163953Srrs ip = mtod(m, struct ip *); 5761163953Srrs } 5762188067Srrs /* validate mbuf chain length with IP payload length */ 5763188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5764188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5765188067Srrs goto bad; 5766188067Srrs } 5767163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5768163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5769170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5770170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5771163953Srrs 5772163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5773163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5774163953Srrs goto bad; 5775163953Srrs } 5776168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5777163953Srrs /* 5778163953Srrs * We only look at broadcast if its a front state, All 5779163953Srrs * others we will not have a tcb for anyway. 5780163953Srrs */ 5781163953Srrs goto bad; 5782163953Srrs } 5783163953Srrs /* validate SCTP checksum */ 5784188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5785188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5786188067Srrs m->m_pkthdr.len, 5787188067Srrs if_name(m->m_pkthdr.rcvif), 5788188067Srrs m->m_pkthdr.csum_flags); 5789188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5790188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5791188067Srrs goto sctp_skip_csum_4; 5792188067Srrs } 5793171990Srrs check = sh->checksum; /* save incoming checksum */ 5794179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 5795171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5796171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5797171990Srrs ) { 5798188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 5799171990Srrs goto sctp_skip_csum_4; 5800171990Srrs } 5801171990Srrs sh->checksum = 0; /* prepare for calc */ 5802188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5803203503Stuexen sh->checksum = check; 5804188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5805171990Srrs if (calc_check != check) { 5806171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5807171990Srrs calc_check, check, m, mlen, iphlen); 5808163953Srrs 5809171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5810171990Srrs offset - sizeof(*ch), 5811171990Srrs sh, ch, &inp, &net, 5812171990Srrs vrf_id); 5813179157Srrs if ((net) && (port)) { 5814179157Srrs if (net->port == 0) { 5815179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5816179157Srrs } 5817179157Srrs net->port = port; 5818179157Srrs } 5819171990Srrs if ((inp) && (stcb)) { 5820171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5821172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5822171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5823171990Srrs refcount_up = 1; 5824163953Srrs } 5825171990Srrs SCTP_STAT_INCR(sctps_badsum); 5826171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5827171990Srrs goto bad; 5828168299Srrs } 5829163953Srrssctp_skip_csum_4: 5830168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5831168124Srrs if (sh->dest_port == 0) { 5832168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5833168124Srrs goto bad; 5834168124Srrs } 5835163953Srrs /* 5836163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5837163953Srrs * IP/SCTP/first chunk header... 5838163953Srrs */ 5839163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5840168299Srrs sh, ch, &inp, &net, vrf_id); 5841179157Srrs if ((net) && (port)) { 5842179157Srrs if (net->port == 0) { 5843179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5844179157Srrs } 5845179157Srrs net->port = port; 5846179157Srrs } 5847163953Srrs /* inp's ref-count increased && stcb locked */ 5848163953Srrs if (inp == NULL) { 5849163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5850163953Srrs 5851163953Srrs SCTP_STAT_INCR(sctps_noport); 5852163953Srrs#ifdef ICMP_BANDLIM 5853163953Srrs /* 5854163953Srrs * we use the bandwidth limiting to protect against sending 5855163953Srrs * too many ABORTS all at once. In this case these count the 5856163953Srrs * same as an ICMP message. 5857163953Srrs */ 5858163953Srrs if (badport_bandlim(0) < 0) 5859163953Srrs goto bad; 5860163953Srrs#endif /* ICMP_BANDLIM */ 5861169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5862169420Srrs "Sending a ABORT from packet entry!\n"); 5863163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5864163953Srrs /* 5865163953Srrs * we do a trick here to get the INIT tag, dig in 5866163953Srrs * and get the tag from the INIT and put it in the 5867163953Srrs * common header. 5868163953Srrs */ 5869163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5870163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5871163953Srrs (uint8_t *) & chunk_buf); 5872163953Srrs if (init_chk != NULL) 5873163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5874163953Srrs } 5875165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5876179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5877165220Srrs goto bad; 5878165220Srrs } 5879165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5880165220Srrs goto bad; 5881165220Srrs } 5882165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5883179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5884163953Srrs goto bad; 5885163953Srrs } else if (stcb == NULL) { 5886163953Srrs refcount_up = 1; 5887163953Srrs } 5888171167Sgnn#ifdef IPSEC 5889163953Srrs /* 5890163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5891163953Srrs * idea, so I will leave it in place. 5892163953Srrs */ 5893163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5894197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5895163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5896163953Srrs goto bad; 5897163953Srrs } 5898163953Srrs#endif /* IPSEC */ 5899163953Srrs 5900163953Srrs /* 5901163953Srrs * common chunk processing 5902163953Srrs */ 5903163953Srrs length = ip->ip_len + iphlen; 5904163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5905163953Srrs 5906163953Srrs ecn_bits = ip->ip_tos; 5907163953Srrs 5908169655Srrs /* sa_ignore NO_NULL_CHK */ 5909163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5910179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5911163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5912163953Srrs if (m) { 5913163953Srrs sctp_m_freem(m); 5914163953Srrs } 5915163953Srrs if ((inp) && (refcount_up)) { 5916163953Srrs /* reduce ref-count */ 5917163953Srrs SCTP_INP_DECR_REF(inp); 5918163953Srrs } 5919163953Srrs return; 5920163953Srrsbad: 5921169420Srrs if (stcb) { 5922163953Srrs SCTP_TCB_UNLOCK(stcb); 5923169420Srrs } 5924163953Srrs if ((inp) && (refcount_up)) { 5925163953Srrs /* reduce ref-count */ 5926163953Srrs SCTP_INP_DECR_REF(inp); 5927163953Srrs } 5928163953Srrs if (m) { 5929163953Srrs sctp_m_freem(m); 5930163953Srrs } 5931163953Srrs return; 5932163953Srrs} 5933179157Srrsvoid 5934179157Srrssctp_input(i_pak, off) 5935179157Srrs struct mbuf *i_pak; 5936179157Srrs int off; 5937179157Srrs{ 5938179157Srrs sctp_input_with_port(i_pak, off, 0); 5939179157Srrs} 5940