sctp_input.c revision 170781
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 170781 2007-06-15 17:59:57Z 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); 130163953Srrs return; 131163953Srrs } 132163953Srrs if (init->num_inbound_streams == 0) { 133163953Srrs /* protocol error... send abort */ 134163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 135169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 136170181Srrs vrf_id); 137168299Srrs if (stcb) 138168299Srrs *abort_no_unlock = 1; 139163953Srrs return; 140163953Srrs } 141163953Srrs if (init->num_outbound_streams == 0) { 142163953Srrs /* protocol error... send abort */ 143163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 144169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 145170181Srrs vrf_id); 146168299Srrs if (stcb) 147168299Srrs *abort_no_unlock = 1; 148163953Srrs return; 149163953Srrs } 150163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 151163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 152163953Srrs init_limit)) { 153163953Srrs /* auth parameter(s) error... send abort */ 154170181Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id); 155168299Srrs if (stcb) 156168299Srrs *abort_no_unlock = 1; 157163953Srrs return; 158163953Srrs } 159163953Srrs /* send an INIT-ACK w/cookie */ 160169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 161170181Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id); 162163953Srrs} 163163953Srrs 164163953Srrs/* 165163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 166163953Srrs */ 167163953Srrsstatic int 168163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 169163953Srrs struct sctp_nets *net) 170163953Srrs{ 171163953Srrs struct sctp_init *init; 172163953Srrs struct sctp_association *asoc; 173163953Srrs struct sctp_nets *lnet; 174163953Srrs unsigned int i; 175163953Srrs 176163953Srrs init = &cp->init; 177163953Srrs asoc = &stcb->asoc; 178163953Srrs /* save off parameters */ 179163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 180163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 181163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 182163953Srrs /* update any ssthresh's that may have a default */ 183163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 184163953Srrs lnet->ssthresh = asoc->peers_rwnd; 185163953Srrs 186170744Srrs if (sctp_logging_level & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 187170744Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 188170744Srrs } 189163953Srrs } 190163953Srrs } 191164181Srrs SCTP_TCB_SEND_LOCK(stcb); 192163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 193163953Srrs unsigned int newcnt; 194163953Srrs struct sctp_stream_out *outs; 195163953Srrs struct sctp_stream_queue_pending *sp; 196163953Srrs 197163953Srrs /* cut back on number of streams */ 198163953Srrs newcnt = ntohs(init->num_inbound_streams); 199163953Srrs /* This if is probably not needed but I am cautious */ 200163953Srrs if (asoc->strmout) { 201163953Srrs /* First make sure no data chunks are trapped */ 202163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 203163953Srrs outs = &asoc->strmout[i]; 204163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 205163953Srrs while (sp) { 206163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 207163953Srrs next); 208163953Srrs asoc->stream_queue_cnt--; 209163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 210163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 211163953Srrs sp); 212163953Srrs if (sp->data) { 213163953Srrs sctp_m_freem(sp->data); 214163953Srrs sp->data = NULL; 215163953Srrs } 216163953Srrs sctp_free_remote_addr(sp->net); 217163953Srrs sp->net = NULL; 218163953Srrs /* Free the chunk */ 219169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 220163979Sru sp, stcb); 221163953Srrs 222163953Srrs sctp_free_a_strmoq(stcb, sp); 223169655Srrs /* sa_ignore FREED_MEMORY */ 224163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 225163953Srrs } 226163953Srrs } 227163953Srrs } 228163953Srrs /* cut back the count and abandon the upper streams */ 229163953Srrs asoc->pre_open_streams = newcnt; 230163953Srrs } 231164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 232163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 233163953Srrs /* init tsn's */ 234163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 235170744Srrs if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) { 236170744Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 237170744Srrs } 238163953Srrs /* This is the next one we expect */ 239163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 240163953Srrs 241163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 242163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 243163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 244163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 245163953Srrs /* open the requested streams */ 246170091Srrs 247163953Srrs if (asoc->strmin != NULL) { 248163953Srrs /* Free the old ones */ 249164181Srrs struct sctp_queued_to_read *ctl; 250164181Srrs 251164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 252164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 253164181Srrs while (ctl) { 254164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 255164181Srrs sctp_free_remote_addr(ctl->whoFrom); 256164181Srrs sctp_m_freem(ctl->data); 257164181Srrs ctl->data = NULL; 258164181Srrs sctp_free_a_readq(stcb, ctl); 259164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 260164181Srrs } 261164181Srrs } 262170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 263163953Srrs } 264164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 265164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 266164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 267164181Srrs } 268163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 269170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 270163953Srrs if (asoc->strmin == NULL) { 271163953Srrs /* we didn't get memory for the streams! */ 272169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 273163953Srrs return (-1); 274163953Srrs } 275163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 276163953Srrs asoc->strmin[i].stream_no = i; 277163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 278163953Srrs /* 279163953Srrs * U-stream ranges will be set when the cookie is unpacked. 280163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 281163953Srrs * supported) when the INIT-ACK arrives. 282163953Srrs */ 283163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 284168943Srrs asoc->strmin[i].delivery_started = 0; 285163953Srrs } 286163953Srrs /* 287163953Srrs * load_address_from_init will put the addresses into the 288163953Srrs * association when the COOKIE is processed or the INIT-ACK is 289163953Srrs * processed. Both types of COOKIE's existing and new call this 290163953Srrs * routine. It will remove addresses that are no longer in the 291163953Srrs * association (for the restarting case where addresses are 292163953Srrs * removed). Up front when the INIT arrives we will discard it if it 293163953Srrs * is a restart and new addresses have been added. 294163953Srrs */ 295169655Srrs /* sa_ignore MEMLEAK */ 296163953Srrs return (0); 297163953Srrs} 298163953Srrs 299163953Srrs/* 300163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 301163953Srrs */ 302163953Srrsstatic int 303163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 304163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 305170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 306163953Srrs{ 307163953Srrs struct sctp_association *asoc; 308163953Srrs struct mbuf *op_err; 309163953Srrs int retval, abort_flag; 310163953Srrs uint32_t initack_limit; 311163953Srrs 312163953Srrs /* First verify that we have no illegal param's */ 313163953Srrs abort_flag = 0; 314163953Srrs op_err = NULL; 315163953Srrs 316163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 317163953Srrs (offset + sizeof(struct sctp_init_chunk)), 318163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 319163953Srrs if (abort_flag) { 320163953Srrs /* Send an abort and notify peer */ 321170056Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err); 322170056Srrs *abort_no_unlock = 1; 323163953Srrs return (-1); 324163953Srrs } 325163953Srrs asoc = &stcb->asoc; 326163953Srrs /* process the peer's parameters in the INIT-ACK */ 327163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 328163953Srrs if (retval < 0) { 329163953Srrs return (retval); 330163953Srrs } 331163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 332163953Srrs /* load all addresses */ 333163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 334163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 335163953Srrs NULL))) { 336163953Srrs /* Huh, we should abort */ 337169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 338169420Srrs "Load addresses from INIT causes an abort %d\n", 339169420Srrs retval); 340163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 341170181Srrs NULL, 0); 342168299Srrs *abort_no_unlock = 1; 343163953Srrs return (-1); 344163953Srrs } 345163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 346163953Srrs stcb->asoc.local_hmacs); 347163953Srrs if (op_err) { 348163953Srrs sctp_queue_op_err(stcb, op_err); 349163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 350163953Srrs op_err = NULL; 351163953Srrs } 352163953Srrs /* extract the cookie and queue it to "echo" it back... */ 353163953Srrs stcb->asoc.overall_error_count = 0; 354163953Srrs net->error_count = 0; 355163953Srrs 356163953Srrs /* 357163953Srrs * Cancel the INIT timer, We do this first before queueing the 358163953Srrs * cookie. We always cancel at the primary to assue that we are 359163953Srrs * canceling the timer started by the INIT which always goes to the 360163953Srrs * primary. 361163953Srrs */ 362163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 363165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 364163953Srrs 365165220Srrs /* calculate the RTO */ 366165220Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered); 367165220Srrs 368163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 369163953Srrs if (retval < 0) { 370163953Srrs /* 371163953Srrs * No cookie, we probably should send a op error. But in any 372163953Srrs * case if there is no cookie in the INIT-ACK, we can 373163953Srrs * abandon the peer, its broke. 374163953Srrs */ 375163953Srrs if (retval == -3) { 376163953Srrs /* We abort with an error of missing mandatory param */ 377163953Srrs op_err = 378163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 379163953Srrs if (op_err) { 380163953Srrs /* 381163953Srrs * Expand beyond to include the mandatory 382163953Srrs * param cookie 383163953Srrs */ 384163953Srrs struct sctp_inv_mandatory_param *mp; 385163953Srrs 386165647Srrs SCTP_BUF_LEN(op_err) = 387163953Srrs sizeof(struct sctp_inv_mandatory_param); 388163953Srrs mp = mtod(op_err, 389163953Srrs struct sctp_inv_mandatory_param *); 390163953Srrs /* Subtract the reserved param */ 391163953Srrs mp->length = 392163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 393163953Srrs mp->num_param = htonl(1); 394163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 395163953Srrs mp->resv = 0; 396163953Srrs } 397163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 398170181Srrs sh, op_err, 0); 399168299Srrs *abort_no_unlock = 1; 400163953Srrs } 401163953Srrs return (retval); 402163953Srrs } 403163953Srrs return (0); 404163953Srrs} 405163953Srrs 406163953Srrsstatic void 407163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 408163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 409163953Srrs{ 410163953Srrs struct sockaddr_storage store; 411163953Srrs struct sockaddr_in *sin; 412163953Srrs struct sockaddr_in6 *sin6; 413163953Srrs struct sctp_nets *r_net; 414163953Srrs struct timeval tv; 415163953Srrs 416163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 417163953Srrs /* Invalid length */ 418163953Srrs return; 419163953Srrs } 420163953Srrs sin = (struct sockaddr_in *)&store; 421163953Srrs sin6 = (struct sockaddr_in6 *)&store; 422163953Srrs 423163953Srrs memset(&store, 0, sizeof(store)); 424163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 425163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 426163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 427163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 428163953Srrs sin->sin_port = stcb->rport; 429163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 430163953Srrs sizeof(sin->sin_addr)); 431163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 432163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 433163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 434163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 435163953Srrs sin6->sin6_port = stcb->rport; 436163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 437163953Srrs sizeof(sin6->sin6_addr)); 438163953Srrs } else { 439163953Srrs return; 440163953Srrs } 441163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 442163953Srrs if (r_net == NULL) { 443169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 444163953Srrs return; 445163953Srrs } 446163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 447163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 448163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 449163953Srrs /* 450163953Srrs * If the its a HB and it's random value is correct when can 451163953Srrs * confirm the destination. 452163953Srrs */ 453163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 454167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 455167598Srrs stcb->asoc.primary_destination = r_net; 456167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 457167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 458167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 459167598Srrs if (r_net != stcb->asoc.primary_destination) { 460167598Srrs /* 461167598Srrs * first one on the list is NOT the primary 462167598Srrs * sctp_cmpaddr() is much more efficent if 463167598Srrs * the primary is the first on the list, 464167598Srrs * make it so. 465167598Srrs */ 466167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 467167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 468167598Srrs } 469167598Srrs } 470163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 471163953Srrs stcb, 0, (void *)r_net); 472163953Srrs } 473163953Srrs r_net->error_count = 0; 474163953Srrs r_net->hb_responded = 1; 475163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 476163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 477163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 478163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 479163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 480163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 481163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 482163953Srrs /* now was it the primary? if so restore */ 483163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 484169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 485163953Srrs } 486163953Srrs } 487163953Srrs /* Now lets do a RTO with this */ 488163953Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv); 489163953Srrs} 490163953Srrs 491163953Srrsstatic void 492163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 493163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 494163953Srrs{ 495169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 496163953Srrs if (stcb == NULL) 497163953Srrs return; 498163953Srrs 499163953Srrs /* stop any receive timers */ 500165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 501163953Srrs /* notify user of the abort and clean up... */ 502163953Srrs sctp_abort_notification(stcb, 0); 503163953Srrs /* free the tcb */ 504163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 505163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 506163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 507163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 508163953Srrs } 509168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 510168859Srrs sctp_print_out_track_log(stcb); 511168859Srrs#endif 512165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 513169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 514163953Srrs} 515163953Srrs 516163953Srrsstatic void 517163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 518163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 519163953Srrs{ 520163953Srrs struct sctp_association *asoc; 521163953Srrs int some_on_streamwheel; 522163953Srrs 523169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 524169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 525163953Srrs if (stcb == NULL) 526163953Srrs return; 527165220Srrs asoc = &stcb->asoc; 528165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 529165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 530163953Srrs return; 531163953Srrs } 532163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 533163953Srrs /* Shutdown NOT the expected size */ 534163953Srrs return; 535163953Srrs } else { 536163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 537163953Srrs } 538165220Srrs if (asoc->control_pdapi) { 539163953Srrs /* 540163953Srrs * With a normal shutdown we assume the end of last record. 541163953Srrs */ 542163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 543165220Srrs asoc->control_pdapi->end_added = 1; 544165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 545165220Srrs asoc->control_pdapi = NULL; 546163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 547163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 548163953Srrs } 549163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 550163953Srrs if (stcb->sctp_socket) { 551163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 552170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 553163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 554163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED; 555163953Srrs /* 556163953Srrs * notify upper layer that peer has initiated a 557163953Srrs * shutdown 558163953Srrs */ 559163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 560163953Srrs 561163953Srrs /* reset time */ 562169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 563163953Srrs } 564163953Srrs } 565163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 566163953Srrs /* 567163953Srrs * stop the shutdown timer, since we WILL move to 568163953Srrs * SHUTDOWN-ACK-SENT. 569163953Srrs */ 570165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 571163953Srrs } 572163953Srrs /* Now are we there yet? */ 573163953Srrs some_on_streamwheel = 0; 574163953Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 575163953Srrs /* Check to see if some data queued */ 576163953Srrs struct sctp_stream_out *outs; 577163953Srrs 578163953Srrs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) { 579163953Srrs if (!TAILQ_EMPTY(&outs->outqueue)) { 580163953Srrs some_on_streamwheel = 1; 581163953Srrs break; 582163953Srrs } 583163953Srrs } 584163953Srrs } 585163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 586163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 587163953Srrs some_on_streamwheel) { 588163953Srrs /* By returning we will push more data out */ 589163953Srrs return; 590163953Srrs } else { 591163953Srrs /* no outstanding data to send, so move on... */ 592163953Srrs /* send SHUTDOWN-ACK */ 593163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 594163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 595166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 596166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 597163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 598163953Srrs } 599163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT; 600163953Srrs 601163953Srrs /* start SHUTDOWN timer */ 602163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 603163953Srrs stcb, net); 604163953Srrs } 605163953Srrs} 606163953Srrs 607163953Srrsstatic void 608163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 609163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 610163953Srrs{ 611163953Srrs struct sctp_association *asoc; 612163953Srrs 613169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 614169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 615163953Srrs if (stcb == NULL) 616163953Srrs return; 617163953Srrs 618163953Srrs asoc = &stcb->asoc; 619163953Srrs /* process according to association state */ 620163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 621163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 622163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 623163953Srrs SCTP_TCB_UNLOCK(stcb); 624163953Srrs return; 625163953Srrs } 626165220Srrs if (asoc->control_pdapi) { 627163953Srrs /* 628163953Srrs * With a normal shutdown we assume the end of last record. 629163953Srrs */ 630163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 631165220Srrs asoc->control_pdapi->end_added = 1; 632165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 633165220Srrs asoc->control_pdapi = NULL; 634163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 635163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 636163953Srrs } 637163953Srrs /* are the queues empty? */ 638163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 639163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 640163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 641164205Srrs sctp_report_all_outbound(stcb, 0); 642163953Srrs } 643163953Srrs /* stop the timer */ 644165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 645163953Srrs /* send SHUTDOWN-COMPLETE */ 646163953Srrs sctp_send_shutdown_complete(stcb, net); 647163953Srrs /* notify upper layer protocol */ 648163953Srrs if (stcb->sctp_socket) { 649163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 650163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 651163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 652163953Srrs /* Set the connected flag to disconnected */ 653163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 654163953Srrs } 655163953Srrs } 656163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 657163953Srrs /* free the TCB but first save off the ep */ 658165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 659165220Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 660163953Srrs} 661163953Srrs 662163953Srrs/* 663163953Srrs * Skip past the param header and then we will find the chunk that caused the 664163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 665163953Srrs * our peer must be broken. 666163953Srrs */ 667163953Srrsstatic void 668163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 669163953Srrs struct sctp_nets *net) 670163953Srrs{ 671163953Srrs struct sctp_chunkhdr *chk; 672163953Srrs 673163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 674163953Srrs switch (chk->chunk_type) { 675163953Srrs case SCTP_ASCONF_ACK: 676163953Srrs case SCTP_ASCONF: 677163953Srrs sctp_asconf_cleanup(stcb, net); 678163953Srrs break; 679163953Srrs case SCTP_FORWARD_CUM_TSN: 680163953Srrs stcb->asoc.peer_supports_prsctp = 0; 681163953Srrs break; 682163953Srrs default: 683169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 684169420Srrs "Peer does not support chunk type %d(%x)??\n", 685169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 686163953Srrs break; 687163953Srrs } 688163953Srrs} 689163953Srrs 690163953Srrs/* 691163953Srrs * Skip past the param header and then we will find the param that caused the 692163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 693163953Srrs * these will turn of specific features. 694163953Srrs */ 695163953Srrsstatic void 696163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 697163953Srrs{ 698163953Srrs struct sctp_paramhdr *pbad; 699163953Srrs 700163953Srrs pbad = phdr + 1; 701163953Srrs switch (ntohs(pbad->param_type)) { 702163953Srrs /* pr-sctp draft */ 703163953Srrs case SCTP_PRSCTP_SUPPORTED: 704163953Srrs stcb->asoc.peer_supports_prsctp = 0; 705163953Srrs break; 706163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 707163953Srrs break; 708163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 709163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 710163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 711163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 712163953Srrs stcb->asoc.ecn_allowed = 0; 713163953Srrs break; 714163953Srrs case SCTP_ADD_IP_ADDRESS: 715163953Srrs case SCTP_DEL_IP_ADDRESS: 716163953Srrs case SCTP_SET_PRIM_ADDR: 717163953Srrs stcb->asoc.peer_supports_asconf = 0; 718163953Srrs break; 719163953Srrs case SCTP_SUCCESS_REPORT: 720163953Srrs case SCTP_ERROR_CAUSE_IND: 721169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 722169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 723169420Srrs "Turning off ASCONF to this strange peer\n"); 724163953Srrs stcb->asoc.peer_supports_asconf = 0; 725163953Srrs break; 726163953Srrs default: 727169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 728169420Srrs "Peer does not support param type %d(%x)??\n", 729169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 730163953Srrs break; 731163953Srrs } 732163953Srrs} 733163953Srrs 734163953Srrsstatic int 735163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 736163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 737163953Srrs{ 738163953Srrs int chklen; 739163953Srrs struct sctp_paramhdr *phdr; 740163953Srrs uint16_t error_type; 741163953Srrs uint16_t error_len; 742163953Srrs struct sctp_association *asoc; 743163953Srrs 744163953Srrs int adjust; 745163953Srrs 746163953Srrs /* parse through all of the errors and process */ 747163953Srrs asoc = &stcb->asoc; 748163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 749163953Srrs sizeof(struct sctp_chunkhdr)); 750163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 751163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 752163953Srrs /* Process an Error Cause */ 753163953Srrs error_type = ntohs(phdr->param_type); 754163953Srrs error_len = ntohs(phdr->param_length); 755163953Srrs if ((error_len > chklen) || (error_len == 0)) { 756163953Srrs /* invalid param length for this param */ 757169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 758169420Srrs chklen, error_len); 759163953Srrs return (0); 760163953Srrs } 761163953Srrs switch (error_type) { 762163953Srrs case SCTP_CAUSE_INVALID_STREAM: 763163953Srrs case SCTP_CAUSE_MISSING_PARAM: 764163953Srrs case SCTP_CAUSE_INVALID_PARAM: 765163953Srrs case SCTP_CAUSE_NO_USER_DATA: 766169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 767169420Srrs error_type); 768163953Srrs break; 769163953Srrs case SCTP_CAUSE_STALE_COOKIE: 770163953Srrs /* 771163953Srrs * We only act if we have echoed a cookie and are 772163953Srrs * waiting. 773163953Srrs */ 774163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 775163953Srrs int *p; 776163953Srrs 777163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 778163953Srrs /* Save the time doubled */ 779163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 780163953Srrs asoc->stale_cookie_count++; 781163953Srrs if (asoc->stale_cookie_count > 782163953Srrs asoc->max_init_times) { 783163953Srrs sctp_abort_notification(stcb, 0); 784163953Srrs /* now free the asoc */ 785165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 786163953Srrs return (-1); 787163953Srrs } 788163953Srrs /* blast back to INIT state */ 789163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 790163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 791163953Srrs 792163953Srrs sctp_stop_all_cookie_timers(stcb); 793163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 794163953Srrs } 795163953Srrs break; 796163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 797163953Srrs /* 798163953Srrs * Nothing we can do here, we don't do hostname 799163953Srrs * addresses so if the peer does not like my IPv6 800163953Srrs * (or IPv4 for that matter) it does not matter. If 801163953Srrs * they don't support that type of address, they can 802163953Srrs * NOT possibly get that packet type... i.e. with no 803163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 804163953Srrs * safely ignore this one. If we ever added support 805163953Srrs * for HOSTNAME Addresses, then we would need to do 806163953Srrs * something here. 807163953Srrs */ 808163953Srrs break; 809163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 810163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 811163953Srrs break; 812163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 813163953Srrs sctp_process_unrecog_param(stcb, phdr); 814163953Srrs break; 815163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 816163953Srrs /* 817163953Srrs * We ignore this since the timer will drive out a 818163953Srrs * new cookie anyway and there timer will drive us 819163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 820163953Srrs * here since we don't have their tag. 821163953Srrs */ 822163953Srrs break; 823163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 824163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 825163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 826163953Srrs /* 827163953Srrs * We should NOT get these here, but in a 828166086Srrs * ASCONF-ACK. 829163953Srrs */ 830169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 831169420Srrs error_type); 832163953Srrs break; 833163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 834163953Srrs /* 835163953Srrs * And what, pray tell do we do with the fact that 836163953Srrs * the peer is out of resources? Not really sure we 837166086Srrs * could do anything but abort. I suspect this 838163953Srrs * should have came WITH an abort instead of in a 839163953Srrs * OP-ERROR. 840163953Srrs */ 841163953Srrs break; 842163953Srrs default: 843169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 844169420Srrs error_type); 845163953Srrs break; 846163953Srrs } 847163953Srrs adjust = SCTP_SIZE32(error_len); 848163953Srrs chklen -= adjust; 849163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 850163953Srrs } 851163953Srrs return (0); 852163953Srrs} 853163953Srrs 854163953Srrsstatic int 855169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 856169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 857170181Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 858163953Srrs{ 859163953Srrs struct sctp_init_ack *init_ack; 860163953Srrs int *state; 861163953Srrs struct mbuf *op_err; 862163953Srrs 863169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 864169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 865169420Srrs 866163953Srrs if (stcb == NULL) { 867169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 868169420Srrs "sctp_handle_init_ack: TCB is null\n"); 869163953Srrs return (-1); 870163953Srrs } 871163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 872163953Srrs /* Invalid length */ 873163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 874163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 875170181Srrs op_err, 0); 876168299Srrs *abort_no_unlock = 1; 877163953Srrs return (-1); 878163953Srrs } 879163953Srrs init_ack = &cp->init; 880163953Srrs /* validate parameters */ 881163953Srrs if (init_ack->initiate_tag == 0) { 882163953Srrs /* protocol error... send an abort */ 883163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 884163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 885170181Srrs op_err, 0); 886168299Srrs *abort_no_unlock = 1; 887163953Srrs return (-1); 888163953Srrs } 889163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 890163953Srrs /* protocol error... send an abort */ 891163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 892163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 893170181Srrs op_err, 0); 894168299Srrs *abort_no_unlock = 1; 895163953Srrs return (-1); 896163953Srrs } 897163953Srrs if (init_ack->num_inbound_streams == 0) { 898163953Srrs /* protocol error... send an abort */ 899163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 900163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 901170181Srrs op_err, 0); 902168299Srrs *abort_no_unlock = 1; 903163953Srrs return (-1); 904163953Srrs } 905163953Srrs if (init_ack->num_outbound_streams == 0) { 906163953Srrs /* protocol error... send an abort */ 907163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 908163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 909170181Srrs op_err, 0); 910168299Srrs *abort_no_unlock = 1; 911163953Srrs return (-1); 912163953Srrs } 913163953Srrs /* process according to association state... */ 914163953Srrs state = &stcb->asoc.state; 915163953Srrs switch (*state & SCTP_STATE_MASK) { 916163953Srrs case SCTP_STATE_COOKIE_WAIT: 917163953Srrs /* this is the expected state for this chunk */ 918163953Srrs /* process the INIT-ACK parameters */ 919163953Srrs if (stcb->asoc.primary_destination->dest_state & 920163953Srrs SCTP_ADDR_UNCONFIRMED) { 921163953Srrs /* 922163953Srrs * The primary is where we sent the INIT, we can 923163953Srrs * always consider it confirmed when the INIT-ACK is 924163953Srrs * returned. Do this before we load addresses 925163953Srrs * though. 926163953Srrs */ 927163953Srrs stcb->asoc.primary_destination->dest_state &= 928163953Srrs ~SCTP_ADDR_UNCONFIRMED; 929163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 930163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 931163953Srrs } 932169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 933170181Srrs net, abort_no_unlock, vrf_id) < 0) { 934163953Srrs /* error in parsing parameters */ 935163953Srrs return (-1); 936163953Srrs } 937163953Srrs /* update our state */ 938169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 939163953Srrs if (*state & SCTP_STATE_SHUTDOWN_PENDING) { 940163953Srrs *state = SCTP_STATE_COOKIE_ECHOED | 941163953Srrs SCTP_STATE_SHUTDOWN_PENDING; 942163953Srrs } else { 943163953Srrs *state = SCTP_STATE_COOKIE_ECHOED; 944163953Srrs } 945163953Srrs 946163953Srrs /* reset the RTO calc */ 947163953Srrs stcb->asoc.overall_error_count = 0; 948169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 949163953Srrs /* 950163953Srrs * collapse the init timer back in case of a exponential 951165220Srrs * backoff 952163953Srrs */ 953163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 954163953Srrs stcb, net); 955163953Srrs /* 956163953Srrs * the send at the end of the inbound data processing will 957163953Srrs * cause the cookie to be sent 958163953Srrs */ 959163953Srrs break; 960163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 961163953Srrs /* incorrect state... discard */ 962163953Srrs break; 963163953Srrs case SCTP_STATE_COOKIE_ECHOED: 964163953Srrs /* incorrect state... discard */ 965163953Srrs break; 966163953Srrs case SCTP_STATE_OPEN: 967163953Srrs /* incorrect state... discard */ 968163953Srrs break; 969163953Srrs case SCTP_STATE_EMPTY: 970163953Srrs case SCTP_STATE_INUSE: 971163953Srrs default: 972163953Srrs /* incorrect state... discard */ 973163953Srrs return (-1); 974163953Srrs break; 975163953Srrs } 976169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 977163953Srrs return (0); 978163953Srrs} 979163953Srrs 980163953Srrs 981163953Srrs/* 982163953Srrs * handle a state cookie for an existing association m: input packet mbuf 983163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 984163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 985163953Srrs * mbuf to the cookie-echo chunk 986163953Srrs */ 987163953Srrsstatic struct sctp_tcb * 988163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 989163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 990163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 991169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 992170181Srrs uint32_t vrf_id) 993163953Srrs{ 994163953Srrs struct sctp_association *asoc; 995163953Srrs struct sctp_init_chunk *init_cp, init_buf; 996163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 997163953Srrs int chk_length; 998165220Srrs int init_offset, initack_offset, i; 999163953Srrs int retval; 1000164205Srrs int spec_flag = 0; 1001170140Srrs uint32_t how_indx; 1002163953Srrs 1003163953Srrs /* I know that the TCB is non-NULL from the caller */ 1004163953Srrs asoc = &stcb->asoc; 1005166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1006166023Srrs if (asoc->cookie_how[how_indx] == 0) 1007166023Srrs break; 1008166023Srrs } 1009166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1010166023Srrs asoc->cookie_how[how_indx] = 1; 1011166023Srrs } 1012163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1013163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1014163953Srrs struct mbuf *op_err; 1015163953Srrs struct sctp_paramhdr *ph; 1016163953Srrs 1017163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1018163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1019165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1020163953Srrs if (op_err == NULL) { 1021163953Srrs /* FOOBAR */ 1022163953Srrs return (NULL); 1023163953Srrs } 1024163953Srrs /* pre-reserve some space */ 1025165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1026165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1027165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1028163953Srrs /* Set the len */ 1029165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1030163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1031163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1032163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1033169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1034170181Srrs vrf_id); 1035166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1036166023Srrs asoc->cookie_how[how_indx] = 2; 1037163953Srrs return (NULL); 1038163953Srrs } 1039163953Srrs /* 1040163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1041163953Srrs * INIT should start after the cookie-echo header struct (chunk 1042163953Srrs * header, state cookie header struct) 1043163953Srrs */ 1044163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1045163953Srrs 1046163953Srrs init_cp = (struct sctp_init_chunk *) 1047163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1048163953Srrs (uint8_t *) & init_buf); 1049163953Srrs if (init_cp == NULL) { 1050163953Srrs /* could not pull a INIT chunk in cookie */ 1051163953Srrs return (NULL); 1052163953Srrs } 1053163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1054163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1055163953Srrs return (NULL); 1056163953Srrs } 1057163953Srrs /* 1058163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1059163953Srrs * INIT-ACK follows the INIT chunk 1060163953Srrs */ 1061163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1062163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1063163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1064163953Srrs (uint8_t *) & initack_buf); 1065163953Srrs if (initack_cp == NULL) { 1066163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1067163953Srrs return (NULL); 1068163953Srrs } 1069163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1070163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1071163953Srrs return (NULL); 1072163953Srrs } 1073163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1074163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1075163953Srrs /* 1076163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1077163953Srrs * to get into the OPEN state 1078163953Srrs */ 1079166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1080166086Srrs#ifdef INVARIANTS 1081166023Srrs panic("Case D and non-match seq?"); 1082166086Srrs#else 1083169420Srrs SCTP_PRINTF("Case D, seq non-match %x vs %x?\n", 1084169420Srrs ntohl(initack_cp->init.initial_tsn), 1085169420Srrs asoc->init_seq_number); 1086166086Srrs#endif 1087166023Srrs } 1088163953Srrs switch SCTP_GET_STATE 1089163953Srrs (asoc) { 1090163953Srrs case SCTP_STATE_COOKIE_WAIT: 1091166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1092163953Srrs /* 1093169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1094166023Srrs * correct tags... just accept it...but we must 1095166023Srrs * process the init so that we can make sure we have 1096166023Srrs * the right seq no's. 1097163953Srrs */ 1098163953Srrs /* First we must process the INIT !! */ 1099163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1100163953Srrs if (retval < 0) { 1101166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1102166023Srrs asoc->cookie_how[how_indx] = 3; 1103163953Srrs return (NULL); 1104163953Srrs } 1105163953Srrs /* we have already processed the INIT so no problem */ 1106163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1107165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1108165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1109163953Srrs /* update current state */ 1110166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1111166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1112166675Srrs else 1113166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1114163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1115166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1116163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1117163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1118163953Srrs 1119166675Srrs } else { 1120163953Srrs /* if ok, move to OPEN state */ 1121163953Srrs asoc->state = SCTP_STATE_OPEN; 1122163953Srrs } 1123166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1124165220Srrs sctp_stop_all_cookie_timers(stcb); 1125163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1126163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1127163953Srrs (inp->sctp_socket->so_qlimit == 0) 1128163953Srrs ) { 1129163953Srrs /* 1130163953Srrs * Here is where collision would go if we 1131163953Srrs * did a connect() and instead got a 1132163953Srrs * init/init-ack/cookie done before the 1133163953Srrs * init-ack came back.. 1134163953Srrs */ 1135163953Srrs stcb->sctp_ep->sctp_flags |= 1136163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1137163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1138163953Srrs } 1139163953Srrs /* notify upper layer */ 1140163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1141163953Srrs /* 1142163953Srrs * since we did not send a HB make sure we don't 1143163953Srrs * double things 1144163953Srrs */ 1145163953Srrs net->hb_responded = 1; 1146170642Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 1147170642Srrs &cookie->time_entered); 1148163953Srrs 1149163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1150163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1151163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1152163953Srrs inp, stcb, NULL); 1153163953Srrs } 1154163953Srrs break; 1155163953Srrs default: 1156163953Srrs /* 1157163953Srrs * we're in the OPEN state (or beyond), so peer must 1158163953Srrs * have simply lost the COOKIE-ACK 1159163953Srrs */ 1160163953Srrs break; 1161163953Srrs } /* end switch */ 1162165220Srrs sctp_stop_all_cookie_timers(stcb); 1163163953Srrs /* 1164163953Srrs * We ignore the return code here.. not sure if we should 1165163953Srrs * somehow abort.. but we do have an existing asoc. This 1166163953Srrs * really should not fail. 1167163953Srrs */ 1168163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1169163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1170163953Srrs initack_offset, sh, init_src)) { 1171166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1172166023Srrs asoc->cookie_how[how_indx] = 4; 1173163953Srrs return (NULL); 1174163953Srrs } 1175163953Srrs /* respond with a COOKIE-ACK */ 1176165220Srrs sctp_toss_old_cookies(stcb, asoc); 1177163953Srrs sctp_send_cookie_ack(stcb); 1178166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1179166023Srrs asoc->cookie_how[how_indx] = 5; 1180163953Srrs return (stcb); 1181169352Srrs } 1182163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1183163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1184163953Srrs cookie->tie_tag_my_vtag == 0 && 1185163953Srrs cookie->tie_tag_peer_vtag == 0) { 1186163953Srrs /* 1187163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1188163953Srrs */ 1189166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1190166023Srrs asoc->cookie_how[how_indx] = 6; 1191163953Srrs return (NULL); 1192163953Srrs } 1193163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1194163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1195163953Srrs init_cp->init.initiate_tag == 0)) { 1196163953Srrs /* 1197163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1198163953Srrs * should be ok, re-accept peer info 1199163953Srrs */ 1200166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1201166023Srrs /* 1202166023Srrs * Extension of case C. If we hit this, then the 1203166023Srrs * random number generator returned the same vtag 1204166023Srrs * when we first sent our INIT-ACK and when we later 1205166023Srrs * sent our INIT. The side with the seq numbers that 1206166023Srrs * are different will be the one that normnally 1207166023Srrs * would have hit case C. This in effect "extends" 1208166023Srrs * our vtags in this collision case to be 64 bits. 1209166023Srrs * The same collision could occur aka you get both 1210166023Srrs * vtag and seq number the same twice in a row.. but 1211166023Srrs * is much less likely. If it did happen then we 1212166023Srrs * would proceed through and bring up the assoc.. we 1213166023Srrs * may end up with the wrong stream setup however.. 1214166023Srrs * which would be bad.. but there is no way to 1215166023Srrs * tell.. until we send on a stream that does not 1216166023Srrs * exist :-) 1217166023Srrs */ 1218166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1219166023Srrs asoc->cookie_how[how_indx] = 7; 1220166023Srrs 1221166023Srrs return (NULL); 1222166023Srrs } 1223166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1224166023Srrs asoc->cookie_how[how_indx] = 8; 1225165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1226163953Srrs sctp_stop_all_cookie_timers(stcb); 1227163953Srrs /* 1228163953Srrs * since we did not send a HB make sure we don't double 1229163953Srrs * things 1230163953Srrs */ 1231163953Srrs net->hb_responded = 1; 1232163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1233163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1234163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1235163953Srrs NULL); 1236163953Srrs } 1237163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1238164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1239164205Srrs 1240164205Srrs /* Note last_cwr_tsn? where is this used? */ 1241163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1242164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1243164205Srrs /* 1244164205Srrs * Ok the peer probably discarded our data (if we 1245164205Srrs * echoed a cookie+data). So anything on the 1246164205Srrs * sent_queue should be marked for retransmit, we 1247164205Srrs * may not get something to kick us so it COULD 1248164205Srrs * still take a timeout to move these.. but it can't 1249164205Srrs * hurt to mark them. 1250164205Srrs */ 1251164205Srrs struct sctp_tmit_chunk *chk; 1252163953Srrs 1253164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1254164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1255164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1256168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1257164205Srrs spec_flag++; 1258164205Srrs } 1259164205Srrs } 1260164205Srrs 1261164205Srrs } 1262163953Srrs /* process the INIT info (peer's info) */ 1263163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1264163953Srrs if (retval < 0) { 1265166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1266166023Srrs asoc->cookie_how[how_indx] = 9; 1267163953Srrs return (NULL); 1268163953Srrs } 1269163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1270163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1271163953Srrs initack_offset, sh, init_src)) { 1272166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1273166023Srrs asoc->cookie_how[how_indx] = 10; 1274163953Srrs return (NULL); 1275163953Srrs } 1276163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1277163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1278163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1279163953Srrs 1280163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1281163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1282163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1283163953Srrs stcb->sctp_ep->sctp_flags |= 1284163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1285163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1286163953Srrs } 1287166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1288166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1289166675Srrs else 1290166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1291166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1292166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1293166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1294166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1295166675Srrs } else { 1296166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1297163953Srrs } 1298163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1299166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1300163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1301163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1302163953Srrs 1303163953Srrs } else { 1304163953Srrs asoc->state = SCTP_STATE_OPEN; 1305163953Srrs } 1306163953Srrs sctp_stop_all_cookie_timers(stcb); 1307165220Srrs sctp_toss_old_cookies(stcb, asoc); 1308163953Srrs sctp_send_cookie_ack(stcb); 1309164205Srrs if (spec_flag) { 1310164205Srrs /* 1311164205Srrs * only if we have retrans set do we do this. What 1312164205Srrs * this call does is get only the COOKIE-ACK out and 1313164205Srrs * then when we return the normal call to 1314164205Srrs * sctp_chunk_output will get the retrans out behind 1315164205Srrs * this. 1316164205Srrs */ 1317164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1318164205Srrs } 1319166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1320166023Srrs asoc->cookie_how[how_indx] = 11; 1321166023Srrs 1322163953Srrs return (stcb); 1323163953Srrs } 1324163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1325163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1326163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1327163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1328163953Srrs cookie->tie_tag_peer_vtag != 0) { 1329163953Srrs struct sctpasochead *head; 1330163953Srrs 1331163953Srrs /* 1332163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1333163953Srrs */ 1334165220Srrs /* temp code */ 1335166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1336166023Srrs asoc->cookie_how[how_indx] = 12; 1337165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1338165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1339165647Srrs 1340163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1341163953Srrs /* notify upper layer */ 1342163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1343165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1344166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1345166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1346166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1347166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1348166675Srrs } 1349166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1350166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1351166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1352166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1353166675Srrs } 1354165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1355165647Srrs asoc->state = SCTP_STATE_OPEN | 1356165647Srrs SCTP_STATE_SHUTDOWN_PENDING; 1357165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1358165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1359165647Srrs 1360165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1361165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1362165647Srrs asoc->state = SCTP_STATE_OPEN; 1363165647Srrs } 1364165647Srrs asoc->pre_open_streams = 1365165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1366165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1367165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1368165647Srrs 1369165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1370165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1371165647Srrs 1372165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1373165647Srrs 1374165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1375170138Srrs if (asoc->mapping_array) { 1376165647Srrs memset(asoc->mapping_array, 0, 1377165647Srrs asoc->mapping_array_size); 1378170138Srrs } 1379165220Srrs SCTP_TCB_UNLOCK(stcb); 1380165220Srrs SCTP_INP_INFO_WLOCK(); 1381165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1382165220Srrs SCTP_TCB_LOCK(stcb); 1383165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1384163953Srrs /* send up all the data */ 1385164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1386165220Srrs 1387164205Srrs sctp_report_all_outbound(stcb, 1); 1388165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1389165220Srrs stcb->asoc.strmout[i].stream_no = i; 1390165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1391165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1392165220Srrs } 1393163953Srrs /* process the INIT-ACK info (my info) */ 1394163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1395163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1396163953Srrs 1397163953Srrs /* pull from vtag hash */ 1398163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1399163953Srrs /* re-insert to new vtag position */ 1400163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1401163953Srrs sctppcbinfo.hashasocmark)]; 1402163953Srrs /* 1403163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1404163953Srrs * system 1405163953Srrs */ 1406163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1407163953Srrs 1408163953Srrs /* Is this the first restart? */ 1409163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1410163953Srrs /* Ok add it to assoc_id vtag hash */ 1411163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1412163953Srrs sctppcbinfo.hashrestartmark)]; 1413163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1414163953Srrs stcb->asoc.in_restart_hash = 1; 1415163953Srrs } 1416163953Srrs /* process the INIT info (peer's info) */ 1417164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1418165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1419165220Srrs SCTP_INP_INFO_WUNLOCK(); 1420165220Srrs 1421163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1422163953Srrs if (retval < 0) { 1423166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1424166023Srrs asoc->cookie_how[how_indx] = 13; 1425166023Srrs 1426163953Srrs return (NULL); 1427163953Srrs } 1428163953Srrs /* 1429163953Srrs * since we did not send a HB make sure we don't double 1430163953Srrs * things 1431163953Srrs */ 1432163953Srrs net->hb_responded = 1; 1433163953Srrs 1434163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1435163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1436163953Srrs initack_offset, sh, init_src)) { 1437166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1438166023Srrs asoc->cookie_how[how_indx] = 14; 1439166023Srrs 1440163953Srrs return (NULL); 1441163953Srrs } 1442163953Srrs /* respond with a COOKIE-ACK */ 1443163953Srrs sctp_stop_all_cookie_timers(stcb); 1444165220Srrs sctp_toss_old_cookies(stcb, asoc); 1445163953Srrs sctp_send_cookie_ack(stcb); 1446166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1447166023Srrs asoc->cookie_how[how_indx] = 15; 1448163953Srrs 1449163953Srrs return (stcb); 1450163953Srrs } 1451166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1452166023Srrs asoc->cookie_how[how_indx] = 16; 1453163953Srrs /* all other cases... */ 1454163953Srrs return (NULL); 1455163953Srrs} 1456163953Srrs 1457166086Srrs 1458163953Srrs/* 1459163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1460163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1461163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1462163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1463163953Srrs * was from returns a new TCB 1464163953Srrs */ 1465163953Srrsstatic struct sctp_tcb * 1466163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1467163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1468163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1469163953Srrs struct sockaddr *init_src, int *notification, 1470169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1471170181Srrs uint32_t vrf_id) 1472163953Srrs{ 1473163953Srrs struct sctp_tcb *stcb; 1474163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1475163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1476163953Srrs struct sockaddr_storage sa_store; 1477163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1478163953Srrs struct sockaddr_in *sin; 1479163953Srrs struct sockaddr_in6 *sin6; 1480163953Srrs struct sctp_association *asoc; 1481163953Srrs int chk_length; 1482163953Srrs int init_offset, initack_offset, initack_limit; 1483163953Srrs int retval; 1484163953Srrs int error = 0; 1485163953Srrs uint32_t old_tag; 1486166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1487163953Srrs 1488163953Srrs /* 1489163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1490163953Srrs * INIT should start after the cookie-echo header struct (chunk 1491163953Srrs * header, state cookie header struct) 1492163953Srrs */ 1493163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1494163953Srrs init_cp = (struct sctp_init_chunk *) 1495163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1496163953Srrs (uint8_t *) & init_buf); 1497163953Srrs if (init_cp == NULL) { 1498163953Srrs /* could not pull a INIT chunk in cookie */ 1499169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1500169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1501163953Srrs return (NULL); 1502163953Srrs } 1503163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1504163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1505169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1506163953Srrs return (NULL); 1507163953Srrs } 1508163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1509163953Srrs /* 1510163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1511163953Srrs * INIT-ACK follows the INIT chunk 1512163953Srrs */ 1513163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1514163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1515163953Srrs (uint8_t *) & initack_buf); 1516163953Srrs if (initack_cp == NULL) { 1517163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1518169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1519163953Srrs return (NULL); 1520163953Srrs } 1521163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1522163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1523163953Srrs return (NULL); 1524163953Srrs } 1525163953Srrs /* 1526163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1527163953Srrs * "initack_limit" value. This is because the chk_length field 1528163953Srrs * includes the length of the cookie, but the cookie is omitted when 1529163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1530163953Srrs */ 1531163953Srrs initack_limit = offset + cookie_len; 1532163953Srrs 1533163953Srrs /* 1534163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1535163953Srrs * and popluate 1536163953Srrs */ 1537163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1538168299Srrs ntohl(initack_cp->init.initiate_tag), vrf_id); 1539163953Srrs if (stcb == NULL) { 1540163953Srrs struct mbuf *op_err; 1541163953Srrs 1542163953Srrs /* memory problem? */ 1543169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1544169420Srrs "process_cookie_new: no room for another TCB!\n"); 1545163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1546170140Srrs 1547163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1548170181Srrs sh, op_err, vrf_id); 1549163953Srrs return (NULL); 1550163953Srrs } 1551163953Srrs /* get the correct sctp_nets */ 1552169420Srrs if (netp) 1553169420Srrs *netp = sctp_findnet(stcb, init_src); 1554169420Srrs 1555163953Srrs asoc = &stcb->asoc; 1556163953Srrs /* get scope variables out of cookie */ 1557163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1558163953Srrs asoc->site_scope = cookie->site_scope; 1559163953Srrs asoc->local_scope = cookie->local_scope; 1560163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1561163953Srrs 1562163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1563163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1564163953Srrs struct mbuf *op_err; 1565163953Srrs 1566163953Srrs /* 1567163953Srrs * Houston we have a problem. The EP changed while the 1568163953Srrs * cookie was in flight. Only recourse is to abort the 1569163953Srrs * association. 1570163953Srrs */ 1571170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1572163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1573163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1574170181Srrs sh, op_err, vrf_id); 1575170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1576163953Srrs return (NULL); 1577163953Srrs } 1578163953Srrs /* process the INIT-ACK info (my info) */ 1579163953Srrs old_tag = asoc->my_vtag; 1580163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1581163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1582163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1583163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1584163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1585163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1586163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1587163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1588163953Srrs 1589163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1590163953Srrs 1591163953Srrs /* process the INIT info (peer's info) */ 1592169420Srrs if (netp) 1593169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1594169420Srrs else 1595169420Srrs retval = 0; 1596163953Srrs if (retval < 0) { 1597170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1598165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1599170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1600163953Srrs return (NULL); 1601163953Srrs } 1602163953Srrs /* load all addresses */ 1603163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1604163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1605163953Srrs init_src)) { 1606170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1607165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1608170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1609163953Srrs return (NULL); 1610163953Srrs } 1611163953Srrs /* 1612163953Srrs * verify any preceding AUTH chunk that was skipped 1613163953Srrs */ 1614163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1615163953Srrs sctp_auth_get_cookie_params(stcb, m, 1616163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1617163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1618163953Srrs if (auth_skipped) { 1619163953Srrs struct sctp_auth_chunk *auth; 1620163953Srrs 1621163953Srrs auth = (struct sctp_auth_chunk *) 1622166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1623169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1624163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1625169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1626169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1627165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1628163953Srrs return (NULL); 1629163953Srrs } else { 1630163953Srrs /* remaining chunks checked... good to go */ 1631163953Srrs stcb->asoc.authenticated = 1; 1632163953Srrs } 1633163953Srrs } 1634163953Srrs /* update current state */ 1635169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1636163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1637163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1638163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1639163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1640163953Srrs } else { 1641163953Srrs asoc->state = SCTP_STATE_OPEN; 1642163953Srrs } 1643165220Srrs sctp_stop_all_cookie_timers(stcb); 1644163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1645163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1646163953Srrs 1647163953Srrs /* 1648163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1649163953Srrs * addresses that need to get added to the peer (eg. addresses 1650163953Srrs * changed while cookie echo in flight). This needs to be done 1651163953Srrs * after we go to the OPEN state to do the correct asconf 1652163953Srrs * processing. else, make sure we have the correct addresses in our 1653163953Srrs * lists 1654163953Srrs */ 1655163953Srrs 1656163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1657163953Srrs /* pull in local_address (our "from" address) */ 1658163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1659163953Srrs /* source addr is IPv4 */ 1660163953Srrs sin = (struct sockaddr_in *)initack_src; 1661163953Srrs memset(sin, 0, sizeof(*sin)); 1662163953Srrs sin->sin_family = AF_INET; 1663163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1664163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1665163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1666163953Srrs /* source addr is IPv6 */ 1667163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1668163953Srrs memset(sin6, 0, sizeof(*sin6)); 1669163953Srrs sin6->sin6_family = AF_INET6; 1670163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1671163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1672163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1673163953Srrs sizeof(sin6->sin6_addr)); 1674163953Srrs } else { 1675170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1676165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1677170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1678163953Srrs return (NULL); 1679163953Srrs } 1680163953Srrs 1681163953Srrs sctp_check_address_list(stcb, m, 1682163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1683163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1684163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1685163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1686163953Srrs 1687163953Srrs 1688163953Srrs /* set up to notify upper layer */ 1689163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1690163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1691163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1692163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1693163953Srrs /* 1694163953Srrs * This is an endpoint that called connect() how it got a 1695163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1696163953Srrs * the INIT was sent, but before it got there.. a complete 1697163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1698163953Srrs * should have went to the other code.. not here.. oh well.. 1699163953Srrs * a bit of protection is worth having.. 1700163953Srrs */ 1701163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1702163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1703163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1704163953Srrs (inp->sctp_socket->so_qlimit)) { 1705163953Srrs /* 1706163953Srrs * We don't want to do anything with this one. Since it is 1707163953Srrs * the listening guy. The timer will get started for 1708163953Srrs * accepted connections in the caller. 1709163953Srrs */ 1710163953Srrs ; 1711163953Srrs } 1712163953Srrs /* since we did not send a HB make sure we don't double things */ 1713169420Srrs if ((netp) && (*netp)) 1714169420Srrs (*netp)->hb_responded = 1; 1715163953Srrs 1716163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1717163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1718163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1719163953Srrs } 1720163953Srrs /* respond with a COOKIE-ACK */ 1721165220Srrs /* calculate the RTT */ 1722170642Srrs if ((netp) && (*netp)) { 1723169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1724169420Srrs &cookie->time_entered); 1725170642Srrs } 1726163953Srrs sctp_send_cookie_ack(stcb); 1727163953Srrs return (stcb); 1728163953Srrs} 1729163953Srrs 1730163953Srrs 1731163953Srrs/* 1732163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1733163953Srrs * existing (non-NULL) TCB 1734163953Srrs */ 1735163953Srrsstatic struct mbuf * 1736163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1737163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1738163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1739169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1740170181Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id) 1741163953Srrs{ 1742163953Srrs struct sctp_state_cookie *cookie; 1743163953Srrs struct sockaddr_in6 sin6; 1744163953Srrs struct sockaddr_in sin; 1745163953Srrs struct sctp_tcb *l_stcb = *stcb; 1746163953Srrs struct sctp_inpcb *l_inp; 1747163953Srrs struct sockaddr *to; 1748163953Srrs sctp_assoc_t sac_restart_id; 1749163953Srrs struct sctp_pcb *ep; 1750163953Srrs struct mbuf *m_sig; 1751163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1752163953Srrs uint8_t *sig; 1753163953Srrs uint8_t cookie_ok = 0; 1754163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1755163953Srrs unsigned int cookie_len; 1756163953Srrs struct timeval now; 1757163953Srrs struct timeval time_expires; 1758163953Srrs struct sockaddr_storage dest_store; 1759163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1760163953Srrs struct ip *iph; 1761163953Srrs int notification = 0; 1762163953Srrs struct sctp_nets *netl; 1763163953Srrs int had_a_existing_tcb = 0; 1764163953Srrs 1765169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1766169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 1767163953Srrs 1768163953Srrs if (inp_p == NULL) { 1769163953Srrs return (NULL); 1770163953Srrs } 1771163953Srrs /* First get the destination address setup too. */ 1772163953Srrs iph = mtod(m, struct ip *); 1773163953Srrs if (iph->ip_v == IPVERSION) { 1774163953Srrs /* its IPv4 */ 1775170140Srrs struct sockaddr_in *lsin; 1776163953Srrs 1777170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 1778170140Srrs memset(lsin, 0, sizeof(*lsin)); 1779170140Srrs lsin->sin_family = AF_INET; 1780170140Srrs lsin->sin_len = sizeof(*lsin); 1781170140Srrs lsin->sin_port = sh->dest_port; 1782170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 1783165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1784163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1785163953Srrs /* its IPv6 */ 1786163953Srrs struct ip6_hdr *ip6; 1787170140Srrs struct sockaddr_in6 *lsin6; 1788163953Srrs 1789170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 1790170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 1791170140Srrs lsin6->sin6_family = AF_INET6; 1792170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 1793163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1794170140Srrs lsin6->sin6_port = sh->dest_port; 1795170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 1796166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1797163953Srrs } else { 1798163953Srrs return (NULL); 1799163953Srrs } 1800163953Srrs 1801163953Srrs cookie = &cp->cookie; 1802163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1803163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1804163953Srrs 1805163953Srrs if ((cookie->peerport != sh->src_port) && 1806163953Srrs (cookie->myport != sh->dest_port) && 1807163953Srrs (cookie->my_vtag != sh->v_tag)) { 1808163953Srrs /* 1809163953Srrs * invalid ports or bad tag. Note that we always leave the 1810163953Srrs * v_tag in the header in network order and when we stored 1811163953Srrs * it in the my_vtag slot we also left it in network order. 1812169352Srrs * This maintains the match even though it may be in the 1813163953Srrs * opposite byte order of the machine :-> 1814163953Srrs */ 1815163953Srrs return (NULL); 1816163953Srrs } 1817163953Srrs if (cookie_len > size_of_pkt || 1818163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1819163953Srrs sizeof(struct sctp_init_chunk) + 1820163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1821163953Srrs /* cookie too long! or too small */ 1822163953Srrs return (NULL); 1823163953Srrs } 1824163953Srrs /* 1825163953Srrs * split off the signature into its own mbuf (since it should not be 1826163953Srrs * calculated in the sctp_hmac_m() call). 1827163953Srrs */ 1828163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1829163953Srrs if (sig_offset > size_of_pkt) { 1830163953Srrs /* packet not correct size! */ 1831163953Srrs /* XXX this may already be accounted for earlier... */ 1832163953Srrs return (NULL); 1833163953Srrs } 1834163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1835163953Srrs if (m_sig == NULL) { 1836163953Srrs /* out of memory or ?? */ 1837163953Srrs return (NULL); 1838163953Srrs } 1839163953Srrs /* 1840163953Srrs * compute the signature/digest for the cookie 1841163953Srrs */ 1842163953Srrs ep = &(*inp_p)->sctp_ep; 1843163953Srrs l_inp = *inp_p; 1844163953Srrs if (l_stcb) { 1845163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1846163953Srrs } 1847163953Srrs SCTP_INP_RLOCK(l_inp); 1848163953Srrs if (l_stcb) { 1849163953Srrs SCTP_TCB_LOCK(l_stcb); 1850163953Srrs } 1851163953Srrs /* which cookie is it? */ 1852163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1853163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1854163953Srrs /* it's the old cookie */ 1855169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1856163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1857170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1858163953Srrs } else { 1859163953Srrs /* it's the current cookie */ 1860169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1861163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1862170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1863163953Srrs } 1864163953Srrs /* get the signature */ 1865163953Srrs SCTP_INP_RUNLOCK(l_inp); 1866163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1867163953Srrs if (sig == NULL) { 1868163953Srrs /* couldn't find signature */ 1869164085Srrs sctp_m_freem(m_sig); 1870163953Srrs return (NULL); 1871163953Srrs } 1872163953Srrs /* compare the received digest with the computed digest */ 1873163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1874163953Srrs /* try the old cookie? */ 1875163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1876163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1877163953Srrs /* compute digest with old */ 1878169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1879163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1880170462Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 1881163953Srrs /* compare */ 1882163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1883163953Srrs cookie_ok = 1; 1884163953Srrs } 1885163953Srrs } else { 1886163953Srrs cookie_ok = 1; 1887163953Srrs } 1888163953Srrs 1889163953Srrs /* 1890163953Srrs * Now before we continue we must reconstruct our mbuf so that 1891163953Srrs * normal processing of any other chunks will work. 1892163953Srrs */ 1893163953Srrs { 1894163953Srrs struct mbuf *m_at; 1895163953Srrs 1896163953Srrs m_at = m; 1897165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1898165647Srrs m_at = SCTP_BUF_NEXT(m_at); 1899163953Srrs } 1900165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 1901163953Srrs } 1902163953Srrs 1903163953Srrs if (cookie_ok == 0) { 1904169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 1905169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1906169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 1907169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 1908163953Srrs return (NULL); 1909163953Srrs } 1910163953Srrs /* 1911163953Srrs * check the cookie timestamps to be sure it's not stale 1912163953Srrs */ 1913169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 1914163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 1915169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 1916163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 1917163953Srrs if (timevalcmp(&now, &time_expires, >)) { 1918163953Srrs /* cookie is stale! */ 1919163953Srrs struct mbuf *op_err; 1920163953Srrs struct sctp_stale_cookie_msg *scm; 1921163953Srrs uint32_t tim; 1922163953Srrs 1923163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 1924165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1925163953Srrs if (op_err == NULL) { 1926163953Srrs /* FOOBAR */ 1927163953Srrs return (NULL); 1928163953Srrs } 1929163953Srrs /* pre-reserve some space */ 1930165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1931165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1932165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1933163953Srrs 1934163953Srrs /* Set the len */ 1935165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 1936163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 1937163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 1938163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 1939163953Srrs (sizeof(uint32_t)))); 1940163953Srrs /* seconds to usec */ 1941163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 1942163953Srrs /* add in usec */ 1943163953Srrs if (tim == 0) 1944163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 1945163953Srrs scm->time_usec = htonl(tim); 1946169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1947170181Srrs vrf_id); 1948163953Srrs return (NULL); 1949163953Srrs } 1950163953Srrs /* 1951163953Srrs * Now we must see with the lookup address if we have an existing 1952163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 1953163953Srrs * and a INIT collided with us and somewhere the peer sent the 1954163953Srrs * cookie on another address besides the single address our assoc 1955163953Srrs * had for him. In this case we will have one of the tie-tags set at 1956163953Srrs * least AND the address field in the cookie can be used to look it 1957163953Srrs * up. 1958163953Srrs */ 1959163953Srrs to = NULL; 1960163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 1961163953Srrs memset(&sin6, 0, sizeof(sin6)); 1962163953Srrs sin6.sin6_family = AF_INET6; 1963163953Srrs sin6.sin6_len = sizeof(sin6); 1964163953Srrs sin6.sin6_port = sh->src_port; 1965163953Srrs sin6.sin6_scope_id = cookie->scope_id; 1966163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 1967163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 1968163953Srrs to = (struct sockaddr *)&sin6; 1969163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 1970163953Srrs memset(&sin, 0, sizeof(sin)); 1971163953Srrs sin.sin_family = AF_INET; 1972163953Srrs sin.sin_len = sizeof(sin); 1973163953Srrs sin.sin_port = sh->src_port; 1974163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 1975163953Srrs to = (struct sockaddr *)&sin; 1976169420Srrs } else { 1977169420Srrs /* This should not happen */ 1978169420Srrs return (NULL); 1979163953Srrs } 1980163953Srrs if ((*stcb == NULL) && to) { 1981163953Srrs /* Yep, lets check */ 1982163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 1983163953Srrs if (*stcb == NULL) { 1984163953Srrs /* 1985163953Srrs * We should have only got back the same inp. If we 1986163953Srrs * got back a different ep we have a problem. The 1987163953Srrs * original findep got back l_inp and now 1988163953Srrs */ 1989163953Srrs if (l_inp != *inp_p) { 1990169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 1991163953Srrs } 1992165220Srrs } else { 1993165220Srrs if (*locked_tcb == NULL) { 1994165220Srrs /* 1995165220Srrs * In this case we found the assoc only 1996165220Srrs * after we locked the create lock. This 1997165220Srrs * means we are in a colliding case and we 1998165220Srrs * must make sure that we unlock the tcb if 1999165220Srrs * its one of the cases where we throw away 2000165220Srrs * the incoming packets. 2001165220Srrs */ 2002165220Srrs *locked_tcb = *stcb; 2003165220Srrs 2004165220Srrs /* 2005165220Srrs * We must also increment the inp ref count 2006165220Srrs * since the ref_count flags was set when we 2007165220Srrs * did not find the TCB, now we found it 2008165220Srrs * which reduces the refcount.. we must 2009165220Srrs * raise it back out to balance it all :-) 2010165220Srrs */ 2011165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2012165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2013169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2014165220Srrs (*stcb)->sctp_ep, l_inp); 2015165220Srrs } 2016165220Srrs } 2017163953Srrs } 2018163953Srrs } 2019169420Srrs if (to == NULL) 2020169420Srrs return (NULL); 2021169420Srrs 2022163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2023163953Srrs if (*stcb == NULL) { 2024163953Srrs /* this is the "normal" case... get a new TCB */ 2025163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2026163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2027170181Srrs auth_skipped, auth_offset, auth_len, vrf_id); 2028163953Srrs } else { 2029163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2030163953Srrs had_a_existing_tcb = 1; 2031163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2032170181Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, 2033170181Srrs ¬ification, &sac_restart_id, vrf_id); 2034163953Srrs } 2035163953Srrs 2036163953Srrs if (*stcb == NULL) { 2037163953Srrs /* still no TCB... must be bad cookie-echo */ 2038163953Srrs return (NULL); 2039163953Srrs } 2040163953Srrs /* 2041163953Srrs * Ok, we built an association so confirm the address we sent the 2042163953Srrs * INIT-ACK to. 2043163953Srrs */ 2044163953Srrs netl = sctp_findnet(*stcb, to); 2045163953Srrs /* 2046163953Srrs * This code should in theory NOT run but 2047163953Srrs */ 2048163953Srrs if (netl == NULL) { 2049163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2050163953Srrs int ret; 2051163953Srrs 2052165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2053165220Srrs SCTP_IN_COOKIE_PROC); 2054163953Srrs netl = sctp_findnet(*stcb, to); 2055163953Srrs } 2056163953Srrs if (netl) { 2057163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2058163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2059169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2060163953Srrs netl); 2061163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2062163953Srrs (*stcb), 0, (void *)netl); 2063163953Srrs } 2064163953Srrs } 2065163953Srrs if (*stcb) { 2066163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2067163953Srrs *stcb, NULL); 2068163953Srrs } 2069163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2070163953Srrs if (!had_a_existing_tcb || 2071163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2072163953Srrs /* 2073163953Srrs * If we have a NEW cookie or the connect never 2074163953Srrs * reached the connected state during collision we 2075163953Srrs * must do the TCP accept thing. 2076163953Srrs */ 2077163953Srrs struct socket *so, *oso; 2078163953Srrs struct sctp_inpcb *inp; 2079163953Srrs 2080163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2081163953Srrs /* 2082163953Srrs * For a restart we will keep the same 2083163953Srrs * socket, no need to do anything. I THINK!! 2084163953Srrs */ 2085163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2086163953Srrs return (m); 2087163953Srrs } 2088163953Srrs oso = (*inp_p)->sctp_socket; 2089163953Srrs /* 2090163953Srrs * We do this to keep the sockets side happy durin 2091163953Srrs * the sonewcon ONLY. 2092163953Srrs */ 2093163953Srrs NET_LOCK_GIANT(); 2094163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2095166086Srrs so = sonewconn(oso, 0 2096163953Srrs ); 2097163953Srrs NET_UNLOCK_GIANT(); 2098163953Srrs SCTP_INP_WLOCK((*stcb)->sctp_ep); 2099163953Srrs SCTP_TCB_LOCK((*stcb)); 2100163953Srrs SCTP_INP_WUNLOCK((*stcb)->sctp_ep); 2101163953Srrs if (so == NULL) { 2102163953Srrs struct mbuf *op_err; 2103163953Srrs 2104163953Srrs /* Too many sockets */ 2105169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2106163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2107163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2108170181Srrs sh, op_err, vrf_id); 2109165220Srrs sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2110163953Srrs return (NULL); 2111163953Srrs } 2112163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2113166086Srrs SCTP_INP_INCR_REF(inp); 2114166086Srrs /* 2115166086Srrs * We add the unbound flag here so that if we get an 2116166086Srrs * soabort() before we get the move_pcb done, we 2117166086Srrs * will properly cleanup. 2118166086Srrs */ 2119163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2120163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2121163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2122166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2123163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2124163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2125163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2126163953Srrs inp->sctp_socket = so; 2127163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2128163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2129163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2130163953Srrs inp->inp_starting_point_for_iterator = NULL; 2131163953Srrs /* 2132163953Srrs * copy in the authentication parameters from the 2133163953Srrs * original endpoint 2134163953Srrs */ 2135163953Srrs if (inp->sctp_ep.local_hmacs) 2136163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2137163953Srrs inp->sctp_ep.local_hmacs = 2138163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2139163953Srrs if (inp->sctp_ep.local_auth_chunks) 2140163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2141163953Srrs inp->sctp_ep.local_auth_chunks = 2142163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2143163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2144163953Srrs &inp->sctp_ep.shared_keys); 2145163953Srrs 2146163953Srrs /* 2147163953Srrs * Now we must move it from one hash table to 2148163953Srrs * another and get the tcb in the right place. 2149163953Srrs */ 2150163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2151170056Srrs 2152170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2153170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2154170056Srrs 2155169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2156170056Srrs SCTP_TCB_LOCK((*stcb)); 2157170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2158163953Srrs 2159170056Srrs 2160166086Srrs /* 2161166086Srrs * now we must check to see if we were aborted while 2162166086Srrs * the move was going on and the lock/unlock 2163166086Srrs * happened. 2164166086Srrs */ 2165166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2166166086Srrs /* 2167166086Srrs * yep it was, we leave the assoc attached 2168166086Srrs * to the socket since the sctp_inpcb_free() 2169166086Srrs * call will send an abort for us. 2170166086Srrs */ 2171166086Srrs SCTP_INP_DECR_REF(inp); 2172166086Srrs return (NULL); 2173166086Srrs } 2174166086Srrs SCTP_INP_DECR_REF(inp); 2175163953Srrs /* Switch over to the new guy */ 2176163953Srrs *inp_p = inp; 2177166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2178163953Srrs 2179166086Srrs /* 2180166086Srrs * Pull it from the incomplete queue and wake the 2181166086Srrs * guy 2182166086Srrs */ 2183166086Srrs soisconnected(so); 2184163953Srrs return (m); 2185163953Srrs } 2186163953Srrs } 2187163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2188163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2189163953Srrs } 2190163953Srrs return (m); 2191163953Srrs} 2192163953Srrs 2193163953Srrsstatic void 2194163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2195163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2196163953Srrs{ 2197163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2198163953Srrs struct sctp_association *asoc; 2199163953Srrs 2200169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2201169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2202163953Srrs if (stcb == NULL) 2203163953Srrs return; 2204163953Srrs 2205163953Srrs asoc = &stcb->asoc; 2206163953Srrs 2207163953Srrs sctp_stop_all_cookie_timers(stcb); 2208163953Srrs /* process according to association state */ 2209163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2210163953Srrs /* state change only needed when I am in right state */ 2211169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2212163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2213163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 2214163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2215163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2216163953Srrs 2217163953Srrs } else { 2218163953Srrs asoc->state = SCTP_STATE_OPEN; 2219163953Srrs } 2220163953Srrs /* update RTO */ 2221163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2222163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2223163953Srrs if (asoc->overall_error_count == 0) { 2224163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2225163953Srrs &asoc->time_entered); 2226163953Srrs } 2227169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2228163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2229163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2230163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2231163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2232163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2233163953Srrs } 2234163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2235163953Srrs stcb, net); 2236163953Srrs /* 2237163953Srrs * since we did not send a HB make sure we don't double 2238163953Srrs * things 2239163953Srrs */ 2240163953Srrs net->hb_responded = 1; 2241163953Srrs 2242163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2243163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2244163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2245163953Srrs stcb->sctp_ep, stcb, NULL); 2246163953Srrs } 2247163953Srrs /* 2248163953Srrs * set ASCONF timer if ASCONFs are pending and allowed (eg. 2249163953Srrs * addresses changed when init/cookie echo in flight) 2250163953Srrs */ 2251163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2252163953Srrs (stcb->asoc.peer_supports_asconf) && 2253163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2254163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2255163953Srrs stcb->sctp_ep, stcb, 2256163953Srrs stcb->asoc.primary_destination); 2257163953Srrs } 2258163953Srrs } 2259163953Srrs /* Toss the cookie if I can */ 2260163953Srrs sctp_toss_old_cookies(stcb, asoc); 2261163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2262163953Srrs /* Restart the timer if we have pending data */ 2263163953Srrs struct sctp_tmit_chunk *chk; 2264163953Srrs 2265163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2266163953Srrs if (chk) { 2267163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2268163953Srrs stcb, chk->whoTo); 2269163953Srrs } 2270163953Srrs } 2271163953Srrs} 2272163953Srrs 2273163953Srrsstatic void 2274163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2275163953Srrs struct sctp_tcb *stcb) 2276163953Srrs{ 2277163953Srrs struct sctp_nets *net; 2278163953Srrs struct sctp_tmit_chunk *lchk; 2279163953Srrs uint32_t tsn; 2280163953Srrs 2281163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2282163953Srrs return; 2283163953Srrs } 2284163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2285163953Srrs tsn = ntohl(cp->tsn); 2286163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2287163953Srrs /* Also we make sure we disable the nonce_wait */ 2288163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2289163953Srrs if (lchk == NULL) { 2290163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2291163953Srrs } else { 2292163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2293163953Srrs } 2294163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2295163953Srrs stcb->asoc.nonce_sum_check = 0; 2296163953Srrs 2297163953Srrs /* Find where it was sent, if possible */ 2298163953Srrs net = NULL; 2299163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2300163953Srrs while (lchk) { 2301163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2302163953Srrs net = lchk->whoTo; 2303163953Srrs break; 2304163953Srrs } 2305163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2306163953Srrs break; 2307163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2308163953Srrs } 2309163953Srrs if (net == NULL) 2310163953Srrs /* default is we use the primary */ 2311163953Srrs net = stcb->asoc.primary_destination; 2312163953Srrs 2313163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2314163953Srrs int old_cwnd; 2315163953Srrs 2316163953Srrs old_cwnd = net->cwnd; 2317163953Srrs SCTP_STAT_INCR(sctps_ecnereducedcwnd); 2318163953Srrs net->ssthresh = net->cwnd / 2; 2319163953Srrs if (net->ssthresh < net->mtu) { 2320163953Srrs net->ssthresh = net->mtu; 2321163953Srrs /* here back off the timer as well, to slow us down */ 2322170428Srrs net->RTO <<= 1; 2323163953Srrs } 2324163953Srrs net->cwnd = net->ssthresh; 2325170744Srrs if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) { 2326170744Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT); 2327170744Srrs } 2328163953Srrs /* 2329163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2330163953Srrs * the next sending seq i.e. the resync_tsn. 2331163953Srrs */ 2332163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2333163953Srrs } 2334163953Srrs /* 2335163953Srrs * We always send a CWR this way if our previous one was lost our 2336163953Srrs * peer will get an update, or if it is not time again to reduce we 2337163953Srrs * still get the cwr to the peer. 2338163953Srrs */ 2339163953Srrs sctp_send_cwr(stcb, net, tsn); 2340163953Srrs} 2341163953Srrs 2342163953Srrsstatic void 2343163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2344163953Srrs{ 2345163953Srrs /* 2346163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2347163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2348163953Srrs * If so remove it. 2349163953Srrs */ 2350163953Srrs struct sctp_tmit_chunk *chk; 2351163953Srrs struct sctp_ecne_chunk *ecne; 2352163953Srrs 2353163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2354163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2355163953Srrs continue; 2356163953Srrs } 2357163953Srrs /* 2358163953Srrs * Look for and remove if it is the right TSN. Since there 2359163953Srrs * is only ONE ECNE on the control queue at any one time we 2360163953Srrs * don't need to worry about more than one! 2361163953Srrs */ 2362163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2363163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2364163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2365163953Srrs /* this covers this ECNE, we can remove it */ 2366163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2367163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2368163953Srrs sctp_next); 2369163953Srrs if (chk->data) { 2370163953Srrs sctp_m_freem(chk->data); 2371163953Srrs chk->data = NULL; 2372163953Srrs } 2373163953Srrs stcb->asoc.ctrl_queue_cnt--; 2374163953Srrs sctp_free_remote_addr(chk->whoTo); 2375163953Srrs sctp_free_a_chunk(stcb, chk); 2376163953Srrs break; 2377163953Srrs } 2378163953Srrs } 2379163953Srrs} 2380163953Srrs 2381163953Srrsstatic void 2382163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2383163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2384163953Srrs{ 2385163953Srrs struct sctp_association *asoc; 2386163953Srrs 2387169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2388169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2389163953Srrs if (stcb == NULL) 2390163953Srrs return; 2391163953Srrs 2392163953Srrs asoc = &stcb->asoc; 2393163953Srrs /* process according to association state */ 2394163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2395163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2396163953Srrs SCTP_TCB_UNLOCK(stcb); 2397163953Srrs return; 2398163953Srrs } 2399163953Srrs /* notify upper layer protocol */ 2400163953Srrs if (stcb->sctp_socket) { 2401163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2402163953Srrs /* are the queues empty? they should be */ 2403163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2404163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2405163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2406164205Srrs sctp_report_all_outbound(stcb, 0); 2407163953Srrs } 2408163953Srrs } 2409163953Srrs /* stop the timer */ 2410165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_21); 2411163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2412163953Srrs /* free the TCB */ 2413165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2414163953Srrs return; 2415163953Srrs} 2416163953Srrs 2417163953Srrsstatic int 2418163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2419163953Srrs struct sctp_nets *net, uint8_t flg) 2420163953Srrs{ 2421163953Srrs switch (desc->chunk_type) { 2422163953Srrs case SCTP_DATA: 2423163953Srrs /* find the tsn to resend (possibly */ 2424163953Srrs { 2425163953Srrs uint32_t tsn; 2426163953Srrs struct sctp_tmit_chunk *tp1; 2427163953Srrs 2428163953Srrs tsn = ntohl(desc->tsn_ifany); 2429163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2430163953Srrs while (tp1) { 2431163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2432163953Srrs /* found it */ 2433163953Srrs break; 2434163953Srrs } 2435163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2436163953Srrs MAX_TSN)) { 2437163953Srrs /* not found */ 2438163953Srrs tp1 = NULL; 2439163953Srrs break; 2440163953Srrs } 2441163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2442163953Srrs } 2443163953Srrs if (tp1 == NULL) { 2444163953Srrs /* 2445163953Srrs * Do it the other way , aka without paying 2446163953Srrs * attention to queue seq order. 2447163953Srrs */ 2448163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2449163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2450163953Srrs while (tp1) { 2451163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2452163953Srrs /* found it */ 2453163953Srrs break; 2454163953Srrs } 2455163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2456163953Srrs } 2457163953Srrs } 2458163953Srrs if (tp1 == NULL) { 2459163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2460163953Srrs } 2461163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2462163953Srrs uint8_t *ddp; 2463163953Srrs 2464163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2465163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2466163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2467163953Srrs return (0); 2468163953Srrs } 2469163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2470163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2471163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2472163953Srrs return (0); 2473163953Srrs } 2474163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2475163953Srrs sizeof(struct sctp_data_chunk)); 2476163953Srrs { 2477163953Srrs unsigned int iii; 2478163953Srrs 2479163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2480163953Srrs iii++) { 2481163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2482163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2483163953Srrs return (-1); 2484163953Srrs } 2485163953Srrs } 2486163953Srrs } 2487163953Srrs /* 2488163953Srrs * We zero out the nonce so resync not 2489163953Srrs * needed 2490163953Srrs */ 2491163953Srrs tp1->rec.data.ect_nonce = 0; 2492163953Srrs 2493163953Srrs if (tp1->do_rtt) { 2494163953Srrs /* 2495163953Srrs * this guy had a RTO calculation 2496163953Srrs * pending on it, cancel it 2497163953Srrs */ 2498163953Srrs tp1->do_rtt = 0; 2499163953Srrs } 2500163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2501163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2502163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2503163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2504163953Srrs /* 2505163953Srrs * mark it as if we were doing a FR, since 2506163953Srrs * we will be getting gap ack reports behind 2507163953Srrs * the info from the router. 2508163953Srrs */ 2509163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2510163953Srrs /* 2511163953Srrs * mark the tsn with what sequences can 2512163953Srrs * cause a new FR. 2513163953Srrs */ 2514163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2515163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2516163953Srrs } else { 2517163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2518163953Srrs } 2519163953Srrs 2520163953Srrs /* restart the timer */ 2521163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2522165220Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2523163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2524163953Srrs stcb, tp1->whoTo); 2525163953Srrs 2526163953Srrs /* fix counts and things */ 2527170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 2528170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2529170744Srrs tp1->whoTo->flight_size, 2530170744Srrs tp1->book_size, 2531170744Srrs (uintptr_t) stcb, 2532170744Srrs tp1->rec.data.TSN_seq); 2533170744Srrs } 2534168709Srrs sctp_flight_size_decrease(tp1); 2535168709Srrs sctp_total_flight_decrease(stcb, tp1); 2536163953Srrs } { 2537163953Srrs /* audit code */ 2538163953Srrs unsigned int audit; 2539163953Srrs 2540163953Srrs audit = 0; 2541163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2542163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2543163953Srrs audit++; 2544163953Srrs } 2545163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2546163953Srrs sctp_next) { 2547163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2548163953Srrs audit++; 2549163953Srrs } 2550163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2551169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2552163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2553163953Srrs#ifndef SCTP_AUDITING_ENABLED 2554163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2555163953Srrs#endif 2556163953Srrs } 2557163953Srrs } 2558163953Srrs } 2559163953Srrs break; 2560163953Srrs case SCTP_ASCONF: 2561163953Srrs { 2562163953Srrs struct sctp_tmit_chunk *asconf; 2563163953Srrs 2564163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2565163953Srrs sctp_next) { 2566163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2567163953Srrs break; 2568163953Srrs } 2569163953Srrs } 2570163953Srrs if (asconf) { 2571163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2572163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2573163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2574163953Srrs asconf->snd_count--; 2575163953Srrs } 2576163953Srrs } 2577163953Srrs break; 2578163953Srrs case SCTP_INITIATION: 2579163953Srrs /* resend the INIT */ 2580163953Srrs stcb->asoc.dropped_special_cnt++; 2581163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2582163953Srrs /* 2583163953Srrs * If we can get it in, in a few attempts we do 2584163953Srrs * this, otherwise we let the timer fire. 2585163953Srrs */ 2586163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2587165220Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2588163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2589163953Srrs } 2590163953Srrs break; 2591163953Srrs case SCTP_SELECTIVE_ACK: 2592163953Srrs /* resend the sack */ 2593163953Srrs sctp_send_sack(stcb); 2594163953Srrs break; 2595163953Srrs case SCTP_HEARTBEAT_REQUEST: 2596163953Srrs /* resend a demand HB */ 2597169378Srrs (void)sctp_send_hb(stcb, 1, net); 2598163953Srrs break; 2599163953Srrs case SCTP_SHUTDOWN: 2600163953Srrs sctp_send_shutdown(stcb, net); 2601163953Srrs break; 2602163953Srrs case SCTP_SHUTDOWN_ACK: 2603163953Srrs sctp_send_shutdown_ack(stcb, net); 2604163953Srrs break; 2605163953Srrs case SCTP_COOKIE_ECHO: 2606163953Srrs { 2607163953Srrs struct sctp_tmit_chunk *cookie; 2608163953Srrs 2609163953Srrs cookie = NULL; 2610163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2611163953Srrs sctp_next) { 2612163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2613163953Srrs break; 2614163953Srrs } 2615163953Srrs } 2616163953Srrs if (cookie) { 2617163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2618163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2619163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2620163953Srrs sctp_stop_all_cookie_timers(stcb); 2621163953Srrs } 2622163953Srrs } 2623163953Srrs break; 2624163953Srrs case SCTP_COOKIE_ACK: 2625163953Srrs sctp_send_cookie_ack(stcb); 2626163953Srrs break; 2627163953Srrs case SCTP_ASCONF_ACK: 2628163953Srrs /* resend last asconf ack */ 2629163953Srrs sctp_send_asconf_ack(stcb, 1); 2630163953Srrs break; 2631163953Srrs case SCTP_FORWARD_CUM_TSN: 2632163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2633163953Srrs break; 2634163953Srrs /* can't do anything with these */ 2635163953Srrs case SCTP_PACKET_DROPPED: 2636163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2637163953Srrs case SCTP_HEARTBEAT_ACK: 2638163953Srrs case SCTP_ABORT_ASSOCIATION: 2639163953Srrs case SCTP_OPERATION_ERROR: 2640163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2641163953Srrs case SCTP_ECN_ECHO: 2642163953Srrs case SCTP_ECN_CWR: 2643163953Srrs default: 2644163953Srrs break; 2645163953Srrs } 2646163953Srrs return (0); 2647163953Srrs} 2648163953Srrs 2649163953Srrsvoid 2650163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2651163953Srrs{ 2652163953Srrs int i; 2653163953Srrs uint16_t temp; 2654163953Srrs 2655163953Srrs /* 2656163953Srrs * We set things to 0xffff since this is the last delivered sequence 2657163953Srrs * and we will be sending in 0 after the reset. 2658163953Srrs */ 2659163953Srrs 2660163953Srrs if (number_entries) { 2661163953Srrs for (i = 0; i < number_entries; i++) { 2662163953Srrs temp = ntohs(list[i]); 2663163953Srrs if (temp >= stcb->asoc.streamincnt) { 2664163953Srrs continue; 2665163953Srrs } 2666163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2667163953Srrs } 2668163953Srrs } else { 2669163953Srrs list = NULL; 2670163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2671163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2672163953Srrs } 2673163953Srrs } 2674163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2675163953Srrs} 2676163953Srrs 2677163953Srrsstatic void 2678163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2679163953Srrs{ 2680163953Srrs int i; 2681163953Srrs 2682163953Srrs if (number_entries == 0) { 2683163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2684163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2685163953Srrs } 2686163953Srrs } else if (number_entries) { 2687163953Srrs for (i = 0; i < number_entries; i++) { 2688163953Srrs uint16_t temp; 2689163953Srrs 2690163953Srrs temp = ntohs(list[i]); 2691163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2692163953Srrs /* no such stream */ 2693163953Srrs continue; 2694163953Srrs } 2695163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2696163953Srrs } 2697163953Srrs } 2698163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2699163953Srrs} 2700163953Srrs 2701163953Srrs 2702163953Srrsstruct sctp_stream_reset_out_request * 2703163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2704163953Srrs{ 2705163953Srrs struct sctp_association *asoc; 2706163953Srrs struct sctp_stream_reset_out_req *req; 2707163953Srrs struct sctp_stream_reset_out_request *r; 2708163953Srrs struct sctp_tmit_chunk *chk; 2709163953Srrs int len, clen; 2710163953Srrs 2711163953Srrs asoc = &stcb->asoc; 2712163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2713169208Srrs asoc->stream_reset_outstanding = 0; 2714163953Srrs return (NULL); 2715163953Srrs } 2716163953Srrs if (stcb->asoc.str_reset == NULL) { 2717169208Srrs asoc->stream_reset_outstanding = 0; 2718163953Srrs return (NULL); 2719163953Srrs } 2720163953Srrs chk = stcb->asoc.str_reset; 2721163953Srrs if (chk->data == NULL) { 2722163953Srrs return (NULL); 2723163953Srrs } 2724163953Srrs if (bchk) { 2725163953Srrs /* he wants a copy of the chk pointer */ 2726163953Srrs *bchk = chk; 2727163953Srrs } 2728163953Srrs clen = chk->send_size; 2729163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2730163953Srrs r = &req->sr_req; 2731163953Srrs if (ntohl(r->request_seq) == seq) { 2732163953Srrs /* found it */ 2733163953Srrs return (r); 2734163953Srrs } 2735163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2736163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2737163953Srrs /* move to the next one, there can only be a max of two */ 2738163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2739163953Srrs if (ntohl(r->request_seq) == seq) { 2740163953Srrs return (r); 2741163953Srrs } 2742163953Srrs } 2743163953Srrs /* that seq is not here */ 2744163953Srrs return (NULL); 2745163953Srrs} 2746163953Srrs 2747163953Srrsstatic void 2748163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2749163953Srrs{ 2750163953Srrs struct sctp_association *asoc; 2751163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2752163953Srrs 2753163953Srrs if (stcb->asoc.str_reset == NULL) { 2754163953Srrs return; 2755163953Srrs } 2756166675Srrs asoc = &stcb->asoc; 2757166675Srrs 2758165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2759163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2760163953Srrs chk, 2761163953Srrs sctp_next); 2762163953Srrs if (chk->data) { 2763163953Srrs sctp_m_freem(chk->data); 2764163953Srrs chk->data = NULL; 2765163953Srrs } 2766163953Srrs asoc->ctrl_queue_cnt--; 2767163953Srrs sctp_free_remote_addr(chk->whoTo); 2768163953Srrs 2769163953Srrs sctp_free_a_chunk(stcb, chk); 2770163953Srrs stcb->asoc.str_reset = NULL; 2771163953Srrs} 2772163953Srrs 2773163953Srrs 2774163953Srrsstatic int 2775163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2776163953Srrs uint32_t seq, uint32_t action, 2777164139Srrs struct sctp_stream_reset_response *respin) 2778163953Srrs{ 2779163953Srrs uint16_t type; 2780163953Srrs int lparm_len; 2781163953Srrs struct sctp_association *asoc = &stcb->asoc; 2782163953Srrs struct sctp_tmit_chunk *chk; 2783163953Srrs struct sctp_stream_reset_out_request *srparam; 2784163953Srrs int number_entries; 2785163953Srrs 2786163953Srrs if (asoc->stream_reset_outstanding == 0) { 2787163953Srrs /* duplicate */ 2788163953Srrs return (0); 2789163953Srrs } 2790163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2791163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2792163953Srrs if (srparam) { 2793163953Srrs stcb->asoc.str_reset_seq_out++; 2794163953Srrs type = ntohs(srparam->ph.param_type); 2795163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2796163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2797164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2798163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2799163953Srrs if (asoc->stream_reset_outstanding) 2800163953Srrs asoc->stream_reset_outstanding--; 2801163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2802163953Srrs /* do it */ 2803163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2804163953Srrs } else { 2805163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2806163953Srrs } 2807163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2808163953Srrs /* Answered my request */ 2809164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2810163953Srrs if (asoc->stream_reset_outstanding) 2811163953Srrs asoc->stream_reset_outstanding--; 2812163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2813163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2814163953Srrs } 2815163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2816163953Srrs /** 2817163953Srrs * a) Adopt the new in tsn. 2818163953Srrs * b) reset the map 2819163953Srrs * c) Adopt the new out-tsn 2820163953Srrs */ 2821163953Srrs struct sctp_stream_reset_response_tsn *resp; 2822163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2823163953Srrs int abort_flag = 0; 2824163953Srrs 2825163953Srrs if (respin == NULL) { 2826163953Srrs /* huh ? */ 2827163953Srrs return (0); 2828163953Srrs } 2829163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2830163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2831163953Srrs asoc->stream_reset_outstanding--; 2832163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2833163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2834163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2835163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2836163953Srrs if (abort_flag) { 2837163953Srrs return (1); 2838163953Srrs } 2839163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2840163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2841163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2842163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2843163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2844163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2845163953Srrs 2846163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2847163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2848163953Srrs 2849163953Srrs } 2850163953Srrs } 2851163953Srrs /* get rid of the request and get the request flags */ 2852163953Srrs if (asoc->stream_reset_outstanding == 0) { 2853163953Srrs sctp_clean_up_stream_reset(stcb); 2854163953Srrs } 2855163953Srrs } 2856163953Srrs } 2857163953Srrs return (0); 2858163953Srrs} 2859163953Srrs 2860163953Srrsstatic void 2861163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2862163953Srrs struct sctp_tmit_chunk *chk, 2863163953Srrs struct sctp_stream_reset_in_request *req) 2864163953Srrs{ 2865163953Srrs uint32_t seq; 2866163953Srrs int len, i; 2867163953Srrs int number_entries; 2868163953Srrs uint16_t temp; 2869163953Srrs 2870163953Srrs /* 2871163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2872163953Srrs * seq_in is right. 2873163953Srrs */ 2874163953Srrs struct sctp_association *asoc = &stcb->asoc; 2875163953Srrs 2876163953Srrs seq = ntohl(req->request_seq); 2877163953Srrs if (asoc->str_reset_seq_in == seq) { 2878163953Srrs if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2879163953Srrs len = ntohs(req->ph.param_length); 2880163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2881163953Srrs for (i = 0; i < number_entries; i++) { 2882163953Srrs temp = ntohs(req->list_of_streams[i]); 2883163953Srrs req->list_of_streams[i] = temp; 2884163953Srrs } 2885163953Srrs /* move the reset action back one */ 2886163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2887163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2888163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2889163953Srrs asoc->str_reset_seq_out, 2890163953Srrs seq, (asoc->sending_seq - 1)); 2891163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2892163953Srrs asoc->str_reset = chk; 2893163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 2894163953Srrs stcb->asoc.stream_reset_outstanding++; 2895163953Srrs } else { 2896163953Srrs /* Can't do it, since we have sent one out */ 2897163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2898163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 2899163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2900163953Srrs } 2901163953Srrs asoc->str_reset_seq_in++; 2902163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 2903163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2904163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 2905163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 2906163953Srrs } else { 2907163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 2908163953Srrs } 2909163953Srrs} 2910163953Srrs 2911163953Srrsstatic int 2912163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 2913163953Srrs struct sctp_tmit_chunk *chk, 2914163953Srrs struct sctp_stream_reset_tsn_request *req) 2915163953Srrs{ 2916163953Srrs /* reset all in and out and update the tsn */ 2917163953Srrs /* 2918163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 2919163953Srrs * and set cum-ack to it. Also process this selected number as a 2920163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 2921163953Srrs */ 2922163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2923163953Srrs struct sctp_association *asoc = &stcb->asoc; 2924163953Srrs int abort_flag = 0; 2925163953Srrs uint32_t seq; 2926163953Srrs 2927163953Srrs seq = ntohl(req->request_seq); 2928163953Srrs if (asoc->str_reset_seq_in == seq) { 2929163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2930163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2931163953Srrs fwdtsn.ch.chunk_flags = 0; 2932163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 2933163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2934163953Srrs if (abort_flag) { 2935163953Srrs return (1); 2936163953Srrs } 2937163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 2938163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2939163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 2940163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2941163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 2942163953Srrs /* save off historical data for retrans */ 2943163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 2944163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 2945163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 2946163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 2947163953Srrs 2948163953Srrs sctp_add_stream_reset_result_tsn(chk, 2949163953Srrs ntohl(req->request_seq), 2950163953Srrs SCTP_STREAM_RESET_PERFORMED, 2951163953Srrs stcb->asoc.sending_seq, 2952163953Srrs stcb->asoc.mapping_array_base_tsn); 2953163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2954163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2955163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 2956163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2957163953Srrs 2958163953Srrs asoc->str_reset_seq_in++; 2959163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 2960163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 2961163953Srrs stcb->asoc.last_sending_seq[0], 2962163953Srrs stcb->asoc.last_base_tsnsent[0] 2963163953Srrs ); 2964163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 2965163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 2966163953Srrs stcb->asoc.last_sending_seq[1], 2967163953Srrs stcb->asoc.last_base_tsnsent[1] 2968163953Srrs ); 2969163953Srrs } else { 2970163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 2971163953Srrs } 2972163953Srrs return (0); 2973163953Srrs} 2974163953Srrs 2975163953Srrsstatic void 2976163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 2977163953Srrs struct sctp_tmit_chunk *chk, 2978163953Srrs struct sctp_stream_reset_out_request *req) 2979163953Srrs{ 2980163953Srrs uint32_t seq, tsn; 2981163953Srrs int number_entries, len; 2982163953Srrs struct sctp_association *asoc = &stcb->asoc; 2983163953Srrs 2984163953Srrs seq = ntohl(req->request_seq); 2985163953Srrs 2986163953Srrs /* now if its not a duplicate we process it */ 2987163953Srrs if (asoc->str_reset_seq_in == seq) { 2988163953Srrs len = ntohs(req->ph.param_length); 2989163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 2990163953Srrs /* 2991163953Srrs * the sender is resetting, handle the list issue.. we must 2992163953Srrs * a) verify if we can do the reset, if so no problem b) If 2993163953Srrs * we can't do the reset we must copy the request. c) queue 2994163953Srrs * it, and setup the data in processor to trigger it off 2995163953Srrs * when needed and dequeue all the queued data. 2996163953Srrs */ 2997163953Srrs tsn = ntohl(req->send_reset_at_tsn); 2998163953Srrs 2999163953Srrs /* move the reset action back one */ 3000163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3001163953Srrs if ((tsn == asoc->cumulative_tsn) || 3002163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3003163953Srrs /* we can do it now */ 3004163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3005163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3006163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3007163953Srrs } else { 3008163953Srrs /* 3009163953Srrs * we must queue it up and thus wait for the TSN's 3010163953Srrs * to arrive that are at or before tsn 3011163953Srrs */ 3012163953Srrs struct sctp_stream_reset_list *liste; 3013163953Srrs int siz; 3014163953Srrs 3015163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3016163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3017170091Srrs siz, SCTP_M_STRESET); 3018163953Srrs if (liste == NULL) { 3019163953Srrs /* gak out of memory */ 3020163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3021163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3022163953Srrs return; 3023163953Srrs } 3024163953Srrs liste->tsn = tsn; 3025163953Srrs liste->number_entries = number_entries; 3026163953Srrs memcpy(&liste->req, req, 3027163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3028163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3029163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3030163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3031163953Srrs } 3032163953Srrs asoc->str_reset_seq_in++; 3033163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3034163953Srrs /* 3035163953Srrs * one seq back, just echo back last action since my 3036163953Srrs * response was lost. 3037163953Srrs */ 3038163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3039163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3040163953Srrs /* 3041163953Srrs * two seq back, just echo back last action since my 3042163953Srrs * response was lost. 3043163953Srrs */ 3044163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3045163953Srrs } else { 3046163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3047163953Srrs } 3048163953Srrs} 3049163953Srrs 3050163953Srrsstatic int 3051163953Srrssctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_out_req *sr_req) 3052163953Srrs{ 3053163953Srrs int chk_length, param_len, ptype; 3054163953Srrs uint32_t seq; 3055163953Srrs int num_req = 0; 3056163953Srrs struct sctp_tmit_chunk *chk; 3057163953Srrs struct sctp_chunkhdr *ch; 3058163953Srrs struct sctp_paramhdr *ph; 3059166675Srrs int ret_code = 0; 3060166675Srrs int num_param = 0; 3061163953Srrs 3062163953Srrs /* now it may be a reset or a reset-response */ 3063163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3064163953Srrs 3065163953Srrs /* setup for adding the response */ 3066163953Srrs sctp_alloc_a_chunk(stcb, chk); 3067163953Srrs if (chk == NULL) { 3068163953Srrs return (ret_code); 3069163953Srrs } 3070163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3071169208Srrs chk->rec.chunk_id.can_take_data = 0; 3072163953Srrs chk->asoc = &stcb->asoc; 3073163953Srrs chk->no_fr_allowed = 0; 3074163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3075166023Srrs chk->book_size_scale = 0; 3076165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3077163953Srrs if (chk->data == NULL) { 3078163953Srrsstrres_nochunk: 3079163953Srrs if (chk->data) { 3080163953Srrs sctp_m_freem(chk->data); 3081163953Srrs chk->data = NULL; 3082163953Srrs } 3083163953Srrs sctp_free_a_chunk(stcb, chk); 3084163953Srrs return (ret_code); 3085163953Srrs } 3086165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3087163953Srrs 3088163953Srrs /* setup chunk parameters */ 3089163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3090163953Srrs chk->snd_count = 0; 3091163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3092163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3093163953Srrs 3094163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3095163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3096163953Srrs ch->chunk_flags = 0; 3097163953Srrs ch->chunk_length = htons(chk->send_size); 3098165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3099163953Srrs ph = (struct sctp_paramhdr *)&sr_req->sr_req; 3100163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3101163953Srrs param_len = ntohs(ph->param_length); 3102163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3103163953Srrs /* bad param */ 3104163953Srrs break; 3105163953Srrs } 3106163953Srrs ptype = ntohs(ph->param_type); 3107163953Srrs num_param++; 3108163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3109163953Srrs /* hit the max of parameters already sorry.. */ 3110163953Srrs break; 3111163953Srrs } 3112163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3113163953Srrs struct sctp_stream_reset_out_request *req_out; 3114163953Srrs 3115163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3116163953Srrs num_req++; 3117163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3118163953Srrs seq = ntohl(req_out->response_seq); 3119163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3120163953Srrs /* implicit ack */ 3121169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3122163953Srrs } 3123163953Srrs } 3124163953Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out); 3125163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3126163953Srrs struct sctp_stream_reset_in_request *req_in; 3127163953Srrs 3128163953Srrs num_req++; 3129163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3130163953Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in); 3131163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3132163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3133163953Srrs 3134163953Srrs num_req++; 3135163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3136163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3137163953Srrs ret_code = 1; 3138163953Srrs goto strres_nochunk; 3139163953Srrs } 3140163953Srrs /* no more */ 3141163953Srrs break; 3142163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3143163953Srrs struct sctp_stream_reset_response *resp; 3144163953Srrs uint32_t result; 3145163953Srrs 3146163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3147163953Srrs seq = ntohl(resp->response_seq); 3148163953Srrs result = ntohl(resp->result); 3149163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3150163953Srrs ret_code = 1; 3151163953Srrs goto strres_nochunk; 3152163953Srrs } 3153163953Srrs } else { 3154163953Srrs break; 3155163953Srrs } 3156163953Srrs 3157163953Srrs ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len)); 3158163953Srrs chk_length -= SCTP_SIZE32(param_len); 3159163953Srrs } 3160163953Srrs if (num_req == 0) { 3161163953Srrs /* we have no response free the stuff */ 3162163953Srrs goto strres_nochunk; 3163163953Srrs } 3164163953Srrs /* ok we have a chunk to link in */ 3165163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3166163953Srrs chk, 3167163953Srrs sctp_next); 3168163953Srrs stcb->asoc.ctrl_queue_cnt++; 3169163953Srrs return (ret_code); 3170163953Srrs} 3171163953Srrs 3172163953Srrs/* 3173163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3174163953Srrs * to handle this, either we get the whole packet and must disect it 3175163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3176163953Srrs * from a middle box that did the disectting for us. 3177163953Srrs */ 3178163953Srrsstatic void 3179163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3180170781Srrs struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 3181163953Srrs{ 3182163953Srrs uint32_t bottle_bw, on_queue; 3183163953Srrs uint16_t trunc_len; 3184163953Srrs unsigned int chlen; 3185163953Srrs unsigned int at; 3186163953Srrs struct sctp_chunk_desc desc; 3187163953Srrs struct sctp_chunkhdr *ch; 3188163953Srrs 3189163953Srrs chlen = ntohs(cp->ch.chunk_length); 3190163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3191163953Srrs /* XXX possible chlen underflow */ 3192163953Srrs if (chlen == 0) { 3193163953Srrs ch = NULL; 3194163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3195163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3196163953Srrs } else { 3197163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3198163953Srrs chlen -= sizeof(struct sctphdr); 3199163953Srrs /* XXX possible chlen underflow */ 3200163953Srrs memset(&desc, 0, sizeof(desc)); 3201163953Srrs } 3202163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3203170781Srrs if (trunc_len > limit) { 3204170781Srrs trunc_len = limit; 3205170781Srrs } 3206163953Srrs /* now the chunks themselves */ 3207163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3208163953Srrs desc.chunk_type = ch->chunk_type; 3209163953Srrs /* get amount we need to move */ 3210163953Srrs at = ntohs(ch->chunk_length); 3211163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3212163953Srrs /* corrupt chunk, maybe at the end? */ 3213163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3214163953Srrs break; 3215163953Srrs } 3216163953Srrs if (trunc_len == 0) { 3217163953Srrs /* we are supposed to have all of it */ 3218163953Srrs if (at > chlen) { 3219163953Srrs /* corrupt skip it */ 3220163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3221163953Srrs break; 3222163953Srrs } 3223163953Srrs } else { 3224163953Srrs /* is there enough of it left ? */ 3225163953Srrs if (desc.chunk_type == SCTP_DATA) { 3226163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3227163953Srrs sizeof(desc.data_bytes))) { 3228163953Srrs break; 3229163953Srrs } 3230163953Srrs } else { 3231163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3232163953Srrs break; 3233163953Srrs } 3234163953Srrs } 3235163953Srrs } 3236163953Srrs if (desc.chunk_type == SCTP_DATA) { 3237163953Srrs /* can we get out the tsn? */ 3238163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3239163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3240163953Srrs 3241163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3242163953Srrs /* yep */ 3243163953Srrs struct sctp_data_chunk *dcp; 3244163953Srrs uint8_t *ddp; 3245163953Srrs unsigned int iii; 3246163953Srrs 3247163953Srrs dcp = (struct sctp_data_chunk *)ch; 3248163953Srrs ddp = (uint8_t *) (dcp + 1); 3249163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3250163953Srrs desc.data_bytes[iii] = ddp[iii]; 3251163953Srrs } 3252163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3253163953Srrs } else { 3254163953Srrs /* nope we are done. */ 3255163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3256163953Srrs break; 3257163953Srrs } 3258163953Srrs } else { 3259163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3260163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3261163953Srrs } 3262163953Srrs 3263163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3264163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3265163953Srrs break; 3266163953Srrs } 3267163953Srrs if (SCTP_SIZE32(at) > chlen) { 3268163953Srrs break; 3269163953Srrs } 3270163953Srrs chlen -= SCTP_SIZE32(at); 3271163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3272163953Srrs /* done, none left */ 3273163953Srrs break; 3274163953Srrs } 3275163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3276163953Srrs } 3277163953Srrs /* Now update any rwnd --- possibly */ 3278163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3279163953Srrs /* From a peer, we get a rwnd report */ 3280163953Srrs uint32_t a_rwnd; 3281163953Srrs 3282163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3283163953Srrs 3284163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3285163953Srrs on_queue = ntohl(cp->current_onq); 3286163953Srrs if (bottle_bw && on_queue) { 3287163953Srrs /* a rwnd report is in here */ 3288163953Srrs if (bottle_bw > on_queue) 3289163953Srrs a_rwnd = bottle_bw - on_queue; 3290163953Srrs else 3291163953Srrs a_rwnd = 0; 3292163953Srrs 3293163953Srrs if (a_rwnd == 0) 3294163953Srrs stcb->asoc.peers_rwnd = 0; 3295163953Srrs else { 3296163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3297163953Srrs stcb->asoc.peers_rwnd = 3298163953Srrs a_rwnd - stcb->asoc.total_flight; 3299163953Srrs } else { 3300163953Srrs stcb->asoc.peers_rwnd = 0; 3301163953Srrs } 3302163953Srrs if (stcb->asoc.peers_rwnd < 3303163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3304163953Srrs /* SWS sender side engages */ 3305163953Srrs stcb->asoc.peers_rwnd = 0; 3306163953Srrs } 3307163953Srrs } 3308163953Srrs } 3309163953Srrs } else { 3310163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3311163953Srrs } 3312163953Srrs 3313163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3314163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3315163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3316163953Srrs (stcb->asoc.sat_network)) { 3317163953Srrs /* 3318163953Srrs * This is debateable but for sat networks it makes sense 3319163953Srrs * Note if a T3 timer has went off, we will prohibit any 3320163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3321163953Srrs */ 3322163953Srrs uint32_t bw_avail; 3323163953Srrs int rtt, incr; 3324163953Srrs 3325163953Srrs int old_cwnd = net->cwnd; 3326163953Srrs 3327163953Srrs /* need real RTT for this calc */ 3328163953Srrs rtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 3329163953Srrs /* get bottle neck bw */ 3330163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3331163953Srrs /* and whats on queue */ 3332163953Srrs on_queue = ntohl(cp->current_onq); 3333163953Srrs /* 3334163953Srrs * adjust the on-queue if our flight is more it could be 3335163953Srrs * that the router has not yet gotten data "in-flight" to it 3336163953Srrs */ 3337163953Srrs if (on_queue < net->flight_size) 3338163953Srrs on_queue = net->flight_size; 3339163953Srrs 3340163953Srrs /* calculate the available space */ 3341163953Srrs bw_avail = (bottle_bw * rtt) / 1000; 3342163953Srrs if (bw_avail > bottle_bw) { 3343163953Srrs /* 3344163953Srrs * Cap the growth to no more than the bottle neck. 3345163953Srrs * This can happen as RTT slides up due to queues. 3346163953Srrs * It also means if you have more than a 1 second 3347163953Srrs * RTT with a empty queue you will be limited to the 3348163953Srrs * bottle_bw per second no matter if other points 3349163953Srrs * have 1/2 the RTT and you could get more out... 3350163953Srrs */ 3351163953Srrs bw_avail = bottle_bw; 3352163953Srrs } 3353163953Srrs if (on_queue > bw_avail) { 3354163953Srrs /* 3355163953Srrs * No room for anything else don't allow anything 3356163953Srrs * else to be "added to the fire". 3357163953Srrs */ 3358163953Srrs int seg_inflight, seg_onqueue, my_portion; 3359163953Srrs 3360163953Srrs net->partial_bytes_acked = 0; 3361163953Srrs 3362163953Srrs /* how much are we over queue size? */ 3363163953Srrs incr = on_queue - bw_avail; 3364163953Srrs if (stcb->asoc.seen_a_sack_this_pkt) { 3365163953Srrs /* 3366163953Srrs * undo any cwnd adjustment that the sack 3367163953Srrs * might have made 3368163953Srrs */ 3369163953Srrs net->cwnd = net->prev_cwnd; 3370163953Srrs } 3371163953Srrs /* Now how much of that is mine? */ 3372163953Srrs seg_inflight = net->flight_size / net->mtu; 3373163953Srrs seg_onqueue = on_queue / net->mtu; 3374163953Srrs my_portion = (incr * seg_inflight) / seg_onqueue; 3375163953Srrs 3376163953Srrs /* Have I made an adjustment already */ 3377163953Srrs if (net->cwnd > net->flight_size) { 3378163953Srrs /* 3379163953Srrs * for this flight I made an adjustment we 3380163953Srrs * need to decrease the portion by a share 3381163953Srrs * our previous adjustment. 3382163953Srrs */ 3383163953Srrs int diff_adj; 3384163953Srrs 3385163953Srrs diff_adj = net->cwnd - net->flight_size; 3386163953Srrs if (diff_adj > my_portion) 3387163953Srrs my_portion = 0; 3388163953Srrs else 3389163953Srrs my_portion -= diff_adj; 3390163953Srrs } 3391163953Srrs /* 3392163953Srrs * back down to the previous cwnd (assume we have 3393163953Srrs * had a sack before this packet). minus what ever 3394163953Srrs * portion of the overage is my fault. 3395163953Srrs */ 3396163953Srrs net->cwnd -= my_portion; 3397163953Srrs 3398163953Srrs /* we will NOT back down more than 1 MTU */ 3399163953Srrs if (net->cwnd <= net->mtu) { 3400163953Srrs net->cwnd = net->mtu; 3401163953Srrs } 3402163953Srrs /* force into CA */ 3403163953Srrs net->ssthresh = net->cwnd - 1; 3404163953Srrs } else { 3405163953Srrs /* 3406163953Srrs * Take 1/4 of the space left or max burst up .. 3407163953Srrs * whichever is less. 3408163953Srrs */ 3409163953Srrs incr = min((bw_avail - on_queue) >> 2, 3410170140Srrs stcb->asoc.max_burst * net->mtu); 3411163953Srrs net->cwnd += incr; 3412163953Srrs } 3413163953Srrs if (net->cwnd > bw_avail) { 3414163953Srrs /* We can't exceed the pipe size */ 3415163953Srrs net->cwnd = bw_avail; 3416163953Srrs } 3417163953Srrs if (net->cwnd < net->mtu) { 3418163953Srrs /* We always have 1 MTU */ 3419163953Srrs net->cwnd = net->mtu; 3420163953Srrs } 3421163953Srrs if (net->cwnd - old_cwnd != 0) { 3422163953Srrs /* log only changes */ 3423170744Srrs if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) { 3424170744Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), 3425170744Srrs SCTP_CWND_LOG_FROM_SAT); 3426170744Srrs } 3427163953Srrs } 3428163953Srrs } 3429163953Srrs} 3430163953Srrs 3431163953Srrs/* 3432163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3433163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3434163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3435163953Srrs * length of the complete packet outputs: - length: modified to remaining 3436163953Srrs * length after control processing - netp: modified to new sctp_nets after 3437163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3438163953Srrs * bad packet,...) otherwise return the tcb for this packet 3439163953Srrs */ 3440170099Srrs#ifdef __GNUC__ 3441170099Srrs__attribute__((noinline)) 3442170099Srrs#endif 3443170099Srrs static struct sctp_tcb * 3444170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3445170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3446170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3447170181Srrs uint32_t vrf_id) 3448163953Srrs{ 3449163953Srrs struct sctp_association *asoc; 3450163953Srrs uint32_t vtag_in; 3451163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3452170140Srrs uint32_t chk_length; 3453163953Srrs int ret; 3454168299Srrs int abort_no_unlock = 0; 3455163953Srrs 3456163953Srrs /* 3457163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3458163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3459163953Srrs * until we get into jumbo grams and such.. 3460163953Srrs */ 3461166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3462163953Srrs struct sctp_tcb *locked_tcb = stcb; 3463163953Srrs int got_auth = 0; 3464163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3465163953Srrs int auth_skipped = 0; 3466163953Srrs 3467169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3468169420Srrs iphlen, *offset, length, stcb); 3469163953Srrs 3470163953Srrs /* validate chunk header length... */ 3471163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3472170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3473170056Srrs ntohs(ch->chunk_length)); 3474170091Srrs if (locked_tcb) { 3475170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3476170091Srrs } 3477163953Srrs return (NULL); 3478163953Srrs } 3479163953Srrs /* 3480163953Srrs * validate the verification tag 3481163953Srrs */ 3482163953Srrs vtag_in = ntohl(sh->v_tag); 3483163953Srrs 3484165220Srrs if (locked_tcb) { 3485165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3486165220Srrs } 3487163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3488170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3489170056Srrs ntohs(ch->chunk_length), vtag_in); 3490163953Srrs if (vtag_in != 0) { 3491163953Srrs /* protocol error- silently discard... */ 3492163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3493169378Srrs if (locked_tcb) { 3494163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3495169378Srrs } 3496163953Srrs return (NULL); 3497163953Srrs } 3498163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3499163953Srrs /* 3500163953Srrs * If there is no stcb, skip the AUTH chunk and process 3501163953Srrs * later after a stcb is found (to validate the lookup was 3502163953Srrs * valid. 3503163953Srrs */ 3504163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3505163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3506163953Srrs /* save this chunk for later processing */ 3507163953Srrs auth_skipped = 1; 3508163953Srrs auth_offset = *offset; 3509163953Srrs auth_len = ntohs(ch->chunk_length); 3510163953Srrs 3511163953Srrs /* (temporarily) move past this chunk */ 3512163953Srrs *offset += SCTP_SIZE32(auth_len); 3513163953Srrs if (*offset >= length) { 3514163953Srrs /* no more data left in the mbuf chain */ 3515163953Srrs *offset = length; 3516170091Srrs if (locked_tcb) { 3517170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3518170091Srrs } 3519163953Srrs return (NULL); 3520163953Srrs } 3521163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3522163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3523163953Srrs } 3524169420Srrs if (ch == NULL) { 3525169420Srrs /* Help */ 3526169420Srrs *offset = length; 3527170091Srrs if (locked_tcb) { 3528170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3529170091Srrs } 3530169420Srrs return (NULL); 3531169420Srrs } 3532163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3533163953Srrs goto process_control_chunks; 3534163953Srrs } 3535163953Srrs /* 3536163953Srrs * first check if it's an ASCONF with an unknown src addr we 3537163953Srrs * need to look inside to find the association 3538163953Srrs */ 3539163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3540163953Srrs /* inp's refcount may be reduced */ 3541163953Srrs SCTP_INP_INCR_REF(inp); 3542163953Srrs 3543163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3544163953Srrs *offset, sh, &inp, netp); 3545163953Srrs if (stcb == NULL) { 3546163953Srrs /* 3547163953Srrs * reduce inp's refcount if not reduced in 3548163953Srrs * sctp_findassociation_ep_asconf(). 3549163953Srrs */ 3550163953Srrs SCTP_INP_DECR_REF(inp); 3551163953Srrs } 3552163953Srrs /* now go back and verify any auth chunk to be sure */ 3553163953Srrs if (auth_skipped && (stcb != NULL)) { 3554163953Srrs struct sctp_auth_chunk *auth; 3555163953Srrs 3556163953Srrs auth = (struct sctp_auth_chunk *) 3557163953Srrs sctp_m_getptr(m, auth_offset, 3558163953Srrs auth_len, chunk_buf); 3559163953Srrs got_auth = 1; 3560163953Srrs auth_skipped = 0; 3561169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3562163953Srrs auth_offset)) { 3563163953Srrs /* auth HMAC failed so dump it */ 3564163953Srrs *offset = length; 3565170091Srrs if (locked_tcb) { 3566170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3567170091Srrs } 3568163953Srrs return (NULL); 3569163953Srrs } else { 3570163953Srrs /* remaining chunks are HMAC checked */ 3571163953Srrs stcb->asoc.authenticated = 1; 3572163953Srrs } 3573163953Srrs } 3574163953Srrs } 3575163953Srrs if (stcb == NULL) { 3576163953Srrs /* no association, so it's out of the blue... */ 3577169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3578170181Srrs vrf_id); 3579163953Srrs *offset = length; 3580169378Srrs if (locked_tcb) { 3581163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3582169378Srrs } 3583163953Srrs return (NULL); 3584163953Srrs } 3585163953Srrs asoc = &stcb->asoc; 3586163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3587163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3588163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3589163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3590163953Srrs if ((vtag_in == asoc->my_vtag) || 3591163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3592163953Srrs (vtag_in == asoc->peer_vtag))) { 3593163953Srrs /* this is valid */ 3594163953Srrs } else { 3595163953Srrs /* drop this packet... */ 3596163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3597169378Srrs if (locked_tcb) { 3598163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3599169378Srrs } 3600163953Srrs return (NULL); 3601163953Srrs } 3602163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3603163953Srrs if (vtag_in != asoc->my_vtag) { 3604163953Srrs /* 3605163953Srrs * this could be a stale SHUTDOWN-ACK or the 3606163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3607163953Srrs * is still hung; we have started a new asoc 3608163953Srrs * but it won't complete until the shutdown 3609163953Srrs * is completed 3610163953Srrs */ 3611169378Srrs if (locked_tcb) { 3612163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3613169378Srrs } 3614163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3615170181Srrs NULL, vrf_id); 3616163953Srrs return (NULL); 3617163953Srrs } 3618163953Srrs } else { 3619163953Srrs /* for all other chunks, vtag must match */ 3620163953Srrs if (vtag_in != asoc->my_vtag) { 3621163953Srrs /* invalid vtag... */ 3622169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3623169420Srrs "invalid vtag: %xh, expect %xh\n", 3624169420Srrs vtag_in, asoc->my_vtag); 3625163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3626169378Srrs if (locked_tcb) { 3627163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3628169378Srrs } 3629163953Srrs *offset = length; 3630163953Srrs return (NULL); 3631163953Srrs } 3632163953Srrs } 3633163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3634163953Srrs /* 3635163953Srrs * process all control chunks... 3636163953Srrs */ 3637163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3638163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3639163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3640163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3641163953Srrs stcb->asoc.overall_error_count = 0; 3642163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3643163953Srrs *netp); 3644163953Srrs } 3645163953Srrsprocess_control_chunks: 3646163953Srrs while (IS_SCTP_CONTROL(ch)) { 3647163953Srrs /* validate chunk length */ 3648163953Srrs chk_length = ntohs(ch->chunk_length); 3649169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 3650169420Srrs ch->chunk_type, chk_length); 3651170744Srrs SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 3652170140Srrs if (chk_length < sizeof(*ch) || 3653170140Srrs (*offset + (int)chk_length) > length) { 3654163953Srrs *offset = length; 3655169378Srrs if (locked_tcb) { 3656163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3657169378Srrs } 3658163953Srrs return (NULL); 3659163953Srrs } 3660163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3661163953Srrs /* 3662163953Srrs * INIT-ACK only gets the init ack "header" portion only 3663163953Srrs * because we don't have to process the peer's COOKIE. All 3664163953Srrs * others get a complete chunk. 3665163953Srrs */ 3666169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3667169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3668163953Srrs /* get an init-ack chunk */ 3669163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3670163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3671163953Srrs if (ch == NULL) { 3672163953Srrs *offset = length; 3673169378Srrs if (locked_tcb) { 3674163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3675169378Srrs } 3676163953Srrs return (NULL); 3677163953Srrs } 3678170642Srrs } else { 3679170642Srrs /* 3680170642Srrs * For cookies and all other chunks. if the 3681170642Srrs */ 3682169208Srrs if (chk_length > sizeof(chunk_buf)) { 3683169208Srrs /* 3684169208Srrs * use just the size of the chunk buffer so 3685170642Srrs * the front part of our chunks fit in 3686170642Srrs * contiguous space up to the chunk buffer 3687170642Srrs * size (508 bytes). For chunks that need to 3688170642Srrs * get more than that they mus use the 3689170642Srrs * sctp_m_getptr() function or other means 3690170642Srrs * (know how to parse mbuf chains). Cookies 3691170642Srrs * do this already. 3692169208Srrs */ 3693169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3694169208Srrs (sizeof(chunk_buf) - 4), 3695169208Srrs chunk_buf); 3696169208Srrs if (ch == NULL) { 3697169208Srrs *offset = length; 3698169378Srrs if (locked_tcb) { 3699169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 3700169378Srrs } 3701169208Srrs return (NULL); 3702169208Srrs } 3703169208Srrs } else { 3704169208Srrs /* We can fit it all */ 3705170642Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3706170642Srrs chk_length, chunk_buf); 3707170642Srrs if (ch == NULL) { 3708170642Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 3709170642Srrs *offset = length; 3710170642Srrs if (locked_tcb) { 3711170642Srrs SCTP_TCB_UNLOCK(locked_tcb); 3712169208Srrs } 3713170642Srrs return (NULL); 3714163953Srrs } 3715163953Srrs } 3716163953Srrs } 3717163953Srrs num_chunks++; 3718163953Srrs /* Save off the last place we got a control from */ 3719163953Srrs if (stcb != NULL) { 3720169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 3721163953Srrs /* 3722163953Srrs * allow last_control to be NULL if 3723163953Srrs * ASCONF... ASCONF processing will find the 3724163953Srrs * right net later 3725163953Srrs */ 3726169420Srrs if ((netp != NULL) && (*netp != NULL)) 3727169420Srrs stcb->asoc.last_control_chunk_from = *netp; 3728163953Srrs } 3729163953Srrs } 3730163953Srrs#ifdef SCTP_AUDITING_ENABLED 3731163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3732163953Srrs#endif 3733163953Srrs 3734163953Srrs /* check to see if this chunk required auth, but isn't */ 3735163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3736163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3737163953Srrs stcb->asoc.local_auth_chunks) && 3738163953Srrs !stcb->asoc.authenticated) { 3739163953Srrs /* "silently" ignore */ 3740163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3741163953Srrs goto next_chunk; 3742163953Srrs } 3743163953Srrs switch (ch->chunk_type) { 3744163953Srrs case SCTP_INITIATION: 3745163953Srrs /* must be first and only chunk */ 3746169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 3747163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3748163953Srrs /* We are not interested anymore? */ 3749163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3750163953Srrs /* 3751163953Srrs * collision case where we are 3752163953Srrs * sending to them too 3753163953Srrs */ 3754163953Srrs ; 3755163953Srrs } else { 3756169378Srrs if (locked_tcb) { 3757163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3758169378Srrs } 3759163953Srrs *offset = length; 3760163953Srrs return (NULL); 3761163953Srrs } 3762163953Srrs } 3763163953Srrs if ((num_chunks > 1) || 3764170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3765163953Srrs *offset = length; 3766169378Srrs if (locked_tcb) { 3767163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3768169378Srrs } 3769163953Srrs return (NULL); 3770163953Srrs } 3771163953Srrs if ((stcb != NULL) && 3772163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3773163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3774163953Srrs sctp_send_shutdown_ack(stcb, 3775163953Srrs stcb->asoc.primary_destination); 3776163953Srrs *offset = length; 3777163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3778169378Srrs if (locked_tcb) { 3779163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3780169378Srrs } 3781163953Srrs return (NULL); 3782163953Srrs } 3783169420Srrs if (netp) { 3784169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 3785169420Srrs (struct sctp_init_chunk *)ch, inp, 3786170181Srrs stcb, *netp, &abort_no_unlock, vrf_id); 3787169420Srrs } 3788168299Srrs if (abort_no_unlock) 3789168299Srrs return (NULL); 3790168299Srrs 3791163953Srrs *offset = length; 3792169378Srrs if (locked_tcb) { 3793163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3794169378Srrs } 3795163953Srrs return (NULL); 3796163953Srrs break; 3797170642Srrs case SCTP_PAD_CHUNK: 3798170642Srrs break; 3799163953Srrs case SCTP_INITIATION_ACK: 3800163953Srrs /* must be first and only chunk */ 3801169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 3802163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3803163953Srrs /* We are not interested anymore */ 3804163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3805163953Srrs ; 3806163953Srrs } else { 3807169378Srrs if (locked_tcb) { 3808163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3809169378Srrs } 3810163953Srrs *offset = length; 3811163953Srrs if (stcb) { 3812165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3813163953Srrs } 3814163953Srrs return (NULL); 3815163953Srrs } 3816163953Srrs } 3817163953Srrs if ((num_chunks > 1) || 3818170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3819163953Srrs *offset = length; 3820169378Srrs if (locked_tcb) { 3821163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3822169378Srrs } 3823163953Srrs return (NULL); 3824163953Srrs } 3825169420Srrs if ((netp) && (*netp)) { 3826169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3827170181Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 3828169420Srrs } else { 3829169420Srrs ret = -1; 3830169420Srrs } 3831163953Srrs /* 3832163953Srrs * Special case, I must call the output routine to 3833163953Srrs * get the cookie echoed 3834163953Srrs */ 3835168299Srrs if (abort_no_unlock) 3836168299Srrs return (NULL); 3837168299Srrs 3838163953Srrs if ((stcb) && ret == 0) 3839163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3840163953Srrs *offset = length; 3841169378Srrs if (locked_tcb) { 3842163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3843169378Srrs } 3844163953Srrs return (NULL); 3845163953Srrs break; 3846163953Srrs case SCTP_SELECTIVE_ACK: 3847169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 3848163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3849163953Srrs { 3850163953Srrs struct sctp_sack_chunk *sack; 3851163953Srrs int abort_now = 0; 3852163953Srrs uint32_t a_rwnd, cum_ack; 3853163953Srrs uint16_t num_seg; 3854163953Srrs int nonce_sum_flag; 3855163953Srrs 3856169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 3857169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 3858169208Srrs *offset = length; 3859169378Srrs if (locked_tcb) { 3860169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 3861169378Srrs } 3862169208Srrs return (NULL); 3863169208Srrs } 3864163953Srrs sack = (struct sctp_sack_chunk *)ch; 3865163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3866163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3867163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3868163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3869170606Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 3870170606Srrs cum_ack, 3871170606Srrs num_seg, 3872170606Srrs a_rwnd 3873170606Srrs ); 3874163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3875163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3876163953Srrs (num_seg == 0) && 3877163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3878163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3879163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3880163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3881163953Srrs ) { 3882163953Srrs /* 3883163953Srrs * We have a SIMPLE sack having no 3884163953Srrs * prior segments and data on sent 3885163953Srrs * queue to be acked.. Use the 3886163953Srrs * faster path sack processing. We 3887163953Srrs * also allow window update sacks 3888163953Srrs * with no missing segments to go 3889163953Srrs * this way too. 3890163953Srrs */ 3891169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 3892169208Srrs &abort_now); 3893163953Srrs } else { 3894169420Srrs if (netp && *netp) 3895170781Srrs sctp_handle_sack(m, *offset, 3896170781Srrs sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 3897163953Srrs } 3898163953Srrs if (abort_now) { 3899163953Srrs /* ABORT signal from sack processing */ 3900163953Srrs *offset = length; 3901163953Srrs return (NULL); 3902163953Srrs } 3903163953Srrs } 3904163953Srrs break; 3905163953Srrs case SCTP_HEARTBEAT_REQUEST: 3906169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 3907169420Srrs if ((stcb) && netp && *netp) { 3908169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 3909169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 3910169420Srrs chk_length, *netp); 3911169420Srrs 3912169420Srrs /* He's alive so give him credit */ 3913169420Srrs stcb->asoc.overall_error_count = 0; 3914163953Srrs } 3915163953Srrs break; 3916163953Srrs case SCTP_HEARTBEAT_ACK: 3917169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 3918169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 3919169208Srrs /* Its not ours */ 3920169352Srrs *offset = length; 3921169378Srrs if (locked_tcb) { 3922169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3923169378Srrs } 3924169208Srrs return (NULL); 3925169208Srrs } 3926163953Srrs /* He's alive so give him credit */ 3927163953Srrs stcb->asoc.overall_error_count = 0; 3928163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 3929169420Srrs if (netp && *netp) 3930169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 3931169420Srrs stcb, *netp); 3932163953Srrs break; 3933163953Srrs case SCTP_ABORT_ASSOCIATION: 3934170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 3935170091Srrs stcb); 3936169420Srrs if ((stcb) && netp && *netp) 3937169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 3938169420Srrs stcb, *netp); 3939163953Srrs *offset = length; 3940163953Srrs return (NULL); 3941163953Srrs break; 3942163953Srrs case SCTP_SHUTDOWN: 3943170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 3944170091Srrs stcb); 3945169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 3946169208Srrs *offset = length; 3947169378Srrs if (locked_tcb) { 3948169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3949169378Srrs } 3950169208Srrs return (NULL); 3951169208Srrs 3952169420Srrs } 3953169420Srrs if (netp && *netp) { 3954163953Srrs int abort_flag = 0; 3955163953Srrs 3956163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 3957163953Srrs stcb, *netp, &abort_flag); 3958163953Srrs if (abort_flag) { 3959163953Srrs *offset = length; 3960163953Srrs return (NULL); 3961163953Srrs } 3962163953Srrs } 3963163953Srrs break; 3964163953Srrs case SCTP_SHUTDOWN_ACK: 3965170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 3966169420Srrs if ((stcb) && (netp) && (*netp)) 3967169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 3968163953Srrs *offset = length; 3969163953Srrs return (NULL); 3970163953Srrs break; 3971169420Srrs 3972163953Srrs case SCTP_OPERATION_ERROR: 3973169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 3974169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 3975169420Srrs 3976163953Srrs *offset = length; 3977163953Srrs return (NULL); 3978163953Srrs } 3979163953Srrs break; 3980163953Srrs case SCTP_COOKIE_ECHO: 3981169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3982170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 3983163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3984163953Srrs ; 3985163953Srrs } else { 3986169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3987163953Srrs /* We are not interested anymore */ 3988163953Srrs *offset = length; 3989163953Srrs return (NULL); 3990163953Srrs } 3991163953Srrs } 3992163953Srrs /* 3993163953Srrs * First are we accepting? We do this again here 3994169420Srrs * sincen it is possible that a previous endpoint 3995169420Srrs * WAS listening responded to a INIT-ACK and then 3996163953Srrs * closed. We opened and bound.. and are now no 3997163953Srrs * longer listening. 3998163953Srrs */ 3999163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4000163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4001163953Srrs /* 4002163953Srrs * special case, is this a retran'd 4003163953Srrs * COOKIE-ECHO or a restarting assoc 4004163953Srrs * that is a peeled off or 4005163953Srrs * one-to-one style socket. 4006163953Srrs */ 4007163953Srrs goto process_cookie_anyway; 4008163953Srrs } 4009169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4010170181Srrs sh, NULL, vrf_id); 4011163953Srrs *offset = length; 4012163953Srrs return (NULL); 4013163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4014163953Srrs /* we are accepting so check limits like TCP */ 4015163953Srrs if (inp->sctp_socket->so_qlen > 4016163953Srrs inp->sctp_socket->so_qlimit) { 4017163953Srrs /* no space */ 4018163953Srrs struct mbuf *oper; 4019163953Srrs struct sctp_paramhdr *phdr; 4020163953Srrs 4021163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4022163953Srrs oper = NULL; 4023163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4024165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4025163953Srrs if (oper) { 4026165647Srrs SCTP_BUF_LEN(oper) = 4027163953Srrs sizeof(struct sctp_paramhdr); 4028163953Srrs phdr = mtod(oper, 4029163953Srrs struct sctp_paramhdr *); 4030163953Srrs phdr->param_type = 4031163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4032163953Srrs phdr->param_length = 4033163953Srrs htons(sizeof(struct sctp_paramhdr)); 4034163953Srrs } 4035163953Srrs sctp_abort_association(inp, stcb, m, 4036170181Srrs iphlen, sh, oper, vrf_id); 4037163953Srrs } 4038163953Srrs *offset = length; 4039163953Srrs return (NULL); 4040163953Srrs } 4041163953Srrs } 4042163953Srrs process_cookie_anyway: 4043163953Srrs { 4044163953Srrs struct mbuf *ret_buf; 4045165220Srrs struct sctp_inpcb *linp; 4046163953Srrs 4047169420Srrs if (stcb) { 4048165220Srrs linp = NULL; 4049169420Srrs } else { 4050165220Srrs linp = inp; 4051169420Srrs } 4052165220Srrs 4053169420Srrs if (linp) { 4054165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4055169420Srrs } 4056169420Srrs if (netp) { 4057169420Srrs ret_buf = 4058169420Srrs sctp_handle_cookie_echo(m, iphlen, 4059169420Srrs *offset, sh, 4060169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4061169420Srrs &inp, &stcb, netp, 4062169420Srrs auth_skipped, 4063169420Srrs auth_offset, 4064169420Srrs auth_len, 4065169420Srrs &locked_tcb, 4066170181Srrs vrf_id); 4067169420Srrs } else { 4068169420Srrs ret_buf = NULL; 4069169420Srrs } 4070169420Srrs if (linp) { 4071165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4072169420Srrs } 4073163953Srrs if (ret_buf == NULL) { 4074163953Srrs if (locked_tcb) { 4075163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4076163953Srrs } 4077169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4078169420Srrs "GAK, null buffer\n"); 4079163953Srrs auth_skipped = 0; 4080163953Srrs *offset = length; 4081163953Srrs return (NULL); 4082163953Srrs } 4083163953Srrs /* if AUTH skipped, see if it verified... */ 4084163953Srrs if (auth_skipped) { 4085163953Srrs got_auth = 1; 4086163953Srrs auth_skipped = 0; 4087163953Srrs } 4088163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4089163953Srrs /* 4090163953Srrs * Restart the timer if we have 4091163953Srrs * pending data 4092163953Srrs */ 4093163953Srrs struct sctp_tmit_chunk *chk; 4094163953Srrs 4095163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4096163953Srrs if (chk) { 4097163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4098163953Srrs stcb->sctp_ep, stcb, 4099163953Srrs chk->whoTo); 4100163953Srrs } 4101163953Srrs } 4102163953Srrs } 4103163953Srrs break; 4104163953Srrs case SCTP_COOKIE_ACK: 4105170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4106169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4107169378Srrs if (locked_tcb) { 4108169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4109169378Srrs } 4110169352Srrs return (NULL); 4111169352Srrs } 4112163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4113163953Srrs /* We are not interested anymore */ 4114163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4115163953Srrs ; 4116169420Srrs } else if (stcb) { 4117165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4118163953Srrs *offset = length; 4119163953Srrs return (NULL); 4120163953Srrs } 4121163953Srrs } 4122163953Srrs /* He's alive so give him credit */ 4123169420Srrs if ((stcb) && netp && *netp) { 4124169378Srrs stcb->asoc.overall_error_count = 0; 4125169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4126169378Srrs } 4127163953Srrs break; 4128163953Srrs case SCTP_ECN_ECHO: 4129169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4130163953Srrs /* He's alive so give him credit */ 4131169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4132169208Srrs /* Its not ours */ 4133169378Srrs if (locked_tcb) { 4134169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4135169378Srrs } 4136169208Srrs *offset = length; 4137169208Srrs return (NULL); 4138169208Srrs } 4139169378Srrs if (stcb) { 4140169378Srrs stcb->asoc.overall_error_count = 0; 4141169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4142169378Srrs stcb); 4143169378Srrs } 4144163953Srrs break; 4145163953Srrs case SCTP_ECN_CWR: 4146169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4147163953Srrs /* He's alive so give him credit */ 4148169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4149169208Srrs /* Its not ours */ 4150169378Srrs if (locked_tcb) { 4151169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4152169378Srrs } 4153169208Srrs *offset = length; 4154169208Srrs return (NULL); 4155169208Srrs } 4156169378Srrs if (stcb) { 4157169378Srrs stcb->asoc.overall_error_count = 0; 4158169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4159169378Srrs } 4160163953Srrs break; 4161163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4162170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4163163953Srrs /* must be first and only chunk */ 4164163953Srrs if ((num_chunks > 1) || 4165170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4166163953Srrs *offset = length; 4167169378Srrs if (locked_tcb) { 4168163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4169169378Srrs } 4170163953Srrs return (NULL); 4171163953Srrs } 4172169420Srrs if ((stcb) && netp && *netp) { 4173169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4174169378Srrs stcb, *netp); 4175169378Srrs } 4176163953Srrs *offset = length; 4177163953Srrs return (NULL); 4178163953Srrs break; 4179163953Srrs case SCTP_ASCONF: 4180169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4181163953Srrs /* He's alive so give him credit */ 4182169378Srrs if (stcb) { 4183169378Srrs stcb->asoc.overall_error_count = 0; 4184169378Srrs sctp_handle_asconf(m, *offset, 4185169378Srrs (struct sctp_asconf_chunk *)ch, stcb); 4186169378Srrs } 4187163953Srrs break; 4188163953Srrs case SCTP_ASCONF_ACK: 4189169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4190169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4191169208Srrs /* Its not ours */ 4192169378Srrs if (locked_tcb) { 4193169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4194169378Srrs } 4195169208Srrs *offset = length; 4196169208Srrs return (NULL); 4197169208Srrs } 4198169420Srrs if ((stcb) && netp && *netp) { 4199169378Srrs /* He's alive so give him credit */ 4200169378Srrs stcb->asoc.overall_error_count = 0; 4201169378Srrs sctp_handle_asconf_ack(m, *offset, 4202169378Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4203169378Srrs } 4204163953Srrs break; 4205163953Srrs case SCTP_FORWARD_CUM_TSN: 4206169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4207169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4208169208Srrs /* Its not ours */ 4209169378Srrs if (locked_tcb) { 4210169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4211169378Srrs } 4212169208Srrs *offset = length; 4213169208Srrs return (NULL); 4214169208Srrs } 4215163953Srrs /* He's alive so give him credit */ 4216169378Srrs if (stcb) { 4217163953Srrs int abort_flag = 0; 4218163953Srrs 4219163953Srrs stcb->asoc.overall_error_count = 0; 4220163953Srrs *fwd_tsn_seen = 1; 4221163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4222163953Srrs /* We are not interested anymore */ 4223165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28); 4224163953Srrs *offset = length; 4225163953Srrs return (NULL); 4226163953Srrs } 4227163953Srrs sctp_handle_forward_tsn(stcb, 4228163953Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag); 4229163953Srrs if (abort_flag) { 4230163953Srrs *offset = length; 4231163953Srrs return (NULL); 4232163953Srrs } else { 4233163953Srrs stcb->asoc.overall_error_count = 0; 4234163953Srrs } 4235163953Srrs 4236163953Srrs } 4237163953Srrs break; 4238163953Srrs case SCTP_STREAM_RESET: 4239169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4240169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4241169208Srrs /* Its not ours */ 4242169378Srrs if (locked_tcb) { 4243169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4244169378Srrs } 4245169208Srrs *offset = length; 4246169208Srrs return (NULL); 4247169208Srrs } 4248163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4249163953Srrs /* We are not interested anymore */ 4250165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4251163953Srrs *offset = length; 4252163953Srrs return (NULL); 4253163953Srrs } 4254169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4255169420Srrs /* 4256169420Srrs * hmm, peer should have announced this, but 4257169420Srrs * we will turn it on since he is sending us 4258169420Srrs * a stream reset. 4259169420Srrs */ 4260169420Srrs stcb->asoc.peer_supports_strreset = 1; 4261163953Srrs } 4262169420Srrs if (sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_out_req *)ch)) { 4263169420Srrs /* stop processing */ 4264169420Srrs *offset = length; 4265169420Srrs return (NULL); 4266169420Srrs } 4267163953Srrs break; 4268163953Srrs case SCTP_PACKET_DROPPED: 4269169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4270163953Srrs /* re-get it all please */ 4271169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4272169208Srrs /* Its not ours */ 4273169378Srrs if (locked_tcb) { 4274169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4275169378Srrs } 4276169208Srrs *offset = length; 4277169208Srrs return (NULL); 4278169208Srrs } 4279169420Srrs if (ch && (stcb) && netp && (*netp)) { 4280169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4281170781Srrs stcb, *netp, 4282170781Srrs min(chk_length, (sizeof(chunk_buf) - 4))); 4283170781Srrs 4284169378Srrs } 4285163953Srrs break; 4286163953Srrs 4287163953Srrs case SCTP_AUTHENTICATION: 4288169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4289163953Srrs if (sctp_auth_disable) 4290163953Srrs goto unknown_chunk; 4291163953Srrs 4292163953Srrs if (stcb == NULL) { 4293163953Srrs /* save the first AUTH for later processing */ 4294163953Srrs if (auth_skipped == 0) { 4295163953Srrs auth_offset = *offset; 4296163953Srrs auth_len = chk_length; 4297163953Srrs auth_skipped = 1; 4298163953Srrs } 4299163953Srrs /* skip this chunk (temporarily) */ 4300163953Srrs goto next_chunk; 4301163953Srrs } 4302169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4303169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4304169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4305169208Srrs /* Its not ours */ 4306169378Srrs if (locked_tcb) { 4307169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4308169378Srrs } 4309169208Srrs *offset = length; 4310169208Srrs return (NULL); 4311169208Srrs } 4312163953Srrs if (got_auth == 1) { 4313163953Srrs /* skip this chunk... it's already auth'd */ 4314163953Srrs goto next_chunk; 4315163953Srrs } 4316163953Srrs got_auth = 1; 4317169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4318163953Srrs m, *offset)) { 4319163953Srrs /* auth HMAC failed so dump the packet */ 4320163953Srrs *offset = length; 4321163953Srrs return (stcb); 4322163953Srrs } else { 4323163953Srrs /* remaining chunks are HMAC checked */ 4324163953Srrs stcb->asoc.authenticated = 1; 4325163953Srrs } 4326163953Srrs break; 4327163953Srrs 4328163953Srrs default: 4329163953Srrs unknown_chunk: 4330163953Srrs /* it's an unknown chunk! */ 4331163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4332163953Srrs struct mbuf *mm; 4333163953Srrs struct sctp_paramhdr *phd; 4334163953Srrs 4335163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4336165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4337163953Srrs if (mm) { 4338163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4339163953Srrs /* 4340163953Srrs * We cheat and use param type since 4341163953Srrs * we did not bother to define a 4342163953Srrs * error cause struct. They are the 4343163953Srrs * same basic format with different 4344163953Srrs * names. 4345163953Srrs */ 4346163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4347163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4348165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4349166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4350163953Srrs M_DONTWAIT); 4351165647Srrs if (SCTP_BUF_NEXT(mm)) { 4352163953Srrs sctp_queue_op_err(stcb, mm); 4353163953Srrs } else { 4354163953Srrs sctp_m_freem(mm); 4355163953Srrs } 4356163953Srrs } 4357163953Srrs } 4358163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4359163953Srrs /* discard this packet */ 4360163953Srrs *offset = length; 4361163953Srrs return (stcb); 4362163953Srrs } /* else skip this bad chunk and continue... */ 4363163953Srrs break; 4364163953Srrs } /* switch (ch->chunk_type) */ 4365163953Srrs 4366163953Srrs 4367163953Srrsnext_chunk: 4368163953Srrs /* get the next chunk */ 4369163953Srrs *offset += SCTP_SIZE32(chk_length); 4370163953Srrs if (*offset >= length) { 4371163953Srrs /* no more data left in the mbuf chain */ 4372163953Srrs break; 4373163953Srrs } 4374163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4375163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4376163953Srrs if (ch == NULL) { 4377169378Srrs if (locked_tcb) { 4378163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4379169378Srrs } 4380163953Srrs *offset = length; 4381163953Srrs return (NULL); 4382163953Srrs } 4383163953Srrs } /* while */ 4384163953Srrs return (stcb); 4385163953Srrs} 4386163953Srrs 4387163953Srrs 4388163953Srrs/* 4389163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4390163953Srrs * ECN(0) or ECN(1) or CE 4391163953Srrs */ 4392163953Srrsstatic __inline void 4393163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4394163953Srrs uint8_t ecn_bits) 4395163953Srrs{ 4396163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4397163953Srrs ; 4398163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4399163953Srrs /* 4400163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4401163953Srrs * change the NS bit (that we have yet to find a way to send 4402163953Srrs * it yet). 4403163953Srrs */ 4404163953Srrs 4405163953Srrs /* ECN Nonce stuff */ 4406163953Srrs stcb->asoc.receiver_nonce_sum++; 4407163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4408163953Srrs 4409163953Srrs /* 4410163953Srrs * Drag up the last_echo point if cumack is larger since we 4411163953Srrs * don't want the point falling way behind by more than 4412163953Srrs * 2^^31 and then having it be incorrect. 4413163953Srrs */ 4414163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4415163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4416163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4417163953Srrs } 4418163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4419163953Srrs /* 4420163953Srrs * Drag up the last_echo point if cumack is larger since we 4421163953Srrs * don't want the point falling way behind by more than 4422163953Srrs * 2^^31 and then having it be incorrect. 4423163953Srrs */ 4424163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4425163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4426163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4427163953Srrs } 4428163953Srrs } 4429163953Srrs} 4430163953Srrs 4431163953Srrsstatic __inline void 4432163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4433163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4434163953Srrs{ 4435163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4436163953Srrs /* 4437163953Srrs * we possibly must notify the sender that a congestion 4438163953Srrs * window reduction is in order. We do this by adding a ECNE 4439163953Srrs * chunk to the output chunk queue. The incoming CWR will 4440163953Srrs * remove this chunk. 4441163953Srrs */ 4442163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4443163953Srrs MAX_TSN)) { 4444163953Srrs /* Yep, we need to add a ECNE */ 4445163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4446163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4447163953Srrs } 4448163953Srrs } 4449163953Srrs} 4450163953Srrs 4451163953Srrs/* 4452163953Srrs * common input chunk processing (v4 and v6) 4453163953Srrs */ 4454169378Srrsvoid 4455163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4456163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4457163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4458170181Srrs uint8_t ecn_bits, uint32_t vrf_id) 4459163953Srrs{ 4460163953Srrs /* 4461163953Srrs * Control chunk processing 4462163953Srrs */ 4463163953Srrs uint32_t high_tsn; 4464163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4465163953Srrs struct mbuf *m = *mm; 4466163953Srrs int abort_flag = 0; 4467163953Srrs int un_sent; 4468163953Srrs 4469163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4470163953Srrs#ifdef SCTP_AUDITING_ENABLED 4471163953Srrs sctp_audit_log(0xE0, 1); 4472163953Srrs sctp_auditing(0, inp, stcb, net); 4473163953Srrs#endif 4474163953Srrs 4475169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4476169420Srrs m, iphlen, offset); 4477163953Srrs 4478163953Srrs if (stcb) { 4479163953Srrs /* always clear this before beginning a packet */ 4480163953Srrs stcb->asoc.authenticated = 0; 4481163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4482163953Srrs } 4483163953Srrs if (IS_SCTP_CONTROL(ch)) { 4484163953Srrs /* process the control portion of the SCTP packet */ 4485169655Srrs /* sa_ignore NO_NULL_CHK */ 4486163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4487170181Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id); 4488163953Srrs if (stcb) { 4489163953Srrs /* 4490163953Srrs * This covers us if the cookie-echo was there and 4491163953Srrs * it changes our INP. 4492163953Srrs */ 4493163953Srrs inp = stcb->sctp_ep; 4494163953Srrs } 4495163953Srrs } else { 4496163953Srrs /* 4497163953Srrs * no control chunks, so pre-process DATA chunks (these 4498163953Srrs * checks are taken care of by control processing) 4499163953Srrs */ 4500163953Srrs 4501163953Srrs /* 4502163953Srrs * if DATA only packet, and auth is required, then punt... 4503163953Srrs * can't have authenticated without any AUTH (control) 4504163953Srrs * chunks 4505163953Srrs */ 4506163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4507163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4508163953Srrs stcb->asoc.local_auth_chunks)) { 4509163953Srrs /* "silently" ignore */ 4510163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4511163953Srrs SCTP_TCB_UNLOCK(stcb); 4512169378Srrs return; 4513163953Srrs } 4514163953Srrs if (stcb == NULL) { 4515163953Srrs /* out of the blue DATA chunk */ 4516169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4517170181Srrs vrf_id); 4518169378Srrs return; 4519163953Srrs } 4520163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4521163953Srrs /* v_tag mismatch! */ 4522163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4523163953Srrs SCTP_TCB_UNLOCK(stcb); 4524169378Srrs return; 4525163953Srrs } 4526163953Srrs } 4527163953Srrs 4528163953Srrs if (stcb == NULL) { 4529163953Srrs /* 4530163953Srrs * no valid TCB for this packet, or we found it's a bad 4531163953Srrs * packet while processing control, or we're done with this 4532163953Srrs * packet (done or skip rest of data), so we drop it... 4533163953Srrs */ 4534169378Srrs return; 4535163953Srrs } 4536163953Srrs /* 4537163953Srrs * DATA chunk processing 4538163953Srrs */ 4539163953Srrs /* plow through the data chunks while length > offset */ 4540163953Srrs 4541163953Srrs /* 4542163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4543163953Srrs * DATA is required. 4544163953Srrs */ 4545165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4546163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4547163953Srrs stcb->asoc.local_auth_chunks) && 4548163953Srrs !stcb->asoc.authenticated) { 4549163953Srrs /* "silently" ignore */ 4550163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4551169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 4552169420Srrs "Data chunk requires AUTH, skipped\n"); 4553165220Srrs goto trigger_send; 4554163953Srrs } 4555163953Srrs if (length > offset) { 4556163953Srrs int retval; 4557163953Srrs 4558163953Srrs /* 4559163953Srrs * First check to make sure our state is correct. We would 4560163953Srrs * not get here unless we really did have a tag, so we don't 4561163953Srrs * abort if this happens, just dump the chunk silently. 4562163953Srrs */ 4563163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4564163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4565163953Srrs /* 4566163953Srrs * we consider data with valid tags in this state 4567163953Srrs * shows us the cookie-ack was lost. Imply it was 4568163953Srrs * there. 4569163953Srrs */ 4570163953Srrs stcb->asoc.overall_error_count = 0; 4571163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4572163953Srrs break; 4573163953Srrs case SCTP_STATE_COOKIE_WAIT: 4574163953Srrs /* 4575163953Srrs * We consider OOTB any data sent during asoc setup. 4576163953Srrs */ 4577169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4578170181Srrs vrf_id); 4579163953Srrs SCTP_TCB_UNLOCK(stcb); 4580169378Srrs return; 4581163953Srrs break; 4582163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4583163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4584163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4585163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4586163953Srrs default: 4587163953Srrs SCTP_TCB_UNLOCK(stcb); 4588169378Srrs return; 4589163953Srrs break; 4590163953Srrs case SCTP_STATE_OPEN: 4591163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4592163953Srrs break; 4593163953Srrs } 4594163953Srrs /* take care of ECN, part 1. */ 4595163953Srrs if (stcb->asoc.ecn_allowed && 4596163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4597163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4598163953Srrs } 4599163953Srrs /* plow through the data chunks while length > offset */ 4600163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4601163953Srrs inp, stcb, net, &high_tsn); 4602163953Srrs if (retval == 2) { 4603163953Srrs /* 4604163953Srrs * The association aborted, NO UNLOCK needed since 4605163953Srrs * the association is destroyed. 4606163953Srrs */ 4607169378Srrs return; 4608163953Srrs } 4609163953Srrs data_processed = 1; 4610163953Srrs if (retval == 0) { 4611163953Srrs /* take care of ecn part 2. */ 4612163953Srrs if (stcb->asoc.ecn_allowed && 4613163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4614163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4615163953Srrs ecn_bits); 4616163953Srrs } 4617163953Srrs } 4618163953Srrs /* 4619163953Srrs * Anything important needs to have been m_copy'ed in 4620163953Srrs * process_data 4621163953Srrs */ 4622163953Srrs } 4623163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4624163953Srrs int was_a_gap = 0; 4625163953Srrs 4626163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4627163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4628163953Srrs /* there was a gap before this data was processed */ 4629163953Srrs was_a_gap = 1; 4630163953Srrs } 4631163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4632163953Srrs if (abort_flag) { 4633163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4634169378Srrs return; 4635163953Srrs } 4636163953Srrs } 4637163953Srrs /* trigger send of any chunks in queue... */ 4638165220Srrstrigger_send: 4639163953Srrs#ifdef SCTP_AUDITING_ENABLED 4640163953Srrs sctp_audit_log(0xE0, 2); 4641163953Srrs sctp_auditing(1, inp, stcb, net); 4642163953Srrs#endif 4643169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4644169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 4645169420Srrs stcb->asoc.peers_rwnd, 4646169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4647169420Srrs stcb->asoc.total_flight); 4648163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4649163953Srrs 4650163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4651163953Srrs ((un_sent) && 4652163953Srrs (stcb->asoc.peers_rwnd > 0 || 4653163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4654169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 4655163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4656169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 4657163953Srrs } 4658163953Srrs#ifdef SCTP_AUDITING_ENABLED 4659163953Srrs sctp_audit_log(0xE0, 3); 4660163953Srrs sctp_auditing(2, inp, stcb, net); 4661163953Srrs#endif 4662163953Srrs SCTP_TCB_UNLOCK(stcb); 4663169378Srrs return; 4664163953Srrs} 4665163953Srrs 4666163953Srrs 4667163953Srrs 4668163953Srrsvoid 4669165647Srrssctp_input(i_pak, off) 4670165647Srrs struct mbuf *i_pak; 4671163953Srrs int off; 4672163953Srrs 4673163953Srrs{ 4674163953Srrs#ifdef SCTP_MBUF_LOGGING 4675163953Srrs struct mbuf *mat; 4676163953Srrs 4677163953Srrs#endif 4678165647Srrs struct mbuf *m; 4679163953Srrs int iphlen; 4680170181Srrs uint32_t vrf_id = 0; 4681163953Srrs uint8_t ecn_bits; 4682163953Srrs struct ip *ip; 4683163953Srrs struct sctphdr *sh; 4684163953Srrs struct sctp_inpcb *inp = NULL; 4685163953Srrs 4686163953Srrs uint32_t check, calc_check; 4687163953Srrs struct sctp_nets *net; 4688163953Srrs struct sctp_tcb *stcb = NULL; 4689163953Srrs struct sctp_chunkhdr *ch; 4690163953Srrs int refcount_up = 0; 4691163953Srrs int length, mlen, offset; 4692163953Srrs 4693169352Srrs 4694169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 4695169352Srrs SCTP_RELEASE_PKT(i_pak); 4696169352Srrs return; 4697169352Srrs } 4698168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 4699163953Srrs iphlen = off; 4700165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4701170056Srrs 4702163953Srrs net = NULL; 4703163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4704163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4705163953Srrs 4706170091Srrs 4707163953Srrs#ifdef SCTP_MBUF_LOGGING 4708163953Srrs /* Log in any input mbufs */ 4709170744Srrs if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) { 4710170744Srrs mat = m; 4711170744Srrs while (mat) { 4712170744Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4713170744Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4714170744Srrs } 4715170744Srrs mat = SCTP_BUF_NEXT(mat); 4716163953Srrs } 4717163953Srrs } 4718163953Srrs#endif 4719170091Srrs#ifdef SCTP_PACKET_LOGGING 4720170091Srrs sctp_packet_log(m, mlen); 4721170091Srrs#endif 4722170091Srrs /* 4723170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 4724170091Srrs * case) 4725170091Srrs */ 4726170091Srrs mlen -= iphlen; 4727168124Srrs 4728163953Srrs /* 4729163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4730163953Srrs */ 4731163953Srrs ip = mtod(m, struct ip *); 4732163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4733165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4734163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4735163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4736163953Srrs return; 4737163953Srrs } 4738163953Srrs ip = mtod(m, struct ip *); 4739163953Srrs } 4740163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4741163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4742163953Srrs 4743163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4744163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4745163953Srrs goto bad; 4746163953Srrs } 4747168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4748163953Srrs /* 4749163953Srrs * We only look at broadcast if its a front state, All 4750163953Srrs * others we will not have a tcb for anyway. 4751163953Srrs */ 4752163953Srrs goto bad; 4753163953Srrs } 4754163953Srrs /* validate SCTP checksum */ 4755168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4756163953Srrs /* 4757163953Srrs * we do NOT validate things from the loopback if the sysctl 4758163953Srrs * is set to 1. 4759163953Srrs */ 4760163953Srrs check = sh->checksum; /* save incoming checksum */ 4761163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4762163953Srrs /* 4763163953Srrs * special hook for where we got a local address 4764163953Srrs * somehow routed across a non IFT_LOOP type 4765163953Srrs * interface 4766163953Srrs */ 4767163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4768163953Srrs goto sctp_skip_csum_4; 4769163953Srrs } 4770163953Srrs sh->checksum = 0; /* prepare for calc */ 4771163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4772163953Srrs if (calc_check != check) { 4773169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4774169420Srrs calc_check, check, m, mlen, iphlen); 4775163953Srrs 4776163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4777163953Srrs offset - sizeof(*ch), 4778169420Srrs sh, ch, &inp, &net, 4779169420Srrs vrf_id); 4780163953Srrs if ((inp) && (stcb)) { 4781163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4782163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4783163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4784163953Srrs refcount_up = 1; 4785163953Srrs } 4786163953Srrs SCTP_STAT_INCR(sctps_badsum); 4787163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4788163953Srrs goto bad; 4789163953Srrs } 4790163953Srrs sh->checksum = calc_check; 4791168299Srrs } 4792163953Srrssctp_skip_csum_4: 4793168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4794168124Srrs if (sh->dest_port == 0) { 4795168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4796168124Srrs goto bad; 4797168124Srrs } 4798163953Srrs /* validate mbuf chain length with IP payload length */ 4799163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4800163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4801163953Srrs goto bad; 4802163953Srrs } 4803163953Srrs /* 4804163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4805163953Srrs * IP/SCTP/first chunk header... 4806163953Srrs */ 4807163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4808168299Srrs sh, ch, &inp, &net, vrf_id); 4809163953Srrs /* inp's ref-count increased && stcb locked */ 4810163953Srrs if (inp == NULL) { 4811163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4812163953Srrs 4813163953Srrs SCTP_STAT_INCR(sctps_noport); 4814163953Srrs#ifdef ICMP_BANDLIM 4815163953Srrs /* 4816163953Srrs * we use the bandwidth limiting to protect against sending 4817163953Srrs * too many ABORTS all at once. In this case these count the 4818163953Srrs * same as an ICMP message. 4819163953Srrs */ 4820163953Srrs if (badport_bandlim(0) < 0) 4821163953Srrs goto bad; 4822163953Srrs#endif /* ICMP_BANDLIM */ 4823169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4824169420Srrs "Sending a ABORT from packet entry!\n"); 4825163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4826163953Srrs /* 4827163953Srrs * we do a trick here to get the INIT tag, dig in 4828163953Srrs * and get the tag from the INIT and put it in the 4829163953Srrs * common header. 4830163953Srrs */ 4831163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4832163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4833163953Srrs (uint8_t *) & chunk_buf); 4834163953Srrs if (init_chk != NULL) 4835163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4836163953Srrs } 4837165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4838170181Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id); 4839165220Srrs goto bad; 4840165220Srrs } 4841165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4842165220Srrs goto bad; 4843165220Srrs } 4844165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4845170181Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id); 4846163953Srrs goto bad; 4847163953Srrs } else if (stcb == NULL) { 4848163953Srrs refcount_up = 1; 4849163953Srrs } 4850163953Srrs#ifdef IPSEC 4851163953Srrs /* 4852163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 4853163953Srrs * idea, so I will leave it in place. 4854163953Srrs */ 4855163953Srrs 4856163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 4857163953Srrs ipsecstat.in_polvio++; 4858163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4859163953Srrs goto bad; 4860163953Srrs } 4861163953Srrs#endif /* IPSEC */ 4862163953Srrs 4863163953Srrs /* 4864163953Srrs * common chunk processing 4865163953Srrs */ 4866163953Srrs length = ip->ip_len + iphlen; 4867163953Srrs offset -= sizeof(struct sctp_chunkhdr); 4868163953Srrs 4869163953Srrs ecn_bits = ip->ip_tos; 4870163953Srrs 4871169655Srrs /* sa_ignore NO_NULL_CHK */ 4872163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 4873170181Srrs inp, stcb, net, ecn_bits, vrf_id); 4874163953Srrs /* inp's ref-count reduced && stcb unlocked */ 4875163953Srrs if (m) { 4876163953Srrs sctp_m_freem(m); 4877163953Srrs } 4878163953Srrs if ((inp) && (refcount_up)) { 4879163953Srrs /* reduce ref-count */ 4880163953Srrs SCTP_INP_WLOCK(inp); 4881163953Srrs SCTP_INP_DECR_REF(inp); 4882163953Srrs SCTP_INP_WUNLOCK(inp); 4883163953Srrs } 4884163953Srrs return; 4885163953Srrsbad: 4886169420Srrs if (stcb) { 4887163953Srrs SCTP_TCB_UNLOCK(stcb); 4888169420Srrs } 4889163953Srrs if ((inp) && (refcount_up)) { 4890163953Srrs /* reduce ref-count */ 4891163953Srrs SCTP_INP_WLOCK(inp); 4892163953Srrs SCTP_INP_DECR_REF(inp); 4893163953Srrs SCTP_INP_WUNLOCK(inp); 4894163953Srrs } 4895163953Srrs if (m) { 4896163953Srrs sctp_m_freem(m); 4897163953Srrs } 4898163953Srrs return; 4899163953Srrs} 4900