sctp_input.c revision 204141
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 204141 2010-02-20 20:30:40Z tuexen $"); 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 } 1863185694Srrs /* EY 05/13/08 - nr_sack version of the above if statement */ 1864185694Srrs if (asoc->nr_mapping_array && SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) 1865185694Srrs && asoc->peer_supports_nr_sack) { 1866185694Srrs memset(asoc->nr_mapping_array, 0, 1867185694Srrs asoc->nr_mapping_array_size); 1868185694Srrs } 1869165220Srrs SCTP_TCB_UNLOCK(stcb); 1870165220Srrs SCTP_INP_INFO_WLOCK(); 1871165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1872165220Srrs SCTP_TCB_LOCK(stcb); 1873165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1874163953Srrs /* send up all the data */ 1875164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1876165220Srrs 1877172090Srrs sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1878165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1879165220Srrs stcb->asoc.strmout[i].stream_no = i; 1880165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1881165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1882165220Srrs } 1883163953Srrs /* process the INIT-ACK info (my info) */ 1884163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1885163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1886163953Srrs 1887163953Srrs /* pull from vtag hash */ 1888163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1889163953Srrs /* re-insert to new vtag position */ 1890179783Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1891179783Srrs SCTP_BASE_INFO(hashasocmark))]; 1892163953Srrs /* 1893163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1894163953Srrs * system 1895163953Srrs */ 1896163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1897163953Srrs 1898163953Srrs /* process the INIT info (peer's info) */ 1899164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1900165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1901165220Srrs SCTP_INP_INFO_WUNLOCK(); 1902165220Srrs 1903163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1904163953Srrs if (retval < 0) { 1905166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1906166023Srrs asoc->cookie_how[how_indx] = 13; 1907166023Srrs 1908163953Srrs return (NULL); 1909163953Srrs } 1910163953Srrs /* 1911163953Srrs * since we did not send a HB make sure we don't double 1912163953Srrs * things 1913163953Srrs */ 1914163953Srrs net->hb_responded = 1; 1915163953Srrs 1916163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1917163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1918163953Srrs initack_offset, sh, init_src)) { 1919166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1920166023Srrs asoc->cookie_how[how_indx] = 14; 1921166023Srrs 1922163953Srrs return (NULL); 1923163953Srrs } 1924163953Srrs /* respond with a COOKIE-ACK */ 1925163953Srrs sctp_stop_all_cookie_timers(stcb); 1926165220Srrs sctp_toss_old_cookies(stcb, asoc); 1927163953Srrs sctp_send_cookie_ack(stcb); 1928166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1929166023Srrs asoc->cookie_how[how_indx] = 15; 1930163953Srrs 1931163953Srrs return (stcb); 1932163953Srrs } 1933166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1934166023Srrs asoc->cookie_how[how_indx] = 16; 1935163953Srrs /* all other cases... */ 1936163953Srrs return (NULL); 1937163953Srrs} 1938163953Srrs 1939166086Srrs 1940163953Srrs/* 1941163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1942163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1943163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1944163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1945163953Srrs * was from returns a new TCB 1946163953Srrs */ 1947185694Srrsstruct sctp_tcb * 1948163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1949163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1950163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1951163953Srrs struct sockaddr *init_src, int *notification, 1952169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1953179157Srrs uint32_t vrf_id, uint16_t port) 1954163953Srrs{ 1955163953Srrs struct sctp_tcb *stcb; 1956163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1957163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1958163953Srrs struct sockaddr_storage sa_store; 1959163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1960163953Srrs struct sockaddr_in *sin; 1961163953Srrs struct sockaddr_in6 *sin6; 1962163953Srrs struct sctp_association *asoc; 1963163953Srrs int chk_length; 1964163953Srrs int init_offset, initack_offset, initack_limit; 1965163953Srrs int retval; 1966163953Srrs int error = 0; 1967163953Srrs uint32_t old_tag; 1968166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1969163953Srrs 1970172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1971172090Srrs struct socket *so; 1972172090Srrs 1973172090Srrs so = SCTP_INP_SO(inp); 1974172090Srrs#endif 1975172090Srrs 1976163953Srrs /* 1977163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1978163953Srrs * INIT should start after the cookie-echo header struct (chunk 1979163953Srrs * header, state cookie header struct) 1980163953Srrs */ 1981163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1982163953Srrs init_cp = (struct sctp_init_chunk *) 1983163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1984163953Srrs (uint8_t *) & init_buf); 1985163953Srrs if (init_cp == NULL) { 1986163953Srrs /* could not pull a INIT chunk in cookie */ 1987169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1988169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1989163953Srrs return (NULL); 1990163953Srrs } 1991163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1992163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1993169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1994163953Srrs return (NULL); 1995163953Srrs } 1996163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1997163953Srrs /* 1998163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1999163953Srrs * INIT-ACK follows the INIT chunk 2000163953Srrs */ 2001163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 2002163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2003163953Srrs (uint8_t *) & initack_buf); 2004163953Srrs if (initack_cp == NULL) { 2005163953Srrs /* could not pull INIT-ACK chunk in cookie */ 2006169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2007163953Srrs return (NULL); 2008163953Srrs } 2009163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 2010163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2011163953Srrs return (NULL); 2012163953Srrs } 2013163953Srrs /* 2014163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 2015163953Srrs * "initack_limit" value. This is because the chk_length field 2016163953Srrs * includes the length of the cookie, but the cookie is omitted when 2017163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 2018163953Srrs */ 2019163953Srrs initack_limit = offset + cookie_len; 2020163953Srrs 2021163953Srrs /* 2022163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 2023163953Srrs * and popluate 2024163953Srrs */ 2025171531Srrs 2026171531Srrs /* 2027171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 2028171531Srrs * We do this since in effect we only use the p argument when the 2029171531Srrs * socket is unbound and we must do an implicit bind. Since we are 2030171531Srrs * getting a cookie, we cannot be unbound. 2031171531Srrs */ 2032163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 2033171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 2034171531Srrs (struct thread *)NULL 2035171531Srrs ); 2036163953Srrs if (stcb == NULL) { 2037163953Srrs struct mbuf *op_err; 2038163953Srrs 2039163953Srrs /* memory problem? */ 2040169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 2041169420Srrs "process_cookie_new: no room for another TCB!\n"); 2042163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2043170140Srrs 2044163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2045179157Srrs sh, op_err, vrf_id, port); 2046163953Srrs return (NULL); 2047163953Srrs } 2048163953Srrs /* get the correct sctp_nets */ 2049169420Srrs if (netp) 2050169420Srrs *netp = sctp_findnet(stcb, init_src); 2051169420Srrs 2052163953Srrs asoc = &stcb->asoc; 2053163953Srrs /* get scope variables out of cookie */ 2054163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 2055163953Srrs asoc->site_scope = cookie->site_scope; 2056163953Srrs asoc->local_scope = cookie->local_scope; 2057163953Srrs asoc->loopback_scope = cookie->loopback_scope; 2058163953Srrs 2059163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2060163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2061163953Srrs struct mbuf *op_err; 2062163953Srrs 2063163953Srrs /* 2064163953Srrs * Houston we have a problem. The EP changed while the 2065163953Srrs * cookie was in flight. Only recourse is to abort the 2066163953Srrs * association. 2067163953Srrs */ 2068170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2069163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2070163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2071179157Srrs sh, op_err, vrf_id, port); 2072172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2073172090Srrs SCTP_TCB_UNLOCK(stcb); 2074172090Srrs SCTP_SOCKET_LOCK(so, 1); 2075172090Srrs SCTP_TCB_LOCK(stcb); 2076172090Srrs#endif 2077171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2078171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2079172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2080172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2081172090Srrs#endif 2082172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2083163953Srrs return (NULL); 2084163953Srrs } 2085163953Srrs /* process the INIT-ACK info (my info) */ 2086163953Srrs old_tag = asoc->my_vtag; 2087185694Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2088163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2089163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2090163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2091163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2092179157Srrs asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2093163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 2094163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2095163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 2096163953Srrs 2097163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2098163953Srrs 2099163953Srrs /* process the INIT info (peer's info) */ 2100169420Srrs if (netp) 2101169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 2102169420Srrs else 2103169420Srrs retval = 0; 2104163953Srrs if (retval < 0) { 2105170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2106172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2107172090Srrs SCTP_TCB_UNLOCK(stcb); 2108172090Srrs SCTP_SOCKET_LOCK(so, 1); 2109172090Srrs SCTP_TCB_LOCK(stcb); 2110172090Srrs#endif 2111171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2112172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2113172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2114172090Srrs#endif 2115172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2116163953Srrs return (NULL); 2117163953Srrs } 2118163953Srrs /* load all addresses */ 2119163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 2120163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2121163953Srrs init_src)) { 2122170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2123172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2124172090Srrs SCTP_TCB_UNLOCK(stcb); 2125172090Srrs SCTP_SOCKET_LOCK(so, 1); 2126172090Srrs SCTP_TCB_LOCK(stcb); 2127172090Srrs#endif 2128171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2129172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2130172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2131172090Srrs#endif 2132172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2133163953Srrs return (NULL); 2134163953Srrs } 2135163953Srrs /* 2136163953Srrs * verify any preceding AUTH chunk that was skipped 2137163953Srrs */ 2138163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 2139163953Srrs sctp_auth_get_cookie_params(stcb, m, 2140163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2141163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2142163953Srrs if (auth_skipped) { 2143163953Srrs struct sctp_auth_chunk *auth; 2144163953Srrs 2145163953Srrs auth = (struct sctp_auth_chunk *) 2146166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2147169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2148163953Srrs /* auth HMAC failed, dump the assoc and packet */ 2149169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 2150169420Srrs "COOKIE-ECHO: AUTH failed\n"); 2151171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2152172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2153172090Srrs SCTP_TCB_UNLOCK(stcb); 2154172090Srrs SCTP_SOCKET_LOCK(so, 1); 2155172090Srrs SCTP_TCB_LOCK(stcb); 2156172090Srrs#endif 2157171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2158172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2159172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2160172090Srrs#endif 2161172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2162163953Srrs return (NULL); 2163163953Srrs } else { 2164163953Srrs /* remaining chunks checked... good to go */ 2165163953Srrs stcb->asoc.authenticated = 1; 2166163953Srrs } 2167163953Srrs } 2168163953Srrs /* update current state */ 2169169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2170171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2171163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2172163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2173163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2174163953Srrs } 2175165220Srrs sctp_stop_all_cookie_timers(stcb); 2176163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2177163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2178163953Srrs 2179163953Srrs /* 2180163953Srrs * if we're doing ASCONFs, check to see if we have any new local 2181163953Srrs * addresses that need to get added to the peer (eg. addresses 2182163953Srrs * changed while cookie echo in flight). This needs to be done 2183163953Srrs * after we go to the OPEN state to do the correct asconf 2184163953Srrs * processing. else, make sure we have the correct addresses in our 2185163953Srrs * lists 2186163953Srrs */ 2187163953Srrs 2188163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 2189163953Srrs /* pull in local_address (our "from" address) */ 2190163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 2191163953Srrs /* source addr is IPv4 */ 2192163953Srrs sin = (struct sockaddr_in *)initack_src; 2193163953Srrs memset(sin, 0, sizeof(*sin)); 2194163953Srrs sin->sin_family = AF_INET; 2195163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 2196163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 2197163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 2198163953Srrs /* source addr is IPv6 */ 2199163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 2200163953Srrs memset(sin6, 0, sizeof(*sin6)); 2201163953Srrs sin6->sin6_family = AF_INET6; 2202163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 2203163953Srrs sin6->sin6_scope_id = cookie->scope_id; 2204163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 2205163953Srrs sizeof(sin6->sin6_addr)); 2206163953Srrs } else { 2207170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2208172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2209172090Srrs SCTP_TCB_UNLOCK(stcb); 2210172090Srrs SCTP_SOCKET_LOCK(so, 1); 2211172090Srrs SCTP_TCB_LOCK(stcb); 2212172090Srrs#endif 2213171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2214172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2215172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2216172090Srrs#endif 2217172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2218163953Srrs return (NULL); 2219163953Srrs } 2220163953Srrs 2221163953Srrs /* set up to notify upper layer */ 2222163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 2223163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2224163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2225163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 2226163953Srrs /* 2227163953Srrs * This is an endpoint that called connect() how it got a 2228163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 2229163953Srrs * the INIT was sent, but before it got there.. a complete 2230163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 2231163953Srrs * should have went to the other code.. not here.. oh well.. 2232163953Srrs * a bit of protection is worth having.. 2233163953Srrs */ 2234163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2235172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2236172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2237172090Srrs SCTP_TCB_UNLOCK(stcb); 2238172090Srrs SCTP_SOCKET_LOCK(so, 1); 2239172090Srrs SCTP_TCB_LOCK(stcb); 2240172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2241172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2242172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2243172090Srrs return (NULL); 2244172090Srrs } 2245172090Srrs#endif 2246172090Srrs soisconnected(stcb->sctp_socket); 2247172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2248172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2249172090Srrs#endif 2250163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2251163953Srrs (inp->sctp_socket->so_qlimit)) { 2252163953Srrs /* 2253163953Srrs * We don't want to do anything with this one. Since it is 2254163953Srrs * the listening guy. The timer will get started for 2255163953Srrs * accepted connections in the caller. 2256163953Srrs */ 2257163953Srrs ; 2258163953Srrs } 2259163953Srrs /* since we did not send a HB make sure we don't double things */ 2260169420Srrs if ((netp) && (*netp)) 2261169420Srrs (*netp)->hb_responded = 1; 2262163953Srrs 2263163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2264163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2265163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2266163953Srrs } 2267165220Srrs /* calculate the RTT */ 2268171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2269170642Srrs if ((netp) && (*netp)) { 2270169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2271171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 2272170642Srrs } 2273172190Srrs /* respond with a COOKIE-ACK */ 2274163953Srrs sctp_send_cookie_ack(stcb); 2275172190Srrs 2276172190Srrs /* 2277172190Srrs * check the address lists for any ASCONFs that need to be sent 2278172190Srrs * AFTER the cookie-ack is sent 2279172190Srrs */ 2280172190Srrs sctp_check_address_list(stcb, m, 2281172190Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 2282172190Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2283172190Srrs initack_src, cookie->local_scope, cookie->site_scope, 2284172190Srrs cookie->ipv4_scope, cookie->loopback_scope); 2285172190Srrs 2286172190Srrs 2287163953Srrs return (stcb); 2288163953Srrs} 2289163953Srrs 2290185694Srrs/* 2291185694Srrs * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2292185694Srrs * we NEED to make sure we are not already using the vtag. If so we 2293185694Srrs * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2294185694Srrs head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2295185694Srrs SCTP_BASE_INFO(hashasocmark))]; 2296185694Srrs LIST_FOREACH(stcb, head, sctp_asocs) { 2297185694Srrs if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2298185694Srrs -- SEND ABORT - TRY AGAIN -- 2299185694Srrs } 2300185694Srrs } 2301185694Srrs*/ 2302163953Srrs 2303163953Srrs/* 2304163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2305163953Srrs * existing (non-NULL) TCB 2306163953Srrs */ 2307163953Srrsstatic struct mbuf * 2308163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2309163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2310163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2311169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2312179157Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2313163953Srrs{ 2314163953Srrs struct sctp_state_cookie *cookie; 2315163953Srrs struct sockaddr_in6 sin6; 2316163953Srrs struct sockaddr_in sin; 2317163953Srrs struct sctp_tcb *l_stcb = *stcb; 2318163953Srrs struct sctp_inpcb *l_inp; 2319163953Srrs struct sockaddr *to; 2320163953Srrs sctp_assoc_t sac_restart_id; 2321163953Srrs struct sctp_pcb *ep; 2322163953Srrs struct mbuf *m_sig; 2323163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2324163953Srrs uint8_t *sig; 2325163953Srrs uint8_t cookie_ok = 0; 2326163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 2327163953Srrs unsigned int cookie_len; 2328163953Srrs struct timeval now; 2329163953Srrs struct timeval time_expires; 2330163953Srrs struct sockaddr_storage dest_store; 2331163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2332163953Srrs struct ip *iph; 2333163953Srrs int notification = 0; 2334163953Srrs struct sctp_nets *netl; 2335163953Srrs int had_a_existing_tcb = 0; 2336163953Srrs 2337169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2338169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2339163953Srrs 2340163953Srrs if (inp_p == NULL) { 2341163953Srrs return (NULL); 2342163953Srrs } 2343163953Srrs /* First get the destination address setup too. */ 2344163953Srrs iph = mtod(m, struct ip *); 2345178251Srrs switch (iph->ip_v) { 2346178251Srrs case IPVERSION: 2347178251Srrs { 2348178251Srrs /* its IPv4 */ 2349178251Srrs struct sockaddr_in *lsin; 2350163953Srrs 2351178251Srrs lsin = (struct sockaddr_in *)(localep_sa); 2352178251Srrs memset(lsin, 0, sizeof(*lsin)); 2353178251Srrs lsin->sin_family = AF_INET; 2354178251Srrs lsin->sin_len = sizeof(*lsin); 2355178251Srrs lsin->sin_port = sh->dest_port; 2356178251Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2357178251Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2358178251Srrs break; 2359178251Srrs } 2360178251Srrs#ifdef INET6 2361178251Srrs case IPV6_VERSION >> 4: 2362178251Srrs { 2363178251Srrs /* its IPv6 */ 2364178251Srrs struct ip6_hdr *ip6; 2365178251Srrs struct sockaddr_in6 *lsin6; 2366163953Srrs 2367178251Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 2368178251Srrs memset(lsin6, 0, sizeof(*lsin6)); 2369178251Srrs lsin6->sin6_family = AF_INET6; 2370178251Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 2371178251Srrs ip6 = mtod(m, struct ip6_hdr *); 2372178251Srrs lsin6->sin6_port = sh->dest_port; 2373178251Srrs lsin6->sin6_addr = ip6->ip6_dst; 2374178251Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2375178251Srrs break; 2376178251Srrs } 2377178251Srrs#endif 2378178251Srrs default: 2379163953Srrs return (NULL); 2380163953Srrs } 2381163953Srrs 2382163953Srrs cookie = &cp->cookie; 2383163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2384163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 2385163953Srrs 2386163953Srrs if ((cookie->peerport != sh->src_port) && 2387163953Srrs (cookie->myport != sh->dest_port) && 2388163953Srrs (cookie->my_vtag != sh->v_tag)) { 2389163953Srrs /* 2390163953Srrs * invalid ports or bad tag. Note that we always leave the 2391163953Srrs * v_tag in the header in network order and when we stored 2392163953Srrs * it in the my_vtag slot we also left it in network order. 2393169352Srrs * This maintains the match even though it may be in the 2394163953Srrs * opposite byte order of the machine :-> 2395163953Srrs */ 2396163953Srrs return (NULL); 2397163953Srrs } 2398163953Srrs if (cookie_len > size_of_pkt || 2399163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2400163953Srrs sizeof(struct sctp_init_chunk) + 2401163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2402163953Srrs /* cookie too long! or too small */ 2403163953Srrs return (NULL); 2404163953Srrs } 2405163953Srrs /* 2406163953Srrs * split off the signature into its own mbuf (since it should not be 2407163953Srrs * calculated in the sctp_hmac_m() call). 2408163953Srrs */ 2409163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2410163953Srrs if (sig_offset > size_of_pkt) { 2411163953Srrs /* packet not correct size! */ 2412163953Srrs /* XXX this may already be accounted for earlier... */ 2413163953Srrs return (NULL); 2414163953Srrs } 2415163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 2416163953Srrs if (m_sig == NULL) { 2417163953Srrs /* out of memory or ?? */ 2418163953Srrs return (NULL); 2419163953Srrs } 2420178202Srrs#ifdef SCTP_MBUF_LOGGING 2421179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2422178202Srrs struct mbuf *mat; 2423178202Srrs 2424178202Srrs mat = m_sig; 2425178202Srrs while (mat) { 2426178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 2427178202Srrs sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2428178202Srrs } 2429178202Srrs mat = SCTP_BUF_NEXT(mat); 2430178202Srrs } 2431178202Srrs } 2432178202Srrs#endif 2433178202Srrs 2434163953Srrs /* 2435163953Srrs * compute the signature/digest for the cookie 2436163953Srrs */ 2437163953Srrs ep = &(*inp_p)->sctp_ep; 2438163953Srrs l_inp = *inp_p; 2439163953Srrs if (l_stcb) { 2440163953Srrs SCTP_TCB_UNLOCK(l_stcb); 2441163953Srrs } 2442163953Srrs SCTP_INP_RLOCK(l_inp); 2443163953Srrs if (l_stcb) { 2444163953Srrs SCTP_TCB_LOCK(l_stcb); 2445163953Srrs } 2446163953Srrs /* which cookie is it? */ 2447163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2448163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2449163953Srrs /* it's the old cookie */ 2450169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2451163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2452170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2453163953Srrs } else { 2454163953Srrs /* it's the current cookie */ 2455169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2456163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2457170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2458163953Srrs } 2459163953Srrs /* get the signature */ 2460163953Srrs SCTP_INP_RUNLOCK(l_inp); 2461163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2462163953Srrs if (sig == NULL) { 2463163953Srrs /* couldn't find signature */ 2464164085Srrs sctp_m_freem(m_sig); 2465163953Srrs return (NULL); 2466163953Srrs } 2467163953Srrs /* compare the received digest with the computed digest */ 2468163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2469163953Srrs /* try the old cookie? */ 2470163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2471163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 2472163953Srrs /* compute digest with old */ 2473169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 2474163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2475170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2476163953Srrs /* compare */ 2477163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2478163953Srrs cookie_ok = 1; 2479163953Srrs } 2480163953Srrs } else { 2481163953Srrs cookie_ok = 1; 2482163953Srrs } 2483163953Srrs 2484163953Srrs /* 2485163953Srrs * Now before we continue we must reconstruct our mbuf so that 2486163953Srrs * normal processing of any other chunks will work. 2487163953Srrs */ 2488163953Srrs { 2489163953Srrs struct mbuf *m_at; 2490163953Srrs 2491163953Srrs m_at = m; 2492165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 2493165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2494163953Srrs } 2495165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2496163953Srrs } 2497163953Srrs 2498163953Srrs if (cookie_ok == 0) { 2499169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2500169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2501169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2502169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2503163953Srrs return (NULL); 2504163953Srrs } 2505163953Srrs /* 2506163953Srrs * check the cookie timestamps to be sure it's not stale 2507163953Srrs */ 2508169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2509163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2510169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2511163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2512180387Srrs /* 2513180387Srrs * TODO sctp_constants.h needs alternative time macros when _KERNEL 2514180387Srrs * is undefined. 2515180387Srrs */ 2516163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2517163953Srrs /* cookie is stale! */ 2518163953Srrs struct mbuf *op_err; 2519163953Srrs struct sctp_stale_cookie_msg *scm; 2520163953Srrs uint32_t tim; 2521163953Srrs 2522163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2523165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2524163953Srrs if (op_err == NULL) { 2525163953Srrs /* FOOBAR */ 2526163953Srrs return (NULL); 2527163953Srrs } 2528163953Srrs /* Set the len */ 2529165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2530163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2531163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2532163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2533163953Srrs (sizeof(uint32_t)))); 2534163953Srrs /* seconds to usec */ 2535163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2536163953Srrs /* add in usec */ 2537163953Srrs if (tim == 0) 2538163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2539163953Srrs scm->time_usec = htonl(tim); 2540169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2541179157Srrs vrf_id, port); 2542163953Srrs return (NULL); 2543163953Srrs } 2544163953Srrs /* 2545163953Srrs * Now we must see with the lookup address if we have an existing 2546163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2547163953Srrs * and a INIT collided with us and somewhere the peer sent the 2548163953Srrs * cookie on another address besides the single address our assoc 2549163953Srrs * had for him. In this case we will have one of the tie-tags set at 2550163953Srrs * least AND the address field in the cookie can be used to look it 2551163953Srrs * up. 2552163953Srrs */ 2553163953Srrs to = NULL; 2554163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2555163953Srrs memset(&sin6, 0, sizeof(sin6)); 2556163953Srrs sin6.sin6_family = AF_INET6; 2557163953Srrs sin6.sin6_len = sizeof(sin6); 2558163953Srrs sin6.sin6_port = sh->src_port; 2559163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2560163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2561163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2562163953Srrs to = (struct sockaddr *)&sin6; 2563163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2564163953Srrs memset(&sin, 0, sizeof(sin)); 2565163953Srrs sin.sin_family = AF_INET; 2566163953Srrs sin.sin_len = sizeof(sin); 2567163953Srrs sin.sin_port = sh->src_port; 2568163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2569163953Srrs to = (struct sockaddr *)&sin; 2570169420Srrs } else { 2571169420Srrs /* This should not happen */ 2572169420Srrs return (NULL); 2573163953Srrs } 2574163953Srrs if ((*stcb == NULL) && to) { 2575163953Srrs /* Yep, lets check */ 2576163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2577163953Srrs if (*stcb == NULL) { 2578163953Srrs /* 2579163953Srrs * We should have only got back the same inp. If we 2580163953Srrs * got back a different ep we have a problem. The 2581163953Srrs * original findep got back l_inp and now 2582163953Srrs */ 2583163953Srrs if (l_inp != *inp_p) { 2584169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2585163953Srrs } 2586165220Srrs } else { 2587165220Srrs if (*locked_tcb == NULL) { 2588165220Srrs /* 2589165220Srrs * In this case we found the assoc only 2590165220Srrs * after we locked the create lock. This 2591165220Srrs * means we are in a colliding case and we 2592165220Srrs * must make sure that we unlock the tcb if 2593165220Srrs * its one of the cases where we throw away 2594165220Srrs * the incoming packets. 2595165220Srrs */ 2596165220Srrs *locked_tcb = *stcb; 2597165220Srrs 2598165220Srrs /* 2599165220Srrs * We must also increment the inp ref count 2600165220Srrs * since the ref_count flags was set when we 2601165220Srrs * did not find the TCB, now we found it 2602165220Srrs * which reduces the refcount.. we must 2603165220Srrs * raise it back out to balance it all :-) 2604165220Srrs */ 2605165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2606165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2607169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2608165220Srrs (*stcb)->sctp_ep, l_inp); 2609165220Srrs } 2610165220Srrs } 2611163953Srrs } 2612163953Srrs } 2613188067Srrs if (to == NULL) { 2614169420Srrs return (NULL); 2615188067Srrs } 2616163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2617163953Srrs if (*stcb == NULL) { 2618163953Srrs /* this is the "normal" case... get a new TCB */ 2619163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2620163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2621179157Srrs auth_skipped, auth_offset, auth_len, vrf_id, port); 2622163953Srrs } else { 2623163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2624163953Srrs had_a_existing_tcb = 1; 2625163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2626185694Srrs cookie, cookie_len, *inp_p, *stcb, netp, to, 2627185694Srrs ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port); 2628163953Srrs } 2629163953Srrs 2630163953Srrs if (*stcb == NULL) { 2631163953Srrs /* still no TCB... must be bad cookie-echo */ 2632163953Srrs return (NULL); 2633163953Srrs } 2634163953Srrs /* 2635163953Srrs * Ok, we built an association so confirm the address we sent the 2636163953Srrs * INIT-ACK to. 2637163953Srrs */ 2638163953Srrs netl = sctp_findnet(*stcb, to); 2639163953Srrs /* 2640163953Srrs * This code should in theory NOT run but 2641163953Srrs */ 2642163953Srrs if (netl == NULL) { 2643163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2644163953Srrs int ret; 2645163953Srrs 2646165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2647165220Srrs SCTP_IN_COOKIE_PROC); 2648163953Srrs netl = sctp_findnet(*stcb, to); 2649163953Srrs } 2650163953Srrs if (netl) { 2651163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2652163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2653169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2654163953Srrs netl); 2655163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2656172090Srrs (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2657163953Srrs } 2658163953Srrs } 2659163953Srrs if (*stcb) { 2660163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2661163953Srrs *stcb, NULL); 2662163953Srrs } 2663163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2664163953Srrs if (!had_a_existing_tcb || 2665163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2666163953Srrs /* 2667163953Srrs * If we have a NEW cookie or the connect never 2668163953Srrs * reached the connected state during collision we 2669163953Srrs * must do the TCP accept thing. 2670163953Srrs */ 2671163953Srrs struct socket *so, *oso; 2672163953Srrs struct sctp_inpcb *inp; 2673163953Srrs 2674163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2675163953Srrs /* 2676163953Srrs * For a restart we will keep the same 2677163953Srrs * socket, no need to do anything. I THINK!! 2678163953Srrs */ 2679172090Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED); 2680163953Srrs return (m); 2681163953Srrs } 2682163953Srrs oso = (*inp_p)->sctp_socket; 2683171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2684163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2685166086Srrs so = sonewconn(oso, 0 2686163953Srrs ); 2687163953Srrs SCTP_TCB_LOCK((*stcb)); 2688171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2689171858Srrs 2690163953Srrs if (so == NULL) { 2691163953Srrs struct mbuf *op_err; 2692163953Srrs 2693172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2694172090Srrs struct socket *pcb_so; 2695172090Srrs 2696172090Srrs#endif 2697163953Srrs /* Too many sockets */ 2698169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2699163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2700163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2701179157Srrs sh, op_err, vrf_id, port); 2702172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2703172090Srrs pcb_so = SCTP_INP_SO(*inp_p); 2704172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2705172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2706172090Srrs SCTP_SOCKET_LOCK(pcb_so, 1); 2707172090Srrs SCTP_TCB_LOCK((*stcb)); 2708172090Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2709172090Srrs#endif 2710171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2711172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2712172090Srrs SCTP_SOCKET_UNLOCK(pcb_so, 1); 2713172090Srrs#endif 2714163953Srrs return (NULL); 2715163953Srrs } 2716163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2717166086Srrs SCTP_INP_INCR_REF(inp); 2718166086Srrs /* 2719166086Srrs * We add the unbound flag here so that if we get an 2720166086Srrs * soabort() before we get the move_pcb done, we 2721166086Srrs * will properly cleanup. 2722166086Srrs */ 2723163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2724163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2725163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2726166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2727163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2728163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2729163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2730172091Srrs inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2731163953Srrs inp->sctp_socket = so; 2732163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2733163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2734163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2735163953Srrs inp->inp_starting_point_for_iterator = NULL; 2736163953Srrs /* 2737163953Srrs * copy in the authentication parameters from the 2738163953Srrs * original endpoint 2739163953Srrs */ 2740163953Srrs if (inp->sctp_ep.local_hmacs) 2741163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2742163953Srrs inp->sctp_ep.local_hmacs = 2743163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2744163953Srrs if (inp->sctp_ep.local_auth_chunks) 2745163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2746163953Srrs inp->sctp_ep.local_auth_chunks = 2747163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2748163953Srrs 2749163953Srrs /* 2750163953Srrs * Now we must move it from one hash table to 2751163953Srrs * another and get the tcb in the right place. 2752163953Srrs */ 2753163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2754170056Srrs 2755170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2756170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2757170056Srrs 2758175845Srwatson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2759175845Srwatson 0); 2760170056Srrs SCTP_TCB_LOCK((*stcb)); 2761170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2762163953Srrs 2763170056Srrs 2764166086Srrs /* 2765166086Srrs * now we must check to see if we were aborted while 2766166086Srrs * the move was going on and the lock/unlock 2767166086Srrs * happened. 2768166086Srrs */ 2769166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2770166086Srrs /* 2771166086Srrs * yep it was, we leave the assoc attached 2772166086Srrs * to the socket since the sctp_inpcb_free() 2773166086Srrs * call will send an abort for us. 2774166086Srrs */ 2775166086Srrs SCTP_INP_DECR_REF(inp); 2776166086Srrs return (NULL); 2777166086Srrs } 2778166086Srrs SCTP_INP_DECR_REF(inp); 2779163953Srrs /* Switch over to the new guy */ 2780163953Srrs *inp_p = inp; 2781172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2782163953Srrs 2783166086Srrs /* 2784166086Srrs * Pull it from the incomplete queue and wake the 2785166086Srrs * guy 2786166086Srrs */ 2787172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2788172090Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2789172090Srrs SCTP_TCB_UNLOCK((*stcb)); 2790172090Srrs SCTP_SOCKET_LOCK(so, 1); 2791172090Srrs#endif 2792166086Srrs soisconnected(so); 2793172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2794172118Srrs SCTP_TCB_LOCK((*stcb)); 2795172118Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2796172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2797172090Srrs#endif 2798163953Srrs return (m); 2799163953Srrs } 2800163953Srrs } 2801163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2802172090Srrs sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2803163953Srrs } 2804163953Srrs return (m); 2805163953Srrs} 2806163953Srrs 2807163953Srrsstatic void 2808163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2809163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2810163953Srrs{ 2811163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2812163953Srrs struct sctp_association *asoc; 2813163953Srrs 2814169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2815169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2816163953Srrs if (stcb == NULL) 2817163953Srrs return; 2818163953Srrs 2819163953Srrs asoc = &stcb->asoc; 2820163953Srrs 2821163953Srrs sctp_stop_all_cookie_timers(stcb); 2822163953Srrs /* process according to association state */ 2823163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2824163953Srrs /* state change only needed when I am in right state */ 2825169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2826171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2827163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2828163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2829163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2830163953Srrs 2831163953Srrs } 2832163953Srrs /* update RTO */ 2833163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2834163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2835163953Srrs if (asoc->overall_error_count == 0) { 2836163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2837171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2838163953Srrs } 2839169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2840172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2841163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2842163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2843172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2844172090Srrs struct socket *so; 2845172090Srrs 2846172090Srrs#endif 2847163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2848172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2849172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 2850172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 2851172090Srrs SCTP_TCB_UNLOCK(stcb); 2852172090Srrs SCTP_SOCKET_LOCK(so, 1); 2853172090Srrs SCTP_TCB_LOCK(stcb); 2854172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 2855172090Srrs if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2856172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2857172090Srrs return; 2858172090Srrs } 2859172090Srrs#endif 2860172090Srrs soisconnected(stcb->sctp_socket); 2861172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2862172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 2863172090Srrs#endif 2864163953Srrs } 2865163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2866163953Srrs stcb, net); 2867163953Srrs /* 2868163953Srrs * since we did not send a HB make sure we don't double 2869163953Srrs * things 2870163953Srrs */ 2871163953Srrs net->hb_responded = 1; 2872163953Srrs 2873163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2874163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2875163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2876163953Srrs stcb->sctp_ep, stcb, NULL); 2877163953Srrs } 2878163953Srrs /* 2879171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2880171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2881171572Srrs * in flight) 2882163953Srrs */ 2883163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2884163953Srrs (stcb->asoc.peer_supports_asconf) && 2885163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2886171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2887163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2888163953Srrs stcb->sctp_ep, stcb, 2889163953Srrs stcb->asoc.primary_destination); 2890171572Srrs#else 2891172190Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2892172190Srrs SCTP_ADDR_NOT_LOCKED); 2893171572Srrs#endif 2894163953Srrs } 2895163953Srrs } 2896163953Srrs /* Toss the cookie if I can */ 2897163953Srrs sctp_toss_old_cookies(stcb, asoc); 2898163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2899163953Srrs /* Restart the timer if we have pending data */ 2900163953Srrs struct sctp_tmit_chunk *chk; 2901163953Srrs 2902163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2903163953Srrs if (chk) { 2904163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2905163953Srrs stcb, chk->whoTo); 2906163953Srrs } 2907163953Srrs } 2908163953Srrs} 2909163953Srrs 2910163953Srrsstatic void 2911163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2912163953Srrs struct sctp_tcb *stcb) 2913163953Srrs{ 2914163953Srrs struct sctp_nets *net; 2915163953Srrs struct sctp_tmit_chunk *lchk; 2916163953Srrs uint32_t tsn; 2917163953Srrs 2918163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2919163953Srrs return; 2920163953Srrs } 2921163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2922163953Srrs tsn = ntohl(cp->tsn); 2923163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2924163953Srrs /* Also we make sure we disable the nonce_wait */ 2925163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2926163953Srrs if (lchk == NULL) { 2927163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2928163953Srrs } else { 2929163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2930163953Srrs } 2931163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2932163953Srrs stcb->asoc.nonce_sum_check = 0; 2933163953Srrs 2934163953Srrs /* Find where it was sent, if possible */ 2935163953Srrs net = NULL; 2936163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2937163953Srrs while (lchk) { 2938163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2939163953Srrs net = lchk->whoTo; 2940163953Srrs break; 2941163953Srrs } 2942163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2943163953Srrs break; 2944163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2945163953Srrs } 2946163953Srrs if (net == NULL) 2947163953Srrs /* default is we use the primary */ 2948163953Srrs net = stcb->asoc.primary_destination; 2949163953Srrs 2950163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2951163953Srrs /* 2952171440Srrs * JRS - Use the congestion control given in the pluggable 2953171440Srrs * CC module 2954171440Srrs */ 2955171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2956171440Srrs /* 2957163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2958163953Srrs * the next sending seq i.e. the resync_tsn. 2959163953Srrs */ 2960163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2961163953Srrs } 2962163953Srrs /* 2963163953Srrs * We always send a CWR this way if our previous one was lost our 2964163953Srrs * peer will get an update, or if it is not time again to reduce we 2965163953Srrs * still get the cwr to the peer. 2966163953Srrs */ 2967163953Srrs sctp_send_cwr(stcb, net, tsn); 2968163953Srrs} 2969163953Srrs 2970163953Srrsstatic void 2971163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2972163953Srrs{ 2973163953Srrs /* 2974163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2975163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2976163953Srrs * If so remove it. 2977163953Srrs */ 2978163953Srrs struct sctp_tmit_chunk *chk; 2979163953Srrs struct sctp_ecne_chunk *ecne; 2980163953Srrs 2981163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2982163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2983163953Srrs continue; 2984163953Srrs } 2985163953Srrs /* 2986163953Srrs * Look for and remove if it is the right TSN. Since there 2987163953Srrs * is only ONE ECNE on the control queue at any one time we 2988163953Srrs * don't need to worry about more than one! 2989163953Srrs */ 2990163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2991163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2992163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2993163953Srrs /* this covers this ECNE, we can remove it */ 2994163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2995163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2996163953Srrs sctp_next); 2997163953Srrs if (chk->data) { 2998163953Srrs sctp_m_freem(chk->data); 2999163953Srrs chk->data = NULL; 3000163953Srrs } 3001163953Srrs stcb->asoc.ctrl_queue_cnt--; 3002163953Srrs sctp_free_a_chunk(stcb, chk); 3003163953Srrs break; 3004163953Srrs } 3005163953Srrs } 3006163953Srrs} 3007163953Srrs 3008163953Srrsstatic void 3009163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 3010163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3011163953Srrs{ 3012163953Srrs struct sctp_association *asoc; 3013163953Srrs 3014172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3015172090Srrs struct socket *so; 3016172090Srrs 3017172090Srrs#endif 3018172090Srrs 3019169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3020169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3021163953Srrs if (stcb == NULL) 3022163953Srrs return; 3023163953Srrs 3024163953Srrs asoc = &stcb->asoc; 3025163953Srrs /* process according to association state */ 3026163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3027163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3028171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3029171990Srrs "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3030163953Srrs SCTP_TCB_UNLOCK(stcb); 3031163953Srrs return; 3032163953Srrs } 3033163953Srrs /* notify upper layer protocol */ 3034163953Srrs if (stcb->sctp_socket) { 3035172090Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3036163953Srrs /* are the queues empty? they should be */ 3037163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 3038163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 3039163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 3040172090Srrs sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3041163953Srrs } 3042163953Srrs } 3043163953Srrs /* stop the timer */ 3044178202Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3045163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3046163953Srrs /* free the TCB */ 3047171990Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 3048171990Srrs "sctp_handle_shutdown_complete: calls free-asoc\n"); 3049172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3050172090Srrs so = SCTP_INP_SO(stcb->sctp_ep); 3051172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 3052172090Srrs SCTP_TCB_UNLOCK(stcb); 3053172090Srrs SCTP_SOCKET_LOCK(so, 1); 3054172090Srrs SCTP_TCB_LOCK(stcb); 3055172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 3056172090Srrs#endif 3057171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3058172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3059172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 3060172090Srrs#endif 3061163953Srrs return; 3062163953Srrs} 3063163953Srrs 3064163953Srrsstatic int 3065163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3066163953Srrs struct sctp_nets *net, uint8_t flg) 3067163953Srrs{ 3068163953Srrs switch (desc->chunk_type) { 3069163953Srrs case SCTP_DATA: 3070163953Srrs /* find the tsn to resend (possibly */ 3071163953Srrs { 3072163953Srrs uint32_t tsn; 3073163953Srrs struct sctp_tmit_chunk *tp1; 3074163953Srrs 3075163953Srrs tsn = ntohl(desc->tsn_ifany); 3076163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3077163953Srrs while (tp1) { 3078163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3079163953Srrs /* found it */ 3080163953Srrs break; 3081163953Srrs } 3082163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 3083163953Srrs MAX_TSN)) { 3084163953Srrs /* not found */ 3085163953Srrs tp1 = NULL; 3086163953Srrs break; 3087163953Srrs } 3088163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3089163953Srrs } 3090163953Srrs if (tp1 == NULL) { 3091163953Srrs /* 3092163953Srrs * Do it the other way , aka without paying 3093163953Srrs * attention to queue seq order. 3094163953Srrs */ 3095163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 3096163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 3097163953Srrs while (tp1) { 3098163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 3099163953Srrs /* found it */ 3100163953Srrs break; 3101163953Srrs } 3102163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 3103163953Srrs } 3104163953Srrs } 3105163953Srrs if (tp1 == NULL) { 3106163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 3107163953Srrs } 3108163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3109163953Srrs uint8_t *ddp; 3110163953Srrs 3111163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 3112163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3113163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 3114163953Srrs return (0); 3115163953Srrs } 3116163953Srrs if (stcb->asoc.peers_rwnd == 0 && 3117163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 3118163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 3119163953Srrs return (0); 3120163953Srrs } 3121163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3122163953Srrs sizeof(struct sctp_data_chunk)); 3123163953Srrs { 3124163953Srrs unsigned int iii; 3125163953Srrs 3126163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 3127163953Srrs iii++) { 3128163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 3129163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 3130163953Srrs return (-1); 3131163953Srrs } 3132163953Srrs } 3133163953Srrs } 3134163953Srrs /* 3135163953Srrs * We zero out the nonce so resync not 3136163953Srrs * needed 3137163953Srrs */ 3138163953Srrs tp1->rec.data.ect_nonce = 0; 3139163953Srrs 3140163953Srrs if (tp1->do_rtt) { 3141163953Srrs /* 3142163953Srrs * this guy had a RTO calculation 3143163953Srrs * pending on it, cancel it 3144163953Srrs */ 3145163953Srrs tp1->do_rtt = 0; 3146163953Srrs } 3147163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 3148163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 3149163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3150163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 3151163953Srrs /* 3152163953Srrs * mark it as if we were doing a FR, since 3153163953Srrs * we will be getting gap ack reports behind 3154163953Srrs * the info from the router. 3155163953Srrs */ 3156163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 3157163953Srrs /* 3158163953Srrs * mark the tsn with what sequences can 3159163953Srrs * cause a new FR. 3160163953Srrs */ 3161163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3162163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3163163953Srrs } else { 3164163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3165163953Srrs } 3166163953Srrs 3167163953Srrs /* restart the timer */ 3168163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3169171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3170163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3171163953Srrs stcb, tp1->whoTo); 3172163953Srrs 3173163953Srrs /* fix counts and things */ 3174179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3175170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3176170744Srrs tp1->whoTo->flight_size, 3177170744Srrs tp1->book_size, 3178170744Srrs (uintptr_t) stcb, 3179170744Srrs tp1->rec.data.TSN_seq); 3180170744Srrs } 3181190689Srrs if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3182190689Srrs sctp_flight_size_decrease(tp1); 3183190689Srrs sctp_total_flight_decrease(stcb, tp1); 3184190689Srrs } 3185163953Srrs } { 3186163953Srrs /* audit code */ 3187163953Srrs unsigned int audit; 3188163953Srrs 3189163953Srrs audit = 0; 3190163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3191163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3192163953Srrs audit++; 3193163953Srrs } 3194163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3195163953Srrs sctp_next) { 3196163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 3197163953Srrs audit++; 3198163953Srrs } 3199163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 3200169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3201163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 3202163953Srrs#ifndef SCTP_AUDITING_ENABLED 3203163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 3204163953Srrs#endif 3205163953Srrs } 3206163953Srrs } 3207163953Srrs } 3208163953Srrs break; 3209163953Srrs case SCTP_ASCONF: 3210163953Srrs { 3211163953Srrs struct sctp_tmit_chunk *asconf; 3212163953Srrs 3213163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3214163953Srrs sctp_next) { 3215163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3216163953Srrs break; 3217163953Srrs } 3218163953Srrs } 3219163953Srrs if (asconf) { 3220163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 3221163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3222163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 3223163953Srrs asconf->snd_count--; 3224163953Srrs } 3225163953Srrs } 3226163953Srrs break; 3227163953Srrs case SCTP_INITIATION: 3228163953Srrs /* resend the INIT */ 3229163953Srrs stcb->asoc.dropped_special_cnt++; 3230163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3231163953Srrs /* 3232163953Srrs * If we can get it in, in a few attempts we do 3233163953Srrs * this, otherwise we let the timer fire. 3234163953Srrs */ 3235163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3236171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3237172090Srrs sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3238163953Srrs } 3239163953Srrs break; 3240163953Srrs case SCTP_SELECTIVE_ACK: 3241163953Srrs /* resend the sack */ 3242163953Srrs sctp_send_sack(stcb); 3243163953Srrs break; 3244185694Srrs /* EY for nr_sacks */ 3245185694Srrs case SCTP_NR_SELECTIVE_ACK: 3246185694Srrs sctp_send_nr_sack(stcb); /* EY resend the nr-sack */ 3247185694Srrs break; 3248163953Srrs case SCTP_HEARTBEAT_REQUEST: 3249163953Srrs /* resend a demand HB */ 3250171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3251171440Srrs /* 3252171440Srrs * Only retransmit if we KNOW we wont destroy the 3253171440Srrs * tcb 3254171440Srrs */ 3255171440Srrs (void)sctp_send_hb(stcb, 1, net); 3256171440Srrs } 3257163953Srrs break; 3258163953Srrs case SCTP_SHUTDOWN: 3259163953Srrs sctp_send_shutdown(stcb, net); 3260163953Srrs break; 3261163953Srrs case SCTP_SHUTDOWN_ACK: 3262163953Srrs sctp_send_shutdown_ack(stcb, net); 3263163953Srrs break; 3264163953Srrs case SCTP_COOKIE_ECHO: 3265163953Srrs { 3266163953Srrs struct sctp_tmit_chunk *cookie; 3267163953Srrs 3268163953Srrs cookie = NULL; 3269163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3270163953Srrs sctp_next) { 3271163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3272163953Srrs break; 3273163953Srrs } 3274163953Srrs } 3275163953Srrs if (cookie) { 3276163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 3277163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3278163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 3279163953Srrs sctp_stop_all_cookie_timers(stcb); 3280163953Srrs } 3281163953Srrs } 3282163953Srrs break; 3283163953Srrs case SCTP_COOKIE_ACK: 3284163953Srrs sctp_send_cookie_ack(stcb); 3285163953Srrs break; 3286163953Srrs case SCTP_ASCONF_ACK: 3287163953Srrs /* resend last asconf ack */ 3288171990Srrs sctp_send_asconf_ack(stcb); 3289163953Srrs break; 3290163953Srrs case SCTP_FORWARD_CUM_TSN: 3291163953Srrs send_forward_tsn(stcb, &stcb->asoc); 3292163953Srrs break; 3293163953Srrs /* can't do anything with these */ 3294163953Srrs case SCTP_PACKET_DROPPED: 3295163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 3296163953Srrs case SCTP_HEARTBEAT_ACK: 3297163953Srrs case SCTP_ABORT_ASSOCIATION: 3298163953Srrs case SCTP_OPERATION_ERROR: 3299163953Srrs case SCTP_SHUTDOWN_COMPLETE: 3300163953Srrs case SCTP_ECN_ECHO: 3301163953Srrs case SCTP_ECN_CWR: 3302163953Srrs default: 3303163953Srrs break; 3304163953Srrs } 3305163953Srrs return (0); 3306163953Srrs} 3307163953Srrs 3308163953Srrsvoid 3309163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3310163953Srrs{ 3311163953Srrs int i; 3312163953Srrs uint16_t temp; 3313163953Srrs 3314163953Srrs /* 3315163953Srrs * We set things to 0xffff since this is the last delivered sequence 3316163953Srrs * and we will be sending in 0 after the reset. 3317163953Srrs */ 3318163953Srrs 3319163953Srrs if (number_entries) { 3320163953Srrs for (i = 0; i < number_entries; i++) { 3321163953Srrs temp = ntohs(list[i]); 3322163953Srrs if (temp >= stcb->asoc.streamincnt) { 3323163953Srrs continue; 3324163953Srrs } 3325163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3326163953Srrs } 3327163953Srrs } else { 3328163953Srrs list = NULL; 3329163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3330163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3331163953Srrs } 3332163953Srrs } 3333172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3334163953Srrs} 3335163953Srrs 3336163953Srrsstatic void 3337163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3338163953Srrs{ 3339163953Srrs int i; 3340163953Srrs 3341163953Srrs if (number_entries == 0) { 3342163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3343163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 3344163953Srrs } 3345163953Srrs } else if (number_entries) { 3346163953Srrs for (i = 0; i < number_entries; i++) { 3347163953Srrs uint16_t temp; 3348163953Srrs 3349163953Srrs temp = ntohs(list[i]); 3350163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 3351163953Srrs /* no such stream */ 3352163953Srrs continue; 3353163953Srrs } 3354163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 3355163953Srrs } 3356163953Srrs } 3357172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3358163953Srrs} 3359163953Srrs 3360163953Srrs 3361163953Srrsstruct sctp_stream_reset_out_request * 3362163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3363163953Srrs{ 3364163953Srrs struct sctp_association *asoc; 3365163953Srrs struct sctp_stream_reset_out_req *req; 3366163953Srrs struct sctp_stream_reset_out_request *r; 3367163953Srrs struct sctp_tmit_chunk *chk; 3368163953Srrs int len, clen; 3369163953Srrs 3370163953Srrs asoc = &stcb->asoc; 3371163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3372169208Srrs asoc->stream_reset_outstanding = 0; 3373163953Srrs return (NULL); 3374163953Srrs } 3375163953Srrs if (stcb->asoc.str_reset == NULL) { 3376169208Srrs asoc->stream_reset_outstanding = 0; 3377163953Srrs return (NULL); 3378163953Srrs } 3379163953Srrs chk = stcb->asoc.str_reset; 3380163953Srrs if (chk->data == NULL) { 3381163953Srrs return (NULL); 3382163953Srrs } 3383163953Srrs if (bchk) { 3384163953Srrs /* he wants a copy of the chk pointer */ 3385163953Srrs *bchk = chk; 3386163953Srrs } 3387163953Srrs clen = chk->send_size; 3388163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3389163953Srrs r = &req->sr_req; 3390163953Srrs if (ntohl(r->request_seq) == seq) { 3391163953Srrs /* found it */ 3392163953Srrs return (r); 3393163953Srrs } 3394163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3395163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3396163953Srrs /* move to the next one, there can only be a max of two */ 3397163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3398163953Srrs if (ntohl(r->request_seq) == seq) { 3399163953Srrs return (r); 3400163953Srrs } 3401163953Srrs } 3402163953Srrs /* that seq is not here */ 3403163953Srrs return (NULL); 3404163953Srrs} 3405163953Srrs 3406163953Srrsstatic void 3407163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3408163953Srrs{ 3409163953Srrs struct sctp_association *asoc; 3410163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3411163953Srrs 3412163953Srrs if (stcb->asoc.str_reset == NULL) { 3413163953Srrs return; 3414163953Srrs } 3415166675Srrs asoc = &stcb->asoc; 3416166675Srrs 3417171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3418163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 3419163953Srrs chk, 3420163953Srrs sctp_next); 3421163953Srrs if (chk->data) { 3422163953Srrs sctp_m_freem(chk->data); 3423163953Srrs chk->data = NULL; 3424163953Srrs } 3425163953Srrs asoc->ctrl_queue_cnt--; 3426163953Srrs sctp_free_a_chunk(stcb, chk); 3427172156Srrs /* sa_ignore NO_NULL_CHK */ 3428163953Srrs stcb->asoc.str_reset = NULL; 3429163953Srrs} 3430163953Srrs 3431163953Srrs 3432163953Srrsstatic int 3433163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3434163953Srrs uint32_t seq, uint32_t action, 3435164139Srrs struct sctp_stream_reset_response *respin) 3436163953Srrs{ 3437163953Srrs uint16_t type; 3438163953Srrs int lparm_len; 3439163953Srrs struct sctp_association *asoc = &stcb->asoc; 3440163953Srrs struct sctp_tmit_chunk *chk; 3441163953Srrs struct sctp_stream_reset_out_request *srparam; 3442163953Srrs int number_entries; 3443163953Srrs 3444163953Srrs if (asoc->stream_reset_outstanding == 0) { 3445163953Srrs /* duplicate */ 3446163953Srrs return (0); 3447163953Srrs } 3448163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3449163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 3450163953Srrs if (srparam) { 3451163953Srrs stcb->asoc.str_reset_seq_out++; 3452163953Srrs type = ntohs(srparam->ph.param_type); 3453163953Srrs lparm_len = ntohs(srparam->ph.param_length); 3454163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 3455164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3456163953Srrs asoc->stream_reset_out_is_outstanding = 0; 3457163953Srrs if (asoc->stream_reset_outstanding) 3458163953Srrs asoc->stream_reset_outstanding--; 3459163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3460163953Srrs /* do it */ 3461163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3462163953Srrs } else { 3463172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3464163953Srrs } 3465163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3466163953Srrs /* Answered my request */ 3467164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3468163953Srrs if (asoc->stream_reset_outstanding) 3469163953Srrs asoc->stream_reset_outstanding--; 3470163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 3471172090Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3472163953Srrs } 3473188854Srrs } else if (type == SCTP_STR_RESET_ADD_STREAMS) { 3474188854Srrs /* Ok we now may have more streams */ 3475189121Srrs if (asoc->stream_reset_outstanding) 3476189121Srrs asoc->stream_reset_outstanding--; 3477188854Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3478188854Srrs /* Put the new streams into effect */ 3479188854Srrs stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize; 3480188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb, 3481188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3482188854Srrs } else { 3483188854Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb, 3484188854Srrs (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED); 3485188854Srrs } 3486163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3487163953Srrs /** 3488163953Srrs * a) Adopt the new in tsn. 3489163953Srrs * b) reset the map 3490163953Srrs * c) Adopt the new out-tsn 3491163953Srrs */ 3492163953Srrs struct sctp_stream_reset_response_tsn *resp; 3493163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3494163953Srrs int abort_flag = 0; 3495163953Srrs 3496163953Srrs if (respin == NULL) { 3497163953Srrs /* huh ? */ 3498163953Srrs return (0); 3499163953Srrs } 3500163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 3501163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 3502163953Srrs asoc->stream_reset_outstanding--; 3503163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3504163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3505163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3506170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3507163953Srrs if (abort_flag) { 3508163953Srrs return (1); 3509163953Srrs } 3510163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3511179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3512179783Srrs sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3513179783Srrs } 3514180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3515163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3516163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3517185694Srrs 3518185694Srrs /* 3519185694Srrs * EY 05/13/08 - nr_sack: to keep 3520185694Srrs * nr_mapping array be consistent 3521185694Srrs * with mapping_array 3522185694Srrs */ 3523185694Srrs if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) { 3524185694Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3525185694Srrs stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.mapping_array_base_tsn; 3526185694Srrs memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size); 3527185694Srrs } 3528163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3529163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3530163953Srrs 3531163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3532163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3533163953Srrs 3534163953Srrs } 3535163953Srrs } 3536163953Srrs /* get rid of the request and get the request flags */ 3537163953Srrs if (asoc->stream_reset_outstanding == 0) { 3538163953Srrs sctp_clean_up_stream_reset(stcb); 3539163953Srrs } 3540163953Srrs } 3541163953Srrs } 3542163953Srrs return (0); 3543163953Srrs} 3544163953Srrs 3545163953Srrsstatic void 3546163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3547163953Srrs struct sctp_tmit_chunk *chk, 3548170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 3549163953Srrs{ 3550163953Srrs uint32_t seq; 3551163953Srrs int len, i; 3552163953Srrs int number_entries; 3553163953Srrs uint16_t temp; 3554163953Srrs 3555163953Srrs /* 3556163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 3557163953Srrs * seq_in is right. 3558163953Srrs */ 3559163953Srrs struct sctp_association *asoc = &stcb->asoc; 3560163953Srrs 3561163953Srrs seq = ntohl(req->request_seq); 3562163953Srrs if (asoc->str_reset_seq_in == seq) { 3563170992Srrs if (trunc) { 3564170992Srrs /* Can't do it, since they exceeded our buffer size */ 3565170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3566170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3567170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3568170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3569163953Srrs len = ntohs(req->ph.param_length); 3570163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3571163953Srrs for (i = 0; i < number_entries; i++) { 3572163953Srrs temp = ntohs(req->list_of_streams[i]); 3573163953Srrs req->list_of_streams[i] = temp; 3574163953Srrs } 3575163953Srrs /* move the reset action back one */ 3576163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3577163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3578163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3579163953Srrs asoc->str_reset_seq_out, 3580163953Srrs seq, (asoc->sending_seq - 1)); 3581163953Srrs asoc->stream_reset_out_is_outstanding = 1; 3582163953Srrs asoc->str_reset = chk; 3583163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3584163953Srrs stcb->asoc.stream_reset_outstanding++; 3585163953Srrs } else { 3586163953Srrs /* Can't do it, since we have sent one out */ 3587163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3588163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3589163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3590163953Srrs } 3591163953Srrs asoc->str_reset_seq_in++; 3592163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3593163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3594163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3595163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3596163953Srrs } else { 3597163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3598163953Srrs } 3599163953Srrs} 3600163953Srrs 3601163953Srrsstatic int 3602163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3603163953Srrs struct sctp_tmit_chunk *chk, 3604163953Srrs struct sctp_stream_reset_tsn_request *req) 3605163953Srrs{ 3606163953Srrs /* reset all in and out and update the tsn */ 3607163953Srrs /* 3608163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3609163953Srrs * and set cum-ack to it. Also process this selected number as a 3610163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3611163953Srrs */ 3612163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3613163953Srrs struct sctp_association *asoc = &stcb->asoc; 3614163953Srrs int abort_flag = 0; 3615163953Srrs uint32_t seq; 3616163953Srrs 3617163953Srrs seq = ntohl(req->request_seq); 3618163953Srrs if (asoc->str_reset_seq_in == seq) { 3619163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3620163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3621163953Srrs fwdtsn.ch.chunk_flags = 0; 3622163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3623170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3624163953Srrs if (abort_flag) { 3625163953Srrs return (1); 3626163953Srrs } 3627163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3628179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3629179783Srrs sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3630179783Srrs } 3631180955Srrs stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3632163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3633163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3634185694Srrs /* 3635185694Srrs * EY 05/13/08 -nr_sack: to keep nr_mapping array consistent 3636185694Srrs * with mapping array 3637185694Srrs */ 3638185694Srrs if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) { 3639185694Srrs stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3640185694Srrs stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3641185694Srrs memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size); 3642185694Srrs } 3643163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3644163953Srrs /* save off historical data for retrans */ 3645163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3646163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3647163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3648163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3649163953Srrs 3650163953Srrs sctp_add_stream_reset_result_tsn(chk, 3651163953Srrs ntohl(req->request_seq), 3652163953Srrs SCTP_STREAM_RESET_PERFORMED, 3653163953Srrs stcb->asoc.sending_seq, 3654163953Srrs stcb->asoc.mapping_array_base_tsn); 3655163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3656163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3657163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3658163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3659163953Srrs 3660163953Srrs asoc->str_reset_seq_in++; 3661163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3662163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3663163953Srrs stcb->asoc.last_sending_seq[0], 3664163953Srrs stcb->asoc.last_base_tsnsent[0] 3665163953Srrs ); 3666163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3667163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3668163953Srrs stcb->asoc.last_sending_seq[1], 3669163953Srrs stcb->asoc.last_base_tsnsent[1] 3670163953Srrs ); 3671163953Srrs } else { 3672163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3673163953Srrs } 3674163953Srrs return (0); 3675163953Srrs} 3676163953Srrs 3677163953Srrsstatic void 3678163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3679163953Srrs struct sctp_tmit_chunk *chk, 3680170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3681163953Srrs{ 3682163953Srrs uint32_t seq, tsn; 3683163953Srrs int number_entries, len; 3684163953Srrs struct sctp_association *asoc = &stcb->asoc; 3685163953Srrs 3686163953Srrs seq = ntohl(req->request_seq); 3687163953Srrs 3688163953Srrs /* now if its not a duplicate we process it */ 3689163953Srrs if (asoc->str_reset_seq_in == seq) { 3690163953Srrs len = ntohs(req->ph.param_length); 3691163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3692163953Srrs /* 3693163953Srrs * the sender is resetting, handle the list issue.. we must 3694163953Srrs * a) verify if we can do the reset, if so no problem b) If 3695163953Srrs * we can't do the reset we must copy the request. c) queue 3696163953Srrs * it, and setup the data in processor to trigger it off 3697163953Srrs * when needed and dequeue all the queued data. 3698163953Srrs */ 3699163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3700163953Srrs 3701163953Srrs /* move the reset action back one */ 3702163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3703170992Srrs if (trunc) { 3704170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3705170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3706170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3707163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3708163953Srrs /* we can do it now */ 3709163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3710163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3711163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3712163953Srrs } else { 3713163953Srrs /* 3714163953Srrs * we must queue it up and thus wait for the TSN's 3715163953Srrs * to arrive that are at or before tsn 3716163953Srrs */ 3717163953Srrs struct sctp_stream_reset_list *liste; 3718163953Srrs int siz; 3719163953Srrs 3720163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3721163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3722170091Srrs siz, SCTP_M_STRESET); 3723163953Srrs if (liste == NULL) { 3724163953Srrs /* gak out of memory */ 3725163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3726163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3727163953Srrs return; 3728163953Srrs } 3729163953Srrs liste->tsn = tsn; 3730163953Srrs liste->number_entries = number_entries; 3731163953Srrs memcpy(&liste->req, req, 3732163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3733163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3734163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3735163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3736163953Srrs } 3737163953Srrs asoc->str_reset_seq_in++; 3738163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3739163953Srrs /* 3740163953Srrs * one seq back, just echo back last action since my 3741163953Srrs * response was lost. 3742163953Srrs */ 3743163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3744163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3745163953Srrs /* 3746163953Srrs * two seq back, just echo back last action since my 3747163953Srrs * response was lost. 3748163953Srrs */ 3749163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3750163953Srrs } else { 3751163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3752163953Srrs } 3753163953Srrs} 3754163953Srrs 3755188854Srrsstatic void 3756188854Srrssctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3757188854Srrs struct sctp_stream_reset_add_strm *str_add) 3758188854Srrs{ 3759188854Srrs /* 3760188854Srrs * Peer is requesting to add more streams. If its within our 3761188854Srrs * max-streams we will allow it. 3762188854Srrs */ 3763188854Srrs uint16_t num_stream, i; 3764188854Srrs uint32_t seq; 3765189121Srrs struct sctp_association *asoc = &stcb->asoc; 3766189121Srrs struct sctp_queued_to_read *ctl; 3767188854Srrs 3768188854Srrs /* Get the number. */ 3769188854Srrs seq = ntohl(str_add->request_seq); 3770188854Srrs num_stream = ntohs(str_add->number_of_streams); 3771188854Srrs /* Now what would be the new total? */ 3772189121Srrs if (asoc->str_reset_seq_in == seq) { 3773189121Srrs num_stream += stcb->asoc.streamincnt; 3774189121Srrs if (num_stream > stcb->asoc.max_inbound_streams) { 3775189121Srrs /* We must reject it they ask for to many */ 3776189121Srrs denied: 3777189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3778189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3779189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3780189121Srrs } else { 3781189121Srrs /* Ok, we can do that :-) */ 3782189121Srrs struct sctp_stream_in *oldstrm; 3783189121Srrs 3784189121Srrs /* save off the old */ 3785189121Srrs oldstrm = stcb->asoc.strmin; 3786189121Srrs SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3787189121Srrs (num_stream * sizeof(struct sctp_stream_in)), 3788189121Srrs SCTP_M_STRMI); 3789189121Srrs if (stcb->asoc.strmin == NULL) { 3790189121Srrs stcb->asoc.strmin = oldstrm; 3791189121Srrs goto denied; 3792189121Srrs } 3793189121Srrs /* copy off the old data */ 3794189121Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 3795189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3796189121Srrs stcb->asoc.strmin[i].stream_no = i; 3797189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3798189121Srrs stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3799189121Srrs /* now anything on those queues? */ 3800189121Srrs while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) { 3801189121Srrs ctl = TAILQ_FIRST(&oldstrm[i].inqueue); 3802189121Srrs TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3803189121Srrs TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3804189121Srrs } 3805189121Srrs } 3806189121Srrs /* Init the new streams */ 3807189121Srrs for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3808189121Srrs TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3809189121Srrs stcb->asoc.strmin[i].stream_no = i; 3810189121Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3811189121Srrs stcb->asoc.strmin[i].delivery_started = 0; 3812189121Srrs } 3813189121Srrs SCTP_FREE(oldstrm, SCTP_M_STRMI); 3814189121Srrs /* update the size */ 3815189121Srrs stcb->asoc.streamincnt = num_stream; 3816189121Srrs /* Send the ack */ 3817189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3818189121Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3819189121Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3820189121Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb, 3821189121Srrs (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED); 3822189121Srrs } 3823189121Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3824189121Srrs /* 3825189121Srrs * one seq back, just echo back last action since my 3826189121Srrs * response was lost. 3827189121Srrs */ 3828189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3829189121Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3830189121Srrs /* 3831189121Srrs * two seq back, just echo back last action since my 3832189121Srrs * response was lost. 3833189121Srrs */ 3834189121Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3835188854Srrs } else { 3836189121Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3837188854Srrs 3838188854Srrs } 3839188854Srrs} 3840188854Srrs 3841170992Srrs#ifdef __GNUC__ 3842170992Srrs__attribute__((noinline)) 3843170992Srrs#endif 3844170992Srrs static int 3845170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3846170992Srrs struct sctp_stream_reset_out_req *sr_req) 3847163953Srrs{ 3848163953Srrs int chk_length, param_len, ptype; 3849170992Srrs struct sctp_paramhdr pstore; 3850170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3851170992Srrs 3852163953Srrs uint32_t seq; 3853163953Srrs int num_req = 0; 3854170992Srrs int trunc = 0; 3855163953Srrs struct sctp_tmit_chunk *chk; 3856163953Srrs struct sctp_chunkhdr *ch; 3857163953Srrs struct sctp_paramhdr *ph; 3858166675Srrs int ret_code = 0; 3859166675Srrs int num_param = 0; 3860163953Srrs 3861163953Srrs /* now it may be a reset or a reset-response */ 3862163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3863163953Srrs 3864163953Srrs /* setup for adding the response */ 3865163953Srrs sctp_alloc_a_chunk(stcb, chk); 3866163953Srrs if (chk == NULL) { 3867163953Srrs return (ret_code); 3868163953Srrs } 3869163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3870169208Srrs chk->rec.chunk_id.can_take_data = 0; 3871163953Srrs chk->asoc = &stcb->asoc; 3872163953Srrs chk->no_fr_allowed = 0; 3873163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3874166023Srrs chk->book_size_scale = 0; 3875165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3876163953Srrs if (chk->data == NULL) { 3877163953Srrsstrres_nochunk: 3878163953Srrs if (chk->data) { 3879163953Srrs sctp_m_freem(chk->data); 3880163953Srrs chk->data = NULL; 3881163953Srrs } 3882163953Srrs sctp_free_a_chunk(stcb, chk); 3883163953Srrs return (ret_code); 3884163953Srrs } 3885165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3886163953Srrs 3887163953Srrs /* setup chunk parameters */ 3888163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3889163953Srrs chk->snd_count = 0; 3890163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3891163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3892163953Srrs 3893163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3894163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3895163953Srrs ch->chunk_flags = 0; 3896163953Srrs ch->chunk_length = htons(chk->send_size); 3897165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3898170992Srrs offset += sizeof(struct sctp_chunkhdr); 3899163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3900170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3901170992Srrs if (ph == NULL) 3902170992Srrs break; 3903163953Srrs param_len = ntohs(ph->param_length); 3904163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3905163953Srrs /* bad param */ 3906163953Srrs break; 3907163953Srrs } 3908170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3909170992Srrs (uint8_t *) & cstore); 3910163953Srrs ptype = ntohs(ph->param_type); 3911163953Srrs num_param++; 3912170994Srrs if (param_len > (int)sizeof(cstore)) { 3913170992Srrs trunc = 1; 3914170992Srrs } else { 3915170992Srrs trunc = 0; 3916170992Srrs } 3917170992Srrs 3918163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3919163953Srrs /* hit the max of parameters already sorry.. */ 3920163953Srrs break; 3921163953Srrs } 3922163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3923163953Srrs struct sctp_stream_reset_out_request *req_out; 3924163953Srrs 3925163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3926163953Srrs num_req++; 3927163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3928163953Srrs seq = ntohl(req_out->response_seq); 3929163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3930163953Srrs /* implicit ack */ 3931169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3932163953Srrs } 3933163953Srrs } 3934170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3935188854Srrs } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) { 3936188854Srrs struct sctp_stream_reset_add_strm *str_add; 3937188854Srrs 3938188854Srrs str_add = (struct sctp_stream_reset_add_strm *)ph; 3939188854Srrs num_req++; 3940188854Srrs sctp_handle_str_reset_add_strm(stcb, chk, str_add); 3941163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3942163953Srrs struct sctp_stream_reset_in_request *req_in; 3943163953Srrs 3944163953Srrs num_req++; 3945170992Srrs 3946163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3947170992Srrs 3948170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3949163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3950163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3951163953Srrs 3952163953Srrs num_req++; 3953163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3954170992Srrs 3955163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3956163953Srrs ret_code = 1; 3957163953Srrs goto strres_nochunk; 3958163953Srrs } 3959163953Srrs /* no more */ 3960163953Srrs break; 3961163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3962163953Srrs struct sctp_stream_reset_response *resp; 3963163953Srrs uint32_t result; 3964163953Srrs 3965163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3966163953Srrs seq = ntohl(resp->response_seq); 3967163953Srrs result = ntohl(resp->result); 3968163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3969163953Srrs ret_code = 1; 3970163953Srrs goto strres_nochunk; 3971163953Srrs } 3972163953Srrs } else { 3973163953Srrs break; 3974163953Srrs } 3975170992Srrs offset += SCTP_SIZE32(param_len); 3976163953Srrs chk_length -= SCTP_SIZE32(param_len); 3977163953Srrs } 3978163953Srrs if (num_req == 0) { 3979163953Srrs /* we have no response free the stuff */ 3980163953Srrs goto strres_nochunk; 3981163953Srrs } 3982163953Srrs /* ok we have a chunk to link in */ 3983163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3984163953Srrs chk, 3985163953Srrs sctp_next); 3986163953Srrs stcb->asoc.ctrl_queue_cnt++; 3987163953Srrs return (ret_code); 3988163953Srrs} 3989163953Srrs 3990163953Srrs/* 3991163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3992163953Srrs * to handle this, either we get the whole packet and must disect it 3993163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3994163953Srrs * from a middle box that did the disectting for us. 3995163953Srrs */ 3996163953Srrsstatic void 3997163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3998170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3999163953Srrs{ 4000163953Srrs uint32_t bottle_bw, on_queue; 4001163953Srrs uint16_t trunc_len; 4002163953Srrs unsigned int chlen; 4003163953Srrs unsigned int at; 4004163953Srrs struct sctp_chunk_desc desc; 4005163953Srrs struct sctp_chunkhdr *ch; 4006163953Srrs 4007163953Srrs chlen = ntohs(cp->ch.chunk_length); 4008163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 4009163953Srrs /* XXX possible chlen underflow */ 4010163953Srrs if (chlen == 0) { 4011163953Srrs ch = NULL; 4012163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4013163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 4014163953Srrs } else { 4015163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4016163953Srrs chlen -= sizeof(struct sctphdr); 4017163953Srrs /* XXX possible chlen underflow */ 4018163953Srrs memset(&desc, 0, sizeof(desc)); 4019163953Srrs } 4020163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 4021170781Srrs if (trunc_len > limit) { 4022170781Srrs trunc_len = limit; 4023170781Srrs } 4024163953Srrs /* now the chunks themselves */ 4025163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4026163953Srrs desc.chunk_type = ch->chunk_type; 4027163953Srrs /* get amount we need to move */ 4028163953Srrs at = ntohs(ch->chunk_length); 4029163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 4030163953Srrs /* corrupt chunk, maybe at the end? */ 4031163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4032163953Srrs break; 4033163953Srrs } 4034163953Srrs if (trunc_len == 0) { 4035163953Srrs /* we are supposed to have all of it */ 4036163953Srrs if (at > chlen) { 4037163953Srrs /* corrupt skip it */ 4038163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 4039163953Srrs break; 4040163953Srrs } 4041163953Srrs } else { 4042163953Srrs /* is there enough of it left ? */ 4043163953Srrs if (desc.chunk_type == SCTP_DATA) { 4044163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 4045163953Srrs sizeof(desc.data_bytes))) { 4046163953Srrs break; 4047163953Srrs } 4048163953Srrs } else { 4049163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4050163953Srrs break; 4051163953Srrs } 4052163953Srrs } 4053163953Srrs } 4054163953Srrs if (desc.chunk_type == SCTP_DATA) { 4055163953Srrs /* can we get out the tsn? */ 4056163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4057163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 4058163953Srrs 4059163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4060163953Srrs /* yep */ 4061163953Srrs struct sctp_data_chunk *dcp; 4062163953Srrs uint8_t *ddp; 4063163953Srrs unsigned int iii; 4064163953Srrs 4065163953Srrs dcp = (struct sctp_data_chunk *)ch; 4066163953Srrs ddp = (uint8_t *) (dcp + 1); 4067163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4068163953Srrs desc.data_bytes[iii] = ddp[iii]; 4069163953Srrs } 4070163953Srrs desc.tsn_ifany = dcp->dp.tsn; 4071163953Srrs } else { 4072163953Srrs /* nope we are done. */ 4073163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 4074163953Srrs break; 4075163953Srrs } 4076163953Srrs } else { 4077163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4078163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 4079163953Srrs } 4080163953Srrs 4081163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4082163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 4083163953Srrs break; 4084163953Srrs } 4085163953Srrs if (SCTP_SIZE32(at) > chlen) { 4086163953Srrs break; 4087163953Srrs } 4088163953Srrs chlen -= SCTP_SIZE32(at); 4089163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 4090163953Srrs /* done, none left */ 4091163953Srrs break; 4092163953Srrs } 4093163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4094163953Srrs } 4095163953Srrs /* Now update any rwnd --- possibly */ 4096163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4097163953Srrs /* From a peer, we get a rwnd report */ 4098163953Srrs uint32_t a_rwnd; 4099163953Srrs 4100163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 4101163953Srrs 4102163953Srrs bottle_bw = ntohl(cp->bottle_bw); 4103163953Srrs on_queue = ntohl(cp->current_onq); 4104163953Srrs if (bottle_bw && on_queue) { 4105163953Srrs /* a rwnd report is in here */ 4106163953Srrs if (bottle_bw > on_queue) 4107163953Srrs a_rwnd = bottle_bw - on_queue; 4108163953Srrs else 4109163953Srrs a_rwnd = 0; 4110163953Srrs 4111163953Srrs if (a_rwnd == 0) 4112163953Srrs stcb->asoc.peers_rwnd = 0; 4113163953Srrs else { 4114163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 4115163953Srrs stcb->asoc.peers_rwnd = 4116163953Srrs a_rwnd - stcb->asoc.total_flight; 4117163953Srrs } else { 4118163953Srrs stcb->asoc.peers_rwnd = 0; 4119163953Srrs } 4120163953Srrs if (stcb->asoc.peers_rwnd < 4121163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4122163953Srrs /* SWS sender side engages */ 4123163953Srrs stcb->asoc.peers_rwnd = 0; 4124163953Srrs } 4125163953Srrs } 4126163953Srrs } 4127163953Srrs } else { 4128163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 4129163953Srrs } 4130163953Srrs 4131163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 4132163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4133163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 4134163953Srrs (stcb->asoc.sat_network)) { 4135163953Srrs /* 4136163953Srrs * This is debateable but for sat networks it makes sense 4137163953Srrs * Note if a T3 timer has went off, we will prohibit any 4138163953Srrs * changes to cwnd until we exit the t3 loss recovery. 4139163953Srrs */ 4140171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4141171440Srrs net, cp, &bottle_bw, &on_queue); 4142163953Srrs } 4143163953Srrs} 4144163953Srrs 4145163953Srrs/* 4146163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4147163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4148163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 4149163953Srrs * length of the complete packet outputs: - length: modified to remaining 4150163953Srrs * length after control processing - netp: modified to new sctp_nets after 4151163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4152163953Srrs * bad packet,...) otherwise return the tcb for this packet 4153163953Srrs */ 4154170099Srrs#ifdef __GNUC__ 4155170099Srrs__attribute__((noinline)) 4156170099Srrs#endif 4157170099Srrs static struct sctp_tcb * 4158170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4159170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4160170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4161179157Srrs uint32_t vrf_id, uint16_t port) 4162163953Srrs{ 4163163953Srrs struct sctp_association *asoc; 4164163953Srrs uint32_t vtag_in; 4165163953Srrs int num_chunks = 0; /* number of control chunks processed */ 4166170140Srrs uint32_t chk_length; 4167163953Srrs int ret; 4168168299Srrs int abort_no_unlock = 0; 4169163953Srrs 4170163953Srrs /* 4171163953Srrs * How big should this be, and should it be alloc'd? Lets try the 4172163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 4173163953Srrs * until we get into jumbo grams and such.. 4174163953Srrs */ 4175166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4176163953Srrs struct sctp_tcb *locked_tcb = stcb; 4177163953Srrs int got_auth = 0; 4178163953Srrs uint32_t auth_offset = 0, auth_len = 0; 4179163953Srrs int auth_skipped = 0; 4180171990Srrs int asconf_cnt = 0; 4181163953Srrs 4182172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4183172090Srrs struct socket *so; 4184172090Srrs 4185172090Srrs#endif 4186172090Srrs 4187169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4188169420Srrs iphlen, *offset, length, stcb); 4189163953Srrs 4190163953Srrs /* validate chunk header length... */ 4191163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4192170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4193170056Srrs ntohs(ch->chunk_length)); 4194170091Srrs if (locked_tcb) { 4195170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4196170091Srrs } 4197163953Srrs return (NULL); 4198163953Srrs } 4199163953Srrs /* 4200163953Srrs * validate the verification tag 4201163953Srrs */ 4202163953Srrs vtag_in = ntohl(sh->v_tag); 4203163953Srrs 4204165220Srrs if (locked_tcb) { 4205165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 4206165220Srrs } 4207163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4208170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4209170056Srrs ntohs(ch->chunk_length), vtag_in); 4210163953Srrs if (vtag_in != 0) { 4211163953Srrs /* protocol error- silently discard... */ 4212163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4213169378Srrs if (locked_tcb) { 4214163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4215169378Srrs } 4216163953Srrs return (NULL); 4217163953Srrs } 4218163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4219163953Srrs /* 4220163953Srrs * If there is no stcb, skip the AUTH chunk and process 4221163953Srrs * later after a stcb is found (to validate the lookup was 4222163953Srrs * valid. 4223163953Srrs */ 4224163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4225179783Srrs (stcb == NULL) && 4226179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4227163953Srrs /* save this chunk for later processing */ 4228163953Srrs auth_skipped = 1; 4229163953Srrs auth_offset = *offset; 4230163953Srrs auth_len = ntohs(ch->chunk_length); 4231163953Srrs 4232163953Srrs /* (temporarily) move past this chunk */ 4233163953Srrs *offset += SCTP_SIZE32(auth_len); 4234163953Srrs if (*offset >= length) { 4235163953Srrs /* no more data left in the mbuf chain */ 4236163953Srrs *offset = length; 4237170091Srrs if (locked_tcb) { 4238170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4239170091Srrs } 4240163953Srrs return (NULL); 4241163953Srrs } 4242163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4243163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4244163953Srrs } 4245169420Srrs if (ch == NULL) { 4246169420Srrs /* Help */ 4247169420Srrs *offset = length; 4248170091Srrs if (locked_tcb) { 4249170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4250170091Srrs } 4251169420Srrs return (NULL); 4252169420Srrs } 4253163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4254163953Srrs goto process_control_chunks; 4255163953Srrs } 4256163953Srrs /* 4257163953Srrs * first check if it's an ASCONF with an unknown src addr we 4258163953Srrs * need to look inside to find the association 4259163953Srrs */ 4260163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4261171990Srrs struct sctp_chunkhdr *asconf_ch = ch; 4262171990Srrs uint32_t asconf_offset = 0, asconf_len = 0; 4263171990Srrs 4264163953Srrs /* inp's refcount may be reduced */ 4265163953Srrs SCTP_INP_INCR_REF(inp); 4266163953Srrs 4267171990Srrs asconf_offset = *offset; 4268171990Srrs do { 4269171990Srrs asconf_len = ntohs(asconf_ch->chunk_length); 4270171990Srrs if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4271171990Srrs break; 4272171990Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 4273185694Srrs *offset, sh, &inp, netp, vrf_id); 4274171990Srrs if (stcb != NULL) 4275171990Srrs break; 4276171990Srrs asconf_offset += SCTP_SIZE32(asconf_len); 4277171990Srrs asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4278171990Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4279171990Srrs } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4280163953Srrs if (stcb == NULL) { 4281163953Srrs /* 4282163953Srrs * reduce inp's refcount if not reduced in 4283163953Srrs * sctp_findassociation_ep_asconf(). 4284163953Srrs */ 4285163953Srrs SCTP_INP_DECR_REF(inp); 4286171990Srrs } else { 4287171990Srrs locked_tcb = stcb; 4288163953Srrs } 4289171990Srrs 4290163953Srrs /* now go back and verify any auth chunk to be sure */ 4291163953Srrs if (auth_skipped && (stcb != NULL)) { 4292163953Srrs struct sctp_auth_chunk *auth; 4293163953Srrs 4294163953Srrs auth = (struct sctp_auth_chunk *) 4295163953Srrs sctp_m_getptr(m, auth_offset, 4296163953Srrs auth_len, chunk_buf); 4297163953Srrs got_auth = 1; 4298163953Srrs auth_skipped = 0; 4299169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4300163953Srrs auth_offset)) { 4301163953Srrs /* auth HMAC failed so dump it */ 4302163953Srrs *offset = length; 4303170091Srrs if (locked_tcb) { 4304170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 4305170091Srrs } 4306163953Srrs return (NULL); 4307163953Srrs } else { 4308163953Srrs /* remaining chunks are HMAC checked */ 4309163953Srrs stcb->asoc.authenticated = 1; 4310163953Srrs } 4311163953Srrs } 4312163953Srrs } 4313163953Srrs if (stcb == NULL) { 4314163953Srrs /* no association, so it's out of the blue... */ 4315169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4316179157Srrs vrf_id, port); 4317163953Srrs *offset = length; 4318169378Srrs if (locked_tcb) { 4319163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4320169378Srrs } 4321163953Srrs return (NULL); 4322163953Srrs } 4323163953Srrs asoc = &stcb->asoc; 4324163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 4325163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4326163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4327163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4328163953Srrs if ((vtag_in == asoc->my_vtag) || 4329163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4330163953Srrs (vtag_in == asoc->peer_vtag))) { 4331163953Srrs /* this is valid */ 4332163953Srrs } else { 4333163953Srrs /* drop this packet... */ 4334163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4335169378Srrs if (locked_tcb) { 4336163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4337169378Srrs } 4338163953Srrs return (NULL); 4339163953Srrs } 4340163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4341163953Srrs if (vtag_in != asoc->my_vtag) { 4342163953Srrs /* 4343163953Srrs * this could be a stale SHUTDOWN-ACK or the 4344163953Srrs * peer never got the SHUTDOWN-COMPLETE and 4345163953Srrs * is still hung; we have started a new asoc 4346163953Srrs * but it won't complete until the shutdown 4347163953Srrs * is completed 4348163953Srrs */ 4349169378Srrs if (locked_tcb) { 4350163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4351169378Srrs } 4352163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4353179157Srrs NULL, vrf_id, port); 4354163953Srrs return (NULL); 4355163953Srrs } 4356163953Srrs } else { 4357163953Srrs /* for all other chunks, vtag must match */ 4358163953Srrs if (vtag_in != asoc->my_vtag) { 4359163953Srrs /* invalid vtag... */ 4360169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4361169420Srrs "invalid vtag: %xh, expect %xh\n", 4362169420Srrs vtag_in, asoc->my_vtag); 4363163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4364169378Srrs if (locked_tcb) { 4365163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4366169378Srrs } 4367163953Srrs *offset = length; 4368163953Srrs return (NULL); 4369163953Srrs } 4370163953Srrs } 4371163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 4372163953Srrs /* 4373163953Srrs * process all control chunks... 4374163953Srrs */ 4375163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4376185694Srrs /* EY */ 4377185694Srrs (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4378163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4379163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4380163953Srrs /* implied cookie-ack.. we must have lost the ack */ 4381179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4382171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4383171943Srrs stcb->asoc.overall_error_count, 4384171943Srrs 0, 4385171943Srrs SCTP_FROM_SCTP_INPUT, 4386171943Srrs __LINE__); 4387171943Srrs } 4388163953Srrs stcb->asoc.overall_error_count = 0; 4389163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4390163953Srrs *netp); 4391163953Srrs } 4392163953Srrsprocess_control_chunks: 4393163953Srrs while (IS_SCTP_CONTROL(ch)) { 4394163953Srrs /* validate chunk length */ 4395163953Srrs chk_length = ntohs(ch->chunk_length); 4396169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4397169420Srrs ch->chunk_type, chk_length); 4398170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4399170140Srrs if (chk_length < sizeof(*ch) || 4400170140Srrs (*offset + (int)chk_length) > length) { 4401163953Srrs *offset = length; 4402169378Srrs if (locked_tcb) { 4403163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4404169378Srrs } 4405163953Srrs return (NULL); 4406163953Srrs } 4407163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4408163953Srrs /* 4409163953Srrs * INIT-ACK only gets the init ack "header" portion only 4410163953Srrs * because we don't have to process the peer's COOKIE. All 4411163953Srrs * others get a complete chunk. 4412163953Srrs */ 4413169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4414169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 4415163953Srrs /* get an init-ack chunk */ 4416163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4417163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 4418163953Srrs if (ch == NULL) { 4419163953Srrs *offset = length; 4420169378Srrs if (locked_tcb) { 4421163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4422169378Srrs } 4423163953Srrs return (NULL); 4424163953Srrs } 4425170642Srrs } else { 4426170786Srrs /* For cookies and all other chunks. */ 4427169208Srrs if (chk_length > sizeof(chunk_buf)) { 4428169208Srrs /* 4429169208Srrs * use just the size of the chunk buffer so 4430170642Srrs * the front part of our chunks fit in 4431170642Srrs * contiguous space up to the chunk buffer 4432170642Srrs * size (508 bytes). For chunks that need to 4433170786Srrs * get more than that they must use the 4434170642Srrs * sctp_m_getptr() function or other means 4435170786Srrs * (e.g. know how to parse mbuf chains). 4436170786Srrs * Cookies do this already. 4437169208Srrs */ 4438169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4439169208Srrs (sizeof(chunk_buf) - 4), 4440169208Srrs chunk_buf); 4441169208Srrs if (ch == NULL) { 4442169208Srrs *offset = length; 4443169378Srrs if (locked_tcb) { 4444169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 4445169378Srrs } 4446169208Srrs return (NULL); 4447169208Srrs } 4448169208Srrs } else { 4449169208Srrs /* We can fit it all */ 4450170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4451170642Srrs chk_length, chunk_buf); 4452170642Srrs if (ch == NULL) { 4453170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4454170642Srrs *offset = length; 4455170642Srrs if (locked_tcb) { 4456170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 4457169208Srrs } 4458170642Srrs return (NULL); 4459163953Srrs } 4460163953Srrs } 4461163953Srrs } 4462163953Srrs num_chunks++; 4463163953Srrs /* Save off the last place we got a control from */ 4464163953Srrs if (stcb != NULL) { 4465169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4466163953Srrs /* 4467163953Srrs * allow last_control to be NULL if 4468163953Srrs * ASCONF... ASCONF processing will find the 4469163953Srrs * right net later 4470163953Srrs */ 4471169420Srrs if ((netp != NULL) && (*netp != NULL)) 4472169420Srrs stcb->asoc.last_control_chunk_from = *netp; 4473163953Srrs } 4474163953Srrs } 4475163953Srrs#ifdef SCTP_AUDITING_ENABLED 4476163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 4477163953Srrs#endif 4478163953Srrs 4479163953Srrs /* check to see if this chunk required auth, but isn't */ 4480179783Srrs if ((stcb != NULL) && 4481179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4482179783Srrs sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4483163953Srrs !stcb->asoc.authenticated) { 4484163953Srrs /* "silently" ignore */ 4485163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4486163953Srrs goto next_chunk; 4487163953Srrs } 4488163953Srrs switch (ch->chunk_type) { 4489163953Srrs case SCTP_INITIATION: 4490163953Srrs /* must be first and only chunk */ 4491169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4492163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4493163953Srrs /* We are not interested anymore? */ 4494163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4495163953Srrs /* 4496163953Srrs * collision case where we are 4497163953Srrs * sending to them too 4498163953Srrs */ 4499163953Srrs ; 4500163953Srrs } else { 4501169378Srrs if (locked_tcb) { 4502163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4503169378Srrs } 4504163953Srrs *offset = length; 4505163953Srrs return (NULL); 4506163953Srrs } 4507163953Srrs } 4508171990Srrs if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) || 4509171990Srrs (num_chunks > 1) || 4510179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4511163953Srrs *offset = length; 4512169378Srrs if (locked_tcb) { 4513163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4514169378Srrs } 4515163953Srrs return (NULL); 4516163953Srrs } 4517163953Srrs if ((stcb != NULL) && 4518163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 4519163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 4520163953Srrs sctp_send_shutdown_ack(stcb, 4521163953Srrs stcb->asoc.primary_destination); 4522163953Srrs *offset = length; 4523172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4524169378Srrs if (locked_tcb) { 4525163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4526169378Srrs } 4527163953Srrs return (NULL); 4528163953Srrs } 4529169420Srrs if (netp) { 4530169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 4531169420Srrs (struct sctp_init_chunk *)ch, inp, 4532179157Srrs stcb, *netp, &abort_no_unlock, vrf_id, port); 4533169420Srrs } 4534168299Srrs if (abort_no_unlock) 4535168299Srrs return (NULL); 4536168299Srrs 4537163953Srrs *offset = length; 4538169378Srrs if (locked_tcb) { 4539163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4540169378Srrs } 4541163953Srrs return (NULL); 4542163953Srrs break; 4543170642Srrs case SCTP_PAD_CHUNK: 4544170642Srrs break; 4545163953Srrs case SCTP_INITIATION_ACK: 4546163953Srrs /* must be first and only chunk */ 4547169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4548163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4549163953Srrs /* We are not interested anymore */ 4550163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4551163953Srrs ; 4552163953Srrs } else { 4553169378Srrs if (locked_tcb) { 4554163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4555169378Srrs } 4556163953Srrs *offset = length; 4557163953Srrs if (stcb) { 4558172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4559172090Srrs so = SCTP_INP_SO(inp); 4560172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4561172090Srrs SCTP_TCB_UNLOCK(stcb); 4562172090Srrs SCTP_SOCKET_LOCK(so, 1); 4563172090Srrs SCTP_TCB_LOCK(stcb); 4564172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4565172090Srrs#endif 4566171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4567172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4568172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 4569172090Srrs#endif 4570163953Srrs } 4571163953Srrs return (NULL); 4572163953Srrs } 4573163953Srrs } 4574163953Srrs if ((num_chunks > 1) || 4575179783Srrs (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 4576163953Srrs *offset = length; 4577169378Srrs if (locked_tcb) { 4578163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4579169378Srrs } 4580163953Srrs return (NULL); 4581163953Srrs } 4582169420Srrs if ((netp) && (*netp)) { 4583169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4584170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4585169420Srrs } else { 4586169420Srrs ret = -1; 4587169420Srrs } 4588163953Srrs /* 4589163953Srrs * Special case, I must call the output routine to 4590163953Srrs * get the cookie echoed 4591163953Srrs */ 4592168299Srrs if (abort_no_unlock) 4593168299Srrs return (NULL); 4594168299Srrs 4595163953Srrs if ((stcb) && ret == 0) 4596172090Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4597163953Srrs *offset = length; 4598169378Srrs if (locked_tcb) { 4599163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4600169378Srrs } 4601163953Srrs return (NULL); 4602163953Srrs break; 4603163953Srrs case SCTP_SELECTIVE_ACK: 4604169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4605163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 4606163953Srrs { 4607163953Srrs struct sctp_sack_chunk *sack; 4608163953Srrs int abort_now = 0; 4609163953Srrs uint32_t a_rwnd, cum_ack; 4610202526Stuexen uint16_t num_seg, num_dup; 4611202526Stuexen uint8_t flags; 4612202526Stuexen int offset_seg, offset_dup; 4613163953Srrs int nonce_sum_flag; 4614163953Srrs 4615202526Stuexen if (stcb == NULL) { 4616202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4617202526Stuexen break; 4618169208Srrs } 4619202526Stuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4620202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4621202526Stuexen break; 4622202526Stuexen } 4623171990Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4624171990Srrs /*- 4625171990Srrs * If we have sent a shutdown-ack, we will pay no 4626171990Srrs * attention to a sack sent in to us since 4627171990Srrs * we don't care anymore. 4628171990Srrs */ 4629184883Srrs break; 4630171990Srrs } 4631163953Srrs sack = (struct sctp_sack_chunk *)ch; 4632202526Stuexen flags = ch->chunk_flags; 4633202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4634163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 4635163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 4636202526Stuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4637163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4638202526Stuexen if (sizeof(struct sctp_sack_chunk) + 4639202526Stuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4640202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4641202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4642202526Stuexen break; 4643202526Stuexen } 4644202526Stuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4645202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4646170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4647202526Stuexen cum_ack, num_seg, a_rwnd); 4648163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4649163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4650163953Srrs (num_seg == 0) && 4651163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4652163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4653163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4654163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4655163953Srrs ) { 4656163953Srrs /* 4657163953Srrs * We have a SIMPLE sack having no 4658163953Srrs * prior segments and data on sent 4659163953Srrs * queue to be acked.. Use the 4660163953Srrs * faster path sack processing. We 4661163953Srrs * also allow window update sacks 4662163953Srrs * with no missing segments to go 4663163953Srrs * this way too. 4664163953Srrs */ 4665169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4666169208Srrs &abort_now); 4667163953Srrs } else { 4668169420Srrs if (netp && *netp) 4669202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4670202526Stuexen stcb, *netp, 4671202526Stuexen num_seg, 0, num_dup, &abort_now, flags, 4672202526Stuexen cum_ack, a_rwnd); 4673163953Srrs } 4674202526Stuexen if (abort_now) { 4675202526Stuexen /* ABORT signal from sack processing */ 4676202526Stuexen *offset = length; 4677202526Stuexen return (NULL); 4678202526Stuexen } 4679185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4680185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4681185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4682185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4683185694Srrs } 4684163953Srrs } 4685163953Srrs break; 4686185694Srrs /* 4687185694Srrs * EY - nr_sack: If the received chunk is an 4688185694Srrs * nr_sack chunk 4689185694Srrs */ 4690185694Srrs case SCTP_NR_SELECTIVE_ACK: 4691185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4692185694Srrs SCTP_STAT_INCR(sctps_recvsacks); 4693185694Srrs { 4694185694Srrs struct sctp_nr_sack_chunk *nr_sack; 4695185694Srrs int abort_now = 0; 4696185694Srrs uint32_t a_rwnd, cum_ack; 4697202526Stuexen uint16_t num_seg, num_nr_seg, num_dup; 4698202526Stuexen uint8_t flags; 4699202526Stuexen int offset_seg, offset_dup; 4700194355Srrs int nonce_sum_flag; 4701185694Srrs 4702185694Srrs /* 4703185694Srrs * EY nr_sacks have not been negotiated but 4704185694Srrs * the peer end sent an nr_sack, silently 4705185694Srrs * discard the chunk 4706185694Srrs */ 4707202526Stuexen if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && 4708202526Stuexen stcb->asoc.peer_supports_nr_sack)) { 4709185694Srrs goto unknown_chunk; 4710185694Srrs } 4711202526Stuexen if (stcb == NULL) { 4712202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4713202526Stuexen break; 4714202526Stuexen } 4715202526Stuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4716202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4717202526Stuexen break; 4718202526Stuexen } 4719185694Srrs if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4720185694Srrs /*- 4721185694Srrs * If we have sent a shutdown-ack, we will pay no 4722185694Srrs * attention to a sack sent in to us since 4723185694Srrs * we don't care anymore. 4724185694Srrs */ 4725202526Stuexen break; 4726185694Srrs } 4727185694Srrs nr_sack = (struct sctp_nr_sack_chunk *)ch; 4728202526Stuexen flags = ch->chunk_flags; 4729202526Stuexen nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM; 4730185694Srrs 4731185694Srrs cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4732185694Srrs num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4733185694Srrs num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4734202526Stuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4735185694Srrs a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4736202526Stuexen if (sizeof(struct sctp_nr_sack_chunk) + 4737202526Stuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4738202526Stuexen num_dup * sizeof(uint32_t) != chk_length) { 4739202526Stuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4740202526Stuexen break; 4741202526Stuexen } 4742202526Stuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4743202526Stuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4744185694Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4745202526Stuexen cum_ack, num_seg, a_rwnd); 4746185694Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 4747185694Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 4748202526Stuexen (num_seg == 0) && (num_nr_seg == 0) && 4749185694Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 4750185694Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 4751185694Srrs (stcb->asoc.saw_sack_with_frags == 0) && 4752202526Stuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4753185694Srrs /* 4754185694Srrs * We have a SIMPLE sack having no 4755185694Srrs * prior segments and data on sent 4756202526Stuexen * queue to be acked. Use the faster 4757202526Stuexen * path sack processing. We also 4758202526Stuexen * allow window update sacks with no 4759202526Stuexen * missing segments to go this way 4760202526Stuexen * too. 4761185694Srrs */ 4762202526Stuexen sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 4763185694Srrs &abort_now); 4764185694Srrs } else { 4765185694Srrs if (netp && *netp) 4766202526Stuexen sctp_handle_sack(m, offset_seg, offset_dup, 4767202526Stuexen stcb, *netp, 4768202526Stuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4769202526Stuexen cum_ack, a_rwnd); 4770185694Srrs } 4771202526Stuexen if (abort_now) { 4772202526Stuexen /* ABORT signal from sack processing */ 4773202526Stuexen *offset = length; 4774202526Stuexen return (NULL); 4775202526Stuexen } 4776185694Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4777185694Srrs TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4778185694Srrs (stcb->asoc.stream_queue_cnt == 0)) { 4779185694Srrs sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4780185694Srrs } 4781185694Srrs } 4782185694Srrs break; 4783185694Srrs 4784163953Srrs case SCTP_HEARTBEAT_REQUEST: 4785169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4786169420Srrs if ((stcb) && netp && *netp) { 4787169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4788169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 4789169420Srrs chk_length, *netp); 4790169420Srrs 4791169420Srrs /* He's alive so give him credit */ 4792179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4793171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4794171943Srrs stcb->asoc.overall_error_count, 4795171943Srrs 0, 4796171943Srrs SCTP_FROM_SCTP_INPUT, 4797171943Srrs __LINE__); 4798171943Srrs } 4799169420Srrs stcb->asoc.overall_error_count = 0; 4800163953Srrs } 4801163953Srrs break; 4802163953Srrs case SCTP_HEARTBEAT_ACK: 4803169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4804169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4805169208Srrs /* Its not ours */ 4806169352Srrs *offset = length; 4807169378Srrs if (locked_tcb) { 4808169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4809169378Srrs } 4810169208Srrs return (NULL); 4811169208Srrs } 4812163953Srrs /* He's alive so give him credit */ 4813179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4814171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4815171943Srrs stcb->asoc.overall_error_count, 4816171943Srrs 0, 4817171943Srrs SCTP_FROM_SCTP_INPUT, 4818171943Srrs __LINE__); 4819171943Srrs } 4820163953Srrs stcb->asoc.overall_error_count = 0; 4821163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4822169420Srrs if (netp && *netp) 4823169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4824169420Srrs stcb, *netp); 4825163953Srrs break; 4826163953Srrs case SCTP_ABORT_ASSOCIATION: 4827170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4828170091Srrs stcb); 4829169420Srrs if ((stcb) && netp && *netp) 4830169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4831169420Srrs stcb, *netp); 4832163953Srrs *offset = length; 4833163953Srrs return (NULL); 4834163953Srrs break; 4835163953Srrs case SCTP_SHUTDOWN: 4836170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4837170091Srrs stcb); 4838169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 4839169208Srrs *offset = length; 4840169378Srrs if (locked_tcb) { 4841169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4842169378Srrs } 4843169208Srrs return (NULL); 4844169420Srrs } 4845169420Srrs if (netp && *netp) { 4846163953Srrs int abort_flag = 0; 4847163953Srrs 4848163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4849163953Srrs stcb, *netp, &abort_flag); 4850163953Srrs if (abort_flag) { 4851163953Srrs *offset = length; 4852163953Srrs return (NULL); 4853163953Srrs } 4854163953Srrs } 4855163953Srrs break; 4856163953Srrs case SCTP_SHUTDOWN_ACK: 4857170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4858169420Srrs if ((stcb) && (netp) && (*netp)) 4859169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4860163953Srrs *offset = length; 4861163953Srrs return (NULL); 4862163953Srrs break; 4863169420Srrs 4864163953Srrs case SCTP_OPERATION_ERROR: 4865169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4866169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4867169420Srrs 4868163953Srrs *offset = length; 4869163953Srrs return (NULL); 4870163953Srrs } 4871163953Srrs break; 4872163953Srrs case SCTP_COOKIE_ECHO: 4873169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4874170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4875163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4876163953Srrs ; 4877163953Srrs } else { 4878169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4879163953Srrs /* We are not interested anymore */ 4880163953Srrs *offset = length; 4881163953Srrs return (NULL); 4882163953Srrs } 4883163953Srrs } 4884163953Srrs /* 4885163953Srrs * First are we accepting? We do this again here 4886169420Srrs * sincen it is possible that a previous endpoint 4887169420Srrs * WAS listening responded to a INIT-ACK and then 4888163953Srrs * closed. We opened and bound.. and are now no 4889163953Srrs * longer listening. 4890163953Srrs */ 4891172703Srrs 4892172703Srrs if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 4893172703Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4894179783Srrs (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4895163953Srrs struct mbuf *oper; 4896163953Srrs struct sctp_paramhdr *phdr; 4897163953Srrs 4898172703Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4899172703Srrs 0, M_DONTWAIT, 1, MT_DATA); 4900172703Srrs if (oper) { 4901172703Srrs SCTP_BUF_LEN(oper) = 4902172703Srrs sizeof(struct sctp_paramhdr); 4903172703Srrs phdr = mtod(oper, 4904172703Srrs struct sctp_paramhdr *); 4905172703Srrs phdr->param_type = 4906172703Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4907172703Srrs phdr->param_length = 4908172703Srrs htons(sizeof(struct sctp_paramhdr)); 4909163953Srrs } 4910172703Srrs sctp_abort_association(inp, stcb, m, 4911179157Srrs iphlen, sh, oper, vrf_id, port); 4912163953Srrs } 4913172703Srrs *offset = length; 4914172703Srrs return (NULL); 4915172703Srrs } else { 4916163953Srrs struct mbuf *ret_buf; 4917165220Srrs struct sctp_inpcb *linp; 4918163953Srrs 4919169420Srrs if (stcb) { 4920165220Srrs linp = NULL; 4921169420Srrs } else { 4922165220Srrs linp = inp; 4923169420Srrs } 4924165220Srrs 4925169420Srrs if (linp) { 4926165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4927169420Srrs } 4928169420Srrs if (netp) { 4929169420Srrs ret_buf = 4930169420Srrs sctp_handle_cookie_echo(m, iphlen, 4931169420Srrs *offset, sh, 4932169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4933169420Srrs &inp, &stcb, netp, 4934169420Srrs auth_skipped, 4935169420Srrs auth_offset, 4936169420Srrs auth_len, 4937169420Srrs &locked_tcb, 4938179157Srrs vrf_id, 4939179157Srrs port); 4940169420Srrs } else { 4941169420Srrs ret_buf = NULL; 4942169420Srrs } 4943169420Srrs if (linp) { 4944165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4945169420Srrs } 4946163953Srrs if (ret_buf == NULL) { 4947163953Srrs if (locked_tcb) { 4948163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4949163953Srrs } 4950169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4951169420Srrs "GAK, null buffer\n"); 4952163953Srrs auth_skipped = 0; 4953163953Srrs *offset = length; 4954163953Srrs return (NULL); 4955163953Srrs } 4956163953Srrs /* if AUTH skipped, see if it verified... */ 4957163953Srrs if (auth_skipped) { 4958163953Srrs got_auth = 1; 4959163953Srrs auth_skipped = 0; 4960163953Srrs } 4961163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4962163953Srrs /* 4963163953Srrs * Restart the timer if we have 4964163953Srrs * pending data 4965163953Srrs */ 4966163953Srrs struct sctp_tmit_chunk *chk; 4967163953Srrs 4968163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4969163953Srrs if (chk) { 4970163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4971163953Srrs stcb->sctp_ep, stcb, 4972163953Srrs chk->whoTo); 4973163953Srrs } 4974163953Srrs } 4975163953Srrs } 4976163953Srrs break; 4977163953Srrs case SCTP_COOKIE_ACK: 4978170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4979169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4980169378Srrs if (locked_tcb) { 4981169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4982169378Srrs } 4983169352Srrs return (NULL); 4984169352Srrs } 4985163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4986163953Srrs /* We are not interested anymore */ 4987163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4988163953Srrs ; 4989169420Srrs } else if (stcb) { 4990172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4991172090Srrs so = SCTP_INP_SO(inp); 4992172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 4993172090Srrs SCTP_TCB_UNLOCK(stcb); 4994172090Srrs SCTP_SOCKET_LOCK(so, 1); 4995172090Srrs SCTP_TCB_LOCK(stcb); 4996172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 4997172090Srrs#endif 4998171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4999172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5000172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5001172090Srrs#endif 5002163953Srrs *offset = length; 5003163953Srrs return (NULL); 5004163953Srrs } 5005163953Srrs } 5006163953Srrs /* He's alive so give him credit */ 5007169420Srrs if ((stcb) && netp && *netp) { 5008179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5009171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5010171943Srrs stcb->asoc.overall_error_count, 5011171943Srrs 0, 5012171943Srrs SCTP_FROM_SCTP_INPUT, 5013171943Srrs __LINE__); 5014171943Srrs } 5015169378Srrs stcb->asoc.overall_error_count = 0; 5016169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5017169378Srrs } 5018163953Srrs break; 5019163953Srrs case SCTP_ECN_ECHO: 5020169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5021163953Srrs /* He's alive so give him credit */ 5022169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5023169208Srrs /* Its not ours */ 5024169378Srrs if (locked_tcb) { 5025169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5026169378Srrs } 5027169208Srrs *offset = length; 5028169208Srrs return (NULL); 5029169208Srrs } 5030169378Srrs if (stcb) { 5031179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5032171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5033171943Srrs stcb->asoc.overall_error_count, 5034171943Srrs 0, 5035171943Srrs SCTP_FROM_SCTP_INPUT, 5036171943Srrs __LINE__); 5037171943Srrs } 5038169378Srrs stcb->asoc.overall_error_count = 0; 5039169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5040169378Srrs stcb); 5041169378Srrs } 5042163953Srrs break; 5043163953Srrs case SCTP_ECN_CWR: 5044169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5045163953Srrs /* He's alive so give him credit */ 5046169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5047169208Srrs /* Its not ours */ 5048169378Srrs if (locked_tcb) { 5049169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5050169378Srrs } 5051169208Srrs *offset = length; 5052169208Srrs return (NULL); 5053169208Srrs } 5054169378Srrs if (stcb) { 5055179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5056171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5057171943Srrs stcb->asoc.overall_error_count, 5058171943Srrs 0, 5059171943Srrs SCTP_FROM_SCTP_INPUT, 5060171943Srrs __LINE__); 5061171943Srrs } 5062169378Srrs stcb->asoc.overall_error_count = 0; 5063169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 5064169378Srrs } 5065163953Srrs break; 5066163953Srrs case SCTP_SHUTDOWN_COMPLETE: 5067170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5068163953Srrs /* must be first and only chunk */ 5069163953Srrs if ((num_chunks > 1) || 5070170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5071163953Srrs *offset = length; 5072169378Srrs if (locked_tcb) { 5073163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5074169378Srrs } 5075163953Srrs return (NULL); 5076163953Srrs } 5077169420Srrs if ((stcb) && netp && *netp) { 5078169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5079169378Srrs stcb, *netp); 5080169378Srrs } 5081163953Srrs *offset = length; 5082163953Srrs return (NULL); 5083163953Srrs break; 5084163953Srrs case SCTP_ASCONF: 5085169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5086163953Srrs /* He's alive so give him credit */ 5087169378Srrs if (stcb) { 5088179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5089171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5090171943Srrs stcb->asoc.overall_error_count, 5091171943Srrs 0, 5092171943Srrs SCTP_FROM_SCTP_INPUT, 5093171943Srrs __LINE__); 5094171943Srrs } 5095169378Srrs stcb->asoc.overall_error_count = 0; 5096169378Srrs sctp_handle_asconf(m, *offset, 5097171990Srrs (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5098171990Srrs asconf_cnt++; 5099169378Srrs } 5100163953Srrs break; 5101163953Srrs case SCTP_ASCONF_ACK: 5102169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5103169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5104169208Srrs /* Its not ours */ 5105169378Srrs if (locked_tcb) { 5106169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5107169378Srrs } 5108169208Srrs *offset = length; 5109169208Srrs return (NULL); 5110169208Srrs } 5111169420Srrs if ((stcb) && netp && *netp) { 5112169378Srrs /* He's alive so give him credit */ 5113179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5114171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5115171943Srrs stcb->asoc.overall_error_count, 5116171943Srrs 0, 5117171943Srrs SCTP_FROM_SCTP_INPUT, 5118171943Srrs __LINE__); 5119171943Srrs } 5120169378Srrs stcb->asoc.overall_error_count = 0; 5121169378Srrs sctp_handle_asconf_ack(m, *offset, 5122172190Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5123172190Srrs if (abort_no_unlock) 5124172190Srrs return (NULL); 5125169378Srrs } 5126163953Srrs break; 5127163953Srrs case SCTP_FORWARD_CUM_TSN: 5128169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5129169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5130169208Srrs /* Its not ours */ 5131169378Srrs if (locked_tcb) { 5132169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5133169378Srrs } 5134169208Srrs *offset = length; 5135169208Srrs return (NULL); 5136169208Srrs } 5137163953Srrs /* He's alive so give him credit */ 5138169378Srrs if (stcb) { 5139163953Srrs int abort_flag = 0; 5140163953Srrs 5141163953Srrs stcb->asoc.overall_error_count = 0; 5142179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5143171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5144171943Srrs stcb->asoc.overall_error_count, 5145171943Srrs 0, 5146171943Srrs SCTP_FROM_SCTP_INPUT, 5147171943Srrs __LINE__); 5148171943Srrs } 5149163953Srrs *fwd_tsn_seen = 1; 5150163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5151163953Srrs /* We are not interested anymore */ 5152172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5153172090Srrs so = SCTP_INP_SO(inp); 5154172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5155172090Srrs SCTP_TCB_UNLOCK(stcb); 5156172090Srrs SCTP_SOCKET_LOCK(so, 1); 5157172090Srrs SCTP_TCB_LOCK(stcb); 5158172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5159172090Srrs#endif 5160171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5161172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5162172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5163172090Srrs#endif 5164163953Srrs *offset = length; 5165163953Srrs return (NULL); 5166163953Srrs } 5167163953Srrs sctp_handle_forward_tsn(stcb, 5168170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5169163953Srrs if (abort_flag) { 5170163953Srrs *offset = length; 5171163953Srrs return (NULL); 5172163953Srrs } else { 5173179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5174171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5175171943Srrs stcb->asoc.overall_error_count, 5176171943Srrs 0, 5177171943Srrs SCTP_FROM_SCTP_INPUT, 5178171943Srrs __LINE__); 5179171943Srrs } 5180163953Srrs stcb->asoc.overall_error_count = 0; 5181163953Srrs } 5182163953Srrs 5183163953Srrs } 5184163953Srrs break; 5185163953Srrs case SCTP_STREAM_RESET: 5186169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5187169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5188169208Srrs /* Its not ours */ 5189169378Srrs if (locked_tcb) { 5190169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5191169378Srrs } 5192169208Srrs *offset = length; 5193169208Srrs return (NULL); 5194169208Srrs } 5195163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5196163953Srrs /* We are not interested anymore */ 5197172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5198172090Srrs so = SCTP_INP_SO(inp); 5199172090Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 5200172090Srrs SCTP_TCB_UNLOCK(stcb); 5201172090Srrs SCTP_SOCKET_LOCK(so, 1); 5202172090Srrs SCTP_TCB_LOCK(stcb); 5203172090Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 5204172090Srrs#endif 5205171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5206172090Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5207172090Srrs SCTP_SOCKET_UNLOCK(so, 1); 5208172090Srrs#endif 5209163953Srrs *offset = length; 5210163953Srrs return (NULL); 5211163953Srrs } 5212169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 5213169420Srrs /* 5214169420Srrs * hmm, peer should have announced this, but 5215169420Srrs * we will turn it on since he is sending us 5216169420Srrs * a stream reset. 5217169420Srrs */ 5218169420Srrs stcb->asoc.peer_supports_strreset = 1; 5219163953Srrs } 5220170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5221169420Srrs /* stop processing */ 5222169420Srrs *offset = length; 5223169420Srrs return (NULL); 5224169420Srrs } 5225163953Srrs break; 5226163953Srrs case SCTP_PACKET_DROPPED: 5227169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5228163953Srrs /* re-get it all please */ 5229169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5230169208Srrs /* Its not ours */ 5231169378Srrs if (locked_tcb) { 5232169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5233169378Srrs } 5234169208Srrs *offset = length; 5235169208Srrs return (NULL); 5236169208Srrs } 5237169420Srrs if (ch && (stcb) && netp && (*netp)) { 5238169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5239170781Srrs stcb, *netp, 5240170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 5241170781Srrs 5242169378Srrs } 5243163953Srrs break; 5244163953Srrs 5245163953Srrs case SCTP_AUTHENTICATION: 5246169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5247179783Srrs if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5248163953Srrs goto unknown_chunk; 5249163953Srrs 5250163953Srrs if (stcb == NULL) { 5251163953Srrs /* save the first AUTH for later processing */ 5252163953Srrs if (auth_skipped == 0) { 5253163953Srrs auth_offset = *offset; 5254163953Srrs auth_len = chk_length; 5255163953Srrs auth_skipped = 1; 5256163953Srrs } 5257163953Srrs /* skip this chunk (temporarily) */ 5258163953Srrs goto next_chunk; 5259163953Srrs } 5260169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5261169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 5262169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 5263169208Srrs /* Its not ours */ 5264169378Srrs if (locked_tcb) { 5265169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 5266169378Srrs } 5267169208Srrs *offset = length; 5268169208Srrs return (NULL); 5269169208Srrs } 5270163953Srrs if (got_auth == 1) { 5271163953Srrs /* skip this chunk... it's already auth'd */ 5272163953Srrs goto next_chunk; 5273163953Srrs } 5274163953Srrs got_auth = 1; 5275169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5276163953Srrs m, *offset)) { 5277163953Srrs /* auth HMAC failed so dump the packet */ 5278163953Srrs *offset = length; 5279163953Srrs return (stcb); 5280163953Srrs } else { 5281163953Srrs /* remaining chunks are HMAC checked */ 5282163953Srrs stcb->asoc.authenticated = 1; 5283163953Srrs } 5284163953Srrs break; 5285163953Srrs 5286163953Srrs default: 5287163953Srrs unknown_chunk: 5288163953Srrs /* it's an unknown chunk! */ 5289163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5290163953Srrs struct mbuf *mm; 5291163953Srrs struct sctp_paramhdr *phd; 5292163953Srrs 5293163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5294165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 5295163953Srrs if (mm) { 5296163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 5297163953Srrs /* 5298163953Srrs * We cheat and use param type since 5299163953Srrs * we did not bother to define a 5300163953Srrs * error cause struct. They are the 5301163953Srrs * same basic format with different 5302163953Srrs * names. 5303163953Srrs */ 5304163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5305163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 5306165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 5307166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 5308163953Srrs M_DONTWAIT); 5309165647Srrs if (SCTP_BUF_NEXT(mm)) { 5310178202Srrs#ifdef SCTP_MBUF_LOGGING 5311179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5312178202Srrs struct mbuf *mat; 5313178202Srrs 5314178202Srrs mat = SCTP_BUF_NEXT(mm); 5315178202Srrs while (mat) { 5316178202Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5317178202Srrs sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5318178202Srrs } 5319178202Srrs mat = SCTP_BUF_NEXT(mat); 5320178202Srrs } 5321178202Srrs } 5322178202Srrs#endif 5323163953Srrs sctp_queue_op_err(stcb, mm); 5324163953Srrs } else { 5325163953Srrs sctp_m_freem(mm); 5326163953Srrs } 5327163953Srrs } 5328163953Srrs } 5329163953Srrs if ((ch->chunk_type & 0x80) == 0) { 5330163953Srrs /* discard this packet */ 5331163953Srrs *offset = length; 5332163953Srrs return (stcb); 5333163953Srrs } /* else skip this bad chunk and continue... */ 5334163953Srrs break; 5335163953Srrs } /* switch (ch->chunk_type) */ 5336163953Srrs 5337163953Srrs 5338163953Srrsnext_chunk: 5339163953Srrs /* get the next chunk */ 5340163953Srrs *offset += SCTP_SIZE32(chk_length); 5341163953Srrs if (*offset >= length) { 5342163953Srrs /* no more data left in the mbuf chain */ 5343163953Srrs break; 5344163953Srrs } 5345163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5346163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 5347163953Srrs if (ch == NULL) { 5348169378Srrs if (locked_tcb) { 5349163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 5350169378Srrs } 5351163953Srrs *offset = length; 5352163953Srrs return (NULL); 5353163953Srrs } 5354163953Srrs } /* while */ 5355171990Srrs 5356171990Srrs if (asconf_cnt > 0 && stcb != NULL) { 5357171990Srrs sctp_send_asconf_ack(stcb); 5358171990Srrs } 5359163953Srrs return (stcb); 5360163953Srrs} 5361163953Srrs 5362163953Srrs 5363163953Srrs/* 5364163953Srrs * Process the ECN bits we have something set so we must look to see if it is 5365163953Srrs * ECN(0) or ECN(1) or CE 5366163953Srrs */ 5367170806Srrsstatic void 5368163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 5369163953Srrs uint8_t ecn_bits) 5370163953Srrs{ 5371163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5372163953Srrs ; 5373163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 5374163953Srrs /* 5375163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 5376163953Srrs * change the NS bit (that we have yet to find a way to send 5377163953Srrs * it yet). 5378163953Srrs */ 5379163953Srrs 5380163953Srrs /* ECN Nonce stuff */ 5381163953Srrs stcb->asoc.receiver_nonce_sum++; 5382163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 5383163953Srrs 5384163953Srrs /* 5385163953Srrs * Drag up the last_echo point if cumack is larger since we 5386163953Srrs * don't want the point falling way behind by more than 5387163953Srrs * 2^^31 and then having it be incorrect. 5388163953Srrs */ 5389163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5390163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5391163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5392163953Srrs } 5393163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 5394163953Srrs /* 5395163953Srrs * Drag up the last_echo point if cumack is larger since we 5396163953Srrs * don't want the point falling way behind by more than 5397163953Srrs * 2^^31 and then having it be incorrect. 5398163953Srrs */ 5399163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 5400163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 5401163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 5402163953Srrs } 5403163953Srrs } 5404163953Srrs} 5405163953Srrs 5406170806Srrsstatic void 5407163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 5408163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 5409163953Srrs{ 5410163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 5411163953Srrs /* 5412163953Srrs * we possibly must notify the sender that a congestion 5413163953Srrs * window reduction is in order. We do this by adding a ECNE 5414163953Srrs * chunk to the output chunk queue. The incoming CWR will 5415163953Srrs * remove this chunk. 5416163953Srrs */ 5417163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 5418163953Srrs MAX_TSN)) { 5419163953Srrs /* Yep, we need to add a ECNE */ 5420163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 5421163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 5422163953Srrs } 5423163953Srrs } 5424163953Srrs} 5425163953Srrs 5426171440Srrs#ifdef INVARIANTS 5427171440Srrsstatic void 5428171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 5429171440Srrs{ 5430171440Srrs struct sctp_tcb *stcb; 5431171440Srrs 5432171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5433171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 5434171440Srrs panic("Own lock on stcb at return from input"); 5435171440Srrs } 5436171440Srrs } 5437171440Srrs} 5438171440Srrs 5439171440Srrs#endif 5440171440Srrs 5441163953Srrs/* 5442163953Srrs * common input chunk processing (v4 and v6) 5443163953Srrs */ 5444169378Srrsvoid 5445163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5446163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5447163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5448179157Srrs uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5449163953Srrs{ 5450163953Srrs /* 5451163953Srrs * Control chunk processing 5452163953Srrs */ 5453163953Srrs uint32_t high_tsn; 5454163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 5455163953Srrs struct mbuf *m = *mm; 5456163953Srrs int abort_flag = 0; 5457163953Srrs int un_sent; 5458163953Srrs 5459163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 5460163953Srrs#ifdef SCTP_AUDITING_ENABLED 5461163953Srrs sctp_audit_log(0xE0, 1); 5462163953Srrs sctp_auditing(0, inp, stcb, net); 5463163953Srrs#endif 5464163953Srrs 5465179783Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5466179783Srrs m, iphlen, offset, length, stcb); 5467163953Srrs if (stcb) { 5468163953Srrs /* always clear this before beginning a packet */ 5469163953Srrs stcb->asoc.authenticated = 0; 5470163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 5471171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5472171990Srrs stcb, stcb->asoc.state); 5473171990Srrs 5474171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5475171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5476171745Srrs /*- 5477171745Srrs * If we hit here, we had a ref count 5478171745Srrs * up when the assoc was aborted and the 5479171745Srrs * timer is clearing out the assoc, we should 5480171745Srrs * NOT respond to any packet.. its OOTB. 5481171745Srrs */ 5482171745Srrs SCTP_TCB_UNLOCK(stcb); 5483171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5484179157Srrs vrf_id, port); 5485171745Srrs goto out_now; 5486171745Srrs } 5487163953Srrs } 5488163953Srrs if (IS_SCTP_CONTROL(ch)) { 5489163953Srrs /* process the control portion of the SCTP packet */ 5490169655Srrs /* sa_ignore NO_NULL_CHK */ 5491163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5492179157Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5493163953Srrs if (stcb) { 5494163953Srrs /* 5495163953Srrs * This covers us if the cookie-echo was there and 5496163953Srrs * it changes our INP. 5497163953Srrs */ 5498163953Srrs inp = stcb->sctp_ep; 5499179783Srrs if ((net) && (port)) { 5500179783Srrs if (net->port == 0) { 5501179783Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5502179783Srrs } 5503179783Srrs net->port = port; 5504179783Srrs } 5505163953Srrs } 5506163953Srrs } else { 5507163953Srrs /* 5508163953Srrs * no control chunks, so pre-process DATA chunks (these 5509163953Srrs * checks are taken care of by control processing) 5510163953Srrs */ 5511163953Srrs 5512163953Srrs /* 5513163953Srrs * if DATA only packet, and auth is required, then punt... 5514163953Srrs * can't have authenticated without any AUTH (control) 5515163953Srrs * chunks 5516163953Srrs */ 5517179783Srrs if ((stcb != NULL) && 5518179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5519179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5520163953Srrs /* "silently" ignore */ 5521163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5522163953Srrs SCTP_TCB_UNLOCK(stcb); 5523171440Srrs goto out_now; 5524163953Srrs } 5525163953Srrs if (stcb == NULL) { 5526163953Srrs /* out of the blue DATA chunk */ 5527169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5528179157Srrs vrf_id, port); 5529171440Srrs goto out_now; 5530163953Srrs } 5531163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5532163953Srrs /* v_tag mismatch! */ 5533163953Srrs SCTP_STAT_INCR(sctps_badvtag); 5534163953Srrs SCTP_TCB_UNLOCK(stcb); 5535171440Srrs goto out_now; 5536163953Srrs } 5537163953Srrs } 5538163953Srrs 5539163953Srrs if (stcb == NULL) { 5540163953Srrs /* 5541163953Srrs * no valid TCB for this packet, or we found it's a bad 5542163953Srrs * packet while processing control, or we're done with this 5543163953Srrs * packet (done or skip rest of data), so we drop it... 5544163953Srrs */ 5545171440Srrs goto out_now; 5546163953Srrs } 5547163953Srrs /* 5548163953Srrs * DATA chunk processing 5549163953Srrs */ 5550163953Srrs /* plow through the data chunks while length > offset */ 5551163953Srrs 5552163953Srrs /* 5553163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 5554163953Srrs * DATA is required. 5555163953Srrs */ 5556179783Srrs if ((length > offset) && 5557179783Srrs (stcb != NULL) && 5558179783Srrs !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5559179783Srrs sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5560163953Srrs !stcb->asoc.authenticated) { 5561163953Srrs /* "silently" ignore */ 5562163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 5563169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 5564169420Srrs "Data chunk requires AUTH, skipped\n"); 5565165220Srrs goto trigger_send; 5566163953Srrs } 5567163953Srrs if (length > offset) { 5568163953Srrs int retval; 5569163953Srrs 5570163953Srrs /* 5571163953Srrs * First check to make sure our state is correct. We would 5572163953Srrs * not get here unless we really did have a tag, so we don't 5573163953Srrs * abort if this happens, just dump the chunk silently. 5574163953Srrs */ 5575163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 5576163953Srrs case SCTP_STATE_COOKIE_ECHOED: 5577163953Srrs /* 5578163953Srrs * we consider data with valid tags in this state 5579163953Srrs * shows us the cookie-ack was lost. Imply it was 5580163953Srrs * there. 5581163953Srrs */ 5582179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5583171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5584171943Srrs stcb->asoc.overall_error_count, 5585171943Srrs 0, 5586171943Srrs SCTP_FROM_SCTP_INPUT, 5587171943Srrs __LINE__); 5588171943Srrs } 5589163953Srrs stcb->asoc.overall_error_count = 0; 5590163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5591163953Srrs break; 5592163953Srrs case SCTP_STATE_COOKIE_WAIT: 5593163953Srrs /* 5594163953Srrs * We consider OOTB any data sent during asoc setup. 5595163953Srrs */ 5596169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5597179157Srrs vrf_id, port); 5598163953Srrs SCTP_TCB_UNLOCK(stcb); 5599171440Srrs goto out_now; 5600171531Srrs /* sa_ignore NOTREACHED */ 5601163953Srrs break; 5602163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 5603163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 5604163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5605163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 5606163953Srrs default: 5607163953Srrs SCTP_TCB_UNLOCK(stcb); 5608171440Srrs goto out_now; 5609171531Srrs /* sa_ignore NOTREACHED */ 5610163953Srrs break; 5611163953Srrs case SCTP_STATE_OPEN: 5612163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 5613163953Srrs break; 5614163953Srrs } 5615163953Srrs /* take care of ECN, part 1. */ 5616163953Srrs if (stcb->asoc.ecn_allowed && 5617163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5618163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 5619163953Srrs } 5620163953Srrs /* plow through the data chunks while length > offset */ 5621163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5622163953Srrs inp, stcb, net, &high_tsn); 5623163953Srrs if (retval == 2) { 5624163953Srrs /* 5625163953Srrs * The association aborted, NO UNLOCK needed since 5626163953Srrs * the association is destroyed. 5627163953Srrs */ 5628171440Srrs goto out_now; 5629163953Srrs } 5630163953Srrs data_processed = 1; 5631163953Srrs if (retval == 0) { 5632163953Srrs /* take care of ecn part 2. */ 5633163953Srrs if (stcb->asoc.ecn_allowed && 5634163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 5635163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 5636163953Srrs ecn_bits); 5637163953Srrs } 5638163953Srrs } 5639163953Srrs /* 5640163953Srrs * Anything important needs to have been m_copy'ed in 5641163953Srrs * process_data 5642163953Srrs */ 5643163953Srrs } 5644163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 5645163953Srrs int was_a_gap = 0; 5646163953Srrs 5647163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 5648163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 5649163953Srrs /* there was a gap before this data was processed */ 5650163953Srrs was_a_gap = 1; 5651163953Srrs } 5652190689Srrs stcb->asoc.send_sack = 1; 5653163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 5654163953Srrs if (abort_flag) { 5655163953Srrs /* Again, we aborted so NO UNLOCK needed */ 5656171440Srrs goto out_now; 5657163953Srrs } 5658190689Srrs } else if (fwd_tsn_seen) { 5659190689Srrs stcb->asoc.send_sack = 1; 5660163953Srrs } 5661163953Srrs /* trigger send of any chunks in queue... */ 5662165220Srrstrigger_send: 5663163953Srrs#ifdef SCTP_AUDITING_ENABLED 5664163953Srrs sctp_audit_log(0xE0, 2); 5665163953Srrs sctp_auditing(1, inp, stcb, net); 5666163953Srrs#endif 5667169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5668169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 5669169420Srrs stcb->asoc.peers_rwnd, 5670169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5671169420Srrs stcb->asoc.total_flight); 5672163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5673163953Srrs 5674163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 5675163953Srrs ((un_sent) && 5676163953Srrs (stcb->asoc.peers_rwnd > 0 || 5677163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5678169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5679172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5680169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5681163953Srrs } 5682163953Srrs#ifdef SCTP_AUDITING_ENABLED 5683163953Srrs sctp_audit_log(0xE0, 3); 5684163953Srrs sctp_auditing(2, inp, stcb, net); 5685163953Srrs#endif 5686163953Srrs SCTP_TCB_UNLOCK(stcb); 5687171440Srrsout_now: 5688171440Srrs#ifdef INVARIANTS 5689171440Srrs sctp_validate_no_locks(inp); 5690171440Srrs#endif 5691169378Srrs return; 5692163953Srrs} 5693163953Srrs 5694185694Srrs#if 0 5695185694Srrsstatic void 5696185694Srrssctp_print_mbuf_chain(struct mbuf *m) 5697185694Srrs{ 5698185694Srrs for (; m; m = SCTP_BUF_NEXT(m)) { 5699185694Srrs printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5700185694Srrs if (SCTP_BUF_IS_EXTENDED(m)) 5701185694Srrs printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5702185694Srrs } 5703185694Srrs} 5704163953Srrs 5705185694Srrs#endif 5706185694Srrs 5707163953Srrsvoid 5708189004Srdivackysctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5709163953Srrs{ 5710163953Srrs#ifdef SCTP_MBUF_LOGGING 5711163953Srrs struct mbuf *mat; 5712163953Srrs 5713163953Srrs#endif 5714165647Srrs struct mbuf *m; 5715163953Srrs int iphlen; 5716170181Srrs uint32_t vrf_id = 0; 5717163953Srrs uint8_t ecn_bits; 5718163953Srrs struct ip *ip; 5719163953Srrs struct sctphdr *sh; 5720163953Srrs struct sctp_inpcb *inp = NULL; 5721163953Srrs 5722163953Srrs uint32_t check, calc_check; 5723163953Srrs struct sctp_nets *net; 5724163953Srrs struct sctp_tcb *stcb = NULL; 5725163953Srrs struct sctp_chunkhdr *ch; 5726163953Srrs int refcount_up = 0; 5727163953Srrs int length, mlen, offset; 5728163953Srrs 5729169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5730169352Srrs SCTP_RELEASE_PKT(i_pak); 5731169352Srrs return; 5732169352Srrs } 5733168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 5734163953Srrs iphlen = off; 5735165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 5736170056Srrs 5737163953Srrs net = NULL; 5738163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 5739163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5740163953Srrs 5741170091Srrs 5742163953Srrs#ifdef SCTP_MBUF_LOGGING 5743163953Srrs /* Log in any input mbufs */ 5744179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5745170744Srrs mat = m; 5746170744Srrs while (mat) { 5747170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 5748170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 5749170744Srrs } 5750170744Srrs mat = SCTP_BUF_NEXT(mat); 5751163953Srrs } 5752163953Srrs } 5753163953Srrs#endif 5754170091Srrs#ifdef SCTP_PACKET_LOGGING 5755179783Srrs if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5756170921Srrs sctp_packet_log(m, mlen); 5757170091Srrs#endif 5758170091Srrs /* 5759170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 5760170091Srrs * case) 5761170091Srrs */ 5762170091Srrs mlen -= iphlen; 5763168124Srrs 5764163953Srrs /* 5765163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 5766163953Srrs */ 5767163953Srrs ip = mtod(m, struct ip *); 5768163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 5769165647Srrs if (SCTP_BUF_LEN(m) < offset) { 5770163953Srrs if ((m = m_pullup(m, offset)) == 0) { 5771163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5772163953Srrs return; 5773163953Srrs } 5774163953Srrs ip = mtod(m, struct ip *); 5775163953Srrs } 5776188067Srrs /* validate mbuf chain length with IP payload length */ 5777188067Srrs if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5778188067Srrs SCTP_STAT_INCR(sctps_hdrops); 5779188067Srrs goto bad; 5780188067Srrs } 5781163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5782163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5783170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5784170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5785163953Srrs 5786163953Srrs /* SCTP does not allow broadcasts or multicasts */ 5787163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5788163953Srrs goto bad; 5789163953Srrs } 5790168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5791163953Srrs /* 5792163953Srrs * We only look at broadcast if its a front state, All 5793163953Srrs * others we will not have a tcb for anyway. 5794163953Srrs */ 5795163953Srrs goto bad; 5796163953Srrs } 5797163953Srrs /* validate SCTP checksum */ 5798188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5799188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5800188067Srrs m->m_pkthdr.len, 5801188067Srrs if_name(m->m_pkthdr.rcvif), 5802188067Srrs m->m_pkthdr.csum_flags); 5803188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5804188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 5805188067Srrs goto sctp_skip_csum_4; 5806188067Srrs } 5807171990Srrs check = sh->checksum; /* save incoming checksum */ 5808179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 5809171990Srrs ((ip->ip_src.s_addr == ip->ip_dst.s_addr) || 5810171990Srrs (SCTP_IS_IT_LOOPBACK(m))) 5811171990Srrs ) { 5812188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 5813171990Srrs goto sctp_skip_csum_4; 5814171990Srrs } 5815171990Srrs sh->checksum = 0; /* prepare for calc */ 5816188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 5817203503Stuexen sh->checksum = check; 5818188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 5819171990Srrs if (calc_check != check) { 5820171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5821171990Srrs calc_check, check, m, mlen, iphlen); 5822163953Srrs 5823171990Srrs stcb = sctp_findassociation_addr(m, iphlen, 5824171990Srrs offset - sizeof(*ch), 5825171990Srrs sh, ch, &inp, &net, 5826171990Srrs vrf_id); 5827179157Srrs if ((net) && (port)) { 5828179157Srrs if (net->port == 0) { 5829179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5830179157Srrs } 5831179157Srrs net->port = port; 5832179157Srrs } 5833171990Srrs if ((inp) && (stcb)) { 5834171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5835172090Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5836171990Srrs } else if ((inp != NULL) && (stcb == NULL)) { 5837171990Srrs refcount_up = 1; 5838163953Srrs } 5839171990Srrs SCTP_STAT_INCR(sctps_badsum); 5840171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5841171990Srrs goto bad; 5842168299Srrs } 5843163953Srrssctp_skip_csum_4: 5844168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 5845168124Srrs if (sh->dest_port == 0) { 5846168124Srrs SCTP_STAT_INCR(sctps_hdrops); 5847168124Srrs goto bad; 5848168124Srrs } 5849163953Srrs /* 5850163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5851163953Srrs * IP/SCTP/first chunk header... 5852163953Srrs */ 5853163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 5854168299Srrs sh, ch, &inp, &net, vrf_id); 5855179157Srrs if ((net) && (port)) { 5856179157Srrs if (net->port == 0) { 5857179157Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr)); 5858179157Srrs } 5859179157Srrs net->port = port; 5860179157Srrs } 5861163953Srrs /* inp's ref-count increased && stcb locked */ 5862163953Srrs if (inp == NULL) { 5863163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 5864163953Srrs 5865163953Srrs SCTP_STAT_INCR(sctps_noport); 5866163953Srrs#ifdef ICMP_BANDLIM 5867163953Srrs /* 5868163953Srrs * we use the bandwidth limiting to protect against sending 5869163953Srrs * too many ABORTS all at once. In this case these count the 5870163953Srrs * same as an ICMP message. 5871163953Srrs */ 5872163953Srrs if (badport_bandlim(0) < 0) 5873163953Srrs goto bad; 5874163953Srrs#endif /* ICMP_BANDLIM */ 5875169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 5876169420Srrs "Sending a ABORT from packet entry!\n"); 5877163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 5878163953Srrs /* 5879163953Srrs * we do a trick here to get the INIT tag, dig in 5880163953Srrs * and get the tag from the INIT and put it in the 5881163953Srrs * common header. 5882163953Srrs */ 5883163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5884163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 5885163953Srrs (uint8_t *) & chunk_buf); 5886163953Srrs if (init_chk != NULL) 5887163953Srrs sh->v_tag = init_chk->init.initiate_tag; 5888163953Srrs } 5889165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5890179157Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 5891165220Srrs goto bad; 5892165220Srrs } 5893165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5894165220Srrs goto bad; 5895165220Srrs } 5896165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 5897179157Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 5898163953Srrs goto bad; 5899163953Srrs } else if (stcb == NULL) { 5900163953Srrs refcount_up = 1; 5901163953Srrs } 5902171167Sgnn#ifdef IPSEC 5903163953Srrs /* 5904163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5905163953Srrs * idea, so I will leave it in place. 5906163953Srrs */ 5907163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5908197288Srrs MODULE_GLOBAL(ipsec4stat).in_polvio++; 5909163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5910163953Srrs goto bad; 5911163953Srrs } 5912163953Srrs#endif /* IPSEC */ 5913163953Srrs 5914163953Srrs /* 5915163953Srrs * common chunk processing 5916163953Srrs */ 5917163953Srrs length = ip->ip_len + iphlen; 5918163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5919163953Srrs 5920163953Srrs ecn_bits = ip->ip_tos; 5921163953Srrs 5922169655Srrs /* sa_ignore NO_NULL_CHK */ 5923163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5924179157Srrs inp, stcb, net, ecn_bits, vrf_id, port); 5925163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5926163953Srrs if (m) { 5927163953Srrs sctp_m_freem(m); 5928163953Srrs } 5929163953Srrs if ((inp) && (refcount_up)) { 5930163953Srrs /* reduce ref-count */ 5931163953Srrs SCTP_INP_DECR_REF(inp); 5932163953Srrs } 5933163953Srrs return; 5934163953Srrsbad: 5935169420Srrs if (stcb) { 5936163953Srrs SCTP_TCB_UNLOCK(stcb); 5937169420Srrs } 5938163953Srrs if ((inp) && (refcount_up)) { 5939163953Srrs /* reduce ref-count */ 5940163953Srrs SCTP_INP_DECR_REF(inp); 5941163953Srrs } 5942163953Srrs if (m) { 5943163953Srrs sctp_m_freem(m); 5944163953Srrs } 5945163953Srrs return; 5946163953Srrs} 5947179157Srrsvoid 5948179157Srrssctp_input(i_pak, off) 5949179157Srrs struct mbuf *i_pak; 5950179157Srrs int off; 5951179157Srrs{ 5952179157Srrs sctp_input_with_port(i_pak, off, 0); 5953179157Srrs} 5954