sctp_input.c revision 171943
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, 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 171943 2007-08-24 00:53:53Z rrs $"); 35163953Srrs 36163953Srrs#include <netinet/sctp_os.h> 37163953Srrs#include <netinet/sctp_var.h> 38167598Srrs#include <netinet/sctp_sysctl.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctputil.h> 42163953Srrs#include <netinet/sctp_output.h> 43163953Srrs#include <netinet/sctp_input.h> 44163953Srrs#include <netinet/sctp_auth.h> 45163953Srrs#include <netinet/sctp_indata.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47170091Srrs#include <netinet/sctp_bsd_addr.h> 48163953Srrs 49163953Srrs 50163953Srrs 51163953Srrsstatic void 52163953Srrssctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 53163953Srrs{ 54163953Srrs struct sctp_nets *net; 55163953Srrs 56165220Srrs /* 57165220Srrs * This now not only stops all cookie timers it also stops any INIT 58165220Srrs * timers as well. This will make sure that the timers are stopped 59165220Srrs * in all collision cases. 60165220Srrs */ 61163953Srrs SCTP_TCB_LOCK_ASSERT(stcb); 62163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 63165220Srrs if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 64163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 65163953Srrs stcb->sctp_ep, 66163953Srrs stcb, 67165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 68165220Srrs } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 69165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 70165220Srrs stcb->sctp_ep, 71165220Srrs stcb, 72165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 73163953Srrs } 74163953Srrs } 75163953Srrs} 76163953Srrs 77163953Srrs/* INIT handler */ 78163953Srrsstatic void 79163953Srrssctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 80163953Srrs struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 81170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 82163953Srrs{ 83163953Srrs struct sctp_init *init; 84163953Srrs struct mbuf *op_err; 85163953Srrs uint32_t init_limit; 86163953Srrs 87169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 88169420Srrs stcb); 89163953Srrs op_err = NULL; 90163953Srrs init = &cp->init; 91163953Srrs /* First are we accepting? */ 92163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 93169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 94169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 95169420Srrs inp->sctp_socket->so_qlimit); 96163953Srrs /* 97163953Srrs * FIX ME ?? What about TCP model and we have a 98163953Srrs * match/restart case? 99163953Srrs */ 100169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 101170181Srrs vrf_id); 102168299Srrs if (stcb) 103168299Srrs *abort_no_unlock = 1; 104163953Srrs return; 105163953Srrs } 106163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 107163953Srrs /* Invalid length */ 108163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 109169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 110170181Srrs vrf_id); 111168299Srrs if (stcb) 112168299Srrs *abort_no_unlock = 1; 113163953Srrs return; 114163953Srrs } 115163953Srrs /* validate parameters */ 116163953Srrs if (init->initiate_tag == 0) { 117163953Srrs /* protocol error... send abort */ 118163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 119169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 120170181Srrs vrf_id); 121168299Srrs if (stcb) 122168299Srrs *abort_no_unlock = 1; 123163953Srrs return; 124163953Srrs } 125163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 126163953Srrs /* invalid parameter... send abort */ 127163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 128169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 129170181Srrs vrf_id); 130171440Srrs if (stcb) 131171440Srrs *abort_no_unlock = 1; 132163953Srrs return; 133163953Srrs } 134163953Srrs if (init->num_inbound_streams == 0) { 135163953Srrs /* protocol error... send abort */ 136163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 137169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 138170181Srrs vrf_id); 139168299Srrs if (stcb) 140168299Srrs *abort_no_unlock = 1; 141163953Srrs return; 142163953Srrs } 143163953Srrs if (init->num_outbound_streams == 0) { 144163953Srrs /* protocol error... send abort */ 145163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 146169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 147170181Srrs vrf_id); 148168299Srrs if (stcb) 149168299Srrs *abort_no_unlock = 1; 150163953Srrs return; 151163953Srrs } 152163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 153163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 154163953Srrs init_limit)) { 155163953Srrs /* auth parameter(s) error... send abort */ 156170181Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id); 157168299Srrs if (stcb) 158168299Srrs *abort_no_unlock = 1; 159163953Srrs return; 160163953Srrs } 161163953Srrs /* send an INIT-ACK w/cookie */ 162169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 163170181Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id); 164163953Srrs} 165163953Srrs 166163953Srrs/* 167163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 168163953Srrs */ 169171158Srrs 170171158Srrsint 171171158Srrssctp_is_there_unsent_data(struct sctp_tcb *stcb) 172171158Srrs{ 173171158Srrs int unsent_data = 0; 174171158Srrs struct sctp_stream_queue_pending *sp; 175171158Srrs struct sctp_stream_out *strq; 176171158Srrs struct sctp_association *asoc; 177171158Srrs 178171158Srrs /* 179171158Srrs * This function returns the number of streams that have true unsent 180171158Srrs * data on them. Note that as it looks through it will clean up any 181171158Srrs * places that have old data that has been sent but left at top of 182171158Srrs * stream queue. 183171158Srrs */ 184171158Srrs asoc = &stcb->asoc; 185171158Srrs SCTP_TCB_SEND_LOCK(stcb); 186171158Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 187171158Srrs /* Check to see if some data queued */ 188171158Srrs TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) { 189171531Srrs is_there_another: 190171158Srrs /* sa_ignore FREED_MEMORY */ 191171158Srrs sp = TAILQ_FIRST(&strq->outqueue); 192171158Srrs if (sp == NULL) { 193171158Srrs continue; 194171158Srrs } 195171158Srrs if ((sp->msg_is_complete) && 196171158Srrs (sp->length == 0) && 197171158Srrs (sp->sender_all_done)) { 198171158Srrs /* 199171158Srrs * We are doing differed cleanup. Last time 200171158Srrs * through when we took all the data the 201171158Srrs * sender_all_done was not set. 202171158Srrs */ 203171158Srrs if (sp->put_last_out == 0) { 204171158Srrs SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 205171158Srrs SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 206171158Srrs sp->sender_all_done, 207171158Srrs sp->length, 208171158Srrs sp->msg_is_complete, 209171158Srrs sp->put_last_out); 210171158Srrs } 211171158Srrs atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 212171158Srrs TAILQ_REMOVE(&strq->outqueue, sp, next); 213171158Srrs sctp_free_remote_addr(sp->net); 214171158Srrs if (sp->data) { 215171158Srrs sctp_m_freem(sp->data); 216171158Srrs sp->data = NULL; 217171158Srrs } 218171158Srrs sctp_free_a_strmoq(stcb, sp); 219171158Srrs goto is_there_another; 220171158Srrs } else { 221171158Srrs unsent_data++; 222171158Srrs continue; 223171158Srrs } 224171158Srrs } 225171158Srrs } 226171158Srrs SCTP_TCB_SEND_UNLOCK(stcb); 227171158Srrs return (unsent_data); 228171158Srrs} 229171158Srrs 230163953Srrsstatic int 231163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 232163953Srrs struct sctp_nets *net) 233163953Srrs{ 234163953Srrs struct sctp_init *init; 235163953Srrs struct sctp_association *asoc; 236163953Srrs struct sctp_nets *lnet; 237163953Srrs unsigned int i; 238163953Srrs 239163953Srrs init = &cp->init; 240163953Srrs asoc = &stcb->asoc; 241163953Srrs /* save off parameters */ 242163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 243163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 244163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 245163953Srrs /* update any ssthresh's that may have a default */ 246163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 247163953Srrs lnet->ssthresh = asoc->peers_rwnd; 248163953Srrs 249170744Srrs if (sctp_logging_level & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 250170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 251170744Srrs } 252163953Srrs } 253163953Srrs } 254164181Srrs SCTP_TCB_SEND_LOCK(stcb); 255163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 256163953Srrs unsigned int newcnt; 257163953Srrs struct sctp_stream_out *outs; 258163953Srrs struct sctp_stream_queue_pending *sp; 259163953Srrs 260163953Srrs /* cut back on number of streams */ 261163953Srrs newcnt = ntohs(init->num_inbound_streams); 262163953Srrs /* This if is probably not needed but I am cautious */ 263163953Srrs if (asoc->strmout) { 264163953Srrs /* First make sure no data chunks are trapped */ 265163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 266163953Srrs outs = &asoc->strmout[i]; 267163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 268163953Srrs while (sp) { 269163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 270163953Srrs next); 271163953Srrs asoc->stream_queue_cnt--; 272163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 273163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 274163953Srrs sp); 275163953Srrs if (sp->data) { 276163953Srrs sctp_m_freem(sp->data); 277163953Srrs sp->data = NULL; 278163953Srrs } 279163953Srrs sctp_free_remote_addr(sp->net); 280163953Srrs sp->net = NULL; 281163953Srrs /* Free the chunk */ 282169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 283163979Sru sp, stcb); 284163953Srrs 285163953Srrs sctp_free_a_strmoq(stcb, sp); 286169655Srrs /* sa_ignore FREED_MEMORY */ 287163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 288163953Srrs } 289163953Srrs } 290163953Srrs } 291163953Srrs /* cut back the count and abandon the upper streams */ 292163953Srrs asoc->pre_open_streams = newcnt; 293163953Srrs } 294164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 295163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 296163953Srrs /* init tsn's */ 297163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 298170744Srrs if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) { 299170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 300170744Srrs } 301163953Srrs /* This is the next one we expect */ 302163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 303163953Srrs 304163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 305163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 306163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 307163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 308163953Srrs /* open the requested streams */ 309170091Srrs 310163953Srrs if (asoc->strmin != NULL) { 311163953Srrs /* Free the old ones */ 312164181Srrs struct sctp_queued_to_read *ctl; 313164181Srrs 314164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 315164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 316164181Srrs while (ctl) { 317164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 318164181Srrs sctp_free_remote_addr(ctl->whoFrom); 319171158Srrs ctl->whoFrom = NULL; 320164181Srrs sctp_m_freem(ctl->data); 321164181Srrs ctl->data = NULL; 322164181Srrs sctp_free_a_readq(stcb, ctl); 323164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 324164181Srrs } 325164181Srrs } 326170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 327163953Srrs } 328164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 329164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 330164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 331164181Srrs } 332163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 333170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 334163953Srrs if (asoc->strmin == NULL) { 335163953Srrs /* we didn't get memory for the streams! */ 336169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 337163953Srrs return (-1); 338163953Srrs } 339163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 340163953Srrs asoc->strmin[i].stream_no = i; 341163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 342163953Srrs /* 343163953Srrs * U-stream ranges will be set when the cookie is unpacked. 344163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 345163953Srrs * supported) when the INIT-ACK arrives. 346163953Srrs */ 347163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 348168943Srrs asoc->strmin[i].delivery_started = 0; 349163953Srrs } 350163953Srrs /* 351163953Srrs * load_address_from_init will put the addresses into the 352163953Srrs * association when the COOKIE is processed or the INIT-ACK is 353163953Srrs * processed. Both types of COOKIE's existing and new call this 354163953Srrs * routine. It will remove addresses that are no longer in the 355163953Srrs * association (for the restarting case where addresses are 356163953Srrs * removed). Up front when the INIT arrives we will discard it if it 357163953Srrs * is a restart and new addresses have been added. 358163953Srrs */ 359169655Srrs /* sa_ignore MEMLEAK */ 360163953Srrs return (0); 361163953Srrs} 362163953Srrs 363163953Srrs/* 364163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 365163953Srrs */ 366163953Srrsstatic int 367163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 368163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 369170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 370163953Srrs{ 371163953Srrs struct sctp_association *asoc; 372163953Srrs struct mbuf *op_err; 373163953Srrs int retval, abort_flag; 374163953Srrs uint32_t initack_limit; 375163953Srrs 376163953Srrs /* First verify that we have no illegal param's */ 377163953Srrs abort_flag = 0; 378163953Srrs op_err = NULL; 379163953Srrs 380163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 381163953Srrs (offset + sizeof(struct sctp_init_chunk)), 382163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 383163953Srrs if (abort_flag) { 384163953Srrs /* Send an abort and notify peer */ 385170056Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err); 386170056Srrs *abort_no_unlock = 1; 387163953Srrs return (-1); 388163953Srrs } 389163953Srrs asoc = &stcb->asoc; 390163953Srrs /* process the peer's parameters in the INIT-ACK */ 391163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 392163953Srrs if (retval < 0) { 393163953Srrs return (retval); 394163953Srrs } 395163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 396163953Srrs /* load all addresses */ 397163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 398163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 399163953Srrs NULL))) { 400163953Srrs /* Huh, we should abort */ 401169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 402169420Srrs "Load addresses from INIT causes an abort %d\n", 403169420Srrs retval); 404163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 405170181Srrs NULL, 0); 406168299Srrs *abort_no_unlock = 1; 407163953Srrs return (-1); 408163953Srrs } 409171477Srrs /* if the peer doesn't support asconf, flush the asconf queue */ 410171477Srrs if (asoc->peer_supports_asconf == 0) { 411171477Srrs struct sctp_asconf_addr *aparam; 412171477Srrs 413171477Srrs while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 414171477Srrs /* sa_ignore FREED_MEMORY */ 415171477Srrs aparam = TAILQ_FIRST(&asoc->asconf_queue); 416171477Srrs TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 417171477Srrs SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 418171477Srrs } 419171477Srrs } 420163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 421163953Srrs stcb->asoc.local_hmacs); 422163953Srrs if (op_err) { 423163953Srrs sctp_queue_op_err(stcb, op_err); 424163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 425163953Srrs op_err = NULL; 426163953Srrs } 427163953Srrs /* extract the cookie and queue it to "echo" it back... */ 428171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 429171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 430171943Srrs stcb->asoc.overall_error_count, 431171943Srrs 0, 432171943Srrs SCTP_FROM_SCTP_INPUT, 433171943Srrs __LINE__); 434171943Srrs } 435163953Srrs stcb->asoc.overall_error_count = 0; 436163953Srrs net->error_count = 0; 437163953Srrs 438163953Srrs /* 439163953Srrs * Cancel the INIT timer, We do this first before queueing the 440163953Srrs * cookie. We always cancel at the primary to assue that we are 441163953Srrs * canceling the timer started by the INIT which always goes to the 442163953Srrs * primary. 443163953Srrs */ 444163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 445165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 446163953Srrs 447165220Srrs /* calculate the RTO */ 448171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 449165220Srrs 450163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 451163953Srrs if (retval < 0) { 452163953Srrs /* 453163953Srrs * No cookie, we probably should send a op error. But in any 454163953Srrs * case if there is no cookie in the INIT-ACK, we can 455163953Srrs * abandon the peer, its broke. 456163953Srrs */ 457163953Srrs if (retval == -3) { 458163953Srrs /* We abort with an error of missing mandatory param */ 459163953Srrs op_err = 460163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 461163953Srrs if (op_err) { 462163953Srrs /* 463163953Srrs * Expand beyond to include the mandatory 464163953Srrs * param cookie 465163953Srrs */ 466163953Srrs struct sctp_inv_mandatory_param *mp; 467163953Srrs 468165647Srrs SCTP_BUF_LEN(op_err) = 469163953Srrs sizeof(struct sctp_inv_mandatory_param); 470163953Srrs mp = mtod(op_err, 471163953Srrs struct sctp_inv_mandatory_param *); 472163953Srrs /* Subtract the reserved param */ 473163953Srrs mp->length = 474163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 475163953Srrs mp->num_param = htonl(1); 476163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 477163953Srrs mp->resv = 0; 478163953Srrs } 479163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 480170181Srrs sh, op_err, 0); 481168299Srrs *abort_no_unlock = 1; 482163953Srrs } 483163953Srrs return (retval); 484163953Srrs } 485163953Srrs return (0); 486163953Srrs} 487163953Srrs 488163953Srrsstatic void 489163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 490163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 491163953Srrs{ 492163953Srrs struct sockaddr_storage store; 493163953Srrs struct sockaddr_in *sin; 494163953Srrs struct sockaddr_in6 *sin6; 495163953Srrs struct sctp_nets *r_net; 496163953Srrs struct timeval tv; 497163953Srrs 498163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 499163953Srrs /* Invalid length */ 500163953Srrs return; 501163953Srrs } 502163953Srrs sin = (struct sockaddr_in *)&store; 503163953Srrs sin6 = (struct sockaddr_in6 *)&store; 504163953Srrs 505163953Srrs memset(&store, 0, sizeof(store)); 506163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 507163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 508163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 509163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 510163953Srrs sin->sin_port = stcb->rport; 511163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 512163953Srrs sizeof(sin->sin_addr)); 513163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 514163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 515163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 516163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 517163953Srrs sin6->sin6_port = stcb->rport; 518163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 519163953Srrs sizeof(sin6->sin6_addr)); 520163953Srrs } else { 521163953Srrs return; 522163953Srrs } 523163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 524163953Srrs if (r_net == NULL) { 525169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 526163953Srrs return; 527163953Srrs } 528163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 529163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 530163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 531163953Srrs /* 532163953Srrs * If the its a HB and it's random value is correct when can 533163953Srrs * confirm the destination. 534163953Srrs */ 535163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 536167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 537167598Srrs stcb->asoc.primary_destination = r_net; 538167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 539167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 540167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 541167598Srrs if (r_net != stcb->asoc.primary_destination) { 542167598Srrs /* 543167598Srrs * first one on the list is NOT the primary 544167598Srrs * sctp_cmpaddr() is much more efficent if 545167598Srrs * the primary is the first on the list, 546167598Srrs * make it so. 547167598Srrs */ 548167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 549167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 550167598Srrs } 551167598Srrs } 552163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 553163953Srrs stcb, 0, (void *)r_net); 554163953Srrs } 555163953Srrs r_net->error_count = 0; 556163953Srrs r_net->hb_responded = 1; 557163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 558163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 559163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 560163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 561163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 562163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 563163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 564163953Srrs /* now was it the primary? if so restore */ 565163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 566169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 567163953Srrs } 568163953Srrs } 569171440Srrs /* 570171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 571171440Srrs * set the destination to active state and set the cwnd to one or 572171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 573171440Srrs * timer is running, for the destination, stop the timer because a 574171440Srrs * PF-heartbeat was received. 575171440Srrs */ 576171477Srrs if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == 577171440Srrs SCTP_ADDR_PF) { 578171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 579171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 580171440Srrs stcb, net, 581171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 582171440Srrs } 583171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 584171440Srrs net->cwnd = net->mtu * sctp_cmt_pf; 585171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 586171440Srrs net, net->cwnd); 587171440Srrs } 588163953Srrs /* Now lets do a RTO with this */ 589171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 590163953Srrs} 591163953Srrs 592163953Srrsstatic void 593163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 594163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 595163953Srrs{ 596169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 597163953Srrs if (stcb == NULL) 598163953Srrs return; 599163953Srrs 600163953Srrs /* stop any receive timers */ 601171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 602163953Srrs /* notify user of the abort and clean up... */ 603163953Srrs sctp_abort_notification(stcb, 0); 604163953Srrs /* free the tcb */ 605163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 606163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 607163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 608163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 609163953Srrs } 610168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 611168859Srrs sctp_print_out_track_log(stcb); 612168859Srrs#endif 613171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 614171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 615169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 616163953Srrs} 617163953Srrs 618163953Srrsstatic void 619163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 620163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 621163953Srrs{ 622163953Srrs struct sctp_association *asoc; 623163953Srrs int some_on_streamwheel; 624163953Srrs 625169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 626169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 627163953Srrs if (stcb == NULL) 628163953Srrs return; 629165220Srrs asoc = &stcb->asoc; 630165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 631165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 632163953Srrs return; 633163953Srrs } 634163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 635163953Srrs /* Shutdown NOT the expected size */ 636163953Srrs return; 637163953Srrs } else { 638163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 639163953Srrs } 640165220Srrs if (asoc->control_pdapi) { 641163953Srrs /* 642163953Srrs * With a normal shutdown we assume the end of last record. 643163953Srrs */ 644163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 645165220Srrs asoc->control_pdapi->end_added = 1; 646165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 647165220Srrs asoc->control_pdapi = NULL; 648163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 649163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 650163953Srrs } 651163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 652163953Srrs if (stcb->sctp_socket) { 653163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 654170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 655163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 656171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 657163953Srrs /* 658163953Srrs * notify upper layer that peer has initiated a 659163953Srrs * shutdown 660163953Srrs */ 661163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 662163953Srrs 663163953Srrs /* reset time */ 664169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 665163953Srrs } 666163953Srrs } 667163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 668163953Srrs /* 669163953Srrs * stop the shutdown timer, since we WILL move to 670163953Srrs * SHUTDOWN-ACK-SENT. 671163953Srrs */ 672171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 673163953Srrs } 674171158Srrs /* Now is there unsent data on a stream somewhere? */ 675171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 676163953Srrs 677163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 678163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 679163953Srrs some_on_streamwheel) { 680163953Srrs /* By returning we will push more data out */ 681163953Srrs return; 682163953Srrs } else { 683163953Srrs /* no outstanding data to send, so move on... */ 684163953Srrs /* send SHUTDOWN-ACK */ 685163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 686163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 687166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 688166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 689163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 690163953Srrs } 691171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 692163953Srrs 693163953Srrs /* start SHUTDOWN timer */ 694163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 695163953Srrs stcb, net); 696163953Srrs } 697163953Srrs} 698163953Srrs 699163953Srrsstatic void 700163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 701163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 702163953Srrs{ 703163953Srrs struct sctp_association *asoc; 704163953Srrs 705169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 706169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 707163953Srrs if (stcb == NULL) 708163953Srrs return; 709163953Srrs 710163953Srrs asoc = &stcb->asoc; 711163953Srrs /* process according to association state */ 712163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 713163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 714163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 715163953Srrs SCTP_TCB_UNLOCK(stcb); 716163953Srrs return; 717163953Srrs } 718165220Srrs if (asoc->control_pdapi) { 719163953Srrs /* 720163953Srrs * With a normal shutdown we assume the end of last record. 721163953Srrs */ 722163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 723165220Srrs asoc->control_pdapi->end_added = 1; 724165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 725165220Srrs asoc->control_pdapi = NULL; 726163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 727163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 728163953Srrs } 729163953Srrs /* are the queues empty? */ 730163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 731163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 732163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 733164205Srrs sctp_report_all_outbound(stcb, 0); 734163953Srrs } 735163953Srrs /* stop the timer */ 736171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 737163953Srrs /* send SHUTDOWN-COMPLETE */ 738163953Srrs sctp_send_shutdown_complete(stcb, net); 739163953Srrs /* notify upper layer protocol */ 740163953Srrs if (stcb->sctp_socket) { 741163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 742163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 743163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 744163953Srrs /* Set the connected flag to disconnected */ 745163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 746163953Srrs } 747163953Srrs } 748163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 749163953Srrs /* free the TCB but first save off the ep */ 750171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 751171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 752163953Srrs} 753163953Srrs 754163953Srrs/* 755163953Srrs * Skip past the param header and then we will find the chunk that caused the 756163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 757163953Srrs * our peer must be broken. 758163953Srrs */ 759163953Srrsstatic void 760163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 761163953Srrs struct sctp_nets *net) 762163953Srrs{ 763163953Srrs struct sctp_chunkhdr *chk; 764163953Srrs 765163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 766163953Srrs switch (chk->chunk_type) { 767163953Srrs case SCTP_ASCONF_ACK: 768163953Srrs case SCTP_ASCONF: 769163953Srrs sctp_asconf_cleanup(stcb, net); 770163953Srrs break; 771163953Srrs case SCTP_FORWARD_CUM_TSN: 772163953Srrs stcb->asoc.peer_supports_prsctp = 0; 773163953Srrs break; 774163953Srrs default: 775169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 776169420Srrs "Peer does not support chunk type %d(%x)??\n", 777169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 778163953Srrs break; 779163953Srrs } 780163953Srrs} 781163953Srrs 782163953Srrs/* 783163953Srrs * Skip past the param header and then we will find the param that caused the 784163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 785163953Srrs * these will turn of specific features. 786163953Srrs */ 787163953Srrsstatic void 788163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 789163953Srrs{ 790163953Srrs struct sctp_paramhdr *pbad; 791163953Srrs 792163953Srrs pbad = phdr + 1; 793163953Srrs switch (ntohs(pbad->param_type)) { 794163953Srrs /* pr-sctp draft */ 795163953Srrs case SCTP_PRSCTP_SUPPORTED: 796163953Srrs stcb->asoc.peer_supports_prsctp = 0; 797163953Srrs break; 798163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 799163953Srrs break; 800163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 801163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 802163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 803163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 804163953Srrs stcb->asoc.ecn_allowed = 0; 805163953Srrs break; 806163953Srrs case SCTP_ADD_IP_ADDRESS: 807163953Srrs case SCTP_DEL_IP_ADDRESS: 808163953Srrs case SCTP_SET_PRIM_ADDR: 809163953Srrs stcb->asoc.peer_supports_asconf = 0; 810163953Srrs break; 811163953Srrs case SCTP_SUCCESS_REPORT: 812163953Srrs case SCTP_ERROR_CAUSE_IND: 813169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 814169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 815169420Srrs "Turning off ASCONF to this strange peer\n"); 816163953Srrs stcb->asoc.peer_supports_asconf = 0; 817163953Srrs break; 818163953Srrs default: 819169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 820169420Srrs "Peer does not support param type %d(%x)??\n", 821169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 822163953Srrs break; 823163953Srrs } 824163953Srrs} 825163953Srrs 826163953Srrsstatic int 827163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 828163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 829163953Srrs{ 830163953Srrs int chklen; 831163953Srrs struct sctp_paramhdr *phdr; 832163953Srrs uint16_t error_type; 833163953Srrs uint16_t error_len; 834163953Srrs struct sctp_association *asoc; 835163953Srrs 836163953Srrs int adjust; 837163953Srrs 838163953Srrs /* parse through all of the errors and process */ 839163953Srrs asoc = &stcb->asoc; 840163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 841163953Srrs sizeof(struct sctp_chunkhdr)); 842163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 843163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 844163953Srrs /* Process an Error Cause */ 845163953Srrs error_type = ntohs(phdr->param_type); 846163953Srrs error_len = ntohs(phdr->param_length); 847163953Srrs if ((error_len > chklen) || (error_len == 0)) { 848163953Srrs /* invalid param length for this param */ 849169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 850169420Srrs chklen, error_len); 851163953Srrs return (0); 852163953Srrs } 853163953Srrs switch (error_type) { 854163953Srrs case SCTP_CAUSE_INVALID_STREAM: 855163953Srrs case SCTP_CAUSE_MISSING_PARAM: 856163953Srrs case SCTP_CAUSE_INVALID_PARAM: 857163953Srrs case SCTP_CAUSE_NO_USER_DATA: 858169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 859169420Srrs error_type); 860163953Srrs break; 861163953Srrs case SCTP_CAUSE_STALE_COOKIE: 862163953Srrs /* 863163953Srrs * We only act if we have echoed a cookie and are 864163953Srrs * waiting. 865163953Srrs */ 866163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 867163953Srrs int *p; 868163953Srrs 869163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 870163953Srrs /* Save the time doubled */ 871163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 872163953Srrs asoc->stale_cookie_count++; 873163953Srrs if (asoc->stale_cookie_count > 874163953Srrs asoc->max_init_times) { 875163953Srrs sctp_abort_notification(stcb, 0); 876163953Srrs /* now free the asoc */ 877171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 878171943Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 879163953Srrs return (-1); 880163953Srrs } 881163953Srrs /* blast back to INIT state */ 882163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 883163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 884163953Srrs 885163953Srrs sctp_stop_all_cookie_timers(stcb); 886163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 887163953Srrs } 888163953Srrs break; 889163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 890163953Srrs /* 891163953Srrs * Nothing we can do here, we don't do hostname 892163953Srrs * addresses so if the peer does not like my IPv6 893163953Srrs * (or IPv4 for that matter) it does not matter. If 894163953Srrs * they don't support that type of address, they can 895163953Srrs * NOT possibly get that packet type... i.e. with no 896163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 897163953Srrs * safely ignore this one. If we ever added support 898163953Srrs * for HOSTNAME Addresses, then we would need to do 899163953Srrs * something here. 900163953Srrs */ 901163953Srrs break; 902163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 903163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 904163953Srrs break; 905163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 906163953Srrs sctp_process_unrecog_param(stcb, phdr); 907163953Srrs break; 908163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 909163953Srrs /* 910163953Srrs * We ignore this since the timer will drive out a 911163953Srrs * new cookie anyway and there timer will drive us 912163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 913163953Srrs * here since we don't have their tag. 914163953Srrs */ 915163953Srrs break; 916163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 917163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 918163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 919163953Srrs /* 920163953Srrs * We should NOT get these here, but in a 921166086Srrs * ASCONF-ACK. 922163953Srrs */ 923169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 924169420Srrs error_type); 925163953Srrs break; 926163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 927163953Srrs /* 928163953Srrs * And what, pray tell do we do with the fact that 929163953Srrs * the peer is out of resources? Not really sure we 930166086Srrs * could do anything but abort. I suspect this 931163953Srrs * should have came WITH an abort instead of in a 932163953Srrs * OP-ERROR. 933163953Srrs */ 934163953Srrs break; 935163953Srrs default: 936169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 937169420Srrs error_type); 938163953Srrs break; 939163953Srrs } 940163953Srrs adjust = SCTP_SIZE32(error_len); 941163953Srrs chklen -= adjust; 942163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 943163953Srrs } 944163953Srrs return (0); 945163953Srrs} 946163953Srrs 947163953Srrsstatic int 948169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 949169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 950170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 951163953Srrs{ 952163953Srrs struct sctp_init_ack *init_ack; 953163953Srrs struct mbuf *op_err; 954163953Srrs 955169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 956169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 957169420Srrs 958163953Srrs if (stcb == NULL) { 959169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 960169420Srrs "sctp_handle_init_ack: TCB is null\n"); 961163953Srrs return (-1); 962163953Srrs } 963163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 964163953Srrs /* Invalid length */ 965163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 966163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 967170181Srrs op_err, 0); 968168299Srrs *abort_no_unlock = 1; 969163953Srrs return (-1); 970163953Srrs } 971163953Srrs init_ack = &cp->init; 972163953Srrs /* validate parameters */ 973163953Srrs if (init_ack->initiate_tag == 0) { 974163953Srrs /* protocol error... send an abort */ 975163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 976163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 977170181Srrs op_err, 0); 978168299Srrs *abort_no_unlock = 1; 979163953Srrs return (-1); 980163953Srrs } 981163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 982163953Srrs /* protocol error... send an abort */ 983163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 984163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 985170181Srrs op_err, 0); 986168299Srrs *abort_no_unlock = 1; 987163953Srrs return (-1); 988163953Srrs } 989163953Srrs if (init_ack->num_inbound_streams == 0) { 990163953Srrs /* protocol error... send an abort */ 991163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 992163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 993170181Srrs op_err, 0); 994168299Srrs *abort_no_unlock = 1; 995163953Srrs return (-1); 996163953Srrs } 997163953Srrs if (init_ack->num_outbound_streams == 0) { 998163953Srrs /* protocol error... send an abort */ 999163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1000163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1001170181Srrs op_err, 0); 1002168299Srrs *abort_no_unlock = 1; 1003163953Srrs return (-1); 1004163953Srrs } 1005163953Srrs /* process according to association state... */ 1006171943Srrs switch (stcb->asoc.state & SCTP_STATE_MASK) { 1007163953Srrs case SCTP_STATE_COOKIE_WAIT: 1008163953Srrs /* this is the expected state for this chunk */ 1009163953Srrs /* process the INIT-ACK parameters */ 1010163953Srrs if (stcb->asoc.primary_destination->dest_state & 1011163953Srrs SCTP_ADDR_UNCONFIRMED) { 1012163953Srrs /* 1013163953Srrs * The primary is where we sent the INIT, we can 1014163953Srrs * always consider it confirmed when the INIT-ACK is 1015163953Srrs * returned. Do this before we load addresses 1016163953Srrs * though. 1017163953Srrs */ 1018163953Srrs stcb->asoc.primary_destination->dest_state &= 1019163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1020163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1021163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 1022163953Srrs } 1023169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1024170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1025163953Srrs /* error in parsing parameters */ 1026163953Srrs return (-1); 1027163953Srrs } 1028163953Srrs /* update our state */ 1029169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1030171943Srrs SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1031163953Srrs 1032163953Srrs /* reset the RTO calc */ 1033171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 1034171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1035171943Srrs stcb->asoc.overall_error_count, 1036171943Srrs 0, 1037171943Srrs SCTP_FROM_SCTP_INPUT, 1038171943Srrs __LINE__); 1039171943Srrs } 1040163953Srrs stcb->asoc.overall_error_count = 0; 1041169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1042163953Srrs /* 1043163953Srrs * collapse the init timer back in case of a exponential 1044165220Srrs * backoff 1045163953Srrs */ 1046163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1047163953Srrs stcb, net); 1048163953Srrs /* 1049163953Srrs * the send at the end of the inbound data processing will 1050163953Srrs * cause the cookie to be sent 1051163953Srrs */ 1052163953Srrs break; 1053163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1054163953Srrs /* incorrect state... discard */ 1055163953Srrs break; 1056163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1057163953Srrs /* incorrect state... discard */ 1058163953Srrs break; 1059163953Srrs case SCTP_STATE_OPEN: 1060163953Srrs /* incorrect state... discard */ 1061163953Srrs break; 1062163953Srrs case SCTP_STATE_EMPTY: 1063163953Srrs case SCTP_STATE_INUSE: 1064163953Srrs default: 1065163953Srrs /* incorrect state... discard */ 1066163953Srrs return (-1); 1067163953Srrs break; 1068163953Srrs } 1069169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1070163953Srrs return (0); 1071163953Srrs} 1072163953Srrs 1073163953Srrs 1074163953Srrs/* 1075163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1076163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1077163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1078163953Srrs * mbuf to the cookie-echo chunk 1079163953Srrs */ 1080163953Srrsstatic struct sctp_tcb * 1081163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1082163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1083163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1084169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1085170181Srrs uint32_t vrf_id) 1086163953Srrs{ 1087163953Srrs struct sctp_association *asoc; 1088163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1089163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1090163953Srrs int chk_length; 1091165220Srrs int init_offset, initack_offset, i; 1092163953Srrs int retval; 1093164205Srrs int spec_flag = 0; 1094170140Srrs uint32_t how_indx; 1095163953Srrs 1096163953Srrs /* I know that the TCB is non-NULL from the caller */ 1097163953Srrs asoc = &stcb->asoc; 1098166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1099166023Srrs if (asoc->cookie_how[how_indx] == 0) 1100166023Srrs break; 1101166023Srrs } 1102166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1103166023Srrs asoc->cookie_how[how_indx] = 1; 1104166023Srrs } 1105163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1106163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1107163953Srrs struct mbuf *op_err; 1108163953Srrs struct sctp_paramhdr *ph; 1109163953Srrs 1110163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1111163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1112165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1113163953Srrs if (op_err == NULL) { 1114163953Srrs /* FOOBAR */ 1115163953Srrs return (NULL); 1116163953Srrs } 1117163953Srrs /* pre-reserve some space */ 1118165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1119165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1120165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1121163953Srrs /* Set the len */ 1122165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1123163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1124163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1125163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1126169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1127170181Srrs vrf_id); 1128166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1129166023Srrs asoc->cookie_how[how_indx] = 2; 1130163953Srrs return (NULL); 1131163953Srrs } 1132163953Srrs /* 1133163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1134163953Srrs * INIT should start after the cookie-echo header struct (chunk 1135163953Srrs * header, state cookie header struct) 1136163953Srrs */ 1137163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1138163953Srrs 1139163953Srrs init_cp = (struct sctp_init_chunk *) 1140163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1141163953Srrs (uint8_t *) & init_buf); 1142163953Srrs if (init_cp == NULL) { 1143163953Srrs /* could not pull a INIT chunk in cookie */ 1144163953Srrs return (NULL); 1145163953Srrs } 1146163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1147163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1148163953Srrs return (NULL); 1149163953Srrs } 1150163953Srrs /* 1151163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1152163953Srrs * INIT-ACK follows the INIT chunk 1153163953Srrs */ 1154163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1155163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1156163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1157163953Srrs (uint8_t *) & initack_buf); 1158163953Srrs if (initack_cp == NULL) { 1159163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1160163953Srrs return (NULL); 1161163953Srrs } 1162163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1163163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1164163953Srrs return (NULL); 1165163953Srrs } 1166163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1167163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1168163953Srrs /* 1169163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1170163953Srrs * to get into the OPEN state 1171163953Srrs */ 1172166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1173166086Srrs#ifdef INVARIANTS 1174166023Srrs panic("Case D and non-match seq?"); 1175166086Srrs#else 1176169420Srrs SCTP_PRINTF("Case D, seq non-match %x vs %x?\n", 1177169420Srrs ntohl(initack_cp->init.initial_tsn), 1178169420Srrs asoc->init_seq_number); 1179166086Srrs#endif 1180166023Srrs } 1181163953Srrs switch SCTP_GET_STATE 1182163953Srrs (asoc) { 1183163953Srrs case SCTP_STATE_COOKIE_WAIT: 1184166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1185163953Srrs /* 1186169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1187166023Srrs * correct tags... just accept it...but we must 1188166023Srrs * process the init so that we can make sure we have 1189166023Srrs * the right seq no's. 1190163953Srrs */ 1191163953Srrs /* First we must process the INIT !! */ 1192163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1193163953Srrs if (retval < 0) { 1194166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1195166023Srrs asoc->cookie_how[how_indx] = 3; 1196163953Srrs return (NULL); 1197163953Srrs } 1198163953Srrs /* we have already processed the INIT so no problem */ 1199163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1200171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1201171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1202163953Srrs /* update current state */ 1203166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1204166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1205166675Srrs else 1206166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1207171943Srrs 1208171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1209163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1210163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1211163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1212163953Srrs } 1213166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1214165220Srrs sctp_stop_all_cookie_timers(stcb); 1215163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1216163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1217163953Srrs (inp->sctp_socket->so_qlimit == 0) 1218163953Srrs ) { 1219163953Srrs /* 1220163953Srrs * Here is where collision would go if we 1221163953Srrs * did a connect() and instead got a 1222163953Srrs * init/init-ack/cookie done before the 1223163953Srrs * init-ack came back.. 1224163953Srrs */ 1225163953Srrs stcb->sctp_ep->sctp_flags |= 1226163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1227163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1228163953Srrs } 1229163953Srrs /* notify upper layer */ 1230163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1231163953Srrs /* 1232163953Srrs * since we did not send a HB make sure we don't 1233163953Srrs * double things 1234163953Srrs */ 1235163953Srrs net->hb_responded = 1; 1236170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1237171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1238163953Srrs 1239163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1240163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1241163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1242163953Srrs inp, stcb, NULL); 1243163953Srrs } 1244163953Srrs break; 1245163953Srrs default: 1246163953Srrs /* 1247163953Srrs * we're in the OPEN state (or beyond), so peer must 1248163953Srrs * have simply lost the COOKIE-ACK 1249163953Srrs */ 1250163953Srrs break; 1251163953Srrs } /* end switch */ 1252165220Srrs sctp_stop_all_cookie_timers(stcb); 1253163953Srrs /* 1254163953Srrs * We ignore the return code here.. not sure if we should 1255163953Srrs * somehow abort.. but we do have an existing asoc. This 1256163953Srrs * really should not fail. 1257163953Srrs */ 1258163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1259163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1260163953Srrs initack_offset, sh, init_src)) { 1261166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1262166023Srrs asoc->cookie_how[how_indx] = 4; 1263163953Srrs return (NULL); 1264163953Srrs } 1265163953Srrs /* respond with a COOKIE-ACK */ 1266165220Srrs sctp_toss_old_cookies(stcb, asoc); 1267163953Srrs sctp_send_cookie_ack(stcb); 1268166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1269166023Srrs asoc->cookie_how[how_indx] = 5; 1270163953Srrs return (stcb); 1271169352Srrs } 1272163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1273163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1274163953Srrs cookie->tie_tag_my_vtag == 0 && 1275163953Srrs cookie->tie_tag_peer_vtag == 0) { 1276163953Srrs /* 1277163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1278163953Srrs */ 1279166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1280166023Srrs asoc->cookie_how[how_indx] = 6; 1281163953Srrs return (NULL); 1282163953Srrs } 1283163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1284163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1285163953Srrs init_cp->init.initiate_tag == 0)) { 1286163953Srrs /* 1287163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1288163953Srrs * should be ok, re-accept peer info 1289163953Srrs */ 1290166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1291166023Srrs /* 1292166023Srrs * Extension of case C. If we hit this, then the 1293166023Srrs * random number generator returned the same vtag 1294166023Srrs * when we first sent our INIT-ACK and when we later 1295166023Srrs * sent our INIT. The side with the seq numbers that 1296166023Srrs * are different will be the one that normnally 1297166023Srrs * would have hit case C. This in effect "extends" 1298166023Srrs * our vtags in this collision case to be 64 bits. 1299166023Srrs * The same collision could occur aka you get both 1300166023Srrs * vtag and seq number the same twice in a row.. but 1301166023Srrs * is much less likely. If it did happen then we 1302166023Srrs * would proceed through and bring up the assoc.. we 1303166023Srrs * may end up with the wrong stream setup however.. 1304166023Srrs * which would be bad.. but there is no way to 1305166023Srrs * tell.. until we send on a stream that does not 1306166023Srrs * exist :-) 1307166023Srrs */ 1308166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1309166023Srrs asoc->cookie_how[how_indx] = 7; 1310166023Srrs 1311166023Srrs return (NULL); 1312166023Srrs } 1313166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1314166023Srrs asoc->cookie_how[how_indx] = 8; 1315171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1316163953Srrs sctp_stop_all_cookie_timers(stcb); 1317163953Srrs /* 1318163953Srrs * since we did not send a HB make sure we don't double 1319163953Srrs * things 1320163953Srrs */ 1321163953Srrs net->hb_responded = 1; 1322163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1323163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1324163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1325163953Srrs NULL); 1326163953Srrs } 1327163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1328164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1329164205Srrs 1330164205Srrs /* Note last_cwr_tsn? where is this used? */ 1331163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1332164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1333164205Srrs /* 1334164205Srrs * Ok the peer probably discarded our data (if we 1335164205Srrs * echoed a cookie+data). So anything on the 1336164205Srrs * sent_queue should be marked for retransmit, we 1337164205Srrs * may not get something to kick us so it COULD 1338164205Srrs * still take a timeout to move these.. but it can't 1339164205Srrs * hurt to mark them. 1340164205Srrs */ 1341164205Srrs struct sctp_tmit_chunk *chk; 1342163953Srrs 1343164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1344164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1345164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1346171440Srrs sctp_flight_size_decrease(chk); 1347171440Srrs sctp_total_flight_decrease(stcb, chk); 1348168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1349164205Srrs spec_flag++; 1350164205Srrs } 1351164205Srrs } 1352164205Srrs 1353164205Srrs } 1354163953Srrs /* process the INIT info (peer's info) */ 1355163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1356163953Srrs if (retval < 0) { 1357166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1358166023Srrs asoc->cookie_how[how_indx] = 9; 1359163953Srrs return (NULL); 1360163953Srrs } 1361163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1362163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1363163953Srrs initack_offset, sh, init_src)) { 1364166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1365166023Srrs asoc->cookie_how[how_indx] = 10; 1366163953Srrs return (NULL); 1367163953Srrs } 1368163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1369163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1370163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1371163953Srrs 1372163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1373163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1374163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1375163953Srrs stcb->sctp_ep->sctp_flags |= 1376163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1377163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1378163953Srrs } 1379166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1380166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1381166675Srrs else 1382166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1383166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1384166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1385166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1386166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1387166675Srrs } else { 1388166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1389163953Srrs } 1390171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1391163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1392163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1393163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1394163953Srrs 1395163953Srrs } 1396163953Srrs sctp_stop_all_cookie_timers(stcb); 1397165220Srrs sctp_toss_old_cookies(stcb, asoc); 1398163953Srrs sctp_send_cookie_ack(stcb); 1399164205Srrs if (spec_flag) { 1400164205Srrs /* 1401164205Srrs * only if we have retrans set do we do this. What 1402164205Srrs * this call does is get only the COOKIE-ACK out and 1403164205Srrs * then when we return the normal call to 1404164205Srrs * sctp_chunk_output will get the retrans out behind 1405164205Srrs * this. 1406164205Srrs */ 1407164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1408164205Srrs } 1409166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1410166023Srrs asoc->cookie_how[how_indx] = 11; 1411166023Srrs 1412163953Srrs return (stcb); 1413163953Srrs } 1414163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1415163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1416163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1417163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1418163953Srrs cookie->tie_tag_peer_vtag != 0) { 1419163953Srrs struct sctpasochead *head; 1420163953Srrs 1421163953Srrs /* 1422163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1423163953Srrs */ 1424165220Srrs /* temp code */ 1425166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1426166023Srrs asoc->cookie_how[how_indx] = 12; 1427171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1428171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1429165647Srrs 1430163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1431163953Srrs /* notify upper layer */ 1432163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1433165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1434166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1435166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1436166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1437166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1438166675Srrs } 1439166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1440166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1441166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1442166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1443166675Srrs } 1444165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1445171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1446165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1447165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1448165647Srrs 1449165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1450165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1451171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1452165647Srrs } 1453165647Srrs asoc->pre_open_streams = 1454165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1455165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1456165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1457165647Srrs 1458165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1459165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1460165647Srrs 1461165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1462165647Srrs 1463165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1464170138Srrs if (asoc->mapping_array) { 1465165647Srrs memset(asoc->mapping_array, 0, 1466165647Srrs asoc->mapping_array_size); 1467170138Srrs } 1468165220Srrs SCTP_TCB_UNLOCK(stcb); 1469165220Srrs SCTP_INP_INFO_WLOCK(); 1470165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1471165220Srrs SCTP_TCB_LOCK(stcb); 1472165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1473163953Srrs /* send up all the data */ 1474164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1475165220Srrs 1476164205Srrs sctp_report_all_outbound(stcb, 1); 1477165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1478165220Srrs stcb->asoc.strmout[i].stream_no = i; 1479165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1480165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1481165220Srrs } 1482163953Srrs /* process the INIT-ACK info (my info) */ 1483163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1484163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1485163953Srrs 1486163953Srrs /* pull from vtag hash */ 1487163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1488163953Srrs /* re-insert to new vtag position */ 1489163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1490163953Srrs sctppcbinfo.hashasocmark)]; 1491163953Srrs /* 1492163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1493163953Srrs * system 1494163953Srrs */ 1495163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1496163953Srrs 1497163953Srrs /* Is this the first restart? */ 1498163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1499163953Srrs /* Ok add it to assoc_id vtag hash */ 1500163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1501163953Srrs sctppcbinfo.hashrestartmark)]; 1502163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1503163953Srrs stcb->asoc.in_restart_hash = 1; 1504163953Srrs } 1505163953Srrs /* process the INIT info (peer's info) */ 1506164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1507165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1508165220Srrs SCTP_INP_INFO_WUNLOCK(); 1509165220Srrs 1510163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1511163953Srrs if (retval < 0) { 1512166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1513166023Srrs asoc->cookie_how[how_indx] = 13; 1514166023Srrs 1515163953Srrs return (NULL); 1516163953Srrs } 1517163953Srrs /* 1518163953Srrs * since we did not send a HB make sure we don't double 1519163953Srrs * things 1520163953Srrs */ 1521163953Srrs net->hb_responded = 1; 1522163953Srrs 1523163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1524163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1525163953Srrs initack_offset, sh, init_src)) { 1526166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1527166023Srrs asoc->cookie_how[how_indx] = 14; 1528166023Srrs 1529163953Srrs return (NULL); 1530163953Srrs } 1531163953Srrs /* respond with a COOKIE-ACK */ 1532163953Srrs sctp_stop_all_cookie_timers(stcb); 1533165220Srrs sctp_toss_old_cookies(stcb, asoc); 1534163953Srrs sctp_send_cookie_ack(stcb); 1535166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1536166023Srrs asoc->cookie_how[how_indx] = 15; 1537163953Srrs 1538163953Srrs return (stcb); 1539163953Srrs } 1540166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1541166023Srrs asoc->cookie_how[how_indx] = 16; 1542163953Srrs /* all other cases... */ 1543163953Srrs return (NULL); 1544163953Srrs} 1545163953Srrs 1546166086Srrs 1547163953Srrs/* 1548163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1549163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1550163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1551163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1552163953Srrs * was from returns a new TCB 1553163953Srrs */ 1554163953Srrsstatic struct sctp_tcb * 1555163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1556163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1557163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1558163953Srrs struct sockaddr *init_src, int *notification, 1559169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1560170181Srrs uint32_t vrf_id) 1561163953Srrs{ 1562163953Srrs struct sctp_tcb *stcb; 1563163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1564163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1565163953Srrs struct sockaddr_storage sa_store; 1566163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1567163953Srrs struct sockaddr_in *sin; 1568163953Srrs struct sockaddr_in6 *sin6; 1569163953Srrs struct sctp_association *asoc; 1570163953Srrs int chk_length; 1571163953Srrs int init_offset, initack_offset, initack_limit; 1572163953Srrs int retval; 1573163953Srrs int error = 0; 1574163953Srrs uint32_t old_tag; 1575166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1576163953Srrs 1577163953Srrs /* 1578163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1579163953Srrs * INIT should start after the cookie-echo header struct (chunk 1580163953Srrs * header, state cookie header struct) 1581163953Srrs */ 1582163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1583163953Srrs init_cp = (struct sctp_init_chunk *) 1584163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1585163953Srrs (uint8_t *) & init_buf); 1586163953Srrs if (init_cp == NULL) { 1587163953Srrs /* could not pull a INIT chunk in cookie */ 1588169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1589169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1590163953Srrs return (NULL); 1591163953Srrs } 1592163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1593163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1594169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1595163953Srrs return (NULL); 1596163953Srrs } 1597163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1598163953Srrs /* 1599163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1600163953Srrs * INIT-ACK follows the INIT chunk 1601163953Srrs */ 1602163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1603163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1604163953Srrs (uint8_t *) & initack_buf); 1605163953Srrs if (initack_cp == NULL) { 1606163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1607169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1608163953Srrs return (NULL); 1609163953Srrs } 1610163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1611163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1612163953Srrs return (NULL); 1613163953Srrs } 1614163953Srrs /* 1615163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1616163953Srrs * "initack_limit" value. This is because the chk_length field 1617163953Srrs * includes the length of the cookie, but the cookie is omitted when 1618163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1619163953Srrs */ 1620163953Srrs initack_limit = offset + cookie_len; 1621163953Srrs 1622163953Srrs /* 1623163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1624163953Srrs * and popluate 1625163953Srrs */ 1626171531Srrs 1627171531Srrs /* 1628171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 1629171531Srrs * We do this since in effect we only use the p argument when the 1630171531Srrs * socket is unbound and we must do an implicit bind. Since we are 1631171531Srrs * getting a cookie, we cannot be unbound. 1632171531Srrs */ 1633163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1634171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 1635171531Srrs (struct thread *)NULL 1636171531Srrs ); 1637163953Srrs if (stcb == NULL) { 1638163953Srrs struct mbuf *op_err; 1639163953Srrs 1640163953Srrs /* memory problem? */ 1641169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1642169420Srrs "process_cookie_new: no room for another TCB!\n"); 1643163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1644170140Srrs 1645163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1646170181Srrs sh, op_err, vrf_id); 1647163953Srrs return (NULL); 1648163953Srrs } 1649163953Srrs /* get the correct sctp_nets */ 1650169420Srrs if (netp) 1651169420Srrs *netp = sctp_findnet(stcb, init_src); 1652169420Srrs 1653163953Srrs asoc = &stcb->asoc; 1654163953Srrs /* get scope variables out of cookie */ 1655163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1656163953Srrs asoc->site_scope = cookie->site_scope; 1657163953Srrs asoc->local_scope = cookie->local_scope; 1658163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1659163953Srrs 1660163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1661163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1662163953Srrs struct mbuf *op_err; 1663163953Srrs 1664163953Srrs /* 1665163953Srrs * Houston we have a problem. The EP changed while the 1666163953Srrs * cookie was in flight. Only recourse is to abort the 1667163953Srrs * association. 1668163953Srrs */ 1669170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1670163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1671163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1672170181Srrs sh, op_err, vrf_id); 1673171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 1674171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1675170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1676163953Srrs return (NULL); 1677163953Srrs } 1678163953Srrs /* process the INIT-ACK info (my info) */ 1679163953Srrs old_tag = asoc->my_vtag; 1680163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1681163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1682163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1683163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1684163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1685163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1686163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1687163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1688163953Srrs 1689163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1690163953Srrs 1691163953Srrs /* process the INIT info (peer's info) */ 1692169420Srrs if (netp) 1693169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1694169420Srrs else 1695169420Srrs retval = 0; 1696163953Srrs if (retval < 0) { 1697170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1698171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1699170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1700163953Srrs return (NULL); 1701163953Srrs } 1702163953Srrs /* load all addresses */ 1703163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1704163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1705163953Srrs init_src)) { 1706170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1707171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1708170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1709163953Srrs return (NULL); 1710163953Srrs } 1711163953Srrs /* 1712163953Srrs * verify any preceding AUTH chunk that was skipped 1713163953Srrs */ 1714163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1715163953Srrs sctp_auth_get_cookie_params(stcb, m, 1716163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1717163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1718163953Srrs if (auth_skipped) { 1719163953Srrs struct sctp_auth_chunk *auth; 1720163953Srrs 1721163953Srrs auth = (struct sctp_auth_chunk *) 1722166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1723169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1724163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1725169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1726169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1727171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1728171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1729171440Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1730163953Srrs return (NULL); 1731163953Srrs } else { 1732163953Srrs /* remaining chunks checked... good to go */ 1733163953Srrs stcb->asoc.authenticated = 1; 1734163953Srrs } 1735163953Srrs } 1736163953Srrs /* update current state */ 1737169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1738171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1739163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1740163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1741163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1742163953Srrs } 1743165220Srrs sctp_stop_all_cookie_timers(stcb); 1744163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1745163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1746163953Srrs 1747163953Srrs /* 1748163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1749163953Srrs * addresses that need to get added to the peer (eg. addresses 1750163953Srrs * changed while cookie echo in flight). This needs to be done 1751163953Srrs * after we go to the OPEN state to do the correct asconf 1752163953Srrs * processing. else, make sure we have the correct addresses in our 1753163953Srrs * lists 1754163953Srrs */ 1755163953Srrs 1756163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1757163953Srrs /* pull in local_address (our "from" address) */ 1758163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1759163953Srrs /* source addr is IPv4 */ 1760163953Srrs sin = (struct sockaddr_in *)initack_src; 1761163953Srrs memset(sin, 0, sizeof(*sin)); 1762163953Srrs sin->sin_family = AF_INET; 1763163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1764163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1765163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1766163953Srrs /* source addr is IPv6 */ 1767163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1768163953Srrs memset(sin6, 0, sizeof(*sin6)); 1769163953Srrs sin6->sin6_family = AF_INET6; 1770163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1771163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1772163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1773163953Srrs sizeof(sin6->sin6_addr)); 1774163953Srrs } else { 1775170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1776171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1777170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1778163953Srrs return (NULL); 1779163953Srrs } 1780163953Srrs 1781163953Srrs sctp_check_address_list(stcb, m, 1782163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1783163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1784163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1785163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1786163953Srrs 1787163953Srrs 1788163953Srrs /* set up to notify upper layer */ 1789163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1790163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1791163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1792163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1793163953Srrs /* 1794163953Srrs * This is an endpoint that called connect() how it got a 1795163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1796163953Srrs * the INIT was sent, but before it got there.. a complete 1797163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1798163953Srrs * should have went to the other code.. not here.. oh well.. 1799163953Srrs * a bit of protection is worth having.. 1800163953Srrs */ 1801163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1802163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1803163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1804163953Srrs (inp->sctp_socket->so_qlimit)) { 1805163953Srrs /* 1806163953Srrs * We don't want to do anything with this one. Since it is 1807163953Srrs * the listening guy. The timer will get started for 1808163953Srrs * accepted connections in the caller. 1809163953Srrs */ 1810163953Srrs ; 1811163953Srrs } 1812163953Srrs /* since we did not send a HB make sure we don't double things */ 1813169420Srrs if ((netp) && (*netp)) 1814169420Srrs (*netp)->hb_responded = 1; 1815163953Srrs 1816163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1817163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1818163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1819163953Srrs } 1820163953Srrs /* respond with a COOKIE-ACK */ 1821165220Srrs /* calculate the RTT */ 1822171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1823170642Srrs if ((netp) && (*netp)) { 1824169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1825171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1826170642Srrs } 1827163953Srrs sctp_send_cookie_ack(stcb); 1828163953Srrs return (stcb); 1829163953Srrs} 1830163953Srrs 1831163953Srrs 1832163953Srrs/* 1833163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1834163953Srrs * existing (non-NULL) TCB 1835163953Srrs */ 1836163953Srrsstatic struct mbuf * 1837163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1838163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1839163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1840169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1841170181Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id) 1842163953Srrs{ 1843163953Srrs struct sctp_state_cookie *cookie; 1844163953Srrs struct sockaddr_in6 sin6; 1845163953Srrs struct sockaddr_in sin; 1846163953Srrs struct sctp_tcb *l_stcb = *stcb; 1847163953Srrs struct sctp_inpcb *l_inp; 1848163953Srrs struct sockaddr *to; 1849163953Srrs sctp_assoc_t sac_restart_id; 1850163953Srrs struct sctp_pcb *ep; 1851163953Srrs struct mbuf *m_sig; 1852163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1853163953Srrs uint8_t *sig; 1854163953Srrs uint8_t cookie_ok = 0; 1855163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1856163953Srrs unsigned int cookie_len; 1857163953Srrs struct timeval now; 1858163953Srrs struct timeval time_expires; 1859163953Srrs struct sockaddr_storage dest_store; 1860163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1861163953Srrs struct ip *iph; 1862163953Srrs int notification = 0; 1863163953Srrs struct sctp_nets *netl; 1864163953Srrs int had_a_existing_tcb = 0; 1865163953Srrs 1866169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1867169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 1868163953Srrs 1869163953Srrs if (inp_p == NULL) { 1870163953Srrs return (NULL); 1871163953Srrs } 1872163953Srrs /* First get the destination address setup too. */ 1873163953Srrs iph = mtod(m, struct ip *); 1874163953Srrs if (iph->ip_v == IPVERSION) { 1875163953Srrs /* its IPv4 */ 1876170140Srrs struct sockaddr_in *lsin; 1877163953Srrs 1878170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 1879170140Srrs memset(lsin, 0, sizeof(*lsin)); 1880170140Srrs lsin->sin_family = AF_INET; 1881170140Srrs lsin->sin_len = sizeof(*lsin); 1882170140Srrs lsin->sin_port = sh->dest_port; 1883170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 1884165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1885163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1886163953Srrs /* its IPv6 */ 1887163953Srrs struct ip6_hdr *ip6; 1888170140Srrs struct sockaddr_in6 *lsin6; 1889163953Srrs 1890170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 1891170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 1892170140Srrs lsin6->sin6_family = AF_INET6; 1893170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 1894163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1895170140Srrs lsin6->sin6_port = sh->dest_port; 1896170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 1897166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1898163953Srrs } else { 1899163953Srrs return (NULL); 1900163953Srrs } 1901163953Srrs 1902163953Srrs cookie = &cp->cookie; 1903163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1904163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1905163953Srrs 1906163953Srrs if ((cookie->peerport != sh->src_port) && 1907163953Srrs (cookie->myport != sh->dest_port) && 1908163953Srrs (cookie->my_vtag != sh->v_tag)) { 1909163953Srrs /* 1910163953Srrs * invalid ports or bad tag. Note that we always leave the 1911163953Srrs * v_tag in the header in network order and when we stored 1912163953Srrs * it in the my_vtag slot we also left it in network order. 1913169352Srrs * This maintains the match even though it may be in the 1914163953Srrs * opposite byte order of the machine :-> 1915163953Srrs */ 1916163953Srrs return (NULL); 1917163953Srrs } 1918163953Srrs if (cookie_len > size_of_pkt || 1919163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1920163953Srrs sizeof(struct sctp_init_chunk) + 1921163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1922163953Srrs /* cookie too long! or too small */ 1923163953Srrs return (NULL); 1924163953Srrs } 1925163953Srrs /* 1926163953Srrs * split off the signature into its own mbuf (since it should not be 1927163953Srrs * calculated in the sctp_hmac_m() call). 1928163953Srrs */ 1929163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1930163953Srrs if (sig_offset > size_of_pkt) { 1931163953Srrs /* packet not correct size! */ 1932163953Srrs /* XXX this may already be accounted for earlier... */ 1933163953Srrs return (NULL); 1934163953Srrs } 1935163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1936163953Srrs if (m_sig == NULL) { 1937163953Srrs /* out of memory or ?? */ 1938163953Srrs return (NULL); 1939163953Srrs } 1940163953Srrs /* 1941163953Srrs * compute the signature/digest for the cookie 1942163953Srrs */ 1943163953Srrs ep = &(*inp_p)->sctp_ep; 1944163953Srrs l_inp = *inp_p; 1945163953Srrs if (l_stcb) { 1946163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1947163953Srrs } 1948163953Srrs SCTP_INP_RLOCK(l_inp); 1949163953Srrs if (l_stcb) { 1950163953Srrs SCTP_TCB_LOCK(l_stcb); 1951163953Srrs } 1952163953Srrs /* which cookie is it? */ 1953163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1954163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1955163953Srrs /* it's the old cookie */ 1956169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1957163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1958170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1959163953Srrs } else { 1960163953Srrs /* it's the current cookie */ 1961169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1962163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1963170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1964163953Srrs } 1965163953Srrs /* get the signature */ 1966163953Srrs SCTP_INP_RUNLOCK(l_inp); 1967163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1968163953Srrs if (sig == NULL) { 1969163953Srrs /* couldn't find signature */ 1970164085Srrs sctp_m_freem(m_sig); 1971163953Srrs return (NULL); 1972163953Srrs } 1973163953Srrs /* compare the received digest with the computed digest */ 1974163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1975163953Srrs /* try the old cookie? */ 1976163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1977163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1978163953Srrs /* compute digest with old */ 1979169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1980163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1981170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1982163953Srrs /* compare */ 1983163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1984163953Srrs cookie_ok = 1; 1985163953Srrs } 1986163953Srrs } else { 1987163953Srrs cookie_ok = 1; 1988163953Srrs } 1989163953Srrs 1990163953Srrs /* 1991163953Srrs * Now before we continue we must reconstruct our mbuf so that 1992163953Srrs * normal processing of any other chunks will work. 1993163953Srrs */ 1994163953Srrs { 1995163953Srrs struct mbuf *m_at; 1996163953Srrs 1997163953Srrs m_at = m; 1998165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1999165647Srrs m_at = SCTP_BUF_NEXT(m_at); 2000163953Srrs } 2001165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2002163953Srrs } 2003163953Srrs 2004163953Srrs if (cookie_ok == 0) { 2005169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2006169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2007169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2008169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2009163953Srrs return (NULL); 2010163953Srrs } 2011163953Srrs /* 2012163953Srrs * check the cookie timestamps to be sure it's not stale 2013163953Srrs */ 2014169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2015163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2016169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2017163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2018163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2019163953Srrs /* cookie is stale! */ 2020163953Srrs struct mbuf *op_err; 2021163953Srrs struct sctp_stale_cookie_msg *scm; 2022163953Srrs uint32_t tim; 2023163953Srrs 2024163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2025165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2026163953Srrs if (op_err == NULL) { 2027163953Srrs /* FOOBAR */ 2028163953Srrs return (NULL); 2029163953Srrs } 2030163953Srrs /* pre-reserve some space */ 2031165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2032165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2033165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2034163953Srrs 2035163953Srrs /* Set the len */ 2036165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2037163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2038163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2039163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2040163953Srrs (sizeof(uint32_t)))); 2041163953Srrs /* seconds to usec */ 2042163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2043163953Srrs /* add in usec */ 2044163953Srrs if (tim == 0) 2045163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2046163953Srrs scm->time_usec = htonl(tim); 2047169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2048170181Srrs vrf_id); 2049163953Srrs return (NULL); 2050163953Srrs } 2051163953Srrs /* 2052163953Srrs * Now we must see with the lookup address if we have an existing 2053163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2054163953Srrs * and a INIT collided with us and somewhere the peer sent the 2055163953Srrs * cookie on another address besides the single address our assoc 2056163953Srrs * had for him. In this case we will have one of the tie-tags set at 2057163953Srrs * least AND the address field in the cookie can be used to look it 2058163953Srrs * up. 2059163953Srrs */ 2060163953Srrs to = NULL; 2061163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2062163953Srrs memset(&sin6, 0, sizeof(sin6)); 2063163953Srrs sin6.sin6_family = AF_INET6; 2064163953Srrs sin6.sin6_len = sizeof(sin6); 2065163953Srrs sin6.sin6_port = sh->src_port; 2066163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2067163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2068163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2069163953Srrs to = (struct sockaddr *)&sin6; 2070163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2071163953Srrs memset(&sin, 0, sizeof(sin)); 2072163953Srrs sin.sin_family = AF_INET; 2073163953Srrs sin.sin_len = sizeof(sin); 2074163953Srrs sin.sin_port = sh->src_port; 2075163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2076163953Srrs to = (struct sockaddr *)&sin; 2077169420Srrs } else { 2078169420Srrs /* This should not happen */ 2079169420Srrs return (NULL); 2080163953Srrs } 2081163953Srrs if ((*stcb == NULL) && to) { 2082163953Srrs /* Yep, lets check */ 2083163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2084163953Srrs if (*stcb == NULL) { 2085163953Srrs /* 2086163953Srrs * We should have only got back the same inp. If we 2087163953Srrs * got back a different ep we have a problem. The 2088163953Srrs * original findep got back l_inp and now 2089163953Srrs */ 2090163953Srrs if (l_inp != *inp_p) { 2091169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2092163953Srrs } 2093165220Srrs } else { 2094165220Srrs if (*locked_tcb == NULL) { 2095165220Srrs /* 2096165220Srrs * In this case we found the assoc only 2097165220Srrs * after we locked the create lock. This 2098165220Srrs * means we are in a colliding case and we 2099165220Srrs * must make sure that we unlock the tcb if 2100165220Srrs * its one of the cases where we throw away 2101165220Srrs * the incoming packets. 2102165220Srrs */ 2103165220Srrs *locked_tcb = *stcb; 2104165220Srrs 2105165220Srrs /* 2106165220Srrs * We must also increment the inp ref count 2107165220Srrs * since the ref_count flags was set when we 2108165220Srrs * did not find the TCB, now we found it 2109165220Srrs * which reduces the refcount.. we must 2110165220Srrs * raise it back out to balance it all :-) 2111165220Srrs */ 2112165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2113165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2114169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2115165220Srrs (*stcb)->sctp_ep, l_inp); 2116165220Srrs } 2117165220Srrs } 2118163953Srrs } 2119163953Srrs } 2120169420Srrs if (to == NULL) 2121169420Srrs return (NULL); 2122169420Srrs 2123163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2124163953Srrs if (*stcb == NULL) { 2125163953Srrs /* this is the "normal" case... get a new TCB */ 2126163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2127163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2128170181Srrs auth_skipped, auth_offset, auth_len, vrf_id); 2129163953Srrs } else { 2130163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2131163953Srrs had_a_existing_tcb = 1; 2132163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2133170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2134170181Srrs ¬ification, &sac_restart_id, vrf_id); 2135163953Srrs } 2136163953Srrs 2137163953Srrs if (*stcb == NULL) { 2138163953Srrs /* still no TCB... must be bad cookie-echo */ 2139163953Srrs return (NULL); 2140163953Srrs } 2141163953Srrs /* 2142163953Srrs * Ok, we built an association so confirm the address we sent the 2143163953Srrs * INIT-ACK to. 2144163953Srrs */ 2145163953Srrs netl = sctp_findnet(*stcb, to); 2146163953Srrs /* 2147163953Srrs * This code should in theory NOT run but 2148163953Srrs */ 2149163953Srrs if (netl == NULL) { 2150163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2151163953Srrs int ret; 2152163953Srrs 2153165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2154165220Srrs SCTP_IN_COOKIE_PROC); 2155163953Srrs netl = sctp_findnet(*stcb, to); 2156163953Srrs } 2157163953Srrs if (netl) { 2158163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2159163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2160169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2161163953Srrs netl); 2162163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2163163953Srrs (*stcb), 0, (void *)netl); 2164163953Srrs } 2165163953Srrs } 2166163953Srrs if (*stcb) { 2167163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2168163953Srrs *stcb, NULL); 2169163953Srrs } 2170163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2171163953Srrs if (!had_a_existing_tcb || 2172163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2173163953Srrs /* 2174163953Srrs * If we have a NEW cookie or the connect never 2175163953Srrs * reached the connected state during collision we 2176163953Srrs * must do the TCP accept thing. 2177163953Srrs */ 2178163953Srrs struct socket *so, *oso; 2179163953Srrs struct sctp_inpcb *inp; 2180163953Srrs 2181163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2182163953Srrs /* 2183163953Srrs * For a restart we will keep the same 2184163953Srrs * socket, no need to do anything. I THINK!! 2185163953Srrs */ 2186163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2187163953Srrs return (m); 2188163953Srrs } 2189163953Srrs oso = (*inp_p)->sctp_socket; 2190171858Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2191163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2192166086Srrs so = sonewconn(oso, 0 2193163953Srrs ); 2194163953Srrs SCTP_TCB_LOCK((*stcb)); 2195171858Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2196171858Srrs 2197163953Srrs if (so == NULL) { 2198163953Srrs struct mbuf *op_err; 2199163953Srrs 2200163953Srrs /* Too many sockets */ 2201169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2202163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2203163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2204170181Srrs sh, op_err, vrf_id); 2205171943Srrs (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2206163953Srrs return (NULL); 2207163953Srrs } 2208163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2209166086Srrs SCTP_INP_INCR_REF(inp); 2210166086Srrs /* 2211166086Srrs * We add the unbound flag here so that if we get an 2212166086Srrs * soabort() before we get the move_pcb done, we 2213166086Srrs * will properly cleanup. 2214166086Srrs */ 2215163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2216163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2217163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2218166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2219163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2220163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2221163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2222163953Srrs inp->sctp_socket = so; 2223163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2224163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2225163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2226163953Srrs inp->inp_starting_point_for_iterator = NULL; 2227163953Srrs /* 2228163953Srrs * copy in the authentication parameters from the 2229163953Srrs * original endpoint 2230163953Srrs */ 2231163953Srrs if (inp->sctp_ep.local_hmacs) 2232163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2233163953Srrs inp->sctp_ep.local_hmacs = 2234163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2235163953Srrs if (inp->sctp_ep.local_auth_chunks) 2236163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2237163953Srrs inp->sctp_ep.local_auth_chunks = 2238163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2239163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2240163953Srrs &inp->sctp_ep.shared_keys); 2241163953Srrs 2242163953Srrs /* 2243163953Srrs * Now we must move it from one hash table to 2244163953Srrs * another and get the tcb in the right place. 2245163953Srrs */ 2246163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2247170056Srrs 2248170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2249170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2250170056Srrs 2251169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2252170056Srrs SCTP_TCB_LOCK((*stcb)); 2253170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2254163953Srrs 2255170056Srrs 2256166086Srrs /* 2257166086Srrs * now we must check to see if we were aborted while 2258166086Srrs * the move was going on and the lock/unlock 2259166086Srrs * happened. 2260166086Srrs */ 2261166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2262166086Srrs /* 2263166086Srrs * yep it was, we leave the assoc attached 2264166086Srrs * to the socket since the sctp_inpcb_free() 2265166086Srrs * call will send an abort for us. 2266166086Srrs */ 2267166086Srrs SCTP_INP_DECR_REF(inp); 2268166086Srrs return (NULL); 2269166086Srrs } 2270166086Srrs SCTP_INP_DECR_REF(inp); 2271163953Srrs /* Switch over to the new guy */ 2272163953Srrs *inp_p = inp; 2273166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2274163953Srrs 2275166086Srrs /* 2276166086Srrs * Pull it from the incomplete queue and wake the 2277166086Srrs * guy 2278166086Srrs */ 2279166086Srrs soisconnected(so); 2280163953Srrs return (m); 2281163953Srrs } 2282163953Srrs } 2283163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2284163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2285163953Srrs } 2286163953Srrs return (m); 2287163953Srrs} 2288163953Srrs 2289163953Srrsstatic void 2290163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2291163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2292163953Srrs{ 2293163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2294163953Srrs struct sctp_association *asoc; 2295163953Srrs 2296169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2297169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2298163953Srrs if (stcb == NULL) 2299163953Srrs return; 2300163953Srrs 2301163953Srrs asoc = &stcb->asoc; 2302163953Srrs 2303163953Srrs sctp_stop_all_cookie_timers(stcb); 2304163953Srrs /* process according to association state */ 2305163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2306163953Srrs /* state change only needed when I am in right state */ 2307169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2308171943Srrs SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2309163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2310163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2311163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2312163953Srrs 2313163953Srrs } 2314163953Srrs /* update RTO */ 2315163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2316163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2317163953Srrs if (asoc->overall_error_count == 0) { 2318163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2319171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2320163953Srrs } 2321169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2322163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2323163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2324163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2325163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2326163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2327163953Srrs } 2328163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2329163953Srrs stcb, net); 2330163953Srrs /* 2331163953Srrs * since we did not send a HB make sure we don't double 2332163953Srrs * things 2333163953Srrs */ 2334163953Srrs net->hb_responded = 1; 2335163953Srrs 2336163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2337163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2338163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2339163953Srrs stcb->sctp_ep, stcb, NULL); 2340163953Srrs } 2341163953Srrs /* 2342171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2343171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2344171572Srrs * in flight) 2345163953Srrs */ 2346163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2347163953Srrs (stcb->asoc.peer_supports_asconf) && 2348163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2349171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2350163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2351163953Srrs stcb->sctp_ep, stcb, 2352163953Srrs stcb->asoc.primary_destination); 2353171572Srrs#else 2354171572Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination); 2355171572Srrs#endif 2356163953Srrs } 2357163953Srrs } 2358163953Srrs /* Toss the cookie if I can */ 2359163953Srrs sctp_toss_old_cookies(stcb, asoc); 2360163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2361163953Srrs /* Restart the timer if we have pending data */ 2362163953Srrs struct sctp_tmit_chunk *chk; 2363163953Srrs 2364163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2365163953Srrs if (chk) { 2366163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2367163953Srrs stcb, chk->whoTo); 2368163953Srrs } 2369163953Srrs } 2370163953Srrs} 2371163953Srrs 2372163953Srrsstatic void 2373163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2374163953Srrs struct sctp_tcb *stcb) 2375163953Srrs{ 2376163953Srrs struct sctp_nets *net; 2377163953Srrs struct sctp_tmit_chunk *lchk; 2378163953Srrs uint32_t tsn; 2379163953Srrs 2380163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2381163953Srrs return; 2382163953Srrs } 2383163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2384163953Srrs tsn = ntohl(cp->tsn); 2385163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2386163953Srrs /* Also we make sure we disable the nonce_wait */ 2387163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2388163953Srrs if (lchk == NULL) { 2389163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2390163953Srrs } else { 2391163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2392163953Srrs } 2393163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2394163953Srrs stcb->asoc.nonce_sum_check = 0; 2395163953Srrs 2396163953Srrs /* Find where it was sent, if possible */ 2397163953Srrs net = NULL; 2398163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2399163953Srrs while (lchk) { 2400163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2401163953Srrs net = lchk->whoTo; 2402163953Srrs break; 2403163953Srrs } 2404163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2405163953Srrs break; 2406163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2407163953Srrs } 2408163953Srrs if (net == NULL) 2409163953Srrs /* default is we use the primary */ 2410163953Srrs net = stcb->asoc.primary_destination; 2411163953Srrs 2412163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2413163953Srrs /* 2414171440Srrs * JRS - Use the congestion control given in the pluggable 2415171440Srrs * CC module 2416171440Srrs */ 2417171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2418171440Srrs /* 2419163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2420163953Srrs * the next sending seq i.e. the resync_tsn. 2421163953Srrs */ 2422163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2423163953Srrs } 2424163953Srrs /* 2425163953Srrs * We always send a CWR this way if our previous one was lost our 2426163953Srrs * peer will get an update, or if it is not time again to reduce we 2427163953Srrs * still get the cwr to the peer. 2428163953Srrs */ 2429163953Srrs sctp_send_cwr(stcb, net, tsn); 2430163953Srrs} 2431163953Srrs 2432163953Srrsstatic void 2433163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2434163953Srrs{ 2435163953Srrs /* 2436163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2437163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2438163953Srrs * If so remove it. 2439163953Srrs */ 2440163953Srrs struct sctp_tmit_chunk *chk; 2441163953Srrs struct sctp_ecne_chunk *ecne; 2442163953Srrs 2443163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2444163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2445163953Srrs continue; 2446163953Srrs } 2447163953Srrs /* 2448163953Srrs * Look for and remove if it is the right TSN. Since there 2449163953Srrs * is only ONE ECNE on the control queue at any one time we 2450163953Srrs * don't need to worry about more than one! 2451163953Srrs */ 2452163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2453163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2454163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2455163953Srrs /* this covers this ECNE, we can remove it */ 2456163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2457163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2458163953Srrs sctp_next); 2459163953Srrs if (chk->data) { 2460163953Srrs sctp_m_freem(chk->data); 2461163953Srrs chk->data = NULL; 2462163953Srrs } 2463163953Srrs stcb->asoc.ctrl_queue_cnt--; 2464163953Srrs sctp_free_a_chunk(stcb, chk); 2465163953Srrs break; 2466163953Srrs } 2467163953Srrs } 2468163953Srrs} 2469163953Srrs 2470163953Srrsstatic void 2471163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2472163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2473163953Srrs{ 2474163953Srrs struct sctp_association *asoc; 2475163953Srrs 2476169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2477169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2478163953Srrs if (stcb == NULL) 2479163953Srrs return; 2480163953Srrs 2481163953Srrs asoc = &stcb->asoc; 2482163953Srrs /* process according to association state */ 2483163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2484163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2485163953Srrs SCTP_TCB_UNLOCK(stcb); 2486163953Srrs return; 2487163953Srrs } 2488163953Srrs /* notify upper layer protocol */ 2489163953Srrs if (stcb->sctp_socket) { 2490163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2491163953Srrs /* are the queues empty? they should be */ 2492163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2493163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2494163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2495164205Srrs sctp_report_all_outbound(stcb, 0); 2496163953Srrs } 2497163953Srrs } 2498163953Srrs /* stop the timer */ 2499171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2500163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2501163953Srrs /* free the TCB */ 2502171943Srrs (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2503163953Srrs return; 2504163953Srrs} 2505163953Srrs 2506163953Srrsstatic int 2507163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2508163953Srrs struct sctp_nets *net, uint8_t flg) 2509163953Srrs{ 2510163953Srrs switch (desc->chunk_type) { 2511163953Srrs case SCTP_DATA: 2512163953Srrs /* find the tsn to resend (possibly */ 2513163953Srrs { 2514163953Srrs uint32_t tsn; 2515163953Srrs struct sctp_tmit_chunk *tp1; 2516163953Srrs 2517163953Srrs tsn = ntohl(desc->tsn_ifany); 2518163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2519163953Srrs while (tp1) { 2520163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2521163953Srrs /* found it */ 2522163953Srrs break; 2523163953Srrs } 2524163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2525163953Srrs MAX_TSN)) { 2526163953Srrs /* not found */ 2527163953Srrs tp1 = NULL; 2528163953Srrs break; 2529163953Srrs } 2530163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2531163953Srrs } 2532163953Srrs if (tp1 == NULL) { 2533163953Srrs /* 2534163953Srrs * Do it the other way , aka without paying 2535163953Srrs * attention to queue seq order. 2536163953Srrs */ 2537163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2538163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2539163953Srrs while (tp1) { 2540163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2541163953Srrs /* found it */ 2542163953Srrs break; 2543163953Srrs } 2544163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2545163953Srrs } 2546163953Srrs } 2547163953Srrs if (tp1 == NULL) { 2548163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2549163953Srrs } 2550163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2551163953Srrs uint8_t *ddp; 2552163953Srrs 2553163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2554163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2555163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2556163953Srrs return (0); 2557163953Srrs } 2558163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2559163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2560163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2561163953Srrs return (0); 2562163953Srrs } 2563163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2564163953Srrs sizeof(struct sctp_data_chunk)); 2565163953Srrs { 2566163953Srrs unsigned int iii; 2567163953Srrs 2568163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2569163953Srrs iii++) { 2570163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2571163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2572163953Srrs return (-1); 2573163953Srrs } 2574163953Srrs } 2575163953Srrs } 2576163953Srrs /* 2577163953Srrs * We zero out the nonce so resync not 2578163953Srrs * needed 2579163953Srrs */ 2580163953Srrs tp1->rec.data.ect_nonce = 0; 2581163953Srrs 2582163953Srrs if (tp1->do_rtt) { 2583163953Srrs /* 2584163953Srrs * this guy had a RTO calculation 2585163953Srrs * pending on it, cancel it 2586163953Srrs */ 2587163953Srrs tp1->do_rtt = 0; 2588163953Srrs } 2589163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2590163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2591163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2592163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2593163953Srrs /* 2594163953Srrs * mark it as if we were doing a FR, since 2595163953Srrs * we will be getting gap ack reports behind 2596163953Srrs * the info from the router. 2597163953Srrs */ 2598163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2599163953Srrs /* 2600163953Srrs * mark the tsn with what sequences can 2601163953Srrs * cause a new FR. 2602163953Srrs */ 2603163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2604163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2605163953Srrs } else { 2606163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2607163953Srrs } 2608163953Srrs 2609163953Srrs /* restart the timer */ 2610163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2611171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2612163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2613163953Srrs stcb, tp1->whoTo); 2614163953Srrs 2615163953Srrs /* fix counts and things */ 2616170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 2617170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2618170744Srrs tp1->whoTo->flight_size, 2619170744Srrs tp1->book_size, 2620170744Srrs (uintptr_t) stcb, 2621170744Srrs tp1->rec.data.TSN_seq); 2622170744Srrs } 2623168709Srrs sctp_flight_size_decrease(tp1); 2624168709Srrs sctp_total_flight_decrease(stcb, tp1); 2625163953Srrs } { 2626163953Srrs /* audit code */ 2627163953Srrs unsigned int audit; 2628163953Srrs 2629163953Srrs audit = 0; 2630163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2631163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2632163953Srrs audit++; 2633163953Srrs } 2634163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2635163953Srrs sctp_next) { 2636163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2637163953Srrs audit++; 2638163953Srrs } 2639163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2640169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2641163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2642163953Srrs#ifndef SCTP_AUDITING_ENABLED 2643163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2644163953Srrs#endif 2645163953Srrs } 2646163953Srrs } 2647163953Srrs } 2648163953Srrs break; 2649163953Srrs case SCTP_ASCONF: 2650163953Srrs { 2651163953Srrs struct sctp_tmit_chunk *asconf; 2652163953Srrs 2653163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2654163953Srrs sctp_next) { 2655163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2656163953Srrs break; 2657163953Srrs } 2658163953Srrs } 2659163953Srrs if (asconf) { 2660163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2661163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2662163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2663163953Srrs asconf->snd_count--; 2664163953Srrs } 2665163953Srrs } 2666163953Srrs break; 2667163953Srrs case SCTP_INITIATION: 2668163953Srrs /* resend the INIT */ 2669163953Srrs stcb->asoc.dropped_special_cnt++; 2670163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2671163953Srrs /* 2672163953Srrs * If we can get it in, in a few attempts we do 2673163953Srrs * this, otherwise we let the timer fire. 2674163953Srrs */ 2675163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2676171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2677163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2678163953Srrs } 2679163953Srrs break; 2680163953Srrs case SCTP_SELECTIVE_ACK: 2681163953Srrs /* resend the sack */ 2682163953Srrs sctp_send_sack(stcb); 2683163953Srrs break; 2684163953Srrs case SCTP_HEARTBEAT_REQUEST: 2685163953Srrs /* resend a demand HB */ 2686171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 2687171440Srrs /* 2688171440Srrs * Only retransmit if we KNOW we wont destroy the 2689171440Srrs * tcb 2690171440Srrs */ 2691171440Srrs (void)sctp_send_hb(stcb, 1, net); 2692171440Srrs } 2693163953Srrs break; 2694163953Srrs case SCTP_SHUTDOWN: 2695163953Srrs sctp_send_shutdown(stcb, net); 2696163953Srrs break; 2697163953Srrs case SCTP_SHUTDOWN_ACK: 2698163953Srrs sctp_send_shutdown_ack(stcb, net); 2699163953Srrs break; 2700163953Srrs case SCTP_COOKIE_ECHO: 2701163953Srrs { 2702163953Srrs struct sctp_tmit_chunk *cookie; 2703163953Srrs 2704163953Srrs cookie = NULL; 2705163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2706163953Srrs sctp_next) { 2707163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2708163953Srrs break; 2709163953Srrs } 2710163953Srrs } 2711163953Srrs if (cookie) { 2712163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2713163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2714163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2715163953Srrs sctp_stop_all_cookie_timers(stcb); 2716163953Srrs } 2717163953Srrs } 2718163953Srrs break; 2719163953Srrs case SCTP_COOKIE_ACK: 2720163953Srrs sctp_send_cookie_ack(stcb); 2721163953Srrs break; 2722163953Srrs case SCTP_ASCONF_ACK: 2723163953Srrs /* resend last asconf ack */ 2724163953Srrs sctp_send_asconf_ack(stcb, 1); 2725163953Srrs break; 2726163953Srrs case SCTP_FORWARD_CUM_TSN: 2727163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2728163953Srrs break; 2729163953Srrs /* can't do anything with these */ 2730163953Srrs case SCTP_PACKET_DROPPED: 2731163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2732163953Srrs case SCTP_HEARTBEAT_ACK: 2733163953Srrs case SCTP_ABORT_ASSOCIATION: 2734163953Srrs case SCTP_OPERATION_ERROR: 2735163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2736163953Srrs case SCTP_ECN_ECHO: 2737163953Srrs case SCTP_ECN_CWR: 2738163953Srrs default: 2739163953Srrs break; 2740163953Srrs } 2741163953Srrs return (0); 2742163953Srrs} 2743163953Srrs 2744163953Srrsvoid 2745163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2746163953Srrs{ 2747163953Srrs int i; 2748163953Srrs uint16_t temp; 2749163953Srrs 2750163953Srrs /* 2751163953Srrs * We set things to 0xffff since this is the last delivered sequence 2752163953Srrs * and we will be sending in 0 after the reset. 2753163953Srrs */ 2754163953Srrs 2755163953Srrs if (number_entries) { 2756163953Srrs for (i = 0; i < number_entries; i++) { 2757163953Srrs temp = ntohs(list[i]); 2758163953Srrs if (temp >= stcb->asoc.streamincnt) { 2759163953Srrs continue; 2760163953Srrs } 2761163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2762163953Srrs } 2763163953Srrs } else { 2764163953Srrs list = NULL; 2765163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2766163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2767163953Srrs } 2768163953Srrs } 2769163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2770163953Srrs} 2771163953Srrs 2772163953Srrsstatic void 2773163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2774163953Srrs{ 2775163953Srrs int i; 2776163953Srrs 2777163953Srrs if (number_entries == 0) { 2778163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2779163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2780163953Srrs } 2781163953Srrs } else if (number_entries) { 2782163953Srrs for (i = 0; i < number_entries; i++) { 2783163953Srrs uint16_t temp; 2784163953Srrs 2785163953Srrs temp = ntohs(list[i]); 2786163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2787163953Srrs /* no such stream */ 2788163953Srrs continue; 2789163953Srrs } 2790163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2791163953Srrs } 2792163953Srrs } 2793163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2794163953Srrs} 2795163953Srrs 2796163953Srrs 2797163953Srrsstruct sctp_stream_reset_out_request * 2798163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2799163953Srrs{ 2800163953Srrs struct sctp_association *asoc; 2801163953Srrs struct sctp_stream_reset_out_req *req; 2802163953Srrs struct sctp_stream_reset_out_request *r; 2803163953Srrs struct sctp_tmit_chunk *chk; 2804163953Srrs int len, clen; 2805163953Srrs 2806163953Srrs asoc = &stcb->asoc; 2807163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2808169208Srrs asoc->stream_reset_outstanding = 0; 2809163953Srrs return (NULL); 2810163953Srrs } 2811163953Srrs if (stcb->asoc.str_reset == NULL) { 2812169208Srrs asoc->stream_reset_outstanding = 0; 2813163953Srrs return (NULL); 2814163953Srrs } 2815163953Srrs chk = stcb->asoc.str_reset; 2816163953Srrs if (chk->data == NULL) { 2817163953Srrs return (NULL); 2818163953Srrs } 2819163953Srrs if (bchk) { 2820163953Srrs /* he wants a copy of the chk pointer */ 2821163953Srrs *bchk = chk; 2822163953Srrs } 2823163953Srrs clen = chk->send_size; 2824163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2825163953Srrs r = &req->sr_req; 2826163953Srrs if (ntohl(r->request_seq) == seq) { 2827163953Srrs /* found it */ 2828163953Srrs return (r); 2829163953Srrs } 2830163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2831163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2832163953Srrs /* move to the next one, there can only be a max of two */ 2833163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2834163953Srrs if (ntohl(r->request_seq) == seq) { 2835163953Srrs return (r); 2836163953Srrs } 2837163953Srrs } 2838163953Srrs /* that seq is not here */ 2839163953Srrs return (NULL); 2840163953Srrs} 2841163953Srrs 2842163953Srrsstatic void 2843163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2844163953Srrs{ 2845163953Srrs struct sctp_association *asoc; 2846163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2847163953Srrs 2848163953Srrs if (stcb->asoc.str_reset == NULL) { 2849163953Srrs return; 2850163953Srrs } 2851166675Srrs asoc = &stcb->asoc; 2852166675Srrs 2853171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 2854163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2855163953Srrs chk, 2856163953Srrs sctp_next); 2857163953Srrs if (chk->data) { 2858163953Srrs sctp_m_freem(chk->data); 2859163953Srrs chk->data = NULL; 2860163953Srrs } 2861163953Srrs asoc->ctrl_queue_cnt--; 2862163953Srrs sctp_free_a_chunk(stcb, chk); 2863163953Srrs stcb->asoc.str_reset = NULL; 2864163953Srrs} 2865163953Srrs 2866163953Srrs 2867163953Srrsstatic int 2868163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2869163953Srrs uint32_t seq, uint32_t action, 2870164139Srrs struct sctp_stream_reset_response *respin) 2871163953Srrs{ 2872163953Srrs uint16_t type; 2873163953Srrs int lparm_len; 2874163953Srrs struct sctp_association *asoc = &stcb->asoc; 2875163953Srrs struct sctp_tmit_chunk *chk; 2876163953Srrs struct sctp_stream_reset_out_request *srparam; 2877163953Srrs int number_entries; 2878163953Srrs 2879163953Srrs if (asoc->stream_reset_outstanding == 0) { 2880163953Srrs /* duplicate */ 2881163953Srrs return (0); 2882163953Srrs } 2883163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2884163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2885163953Srrs if (srparam) { 2886163953Srrs stcb->asoc.str_reset_seq_out++; 2887163953Srrs type = ntohs(srparam->ph.param_type); 2888163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2889163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2890164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2891163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2892163953Srrs if (asoc->stream_reset_outstanding) 2893163953Srrs asoc->stream_reset_outstanding--; 2894163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2895163953Srrs /* do it */ 2896163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2897163953Srrs } else { 2898163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2899163953Srrs } 2900163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2901163953Srrs /* Answered my request */ 2902164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2903163953Srrs if (asoc->stream_reset_outstanding) 2904163953Srrs asoc->stream_reset_outstanding--; 2905163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2906163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2907163953Srrs } 2908163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2909163953Srrs /** 2910163953Srrs * a) Adopt the new in tsn. 2911163953Srrs * b) reset the map 2912163953Srrs * c) Adopt the new out-tsn 2913163953Srrs */ 2914163953Srrs struct sctp_stream_reset_response_tsn *resp; 2915163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2916163953Srrs int abort_flag = 0; 2917163953Srrs 2918163953Srrs if (respin == NULL) { 2919163953Srrs /* huh ? */ 2920163953Srrs return (0); 2921163953Srrs } 2922163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2923163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2924163953Srrs asoc->stream_reset_outstanding--; 2925163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2926163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2927163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2928170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 2929163953Srrs if (abort_flag) { 2930163953Srrs return (1); 2931163953Srrs } 2932163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2933163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2934163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2935163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2936163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2937163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2938163953Srrs 2939163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2940163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2941163953Srrs 2942163953Srrs } 2943163953Srrs } 2944163953Srrs /* get rid of the request and get the request flags */ 2945163953Srrs if (asoc->stream_reset_outstanding == 0) { 2946163953Srrs sctp_clean_up_stream_reset(stcb); 2947163953Srrs } 2948163953Srrs } 2949163953Srrs } 2950163953Srrs return (0); 2951163953Srrs} 2952163953Srrs 2953163953Srrsstatic void 2954163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2955163953Srrs struct sctp_tmit_chunk *chk, 2956170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 2957163953Srrs{ 2958163953Srrs uint32_t seq; 2959163953Srrs int len, i; 2960163953Srrs int number_entries; 2961163953Srrs uint16_t temp; 2962163953Srrs 2963163953Srrs /* 2964163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2965163953Srrs * seq_in is right. 2966163953Srrs */ 2967163953Srrs struct sctp_association *asoc = &stcb->asoc; 2968163953Srrs 2969163953Srrs seq = ntohl(req->request_seq); 2970163953Srrs if (asoc->str_reset_seq_in == seq) { 2971170992Srrs if (trunc) { 2972170992Srrs /* Can't do it, since they exceeded our buffer size */ 2973170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2974170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 2975170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2976170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2977163953Srrs len = ntohs(req->ph.param_length); 2978163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2979163953Srrs for (i = 0; i < number_entries; i++) { 2980163953Srrs temp = ntohs(req->list_of_streams[i]); 2981163953Srrs req->list_of_streams[i] = temp; 2982163953Srrs } 2983163953Srrs /* move the reset action back one */ 2984163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2985163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2986163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2987163953Srrs asoc->str_reset_seq_out, 2988163953Srrs seq, (asoc->sending_seq - 1)); 2989163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2990163953Srrs asoc->str_reset = chk; 2991163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 2992163953Srrs stcb->asoc.stream_reset_outstanding++; 2993163953Srrs } else { 2994163953Srrs /* Can't do it, since we have sent one out */ 2995163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2996163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 2997163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2998163953Srrs } 2999163953Srrs asoc->str_reset_seq_in++; 3000163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3001163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3002163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3003163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3004163953Srrs } else { 3005163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3006163953Srrs } 3007163953Srrs} 3008163953Srrs 3009163953Srrsstatic int 3010163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3011163953Srrs struct sctp_tmit_chunk *chk, 3012163953Srrs struct sctp_stream_reset_tsn_request *req) 3013163953Srrs{ 3014163953Srrs /* reset all in and out and update the tsn */ 3015163953Srrs /* 3016163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3017163953Srrs * and set cum-ack to it. Also process this selected number as a 3018163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3019163953Srrs */ 3020163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3021163953Srrs struct sctp_association *asoc = &stcb->asoc; 3022163953Srrs int abort_flag = 0; 3023163953Srrs uint32_t seq; 3024163953Srrs 3025163953Srrs seq = ntohl(req->request_seq); 3026163953Srrs if (asoc->str_reset_seq_in == seq) { 3027163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3028163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3029163953Srrs fwdtsn.ch.chunk_flags = 0; 3030163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3031170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3032163953Srrs if (abort_flag) { 3033163953Srrs return (1); 3034163953Srrs } 3035163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3036163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3037163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3038163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3039163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3040163953Srrs /* save off historical data for retrans */ 3041163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3042163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3043163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3044163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3045163953Srrs 3046163953Srrs sctp_add_stream_reset_result_tsn(chk, 3047163953Srrs ntohl(req->request_seq), 3048163953Srrs SCTP_STREAM_RESET_PERFORMED, 3049163953Srrs stcb->asoc.sending_seq, 3050163953Srrs stcb->asoc.mapping_array_base_tsn); 3051163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3052163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3053163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3054163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3055163953Srrs 3056163953Srrs asoc->str_reset_seq_in++; 3057163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3058163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3059163953Srrs stcb->asoc.last_sending_seq[0], 3060163953Srrs stcb->asoc.last_base_tsnsent[0] 3061163953Srrs ); 3062163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3063163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3064163953Srrs stcb->asoc.last_sending_seq[1], 3065163953Srrs stcb->asoc.last_base_tsnsent[1] 3066163953Srrs ); 3067163953Srrs } else { 3068163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3069163953Srrs } 3070163953Srrs return (0); 3071163953Srrs} 3072163953Srrs 3073163953Srrsstatic void 3074163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3075163953Srrs struct sctp_tmit_chunk *chk, 3076170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3077163953Srrs{ 3078163953Srrs uint32_t seq, tsn; 3079163953Srrs int number_entries, len; 3080163953Srrs struct sctp_association *asoc = &stcb->asoc; 3081163953Srrs 3082163953Srrs seq = ntohl(req->request_seq); 3083163953Srrs 3084163953Srrs /* now if its not a duplicate we process it */ 3085163953Srrs if (asoc->str_reset_seq_in == seq) { 3086163953Srrs len = ntohs(req->ph.param_length); 3087163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3088163953Srrs /* 3089163953Srrs * the sender is resetting, handle the list issue.. we must 3090163953Srrs * a) verify if we can do the reset, if so no problem b) If 3091163953Srrs * we can't do the reset we must copy the request. c) queue 3092163953Srrs * it, and setup the data in processor to trigger it off 3093163953Srrs * when needed and dequeue all the queued data. 3094163953Srrs */ 3095163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3096163953Srrs 3097163953Srrs /* move the reset action back one */ 3098163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3099170992Srrs if (trunc) { 3100170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3101170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3102170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3103163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3104163953Srrs /* we can do it now */ 3105163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3106163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3107163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3108163953Srrs } else { 3109163953Srrs /* 3110163953Srrs * we must queue it up and thus wait for the TSN's 3111163953Srrs * to arrive that are at or before tsn 3112163953Srrs */ 3113163953Srrs struct sctp_stream_reset_list *liste; 3114163953Srrs int siz; 3115163953Srrs 3116163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3117163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3118170091Srrs siz, SCTP_M_STRESET); 3119163953Srrs if (liste == NULL) { 3120163953Srrs /* gak out of memory */ 3121163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3122163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3123163953Srrs return; 3124163953Srrs } 3125163953Srrs liste->tsn = tsn; 3126163953Srrs liste->number_entries = number_entries; 3127163953Srrs memcpy(&liste->req, req, 3128163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3129163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3130163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3131163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3132163953Srrs } 3133163953Srrs asoc->str_reset_seq_in++; 3134163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3135163953Srrs /* 3136163953Srrs * one seq back, just echo back last action since my 3137163953Srrs * response was lost. 3138163953Srrs */ 3139163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3140163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3141163953Srrs /* 3142163953Srrs * two seq back, just echo back last action since my 3143163953Srrs * response was lost. 3144163953Srrs */ 3145163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3146163953Srrs } else { 3147163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3148163953Srrs } 3149163953Srrs} 3150163953Srrs 3151170992Srrs#ifdef __GNUC__ 3152170992Srrs__attribute__((noinline)) 3153170992Srrs#endif 3154170992Srrs static int 3155170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3156170992Srrs struct sctp_stream_reset_out_req *sr_req) 3157163953Srrs{ 3158163953Srrs int chk_length, param_len, ptype; 3159170992Srrs struct sctp_paramhdr pstore; 3160170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3161170992Srrs 3162163953Srrs uint32_t seq; 3163163953Srrs int num_req = 0; 3164170992Srrs int trunc = 0; 3165163953Srrs struct sctp_tmit_chunk *chk; 3166163953Srrs struct sctp_chunkhdr *ch; 3167163953Srrs struct sctp_paramhdr *ph; 3168166675Srrs int ret_code = 0; 3169166675Srrs int num_param = 0; 3170163953Srrs 3171163953Srrs /* now it may be a reset or a reset-response */ 3172163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3173163953Srrs 3174163953Srrs /* setup for adding the response */ 3175163953Srrs sctp_alloc_a_chunk(stcb, chk); 3176163953Srrs if (chk == NULL) { 3177163953Srrs return (ret_code); 3178163953Srrs } 3179163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3180169208Srrs chk->rec.chunk_id.can_take_data = 0; 3181163953Srrs chk->asoc = &stcb->asoc; 3182163953Srrs chk->no_fr_allowed = 0; 3183163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3184166023Srrs chk->book_size_scale = 0; 3185165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3186163953Srrs if (chk->data == NULL) { 3187163953Srrsstrres_nochunk: 3188163953Srrs if (chk->data) { 3189163953Srrs sctp_m_freem(chk->data); 3190163953Srrs chk->data = NULL; 3191163953Srrs } 3192163953Srrs sctp_free_a_chunk(stcb, chk); 3193163953Srrs return (ret_code); 3194163953Srrs } 3195165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3196163953Srrs 3197163953Srrs /* setup chunk parameters */ 3198163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3199163953Srrs chk->snd_count = 0; 3200163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3201163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3202163953Srrs 3203163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3204163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3205163953Srrs ch->chunk_flags = 0; 3206163953Srrs ch->chunk_length = htons(chk->send_size); 3207165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3208170992Srrs offset += sizeof(struct sctp_chunkhdr); 3209163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3210170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3211170992Srrs if (ph == NULL) 3212170992Srrs break; 3213163953Srrs param_len = ntohs(ph->param_length); 3214163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3215163953Srrs /* bad param */ 3216163953Srrs break; 3217163953Srrs } 3218170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3219170992Srrs (uint8_t *) & cstore); 3220163953Srrs ptype = ntohs(ph->param_type); 3221163953Srrs num_param++; 3222170994Srrs if (param_len > (int)sizeof(cstore)) { 3223170992Srrs trunc = 1; 3224170992Srrs } else { 3225170992Srrs trunc = 0; 3226170992Srrs } 3227170992Srrs 3228163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3229163953Srrs /* hit the max of parameters already sorry.. */ 3230163953Srrs break; 3231163953Srrs } 3232163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3233163953Srrs struct sctp_stream_reset_out_request *req_out; 3234163953Srrs 3235163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3236163953Srrs num_req++; 3237163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3238163953Srrs seq = ntohl(req_out->response_seq); 3239163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3240163953Srrs /* implicit ack */ 3241169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3242163953Srrs } 3243163953Srrs } 3244170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3245163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3246163953Srrs struct sctp_stream_reset_in_request *req_in; 3247163953Srrs 3248163953Srrs num_req++; 3249170992Srrs 3250163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3251170992Srrs 3252170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3253163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3254163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3255163953Srrs 3256163953Srrs num_req++; 3257163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3258170992Srrs 3259163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3260163953Srrs ret_code = 1; 3261163953Srrs goto strres_nochunk; 3262163953Srrs } 3263163953Srrs /* no more */ 3264163953Srrs break; 3265163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3266163953Srrs struct sctp_stream_reset_response *resp; 3267163953Srrs uint32_t result; 3268163953Srrs 3269163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3270163953Srrs seq = ntohl(resp->response_seq); 3271163953Srrs result = ntohl(resp->result); 3272163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3273163953Srrs ret_code = 1; 3274163953Srrs goto strres_nochunk; 3275163953Srrs } 3276163953Srrs } else { 3277163953Srrs break; 3278163953Srrs } 3279170992Srrs offset += SCTP_SIZE32(param_len); 3280163953Srrs chk_length -= SCTP_SIZE32(param_len); 3281163953Srrs } 3282163953Srrs if (num_req == 0) { 3283163953Srrs /* we have no response free the stuff */ 3284163953Srrs goto strres_nochunk; 3285163953Srrs } 3286163953Srrs /* ok we have a chunk to link in */ 3287163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3288163953Srrs chk, 3289163953Srrs sctp_next); 3290163953Srrs stcb->asoc.ctrl_queue_cnt++; 3291163953Srrs return (ret_code); 3292163953Srrs} 3293163953Srrs 3294163953Srrs/* 3295163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3296163953Srrs * to handle this, either we get the whole packet and must disect it 3297163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3298163953Srrs * from a middle box that did the disectting for us. 3299163953Srrs */ 3300163953Srrsstatic void 3301163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3302170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3303163953Srrs{ 3304163953Srrs uint32_t bottle_bw, on_queue; 3305163953Srrs uint16_t trunc_len; 3306163953Srrs unsigned int chlen; 3307163953Srrs unsigned int at; 3308163953Srrs struct sctp_chunk_desc desc; 3309163953Srrs struct sctp_chunkhdr *ch; 3310163953Srrs 3311163953Srrs chlen = ntohs(cp->ch.chunk_length); 3312163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3313163953Srrs /* XXX possible chlen underflow */ 3314163953Srrs if (chlen == 0) { 3315163953Srrs ch = NULL; 3316163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3317163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3318163953Srrs } else { 3319163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3320163953Srrs chlen -= sizeof(struct sctphdr); 3321163953Srrs /* XXX possible chlen underflow */ 3322163953Srrs memset(&desc, 0, sizeof(desc)); 3323163953Srrs } 3324163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3325170781Srrs if (trunc_len > limit) { 3326170781Srrs trunc_len = limit; 3327170781Srrs } 3328163953Srrs /* now the chunks themselves */ 3329163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3330163953Srrs desc.chunk_type = ch->chunk_type; 3331163953Srrs /* get amount we need to move */ 3332163953Srrs at = ntohs(ch->chunk_length); 3333163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3334163953Srrs /* corrupt chunk, maybe at the end? */ 3335163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3336163953Srrs break; 3337163953Srrs } 3338163953Srrs if (trunc_len == 0) { 3339163953Srrs /* we are supposed to have all of it */ 3340163953Srrs if (at > chlen) { 3341163953Srrs /* corrupt skip it */ 3342163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3343163953Srrs break; 3344163953Srrs } 3345163953Srrs } else { 3346163953Srrs /* is there enough of it left ? */ 3347163953Srrs if (desc.chunk_type == SCTP_DATA) { 3348163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3349163953Srrs sizeof(desc.data_bytes))) { 3350163953Srrs break; 3351163953Srrs } 3352163953Srrs } else { 3353163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3354163953Srrs break; 3355163953Srrs } 3356163953Srrs } 3357163953Srrs } 3358163953Srrs if (desc.chunk_type == SCTP_DATA) { 3359163953Srrs /* can we get out the tsn? */ 3360163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3361163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3362163953Srrs 3363163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3364163953Srrs /* yep */ 3365163953Srrs struct sctp_data_chunk *dcp; 3366163953Srrs uint8_t *ddp; 3367163953Srrs unsigned int iii; 3368163953Srrs 3369163953Srrs dcp = (struct sctp_data_chunk *)ch; 3370163953Srrs ddp = (uint8_t *) (dcp + 1); 3371163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3372163953Srrs desc.data_bytes[iii] = ddp[iii]; 3373163953Srrs } 3374163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3375163953Srrs } else { 3376163953Srrs /* nope we are done. */ 3377163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3378163953Srrs break; 3379163953Srrs } 3380163953Srrs } else { 3381163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3382163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3383163953Srrs } 3384163953Srrs 3385163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3386163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3387163953Srrs break; 3388163953Srrs } 3389163953Srrs if (SCTP_SIZE32(at) > chlen) { 3390163953Srrs break; 3391163953Srrs } 3392163953Srrs chlen -= SCTP_SIZE32(at); 3393163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3394163953Srrs /* done, none left */ 3395163953Srrs break; 3396163953Srrs } 3397163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3398163953Srrs } 3399163953Srrs /* Now update any rwnd --- possibly */ 3400163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3401163953Srrs /* From a peer, we get a rwnd report */ 3402163953Srrs uint32_t a_rwnd; 3403163953Srrs 3404163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3405163953Srrs 3406163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3407163953Srrs on_queue = ntohl(cp->current_onq); 3408163953Srrs if (bottle_bw && on_queue) { 3409163953Srrs /* a rwnd report is in here */ 3410163953Srrs if (bottle_bw > on_queue) 3411163953Srrs a_rwnd = bottle_bw - on_queue; 3412163953Srrs else 3413163953Srrs a_rwnd = 0; 3414163953Srrs 3415163953Srrs if (a_rwnd == 0) 3416163953Srrs stcb->asoc.peers_rwnd = 0; 3417163953Srrs else { 3418163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3419163953Srrs stcb->asoc.peers_rwnd = 3420163953Srrs a_rwnd - stcb->asoc.total_flight; 3421163953Srrs } else { 3422163953Srrs stcb->asoc.peers_rwnd = 0; 3423163953Srrs } 3424163953Srrs if (stcb->asoc.peers_rwnd < 3425163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3426163953Srrs /* SWS sender side engages */ 3427163953Srrs stcb->asoc.peers_rwnd = 0; 3428163953Srrs } 3429163953Srrs } 3430163953Srrs } 3431163953Srrs } else { 3432163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3433163953Srrs } 3434163953Srrs 3435163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3436163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3437163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3438163953Srrs (stcb->asoc.sat_network)) { 3439163953Srrs /* 3440163953Srrs * This is debateable but for sat networks it makes sense 3441163953Srrs * Note if a T3 timer has went off, we will prohibit any 3442163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3443163953Srrs */ 3444171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 3445171440Srrs net, cp, &bottle_bw, &on_queue); 3446163953Srrs } 3447163953Srrs} 3448163953Srrs 3449163953Srrs/* 3450163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3451163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3452163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3453163953Srrs * length of the complete packet outputs: - length: modified to remaining 3454163953Srrs * length after control processing - netp: modified to new sctp_nets after 3455163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3456163953Srrs * bad packet,...) otherwise return the tcb for this packet 3457163953Srrs */ 3458170099Srrs#ifdef __GNUC__ 3459170099Srrs__attribute__((noinline)) 3460170099Srrs#endif 3461170099Srrs static struct sctp_tcb * 3462170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3463170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3464170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3465170181Srrs uint32_t vrf_id) 3466163953Srrs{ 3467163953Srrs struct sctp_association *asoc; 3468163953Srrs uint32_t vtag_in; 3469163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3470170140Srrs uint32_t chk_length; 3471163953Srrs int ret; 3472168299Srrs int abort_no_unlock = 0; 3473163953Srrs 3474163953Srrs /* 3475163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3476163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3477163953Srrs * until we get into jumbo grams and such.. 3478163953Srrs */ 3479166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3480163953Srrs struct sctp_tcb *locked_tcb = stcb; 3481163953Srrs int got_auth = 0; 3482163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3483163953Srrs int auth_skipped = 0; 3484163953Srrs 3485169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3486169420Srrs iphlen, *offset, length, stcb); 3487163953Srrs 3488163953Srrs /* validate chunk header length... */ 3489163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3490170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3491170056Srrs ntohs(ch->chunk_length)); 3492170091Srrs if (locked_tcb) { 3493170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3494170091Srrs } 3495163953Srrs return (NULL); 3496163953Srrs } 3497163953Srrs /* 3498163953Srrs * validate the verification tag 3499163953Srrs */ 3500163953Srrs vtag_in = ntohl(sh->v_tag); 3501163953Srrs 3502165220Srrs if (locked_tcb) { 3503165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3504165220Srrs } 3505163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3506170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3507170056Srrs ntohs(ch->chunk_length), vtag_in); 3508163953Srrs if (vtag_in != 0) { 3509163953Srrs /* protocol error- silently discard... */ 3510163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3511169378Srrs if (locked_tcb) { 3512163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3513169378Srrs } 3514163953Srrs return (NULL); 3515163953Srrs } 3516163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3517163953Srrs /* 3518163953Srrs * If there is no stcb, skip the AUTH chunk and process 3519163953Srrs * later after a stcb is found (to validate the lookup was 3520163953Srrs * valid. 3521163953Srrs */ 3522163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3523163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3524163953Srrs /* save this chunk for later processing */ 3525163953Srrs auth_skipped = 1; 3526163953Srrs auth_offset = *offset; 3527163953Srrs auth_len = ntohs(ch->chunk_length); 3528163953Srrs 3529163953Srrs /* (temporarily) move past this chunk */ 3530163953Srrs *offset += SCTP_SIZE32(auth_len); 3531163953Srrs if (*offset >= length) { 3532163953Srrs /* no more data left in the mbuf chain */ 3533163953Srrs *offset = length; 3534170091Srrs if (locked_tcb) { 3535170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3536170091Srrs } 3537163953Srrs return (NULL); 3538163953Srrs } 3539163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3540163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3541163953Srrs } 3542169420Srrs if (ch == NULL) { 3543169420Srrs /* Help */ 3544169420Srrs *offset = length; 3545170091Srrs if (locked_tcb) { 3546170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3547170091Srrs } 3548169420Srrs return (NULL); 3549169420Srrs } 3550163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3551163953Srrs goto process_control_chunks; 3552163953Srrs } 3553163953Srrs /* 3554163953Srrs * first check if it's an ASCONF with an unknown src addr we 3555163953Srrs * need to look inside to find the association 3556163953Srrs */ 3557163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3558163953Srrs /* inp's refcount may be reduced */ 3559163953Srrs SCTP_INP_INCR_REF(inp); 3560163953Srrs 3561163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3562163953Srrs *offset, sh, &inp, netp); 3563163953Srrs if (stcb == NULL) { 3564163953Srrs /* 3565163953Srrs * reduce inp's refcount if not reduced in 3566163953Srrs * sctp_findassociation_ep_asconf(). 3567163953Srrs */ 3568163953Srrs SCTP_INP_DECR_REF(inp); 3569163953Srrs } 3570163953Srrs /* now go back and verify any auth chunk to be sure */ 3571163953Srrs if (auth_skipped && (stcb != NULL)) { 3572163953Srrs struct sctp_auth_chunk *auth; 3573163953Srrs 3574163953Srrs auth = (struct sctp_auth_chunk *) 3575163953Srrs sctp_m_getptr(m, auth_offset, 3576163953Srrs auth_len, chunk_buf); 3577163953Srrs got_auth = 1; 3578163953Srrs auth_skipped = 0; 3579169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3580163953Srrs auth_offset)) { 3581163953Srrs /* auth HMAC failed so dump it */ 3582163953Srrs *offset = length; 3583170091Srrs if (locked_tcb) { 3584170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3585170091Srrs } 3586163953Srrs return (NULL); 3587163953Srrs } else { 3588163953Srrs /* remaining chunks are HMAC checked */ 3589163953Srrs stcb->asoc.authenticated = 1; 3590163953Srrs } 3591163953Srrs } 3592163953Srrs } 3593163953Srrs if (stcb == NULL) { 3594163953Srrs /* no association, so it's out of the blue... */ 3595169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3596170181Srrs vrf_id); 3597163953Srrs *offset = length; 3598169378Srrs if (locked_tcb) { 3599163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3600169378Srrs } 3601163953Srrs return (NULL); 3602163953Srrs } 3603163953Srrs asoc = &stcb->asoc; 3604163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3605163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3606163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3607163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3608163953Srrs if ((vtag_in == asoc->my_vtag) || 3609163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3610163953Srrs (vtag_in == asoc->peer_vtag))) { 3611163953Srrs /* this is valid */ 3612163953Srrs } else { 3613163953Srrs /* drop this packet... */ 3614163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3615169378Srrs if (locked_tcb) { 3616163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3617169378Srrs } 3618163953Srrs return (NULL); 3619163953Srrs } 3620163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3621163953Srrs if (vtag_in != asoc->my_vtag) { 3622163953Srrs /* 3623163953Srrs * this could be a stale SHUTDOWN-ACK or the 3624163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3625163953Srrs * is still hung; we have started a new asoc 3626163953Srrs * but it won't complete until the shutdown 3627163953Srrs * is completed 3628163953Srrs */ 3629169378Srrs if (locked_tcb) { 3630163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3631169378Srrs } 3632163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3633170181Srrs NULL, vrf_id); 3634163953Srrs return (NULL); 3635163953Srrs } 3636163953Srrs } else { 3637163953Srrs /* for all other chunks, vtag must match */ 3638163953Srrs if (vtag_in != asoc->my_vtag) { 3639163953Srrs /* invalid vtag... */ 3640169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3641169420Srrs "invalid vtag: %xh, expect %xh\n", 3642169420Srrs vtag_in, asoc->my_vtag); 3643163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3644169378Srrs if (locked_tcb) { 3645163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3646169378Srrs } 3647163953Srrs *offset = length; 3648163953Srrs return (NULL); 3649163953Srrs } 3650163953Srrs } 3651163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3652163953Srrs /* 3653163953Srrs * process all control chunks... 3654163953Srrs */ 3655163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3656163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3657163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3658163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3659171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 3660171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3661171943Srrs stcb->asoc.overall_error_count, 3662171943Srrs 0, 3663171943Srrs SCTP_FROM_SCTP_INPUT, 3664171943Srrs __LINE__); 3665171943Srrs } 3666163953Srrs stcb->asoc.overall_error_count = 0; 3667163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3668163953Srrs *netp); 3669163953Srrs } 3670163953Srrsprocess_control_chunks: 3671163953Srrs while (IS_SCTP_CONTROL(ch)) { 3672163953Srrs /* validate chunk length */ 3673163953Srrs chk_length = ntohs(ch->chunk_length); 3674169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 3675169420Srrs ch->chunk_type, chk_length); 3676170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 3677170140Srrs if (chk_length < sizeof(*ch) || 3678170140Srrs (*offset + (int)chk_length) > length) { 3679163953Srrs *offset = length; 3680169378Srrs if (locked_tcb) { 3681163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3682169378Srrs } 3683163953Srrs return (NULL); 3684163953Srrs } 3685163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3686163953Srrs /* 3687163953Srrs * INIT-ACK only gets the init ack "header" portion only 3688163953Srrs * because we don't have to process the peer's COOKIE. All 3689163953Srrs * others get a complete chunk. 3690163953Srrs */ 3691169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3692169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3693163953Srrs /* get an init-ack chunk */ 3694163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3695163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3696163953Srrs if (ch == NULL) { 3697163953Srrs *offset = length; 3698169378Srrs if (locked_tcb) { 3699163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3700169378Srrs } 3701163953Srrs return (NULL); 3702163953Srrs } 3703170642Srrs } else { 3704170786Srrs /* For cookies and all other chunks. */ 3705169208Srrs if (chk_length > sizeof(chunk_buf)) { 3706169208Srrs /* 3707169208Srrs * use just the size of the chunk buffer so 3708170642Srrs * the front part of our chunks fit in 3709170642Srrs * contiguous space up to the chunk buffer 3710170642Srrs * size (508 bytes). For chunks that need to 3711170786Srrs * get more than that they must use the 3712170642Srrs * sctp_m_getptr() function or other means 3713170786Srrs * (e.g. know how to parse mbuf chains). 3714170786Srrs * Cookies do this already. 3715169208Srrs */ 3716169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3717169208Srrs (sizeof(chunk_buf) - 4), 3718169208Srrs chunk_buf); 3719169208Srrs if (ch == NULL) { 3720169208Srrs *offset = length; 3721169378Srrs if (locked_tcb) { 3722169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 3723169378Srrs } 3724169208Srrs return (NULL); 3725169208Srrs } 3726169208Srrs } else { 3727169208Srrs /* We can fit it all */ 3728170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3729170642Srrs chk_length, chunk_buf); 3730170642Srrs if (ch == NULL) { 3731170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 3732170642Srrs *offset = length; 3733170642Srrs if (locked_tcb) { 3734170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 3735169208Srrs } 3736170642Srrs return (NULL); 3737163953Srrs } 3738163953Srrs } 3739163953Srrs } 3740163953Srrs num_chunks++; 3741163953Srrs /* Save off the last place we got a control from */ 3742163953Srrs if (stcb != NULL) { 3743169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 3744163953Srrs /* 3745163953Srrs * allow last_control to be NULL if 3746163953Srrs * ASCONF... ASCONF processing will find the 3747163953Srrs * right net later 3748163953Srrs */ 3749169420Srrs if ((netp != NULL) && (*netp != NULL)) 3750169420Srrs stcb->asoc.last_control_chunk_from = *netp; 3751163953Srrs } 3752163953Srrs } 3753163953Srrs#ifdef SCTP_AUDITING_ENABLED 3754163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3755163953Srrs#endif 3756163953Srrs 3757163953Srrs /* check to see if this chunk required auth, but isn't */ 3758163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3759163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3760163953Srrs stcb->asoc.local_auth_chunks) && 3761163953Srrs !stcb->asoc.authenticated) { 3762163953Srrs /* "silently" ignore */ 3763163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3764163953Srrs goto next_chunk; 3765163953Srrs } 3766163953Srrs switch (ch->chunk_type) { 3767163953Srrs case SCTP_INITIATION: 3768163953Srrs /* must be first and only chunk */ 3769169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 3770163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3771163953Srrs /* We are not interested anymore? */ 3772163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3773163953Srrs /* 3774163953Srrs * collision case where we are 3775163953Srrs * sending to them too 3776163953Srrs */ 3777163953Srrs ; 3778163953Srrs } else { 3779169378Srrs if (locked_tcb) { 3780163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3781169378Srrs } 3782163953Srrs *offset = length; 3783163953Srrs return (NULL); 3784163953Srrs } 3785163953Srrs } 3786163953Srrs if ((num_chunks > 1) || 3787170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3788163953Srrs *offset = length; 3789169378Srrs if (locked_tcb) { 3790163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3791169378Srrs } 3792163953Srrs return (NULL); 3793163953Srrs } 3794163953Srrs if ((stcb != NULL) && 3795163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3796163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3797163953Srrs sctp_send_shutdown_ack(stcb, 3798163953Srrs stcb->asoc.primary_destination); 3799163953Srrs *offset = length; 3800163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3801169378Srrs if (locked_tcb) { 3802163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3803169378Srrs } 3804163953Srrs return (NULL); 3805163953Srrs } 3806169420Srrs if (netp) { 3807169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 3808169420Srrs (struct sctp_init_chunk *)ch, inp, 3809170181Srrs stcb, *netp, &abort_no_unlock, vrf_id); 3810169420Srrs } 3811168299Srrs if (abort_no_unlock) 3812168299Srrs return (NULL); 3813168299Srrs 3814163953Srrs *offset = length; 3815169378Srrs if (locked_tcb) { 3816163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3817169378Srrs } 3818163953Srrs return (NULL); 3819163953Srrs break; 3820170642Srrs case SCTP_PAD_CHUNK: 3821170642Srrs break; 3822163953Srrs case SCTP_INITIATION_ACK: 3823163953Srrs /* must be first and only chunk */ 3824169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 3825163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3826163953Srrs /* We are not interested anymore */ 3827163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3828163953Srrs ; 3829163953Srrs } else { 3830169378Srrs if (locked_tcb) { 3831163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3832169378Srrs } 3833163953Srrs *offset = length; 3834163953Srrs if (stcb) { 3835171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 3836163953Srrs } 3837163953Srrs return (NULL); 3838163953Srrs } 3839163953Srrs } 3840163953Srrs if ((num_chunks > 1) || 3841170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3842163953Srrs *offset = length; 3843169378Srrs if (locked_tcb) { 3844163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3845169378Srrs } 3846163953Srrs return (NULL); 3847163953Srrs } 3848169420Srrs if ((netp) && (*netp)) { 3849169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3850170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 3851169420Srrs } else { 3852169420Srrs ret = -1; 3853169420Srrs } 3854163953Srrs /* 3855163953Srrs * Special case, I must call the output routine to 3856163953Srrs * get the cookie echoed 3857163953Srrs */ 3858168299Srrs if (abort_no_unlock) 3859168299Srrs return (NULL); 3860168299Srrs 3861163953Srrs if ((stcb) && ret == 0) 3862163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3863163953Srrs *offset = length; 3864169378Srrs if (locked_tcb) { 3865163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3866169378Srrs } 3867163953Srrs return (NULL); 3868163953Srrs break; 3869163953Srrs case SCTP_SELECTIVE_ACK: 3870169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 3871163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3872163953Srrs { 3873163953Srrs struct sctp_sack_chunk *sack; 3874163953Srrs int abort_now = 0; 3875163953Srrs uint32_t a_rwnd, cum_ack; 3876163953Srrs uint16_t num_seg; 3877163953Srrs int nonce_sum_flag; 3878163953Srrs 3879169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 3880169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 3881169208Srrs *offset = length; 3882169378Srrs if (locked_tcb) { 3883169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 3884169378Srrs } 3885169208Srrs return (NULL); 3886169208Srrs } 3887163953Srrs sack = (struct sctp_sack_chunk *)ch; 3888163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3889163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3890163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3891163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3892170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 3893170606Srrs cum_ack, 3894170606Srrs num_seg, 3895170606Srrs a_rwnd 3896170606Srrs ); 3897163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3898163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3899163953Srrs (num_seg == 0) && 3900163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3901163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3902163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3903163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3904163953Srrs ) { 3905163953Srrs /* 3906163953Srrs * We have a SIMPLE sack having no 3907163953Srrs * prior segments and data on sent 3908163953Srrs * queue to be acked.. Use the 3909163953Srrs * faster path sack processing. We 3910163953Srrs * also allow window update sacks 3911163953Srrs * with no missing segments to go 3912163953Srrs * this way too. 3913163953Srrs */ 3914169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 3915169208Srrs &abort_now); 3916163953Srrs } else { 3917169420Srrs if (netp && *netp) 3918170781Srrs sctp_handle_sack(m, *offset, 3919170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 3920163953Srrs } 3921163953Srrs if (abort_now) { 3922163953Srrs /* ABORT signal from sack processing */ 3923163953Srrs *offset = length; 3924163953Srrs return (NULL); 3925163953Srrs } 3926163953Srrs } 3927163953Srrs break; 3928163953Srrs case SCTP_HEARTBEAT_REQUEST: 3929169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 3930169420Srrs if ((stcb) && netp && *netp) { 3931169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 3932169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 3933169420Srrs chk_length, *netp); 3934169420Srrs 3935169420Srrs /* He's alive so give him credit */ 3936171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 3937171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3938171943Srrs stcb->asoc.overall_error_count, 3939171943Srrs 0, 3940171943Srrs SCTP_FROM_SCTP_INPUT, 3941171943Srrs __LINE__); 3942171943Srrs } 3943169420Srrs stcb->asoc.overall_error_count = 0; 3944163953Srrs } 3945163953Srrs break; 3946163953Srrs case SCTP_HEARTBEAT_ACK: 3947169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 3948169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 3949169208Srrs /* Its not ours */ 3950169352Srrs *offset = length; 3951169378Srrs if (locked_tcb) { 3952169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3953169378Srrs } 3954169208Srrs return (NULL); 3955169208Srrs } 3956163953Srrs /* He's alive so give him credit */ 3957171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 3958171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3959171943Srrs stcb->asoc.overall_error_count, 3960171943Srrs 0, 3961171943Srrs SCTP_FROM_SCTP_INPUT, 3962171943Srrs __LINE__); 3963171943Srrs } 3964163953Srrs stcb->asoc.overall_error_count = 0; 3965163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 3966169420Srrs if (netp && *netp) 3967169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 3968169420Srrs stcb, *netp); 3969163953Srrs break; 3970163953Srrs case SCTP_ABORT_ASSOCIATION: 3971170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 3972170091Srrs stcb); 3973169420Srrs if ((stcb) && netp && *netp) 3974169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 3975169420Srrs stcb, *netp); 3976163953Srrs *offset = length; 3977163953Srrs return (NULL); 3978163953Srrs break; 3979163953Srrs case SCTP_SHUTDOWN: 3980170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 3981170091Srrs stcb); 3982169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 3983169208Srrs *offset = length; 3984169378Srrs if (locked_tcb) { 3985169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3986169378Srrs } 3987169208Srrs return (NULL); 3988169420Srrs } 3989169420Srrs if (netp && *netp) { 3990163953Srrs int abort_flag = 0; 3991163953Srrs 3992163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 3993163953Srrs stcb, *netp, &abort_flag); 3994163953Srrs if (abort_flag) { 3995163953Srrs *offset = length; 3996163953Srrs return (NULL); 3997163953Srrs } 3998163953Srrs } 3999163953Srrs break; 4000163953Srrs case SCTP_SHUTDOWN_ACK: 4001170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 4002169420Srrs if ((stcb) && (netp) && (*netp)) 4003169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4004163953Srrs *offset = length; 4005163953Srrs return (NULL); 4006163953Srrs break; 4007169420Srrs 4008163953Srrs case SCTP_OPERATION_ERROR: 4009169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 4010169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 4011169420Srrs 4012163953Srrs *offset = length; 4013163953Srrs return (NULL); 4014163953Srrs } 4015163953Srrs break; 4016163953Srrs case SCTP_COOKIE_ECHO: 4017169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4018170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4019163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4020163953Srrs ; 4021163953Srrs } else { 4022169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4023163953Srrs /* We are not interested anymore */ 4024163953Srrs *offset = length; 4025163953Srrs return (NULL); 4026163953Srrs } 4027163953Srrs } 4028163953Srrs /* 4029163953Srrs * First are we accepting? We do this again here 4030169420Srrs * sincen it is possible that a previous endpoint 4031169420Srrs * WAS listening responded to a INIT-ACK and then 4032163953Srrs * closed. We opened and bound.. and are now no 4033163953Srrs * longer listening. 4034163953Srrs */ 4035163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4036163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4037163953Srrs /* 4038163953Srrs * special case, is this a retran'd 4039163953Srrs * COOKIE-ECHO or a restarting assoc 4040163953Srrs * that is a peeled off or 4041163953Srrs * one-to-one style socket. 4042163953Srrs */ 4043163953Srrs goto process_cookie_anyway; 4044163953Srrs } 4045169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4046170181Srrs sh, NULL, vrf_id); 4047163953Srrs *offset = length; 4048163953Srrs return (NULL); 4049163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4050163953Srrs /* we are accepting so check limits like TCP */ 4051163953Srrs if (inp->sctp_socket->so_qlen > 4052163953Srrs inp->sctp_socket->so_qlimit) { 4053163953Srrs /* no space */ 4054163953Srrs struct mbuf *oper; 4055163953Srrs struct sctp_paramhdr *phdr; 4056163953Srrs 4057163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4058163953Srrs oper = NULL; 4059163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4060165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4061163953Srrs if (oper) { 4062165647Srrs SCTP_BUF_LEN(oper) = 4063163953Srrs sizeof(struct sctp_paramhdr); 4064163953Srrs phdr = mtod(oper, 4065163953Srrs struct sctp_paramhdr *); 4066163953Srrs phdr->param_type = 4067163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4068163953Srrs phdr->param_length = 4069163953Srrs htons(sizeof(struct sctp_paramhdr)); 4070163953Srrs } 4071163953Srrs sctp_abort_association(inp, stcb, m, 4072170181Srrs iphlen, sh, oper, vrf_id); 4073163953Srrs } 4074163953Srrs *offset = length; 4075163953Srrs return (NULL); 4076163953Srrs } 4077163953Srrs } 4078163953Srrs process_cookie_anyway: 4079163953Srrs { 4080163953Srrs struct mbuf *ret_buf; 4081165220Srrs struct sctp_inpcb *linp; 4082163953Srrs 4083169420Srrs if (stcb) { 4084165220Srrs linp = NULL; 4085169420Srrs } else { 4086165220Srrs linp = inp; 4087169420Srrs } 4088165220Srrs 4089169420Srrs if (linp) { 4090165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4091169420Srrs } 4092169420Srrs if (netp) { 4093169420Srrs ret_buf = 4094169420Srrs sctp_handle_cookie_echo(m, iphlen, 4095169420Srrs *offset, sh, 4096169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4097169420Srrs &inp, &stcb, netp, 4098169420Srrs auth_skipped, 4099169420Srrs auth_offset, 4100169420Srrs auth_len, 4101169420Srrs &locked_tcb, 4102170181Srrs vrf_id); 4103169420Srrs } else { 4104169420Srrs ret_buf = NULL; 4105169420Srrs } 4106169420Srrs if (linp) { 4107165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4108169420Srrs } 4109163953Srrs if (ret_buf == NULL) { 4110163953Srrs if (locked_tcb) { 4111163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4112163953Srrs } 4113169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4114169420Srrs "GAK, null buffer\n"); 4115163953Srrs auth_skipped = 0; 4116163953Srrs *offset = length; 4117163953Srrs return (NULL); 4118163953Srrs } 4119163953Srrs /* if AUTH skipped, see if it verified... */ 4120163953Srrs if (auth_skipped) { 4121163953Srrs got_auth = 1; 4122163953Srrs auth_skipped = 0; 4123163953Srrs } 4124163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4125163953Srrs /* 4126163953Srrs * Restart the timer if we have 4127163953Srrs * pending data 4128163953Srrs */ 4129163953Srrs struct sctp_tmit_chunk *chk; 4130163953Srrs 4131163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4132163953Srrs if (chk) { 4133163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4134163953Srrs stcb->sctp_ep, stcb, 4135163953Srrs chk->whoTo); 4136163953Srrs } 4137163953Srrs } 4138163953Srrs } 4139163953Srrs break; 4140163953Srrs case SCTP_COOKIE_ACK: 4141170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4142169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4143169378Srrs if (locked_tcb) { 4144169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4145169378Srrs } 4146169352Srrs return (NULL); 4147169352Srrs } 4148163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4149163953Srrs /* We are not interested anymore */ 4150163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4151163953Srrs ; 4152169420Srrs } else if (stcb) { 4153171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4154163953Srrs *offset = length; 4155163953Srrs return (NULL); 4156163953Srrs } 4157163953Srrs } 4158163953Srrs /* He's alive so give him credit */ 4159169420Srrs if ((stcb) && netp && *netp) { 4160171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4161171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4162171943Srrs stcb->asoc.overall_error_count, 4163171943Srrs 0, 4164171943Srrs SCTP_FROM_SCTP_INPUT, 4165171943Srrs __LINE__); 4166171943Srrs } 4167169378Srrs stcb->asoc.overall_error_count = 0; 4168169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4169169378Srrs } 4170163953Srrs break; 4171163953Srrs case SCTP_ECN_ECHO: 4172169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4173163953Srrs /* He's alive so give him credit */ 4174169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4175169208Srrs /* Its not ours */ 4176169378Srrs if (locked_tcb) { 4177169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4178169378Srrs } 4179169208Srrs *offset = length; 4180169208Srrs return (NULL); 4181169208Srrs } 4182169378Srrs if (stcb) { 4183171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4184171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4185171943Srrs stcb->asoc.overall_error_count, 4186171943Srrs 0, 4187171943Srrs SCTP_FROM_SCTP_INPUT, 4188171943Srrs __LINE__); 4189171943Srrs } 4190169378Srrs stcb->asoc.overall_error_count = 0; 4191169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4192169378Srrs stcb); 4193169378Srrs } 4194163953Srrs break; 4195163953Srrs case SCTP_ECN_CWR: 4196169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4197163953Srrs /* He's alive so give him credit */ 4198169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4199169208Srrs /* Its not ours */ 4200169378Srrs if (locked_tcb) { 4201169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4202169378Srrs } 4203169208Srrs *offset = length; 4204169208Srrs return (NULL); 4205169208Srrs } 4206169378Srrs if (stcb) { 4207171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4208171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4209171943Srrs stcb->asoc.overall_error_count, 4210171943Srrs 0, 4211171943Srrs SCTP_FROM_SCTP_INPUT, 4212171943Srrs __LINE__); 4213171943Srrs } 4214169378Srrs stcb->asoc.overall_error_count = 0; 4215169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4216169378Srrs } 4217163953Srrs break; 4218163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4219170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4220163953Srrs /* must be first and only chunk */ 4221163953Srrs if ((num_chunks > 1) || 4222170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4223163953Srrs *offset = length; 4224169378Srrs if (locked_tcb) { 4225163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4226169378Srrs } 4227163953Srrs return (NULL); 4228163953Srrs } 4229169420Srrs if ((stcb) && netp && *netp) { 4230169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4231169378Srrs stcb, *netp); 4232169378Srrs } 4233163953Srrs *offset = length; 4234163953Srrs return (NULL); 4235163953Srrs break; 4236163953Srrs case SCTP_ASCONF: 4237169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4238163953Srrs /* He's alive so give him credit */ 4239169378Srrs if (stcb) { 4240171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4241171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4242171943Srrs stcb->asoc.overall_error_count, 4243171943Srrs 0, 4244171943Srrs SCTP_FROM_SCTP_INPUT, 4245171943Srrs __LINE__); 4246171943Srrs } 4247169378Srrs stcb->asoc.overall_error_count = 0; 4248169378Srrs sctp_handle_asconf(m, *offset, 4249169378Srrs (struct sctp_asconf_chunk *)ch, stcb); 4250169378Srrs } 4251163953Srrs break; 4252163953Srrs case SCTP_ASCONF_ACK: 4253169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4254169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4255169208Srrs /* Its not ours */ 4256169378Srrs if (locked_tcb) { 4257169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4258169378Srrs } 4259169208Srrs *offset = length; 4260169208Srrs return (NULL); 4261169208Srrs } 4262169420Srrs if ((stcb) && netp && *netp) { 4263169378Srrs /* He's alive so give him credit */ 4264171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4265171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4266171943Srrs stcb->asoc.overall_error_count, 4267171943Srrs 0, 4268171943Srrs SCTP_FROM_SCTP_INPUT, 4269171943Srrs __LINE__); 4270171943Srrs } 4271169378Srrs stcb->asoc.overall_error_count = 0; 4272169378Srrs sctp_handle_asconf_ack(m, *offset, 4273169378Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4274169378Srrs } 4275163953Srrs break; 4276163953Srrs case SCTP_FORWARD_CUM_TSN: 4277169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4278169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4279169208Srrs /* Its not ours */ 4280169378Srrs if (locked_tcb) { 4281169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4282169378Srrs } 4283169208Srrs *offset = length; 4284169208Srrs return (NULL); 4285169208Srrs } 4286163953Srrs /* He's alive so give him credit */ 4287169378Srrs if (stcb) { 4288163953Srrs int abort_flag = 0; 4289163953Srrs 4290163953Srrs stcb->asoc.overall_error_count = 0; 4291171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4292171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4293171943Srrs stcb->asoc.overall_error_count, 4294171943Srrs 0, 4295171943Srrs SCTP_FROM_SCTP_INPUT, 4296171943Srrs __LINE__); 4297171943Srrs } 4298163953Srrs *fwd_tsn_seen = 1; 4299163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4300163953Srrs /* We are not interested anymore */ 4301171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4302163953Srrs *offset = length; 4303163953Srrs return (NULL); 4304163953Srrs } 4305163953Srrs sctp_handle_forward_tsn(stcb, 4306170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 4307163953Srrs if (abort_flag) { 4308163953Srrs *offset = length; 4309163953Srrs return (NULL); 4310163953Srrs } else { 4311171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4312171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4313171943Srrs stcb->asoc.overall_error_count, 4314171943Srrs 0, 4315171943Srrs SCTP_FROM_SCTP_INPUT, 4316171943Srrs __LINE__); 4317171943Srrs } 4318163953Srrs stcb->asoc.overall_error_count = 0; 4319163953Srrs } 4320163953Srrs 4321163953Srrs } 4322163953Srrs break; 4323163953Srrs case SCTP_STREAM_RESET: 4324169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4325169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4326169208Srrs /* Its not ours */ 4327169378Srrs if (locked_tcb) { 4328169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4329169378Srrs } 4330169208Srrs *offset = length; 4331169208Srrs return (NULL); 4332169208Srrs } 4333163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4334163953Srrs /* We are not interested anymore */ 4335171943Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4336163953Srrs *offset = length; 4337163953Srrs return (NULL); 4338163953Srrs } 4339169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4340169420Srrs /* 4341169420Srrs * hmm, peer should have announced this, but 4342169420Srrs * we will turn it on since he is sending us 4343169420Srrs * a stream reset. 4344169420Srrs */ 4345169420Srrs stcb->asoc.peer_supports_strreset = 1; 4346163953Srrs } 4347170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 4348169420Srrs /* stop processing */ 4349169420Srrs *offset = length; 4350169420Srrs return (NULL); 4351169420Srrs } 4352163953Srrs break; 4353163953Srrs case SCTP_PACKET_DROPPED: 4354169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4355163953Srrs /* re-get it all please */ 4356169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4357169208Srrs /* Its not ours */ 4358169378Srrs if (locked_tcb) { 4359169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4360169378Srrs } 4361169208Srrs *offset = length; 4362169208Srrs return (NULL); 4363169208Srrs } 4364169420Srrs if (ch && (stcb) && netp && (*netp)) { 4365169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4366170781Srrs stcb, *netp, 4367170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4368170781Srrs 4369169378Srrs } 4370163953Srrs break; 4371163953Srrs 4372163953Srrs case SCTP_AUTHENTICATION: 4373169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4374163953Srrs if (sctp_auth_disable) 4375163953Srrs goto unknown_chunk; 4376163953Srrs 4377163953Srrs if (stcb == NULL) { 4378163953Srrs /* save the first AUTH for later processing */ 4379163953Srrs if (auth_skipped == 0) { 4380163953Srrs auth_offset = *offset; 4381163953Srrs auth_len = chk_length; 4382163953Srrs auth_skipped = 1; 4383163953Srrs } 4384163953Srrs /* skip this chunk (temporarily) */ 4385163953Srrs goto next_chunk; 4386163953Srrs } 4387169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4388169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4389169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4390169208Srrs /* Its not ours */ 4391169378Srrs if (locked_tcb) { 4392169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4393169378Srrs } 4394169208Srrs *offset = length; 4395169208Srrs return (NULL); 4396169208Srrs } 4397163953Srrs if (got_auth == 1) { 4398163953Srrs /* skip this chunk... it's already auth'd */ 4399163953Srrs goto next_chunk; 4400163953Srrs } 4401163953Srrs got_auth = 1; 4402169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4403163953Srrs m, *offset)) { 4404163953Srrs /* auth HMAC failed so dump the packet */ 4405163953Srrs *offset = length; 4406163953Srrs return (stcb); 4407163953Srrs } else { 4408163953Srrs /* remaining chunks are HMAC checked */ 4409163953Srrs stcb->asoc.authenticated = 1; 4410163953Srrs } 4411163953Srrs break; 4412163953Srrs 4413163953Srrs default: 4414163953Srrs unknown_chunk: 4415163953Srrs /* it's an unknown chunk! */ 4416163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4417163953Srrs struct mbuf *mm; 4418163953Srrs struct sctp_paramhdr *phd; 4419163953Srrs 4420163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4421165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4422163953Srrs if (mm) { 4423163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4424163953Srrs /* 4425163953Srrs * We cheat and use param type since 4426163953Srrs * we did not bother to define a 4427163953Srrs * error cause struct. They are the 4428163953Srrs * same basic format with different 4429163953Srrs * names. 4430163953Srrs */ 4431163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4432163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4433165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4434166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4435163953Srrs M_DONTWAIT); 4436165647Srrs if (SCTP_BUF_NEXT(mm)) { 4437163953Srrs sctp_queue_op_err(stcb, mm); 4438163953Srrs } else { 4439163953Srrs sctp_m_freem(mm); 4440163953Srrs } 4441163953Srrs } 4442163953Srrs } 4443163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4444163953Srrs /* discard this packet */ 4445163953Srrs *offset = length; 4446163953Srrs return (stcb); 4447163953Srrs } /* else skip this bad chunk and continue... */ 4448163953Srrs break; 4449163953Srrs } /* switch (ch->chunk_type) */ 4450163953Srrs 4451163953Srrs 4452163953Srrsnext_chunk: 4453163953Srrs /* get the next chunk */ 4454163953Srrs *offset += SCTP_SIZE32(chk_length); 4455163953Srrs if (*offset >= length) { 4456163953Srrs /* no more data left in the mbuf chain */ 4457163953Srrs break; 4458163953Srrs } 4459163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4460163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4461163953Srrs if (ch == NULL) { 4462169378Srrs if (locked_tcb) { 4463163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4464169378Srrs } 4465163953Srrs *offset = length; 4466163953Srrs return (NULL); 4467163953Srrs } 4468163953Srrs } /* while */ 4469163953Srrs return (stcb); 4470163953Srrs} 4471163953Srrs 4472163953Srrs 4473163953Srrs/* 4474163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4475163953Srrs * ECN(0) or ECN(1) or CE 4476163953Srrs */ 4477170806Srrsstatic void 4478163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4479163953Srrs uint8_t ecn_bits) 4480163953Srrs{ 4481163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4482163953Srrs ; 4483163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4484163953Srrs /* 4485163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4486163953Srrs * change the NS bit (that we have yet to find a way to send 4487163953Srrs * it yet). 4488163953Srrs */ 4489163953Srrs 4490163953Srrs /* ECN Nonce stuff */ 4491163953Srrs stcb->asoc.receiver_nonce_sum++; 4492163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4493163953Srrs 4494163953Srrs /* 4495163953Srrs * Drag up the last_echo point if cumack is larger since we 4496163953Srrs * don't want the point falling way behind by more than 4497163953Srrs * 2^^31 and then having it be incorrect. 4498163953Srrs */ 4499163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4500163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4501163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4502163953Srrs } 4503163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4504163953Srrs /* 4505163953Srrs * Drag up the last_echo point if cumack is larger since we 4506163953Srrs * don't want the point falling way behind by more than 4507163953Srrs * 2^^31 and then having it be incorrect. 4508163953Srrs */ 4509163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4510163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4511163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4512163953Srrs } 4513163953Srrs } 4514163953Srrs} 4515163953Srrs 4516170806Srrsstatic void 4517163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4518163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4519163953Srrs{ 4520163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4521163953Srrs /* 4522163953Srrs * we possibly must notify the sender that a congestion 4523163953Srrs * window reduction is in order. We do this by adding a ECNE 4524163953Srrs * chunk to the output chunk queue. The incoming CWR will 4525163953Srrs * remove this chunk. 4526163953Srrs */ 4527163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4528163953Srrs MAX_TSN)) { 4529163953Srrs /* Yep, we need to add a ECNE */ 4530163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4531163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4532163953Srrs } 4533163953Srrs } 4534163953Srrs} 4535163953Srrs 4536171440Srrs#ifdef INVARIANTS 4537171440Srrsstatic void 4538171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 4539171440Srrs{ 4540171440Srrs struct sctp_tcb *stcb; 4541171440Srrs 4542171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4543171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 4544171440Srrs panic("Own lock on stcb at return from input"); 4545171440Srrs } 4546171440Srrs } 4547171440Srrs} 4548171440Srrs 4549171440Srrs#endif 4550171440Srrs 4551163953Srrs/* 4552163953Srrs * common input chunk processing (v4 and v6) 4553163953Srrs */ 4554169378Srrsvoid 4555163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4556163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4557163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4558170181Srrs uint8_t ecn_bits, uint32_t vrf_id) 4559163953Srrs{ 4560163953Srrs /* 4561163953Srrs * Control chunk processing 4562163953Srrs */ 4563163953Srrs uint32_t high_tsn; 4564163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4565163953Srrs struct mbuf *m = *mm; 4566163953Srrs int abort_flag = 0; 4567163953Srrs int un_sent; 4568163953Srrs 4569163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4570163953Srrs#ifdef SCTP_AUDITING_ENABLED 4571163953Srrs sctp_audit_log(0xE0, 1); 4572163953Srrs sctp_auditing(0, inp, stcb, net); 4573163953Srrs#endif 4574163953Srrs 4575169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4576169420Srrs m, iphlen, offset); 4577163953Srrs 4578163953Srrs if (stcb) { 4579163953Srrs /* always clear this before beginning a packet */ 4580163953Srrs stcb->asoc.authenticated = 0; 4581163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4582171943Srrs if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 4583171943Srrs (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 4584171745Srrs /*- 4585171745Srrs * If we hit here, we had a ref count 4586171745Srrs * up when the assoc was aborted and the 4587171745Srrs * timer is clearing out the assoc, we should 4588171745Srrs * NOT respond to any packet.. its OOTB. 4589171745Srrs */ 4590171745Srrs SCTP_TCB_UNLOCK(stcb); 4591171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4592171745Srrs vrf_id); 4593171745Srrs goto out_now; 4594171745Srrs } 4595163953Srrs } 4596163953Srrs if (IS_SCTP_CONTROL(ch)) { 4597163953Srrs /* process the control portion of the SCTP packet */ 4598169655Srrs /* sa_ignore NO_NULL_CHK */ 4599163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4600170181Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id); 4601163953Srrs if (stcb) { 4602163953Srrs /* 4603163953Srrs * This covers us if the cookie-echo was there and 4604163953Srrs * it changes our INP. 4605163953Srrs */ 4606163953Srrs inp = stcb->sctp_ep; 4607163953Srrs } 4608163953Srrs } else { 4609163953Srrs /* 4610163953Srrs * no control chunks, so pre-process DATA chunks (these 4611163953Srrs * checks are taken care of by control processing) 4612163953Srrs */ 4613163953Srrs 4614163953Srrs /* 4615163953Srrs * if DATA only packet, and auth is required, then punt... 4616163953Srrs * can't have authenticated without any AUTH (control) 4617163953Srrs * chunks 4618163953Srrs */ 4619163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4620163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4621163953Srrs stcb->asoc.local_auth_chunks)) { 4622163953Srrs /* "silently" ignore */ 4623163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4624163953Srrs SCTP_TCB_UNLOCK(stcb); 4625171440Srrs goto out_now; 4626163953Srrs } 4627163953Srrs if (stcb == NULL) { 4628163953Srrs /* out of the blue DATA chunk */ 4629169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4630170181Srrs vrf_id); 4631171440Srrs goto out_now; 4632163953Srrs } 4633163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4634163953Srrs /* v_tag mismatch! */ 4635163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4636163953Srrs SCTP_TCB_UNLOCK(stcb); 4637171440Srrs goto out_now; 4638163953Srrs } 4639163953Srrs } 4640163953Srrs 4641163953Srrs if (stcb == NULL) { 4642163953Srrs /* 4643163953Srrs * no valid TCB for this packet, or we found it's a bad 4644163953Srrs * packet while processing control, or we're done with this 4645163953Srrs * packet (done or skip rest of data), so we drop it... 4646163953Srrs */ 4647171440Srrs goto out_now; 4648163953Srrs } 4649163953Srrs /* 4650163953Srrs * DATA chunk processing 4651163953Srrs */ 4652163953Srrs /* plow through the data chunks while length > offset */ 4653163953Srrs 4654163953Srrs /* 4655163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4656163953Srrs * DATA is required. 4657163953Srrs */ 4658165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4659163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4660163953Srrs stcb->asoc.local_auth_chunks) && 4661163953Srrs !stcb->asoc.authenticated) { 4662163953Srrs /* "silently" ignore */ 4663163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4664169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 4665169420Srrs "Data chunk requires AUTH, skipped\n"); 4666165220Srrs goto trigger_send; 4667163953Srrs } 4668163953Srrs if (length > offset) { 4669163953Srrs int retval; 4670163953Srrs 4671163953Srrs /* 4672163953Srrs * First check to make sure our state is correct. We would 4673163953Srrs * not get here unless we really did have a tag, so we don't 4674163953Srrs * abort if this happens, just dump the chunk silently. 4675163953Srrs */ 4676163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4677163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4678163953Srrs /* 4679163953Srrs * we consider data with valid tags in this state 4680163953Srrs * shows us the cookie-ack was lost. Imply it was 4681163953Srrs * there. 4682163953Srrs */ 4683171943Srrs if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) { 4684171943Srrs sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4685171943Srrs stcb->asoc.overall_error_count, 4686171943Srrs 0, 4687171943Srrs SCTP_FROM_SCTP_INPUT, 4688171943Srrs __LINE__); 4689171943Srrs } 4690163953Srrs stcb->asoc.overall_error_count = 0; 4691163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4692163953Srrs break; 4693163953Srrs case SCTP_STATE_COOKIE_WAIT: 4694163953Srrs /* 4695163953Srrs * We consider OOTB any data sent during asoc setup. 4696163953Srrs */ 4697169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4698170181Srrs vrf_id); 4699163953Srrs SCTP_TCB_UNLOCK(stcb); 4700171440Srrs goto out_now; 4701171531Srrs /* sa_ignore NOTREACHED */ 4702163953Srrs break; 4703163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4704163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4705163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4706163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4707163953Srrs default: 4708163953Srrs SCTP_TCB_UNLOCK(stcb); 4709171440Srrs goto out_now; 4710171531Srrs /* sa_ignore NOTREACHED */ 4711163953Srrs break; 4712163953Srrs case SCTP_STATE_OPEN: 4713163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4714163953Srrs break; 4715163953Srrs } 4716163953Srrs /* take care of ECN, part 1. */ 4717163953Srrs if (stcb->asoc.ecn_allowed && 4718163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4719163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4720163953Srrs } 4721163953Srrs /* plow through the data chunks while length > offset */ 4722163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4723163953Srrs inp, stcb, net, &high_tsn); 4724163953Srrs if (retval == 2) { 4725163953Srrs /* 4726163953Srrs * The association aborted, NO UNLOCK needed since 4727163953Srrs * the association is destroyed. 4728163953Srrs */ 4729171440Srrs goto out_now; 4730163953Srrs } 4731163953Srrs data_processed = 1; 4732163953Srrs if (retval == 0) { 4733163953Srrs /* take care of ecn part 2. */ 4734163953Srrs if (stcb->asoc.ecn_allowed && 4735163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4736163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4737163953Srrs ecn_bits); 4738163953Srrs } 4739163953Srrs } 4740163953Srrs /* 4741163953Srrs * Anything important needs to have been m_copy'ed in 4742163953Srrs * process_data 4743163953Srrs */ 4744163953Srrs } 4745163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4746163953Srrs int was_a_gap = 0; 4747163953Srrs 4748163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4749163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4750163953Srrs /* there was a gap before this data was processed */ 4751163953Srrs was_a_gap = 1; 4752163953Srrs } 4753163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4754163953Srrs if (abort_flag) { 4755163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4756171440Srrs goto out_now; 4757163953Srrs } 4758163953Srrs } 4759163953Srrs /* trigger send of any chunks in queue... */ 4760165220Srrstrigger_send: 4761163953Srrs#ifdef SCTP_AUDITING_ENABLED 4762163953Srrs sctp_audit_log(0xE0, 2); 4763163953Srrs sctp_auditing(1, inp, stcb, net); 4764163953Srrs#endif 4765169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4766169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 4767169420Srrs stcb->asoc.peers_rwnd, 4768169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4769169420Srrs stcb->asoc.total_flight); 4770163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4771163953Srrs 4772163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4773163953Srrs ((un_sent) && 4774163953Srrs (stcb->asoc.peers_rwnd > 0 || 4775163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4776169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 4777163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4778169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 4779163953Srrs } 4780163953Srrs#ifdef SCTP_AUDITING_ENABLED 4781163953Srrs sctp_audit_log(0xE0, 3); 4782163953Srrs sctp_auditing(2, inp, stcb, net); 4783163953Srrs#endif 4784163953Srrs SCTP_TCB_UNLOCK(stcb); 4785171440Srrsout_now: 4786171440Srrs#ifdef INVARIANTS 4787171440Srrs sctp_validate_no_locks(inp); 4788171440Srrs#endif 4789169378Srrs return; 4790163953Srrs} 4791163953Srrs 4792163953Srrs 4793163953Srrs 4794163953Srrsvoid 4795165647Srrssctp_input(i_pak, off) 4796165647Srrs struct mbuf *i_pak; 4797163953Srrs int off; 4798163953Srrs 4799163953Srrs{ 4800163953Srrs#ifdef SCTP_MBUF_LOGGING 4801163953Srrs struct mbuf *mat; 4802163953Srrs 4803163953Srrs#endif 4804165647Srrs struct mbuf *m; 4805163953Srrs int iphlen; 4806170181Srrs uint32_t vrf_id = 0; 4807163953Srrs uint8_t ecn_bits; 4808163953Srrs struct ip *ip; 4809163953Srrs struct sctphdr *sh; 4810163953Srrs struct sctp_inpcb *inp = NULL; 4811163953Srrs 4812163953Srrs uint32_t check, calc_check; 4813163953Srrs struct sctp_nets *net; 4814163953Srrs struct sctp_tcb *stcb = NULL; 4815163953Srrs struct sctp_chunkhdr *ch; 4816163953Srrs int refcount_up = 0; 4817163953Srrs int length, mlen, offset; 4818163953Srrs 4819169352Srrs 4820169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 4821169352Srrs SCTP_RELEASE_PKT(i_pak); 4822169352Srrs return; 4823169352Srrs } 4824168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 4825163953Srrs iphlen = off; 4826165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4827170056Srrs 4828163953Srrs net = NULL; 4829163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4830163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4831163953Srrs 4832170091Srrs 4833163953Srrs#ifdef SCTP_MBUF_LOGGING 4834163953Srrs /* Log in any input mbufs */ 4835170744Srrs if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) { 4836170744Srrs mat = m; 4837170744Srrs while (mat) { 4838170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4839170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4840170744Srrs } 4841170744Srrs mat = SCTP_BUF_NEXT(mat); 4842163953Srrs } 4843163953Srrs } 4844163953Srrs#endif 4845170091Srrs#ifdef SCTP_PACKET_LOGGING 4846170921Srrs if (sctp_logging_level & SCTP_LAST_PACKET_TRACING) 4847170921Srrs sctp_packet_log(m, mlen); 4848170091Srrs#endif 4849170091Srrs /* 4850170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 4851170091Srrs * case) 4852170091Srrs */ 4853170091Srrs mlen -= iphlen; 4854168124Srrs 4855163953Srrs /* 4856163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4857163953Srrs */ 4858163953Srrs ip = mtod(m, struct ip *); 4859163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4860165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4861163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4862163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4863163953Srrs return; 4864163953Srrs } 4865163953Srrs ip = mtod(m, struct ip *); 4866163953Srrs } 4867163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4868163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4869170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4870170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 4871163953Srrs 4872163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4873163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4874163953Srrs goto bad; 4875163953Srrs } 4876168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4877163953Srrs /* 4878163953Srrs * We only look at broadcast if its a front state, All 4879163953Srrs * others we will not have a tcb for anyway. 4880163953Srrs */ 4881163953Srrs goto bad; 4882163953Srrs } 4883163953Srrs /* validate SCTP checksum */ 4884168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4885163953Srrs /* 4886163953Srrs * we do NOT validate things from the loopback if the sysctl 4887163953Srrs * is set to 1. 4888163953Srrs */ 4889163953Srrs check = sh->checksum; /* save incoming checksum */ 4890163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4891163953Srrs /* 4892163953Srrs * special hook for where we got a local address 4893163953Srrs * somehow routed across a non IFT_LOOP type 4894163953Srrs * interface 4895163953Srrs */ 4896163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4897163953Srrs goto sctp_skip_csum_4; 4898163953Srrs } 4899163953Srrs sh->checksum = 0; /* prepare for calc */ 4900163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4901163953Srrs if (calc_check != check) { 4902169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4903169420Srrs calc_check, check, m, mlen, iphlen); 4904163953Srrs 4905163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4906163953Srrs offset - sizeof(*ch), 4907169420Srrs sh, ch, &inp, &net, 4908169420Srrs vrf_id); 4909163953Srrs if ((inp) && (stcb)) { 4910163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4911163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4912163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4913163953Srrs refcount_up = 1; 4914163953Srrs } 4915163953Srrs SCTP_STAT_INCR(sctps_badsum); 4916163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4917163953Srrs goto bad; 4918163953Srrs } 4919163953Srrs sh->checksum = calc_check; 4920168299Srrs } 4921163953Srrssctp_skip_csum_4: 4922168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4923168124Srrs if (sh->dest_port == 0) { 4924168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4925168124Srrs goto bad; 4926168124Srrs } 4927163953Srrs /* validate mbuf chain length with IP payload length */ 4928163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4929163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4930163953Srrs goto bad; 4931163953Srrs } 4932163953Srrs /* 4933163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4934163953Srrs * IP/SCTP/first chunk header... 4935163953Srrs */ 4936163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4937168299Srrs sh, ch, &inp, &net, vrf_id); 4938163953Srrs /* inp's ref-count increased && stcb locked */ 4939163953Srrs if (inp == NULL) { 4940163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4941163953Srrs 4942163953Srrs SCTP_STAT_INCR(sctps_noport); 4943163953Srrs#ifdef ICMP_BANDLIM 4944163953Srrs /* 4945163953Srrs * we use the bandwidth limiting to protect against sending 4946163953Srrs * too many ABORTS all at once. In this case these count the 4947163953Srrs * same as an ICMP message. 4948163953Srrs */ 4949163953Srrs if (badport_bandlim(0) < 0) 4950163953Srrs goto bad; 4951163953Srrs#endif /* ICMP_BANDLIM */ 4952169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4953169420Srrs "Sending a ABORT from packet entry!\n"); 4954163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4955163953Srrs /* 4956163953Srrs * we do a trick here to get the INIT tag, dig in 4957163953Srrs * and get the tag from the INIT and put it in the 4958163953Srrs * common header. 4959163953Srrs */ 4960163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4961163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4962163953Srrs (uint8_t *) & chunk_buf); 4963163953Srrs if (init_chk != NULL) 4964163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4965163953Srrs } 4966165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4967170181Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id); 4968165220Srrs goto bad; 4969165220Srrs } 4970165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4971165220Srrs goto bad; 4972165220Srrs } 4973165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4974170181Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id); 4975163953Srrs goto bad; 4976163953Srrs } else if (stcb == NULL) { 4977163953Srrs refcount_up = 1; 4978163953Srrs } 4979171167Sgnn#ifdef IPSEC 4980163953Srrs /* 4981163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 4982163953Srrs * idea, so I will leave it in place. 4983163953Srrs */ 4984163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 4985171133Sgnn ipsec4stat.in_polvio++; 4986163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4987163953Srrs goto bad; 4988163953Srrs } 4989163953Srrs#endif /* IPSEC */ 4990163953Srrs 4991163953Srrs /* 4992163953Srrs * common chunk processing 4993163953Srrs */ 4994163953Srrs length = ip->ip_len + iphlen; 4995163953Srrs offset -= sizeof(struct sctp_chunkhdr); 4996163953Srrs 4997163953Srrs ecn_bits = ip->ip_tos; 4998163953Srrs 4999169655Srrs /* sa_ignore NO_NULL_CHK */ 5000163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5001170181Srrs inp, stcb, net, ecn_bits, vrf_id); 5002163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5003163953Srrs if (m) { 5004163953Srrs sctp_m_freem(m); 5005163953Srrs } 5006163953Srrs if ((inp) && (refcount_up)) { 5007163953Srrs /* reduce ref-count */ 5008163953Srrs SCTP_INP_DECR_REF(inp); 5009163953Srrs } 5010163953Srrs return; 5011163953Srrsbad: 5012169420Srrs if (stcb) { 5013163953Srrs SCTP_TCB_UNLOCK(stcb); 5014169420Srrs } 5015163953Srrs if ((inp) && (refcount_up)) { 5016163953Srrs /* reduce ref-count */ 5017163953Srrs SCTP_INP_DECR_REF(inp); 5018163953Srrs } 5019163953Srrs if (m) { 5020163953Srrs sctp_m_freem(m); 5021163953Srrs } 5022163953Srrs return; 5023163953Srrs} 5024