sctp_input.c revision 170140
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 170140 2007-05-30 22:34:21Z 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, 81169352Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, 82169352Srrs uint32_t table_id) 83163953Srrs{ 84163953Srrs struct sctp_init *init; 85163953Srrs struct mbuf *op_err; 86163953Srrs uint32_t init_limit; 87163953Srrs 88169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 89169420Srrs stcb); 90163953Srrs op_err = NULL; 91163953Srrs init = &cp->init; 92163953Srrs /* First are we accepting? */ 93163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 94169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 95169420Srrs "sctp_handle_init: Abort, so_qlimit:%d\n", 96169420Srrs inp->sctp_socket->so_qlimit); 97163953Srrs /* 98163953Srrs * FIX ME ?? What about TCP model and we have a 99163953Srrs * match/restart case? 100163953Srrs */ 101169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 102169352Srrs vrf_id, table_id); 103168299Srrs if (stcb) 104168299Srrs *abort_no_unlock = 1; 105163953Srrs return; 106163953Srrs } 107163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 108163953Srrs /* Invalid length */ 109163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 110169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 111169352Srrs vrf_id, table_id); 112168299Srrs if (stcb) 113168299Srrs *abort_no_unlock = 1; 114163953Srrs return; 115163953Srrs } 116163953Srrs /* validate parameters */ 117163953Srrs if (init->initiate_tag == 0) { 118163953Srrs /* protocol error... send abort */ 119163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 120169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121169352Srrs vrf_id, table_id); 122168299Srrs if (stcb) 123168299Srrs *abort_no_unlock = 1; 124163953Srrs return; 125163953Srrs } 126163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 127163953Srrs /* invalid parameter... send abort */ 128163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 129169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 130169352Srrs vrf_id, table_id); 131163953Srrs return; 132163953Srrs } 133163953Srrs if (init->num_inbound_streams == 0) { 134163953Srrs /* protocol error... send abort */ 135163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 136169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 137169352Srrs vrf_id, table_id); 138168299Srrs if (stcb) 139168299Srrs *abort_no_unlock = 1; 140163953Srrs return; 141163953Srrs } 142163953Srrs if (init->num_outbound_streams == 0) { 143163953Srrs /* protocol error... send abort */ 144163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 145169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 146169352Srrs vrf_id, table_id); 147168299Srrs if (stcb) 148168299Srrs *abort_no_unlock = 1; 149163953Srrs return; 150163953Srrs } 151163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 152163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 153163953Srrs init_limit)) { 154163953Srrs /* auth parameter(s) error... send abort */ 155169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, 156169352Srrs table_id); 157168299Srrs if (stcb) 158168299Srrs *abort_no_unlock = 1; 159163953Srrs return; 160163953Srrs } 161163953Srrs /* send an INIT-ACK w/cookie */ 162169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 163169352Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, 164169352Srrs table_id); 165163953Srrs} 166163953Srrs 167163953Srrs/* 168163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 169163953Srrs */ 170163953Srrsstatic int 171163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 172163953Srrs struct sctp_nets *net) 173163953Srrs{ 174163953Srrs struct sctp_init *init; 175163953Srrs struct sctp_association *asoc; 176163953Srrs struct sctp_nets *lnet; 177163953Srrs unsigned int i; 178163953Srrs 179163953Srrs init = &cp->init; 180163953Srrs asoc = &stcb->asoc; 181163953Srrs /* save off parameters */ 182163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 183163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 184163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 185163953Srrs /* update any ssthresh's that may have a default */ 186163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 187163953Srrs lnet->ssthresh = asoc->peers_rwnd; 188163953Srrs 189163953Srrs#if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING) 190163953Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 191163953Srrs#endif 192163953Srrs 193163953Srrs } 194163953Srrs } 195164181Srrs SCTP_TCB_SEND_LOCK(stcb); 196163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 197163953Srrs unsigned int newcnt; 198163953Srrs struct sctp_stream_out *outs; 199163953Srrs struct sctp_stream_queue_pending *sp; 200163953Srrs 201163953Srrs /* cut back on number of streams */ 202163953Srrs newcnt = ntohs(init->num_inbound_streams); 203163953Srrs /* This if is probably not needed but I am cautious */ 204163953Srrs if (asoc->strmout) { 205163953Srrs /* First make sure no data chunks are trapped */ 206163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 207163953Srrs outs = &asoc->strmout[i]; 208163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 209163953Srrs while (sp) { 210163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 211163953Srrs next); 212163953Srrs asoc->stream_queue_cnt--; 213163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 214163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 215163953Srrs sp); 216163953Srrs if (sp->data) { 217163953Srrs sctp_m_freem(sp->data); 218163953Srrs sp->data = NULL; 219163953Srrs } 220163953Srrs sctp_free_remote_addr(sp->net); 221163953Srrs sp->net = NULL; 222163953Srrs /* Free the chunk */ 223169420Srrs SCTP_PRINTF("sp:%p tcb:%p weird free case\n", 224163979Sru sp, stcb); 225163953Srrs 226163953Srrs sctp_free_a_strmoq(stcb, sp); 227169655Srrs /* sa_ignore FREED_MEMORY */ 228163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 229163953Srrs } 230163953Srrs } 231163953Srrs } 232163953Srrs /* cut back the count and abandon the upper streams */ 233163953Srrs asoc->pre_open_streams = newcnt; 234163953Srrs } 235164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 236163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 237163953Srrs /* init tsn's */ 238163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 239163953Srrs#ifdef SCTP_MAP_LOGGING 240163953Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 241163953Srrs#endif 242163953Srrs /* This is the next one we expect */ 243163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 244163953Srrs 245163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 246163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 247163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 248163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 249163953Srrs /* open the requested streams */ 250170091Srrs 251163953Srrs if (asoc->strmin != NULL) { 252163953Srrs /* Free the old ones */ 253164181Srrs struct sctp_queued_to_read *ctl; 254164181Srrs 255164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 256164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 257164181Srrs while (ctl) { 258164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 259164181Srrs sctp_free_remote_addr(ctl->whoFrom); 260164181Srrs sctp_m_freem(ctl->data); 261164181Srrs ctl->data = NULL; 262164181Srrs sctp_free_a_readq(stcb, ctl); 263164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 264164181Srrs } 265164181Srrs } 266170091Srrs SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 267163953Srrs } 268164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 269164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 270164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 271164181Srrs } 272163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 273170091Srrs sizeof(struct sctp_stream_in), SCTP_M_STRMI); 274163953Srrs if (asoc->strmin == NULL) { 275163953Srrs /* we didn't get memory for the streams! */ 276169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 277163953Srrs return (-1); 278163953Srrs } 279163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 280163953Srrs asoc->strmin[i].stream_no = i; 281163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 282163953Srrs /* 283163953Srrs * U-stream ranges will be set when the cookie is unpacked. 284163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 285163953Srrs * supported) when the INIT-ACK arrives. 286163953Srrs */ 287163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 288168943Srrs asoc->strmin[i].delivery_started = 0; 289163953Srrs } 290163953Srrs /* 291163953Srrs * load_address_from_init will put the addresses into the 292163953Srrs * association when the COOKIE is processed or the INIT-ACK is 293163953Srrs * processed. Both types of COOKIE's existing and new call this 294163953Srrs * routine. It will remove addresses that are no longer in the 295163953Srrs * association (for the restarting case where addresses are 296163953Srrs * removed). Up front when the INIT arrives we will discard it if it 297163953Srrs * is a restart and new addresses have been added. 298163953Srrs */ 299169655Srrs /* sa_ignore MEMLEAK */ 300163953Srrs return (0); 301163953Srrs} 302163953Srrs 303163953Srrs/* 304163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 305163953Srrs */ 306163953Srrsstatic int 307163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 308163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 309169352Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, 310169352Srrs uint32_t table_id) 311163953Srrs{ 312163953Srrs struct sctp_association *asoc; 313163953Srrs struct mbuf *op_err; 314163953Srrs int retval, abort_flag; 315163953Srrs uint32_t initack_limit; 316163953Srrs 317163953Srrs /* First verify that we have no illegal param's */ 318163953Srrs abort_flag = 0; 319163953Srrs op_err = NULL; 320163953Srrs 321163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 322163953Srrs (offset + sizeof(struct sctp_init_chunk)), 323163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 324163953Srrs if (abort_flag) { 325163953Srrs /* Send an abort and notify peer */ 326170056Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err); 327170056Srrs *abort_no_unlock = 1; 328163953Srrs return (-1); 329163953Srrs } 330163953Srrs asoc = &stcb->asoc; 331163953Srrs /* process the peer's parameters in the INIT-ACK */ 332163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 333163953Srrs if (retval < 0) { 334163953Srrs return (retval); 335163953Srrs } 336163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 337163953Srrs /* load all addresses */ 338163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 339163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 340163953Srrs NULL))) { 341163953Srrs /* Huh, we should abort */ 342169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 343169420Srrs "Load addresses from INIT causes an abort %d\n", 344169420Srrs retval); 345163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 346169352Srrs NULL, 0, 0); 347168299Srrs *abort_no_unlock = 1; 348163953Srrs return (-1); 349163953Srrs } 350163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 351163953Srrs stcb->asoc.local_hmacs); 352163953Srrs if (op_err) { 353163953Srrs sctp_queue_op_err(stcb, op_err); 354163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 355163953Srrs op_err = NULL; 356163953Srrs } 357163953Srrs /* extract the cookie and queue it to "echo" it back... */ 358163953Srrs stcb->asoc.overall_error_count = 0; 359163953Srrs net->error_count = 0; 360163953Srrs 361163953Srrs /* 362163953Srrs * Cancel the INIT timer, We do this first before queueing the 363163953Srrs * cookie. We always cancel at the primary to assue that we are 364163953Srrs * canceling the timer started by the INIT which always goes to the 365163953Srrs * primary. 366163953Srrs */ 367163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 368165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 369163953Srrs 370165220Srrs /* calculate the RTO */ 371165220Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered); 372165220Srrs 373163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 374163953Srrs if (retval < 0) { 375163953Srrs /* 376163953Srrs * No cookie, we probably should send a op error. But in any 377163953Srrs * case if there is no cookie in the INIT-ACK, we can 378163953Srrs * abandon the peer, its broke. 379163953Srrs */ 380163953Srrs if (retval == -3) { 381163953Srrs /* We abort with an error of missing mandatory param */ 382163953Srrs op_err = 383163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 384163953Srrs if (op_err) { 385163953Srrs /* 386163953Srrs * Expand beyond to include the mandatory 387163953Srrs * param cookie 388163953Srrs */ 389163953Srrs struct sctp_inv_mandatory_param *mp; 390163953Srrs 391165647Srrs SCTP_BUF_LEN(op_err) = 392163953Srrs sizeof(struct sctp_inv_mandatory_param); 393163953Srrs mp = mtod(op_err, 394163953Srrs struct sctp_inv_mandatory_param *); 395163953Srrs /* Subtract the reserved param */ 396163953Srrs mp->length = 397163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 398163953Srrs mp->num_param = htonl(1); 399163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 400163953Srrs mp->resv = 0; 401163953Srrs } 402163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 403169352Srrs sh, op_err, 0, 0); 404168299Srrs *abort_no_unlock = 1; 405163953Srrs } 406163953Srrs return (retval); 407163953Srrs } 408163953Srrs return (0); 409163953Srrs} 410163953Srrs 411163953Srrsstatic void 412163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 413163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 414163953Srrs{ 415163953Srrs struct sockaddr_storage store; 416163953Srrs struct sockaddr_in *sin; 417163953Srrs struct sockaddr_in6 *sin6; 418163953Srrs struct sctp_nets *r_net; 419163953Srrs struct timeval tv; 420163953Srrs 421163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 422163953Srrs /* Invalid length */ 423163953Srrs return; 424163953Srrs } 425163953Srrs sin = (struct sockaddr_in *)&store; 426163953Srrs sin6 = (struct sockaddr_in6 *)&store; 427163953Srrs 428163953Srrs memset(&store, 0, sizeof(store)); 429163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 430163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 431163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 432163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 433163953Srrs sin->sin_port = stcb->rport; 434163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 435163953Srrs sizeof(sin->sin_addr)); 436163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 437163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 438163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 439163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 440163953Srrs sin6->sin6_port = stcb->rport; 441163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 442163953Srrs sizeof(sin6->sin6_addr)); 443163953Srrs } else { 444163953Srrs return; 445163953Srrs } 446163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 447163953Srrs if (r_net == NULL) { 448169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 449163953Srrs return; 450163953Srrs } 451163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 452163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 453163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 454163953Srrs /* 455163953Srrs * If the its a HB and it's random value is correct when can 456163953Srrs * confirm the destination. 457163953Srrs */ 458163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 459167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 460167598Srrs stcb->asoc.primary_destination = r_net; 461167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 462167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 463167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 464167598Srrs if (r_net != stcb->asoc.primary_destination) { 465167598Srrs /* 466167598Srrs * first one on the list is NOT the primary 467167598Srrs * sctp_cmpaddr() is much more efficent if 468167598Srrs * the primary is the first on the list, 469167598Srrs * make it so. 470167598Srrs */ 471167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 472167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 473167598Srrs } 474167598Srrs } 475163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 476163953Srrs stcb, 0, (void *)r_net); 477163953Srrs } 478163953Srrs r_net->error_count = 0; 479163953Srrs r_net->hb_responded = 1; 480163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 481163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 482163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 483163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 484163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 485163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 486163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 487163953Srrs /* now was it the primary? if so restore */ 488163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 489169420Srrs (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 490163953Srrs } 491163953Srrs } 492163953Srrs /* Now lets do a RTO with this */ 493163953Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv); 494163953Srrs} 495163953Srrs 496163953Srrsstatic void 497163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 498163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 499163953Srrs{ 500169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 501163953Srrs if (stcb == NULL) 502163953Srrs return; 503163953Srrs 504163953Srrs /* stop any receive timers */ 505165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 506163953Srrs /* notify user of the abort and clean up... */ 507163953Srrs sctp_abort_notification(stcb, 0); 508163953Srrs /* free the tcb */ 509163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 510163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 511163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 512163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 513163953Srrs } 514168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 515168859Srrs sctp_print_out_track_log(stcb); 516168859Srrs#endif 517165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 518169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 519163953Srrs} 520163953Srrs 521163953Srrsstatic void 522163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 523163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 524163953Srrs{ 525163953Srrs struct sctp_association *asoc; 526163953Srrs int some_on_streamwheel; 527163953Srrs 528169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 529169420Srrs "sctp_handle_shutdown: handling SHUTDOWN\n"); 530163953Srrs if (stcb == NULL) 531163953Srrs return; 532165220Srrs asoc = &stcb->asoc; 533165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 534165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 535163953Srrs return; 536163953Srrs } 537163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 538163953Srrs /* Shutdown NOT the expected size */ 539163953Srrs return; 540163953Srrs } else { 541163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 542163953Srrs } 543165220Srrs if (asoc->control_pdapi) { 544163953Srrs /* 545163953Srrs * With a normal shutdown we assume the end of last record. 546163953Srrs */ 547163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 548165220Srrs asoc->control_pdapi->end_added = 1; 549165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 550165220Srrs asoc->control_pdapi = NULL; 551163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 552163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 553163953Srrs } 554163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 555163953Srrs if (stcb->sctp_socket) { 556163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 557170056Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 558163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 559163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED; 560163953Srrs /* 561163953Srrs * notify upper layer that peer has initiated a 562163953Srrs * shutdown 563163953Srrs */ 564163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 565163953Srrs 566163953Srrs /* reset time */ 567169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 568163953Srrs } 569163953Srrs } 570163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 571163953Srrs /* 572163953Srrs * stop the shutdown timer, since we WILL move to 573163953Srrs * SHUTDOWN-ACK-SENT. 574163953Srrs */ 575165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 576163953Srrs } 577163953Srrs /* Now are we there yet? */ 578163953Srrs some_on_streamwheel = 0; 579163953Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 580163953Srrs /* Check to see if some data queued */ 581163953Srrs struct sctp_stream_out *outs; 582163953Srrs 583163953Srrs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) { 584163953Srrs if (!TAILQ_EMPTY(&outs->outqueue)) { 585163953Srrs some_on_streamwheel = 1; 586163953Srrs break; 587163953Srrs } 588163953Srrs } 589163953Srrs } 590163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 591163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 592163953Srrs some_on_streamwheel) { 593163953Srrs /* By returning we will push more data out */ 594163953Srrs return; 595163953Srrs } else { 596163953Srrs /* no outstanding data to send, so move on... */ 597163953Srrs /* send SHUTDOWN-ACK */ 598163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 599163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 600166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 601166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 602163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 603163953Srrs } 604163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT; 605163953Srrs 606163953Srrs /* start SHUTDOWN timer */ 607163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 608163953Srrs stcb, net); 609163953Srrs } 610163953Srrs} 611163953Srrs 612163953Srrsstatic void 613163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 614163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 615163953Srrs{ 616163953Srrs struct sctp_association *asoc; 617163953Srrs 618169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 619169420Srrs "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 620163953Srrs if (stcb == NULL) 621163953Srrs return; 622163953Srrs 623163953Srrs asoc = &stcb->asoc; 624163953Srrs /* process according to association state */ 625163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 626163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 627163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 628163953Srrs SCTP_TCB_UNLOCK(stcb); 629163953Srrs return; 630163953Srrs } 631165220Srrs if (asoc->control_pdapi) { 632163953Srrs /* 633163953Srrs * With a normal shutdown we assume the end of last record. 634163953Srrs */ 635163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 636165220Srrs asoc->control_pdapi->end_added = 1; 637165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 638165220Srrs asoc->control_pdapi = NULL; 639163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 640163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 641163953Srrs } 642163953Srrs /* are the queues empty? */ 643163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 644163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 645163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 646164205Srrs sctp_report_all_outbound(stcb, 0); 647163953Srrs } 648163953Srrs /* stop the timer */ 649165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 650163953Srrs /* send SHUTDOWN-COMPLETE */ 651163953Srrs sctp_send_shutdown_complete(stcb, net); 652163953Srrs /* notify upper layer protocol */ 653163953Srrs if (stcb->sctp_socket) { 654163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 655163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 656163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 657163953Srrs /* Set the connected flag to disconnected */ 658163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 659163953Srrs } 660163953Srrs } 661163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 662163953Srrs /* free the TCB but first save off the ep */ 663165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 664165220Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 665163953Srrs} 666163953Srrs 667163953Srrs/* 668163953Srrs * Skip past the param header and then we will find the chunk that caused the 669163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 670163953Srrs * our peer must be broken. 671163953Srrs */ 672163953Srrsstatic void 673163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 674163953Srrs struct sctp_nets *net) 675163953Srrs{ 676163953Srrs struct sctp_chunkhdr *chk; 677163953Srrs 678163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 679163953Srrs switch (chk->chunk_type) { 680163953Srrs case SCTP_ASCONF_ACK: 681163953Srrs case SCTP_ASCONF: 682163953Srrs sctp_asconf_cleanup(stcb, net); 683163953Srrs break; 684163953Srrs case SCTP_FORWARD_CUM_TSN: 685163953Srrs stcb->asoc.peer_supports_prsctp = 0; 686163953Srrs break; 687163953Srrs default: 688169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 689169420Srrs "Peer does not support chunk type %d(%x)??\n", 690169420Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 691163953Srrs break; 692163953Srrs } 693163953Srrs} 694163953Srrs 695163953Srrs/* 696163953Srrs * Skip past the param header and then we will find the param that caused the 697163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 698163953Srrs * these will turn of specific features. 699163953Srrs */ 700163953Srrsstatic void 701163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 702163953Srrs{ 703163953Srrs struct sctp_paramhdr *pbad; 704163953Srrs 705163953Srrs pbad = phdr + 1; 706163953Srrs switch (ntohs(pbad->param_type)) { 707163953Srrs /* pr-sctp draft */ 708163953Srrs case SCTP_PRSCTP_SUPPORTED: 709163953Srrs stcb->asoc.peer_supports_prsctp = 0; 710163953Srrs break; 711163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 712163953Srrs break; 713163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 714163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 715163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 716163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 717163953Srrs stcb->asoc.ecn_allowed = 0; 718163953Srrs break; 719163953Srrs case SCTP_ADD_IP_ADDRESS: 720163953Srrs case SCTP_DEL_IP_ADDRESS: 721163953Srrs case SCTP_SET_PRIM_ADDR: 722163953Srrs stcb->asoc.peer_supports_asconf = 0; 723163953Srrs break; 724163953Srrs case SCTP_SUCCESS_REPORT: 725163953Srrs case SCTP_ERROR_CAUSE_IND: 726169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 727169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 728169420Srrs "Turning off ASCONF to this strange peer\n"); 729163953Srrs stcb->asoc.peer_supports_asconf = 0; 730163953Srrs break; 731163953Srrs default: 732169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 733169420Srrs "Peer does not support param type %d(%x)??\n", 734169420Srrs pbad->param_type, (uint32_t) pbad->param_type); 735163953Srrs break; 736163953Srrs } 737163953Srrs} 738163953Srrs 739163953Srrsstatic int 740163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 741163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 742163953Srrs{ 743163953Srrs int chklen; 744163953Srrs struct sctp_paramhdr *phdr; 745163953Srrs uint16_t error_type; 746163953Srrs uint16_t error_len; 747163953Srrs struct sctp_association *asoc; 748163953Srrs 749163953Srrs int adjust; 750163953Srrs 751163953Srrs /* parse through all of the errors and process */ 752163953Srrs asoc = &stcb->asoc; 753163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 754163953Srrs sizeof(struct sctp_chunkhdr)); 755163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 756163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 757163953Srrs /* Process an Error Cause */ 758163953Srrs error_type = ntohs(phdr->param_type); 759163953Srrs error_len = ntohs(phdr->param_length); 760163953Srrs if ((error_len > chklen) || (error_len == 0)) { 761163953Srrs /* invalid param length for this param */ 762169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 763169420Srrs chklen, error_len); 764163953Srrs return (0); 765163953Srrs } 766163953Srrs switch (error_type) { 767163953Srrs case SCTP_CAUSE_INVALID_STREAM: 768163953Srrs case SCTP_CAUSE_MISSING_PARAM: 769163953Srrs case SCTP_CAUSE_INVALID_PARAM: 770163953Srrs case SCTP_CAUSE_NO_USER_DATA: 771169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 772169420Srrs error_type); 773163953Srrs break; 774163953Srrs case SCTP_CAUSE_STALE_COOKIE: 775163953Srrs /* 776163953Srrs * We only act if we have echoed a cookie and are 777163953Srrs * waiting. 778163953Srrs */ 779163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 780163953Srrs int *p; 781163953Srrs 782163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 783163953Srrs /* Save the time doubled */ 784163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 785163953Srrs asoc->stale_cookie_count++; 786163953Srrs if (asoc->stale_cookie_count > 787163953Srrs asoc->max_init_times) { 788163953Srrs sctp_abort_notification(stcb, 0); 789163953Srrs /* now free the asoc */ 790165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 791163953Srrs return (-1); 792163953Srrs } 793163953Srrs /* blast back to INIT state */ 794163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 795163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 796163953Srrs 797163953Srrs sctp_stop_all_cookie_timers(stcb); 798163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 799163953Srrs } 800163953Srrs break; 801163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 802163953Srrs /* 803163953Srrs * Nothing we can do here, we don't do hostname 804163953Srrs * addresses so if the peer does not like my IPv6 805163953Srrs * (or IPv4 for that matter) it does not matter. If 806163953Srrs * they don't support that type of address, they can 807163953Srrs * NOT possibly get that packet type... i.e. with no 808163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 809163953Srrs * safely ignore this one. If we ever added support 810163953Srrs * for HOSTNAME Addresses, then we would need to do 811163953Srrs * something here. 812163953Srrs */ 813163953Srrs break; 814163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 815163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 816163953Srrs break; 817163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 818163953Srrs sctp_process_unrecog_param(stcb, phdr); 819163953Srrs break; 820163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 821163953Srrs /* 822163953Srrs * We ignore this since the timer will drive out a 823163953Srrs * new cookie anyway and there timer will drive us 824163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 825163953Srrs * here since we don't have their tag. 826163953Srrs */ 827163953Srrs break; 828163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 829163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 830163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 831163953Srrs /* 832163953Srrs * We should NOT get these here, but in a 833166086Srrs * ASCONF-ACK. 834163953Srrs */ 835169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 836169420Srrs error_type); 837163953Srrs break; 838163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 839163953Srrs /* 840163953Srrs * And what, pray tell do we do with the fact that 841163953Srrs * the peer is out of resources? Not really sure we 842166086Srrs * could do anything but abort. I suspect this 843163953Srrs * should have came WITH an abort instead of in a 844163953Srrs * OP-ERROR. 845163953Srrs */ 846163953Srrs break; 847163953Srrs default: 848169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 849169420Srrs error_type); 850163953Srrs break; 851163953Srrs } 852163953Srrs adjust = SCTP_SIZE32(error_len); 853163953Srrs chklen -= adjust; 854163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 855163953Srrs } 856163953Srrs return (0); 857163953Srrs} 858163953Srrs 859163953Srrsstatic int 860169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 861169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 862169352Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, 863169352Srrs uint32_t table_id) 864163953Srrs{ 865163953Srrs struct sctp_init_ack *init_ack; 866163953Srrs int *state; 867163953Srrs struct mbuf *op_err; 868163953Srrs 869169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 870169420Srrs "sctp_handle_init_ack: handling INIT-ACK\n"); 871169420Srrs 872163953Srrs if (stcb == NULL) { 873169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 874169420Srrs "sctp_handle_init_ack: TCB is null\n"); 875163953Srrs return (-1); 876163953Srrs } 877163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 878163953Srrs /* Invalid length */ 879163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 880163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 881169352Srrs op_err, 0, 0); 882168299Srrs *abort_no_unlock = 1; 883163953Srrs return (-1); 884163953Srrs } 885163953Srrs init_ack = &cp->init; 886163953Srrs /* validate parameters */ 887163953Srrs if (init_ack->initiate_tag == 0) { 888163953Srrs /* protocol error... send an abort */ 889163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 890163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 891169352Srrs op_err, 0, 0); 892168299Srrs *abort_no_unlock = 1; 893163953Srrs return (-1); 894163953Srrs } 895163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 896163953Srrs /* protocol error... send an abort */ 897163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 898163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 899169352Srrs op_err, 0, 0); 900168299Srrs *abort_no_unlock = 1; 901163953Srrs return (-1); 902163953Srrs } 903163953Srrs if (init_ack->num_inbound_streams == 0) { 904163953Srrs /* protocol error... send an abort */ 905163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 906163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 907169352Srrs op_err, 0, 0); 908168299Srrs *abort_no_unlock = 1; 909163953Srrs return (-1); 910163953Srrs } 911163953Srrs if (init_ack->num_outbound_streams == 0) { 912163953Srrs /* protocol error... send an abort */ 913163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 914163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 915169352Srrs op_err, 0, 0); 916168299Srrs *abort_no_unlock = 1; 917163953Srrs return (-1); 918163953Srrs } 919163953Srrs /* process according to association state... */ 920163953Srrs state = &stcb->asoc.state; 921163953Srrs switch (*state & SCTP_STATE_MASK) { 922163953Srrs case SCTP_STATE_COOKIE_WAIT: 923163953Srrs /* this is the expected state for this chunk */ 924163953Srrs /* process the INIT-ACK parameters */ 925163953Srrs if (stcb->asoc.primary_destination->dest_state & 926163953Srrs SCTP_ADDR_UNCONFIRMED) { 927163953Srrs /* 928163953Srrs * The primary is where we sent the INIT, we can 929163953Srrs * always consider it confirmed when the INIT-ACK is 930163953Srrs * returned. Do this before we load addresses 931163953Srrs * though. 932163953Srrs */ 933163953Srrs stcb->asoc.primary_destination->dest_state &= 934163953Srrs ~SCTP_ADDR_UNCONFIRMED; 935163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 936163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 937163953Srrs } 938169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 939169352Srrs net, abort_no_unlock, vrf_id, 940169352Srrs table_id) < 0) { 941163953Srrs /* error in parsing parameters */ 942163953Srrs return (-1); 943163953Srrs } 944163953Srrs /* update our state */ 945169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 946163953Srrs if (*state & SCTP_STATE_SHUTDOWN_PENDING) { 947163953Srrs *state = SCTP_STATE_COOKIE_ECHOED | 948163953Srrs SCTP_STATE_SHUTDOWN_PENDING; 949163953Srrs } else { 950163953Srrs *state = SCTP_STATE_COOKIE_ECHOED; 951163953Srrs } 952163953Srrs 953163953Srrs /* reset the RTO calc */ 954163953Srrs stcb->asoc.overall_error_count = 0; 955169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 956163953Srrs /* 957163953Srrs * collapse the init timer back in case of a exponential 958165220Srrs * backoff 959163953Srrs */ 960163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 961163953Srrs stcb, net); 962163953Srrs /* 963163953Srrs * the send at the end of the inbound data processing will 964163953Srrs * cause the cookie to be sent 965163953Srrs */ 966163953Srrs break; 967163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 968163953Srrs /* incorrect state... discard */ 969163953Srrs break; 970163953Srrs case SCTP_STATE_COOKIE_ECHOED: 971163953Srrs /* incorrect state... discard */ 972163953Srrs break; 973163953Srrs case SCTP_STATE_OPEN: 974163953Srrs /* incorrect state... discard */ 975163953Srrs break; 976163953Srrs case SCTP_STATE_EMPTY: 977163953Srrs case SCTP_STATE_INUSE: 978163953Srrs default: 979163953Srrs /* incorrect state... discard */ 980163953Srrs return (-1); 981163953Srrs break; 982163953Srrs } 983169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 984163953Srrs return (0); 985163953Srrs} 986163953Srrs 987163953Srrs 988163953Srrs/* 989163953Srrs * handle a state cookie for an existing association m: input packet mbuf 990163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 991163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 992163953Srrs * mbuf to the cookie-echo chunk 993163953Srrs */ 994163953Srrsstatic struct sctp_tcb * 995163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 996163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 997163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 998169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 999169352Srrs uint32_t vrf_id, uint32_t table_id) 1000163953Srrs{ 1001163953Srrs struct sctp_association *asoc; 1002163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1003163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1004163953Srrs int chk_length; 1005165220Srrs int init_offset, initack_offset, i; 1006163953Srrs int retval; 1007164205Srrs int spec_flag = 0; 1008170140Srrs uint32_t how_indx; 1009163953Srrs 1010163953Srrs /* I know that the TCB is non-NULL from the caller */ 1011163953Srrs asoc = &stcb->asoc; 1012166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1013166023Srrs if (asoc->cookie_how[how_indx] == 0) 1014166023Srrs break; 1015166023Srrs } 1016166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1017166023Srrs asoc->cookie_how[how_indx] = 1; 1018166023Srrs } 1019163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1020163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1021163953Srrs struct mbuf *op_err; 1022163953Srrs struct sctp_paramhdr *ph; 1023163953Srrs 1024163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1025163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1026165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1027163953Srrs if (op_err == NULL) { 1028163953Srrs /* FOOBAR */ 1029163953Srrs return (NULL); 1030163953Srrs } 1031163953Srrs /* pre-reserve some space */ 1032165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1033165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1034165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1035163953Srrs /* Set the len */ 1036165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1037163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1038163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1039163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1040169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1041169352Srrs vrf_id, table_id); 1042166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1043166023Srrs asoc->cookie_how[how_indx] = 2; 1044163953Srrs return (NULL); 1045163953Srrs } 1046163953Srrs /* 1047163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1048163953Srrs * INIT should start after the cookie-echo header struct (chunk 1049163953Srrs * header, state cookie header struct) 1050163953Srrs */ 1051163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1052163953Srrs 1053163953Srrs init_cp = (struct sctp_init_chunk *) 1054163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1055163953Srrs (uint8_t *) & init_buf); 1056163953Srrs if (init_cp == NULL) { 1057163953Srrs /* could not pull a INIT chunk in cookie */ 1058163953Srrs return (NULL); 1059163953Srrs } 1060163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1061163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1062163953Srrs return (NULL); 1063163953Srrs } 1064163953Srrs /* 1065163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1066163953Srrs * INIT-ACK follows the INIT chunk 1067163953Srrs */ 1068163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1069163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1070163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1071163953Srrs (uint8_t *) & initack_buf); 1072163953Srrs if (initack_cp == NULL) { 1073163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1074163953Srrs return (NULL); 1075163953Srrs } 1076163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1077163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1078163953Srrs return (NULL); 1079163953Srrs } 1080163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1081163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1082163953Srrs /* 1083163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1084163953Srrs * to get into the OPEN state 1085163953Srrs */ 1086166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1087166086Srrs#ifdef INVARIANTS 1088166023Srrs panic("Case D and non-match seq?"); 1089166086Srrs#else 1090169420Srrs SCTP_PRINTF("Case D, seq non-match %x vs %x?\n", 1091169420Srrs ntohl(initack_cp->init.initial_tsn), 1092169420Srrs asoc->init_seq_number); 1093166086Srrs#endif 1094166023Srrs } 1095163953Srrs switch SCTP_GET_STATE 1096163953Srrs (asoc) { 1097163953Srrs case SCTP_STATE_COOKIE_WAIT: 1098166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1099163953Srrs /* 1100169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1101166023Srrs * correct tags... just accept it...but we must 1102166023Srrs * process the init so that we can make sure we have 1103166023Srrs * the right seq no's. 1104163953Srrs */ 1105163953Srrs /* First we must process the INIT !! */ 1106163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1107163953Srrs if (retval < 0) { 1108166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1109166023Srrs asoc->cookie_how[how_indx] = 3; 1110163953Srrs return (NULL); 1111163953Srrs } 1112163953Srrs /* we have already processed the INIT so no problem */ 1113163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1114165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1115165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1116163953Srrs /* update current state */ 1117166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1118166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1119166675Srrs else 1120166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1121163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1122166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1123163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1124163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1125163953Srrs 1126166675Srrs } else { 1127163953Srrs /* if ok, move to OPEN state */ 1128163953Srrs asoc->state = SCTP_STATE_OPEN; 1129163953Srrs } 1130166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1131165220Srrs sctp_stop_all_cookie_timers(stcb); 1132163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1133163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1134163953Srrs (inp->sctp_socket->so_qlimit == 0) 1135163953Srrs ) { 1136163953Srrs /* 1137163953Srrs * Here is where collision would go if we 1138163953Srrs * did a connect() and instead got a 1139163953Srrs * init/init-ack/cookie done before the 1140163953Srrs * init-ack came back.. 1141163953Srrs */ 1142163953Srrs stcb->sctp_ep->sctp_flags |= 1143163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1144163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1145163953Srrs } 1146163953Srrs /* notify upper layer */ 1147163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1148163953Srrs /* 1149163953Srrs * since we did not send a HB make sure we don't 1150163953Srrs * double things 1151163953Srrs */ 1152163953Srrs net->hb_responded = 1; 1153163953Srrs 1154163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1155163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1156163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1157163953Srrs inp, stcb, NULL); 1158163953Srrs } 1159163953Srrs break; 1160163953Srrs default: 1161163953Srrs /* 1162163953Srrs * we're in the OPEN state (or beyond), so peer must 1163163953Srrs * have simply lost the COOKIE-ACK 1164163953Srrs */ 1165163953Srrs break; 1166163953Srrs } /* end switch */ 1167165220Srrs sctp_stop_all_cookie_timers(stcb); 1168163953Srrs /* 1169163953Srrs * We ignore the return code here.. not sure if we should 1170163953Srrs * somehow abort.. but we do have an existing asoc. This 1171163953Srrs * really should not fail. 1172163953Srrs */ 1173163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1174163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1175163953Srrs initack_offset, sh, init_src)) { 1176166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1177166023Srrs asoc->cookie_how[how_indx] = 4; 1178163953Srrs return (NULL); 1179163953Srrs } 1180163953Srrs /* respond with a COOKIE-ACK */ 1181165220Srrs sctp_toss_old_cookies(stcb, asoc); 1182163953Srrs sctp_send_cookie_ack(stcb); 1183166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1184166023Srrs asoc->cookie_how[how_indx] = 5; 1185163953Srrs return (stcb); 1186169352Srrs } 1187163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1188163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1189163953Srrs cookie->tie_tag_my_vtag == 0 && 1190163953Srrs cookie->tie_tag_peer_vtag == 0) { 1191163953Srrs /* 1192163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1193163953Srrs */ 1194166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1195166023Srrs asoc->cookie_how[how_indx] = 6; 1196163953Srrs return (NULL); 1197163953Srrs } 1198163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1199163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1200163953Srrs init_cp->init.initiate_tag == 0)) { 1201163953Srrs /* 1202163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1203163953Srrs * should be ok, re-accept peer info 1204163953Srrs */ 1205166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1206166023Srrs /* 1207166023Srrs * Extension of case C. If we hit this, then the 1208166023Srrs * random number generator returned the same vtag 1209166023Srrs * when we first sent our INIT-ACK and when we later 1210166023Srrs * sent our INIT. The side with the seq numbers that 1211166023Srrs * are different will be the one that normnally 1212166023Srrs * would have hit case C. This in effect "extends" 1213166023Srrs * our vtags in this collision case to be 64 bits. 1214166023Srrs * The same collision could occur aka you get both 1215166023Srrs * vtag and seq number the same twice in a row.. but 1216166023Srrs * is much less likely. If it did happen then we 1217166023Srrs * would proceed through and bring up the assoc.. we 1218166023Srrs * may end up with the wrong stream setup however.. 1219166023Srrs * which would be bad.. but there is no way to 1220166023Srrs * tell.. until we send on a stream that does not 1221166023Srrs * exist :-) 1222166023Srrs */ 1223166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1224166023Srrs asoc->cookie_how[how_indx] = 7; 1225166023Srrs 1226166023Srrs return (NULL); 1227166023Srrs } 1228166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1229166023Srrs asoc->cookie_how[how_indx] = 8; 1230165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1231163953Srrs sctp_stop_all_cookie_timers(stcb); 1232163953Srrs /* 1233163953Srrs * since we did not send a HB make sure we don't double 1234163953Srrs * things 1235163953Srrs */ 1236163953Srrs net->hb_responded = 1; 1237163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1238163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1239163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1240163953Srrs NULL); 1241163953Srrs } 1242163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1243164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1244164205Srrs 1245164205Srrs /* Note last_cwr_tsn? where is this used? */ 1246163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1247164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1248164205Srrs /* 1249164205Srrs * Ok the peer probably discarded our data (if we 1250164205Srrs * echoed a cookie+data). So anything on the 1251164205Srrs * sent_queue should be marked for retransmit, we 1252164205Srrs * may not get something to kick us so it COULD 1253164205Srrs * still take a timeout to move these.. but it can't 1254164205Srrs * hurt to mark them. 1255164205Srrs */ 1256164205Srrs struct sctp_tmit_chunk *chk; 1257163953Srrs 1258164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1259164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1260164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1261168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1262164205Srrs spec_flag++; 1263164205Srrs } 1264164205Srrs } 1265164205Srrs 1266164205Srrs } 1267163953Srrs /* process the INIT info (peer's info) */ 1268163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1269163953Srrs if (retval < 0) { 1270166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1271166023Srrs asoc->cookie_how[how_indx] = 9; 1272163953Srrs return (NULL); 1273163953Srrs } 1274163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1275163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1276163953Srrs initack_offset, sh, init_src)) { 1277166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1278166023Srrs asoc->cookie_how[how_indx] = 10; 1279163953Srrs return (NULL); 1280163953Srrs } 1281163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1282163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1283163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1284163953Srrs 1285163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1286163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1287163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1288163953Srrs stcb->sctp_ep->sctp_flags |= 1289163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1290163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1291163953Srrs } 1292166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1293166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1294166675Srrs else 1295166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1296166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1297166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1298166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1299166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1300166675Srrs } else { 1301166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1302163953Srrs } 1303163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1304166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1305163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1306163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1307163953Srrs 1308163953Srrs } else { 1309163953Srrs asoc->state = SCTP_STATE_OPEN; 1310163953Srrs } 1311163953Srrs sctp_stop_all_cookie_timers(stcb); 1312165220Srrs sctp_toss_old_cookies(stcb, asoc); 1313163953Srrs sctp_send_cookie_ack(stcb); 1314164205Srrs if (spec_flag) { 1315164205Srrs /* 1316164205Srrs * only if we have retrans set do we do this. What 1317164205Srrs * this call does is get only the COOKIE-ACK out and 1318164205Srrs * then when we return the normal call to 1319164205Srrs * sctp_chunk_output will get the retrans out behind 1320164205Srrs * this. 1321164205Srrs */ 1322164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1323164205Srrs } 1324166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1325166023Srrs asoc->cookie_how[how_indx] = 11; 1326166023Srrs 1327163953Srrs return (stcb); 1328163953Srrs } 1329163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1330163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1331163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1332163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1333163953Srrs cookie->tie_tag_peer_vtag != 0) { 1334163953Srrs struct sctpasochead *head; 1335163953Srrs 1336163953Srrs /* 1337163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1338163953Srrs */ 1339165220Srrs /* temp code */ 1340166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1341166023Srrs asoc->cookie_how[how_indx] = 12; 1342165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1343165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1344165647Srrs 1345163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1346163953Srrs /* notify upper layer */ 1347163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1348165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1349166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1350166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1351166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1352166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1353166675Srrs } 1354166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1355166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1356166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1357166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1358166675Srrs } 1359165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1360165647Srrs asoc->state = SCTP_STATE_OPEN | 1361165647Srrs SCTP_STATE_SHUTDOWN_PENDING; 1362165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1363165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1364165647Srrs 1365165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1366165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1367165647Srrs asoc->state = SCTP_STATE_OPEN; 1368165647Srrs } 1369165647Srrs asoc->pre_open_streams = 1370165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1371165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1372165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1373165647Srrs 1374165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1375165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1376165647Srrs 1377165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1378165647Srrs 1379165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1380170138Srrs if (asoc->mapping_array) { 1381165647Srrs memset(asoc->mapping_array, 0, 1382165647Srrs asoc->mapping_array_size); 1383170138Srrs } 1384165220Srrs SCTP_TCB_UNLOCK(stcb); 1385165220Srrs SCTP_INP_INFO_WLOCK(); 1386165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1387165220Srrs SCTP_TCB_LOCK(stcb); 1388165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1389163953Srrs /* send up all the data */ 1390164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1391165220Srrs 1392164205Srrs sctp_report_all_outbound(stcb, 1); 1393165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1394165220Srrs stcb->asoc.strmout[i].stream_no = i; 1395165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1396165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1397165220Srrs } 1398163953Srrs /* process the INIT-ACK info (my info) */ 1399163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1400163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1401163953Srrs 1402163953Srrs /* pull from vtag hash */ 1403163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1404163953Srrs /* re-insert to new vtag position */ 1405163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1406163953Srrs sctppcbinfo.hashasocmark)]; 1407163953Srrs /* 1408163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1409163953Srrs * system 1410163953Srrs */ 1411163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1412163953Srrs 1413163953Srrs /* Is this the first restart? */ 1414163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1415163953Srrs /* Ok add it to assoc_id vtag hash */ 1416163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1417163953Srrs sctppcbinfo.hashrestartmark)]; 1418163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1419163953Srrs stcb->asoc.in_restart_hash = 1; 1420163953Srrs } 1421163953Srrs /* process the INIT info (peer's info) */ 1422164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1423165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1424165220Srrs SCTP_INP_INFO_WUNLOCK(); 1425165220Srrs 1426163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1427163953Srrs if (retval < 0) { 1428166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1429166023Srrs asoc->cookie_how[how_indx] = 13; 1430166023Srrs 1431163953Srrs return (NULL); 1432163953Srrs } 1433163953Srrs /* 1434163953Srrs * since we did not send a HB make sure we don't double 1435163953Srrs * things 1436163953Srrs */ 1437163953Srrs net->hb_responded = 1; 1438163953Srrs 1439163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1440163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1441163953Srrs initack_offset, sh, init_src)) { 1442166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1443166023Srrs asoc->cookie_how[how_indx] = 14; 1444166023Srrs 1445163953Srrs return (NULL); 1446163953Srrs } 1447163953Srrs /* respond with a COOKIE-ACK */ 1448163953Srrs sctp_stop_all_cookie_timers(stcb); 1449165220Srrs sctp_toss_old_cookies(stcb, asoc); 1450163953Srrs sctp_send_cookie_ack(stcb); 1451166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1452166023Srrs asoc->cookie_how[how_indx] = 15; 1453163953Srrs 1454163953Srrs return (stcb); 1455163953Srrs } 1456166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1457166023Srrs asoc->cookie_how[how_indx] = 16; 1458163953Srrs /* all other cases... */ 1459163953Srrs return (NULL); 1460163953Srrs} 1461163953Srrs 1462166086Srrs 1463163953Srrs/* 1464163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1465163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1466163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1467163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1468163953Srrs * was from returns a new TCB 1469163953Srrs */ 1470163953Srrsstatic struct sctp_tcb * 1471163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1472163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1473163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1474163953Srrs struct sockaddr *init_src, int *notification, 1475169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1476169352Srrs uint32_t vrf_id, uint32_t table_id) 1477163953Srrs{ 1478163953Srrs struct sctp_tcb *stcb; 1479163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1480163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1481163953Srrs struct sockaddr_storage sa_store; 1482163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1483163953Srrs struct sockaddr_in *sin; 1484163953Srrs struct sockaddr_in6 *sin6; 1485163953Srrs struct sctp_association *asoc; 1486163953Srrs int chk_length; 1487163953Srrs int init_offset, initack_offset, initack_limit; 1488163953Srrs int retval; 1489163953Srrs int error = 0; 1490163953Srrs uint32_t old_tag; 1491166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1492163953Srrs 1493163953Srrs /* 1494163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1495163953Srrs * INIT should start after the cookie-echo header struct (chunk 1496163953Srrs * header, state cookie header struct) 1497163953Srrs */ 1498163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1499163953Srrs init_cp = (struct sctp_init_chunk *) 1500163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1501163953Srrs (uint8_t *) & init_buf); 1502163953Srrs if (init_cp == NULL) { 1503163953Srrs /* could not pull a INIT chunk in cookie */ 1504169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1505169420Srrs "process_cookie_new: could not pull INIT chunk hdr\n"); 1506163953Srrs return (NULL); 1507163953Srrs } 1508163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1509163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1510169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 1511163953Srrs return (NULL); 1512163953Srrs } 1513163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1514163953Srrs /* 1515163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1516163953Srrs * INIT-ACK follows the INIT chunk 1517163953Srrs */ 1518163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1519163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1520163953Srrs (uint8_t *) & initack_buf); 1521163953Srrs if (initack_cp == NULL) { 1522163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1523169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1524163953Srrs return (NULL); 1525163953Srrs } 1526163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1527163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1528163953Srrs return (NULL); 1529163953Srrs } 1530163953Srrs /* 1531163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1532163953Srrs * "initack_limit" value. This is because the chk_length field 1533163953Srrs * includes the length of the cookie, but the cookie is omitted when 1534163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1535163953Srrs */ 1536163953Srrs initack_limit = offset + cookie_len; 1537163953Srrs 1538163953Srrs /* 1539163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1540163953Srrs * and popluate 1541163953Srrs */ 1542163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1543168299Srrs ntohl(initack_cp->init.initiate_tag), vrf_id); 1544163953Srrs if (stcb == NULL) { 1545163953Srrs struct mbuf *op_err; 1546163953Srrs 1547163953Srrs /* memory problem? */ 1548169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 1549169420Srrs "process_cookie_new: no room for another TCB!\n"); 1550163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1551170140Srrs 1552163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1553169352Srrs sh, op_err, vrf_id, table_id); 1554163953Srrs return (NULL); 1555163953Srrs } 1556163953Srrs /* get the correct sctp_nets */ 1557169420Srrs if (netp) 1558169420Srrs *netp = sctp_findnet(stcb, init_src); 1559169420Srrs 1560163953Srrs asoc = &stcb->asoc; 1561169352Srrs /* save the table id (vrf_id is done in aloc_assoc) */ 1562169352Srrs asoc->table_id = table_id; 1563163953Srrs /* get scope variables out of cookie */ 1564163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1565163953Srrs asoc->site_scope = cookie->site_scope; 1566163953Srrs asoc->local_scope = cookie->local_scope; 1567163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1568163953Srrs 1569163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1570163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1571163953Srrs struct mbuf *op_err; 1572163953Srrs 1573163953Srrs /* 1574163953Srrs * Houston we have a problem. The EP changed while the 1575163953Srrs * cookie was in flight. Only recourse is to abort the 1576163953Srrs * association. 1577163953Srrs */ 1578170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1579163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1580163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1581169352Srrs sh, op_err, vrf_id, table_id); 1582170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1583163953Srrs return (NULL); 1584163953Srrs } 1585163953Srrs /* process the INIT-ACK info (my info) */ 1586163953Srrs old_tag = asoc->my_vtag; 1587163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1588163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1589163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1590163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1591163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1592163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1593163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1594163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1595163953Srrs 1596163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1597163953Srrs 1598163953Srrs /* process the INIT info (peer's info) */ 1599169420Srrs if (netp) 1600169420Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1601169420Srrs else 1602169420Srrs retval = 0; 1603163953Srrs if (retval < 0) { 1604170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1605165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1606170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1607163953Srrs return (NULL); 1608163953Srrs } 1609163953Srrs /* load all addresses */ 1610163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1611163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1612163953Srrs init_src)) { 1613170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1614165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1615170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1616163953Srrs return (NULL); 1617163953Srrs } 1618163953Srrs /* 1619163953Srrs * verify any preceding AUTH chunk that was skipped 1620163953Srrs */ 1621163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1622163953Srrs sctp_auth_get_cookie_params(stcb, m, 1623163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1624163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1625163953Srrs if (auth_skipped) { 1626163953Srrs struct sctp_auth_chunk *auth; 1627163953Srrs 1628163953Srrs auth = (struct sctp_auth_chunk *) 1629166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1630169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 1631163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1632169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 1633169420Srrs "COOKIE-ECHO: AUTH failed\n"); 1634165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1635163953Srrs return (NULL); 1636163953Srrs } else { 1637163953Srrs /* remaining chunks checked... good to go */ 1638163953Srrs stcb->asoc.authenticated = 1; 1639163953Srrs } 1640163953Srrs } 1641163953Srrs /* update current state */ 1642169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 1643163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1644163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1645163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1646163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1647163953Srrs } else { 1648163953Srrs asoc->state = SCTP_STATE_OPEN; 1649163953Srrs } 1650165220Srrs sctp_stop_all_cookie_timers(stcb); 1651163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1652163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1653163953Srrs 1654163953Srrs /* 1655163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1656163953Srrs * addresses that need to get added to the peer (eg. addresses 1657163953Srrs * changed while cookie echo in flight). This needs to be done 1658163953Srrs * after we go to the OPEN state to do the correct asconf 1659163953Srrs * processing. else, make sure we have the correct addresses in our 1660163953Srrs * lists 1661163953Srrs */ 1662163953Srrs 1663163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1664163953Srrs /* pull in local_address (our "from" address) */ 1665163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1666163953Srrs /* source addr is IPv4 */ 1667163953Srrs sin = (struct sockaddr_in *)initack_src; 1668163953Srrs memset(sin, 0, sizeof(*sin)); 1669163953Srrs sin->sin_family = AF_INET; 1670163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1671163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1672163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1673163953Srrs /* source addr is IPv6 */ 1674163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1675163953Srrs memset(sin6, 0, sizeof(*sin6)); 1676163953Srrs sin6->sin6_family = AF_INET6; 1677163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1678163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1679163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1680163953Srrs sizeof(sin6->sin6_addr)); 1681163953Srrs } else { 1682170140Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1683165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1684170140Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1685163953Srrs return (NULL); 1686163953Srrs } 1687163953Srrs 1688163953Srrs sctp_check_address_list(stcb, m, 1689163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1690163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1691163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1692163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1693163953Srrs 1694163953Srrs 1695163953Srrs /* set up to notify upper layer */ 1696163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1697163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1698163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1699163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1700163953Srrs /* 1701163953Srrs * This is an endpoint that called connect() how it got a 1702163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1703163953Srrs * the INIT was sent, but before it got there.. a complete 1704163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1705163953Srrs * should have went to the other code.. not here.. oh well.. 1706163953Srrs * a bit of protection is worth having.. 1707163953Srrs */ 1708163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1709163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1710163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1711163953Srrs (inp->sctp_socket->so_qlimit)) { 1712163953Srrs /* 1713163953Srrs * We don't want to do anything with this one. Since it is 1714163953Srrs * the listening guy. The timer will get started for 1715163953Srrs * accepted connections in the caller. 1716163953Srrs */ 1717163953Srrs ; 1718163953Srrs } 1719163953Srrs /* since we did not send a HB make sure we don't double things */ 1720169420Srrs if ((netp) && (*netp)) 1721169420Srrs (*netp)->hb_responded = 1; 1722163953Srrs 1723163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1724163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1725163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1726163953Srrs } 1727163953Srrs /* respond with a COOKIE-ACK */ 1728165220Srrs /* calculate the RTT */ 1729169420Srrs if ((netp) && (*netp)) 1730169420Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1731169420Srrs &cookie->time_entered); 1732163953Srrs sctp_send_cookie_ack(stcb); 1733163953Srrs return (stcb); 1734163953Srrs} 1735163953Srrs 1736163953Srrs 1737163953Srrs/* 1738163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1739163953Srrs * existing (non-NULL) TCB 1740163953Srrs */ 1741163953Srrsstatic struct mbuf * 1742163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1743163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1744163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1745169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1746169352Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint32_t table_id) 1747163953Srrs{ 1748163953Srrs struct sctp_state_cookie *cookie; 1749163953Srrs struct sockaddr_in6 sin6; 1750163953Srrs struct sockaddr_in sin; 1751163953Srrs struct sctp_tcb *l_stcb = *stcb; 1752163953Srrs struct sctp_inpcb *l_inp; 1753163953Srrs struct sockaddr *to; 1754163953Srrs sctp_assoc_t sac_restart_id; 1755163953Srrs struct sctp_pcb *ep; 1756163953Srrs struct mbuf *m_sig; 1757163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1758163953Srrs uint8_t *sig; 1759163953Srrs uint8_t cookie_ok = 0; 1760163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1761163953Srrs unsigned int cookie_len; 1762163953Srrs struct timeval now; 1763163953Srrs struct timeval time_expires; 1764163953Srrs struct sockaddr_storage dest_store; 1765163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1766163953Srrs struct ip *iph; 1767163953Srrs int notification = 0; 1768163953Srrs struct sctp_nets *netl; 1769163953Srrs int had_a_existing_tcb = 0; 1770163953Srrs 1771169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1772169420Srrs "sctp_handle_cookie: handling COOKIE-ECHO\n"); 1773163953Srrs 1774163953Srrs if (inp_p == NULL) { 1775163953Srrs return (NULL); 1776163953Srrs } 1777163953Srrs /* First get the destination address setup too. */ 1778163953Srrs iph = mtod(m, struct ip *); 1779163953Srrs if (iph->ip_v == IPVERSION) { 1780163953Srrs /* its IPv4 */ 1781170140Srrs struct sockaddr_in *lsin; 1782163953Srrs 1783170140Srrs lsin = (struct sockaddr_in *)(localep_sa); 1784170140Srrs memset(lsin, 0, sizeof(*lsin)); 1785170140Srrs lsin->sin_family = AF_INET; 1786170140Srrs lsin->sin_len = sizeof(*lsin); 1787170140Srrs lsin->sin_port = sh->dest_port; 1788170140Srrs lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 1789165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1790163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1791163953Srrs /* its IPv6 */ 1792163953Srrs struct ip6_hdr *ip6; 1793170140Srrs struct sockaddr_in6 *lsin6; 1794163953Srrs 1795170140Srrs lsin6 = (struct sockaddr_in6 *)(localep_sa); 1796170140Srrs memset(lsin6, 0, sizeof(*lsin6)); 1797170140Srrs lsin6->sin6_family = AF_INET6; 1798170140Srrs lsin6->sin6_len = sizeof(struct sockaddr_in6); 1799163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1800170140Srrs lsin6->sin6_port = sh->dest_port; 1801170140Srrs lsin6->sin6_addr = ip6->ip6_dst; 1802166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1803163953Srrs } else { 1804163953Srrs return (NULL); 1805163953Srrs } 1806163953Srrs 1807163953Srrs cookie = &cp->cookie; 1808163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1809163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1810163953Srrs 1811163953Srrs if ((cookie->peerport != sh->src_port) && 1812163953Srrs (cookie->myport != sh->dest_port) && 1813163953Srrs (cookie->my_vtag != sh->v_tag)) { 1814163953Srrs /* 1815163953Srrs * invalid ports or bad tag. Note that we always leave the 1816163953Srrs * v_tag in the header in network order and when we stored 1817163953Srrs * it in the my_vtag slot we also left it in network order. 1818169352Srrs * This maintains the match even though it may be in the 1819163953Srrs * opposite byte order of the machine :-> 1820163953Srrs */ 1821163953Srrs return (NULL); 1822163953Srrs } 1823163953Srrs if (cookie_len > size_of_pkt || 1824163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1825163953Srrs sizeof(struct sctp_init_chunk) + 1826163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1827163953Srrs /* cookie too long! or too small */ 1828163953Srrs return (NULL); 1829163953Srrs } 1830163953Srrs /* 1831163953Srrs * split off the signature into its own mbuf (since it should not be 1832163953Srrs * calculated in the sctp_hmac_m() call). 1833163953Srrs */ 1834163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1835163953Srrs if (sig_offset > size_of_pkt) { 1836163953Srrs /* packet not correct size! */ 1837163953Srrs /* XXX this may already be accounted for earlier... */ 1838163953Srrs return (NULL); 1839163953Srrs } 1840163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1841163953Srrs if (m_sig == NULL) { 1842163953Srrs /* out of memory or ?? */ 1843163953Srrs return (NULL); 1844163953Srrs } 1845163953Srrs /* 1846163953Srrs * compute the signature/digest for the cookie 1847163953Srrs */ 1848163953Srrs ep = &(*inp_p)->sctp_ep; 1849163953Srrs l_inp = *inp_p; 1850163953Srrs if (l_stcb) { 1851163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1852163953Srrs } 1853163953Srrs SCTP_INP_RLOCK(l_inp); 1854163953Srrs if (l_stcb) { 1855163953Srrs SCTP_TCB_LOCK(l_stcb); 1856163953Srrs } 1857163953Srrs /* which cookie is it? */ 1858163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1859163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1860163953Srrs /* it's the old cookie */ 1861169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1862163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1863163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1864163953Srrs } else { 1865163953Srrs /* it's the current cookie */ 1866169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1867163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1868163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1869163953Srrs } 1870163953Srrs /* get the signature */ 1871163953Srrs SCTP_INP_RUNLOCK(l_inp); 1872163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1873163953Srrs if (sig == NULL) { 1874163953Srrs /* couldn't find signature */ 1875164085Srrs sctp_m_freem(m_sig); 1876163953Srrs return (NULL); 1877163953Srrs } 1878163953Srrs /* compare the received digest with the computed digest */ 1879163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1880163953Srrs /* try the old cookie? */ 1881163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1882163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1883163953Srrs /* compute digest with old */ 1884169378Srrs (void)sctp_hmac_m(SCTP_HMAC, 1885163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1886163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1887163953Srrs /* compare */ 1888163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1889163953Srrs cookie_ok = 1; 1890163953Srrs } 1891163953Srrs } else { 1892163953Srrs cookie_ok = 1; 1893163953Srrs } 1894163953Srrs 1895163953Srrs /* 1896163953Srrs * Now before we continue we must reconstruct our mbuf so that 1897163953Srrs * normal processing of any other chunks will work. 1898163953Srrs */ 1899163953Srrs { 1900163953Srrs struct mbuf *m_at; 1901163953Srrs 1902163953Srrs m_at = m; 1903165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1904165647Srrs m_at = SCTP_BUF_NEXT(m_at); 1905163953Srrs } 1906165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 1907163953Srrs } 1908163953Srrs 1909163953Srrs if (cookie_ok == 0) { 1910169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 1911169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 1912169420Srrs "offset = %u, cookie_offset = %u, sig_offset = %u\n", 1913169420Srrs (uint32_t) offset, cookie_offset, sig_offset); 1914163953Srrs return (NULL); 1915163953Srrs } 1916163953Srrs /* 1917163953Srrs * check the cookie timestamps to be sure it's not stale 1918163953Srrs */ 1919169378Srrs (void)SCTP_GETTIME_TIMEVAL(&now); 1920163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 1921169655Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 1922163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 1923163953Srrs if (timevalcmp(&now, &time_expires, >)) { 1924163953Srrs /* cookie is stale! */ 1925163953Srrs struct mbuf *op_err; 1926163953Srrs struct sctp_stale_cookie_msg *scm; 1927163953Srrs uint32_t tim; 1928163953Srrs 1929163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 1930165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1931163953Srrs if (op_err == NULL) { 1932163953Srrs /* FOOBAR */ 1933163953Srrs return (NULL); 1934163953Srrs } 1935163953Srrs /* pre-reserve some space */ 1936165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1937165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1938165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1939163953Srrs 1940163953Srrs /* Set the len */ 1941165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 1942163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 1943163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 1944163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 1945163953Srrs (sizeof(uint32_t)))); 1946163953Srrs /* seconds to usec */ 1947163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 1948163953Srrs /* add in usec */ 1949163953Srrs if (tim == 0) 1950163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 1951163953Srrs scm->time_usec = htonl(tim); 1952169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1953169352Srrs vrf_id, table_id); 1954163953Srrs return (NULL); 1955163953Srrs } 1956163953Srrs /* 1957163953Srrs * Now we must see with the lookup address if we have an existing 1958163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 1959163953Srrs * and a INIT collided with us and somewhere the peer sent the 1960163953Srrs * cookie on another address besides the single address our assoc 1961163953Srrs * had for him. In this case we will have one of the tie-tags set at 1962163953Srrs * least AND the address field in the cookie can be used to look it 1963163953Srrs * up. 1964163953Srrs */ 1965163953Srrs to = NULL; 1966163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 1967163953Srrs memset(&sin6, 0, sizeof(sin6)); 1968163953Srrs sin6.sin6_family = AF_INET6; 1969163953Srrs sin6.sin6_len = sizeof(sin6); 1970163953Srrs sin6.sin6_port = sh->src_port; 1971163953Srrs sin6.sin6_scope_id = cookie->scope_id; 1972163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 1973163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 1974163953Srrs to = (struct sockaddr *)&sin6; 1975163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 1976163953Srrs memset(&sin, 0, sizeof(sin)); 1977163953Srrs sin.sin_family = AF_INET; 1978163953Srrs sin.sin_len = sizeof(sin); 1979163953Srrs sin.sin_port = sh->src_port; 1980163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 1981163953Srrs to = (struct sockaddr *)&sin; 1982169420Srrs } else { 1983169420Srrs /* This should not happen */ 1984169420Srrs return (NULL); 1985163953Srrs } 1986163953Srrs if ((*stcb == NULL) && to) { 1987163953Srrs /* Yep, lets check */ 1988163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 1989163953Srrs if (*stcb == NULL) { 1990163953Srrs /* 1991163953Srrs * We should have only got back the same inp. If we 1992163953Srrs * got back a different ep we have a problem. The 1993163953Srrs * original findep got back l_inp and now 1994163953Srrs */ 1995163953Srrs if (l_inp != *inp_p) { 1996169420Srrs SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 1997163953Srrs } 1998165220Srrs } else { 1999165220Srrs if (*locked_tcb == NULL) { 2000165220Srrs /* 2001165220Srrs * In this case we found the assoc only 2002165220Srrs * after we locked the create lock. This 2003165220Srrs * means we are in a colliding case and we 2004165220Srrs * must make sure that we unlock the tcb if 2005165220Srrs * its one of the cases where we throw away 2006165220Srrs * the incoming packets. 2007165220Srrs */ 2008165220Srrs *locked_tcb = *stcb; 2009165220Srrs 2010165220Srrs /* 2011165220Srrs * We must also increment the inp ref count 2012165220Srrs * since the ref_count flags was set when we 2013165220Srrs * did not find the TCB, now we found it 2014165220Srrs * which reduces the refcount.. we must 2015165220Srrs * raise it back out to balance it all :-) 2016165220Srrs */ 2017165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2018165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2019169420Srrs SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2020165220Srrs (*stcb)->sctp_ep, l_inp); 2021165220Srrs } 2022165220Srrs } 2023163953Srrs } 2024163953Srrs } 2025169420Srrs if (to == NULL) 2026169420Srrs return (NULL); 2027169420Srrs 2028163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2029163953Srrs if (*stcb == NULL) { 2030163953Srrs /* this is the "normal" case... get a new TCB */ 2031163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2032163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2033169352Srrs auth_skipped, auth_offset, auth_len, vrf_id, table_id); 2034163953Srrs } else { 2035163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2036163953Srrs had_a_existing_tcb = 1; 2037163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2038163953Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification, 2039169352Srrs &sac_restart_id, vrf_id, table_id); 2040163953Srrs } 2041163953Srrs 2042163953Srrs if (*stcb == NULL) { 2043163953Srrs /* still no TCB... must be bad cookie-echo */ 2044163953Srrs return (NULL); 2045163953Srrs } 2046163953Srrs /* 2047163953Srrs * Ok, we built an association so confirm the address we sent the 2048163953Srrs * INIT-ACK to. 2049163953Srrs */ 2050163953Srrs netl = sctp_findnet(*stcb, to); 2051163953Srrs /* 2052163953Srrs * This code should in theory NOT run but 2053163953Srrs */ 2054163953Srrs if (netl == NULL) { 2055163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2056163953Srrs int ret; 2057163953Srrs 2058165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2059165220Srrs SCTP_IN_COOKIE_PROC); 2060163953Srrs netl = sctp_findnet(*stcb, to); 2061163953Srrs } 2062163953Srrs if (netl) { 2063163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2064163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2065169420Srrs (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2066163953Srrs netl); 2067163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2068163953Srrs (*stcb), 0, (void *)netl); 2069163953Srrs } 2070163953Srrs } 2071163953Srrs if (*stcb) { 2072163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2073163953Srrs *stcb, NULL); 2074163953Srrs } 2075163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2076163953Srrs if (!had_a_existing_tcb || 2077163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2078163953Srrs /* 2079163953Srrs * If we have a NEW cookie or the connect never 2080163953Srrs * reached the connected state during collision we 2081163953Srrs * must do the TCP accept thing. 2082163953Srrs */ 2083163953Srrs struct socket *so, *oso; 2084163953Srrs struct sctp_inpcb *inp; 2085163953Srrs 2086163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2087163953Srrs /* 2088163953Srrs * For a restart we will keep the same 2089163953Srrs * socket, no need to do anything. I THINK!! 2090163953Srrs */ 2091163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2092163953Srrs return (m); 2093163953Srrs } 2094163953Srrs oso = (*inp_p)->sctp_socket; 2095163953Srrs /* 2096163953Srrs * We do this to keep the sockets side happy durin 2097163953Srrs * the sonewcon ONLY. 2098163953Srrs */ 2099163953Srrs NET_LOCK_GIANT(); 2100163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2101166086Srrs so = sonewconn(oso, 0 2102163953Srrs ); 2103163953Srrs NET_UNLOCK_GIANT(); 2104163953Srrs SCTP_INP_WLOCK((*stcb)->sctp_ep); 2105163953Srrs SCTP_TCB_LOCK((*stcb)); 2106163953Srrs SCTP_INP_WUNLOCK((*stcb)->sctp_ep); 2107163953Srrs if (so == NULL) { 2108163953Srrs struct mbuf *op_err; 2109163953Srrs 2110163953Srrs /* Too many sockets */ 2111169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2112163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2113163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2114169352Srrs sh, op_err, vrf_id, 2115169352Srrs table_id); 2116165220Srrs sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2117163953Srrs return (NULL); 2118163953Srrs } 2119163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2120166086Srrs SCTP_INP_INCR_REF(inp); 2121166086Srrs /* 2122166086Srrs * We add the unbound flag here so that if we get an 2123166086Srrs * soabort() before we get the move_pcb done, we 2124166086Srrs * will properly cleanup. 2125166086Srrs */ 2126163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2127163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2128163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2129166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2130163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2131163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2132163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2133163953Srrs inp->sctp_socket = so; 2134163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2135163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2136163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2137163953Srrs inp->inp_starting_point_for_iterator = NULL; 2138163953Srrs /* 2139163953Srrs * copy in the authentication parameters from the 2140163953Srrs * original endpoint 2141163953Srrs */ 2142163953Srrs if (inp->sctp_ep.local_hmacs) 2143163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2144163953Srrs inp->sctp_ep.local_hmacs = 2145163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2146163953Srrs if (inp->sctp_ep.local_auth_chunks) 2147163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2148163953Srrs inp->sctp_ep.local_auth_chunks = 2149163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2150163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2151163953Srrs &inp->sctp_ep.shared_keys); 2152163953Srrs 2153163953Srrs /* 2154163953Srrs * Now we must move it from one hash table to 2155163953Srrs * another and get the tcb in the right place. 2156163953Srrs */ 2157163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2158170056Srrs 2159170056Srrs atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2160170056Srrs SCTP_TCB_UNLOCK((*stcb)); 2161170056Srrs 2162169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2163170056Srrs SCTP_TCB_LOCK((*stcb)); 2164170056Srrs atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2165163953Srrs 2166170056Srrs 2167166086Srrs /* 2168166086Srrs * now we must check to see if we were aborted while 2169166086Srrs * the move was going on and the lock/unlock 2170166086Srrs * happened. 2171166086Srrs */ 2172166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2173166086Srrs /* 2174166086Srrs * yep it was, we leave the assoc attached 2175166086Srrs * to the socket since the sctp_inpcb_free() 2176166086Srrs * call will send an abort for us. 2177166086Srrs */ 2178166086Srrs SCTP_INP_DECR_REF(inp); 2179166086Srrs return (NULL); 2180166086Srrs } 2181166086Srrs SCTP_INP_DECR_REF(inp); 2182163953Srrs /* Switch over to the new guy */ 2183163953Srrs *inp_p = inp; 2184166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2185163953Srrs 2186166086Srrs /* 2187166086Srrs * Pull it from the incomplete queue and wake the 2188166086Srrs * guy 2189166086Srrs */ 2190166086Srrs soisconnected(so); 2191163953Srrs return (m); 2192163953Srrs } 2193163953Srrs } 2194163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2195163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2196163953Srrs } 2197163953Srrs return (m); 2198163953Srrs} 2199163953Srrs 2200163953Srrsstatic void 2201163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2202163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2203163953Srrs{ 2204163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2205163953Srrs struct sctp_association *asoc; 2206163953Srrs 2207169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2208169420Srrs "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2209163953Srrs if (stcb == NULL) 2210163953Srrs return; 2211163953Srrs 2212163953Srrs asoc = &stcb->asoc; 2213163953Srrs 2214163953Srrs sctp_stop_all_cookie_timers(stcb); 2215163953Srrs /* process according to association state */ 2216163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2217163953Srrs /* state change only needed when I am in right state */ 2218169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2219163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2220163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 2221163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2222163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2223163953Srrs 2224163953Srrs } else { 2225163953Srrs asoc->state = SCTP_STATE_OPEN; 2226163953Srrs } 2227163953Srrs /* update RTO */ 2228163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2229163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2230163953Srrs if (asoc->overall_error_count == 0) { 2231163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2232163953Srrs &asoc->time_entered); 2233163953Srrs } 2234169378Srrs (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2235163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2236163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2237163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2238163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2239163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2240163953Srrs } 2241163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2242163953Srrs stcb, net); 2243163953Srrs /* 2244163953Srrs * since we did not send a HB make sure we don't double 2245163953Srrs * things 2246163953Srrs */ 2247163953Srrs net->hb_responded = 1; 2248163953Srrs 2249163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2250163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2251163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2252163953Srrs stcb->sctp_ep, stcb, NULL); 2253163953Srrs } 2254163953Srrs /* 2255163953Srrs * set ASCONF timer if ASCONFs are pending and allowed (eg. 2256163953Srrs * addresses changed when init/cookie echo in flight) 2257163953Srrs */ 2258163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2259163953Srrs (stcb->asoc.peer_supports_asconf) && 2260163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2261163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2262163953Srrs stcb->sctp_ep, stcb, 2263163953Srrs stcb->asoc.primary_destination); 2264163953Srrs } 2265163953Srrs } 2266163953Srrs /* Toss the cookie if I can */ 2267163953Srrs sctp_toss_old_cookies(stcb, asoc); 2268163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2269163953Srrs /* Restart the timer if we have pending data */ 2270163953Srrs struct sctp_tmit_chunk *chk; 2271163953Srrs 2272163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2273163953Srrs if (chk) { 2274163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2275163953Srrs stcb, chk->whoTo); 2276163953Srrs } 2277163953Srrs } 2278163953Srrs} 2279163953Srrs 2280163953Srrsstatic void 2281163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2282163953Srrs struct sctp_tcb *stcb) 2283163953Srrs{ 2284163953Srrs struct sctp_nets *net; 2285163953Srrs struct sctp_tmit_chunk *lchk; 2286163953Srrs uint32_t tsn; 2287163953Srrs 2288163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2289163953Srrs return; 2290163953Srrs } 2291163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2292163953Srrs tsn = ntohl(cp->tsn); 2293163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2294163953Srrs /* Also we make sure we disable the nonce_wait */ 2295163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2296163953Srrs if (lchk == NULL) { 2297163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2298163953Srrs } else { 2299163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2300163953Srrs } 2301163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2302163953Srrs stcb->asoc.nonce_sum_check = 0; 2303163953Srrs 2304163953Srrs /* Find where it was sent, if possible */ 2305163953Srrs net = NULL; 2306163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2307163953Srrs while (lchk) { 2308163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2309163953Srrs net = lchk->whoTo; 2310163953Srrs break; 2311163953Srrs } 2312163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2313163953Srrs break; 2314163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2315163953Srrs } 2316163953Srrs if (net == NULL) 2317163953Srrs /* default is we use the primary */ 2318163953Srrs net = stcb->asoc.primary_destination; 2319163953Srrs 2320163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2321163953Srrs#ifdef SCTP_CWND_MONITOR 2322163953Srrs int old_cwnd; 2323163953Srrs 2324163953Srrs old_cwnd = net->cwnd; 2325163953Srrs#endif 2326163953Srrs SCTP_STAT_INCR(sctps_ecnereducedcwnd); 2327163953Srrs net->ssthresh = net->cwnd / 2; 2328163953Srrs if (net->ssthresh < net->mtu) { 2329163953Srrs net->ssthresh = net->mtu; 2330163953Srrs /* here back off the timer as well, to slow us down */ 2331163953Srrs net->RTO <<= 2; 2332163953Srrs } 2333163953Srrs net->cwnd = net->ssthresh; 2334163953Srrs#ifdef SCTP_CWND_MONITOR 2335163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT); 2336163953Srrs#endif 2337163953Srrs /* 2338163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2339163953Srrs * the next sending seq i.e. the resync_tsn. 2340163953Srrs */ 2341163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2342163953Srrs } 2343163953Srrs /* 2344163953Srrs * We always send a CWR this way if our previous one was lost our 2345163953Srrs * peer will get an update, or if it is not time again to reduce we 2346163953Srrs * still get the cwr to the peer. 2347163953Srrs */ 2348163953Srrs sctp_send_cwr(stcb, net, tsn); 2349163953Srrs} 2350163953Srrs 2351163953Srrsstatic void 2352163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2353163953Srrs{ 2354163953Srrs /* 2355163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2356163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2357163953Srrs * If so remove it. 2358163953Srrs */ 2359163953Srrs struct sctp_tmit_chunk *chk; 2360163953Srrs struct sctp_ecne_chunk *ecne; 2361163953Srrs 2362163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2363163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2364163953Srrs continue; 2365163953Srrs } 2366163953Srrs /* 2367163953Srrs * Look for and remove if it is the right TSN. Since there 2368163953Srrs * is only ONE ECNE on the control queue at any one time we 2369163953Srrs * don't need to worry about more than one! 2370163953Srrs */ 2371163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2372163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2373163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2374163953Srrs /* this covers this ECNE, we can remove it */ 2375163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2376163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2377163953Srrs sctp_next); 2378163953Srrs if (chk->data) { 2379163953Srrs sctp_m_freem(chk->data); 2380163953Srrs chk->data = NULL; 2381163953Srrs } 2382163953Srrs stcb->asoc.ctrl_queue_cnt--; 2383163953Srrs sctp_free_remote_addr(chk->whoTo); 2384163953Srrs sctp_free_a_chunk(stcb, chk); 2385163953Srrs break; 2386163953Srrs } 2387163953Srrs } 2388163953Srrs} 2389163953Srrs 2390163953Srrsstatic void 2391163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2392163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2393163953Srrs{ 2394163953Srrs struct sctp_association *asoc; 2395163953Srrs 2396169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, 2397169420Srrs "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2398163953Srrs if (stcb == NULL) 2399163953Srrs return; 2400163953Srrs 2401163953Srrs asoc = &stcb->asoc; 2402163953Srrs /* process according to association state */ 2403163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2404163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2405163953Srrs SCTP_TCB_UNLOCK(stcb); 2406163953Srrs return; 2407163953Srrs } 2408163953Srrs /* notify upper layer protocol */ 2409163953Srrs if (stcb->sctp_socket) { 2410163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2411163953Srrs /* are the queues empty? they should be */ 2412163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2413163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2414163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2415164205Srrs sctp_report_all_outbound(stcb, 0); 2416163953Srrs } 2417163953Srrs } 2418163953Srrs /* stop the timer */ 2419165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_21); 2420163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2421163953Srrs /* free the TCB */ 2422165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2423163953Srrs return; 2424163953Srrs} 2425163953Srrs 2426163953Srrsstatic int 2427163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2428163953Srrs struct sctp_nets *net, uint8_t flg) 2429163953Srrs{ 2430163953Srrs switch (desc->chunk_type) { 2431163953Srrs case SCTP_DATA: 2432163953Srrs /* find the tsn to resend (possibly */ 2433163953Srrs { 2434163953Srrs uint32_t tsn; 2435163953Srrs struct sctp_tmit_chunk *tp1; 2436163953Srrs 2437163953Srrs tsn = ntohl(desc->tsn_ifany); 2438163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2439163953Srrs while (tp1) { 2440163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2441163953Srrs /* found it */ 2442163953Srrs break; 2443163953Srrs } 2444163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2445163953Srrs MAX_TSN)) { 2446163953Srrs /* not found */ 2447163953Srrs tp1 = NULL; 2448163953Srrs break; 2449163953Srrs } 2450163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2451163953Srrs } 2452163953Srrs if (tp1 == NULL) { 2453163953Srrs /* 2454163953Srrs * Do it the other way , aka without paying 2455163953Srrs * attention to queue seq order. 2456163953Srrs */ 2457163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2458163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2459163953Srrs while (tp1) { 2460163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2461163953Srrs /* found it */ 2462163953Srrs break; 2463163953Srrs } 2464163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2465163953Srrs } 2466163953Srrs } 2467163953Srrs if (tp1 == NULL) { 2468163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2469163953Srrs } 2470163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2471163953Srrs uint8_t *ddp; 2472163953Srrs 2473163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2474163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2475163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2476163953Srrs return (0); 2477163953Srrs } 2478163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2479163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2480163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2481163953Srrs return (0); 2482163953Srrs } 2483163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2484163953Srrs sizeof(struct sctp_data_chunk)); 2485163953Srrs { 2486163953Srrs unsigned int iii; 2487163953Srrs 2488163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2489163953Srrs iii++) { 2490163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2491163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2492163953Srrs return (-1); 2493163953Srrs } 2494163953Srrs } 2495163953Srrs } 2496163953Srrs /* 2497163953Srrs * We zero out the nonce so resync not 2498163953Srrs * needed 2499163953Srrs */ 2500163953Srrs tp1->rec.data.ect_nonce = 0; 2501163953Srrs 2502163953Srrs if (tp1->do_rtt) { 2503163953Srrs /* 2504163953Srrs * this guy had a RTO calculation 2505163953Srrs * pending on it, cancel it 2506163953Srrs */ 2507163953Srrs tp1->do_rtt = 0; 2508163953Srrs } 2509163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2510163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2511163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2512163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2513163953Srrs /* 2514163953Srrs * mark it as if we were doing a FR, since 2515163953Srrs * we will be getting gap ack reports behind 2516163953Srrs * the info from the router. 2517163953Srrs */ 2518163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2519163953Srrs /* 2520163953Srrs * mark the tsn with what sequences can 2521163953Srrs * cause a new FR. 2522163953Srrs */ 2523163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2524163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2525163953Srrs } else { 2526163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2527163953Srrs } 2528163953Srrs 2529163953Srrs /* restart the timer */ 2530163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2531165220Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2532163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2533163953Srrs stcb, tp1->whoTo); 2534163953Srrs 2535163953Srrs /* fix counts and things */ 2536165220Srrs#ifdef SCTP_FLIGHT_LOGGING 2537168709Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2538165220Srrs tp1->whoTo->flight_size, 2539165220Srrs tp1->book_size, 2540165220Srrs (uintptr_t) stcb, 2541165220Srrs tp1->rec.data.TSN_seq); 2542165220Srrs#endif 2543168709Srrs sctp_flight_size_decrease(tp1); 2544168709Srrs sctp_total_flight_decrease(stcb, tp1); 2545163953Srrs } { 2546163953Srrs /* audit code */ 2547163953Srrs unsigned int audit; 2548163953Srrs 2549163953Srrs audit = 0; 2550163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2551163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2552163953Srrs audit++; 2553163953Srrs } 2554163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2555163953Srrs sctp_next) { 2556163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2557163953Srrs audit++; 2558163953Srrs } 2559163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2560169420Srrs SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 2561163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2562163953Srrs#ifndef SCTP_AUDITING_ENABLED 2563163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2564163953Srrs#endif 2565163953Srrs } 2566163953Srrs } 2567163953Srrs } 2568163953Srrs break; 2569163953Srrs case SCTP_ASCONF: 2570163953Srrs { 2571163953Srrs struct sctp_tmit_chunk *asconf; 2572163953Srrs 2573163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2574163953Srrs sctp_next) { 2575163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2576163953Srrs break; 2577163953Srrs } 2578163953Srrs } 2579163953Srrs if (asconf) { 2580163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2581163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2582163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2583163953Srrs asconf->snd_count--; 2584163953Srrs } 2585163953Srrs } 2586163953Srrs break; 2587163953Srrs case SCTP_INITIATION: 2588163953Srrs /* resend the INIT */ 2589163953Srrs stcb->asoc.dropped_special_cnt++; 2590163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2591163953Srrs /* 2592163953Srrs * If we can get it in, in a few attempts we do 2593163953Srrs * this, otherwise we let the timer fire. 2594163953Srrs */ 2595163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2596165220Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2597163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2598163953Srrs } 2599163953Srrs break; 2600163953Srrs case SCTP_SELECTIVE_ACK: 2601163953Srrs /* resend the sack */ 2602163953Srrs sctp_send_sack(stcb); 2603163953Srrs break; 2604163953Srrs case SCTP_HEARTBEAT_REQUEST: 2605163953Srrs /* resend a demand HB */ 2606169378Srrs (void)sctp_send_hb(stcb, 1, net); 2607163953Srrs break; 2608163953Srrs case SCTP_SHUTDOWN: 2609163953Srrs sctp_send_shutdown(stcb, net); 2610163953Srrs break; 2611163953Srrs case SCTP_SHUTDOWN_ACK: 2612163953Srrs sctp_send_shutdown_ack(stcb, net); 2613163953Srrs break; 2614163953Srrs case SCTP_COOKIE_ECHO: 2615163953Srrs { 2616163953Srrs struct sctp_tmit_chunk *cookie; 2617163953Srrs 2618163953Srrs cookie = NULL; 2619163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2620163953Srrs sctp_next) { 2621163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2622163953Srrs break; 2623163953Srrs } 2624163953Srrs } 2625163953Srrs if (cookie) { 2626163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2627163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2628163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2629163953Srrs sctp_stop_all_cookie_timers(stcb); 2630163953Srrs } 2631163953Srrs } 2632163953Srrs break; 2633163953Srrs case SCTP_COOKIE_ACK: 2634163953Srrs sctp_send_cookie_ack(stcb); 2635163953Srrs break; 2636163953Srrs case SCTP_ASCONF_ACK: 2637163953Srrs /* resend last asconf ack */ 2638163953Srrs sctp_send_asconf_ack(stcb, 1); 2639163953Srrs break; 2640163953Srrs case SCTP_FORWARD_CUM_TSN: 2641163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2642163953Srrs break; 2643163953Srrs /* can't do anything with these */ 2644163953Srrs case SCTP_PACKET_DROPPED: 2645163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2646163953Srrs case SCTP_HEARTBEAT_ACK: 2647163953Srrs case SCTP_ABORT_ASSOCIATION: 2648163953Srrs case SCTP_OPERATION_ERROR: 2649163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2650163953Srrs case SCTP_ECN_ECHO: 2651163953Srrs case SCTP_ECN_CWR: 2652163953Srrs default: 2653163953Srrs break; 2654163953Srrs } 2655163953Srrs return (0); 2656163953Srrs} 2657163953Srrs 2658163953Srrsvoid 2659163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2660163953Srrs{ 2661163953Srrs int i; 2662163953Srrs uint16_t temp; 2663163953Srrs 2664163953Srrs /* 2665163953Srrs * We set things to 0xffff since this is the last delivered sequence 2666163953Srrs * and we will be sending in 0 after the reset. 2667163953Srrs */ 2668163953Srrs 2669163953Srrs if (number_entries) { 2670163953Srrs for (i = 0; i < number_entries; i++) { 2671163953Srrs temp = ntohs(list[i]); 2672163953Srrs if (temp >= stcb->asoc.streamincnt) { 2673163953Srrs continue; 2674163953Srrs } 2675163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2676163953Srrs } 2677163953Srrs } else { 2678163953Srrs list = NULL; 2679163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2680163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2681163953Srrs } 2682163953Srrs } 2683163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2684163953Srrs} 2685163953Srrs 2686163953Srrsstatic void 2687163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2688163953Srrs{ 2689163953Srrs int i; 2690163953Srrs 2691163953Srrs if (number_entries == 0) { 2692163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2693163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2694163953Srrs } 2695163953Srrs } else if (number_entries) { 2696163953Srrs for (i = 0; i < number_entries; i++) { 2697163953Srrs uint16_t temp; 2698163953Srrs 2699163953Srrs temp = ntohs(list[i]); 2700163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2701163953Srrs /* no such stream */ 2702163953Srrs continue; 2703163953Srrs } 2704163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2705163953Srrs } 2706163953Srrs } 2707163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2708163953Srrs} 2709163953Srrs 2710163953Srrs 2711163953Srrsstruct sctp_stream_reset_out_request * 2712163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2713163953Srrs{ 2714163953Srrs struct sctp_association *asoc; 2715163953Srrs struct sctp_stream_reset_out_req *req; 2716163953Srrs struct sctp_stream_reset_out_request *r; 2717163953Srrs struct sctp_tmit_chunk *chk; 2718163953Srrs int len, clen; 2719163953Srrs 2720163953Srrs asoc = &stcb->asoc; 2721163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2722169208Srrs asoc->stream_reset_outstanding = 0; 2723163953Srrs return (NULL); 2724163953Srrs } 2725163953Srrs if (stcb->asoc.str_reset == NULL) { 2726169208Srrs asoc->stream_reset_outstanding = 0; 2727163953Srrs return (NULL); 2728163953Srrs } 2729163953Srrs chk = stcb->asoc.str_reset; 2730163953Srrs if (chk->data == NULL) { 2731163953Srrs return (NULL); 2732163953Srrs } 2733163953Srrs if (bchk) { 2734163953Srrs /* he wants a copy of the chk pointer */ 2735163953Srrs *bchk = chk; 2736163953Srrs } 2737163953Srrs clen = chk->send_size; 2738163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2739163953Srrs r = &req->sr_req; 2740163953Srrs if (ntohl(r->request_seq) == seq) { 2741163953Srrs /* found it */ 2742163953Srrs return (r); 2743163953Srrs } 2744163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2745163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2746163953Srrs /* move to the next one, there can only be a max of two */ 2747163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2748163953Srrs if (ntohl(r->request_seq) == seq) { 2749163953Srrs return (r); 2750163953Srrs } 2751163953Srrs } 2752163953Srrs /* that seq is not here */ 2753163953Srrs return (NULL); 2754163953Srrs} 2755163953Srrs 2756163953Srrsstatic void 2757163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2758163953Srrs{ 2759163953Srrs struct sctp_association *asoc; 2760163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2761163953Srrs 2762163953Srrs if (stcb->asoc.str_reset == NULL) { 2763163953Srrs return; 2764163953Srrs } 2765166675Srrs asoc = &stcb->asoc; 2766166675Srrs 2767165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2768163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2769163953Srrs chk, 2770163953Srrs sctp_next); 2771163953Srrs if (chk->data) { 2772163953Srrs sctp_m_freem(chk->data); 2773163953Srrs chk->data = NULL; 2774163953Srrs } 2775163953Srrs asoc->ctrl_queue_cnt--; 2776163953Srrs sctp_free_remote_addr(chk->whoTo); 2777163953Srrs 2778163953Srrs sctp_free_a_chunk(stcb, chk); 2779163953Srrs stcb->asoc.str_reset = NULL; 2780163953Srrs} 2781163953Srrs 2782163953Srrs 2783163953Srrsstatic int 2784163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2785163953Srrs uint32_t seq, uint32_t action, 2786164139Srrs struct sctp_stream_reset_response *respin) 2787163953Srrs{ 2788163953Srrs uint16_t type; 2789163953Srrs int lparm_len; 2790163953Srrs struct sctp_association *asoc = &stcb->asoc; 2791163953Srrs struct sctp_tmit_chunk *chk; 2792163953Srrs struct sctp_stream_reset_out_request *srparam; 2793163953Srrs int number_entries; 2794163953Srrs 2795163953Srrs if (asoc->stream_reset_outstanding == 0) { 2796163953Srrs /* duplicate */ 2797163953Srrs return (0); 2798163953Srrs } 2799163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2800163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2801163953Srrs if (srparam) { 2802163953Srrs stcb->asoc.str_reset_seq_out++; 2803163953Srrs type = ntohs(srparam->ph.param_type); 2804163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2805163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2806164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2807163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2808163953Srrs if (asoc->stream_reset_outstanding) 2809163953Srrs asoc->stream_reset_outstanding--; 2810163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2811163953Srrs /* do it */ 2812163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2813163953Srrs } else { 2814163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2815163953Srrs } 2816163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2817163953Srrs /* Answered my request */ 2818164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2819163953Srrs if (asoc->stream_reset_outstanding) 2820163953Srrs asoc->stream_reset_outstanding--; 2821163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2822163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2823163953Srrs } 2824163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2825163953Srrs /** 2826163953Srrs * a) Adopt the new in tsn. 2827163953Srrs * b) reset the map 2828163953Srrs * c) Adopt the new out-tsn 2829163953Srrs */ 2830163953Srrs struct sctp_stream_reset_response_tsn *resp; 2831163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2832163953Srrs int abort_flag = 0; 2833163953Srrs 2834163953Srrs if (respin == NULL) { 2835163953Srrs /* huh ? */ 2836163953Srrs return (0); 2837163953Srrs } 2838163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2839163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2840163953Srrs asoc->stream_reset_outstanding--; 2841163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2842163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2843163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2844163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2845163953Srrs if (abort_flag) { 2846163953Srrs return (1); 2847163953Srrs } 2848163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2849163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2850163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2851163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2852163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2853163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2854163953Srrs 2855163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2856163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2857163953Srrs 2858163953Srrs } 2859163953Srrs } 2860163953Srrs /* get rid of the request and get the request flags */ 2861163953Srrs if (asoc->stream_reset_outstanding == 0) { 2862163953Srrs sctp_clean_up_stream_reset(stcb); 2863163953Srrs } 2864163953Srrs } 2865163953Srrs } 2866163953Srrs return (0); 2867163953Srrs} 2868163953Srrs 2869163953Srrsstatic void 2870163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2871163953Srrs struct sctp_tmit_chunk *chk, 2872163953Srrs struct sctp_stream_reset_in_request *req) 2873163953Srrs{ 2874163953Srrs uint32_t seq; 2875163953Srrs int len, i; 2876163953Srrs int number_entries; 2877163953Srrs uint16_t temp; 2878163953Srrs 2879163953Srrs /* 2880163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2881163953Srrs * seq_in is right. 2882163953Srrs */ 2883163953Srrs struct sctp_association *asoc = &stcb->asoc; 2884163953Srrs 2885163953Srrs seq = ntohl(req->request_seq); 2886163953Srrs if (asoc->str_reset_seq_in == seq) { 2887163953Srrs if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2888163953Srrs len = ntohs(req->ph.param_length); 2889163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2890163953Srrs for (i = 0; i < number_entries; i++) { 2891163953Srrs temp = ntohs(req->list_of_streams[i]); 2892163953Srrs req->list_of_streams[i] = temp; 2893163953Srrs } 2894163953Srrs /* move the reset action back one */ 2895163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2896163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2897163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2898163953Srrs asoc->str_reset_seq_out, 2899163953Srrs seq, (asoc->sending_seq - 1)); 2900163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2901163953Srrs asoc->str_reset = chk; 2902163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 2903163953Srrs stcb->asoc.stream_reset_outstanding++; 2904163953Srrs } else { 2905163953Srrs /* Can't do it, since we have sent one out */ 2906163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2907163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 2908163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2909163953Srrs } 2910163953Srrs asoc->str_reset_seq_in++; 2911163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 2912163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 2913163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 2914163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 2915163953Srrs } else { 2916163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 2917163953Srrs } 2918163953Srrs} 2919163953Srrs 2920163953Srrsstatic int 2921163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 2922163953Srrs struct sctp_tmit_chunk *chk, 2923163953Srrs struct sctp_stream_reset_tsn_request *req) 2924163953Srrs{ 2925163953Srrs /* reset all in and out and update the tsn */ 2926163953Srrs /* 2927163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 2928163953Srrs * and set cum-ack to it. Also process this selected number as a 2929163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 2930163953Srrs */ 2931163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2932163953Srrs struct sctp_association *asoc = &stcb->asoc; 2933163953Srrs int abort_flag = 0; 2934163953Srrs uint32_t seq; 2935163953Srrs 2936163953Srrs seq = ntohl(req->request_seq); 2937163953Srrs if (asoc->str_reset_seq_in == seq) { 2938163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2939163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2940163953Srrs fwdtsn.ch.chunk_flags = 0; 2941163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 2942163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2943163953Srrs if (abort_flag) { 2944163953Srrs return (1); 2945163953Srrs } 2946163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 2947163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2948163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 2949163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2950163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 2951163953Srrs /* save off historical data for retrans */ 2952163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 2953163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 2954163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 2955163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 2956163953Srrs 2957163953Srrs sctp_add_stream_reset_result_tsn(chk, 2958163953Srrs ntohl(req->request_seq), 2959163953Srrs SCTP_STREAM_RESET_PERFORMED, 2960163953Srrs stcb->asoc.sending_seq, 2961163953Srrs stcb->asoc.mapping_array_base_tsn); 2962163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2963163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2964163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 2965163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2966163953Srrs 2967163953Srrs asoc->str_reset_seq_in++; 2968163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 2969163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 2970163953Srrs stcb->asoc.last_sending_seq[0], 2971163953Srrs stcb->asoc.last_base_tsnsent[0] 2972163953Srrs ); 2973163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 2974163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 2975163953Srrs stcb->asoc.last_sending_seq[1], 2976163953Srrs stcb->asoc.last_base_tsnsent[1] 2977163953Srrs ); 2978163953Srrs } else { 2979163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 2980163953Srrs } 2981163953Srrs return (0); 2982163953Srrs} 2983163953Srrs 2984163953Srrsstatic void 2985163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 2986163953Srrs struct sctp_tmit_chunk *chk, 2987163953Srrs struct sctp_stream_reset_out_request *req) 2988163953Srrs{ 2989163953Srrs uint32_t seq, tsn; 2990163953Srrs int number_entries, len; 2991163953Srrs struct sctp_association *asoc = &stcb->asoc; 2992163953Srrs 2993163953Srrs seq = ntohl(req->request_seq); 2994163953Srrs 2995163953Srrs /* now if its not a duplicate we process it */ 2996163953Srrs if (asoc->str_reset_seq_in == seq) { 2997163953Srrs len = ntohs(req->ph.param_length); 2998163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 2999163953Srrs /* 3000163953Srrs * the sender is resetting, handle the list issue.. we must 3001163953Srrs * a) verify if we can do the reset, if so no problem b) If 3002163953Srrs * we can't do the reset we must copy the request. c) queue 3003163953Srrs * it, and setup the data in processor to trigger it off 3004163953Srrs * when needed and dequeue all the queued data. 3005163953Srrs */ 3006163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3007163953Srrs 3008163953Srrs /* move the reset action back one */ 3009163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3010163953Srrs if ((tsn == asoc->cumulative_tsn) || 3011163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3012163953Srrs /* we can do it now */ 3013163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3014163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3015163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3016163953Srrs } else { 3017163953Srrs /* 3018163953Srrs * we must queue it up and thus wait for the TSN's 3019163953Srrs * to arrive that are at or before tsn 3020163953Srrs */ 3021163953Srrs struct sctp_stream_reset_list *liste; 3022163953Srrs int siz; 3023163953Srrs 3024163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3025163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3026170091Srrs siz, SCTP_M_STRESET); 3027163953Srrs if (liste == NULL) { 3028163953Srrs /* gak out of memory */ 3029163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3030163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3031163953Srrs return; 3032163953Srrs } 3033163953Srrs liste->tsn = tsn; 3034163953Srrs liste->number_entries = number_entries; 3035163953Srrs memcpy(&liste->req, req, 3036163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3037163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3038163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3039163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3040163953Srrs } 3041163953Srrs asoc->str_reset_seq_in++; 3042163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3043163953Srrs /* 3044163953Srrs * one seq back, just echo back last action since my 3045163953Srrs * response was lost. 3046163953Srrs */ 3047163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3048163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3049163953Srrs /* 3050163953Srrs * two seq back, just echo back last action since my 3051163953Srrs * response was lost. 3052163953Srrs */ 3053163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3054163953Srrs } else { 3055163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3056163953Srrs } 3057163953Srrs} 3058163953Srrs 3059163953Srrsstatic int 3060163953Srrssctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_out_req *sr_req) 3061163953Srrs{ 3062163953Srrs int chk_length, param_len, ptype; 3063163953Srrs uint32_t seq; 3064163953Srrs int num_req = 0; 3065163953Srrs struct sctp_tmit_chunk *chk; 3066163953Srrs struct sctp_chunkhdr *ch; 3067163953Srrs struct sctp_paramhdr *ph; 3068166675Srrs int ret_code = 0; 3069166675Srrs int num_param = 0; 3070163953Srrs 3071163953Srrs /* now it may be a reset or a reset-response */ 3072163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3073163953Srrs 3074163953Srrs /* setup for adding the response */ 3075163953Srrs sctp_alloc_a_chunk(stcb, chk); 3076163953Srrs if (chk == NULL) { 3077163953Srrs return (ret_code); 3078163953Srrs } 3079163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3080169208Srrs chk->rec.chunk_id.can_take_data = 0; 3081163953Srrs chk->asoc = &stcb->asoc; 3082163953Srrs chk->no_fr_allowed = 0; 3083163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3084166023Srrs chk->book_size_scale = 0; 3085165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3086163953Srrs if (chk->data == NULL) { 3087163953Srrsstrres_nochunk: 3088163953Srrs if (chk->data) { 3089163953Srrs sctp_m_freem(chk->data); 3090163953Srrs chk->data = NULL; 3091163953Srrs } 3092163953Srrs sctp_free_a_chunk(stcb, chk); 3093163953Srrs return (ret_code); 3094163953Srrs } 3095165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3096163953Srrs 3097163953Srrs /* setup chunk parameters */ 3098163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3099163953Srrs chk->snd_count = 0; 3100163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3101163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3102163953Srrs 3103163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3104163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3105163953Srrs ch->chunk_flags = 0; 3106163953Srrs ch->chunk_length = htons(chk->send_size); 3107165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3108163953Srrs ph = (struct sctp_paramhdr *)&sr_req->sr_req; 3109163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3110163953Srrs param_len = ntohs(ph->param_length); 3111163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3112163953Srrs /* bad param */ 3113163953Srrs break; 3114163953Srrs } 3115163953Srrs ptype = ntohs(ph->param_type); 3116163953Srrs num_param++; 3117163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3118163953Srrs /* hit the max of parameters already sorry.. */ 3119163953Srrs break; 3120163953Srrs } 3121163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3122163953Srrs struct sctp_stream_reset_out_request *req_out; 3123163953Srrs 3124163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3125163953Srrs num_req++; 3126163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3127163953Srrs seq = ntohl(req_out->response_seq); 3128163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3129163953Srrs /* implicit ack */ 3130169420Srrs (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3131163953Srrs } 3132163953Srrs } 3133163953Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out); 3134163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3135163953Srrs struct sctp_stream_reset_in_request *req_in; 3136163953Srrs 3137163953Srrs num_req++; 3138163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3139163953Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in); 3140163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3141163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3142163953Srrs 3143163953Srrs num_req++; 3144163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3145163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3146163953Srrs ret_code = 1; 3147163953Srrs goto strres_nochunk; 3148163953Srrs } 3149163953Srrs /* no more */ 3150163953Srrs break; 3151163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3152163953Srrs struct sctp_stream_reset_response *resp; 3153163953Srrs uint32_t result; 3154163953Srrs 3155163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3156163953Srrs seq = ntohl(resp->response_seq); 3157163953Srrs result = ntohl(resp->result); 3158163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3159163953Srrs ret_code = 1; 3160163953Srrs goto strres_nochunk; 3161163953Srrs } 3162163953Srrs } else { 3163163953Srrs break; 3164163953Srrs } 3165163953Srrs 3166163953Srrs ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len)); 3167163953Srrs chk_length -= SCTP_SIZE32(param_len); 3168163953Srrs } 3169163953Srrs if (num_req == 0) { 3170163953Srrs /* we have no response free the stuff */ 3171163953Srrs goto strres_nochunk; 3172163953Srrs } 3173163953Srrs /* ok we have a chunk to link in */ 3174163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3175163953Srrs chk, 3176163953Srrs sctp_next); 3177163953Srrs stcb->asoc.ctrl_queue_cnt++; 3178163953Srrs return (ret_code); 3179163953Srrs} 3180163953Srrs 3181163953Srrs/* 3182163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3183163953Srrs * to handle this, either we get the whole packet and must disect it 3184163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3185163953Srrs * from a middle box that did the disectting for us. 3186163953Srrs */ 3187163953Srrsstatic void 3188163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3189163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3190163953Srrs{ 3191163953Srrs uint32_t bottle_bw, on_queue; 3192163953Srrs uint16_t trunc_len; 3193163953Srrs unsigned int chlen; 3194163953Srrs unsigned int at; 3195163953Srrs struct sctp_chunk_desc desc; 3196163953Srrs struct sctp_chunkhdr *ch; 3197163953Srrs 3198163953Srrs chlen = ntohs(cp->ch.chunk_length); 3199163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3200163953Srrs /* XXX possible chlen underflow */ 3201163953Srrs if (chlen == 0) { 3202163953Srrs ch = NULL; 3203163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3204163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3205163953Srrs } else { 3206163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3207163953Srrs chlen -= sizeof(struct sctphdr); 3208163953Srrs /* XXX possible chlen underflow */ 3209163953Srrs memset(&desc, 0, sizeof(desc)); 3210163953Srrs } 3211163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3212163953Srrs /* now the chunks themselves */ 3213163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3214163953Srrs desc.chunk_type = ch->chunk_type; 3215163953Srrs /* get amount we need to move */ 3216163953Srrs at = ntohs(ch->chunk_length); 3217163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3218163953Srrs /* corrupt chunk, maybe at the end? */ 3219163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3220163953Srrs break; 3221163953Srrs } 3222163953Srrs if (trunc_len == 0) { 3223163953Srrs /* we are supposed to have all of it */ 3224163953Srrs if (at > chlen) { 3225163953Srrs /* corrupt skip it */ 3226163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3227163953Srrs break; 3228163953Srrs } 3229163953Srrs } else { 3230163953Srrs /* is there enough of it left ? */ 3231163953Srrs if (desc.chunk_type == SCTP_DATA) { 3232163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3233163953Srrs sizeof(desc.data_bytes))) { 3234163953Srrs break; 3235163953Srrs } 3236163953Srrs } else { 3237163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3238163953Srrs break; 3239163953Srrs } 3240163953Srrs } 3241163953Srrs } 3242163953Srrs if (desc.chunk_type == SCTP_DATA) { 3243163953Srrs /* can we get out the tsn? */ 3244163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3245163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3246163953Srrs 3247163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3248163953Srrs /* yep */ 3249163953Srrs struct sctp_data_chunk *dcp; 3250163953Srrs uint8_t *ddp; 3251163953Srrs unsigned int iii; 3252163953Srrs 3253163953Srrs dcp = (struct sctp_data_chunk *)ch; 3254163953Srrs ddp = (uint8_t *) (dcp + 1); 3255163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3256163953Srrs desc.data_bytes[iii] = ddp[iii]; 3257163953Srrs } 3258163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3259163953Srrs } else { 3260163953Srrs /* nope we are done. */ 3261163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3262163953Srrs break; 3263163953Srrs } 3264163953Srrs } else { 3265163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3266163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3267163953Srrs } 3268163953Srrs 3269163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3270163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3271163953Srrs break; 3272163953Srrs } 3273163953Srrs if (SCTP_SIZE32(at) > chlen) { 3274163953Srrs break; 3275163953Srrs } 3276163953Srrs chlen -= SCTP_SIZE32(at); 3277163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3278163953Srrs /* done, none left */ 3279163953Srrs break; 3280163953Srrs } 3281163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3282163953Srrs } 3283163953Srrs /* Now update any rwnd --- possibly */ 3284163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3285163953Srrs /* From a peer, we get a rwnd report */ 3286163953Srrs uint32_t a_rwnd; 3287163953Srrs 3288163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3289163953Srrs 3290163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3291163953Srrs on_queue = ntohl(cp->current_onq); 3292163953Srrs if (bottle_bw && on_queue) { 3293163953Srrs /* a rwnd report is in here */ 3294163953Srrs if (bottle_bw > on_queue) 3295163953Srrs a_rwnd = bottle_bw - on_queue; 3296163953Srrs else 3297163953Srrs a_rwnd = 0; 3298163953Srrs 3299163953Srrs if (a_rwnd == 0) 3300163953Srrs stcb->asoc.peers_rwnd = 0; 3301163953Srrs else { 3302163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3303163953Srrs stcb->asoc.peers_rwnd = 3304163953Srrs a_rwnd - stcb->asoc.total_flight; 3305163953Srrs } else { 3306163953Srrs stcb->asoc.peers_rwnd = 0; 3307163953Srrs } 3308163953Srrs if (stcb->asoc.peers_rwnd < 3309163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3310163953Srrs /* SWS sender side engages */ 3311163953Srrs stcb->asoc.peers_rwnd = 0; 3312163953Srrs } 3313163953Srrs } 3314163953Srrs } 3315163953Srrs } else { 3316163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3317163953Srrs } 3318163953Srrs 3319163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3320163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3321163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3322163953Srrs (stcb->asoc.sat_network)) { 3323163953Srrs /* 3324163953Srrs * This is debateable but for sat networks it makes sense 3325163953Srrs * Note if a T3 timer has went off, we will prohibit any 3326163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3327163953Srrs */ 3328163953Srrs uint32_t bw_avail; 3329163953Srrs int rtt, incr; 3330163953Srrs 3331163953Srrs#ifdef SCTP_CWND_MONITOR 3332163953Srrs int old_cwnd = net->cwnd; 3333163953Srrs 3334163953Srrs#endif 3335163953Srrs /* need real RTT for this calc */ 3336163953Srrs rtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 3337163953Srrs /* get bottle neck bw */ 3338163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3339163953Srrs /* and whats on queue */ 3340163953Srrs on_queue = ntohl(cp->current_onq); 3341163953Srrs /* 3342163953Srrs * adjust the on-queue if our flight is more it could be 3343163953Srrs * that the router has not yet gotten data "in-flight" to it 3344163953Srrs */ 3345163953Srrs if (on_queue < net->flight_size) 3346163953Srrs on_queue = net->flight_size; 3347163953Srrs 3348163953Srrs /* calculate the available space */ 3349163953Srrs bw_avail = (bottle_bw * rtt) / 1000; 3350163953Srrs if (bw_avail > bottle_bw) { 3351163953Srrs /* 3352163953Srrs * Cap the growth to no more than the bottle neck. 3353163953Srrs * This can happen as RTT slides up due to queues. 3354163953Srrs * It also means if you have more than a 1 second 3355163953Srrs * RTT with a empty queue you will be limited to the 3356163953Srrs * bottle_bw per second no matter if other points 3357163953Srrs * have 1/2 the RTT and you could get more out... 3358163953Srrs */ 3359163953Srrs bw_avail = bottle_bw; 3360163953Srrs } 3361163953Srrs if (on_queue > bw_avail) { 3362163953Srrs /* 3363163953Srrs * No room for anything else don't allow anything 3364163953Srrs * else to be "added to the fire". 3365163953Srrs */ 3366163953Srrs int seg_inflight, seg_onqueue, my_portion; 3367163953Srrs 3368163953Srrs net->partial_bytes_acked = 0; 3369163953Srrs 3370163953Srrs /* how much are we over queue size? */ 3371163953Srrs incr = on_queue - bw_avail; 3372163953Srrs if (stcb->asoc.seen_a_sack_this_pkt) { 3373163953Srrs /* 3374163953Srrs * undo any cwnd adjustment that the sack 3375163953Srrs * might have made 3376163953Srrs */ 3377163953Srrs net->cwnd = net->prev_cwnd; 3378163953Srrs } 3379163953Srrs /* Now how much of that is mine? */ 3380163953Srrs seg_inflight = net->flight_size / net->mtu; 3381163953Srrs seg_onqueue = on_queue / net->mtu; 3382163953Srrs my_portion = (incr * seg_inflight) / seg_onqueue; 3383163953Srrs 3384163953Srrs /* Have I made an adjustment already */ 3385163953Srrs if (net->cwnd > net->flight_size) { 3386163953Srrs /* 3387163953Srrs * for this flight I made an adjustment we 3388163953Srrs * need to decrease the portion by a share 3389163953Srrs * our previous adjustment. 3390163953Srrs */ 3391163953Srrs int diff_adj; 3392163953Srrs 3393163953Srrs diff_adj = net->cwnd - net->flight_size; 3394163953Srrs if (diff_adj > my_portion) 3395163953Srrs my_portion = 0; 3396163953Srrs else 3397163953Srrs my_portion -= diff_adj; 3398163953Srrs } 3399163953Srrs /* 3400163953Srrs * back down to the previous cwnd (assume we have 3401163953Srrs * had a sack before this packet). minus what ever 3402163953Srrs * portion of the overage is my fault. 3403163953Srrs */ 3404163953Srrs net->cwnd -= my_portion; 3405163953Srrs 3406163953Srrs /* we will NOT back down more than 1 MTU */ 3407163953Srrs if (net->cwnd <= net->mtu) { 3408163953Srrs net->cwnd = net->mtu; 3409163953Srrs } 3410163953Srrs /* force into CA */ 3411163953Srrs net->ssthresh = net->cwnd - 1; 3412163953Srrs } else { 3413163953Srrs /* 3414163953Srrs * Take 1/4 of the space left or max burst up .. 3415163953Srrs * whichever is less. 3416163953Srrs */ 3417163953Srrs incr = min((bw_avail - on_queue) >> 2, 3418170140Srrs stcb->asoc.max_burst * net->mtu); 3419163953Srrs net->cwnd += incr; 3420163953Srrs } 3421163953Srrs if (net->cwnd > bw_avail) { 3422163953Srrs /* We can't exceed the pipe size */ 3423163953Srrs net->cwnd = bw_avail; 3424163953Srrs } 3425163953Srrs if (net->cwnd < net->mtu) { 3426163953Srrs /* We always have 1 MTU */ 3427163953Srrs net->cwnd = net->mtu; 3428163953Srrs } 3429163953Srrs#ifdef SCTP_CWND_MONITOR 3430163953Srrs if (net->cwnd - old_cwnd != 0) { 3431163953Srrs /* log only changes */ 3432163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), 3433163953Srrs SCTP_CWND_LOG_FROM_SAT); 3434163953Srrs } 3435163953Srrs#endif 3436163953Srrs } 3437163953Srrs} 3438163953Srrs 3439163953Srrs/* 3440163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3441163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3442163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3443163953Srrs * length of the complete packet outputs: - length: modified to remaining 3444163953Srrs * length after control processing - netp: modified to new sctp_nets after 3445163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3446163953Srrs * bad packet,...) otherwise return the tcb for this packet 3447163953Srrs */ 3448170099Srrs#ifdef __GNUC__ 3449170099Srrs__attribute__((noinline)) 3450170099Srrs#endif 3451170099Srrs static struct sctp_tcb * 3452170099Srrs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3453170099Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3454170099Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3455170099Srrs uint32_t vrf_id, uint32_t table_id) 3456163953Srrs{ 3457163953Srrs struct sctp_association *asoc; 3458163953Srrs uint32_t vtag_in; 3459163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3460170140Srrs uint32_t chk_length; 3461163953Srrs int ret; 3462168299Srrs int abort_no_unlock = 0; 3463163953Srrs 3464163953Srrs /* 3465163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3466163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3467163953Srrs * until we get into jumbo grams and such.. 3468163953Srrs */ 3469166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3470163953Srrs struct sctp_tcb *locked_tcb = stcb; 3471163953Srrs int got_auth = 0; 3472163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3473163953Srrs int auth_skipped = 0; 3474163953Srrs 3475169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3476169420Srrs iphlen, *offset, length, stcb); 3477163953Srrs 3478163953Srrs /* validate chunk header length... */ 3479163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3480170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 3481170056Srrs ntohs(ch->chunk_length)); 3482170091Srrs if (locked_tcb) { 3483170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3484170091Srrs } 3485163953Srrs return (NULL); 3486163953Srrs } 3487163953Srrs /* 3488163953Srrs * validate the verification tag 3489163953Srrs */ 3490163953Srrs vtag_in = ntohl(sh->v_tag); 3491163953Srrs 3492165220Srrs if (locked_tcb) { 3493165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3494165220Srrs } 3495163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3496170056Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 3497170056Srrs ntohs(ch->chunk_length), vtag_in); 3498163953Srrs if (vtag_in != 0) { 3499163953Srrs /* protocol error- silently discard... */ 3500163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3501169378Srrs if (locked_tcb) { 3502163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3503169378Srrs } 3504163953Srrs return (NULL); 3505163953Srrs } 3506163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3507163953Srrs /* 3508163953Srrs * If there is no stcb, skip the AUTH chunk and process 3509163953Srrs * later after a stcb is found (to validate the lookup was 3510163953Srrs * valid. 3511163953Srrs */ 3512163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3513163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3514163953Srrs /* save this chunk for later processing */ 3515163953Srrs auth_skipped = 1; 3516163953Srrs auth_offset = *offset; 3517163953Srrs auth_len = ntohs(ch->chunk_length); 3518163953Srrs 3519163953Srrs /* (temporarily) move past this chunk */ 3520163953Srrs *offset += SCTP_SIZE32(auth_len); 3521163953Srrs if (*offset >= length) { 3522163953Srrs /* no more data left in the mbuf chain */ 3523163953Srrs *offset = length; 3524170091Srrs if (locked_tcb) { 3525170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3526170091Srrs } 3527163953Srrs return (NULL); 3528163953Srrs } 3529163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3530163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3531163953Srrs } 3532169420Srrs if (ch == NULL) { 3533169420Srrs /* Help */ 3534169420Srrs *offset = length; 3535170091Srrs if (locked_tcb) { 3536170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3537170091Srrs } 3538169420Srrs return (NULL); 3539169420Srrs } 3540163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3541163953Srrs goto process_control_chunks; 3542163953Srrs } 3543163953Srrs /* 3544163953Srrs * first check if it's an ASCONF with an unknown src addr we 3545163953Srrs * need to look inside to find the association 3546163953Srrs */ 3547163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3548163953Srrs /* inp's refcount may be reduced */ 3549163953Srrs SCTP_INP_INCR_REF(inp); 3550163953Srrs 3551163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3552163953Srrs *offset, sh, &inp, netp); 3553163953Srrs if (stcb == NULL) { 3554163953Srrs /* 3555163953Srrs * reduce inp's refcount if not reduced in 3556163953Srrs * sctp_findassociation_ep_asconf(). 3557163953Srrs */ 3558163953Srrs SCTP_INP_DECR_REF(inp); 3559163953Srrs } 3560163953Srrs /* now go back and verify any auth chunk to be sure */ 3561163953Srrs if (auth_skipped && (stcb != NULL)) { 3562163953Srrs struct sctp_auth_chunk *auth; 3563163953Srrs 3564163953Srrs auth = (struct sctp_auth_chunk *) 3565163953Srrs sctp_m_getptr(m, auth_offset, 3566163953Srrs auth_len, chunk_buf); 3567163953Srrs got_auth = 1; 3568163953Srrs auth_skipped = 0; 3569169420Srrs if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 3570163953Srrs auth_offset)) { 3571163953Srrs /* auth HMAC failed so dump it */ 3572163953Srrs *offset = length; 3573170091Srrs if (locked_tcb) { 3574170091Srrs SCTP_TCB_UNLOCK(locked_tcb); 3575170091Srrs } 3576163953Srrs return (NULL); 3577163953Srrs } else { 3578163953Srrs /* remaining chunks are HMAC checked */ 3579163953Srrs stcb->asoc.authenticated = 1; 3580163953Srrs } 3581163953Srrs } 3582163953Srrs } 3583163953Srrs if (stcb == NULL) { 3584163953Srrs /* no association, so it's out of the blue... */ 3585169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3586169352Srrs vrf_id, table_id); 3587163953Srrs *offset = length; 3588169378Srrs if (locked_tcb) { 3589163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3590169378Srrs } 3591163953Srrs return (NULL); 3592163953Srrs } 3593163953Srrs asoc = &stcb->asoc; 3594163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3595163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3596163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3597163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3598163953Srrs if ((vtag_in == asoc->my_vtag) || 3599163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3600163953Srrs (vtag_in == asoc->peer_vtag))) { 3601163953Srrs /* this is valid */ 3602163953Srrs } else { 3603163953Srrs /* drop this packet... */ 3604163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3605169378Srrs if (locked_tcb) { 3606163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3607169378Srrs } 3608163953Srrs return (NULL); 3609163953Srrs } 3610163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3611163953Srrs if (vtag_in != asoc->my_vtag) { 3612163953Srrs /* 3613163953Srrs * this could be a stale SHUTDOWN-ACK or the 3614163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3615163953Srrs * is still hung; we have started a new asoc 3616163953Srrs * but it won't complete until the shutdown 3617163953Srrs * is completed 3618163953Srrs */ 3619169378Srrs if (locked_tcb) { 3620163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3621169378Srrs } 3622163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3623169352Srrs NULL, vrf_id, table_id); 3624163953Srrs return (NULL); 3625163953Srrs } 3626163953Srrs } else { 3627163953Srrs /* for all other chunks, vtag must match */ 3628163953Srrs if (vtag_in != asoc->my_vtag) { 3629163953Srrs /* invalid vtag... */ 3630169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 3631169420Srrs "invalid vtag: %xh, expect %xh\n", 3632169420Srrs vtag_in, asoc->my_vtag); 3633163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3634169378Srrs if (locked_tcb) { 3635163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3636169378Srrs } 3637163953Srrs *offset = length; 3638163953Srrs return (NULL); 3639163953Srrs } 3640163953Srrs } 3641163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3642163953Srrs /* 3643163953Srrs * process all control chunks... 3644163953Srrs */ 3645163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3646163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3647163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3648163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3649163953Srrs stcb->asoc.overall_error_count = 0; 3650163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3651163953Srrs *netp); 3652163953Srrs } 3653163953Srrsprocess_control_chunks: 3654163953Srrs while (IS_SCTP_CONTROL(ch)) { 3655163953Srrs /* validate chunk length */ 3656163953Srrs chk_length = ntohs(ch->chunk_length); 3657169420Srrs SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 3658169420Srrs ch->chunk_type, chk_length); 3659170140Srrs if (chk_length < sizeof(*ch) || 3660170140Srrs (*offset + (int)chk_length) > length) { 3661163953Srrs *offset = length; 3662169378Srrs if (locked_tcb) { 3663163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3664169378Srrs } 3665163953Srrs return (NULL); 3666163953Srrs } 3667163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3668163953Srrs /* 3669163953Srrs * INIT-ACK only gets the init ack "header" portion only 3670163953Srrs * because we don't have to process the peer's COOKIE. All 3671163953Srrs * others get a complete chunk. 3672163953Srrs */ 3673169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3674169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3675163953Srrs /* get an init-ack chunk */ 3676163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3677163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3678163953Srrs if (ch == NULL) { 3679163953Srrs *offset = length; 3680169378Srrs if (locked_tcb) { 3681163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3682169378Srrs } 3683163953Srrs return (NULL); 3684163953Srrs } 3685169208Srrs } else if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3686169208Srrs if (chk_length > sizeof(chunk_buf)) { 3687169208Srrs /* 3688169208Srrs * use just the size of the chunk buffer so 3689169208Srrs * the front part of our cookie is intact. 3690169208Srrs * The rest of cookie processing should use 3691169208Srrs * the sctp_m_getptr() function to access 3692169208Srrs * the other parts. 3693169208Srrs */ 3694169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3695169208Srrs (sizeof(chunk_buf) - 4), 3696169208Srrs chunk_buf); 3697169208Srrs if (ch == NULL) { 3698169208Srrs *offset = length; 3699169378Srrs if (locked_tcb) { 3700169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 3701169378Srrs } 3702169208Srrs return (NULL); 3703169208Srrs } 3704169208Srrs } else { 3705169208Srrs /* We can fit it all */ 3706169208Srrs goto all_fits; 3707169208Srrs } 3708163953Srrs } else { 3709163953Srrs /* get a complete chunk... */ 3710170140Srrs if (chk_length > sizeof(chunk_buf)) { 3711163953Srrs struct mbuf *oper; 3712163953Srrs struct sctp_paramhdr *phdr; 3713163953Srrs 3714163953Srrs oper = NULL; 3715169208Srrs if (stcb) { 3716169208Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 3717169208Srrs 0, M_DONTWAIT, 1, MT_DATA); 3718169208Srrs 3719169208Srrs if (oper) { 3720169208Srrs /* pre-reserve some space */ 3721169208Srrs SCTP_BUF_RESV_UF(oper, sizeof(struct sctp_chunkhdr)); 3722169208Srrs SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr); 3723169208Srrs phdr = mtod(oper, struct sctp_paramhdr *); 3724169208Srrs phdr->param_type = htons(SCTP_CAUSE_OUT_OF_RESC); 3725169208Srrs phdr->param_length = htons(sizeof(struct sctp_paramhdr)); 3726169208Srrs sctp_queue_op_err(stcb, oper); 3727169208Srrs } 3728163953Srrs } 3729169378Srrs if (locked_tcb) { 3730163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3731169378Srrs } 3732163953Srrs return (NULL); 3733163953Srrs } 3734169208Srrs all_fits: 3735163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3736163953Srrs chk_length, chunk_buf); 3737163953Srrs if (ch == NULL) { 3738169420Srrs SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 3739163953Srrs *offset = length; 3740169378Srrs if (locked_tcb) { 3741163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3742169378Srrs } 3743163953Srrs return (NULL); 3744163953Srrs } 3745163953Srrs } 3746163953Srrs num_chunks++; 3747163953Srrs /* Save off the last place we got a control from */ 3748163953Srrs if (stcb != NULL) { 3749169420Srrs if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 3750163953Srrs /* 3751163953Srrs * allow last_control to be NULL if 3752163953Srrs * ASCONF... ASCONF processing will find the 3753163953Srrs * right net later 3754163953Srrs */ 3755169420Srrs if ((netp != NULL) && (*netp != NULL)) 3756169420Srrs stcb->asoc.last_control_chunk_from = *netp; 3757163953Srrs } 3758163953Srrs } 3759163953Srrs#ifdef SCTP_AUDITING_ENABLED 3760163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3761163953Srrs#endif 3762163953Srrs 3763163953Srrs /* check to see if this chunk required auth, but isn't */ 3764163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3765163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3766163953Srrs stcb->asoc.local_auth_chunks) && 3767163953Srrs !stcb->asoc.authenticated) { 3768163953Srrs /* "silently" ignore */ 3769163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3770163953Srrs goto next_chunk; 3771163953Srrs } 3772163953Srrs switch (ch->chunk_type) { 3773163953Srrs case SCTP_INITIATION: 3774163953Srrs /* must be first and only chunk */ 3775169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 3776163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3777163953Srrs /* We are not interested anymore? */ 3778163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3779163953Srrs /* 3780163953Srrs * collision case where we are 3781163953Srrs * sending to them too 3782163953Srrs */ 3783163953Srrs ; 3784163953Srrs } else { 3785169378Srrs if (locked_tcb) { 3786163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3787169378Srrs } 3788163953Srrs *offset = length; 3789163953Srrs return (NULL); 3790163953Srrs } 3791163953Srrs } 3792163953Srrs if ((num_chunks > 1) || 3793170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3794163953Srrs *offset = length; 3795169378Srrs if (locked_tcb) { 3796163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3797169378Srrs } 3798163953Srrs return (NULL); 3799163953Srrs } 3800163953Srrs if ((stcb != NULL) && 3801163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3802163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3803163953Srrs sctp_send_shutdown_ack(stcb, 3804163953Srrs stcb->asoc.primary_destination); 3805163953Srrs *offset = length; 3806163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3807169378Srrs if (locked_tcb) { 3808163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3809169378Srrs } 3810163953Srrs return (NULL); 3811163953Srrs } 3812169420Srrs if (netp) { 3813169420Srrs sctp_handle_init(m, iphlen, *offset, sh, 3814169420Srrs (struct sctp_init_chunk *)ch, inp, 3815169420Srrs stcb, *netp, &abort_no_unlock, vrf_id, table_id); 3816169420Srrs } 3817168299Srrs if (abort_no_unlock) 3818168299Srrs return (NULL); 3819168299Srrs 3820163953Srrs *offset = length; 3821169378Srrs if (locked_tcb) { 3822163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3823169378Srrs } 3824163953Srrs return (NULL); 3825163953Srrs break; 3826163953Srrs case SCTP_INITIATION_ACK: 3827163953Srrs /* must be first and only chunk */ 3828169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 3829163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3830163953Srrs /* We are not interested anymore */ 3831163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3832163953Srrs ; 3833163953Srrs } else { 3834169378Srrs if (locked_tcb) { 3835163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3836169378Srrs } 3837163953Srrs *offset = length; 3838163953Srrs if (stcb) { 3839165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3840163953Srrs } 3841163953Srrs return (NULL); 3842163953Srrs } 3843163953Srrs } 3844163953Srrs if ((num_chunks > 1) || 3845170140Srrs (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) { 3846163953Srrs *offset = length; 3847169378Srrs if (locked_tcb) { 3848163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3849169378Srrs } 3850163953Srrs return (NULL); 3851163953Srrs } 3852169420Srrs if ((netp) && (*netp)) { 3853169420Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3854169420Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id, table_id); 3855169420Srrs } else { 3856169420Srrs ret = -1; 3857169420Srrs } 3858163953Srrs /* 3859163953Srrs * Special case, I must call the output routine to 3860163953Srrs * get the cookie echoed 3861163953Srrs */ 3862168299Srrs if (abort_no_unlock) 3863168299Srrs return (NULL); 3864168299Srrs 3865163953Srrs if ((stcb) && ret == 0) 3866163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3867163953Srrs *offset = length; 3868169378Srrs if (locked_tcb) { 3869163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3870169378Srrs } 3871163953Srrs return (NULL); 3872163953Srrs break; 3873163953Srrs case SCTP_SELECTIVE_ACK: 3874169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 3875163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3876163953Srrs { 3877163953Srrs struct sctp_sack_chunk *sack; 3878163953Srrs int abort_now = 0; 3879163953Srrs uint32_t a_rwnd, cum_ack; 3880163953Srrs uint16_t num_seg; 3881163953Srrs int nonce_sum_flag; 3882163953Srrs 3883169420Srrs if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) { 3884169420Srrs SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n"); 3885169208Srrs *offset = length; 3886169378Srrs if (locked_tcb) { 3887169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 3888169378Srrs } 3889169208Srrs return (NULL); 3890169208Srrs } 3891163953Srrs sack = (struct sctp_sack_chunk *)ch; 3892163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3893163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3894163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3895163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3896163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3897163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3898163953Srrs (num_seg == 0) && 3899163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3900163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3901163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3902163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3903163953Srrs ) { 3904163953Srrs /* 3905163953Srrs * We have a SIMPLE sack having no 3906163953Srrs * prior segments and data on sent 3907163953Srrs * queue to be acked.. Use the 3908163953Srrs * faster path sack processing. We 3909163953Srrs * also allow window update sacks 3910163953Srrs * with no missing segments to go 3911163953Srrs * this way too. 3912163953Srrs */ 3913169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 3914169208Srrs &abort_now); 3915163953Srrs } else { 3916169420Srrs if (netp && *netp) 3917169420Srrs sctp_handle_sack(sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 3918163953Srrs } 3919163953Srrs if (abort_now) { 3920163953Srrs /* ABORT signal from sack processing */ 3921163953Srrs *offset = length; 3922163953Srrs return (NULL); 3923163953Srrs } 3924163953Srrs } 3925163953Srrs break; 3926163953Srrs case SCTP_HEARTBEAT_REQUEST: 3927169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 3928169420Srrs if ((stcb) && netp && *netp) { 3929169420Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 3930169420Srrs sctp_send_heartbeat_ack(stcb, m, *offset, 3931169420Srrs chk_length, *netp); 3932169420Srrs 3933169420Srrs /* He's alive so give him credit */ 3934169420Srrs stcb->asoc.overall_error_count = 0; 3935163953Srrs } 3936163953Srrs break; 3937163953Srrs case SCTP_HEARTBEAT_ACK: 3938169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 3939169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 3940169208Srrs /* Its not ours */ 3941169352Srrs *offset = length; 3942169378Srrs if (locked_tcb) { 3943169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3944169378Srrs } 3945169208Srrs return (NULL); 3946169208Srrs } 3947163953Srrs /* He's alive so give him credit */ 3948163953Srrs stcb->asoc.overall_error_count = 0; 3949163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 3950169420Srrs if (netp && *netp) 3951169420Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 3952169420Srrs stcb, *netp); 3953163953Srrs break; 3954163953Srrs case SCTP_ABORT_ASSOCIATION: 3955170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 3956170091Srrs stcb); 3957169420Srrs if ((stcb) && netp && *netp) 3958169420Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 3959169420Srrs stcb, *netp); 3960163953Srrs *offset = length; 3961163953Srrs return (NULL); 3962163953Srrs break; 3963163953Srrs case SCTP_SHUTDOWN: 3964170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 3965170091Srrs stcb); 3966169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 3967169208Srrs *offset = length; 3968169378Srrs if (locked_tcb) { 3969169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 3970169378Srrs } 3971169208Srrs return (NULL); 3972169208Srrs 3973169420Srrs } 3974169420Srrs if (netp && *netp) { 3975163953Srrs int abort_flag = 0; 3976163953Srrs 3977163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 3978163953Srrs stcb, *netp, &abort_flag); 3979163953Srrs if (abort_flag) { 3980163953Srrs *offset = length; 3981163953Srrs return (NULL); 3982163953Srrs } 3983163953Srrs } 3984163953Srrs break; 3985163953Srrs case SCTP_SHUTDOWN_ACK: 3986170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 3987169420Srrs if ((stcb) && (netp) && (*netp)) 3988169420Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 3989163953Srrs *offset = length; 3990163953Srrs return (NULL); 3991163953Srrs break; 3992169420Srrs 3993163953Srrs case SCTP_OPERATION_ERROR: 3994169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 3995169420Srrs if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 3996169420Srrs 3997163953Srrs *offset = length; 3998163953Srrs return (NULL); 3999163953Srrs } 4000163953Srrs break; 4001163953Srrs case SCTP_COOKIE_ECHO: 4002169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4003170091Srrs "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 4004163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4005163953Srrs ; 4006163953Srrs } else { 4007169420Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4008163953Srrs /* We are not interested anymore */ 4009163953Srrs *offset = length; 4010163953Srrs return (NULL); 4011163953Srrs } 4012163953Srrs } 4013163953Srrs /* 4014163953Srrs * First are we accepting? We do this again here 4015169420Srrs * sincen it is possible that a previous endpoint 4016169420Srrs * WAS listening responded to a INIT-ACK and then 4017163953Srrs * closed. We opened and bound.. and are now no 4018163953Srrs * longer listening. 4019163953Srrs */ 4020163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4021163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4022163953Srrs /* 4023163953Srrs * special case, is this a retran'd 4024163953Srrs * COOKIE-ECHO or a restarting assoc 4025163953Srrs * that is a peeled off or 4026163953Srrs * one-to-one style socket. 4027163953Srrs */ 4028163953Srrs goto process_cookie_anyway; 4029163953Srrs } 4030169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4031169352Srrs sh, NULL, vrf_id, 4032169352Srrs table_id); 4033163953Srrs *offset = length; 4034163953Srrs return (NULL); 4035163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4036163953Srrs /* we are accepting so check limits like TCP */ 4037163953Srrs if (inp->sctp_socket->so_qlen > 4038163953Srrs inp->sctp_socket->so_qlimit) { 4039163953Srrs /* no space */ 4040163953Srrs struct mbuf *oper; 4041163953Srrs struct sctp_paramhdr *phdr; 4042163953Srrs 4043163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4044163953Srrs oper = NULL; 4045163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4046165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4047163953Srrs if (oper) { 4048165647Srrs SCTP_BUF_LEN(oper) = 4049163953Srrs sizeof(struct sctp_paramhdr); 4050163953Srrs phdr = mtod(oper, 4051163953Srrs struct sctp_paramhdr *); 4052163953Srrs phdr->param_type = 4053163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4054163953Srrs phdr->param_length = 4055163953Srrs htons(sizeof(struct sctp_paramhdr)); 4056163953Srrs } 4057163953Srrs sctp_abort_association(inp, stcb, m, 4058169352Srrs iphlen, sh, oper, vrf_id, table_id); 4059163953Srrs } 4060163953Srrs *offset = length; 4061163953Srrs return (NULL); 4062163953Srrs } 4063163953Srrs } 4064163953Srrs process_cookie_anyway: 4065163953Srrs { 4066163953Srrs struct mbuf *ret_buf; 4067165220Srrs struct sctp_inpcb *linp; 4068163953Srrs 4069169420Srrs if (stcb) { 4070165220Srrs linp = NULL; 4071169420Srrs } else { 4072165220Srrs linp = inp; 4073169420Srrs } 4074165220Srrs 4075169420Srrs if (linp) { 4076165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4077169420Srrs } 4078169420Srrs if (netp) { 4079169420Srrs ret_buf = 4080169420Srrs sctp_handle_cookie_echo(m, iphlen, 4081169420Srrs *offset, sh, 4082169420Srrs (struct sctp_cookie_echo_chunk *)ch, 4083169420Srrs &inp, &stcb, netp, 4084169420Srrs auth_skipped, 4085169420Srrs auth_offset, 4086169420Srrs auth_len, 4087169420Srrs &locked_tcb, 4088169420Srrs vrf_id, 4089169420Srrs table_id); 4090169420Srrs } else { 4091169420Srrs ret_buf = NULL; 4092169420Srrs } 4093169420Srrs if (linp) { 4094165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4095169420Srrs } 4096163953Srrs if (ret_buf == NULL) { 4097163953Srrs if (locked_tcb) { 4098163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4099163953Srrs } 4100169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, 4101169420Srrs "GAK, null buffer\n"); 4102163953Srrs auth_skipped = 0; 4103163953Srrs *offset = length; 4104163953Srrs return (NULL); 4105163953Srrs } 4106163953Srrs /* if AUTH skipped, see if it verified... */ 4107163953Srrs if (auth_skipped) { 4108163953Srrs got_auth = 1; 4109163953Srrs auth_skipped = 0; 4110163953Srrs } 4111163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4112163953Srrs /* 4113163953Srrs * Restart the timer if we have 4114163953Srrs * pending data 4115163953Srrs */ 4116163953Srrs struct sctp_tmit_chunk *chk; 4117163953Srrs 4118163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4119163953Srrs if (chk) { 4120163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4121163953Srrs stcb->sctp_ep, stcb, 4122163953Srrs chk->whoTo); 4123163953Srrs } 4124163953Srrs } 4125163953Srrs } 4126163953Srrs break; 4127163953Srrs case SCTP_COOKIE_ACK: 4128170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 4129169420Srrs if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4130169378Srrs if (locked_tcb) { 4131169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4132169378Srrs } 4133169352Srrs return (NULL); 4134169352Srrs } 4135163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4136163953Srrs /* We are not interested anymore */ 4137163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4138163953Srrs ; 4139169420Srrs } else if (stcb) { 4140165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4141163953Srrs *offset = length; 4142163953Srrs return (NULL); 4143163953Srrs } 4144163953Srrs } 4145163953Srrs /* He's alive so give him credit */ 4146169420Srrs if ((stcb) && netp && *netp) { 4147169378Srrs stcb->asoc.overall_error_count = 0; 4148169378Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4149169378Srrs } 4150163953Srrs break; 4151163953Srrs case SCTP_ECN_ECHO: 4152169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 4153163953Srrs /* He's alive so give him credit */ 4154169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 4155169208Srrs /* Its not ours */ 4156169378Srrs if (locked_tcb) { 4157169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4158169378Srrs } 4159169208Srrs *offset = length; 4160169208Srrs return (NULL); 4161169208Srrs } 4162169378Srrs if (stcb) { 4163169378Srrs stcb->asoc.overall_error_count = 0; 4164169378Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4165169378Srrs stcb); 4166169378Srrs } 4167163953Srrs break; 4168163953Srrs case SCTP_ECN_CWR: 4169169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 4170163953Srrs /* He's alive so give him credit */ 4171169420Srrs if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 4172169208Srrs /* Its not ours */ 4173169378Srrs if (locked_tcb) { 4174169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4175169378Srrs } 4176169208Srrs *offset = length; 4177169208Srrs return (NULL); 4178169208Srrs } 4179169378Srrs if (stcb) { 4180169378Srrs stcb->asoc.overall_error_count = 0; 4181169378Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4182169378Srrs } 4183163953Srrs break; 4184163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4185170091Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 4186163953Srrs /* must be first and only chunk */ 4187163953Srrs if ((num_chunks > 1) || 4188170140Srrs (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4189163953Srrs *offset = length; 4190169378Srrs if (locked_tcb) { 4191163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4192169378Srrs } 4193163953Srrs return (NULL); 4194163953Srrs } 4195169420Srrs if ((stcb) && netp && *netp) { 4196169378Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4197169378Srrs stcb, *netp); 4198169378Srrs } 4199163953Srrs *offset = length; 4200163953Srrs return (NULL); 4201163953Srrs break; 4202163953Srrs case SCTP_ASCONF: 4203169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 4204163953Srrs /* He's alive so give him credit */ 4205169378Srrs if (stcb) { 4206169378Srrs stcb->asoc.overall_error_count = 0; 4207169378Srrs sctp_handle_asconf(m, *offset, 4208169378Srrs (struct sctp_asconf_chunk *)ch, stcb); 4209169378Srrs } 4210163953Srrs break; 4211163953Srrs case SCTP_ASCONF_ACK: 4212169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 4213169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4214169208Srrs /* Its not ours */ 4215169378Srrs if (locked_tcb) { 4216169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4217169378Srrs } 4218169208Srrs *offset = length; 4219169208Srrs return (NULL); 4220169208Srrs } 4221169420Srrs if ((stcb) && netp && *netp) { 4222169378Srrs /* He's alive so give him credit */ 4223169378Srrs stcb->asoc.overall_error_count = 0; 4224169378Srrs sctp_handle_asconf_ack(m, *offset, 4225169378Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4226169378Srrs } 4227163953Srrs break; 4228163953Srrs case SCTP_FORWARD_CUM_TSN: 4229169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 4230169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4231169208Srrs /* Its not ours */ 4232169378Srrs if (locked_tcb) { 4233169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4234169378Srrs } 4235169208Srrs *offset = length; 4236169208Srrs return (NULL); 4237169208Srrs } 4238163953Srrs /* He's alive so give him credit */ 4239169378Srrs if (stcb) { 4240163953Srrs int abort_flag = 0; 4241163953Srrs 4242163953Srrs stcb->asoc.overall_error_count = 0; 4243163953Srrs *fwd_tsn_seen = 1; 4244163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4245163953Srrs /* We are not interested anymore */ 4246165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28); 4247163953Srrs *offset = length; 4248163953Srrs return (NULL); 4249163953Srrs } 4250163953Srrs sctp_handle_forward_tsn(stcb, 4251163953Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag); 4252163953Srrs if (abort_flag) { 4253163953Srrs *offset = length; 4254163953Srrs return (NULL); 4255163953Srrs } else { 4256163953Srrs stcb->asoc.overall_error_count = 0; 4257163953Srrs } 4258163953Srrs 4259163953Srrs } 4260163953Srrs break; 4261163953Srrs case SCTP_STREAM_RESET: 4262169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 4263163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4264163953Srrs chk_length, chunk_buf); 4265169420Srrs if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 4266169208Srrs /* Its not ours */ 4267169378Srrs if (locked_tcb) { 4268169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4269169378Srrs } 4270169208Srrs *offset = length; 4271169208Srrs return (NULL); 4272169208Srrs } 4273163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4274163953Srrs /* We are not interested anymore */ 4275165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4276163953Srrs *offset = length; 4277163953Srrs return (NULL); 4278163953Srrs } 4279169420Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4280169420Srrs /* 4281169420Srrs * hmm, peer should have announced this, but 4282169420Srrs * we will turn it on since he is sending us 4283169420Srrs * a stream reset. 4284169420Srrs */ 4285169420Srrs stcb->asoc.peer_supports_strreset = 1; 4286163953Srrs } 4287169420Srrs if (sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_out_req *)ch)) { 4288169420Srrs /* stop processing */ 4289169420Srrs *offset = length; 4290169420Srrs return (NULL); 4291169420Srrs } 4292163953Srrs break; 4293163953Srrs case SCTP_PACKET_DROPPED: 4294169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 4295163953Srrs /* re-get it all please */ 4296169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4297169208Srrs /* Its not ours */ 4298169378Srrs if (locked_tcb) { 4299169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4300169378Srrs } 4301169208Srrs *offset = length; 4302169208Srrs return (NULL); 4303169208Srrs } 4304163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4305163953Srrs chk_length, chunk_buf); 4306163953Srrs 4307169420Srrs if (ch && (stcb) && netp && (*netp)) { 4308169378Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4309169378Srrs stcb, *netp); 4310169378Srrs } 4311163953Srrs break; 4312163953Srrs 4313163953Srrs case SCTP_AUTHENTICATION: 4314169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 4315163953Srrs if (sctp_auth_disable) 4316163953Srrs goto unknown_chunk; 4317163953Srrs 4318163953Srrs if (stcb == NULL) { 4319163953Srrs /* save the first AUTH for later processing */ 4320163953Srrs if (auth_skipped == 0) { 4321163953Srrs auth_offset = *offset; 4322163953Srrs auth_len = chk_length; 4323163953Srrs auth_skipped = 1; 4324163953Srrs } 4325163953Srrs /* skip this chunk (temporarily) */ 4326163953Srrs goto next_chunk; 4327163953Srrs } 4328169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4329169420Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + 4330169420Srrs SCTP_AUTH_DIGEST_LEN_MAX))) { 4331169208Srrs /* Its not ours */ 4332169378Srrs if (locked_tcb) { 4333169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4334169378Srrs } 4335169208Srrs *offset = length; 4336169208Srrs return (NULL); 4337169208Srrs } 4338163953Srrs if (got_auth == 1) { 4339163953Srrs /* skip this chunk... it's already auth'd */ 4340163953Srrs goto next_chunk; 4341163953Srrs } 4342163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4343163953Srrs chk_length, chunk_buf); 4344163953Srrs got_auth = 1; 4345169420Srrs if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4346163953Srrs m, *offset)) { 4347163953Srrs /* auth HMAC failed so dump the packet */ 4348163953Srrs *offset = length; 4349163953Srrs return (stcb); 4350163953Srrs } else { 4351163953Srrs /* remaining chunks are HMAC checked */ 4352163953Srrs stcb->asoc.authenticated = 1; 4353163953Srrs } 4354163953Srrs break; 4355163953Srrs 4356163953Srrs default: 4357163953Srrs unknown_chunk: 4358163953Srrs /* it's an unknown chunk! */ 4359163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4360163953Srrs struct mbuf *mm; 4361163953Srrs struct sctp_paramhdr *phd; 4362163953Srrs 4363163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4364165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4365163953Srrs if (mm) { 4366163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4367163953Srrs /* 4368163953Srrs * We cheat and use param type since 4369163953Srrs * we did not bother to define a 4370163953Srrs * error cause struct. They are the 4371163953Srrs * same basic format with different 4372163953Srrs * names. 4373163953Srrs */ 4374163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4375163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4376165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4377166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4378163953Srrs M_DONTWAIT); 4379165647Srrs if (SCTP_BUF_NEXT(mm)) { 4380163953Srrs sctp_queue_op_err(stcb, mm); 4381163953Srrs } else { 4382163953Srrs sctp_m_freem(mm); 4383163953Srrs } 4384163953Srrs } 4385163953Srrs } 4386163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4387163953Srrs /* discard this packet */ 4388163953Srrs *offset = length; 4389163953Srrs return (stcb); 4390163953Srrs } /* else skip this bad chunk and continue... */ 4391163953Srrs break; 4392163953Srrs } /* switch (ch->chunk_type) */ 4393163953Srrs 4394163953Srrs 4395163953Srrsnext_chunk: 4396163953Srrs /* get the next chunk */ 4397163953Srrs *offset += SCTP_SIZE32(chk_length); 4398163953Srrs if (*offset >= length) { 4399163953Srrs /* no more data left in the mbuf chain */ 4400163953Srrs break; 4401163953Srrs } 4402163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4403163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4404163953Srrs if (ch == NULL) { 4405169378Srrs if (locked_tcb) { 4406163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4407169378Srrs } 4408163953Srrs *offset = length; 4409163953Srrs return (NULL); 4410163953Srrs } 4411163953Srrs } /* while */ 4412163953Srrs return (stcb); 4413163953Srrs} 4414163953Srrs 4415163953Srrs 4416163953Srrs/* 4417163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4418163953Srrs * ECN(0) or ECN(1) or CE 4419163953Srrs */ 4420163953Srrsstatic __inline void 4421163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4422163953Srrs uint8_t ecn_bits) 4423163953Srrs{ 4424163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4425163953Srrs ; 4426163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4427163953Srrs /* 4428163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4429163953Srrs * change the NS bit (that we have yet to find a way to send 4430163953Srrs * it yet). 4431163953Srrs */ 4432163953Srrs 4433163953Srrs /* ECN Nonce stuff */ 4434163953Srrs stcb->asoc.receiver_nonce_sum++; 4435163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4436163953Srrs 4437163953Srrs /* 4438163953Srrs * Drag up the last_echo point if cumack is larger since we 4439163953Srrs * don't want the point falling way behind by more than 4440163953Srrs * 2^^31 and then having it be incorrect. 4441163953Srrs */ 4442163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4443163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4444163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4445163953Srrs } 4446163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4447163953Srrs /* 4448163953Srrs * Drag up the last_echo point if cumack is larger since we 4449163953Srrs * don't want the point falling way behind by more than 4450163953Srrs * 2^^31 and then having it be incorrect. 4451163953Srrs */ 4452163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4453163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4454163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4455163953Srrs } 4456163953Srrs } 4457163953Srrs} 4458163953Srrs 4459163953Srrsstatic __inline void 4460163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4461163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4462163953Srrs{ 4463163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4464163953Srrs /* 4465163953Srrs * we possibly must notify the sender that a congestion 4466163953Srrs * window reduction is in order. We do this by adding a ECNE 4467163953Srrs * chunk to the output chunk queue. The incoming CWR will 4468163953Srrs * remove this chunk. 4469163953Srrs */ 4470163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4471163953Srrs MAX_TSN)) { 4472163953Srrs /* Yep, we need to add a ECNE */ 4473163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4474163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4475163953Srrs } 4476163953Srrs } 4477163953Srrs} 4478163953Srrs 4479163953Srrs/* 4480163953Srrs * common input chunk processing (v4 and v6) 4481163953Srrs */ 4482169378Srrsvoid 4483163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4484163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4485163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4486169352Srrs uint8_t ecn_bits, uint32_t vrf_id, uint32_t table_id) 4487163953Srrs{ 4488163953Srrs /* 4489163953Srrs * Control chunk processing 4490163953Srrs */ 4491163953Srrs uint32_t high_tsn; 4492163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4493163953Srrs struct mbuf *m = *mm; 4494163953Srrs int abort_flag = 0; 4495163953Srrs int un_sent; 4496163953Srrs 4497163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4498163953Srrs#ifdef SCTP_AUDITING_ENABLED 4499163953Srrs sctp_audit_log(0xE0, 1); 4500163953Srrs sctp_auditing(0, inp, stcb, net); 4501163953Srrs#endif 4502163953Srrs 4503169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4504169420Srrs m, iphlen, offset); 4505163953Srrs 4506163953Srrs if (stcb) { 4507163953Srrs /* always clear this before beginning a packet */ 4508163953Srrs stcb->asoc.authenticated = 0; 4509163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4510163953Srrs } 4511163953Srrs if (IS_SCTP_CONTROL(ch)) { 4512163953Srrs /* process the control portion of the SCTP packet */ 4513169655Srrs /* sa_ignore NO_NULL_CHK */ 4514163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4515169352Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, table_id); 4516163953Srrs if (stcb) { 4517163953Srrs /* 4518163953Srrs * This covers us if the cookie-echo was there and 4519163953Srrs * it changes our INP. 4520163953Srrs */ 4521163953Srrs inp = stcb->sctp_ep; 4522163953Srrs } 4523163953Srrs } else { 4524163953Srrs /* 4525163953Srrs * no control chunks, so pre-process DATA chunks (these 4526163953Srrs * checks are taken care of by control processing) 4527163953Srrs */ 4528163953Srrs 4529163953Srrs /* 4530163953Srrs * if DATA only packet, and auth is required, then punt... 4531163953Srrs * can't have authenticated without any AUTH (control) 4532163953Srrs * chunks 4533163953Srrs */ 4534163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4535163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4536163953Srrs stcb->asoc.local_auth_chunks)) { 4537163953Srrs /* "silently" ignore */ 4538163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4539163953Srrs SCTP_TCB_UNLOCK(stcb); 4540169378Srrs return; 4541163953Srrs } 4542163953Srrs if (stcb == NULL) { 4543163953Srrs /* out of the blue DATA chunk */ 4544169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4545169352Srrs vrf_id, table_id); 4546169378Srrs return; 4547163953Srrs } 4548163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4549163953Srrs /* v_tag mismatch! */ 4550163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4551163953Srrs SCTP_TCB_UNLOCK(stcb); 4552169378Srrs return; 4553163953Srrs } 4554163953Srrs } 4555163953Srrs 4556163953Srrs if (stcb == NULL) { 4557163953Srrs /* 4558163953Srrs * no valid TCB for this packet, or we found it's a bad 4559163953Srrs * packet while processing control, or we're done with this 4560163953Srrs * packet (done or skip rest of data), so we drop it... 4561163953Srrs */ 4562169378Srrs return; 4563163953Srrs } 4564163953Srrs /* 4565163953Srrs * DATA chunk processing 4566163953Srrs */ 4567163953Srrs /* plow through the data chunks while length > offset */ 4568163953Srrs 4569163953Srrs /* 4570163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4571163953Srrs * DATA is required. 4572163953Srrs */ 4573165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4574163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4575163953Srrs stcb->asoc.local_auth_chunks) && 4576163953Srrs !stcb->asoc.authenticated) { 4577163953Srrs /* "silently" ignore */ 4578163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4579169420Srrs SCTPDBG(SCTP_DEBUG_AUTH1, 4580169420Srrs "Data chunk requires AUTH, skipped\n"); 4581165220Srrs goto trigger_send; 4582163953Srrs } 4583163953Srrs if (length > offset) { 4584163953Srrs int retval; 4585163953Srrs 4586163953Srrs /* 4587163953Srrs * First check to make sure our state is correct. We would 4588163953Srrs * not get here unless we really did have a tag, so we don't 4589163953Srrs * abort if this happens, just dump the chunk silently. 4590163953Srrs */ 4591163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4592163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4593163953Srrs /* 4594163953Srrs * we consider data with valid tags in this state 4595163953Srrs * shows us the cookie-ack was lost. Imply it was 4596163953Srrs * there. 4597163953Srrs */ 4598163953Srrs stcb->asoc.overall_error_count = 0; 4599163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4600163953Srrs break; 4601163953Srrs case SCTP_STATE_COOKIE_WAIT: 4602163953Srrs /* 4603163953Srrs * We consider OOTB any data sent during asoc setup. 4604163953Srrs */ 4605169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4606169352Srrs vrf_id, table_id); 4607163953Srrs SCTP_TCB_UNLOCK(stcb); 4608169378Srrs return; 4609163953Srrs break; 4610163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4611163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4612163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4613163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4614163953Srrs default: 4615163953Srrs SCTP_TCB_UNLOCK(stcb); 4616169378Srrs return; 4617163953Srrs break; 4618163953Srrs case SCTP_STATE_OPEN: 4619163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4620163953Srrs break; 4621163953Srrs } 4622163953Srrs /* take care of ECN, part 1. */ 4623163953Srrs if (stcb->asoc.ecn_allowed && 4624163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4625163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4626163953Srrs } 4627163953Srrs /* plow through the data chunks while length > offset */ 4628163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4629163953Srrs inp, stcb, net, &high_tsn); 4630163953Srrs if (retval == 2) { 4631163953Srrs /* 4632163953Srrs * The association aborted, NO UNLOCK needed since 4633163953Srrs * the association is destroyed. 4634163953Srrs */ 4635169378Srrs return; 4636163953Srrs } 4637163953Srrs data_processed = 1; 4638163953Srrs if (retval == 0) { 4639163953Srrs /* take care of ecn part 2. */ 4640163953Srrs if (stcb->asoc.ecn_allowed && 4641163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4642163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4643163953Srrs ecn_bits); 4644163953Srrs } 4645163953Srrs } 4646163953Srrs /* 4647163953Srrs * Anything important needs to have been m_copy'ed in 4648163953Srrs * process_data 4649163953Srrs */ 4650163953Srrs } 4651163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4652163953Srrs int was_a_gap = 0; 4653163953Srrs 4654163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4655163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4656163953Srrs /* there was a gap before this data was processed */ 4657163953Srrs was_a_gap = 1; 4658163953Srrs } 4659163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4660163953Srrs if (abort_flag) { 4661163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4662169378Srrs return; 4663163953Srrs } 4664163953Srrs } 4665163953Srrs /* trigger send of any chunks in queue... */ 4666165220Srrstrigger_send: 4667163953Srrs#ifdef SCTP_AUDITING_ENABLED 4668163953Srrs sctp_audit_log(0xE0, 2); 4669163953Srrs sctp_auditing(1, inp, stcb, net); 4670163953Srrs#endif 4671169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4672169420Srrs "Check for chunk output prw:%d tqe:%d tf=%d\n", 4673169420Srrs stcb->asoc.peers_rwnd, 4674169420Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4675169420Srrs stcb->asoc.total_flight); 4676163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4677163953Srrs 4678163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4679163953Srrs ((un_sent) && 4680163953Srrs (stcb->asoc.peers_rwnd > 0 || 4681163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4682169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 4683163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4684169420Srrs SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 4685163953Srrs } 4686163953Srrs#ifdef SCTP_AUDITING_ENABLED 4687163953Srrs sctp_audit_log(0xE0, 3); 4688163953Srrs sctp_auditing(2, inp, stcb, net); 4689163953Srrs#endif 4690163953Srrs SCTP_TCB_UNLOCK(stcb); 4691169378Srrs return; 4692163953Srrs} 4693163953Srrs 4694163953Srrs 4695163953Srrs 4696163953Srrsvoid 4697165647Srrssctp_input(i_pak, off) 4698165647Srrs struct mbuf *i_pak; 4699163953Srrs int off; 4700163953Srrs 4701163953Srrs{ 4702163953Srrs#ifdef SCTP_MBUF_LOGGING 4703163953Srrs struct mbuf *mat; 4704163953Srrs 4705163953Srrs#endif 4706165647Srrs struct mbuf *m; 4707163953Srrs int iphlen; 4708169352Srrs uint32_t vrf_id = 0, table_id = 0; 4709163953Srrs uint8_t ecn_bits; 4710163953Srrs struct ip *ip; 4711163953Srrs struct sctphdr *sh; 4712163953Srrs struct sctp_inpcb *inp = NULL; 4713163953Srrs 4714163953Srrs uint32_t check, calc_check; 4715163953Srrs struct sctp_nets *net; 4716163953Srrs struct sctp_tcb *stcb = NULL; 4717163953Srrs struct sctp_chunkhdr *ch; 4718163953Srrs int refcount_up = 0; 4719163953Srrs int length, mlen, offset; 4720163953Srrs 4721169352Srrs 4722169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 4723169352Srrs SCTP_RELEASE_PKT(i_pak); 4724169352Srrs return; 4725169352Srrs } 4726169352Srrs if (SCTP_GET_PKT_TABLEID(i_pak, table_id)) { 4727169352Srrs SCTP_RELEASE_PKT(i_pak); 4728169352Srrs return; 4729169352Srrs } 4730168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 4731163953Srrs iphlen = off; 4732165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4733170056Srrs 4734163953Srrs net = NULL; 4735163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4736163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4737163953Srrs 4738170091Srrs 4739163953Srrs#ifdef SCTP_MBUF_LOGGING 4740163953Srrs /* Log in any input mbufs */ 4741163953Srrs mat = m; 4742163953Srrs while (mat) { 4743165647Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4744163953Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4745163953Srrs } 4746165647Srrs mat = SCTP_BUF_NEXT(mat); 4747163953Srrs } 4748163953Srrs#endif 4749170091Srrs#ifdef SCTP_PACKET_LOGGING 4750170091Srrs sctp_packet_log(m, mlen); 4751170091Srrs#endif 4752170091Srrs /* 4753170091Srrs * Must take out the iphlen, since mlen expects this (only effect lb 4754170091Srrs * case) 4755170091Srrs */ 4756170091Srrs mlen -= iphlen; 4757168124Srrs 4758163953Srrs /* 4759163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4760163953Srrs */ 4761163953Srrs ip = mtod(m, struct ip *); 4762163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4763165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4764163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4765163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4766163953Srrs return; 4767163953Srrs } 4768163953Srrs ip = mtod(m, struct ip *); 4769163953Srrs } 4770163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4771163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4772163953Srrs 4773163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4774163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4775163953Srrs goto bad; 4776163953Srrs } 4777168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4778163953Srrs /* 4779163953Srrs * We only look at broadcast if its a front state, All 4780163953Srrs * others we will not have a tcb for anyway. 4781163953Srrs */ 4782163953Srrs goto bad; 4783163953Srrs } 4784163953Srrs /* validate SCTP checksum */ 4785168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4786163953Srrs /* 4787163953Srrs * we do NOT validate things from the loopback if the sysctl 4788163953Srrs * is set to 1. 4789163953Srrs */ 4790163953Srrs check = sh->checksum; /* save incoming checksum */ 4791163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4792163953Srrs /* 4793163953Srrs * special hook for where we got a local address 4794163953Srrs * somehow routed across a non IFT_LOOP type 4795163953Srrs * interface 4796163953Srrs */ 4797163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4798163953Srrs goto sctp_skip_csum_4; 4799163953Srrs } 4800163953Srrs sh->checksum = 0; /* prepare for calc */ 4801163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4802163953Srrs if (calc_check != check) { 4803169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4804169420Srrs calc_check, check, m, mlen, iphlen); 4805163953Srrs 4806163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4807163953Srrs offset - sizeof(*ch), 4808169420Srrs sh, ch, &inp, &net, 4809169420Srrs vrf_id); 4810163953Srrs if ((inp) && (stcb)) { 4811163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4812163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4813163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4814163953Srrs refcount_up = 1; 4815163953Srrs } 4816163953Srrs SCTP_STAT_INCR(sctps_badsum); 4817163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4818163953Srrs goto bad; 4819163953Srrs } 4820163953Srrs sh->checksum = calc_check; 4821168299Srrs } 4822163953Srrssctp_skip_csum_4: 4823168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4824168124Srrs if (sh->dest_port == 0) { 4825168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4826168124Srrs goto bad; 4827168124Srrs } 4828163953Srrs /* validate mbuf chain length with IP payload length */ 4829163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4830163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4831163953Srrs goto bad; 4832163953Srrs } 4833163953Srrs /* 4834163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4835163953Srrs * IP/SCTP/first chunk header... 4836163953Srrs */ 4837163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4838168299Srrs sh, ch, &inp, &net, vrf_id); 4839163953Srrs /* inp's ref-count increased && stcb locked */ 4840163953Srrs if (inp == NULL) { 4841163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4842163953Srrs 4843163953Srrs SCTP_STAT_INCR(sctps_noport); 4844163953Srrs#ifdef ICMP_BANDLIM 4845163953Srrs /* 4846163953Srrs * we use the bandwidth limiting to protect against sending 4847163953Srrs * too many ABORTS all at once. In this case these count the 4848163953Srrs * same as an ICMP message. 4849163953Srrs */ 4850163953Srrs if (badport_bandlim(0) < 0) 4851163953Srrs goto bad; 4852163953Srrs#endif /* ICMP_BANDLIM */ 4853169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 4854169420Srrs "Sending a ABORT from packet entry!\n"); 4855163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4856163953Srrs /* 4857163953Srrs * we do a trick here to get the INIT tag, dig in 4858163953Srrs * and get the tag from the INIT and put it in the 4859163953Srrs * common header. 4860163953Srrs */ 4861163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4862163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4863163953Srrs (uint8_t *) & chunk_buf); 4864163953Srrs if (init_chk != NULL) 4865163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4866163953Srrs } 4867165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4868169352Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, 4869169352Srrs table_id); 4870165220Srrs goto bad; 4871165220Srrs } 4872165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4873165220Srrs goto bad; 4874165220Srrs } 4875165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4876169352Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, 4877169352Srrs table_id); 4878163953Srrs goto bad; 4879163953Srrs } else if (stcb == NULL) { 4880163953Srrs refcount_up = 1; 4881163953Srrs } 4882163953Srrs#ifdef IPSEC 4883163953Srrs /* 4884163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 4885163953Srrs * idea, so I will leave it in place. 4886163953Srrs */ 4887163953Srrs 4888163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 4889163953Srrs ipsecstat.in_polvio++; 4890163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4891163953Srrs goto bad; 4892163953Srrs } 4893163953Srrs#endif /* IPSEC */ 4894163953Srrs 4895163953Srrs /* 4896163953Srrs * common chunk processing 4897163953Srrs */ 4898163953Srrs length = ip->ip_len + iphlen; 4899163953Srrs offset -= sizeof(struct sctp_chunkhdr); 4900163953Srrs 4901163953Srrs ecn_bits = ip->ip_tos; 4902163953Srrs 4903169655Srrs /* sa_ignore NO_NULL_CHK */ 4904163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 4905169352Srrs inp, stcb, net, ecn_bits, vrf_id, 4906169352Srrs table_id); 4907163953Srrs /* inp's ref-count reduced && stcb unlocked */ 4908163953Srrs if (m) { 4909163953Srrs sctp_m_freem(m); 4910163953Srrs } 4911163953Srrs if ((inp) && (refcount_up)) { 4912163953Srrs /* reduce ref-count */ 4913163953Srrs SCTP_INP_WLOCK(inp); 4914163953Srrs SCTP_INP_DECR_REF(inp); 4915163953Srrs SCTP_INP_WUNLOCK(inp); 4916163953Srrs } 4917163953Srrs return; 4918163953Srrsbad: 4919169420Srrs if (stcb) { 4920163953Srrs SCTP_TCB_UNLOCK(stcb); 4921169420Srrs } 4922163953Srrs if ((inp) && (refcount_up)) { 4923163953Srrs /* reduce ref-count */ 4924163953Srrs SCTP_INP_WLOCK(inp); 4925163953Srrs SCTP_INP_DECR_REF(inp); 4926163953Srrs SCTP_INP_WUNLOCK(inp); 4927163953Srrs } 4928163953Srrs if (m) { 4929163953Srrs sctp_m_freem(m); 4930163953Srrs } 4931163953Srrs return; 4932163953Srrs} 4933