sctp_input.c revision 235283
1186690Sobrien/*- 2186690Sobrien * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3186690Sobrien * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. 4186690Sobrien * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. 5186690Sobrien * 6186690Sobrien * Redistribution and use in source and binary forms, with or without 7186690Sobrien * modification, are permitted provided that the following conditions are met: 8186690Sobrien * 9186690Sobrien * a) Redistributions of source code must retain the above copyright notice, 10186690Sobrien * this list of conditions and the following disclaimer. 11186690Sobrien * 12186690Sobrien * b) Redistributions in binary form must reproduce the above copyright 13186690Sobrien * notice, this list of conditions and the following disclaimer in 14186690Sobrien * the documentation and/or other materials provided with the distribution. 15186690Sobrien * 16186690Sobrien * c) Neither the name of Cisco Systems, Inc. nor the names of its 17186690Sobrien * contributors may be used to endorse or promote products derived 18186690Sobrien * from this software without specific prior written permission. 19186690Sobrien * 20186690Sobrien * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21186690Sobrien * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22186690Sobrien * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23186690Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24186690Sobrien * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25186690Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26186690Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27186690Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28186690Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29186690Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30186690Sobrien * THE POSSIBILITY OF SUCH DAMAGE. 31186690Sobrien */ 32186690Sobrien 33186690Sobrien/* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */ 34186690Sobrien 35186690Sobrien#include <sys/cdefs.h> 36186690Sobrien__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 235283 2012-05-11 19:15:33Z tuexen $"); 37186690Sobrien 38186690Sobrien#include <netinet/sctp_os.h> 39186690Sobrien#include <netinet/sctp_var.h> 40186690Sobrien#include <netinet/sctp_sysctl.h> 41186690Sobrien#include <netinet/sctp_pcb.h> 42186690Sobrien#include <netinet/sctp_header.h> 43186690Sobrien#include <netinet/sctputil.h> 44186690Sobrien#include <netinet/sctp_output.h> 45186690Sobrien#include <netinet/sctp_input.h> 46186690Sobrien#include <netinet/sctp_auth.h> 47186690Sobrien#include <netinet/sctp_indata.h> 48186690Sobrien#include <netinet/sctp_asconf.h> 49186690Sobrien#include <netinet/sctp_bsd_addr.h> 50186690Sobrien#include <netinet/sctp_timer.h> 51186690Sobrien#include <netinet/sctp_crc32.h> 52186690Sobrien#include <netinet/udp.h> 53186690Sobrien#include <sys/smp.h> 54186690Sobrien 55186690Sobrien 56186690Sobrien 57186690Sobrienstatic void 58186690Sobriensctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 59186690Sobrien{ 60186690Sobrien struct sctp_nets *net; 61186690Sobrien 62186690Sobrien /* 63186690Sobrien * This now not only stops all cookie timers it also stops any INIT 64186690Sobrien * timers as well. This will make sure that the timers are stopped 65186690Sobrien * in all collision cases. 66186690Sobrien */ 67186690Sobrien SCTP_TCB_LOCK_ASSERT(stcb); 68186690Sobrien TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 69186690Sobrien if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 70186690Sobrien sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 71186690Sobrien stcb->sctp_ep, 72186690Sobrien stcb, 73186690Sobrien net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 74186690Sobrien } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 75186690Sobrien sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 76186690Sobrien stcb->sctp_ep, 77186690Sobrien stcb, 78186690Sobrien net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 79186690Sobrien } 80186690Sobrien } 81186690Sobrien} 82186690Sobrien 83186690Sobrien/* INIT handler */ 84186690Sobrienstatic void 85186690Sobriensctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh, 86186690Sobrien struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 87186690Sobrien int *abort_no_unlock, uint32_t vrf_id, uint16_t port) 88186690Sobrien{ 89186690Sobrien struct sctp_init *init; 90186690Sobrien struct mbuf *op_err; 91186690Sobrien 92186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 93186690Sobrien stcb); 94186690Sobrien if (stcb == NULL) { 95186690Sobrien SCTP_INP_RLOCK(inp); 96186690Sobrien } 97186690Sobrien /* validate length */ 98186690Sobrien if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { 99186690Sobrien op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 100186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 101186690Sobrien vrf_id, port); 102186690Sobrien if (stcb) 103186690Sobrien *abort_no_unlock = 1; 104186690Sobrien goto outnow; 105186690Sobrien } 106186690Sobrien /* validate parameters */ 107186690Sobrien init = &cp->init; 108186690Sobrien if (init->initiate_tag == 0) { 109186690Sobrien /* protocol error... send abort */ 110186690Sobrien op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 111186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 112186690Sobrien vrf_id, port); 113186690Sobrien if (stcb) 114186690Sobrien *abort_no_unlock = 1; 115186690Sobrien goto outnow; 116186690Sobrien } 117186690Sobrien if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { 118186690Sobrien /* invalid parameter... send abort */ 119186690Sobrien op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 120186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 121186690Sobrien vrf_id, port); 122186690Sobrien if (stcb) 123186690Sobrien *abort_no_unlock = 1; 124186690Sobrien goto outnow; 125186690Sobrien } 126186690Sobrien if (init->num_inbound_streams == 0) { 127186690Sobrien /* protocol error... send abort */ 128186690Sobrien op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 129186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 130186690Sobrien vrf_id, port); 131186690Sobrien if (stcb) 132186690Sobrien *abort_no_unlock = 1; 133186690Sobrien goto outnow; 134186690Sobrien } 135186690Sobrien if (init->num_outbound_streams == 0) { 136186690Sobrien /* protocol error... send abort */ 137186690Sobrien op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 138186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 139186690Sobrien vrf_id, port); 140186690Sobrien if (stcb) 141186690Sobrien *abort_no_unlock = 1; 142186690Sobrien goto outnow; 143186690Sobrien } 144186690Sobrien if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 145186690Sobrien offset + ntohs(cp->ch.chunk_length))) { 146186690Sobrien /* auth parameter(s) error... send abort */ 147186690Sobrien sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port); 148186690Sobrien if (stcb) 149186690Sobrien *abort_no_unlock = 1; 150186690Sobrien goto outnow; 151186690Sobrien } 152186690Sobrien /* 153186690Sobrien * We are only accepting if we have a socket with positive 154186690Sobrien * so_qlimit. 155186690Sobrien */ 156186690Sobrien if ((stcb == NULL) && 157186690Sobrien ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 158186690Sobrien (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 159186690Sobrien (inp->sctp_socket == NULL) || 160186690Sobrien (inp->sctp_socket->so_qlimit == 0))) { 161186690Sobrien /* 162186690Sobrien * FIX ME ?? What about TCP model and we have a 163186690Sobrien * match/restart case? Actually no fix is needed. the lookup 164186690Sobrien * will always find the existing assoc so stcb would not be 165186690Sobrien * NULL. It may be questionable to do this since we COULD 166186690Sobrien * just send back the INIT-ACK and hope that the app did 167186690Sobrien * accept()'s by the time the COOKIE was sent. But there is 168186690Sobrien * a price to pay for COOKIE generation and I don't want to 169186690Sobrien * pay it on the chance that the app will actually do some 170186690Sobrien * accepts(). The App just looses and should NOT be in this 171186690Sobrien * state :-) 172186690Sobrien */ 173186690Sobrien if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { 174186690Sobrien sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 175186690Sobrien } 176186690Sobrien goto outnow; 177186690Sobrien } 178186690Sobrien if ((stcb != NULL) && 179186690Sobrien (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { 180186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); 181186690Sobrien sctp_send_shutdown_ack(stcb, NULL); 182186690Sobrien sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 183186690Sobrien } else { 184186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 185186690Sobrien sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port, 186186690Sobrien ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); 187186690Sobrien } 188186690Sobrienoutnow: 189186690Sobrien if (stcb == NULL) { 190186690Sobrien SCTP_INP_RUNLOCK(inp); 191186690Sobrien } 192186690Sobrien} 193186690Sobrien 194186690Sobrien/* 195186690Sobrien * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 196186690Sobrien */ 197186690Sobrien 198186690Sobrienint 199186690Sobriensctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked 200186690Sobrien#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 201186690Sobrien SCTP_UNUSED 202186690Sobrien#endif 203186690Sobrien) 204186690Sobrien{ 205186690Sobrien int unsent_data = 0; 206186690Sobrien unsigned int i; 207186690Sobrien struct sctp_stream_queue_pending *sp; 208186690Sobrien struct sctp_association *asoc; 209186690Sobrien 210186690Sobrien /* 211186690Sobrien * This function returns the number of streams that have true unsent 212186690Sobrien * data on them. Note that as it looks through it will clean up any 213186690Sobrien * places that have old data that has been sent but left at top of 214186690Sobrien * stream queue. 215186690Sobrien */ 216186690Sobrien asoc = &stcb->asoc; 217186690Sobrien SCTP_TCB_SEND_LOCK(stcb); 218186690Sobrien if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 219186690Sobrien /* Check to see if some data queued */ 220186690Sobrien for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 221186690Sobrien /* sa_ignore FREED_MEMORY */ 222186690Sobrien sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 223186690Sobrien if (sp == NULL) { 224186690Sobrien continue; 225186690Sobrien } 226186690Sobrien if ((sp->msg_is_complete) && 227186690Sobrien (sp->length == 0) && 228186690Sobrien (sp->sender_all_done)) { 229186690Sobrien /* 230186690Sobrien * We are doing differed cleanup. Last time 231186690Sobrien * through when we took all the data the 232186690Sobrien * sender_all_done was not set. 233186690Sobrien */ 234186690Sobrien if (sp->put_last_out == 0) { 235186690Sobrien SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 236186690Sobrien SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 237186690Sobrien sp->sender_all_done, 238186690Sobrien sp->length, 239186690Sobrien sp->msg_is_complete, 240186690Sobrien sp->put_last_out); 241186690Sobrien } 242186690Sobrien atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 243186690Sobrien TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 244186690Sobrien if (sp->net) { 245186690Sobrien sctp_free_remote_addr(sp->net); 246186690Sobrien sp->net = NULL; 247186690Sobrien } 248186690Sobrien if (sp->data) { 249186690Sobrien sctp_m_freem(sp->data); 250186690Sobrien sp->data = NULL; 251186690Sobrien } 252186690Sobrien sctp_free_a_strmoq(stcb, sp, so_locked); 253186690Sobrien } else { 254186690Sobrien unsent_data++; 255186690Sobrien break; 256186690Sobrien } 257186690Sobrien } 258186690Sobrien } 259186690Sobrien SCTP_TCB_SEND_UNLOCK(stcb); 260186690Sobrien return (unsent_data); 261186690Sobrien} 262186690Sobrien 263186690Sobrienstatic int 264186690Sobriensctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) 265186690Sobrien{ 266186690Sobrien struct sctp_init *init; 267186690Sobrien struct sctp_association *asoc; 268186690Sobrien struct sctp_nets *lnet; 269186690Sobrien unsigned int i; 270186690Sobrien 271186690Sobrien init = &cp->init; 272186690Sobrien asoc = &stcb->asoc; 273186690Sobrien /* save off parameters */ 274186690Sobrien asoc->peer_vtag = ntohl(init->initiate_tag); 275186690Sobrien asoc->peers_rwnd = ntohl(init->a_rwnd); 276186690Sobrien /* init tsn's */ 277186690Sobrien asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 278186690Sobrien 279186690Sobrien if (!TAILQ_EMPTY(&asoc->nets)) { 280186690Sobrien /* update any ssthresh's that may have a default */ 281186690Sobrien TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 282186690Sobrien lnet->ssthresh = asoc->peers_rwnd; 283186690Sobrien if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 284186690Sobrien sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 285186690Sobrien } 286186690Sobrien } 287186690Sobrien } 288186690Sobrien SCTP_TCB_SEND_LOCK(stcb); 289186690Sobrien if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 290186690Sobrien unsigned int newcnt; 291186690Sobrien struct sctp_stream_out *outs; 292186690Sobrien struct sctp_stream_queue_pending *sp, *nsp; 293186690Sobrien struct sctp_tmit_chunk *chk, *nchk; 294186690Sobrien 295186690Sobrien /* abandon the upper streams */ 296186690Sobrien newcnt = ntohs(init->num_inbound_streams); 297186690Sobrien TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 298186690Sobrien if (chk->rec.data.stream_number >= newcnt) { 299186690Sobrien TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 300186690Sobrien asoc->send_queue_cnt--; 301186690Sobrien if (chk->data != NULL) { 302186690Sobrien sctp_free_bufspace(stcb, asoc, chk, 1); 303186690Sobrien sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, 304186690Sobrien SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED); 305186690Sobrien if (chk->data) { 306186690Sobrien sctp_m_freem(chk->data); 307186690Sobrien chk->data = NULL; 308186690Sobrien } 309186690Sobrien } 310186690Sobrien sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 311186690Sobrien /* sa_ignore FREED_MEMORY */ 312186690Sobrien } 313186690Sobrien } 314186690Sobrien if (asoc->strmout) { 315186690Sobrien for (i = newcnt; i < asoc->pre_open_streams; i++) { 316186690Sobrien outs = &asoc->strmout[i]; 317186690Sobrien TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 318186690Sobrien TAILQ_REMOVE(&outs->outqueue, sp, next); 319186690Sobrien asoc->stream_queue_cnt--; 320186690Sobrien sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 321186690Sobrien stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, 322186690Sobrien sp, SCTP_SO_NOT_LOCKED); 323186690Sobrien if (sp->data) { 324186690Sobrien sctp_m_freem(sp->data); 325186690Sobrien sp->data = NULL; 326186690Sobrien } 327186690Sobrien if (sp->net) { 328186690Sobrien sctp_free_remote_addr(sp->net); 329186690Sobrien sp->net = NULL; 330186690Sobrien } 331186690Sobrien /* Free the chunk */ 332186690Sobrien sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 333186690Sobrien /* sa_ignore FREED_MEMORY */ 334186690Sobrien } 335186690Sobrien } 336186690Sobrien } 337186690Sobrien /* cut back the count */ 338186690Sobrien asoc->pre_open_streams = newcnt; 339186690Sobrien } 340186690Sobrien SCTP_TCB_SEND_UNLOCK(stcb); 341186690Sobrien asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; 342186690Sobrien 343186690Sobrien /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 344186690Sobrien asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 345186690Sobrien if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 346186690Sobrien sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 347186690Sobrien } 348186690Sobrien /* This is the next one we expect */ 349186690Sobrien asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 350186690Sobrien 351186690Sobrien asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 352186690Sobrien asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 353186690Sobrien 354186690Sobrien asoc->advanced_peer_ack_point = asoc->last_acked_seq; 355186690Sobrien /* open the requested streams */ 356186690Sobrien 357186690Sobrien if (asoc->strmin != NULL) { 358186690Sobrien /* Free the old ones */ 359186690Sobrien struct sctp_queued_to_read *ctl, *nctl; 360186690Sobrien 361186690Sobrien for (i = 0; i < asoc->streamincnt; i++) { 362186690Sobrien TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { 363186690Sobrien TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); 364186690Sobrien sctp_free_remote_addr(ctl->whoFrom); 365186690Sobrien ctl->whoFrom = NULL; 366186690Sobrien sctp_m_freem(ctl->data); 367186690Sobrien ctl->data = NULL; 368186690Sobrien sctp_free_a_readq(stcb, ctl); 369186690Sobrien } 370186690Sobrien } 371186690Sobrien SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 372186690Sobrien } 373186690Sobrien asoc->streamincnt = ntohs(init->num_outbound_streams); 374186690Sobrien if (asoc->streamincnt > MAX_SCTP_STREAMS) { 375186690Sobrien asoc->streamincnt = MAX_SCTP_STREAMS; 376186690Sobrien } 377186690Sobrien SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 378186690Sobrien sizeof(struct sctp_stream_in), SCTP_M_STRMI); 379186690Sobrien if (asoc->strmin == NULL) { 380186690Sobrien /* we didn't get memory for the streams! */ 381186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 382186690Sobrien return (-1); 383186690Sobrien } 384186690Sobrien for (i = 0; i < asoc->streamincnt; i++) { 385186690Sobrien asoc->strmin[i].stream_no = i; 386186690Sobrien asoc->strmin[i].last_sequence_delivered = 0xffff; 387186690Sobrien /* 388186690Sobrien * U-stream ranges will be set when the cookie is unpacked. 389186690Sobrien * Or for the INIT sender they are un set (if pr-sctp not 390186690Sobrien * supported) when the INIT-ACK arrives. 391186690Sobrien */ 392186690Sobrien TAILQ_INIT(&asoc->strmin[i].inqueue); 393186690Sobrien asoc->strmin[i].delivery_started = 0; 394186690Sobrien } 395186690Sobrien /* 396186690Sobrien * load_address_from_init will put the addresses into the 397186690Sobrien * association when the COOKIE is processed or the INIT-ACK is 398186690Sobrien * processed. Both types of COOKIE's existing and new call this 399186690Sobrien * routine. It will remove addresses that are no longer in the 400186690Sobrien * association (for the restarting case where addresses are 401186690Sobrien * removed). Up front when the INIT arrives we will discard it if it 402186690Sobrien * is a restart and new addresses have been added. 403186690Sobrien */ 404186690Sobrien /* sa_ignore MEMLEAK */ 405186690Sobrien return (0); 406186690Sobrien} 407186690Sobrien 408186690Sobrien/* 409186690Sobrien * INIT-ACK message processing/consumption returns value < 0 on error 410186690Sobrien */ 411186690Sobrienstatic int 412186690Sobriensctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 413186690Sobrien struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 414186690Sobrien struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 415186690Sobrien{ 416186690Sobrien struct sctp_association *asoc; 417186690Sobrien struct mbuf *op_err; 418186690Sobrien int retval, abort_flag; 419186690Sobrien uint32_t initack_limit; 420186690Sobrien int nat_friendly = 0; 421186690Sobrien 422186690Sobrien /* First verify that we have no illegal param's */ 423186690Sobrien abort_flag = 0; 424186690Sobrien op_err = NULL; 425186690Sobrien 426186690Sobrien op_err = sctp_arethere_unrecognized_parameters(m, 427186690Sobrien (offset + sizeof(struct sctp_init_chunk)), 428186690Sobrien &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); 429186690Sobrien if (abort_flag) { 430186690Sobrien /* Send an abort and notify peer */ 431186690Sobrien sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED); 432186690Sobrien *abort_no_unlock = 1; 433186690Sobrien return (-1); 434186690Sobrien } 435186690Sobrien asoc = &stcb->asoc; 436186690Sobrien asoc->peer_supports_nat = (uint8_t) nat_friendly; 437186690Sobrien /* process the peer's parameters in the INIT-ACK */ 438186690Sobrien retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); 439186690Sobrien if (retval < 0) { 440186690Sobrien return (retval); 441186690Sobrien } 442186690Sobrien initack_limit = offset + ntohs(cp->ch.chunk_length); 443186690Sobrien /* load all addresses */ 444186690Sobrien if ((retval = sctp_load_addresses_from_init(stcb, m, 445186690Sobrien (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh, 446186690Sobrien NULL))) { 447186690Sobrien /* Huh, we should abort */ 448186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT1, 449186690Sobrien "Load addresses from INIT causes an abort %d\n", 450186690Sobrien retval); 451186690Sobrien sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 452186690Sobrien NULL, 0, net->port); 453186690Sobrien *abort_no_unlock = 1; 454186690Sobrien return (-1); 455186690Sobrien } 456186690Sobrien /* if the peer doesn't support asconf, flush the asconf queue */ 457186690Sobrien if (asoc->peer_supports_asconf == 0) { 458186690Sobrien struct sctp_asconf_addr *param, *nparam; 459186690Sobrien 460186690Sobrien TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 461186690Sobrien TAILQ_REMOVE(&asoc->asconf_queue, param, next); 462186690Sobrien SCTP_FREE(param, SCTP_M_ASC_ADDR); 463186690Sobrien } 464186690Sobrien } 465186690Sobrien stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 466186690Sobrien stcb->asoc.local_hmacs); 467186690Sobrien if (op_err) { 468186690Sobrien sctp_queue_op_err(stcb, op_err); 469186690Sobrien /* queuing will steal away the mbuf chain to the out queue */ 470186690Sobrien op_err = NULL; 471186690Sobrien } 472186690Sobrien /* extract the cookie and queue it to "echo" it back... */ 473186690Sobrien if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 474186690Sobrien sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 475186690Sobrien stcb->asoc.overall_error_count, 476186690Sobrien 0, 477186690Sobrien SCTP_FROM_SCTP_INPUT, 478186690Sobrien __LINE__); 479186690Sobrien } 480186690Sobrien stcb->asoc.overall_error_count = 0; 481186690Sobrien net->error_count = 0; 482186690Sobrien 483186690Sobrien /* 484186690Sobrien * Cancel the INIT timer, We do this first before queueing the 485186690Sobrien * cookie. We always cancel at the primary to assue that we are 486186690Sobrien * canceling the timer started by the INIT which always goes to the 487186690Sobrien * primary. 488186690Sobrien */ 489186690Sobrien sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 490186690Sobrien asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 491186690Sobrien 492186690Sobrien /* calculate the RTO */ 493186690Sobrien net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, 494186690Sobrien SCTP_RTT_FROM_NON_DATA); 495186690Sobrien 496186690Sobrien retval = sctp_send_cookie_echo(m, offset, stcb, net); 497186690Sobrien if (retval < 0) { 498186690Sobrien /* 499186690Sobrien * No cookie, we probably should send a op error. But in any 500186690Sobrien * case if there is no cookie in the INIT-ACK, we can 501186690Sobrien * abandon the peer, its broke. 502186690Sobrien */ 503186690Sobrien if (retval == -3) { 504186690Sobrien /* We abort with an error of missing mandatory param */ 505186690Sobrien op_err = 506186690Sobrien sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); 507186690Sobrien if (op_err) { 508186690Sobrien /* 509186690Sobrien * Expand beyond to include the mandatory 510186690Sobrien * param cookie 511186690Sobrien */ 512186690Sobrien struct sctp_inv_mandatory_param *mp; 513186690Sobrien 514186690Sobrien SCTP_BUF_LEN(op_err) = 515186690Sobrien sizeof(struct sctp_inv_mandatory_param); 516186690Sobrien mp = mtod(op_err, 517186690Sobrien struct sctp_inv_mandatory_param *); 518186690Sobrien /* Subtract the reserved param */ 519186690Sobrien mp->length = 520186690Sobrien htons(sizeof(struct sctp_inv_mandatory_param) - 2); 521186690Sobrien mp->num_param = htonl(1); 522186690Sobrien mp->param = htons(SCTP_STATE_COOKIE); 523186690Sobrien mp->resv = 0; 524186690Sobrien } 525186690Sobrien sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 526186690Sobrien sh, op_err, vrf_id, net->port); 527186690Sobrien *abort_no_unlock = 1; 528186690Sobrien } 529186690Sobrien return (retval); 530186690Sobrien } 531186690Sobrien return (0); 532186690Sobrien} 533186690Sobrien 534186690Sobrienstatic void 535186690Sobriensctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 536186690Sobrien struct sctp_tcb *stcb, struct sctp_nets *net) 537186690Sobrien{ 538186690Sobrien struct sockaddr_storage store; 539186690Sobrien struct sctp_nets *r_net, *f_net; 540186690Sobrien struct timeval tv; 541186690Sobrien int req_prim = 0; 542186690Sobrien uint16_t old_error_counter; 543186690Sobrien 544186690Sobrien#ifdef INET 545186690Sobrien struct sockaddr_in *sin; 546186690Sobrien 547186690Sobrien#endif 548186690Sobrien#ifdef INET6 549186690Sobrien struct sockaddr_in6 *sin6; 550186690Sobrien 551186690Sobrien#endif 552186690Sobrien 553186690Sobrien if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 554186690Sobrien /* Invalid length */ 555186690Sobrien return; 556186690Sobrien } 557186690Sobrien memset(&store, 0, sizeof(store)); 558186690Sobrien switch (cp->heartbeat.hb_info.addr_family) { 559186690Sobrien#ifdef INET 560186690Sobrien case AF_INET: 561186690Sobrien if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 562186690Sobrien sin = (struct sockaddr_in *)&store; 563186690Sobrien sin->sin_family = cp->heartbeat.hb_info.addr_family; 564186690Sobrien sin->sin_len = cp->heartbeat.hb_info.addr_len; 565186690Sobrien sin->sin_port = stcb->rport; 566186690Sobrien memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, 567186690Sobrien sizeof(sin->sin_addr)); 568186690Sobrien } else { 569186690Sobrien return; 570186690Sobrien } 571186690Sobrien break; 572186690Sobrien#endif 573186690Sobrien#ifdef INET6 574186690Sobrien case AF_INET6: 575186690Sobrien if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 576186690Sobrien sin6 = (struct sockaddr_in6 *)&store; 577186690Sobrien sin6->sin6_family = cp->heartbeat.hb_info.addr_family; 578186690Sobrien sin6->sin6_len = cp->heartbeat.hb_info.addr_len; 579186690Sobrien sin6->sin6_port = stcb->rport; 580186690Sobrien memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, 581186690Sobrien sizeof(sin6->sin6_addr)); 582186690Sobrien } else { 583186690Sobrien return; 584186690Sobrien } 585186690Sobrien break; 586186690Sobrien#endif 587186690Sobrien default: 588186690Sobrien return; 589186690Sobrien } 590186690Sobrien r_net = sctp_findnet(stcb, (struct sockaddr *)&store); 591186690Sobrien if (r_net == NULL) { 592186690Sobrien SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 593186690Sobrien return; 594186690Sobrien } 595186690Sobrien if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 596186690Sobrien (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 597186690Sobrien (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 598186690Sobrien /* 599186690Sobrien * If the its a HB and it's random value is correct when can 600186690Sobrien * confirm the destination. 601186690Sobrien */ 602186690Sobrien r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 603186690Sobrien if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 604186690Sobrien stcb->asoc.primary_destination = r_net; 605186690Sobrien r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 606186690Sobrien f_net = TAILQ_FIRST(&stcb->asoc.nets); 607186690Sobrien if (f_net != r_net) { 608186690Sobrien /* 609186690Sobrien * first one on the list is NOT the primary 610186690Sobrien * sctp_cmpaddr() is much more efficent if 611186690Sobrien * the primary is the first on the list, 612186690Sobrien * make it so. 613186690Sobrien */ 614186690Sobrien TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 615186690Sobrien TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 616186690Sobrien } 617186690Sobrien req_prim = 1; 618186690Sobrien } 619186690Sobrien sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 620186690Sobrien stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 621186690Sobrien sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 622186690Sobrien sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 623186690Sobrien } 624186690Sobrien old_error_counter = r_net->error_count; 625186690Sobrien r_net->error_count = 0; 626186690Sobrien r_net->hb_responded = 1; 627186690Sobrien tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 628186690Sobrien tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 629186690Sobrien /* Now lets do a RTO with this */ 630186690Sobrien r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, 631186690Sobrien SCTP_RTT_FROM_NON_DATA); 632186690Sobrien if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 633186690Sobrien r_net->dest_state |= SCTP_ADDR_REACHABLE; 634186690Sobrien sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 635186690Sobrien SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED); 636186690Sobrien } 637186690Sobrien if (r_net->dest_state & SCTP_ADDR_PF) { 638186690Sobrien r_net->dest_state &= ~SCTP_ADDR_PF; 639186690Sobrien stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 640186690Sobrien } 641186690Sobrien if (old_error_counter > 0) { 642 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 643 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 644 } 645 if (r_net == stcb->asoc.primary_destination) { 646 if (stcb->asoc.alternate) { 647 /* release the alternate, primary is good */ 648 sctp_free_remote_addr(stcb->asoc.alternate); 649 stcb->asoc.alternate = NULL; 650 } 651 } 652 /* Mobility adaptation */ 653 if (req_prim) { 654 if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 655 SCTP_MOBILITY_BASE) || 656 sctp_is_mobility_feature_on(stcb->sctp_ep, 657 SCTP_MOBILITY_FASTHANDOFF)) && 658 sctp_is_mobility_feature_on(stcb->sctp_ep, 659 SCTP_MOBILITY_PRIM_DELETED)) { 660 661 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7); 662 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 663 SCTP_MOBILITY_FASTHANDOFF)) { 664 sctp_assoc_immediate_retrans(stcb, 665 stcb->asoc.primary_destination); 666 } 667 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 668 SCTP_MOBILITY_BASE)) { 669 sctp_move_chunks_from_net(stcb, 670 stcb->asoc.deleted_primary); 671 } 672 sctp_delete_prim_timer(stcb->sctp_ep, stcb, 673 stcb->asoc.deleted_primary); 674 } 675 } 676} 677 678static int 679sctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 680{ 681 /* 682 * return 0 means we want you to proceed with the abort non-zero 683 * means no abort processing 684 */ 685 struct sctpasochead *head; 686 687 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 688 /* generate a new vtag and send init */ 689 LIST_REMOVE(stcb, sctp_asocs); 690 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 691 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 692 /* 693 * put it in the bucket in the vtag hash of assoc's for the 694 * system 695 */ 696 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 697 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 698 return (1); 699 } 700 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { 701 /* 702 * treat like a case where the cookie expired i.e.: - dump 703 * current cookie. - generate a new vtag. - resend init. 704 */ 705 /* generate a new vtag and send init */ 706 LIST_REMOVE(stcb, sctp_asocs); 707 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; 708 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; 709 sctp_stop_all_cookie_timers(stcb); 710 sctp_toss_old_cookies(stcb, &stcb->asoc); 711 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 712 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 713 /* 714 * put it in the bucket in the vtag hash of assoc's for the 715 * system 716 */ 717 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 718 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 719 return (1); 720 } 721 return (0); 722} 723 724static int 725sctp_handle_nat_missing_state(struct sctp_tcb *stcb, 726 struct sctp_nets *net) 727{ 728 /* 729 * return 0 means we want you to proceed with the abort non-zero 730 * means no abort processing 731 */ 732 if (stcb->asoc.peer_supports_auth == 0) { 733 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 734 return (0); 735 } 736 sctp_asconf_send_nat_state_update(stcb, net); 737 return (1); 738} 739 740 741static void 742sctp_handle_abort(struct sctp_abort_chunk *cp, 743 struct sctp_tcb *stcb, struct sctp_nets *net) 744{ 745#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 746 struct socket *so; 747 748#endif 749 uint16_t len; 750 751 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 752 if (stcb == NULL) 753 return; 754 755 len = ntohs(cp->ch.chunk_length); 756 if (len > sizeof(struct sctp_chunkhdr)) { 757 /* 758 * Need to check the cause codes for our two magic nat 759 * aborts which don't kill the assoc necessarily. 760 */ 761 struct sctp_abort_chunk *cpnext; 762 struct sctp_missing_nat_state *natc; 763 uint16_t cause; 764 765 cpnext = cp; 766 cpnext++; 767 natc = (struct sctp_missing_nat_state *)cpnext; 768 cause = ntohs(natc->cause); 769 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) { 770 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 771 cp->ch.chunk_flags); 772 if (sctp_handle_nat_colliding_state(stcb)) { 773 return; 774 } 775 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) { 776 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 777 cp->ch.chunk_flags); 778 if (sctp_handle_nat_missing_state(stcb, net)) { 779 return; 780 } 781 } 782 } 783 /* stop any receive timers */ 784 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 785 /* notify user of the abort and clean up... */ 786 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 787 /* free the tcb */ 788 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 789 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 790 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 791 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 792 } 793#ifdef SCTP_ASOCLOG_OF_TSNS 794 sctp_print_out_track_log(stcb); 795#endif 796#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 797 so = SCTP_INP_SO(stcb->sctp_ep); 798 atomic_add_int(&stcb->asoc.refcnt, 1); 799 SCTP_TCB_UNLOCK(stcb); 800 SCTP_SOCKET_LOCK(so, 1); 801 SCTP_TCB_LOCK(stcb); 802 atomic_subtract_int(&stcb->asoc.refcnt, 1); 803#endif 804 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 805 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 806 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 807#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 808 SCTP_SOCKET_UNLOCK(so, 1); 809#endif 810 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 811} 812 813static void 814sctp_start_net_timers(struct sctp_tcb *stcb) 815{ 816 uint32_t cnt_hb_sent; 817 struct sctp_nets *net; 818 819 cnt_hb_sent = 0; 820 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 821 /* 822 * For each network start: 1) A pmtu timer. 2) A HB timer 3) 823 * If the dest in unconfirmed send a hb as well if under 824 * max_hb_burst have been sent. 825 */ 826 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 827 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 828 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 829 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 830 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 831 cnt_hb_sent++; 832 } 833 } 834 if (cnt_hb_sent) { 835 sctp_chunk_output(stcb->sctp_ep, stcb, 836 SCTP_OUTPUT_FROM_COOKIE_ACK, 837 SCTP_SO_NOT_LOCKED); 838 } 839} 840 841 842static void 843sctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 844 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 845{ 846 struct sctp_association *asoc; 847 int some_on_streamwheel; 848 849#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 850 struct socket *so; 851 852#endif 853 854 SCTPDBG(SCTP_DEBUG_INPUT2, 855 "sctp_handle_shutdown: handling SHUTDOWN\n"); 856 if (stcb == NULL) 857 return; 858 asoc = &stcb->asoc; 859 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 860 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 861 return; 862 } 863 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 864 /* Shutdown NOT the expected size */ 865 return; 866 } else { 867 sctp_update_acked(stcb, cp, abort_flag); 868 if (*abort_flag) { 869 return; 870 } 871 } 872 if (asoc->control_pdapi) { 873 /* 874 * With a normal shutdown we assume the end of last record. 875 */ 876 SCTP_INP_READ_LOCK(stcb->sctp_ep); 877 asoc->control_pdapi->end_added = 1; 878 asoc->control_pdapi->pdapi_aborted = 1; 879 asoc->control_pdapi = NULL; 880 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 881#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 882 so = SCTP_INP_SO(stcb->sctp_ep); 883 atomic_add_int(&stcb->asoc.refcnt, 1); 884 SCTP_TCB_UNLOCK(stcb); 885 SCTP_SOCKET_LOCK(so, 1); 886 SCTP_TCB_LOCK(stcb); 887 atomic_subtract_int(&stcb->asoc.refcnt, 1); 888 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 889 /* assoc was freed while we were unlocked */ 890 SCTP_SOCKET_UNLOCK(so, 1); 891 return; 892 } 893#endif 894 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 895#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 896 SCTP_SOCKET_UNLOCK(so, 1); 897#endif 898 } 899 /* goto SHUTDOWN_RECEIVED state to block new requests */ 900 if (stcb->sctp_socket) { 901 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 902 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 903 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 904 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); 905 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 906 /* 907 * notify upper layer that peer has initiated a 908 * shutdown 909 */ 910 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 911 912 /* reset time */ 913 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 914 } 915 } 916 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 917 /* 918 * stop the shutdown timer, since we WILL move to 919 * SHUTDOWN-ACK-SENT. 920 */ 921 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 922 } 923 /* Now is there unsent data on a stream somewhere? */ 924 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 925 926 if (!TAILQ_EMPTY(&asoc->send_queue) || 927 !TAILQ_EMPTY(&asoc->sent_queue) || 928 some_on_streamwheel) { 929 /* By returning we will push more data out */ 930 return; 931 } else { 932 /* no outstanding data to send, so move on... */ 933 /* send SHUTDOWN-ACK */ 934 sctp_send_shutdown_ack(stcb, net); 935 /* move to SHUTDOWN-ACK-SENT state */ 936 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 937 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 938 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 939 } 940 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 941 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 942 sctp_stop_timers_for_shutdown(stcb); 943 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, 944 stcb, net); 945 } 946} 947 948static void 949sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 950 struct sctp_tcb *stcb, 951 struct sctp_nets *net) 952{ 953 struct sctp_association *asoc; 954 955#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 956 struct socket *so; 957 958 so = SCTP_INP_SO(stcb->sctp_ep); 959#endif 960 SCTPDBG(SCTP_DEBUG_INPUT2, 961 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 962 if (stcb == NULL) 963 return; 964 965 asoc = &stcb->asoc; 966 /* process according to association state */ 967 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 968 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 969 /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 970 sctp_send_shutdown_complete(stcb, net, 1); 971 SCTP_TCB_UNLOCK(stcb); 972 return; 973 } 974 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 975 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 976 /* unexpected SHUTDOWN-ACK... so ignore... */ 977 SCTP_TCB_UNLOCK(stcb); 978 return; 979 } 980 if (asoc->control_pdapi) { 981 /* 982 * With a normal shutdown we assume the end of last record. 983 */ 984 SCTP_INP_READ_LOCK(stcb->sctp_ep); 985 asoc->control_pdapi->end_added = 1; 986 asoc->control_pdapi->pdapi_aborted = 1; 987 asoc->control_pdapi = NULL; 988 SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 989#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 990 atomic_add_int(&stcb->asoc.refcnt, 1); 991 SCTP_TCB_UNLOCK(stcb); 992 SCTP_SOCKET_LOCK(so, 1); 993 SCTP_TCB_LOCK(stcb); 994 atomic_subtract_int(&stcb->asoc.refcnt, 1); 995 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 996 /* assoc was freed while we were unlocked */ 997 SCTP_SOCKET_UNLOCK(so, 1); 998 return; 999 } 1000#endif 1001 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1002#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1003 SCTP_SOCKET_UNLOCK(so, 1); 1004#endif 1005 } 1006 /* are the queues empty? */ 1007 if (!TAILQ_EMPTY(&asoc->send_queue) || 1008 !TAILQ_EMPTY(&asoc->sent_queue) || 1009 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 1010 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 1011 } 1012 /* stop the timer */ 1013 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 1014 /* send SHUTDOWN-COMPLETE */ 1015 sctp_send_shutdown_complete(stcb, net, 0); 1016 /* notify upper layer protocol */ 1017 if (stcb->sctp_socket) { 1018 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1019 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1020 stcb->sctp_socket->so_snd.sb_cc = 0; 1021 } 1022 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 1023 } 1024 SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1025 /* free the TCB but first save off the ep */ 1026#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1027 atomic_add_int(&stcb->asoc.refcnt, 1); 1028 SCTP_TCB_UNLOCK(stcb); 1029 SCTP_SOCKET_LOCK(so, 1); 1030 SCTP_TCB_LOCK(stcb); 1031 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1032#endif 1033 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1034 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 1035#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1036 SCTP_SOCKET_UNLOCK(so, 1); 1037#endif 1038} 1039 1040/* 1041 * Skip past the param header and then we will find the chunk that caused the 1042 * problem. There are two possiblities ASCONF or FWD-TSN other than that and 1043 * our peer must be broken. 1044 */ 1045static void 1046sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, 1047 struct sctp_nets *net) 1048{ 1049 struct sctp_chunkhdr *chk; 1050 1051 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); 1052 switch (chk->chunk_type) { 1053 case SCTP_ASCONF_ACK: 1054 case SCTP_ASCONF: 1055 sctp_asconf_cleanup(stcb, net); 1056 break; 1057 case SCTP_FORWARD_CUM_TSN: 1058 stcb->asoc.peer_supports_prsctp = 0; 1059 break; 1060 default: 1061 SCTPDBG(SCTP_DEBUG_INPUT2, 1062 "Peer does not support chunk type %d(%x)??\n", 1063 chk->chunk_type, (uint32_t) chk->chunk_type); 1064 break; 1065 } 1066} 1067 1068/* 1069 * Skip past the param header and then we will find the param that caused the 1070 * problem. There are a number of param's in a ASCONF OR the prsctp param 1071 * these will turn of specific features. 1072 */ 1073static void 1074sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) 1075{ 1076 struct sctp_paramhdr *pbad; 1077 1078 pbad = phdr + 1; 1079 switch (ntohs(pbad->param_type)) { 1080 /* pr-sctp draft */ 1081 case SCTP_PRSCTP_SUPPORTED: 1082 stcb->asoc.peer_supports_prsctp = 0; 1083 break; 1084 case SCTP_SUPPORTED_CHUNK_EXT: 1085 break; 1086 /* draft-ietf-tsvwg-addip-sctp */ 1087 case SCTP_HAS_NAT_SUPPORT: 1088 stcb->asoc.peer_supports_nat = 0; 1089 break; 1090 case SCTP_ADD_IP_ADDRESS: 1091 case SCTP_DEL_IP_ADDRESS: 1092 case SCTP_SET_PRIM_ADDR: 1093 stcb->asoc.peer_supports_asconf = 0; 1094 break; 1095 case SCTP_SUCCESS_REPORT: 1096 case SCTP_ERROR_CAUSE_IND: 1097 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1098 SCTPDBG(SCTP_DEBUG_INPUT2, 1099 "Turning off ASCONF to this strange peer\n"); 1100 stcb->asoc.peer_supports_asconf = 0; 1101 break; 1102 default: 1103 SCTPDBG(SCTP_DEBUG_INPUT2, 1104 "Peer does not support param type %d(%x)??\n", 1105 pbad->param_type, (uint32_t) pbad->param_type); 1106 break; 1107 } 1108} 1109 1110static int 1111sctp_handle_error(struct sctp_chunkhdr *ch, 1112 struct sctp_tcb *stcb, struct sctp_nets *net) 1113{ 1114 int chklen; 1115 struct sctp_paramhdr *phdr; 1116 uint16_t error_type; 1117 uint16_t error_len; 1118 struct sctp_association *asoc; 1119 int adjust; 1120 1121#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1122 struct socket *so; 1123 1124#endif 1125 1126 /* parse through all of the errors and process */ 1127 asoc = &stcb->asoc; 1128 phdr = (struct sctp_paramhdr *)((caddr_t)ch + 1129 sizeof(struct sctp_chunkhdr)); 1130 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); 1131 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { 1132 /* Process an Error Cause */ 1133 error_type = ntohs(phdr->param_type); 1134 error_len = ntohs(phdr->param_length); 1135 if ((error_len > chklen) || (error_len == 0)) { 1136 /* invalid param length for this param */ 1137 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", 1138 chklen, error_len); 1139 return (0); 1140 } 1141 switch (error_type) { 1142 case SCTP_CAUSE_INVALID_STREAM: 1143 case SCTP_CAUSE_MISSING_PARAM: 1144 case SCTP_CAUSE_INVALID_PARAM: 1145 case SCTP_CAUSE_NO_USER_DATA: 1146 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", 1147 error_type); 1148 break; 1149 case SCTP_CAUSE_NAT_COLLIDING_STATE: 1150 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", 1151 ch->chunk_flags); 1152 if (sctp_handle_nat_colliding_state(stcb)) { 1153 return (0); 1154 } 1155 break; 1156 case SCTP_CAUSE_NAT_MISSING_STATE: 1157 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", 1158 ch->chunk_flags); 1159 if (sctp_handle_nat_missing_state(stcb, net)) { 1160 return (0); 1161 } 1162 break; 1163 case SCTP_CAUSE_STALE_COOKIE: 1164 /* 1165 * We only act if we have echoed a cookie and are 1166 * waiting. 1167 */ 1168 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 1169 int *p; 1170 1171 p = (int *)((caddr_t)phdr + sizeof(*phdr)); 1172 /* Save the time doubled */ 1173 asoc->cookie_preserve_req = ntohl(*p) << 1; 1174 asoc->stale_cookie_count++; 1175 if (asoc->stale_cookie_count > 1176 asoc->max_init_times) { 1177 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED); 1178 /* now free the asoc */ 1179#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1180 so = SCTP_INP_SO(stcb->sctp_ep); 1181 atomic_add_int(&stcb->asoc.refcnt, 1); 1182 SCTP_TCB_UNLOCK(stcb); 1183 SCTP_SOCKET_LOCK(so, 1); 1184 SCTP_TCB_LOCK(stcb); 1185 atomic_subtract_int(&stcb->asoc.refcnt, 1); 1186#endif 1187 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1188 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1189#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1190 SCTP_SOCKET_UNLOCK(so, 1); 1191#endif 1192 return (-1); 1193 } 1194 /* blast back to INIT state */ 1195 sctp_toss_old_cookies(stcb, &stcb->asoc); 1196 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; 1197 asoc->state |= SCTP_STATE_COOKIE_WAIT; 1198 sctp_stop_all_cookie_timers(stcb); 1199 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1200 } 1201 break; 1202 case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1203 /* 1204 * Nothing we can do here, we don't do hostname 1205 * addresses so if the peer does not like my IPv6 1206 * (or IPv4 for that matter) it does not matter. If 1207 * they don't support that type of address, they can 1208 * NOT possibly get that packet type... i.e. with no 1209 * IPv6 you can't recieve a IPv6 packet. so we can 1210 * safely ignore this one. If we ever added support 1211 * for HOSTNAME Addresses, then we would need to do 1212 * something here. 1213 */ 1214 break; 1215 case SCTP_CAUSE_UNRECOG_CHUNK: 1216 sctp_process_unrecog_chunk(stcb, phdr, net); 1217 break; 1218 case SCTP_CAUSE_UNRECOG_PARAM: 1219 sctp_process_unrecog_param(stcb, phdr); 1220 break; 1221 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1222 /* 1223 * We ignore this since the timer will drive out a 1224 * new cookie anyway and there timer will drive us 1225 * to send a SHUTDOWN_COMPLETE. We can't send one 1226 * here since we don't have their tag. 1227 */ 1228 break; 1229 case SCTP_CAUSE_DELETING_LAST_ADDR: 1230 case SCTP_CAUSE_RESOURCE_SHORTAGE: 1231 case SCTP_CAUSE_DELETING_SRC_ADDR: 1232 /* 1233 * We should NOT get these here, but in a 1234 * ASCONF-ACK. 1235 */ 1236 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", 1237 error_type); 1238 break; 1239 case SCTP_CAUSE_OUT_OF_RESC: 1240 /* 1241 * And what, pray tell do we do with the fact that 1242 * the peer is out of resources? Not really sure we 1243 * could do anything but abort. I suspect this 1244 * should have came WITH an abort instead of in a 1245 * OP-ERROR. 1246 */ 1247 break; 1248 default: 1249 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", 1250 error_type); 1251 break; 1252 } 1253 adjust = SCTP_SIZE32(error_len); 1254 chklen -= adjust; 1255 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); 1256 } 1257 return (0); 1258} 1259 1260static int 1261sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1262 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1263 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id) 1264{ 1265 struct sctp_init_ack *init_ack; 1266 struct mbuf *op_err; 1267 1268 SCTPDBG(SCTP_DEBUG_INPUT2, 1269 "sctp_handle_init_ack: handling INIT-ACK\n"); 1270 1271 if (stcb == NULL) { 1272 SCTPDBG(SCTP_DEBUG_INPUT2, 1273 "sctp_handle_init_ack: TCB is null\n"); 1274 return (-1); 1275 } 1276 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { 1277 /* Invalid length */ 1278 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1279 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1280 op_err, 0, net->port); 1281 *abort_no_unlock = 1; 1282 return (-1); 1283 } 1284 init_ack = &cp->init; 1285 /* validate parameters */ 1286 if (init_ack->initiate_tag == 0) { 1287 /* protocol error... send an abort */ 1288 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1289 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1290 op_err, 0, net->port); 1291 *abort_no_unlock = 1; 1292 return (-1); 1293 } 1294 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { 1295 /* protocol error... send an abort */ 1296 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1297 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1298 op_err, 0, net->port); 1299 *abort_no_unlock = 1; 1300 return (-1); 1301 } 1302 if (init_ack->num_inbound_streams == 0) { 1303 /* protocol error... send an abort */ 1304 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1305 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1306 op_err, 0, net->port); 1307 *abort_no_unlock = 1; 1308 return (-1); 1309 } 1310 if (init_ack->num_outbound_streams == 0) { 1311 /* protocol error... send an abort */ 1312 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); 1313 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh, 1314 op_err, 0, net->port); 1315 *abort_no_unlock = 1; 1316 return (-1); 1317 } 1318 /* process according to association state... */ 1319 switch (stcb->asoc.state & SCTP_STATE_MASK) { 1320 case SCTP_STATE_COOKIE_WAIT: 1321 /* this is the expected state for this chunk */ 1322 /* process the INIT-ACK parameters */ 1323 if (stcb->asoc.primary_destination->dest_state & 1324 SCTP_ADDR_UNCONFIRMED) { 1325 /* 1326 * The primary is where we sent the INIT, we can 1327 * always consider it confirmed when the INIT-ACK is 1328 * returned. Do this before we load addresses 1329 * though. 1330 */ 1331 stcb->asoc.primary_destination->dest_state &= 1332 ~SCTP_ADDR_UNCONFIRMED; 1333 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1334 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1335 } 1336 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, 1337 net, abort_no_unlock, vrf_id) < 0) { 1338 /* error in parsing parameters */ 1339 return (-1); 1340 } 1341 /* update our state */ 1342 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1343 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); 1344 1345 /* reset the RTO calc */ 1346 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1347 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1348 stcb->asoc.overall_error_count, 1349 0, 1350 SCTP_FROM_SCTP_INPUT, 1351 __LINE__); 1352 } 1353 stcb->asoc.overall_error_count = 0; 1354 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1355 /* 1356 * collapse the init timer back in case of a exponential 1357 * backoff 1358 */ 1359 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1360 stcb, net); 1361 /* 1362 * the send at the end of the inbound data processing will 1363 * cause the cookie to be sent 1364 */ 1365 break; 1366 case SCTP_STATE_SHUTDOWN_SENT: 1367 /* incorrect state... discard */ 1368 break; 1369 case SCTP_STATE_COOKIE_ECHOED: 1370 /* incorrect state... discard */ 1371 break; 1372 case SCTP_STATE_OPEN: 1373 /* incorrect state... discard */ 1374 break; 1375 case SCTP_STATE_EMPTY: 1376 case SCTP_STATE_INUSE: 1377 default: 1378 /* incorrect state... discard */ 1379 return (-1); 1380 break; 1381 } 1382 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1383 return (0); 1384} 1385 1386static struct sctp_tcb * 1387sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1388 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1389 struct sctp_inpcb *inp, struct sctp_nets **netp, 1390 struct sockaddr *init_src, int *notification, 1391 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1392 uint32_t vrf_id, uint16_t port); 1393 1394 1395/* 1396 * handle a state cookie for an existing association m: input packet mbuf 1397 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1398 * "split" mbuf and the cookie signature does not exist offset: offset into 1399 * mbuf to the cookie-echo chunk 1400 */ 1401static struct sctp_tcb * 1402sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1403 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1404 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 1405 struct sockaddr *init_src, int *notification, 1406 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port) 1407{ 1408 struct sctp_association *asoc; 1409 struct sctp_init_chunk *init_cp, init_buf; 1410 struct sctp_init_ack_chunk *initack_cp, initack_buf; 1411 struct sctp_nets *net; 1412 struct mbuf *op_err; 1413 struct sctp_paramhdr *ph; 1414 int init_offset, initack_offset, i; 1415 int retval; 1416 int spec_flag = 0; 1417 uint32_t how_indx; 1418 1419 net = *netp; 1420 /* I know that the TCB is non-NULL from the caller */ 1421 asoc = &stcb->asoc; 1422 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 1423 if (asoc->cookie_how[how_indx] == 0) 1424 break; 1425 } 1426 if (how_indx < sizeof(asoc->cookie_how)) { 1427 asoc->cookie_how[how_indx] = 1; 1428 } 1429 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1430 /* SHUTDOWN came in after sending INIT-ACK */ 1431 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1432 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1433 0, M_DONTWAIT, 1, MT_DATA); 1434 if (op_err == NULL) { 1435 /* FOOBAR */ 1436 return (NULL); 1437 } 1438 /* Set the len */ 1439 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1440 ph = mtod(op_err, struct sctp_paramhdr *); 1441 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); 1442 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1443 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 1444 vrf_id, net->port); 1445 if (how_indx < sizeof(asoc->cookie_how)) 1446 asoc->cookie_how[how_indx] = 2; 1447 return (NULL); 1448 } 1449 /* 1450 * find and validate the INIT chunk in the cookie (peer's info) the 1451 * INIT should start after the cookie-echo header struct (chunk 1452 * header, state cookie header struct) 1453 */ 1454 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1455 1456 init_cp = (struct sctp_init_chunk *) 1457 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1458 (uint8_t *) & init_buf); 1459 if (init_cp == NULL) { 1460 /* could not pull a INIT chunk in cookie */ 1461 return (NULL); 1462 } 1463 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 1464 return (NULL); 1465 } 1466 /* 1467 * find and validate the INIT-ACK chunk in the cookie (my info) the 1468 * INIT-ACK follows the INIT chunk 1469 */ 1470 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1471 initack_cp = (struct sctp_init_ack_chunk *) 1472 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1473 (uint8_t *) & initack_buf); 1474 if (initack_cp == NULL) { 1475 /* could not pull INIT-ACK chunk in cookie */ 1476 return (NULL); 1477 } 1478 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 1479 return (NULL); 1480 } 1481 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1482 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1483 /* 1484 * case D in Section 5.2.4 Table 2: MMAA process accordingly 1485 * to get into the OPEN state 1486 */ 1487 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1488 /*- 1489 * Opps, this means that we somehow generated two vtag's 1490 * the same. I.e. we did: 1491 * Us Peer 1492 * <---INIT(tag=a)------ 1493 * ----INIT-ACK(tag=t)--> 1494 * ----INIT(tag=t)------> *1 1495 * <---INIT-ACK(tag=a)--- 1496 * <----CE(tag=t)------------- *2 1497 * 1498 * At point *1 we should be generating a different 1499 * tag t'. Which means we would throw away the CE and send 1500 * ours instead. Basically this is case C (throw away side). 1501 */ 1502 if (how_indx < sizeof(asoc->cookie_how)) 1503 asoc->cookie_how[how_indx] = 17; 1504 return (NULL); 1505 1506 } 1507 switch SCTP_GET_STATE 1508 (asoc) { 1509 case SCTP_STATE_COOKIE_WAIT: 1510 case SCTP_STATE_COOKIE_ECHOED: 1511 /* 1512 * INIT was sent but got a COOKIE_ECHO with the 1513 * correct tags... just accept it...but we must 1514 * process the init so that we can make sure we have 1515 * the right seq no's. 1516 */ 1517 /* First we must process the INIT !! */ 1518 retval = sctp_process_init(init_cp, stcb); 1519 if (retval < 0) { 1520 if (how_indx < sizeof(asoc->cookie_how)) 1521 asoc->cookie_how[how_indx] = 3; 1522 return (NULL); 1523 } 1524 /* we have already processed the INIT so no problem */ 1525 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, 1526 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1527 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1528 /* update current state */ 1529 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1530 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1531 else 1532 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1533 1534 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1535 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1536 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1537 stcb->sctp_ep, stcb, asoc->primary_destination); 1538 } 1539 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1540 sctp_stop_all_cookie_timers(stcb); 1541 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1542 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1543 (inp->sctp_socket->so_qlimit == 0) 1544 ) { 1545#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1546 struct socket *so; 1547 1548#endif 1549 /* 1550 * Here is where collision would go if we 1551 * did a connect() and instead got a 1552 * init/init-ack/cookie done before the 1553 * init-ack came back.. 1554 */ 1555 stcb->sctp_ep->sctp_flags |= 1556 SCTP_PCB_FLAGS_CONNECTED; 1557#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1558 so = SCTP_INP_SO(stcb->sctp_ep); 1559 atomic_add_int(&stcb->asoc.refcnt, 1); 1560 SCTP_TCB_UNLOCK(stcb); 1561 SCTP_SOCKET_LOCK(so, 1); 1562 SCTP_TCB_LOCK(stcb); 1563 atomic_add_int(&stcb->asoc.refcnt, -1); 1564 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1565 SCTP_SOCKET_UNLOCK(so, 1); 1566 return (NULL); 1567 } 1568#endif 1569 soisconnected(stcb->sctp_socket); 1570#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1571 SCTP_SOCKET_UNLOCK(so, 1); 1572#endif 1573 } 1574 /* notify upper layer */ 1575 *notification = SCTP_NOTIFY_ASSOC_UP; 1576 /* 1577 * since we did not send a HB make sure we don't 1578 * double things 1579 */ 1580 net->hb_responded = 1; 1581 net->RTO = sctp_calculate_rto(stcb, asoc, net, 1582 &cookie->time_entered, 1583 sctp_align_unsafe_makecopy, 1584 SCTP_RTT_FROM_NON_DATA); 1585 1586 if (stcb->asoc.sctp_autoclose_ticks && 1587 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1588 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1589 inp, stcb, NULL); 1590 } 1591 break; 1592 default: 1593 /* 1594 * we're in the OPEN state (or beyond), so peer must 1595 * have simply lost the COOKIE-ACK 1596 */ 1597 break; 1598 } /* end switch */ 1599 sctp_stop_all_cookie_timers(stcb); 1600 /* 1601 * We ignore the return code here.. not sure if we should 1602 * somehow abort.. but we do have an existing asoc. This 1603 * really should not fail. 1604 */ 1605 if (sctp_load_addresses_from_init(stcb, m, 1606 init_offset + sizeof(struct sctp_init_chunk), 1607 initack_offset, sh, init_src)) { 1608 if (how_indx < sizeof(asoc->cookie_how)) 1609 asoc->cookie_how[how_indx] = 4; 1610 return (NULL); 1611 } 1612 /* respond with a COOKIE-ACK */ 1613 sctp_toss_old_cookies(stcb, asoc); 1614 sctp_send_cookie_ack(stcb); 1615 if (how_indx < sizeof(asoc->cookie_how)) 1616 asoc->cookie_how[how_indx] = 5; 1617 return (stcb); 1618 } 1619 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1620 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1621 cookie->tie_tag_my_vtag == 0 && 1622 cookie->tie_tag_peer_vtag == 0) { 1623 /* 1624 * case C in Section 5.2.4 Table 2: XMOO silently discard 1625 */ 1626 if (how_indx < sizeof(asoc->cookie_how)) 1627 asoc->cookie_how[how_indx] = 6; 1628 return (NULL); 1629 } 1630 /* 1631 * If nat support, and the below and stcb is established, send back 1632 * a ABORT(colliding state) if we are established. 1633 */ 1634 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && 1635 (asoc->peer_supports_nat) && 1636 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1637 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1638 (asoc->peer_vtag == 0)))) { 1639 /* 1640 * Special case - Peer's support nat. We may have two init's 1641 * that we gave out the same tag on since one was not 1642 * established.. i.e. we get INIT from host-1 behind the nat 1643 * and we respond tag-a, we get a INIT from host-2 behind 1644 * the nat and we get tag-a again. Then we bring up host-1 1645 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1646 * Now we have colliding state. We must send an abort here 1647 * with colliding state indication. 1648 */ 1649 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 1650 0, M_DONTWAIT, 1, MT_DATA); 1651 if (op_err == NULL) { 1652 /* FOOBAR */ 1653 return (NULL); 1654 } 1655 /* pre-reserve some space */ 1656#ifdef INET6 1657 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 1658#else 1659 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 1660#endif 1661 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 1662 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1663 /* Set the len */ 1664 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); 1665 ph = mtod(op_err, struct sctp_paramhdr *); 1666 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); 1667 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 1668 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port); 1669 return (NULL); 1670 } 1671 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1672 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1673 (asoc->peer_vtag == 0))) { 1674 /* 1675 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1676 * should be ok, re-accept peer info 1677 */ 1678 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1679 /* 1680 * Extension of case C. If we hit this, then the 1681 * random number generator returned the same vtag 1682 * when we first sent our INIT-ACK and when we later 1683 * sent our INIT. The side with the seq numbers that 1684 * are different will be the one that normnally 1685 * would have hit case C. This in effect "extends" 1686 * our vtags in this collision case to be 64 bits. 1687 * The same collision could occur aka you get both 1688 * vtag and seq number the same twice in a row.. but 1689 * is much less likely. If it did happen then we 1690 * would proceed through and bring up the assoc.. we 1691 * may end up with the wrong stream setup however.. 1692 * which would be bad.. but there is no way to 1693 * tell.. until we send on a stream that does not 1694 * exist :-) 1695 */ 1696 if (how_indx < sizeof(asoc->cookie_how)) 1697 asoc->cookie_how[how_indx] = 7; 1698 1699 return (NULL); 1700 } 1701 if (how_indx < sizeof(asoc->cookie_how)) 1702 asoc->cookie_how[how_indx] = 8; 1703 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1704 sctp_stop_all_cookie_timers(stcb); 1705 /* 1706 * since we did not send a HB make sure we don't double 1707 * things 1708 */ 1709 net->hb_responded = 1; 1710 if (stcb->asoc.sctp_autoclose_ticks && 1711 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1712 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1713 NULL); 1714 } 1715 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1716 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 1717 1718 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 1719 /* 1720 * Ok the peer probably discarded our data (if we 1721 * echoed a cookie+data). So anything on the 1722 * sent_queue should be marked for retransmit, we 1723 * may not get something to kick us so it COULD 1724 * still take a timeout to move these.. but it can't 1725 * hurt to mark them. 1726 */ 1727 struct sctp_tmit_chunk *chk; 1728 1729 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 1730 if (chk->sent < SCTP_DATAGRAM_RESEND) { 1731 chk->sent = SCTP_DATAGRAM_RESEND; 1732 sctp_flight_size_decrease(chk); 1733 sctp_total_flight_decrease(stcb, chk); 1734 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1735 spec_flag++; 1736 } 1737 } 1738 1739 } 1740 /* process the INIT info (peer's info) */ 1741 retval = sctp_process_init(init_cp, stcb); 1742 if (retval < 0) { 1743 if (how_indx < sizeof(asoc->cookie_how)) 1744 asoc->cookie_how[how_indx] = 9; 1745 return (NULL); 1746 } 1747 if (sctp_load_addresses_from_init(stcb, m, 1748 init_offset + sizeof(struct sctp_init_chunk), 1749 initack_offset, sh, init_src)) { 1750 if (how_indx < sizeof(asoc->cookie_how)) 1751 asoc->cookie_how[how_indx] = 10; 1752 return (NULL); 1753 } 1754 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || 1755 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { 1756 *notification = SCTP_NOTIFY_ASSOC_UP; 1757 1758 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1759 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 1760 (inp->sctp_socket->so_qlimit == 0)) { 1761#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1762 struct socket *so; 1763 1764#endif 1765 stcb->sctp_ep->sctp_flags |= 1766 SCTP_PCB_FLAGS_CONNECTED; 1767#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1768 so = SCTP_INP_SO(stcb->sctp_ep); 1769 atomic_add_int(&stcb->asoc.refcnt, 1); 1770 SCTP_TCB_UNLOCK(stcb); 1771 SCTP_SOCKET_LOCK(so, 1); 1772 SCTP_TCB_LOCK(stcb); 1773 atomic_add_int(&stcb->asoc.refcnt, -1); 1774 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1775 SCTP_SOCKET_UNLOCK(so, 1); 1776 return (NULL); 1777 } 1778#endif 1779 soisconnected(stcb->sctp_socket); 1780#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1781 SCTP_SOCKET_UNLOCK(so, 1); 1782#endif 1783 } 1784 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) 1785 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1786 else 1787 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1788 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1789 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1790 SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1791 } else { 1792 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1793 } 1794 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1795 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1796 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1797 stcb->sctp_ep, stcb, asoc->primary_destination); 1798 } 1799 sctp_stop_all_cookie_timers(stcb); 1800 sctp_toss_old_cookies(stcb, asoc); 1801 sctp_send_cookie_ack(stcb); 1802 if (spec_flag) { 1803 /* 1804 * only if we have retrans set do we do this. What 1805 * this call does is get only the COOKIE-ACK out and 1806 * then when we return the normal call to 1807 * sctp_chunk_output will get the retrans out behind 1808 * this. 1809 */ 1810 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 1811 } 1812 if (how_indx < sizeof(asoc->cookie_how)) 1813 asoc->cookie_how[how_indx] = 11; 1814 1815 return (stcb); 1816 } 1817 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1818 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1819 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1820 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1821 cookie->tie_tag_peer_vtag != 0) { 1822 struct sctpasochead *head; 1823 1824 if (asoc->peer_supports_nat) { 1825 /* 1826 * This is a gross gross hack. just call the 1827 * cookie_new code since we are allowing a duplicate 1828 * association. I hope this works... 1829 */ 1830 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len, 1831 inp, netp, init_src, notification, 1832 auth_skipped, auth_offset, auth_len, 1833 vrf_id, port)); 1834 } 1835 /* 1836 * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1837 */ 1838 /* temp code */ 1839 if (how_indx < sizeof(asoc->cookie_how)) 1840 asoc->cookie_how[how_indx] = 12; 1841 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1842 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 1843 1844 /* notify upper layer */ 1845 *notification = SCTP_NOTIFY_ASSOC_RESTART; 1846 atomic_add_int(&stcb->asoc.refcnt, 1); 1847 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && 1848 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1849 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { 1850 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1851 } 1852 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 1853 SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1854 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1855 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1856 } 1857 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1858 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1859 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1860 stcb->sctp_ep, stcb, asoc->primary_destination); 1861 1862 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { 1863 /* move to OPEN state, if not in SHUTDOWN_SENT */ 1864 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 1865 } 1866 asoc->pre_open_streams = 1867 ntohs(initack_cp->init.num_outbound_streams); 1868 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1869 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1870 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1871 1872 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1873 1874 asoc->str_reset_seq_in = asoc->init_seq_number; 1875 1876 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 1877 if (asoc->mapping_array) { 1878 memset(asoc->mapping_array, 0, 1879 asoc->mapping_array_size); 1880 } 1881 if (asoc->nr_mapping_array) { 1882 memset(asoc->nr_mapping_array, 0, 1883 asoc->mapping_array_size); 1884 } 1885 SCTP_TCB_UNLOCK(stcb); 1886 SCTP_INP_INFO_WLOCK(); 1887 SCTP_INP_WLOCK(stcb->sctp_ep); 1888 SCTP_TCB_LOCK(stcb); 1889 atomic_add_int(&stcb->asoc.refcnt, -1); 1890 /* send up all the data */ 1891 SCTP_TCB_SEND_LOCK(stcb); 1892 1893 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED); 1894 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1895 stcb->asoc.strmout[i].stream_no = i; 1896 stcb->asoc.strmout[i].next_sequence_sent = 0; 1897 stcb->asoc.strmout[i].last_msg_incomplete = 0; 1898 } 1899 /* process the INIT-ACK info (my info) */ 1900 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1901 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1902 1903 /* pull from vtag hash */ 1904 LIST_REMOVE(stcb, sctp_asocs); 1905 /* re-insert to new vtag position */ 1906 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1907 SCTP_BASE_INFO(hashasocmark))]; 1908 /* 1909 * put it in the bucket in the vtag hash of assoc's for the 1910 * system 1911 */ 1912 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1913 1914 /* process the INIT info (peer's info) */ 1915 SCTP_TCB_SEND_UNLOCK(stcb); 1916 SCTP_INP_WUNLOCK(stcb->sctp_ep); 1917 SCTP_INP_INFO_WUNLOCK(); 1918 1919 retval = sctp_process_init(init_cp, stcb); 1920 if (retval < 0) { 1921 if (how_indx < sizeof(asoc->cookie_how)) 1922 asoc->cookie_how[how_indx] = 13; 1923 1924 return (NULL); 1925 } 1926 /* 1927 * since we did not send a HB make sure we don't double 1928 * things 1929 */ 1930 net->hb_responded = 1; 1931 1932 if (sctp_load_addresses_from_init(stcb, m, 1933 init_offset + sizeof(struct sctp_init_chunk), 1934 initack_offset, sh, init_src)) { 1935 if (how_indx < sizeof(asoc->cookie_how)) 1936 asoc->cookie_how[how_indx] = 14; 1937 1938 return (NULL); 1939 } 1940 /* respond with a COOKIE-ACK */ 1941 sctp_stop_all_cookie_timers(stcb); 1942 sctp_toss_old_cookies(stcb, asoc); 1943 sctp_send_cookie_ack(stcb); 1944 if (how_indx < sizeof(asoc->cookie_how)) 1945 asoc->cookie_how[how_indx] = 15; 1946 1947 return (stcb); 1948 } 1949 if (how_indx < sizeof(asoc->cookie_how)) 1950 asoc->cookie_how[how_indx] = 16; 1951 /* all other cases... */ 1952 return (NULL); 1953} 1954 1955 1956/* 1957 * handle a state cookie for a new association m: input packet mbuf chain-- 1958 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1959 * and the cookie signature does not exist offset: offset into mbuf to the 1960 * cookie-echo chunk length: length of the cookie chunk to: where the init 1961 * was from returns a new TCB 1962 */ 1963struct sctp_tcb * 1964sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1965 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1966 struct sctp_inpcb *inp, struct sctp_nets **netp, 1967 struct sockaddr *init_src, int *notification, 1968 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1969 uint32_t vrf_id, uint16_t port) 1970{ 1971 struct sctp_tcb *stcb; 1972 struct sctp_init_chunk *init_cp, init_buf; 1973 struct sctp_init_ack_chunk *initack_cp, initack_buf; 1974 struct sockaddr_storage sa_store; 1975 struct sockaddr *initack_src = (struct sockaddr *)&sa_store; 1976 struct sctp_association *asoc; 1977 int init_offset, initack_offset, initack_limit; 1978 int retval; 1979 int error = 0; 1980 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; 1981 1982#ifdef INET 1983 struct sockaddr_in *sin; 1984 1985#endif 1986#ifdef INET6 1987 struct sockaddr_in6 *sin6; 1988 1989#endif 1990#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1991 struct socket *so; 1992 1993 so = SCTP_INP_SO(inp); 1994#endif 1995 1996 /* 1997 * find and validate the INIT chunk in the cookie (peer's info) the 1998 * INIT should start after the cookie-echo header struct (chunk 1999 * header, state cookie header struct) 2000 */ 2001 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2002 init_cp = (struct sctp_init_chunk *) 2003 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2004 (uint8_t *) & init_buf); 2005 if (init_cp == NULL) { 2006 /* could not pull a INIT chunk in cookie */ 2007 SCTPDBG(SCTP_DEBUG_INPUT1, 2008 "process_cookie_new: could not pull INIT chunk hdr\n"); 2009 return (NULL); 2010 } 2011 if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2012 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2013 return (NULL); 2014 } 2015 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2016 /* 2017 * find and validate the INIT-ACK chunk in the cookie (my info) the 2018 * INIT-ACK follows the INIT chunk 2019 */ 2020 initack_cp = (struct sctp_init_ack_chunk *) 2021 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2022 (uint8_t *) & initack_buf); 2023 if (initack_cp == NULL) { 2024 /* could not pull INIT-ACK chunk in cookie */ 2025 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2026 return (NULL); 2027 } 2028 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2029 return (NULL); 2030 } 2031 /* 2032 * NOTE: We can't use the INIT_ACK's chk_length to determine the 2033 * "initack_limit" value. This is because the chk_length field 2034 * includes the length of the cookie, but the cookie is omitted when 2035 * the INIT and INIT_ACK are tacked onto the cookie... 2036 */ 2037 initack_limit = offset + cookie_len; 2038 2039 /* 2040 * now that we know the INIT/INIT-ACK are in place, create a new TCB 2041 * and popluate 2042 */ 2043 2044 /* 2045 * Here we do a trick, we set in NULL for the proc/thread argument. 2046 * We do this since in effect we only use the p argument when the 2047 * socket is unbound and we must do an implicit bind. Since we are 2048 * getting a cookie, we cannot be unbound. 2049 */ 2050 stcb = sctp_aloc_assoc(inp, init_src, &error, 2051 ntohl(initack_cp->init.initiate_tag), vrf_id, 2052 (struct thread *)NULL 2053 ); 2054 if (stcb == NULL) { 2055 struct mbuf *op_err; 2056 2057 /* memory problem? */ 2058 SCTPDBG(SCTP_DEBUG_INPUT1, 2059 "process_cookie_new: no room for another TCB!\n"); 2060 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2061 2062 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2063 sh, op_err, vrf_id, port); 2064 return (NULL); 2065 } 2066 /* get the correct sctp_nets */ 2067 if (netp) 2068 *netp = sctp_findnet(stcb, init_src); 2069 2070 asoc = &stcb->asoc; 2071 /* get scope variables out of cookie */ 2072 asoc->ipv4_local_scope = cookie->ipv4_scope; 2073 asoc->site_scope = cookie->site_scope; 2074 asoc->local_scope = cookie->local_scope; 2075 asoc->loopback_scope = cookie->loopback_scope; 2076 2077 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) || 2078 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2079 struct mbuf *op_err; 2080 2081 /* 2082 * Houston we have a problem. The EP changed while the 2083 * cookie was in flight. Only recourse is to abort the 2084 * association. 2085 */ 2086 atomic_add_int(&stcb->asoc.refcnt, 1); 2087 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2088 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2089 sh, op_err, vrf_id, port); 2090#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2091 SCTP_TCB_UNLOCK(stcb); 2092 SCTP_SOCKET_LOCK(so, 1); 2093 SCTP_TCB_LOCK(stcb); 2094#endif 2095 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2096 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2097#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2098 SCTP_SOCKET_UNLOCK(so, 1); 2099#endif 2100 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2101 return (NULL); 2102 } 2103 /* process the INIT-ACK info (my info) */ 2104 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 2105 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2106 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); 2107 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 2108 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 2109 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 2110 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 2111 asoc->str_reset_seq_in = asoc->init_seq_number; 2112 2113 asoc->advanced_peer_ack_point = asoc->last_acked_seq; 2114 2115 /* process the INIT info (peer's info) */ 2116 if (netp) 2117 retval = sctp_process_init(init_cp, stcb); 2118 else 2119 retval = 0; 2120 if (retval < 0) { 2121 atomic_add_int(&stcb->asoc.refcnt, 1); 2122#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2123 SCTP_TCB_UNLOCK(stcb); 2124 SCTP_SOCKET_LOCK(so, 1); 2125 SCTP_TCB_LOCK(stcb); 2126#endif 2127 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16); 2128#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2129 SCTP_SOCKET_UNLOCK(so, 1); 2130#endif 2131 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2132 return (NULL); 2133 } 2134 /* load all addresses */ 2135 if (sctp_load_addresses_from_init(stcb, m, 2136 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh, 2137 init_src)) { 2138 atomic_add_int(&stcb->asoc.refcnt, 1); 2139#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2140 SCTP_TCB_UNLOCK(stcb); 2141 SCTP_SOCKET_LOCK(so, 1); 2142 SCTP_TCB_LOCK(stcb); 2143#endif 2144 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17); 2145#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2146 SCTP_SOCKET_UNLOCK(so, 1); 2147#endif 2148 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2149 return (NULL); 2150 } 2151 /* 2152 * verify any preceding AUTH chunk that was skipped 2153 */ 2154 /* pull the local authentication parameters from the cookie/init-ack */ 2155 sctp_auth_get_cookie_params(stcb, m, 2156 initack_offset + sizeof(struct sctp_init_ack_chunk), 2157 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2158 if (auth_skipped) { 2159 struct sctp_auth_chunk *auth; 2160 2161 auth = (struct sctp_auth_chunk *) 2162 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 2163 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2164 /* auth HMAC failed, dump the assoc and packet */ 2165 SCTPDBG(SCTP_DEBUG_AUTH1, 2166 "COOKIE-ECHO: AUTH failed\n"); 2167 atomic_add_int(&stcb->asoc.refcnt, 1); 2168#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2169 SCTP_TCB_UNLOCK(stcb); 2170 SCTP_SOCKET_LOCK(so, 1); 2171 SCTP_TCB_LOCK(stcb); 2172#endif 2173 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2174#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2175 SCTP_SOCKET_UNLOCK(so, 1); 2176#endif 2177 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2178 return (NULL); 2179 } else { 2180 /* remaining chunks checked... good to go */ 2181 stcb->asoc.authenticated = 1; 2182 } 2183 } 2184 /* update current state */ 2185 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2186 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2187 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2188 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2189 stcb->sctp_ep, stcb, asoc->primary_destination); 2190 } 2191 sctp_stop_all_cookie_timers(stcb); 2192 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 2193 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2194 2195 /* 2196 * if we're doing ASCONFs, check to see if we have any new local 2197 * addresses that need to get added to the peer (eg. addresses 2198 * changed while cookie echo in flight). This needs to be done 2199 * after we go to the OPEN state to do the correct asconf 2200 * processing. else, make sure we have the correct addresses in our 2201 * lists 2202 */ 2203 2204 /* warning, we re-use sin, sin6, sa_store here! */ 2205 /* pull in local_address (our "from" address) */ 2206 switch (cookie->laddr_type) { 2207#ifdef INET 2208 case SCTP_IPV4_ADDRESS: 2209 /* source addr is IPv4 */ 2210 sin = (struct sockaddr_in *)initack_src; 2211 memset(sin, 0, sizeof(*sin)); 2212 sin->sin_family = AF_INET; 2213 sin->sin_len = sizeof(struct sockaddr_in); 2214 sin->sin_addr.s_addr = cookie->laddress[0]; 2215 break; 2216#endif 2217#ifdef INET6 2218 case SCTP_IPV6_ADDRESS: 2219 /* source addr is IPv6 */ 2220 sin6 = (struct sockaddr_in6 *)initack_src; 2221 memset(sin6, 0, sizeof(*sin6)); 2222 sin6->sin6_family = AF_INET6; 2223 sin6->sin6_len = sizeof(struct sockaddr_in6); 2224 sin6->sin6_scope_id = cookie->scope_id; 2225 memcpy(&sin6->sin6_addr, cookie->laddress, 2226 sizeof(sin6->sin6_addr)); 2227 break; 2228#endif 2229 default: 2230 atomic_add_int(&stcb->asoc.refcnt, 1); 2231#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2232 SCTP_TCB_UNLOCK(stcb); 2233 SCTP_SOCKET_LOCK(so, 1); 2234 SCTP_TCB_LOCK(stcb); 2235#endif 2236 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2237#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2238 SCTP_SOCKET_UNLOCK(so, 1); 2239#endif 2240 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2241 return (NULL); 2242 } 2243 2244 /* set up to notify upper layer */ 2245 *notification = SCTP_NOTIFY_ASSOC_UP; 2246 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2247 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 2248 (inp->sctp_socket->so_qlimit == 0)) { 2249 /* 2250 * This is an endpoint that called connect() how it got a 2251 * cookie that is NEW is a bit of a mystery. It must be that 2252 * the INIT was sent, but before it got there.. a complete 2253 * INIT/INIT-ACK/COOKIE arrived. But of course then it 2254 * should have went to the other code.. not here.. oh well.. 2255 * a bit of protection is worth having.. 2256 */ 2257 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2258#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2259 atomic_add_int(&stcb->asoc.refcnt, 1); 2260 SCTP_TCB_UNLOCK(stcb); 2261 SCTP_SOCKET_LOCK(so, 1); 2262 SCTP_TCB_LOCK(stcb); 2263 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2264 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2265 SCTP_SOCKET_UNLOCK(so, 1); 2266 return (NULL); 2267 } 2268#endif 2269 soisconnected(stcb->sctp_socket); 2270#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2271 SCTP_SOCKET_UNLOCK(so, 1); 2272#endif 2273 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 2274 (inp->sctp_socket->so_qlimit)) { 2275 /* 2276 * We don't want to do anything with this one. Since it is 2277 * the listening guy. The timer will get started for 2278 * accepted connections in the caller. 2279 */ 2280 ; 2281 } 2282 /* since we did not send a HB make sure we don't double things */ 2283 if ((netp) && (*netp)) 2284 (*netp)->hb_responded = 1; 2285 2286 if (stcb->asoc.sctp_autoclose_ticks && 2287 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2288 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2289 } 2290 /* calculate the RTT */ 2291 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2292 if ((netp) && (*netp)) { 2293 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, 2294 &cookie->time_entered, sctp_align_unsafe_makecopy, 2295 SCTP_RTT_FROM_NON_DATA); 2296 } 2297 /* respond with a COOKIE-ACK */ 2298 sctp_send_cookie_ack(stcb); 2299 2300 /* 2301 * check the address lists for any ASCONFs that need to be sent 2302 * AFTER the cookie-ack is sent 2303 */ 2304 sctp_check_address_list(stcb, m, 2305 initack_offset + sizeof(struct sctp_init_ack_chunk), 2306 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 2307 initack_src, cookie->local_scope, cookie->site_scope, 2308 cookie->ipv4_scope, cookie->loopback_scope); 2309 2310 2311 return (stcb); 2312} 2313 2314/* 2315 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2316 * we NEED to make sure we are not already using the vtag. If so we 2317 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2318 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2319 SCTP_BASE_INFO(hashasocmark))]; 2320 LIST_FOREACH(stcb, head, sctp_asocs) { 2321 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2322 -- SEND ABORT - TRY AGAIN -- 2323 } 2324 } 2325*/ 2326 2327/* 2328 * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2329 * existing (non-NULL) TCB 2330 */ 2331static struct mbuf * 2332sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2333 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2334 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 2335 int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2336 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port) 2337{ 2338 struct sctp_state_cookie *cookie; 2339 struct sctp_tcb *l_stcb = *stcb; 2340 struct sctp_inpcb *l_inp; 2341 struct sockaddr *to; 2342 struct sctp_pcb *ep; 2343 struct mbuf *m_sig; 2344 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2345 uint8_t *sig; 2346 uint8_t cookie_ok = 0; 2347 unsigned int size_of_pkt, sig_offset, cookie_offset; 2348 unsigned int cookie_len; 2349 struct timeval now; 2350 struct timeval time_expires; 2351 struct sockaddr_storage dest_store; 2352 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store; 2353 struct ip *iph; 2354 int notification = 0; 2355 struct sctp_nets *netl; 2356 int had_a_existing_tcb = 0; 2357 int send_int_conf = 0; 2358 2359#ifdef INET 2360 struct sockaddr_in sin; 2361 2362#endif 2363#ifdef INET6 2364 struct sockaddr_in6 sin6; 2365 2366#endif 2367 2368 SCTPDBG(SCTP_DEBUG_INPUT2, 2369 "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2370 2371 if (inp_p == NULL) { 2372 return (NULL); 2373 } 2374 /* First get the destination address setup too. */ 2375 iph = mtod(m, struct ip *); 2376 switch (iph->ip_v) { 2377#ifdef INET 2378 case IPVERSION: 2379 { 2380 /* its IPv4 */ 2381 struct sockaddr_in *lsin; 2382 2383 lsin = (struct sockaddr_in *)(localep_sa); 2384 memset(lsin, 0, sizeof(*lsin)); 2385 lsin->sin_family = AF_INET; 2386 lsin->sin_len = sizeof(*lsin); 2387 lsin->sin_port = sh->dest_port; 2388 lsin->sin_addr.s_addr = iph->ip_dst.s_addr; 2389 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph); 2390 break; 2391 } 2392#endif 2393#ifdef INET6 2394 case IPV6_VERSION >> 4: 2395 { 2396 /* its IPv6 */ 2397 struct ip6_hdr *ip6; 2398 struct sockaddr_in6 *lsin6; 2399 2400 lsin6 = (struct sockaddr_in6 *)(localep_sa); 2401 memset(lsin6, 0, sizeof(*lsin6)); 2402 lsin6->sin6_family = AF_INET6; 2403 lsin6->sin6_len = sizeof(struct sockaddr_in6); 2404 ip6 = mtod(m, struct ip6_hdr *); 2405 lsin6->sin6_port = sh->dest_port; 2406 lsin6->sin6_addr = ip6->ip6_dst; 2407 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen; 2408 break; 2409 } 2410#endif 2411 default: 2412 return (NULL); 2413 } 2414 2415 cookie = &cp->cookie; 2416 cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2417 cookie_len = ntohs(cp->ch.chunk_length); 2418 2419 if ((cookie->peerport != sh->src_port) && 2420 (cookie->myport != sh->dest_port) && 2421 (cookie->my_vtag != sh->v_tag)) { 2422 /* 2423 * invalid ports or bad tag. Note that we always leave the 2424 * v_tag in the header in network order and when we stored 2425 * it in the my_vtag slot we also left it in network order. 2426 * This maintains the match even though it may be in the 2427 * opposite byte order of the machine :-> 2428 */ 2429 return (NULL); 2430 } 2431 if (cookie_len > size_of_pkt || 2432 cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2433 sizeof(struct sctp_init_chunk) + 2434 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2435 /* cookie too long! or too small */ 2436 return (NULL); 2437 } 2438 /* 2439 * split off the signature into its own mbuf (since it should not be 2440 * calculated in the sctp_hmac_m() call). 2441 */ 2442 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2443 if (sig_offset > size_of_pkt) { 2444 /* packet not correct size! */ 2445 /* XXX this may already be accounted for earlier... */ 2446 return (NULL); 2447 } 2448 m_sig = m_split(m, sig_offset, M_DONTWAIT); 2449 if (m_sig == NULL) { 2450 /* out of memory or ?? */ 2451 return (NULL); 2452 } 2453#ifdef SCTP_MBUF_LOGGING 2454 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 2455 struct mbuf *mat; 2456 2457 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) { 2458 if (SCTP_BUF_IS_EXTENDED(mat)) { 2459 sctp_log_mb(mat, SCTP_MBUF_SPLIT); 2460 } 2461 } 2462 } 2463#endif 2464 2465 /* 2466 * compute the signature/digest for the cookie 2467 */ 2468 ep = &(*inp_p)->sctp_ep; 2469 l_inp = *inp_p; 2470 if (l_stcb) { 2471 SCTP_TCB_UNLOCK(l_stcb); 2472 } 2473 SCTP_INP_RLOCK(l_inp); 2474 if (l_stcb) { 2475 SCTP_TCB_LOCK(l_stcb); 2476 } 2477 /* which cookie is it? */ 2478 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2479 (ep->current_secret_number != ep->last_secret_number)) { 2480 /* it's the old cookie */ 2481 (void)sctp_hmac_m(SCTP_HMAC, 2482 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2483 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2484 } else { 2485 /* it's the current cookie */ 2486 (void)sctp_hmac_m(SCTP_HMAC, 2487 (uint8_t *) ep->secret_key[(int)ep->current_secret_number], 2488 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2489 } 2490 /* get the signature */ 2491 SCTP_INP_RUNLOCK(l_inp); 2492 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); 2493 if (sig == NULL) { 2494 /* couldn't find signature */ 2495 sctp_m_freem(m_sig); 2496 return (NULL); 2497 } 2498 /* compare the received digest with the computed digest */ 2499 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2500 /* try the old cookie? */ 2501 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2502 (ep->current_secret_number != ep->last_secret_number)) { 2503 /* compute digest with old */ 2504 (void)sctp_hmac_m(SCTP_HMAC, 2505 (uint8_t *) ep->secret_key[(int)ep->last_secret_number], 2506 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2507 /* compare */ 2508 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2509 cookie_ok = 1; 2510 } 2511 } else { 2512 cookie_ok = 1; 2513 } 2514 2515 /* 2516 * Now before we continue we must reconstruct our mbuf so that 2517 * normal processing of any other chunks will work. 2518 */ 2519 { 2520 struct mbuf *m_at; 2521 2522 m_at = m; 2523 while (SCTP_BUF_NEXT(m_at) != NULL) { 2524 m_at = SCTP_BUF_NEXT(m_at); 2525 } 2526 SCTP_BUF_NEXT(m_at) = m_sig; 2527 } 2528 2529 if (cookie_ok == 0) { 2530 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2531 SCTPDBG(SCTP_DEBUG_INPUT2, 2532 "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2533 (uint32_t) offset, cookie_offset, sig_offset); 2534 return (NULL); 2535 } 2536 /* 2537 * check the cookie timestamps to be sure it's not stale 2538 */ 2539 (void)SCTP_GETTIME_TIMEVAL(&now); 2540 /* Expire time is in Ticks, so we convert to seconds */ 2541 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); 2542 time_expires.tv_usec = cookie->time_entered.tv_usec; 2543 /* 2544 * TODO sctp_constants.h needs alternative time macros when _KERNEL 2545 * is undefined. 2546 */ 2547 if (timevalcmp(&now, &time_expires, >)) { 2548 /* cookie is stale! */ 2549 struct mbuf *op_err; 2550 struct sctp_stale_cookie_msg *scm; 2551 uint32_t tim; 2552 2553 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), 2554 0, M_DONTWAIT, 1, MT_DATA); 2555 if (op_err == NULL) { 2556 /* FOOBAR */ 2557 return (NULL); 2558 } 2559 /* Set the len */ 2560 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); 2561 scm = mtod(op_err, struct sctp_stale_cookie_msg *); 2562 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); 2563 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + 2564 (sizeof(uint32_t)))); 2565 /* seconds to usec */ 2566 tim = (now.tv_sec - time_expires.tv_sec) * 1000000; 2567 /* add in usec */ 2568 if (tim == 0) 2569 tim = now.tv_usec - cookie->time_entered.tv_usec; 2570 scm->time_usec = htonl(tim); 2571 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag, 2572 vrf_id, port); 2573 return (NULL); 2574 } 2575 /* 2576 * Now we must see with the lookup address if we have an existing 2577 * asoc. This will only happen if we were in the COOKIE-WAIT state 2578 * and a INIT collided with us and somewhere the peer sent the 2579 * cookie on another address besides the single address our assoc 2580 * had for him. In this case we will have one of the tie-tags set at 2581 * least AND the address field in the cookie can be used to look it 2582 * up. 2583 */ 2584 to = NULL; 2585 switch (cookie->addr_type) { 2586#ifdef INET6 2587 case SCTP_IPV6_ADDRESS: 2588 memset(&sin6, 0, sizeof(sin6)); 2589 sin6.sin6_family = AF_INET6; 2590 sin6.sin6_len = sizeof(sin6); 2591 sin6.sin6_port = sh->src_port; 2592 sin6.sin6_scope_id = cookie->scope_id; 2593 memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2594 sizeof(sin6.sin6_addr.s6_addr)); 2595 to = (struct sockaddr *)&sin6; 2596 break; 2597#endif 2598#ifdef INET 2599 case SCTP_IPV4_ADDRESS: 2600 memset(&sin, 0, sizeof(sin)); 2601 sin.sin_family = AF_INET; 2602 sin.sin_len = sizeof(sin); 2603 sin.sin_port = sh->src_port; 2604 sin.sin_addr.s_addr = cookie->address[0]; 2605 to = (struct sockaddr *)&sin; 2606 break; 2607#endif 2608 default: 2609 /* This should not happen */ 2610 return (NULL); 2611 } 2612 if ((*stcb == NULL) && to) { 2613 /* Yep, lets check */ 2614 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL); 2615 if (*stcb == NULL) { 2616 /* 2617 * We should have only got back the same inp. If we 2618 * got back a different ep we have a problem. The 2619 * original findep got back l_inp and now 2620 */ 2621 if (l_inp != *inp_p) { 2622 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2623 } 2624 } else { 2625 if (*locked_tcb == NULL) { 2626 /* 2627 * In this case we found the assoc only 2628 * after we locked the create lock. This 2629 * means we are in a colliding case and we 2630 * must make sure that we unlock the tcb if 2631 * its one of the cases where we throw away 2632 * the incoming packets. 2633 */ 2634 *locked_tcb = *stcb; 2635 2636 /* 2637 * We must also increment the inp ref count 2638 * since the ref_count flags was set when we 2639 * did not find the TCB, now we found it 2640 * which reduces the refcount.. we must 2641 * raise it back out to balance it all :-) 2642 */ 2643 SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2644 if ((*stcb)->sctp_ep != l_inp) { 2645 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2646 (*stcb)->sctp_ep, l_inp); 2647 } 2648 } 2649 } 2650 } 2651 if (to == NULL) { 2652 return (NULL); 2653 } 2654 cookie_len -= SCTP_SIGNATURE_SIZE; 2655 if (*stcb == NULL) { 2656 /* this is the "normal" case... get a new TCB */ 2657 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie, 2658 cookie_len, *inp_p, netp, to, ¬ification, 2659 auth_skipped, auth_offset, auth_len, vrf_id, port); 2660 } else { 2661 /* this is abnormal... cookie-echo on existing TCB */ 2662 had_a_existing_tcb = 1; 2663 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh, 2664 cookie, cookie_len, *inp_p, *stcb, netp, to, 2665 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port); 2666 } 2667 2668 if (*stcb == NULL) { 2669 /* still no TCB... must be bad cookie-echo */ 2670 return (NULL); 2671 } 2672 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) { 2673 (*netp)->flowid = m->m_pkthdr.flowid; 2674#ifdef INVARIANTS 2675 (*netp)->flowidset = 1; 2676#endif 2677 } 2678 /* 2679 * Ok, we built an association so confirm the address we sent the 2680 * INIT-ACK to. 2681 */ 2682 netl = sctp_findnet(*stcb, to); 2683 /* 2684 * This code should in theory NOT run but 2685 */ 2686 if (netl == NULL) { 2687 /* TSNH! Huh, why do I need to add this address here? */ 2688 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 2689 return (NULL); 2690 } 2691 netl = sctp_findnet(*stcb, to); 2692 } 2693 if (netl) { 2694 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2695 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2696 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2697 netl); 2698 send_int_conf = 1; 2699 } 2700 } 2701 sctp_start_net_timers(*stcb); 2702 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2703 if (!had_a_existing_tcb || 2704 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2705 /* 2706 * If we have a NEW cookie or the connect never 2707 * reached the connected state during collision we 2708 * must do the TCP accept thing. 2709 */ 2710 struct socket *so, *oso; 2711 struct sctp_inpcb *inp; 2712 2713 if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2714 /* 2715 * For a restart we will keep the same 2716 * socket, no need to do anything. I THINK!! 2717 */ 2718 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2719 if (send_int_conf) { 2720 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2721 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2722 } 2723 return (m); 2724 } 2725 oso = (*inp_p)->sctp_socket; 2726 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2727 SCTP_TCB_UNLOCK((*stcb)); 2728 CURVNET_SET(oso->so_vnet); 2729 so = sonewconn(oso, 0 2730 ); 2731 CURVNET_RESTORE(); 2732 SCTP_TCB_LOCK((*stcb)); 2733 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2734 2735 if (so == NULL) { 2736 struct mbuf *op_err; 2737 2738#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2739 struct socket *pcb_so; 2740 2741#endif 2742 /* Too many sockets */ 2743 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2744 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 2745 sctp_abort_association(*inp_p, NULL, m, iphlen, 2746 sh, op_err, vrf_id, port); 2747#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2748 pcb_so = SCTP_INP_SO(*inp_p); 2749 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2750 SCTP_TCB_UNLOCK((*stcb)); 2751 SCTP_SOCKET_LOCK(pcb_so, 1); 2752 SCTP_TCB_LOCK((*stcb)); 2753 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2754#endif 2755 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2756#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2757 SCTP_SOCKET_UNLOCK(pcb_so, 1); 2758#endif 2759 return (NULL); 2760 } 2761 inp = (struct sctp_inpcb *)so->so_pcb; 2762 SCTP_INP_INCR_REF(inp); 2763 /* 2764 * We add the unbound flag here so that if we get an 2765 * soabort() before we get the move_pcb done, we 2766 * will properly cleanup. 2767 */ 2768 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2769 SCTP_PCB_FLAGS_CONNECTED | 2770 SCTP_PCB_FLAGS_IN_TCPPOOL | 2771 SCTP_PCB_FLAGS_UNBOUND | 2772 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2773 SCTP_PCB_FLAGS_DONT_WAKE); 2774 inp->sctp_features = (*inp_p)->sctp_features; 2775 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2776 inp->sctp_socket = so; 2777 inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 2778 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2779 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; 2780 inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2781 inp->sctp_context = (*inp_p)->sctp_context; 2782 inp->local_strreset_support = (*inp_p)->local_strreset_support; 2783 inp->inp_starting_point_for_iterator = NULL; 2784 /* 2785 * copy in the authentication parameters from the 2786 * original endpoint 2787 */ 2788 if (inp->sctp_ep.local_hmacs) 2789 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2790 inp->sctp_ep.local_hmacs = 2791 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2792 if (inp->sctp_ep.local_auth_chunks) 2793 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2794 inp->sctp_ep.local_auth_chunks = 2795 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2796 2797 /* 2798 * Now we must move it from one hash table to 2799 * another and get the tcb in the right place. 2800 */ 2801 2802 /* 2803 * This is where the one-2-one socket is put into 2804 * the accept state waiting for the accept! 2805 */ 2806 if (*stcb) { 2807 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; 2808 } 2809 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2810 2811 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2812 SCTP_TCB_UNLOCK((*stcb)); 2813 2814 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2815 0); 2816 SCTP_TCB_LOCK((*stcb)); 2817 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2818 2819 2820 /* 2821 * now we must check to see if we were aborted while 2822 * the move was going on and the lock/unlock 2823 * happened. 2824 */ 2825 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 2826 /* 2827 * yep it was, we leave the assoc attached 2828 * to the socket since the sctp_inpcb_free() 2829 * call will send an abort for us. 2830 */ 2831 SCTP_INP_DECR_REF(inp); 2832 return (NULL); 2833 } 2834 SCTP_INP_DECR_REF(inp); 2835 /* Switch over to the new guy */ 2836 *inp_p = inp; 2837 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2838 if (send_int_conf) { 2839 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2840 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2841 } 2842 /* 2843 * Pull it from the incomplete queue and wake the 2844 * guy 2845 */ 2846#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2847 atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2848 SCTP_TCB_UNLOCK((*stcb)); 2849 SCTP_SOCKET_LOCK(so, 1); 2850#endif 2851 soisconnected(so); 2852#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2853 SCTP_TCB_LOCK((*stcb)); 2854 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2855 SCTP_SOCKET_UNLOCK(so, 1); 2856#endif 2857 return (m); 2858 } 2859 } 2860 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 2861 if (notification) { 2862 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2863 } 2864 if (send_int_conf) { 2865 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 2866 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 2867 } 2868 } 2869 return (m); 2870} 2871 2872static void 2873sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2874 struct sctp_tcb *stcb, struct sctp_nets *net) 2875{ 2876 /* cp must not be used, others call this without a c-ack :-) */ 2877 struct sctp_association *asoc; 2878 2879 SCTPDBG(SCTP_DEBUG_INPUT2, 2880 "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2881 if (stcb == NULL) 2882 return; 2883 2884 asoc = &stcb->asoc; 2885 2886 sctp_stop_all_cookie_timers(stcb); 2887 /* process according to association state */ 2888 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { 2889 /* state change only needed when I am in right state */ 2890 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2891 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); 2892 sctp_start_net_timers(stcb); 2893 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2894 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 2895 stcb->sctp_ep, stcb, asoc->primary_destination); 2896 2897 } 2898 /* update RTO */ 2899 SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2900 SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2901 if (asoc->overall_error_count == 0) { 2902 net->RTO = sctp_calculate_rto(stcb, asoc, net, 2903 &asoc->time_entered, sctp_align_safe_nocopy, 2904 SCTP_RTT_FROM_NON_DATA); 2905 } 2906 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2907 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2908 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2909 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2910#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2911 struct socket *so; 2912 2913#endif 2914 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2915#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2916 so = SCTP_INP_SO(stcb->sctp_ep); 2917 atomic_add_int(&stcb->asoc.refcnt, 1); 2918 SCTP_TCB_UNLOCK(stcb); 2919 SCTP_SOCKET_LOCK(so, 1); 2920 SCTP_TCB_LOCK(stcb); 2921 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2922#endif 2923 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2924 soisconnected(stcb->sctp_socket); 2925 } 2926#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2927 SCTP_SOCKET_UNLOCK(so, 1); 2928#endif 2929 } 2930 /* 2931 * since we did not send a HB make sure we don't double 2932 * things 2933 */ 2934 net->hb_responded = 1; 2935 2936 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2937 /* 2938 * We don't need to do the asconf thing, nor hb or 2939 * autoclose if the socket is closed. 2940 */ 2941 goto closed_socket; 2942 } 2943 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2944 stcb, net); 2945 2946 2947 if (stcb->asoc.sctp_autoclose_ticks && 2948 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2949 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2950 stcb->sctp_ep, stcb, NULL); 2951 } 2952 /* 2953 * send ASCONF if parameters are pending and ASCONFs are 2954 * allowed (eg. addresses changed when init/cookie echo were 2955 * in flight) 2956 */ 2957 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2958 (stcb->asoc.peer_supports_asconf) && 2959 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 2960#ifdef SCTP_TIMER_BASED_ASCONF 2961 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2962 stcb->sctp_ep, stcb, 2963 stcb->asoc.primary_destination); 2964#else 2965 sctp_send_asconf(stcb, stcb->asoc.primary_destination, 2966 SCTP_ADDR_NOT_LOCKED); 2967#endif 2968 } 2969 } 2970closed_socket: 2971 /* Toss the cookie if I can */ 2972 sctp_toss_old_cookies(stcb, asoc); 2973 if (!TAILQ_EMPTY(&asoc->sent_queue)) { 2974 /* Restart the timer if we have pending data */ 2975 struct sctp_tmit_chunk *chk; 2976 2977 chk = TAILQ_FIRST(&asoc->sent_queue); 2978 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2979 } 2980} 2981 2982static void 2983sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2984 struct sctp_tcb *stcb) 2985{ 2986 struct sctp_nets *net; 2987 struct sctp_tmit_chunk *lchk; 2988 struct sctp_ecne_chunk bkup; 2989 uint8_t override_bit; 2990 uint32_t tsn, window_data_tsn; 2991 int len; 2992 unsigned int pkt_cnt; 2993 2994 len = ntohs(cp->ch.chunk_length); 2995 if ((len != sizeof(struct sctp_ecne_chunk)) && 2996 (len != sizeof(struct old_sctp_ecne_chunk))) { 2997 return; 2998 } 2999 if (len == sizeof(struct old_sctp_ecne_chunk)) { 3000 /* Its the old format */ 3001 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 3002 bkup.num_pkts_since_cwr = htonl(1); 3003 cp = &bkup; 3004 } 3005 SCTP_STAT_INCR(sctps_recvecne); 3006 tsn = ntohl(cp->tsn); 3007 pkt_cnt = ntohl(cp->num_pkts_since_cwr); 3008 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 3009 if (lchk == NULL) { 3010 window_data_tsn = stcb->asoc.sending_seq - 1; 3011 } else { 3012 window_data_tsn = lchk->rec.data.TSN_seq; 3013 } 3014 3015 /* Find where it was sent to if possible. */ 3016 net = NULL; 3017 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 3018 if (lchk->rec.data.TSN_seq == tsn) { 3019 net = lchk->whoTo; 3020 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 3021 break; 3022 } 3023 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { 3024 break; 3025 } 3026 } 3027 if (net == NULL) { 3028 /* 3029 * What to do. A previous send of a CWR was possibly lost. 3030 * See how old it is, we may have it marked on the actual 3031 * net. 3032 */ 3033 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3034 if (tsn == net->last_cwr_tsn) { 3035 /* Found him, send it off */ 3036 break; 3037 } 3038 } 3039 if (net == NULL) { 3040 /* 3041 * If we reach here, we need to send a special CWR 3042 * that says hey, we did this a long time ago and 3043 * you lost the response. 3044 */ 3045 net = TAILQ_FIRST(&stcb->asoc.nets); 3046 if (net == NULL) { 3047 /* TSNH */ 3048 return; 3049 } 3050 override_bit = SCTP_CWR_REDUCE_OVERRIDE; 3051 } else { 3052 override_bit = 0; 3053 } 3054 } else { 3055 override_bit = 0; 3056 } 3057 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 3058 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3059 /* 3060 * JRS - Use the congestion control given in the pluggable 3061 * CC module 3062 */ 3063 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 3064 /* 3065 * We reduce once every RTT. So we will only lower cwnd at 3066 * the next sending seq i.e. the window_data_tsn 3067 */ 3068 net->cwr_window_tsn = window_data_tsn; 3069 net->ecn_ce_pkt_cnt += pkt_cnt; 3070 net->lost_cnt = pkt_cnt; 3071 net->last_cwr_tsn = tsn; 3072 } else { 3073 override_bit |= SCTP_CWR_IN_SAME_WINDOW; 3074 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 3075 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 3076 /* 3077 * Another loss in the same window update how many 3078 * marks/packets lost we have had. 3079 */ 3080 int cnt = 1; 3081 3082 if (pkt_cnt > net->lost_cnt) { 3083 /* Should be the case */ 3084 cnt = (pkt_cnt - net->lost_cnt); 3085 net->ecn_ce_pkt_cnt += cnt; 3086 } 3087 net->lost_cnt = pkt_cnt; 3088 net->last_cwr_tsn = tsn; 3089 /* 3090 * Most CC functions will ignore this call, since we 3091 * are in-window yet of the initial CE the peer saw. 3092 */ 3093 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 3094 } 3095 } 3096 /* 3097 * We always send a CWR this way if our previous one was lost our 3098 * peer will get an update, or if it is not time again to reduce we 3099 * still get the cwr to the peer. Note we set the override when we 3100 * could not find the TSN on the chunk or the destination network. 3101 */ 3102 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 3103} 3104 3105static void 3106sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 3107{ 3108 /* 3109 * Here we get a CWR from the peer. We must look in the outqueue and 3110 * make sure that we have a covered ECNE in teh control chunk part. 3111 * If so remove it. 3112 */ 3113 struct sctp_tmit_chunk *chk; 3114 struct sctp_ecne_chunk *ecne; 3115 int override; 3116 uint32_t cwr_tsn; 3117 3118 cwr_tsn = ntohl(cp->tsn); 3119 3120 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 3121 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 3122 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3123 continue; 3124 } 3125 if ((override == 0) && (chk->whoTo != net)) { 3126 /* Must be from the right src unless override is set */ 3127 continue; 3128 } 3129 ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3130 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3131 /* this covers this ECNE, we can remove it */ 3132 stcb->asoc.ecn_echo_cnt_onq--; 3133 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3134 sctp_next); 3135 if (chk->data) { 3136 sctp_m_freem(chk->data); 3137 chk->data = NULL; 3138 } 3139 stcb->asoc.ctrl_queue_cnt--; 3140 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3141 if (override == 0) { 3142 break; 3143 } 3144 } 3145 } 3146} 3147 3148static void 3149sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3150 struct sctp_tcb *stcb, struct sctp_nets *net) 3151{ 3152 struct sctp_association *asoc; 3153 3154#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3155 struct socket *so; 3156 3157#endif 3158 3159 SCTPDBG(SCTP_DEBUG_INPUT2, 3160 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3161 if (stcb == NULL) 3162 return; 3163 3164 asoc = &stcb->asoc; 3165 /* process according to association state */ 3166 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3167 /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 3168 SCTPDBG(SCTP_DEBUG_INPUT2, 3169 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3170 SCTP_TCB_UNLOCK(stcb); 3171 return; 3172 } 3173 /* notify upper layer protocol */ 3174 if (stcb->sctp_socket) { 3175 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 3176 /* are the queues empty? they should be */ 3177 if (!TAILQ_EMPTY(&asoc->send_queue) || 3178 !TAILQ_EMPTY(&asoc->sent_queue) || 3179 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 3180 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED); 3181 } 3182 } 3183 /* stop the timer */ 3184 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 3185 SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3186 /* free the TCB */ 3187 SCTPDBG(SCTP_DEBUG_INPUT2, 3188 "sctp_handle_shutdown_complete: calls free-asoc\n"); 3189#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3190 so = SCTP_INP_SO(stcb->sctp_ep); 3191 atomic_add_int(&stcb->asoc.refcnt, 1); 3192 SCTP_TCB_UNLOCK(stcb); 3193 SCTP_SOCKET_LOCK(so, 1); 3194 SCTP_TCB_LOCK(stcb); 3195 atomic_subtract_int(&stcb->asoc.refcnt, 1); 3196#endif 3197 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 3198#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3199 SCTP_SOCKET_UNLOCK(so, 1); 3200#endif 3201 return; 3202} 3203 3204static int 3205process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3206 struct sctp_nets *net, uint8_t flg) 3207{ 3208 switch (desc->chunk_type) { 3209 case SCTP_DATA: 3210 /* find the tsn to resend (possibly */ 3211 { 3212 uint32_t tsn; 3213 struct sctp_tmit_chunk *tp1; 3214 3215 tsn = ntohl(desc->tsn_ifany); 3216 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3217 if (tp1->rec.data.TSN_seq == tsn) { 3218 /* found it */ 3219 break; 3220 } 3221 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { 3222 /* not found */ 3223 tp1 = NULL; 3224 break; 3225 } 3226 } 3227 if (tp1 == NULL) { 3228 /* 3229 * Do it the other way , aka without paying 3230 * attention to queue seq order. 3231 */ 3232 SCTP_STAT_INCR(sctps_pdrpdnfnd); 3233 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3234 if (tp1->rec.data.TSN_seq == tsn) { 3235 /* found it */ 3236 break; 3237 } 3238 } 3239 } 3240 if (tp1 == NULL) { 3241 SCTP_STAT_INCR(sctps_pdrptsnnf); 3242 } 3243 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 3244 uint8_t *ddp; 3245 3246 if (((flg & SCTP_BADCRC) == 0) && 3247 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3248 return (0); 3249 } 3250 if ((stcb->asoc.peers_rwnd == 0) && 3251 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3252 SCTP_STAT_INCR(sctps_pdrpdiwnp); 3253 return (0); 3254 } 3255 if (stcb->asoc.peers_rwnd == 0 && 3256 (flg & SCTP_FROM_MIDDLE_BOX)) { 3257 SCTP_STAT_INCR(sctps_pdrpdizrw); 3258 return (0); 3259 } 3260 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+ 3261 sizeof(struct sctp_data_chunk)); 3262 { 3263 unsigned int iii; 3264 3265 for (iii = 0; iii < sizeof(desc->data_bytes); 3266 iii++) { 3267 if (ddp[iii] != desc->data_bytes[iii]) { 3268 SCTP_STAT_INCR(sctps_pdrpbadd); 3269 return (-1); 3270 } 3271 } 3272 } 3273 3274 if (tp1->do_rtt) { 3275 /* 3276 * this guy had a RTO calculation 3277 * pending on it, cancel it 3278 */ 3279 if (tp1->whoTo->rto_needed == 0) { 3280 tp1->whoTo->rto_needed = 1; 3281 } 3282 tp1->do_rtt = 0; 3283 } 3284 SCTP_STAT_INCR(sctps_pdrpmark); 3285 if (tp1->sent != SCTP_DATAGRAM_RESEND) 3286 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3287 /* 3288 * mark it as if we were doing a FR, since 3289 * we will be getting gap ack reports behind 3290 * the info from the router. 3291 */ 3292 tp1->rec.data.doing_fast_retransmit = 1; 3293 /* 3294 * mark the tsn with what sequences can 3295 * cause a new FR. 3296 */ 3297 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3298 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3299 } else { 3300 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; 3301 } 3302 3303 /* restart the timer */ 3304 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3305 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3306 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3307 stcb, tp1->whoTo); 3308 3309 /* fix counts and things */ 3310 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3311 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3312 tp1->whoTo->flight_size, 3313 tp1->book_size, 3314 (uintptr_t) stcb, 3315 tp1->rec.data.TSN_seq); 3316 } 3317 if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3318 sctp_flight_size_decrease(tp1); 3319 sctp_total_flight_decrease(stcb, tp1); 3320 } 3321 tp1->sent = SCTP_DATAGRAM_RESEND; 3322 } { 3323 /* audit code */ 3324 unsigned int audit; 3325 3326 audit = 0; 3327 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3328 if (tp1->sent == SCTP_DATAGRAM_RESEND) 3329 audit++; 3330 } 3331 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3332 sctp_next) { 3333 if (tp1->sent == SCTP_DATAGRAM_RESEND) 3334 audit++; 3335 } 3336 if (audit != stcb->asoc.sent_queue_retran_cnt) { 3337 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3338 audit, stcb->asoc.sent_queue_retran_cnt); 3339#ifndef SCTP_AUDITING_ENABLED 3340 stcb->asoc.sent_queue_retran_cnt = audit; 3341#endif 3342 } 3343 } 3344 } 3345 break; 3346 case SCTP_ASCONF: 3347 { 3348 struct sctp_tmit_chunk *asconf; 3349 3350 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3351 sctp_next) { 3352 if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3353 break; 3354 } 3355 } 3356 if (asconf) { 3357 if (asconf->sent != SCTP_DATAGRAM_RESEND) 3358 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3359 asconf->sent = SCTP_DATAGRAM_RESEND; 3360 asconf->snd_count--; 3361 } 3362 } 3363 break; 3364 case SCTP_INITIATION: 3365 /* resend the INIT */ 3366 stcb->asoc.dropped_special_cnt++; 3367 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3368 /* 3369 * If we can get it in, in a few attempts we do 3370 * this, otherwise we let the timer fire. 3371 */ 3372 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3373 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3374 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3375 } 3376 break; 3377 case SCTP_SELECTIVE_ACK: 3378 case SCTP_NR_SELECTIVE_ACK: 3379 /* resend the sack */ 3380 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3381 break; 3382 case SCTP_HEARTBEAT_REQUEST: 3383 /* resend a demand HB */ 3384 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3385 /* 3386 * Only retransmit if we KNOW we wont destroy the 3387 * tcb 3388 */ 3389 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3390 } 3391 break; 3392 case SCTP_SHUTDOWN: 3393 sctp_send_shutdown(stcb, net); 3394 break; 3395 case SCTP_SHUTDOWN_ACK: 3396 sctp_send_shutdown_ack(stcb, net); 3397 break; 3398 case SCTP_COOKIE_ECHO: 3399 { 3400 struct sctp_tmit_chunk *cookie; 3401 3402 cookie = NULL; 3403 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3404 sctp_next) { 3405 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3406 break; 3407 } 3408 } 3409 if (cookie) { 3410 if (cookie->sent != SCTP_DATAGRAM_RESEND) 3411 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3412 cookie->sent = SCTP_DATAGRAM_RESEND; 3413 sctp_stop_all_cookie_timers(stcb); 3414 } 3415 } 3416 break; 3417 case SCTP_COOKIE_ACK: 3418 sctp_send_cookie_ack(stcb); 3419 break; 3420 case SCTP_ASCONF_ACK: 3421 /* resend last asconf ack */ 3422 sctp_send_asconf_ack(stcb); 3423 break; 3424 case SCTP_FORWARD_CUM_TSN: 3425 send_forward_tsn(stcb, &stcb->asoc); 3426 break; 3427 /* can't do anything with these */ 3428 case SCTP_PACKET_DROPPED: 3429 case SCTP_INITIATION_ACK: /* this should not happen */ 3430 case SCTP_HEARTBEAT_ACK: 3431 case SCTP_ABORT_ASSOCIATION: 3432 case SCTP_OPERATION_ERROR: 3433 case SCTP_SHUTDOWN_COMPLETE: 3434 case SCTP_ECN_ECHO: 3435 case SCTP_ECN_CWR: 3436 default: 3437 break; 3438 } 3439 return (0); 3440} 3441 3442void 3443sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3444{ 3445 int i; 3446 uint16_t temp; 3447 3448 /* 3449 * We set things to 0xffff since this is the last delivered sequence 3450 * and we will be sending in 0 after the reset. 3451 */ 3452 3453 if (number_entries) { 3454 for (i = 0; i < number_entries; i++) { 3455 temp = ntohs(list[i]); 3456 if (temp >= stcb->asoc.streamincnt) { 3457 continue; 3458 } 3459 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; 3460 } 3461 } else { 3462 list = NULL; 3463 for (i = 0; i < stcb->asoc.streamincnt; i++) { 3464 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3465 } 3466 } 3467 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3468} 3469 3470static void 3471sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list) 3472{ 3473 int i; 3474 3475 if (number_entries == 0) { 3476 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3477 stcb->asoc.strmout[i].next_sequence_sent = 0; 3478 } 3479 } else if (number_entries) { 3480 for (i = 0; i < number_entries; i++) { 3481 uint16_t temp; 3482 3483 temp = ntohs(list[i]); 3484 if (temp >= stcb->asoc.streamoutcnt) { 3485 /* no such stream */ 3486 continue; 3487 } 3488 stcb->asoc.strmout[temp].next_sequence_sent = 0; 3489 } 3490 } 3491 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3492} 3493 3494 3495struct sctp_stream_reset_out_request * 3496sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3497{ 3498 struct sctp_association *asoc; 3499 struct sctp_stream_reset_out_req *req; 3500 struct sctp_stream_reset_out_request *r; 3501 struct sctp_tmit_chunk *chk; 3502 int len, clen; 3503 3504 asoc = &stcb->asoc; 3505 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 3506 asoc->stream_reset_outstanding = 0; 3507 return (NULL); 3508 } 3509 if (stcb->asoc.str_reset == NULL) { 3510 asoc->stream_reset_outstanding = 0; 3511 return (NULL); 3512 } 3513 chk = stcb->asoc.str_reset; 3514 if (chk->data == NULL) { 3515 return (NULL); 3516 } 3517 if (bchk) { 3518 /* he wants a copy of the chk pointer */ 3519 *bchk = chk; 3520 } 3521 clen = chk->send_size; 3522 req = mtod(chk->data, struct sctp_stream_reset_out_req *); 3523 r = &req->sr_req; 3524 if (ntohl(r->request_seq) == seq) { 3525 /* found it */ 3526 return (r); 3527 } 3528 len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3529 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3530 /* move to the next one, there can only be a max of two */ 3531 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); 3532 if (ntohl(r->request_seq) == seq) { 3533 return (r); 3534 } 3535 } 3536 /* that seq is not here */ 3537 return (NULL); 3538} 3539 3540static void 3541sctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3542{ 3543 struct sctp_association *asoc; 3544 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; 3545 3546 if (stcb->asoc.str_reset == NULL) { 3547 return; 3548 } 3549 asoc = &stcb->asoc; 3550 3551 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3552 TAILQ_REMOVE(&asoc->control_send_queue, 3553 chk, 3554 sctp_next); 3555 if (chk->data) { 3556 sctp_m_freem(chk->data); 3557 chk->data = NULL; 3558 } 3559 asoc->ctrl_queue_cnt--; 3560 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3561 /* sa_ignore NO_NULL_CHK */ 3562 stcb->asoc.str_reset = NULL; 3563} 3564 3565 3566static int 3567sctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3568 uint32_t seq, uint32_t action, 3569 struct sctp_stream_reset_response *respin) 3570{ 3571 uint16_t type; 3572 int lparm_len; 3573 struct sctp_association *asoc = &stcb->asoc; 3574 struct sctp_tmit_chunk *chk; 3575 struct sctp_stream_reset_out_request *srparam; 3576 int number_entries; 3577 3578 if (asoc->stream_reset_outstanding == 0) { 3579 /* duplicate */ 3580 return (0); 3581 } 3582 if (seq == stcb->asoc.str_reset_seq_out) { 3583 srparam = sctp_find_stream_reset(stcb, seq, &chk); 3584 if (srparam) { 3585 stcb->asoc.str_reset_seq_out++; 3586 type = ntohs(srparam->ph.param_type); 3587 lparm_len = ntohs(srparam->ph.param_length); 3588 if (type == SCTP_STR_RESET_OUT_REQUEST) { 3589 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3590 asoc->stream_reset_out_is_outstanding = 0; 3591 if (asoc->stream_reset_outstanding) 3592 asoc->stream_reset_outstanding--; 3593 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3594 /* do it */ 3595 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); 3596 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3597 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3598 } else { 3599 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3600 } 3601 } else if (type == SCTP_STR_RESET_IN_REQUEST) { 3602 /* Answered my request */ 3603 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3604 if (asoc->stream_reset_outstanding) 3605 asoc->stream_reset_outstanding--; 3606 if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3607 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, 3608 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3609 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3610 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, 3611 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); 3612 } 3613 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { 3614 /* Ok we now may have more streams */ 3615 int num_stream; 3616 3617 num_stream = stcb->asoc.strm_pending_add_size; 3618 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { 3619 /* TSNH */ 3620 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; 3621 } 3622 stcb->asoc.strm_pending_add_size = 0; 3623 if (asoc->stream_reset_outstanding) 3624 asoc->stream_reset_outstanding--; 3625 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3626 /* Put the new streams into effect */ 3627 stcb->asoc.streamoutcnt += num_stream; 3628 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3629 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3630 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3631 SCTP_STREAM_CHANGE_DENIED); 3632 } else { 3633 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3634 SCTP_STREAM_CHANGE_FAILED); 3635 } 3636 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { 3637 if (asoc->stream_reset_outstanding) 3638 asoc->stream_reset_outstanding--; 3639 if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3640 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3641 SCTP_STREAM_CHANGE_DENIED); 3642 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3643 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3644 SCTP_STREAM_CHANGE_FAILED); 3645 } 3646 } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3647 /** 3648 * a) Adopt the new in tsn. 3649 * b) reset the map 3650 * c) Adopt the new out-tsn 3651 */ 3652 struct sctp_stream_reset_response_tsn *resp; 3653 struct sctp_forward_tsn_chunk fwdtsn; 3654 int abort_flag = 0; 3655 3656 if (respin == NULL) { 3657 /* huh ? */ 3658 return (0); 3659 } 3660 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3661 resp = (struct sctp_stream_reset_response_tsn *)respin; 3662 asoc->stream_reset_outstanding--; 3663 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3664 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3665 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3666 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3667 if (abort_flag) { 3668 return (1); 3669 } 3670 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3671 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3672 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3673 } 3674 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3675 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3676 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3677 3678 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3679 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 3680 3681 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3682 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3683 3684 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3685 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3686 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); 3687 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3688 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3689 SCTP_ASSOC_RESET_DENIED); 3690 } else { 3691 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3692 SCTP_ASSOC_RESET_FAILED); 3693 } 3694 } 3695 /* get rid of the request and get the request flags */ 3696 if (asoc->stream_reset_outstanding == 0) { 3697 sctp_clean_up_stream_reset(stcb); 3698 } 3699 } 3700 } 3701 return (0); 3702} 3703 3704static void 3705sctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3706 struct sctp_tmit_chunk *chk, 3707 struct sctp_stream_reset_in_request *req, int trunc) 3708{ 3709 uint32_t seq; 3710 int len, i; 3711 int number_entries; 3712 uint16_t temp; 3713 3714 /* 3715 * peer wants me to send a str-reset to him for my outgoing seq's if 3716 * seq_in is right. 3717 */ 3718 struct sctp_association *asoc = &stcb->asoc; 3719 3720 seq = ntohl(req->request_seq); 3721 if (asoc->str_reset_seq_in == seq) { 3722 asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3723 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3724 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3725 } else if (trunc) { 3726 /* Can't do it, since they exceeded our buffer size */ 3727 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3728 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3729 len = ntohs(req->ph.param_length); 3730 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 3731 for (i = 0; i < number_entries; i++) { 3732 temp = ntohs(req->list_of_streams[i]); 3733 req->list_of_streams[i] = temp; 3734 } 3735 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3736 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, 3737 asoc->str_reset_seq_out, 3738 seq, (asoc->sending_seq - 1)); 3739 asoc->stream_reset_out_is_outstanding = 1; 3740 asoc->str_reset = chk; 3741 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 3742 stcb->asoc.stream_reset_outstanding++; 3743 } else { 3744 /* Can't do it, since we have sent one out */ 3745 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3746 } 3747 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3748 asoc->str_reset_seq_in++; 3749 } else if (asoc->str_reset_seq_in - 1 == seq) { 3750 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3751 } else if (asoc->str_reset_seq_in - 2 == seq) { 3752 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3753 } else { 3754 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3755 } 3756} 3757 3758static int 3759sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3760 struct sctp_tmit_chunk *chk, 3761 struct sctp_stream_reset_tsn_request *req) 3762{ 3763 /* reset all in and out and update the tsn */ 3764 /* 3765 * A) reset my str-seq's on in and out. B) Select a receive next, 3766 * and set cum-ack to it. Also process this selected number as a 3767 * fwd-tsn as well. C) set in the response my next sending seq. 3768 */ 3769 struct sctp_forward_tsn_chunk fwdtsn; 3770 struct sctp_association *asoc = &stcb->asoc; 3771 int abort_flag = 0; 3772 uint32_t seq; 3773 3774 seq = ntohl(req->request_seq); 3775 if (asoc->str_reset_seq_in == seq) { 3776 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3777 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3778 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3779 } else { 3780 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3781 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3782 fwdtsn.ch.chunk_flags = 0; 3783 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3784 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3785 if (abort_flag) { 3786 return (1); 3787 } 3788 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3789 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3790 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3791 } 3792 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; 3793 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; 3794 memset(asoc->mapping_array, 0, asoc->mapping_array_size); 3795 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 3796 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 3797 atomic_add_int(&asoc->sending_seq, 1); 3798 /* save off historical data for retrans */ 3799 asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; 3800 asoc->last_sending_seq[0] = asoc->sending_seq; 3801 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; 3802 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; 3803 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); 3804 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); 3805 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3806 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); 3807 } 3808 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3809 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3810 asoc->str_reset_seq_in++; 3811 } else if (asoc->str_reset_seq_in - 1 == seq) { 3812 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3813 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3814 } else if (asoc->str_reset_seq_in - 2 == seq) { 3815 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3816 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); 3817 } else { 3818 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3819 } 3820 return (0); 3821} 3822 3823static void 3824sctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3825 struct sctp_tmit_chunk *chk, 3826 struct sctp_stream_reset_out_request *req, int trunc) 3827{ 3828 uint32_t seq, tsn; 3829 int number_entries, len; 3830 struct sctp_association *asoc = &stcb->asoc; 3831 3832 seq = ntohl(req->request_seq); 3833 3834 /* now if its not a duplicate we process it */ 3835 if (asoc->str_reset_seq_in == seq) { 3836 len = ntohs(req->ph.param_length); 3837 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3838 /* 3839 * the sender is resetting, handle the list issue.. we must 3840 * a) verify if we can do the reset, if so no problem b) If 3841 * we can't do the reset we must copy the request. c) queue 3842 * it, and setup the data in processor to trigger it off 3843 * when needed and dequeue all the queued data. 3844 */ 3845 tsn = ntohl(req->send_reset_at_tsn); 3846 3847 /* move the reset action back one */ 3848 asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3849 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3850 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3851 } else if (trunc) { 3852 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3853 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3854 /* we can do it now */ 3855 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3856 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3857 } else { 3858 /* 3859 * we must queue it up and thus wait for the TSN's 3860 * to arrive that are at or before tsn 3861 */ 3862 struct sctp_stream_reset_list *liste; 3863 int siz; 3864 3865 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3866 SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3867 siz, SCTP_M_STRESET); 3868 if (liste == NULL) { 3869 /* gak out of memory */ 3870 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3871 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3872 return; 3873 } 3874 liste->tsn = tsn; 3875 liste->number_entries = number_entries; 3876 memcpy(&liste->req, req, 3877 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t)))); 3878 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 3879 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3880 } 3881 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3882 asoc->str_reset_seq_in++; 3883 } else if ((asoc->str_reset_seq_in - 1) == seq) { 3884 /* 3885 * one seq back, just echo back last action since my 3886 * response was lost. 3887 */ 3888 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3889 } else if ((asoc->str_reset_seq_in - 2) == seq) { 3890 /* 3891 * two seq back, just echo back last action since my 3892 * response was lost. 3893 */ 3894 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3895 } else { 3896 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3897 } 3898} 3899 3900static void 3901sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3902 struct sctp_stream_reset_add_strm *str_add) 3903{ 3904 /* 3905 * Peer is requesting to add more streams. If its within our 3906 * max-streams we will allow it. 3907 */ 3908 uint32_t num_stream, i; 3909 uint32_t seq; 3910 struct sctp_association *asoc = &stcb->asoc; 3911 struct sctp_queued_to_read *ctl, *nctl; 3912 3913 /* Get the number. */ 3914 seq = ntohl(str_add->request_seq); 3915 num_stream = ntohs(str_add->number_of_streams); 3916 /* Now what would be the new total? */ 3917 if (asoc->str_reset_seq_in == seq) { 3918 num_stream += stcb->asoc.streamincnt; 3919 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3920 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3921 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3922 } else if ((num_stream > stcb->asoc.max_inbound_streams) || 3923 (num_stream > 0xffff)) { 3924 /* We must reject it they ask for to many */ 3925 denied: 3926 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3927 } else { 3928 /* Ok, we can do that :-) */ 3929 struct sctp_stream_in *oldstrm; 3930 3931 /* save off the old */ 3932 oldstrm = stcb->asoc.strmin; 3933 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3934 (num_stream * sizeof(struct sctp_stream_in)), 3935 SCTP_M_STRMI); 3936 if (stcb->asoc.strmin == NULL) { 3937 stcb->asoc.strmin = oldstrm; 3938 goto denied; 3939 } 3940 /* copy off the old data */ 3941 for (i = 0; i < stcb->asoc.streamincnt; i++) { 3942 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3943 stcb->asoc.strmin[i].stream_no = i; 3944 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; 3945 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 3946 /* now anything on those queues? */ 3947 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { 3948 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); 3949 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); 3950 } 3951 } 3952 /* Init the new streams */ 3953 for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3954 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 3955 stcb->asoc.strmin[i].stream_no = i; 3956 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; 3957 stcb->asoc.strmin[i].delivery_started = 0; 3958 } 3959 SCTP_FREE(oldstrm, SCTP_M_STRMI); 3960 /* update the size */ 3961 stcb->asoc.streamincnt = num_stream; 3962 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3963 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3964 } 3965 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3966 asoc->str_reset_seq_in++; 3967 } else if ((asoc->str_reset_seq_in - 1) == seq) { 3968 /* 3969 * one seq back, just echo back last action since my 3970 * response was lost. 3971 */ 3972 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3973 } else if ((asoc->str_reset_seq_in - 2) == seq) { 3974 /* 3975 * two seq back, just echo back last action since my 3976 * response was lost. 3977 */ 3978 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3979 } else { 3980 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3981 3982 } 3983} 3984 3985static void 3986sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3987 struct sctp_stream_reset_add_strm *str_add) 3988{ 3989 /* 3990 * Peer is requesting to add more streams. If its within our 3991 * max-streams we will allow it. 3992 */ 3993 uint16_t num_stream; 3994 uint32_t seq; 3995 struct sctp_association *asoc = &stcb->asoc; 3996 3997 /* Get the number. */ 3998 seq = ntohl(str_add->request_seq); 3999 num_stream = ntohs(str_add->number_of_streams); 4000 /* Now what would be the new total? */ 4001 if (asoc->str_reset_seq_in == seq) { 4002 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 4003 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 4004 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4005 } else if (stcb->asoc.stream_reset_outstanding) { 4006 /* We must reject it we have something pending */ 4007 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 4008 } else { 4009 /* Ok, we can do that :-) */ 4010 int mychk; 4011 4012 mychk = stcb->asoc.streamoutcnt; 4013 mychk += num_stream; 4014 if (mychk < 0x10000) { 4015 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 4016 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) { 4017 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4018 } 4019 } else { 4020 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 4021 } 4022 } 4023 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); 4024 asoc->str_reset_seq_in++; 4025 } else if ((asoc->str_reset_seq_in - 1) == seq) { 4026 /* 4027 * one seq back, just echo back last action since my 4028 * response was lost. 4029 */ 4030 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 4031 } else if ((asoc->str_reset_seq_in - 2) == seq) { 4032 /* 4033 * two seq back, just echo back last action since my 4034 * response was lost. 4035 */ 4036 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 4037 } else { 4038 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 4039 } 4040} 4041 4042#ifdef __GNUC__ 4043__attribute__((noinline)) 4044#endif 4045 static int 4046 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 4047 struct sctp_stream_reset_out_req *sr_req) 4048{ 4049 int chk_length, param_len, ptype; 4050 struct sctp_paramhdr pstore; 4051 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 4052 uint32_t seq = 0; 4053 int num_req = 0; 4054 int trunc = 0; 4055 struct sctp_tmit_chunk *chk; 4056 struct sctp_chunkhdr *ch; 4057 struct sctp_paramhdr *ph; 4058 int ret_code = 0; 4059 int num_param = 0; 4060 4061 /* now it may be a reset or a reset-response */ 4062 chk_length = ntohs(sr_req->ch.chunk_length); 4063 4064 /* setup for adding the response */ 4065 sctp_alloc_a_chunk(stcb, chk); 4066 if (chk == NULL) { 4067 return (ret_code); 4068 } 4069 chk->rec.chunk_id.id = SCTP_STREAM_RESET; 4070 chk->rec.chunk_id.can_take_data = 0; 4071 chk->asoc = &stcb->asoc; 4072 chk->no_fr_allowed = 0; 4073 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 4074 chk->book_size_scale = 0; 4075 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 4076 if (chk->data == NULL) { 4077strres_nochunk: 4078 if (chk->data) { 4079 sctp_m_freem(chk->data); 4080 chk->data = NULL; 4081 } 4082 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4083 return (ret_code); 4084 } 4085 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4086 4087 /* setup chunk parameters */ 4088 chk->sent = SCTP_DATAGRAM_UNSENT; 4089 chk->snd_count = 0; 4090 chk->whoTo = NULL; 4091 4092 ch = mtod(chk->data, struct sctp_chunkhdr *); 4093 ch->chunk_type = SCTP_STREAM_RESET; 4094 ch->chunk_flags = 0; 4095 ch->chunk_length = htons(chk->send_size); 4096 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4097 offset += sizeof(struct sctp_chunkhdr); 4098 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { 4099 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore); 4100 if (ph == NULL) 4101 break; 4102 param_len = ntohs(ph->param_length); 4103 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { 4104 /* bad param */ 4105 break; 4106 } 4107 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), 4108 (uint8_t *) & cstore); 4109 ptype = ntohs(ph->param_type); 4110 num_param++; 4111 if (param_len > (int)sizeof(cstore)) { 4112 trunc = 1; 4113 } else { 4114 trunc = 0; 4115 } 4116 if (num_param > SCTP_MAX_RESET_PARAMS) { 4117 /* hit the max of parameters already sorry.. */ 4118 break; 4119 } 4120 if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4121 struct sctp_stream_reset_out_request *req_out; 4122 4123 req_out = (struct sctp_stream_reset_out_request *)ph; 4124 num_req++; 4125 if (stcb->asoc.stream_reset_outstanding) { 4126 seq = ntohl(req_out->response_seq); 4127 if (seq == stcb->asoc.str_reset_seq_out) { 4128 /* implicit ack */ 4129 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); 4130 } 4131 } 4132 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4133 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { 4134 struct sctp_stream_reset_add_strm *str_add; 4135 4136 str_add = (struct sctp_stream_reset_add_strm *)ph; 4137 num_req++; 4138 sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4139 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { 4140 struct sctp_stream_reset_add_strm *str_add; 4141 4142 str_add = (struct sctp_stream_reset_add_strm *)ph; 4143 num_req++; 4144 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); 4145 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4146 struct sctp_stream_reset_in_request *req_in; 4147 4148 num_req++; 4149 req_in = (struct sctp_stream_reset_in_request *)ph; 4150 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4151 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4152 struct sctp_stream_reset_tsn_request *req_tsn; 4153 4154 num_req++; 4155 req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4156 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4157 ret_code = 1; 4158 goto strres_nochunk; 4159 } 4160 /* no more */ 4161 break; 4162 } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4163 struct sctp_stream_reset_response *resp; 4164 uint32_t result; 4165 4166 resp = (struct sctp_stream_reset_response *)ph; 4167 seq = ntohl(resp->response_seq); 4168 result = ntohl(resp->result); 4169 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4170 ret_code = 1; 4171 goto strres_nochunk; 4172 } 4173 } else { 4174 break; 4175 } 4176 offset += SCTP_SIZE32(param_len); 4177 chk_length -= SCTP_SIZE32(param_len); 4178 } 4179 if (num_req == 0) { 4180 /* we have no response free the stuff */ 4181 goto strres_nochunk; 4182 } 4183 /* ok we have a chunk to link in */ 4184 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4185 chk, 4186 sctp_next); 4187 stcb->asoc.ctrl_queue_cnt++; 4188 return (ret_code); 4189} 4190 4191/* 4192 * Handle a router or endpoints report of a packet loss, there are two ways 4193 * to handle this, either we get the whole packet and must disect it 4194 * ourselves (possibly with truncation and or corruption) or it is a summary 4195 * from a middle box that did the disectting for us. 4196 */ 4197static void 4198sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4199 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4200{ 4201 uint32_t bottle_bw, on_queue; 4202 uint16_t trunc_len; 4203 unsigned int chlen; 4204 unsigned int at; 4205 struct sctp_chunk_desc desc; 4206 struct sctp_chunkhdr *ch; 4207 4208 chlen = ntohs(cp->ch.chunk_length); 4209 chlen -= sizeof(struct sctp_pktdrop_chunk); 4210 /* XXX possible chlen underflow */ 4211 if (chlen == 0) { 4212 ch = NULL; 4213 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) 4214 SCTP_STAT_INCR(sctps_pdrpbwrpt); 4215 } else { 4216 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); 4217 chlen -= sizeof(struct sctphdr); 4218 /* XXX possible chlen underflow */ 4219 memset(&desc, 0, sizeof(desc)); 4220 } 4221 trunc_len = (uint16_t) ntohs(cp->trunc_len); 4222 if (trunc_len > limit) { 4223 trunc_len = limit; 4224 } 4225 /* now the chunks themselves */ 4226 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { 4227 desc.chunk_type = ch->chunk_type; 4228 /* get amount we need to move */ 4229 at = ntohs(ch->chunk_length); 4230 if (at < sizeof(struct sctp_chunkhdr)) { 4231 /* corrupt chunk, maybe at the end? */ 4232 SCTP_STAT_INCR(sctps_pdrpcrupt); 4233 break; 4234 } 4235 if (trunc_len == 0) { 4236 /* we are supposed to have all of it */ 4237 if (at > chlen) { 4238 /* corrupt skip it */ 4239 SCTP_STAT_INCR(sctps_pdrpcrupt); 4240 break; 4241 } 4242 } else { 4243 /* is there enough of it left ? */ 4244 if (desc.chunk_type == SCTP_DATA) { 4245 if (chlen < (sizeof(struct sctp_data_chunk) + 4246 sizeof(desc.data_bytes))) { 4247 break; 4248 } 4249 } else { 4250 if (chlen < sizeof(struct sctp_chunkhdr)) { 4251 break; 4252 } 4253 } 4254 } 4255 if (desc.chunk_type == SCTP_DATA) { 4256 /* can we get out the tsn? */ 4257 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4258 SCTP_STAT_INCR(sctps_pdrpmbda); 4259 4260 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { 4261 /* yep */ 4262 struct sctp_data_chunk *dcp; 4263 uint8_t *ddp; 4264 unsigned int iii; 4265 4266 dcp = (struct sctp_data_chunk *)ch; 4267 ddp = (uint8_t *) (dcp + 1); 4268 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { 4269 desc.data_bytes[iii] = ddp[iii]; 4270 } 4271 desc.tsn_ifany = dcp->dp.tsn; 4272 } else { 4273 /* nope we are done. */ 4274 SCTP_STAT_INCR(sctps_pdrpnedat); 4275 break; 4276 } 4277 } else { 4278 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) 4279 SCTP_STAT_INCR(sctps_pdrpmbct); 4280 } 4281 4282 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { 4283 SCTP_STAT_INCR(sctps_pdrppdbrk); 4284 break; 4285 } 4286 if (SCTP_SIZE32(at) > chlen) { 4287 break; 4288 } 4289 chlen -= SCTP_SIZE32(at); 4290 if (chlen < sizeof(struct sctp_chunkhdr)) { 4291 /* done, none left */ 4292 break; 4293 } 4294 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); 4295 } 4296 /* Now update any rwnd --- possibly */ 4297 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4298 /* From a peer, we get a rwnd report */ 4299 uint32_t a_rwnd; 4300 4301 SCTP_STAT_INCR(sctps_pdrpfehos); 4302 4303 bottle_bw = ntohl(cp->bottle_bw); 4304 on_queue = ntohl(cp->current_onq); 4305 if (bottle_bw && on_queue) { 4306 /* a rwnd report is in here */ 4307 if (bottle_bw > on_queue) 4308 a_rwnd = bottle_bw - on_queue; 4309 else 4310 a_rwnd = 0; 4311 4312 if (a_rwnd == 0) 4313 stcb->asoc.peers_rwnd = 0; 4314 else { 4315 if (a_rwnd > stcb->asoc.total_flight) { 4316 stcb->asoc.peers_rwnd = 4317 a_rwnd - stcb->asoc.total_flight; 4318 } else { 4319 stcb->asoc.peers_rwnd = 0; 4320 } 4321 if (stcb->asoc.peers_rwnd < 4322 stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4323 /* SWS sender side engages */ 4324 stcb->asoc.peers_rwnd = 0; 4325 } 4326 } 4327 } 4328 } else { 4329 SCTP_STAT_INCR(sctps_pdrpfmbox); 4330 } 4331 4332 /* now middle boxes in sat networks get a cwnd bump */ 4333 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && 4334 (stcb->asoc.sat_t3_loss_recovery == 0) && 4335 (stcb->asoc.sat_network)) { 4336 /* 4337 * This is debateable but for sat networks it makes sense 4338 * Note if a T3 timer has went off, we will prohibit any 4339 * changes to cwnd until we exit the t3 loss recovery. 4340 */ 4341 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4342 net, cp, &bottle_bw, &on_queue); 4343 } 4344} 4345 4346/* 4347 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4348 * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4349 * offset: offset into the mbuf chain to first chunkhdr - length: is the 4350 * length of the complete packet outputs: - length: modified to remaining 4351 * length after control processing - netp: modified to new sctp_nets after 4352 * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4353 * bad packet,...) otherwise return the tcb for this packet 4354 */ 4355#ifdef __GNUC__ 4356__attribute__((noinline)) 4357#endif 4358 static struct sctp_tcb * 4359 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4360 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 4361 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4362 uint32_t vrf_id, uint16_t port) 4363{ 4364 struct sctp_association *asoc; 4365 uint32_t vtag_in; 4366 int num_chunks = 0; /* number of control chunks processed */ 4367 uint32_t chk_length; 4368 int ret; 4369 int abort_no_unlock = 0; 4370 int ecne_seen = 0; 4371 4372 /* 4373 * How big should this be, and should it be alloc'd? Lets try the 4374 * d-mtu-ceiling for now (2k) and that should hopefully work ... 4375 * until we get into jumbo grams and such.. 4376 */ 4377 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4378 struct sctp_tcb *locked_tcb = stcb; 4379 int got_auth = 0; 4380 uint32_t auth_offset = 0, auth_len = 0; 4381 int auth_skipped = 0; 4382 int asconf_cnt = 0; 4383 4384#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4385 struct socket *so; 4386 4387#endif 4388 4389 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4390 iphlen, *offset, length, stcb); 4391 4392 /* validate chunk header length... */ 4393 if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4394 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4395 ntohs(ch->chunk_length)); 4396 if (locked_tcb) { 4397 SCTP_TCB_UNLOCK(locked_tcb); 4398 } 4399 return (NULL); 4400 } 4401 /* 4402 * validate the verification tag 4403 */ 4404 vtag_in = ntohl(sh->v_tag); 4405 4406 if (locked_tcb) { 4407 SCTP_TCB_LOCK_ASSERT(locked_tcb); 4408 } 4409 if (ch->chunk_type == SCTP_INITIATION) { 4410 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4411 ntohs(ch->chunk_length), vtag_in); 4412 if (vtag_in != 0) { 4413 /* protocol error- silently discard... */ 4414 SCTP_STAT_INCR(sctps_badvtag); 4415 if (locked_tcb) { 4416 SCTP_TCB_UNLOCK(locked_tcb); 4417 } 4418 return (NULL); 4419 } 4420 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4421 /* 4422 * If there is no stcb, skip the AUTH chunk and process 4423 * later after a stcb is found (to validate the lookup was 4424 * valid. 4425 */ 4426 if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4427 (stcb == NULL) && 4428 !SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4429 /* save this chunk for later processing */ 4430 auth_skipped = 1; 4431 auth_offset = *offset; 4432 auth_len = ntohs(ch->chunk_length); 4433 4434 /* (temporarily) move past this chunk */ 4435 *offset += SCTP_SIZE32(auth_len); 4436 if (*offset >= length) { 4437 /* no more data left in the mbuf chain */ 4438 *offset = length; 4439 if (locked_tcb) { 4440 SCTP_TCB_UNLOCK(locked_tcb); 4441 } 4442 return (NULL); 4443 } 4444 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4445 sizeof(struct sctp_chunkhdr), chunk_buf); 4446 } 4447 if (ch == NULL) { 4448 /* Help */ 4449 *offset = length; 4450 if (locked_tcb) { 4451 SCTP_TCB_UNLOCK(locked_tcb); 4452 } 4453 return (NULL); 4454 } 4455 if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4456 goto process_control_chunks; 4457 } 4458 /* 4459 * first check if it's an ASCONF with an unknown src addr we 4460 * need to look inside to find the association 4461 */ 4462 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 4463 struct sctp_chunkhdr *asconf_ch = ch; 4464 uint32_t asconf_offset = 0, asconf_len = 0; 4465 4466 /* inp's refcount may be reduced */ 4467 SCTP_INP_INCR_REF(inp); 4468 4469 asconf_offset = *offset; 4470 do { 4471 asconf_len = ntohs(asconf_ch->chunk_length); 4472 if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 4473 break; 4474 stcb = sctp_findassociation_ep_asconf(m, 4475 *offset, sh, &inp, netp, vrf_id); 4476 if (stcb != NULL) 4477 break; 4478 asconf_offset += SCTP_SIZE32(asconf_len); 4479 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 4480 sizeof(struct sctp_chunkhdr), chunk_buf); 4481 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4482 if (stcb == NULL) { 4483 /* 4484 * reduce inp's refcount if not reduced in 4485 * sctp_findassociation_ep_asconf(). 4486 */ 4487 SCTP_INP_DECR_REF(inp); 4488 } else { 4489 locked_tcb = stcb; 4490 } 4491 4492 /* now go back and verify any auth chunk to be sure */ 4493 if (auth_skipped && (stcb != NULL)) { 4494 struct sctp_auth_chunk *auth; 4495 4496 auth = (struct sctp_auth_chunk *) 4497 sctp_m_getptr(m, auth_offset, 4498 auth_len, chunk_buf); 4499 got_auth = 1; 4500 auth_skipped = 0; 4501 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4502 auth_offset)) { 4503 /* auth HMAC failed so dump it */ 4504 *offset = length; 4505 if (locked_tcb) { 4506 SCTP_TCB_UNLOCK(locked_tcb); 4507 } 4508 return (NULL); 4509 } else { 4510 /* remaining chunks are HMAC checked */ 4511 stcb->asoc.authenticated = 1; 4512 } 4513 } 4514 } 4515 if (stcb == NULL) { 4516 /* no association, so it's out of the blue... */ 4517 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL, 4518 vrf_id, port); 4519 *offset = length; 4520 if (locked_tcb) { 4521 SCTP_TCB_UNLOCK(locked_tcb); 4522 } 4523 return (NULL); 4524 } 4525 asoc = &stcb->asoc; 4526 /* ABORT and SHUTDOWN can use either v_tag... */ 4527 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4528 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4529 (ch->chunk_type == SCTP_PACKET_DROPPED)) { 4530 if ((vtag_in == asoc->my_vtag) || 4531 ((ch->chunk_flags & SCTP_HAD_NO_TCB) && 4532 (vtag_in == asoc->peer_vtag))) { 4533 /* this is valid */ 4534 } else { 4535 /* drop this packet... */ 4536 SCTP_STAT_INCR(sctps_badvtag); 4537 if (locked_tcb) { 4538 SCTP_TCB_UNLOCK(locked_tcb); 4539 } 4540 return (NULL); 4541 } 4542 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 4543 if (vtag_in != asoc->my_vtag) { 4544 /* 4545 * this could be a stale SHUTDOWN-ACK or the 4546 * peer never got the SHUTDOWN-COMPLETE and 4547 * is still hung; we have started a new asoc 4548 * but it won't complete until the shutdown 4549 * is completed 4550 */ 4551 if (locked_tcb) { 4552 SCTP_TCB_UNLOCK(locked_tcb); 4553 } 4554 sctp_handle_ootb(m, iphlen, *offset, sh, inp, 4555 NULL, vrf_id, port); 4556 return (NULL); 4557 } 4558 } else { 4559 /* for all other chunks, vtag must match */ 4560 if (vtag_in != asoc->my_vtag) { 4561 /* invalid vtag... */ 4562 SCTPDBG(SCTP_DEBUG_INPUT3, 4563 "invalid vtag: %xh, expect %xh\n", 4564 vtag_in, asoc->my_vtag); 4565 SCTP_STAT_INCR(sctps_badvtag); 4566 if (locked_tcb) { 4567 SCTP_TCB_UNLOCK(locked_tcb); 4568 } 4569 *offset = length; 4570 return (NULL); 4571 } 4572 } 4573 } /* end if !SCTP_COOKIE_ECHO */ 4574 /* 4575 * process all control chunks... 4576 */ 4577 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4578 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4579 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4580 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 4581 /* implied cookie-ack.. we must have lost the ack */ 4582 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4583 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4584 stcb->asoc.overall_error_count, 4585 0, 4586 SCTP_FROM_SCTP_INPUT, 4587 __LINE__); 4588 } 4589 stcb->asoc.overall_error_count = 0; 4590 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4591 *netp); 4592 } 4593process_control_chunks: 4594 while (IS_SCTP_CONTROL(ch)) { 4595 /* validate chunk length */ 4596 chk_length = ntohs(ch->chunk_length); 4597 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4598 ch->chunk_type, chk_length); 4599 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 4600 if (chk_length < sizeof(*ch) || 4601 (*offset + (int)chk_length) > length) { 4602 *offset = length; 4603 if (locked_tcb) { 4604 SCTP_TCB_UNLOCK(locked_tcb); 4605 } 4606 return (NULL); 4607 } 4608 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4609 /* 4610 * INIT-ACK only gets the init ack "header" portion only 4611 * because we don't have to process the peer's COOKIE. All 4612 * others get a complete chunk. 4613 */ 4614 if ((ch->chunk_type == SCTP_INITIATION_ACK) || 4615 (ch->chunk_type == SCTP_INITIATION)) { 4616 /* get an init-ack chunk */ 4617 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4618 sizeof(struct sctp_init_ack_chunk), chunk_buf); 4619 if (ch == NULL) { 4620 *offset = length; 4621 if (locked_tcb) { 4622 SCTP_TCB_UNLOCK(locked_tcb); 4623 } 4624 return (NULL); 4625 } 4626 } else { 4627 /* For cookies and all other chunks. */ 4628 if (chk_length > sizeof(chunk_buf)) { 4629 /* 4630 * use just the size of the chunk buffer so 4631 * the front part of our chunks fit in 4632 * contiguous space up to the chunk buffer 4633 * size (508 bytes). For chunks that need to 4634 * get more than that they must use the 4635 * sctp_m_getptr() function or other means 4636 * (e.g. know how to parse mbuf chains). 4637 * Cookies do this already. 4638 */ 4639 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4640 (sizeof(chunk_buf) - 4), 4641 chunk_buf); 4642 if (ch == NULL) { 4643 *offset = length; 4644 if (locked_tcb) { 4645 SCTP_TCB_UNLOCK(locked_tcb); 4646 } 4647 return (NULL); 4648 } 4649 } else { 4650 /* We can fit it all */ 4651 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4652 chk_length, chunk_buf); 4653 if (ch == NULL) { 4654 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); 4655 *offset = length; 4656 if (locked_tcb) { 4657 SCTP_TCB_UNLOCK(locked_tcb); 4658 } 4659 return (NULL); 4660 } 4661 } 4662 } 4663 num_chunks++; 4664 /* Save off the last place we got a control from */ 4665 if (stcb != NULL) { 4666 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4667 /* 4668 * allow last_control to be NULL if 4669 * ASCONF... ASCONF processing will find the 4670 * right net later 4671 */ 4672 if ((netp != NULL) && (*netp != NULL)) 4673 stcb->asoc.last_control_chunk_from = *netp; 4674 } 4675 } 4676#ifdef SCTP_AUDITING_ENABLED 4677 sctp_audit_log(0xB0, ch->chunk_type); 4678#endif 4679 4680 /* check to see if this chunk required auth, but isn't */ 4681 if ((stcb != NULL) && 4682 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 4683 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4684 !stcb->asoc.authenticated) { 4685 /* "silently" ignore */ 4686 SCTP_STAT_INCR(sctps_recvauthmissing); 4687 goto next_chunk; 4688 } 4689 switch (ch->chunk_type) { 4690 case SCTP_INITIATION: 4691 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4692 /* The INIT chunk must be the only chunk. */ 4693 if ((num_chunks > 1) || 4694 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4695 sctp_abort_association(inp, stcb, m, 4696 iphlen, sh, NULL, vrf_id, port); 4697 *offset = length; 4698 return (NULL); 4699 } 4700 /* Honor our resource limit. */ 4701 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4702 struct mbuf *op_err; 4703 4704 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 4705 sctp_abort_association(inp, stcb, m, 4706 iphlen, sh, op_err, vrf_id, port); 4707 *offset = length; 4708 return (NULL); 4709 } 4710 sctp_handle_init(m, iphlen, *offset, sh, 4711 (struct sctp_init_chunk *)ch, inp, 4712 stcb, &abort_no_unlock, vrf_id, port); 4713 *offset = length; 4714 if ((!abort_no_unlock) && (locked_tcb)) { 4715 SCTP_TCB_UNLOCK(locked_tcb); 4716 } 4717 return (NULL); 4718 break; 4719 case SCTP_PAD_CHUNK: 4720 break; 4721 case SCTP_INITIATION_ACK: 4722 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); 4723 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4724 /* We are not interested anymore */ 4725 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4726 ; 4727 } else { 4728 if (locked_tcb != stcb) { 4729 /* Very unlikely */ 4730 SCTP_TCB_UNLOCK(locked_tcb); 4731 } 4732 *offset = length; 4733 if (stcb) { 4734#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4735 so = SCTP_INP_SO(inp); 4736 atomic_add_int(&stcb->asoc.refcnt, 1); 4737 SCTP_TCB_UNLOCK(stcb); 4738 SCTP_SOCKET_LOCK(so, 1); 4739 SCTP_TCB_LOCK(stcb); 4740 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4741#endif 4742 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 4743#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4744 SCTP_SOCKET_UNLOCK(so, 1); 4745#endif 4746 } 4747 return (NULL); 4748 } 4749 } 4750 /* The INIT-ACK chunk must be the only chunk. */ 4751 if ((num_chunks > 1) || 4752 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4753 *offset = length; 4754 if (locked_tcb) { 4755 SCTP_TCB_UNLOCK(locked_tcb); 4756 } 4757 return (NULL); 4758 } 4759 if ((netp) && (*netp)) { 4760 ret = sctp_handle_init_ack(m, iphlen, *offset, sh, 4761 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id); 4762 } else { 4763 ret = -1; 4764 } 4765 *offset = length; 4766 if (abort_no_unlock) { 4767 return (NULL); 4768 } 4769 /* 4770 * Special case, I must call the output routine to 4771 * get the cookie echoed 4772 */ 4773 if ((stcb != NULL) && (ret == 0)) { 4774 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4775 } 4776 if (locked_tcb) { 4777 SCTP_TCB_UNLOCK(locked_tcb); 4778 } 4779 return (NULL); 4780 break; 4781 case SCTP_SELECTIVE_ACK: 4782 { 4783 struct sctp_sack_chunk *sack; 4784 int abort_now = 0; 4785 uint32_t a_rwnd, cum_ack; 4786 uint16_t num_seg, num_dup; 4787 uint8_t flags; 4788 int offset_seg, offset_dup; 4789 4790 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); 4791 SCTP_STAT_INCR(sctps_recvsacks); 4792 if (stcb == NULL) { 4793 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); 4794 break; 4795 } 4796 if (chk_length < sizeof(struct sctp_sack_chunk)) { 4797 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4798 break; 4799 } 4800 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4801 /*- 4802 * If we have sent a shutdown-ack, we will pay no 4803 * attention to a sack sent in to us since 4804 * we don't care anymore. 4805 */ 4806 break; 4807 } 4808 sack = (struct sctp_sack_chunk *)ch; 4809 flags = ch->chunk_flags; 4810 cum_ack = ntohl(sack->sack.cum_tsn_ack); 4811 num_seg = ntohs(sack->sack.num_gap_ack_blks); 4812 num_dup = ntohs(sack->sack.num_dup_tsns); 4813 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); 4814 if (sizeof(struct sctp_sack_chunk) + 4815 num_seg * sizeof(struct sctp_gap_ack_block) + 4816 num_dup * sizeof(uint32_t) != chk_length) { 4817 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4818 break; 4819 } 4820 offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4821 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4822 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4823 cum_ack, num_seg, a_rwnd); 4824 stcb->asoc.seen_a_sack_this_pkt = 1; 4825 if ((stcb->asoc.pr_sctp_cnt == 0) && 4826 (num_seg == 0) && 4827 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4828 (stcb->asoc.saw_sack_with_frags == 0) && 4829 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4830 (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) 4831 ) { 4832 /* 4833 * We have a SIMPLE sack having no 4834 * prior segments and data on sent 4835 * queue to be acked.. Use the 4836 * faster path sack processing. We 4837 * also allow window update sacks 4838 * with no missing segments to go 4839 * this way too. 4840 */ 4841 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); 4842 } else { 4843 if (netp && *netp) 4844 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4845 num_seg, 0, num_dup, &abort_now, flags, 4846 cum_ack, a_rwnd, ecne_seen); 4847 } 4848 if (abort_now) { 4849 /* ABORT signal from sack processing */ 4850 *offset = length; 4851 return (NULL); 4852 } 4853 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4854 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4855 (stcb->asoc.stream_queue_cnt == 0)) { 4856 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4857 } 4858 } 4859 break; 4860 /* 4861 * EY - nr_sack: If the received chunk is an 4862 * nr_sack chunk 4863 */ 4864 case SCTP_NR_SELECTIVE_ACK: 4865 { 4866 struct sctp_nr_sack_chunk *nr_sack; 4867 int abort_now = 0; 4868 uint32_t a_rwnd, cum_ack; 4869 uint16_t num_seg, num_nr_seg, num_dup; 4870 uint8_t flags; 4871 int offset_seg, offset_dup; 4872 4873 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); 4874 SCTP_STAT_INCR(sctps_recvsacks); 4875 if (stcb == NULL) { 4876 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); 4877 break; 4878 } 4879 if ((stcb->asoc.sctp_nr_sack_on_off == 0) || 4880 (stcb->asoc.peer_supports_nr_sack == 0)) { 4881 goto unknown_chunk; 4882 } 4883 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4884 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); 4885 break; 4886 } 4887 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 4888 /*- 4889 * If we have sent a shutdown-ack, we will pay no 4890 * attention to a sack sent in to us since 4891 * we don't care anymore. 4892 */ 4893 break; 4894 } 4895 nr_sack = (struct sctp_nr_sack_chunk *)ch; 4896 flags = ch->chunk_flags; 4897 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4898 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4899 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4900 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4901 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); 4902 if (sizeof(struct sctp_nr_sack_chunk) + 4903 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4904 num_dup * sizeof(uint32_t) != chk_length) { 4905 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4906 break; 4907 } 4908 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4909 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4910 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4911 cum_ack, num_seg, a_rwnd); 4912 stcb->asoc.seen_a_sack_this_pkt = 1; 4913 if ((stcb->asoc.pr_sctp_cnt == 0) && 4914 (num_seg == 0) && (num_nr_seg == 0) && 4915 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4916 (stcb->asoc.saw_sack_with_frags == 0) && 4917 (stcb->asoc.saw_sack_with_nr_frags == 0) && 4918 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4919 /* 4920 * We have a SIMPLE sack having no 4921 * prior segments and data on sent 4922 * queue to be acked. Use the faster 4923 * path sack processing. We also 4924 * allow window update sacks with no 4925 * missing segments to go this way 4926 * too. 4927 */ 4928 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4929 &abort_now, ecne_seen); 4930 } else { 4931 if (netp && *netp) 4932 sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4933 num_seg, num_nr_seg, num_dup, &abort_now, flags, 4934 cum_ack, a_rwnd, ecne_seen); 4935 } 4936 if (abort_now) { 4937 /* ABORT signal from sack processing */ 4938 *offset = length; 4939 return (NULL); 4940 } 4941 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4942 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4943 (stcb->asoc.stream_queue_cnt == 0)) { 4944 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4945 } 4946 } 4947 break; 4948 4949 case SCTP_HEARTBEAT_REQUEST: 4950 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4951 if ((stcb) && netp && *netp) { 4952 SCTP_STAT_INCR(sctps_recvheartbeat); 4953 sctp_send_heartbeat_ack(stcb, m, *offset, 4954 chk_length, *netp); 4955 4956 /* He's alive so give him credit */ 4957 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4958 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4959 stcb->asoc.overall_error_count, 4960 0, 4961 SCTP_FROM_SCTP_INPUT, 4962 __LINE__); 4963 } 4964 stcb->asoc.overall_error_count = 0; 4965 } 4966 break; 4967 case SCTP_HEARTBEAT_ACK: 4968 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); 4969 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4970 /* Its not ours */ 4971 *offset = length; 4972 if (locked_tcb) { 4973 SCTP_TCB_UNLOCK(locked_tcb); 4974 } 4975 return (NULL); 4976 } 4977 /* He's alive so give him credit */ 4978 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4979 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4980 stcb->asoc.overall_error_count, 4981 0, 4982 SCTP_FROM_SCTP_INPUT, 4983 __LINE__); 4984 } 4985 stcb->asoc.overall_error_count = 0; 4986 SCTP_STAT_INCR(sctps_recvheartbeatack); 4987 if (netp && *netp) 4988 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4989 stcb, *netp); 4990 break; 4991 case SCTP_ABORT_ASSOCIATION: 4992 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4993 stcb); 4994 if ((stcb) && netp && *netp) 4995 sctp_handle_abort((struct sctp_abort_chunk *)ch, 4996 stcb, *netp); 4997 *offset = length; 4998 return (NULL); 4999 break; 5000 case SCTP_SHUTDOWN: 5001 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 5002 stcb); 5003 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 5004 *offset = length; 5005 if (locked_tcb) { 5006 SCTP_TCB_UNLOCK(locked_tcb); 5007 } 5008 return (NULL); 5009 } 5010 if (netp && *netp) { 5011 int abort_flag = 0; 5012 5013 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 5014 stcb, *netp, &abort_flag); 5015 if (abort_flag) { 5016 *offset = length; 5017 return (NULL); 5018 } 5019 } 5020 break; 5021 case SCTP_SHUTDOWN_ACK: 5022 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb); 5023 if ((stcb) && (netp) && (*netp)) 5024 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 5025 *offset = length; 5026 return (NULL); 5027 break; 5028 5029 case SCTP_OPERATION_ERROR: 5030 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); 5031 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { 5032 *offset = length; 5033 return (NULL); 5034 } 5035 break; 5036 case SCTP_COOKIE_ECHO: 5037 SCTPDBG(SCTP_DEBUG_INPUT3, 5038 "SCTP_COOKIE-ECHO, stcb %p\n", stcb); 5039 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5040 ; 5041 } else { 5042 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5043 /* We are not interested anymore */ 5044 abend: 5045 if (stcb) { 5046 SCTP_TCB_UNLOCK(stcb); 5047 } 5048 *offset = length; 5049 return (NULL); 5050 } 5051 } 5052 /* 5053 * First are we accepting? We do this again here 5054 * since it is possible that a previous endpoint WAS 5055 * listening responded to a INIT-ACK and then 5056 * closed. We opened and bound.. and are now no 5057 * longer listening. 5058 */ 5059 5060 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { 5061 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5062 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 5063 struct mbuf *op_err; 5064 5065 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); 5066 sctp_abort_association(inp, stcb, m, 5067 iphlen, sh, op_err, vrf_id, port); 5068 } 5069 *offset = length; 5070 return (NULL); 5071 } else { 5072 struct mbuf *ret_buf; 5073 struct sctp_inpcb *linp; 5074 5075 if (stcb) { 5076 linp = NULL; 5077 } else { 5078 linp = inp; 5079 } 5080 5081 if (linp) { 5082 SCTP_ASOC_CREATE_LOCK(linp); 5083 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5084 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5085 SCTP_ASOC_CREATE_UNLOCK(linp); 5086 goto abend; 5087 } 5088 } 5089 if (netp) { 5090 ret_buf = 5091 sctp_handle_cookie_echo(m, iphlen, 5092 *offset, sh, 5093 (struct sctp_cookie_echo_chunk *)ch, 5094 &inp, &stcb, netp, 5095 auth_skipped, 5096 auth_offset, 5097 auth_len, 5098 &locked_tcb, 5099 vrf_id, 5100 port); 5101 } else { 5102 ret_buf = NULL; 5103 } 5104 if (linp) { 5105 SCTP_ASOC_CREATE_UNLOCK(linp); 5106 } 5107 if (ret_buf == NULL) { 5108 if (locked_tcb) { 5109 SCTP_TCB_UNLOCK(locked_tcb); 5110 } 5111 SCTPDBG(SCTP_DEBUG_INPUT3, 5112 "GAK, null buffer\n"); 5113 *offset = length; 5114 return (NULL); 5115 } 5116 /* if AUTH skipped, see if it verified... */ 5117 if (auth_skipped) { 5118 got_auth = 1; 5119 auth_skipped = 0; 5120 } 5121 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { 5122 /* 5123 * Restart the timer if we have 5124 * pending data 5125 */ 5126 struct sctp_tmit_chunk *chk; 5127 5128 chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 5129 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 5130 } 5131 } 5132 break; 5133 case SCTP_COOKIE_ACK: 5134 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb); 5135 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 5136 if (locked_tcb) { 5137 SCTP_TCB_UNLOCK(locked_tcb); 5138 } 5139 return (NULL); 5140 } 5141 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5142 /* We are not interested anymore */ 5143 if ((stcb) && (stcb->asoc.total_output_queue_size)) { 5144 ; 5145 } else if (stcb) { 5146#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5147 so = SCTP_INP_SO(inp); 5148 atomic_add_int(&stcb->asoc.refcnt, 1); 5149 SCTP_TCB_UNLOCK(stcb); 5150 SCTP_SOCKET_LOCK(so, 1); 5151 SCTP_TCB_LOCK(stcb); 5152 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5153#endif 5154 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 5155#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5156 SCTP_SOCKET_UNLOCK(so, 1); 5157#endif 5158 *offset = length; 5159 return (NULL); 5160 } 5161 } 5162 /* He's alive so give him credit */ 5163 if ((stcb) && netp && *netp) { 5164 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5165 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5166 stcb->asoc.overall_error_count, 5167 0, 5168 SCTP_FROM_SCTP_INPUT, 5169 __LINE__); 5170 } 5171 stcb->asoc.overall_error_count = 0; 5172 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 5173 } 5174 break; 5175 case SCTP_ECN_ECHO: 5176 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); 5177 /* He's alive so give him credit */ 5178 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { 5179 /* Its not ours */ 5180 if (locked_tcb) { 5181 SCTP_TCB_UNLOCK(locked_tcb); 5182 } 5183 *offset = length; 5184 return (NULL); 5185 } 5186 if (stcb) { 5187 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5188 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5189 stcb->asoc.overall_error_count, 5190 0, 5191 SCTP_FROM_SCTP_INPUT, 5192 __LINE__); 5193 } 5194 stcb->asoc.overall_error_count = 0; 5195 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, 5196 stcb); 5197 ecne_seen = 1; 5198 } 5199 break; 5200 case SCTP_ECN_CWR: 5201 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); 5202 /* He's alive so give him credit */ 5203 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { 5204 /* Its not ours */ 5205 if (locked_tcb) { 5206 SCTP_TCB_UNLOCK(locked_tcb); 5207 } 5208 *offset = length; 5209 return (NULL); 5210 } 5211 if (stcb) { 5212 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5213 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5214 stcb->asoc.overall_error_count, 5215 0, 5216 SCTP_FROM_SCTP_INPUT, 5217 __LINE__); 5218 } 5219 stcb->asoc.overall_error_count = 0; 5220 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5221 } 5222 break; 5223 case SCTP_SHUTDOWN_COMPLETE: 5224 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb); 5225 /* must be first and only chunk */ 5226 if ((num_chunks > 1) || 5227 (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5228 *offset = length; 5229 if (locked_tcb) { 5230 SCTP_TCB_UNLOCK(locked_tcb); 5231 } 5232 return (NULL); 5233 } 5234 if ((stcb) && netp && *netp) { 5235 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5236 stcb, *netp); 5237 } 5238 *offset = length; 5239 return (NULL); 5240 break; 5241 case SCTP_ASCONF: 5242 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5243 /* He's alive so give him credit */ 5244 if (stcb) { 5245 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5246 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5247 stcb->asoc.overall_error_count, 5248 0, 5249 SCTP_FROM_SCTP_INPUT, 5250 __LINE__); 5251 } 5252 stcb->asoc.overall_error_count = 0; 5253 sctp_handle_asconf(m, *offset, 5254 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 5255 asconf_cnt++; 5256 } 5257 break; 5258 case SCTP_ASCONF_ACK: 5259 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); 5260 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 5261 /* Its not ours */ 5262 if (locked_tcb) { 5263 SCTP_TCB_UNLOCK(locked_tcb); 5264 } 5265 *offset = length; 5266 return (NULL); 5267 } 5268 if ((stcb) && netp && *netp) { 5269 /* He's alive so give him credit */ 5270 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5271 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5272 stcb->asoc.overall_error_count, 5273 0, 5274 SCTP_FROM_SCTP_INPUT, 5275 __LINE__); 5276 } 5277 stcb->asoc.overall_error_count = 0; 5278 sctp_handle_asconf_ack(m, *offset, 5279 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 5280 if (abort_no_unlock) 5281 return (NULL); 5282 } 5283 break; 5284 case SCTP_FORWARD_CUM_TSN: 5285 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); 5286 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 5287 /* Its not ours */ 5288 if (locked_tcb) { 5289 SCTP_TCB_UNLOCK(locked_tcb); 5290 } 5291 *offset = length; 5292 return (NULL); 5293 } 5294 /* He's alive so give him credit */ 5295 if (stcb) { 5296 int abort_flag = 0; 5297 5298 stcb->asoc.overall_error_count = 0; 5299 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5300 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5301 stcb->asoc.overall_error_count, 5302 0, 5303 SCTP_FROM_SCTP_INPUT, 5304 __LINE__); 5305 } 5306 *fwd_tsn_seen = 1; 5307 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5308 /* We are not interested anymore */ 5309#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5310 so = SCTP_INP_SO(inp); 5311 atomic_add_int(&stcb->asoc.refcnt, 1); 5312 SCTP_TCB_UNLOCK(stcb); 5313 SCTP_SOCKET_LOCK(so, 1); 5314 SCTP_TCB_LOCK(stcb); 5315 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5316#endif 5317 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 5318#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5319 SCTP_SOCKET_UNLOCK(so, 1); 5320#endif 5321 *offset = length; 5322 return (NULL); 5323 } 5324 sctp_handle_forward_tsn(stcb, 5325 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5326 if (abort_flag) { 5327 *offset = length; 5328 return (NULL); 5329 } else { 5330 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5331 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5332 stcb->asoc.overall_error_count, 5333 0, 5334 SCTP_FROM_SCTP_INPUT, 5335 __LINE__); 5336 } 5337 stcb->asoc.overall_error_count = 0; 5338 } 5339 5340 } 5341 break; 5342 case SCTP_STREAM_RESET: 5343 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 5344 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { 5345 /* Its not ours */ 5346 if (locked_tcb) { 5347 SCTP_TCB_UNLOCK(locked_tcb); 5348 } 5349 *offset = length; 5350 return (NULL); 5351 } 5352 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5353 /* We are not interested anymore */ 5354#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5355 so = SCTP_INP_SO(inp); 5356 atomic_add_int(&stcb->asoc.refcnt, 1); 5357 SCTP_TCB_UNLOCK(stcb); 5358 SCTP_SOCKET_LOCK(so, 1); 5359 SCTP_TCB_LOCK(stcb); 5360 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5361#endif 5362 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 5363#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 5364 SCTP_SOCKET_UNLOCK(so, 1); 5365#endif 5366 *offset = length; 5367 return (NULL); 5368 } 5369 if (stcb->asoc.peer_supports_strreset == 0) { 5370 /* 5371 * hmm, peer should have announced this, but 5372 * we will turn it on since he is sending us 5373 * a stream reset. 5374 */ 5375 stcb->asoc.peer_supports_strreset = 1; 5376 } 5377 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) { 5378 /* stop processing */ 5379 *offset = length; 5380 return (NULL); 5381 } 5382 break; 5383 case SCTP_PACKET_DROPPED: 5384 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 5385 /* re-get it all please */ 5386 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 5387 /* Its not ours */ 5388 if (locked_tcb) { 5389 SCTP_TCB_UNLOCK(locked_tcb); 5390 } 5391 *offset = length; 5392 return (NULL); 5393 } 5394 if (ch && (stcb) && netp && (*netp)) { 5395 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5396 stcb, *netp, 5397 min(chk_length, (sizeof(chunk_buf) - 4))); 5398 5399 } 5400 break; 5401 5402 case SCTP_AUTHENTICATION: 5403 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5404 if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 5405 goto unknown_chunk; 5406 5407 if (stcb == NULL) { 5408 /* save the first AUTH for later processing */ 5409 if (auth_skipped == 0) { 5410 auth_offset = *offset; 5411 auth_len = chk_length; 5412 auth_skipped = 1; 5413 } 5414 /* skip this chunk (temporarily) */ 5415 goto next_chunk; 5416 } 5417 if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5418 (chk_length > (sizeof(struct sctp_auth_chunk) + 5419 SCTP_AUTH_DIGEST_LEN_MAX))) { 5420 /* Its not ours */ 5421 if (locked_tcb) { 5422 SCTP_TCB_UNLOCK(locked_tcb); 5423 } 5424 *offset = length; 5425 return (NULL); 5426 } 5427 if (got_auth == 1) { 5428 /* skip this chunk... it's already auth'd */ 5429 goto next_chunk; 5430 } 5431 got_auth = 1; 5432 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, 5433 m, *offset)) { 5434 /* auth HMAC failed so dump the packet */ 5435 *offset = length; 5436 return (stcb); 5437 } else { 5438 /* remaining chunks are HMAC checked */ 5439 stcb->asoc.authenticated = 1; 5440 } 5441 break; 5442 5443 default: 5444 unknown_chunk: 5445 /* it's an unknown chunk! */ 5446 if ((ch->chunk_type & 0x40) && (stcb != NULL)) { 5447 struct mbuf *mm; 5448 struct sctp_paramhdr *phd; 5449 5450 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 5451 0, M_DONTWAIT, 1, MT_DATA); 5452 if (mm) { 5453 phd = mtod(mm, struct sctp_paramhdr *); 5454 /* 5455 * We cheat and use param type since 5456 * we did not bother to define a 5457 * error cause struct. They are the 5458 * same basic format with different 5459 * names. 5460 */ 5461 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); 5462 phd->param_length = htons(chk_length + sizeof(*phd)); 5463 SCTP_BUF_LEN(mm) = sizeof(*phd); 5464 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT); 5465 if (SCTP_BUF_NEXT(mm)) { 5466 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) { 5467 sctp_m_freem(mm); 5468 } else { 5469#ifdef SCTP_MBUF_LOGGING 5470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5471 struct mbuf *mat; 5472 5473 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) { 5474 if (SCTP_BUF_IS_EXTENDED(mat)) { 5475 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 5476 } 5477 } 5478 } 5479#endif 5480 sctp_queue_op_err(stcb, mm); 5481 } 5482 } else { 5483 sctp_m_freem(mm); 5484 } 5485 } 5486 } 5487 if ((ch->chunk_type & 0x80) == 0) { 5488 /* discard this packet */ 5489 *offset = length; 5490 return (stcb); 5491 } /* else skip this bad chunk and continue... */ 5492 break; 5493 } /* switch (ch->chunk_type) */ 5494 5495 5496next_chunk: 5497 /* get the next chunk */ 5498 *offset += SCTP_SIZE32(chk_length); 5499 if (*offset >= length) { 5500 /* no more data left in the mbuf chain */ 5501 break; 5502 } 5503 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5504 sizeof(struct sctp_chunkhdr), chunk_buf); 5505 if (ch == NULL) { 5506 if (locked_tcb) { 5507 SCTP_TCB_UNLOCK(locked_tcb); 5508 } 5509 *offset = length; 5510 return (NULL); 5511 } 5512 } /* while */ 5513 5514 if (asconf_cnt > 0 && stcb != NULL) { 5515 sctp_send_asconf_ack(stcb); 5516 } 5517 return (stcb); 5518} 5519 5520 5521#ifdef INVARIANTS 5522#ifdef __GNUC__ 5523__attribute__((noinline)) 5524#endif 5525 void 5526 sctp_validate_no_locks(struct sctp_inpcb *inp) 5527{ 5528 struct sctp_tcb *lstcb; 5529 5530 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { 5531 if (mtx_owned(&lstcb->tcb_mtx)) { 5532 panic("Own lock on stcb at return from input"); 5533 } 5534 } 5535 if (mtx_owned(&inp->inp_create_mtx)) { 5536 panic("Own create lock on inp"); 5537 } 5538 if (mtx_owned(&inp->inp_mtx)) { 5539 panic("Own inp lock on inp"); 5540 } 5541} 5542 5543#endif 5544 5545/* 5546 * common input chunk processing (v4 and v6) 5547 */ 5548void 5549sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, 5550 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch, 5551 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net, 5552 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port) 5553{ 5554 /* 5555 * Control chunk processing 5556 */ 5557 uint32_t high_tsn; 5558 int fwd_tsn_seen = 0, data_processed = 0; 5559 struct mbuf *m = *mm; 5560 int un_sent; 5561 int cnt_ctrl_ready = 0; 5562 5563 SCTP_STAT_INCR(sctps_recvdatagrams); 5564#ifdef SCTP_AUDITING_ENABLED 5565 sctp_audit_log(0xE0, 1); 5566 sctp_auditing(0, inp, stcb, net); 5567#endif 5568 5569 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5570 m, iphlen, offset, length, stcb); 5571 if (stcb) { 5572 /* always clear this before beginning a packet */ 5573 stcb->asoc.authenticated = 0; 5574 stcb->asoc.seen_a_sack_this_pkt = 0; 5575 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5576 stcb, stcb->asoc.state); 5577 5578 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5579 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 5580 /*- 5581 * If we hit here, we had a ref count 5582 * up when the assoc was aborted and the 5583 * timer is clearing out the assoc, we should 5584 * NOT respond to any packet.. its OOTB. 5585 */ 5586 SCTP_TCB_UNLOCK(stcb); 5587 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5588 vrf_id, port); 5589 goto out_now; 5590 } 5591 } 5592 if (IS_SCTP_CONTROL(ch)) { 5593 /* process the control portion of the SCTP packet */ 5594 /* sa_ignore NO_NULL_CHK */ 5595 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch, 5596 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port); 5597 if (stcb) { 5598 /* 5599 * This covers us if the cookie-echo was there and 5600 * it changes our INP. 5601 */ 5602 inp = stcb->sctp_ep; 5603 if ((net) && (port)) { 5604 if (net->port == 0) { 5605 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5606 } 5607 net->port = port; 5608 } 5609 } 5610 } else { 5611 /* 5612 * no control chunks, so pre-process DATA chunks (these 5613 * checks are taken care of by control processing) 5614 */ 5615 5616 /* 5617 * if DATA only packet, and auth is required, then punt... 5618 * can't have authenticated without any AUTH (control) 5619 * chunks 5620 */ 5621 if ((stcb != NULL) && 5622 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5623 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5624 /* "silently" ignore */ 5625 SCTP_STAT_INCR(sctps_recvauthmissing); 5626 SCTP_TCB_UNLOCK(stcb); 5627 goto out_now; 5628 } 5629 if (stcb == NULL) { 5630 /* out of the blue DATA chunk */ 5631 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5632 vrf_id, port); 5633 goto out_now; 5634 } 5635 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5636 /* v_tag mismatch! */ 5637 SCTP_STAT_INCR(sctps_badvtag); 5638 SCTP_TCB_UNLOCK(stcb); 5639 goto out_now; 5640 } 5641 } 5642 5643 if (stcb == NULL) { 5644 /* 5645 * no valid TCB for this packet, or we found it's a bad 5646 * packet while processing control, or we're done with this 5647 * packet (done or skip rest of data), so we drop it... 5648 */ 5649 goto out_now; 5650 } 5651 /* 5652 * DATA chunk processing 5653 */ 5654 /* plow through the data chunks while length > offset */ 5655 5656 /* 5657 * Rest should be DATA only. Check authentication state if AUTH for 5658 * DATA is required. 5659 */ 5660 if ((length > offset) && 5661 (stcb != NULL) && 5662 !SCTP_BASE_SYSCTL(sctp_auth_disable) && 5663 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5664 !stcb->asoc.authenticated) { 5665 /* "silently" ignore */ 5666 SCTP_STAT_INCR(sctps_recvauthmissing); 5667 SCTPDBG(SCTP_DEBUG_AUTH1, 5668 "Data chunk requires AUTH, skipped\n"); 5669 goto trigger_send; 5670 } 5671 if (length > offset) { 5672 int retval; 5673 5674 /* 5675 * First check to make sure our state is correct. We would 5676 * not get here unless we really did have a tag, so we don't 5677 * abort if this happens, just dump the chunk silently. 5678 */ 5679 switch (SCTP_GET_STATE(&stcb->asoc)) { 5680 case SCTP_STATE_COOKIE_ECHOED: 5681 /* 5682 * we consider data with valid tags in this state 5683 * shows us the cookie-ack was lost. Imply it was 5684 * there. 5685 */ 5686 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5687 sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5688 stcb->asoc.overall_error_count, 5689 0, 5690 SCTP_FROM_SCTP_INPUT, 5691 __LINE__); 5692 } 5693 stcb->asoc.overall_error_count = 0; 5694 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5695 break; 5696 case SCTP_STATE_COOKIE_WAIT: 5697 /* 5698 * We consider OOTB any data sent during asoc setup. 5699 */ 5700 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL, 5701 vrf_id, port); 5702 SCTP_TCB_UNLOCK(stcb); 5703 goto out_now; 5704 /* sa_ignore NOTREACHED */ 5705 break; 5706 case SCTP_STATE_EMPTY: /* should not happen */ 5707 case SCTP_STATE_INUSE: /* should not happen */ 5708 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5709 case SCTP_STATE_SHUTDOWN_ACK_SENT: 5710 default: 5711 SCTP_TCB_UNLOCK(stcb); 5712 goto out_now; 5713 /* sa_ignore NOTREACHED */ 5714 break; 5715 case SCTP_STATE_OPEN: 5716 case SCTP_STATE_SHUTDOWN_SENT: 5717 break; 5718 } 5719 /* plow through the data chunks while length > offset */ 5720 retval = sctp_process_data(mm, iphlen, &offset, length, sh, 5721 inp, stcb, net, &high_tsn); 5722 if (retval == 2) { 5723 /* 5724 * The association aborted, NO UNLOCK needed since 5725 * the association is destroyed. 5726 */ 5727 goto out_now; 5728 } 5729 data_processed = 1; 5730 /* 5731 * Anything important needs to have been m_copy'ed in 5732 * process_data 5733 */ 5734 } 5735 /* take care of ecn */ 5736 if ((data_processed == 1) && 5737 (stcb->asoc.ecn_allowed == 1) && 5738 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5739 /* Yep, we need to add a ECNE */ 5740 sctp_send_ecn_echo(stcb, net, high_tsn); 5741 } 5742 if ((data_processed == 0) && (fwd_tsn_seen)) { 5743 int was_a_gap; 5744 uint32_t highest_tsn; 5745 5746 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 5747 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 5748 } else { 5749 highest_tsn = stcb->asoc.highest_tsn_inside_map; 5750 } 5751 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 5752 stcb->asoc.send_sack = 1; 5753 sctp_sack_check(stcb, was_a_gap); 5754 } else if (fwd_tsn_seen) { 5755 stcb->asoc.send_sack = 1; 5756 } 5757 /* trigger send of any chunks in queue... */ 5758trigger_send: 5759#ifdef SCTP_AUDITING_ENABLED 5760 sctp_audit_log(0xE0, 2); 5761 sctp_auditing(1, inp, stcb, net); 5762#endif 5763 SCTPDBG(SCTP_DEBUG_INPUT1, 5764 "Check for chunk output prw:%d tqe:%d tf=%d\n", 5765 stcb->asoc.peers_rwnd, 5766 TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5767 stcb->asoc.total_flight); 5768 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5769 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5770 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5771 } 5772 if (cnt_ctrl_ready || 5773 ((un_sent) && 5774 (stcb->asoc.peers_rwnd > 0 || 5775 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { 5776 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5777 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5778 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5779 } 5780#ifdef SCTP_AUDITING_ENABLED 5781 sctp_audit_log(0xE0, 3); 5782 sctp_auditing(2, inp, stcb, net); 5783#endif 5784 SCTP_TCB_UNLOCK(stcb); 5785out_now: 5786#ifdef INVARIANTS 5787 sctp_validate_no_locks(inp); 5788#endif 5789 return; 5790} 5791 5792#if 0 5793static void 5794sctp_print_mbuf_chain(struct mbuf *m) 5795{ 5796 for (; m; m = SCTP_BUF_NEXT(m)) { 5797 SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m)); 5798 if (SCTP_BUF_IS_EXTENDED(m)) 5799 SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m)); 5800 } 5801} 5802 5803#endif 5804 5805#ifdef INET 5806void 5807sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5808{ 5809 struct mbuf *m; 5810 int iphlen; 5811 uint32_t vrf_id = 0; 5812 uint8_t ecn_bits; 5813 struct ip *ip; 5814 struct sctphdr *sh; 5815 struct sctp_inpcb *inp = NULL; 5816 struct sctp_nets *net; 5817 struct sctp_tcb *stcb = NULL; 5818 struct sctp_chunkhdr *ch; 5819 int refcount_up = 0; 5820 int length, mlen, offset; 5821 5822#if !defined(SCTP_WITH_NO_CSUM) 5823 uint32_t check, calc_check; 5824 5825#endif 5826 5827 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 5828 SCTP_RELEASE_PKT(i_pak); 5829 return; 5830 } 5831 mlen = SCTP_HEADER_LEN(i_pak); 5832 iphlen = off; 5833 m = SCTP_HEADER_TO_CHAIN(i_pak); 5834 5835 net = NULL; 5836 SCTP_STAT_INCR(sctps_recvpackets); 5837 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 5838 5839 5840#ifdef SCTP_MBUF_LOGGING 5841 /* Log in any input mbufs */ 5842 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 5843 struct mbuf *mat; 5844 5845 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { 5846 if (SCTP_BUF_IS_EXTENDED(mat)) { 5847 sctp_log_mb(mat, SCTP_MBUF_INPUT); 5848 } 5849 } 5850 } 5851#endif 5852#ifdef SCTP_PACKET_LOGGING 5853 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 5854 sctp_packet_log(m, mlen); 5855#endif 5856 /* 5857 * Must take out the iphlen, since mlen expects this (only effect lb 5858 * case) 5859 */ 5860 mlen -= iphlen; 5861 5862 /* 5863 * Get IP, SCTP, and first chunk header together in first mbuf. 5864 */ 5865 ip = mtod(m, struct ip *); 5866 offset = iphlen + sizeof(*sh) + sizeof(*ch); 5867 if (SCTP_BUF_LEN(m) < offset) { 5868 if ((m = m_pullup(m, offset)) == 0) { 5869 SCTP_STAT_INCR(sctps_hdrops); 5870 return; 5871 } 5872 ip = mtod(m, struct ip *); 5873 } 5874 /* validate mbuf chain length with IP payload length */ 5875 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) { 5876 SCTP_STAT_INCR(sctps_hdrops); 5877 goto bad; 5878 } 5879 sh = (struct sctphdr *)((caddr_t)ip + iphlen); 5880 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 5881 SCTPDBG(SCTP_DEBUG_INPUT1, 5882 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen); 5883 5884 /* SCTP does not allow broadcasts or multicasts */ 5885 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5886 goto bad; 5887 } 5888 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) { 5889 /* 5890 * We only look at broadcast if its a front state, All 5891 * others we will not have a tcb for anyway. 5892 */ 5893 goto bad; 5894 } 5895 /* validate SCTP checksum */ 5896 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 5897 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 5898 m->m_pkthdr.len, 5899 if_name(m->m_pkthdr.rcvif), 5900 m->m_pkthdr.csum_flags); 5901#if defined(SCTP_WITH_NO_CSUM) 5902 SCTP_STAT_INCR(sctps_recvnocrc); 5903#else 5904 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5905 SCTP_STAT_INCR(sctps_recvhwcrc); 5906 goto sctp_skip_csum_4; 5907 } 5908 check = sh->checksum; /* save incoming checksum */ 5909 sh->checksum = 0; /* prepare for calc */ 5910 calc_check = sctp_calculate_cksum(m, iphlen); 5911 sh->checksum = check; 5912 SCTP_STAT_INCR(sctps_recvswcrc); 5913 if (calc_check != check) { 5914 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5915 calc_check, check, m, mlen, iphlen); 5916 5917 stcb = sctp_findassociation_addr(m, 5918 offset - sizeof(*ch), 5919 sh, ch, &inp, &net, 5920 vrf_id); 5921 if ((net) && (port)) { 5922 if (net->port == 0) { 5923 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5924 } 5925 net->port = port; 5926 } 5927 if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5928 net->flowid = m->m_pkthdr.flowid; 5929#ifdef INVARIANTS 5930 net->flowidset = 1; 5931#endif 5932 } 5933 if ((inp) && (stcb)) { 5934 sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 5935 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5936 } else if ((inp != NULL) && (stcb == NULL)) { 5937 refcount_up = 1; 5938 } 5939 SCTP_STAT_INCR(sctps_badsum); 5940 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5941 goto bad; 5942 } 5943sctp_skip_csum_4: 5944#endif 5945 /* destination port of 0 is illegal, based on RFC2960. */ 5946 if (sh->dest_port == 0) { 5947 SCTP_STAT_INCR(sctps_hdrops); 5948 goto bad; 5949 } 5950 /* 5951 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 5952 * IP/SCTP/first chunk header... 5953 */ 5954 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch), 5955 sh, ch, &inp, &net, vrf_id); 5956 if ((net) && (port)) { 5957 if (net->port == 0) { 5958 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); 5959 } 5960 net->port = port; 5961 } 5962 if ((net != NULL) && (m->m_flags & M_FLOWID)) { 5963 net->flowid = m->m_pkthdr.flowid; 5964#ifdef INVARIANTS 5965 net->flowidset = 1; 5966#endif 5967 } 5968 /* inp's ref-count increased && stcb locked */ 5969 if (inp == NULL) { 5970 struct sctp_init_chunk *init_chk, chunk_buf; 5971 5972 SCTP_STAT_INCR(sctps_noport); 5973#ifdef ICMP_BANDLIM 5974 /* 5975 * we use the bandwidth limiting to protect against sending 5976 * too many ABORTS all at once. In this case these count the 5977 * same as an ICMP message. 5978 */ 5979 if (badport_bandlim(0) < 0) 5980 goto bad; 5981#endif /* ICMP_BANDLIM */ 5982 SCTPDBG(SCTP_DEBUG_INPUT1, 5983 "Sending a ABORT from packet entry!\n"); 5984 if (ch->chunk_type == SCTP_INITIATION) { 5985 /* 5986 * we do a trick here to get the INIT tag, dig in 5987 * and get the tag from the INIT and put it in the 5988 * common header. 5989 */ 5990 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 5991 iphlen + sizeof(*sh), sizeof(*init_chk), 5992 (uint8_t *) & chunk_buf); 5993 if (init_chk != NULL) 5994 sh->v_tag = init_chk->init.initiate_tag; 5995 } 5996 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5997 sctp_send_shutdown_complete2(m, sh, vrf_id, port); 5998 goto bad; 5999 } 6000 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 6001 goto bad; 6002 } 6003 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 6004 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 6005 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 6006 (ch->chunk_type != SCTP_INIT))) { 6007 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 6008 } 6009 } 6010 goto bad; 6011 } else if (stcb == NULL) { 6012 refcount_up = 1; 6013 } 6014#ifdef IPSEC 6015 /* 6016 * I very much doubt any of the IPSEC stuff will work but I have no 6017 * idea, so I will leave it in place. 6018 */ 6019 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) { 6020 MODULE_GLOBAL(ipsec4stat).in_polvio++; 6021 SCTP_STAT_INCR(sctps_hdrops); 6022 goto bad; 6023 } 6024#endif /* IPSEC */ 6025 6026 /* 6027 * common chunk processing 6028 */ 6029 length = ip->ip_len + iphlen; 6030 offset -= sizeof(struct sctp_chunkhdr); 6031 6032 ecn_bits = ip->ip_tos; 6033 6034 /* sa_ignore NO_NULL_CHK */ 6035 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 6036 inp, stcb, net, ecn_bits, vrf_id, port); 6037 /* inp's ref-count reduced && stcb unlocked */ 6038 if (m) { 6039 sctp_m_freem(m); 6040 } 6041 if ((inp) && (refcount_up)) { 6042 /* reduce ref-count */ 6043 SCTP_INP_DECR_REF(inp); 6044 } 6045 return; 6046bad: 6047 if (stcb) { 6048 SCTP_TCB_UNLOCK(stcb); 6049 } 6050 if ((inp) && (refcount_up)) { 6051 /* reduce ref-count */ 6052 SCTP_INP_DECR_REF(inp); 6053 } 6054 if (m) { 6055 sctp_m_freem(m); 6056 } 6057 return; 6058} 6059 6060#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6061extern int *sctp_cpuarry; 6062 6063#endif 6064 6065void 6066sctp_input(struct mbuf *m, int off) 6067{ 6068#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) 6069 struct ip *ip; 6070 struct sctphdr *sh; 6071 int offset; 6072 int cpu_to_use; 6073 uint32_t flowid, tag; 6074 6075 if (mp_ncpus > 1) { 6076 if (m->m_flags & M_FLOWID) { 6077 flowid = m->m_pkthdr.flowid; 6078 } else { 6079 /* 6080 * No flow id built by lower layers fix it so we 6081 * create one. 6082 */ 6083 ip = mtod(m, struct ip *); 6084 offset = off + sizeof(*sh); 6085 if (SCTP_BUF_LEN(m) < offset) { 6086 if ((m = m_pullup(m, offset)) == 0) { 6087 SCTP_STAT_INCR(sctps_hdrops); 6088 return; 6089 } 6090 ip = mtod(m, struct ip *); 6091 } 6092 sh = (struct sctphdr *)((caddr_t)ip + off); 6093 tag = htonl(sh->v_tag); 6094 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 6095 m->m_pkthdr.flowid = flowid; 6096 m->m_flags |= M_FLOWID; 6097 } 6098 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 6099 sctp_queue_to_mcore(m, off, cpu_to_use); 6100 return; 6101 } 6102#endif 6103 sctp_input_with_port(m, off, 0); 6104} 6105 6106#endif 6107