1f8829a4aSRandall Stewart /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4830d754dSRandall Stewart * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 5807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7f8829a4aSRandall Stewart * 8f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 9f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 10f8829a4aSRandall Stewart * 11f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 12f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 13f8829a4aSRandall Stewart * 14f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 15f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 16f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 17f8829a4aSRandall Stewart * 18f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 19f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 20f8829a4aSRandall Stewart * from this software without specific prior written permission. 21f8829a4aSRandall Stewart * 22f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 33f8829a4aSRandall Stewart */ 34f8829a4aSRandall Stewart 35f8829a4aSRandall Stewart #include <sys/cdefs.h> 36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 37f8829a4aSRandall Stewart 38f8829a4aSRandall Stewart #include <netinet/sctp_os.h> 39f8829a4aSRandall Stewart #include <netinet/sctp_var.h> 4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h> 41f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h> 42f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 43f8829a4aSRandall Stewart #include <netinet/sctputil.h> 44f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_input.h> 46f8829a4aSRandall Stewart #include <netinet/sctp_auth.h> 47f8829a4aSRandall Stewart #include <netinet/sctp_indata.h> 48f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h> 49207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h> 50851b7298SRandall Stewart #include <netinet/sctp_timer.h> 51a99b6783SRandall Stewart #include <netinet/sctp_crc32.h> 52776cd558SMichael Tuexen #include <netinet/sctp_kdtrace.h> 534474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 54c54a18d2SRandall Stewart #include <netinet/udp.h> 554474d71aSMichael Tuexen #endif 56bfc46083SRandall Stewart #include <sys/smp.h> 57f8829a4aSRandall Stewart 58f8829a4aSRandall Stewart static void 59f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(struct sctp_tcb *stcb) 60f8829a4aSRandall Stewart { 61f8829a4aSRandall Stewart struct sctp_nets *net; 62f8829a4aSRandall Stewart 63a5d547adSRandall Stewart /* 64a5d547adSRandall Stewart * This now not only stops all cookie timers it also stops any INIT 65a5d547adSRandall Stewart * timers as well. This will make sure that the timers are stopped 66a5d547adSRandall Stewart * in all collision cases. 67a5d547adSRandall Stewart */ 68f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 69f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 70a5d547adSRandall Stewart if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { 71f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, 72f8829a4aSRandall Stewart stcb->sctp_ep, 73f8829a4aSRandall Stewart stcb, 74a5d547adSRandall Stewart net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1); 75a5d547adSRandall Stewart } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { 76a5d547adSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_INIT, 77a5d547adSRandall Stewart stcb->sctp_ep, 78a5d547adSRandall Stewart stcb, 79a5d547adSRandall Stewart net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2); 80f8829a4aSRandall Stewart } 81f8829a4aSRandall Stewart } 82f8829a4aSRandall Stewart } 83f8829a4aSRandall Stewart 84f8829a4aSRandall Stewart /* INIT handler */ 85f8829a4aSRandall Stewart static void 86b1754ad1SMichael Tuexen sctp_handle_init(struct mbuf *m, int iphlen, int offset, 87b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 88f30ac432SMichael Tuexen struct sctp_init_chunk *cp, struct sctp_inpcb *inp, 89*eba8e643SMichael Tuexen struct sctp_tcb *stcb, struct sctp_nets *net, 90457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 91f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 92f8829a4aSRandall Stewart { 93f8829a4aSRandall Stewart struct sctp_init *init; 94f8829a4aSRandall Stewart struct mbuf *op_err; 95f8829a4aSRandall Stewart 96ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", 97dd294dceSMichael Tuexen (void *)stcb); 98d55b0b1bSRandall Stewart if (stcb == NULL) { 99d55b0b1bSRandall Stewart SCTP_INP_RLOCK(inp); 100d55b0b1bSRandall Stewart } 101059ec222SMichael Tuexen /* Validate parameters */ 102d68fdc4dSMichael Tuexen init = &cp->init; 103*eba8e643SMichael Tuexen if (ntohl(init->initiate_tag) == 0) { 104*eba8e643SMichael Tuexen goto outnow; 105*eba8e643SMichael Tuexen } 106*eba8e643SMichael Tuexen if ((ntohl(init->a_rwnd) < SCTP_MIN_RWND) || 107059ec222SMichael Tuexen (ntohs(init->num_inbound_streams) == 0) || 108059ec222SMichael Tuexen (ntohs(init->num_outbound_streams) == 0)) { 109f8829a4aSRandall Stewart /* protocol error... send abort */ 110ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, ""); 111*eba8e643SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err, 112*eba8e643SMichael Tuexen mflowtype, mflowid, inp->fibnum, 113c54a18d2SRandall Stewart vrf_id, port); 114d55b0b1bSRandall Stewart goto outnow; 115f8829a4aSRandall Stewart } 116f8829a4aSRandall Stewart if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), 117d68fdc4dSMichael Tuexen offset + ntohs(cp->ch.chunk_length))) { 118f8829a4aSRandall Stewart /* auth parameter(s) error... send abort */ 119ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 120ff1ffd74SMichael Tuexen "Problem with AUTH parameters"); 121*eba8e643SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err, 122*eba8e643SMichael Tuexen mflowtype, mflowid, inp->fibnum, 123f30ac432SMichael Tuexen vrf_id, port); 124d55b0b1bSRandall Stewart goto outnow; 125f8829a4aSRandall Stewart } 1265d08768aSMichael Tuexen /* We are only accepting if we have a listening socket. */ 127d68fdc4dSMichael Tuexen if ((stcb == NULL) && 128d68fdc4dSMichael Tuexen ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 129d68fdc4dSMichael Tuexen (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1305d08768aSMichael Tuexen (!SCTP_IS_LISTENING(inp)))) { 131d68fdc4dSMichael Tuexen /* 132d68fdc4dSMichael Tuexen * FIX ME ?? What about TCP model and we have a 133d68fdc4dSMichael Tuexen * match/restart case? Actually no fix is needed. the lookup 134d68fdc4dSMichael Tuexen * will always find the existing assoc so stcb would not be 135d68fdc4dSMichael Tuexen * NULL. It may be questionable to do this since we COULD 136d68fdc4dSMichael Tuexen * just send back the INIT-ACK and hope that the app did 137d68fdc4dSMichael Tuexen * accept()'s by the time the COOKIE was sent. But there is 138d68fdc4dSMichael Tuexen * a price to pay for COOKIE generation and I don't want to 139d68fdc4dSMichael Tuexen * pay it on the chance that the app will actually do some 140d68fdc4dSMichael Tuexen * accepts(). The App just looses and should NOT be in this 141d68fdc4dSMichael Tuexen * state :-) 142d68fdc4dSMichael Tuexen */ 143c58e60beSMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { 144ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 145ff1ffd74SMichael Tuexen "No listener"); 146ff1ffd74SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err, 147d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 148f30ac432SMichael Tuexen vrf_id, port); 149c58e60beSMichael Tuexen } 150d68fdc4dSMichael Tuexen goto outnow; 151d68fdc4dSMichael Tuexen } 152d68fdc4dSMichael Tuexen if ((stcb != NULL) && 153839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { 154d68fdc4dSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); 155d68fdc4dSMichael Tuexen sctp_send_shutdown_ack(stcb, NULL); 156d68fdc4dSMichael Tuexen sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 157d68fdc4dSMichael Tuexen } else { 158ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); 159ca83f93cSMichael Tuexen sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset, 160ca83f93cSMichael Tuexen src, dst, sh, cp, 161457b4b88SMichael Tuexen mflowtype, mflowid, 162cdd2d7d4SMichael Tuexen vrf_id, port); 163d68fdc4dSMichael Tuexen } 164d55b0b1bSRandall Stewart outnow: 165d55b0b1bSRandall Stewart if (stcb == NULL) { 166d55b0b1bSRandall Stewart SCTP_INP_RUNLOCK(inp); 167d55b0b1bSRandall Stewart } 168f8829a4aSRandall Stewart } 169f8829a4aSRandall Stewart 170f8829a4aSRandall Stewart /* 171f8829a4aSRandall Stewart * process peer "INIT/INIT-ACK" chunk returns value < 0 on error 172f8829a4aSRandall Stewart */ 1735bead436SRandall Stewart 1745bead436SRandall Stewart int 17528397ac1SMichael Tuexen sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked) 1765bead436SRandall Stewart { 177124d851aSMichael Tuexen int unsent_data; 178f7a77f6fSMichael Tuexen unsigned int i; 179f7a77f6fSMichael Tuexen struct sctp_stream_queue_pending *sp; 1805bead436SRandall Stewart struct sctp_association *asoc; 1815bead436SRandall Stewart 1825bead436SRandall Stewart /* 183124d851aSMichael Tuexen * This function returns if any stream has true unsent data on it. 184124d851aSMichael Tuexen * Note that as it looks through it will clean up any places that 185124d851aSMichael Tuexen * have old data that has been sent but left at top of stream queue. 1865bead436SRandall Stewart */ 1875bead436SRandall Stewart asoc = &stcb->asoc; 188124d851aSMichael Tuexen unsent_data = 0; 1895bead436SRandall Stewart SCTP_TCB_SEND_LOCK(stcb); 190f7a77f6fSMichael Tuexen if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 191f7a77f6fSMichael Tuexen /* Check to see if some data queued */ 192f7a77f6fSMichael Tuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 19352be287eSRandall Stewart /* sa_ignore FREED_MEMORY */ 194f7a77f6fSMichael Tuexen sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); 195f7a77f6fSMichael Tuexen if (sp == NULL) { 196f7a77f6fSMichael Tuexen continue; 197f7a77f6fSMichael Tuexen } 1985bead436SRandall Stewart if ((sp->msg_is_complete) && 1995bead436SRandall Stewart (sp->length == 0) && 2005bead436SRandall Stewart (sp->sender_all_done)) { 2015bead436SRandall Stewart /* 2025bead436SRandall Stewart * We are doing differed cleanup. Last time 2035bead436SRandall Stewart * through when we took all the data the 2045bead436SRandall Stewart * sender_all_done was not set. 2055bead436SRandall Stewart */ 2065bead436SRandall Stewart if (sp->put_last_out == 0) { 2075bead436SRandall Stewart SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 2085bead436SRandall Stewart SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", 2095bead436SRandall Stewart sp->sender_all_done, 2105bead436SRandall Stewart sp->length, 2115bead436SRandall Stewart sp->msg_is_complete, 2125bead436SRandall Stewart sp->put_last_out); 2135bead436SRandall Stewart } 214f7a77f6fSMichael Tuexen atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 215f7a77f6fSMichael Tuexen TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); 2164d58b0c3SMichael Tuexen stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1); 2179eea4a2dSMichael Tuexen if (sp->net) { 2185bead436SRandall Stewart sctp_free_remote_addr(sp->net); 2199eea4a2dSMichael Tuexen sp->net = NULL; 2209eea4a2dSMichael Tuexen } 2215bead436SRandall Stewart if (sp->data) { 2225bead436SRandall Stewart sctp_m_freem(sp->data); 2235bead436SRandall Stewart sp->data = NULL; 2245bead436SRandall Stewart } 225689e6a5fSMichael Tuexen sctp_free_a_strmoq(stcb, sp, so_locked); 226124d851aSMichael Tuexen if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { 227124d851aSMichael Tuexen unsent_data++; 228124d851aSMichael Tuexen } 2295bead436SRandall Stewart } else { 2305bead436SRandall Stewart unsent_data++; 231124d851aSMichael Tuexen } 232124d851aSMichael Tuexen if (unsent_data > 0) { 2334a9ef3f8SMichael Tuexen break; 2345bead436SRandall Stewart } 2355bead436SRandall Stewart } 2365bead436SRandall Stewart } 2375bead436SRandall Stewart SCTP_TCB_SEND_UNLOCK(stcb); 2385bead436SRandall Stewart return (unsent_data); 2395bead436SRandall Stewart } 2405bead436SRandall Stewart 241f8829a4aSRandall Stewart static int 2427215cc1bSMichael Tuexen sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) 243f8829a4aSRandall Stewart { 244f8829a4aSRandall Stewart struct sctp_init *init; 245f8829a4aSRandall Stewart struct sctp_association *asoc; 246f8829a4aSRandall Stewart struct sctp_nets *lnet; 247f8829a4aSRandall Stewart unsigned int i; 248f8829a4aSRandall Stewart 249f8829a4aSRandall Stewart init = &cp->init; 250f8829a4aSRandall Stewart asoc = &stcb->asoc; 251f8829a4aSRandall Stewart /* save off parameters */ 252f8829a4aSRandall Stewart asoc->peer_vtag = ntohl(init->initiate_tag); 253f8829a4aSRandall Stewart asoc->peers_rwnd = ntohl(init->a_rwnd); 254493d8e5aSRandall Stewart /* init tsn's */ 255493d8e5aSRandall Stewart asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; 256493d8e5aSRandall Stewart 2579eea4a2dSMichael Tuexen if (!TAILQ_EMPTY(&asoc->nets)) { 258f8829a4aSRandall Stewart /* update any ssthresh's that may have a default */ 259f8829a4aSRandall Stewart TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 260f8829a4aSRandall Stewart lnet->ssthresh = asoc->peers_rwnd; 261b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) { 262f8829a4aSRandall Stewart sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); 26380fefe0aSRandall Stewart } 264f8829a4aSRandall Stewart } 265f8829a4aSRandall Stewart } 2666a91f103SRandall Stewart SCTP_TCB_SEND_LOCK(stcb); 267f8829a4aSRandall Stewart if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { 268f8829a4aSRandall Stewart unsigned int newcnt; 269f8829a4aSRandall Stewart struct sctp_stream_out *outs; 2704a9ef3f8SMichael Tuexen struct sctp_stream_queue_pending *sp, *nsp; 2714a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 272f8829a4aSRandall Stewart 273810ec536SMichael Tuexen /* abandon the upper streams */ 274f8829a4aSRandall Stewart newcnt = ntohs(init->num_inbound_streams); 2754a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 27649656eefSMichael Tuexen if (chk->rec.data.sid >= newcnt) { 277810ec536SMichael Tuexen TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 278810ec536SMichael Tuexen asoc->send_queue_cnt--; 27949656eefSMichael Tuexen if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 28049656eefSMichael Tuexen asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 281a7ad6026SMichael Tuexen #ifdef INVARIANTS 282a7ad6026SMichael Tuexen } else { 28349656eefSMichael Tuexen panic("No chunks on the queues for sid %u.", chk->rec.data.sid); 284a7ad6026SMichael Tuexen #endif 285a7ad6026SMichael Tuexen } 286810ec536SMichael Tuexen if (chk->data != NULL) { 287810ec536SMichael Tuexen sctp_free_bufspace(stcb, asoc, chk, 1); 2881edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 2891edc9dbaSMichael Tuexen 0, chk, SCTP_SO_NOT_LOCKED); 290810ec536SMichael Tuexen if (chk->data) { 291810ec536SMichael Tuexen sctp_m_freem(chk->data); 292810ec536SMichael Tuexen chk->data = NULL; 293810ec536SMichael Tuexen } 294810ec536SMichael Tuexen } 295689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 296810ec536SMichael Tuexen /* sa_ignore FREED_MEMORY */ 297810ec536SMichael Tuexen } 298810ec536SMichael Tuexen } 299f8829a4aSRandall Stewart if (asoc->strmout) { 300f8829a4aSRandall Stewart for (i = newcnt; i < asoc->pre_open_streams; i++) { 301f8829a4aSRandall Stewart outs = &asoc->strmout[i]; 3024a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 3034d58b0c3SMichael Tuexen atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); 304810ec536SMichael Tuexen TAILQ_REMOVE(&outs->outqueue, sp, next); 3054d58b0c3SMichael Tuexen stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1); 306f8829a4aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, 3071edc9dbaSMichael Tuexen stcb, 0, sp, SCTP_SO_NOT_LOCKED); 308f8829a4aSRandall Stewart if (sp->data) { 309f8829a4aSRandall Stewart sctp_m_freem(sp->data); 310f8829a4aSRandall Stewart sp->data = NULL; 311f8829a4aSRandall Stewart } 3129eea4a2dSMichael Tuexen if (sp->net) { 313f8829a4aSRandall Stewart sctp_free_remote_addr(sp->net); 314f8829a4aSRandall Stewart sp->net = NULL; 3159eea4a2dSMichael Tuexen } 316f8829a4aSRandall Stewart /* Free the chunk */ 317689e6a5fSMichael Tuexen sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); 3183c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 319f8829a4aSRandall Stewart } 3207cca1775SRandall Stewart outs->state = SCTP_STREAM_CLOSED; 321f8829a4aSRandall Stewart } 322f8829a4aSRandall Stewart } 323810ec536SMichael Tuexen /* cut back the count */ 324f8829a4aSRandall Stewart asoc->pre_open_streams = newcnt; 325f8829a4aSRandall Stewart } 3266a91f103SRandall Stewart SCTP_TCB_SEND_UNLOCK(stcb); 3277cca1775SRandall Stewart asoc->streamoutcnt = asoc->pre_open_streams; 328fdc4c9d0SMichael Tuexen if (asoc->strmout) { 3297cca1775SRandall Stewart for (i = 0; i < asoc->streamoutcnt; i++) { 3307cca1775SRandall Stewart asoc->strmout[i].state = SCTP_STREAM_OPEN; 3317cca1775SRandall Stewart } 332fdc4c9d0SMichael Tuexen } 333830d754dSRandall Stewart /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ 334830d754dSRandall Stewart asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 335b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 336f8829a4aSRandall Stewart sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 33780fefe0aSRandall Stewart } 338f8829a4aSRandall Stewart /* This is the next one we expect */ 339f8829a4aSRandall Stewart asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; 340f8829a4aSRandall Stewart 341f8829a4aSRandall Stewart asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); 342d6af161aSRandall Stewart asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; 343493d8e5aSRandall Stewart 344f8829a4aSRandall Stewart asoc->advanced_peer_ack_point = asoc->last_acked_seq; 345f8829a4aSRandall Stewart /* open the requested streams */ 346207304d4SRandall Stewart 347f8829a4aSRandall Stewart if (asoc->strmin != NULL) { 348f8829a4aSRandall Stewart /* Free the old ones */ 3496a91f103SRandall Stewart for (i = 0; i < asoc->streamincnt; i++) { 35044249214SRandall Stewart sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); 35144249214SRandall Stewart sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); 3526a91f103SRandall Stewart } 353207304d4SRandall Stewart SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 354f8829a4aSRandall Stewart } 355ee1ccd92SMichael Tuexen if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) { 3566a91f103SRandall Stewart asoc->streamincnt = ntohs(init->num_outbound_streams); 357ee1ccd92SMichael Tuexen } else { 358ee1ccd92SMichael Tuexen asoc->streamincnt = asoc->max_inbound_streams; 3596a91f103SRandall Stewart } 360f8829a4aSRandall Stewart SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * 361207304d4SRandall Stewart sizeof(struct sctp_stream_in), SCTP_M_STRMI); 362f8829a4aSRandall Stewart if (asoc->strmin == NULL) { 363f8829a4aSRandall Stewart /* we didn't get memory for the streams! */ 364ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); 365f8829a4aSRandall Stewart return (-1); 366f8829a4aSRandall Stewart } 367f8829a4aSRandall Stewart for (i = 0; i < asoc->streamincnt; i++) { 36849656eefSMichael Tuexen asoc->strmin[i].sid = i; 36949656eefSMichael Tuexen asoc->strmin[i].last_mid_delivered = 0xffffffff; 370f8829a4aSRandall Stewart TAILQ_INIT(&asoc->strmin[i].inqueue); 37144249214SRandall Stewart TAILQ_INIT(&asoc->strmin[i].uno_inqueue); 37244249214SRandall Stewart asoc->strmin[i].pd_api_started = 0; 3739a6142d8SRandall Stewart asoc->strmin[i].delivery_started = 0; 374f8829a4aSRandall Stewart } 375f8829a4aSRandall Stewart /* 376f8829a4aSRandall Stewart * load_address_from_init will put the addresses into the 377f8829a4aSRandall Stewart * association when the COOKIE is processed or the INIT-ACK is 378f8829a4aSRandall Stewart * processed. Both types of COOKIE's existing and new call this 379f8829a4aSRandall Stewart * routine. It will remove addresses that are no longer in the 380f8829a4aSRandall Stewart * association (for the restarting case where addresses are 381f8829a4aSRandall Stewart * removed). Up front when the INIT arrives we will discard it if it 382f8829a4aSRandall Stewart * is a restart and new addresses have been added. 383f8829a4aSRandall Stewart */ 3843c503c28SRandall Stewart /* sa_ignore MEMLEAK */ 385f8829a4aSRandall Stewart return (0); 386f8829a4aSRandall Stewart } 387f8829a4aSRandall Stewart 388f8829a4aSRandall Stewart /* 389f8829a4aSRandall Stewart * INIT-ACK message processing/consumption returns value < 0 on error 390f8829a4aSRandall Stewart */ 391f8829a4aSRandall Stewart static int 392b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset, 393b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 394f30ac432SMichael Tuexen struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 395f30ac432SMichael Tuexen struct sctp_nets *net, int *abort_no_unlock, 396457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 397f30ac432SMichael Tuexen uint32_t vrf_id) 398f8829a4aSRandall Stewart { 399f8829a4aSRandall Stewart struct sctp_association *asoc; 400f8829a4aSRandall Stewart struct mbuf *op_err; 4016182677fSMichael Tuexen int retval, abort_flag, cookie_found; 4026182677fSMichael Tuexen int initack_limit; 403830d754dSRandall Stewart int nat_friendly = 0; 404f8829a4aSRandall Stewart 405f8829a4aSRandall Stewart /* First verify that we have no illegal param's */ 406f8829a4aSRandall Stewart abort_flag = 0; 4076182677fSMichael Tuexen cookie_found = 0; 408f8829a4aSRandall Stewart 409f8829a4aSRandall Stewart op_err = sctp_arethere_unrecognized_parameters(m, 410f8829a4aSRandall Stewart (offset + sizeof(struct sctp_init_chunk)), 4116182677fSMichael Tuexen &abort_flag, (struct sctp_chunkhdr *)cp, 4126182677fSMichael Tuexen &nat_friendly, &cookie_found); 413f8829a4aSRandall Stewart if (abort_flag) { 414f8829a4aSRandall Stewart /* Send an abort and notify peer */ 415105b68b4SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED); 416bff64a4dSRandall Stewart *abort_no_unlock = 1; 417f8829a4aSRandall Stewart return (-1); 418f8829a4aSRandall Stewart } 4196182677fSMichael Tuexen if (!cookie_found) { 4206182677fSMichael Tuexen uint16_t len; 4216182677fSMichael Tuexen 4220941b9dcSMichael Tuexen /* Only report the missing cookie parameter */ 4230941b9dcSMichael Tuexen if (op_err != NULL) { 4240941b9dcSMichael Tuexen sctp_m_freem(op_err); 4250941b9dcSMichael Tuexen } 4266182677fSMichael Tuexen len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t)); 4276182677fSMichael Tuexen /* We abort with an error of missing mandatory param */ 4286182677fSMichael Tuexen op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA); 4296182677fSMichael Tuexen if (op_err != NULL) { 4306182677fSMichael Tuexen struct sctp_error_missing_param *cause; 4316182677fSMichael Tuexen 4326182677fSMichael Tuexen SCTP_BUF_LEN(op_err) = len; 4336182677fSMichael Tuexen cause = mtod(op_err, struct sctp_error_missing_param *); 4346182677fSMichael Tuexen /* Subtract the reserved param */ 4356182677fSMichael Tuexen cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM); 4366182677fSMichael Tuexen cause->cause.length = htons(len); 4376182677fSMichael Tuexen cause->num_missing_params = htonl(1); 4386182677fSMichael Tuexen cause->type[0] = htons(SCTP_STATE_COOKIE); 4396182677fSMichael Tuexen } 4406182677fSMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 4416182677fSMichael Tuexen src, dst, sh, op_err, 4426182677fSMichael Tuexen mflowtype, mflowid, 4436182677fSMichael Tuexen vrf_id, net->port); 4446182677fSMichael Tuexen *abort_no_unlock = 1; 4456182677fSMichael Tuexen return (-3); 4466182677fSMichael Tuexen } 447f8829a4aSRandall Stewart asoc = &stcb->asoc; 448830d754dSRandall Stewart asoc->peer_supports_nat = (uint8_t)nat_friendly; 449f8829a4aSRandall Stewart /* process the peer's parameters in the INIT-ACK */ 4505f2e1835SMichael Tuexen if (sctp_process_init((struct sctp_init_chunk *)cp, stcb) < 0) { 451645f3a1cSMichael Tuexen if (op_err != NULL) { 452645f3a1cSMichael Tuexen sctp_m_freem(op_err); 453645f3a1cSMichael Tuexen } 4545f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 4555f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n"); 4565f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 4575f2e1835SMichael Tuexen src, dst, sh, op_err, 4585f2e1835SMichael Tuexen mflowtype, mflowid, 4595f2e1835SMichael Tuexen vrf_id, net->port); 4605f2e1835SMichael Tuexen *abort_no_unlock = 1; 4615f2e1835SMichael Tuexen return (-1); 462f8829a4aSRandall Stewart } 463f8829a4aSRandall Stewart initack_limit = offset + ntohs(cp->ch.chunk_length); 464f8829a4aSRandall Stewart /* load all addresses */ 4657215cc1bSMichael Tuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 4665f2e1835SMichael Tuexen offset + sizeof(struct sctp_init_chunk), 4675f2e1835SMichael Tuexen initack_limit, src, dst, NULL, stcb->asoc.port)) < 0) { 468645f3a1cSMichael Tuexen if (op_err != NULL) { 469645f3a1cSMichael Tuexen sctp_m_freem(op_err); 470645f3a1cSMichael Tuexen } 471ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 472ff1ffd74SMichael Tuexen "Problem with address parameters"); 473ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, 474ad81507eSRandall Stewart "Load addresses from INIT causes an abort %d\n", 475ad81507eSRandall Stewart retval); 476b1754ad1SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 477ff1ffd74SMichael Tuexen src, dst, sh, op_err, 478457b4b88SMichael Tuexen mflowtype, mflowid, 479f30ac432SMichael Tuexen vrf_id, net->port); 480bff64a4dSRandall Stewart *abort_no_unlock = 1; 481f8829a4aSRandall Stewart return (-1); 482f8829a4aSRandall Stewart } 48318e198d3SRandall Stewart /* if the peer doesn't support asconf, flush the asconf queue */ 484c79bec9cSMichael Tuexen if (asoc->asconf_supported == 0) { 4854a9ef3f8SMichael Tuexen struct sctp_asconf_addr *param, *nparam; 48618e198d3SRandall Stewart 4874a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { 4884a9ef3f8SMichael Tuexen TAILQ_REMOVE(&asoc->asconf_queue, param, next); 4894a9ef3f8SMichael Tuexen SCTP_FREE(param, SCTP_M_ASC_ADDR); 49018e198d3SRandall Stewart } 49118e198d3SRandall Stewart } 4920053ed28SMichael Tuexen 493f8829a4aSRandall Stewart stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 494f8829a4aSRandall Stewart stcb->asoc.local_hmacs); 495f8829a4aSRandall Stewart if (op_err) { 496f8829a4aSRandall Stewart sctp_queue_op_err(stcb, op_err); 497f8829a4aSRandall Stewart /* queuing will steal away the mbuf chain to the out queue */ 498f8829a4aSRandall Stewart op_err = NULL; 499f8829a4aSRandall Stewart } 500f8829a4aSRandall Stewart /* extract the cookie and queue it to "echo" it back... */ 501b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 502c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 503c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 504c4739e2fSRandall Stewart 0, 505c4739e2fSRandall Stewart SCTP_FROM_SCTP_INPUT, 506c4739e2fSRandall Stewart __LINE__); 507c4739e2fSRandall Stewart } 508f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 509f8829a4aSRandall Stewart net->error_count = 0; 510f8829a4aSRandall Stewart 511f8829a4aSRandall Stewart /* 512f8829a4aSRandall Stewart * Cancel the INIT timer, We do this first before queueing the 513f8829a4aSRandall Stewart * cookie. We always cancel at the primary to assue that we are 514f8829a4aSRandall Stewart * canceling the timer started by the INIT which always goes to the 515f8829a4aSRandall Stewart * primary. 516f8829a4aSRandall Stewart */ 517f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, 518b7d130beSMichael Tuexen asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); 519a5d547adSRandall Stewart 520a5d547adSRandall Stewart /* calculate the RTO */ 52144f2a327SMichael Tuexen sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, 522f79aab18SRandall Stewart SCTP_RTT_FROM_NON_DATA); 5236182677fSMichael Tuexen retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net); 524f8829a4aSRandall Stewart return (retval); 525f8829a4aSRandall Stewart } 5260053ed28SMichael Tuexen 527f8829a4aSRandall Stewart static void 528f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, 529f8829a4aSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net) 530f8829a4aSRandall Stewart { 53124aaac8dSMichael Tuexen union sctp_sockstore store; 53252129fcdSRandall Stewart struct sctp_nets *r_net, *f_net; 533f8829a4aSRandall Stewart struct timeval tv; 534d55b0b1bSRandall Stewart int req_prim = 0; 535ca85e948SMichael Tuexen uint16_t old_error_counter; 536f8829a4aSRandall Stewart 537f8829a4aSRandall Stewart if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { 538f8829a4aSRandall Stewart /* Invalid length */ 539f8829a4aSRandall Stewart return; 540f8829a4aSRandall Stewart } 5410053ed28SMichael Tuexen 542f8829a4aSRandall Stewart memset(&store, 0, sizeof(store)); 543e6194c2eSMichael Tuexen switch (cp->heartbeat.hb_info.addr_family) { 544e6194c2eSMichael Tuexen #ifdef INET 545e6194c2eSMichael Tuexen case AF_INET: 546e6194c2eSMichael Tuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { 54724aaac8dSMichael Tuexen store.sin.sin_family = cp->heartbeat.hb_info.addr_family; 54824aaac8dSMichael Tuexen store.sin.sin_len = cp->heartbeat.hb_info.addr_len; 54924aaac8dSMichael Tuexen store.sin.sin_port = stcb->rport; 55024aaac8dSMichael Tuexen memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address, 55124aaac8dSMichael Tuexen sizeof(store.sin.sin_addr)); 552e6194c2eSMichael Tuexen } else { 553e6194c2eSMichael Tuexen return; 554e6194c2eSMichael Tuexen } 555e6194c2eSMichael Tuexen break; 556e6194c2eSMichael Tuexen #endif 557e6194c2eSMichael Tuexen #ifdef INET6 558e6194c2eSMichael Tuexen case AF_INET6: 559e6194c2eSMichael Tuexen if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { 56024aaac8dSMichael Tuexen store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family; 56124aaac8dSMichael Tuexen store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len; 56224aaac8dSMichael Tuexen store.sin6.sin6_port = stcb->rport; 56323602b60SMichael Tuexen memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr)); 564f8829a4aSRandall Stewart } else { 565f8829a4aSRandall Stewart return; 566f8829a4aSRandall Stewart } 567e6194c2eSMichael Tuexen break; 568e6194c2eSMichael Tuexen #endif 569e6194c2eSMichael Tuexen default: 570e6194c2eSMichael Tuexen return; 571e6194c2eSMichael Tuexen } 57224aaac8dSMichael Tuexen r_net = sctp_findnet(stcb, &store.sa); 573f8829a4aSRandall Stewart if (r_net == NULL) { 574ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); 575f8829a4aSRandall Stewart return; 576f8829a4aSRandall Stewart } 577f8829a4aSRandall Stewart if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && 578f8829a4aSRandall Stewart (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && 579f8829a4aSRandall Stewart (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { 580f8829a4aSRandall Stewart /* 581f8829a4aSRandall Stewart * If the its a HB and it's random value is correct when can 582f8829a4aSRandall Stewart * confirm the destination. 583f8829a4aSRandall Stewart */ 584f8829a4aSRandall Stewart r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 58542551e99SRandall Stewart if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { 58642551e99SRandall Stewart stcb->asoc.primary_destination = r_net; 58742551e99SRandall Stewart r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 58852129fcdSRandall Stewart f_net = TAILQ_FIRST(&stcb->asoc.nets); 58952129fcdSRandall Stewart if (f_net != r_net) { 59042551e99SRandall Stewart /* 59142551e99SRandall Stewart * first one on the list is NOT the primary 592cd0a4ff6SPedro F. Giffuni * sctp_cmpaddr() is much more efficient if 59342551e99SRandall Stewart * the primary is the first on the list, 59442551e99SRandall Stewart * make it so. 59542551e99SRandall Stewart */ 59652129fcdSRandall Stewart TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); 59752129fcdSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); 59842551e99SRandall Stewart } 599d55b0b1bSRandall Stewart req_prim = 1; 60042551e99SRandall Stewart } 601f8829a4aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 602ceaad40aSRandall Stewart stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 603b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, 604b7d130beSMichael Tuexen r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4); 605ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 606f8829a4aSRandall Stewart } 607469a65d1SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 608469a65d1SMichael Tuexen sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 609469a65d1SMichael Tuexen stcb->asoc.overall_error_count, 610469a65d1SMichael Tuexen 0, 611469a65d1SMichael Tuexen SCTP_FROM_SCTP_INPUT, 612469a65d1SMichael Tuexen __LINE__); 613469a65d1SMichael Tuexen } 614469a65d1SMichael Tuexen stcb->asoc.overall_error_count = 0; 615ca85e948SMichael Tuexen old_error_counter = r_net->error_count; 616f8829a4aSRandall Stewart r_net->error_count = 0; 617f8829a4aSRandall Stewart r_net->hb_responded = 1; 618f8829a4aSRandall Stewart tv.tv_sec = cp->heartbeat.hb_info.time_value_1; 619f8829a4aSRandall Stewart tv.tv_usec = cp->heartbeat.hb_info.time_value_2; 620f8829a4aSRandall Stewart /* Now lets do a RTO with this */ 62144f2a327SMichael Tuexen sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, 622f79aab18SRandall Stewart SCTP_RTT_FROM_NON_DATA); 623ca85e948SMichael Tuexen if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { 624ca85e948SMichael Tuexen r_net->dest_state |= SCTP_ADDR_REACHABLE; 625ca85e948SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 6264b1f78e1SMichael Tuexen 0, (void *)r_net, SCTP_SO_NOT_LOCKED); 627ca85e948SMichael Tuexen } 628ca85e948SMichael Tuexen if (r_net->dest_state & SCTP_ADDR_PF) { 629ca85e948SMichael Tuexen r_net->dest_state &= ~SCTP_ADDR_PF; 630ca85e948SMichael Tuexen stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 631ca85e948SMichael Tuexen } 632ca85e948SMichael Tuexen if (old_error_counter > 0) { 633b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 634b7d130beSMichael Tuexen stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5); 635ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); 636ca85e948SMichael Tuexen } 637ca85e948SMichael Tuexen if (r_net == stcb->asoc.primary_destination) { 638ca85e948SMichael Tuexen if (stcb->asoc.alternate) { 639ca85e948SMichael Tuexen /* release the alternate, primary is good */ 640ca85e948SMichael Tuexen sctp_free_remote_addr(stcb->asoc.alternate); 641ca85e948SMichael Tuexen stcb->asoc.alternate = NULL; 642ca85e948SMichael Tuexen } 643ca85e948SMichael Tuexen } 644d55b0b1bSRandall Stewart /* Mobility adaptation */ 645d55b0b1bSRandall Stewart if (req_prim) { 646d55b0b1bSRandall Stewart if ((sctp_is_mobility_feature_on(stcb->sctp_ep, 647d55b0b1bSRandall Stewart SCTP_MOBILITY_BASE) || 648d55b0b1bSRandall Stewart sctp_is_mobility_feature_on(stcb->sctp_ep, 649d55b0b1bSRandall Stewart SCTP_MOBILITY_FASTHANDOFF)) && 650d55b0b1bSRandall Stewart sctp_is_mobility_feature_on(stcb->sctp_ep, 651d55b0b1bSRandall Stewart SCTP_MOBILITY_PRIM_DELETED)) { 652b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, 653b7d130beSMichael Tuexen stcb->sctp_ep, stcb, NULL, 654b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_6); 655d55b0b1bSRandall Stewart if (sctp_is_mobility_feature_on(stcb->sctp_ep, 656d55b0b1bSRandall Stewart SCTP_MOBILITY_FASTHANDOFF)) { 657d55b0b1bSRandall Stewart sctp_assoc_immediate_retrans(stcb, 658d55b0b1bSRandall Stewart stcb->asoc.primary_destination); 659d55b0b1bSRandall Stewart } 660d55b0b1bSRandall Stewart if (sctp_is_mobility_feature_on(stcb->sctp_ep, 661d55b0b1bSRandall Stewart SCTP_MOBILITY_BASE)) { 6629eea4a2dSMichael Tuexen sctp_move_chunks_from_net(stcb, 6639eea4a2dSMichael Tuexen stcb->asoc.deleted_primary); 664d55b0b1bSRandall Stewart } 665a57fb68bSMichael Tuexen sctp_delete_prim_timer(stcb->sctp_ep, stcb); 666d55b0b1bSRandall Stewart } 667d55b0b1bSRandall Stewart } 668f8829a4aSRandall Stewart } 669f8829a4aSRandall Stewart 670830d754dSRandall Stewart static int 671830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb) 672830d754dSRandall Stewart { 673830d754dSRandall Stewart /* 6741325a0deSMichael Tuexen * Return 0 means we want you to proceed with the abort non-zero 6751325a0deSMichael Tuexen * means no abort processing. 676830d754dSRandall Stewart */ 6771325a0deSMichael Tuexen uint32_t new_vtag; 678830d754dSRandall Stewart struct sctpasochead *head; 679830d754dSRandall Stewart 680839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || 681839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 6821325a0deSMichael Tuexen new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 683d28a3a39SMichael Tuexen atomic_add_int(&stcb->asoc.refcnt, 1); 684d28a3a39SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 685d28a3a39SMichael Tuexen SCTP_INP_INFO_WLOCK(); 686d28a3a39SMichael Tuexen SCTP_TCB_LOCK(stcb); 687d28a3a39SMichael Tuexen atomic_subtract_int(&stcb->asoc.refcnt, 1); 6881325a0deSMichael Tuexen } else { 6891325a0deSMichael Tuexen return (0); 690d28a3a39SMichael Tuexen } 691839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) { 692830d754dSRandall Stewart /* generate a new vtag and send init */ 693830d754dSRandall Stewart LIST_REMOVE(stcb, sctp_asocs); 6941325a0deSMichael Tuexen stcb->asoc.my_vtag = new_vtag; 695830d754dSRandall Stewart head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 696b7b84c0eSMichael Tuexen /* 697b7b84c0eSMichael Tuexen * put it in the bucket in the vtag hash of assoc's for the 698b7b84c0eSMichael Tuexen * system 699b7b84c0eSMichael Tuexen */ 700830d754dSRandall Stewart LIST_INSERT_HEAD(head, stcb, sctp_asocs); 701d28a3a39SMichael Tuexen SCTP_INP_INFO_WUNLOCK(); 7021325a0deSMichael Tuexen sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 703830d754dSRandall Stewart return (1); 7041325a0deSMichael Tuexen } else { 705830d754dSRandall Stewart /* 706830d754dSRandall Stewart * treat like a case where the cookie expired i.e.: - dump 707830d754dSRandall Stewart * current cookie. - generate a new vtag. - resend init. 708830d754dSRandall Stewart */ 709830d754dSRandall Stewart /* generate a new vtag and send init */ 710830d754dSRandall Stewart LIST_REMOVE(stcb, sctp_asocs); 711839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); 712830d754dSRandall Stewart sctp_stop_all_cookie_timers(stcb); 713830d754dSRandall Stewart sctp_toss_old_cookies(stcb, &stcb->asoc); 7141325a0deSMichael Tuexen stcb->asoc.my_vtag = new_vtag; 715830d754dSRandall Stewart head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 716b7b84c0eSMichael Tuexen /* 717b7b84c0eSMichael Tuexen * put it in the bucket in the vtag hash of assoc's for the 718b7b84c0eSMichael Tuexen * system 719b7b84c0eSMichael Tuexen */ 720830d754dSRandall Stewart LIST_INSERT_HEAD(head, stcb, sctp_asocs); 721d28a3a39SMichael Tuexen SCTP_INP_INFO_WUNLOCK(); 7221325a0deSMichael Tuexen sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 723830d754dSRandall Stewart return (1); 724830d754dSRandall Stewart } 725830d754dSRandall Stewart return (0); 726830d754dSRandall Stewart } 727830d754dSRandall Stewart 728830d754dSRandall Stewart static int 729830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb, 730830d754dSRandall Stewart struct sctp_nets *net) 731830d754dSRandall Stewart { 732830d754dSRandall Stewart /* 733830d754dSRandall Stewart * return 0 means we want you to proceed with the abort non-zero 734830d754dSRandall Stewart * means no abort processing 735830d754dSRandall Stewart */ 736c79bec9cSMichael Tuexen if (stcb->asoc.auth_supported == 0) { 737830d754dSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); 738830d754dSRandall Stewart return (0); 739830d754dSRandall Stewart } 740830d754dSRandall Stewart sctp_asconf_send_nat_state_update(stcb, net); 741830d754dSRandall Stewart return (1); 742830d754dSRandall Stewart } 743830d754dSRandall Stewart 744701492a5SMichael Tuexen /* Returns 1 if the stcb was aborted, 0 otherwise */ 745701492a5SMichael Tuexen static int 746a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort, 747f8829a4aSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net) 748f8829a4aSRandall Stewart { 749830d754dSRandall Stewart uint16_t len; 750a2b42326SMichael Tuexen uint16_t error; 751ceaad40aSRandall Stewart 752ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); 753f8829a4aSRandall Stewart if (stcb == NULL) 754701492a5SMichael Tuexen return (0); 755f8829a4aSRandall Stewart 756a2b42326SMichael Tuexen len = ntohs(abort->ch.chunk_length); 757701492a5SMichael Tuexen if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) { 758830d754dSRandall Stewart /* 759830d754dSRandall Stewart * Need to check the cause codes for our two magic nat 760830d754dSRandall Stewart * aborts which don't kill the assoc necessarily. 761830d754dSRandall Stewart */ 762701492a5SMichael Tuexen struct sctp_error_cause *cause; 763830d754dSRandall Stewart 764701492a5SMichael Tuexen cause = (struct sctp_error_cause *)(abort + 1); 76586eda749SMichael Tuexen error = ntohs(cause->code); 766a2b42326SMichael Tuexen if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) { 767504ee6a0SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ABORT flags:%x\n", 768a2b42326SMichael Tuexen abort->ch.chunk_flags); 769830d754dSRandall Stewart if (sctp_handle_nat_colliding_state(stcb)) { 770701492a5SMichael Tuexen return (0); 771830d754dSRandall Stewart } 772a2b42326SMichael Tuexen } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) { 773504ee6a0SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ABORT flags:%x\n", 774a2b42326SMichael Tuexen abort->ch.chunk_flags); 775830d754dSRandall Stewart if (sctp_handle_nat_missing_state(stcb, net)) { 776701492a5SMichael Tuexen return (0); 777830d754dSRandall Stewart } 778830d754dSRandall Stewart } 779a2b42326SMichael Tuexen } else { 780a2b42326SMichael Tuexen error = 0; 781830d754dSRandall Stewart } 782f8829a4aSRandall Stewart /* stop any receive timers */ 7836fb7b4fbSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, 784b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_7); 785f8829a4aSRandall Stewart /* notify user of the abort and clean up... */ 786105b68b4SMichael Tuexen sctp_abort_notification(stcb, true, false, error, abort, SCTP_SO_NOT_LOCKED); 787f8829a4aSRandall Stewart /* free the tcb */ 788f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_aborted); 789839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 790839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 791f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 792f8829a4aSRandall Stewart } 793f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 794f1f73e57SRandall Stewart sctp_print_out_track_log(stcb); 795f1f73e57SRandall Stewart #endif 796c4739e2fSRandall Stewart (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 797b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_8); 798ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); 799701492a5SMichael Tuexen return (1); 800f8829a4aSRandall Stewart } 801f8829a4aSRandall Stewart 802f8829a4aSRandall Stewart static void 803ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb) 804ca85e948SMichael Tuexen { 805ca85e948SMichael Tuexen uint32_t cnt_hb_sent; 806ca85e948SMichael Tuexen struct sctp_nets *net; 807ca85e948SMichael Tuexen 808ca85e948SMichael Tuexen cnt_hb_sent = 0; 809ca85e948SMichael Tuexen TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 810ca85e948SMichael Tuexen /* 811ca85e948SMichael Tuexen * For each network start: 1) A pmtu timer. 2) A HB timer 3) 812ca85e948SMichael Tuexen * If the dest in unconfirmed send a hb as well if under 813ca85e948SMichael Tuexen * max_hb_burst have been sent. 814ca85e948SMichael Tuexen */ 815ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); 816ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 817ca85e948SMichael Tuexen if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && 818ca85e948SMichael Tuexen (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { 819ca85e948SMichael Tuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 820ca85e948SMichael Tuexen cnt_hb_sent++; 821ca85e948SMichael Tuexen } 822ca85e948SMichael Tuexen } 823ca85e948SMichael Tuexen if (cnt_hb_sent) { 824ca85e948SMichael Tuexen sctp_chunk_output(stcb->sctp_ep, stcb, 825ca85e948SMichael Tuexen SCTP_OUTPUT_FROM_COOKIE_ACK, 826ca85e948SMichael Tuexen SCTP_SO_NOT_LOCKED); 827ca85e948SMichael Tuexen } 828ca85e948SMichael Tuexen } 829ca85e948SMichael Tuexen 830ca85e948SMichael Tuexen static void 831f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp, 832f8829a4aSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) 833f8829a4aSRandall Stewart { 834f8829a4aSRandall Stewart struct sctp_association *asoc; 835f8829a4aSRandall Stewart int some_on_streamwheel; 836aa1cfca9SMichael Tuexen int old_state; 837ceaad40aSRandall Stewart 838ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 839ad81507eSRandall Stewart "sctp_handle_shutdown: handling SHUTDOWN\n"); 840f8829a4aSRandall Stewart if (stcb == NULL) 841f8829a4aSRandall Stewart return; 842a5d547adSRandall Stewart asoc = &stcb->asoc; 843839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || 844839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 845f8829a4aSRandall Stewart return; 846f8829a4aSRandall Stewart } 847f8829a4aSRandall Stewart if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { 848f8829a4aSRandall Stewart /* Shutdown NOT the expected size */ 849f8829a4aSRandall Stewart return; 850aa1cfca9SMichael Tuexen } 851839d21d6SMichael Tuexen old_state = SCTP_GET_STATE(stcb); 8527215cc1bSMichael Tuexen sctp_update_acked(stcb, cp, abort_flag); 8537e6206afSMichael Tuexen if (*abort_flag) { 8547e6206afSMichael Tuexen return; 8557e6206afSMichael Tuexen } 856a5d547adSRandall Stewart if (asoc->control_pdapi) { 857f8829a4aSRandall Stewart /* 858f8829a4aSRandall Stewart * With a normal shutdown we assume the end of last record. 859f8829a4aSRandall Stewart */ 860f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(stcb->sctp_ep); 86144249214SRandall Stewart if (asoc->control_pdapi->on_strm_q) { 86244249214SRandall Stewart struct sctp_stream_in *strm; 86344249214SRandall Stewart 86444249214SRandall Stewart strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream]; 86544249214SRandall Stewart if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) { 86644249214SRandall Stewart /* Unordered */ 86744249214SRandall Stewart TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm); 86844249214SRandall Stewart asoc->control_pdapi->on_strm_q = 0; 86944249214SRandall Stewart } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) { 87044249214SRandall Stewart /* Ordered */ 87144249214SRandall Stewart TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm); 87244249214SRandall Stewart asoc->control_pdapi->on_strm_q = 0; 87398d5fd97SMichael Tuexen #ifdef INVARIANTS 87444249214SRandall Stewart } else { 87544249214SRandall Stewart panic("Unknown state on ctrl:%p on_strm_q:%d", 87644249214SRandall Stewart asoc->control_pdapi, 87744249214SRandall Stewart asoc->control_pdapi->on_strm_q); 87898d5fd97SMichael Tuexen #endif 87944249214SRandall Stewart } 88044249214SRandall Stewart } 881a5d547adSRandall Stewart asoc->control_pdapi->end_added = 1; 882a5d547adSRandall Stewart asoc->control_pdapi->pdapi_aborted = 1; 883a5d547adSRandall Stewart asoc->control_pdapi = NULL; 884f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 885828318e1SMichael Tuexen if (stcb->sctp_socket) { 88650cec919SRandall Stewart sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 887828318e1SMichael Tuexen } 888f8829a4aSRandall Stewart } 889f8829a4aSRandall Stewart /* goto SHUTDOWN_RECEIVED state to block new requests */ 890f8829a4aSRandall Stewart if (stcb->sctp_socket) { 891839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && 892839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) && 893839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) { 894839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED); 895b7b84c0eSMichael Tuexen /* 896b7b84c0eSMichael Tuexen * notify upper layer that peer has initiated a 897b7b84c0eSMichael Tuexen * shutdown 898b7b84c0eSMichael Tuexen */ 899ceaad40aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 900f8829a4aSRandall Stewart 901f8829a4aSRandall Stewart /* reset time */ 9026e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 903f8829a4aSRandall Stewart } 904f8829a4aSRandall Stewart } 905839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) { 906f8829a4aSRandall Stewart /* 907f8829a4aSRandall Stewart * stop the shutdown timer, since we WILL move to 908f8829a4aSRandall Stewart * SHUTDOWN-ACK-SENT. 909f8829a4aSRandall Stewart */ 910b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, 911b7d130beSMichael Tuexen net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9); 912f8829a4aSRandall Stewart } 9135bead436SRandall Stewart /* Now is there unsent data on a stream somewhere? */ 914689e6a5fSMichael Tuexen some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 915f8829a4aSRandall Stewart 916f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->send_queue) || 917f8829a4aSRandall Stewart !TAILQ_EMPTY(&asoc->sent_queue) || 918f8829a4aSRandall Stewart some_on_streamwheel) { 919f8829a4aSRandall Stewart /* By returning we will push more data out */ 920f8829a4aSRandall Stewart return; 921f8829a4aSRandall Stewart } else { 922f8829a4aSRandall Stewart /* no outstanding data to send, so move on... */ 923f8829a4aSRandall Stewart /* send SHUTDOWN-ACK */ 924f8829a4aSRandall Stewart /* move to SHUTDOWN-ACK-SENT state */ 925839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 926839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 927f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 928f8829a4aSRandall Stewart } 929839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 930839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT); 93112af6654SMichael Tuexen sctp_stop_timers_for_shutdown(stcb); 932c39cfa1fSMichael Tuexen sctp_send_shutdown_ack(stcb, net); 933aa1cfca9SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, 934aa1cfca9SMichael Tuexen stcb->sctp_ep, stcb, net); 935aa1cfca9SMichael Tuexen } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) { 936aa1cfca9SMichael Tuexen sctp_send_shutdown_ack(stcb, net); 937aa1cfca9SMichael Tuexen } 938f8829a4aSRandall Stewart } 939f8829a4aSRandall Stewart } 940f8829a4aSRandall Stewart 941f8829a4aSRandall Stewart static void 9427215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, 9437b470fc3SMichael Tuexen struct sctp_tcb *stcb, 9447b470fc3SMichael Tuexen struct sctp_nets *net) 945f8829a4aSRandall Stewart { 946f8829a4aSRandall Stewart struct sctp_association *asoc; 947ceaad40aSRandall Stewart 948ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 949ad81507eSRandall Stewart "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); 950f8829a4aSRandall Stewart if (stcb == NULL) 951f8829a4aSRandall Stewart return; 952f8829a4aSRandall Stewart 953f8829a4aSRandall Stewart asoc = &stcb->asoc; 954f8829a4aSRandall Stewart /* process according to association state */ 955839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || 956839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 9577b470fc3SMichael Tuexen /* unexpected SHUTDOWN-ACK... do OOTB handling... */ 9587b470fc3SMichael Tuexen sctp_send_shutdown_complete(stcb, net, 1); 9597b470fc3SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 9607b470fc3SMichael Tuexen return; 9617b470fc3SMichael Tuexen } 962839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && 963839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 964f8829a4aSRandall Stewart /* unexpected SHUTDOWN-ACK... so ignore... */ 965f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 966f8829a4aSRandall Stewart return; 967f8829a4aSRandall Stewart } 968a5d547adSRandall Stewart if (asoc->control_pdapi) { 969f8829a4aSRandall Stewart /* 970f8829a4aSRandall Stewart * With a normal shutdown we assume the end of last record. 971f8829a4aSRandall Stewart */ 972f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(stcb->sctp_ep); 973a5d547adSRandall Stewart asoc->control_pdapi->end_added = 1; 974a5d547adSRandall Stewart asoc->control_pdapi->pdapi_aborted = 1; 975a5d547adSRandall Stewart asoc->control_pdapi = NULL; 976f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 97750cec919SRandall Stewart sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 978f8829a4aSRandall Stewart } 97956f778aaSMichael Tuexen #ifdef INVARIANTS 980f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->send_queue) || 981f8829a4aSRandall Stewart !TAILQ_EMPTY(&asoc->sent_queue) || 982124d851aSMichael Tuexen sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) { 98356f778aaSMichael Tuexen panic("Queues are not empty when handling SHUTDOWN-ACK"); 984f8829a4aSRandall Stewart } 98556f778aaSMichael Tuexen #endif 986f8829a4aSRandall Stewart /* stop the timer */ 987b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, 988b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_10); 989f8829a4aSRandall Stewart /* send SHUTDOWN-COMPLETE */ 9907b470fc3SMichael Tuexen sctp_send_shutdown_complete(stcb, net, 0); 991f8829a4aSRandall Stewart /* notify upper layer protocol */ 992f8829a4aSRandall Stewart if (stcb->sctp_socket) { 993f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 994f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 9954e88d37aSMichael Tuexen stcb->sctp_socket->so_snd.sb_cc = 0; 996f8829a4aSRandall Stewart } 997c75fdd30SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 998f8829a4aSRandall Stewart } 999f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 1000f8829a4aSRandall Stewart /* free the TCB but first save off the ep */ 1001c4739e2fSRandall Stewart (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1002b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_11); 1003f8829a4aSRandall Stewart } 1004f8829a4aSRandall Stewart 1005f8829a4aSRandall Stewart static void 10066fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type) 1007f8829a4aSRandall Stewart { 10083e87bccdSMichael Tuexen switch (chunk_type) { 1009f8829a4aSRandall Stewart case SCTP_ASCONF_ACK: 1010f8829a4aSRandall Stewart case SCTP_ASCONF: 10116fb7b4fbSMichael Tuexen sctp_asconf_cleanup(stcb); 1012f8829a4aSRandall Stewart break; 101344249214SRandall Stewart case SCTP_IFORWARD_CUM_TSN: 1014f8829a4aSRandall Stewart case SCTP_FORWARD_CUM_TSN: 1015dd973b0eSMichael Tuexen stcb->asoc.prsctp_supported = 0; 1016f8829a4aSRandall Stewart break; 1017f8829a4aSRandall Stewart default: 1018ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 10193e87bccdSMichael Tuexen "Peer does not support chunk type %d (0x%x).\n", 10203e87bccdSMichael Tuexen chunk_type, chunk_type); 1021f8829a4aSRandall Stewart break; 1022f8829a4aSRandall Stewart } 1023f8829a4aSRandall Stewart } 1024f8829a4aSRandall Stewart 1025f8829a4aSRandall Stewart /* 1026f8829a4aSRandall Stewart * Skip past the param header and then we will find the param that caused the 1027f8829a4aSRandall Stewart * problem. There are a number of param's in a ASCONF OR the prsctp param 1028f8829a4aSRandall Stewart * these will turn of specific features. 1029c79bec9cSMichael Tuexen * XXX: Is this the right thing to do? 1030f8829a4aSRandall Stewart */ 1031f8829a4aSRandall Stewart static void 10323e87bccdSMichael Tuexen sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type) 1033f8829a4aSRandall Stewart { 10343e87bccdSMichael Tuexen switch (parameter_type) { 1035f8829a4aSRandall Stewart /* pr-sctp draft */ 1036f8829a4aSRandall Stewart case SCTP_PRSCTP_SUPPORTED: 1037dd973b0eSMichael Tuexen stcb->asoc.prsctp_supported = 0; 1038f8829a4aSRandall Stewart break; 1039f8829a4aSRandall Stewart case SCTP_SUPPORTED_CHUNK_EXT: 1040f8829a4aSRandall Stewart break; 1041f8829a4aSRandall Stewart /* draft-ietf-tsvwg-addip-sctp */ 1042830d754dSRandall Stewart case SCTP_HAS_NAT_SUPPORT: 1043830d754dSRandall Stewart stcb->asoc.peer_supports_nat = 0; 1044830d754dSRandall Stewart break; 1045f8829a4aSRandall Stewart case SCTP_ADD_IP_ADDRESS: 1046f8829a4aSRandall Stewart case SCTP_DEL_IP_ADDRESS: 1047f8829a4aSRandall Stewart case SCTP_SET_PRIM_ADDR: 1048c79bec9cSMichael Tuexen stcb->asoc.asconf_supported = 0; 1049f8829a4aSRandall Stewart break; 1050f8829a4aSRandall Stewart case SCTP_SUCCESS_REPORT: 1051f8829a4aSRandall Stewart case SCTP_ERROR_CAUSE_IND: 1052ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); 1053ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 1054ad81507eSRandall Stewart "Turning off ASCONF to this strange peer\n"); 1055c79bec9cSMichael Tuexen stcb->asoc.asconf_supported = 0; 1056f8829a4aSRandall Stewart break; 1057f8829a4aSRandall Stewart default: 1058ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 10593e87bccdSMichael Tuexen "Peer does not support param type %d (0x%x)??\n", 10603e87bccdSMichael Tuexen parameter_type, parameter_type); 1061f8829a4aSRandall Stewart break; 1062f8829a4aSRandall Stewart } 1063f8829a4aSRandall Stewart } 1064f8829a4aSRandall Stewart 1065f8829a4aSRandall Stewart static int 1066f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch, 10673e87bccdSMichael Tuexen struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 1068f8829a4aSRandall Stewart { 10693e87bccdSMichael Tuexen struct sctp_error_cause *cause; 1070f8829a4aSRandall Stewart struct sctp_association *asoc; 10713e87bccdSMichael Tuexen uint32_t remaining_length, adjust; 10723e87bccdSMichael Tuexen uint16_t code, cause_code, cause_length; 1073ceaad40aSRandall Stewart 1074f8829a4aSRandall Stewart /* parse through all of the errors and process */ 1075f8829a4aSRandall Stewart asoc = &stcb->asoc; 10763e87bccdSMichael Tuexen cause = (struct sctp_error_cause *)((caddr_t)ch + 1077f8829a4aSRandall Stewart sizeof(struct sctp_chunkhdr)); 10783e87bccdSMichael Tuexen remaining_length = ntohs(ch->chunk_length); 10793e87bccdSMichael Tuexen if (remaining_length > limit) { 10803e87bccdSMichael Tuexen remaining_length = limit; 10813e87bccdSMichael Tuexen } 10823e87bccdSMichael Tuexen if (remaining_length >= sizeof(struct sctp_chunkhdr)) { 10833e87bccdSMichael Tuexen remaining_length -= sizeof(struct sctp_chunkhdr); 10843e87bccdSMichael Tuexen } else { 10853e87bccdSMichael Tuexen remaining_length = 0; 10863e87bccdSMichael Tuexen } 10873e87bccdSMichael Tuexen code = 0; 10883e87bccdSMichael Tuexen while (remaining_length >= sizeof(struct sctp_error_cause)) { 1089f8829a4aSRandall Stewart /* Process an Error Cause */ 10903e87bccdSMichael Tuexen cause_code = ntohs(cause->code); 10913e87bccdSMichael Tuexen cause_length = ntohs(cause->length); 10923e87bccdSMichael Tuexen if ((cause_length > remaining_length) || (cause_length == 0)) { 10933e87bccdSMichael Tuexen /* Invalid cause length, possibly due to truncation. */ 10943e87bccdSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n", 10953e87bccdSMichael Tuexen remaining_length, cause_length); 1096f8829a4aSRandall Stewart return (0); 1097f8829a4aSRandall Stewart } 10983e87bccdSMichael Tuexen if (code == 0) { 1099389b1b11SMichael Tuexen /* report the first error cause */ 11003e87bccdSMichael Tuexen code = cause_code; 1101389b1b11SMichael Tuexen } 11023e87bccdSMichael Tuexen switch (cause_code) { 1103f8829a4aSRandall Stewart case SCTP_CAUSE_INVALID_STREAM: 1104f8829a4aSRandall Stewart case SCTP_CAUSE_MISSING_PARAM: 1105f8829a4aSRandall Stewart case SCTP_CAUSE_INVALID_PARAM: 1106f8829a4aSRandall Stewart case SCTP_CAUSE_NO_USER_DATA: 11073e87bccdSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n", 11083e87bccdSMichael Tuexen cause_code); 1109f8829a4aSRandall Stewart break; 1110830d754dSRandall Stewart case SCTP_CAUSE_NAT_COLLIDING_STATE: 1111504ee6a0SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ERROR flags: %x\n", 1112830d754dSRandall Stewart ch->chunk_flags); 1113830d754dSRandall Stewart if (sctp_handle_nat_colliding_state(stcb)) { 1114830d754dSRandall Stewart return (0); 1115830d754dSRandall Stewart } 1116830d754dSRandall Stewart break; 1117830d754dSRandall Stewart case SCTP_CAUSE_NAT_MISSING_STATE: 1118504ee6a0SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ERROR flags: %x\n", 1119830d754dSRandall Stewart ch->chunk_flags); 1120830d754dSRandall Stewart if (sctp_handle_nat_missing_state(stcb, net)) { 1121830d754dSRandall Stewart return (0); 1122830d754dSRandall Stewart } 1123830d754dSRandall Stewart break; 1124f8829a4aSRandall Stewart case SCTP_CAUSE_STALE_COOKIE: 1125f8829a4aSRandall Stewart /* 1126f8829a4aSRandall Stewart * We only act if we have echoed a cookie and are 1127f8829a4aSRandall Stewart * waiting. 1128f8829a4aSRandall Stewart */ 11293e87bccdSMichael Tuexen if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) && 1130839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 11313e87bccdSMichael Tuexen struct sctp_error_stale_cookie *stale_cookie; 1132f8829a4aSRandall Stewart 11333e87bccdSMichael Tuexen stale_cookie = (struct sctp_error_stale_cookie *)cause; 1134a92d5016SMichael Tuexen /* stable_time is in usec, convert to msec. */ 1135a92d5016SMichael Tuexen asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time) / 1000; 1136a92d5016SMichael Tuexen /* Double it to be more robust on RTX. */ 11373e87bccdSMichael Tuexen asoc->cookie_preserve_req *= 2; 1138f8829a4aSRandall Stewart asoc->stale_cookie_count++; 1139f8829a4aSRandall Stewart if (asoc->stale_cookie_count > 1140f8829a4aSRandall Stewart asoc->max_init_times) { 1141105b68b4SMichael Tuexen sctp_abort_notification(stcb, false, true, 0, NULL, SCTP_SO_NOT_LOCKED); 1142f8829a4aSRandall Stewart /* now free the asoc */ 1143c4739e2fSRandall Stewart (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 1144b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_12); 1145f8829a4aSRandall Stewart return (-1); 1146f8829a4aSRandall Stewart } 1147f8829a4aSRandall Stewart /* blast back to INIT state */ 1148830d754dSRandall Stewart sctp_toss_old_cookies(stcb, &stcb->asoc); 1149839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); 1150f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(stcb); 1151ceaad40aSRandall Stewart sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 1152f8829a4aSRandall Stewart } 1153f8829a4aSRandall Stewart break; 1154f8829a4aSRandall Stewart case SCTP_CAUSE_UNRESOLVABLE_ADDR: 1155f8829a4aSRandall Stewart /* 1156f8829a4aSRandall Stewart * Nothing we can do here, we don't do hostname 1157f8829a4aSRandall Stewart * addresses so if the peer does not like my IPv6 1158f8829a4aSRandall Stewart * (or IPv4 for that matter) it does not matter. If 1159f8829a4aSRandall Stewart * they don't support that type of address, they can 1160f8829a4aSRandall Stewart * NOT possibly get that packet type... i.e. with no 1161cd0a4ff6SPedro F. Giffuni * IPv6 you can't receive a IPv6 packet. so we can 1162f8829a4aSRandall Stewart * safely ignore this one. If we ever added support 1163f8829a4aSRandall Stewart * for HOSTNAME Addresses, then we would need to do 1164f8829a4aSRandall Stewart * something here. 1165f8829a4aSRandall Stewart */ 1166f8829a4aSRandall Stewart break; 1167f8829a4aSRandall Stewart case SCTP_CAUSE_UNRECOG_CHUNK: 11683e87bccdSMichael Tuexen if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) { 11693e87bccdSMichael Tuexen struct sctp_error_unrecognized_chunk *unrec_chunk; 11703e87bccdSMichael Tuexen 11713e87bccdSMichael Tuexen unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause; 11726fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type); 11733e87bccdSMichael Tuexen } 1174f8829a4aSRandall Stewart break; 1175f8829a4aSRandall Stewart case SCTP_CAUSE_UNRECOG_PARAM: 11763e87bccdSMichael Tuexen /* XXX: We only consider the first parameter */ 11773e87bccdSMichael Tuexen if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) { 11783e87bccdSMichael Tuexen struct sctp_paramhdr *unrec_parameter; 11793e87bccdSMichael Tuexen 11803e87bccdSMichael Tuexen unrec_parameter = (struct sctp_paramhdr *)(cause + 1); 11813e87bccdSMichael Tuexen sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type)); 11823e87bccdSMichael Tuexen } 1183f8829a4aSRandall Stewart break; 1184f8829a4aSRandall Stewart case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: 1185f8829a4aSRandall Stewart /* 1186f8829a4aSRandall Stewart * We ignore this since the timer will drive out a 1187f8829a4aSRandall Stewart * new cookie anyway and there timer will drive us 1188f8829a4aSRandall Stewart * to send a SHUTDOWN_COMPLETE. We can't send one 1189f8829a4aSRandall Stewart * here since we don't have their tag. 1190f8829a4aSRandall Stewart */ 1191f8829a4aSRandall Stewart break; 1192f8829a4aSRandall Stewart case SCTP_CAUSE_DELETING_LAST_ADDR: 1193f8829a4aSRandall Stewart case SCTP_CAUSE_RESOURCE_SHORTAGE: 1194f8829a4aSRandall Stewart case SCTP_CAUSE_DELETING_SRC_ADDR: 1195f8829a4aSRandall Stewart /* 1196f8829a4aSRandall Stewart * We should NOT get these here, but in a 119793164cf9SRandall Stewart * ASCONF-ACK. 1198f8829a4aSRandall Stewart */ 11993e87bccdSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n", 12003e87bccdSMichael Tuexen cause_code); 1201f8829a4aSRandall Stewart break; 1202f8829a4aSRandall Stewart case SCTP_CAUSE_OUT_OF_RESC: 1203f8829a4aSRandall Stewart /* 1204f8829a4aSRandall Stewart * And what, pray tell do we do with the fact that 1205f8829a4aSRandall Stewart * the peer is out of resources? Not really sure we 120693164cf9SRandall Stewart * could do anything but abort. I suspect this 1207f8829a4aSRandall Stewart * should have came WITH an abort instead of in a 1208f8829a4aSRandall Stewart * OP-ERROR. 1209f8829a4aSRandall Stewart */ 1210f8829a4aSRandall Stewart break; 1211f8829a4aSRandall Stewart default: 12123e87bccdSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n", 12133e87bccdSMichael Tuexen cause_code); 1214f8829a4aSRandall Stewart break; 1215f8829a4aSRandall Stewart } 12163e87bccdSMichael Tuexen adjust = SCTP_SIZE32(cause_length); 12173e87bccdSMichael Tuexen if (remaining_length >= adjust) { 12183e87bccdSMichael Tuexen remaining_length -= adjust; 12193e87bccdSMichael Tuexen } else { 12203e87bccdSMichael Tuexen remaining_length = 0; 1221f8829a4aSRandall Stewart } 12223e87bccdSMichael Tuexen cause = (struct sctp_error_cause *)((caddr_t)cause + adjust); 12233e87bccdSMichael Tuexen } 12243e87bccdSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED); 1225f8829a4aSRandall Stewart return (0); 1226f8829a4aSRandall Stewart } 1227f8829a4aSRandall Stewart 1228f8829a4aSRandall Stewart static int 1229b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, 1230b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, 1231f30ac432SMichael Tuexen struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, 1232f30ac432SMichael Tuexen struct sctp_nets *net, int *abort_no_unlock, 1233457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 1234f30ac432SMichael Tuexen uint32_t vrf_id) 1235f8829a4aSRandall Stewart { 1236f8829a4aSRandall Stewart struct sctp_init_ack *init_ack; 1237f8829a4aSRandall Stewart struct mbuf *op_err; 1238f8829a4aSRandall Stewart 1239ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 1240ad81507eSRandall Stewart "sctp_handle_init_ack: handling INIT-ACK\n"); 1241ad81507eSRandall Stewart 1242f8829a4aSRandall Stewart if (stcb == NULL) { 1243ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 1244ad81507eSRandall Stewart "sctp_handle_init_ack: TCB is null\n"); 1245f8829a4aSRandall Stewart return (-1); 1246f8829a4aSRandall Stewart } 1247f8829a4aSRandall Stewart init_ack = &cp->init; 1248059ec222SMichael Tuexen /* Validate parameters. */ 1249059ec222SMichael Tuexen if ((ntohl(init_ack->initiate_tag) == 0) || 1250059ec222SMichael Tuexen (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) || 1251059ec222SMichael Tuexen (ntohs(init_ack->num_inbound_streams) == 0) || 1252059ec222SMichael Tuexen (ntohs(init_ack->num_outbound_streams) == 0)) { 1253f8829a4aSRandall Stewart /* protocol error... send an abort */ 1254ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, ""); 1255b1754ad1SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 1256b1754ad1SMichael Tuexen src, dst, sh, op_err, 1257457b4b88SMichael Tuexen mflowtype, mflowid, 1258f30ac432SMichael Tuexen vrf_id, net->port); 1259bff64a4dSRandall Stewart *abort_no_unlock = 1; 1260f8829a4aSRandall Stewart return (-1); 1261f8829a4aSRandall Stewart } 1262f8829a4aSRandall Stewart /* process according to association state... */ 1263839d21d6SMichael Tuexen switch (SCTP_GET_STATE(stcb)) { 1264f8829a4aSRandall Stewart case SCTP_STATE_COOKIE_WAIT: 1265f8829a4aSRandall Stewart /* this is the expected state for this chunk */ 1266f8829a4aSRandall Stewart /* process the INIT-ACK parameters */ 1267f8829a4aSRandall Stewart if (stcb->asoc.primary_destination->dest_state & 1268f8829a4aSRandall Stewart SCTP_ADDR_UNCONFIRMED) { 1269f8829a4aSRandall Stewart /* 1270f8829a4aSRandall Stewart * The primary is where we sent the INIT, we can 1271f8829a4aSRandall Stewart * always consider it confirmed when the INIT-ACK is 1272f8829a4aSRandall Stewart * returned. Do this before we load addresses 1273f8829a4aSRandall Stewart * though. 1274f8829a4aSRandall Stewart */ 1275f8829a4aSRandall Stewart stcb->asoc.primary_destination->dest_state &= 1276f8829a4aSRandall Stewart ~SCTP_ADDR_UNCONFIRMED; 1277f8829a4aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 1278ceaad40aSRandall Stewart stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); 1279f8829a4aSRandall Stewart } 1280b1754ad1SMichael Tuexen if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb, 1281f30ac432SMichael Tuexen net, abort_no_unlock, 1282457b4b88SMichael Tuexen mflowtype, mflowid, 1283f30ac432SMichael Tuexen vrf_id) < 0) { 1284f8829a4aSRandall Stewart /* error in parsing parameters */ 1285f8829a4aSRandall Stewart return (-1); 1286f8829a4aSRandall Stewart } 1287f8829a4aSRandall Stewart /* update our state */ 1288ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); 1289839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED); 1290f8829a4aSRandall Stewart 1291f8829a4aSRandall Stewart /* reset the RTO calc */ 1292b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 1293c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 1294c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 1295c4739e2fSRandall Stewart 0, 1296c4739e2fSRandall Stewart SCTP_FROM_SCTP_INPUT, 1297c4739e2fSRandall Stewart __LINE__); 1298c4739e2fSRandall Stewart } 1299f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 13006e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1301f8829a4aSRandall Stewart /* 1302f8829a4aSRandall Stewart * collapse the init timer back in case of a exponential 1303a5d547adSRandall Stewart * backoff 1304f8829a4aSRandall Stewart */ 1305f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, 1306f8829a4aSRandall Stewart stcb, net); 1307f8829a4aSRandall Stewart /* 1308f8829a4aSRandall Stewart * the send at the end of the inbound data processing will 1309f8829a4aSRandall Stewart * cause the cookie to be sent 1310f8829a4aSRandall Stewart */ 1311f8829a4aSRandall Stewart break; 1312f8829a4aSRandall Stewart case SCTP_STATE_SHUTDOWN_SENT: 1313f8829a4aSRandall Stewart /* incorrect state... discard */ 1314f8829a4aSRandall Stewart break; 1315f8829a4aSRandall Stewart case SCTP_STATE_COOKIE_ECHOED: 1316f8829a4aSRandall Stewart /* incorrect state... discard */ 1317f8829a4aSRandall Stewart break; 1318f8829a4aSRandall Stewart case SCTP_STATE_OPEN: 1319f8829a4aSRandall Stewart /* incorrect state... discard */ 1320f8829a4aSRandall Stewart break; 1321f8829a4aSRandall Stewart case SCTP_STATE_EMPTY: 1322f8829a4aSRandall Stewart case SCTP_STATE_INUSE: 1323f8829a4aSRandall Stewart default: 1324f8829a4aSRandall Stewart /* incorrect state... discard */ 1325f8829a4aSRandall Stewart return (-1); 1326f8829a4aSRandall Stewart break; 1327f8829a4aSRandall Stewart } 1328ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); 1329f8829a4aSRandall Stewart return (0); 1330f8829a4aSRandall Stewart } 1331f8829a4aSRandall Stewart 1332830d754dSRandall Stewart static struct sctp_tcb * 1333830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1334b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 1335830d754dSRandall Stewart struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1336830d754dSRandall Stewart struct sctp_inpcb *inp, struct sctp_nets **netp, 1337830d754dSRandall Stewart struct sockaddr *init_src, int *notification, 1338830d754dSRandall Stewart int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1339457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 1340830d754dSRandall Stewart uint32_t vrf_id, uint16_t port); 1341830d754dSRandall Stewart 1342f8829a4aSRandall Stewart /* 1343f8829a4aSRandall Stewart * handle a state cookie for an existing association m: input packet mbuf 1344f8829a4aSRandall Stewart * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a 1345f8829a4aSRandall Stewart * "split" mbuf and the cookie signature does not exist offset: offset into 1346f8829a4aSRandall Stewart * mbuf to the cookie-echo chunk 1347f8829a4aSRandall Stewart */ 1348f8829a4aSRandall Stewart static struct sctp_tcb * 1349f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, 1350b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 1351f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1352830d754dSRandall Stewart struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, 13537215cc1bSMichael Tuexen struct sockaddr *init_src, int *notification, 1354f30ac432SMichael Tuexen int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1355457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 1356f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 1357f8829a4aSRandall Stewart { 1358f8829a4aSRandall Stewart struct sctp_association *asoc; 1359f8829a4aSRandall Stewart struct sctp_init_chunk *init_cp, init_buf; 1360f8829a4aSRandall Stewart struct sctp_init_ack_chunk *initack_cp, initack_buf; 1361aa6db9a0SMichael Tuexen struct sctp_asconf_addr *aparam, *naparam; 1362aa6db9a0SMichael Tuexen struct sctp_asconf_ack *aack, *naack; 1363aa6db9a0SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 1364aa6db9a0SMichael Tuexen struct sctp_stream_reset_list *strrst, *nstrrst; 1365aa6db9a0SMichael Tuexen struct sctp_queued_to_read *sq, *nsq; 1366830d754dSRandall Stewart struct sctp_nets *net; 1367830d754dSRandall Stewart struct mbuf *op_err; 13688c8e10b7SMichael Tuexen struct timeval old; 1369a5d547adSRandall Stewart int init_offset, initack_offset, i; 1370f8829a4aSRandall Stewart int retval; 13717f34832bSRandall Stewart int spec_flag = 0; 13724c9179adSRandall Stewart uint32_t how_indx; 1373f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS) 1374f0396ad1SMichael Tuexen int j; 1375f0396ad1SMichael Tuexen #endif 1376f0396ad1SMichael Tuexen 1377830d754dSRandall Stewart net = *netp; 1378f8829a4aSRandall Stewart /* I know that the TCB is non-NULL from the caller */ 1379f8829a4aSRandall Stewart asoc = &stcb->asoc; 1380f42a358aSRandall Stewart for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { 138144b7479bSRandall Stewart if (asoc->cookie_how[how_indx] == 0) 138244b7479bSRandall Stewart break; 138344b7479bSRandall Stewart } 138444b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) { 138544b7479bSRandall Stewart asoc->cookie_how[how_indx] = 1; 138644b7479bSRandall Stewart } 1387839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 1388f8829a4aSRandall Stewart /* SHUTDOWN came in after sending INIT-ACK */ 1389f8829a4aSRandall Stewart sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); 1390ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, ""); 1391b1754ad1SMichael Tuexen sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, 1392d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 1393c54a18d2SRandall Stewart vrf_id, net->port); 139444b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 139544b7479bSRandall Stewart asoc->cookie_how[how_indx] = 2; 139612dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1397f8829a4aSRandall Stewart return (NULL); 1398f8829a4aSRandall Stewart } 1399f8829a4aSRandall Stewart /* 1400f8829a4aSRandall Stewart * find and validate the INIT chunk in the cookie (peer's info) the 1401f8829a4aSRandall Stewart * INIT should start after the cookie-echo header struct (chunk 1402f8829a4aSRandall Stewart * header, state cookie header struct) 1403f8829a4aSRandall Stewart */ 1404f8829a4aSRandall Stewart init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); 1405f8829a4aSRandall Stewart 1406f8829a4aSRandall Stewart init_cp = (struct sctp_init_chunk *) 1407f8829a4aSRandall Stewart sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 1408f8829a4aSRandall Stewart (uint8_t *)&init_buf); 1409f8829a4aSRandall Stewart if (init_cp == NULL) { 1410f8829a4aSRandall Stewart /* could not pull a INIT chunk in cookie */ 141112dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1412f8829a4aSRandall Stewart return (NULL); 1413f8829a4aSRandall Stewart } 1414f8829a4aSRandall Stewart if (init_cp->ch.chunk_type != SCTP_INITIATION) { 141512dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1416f8829a4aSRandall Stewart return (NULL); 1417f8829a4aSRandall Stewart } 1418f8829a4aSRandall Stewart /* 1419f8829a4aSRandall Stewart * find and validate the INIT-ACK chunk in the cookie (my info) the 1420f8829a4aSRandall Stewart * INIT-ACK follows the INIT chunk 1421f8829a4aSRandall Stewart */ 142260990c0cSMichael Tuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 1423f8829a4aSRandall Stewart initack_cp = (struct sctp_init_ack_chunk *) 1424f8829a4aSRandall Stewart sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 1425f8829a4aSRandall Stewart (uint8_t *)&initack_buf); 1426f8829a4aSRandall Stewart if (initack_cp == NULL) { 1427f8829a4aSRandall Stewart /* could not pull INIT-ACK chunk in cookie */ 142812dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1429f8829a4aSRandall Stewart return (NULL); 1430f8829a4aSRandall Stewart } 1431f8829a4aSRandall Stewart if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 143212dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1433f8829a4aSRandall Stewart return (NULL); 1434f8829a4aSRandall Stewart } 1435f8829a4aSRandall Stewart if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1436f8829a4aSRandall Stewart (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { 1437f8829a4aSRandall Stewart /* 1438f8829a4aSRandall Stewart * case D in Section 5.2.4 Table 2: MMAA process accordingly 1439f8829a4aSRandall Stewart * to get into the OPEN state 1440f8829a4aSRandall Stewart */ 144144b7479bSRandall Stewart if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 1442851b7298SRandall Stewart /*- 1443851b7298SRandall Stewart * Opps, this means that we somehow generated two vtag's 1444851b7298SRandall Stewart * the same. I.e. we did: 1445851b7298SRandall Stewart * Us Peer 1446851b7298SRandall Stewart * <---INIT(tag=a)------ 1447851b7298SRandall Stewart * ----INIT-ACK(tag=t)--> 1448851b7298SRandall Stewart * ----INIT(tag=t)------> *1 1449851b7298SRandall Stewart * <---INIT-ACK(tag=a)--- 1450851b7298SRandall Stewart * <----CE(tag=t)------------- *2 1451851b7298SRandall Stewart * 1452851b7298SRandall Stewart * At point *1 we should be generating a different 1453851b7298SRandall Stewart * tag t'. Which means we would throw away the CE and send 1454851b7298SRandall Stewart * ours instead. Basically this is case C (throw away side). 1455851b7298SRandall Stewart */ 1456851b7298SRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 1457851b7298SRandall Stewart asoc->cookie_how[how_indx] = 17; 145812dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1459851b7298SRandall Stewart return (NULL); 146044b7479bSRandall Stewart } 1461839d21d6SMichael Tuexen switch (SCTP_GET_STATE(stcb)) { 1462f8829a4aSRandall Stewart case SCTP_STATE_COOKIE_WAIT: 146344b7479bSRandall Stewart case SCTP_STATE_COOKIE_ECHOED: 1464f8829a4aSRandall Stewart /* 146517205eccSRandall Stewart * INIT was sent but got a COOKIE_ECHO with the 146644b7479bSRandall Stewart * correct tags... just accept it...but we must 146744b7479bSRandall Stewart * process the init so that we can make sure we have 146844b7479bSRandall Stewart * the right seq no's. 1469f8829a4aSRandall Stewart */ 1470f8829a4aSRandall Stewart /* First we must process the INIT !! */ 14715f2e1835SMichael Tuexen if (sctp_process_init(init_cp, stcb) < 0) { 147244b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 147344b7479bSRandall Stewart asoc->cookie_how[how_indx] = 3; 14745f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 14755f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n"); 14765f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 14775f2e1835SMichael Tuexen src, dst, sh, op_err, 14785f2e1835SMichael Tuexen mflowtype, mflowid, 14795f2e1835SMichael Tuexen vrf_id, net->port); 1480f8829a4aSRandall Stewart return (NULL); 1481f8829a4aSRandall Stewart } 1482f8829a4aSRandall Stewart /* we have already processed the INIT so no problem */ 1483b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, 1484b7d130beSMichael Tuexen stcb, net, 1485b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_13); 1486b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, 1487b7d130beSMichael Tuexen stcb, net, 1488b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_14); 1489f8829a4aSRandall Stewart /* update current state */ 1490839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) 1491f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1492f42a358aSRandall Stewart else 1493f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1494c4739e2fSRandall Stewart 1495839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 1496f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1497f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 14986fb7b4fbSMichael Tuexen stcb->sctp_ep, stcb, NULL); 1499f8829a4aSRandall Stewart } 1500f42a358aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1501a5d547adSRandall Stewart sctp_stop_all_cookie_timers(stcb); 1502f8829a4aSRandall Stewart if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1503f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 15045d08768aSMichael Tuexen (!SCTP_IS_LISTENING(inp))) { 1505f8829a4aSRandall Stewart /* 1506f8829a4aSRandall Stewart * Here is where collision would go if we 1507f8829a4aSRandall Stewart * did a connect() and instead got a 1508f8829a4aSRandall Stewart * init/init-ack/cookie done before the 1509f8829a4aSRandall Stewart * init-ack came back.. 1510f8829a4aSRandall Stewart */ 1511f8829a4aSRandall Stewart stcb->sctp_ep->sctp_flags |= 1512f8829a4aSRandall Stewart SCTP_PCB_FLAGS_CONNECTED; 1513ceaad40aSRandall Stewart soisconnected(stcb->sctp_socket); 1514f8829a4aSRandall Stewart } 1515f8829a4aSRandall Stewart /* notify upper layer */ 1516f8829a4aSRandall Stewart *notification = SCTP_NOTIFY_ASSOC_UP; 1517f8829a4aSRandall Stewart /* 1518f8829a4aSRandall Stewart * since we did not send a HB make sure we don't 1519f8829a4aSRandall Stewart * double things 1520f8829a4aSRandall Stewart */ 15218c8e10b7SMichael Tuexen old.tv_sec = cookie->time_entered.tv_sec; 15228c8e10b7SMichael Tuexen old.tv_usec = cookie->time_entered.tv_usec; 1523f8829a4aSRandall Stewart net->hb_responded = 1; 152444f2a327SMichael Tuexen sctp_calculate_rto(stcb, asoc, net, &old, 1525f79aab18SRandall Stewart SCTP_RTT_FROM_NON_DATA); 1526f8829a4aSRandall Stewart 1527f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks && 1528f8829a4aSRandall Stewart (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { 1529f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 1530f8829a4aSRandall Stewart inp, stcb, NULL); 1531f8829a4aSRandall Stewart } 1532f8829a4aSRandall Stewart break; 1533f8829a4aSRandall Stewart default: 1534f8829a4aSRandall Stewart /* 1535f8829a4aSRandall Stewart * we're in the OPEN state (or beyond), so peer must 1536f8829a4aSRandall Stewart * have simply lost the COOKIE-ACK 1537f8829a4aSRandall Stewart */ 1538f8829a4aSRandall Stewart break; 1539f8829a4aSRandall Stewart } /* end switch */ 1540a5d547adSRandall Stewart sctp_stop_all_cookie_timers(stcb); 1541655c200cSAlexander Motin if ((retval = sctp_load_addresses_from_init(stcb, m, 1542f8829a4aSRandall Stewart init_offset + sizeof(struct sctp_init_chunk), 15435f2e1835SMichael Tuexen initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) { 154444b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 154544b7479bSRandall Stewart asoc->cookie_how[how_indx] = 4; 15465f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 15475f2e1835SMichael Tuexen "Problem with address parameters"); 15485f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, 15495f2e1835SMichael Tuexen "Load addresses from INIT causes an abort %d\n", 15505f2e1835SMichael Tuexen retval); 15515f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 15525f2e1835SMichael Tuexen src, dst, sh, op_err, 15535f2e1835SMichael Tuexen mflowtype, mflowid, 15545f2e1835SMichael Tuexen vrf_id, net->port); 1555f8829a4aSRandall Stewart return (NULL); 1556f8829a4aSRandall Stewart } 1557f8829a4aSRandall Stewart /* respond with a COOKIE-ACK */ 1558a5d547adSRandall Stewart sctp_toss_old_cookies(stcb, asoc); 1559f8829a4aSRandall Stewart sctp_send_cookie_ack(stcb); 156044b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 156144b7479bSRandall Stewart asoc->cookie_how[how_indx] = 5; 1562f8829a4aSRandall Stewart return (stcb); 156317205eccSRandall Stewart } 15640053ed28SMichael Tuexen 1565f8829a4aSRandall Stewart if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1566f8829a4aSRandall Stewart ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && 1567f8829a4aSRandall Stewart cookie->tie_tag_my_vtag == 0 && 1568f8829a4aSRandall Stewart cookie->tie_tag_peer_vtag == 0) { 1569f8829a4aSRandall Stewart /* 1570f8829a4aSRandall Stewart * case C in Section 5.2.4 Table 2: XMOO silently discard 1571f8829a4aSRandall Stewart */ 157244b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 157344b7479bSRandall Stewart asoc->cookie_how[how_indx] = 6; 157412dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1575f8829a4aSRandall Stewart return (NULL); 1576f8829a4aSRandall Stewart } 1577830d754dSRandall Stewart /* 1578830d754dSRandall Stewart * If nat support, and the below and stcb is established, send back 1579830d754dSRandall Stewart * a ABORT(colliding state) if we are established. 1580830d754dSRandall Stewart */ 1581839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) && 1582830d754dSRandall Stewart (asoc->peer_supports_nat) && 1583830d754dSRandall Stewart ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1584830d754dSRandall Stewart ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1585830d754dSRandall Stewart (asoc->peer_vtag == 0)))) { 1586830d754dSRandall Stewart /* 1587830d754dSRandall Stewart * Special case - Peer's support nat. We may have two init's 1588830d754dSRandall Stewart * that we gave out the same tag on since one was not 1589830d754dSRandall Stewart * established.. i.e. we get INIT from host-1 behind the nat 1590830d754dSRandall Stewart * and we respond tag-a, we get a INIT from host-2 behind 1591830d754dSRandall Stewart * the nat and we get tag-a again. Then we bring up host-1 1592830d754dSRandall Stewart * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1). 1593830d754dSRandall Stewart * Now we have colliding state. We must send an abort here 1594830d754dSRandall Stewart * with colliding state indication. 1595830d754dSRandall Stewart */ 1596ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, ""); 1597b1754ad1SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err, 1598d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 1599f30ac432SMichael Tuexen vrf_id, port); 160012dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1601830d754dSRandall Stewart return (NULL); 1602830d754dSRandall Stewart } 1603830d754dSRandall Stewart if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && 1604830d754dSRandall Stewart ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || 1605830d754dSRandall Stewart (asoc->peer_vtag == 0))) { 1606f8829a4aSRandall Stewart /* 1607f8829a4aSRandall Stewart * case B in Section 5.2.4 Table 2: MXAA or MOAA my info 1608f8829a4aSRandall Stewart * should be ok, re-accept peer info 1609f8829a4aSRandall Stewart */ 161044b7479bSRandall Stewart if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { 161144b7479bSRandall Stewart /* 161244b7479bSRandall Stewart * Extension of case C. If we hit this, then the 161344b7479bSRandall Stewart * random number generator returned the same vtag 161444b7479bSRandall Stewart * when we first sent our INIT-ACK and when we later 161544b7479bSRandall Stewart * sent our INIT. The side with the seq numbers that 161644b7479bSRandall Stewart * are different will be the one that normnally 161744b7479bSRandall Stewart * would have hit case C. This in effect "extends" 161844b7479bSRandall Stewart * our vtags in this collision case to be 64 bits. 161944b7479bSRandall Stewart * The same collision could occur aka you get both 162044b7479bSRandall Stewart * vtag and seq number the same twice in a row.. but 162144b7479bSRandall Stewart * is much less likely. If it did happen then we 162244b7479bSRandall Stewart * would proceed through and bring up the assoc.. we 162344b7479bSRandall Stewart * may end up with the wrong stream setup however.. 162444b7479bSRandall Stewart * which would be bad.. but there is no way to 162544b7479bSRandall Stewart * tell.. until we send on a stream that does not 162644b7479bSRandall Stewart * exist :-) 162744b7479bSRandall Stewart */ 162844b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 162944b7479bSRandall Stewart asoc->cookie_how[how_indx] = 7; 163044b7479bSRandall Stewart 163112dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 163244b7479bSRandall Stewart return (NULL); 163344b7479bSRandall Stewart } 163444b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 163544b7479bSRandall Stewart asoc->cookie_how[how_indx] = 8; 1636b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 1637b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_15); 1638f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(stcb); 1639f8829a4aSRandall Stewart /* 1640f8829a4aSRandall Stewart * since we did not send a HB make sure we don't double 1641f8829a4aSRandall Stewart * things 1642f8829a4aSRandall Stewart */ 1643f8829a4aSRandall Stewart net->hb_responded = 1; 1644f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks && 1645f8829a4aSRandall Stewart sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1646f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, 1647f8829a4aSRandall Stewart NULL); 1648f8829a4aSRandall Stewart } 1649f8829a4aSRandall Stewart asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 16500066de1cSMichael Tuexen if (asoc->pre_open_streams < asoc->streamoutcnt) { 16510066de1cSMichael Tuexen asoc->pre_open_streams = asoc->streamoutcnt; 16520066de1cSMichael Tuexen } 1653f8829a4aSRandall Stewart 16547f34832bSRandall Stewart if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { 16557f34832bSRandall Stewart /* 16567f34832bSRandall Stewart * Ok the peer probably discarded our data (if we 16577f34832bSRandall Stewart * echoed a cookie+data). So anything on the 16587f34832bSRandall Stewart * sent_queue should be marked for retransmit, we 16597f34832bSRandall Stewart * may not get something to kick us so it COULD 16607f34832bSRandall Stewart * still take a timeout to move these.. but it can't 16617f34832bSRandall Stewart * hurt to mark them. 16627f34832bSRandall Stewart */ 16637f34832bSRandall Stewart 16647f34832bSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 16657f34832bSRandall Stewart if (chk->sent < SCTP_DATAGRAM_RESEND) { 16667f34832bSRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 1667b54d3a6cSRandall Stewart sctp_flight_size_decrease(chk); 1668b54d3a6cSRandall Stewart sctp_total_flight_decrease(stcb, chk); 16695e54f665SRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 16707f34832bSRandall Stewart spec_flag++; 16717f34832bSRandall Stewart } 16727f34832bSRandall Stewart } 16737f34832bSRandall Stewart } 1674f8829a4aSRandall Stewart /* process the INIT info (peer's info) */ 16755f2e1835SMichael Tuexen if (sctp_process_init(init_cp, stcb) < 0) { 167644b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 167744b7479bSRandall Stewart asoc->cookie_how[how_indx] = 9; 16785f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 16795f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n"); 16805f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 16815f2e1835SMichael Tuexen src, dst, sh, op_err, 16825f2e1835SMichael Tuexen mflowtype, mflowid, 16835f2e1835SMichael Tuexen vrf_id, net->port); 1684f8829a4aSRandall Stewart return (NULL); 1685f8829a4aSRandall Stewart } 16865f2e1835SMichael Tuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 1687f8829a4aSRandall Stewart init_offset + sizeof(struct sctp_init_chunk), 16885f2e1835SMichael Tuexen initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) { 168944b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 169044b7479bSRandall Stewart asoc->cookie_how[how_indx] = 10; 16915f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 16925f2e1835SMichael Tuexen "Problem with address parameters"); 16935f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, 16945f2e1835SMichael Tuexen "Load addresses from INIT causes an abort %d\n", 16955f2e1835SMichael Tuexen retval); 16965f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 16975f2e1835SMichael Tuexen src, dst, sh, op_err, 16985f2e1835SMichael Tuexen mflowtype, mflowid, 16995f2e1835SMichael Tuexen vrf_id, net->port); 1700f8829a4aSRandall Stewart return (NULL); 1701f8829a4aSRandall Stewart } 1702839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || 1703839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 1704f8829a4aSRandall Stewart *notification = SCTP_NOTIFY_ASSOC_UP; 1705f8829a4aSRandall Stewart 1706f8829a4aSRandall Stewart if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1707f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 17085d08768aSMichael Tuexen (!SCTP_IS_LISTENING(inp))) { 1709c8e55b3cSMichael Tuexen stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1710ceaad40aSRandall Stewart soisconnected(stcb->sctp_socket); 1711f8829a4aSRandall Stewart } 1712839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) 1713f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 1714f42a358aSRandall Stewart else 1715f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1716f42a358aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1717839d21d6SMichael Tuexen } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { 1718f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_restartestab); 1719f42a358aSRandall Stewart } else { 1720f42a358aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); 1721f8829a4aSRandall Stewart } 1722839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 1723f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1724f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 17256fb7b4fbSMichael Tuexen stcb->sctp_ep, stcb, NULL); 1726f8829a4aSRandall Stewart } 1727f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(stcb); 1728a5d547adSRandall Stewart sctp_toss_old_cookies(stcb, asoc); 1729f8829a4aSRandall Stewart sctp_send_cookie_ack(stcb); 17307f34832bSRandall Stewart if (spec_flag) { 17317f34832bSRandall Stewart /* 17327f34832bSRandall Stewart * only if we have retrans set do we do this. What 17337f34832bSRandall Stewart * this call does is get only the COOKIE-ACK out and 17347f34832bSRandall Stewart * then when we return the normal call to 17357f34832bSRandall Stewart * sctp_chunk_output will get the retrans out behind 17367f34832bSRandall Stewart * this. 17377f34832bSRandall Stewart */ 1738ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); 17397f34832bSRandall Stewart } 174044b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 174144b7479bSRandall Stewart asoc->cookie_how[how_indx] = 11; 174244b7479bSRandall Stewart 1743f8829a4aSRandall Stewart return (stcb); 1744f8829a4aSRandall Stewart } 1745f8829a4aSRandall Stewart if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && 1746f8829a4aSRandall Stewart ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && 1747f8829a4aSRandall Stewart cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && 1748f8829a4aSRandall Stewart cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && 1749f8829a4aSRandall Stewart cookie->tie_tag_peer_vtag != 0) { 1750f8829a4aSRandall Stewart struct sctpasochead *head; 175156f778aaSMichael Tuexen 1752830d754dSRandall Stewart if (asoc->peer_supports_nat) { 1753eec6aed5SMichael Tuexen struct sctp_tcb *local_stcb; 1754eec6aed5SMichael Tuexen 1755830d754dSRandall Stewart /* 175656f778aaSMichael Tuexen * This is a gross gross hack. Just call the 1757830d754dSRandall Stewart * cookie_new code since we are allowing a duplicate 1758830d754dSRandall Stewart * association. I hope this works... 1759830d754dSRandall Stewart */ 1760eec6aed5SMichael Tuexen local_stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, 1761b1754ad1SMichael Tuexen sh, cookie, cookie_len, 1762830d754dSRandall Stewart inp, netp, init_src, notification, 1763830d754dSRandall Stewart auth_skipped, auth_offset, auth_len, 1764457b4b88SMichael Tuexen mflowtype, mflowid, 1765eec6aed5SMichael Tuexen vrf_id, port); 1766eec6aed5SMichael Tuexen if (local_stcb == NULL) { 1767eec6aed5SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1768eec6aed5SMichael Tuexen } 1769eec6aed5SMichael Tuexen return (local_stcb); 1770830d754dSRandall Stewart } 1771f8829a4aSRandall Stewart /* 1772f8829a4aSRandall Stewart * case A in Section 5.2.4 Table 2: XXMM (peer restarted) 1773f8829a4aSRandall Stewart */ 1774a5d547adSRandall Stewart /* temp code */ 177544b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 177644b7479bSRandall Stewart asoc->cookie_how[how_indx] = 12; 1777a89481d3SMichael Tuexen sctp_stop_association_timers(stcb, false); 1778f8829a4aSRandall Stewart /* notify upper layer */ 1779f8829a4aSRandall Stewart *notification = SCTP_NOTIFY_ASSOC_RESTART; 1780a5d547adSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 1781839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) && 1782839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && 1783839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) { 1784f42a358aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_currestab); 1785f42a358aSRandall Stewart } 1786839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { 1787f42a358aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_restartestab); 1788839d21d6SMichael Tuexen } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) { 1789f42a358aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); 1790f42a358aSRandall Stewart } 1791139bc87fSRandall Stewart if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 1792839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 1793139bc87fSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 17946fb7b4fbSMichael Tuexen stcb->sctp_ep, stcb, NULL); 1795139bc87fSRandall Stewart 1796839d21d6SMichael Tuexen } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) { 1797139bc87fSRandall Stewart /* move to OPEN state, if not in SHUTDOWN_SENT */ 1798839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 1799139bc87fSRandall Stewart } 18000066de1cSMichael Tuexen if (asoc->pre_open_streams < asoc->streamoutcnt) { 18010066de1cSMichael Tuexen asoc->pre_open_streams = asoc->streamoutcnt; 18020066de1cSMichael Tuexen } 1803139bc87fSRandall Stewart asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); 1804139bc87fSRandall Stewart asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; 1805c54a18d2SRandall Stewart asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 1806139bc87fSRandall Stewart asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; 1807139bc87fSRandall Stewart asoc->str_reset_seq_in = asoc->init_seq_number; 1808139bc87fSRandall Stewart asoc->advanced_peer_ack_point = asoc->last_acked_seq; 18096f155d69SMichael Tuexen asoc->send_sack = 1; 1810a89481d3SMichael Tuexen asoc->data_pkts_seen = 0; 1811a89481d3SMichael Tuexen asoc->last_data_chunk_from = NULL; 1812a89481d3SMichael Tuexen asoc->last_control_chunk_from = NULL; 1813a89481d3SMichael Tuexen asoc->last_net_cmt_send_started = NULL; 18140696e120SRandall Stewart if (asoc->mapping_array) { 1815139bc87fSRandall Stewart memset(asoc->mapping_array, 0, 1816139bc87fSRandall Stewart asoc->mapping_array_size); 18170696e120SRandall Stewart } 181877acdc25SRandall Stewart if (asoc->nr_mapping_array) { 1819830d754dSRandall Stewart memset(asoc->nr_mapping_array, 0, 1820b5c16493SMichael Tuexen asoc->mapping_array_size); 1821830d754dSRandall Stewart } 1822a5d547adSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1823a5d547adSRandall Stewart SCTP_INP_INFO_WLOCK(); 1824a5d547adSRandall Stewart SCTP_INP_WLOCK(stcb->sctp_ep); 1825a5d547adSRandall Stewart SCTP_TCB_LOCK(stcb); 1826a5d547adSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 1827f8829a4aSRandall Stewart /* send up all the data */ 18287f34832bSRandall Stewart SCTP_TCB_SEND_LOCK(stcb); 1829f8829a4aSRandall Stewart 1830f5d30f7fSMichael Tuexen sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED); 1831a5d547adSRandall Stewart for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1832325c8c46SMichael Tuexen stcb->asoc.strmout[i].chunks_on_queues = 0; 1833f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS) 1834f0396ad1SMichael Tuexen for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { 1835f0396ad1SMichael Tuexen asoc->strmout[i].abandoned_sent[j] = 0; 1836f0396ad1SMichael Tuexen asoc->strmout[i].abandoned_unsent[j] = 0; 1837f0396ad1SMichael Tuexen } 1838f0396ad1SMichael Tuexen #else 1839f0396ad1SMichael Tuexen asoc->strmout[i].abandoned_sent[0] = 0; 1840f0396ad1SMichael Tuexen asoc->strmout[i].abandoned_unsent[0] = 0; 1841f0396ad1SMichael Tuexen #endif 184263d5b568SMichael Tuexen stcb->asoc.strmout[i].next_mid_ordered = 0; 184363d5b568SMichael Tuexen stcb->asoc.strmout[i].next_mid_unordered = 0; 18447a051c0aSMichael Tuexen stcb->asoc.strmout[i].sid = i; 1845a5d547adSRandall Stewart stcb->asoc.strmout[i].last_msg_incomplete = 0; 1846a5d547adSRandall Stewart } 1847aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { 1848aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); 1849aa6db9a0SMichael Tuexen SCTP_FREE(strrst, SCTP_M_STRESET); 1850aa6db9a0SMichael Tuexen } 1851aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { 1852aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); 1853aa6db9a0SMichael Tuexen if (sq->data) { 1854aa6db9a0SMichael Tuexen sctp_m_freem(sq->data); 1855aa6db9a0SMichael Tuexen sq->data = NULL; 1856aa6db9a0SMichael Tuexen } 1857aa6db9a0SMichael Tuexen sctp_free_remote_addr(sq->whoFrom); 1858aa6db9a0SMichael Tuexen sq->whoFrom = NULL; 1859aa6db9a0SMichael Tuexen sq->stcb = NULL; 1860aa6db9a0SMichael Tuexen sctp_free_a_readq(stcb, sq); 1861aa6db9a0SMichael Tuexen } 1862aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 1863aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 1864aa6db9a0SMichael Tuexen if (chk->data) { 1865aa6db9a0SMichael Tuexen sctp_m_freem(chk->data); 1866aa6db9a0SMichael Tuexen chk->data = NULL; 1867aa6db9a0SMichael Tuexen } 1868aa6db9a0SMichael Tuexen if (chk->holds_key_ref) 1869aa6db9a0SMichael Tuexen sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 1870aa6db9a0SMichael Tuexen sctp_free_remote_addr(chk->whoTo); 1871aa6db9a0SMichael Tuexen SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 1872aa6db9a0SMichael Tuexen SCTP_DECR_CHK_COUNT(); 1873aa6db9a0SMichael Tuexen } 1874a89481d3SMichael Tuexen asoc->ctrl_queue_cnt = 0; 1875a89481d3SMichael Tuexen asoc->str_reset = NULL; 1876a89481d3SMichael Tuexen asoc->stream_reset_outstanding = 0; 1877aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { 1878aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); 1879aa6db9a0SMichael Tuexen if (chk->data) { 1880aa6db9a0SMichael Tuexen sctp_m_freem(chk->data); 1881aa6db9a0SMichael Tuexen chk->data = NULL; 1882aa6db9a0SMichael Tuexen } 1883aa6db9a0SMichael Tuexen if (chk->holds_key_ref) 1884aa6db9a0SMichael Tuexen sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 1885aa6db9a0SMichael Tuexen sctp_free_remote_addr(chk->whoTo); 1886aa6db9a0SMichael Tuexen SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 1887aa6db9a0SMichael Tuexen SCTP_DECR_CHK_COUNT(); 1888aa6db9a0SMichael Tuexen } 1889aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { 1890aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 1891aa6db9a0SMichael Tuexen SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 1892aa6db9a0SMichael Tuexen } 1893aa6db9a0SMichael Tuexen TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { 1894aa6db9a0SMichael Tuexen TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); 1895aa6db9a0SMichael Tuexen if (aack->data != NULL) { 1896aa6db9a0SMichael Tuexen sctp_m_freem(aack->data); 1897aa6db9a0SMichael Tuexen } 1898aa6db9a0SMichael Tuexen SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); 1899aa6db9a0SMichael Tuexen } 1900aa6db9a0SMichael Tuexen 1901f8829a4aSRandall Stewart /* process the INIT-ACK info (my info) */ 1902f8829a4aSRandall Stewart asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); 1903f8829a4aSRandall Stewart asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 1904f8829a4aSRandall Stewart 1905f8829a4aSRandall Stewart /* pull from vtag hash */ 1906f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_asocs); 1907f8829a4aSRandall Stewart /* re-insert to new vtag position */ 1908b3f1ea41SRandall Stewart head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 1909b3f1ea41SRandall Stewart SCTP_BASE_INFO(hashasocmark))]; 1910f8829a4aSRandall Stewart /* 1911f8829a4aSRandall Stewart * put it in the bucket in the vtag hash of assoc's for the 1912f8829a4aSRandall Stewart * system 1913f8829a4aSRandall Stewart */ 1914f8829a4aSRandall Stewart LIST_INSERT_HEAD(head, stcb, sctp_asocs); 1915f8829a4aSRandall Stewart 19167f34832bSRandall Stewart SCTP_TCB_SEND_UNLOCK(stcb); 1917a5d547adSRandall Stewart SCTP_INP_WUNLOCK(stcb->sctp_ep); 1918a5d547adSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 191956f778aaSMichael Tuexen asoc->total_flight = 0; 192056f778aaSMichael Tuexen asoc->total_flight_count = 0; 192156f778aaSMichael Tuexen /* process the INIT info (peer's info) */ 19225f2e1835SMichael Tuexen if (sctp_process_init(init_cp, stcb) < 0) { 192344b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 192444b7479bSRandall Stewart asoc->cookie_how[how_indx] = 13; 19255f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 19265f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n"); 19275f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 19285f2e1835SMichael Tuexen src, dst, sh, op_err, 19295f2e1835SMichael Tuexen mflowtype, mflowid, 19305f2e1835SMichael Tuexen vrf_id, net->port); 1931f8829a4aSRandall Stewart return (NULL); 1932f8829a4aSRandall Stewart } 1933f8829a4aSRandall Stewart /* 1934f8829a4aSRandall Stewart * since we did not send a HB make sure we don't double 1935f8829a4aSRandall Stewart * things 1936f8829a4aSRandall Stewart */ 1937f8829a4aSRandall Stewart net->hb_responded = 1; 1938f8829a4aSRandall Stewart 19395f2e1835SMichael Tuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 1940f8829a4aSRandall Stewart init_offset + sizeof(struct sctp_init_chunk), 19415f2e1835SMichael Tuexen initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) { 194244b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 194344b7479bSRandall Stewart asoc->cookie_how[how_indx] = 14; 19445f2e1835SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 19455f2e1835SMichael Tuexen "Problem with address parameters"); 19465f2e1835SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, 19475f2e1835SMichael Tuexen "Load addresses from INIT causes an abort %d\n", 19485f2e1835SMichael Tuexen retval); 19495f2e1835SMichael Tuexen sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, 19505f2e1835SMichael Tuexen src, dst, sh, op_err, 19515f2e1835SMichael Tuexen mflowtype, mflowid, 19525f2e1835SMichael Tuexen vrf_id, net->port); 1953f8829a4aSRandall Stewart return (NULL); 1954f8829a4aSRandall Stewart } 1955f8829a4aSRandall Stewart /* respond with a COOKIE-ACK */ 1956f8829a4aSRandall Stewart sctp_send_cookie_ack(stcb); 195744b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 195844b7479bSRandall Stewart asoc->cookie_how[how_indx] = 15; 1959a89481d3SMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE) && 1960a89481d3SMichael Tuexen (asoc->sctp_autoclose_ticks > 0)) { 1961a89481d3SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1962a89481d3SMichael Tuexen } 1963f8829a4aSRandall Stewart return (stcb); 1964f8829a4aSRandall Stewart } 196544b7479bSRandall Stewart if (how_indx < sizeof(asoc->cookie_how)) 196644b7479bSRandall Stewart asoc->cookie_how[how_indx] = 16; 1967f8829a4aSRandall Stewart /* all other cases... */ 196812dda000SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 1969f8829a4aSRandall Stewart return (NULL); 1970f8829a4aSRandall Stewart } 1971f8829a4aSRandall Stewart 1972f8829a4aSRandall Stewart /* 1973f8829a4aSRandall Stewart * handle a state cookie for a new association m: input packet mbuf chain-- 1974f8829a4aSRandall Stewart * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf 1975f8829a4aSRandall Stewart * and the cookie signature does not exist offset: offset into mbuf to the 1976f8829a4aSRandall Stewart * cookie-echo chunk length: length of the cookie chunk to: where the init 1977f8829a4aSRandall Stewart * was from returns a new TCB 1978f8829a4aSRandall Stewart */ 1979f30ac432SMichael Tuexen static struct sctp_tcb * 1980f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, 1981b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 1982f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, 1983f8829a4aSRandall Stewart struct sctp_inpcb *inp, struct sctp_nets **netp, 1984f8829a4aSRandall Stewart struct sockaddr *init_src, int *notification, 198517205eccSRandall Stewart int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 1986457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 1987c54a18d2SRandall Stewart uint32_t vrf_id, uint16_t port) 1988f8829a4aSRandall Stewart { 1989f8829a4aSRandall Stewart struct sctp_tcb *stcb; 1990f8829a4aSRandall Stewart struct sctp_init_chunk *init_cp, init_buf; 1991f8829a4aSRandall Stewart struct sctp_init_ack_chunk *initack_cp, initack_buf; 199224aaac8dSMichael Tuexen union sctp_sockstore store; 1993f8829a4aSRandall Stewart struct sctp_association *asoc; 1994f8829a4aSRandall Stewart int init_offset, initack_offset, initack_limit; 1995f8829a4aSRandall Stewart int retval; 1996f8829a4aSRandall Stewart int error = 0; 19978262311cSMichael Tuexen uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 1998ceaad40aSRandall Stewart 1999f8829a4aSRandall Stewart /* 2000f8829a4aSRandall Stewart * find and validate the INIT chunk in the cookie (peer's info) the 2001f8829a4aSRandall Stewart * INIT should start after the cookie-echo header struct (chunk 2002f8829a4aSRandall Stewart * header, state cookie header struct) 2003f8829a4aSRandall Stewart */ 2004f8829a4aSRandall Stewart init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); 2005f8829a4aSRandall Stewart init_cp = (struct sctp_init_chunk *) 2006f8829a4aSRandall Stewart sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), 2007f8829a4aSRandall Stewart (uint8_t *)&init_buf); 2008f8829a4aSRandall Stewart if (init_cp == NULL) { 2009f8829a4aSRandall Stewart /* could not pull a INIT chunk in cookie */ 2010ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, 2011ad81507eSRandall Stewart "process_cookie_new: could not pull INIT chunk hdr\n"); 2012f8829a4aSRandall Stewart return (NULL); 2013f8829a4aSRandall Stewart } 2014f8829a4aSRandall Stewart if (init_cp->ch.chunk_type != SCTP_INITIATION) { 2015ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); 2016f8829a4aSRandall Stewart return (NULL); 2017f8829a4aSRandall Stewart } 201860990c0cSMichael Tuexen initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); 2019f8829a4aSRandall Stewart /* 2020f8829a4aSRandall Stewart * find and validate the INIT-ACK chunk in the cookie (my info) the 2021f8829a4aSRandall Stewart * INIT-ACK follows the INIT chunk 2022f8829a4aSRandall Stewart */ 2023f8829a4aSRandall Stewart initack_cp = (struct sctp_init_ack_chunk *) 2024f8829a4aSRandall Stewart sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), 2025f8829a4aSRandall Stewart (uint8_t *)&initack_buf); 2026f8829a4aSRandall Stewart if (initack_cp == NULL) { 2027f8829a4aSRandall Stewart /* could not pull INIT-ACK chunk in cookie */ 2028ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); 2029f8829a4aSRandall Stewart return (NULL); 2030f8829a4aSRandall Stewart } 2031f8829a4aSRandall Stewart if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { 2032f8829a4aSRandall Stewart return (NULL); 2033f8829a4aSRandall Stewart } 2034f8829a4aSRandall Stewart /* 2035f8829a4aSRandall Stewart * NOTE: We can't use the INIT_ACK's chk_length to determine the 2036f8829a4aSRandall Stewart * "initack_limit" value. This is because the chk_length field 2037f8829a4aSRandall Stewart * includes the length of the cookie, but the cookie is omitted when 2038f8829a4aSRandall Stewart * the INIT and INIT_ACK are tacked onto the cookie... 2039f8829a4aSRandall Stewart */ 2040f8829a4aSRandall Stewart initack_limit = offset + cookie_len; 2041f8829a4aSRandall Stewart 2042f8829a4aSRandall Stewart /* 2043f8829a4aSRandall Stewart * now that we know the INIT/INIT-ACK are in place, create a new TCB 2044f8829a4aSRandall Stewart * and popluate 2045f8829a4aSRandall Stewart */ 204652be287eSRandall Stewart 204752be287eSRandall Stewart /* 204852be287eSRandall Stewart * Here we do a trick, we set in NULL for the proc/thread argument. 204952be287eSRandall Stewart * We do this since in effect we only use the p argument when the 205052be287eSRandall Stewart * socket is unbound and we must do an implicit bind. Since we are 205152be287eSRandall Stewart * getting a cookie, we cannot be unbound. 205252be287eSRandall Stewart */ 2053b5c16493SMichael Tuexen stcb = sctp_aloc_assoc(inp, init_src, &error, 2054c7f048abSMichael Tuexen ntohl(initack_cp->init.initiate_tag), 2055c7f048abSMichael Tuexen ntohl(initack_cp->init.initial_tsn), vrf_id, 2056c979034bSMichael Tuexen ntohs(initack_cp->init.num_outbound_streams), 2057ec70917fSMichael Tuexen port, 20588a956abeSMichael Tuexen (struct thread *)NULL, 20598a956abeSMichael Tuexen SCTP_DONT_INITIALIZE_AUTH_PARAMS); 2060f8829a4aSRandall Stewart if (stcb == NULL) { 2061f8829a4aSRandall Stewart struct mbuf *op_err; 2062f8829a4aSRandall Stewart 2063f8829a4aSRandall Stewart /* memory problem? */ 2064ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, 2065ad81507eSRandall Stewart "process_cookie_new: no room for another TCB!\n"); 2066ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 2067f8829a4aSRandall Stewart sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2068b1754ad1SMichael Tuexen src, dst, sh, op_err, 2069457b4b88SMichael Tuexen mflowtype, mflowid, 2070f30ac432SMichael Tuexen vrf_id, port); 2071f8829a4aSRandall Stewart return (NULL); 2072f8829a4aSRandall Stewart } 2073f8829a4aSRandall Stewart asoc = &stcb->asoc; 2074f8829a4aSRandall Stewart /* get scope variables out of cookie */ 2075a1cb341bSMichael Tuexen asoc->scope.ipv4_local_scope = cookie->ipv4_scope; 2076a1cb341bSMichael Tuexen asoc->scope.site_scope = cookie->site_scope; 2077a1cb341bSMichael Tuexen asoc->scope.local_scope = cookie->local_scope; 2078a1cb341bSMichael Tuexen asoc->scope.loopback_scope = cookie->loopback_scope; 2079f8829a4aSRandall Stewart 2080a1cb341bSMichael Tuexen if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) || 2081a1cb341bSMichael Tuexen (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) { 2082f8829a4aSRandall Stewart struct mbuf *op_err; 2083f8829a4aSRandall Stewart 2084f8829a4aSRandall Stewart /* 2085f8829a4aSRandall Stewart * Houston we have a problem. The EP changed while the 2086f8829a4aSRandall Stewart * cookie was in flight. Only recourse is to abort the 2087f8829a4aSRandall Stewart * association. 2088f8829a4aSRandall Stewart */ 2089ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 2090f8829a4aSRandall Stewart sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, 2091b1754ad1SMichael Tuexen src, dst, sh, op_err, 2092457b4b88SMichael Tuexen mflowtype, mflowid, 2093f30ac432SMichael Tuexen vrf_id, port); 2094c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2095b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_18); 2096f8829a4aSRandall Stewart return (NULL); 2097f8829a4aSRandall Stewart } 2098f8829a4aSRandall Stewart /* process the INIT-ACK info (my info) */ 2099f8829a4aSRandall Stewart asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); 2100f8829a4aSRandall Stewart 2101f8829a4aSRandall Stewart /* process the INIT info (peer's info) */ 21025f2e1835SMichael Tuexen if (sctp_process_init(init_cp, stcb) < 0) { 2103b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2104b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_19); 2105f8829a4aSRandall Stewart return (NULL); 2106f8829a4aSRandall Stewart } 2107f8829a4aSRandall Stewart /* load all addresses */ 21085f2e1835SMichael Tuexen if ((retval = sctp_load_addresses_from_init(stcb, m, 21095f2e1835SMichael Tuexen init_offset + sizeof(struct sctp_init_chunk), 21105f2e1835SMichael Tuexen initack_offset, src, dst, init_src, port)) < 0) { 2111b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2112b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_20); 2113f8829a4aSRandall Stewart return (NULL); 2114f8829a4aSRandall Stewart } 2115f8829a4aSRandall Stewart /* 2116f8829a4aSRandall Stewart * verify any preceding AUTH chunk that was skipped 2117f8829a4aSRandall Stewart */ 2118f8829a4aSRandall Stewart /* pull the local authentication parameters from the cookie/init-ack */ 2119f8829a4aSRandall Stewart sctp_auth_get_cookie_params(stcb, m, 2120f8829a4aSRandall Stewart initack_offset + sizeof(struct sctp_init_ack_chunk), 2121f8829a4aSRandall Stewart initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); 2122f8829a4aSRandall Stewart if (auth_skipped) { 2123f8829a4aSRandall Stewart struct sctp_auth_chunk *auth; 2124f8829a4aSRandall Stewart 21258262311cSMichael Tuexen if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) { 212697feba89SMichael Tuexen auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); 212797feba89SMichael Tuexen } else { 212897feba89SMichael Tuexen auth = NULL; 212997feba89SMichael Tuexen } 2130ad81507eSRandall Stewart if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { 2131f8829a4aSRandall Stewart /* auth HMAC failed, dump the assoc and packet */ 2132ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_AUTH1, 2133ad81507eSRandall Stewart "COOKIE-ECHO: AUTH failed\n"); 2134b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2135b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_21); 2136f8829a4aSRandall Stewart return (NULL); 2137f8829a4aSRandall Stewart } else { 2138f8829a4aSRandall Stewart /* remaining chunks checked... good to go */ 2139f8829a4aSRandall Stewart stcb->asoc.authenticated = 1; 2140f8829a4aSRandall Stewart } 2141f8829a4aSRandall Stewart } 21420053ed28SMichael Tuexen 2143f8829a4aSRandall Stewart /* 2144f8829a4aSRandall Stewart * if we're doing ASCONFs, check to see if we have any new local 2145f8829a4aSRandall Stewart * addresses that need to get added to the peer (eg. addresses 2146f8829a4aSRandall Stewart * changed while cookie echo in flight). This needs to be done 2147f8829a4aSRandall Stewart * after we go to the OPEN state to do the correct asconf 2148f8829a4aSRandall Stewart * processing. else, make sure we have the correct addresses in our 2149f8829a4aSRandall Stewart * lists 2150f8829a4aSRandall Stewart */ 2151f8829a4aSRandall Stewart 2152f8829a4aSRandall Stewart /* warning, we re-use sin, sin6, sa_store here! */ 2153f8829a4aSRandall Stewart /* pull in local_address (our "from" address) */ 2154e6194c2eSMichael Tuexen switch (cookie->laddr_type) { 2155e6194c2eSMichael Tuexen #ifdef INET 2156e6194c2eSMichael Tuexen case SCTP_IPV4_ADDRESS: 2157f8829a4aSRandall Stewart /* source addr is IPv4 */ 215824aaac8dSMichael Tuexen memset(&store.sin, 0, sizeof(struct sockaddr_in)); 215924aaac8dSMichael Tuexen store.sin.sin_family = AF_INET; 216024aaac8dSMichael Tuexen store.sin.sin_len = sizeof(struct sockaddr_in); 216124aaac8dSMichael Tuexen store.sin.sin_addr.s_addr = cookie->laddress[0]; 2162e6194c2eSMichael Tuexen break; 2163e6194c2eSMichael Tuexen #endif 2164e6194c2eSMichael Tuexen #ifdef INET6 2165e6194c2eSMichael Tuexen case SCTP_IPV6_ADDRESS: 2166f8829a4aSRandall Stewart /* source addr is IPv6 */ 216724aaac8dSMichael Tuexen memset(&store.sin6, 0, sizeof(struct sockaddr_in6)); 216824aaac8dSMichael Tuexen store.sin6.sin6_family = AF_INET6; 216924aaac8dSMichael Tuexen store.sin6.sin6_len = sizeof(struct sockaddr_in6); 217024aaac8dSMichael Tuexen store.sin6.sin6_scope_id = cookie->scope_id; 217123602b60SMichael Tuexen memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr)); 2172e6194c2eSMichael Tuexen break; 2173e6194c2eSMichael Tuexen #endif 2174e6194c2eSMichael Tuexen default: 2175b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 2176b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_22); 2177f8829a4aSRandall Stewart return (NULL); 2178f8829a4aSRandall Stewart } 2179f8829a4aSRandall Stewart 21801698cbd9SMichael Tuexen /* update current state */ 21811698cbd9SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2182839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 21831698cbd9SMichael Tuexen if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 21841698cbd9SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 21856fb7b4fbSMichael Tuexen stcb->sctp_ep, stcb, NULL); 21861698cbd9SMichael Tuexen } 21871698cbd9SMichael Tuexen sctp_stop_all_cookie_timers(stcb); 21881698cbd9SMichael Tuexen SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); 21891698cbd9SMichael Tuexen SCTP_STAT_INCR_GAUGE32(sctps_currestab); 21901698cbd9SMichael Tuexen 2191f8829a4aSRandall Stewart /* set up to notify upper layer */ 2192f8829a4aSRandall Stewart *notification = SCTP_NOTIFY_ASSOC_UP; 2193f8829a4aSRandall Stewart if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2194f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 21955d08768aSMichael Tuexen (!SCTP_IS_LISTENING(inp))) { 2196f8829a4aSRandall Stewart /* 2197f8829a4aSRandall Stewart * This is an endpoint that called connect() how it got a 2198f8829a4aSRandall Stewart * cookie that is NEW is a bit of a mystery. It must be that 2199f8829a4aSRandall Stewart * the INIT was sent, but before it got there.. a complete 2200f8829a4aSRandall Stewart * INIT/INIT-ACK/COOKIE arrived. But of course then it 2201f8829a4aSRandall Stewart * should have went to the other code.. not here.. oh well.. 2202f8829a4aSRandall Stewart * a bit of protection is worth having.. 2203f8829a4aSRandall Stewart */ 2204f8829a4aSRandall Stewart stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2205ceaad40aSRandall Stewart soisconnected(stcb->sctp_socket); 2206f8829a4aSRandall Stewart } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 22075d08768aSMichael Tuexen (SCTP_IS_LISTENING(inp))) { 2208f8829a4aSRandall Stewart /* 2209f8829a4aSRandall Stewart * We don't want to do anything with this one. Since it is 2210f8829a4aSRandall Stewart * the listening guy. The timer will get started for 2211f8829a4aSRandall Stewart * accepted connections in the caller. 2212f8829a4aSRandall Stewart */ 2213f8829a4aSRandall Stewart ; 2214f8829a4aSRandall Stewart } 2215f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks && 2216f8829a4aSRandall Stewart sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2217f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 2218f8829a4aSRandall Stewart } 2219b54d3a6cSRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2220b15f5411SMichael Tuexen *netp = sctp_findnet(stcb, init_src); 2221b15f5411SMichael Tuexen if (*netp != NULL) { 22228c8e10b7SMichael Tuexen struct timeval old; 22238c8e10b7SMichael Tuexen 2224b15f5411SMichael Tuexen /* 2225b15f5411SMichael Tuexen * Since we did not send a HB, make sure we don't double 2226b15f5411SMichael Tuexen * things. 2227b15f5411SMichael Tuexen */ 2228b15f5411SMichael Tuexen (*netp)->hb_responded = 1; 2229b15f5411SMichael Tuexen /* Calculate the RTT. */ 22308c8e10b7SMichael Tuexen old.tv_sec = cookie->time_entered.tv_sec; 22318c8e10b7SMichael Tuexen old.tv_usec = cookie->time_entered.tv_usec; 223244f2a327SMichael Tuexen sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA); 22339a972525SRandall Stewart } 22343232788eSRandall Stewart /* respond with a COOKIE-ACK */ 2235f8829a4aSRandall Stewart sctp_send_cookie_ack(stcb); 22363232788eSRandall Stewart 22373232788eSRandall Stewart /* 22383232788eSRandall Stewart * check the address lists for any ASCONFs that need to be sent 22393232788eSRandall Stewart * AFTER the cookie-ack is sent 22403232788eSRandall Stewart */ 22413232788eSRandall Stewart sctp_check_address_list(stcb, m, 22423232788eSRandall Stewart initack_offset + sizeof(struct sctp_init_ack_chunk), 22433232788eSRandall Stewart initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), 224424aaac8dSMichael Tuexen &store.sa, cookie->local_scope, cookie->site_scope, 22453232788eSRandall Stewart cookie->ipv4_scope, cookie->loopback_scope); 22463232788eSRandall Stewart 2247f8829a4aSRandall Stewart return (stcb); 2248f8829a4aSRandall Stewart } 2249f8829a4aSRandall Stewart 2250830d754dSRandall Stewart /* 2251830d754dSRandall Stewart * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e 2252830d754dSRandall Stewart * we NEED to make sure we are not already using the vtag. If so we 2253830d754dSRandall Stewart * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! 2254830d754dSRandall Stewart head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, 2255830d754dSRandall Stewart SCTP_BASE_INFO(hashasocmark))]; 2256830d754dSRandall Stewart LIST_FOREACH(stcb, head, sctp_asocs) { 2257830d754dSRandall Stewart if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { 2258830d754dSRandall Stewart -- SEND ABORT - TRY AGAIN -- 2259830d754dSRandall Stewart } 2260830d754dSRandall Stewart } 2261830d754dSRandall Stewart */ 2262f8829a4aSRandall Stewart 2263f8829a4aSRandall Stewart /* 2264f8829a4aSRandall Stewart * handles a COOKIE-ECHO message stcb: modified to either a new or left as 2265f8829a4aSRandall Stewart * existing (non-NULL) TCB 2266f8829a4aSRandall Stewart */ 2267f8829a4aSRandall Stewart static struct mbuf * 2268f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, 2269b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 2270f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, 2271f8829a4aSRandall Stewart struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, 227217205eccSRandall Stewart int auth_skipped, uint32_t auth_offset, uint32_t auth_len, 2273f30ac432SMichael Tuexen struct sctp_tcb **locked_tcb, 2274457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 2275f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 2276f8829a4aSRandall Stewart { 2277f8829a4aSRandall Stewart struct sctp_state_cookie *cookie; 2278f8829a4aSRandall Stewart struct sctp_tcb *l_stcb = *stcb; 2279f8829a4aSRandall Stewart struct sctp_inpcb *l_inp; 2280f8829a4aSRandall Stewart struct sockaddr *to; 2281f8829a4aSRandall Stewart struct sctp_pcb *ep; 2282f8829a4aSRandall Stewart struct mbuf *m_sig; 2283f8829a4aSRandall Stewart uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; 2284f8829a4aSRandall Stewart uint8_t *sig; 2285f8829a4aSRandall Stewart uint8_t cookie_ok = 0; 2286329204ffSMichael Tuexen unsigned int sig_offset, cookie_offset; 2287f8829a4aSRandall Stewart unsigned int cookie_len; 2288f8829a4aSRandall Stewart struct timeval now; 2289a92d5016SMichael Tuexen struct timeval time_entered, time_expires; 2290f8829a4aSRandall Stewart int notification = 0; 2291f8829a4aSRandall Stewart struct sctp_nets *netl; 2292f8829a4aSRandall Stewart int had_a_existing_tcb = 0; 22932fad0e55SMichael Tuexen int send_int_conf = 0; 2294e6194c2eSMichael Tuexen #ifdef INET 2295e6194c2eSMichael Tuexen struct sockaddr_in sin; 2296e6194c2eSMichael Tuexen #endif 2297e6194c2eSMichael Tuexen #ifdef INET6 2298e6194c2eSMichael Tuexen struct sockaddr_in6 sin6; 2299e6194c2eSMichael Tuexen #endif 2300e6194c2eSMichael Tuexen 2301ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 2302ad81507eSRandall Stewart "sctp_handle_cookie: handling COOKIE-ECHO\n"); 2303f8829a4aSRandall Stewart 2304f8829a4aSRandall Stewart if (inp_p == NULL) { 2305f8829a4aSRandall Stewart return (NULL); 2306f8829a4aSRandall Stewart } 2307f8829a4aSRandall Stewart cookie = &cp->cookie; 2308f8829a4aSRandall Stewart cookie_offset = offset + sizeof(struct sctp_chunkhdr); 2309f8829a4aSRandall Stewart cookie_len = ntohs(cp->ch.chunk_length); 2310f8829a4aSRandall Stewart 2311d44b45dfSMichael Tuexen if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) + 2312d44b45dfSMichael Tuexen sizeof(struct sctp_init_chunk) + 2313d44b45dfSMichael Tuexen sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { 2314d44b45dfSMichael Tuexen /* cookie too small */ 2315d44b45dfSMichael Tuexen return (NULL); 2316d44b45dfSMichael Tuexen } 23173db4ea95SMichael Tuexen if ((cookie->peerport != sh->src_port) || 23183db4ea95SMichael Tuexen (cookie->myport != sh->dest_port) || 2319f8829a4aSRandall Stewart (cookie->my_vtag != sh->v_tag)) { 2320f8829a4aSRandall Stewart /* 2321f8829a4aSRandall Stewart * invalid ports or bad tag. Note that we always leave the 2322f8829a4aSRandall Stewart * v_tag in the header in network order and when we stored 2323f8829a4aSRandall Stewart * it in the my_vtag slot we also left it in network order. 232417205eccSRandall Stewart * This maintains the match even though it may be in the 2325f8829a4aSRandall Stewart * opposite byte order of the machine :-> 2326f8829a4aSRandall Stewart */ 2327f8829a4aSRandall Stewart return (NULL); 2328f8829a4aSRandall Stewart } 2329f8829a4aSRandall Stewart /* 2330f8829a4aSRandall Stewart * split off the signature into its own mbuf (since it should not be 2331f8829a4aSRandall Stewart * calculated in the sctp_hmac_m() call). 2332f8829a4aSRandall Stewart */ 2333f8829a4aSRandall Stewart sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; 2334eb1b1807SGleb Smirnoff m_sig = m_split(m, sig_offset, M_NOWAIT); 2335f8829a4aSRandall Stewart if (m_sig == NULL) { 2336f8829a4aSRandall Stewart /* out of memory or ?? */ 2337f8829a4aSRandall Stewart return (NULL); 2338f8829a4aSRandall Stewart } 2339eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING 2340b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 23414be807c4SMichael Tuexen sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT); 2342eadccaccSRandall Stewart } 2343eadccaccSRandall Stewart #endif 2344eadccaccSRandall Stewart 2345f8829a4aSRandall Stewart /* 2346f8829a4aSRandall Stewart * compute the signature/digest for the cookie 2347f8829a4aSRandall Stewart */ 2348f8829a4aSRandall Stewart ep = &(*inp_p)->sctp_ep; 2349f8829a4aSRandall Stewart l_inp = *inp_p; 2350f8829a4aSRandall Stewart if (l_stcb) { 2351f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(l_stcb); 2352f8829a4aSRandall Stewart } 2353f8829a4aSRandall Stewart SCTP_INP_RLOCK(l_inp); 2354f8829a4aSRandall Stewart if (l_stcb) { 2355f8829a4aSRandall Stewart SCTP_TCB_LOCK(l_stcb); 2356f8829a4aSRandall Stewart } 2357f8829a4aSRandall Stewart /* which cookie is it? */ 2358f8829a4aSRandall Stewart if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && 2359f8829a4aSRandall Stewart (ep->current_secret_number != ep->last_secret_number)) { 2360f8829a4aSRandall Stewart /* it's the old cookie */ 23616e55db54SRandall Stewart (void)sctp_hmac_m(SCTP_HMAC, 2362f8829a4aSRandall Stewart (uint8_t *)ep->secret_key[(int)ep->last_secret_number], 2363d00aff5dSRandall Stewart SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2364f8829a4aSRandall Stewart } else { 2365f8829a4aSRandall Stewart /* it's the current cookie */ 23666e55db54SRandall Stewart (void)sctp_hmac_m(SCTP_HMAC, 2367f8829a4aSRandall Stewart (uint8_t *)ep->secret_key[(int)ep->current_secret_number], 2368d00aff5dSRandall Stewart SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2369f8829a4aSRandall Stewart } 2370f8829a4aSRandall Stewart /* get the signature */ 2371f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(l_inp); 2372f8829a4aSRandall Stewart sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig); 2373f8829a4aSRandall Stewart if (sig == NULL) { 2374f8829a4aSRandall Stewart /* couldn't find signature */ 237503b0b021SRandall Stewart sctp_m_freem(m_sig); 2376f8829a4aSRandall Stewart return (NULL); 2377f8829a4aSRandall Stewart } 2378f8829a4aSRandall Stewart /* compare the received digest with the computed digest */ 237915a087e5SMichael Tuexen if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { 2380f8829a4aSRandall Stewart /* try the old cookie? */ 2381f8829a4aSRandall Stewart if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && 2382f8829a4aSRandall Stewart (ep->current_secret_number != ep->last_secret_number)) { 2383f8829a4aSRandall Stewart /* compute digest with old */ 23846e55db54SRandall Stewart (void)sctp_hmac_m(SCTP_HMAC, 2385f8829a4aSRandall Stewart (uint8_t *)ep->secret_key[(int)ep->last_secret_number], 2386d00aff5dSRandall Stewart SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); 2387f8829a4aSRandall Stewart /* compare */ 238815a087e5SMichael Tuexen if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) 2389f8829a4aSRandall Stewart cookie_ok = 1; 2390f8829a4aSRandall Stewart } 2391f8829a4aSRandall Stewart } else { 2392f8829a4aSRandall Stewart cookie_ok = 1; 2393f8829a4aSRandall Stewart } 2394f8829a4aSRandall Stewart 2395f8829a4aSRandall Stewart /* 2396f8829a4aSRandall Stewart * Now before we continue we must reconstruct our mbuf so that 2397f8829a4aSRandall Stewart * normal processing of any other chunks will work. 2398f8829a4aSRandall Stewart */ 2399f8829a4aSRandall Stewart { 2400f8829a4aSRandall Stewart struct mbuf *m_at; 2401f8829a4aSRandall Stewart 2402f8829a4aSRandall Stewart m_at = m; 2403139bc87fSRandall Stewart while (SCTP_BUF_NEXT(m_at) != NULL) { 2404139bc87fSRandall Stewart m_at = SCTP_BUF_NEXT(m_at); 2405f8829a4aSRandall Stewart } 2406139bc87fSRandall Stewart SCTP_BUF_NEXT(m_at) = m_sig; 2407f8829a4aSRandall Stewart } 2408f8829a4aSRandall Stewart 2409f8829a4aSRandall Stewart if (cookie_ok == 0) { 2410ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); 2411ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 2412ad81507eSRandall Stewart "offset = %u, cookie_offset = %u, sig_offset = %u\n", 2413f8829a4aSRandall Stewart (uint32_t)offset, cookie_offset, sig_offset); 2414f8829a4aSRandall Stewart return (NULL); 2415f8829a4aSRandall Stewart } 24160053ed28SMichael Tuexen 2417a92d5016SMichael Tuexen if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) { 2418a92d5016SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n"); 2419a92d5016SMichael Tuexen return (NULL); 2420a92d5016SMichael Tuexen } 2421a92d5016SMichael Tuexen time_entered.tv_sec = cookie->time_entered.tv_sec; 2422a92d5016SMichael Tuexen time_entered.tv_usec = cookie->time_entered.tv_usec; 2423a92d5016SMichael Tuexen if ((time_entered.tv_sec < 0) || 2424a92d5016SMichael Tuexen (time_entered.tv_usec < 0) || 2425a92d5016SMichael Tuexen (time_entered.tv_usec >= 1000000)) { 2426a92d5016SMichael Tuexen /* Invalid time stamp. Cookie must have been modified. */ 2427a92d5016SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n"); 2428a92d5016SMichael Tuexen return (NULL); 2429a92d5016SMichael Tuexen } 24306e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 2431a92d5016SMichael Tuexen if (timevalcmp(&now, &time_entered, <)) { 2432a92d5016SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n"); 2433a92d5016SMichael Tuexen return (NULL); 2434a92d5016SMichael Tuexen } 2435a92d5016SMichael Tuexen /* 2436a92d5016SMichael Tuexen * Check the cookie timestamps to be sure it's not stale. 2437a92d5016SMichael Tuexen * cookie_life is in ticks, so we convert to seconds. 2438a92d5016SMichael Tuexen */ 2439a92d5016SMichael Tuexen time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life); 2440a92d5016SMichael Tuexen time_expires.tv_usec = time_entered.tv_usec; 2441f8829a4aSRandall Stewart if (timevalcmp(&now, &time_expires, >)) { 2442f8829a4aSRandall Stewart /* cookie is stale! */ 2443f8829a4aSRandall Stewart struct mbuf *op_err; 244486eda749SMichael Tuexen struct sctp_error_stale_cookie *cause; 2445564a95f4SMichael Tuexen struct timeval diff; 2446564a95f4SMichael Tuexen uint32_t staleness; 2447f8829a4aSRandall Stewart 244886eda749SMichael Tuexen op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie), 2449eb1b1807SGleb Smirnoff 0, M_NOWAIT, 1, MT_DATA); 2450f8829a4aSRandall Stewart if (op_err == NULL) { 2451f8829a4aSRandall Stewart /* FOOBAR */ 2452f8829a4aSRandall Stewart return (NULL); 2453f8829a4aSRandall Stewart } 2454f8829a4aSRandall Stewart /* Set the len */ 245586eda749SMichael Tuexen SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie); 245686eda749SMichael Tuexen cause = mtod(op_err, struct sctp_error_stale_cookie *); 245786eda749SMichael Tuexen cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE); 2458a92d5016SMichael Tuexen cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie)); 2459564a95f4SMichael Tuexen diff = now; 2460564a95f4SMichael Tuexen timevalsub(&diff, &time_expires); 2461c3115febSMichael Tuexen if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) { 2462564a95f4SMichael Tuexen staleness = UINT32_MAX; 2463564a95f4SMichael Tuexen } else { 2464564a95f4SMichael Tuexen staleness = diff.tv_sec * 1000000; 2465564a95f4SMichael Tuexen } 24663ec509bcSMichael Tuexen if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) { 2467564a95f4SMichael Tuexen staleness += diff.tv_usec; 2468564a95f4SMichael Tuexen } else { 2469564a95f4SMichael Tuexen staleness = UINT32_MAX; 2470564a95f4SMichael Tuexen } 2471564a95f4SMichael Tuexen cause->stale_time = htonl(staleness); 2472b1754ad1SMichael Tuexen sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, 2473d089f9b9SMichael Tuexen mflowtype, mflowid, l_inp->fibnum, 2474c54a18d2SRandall Stewart vrf_id, port); 2475f8829a4aSRandall Stewart return (NULL); 2476f8829a4aSRandall Stewart } 2477f8829a4aSRandall Stewart /* 2478f8829a4aSRandall Stewart * Now we must see with the lookup address if we have an existing 2479f8829a4aSRandall Stewart * asoc. This will only happen if we were in the COOKIE-WAIT state 2480f8829a4aSRandall Stewart * and a INIT collided with us and somewhere the peer sent the 2481f8829a4aSRandall Stewart * cookie on another address besides the single address our assoc 2482f8829a4aSRandall Stewart * had for him. In this case we will have one of the tie-tags set at 2483f8829a4aSRandall Stewart * least AND the address field in the cookie can be used to look it 2484f8829a4aSRandall Stewart * up. 2485f8829a4aSRandall Stewart */ 2486f8829a4aSRandall Stewart to = NULL; 2487e6194c2eSMichael Tuexen switch (cookie->addr_type) { 2488e6194c2eSMichael Tuexen #ifdef INET6 2489e6194c2eSMichael Tuexen case SCTP_IPV6_ADDRESS: 2490f8829a4aSRandall Stewart memset(&sin6, 0, sizeof(sin6)); 2491f8829a4aSRandall Stewart sin6.sin6_family = AF_INET6; 2492f8829a4aSRandall Stewart sin6.sin6_len = sizeof(sin6); 2493f8829a4aSRandall Stewart sin6.sin6_port = sh->src_port; 2494f8829a4aSRandall Stewart sin6.sin6_scope_id = cookie->scope_id; 2495f8829a4aSRandall Stewart memcpy(&sin6.sin6_addr.s6_addr, cookie->address, 2496f8829a4aSRandall Stewart sizeof(sin6.sin6_addr.s6_addr)); 2497f8829a4aSRandall Stewart to = (struct sockaddr *)&sin6; 2498e6194c2eSMichael Tuexen break; 2499e6194c2eSMichael Tuexen #endif 2500e6194c2eSMichael Tuexen #ifdef INET 2501e6194c2eSMichael Tuexen case SCTP_IPV4_ADDRESS: 2502f8829a4aSRandall Stewart memset(&sin, 0, sizeof(sin)); 2503f8829a4aSRandall Stewart sin.sin_family = AF_INET; 2504f8829a4aSRandall Stewart sin.sin_len = sizeof(sin); 2505f8829a4aSRandall Stewart sin.sin_port = sh->src_port; 2506f8829a4aSRandall Stewart sin.sin_addr.s_addr = cookie->address[0]; 2507f8829a4aSRandall Stewart to = (struct sockaddr *)&sin; 2508e6194c2eSMichael Tuexen break; 2509e6194c2eSMichael Tuexen #endif 2510e6194c2eSMichael Tuexen default: 2511ad81507eSRandall Stewart /* This should not happen */ 2512ad81507eSRandall Stewart return (NULL); 2513f8829a4aSRandall Stewart } 2514f0dc2113SMichael Tuexen if (*stcb == NULL) { 2515f8829a4aSRandall Stewart /* Yep, lets check */ 2516b1754ad1SMichael Tuexen *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL); 2517f8829a4aSRandall Stewart if (*stcb == NULL) { 2518f8829a4aSRandall Stewart /* 2519f8829a4aSRandall Stewart * We should have only got back the same inp. If we 2520f8829a4aSRandall Stewart * got back a different ep we have a problem. The 2521f8829a4aSRandall Stewart * original findep got back l_inp and now 2522f8829a4aSRandall Stewart */ 2523f8829a4aSRandall Stewart if (l_inp != *inp_p) { 2524ad81507eSRandall Stewart SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); 2525f8829a4aSRandall Stewart } 2526a5d547adSRandall Stewart } else { 2527a5d547adSRandall Stewart if (*locked_tcb == NULL) { 2528a5d547adSRandall Stewart /* 2529a5d547adSRandall Stewart * In this case we found the assoc only 2530a5d547adSRandall Stewart * after we locked the create lock. This 2531a5d547adSRandall Stewart * means we are in a colliding case and we 2532a5d547adSRandall Stewart * must make sure that we unlock the tcb if 2533a5d547adSRandall Stewart * its one of the cases where we throw away 2534a5d547adSRandall Stewart * the incoming packets. 2535a5d547adSRandall Stewart */ 2536a5d547adSRandall Stewart *locked_tcb = *stcb; 2537a5d547adSRandall Stewart 2538a5d547adSRandall Stewart /* 2539a5d547adSRandall Stewart * We must also increment the inp ref count 2540a5d547adSRandall Stewart * since the ref_count flags was set when we 2541a5d547adSRandall Stewart * did not find the TCB, now we found it 2542a5d547adSRandall Stewart * which reduces the refcount.. we must 2543a5d547adSRandall Stewart * raise it back out to balance it all :-) 2544a5d547adSRandall Stewart */ 2545a5d547adSRandall Stewart SCTP_INP_INCR_REF((*stcb)->sctp_ep); 2546a5d547adSRandall Stewart if ((*stcb)->sctp_ep != l_inp) { 2547ad81507eSRandall Stewart SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", 2548dd294dceSMichael Tuexen (void *)(*stcb)->sctp_ep, (void *)l_inp); 2549a5d547adSRandall Stewart } 2550a5d547adSRandall Stewart } 2551f8829a4aSRandall Stewart } 2552f8829a4aSRandall Stewart } 25530053ed28SMichael Tuexen 2554f8829a4aSRandall Stewart cookie_len -= SCTP_SIGNATURE_SIZE; 2555f8829a4aSRandall Stewart if (*stcb == NULL) { 2556f8829a4aSRandall Stewart /* this is the "normal" case... get a new TCB */ 2557b1754ad1SMichael Tuexen *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh, 2558b1754ad1SMichael Tuexen cookie, cookie_len, *inp_p, 2559b1754ad1SMichael Tuexen netp, to, ¬ification, 2560f30ac432SMichael Tuexen auth_skipped, auth_offset, auth_len, 2561457b4b88SMichael Tuexen mflowtype, mflowid, 2562f30ac432SMichael Tuexen vrf_id, port); 2563f8829a4aSRandall Stewart } else { 2564f8829a4aSRandall Stewart /* this is abnormal... cookie-echo on existing TCB */ 2565f8829a4aSRandall Stewart had_a_existing_tcb = 1; 2566b1754ad1SMichael Tuexen *stcb = sctp_process_cookie_existing(m, iphlen, offset, 2567b1754ad1SMichael Tuexen src, dst, sh, 2568830d754dSRandall Stewart cookie, cookie_len, *inp_p, *stcb, netp, to, 2569f30ac432SMichael Tuexen ¬ification, auth_skipped, auth_offset, auth_len, 2570457b4b88SMichael Tuexen mflowtype, mflowid, 2571f30ac432SMichael Tuexen vrf_id, port); 25725f2e1835SMichael Tuexen if (*stcb == NULL) { 25735f2e1835SMichael Tuexen *locked_tcb = NULL; 25745f2e1835SMichael Tuexen } 2575f8829a4aSRandall Stewart } 2576f8829a4aSRandall Stewart 2577f8829a4aSRandall Stewart if (*stcb == NULL) { 2578f8829a4aSRandall Stewart /* still no TCB... must be bad cookie-echo */ 2579f8829a4aSRandall Stewart return (NULL); 2580f8829a4aSRandall Stewart } 25815fe29cdfSMichael Tuexen if (*netp != NULL) { 2582457b4b88SMichael Tuexen (*netp)->flowtype = mflowtype; 25835fe29cdfSMichael Tuexen (*netp)->flowid = mflowid; 2584a4ae38f1SMichael Tuexen } 2585f8829a4aSRandall Stewart /* 2586f8829a4aSRandall Stewart * Ok, we built an association so confirm the address we sent the 2587f8829a4aSRandall Stewart * INIT-ACK to. 2588f8829a4aSRandall Stewart */ 2589f8829a4aSRandall Stewart netl = sctp_findnet(*stcb, to); 2590f8829a4aSRandall Stewart /* 2591f8829a4aSRandall Stewart * This code should in theory NOT run but 2592f8829a4aSRandall Stewart */ 2593f8829a4aSRandall Stewart if (netl == NULL) { 2594f8829a4aSRandall Stewart /* TSNH! Huh, why do I need to add this address here? */ 2595ec70917fSMichael Tuexen if (sctp_add_remote_addr(*stcb, to, NULL, port, 25967154bf4aSMichael Tuexen SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { 259760990c0cSMichael Tuexen return (NULL); 259860990c0cSMichael Tuexen } 2599f8829a4aSRandall Stewart netl = sctp_findnet(*stcb, to); 2600f8829a4aSRandall Stewart } 2601f8829a4aSRandall Stewart if (netl) { 2602f8829a4aSRandall Stewart if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { 2603f8829a4aSRandall Stewart netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 2604ad81507eSRandall Stewart (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, 2605f8829a4aSRandall Stewart netl); 26062fad0e55SMichael Tuexen send_int_conf = 1; 2607f8829a4aSRandall Stewart } 2608f8829a4aSRandall Stewart } 2609ca85e948SMichael Tuexen sctp_start_net_timers(*stcb); 2610f8829a4aSRandall Stewart if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 2611f8829a4aSRandall Stewart if (!had_a_existing_tcb || 2612f8829a4aSRandall Stewart (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 2613f8829a4aSRandall Stewart /* 2614f8829a4aSRandall Stewart * If we have a NEW cookie or the connect never 2615f8829a4aSRandall Stewart * reached the connected state during collision we 2616f8829a4aSRandall Stewart * must do the TCP accept thing. 2617f8829a4aSRandall Stewart */ 2618f8829a4aSRandall Stewart struct socket *so, *oso; 2619f8829a4aSRandall Stewart struct sctp_inpcb *inp; 2620f8829a4aSRandall Stewart 2621f8829a4aSRandall Stewart if (notification == SCTP_NOTIFY_ASSOC_RESTART) { 2622f8829a4aSRandall Stewart /* 2623f8829a4aSRandall Stewart * For a restart we will keep the same 2624f8829a4aSRandall Stewart * socket, no need to do anything. I THINK!! 2625f8829a4aSRandall Stewart */ 26267215cc1bSMichael Tuexen sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 26272fad0e55SMichael Tuexen if (send_int_conf) { 26282fad0e55SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 26292fad0e55SMichael Tuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 26302fad0e55SMichael Tuexen } 2631f8829a4aSRandall Stewart return (m); 2632f8829a4aSRandall Stewart } 2633f8829a4aSRandall Stewart oso = (*inp_p)->sctp_socket; 26342dad8a55SRandall Stewart atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2635f8829a4aSRandall Stewart SCTP_TCB_UNLOCK((*stcb)); 26361fb51a12SBjoern A. Zeeb CURVNET_SET(oso->so_vnet); 263793164cf9SRandall Stewart so = sonewconn(oso, 0 2638f8829a4aSRandall Stewart ); 26391fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2640f8829a4aSRandall Stewart SCTP_TCB_LOCK((*stcb)); 26412dad8a55SRandall Stewart atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 26422dad8a55SRandall Stewart 2643f8829a4aSRandall Stewart if (so == NULL) { 2644f8829a4aSRandall Stewart struct mbuf *op_err; 264570486b27SMichael Tuexen 2646f8829a4aSRandall Stewart /* Too many sockets */ 2647ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); 2648ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 2649f8829a4aSRandall Stewart sctp_abort_association(*inp_p, NULL, m, iphlen, 2650b1754ad1SMichael Tuexen src, dst, sh, op_err, 2651457b4b88SMichael Tuexen mflowtype, mflowid, 2652f30ac432SMichael Tuexen vrf_id, port); 2653b7d130beSMichael Tuexen (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, 2654b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_23); 2655f8829a4aSRandall Stewart return (NULL); 2656f8829a4aSRandall Stewart } 2657f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb; 265893164cf9SRandall Stewart SCTP_INP_INCR_REF(inp); 265993164cf9SRandall Stewart /* 266093164cf9SRandall Stewart * We add the unbound flag here so that if we get an 266193164cf9SRandall Stewart * soabort() before we get the move_pcb done, we 266293164cf9SRandall Stewart * will properly cleanup. 266393164cf9SRandall Stewart */ 2664f8829a4aSRandall Stewart inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2665f8829a4aSRandall Stewart SCTP_PCB_FLAGS_CONNECTED | 2666f8829a4aSRandall Stewart SCTP_PCB_FLAGS_IN_TCPPOOL | 266793164cf9SRandall Stewart SCTP_PCB_FLAGS_UNBOUND | 2668f8829a4aSRandall Stewart (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | 2669f8829a4aSRandall Stewart SCTP_PCB_FLAGS_DONT_WAKE); 2670f8829a4aSRandall Stewart inp->sctp_features = (*inp_p)->sctp_features; 2671851b7298SRandall Stewart inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; 2672f8829a4aSRandall Stewart inp->sctp_socket = so; 2673f8829a4aSRandall Stewart inp->sctp_frag_point = (*inp_p)->sctp_frag_point; 267459b6d5beSMichael Tuexen inp->max_cwnd = (*inp_p)->max_cwnd; 267520083c2eSMichael Tuexen inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; 2676f342355aSMichael Tuexen inp->ecn_supported = (*inp_p)->ecn_supported; 2677dd973b0eSMichael Tuexen inp->prsctp_supported = (*inp_p)->prsctp_supported; 2678c79bec9cSMichael Tuexen inp->auth_supported = (*inp_p)->auth_supported; 2679c79bec9cSMichael Tuexen inp->asconf_supported = (*inp_p)->asconf_supported; 2680317e00efSMichael Tuexen inp->reconfig_supported = (*inp_p)->reconfig_supported; 2681caea9879SMichael Tuexen inp->nrsack_supported = (*inp_p)->nrsack_supported; 2682cb9b8e6fSMichael Tuexen inp->pktdrop_supported = (*inp_p)->pktdrop_supported; 2683f8829a4aSRandall Stewart inp->partial_delivery_point = (*inp_p)->partial_delivery_point; 2684f8829a4aSRandall Stewart inp->sctp_context = (*inp_p)->sctp_context; 2685c4e848b7SRandall Stewart inp->local_strreset_support = (*inp_p)->local_strreset_support; 2686d089f9b9SMichael Tuexen inp->fibnum = (*inp_p)->fibnum; 2687f8829a4aSRandall Stewart inp->inp_starting_point_for_iterator = NULL; 2688f8829a4aSRandall Stewart /* 2689f8829a4aSRandall Stewart * copy in the authentication parameters from the 2690f8829a4aSRandall Stewart * original endpoint 2691f8829a4aSRandall Stewart */ 2692f8829a4aSRandall Stewart if (inp->sctp_ep.local_hmacs) 2693f8829a4aSRandall Stewart sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2694f8829a4aSRandall Stewart inp->sctp_ep.local_hmacs = 2695f8829a4aSRandall Stewart sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); 2696f8829a4aSRandall Stewart if (inp->sctp_ep.local_auth_chunks) 2697f8829a4aSRandall Stewart sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2698f8829a4aSRandall Stewart inp->sctp_ep.local_auth_chunks = 2699f8829a4aSRandall Stewart sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); 2700f8829a4aSRandall Stewart 2701f8829a4aSRandall Stewart /* 2702f8829a4aSRandall Stewart * Now we must move it from one hash table to 2703f8829a4aSRandall Stewart * another and get the tcb in the right place. 2704f8829a4aSRandall Stewart */ 270588a7eb29SRandall Stewart 270688a7eb29SRandall Stewart /* 270788a7eb29SRandall Stewart * This is where the one-2-one socket is put into 270888a7eb29SRandall Stewart * the accept state waiting for the accept! 270988a7eb29SRandall Stewart */ 271088a7eb29SRandall Stewart if (*stcb) { 2711839d21d6SMichael Tuexen SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 271288a7eb29SRandall Stewart } 2713f8829a4aSRandall Stewart sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); 2714d61a0ae0SRandall Stewart 2715d61a0ae0SRandall Stewart atomic_add_int(&(*stcb)->asoc.refcnt, 1); 2716d61a0ae0SRandall Stewart SCTP_TCB_UNLOCK((*stcb)); 2717d61a0ae0SRandall Stewart 2718265de5bbSRobert Watson sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, 2719265de5bbSRobert Watson 0); 2720d61a0ae0SRandall Stewart SCTP_TCB_LOCK((*stcb)); 2721d61a0ae0SRandall Stewart atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); 2722d61a0ae0SRandall Stewart 272393164cf9SRandall Stewart /* 272493164cf9SRandall Stewart * now we must check to see if we were aborted while 272593164cf9SRandall Stewart * the move was going on and the lock/unlock 272693164cf9SRandall Stewart * happened. 272793164cf9SRandall Stewart */ 272893164cf9SRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 272993164cf9SRandall Stewart /* 273093164cf9SRandall Stewart * yep it was, we leave the assoc attached 273193164cf9SRandall Stewart * to the socket since the sctp_inpcb_free() 273293164cf9SRandall Stewart * call will send an abort for us. 273393164cf9SRandall Stewart */ 273493164cf9SRandall Stewart SCTP_INP_DECR_REF(inp); 273593164cf9SRandall Stewart return (NULL); 273693164cf9SRandall Stewart } 273793164cf9SRandall Stewart SCTP_INP_DECR_REF(inp); 2738f8829a4aSRandall Stewart /* Switch over to the new guy */ 2739f8829a4aSRandall Stewart *inp_p = inp; 2740ceaad40aSRandall Stewart sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 27412fad0e55SMichael Tuexen if (send_int_conf) { 27422fad0e55SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 27432fad0e55SMichael Tuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 27442fad0e55SMichael Tuexen } 27450053ed28SMichael Tuexen 2746b7b84c0eSMichael Tuexen /* 2747b7b84c0eSMichael Tuexen * Pull it from the incomplete queue and wake the 2748b7b84c0eSMichael Tuexen * guy 2749b7b84c0eSMichael Tuexen */ 275093164cf9SRandall Stewart soisconnected(so); 2751f8829a4aSRandall Stewart return (m); 2752f8829a4aSRandall Stewart } 2753f8829a4aSRandall Stewart } 27542fad0e55SMichael Tuexen if (notification) { 2755ceaad40aSRandall Stewart sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2756f8829a4aSRandall Stewart } 27572fad0e55SMichael Tuexen if (send_int_conf) { 27582fad0e55SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, 27592fad0e55SMichael Tuexen (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); 27602fad0e55SMichael Tuexen } 2761f8829a4aSRandall Stewart return (m); 2762f8829a4aSRandall Stewart } 2763f8829a4aSRandall Stewart 2764f8829a4aSRandall Stewart static void 27657215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, 2766f8829a4aSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net) 2767f8829a4aSRandall Stewart { 2768f8829a4aSRandall Stewart /* cp must not be used, others call this without a c-ack :-) */ 2769f8829a4aSRandall Stewart struct sctp_association *asoc; 2770efd5e692SMichael Tuexen struct sctp_tmit_chunk *chk; 2771f8829a4aSRandall Stewart 2772ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 2773ad81507eSRandall Stewart "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); 2774ad234e3cSMichael Tuexen if ((stcb == NULL) || (net == NULL)) { 2775f8829a4aSRandall Stewart return; 2776ad234e3cSMichael Tuexen } 27770053ed28SMichael Tuexen 2778f8829a4aSRandall Stewart asoc = &stcb->asoc; 2779469a65d1SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 2780469a65d1SMichael Tuexen sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 2781469a65d1SMichael Tuexen asoc->overall_error_count, 2782469a65d1SMichael Tuexen 0, 2783469a65d1SMichael Tuexen SCTP_FROM_SCTP_INPUT, 2784469a65d1SMichael Tuexen __LINE__); 2785469a65d1SMichael Tuexen } 2786469a65d1SMichael Tuexen asoc->overall_error_count = 0; 2787f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(stcb); 2788f8829a4aSRandall Stewart /* process according to association state */ 2789839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) { 2790f8829a4aSRandall Stewart /* state change only needed when I am in right state */ 2791ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); 2792839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_OPEN); 2793ca85e948SMichael Tuexen sctp_start_net_timers(stcb); 2794f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { 2795f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 27966fb7b4fbSMichael Tuexen stcb->sctp_ep, stcb, NULL); 2797f8829a4aSRandall Stewart } 2798f8829a4aSRandall Stewart /* update RTO */ 2799f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_activeestab); 2800f8829a4aSRandall Stewart SCTP_STAT_INCR_GAUGE32(sctps_currestab); 2801f8829a4aSRandall Stewart if (asoc->overall_error_count == 0) { 280244f2a327SMichael Tuexen sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, 2803f79aab18SRandall Stewart SCTP_RTT_FROM_NON_DATA); 2804f8829a4aSRandall Stewart } 28056e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 2806ceaad40aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 2807f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2808f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2809f8829a4aSRandall Stewart stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 2810d69e7322SRandall Stewart if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { 2811ceaad40aSRandall Stewart soisconnected(stcb->sctp_socket); 2812d69e7322SRandall Stewart } 2813f8829a4aSRandall Stewart } 2814f8829a4aSRandall Stewart /* 2815f8829a4aSRandall Stewart * since we did not send a HB make sure we don't double 2816f8829a4aSRandall Stewart * things 2817f8829a4aSRandall Stewart */ 2818f8829a4aSRandall Stewart net->hb_responded = 1; 2819f8829a4aSRandall Stewart 2820d69e7322SRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2821d69e7322SRandall Stewart /* 2822d69e7322SRandall Stewart * We don't need to do the asconf thing, nor hb or 2823d69e7322SRandall Stewart * autoclose if the socket is closed. 2824d69e7322SRandall Stewart */ 2825d69e7322SRandall Stewart goto closed_socket; 2826d69e7322SRandall Stewart } 28270053ed28SMichael Tuexen 2828d69e7322SRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, 2829d69e7322SRandall Stewart stcb, net); 2830d69e7322SRandall Stewart 2831f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks && 2832f8829a4aSRandall Stewart sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { 2833f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, 2834f8829a4aSRandall Stewart stcb->sctp_ep, stcb, NULL); 2835f8829a4aSRandall Stewart } 2836f8829a4aSRandall Stewart /* 28371b649582SRandall Stewart * send ASCONF if parameters are pending and ASCONFs are 28381b649582SRandall Stewart * allowed (eg. addresses changed when init/cookie echo were 28391b649582SRandall Stewart * in flight) 2840f8829a4aSRandall Stewart */ 2841f8829a4aSRandall Stewart if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && 2842c79bec9cSMichael Tuexen (stcb->asoc.asconf_supported == 1) && 2843f8829a4aSRandall Stewart (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { 28441b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF 2845f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, 2846f8829a4aSRandall Stewart stcb->sctp_ep, stcb, 2847f8829a4aSRandall Stewart stcb->asoc.primary_destination); 28481b649582SRandall Stewart #else 28493232788eSRandall Stewart sctp_send_asconf(stcb, stcb->asoc.primary_destination, 28503232788eSRandall Stewart SCTP_ADDR_NOT_LOCKED); 28511b649582SRandall Stewart #endif 2852f8829a4aSRandall Stewart } 2853f8829a4aSRandall Stewart } 2854d69e7322SRandall Stewart closed_socket: 2855f8829a4aSRandall Stewart /* Toss the cookie if I can */ 2856f8829a4aSRandall Stewart sctp_toss_old_cookies(stcb, asoc); 2857f8829a4aSRandall Stewart /* Restart the timer if we have pending data */ 2858efd5e692SMichael Tuexen TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 2859efd5e692SMichael Tuexen if (chk->whoTo != NULL) { 2860efd5e692SMichael Tuexen break; 2861efd5e692SMichael Tuexen } 2862efd5e692SMichael Tuexen } 2863efd5e692SMichael Tuexen if (chk != NULL) { 28644a9ef3f8SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 2865f8829a4aSRandall Stewart } 2866f8829a4aSRandall Stewart } 2867f8829a4aSRandall Stewart 2868f8829a4aSRandall Stewart static void 2869f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, 2870f8829a4aSRandall Stewart struct sctp_tcb *stcb) 2871f8829a4aSRandall Stewart { 2872f8829a4aSRandall Stewart struct sctp_nets *net; 2873f8829a4aSRandall Stewart struct sctp_tmit_chunk *lchk; 2874a21779f0SRandall Stewart struct sctp_ecne_chunk bkup; 287560990c0cSMichael Tuexen uint8_t override_bit; 2876a21779f0SRandall Stewart uint32_t tsn, window_data_tsn; 2877a21779f0SRandall Stewart int len; 2878dec0177dSRandall Stewart unsigned int pkt_cnt; 2879f8829a4aSRandall Stewart 2880a21779f0SRandall Stewart len = ntohs(cp->ch.chunk_length); 2881a21779f0SRandall Stewart if (len == sizeof(struct old_sctp_ecne_chunk)) { 2882a21779f0SRandall Stewart /* Its the old format */ 2883a21779f0SRandall Stewart memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); 2884a21779f0SRandall Stewart bkup.num_pkts_since_cwr = htonl(1); 2885a21779f0SRandall Stewart cp = &bkup; 2886a21779f0SRandall Stewart } 2887f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvecne); 2888f8829a4aSRandall Stewart tsn = ntohl(cp->tsn); 2889a21779f0SRandall Stewart pkt_cnt = ntohl(cp->num_pkts_since_cwr); 2890a21779f0SRandall Stewart lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); 2891f8829a4aSRandall Stewart if (lchk == NULL) { 2892493d8e5aSRandall Stewart window_data_tsn = stcb->asoc.sending_seq - 1; 2893f8829a4aSRandall Stewart } else { 289449656eefSMichael Tuexen window_data_tsn = lchk->rec.data.tsn; 2895f8829a4aSRandall Stewart } 2896f8829a4aSRandall Stewart 2897a21779f0SRandall Stewart /* Find where it was sent to if possible. */ 2898f8829a4aSRandall Stewart net = NULL; 28994a9ef3f8SMichael Tuexen TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { 290049656eefSMichael Tuexen if (lchk->rec.data.tsn == tsn) { 2901f8829a4aSRandall Stewart net = lchk->whoTo; 2902899288aeSRandall Stewart net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; 2903f8829a4aSRandall Stewart break; 2904f8829a4aSRandall Stewart } 290549656eefSMichael Tuexen if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) { 2906f8829a4aSRandall Stewart break; 2907f8829a4aSRandall Stewart } 290820b07a4dSMichael Tuexen } 2909a21779f0SRandall Stewart if (net == NULL) { 2910a21779f0SRandall Stewart /* 2911a21779f0SRandall Stewart * What to do. A previous send of a CWR was possibly lost. 2912a21779f0SRandall Stewart * See how old it is, we may have it marked on the actual 2913a21779f0SRandall Stewart * net. 2914a21779f0SRandall Stewart */ 2915a21779f0SRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2916a21779f0SRandall Stewart if (tsn == net->last_cwr_tsn) { 2917a21779f0SRandall Stewart /* Found him, send it off */ 291860990c0cSMichael Tuexen break; 2919a21779f0SRandall Stewart } 2920a21779f0SRandall Stewart } 292160990c0cSMichael Tuexen if (net == NULL) { 2922a21779f0SRandall Stewart /* 292360990c0cSMichael Tuexen * If we reach here, we need to send a special CWR 292460990c0cSMichael Tuexen * that says hey, we did this a long time ago and 292560990c0cSMichael Tuexen * you lost the response. 2926a21779f0SRandall Stewart */ 2927a21779f0SRandall Stewart net = TAILQ_FIRST(&stcb->asoc.nets); 292860990c0cSMichael Tuexen if (net == NULL) { 292960990c0cSMichael Tuexen /* TSNH */ 293060990c0cSMichael Tuexen return; 2931a21779f0SRandall Stewart } 293260990c0cSMichael Tuexen override_bit = SCTP_CWR_REDUCE_OVERRIDE; 293360990c0cSMichael Tuexen } else { 293460990c0cSMichael Tuexen override_bit = 0; 293560990c0cSMichael Tuexen } 293660990c0cSMichael Tuexen } else { 293760990c0cSMichael Tuexen override_bit = 0; 293860990c0cSMichael Tuexen } 2939493d8e5aSRandall Stewart if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && 2940493d8e5aSRandall Stewart ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 2941b7b84c0eSMichael Tuexen /* 2942b7b84c0eSMichael Tuexen * JRS - Use the congestion control given in the pluggable 2943b7b84c0eSMichael Tuexen * CC module 2944b7b84c0eSMichael Tuexen */ 2945493d8e5aSRandall Stewart stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); 2946f8829a4aSRandall Stewart /* 2947a21779f0SRandall Stewart * We reduce once every RTT. So we will only lower cwnd at 2948a21779f0SRandall Stewart * the next sending seq i.e. the window_data_tsn 2949f8829a4aSRandall Stewart */ 2950a21779f0SRandall Stewart net->cwr_window_tsn = window_data_tsn; 2951a21779f0SRandall Stewart net->ecn_ce_pkt_cnt += pkt_cnt; 2952a21779f0SRandall Stewart net->lost_cnt = pkt_cnt; 2953a21779f0SRandall Stewart net->last_cwr_tsn = tsn; 2954a21779f0SRandall Stewart } else { 2955a21779f0SRandall Stewart override_bit |= SCTP_CWR_IN_SAME_WINDOW; 2956493d8e5aSRandall Stewart if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && 2957493d8e5aSRandall Stewart ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) { 2958a21779f0SRandall Stewart /* 2959493d8e5aSRandall Stewart * Another loss in the same window update how many 2960493d8e5aSRandall Stewart * marks/packets lost we have had. 2961a21779f0SRandall Stewart */ 2962493d8e5aSRandall Stewart int cnt = 1; 2963a21779f0SRandall Stewart 2964a21779f0SRandall Stewart if (pkt_cnt > net->lost_cnt) { 2965a21779f0SRandall Stewart /* Should be the case */ 2966493d8e5aSRandall Stewart cnt = (pkt_cnt - net->lost_cnt); 2967493d8e5aSRandall Stewart net->ecn_ce_pkt_cnt += cnt; 2968a21779f0SRandall Stewart } 2969493d8e5aSRandall Stewart net->lost_cnt = pkt_cnt; 2970a21779f0SRandall Stewart net->last_cwr_tsn = tsn; 2971493d8e5aSRandall Stewart /* 2972493d8e5aSRandall Stewart * Most CC functions will ignore this call, since we 2973493d8e5aSRandall Stewart * are in-window yet of the initial CE the peer saw. 2974493d8e5aSRandall Stewart */ 2975493d8e5aSRandall Stewart stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); 2976a21779f0SRandall Stewart } 2977f8829a4aSRandall Stewart } 2978f8829a4aSRandall Stewart /* 2979f8829a4aSRandall Stewart * We always send a CWR this way if our previous one was lost our 2980f8829a4aSRandall Stewart * peer will get an update, or if it is not time again to reduce we 2981a21779f0SRandall Stewart * still get the cwr to the peer. Note we set the override when we 2982a21779f0SRandall Stewart * could not find the TSN on the chunk or the destination network. 2983f8829a4aSRandall Stewart */ 2984a21779f0SRandall Stewart sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); 2985f8829a4aSRandall Stewart } 2986f8829a4aSRandall Stewart 2987f8829a4aSRandall Stewart static void 2988a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) 2989f8829a4aSRandall Stewart { 2990f8829a4aSRandall Stewart /* 2991f8829a4aSRandall Stewart * Here we get a CWR from the peer. We must look in the outqueue and 2992582212faSEitan Adler * make sure that we have a covered ECNE in the control chunk part. 2993f8829a4aSRandall Stewart * If so remove it. 2994f8829a4aSRandall Stewart */ 29956c2cfc04SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 2996f8829a4aSRandall Stewart struct sctp_ecne_chunk *ecne; 2997a21779f0SRandall Stewart int override; 2998a21779f0SRandall Stewart uint32_t cwr_tsn; 2999f8829a4aSRandall Stewart 3000a21779f0SRandall Stewart cwr_tsn = ntohl(cp->tsn); 3001a21779f0SRandall Stewart override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; 30026c2cfc04SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) { 3003f8829a4aSRandall Stewart if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { 3004f8829a4aSRandall Stewart continue; 3005f8829a4aSRandall Stewart } 3006a21779f0SRandall Stewart if ((override == 0) && (chk->whoTo != net)) { 3007a21779f0SRandall Stewart /* Must be from the right src unless override is set */ 3008a21779f0SRandall Stewart continue; 3009a21779f0SRandall Stewart } 3010f8829a4aSRandall Stewart ecne = mtod(chk->data, struct sctp_ecne_chunk *); 3011a21779f0SRandall Stewart if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { 3012f8829a4aSRandall Stewart /* this covers this ECNE, we can remove it */ 3013f8829a4aSRandall Stewart stcb->asoc.ecn_echo_cnt_onq--; 3014f8829a4aSRandall Stewart TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, 3015f8829a4aSRandall Stewart sctp_next); 3016cd6340caSMichael Tuexen stcb->asoc.ctrl_queue_cnt--; 3017f8829a4aSRandall Stewart sctp_m_freem(chk->data); 3018f8829a4aSRandall Stewart chk->data = NULL; 3019689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3020a21779f0SRandall Stewart if (override == 0) { 3021f8829a4aSRandall Stewart break; 3022f8829a4aSRandall Stewart } 3023f8829a4aSRandall Stewart } 3024f8829a4aSRandall Stewart } 3025a21779f0SRandall Stewart } 3026f8829a4aSRandall Stewart 3027f8829a4aSRandall Stewart static void 30287215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, 3029f8829a4aSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net) 3030f8829a4aSRandall Stewart { 3031ceaad40aSRandall Stewart 3032ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 3033ad81507eSRandall Stewart "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); 3034f8829a4aSRandall Stewart if (stcb == NULL) 3035f8829a4aSRandall Stewart return; 3036f8829a4aSRandall Stewart 3037f8829a4aSRandall Stewart /* process according to association state */ 3038839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) { 3039f8829a4aSRandall Stewart /* unexpected SHUTDOWN-COMPLETE... so ignore... */ 30402afb3e84SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 30412afb3e84SRandall Stewart "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); 3042f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 3043f8829a4aSRandall Stewart return; 3044f8829a4aSRandall Stewart } 3045f8829a4aSRandall Stewart /* notify upper layer protocol */ 3046f8829a4aSRandall Stewart if (stcb->sctp_socket) { 3047ceaad40aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 304856f778aaSMichael Tuexen } 304956f778aaSMichael Tuexen #ifdef INVARIANTS 30500f1346f7SMichael Tuexen if (!TAILQ_EMPTY(&stcb->asoc.send_queue) || 30510f1346f7SMichael Tuexen !TAILQ_EMPTY(&stcb->asoc.sent_queue) || 3052124d851aSMichael Tuexen sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) { 305356f778aaSMichael Tuexen panic("Queues are not empty when handling SHUTDOWN-COMPLETE"); 3054f8829a4aSRandall Stewart } 305556f778aaSMichael Tuexen #endif 3056f8829a4aSRandall Stewart /* stop the timer */ 3057b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, 3058b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_24); 3059f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_shutdown); 3060f8829a4aSRandall Stewart /* free the TCB */ 30612afb3e84SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, 30622afb3e84SRandall Stewart "sctp_handle_shutdown_complete: calls free-asoc\n"); 3063b7d130beSMichael Tuexen (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, 3064b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_25); 3065f8829a4aSRandall Stewart return; 3066f8829a4aSRandall Stewart } 3067f8829a4aSRandall Stewart 3068f8829a4aSRandall Stewart static int 3069f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, 3070f8829a4aSRandall Stewart struct sctp_nets *net, uint8_t flg) 3071f8829a4aSRandall Stewart { 3072f8829a4aSRandall Stewart switch (desc->chunk_type) { 3073f8829a4aSRandall Stewart case SCTP_DATA: 307432df1c9eSMichael Tuexen case SCTP_IDATA: 307532df1c9eSMichael Tuexen /* find the tsn to resend (possibly) */ 3076f8829a4aSRandall Stewart { 3077f8829a4aSRandall Stewart uint32_t tsn; 3078f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1; 3079f8829a4aSRandall Stewart 3080f8829a4aSRandall Stewart tsn = ntohl(desc->tsn_ifany); 30814a9ef3f8SMichael Tuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 308249656eefSMichael Tuexen if (tp1->rec.data.tsn == tsn) { 3083f8829a4aSRandall Stewart /* found it */ 3084f8829a4aSRandall Stewart break; 3085f8829a4aSRandall Stewart } 308649656eefSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) { 3087f8829a4aSRandall Stewart /* not found */ 3088f8829a4aSRandall Stewart tp1 = NULL; 3089f8829a4aSRandall Stewart break; 3090f8829a4aSRandall Stewart } 3091f8829a4aSRandall Stewart } 3092f8829a4aSRandall Stewart if (tp1 == NULL) { 3093f8829a4aSRandall Stewart /* 3094f8829a4aSRandall Stewart * Do it the other way , aka without paying 3095f8829a4aSRandall Stewart * attention to queue seq order. 3096f8829a4aSRandall Stewart */ 3097f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpdnfnd); 30984a9ef3f8SMichael Tuexen TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 309949656eefSMichael Tuexen if (tp1->rec.data.tsn == tsn) { 3100f8829a4aSRandall Stewart /* found it */ 3101f8829a4aSRandall Stewart break; 3102f8829a4aSRandall Stewart } 3103f8829a4aSRandall Stewart } 3104f8829a4aSRandall Stewart } 3105f8829a4aSRandall Stewart if (tp1 == NULL) { 3106f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrptsnnf); 3107f8829a4aSRandall Stewart } 3108f8829a4aSRandall Stewart if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { 31097da23bc8SMichael Tuexen if (((flg & SCTP_BADCRC) == 0) && 31107da23bc8SMichael Tuexen ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 31117da23bc8SMichael Tuexen return (0); 31127da23bc8SMichael Tuexen } 3113f8829a4aSRandall Stewart if ((stcb->asoc.peers_rwnd == 0) && 3114f8829a4aSRandall Stewart ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { 3115f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpdiwnp); 3116f8829a4aSRandall Stewart return (0); 3117f8829a4aSRandall Stewart } 3118f8829a4aSRandall Stewart if (stcb->asoc.peers_rwnd == 0 && 3119f8829a4aSRandall Stewart (flg & SCTP_FROM_MIDDLE_BOX)) { 3120f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpdizrw); 3121f8829a4aSRandall Stewart return (0); 3122f8829a4aSRandall Stewart } 312332df1c9eSMichael Tuexen if ((uint32_t)SCTP_BUF_LEN(tp1->data) < 312432df1c9eSMichael Tuexen SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) { 312532df1c9eSMichael Tuexen /* Payload not matching. */ 3126f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpbadd); 3127f8829a4aSRandall Stewart return (-1); 3128f8829a4aSRandall Stewart } 312932df1c9eSMichael Tuexen if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb), 313032df1c9eSMichael Tuexen desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) { 313132df1c9eSMichael Tuexen /* Payload not matching. */ 313232df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpbadd); 313332df1c9eSMichael Tuexen return (-1); 3134f8829a4aSRandall Stewart } 3135f8829a4aSRandall Stewart if (tp1->do_rtt) { 3136f8829a4aSRandall Stewart /* 3137f8829a4aSRandall Stewart * this guy had a RTO calculation 3138f8829a4aSRandall Stewart * pending on it, cancel it 3139f8829a4aSRandall Stewart */ 3140f79aab18SRandall Stewart if (tp1->whoTo->rto_needed == 0) { 3141f79aab18SRandall Stewart tp1->whoTo->rto_needed = 1; 3142f79aab18SRandall Stewart } 3143f8829a4aSRandall Stewart tp1->do_rtt = 0; 3144f8829a4aSRandall Stewart } 3145f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpmark); 3146f8829a4aSRandall Stewart if (tp1->sent != SCTP_DATAGRAM_RESEND) 3147f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3148f8829a4aSRandall Stewart /* 3149f8829a4aSRandall Stewart * mark it as if we were doing a FR, since 3150f8829a4aSRandall Stewart * we will be getting gap ack reports behind 3151f8829a4aSRandall Stewart * the info from the router. 3152f8829a4aSRandall Stewart */ 3153f8829a4aSRandall Stewart tp1->rec.data.doing_fast_retransmit = 1; 3154f8829a4aSRandall Stewart /* 3155f8829a4aSRandall Stewart * mark the tsn with what sequences can 3156f8829a4aSRandall Stewart * cause a new FR. 3157f8829a4aSRandall Stewart */ 3158f8829a4aSRandall Stewart if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 3159f8829a4aSRandall Stewart tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 3160f8829a4aSRandall Stewart } else { 316149656eefSMichael Tuexen tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn; 3162f8829a4aSRandall Stewart } 3163f8829a4aSRandall Stewart 3164f8829a4aSRandall Stewart /* restart the timer */ 3165f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3166b7d130beSMichael Tuexen stcb, tp1->whoTo, 3167b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_26); 3168f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3169f8829a4aSRandall Stewart stcb, tp1->whoTo); 3170f8829a4aSRandall Stewart 3171f8829a4aSRandall Stewart /* fix counts and things */ 3172b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3173c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, 3174a5d547adSRandall Stewart tp1->whoTo->flight_size, 3175a5d547adSRandall Stewart tp1->book_size, 31769a8e3088SMichael Tuexen (uint32_t)(uintptr_t)stcb, 317749656eefSMichael Tuexen tp1->rec.data.tsn); 317880fefe0aSRandall Stewart } 31798933fa13SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3180c105859eSRandall Stewart sctp_flight_size_decrease(tp1); 3181c105859eSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 31828933fa13SRandall Stewart } 3183f23ba7b1SMichael Tuexen tp1->sent = SCTP_DATAGRAM_RESEND; 3184f8829a4aSRandall Stewart } { 3185f8829a4aSRandall Stewart /* audit code */ 3186f8829a4aSRandall Stewart unsigned int audit; 3187f8829a4aSRandall Stewart 3188f8829a4aSRandall Stewart audit = 0; 3189f8829a4aSRandall Stewart TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { 3190f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) 3191f8829a4aSRandall Stewart audit++; 3192f8829a4aSRandall Stewart } 3193f8829a4aSRandall Stewart TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, 3194f8829a4aSRandall Stewart sctp_next) { 3195f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) 3196f8829a4aSRandall Stewart audit++; 3197f8829a4aSRandall Stewart } 3198f8829a4aSRandall Stewart if (audit != stcb->asoc.sent_queue_retran_cnt) { 3199ad81507eSRandall Stewart SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", 3200f8829a4aSRandall Stewart audit, stcb->asoc.sent_queue_retran_cnt); 3201f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED 3202f8829a4aSRandall Stewart stcb->asoc.sent_queue_retran_cnt = audit; 3203f8829a4aSRandall Stewart #endif 3204f8829a4aSRandall Stewart } 3205f8829a4aSRandall Stewart } 3206f8829a4aSRandall Stewart } 3207f8829a4aSRandall Stewart break; 3208f8829a4aSRandall Stewart case SCTP_ASCONF: 3209f8829a4aSRandall Stewart { 3210f8829a4aSRandall Stewart struct sctp_tmit_chunk *asconf; 3211f8829a4aSRandall Stewart 3212f8829a4aSRandall Stewart TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, 3213f8829a4aSRandall Stewart sctp_next) { 3214f8829a4aSRandall Stewart if (asconf->rec.chunk_id.id == SCTP_ASCONF) { 3215f8829a4aSRandall Stewart break; 3216f8829a4aSRandall Stewart } 3217f8829a4aSRandall Stewart } 3218f8829a4aSRandall Stewart if (asconf) { 3219f8829a4aSRandall Stewart if (asconf->sent != SCTP_DATAGRAM_RESEND) 3220f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3221f8829a4aSRandall Stewart asconf->sent = SCTP_DATAGRAM_RESEND; 3222f8829a4aSRandall Stewart asconf->snd_count--; 3223f8829a4aSRandall Stewart } 3224f8829a4aSRandall Stewart } 3225f8829a4aSRandall Stewart break; 3226f8829a4aSRandall Stewart case SCTP_INITIATION: 3227f8829a4aSRandall Stewart /* resend the INIT */ 3228f8829a4aSRandall Stewart stcb->asoc.dropped_special_cnt++; 3229f8829a4aSRandall Stewart if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { 3230f8829a4aSRandall Stewart /* 3231f8829a4aSRandall Stewart * If we can get it in, in a few attempts we do 3232f8829a4aSRandall Stewart * this, otherwise we let the timer fire. 3233f8829a4aSRandall Stewart */ 3234f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, 3235b7d130beSMichael Tuexen stcb, net, 3236b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_27); 3237ceaad40aSRandall Stewart sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); 3238f8829a4aSRandall Stewart } 3239f8829a4aSRandall Stewart break; 3240f8829a4aSRandall Stewart case SCTP_SELECTIVE_ACK: 3241b5c16493SMichael Tuexen case SCTP_NR_SELECTIVE_ACK: 3242f8829a4aSRandall Stewart /* resend the sack */ 3243689e6a5fSMichael Tuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 3244f8829a4aSRandall Stewart break; 3245f8829a4aSRandall Stewart case SCTP_HEARTBEAT_REQUEST: 3246f8829a4aSRandall Stewart /* resend a demand HB */ 3247b54d3a6cSRandall Stewart if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { 3248b7b84c0eSMichael Tuexen /* 3249b7b84c0eSMichael Tuexen * Only retransmit if we KNOW we wont destroy the 3250b7b84c0eSMichael Tuexen * tcb 3251b7b84c0eSMichael Tuexen */ 3252ca85e948SMichael Tuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 3253b54d3a6cSRandall Stewart } 3254f8829a4aSRandall Stewart break; 3255f8829a4aSRandall Stewart case SCTP_SHUTDOWN: 3256f8829a4aSRandall Stewart sctp_send_shutdown(stcb, net); 3257f8829a4aSRandall Stewart break; 3258f8829a4aSRandall Stewart case SCTP_SHUTDOWN_ACK: 3259f8829a4aSRandall Stewart sctp_send_shutdown_ack(stcb, net); 3260f8829a4aSRandall Stewart break; 3261f8829a4aSRandall Stewart case SCTP_COOKIE_ECHO: 3262f8829a4aSRandall Stewart { 3263f8829a4aSRandall Stewart struct sctp_tmit_chunk *cookie; 3264f8829a4aSRandall Stewart 3265f8829a4aSRandall Stewart cookie = NULL; 3266f8829a4aSRandall Stewart TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, 3267f8829a4aSRandall Stewart sctp_next) { 3268f8829a4aSRandall Stewart if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 3269f8829a4aSRandall Stewart break; 3270f8829a4aSRandall Stewart } 3271f8829a4aSRandall Stewart } 3272f8829a4aSRandall Stewart if (cookie) { 3273f8829a4aSRandall Stewart if (cookie->sent != SCTP_DATAGRAM_RESEND) 3274f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 3275f8829a4aSRandall Stewart cookie->sent = SCTP_DATAGRAM_RESEND; 3276f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(stcb); 3277f8829a4aSRandall Stewart } 3278f8829a4aSRandall Stewart } 3279f8829a4aSRandall Stewart break; 3280f8829a4aSRandall Stewart case SCTP_COOKIE_ACK: 3281f8829a4aSRandall Stewart sctp_send_cookie_ack(stcb); 3282f8829a4aSRandall Stewart break; 3283f8829a4aSRandall Stewart case SCTP_ASCONF_ACK: 3284f8829a4aSRandall Stewart /* resend last asconf ack */ 32852afb3e84SRandall Stewart sctp_send_asconf_ack(stcb); 3286f8829a4aSRandall Stewart break; 328744249214SRandall Stewart case SCTP_IFORWARD_CUM_TSN: 3288f8829a4aSRandall Stewart case SCTP_FORWARD_CUM_TSN: 3289f8829a4aSRandall Stewart send_forward_tsn(stcb, &stcb->asoc); 3290f8829a4aSRandall Stewart break; 3291f8829a4aSRandall Stewart /* can't do anything with these */ 3292f8829a4aSRandall Stewart case SCTP_PACKET_DROPPED: 3293f8829a4aSRandall Stewart case SCTP_INITIATION_ACK: /* this should not happen */ 3294f8829a4aSRandall Stewart case SCTP_HEARTBEAT_ACK: 3295f8829a4aSRandall Stewart case SCTP_ABORT_ASSOCIATION: 3296f8829a4aSRandall Stewart case SCTP_OPERATION_ERROR: 3297f8829a4aSRandall Stewart case SCTP_SHUTDOWN_COMPLETE: 3298f8829a4aSRandall Stewart case SCTP_ECN_ECHO: 3299f8829a4aSRandall Stewart case SCTP_ECN_CWR: 3300f8829a4aSRandall Stewart default: 3301f8829a4aSRandall Stewart break; 3302f8829a4aSRandall Stewart } 3303f8829a4aSRandall Stewart return (0); 3304f8829a4aSRandall Stewart } 3305f8829a4aSRandall Stewart 3306f8829a4aSRandall Stewart void 3307a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list) 3308f8829a4aSRandall Stewart { 3309a169d6ecSMichael Tuexen uint32_t i; 3310f8829a4aSRandall Stewart uint16_t temp; 3311f8829a4aSRandall Stewart 3312f8829a4aSRandall Stewart /* 331344249214SRandall Stewart * We set things to 0xffffffff since this is the last delivered 331444249214SRandall Stewart * sequence and we will be sending in 0 after the reset. 3315f8829a4aSRandall Stewart */ 3316f8829a4aSRandall Stewart 3317f8829a4aSRandall Stewart if (number_entries) { 3318f8829a4aSRandall Stewart for (i = 0; i < number_entries; i++) { 3319f8829a4aSRandall Stewart temp = ntohs(list[i]); 3320f8829a4aSRandall Stewart if (temp >= stcb->asoc.streamincnt) { 3321f8829a4aSRandall Stewart continue; 3322f8829a4aSRandall Stewart } 332349656eefSMichael Tuexen stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff; 3324f8829a4aSRandall Stewart } 3325f8829a4aSRandall Stewart } else { 3326f8829a4aSRandall Stewart list = NULL; 3327f8829a4aSRandall Stewart for (i = 0; i < stcb->asoc.streamincnt; i++) { 332849656eefSMichael Tuexen stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff; 3329f8829a4aSRandall Stewart } 3330f8829a4aSRandall Stewart } 3331ceaad40aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3332f8829a4aSRandall Stewart } 3333f8829a4aSRandall Stewart 3334f8829a4aSRandall Stewart static void 333556f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list) 3336f8829a4aSRandall Stewart { 333756f778aaSMichael Tuexen uint32_t i; 3338f8829a4aSRandall Stewart uint16_t temp; 3339f8829a4aSRandall Stewart 334056f778aaSMichael Tuexen if (number_entries > 0) { 334156f778aaSMichael Tuexen for (i = 0; i < number_entries; i++) { 3342f8829a4aSRandall Stewart temp = ntohs(list[i]); 3343f8829a4aSRandall Stewart if (temp >= stcb->asoc.streamoutcnt) { 3344f8829a4aSRandall Stewart /* no such stream */ 3345f8829a4aSRandall Stewart continue; 3346f8829a4aSRandall Stewart } 334763d5b568SMichael Tuexen stcb->asoc.strmout[temp].next_mid_ordered = 0; 334863d5b568SMichael Tuexen stcb->asoc.strmout[temp].next_mid_unordered = 0; 3349f8829a4aSRandall Stewart } 335056f778aaSMichael Tuexen } else { 335156f778aaSMichael Tuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 335263d5b568SMichael Tuexen stcb->asoc.strmout[i].next_mid_ordered = 0; 335363d5b568SMichael Tuexen stcb->asoc.strmout[i].next_mid_unordered = 0; 335456f778aaSMichael Tuexen } 3355f8829a4aSRandall Stewart } 3356ceaad40aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); 3357f8829a4aSRandall Stewart } 3358f8829a4aSRandall Stewart 33597cca1775SRandall Stewart static void 33607cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list) 33617cca1775SRandall Stewart { 33627cca1775SRandall Stewart uint32_t i; 33637cca1775SRandall Stewart uint16_t temp; 33647cca1775SRandall Stewart 33657cca1775SRandall Stewart if (number_entries > 0) { 33667cca1775SRandall Stewart for (i = 0; i < number_entries; i++) { 33677cca1775SRandall Stewart temp = ntohs(list[i]); 33687cca1775SRandall Stewart if (temp >= stcb->asoc.streamoutcnt) { 33697cca1775SRandall Stewart /* no such stream */ 33707cca1775SRandall Stewart continue; 33717cca1775SRandall Stewart } 33727cca1775SRandall Stewart stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN; 33737cca1775SRandall Stewart } 33747cca1775SRandall Stewart } else { 33757cca1775SRandall Stewart for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 33767cca1775SRandall Stewart stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN; 33777cca1775SRandall Stewart } 33787cca1775SRandall Stewart } 33797cca1775SRandall Stewart } 33807cca1775SRandall Stewart 338184f3b49aSMichael Tuexen struct sctp_stream_reset_request * 3382f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) 3383f8829a4aSRandall Stewart { 3384f8829a4aSRandall Stewart struct sctp_association *asoc; 3385a169d6ecSMichael Tuexen struct sctp_chunkhdr *ch; 338684f3b49aSMichael Tuexen struct sctp_stream_reset_request *r; 3387f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 3388f8829a4aSRandall Stewart int len, clen; 3389f8829a4aSRandall Stewart 3390f8829a4aSRandall Stewart asoc = &stcb->asoc; 3391163153c2SMichael Tuexen chk = asoc->str_reset; 3392163153c2SMichael Tuexen if (TAILQ_EMPTY(&asoc->control_send_queue) || 3393163153c2SMichael Tuexen (chk == NULL)) { 3394d06c82f1SRandall Stewart asoc->stream_reset_outstanding = 0; 3395f8829a4aSRandall Stewart return (NULL); 3396f8829a4aSRandall Stewart } 3397f8829a4aSRandall Stewart if (chk->data == NULL) { 3398f8829a4aSRandall Stewart return (NULL); 3399f8829a4aSRandall Stewart } 3400163153c2SMichael Tuexen if (bchk != NULL) { 3401f8829a4aSRandall Stewart /* he wants a copy of the chk pointer */ 3402f8829a4aSRandall Stewart *bchk = chk; 3403f8829a4aSRandall Stewart } 3404f8829a4aSRandall Stewart clen = chk->send_size; 3405a169d6ecSMichael Tuexen ch = mtod(chk->data, struct sctp_chunkhdr *); 340684f3b49aSMichael Tuexen r = (struct sctp_stream_reset_request *)(ch + 1); 3407f8829a4aSRandall Stewart if (ntohl(r->request_seq) == seq) { 3408f8829a4aSRandall Stewart /* found it */ 3409f8829a4aSRandall Stewart return (r); 3410f8829a4aSRandall Stewart } 3411f8829a4aSRandall Stewart len = SCTP_SIZE32(ntohs(r->ph.param_length)); 3412f8829a4aSRandall Stewart if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { 3413f8829a4aSRandall Stewart /* move to the next one, there can only be a max of two */ 341484f3b49aSMichael Tuexen r = (struct sctp_stream_reset_request *)((caddr_t)r + len); 3415f8829a4aSRandall Stewart if (ntohl(r->request_seq) == seq) { 3416f8829a4aSRandall Stewart return (r); 3417f8829a4aSRandall Stewart } 3418f8829a4aSRandall Stewart } 3419f8829a4aSRandall Stewart /* that seq is not here */ 3420f8829a4aSRandall Stewart return (NULL); 3421f8829a4aSRandall Stewart } 3422f8829a4aSRandall Stewart 3423f8829a4aSRandall Stewart static void 3424f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb) 3425f8829a4aSRandall Stewart { 3426f8829a4aSRandall Stewart struct sctp_association *asoc; 3427cd6340caSMichael Tuexen struct sctp_tmit_chunk *chk; 3428f8829a4aSRandall Stewart 3429cd6340caSMichael Tuexen asoc = &stcb->asoc; 3430cd6340caSMichael Tuexen chk = asoc->str_reset; 3431cd6340caSMichael Tuexen if (chk == NULL) { 3432f8829a4aSRandall Stewart return; 3433f8829a4aSRandall Stewart } 3434cd6340caSMichael Tuexen asoc->str_reset = NULL; 3435b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, 34366fb7b4fbSMichael Tuexen NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28); 3437cd6340caSMichael Tuexen TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 3438cd6340caSMichael Tuexen asoc->ctrl_queue_cnt--; 3439f8829a4aSRandall Stewart if (chk->data) { 3440f8829a4aSRandall Stewart sctp_m_freem(chk->data); 3441f8829a4aSRandall Stewart chk->data = NULL; 3442f8829a4aSRandall Stewart } 3443689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 3444f8829a4aSRandall Stewart } 3445f8829a4aSRandall Stewart 3446f8829a4aSRandall Stewart static int 3447f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb, 3448f8829a4aSRandall Stewart uint32_t seq, uint32_t action, 344908598d70SRandall Stewart struct sctp_stream_reset_response *respin) 3450f8829a4aSRandall Stewart { 3451f8829a4aSRandall Stewart uint16_t type; 3452f4358911SMichael Tuexen int lparam_len; 3453f8829a4aSRandall Stewart struct sctp_association *asoc = &stcb->asoc; 3454f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 345584f3b49aSMichael Tuexen struct sctp_stream_reset_request *req_param; 345684f3b49aSMichael Tuexen struct sctp_stream_reset_out_request *req_out_param; 345784f3b49aSMichael Tuexen struct sctp_stream_reset_in_request *req_in_param; 345856f778aaSMichael Tuexen uint32_t number_entries; 3459f8829a4aSRandall Stewart 3460f8829a4aSRandall Stewart if (asoc->stream_reset_outstanding == 0) { 3461f8829a4aSRandall Stewart /* duplicate */ 3462f8829a4aSRandall Stewart return (0); 3463f8829a4aSRandall Stewart } 3464f8829a4aSRandall Stewart if (seq == stcb->asoc.str_reset_seq_out) { 346584f3b49aSMichael Tuexen req_param = sctp_find_stream_reset(stcb, seq, &chk); 346684f3b49aSMichael Tuexen if (req_param != NULL) { 3467f8829a4aSRandall Stewart stcb->asoc.str_reset_seq_out++; 346884f3b49aSMichael Tuexen type = ntohs(req_param->ph.param_type); 3469f4358911SMichael Tuexen lparam_len = ntohs(req_param->ph.param_length); 3470f8829a4aSRandall Stewart if (type == SCTP_STR_RESET_OUT_REQUEST) { 34717cca1775SRandall Stewart int no_clear = 0; 34727cca1775SRandall Stewart 347384f3b49aSMichael Tuexen req_out_param = (struct sctp_stream_reset_out_request *)req_param; 3474f4358911SMichael Tuexen number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); 3475f8829a4aSRandall Stewart asoc->stream_reset_out_is_outstanding = 0; 3476f8829a4aSRandall Stewart if (asoc->stream_reset_outstanding) 3477f8829a4aSRandall Stewart asoc->stream_reset_outstanding--; 3478f3ebe64cSMichael Tuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3479f8829a4aSRandall Stewart /* do it */ 348084f3b49aSMichael Tuexen sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams); 3481d4260646SMichael Tuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 348284f3b49aSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED); 34837cca1775SRandall Stewart } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) { 3484b7b84c0eSMichael Tuexen /* 3485b7b84c0eSMichael Tuexen * Set it up so we don't stop 3486b7b84c0eSMichael Tuexen * retransmitting 3487b7b84c0eSMichael Tuexen */ 3488a4889f2dSMichael Tuexen asoc->stream_reset_outstanding++; 34897cca1775SRandall Stewart stcb->asoc.str_reset_seq_out--; 34907cca1775SRandall Stewart asoc->stream_reset_out_is_outstanding = 1; 34917cca1775SRandall Stewart no_clear = 1; 3492f8829a4aSRandall Stewart } else { 349384f3b49aSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED); 3494f8829a4aSRandall Stewart } 34957cca1775SRandall Stewart if (no_clear == 0) { 34967cca1775SRandall Stewart sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams); 34977cca1775SRandall Stewart } 3498f8829a4aSRandall Stewart } else if (type == SCTP_STR_RESET_IN_REQUEST) { 349984f3b49aSMichael Tuexen req_in_param = (struct sctp_stream_reset_in_request *)req_param; 3500f4358911SMichael Tuexen number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); 3501f8829a4aSRandall Stewart if (asoc->stream_reset_outstanding) 3502f8829a4aSRandall Stewart asoc->stream_reset_outstanding--; 3503d4260646SMichael Tuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3504d4260646SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, 350584f3b49aSMichael Tuexen number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED); 3506d4260646SMichael Tuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3507c4e848b7SRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, 350884f3b49aSMichael Tuexen number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED); 3509f8829a4aSRandall Stewart } 3510c4e848b7SRandall Stewart } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { 3511ea44232bSRandall Stewart /* Ok we now may have more streams */ 3512c4e848b7SRandall Stewart int num_stream; 3513c4e848b7SRandall Stewart 3514c4e848b7SRandall Stewart num_stream = stcb->asoc.strm_pending_add_size; 3515c4e848b7SRandall Stewart if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { 3516c4e848b7SRandall Stewart /* TSNH */ 3517c4e848b7SRandall Stewart num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; 3518c4e848b7SRandall Stewart } 3519c4e848b7SRandall Stewart stcb->asoc.strm_pending_add_size = 0; 35208aae9493SRandall Stewart if (asoc->stream_reset_outstanding) 35218aae9493SRandall Stewart asoc->stream_reset_outstanding--; 3522f3ebe64cSMichael Tuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3523ea44232bSRandall Stewart /* Put the new streams into effect */ 35247cca1775SRandall Stewart int i; 35257cca1775SRandall Stewart 35267cca1775SRandall Stewart for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) { 35277cca1775SRandall Stewart asoc->strmout[i].state = SCTP_STREAM_OPEN; 35287cca1775SRandall Stewart } 35297cca1775SRandall Stewart asoc->streamoutcnt += num_stream; 3530c4e848b7SRandall Stewart sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3531d4260646SMichael Tuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3532d4260646SMichael Tuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3533d4260646SMichael Tuexen SCTP_STREAM_CHANGE_DENIED); 3534ea44232bSRandall Stewart } else { 3535c4e848b7SRandall Stewart sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3536d4260646SMichael Tuexen SCTP_STREAM_CHANGE_FAILED); 3537c4e848b7SRandall Stewart } 3538c4e848b7SRandall Stewart } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { 3539c4e848b7SRandall Stewart if (asoc->stream_reset_outstanding) 3540c4e848b7SRandall Stewart asoc->stream_reset_outstanding--; 3541d4260646SMichael Tuexen if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3542c4e848b7SRandall Stewart sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3543d4260646SMichael Tuexen SCTP_STREAM_CHANGE_DENIED); 3544d4260646SMichael Tuexen } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { 3545d4260646SMichael Tuexen sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 3546d4260646SMichael Tuexen SCTP_STREAM_CHANGE_FAILED); 3547ea44232bSRandall Stewart } 3548f8829a4aSRandall Stewart } else if (type == SCTP_STR_RESET_TSN_REQUEST) { 3549f8829a4aSRandall Stewart /** 3550f8829a4aSRandall Stewart * a) Adopt the new in tsn. 3551f8829a4aSRandall Stewart * b) reset the map 3552f8829a4aSRandall Stewart * c) Adopt the new out-tsn 3553f8829a4aSRandall Stewart */ 3554f8829a4aSRandall Stewart struct sctp_stream_reset_response_tsn *resp; 3555f8829a4aSRandall Stewart struct sctp_forward_tsn_chunk fwdtsn; 3556f8829a4aSRandall Stewart int abort_flag = 0; 3557f8829a4aSRandall Stewart 3558f8829a4aSRandall Stewart if (respin == NULL) { 3559f8829a4aSRandall Stewart /* huh ? */ 3560f8829a4aSRandall Stewart return (0); 3561f8829a4aSRandall Stewart } 35626a58f0e9SXin LI if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) { 35636a58f0e9SXin LI return (0); 35646a58f0e9SXin LI } 3565f3ebe64cSMichael Tuexen if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { 3566f8829a4aSRandall Stewart resp = (struct sctp_stream_reset_response_tsn *)respin; 3567f8829a4aSRandall Stewart asoc->stream_reset_outstanding--; 3568f8829a4aSRandall Stewart fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3569f8829a4aSRandall Stewart fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3570f8829a4aSRandall Stewart fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); 3571671d309cSRandall Stewart sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3572f8829a4aSRandall Stewart if (abort_flag) { 3573f8829a4aSRandall Stewart return (1); 3574f8829a4aSRandall Stewart } 3575f8829a4aSRandall Stewart stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); 3576b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3577b3f1ea41SRandall Stewart sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3578b3f1ea41SRandall Stewart } 35790053ed28SMichael Tuexen 3580d6af161aSRandall Stewart stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; 3581f8829a4aSRandall Stewart stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); 3582f8829a4aSRandall Stewart memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 3583830d754dSRandall Stewart 3584830d754dSRandall Stewart stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; 3585b5c16493SMichael Tuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 358677acdc25SRandall Stewart 3587f8829a4aSRandall Stewart stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); 3588f8829a4aSRandall Stewart stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; 3589f8829a4aSRandall Stewart 3590f8829a4aSRandall Stewart sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL); 3591f8829a4aSRandall Stewart sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL); 3592c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); 3593d4260646SMichael Tuexen } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { 3594d4260646SMichael Tuexen sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3595d4260646SMichael Tuexen SCTP_ASSOC_RESET_DENIED); 3596c4e848b7SRandall Stewart } else { 3597c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 3598d4260646SMichael Tuexen SCTP_ASSOC_RESET_FAILED); 3599f8829a4aSRandall Stewart } 3600f8829a4aSRandall Stewart } 3601f8829a4aSRandall Stewart /* get rid of the request and get the request flags */ 3602f8829a4aSRandall Stewart if (asoc->stream_reset_outstanding == 0) { 3603f8829a4aSRandall Stewart sctp_clean_up_stream_reset(stcb); 3604f8829a4aSRandall Stewart } 3605f8829a4aSRandall Stewart } 3606f8829a4aSRandall Stewart } 36077cca1775SRandall Stewart if (asoc->stream_reset_outstanding == 0) { 3608c6168599SRandall Stewart sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED); 36097cca1775SRandall Stewart } 3610f8829a4aSRandall Stewart return (0); 3611f8829a4aSRandall Stewart } 3612f8829a4aSRandall Stewart 3613f8829a4aSRandall Stewart static void 3614f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb, 3615f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk, 3616671d309cSRandall Stewart struct sctp_stream_reset_in_request *req, int trunc) 3617f8829a4aSRandall Stewart { 3618f8829a4aSRandall Stewart uint32_t seq; 3619f8829a4aSRandall Stewart int len, i; 3620f8829a4aSRandall Stewart int number_entries; 3621f8829a4aSRandall Stewart uint16_t temp; 3622f8829a4aSRandall Stewart 3623f8829a4aSRandall Stewart /* 3624f8829a4aSRandall Stewart * peer wants me to send a str-reset to him for my outgoing seq's if 3625f8829a4aSRandall Stewart * seq_in is right. 3626f8829a4aSRandall Stewart */ 3627f8829a4aSRandall Stewart struct sctp_association *asoc = &stcb->asoc; 3628f8829a4aSRandall Stewart 3629f8829a4aSRandall Stewart seq = ntohl(req->request_seq); 3630f8829a4aSRandall Stewart if (asoc->str_reset_seq_in == seq) { 3631671d309cSRandall Stewart asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3632f3ebe64cSMichael Tuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3633f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3634f3ebe64cSMichael Tuexen } else if (trunc) { 3635f3ebe64cSMichael Tuexen /* Can't do it, since they exceeded our buffer size */ 3636f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3637671d309cSRandall Stewart } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { 3638f8829a4aSRandall Stewart len = ntohs(req->ph.param_length); 3639f8829a4aSRandall Stewart number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); 36407cca1775SRandall Stewart if (number_entries) { 3641f8829a4aSRandall Stewart for (i = 0; i < number_entries; i++) { 3642f8829a4aSRandall Stewart temp = ntohs(req->list_of_streams[i]); 36437cca1775SRandall Stewart if (temp >= stcb->asoc.streamoutcnt) { 36447cca1775SRandall Stewart asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 36457cca1775SRandall Stewart goto bad_boy; 36467cca1775SRandall Stewart } 3647f8829a4aSRandall Stewart req->list_of_streams[i] = temp; 3648f8829a4aSRandall Stewart } 36497cca1775SRandall Stewart for (i = 0; i < number_entries; i++) { 36507cca1775SRandall Stewart if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) { 36517cca1775SRandall Stewart stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING; 36527cca1775SRandall Stewart } 36537cca1775SRandall Stewart } 36547cca1775SRandall Stewart } else { 36557cca1775SRandall Stewart /* Its all */ 36567cca1775SRandall Stewart for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 36577cca1775SRandall Stewart if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) 36587cca1775SRandall Stewart stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING; 36597cca1775SRandall Stewart } 36607cca1775SRandall Stewart } 3661f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3662f8829a4aSRandall Stewart } else { 3663f8829a4aSRandall Stewart /* Can't do it, since we have sent one out */ 3664f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3665f8829a4aSRandall Stewart } 36667cca1775SRandall Stewart bad_boy: 3667c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3668f8829a4aSRandall Stewart asoc->str_reset_seq_in++; 3669f8829a4aSRandall Stewart } else if (asoc->str_reset_seq_in - 1 == seq) { 3670f8829a4aSRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3671f8829a4aSRandall Stewart } else if (asoc->str_reset_seq_in - 2 == seq) { 3672f8829a4aSRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3673f8829a4aSRandall Stewart } else { 3674f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3675f8829a4aSRandall Stewart } 3676c6168599SRandall Stewart sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED); 3677f8829a4aSRandall Stewart } 3678f8829a4aSRandall Stewart 3679f8829a4aSRandall Stewart static int 3680f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, 3681f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk, 3682f8829a4aSRandall Stewart struct sctp_stream_reset_tsn_request *req) 3683f8829a4aSRandall Stewart { 3684f8829a4aSRandall Stewart /* reset all in and out and update the tsn */ 3685f8829a4aSRandall Stewart /* 3686f8829a4aSRandall Stewart * A) reset my str-seq's on in and out. B) Select a receive next, 3687f8829a4aSRandall Stewart * and set cum-ack to it. Also process this selected number as a 3688f8829a4aSRandall Stewart * fwd-tsn as well. C) set in the response my next sending seq. 3689f8829a4aSRandall Stewart */ 3690f8829a4aSRandall Stewart struct sctp_forward_tsn_chunk fwdtsn; 3691f8829a4aSRandall Stewart struct sctp_association *asoc = &stcb->asoc; 3692f8829a4aSRandall Stewart int abort_flag = 0; 3693f8829a4aSRandall Stewart uint32_t seq; 3694f8829a4aSRandall Stewart 3695f8829a4aSRandall Stewart seq = ntohl(req->request_seq); 3696f8829a4aSRandall Stewart if (asoc->str_reset_seq_in == seq) { 3697ce228dabSMichael Tuexen asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3698f3ebe64cSMichael Tuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3699f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3700ce228dabSMichael Tuexen } else { 3701f8829a4aSRandall Stewart fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); 3702f8829a4aSRandall Stewart fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; 3703f8829a4aSRandall Stewart fwdtsn.ch.chunk_flags = 0; 3704f8829a4aSRandall Stewart fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); 3705671d309cSRandall Stewart sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); 3706f8829a4aSRandall Stewart if (abort_flag) { 3707f8829a4aSRandall Stewart return (1); 3708f8829a4aSRandall Stewart } 3709f3ebe64cSMichael Tuexen asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; 3710b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 3711b3f1ea41SRandall Stewart sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 3712b3f1ea41SRandall Stewart } 3713f3ebe64cSMichael Tuexen asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; 3714f3ebe64cSMichael Tuexen asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; 3715f3ebe64cSMichael Tuexen memset(asoc->mapping_array, 0, asoc->mapping_array_size); 3716f3ebe64cSMichael Tuexen asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; 3717f3ebe64cSMichael Tuexen memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 3718f3ebe64cSMichael Tuexen atomic_add_int(&asoc->sending_seq, 1); 3719f8829a4aSRandall Stewart /* save off historical data for retrans */ 3720f3ebe64cSMichael Tuexen asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; 3721f3ebe64cSMichael Tuexen asoc->last_sending_seq[0] = asoc->sending_seq; 3722f3ebe64cSMichael Tuexen asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; 3723f3ebe64cSMichael Tuexen asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; 3724f8829a4aSRandall Stewart sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL); 3725f8829a4aSRandall Stewart sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL); 3726f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3727f3ebe64cSMichael Tuexen sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); 3728ce228dabSMichael Tuexen } 3729ce228dabSMichael Tuexen sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3730ce228dabSMichael Tuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3731f8829a4aSRandall Stewart asoc->str_reset_seq_in++; 3732f8829a4aSRandall Stewart } else if (asoc->str_reset_seq_in - 1 == seq) { 3733f8829a4aSRandall Stewart sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], 3734f3ebe64cSMichael Tuexen asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); 3735f8829a4aSRandall Stewart } else if (asoc->str_reset_seq_in - 2 == seq) { 3736f8829a4aSRandall Stewart sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], 3737f3ebe64cSMichael Tuexen asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); 3738f8829a4aSRandall Stewart } else { 3739f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3740f8829a4aSRandall Stewart } 3741f8829a4aSRandall Stewart return (0); 3742f8829a4aSRandall Stewart } 3743f8829a4aSRandall Stewart 3744f8829a4aSRandall Stewart static void 3745f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb, 3746f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk, 3747671d309cSRandall Stewart struct sctp_stream_reset_out_request *req, int trunc) 3748f8829a4aSRandall Stewart { 3749f8829a4aSRandall Stewart uint32_t seq, tsn; 3750f8829a4aSRandall Stewart int number_entries, len; 3751f8829a4aSRandall Stewart struct sctp_association *asoc = &stcb->asoc; 3752f8829a4aSRandall Stewart 3753f8829a4aSRandall Stewart seq = ntohl(req->request_seq); 3754f8829a4aSRandall Stewart 3755f8829a4aSRandall Stewart /* now if its not a duplicate we process it */ 3756f8829a4aSRandall Stewart if (asoc->str_reset_seq_in == seq) { 3757f8829a4aSRandall Stewart len = ntohs(req->ph.param_length); 3758f8829a4aSRandall Stewart number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); 3759f8829a4aSRandall Stewart /* 3760f8829a4aSRandall Stewart * the sender is resetting, handle the list issue.. we must 3761f8829a4aSRandall Stewart * a) verify if we can do the reset, if so no problem b) If 3762f8829a4aSRandall Stewart * we can't do the reset we must copy the request. c) queue 3763f8829a4aSRandall Stewart * it, and setup the data in processor to trigger it off 3764f8829a4aSRandall Stewart * when needed and dequeue all the queued data. 3765f8829a4aSRandall Stewart */ 3766f8829a4aSRandall Stewart tsn = ntohl(req->send_reset_at_tsn); 3767f8829a4aSRandall Stewart 3768f8829a4aSRandall Stewart /* move the reset action back one */ 3769f8829a4aSRandall Stewart asoc->last_reset_action[1] = asoc->last_reset_action[0]; 3770f3ebe64cSMichael Tuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { 3771f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3772f3ebe64cSMichael Tuexen } else if (trunc) { 3773f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 377420b07a4dSMichael Tuexen } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 3775f8829a4aSRandall Stewart /* we can do it now */ 3776f8829a4aSRandall Stewart sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); 3777f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3778f8829a4aSRandall Stewart } else { 3779f8829a4aSRandall Stewart /* 3780f8829a4aSRandall Stewart * we must queue it up and thus wait for the TSN's 3781f8829a4aSRandall Stewart * to arrive that are at or before tsn 3782f8829a4aSRandall Stewart */ 3783f8829a4aSRandall Stewart struct sctp_stream_reset_list *liste; 3784f8829a4aSRandall Stewart int siz; 3785f8829a4aSRandall Stewart 3786f8829a4aSRandall Stewart siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); 3787f8829a4aSRandall Stewart SCTP_MALLOC(liste, struct sctp_stream_reset_list *, 3788207304d4SRandall Stewart siz, SCTP_M_STRESET); 3789f8829a4aSRandall Stewart if (liste == NULL) { 3790f8829a4aSRandall Stewart /* gak out of memory */ 3791f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3792f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3793f8829a4aSRandall Stewart return; 3794f8829a4aSRandall Stewart } 37957cca1775SRandall Stewart liste->seq = seq; 3796f8829a4aSRandall Stewart liste->tsn = tsn; 3797f8829a4aSRandall Stewart liste->number_entries = number_entries; 3798a169d6ecSMichael Tuexen memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t)); 3799f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); 38007cca1775SRandall Stewart asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS; 3801f8829a4aSRandall Stewart } 3802c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3803f8829a4aSRandall Stewart asoc->str_reset_seq_in++; 3804f8829a4aSRandall Stewart } else if ((asoc->str_reset_seq_in - 1) == seq) { 3805f8829a4aSRandall Stewart /* 3806f8829a4aSRandall Stewart * one seq back, just echo back last action since my 3807f8829a4aSRandall Stewart * response was lost. 3808f8829a4aSRandall Stewart */ 3809f8829a4aSRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3810f8829a4aSRandall Stewart } else if ((asoc->str_reset_seq_in - 2) == seq) { 3811f8829a4aSRandall Stewart /* 3812f8829a4aSRandall Stewart * two seq back, just echo back last action since my 3813f8829a4aSRandall Stewart * response was lost. 3814f8829a4aSRandall Stewart */ 3815f8829a4aSRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3816f8829a4aSRandall Stewart } else { 3817f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3818f8829a4aSRandall Stewart } 3819f8829a4aSRandall Stewart } 3820f8829a4aSRandall Stewart 3821ea44232bSRandall Stewart static void 3822ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3823ea44232bSRandall Stewart struct sctp_stream_reset_add_strm *str_add) 3824ea44232bSRandall Stewart { 3825ea44232bSRandall Stewart /* 3826ea44232bSRandall Stewart * Peer is requesting to add more streams. If its within our 3827ea44232bSRandall Stewart * max-streams we will allow it. 3828ea44232bSRandall Stewart */ 3829c4e848b7SRandall Stewart uint32_t num_stream, i; 3830ea44232bSRandall Stewart uint32_t seq; 38318aae9493SRandall Stewart struct sctp_association *asoc = &stcb->asoc; 38324a9ef3f8SMichael Tuexen struct sctp_queued_to_read *ctl, *nctl; 3833ea44232bSRandall Stewart 3834ea44232bSRandall Stewart /* Get the number. */ 3835ea44232bSRandall Stewart seq = ntohl(str_add->request_seq); 3836ea44232bSRandall Stewart num_stream = ntohs(str_add->number_of_streams); 3837ea44232bSRandall Stewart /* Now what would be the new total? */ 38388aae9493SRandall Stewart if (asoc->str_reset_seq_in == seq) { 3839ea44232bSRandall Stewart num_stream += stcb->asoc.streamincnt; 3840f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3841f3ebe64cSMichael Tuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3842f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3843f3ebe64cSMichael Tuexen } else if ((num_stream > stcb->asoc.max_inbound_streams) || 3844c4e848b7SRandall Stewart (num_stream > 0xffff)) { 3845ea44232bSRandall Stewart /* We must reject it they ask for to many */ 3846ea44232bSRandall Stewart denied: 3847f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3848ea44232bSRandall Stewart } else { 3849ea44232bSRandall Stewart /* Ok, we can do that :-) */ 3850ea44232bSRandall Stewart struct sctp_stream_in *oldstrm; 3851ea44232bSRandall Stewart 3852ea44232bSRandall Stewart /* save off the old */ 3853ea44232bSRandall Stewart oldstrm = stcb->asoc.strmin; 3854ea44232bSRandall Stewart SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, 3855ea44232bSRandall Stewart (num_stream * sizeof(struct sctp_stream_in)), 3856ea44232bSRandall Stewart SCTP_M_STRMI); 3857ea44232bSRandall Stewart if (stcb->asoc.strmin == NULL) { 3858ea44232bSRandall Stewart stcb->asoc.strmin = oldstrm; 3859ea44232bSRandall Stewart goto denied; 3860ea44232bSRandall Stewart } 3861ea44232bSRandall Stewart /* copy off the old data */ 38628aae9493SRandall Stewart for (i = 0; i < stcb->asoc.streamincnt; i++) { 38638aae9493SRandall Stewart TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 386444249214SRandall Stewart TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue); 386549656eefSMichael Tuexen stcb->asoc.strmin[i].sid = i; 386649656eefSMichael Tuexen stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered; 38678aae9493SRandall Stewart stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; 386844249214SRandall Stewart stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started; 38698aae9493SRandall Stewart /* now anything on those queues? */ 387044249214SRandall Stewart TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) { 387144249214SRandall Stewart TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm); 387244249214SRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm); 387344249214SRandall Stewart } 387444249214SRandall Stewart TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) { 387544249214SRandall Stewart TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm); 387644249214SRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm); 38778aae9493SRandall Stewart } 38788aae9493SRandall Stewart } 3879ea44232bSRandall Stewart /* Init the new streams */ 3880ea44232bSRandall Stewart for (i = stcb->asoc.streamincnt; i < num_stream; i++) { 3881ea44232bSRandall Stewart TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); 388244249214SRandall Stewart TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue); 388349656eefSMichael Tuexen stcb->asoc.strmin[i].sid = i; 388449656eefSMichael Tuexen stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff; 388544249214SRandall Stewart stcb->asoc.strmin[i].pd_api_started = 0; 3886ea44232bSRandall Stewart stcb->asoc.strmin[i].delivery_started = 0; 3887ea44232bSRandall Stewart } 3888ea44232bSRandall Stewart SCTP_FREE(oldstrm, SCTP_M_STRMI); 3889ea44232bSRandall Stewart /* update the size */ 3890ea44232bSRandall Stewart stcb->asoc.streamincnt = num_stream; 3891f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 3892c4e848b7SRandall Stewart sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); 3893ea44232bSRandall Stewart } 3894c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3895c4e848b7SRandall Stewart asoc->str_reset_seq_in++; 38968aae9493SRandall Stewart } else if ((asoc->str_reset_seq_in - 1) == seq) { 38978aae9493SRandall Stewart /* 38988aae9493SRandall Stewart * one seq back, just echo back last action since my 38998aae9493SRandall Stewart * response was lost. 39008aae9493SRandall Stewart */ 39018aae9493SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 39028aae9493SRandall Stewart } else if ((asoc->str_reset_seq_in - 2) == seq) { 39038aae9493SRandall Stewart /* 39048aae9493SRandall Stewart * two seq back, just echo back last action since my 39058aae9493SRandall Stewart * response was lost. 39068aae9493SRandall Stewart */ 39078aae9493SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 39088aae9493SRandall Stewart } else { 3909f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 39108aae9493SRandall Stewart } 3911ea44232bSRandall Stewart } 3912ea44232bSRandall Stewart 3913c4e848b7SRandall Stewart static void 3914c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, 3915c4e848b7SRandall Stewart struct sctp_stream_reset_add_strm *str_add) 3916c4e848b7SRandall Stewart { 3917c4e848b7SRandall Stewart /* 3918c4e848b7SRandall Stewart * Peer is requesting to add more streams. If its within our 3919c4e848b7SRandall Stewart * max-streams we will allow it. 3920c4e848b7SRandall Stewart */ 3921c4e848b7SRandall Stewart uint16_t num_stream; 3922c4e848b7SRandall Stewart uint32_t seq; 3923c4e848b7SRandall Stewart struct sctp_association *asoc = &stcb->asoc; 3924c4e848b7SRandall Stewart 3925c4e848b7SRandall Stewart /* Get the number. */ 3926c4e848b7SRandall Stewart seq = ntohl(str_add->request_seq); 3927c4e848b7SRandall Stewart num_stream = ntohs(str_add->number_of_streams); 3928c4e848b7SRandall Stewart /* Now what would be the new total? */ 3929c4e848b7SRandall Stewart if (asoc->str_reset_seq_in == seq) { 3930c4e848b7SRandall Stewart stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; 3931f3ebe64cSMichael Tuexen if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { 3932f3ebe64cSMichael Tuexen asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3933f3ebe64cSMichael Tuexen } else if (stcb->asoc.stream_reset_outstanding) { 3934f3ebe64cSMichael Tuexen /* We must reject it we have something pending */ 3935f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; 3936c4e848b7SRandall Stewart } else { 3937c4e848b7SRandall Stewart /* Ok, we can do that :-) */ 3938c4e848b7SRandall Stewart int mychk; 3939c4e848b7SRandall Stewart 3940c4e848b7SRandall Stewart mychk = stcb->asoc.streamoutcnt; 3941c4e848b7SRandall Stewart mychk += num_stream; 3942c4e848b7SRandall Stewart if (mychk < 0x10000) { 3943f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; 39447cca1775SRandall Stewart if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) { 3945f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3946c4e848b7SRandall Stewart } 3947c4e848b7SRandall Stewart } else { 3948f3ebe64cSMichael Tuexen stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; 3949c4e848b7SRandall Stewart } 3950c4e848b7SRandall Stewart } 3951c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); 3952c4e848b7SRandall Stewart asoc->str_reset_seq_in++; 3953c4e848b7SRandall Stewart } else if ((asoc->str_reset_seq_in - 1) == seq) { 3954c4e848b7SRandall Stewart /* 3955c4e848b7SRandall Stewart * one seq back, just echo back last action since my 3956c4e848b7SRandall Stewart * response was lost. 3957c4e848b7SRandall Stewart */ 3958c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); 3959c4e848b7SRandall Stewart } else if ((asoc->str_reset_seq_in - 2) == seq) { 3960c4e848b7SRandall Stewart /* 3961c4e848b7SRandall Stewart * two seq back, just echo back last action since my 3962c4e848b7SRandall Stewart * response was lost. 3963c4e848b7SRandall Stewart */ 3964c4e848b7SRandall Stewart sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); 3965c4e848b7SRandall Stewart } else { 3966f3ebe64cSMichael Tuexen sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); 3967c4e848b7SRandall Stewart } 3968c4e848b7SRandall Stewart } 3969c4e848b7SRandall Stewart 3970671d309cSRandall Stewart #ifdef __GNUC__ 3971671d309cSRandall Stewart __attribute__((noinline)) 3972671d309cSRandall Stewart #endif 3973f8829a4aSRandall Stewart static int 3974671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, 3975a169d6ecSMichael Tuexen struct sctp_chunkhdr *ch_req) 3976f8829a4aSRandall Stewart { 39776a58f0e9SXin LI uint16_t remaining_length, param_len, ptype; 3978671d309cSRandall Stewart struct sctp_paramhdr pstore; 3979671d309cSRandall Stewart uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; 3980c4e848b7SRandall Stewart uint32_t seq = 0; 3981f8829a4aSRandall Stewart int num_req = 0; 3982671d309cSRandall Stewart int trunc = 0; 3983f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 3984f8829a4aSRandall Stewart struct sctp_chunkhdr *ch; 3985f8829a4aSRandall Stewart struct sctp_paramhdr *ph; 3986f42a358aSRandall Stewart int ret_code = 0; 3987f42a358aSRandall Stewart int num_param = 0; 3988f8829a4aSRandall Stewart 3989f8829a4aSRandall Stewart /* now it may be a reset or a reset-response */ 39906a58f0e9SXin LI remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr); 3991f8829a4aSRandall Stewart 3992f8829a4aSRandall Stewart /* setup for adding the response */ 3993f8829a4aSRandall Stewart sctp_alloc_a_chunk(stcb, chk); 3994f8829a4aSRandall Stewart if (chk == NULL) { 3995f8829a4aSRandall Stewart return (ret_code); 3996f8829a4aSRandall Stewart } 3997e03159eaSMichael Tuexen chk->copy_by_ref = 0; 3998f8829a4aSRandall Stewart chk->rec.chunk_id.id = SCTP_STREAM_RESET; 3999d06c82f1SRandall Stewart chk->rec.chunk_id.can_take_data = 0; 4000e03159eaSMichael Tuexen chk->flags = 0; 4001f8829a4aSRandall Stewart chk->asoc = &stcb->asoc; 4002f8829a4aSRandall Stewart chk->no_fr_allowed = 0; 4003f8829a4aSRandall Stewart chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); 400444b7479bSRandall Stewart chk->book_size_scale = 0; 4005eb1b1807SGleb Smirnoff chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); 4006f8829a4aSRandall Stewart if (chk->data == NULL) { 4007f8829a4aSRandall Stewart strres_nochunk: 4008f8829a4aSRandall Stewart if (chk->data) { 4009f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4010f8829a4aSRandall Stewart chk->data = NULL; 4011f8829a4aSRandall Stewart } 4012689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4013f8829a4aSRandall Stewart return (ret_code); 4014f8829a4aSRandall Stewart } 4015139bc87fSRandall Stewart SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 4016f8829a4aSRandall Stewart 4017f8829a4aSRandall Stewart /* setup chunk parameters */ 4018f8829a4aSRandall Stewart chk->sent = SCTP_DATAGRAM_UNSENT; 4019f8829a4aSRandall Stewart chk->snd_count = 0; 4020ca85e948SMichael Tuexen chk->whoTo = NULL; 4021f8829a4aSRandall Stewart 4022f8829a4aSRandall Stewart ch = mtod(chk->data, struct sctp_chunkhdr *); 4023f8829a4aSRandall Stewart ch->chunk_type = SCTP_STREAM_RESET; 4024f8829a4aSRandall Stewart ch->chunk_flags = 0; 4025f8829a4aSRandall Stewart ch->chunk_length = htons(chk->send_size); 4026139bc87fSRandall Stewart SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 4027671d309cSRandall Stewart offset += sizeof(struct sctp_chunkhdr); 40286a58f0e9SXin LI while (remaining_length >= sizeof(struct sctp_paramhdr)) { 4029671d309cSRandall Stewart ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore); 40306a58f0e9SXin LI if (ph == NULL) { 40316a58f0e9SXin LI /* TSNH */ 4032f8829a4aSRandall Stewart break; 4033f8829a4aSRandall Stewart } 40346a58f0e9SXin LI param_len = ntohs(ph->param_length); 40356a58f0e9SXin LI if ((param_len > remaining_length) || 40366a58f0e9SXin LI (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) { 40376a58f0e9SXin LI /* bad parameter length */ 40386a58f0e9SXin LI break; 40396a58f0e9SXin LI } 40406a58f0e9SXin LI ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)), 4041671d309cSRandall Stewart (uint8_t *)&cstore); 40426a58f0e9SXin LI if (ph == NULL) { 40436a58f0e9SXin LI /* TSNH */ 40446a58f0e9SXin LI break; 40456a58f0e9SXin LI } 4046f8829a4aSRandall Stewart ptype = ntohs(ph->param_type); 4047f8829a4aSRandall Stewart num_param++; 40486a58f0e9SXin LI if (param_len > sizeof(cstore)) { 4049671d309cSRandall Stewart trunc = 1; 4050671d309cSRandall Stewart } else { 4051671d309cSRandall Stewart trunc = 0; 4052671d309cSRandall Stewart } 4053f8829a4aSRandall Stewart if (num_param > SCTP_MAX_RESET_PARAMS) { 4054f8829a4aSRandall Stewart /* hit the max of parameters already sorry.. */ 4055f8829a4aSRandall Stewart break; 4056f8829a4aSRandall Stewart } 4057f8829a4aSRandall Stewart if (ptype == SCTP_STR_RESET_OUT_REQUEST) { 4058f8829a4aSRandall Stewart struct sctp_stream_reset_out_request *req_out; 4059f8829a4aSRandall Stewart 40606a58f0e9SXin LI if (param_len < sizeof(struct sctp_stream_reset_out_request)) { 40616a58f0e9SXin LI break; 40626a58f0e9SXin LI } 4063f8829a4aSRandall Stewart req_out = (struct sctp_stream_reset_out_request *)ph; 4064f8829a4aSRandall Stewart num_req++; 4065f8829a4aSRandall Stewart if (stcb->asoc.stream_reset_outstanding) { 4066f8829a4aSRandall Stewart seq = ntohl(req_out->response_seq); 4067f8829a4aSRandall Stewart if (seq == stcb->asoc.str_reset_seq_out) { 4068f8829a4aSRandall Stewart /* implicit ack */ 4069f3ebe64cSMichael Tuexen (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); 4070f8829a4aSRandall Stewart } 4071f8829a4aSRandall Stewart } 4072671d309cSRandall Stewart sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); 4073c4e848b7SRandall Stewart } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { 4074ea44232bSRandall Stewart struct sctp_stream_reset_add_strm *str_add; 4075ea44232bSRandall Stewart 40766a58f0e9SXin LI if (param_len < sizeof(struct sctp_stream_reset_add_strm)) { 40776a58f0e9SXin LI break; 40786a58f0e9SXin LI } 4079ea44232bSRandall Stewart str_add = (struct sctp_stream_reset_add_strm *)ph; 4080ea44232bSRandall Stewart num_req++; 4081ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(stcb, chk, str_add); 4082c4e848b7SRandall Stewart } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { 4083c4e848b7SRandall Stewart struct sctp_stream_reset_add_strm *str_add; 4084c4e848b7SRandall Stewart 40856a58f0e9SXin LI if (param_len < sizeof(struct sctp_stream_reset_add_strm)) { 40866a58f0e9SXin LI break; 40876a58f0e9SXin LI } 4088c4e848b7SRandall Stewart str_add = (struct sctp_stream_reset_add_strm *)ph; 4089c4e848b7SRandall Stewart num_req++; 4090c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); 4091f8829a4aSRandall Stewart } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { 4092f8829a4aSRandall Stewart struct sctp_stream_reset_in_request *req_in; 4093f8829a4aSRandall Stewart 4094f8829a4aSRandall Stewart num_req++; 4095f8829a4aSRandall Stewart req_in = (struct sctp_stream_reset_in_request *)ph; 4096671d309cSRandall Stewart sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); 4097f8829a4aSRandall Stewart } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { 4098f8829a4aSRandall Stewart struct sctp_stream_reset_tsn_request *req_tsn; 4099f8829a4aSRandall Stewart 4100f8829a4aSRandall Stewart num_req++; 4101f8829a4aSRandall Stewart req_tsn = (struct sctp_stream_reset_tsn_request *)ph; 4102f8829a4aSRandall Stewart if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { 4103f8829a4aSRandall Stewart ret_code = 1; 4104f8829a4aSRandall Stewart goto strres_nochunk; 4105f8829a4aSRandall Stewart } 4106f8829a4aSRandall Stewart /* no more */ 4107f8829a4aSRandall Stewart break; 4108f8829a4aSRandall Stewart } else if (ptype == SCTP_STR_RESET_RESPONSE) { 4109f8829a4aSRandall Stewart struct sctp_stream_reset_response *resp; 4110f8829a4aSRandall Stewart uint32_t result; 4111f8829a4aSRandall Stewart 41126a58f0e9SXin LI if (param_len < sizeof(struct sctp_stream_reset_response)) { 41136a58f0e9SXin LI break; 41146a58f0e9SXin LI } 4115f8829a4aSRandall Stewart resp = (struct sctp_stream_reset_response *)ph; 4116f8829a4aSRandall Stewart seq = ntohl(resp->response_seq); 4117f8829a4aSRandall Stewart result = ntohl(resp->result); 4118f8829a4aSRandall Stewart if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { 4119f8829a4aSRandall Stewart ret_code = 1; 4120f8829a4aSRandall Stewart goto strres_nochunk; 4121f8829a4aSRandall Stewart } 4122f8829a4aSRandall Stewart } else { 4123f8829a4aSRandall Stewart break; 4124f8829a4aSRandall Stewart } 4125671d309cSRandall Stewart offset += SCTP_SIZE32(param_len); 41266a58f0e9SXin LI if (remaining_length >= SCTP_SIZE32(param_len)) { 41276a58f0e9SXin LI remaining_length -= SCTP_SIZE32(param_len); 41286a58f0e9SXin LI } else { 41296a58f0e9SXin LI remaining_length = 0; 41306a58f0e9SXin LI } 4131f8829a4aSRandall Stewart } 4132f8829a4aSRandall Stewart if (num_req == 0) { 4133f8829a4aSRandall Stewart /* we have no response free the stuff */ 4134f8829a4aSRandall Stewart goto strres_nochunk; 4135f8829a4aSRandall Stewart } 4136f8829a4aSRandall Stewart /* ok we have a chunk to link in */ 4137f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, 4138f8829a4aSRandall Stewart chk, 4139f8829a4aSRandall Stewart sctp_next); 4140f8829a4aSRandall Stewart stcb->asoc.ctrl_queue_cnt++; 4141f8829a4aSRandall Stewart return (ret_code); 4142f8829a4aSRandall Stewart } 4143f8829a4aSRandall Stewart 4144f8829a4aSRandall Stewart /* 4145f8829a4aSRandall Stewart * Handle a router or endpoints report of a packet loss, there are two ways 4146f8829a4aSRandall Stewart * to handle this, either we get the whole packet and must disect it 4147f8829a4aSRandall Stewart * ourselves (possibly with truncation and or corruption) or it is a summary 4148f8829a4aSRandall Stewart * from a middle box that did the disectting for us. 4149f8829a4aSRandall Stewart */ 4150f8829a4aSRandall Stewart static void 4151f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, 4152458303daSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) 4153f8829a4aSRandall Stewart { 4154f8829a4aSRandall Stewart struct sctp_chunk_desc desc; 415532df1c9eSMichael Tuexen struct sctp_chunkhdr *chk_hdr; 415632df1c9eSMichael Tuexen struct sctp_data_chunk *data_chunk; 415732df1c9eSMichael Tuexen struct sctp_idata_chunk *idata_chunk; 415832df1c9eSMichael Tuexen uint32_t bottle_bw, on_queue; 415932df1c9eSMichael Tuexen uint32_t offset, chk_len; 416032df1c9eSMichael Tuexen uint16_t pktdrp_len; 416132df1c9eSMichael Tuexen uint8_t pktdrp_flags; 4162f8829a4aSRandall Stewart 416332df1c9eSMichael Tuexen KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit, 416432df1c9eSMichael Tuexen ("PKTDROP chunk too small")); 416532df1c9eSMichael Tuexen pktdrp_flags = cp->ch.chunk_flags; 416632df1c9eSMichael Tuexen pktdrp_len = ntohs(cp->ch.chunk_length); 416732df1c9eSMichael Tuexen KASSERT(limit <= pktdrp_len, ("Inconsistent limit")); 416832df1c9eSMichael Tuexen if (pktdrp_flags & SCTP_PACKET_TRUNCATED) { 41696176f9d6SMichael Tuexen if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) { 417032df1c9eSMichael Tuexen /* The peer plays games with us. */ 417132df1c9eSMichael Tuexen return; 417232df1c9eSMichael Tuexen } 417332df1c9eSMichael Tuexen } 417432df1c9eSMichael Tuexen limit -= sizeof(struct sctp_pktdrop_chunk); 417532df1c9eSMichael Tuexen offset = 0; 417632df1c9eSMichael Tuexen if (offset == limit) { 417732df1c9eSMichael Tuexen if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) { 4178f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpbwrpt); 417932df1c9eSMichael Tuexen } 418032df1c9eSMichael Tuexen } else if (offset + sizeof(struct sctphdr) > limit) { 418132df1c9eSMichael Tuexen /* Only a partial SCTP common header. */ 418232df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpcrupt); 418332df1c9eSMichael Tuexen offset = limit; 4184f8829a4aSRandall Stewart } else { 418532df1c9eSMichael Tuexen /* XXX: Check embedded SCTP common header. */ 418632df1c9eSMichael Tuexen offset += sizeof(struct sctphdr); 4187f8829a4aSRandall Stewart } 418832df1c9eSMichael Tuexen /* Now parse through the chunks themselves. */ 418932df1c9eSMichael Tuexen while (offset < limit) { 419032df1c9eSMichael Tuexen if (offset + sizeof(struct sctp_chunkhdr) > limit) { 419132df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpcrupt); 419232df1c9eSMichael Tuexen break; 4193458303daSRandall Stewart } 419432df1c9eSMichael Tuexen chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset); 419532df1c9eSMichael Tuexen desc.chunk_type = chk_hdr->chunk_type; 4196f8829a4aSRandall Stewart /* get amount we need to move */ 419732df1c9eSMichael Tuexen chk_len = (uint32_t)ntohs(chk_hdr->chunk_length); 419832df1c9eSMichael Tuexen if (chk_len < sizeof(struct sctp_chunkhdr)) { 419932df1c9eSMichael Tuexen /* Someone is lying... */ 4200f8829a4aSRandall Stewart break; 4201f8829a4aSRandall Stewart } 4202f8829a4aSRandall Stewart if (desc.chunk_type == SCTP_DATA) { 420332df1c9eSMichael Tuexen if (stcb->asoc.idata_supported) { 420432df1c9eSMichael Tuexen /* Some is playing games with us. */ 420532df1c9eSMichael Tuexen break; 4206f8829a4aSRandall Stewart } 420732df1c9eSMichael Tuexen if (chk_len <= sizeof(struct sctp_data_chunk)) { 420832df1c9eSMichael Tuexen /* Some is playing games with us. */ 420932df1c9eSMichael Tuexen break; 421032df1c9eSMichael Tuexen } 421132df1c9eSMichael Tuexen if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) { 421232df1c9eSMichael Tuexen /* 421332df1c9eSMichael Tuexen * Not enough data bytes available in the 421432df1c9eSMichael Tuexen * chunk. 421532df1c9eSMichael Tuexen */ 4216f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpnedat); 421732df1c9eSMichael Tuexen goto next_chunk; 421832df1c9eSMichael Tuexen } 421932df1c9eSMichael Tuexen if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) { 422032df1c9eSMichael Tuexen /* Not enough data in buffer. */ 4221f8829a4aSRandall Stewart break; 4222f8829a4aSRandall Stewart } 422332df1c9eSMichael Tuexen data_chunk = (struct sctp_data_chunk *)(cp->data + offset); 422432df1c9eSMichael Tuexen memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY); 422532df1c9eSMichael Tuexen desc.tsn_ifany = data_chunk->dp.tsn; 422632df1c9eSMichael Tuexen if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) { 422732df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpmbda); 422832df1c9eSMichael Tuexen } 422932df1c9eSMichael Tuexen } else if (desc.chunk_type == SCTP_IDATA) { 423032df1c9eSMichael Tuexen if (!stcb->asoc.idata_supported) { 423132df1c9eSMichael Tuexen /* Some is playing games with us. */ 423232df1c9eSMichael Tuexen break; 423332df1c9eSMichael Tuexen } 423432df1c9eSMichael Tuexen if (chk_len <= sizeof(struct sctp_idata_chunk)) { 423532df1c9eSMichael Tuexen /* Some is playing games with us. */ 423632df1c9eSMichael Tuexen break; 423732df1c9eSMichael Tuexen } 423832df1c9eSMichael Tuexen if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) { 423932df1c9eSMichael Tuexen /* 424032df1c9eSMichael Tuexen * Not enough data bytes available in the 424132df1c9eSMichael Tuexen * chunk. 424232df1c9eSMichael Tuexen */ 424332df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpnedat); 424432df1c9eSMichael Tuexen goto next_chunk; 424532df1c9eSMichael Tuexen } 424632df1c9eSMichael Tuexen if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) { 424732df1c9eSMichael Tuexen /* Not enough data in buffer. */ 424832df1c9eSMichael Tuexen break; 424932df1c9eSMichael Tuexen } 425032df1c9eSMichael Tuexen idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset); 425132df1c9eSMichael Tuexen memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY); 425232df1c9eSMichael Tuexen desc.tsn_ifany = idata_chunk->dp.tsn; 425332df1c9eSMichael Tuexen if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) { 425432df1c9eSMichael Tuexen SCTP_STAT_INCR(sctps_pdrpmbda); 425532df1c9eSMichael Tuexen } 4256f8829a4aSRandall Stewart } else { 425732df1c9eSMichael Tuexen if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) { 4258f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpmbct); 4259f8829a4aSRandall Stewart } 426032df1c9eSMichael Tuexen } 426132df1c9eSMichael Tuexen if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) { 4262f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrppdbrk); 4263f8829a4aSRandall Stewart break; 4264f8829a4aSRandall Stewart } 426532df1c9eSMichael Tuexen next_chunk: 426632df1c9eSMichael Tuexen offset += SCTP_SIZE32(chk_len); 4267f8829a4aSRandall Stewart } 4268f8829a4aSRandall Stewart /* Now update any rwnd --- possibly */ 426932df1c9eSMichael Tuexen if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) { 4270f8829a4aSRandall Stewart /* From a peer, we get a rwnd report */ 4271f8829a4aSRandall Stewart uint32_t a_rwnd; 4272f8829a4aSRandall Stewart 4273f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpfehos); 4274f8829a4aSRandall Stewart 4275f8829a4aSRandall Stewart bottle_bw = ntohl(cp->bottle_bw); 4276f8829a4aSRandall Stewart on_queue = ntohl(cp->current_onq); 4277f8829a4aSRandall Stewart if (bottle_bw && on_queue) { 4278f8829a4aSRandall Stewart /* a rwnd report is in here */ 4279f8829a4aSRandall Stewart if (bottle_bw > on_queue) 4280f8829a4aSRandall Stewart a_rwnd = bottle_bw - on_queue; 4281f8829a4aSRandall Stewart else 4282f8829a4aSRandall Stewart a_rwnd = 0; 4283f8829a4aSRandall Stewart 4284f8829a4aSRandall Stewart if (a_rwnd == 0) 4285f8829a4aSRandall Stewart stcb->asoc.peers_rwnd = 0; 4286f8829a4aSRandall Stewart else { 4287f8829a4aSRandall Stewart if (a_rwnd > stcb->asoc.total_flight) { 4288f8829a4aSRandall Stewart stcb->asoc.peers_rwnd = 4289f8829a4aSRandall Stewart a_rwnd - stcb->asoc.total_flight; 4290f8829a4aSRandall Stewart } else { 4291f8829a4aSRandall Stewart stcb->asoc.peers_rwnd = 0; 4292f8829a4aSRandall Stewart } 4293f8829a4aSRandall Stewart if (stcb->asoc.peers_rwnd < 4294f8829a4aSRandall Stewart stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4295f8829a4aSRandall Stewart /* SWS sender side engages */ 4296f8829a4aSRandall Stewart stcb->asoc.peers_rwnd = 0; 4297f8829a4aSRandall Stewart } 4298f8829a4aSRandall Stewart } 4299f8829a4aSRandall Stewart } 4300f8829a4aSRandall Stewart } else { 4301f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_pdrpfmbox); 4302f8829a4aSRandall Stewart } 4303f8829a4aSRandall Stewart 4304f8829a4aSRandall Stewart /* now middle boxes in sat networks get a cwnd bump */ 430532df1c9eSMichael Tuexen if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) && 4306f8829a4aSRandall Stewart (stcb->asoc.sat_t3_loss_recovery == 0) && 4307f8829a4aSRandall Stewart (stcb->asoc.sat_network)) { 4308f8829a4aSRandall Stewart /* 4309cd0a4ff6SPedro F. Giffuni * This is debatable but for sat networks it makes sense 4310f8829a4aSRandall Stewart * Note if a T3 timer has went off, we will prohibit any 4311f8829a4aSRandall Stewart * changes to cwnd until we exit the t3 loss recovery. 4312f8829a4aSRandall Stewart */ 4313b54d3a6cSRandall Stewart stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, 4314b54d3a6cSRandall Stewart net, cp, &bottle_bw, &on_queue); 4315f8829a4aSRandall Stewart } 4316f8829a4aSRandall Stewart } 4317f8829a4aSRandall Stewart 4318f8829a4aSRandall Stewart /* 4319f8829a4aSRandall Stewart * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to 4320f8829a4aSRandall Stewart * still contain IP/SCTP header - stcb: is the tcb found for this packet - 4321f8829a4aSRandall Stewart * offset: offset into the mbuf chain to first chunkhdr - length: is the 4322f8829a4aSRandall Stewart * length of the complete packet outputs: - length: modified to remaining 4323f8829a4aSRandall Stewart * length after control processing - netp: modified to new sctp_nets after 4324f8829a4aSRandall Stewart * cookie-echo processing - return NULL to discard the packet (ie. no asoc, 4325f8829a4aSRandall Stewart * bad packet,...) otherwise return the tcb for this packet 4326f8829a4aSRandall Stewart */ 43273c6f3536SRandall Stewart #ifdef __GNUC__ 43283c6f3536SRandall Stewart __attribute__((noinline)) 43293c6f3536SRandall Stewart #endif 4330f8829a4aSRandall Stewart static struct sctp_tcb * 4331f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, 4332b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 4333f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, 433417205eccSRandall Stewart struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, 4335d089f9b9SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, 4336c54a18d2SRandall Stewart uint32_t vrf_id, uint16_t port) 4337f8829a4aSRandall Stewart { 4338f8829a4aSRandall Stewart struct sctp_association *asoc; 4339ff1ffd74SMichael Tuexen struct mbuf *op_err; 4340ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 4341f8829a4aSRandall Stewart uint32_t vtag_in; 4342f8829a4aSRandall Stewart int num_chunks = 0; /* number of control chunks processed */ 4343d0f6ab79SMichael Tuexen uint32_t chk_length, contiguous; 4344f8829a4aSRandall Stewart int ret; 4345bff64a4dSRandall Stewart int abort_no_unlock = 0; 4346899288aeSRandall Stewart int ecne_seen = 0; 43479de7354bSMichael Tuexen int abort_flag; 4348f8829a4aSRandall Stewart 4349f8829a4aSRandall Stewart /* 4350f8829a4aSRandall Stewart * How big should this be, and should it be alloc'd? Lets try the 4351f8829a4aSRandall Stewart * d-mtu-ceiling for now (2k) and that should hopefully work ... 4352f8829a4aSRandall Stewart * until we get into jumbo grams and such.. 4353f8829a4aSRandall Stewart */ 4354f42a358aSRandall Stewart uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; 4355f8829a4aSRandall Stewart int got_auth = 0; 4356f8829a4aSRandall Stewart uint32_t auth_offset = 0, auth_len = 0; 4357f8829a4aSRandall Stewart int auth_skipped = 0; 43582afb3e84SRandall Stewart int asconf_cnt = 0; 4359ceaad40aSRandall Stewart 4360ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", 4361dd294dceSMichael Tuexen iphlen, *offset, length, (void *)stcb); 4362f8829a4aSRandall Stewart 436380a2d140SMichael Tuexen if (stcb) { 436480a2d140SMichael Tuexen SCTP_TCB_LOCK_ASSERT(stcb); 436580a2d140SMichael Tuexen } 4366f8829a4aSRandall Stewart /* validate chunk header length... */ 4367f8829a4aSRandall Stewart if (ntohs(ch->chunk_length) < sizeof(*ch)) { 4368d61a0ae0SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", 4369d61a0ae0SRandall Stewart ntohs(ch->chunk_length)); 43703ed8d364SMichael Tuexen *offset = length; 437180a2d140SMichael Tuexen return (stcb); 4372f8829a4aSRandall Stewart } 4373f8829a4aSRandall Stewart /* 4374f8829a4aSRandall Stewart * validate the verification tag 4375f8829a4aSRandall Stewart */ 4376f8829a4aSRandall Stewart vtag_in = ntohl(sh->v_tag); 4377f8829a4aSRandall Stewart 4378f8829a4aSRandall Stewart if (ch->chunk_type == SCTP_INITIATION) { 4379d61a0ae0SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", 4380d61a0ae0SRandall Stewart ntohs(ch->chunk_length), vtag_in); 4381f8829a4aSRandall Stewart if (vtag_in != 0) { 4382f8829a4aSRandall Stewart /* protocol error- silently discard... */ 4383f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_badvtag); 438480a2d140SMichael Tuexen if (stcb != NULL) { 438580a2d140SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 43866e55db54SRandall Stewart } 4387f8829a4aSRandall Stewart return (NULL); 4388f8829a4aSRandall Stewart } 4389f8829a4aSRandall Stewart } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { 4390f8829a4aSRandall Stewart /* 4391f8829a4aSRandall Stewart * If there is no stcb, skip the AUTH chunk and process 4392f8829a4aSRandall Stewart * later after a stcb is found (to validate the lookup was 4393f8829a4aSRandall Stewart * valid. 4394f8829a4aSRandall Stewart */ 4395f8829a4aSRandall Stewart if ((ch->chunk_type == SCTP_AUTHENTICATION) && 4396b3f1ea41SRandall Stewart (stcb == NULL) && 4397c79bec9cSMichael Tuexen (inp->auth_supported == 1)) { 4398f8829a4aSRandall Stewart /* save this chunk for later processing */ 4399f8829a4aSRandall Stewart auth_skipped = 1; 4400f8829a4aSRandall Stewart auth_offset = *offset; 4401f8829a4aSRandall Stewart auth_len = ntohs(ch->chunk_length); 4402f8829a4aSRandall Stewart 4403f8829a4aSRandall Stewart /* (temporarily) move past this chunk */ 4404f8829a4aSRandall Stewart *offset += SCTP_SIZE32(auth_len); 4405f8829a4aSRandall Stewart if (*offset >= length) { 4406f8829a4aSRandall Stewart /* no more data left in the mbuf chain */ 4407f8829a4aSRandall Stewart *offset = length; 4408f8829a4aSRandall Stewart return (NULL); 4409f8829a4aSRandall Stewart } 4410f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4411f8829a4aSRandall Stewart sizeof(struct sctp_chunkhdr), chunk_buf); 4412f8829a4aSRandall Stewart } 4413ad81507eSRandall Stewart if (ch == NULL) { 4414ad81507eSRandall Stewart /* Help */ 4415ad81507eSRandall Stewart *offset = length; 441680a2d140SMichael Tuexen return (stcb); 4417ad81507eSRandall Stewart } 4418f8829a4aSRandall Stewart if (ch->chunk_type == SCTP_COOKIE_ECHO) { 4419f8829a4aSRandall Stewart goto process_control_chunks; 4420f8829a4aSRandall Stewart } 4421f8829a4aSRandall Stewart /* 4422f8829a4aSRandall Stewart * first check if it's an ASCONF with an unknown src addr we 4423f8829a4aSRandall Stewart * need to look inside to find the association 4424f8829a4aSRandall Stewart */ 4425f8829a4aSRandall Stewart if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { 44262afb3e84SRandall Stewart struct sctp_chunkhdr *asconf_ch = ch; 44272afb3e84SRandall Stewart uint32_t asconf_offset = 0, asconf_len = 0; 44282afb3e84SRandall Stewart 4429f8829a4aSRandall Stewart /* inp's refcount may be reduced */ 4430f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp); 4431f8829a4aSRandall Stewart 44322afb3e84SRandall Stewart asconf_offset = *offset; 44332afb3e84SRandall Stewart do { 44342afb3e84SRandall Stewart asconf_len = ntohs(asconf_ch->chunk_length); 44352afb3e84SRandall Stewart if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) 44362afb3e84SRandall Stewart break; 44377215cc1bSMichael Tuexen stcb = sctp_findassociation_ep_asconf(m, 4438b1754ad1SMichael Tuexen *offset, 4439b1754ad1SMichael Tuexen dst, 4440b1754ad1SMichael Tuexen sh, &inp, netp, vrf_id); 44412afb3e84SRandall Stewart if (stcb != NULL) 44422afb3e84SRandall Stewart break; 44432afb3e84SRandall Stewart asconf_offset += SCTP_SIZE32(asconf_len); 44442afb3e84SRandall Stewart asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, 44452afb3e84SRandall Stewart sizeof(struct sctp_chunkhdr), chunk_buf); 44462afb3e84SRandall Stewart } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); 4447f8829a4aSRandall Stewart if (stcb == NULL) { 4448f8829a4aSRandall Stewart /* 4449f8829a4aSRandall Stewart * reduce inp's refcount if not reduced in 4450f8829a4aSRandall Stewart * sctp_findassociation_ep_asconf(). 4451f8829a4aSRandall Stewart */ 4452f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 4453f8829a4aSRandall Stewart } 44540053ed28SMichael Tuexen 4455f8829a4aSRandall Stewart /* now go back and verify any auth chunk to be sure */ 4456f8829a4aSRandall Stewart if (auth_skipped && (stcb != NULL)) { 4457f8829a4aSRandall Stewart struct sctp_auth_chunk *auth; 4458f8829a4aSRandall Stewart 44598262311cSMichael Tuexen if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) { 44608262311cSMichael Tuexen auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf); 4461f8829a4aSRandall Stewart got_auth = 1; 4462f8829a4aSRandall Stewart auth_skipped = 0; 44638262311cSMichael Tuexen } else { 44648262311cSMichael Tuexen auth = NULL; 44658262311cSMichael Tuexen } 4466ad81507eSRandall Stewart if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, 4467f8829a4aSRandall Stewart auth_offset)) { 4468f8829a4aSRandall Stewart /* auth HMAC failed so dump it */ 4469f8829a4aSRandall Stewart *offset = length; 447080a2d140SMichael Tuexen return (stcb); 4471f8829a4aSRandall Stewart } else { 4472f8829a4aSRandall Stewart /* remaining chunks are HMAC checked */ 4473f8829a4aSRandall Stewart stcb->asoc.authenticated = 1; 4474f8829a4aSRandall Stewart } 4475f8829a4aSRandall Stewart } 4476f8829a4aSRandall Stewart } 4477f8829a4aSRandall Stewart if (stcb == NULL) { 4478999f86d6SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__); 4479ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 4480ff1ffd74SMichael Tuexen msg); 4481f8829a4aSRandall Stewart /* no association, so it's out of the blue... */ 4482ff1ffd74SMichael Tuexen sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err, 4483d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 4484c54a18d2SRandall Stewart vrf_id, port); 4485f8829a4aSRandall Stewart *offset = length; 4486f8829a4aSRandall Stewart return (NULL); 4487f8829a4aSRandall Stewart } 4488f8829a4aSRandall Stewart asoc = &stcb->asoc; 4489f8829a4aSRandall Stewart /* ABORT and SHUTDOWN can use either v_tag... */ 4490f8829a4aSRandall Stewart if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || 4491f8829a4aSRandall Stewart (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || 4492f8829a4aSRandall Stewart (ch->chunk_type == SCTP_PACKET_DROPPED)) { 44935db47b3dSMichael Tuexen /* Take the T-bit always into account. */ 44945db47b3dSMichael Tuexen if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) && 44955db47b3dSMichael Tuexen (vtag_in == asoc->my_vtag)) || 44965db47b3dSMichael Tuexen (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) && 4497ff76c8c9SMichael Tuexen (asoc->peer_vtag != htonl(0)) && 4498f8829a4aSRandall Stewart (vtag_in == asoc->peer_vtag))) { 4499f8829a4aSRandall Stewart /* this is valid */ 4500f8829a4aSRandall Stewart } else { 4501f8829a4aSRandall Stewart /* drop this packet... */ 4502f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_badvtag); 450380a2d140SMichael Tuexen if (stcb != NULL) { 450480a2d140SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 45056e55db54SRandall Stewart } 4506f8829a4aSRandall Stewart return (NULL); 4507f8829a4aSRandall Stewart } 4508f8829a4aSRandall Stewart } else { 4509f8829a4aSRandall Stewart /* for all other chunks, vtag must match */ 4510f8829a4aSRandall Stewart if (vtag_in != asoc->my_vtag) { 4511f8829a4aSRandall Stewart /* invalid vtag... */ 4512ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, 4513ad81507eSRandall Stewart "invalid vtag: %xh, expect %xh\n", 4514ad81507eSRandall Stewart vtag_in, asoc->my_vtag); 4515f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_badvtag); 451680a2d140SMichael Tuexen if (stcb != NULL) { 451780a2d140SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 45186e55db54SRandall Stewart } 4519f8829a4aSRandall Stewart *offset = length; 4520f8829a4aSRandall Stewart return (NULL); 4521f8829a4aSRandall Stewart } 4522f8829a4aSRandall Stewart } 4523b7b84c0eSMichael Tuexen } /* end if !SCTP_COOKIE_ECHO */ 4524b7b84c0eSMichael Tuexen /* 4525b7b84c0eSMichael Tuexen * process all control chunks... 4526b7b84c0eSMichael Tuexen */ 4527f8829a4aSRandall Stewart if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || 4528830d754dSRandall Stewart (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || 4529f8829a4aSRandall Stewart (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && 4530839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { 4531f8829a4aSRandall Stewart /* implied cookie-ack.. we must have lost the ack */ 4532f8829a4aSRandall Stewart sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, 4533f8829a4aSRandall Stewart *netp); 4534f8829a4aSRandall Stewart } 45350053ed28SMichael Tuexen 4536f8829a4aSRandall Stewart process_control_chunks: 4537f8829a4aSRandall Stewart while (IS_SCTP_CONTROL(ch)) { 4538f8829a4aSRandall Stewart /* validate chunk length */ 4539f8829a4aSRandall Stewart chk_length = ntohs(ch->chunk_length); 4540ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", 4541f8829a4aSRandall Stewart ch->chunk_type, chk_length); 454280fefe0aSRandall Stewart SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); 45434c9179adSRandall Stewart if (chk_length < sizeof(*ch) || 45444c9179adSRandall Stewart (*offset + (int)chk_length) > length) { 4545f8829a4aSRandall Stewart *offset = length; 454680a2d140SMichael Tuexen return (stcb); 4547f8829a4aSRandall Stewart } 4548f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); 4549f8829a4aSRandall Stewart /* 4550d0f6ab79SMichael Tuexen * INIT and INIT-ACK only gets the init ack "header" portion 4551d0f6ab79SMichael Tuexen * only because we don't have to process the peer's COOKIE. 4552d0f6ab79SMichael Tuexen * All others get a complete chunk. 4553f8829a4aSRandall Stewart */ 4554d0f6ab79SMichael Tuexen switch (ch->chunk_type) { 4555d0f6ab79SMichael Tuexen case SCTP_INITIATION: 4556d0f6ab79SMichael Tuexen contiguous = sizeof(struct sctp_init_chunk); 4557d0f6ab79SMichael Tuexen break; 4558d0f6ab79SMichael Tuexen case SCTP_INITIATION_ACK: 4559d0f6ab79SMichael Tuexen contiguous = sizeof(struct sctp_init_ack_chunk); 4560d0f6ab79SMichael Tuexen break; 4561d0f6ab79SMichael Tuexen default: 4562d0f6ab79SMichael Tuexen contiguous = min(chk_length, sizeof(chunk_buf)); 4563d0f6ab79SMichael Tuexen break; 45646e55db54SRandall Stewart } 4565d06c82f1SRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 4566d0f6ab79SMichael Tuexen contiguous, 4567d06c82f1SRandall Stewart chunk_buf); 4568d06c82f1SRandall Stewart if (ch == NULL) { 4569d06c82f1SRandall Stewart *offset = length; 4570c70d1ef1SMichael Tuexen return (stcb); 4571d06c82f1SRandall Stewart } 45720053ed28SMichael Tuexen 4573f8829a4aSRandall Stewart num_chunks++; 4574f8829a4aSRandall Stewart /* Save off the last place we got a control from */ 4575f8829a4aSRandall Stewart if (stcb != NULL) { 4576ad81507eSRandall Stewart if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { 4577f8829a4aSRandall Stewart /* 4578f8829a4aSRandall Stewart * allow last_control to be NULL if 4579f8829a4aSRandall Stewart * ASCONF... ASCONF processing will find the 4580f8829a4aSRandall Stewart * right net later 4581f8829a4aSRandall Stewart */ 4582ad81507eSRandall Stewart if ((netp != NULL) && (*netp != NULL)) 4583f8829a4aSRandall Stewart stcb->asoc.last_control_chunk_from = *netp; 4584f8829a4aSRandall Stewart } 4585f8829a4aSRandall Stewart } 4586f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 4587f8829a4aSRandall Stewart sctp_audit_log(0xB0, ch->chunk_type); 4588f8829a4aSRandall Stewart #endif 4589f8829a4aSRandall Stewart 4590f8829a4aSRandall Stewart /* check to see if this chunk required auth, but isn't */ 4591b3f1ea41SRandall Stewart if ((stcb != NULL) && 4592b3f1ea41SRandall Stewart sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && 4593f8829a4aSRandall Stewart !stcb->asoc.authenticated) { 4594f8829a4aSRandall Stewart /* "silently" ignore */ 4595f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvauthmissing); 4596f8829a4aSRandall Stewart goto next_chunk; 4597f8829a4aSRandall Stewart } 4598f8829a4aSRandall Stewart switch (ch->chunk_type) { 4599f8829a4aSRandall Stewart case SCTP_INITIATION: 4600ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); 4601d68fdc4dSMichael Tuexen /* The INIT chunk must be the only chunk. */ 4602d68fdc4dSMichael Tuexen if ((num_chunks > 1) || 4603ab6174d5SMichael Tuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4604c70d1ef1SMichael Tuexen /* 4605c70d1ef1SMichael Tuexen * RFC 4960bis requires stopping the 4606c70d1ef1SMichael Tuexen * processing of the packet. 4607c70d1ef1SMichael Tuexen */ 4608f8829a4aSRandall Stewart *offset = length; 4609c70d1ef1SMichael Tuexen return (stcb); 4610f8829a4aSRandall Stewart } 4611d68fdc4dSMichael Tuexen /* Honor our resource limit. */ 4612d68fdc4dSMichael Tuexen if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { 4613ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 4614*eba8e643SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err, 4615*eba8e643SMichael Tuexen mflowtype, mflowid, inp->fibnum, 4616f30ac432SMichael Tuexen vrf_id, port); 4617f8829a4aSRandall Stewart *offset = length; 4618*eba8e643SMichael Tuexen if (stcb != NULL) { 4619*eba8e643SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 4620*eba8e643SMichael Tuexen } 4621f8829a4aSRandall Stewart return (NULL); 4622f8829a4aSRandall Stewart } 4623b1754ad1SMichael Tuexen sctp_handle_init(m, iphlen, *offset, src, dst, sh, 4624ad81507eSRandall Stewart (struct sctp_init_chunk *)ch, inp, 4625*eba8e643SMichael Tuexen stcb, *netp, 4626457b4b88SMichael Tuexen mflowtype, mflowid, 4627f30ac432SMichael Tuexen vrf_id, port); 4628f8829a4aSRandall Stewart *offset = length; 4629*eba8e643SMichael Tuexen if (stcb != NULL) { 463080a2d140SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 46316e55db54SRandall Stewart } 4632f8829a4aSRandall Stewart return (NULL); 4633f8829a4aSRandall Stewart break; 46349a972525SRandall Stewart case SCTP_PAD_CHUNK: 46359a972525SRandall Stewart break; 4636f8829a4aSRandall Stewart case SCTP_INITIATION_ACK: 4637469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n"); 4638f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4639f8829a4aSRandall Stewart /* We are not interested anymore */ 464080a2d140SMichael Tuexen if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) { 4641f8829a4aSRandall Stewart ; 4642f8829a4aSRandall Stewart } else { 4643f8829a4aSRandall Stewart *offset = length; 464480a2d140SMichael Tuexen if (stcb != NULL) { 4645b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 4646b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_29); 4647f8829a4aSRandall Stewart } 4648f8829a4aSRandall Stewart return (NULL); 4649f8829a4aSRandall Stewart } 4650f8829a4aSRandall Stewart } 4651ab6174d5SMichael Tuexen /* The INIT-ACK chunk must be the only chunk. */ 4652f8829a4aSRandall Stewart if ((num_chunks > 1) || 4653ab6174d5SMichael Tuexen (length - *offset > (int)SCTP_SIZE32(chk_length))) { 4654f8829a4aSRandall Stewart *offset = length; 465580a2d140SMichael Tuexen return (stcb); 4656f8829a4aSRandall Stewart } 4657469a65d1SMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 4658b1754ad1SMichael Tuexen ret = sctp_handle_init_ack(m, iphlen, *offset, 4659b1754ad1SMichael Tuexen src, dst, sh, 4660f30ac432SMichael Tuexen (struct sctp_init_ack_chunk *)ch, 4661f30ac432SMichael Tuexen stcb, *netp, 4662f30ac432SMichael Tuexen &abort_no_unlock, 4663457b4b88SMichael Tuexen mflowtype, mflowid, 4664f30ac432SMichael Tuexen vrf_id); 4665ad81507eSRandall Stewart } else { 4666ad81507eSRandall Stewart ret = -1; 4667ad81507eSRandall Stewart } 4668d68fdc4dSMichael Tuexen *offset = length; 4669d68fdc4dSMichael Tuexen if (abort_no_unlock) { 4670d68fdc4dSMichael Tuexen return (NULL); 4671d68fdc4dSMichael Tuexen } 4672f8829a4aSRandall Stewart /* 4673f8829a4aSRandall Stewart * Special case, I must call the output routine to 4674f8829a4aSRandall Stewart * get the cookie echoed 4675f8829a4aSRandall Stewart */ 4676d68fdc4dSMichael Tuexen if ((stcb != NULL) && (ret == 0)) { 4677ceaad40aSRandall Stewart sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 4678d68fdc4dSMichael Tuexen } 467980a2d140SMichael Tuexen return (stcb); 4680f8829a4aSRandall Stewart break; 4681f8829a4aSRandall Stewart case SCTP_SELECTIVE_ACK: 4682469a65d1SMichael Tuexen case SCTP_NR_SELECTIVE_ACK: 4683f8829a4aSRandall Stewart { 4684f8829a4aSRandall Stewart int abort_now = 0; 4685f8829a4aSRandall Stewart uint32_t a_rwnd, cum_ack; 4686469a65d1SMichael Tuexen uint16_t num_seg, num_nr_seg, num_dup; 4687cd554309SMichael Tuexen uint8_t flags; 4688cd554309SMichael Tuexen int offset_seg, offset_dup; 4689f8829a4aSRandall Stewart 4690469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n", 4691469a65d1SMichael Tuexen ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK"); 469220083c2eSMichael Tuexen SCTP_STAT_INCR(sctps_recvsacks); 4693cd554309SMichael Tuexen if (stcb == NULL) { 4694469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n", 4695469a65d1SMichael Tuexen (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK"); 4696cd554309SMichael Tuexen break; 46976e55db54SRandall Stewart } 4698469a65d1SMichael Tuexen if (ch->chunk_type == SCTP_SELECTIVE_ACK) { 4699cd554309SMichael Tuexen if (chk_length < sizeof(struct sctp_sack_chunk)) { 4700cd554309SMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); 4701cd554309SMichael Tuexen break; 4702d06c82f1SRandall Stewart } 4703469a65d1SMichael Tuexen } else { 4704469a65d1SMichael Tuexen if (stcb->asoc.nrsack_supported == 0) { 4705469a65d1SMichael Tuexen goto unknown_chunk; 4706469a65d1SMichael Tuexen } 4707469a65d1SMichael Tuexen if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { 4708469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n"); 4709469a65d1SMichael Tuexen break; 4710469a65d1SMichael Tuexen } 4711469a65d1SMichael Tuexen } 4712839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) { 47132afb3e84SRandall Stewart /*- 47142afb3e84SRandall Stewart * If we have sent a shutdown-ack, we will pay no 47152afb3e84SRandall Stewart * attention to a sack sent in to us since 47162afb3e84SRandall Stewart * we don't care anymore. 47172afb3e84SRandall Stewart */ 4718a1e13272SRandall Stewart break; 47192afb3e84SRandall Stewart } 4720cd554309SMichael Tuexen flags = ch->chunk_flags; 4721469a65d1SMichael Tuexen if (ch->chunk_type == SCTP_SELECTIVE_ACK) { 4722469a65d1SMichael Tuexen struct sctp_sack_chunk *sack; 4723469a65d1SMichael Tuexen 4724469a65d1SMichael Tuexen sack = (struct sctp_sack_chunk *)ch; 4725f8829a4aSRandall Stewart cum_ack = ntohl(sack->sack.cum_tsn_ack); 4726f8829a4aSRandall Stewart num_seg = ntohs(sack->sack.num_gap_ack_blks); 4727469a65d1SMichael Tuexen num_nr_seg = 0; 4728cd554309SMichael Tuexen num_dup = ntohs(sack->sack.num_dup_tsns); 4729469a65d1SMichael Tuexen a_rwnd = ntohl(sack->sack.a_rwnd); 4730cd554309SMichael Tuexen if (sizeof(struct sctp_sack_chunk) + 4731cd554309SMichael Tuexen num_seg * sizeof(struct sctp_gap_ack_block) + 4732cd554309SMichael Tuexen num_dup * sizeof(uint32_t) != chk_length) { 4733cd554309SMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); 4734cd554309SMichael Tuexen break; 4735cd554309SMichael Tuexen } 4736cd554309SMichael Tuexen offset_seg = *offset + sizeof(struct sctp_sack_chunk); 4737cd554309SMichael Tuexen offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); 4738f8829a4aSRandall Stewart } else { 4739830d754dSRandall Stewart struct sctp_nr_sack_chunk *nr_sack; 4740830d754dSRandall Stewart 4741830d754dSRandall Stewart nr_sack = (struct sctp_nr_sack_chunk *)ch; 4742830d754dSRandall Stewart cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); 4743830d754dSRandall Stewart num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); 4744830d754dSRandall Stewart num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); 4745cd554309SMichael Tuexen num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); 4746469a65d1SMichael Tuexen a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd); 4747cd554309SMichael Tuexen if (sizeof(struct sctp_nr_sack_chunk) + 4748cd554309SMichael Tuexen (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + 4749cd554309SMichael Tuexen num_dup * sizeof(uint32_t) != chk_length) { 4750cd554309SMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); 4751cd554309SMichael Tuexen break; 4752cd554309SMichael Tuexen } 4753cd554309SMichael Tuexen offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); 4754469a65d1SMichael Tuexen offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block); 4755469a65d1SMichael Tuexen } 4756469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n", 4757469a65d1SMichael Tuexen (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK", 4758cd554309SMichael Tuexen cum_ack, num_seg, a_rwnd); 4759830d754dSRandall Stewart stcb->asoc.seen_a_sack_this_pkt = 1; 4760830d754dSRandall Stewart if ((stcb->asoc.pr_sctp_cnt == 0) && 4761cd554309SMichael Tuexen (num_seg == 0) && (num_nr_seg == 0) && 476220b07a4dSMichael Tuexen SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && 4763830d754dSRandall Stewart (stcb->asoc.saw_sack_with_frags == 0) && 4764d9c5cfeaSMichael Tuexen (stcb->asoc.saw_sack_with_nr_frags == 0) && 4765cd554309SMichael Tuexen (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 4766830d754dSRandall Stewart /* 4767830d754dSRandall Stewart * We have a SIMPLE sack having no 4768830d754dSRandall Stewart * prior segments and data on sent 4769cd554309SMichael Tuexen * queue to be acked. Use the faster 4770cd554309SMichael Tuexen * path sack processing. We also 4771cd554309SMichael Tuexen * allow window update sacks with no 4772cd554309SMichael Tuexen * missing segments to go this way 4773cd554309SMichael Tuexen * too. 4774830d754dSRandall Stewart */ 4775493d8e5aSRandall Stewart sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 4776899288aeSRandall Stewart &abort_now, ecne_seen); 4777830d754dSRandall Stewart } else { 4778469a65d1SMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 47797215cc1bSMichael Tuexen sctp_handle_sack(m, offset_seg, offset_dup, stcb, 4780cd554309SMichael Tuexen num_seg, num_nr_seg, num_dup, &abort_now, flags, 4781899288aeSRandall Stewart cum_ack, a_rwnd, ecne_seen); 4782830d754dSRandall Stewart } 4783469a65d1SMichael Tuexen } 4784830d754dSRandall Stewart if (abort_now) { 4785830d754dSRandall Stewart /* ABORT signal from sack processing */ 4786830d754dSRandall Stewart *offset = length; 4787830d754dSRandall Stewart return (NULL); 4788830d754dSRandall Stewart } 4789cd554309SMichael Tuexen if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4790cd554309SMichael Tuexen TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4791cd554309SMichael Tuexen (stcb->asoc.stream_queue_cnt == 0)) { 4792cd554309SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); 4793cd554309SMichael Tuexen } 4794830d754dSRandall Stewart break; 4795469a65d1SMichael Tuexen } 4796f8829a4aSRandall Stewart case SCTP_HEARTBEAT_REQUEST: 4797ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); 4798469a65d1SMichael Tuexen if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) { 4799f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvheartbeat); 4800ad81507eSRandall Stewart sctp_send_heartbeat_ack(stcb, m, *offset, 4801ad81507eSRandall Stewart chk_length, *netp); 4802ad81507eSRandall Stewart } 4803f8829a4aSRandall Stewart break; 4804f8829a4aSRandall Stewart case SCTP_HEARTBEAT_ACK: 4805469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n"); 4806ad81507eSRandall Stewart if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { 4807d06c82f1SRandall Stewart /* Its not ours */ 48089de7354bSMichael Tuexen break; 4809d06c82f1SRandall Stewart } 4810f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvheartbeatack); 4811469a65d1SMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 4812f8829a4aSRandall Stewart sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, 4813f8829a4aSRandall Stewart stcb, *netp); 4814469a65d1SMichael Tuexen } 4815f8829a4aSRandall Stewart break; 4816f8829a4aSRandall Stewart case SCTP_ABORT_ASSOCIATION: 4817207304d4SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", 4818dd294dceSMichael Tuexen (void *)stcb); 4819f8829a4aSRandall Stewart *offset = length; 4820701492a5SMichael Tuexen if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) { 4821701492a5SMichael Tuexen if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) { 4822f8829a4aSRandall Stewart return (NULL); 4823701492a5SMichael Tuexen } else { 4824701492a5SMichael Tuexen return (stcb); 4825701492a5SMichael Tuexen } 4826701492a5SMichael Tuexen } else { 4827701492a5SMichael Tuexen return (NULL); 4828701492a5SMichael Tuexen } 4829f8829a4aSRandall Stewart break; 4830f8829a4aSRandall Stewart case SCTP_SHUTDOWN: 4831207304d4SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", 4832dd294dceSMichael Tuexen (void *)stcb); 4833ad81507eSRandall Stewart if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { 48349de7354bSMichael Tuexen break; 4835ad81507eSRandall Stewart } 4836469a65d1SMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 48379de7354bSMichael Tuexen abort_flag = 0; 4838f8829a4aSRandall Stewart sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, 4839f8829a4aSRandall Stewart stcb, *netp, &abort_flag); 4840f8829a4aSRandall Stewart if (abort_flag) { 4841f8829a4aSRandall Stewart *offset = length; 4842f8829a4aSRandall Stewart return (NULL); 4843f8829a4aSRandall Stewart } 4844f8829a4aSRandall Stewart } 4845f8829a4aSRandall Stewart break; 4846f8829a4aSRandall Stewart case SCTP_SHUTDOWN_ACK: 4847469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb); 4848d1cb8d11SMichael Tuexen if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) && 4849d1cb8d11SMichael Tuexen (stcb != NULL) && (netp != NULL) && (*netp != NULL)) { 4850f8829a4aSRandall Stewart sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); 4851f8829a4aSRandall Stewart *offset = length; 4852f8829a4aSRandall Stewart return (NULL); 4853d1cb8d11SMichael Tuexen } 4854f8829a4aSRandall Stewart break; 4855f8829a4aSRandall Stewart case SCTP_OPERATION_ERROR: 4856469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n"); 4857469a65d1SMichael Tuexen if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) && 48583e87bccdSMichael Tuexen sctp_handle_error(ch, stcb, *netp, contiguous) < 0) { 4859f8829a4aSRandall Stewart *offset = length; 4860f8829a4aSRandall Stewart return (NULL); 4861f8829a4aSRandall Stewart } 4862f8829a4aSRandall Stewart break; 4863f8829a4aSRandall Stewart case SCTP_COOKIE_ECHO: 4864ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, 4865469a65d1SMichael Tuexen "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb); 4866469a65d1SMichael Tuexen if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) { 4867f8829a4aSRandall Stewart ; 4868f8829a4aSRandall Stewart } else { 4869ad81507eSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4870f8829a4aSRandall Stewart /* We are not interested anymore */ 48710c7dc840SRandall Stewart abend: 487280a2d140SMichael Tuexen if (stcb != NULL) { 487328085b2eSRandall Stewart SCTP_TCB_UNLOCK(stcb); 487428085b2eSRandall Stewart } 4875f8829a4aSRandall Stewart *offset = length; 4876f8829a4aSRandall Stewart return (NULL); 4877f8829a4aSRandall Stewart } 4878f8829a4aSRandall Stewart } 48793017b21bSMichael Tuexen /*- 4880f8829a4aSRandall Stewart * First are we accepting? We do this again here 488188a7eb29SRandall Stewart * since it is possible that a previous endpoint WAS 488288a7eb29SRandall Stewart * listening responded to a INIT-ACK and then 4883f8829a4aSRandall Stewart * closed. We opened and bound.. and are now no 4884f8829a4aSRandall Stewart * longer listening. 4885779f106aSGleb Smirnoff * 4886779f106aSGleb Smirnoff * XXXGL: notes on checking listen queue length. 4887779f106aSGleb Smirnoff * 1) SCTP_IS_LISTENING() doesn't necessarily mean 4888779f106aSGleb Smirnoff * SOLISTENING(), because a listening "UDP type" 4889779f106aSGleb Smirnoff * socket isn't listening in terms of the socket 4890779f106aSGleb Smirnoff * layer. It is a normal data flow socket, that 4891779f106aSGleb Smirnoff * can fork off new connections. Thus, we should 4892779f106aSGleb Smirnoff * look into sol_qlen only in case we are !UDP. 4893779f106aSGleb Smirnoff * 2) Checking sol_qlen in general requires locking 4894779f106aSGleb Smirnoff * the socket, and this code lacks that. 4895f8829a4aSRandall Stewart */ 48965d08768aSMichael Tuexen if ((stcb == NULL) && 48975d08768aSMichael Tuexen (!SCTP_IS_LISTENING(inp) || 4898779f106aSGleb Smirnoff (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) && 4899779f106aSGleb Smirnoff inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) { 4900b201f536SRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4901b3f1ea41SRandall Stewart (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { 4902ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 4903b1754ad1SMichael Tuexen sctp_abort_association(inp, stcb, m, iphlen, 4904b1754ad1SMichael Tuexen src, dst, sh, op_err, 4905457b4b88SMichael Tuexen mflowtype, mflowid, 4906f30ac432SMichael Tuexen vrf_id, port); 4907f8829a4aSRandall Stewart } 4908f8829a4aSRandall Stewart *offset = length; 4909f8829a4aSRandall Stewart return (NULL); 4910b201f536SRandall Stewart } else { 4911f8829a4aSRandall Stewart struct mbuf *ret_buf; 4912a5d547adSRandall Stewart struct sctp_inpcb *linp; 4913efd5e692SMichael Tuexen struct sctp_tmit_chunk *chk; 4914f8829a4aSRandall Stewart 4915ad81507eSRandall Stewart if (stcb) { 4916a5d547adSRandall Stewart linp = NULL; 4917ad81507eSRandall Stewart } else { 4918a5d547adSRandall Stewart linp = inp; 4919ad81507eSRandall Stewart } 4920a5d547adSRandall Stewart 4921469a65d1SMichael Tuexen if (linp != NULL) { 4922a5d547adSRandall Stewart SCTP_ASOC_CREATE_LOCK(linp); 49230c7dc840SRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 49240c7dc840SRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 49250c7dc840SRandall Stewart SCTP_ASOC_CREATE_UNLOCK(linp); 49260c7dc840SRandall Stewart goto abend; 49270c7dc840SRandall Stewart } 4928ad81507eSRandall Stewart } 49290053ed28SMichael Tuexen 4930469a65d1SMichael Tuexen if (netp != NULL) { 493180a2d140SMichael Tuexen struct sctp_tcb *locked_stcb; 493280a2d140SMichael Tuexen 493380a2d140SMichael Tuexen locked_stcb = stcb; 4934f8829a4aSRandall Stewart ret_buf = 4935f8829a4aSRandall Stewart sctp_handle_cookie_echo(m, iphlen, 4936b1754ad1SMichael Tuexen *offset, 4937b1754ad1SMichael Tuexen src, dst, 4938b1754ad1SMichael Tuexen sh, 4939f8829a4aSRandall Stewart (struct sctp_cookie_echo_chunk *)ch, 4940f8829a4aSRandall Stewart &inp, &stcb, netp, 4941f8829a4aSRandall Stewart auth_skipped, 4942f8829a4aSRandall Stewart auth_offset, 4943a5d547adSRandall Stewart auth_len, 494480a2d140SMichael Tuexen &locked_stcb, 4945457b4b88SMichael Tuexen mflowtype, 4946f30ac432SMichael Tuexen mflowid, 4947c54a18d2SRandall Stewart vrf_id, 4948c54a18d2SRandall Stewart port); 494980a2d140SMichael Tuexen if ((locked_stcb != NULL) && (locked_stcb != stcb)) { 495080a2d140SMichael Tuexen SCTP_TCB_UNLOCK(locked_stcb); 495180a2d140SMichael Tuexen } 495280a2d140SMichael Tuexen if (stcb != NULL) { 495380a2d140SMichael Tuexen SCTP_TCB_LOCK_ASSERT(stcb); 495480a2d140SMichael Tuexen } 4955ad81507eSRandall Stewart } else { 4956ad81507eSRandall Stewart ret_buf = NULL; 4957ad81507eSRandall Stewart } 4958469a65d1SMichael Tuexen if (linp != NULL) { 4959a5d547adSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(linp); 4960ad81507eSRandall Stewart } 4961f8829a4aSRandall Stewart if (ret_buf == NULL) { 496280a2d140SMichael Tuexen if (stcb != NULL) { 496380a2d140SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 4964f8829a4aSRandall Stewart } 4965ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, 4966ad81507eSRandall Stewart "GAK, null buffer\n"); 4967f8829a4aSRandall Stewart *offset = length; 4968f8829a4aSRandall Stewart return (NULL); 4969f8829a4aSRandall Stewart } 4970f8829a4aSRandall Stewart /* if AUTH skipped, see if it verified... */ 4971f8829a4aSRandall Stewart if (auth_skipped) { 4972f8829a4aSRandall Stewart got_auth = 1; 4973f8829a4aSRandall Stewart auth_skipped = 0; 4974f8829a4aSRandall Stewart } 4975efd5e692SMichael Tuexen /* Restart the timer if we have pending data */ 497686fd36c5SMichael Tuexen TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 4977efd5e692SMichael Tuexen if (chk->whoTo != NULL) { 4978efd5e692SMichael Tuexen break; 4979efd5e692SMichael Tuexen } 4980efd5e692SMichael Tuexen } 4981efd5e692SMichael Tuexen if (chk != NULL) { 49824a9ef3f8SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); 4983f8829a4aSRandall Stewart } 4984f8829a4aSRandall Stewart } 4985f8829a4aSRandall Stewart break; 4986f8829a4aSRandall Stewart case SCTP_COOKIE_ACK: 4987469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb); 4988ad81507eSRandall Stewart if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { 49899de7354bSMichael Tuexen break; 499017205eccSRandall Stewart } 4991f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4992f8829a4aSRandall Stewart /* We are not interested anymore */ 4993f8829a4aSRandall Stewart if ((stcb) && (stcb->asoc.total_output_queue_size)) { 4994f8829a4aSRandall Stewart ; 4995ad81507eSRandall Stewart } else if (stcb) { 4996b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 4997b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_30); 4998f8829a4aSRandall Stewart *offset = length; 4999f8829a4aSRandall Stewart return (NULL); 5000f8829a4aSRandall Stewart } 5001f8829a4aSRandall Stewart } 5002469a65d1SMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 5003f8829a4aSRandall Stewart sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp); 50046e55db54SRandall Stewart } 5005f8829a4aSRandall Stewart break; 5006f8829a4aSRandall Stewart case SCTP_ECN_ECHO: 5007469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n"); 50089de7354bSMichael Tuexen if (stcb == NULL) { 50099de7354bSMichael Tuexen break; 5010d06c82f1SRandall Stewart } 5011c79bec9cSMichael Tuexen if (stcb->asoc.ecn_supported == 0) { 5012c79bec9cSMichael Tuexen goto unknown_chunk; 5013c79bec9cSMichael Tuexen } 50146587a2bdSMichael Tuexen if ((chk_length != sizeof(struct sctp_ecne_chunk)) && 50156587a2bdSMichael Tuexen (chk_length != sizeof(struct old_sctp_ecne_chunk))) { 50169de7354bSMichael Tuexen break; 50179de7354bSMichael Tuexen } 5018469a65d1SMichael Tuexen sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb); 5019899288aeSRandall Stewart ecne_seen = 1; 5020f8829a4aSRandall Stewart break; 5021f8829a4aSRandall Stewart case SCTP_ECN_CWR: 5022469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n"); 50239de7354bSMichael Tuexen if (stcb == NULL) { 50249de7354bSMichael Tuexen break; 5025d06c82f1SRandall Stewart } 5026c79bec9cSMichael Tuexen if (stcb->asoc.ecn_supported == 0) { 5027c79bec9cSMichael Tuexen goto unknown_chunk; 5028c79bec9cSMichael Tuexen } 50299de7354bSMichael Tuexen if (chk_length != sizeof(struct sctp_cwr_chunk)) { 50309de7354bSMichael Tuexen break; 50319de7354bSMichael Tuexen } 5032a21779f0SRandall Stewart sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); 5033f8829a4aSRandall Stewart break; 5034f8829a4aSRandall Stewart case SCTP_SHUTDOWN_COMPLETE: 5035469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb); 5036f8829a4aSRandall Stewart /* must be first and only chunk */ 5037f8829a4aSRandall Stewart if ((num_chunks > 1) || 50384c9179adSRandall Stewart (length - *offset > (int)SCTP_SIZE32(chk_length))) { 5039f8829a4aSRandall Stewart *offset = length; 504080a2d140SMichael Tuexen return (stcb); 5041f8829a4aSRandall Stewart } 5042d1cb8d11SMichael Tuexen if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) && 5043d1cb8d11SMichael Tuexen (stcb != NULL) && (netp != NULL) && (*netp != NULL)) { 5044f8829a4aSRandall Stewart sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, 5045f8829a4aSRandall Stewart stcb, *netp); 5046f8829a4aSRandall Stewart *offset = length; 5047f8829a4aSRandall Stewart return (NULL); 5048d1cb8d11SMichael Tuexen } 5049f8829a4aSRandall Stewart break; 5050f8829a4aSRandall Stewart case SCTP_ASCONF: 5051ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); 5052469a65d1SMichael Tuexen if (stcb != NULL) { 5053c79bec9cSMichael Tuexen if (stcb->asoc.asconf_supported == 0) { 5054c79bec9cSMichael Tuexen goto unknown_chunk; 5055c79bec9cSMichael Tuexen } 5056b1754ad1SMichael Tuexen sctp_handle_asconf(m, *offset, src, 50572afb3e84SRandall Stewart (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); 50582afb3e84SRandall Stewart asconf_cnt++; 50596e55db54SRandall Stewart } 5060f8829a4aSRandall Stewart break; 5061f8829a4aSRandall Stewart case SCTP_ASCONF_ACK: 5062469a65d1SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n"); 50639de7354bSMichael Tuexen if (stcb == NULL) { 50649de7354bSMichael Tuexen break; 5065d06c82f1SRandall Stewart } 5066c79bec9cSMichael Tuexen if (stcb->asoc.asconf_supported == 0) { 5067c79bec9cSMichael Tuexen goto unknown_chunk; 5068c79bec9cSMichael Tuexen } 50699de7354bSMichael Tuexen if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { 50709de7354bSMichael Tuexen break; 50719de7354bSMichael Tuexen } 50729de7354bSMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 5073f8829a4aSRandall Stewart /* He's alive so give him credit */ 5074b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 5075c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 5076c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 5077c4739e2fSRandall Stewart 0, 5078c4739e2fSRandall Stewart SCTP_FROM_SCTP_INPUT, 5079c4739e2fSRandall Stewart __LINE__); 5080c4739e2fSRandall Stewart } 5081f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 5082f8829a4aSRandall Stewart sctp_handle_asconf_ack(m, *offset, 50833232788eSRandall Stewart (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); 50843232788eSRandall Stewart if (abort_no_unlock) 50853232788eSRandall Stewart return (NULL); 50866e55db54SRandall Stewart } 5087f8829a4aSRandall Stewart break; 5088f8829a4aSRandall Stewart case SCTP_FORWARD_CUM_TSN: 508944249214SRandall Stewart case SCTP_IFORWARD_CUM_TSN: 5090c96d7c37SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n", 5091c96d7c37SMichael Tuexen ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN"); 50929de7354bSMichael Tuexen if (stcb == NULL) { 50939de7354bSMichael Tuexen break; 5094d06c82f1SRandall Stewart } 5095c79bec9cSMichael Tuexen if (stcb->asoc.prsctp_supported == 0) { 5096c79bec9cSMichael Tuexen goto unknown_chunk; 5097c79bec9cSMichael Tuexen } 50989de7354bSMichael Tuexen if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { 50999de7354bSMichael Tuexen break; 51009de7354bSMichael Tuexen } 5101fcbfdc0aSMichael Tuexen if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) || 5102fcbfdc0aSMichael Tuexen ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) { 5103c96d7c37SMichael Tuexen if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) { 5104c96d7c37SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated"); 5105c96d7c37SMichael Tuexen } else { 5106c96d7c37SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated"); 5107c96d7c37SMichael Tuexen } 5108c96d7c37SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 5109105b68b4SMichael Tuexen sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED); 5110c96d7c37SMichael Tuexen *offset = length; 5111c96d7c37SMichael Tuexen return (NULL); 5112c96d7c37SMichael Tuexen } 5113f8829a4aSRandall Stewart *fwd_tsn_seen = 1; 5114f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5115f8829a4aSRandall Stewart /* We are not interested anymore */ 5116b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 5117b7d130beSMichael Tuexen SCTP_FROM_SCTP_INPUT + SCTP_LOC_31); 5118f8829a4aSRandall Stewart *offset = length; 5119f8829a4aSRandall Stewart return (NULL); 5120f8829a4aSRandall Stewart } 512191843cf3SMichael Tuexen /* 51229de7354bSMichael Tuexen * For sending a SACK this looks like DATA chunks. 512391843cf3SMichael Tuexen */ 512491843cf3SMichael Tuexen stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from; 51259de7354bSMichael Tuexen abort_flag = 0; 5126f8829a4aSRandall Stewart sctp_handle_forward_tsn(stcb, 5127671d309cSRandall Stewart (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); 5128f8829a4aSRandall Stewart if (abort_flag) { 5129f8829a4aSRandall Stewart *offset = length; 5130f8829a4aSRandall Stewart return (NULL); 5131c4739e2fSRandall Stewart } 5132f8829a4aSRandall Stewart break; 5133f8829a4aSRandall Stewart case SCTP_STREAM_RESET: 5134ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); 51359de7354bSMichael Tuexen if (stcb == NULL) { 51369de7354bSMichael Tuexen break; 5137d06c82f1SRandall Stewart } 5138317e00efSMichael Tuexen if (stcb->asoc.reconfig_supported == 0) { 5139c79bec9cSMichael Tuexen goto unknown_chunk; 5140f8829a4aSRandall Stewart } 51419de7354bSMichael Tuexen if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) { 51429de7354bSMichael Tuexen break; 51439de7354bSMichael Tuexen } 5144a169d6ecSMichael Tuexen if (sctp_handle_stream_reset(stcb, m, *offset, ch)) { 5145f8829a4aSRandall Stewart /* stop processing */ 5146f8829a4aSRandall Stewart *offset = length; 5147f8829a4aSRandall Stewart return (NULL); 5148f8829a4aSRandall Stewart } 5149f8829a4aSRandall Stewart break; 5150f8829a4aSRandall Stewart case SCTP_PACKET_DROPPED: 5151ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); 51529de7354bSMichael Tuexen if (stcb == NULL) { 51539de7354bSMichael Tuexen break; 5154d06c82f1SRandall Stewart } 5155c79bec9cSMichael Tuexen if (stcb->asoc.pktdrop_supported == 0) { 5156c79bec9cSMichael Tuexen goto unknown_chunk; 5157c79bec9cSMichael Tuexen } 51589de7354bSMichael Tuexen if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { 51599de7354bSMichael Tuexen break; 51609de7354bSMichael Tuexen } 51619de7354bSMichael Tuexen if ((netp != NULL) && (*netp != NULL)) { 5162f8829a4aSRandall Stewart sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, 5163458303daSRandall Stewart stcb, *netp, 5164d0f6ab79SMichael Tuexen min(chk_length, contiguous)); 51656e55db54SRandall Stewart } 5166f8829a4aSRandall Stewart break; 5167f8829a4aSRandall Stewart case SCTP_AUTHENTICATION: 5168ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); 5169f8829a4aSRandall Stewart if (stcb == NULL) { 5170f8829a4aSRandall Stewart /* save the first AUTH for later processing */ 5171f8829a4aSRandall Stewart if (auth_skipped == 0) { 5172f8829a4aSRandall Stewart auth_offset = *offset; 5173f8829a4aSRandall Stewart auth_len = chk_length; 5174f8829a4aSRandall Stewart auth_skipped = 1; 5175f8829a4aSRandall Stewart } 5176f8829a4aSRandall Stewart /* skip this chunk (temporarily) */ 51779de7354bSMichael Tuexen break; 5178f8829a4aSRandall Stewart } 5179c79bec9cSMichael Tuexen if (stcb->asoc.auth_supported == 0) { 5180c79bec9cSMichael Tuexen goto unknown_chunk; 5181c79bec9cSMichael Tuexen } 5182d06c82f1SRandall Stewart if ((chk_length < (sizeof(struct sctp_auth_chunk))) || 5183ad81507eSRandall Stewart (chk_length > (sizeof(struct sctp_auth_chunk) + 5184ad81507eSRandall Stewart SCTP_AUTH_DIGEST_LEN_MAX))) { 5185d06c82f1SRandall Stewart /* Its not ours */ 5186d06c82f1SRandall Stewart *offset = length; 518780a2d140SMichael Tuexen return (stcb); 5188d06c82f1SRandall Stewart } 5189f8829a4aSRandall Stewart if (got_auth == 1) { 5190f8829a4aSRandall Stewart /* skip this chunk... it's already auth'd */ 51919de7354bSMichael Tuexen break; 5192f8829a4aSRandall Stewart } 5193f8829a4aSRandall Stewart got_auth = 1; 5194f2f66ef6SMichael Tuexen if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) { 5195f8829a4aSRandall Stewart /* auth HMAC failed so dump the packet */ 5196f8829a4aSRandall Stewart *offset = length; 5197f8829a4aSRandall Stewart return (stcb); 5198f8829a4aSRandall Stewart } else { 5199f8829a4aSRandall Stewart /* remaining chunks are HMAC checked */ 5200f8829a4aSRandall Stewart stcb->asoc.authenticated = 1; 5201f8829a4aSRandall Stewart } 5202f8829a4aSRandall Stewart break; 5203f8829a4aSRandall Stewart 5204f8829a4aSRandall Stewart default: 5205f8829a4aSRandall Stewart unknown_chunk: 5206f8829a4aSRandall Stewart /* it's an unknown chunk! */ 5207e99ce3eaSMichael Tuexen if ((ch->chunk_type & 0x40) && 5208e99ce3eaSMichael Tuexen (stcb != NULL) && 5209e99ce3eaSMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) && 5210e99ce3eaSMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) && 5211e99ce3eaSMichael Tuexen (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) { 521286eda749SMichael Tuexen struct sctp_gen_error_cause *cause; 521339cbb549SMichael Tuexen int len; 5214f8829a4aSRandall Stewart 521586eda749SMichael Tuexen op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause), 5216eb1b1807SGleb Smirnoff 0, M_NOWAIT, 1, MT_DATA); 521786eda749SMichael Tuexen if (op_err != NULL) { 521839cbb549SMichael Tuexen len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset)); 521986eda749SMichael Tuexen cause = mtod(op_err, struct sctp_gen_error_cause *); 522086eda749SMichael Tuexen cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK); 52219a8e3088SMichael Tuexen cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause))); 522286eda749SMichael Tuexen SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause); 522386eda749SMichael Tuexen SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT); 522486eda749SMichael Tuexen if (SCTP_BUF_NEXT(op_err) != NULL) { 5225eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING 5226b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 522730811e70SMichael Tuexen sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY); 5228eadccaccSRandall Stewart } 5229eadccaccSRandall Stewart #endif 523086eda749SMichael Tuexen sctp_queue_op_err(stcb, op_err); 5231f8829a4aSRandall Stewart } else { 523286eda749SMichael Tuexen sctp_m_freem(op_err); 5233f8829a4aSRandall Stewart } 5234f8829a4aSRandall Stewart } 5235f8829a4aSRandall Stewart } 5236f8829a4aSRandall Stewart if ((ch->chunk_type & 0x80) == 0) { 5237f8829a4aSRandall Stewart /* discard this packet */ 5238f8829a4aSRandall Stewart *offset = length; 5239f8829a4aSRandall Stewart return (stcb); 5240b7b84c0eSMichael Tuexen } /* else skip this bad chunk and continue... */ 5241b7b84c0eSMichael Tuexen break; 5242f8829a4aSRandall Stewart } /* switch (ch->chunk_type) */ 5243f8829a4aSRandall Stewart 5244f8829a4aSRandall Stewart next_chunk: 5245f8829a4aSRandall Stewart /* get the next chunk */ 5246f8829a4aSRandall Stewart *offset += SCTP_SIZE32(chk_length); 5247f8829a4aSRandall Stewart if (*offset >= length) { 5248f8829a4aSRandall Stewart /* no more data left in the mbuf chain */ 5249f8829a4aSRandall Stewart break; 5250f8829a4aSRandall Stewart } 5251f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, 5252f8829a4aSRandall Stewart sizeof(struct sctp_chunkhdr), chunk_buf); 5253f8829a4aSRandall Stewart if (ch == NULL) { 5254f8829a4aSRandall Stewart *offset = length; 525580a2d140SMichael Tuexen return (stcb); 5256f8829a4aSRandall Stewart } 5257f8829a4aSRandall Stewart } /* while */ 52582afb3e84SRandall Stewart 5259469a65d1SMichael Tuexen if ((asconf_cnt > 0) && (stcb != NULL)) { 52602afb3e84SRandall Stewart sctp_send_asconf_ack(stcb); 52612afb3e84SRandall Stewart } 5262f8829a4aSRandall Stewart return (stcb); 5263f8829a4aSRandall Stewart } 5264f8829a4aSRandall Stewart 5265f8829a4aSRandall Stewart /* 5266f8829a4aSRandall Stewart * common input chunk processing (v4 and v6) 5267f8829a4aSRandall Stewart */ 52686e55db54SRandall Stewart void 5269b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length, 5270b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 5271b1754ad1SMichael Tuexen struct sctphdr *sh, struct sctp_chunkhdr *ch, 5272a8775ad9SMichael Tuexen uint8_t compute_crc, 5273a8775ad9SMichael Tuexen uint8_t ecn_bits, 5274d089f9b9SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, 5275f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 5276f8829a4aSRandall Stewart { 5277f8829a4aSRandall Stewart uint32_t high_tsn; 5278f8829a4aSRandall Stewart int fwd_tsn_seen = 0, data_processed = 0; 5279ff1ffd74SMichael Tuexen struct mbuf *m = *mm, *op_err; 5280ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 5281f8829a4aSRandall Stewart int un_sent; 5282493d8e5aSRandall Stewart int cnt_ctrl_ready = 0; 528355b175e7SMichael Tuexen struct sctp_inpcb *inp = NULL, *inp_decr = NULL; 5284a8775ad9SMichael Tuexen struct sctp_tcb *stcb = NULL; 5285e3d6ef0bSMichael Tuexen struct sctp_nets *net = NULL; 5286f8829a4aSRandall Stewart 5287f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvdatagrams); 5288f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 5289f8829a4aSRandall Stewart sctp_audit_log(0xE0, 1); 5290f8829a4aSRandall Stewart sctp_auditing(0, inp, stcb, net); 5291f8829a4aSRandall Stewart #endif 5292a8775ad9SMichael Tuexen if (compute_crc != 0) { 5293a8775ad9SMichael Tuexen uint32_t check, calc_check; 5294f8829a4aSRandall Stewart 5295a8775ad9SMichael Tuexen check = sh->checksum; 5296a8775ad9SMichael Tuexen sh->checksum = 0; 5297a8775ad9SMichael Tuexen calc_check = sctp_calculate_cksum(m, iphlen); 5298a8775ad9SMichael Tuexen sh->checksum = check; 5299a8775ad9SMichael Tuexen if (calc_check != check) { 5300a8775ad9SMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 5301dd294dceSMichael Tuexen calc_check, check, (void *)m, length, iphlen); 5302a8775ad9SMichael Tuexen stcb = sctp_findassociation_addr(m, offset, src, dst, 5303a8775ad9SMichael Tuexen sh, ch, &inp, &net, vrf_id); 53044474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 53053cf729a9SMichael Tuexen if ((ch->chunk_type != SCTP_INITIATION) && 53063cf729a9SMichael Tuexen (net != NULL) && (net->port != port)) { 5307a8775ad9SMichael Tuexen if (net->port == 0) { 53083cf729a9SMichael Tuexen /* UDP encapsulation turned on. */ 53093cf729a9SMichael Tuexen net->mtu -= sizeof(struct udphdr); 53103cf729a9SMichael Tuexen if (stcb->asoc.smallest_mtu > net->mtu) { 53113cf729a9SMichael Tuexen sctp_pathmtu_adjustment(stcb, net->mtu); 53123cf729a9SMichael Tuexen } 53133cf729a9SMichael Tuexen } else if (port == 0) { 53143cf729a9SMichael Tuexen /* UDP encapsulation turned off. */ 53153cf729a9SMichael Tuexen net->mtu += sizeof(struct udphdr); 53163cf729a9SMichael Tuexen /* XXX Update smallest_mtu */ 5317a8775ad9SMichael Tuexen } 5318a8775ad9SMichael Tuexen net->port = port; 5319a8775ad9SMichael Tuexen } 53204474d71aSMichael Tuexen #endif 53215fe29cdfSMichael Tuexen if (net != NULL) { 5322457b4b88SMichael Tuexen net->flowtype = mflowtype; 53235fe29cdfSMichael Tuexen net->flowid = mflowid; 5324a8775ad9SMichael Tuexen } 53251e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5326a8775ad9SMichael Tuexen if ((inp != NULL) && (stcb != NULL)) { 5327a8775ad9SMichael Tuexen sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1); 5328a8775ad9SMichael Tuexen sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 5329a8775ad9SMichael Tuexen } else if ((inp != NULL) && (stcb == NULL)) { 5330a8775ad9SMichael Tuexen inp_decr = inp; 5331a8775ad9SMichael Tuexen } 5332a8775ad9SMichael Tuexen SCTP_STAT_INCR(sctps_badsum); 5333a8775ad9SMichael Tuexen SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 5334a8775ad9SMichael Tuexen goto out; 5335a8775ad9SMichael Tuexen } 5336a8775ad9SMichael Tuexen } 5337a8775ad9SMichael Tuexen /* Destination port of 0 is illegal, based on RFC4960. */ 5338a8775ad9SMichael Tuexen if (sh->dest_port == 0) { 5339a8775ad9SMichael Tuexen SCTP_STAT_INCR(sctps_hdrops); 5340a8775ad9SMichael Tuexen goto out; 5341a8775ad9SMichael Tuexen } 5342a8775ad9SMichael Tuexen stcb = sctp_findassociation_addr(m, offset, src, dst, 5343a8775ad9SMichael Tuexen sh, ch, &inp, &net, vrf_id); 53444474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 53453cf729a9SMichael Tuexen if ((ch->chunk_type != SCTP_INITIATION) && 53463cf729a9SMichael Tuexen (net != NULL) && (net->port != port)) { 5347a8775ad9SMichael Tuexen if (net->port == 0) { 53483cf729a9SMichael Tuexen /* UDP encapsulation turned on. */ 53493cf729a9SMichael Tuexen net->mtu -= sizeof(struct udphdr); 53503cf729a9SMichael Tuexen if (stcb->asoc.smallest_mtu > net->mtu) { 53513cf729a9SMichael Tuexen sctp_pathmtu_adjustment(stcb, net->mtu); 53523cf729a9SMichael Tuexen } 53533cf729a9SMichael Tuexen } else if (port == 0) { 53543cf729a9SMichael Tuexen /* UDP encapsulation turned off. */ 53553cf729a9SMichael Tuexen net->mtu += sizeof(struct udphdr); 53563cf729a9SMichael Tuexen /* XXX Update smallest_mtu */ 5357a8775ad9SMichael Tuexen } 5358a8775ad9SMichael Tuexen net->port = port; 5359a8775ad9SMichael Tuexen } 53604474d71aSMichael Tuexen #endif 53615fe29cdfSMichael Tuexen if (net != NULL) { 5362457b4b88SMichael Tuexen net->flowtype = mflowtype; 53635fe29cdfSMichael Tuexen net->flowid = mflowid; 5364a8775ad9SMichael Tuexen } 5365a8775ad9SMichael Tuexen if (inp == NULL) { 53661e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5367a8775ad9SMichael Tuexen SCTP_STAT_INCR(sctps_noport); 5368a8775ad9SMichael Tuexen if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) { 5369a8775ad9SMichael Tuexen goto out; 5370a8775ad9SMichael Tuexen } 5371a8775ad9SMichael Tuexen if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 5372a8775ad9SMichael Tuexen sctp_send_shutdown_complete2(src, dst, sh, 5373d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum, 5374a8775ad9SMichael Tuexen vrf_id, port); 5375a8775ad9SMichael Tuexen goto out; 5376a8775ad9SMichael Tuexen } 5377a8775ad9SMichael Tuexen if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 5378a8775ad9SMichael Tuexen goto out; 5379a8775ad9SMichael Tuexen } 5380a8775ad9SMichael Tuexen if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { 5381a8775ad9SMichael Tuexen if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 5382a8775ad9SMichael Tuexen ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 5383a8775ad9SMichael Tuexen (ch->chunk_type != SCTP_INIT))) { 5384ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 5385ff1ffd74SMichael Tuexen "Out of the blue"); 5386a8775ad9SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, 5387ff1ffd74SMichael Tuexen sh, 0, op_err, 5388d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum, 5389a8775ad9SMichael Tuexen vrf_id, port); 5390a8775ad9SMichael Tuexen } 5391a8775ad9SMichael Tuexen } 5392a8775ad9SMichael Tuexen goto out; 5393a8775ad9SMichael Tuexen } else if (stcb == NULL) { 5394a8775ad9SMichael Tuexen inp_decr = inp; 5395a8775ad9SMichael Tuexen } 5396b3f1ea41SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", 5397dd294dceSMichael Tuexen (void *)m, iphlen, offset, length, (void *)stcb); 5398f8829a4aSRandall Stewart if (stcb) { 5399f8829a4aSRandall Stewart /* always clear this before beginning a packet */ 5400f8829a4aSRandall Stewart stcb->asoc.authenticated = 0; 5401f8829a4aSRandall Stewart stcb->asoc.seen_a_sack_this_pkt = 0; 54022afb3e84SRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", 5403dd294dceSMichael Tuexen (void *)stcb, stcb->asoc.state); 54042afb3e84SRandall Stewart 5405c4739e2fSRandall Stewart if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || 5406c4739e2fSRandall Stewart (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 540763981c2bSRandall Stewart /*- 540863981c2bSRandall Stewart * If we hit here, we had a ref count 540963981c2bSRandall Stewart * up when the assoc was aborted and the 541063981c2bSRandall Stewart * timer is clearing out the assoc, we should 541163981c2bSRandall Stewart * NOT respond to any packet.. its OOTB. 541263981c2bSRandall Stewart */ 541363981c2bSRandall Stewart SCTP_TCB_UNLOCK(stcb); 5414a8775ad9SMichael Tuexen stcb = NULL; 54151e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5416999f86d6SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__); 5417ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 5418ff1ffd74SMichael Tuexen msg); 5419ff1ffd74SMichael Tuexen sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err, 5420d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 5421c54a18d2SRandall Stewart vrf_id, port); 5422a8775ad9SMichael Tuexen goto out; 542363981c2bSRandall Stewart } 5424f8829a4aSRandall Stewart } 5425f8829a4aSRandall Stewart if (IS_SCTP_CONTROL(ch)) { 5426f8829a4aSRandall Stewart /* process the control portion of the SCTP packet */ 54273c503c28SRandall Stewart /* sa_ignore NO_NULL_CHK */ 5428b1754ad1SMichael Tuexen stcb = sctp_process_control(m, iphlen, &offset, length, 5429b1754ad1SMichael Tuexen src, dst, sh, ch, 5430f30ac432SMichael Tuexen inp, stcb, &net, &fwd_tsn_seen, 5431d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum, 5432f30ac432SMichael Tuexen vrf_id, port); 5433f8829a4aSRandall Stewart if (stcb) { 5434f8829a4aSRandall Stewart /* 5435f8829a4aSRandall Stewart * This covers us if the cookie-echo was there and 5436f8829a4aSRandall Stewart * it changes our INP. 5437f8829a4aSRandall Stewart */ 5438f8829a4aSRandall Stewart inp = stcb->sctp_ep; 54394474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 54403cf729a9SMichael Tuexen if ((ch->chunk_type != SCTP_INITIATION) && 54413cf729a9SMichael Tuexen (net != NULL) && (net->port != port)) { 5442b3f1ea41SRandall Stewart if (net->port == 0) { 54433cf729a9SMichael Tuexen /* UDP encapsulation turned on. */ 54443cf729a9SMichael Tuexen net->mtu -= sizeof(struct udphdr); 54453cf729a9SMichael Tuexen if (stcb->asoc.smallest_mtu > net->mtu) { 54463cf729a9SMichael Tuexen sctp_pathmtu_adjustment(stcb, net->mtu); 54473cf729a9SMichael Tuexen } 54483cf729a9SMichael Tuexen } else if (port == 0) { 54493cf729a9SMichael Tuexen /* UDP encapsulation turned off. */ 54503cf729a9SMichael Tuexen net->mtu += sizeof(struct udphdr); 54513cf729a9SMichael Tuexen /* XXX Update smallest_mtu */ 5452b3f1ea41SRandall Stewart } 5453b3f1ea41SRandall Stewart net->port = port; 5454b3f1ea41SRandall Stewart } 54554474d71aSMichael Tuexen #endif 5456f8829a4aSRandall Stewart } 5457f8829a4aSRandall Stewart } else { 5458f8829a4aSRandall Stewart /* 5459f8829a4aSRandall Stewart * no control chunks, so pre-process DATA chunks (these 5460f8829a4aSRandall Stewart * checks are taken care of by control processing) 5461f8829a4aSRandall Stewart */ 5462f8829a4aSRandall Stewart 5463f8829a4aSRandall Stewart /* 5464f8829a4aSRandall Stewart * if DATA only packet, and auth is required, then punt... 5465f8829a4aSRandall Stewart * can't have authenticated without any AUTH (control) 5466f8829a4aSRandall Stewart * chunks 5467f8829a4aSRandall Stewart */ 5468b3f1ea41SRandall Stewart if ((stcb != NULL) && 5469b3f1ea41SRandall Stewart sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { 5470f8829a4aSRandall Stewart /* "silently" ignore */ 54711e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5472f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvauthmissing); 5473a8775ad9SMichael Tuexen goto out; 5474f8829a4aSRandall Stewart } 5475f8829a4aSRandall Stewart if (stcb == NULL) { 5476f8829a4aSRandall Stewart /* out of the blue DATA chunk */ 54771e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh); 5478999f86d6SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__); 5479ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 5480ff1ffd74SMichael Tuexen msg); 5481ff1ffd74SMichael Tuexen sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err, 5482d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum, 5483c54a18d2SRandall Stewart vrf_id, port); 5484a8775ad9SMichael Tuexen goto out; 5485f8829a4aSRandall Stewart } 5486f8829a4aSRandall Stewart if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { 5487f8829a4aSRandall Stewart /* v_tag mismatch! */ 54881e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5489f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_badvtag); 5490a8775ad9SMichael Tuexen goto out; 5491f8829a4aSRandall Stewart } 5492f8829a4aSRandall Stewart } 5493f8829a4aSRandall Stewart 54941e88cc8bSMichael Tuexen SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh); 5495f8829a4aSRandall Stewart if (stcb == NULL) { 5496f8829a4aSRandall Stewart /* 5497f8829a4aSRandall Stewart * no valid TCB for this packet, or we found it's a bad 5498f8829a4aSRandall Stewart * packet while processing control, or we're done with this 5499f8829a4aSRandall Stewart * packet (done or skip rest of data), so we drop it... 5500f8829a4aSRandall Stewart */ 5501a8775ad9SMichael Tuexen goto out; 5502f8829a4aSRandall Stewart } 55030053ed28SMichael Tuexen 5504f8829a4aSRandall Stewart /* 5505f8829a4aSRandall Stewart * DATA chunk processing 5506f8829a4aSRandall Stewart */ 5507f8829a4aSRandall Stewart /* plow through the data chunks while length > offset */ 5508f8829a4aSRandall Stewart 5509f8829a4aSRandall Stewart /* 5510f8829a4aSRandall Stewart * Rest should be DATA only. Check authentication state if AUTH for 5511f8829a4aSRandall Stewart * DATA is required. 5512f8829a4aSRandall Stewart */ 5513b3f1ea41SRandall Stewart if ((length > offset) && 5514b3f1ea41SRandall Stewart (stcb != NULL) && 5515b3f1ea41SRandall Stewart sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && 5516f8829a4aSRandall Stewart !stcb->asoc.authenticated) { 5517f8829a4aSRandall Stewart /* "silently" ignore */ 5518f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvauthmissing); 5519ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_AUTH1, 5520ad81507eSRandall Stewart "Data chunk requires AUTH, skipped\n"); 5521a5d547adSRandall Stewart goto trigger_send; 5522f8829a4aSRandall Stewart } 5523f8829a4aSRandall Stewart if (length > offset) { 5524f8829a4aSRandall Stewart int retval; 5525f8829a4aSRandall Stewart 5526f8829a4aSRandall Stewart /* 5527f8829a4aSRandall Stewart * First check to make sure our state is correct. We would 5528f8829a4aSRandall Stewart * not get here unless we really did have a tag, so we don't 5529f8829a4aSRandall Stewart * abort if this happens, just dump the chunk silently. 5530f8829a4aSRandall Stewart */ 5531839d21d6SMichael Tuexen switch (SCTP_GET_STATE(stcb)) { 5532f8829a4aSRandall Stewart case SCTP_STATE_COOKIE_ECHOED: 5533f8829a4aSRandall Stewart /* 5534f8829a4aSRandall Stewart * we consider data with valid tags in this state 5535f8829a4aSRandall Stewart * shows us the cookie-ack was lost. Imply it was 5536f8829a4aSRandall Stewart * there. 5537f8829a4aSRandall Stewart */ 5538f8829a4aSRandall Stewart sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); 5539f8829a4aSRandall Stewart break; 5540f8829a4aSRandall Stewart case SCTP_STATE_COOKIE_WAIT: 5541f8829a4aSRandall Stewart /* 5542f8829a4aSRandall Stewart * We consider OOTB any data sent during asoc setup. 5543f8829a4aSRandall Stewart */ 5544999f86d6SMichael Tuexen SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__); 5545ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 5546ff1ffd74SMichael Tuexen msg); 5547ff1ffd74SMichael Tuexen sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err, 5548d089f9b9SMichael Tuexen mflowtype, mflowid, inp->fibnum, 5549c54a18d2SRandall Stewart vrf_id, port); 5550a8775ad9SMichael Tuexen goto out; 555152be287eSRandall Stewart /* sa_ignore NOTREACHED */ 5552f8829a4aSRandall Stewart break; 5553f8829a4aSRandall Stewart case SCTP_STATE_EMPTY: /* should not happen */ 5554f8829a4aSRandall Stewart case SCTP_STATE_INUSE: /* should not happen */ 5555f8829a4aSRandall Stewart case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ 5556f8829a4aSRandall Stewart case SCTP_STATE_SHUTDOWN_ACK_SENT: 5557f8829a4aSRandall Stewart default: 5558a8775ad9SMichael Tuexen goto out; 555952be287eSRandall Stewart /* sa_ignore NOTREACHED */ 5560f8829a4aSRandall Stewart break; 5561f8829a4aSRandall Stewart case SCTP_STATE_OPEN: 5562f8829a4aSRandall Stewart case SCTP_STATE_SHUTDOWN_SENT: 5563f8829a4aSRandall Stewart break; 5564f8829a4aSRandall Stewart } 5565f8829a4aSRandall Stewart /* plow through the data chunks while length > offset */ 5566b1754ad1SMichael Tuexen retval = sctp_process_data(mm, iphlen, &offset, length, 5567e7e71dd7SMichael Tuexen inp, stcb, net, &high_tsn); 5568f8829a4aSRandall Stewart if (retval == 2) { 5569f8829a4aSRandall Stewart /* 5570f8829a4aSRandall Stewart * The association aborted, NO UNLOCK needed since 5571f8829a4aSRandall Stewart * the association is destroyed. 5572f8829a4aSRandall Stewart */ 5573a8775ad9SMichael Tuexen stcb = NULL; 5574a8775ad9SMichael Tuexen goto out; 5575f8829a4aSRandall Stewart } 5576b954d816SMichael Tuexen if (retval == 0) { 5577f8829a4aSRandall Stewart data_processed = 1; 5578b954d816SMichael Tuexen } 5579f8829a4aSRandall Stewart /* 5580f8829a4aSRandall Stewart * Anything important needs to have been m_copy'ed in 5581f8829a4aSRandall Stewart * process_data 5582f8829a4aSRandall Stewart */ 5583f8829a4aSRandall Stewart } 55840053ed28SMichael Tuexen 5585493d8e5aSRandall Stewart /* take care of ecn */ 558660990c0cSMichael Tuexen if ((data_processed == 1) && 5587f342355aSMichael Tuexen (stcb->asoc.ecn_supported == 1) && 5588c446091bSMichael Tuexen ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { 5589493d8e5aSRandall Stewart /* Yep, we need to add a ECNE */ 5590493d8e5aSRandall Stewart sctp_send_ecn_echo(stcb, net, high_tsn); 5591493d8e5aSRandall Stewart } 55920053ed28SMichael Tuexen 5593f8829a4aSRandall Stewart if ((data_processed == 0) && (fwd_tsn_seen)) { 55948f777478SMichael Tuexen int was_a_gap; 55958f777478SMichael Tuexen uint32_t highest_tsn; 5596f8829a4aSRandall Stewart 559720b07a4dSMichael Tuexen if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { 55988f777478SMichael Tuexen highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; 55998f777478SMichael Tuexen } else { 56008f777478SMichael Tuexen highest_tsn = stcb->asoc.highest_tsn_inside_map; 5601f8829a4aSRandall Stewart } 560220b07a4dSMichael Tuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 56038933fa13SRandall Stewart stcb->asoc.send_sack = 1; 56047215cc1bSMichael Tuexen sctp_sack_check(stcb, was_a_gap); 56058933fa13SRandall Stewart } else if (fwd_tsn_seen) { 56068933fa13SRandall Stewart stcb->asoc.send_sack = 1; 5607f8829a4aSRandall Stewart } 5608f8829a4aSRandall Stewart /* trigger send of any chunks in queue... */ 5609a5d547adSRandall Stewart trigger_send: 5610f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 5611f8829a4aSRandall Stewart sctp_audit_log(0xE0, 2); 5612f8829a4aSRandall Stewart sctp_auditing(1, inp, stcb, net); 5613f8829a4aSRandall Stewart #endif 5614ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT1, 5615ad81507eSRandall Stewart "Check for chunk output prw:%d tqe:%d tf=%d\n", 5616f8829a4aSRandall Stewart stcb->asoc.peers_rwnd, 5617f8829a4aSRandall Stewart TAILQ_EMPTY(&stcb->asoc.control_send_queue), 5618f8829a4aSRandall Stewart stcb->asoc.total_flight); 5619f8829a4aSRandall Stewart un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 5620493d8e5aSRandall Stewart if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 5621493d8e5aSRandall Stewart cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; 5622493d8e5aSRandall Stewart } 56235114dccbSMichael Tuexen if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) || 56245114dccbSMichael Tuexen cnt_ctrl_ready || 56255114dccbSMichael Tuexen stcb->asoc.trigger_reset || 562664c8fc5dSMichael Tuexen ((un_sent > 0) && 562764c8fc5dSMichael Tuexen (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) { 5628ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); 5629ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); 5630ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); 5631f8829a4aSRandall Stewart } 5632f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 5633f8829a4aSRandall Stewart sctp_audit_log(0xE0, 3); 5634f8829a4aSRandall Stewart sctp_auditing(2, inp, stcb, net); 5635f8829a4aSRandall Stewart #endif 5636a8775ad9SMichael Tuexen out: 5637a8775ad9SMichael Tuexen if (stcb != NULL) { 5638f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 5639a8775ad9SMichael Tuexen } 5640a8775ad9SMichael Tuexen if (inp_decr != NULL) { 5641a8775ad9SMichael Tuexen /* reduce ref-count */ 5642a8775ad9SMichael Tuexen SCTP_INP_WLOCK(inp_decr); 5643a8775ad9SMichael Tuexen SCTP_INP_DECR_REF(inp_decr); 5644a8775ad9SMichael Tuexen SCTP_INP_WUNLOCK(inp_decr); 5645a8775ad9SMichael Tuexen } 56466e55db54SRandall Stewart return; 5647f8829a4aSRandall Stewart } 5648f8829a4aSRandall Stewart 5649e6194c2eSMichael Tuexen #ifdef INET 5650f8829a4aSRandall Stewart void 5651af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) 5652f8829a4aSRandall Stewart { 5653139bc87fSRandall Stewart struct mbuf *m; 5654f8829a4aSRandall Stewart int iphlen; 5655ad21a364SRandall Stewart uint32_t vrf_id = 0; 5656f8829a4aSRandall Stewart uint8_t ecn_bits; 5657b1754ad1SMichael Tuexen struct sockaddr_in src, dst; 5658f8829a4aSRandall Stewart struct ip *ip; 5659f8829a4aSRandall Stewart struct sctphdr *sh; 5660f8829a4aSRandall Stewart struct sctp_chunkhdr *ch; 56616dc5aabcSMichael Tuexen int length, offset; 5662a8775ad9SMichael Tuexen uint8_t compute_crc; 5663a8775ad9SMichael Tuexen uint32_t mflowid; 5664457b4b88SMichael Tuexen uint8_t mflowtype; 5665d089f9b9SMichael Tuexen uint16_t fibnum; 56669c7635e1SMichael Tuexen 56676dc5aabcSMichael Tuexen iphlen = off; 566817205eccSRandall Stewart if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { 566917205eccSRandall Stewart SCTP_RELEASE_PKT(i_pak); 567017205eccSRandall Stewart return; 567117205eccSRandall Stewart } 5672139bc87fSRandall Stewart m = SCTP_HEADER_TO_CHAIN(i_pak); 5673f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING 5674f8829a4aSRandall Stewart /* Log in any input mbufs */ 5675b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 56764be807c4SMichael Tuexen sctp_log_mbc(m, SCTP_MBUF_INPUT); 567780fefe0aSRandall Stewart } 5678f8829a4aSRandall Stewart #endif 5679207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING 56806dc5aabcSMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { 5681f9384252SMichael Tuexen sctp_packet_log(m); 56826dc5aabcSMichael Tuexen } 5683207304d4SRandall Stewart #endif 5684a8775ad9SMichael Tuexen SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 56851a94cdbeSMichael Tuexen "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", 5686a8775ad9SMichael Tuexen m->m_pkthdr.len, 5687a8775ad9SMichael Tuexen if_name(m->m_pkthdr.rcvif), 56881a94cdbeSMichael Tuexen (int)m->m_pkthdr.csum_flags, CSUM_BITS); 5689f30ac432SMichael Tuexen mflowid = m->m_pkthdr.flowid; 5690457b4b88SMichael Tuexen mflowtype = M_HASHTYPE_GET(m); 5691d089f9b9SMichael Tuexen fibnum = M_GETFIB(m); 56926dc5aabcSMichael Tuexen SCTP_STAT_INCR(sctps_recvpackets); 56936dc5aabcSMichael Tuexen SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 56946dc5aabcSMichael Tuexen /* Get IP, SCTP, and first chunk header together in the first mbuf. */ 56956dc5aabcSMichael Tuexen offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5696139bc87fSRandall Stewart if (SCTP_BUF_LEN(m) < offset) { 5697b1754ad1SMichael Tuexen if ((m = m_pullup(m, offset)) == NULL) { 5698f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_hdrops); 5699f8829a4aSRandall Stewart return; 5700f8829a4aSRandall Stewart } 5701f8829a4aSRandall Stewart } 5702b1754ad1SMichael Tuexen ip = mtod(m, struct ip *); 57036dc5aabcSMichael Tuexen sh = (struct sctphdr *)((caddr_t)ip + iphlen); 57046dc5aabcSMichael Tuexen ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 57056dc5aabcSMichael Tuexen offset -= sizeof(struct sctp_chunkhdr); 5706b1754ad1SMichael Tuexen memset(&src, 0, sizeof(struct sockaddr_in)); 5707b1754ad1SMichael Tuexen src.sin_family = AF_INET; 5708b1754ad1SMichael Tuexen src.sin_len = sizeof(struct sockaddr_in); 5709b1754ad1SMichael Tuexen src.sin_port = sh->src_port; 5710b1754ad1SMichael Tuexen src.sin_addr = ip->ip_src; 5711b1754ad1SMichael Tuexen memset(&dst, 0, sizeof(struct sockaddr_in)); 5712b1754ad1SMichael Tuexen dst.sin_family = AF_INET; 5713b1754ad1SMichael Tuexen dst.sin_len = sizeof(struct sockaddr_in); 5714b1754ad1SMichael Tuexen dst.sin_port = sh->dest_port; 5715b1754ad1SMichael Tuexen dst.sin_addr = ip->ip_dst; 57168ad458a4SGleb Smirnoff length = ntohs(ip->ip_len); 57176dc5aabcSMichael Tuexen /* Validate mbuf chain length with IP payload length. */ 5718a8775ad9SMichael Tuexen if (SCTP_HEADER_LEN(m) != length) { 57196dc5aabcSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1, 5720a8775ad9SMichael Tuexen "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); 5721a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_hdrops); 5722a8775ad9SMichael Tuexen goto out; 5723a99b6783SRandall Stewart } 5724f8829a4aSRandall Stewart /* SCTP does not allow broadcasts or multicasts */ 5725b1754ad1SMichael Tuexen if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) { 5726a8775ad9SMichael Tuexen goto out; 5727f8829a4aSRandall Stewart } 5728b1754ad1SMichael Tuexen if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) { 5729a8775ad9SMichael Tuexen goto out; 5730f8829a4aSRandall Stewart } 5731a8775ad9SMichael Tuexen ecn_bits = ip->ip_tos; 5732a99b6783SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 5733a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_recvhwcrc); 5734a8775ad9SMichael Tuexen compute_crc = 0; 5735a8775ad9SMichael Tuexen } else { 5736a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_recvswcrc); 5737a8775ad9SMichael Tuexen compute_crc = 1; 5738f8829a4aSRandall Stewart } 5739b1754ad1SMichael Tuexen sctp_common_input_processing(&m, iphlen, offset, length, 5740b1754ad1SMichael Tuexen (struct sockaddr *)&src, 5741b1754ad1SMichael Tuexen (struct sockaddr *)&dst, 5742a8775ad9SMichael Tuexen sh, ch, 5743a8775ad9SMichael Tuexen compute_crc, 5744a8775ad9SMichael Tuexen ecn_bits, 5745d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum, 5746f30ac432SMichael Tuexen vrf_id, port); 5747a8775ad9SMichael Tuexen out: 5748f8829a4aSRandall Stewart if (m) { 5749f8829a4aSRandall Stewart sctp_m_freem(m); 5750f8829a4aSRandall Stewart } 5751f8829a4aSRandall Stewart return; 5752f8829a4aSRandall Stewart } 5753bfc46083SRandall Stewart 57542f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP) 57550071ee5eSRandall Stewart extern int *sctp_cpuarry; 57560071ee5eSRandall Stewart #endif 5757bfc46083SRandall Stewart 57588f5a8818SKevin Lo int 575982eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED) 5760c54a18d2SRandall Stewart { 57618f5a8818SKevin Lo struct mbuf *m; 57628f5a8818SKevin Lo int off; 57638f5a8818SKevin Lo 57648f5a8818SKevin Lo m = *mp; 57658f5a8818SKevin Lo off = *offp; 57662f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP) 576782eaf95eSMichael Tuexen if (mp_ncpus > 1) { 5768bfc46083SRandall Stewart struct ip *ip; 5769bfc46083SRandall Stewart struct sctphdr *sh; 5770bfc46083SRandall Stewart int offset; 5771bfc46083SRandall Stewart int cpu_to_use; 577238521fb9SRandall Stewart uint32_t flowid, tag; 5773bfc46083SRandall Stewart 5774457b4b88SMichael Tuexen if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 577538521fb9SRandall Stewart flowid = m->m_pkthdr.flowid; 577638521fb9SRandall Stewart } else { 577738521fb9SRandall Stewart /* 577838521fb9SRandall Stewart * No flow id built by lower layers fix it so we 577938521fb9SRandall Stewart * create one. 578038521fb9SRandall Stewart */ 5781b1754ad1SMichael Tuexen offset = off + sizeof(struct sctphdr); 5782bfc46083SRandall Stewart if (SCTP_BUF_LEN(m) < offset) { 5783b1754ad1SMichael Tuexen if ((m = m_pullup(m, offset)) == NULL) { 5784bfc46083SRandall Stewart SCTP_STAT_INCR(sctps_hdrops); 57858f5a8818SKevin Lo return (IPPROTO_DONE); 5786bfc46083SRandall Stewart } 5787bfc46083SRandall Stewart } 5788b1754ad1SMichael Tuexen ip = mtod(m, struct ip *); 5789bfc46083SRandall Stewart sh = (struct sctphdr *)((caddr_t)ip + off); 57900071ee5eSRandall Stewart tag = htonl(sh->v_tag); 579138521fb9SRandall Stewart flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); 579238521fb9SRandall Stewart m->m_pkthdr.flowid = flowid; 579336ad8372SSepherosa Ziehau M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH); 57940071ee5eSRandall Stewart } 579538521fb9SRandall Stewart cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; 5796bfc46083SRandall Stewart sctp_queue_to_mcore(m, off, cpu_to_use); 57978f5a8818SKevin Lo return (IPPROTO_DONE); 5798bfc46083SRandall Stewart } 5799bfc46083SRandall Stewart #endif 5800bfc46083SRandall Stewart sctp_input_with_port(m, off, 0); 58018f5a8818SKevin Lo return (IPPROTO_DONE); 5802c54a18d2SRandall Stewart } 5803e6194c2eSMichael Tuexen #endif 5804