sctp_input.c revision 169352
1163953Srrs/*- 2166675Srrs * Copyright (c) 2001-2007, 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 169352 2007-05-08 00:21:05Z 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> 47163953Srrs 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 88163953Srrs#ifdef SCTP_DEBUG 89163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 90163953Srrs printf("sctp_handle_init: handling INIT tcb:%p\n", stcb); 91163953Srrs } 92163953Srrs#endif 93163953Srrs op_err = NULL; 94163953Srrs init = &cp->init; 95163953Srrs /* First are we accepting? */ 96163953Srrs if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) { 97163953Srrs#ifdef SCTP_DEBUG 98163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 99163953Srrs printf("sctp_handle_init: Abort, so_qlimit:%d\n", inp->sctp_socket->so_qlimit); 100163953Srrs } 101163953Srrs#endif 102163953Srrs /* 103163953Srrs * FIX ME ?? What about TCP model and we have a 104163953Srrs * match/restart case? 105163953Srrs */ 106169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 107169352Srrs vrf_id, table_id); 108168299Srrs if (stcb) 109168299Srrs *abort_no_unlock = 1; 110163953Srrs return; 111163953Srrs } 112163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 113163953Srrs /* Invalid length */ 114163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 115169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 116169352Srrs vrf_id, table_id); 117168299Srrs if (stcb) 118168299Srrs *abort_no_unlock = 1; 119163953Srrs return; 120163953Srrs } 121163953Srrs /* validate parameters */ 122163953Srrs if (init->initiate_tag == 0) { 123163953Srrs /* protocol error... send abort */ 124163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 125169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 126169352Srrs vrf_id, table_id); 127168299Srrs if (stcb) 128168299Srrs *abort_no_unlock = 1; 129163953Srrs return; 130163953Srrs } 131163953Srrs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 132163953Srrs /* invalid parameter... send abort */ 133163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 134169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 135169352Srrs vrf_id, table_id); 136163953Srrs return; 137163953Srrs } 138163953Srrs if (init->num_inbound_streams == 0) { 139163953Srrs /* protocol error... send abort */ 140163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 141169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 142169352Srrs vrf_id, table_id); 143168299Srrs if (stcb) 144168299Srrs *abort_no_unlock = 1; 145163953Srrs return; 146163953Srrs } 147163953Srrs if (init->num_outbound_streams == 0) { 148163953Srrs /* protocol error... send abort */ 149163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 150169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 151169352Srrs vrf_id, table_id); 152168299Srrs if (stcb) 153168299Srrs *abort_no_unlock = 1; 154163953Srrs return; 155163953Srrs } 156163953Srrs init_limit = offset + ntohs(cp->ch.chunk_length); 157163953Srrs if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 158163953Srrs init_limit)) { 159163953Srrs /* auth parameter(s) error... send abort */ 160169352Srrs sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, 161169352Srrs table_id); 162168299Srrs if (stcb) 163168299Srrs *abort_no_unlock = 1; 164163953Srrs return; 165163953Srrs } 166163953Srrs /* send an INIT-ACK w/cookie */ 167163953Srrs#ifdef SCTP_DEBUG 168163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 169163953Srrs printf("sctp_handle_init: sending INIT-ACK\n"); 170163953Srrs } 171163953Srrs#endif 172169352Srrs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, 173169352Srrs table_id); 174163953Srrs} 175163953Srrs 176163953Srrs/* 177163953Srrs * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 178163953Srrs */ 179163953Srrsstatic int 180163953Srrssctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb, 181163953Srrs struct sctp_nets *net) 182163953Srrs{ 183163953Srrs struct sctp_init *init; 184163953Srrs struct sctp_association *asoc; 185163953Srrs struct sctp_nets *lnet; 186163953Srrs unsigned int i; 187163953Srrs 188163953Srrs init = &cp->init; 189163953Srrs asoc = &stcb->asoc; 190163953Srrs /* save off parameters */ 191163953Srrs asoc->peer_vtag = ntohl(init->initiate_tag); 192163953Srrs asoc->peers_rwnd = ntohl(init->a_rwnd); 193163953Srrs if (TAILQ_FIRST(&asoc->nets)) { 194163953Srrs /* update any ssthresh's that may have a default */ 195163953Srrs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 196163953Srrs lnet->ssthresh = asoc->peers_rwnd; 197163953Srrs 198163953Srrs#if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING) 199163953Srrs sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 200163953Srrs#endif 201163953Srrs 202163953Srrs } 203163953Srrs } 204164181Srrs SCTP_TCB_SEND_LOCK(stcb); 205163953Srrs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 206163953Srrs unsigned int newcnt; 207163953Srrs struct sctp_stream_out *outs; 208163953Srrs struct sctp_stream_queue_pending *sp; 209163953Srrs 210163953Srrs /* cut back on number of streams */ 211163953Srrs newcnt = ntohs(init->num_inbound_streams); 212163953Srrs /* This if is probably not needed but I am cautious */ 213163953Srrs if (asoc->strmout) { 214163953Srrs /* First make sure no data chunks are trapped */ 215163953Srrs for (i = newcnt; i < asoc->pre_open_streams; i++) { 216163953Srrs outs = &asoc->strmout[i]; 217163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 218163953Srrs while (sp) { 219163953Srrs TAILQ_REMOVE(&outs->outqueue, sp, 220163953Srrs next); 221163953Srrs asoc->stream_queue_cnt--; 222163953Srrs sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 223163953Srrs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 224163953Srrs sp); 225163953Srrs if (sp->data) { 226163953Srrs sctp_m_freem(sp->data); 227163953Srrs sp->data = NULL; 228163953Srrs } 229163953Srrs sctp_free_remote_addr(sp->net); 230163953Srrs sp->net = NULL; 231163953Srrs /* Free the chunk */ 232163964Srrs printf("sp:%p tcb:%p weird free case\n", 233163979Sru sp, stcb); 234163953Srrs 235163953Srrs sctp_free_a_strmoq(stcb, sp); 236163953Srrs sp = TAILQ_FIRST(&outs->outqueue); 237163953Srrs } 238163953Srrs } 239163953Srrs } 240163953Srrs /* cut back the count and abandon the upper streams */ 241163953Srrs asoc->pre_open_streams = newcnt; 242163953Srrs } 243164181Srrs SCTP_TCB_SEND_UNLOCK(stcb); 244163953Srrs asoc->streamoutcnt = asoc->pre_open_streams; 245163953Srrs /* init tsn's */ 246163953Srrs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 247163953Srrs#ifdef SCTP_MAP_LOGGING 248163953Srrs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 249163953Srrs#endif 250163953Srrs /* This is the next one we expect */ 251163953Srrs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 252163953Srrs 253163953Srrs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 254163953Srrs asoc->cumulative_tsn = asoc->asconf_seq_in; 255163953Srrs asoc->last_echo_tsn = asoc->asconf_seq_in; 256163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 257163953Srrs /* open the requested streams */ 258163953Srrs if (asoc->strmin != NULL) { 259163953Srrs /* Free the old ones */ 260164181Srrs struct sctp_queued_to_read *ctl; 261164181Srrs 262164181Srrs for (i = 0; i < asoc->streamincnt; i++) { 263164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 264164181Srrs while (ctl) { 265164181Srrs TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 266164181Srrs sctp_free_remote_addr(ctl->whoFrom); 267164181Srrs sctp_m_freem(ctl->data); 268164181Srrs ctl->data = NULL; 269164181Srrs sctp_free_a_readq(stcb, ctl); 270164181Srrs ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 271164181Srrs } 272164181Srrs } 273163953Srrs SCTP_FREE(asoc->strmin); 274163953Srrs } 275164181Srrs asoc->streamincnt = ntohs(init->num_outbound_streams); 276164181Srrs if (asoc->streamincnt > MAX_SCTP_STREAMS) { 277164181Srrs asoc->streamincnt = MAX_SCTP_STREAMS; 278164181Srrs } 279163953Srrs SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 280163953Srrs sizeof(struct sctp_stream_in), "StreamsIn"); 281163953Srrs if (asoc->strmin == NULL) { 282163953Srrs /* we didn't get memory for the streams! */ 283163953Srrs#ifdef SCTP_DEBUG 284163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 285163953Srrs printf("process_init: couldn't get memory for the streams!\n"); 286163953Srrs } 287163953Srrs#endif 288163953Srrs return (-1); 289163953Srrs } 290163953Srrs for (i = 0; i < asoc->streamincnt; i++) { 291163953Srrs asoc->strmin[i].stream_no = i; 292163953Srrs asoc->strmin[i].last_sequence_delivered = 0xffff; 293163953Srrs /* 294163953Srrs * U-stream ranges will be set when the cookie is unpacked. 295163953Srrs * Or for the INIT sender they are un set (if pr-sctp not 296163953Srrs * supported) when the INIT-ACK arrives. 297163953Srrs */ 298163953Srrs TAILQ_INIT(&asoc->strmin[i].inqueue); 299168943Srrs asoc->strmin[i].delivery_started = 0; 300163953Srrs } 301163953Srrs /* 302163953Srrs * load_address_from_init will put the addresses into the 303163953Srrs * association when the COOKIE is processed or the INIT-ACK is 304163953Srrs * processed. Both types of COOKIE's existing and new call this 305163953Srrs * routine. It will remove addresses that are no longer in the 306163953Srrs * association (for the restarting case where addresses are 307163953Srrs * removed). Up front when the INIT arrives we will discard it if it 308163953Srrs * is a restart and new addresses have been added. 309163953Srrs */ 310163953Srrs return (0); 311163953Srrs} 312163953Srrs 313163953Srrs/* 314163953Srrs * INIT-ACK message processing/consumption returns value < 0 on error 315163953Srrs */ 316163953Srrsstatic int 317163953Srrssctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 318163953Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 319169352Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, 320169352Srrs uint32_t table_id) 321163953Srrs{ 322163953Srrs struct sctp_association *asoc; 323163953Srrs struct mbuf *op_err; 324163953Srrs int retval, abort_flag; 325163953Srrs uint32_t initack_limit; 326163953Srrs 327163953Srrs /* First verify that we have no illegal param's */ 328163953Srrs abort_flag = 0; 329163953Srrs op_err = NULL; 330163953Srrs 331163953Srrs op_err = sctp_arethere_unrecognized_parameters(m, 332163953Srrs (offset + sizeof(struct sctp_init_chunk)), 333163953Srrs &abort_flag, (struct sctp_chunkhdr *)cp); 334163953Srrs if (abort_flag) { 335163953Srrs /* Send an abort and notify peer */ 336163953Srrs if (op_err != NULL) { 337169352Srrs sctp_send_operr_to(m, iphlen, op_err, 338169352Srrs cp->init.initiate_tag, vrf_id, 339169352Srrs table_id); 340163953Srrs } else { 341163953Srrs /* 342163953Srrs * Just notify (abort_assoc does this if we send an 343163953Srrs * abort). 344163953Srrs */ 345163953Srrs sctp_abort_notification(stcb, 0); 346163953Srrs /* 347163953Srrs * No sense in further INIT's since we will get the 348163953Srrs * same param back 349163953Srrs */ 350165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 351168299Srrs *abort_no_unlock = 1; 352163953Srrs } 353163953Srrs return (-1); 354163953Srrs } 355163953Srrs asoc = &stcb->asoc; 356163953Srrs /* process the peer's parameters in the INIT-ACK */ 357163953Srrs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net); 358163953Srrs if (retval < 0) { 359163953Srrs return (retval); 360163953Srrs } 361163953Srrs initack_limit = offset + ntohs(cp->ch.chunk_length); 362163953Srrs /* load all addresses */ 363163953Srrs if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen, 364163953Srrs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 365163953Srrs NULL))) { 366163953Srrs /* Huh, we should abort */ 367163953Srrs#ifdef SCTP_DEBUG 368163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 369163953Srrs printf("Load addresses from INIT causes an abort %d\n", retval); 370163953Srrs } 371163953Srrs#endif 372163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 373169352Srrs NULL, 0, 0); 374168299Srrs *abort_no_unlock = 1; 375163953Srrs return (-1); 376163953Srrs } 377163953Srrs stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 378163953Srrs stcb->asoc.local_hmacs); 379163953Srrs if (op_err) { 380163953Srrs sctp_queue_op_err(stcb, op_err); 381163953Srrs /* queuing will steal away the mbuf chain to the out queue */ 382163953Srrs op_err = NULL; 383163953Srrs } 384163953Srrs /* extract the cookie and queue it to "echo" it back... */ 385163953Srrs stcb->asoc.overall_error_count = 0; 386163953Srrs net->error_count = 0; 387163953Srrs 388163953Srrs /* 389163953Srrs * Cancel the INIT timer, We do this first before queueing the 390163953Srrs * cookie. We always cancel at the primary to assue that we are 391163953Srrs * canceling the timer started by the INIT which always goes to the 392163953Srrs * primary. 393163953Srrs */ 394163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 395165220Srrs asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 396163953Srrs 397165220Srrs /* calculate the RTO */ 398165220Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered); 399165220Srrs 400163953Srrs retval = sctp_send_cookie_echo(m, offset, stcb, net); 401163953Srrs if (retval < 0) { 402163953Srrs /* 403163953Srrs * No cookie, we probably should send a op error. But in any 404163953Srrs * case if there is no cookie in the INIT-ACK, we can 405163953Srrs * abandon the peer, its broke. 406163953Srrs */ 407163953Srrs if (retval == -3) { 408163953Srrs /* We abort with an error of missing mandatory param */ 409163953Srrs struct mbuf *op_err; 410163953Srrs 411163953Srrs op_err = 412163953Srrs sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 413163953Srrs if (op_err) { 414163953Srrs /* 415163953Srrs * Expand beyond to include the mandatory 416163953Srrs * param cookie 417163953Srrs */ 418163953Srrs struct sctp_inv_mandatory_param *mp; 419163953Srrs 420165647Srrs SCTP_BUF_LEN(op_err) = 421163953Srrs sizeof(struct sctp_inv_mandatory_param); 422163953Srrs mp = mtod(op_err, 423163953Srrs struct sctp_inv_mandatory_param *); 424163953Srrs /* Subtract the reserved param */ 425163953Srrs mp->length = 426163953Srrs htons(sizeof(struct sctp_inv_mandatory_param) - 2); 427163953Srrs mp->num_param = htonl(1); 428163953Srrs mp->param = htons(SCTP_STATE_COOKIE); 429163953Srrs mp->resv = 0; 430163953Srrs } 431163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 432169352Srrs sh, op_err, 0, 0); 433168299Srrs *abort_no_unlock = 1; 434163953Srrs } 435163953Srrs return (retval); 436163953Srrs } 437163953Srrs return (0); 438163953Srrs} 439163953Srrs 440163953Srrsstatic void 441163953Srrssctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 442163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 443163953Srrs{ 444163953Srrs struct sockaddr_storage store; 445163953Srrs struct sockaddr_in *sin; 446163953Srrs struct sockaddr_in6 *sin6; 447163953Srrs struct sctp_nets *r_net; 448163953Srrs struct timeval tv; 449163953Srrs 450163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 451163953Srrs /* Invalid length */ 452163953Srrs return; 453163953Srrs } 454163953Srrs sin = (struct sockaddr_in *)&store; 455163953Srrs sin6 = (struct sockaddr_in6 *)&store; 456163953Srrs 457163953Srrs memset(&store, 0, sizeof(store)); 458163953Srrs if (cp->heartbeat.hb_info.addr_family == AF_INET && 459163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 460163953Srrs sin->sin_family = cp->heartbeat.hb_info.addr_family; 461163953Srrs sin->sin_len = cp->heartbeat.hb_info.addr_len; 462163953Srrs sin->sin_port = stcb->rport; 463163953Srrs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 464163953Srrs sizeof(sin->sin_addr)); 465163953Srrs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 && 466163953Srrs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 467163953Srrs sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 468163953Srrs sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 469163953Srrs sin6->sin6_port = stcb->rport; 470163953Srrs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 471163953Srrs sizeof(sin6->sin6_addr)); 472163953Srrs } else { 473163953Srrs return; 474163953Srrs } 475163953Srrs r_net = sctp_findnet(stcb, (struct sockaddr *)sin); 476163953Srrs if (r_net == NULL) { 477163953Srrs#ifdef SCTP_DEBUG 478163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 479163953Srrs printf("Huh? I can't find the address I sent it to, discard\n"); 480163953Srrs } 481163953Srrs#endif 482163953Srrs return; 483163953Srrs } 484163953Srrs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 485163953Srrs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 486163953Srrs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 487163953Srrs /* 488163953Srrs * If the its a HB and it's random value is correct when can 489163953Srrs * confirm the destination. 490163953Srrs */ 491163953Srrs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 492167598Srrs if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 493167598Srrs stcb->asoc.primary_destination = r_net; 494167598Srrs r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 495167598Srrs r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 496167598Srrs r_net = TAILQ_FIRST(&stcb->asoc.nets); 497167598Srrs if (r_net != stcb->asoc.primary_destination) { 498167598Srrs /* 499167598Srrs * first one on the list is NOT the primary 500167598Srrs * sctp_cmpaddr() is much more efficent if 501167598Srrs * the primary is the first on the list, 502167598Srrs * make it so. 503167598Srrs */ 504167598Srrs TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 505167598Srrs TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 506167598Srrs } 507167598Srrs } 508163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 509163953Srrs stcb, 0, (void *)r_net); 510163953Srrs } 511163953Srrs r_net->error_count = 0; 512163953Srrs r_net->hb_responded = 1; 513163953Srrs tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 514163953Srrs tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 515163953Srrs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) { 516163953Srrs r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE; 517163953Srrs r_net->dest_state |= SCTP_ADDR_REACHABLE; 518163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 519163953Srrs SCTP_HEARTBEAT_SUCCESS, (void *)r_net); 520163953Srrs /* now was it the primary? if so restore */ 521163953Srrs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) { 522163953Srrs sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net); 523163953Srrs } 524163953Srrs } 525163953Srrs /* Now lets do a RTO with this */ 526163953Srrs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv); 527163953Srrs} 528163953Srrs 529163953Srrsstatic void 530163953Srrssctp_handle_abort(struct sctp_abort_chunk *cp, 531163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 532163953Srrs{ 533163953Srrs 534163953Srrs#ifdef SCTP_DEBUG 535163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 536163953Srrs printf("sctp_handle_abort: handling ABORT\n"); 537163953Srrs } 538163953Srrs#endif 539163953Srrs if (stcb == NULL) 540163953Srrs return; 541163953Srrs /* verify that the destination addr is in the association */ 542163953Srrs /* ignore abort for addresses being deleted */ 543163953Srrs 544163953Srrs /* stop any receive timers */ 545165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 546163953Srrs /* notify user of the abort and clean up... */ 547163953Srrs sctp_abort_notification(stcb, 0); 548163953Srrs /* free the tcb */ 549163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 550163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 551163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 552163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 553163953Srrs } 554168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 555168859Srrs sctp_print_out_track_log(stcb); 556168859Srrs#endif 557165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 558163953Srrs#ifdef SCTP_DEBUG 559163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 560163953Srrs printf("sctp_handle_abort: finished\n"); 561163953Srrs } 562163953Srrs#endif 563163953Srrs} 564163953Srrs 565163953Srrsstatic void 566163953Srrssctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 567163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 568163953Srrs{ 569163953Srrs struct sctp_association *asoc; 570163953Srrs int some_on_streamwheel; 571163953Srrs 572163953Srrs#ifdef SCTP_DEBUG 573163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 574163953Srrs printf("sctp_handle_shutdown: handling SHUTDOWN\n"); 575163953Srrs } 576163953Srrs#endif 577163953Srrs if (stcb == NULL) 578163953Srrs return; 579165220Srrs asoc = &stcb->asoc; 580165220Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 581165220Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 582163953Srrs return; 583163953Srrs } 584163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 585163953Srrs /* Shutdown NOT the expected size */ 586163953Srrs return; 587163953Srrs } else { 588163953Srrs sctp_update_acked(stcb, cp, net, abort_flag); 589163953Srrs } 590165220Srrs if (asoc->control_pdapi) { 591163953Srrs /* 592163953Srrs * With a normal shutdown we assume the end of last record. 593163953Srrs */ 594163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 595165220Srrs asoc->control_pdapi->end_added = 1; 596165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 597165220Srrs asoc->control_pdapi = NULL; 598163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 599163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 600163953Srrs } 601163953Srrs /* goto SHUTDOWN_RECEIVED state to block new requests */ 602163953Srrs if (stcb->sctp_socket) { 603163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 604163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 605163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED; 606163953Srrs /* 607163953Srrs * notify upper layer that peer has initiated a 608163953Srrs * shutdown 609163953Srrs */ 610163953Srrs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL); 611163953Srrs 612163953Srrs /* reset time */ 613163953Srrs SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 614163953Srrs } 615163953Srrs } 616163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 617163953Srrs /* 618163953Srrs * stop the shutdown timer, since we WILL move to 619163953Srrs * SHUTDOWN-ACK-SENT. 620163953Srrs */ 621165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 622163953Srrs } 623163953Srrs /* Now are we there yet? */ 624163953Srrs some_on_streamwheel = 0; 625163953Srrs if (!TAILQ_EMPTY(&asoc->out_wheel)) { 626163953Srrs /* Check to see if some data queued */ 627163953Srrs struct sctp_stream_out *outs; 628163953Srrs 629163953Srrs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) { 630163953Srrs if (!TAILQ_EMPTY(&outs->outqueue)) { 631163953Srrs some_on_streamwheel = 1; 632163953Srrs break; 633163953Srrs } 634163953Srrs } 635163953Srrs } 636163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 637163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 638163953Srrs some_on_streamwheel) { 639163953Srrs /* By returning we will push more data out */ 640163953Srrs return; 641163953Srrs } else { 642163953Srrs /* no outstanding data to send, so move on... */ 643163953Srrs /* send SHUTDOWN-ACK */ 644163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 645163953Srrs /* move to SHUTDOWN-ACK-SENT state */ 646166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 647166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 648163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 649163953Srrs } 650163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT; 651163953Srrs 652163953Srrs /* start SHUTDOWN timer */ 653163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 654163953Srrs stcb, net); 655163953Srrs } 656163953Srrs} 657163953Srrs 658163953Srrsstatic void 659163953Srrssctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp, 660163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 661163953Srrs{ 662163953Srrs struct sctp_association *asoc; 663163953Srrs 664163953Srrs#ifdef SCTP_DEBUG 665163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 666163953Srrs printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 667163953Srrs } 668163953Srrs#endif 669163953Srrs if (stcb == NULL) 670163953Srrs return; 671163953Srrs 672163953Srrs asoc = &stcb->asoc; 673163953Srrs /* process according to association state */ 674163953Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 675163953Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 676163953Srrs /* unexpected SHUTDOWN-ACK... so ignore... */ 677163953Srrs SCTP_TCB_UNLOCK(stcb); 678163953Srrs return; 679163953Srrs } 680165220Srrs if (asoc->control_pdapi) { 681163953Srrs /* 682163953Srrs * With a normal shutdown we assume the end of last record. 683163953Srrs */ 684163953Srrs SCTP_INP_READ_LOCK(stcb->sctp_ep); 685165220Srrs asoc->control_pdapi->end_added = 1; 686165220Srrs asoc->control_pdapi->pdapi_aborted = 1; 687165220Srrs asoc->control_pdapi = NULL; 688163953Srrs SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 689163996Srrs sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 690163953Srrs } 691163953Srrs /* are the queues empty? */ 692163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 693163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 694163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 695164205Srrs sctp_report_all_outbound(stcb, 0); 696163953Srrs } 697163953Srrs /* stop the timer */ 698165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 699163953Srrs /* send SHUTDOWN-COMPLETE */ 700163953Srrs sctp_send_shutdown_complete(stcb, net); 701163953Srrs /* notify upper layer protocol */ 702163953Srrs if (stcb->sctp_socket) { 703163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 704163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 705163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 706163953Srrs /* Set the connected flag to disconnected */ 707163953Srrs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0; 708163953Srrs } 709163953Srrs } 710163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 711163953Srrs /* free the TCB but first save off the ep */ 712165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 713165220Srrs SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 714163953Srrs} 715163953Srrs 716163953Srrs/* 717163953Srrs * Skip past the param header and then we will find the chunk that caused the 718163953Srrs * problem. There are two possiblities ASCONF or FWD-TSN other than that and 719163953Srrs * our peer must be broken. 720163953Srrs */ 721163953Srrsstatic void 722163953Srrssctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 723163953Srrs struct sctp_nets *net) 724163953Srrs{ 725163953Srrs struct sctp_chunkhdr *chk; 726163953Srrs 727163953Srrs chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 728163953Srrs switch (chk->chunk_type) { 729163953Srrs case SCTP_ASCONF_ACK: 730163953Srrs case SCTP_ASCONF: 731163953Srrs sctp_asconf_cleanup(stcb, net); 732163953Srrs break; 733163953Srrs case SCTP_FORWARD_CUM_TSN: 734163953Srrs stcb->asoc.peer_supports_prsctp = 0; 735163953Srrs break; 736163953Srrs default: 737163953Srrs#ifdef SCTP_DEBUG 738163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 739163953Srrs printf("Peer does not support chunk type %d(%x)??\n", 740163953Srrs chk->chunk_type, (uint32_t) chk->chunk_type); 741163953Srrs } 742163953Srrs#endif 743163953Srrs break; 744163953Srrs } 745163953Srrs} 746163953Srrs 747163953Srrs/* 748163953Srrs * Skip past the param header and then we will find the param that caused the 749163953Srrs * problem. There are a number of param's in a ASCONF OR the prsctp param 750163953Srrs * these will turn of specific features. 751163953Srrs */ 752163953Srrsstatic void 753163953Srrssctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 754163953Srrs{ 755163953Srrs struct sctp_paramhdr *pbad; 756163953Srrs 757163953Srrs pbad = phdr + 1; 758163953Srrs switch (ntohs(pbad->param_type)) { 759163953Srrs /* pr-sctp draft */ 760163953Srrs case SCTP_PRSCTP_SUPPORTED: 761163953Srrs stcb->asoc.peer_supports_prsctp = 0; 762163953Srrs break; 763163953Srrs case SCTP_SUPPORTED_CHUNK_EXT: 764163953Srrs break; 765163953Srrs /* draft-ietf-tsvwg-addip-sctp */ 766163953Srrs case SCTP_ECN_NONCE_SUPPORTED: 767163953Srrs stcb->asoc.peer_supports_ecn_nonce = 0; 768163953Srrs stcb->asoc.ecn_nonce_allowed = 0; 769163953Srrs stcb->asoc.ecn_allowed = 0; 770163953Srrs break; 771163953Srrs case SCTP_ADD_IP_ADDRESS: 772163953Srrs case SCTP_DEL_IP_ADDRESS: 773163953Srrs case SCTP_SET_PRIM_ADDR: 774163953Srrs stcb->asoc.peer_supports_asconf = 0; 775163953Srrs break; 776163953Srrs case SCTP_SUCCESS_REPORT: 777163953Srrs case SCTP_ERROR_CAUSE_IND: 778163953Srrs#ifdef SCTP_DEBUG 779163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 780163953Srrs printf("Huh, the peer does not support success? or error cause?\n"); 781163953Srrs printf("Turning off ASCONF to this strange peer\n"); 782163953Srrs } 783163953Srrs#endif 784163953Srrs stcb->asoc.peer_supports_asconf = 0; 785163953Srrs break; 786163953Srrs default: 787163953Srrs#ifdef SCTP_DEBUG 788163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 789163953Srrs printf("Peer does not support param type %d(%x)??\n", 790163953Srrs pbad->param_type, (uint32_t) pbad->param_type); 791163953Srrs } 792163953Srrs#endif 793163953Srrs break; 794163953Srrs } 795163953Srrs} 796163953Srrs 797163953Srrsstatic int 798163953Srrssctp_handle_error(struct sctp_chunkhdr *ch, 799163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 800163953Srrs{ 801163953Srrs int chklen; 802163953Srrs struct sctp_paramhdr *phdr; 803163953Srrs uint16_t error_type; 804163953Srrs uint16_t error_len; 805163953Srrs struct sctp_association *asoc; 806163953Srrs 807163953Srrs int adjust; 808163953Srrs 809163953Srrs /* parse through all of the errors and process */ 810163953Srrs asoc = &stcb->asoc; 811163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)ch + 812163953Srrs sizeof(struct sctp_chunkhdr)); 813163953Srrs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 814163953Srrs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 815163953Srrs /* Process an Error Cause */ 816163953Srrs error_type = ntohs(phdr->param_type); 817163953Srrs error_len = ntohs(phdr->param_length); 818163953Srrs if ((error_len > chklen) || (error_len == 0)) { 819163953Srrs /* invalid param length for this param */ 820163953Srrs#ifdef SCTP_DEBUG 821163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 822163953Srrs printf("Bogus length in error param- chunk left:%d errorlen:%d\n", 823163953Srrs chklen, error_len); 824163953Srrs } 825163953Srrs#endif /* SCTP_DEBUG */ 826163953Srrs return (0); 827163953Srrs } 828163953Srrs switch (error_type) { 829163953Srrs case SCTP_CAUSE_INVALID_STREAM: 830163953Srrs case SCTP_CAUSE_MISSING_PARAM: 831163953Srrs case SCTP_CAUSE_INVALID_PARAM: 832163953Srrs case SCTP_CAUSE_NO_USER_DATA: 833163953Srrs#ifdef SCTP_DEBUG 834163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 835163953Srrs printf("Software error we got a %d back? We have a bug :/ (or do they?)\n", 836163953Srrs error_type); 837163953Srrs } 838163953Srrs#endif 839163953Srrs break; 840163953Srrs case SCTP_CAUSE_STALE_COOKIE: 841163953Srrs /* 842163953Srrs * We only act if we have echoed a cookie and are 843163953Srrs * waiting. 844163953Srrs */ 845163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 846163953Srrs int *p; 847163953Srrs 848163953Srrs p = (int *)((caddr_t)phdr + sizeof(*phdr)); 849163953Srrs /* Save the time doubled */ 850163953Srrs asoc->cookie_preserve_req = ntohl(*p) << 1; 851163953Srrs asoc->stale_cookie_count++; 852163953Srrs if (asoc->stale_cookie_count > 853163953Srrs asoc->max_init_times) { 854163953Srrs sctp_abort_notification(stcb, 0); 855163953Srrs /* now free the asoc */ 856165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 857163953Srrs return (-1); 858163953Srrs } 859163953Srrs /* blast back to INIT state */ 860163953Srrs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 861163953Srrs asoc->state |= SCTP_STATE_COOKIE_WAIT; 862163953Srrs 863163953Srrs sctp_stop_all_cookie_timers(stcb); 864163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 865163953Srrs } 866163953Srrs break; 867163953Srrs case SCTP_CAUSE_UNRESOLVABLE_ADDR: 868163953Srrs /* 869163953Srrs * Nothing we can do here, we don't do hostname 870163953Srrs * addresses so if the peer does not like my IPv6 871163953Srrs * (or IPv4 for that matter) it does not matter. If 872163953Srrs * they don't support that type of address, they can 873163953Srrs * NOT possibly get that packet type... i.e. with no 874163953Srrs * IPv6 you can't recieve a IPv6 packet. so we can 875163953Srrs * safely ignore this one. If we ever added support 876163953Srrs * for HOSTNAME Addresses, then we would need to do 877163953Srrs * something here. 878163953Srrs */ 879163953Srrs break; 880163953Srrs case SCTP_CAUSE_UNRECOG_CHUNK: 881163953Srrs sctp_process_unrecog_chunk(stcb, phdr, net); 882163953Srrs break; 883163953Srrs case SCTP_CAUSE_UNRECOG_PARAM: 884163953Srrs sctp_process_unrecog_param(stcb, phdr); 885163953Srrs break; 886163953Srrs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 887163953Srrs /* 888163953Srrs * We ignore this since the timer will drive out a 889163953Srrs * new cookie anyway and there timer will drive us 890163953Srrs * to send a SHUTDOWN_COMPLETE. We can't send one 891163953Srrs * here since we don't have their tag. 892163953Srrs */ 893163953Srrs break; 894163953Srrs case SCTP_CAUSE_DELETING_LAST_ADDR: 895163953Srrs case SCTP_CAUSE_RESOURCE_SHORTAGE: 896163953Srrs case SCTP_CAUSE_DELETING_SRC_ADDR: 897163953Srrs /* 898163953Srrs * We should NOT get these here, but in a 899166086Srrs * ASCONF-ACK. 900163953Srrs */ 901163953Srrs#ifdef SCTP_DEBUG 902163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 903163953Srrs printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n", 904163953Srrs error_type); 905163953Srrs } 906163953Srrs#endif 907163953Srrs break; 908163953Srrs case SCTP_CAUSE_OUT_OF_RESC: 909163953Srrs /* 910163953Srrs * And what, pray tell do we do with the fact that 911163953Srrs * the peer is out of resources? Not really sure we 912166086Srrs * could do anything but abort. I suspect this 913163953Srrs * should have came WITH an abort instead of in a 914163953Srrs * OP-ERROR. 915163953Srrs */ 916163953Srrs break; 917163953Srrs default: 918163953Srrs#ifdef SCTP_DEBUG 919163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 920163953Srrs /* don't know what this error cause is... */ 921163953Srrs printf("sctp_handle_error: unknown error type = 0x%xh\n", 922163953Srrs error_type); 923163953Srrs } 924163953Srrs#endif /* SCTP_DEBUG */ 925163953Srrs break; 926163953Srrs } 927163953Srrs adjust = SCTP_SIZE32(error_len); 928163953Srrs chklen -= adjust; 929163953Srrs phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 930163953Srrs } 931163953Srrs return (0); 932163953Srrs} 933163953Srrs 934163953Srrsstatic int 935169352Srrssctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 936169352Srrs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 937169352Srrs struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, 938169352Srrs uint32_t table_id) 939163953Srrs{ 940163953Srrs struct sctp_init_ack *init_ack; 941163953Srrs int *state; 942163953Srrs struct mbuf *op_err; 943163953Srrs 944163953Srrs#ifdef SCTP_DEBUG 945163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 946163953Srrs printf("sctp_handle_init_ack: handling INIT-ACK\n"); 947163953Srrs } 948163953Srrs#endif 949163953Srrs if (stcb == NULL) { 950163953Srrs#ifdef SCTP_DEBUG 951163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 952163953Srrs printf("sctp_handle_init_ack: TCB is null\n"); 953163953Srrs } 954163953Srrs#endif 955163953Srrs return (-1); 956163953Srrs } 957163953Srrs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 958163953Srrs /* Invalid length */ 959163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 960163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 961169352Srrs op_err, 0, 0); 962168299Srrs *abort_no_unlock = 1; 963163953Srrs return (-1); 964163953Srrs } 965163953Srrs init_ack = &cp->init; 966163953Srrs /* validate parameters */ 967163953Srrs if (init_ack->initiate_tag == 0) { 968163953Srrs /* protocol error... send an abort */ 969163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 970163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 971169352Srrs op_err, 0, 0); 972168299Srrs *abort_no_unlock = 1; 973163953Srrs return (-1); 974163953Srrs } 975163953Srrs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 976163953Srrs /* protocol error... send an abort */ 977163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 978163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 979169352Srrs op_err, 0, 0); 980168299Srrs *abort_no_unlock = 1; 981163953Srrs return (-1); 982163953Srrs } 983163953Srrs if (init_ack->num_inbound_streams == 0) { 984163953Srrs /* protocol error... send an abort */ 985163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 986163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 987169352Srrs op_err, 0, 0); 988168299Srrs *abort_no_unlock = 1; 989163953Srrs return (-1); 990163953Srrs } 991163953Srrs if (init_ack->num_outbound_streams == 0) { 992163953Srrs /* protocol error... send an abort */ 993163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 994163953Srrs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 995169352Srrs op_err, 0, 0); 996168299Srrs *abort_no_unlock = 1; 997163953Srrs return (-1); 998163953Srrs } 999163953Srrs /* process according to association state... */ 1000163953Srrs state = &stcb->asoc.state; 1001163953Srrs switch (*state & SCTP_STATE_MASK) { 1002163953Srrs case SCTP_STATE_COOKIE_WAIT: 1003163953Srrs /* this is the expected state for this chunk */ 1004163953Srrs /* process the INIT-ACK parameters */ 1005163953Srrs if (stcb->asoc.primary_destination->dest_state & 1006163953Srrs SCTP_ADDR_UNCONFIRMED) { 1007163953Srrs /* 1008163953Srrs * The primary is where we sent the INIT, we can 1009163953Srrs * always consider it confirmed when the INIT-ACK is 1010163953Srrs * returned. Do this before we load addresses 1011163953Srrs * though. 1012163953Srrs */ 1013163953Srrs stcb->asoc.primary_destination->dest_state &= 1014163953Srrs ~SCTP_ADDR_UNCONFIRMED; 1015163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1016163953Srrs stcb, 0, (void *)stcb->asoc.primary_destination); 1017163953Srrs } 1018169352Srrs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1019169352Srrs net, abort_no_unlock, vrf_id, 1020169352Srrs table_id) < 0) { 1021163953Srrs /* error in parsing parameters */ 1022163953Srrs return (-1); 1023163953Srrs } 1024163953Srrs /* update our state */ 1025163953Srrs#ifdef SCTP_DEBUG 1026163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 1027163953Srrs printf("moving to COOKIE-ECHOED state\n"); 1028163953Srrs } 1029163953Srrs#endif 1030163953Srrs if (*state & SCTP_STATE_SHUTDOWN_PENDING) { 1031163953Srrs *state = SCTP_STATE_COOKIE_ECHOED | 1032163953Srrs SCTP_STATE_SHUTDOWN_PENDING; 1033163953Srrs } else { 1034163953Srrs *state = SCTP_STATE_COOKIE_ECHOED; 1035163953Srrs } 1036163953Srrs 1037163953Srrs /* reset the RTO calc */ 1038163953Srrs stcb->asoc.overall_error_count = 0; 1039163953Srrs SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1040163953Srrs /* 1041163953Srrs * collapse the init timer back in case of a exponential 1042165220Srrs * backoff 1043163953Srrs */ 1044163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1045163953Srrs stcb, net); 1046163953Srrs /* 1047163953Srrs * the send at the end of the inbound data processing will 1048163953Srrs * cause the cookie to be sent 1049163953Srrs */ 1050163953Srrs break; 1051163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 1052163953Srrs /* incorrect state... discard */ 1053163953Srrs break; 1054163953Srrs case SCTP_STATE_COOKIE_ECHOED: 1055163953Srrs /* incorrect state... discard */ 1056163953Srrs break; 1057163953Srrs case SCTP_STATE_OPEN: 1058163953Srrs /* incorrect state... discard */ 1059163953Srrs break; 1060163953Srrs case SCTP_STATE_EMPTY: 1061163953Srrs case SCTP_STATE_INUSE: 1062163953Srrs default: 1063163953Srrs /* incorrect state... discard */ 1064163953Srrs return (-1); 1065163953Srrs break; 1066163953Srrs } /* end switch asoc state */ 1067163953Srrs#ifdef SCTP_DEBUG 1068163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1069163953Srrs printf("Leaving handle-init-ack end\n"); 1070163953Srrs } 1071163953Srrs#endif 1072163953Srrs return (0); 1073163953Srrs} 1074163953Srrs 1075163953Srrs 1076163953Srrs/* 1077163953Srrs * handle a state cookie for an existing association m: input packet mbuf 1078163953Srrs * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1079163953Srrs * "split" mbuf and the cookie signature does not exist offset: offset into 1080163953Srrs * mbuf to the cookie-echo chunk 1081163953Srrs */ 1082163953Srrsstatic struct sctp_tcb * 1083163953Srrssctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1084163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1085163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 1086169352Srrs struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id, 1087169352Srrs uint32_t vrf_id, uint32_t table_id) 1088163953Srrs{ 1089163953Srrs struct sctp_association *asoc; 1090163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1091163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1092163953Srrs int chk_length; 1093165220Srrs int init_offset, initack_offset, i; 1094163953Srrs int retval; 1095164205Srrs int spec_flag = 0; 1096166023Srrs int how_indx; 1097163953Srrs 1098163953Srrs /* I know that the TCB is non-NULL from the caller */ 1099163953Srrs asoc = &stcb->asoc; 1100166675Srrs for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1101166023Srrs if (asoc->cookie_how[how_indx] == 0) 1102166023Srrs break; 1103166023Srrs } 1104166023Srrs if (how_indx < sizeof(asoc->cookie_how)) { 1105166023Srrs asoc->cookie_how[how_indx] = 1; 1106166023Srrs } 1107163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1108163953Srrs /* SHUTDOWN came in after sending INIT-ACK */ 1109163953Srrs struct mbuf *op_err; 1110163953Srrs struct sctp_paramhdr *ph; 1111163953Srrs 1112163953Srrs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1113163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1114165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 1115163953Srrs if (op_err == NULL) { 1116163953Srrs /* FOOBAR */ 1117163953Srrs return (NULL); 1118163953Srrs } 1119163953Srrs /* pre-reserve some space */ 1120165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1121165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1122165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1123163953Srrs /* Set the len */ 1124165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1125163953Srrs ph = mtod(op_err, struct sctp_paramhdr *); 1126163953Srrs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1127163953Srrs ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1128169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1129169352Srrs vrf_id, table_id); 1130166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1131166023Srrs asoc->cookie_how[how_indx] = 2; 1132163953Srrs return (NULL); 1133163953Srrs } 1134163953Srrs /* 1135163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1136163953Srrs * INIT should start after the cookie-echo header struct (chunk 1137163953Srrs * header, state cookie header struct) 1138163953Srrs */ 1139163953Srrs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1140163953Srrs 1141163953Srrs init_cp = (struct sctp_init_chunk *) 1142163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1143163953Srrs (uint8_t *) & init_buf); 1144163953Srrs if (init_cp == NULL) { 1145163953Srrs /* could not pull a INIT chunk in cookie */ 1146163953Srrs return (NULL); 1147163953Srrs } 1148163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1149163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1150163953Srrs return (NULL); 1151163953Srrs } 1152163953Srrs /* 1153163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1154163953Srrs * INIT-ACK follows the INIT chunk 1155163953Srrs */ 1156163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1157163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1158163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1159163953Srrs (uint8_t *) & initack_buf); 1160163953Srrs if (initack_cp == NULL) { 1161163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1162163953Srrs return (NULL); 1163163953Srrs } 1164163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1165163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1166163953Srrs return (NULL); 1167163953Srrs } 1168163953Srrs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1169163953Srrs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1170163953Srrs /* 1171163953Srrs * case D in Section 5.2.4 Table 2: MMAA process accordingly 1172163953Srrs * to get into the OPEN state 1173163953Srrs */ 1174166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1175166086Srrs#ifdef INVARIANTS 1176166023Srrs panic("Case D and non-match seq?"); 1177166086Srrs#else 1178166086Srrs printf("Case D, seq non-match %x vs %x?\n", 1179166086Srrs ntohl(initack_cp->init.initial_tsn), asoc->init_seq_number); 1180166086Srrs#endif 1181166023Srrs } 1182163953Srrs switch SCTP_GET_STATE 1183163953Srrs (asoc) { 1184163953Srrs case SCTP_STATE_COOKIE_WAIT: 1185166023Srrs case SCTP_STATE_COOKIE_ECHOED: 1186163953Srrs /* 1187169352Srrs * INIT was sent but got a COOKIE_ECHO with the 1188166023Srrs * correct tags... just accept it...but we must 1189166023Srrs * process the init so that we can make sure we have 1190166023Srrs * the right seq no's. 1191163953Srrs */ 1192163953Srrs /* First we must process the INIT !! */ 1193163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1194163953Srrs if (retval < 0) { 1195166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1196166023Srrs asoc->cookie_how[how_indx] = 3; 1197163953Srrs return (NULL); 1198163953Srrs } 1199163953Srrs /* we have already processed the INIT so no problem */ 1200163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1201165220Srrs net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1202165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1203163953Srrs /* update current state */ 1204166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1205166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1206166675Srrs else 1207166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1208163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1209166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1210163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1211163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1212163953Srrs 1213166675Srrs } else { 1214163953Srrs /* if ok, move to OPEN state */ 1215163953Srrs asoc->state = SCTP_STATE_OPEN; 1216163953Srrs } 1217166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1218165220Srrs sctp_stop_all_cookie_timers(stcb); 1219163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1220163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1221163953Srrs (inp->sctp_socket->so_qlimit == 0) 1222163953Srrs ) { 1223163953Srrs /* 1224163953Srrs * Here is where collision would go if we 1225163953Srrs * did a connect() and instead got a 1226163953Srrs * init/init-ack/cookie done before the 1227163953Srrs * init-ack came back.. 1228163953Srrs */ 1229163953Srrs stcb->sctp_ep->sctp_flags |= 1230163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1231163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1232163953Srrs } 1233163953Srrs /* notify upper layer */ 1234163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1235163953Srrs /* 1236163953Srrs * since we did not send a HB make sure we don't 1237163953Srrs * double things 1238163953Srrs */ 1239163953Srrs net->hb_responded = 1; 1240163953Srrs 1241163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1242163953Srrs (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1243163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1244163953Srrs inp, stcb, NULL); 1245163953Srrs } 1246163953Srrs break; 1247163953Srrs default: 1248163953Srrs /* 1249163953Srrs * we're in the OPEN state (or beyond), so peer must 1250163953Srrs * have simply lost the COOKIE-ACK 1251163953Srrs */ 1252163953Srrs break; 1253163953Srrs } /* end switch */ 1254165220Srrs sctp_stop_all_cookie_timers(stcb); 1255163953Srrs /* 1256163953Srrs * We ignore the return code here.. not sure if we should 1257163953Srrs * somehow abort.. but we do have an existing asoc. This 1258163953Srrs * really should not fail. 1259163953Srrs */ 1260163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1261163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1262163953Srrs initack_offset, sh, init_src)) { 1263166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1264166023Srrs asoc->cookie_how[how_indx] = 4; 1265163953Srrs return (NULL); 1266163953Srrs } 1267163953Srrs /* respond with a COOKIE-ACK */ 1268165220Srrs sctp_toss_old_cookies(stcb, asoc); 1269163953Srrs sctp_send_cookie_ack(stcb); 1270166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1271166023Srrs asoc->cookie_how[how_indx] = 5; 1272163953Srrs return (stcb); 1273169352Srrs } 1274163953Srrs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1275163953Srrs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1276163953Srrs cookie->tie_tag_my_vtag == 0 && 1277163953Srrs cookie->tie_tag_peer_vtag == 0) { 1278163953Srrs /* 1279163953Srrs * case C in Section 5.2.4 Table 2: XMOO silently discard 1280163953Srrs */ 1281166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1282166023Srrs asoc->cookie_how[how_indx] = 6; 1283163953Srrs return (NULL); 1284163953Srrs } 1285163953Srrs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag && 1286163953Srrs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag || 1287163953Srrs init_cp->init.initiate_tag == 0)) { 1288163953Srrs /* 1289163953Srrs * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1290163953Srrs * should be ok, re-accept peer info 1291163953Srrs */ 1292166023Srrs if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1293166023Srrs /* 1294166023Srrs * Extension of case C. If we hit this, then the 1295166023Srrs * random number generator returned the same vtag 1296166023Srrs * when we first sent our INIT-ACK and when we later 1297166023Srrs * sent our INIT. The side with the seq numbers that 1298166023Srrs * are different will be the one that normnally 1299166023Srrs * would have hit case C. This in effect "extends" 1300166023Srrs * our vtags in this collision case to be 64 bits. 1301166023Srrs * The same collision could occur aka you get both 1302166023Srrs * vtag and seq number the same twice in a row.. but 1303166023Srrs * is much less likely. If it did happen then we 1304166023Srrs * would proceed through and bring up the assoc.. we 1305166023Srrs * may end up with the wrong stream setup however.. 1306166023Srrs * which would be bad.. but there is no way to 1307166023Srrs * tell.. until we send on a stream that does not 1308166023Srrs * exist :-) 1309166023Srrs */ 1310166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1311166023Srrs asoc->cookie_how[how_indx] = 7; 1312166023Srrs 1313166023Srrs return (NULL); 1314166023Srrs } 1315166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1316166023Srrs asoc->cookie_how[how_indx] = 8; 1317165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1318163953Srrs sctp_stop_all_cookie_timers(stcb); 1319163953Srrs /* 1320163953Srrs * since we did not send a HB make sure we don't double 1321163953Srrs * things 1322163953Srrs */ 1323163953Srrs net->hb_responded = 1; 1324163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1325163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1326163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1327163953Srrs NULL); 1328163953Srrs } 1329163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1330164205Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1331164205Srrs 1332164205Srrs /* Note last_cwr_tsn? where is this used? */ 1333163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1334164205Srrs if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1335164205Srrs /* 1336164205Srrs * Ok the peer probably discarded our data (if we 1337164205Srrs * echoed a cookie+data). So anything on the 1338164205Srrs * sent_queue should be marked for retransmit, we 1339164205Srrs * may not get something to kick us so it COULD 1340164205Srrs * still take a timeout to move these.. but it can't 1341164205Srrs * hurt to mark them. 1342164205Srrs */ 1343164205Srrs struct sctp_tmit_chunk *chk; 1344163953Srrs 1345164205Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1346164205Srrs if (chk->sent < SCTP_DATAGRAM_RESEND) { 1347164205Srrs chk->sent = SCTP_DATAGRAM_RESEND; 1348168124Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1349164205Srrs spec_flag++; 1350164205Srrs } 1351164205Srrs } 1352164205Srrs 1353164205Srrs } 1354163953Srrs /* process the INIT info (peer's info) */ 1355163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1356163953Srrs if (retval < 0) { 1357166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1358166023Srrs asoc->cookie_how[how_indx] = 9; 1359163953Srrs return (NULL); 1360163953Srrs } 1361163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1362163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1363163953Srrs initack_offset, sh, init_src)) { 1364166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1365166023Srrs asoc->cookie_how[how_indx] = 10; 1366163953Srrs return (NULL); 1367163953Srrs } 1368163953Srrs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1369163953Srrs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1370163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1371163953Srrs 1372163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1373163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1374163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1375163953Srrs stcb->sctp_ep->sctp_flags |= 1376163953Srrs SCTP_PCB_FLAGS_CONNECTED; 1377163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1378163953Srrs } 1379166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1380166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1381166675Srrs else 1382166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1383166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1384166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1385166675Srrs } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1386166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1387166675Srrs } else { 1388166675Srrs SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1389163953Srrs } 1390163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1391166675Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1392163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1393163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1394163953Srrs 1395163953Srrs } else { 1396163953Srrs asoc->state = SCTP_STATE_OPEN; 1397163953Srrs } 1398163953Srrs sctp_stop_all_cookie_timers(stcb); 1399165220Srrs sctp_toss_old_cookies(stcb, asoc); 1400163953Srrs sctp_send_cookie_ack(stcb); 1401164205Srrs if (spec_flag) { 1402164205Srrs /* 1403164205Srrs * only if we have retrans set do we do this. What 1404164205Srrs * this call does is get only the COOKIE-ACK out and 1405164205Srrs * then when we return the normal call to 1406164205Srrs * sctp_chunk_output will get the retrans out behind 1407164205Srrs * this. 1408164205Srrs */ 1409164205Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK); 1410164205Srrs } 1411166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1412166023Srrs asoc->cookie_how[how_indx] = 11; 1413166023Srrs 1414163953Srrs return (stcb); 1415163953Srrs } 1416163953Srrs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1417163953Srrs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1418163953Srrs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1419163953Srrs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1420163953Srrs cookie->tie_tag_peer_vtag != 0) { 1421163953Srrs struct sctpasochead *head; 1422163953Srrs 1423163953Srrs /* 1424163953Srrs * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1425163953Srrs */ 1426165220Srrs /* temp code */ 1427166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1428166023Srrs asoc->cookie_how[how_indx] = 12; 1429165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1430165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1431165647Srrs 1432163953Srrs *sac_assoc_id = sctp_get_associd(stcb); 1433163953Srrs /* notify upper layer */ 1434163953Srrs *notification = SCTP_NOTIFY_ASSOC_RESTART; 1435165220Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 1436166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1437166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1438166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1439166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1440166675Srrs } 1441166675Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1442166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1443166675Srrs } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1444166675Srrs SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1445166675Srrs } 1446165647Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1447165647Srrs asoc->state = SCTP_STATE_OPEN | 1448165647Srrs SCTP_STATE_SHUTDOWN_PENDING; 1449165647Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1450165647Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1451165647Srrs 1452165647Srrs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1453165647Srrs /* move to OPEN state, if not in SHUTDOWN_SENT */ 1454165647Srrs asoc->state = SCTP_STATE_OPEN; 1455165647Srrs } 1456165647Srrs asoc->pre_open_streams = 1457165647Srrs ntohs(initack_cp->init.num_outbound_streams); 1458165647Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1459165647Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1460165647Srrs 1461165647Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1462165647Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1463165647Srrs 1464165647Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1465165647Srrs 1466165647Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1467165647Srrs if (asoc->mapping_array) 1468165647Srrs memset(asoc->mapping_array, 0, 1469165647Srrs asoc->mapping_array_size); 1470165220Srrs SCTP_TCB_UNLOCK(stcb); 1471165220Srrs SCTP_INP_INFO_WLOCK(); 1472165220Srrs SCTP_INP_WLOCK(stcb->sctp_ep); 1473165220Srrs SCTP_TCB_LOCK(stcb); 1474165220Srrs atomic_add_int(&stcb->asoc.refcnt, -1); 1475163953Srrs /* send up all the data */ 1476164205Srrs SCTP_TCB_SEND_LOCK(stcb); 1477165220Srrs 1478164205Srrs sctp_report_all_outbound(stcb, 1); 1479165220Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1480165220Srrs stcb->asoc.strmout[i].stream_no = i; 1481165220Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 1482165220Srrs stcb->asoc.strmout[i].last_msg_incomplete = 0; 1483165220Srrs } 1484163953Srrs /* process the INIT-ACK info (my info) */ 1485163953Srrs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1486163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1487163953Srrs 1488163953Srrs /* pull from vtag hash */ 1489163953Srrs LIST_REMOVE(stcb, sctp_asocs); 1490163953Srrs /* re-insert to new vtag position */ 1491163953Srrs head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1492163953Srrs sctppcbinfo.hashasocmark)]; 1493163953Srrs /* 1494163953Srrs * put it in the bucket in the vtag hash of assoc's for the 1495163953Srrs * system 1496163953Srrs */ 1497163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1498163953Srrs 1499163953Srrs /* Is this the first restart? */ 1500163953Srrs if (stcb->asoc.in_restart_hash == 0) { 1501163953Srrs /* Ok add it to assoc_id vtag hash */ 1502163953Srrs head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 1503163953Srrs sctppcbinfo.hashrestartmark)]; 1504163953Srrs LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash); 1505163953Srrs stcb->asoc.in_restart_hash = 1; 1506163953Srrs } 1507163953Srrs /* process the INIT info (peer's info) */ 1508164205Srrs SCTP_TCB_SEND_UNLOCK(stcb); 1509165220Srrs SCTP_INP_WUNLOCK(stcb->sctp_ep); 1510165220Srrs SCTP_INP_INFO_WUNLOCK(); 1511165220Srrs 1512163953Srrs retval = sctp_process_init(init_cp, stcb, net); 1513163953Srrs if (retval < 0) { 1514166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1515166023Srrs asoc->cookie_how[how_indx] = 13; 1516166023Srrs 1517163953Srrs return (NULL); 1518163953Srrs } 1519163953Srrs /* 1520163953Srrs * since we did not send a HB make sure we don't double 1521163953Srrs * things 1522163953Srrs */ 1523163953Srrs net->hb_responded = 1; 1524163953Srrs 1525163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1526163953Srrs init_offset + sizeof(struct sctp_init_chunk), 1527163953Srrs initack_offset, sh, init_src)) { 1528166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1529166023Srrs asoc->cookie_how[how_indx] = 14; 1530166023Srrs 1531163953Srrs return (NULL); 1532163953Srrs } 1533163953Srrs /* respond with a COOKIE-ACK */ 1534163953Srrs sctp_stop_all_cookie_timers(stcb); 1535165220Srrs sctp_toss_old_cookies(stcb, asoc); 1536163953Srrs sctp_send_cookie_ack(stcb); 1537166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1538166023Srrs asoc->cookie_how[how_indx] = 15; 1539163953Srrs 1540163953Srrs return (stcb); 1541163953Srrs } 1542166023Srrs if (how_indx < sizeof(asoc->cookie_how)) 1543166023Srrs asoc->cookie_how[how_indx] = 16; 1544163953Srrs /* all other cases... */ 1545163953Srrs return (NULL); 1546163953Srrs} 1547163953Srrs 1548166086Srrs 1549163953Srrs/* 1550163953Srrs * handle a state cookie for a new association m: input packet mbuf chain-- 1551163953Srrs * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1552163953Srrs * and the cookie signature does not exist offset: offset into mbuf to the 1553163953Srrs * cookie-echo chunk length: length of the cookie chunk to: where the init 1554163953Srrs * was from returns a new TCB 1555163953Srrs */ 1556163953Srrsstatic struct sctp_tcb * 1557163953Srrssctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1558163953Srrs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1559163953Srrs struct sctp_inpcb *inp, struct sctp_nets **netp, 1560163953Srrs struct sockaddr *init_src, int *notification, 1561169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1562169352Srrs uint32_t vrf_id, uint32_t table_id) 1563163953Srrs{ 1564163953Srrs struct sctp_tcb *stcb; 1565163953Srrs struct sctp_init_chunk *init_cp, init_buf; 1566163953Srrs struct sctp_init_ack_chunk *initack_cp, initack_buf; 1567163953Srrs struct sockaddr_storage sa_store; 1568163953Srrs struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1569163953Srrs struct sockaddr_in *sin; 1570163953Srrs struct sockaddr_in6 *sin6; 1571163953Srrs struct sctp_association *asoc; 1572163953Srrs int chk_length; 1573163953Srrs int init_offset, initack_offset, initack_limit; 1574163953Srrs int retval; 1575163953Srrs int error = 0; 1576163953Srrs uint32_t old_tag; 1577166675Srrs uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1578163953Srrs 1579163953Srrs /* 1580163953Srrs * find and validate the INIT chunk in the cookie (peer's info) the 1581163953Srrs * INIT should start after the cookie-echo header struct (chunk 1582163953Srrs * header, state cookie header struct) 1583163953Srrs */ 1584163953Srrs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 1585163953Srrs init_cp = (struct sctp_init_chunk *) 1586163953Srrs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1587163953Srrs (uint8_t *) & init_buf); 1588163953Srrs if (init_cp == NULL) { 1589163953Srrs /* could not pull a INIT chunk in cookie */ 1590163953Srrs#ifdef SCTP_DEBUG 1591163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1592163953Srrs printf("process_cookie_new: could not pull INIT chunk hdr\n"); 1593163953Srrs } 1594163953Srrs#endif /* SCTP_DEBUG */ 1595163953Srrs return (NULL); 1596163953Srrs } 1597163953Srrs chk_length = ntohs(init_cp->ch.chunk_length); 1598163953Srrs if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1599163953Srrs#ifdef SCTP_DEBUG 1600163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1601163953Srrs printf("HUH? process_cookie_new: could not find INIT chunk!\n"); 1602163953Srrs } 1603163953Srrs#endif /* SCTP_DEBUG */ 1604163953Srrs return (NULL); 1605163953Srrs } 1606163953Srrs initack_offset = init_offset + SCTP_SIZE32(chk_length); 1607163953Srrs /* 1608163953Srrs * find and validate the INIT-ACK chunk in the cookie (my info) the 1609163953Srrs * INIT-ACK follows the INIT chunk 1610163953Srrs */ 1611163953Srrs initack_cp = (struct sctp_init_ack_chunk *) 1612163953Srrs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1613163953Srrs (uint8_t *) & initack_buf); 1614163953Srrs if (initack_cp == NULL) { 1615163953Srrs /* could not pull INIT-ACK chunk in cookie */ 1616163953Srrs#ifdef SCTP_DEBUG 1617163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1618163953Srrs printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 1619163953Srrs } 1620163953Srrs#endif /* SCTP_DEBUG */ 1621163953Srrs return (NULL); 1622163953Srrs } 1623163953Srrs chk_length = ntohs(initack_cp->ch.chunk_length); 1624163953Srrs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1625163953Srrs return (NULL); 1626163953Srrs } 1627163953Srrs /* 1628163953Srrs * NOTE: We can't use the INIT_ACK's chk_length to determine the 1629163953Srrs * "initack_limit" value. This is because the chk_length field 1630163953Srrs * includes the length of the cookie, but the cookie is omitted when 1631163953Srrs * the INIT and INIT_ACK are tacked onto the cookie... 1632163953Srrs */ 1633163953Srrs initack_limit = offset + cookie_len; 1634163953Srrs 1635163953Srrs /* 1636163953Srrs * now that we know the INIT/INIT-ACK are in place, create a new TCB 1637163953Srrs * and popluate 1638163953Srrs */ 1639163953Srrs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, 1640168299Srrs ntohl(initack_cp->init.initiate_tag), vrf_id); 1641163953Srrs if (stcb == NULL) { 1642163953Srrs struct mbuf *op_err; 1643163953Srrs 1644163953Srrs /* memory problem? */ 1645163953Srrs#ifdef SCTP_DEBUG 1646163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1647163953Srrs printf("process_cookie_new: no room for another TCB!\n"); 1648163953Srrs } 1649169352Srrs#endif 1650163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1651163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1652169352Srrs sh, op_err, vrf_id, table_id); 1653163953Srrs return (NULL); 1654163953Srrs } 1655163953Srrs /* get the correct sctp_nets */ 1656163953Srrs *netp = sctp_findnet(stcb, init_src); 1657163953Srrs asoc = &stcb->asoc; 1658169352Srrs /* save the table id (vrf_id is done in aloc_assoc) */ 1659169352Srrs asoc->table_id = table_id; 1660163953Srrs /* get scope variables out of cookie */ 1661163953Srrs asoc->ipv4_local_scope = cookie->ipv4_scope; 1662163953Srrs asoc->site_scope = cookie->site_scope; 1663163953Srrs asoc->local_scope = cookie->local_scope; 1664163953Srrs asoc->loopback_scope = cookie->loopback_scope; 1665163953Srrs 1666163953Srrs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 1667163953Srrs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 1668163953Srrs struct mbuf *op_err; 1669163953Srrs 1670163953Srrs /* 1671163953Srrs * Houston we have a problem. The EP changed while the 1672163953Srrs * cookie was in flight. Only recourse is to abort the 1673163953Srrs * association. 1674163953Srrs */ 1675163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 1676163953Srrs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 1677169352Srrs sh, op_err, vrf_id, table_id); 1678163953Srrs return (NULL); 1679163953Srrs } 1680163953Srrs /* process the INIT-ACK info (my info) */ 1681163953Srrs old_tag = asoc->my_vtag; 1682163953Srrs asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1683163953Srrs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1684163953Srrs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1685163953Srrs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1686163953Srrs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1687163953Srrs asoc->last_cwr_tsn = asoc->init_seq_number - 1; 1688163953Srrs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1689163953Srrs asoc->str_reset_seq_in = asoc->init_seq_number; 1690163953Srrs 1691163953Srrs asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1692163953Srrs 1693163953Srrs /* process the INIT info (peer's info) */ 1694163953Srrs retval = sctp_process_init(init_cp, stcb, *netp); 1695163953Srrs if (retval < 0) { 1696165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1697163953Srrs return (NULL); 1698163953Srrs } 1699163953Srrs /* load all addresses */ 1700163953Srrs if (sctp_load_addresses_from_init(stcb, m, iphlen, 1701163953Srrs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 1702163953Srrs init_src)) { 1703165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 1704163953Srrs return (NULL); 1705163953Srrs } 1706163953Srrs /* 1707163953Srrs * verify any preceding AUTH chunk that was skipped 1708163953Srrs */ 1709163953Srrs /* pull the local authentication parameters from the cookie/init-ack */ 1710163953Srrs sctp_auth_get_cookie_params(stcb, m, 1711163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1712163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 1713163953Srrs if (auth_skipped) { 1714163953Srrs struct sctp_auth_chunk *auth; 1715163953Srrs 1716163953Srrs auth = (struct sctp_auth_chunk *) 1717166675Srrs sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 1718163953Srrs if (sctp_handle_auth(stcb, auth, m, auth_offset)) { 1719163953Srrs /* auth HMAC failed, dump the assoc and packet */ 1720163953Srrs#ifdef SCTP_DEBUG 1721163953Srrs if (sctp_debug_on & SCTP_DEBUG_AUTH1) 1722163953Srrs printf("COOKIE-ECHO: AUTH failed\n"); 1723163953Srrs#endif /* SCTP_DEBUG */ 1724165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 1725163953Srrs return (NULL); 1726163953Srrs } else { 1727163953Srrs /* remaining chunks checked... good to go */ 1728163953Srrs stcb->asoc.authenticated = 1; 1729163953Srrs } 1730163953Srrs } 1731163953Srrs /* update current state */ 1732163953Srrs#ifdef SCTP_DEBUG 1733163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 1734163953Srrs printf("moving to OPEN state\n"); 1735163953Srrs } 1736163953Srrs#endif 1737163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1738163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 1739163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1740163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 1741163953Srrs } else { 1742163953Srrs asoc->state = SCTP_STATE_OPEN; 1743163953Srrs } 1744165220Srrs sctp_stop_all_cookie_timers(stcb); 1745163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 1746163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1747163953Srrs 1748163953Srrs /* 1749163953Srrs * if we're doing ASCONFs, check to see if we have any new local 1750163953Srrs * addresses that need to get added to the peer (eg. addresses 1751163953Srrs * changed while cookie echo in flight). This needs to be done 1752163953Srrs * after we go to the OPEN state to do the correct asconf 1753163953Srrs * processing. else, make sure we have the correct addresses in our 1754163953Srrs * lists 1755163953Srrs */ 1756163953Srrs 1757163953Srrs /* warning, we re-use sin, sin6, sa_store here! */ 1758163953Srrs /* pull in local_address (our "from" address) */ 1759163953Srrs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) { 1760163953Srrs /* source addr is IPv4 */ 1761163953Srrs sin = (struct sockaddr_in *)initack_src; 1762163953Srrs memset(sin, 0, sizeof(*sin)); 1763163953Srrs sin->sin_family = AF_INET; 1764163953Srrs sin->sin_len = sizeof(struct sockaddr_in); 1765163953Srrs sin->sin_addr.s_addr = cookie->laddress[0]; 1766163953Srrs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) { 1767163953Srrs /* source addr is IPv6 */ 1768163953Srrs sin6 = (struct sockaddr_in6 *)initack_src; 1769163953Srrs memset(sin6, 0, sizeof(*sin6)); 1770163953Srrs sin6->sin6_family = AF_INET6; 1771163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1772163953Srrs sin6->sin6_scope_id = cookie->scope_id; 1773163953Srrs memcpy(&sin6->sin6_addr, cookie->laddress, 1774163953Srrs sizeof(sin6->sin6_addr)); 1775163953Srrs } else { 1776165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 1777163953Srrs return (NULL); 1778163953Srrs } 1779163953Srrs 1780163953Srrs sctp_check_address_list(stcb, m, 1781163953Srrs initack_offset + sizeof(struct sctp_init_ack_chunk), 1782163953Srrs initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 1783163953Srrs initack_src, cookie->local_scope, cookie->site_scope, 1784163953Srrs cookie->ipv4_scope, cookie->loopback_scope); 1785163953Srrs 1786163953Srrs 1787163953Srrs /* set up to notify upper layer */ 1788163953Srrs *notification = SCTP_NOTIFY_ASSOC_UP; 1789163953Srrs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1790163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1791163953Srrs (inp->sctp_socket->so_qlimit == 0)) { 1792163953Srrs /* 1793163953Srrs * This is an endpoint that called connect() how it got a 1794163953Srrs * cookie that is NEW is a bit of a mystery. It must be that 1795163953Srrs * the INIT was sent, but before it got there.. a complete 1796163953Srrs * INIT/INIT-ACK/COOKIE arrived. But of course then it 1797163953Srrs * should have went to the other code.. not here.. oh well.. 1798163953Srrs * a bit of protection is worth having.. 1799163953Srrs */ 1800163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1801163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 1802163953Srrs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1803163953Srrs (inp->sctp_socket->so_qlimit)) { 1804163953Srrs /* 1805163953Srrs * We don't want to do anything with this one. Since it is 1806163953Srrs * the listening guy. The timer will get started for 1807163953Srrs * accepted connections in the caller. 1808163953Srrs */ 1809163953Srrs ; 1810163953Srrs } 1811163953Srrs /* since we did not send a HB make sure we don't double things */ 1812163953Srrs (*netp)->hb_responded = 1; 1813163953Srrs 1814163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 1815163953Srrs sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1816163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1817163953Srrs } 1818163953Srrs /* respond with a COOKIE-ACK */ 1819165220Srrs /* calculate the RTT */ 1820165220Srrs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 1821165220Srrs &cookie->time_entered); 1822163953Srrs sctp_send_cookie_ack(stcb); 1823163953Srrs return (stcb); 1824163953Srrs} 1825163953Srrs 1826163953Srrs 1827163953Srrs/* 1828163953Srrs * handles a COOKIE-ECHO message stcb: modified to either a new or left as 1829163953Srrs * existing (non-NULL) TCB 1830163953Srrs */ 1831163953Srrsstatic struct mbuf * 1832163953Srrssctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 1833163953Srrs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 1834163953Srrs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 1835169352Srrs int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1836169352Srrs struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint32_t table_id) 1837163953Srrs{ 1838163953Srrs struct sctp_state_cookie *cookie; 1839163953Srrs struct sockaddr_in6 sin6; 1840163953Srrs struct sockaddr_in sin; 1841163953Srrs struct sctp_tcb *l_stcb = *stcb; 1842163953Srrs struct sctp_inpcb *l_inp; 1843163953Srrs struct sockaddr *to; 1844163953Srrs sctp_assoc_t sac_restart_id; 1845163953Srrs struct sctp_pcb *ep; 1846163953Srrs struct mbuf *m_sig; 1847163953Srrs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 1848163953Srrs uint8_t *sig; 1849163953Srrs uint8_t cookie_ok = 0; 1850163953Srrs unsigned int size_of_pkt, sig_offset, cookie_offset; 1851163953Srrs unsigned int cookie_len; 1852163953Srrs struct timeval now; 1853163953Srrs struct timeval time_expires; 1854163953Srrs struct sockaddr_storage dest_store; 1855163953Srrs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 1856163953Srrs struct ip *iph; 1857163953Srrs int notification = 0; 1858163953Srrs struct sctp_nets *netl; 1859163953Srrs int had_a_existing_tcb = 0; 1860163953Srrs 1861163953Srrs#ifdef SCTP_DEBUG 1862163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 1863163953Srrs printf("sctp_handle_cookie: handling COOKIE-ECHO\n"); 1864163953Srrs } 1865163953Srrs#endif 1866163953Srrs 1867163953Srrs if (inp_p == NULL) { 1868163953Srrs return (NULL); 1869163953Srrs } 1870163953Srrs /* First get the destination address setup too. */ 1871163953Srrs iph = mtod(m, struct ip *); 1872163953Srrs if (iph->ip_v == IPVERSION) { 1873163953Srrs /* its IPv4 */ 1874163953Srrs struct sockaddr_in *sin; 1875163953Srrs 1876163953Srrs sin = (struct sockaddr_in *)(localep_sa); 1877163953Srrs memset(sin, 0, sizeof(*sin)); 1878163953Srrs sin->sin_family = AF_INET; 1879163953Srrs sin->sin_len = sizeof(*sin); 1880163953Srrs sin->sin_port = sh->dest_port; 1881163953Srrs sin->sin_addr.s_addr = iph->ip_dst.s_addr; 1882165647Srrs size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 1883163953Srrs } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1884163953Srrs /* its IPv6 */ 1885163953Srrs struct ip6_hdr *ip6; 1886163953Srrs struct sockaddr_in6 *sin6; 1887163953Srrs 1888163953Srrs sin6 = (struct sockaddr_in6 *)(localep_sa); 1889163953Srrs memset(sin6, 0, sizeof(*sin6)); 1890163953Srrs sin6->sin6_family = AF_INET6; 1891163953Srrs sin6->sin6_len = sizeof(struct sockaddr_in6); 1892163953Srrs ip6 = mtod(m, struct ip6_hdr *); 1893163953Srrs sin6->sin6_port = sh->dest_port; 1894163953Srrs sin6->sin6_addr = ip6->ip6_dst; 1895166675Srrs size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 1896163953Srrs } else { 1897163953Srrs return (NULL); 1898163953Srrs } 1899163953Srrs 1900163953Srrs cookie = &cp->cookie; 1901163953Srrs cookie_offset = offset + sizeof(struct sctp_chunkhdr); 1902163953Srrs cookie_len = ntohs(cp->ch.chunk_length); 1903163953Srrs 1904163953Srrs if ((cookie->peerport != sh->src_port) && 1905163953Srrs (cookie->myport != sh->dest_port) && 1906163953Srrs (cookie->my_vtag != sh->v_tag)) { 1907163953Srrs /* 1908163953Srrs * invalid ports or bad tag. Note that we always leave the 1909163953Srrs * v_tag in the header in network order and when we stored 1910163953Srrs * it in the my_vtag slot we also left it in network order. 1911169352Srrs * This maintains the match even though it may be in the 1912163953Srrs * opposite byte order of the machine :-> 1913163953Srrs */ 1914163953Srrs return (NULL); 1915163953Srrs } 1916163953Srrs if (cookie_len > size_of_pkt || 1917163953Srrs cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 1918163953Srrs sizeof(struct sctp_init_chunk) + 1919163953Srrs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 1920163953Srrs /* cookie too long! or too small */ 1921163953Srrs return (NULL); 1922163953Srrs } 1923163953Srrs /* 1924163953Srrs * split off the signature into its own mbuf (since it should not be 1925163953Srrs * calculated in the sctp_hmac_m() call). 1926163953Srrs */ 1927163953Srrs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 1928163953Srrs if (sig_offset > size_of_pkt) { 1929163953Srrs /* packet not correct size! */ 1930163953Srrs /* XXX this may already be accounted for earlier... */ 1931163953Srrs return (NULL); 1932163953Srrs } 1933163953Srrs m_sig = m_split(m, sig_offset, M_DONTWAIT); 1934163953Srrs if (m_sig == NULL) { 1935163953Srrs /* out of memory or ?? */ 1936163953Srrs return (NULL); 1937163953Srrs } 1938163953Srrs /* 1939163953Srrs * compute the signature/digest for the cookie 1940163953Srrs */ 1941163953Srrs ep = &(*inp_p)->sctp_ep; 1942163953Srrs l_inp = *inp_p; 1943163953Srrs if (l_stcb) { 1944163953Srrs SCTP_TCB_UNLOCK(l_stcb); 1945163953Srrs } 1946163953Srrs SCTP_INP_RLOCK(l_inp); 1947163953Srrs if (l_stcb) { 1948163953Srrs SCTP_TCB_LOCK(l_stcb); 1949163953Srrs } 1950163953Srrs /* which cookie is it? */ 1951163953Srrs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 1952163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1953163953Srrs /* it's the old cookie */ 1954163953Srrs sctp_hmac_m(SCTP_HMAC, 1955163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1956163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1957163953Srrs } else { 1958163953Srrs /* it's the current cookie */ 1959163953Srrs sctp_hmac_m(SCTP_HMAC, 1960163953Srrs (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 1961163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1962163953Srrs } 1963163953Srrs /* get the signature */ 1964163953Srrs SCTP_INP_RUNLOCK(l_inp); 1965163953Srrs sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 1966163953Srrs if (sig == NULL) { 1967163953Srrs /* couldn't find signature */ 1968164085Srrs sctp_m_freem(m_sig); 1969163953Srrs return (NULL); 1970163953Srrs } 1971163953Srrs /* compare the received digest with the computed digest */ 1972163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 1973163953Srrs /* try the old cookie? */ 1974163953Srrs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 1975163953Srrs (ep->current_secret_number != ep->last_secret_number)) { 1976163953Srrs /* compute digest with old */ 1977163953Srrs sctp_hmac_m(SCTP_HMAC, 1978163953Srrs (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 1979163953Srrs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig); 1980163953Srrs /* compare */ 1981163953Srrs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 1982163953Srrs cookie_ok = 1; 1983163953Srrs } 1984163953Srrs } else { 1985163953Srrs cookie_ok = 1; 1986163953Srrs } 1987163953Srrs 1988163953Srrs /* 1989163953Srrs * Now before we continue we must reconstruct our mbuf so that 1990163953Srrs * normal processing of any other chunks will work. 1991163953Srrs */ 1992163953Srrs { 1993163953Srrs struct mbuf *m_at; 1994163953Srrs 1995163953Srrs m_at = m; 1996165647Srrs while (SCTP_BUF_NEXT(m_at) != NULL) { 1997165647Srrs m_at = SCTP_BUF_NEXT(m_at); 1998163953Srrs } 1999165647Srrs SCTP_BUF_NEXT(m_at) = m_sig; 2000163953Srrs } 2001163953Srrs 2002163953Srrs if (cookie_ok == 0) { 2003163953Srrs#ifdef SCTP_DEBUG 2004163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2005163953Srrs printf("handle_cookie_echo: cookie signature validation failed!\n"); 2006163953Srrs printf("offset = %u, cookie_offset = %u, sig_offset = %u\n", 2007163953Srrs (uint32_t) offset, cookie_offset, sig_offset); 2008163953Srrs } 2009163953Srrs#endif 2010163953Srrs return (NULL); 2011163953Srrs } 2012163953Srrs /* 2013163953Srrs * check the cookie timestamps to be sure it's not stale 2014163953Srrs */ 2015163953Srrs SCTP_GETTIME_TIMEVAL(&now); 2016163953Srrs /* Expire time is in Ticks, so we convert to seconds */ 2017163953Srrs time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life; 2018163953Srrs time_expires.tv_usec = cookie->time_entered.tv_usec; 2019163953Srrs if (timevalcmp(&now, &time_expires, >)) { 2020163953Srrs /* cookie is stale! */ 2021163953Srrs struct mbuf *op_err; 2022163953Srrs struct sctp_stale_cookie_msg *scm; 2023163953Srrs uint32_t tim; 2024163953Srrs 2025163953Srrs op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2026165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 2027163953Srrs if (op_err == NULL) { 2028163953Srrs /* FOOBAR */ 2029163953Srrs return (NULL); 2030163953Srrs } 2031163953Srrs /* pre-reserve some space */ 2032165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 2033165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 2034165647Srrs SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 2035163953Srrs 2036163953Srrs /* Set the len */ 2037165647Srrs SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2038163953Srrs scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2039163953Srrs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2040163953Srrs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2041163953Srrs (sizeof(uint32_t)))); 2042163953Srrs /* seconds to usec */ 2043163953Srrs tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2044163953Srrs /* add in usec */ 2045163953Srrs if (tim == 0) 2046163953Srrs tim = now.tv_usec - cookie->time_entered.tv_usec; 2047163953Srrs scm->time_usec = htonl(tim); 2048169352Srrs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2049169352Srrs vrf_id, table_id); 2050163953Srrs return (NULL); 2051163953Srrs } 2052163953Srrs /* 2053163953Srrs * Now we must see with the lookup address if we have an existing 2054163953Srrs * asoc. This will only happen if we were in the COOKIE-WAIT state 2055163953Srrs * and a INIT collided with us and somewhere the peer sent the 2056163953Srrs * cookie on another address besides the single address our assoc 2057163953Srrs * had for him. In this case we will have one of the tie-tags set at 2058163953Srrs * least AND the address field in the cookie can be used to look it 2059163953Srrs * up. 2060163953Srrs */ 2061163953Srrs to = NULL; 2062163953Srrs if (cookie->addr_type == SCTP_IPV6_ADDRESS) { 2063163953Srrs memset(&sin6, 0, sizeof(sin6)); 2064163953Srrs sin6.sin6_family = AF_INET6; 2065163953Srrs sin6.sin6_len = sizeof(sin6); 2066163953Srrs sin6.sin6_port = sh->src_port; 2067163953Srrs sin6.sin6_scope_id = cookie->scope_id; 2068163953Srrs memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2069163953Srrs sizeof(sin6.sin6_addr.s6_addr)); 2070163953Srrs to = (struct sockaddr *)&sin6; 2071163953Srrs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) { 2072163953Srrs memset(&sin, 0, sizeof(sin)); 2073163953Srrs sin.sin_family = AF_INET; 2074163953Srrs sin.sin_len = sizeof(sin); 2075163953Srrs sin.sin_port = sh->src_port; 2076163953Srrs sin.sin_addr.s_addr = cookie->address[0]; 2077163953Srrs to = (struct sockaddr *)&sin; 2078163953Srrs } 2079163953Srrs if ((*stcb == NULL) && to) { 2080163953Srrs /* Yep, lets check */ 2081163953Srrs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2082163953Srrs if (*stcb == NULL) { 2083163953Srrs /* 2084163953Srrs * We should have only got back the same inp. If we 2085163953Srrs * got back a different ep we have a problem. The 2086163953Srrs * original findep got back l_inp and now 2087163953Srrs */ 2088163953Srrs if (l_inp != *inp_p) { 2089163953Srrs printf("Bad problem find_ep got a diff inp then special_locate?\n"); 2090163953Srrs } 2091165220Srrs } else { 2092165220Srrs if (*locked_tcb == NULL) { 2093165220Srrs /* 2094165220Srrs * In this case we found the assoc only 2095165220Srrs * after we locked the create lock. This 2096165220Srrs * means we are in a colliding case and we 2097165220Srrs * must make sure that we unlock the tcb if 2098165220Srrs * its one of the cases where we throw away 2099165220Srrs * the incoming packets. 2100165220Srrs */ 2101165220Srrs *locked_tcb = *stcb; 2102165220Srrs 2103165220Srrs /* 2104165220Srrs * We must also increment the inp ref count 2105165220Srrs * since the ref_count flags was set when we 2106165220Srrs * did not find the TCB, now we found it 2107165220Srrs * which reduces the refcount.. we must 2108165220Srrs * raise it back out to balance it all :-) 2109165220Srrs */ 2110165220Srrs SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2111165220Srrs if ((*stcb)->sctp_ep != l_inp) { 2112165220Srrs printf("Huh? ep:%p diff then l_inp:%p?\n", 2113165220Srrs (*stcb)->sctp_ep, l_inp); 2114165220Srrs } 2115165220Srrs } 2116163953Srrs } 2117163953Srrs } 2118163953Srrs cookie_len -= SCTP_SIGNATURE_SIZE; 2119163953Srrs if (*stcb == NULL) { 2120163953Srrs /* this is the "normal" case... get a new TCB */ 2121163953Srrs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2122163953Srrs cookie_len, *inp_p, netp, to, ¬ification, 2123169352Srrs auth_skipped, auth_offset, auth_len, vrf_id, table_id); 2124163953Srrs } else { 2125163953Srrs /* this is abnormal... cookie-echo on existing TCB */ 2126163953Srrs had_a_existing_tcb = 1; 2127163953Srrs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2128163953Srrs cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification, 2129169352Srrs &sac_restart_id, vrf_id, table_id); 2130163953Srrs } 2131163953Srrs 2132163953Srrs if (*stcb == NULL) { 2133163953Srrs /* still no TCB... must be bad cookie-echo */ 2134163953Srrs return (NULL); 2135163953Srrs } 2136163953Srrs /* 2137163953Srrs * Ok, we built an association so confirm the address we sent the 2138163953Srrs * INIT-ACK to. 2139163953Srrs */ 2140163953Srrs netl = sctp_findnet(*stcb, to); 2141163953Srrs /* 2142163953Srrs * This code should in theory NOT run but 2143163953Srrs */ 2144163953Srrs if (netl == NULL) { 2145163953Srrs /* TSNH! Huh, why do I need to add this address here? */ 2146163953Srrs int ret; 2147163953Srrs 2148165220Srrs ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE, 2149165220Srrs SCTP_IN_COOKIE_PROC); 2150163953Srrs netl = sctp_findnet(*stcb, to); 2151163953Srrs } 2152163953Srrs if (netl) { 2153163953Srrs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2154163953Srrs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2155163953Srrs sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2156163953Srrs netl); 2157163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2158163953Srrs (*stcb), 0, (void *)netl); 2159163953Srrs } 2160163953Srrs } 2161163953Srrs if (*stcb) { 2162163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p, 2163163953Srrs *stcb, NULL); 2164163953Srrs } 2165163953Srrs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2166163953Srrs if (!had_a_existing_tcb || 2167163953Srrs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2168163953Srrs /* 2169163953Srrs * If we have a NEW cookie or the connect never 2170163953Srrs * reached the connected state during collision we 2171163953Srrs * must do the TCP accept thing. 2172163953Srrs */ 2173163953Srrs struct socket *so, *oso; 2174163953Srrs struct sctp_inpcb *inp; 2175163953Srrs 2176163953Srrs if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2177163953Srrs /* 2178163953Srrs * For a restart we will keep the same 2179163953Srrs * socket, no need to do anything. I THINK!! 2180163953Srrs */ 2181163953Srrs sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id); 2182163953Srrs return (m); 2183163953Srrs } 2184163953Srrs oso = (*inp_p)->sctp_socket; 2185163953Srrs /* 2186163953Srrs * We do this to keep the sockets side happy durin 2187163953Srrs * the sonewcon ONLY. 2188163953Srrs */ 2189163953Srrs NET_LOCK_GIANT(); 2190163953Srrs SCTP_TCB_UNLOCK((*stcb)); 2191166086Srrs so = sonewconn(oso, 0 2192163953Srrs ); 2193163953Srrs NET_UNLOCK_GIANT(); 2194163953Srrs SCTP_INP_WLOCK((*stcb)->sctp_ep); 2195163953Srrs SCTP_TCB_LOCK((*stcb)); 2196163953Srrs SCTP_INP_WUNLOCK((*stcb)->sctp_ep); 2197163953Srrs if (so == NULL) { 2198163953Srrs struct mbuf *op_err; 2199163953Srrs 2200163953Srrs /* Too many sockets */ 2201163953Srrs#ifdef SCTP_DEBUG 2202163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 2203163953Srrs printf("process_cookie_new: no room for another socket!\n"); 2204163953Srrs } 2205163953Srrs#endif /* SCTP_DEBUG */ 2206163953Srrs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2207163953Srrs sctp_abort_association(*inp_p, NULL, m, iphlen, 2208169352Srrs sh, op_err, vrf_id, 2209169352Srrs table_id); 2210165220Srrs sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2211163953Srrs return (NULL); 2212163953Srrs } 2213163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2214166086Srrs SCTP_INP_INCR_REF(inp); 2215166086Srrs /* 2216166086Srrs * We add the unbound flag here so that if we get an 2217166086Srrs * soabort() before we get the move_pcb done, we 2218166086Srrs * will properly cleanup. 2219166086Srrs */ 2220163953Srrs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2221163953Srrs SCTP_PCB_FLAGS_CONNECTED | 2222163953Srrs SCTP_PCB_FLAGS_IN_TCPPOOL | 2223166086Srrs SCTP_PCB_FLAGS_UNBOUND | 2224163953Srrs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2225163953Srrs SCTP_PCB_FLAGS_DONT_WAKE); 2226163953Srrs inp->sctp_features = (*inp_p)->sctp_features; 2227163953Srrs inp->sctp_socket = so; 2228163953Srrs inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2229163953Srrs inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2230163953Srrs inp->sctp_context = (*inp_p)->sctp_context; 2231163953Srrs inp->inp_starting_point_for_iterator = NULL; 2232163953Srrs /* 2233163953Srrs * copy in the authentication parameters from the 2234163953Srrs * original endpoint 2235163953Srrs */ 2236163953Srrs if (inp->sctp_ep.local_hmacs) 2237163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2238163953Srrs inp->sctp_ep.local_hmacs = 2239163953Srrs sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2240163953Srrs if (inp->sctp_ep.local_auth_chunks) 2241163953Srrs sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2242163953Srrs inp->sctp_ep.local_auth_chunks = 2243163953Srrs sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2244163953Srrs (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys, 2245163953Srrs &inp->sctp_ep.shared_keys); 2246163953Srrs 2247163953Srrs /* 2248163953Srrs * Now we must move it from one hash table to 2249163953Srrs * another and get the tcb in the right place. 2250163953Srrs */ 2251163953Srrs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2252169208Srrs sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); 2253163953Srrs 2254166086Srrs /* 2255166086Srrs * now we must check to see if we were aborted while 2256166086Srrs * the move was going on and the lock/unlock 2257166086Srrs * happened. 2258166086Srrs */ 2259166086Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2260166086Srrs /* 2261166086Srrs * yep it was, we leave the assoc attached 2262166086Srrs * to the socket since the sctp_inpcb_free() 2263166086Srrs * call will send an abort for us. 2264166086Srrs */ 2265166086Srrs SCTP_INP_DECR_REF(inp); 2266166086Srrs return (NULL); 2267166086Srrs } 2268166086Srrs SCTP_INP_DECR_REF(inp); 2269163953Srrs /* Switch over to the new guy */ 2270163953Srrs *inp_p = inp; 2271166086Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2272163953Srrs 2273166086Srrs /* 2274166086Srrs * Pull it from the incomplete queue and wake the 2275166086Srrs * guy 2276166086Srrs */ 2277166086Srrs soisconnected(so); 2278163953Srrs return (m); 2279163953Srrs } 2280163953Srrs } 2281163953Srrs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 2282163953Srrs sctp_ulp_notify(notification, *stcb, 0, NULL); 2283163953Srrs } 2284163953Srrs return (m); 2285163953Srrs} 2286163953Srrs 2287163953Srrsstatic void 2288163953Srrssctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp, 2289163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2290163953Srrs{ 2291163953Srrs /* cp must not be used, others call this without a c-ack :-) */ 2292163953Srrs struct sctp_association *asoc; 2293163953Srrs 2294163953Srrs#ifdef SCTP_DEBUG 2295163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2296163953Srrs printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2297163953Srrs } 2298163953Srrs#endif 2299163953Srrs if (stcb == NULL) 2300163953Srrs return; 2301163953Srrs 2302163953Srrs asoc = &stcb->asoc; 2303163953Srrs 2304163953Srrs sctp_stop_all_cookie_timers(stcb); 2305163953Srrs /* process according to association state */ 2306163953Srrs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2307163953Srrs /* state change only needed when I am in right state */ 2308163953Srrs#ifdef SCTP_DEBUG 2309163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2310163953Srrs printf("moving to OPEN state\n"); 2311163953Srrs } 2312163953Srrs#endif 2313163953Srrs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2314163953Srrs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING; 2315163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2316163953Srrs stcb->sctp_ep, stcb, asoc->primary_destination); 2317163953Srrs 2318163953Srrs } else { 2319163953Srrs asoc->state = SCTP_STATE_OPEN; 2320163953Srrs } 2321163953Srrs /* update RTO */ 2322163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2323163953Srrs SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2324163953Srrs if (asoc->overall_error_count == 0) { 2325163953Srrs net->RTO = sctp_calculate_rto(stcb, asoc, net, 2326163953Srrs &asoc->time_entered); 2327163953Srrs } 2328163953Srrs SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2329163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL); 2330163953Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2331163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2332163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2333163953Srrs soisconnected(stcb->sctp_ep->sctp_socket); 2334163953Srrs } 2335163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2336163953Srrs stcb, net); 2337163953Srrs /* 2338163953Srrs * since we did not send a HB make sure we don't double 2339163953Srrs * things 2340163953Srrs */ 2341163953Srrs net->hb_responded = 1; 2342163953Srrs 2343163953Srrs if (stcb->asoc.sctp_autoclose_ticks && 2344163953Srrs sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2345163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2346163953Srrs stcb->sctp_ep, stcb, NULL); 2347163953Srrs } 2348163953Srrs /* 2349163953Srrs * set ASCONF timer if ASCONFs are pending and allowed (eg. 2350163953Srrs * addresses changed when init/cookie echo in flight) 2351163953Srrs */ 2352163953Srrs if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2353163953Srrs (stcb->asoc.peer_supports_asconf) && 2354163953Srrs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2355163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2356163953Srrs stcb->sctp_ep, stcb, 2357163953Srrs stcb->asoc.primary_destination); 2358163953Srrs } 2359163953Srrs } 2360163953Srrs /* Toss the cookie if I can */ 2361163953Srrs sctp_toss_old_cookies(stcb, asoc); 2362163953Srrs if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2363163953Srrs /* Restart the timer if we have pending data */ 2364163953Srrs struct sctp_tmit_chunk *chk; 2365163953Srrs 2366163953Srrs chk = TAILQ_FIRST(&asoc->sent_queue); 2367163953Srrs if (chk) { 2368163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2369163953Srrs stcb, chk->whoTo); 2370163953Srrs } 2371163953Srrs } 2372163953Srrs} 2373163953Srrs 2374163953Srrsstatic void 2375163953Srrssctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2376163953Srrs struct sctp_tcb *stcb) 2377163953Srrs{ 2378163953Srrs struct sctp_nets *net; 2379163953Srrs struct sctp_tmit_chunk *lchk; 2380163953Srrs uint32_t tsn; 2381163953Srrs 2382163953Srrs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) { 2383163953Srrs return; 2384163953Srrs } 2385163953Srrs SCTP_STAT_INCR(sctps_recvecne); 2386163953Srrs tsn = ntohl(cp->tsn); 2387163953Srrs /* ECN Nonce stuff: need a resync and disable the nonce sum check */ 2388163953Srrs /* Also we make sure we disable the nonce_wait */ 2389163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.send_queue); 2390163953Srrs if (lchk == NULL) { 2391163953Srrs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq; 2392163953Srrs } else { 2393163953Srrs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq; 2394163953Srrs } 2395163953Srrs stcb->asoc.nonce_wait_for_ecne = 0; 2396163953Srrs stcb->asoc.nonce_sum_check = 0; 2397163953Srrs 2398163953Srrs /* Find where it was sent, if possible */ 2399163953Srrs net = NULL; 2400163953Srrs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue); 2401163953Srrs while (lchk) { 2402163953Srrs if (lchk->rec.data.TSN_seq == tsn) { 2403163953Srrs net = lchk->whoTo; 2404163953Srrs break; 2405163953Srrs } 2406163953Srrs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ)) 2407163953Srrs break; 2408163953Srrs lchk = TAILQ_NEXT(lchk, sctp_next); 2409163953Srrs } 2410163953Srrs if (net == NULL) 2411163953Srrs /* default is we use the primary */ 2412163953Srrs net = stcb->asoc.primary_destination; 2413163953Srrs 2414163953Srrs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) { 2415163953Srrs#ifdef SCTP_CWND_MONITOR 2416163953Srrs int old_cwnd; 2417163953Srrs 2418163953Srrs old_cwnd = net->cwnd; 2419163953Srrs#endif 2420163953Srrs SCTP_STAT_INCR(sctps_ecnereducedcwnd); 2421163953Srrs net->ssthresh = net->cwnd / 2; 2422163953Srrs if (net->ssthresh < net->mtu) { 2423163953Srrs net->ssthresh = net->mtu; 2424163953Srrs /* here back off the timer as well, to slow us down */ 2425163953Srrs net->RTO <<= 2; 2426163953Srrs } 2427163953Srrs net->cwnd = net->ssthresh; 2428163953Srrs#ifdef SCTP_CWND_MONITOR 2429163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT); 2430163953Srrs#endif 2431163953Srrs /* 2432163953Srrs * we reduce once every RTT. So we will only lower cwnd at 2433163953Srrs * the next sending seq i.e. the resync_tsn. 2434163953Srrs */ 2435163953Srrs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn; 2436163953Srrs } 2437163953Srrs /* 2438163953Srrs * We always send a CWR this way if our previous one was lost our 2439163953Srrs * peer will get an update, or if it is not time again to reduce we 2440163953Srrs * still get the cwr to the peer. 2441163953Srrs */ 2442163953Srrs sctp_send_cwr(stcb, net, tsn); 2443163953Srrs} 2444163953Srrs 2445163953Srrsstatic void 2446163953Srrssctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb) 2447163953Srrs{ 2448163953Srrs /* 2449163953Srrs * Here we get a CWR from the peer. We must look in the outqueue and 2450163953Srrs * make sure that we have a covered ECNE in teh control chunk part. 2451163953Srrs * If so remove it. 2452163953Srrs */ 2453163953Srrs struct sctp_tmit_chunk *chk; 2454163953Srrs struct sctp_ecne_chunk *ecne; 2455163953Srrs 2456163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 2457163953Srrs if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 2458163953Srrs continue; 2459163953Srrs } 2460163953Srrs /* 2461163953Srrs * Look for and remove if it is the right TSN. Since there 2462163953Srrs * is only ONE ECNE on the control queue at any one time we 2463163953Srrs * don't need to worry about more than one! 2464163953Srrs */ 2465163953Srrs ecne = mtod(chk->data, struct sctp_ecne_chunk *); 2466163953Srrs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn), 2467163953Srrs MAX_TSN) || (cp->tsn == ecne->tsn)) { 2468163953Srrs /* this covers this ECNE, we can remove it */ 2469163953Srrs stcb->asoc.ecn_echo_cnt_onq--; 2470163953Srrs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 2471163953Srrs sctp_next); 2472163953Srrs if (chk->data) { 2473163953Srrs sctp_m_freem(chk->data); 2474163953Srrs chk->data = NULL; 2475163953Srrs } 2476163953Srrs stcb->asoc.ctrl_queue_cnt--; 2477163953Srrs sctp_free_remote_addr(chk->whoTo); 2478163953Srrs sctp_free_a_chunk(stcb, chk); 2479163953Srrs break; 2480163953Srrs } 2481163953Srrs } 2482163953Srrs} 2483163953Srrs 2484163953Srrsstatic void 2485163953Srrssctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp, 2486163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 2487163953Srrs{ 2488163953Srrs struct sctp_association *asoc; 2489163953Srrs 2490163953Srrs#ifdef SCTP_DEBUG 2491163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 2492163953Srrs printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 2493163953Srrs } 2494163953Srrs#endif 2495163953Srrs if (stcb == NULL) 2496163953Srrs return; 2497163953Srrs 2498163953Srrs asoc = &stcb->asoc; 2499163953Srrs /* process according to association state */ 2500163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 2501163953Srrs /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 2502163953Srrs SCTP_TCB_UNLOCK(stcb); 2503163953Srrs return; 2504163953Srrs } 2505163953Srrs /* notify upper layer protocol */ 2506163953Srrs if (stcb->sctp_socket) { 2507163953Srrs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL); 2508163953Srrs /* are the queues empty? they should be */ 2509163953Srrs if (!TAILQ_EMPTY(&asoc->send_queue) || 2510163953Srrs !TAILQ_EMPTY(&asoc->sent_queue) || 2511163953Srrs !TAILQ_EMPTY(&asoc->out_wheel)) { 2512164205Srrs sctp_report_all_outbound(stcb, 0); 2513163953Srrs } 2514163953Srrs } 2515163953Srrs /* stop the timer */ 2516165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_21); 2517163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 2518163953Srrs /* free the TCB */ 2519165220Srrs sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2520163953Srrs return; 2521163953Srrs} 2522163953Srrs 2523163953Srrsstatic int 2524163953Srrsprocess_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 2525163953Srrs struct sctp_nets *net, uint8_t flg) 2526163953Srrs{ 2527163953Srrs switch (desc->chunk_type) { 2528163953Srrs case SCTP_DATA: 2529163953Srrs /* find the tsn to resend (possibly */ 2530163953Srrs { 2531163953Srrs uint32_t tsn; 2532163953Srrs struct sctp_tmit_chunk *tp1; 2533163953Srrs 2534163953Srrs tsn = ntohl(desc->tsn_ifany); 2535163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2536163953Srrs while (tp1) { 2537163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2538163953Srrs /* found it */ 2539163953Srrs break; 2540163953Srrs } 2541163953Srrs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn, 2542163953Srrs MAX_TSN)) { 2543163953Srrs /* not found */ 2544163953Srrs tp1 = NULL; 2545163953Srrs break; 2546163953Srrs } 2547163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2548163953Srrs } 2549163953Srrs if (tp1 == NULL) { 2550163953Srrs /* 2551163953Srrs * Do it the other way , aka without paying 2552163953Srrs * attention to queue seq order. 2553163953Srrs */ 2554163953Srrs SCTP_STAT_INCR(sctps_pdrpdnfnd); 2555163953Srrs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2556163953Srrs while (tp1) { 2557163953Srrs if (tp1->rec.data.TSN_seq == tsn) { 2558163953Srrs /* found it */ 2559163953Srrs break; 2560163953Srrs } 2561163953Srrs tp1 = TAILQ_NEXT(tp1, sctp_next); 2562163953Srrs } 2563163953Srrs } 2564163953Srrs if (tp1 == NULL) { 2565163953Srrs SCTP_STAT_INCR(sctps_pdrptsnnf); 2566163953Srrs } 2567163953Srrs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 2568163953Srrs uint8_t *ddp; 2569163953Srrs 2570163953Srrs if ((stcb->asoc.peers_rwnd == 0) && 2571163953Srrs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 2572163953Srrs SCTP_STAT_INCR(sctps_pdrpdiwnp); 2573163953Srrs return (0); 2574163953Srrs } 2575163953Srrs if (stcb->asoc.peers_rwnd == 0 && 2576163953Srrs (flg & SCTP_FROM_MIDDLE_BOX)) { 2577163953Srrs SCTP_STAT_INCR(sctps_pdrpdizrw); 2578163953Srrs return (0); 2579163953Srrs } 2580163953Srrs ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 2581163953Srrs sizeof(struct sctp_data_chunk)); 2582163953Srrs { 2583163953Srrs unsigned int iii; 2584163953Srrs 2585163953Srrs for (iii = 0; iii < sizeof(desc->data_bytes); 2586163953Srrs iii++) { 2587163953Srrs if (ddp[iii] != desc->data_bytes[iii]) { 2588163953Srrs SCTP_STAT_INCR(sctps_pdrpbadd); 2589163953Srrs return (-1); 2590163953Srrs } 2591163953Srrs } 2592163953Srrs } 2593163953Srrs /* 2594163953Srrs * We zero out the nonce so resync not 2595163953Srrs * needed 2596163953Srrs */ 2597163953Srrs tp1->rec.data.ect_nonce = 0; 2598163953Srrs 2599163953Srrs if (tp1->do_rtt) { 2600163953Srrs /* 2601163953Srrs * this guy had a RTO calculation 2602163953Srrs * pending on it, cancel it 2603163953Srrs */ 2604163953Srrs tp1->do_rtt = 0; 2605163953Srrs } 2606163953Srrs SCTP_STAT_INCR(sctps_pdrpmark); 2607163953Srrs if (tp1->sent != SCTP_DATAGRAM_RESEND) 2608163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2609163953Srrs tp1->sent = SCTP_DATAGRAM_RESEND; 2610163953Srrs /* 2611163953Srrs * mark it as if we were doing a FR, since 2612163953Srrs * we will be getting gap ack reports behind 2613163953Srrs * the info from the router. 2614163953Srrs */ 2615163953Srrs tp1->rec.data.doing_fast_retransmit = 1; 2616163953Srrs /* 2617163953Srrs * mark the tsn with what sequences can 2618163953Srrs * cause a new FR. 2619163953Srrs */ 2620163953Srrs if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 2621163953Srrs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 2622163953Srrs } else { 2623163953Srrs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 2624163953Srrs } 2625163953Srrs 2626163953Srrs /* restart the timer */ 2627163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2628165220Srrs stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2629163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 2630163953Srrs stcb, tp1->whoTo); 2631163953Srrs 2632163953Srrs /* fix counts and things */ 2633165220Srrs#ifdef SCTP_FLIGHT_LOGGING 2634168709Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 2635165220Srrs tp1->whoTo->flight_size, 2636165220Srrs tp1->book_size, 2637165220Srrs (uintptr_t) stcb, 2638165220Srrs tp1->rec.data.TSN_seq); 2639165220Srrs#endif 2640168709Srrs sctp_flight_size_decrease(tp1); 2641168709Srrs sctp_total_flight_decrease(stcb, tp1); 2642163953Srrs } { 2643163953Srrs /* audit code */ 2644163953Srrs unsigned int audit; 2645163953Srrs 2646163953Srrs audit = 0; 2647163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 2648163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2649163953Srrs audit++; 2650163953Srrs } 2651163953Srrs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 2652163953Srrs sctp_next) { 2653163953Srrs if (tp1->sent == SCTP_DATAGRAM_RESEND) 2654163953Srrs audit++; 2655163953Srrs } 2656163953Srrs if (audit != stcb->asoc.sent_queue_retran_cnt) { 2657163953Srrs printf("**Local Audit finds cnt:%d asoc cnt:%d\n", 2658163953Srrs audit, stcb->asoc.sent_queue_retran_cnt); 2659163953Srrs#ifndef SCTP_AUDITING_ENABLED 2660163953Srrs stcb->asoc.sent_queue_retran_cnt = audit; 2661163953Srrs#endif 2662163953Srrs } 2663163953Srrs } 2664163953Srrs } 2665163953Srrs break; 2666163953Srrs case SCTP_ASCONF: 2667163953Srrs { 2668163953Srrs struct sctp_tmit_chunk *asconf; 2669163953Srrs 2670163953Srrs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 2671163953Srrs sctp_next) { 2672163953Srrs if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 2673163953Srrs break; 2674163953Srrs } 2675163953Srrs } 2676163953Srrs if (asconf) { 2677163953Srrs if (asconf->sent != SCTP_DATAGRAM_RESEND) 2678163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2679163953Srrs asconf->sent = SCTP_DATAGRAM_RESEND; 2680163953Srrs asconf->snd_count--; 2681163953Srrs } 2682163953Srrs } 2683163953Srrs break; 2684163953Srrs case SCTP_INITIATION: 2685163953Srrs /* resend the INIT */ 2686163953Srrs stcb->asoc.dropped_special_cnt++; 2687163953Srrs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 2688163953Srrs /* 2689163953Srrs * If we can get it in, in a few attempts we do 2690163953Srrs * this, otherwise we let the timer fire. 2691163953Srrs */ 2692163953Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 2693165220Srrs stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 2694163953Srrs sctp_send_initiate(stcb->sctp_ep, stcb); 2695163953Srrs } 2696163953Srrs break; 2697163953Srrs case SCTP_SELECTIVE_ACK: 2698163953Srrs /* resend the sack */ 2699163953Srrs sctp_send_sack(stcb); 2700163953Srrs break; 2701163953Srrs case SCTP_HEARTBEAT_REQUEST: 2702163953Srrs /* resend a demand HB */ 2703163953Srrs sctp_send_hb(stcb, 1, net); 2704163953Srrs break; 2705163953Srrs case SCTP_SHUTDOWN: 2706163953Srrs sctp_send_shutdown(stcb, net); 2707163953Srrs break; 2708163953Srrs case SCTP_SHUTDOWN_ACK: 2709163953Srrs sctp_send_shutdown_ack(stcb, net); 2710163953Srrs break; 2711163953Srrs case SCTP_COOKIE_ECHO: 2712163953Srrs { 2713163953Srrs struct sctp_tmit_chunk *cookie; 2714163953Srrs 2715163953Srrs cookie = NULL; 2716163953Srrs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 2717163953Srrs sctp_next) { 2718163953Srrs if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 2719163953Srrs break; 2720163953Srrs } 2721163953Srrs } 2722163953Srrs if (cookie) { 2723163953Srrs if (cookie->sent != SCTP_DATAGRAM_RESEND) 2724163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 2725163953Srrs cookie->sent = SCTP_DATAGRAM_RESEND; 2726163953Srrs sctp_stop_all_cookie_timers(stcb); 2727163953Srrs } 2728163953Srrs } 2729163953Srrs break; 2730163953Srrs case SCTP_COOKIE_ACK: 2731163953Srrs sctp_send_cookie_ack(stcb); 2732163953Srrs break; 2733163953Srrs case SCTP_ASCONF_ACK: 2734163953Srrs /* resend last asconf ack */ 2735163953Srrs sctp_send_asconf_ack(stcb, 1); 2736163953Srrs break; 2737163953Srrs case SCTP_FORWARD_CUM_TSN: 2738163953Srrs send_forward_tsn(stcb, &stcb->asoc); 2739163953Srrs break; 2740163953Srrs /* can't do anything with these */ 2741163953Srrs case SCTP_PACKET_DROPPED: 2742163953Srrs case SCTP_INITIATION_ACK: /* this should not happen */ 2743163953Srrs case SCTP_HEARTBEAT_ACK: 2744163953Srrs case SCTP_ABORT_ASSOCIATION: 2745163953Srrs case SCTP_OPERATION_ERROR: 2746163953Srrs case SCTP_SHUTDOWN_COMPLETE: 2747163953Srrs case SCTP_ECN_ECHO: 2748163953Srrs case SCTP_ECN_CWR: 2749163953Srrs default: 2750163953Srrs break; 2751163953Srrs } 2752163953Srrs return (0); 2753163953Srrs} 2754163953Srrs 2755163953Srrsvoid 2756163953Srrssctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2757163953Srrs{ 2758163953Srrs int i; 2759163953Srrs uint16_t temp; 2760163953Srrs 2761163953Srrs /* 2762163953Srrs * We set things to 0xffff since this is the last delivered sequence 2763163953Srrs * and we will be sending in 0 after the reset. 2764163953Srrs */ 2765163953Srrs 2766163953Srrs if (number_entries) { 2767163953Srrs for (i = 0; i < number_entries; i++) { 2768163953Srrs temp = ntohs(list[i]); 2769163953Srrs if (temp >= stcb->asoc.streamincnt) { 2770163953Srrs continue; 2771163953Srrs } 2772163953Srrs stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 2773163953Srrs } 2774163953Srrs } else { 2775163953Srrs list = NULL; 2776163953Srrs for (i = 0; i < stcb->asoc.streamincnt; i++) { 2777163953Srrs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 2778163953Srrs } 2779163953Srrs } 2780163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list); 2781163953Srrs} 2782163953Srrs 2783163953Srrsstatic void 2784163953Srrssctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 2785163953Srrs{ 2786163953Srrs int i; 2787163953Srrs 2788163953Srrs if (number_entries == 0) { 2789163953Srrs for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 2790163953Srrs stcb->asoc.strmout[i].next_sequence_sent = 0; 2791163953Srrs } 2792163953Srrs } else if (number_entries) { 2793163953Srrs for (i = 0; i < number_entries; i++) { 2794163953Srrs uint16_t temp; 2795163953Srrs 2796163953Srrs temp = ntohs(list[i]); 2797163953Srrs if (temp >= stcb->asoc.streamoutcnt) { 2798163953Srrs /* no such stream */ 2799163953Srrs continue; 2800163953Srrs } 2801163953Srrs stcb->asoc.strmout[temp].next_sequence_sent = 0; 2802163953Srrs } 2803163953Srrs } 2804163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list); 2805163953Srrs} 2806163953Srrs 2807163953Srrs 2808163953Srrsstruct sctp_stream_reset_out_request * 2809163953Srrssctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 2810163953Srrs{ 2811163953Srrs struct sctp_association *asoc; 2812163953Srrs struct sctp_stream_reset_out_req *req; 2813163953Srrs struct sctp_stream_reset_out_request *r; 2814163953Srrs struct sctp_tmit_chunk *chk; 2815163953Srrs int len, clen; 2816163953Srrs 2817163953Srrs asoc = &stcb->asoc; 2818163953Srrs if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 2819169208Srrs asoc->stream_reset_outstanding = 0; 2820163953Srrs return (NULL); 2821163953Srrs } 2822163953Srrs if (stcb->asoc.str_reset == NULL) { 2823169208Srrs asoc->stream_reset_outstanding = 0; 2824163953Srrs return (NULL); 2825163953Srrs } 2826163953Srrs chk = stcb->asoc.str_reset; 2827163953Srrs if (chk->data == NULL) { 2828163953Srrs return (NULL); 2829163953Srrs } 2830163953Srrs if (bchk) { 2831163953Srrs /* he wants a copy of the chk pointer */ 2832163953Srrs *bchk = chk; 2833163953Srrs } 2834163953Srrs clen = chk->send_size; 2835163953Srrs req = mtod(chk->data, struct sctp_stream_reset_out_req *); 2836163953Srrs r = &req->sr_req; 2837163953Srrs if (ntohl(r->request_seq) == seq) { 2838163953Srrs /* found it */ 2839163953Srrs return (r); 2840163953Srrs } 2841163953Srrs len = SCTP_SIZE32(ntohs(r->ph.param_length)); 2842163953Srrs if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 2843163953Srrs /* move to the next one, there can only be a max of two */ 2844163953Srrs r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 2845163953Srrs if (ntohl(r->request_seq) == seq) { 2846163953Srrs return (r); 2847163953Srrs } 2848163953Srrs } 2849163953Srrs /* that seq is not here */ 2850163953Srrs return (NULL); 2851163953Srrs} 2852163953Srrs 2853163953Srrsstatic void 2854163953Srrssctp_clean_up_stream_reset(struct sctp_tcb *stcb) 2855163953Srrs{ 2856163953Srrs struct sctp_association *asoc; 2857163953Srrs struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 2858163953Srrs 2859163953Srrs if (stcb->asoc.str_reset == NULL) { 2860163953Srrs return; 2861163953Srrs } 2862166675Srrs asoc = &stcb->asoc; 2863166675Srrs 2864165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 2865163953Srrs TAILQ_REMOVE(&asoc->control_send_queue, 2866163953Srrs chk, 2867163953Srrs sctp_next); 2868163953Srrs if (chk->data) { 2869163953Srrs sctp_m_freem(chk->data); 2870163953Srrs chk->data = NULL; 2871163953Srrs } 2872163953Srrs asoc->ctrl_queue_cnt--; 2873163953Srrs sctp_free_remote_addr(chk->whoTo); 2874163953Srrs 2875163953Srrs sctp_free_a_chunk(stcb, chk); 2876163953Srrs stcb->asoc.str_reset = NULL; 2877163953Srrs} 2878163953Srrs 2879163953Srrs 2880163953Srrsstatic int 2881163953Srrssctp_handle_stream_reset_response(struct sctp_tcb *stcb, 2882163953Srrs uint32_t seq, uint32_t action, 2883164139Srrs struct sctp_stream_reset_response *respin) 2884163953Srrs{ 2885163953Srrs uint16_t type; 2886163953Srrs int lparm_len; 2887163953Srrs struct sctp_association *asoc = &stcb->asoc; 2888163953Srrs struct sctp_tmit_chunk *chk; 2889163953Srrs struct sctp_stream_reset_out_request *srparam; 2890163953Srrs int number_entries; 2891163953Srrs 2892163953Srrs if (asoc->stream_reset_outstanding == 0) { 2893163953Srrs /* duplicate */ 2894163953Srrs return (0); 2895163953Srrs } 2896163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 2897163953Srrs srparam = sctp_find_stream_reset(stcb, seq, &chk); 2898163953Srrs if (srparam) { 2899163953Srrs stcb->asoc.str_reset_seq_out++; 2900163953Srrs type = ntohs(srparam->ph.param_type); 2901163953Srrs lparm_len = ntohs(srparam->ph.param_length); 2902163953Srrs if (type == SCTP_STR_RESET_OUT_REQUEST) { 2903164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 2904163953Srrs asoc->stream_reset_out_is_outstanding = 0; 2905163953Srrs if (asoc->stream_reset_outstanding) 2906163953Srrs asoc->stream_reset_outstanding--; 2907163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2908163953Srrs /* do it */ 2909163953Srrs sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 2910163953Srrs } else { 2911163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams); 2912163953Srrs } 2913163953Srrs } else if (type == SCTP_STR_RESET_IN_REQUEST) { 2914163953Srrs /* Answered my request */ 2915164139Srrs number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 2916163953Srrs if (asoc->stream_reset_outstanding) 2917163953Srrs asoc->stream_reset_outstanding--; 2918163953Srrs if (action != SCTP_STREAM_RESET_PERFORMED) { 2919163953Srrs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams); 2920163953Srrs } 2921163953Srrs } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 2922163953Srrs /** 2923163953Srrs * a) Adopt the new in tsn. 2924163953Srrs * b) reset the map 2925163953Srrs * c) Adopt the new out-tsn 2926163953Srrs */ 2927163953Srrs struct sctp_stream_reset_response_tsn *resp; 2928163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 2929163953Srrs int abort_flag = 0; 2930163953Srrs 2931163953Srrs if (respin == NULL) { 2932163953Srrs /* huh ? */ 2933163953Srrs return (0); 2934163953Srrs } 2935163953Srrs if (action == SCTP_STREAM_RESET_PERFORMED) { 2936163953Srrs resp = (struct sctp_stream_reset_response_tsn *)respin; 2937163953Srrs asoc->stream_reset_outstanding--; 2938163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 2939163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 2940163953Srrs fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 2941163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 2942163953Srrs if (abort_flag) { 2943163953Srrs return (1); 2944163953Srrs } 2945163953Srrs stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 2946163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 2947163953Srrs stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 2948163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 2949163953Srrs stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 2950163953Srrs stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 2951163953Srrs 2952163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 2953163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 2954163953Srrs 2955163953Srrs } 2956163953Srrs } 2957163953Srrs /* get rid of the request and get the request flags */ 2958163953Srrs if (asoc->stream_reset_outstanding == 0) { 2959163953Srrs sctp_clean_up_stream_reset(stcb); 2960163953Srrs } 2961163953Srrs } 2962163953Srrs } 2963163953Srrs return (0); 2964163953Srrs} 2965163953Srrs 2966163953Srrsstatic void 2967163953Srrssctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 2968163953Srrs struct sctp_tmit_chunk *chk, 2969163953Srrs struct sctp_stream_reset_in_request *req) 2970163953Srrs{ 2971163953Srrs uint32_t seq; 2972163953Srrs int len, i; 2973163953Srrs int number_entries; 2974163953Srrs uint16_t temp; 2975163953Srrs 2976163953Srrs /* 2977163953Srrs * peer wants me to send a str-reset to him for my outgoing seq's if 2978163953Srrs * seq_in is right. 2979163953Srrs */ 2980163953Srrs struct sctp_association *asoc = &stcb->asoc; 2981163953Srrs 2982163953Srrs seq = ntohl(req->request_seq); 2983163953Srrs if (asoc->str_reset_seq_in == seq) { 2984163953Srrs if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 2985163953Srrs len = ntohs(req->ph.param_length); 2986163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 2987163953Srrs for (i = 0; i < number_entries; i++) { 2988163953Srrs temp = ntohs(req->list_of_streams[i]); 2989163953Srrs req->list_of_streams[i] = temp; 2990163953Srrs } 2991163953Srrs /* move the reset action back one */ 2992163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 2993163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 2994163953Srrs sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 2995163953Srrs asoc->str_reset_seq_out, 2996163953Srrs seq, (asoc->sending_seq - 1)); 2997163953Srrs asoc->stream_reset_out_is_outstanding = 1; 2998163953Srrs asoc->str_reset = chk; 2999163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3000163953Srrs stcb->asoc.stream_reset_outstanding++; 3001163953Srrs } else { 3002163953Srrs /* Can't do it, since we have sent one out */ 3003163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3004163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER; 3005163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3006163953Srrs } 3007163953Srrs asoc->str_reset_seq_in++; 3008163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3009163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3010163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3011163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3012163953Srrs } else { 3013163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3014163953Srrs } 3015163953Srrs} 3016163953Srrs 3017163953Srrsstatic int 3018163953Srrssctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3019163953Srrs struct sctp_tmit_chunk *chk, 3020163953Srrs struct sctp_stream_reset_tsn_request *req) 3021163953Srrs{ 3022163953Srrs /* reset all in and out and update the tsn */ 3023163953Srrs /* 3024163953Srrs * A) reset my str-seq's on in and out. B) Select a receive next, 3025163953Srrs * and set cum-ack to it. Also process this selected number as a 3026163953Srrs * fwd-tsn as well. C) set in the response my next sending seq. 3027163953Srrs */ 3028163953Srrs struct sctp_forward_tsn_chunk fwdtsn; 3029163953Srrs struct sctp_association *asoc = &stcb->asoc; 3030163953Srrs int abort_flag = 0; 3031163953Srrs uint32_t seq; 3032163953Srrs 3033163953Srrs seq = ntohl(req->request_seq); 3034163953Srrs if (asoc->str_reset_seq_in == seq) { 3035163953Srrs fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3036163953Srrs fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3037163953Srrs fwdtsn.ch.chunk_flags = 0; 3038163953Srrs fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3039163953Srrs sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag); 3040163953Srrs if (abort_flag) { 3041163953Srrs return (1); 3042163953Srrs } 3043163953Srrs stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3044163953Srrs stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3045163953Srrs stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1; 3046163953Srrs memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3047163953Srrs atomic_add_int(&stcb->asoc.sending_seq, 1); 3048163953Srrs /* save off historical data for retrans */ 3049163953Srrs stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0]; 3050163953Srrs stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq; 3051163953Srrs stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0]; 3052163953Srrs stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn; 3053163953Srrs 3054163953Srrs sctp_add_stream_reset_result_tsn(chk, 3055163953Srrs ntohl(req->request_seq), 3056163953Srrs SCTP_STREAM_RESET_PERFORMED, 3057163953Srrs stcb->asoc.sending_seq, 3058163953Srrs stcb->asoc.mapping_array_base_tsn); 3059163953Srrs sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3060163953Srrs sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3061163953Srrs stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3062163953Srrs stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3063163953Srrs 3064163953Srrs asoc->str_reset_seq_in++; 3065163953Srrs } else if (asoc->str_reset_seq_in - 1 == seq) { 3066163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3067163953Srrs stcb->asoc.last_sending_seq[0], 3068163953Srrs stcb->asoc.last_base_tsnsent[0] 3069163953Srrs ); 3070163953Srrs } else if (asoc->str_reset_seq_in - 2 == seq) { 3071163953Srrs sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3072163953Srrs stcb->asoc.last_sending_seq[1], 3073163953Srrs stcb->asoc.last_base_tsnsent[1] 3074163953Srrs ); 3075163953Srrs } else { 3076163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3077163953Srrs } 3078163953Srrs return (0); 3079163953Srrs} 3080163953Srrs 3081163953Srrsstatic void 3082163953Srrssctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3083163953Srrs struct sctp_tmit_chunk *chk, 3084163953Srrs struct sctp_stream_reset_out_request *req) 3085163953Srrs{ 3086163953Srrs uint32_t seq, tsn; 3087163953Srrs int number_entries, len; 3088163953Srrs struct sctp_association *asoc = &stcb->asoc; 3089163953Srrs 3090163953Srrs seq = ntohl(req->request_seq); 3091163953Srrs 3092163953Srrs /* now if its not a duplicate we process it */ 3093163953Srrs if (asoc->str_reset_seq_in == seq) { 3094163953Srrs len = ntohs(req->ph.param_length); 3095163953Srrs number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3096163953Srrs /* 3097163953Srrs * the sender is resetting, handle the list issue.. we must 3098163953Srrs * a) verify if we can do the reset, if so no problem b) If 3099163953Srrs * we can't do the reset we must copy the request. c) queue 3100163953Srrs * it, and setup the data in processor to trigger it off 3101163953Srrs * when needed and dequeue all the queued data. 3102163953Srrs */ 3103163953Srrs tsn = ntohl(req->send_reset_at_tsn); 3104163953Srrs 3105163953Srrs /* move the reset action back one */ 3106163953Srrs asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3107163953Srrs if ((tsn == asoc->cumulative_tsn) || 3108163953Srrs (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) { 3109163953Srrs /* we can do it now */ 3110163953Srrs sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3111163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3112163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3113163953Srrs } else { 3114163953Srrs /* 3115163953Srrs * we must queue it up and thus wait for the TSN's 3116163953Srrs * to arrive that are at or before tsn 3117163953Srrs */ 3118163953Srrs struct sctp_stream_reset_list *liste; 3119163953Srrs int siz; 3120163953Srrs 3121163953Srrs siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3122163953Srrs SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3123163953Srrs siz, "StrRstList"); 3124163953Srrs if (liste == NULL) { 3125163953Srrs /* gak out of memory */ 3126163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED); 3127163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED; 3128163953Srrs return; 3129163953Srrs } 3130163953Srrs liste->tsn = tsn; 3131163953Srrs liste->number_entries = number_entries; 3132163953Srrs memcpy(&liste->req, req, 3133163953Srrs (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3134163953Srrs TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3135163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED); 3136163953Srrs asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED; 3137163953Srrs } 3138163953Srrs asoc->str_reset_seq_in++; 3139163953Srrs } else if ((asoc->str_reset_seq_in - 1) == seq) { 3140163953Srrs /* 3141163953Srrs * one seq back, just echo back last action since my 3142163953Srrs * response was lost. 3143163953Srrs */ 3144163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3145163953Srrs } else if ((asoc->str_reset_seq_in - 2) == seq) { 3146163953Srrs /* 3147163953Srrs * two seq back, just echo back last action since my 3148163953Srrs * response was lost. 3149163953Srrs */ 3150163953Srrs sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3151163953Srrs } else { 3152163953Srrs sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO); 3153163953Srrs } 3154163953Srrs} 3155163953Srrs 3156163953Srrsstatic int 3157163953Srrssctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_out_req *sr_req) 3158163953Srrs{ 3159163953Srrs int chk_length, param_len, ptype; 3160163953Srrs uint32_t seq; 3161163953Srrs int num_req = 0; 3162163953Srrs struct sctp_tmit_chunk *chk; 3163163953Srrs struct sctp_chunkhdr *ch; 3164163953Srrs struct sctp_paramhdr *ph; 3165166675Srrs int ret_code = 0; 3166166675Srrs int num_param = 0; 3167163953Srrs 3168163953Srrs /* now it may be a reset or a reset-response */ 3169163953Srrs chk_length = ntohs(sr_req->ch.chunk_length); 3170163953Srrs 3171163953Srrs /* setup for adding the response */ 3172163953Srrs sctp_alloc_a_chunk(stcb, chk); 3173163953Srrs if (chk == NULL) { 3174163953Srrs return (ret_code); 3175163953Srrs } 3176163953Srrs chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3177169208Srrs chk->rec.chunk_id.can_take_data = 0; 3178163953Srrs chk->asoc = &stcb->asoc; 3179163953Srrs chk->no_fr_allowed = 0; 3180163953Srrs chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 3181166023Srrs chk->book_size_scale = 0; 3182165647Srrs chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3183163953Srrs if (chk->data == NULL) { 3184163953Srrsstrres_nochunk: 3185163953Srrs if (chk->data) { 3186163953Srrs sctp_m_freem(chk->data); 3187163953Srrs chk->data = NULL; 3188163953Srrs } 3189163953Srrs sctp_free_a_chunk(stcb, chk); 3190163953Srrs return (ret_code); 3191163953Srrs } 3192165647Srrs SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 3193163953Srrs 3194163953Srrs /* setup chunk parameters */ 3195163953Srrs chk->sent = SCTP_DATAGRAM_UNSENT; 3196163953Srrs chk->snd_count = 0; 3197163953Srrs chk->whoTo = stcb->asoc.primary_destination; 3198163953Srrs atomic_add_int(&chk->whoTo->ref_count, 1); 3199163953Srrs 3200163953Srrs ch = mtod(chk->data, struct sctp_chunkhdr *); 3201163953Srrs ch->chunk_type = SCTP_STREAM_RESET; 3202163953Srrs ch->chunk_flags = 0; 3203163953Srrs ch->chunk_length = htons(chk->send_size); 3204165647Srrs SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 3205163953Srrs ph = (struct sctp_paramhdr *)&sr_req->sr_req; 3206163953Srrs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 3207163953Srrs param_len = ntohs(ph->param_length); 3208163953Srrs if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 3209163953Srrs /* bad param */ 3210163953Srrs break; 3211163953Srrs } 3212163953Srrs ptype = ntohs(ph->param_type); 3213163953Srrs num_param++; 3214163953Srrs if (num_param > SCTP_MAX_RESET_PARAMS) { 3215163953Srrs /* hit the max of parameters already sorry.. */ 3216163953Srrs break; 3217163953Srrs } 3218163953Srrs if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 3219163953Srrs struct sctp_stream_reset_out_request *req_out; 3220163953Srrs 3221163953Srrs req_out = (struct sctp_stream_reset_out_request *)ph; 3222163953Srrs num_req++; 3223163953Srrs if (stcb->asoc.stream_reset_outstanding) { 3224163953Srrs seq = ntohl(req_out->response_seq); 3225163953Srrs if (seq == stcb->asoc.str_reset_seq_out) { 3226163953Srrs /* implicit ack */ 3227163953Srrs sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL); 3228163953Srrs } 3229163953Srrs } 3230163953Srrs sctp_handle_str_reset_request_out(stcb, chk, req_out); 3231163953Srrs } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 3232163953Srrs struct sctp_stream_reset_in_request *req_in; 3233163953Srrs 3234163953Srrs num_req++; 3235163953Srrs req_in = (struct sctp_stream_reset_in_request *)ph; 3236163953Srrs sctp_handle_str_reset_request_in(stcb, chk, req_in); 3237163953Srrs } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 3238163953Srrs struct sctp_stream_reset_tsn_request *req_tsn; 3239163953Srrs 3240163953Srrs num_req++; 3241163953Srrs req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 3242163953Srrs if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 3243163953Srrs ret_code = 1; 3244163953Srrs goto strres_nochunk; 3245163953Srrs } 3246163953Srrs /* no more */ 3247163953Srrs break; 3248163953Srrs } else if (ptype == SCTP_STR_RESET_RESPONSE) { 3249163953Srrs struct sctp_stream_reset_response *resp; 3250163953Srrs uint32_t result; 3251163953Srrs 3252163953Srrs resp = (struct sctp_stream_reset_response *)ph; 3253163953Srrs seq = ntohl(resp->response_seq); 3254163953Srrs result = ntohl(resp->result); 3255163953Srrs if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 3256163953Srrs ret_code = 1; 3257163953Srrs goto strres_nochunk; 3258163953Srrs } 3259163953Srrs } else { 3260163953Srrs break; 3261163953Srrs } 3262163953Srrs 3263163953Srrs ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len)); 3264163953Srrs chk_length -= SCTP_SIZE32(param_len); 3265163953Srrs } 3266163953Srrs if (num_req == 0) { 3267163953Srrs /* we have no response free the stuff */ 3268163953Srrs goto strres_nochunk; 3269163953Srrs } 3270163953Srrs /* ok we have a chunk to link in */ 3271163953Srrs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 3272163953Srrs chk, 3273163953Srrs sctp_next); 3274163953Srrs stcb->asoc.ctrl_queue_cnt++; 3275163953Srrs return (ret_code); 3276163953Srrs} 3277163953Srrs 3278163953Srrs/* 3279163953Srrs * Handle a router or endpoints report of a packet loss, there are two ways 3280163953Srrs * to handle this, either we get the whole packet and must disect it 3281163953Srrs * ourselves (possibly with truncation and or corruption) or it is a summary 3282163953Srrs * from a middle box that did the disectting for us. 3283163953Srrs */ 3284163953Srrsstatic void 3285163953Srrssctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 3286163953Srrs struct sctp_tcb *stcb, struct sctp_nets *net) 3287163953Srrs{ 3288163953Srrs uint32_t bottle_bw, on_queue; 3289163953Srrs uint16_t trunc_len; 3290163953Srrs unsigned int chlen; 3291163953Srrs unsigned int at; 3292163953Srrs struct sctp_chunk_desc desc; 3293163953Srrs struct sctp_chunkhdr *ch; 3294163953Srrs 3295163953Srrs chlen = ntohs(cp->ch.chunk_length); 3296163953Srrs chlen -= sizeof(struct sctp_pktdrop_chunk); 3297163953Srrs /* XXX possible chlen underflow */ 3298163953Srrs if (chlen == 0) { 3299163953Srrs ch = NULL; 3300163953Srrs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 3301163953Srrs SCTP_STAT_INCR(sctps_pdrpbwrpt); 3302163953Srrs } else { 3303163953Srrs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 3304163953Srrs chlen -= sizeof(struct sctphdr); 3305163953Srrs /* XXX possible chlen underflow */ 3306163953Srrs memset(&desc, 0, sizeof(desc)); 3307163953Srrs } 3308163953Srrs trunc_len = (uint16_t) ntohs(cp->trunc_len); 3309163953Srrs /* now the chunks themselves */ 3310163953Srrs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 3311163953Srrs desc.chunk_type = ch->chunk_type; 3312163953Srrs /* get amount we need to move */ 3313163953Srrs at = ntohs(ch->chunk_length); 3314163953Srrs if (at < sizeof(struct sctp_chunkhdr)) { 3315163953Srrs /* corrupt chunk, maybe at the end? */ 3316163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3317163953Srrs break; 3318163953Srrs } 3319163953Srrs if (trunc_len == 0) { 3320163953Srrs /* we are supposed to have all of it */ 3321163953Srrs if (at > chlen) { 3322163953Srrs /* corrupt skip it */ 3323163953Srrs SCTP_STAT_INCR(sctps_pdrpcrupt); 3324163953Srrs break; 3325163953Srrs } 3326163953Srrs } else { 3327163953Srrs /* is there enough of it left ? */ 3328163953Srrs if (desc.chunk_type == SCTP_DATA) { 3329163953Srrs if (chlen < (sizeof(struct sctp_data_chunk) + 3330163953Srrs sizeof(desc.data_bytes))) { 3331163953Srrs break; 3332163953Srrs } 3333163953Srrs } else { 3334163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3335163953Srrs break; 3336163953Srrs } 3337163953Srrs } 3338163953Srrs } 3339163953Srrs if (desc.chunk_type == SCTP_DATA) { 3340163953Srrs /* can we get out the tsn? */ 3341163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3342163953Srrs SCTP_STAT_INCR(sctps_pdrpmbda); 3343163953Srrs 3344163953Srrs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 3345163953Srrs /* yep */ 3346163953Srrs struct sctp_data_chunk *dcp; 3347163953Srrs uint8_t *ddp; 3348163953Srrs unsigned int iii; 3349163953Srrs 3350163953Srrs dcp = (struct sctp_data_chunk *)ch; 3351163953Srrs ddp = (uint8_t *) (dcp + 1); 3352163953Srrs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 3353163953Srrs desc.data_bytes[iii] = ddp[iii]; 3354163953Srrs } 3355163953Srrs desc.tsn_ifany = dcp->dp.tsn; 3356163953Srrs } else { 3357163953Srrs /* nope we are done. */ 3358163953Srrs SCTP_STAT_INCR(sctps_pdrpnedat); 3359163953Srrs break; 3360163953Srrs } 3361163953Srrs } else { 3362163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 3363163953Srrs SCTP_STAT_INCR(sctps_pdrpmbct); 3364163953Srrs } 3365163953Srrs 3366163953Srrs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 3367163953Srrs SCTP_STAT_INCR(sctps_pdrppdbrk); 3368163953Srrs break; 3369163953Srrs } 3370163953Srrs if (SCTP_SIZE32(at) > chlen) { 3371163953Srrs break; 3372163953Srrs } 3373163953Srrs chlen -= SCTP_SIZE32(at); 3374163953Srrs if (chlen < sizeof(struct sctp_chunkhdr)) { 3375163953Srrs /* done, none left */ 3376163953Srrs break; 3377163953Srrs } 3378163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 3379163953Srrs } 3380163953Srrs /* Now update any rwnd --- possibly */ 3381163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 3382163953Srrs /* From a peer, we get a rwnd report */ 3383163953Srrs uint32_t a_rwnd; 3384163953Srrs 3385163953Srrs SCTP_STAT_INCR(sctps_pdrpfehos); 3386163953Srrs 3387163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3388163953Srrs on_queue = ntohl(cp->current_onq); 3389163953Srrs if (bottle_bw && on_queue) { 3390163953Srrs /* a rwnd report is in here */ 3391163953Srrs if (bottle_bw > on_queue) 3392163953Srrs a_rwnd = bottle_bw - on_queue; 3393163953Srrs else 3394163953Srrs a_rwnd = 0; 3395163953Srrs 3396163953Srrs if (a_rwnd == 0) 3397163953Srrs stcb->asoc.peers_rwnd = 0; 3398163953Srrs else { 3399163953Srrs if (a_rwnd > stcb->asoc.total_flight) { 3400163953Srrs stcb->asoc.peers_rwnd = 3401163953Srrs a_rwnd - stcb->asoc.total_flight; 3402163953Srrs } else { 3403163953Srrs stcb->asoc.peers_rwnd = 0; 3404163953Srrs } 3405163953Srrs if (stcb->asoc.peers_rwnd < 3406163953Srrs stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3407163953Srrs /* SWS sender side engages */ 3408163953Srrs stcb->asoc.peers_rwnd = 0; 3409163953Srrs } 3410163953Srrs } 3411163953Srrs } 3412163953Srrs } else { 3413163953Srrs SCTP_STAT_INCR(sctps_pdrpfmbox); 3414163953Srrs } 3415163953Srrs 3416163953Srrs /* now middle boxes in sat networks get a cwnd bump */ 3417163953Srrs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 3418163953Srrs (stcb->asoc.sat_t3_loss_recovery == 0) && 3419163953Srrs (stcb->asoc.sat_network)) { 3420163953Srrs /* 3421163953Srrs * This is debateable but for sat networks it makes sense 3422163953Srrs * Note if a T3 timer has went off, we will prohibit any 3423163953Srrs * changes to cwnd until we exit the t3 loss recovery. 3424163953Srrs */ 3425163953Srrs uint32_t bw_avail; 3426163953Srrs int rtt, incr; 3427163953Srrs 3428163953Srrs#ifdef SCTP_CWND_MONITOR 3429163953Srrs int old_cwnd = net->cwnd; 3430163953Srrs 3431163953Srrs#endif 3432163953Srrs /* need real RTT for this calc */ 3433163953Srrs rtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 3434163953Srrs /* get bottle neck bw */ 3435163953Srrs bottle_bw = ntohl(cp->bottle_bw); 3436163953Srrs /* and whats on queue */ 3437163953Srrs on_queue = ntohl(cp->current_onq); 3438163953Srrs /* 3439163953Srrs * adjust the on-queue if our flight is more it could be 3440163953Srrs * that the router has not yet gotten data "in-flight" to it 3441163953Srrs */ 3442163953Srrs if (on_queue < net->flight_size) 3443163953Srrs on_queue = net->flight_size; 3444163953Srrs 3445163953Srrs /* calculate the available space */ 3446163953Srrs bw_avail = (bottle_bw * rtt) / 1000; 3447163953Srrs if (bw_avail > bottle_bw) { 3448163953Srrs /* 3449163953Srrs * Cap the growth to no more than the bottle neck. 3450163953Srrs * This can happen as RTT slides up due to queues. 3451163953Srrs * It also means if you have more than a 1 second 3452163953Srrs * RTT with a empty queue you will be limited to the 3453163953Srrs * bottle_bw per second no matter if other points 3454163953Srrs * have 1/2 the RTT and you could get more out... 3455163953Srrs */ 3456163953Srrs bw_avail = bottle_bw; 3457163953Srrs } 3458163953Srrs if (on_queue > bw_avail) { 3459163953Srrs /* 3460163953Srrs * No room for anything else don't allow anything 3461163953Srrs * else to be "added to the fire". 3462163953Srrs */ 3463163953Srrs int seg_inflight, seg_onqueue, my_portion; 3464163953Srrs 3465163953Srrs net->partial_bytes_acked = 0; 3466163953Srrs 3467163953Srrs /* how much are we over queue size? */ 3468163953Srrs incr = on_queue - bw_avail; 3469163953Srrs if (stcb->asoc.seen_a_sack_this_pkt) { 3470163953Srrs /* 3471163953Srrs * undo any cwnd adjustment that the sack 3472163953Srrs * might have made 3473163953Srrs */ 3474163953Srrs net->cwnd = net->prev_cwnd; 3475163953Srrs } 3476163953Srrs /* Now how much of that is mine? */ 3477163953Srrs seg_inflight = net->flight_size / net->mtu; 3478163953Srrs seg_onqueue = on_queue / net->mtu; 3479163953Srrs my_portion = (incr * seg_inflight) / seg_onqueue; 3480163953Srrs 3481163953Srrs /* Have I made an adjustment already */ 3482163953Srrs if (net->cwnd > net->flight_size) { 3483163953Srrs /* 3484163953Srrs * for this flight I made an adjustment we 3485163953Srrs * need to decrease the portion by a share 3486163953Srrs * our previous adjustment. 3487163953Srrs */ 3488163953Srrs int diff_adj; 3489163953Srrs 3490163953Srrs diff_adj = net->cwnd - net->flight_size; 3491163953Srrs if (diff_adj > my_portion) 3492163953Srrs my_portion = 0; 3493163953Srrs else 3494163953Srrs my_portion -= diff_adj; 3495163953Srrs } 3496163953Srrs /* 3497163953Srrs * back down to the previous cwnd (assume we have 3498163953Srrs * had a sack before this packet). minus what ever 3499163953Srrs * portion of the overage is my fault. 3500163953Srrs */ 3501163953Srrs net->cwnd -= my_portion; 3502163953Srrs 3503163953Srrs /* we will NOT back down more than 1 MTU */ 3504163953Srrs if (net->cwnd <= net->mtu) { 3505163953Srrs net->cwnd = net->mtu; 3506163953Srrs } 3507163953Srrs /* force into CA */ 3508163953Srrs net->ssthresh = net->cwnd - 1; 3509163953Srrs } else { 3510163953Srrs /* 3511163953Srrs * Take 1/4 of the space left or max burst up .. 3512163953Srrs * whichever is less. 3513163953Srrs */ 3514163953Srrs incr = min((bw_avail - on_queue) >> 2, 3515163953Srrs (int)stcb->asoc.max_burst * (int)net->mtu); 3516163953Srrs net->cwnd += incr; 3517163953Srrs } 3518163953Srrs if (net->cwnd > bw_avail) { 3519163953Srrs /* We can't exceed the pipe size */ 3520163953Srrs net->cwnd = bw_avail; 3521163953Srrs } 3522163953Srrs if (net->cwnd < net->mtu) { 3523163953Srrs /* We always have 1 MTU */ 3524163953Srrs net->cwnd = net->mtu; 3525163953Srrs } 3526163953Srrs#ifdef SCTP_CWND_MONITOR 3527163953Srrs if (net->cwnd - old_cwnd != 0) { 3528163953Srrs /* log only changes */ 3529163953Srrs sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), 3530163953Srrs SCTP_CWND_LOG_FROM_SAT); 3531163953Srrs } 3532163953Srrs#endif 3533163953Srrs } 3534163953Srrs} 3535163953Srrs 3536163953Srrs/* 3537163953Srrs * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 3538163953Srrs * still contain IP/SCTP header - stcb: is the tcb found for this packet - 3539163953Srrs * offset: offset into the mbuf chain to first chunkhdr - length: is the 3540163953Srrs * length of the complete packet outputs: - length: modified to remaining 3541163953Srrs * length after control processing - netp: modified to new sctp_nets after 3542163953Srrs * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 3543163953Srrs * bad packet,...) otherwise return the tcb for this packet 3544163953Srrs */ 3545163953Srrsstatic struct sctp_tcb * 3546163953Srrssctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 3547163953Srrs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 3548169352Srrs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 3549169352Srrs uint32_t vrf_id, uint32_t table_id) 3550163953Srrs{ 3551163953Srrs struct sctp_association *asoc; 3552163953Srrs uint32_t vtag_in; 3553163953Srrs int num_chunks = 0; /* number of control chunks processed */ 3554163953Srrs int chk_length; 3555163953Srrs int ret; 3556168299Srrs int abort_no_unlock = 0; 3557163953Srrs 3558163953Srrs /* 3559163953Srrs * How big should this be, and should it be alloc'd? Lets try the 3560163953Srrs * d-mtu-ceiling for now (2k) and that should hopefully work ... 3561163953Srrs * until we get into jumbo grams and such.. 3562163953Srrs */ 3563166675Srrs uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 3564163953Srrs struct sctp_tcb *locked_tcb = stcb; 3565163953Srrs int got_auth = 0; 3566163953Srrs uint32_t auth_offset = 0, auth_len = 0; 3567163953Srrs int auth_skipped = 0; 3568163953Srrs 3569163953Srrs#ifdef SCTP_DEBUG 3570163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 3571163953Srrs printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 3572163953Srrs iphlen, *offset, length, stcb); 3573163953Srrs } 3574163953Srrs#endif /* SCTP_DEBUG */ 3575163953Srrs 3576163953Srrs /* validate chunk header length... */ 3577163953Srrs if (ntohs(ch->chunk_length) < sizeof(*ch)) { 3578163953Srrs return (NULL); 3579163953Srrs } 3580163953Srrs /* 3581163953Srrs * validate the verification tag 3582163953Srrs */ 3583163953Srrs vtag_in = ntohl(sh->v_tag); 3584163953Srrs 3585165220Srrs if (locked_tcb) { 3586165220Srrs SCTP_TCB_LOCK_ASSERT(locked_tcb); 3587165220Srrs } 3588163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 3589163953Srrs if (vtag_in != 0) { 3590163953Srrs /* protocol error- silently discard... */ 3591163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3592163953Srrs if (locked_tcb) 3593163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3594163953Srrs return (NULL); 3595163953Srrs } 3596163953Srrs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 3597163953Srrs /* 3598163953Srrs * If there is no stcb, skip the AUTH chunk and process 3599163953Srrs * later after a stcb is found (to validate the lookup was 3600163953Srrs * valid. 3601163953Srrs */ 3602163953Srrs if ((ch->chunk_type == SCTP_AUTHENTICATION) && 3603163953Srrs (stcb == NULL) && !sctp_auth_disable) { 3604163953Srrs /* save this chunk for later processing */ 3605163953Srrs auth_skipped = 1; 3606163953Srrs auth_offset = *offset; 3607163953Srrs auth_len = ntohs(ch->chunk_length); 3608163953Srrs 3609163953Srrs /* (temporarily) move past this chunk */ 3610163953Srrs *offset += SCTP_SIZE32(auth_len); 3611163953Srrs if (*offset >= length) { 3612163953Srrs /* no more data left in the mbuf chain */ 3613163953Srrs *offset = length; 3614163953Srrs return (NULL); 3615163953Srrs } 3616163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3617163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 3618163953Srrs } 3619163953Srrs if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3620163953Srrs goto process_control_chunks; 3621163953Srrs } 3622163953Srrs /* 3623163953Srrs * first check if it's an ASCONF with an unknown src addr we 3624163953Srrs * need to look inside to find the association 3625163953Srrs */ 3626163953Srrs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 3627163953Srrs /* inp's refcount may be reduced */ 3628163953Srrs SCTP_INP_INCR_REF(inp); 3629163953Srrs 3630163953Srrs stcb = sctp_findassociation_ep_asconf(m, iphlen, 3631163953Srrs *offset, sh, &inp, netp); 3632163953Srrs if (stcb == NULL) { 3633163953Srrs /* 3634163953Srrs * reduce inp's refcount if not reduced in 3635163953Srrs * sctp_findassociation_ep_asconf(). 3636163953Srrs */ 3637163953Srrs SCTP_INP_DECR_REF(inp); 3638163953Srrs } 3639163953Srrs /* now go back and verify any auth chunk to be sure */ 3640163953Srrs if (auth_skipped && (stcb != NULL)) { 3641163953Srrs struct sctp_auth_chunk *auth; 3642163953Srrs 3643163953Srrs auth = (struct sctp_auth_chunk *) 3644163953Srrs sctp_m_getptr(m, auth_offset, 3645163953Srrs auth_len, chunk_buf); 3646163953Srrs got_auth = 1; 3647163953Srrs auth_skipped = 0; 3648163953Srrs if (sctp_handle_auth(stcb, auth, m, 3649163953Srrs auth_offset)) { 3650163953Srrs /* auth HMAC failed so dump it */ 3651163953Srrs *offset = length; 3652163953Srrs return (NULL); 3653163953Srrs } else { 3654163953Srrs /* remaining chunks are HMAC checked */ 3655163953Srrs stcb->asoc.authenticated = 1; 3656163953Srrs } 3657163953Srrs } 3658163953Srrs } 3659163953Srrs if (stcb == NULL) { 3660163953Srrs /* no association, so it's out of the blue... */ 3661169352Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 3662169352Srrs vrf_id, table_id); 3663163953Srrs *offset = length; 3664163953Srrs if (locked_tcb) 3665163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3666163953Srrs return (NULL); 3667163953Srrs } 3668163953Srrs asoc = &stcb->asoc; 3669163953Srrs /* ABORT and SHUTDOWN can use either v_tag... */ 3670163953Srrs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 3671163953Srrs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 3672163953Srrs (ch->chunk_type == SCTP_PACKET_DROPPED)) { 3673163953Srrs if ((vtag_in == asoc->my_vtag) || 3674163953Srrs ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 3675163953Srrs (vtag_in == asoc->peer_vtag))) { 3676163953Srrs /* this is valid */ 3677163953Srrs } else { 3678163953Srrs /* drop this packet... */ 3679163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3680163953Srrs if (locked_tcb) 3681163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3682163953Srrs return (NULL); 3683163953Srrs } 3684163953Srrs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 3685163953Srrs if (vtag_in != asoc->my_vtag) { 3686163953Srrs /* 3687163953Srrs * this could be a stale SHUTDOWN-ACK or the 3688163953Srrs * peer never got the SHUTDOWN-COMPLETE and 3689163953Srrs * is still hung; we have started a new asoc 3690163953Srrs * but it won't complete until the shutdown 3691163953Srrs * is completed 3692163953Srrs */ 3693163953Srrs if (locked_tcb) 3694163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3695163953Srrs sctp_handle_ootb(m, iphlen, *offset, sh, inp, 3696169352Srrs NULL, vrf_id, table_id); 3697163953Srrs return (NULL); 3698163953Srrs } 3699163953Srrs } else { 3700163953Srrs /* for all other chunks, vtag must match */ 3701163953Srrs if (vtag_in != asoc->my_vtag) { 3702163953Srrs /* invalid vtag... */ 3703163953Srrs#ifdef SCTP_DEBUG 3704163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3705163953Srrs printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag); 3706163953Srrs } 3707163953Srrs#endif /* SCTP_DEBUG */ 3708163953Srrs SCTP_STAT_INCR(sctps_badvtag); 3709163953Srrs if (locked_tcb) 3710163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3711163953Srrs *offset = length; 3712163953Srrs return (NULL); 3713163953Srrs } 3714163953Srrs } 3715163953Srrs } /* end if !SCTP_COOKIE_ECHO */ 3716163953Srrs /* 3717163953Srrs * process all control chunks... 3718163953Srrs */ 3719163953Srrs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 3720163953Srrs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 3721163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 3722163953Srrs /* implied cookie-ack.. we must have lost the ack */ 3723163953Srrs stcb->asoc.overall_error_count = 0; 3724163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 3725163953Srrs *netp); 3726163953Srrs } 3727163953Srrsprocess_control_chunks: 3728163953Srrs while (IS_SCTP_CONTROL(ch)) { 3729163953Srrs /* validate chunk length */ 3730163953Srrs chk_length = ntohs(ch->chunk_length); 3731163953Srrs#ifdef SCTP_DEBUG 3732163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT2) { 3733163953Srrs printf("sctp_process_control: processing a chunk type=%u, len=%u\n", 3734163953Srrs ch->chunk_type, chk_length); 3735163953Srrs } 3736163953Srrs#endif /* SCTP_DEBUG */ 3737163953Srrs if ((size_t)chk_length < sizeof(*ch) || 3738163953Srrs (*offset + chk_length) > length) { 3739163953Srrs *offset = length; 3740163953Srrs if (locked_tcb) 3741163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3742163953Srrs return (NULL); 3743163953Srrs } 3744163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 3745163953Srrs /* 3746163953Srrs * INIT-ACK only gets the init ack "header" portion only 3747163953Srrs * because we don't have to process the peer's COOKIE. All 3748163953Srrs * others get a complete chunk. 3749163953Srrs */ 3750169208Srrs if ((ch->chunk_type == SCTP_INITIATION_ACK) || 3751169208Srrs (ch->chunk_type == SCTP_INITIATION)) { 3752163953Srrs /* get an init-ack chunk */ 3753163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3754163953Srrs sizeof(struct sctp_init_ack_chunk), chunk_buf); 3755163953Srrs if (ch == NULL) { 3756163953Srrs *offset = length; 3757163953Srrs if (locked_tcb) 3758163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3759163953Srrs return (NULL); 3760163953Srrs } 3761169208Srrs } else if (ch->chunk_type == SCTP_COOKIE_ECHO) { 3762169208Srrs if (chk_length > sizeof(chunk_buf)) { 3763169208Srrs /* 3764169208Srrs * use just the size of the chunk buffer so 3765169208Srrs * the front part of our cookie is intact. 3766169208Srrs * The rest of cookie processing should use 3767169208Srrs * the sctp_m_getptr() function to access 3768169208Srrs * the other parts. 3769169208Srrs */ 3770169208Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3771169208Srrs (sizeof(chunk_buf) - 4), 3772169208Srrs chunk_buf); 3773169208Srrs if (ch == NULL) { 3774169208Srrs *offset = length; 3775169208Srrs if (locked_tcb) 3776169208Srrs SCTP_TCB_UNLOCK(locked_tcb); 3777169208Srrs return (NULL); 3778169208Srrs } 3779169208Srrs } else { 3780169208Srrs /* We can fit it all */ 3781169208Srrs goto all_fits; 3782169208Srrs } 3783163953Srrs } else { 3784163953Srrs /* get a complete chunk... */ 3785163953Srrs if ((size_t)chk_length > sizeof(chunk_buf)) { 3786163953Srrs struct mbuf *oper; 3787163953Srrs struct sctp_paramhdr *phdr; 3788163953Srrs 3789163953Srrs oper = NULL; 3790169208Srrs if (stcb) { 3791169208Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 3792169208Srrs 0, M_DONTWAIT, 1, MT_DATA); 3793169208Srrs 3794169208Srrs if (oper) { 3795169208Srrs /* pre-reserve some space */ 3796169208Srrs SCTP_BUF_RESV_UF(oper, sizeof(struct sctp_chunkhdr)); 3797169208Srrs SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr); 3798169208Srrs phdr = mtod(oper, struct sctp_paramhdr *); 3799169208Srrs phdr->param_type = htons(SCTP_CAUSE_OUT_OF_RESC); 3800169208Srrs phdr->param_length = htons(sizeof(struct sctp_paramhdr)); 3801169208Srrs sctp_queue_op_err(stcb, oper); 3802169208Srrs } 3803163953Srrs } 3804163953Srrs if (locked_tcb) 3805163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3806163953Srrs return (NULL); 3807163953Srrs } 3808169208Srrs all_fits: 3809163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 3810163953Srrs chk_length, chunk_buf); 3811163953Srrs if (ch == NULL) { 3812163953Srrs printf("sctp_process_control: Can't get the all data....\n"); 3813163953Srrs *offset = length; 3814163953Srrs if (locked_tcb) 3815163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3816163953Srrs return (NULL); 3817163953Srrs } 3818163953Srrs } 3819163953Srrs num_chunks++; 3820163953Srrs /* Save off the last place we got a control from */ 3821163953Srrs if (stcb != NULL) { 3822163953Srrs if ((*netp != NULL) || (ch->chunk_type == SCTP_ASCONF)) { 3823163953Srrs /* 3824163953Srrs * allow last_control to be NULL if 3825163953Srrs * ASCONF... ASCONF processing will find the 3826163953Srrs * right net later 3827163953Srrs */ 3828163953Srrs stcb->asoc.last_control_chunk_from = *netp; 3829163953Srrs } 3830163953Srrs } 3831163953Srrs#ifdef SCTP_AUDITING_ENABLED 3832163953Srrs sctp_audit_log(0xB0, ch->chunk_type); 3833163953Srrs#endif 3834163953Srrs 3835163953Srrs /* check to see if this chunk required auth, but isn't */ 3836163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 3837163953Srrs sctp_auth_is_required_chunk(ch->chunk_type, 3838163953Srrs stcb->asoc.local_auth_chunks) && 3839163953Srrs !stcb->asoc.authenticated) { 3840163953Srrs /* "silently" ignore */ 3841163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 3842163953Srrs goto next_chunk; 3843163953Srrs } 3844163953Srrs switch (ch->chunk_type) { 3845163953Srrs case SCTP_INITIATION: 3846163953Srrs /* must be first and only chunk */ 3847163953Srrs#ifdef SCTP_DEBUG 3848163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3849163953Srrs printf("SCTP_INIT\n"); 3850163953Srrs } 3851163953Srrs#endif /* SCTP_DEBUG */ 3852163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3853163953Srrs /* We are not interested anymore? */ 3854163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3855163953Srrs /* 3856163953Srrs * collision case where we are 3857163953Srrs * sending to them too 3858163953Srrs */ 3859163953Srrs ; 3860163953Srrs } else { 3861163953Srrs if (locked_tcb) 3862163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3863163953Srrs *offset = length; 3864163953Srrs return (NULL); 3865163953Srrs } 3866163953Srrs } 3867163953Srrs if ((num_chunks > 1) || 3868163953Srrs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) { 3869163953Srrs *offset = length; 3870163953Srrs if (locked_tcb) 3871163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3872163953Srrs return (NULL); 3873163953Srrs } 3874163953Srrs if ((stcb != NULL) && 3875163953Srrs (SCTP_GET_STATE(&stcb->asoc) == 3876163953Srrs SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3877163953Srrs sctp_send_shutdown_ack(stcb, 3878163953Srrs stcb->asoc.primary_destination); 3879163953Srrs *offset = length; 3880163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3881163953Srrs if (locked_tcb) 3882163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3883163953Srrs return (NULL); 3884163953Srrs } 3885163953Srrs sctp_handle_init(m, iphlen, *offset, sh, 3886169352Srrs (struct sctp_init_chunk *)ch, inp, stcb, *netp, &abort_no_unlock, vrf_id, table_id); 3887168299Srrs if (abort_no_unlock) 3888168299Srrs return (NULL); 3889168299Srrs 3890163953Srrs *offset = length; 3891163953Srrs if (locked_tcb) 3892163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3893163953Srrs return (NULL); 3894163953Srrs break; 3895163953Srrs case SCTP_INITIATION_ACK: 3896163953Srrs /* must be first and only chunk */ 3897163953Srrs#ifdef SCTP_DEBUG 3898163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3899163953Srrs printf("SCTP_INIT-ACK\n"); 3900163953Srrs } 3901163953Srrs#endif /* SCTP_DEBUG */ 3902163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3903163953Srrs /* We are not interested anymore */ 3904163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 3905163953Srrs ; 3906163953Srrs } else { 3907163953Srrs if (locked_tcb) 3908163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3909163953Srrs *offset = length; 3910163953Srrs if (stcb) { 3911165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3912163953Srrs } 3913163953Srrs return (NULL); 3914163953Srrs } 3915163953Srrs } 3916163953Srrs if ((num_chunks > 1) || 3917163953Srrs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) { 3918163953Srrs *offset = length; 3919163953Srrs if (locked_tcb) 3920163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3921163953Srrs return (NULL); 3922163953Srrs } 3923163953Srrs ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 3924169352Srrs (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id, table_id); 3925163953Srrs /* 3926163953Srrs * Special case, I must call the output routine to 3927163953Srrs * get the cookie echoed 3928163953Srrs */ 3929168299Srrs if (abort_no_unlock) 3930168299Srrs return (NULL); 3931168299Srrs 3932163953Srrs if ((stcb) && ret == 0) 3933163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 3934163953Srrs *offset = length; 3935163953Srrs if (locked_tcb) 3936163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 3937163953Srrs return (NULL); 3938163953Srrs break; 3939163953Srrs case SCTP_SELECTIVE_ACK: 3940163953Srrs#ifdef SCTP_DEBUG 3941163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 3942163953Srrs printf("SCTP_SACK\n"); 3943163953Srrs } 3944163953Srrs#endif /* SCTP_DEBUG */ 3945163953Srrs SCTP_STAT_INCR(sctps_recvsacks); 3946163953Srrs { 3947163953Srrs struct sctp_sack_chunk *sack; 3948163953Srrs int abort_now = 0; 3949163953Srrs uint32_t a_rwnd, cum_ack; 3950163953Srrs uint16_t num_seg; 3951163953Srrs int nonce_sum_flag; 3952163953Srrs 3953169208Srrs if (chk_length < sizeof(struct sctp_sack_chunk)) { 3954169208Srrs#ifdef SCTP_DEBUG 3955169208Srrs if (sctp_debug_on & SCTP_DEBUG_INDATA1) { 3956169208Srrs printf("Bad size on sack chunk .. to small\n"); 3957169208Srrs } 3958169208Srrs#endif 3959169208Srrs *offset = length; 3960169352Srrs if (locked_tcb) 3961169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 3962169352Srrs 3963169208Srrs return (NULL); 3964169208Srrs } 3965163953Srrs sack = (struct sctp_sack_chunk *)ch; 3966163953Srrs nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM; 3967163953Srrs cum_ack = ntohl(sack->sack.cum_tsn_ack); 3968163953Srrs num_seg = ntohs(sack->sack.num_gap_ack_blks); 3969163953Srrs a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 3970163953Srrs stcb->asoc.seen_a_sack_this_pkt = 1; 3971163953Srrs if ((stcb->asoc.pr_sctp_cnt == 0) && 3972163953Srrs (num_seg == 0) && 3973163953Srrs ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) || 3974163953Srrs (cum_ack == stcb->asoc.last_acked_seq)) && 3975163953Srrs (stcb->asoc.saw_sack_with_frags == 0) && 3976163953Srrs (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 3977163953Srrs ) { 3978163953Srrs /* 3979163953Srrs * We have a SIMPLE sack having no 3980163953Srrs * prior segments and data on sent 3981163953Srrs * queue to be acked.. Use the 3982163953Srrs * faster path sack processing. We 3983163953Srrs * also allow window update sacks 3984163953Srrs * with no missing segments to go 3985163953Srrs * this way too. 3986163953Srrs */ 3987169208Srrs sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag, 3988169208Srrs &abort_now); 3989163953Srrs } else { 3990169208Srrs sctp_handle_sack(sack, stcb, *netp, &abort_now, chk_length, a_rwnd); 3991163953Srrs } 3992163953Srrs if (abort_now) { 3993163953Srrs /* ABORT signal from sack processing */ 3994163953Srrs *offset = length; 3995163953Srrs return (NULL); 3996163953Srrs } 3997163953Srrs } 3998163953Srrs break; 3999163953Srrs case SCTP_HEARTBEAT_REQUEST: 4000163953Srrs#ifdef SCTP_DEBUG 4001163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4002163953Srrs printf("SCTP_HEARTBEAT\n"); 4003163953Srrs } 4004163953Srrs#endif /* SCTP_DEBUG */ 4005163953Srrs SCTP_STAT_INCR(sctps_recvheartbeat); 4006163953Srrs sctp_send_heartbeat_ack(stcb, m, *offset, chk_length, 4007163953Srrs *netp); 4008163953Srrs 4009163953Srrs /* He's alive so give him credit */ 4010163953Srrs stcb->asoc.overall_error_count = 0; 4011163953Srrs break; 4012163953Srrs case SCTP_HEARTBEAT_ACK: 4013163953Srrs#ifdef SCTP_DEBUG 4014163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4015163953Srrs printf("SCTP_HEARTBEAT-ACK\n"); 4016163953Srrs } 4017163953Srrs#endif /* SCTP_DEBUG */ 4018169208Srrs if (chk_length != sizeof(struct sctp_heartbeat_chunk)) { 4019169208Srrs /* Its not ours */ 4020169352Srrs *offset = length; 4021169295Srrs if (locked_tcb) 4022169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4023169208Srrs return (NULL); 4024169208Srrs } 4025163953Srrs /* He's alive so give him credit */ 4026163953Srrs stcb->asoc.overall_error_count = 0; 4027163953Srrs SCTP_STAT_INCR(sctps_recvheartbeatack); 4028163953Srrs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4029163953Srrs stcb, *netp); 4030163953Srrs break; 4031163953Srrs case SCTP_ABORT_ASSOCIATION: 4032163953Srrs#ifdef SCTP_DEBUG 4033163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4034163953Srrs printf("SCTP_ABORT\n"); 4035163953Srrs } 4036163953Srrs#endif /* SCTP_DEBUG */ 4037163953Srrs sctp_handle_abort((struct sctp_abort_chunk *)ch, 4038163953Srrs stcb, *netp); 4039163953Srrs *offset = length; 4040163953Srrs return (NULL); 4041163953Srrs break; 4042163953Srrs case SCTP_SHUTDOWN: 4043163953Srrs#ifdef SCTP_DEBUG 4044163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4045163953Srrs printf("SCTP_SHUTDOWN\n"); 4046163953Srrs } 4047163953Srrs#endif /* SCTP_DEBUG */ 4048169208Srrs if (chk_length != sizeof(struct sctp_shutdown_chunk)) { 4049169208Srrs *offset = length; 4050169295Srrs if (locked_tcb) 4051169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4052169208Srrs return (NULL); 4053169208Srrs 4054169208Srrs } { 4055163953Srrs int abort_flag = 0; 4056163953Srrs 4057163953Srrs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4058163953Srrs stcb, *netp, &abort_flag); 4059163953Srrs if (abort_flag) { 4060163953Srrs *offset = length; 4061163953Srrs return (NULL); 4062163953Srrs } 4063163953Srrs } 4064163953Srrs break; 4065163953Srrs case SCTP_SHUTDOWN_ACK: 4066163953Srrs#ifdef SCTP_DEBUG 4067163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4068163953Srrs printf("SCTP_SHUTDOWN-ACK\n"); 4069163953Srrs } 4070163953Srrs#endif /* SCTP_DEBUG */ 4071163953Srrs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4072163953Srrs *offset = length; 4073163953Srrs return (NULL); 4074163953Srrs break; 4075163953Srrs case SCTP_OPERATION_ERROR: 4076163953Srrs#ifdef SCTP_DEBUG 4077163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4078163953Srrs printf("SCTP_OP-ERR\n"); 4079163953Srrs } 4080163953Srrs#endif /* SCTP_DEBUG */ 4081163953Srrs if (sctp_handle_error(ch, stcb, *netp) < 0) { 4082163953Srrs *offset = length; 4083163953Srrs return (NULL); 4084163953Srrs } 4085163953Srrs break; 4086163953Srrs case SCTP_COOKIE_ECHO: 4087163953Srrs#ifdef SCTP_DEBUG 4088163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4089163953Srrs printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb); 4090163953Srrs } 4091163953Srrs#endif /* SCTP_DEBUG */ 4092163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4093163953Srrs ; 4094163953Srrs } else { 4095163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) && 4096163953Srrs (stcb == NULL)) { 4097163953Srrs /* We are not interested anymore */ 4098163953Srrs *offset = length; 4099163953Srrs return (NULL); 4100163953Srrs } 4101163953Srrs } 4102163953Srrs /* 4103163953Srrs * First are we accepting? We do this again here 4104163953Srrs * since it is possible that a previous endpoint WAS 4105163953Srrs * listening responded to a INIT-ACK and then 4106163953Srrs * closed. We opened and bound.. and are now no 4107163953Srrs * longer listening. 4108163953Srrs */ 4109163953Srrs if (inp->sctp_socket->so_qlimit == 0) { 4110163953Srrs if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4111163953Srrs /* 4112163953Srrs * special case, is this a retran'd 4113163953Srrs * COOKIE-ECHO or a restarting assoc 4114163953Srrs * that is a peeled off or 4115163953Srrs * one-to-one style socket. 4116163953Srrs */ 4117163953Srrs goto process_cookie_anyway; 4118163953Srrs } 4119169352Srrs sctp_abort_association(inp, stcb, m, iphlen, 4120169352Srrs sh, NULL, vrf_id, 4121169352Srrs table_id); 4122163953Srrs *offset = length; 4123163953Srrs return (NULL); 4124163953Srrs } else if (inp->sctp_socket->so_qlimit) { 4125163953Srrs /* we are accepting so check limits like TCP */ 4126163953Srrs if (inp->sctp_socket->so_qlen > 4127163953Srrs inp->sctp_socket->so_qlimit) { 4128163953Srrs /* no space */ 4129163953Srrs struct mbuf *oper; 4130163953Srrs struct sctp_paramhdr *phdr; 4131163953Srrs 4132163953Srrs if (sctp_abort_if_one_2_one_hits_limit) { 4133163953Srrs oper = NULL; 4134163953Srrs oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4135165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4136163953Srrs if (oper) { 4137165647Srrs SCTP_BUF_LEN(oper) = 4138163953Srrs sizeof(struct sctp_paramhdr); 4139163953Srrs phdr = mtod(oper, 4140163953Srrs struct sctp_paramhdr *); 4141163953Srrs phdr->param_type = 4142163953Srrs htons(SCTP_CAUSE_OUT_OF_RESC); 4143163953Srrs phdr->param_length = 4144163953Srrs htons(sizeof(struct sctp_paramhdr)); 4145163953Srrs } 4146163953Srrs sctp_abort_association(inp, stcb, m, 4147169352Srrs iphlen, sh, oper, vrf_id, table_id); 4148163953Srrs } 4149163953Srrs *offset = length; 4150163953Srrs return (NULL); 4151163953Srrs } 4152163953Srrs } 4153163953Srrs process_cookie_anyway: 4154163953Srrs { 4155163953Srrs struct mbuf *ret_buf; 4156165220Srrs struct sctp_inpcb *linp; 4157163953Srrs 4158165220Srrs if (stcb) 4159165220Srrs linp = NULL; 4160165220Srrs else 4161165220Srrs linp = inp; 4162165220Srrs 4163165220Srrs if (linp) 4164165220Srrs SCTP_ASOC_CREATE_LOCK(linp); 4165163953Srrs ret_buf = 4166163953Srrs sctp_handle_cookie_echo(m, iphlen, 4167163953Srrs *offset, sh, 4168163953Srrs (struct sctp_cookie_echo_chunk *)ch, 4169163953Srrs &inp, &stcb, netp, 4170163953Srrs auth_skipped, 4171163953Srrs auth_offset, 4172165220Srrs auth_len, 4173169352Srrs &locked_tcb, 4174169352Srrs vrf_id, 4175169352Srrs table_id); 4176165220Srrs if (linp) 4177165220Srrs SCTP_ASOC_CREATE_UNLOCK(linp); 4178163953Srrs if (ret_buf == NULL) { 4179163953Srrs if (locked_tcb) { 4180163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4181163953Srrs } 4182163953Srrs#ifdef SCTP_DEBUG 4183163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4184163953Srrs printf("GAK, null buffer\n"); 4185163953Srrs } 4186163953Srrs#endif /* SCTP_DEBUG */ 4187163953Srrs auth_skipped = 0; 4188163953Srrs *offset = length; 4189163953Srrs return (NULL); 4190163953Srrs } 4191163953Srrs /* if AUTH skipped, see if it verified... */ 4192163953Srrs if (auth_skipped) { 4193163953Srrs got_auth = 1; 4194163953Srrs auth_skipped = 0; 4195163953Srrs } 4196163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 4197163953Srrs /* 4198163953Srrs * Restart the timer if we have 4199163953Srrs * pending data 4200163953Srrs */ 4201163953Srrs struct sctp_tmit_chunk *chk; 4202163953Srrs 4203163953Srrs chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 4204163953Srrs if (chk) { 4205163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4206163953Srrs stcb->sctp_ep, stcb, 4207163953Srrs chk->whoTo); 4208163953Srrs } 4209163953Srrs } 4210163953Srrs } 4211163953Srrs break; 4212163953Srrs case SCTP_COOKIE_ACK: 4213163953Srrs#ifdef SCTP_DEBUG 4214163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4215163953Srrs printf("SCTP_COOKIE-ACK\n"); 4216163953Srrs } 4217163953Srrs#endif /* SCTP_DEBUG */ 4218169352Srrs if (chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 4219169352Srrs if (locked_tcb) 4220169352Srrs SCTP_TCB_UNLOCK(locked_tcb); 4221169352Srrs return (NULL); 4222169352Srrs } 4223163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4224163953Srrs /* We are not interested anymore */ 4225163953Srrs if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4226163953Srrs ; 4227163953Srrs } else { 4228165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4229163953Srrs *offset = length; 4230163953Srrs return (NULL); 4231163953Srrs } 4232163953Srrs } 4233163953Srrs /* He's alive so give him credit */ 4234163953Srrs stcb->asoc.overall_error_count = 0; 4235163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 4236163953Srrs break; 4237163953Srrs case SCTP_ECN_ECHO: 4238163953Srrs#ifdef SCTP_DEBUG 4239163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4240163953Srrs printf("SCTP_ECN-ECHO\n"); 4241163953Srrs } 4242163953Srrs#endif /* SCTP_DEBUG */ 4243163953Srrs /* He's alive so give him credit */ 4244169208Srrs if (chk_length != sizeof(struct sctp_ecne_chunk)) { 4245169208Srrs /* Its not ours */ 4246169295Srrs if (locked_tcb) 4247169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4248169208Srrs *offset = length; 4249169208Srrs return (NULL); 4250169208Srrs } 4251163953Srrs stcb->asoc.overall_error_count = 0; 4252163953Srrs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 4253163953Srrs stcb); 4254163953Srrs break; 4255163953Srrs case SCTP_ECN_CWR: 4256163953Srrs#ifdef SCTP_DEBUG 4257163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4258163953Srrs printf("SCTP_ECN-CWR\n"); 4259163953Srrs } 4260163953Srrs#endif /* SCTP_DEBUG */ 4261163953Srrs /* He's alive so give him credit */ 4262169208Srrs if (chk_length != sizeof(struct sctp_cwr_chunk)) { 4263169208Srrs /* Its not ours */ 4264169295Srrs if (locked_tcb) 4265169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4266169208Srrs *offset = length; 4267169208Srrs return (NULL); 4268169208Srrs } 4269163953Srrs stcb->asoc.overall_error_count = 0; 4270163953Srrs 4271163953Srrs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb); 4272163953Srrs break; 4273163953Srrs case SCTP_SHUTDOWN_COMPLETE: 4274163953Srrs#ifdef SCTP_DEBUG 4275163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4276163953Srrs printf("SCTP_SHUTDOWN-COMPLETE\n"); 4277163953Srrs } 4278163953Srrs#endif /* SCTP_DEBUG */ 4279163953Srrs /* must be first and only chunk */ 4280163953Srrs if ((num_chunks > 1) || 4281163953Srrs (length - *offset > SCTP_SIZE32(chk_length))) { 4282163953Srrs *offset = length; 4283163953Srrs if (locked_tcb) 4284163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4285163953Srrs 4286163953Srrs return (NULL); 4287163953Srrs } 4288163953Srrs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 4289163953Srrs stcb, *netp); 4290163953Srrs *offset = length; 4291163953Srrs return (NULL); 4292163953Srrs break; 4293163953Srrs case SCTP_ASCONF: 4294163953Srrs#ifdef SCTP_DEBUG 4295163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4296163953Srrs printf("SCTP_ASCONF\n"); 4297163953Srrs } 4298163953Srrs#endif /* SCTP_DEBUG */ 4299163953Srrs /* He's alive so give him credit */ 4300163953Srrs stcb->asoc.overall_error_count = 0; 4301163953Srrs 4302163953Srrs sctp_handle_asconf(m, *offset, 4303163953Srrs (struct sctp_asconf_chunk *)ch, stcb); 4304163953Srrs break; 4305163953Srrs case SCTP_ASCONF_ACK: 4306163953Srrs#ifdef SCTP_DEBUG 4307163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4308163953Srrs printf("SCTP_ASCONF-ACK\n"); 4309163953Srrs } 4310163953Srrs#endif /* SCTP_DEBUG */ 4311169208Srrs if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 4312169208Srrs /* Its not ours */ 4313169295Srrs if (locked_tcb) 4314169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4315169208Srrs *offset = length; 4316169208Srrs return (NULL); 4317169208Srrs } 4318163953Srrs /* He's alive so give him credit */ 4319163953Srrs stcb->asoc.overall_error_count = 0; 4320163953Srrs 4321163953Srrs sctp_handle_asconf_ack(m, *offset, 4322163953Srrs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp); 4323163953Srrs break; 4324163953Srrs case SCTP_FORWARD_CUM_TSN: 4325163953Srrs#ifdef SCTP_DEBUG 4326163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4327163953Srrs printf("SCTP_FWD-TSN\n"); 4328163953Srrs } 4329163953Srrs#endif /* SCTP_DEBUG */ 4330169208Srrs if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 4331169208Srrs /* Its not ours */ 4332169295Srrs if (locked_tcb) 4333169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4334169208Srrs *offset = length; 4335169208Srrs return (NULL); 4336169208Srrs } 4337163953Srrs /* He's alive so give him credit */ 4338163953Srrs { 4339163953Srrs int abort_flag = 0; 4340163953Srrs 4341163953Srrs stcb->asoc.overall_error_count = 0; 4342163953Srrs *fwd_tsn_seen = 1; 4343163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4344163953Srrs /* We are not interested anymore */ 4345165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28); 4346163953Srrs *offset = length; 4347163953Srrs return (NULL); 4348163953Srrs } 4349163953Srrs sctp_handle_forward_tsn(stcb, 4350163953Srrs (struct sctp_forward_tsn_chunk *)ch, &abort_flag); 4351163953Srrs if (abort_flag) { 4352163953Srrs *offset = length; 4353163953Srrs return (NULL); 4354163953Srrs } else { 4355163953Srrs stcb->asoc.overall_error_count = 0; 4356163953Srrs } 4357163953Srrs 4358163953Srrs } 4359163953Srrs break; 4360163953Srrs case SCTP_STREAM_RESET: 4361163953Srrs#ifdef SCTP_DEBUG 4362163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4363163953Srrs printf("SCTP_STREAM_RESET\n"); 4364163953Srrs } 4365163953Srrs#endif /* SCTP_DEBUG */ 4366163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4367163953Srrs chk_length, chunk_buf); 4368169208Srrs if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) { 4369169208Srrs /* Its not ours */ 4370169295Srrs if (locked_tcb) 4371169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4372169208Srrs *offset = length; 4373169208Srrs return (NULL); 4374169208Srrs } 4375163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4376163953Srrs /* We are not interested anymore */ 4377165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4378163953Srrs *offset = length; 4379163953Srrs return (NULL); 4380163953Srrs } 4381163953Srrs if (stcb->asoc.peer_supports_strreset == 0) { 4382163953Srrs /* 4383163953Srrs * hmm, peer should have announced this, but 4384163953Srrs * we will turn it on since he is sending us 4385163953Srrs * a stream reset. 4386163953Srrs */ 4387163953Srrs stcb->asoc.peer_supports_strreset = 1; 4388163953Srrs } 4389163953Srrs if (sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_out_req *)ch)) { 4390163953Srrs /* stop processing */ 4391163953Srrs *offset = length; 4392163953Srrs return (NULL); 4393163953Srrs } 4394163953Srrs break; 4395163953Srrs case SCTP_PACKET_DROPPED: 4396163953Srrs#ifdef SCTP_DEBUG 4397163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4398163953Srrs printf("SCTP_PACKET_DROPPED\n"); 4399163953Srrs } 4400163953Srrs#endif /* SCTP_DEBUG */ 4401163953Srrs /* re-get it all please */ 4402169208Srrs if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 4403169208Srrs /* Its not ours */ 4404169295Srrs if (locked_tcb) 4405169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4406169208Srrs *offset = length; 4407169208Srrs return (NULL); 4408169208Srrs } 4409163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4410163953Srrs chk_length, chunk_buf); 4411163953Srrs 4412163953Srrs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 4413163953Srrs stcb, *netp); 4414163953Srrs 4415163953Srrs break; 4416163953Srrs 4417163953Srrs case SCTP_AUTHENTICATION: 4418163953Srrs#ifdef SCTP_DEBUG 4419163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4420163953Srrs printf("SCTP_AUTHENTICATION\n"); 4421163953Srrs } 4422163953Srrs#endif /* SCTP_DEBUG */ 4423163953Srrs if (sctp_auth_disable) 4424163953Srrs goto unknown_chunk; 4425163953Srrs 4426163953Srrs if (stcb == NULL) { 4427163953Srrs /* save the first AUTH for later processing */ 4428163953Srrs if (auth_skipped == 0) { 4429163953Srrs auth_offset = *offset; 4430163953Srrs auth_len = chk_length; 4431163953Srrs auth_skipped = 1; 4432163953Srrs } 4433163953Srrs /* skip this chunk (temporarily) */ 4434163953Srrs goto next_chunk; 4435163953Srrs } 4436169208Srrs if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 4437169208Srrs (chk_length > (sizeof(struct sctp_auth_chunk) + SCTP_AUTH_DIGEST_LEN_MAX))) { 4438169208Srrs /* Its not ours */ 4439169295Srrs if (locked_tcb) 4440169295Srrs SCTP_TCB_UNLOCK(locked_tcb); 4441169208Srrs *offset = length; 4442169208Srrs return (NULL); 4443169208Srrs } 4444163953Srrs if (got_auth == 1) { 4445163953Srrs /* skip this chunk... it's already auth'd */ 4446163953Srrs goto next_chunk; 4447163953Srrs } 4448163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4449163953Srrs chk_length, chunk_buf); 4450163953Srrs got_auth = 1; 4451163953Srrs if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 4452163953Srrs m, *offset)) { 4453163953Srrs /* auth HMAC failed so dump the packet */ 4454163953Srrs *offset = length; 4455163953Srrs return (stcb); 4456163953Srrs } else { 4457163953Srrs /* remaining chunks are HMAC checked */ 4458163953Srrs stcb->asoc.authenticated = 1; 4459163953Srrs } 4460163953Srrs break; 4461163953Srrs 4462163953Srrs default: 4463163953Srrs unknown_chunk: 4464163953Srrs /* it's an unknown chunk! */ 4465163953Srrs if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 4466163953Srrs struct mbuf *mm; 4467163953Srrs struct sctp_paramhdr *phd; 4468163953Srrs 4469163953Srrs mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 4470165647Srrs 0, M_DONTWAIT, 1, MT_DATA); 4471163953Srrs if (mm) { 4472163953Srrs phd = mtod(mm, struct sctp_paramhdr *); 4473163953Srrs /* 4474163953Srrs * We cheat and use param type since 4475163953Srrs * we did not bother to define a 4476163953Srrs * error cause struct. They are the 4477163953Srrs * same basic format with different 4478163953Srrs * names. 4479163953Srrs */ 4480163953Srrs phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 4481163953Srrs phd->param_length = htons(chk_length + sizeof(*phd)); 4482165647Srrs SCTP_BUF_LEN(mm) = sizeof(*phd); 4483166023Srrs SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length), 4484163953Srrs M_DONTWAIT); 4485165647Srrs if (SCTP_BUF_NEXT(mm)) { 4486163953Srrs sctp_queue_op_err(stcb, mm); 4487163953Srrs } else { 4488163953Srrs sctp_m_freem(mm); 4489163953Srrs } 4490163953Srrs } 4491163953Srrs } 4492163953Srrs if ((ch->chunk_type & 0x80) == 0) { 4493163953Srrs /* discard this packet */ 4494163953Srrs *offset = length; 4495163953Srrs return (stcb); 4496163953Srrs } /* else skip this bad chunk and continue... */ 4497163953Srrs break; 4498163953Srrs } /* switch (ch->chunk_type) */ 4499163953Srrs 4500163953Srrs 4501163953Srrsnext_chunk: 4502163953Srrs /* get the next chunk */ 4503163953Srrs *offset += SCTP_SIZE32(chk_length); 4504163953Srrs if (*offset >= length) { 4505163953Srrs /* no more data left in the mbuf chain */ 4506163953Srrs break; 4507163953Srrs } 4508163953Srrs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4509163953Srrs sizeof(struct sctp_chunkhdr), chunk_buf); 4510163953Srrs if (ch == NULL) { 4511163953Srrs if (locked_tcb) 4512163953Srrs SCTP_TCB_UNLOCK(locked_tcb); 4513163953Srrs *offset = length; 4514163953Srrs return (NULL); 4515163953Srrs } 4516163953Srrs } /* while */ 4517163953Srrs return (stcb); 4518163953Srrs} 4519163953Srrs 4520163953Srrs 4521163953Srrs/* 4522163953Srrs * Process the ECN bits we have something set so we must look to see if it is 4523163953Srrs * ECN(0) or ECN(1) or CE 4524163953Srrs */ 4525163953Srrsstatic __inline void 4526163953Srrssctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net, 4527163953Srrs uint8_t ecn_bits) 4528163953Srrs{ 4529163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4530163953Srrs ; 4531163953Srrs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) { 4532163953Srrs /* 4533163953Srrs * we only add to the nonce sum for ECT1, ECT0 does not 4534163953Srrs * change the NS bit (that we have yet to find a way to send 4535163953Srrs * it yet). 4536163953Srrs */ 4537163953Srrs 4538163953Srrs /* ECN Nonce stuff */ 4539163953Srrs stcb->asoc.receiver_nonce_sum++; 4540163953Srrs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM; 4541163953Srrs 4542163953Srrs /* 4543163953Srrs * Drag up the last_echo point if cumack is larger since we 4544163953Srrs * don't want the point falling way behind by more than 4545163953Srrs * 2^^31 and then having it be incorrect. 4546163953Srrs */ 4547163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4548163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4549163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4550163953Srrs } 4551163953Srrs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) { 4552163953Srrs /* 4553163953Srrs * Drag up the last_echo point if cumack is larger since we 4554163953Srrs * don't want the point falling way behind by more than 4555163953Srrs * 2^^31 and then having it be incorrect. 4556163953Srrs */ 4557163953Srrs if (compare_with_wrap(stcb->asoc.cumulative_tsn, 4558163953Srrs stcb->asoc.last_echo_tsn, MAX_TSN)) { 4559163953Srrs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn; 4560163953Srrs } 4561163953Srrs } 4562163953Srrs} 4563163953Srrs 4564163953Srrsstatic __inline void 4565163953Srrssctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net, 4566163953Srrs uint32_t high_tsn, uint8_t ecn_bits) 4567163953Srrs{ 4568163953Srrs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) { 4569163953Srrs /* 4570163953Srrs * we possibly must notify the sender that a congestion 4571163953Srrs * window reduction is in order. We do this by adding a ECNE 4572163953Srrs * chunk to the output chunk queue. The incoming CWR will 4573163953Srrs * remove this chunk. 4574163953Srrs */ 4575163953Srrs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn, 4576163953Srrs MAX_TSN)) { 4577163953Srrs /* Yep, we need to add a ECNE */ 4578163953Srrs sctp_send_ecn_echo(stcb, net, high_tsn); 4579163953Srrs stcb->asoc.last_echo_tsn = high_tsn; 4580163953Srrs } 4581163953Srrs } 4582163953Srrs} 4583163953Srrs 4584163953Srrs/* 4585163953Srrs * common input chunk processing (v4 and v6) 4586163953Srrs */ 4587163953Srrsint 4588163953Srrssctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 4589163953Srrs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 4590163953Srrs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 4591169352Srrs uint8_t ecn_bits, uint32_t vrf_id, uint32_t table_id) 4592163953Srrs{ 4593163953Srrs /* 4594163953Srrs * Control chunk processing 4595163953Srrs */ 4596163953Srrs uint32_t high_tsn; 4597163953Srrs int fwd_tsn_seen = 0, data_processed = 0; 4598163953Srrs struct mbuf *m = *mm; 4599163953Srrs int abort_flag = 0; 4600163953Srrs int un_sent; 4601163953Srrs 4602163953Srrs SCTP_STAT_INCR(sctps_recvdatagrams); 4603163953Srrs#ifdef SCTP_AUDITING_ENABLED 4604163953Srrs sctp_audit_log(0xE0, 1); 4605163953Srrs sctp_auditing(0, inp, stcb, net); 4606163953Srrs#endif 4607163953Srrs 4608163953Srrs#ifdef SCTP_DEBUG 4609163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4610163996Srrs printf("Ok, Common input processing called, m:%p iphlen:%d offset:%d\n", 4611163996Srrs m, iphlen, offset); 4612163953Srrs } 4613163953Srrs#endif /* SCTP_DEBUG */ 4614163953Srrs 4615163953Srrs if (stcb) { 4616163953Srrs /* always clear this before beginning a packet */ 4617163953Srrs stcb->asoc.authenticated = 0; 4618163953Srrs stcb->asoc.seen_a_sack_this_pkt = 0; 4619163953Srrs } 4620163953Srrs if (IS_SCTP_CONTROL(ch)) { 4621163953Srrs /* process the control portion of the SCTP packet */ 4622163953Srrs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 4623169352Srrs inp, stcb, &net, &fwd_tsn_seen, vrf_id, table_id); 4624163953Srrs if (stcb) { 4625163953Srrs /* 4626163953Srrs * This covers us if the cookie-echo was there and 4627163953Srrs * it changes our INP. 4628163953Srrs */ 4629163953Srrs inp = stcb->sctp_ep; 4630163953Srrs } 4631163953Srrs } else { 4632163953Srrs /* 4633163953Srrs * no control chunks, so pre-process DATA chunks (these 4634163953Srrs * checks are taken care of by control processing) 4635163953Srrs */ 4636163953Srrs 4637163953Srrs /* 4638163953Srrs * if DATA only packet, and auth is required, then punt... 4639163953Srrs * can't have authenticated without any AUTH (control) 4640163953Srrs * chunks 4641163953Srrs */ 4642163953Srrs if ((stcb != NULL) && !sctp_auth_disable && 4643163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4644163953Srrs stcb->asoc.local_auth_chunks)) { 4645163953Srrs /* "silently" ignore */ 4646163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4647163953Srrs SCTP_TCB_UNLOCK(stcb); 4648163953Srrs return (1); 4649163953Srrs } 4650163953Srrs if (stcb == NULL) { 4651163953Srrs /* out of the blue DATA chunk */ 4652169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4653169352Srrs vrf_id, table_id); 4654163953Srrs return (1); 4655163953Srrs } 4656163953Srrs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 4657163953Srrs /* v_tag mismatch! */ 4658163953Srrs SCTP_STAT_INCR(sctps_badvtag); 4659163953Srrs SCTP_TCB_UNLOCK(stcb); 4660163953Srrs return (1); 4661163953Srrs } 4662163953Srrs } 4663163953Srrs 4664163953Srrs if (stcb == NULL) { 4665163953Srrs /* 4666163953Srrs * no valid TCB for this packet, or we found it's a bad 4667163953Srrs * packet while processing control, or we're done with this 4668163953Srrs * packet (done or skip rest of data), so we drop it... 4669163953Srrs */ 4670163953Srrs return (1); 4671163953Srrs } 4672163953Srrs /* 4673163953Srrs * DATA chunk processing 4674163953Srrs */ 4675163953Srrs /* plow through the data chunks while length > offset */ 4676163953Srrs 4677163953Srrs /* 4678163953Srrs * Rest should be DATA only. Check authentication state if AUTH for 4679163953Srrs * DATA is required. 4680163953Srrs */ 4681165220Srrs if ((length > offset) && (stcb != NULL) && !sctp_auth_disable && 4682163953Srrs sctp_auth_is_required_chunk(SCTP_DATA, 4683163953Srrs stcb->asoc.local_auth_chunks) && 4684163953Srrs !stcb->asoc.authenticated) { 4685163953Srrs /* "silently" ignore */ 4686163953Srrs SCTP_STAT_INCR(sctps_recvauthmissing); 4687163953Srrs#ifdef SCTP_DEBUG 4688163953Srrs if (sctp_debug_on & SCTP_DEBUG_AUTH1) 4689163953Srrs printf("Data chunk requires AUTH, skipped\n"); 4690163953Srrs#endif 4691165220Srrs goto trigger_send; 4692163953Srrs } 4693163953Srrs if (length > offset) { 4694163953Srrs int retval; 4695163953Srrs 4696163953Srrs /* 4697163953Srrs * First check to make sure our state is correct. We would 4698163953Srrs * not get here unless we really did have a tag, so we don't 4699163953Srrs * abort if this happens, just dump the chunk silently. 4700163953Srrs */ 4701163953Srrs switch (SCTP_GET_STATE(&stcb->asoc)) { 4702163953Srrs case SCTP_STATE_COOKIE_ECHOED: 4703163953Srrs /* 4704163953Srrs * we consider data with valid tags in this state 4705163953Srrs * shows us the cookie-ack was lost. Imply it was 4706163953Srrs * there. 4707163953Srrs */ 4708163953Srrs stcb->asoc.overall_error_count = 0; 4709163953Srrs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 4710163953Srrs break; 4711163953Srrs case SCTP_STATE_COOKIE_WAIT: 4712163953Srrs /* 4713163953Srrs * We consider OOTB any data sent during asoc setup. 4714163953Srrs */ 4715169352Srrs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 4716169352Srrs vrf_id, table_id); 4717163953Srrs SCTP_TCB_UNLOCK(stcb); 4718163953Srrs return (1); 4719163953Srrs break; 4720163953Srrs case SCTP_STATE_EMPTY: /* should not happen */ 4721163953Srrs case SCTP_STATE_INUSE: /* should not happen */ 4722163953Srrs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 4723163953Srrs case SCTP_STATE_SHUTDOWN_ACK_SENT: 4724163953Srrs default: 4725163953Srrs SCTP_TCB_UNLOCK(stcb); 4726163953Srrs return (1); 4727163953Srrs break; 4728163953Srrs case SCTP_STATE_OPEN: 4729163953Srrs case SCTP_STATE_SHUTDOWN_SENT: 4730163953Srrs break; 4731163953Srrs } 4732163953Srrs /* take care of ECN, part 1. */ 4733163953Srrs if (stcb->asoc.ecn_allowed && 4734163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4735163953Srrs sctp_process_ecn_marked_a(stcb, net, ecn_bits); 4736163953Srrs } 4737163953Srrs /* plow through the data chunks while length > offset */ 4738163953Srrs retval = sctp_process_data(mm, iphlen, &offset, length, sh, 4739163953Srrs inp, stcb, net, &high_tsn); 4740163953Srrs if (retval == 2) { 4741163953Srrs /* 4742163953Srrs * The association aborted, NO UNLOCK needed since 4743163953Srrs * the association is destroyed. 4744163953Srrs */ 4745163953Srrs return (0); 4746163953Srrs } 4747163953Srrs data_processed = 1; 4748163953Srrs if (retval == 0) { 4749163953Srrs /* take care of ecn part 2. */ 4750163953Srrs if (stcb->asoc.ecn_allowed && 4751163953Srrs (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) { 4752163953Srrs sctp_process_ecn_marked_b(stcb, net, high_tsn, 4753163953Srrs ecn_bits); 4754163953Srrs } 4755163953Srrs } 4756163953Srrs /* 4757163953Srrs * Anything important needs to have been m_copy'ed in 4758163953Srrs * process_data 4759163953Srrs */ 4760163953Srrs } 4761163953Srrs if ((data_processed == 0) && (fwd_tsn_seen)) { 4762163953Srrs int was_a_gap = 0; 4763163953Srrs 4764163953Srrs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map, 4765163953Srrs stcb->asoc.cumulative_tsn, MAX_TSN)) { 4766163953Srrs /* there was a gap before this data was processed */ 4767163953Srrs was_a_gap = 1; 4768163953Srrs } 4769163953Srrs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag); 4770163953Srrs if (abort_flag) { 4771163953Srrs /* Again, we aborted so NO UNLOCK needed */ 4772163953Srrs return (0); 4773163953Srrs } 4774163953Srrs } 4775163953Srrs /* trigger send of any chunks in queue... */ 4776165220Srrstrigger_send: 4777163953Srrs#ifdef SCTP_AUDITING_ENABLED 4778163953Srrs sctp_audit_log(0xE0, 2); 4779163953Srrs sctp_auditing(1, inp, stcb, net); 4780163953Srrs#endif 4781163953Srrs#ifdef SCTP_DEBUG 4782163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4783163953Srrs printf("Check for chunk output prw:%d tqe:%d tf=%d\n", 4784163953Srrs stcb->asoc.peers_rwnd, 4785163953Srrs TAILQ_EMPTY(&stcb->asoc.control_send_queue), 4786163953Srrs stcb->asoc.total_flight); 4787163953Srrs } 4788163953Srrs#endif 4789163953Srrs un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 4790163953Srrs 4791163953Srrs if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) || 4792163953Srrs ((un_sent) && 4793163953Srrs (stcb->asoc.peers_rwnd > 0 || 4794163953Srrs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 4795163953Srrs#ifdef SCTP_DEBUG 4796163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4797163953Srrs printf("Calling chunk OUTPUT\n"); 4798163953Srrs } 4799163953Srrs#endif 4800163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC); 4801163953Srrs#ifdef SCTP_DEBUG 4802163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT3) { 4803163953Srrs printf("chunk OUTPUT returns\n"); 4804163953Srrs } 4805163953Srrs#endif 4806163953Srrs } 4807163953Srrs#ifdef SCTP_AUDITING_ENABLED 4808163953Srrs sctp_audit_log(0xE0, 3); 4809163953Srrs sctp_auditing(2, inp, stcb, net); 4810163953Srrs#endif 4811163953Srrs SCTP_TCB_UNLOCK(stcb); 4812163953Srrs return (0); 4813163953Srrs} 4814163953Srrs 4815163953Srrs 4816163953Srrs 4817163953Srrsvoid 4818165647Srrssctp_input(i_pak, off) 4819165647Srrs struct mbuf *i_pak; 4820163953Srrs int off; 4821163953Srrs 4822163953Srrs{ 4823163953Srrs#ifdef SCTP_MBUF_LOGGING 4824163953Srrs struct mbuf *mat; 4825163953Srrs 4826163953Srrs#endif 4827165647Srrs struct mbuf *m; 4828163953Srrs int iphlen; 4829169352Srrs uint32_t vrf_id = 0, table_id = 0; 4830163953Srrs uint8_t ecn_bits; 4831163953Srrs struct ip *ip; 4832163953Srrs struct sctphdr *sh; 4833163953Srrs struct sctp_inpcb *inp = NULL; 4834163953Srrs 4835163953Srrs uint32_t check, calc_check; 4836163953Srrs struct sctp_nets *net; 4837163953Srrs struct sctp_tcb *stcb = NULL; 4838163953Srrs struct sctp_chunkhdr *ch; 4839163953Srrs int refcount_up = 0; 4840163953Srrs int length, mlen, offset; 4841163953Srrs 4842169352Srrs 4843169352Srrs if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 4844169352Srrs SCTP_RELEASE_PKT(i_pak); 4845169352Srrs return; 4846169352Srrs } 4847169352Srrs if (SCTP_GET_PKT_TABLEID(i_pak, table_id)) { 4848169352Srrs SCTP_RELEASE_PKT(i_pak); 4849169352Srrs return; 4850169352Srrs } 4851168299Srrs mlen = SCTP_HEADER_LEN(i_pak); 4852163953Srrs iphlen = off; 4853165647Srrs m = SCTP_HEADER_TO_CHAIN(i_pak); 4854163953Srrs net = NULL; 4855163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 4856163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 4857163953Srrs 4858163953Srrs#ifdef SCTP_MBUF_LOGGING 4859163953Srrs /* Log in any input mbufs */ 4860163953Srrs mat = m; 4861163953Srrs while (mat) { 4862165647Srrs if (SCTP_BUF_IS_EXTENDED(mat)) { 4863163953Srrs sctp_log_mb(mat, SCTP_MBUF_INPUT); 4864163953Srrs } 4865165647Srrs mat = SCTP_BUF_NEXT(mat); 4866163953Srrs } 4867163953Srrs#endif 4868168124Srrs 4869163953Srrs /* 4870163953Srrs * Get IP, SCTP, and first chunk header together in first mbuf. 4871163953Srrs */ 4872163953Srrs ip = mtod(m, struct ip *); 4873163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 4874165647Srrs if (SCTP_BUF_LEN(m) < offset) { 4875163953Srrs if ((m = m_pullup(m, offset)) == 0) { 4876163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4877163953Srrs return; 4878163953Srrs } 4879163953Srrs ip = mtod(m, struct ip *); 4880163953Srrs } 4881163953Srrs sh = (struct sctphdr *)((caddr_t)ip + iphlen); 4882163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 4883163953Srrs 4884163953Srrs /* SCTP does not allow broadcasts or multicasts */ 4885163953Srrs if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 4886163953Srrs goto bad; 4887163953Srrs } 4888168124Srrs if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 4889163953Srrs /* 4890163953Srrs * We only look at broadcast if its a front state, All 4891163953Srrs * others we will not have a tcb for anyway. 4892163953Srrs */ 4893163953Srrs goto bad; 4894163953Srrs } 4895163953Srrs /* validate SCTP checksum */ 4896168124Srrs if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) { 4897163953Srrs /* 4898163953Srrs * we do NOT validate things from the loopback if the sysctl 4899163953Srrs * is set to 1. 4900163953Srrs */ 4901163953Srrs check = sh->checksum; /* save incoming checksum */ 4902163953Srrs if ((check == 0) && (sctp_no_csum_on_loopback)) { 4903163953Srrs /* 4904163953Srrs * special hook for where we got a local address 4905163953Srrs * somehow routed across a non IFT_LOOP type 4906163953Srrs * interface 4907163953Srrs */ 4908163953Srrs if (ip->ip_src.s_addr == ip->ip_dst.s_addr) 4909163953Srrs goto sctp_skip_csum_4; 4910163953Srrs } 4911163953Srrs sh->checksum = 0; /* prepare for calc */ 4912163953Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 4913163953Srrs if (calc_check != check) { 4914163953Srrs#ifdef SCTP_DEBUG 4915163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4916163996Srrs printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 4917163996Srrs calc_check, check, m, mlen, iphlen); 4918163953Srrs } 4919163953Srrs#endif 4920163953Srrs 4921163953Srrs stcb = sctp_findassociation_addr(m, iphlen, 4922163953Srrs offset - sizeof(*ch), 4923168299Srrs sh, ch, &inp, &net, vrf_id); 4924163953Srrs if ((inp) && (stcb)) { 4925163953Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 4926163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR); 4927163953Srrs } else if ((inp != NULL) && (stcb == NULL)) { 4928163953Srrs refcount_up = 1; 4929163953Srrs } 4930163953Srrs SCTP_STAT_INCR(sctps_badsum); 4931163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 4932163953Srrs goto bad; 4933163953Srrs } 4934163953Srrs sh->checksum = calc_check; 4935168299Srrs } 4936163953Srrssctp_skip_csum_4: 4937168124Srrs /* destination port of 0 is illegal, based on RFC2960. */ 4938168124Srrs if (sh->dest_port == 0) { 4939168124Srrs SCTP_STAT_INCR(sctps_hdrops); 4940168124Srrs goto bad; 4941168124Srrs } 4942163953Srrs /* validate mbuf chain length with IP payload length */ 4943163953Srrs if (mlen < (ip->ip_len - iphlen)) { 4944163953Srrs SCTP_STAT_INCR(sctps_hdrops); 4945163953Srrs goto bad; 4946163953Srrs } 4947163953Srrs /* 4948163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 4949163953Srrs * IP/SCTP/first chunk header... 4950163953Srrs */ 4951163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 4952168299Srrs sh, ch, &inp, &net, vrf_id); 4953163953Srrs /* inp's ref-count increased && stcb locked */ 4954163953Srrs if (inp == NULL) { 4955163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 4956163953Srrs 4957163953Srrs SCTP_STAT_INCR(sctps_noport); 4958163953Srrs#ifdef ICMP_BANDLIM 4959163953Srrs /* 4960163953Srrs * we use the bandwidth limiting to protect against sending 4961163953Srrs * too many ABORTS all at once. In this case these count the 4962163953Srrs * same as an ICMP message. 4963163953Srrs */ 4964163953Srrs if (badport_bandlim(0) < 0) 4965163953Srrs goto bad; 4966163953Srrs#endif /* ICMP_BANDLIM */ 4967163953Srrs#ifdef SCTP_DEBUG 4968163953Srrs if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 4969163953Srrs printf("Sending a ABORT from packet entry!\n"); 4970163953Srrs } 4971163953Srrs#endif 4972163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 4973163953Srrs /* 4974163953Srrs * we do a trick here to get the INIT tag, dig in 4975163953Srrs * and get the tag from the INIT and put it in the 4976163953Srrs * common header. 4977163953Srrs */ 4978163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4979163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 4980163953Srrs (uint8_t *) & chunk_buf); 4981163953Srrs if (init_chk != NULL) 4982163953Srrs sh->v_tag = init_chk->init.initiate_tag; 4983163953Srrs } 4984165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4985169352Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, 4986169352Srrs table_id); 4987165220Srrs goto bad; 4988165220Srrs } 4989165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 4990165220Srrs goto bad; 4991165220Srrs } 4992165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 4993169352Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, 4994169352Srrs table_id); 4995163953Srrs goto bad; 4996163953Srrs } else if (stcb == NULL) { 4997163953Srrs refcount_up = 1; 4998163953Srrs } 4999163953Srrs#ifdef IPSEC 5000163953Srrs /* 5001163953Srrs * I very much doubt any of the IPSEC stuff will work but I have no 5002163953Srrs * idea, so I will leave it in place. 5003163953Srrs */ 5004163953Srrs 5005163996Srrs if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 5006163953Srrs ipsecstat.in_polvio++; 5007163953Srrs SCTP_STAT_INCR(sctps_hdrops); 5008163953Srrs goto bad; 5009163953Srrs } 5010163953Srrs#endif /* IPSEC */ 5011163953Srrs 5012163953Srrs /* 5013163953Srrs * common chunk processing 5014163953Srrs */ 5015163953Srrs length = ip->ip_len + iphlen; 5016163953Srrs offset -= sizeof(struct sctp_chunkhdr); 5017163953Srrs 5018163953Srrs ecn_bits = ip->ip_tos; 5019163953Srrs 5020163953Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 5021169352Srrs inp, stcb, net, ecn_bits, vrf_id, 5022169352Srrs table_id); 5023163953Srrs /* inp's ref-count reduced && stcb unlocked */ 5024163953Srrs if (m) { 5025163953Srrs sctp_m_freem(m); 5026163953Srrs } 5027163953Srrs if ((inp) && (refcount_up)) { 5028163953Srrs /* reduce ref-count */ 5029163953Srrs SCTP_INP_WLOCK(inp); 5030163953Srrs SCTP_INP_DECR_REF(inp); 5031163953Srrs SCTP_INP_WUNLOCK(inp); 5032163953Srrs } 5033163953Srrs return; 5034163953Srrsbad: 5035163953Srrs if (stcb) 5036163953Srrs SCTP_TCB_UNLOCK(stcb); 5037163953Srrs 5038163953Srrs if ((inp) && (refcount_up)) { 5039163953Srrs /* reduce ref-count */ 5040163953Srrs SCTP_INP_WLOCK(inp); 5041163953Srrs SCTP_INP_DECR_REF(inp); 5042163953Srrs SCTP_INP_WUNLOCK(inp); 5043163953Srrs } 5044163953Srrs if (m) { 5045163953Srrs sctp_m_freem(m); 5046163953Srrs } 5047169208Srrs /* For BSD/MAC this does nothing */ 5048169352Srrs SCTP_DETACH_HEADER_FROM_CHAIN(i_pak); 5049169352Srrs SCTP_RELEASE_HEADER(i_pak); 5050163953Srrs return; 5051163953Srrs} 5052