sctp_input.c revision 171745
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 171745 2007-08-06 15:46:46Z 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... */ 428163953Srrs stcb->asoc.overall_error_count = 0; 429163953Srrs net->error_count = 0; 430163953Srrs 431163953Srrs /* 432163953Srrs * Cancel the INIT timer, We do this first before queueing the 433163953Srrs * cookie. We always cancel at the primary to assue that we are 434163953Srrs * canceling the timer started by the INIT which always goes to the 435163953Srrs * primary. 436163953Srrs */ 437163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 438165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 439163953Srrs 440165220Srrs /* calculate the RTO */ 441171477Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy); 442165220Srrs 443163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 444163953Srrs if (retval < 0) { 445163953Srrs /* 446163953Srrs * No cookie, we probably should send a op error. But in any 447163953Srrs * case if there is no cookie in the INIT-ACK, we can 448163953Srrs * abandon the peer, its broke. 449163953Srrs */ 450163953Srrs if (retval == -3) { 451163953Srrs /* We abort with an error of missing mandatory param */ 452163953Srrs op_err = 453163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 454163953Srrs if (op_err) { 455163953Srrs /* 456163953Srrs * Expand beyond to include the mandatory 457163953Srrs * param cookie 458163953Srrs */ 459163953Srrs struct sctp_inv_mandatory_param *mp; 460163953Srrs 461165647Srrs SCTP_BUF_LEN(op_err) = 462163953Srrs sizeof(struct sctp_inv_mandatory_param); 463163953Srrs mp = mtod(op_err, 464163953Srrs struct sctp_inv_mandatory_param *); 465163953Srrs /* Subtract the reserved param */ 466163953Srrs mp->length = 467163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 468163953Srrs mp->num_param = htonl(1); 469163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 470163953Srrs mp->resv = 0; 471163953Srrs } 472163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 473170181Srrs sh, op_err, 0); 474168299Srrs *abort_no_unlock = 1; 475163953Srrs } 476163953Srrs return (retval); 477163953Srrs } 478163953Srrs return (0); 479163953Srrs} 480163953Srrs 481163953Srrsstatic void 482163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 483163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 484163953Srrs{ 485163953Srrs struct sockaddr_storage store; 486163953Srrs struct sockaddr_in *sin; 487163953Srrs struct sockaddr_in6 *sin6; 488163953Srrs struct sctp_nets *r_net; 489163953Srrs struct timeval tv; 490163953Srrs 491163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 492163953Srrs /* Invalid length */ 493163953Srrs return; 494163953Srrs } 495163953Srrs sin = (struct sockaddr_in *)&store; 496163953Srrs sin6 = (struct sockaddr_in6 *)&store; 497163953Srrs 498163953Srrs memset(&store, 0, sizeof(store)); 499163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 500163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 501163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 502163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 503163953Srrs sin->sin_port = stcb->rport; 504163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 505163953Srrs sizeof(sin->sin_addr)); 506163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 507163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 508163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 509163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 510163953Srrs sin6->sin6_port = stcb->rport; 511163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 512163953Srrs sizeof(sin6->sin6_addr)); 513163953Srrs } else { 514163953Srrs return; 515163953Srrs } 516163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 517163953Srrs if (r_net == NULL) { 518169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 519163953Srrs return; 520163953Srrs } 521163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 522163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 523163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 524163953Srrs /* 525163953Srrs * If the its a HB and it's random value is correct when can 526163953Srrs * confirm the destination. 527163953Srrs */ 528163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 529167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 530167598Srrs stcb->asoc.primary_destination = r_net; 531167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 532167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 533167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 534167598Srrs if (r_net != stcb->asoc.primary_destination) { 535167598Srrs /* 536167598Srrs * first one on the list is NOT the primary 537167598Srrs * sctp_cmpaddr() is much more efficent if 538167598Srrs * the primary is the first on the list, 539167598Srrs * make it so. 540167598Srrs */ 541167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 542167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 543167598Srrs } 544167598Srrs } 545163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 546163953Srrs stcb, 0, (void *)r_net); 547163953Srrs } 548163953Srrs r_net->error_count = 0; 549163953Srrs r_net->hb_responded = 1; 550163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 551163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 552163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 553163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 554163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 555163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 556163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 557163953Srrs /* now was it the primary? if so restore */ 558163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 559169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 560163953Srrs } 561163953Srrs } 562171440Srrs /* 563171440Srrs * JRS 5/14/07 - If CMT PF is on and the destination is in PF state, 564171440Srrs * set the destination to active state and set the cwnd to one or 565171440Srrs * two MTU's based on whether PF1 or PF2 is being used. If a T3 566171440Srrs * timer is running, for the destination, stop the timer because a 567171440Srrs * PF-heartbeat was received. 568171440Srrs */ 569171477Srrs if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == 570171440Srrs SCTP_ADDR_PF) { 571171440Srrs if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 572171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 573171440Srrs stcb, net, 574171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 575171440Srrs } 576171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 577171440Srrs net->cwnd = net->mtu * sctp_cmt_pf; 578171440Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n", 579171440Srrs net, net->cwnd); 580171440Srrs } 581163953Srrs /* Now lets do a RTO with this */ 582171477Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy); 583163953Srrs} 584163953Srrs 585163953Srrsstatic void 586163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 587163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 588163953Srrs{ 589169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 590163953Srrs if (stcb == NULL) 591163953Srrs return; 592163953Srrs 593163953Srrs /* stop any receive timers */ 594171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 595163953Srrs /* notify user of the abort and clean up... */ 596163953Srrs sctp_abort_notification(stcb, 0); 597163953Srrs /* free the tcb */ 598163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 599163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 600163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 601163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 602163953Srrs } 603168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 604168859Srrs sctp_print_out_track_log(stcb); 605168859Srrs#endif 606165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 607169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 608163953Srrs} 609163953Srrs 610163953Srrsstatic void 611163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 612163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 613163953Srrs{ 614163953Srrs struct sctp_association *asoc; 615163953Srrs int some_on_streamwheel; 616163953Srrs 617169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 618169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 619163953Srrs if (stcb == NULL) 620163953Srrs return; 621165220Srrs asoc = &stcb->asoc; 622165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 623165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 624163953Srrs return; 625163953Srrs } 626163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 627163953Srrs /* Shutdown NOT the expected size */ 628163953Srrs return; 629163953Srrs } else { 630163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 631163953Srrs } 632165220Srrs if (asoc->control_pdapi) { 633163953Srrs /* 634163953Srrs * With a normal shutdown we assume the end of last record. 635163953Srrs */ 636163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 637165220Srrs asoc->control_pdapi->end_added = 1; 638165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 639165220Srrs asoc->control_pdapi = NULL; 640163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 641163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 642163953Srrs } 643163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 644163953Srrs if (stcb->sctp_socket) { 645163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 646170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 647163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 648163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED; 649163953Srrs /* 650163953Srrs * notify upper layer that peer has initiated a 651163953Srrs * shutdown 652163953Srrs */ 653163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 654163953Srrs 655163953Srrs /* reset time */ 656169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 657163953Srrs } 658163953Srrs } 659163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 660163953Srrs /* 661163953Srrs * stop the shutdown timer, since we WILL move to 662163953Srrs * SHUTDOWN-ACK-SENT. 663163953Srrs */ 664171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 665163953Srrs } 666171158Srrs /* Now is there unsent data on a stream somewhere? */ 667171158Srrs some_on_streamwheel = sctp_is_there_unsent_data(stcb); 668163953Srrs 669163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 670163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 671163953Srrs some_on_streamwheel) { 672163953Srrs /* By returning we will push more data out */ 673163953Srrs return; 674163953Srrs } else { 675163953Srrs /* no outstanding data to send, so move on... */ 676163953Srrs /* send SHUTDOWN-ACK */ 677163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 678163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 679166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 680166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 681163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 682163953Srrs } 683163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT; 684163953Srrs 685163953Srrs /* start SHUTDOWN timer */ 686163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 687163953Srrs stcb, net); 688163953Srrs } 689163953Srrs} 690163953Srrs 691163953Srrsstatic void 692163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 693163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 694163953Srrs{ 695163953Srrs struct sctp_association *asoc; 696163953Srrs 697169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 698169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 699163953Srrs if (stcb == NULL) 700163953Srrs return; 701163953Srrs 702163953Srrs asoc = &stcb->asoc; 703163953Srrs /* process according to association state */ 704163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 705163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 706163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 707163953Srrs SCTP_TCB_UNLOCK(stcb); 708163953Srrs return; 709163953Srrs } 710165220Srrs if (asoc->control_pdapi) { 711163953Srrs /* 712163953Srrs * With a normal shutdown we assume the end of last record. 713163953Srrs */ 714163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 715165220Srrs asoc->control_pdapi->end_added = 1; 716165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 717165220Srrs asoc->control_pdapi = NULL; 718163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 719163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 720163953Srrs } 721163953Srrs /* are the queues empty? */ 722163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 723163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 724163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 725164205Srrs sctp_report_all_outbound(stcb, 0); 726163953Srrs } 727163953Srrs /* stop the timer */ 728171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 729163953Srrs /* send SHUTDOWN-COMPLETE */ 730163953Srrs sctp_send_shutdown_complete(stcb, net); 731163953Srrs /* notify upper layer protocol */ 732163953Srrs if (stcb->sctp_socket) { 733163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 734163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 735163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 736163953Srrs /* Set the connected flag to disconnected */ 737163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 738163953Srrs } 739163953Srrs } 740163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 741163953Srrs /* free the TCB but first save off the ep */ 742165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 743171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 744163953Srrs} 745163953Srrs 746163953Srrs/* 747163953Srrs * Skip past the param header and then we will find the chunk that caused the 748163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 749163953Srrs * our peer must be broken. 750163953Srrs */ 751163953Srrsstatic void 752163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 753163953Srrs struct sctp_nets *net) 754163953Srrs{ 755163953Srrs struct sctp_chunkhdr *chk; 756163953Srrs 757163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 758163953Srrs switch (chk->chunk_type) { 759163953Srrs case SCTP_ASCONF_ACK: 760163953Srrs case SCTP_ASCONF: 761163953Srrs sctp_asconf_cleanup(stcb, net); 762163953Srrs break; 763163953Srrs case SCTP_FORWARD_CUM_TSN: 764163953Srrs stcb->asoc.peer_supports_prsctp = 0; 765163953Srrs break; 766163953Srrs default: 767169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 768169420Srrs "Peer does not support chunk type %d(%x)??\n", 769169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 770163953Srrs break; 771163953Srrs } 772163953Srrs} 773163953Srrs 774163953Srrs/* 775163953Srrs * Skip past the param header and then we will find the param that caused the 776163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 777163953Srrs * these will turn of specific features. 778163953Srrs */ 779163953Srrsstatic void 780163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 781163953Srrs{ 782163953Srrs struct sctp_paramhdr *pbad; 783163953Srrs 784163953Srrs pbad = phdr + 1; 785163953Srrs switch (ntohs(pbad->param_type)) { 786163953Srrs /* pr-sctp draft */ 787163953Srrs case SCTP_PRSCTP_SUPPORTED: 788163953Srrs stcb->asoc.peer_supports_prsctp = 0; 789163953Srrs break; 790163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 791163953Srrs break; 792163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 793163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 794163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 795163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 796163953Srrs stcb->asoc.ecn_allowed = 0; 797163953Srrs break; 798163953Srrs case SCTP_ADD_IP_ADDRESS: 799163953Srrs case SCTP_DEL_IP_ADDRESS: 800163953Srrs case SCTP_SET_PRIM_ADDR: 801163953Srrs stcb->asoc.peer_supports_asconf = 0; 802163953Srrs break; 803163953Srrs case SCTP_SUCCESS_REPORT: 804163953Srrs case SCTP_ERROR_CAUSE_IND: 805169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 806169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 807169420Srrs "Turning off ASCONF to this strange peer\n"); 808163953Srrs stcb->asoc.peer_supports_asconf = 0; 809163953Srrs break; 810163953Srrs default: 811169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 812169420Srrs "Peer does not support param type %d(%x)??\n", 813169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 814163953Srrs break; 815163953Srrs } 816163953Srrs} 817163953Srrs 818163953Srrsstatic int 819163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 820163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 821163953Srrs{ 822163953Srrs int chklen; 823163953Srrs struct sctp_paramhdr *phdr; 824163953Srrs uint16_t error_type; 825163953Srrs uint16_t error_len; 826163953Srrs struct sctp_association *asoc; 827163953Srrs 828163953Srrs int adjust; 829163953Srrs 830163953Srrs /* parse through all of the errors and process */ 831163953Srrs asoc = &stcb->asoc; 832163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 833163953Srrs sizeof(struct sctp_chunkhdr)); 834163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 835163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 836163953Srrs /* Process an Error Cause */ 837163953Srrs error_type = ntohs(phdr->param_type); 838163953Srrs error_len = ntohs(phdr->param_length); 839163953Srrs if ((error_len > chklen) || (error_len == 0)) { 840163953Srrs /* invalid param length for this param */ 841169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 842169420Srrs chklen, error_len); 843163953Srrs return (0); 844163953Srrs } 845163953Srrs switch (error_type) { 846163953Srrs case SCTP_CAUSE_INVALID_STREAM: 847163953Srrs case SCTP_CAUSE_MISSING_PARAM: 848163953Srrs case SCTP_CAUSE_INVALID_PARAM: 849163953Srrs case SCTP_CAUSE_NO_USER_DATA: 850169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 851169420Srrs error_type); 852163953Srrs break; 853163953Srrs case SCTP_CAUSE_STALE_COOKIE: 854163953Srrs /* 855163953Srrs * We only act if we have echoed a cookie and are 856163953Srrs * waiting. 857163953Srrs */ 858163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 859163953Srrs int *p; 860163953Srrs 861163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 862163953Srrs /* Save the time doubled */ 863163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 864163953Srrs asoc->stale_cookie_count++; 865163953Srrs if (asoc->stale_cookie_count > 866163953Srrs asoc->max_init_times) { 867163953Srrs sctp_abort_notification(stcb, 0); 868163953Srrs /* now free the asoc */ 869171440Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 870163953Srrs return (-1); 871163953Srrs } 872163953Srrs /* blast back to INIT state */ 873163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 874163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 875163953Srrs 876163953Srrs sctp_stop_all_cookie_timers(stcb); 877163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 878163953Srrs } 879163953Srrs break; 880163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 881163953Srrs /* 882163953Srrs * Nothing we can do here, we don't do hostname 883163953Srrs * addresses so if the peer does not like my IPv6 884163953Srrs * (or IPv4 for that matter) it does not matter. If 885163953Srrs * they don't support that type of address, they can 886163953Srrs * NOT possibly get that packet type... i.e. with no 887163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 888163953Srrs * safely ignore this one. If we ever added support 889163953Srrs * for HOSTNAME Addresses, then we would need to do 890163953Srrs * something here. 891163953Srrs */ 892163953Srrs break; 893163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 894163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 895163953Srrs break; 896163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 897163953Srrs sctp_process_unrecog_param(stcb, phdr); 898163953Srrs break; 899163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 900163953Srrs /* 901163953Srrs * We ignore this since the timer will drive out a 902163953Srrs * new cookie anyway and there timer will drive us 903163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 904163953Srrs * here since we don't have their tag. 905163953Srrs */ 906163953Srrs break; 907163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 908163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 909163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 910163953Srrs /* 911163953Srrs * We should NOT get these here, but in a 912166086Srrs * ASCONF-ACK. 913163953Srrs */ 914169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 915169420Srrs error_type); 916163953Srrs break; 917163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 918163953Srrs /* 919163953Srrs * And what, pray tell do we do with the fact that 920163953Srrs * the peer is out of resources? Not really sure we 921166086Srrs * could do anything but abort. I suspect this 922163953Srrs * should have came WITH an abort instead of in a 923163953Srrs * OP-ERROR. 924163953Srrs */ 925163953Srrs break; 926163953Srrs default: 927169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 928169420Srrs error_type); 929163953Srrs break; 930163953Srrs } 931163953Srrs adjust = SCTP_SIZE32(error_len); 932163953Srrs chklen -= adjust; 933163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 934163953Srrs } 935163953Srrs return (0); 936163953Srrs} 937163953Srrs 938163953Srrsstatic int 939169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 940169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 941170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 942163953Srrs{ 943163953Srrs struct sctp_init_ack *init_ack; 944163953Srrs int *state; 945163953Srrs struct mbuf *op_err; 946163953Srrs 947169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 948169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 949169420Srrs 950163953Srrs if (stcb == NULL) { 951169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 952169420Srrs "sctp_handle_init_ack: TCB is null\n"); 953163953Srrs return (-1); 954163953Srrs } 955163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 956163953Srrs /* Invalid length */ 957163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 958163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 959170181Srrs op_err, 0); 960168299Srrs *abort_no_unlock = 1; 961163953Srrs return (-1); 962163953Srrs } 963163953Srrs init_ack = &cp->init; 964163953Srrs /* validate parameters */ 965163953Srrs if (init_ack->initiate_tag == 0) { 966163953Srrs /* protocol error... send an abort */ 967163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 968163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 969170181Srrs op_err, 0); 970168299Srrs *abort_no_unlock = 1; 971163953Srrs return (-1); 972163953Srrs } 973163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 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 (init_ack->num_inbound_streams == 0) { 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_outbound_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 /* process according to association state... */ 998163953Srrs state = &stcb->asoc.state; 999163953Srrs switch (*state & SCTP_STATE_MASK) { 1000163953Srrs case SCTP_STATE_COOKIE_WAIT: 1001163953Srrs /* this is the expected state for this chunk */ 1002163953Srrs /* process the INIT-ACK parameters */ 1003163953Srrs if (stcb->asoc.primary_destination->dest_state & 1004163953Srrs SCTP_ADDR_UNCONFIRMED) { 1005163953Srrs /* 1006163953Srrs * The primary is where we sent the INIT, we can 1007163953Srrs * always consider it confirmed when the INIT-ACK is 1008163953Srrs * returned. Do this before we load addresses 1009163953Srrs * though. 1010163953Srrs */ 1011163953Srrs stcb->asoc.primary_destination->dest_state &= 1012163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1013163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1014163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 1015163953Srrs } 1016169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1017170181Srrs net, abort_no_unlock, vrf_id) < 0) { 1018163953Srrs /* error in parsing parameters */ 1019163953Srrs return (-1); 1020163953Srrs } 1021163953Srrs /* update our state */ 1022169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1023163953Srrs if (*state & SCTP_STATE_SHUTDOWN_PENDING) { 1024163953Srrs *state = SCTP_STATE_COOKIE_ECHOED | 1025163953Srrs SCTP_STATE_SHUTDOWN_PENDING; 1026163953Srrs } else { 1027163953Srrs *state = SCTP_STATE_COOKIE_ECHOED; 1028163953Srrs } 1029163953Srrs 1030163953Srrs /* reset the RTO calc */ 1031163953Srrs stcb->asoc.overall_error_count = 0; 1032169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1033163953Srrs /* 1034163953Srrs * collapse the init timer back in case of a exponential 1035165220Srrs * backoff 1036163953Srrs */ 1037163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1038163953Srrs stcb, net); 1039163953Srrs /* 1040163953Srrs * the send at the end of the inbound data processing will 1041163953Srrs * cause the cookie to be sent 1042163953Srrs */ 1043163953Srrs break; 1044163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1045163953Srrs /* incorrect state... discard */ 1046163953Srrs break; 1047163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1048163953Srrs /* incorrect state... discard */ 1049163953Srrs break; 1050163953Srrs case SCTP_STATE_OPEN: 1051163953Srrs /* incorrect state... discard */ 1052163953Srrs break; 1053163953Srrs case SCTP_STATE_EMPTY: 1054163953Srrs case SCTP_STATE_INUSE: 1055163953Srrs default: 1056163953Srrs /* incorrect state... discard */ 1057163953Srrs return (-1); 1058163953Srrs break; 1059163953Srrs } 1060169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1061163953Srrs return (0); 1062163953Srrs} 1063163953Srrs 1064163953Srrs 1065163953Srrs/* 1066163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1067163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1068163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1069163953Srrs * mbuf to the cookie-echo chunk 1070163953Srrs */ 1071163953Srrsstatic struct sctp_tcb * 1072163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1073163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1074163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1075169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1076170181Srrs uint32_t vrf_id) 1077163953Srrs{ 1078163953Srrs struct sctp_association *asoc; 1079163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1080163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1081163953Srrs int chk_length; 1082165220Srrs int init_offset, initack_offset, i; 1083163953Srrs int retval; 1084164205Srrs int spec_flag = 0; 1085170140Srrs uint32_t how_indx; 1086163953Srrs 1087163953Srrs /* I know that the TCB is non-NULL from the caller */ 1088163953Srrs asoc = &stcb->asoc; 1089166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1090166023Srrs if (asoc->cookie_how[how_indx] == 0) 1091166023Srrs break; 1092166023Srrs } 1093166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1094166023Srrs asoc->cookie_how[how_indx] = 1; 1095166023Srrs } 1096163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1097163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1098163953Srrs struct mbuf *op_err; 1099163953Srrs struct sctp_paramhdr *ph; 1100163953Srrs 1101163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1102163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1103165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1104163953Srrs if (op_err == NULL) { 1105163953Srrs /* FOOBAR */ 1106163953Srrs return (NULL); 1107163953Srrs } 1108163953Srrs /* pre-reserve some space */ 1109165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1110165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1111165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1112163953Srrs /* Set the len */ 1113165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1114163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1115163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1116163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1117169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1118170181Srrs vrf_id); 1119166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1120166023Srrs asoc->cookie_how[how_indx] = 2; 1121163953Srrs return (NULL); 1122163953Srrs } 1123163953Srrs /* 1124163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1125163953Srrs * INIT should start after the cookie-echo header struct (chunk 1126163953Srrs * header, state cookie header struct) 1127163953Srrs */ 1128163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1129163953Srrs 1130163953Srrs init_cp = (struct sctp_init_chunk *) 1131163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1132163953Srrs (uint8_t *) & init_buf); 1133163953Srrs if (init_cp == NULL) { 1134163953Srrs /* could not pull a INIT chunk in cookie */ 1135163953Srrs return (NULL); 1136163953Srrs } 1137163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1138163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1139163953Srrs return (NULL); 1140163953Srrs } 1141163953Srrs /* 1142163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1143163953Srrs * INIT-ACK follows the INIT chunk 1144163953Srrs */ 1145163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1146163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1147163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1148163953Srrs (uint8_t *) & initack_buf); 1149163953Srrs if (initack_cp == NULL) { 1150163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1151163953Srrs return (NULL); 1152163953Srrs } 1153163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1154163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1155163953Srrs return (NULL); 1156163953Srrs } 1157163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1158163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1159163953Srrs /* 1160163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1161163953Srrs * to get into the OPEN state 1162163953Srrs */ 1163166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1164166086Srrs#ifdef INVARIANTS 1165166023Srrs panic("Case D and non-match seq?"); 1166166086Srrs#else 1167169420Srrs SCTP_PRINTF("Case D, seq non-match %x vs %x?\n", 1168169420Srrs ntohl(initack_cp->init.initial_tsn), 1169169420Srrs asoc->init_seq_number); 1170166086Srrs#endif 1171166023Srrs } 1172163953Srrs switch SCTP_GET_STATE 1173163953Srrs (asoc) { 1174163953Srrs case SCTP_STATE_COOKIE_WAIT: 1175166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1176163953Srrs /* 1177169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1178166023Srrs * correct tags... just accept it...but we must 1179166023Srrs * process the init so that we can make sure we have 1180166023Srrs * the right seq no's. 1181163953Srrs */ 1182163953Srrs /* First we must process the INIT !! */ 1183163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1184163953Srrs if (retval < 0) { 1185166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1186166023Srrs asoc->cookie_how[how_indx] = 3; 1187163953Srrs return (NULL); 1188163953Srrs } 1189163953Srrs /* we have already processed the INIT so no problem */ 1190163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1191171440Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1192171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1193163953Srrs /* update current state */ 1194166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1195166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1196166675Srrs else 1197166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1198163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1199166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1200163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1201163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1202163953Srrs 1203166675Srrs } else { 1204163953Srrs /* if ok, move to OPEN state */ 1205163953Srrs asoc->state = SCTP_STATE_OPEN; 1206163953Srrs } 1207166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1208165220Srrs sctp_stop_all_cookie_timers(stcb); 1209163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1210163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1211163953Srrs (inp->sctp_socket->so_qlimit == 0) 1212163953Srrs ) { 1213163953Srrs /* 1214163953Srrs * Here is where collision would go if we 1215163953Srrs * did a connect() and instead got a 1216163953Srrs * init/init-ack/cookie done before the 1217163953Srrs * init-ack came back.. 1218163953Srrs */ 1219163953Srrs stcb->sctp_ep->sctp_flags |= 1220163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1221163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1222163953Srrs } 1223163953Srrs /* notify upper layer */ 1224163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1225163953Srrs /* 1226163953Srrs * since we did not send a HB make sure we don't 1227163953Srrs * double things 1228163953Srrs */ 1229163953Srrs net->hb_responded = 1; 1230170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1231171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1232163953Srrs 1233163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1234163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1235163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1236163953Srrs inp, stcb, NULL); 1237163953Srrs } 1238163953Srrs break; 1239163953Srrs default: 1240163953Srrs /* 1241163953Srrs * we're in the OPEN state (or beyond), so peer must 1242163953Srrs * have simply lost the COOKIE-ACK 1243163953Srrs */ 1244163953Srrs break; 1245163953Srrs } /* end switch */ 1246165220Srrs sctp_stop_all_cookie_timers(stcb); 1247163953Srrs /* 1248163953Srrs * We ignore the return code here.. not sure if we should 1249163953Srrs * somehow abort.. but we do have an existing asoc. This 1250163953Srrs * really should not fail. 1251163953Srrs */ 1252163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1253163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1254163953Srrs initack_offset, sh, init_src)) { 1255166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1256166023Srrs asoc->cookie_how[how_indx] = 4; 1257163953Srrs return (NULL); 1258163953Srrs } 1259163953Srrs /* respond with a COOKIE-ACK */ 1260165220Srrs sctp_toss_old_cookies(stcb, asoc); 1261163953Srrs sctp_send_cookie_ack(stcb); 1262166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1263166023Srrs asoc->cookie_how[how_indx] = 5; 1264163953Srrs return (stcb); 1265169352Srrs } 1266163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1267163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1268163953Srrs cookie->tie_tag_my_vtag == 0 && 1269163953Srrs cookie->tie_tag_peer_vtag == 0) { 1270163953Srrs /* 1271163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1272163953Srrs */ 1273166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1274166023Srrs asoc->cookie_how[how_indx] = 6; 1275163953Srrs return (NULL); 1276163953Srrs } 1277163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1278163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1279163953Srrs init_cp->init.initiate_tag == 0)) { 1280163953Srrs /* 1281163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1282163953Srrs * should be ok, re-accept peer info 1283163953Srrs */ 1284166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1285166023Srrs /* 1286166023Srrs * Extension of case C. If we hit this, then the 1287166023Srrs * random number generator returned the same vtag 1288166023Srrs * when we first sent our INIT-ACK and when we later 1289166023Srrs * sent our INIT. The side with the seq numbers that 1290166023Srrs * are different will be the one that normnally 1291166023Srrs * would have hit case C. This in effect "extends" 1292166023Srrs * our vtags in this collision case to be 64 bits. 1293166023Srrs * The same collision could occur aka you get both 1294166023Srrs * vtag and seq number the same twice in a row.. but 1295166023Srrs * is much less likely. If it did happen then we 1296166023Srrs * would proceed through and bring up the assoc.. we 1297166023Srrs * may end up with the wrong stream setup however.. 1298166023Srrs * which would be bad.. but there is no way to 1299166023Srrs * tell.. until we send on a stream that does not 1300166023Srrs * exist :-) 1301166023Srrs */ 1302166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1303166023Srrs asoc->cookie_how[how_indx] = 7; 1304166023Srrs 1305166023Srrs return (NULL); 1306166023Srrs } 1307166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1308166023Srrs asoc->cookie_how[how_indx] = 8; 1309171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1310163953Srrs sctp_stop_all_cookie_timers(stcb); 1311163953Srrs /* 1312163953Srrs * since we did not send a HB make sure we don't double 1313163953Srrs * things 1314163953Srrs */ 1315163953Srrs net->hb_responded = 1; 1316163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1317163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1318163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1319163953Srrs NULL); 1320163953Srrs } 1321163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1322164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1323164205Srrs 1324164205Srrs /* Note last_cwr_tsn? where is this used? */ 1325163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1326164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1327164205Srrs /* 1328164205Srrs * Ok the peer probably discarded our data (if we 1329164205Srrs * echoed a cookie+data). So anything on the 1330164205Srrs * sent_queue should be marked for retransmit, we 1331164205Srrs * may not get something to kick us so it COULD 1332164205Srrs * still take a timeout to move these.. but it can't 1333164205Srrs * hurt to mark them. 1334164205Srrs */ 1335164205Srrs struct sctp_tmit_chunk *chk; 1336163953Srrs 1337164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1338164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1339164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1340171440Srrs sctp_flight_size_decrease(chk); 1341171440Srrs sctp_total_flight_decrease(stcb, chk); 1342168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1343164205Srrs spec_flag++; 1344164205Srrs } 1345164205Srrs } 1346164205Srrs 1347164205Srrs } 1348163953Srrs /* process the INIT info (peer's info) */ 1349163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1350163953Srrs if (retval < 0) { 1351166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1352166023Srrs asoc->cookie_how[how_indx] = 9; 1353163953Srrs return (NULL); 1354163953Srrs } 1355163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1356163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1357163953Srrs initack_offset, sh, init_src)) { 1358166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1359166023Srrs asoc->cookie_how[how_indx] = 10; 1360163953Srrs return (NULL); 1361163953Srrs } 1362163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1363163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1364163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1365163953Srrs 1366163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1367163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1368163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1369163953Srrs stcb->sctp_ep->sctp_flags |= 1370163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1371163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1372163953Srrs } 1373166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1374166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1375166675Srrs else 1376166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1377166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1378166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1379166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1380166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1381166675Srrs } else { 1382166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1383163953Srrs } 1384163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1385166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1386163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1387163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1388163953Srrs 1389163953Srrs } else { 1390163953Srrs asoc->state = SCTP_STATE_OPEN; 1391163953Srrs } 1392163953Srrs sctp_stop_all_cookie_timers(stcb); 1393165220Srrs sctp_toss_old_cookies(stcb, asoc); 1394163953Srrs sctp_send_cookie_ack(stcb); 1395164205Srrs if (spec_flag) { 1396164205Srrs /* 1397164205Srrs * only if we have retrans set do we do this. What 1398164205Srrs * this call does is get only the COOKIE-ACK out and 1399164205Srrs * then when we return the normal call to 1400164205Srrs * sctp_chunk_output will get the retrans out behind 1401164205Srrs * this. 1402164205Srrs */ 1403164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1404164205Srrs } 1405166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1406166023Srrs asoc->cookie_how[how_indx] = 11; 1407166023Srrs 1408163953Srrs return (stcb); 1409163953Srrs } 1410163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1411163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1412163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1413163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1414163953Srrs cookie->tie_tag_peer_vtag != 0) { 1415163953Srrs struct sctpasochead *head; 1416163953Srrs 1417163953Srrs /* 1418163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1419163953Srrs */ 1420165220Srrs /* temp code */ 1421166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1422166023Srrs asoc->cookie_how[how_indx] = 12; 1423171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1424171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1425165647Srrs 1426163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1427163953Srrs /* notify upper layer */ 1428163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1429165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1430166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1431166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1432166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1433166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1434166675Srrs } 1435166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1436166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1437166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1438166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1439166675Srrs } 1440165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1441165647Srrs asoc->state = SCTP_STATE_OPEN | 1442165647Srrs SCTP_STATE_SHUTDOWN_PENDING; 1443165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1444165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1445165647Srrs 1446165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1447165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1448165647Srrs asoc->state = SCTP_STATE_OPEN; 1449165647Srrs } 1450165647Srrs asoc->pre_open_streams = 1451165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1452165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1453165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1454165647Srrs 1455165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1456165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1457165647Srrs 1458165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1459165647Srrs 1460165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1461170138Srrs if (asoc->mapping_array) { 1462165647Srrs memset(asoc->mapping_array, 0, 1463165647Srrs asoc->mapping_array_size); 1464170138Srrs } 1465165220Srrs SCTP_TCB_UNLOCK(stcb); 1466165220Srrs SCTP_INP_INFO_WLOCK(); 1467165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1468165220Srrs SCTP_TCB_LOCK(stcb); 1469165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1470163953Srrs /* send up all the data */ 1471164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1472165220Srrs 1473164205Srrs sctp_report_all_outbound(stcb, 1); 1474165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1475165220Srrs stcb->asoc.strmout[i].stream_no = i; 1476165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1477165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1478165220Srrs } 1479163953Srrs /* process the INIT-ACK info (my info) */ 1480163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1481163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1482163953Srrs 1483163953Srrs /* pull from vtag hash */ 1484163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1485163953Srrs /* re-insert to new vtag position */ 1486163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1487163953Srrs sctppcbinfo.hashasocmark)]; 1488163953Srrs /* 1489163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1490163953Srrs * system 1491163953Srrs */ 1492163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1493163953Srrs 1494163953Srrs /* Is this the first restart? */ 1495163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1496163953Srrs /* Ok add it to assoc_id vtag hash */ 1497163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1498163953Srrs sctppcbinfo.hashrestartmark)]; 1499163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1500163953Srrs stcb->asoc.in_restart_hash = 1; 1501163953Srrs } 1502163953Srrs /* process the INIT info (peer's info) */ 1503164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1504165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1505165220Srrs SCTP_INP_INFO_WUNLOCK(); 1506165220Srrs 1507163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1508163953Srrs if (retval < 0) { 1509166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1510166023Srrs asoc->cookie_how[how_indx] = 13; 1511166023Srrs 1512163953Srrs return (NULL); 1513163953Srrs } 1514163953Srrs /* 1515163953Srrs * since we did not send a HB make sure we don't double 1516163953Srrs * things 1517163953Srrs */ 1518163953Srrs net->hb_responded = 1; 1519163953Srrs 1520163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1521163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1522163953Srrs initack_offset, sh, init_src)) { 1523166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1524166023Srrs asoc->cookie_how[how_indx] = 14; 1525166023Srrs 1526163953Srrs return (NULL); 1527163953Srrs } 1528163953Srrs /* respond with a COOKIE-ACK */ 1529163953Srrs sctp_stop_all_cookie_timers(stcb); 1530165220Srrs sctp_toss_old_cookies(stcb, asoc); 1531163953Srrs sctp_send_cookie_ack(stcb); 1532166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1533166023Srrs asoc->cookie_how[how_indx] = 15; 1534163953Srrs 1535163953Srrs return (stcb); 1536163953Srrs } 1537166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1538166023Srrs asoc->cookie_how[how_indx] = 16; 1539163953Srrs /* all other cases... */ 1540163953Srrs return (NULL); 1541163953Srrs} 1542163953Srrs 1543166086Srrs 1544163953Srrs/* 1545163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1546163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1547163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1548163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1549163953Srrs * was from returns a new TCB 1550163953Srrs */ 1551163953Srrsstatic struct sctp_tcb * 1552163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1553163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1554163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1555163953Srrs struct sockaddr *init_src, int *notification, 1556169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1557170181Srrs uint32_t vrf_id) 1558163953Srrs{ 1559163953Srrs struct sctp_tcb *stcb; 1560163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1561163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1562163953Srrs struct sockaddr_storage sa_store; 1563163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1564163953Srrs struct sockaddr_in *sin; 1565163953Srrs struct sockaddr_in6 *sin6; 1566163953Srrs struct sctp_association *asoc; 1567163953Srrs int chk_length; 1568163953Srrs int init_offset, initack_offset, initack_limit; 1569163953Srrs int retval; 1570163953Srrs int error = 0; 1571163953Srrs uint32_t old_tag; 1572166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1573163953Srrs 1574163953Srrs /* 1575163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1576163953Srrs * INIT should start after the cookie-echo header struct (chunk 1577163953Srrs * header, state cookie header struct) 1578163953Srrs */ 1579163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1580163953Srrs init_cp = (struct sctp_init_chunk *) 1581163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1582163953Srrs (uint8_t *) & init_buf); 1583163953Srrs if (init_cp == NULL) { 1584163953Srrs /* could not pull a INIT chunk in cookie */ 1585169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1586169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1587163953Srrs return (NULL); 1588163953Srrs } 1589163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1590163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1591169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1592163953Srrs return (NULL); 1593163953Srrs } 1594163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1595163953Srrs /* 1596163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1597163953Srrs * INIT-ACK follows the INIT chunk 1598163953Srrs */ 1599163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1600163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1601163953Srrs (uint8_t *) & initack_buf); 1602163953Srrs if (initack_cp == NULL) { 1603163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1604169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1605163953Srrs return (NULL); 1606163953Srrs } 1607163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1608163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1609163953Srrs return (NULL); 1610163953Srrs } 1611163953Srrs /* 1612163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1613163953Srrs * "initack_limit" value. This is because the chk_length field 1614163953Srrs * includes the length of the cookie, but the cookie is omitted when 1615163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1616163953Srrs */ 1617163953Srrs initack_limit = offset + cookie_len; 1618163953Srrs 1619163953Srrs /* 1620163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1621163953Srrs * and popluate 1622163953Srrs */ 1623171531Srrs 1624171531Srrs /* 1625171531Srrs * Here we do a trick, we set in NULL for the proc/thread argument. 1626171531Srrs * We do this since in effect we only use the p argument when the 1627171531Srrs * socket is unbound and we must do an implicit bind. Since we are 1628171531Srrs * getting a cookie, we cannot be unbound. 1629171531Srrs */ 1630163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1631171531Srrs ntohl(initack_cp->init.initiate_tag), vrf_id, 1632171531Srrs (struct thread *)NULL 1633171531Srrs ); 1634163953Srrs if (stcb == NULL) { 1635163953Srrs struct mbuf *op_err; 1636163953Srrs 1637163953Srrs /* memory problem? */ 1638169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1639169420Srrs "process_cookie_new: no room for another TCB!\n"); 1640163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1641170140Srrs 1642163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1643170181Srrs sh, op_err, vrf_id); 1644163953Srrs return (NULL); 1645163953Srrs } 1646163953Srrs /* get the correct sctp_nets */ 1647169420Srrs if (netp) 1648169420Srrs *netp = sctp_findnet(stcb, init_src); 1649169420Srrs 1650163953Srrs asoc = &stcb->asoc; 1651163953Srrs /* get scope variables out of cookie */ 1652163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1653163953Srrs asoc->site_scope = cookie->site_scope; 1654163953Srrs asoc->local_scope = cookie->local_scope; 1655163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1656163953Srrs 1657163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1658163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1659163953Srrs struct mbuf *op_err; 1660163953Srrs 1661163953Srrs /* 1662163953Srrs * Houston we have a problem. The EP changed while the 1663163953Srrs * cookie was in flight. Only recourse is to abort the 1664163953Srrs * association. 1665163953Srrs */ 1666170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1667163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1668163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1669170181Srrs sh, op_err, vrf_id); 1670171440Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 1671171440Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1672170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1673163953Srrs return (NULL); 1674163953Srrs } 1675163953Srrs /* process the INIT-ACK info (my info) */ 1676163953Srrs old_tag = asoc->my_vtag; 1677163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1678163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1679163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1680163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1681163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1682163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1683163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1684163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1685163953Srrs 1686163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1687163953Srrs 1688163953Srrs /* process the INIT info (peer's info) */ 1689169420Srrs if (netp) 1690169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1691169420Srrs else 1692169420Srrs retval = 0; 1693163953Srrs if (retval < 0) { 1694170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1695165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1696170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1697163953Srrs return (NULL); 1698163953Srrs } 1699163953Srrs /* load all addresses */ 1700163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1701163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1702163953Srrs init_src)) { 1703170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1704165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1705170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1706163953Srrs return (NULL); 1707163953Srrs } 1708163953Srrs /* 1709163953Srrs * verify any preceding AUTH chunk that was skipped 1710163953Srrs */ 1711163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1712163953Srrs sctp_auth_get_cookie_params(stcb, m, 1713163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1714163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1715163953Srrs if (auth_skipped) { 1716163953Srrs struct sctp_auth_chunk *auth; 1717163953Srrs 1718163953Srrs auth = (struct sctp_auth_chunk *) 1719166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1720169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1721163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1722169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1723169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1724171440Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1725165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1726171440Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1727163953Srrs return (NULL); 1728163953Srrs } else { 1729163953Srrs /* remaining chunks checked... good to go */ 1730163953Srrs stcb->asoc.authenticated = 1; 1731163953Srrs } 1732163953Srrs } 1733163953Srrs /* update current state */ 1734169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1735163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1736163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1737163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1738163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1739163953Srrs } else { 1740163953Srrs asoc->state = SCTP_STATE_OPEN; 1741163953Srrs } 1742165220Srrs sctp_stop_all_cookie_timers(stcb); 1743163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1744163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1745163953Srrs 1746163953Srrs /* 1747163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1748163953Srrs * addresses that need to get added to the peer (eg. addresses 1749163953Srrs * changed while cookie echo in flight). This needs to be done 1750163953Srrs * after we go to the OPEN state to do the correct asconf 1751163953Srrs * processing. else, make sure we have the correct addresses in our 1752163953Srrs * lists 1753163953Srrs */ 1754163953Srrs 1755163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1756163953Srrs /* pull in local_address (our "from" address) */ 1757163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1758163953Srrs /* source addr is IPv4 */ 1759163953Srrs sin = (struct sockaddr_in *)initack_src; 1760163953Srrs memset(sin, 0, sizeof(*sin)); 1761163953Srrs sin->sin_family = AF_INET; 1762163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1763163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1764163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1765163953Srrs /* source addr is IPv6 */ 1766163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1767163953Srrs memset(sin6, 0, sizeof(*sin6)); 1768163953Srrs sin6->sin6_family = AF_INET6; 1769163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1770163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1771163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1772163953Srrs sizeof(sin6->sin6_addr)); 1773163953Srrs } else { 1774170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1775165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1776170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1777163953Srrs return (NULL); 1778163953Srrs } 1779163953Srrs 1780163953Srrs sctp_check_address_list(stcb, m, 1781163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1782163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1783163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1784163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1785163953Srrs 1786163953Srrs 1787163953Srrs /* set up to notify upper layer */ 1788163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1789163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1790163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1791163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1792163953Srrs /* 1793163953Srrs * This is an endpoint that called connect() how it got a 1794163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1795163953Srrs * the INIT was sent, but before it got there.. a complete 1796163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1797163953Srrs * should have went to the other code.. not here.. oh well.. 1798163953Srrs * a bit of protection is worth having.. 1799163953Srrs */ 1800163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1801163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1802163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1803163953Srrs (inp->sctp_socket->so_qlimit)) { 1804163953Srrs /* 1805163953Srrs * We don't want to do anything with this one. Since it is 1806163953Srrs * the listening guy. The timer will get started for 1807163953Srrs * accepted connections in the caller. 1808163953Srrs */ 1809163953Srrs ; 1810163953Srrs } 1811163953Srrs /* since we did not send a HB make sure we don't double things */ 1812169420Srrs if ((netp) && (*netp)) 1813169420Srrs (*netp)->hb_responded = 1; 1814163953Srrs 1815163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1816163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1817163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1818163953Srrs } 1819163953Srrs /* respond with a COOKIE-ACK */ 1820165220Srrs /* calculate the RTT */ 1821171440Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1822170642Srrs if ((netp) && (*netp)) { 1823169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1824171477Srrs &cookie->time_entered, sctp_align_unsafe_makecopy); 1825170642Srrs } 1826163953Srrs sctp_send_cookie_ack(stcb); 1827163953Srrs return (stcb); 1828163953Srrs} 1829163953Srrs 1830163953Srrs 1831163953Srrs/* 1832163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1833163953Srrs * existing (non-NULL) TCB 1834163953Srrs */ 1835163953Srrsstatic struct mbuf * 1836163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1837163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1838163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1839169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1840170181Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id) 1841163953Srrs{ 1842163953Srrs struct sctp_state_cookie *cookie; 1843163953Srrs struct sockaddr_in6 sin6; 1844163953Srrs struct sockaddr_in sin; 1845163953Srrs struct sctp_tcb *l_stcb = *stcb; 1846163953Srrs struct sctp_inpcb *l_inp; 1847163953Srrs struct sockaddr *to; 1848163953Srrs sctp_assoc_t sac_restart_id; 1849163953Srrs struct sctp_pcb *ep; 1850163953Srrs struct mbuf *m_sig; 1851163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1852163953Srrs uint8_t *sig; 1853163953Srrs uint8_t cookie_ok = 0; 1854163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1855163953Srrs unsigned int cookie_len; 1856163953Srrs struct timeval now; 1857163953Srrs struct timeval time_expires; 1858163953Srrs struct sockaddr_storage dest_store; 1859163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1860163953Srrs struct ip *iph; 1861163953Srrs int notification = 0; 1862163953Srrs struct sctp_nets *netl; 1863163953Srrs int had_a_existing_tcb = 0; 1864163953Srrs 1865169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1866169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 1867163953Srrs 1868163953Srrs if (inp_p == NULL) { 1869163953Srrs return (NULL); 1870163953Srrs } 1871163953Srrs /* First get the destination address setup too. */ 1872163953Srrs iph = mtod(m, struct ip *); 1873163953Srrs if (iph->ip_v == IPVERSION) { 1874163953Srrs /* its IPv4 */ 1875170140Srrs struct sockaddr_in *lsin; 1876163953Srrs 1877170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 1878170140Srrs memset(lsin, 0, sizeof(*lsin)); 1879170140Srrs lsin->sin_family = AF_INET; 1880170140Srrs lsin->sin_len = sizeof(*lsin); 1881170140Srrs lsin->sin_port = sh->dest_port; 1882170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 1883165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1884163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1885163953Srrs /* its IPv6 */ 1886163953Srrs struct ip6_hdr *ip6; 1887170140Srrs struct sockaddr_in6 *lsin6; 1888163953Srrs 1889170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 1890170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 1891170140Srrs lsin6->sin6_family = AF_INET6; 1892170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 1893163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1894170140Srrs lsin6->sin6_port = sh->dest_port; 1895170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 1896166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1897163953Srrs } else { 1898163953Srrs return (NULL); 1899163953Srrs } 1900163953Srrs 1901163953Srrs cookie = &cp->cookie; 1902163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1903163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1904163953Srrs 1905163953Srrs if ((cookie->peerport != sh->src_port) && 1906163953Srrs (cookie->myport != sh->dest_port) && 1907163953Srrs (cookie->my_vtag != sh->v_tag)) { 1908163953Srrs /* 1909163953Srrs * invalid ports or bad tag. Note that we always leave the 1910163953Srrs * v_tag in the header in network order and when we stored 1911163953Srrs * it in the my_vtag slot we also left it in network order. 1912169352Srrs * This maintains the match even though it may be in the 1913163953Srrs * opposite byte order of the machine :-> 1914163953Srrs */ 1915163953Srrs return (NULL); 1916163953Srrs } 1917163953Srrs if (cookie_len > size_of_pkt || 1918163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1919163953Srrs sizeof(struct sctp_init_chunk) + 1920163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1921163953Srrs /* cookie too long! or too small */ 1922163953Srrs return (NULL); 1923163953Srrs } 1924163953Srrs /* 1925163953Srrs * split off the signature into its own mbuf (since it should not be 1926163953Srrs * calculated in the sctp_hmac_m() call). 1927163953Srrs */ 1928163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1929163953Srrs if (sig_offset > size_of_pkt) { 1930163953Srrs /* packet not correct size! */ 1931163953Srrs /* XXX this may already be accounted for earlier... */ 1932163953Srrs return (NULL); 1933163953Srrs } 1934163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1935163953Srrs if (m_sig == NULL) { 1936163953Srrs /* out of memory or ?? */ 1937163953Srrs return (NULL); 1938163953Srrs } 1939163953Srrs /* 1940163953Srrs * compute the signature/digest for the cookie 1941163953Srrs */ 1942163953Srrs ep = &(*inp_p)->sctp_ep; 1943163953Srrs l_inp = *inp_p; 1944163953Srrs if (l_stcb) { 1945163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1946163953Srrs } 1947163953Srrs SCTP_INP_RLOCK(l_inp); 1948163953Srrs if (l_stcb) { 1949163953Srrs SCTP_TCB_LOCK(l_stcb); 1950163953Srrs } 1951163953Srrs /* which cookie is it? */ 1952163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1953163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1954163953Srrs /* it's the old cookie */ 1955169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1956163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1957170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1958163953Srrs } else { 1959163953Srrs /* it's the current cookie */ 1960169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1961163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1962170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1963163953Srrs } 1964163953Srrs /* get the signature */ 1965163953Srrs SCTP_INP_RUNLOCK(l_inp); 1966163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1967163953Srrs if (sig == NULL) { 1968163953Srrs /* couldn't find signature */ 1969164085Srrs sctp_m_freem(m_sig); 1970163953Srrs return (NULL); 1971163953Srrs } 1972163953Srrs /* compare the received digest with the computed digest */ 1973163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1974163953Srrs /* try the old cookie? */ 1975163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1976163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1977163953Srrs /* compute digest with old */ 1978169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1979163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1980170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1981163953Srrs /* compare */ 1982163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1983163953Srrs cookie_ok = 1; 1984163953Srrs } 1985163953Srrs } else { 1986163953Srrs cookie_ok = 1; 1987163953Srrs } 1988163953Srrs 1989163953Srrs /* 1990163953Srrs * Now before we continue we must reconstruct our mbuf so that 1991163953Srrs * normal processing of any other chunks will work. 1992163953Srrs */ 1993163953Srrs { 1994163953Srrs struct mbuf *m_at; 1995163953Srrs 1996163953Srrs m_at = m; 1997165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1998165647Srrs m_at = SCTP_BUF_NEXT(m_at); 1999163953Srrs } 2000165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2001163953Srrs } 2002163953Srrs 2003163953Srrs if (cookie_ok == 0) { 2004169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2005169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2006169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2007169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 2008163953Srrs return (NULL); 2009163953Srrs } 2010163953Srrs /* 2011163953Srrs * check the cookie timestamps to be sure it's not stale 2012163953Srrs */ 2013169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 2014163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2015169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2016163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2017163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2018163953Srrs /* cookie is stale! */ 2019163953Srrs struct mbuf *op_err; 2020163953Srrs struct sctp_stale_cookie_msg *scm; 2021163953Srrs uint32_t tim; 2022163953Srrs 2023163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2024165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2025163953Srrs if (op_err == NULL) { 2026163953Srrs /* FOOBAR */ 2027163953Srrs return (NULL); 2028163953Srrs } 2029163953Srrs /* pre-reserve some space */ 2030165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2031165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2032165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2033163953Srrs 2034163953Srrs /* Set the len */ 2035165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2036163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2037163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2038163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2039163953Srrs (sizeof(uint32_t)))); 2040163953Srrs /* seconds to usec */ 2041163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2042163953Srrs /* add in usec */ 2043163953Srrs if (tim == 0) 2044163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2045163953Srrs scm->time_usec = htonl(tim); 2046169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2047170181Srrs vrf_id); 2048163953Srrs return (NULL); 2049163953Srrs } 2050163953Srrs /* 2051163953Srrs * Now we must see with the lookup address if we have an existing 2052163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2053163953Srrs * and a INIT collided with us and somewhere the peer sent the 2054163953Srrs * cookie on another address besides the single address our assoc 2055163953Srrs * had for him. In this case we will have one of the tie-tags set at 2056163953Srrs * least AND the address field in the cookie can be used to look it 2057163953Srrs * up. 2058163953Srrs */ 2059163953Srrs to = NULL; 2060163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2061163953Srrs memset(&sin6, 0, sizeof(sin6)); 2062163953Srrs sin6.sin6_family = AF_INET6; 2063163953Srrs sin6.sin6_len = sizeof(sin6); 2064163953Srrs sin6.sin6_port = sh->src_port; 2065163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2066163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2067163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2068163953Srrs to = (struct sockaddr *)&sin6; 2069163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2070163953Srrs memset(&sin, 0, sizeof(sin)); 2071163953Srrs sin.sin_family = AF_INET; 2072163953Srrs sin.sin_len = sizeof(sin); 2073163953Srrs sin.sin_port = sh->src_port; 2074163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2075163953Srrs to = (struct sockaddr *)&sin; 2076169420Srrs } else { 2077169420Srrs /* This should not happen */ 2078169420Srrs return (NULL); 2079163953Srrs } 2080163953Srrs if ((*stcb == NULL) && to) { 2081163953Srrs /* Yep, lets check */ 2082163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2083163953Srrs if (*stcb == NULL) { 2084163953Srrs /* 2085163953Srrs * We should have only got back the same inp. If we 2086163953Srrs * got back a different ep we have a problem. The 2087163953Srrs * original findep got back l_inp and now 2088163953Srrs */ 2089163953Srrs if (l_inp != *inp_p) { 2090169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2091163953Srrs } 2092165220Srrs } else { 2093165220Srrs if (*locked_tcb == NULL) { 2094165220Srrs /* 2095165220Srrs * In this case we found the assoc only 2096165220Srrs * after we locked the create lock. This 2097165220Srrs * means we are in a colliding case and we 2098165220Srrs * must make sure that we unlock the tcb if 2099165220Srrs * its one of the cases where we throw away 2100165220Srrs * the incoming packets. 2101165220Srrs */ 2102165220Srrs *locked_tcb = *stcb; 2103165220Srrs 2104165220Srrs /* 2105165220Srrs * We must also increment the inp ref count 2106165220Srrs * since the ref_count flags was set when we 2107165220Srrs * did not find the TCB, now we found it 2108165220Srrs * which reduces the refcount.. we must 2109165220Srrs * raise it back out to balance it all :-) 2110165220Srrs */ 2111165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2112165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2113169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2114165220Srrs (*stcb)->sctp_ep, l_inp); 2115165220Srrs } 2116165220Srrs } 2117163953Srrs } 2118163953Srrs } 2119169420Srrs if (to == NULL) 2120169420Srrs return (NULL); 2121169420Srrs 2122163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2123163953Srrs if (*stcb == NULL) { 2124163953Srrs /* this is the "normal" case... get a new TCB */ 2125163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2126163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2127170181Srrs auth_skipped, auth_offset, auth_len, vrf_id); 2128163953Srrs } else { 2129163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2130163953Srrs had_a_existing_tcb = 1; 2131163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2132170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2133170181Srrs ¬ification, &sac_restart_id, vrf_id); 2134163953Srrs } 2135163953Srrs 2136163953Srrs if (*stcb == NULL) { 2137163953Srrs /* still no TCB... must be bad cookie-echo */ 2138163953Srrs return (NULL); 2139163953Srrs } 2140163953Srrs /* 2141163953Srrs * Ok, we built an association so confirm the address we sent the 2142163953Srrs * INIT-ACK to. 2143163953Srrs */ 2144163953Srrs netl = sctp_findnet(*stcb, to); 2145163953Srrs /* 2146163953Srrs * This code should in theory NOT run but 2147163953Srrs */ 2148163953Srrs if (netl == NULL) { 2149163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2150163953Srrs int ret; 2151163953Srrs 2152165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2153165220Srrs SCTP_IN_COOKIE_PROC); 2154163953Srrs netl = sctp_findnet(*stcb, to); 2155163953Srrs } 2156163953Srrs if (netl) { 2157163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2158163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2159169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2160163953Srrs netl); 2161163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2162163953Srrs (*stcb), 0, (void *)netl); 2163163953Srrs } 2164163953Srrs } 2165163953Srrs if (*stcb) { 2166163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2167163953Srrs *stcb, NULL); 2168163953Srrs } 2169163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2170163953Srrs if (!had_a_existing_tcb || 2171163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2172163953Srrs /* 2173163953Srrs * If we have a NEW cookie or the connect never 2174163953Srrs * reached the connected state during collision we 2175163953Srrs * must do the TCP accept thing. 2176163953Srrs */ 2177163953Srrs struct socket *so, *oso; 2178163953Srrs struct sctp_inpcb *inp; 2179163953Srrs 2180163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2181163953Srrs /* 2182163953Srrs * For a restart we will keep the same 2183163953Srrs * socket, no need to do anything. I THINK!! 2184163953Srrs */ 2185163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2186163953Srrs return (m); 2187163953Srrs } 2188163953Srrs oso = (*inp_p)->sctp_socket; 2189163953Srrs /* 2190163953Srrs * We do this to keep the sockets side happy durin 2191163953Srrs * the sonewcon ONLY. 2192163953Srrs */ 2193163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2194166086Srrs so = sonewconn(oso, 0 2195163953Srrs ); 2196163953Srrs SCTP_INP_WLOCK((*stcb)->sctp_ep); 2197163953Srrs SCTP_TCB_LOCK((*stcb)); 2198163953Srrs SCTP_INP_WUNLOCK((*stcb)->sctp_ep); 2199163953Srrs if (so == NULL) { 2200163953Srrs struct mbuf *op_err; 2201163953Srrs 2202163953Srrs /* Too many sockets */ 2203169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2204163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2205163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2206170181Srrs sh, op_err, vrf_id); 2207165220Srrs sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2208163953Srrs return (NULL); 2209163953Srrs } 2210163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2211166086Srrs SCTP_INP_INCR_REF(inp); 2212166086Srrs /* 2213166086Srrs * We add the unbound flag here so that if we get an 2214166086Srrs * soabort() before we get the move_pcb done, we 2215166086Srrs * will properly cleanup. 2216166086Srrs */ 2217163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2218163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2219163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2220166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2221163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2222163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2223163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2224163953Srrs inp->sctp_socket = so; 2225163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2226163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2227163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2228163953Srrs inp->inp_starting_point_for_iterator = NULL; 2229163953Srrs /* 2230163953Srrs * copy in the authentication parameters from the 2231163953Srrs * original endpoint 2232163953Srrs */ 2233163953Srrs if (inp->sctp_ep.local_hmacs) 2234163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2235163953Srrs inp->sctp_ep.local_hmacs = 2236163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2237163953Srrs if (inp->sctp_ep.local_auth_chunks) 2238163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2239163953Srrs inp->sctp_ep.local_auth_chunks = 2240163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2241163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2242163953Srrs &inp->sctp_ep.shared_keys); 2243163953Srrs 2244163953Srrs /* 2245163953Srrs * Now we must move it from one hash table to 2246163953Srrs * another and get the tcb in the right place. 2247163953Srrs */ 2248163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2249170056Srrs 2250170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2251170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2252170056Srrs 2253169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2254170056Srrs SCTP_TCB_LOCK((*stcb)); 2255170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2256163953Srrs 2257170056Srrs 2258166086Srrs /* 2259166086Srrs * now we must check to see if we were aborted while 2260166086Srrs * the move was going on and the lock/unlock 2261166086Srrs * happened. 2262166086Srrs */ 2263166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2264166086Srrs /* 2265166086Srrs * yep it was, we leave the assoc attached 2266166086Srrs * to the socket since the sctp_inpcb_free() 2267166086Srrs * call will send an abort for us. 2268166086Srrs */ 2269166086Srrs SCTP_INP_DECR_REF(inp); 2270166086Srrs return (NULL); 2271166086Srrs } 2272166086Srrs SCTP_INP_DECR_REF(inp); 2273163953Srrs /* Switch over to the new guy */ 2274163953Srrs *inp_p = inp; 2275166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2276163953Srrs 2277166086Srrs /* 2278166086Srrs * Pull it from the incomplete queue and wake the 2279166086Srrs * guy 2280166086Srrs */ 2281166086Srrs soisconnected(so); 2282163953Srrs return (m); 2283163953Srrs } 2284163953Srrs } 2285163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2286163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2287163953Srrs } 2288163953Srrs return (m); 2289163953Srrs} 2290163953Srrs 2291163953Srrsstatic void 2292163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2293163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2294163953Srrs{ 2295163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2296163953Srrs struct sctp_association *asoc; 2297163953Srrs 2298169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2299169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2300163953Srrs if (stcb == NULL) 2301163953Srrs return; 2302163953Srrs 2303163953Srrs asoc = &stcb->asoc; 2304163953Srrs 2305163953Srrs sctp_stop_all_cookie_timers(stcb); 2306163953Srrs /* process according to association state */ 2307163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2308163953Srrs /* state change only needed when I am in right state */ 2309169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2310163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2311163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 2312163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2313163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2314163953Srrs 2315163953Srrs } else { 2316163953Srrs asoc->state = SCTP_STATE_OPEN; 2317163953Srrs } 2318163953Srrs /* update RTO */ 2319163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2320163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2321163953Srrs if (asoc->overall_error_count == 0) { 2322163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2323171477Srrs &asoc->time_entered, sctp_align_safe_nocopy); 2324163953Srrs } 2325169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2326163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2327163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2328163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2329163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2330163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2331163953Srrs } 2332163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2333163953Srrs stcb, net); 2334163953Srrs /* 2335163953Srrs * since we did not send a HB make sure we don't double 2336163953Srrs * things 2337163953Srrs */ 2338163953Srrs net->hb_responded = 1; 2339163953Srrs 2340163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2341163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2342163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2343163953Srrs stcb->sctp_ep, stcb, NULL); 2344163953Srrs } 2345163953Srrs /* 2346171572Srrs * send ASCONF if parameters are pending and ASCONFs are 2347171572Srrs * allowed (eg. addresses changed when init/cookie echo were 2348171572Srrs * in flight) 2349163953Srrs */ 2350163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2351163953Srrs (stcb->asoc.peer_supports_asconf) && 2352163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2353171572Srrs#ifdef SCTP_TIMER_BASED_ASCONF 2354163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2355163953Srrs stcb->sctp_ep, stcb, 2356163953Srrs stcb->asoc.primary_destination); 2357171572Srrs#else 2358171572Srrs sctp_send_asconf(stcb, stcb->asoc.primary_destination); 2359171572Srrs#endif 2360163953Srrs } 2361163953Srrs } 2362163953Srrs /* Toss the cookie if I can */ 2363163953Srrs sctp_toss_old_cookies(stcb, asoc); 2364163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2365163953Srrs /* Restart the timer if we have pending data */ 2366163953Srrs struct sctp_tmit_chunk *chk; 2367163953Srrs 2368163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2369163953Srrs if (chk) { 2370163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2371163953Srrs stcb, chk->whoTo); 2372163953Srrs } 2373163953Srrs } 2374163953Srrs} 2375163953Srrs 2376163953Srrsstatic void 2377163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2378163953Srrs struct sctp_tcb *stcb) 2379163953Srrs{ 2380163953Srrs struct sctp_nets *net; 2381163953Srrs struct sctp_tmit_chunk *lchk; 2382163953Srrs uint32_t tsn; 2383163953Srrs 2384163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2385163953Srrs return; 2386163953Srrs } 2387163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2388163953Srrs tsn = ntohl(cp->tsn); 2389163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2390163953Srrs /* Also we make sure we disable the nonce_wait */ 2391163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2392163953Srrs if (lchk == NULL) { 2393163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2394163953Srrs } else { 2395163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2396163953Srrs } 2397163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2398163953Srrs stcb->asoc.nonce_sum_check = 0; 2399163953Srrs 2400163953Srrs /* Find where it was sent, if possible */ 2401163953Srrs net = NULL; 2402163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2403163953Srrs while (lchk) { 2404163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2405163953Srrs net = lchk->whoTo; 2406163953Srrs break; 2407163953Srrs } 2408163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2409163953Srrs break; 2410163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2411163953Srrs } 2412163953Srrs if (net == NULL) 2413163953Srrs /* default is we use the primary */ 2414163953Srrs net = stcb->asoc.primary_destination; 2415163953Srrs 2416163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2417163953Srrs /* 2418171440Srrs * JRS - Use the congestion control given in the pluggable 2419171440Srrs * CC module 2420171440Srrs */ 2421171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net); 2422171440Srrs /* 2423163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2424163953Srrs * the next sending seq i.e. the resync_tsn. 2425163953Srrs */ 2426163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2427163953Srrs } 2428163953Srrs /* 2429163953Srrs * We always send a CWR this way if our previous one was lost our 2430163953Srrs * peer will get an update, or if it is not time again to reduce we 2431163953Srrs * still get the cwr to the peer. 2432163953Srrs */ 2433163953Srrs sctp_send_cwr(stcb, net, tsn); 2434163953Srrs} 2435163953Srrs 2436163953Srrsstatic void 2437163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2438163953Srrs{ 2439163953Srrs /* 2440163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2441163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2442163953Srrs * If so remove it. 2443163953Srrs */ 2444163953Srrs struct sctp_tmit_chunk *chk; 2445163953Srrs struct sctp_ecne_chunk *ecne; 2446163953Srrs 2447163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2448163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2449163953Srrs continue; 2450163953Srrs } 2451163953Srrs /* 2452163953Srrs * Look for and remove if it is the right TSN. Since there 2453163953Srrs * is only ONE ECNE on the control queue at any one time we 2454163953Srrs * don't need to worry about more than one! 2455163953Srrs */ 2456163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2457163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2458163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2459163953Srrs /* this covers this ECNE, we can remove it */ 2460163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2461163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2462163953Srrs sctp_next); 2463163953Srrs if (chk->data) { 2464163953Srrs sctp_m_freem(chk->data); 2465163953Srrs chk->data = NULL; 2466163953Srrs } 2467163953Srrs stcb->asoc.ctrl_queue_cnt--; 2468163953Srrs sctp_free_a_chunk(stcb, chk); 2469163953Srrs break; 2470163953Srrs } 2471163953Srrs } 2472163953Srrs} 2473163953Srrs 2474163953Srrsstatic void 2475163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2476163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2477163953Srrs{ 2478163953Srrs struct sctp_association *asoc; 2479163953Srrs 2480169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2481169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2482163953Srrs if (stcb == NULL) 2483163953Srrs return; 2484163953Srrs 2485163953Srrs asoc = &stcb->asoc; 2486163953Srrs /* process according to association state */ 2487163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2488163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2489163953Srrs SCTP_TCB_UNLOCK(stcb); 2490163953Srrs return; 2491163953Srrs } 2492163953Srrs /* notify upper layer protocol */ 2493163953Srrs if (stcb->sctp_socket) { 2494163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2495163953Srrs /* are the queues empty? they should be */ 2496163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2497163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2498163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2499164205Srrs sctp_report_all_outbound(stcb, 0); 2500163953Srrs } 2501163953Srrs } 2502163953Srrs /* stop the timer */ 2503171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2504163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2505163953Srrs /* free the TCB */ 2506171440Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2507163953Srrs return; 2508163953Srrs} 2509163953Srrs 2510163953Srrsstatic int 2511163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2512163953Srrs struct sctp_nets *net, uint8_t flg) 2513163953Srrs{ 2514163953Srrs switch (desc->chunk_type) { 2515163953Srrs case SCTP_DATA: 2516163953Srrs /* find the tsn to resend (possibly */ 2517163953Srrs { 2518163953Srrs uint32_t tsn; 2519163953Srrs struct sctp_tmit_chunk *tp1; 2520163953Srrs 2521163953Srrs tsn = ntohl(desc->tsn_ifany); 2522163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2523163953Srrs while (tp1) { 2524163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2525163953Srrs /* found it */ 2526163953Srrs break; 2527163953Srrs } 2528163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2529163953Srrs MAX_TSN)) { 2530163953Srrs /* not found */ 2531163953Srrs tp1 = NULL; 2532163953Srrs break; 2533163953Srrs } 2534163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2535163953Srrs } 2536163953Srrs if (tp1 == NULL) { 2537163953Srrs /* 2538163953Srrs * Do it the other way , aka without paying 2539163953Srrs * attention to queue seq order. 2540163953Srrs */ 2541163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2542163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2543163953Srrs while (tp1) { 2544163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2545163953Srrs /* found it */ 2546163953Srrs break; 2547163953Srrs } 2548163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2549163953Srrs } 2550163953Srrs } 2551163953Srrs if (tp1 == NULL) { 2552163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2553163953Srrs } 2554163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2555163953Srrs uint8_t *ddp; 2556163953Srrs 2557163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2558163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2559163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2560163953Srrs return (0); 2561163953Srrs } 2562163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2563163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2564163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2565163953Srrs return (0); 2566163953Srrs } 2567163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2568163953Srrs sizeof(struct sctp_data_chunk)); 2569163953Srrs { 2570163953Srrs unsigned int iii; 2571163953Srrs 2572163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2573163953Srrs iii++) { 2574163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2575163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2576163953Srrs return (-1); 2577163953Srrs } 2578163953Srrs } 2579163953Srrs } 2580163953Srrs /* 2581163953Srrs * We zero out the nonce so resync not 2582163953Srrs * needed 2583163953Srrs */ 2584163953Srrs tp1->rec.data.ect_nonce = 0; 2585163953Srrs 2586163953Srrs if (tp1->do_rtt) { 2587163953Srrs /* 2588163953Srrs * this guy had a RTO calculation 2589163953Srrs * pending on it, cancel it 2590163953Srrs */ 2591163953Srrs tp1->do_rtt = 0; 2592163953Srrs } 2593163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2594163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2595163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2596163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2597163953Srrs /* 2598163953Srrs * mark it as if we were doing a FR, since 2599163953Srrs * we will be getting gap ack reports behind 2600163953Srrs * the info from the router. 2601163953Srrs */ 2602163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2603163953Srrs /* 2604163953Srrs * mark the tsn with what sequences can 2605163953Srrs * cause a new FR. 2606163953Srrs */ 2607163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2608163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2609163953Srrs } else { 2610163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2611163953Srrs } 2612163953Srrs 2613163953Srrs /* restart the timer */ 2614163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2615171440Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2616163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2617163953Srrs stcb, tp1->whoTo); 2618163953Srrs 2619163953Srrs /* fix counts and things */ 2620170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 2621170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2622170744Srrs tp1->whoTo->flight_size, 2623170744Srrs tp1->book_size, 2624170744Srrs (uintptr_t) stcb, 2625170744Srrs tp1->rec.data.TSN_seq); 2626170744Srrs } 2627168709Srrs sctp_flight_size_decrease(tp1); 2628168709Srrs sctp_total_flight_decrease(stcb, tp1); 2629163953Srrs } { 2630163953Srrs /* audit code */ 2631163953Srrs unsigned int audit; 2632163953Srrs 2633163953Srrs audit = 0; 2634163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2635163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2636163953Srrs audit++; 2637163953Srrs } 2638163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2639163953Srrs sctp_next) { 2640163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2641163953Srrs audit++; 2642163953Srrs } 2643163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2644169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2645163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2646163953Srrs#ifndef SCTP_AUDITING_ENABLED 2647163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2648163953Srrs#endif 2649163953Srrs } 2650163953Srrs } 2651163953Srrs } 2652163953Srrs break; 2653163953Srrs case SCTP_ASCONF: 2654163953Srrs { 2655163953Srrs struct sctp_tmit_chunk *asconf; 2656163953Srrs 2657163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2658163953Srrs sctp_next) { 2659163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2660163953Srrs break; 2661163953Srrs } 2662163953Srrs } 2663163953Srrs if (asconf) { 2664163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2665163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2666163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2667163953Srrs asconf->snd_count--; 2668163953Srrs } 2669163953Srrs } 2670163953Srrs break; 2671163953Srrs case SCTP_INITIATION: 2672163953Srrs /* resend the INIT */ 2673163953Srrs stcb->asoc.dropped_special_cnt++; 2674163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2675163953Srrs /* 2676163953Srrs * If we can get it in, in a few attempts we do 2677163953Srrs * this, otherwise we let the timer fire. 2678163953Srrs */ 2679163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2680171440Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2681163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2682163953Srrs } 2683163953Srrs break; 2684163953Srrs case SCTP_SELECTIVE_ACK: 2685163953Srrs /* resend the sack */ 2686163953Srrs sctp_send_sack(stcb); 2687163953Srrs break; 2688163953Srrs case SCTP_HEARTBEAT_REQUEST: 2689163953Srrs /* resend a demand HB */ 2690171440Srrs if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 2691171440Srrs /* 2692171440Srrs * Only retransmit if we KNOW we wont destroy the 2693171440Srrs * tcb 2694171440Srrs */ 2695171440Srrs (void)sctp_send_hb(stcb, 1, net); 2696171440Srrs } 2697163953Srrs break; 2698163953Srrs case SCTP_SHUTDOWN: 2699163953Srrs sctp_send_shutdown(stcb, net); 2700163953Srrs break; 2701163953Srrs case SCTP_SHUTDOWN_ACK: 2702163953Srrs sctp_send_shutdown_ack(stcb, net); 2703163953Srrs break; 2704163953Srrs case SCTP_COOKIE_ECHO: 2705163953Srrs { 2706163953Srrs struct sctp_tmit_chunk *cookie; 2707163953Srrs 2708163953Srrs cookie = NULL; 2709163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2710163953Srrs sctp_next) { 2711163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2712163953Srrs break; 2713163953Srrs } 2714163953Srrs } 2715163953Srrs if (cookie) { 2716163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2717163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2718163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2719163953Srrs sctp_stop_all_cookie_timers(stcb); 2720163953Srrs } 2721163953Srrs } 2722163953Srrs break; 2723163953Srrs case SCTP_COOKIE_ACK: 2724163953Srrs sctp_send_cookie_ack(stcb); 2725163953Srrs break; 2726163953Srrs case SCTP_ASCONF_ACK: 2727163953Srrs /* resend last asconf ack */ 2728163953Srrs sctp_send_asconf_ack(stcb, 1); 2729163953Srrs break; 2730163953Srrs case SCTP_FORWARD_CUM_TSN: 2731163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2732163953Srrs break; 2733163953Srrs /* can't do anything with these */ 2734163953Srrs case SCTP_PACKET_DROPPED: 2735163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2736163953Srrs case SCTP_HEARTBEAT_ACK: 2737163953Srrs case SCTP_ABORT_ASSOCIATION: 2738163953Srrs case SCTP_OPERATION_ERROR: 2739163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2740163953Srrs case SCTP_ECN_ECHO: 2741163953Srrs case SCTP_ECN_CWR: 2742163953Srrs default: 2743163953Srrs break; 2744163953Srrs } 2745163953Srrs return (0); 2746163953Srrs} 2747163953Srrs 2748163953Srrsvoid 2749163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2750163953Srrs{ 2751163953Srrs int i; 2752163953Srrs uint16_t temp; 2753163953Srrs 2754163953Srrs /* 2755163953Srrs * We set things to 0xffff since this is the last delivered sequence 2756163953Srrs * and we will be sending in 0 after the reset. 2757163953Srrs */ 2758163953Srrs 2759163953Srrs if (number_entries) { 2760163953Srrs for (i = 0; i < number_entries; i++) { 2761163953Srrs temp = ntohs(list[i]); 2762163953Srrs if (temp >= stcb->asoc.streamincnt) { 2763163953Srrs continue; 2764163953Srrs } 2765163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2766163953Srrs } 2767163953Srrs } else { 2768163953Srrs list = NULL; 2769163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2770163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2771163953Srrs } 2772163953Srrs } 2773163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2774163953Srrs} 2775163953Srrs 2776163953Srrsstatic void 2777163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2778163953Srrs{ 2779163953Srrs int i; 2780163953Srrs 2781163953Srrs if (number_entries == 0) { 2782163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2783163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2784163953Srrs } 2785163953Srrs } else if (number_entries) { 2786163953Srrs for (i = 0; i < number_entries; i++) { 2787163953Srrs uint16_t temp; 2788163953Srrs 2789163953Srrs temp = ntohs(list[i]); 2790163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2791163953Srrs /* no such stream */ 2792163953Srrs continue; 2793163953Srrs } 2794163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2795163953Srrs } 2796163953Srrs } 2797163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2798163953Srrs} 2799163953Srrs 2800163953Srrs 2801163953Srrsstruct sctp_stream_reset_out_request * 2802163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2803163953Srrs{ 2804163953Srrs struct sctp_association *asoc; 2805163953Srrs struct sctp_stream_reset_out_req *req; 2806163953Srrs struct sctp_stream_reset_out_request *r; 2807163953Srrs struct sctp_tmit_chunk *chk; 2808163953Srrs int len, clen; 2809163953Srrs 2810163953Srrs asoc = &stcb->asoc; 2811163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2812169208Srrs asoc->stream_reset_outstanding = 0; 2813163953Srrs return (NULL); 2814163953Srrs } 2815163953Srrs if (stcb->asoc.str_reset == NULL) { 2816169208Srrs asoc->stream_reset_outstanding = 0; 2817163953Srrs return (NULL); 2818163953Srrs } 2819163953Srrs chk = stcb->asoc.str_reset; 2820163953Srrs if (chk->data == NULL) { 2821163953Srrs return (NULL); 2822163953Srrs } 2823163953Srrs if (bchk) { 2824163953Srrs /* he wants a copy of the chk pointer */ 2825163953Srrs *bchk = chk; 2826163953Srrs } 2827163953Srrs clen = chk->send_size; 2828163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2829163953Srrs r = &req->sr_req; 2830163953Srrs if (ntohl(r->request_seq) == seq) { 2831163953Srrs /* found it */ 2832163953Srrs return (r); 2833163953Srrs } 2834163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2835163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2836163953Srrs /* move to the next one, there can only be a max of two */ 2837163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2838163953Srrs if (ntohl(r->request_seq) == seq) { 2839163953Srrs return (r); 2840163953Srrs } 2841163953Srrs } 2842163953Srrs /* that seq is not here */ 2843163953Srrs return (NULL); 2844163953Srrs} 2845163953Srrs 2846163953Srrsstatic void 2847163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2848163953Srrs{ 2849163953Srrs struct sctp_association *asoc; 2850163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2851163953Srrs 2852163953Srrs if (stcb->asoc.str_reset == NULL) { 2853163953Srrs return; 2854163953Srrs } 2855166675Srrs asoc = &stcb->asoc; 2856166675Srrs 2857171440Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 2858163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2859163953Srrs chk, 2860163953Srrs sctp_next); 2861163953Srrs if (chk->data) { 2862163953Srrs sctp_m_freem(chk->data); 2863163953Srrs chk->data = NULL; 2864163953Srrs } 2865163953Srrs asoc->ctrl_queue_cnt--; 2866163953Srrs sctp_free_a_chunk(stcb, chk); 2867163953Srrs stcb->asoc.str_reset = NULL; 2868163953Srrs} 2869163953Srrs 2870163953Srrs 2871163953Srrsstatic int 2872163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2873163953Srrs uint32_t seq, uint32_t action, 2874164139Srrs struct sctp_stream_reset_response *respin) 2875163953Srrs{ 2876163953Srrs uint16_t type; 2877163953Srrs int lparm_len; 2878163953Srrs struct sctp_association *asoc = &stcb->asoc; 2879163953Srrs struct sctp_tmit_chunk *chk; 2880163953Srrs struct sctp_stream_reset_out_request *srparam; 2881163953Srrs int number_entries; 2882163953Srrs 2883163953Srrs if (asoc->stream_reset_outstanding == 0) { 2884163953Srrs /* duplicate */ 2885163953Srrs return (0); 2886163953Srrs } 2887163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2888163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2889163953Srrs if (srparam) { 2890163953Srrs stcb->asoc.str_reset_seq_out++; 2891163953Srrs type = ntohs(srparam->ph.param_type); 2892163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2893163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2894164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2895163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2896163953Srrs if (asoc->stream_reset_outstanding) 2897163953Srrs asoc->stream_reset_outstanding--; 2898163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2899163953Srrs /* do it */ 2900163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2901163953Srrs } else { 2902163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2903163953Srrs } 2904163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2905163953Srrs /* Answered my request */ 2906164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2907163953Srrs if (asoc->stream_reset_outstanding) 2908163953Srrs asoc->stream_reset_outstanding--; 2909163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2910163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2911163953Srrs } 2912163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2913163953Srrs /** 2914163953Srrs * a) Adopt the new in tsn. 2915163953Srrs * b) reset the map 2916163953Srrs * c) Adopt the new out-tsn 2917163953Srrs */ 2918163953Srrs struct sctp_stream_reset_response_tsn *resp; 2919163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2920163953Srrs int abort_flag = 0; 2921163953Srrs 2922163953Srrs if (respin == NULL) { 2923163953Srrs /* huh ? */ 2924163953Srrs return (0); 2925163953Srrs } 2926163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2927163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2928163953Srrs asoc->stream_reset_outstanding--; 2929163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2930163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2931163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2932170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 2933163953Srrs if (abort_flag) { 2934163953Srrs return (1); 2935163953Srrs } 2936163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2937163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2938163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2939163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2940163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2941163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2942163953Srrs 2943163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2944163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2945163953Srrs 2946163953Srrs } 2947163953Srrs } 2948163953Srrs /* get rid of the request and get the request flags */ 2949163953Srrs if (asoc->stream_reset_outstanding == 0) { 2950163953Srrs sctp_clean_up_stream_reset(stcb); 2951163953Srrs } 2952163953Srrs } 2953163953Srrs } 2954163953Srrs return (0); 2955163953Srrs} 2956163953Srrs 2957163953Srrsstatic void 2958163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2959163953Srrs struct sctp_tmit_chunk *chk, 2960170992Srrs struct sctp_stream_reset_in_request *req, int trunc) 2961163953Srrs{ 2962163953Srrs uint32_t seq; 2963163953Srrs int len, i; 2964163953Srrs int number_entries; 2965163953Srrs uint16_t temp; 2966163953Srrs 2967163953Srrs /* 2968163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2969163953Srrs * seq_in is right. 2970163953Srrs */ 2971163953Srrs struct sctp_association *asoc = &stcb->asoc; 2972163953Srrs 2973163953Srrs seq = ntohl(req->request_seq); 2974163953Srrs if (asoc->str_reset_seq_in == seq) { 2975170992Srrs if (trunc) { 2976170992Srrs /* Can't do it, since they exceeded our buffer size */ 2977170992Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2978170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 2979170992Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2980170992Srrs } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2981163953Srrs len = ntohs(req->ph.param_length); 2982163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2983163953Srrs for (i = 0; i < number_entries; i++) { 2984163953Srrs temp = ntohs(req->list_of_streams[i]); 2985163953Srrs req->list_of_streams[i] = temp; 2986163953Srrs } 2987163953Srrs /* move the reset action back one */ 2988163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2989163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2990163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2991163953Srrs asoc->str_reset_seq_out, 2992163953Srrs seq, (asoc->sending_seq - 1)); 2993163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2994163953Srrs asoc->str_reset = chk; 2995163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 2996163953Srrs stcb->asoc.stream_reset_outstanding++; 2997163953Srrs } else { 2998163953Srrs /* Can't do it, since we have sent one out */ 2999163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3000163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3001163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3002163953Srrs } 3003163953Srrs asoc->str_reset_seq_in++; 3004163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3005163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3006163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3007163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3008163953Srrs } else { 3009163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3010163953Srrs } 3011163953Srrs} 3012163953Srrs 3013163953Srrsstatic int 3014163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3015163953Srrs struct sctp_tmit_chunk *chk, 3016163953Srrs struct sctp_stream_reset_tsn_request *req) 3017163953Srrs{ 3018163953Srrs /* reset all in and out and update the tsn */ 3019163953Srrs /* 3020163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3021163953Srrs * and set cum-ack to it. Also process this selected number as a 3022163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3023163953Srrs */ 3024163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3025163953Srrs struct sctp_association *asoc = &stcb->asoc; 3026163953Srrs int abort_flag = 0; 3027163953Srrs uint32_t seq; 3028163953Srrs 3029163953Srrs seq = ntohl(req->request_seq); 3030163953Srrs if (asoc->str_reset_seq_in == seq) { 3031163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3032163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3033163953Srrs fwdtsn.ch.chunk_flags = 0; 3034163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3035170992Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3036163953Srrs if (abort_flag) { 3037163953Srrs return (1); 3038163953Srrs } 3039163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3040163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3041163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3042163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3043163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3044163953Srrs /* save off historical data for retrans */ 3045163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3046163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3047163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3048163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3049163953Srrs 3050163953Srrs sctp_add_stream_reset_result_tsn(chk, 3051163953Srrs ntohl(req->request_seq), 3052163953Srrs SCTP_STREAM_RESET_PERFORMED, 3053163953Srrs stcb->asoc.sending_seq, 3054163953Srrs stcb->asoc.mapping_array_base_tsn); 3055163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3056163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3057163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3058163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3059163953Srrs 3060163953Srrs asoc->str_reset_seq_in++; 3061163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3062163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3063163953Srrs stcb->asoc.last_sending_seq[0], 3064163953Srrs stcb->asoc.last_base_tsnsent[0] 3065163953Srrs ); 3066163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3067163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3068163953Srrs stcb->asoc.last_sending_seq[1], 3069163953Srrs stcb->asoc.last_base_tsnsent[1] 3070163953Srrs ); 3071163953Srrs } else { 3072163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3073163953Srrs } 3074163953Srrs return (0); 3075163953Srrs} 3076163953Srrs 3077163953Srrsstatic void 3078163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3079163953Srrs struct sctp_tmit_chunk *chk, 3080170992Srrs struct sctp_stream_reset_out_request *req, int trunc) 3081163953Srrs{ 3082163953Srrs uint32_t seq, tsn; 3083163953Srrs int number_entries, len; 3084163953Srrs struct sctp_association *asoc = &stcb->asoc; 3085163953Srrs 3086163953Srrs seq = ntohl(req->request_seq); 3087163953Srrs 3088163953Srrs /* now if its not a duplicate we process it */ 3089163953Srrs if (asoc->str_reset_seq_in == seq) { 3090163953Srrs len = ntohs(req->ph.param_length); 3091163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3092163953Srrs /* 3093163953Srrs * the sender is resetting, handle the list issue.. we must 3094163953Srrs * a) verify if we can do the reset, if so no problem b) If 3095163953Srrs * we can't do the reset we must copy the request. c) queue 3096163953Srrs * it, and setup the data in processor to trigger it off 3097163953Srrs * when needed and dequeue all the queued data. 3098163953Srrs */ 3099163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3100163953Srrs 3101163953Srrs /* move the reset action back one */ 3102163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3103170992Srrs if (trunc) { 3104170992Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3105170992Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3106170992Srrs } else if ((tsn == asoc->cumulative_tsn) || 3107163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3108163953Srrs /* we can do it now */ 3109163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3110163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3111163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3112163953Srrs } else { 3113163953Srrs /* 3114163953Srrs * we must queue it up and thus wait for the TSN's 3115163953Srrs * to arrive that are at or before tsn 3116163953Srrs */ 3117163953Srrs struct sctp_stream_reset_list *liste; 3118163953Srrs int siz; 3119163953Srrs 3120163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3121163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3122170091Srrs siz, SCTP_M_STRESET); 3123163953Srrs if (liste == NULL) { 3124163953Srrs /* gak out of memory */ 3125163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3126163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3127163953Srrs return; 3128163953Srrs } 3129163953Srrs liste->tsn = tsn; 3130163953Srrs liste->number_entries = number_entries; 3131163953Srrs memcpy(&liste->req, req, 3132163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3133163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3134163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3135163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3136163953Srrs } 3137163953Srrs asoc->str_reset_seq_in++; 3138163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3139163953Srrs /* 3140163953Srrs * one seq back, just echo back last action since my 3141163953Srrs * response was lost. 3142163953Srrs */ 3143163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3144163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3145163953Srrs /* 3146163953Srrs * two seq back, just echo back last action since my 3147163953Srrs * response was lost. 3148163953Srrs */ 3149163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3150163953Srrs } else { 3151163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3152163953Srrs } 3153163953Srrs} 3154163953Srrs 3155170992Srrs#ifdef __GNUC__ 3156170992Srrs__attribute__((noinline)) 3157170992Srrs#endif 3158170992Srrs static int 3159170992Srrs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3160170992Srrs struct sctp_stream_reset_out_req *sr_req) 3161163953Srrs{ 3162163953Srrs int chk_length, param_len, ptype; 3163170992Srrs struct sctp_paramhdr pstore; 3164170992Srrs uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3165170992Srrs 3166163953Srrs uint32_t seq; 3167163953Srrs int num_req = 0; 3168170992Srrs int trunc = 0; 3169163953Srrs struct sctp_tmit_chunk *chk; 3170163953Srrs struct sctp_chunkhdr *ch; 3171163953Srrs struct sctp_paramhdr *ph; 3172166675Srrs int ret_code = 0; 3173166675Srrs int num_param = 0; 3174163953Srrs 3175163953Srrs /* now it may be a reset or a reset-response */ 3176163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3177163953Srrs 3178163953Srrs /* setup for adding the response */ 3179163953Srrs sctp_alloc_a_chunk(stcb, chk); 3180163953Srrs if (chk == NULL) { 3181163953Srrs return (ret_code); 3182163953Srrs } 3183163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3184169208Srrs chk->rec.chunk_id.can_take_data = 0; 3185163953Srrs chk->asoc = &stcb->asoc; 3186163953Srrs chk->no_fr_allowed = 0; 3187163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3188166023Srrs chk->book_size_scale = 0; 3189165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3190163953Srrs if (chk->data == NULL) { 3191163953Srrsstrres_nochunk: 3192163953Srrs if (chk->data) { 3193163953Srrs sctp_m_freem(chk->data); 3194163953Srrs chk->data = NULL; 3195163953Srrs } 3196163953Srrs sctp_free_a_chunk(stcb, chk); 3197163953Srrs return (ret_code); 3198163953Srrs } 3199165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3200163953Srrs 3201163953Srrs /* setup chunk parameters */ 3202163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3203163953Srrs chk->snd_count = 0; 3204163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3205163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3206163953Srrs 3207163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3208163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3209163953Srrs ch->chunk_flags = 0; 3210163953Srrs ch->chunk_length = htons(chk->send_size); 3211165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3212170992Srrs offset += sizeof(struct sctp_chunkhdr); 3213163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3214170992Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 3215170992Srrs if (ph == NULL) 3216170992Srrs break; 3217163953Srrs param_len = ntohs(ph->param_length); 3218163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3219163953Srrs /* bad param */ 3220163953Srrs break; 3221163953Srrs } 3222170994Srrs ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 3223170992Srrs (uint8_t *) & cstore); 3224163953Srrs ptype = ntohs(ph->param_type); 3225163953Srrs num_param++; 3226170994Srrs if (param_len > (int)sizeof(cstore)) { 3227170992Srrs trunc = 1; 3228170992Srrs } else { 3229170992Srrs trunc = 0; 3230170992Srrs } 3231170992Srrs 3232163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3233163953Srrs /* hit the max of parameters already sorry.. */ 3234163953Srrs break; 3235163953Srrs } 3236163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3237163953Srrs struct sctp_stream_reset_out_request *req_out; 3238163953Srrs 3239163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3240163953Srrs num_req++; 3241163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3242163953Srrs seq = ntohl(req_out->response_seq); 3243163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3244163953Srrs /* implicit ack */ 3245169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3246163953Srrs } 3247163953Srrs } 3248170992Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 3249163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3250163953Srrs struct sctp_stream_reset_in_request *req_in; 3251163953Srrs 3252163953Srrs num_req++; 3253170992Srrs 3254163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3255170992Srrs 3256170992Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 3257163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3258163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3259163953Srrs 3260163953Srrs num_req++; 3261163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3262170992Srrs 3263163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3264163953Srrs ret_code = 1; 3265163953Srrs goto strres_nochunk; 3266163953Srrs } 3267163953Srrs /* no more */ 3268163953Srrs break; 3269163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3270163953Srrs struct sctp_stream_reset_response *resp; 3271163953Srrs uint32_t result; 3272163953Srrs 3273163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3274163953Srrs seq = ntohl(resp->response_seq); 3275163953Srrs result = ntohl(resp->result); 3276163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3277163953Srrs ret_code = 1; 3278163953Srrs goto strres_nochunk; 3279163953Srrs } 3280163953Srrs } else { 3281163953Srrs break; 3282163953Srrs } 3283170992Srrs offset += SCTP_SIZE32(param_len); 3284163953Srrs chk_length -= SCTP_SIZE32(param_len); 3285163953Srrs } 3286163953Srrs if (num_req == 0) { 3287163953Srrs /* we have no response free the stuff */ 3288163953Srrs goto strres_nochunk; 3289163953Srrs } 3290163953Srrs /* ok we have a chunk to link in */ 3291163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3292163953Srrs chk, 3293163953Srrs sctp_next); 3294163953Srrs stcb->asoc.ctrl_queue_cnt++; 3295163953Srrs return (ret_code); 3296163953Srrs} 3297163953Srrs 3298163953Srrs/* 3299163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3300163953Srrs * to handle this, either we get the whole packet and must disect it 3301163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3302163953Srrs * from a middle box that did the disectting for us. 3303163953Srrs */ 3304163953Srrsstatic void 3305163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3306170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3307163953Srrs{ 3308163953Srrs uint32_t bottle_bw, on_queue; 3309163953Srrs uint16_t trunc_len; 3310163953Srrs unsigned int chlen; 3311163953Srrs unsigned int at; 3312163953Srrs struct sctp_chunk_desc desc; 3313163953Srrs struct sctp_chunkhdr *ch; 3314163953Srrs 3315163953Srrs chlen = ntohs(cp->ch.chunk_length); 3316163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3317163953Srrs /* XXX possible chlen underflow */ 3318163953Srrs if (chlen == 0) { 3319163953Srrs ch = NULL; 3320163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3321163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3322163953Srrs } else { 3323163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3324163953Srrs chlen -= sizeof(struct sctphdr); 3325163953Srrs /* XXX possible chlen underflow */ 3326163953Srrs memset(&desc, 0, sizeof(desc)); 3327163953Srrs } 3328163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3329170781Srrs if (trunc_len > limit) { 3330170781Srrs trunc_len = limit; 3331170781Srrs } 3332163953Srrs /* now the chunks themselves */ 3333163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3334163953Srrs desc.chunk_type = ch->chunk_type; 3335163953Srrs /* get amount we need to move */ 3336163953Srrs at = ntohs(ch->chunk_length); 3337163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3338163953Srrs /* corrupt chunk, maybe at the end? */ 3339163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3340163953Srrs break; 3341163953Srrs } 3342163953Srrs if (trunc_len == 0) { 3343163953Srrs /* we are supposed to have all of it */ 3344163953Srrs if (at > chlen) { 3345163953Srrs /* corrupt skip it */ 3346163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3347163953Srrs break; 3348163953Srrs } 3349163953Srrs } else { 3350163953Srrs /* is there enough of it left ? */ 3351163953Srrs if (desc.chunk_type == SCTP_DATA) { 3352163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3353163953Srrs sizeof(desc.data_bytes))) { 3354163953Srrs break; 3355163953Srrs } 3356163953Srrs } else { 3357163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3358163953Srrs break; 3359163953Srrs } 3360163953Srrs } 3361163953Srrs } 3362163953Srrs if (desc.chunk_type == SCTP_DATA) { 3363163953Srrs /* can we get out the tsn? */ 3364163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3365163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3366163953Srrs 3367163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3368163953Srrs /* yep */ 3369163953Srrs struct sctp_data_chunk *dcp; 3370163953Srrs uint8_t *ddp; 3371163953Srrs unsigned int iii; 3372163953Srrs 3373163953Srrs dcp = (struct sctp_data_chunk *)ch; 3374163953Srrs ddp = (uint8_t *) (dcp + 1); 3375163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3376163953Srrs desc.data_bytes[iii] = ddp[iii]; 3377163953Srrs } 3378163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3379163953Srrs } else { 3380163953Srrs /* nope we are done. */ 3381163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3382163953Srrs break; 3383163953Srrs } 3384163953Srrs } else { 3385163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3386163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3387163953Srrs } 3388163953Srrs 3389163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3390163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3391163953Srrs break; 3392163953Srrs } 3393163953Srrs if (SCTP_SIZE32(at) > chlen) { 3394163953Srrs break; 3395163953Srrs } 3396163953Srrs chlen -= SCTP_SIZE32(at); 3397163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3398163953Srrs /* done, none left */ 3399163953Srrs break; 3400163953Srrs } 3401163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3402163953Srrs } 3403163953Srrs /* Now update any rwnd --- possibly */ 3404163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3405163953Srrs /* From a peer, we get a rwnd report */ 3406163953Srrs uint32_t a_rwnd; 3407163953Srrs 3408163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3409163953Srrs 3410163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3411163953Srrs on_queue = ntohl(cp->current_onq); 3412163953Srrs if (bottle_bw && on_queue) { 3413163953Srrs /* a rwnd report is in here */ 3414163953Srrs if (bottle_bw > on_queue) 3415163953Srrs a_rwnd = bottle_bw - on_queue; 3416163953Srrs else 3417163953Srrs a_rwnd = 0; 3418163953Srrs 3419163953Srrs if (a_rwnd == 0) 3420163953Srrs stcb->asoc.peers_rwnd = 0; 3421163953Srrs else { 3422163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3423163953Srrs stcb->asoc.peers_rwnd = 3424163953Srrs a_rwnd - stcb->asoc.total_flight; 3425163953Srrs } else { 3426163953Srrs stcb->asoc.peers_rwnd = 0; 3427163953Srrs } 3428163953Srrs if (stcb->asoc.peers_rwnd < 3429163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3430163953Srrs /* SWS sender side engages */ 3431163953Srrs stcb->asoc.peers_rwnd = 0; 3432163953Srrs } 3433163953Srrs } 3434163953Srrs } 3435163953Srrs } else { 3436163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3437163953Srrs } 3438163953Srrs 3439163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3440163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3441163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3442163953Srrs (stcb->asoc.sat_network)) { 3443163953Srrs /* 3444163953Srrs * This is debateable but for sat networks it makes sense 3445163953Srrs * Note if a T3 timer has went off, we will prohibit any 3446163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3447163953Srrs */ 3448171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 3449171440Srrs net, cp, &bottle_bw, &on_queue); 3450163953Srrs } 3451163953Srrs} 3452163953Srrs 3453163953Srrs/* 3454163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3455163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3456163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3457163953Srrs * length of the complete packet outputs: - length: modified to remaining 3458163953Srrs * length after control processing - netp: modified to new sctp_nets after 3459163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3460163953Srrs * bad packet,...) otherwise return the tcb for this packet 3461163953Srrs */ 3462170099Srrs#ifdef __GNUC__ 3463170099Srrs__attribute__((noinline)) 3464170099Srrs#endif 3465170099Srrs static struct sctp_tcb * 3466170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3467170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3468170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3469170181Srrs uint32_t vrf_id) 3470163953Srrs{ 3471163953Srrs struct sctp_association *asoc; 3472163953Srrs uint32_t vtag_in; 3473163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3474170140Srrs uint32_t chk_length; 3475163953Srrs int ret; 3476168299Srrs int abort_no_unlock = 0; 3477163953Srrs 3478163953Srrs /* 3479163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3480163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3481163953Srrs * until we get into jumbo grams and such.. 3482163953Srrs */ 3483166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3484163953Srrs struct sctp_tcb *locked_tcb = stcb; 3485163953Srrs int got_auth = 0; 3486163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3487163953Srrs int auth_skipped = 0; 3488163953Srrs 3489169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3490169420Srrs iphlen, *offset, length, stcb); 3491163953Srrs 3492163953Srrs /* validate chunk header length... */ 3493163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3494170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3495170056Srrs ntohs(ch->chunk_length)); 3496170091Srrs if (locked_tcb) { 3497170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3498170091Srrs } 3499163953Srrs return (NULL); 3500163953Srrs } 3501163953Srrs /* 3502163953Srrs * validate the verification tag 3503163953Srrs */ 3504163953Srrs vtag_in = ntohl(sh->v_tag); 3505163953Srrs 3506165220Srrs if (locked_tcb) { 3507165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3508165220Srrs } 3509163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3510170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3511170056Srrs ntohs(ch->chunk_length), vtag_in); 3512163953Srrs if (vtag_in != 0) { 3513163953Srrs /* protocol error- silently discard... */ 3514163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3515169378Srrs if (locked_tcb) { 3516163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3517169378Srrs } 3518163953Srrs return (NULL); 3519163953Srrs } 3520163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3521163953Srrs /* 3522163953Srrs * If there is no stcb, skip the AUTH chunk and process 3523163953Srrs * later after a stcb is found (to validate the lookup was 3524163953Srrs * valid. 3525163953Srrs */ 3526163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3527163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3528163953Srrs /* save this chunk for later processing */ 3529163953Srrs auth_skipped = 1; 3530163953Srrs auth_offset = *offset; 3531163953Srrs auth_len = ntohs(ch->chunk_length); 3532163953Srrs 3533163953Srrs /* (temporarily) move past this chunk */ 3534163953Srrs *offset += SCTP_SIZE32(auth_len); 3535163953Srrs if (*offset >= length) { 3536163953Srrs /* no more data left in the mbuf chain */ 3537163953Srrs *offset = length; 3538170091Srrs if (locked_tcb) { 3539170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3540170091Srrs } 3541163953Srrs return (NULL); 3542163953Srrs } 3543163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3544163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3545163953Srrs } 3546169420Srrs if (ch == NULL) { 3547169420Srrs /* Help */ 3548169420Srrs *offset = length; 3549170091Srrs if (locked_tcb) { 3550170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3551170091Srrs } 3552169420Srrs return (NULL); 3553169420Srrs } 3554163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3555163953Srrs goto process_control_chunks; 3556163953Srrs } 3557163953Srrs /* 3558163953Srrs * first check if it's an ASCONF with an unknown src addr we 3559163953Srrs * need to look inside to find the association 3560163953Srrs */ 3561163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3562163953Srrs /* inp's refcount may be reduced */ 3563163953Srrs SCTP_INP_INCR_REF(inp); 3564163953Srrs 3565163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3566163953Srrs *offset, sh, &inp, netp); 3567163953Srrs if (stcb == NULL) { 3568163953Srrs /* 3569163953Srrs * reduce inp's refcount if not reduced in 3570163953Srrs * sctp_findassociation_ep_asconf(). 3571163953Srrs */ 3572163953Srrs SCTP_INP_DECR_REF(inp); 3573163953Srrs } 3574163953Srrs /* now go back and verify any auth chunk to be sure */ 3575163953Srrs if (auth_skipped && (stcb != NULL)) { 3576163953Srrs struct sctp_auth_chunk *auth; 3577163953Srrs 3578163953Srrs auth = (struct sctp_auth_chunk *) 3579163953Srrs sctp_m_getptr(m, auth_offset, 3580163953Srrs auth_len, chunk_buf); 3581163953Srrs got_auth = 1; 3582163953Srrs auth_skipped = 0; 3583169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3584163953Srrs auth_offset)) { 3585163953Srrs /* auth HMAC failed so dump it */ 3586163953Srrs *offset = length; 3587170091Srrs if (locked_tcb) { 3588170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3589170091Srrs } 3590163953Srrs return (NULL); 3591163953Srrs } else { 3592163953Srrs /* remaining chunks are HMAC checked */ 3593163953Srrs stcb->asoc.authenticated = 1; 3594163953Srrs } 3595163953Srrs } 3596163953Srrs } 3597163953Srrs if (stcb == NULL) { 3598163953Srrs /* no association, so it's out of the blue... */ 3599169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3600170181Srrs vrf_id); 3601163953Srrs *offset = length; 3602169378Srrs if (locked_tcb) { 3603163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3604169378Srrs } 3605163953Srrs return (NULL); 3606163953Srrs } 3607163953Srrs asoc = &stcb->asoc; 3608163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3609163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3610163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3611163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3612163953Srrs if ((vtag_in == asoc->my_vtag) || 3613163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3614163953Srrs (vtag_in == asoc->peer_vtag))) { 3615163953Srrs /* this is valid */ 3616163953Srrs } else { 3617163953Srrs /* drop this packet... */ 3618163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3619169378Srrs if (locked_tcb) { 3620163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3621169378Srrs } 3622163953Srrs return (NULL); 3623163953Srrs } 3624163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3625163953Srrs if (vtag_in != asoc->my_vtag) { 3626163953Srrs /* 3627163953Srrs * this could be a stale SHUTDOWN-ACK or the 3628163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3629163953Srrs * is still hung; we have started a new asoc 3630163953Srrs * but it won't complete until the shutdown 3631163953Srrs * is completed 3632163953Srrs */ 3633169378Srrs if (locked_tcb) { 3634163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3635169378Srrs } 3636163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3637170181Srrs NULL, vrf_id); 3638163953Srrs return (NULL); 3639163953Srrs } 3640163953Srrs } else { 3641163953Srrs /* for all other chunks, vtag must match */ 3642163953Srrs if (vtag_in != asoc->my_vtag) { 3643163953Srrs /* invalid vtag... */ 3644169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3645169420Srrs "invalid vtag: %xh, expect %xh\n", 3646169420Srrs vtag_in, asoc->my_vtag); 3647163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3648169378Srrs if (locked_tcb) { 3649163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3650169378Srrs } 3651163953Srrs *offset = length; 3652163953Srrs return (NULL); 3653163953Srrs } 3654163953Srrs } 3655163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3656163953Srrs /* 3657163953Srrs * process all control chunks... 3658163953Srrs */ 3659163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3660163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3661163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3662163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3663163953Srrs stcb->asoc.overall_error_count = 0; 3664163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3665163953Srrs *netp); 3666163953Srrs } 3667163953Srrsprocess_control_chunks: 3668163953Srrs while (IS_SCTP_CONTROL(ch)) { 3669163953Srrs /* validate chunk length */ 3670163953Srrs chk_length = ntohs(ch->chunk_length); 3671169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 3672169420Srrs ch->chunk_type, chk_length); 3673170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 3674170140Srrs if (chk_length < sizeof(*ch) || 3675170140Srrs (*offset + (int)chk_length) > length) { 3676163953Srrs *offset = length; 3677169378Srrs if (locked_tcb) { 3678163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3679169378Srrs } 3680163953Srrs return (NULL); 3681163953Srrs } 3682163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3683163953Srrs /* 3684163953Srrs * INIT-ACK only gets the init ack "header" portion only 3685163953Srrs * because we don't have to process the peer's COOKIE. All 3686163953Srrs * others get a complete chunk. 3687163953Srrs */ 3688169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3689169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3690163953Srrs /* get an init-ack chunk */ 3691163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3692163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3693163953Srrs if (ch == NULL) { 3694163953Srrs *offset = length; 3695169378Srrs if (locked_tcb) { 3696163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3697169378Srrs } 3698163953Srrs return (NULL); 3699163953Srrs } 3700170642Srrs } else { 3701170786Srrs /* For cookies and all other chunks. */ 3702169208Srrs if (chk_length > sizeof(chunk_buf)) { 3703169208Srrs /* 3704169208Srrs * use just the size of the chunk buffer so 3705170642Srrs * the front part of our chunks fit in 3706170642Srrs * contiguous space up to the chunk buffer 3707170642Srrs * size (508 bytes). For chunks that need to 3708170786Srrs * get more than that they must use the 3709170642Srrs * sctp_m_getptr() function or other means 3710170786Srrs * (e.g. know how to parse mbuf chains). 3711170786Srrs * Cookies do this already. 3712169208Srrs */ 3713169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3714169208Srrs (sizeof(chunk_buf) - 4), 3715169208Srrs chunk_buf); 3716169208Srrs if (ch == NULL) { 3717169208Srrs *offset = length; 3718169378Srrs if (locked_tcb) { 3719169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 3720169378Srrs } 3721169208Srrs return (NULL); 3722169208Srrs } 3723169208Srrs } else { 3724169208Srrs /* We can fit it all */ 3725170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3726170642Srrs chk_length, chunk_buf); 3727170642Srrs if (ch == NULL) { 3728170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 3729170642Srrs *offset = length; 3730170642Srrs if (locked_tcb) { 3731170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 3732169208Srrs } 3733170642Srrs return (NULL); 3734163953Srrs } 3735163953Srrs } 3736163953Srrs } 3737163953Srrs num_chunks++; 3738163953Srrs /* Save off the last place we got a control from */ 3739163953Srrs if (stcb != NULL) { 3740169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 3741163953Srrs /* 3742163953Srrs * allow last_control to be NULL if 3743163953Srrs * ASCONF... ASCONF processing will find the 3744163953Srrs * right net later 3745163953Srrs */ 3746169420Srrs if ((netp != NULL) && (*netp != NULL)) 3747169420Srrs stcb->asoc.last_control_chunk_from = *netp; 3748163953Srrs } 3749163953Srrs } 3750163953Srrs#ifdef SCTP_AUDITING_ENABLED 3751163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3752163953Srrs#endif 3753163953Srrs 3754163953Srrs /* check to see if this chunk required auth, but isn't */ 3755163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3756163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3757163953Srrs stcb->asoc.local_auth_chunks) && 3758163953Srrs !stcb->asoc.authenticated) { 3759163953Srrs /* "silently" ignore */ 3760163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3761163953Srrs goto next_chunk; 3762163953Srrs } 3763163953Srrs switch (ch->chunk_type) { 3764163953Srrs case SCTP_INITIATION: 3765163953Srrs /* must be first and only chunk */ 3766169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 3767163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3768163953Srrs /* We are not interested anymore? */ 3769163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3770163953Srrs /* 3771163953Srrs * collision case where we are 3772163953Srrs * sending to them too 3773163953Srrs */ 3774163953Srrs ; 3775163953Srrs } else { 3776169378Srrs if (locked_tcb) { 3777163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3778169378Srrs } 3779163953Srrs *offset = length; 3780163953Srrs return (NULL); 3781163953Srrs } 3782163953Srrs } 3783163953Srrs if ((num_chunks > 1) || 3784170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3785163953Srrs *offset = length; 3786169378Srrs if (locked_tcb) { 3787163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3788169378Srrs } 3789163953Srrs return (NULL); 3790163953Srrs } 3791163953Srrs if ((stcb != NULL) && 3792163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3793163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3794163953Srrs sctp_send_shutdown_ack(stcb, 3795163953Srrs stcb->asoc.primary_destination); 3796163953Srrs *offset = length; 3797163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3798169378Srrs if (locked_tcb) { 3799163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3800169378Srrs } 3801163953Srrs return (NULL); 3802163953Srrs } 3803169420Srrs if (netp) { 3804169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 3805169420Srrs (struct sctp_init_chunk *)ch, inp, 3806170181Srrs stcb, *netp, &abort_no_unlock, vrf_id); 3807169420Srrs } 3808168299Srrs if (abort_no_unlock) 3809168299Srrs return (NULL); 3810168299Srrs 3811163953Srrs *offset = length; 3812169378Srrs if (locked_tcb) { 3813163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3814169378Srrs } 3815163953Srrs return (NULL); 3816163953Srrs break; 3817170642Srrs case SCTP_PAD_CHUNK: 3818170642Srrs break; 3819163953Srrs case SCTP_INITIATION_ACK: 3820163953Srrs /* must be first and only chunk */ 3821169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 3822163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3823163953Srrs /* We are not interested anymore */ 3824163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3825163953Srrs ; 3826163953Srrs } else { 3827169378Srrs if (locked_tcb) { 3828163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3829169378Srrs } 3830163953Srrs *offset = length; 3831163953Srrs if (stcb) { 3832171440Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 3833163953Srrs } 3834163953Srrs return (NULL); 3835163953Srrs } 3836163953Srrs } 3837163953Srrs if ((num_chunks > 1) || 3838170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3839163953Srrs *offset = length; 3840169378Srrs if (locked_tcb) { 3841163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3842169378Srrs } 3843163953Srrs return (NULL); 3844163953Srrs } 3845169420Srrs if ((netp) && (*netp)) { 3846169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3847170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 3848169420Srrs } else { 3849169420Srrs ret = -1; 3850169420Srrs } 3851163953Srrs /* 3852163953Srrs * Special case, I must call the output routine to 3853163953Srrs * get the cookie echoed 3854163953Srrs */ 3855168299Srrs if (abort_no_unlock) 3856168299Srrs return (NULL); 3857168299Srrs 3858163953Srrs if ((stcb) && ret == 0) 3859163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3860163953Srrs *offset = length; 3861169378Srrs if (locked_tcb) { 3862163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3863169378Srrs } 3864163953Srrs return (NULL); 3865163953Srrs break; 3866163953Srrs case SCTP_SELECTIVE_ACK: 3867169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 3868163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3869163953Srrs { 3870163953Srrs struct sctp_sack_chunk *sack; 3871163953Srrs int abort_now = 0; 3872163953Srrs uint32_t a_rwnd, cum_ack; 3873163953Srrs uint16_t num_seg; 3874163953Srrs int nonce_sum_flag; 3875163953Srrs 3876169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 3877169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 3878169208Srrs *offset = length; 3879169378Srrs if (locked_tcb) { 3880169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 3881169378Srrs } 3882169208Srrs return (NULL); 3883169208Srrs } 3884163953Srrs sack = (struct sctp_sack_chunk *)ch; 3885163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3886163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3887163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3888163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3889170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 3890170606Srrs cum_ack, 3891170606Srrs num_seg, 3892170606Srrs a_rwnd 3893170606Srrs ); 3894163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3895163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3896163953Srrs (num_seg == 0) && 3897163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3898163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3899163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3900163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3901163953Srrs ) { 3902163953Srrs /* 3903163953Srrs * We have a SIMPLE sack having no 3904163953Srrs * prior segments and data on sent 3905163953Srrs * queue to be acked.. Use the 3906163953Srrs * faster path sack processing. We 3907163953Srrs * also allow window update sacks 3908163953Srrs * with no missing segments to go 3909163953Srrs * this way too. 3910163953Srrs */ 3911169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 3912169208Srrs &abort_now); 3913163953Srrs } else { 3914169420Srrs if (netp && *netp) 3915170781Srrs sctp_handle_sack(m, *offset, 3916170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 3917163953Srrs } 3918163953Srrs if (abort_now) { 3919163953Srrs /* ABORT signal from sack processing */ 3920163953Srrs *offset = length; 3921163953Srrs return (NULL); 3922163953Srrs } 3923163953Srrs } 3924163953Srrs break; 3925163953Srrs case SCTP_HEARTBEAT_REQUEST: 3926169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 3927169420Srrs if ((stcb) && netp && *netp) { 3928169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 3929169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 3930169420Srrs chk_length, *netp); 3931169420Srrs 3932169420Srrs /* He's alive so give him credit */ 3933169420Srrs stcb->asoc.overall_error_count = 0; 3934163953Srrs } 3935163953Srrs break; 3936163953Srrs case SCTP_HEARTBEAT_ACK: 3937169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 3938169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 3939169208Srrs /* Its not ours */ 3940169352Srrs *offset = length; 3941169378Srrs if (locked_tcb) { 3942169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3943169378Srrs } 3944169208Srrs return (NULL); 3945169208Srrs } 3946163953Srrs /* He's alive so give him credit */ 3947163953Srrs stcb->asoc.overall_error_count = 0; 3948163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 3949169420Srrs if (netp && *netp) 3950169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 3951169420Srrs stcb, *netp); 3952163953Srrs break; 3953163953Srrs case SCTP_ABORT_ASSOCIATION: 3954170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 3955170091Srrs stcb); 3956169420Srrs if ((stcb) && netp && *netp) 3957169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 3958169420Srrs stcb, *netp); 3959163953Srrs *offset = length; 3960163953Srrs return (NULL); 3961163953Srrs break; 3962163953Srrs case SCTP_SHUTDOWN: 3963170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 3964170091Srrs stcb); 3965169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 3966169208Srrs *offset = length; 3967169378Srrs if (locked_tcb) { 3968169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3969169378Srrs } 3970169208Srrs return (NULL); 3971169208Srrs 3972169420Srrs } 3973169420Srrs if (netp && *netp) { 3974163953Srrs int abort_flag = 0; 3975163953Srrs 3976163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 3977163953Srrs stcb, *netp, &abort_flag); 3978163953Srrs if (abort_flag) { 3979163953Srrs *offset = length; 3980163953Srrs return (NULL); 3981163953Srrs } 3982163953Srrs } 3983163953Srrs break; 3984163953Srrs case SCTP_SHUTDOWN_ACK: 3985170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 3986169420Srrs if ((stcb) && (netp) && (*netp)) 3987169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 3988163953Srrs *offset = length; 3989163953Srrs return (NULL); 3990163953Srrs break; 3991169420Srrs 3992163953Srrs case SCTP_OPERATION_ERROR: 3993169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 3994169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 3995169420Srrs 3996163953Srrs *offset = length; 3997163953Srrs return (NULL); 3998163953Srrs } 3999163953Srrs break; 4000163953Srrs case SCTP_COOKIE_ECHO: 4001169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4002170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4003163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4004163953Srrs ; 4005163953Srrs } else { 4006169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4007163953Srrs /* We are not interested anymore */ 4008163953Srrs *offset = length; 4009163953Srrs return (NULL); 4010163953Srrs } 4011163953Srrs } 4012163953Srrs /* 4013163953Srrs * First are we accepting? We do this again here 4014169420Srrs * sincen it is possible that a previous endpoint 4015169420Srrs * WAS listening responded to a INIT-ACK and then 4016163953Srrs * closed. We opened and bound.. and are now no 4017163953Srrs * longer listening. 4018163953Srrs */ 4019163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4020163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4021163953Srrs /* 4022163953Srrs * special case, is this a retran'd 4023163953Srrs * COOKIE-ECHO or a restarting assoc 4024163953Srrs * that is a peeled off or 4025163953Srrs * one-to-one style socket. 4026163953Srrs */ 4027163953Srrs goto process_cookie_anyway; 4028163953Srrs } 4029169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4030170181Srrs sh, NULL, vrf_id); 4031163953Srrs *offset = length; 4032163953Srrs return (NULL); 4033163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4034163953Srrs /* we are accepting so check limits like TCP */ 4035163953Srrs if (inp->sctp_socket->so_qlen > 4036163953Srrs inp->sctp_socket->so_qlimit) { 4037163953Srrs /* no space */ 4038163953Srrs struct mbuf *oper; 4039163953Srrs struct sctp_paramhdr *phdr; 4040163953Srrs 4041163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4042163953Srrs oper = NULL; 4043163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4044165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4045163953Srrs if (oper) { 4046165647Srrs SCTP_BUF_LEN(oper) = 4047163953Srrs sizeof(struct sctp_paramhdr); 4048163953Srrs phdr = mtod(oper, 4049163953Srrs struct sctp_paramhdr *); 4050163953Srrs phdr->param_type = 4051163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4052163953Srrs phdr->param_length = 4053163953Srrs htons(sizeof(struct sctp_paramhdr)); 4054163953Srrs } 4055163953Srrs sctp_abort_association(inp, stcb, m, 4056170181Srrs iphlen, sh, oper, vrf_id); 4057163953Srrs } 4058163953Srrs *offset = length; 4059163953Srrs return (NULL); 4060163953Srrs } 4061163953Srrs } 4062163953Srrs process_cookie_anyway: 4063163953Srrs { 4064163953Srrs struct mbuf *ret_buf; 4065165220Srrs struct sctp_inpcb *linp; 4066163953Srrs 4067169420Srrs if (stcb) { 4068165220Srrs linp = NULL; 4069169420Srrs } else { 4070165220Srrs linp = inp; 4071169420Srrs } 4072165220Srrs 4073169420Srrs if (linp) { 4074165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4075169420Srrs } 4076169420Srrs if (netp) { 4077169420Srrs ret_buf = 4078169420Srrs sctp_handle_cookie_echo(m, iphlen, 4079169420Srrs *offset, sh, 4080169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4081169420Srrs &inp, &stcb, netp, 4082169420Srrs auth_skipped, 4083169420Srrs auth_offset, 4084169420Srrs auth_len, 4085169420Srrs &locked_tcb, 4086170181Srrs vrf_id); 4087169420Srrs } else { 4088169420Srrs ret_buf = NULL; 4089169420Srrs } 4090169420Srrs if (linp) { 4091165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4092169420Srrs } 4093163953Srrs if (ret_buf == NULL) { 4094163953Srrs if (locked_tcb) { 4095163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4096163953Srrs } 4097169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4098169420Srrs "GAK, null buffer\n"); 4099163953Srrs auth_skipped = 0; 4100163953Srrs *offset = length; 4101163953Srrs return (NULL); 4102163953Srrs } 4103163953Srrs /* if AUTH skipped, see if it verified... */ 4104163953Srrs if (auth_skipped) { 4105163953Srrs got_auth = 1; 4106163953Srrs auth_skipped = 0; 4107163953Srrs } 4108163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4109163953Srrs /* 4110163953Srrs * Restart the timer if we have 4111163953Srrs * pending data 4112163953Srrs */ 4113163953Srrs struct sctp_tmit_chunk *chk; 4114163953Srrs 4115163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4116163953Srrs if (chk) { 4117163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4118163953Srrs stcb->sctp_ep, stcb, 4119163953Srrs chk->whoTo); 4120163953Srrs } 4121163953Srrs } 4122163953Srrs } 4123163953Srrs break; 4124163953Srrs case SCTP_COOKIE_ACK: 4125170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4126169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4127169378Srrs if (locked_tcb) { 4128169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4129169378Srrs } 4130169352Srrs return (NULL); 4131169352Srrs } 4132163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4133163953Srrs /* We are not interested anymore */ 4134163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4135163953Srrs ; 4136169420Srrs } else if (stcb) { 4137165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4138163953Srrs *offset = length; 4139163953Srrs return (NULL); 4140163953Srrs } 4141163953Srrs } 4142163953Srrs /* He's alive so give him credit */ 4143169420Srrs if ((stcb) && netp && *netp) { 4144169378Srrs stcb->asoc.overall_error_count = 0; 4145169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4146169378Srrs } 4147163953Srrs break; 4148163953Srrs case SCTP_ECN_ECHO: 4149169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4150163953Srrs /* He's alive so give him credit */ 4151169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4152169208Srrs /* Its not ours */ 4153169378Srrs if (locked_tcb) { 4154169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4155169378Srrs } 4156169208Srrs *offset = length; 4157169208Srrs return (NULL); 4158169208Srrs } 4159169378Srrs if (stcb) { 4160169378Srrs stcb->asoc.overall_error_count = 0; 4161169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4162169378Srrs stcb); 4163169378Srrs } 4164163953Srrs break; 4165163953Srrs case SCTP_ECN_CWR: 4166169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4167163953Srrs /* He's alive so give him credit */ 4168169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4169169208Srrs /* Its not ours */ 4170169378Srrs if (locked_tcb) { 4171169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4172169378Srrs } 4173169208Srrs *offset = length; 4174169208Srrs return (NULL); 4175169208Srrs } 4176169378Srrs if (stcb) { 4177169378Srrs stcb->asoc.overall_error_count = 0; 4178169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4179169378Srrs } 4180163953Srrs break; 4181163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4182170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4183163953Srrs /* must be first and only chunk */ 4184163953Srrs if ((num_chunks > 1) || 4185170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4186163953Srrs *offset = length; 4187169378Srrs if (locked_tcb) { 4188163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4189169378Srrs } 4190163953Srrs return (NULL); 4191163953Srrs } 4192169420Srrs if ((stcb) && netp && *netp) { 4193169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4194169378Srrs stcb, *netp); 4195169378Srrs } 4196163953Srrs *offset = length; 4197163953Srrs return (NULL); 4198163953Srrs break; 4199163953Srrs case SCTP_ASCONF: 4200169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4201163953Srrs /* He's alive so give him credit */ 4202169378Srrs if (stcb) { 4203169378Srrs stcb->asoc.overall_error_count = 0; 4204169378Srrs sctp_handle_asconf(m, *offset, 4205169378Srrs (struct sctp_asconf_chunk *)ch, stcb); 4206169378Srrs } 4207163953Srrs break; 4208163953Srrs case SCTP_ASCONF_ACK: 4209169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4210169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4211169208Srrs /* Its not ours */ 4212169378Srrs if (locked_tcb) { 4213169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4214169378Srrs } 4215169208Srrs *offset = length; 4216169208Srrs return (NULL); 4217169208Srrs } 4218169420Srrs if ((stcb) && netp && *netp) { 4219169378Srrs /* He's alive so give him credit */ 4220169378Srrs stcb->asoc.overall_error_count = 0; 4221169378Srrs sctp_handle_asconf_ack(m, *offset, 4222169378Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4223169378Srrs } 4224163953Srrs break; 4225163953Srrs case SCTP_FORWARD_CUM_TSN: 4226169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4227169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4228169208Srrs /* Its not ours */ 4229169378Srrs if (locked_tcb) { 4230169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4231169378Srrs } 4232169208Srrs *offset = length; 4233169208Srrs return (NULL); 4234169208Srrs } 4235163953Srrs /* He's alive so give him credit */ 4236169378Srrs if (stcb) { 4237163953Srrs int abort_flag = 0; 4238163953Srrs 4239163953Srrs stcb->asoc.overall_error_count = 0; 4240163953Srrs *fwd_tsn_seen = 1; 4241163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4242163953Srrs /* We are not interested anymore */ 4243171440Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4244163953Srrs *offset = length; 4245163953Srrs return (NULL); 4246163953Srrs } 4247163953Srrs sctp_handle_forward_tsn(stcb, 4248170992Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 4249163953Srrs if (abort_flag) { 4250163953Srrs *offset = length; 4251163953Srrs return (NULL); 4252163953Srrs } else { 4253163953Srrs stcb->asoc.overall_error_count = 0; 4254163953Srrs } 4255163953Srrs 4256163953Srrs } 4257163953Srrs break; 4258163953Srrs case SCTP_STREAM_RESET: 4259169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4260169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4261169208Srrs /* Its not ours */ 4262169378Srrs if (locked_tcb) { 4263169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4264169378Srrs } 4265169208Srrs *offset = length; 4266169208Srrs return (NULL); 4267169208Srrs } 4268163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4269163953Srrs /* We are not interested anymore */ 4270171440Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4271163953Srrs *offset = length; 4272163953Srrs return (NULL); 4273163953Srrs } 4274169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4275169420Srrs /* 4276169420Srrs * hmm, peer should have announced this, but 4277169420Srrs * we will turn it on since he is sending us 4278169420Srrs * a stream reset. 4279169420Srrs */ 4280169420Srrs stcb->asoc.peer_supports_strreset = 1; 4281163953Srrs } 4282170992Srrs if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 4283169420Srrs /* stop processing */ 4284169420Srrs *offset = length; 4285169420Srrs return (NULL); 4286169420Srrs } 4287163953Srrs break; 4288163953Srrs case SCTP_PACKET_DROPPED: 4289169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4290163953Srrs /* re-get it all please */ 4291169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4292169208Srrs /* Its not ours */ 4293169378Srrs if (locked_tcb) { 4294169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4295169378Srrs } 4296169208Srrs *offset = length; 4297169208Srrs return (NULL); 4298169208Srrs } 4299169420Srrs if (ch && (stcb) && netp && (*netp)) { 4300169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4301170781Srrs stcb, *netp, 4302170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4303170781Srrs 4304169378Srrs } 4305163953Srrs break; 4306163953Srrs 4307163953Srrs case SCTP_AUTHENTICATION: 4308169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4309163953Srrs if (sctp_auth_disable) 4310163953Srrs goto unknown_chunk; 4311163953Srrs 4312163953Srrs if (stcb == NULL) { 4313163953Srrs /* save the first AUTH for later processing */ 4314163953Srrs if (auth_skipped == 0) { 4315163953Srrs auth_offset = *offset; 4316163953Srrs auth_len = chk_length; 4317163953Srrs auth_skipped = 1; 4318163953Srrs } 4319163953Srrs /* skip this chunk (temporarily) */ 4320163953Srrs goto next_chunk; 4321163953Srrs } 4322169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4323169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4324169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4325169208Srrs /* Its not ours */ 4326169378Srrs if (locked_tcb) { 4327169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4328169378Srrs } 4329169208Srrs *offset = length; 4330169208Srrs return (NULL); 4331169208Srrs } 4332163953Srrs if (got_auth == 1) { 4333163953Srrs /* skip this chunk... it's already auth'd */ 4334163953Srrs goto next_chunk; 4335163953Srrs } 4336163953Srrs got_auth = 1; 4337169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4338163953Srrs m, *offset)) { 4339163953Srrs /* auth HMAC failed so dump the packet */ 4340163953Srrs *offset = length; 4341163953Srrs return (stcb); 4342163953Srrs } else { 4343163953Srrs /* remaining chunks are HMAC checked */ 4344163953Srrs stcb->asoc.authenticated = 1; 4345163953Srrs } 4346163953Srrs break; 4347163953Srrs 4348163953Srrs default: 4349163953Srrs unknown_chunk: 4350163953Srrs /* it's an unknown chunk! */ 4351163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4352163953Srrs struct mbuf *mm; 4353163953Srrs struct sctp_paramhdr *phd; 4354163953Srrs 4355163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4356165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4357163953Srrs if (mm) { 4358163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4359163953Srrs /* 4360163953Srrs * We cheat and use param type since 4361163953Srrs * we did not bother to define a 4362163953Srrs * error cause struct. They are the 4363163953Srrs * same basic format with different 4364163953Srrs * names. 4365163953Srrs */ 4366163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4367163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4368165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4369166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4370163953Srrs M_DONTWAIT); 4371165647Srrs if (SCTP_BUF_NEXT(mm)) { 4372163953Srrs sctp_queue_op_err(stcb, mm); 4373163953Srrs } else { 4374163953Srrs sctp_m_freem(mm); 4375163953Srrs } 4376163953Srrs } 4377163953Srrs } 4378163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4379163953Srrs /* discard this packet */ 4380163953Srrs *offset = length; 4381163953Srrs return (stcb); 4382163953Srrs } /* else skip this bad chunk and continue... */ 4383163953Srrs break; 4384163953Srrs } /* switch (ch->chunk_type) */ 4385163953Srrs 4386163953Srrs 4387163953Srrsnext_chunk: 4388163953Srrs /* get the next chunk */ 4389163953Srrs *offset += SCTP_SIZE32(chk_length); 4390163953Srrs if (*offset >= length) { 4391163953Srrs /* no more data left in the mbuf chain */ 4392163953Srrs break; 4393163953Srrs } 4394163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4395163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4396163953Srrs if (ch == NULL) { 4397169378Srrs if (locked_tcb) { 4398163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4399169378Srrs } 4400163953Srrs *offset = length; 4401163953Srrs return (NULL); 4402163953Srrs } 4403163953Srrs } /* while */ 4404163953Srrs return (stcb); 4405163953Srrs} 4406163953Srrs 4407163953Srrs 4408163953Srrs/* 4409163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4410163953Srrs * ECN(0) or ECN(1) or CE 4411163953Srrs */ 4412170806Srrsstatic void 4413163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4414163953Srrs uint8_t ecn_bits) 4415163953Srrs{ 4416163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4417163953Srrs ; 4418163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4419163953Srrs /* 4420163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4421163953Srrs * change the NS bit (that we have yet to find a way to send 4422163953Srrs * it yet). 4423163953Srrs */ 4424163953Srrs 4425163953Srrs /* ECN Nonce stuff */ 4426163953Srrs stcb->asoc.receiver_nonce_sum++; 4427163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4428163953Srrs 4429163953Srrs /* 4430163953Srrs * Drag up the last_echo point if cumack is larger since we 4431163953Srrs * don't want the point falling way behind by more than 4432163953Srrs * 2^^31 and then having it be incorrect. 4433163953Srrs */ 4434163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4435163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4436163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4437163953Srrs } 4438163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4439163953Srrs /* 4440163953Srrs * Drag up the last_echo point if cumack is larger since we 4441163953Srrs * don't want the point falling way behind by more than 4442163953Srrs * 2^^31 and then having it be incorrect. 4443163953Srrs */ 4444163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4445163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4446163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4447163953Srrs } 4448163953Srrs } 4449163953Srrs} 4450163953Srrs 4451170806Srrsstatic void 4452163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4453163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4454163953Srrs{ 4455163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4456163953Srrs /* 4457163953Srrs * we possibly must notify the sender that a congestion 4458163953Srrs * window reduction is in order. We do this by adding a ECNE 4459163953Srrs * chunk to the output chunk queue. The incoming CWR will 4460163953Srrs * remove this chunk. 4461163953Srrs */ 4462163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4463163953Srrs MAX_TSN)) { 4464163953Srrs /* Yep, we need to add a ECNE */ 4465163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4466163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4467163953Srrs } 4468163953Srrs } 4469163953Srrs} 4470163953Srrs 4471171440Srrs#ifdef INVARIANTS 4472171440Srrsstatic void 4473171440Srrssctp_validate_no_locks(struct sctp_inpcb *inp) 4474171440Srrs{ 4475171440Srrs struct sctp_tcb *stcb; 4476171440Srrs 4477171440Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4478171440Srrs if (mtx_owned(&stcb->tcb_mtx)) { 4479171440Srrs panic("Own lock on stcb at return from input"); 4480171440Srrs } 4481171440Srrs } 4482171440Srrs} 4483171440Srrs 4484171440Srrs#endif 4485171440Srrs 4486163953Srrs/* 4487163953Srrs * common input chunk processing (v4 and v6) 4488163953Srrs */ 4489169378Srrsvoid 4490163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4491163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4492163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4493170181Srrs uint8_t ecn_bits, uint32_t vrf_id) 4494163953Srrs{ 4495163953Srrs /* 4496163953Srrs * Control chunk processing 4497163953Srrs */ 4498163953Srrs uint32_t high_tsn; 4499163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4500163953Srrs struct mbuf *m = *mm; 4501163953Srrs int abort_flag = 0; 4502163953Srrs int un_sent; 4503163953Srrs 4504163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4505163953Srrs#ifdef SCTP_AUDITING_ENABLED 4506163953Srrs sctp_audit_log(0xE0, 1); 4507163953Srrs sctp_auditing(0, inp, stcb, net); 4508163953Srrs#endif 4509163953Srrs 4510169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4511169420Srrs m, iphlen, offset); 4512163953Srrs 4513163953Srrs if (stcb) { 4514163953Srrs /* always clear this before beginning a packet */ 4515163953Srrs stcb->asoc.authenticated = 0; 4516163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4517171745Srrs if (stcb->asoc.state & SCTP_STATE_WAS_ABORTED) { 4518171745Srrs /*- 4519171745Srrs * If we hit here, we had a ref count 4520171745Srrs * up when the assoc was aborted and the 4521171745Srrs * timer is clearing out the assoc, we should 4522171745Srrs * NOT respond to any packet.. its OOTB. 4523171745Srrs */ 4524171745Srrs SCTP_TCB_UNLOCK(stcb); 4525171745Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4526171745Srrs vrf_id); 4527171745Srrs goto out_now; 4528171745Srrs } 4529163953Srrs } 4530163953Srrs if (IS_SCTP_CONTROL(ch)) { 4531163953Srrs /* process the control portion of the SCTP packet */ 4532169655Srrs /* sa_ignore NO_NULL_CHK */ 4533163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4534170181Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id); 4535163953Srrs if (stcb) { 4536163953Srrs /* 4537163953Srrs * This covers us if the cookie-echo was there and 4538163953Srrs * it changes our INP. 4539163953Srrs */ 4540163953Srrs inp = stcb->sctp_ep; 4541163953Srrs } 4542163953Srrs } else { 4543163953Srrs /* 4544163953Srrs * no control chunks, so pre-process DATA chunks (these 4545163953Srrs * checks are taken care of by control processing) 4546163953Srrs */ 4547163953Srrs 4548163953Srrs /* 4549163953Srrs * if DATA only packet, and auth is required, then punt... 4550163953Srrs * can't have authenticated without any AUTH (control) 4551163953Srrs * chunks 4552163953Srrs */ 4553163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4554163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4555163953Srrs stcb->asoc.local_auth_chunks)) { 4556163953Srrs /* "silently" ignore */ 4557163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4558163953Srrs SCTP_TCB_UNLOCK(stcb); 4559171440Srrs goto out_now; 4560163953Srrs } 4561163953Srrs if (stcb == NULL) { 4562163953Srrs /* out of the blue DATA chunk */ 4563169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4564170181Srrs vrf_id); 4565171440Srrs goto out_now; 4566163953Srrs } 4567163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4568163953Srrs /* v_tag mismatch! */ 4569163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4570163953Srrs SCTP_TCB_UNLOCK(stcb); 4571171440Srrs goto out_now; 4572163953Srrs } 4573163953Srrs } 4574163953Srrs 4575163953Srrs if (stcb == NULL) { 4576163953Srrs /* 4577163953Srrs * no valid TCB for this packet, or we found it's a bad 4578163953Srrs * packet while processing control, or we're done with this 4579163953Srrs * packet (done or skip rest of data), so we drop it... 4580163953Srrs */ 4581171440Srrs goto out_now; 4582163953Srrs } 4583163953Srrs /* 4584163953Srrs * DATA chunk processing 4585163953Srrs */ 4586163953Srrs /* plow through the data chunks while length > offset */ 4587163953Srrs 4588163953Srrs /* 4589163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4590163953Srrs * DATA is required. 4591163953Srrs */ 4592165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4593163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4594163953Srrs stcb->asoc.local_auth_chunks) && 4595163953Srrs !stcb->asoc.authenticated) { 4596163953Srrs /* "silently" ignore */ 4597163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4598169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 4599169420Srrs "Data chunk requires AUTH, skipped\n"); 4600165220Srrs goto trigger_send; 4601163953Srrs } 4602163953Srrs if (length > offset) { 4603163953Srrs int retval; 4604163953Srrs 4605163953Srrs /* 4606163953Srrs * First check to make sure our state is correct. We would 4607163953Srrs * not get here unless we really did have a tag, so we don't 4608163953Srrs * abort if this happens, just dump the chunk silently. 4609163953Srrs */ 4610163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4611163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4612163953Srrs /* 4613163953Srrs * we consider data with valid tags in this state 4614163953Srrs * shows us the cookie-ack was lost. Imply it was 4615163953Srrs * there. 4616163953Srrs */ 4617163953Srrs stcb->asoc.overall_error_count = 0; 4618163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4619163953Srrs break; 4620163953Srrs case SCTP_STATE_COOKIE_WAIT: 4621163953Srrs /* 4622163953Srrs * We consider OOTB any data sent during asoc setup. 4623163953Srrs */ 4624169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4625170181Srrs vrf_id); 4626163953Srrs SCTP_TCB_UNLOCK(stcb); 4627171440Srrs goto out_now; 4628171531Srrs /* sa_ignore NOTREACHED */ 4629163953Srrs break; 4630163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4631163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4632163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4633163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4634163953Srrs default: 4635163953Srrs SCTP_TCB_UNLOCK(stcb); 4636171440Srrs goto out_now; 4637171531Srrs /* sa_ignore NOTREACHED */ 4638163953Srrs break; 4639163953Srrs case SCTP_STATE_OPEN: 4640163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4641163953Srrs break; 4642163953Srrs } 4643163953Srrs /* take care of ECN, part 1. */ 4644163953Srrs if (stcb->asoc.ecn_allowed && 4645163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4646163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4647163953Srrs } 4648163953Srrs /* plow through the data chunks while length > offset */ 4649163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4650163953Srrs inp, stcb, net, &high_tsn); 4651163953Srrs if (retval == 2) { 4652163953Srrs /* 4653163953Srrs * The association aborted, NO UNLOCK needed since 4654163953Srrs * the association is destroyed. 4655163953Srrs */ 4656171440Srrs goto out_now; 4657163953Srrs } 4658163953Srrs data_processed = 1; 4659163953Srrs if (retval == 0) { 4660163953Srrs /* take care of ecn part 2. */ 4661163953Srrs if (stcb->asoc.ecn_allowed && 4662163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4663163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4664163953Srrs ecn_bits); 4665163953Srrs } 4666163953Srrs } 4667163953Srrs /* 4668163953Srrs * Anything important needs to have been m_copy'ed in 4669163953Srrs * process_data 4670163953Srrs */ 4671163953Srrs } 4672163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4673163953Srrs int was_a_gap = 0; 4674163953Srrs 4675163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4676163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4677163953Srrs /* there was a gap before this data was processed */ 4678163953Srrs was_a_gap = 1; 4679163953Srrs } 4680163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4681163953Srrs if (abort_flag) { 4682163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4683171440Srrs goto out_now; 4684163953Srrs } 4685163953Srrs } 4686163953Srrs /* trigger send of any chunks in queue... */ 4687165220Srrstrigger_send: 4688163953Srrs#ifdef SCTP_AUDITING_ENABLED 4689163953Srrs sctp_audit_log(0xE0, 2); 4690163953Srrs sctp_auditing(1, inp, stcb, net); 4691163953Srrs#endif 4692169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4693169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 4694169420Srrs stcb->asoc.peers_rwnd, 4695169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4696169420Srrs stcb->asoc.total_flight); 4697163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4698163953Srrs 4699163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4700163953Srrs ((un_sent) && 4701163953Srrs (stcb->asoc.peers_rwnd > 0 || 4702163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4703169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 4704163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4705169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 4706163953Srrs } 4707163953Srrs#ifdef SCTP_AUDITING_ENABLED 4708163953Srrs sctp_audit_log(0xE0, 3); 4709163953Srrs sctp_auditing(2, inp, stcb, net); 4710163953Srrs#endif 4711163953Srrs SCTP_TCB_UNLOCK(stcb); 4712171440Srrsout_now: 4713171440Srrs#ifdef INVARIANTS 4714171440Srrs sctp_validate_no_locks(inp); 4715171440Srrs#endif 4716169378Srrs return; 4717163953Srrs} 4718163953Srrs 4719163953Srrs 4720163953Srrs 4721163953Srrsvoid 4722165647Srrssctp_input(i_pak, off) 4723165647Srrs struct mbuf *i_pak; 4724163953Srrs int off; 4725163953Srrs 4726163953Srrs{ 4727163953Srrs#ifdef SCTP_MBUF_LOGGING 4728163953Srrs struct mbuf *mat; 4729163953Srrs 4730163953Srrs#endif 4731165647Srrs struct mbuf *m; 4732163953Srrs int iphlen; 4733170181Srrs uint32_t vrf_id = 0; 4734163953Srrs uint8_t ecn_bits; 4735163953Srrs struct ip *ip; 4736163953Srrs struct sctphdr *sh; 4737163953Srrs struct sctp_inpcb *inp = NULL; 4738163953Srrs 4739163953Srrs uint32_t check, calc_check; 4740163953Srrs struct sctp_nets *net; 4741163953Srrs struct sctp_tcb *stcb = NULL; 4742163953Srrs struct sctp_chunkhdr *ch; 4743163953Srrs int refcount_up = 0; 4744163953Srrs int length, mlen, offset; 4745163953Srrs 4746169352Srrs 4747169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 4748169352Srrs SCTP_RELEASE_PKT(i_pak); 4749169352Srrs return; 4750169352Srrs } 4751168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 4752163953Srrs iphlen = off; 4753165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4754170056Srrs 4755163953Srrs net = NULL; 4756163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4757163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4758163953Srrs 4759170091Srrs 4760163953Srrs#ifdef SCTP_MBUF_LOGGING 4761163953Srrs /* Log in any input mbufs */ 4762170744Srrs if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) { 4763170744Srrs mat = m; 4764170744Srrs while (mat) { 4765170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4766170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4767170744Srrs } 4768170744Srrs mat = SCTP_BUF_NEXT(mat); 4769163953Srrs } 4770163953Srrs } 4771163953Srrs#endif 4772170091Srrs#ifdef SCTP_PACKET_LOGGING 4773170921Srrs if (sctp_logging_level & SCTP_LAST_PACKET_TRACING) 4774170921Srrs sctp_packet_log(m, mlen); 4775170091Srrs#endif 4776170091Srrs /* 4777170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 4778170091Srrs * case) 4779170091Srrs */ 4780170091Srrs mlen -= iphlen; 4781168124Srrs 4782163953Srrs /* 4783163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4784163953Srrs */ 4785163953Srrs ip = mtod(m, struct ip *); 4786163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4787165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4788163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4789163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4790163953Srrs return; 4791163953Srrs } 4792163953Srrs ip = mtod(m, struct ip *); 4793163953Srrs } 4794163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4795163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4796170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4797170859Srrs "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 4798163953Srrs 4799163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4800163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4801163953Srrs goto bad; 4802163953Srrs } 4803168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4804163953Srrs /* 4805163953Srrs * We only look at broadcast if its a front state, All 4806163953Srrs * others we will not have a tcb for anyway. 4807163953Srrs */ 4808163953Srrs goto bad; 4809163953Srrs } 4810163953Srrs /* validate SCTP checksum */ 4811168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4812163953Srrs /* 4813163953Srrs * we do NOT validate things from the loopback if the sysctl 4814163953Srrs * is set to 1. 4815163953Srrs */ 4816163953Srrs check = sh->checksum; /* save incoming checksum */ 4817163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4818163953Srrs /* 4819163953Srrs * special hook for where we got a local address 4820163953Srrs * somehow routed across a non IFT_LOOP type 4821163953Srrs * interface 4822163953Srrs */ 4823163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4824163953Srrs goto sctp_skip_csum_4; 4825163953Srrs } 4826163953Srrs sh->checksum = 0; /* prepare for calc */ 4827163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4828163953Srrs if (calc_check != check) { 4829169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4830169420Srrs calc_check, check, m, mlen, iphlen); 4831163953Srrs 4832163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4833163953Srrs offset - sizeof(*ch), 4834169420Srrs sh, ch, &inp, &net, 4835169420Srrs vrf_id); 4836163953Srrs if ((inp) && (stcb)) { 4837163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4838163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4839163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4840163953Srrs refcount_up = 1; 4841163953Srrs } 4842163953Srrs SCTP_STAT_INCR(sctps_badsum); 4843163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4844163953Srrs goto bad; 4845163953Srrs } 4846163953Srrs sh->checksum = calc_check; 4847168299Srrs } 4848163953Srrssctp_skip_csum_4: 4849168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4850168124Srrs if (sh->dest_port == 0) { 4851168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4852168124Srrs goto bad; 4853168124Srrs } 4854163953Srrs /* validate mbuf chain length with IP payload length */ 4855163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4856163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4857163953Srrs goto bad; 4858163953Srrs } 4859163953Srrs /* 4860163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4861163953Srrs * IP/SCTP/first chunk header... 4862163953Srrs */ 4863163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4864168299Srrs sh, ch, &inp, &net, vrf_id); 4865163953Srrs /* inp's ref-count increased && stcb locked */ 4866163953Srrs if (inp == NULL) { 4867163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4868163953Srrs 4869163953Srrs SCTP_STAT_INCR(sctps_noport); 4870163953Srrs#ifdef ICMP_BANDLIM 4871163953Srrs /* 4872163953Srrs * we use the bandwidth limiting to protect against sending 4873163953Srrs * too many ABORTS all at once. In this case these count the 4874163953Srrs * same as an ICMP message. 4875163953Srrs */ 4876163953Srrs if (badport_bandlim(0) < 0) 4877163953Srrs goto bad; 4878163953Srrs#endif /* ICMP_BANDLIM */ 4879169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4880169420Srrs "Sending a ABORT from packet entry!\n"); 4881163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4882163953Srrs /* 4883163953Srrs * we do a trick here to get the INIT tag, dig in 4884163953Srrs * and get the tag from the INIT and put it in the 4885163953Srrs * common header. 4886163953Srrs */ 4887163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4888163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4889163953Srrs (uint8_t *) & chunk_buf); 4890163953Srrs if (init_chk != NULL) 4891163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4892163953Srrs } 4893165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4894170181Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id); 4895165220Srrs goto bad; 4896165220Srrs } 4897165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4898165220Srrs goto bad; 4899165220Srrs } 4900165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4901170181Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id); 4902163953Srrs goto bad; 4903163953Srrs } else if (stcb == NULL) { 4904163953Srrs refcount_up = 1; 4905163953Srrs } 4906171167Sgnn#ifdef IPSEC 4907163953Srrs /* 4908163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 4909163953Srrs * idea, so I will leave it in place. 4910163953Srrs */ 4911163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 4912171133Sgnn ipsec4stat.in_polvio++; 4913163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4914163953Srrs goto bad; 4915163953Srrs } 4916163953Srrs#endif /* IPSEC */ 4917163953Srrs 4918163953Srrs /* 4919163953Srrs * common chunk processing 4920163953Srrs */ 4921163953Srrs length = ip->ip_len + iphlen; 4922163953Srrs offset -= sizeof(struct sctp_chunkhdr); 4923163953Srrs 4924163953Srrs ecn_bits = ip->ip_tos; 4925163953Srrs 4926169655Srrs /* sa_ignore NO_NULL_CHK */ 4927163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 4928170181Srrs inp, stcb, net, ecn_bits, vrf_id); 4929163953Srrs /* inp's ref-count reduced && stcb unlocked */ 4930163953Srrs if (m) { 4931163953Srrs sctp_m_freem(m); 4932163953Srrs } 4933163953Srrs if ((inp) && (refcount_up)) { 4934163953Srrs /* reduce ref-count */ 4935163953Srrs SCTP_INP_WLOCK(inp); 4936163953Srrs SCTP_INP_DECR_REF(inp); 4937163953Srrs SCTP_INP_WUNLOCK(inp); 4938163953Srrs } 4939163953Srrs return; 4940163953Srrsbad: 4941169420Srrs if (stcb) { 4942163953Srrs SCTP_TCB_UNLOCK(stcb); 4943169420Srrs } 4944163953Srrs if ((inp) && (refcount_up)) { 4945163953Srrs /* reduce ref-count */ 4946163953Srrs SCTP_INP_WLOCK(inp); 4947163953Srrs SCTP_INP_DECR_REF(inp); 4948163953Srrs SCTP_INP_WUNLOCK(inp); 4949163953Srrs } 4950163953Srrs if (m) { 4951163953Srrs sctp_m_freem(m); 4952163953Srrs } 4953163953Srrs return; 4954163953Srrs} 4955