1f8829a4aSRandall Stewart /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4b1006367SRandall Stewart * Copyright (c) 2001-2007, 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 #define _IP_VHL 3993164cf9SRandall Stewart #include <netinet/sctp_os.h> 40f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h> 41f8829a4aSRandall Stewart #ifdef INET6 42f8829a4aSRandall Stewart #endif 43f8829a4aSRandall Stewart #include <netinet/sctp_var.h> 4442551e99SRandall Stewart #include <netinet/sctp_sysctl.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_timer.h> 46f8829a4aSRandall Stewart #include <netinet/sctputil.h> 47f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 48f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 49f8829a4aSRandall Stewart #include <netinet/sctp_indata.h> 50f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h> 51f8829a4aSRandall Stewart #include <netinet/sctp_input.h> 52f8829a4aSRandall Stewart #include <netinet/sctp.h> 53f8829a4aSRandall Stewart #include <netinet/sctp_uio.h> 544474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 55830d754dSRandall Stewart #include <netinet/udp.h> 564474d71aSMichael Tuexen #endif 57f8829a4aSRandall Stewart 58f8829a4aSRandall Stewart 59f8829a4aSRandall Stewart void 60f8829a4aSRandall Stewart sctp_audit_retranmission_queue(struct sctp_association *asoc) 61f8829a4aSRandall Stewart { 62f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 63f8829a4aSRandall Stewart 64ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n", 65f8829a4aSRandall Stewart asoc->sent_queue_retran_cnt, 66f8829a4aSRandall Stewart asoc->sent_queue_cnt); 67f8829a4aSRandall Stewart asoc->sent_queue_retran_cnt = 0; 68f8829a4aSRandall Stewart asoc->sent_queue_cnt = 0; 69f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 70f8829a4aSRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 71f8829a4aSRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 72f8829a4aSRandall Stewart } 73f8829a4aSRandall Stewart asoc->sent_queue_cnt++; 74f8829a4aSRandall Stewart } 75f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 76f8829a4aSRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 77f8829a4aSRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 78f8829a4aSRandall Stewart } 79f8829a4aSRandall Stewart } 80c54a18d2SRandall Stewart TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) { 81c54a18d2SRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 82c54a18d2SRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 83c54a18d2SRandall Stewart } 84c54a18d2SRandall Stewart } 85ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n", 86f8829a4aSRandall Stewart asoc->sent_queue_retran_cnt, 87f8829a4aSRandall Stewart asoc->sent_queue_cnt); 88f8829a4aSRandall Stewart } 89f8829a4aSRandall Stewart 90815f806bSMichael Tuexen static int 91f8829a4aSRandall Stewart sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 92f8829a4aSRandall Stewart struct sctp_nets *net, uint16_t threshold) 93f8829a4aSRandall Stewart { 94f8829a4aSRandall Stewart if (net) { 95f8829a4aSRandall Stewart net->error_count++; 96ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n", 97dd294dceSMichael Tuexen (void *)net, net->error_count, 98f8829a4aSRandall Stewart net->failure_threshold); 99f8829a4aSRandall Stewart if (net->error_count > net->failure_threshold) { 100f8829a4aSRandall Stewart /* We had a threshold failure */ 101f8829a4aSRandall Stewart if (net->dest_state & SCTP_ADDR_REACHABLE) { 102f8829a4aSRandall Stewart net->dest_state &= ~SCTP_ADDR_REACHABLE; 10342551e99SRandall Stewart net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; 104b54d3a6cSRandall Stewart net->dest_state &= ~SCTP_ADDR_PF; 105f8829a4aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 1064b1f78e1SMichael Tuexen stcb, 0, 107ceaad40aSRandall Stewart (void *)net, SCTP_SO_NOT_LOCKED); 108f8829a4aSRandall Stewart } 109ca85e948SMichael Tuexen } else if ((net->pf_threshold < net->failure_threshold) && 110ca85e948SMichael Tuexen (net->error_count > net->pf_threshold)) { 111ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_PF)) { 112ca85e948SMichael Tuexen net->dest_state |= SCTP_ADDR_PF; 113ca85e948SMichael Tuexen net->last_active = sctp_get_tick_count(); 114ca85e948SMichael Tuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 115b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, 116815f806bSMichael Tuexen inp, stcb, net, 117b7d130beSMichael Tuexen SCTP_FROM_SCTP_TIMER + SCTP_LOC_1); 118815f806bSMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 119f8829a4aSRandall Stewart } 120ca85e948SMichael Tuexen } 121f8829a4aSRandall Stewart } 122f8829a4aSRandall Stewart if (stcb == NULL) 123f8829a4aSRandall Stewart return (0); 124f8829a4aSRandall Stewart 125f8829a4aSRandall Stewart if (net) { 126f8829a4aSRandall Stewart if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) { 127b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 128c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_INCR, 129c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 130c4739e2fSRandall Stewart (stcb->asoc.overall_error_count + 1), 131c4739e2fSRandall Stewart SCTP_FROM_SCTP_TIMER, 132c4739e2fSRandall Stewart __LINE__); 133c4739e2fSRandall Stewart } 134f8829a4aSRandall Stewart stcb->asoc.overall_error_count++; 135f8829a4aSRandall Stewart } 136f8829a4aSRandall Stewart } else { 137b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 138c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_INCR, 139c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 140c4739e2fSRandall Stewart (stcb->asoc.overall_error_count + 1), 141c4739e2fSRandall Stewart SCTP_FROM_SCTP_TIMER, 142c4739e2fSRandall Stewart __LINE__); 143c4739e2fSRandall Stewart } 144f8829a4aSRandall Stewart stcb->asoc.overall_error_count++; 145f8829a4aSRandall Stewart } 146ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n", 147dd294dceSMichael Tuexen (void *)&stcb->asoc, stcb->asoc.overall_error_count, 148f8829a4aSRandall Stewart (uint32_t)threshold, 149f8829a4aSRandall Stewart ((net == NULL) ? (uint32_t)0 : (uint32_t)net->dest_state)); 150f8829a4aSRandall Stewart /* 151f8829a4aSRandall Stewart * We specifically do not do >= to give the assoc one more change 152f8829a4aSRandall Stewart * before we fail it. 153f8829a4aSRandall Stewart */ 154f8829a4aSRandall Stewart if (stcb->asoc.overall_error_count > threshold) { 155f8829a4aSRandall Stewart /* Abort notification sends a ULP notify */ 156ff1ffd74SMichael Tuexen struct mbuf *op_err; 157f8829a4aSRandall Stewart 158267dbe63SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 1598f290ed5SMichael Tuexen "Association error counter exceeded"); 160b7d130beSMichael Tuexen inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2; 161ff1ffd74SMichael Tuexen sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED); 162f8829a4aSRandall Stewart return (1); 163f8829a4aSRandall Stewart } 164f8829a4aSRandall Stewart return (0); 165f8829a4aSRandall Stewart } 166f8829a4aSRandall Stewart 167b61c3588SMichael Tuexen /* 168b61c3588SMichael Tuexen * sctp_find_alternate_net() returns a non-NULL pointer as long 169b61c3588SMichael Tuexen * the argument net is non-NULL. 170b61c3588SMichael Tuexen */ 171f8829a4aSRandall Stewart struct sctp_nets * 172f8829a4aSRandall Stewart sctp_find_alternate_net(struct sctp_tcb *stcb, 173f8829a4aSRandall Stewart struct sctp_nets *net, 174b54d3a6cSRandall Stewart int mode) 175f8829a4aSRandall Stewart { 176f8829a4aSRandall Stewart /* Find and return an alternate network if possible */ 177b54d3a6cSRandall Stewart struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL; 178f8829a4aSRandall Stewart int once; 179b54d3a6cSRandall Stewart 180b54d3a6cSRandall Stewart /* JRS 5/14/07 - Initialize min_errors to an impossible value. */ 181b54d3a6cSRandall Stewart int min_errors = -1; 182b54d3a6cSRandall Stewart uint32_t max_cwnd = 0; 183f8829a4aSRandall Stewart 184f8829a4aSRandall Stewart if (stcb->asoc.numnets == 1) { 185f8829a4aSRandall Stewart /* No others but net */ 186f8829a4aSRandall Stewart return (TAILQ_FIRST(&stcb->asoc.nets)); 187f8829a4aSRandall Stewart } 188b54d3a6cSRandall Stewart /* 189b54d3a6cSRandall Stewart * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate 190b54d3a6cSRandall Stewart * net algorithm. This algorithm chooses the active destination (not 191b54d3a6cSRandall Stewart * in PF state) with the largest cwnd value. If all destinations are 192b54d3a6cSRandall Stewart * in PF state, unreachable, or unconfirmed, choose the desination 193b54d3a6cSRandall Stewart * that is in PF state with the lowest error count. In case of a 194b54d3a6cSRandall Stewart * tie, choose the destination that was most recently active. 195b54d3a6cSRandall Stewart */ 196b54d3a6cSRandall Stewart if (mode == 2) { 197b54d3a6cSRandall Stewart TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) { 198b7b84c0eSMichael Tuexen /* 199b7b84c0eSMichael Tuexen * JRS 5/14/07 - If the destination is unreachable 200b7b84c0eSMichael Tuexen * or unconfirmed, skip it. 201b7b84c0eSMichael Tuexen */ 202b54d3a6cSRandall Stewart if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) || 203b54d3a6cSRandall Stewart (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) { 204b54d3a6cSRandall Stewart continue; 205b54d3a6cSRandall Stewart } 206b54d3a6cSRandall Stewart /* 207b54d3a6cSRandall Stewart * JRS 5/14/07 - If the destination is reachable 208b54d3a6cSRandall Stewart * but in PF state, compare the error count of the 209b54d3a6cSRandall Stewart * destination to the minimum error count seen thus 210b54d3a6cSRandall Stewart * far. Store the destination with the lower error 211b54d3a6cSRandall Stewart * count. If the error counts are equal, store the 212b54d3a6cSRandall Stewart * destination that was most recently active. 213b54d3a6cSRandall Stewart */ 214b54d3a6cSRandall Stewart if (mnet->dest_state & SCTP_ADDR_PF) { 215b54d3a6cSRandall Stewart /* 216b54d3a6cSRandall Stewart * JRS 5/14/07 - If the destination under 217b54d3a6cSRandall Stewart * consideration is the current destination, 218b54d3a6cSRandall Stewart * work as if the error count is one higher. 219b54d3a6cSRandall Stewart * The actual error count will not be 220b54d3a6cSRandall Stewart * incremented until later in the t3 221b54d3a6cSRandall Stewart * handler. 222b54d3a6cSRandall Stewart */ 223b54d3a6cSRandall Stewart if (mnet == net) { 224b54d3a6cSRandall Stewart if (min_errors == -1) { 225b54d3a6cSRandall Stewart min_errors = mnet->error_count + 1; 226b54d3a6cSRandall Stewart min_errors_net = mnet; 227b54d3a6cSRandall Stewart } else if (mnet->error_count + 1 < min_errors) { 228b54d3a6cSRandall Stewart min_errors = mnet->error_count + 1; 229b54d3a6cSRandall Stewart min_errors_net = mnet; 230b54d3a6cSRandall Stewart } else if (mnet->error_count + 1 == min_errors 231b54d3a6cSRandall Stewart && mnet->last_active > min_errors_net->last_active) { 232b54d3a6cSRandall Stewart min_errors_net = mnet; 233b54d3a6cSRandall Stewart min_errors = mnet->error_count + 1; 234b54d3a6cSRandall Stewart } 235b54d3a6cSRandall Stewart continue; 236b54d3a6cSRandall Stewart } else { 237b54d3a6cSRandall Stewart if (min_errors == -1) { 238b54d3a6cSRandall Stewart min_errors = mnet->error_count; 239b54d3a6cSRandall Stewart min_errors_net = mnet; 240b54d3a6cSRandall Stewart } else if (mnet->error_count < min_errors) { 241b54d3a6cSRandall Stewart min_errors = mnet->error_count; 242b54d3a6cSRandall Stewart min_errors_net = mnet; 243b54d3a6cSRandall Stewart } else if (mnet->error_count == min_errors 244b54d3a6cSRandall Stewart && mnet->last_active > min_errors_net->last_active) { 245b54d3a6cSRandall Stewart min_errors_net = mnet; 246b54d3a6cSRandall Stewart min_errors = mnet->error_count; 247b54d3a6cSRandall Stewart } 248b54d3a6cSRandall Stewart continue; 249b54d3a6cSRandall Stewart } 250b54d3a6cSRandall Stewart } 251b54d3a6cSRandall Stewart /* 252b54d3a6cSRandall Stewart * JRS 5/14/07 - If the destination is reachable and 253b54d3a6cSRandall Stewart * not in PF state, compare the cwnd of the 254b54d3a6cSRandall Stewart * destination to the highest cwnd seen thus far. 255b54d3a6cSRandall Stewart * Store the destination with the higher cwnd value. 256b54d3a6cSRandall Stewart * If the cwnd values are equal, randomly choose one 257b54d3a6cSRandall Stewart * of the two destinations. 258b54d3a6cSRandall Stewart */ 259b54d3a6cSRandall Stewart if (max_cwnd < mnet->cwnd) { 260b54d3a6cSRandall Stewart max_cwnd_net = mnet; 261b54d3a6cSRandall Stewart max_cwnd = mnet->cwnd; 262b54d3a6cSRandall Stewart } else if (max_cwnd == mnet->cwnd) { 263b54d3a6cSRandall Stewart uint32_t rndval; 264b54d3a6cSRandall Stewart uint8_t this_random; 265b54d3a6cSRandall Stewart 266b54d3a6cSRandall Stewart if (stcb->asoc.hb_random_idx > 3) { 267b54d3a6cSRandall Stewart rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); 268b54d3a6cSRandall Stewart memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values)); 269b54d3a6cSRandall Stewart this_random = stcb->asoc.hb_random_values[0]; 270b54d3a6cSRandall Stewart stcb->asoc.hb_random_idx++; 271b54d3a6cSRandall Stewart stcb->asoc.hb_ect_randombit = 0; 272b54d3a6cSRandall Stewart } else { 273b54d3a6cSRandall Stewart this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx]; 274b54d3a6cSRandall Stewart stcb->asoc.hb_random_idx++; 275b54d3a6cSRandall Stewart stcb->asoc.hb_ect_randombit = 0; 276b54d3a6cSRandall Stewart } 277b54d3a6cSRandall Stewart if (this_random % 2 == 1) { 278b54d3a6cSRandall Stewart max_cwnd_net = mnet; 279fc14de76SRandall Stewart max_cwnd = mnet->cwnd; /* Useless? */ 280b54d3a6cSRandall Stewart } 281b54d3a6cSRandall Stewart } 282b54d3a6cSRandall Stewart } 283b54d3a6cSRandall Stewart if (max_cwnd_net == NULL) { 284b54d3a6cSRandall Stewart if (min_errors_net == NULL) { 285b54d3a6cSRandall Stewart return (net); 286b54d3a6cSRandall Stewart } 287b54d3a6cSRandall Stewart return (min_errors_net); 288b54d3a6cSRandall Stewart } else { 289b54d3a6cSRandall Stewart return (max_cwnd_net); 290b54d3a6cSRandall Stewart } 2915b495f17SMichael Tuexen } /* JRS 5/14/07 - If mode is set to 1, use the 2925b495f17SMichael Tuexen * CMT policy for choosing an alternate net. */ 293b54d3a6cSRandall Stewart else if (mode == 1) { 294f8829a4aSRandall Stewart TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) { 295f8829a4aSRandall Stewart if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) || 296b61c3588SMichael Tuexen (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) { 297f8829a4aSRandall Stewart /* 298f8829a4aSRandall Stewart * will skip ones that are not-reachable or 299f8829a4aSRandall Stewart * unconfirmed 300f8829a4aSRandall Stewart */ 301f8829a4aSRandall Stewart continue; 302f8829a4aSRandall Stewart } 303b54d3a6cSRandall Stewart if (max_cwnd < mnet->cwnd) { 304b54d3a6cSRandall Stewart max_cwnd_net = mnet; 305b54d3a6cSRandall Stewart max_cwnd = mnet->cwnd; 306b54d3a6cSRandall Stewart } else if (max_cwnd == mnet->cwnd) { 307f8829a4aSRandall Stewart uint32_t rndval; 308f8829a4aSRandall Stewart uint8_t this_random; 309f8829a4aSRandall Stewart 310f8829a4aSRandall Stewart if (stcb->asoc.hb_random_idx > 3) { 311f8829a4aSRandall Stewart rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); 312f8829a4aSRandall Stewart memcpy(stcb->asoc.hb_random_values, &rndval, 313f8829a4aSRandall Stewart sizeof(stcb->asoc.hb_random_values)); 314f8829a4aSRandall Stewart this_random = stcb->asoc.hb_random_values[0]; 315f8829a4aSRandall Stewart stcb->asoc.hb_random_idx = 0; 316f8829a4aSRandall Stewart stcb->asoc.hb_ect_randombit = 0; 317f8829a4aSRandall Stewart } else { 318f8829a4aSRandall Stewart this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx]; 319f8829a4aSRandall Stewart stcb->asoc.hb_random_idx++; 320f8829a4aSRandall Stewart stcb->asoc.hb_ect_randombit = 0; 321f8829a4aSRandall Stewart } 322f8829a4aSRandall Stewart if (this_random % 2) { 323b54d3a6cSRandall Stewart max_cwnd_net = mnet; 324b54d3a6cSRandall Stewart max_cwnd = mnet->cwnd; 325f8829a4aSRandall Stewart } 326f8829a4aSRandall Stewart } 327f8829a4aSRandall Stewart } 328b54d3a6cSRandall Stewart if (max_cwnd_net) { 329b54d3a6cSRandall Stewart return (max_cwnd_net); 330f8829a4aSRandall Stewart } 331f8829a4aSRandall Stewart } 332f8829a4aSRandall Stewart mnet = net; 333f8829a4aSRandall Stewart once = 0; 334f8829a4aSRandall Stewart 335f8829a4aSRandall Stewart if (mnet == NULL) { 336f8829a4aSRandall Stewart mnet = TAILQ_FIRST(&stcb->asoc.nets); 33752129fcdSRandall Stewart if (mnet == NULL) { 33852129fcdSRandall Stewart return (NULL); 33952129fcdSRandall Stewart } 340f8829a4aSRandall Stewart } 3412010054dSMichael Tuexen for (;;) { 342f8829a4aSRandall Stewart alt = TAILQ_NEXT(mnet, sctp_next); 343f8829a4aSRandall Stewart if (alt == NULL) { 344f8829a4aSRandall Stewart once++; 345f8829a4aSRandall Stewart if (once > 1) { 346f8829a4aSRandall Stewart break; 347f8829a4aSRandall Stewart } 348f8829a4aSRandall Stewart alt = TAILQ_FIRST(&stcb->asoc.nets); 34952129fcdSRandall Stewart if (alt == NULL) { 35052129fcdSRandall Stewart return (NULL); 35152129fcdSRandall Stewart } 352f8829a4aSRandall Stewart } 353f8829a4aSRandall Stewart if (alt->ro.ro_rt == NULL) { 35442551e99SRandall Stewart if (alt->ro._s_addr) { 35542551e99SRandall Stewart sctp_free_ifa(alt->ro._s_addr); 35642551e99SRandall Stewart alt->ro._s_addr = NULL; 35742551e99SRandall Stewart } 358f8829a4aSRandall Stewart alt->src_addr_selected = 0; 359f8829a4aSRandall Stewart } 360b61c3588SMichael Tuexen if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) && 361b61c3588SMichael Tuexen (alt->ro.ro_rt != NULL) && 362b61c3588SMichael Tuexen (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) { 363f8829a4aSRandall Stewart /* Found a reachable address */ 364f8829a4aSRandall Stewart break; 365f8829a4aSRandall Stewart } 366f8829a4aSRandall Stewart mnet = alt; 3672010054dSMichael Tuexen } 368f8829a4aSRandall Stewart 369f8829a4aSRandall Stewart if (alt == NULL) { 370f8829a4aSRandall Stewart /* Case where NO insv network exists (dormant state) */ 371f8829a4aSRandall Stewart /* we rotate destinations */ 372f8829a4aSRandall Stewart once = 0; 373f8829a4aSRandall Stewart mnet = net; 3742010054dSMichael Tuexen for (;;) { 37552129fcdSRandall Stewart if (mnet == NULL) { 37652129fcdSRandall Stewart return (TAILQ_FIRST(&stcb->asoc.nets)); 37752129fcdSRandall Stewart } 378f8829a4aSRandall Stewart alt = TAILQ_NEXT(mnet, sctp_next); 379f8829a4aSRandall Stewart if (alt == NULL) { 380f8829a4aSRandall Stewart once++; 381f8829a4aSRandall Stewart if (once > 1) { 382f8829a4aSRandall Stewart break; 383f8829a4aSRandall Stewart } 384f8829a4aSRandall Stewart alt = TAILQ_FIRST(&stcb->asoc.nets); 3852010054dSMichael Tuexen if (alt == NULL) { 3862010054dSMichael Tuexen break; 387f8829a4aSRandall Stewart } 3882010054dSMichael Tuexen } 389f8829a4aSRandall Stewart if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) && 390f8829a4aSRandall Stewart (alt != net)) { 391f8829a4aSRandall Stewart /* Found an alternate address */ 392f8829a4aSRandall Stewart break; 393f8829a4aSRandall Stewart } 394f8829a4aSRandall Stewart mnet = alt; 3952010054dSMichael Tuexen } 396f8829a4aSRandall Stewart } 397f8829a4aSRandall Stewart if (alt == NULL) { 398f8829a4aSRandall Stewart return (net); 399f8829a4aSRandall Stewart } 400f8829a4aSRandall Stewart return (alt); 401f8829a4aSRandall Stewart } 402f8829a4aSRandall Stewart 403f8829a4aSRandall Stewart static void 404f8829a4aSRandall Stewart sctp_backoff_on_timeout(struct sctp_tcb *stcb, 405f8829a4aSRandall Stewart struct sctp_nets *net, 406f8829a4aSRandall Stewart int win_probe, 40744fbe462SRandall Stewart int num_marked, int num_abandoned) 408f8829a4aSRandall Stewart { 4099a972525SRandall Stewart if (net->RTO == 0) { 41093725308SMichael Tuexen if (net->RTO_measured) { 4119a972525SRandall Stewart net->RTO = stcb->asoc.minrto; 41293725308SMichael Tuexen } else { 41393725308SMichael Tuexen net->RTO = stcb->asoc.initial_rto; 41493725308SMichael Tuexen } 4159a972525SRandall Stewart } 416f8829a4aSRandall Stewart net->RTO <<= 1; 417f8829a4aSRandall Stewart if (net->RTO > stcb->asoc.maxrto) { 418f8829a4aSRandall Stewart net->RTO = stcb->asoc.maxrto; 419f8829a4aSRandall Stewart } 42044fbe462SRandall Stewart if ((win_probe == 0) && (num_marked || num_abandoned)) { 421f8829a4aSRandall Stewart /* We don't apply penalty to window probe scenarios */ 422b54d3a6cSRandall Stewart /* JRS - Use the congestion control given in the CC module */ 423b54d3a6cSRandall Stewart stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net); 424f8829a4aSRandall Stewart } 425f8829a4aSRandall Stewart } 426f8829a4aSRandall Stewart 42783416c88SRandall Stewart #ifndef INVARIANTS 42883416c88SRandall Stewart static void 429df6e0cc3SRandall Stewart sctp_recover_sent_list(struct sctp_tcb *stcb) 430df6e0cc3SRandall Stewart { 4314a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 432df6e0cc3SRandall Stewart struct sctp_association *asoc; 433df6e0cc3SRandall Stewart 434df6e0cc3SRandall Stewart asoc = &stcb->asoc; 4354a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { 43649656eefSMichael Tuexen if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.tsn)) { 437df6e0cc3SRandall Stewart SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n", 43849656eefSMichael Tuexen (void *)chk, chk->rec.data.tsn, asoc->last_acked_seq); 439325c8c46SMichael Tuexen if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { 44049656eefSMichael Tuexen if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 44149656eefSMichael Tuexen asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 442a7ad6026SMichael Tuexen } 443a7ad6026SMichael Tuexen } 44449656eefSMichael Tuexen if ((asoc->strmout[chk->rec.data.sid].chunks_on_queues == 0) && 44549656eefSMichael Tuexen (asoc->strmout[chk->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) && 44649656eefSMichael Tuexen TAILQ_EMPTY(&asoc->strmout[chk->rec.data.sid].outqueue)) { 447d96bef9cSMichael Tuexen asoc->trigger_reset = 1; 448d96bef9cSMichael Tuexen } 449df6e0cc3SRandall Stewart TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 4500ddb4299SMichael Tuexen if (PR_SCTP_ENABLED(chk->flags)) { 451df6e0cc3SRandall Stewart if (asoc->pr_sctp_cnt != 0) 452df6e0cc3SRandall Stewart asoc->pr_sctp_cnt--; 453df6e0cc3SRandall Stewart } 454df6e0cc3SRandall Stewart if (chk->data) { 455df6e0cc3SRandall Stewart /* sa_ignore NO_NULL_CHK */ 456df6e0cc3SRandall Stewart sctp_free_bufspace(stcb, asoc, chk, 1); 457df6e0cc3SRandall Stewart sctp_m_freem(chk->data); 4584a9ef3f8SMichael Tuexen chk->data = NULL; 459dd973b0eSMichael Tuexen if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) { 460df6e0cc3SRandall Stewart asoc->sent_queue_cnt_removeable--; 461df6e0cc3SRandall Stewart } 462df6e0cc3SRandall Stewart } 463df6e0cc3SRandall Stewart asoc->sent_queue_cnt--; 464689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 465df6e0cc3SRandall Stewart } 466df6e0cc3SRandall Stewart } 467df6e0cc3SRandall Stewart SCTP_PRINTF("after recover order is as follows\n"); 4684a9ef3f8SMichael Tuexen TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 46949656eefSMichael Tuexen SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.tsn); 470df6e0cc3SRandall Stewart } 471df6e0cc3SRandall Stewart } 47283416c88SRandall Stewart #endif 47383416c88SRandall Stewart 474f8829a4aSRandall Stewart static int 475f8829a4aSRandall Stewart sctp_mark_all_for_resend(struct sctp_tcb *stcb, 476f8829a4aSRandall Stewart struct sctp_nets *net, 477f8829a4aSRandall Stewart struct sctp_nets *alt, 478f8829a4aSRandall Stewart int window_probe, 47944fbe462SRandall Stewart int *num_marked, 48044fbe462SRandall Stewart int *num_abandoned) 481f8829a4aSRandall Stewart { 482f8829a4aSRandall Stewart 483f8829a4aSRandall Stewart /* 484f8829a4aSRandall Stewart * Mark all chunks (well not all) that were sent to *net for 485f8829a4aSRandall Stewart * retransmission. Move them to alt for there destination as well... 486f8829a4aSRandall Stewart * We only mark chunks that have been outstanding long enough to 487f8829a4aSRandall Stewart * have received feed-back. 488f8829a4aSRandall Stewart */ 4894a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 490f8829a4aSRandall Stewart struct sctp_nets *lnets; 491f8829a4aSRandall Stewart struct timeval now, min_wait, tv; 4920191fb6dSMichael Tuexen int cur_rto; 49344fbe462SRandall Stewart int cnt_abandoned; 494c105859eSRandall Stewart int audit_tf, num_mk, fir; 495f8829a4aSRandall Stewart unsigned int cnt_mk; 496c105859eSRandall Stewart uint32_t orig_flight, orig_tf; 497f8829a4aSRandall Stewart uint32_t tsnlast, tsnfirst; 498df6e0cc3SRandall Stewart int recovery_cnt = 0; 499f8829a4aSRandall Stewart 500b54d3a6cSRandall Stewart 501f8829a4aSRandall Stewart /* none in flight now */ 502f8829a4aSRandall Stewart audit_tf = 0; 503f8829a4aSRandall Stewart fir = 0; 504f8829a4aSRandall Stewart /* 505f8829a4aSRandall Stewart * figure out how long a data chunk must be pending before we can 506f8829a4aSRandall Stewart * mark it .. 507f8829a4aSRandall Stewart */ 5086e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 509f8829a4aSRandall Stewart /* get cur rto in micro-seconds */ 5100191fb6dSMichael Tuexen cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv; 5110191fb6dSMichael Tuexen cur_rto *= 1000; 512ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 5130191fb6dSMichael Tuexen sctp_log_fr(cur_rto, 514f8829a4aSRandall Stewart stcb->asoc.peers_rwnd, 515f8829a4aSRandall Stewart window_probe, 516f8829a4aSRandall Stewart SCTP_FR_T3_MARK_TIME); 517ca85e948SMichael Tuexen sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT); 518f8829a4aSRandall Stewart sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT); 51980fefe0aSRandall Stewart } 5200191fb6dSMichael Tuexen tv.tv_sec = cur_rto / 1000000; 5210191fb6dSMichael Tuexen tv.tv_usec = cur_rto % 1000000; 522f8829a4aSRandall Stewart min_wait = now; 523f8829a4aSRandall Stewart timevalsub(&min_wait, &tv); 524f8829a4aSRandall Stewart if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) { 525f8829a4aSRandall Stewart /* 526f8829a4aSRandall Stewart * if we hit here, we don't have enough seconds on the clock 527f8829a4aSRandall Stewart * to account for the RTO. We just let the lower seconds be 528f8829a4aSRandall Stewart * the bounds and don't worry about it. This may mean we 529f8829a4aSRandall Stewart * will mark a lot more than we should. 530f8829a4aSRandall Stewart */ 531f8829a4aSRandall Stewart min_wait.tv_sec = min_wait.tv_usec = 0; 532f8829a4aSRandall Stewart } 533ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 5340191fb6dSMichael Tuexen sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME); 535f8829a4aSRandall Stewart sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME); 53680fefe0aSRandall Stewart } 537f8829a4aSRandall Stewart /* 538f8829a4aSRandall Stewart * Our rwnd will be incorrect here since we are not adding back the 539f8829a4aSRandall Stewart * cnt * mbuf but we will fix that down below. 540f8829a4aSRandall Stewart */ 541f8829a4aSRandall Stewart orig_flight = net->flight_size; 542c105859eSRandall Stewart orig_tf = stcb->asoc.total_flight; 543c105859eSRandall Stewart 544f8829a4aSRandall Stewart net->fast_retran_ip = 0; 545f8829a4aSRandall Stewart /* Now on to each chunk */ 54644fbe462SRandall Stewart cnt_abandoned = 0; 547f8829a4aSRandall Stewart num_mk = cnt_mk = 0; 548f8829a4aSRandall Stewart tsnfirst = tsnlast = 0; 54983416c88SRandall Stewart #ifndef INVARIANTS 550df6e0cc3SRandall Stewart start_again: 55183416c88SRandall Stewart #endif 5524a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) { 55349656eefSMichael Tuexen if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.tsn)) { 554f8829a4aSRandall Stewart /* Strange case our list got out of order? */ 5556ed72810SMichael Tuexen SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n", 55649656eefSMichael Tuexen (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.tsn); 557df6e0cc3SRandall Stewart recovery_cnt++; 558df6e0cc3SRandall Stewart #ifdef INVARIANTS 559df6e0cc3SRandall Stewart panic("last acked >= chk on sent-Q"); 560df6e0cc3SRandall Stewart #else 561df6e0cc3SRandall Stewart SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt); 562df6e0cc3SRandall Stewart sctp_recover_sent_list(stcb); 563df6e0cc3SRandall Stewart if (recovery_cnt < 10) { 564df6e0cc3SRandall Stewart goto start_again; 565df6e0cc3SRandall Stewart } else { 566df6e0cc3SRandall Stewart SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt); 567df6e0cc3SRandall Stewart } 568df6e0cc3SRandall Stewart #endif 569f8829a4aSRandall Stewart } 570f8829a4aSRandall Stewart if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) { 571f8829a4aSRandall Stewart /* 572f8829a4aSRandall Stewart * found one to mark: If it is less than 573f8829a4aSRandall Stewart * DATAGRAM_ACKED it MUST not be a skipped or marked 574f8829a4aSRandall Stewart * TSN but instead one that is either already set 575f8829a4aSRandall Stewart * for retransmission OR one that needs 576f8829a4aSRandall Stewart * retransmission. 577f8829a4aSRandall Stewart */ 578f8829a4aSRandall Stewart 579f8829a4aSRandall Stewart /* validate its been outstanding long enough */ 580ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 58149656eefSMichael Tuexen sctp_log_fr(chk->rec.data.tsn, 582f8829a4aSRandall Stewart chk->sent_rcv_time.tv_sec, 583f8829a4aSRandall Stewart chk->sent_rcv_time.tv_usec, 584f8829a4aSRandall Stewart SCTP_FR_T3_MARK_TIME); 58580fefe0aSRandall Stewart } 586f8829a4aSRandall Stewart if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) { 587f8829a4aSRandall Stewart /* 588f8829a4aSRandall Stewart * we have reached a chunk that was sent 589f8829a4aSRandall Stewart * some seconds past our min.. forget it we 590f8829a4aSRandall Stewart * will find no more to send. 591f8829a4aSRandall Stewart */ 592ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 593f8829a4aSRandall Stewart sctp_log_fr(0, 594f8829a4aSRandall Stewart chk->sent_rcv_time.tv_sec, 595f8829a4aSRandall Stewart chk->sent_rcv_time.tv_usec, 596f8829a4aSRandall Stewart SCTP_FR_T3_STOPPED); 59780fefe0aSRandall Stewart } 598f8829a4aSRandall Stewart continue; 599f8829a4aSRandall Stewart } else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) && 600f8829a4aSRandall Stewart (window_probe == 0)) { 601f8829a4aSRandall Stewart /* 602f8829a4aSRandall Stewart * we must look at the micro seconds to 603f8829a4aSRandall Stewart * know. 604f8829a4aSRandall Stewart */ 605f8829a4aSRandall Stewart if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) { 606f8829a4aSRandall Stewart /* 607f8829a4aSRandall Stewart * ok it was sent after our boundary 608f8829a4aSRandall Stewart * time. 609f8829a4aSRandall Stewart */ 610f8829a4aSRandall Stewart continue; 611f8829a4aSRandall Stewart } 612f8829a4aSRandall Stewart } 613dd973b0eSMichael Tuexen if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) { 614f8829a4aSRandall Stewart /* Is it expired? */ 61599ddc825SMichael Tuexen if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) { 616f8829a4aSRandall Stewart /* Yes so drop it */ 617f8829a4aSRandall Stewart if (chk->data) { 618ad81507eSRandall Stewart (void)sctp_release_pr_sctp_chunk(stcb, 619f8829a4aSRandall Stewart chk, 6201edc9dbaSMichael Tuexen 1, 6210c0982b8SRandall Stewart SCTP_SO_NOT_LOCKED); 62244fbe462SRandall Stewart cnt_abandoned++; 623f8829a4aSRandall Stewart } 624f8829a4aSRandall Stewart continue; 625f8829a4aSRandall Stewart } 626830d754dSRandall Stewart } 627dd973b0eSMichael Tuexen if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) { 628f8829a4aSRandall Stewart /* Has it been retransmitted tv_sec times? */ 629f8829a4aSRandall Stewart if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) { 630f8829a4aSRandall Stewart if (chk->data) { 631ad81507eSRandall Stewart (void)sctp_release_pr_sctp_chunk(stcb, 632f8829a4aSRandall Stewart chk, 6331edc9dbaSMichael Tuexen 1, 6340c0982b8SRandall Stewart SCTP_SO_NOT_LOCKED); 63544fbe462SRandall Stewart cnt_abandoned++; 636f8829a4aSRandall Stewart } 637f8829a4aSRandall Stewart continue; 638f8829a4aSRandall Stewart } 639830d754dSRandall Stewart } 640c105859eSRandall Stewart if (chk->sent < SCTP_DATAGRAM_RESEND) { 641f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 642f8829a4aSRandall Stewart num_mk++; 643f8829a4aSRandall Stewart if (fir == 0) { 644f8829a4aSRandall Stewart fir = 1; 64549656eefSMichael Tuexen tsnfirst = chk->rec.data.tsn; 646f8829a4aSRandall Stewart } 64749656eefSMichael Tuexen tsnlast = chk->rec.data.tsn; 648ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 64949656eefSMichael Tuexen sctp_log_fr(chk->rec.data.tsn, chk->snd_count, 650f8829a4aSRandall Stewart 0, SCTP_FR_T3_MARKED); 65180fefe0aSRandall Stewart } 6520053ed28SMichael Tuexen 65342551e99SRandall Stewart if (chk->rec.data.chunk_was_revoked) { 65442551e99SRandall Stewart /* deflate the cwnd */ 65542551e99SRandall Stewart chk->whoTo->cwnd -= chk->book_size; 65642551e99SRandall Stewart chk->rec.data.chunk_was_revoked = 0; 65742551e99SRandall Stewart } 658f42a358aSRandall Stewart net->marked_retrans++; 659f42a358aSRandall Stewart stcb->asoc.marked_retrans++; 660b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 661c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO, 662a5d547adSRandall Stewart chk->whoTo->flight_size, 663a5d547adSRandall Stewart chk->book_size, 6649a8e3088SMichael Tuexen (uint32_t)(uintptr_t)chk->whoTo, 66549656eefSMichael Tuexen chk->rec.data.tsn); 66680fefe0aSRandall Stewart } 667c105859eSRandall Stewart sctp_flight_size_decrease(chk); 668c105859eSRandall Stewart sctp_total_flight_decrease(stcb, chk); 669f8829a4aSRandall Stewart stcb->asoc.peers_rwnd += chk->send_size; 670b3f1ea41SRandall Stewart stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh); 671c105859eSRandall Stewart } 672c105859eSRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 6734f43a14aSMichael Tuexen chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 674c105859eSRandall Stewart SCTP_STAT_INCR(sctps_markedretrans); 675f8829a4aSRandall Stewart 676f8829a4aSRandall Stewart /* reset the TSN for striking and other FR stuff */ 677f8829a4aSRandall Stewart chk->rec.data.doing_fast_retransmit = 0; 678f8829a4aSRandall Stewart /* Clear any time so NO RTT is being done */ 679f79aab18SRandall Stewart 680f79aab18SRandall Stewart if (chk->do_rtt) { 681f79aab18SRandall Stewart if (chk->whoTo->rto_needed == 0) { 682f79aab18SRandall Stewart chk->whoTo->rto_needed = 1; 683f79aab18SRandall Stewart } 684f79aab18SRandall Stewart } 685f8829a4aSRandall Stewart chk->do_rtt = 0; 686f8829a4aSRandall Stewart if (alt != net) { 687f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 688f8829a4aSRandall Stewart chk->no_fr_allowed = 1; 689f8829a4aSRandall Stewart chk->whoTo = alt; 690f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 691f8829a4aSRandall Stewart } else { 692f8829a4aSRandall Stewart chk->no_fr_allowed = 0; 693f8829a4aSRandall Stewart if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { 694f8829a4aSRandall Stewart chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; 695f8829a4aSRandall Stewart } else { 69649656eefSMichael Tuexen chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn; 697f8829a4aSRandall Stewart } 698f8829a4aSRandall Stewart } 699ad21a364SRandall Stewart /* 700ad21a364SRandall Stewart * CMT: Do not allow FRs on retransmitted TSNs. 701ad21a364SRandall Stewart */ 7027c99d56fSMichael Tuexen if (stcb->asoc.sctp_cmt_on_off > 0) { 703f8829a4aSRandall Stewart chk->no_fr_allowed = 1; 704f8829a4aSRandall Stewart } 70544fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE 706f8829a4aSRandall Stewart } else if (chk->sent == SCTP_DATAGRAM_ACKED) { 707f8829a4aSRandall Stewart /* remember highest acked one */ 708f8829a4aSRandall Stewart could_be_sent = chk; 70944fbe462SRandall Stewart #endif 710f8829a4aSRandall Stewart } 711f8829a4aSRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 712f8829a4aSRandall Stewart cnt_mk++; 713f8829a4aSRandall Stewart } 714f8829a4aSRandall Stewart } 715c105859eSRandall Stewart if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) { 716c105859eSRandall Stewart /* we did not subtract the same things? */ 717c105859eSRandall Stewart audit_tf = 1; 718c105859eSRandall Stewart } 7190053ed28SMichael Tuexen 720ca85e948SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 721f8829a4aSRandall Stewart sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT); 72280fefe0aSRandall Stewart } 723f8829a4aSRandall Stewart #ifdef SCTP_DEBUG 724f8829a4aSRandall Stewart if (num_mk) { 725ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n", 726ad81507eSRandall Stewart tsnlast); 7277858d7cbSMichael Tuexen SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%u\n", 728f8829a4aSRandall Stewart num_mk, 7297858d7cbSMichael Tuexen stcb->asoc.peers_rwnd); 730f8829a4aSRandall Stewart } 731f8829a4aSRandall Stewart #endif 732f8829a4aSRandall Stewart *num_marked = num_mk; 73344fbe462SRandall Stewart *num_abandoned = cnt_abandoned; 7346c065bbeSRandall Stewart /* 7356c065bbeSRandall Stewart * Now check for a ECN Echo that may be stranded And include the 7366c065bbeSRandall Stewart * cnt_mk'd to have all resends in the control queue. 7376c065bbeSRandall Stewart */ 7386c065bbeSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 7396c065bbeSRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 7406c065bbeSRandall Stewart cnt_mk++; 7416c065bbeSRandall Stewart } 7426c065bbeSRandall Stewart if ((chk->whoTo == net) && 7436c065bbeSRandall Stewart (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) { 7446c065bbeSRandall Stewart sctp_free_remote_addr(chk->whoTo); 7456c065bbeSRandall Stewart chk->whoTo = alt; 7466c065bbeSRandall Stewart if (chk->sent != SCTP_DATAGRAM_RESEND) { 7476c065bbeSRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 7484f43a14aSMichael Tuexen chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 7496c065bbeSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 7506c065bbeSRandall Stewart cnt_mk++; 7516c065bbeSRandall Stewart } 7526c065bbeSRandall Stewart atomic_add_int(&alt->ref_count, 1); 7536c065bbeSRandall Stewart } 7546c065bbeSRandall Stewart } 75544fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE 756f8829a4aSRandall Stewart if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) { 757f8829a4aSRandall Stewart /* fix it so we retransmit the highest acked anyway */ 758f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 759f8829a4aSRandall Stewart cnt_mk++; 760f8829a4aSRandall Stewart could_be_sent->sent = SCTP_DATAGRAM_RESEND; 761f8829a4aSRandall Stewart } 76244fbe462SRandall Stewart #endif 763f8829a4aSRandall Stewart if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) { 764a5d547adSRandall Stewart #ifdef INVARIANTS 76518e198d3SRandall Stewart SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n", 76618e198d3SRandall Stewart cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk); 767f8829a4aSRandall Stewart #endif 768f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED 769f8829a4aSRandall Stewart stcb->asoc.sent_queue_retran_cnt = cnt_mk; 770f8829a4aSRandall Stewart #endif 771f8829a4aSRandall Stewart } 772f8829a4aSRandall Stewart if (audit_tf) { 773ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, 774ad81507eSRandall Stewart "Audit total flight due to negative value net:%p\n", 775dd294dceSMichael Tuexen (void *)net); 776f8829a4aSRandall Stewart stcb->asoc.total_flight = 0; 777f8829a4aSRandall Stewart stcb->asoc.total_flight_count = 0; 778f8829a4aSRandall Stewart /* Clear all networks flight size */ 779f8829a4aSRandall Stewart TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) { 780f8829a4aSRandall Stewart lnets->flight_size = 0; 781ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER4, 782ad81507eSRandall Stewart "Net:%p c-f cwnd:%d ssthresh:%d\n", 783dd294dceSMichael Tuexen (void *)lnets, lnets->cwnd, lnets->ssthresh); 784f8829a4aSRandall Stewart } 785f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 786f8829a4aSRandall Stewart if (chk->sent < SCTP_DATAGRAM_RESEND) { 787b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 788a5d547adSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_UP, 789a5d547adSRandall Stewart chk->whoTo->flight_size, 790a5d547adSRandall Stewart chk->book_size, 7919a8e3088SMichael Tuexen (uint32_t)(uintptr_t)chk->whoTo, 79249656eefSMichael Tuexen chk->rec.data.tsn); 79380fefe0aSRandall Stewart } 7940053ed28SMichael Tuexen 795c105859eSRandall Stewart sctp_flight_size_increase(chk); 796c105859eSRandall Stewart sctp_total_flight_increase(stcb, chk); 797f8829a4aSRandall Stewart } 798f8829a4aSRandall Stewart } 799f8829a4aSRandall Stewart } 800f8829a4aSRandall Stewart /* We return 1 if we only have a window probe outstanding */ 801f8829a4aSRandall Stewart return (0); 802f8829a4aSRandall Stewart } 803f8829a4aSRandall Stewart 804f8829a4aSRandall Stewart 805f8829a4aSRandall Stewart int 806f8829a4aSRandall Stewart sctp_t3rxt_timer(struct sctp_inpcb *inp, 807f8829a4aSRandall Stewart struct sctp_tcb *stcb, 808f8829a4aSRandall Stewart struct sctp_nets *net) 809f8829a4aSRandall Stewart { 810f8829a4aSRandall Stewart struct sctp_nets *alt; 81144fbe462SRandall Stewart int win_probe, num_mk, num_abandoned; 812f8829a4aSRandall Stewart 813b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 814562a89b5SRandall Stewart sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT); 81580fefe0aSRandall Stewart } 816b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 817f8829a4aSRandall Stewart struct sctp_nets *lnet; 818f8829a4aSRandall Stewart 819f8829a4aSRandall Stewart TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { 820f8829a4aSRandall Stewart if (net == lnet) { 821f8829a4aSRandall Stewart sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3); 822f8829a4aSRandall Stewart } else { 823f8829a4aSRandall Stewart sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3); 824f8829a4aSRandall Stewart } 825f8829a4aSRandall Stewart } 826f8829a4aSRandall Stewart } 827f8829a4aSRandall Stewart /* Find an alternate and mark those for retransmission */ 828f8829a4aSRandall Stewart if ((stcb->asoc.peers_rwnd == 0) && 829f8829a4aSRandall Stewart (stcb->asoc.total_flight < net->mtu)) { 830f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timowindowprobe); 831f8829a4aSRandall Stewart win_probe = 1; 832f8829a4aSRandall Stewart } else { 833f8829a4aSRandall Stewart win_probe = 0; 834f8829a4aSRandall Stewart } 835c105859eSRandall Stewart 836ca85e948SMichael Tuexen if (win_probe == 0) { 837ca85e948SMichael Tuexen /* We don't do normal threshold management on window probes */ 838ca85e948SMichael Tuexen if (sctp_threshold_management(inp, stcb, net, 839ca85e948SMichael Tuexen stcb->asoc.max_send_times)) { 840ca85e948SMichael Tuexen /* Association was destroyed */ 841ca85e948SMichael Tuexen return (1); 842ca85e948SMichael Tuexen } else { 843ca85e948SMichael Tuexen if (net != stcb->asoc.primary_destination) { 844ca85e948SMichael Tuexen /* send a immediate HB if our RTO is stale */ 845ca85e948SMichael Tuexen struct timeval now; 846ca85e948SMichael Tuexen unsigned int ms_goneby; 847ca85e948SMichael Tuexen 848ca85e948SMichael Tuexen (void)SCTP_GETTIME_TIMEVAL(&now); 849ca85e948SMichael Tuexen if (net->last_sent_time.tv_sec) { 850ca85e948SMichael Tuexen ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000; 851ca85e948SMichael Tuexen } else { 852ca85e948SMichael Tuexen ms_goneby = 0; 853b54d3a6cSRandall Stewart } 854ca85e948SMichael Tuexen if ((net->dest_state & SCTP_ADDR_PF) == 0) { 855ca85e948SMichael Tuexen if ((ms_goneby > net->RTO) || (net->RTO == 0)) { 856c105859eSRandall Stewart /* 857ca85e948SMichael Tuexen * no recent feed back in an 858ca85e948SMichael Tuexen * RTO or more, request a 859ca85e948SMichael Tuexen * RTT update 860ca85e948SMichael Tuexen */ 861ca85e948SMichael Tuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 862ca85e948SMichael Tuexen } 863ca85e948SMichael Tuexen } 864ca85e948SMichael Tuexen } 865ca85e948SMichael Tuexen } 866ca85e948SMichael Tuexen } else { 867ca85e948SMichael Tuexen /* 868ca85e948SMichael Tuexen * For a window probe we don't penalize the net's but only 869ca85e948SMichael Tuexen * the association. This may fail it if SACKs are not coming 870ca85e948SMichael Tuexen * back. If sack's are coming with rwnd locked at 0, we will 871ca85e948SMichael Tuexen * continue to hold things waiting for rwnd to raise 872ca85e948SMichael Tuexen */ 873ca85e948SMichael Tuexen if (sctp_threshold_management(inp, stcb, NULL, 874ca85e948SMichael Tuexen stcb->asoc.max_send_times)) { 875ca85e948SMichael Tuexen /* Association was destroyed */ 876ca85e948SMichael Tuexen return (1); 877ca85e948SMichael Tuexen } 878ca85e948SMichael Tuexen } 879ca85e948SMichael Tuexen if (stcb->asoc.sctp_cmt_on_off > 0) { 880ca85e948SMichael Tuexen if (net->pf_threshold < net->failure_threshold) { 881ca85e948SMichael Tuexen alt = sctp_find_alternate_net(stcb, net, 2); 882ca85e948SMichael Tuexen } else { 883ca85e948SMichael Tuexen /* 884ca85e948SMichael Tuexen * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is 885ca85e948SMichael Tuexen * being used, then pick dest with largest ssthresh 886ca85e948SMichael Tuexen * for any retransmission. 887c105859eSRandall Stewart */ 888b61c3588SMichael Tuexen alt = sctp_find_alternate_net(stcb, net, 1); 889c105859eSRandall Stewart /* 890c105859eSRandall Stewart * CUCv2: If a different dest is picked for the 891ca85e948SMichael Tuexen * retransmission, then new (rtx-)pseudo_cumack 892ca85e948SMichael Tuexen * needs to be tracked for orig dest. Let CUCv2 893ca85e948SMichael Tuexen * track new (rtx-) pseudo-cumack always. 894c105859eSRandall Stewart */ 895c105859eSRandall Stewart net->find_pseudo_cumack = 1; 896c105859eSRandall Stewart net->find_rtx_pseudo_cumack = 1; 897ca85e948SMichael Tuexen } 898ca85e948SMichael Tuexen } else { 899f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, net, 0); 900c105859eSRandall Stewart } 901ca85e948SMichael Tuexen 90244fbe462SRandall Stewart num_mk = 0; 90344fbe462SRandall Stewart num_abandoned = 0; 90444fbe462SRandall Stewart (void)sctp_mark_all_for_resend(stcb, net, alt, win_probe, 90544fbe462SRandall Stewart &num_mk, &num_abandoned); 906f8829a4aSRandall Stewart /* FR Loss recovery just ended with the T3. */ 907f8829a4aSRandall Stewart stcb->asoc.fast_retran_loss_recovery = 0; 908f8829a4aSRandall Stewart 909f8829a4aSRandall Stewart /* CMT FR loss recovery ended with the T3 */ 910f8829a4aSRandall Stewart net->fast_retran_loss_recovery = 0; 911299108c5SRandall Stewart if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) && 912299108c5SRandall Stewart (net->flight_size == 0)) { 913299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net); 914299108c5SRandall Stewart } 9150053ed28SMichael Tuexen 916f8829a4aSRandall Stewart /* 917f8829a4aSRandall Stewart * setup the sat loss recovery that prevents satellite cwnd advance. 918f8829a4aSRandall Stewart */ 919f8829a4aSRandall Stewart stcb->asoc.sat_t3_loss_recovery = 1; 920f8829a4aSRandall Stewart stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq; 921f8829a4aSRandall Stewart 922f8829a4aSRandall Stewart /* Backoff the timer and cwnd */ 92344fbe462SRandall Stewart sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned); 924ca85e948SMichael Tuexen if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) || 925ca85e948SMichael Tuexen (net->dest_state & SCTP_ADDR_PF)) { 926f8829a4aSRandall Stewart /* Move all pending over too */ 9279eea4a2dSMichael Tuexen sctp_move_chunks_from_net(stcb, net); 92817205eccSRandall Stewart 92917205eccSRandall Stewart /* 93017205eccSRandall Stewart * Get the address that failed, to force a new src address 93117205eccSRandall Stewart * selecton and a route allocation. 93217205eccSRandall Stewart */ 93317205eccSRandall Stewart if (net->ro._s_addr) { 93417205eccSRandall Stewart sctp_free_ifa(net->ro._s_addr); 93517205eccSRandall Stewart net->ro._s_addr = NULL; 93617205eccSRandall Stewart } 93717205eccSRandall Stewart net->src_addr_selected = 0; 93817205eccSRandall Stewart 93917205eccSRandall Stewart /* Force a route allocation too */ 94017205eccSRandall Stewart if (net->ro.ro_rt) { 94117205eccSRandall Stewart RTFREE(net->ro.ro_rt); 94217205eccSRandall Stewart net->ro.ro_rt = NULL; 94317205eccSRandall Stewart } 9440053ed28SMichael Tuexen 945f8829a4aSRandall Stewart /* Was it our primary? */ 946f8829a4aSRandall Stewart if ((stcb->asoc.primary_destination == net) && (alt != net)) { 947f8829a4aSRandall Stewart /* 948f8829a4aSRandall Stewart * Yes, note it as such and find an alternate note: 949f8829a4aSRandall Stewart * this means HB code must use this to resent the 950f8829a4aSRandall Stewart * primary if it goes active AND if someone does a 951f8829a4aSRandall Stewart * change-primary then this flag must be cleared 952f8829a4aSRandall Stewart * from any net structures. 953f8829a4aSRandall Stewart */ 954ca85e948SMichael Tuexen if (stcb->asoc.alternate) { 955ca85e948SMichael Tuexen sctp_free_remote_addr(stcb->asoc.alternate); 956f8829a4aSRandall Stewart } 957ca85e948SMichael Tuexen stcb->asoc.alternate = alt; 958ca85e948SMichael Tuexen atomic_add_int(&stcb->asoc.alternate->ref_count, 1); 959f8829a4aSRandall Stewart } 960f8829a4aSRandall Stewart } 961f8829a4aSRandall Stewart /* 962f8829a4aSRandall Stewart * Special case for cookie-echo'ed case, we don't do output but must 963f8829a4aSRandall Stewart * await the COOKIE-ACK before retransmission 964f8829a4aSRandall Stewart */ 965839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) { 966f8829a4aSRandall Stewart /* 967f8829a4aSRandall Stewart * Here we just reset the timer and start again since we 968f8829a4aSRandall Stewart * have not established the asoc 969f8829a4aSRandall Stewart */ 970f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); 971f8829a4aSRandall Stewart return (0); 972f8829a4aSRandall Stewart } 973dd973b0eSMichael Tuexen if (stcb->asoc.prsctp_supported) { 974f8829a4aSRandall Stewart struct sctp_tmit_chunk *lchk; 975f8829a4aSRandall Stewart 976f8829a4aSRandall Stewart lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc); 977f8829a4aSRandall Stewart /* C3. See if we need to send a Fwd-TSN */ 97820b07a4dSMichael Tuexen if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) { 979f8829a4aSRandall Stewart send_forward_tsn(stcb, &stcb->asoc); 980f8829a4aSRandall Stewart if (lchk) { 981f8829a4aSRandall Stewart /* Assure a timer is up */ 982f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo); 983f8829a4aSRandall Stewart } 984f8829a4aSRandall Stewart } 985f8829a4aSRandall Stewart } 986b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) { 987f8829a4aSRandall Stewart sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX); 98880fefe0aSRandall Stewart } 989f8829a4aSRandall Stewart return (0); 990f8829a4aSRandall Stewart } 991f8829a4aSRandall Stewart 992f8829a4aSRandall Stewart int 993f8829a4aSRandall Stewart sctp_t1init_timer(struct sctp_inpcb *inp, 994f8829a4aSRandall Stewart struct sctp_tcb *stcb, 995f8829a4aSRandall Stewart struct sctp_nets *net) 996f8829a4aSRandall Stewart { 997f8829a4aSRandall Stewart /* bump the thresholds */ 998f8829a4aSRandall Stewart if (stcb->asoc.delayed_connection) { 999f8829a4aSRandall Stewart /* 1000f8829a4aSRandall Stewart * special hook for delayed connection. The library did NOT 1001f8829a4aSRandall Stewart * complete the rest of its sends. 1002f8829a4aSRandall Stewart */ 1003f8829a4aSRandall Stewart stcb->asoc.delayed_connection = 0; 1004ceaad40aSRandall Stewart sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED); 1005f8829a4aSRandall Stewart return (0); 1006f8829a4aSRandall Stewart } 1007839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) { 1008f8829a4aSRandall Stewart return (0); 1009f8829a4aSRandall Stewart } 1010f8829a4aSRandall Stewart if (sctp_threshold_management(inp, stcb, net, 1011f8829a4aSRandall Stewart stcb->asoc.max_init_times)) { 1012f8829a4aSRandall Stewart /* Association was destroyed */ 1013f8829a4aSRandall Stewart return (1); 1014f8829a4aSRandall Stewart } 1015f8829a4aSRandall Stewart stcb->asoc.dropped_special_cnt = 0; 101644fbe462SRandall Stewart sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0); 1017f8829a4aSRandall Stewart if (stcb->asoc.initial_init_rto_max < net->RTO) { 1018f8829a4aSRandall Stewart net->RTO = stcb->asoc.initial_init_rto_max; 1019f8829a4aSRandall Stewart } 1020f8829a4aSRandall Stewart if (stcb->asoc.numnets > 1) { 1021f8829a4aSRandall Stewart /* If we have more than one addr use it */ 1022f8829a4aSRandall Stewart struct sctp_nets *alt; 1023f8829a4aSRandall Stewart 1024f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0); 1025b61c3588SMichael Tuexen if (alt != stcb->asoc.primary_destination) { 10269eea4a2dSMichael Tuexen sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination); 1027f8829a4aSRandall Stewart stcb->asoc.primary_destination = alt; 1028f8829a4aSRandall Stewart } 1029f8829a4aSRandall Stewart } 1030f8829a4aSRandall Stewart /* Send out a new init */ 1031ceaad40aSRandall Stewart sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED); 1032f8829a4aSRandall Stewart return (0); 1033f8829a4aSRandall Stewart } 1034f8829a4aSRandall Stewart 1035f8829a4aSRandall Stewart /* 1036f8829a4aSRandall Stewart * For cookie and asconf we actually need to find and mark for resend, then 1037f8829a4aSRandall Stewart * increment the resend counter (after all the threshold management stuff of 1038f8829a4aSRandall Stewart * course). 1039f8829a4aSRandall Stewart */ 1040f8829a4aSRandall Stewart int 1041f8829a4aSRandall Stewart sctp_cookie_timer(struct sctp_inpcb *inp, 1042f8829a4aSRandall Stewart struct sctp_tcb *stcb, 10437215cc1bSMichael Tuexen struct sctp_nets *net SCTP_UNUSED) 1044f8829a4aSRandall Stewart { 1045f8829a4aSRandall Stewart struct sctp_nets *alt; 1046f8829a4aSRandall Stewart struct sctp_tmit_chunk *cookie; 1047f8829a4aSRandall Stewart 1048f8829a4aSRandall Stewart /* first before all else we must find the cookie */ 1049f8829a4aSRandall Stewart TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) { 1050f8829a4aSRandall Stewart if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 1051f8829a4aSRandall Stewart break; 1052f8829a4aSRandall Stewart } 1053f8829a4aSRandall Stewart } 1054f8829a4aSRandall Stewart if (cookie == NULL) { 1055839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) { 1056f8829a4aSRandall Stewart /* FOOBAR! */ 1057ff1ffd74SMichael Tuexen struct mbuf *op_err; 1058f8829a4aSRandall Stewart 1059267dbe63SMichael Tuexen op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 1060ff1ffd74SMichael Tuexen "Cookie timer expired, but no cookie"); 1061b7d130beSMichael Tuexen inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3; 1062ff1ffd74SMichael Tuexen sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED); 1063f8829a4aSRandall Stewart } else { 1064a5d547adSRandall Stewart #ifdef INVARIANTS 1065f8829a4aSRandall Stewart panic("Cookie timer expires in wrong state?"); 1066f8829a4aSRandall Stewart #else 1067839d21d6SMichael Tuexen SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(stcb)); 1068f8829a4aSRandall Stewart return (0); 1069f8829a4aSRandall Stewart #endif 1070f8829a4aSRandall Stewart } 1071f8829a4aSRandall Stewart return (0); 1072f8829a4aSRandall Stewart } 1073f8829a4aSRandall Stewart /* Ok we found the cookie, threshold management next */ 1074f8829a4aSRandall Stewart if (sctp_threshold_management(inp, stcb, cookie->whoTo, 1075f8829a4aSRandall Stewart stcb->asoc.max_init_times)) { 1076f8829a4aSRandall Stewart /* Assoc is over */ 1077f8829a4aSRandall Stewart return (1); 1078f8829a4aSRandall Stewart } 1079f8829a4aSRandall Stewart /* 1080a807fe2dSMichael Tuexen * Cleared threshold management, now lets backoff the address and 1081a807fe2dSMichael Tuexen * select an alternate 1082f8829a4aSRandall Stewart */ 1083f8829a4aSRandall Stewart stcb->asoc.dropped_special_cnt = 0; 108444fbe462SRandall Stewart sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0); 1085f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0); 1086f8829a4aSRandall Stewart if (alt != cookie->whoTo) { 1087f8829a4aSRandall Stewart sctp_free_remote_addr(cookie->whoTo); 1088f8829a4aSRandall Stewart cookie->whoTo = alt; 1089f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 1090f8829a4aSRandall Stewart } 1091f8829a4aSRandall Stewart /* Now mark the retran info */ 1092f8829a4aSRandall Stewart if (cookie->sent != SCTP_DATAGRAM_RESEND) { 1093f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1094f8829a4aSRandall Stewart } 1095f8829a4aSRandall Stewart cookie->sent = SCTP_DATAGRAM_RESEND; 10964f43a14aSMichael Tuexen cookie->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1097f8829a4aSRandall Stewart /* 1098f8829a4aSRandall Stewart * Now call the output routine to kick out the cookie again, Note we 1099f8829a4aSRandall Stewart * don't mark any chunks for retran so that FR will need to kick in 1100f8829a4aSRandall Stewart * to move these (or a send timer). 1101f8829a4aSRandall Stewart */ 1102f8829a4aSRandall Stewart return (0); 1103f8829a4aSRandall Stewart } 1104f8829a4aSRandall Stewart 1105f8829a4aSRandall Stewart int 1106e95b3d7fSMichael Tuexen sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 1107f8829a4aSRandall Stewart { 1108e95b3d7fSMichael Tuexen struct sctp_nets *alt, *net; 1109f8829a4aSRandall Stewart struct sctp_tmit_chunk *strrst = NULL, *chk = NULL; 1110f8829a4aSRandall Stewart 1111f8829a4aSRandall Stewart if (stcb->asoc.stream_reset_outstanding == 0) { 1112f8829a4aSRandall Stewart return (0); 1113f8829a4aSRandall Stewart } 1114f8829a4aSRandall Stewart /* find the existing STRRESET, we use the seq number we sent out on */ 1115ad81507eSRandall Stewart (void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst); 1116f8829a4aSRandall Stewart if (strrst == NULL) { 1117f8829a4aSRandall Stewart return (0); 1118f8829a4aSRandall Stewart } 1119e95b3d7fSMichael Tuexen net = strrst->whoTo; 1120f8829a4aSRandall Stewart /* do threshold management */ 1121e95b3d7fSMichael Tuexen if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) { 1122f8829a4aSRandall Stewart /* Assoc is over */ 1123f8829a4aSRandall Stewart return (1); 1124f8829a4aSRandall Stewart } 1125f8829a4aSRandall Stewart /* 1126a807fe2dSMichael Tuexen * Cleared threshold management, now lets backoff the address and 1127a807fe2dSMichael Tuexen * select an alternate 1128f8829a4aSRandall Stewart */ 1129e95b3d7fSMichael Tuexen sctp_backoff_on_timeout(stcb, net, 1, 0, 0); 1130e95b3d7fSMichael Tuexen alt = sctp_find_alternate_net(stcb, net, 0); 1131f8829a4aSRandall Stewart strrst->whoTo = alt; 1132f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 1133f8829a4aSRandall Stewart 1134f8829a4aSRandall Stewart /* See if a ECN Echo is also stranded */ 1135f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 1136f8829a4aSRandall Stewart if ((chk->whoTo == net) && 1137f8829a4aSRandall Stewart (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) { 1138f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 1139f8829a4aSRandall Stewart if (chk->sent != SCTP_DATAGRAM_RESEND) { 1140f8829a4aSRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 11414f43a14aSMichael Tuexen chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1142f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1143f8829a4aSRandall Stewart } 1144f8829a4aSRandall Stewart chk->whoTo = alt; 1145f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 1146f8829a4aSRandall Stewart } 1147f8829a4aSRandall Stewart } 1148ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { 1149f8829a4aSRandall Stewart /* 1150f8829a4aSRandall Stewart * If the address went un-reachable, we need to move to 1151f8829a4aSRandall Stewart * alternates for ALL chk's in queue 1152f8829a4aSRandall Stewart */ 11539eea4a2dSMichael Tuexen sctp_move_chunks_from_net(stcb, net); 1154f8829a4aSRandall Stewart } 1155e95b3d7fSMichael Tuexen sctp_free_remote_addr(net); 1156e95b3d7fSMichael Tuexen 1157f8829a4aSRandall Stewart /* mark the retran info */ 1158f8829a4aSRandall Stewart if (strrst->sent != SCTP_DATAGRAM_RESEND) 1159f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1160f8829a4aSRandall Stewart strrst->sent = SCTP_DATAGRAM_RESEND; 11614f43a14aSMichael Tuexen strrst->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1162f8829a4aSRandall Stewart 1163f8829a4aSRandall Stewart /* restart the timer */ 1164e95b3d7fSMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, alt); 1165f8829a4aSRandall Stewart return (0); 1166f8829a4aSRandall Stewart } 1167f8829a4aSRandall Stewart 1168f8829a4aSRandall Stewart int 1169f8829a4aSRandall Stewart sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 1170f8829a4aSRandall Stewart struct sctp_nets *net) 1171f8829a4aSRandall Stewart { 1172f8829a4aSRandall Stewart struct sctp_nets *alt; 11734a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *asconf, *chk; 1174f8829a4aSRandall Stewart 11751b649582SRandall Stewart /* is this a first send, or a retransmission? */ 1176c54a18d2SRandall Stewart if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) { 1177f8829a4aSRandall Stewart /* compose a new ASCONF chunk and send it */ 11783232788eSRandall Stewart sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED); 1179f8829a4aSRandall Stewart } else { 11801b649582SRandall Stewart /* 11811b649582SRandall Stewart * Retransmission of the existing ASCONF is needed 11821b649582SRandall Stewart */ 1183f8829a4aSRandall Stewart 1184f8829a4aSRandall Stewart /* find the existing ASCONF */ 1185c54a18d2SRandall Stewart asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue); 1186f8829a4aSRandall Stewart if (asconf == NULL) { 1187f8829a4aSRandall Stewart return (0); 1188f8829a4aSRandall Stewart } 1189e95b3d7fSMichael Tuexen net = asconf->whoTo; 1190f8829a4aSRandall Stewart /* do threshold management */ 1191e95b3d7fSMichael Tuexen if (sctp_threshold_management(inp, stcb, net, 1192f8829a4aSRandall Stewart stcb->asoc.max_send_times)) { 1193f8829a4aSRandall Stewart /* Assoc is over */ 1194f8829a4aSRandall Stewart return (1); 1195f8829a4aSRandall Stewart } 1196f8829a4aSRandall Stewart if (asconf->snd_count > stcb->asoc.max_send_times) { 1197f8829a4aSRandall Stewart /* 11981b649582SRandall Stewart * Something is rotten: our peer is not responding 11991b649582SRandall Stewart * to ASCONFs but apparently is to other chunks. 12001b649582SRandall Stewart * i.e. it is not properly handling the chunk type 12011b649582SRandall Stewart * upper bits. Mark this peer as ASCONF incapable 12021b649582SRandall Stewart * and cleanup. 1203f8829a4aSRandall Stewart */ 1204ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n"); 12056fb7b4fbSMichael Tuexen sctp_asconf_cleanup(stcb); 1206f8829a4aSRandall Stewart return (0); 1207f8829a4aSRandall Stewart } 1208f8829a4aSRandall Stewart /* 12091b649582SRandall Stewart * cleared threshold management, so now backoff the net and 12101b649582SRandall Stewart * select an alternate 1211f8829a4aSRandall Stewart */ 1212e95b3d7fSMichael Tuexen sctp_backoff_on_timeout(stcb, net, 1, 0, 0); 1213e95b3d7fSMichael Tuexen alt = sctp_find_alternate_net(stcb, net, 0); 1214c54a18d2SRandall Stewart if (asconf->whoTo != alt) { 1215f8829a4aSRandall Stewart asconf->whoTo = alt; 1216f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 1217c54a18d2SRandall Stewart } 12180053ed28SMichael Tuexen 12191b649582SRandall Stewart /* See if an ECN Echo is also stranded */ 1220f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { 1221f8829a4aSRandall Stewart if ((chk->whoTo == net) && 1222f8829a4aSRandall Stewart (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) { 1223f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 1224f8829a4aSRandall Stewart chk->whoTo = alt; 1225f8829a4aSRandall Stewart if (chk->sent != SCTP_DATAGRAM_RESEND) { 1226f8829a4aSRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 12274f43a14aSMichael Tuexen chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1228f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1229f8829a4aSRandall Stewart } 1230f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 1231f8829a4aSRandall Stewart } 1232f8829a4aSRandall Stewart } 12334a9ef3f8SMichael Tuexen TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) { 1234c54a18d2SRandall Stewart if (chk->whoTo != alt) { 1235c54a18d2SRandall Stewart sctp_free_remote_addr(chk->whoTo); 1236c54a18d2SRandall Stewart chk->whoTo = alt; 1237c54a18d2SRandall Stewart atomic_add_int(&alt->ref_count, 1); 1238c54a18d2SRandall Stewart } 1239c54a18d2SRandall Stewart if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT) 1240c54a18d2SRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1241c54a18d2SRandall Stewart chk->sent = SCTP_DATAGRAM_RESEND; 12424f43a14aSMichael Tuexen chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1243c54a18d2SRandall Stewart } 1244ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { 1245f8829a4aSRandall Stewart /* 1246f8829a4aSRandall Stewart * If the address went un-reachable, we need to move 12471b649582SRandall Stewart * to the alternate for ALL chunks in queue 1248f8829a4aSRandall Stewart */ 12499eea4a2dSMichael Tuexen sctp_move_chunks_from_net(stcb, net); 1250f8829a4aSRandall Stewart } 1251e95b3d7fSMichael Tuexen sctp_free_remote_addr(net); 1252e95b3d7fSMichael Tuexen 1253f8829a4aSRandall Stewart /* mark the retran info */ 1254f8829a4aSRandall Stewart if (asconf->sent != SCTP_DATAGRAM_RESEND) 1255f8829a4aSRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 1256f8829a4aSRandall Stewart asconf->sent = SCTP_DATAGRAM_RESEND; 12574f43a14aSMichael Tuexen asconf->flags |= CHUNK_FLAGS_FRAGMENT_OK; 1258c54a18d2SRandall Stewart 1259c54a18d2SRandall Stewart /* send another ASCONF if any and we can do */ 1260c54a18d2SRandall Stewart sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED); 1261f8829a4aSRandall Stewart } 1262f8829a4aSRandall Stewart return (0); 1263f8829a4aSRandall Stewart } 1264f8829a4aSRandall Stewart 1265851b7298SRandall Stewart /* Mobility adaptation */ 126604ee05e8SRandall Stewart void 1267*a57fb68bSMichael Tuexen sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 1268851b7298SRandall Stewart { 1269851b7298SRandall Stewart if (stcb->asoc.deleted_primary == NULL) { 1270851b7298SRandall Stewart SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n"); 1271851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 127204ee05e8SRandall Stewart return; 1273851b7298SRandall Stewart } 1274851b7298SRandall Stewart SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary "); 1275851b7298SRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa); 1276851b7298SRandall Stewart sctp_free_remote_addr(stcb->asoc.deleted_primary); 1277851b7298SRandall Stewart stcb->asoc.deleted_primary = NULL; 1278851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 127904ee05e8SRandall Stewart return; 1280851b7298SRandall Stewart } 1281851b7298SRandall Stewart 1282f8829a4aSRandall Stewart /* 1283f8829a4aSRandall Stewart * For the shutdown and shutdown-ack, we do not keep one around on the 1284f8829a4aSRandall Stewart * control queue. This means we must generate a new one and call the general 1285f8829a4aSRandall Stewart * chunk output routine, AFTER having done threshold management. 1286b61c3588SMichael Tuexen * It is assumed that net is non-NULL. 1287f8829a4aSRandall Stewart */ 1288f8829a4aSRandall Stewart int 1289f8829a4aSRandall Stewart sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 1290f8829a4aSRandall Stewart struct sctp_nets *net) 1291f8829a4aSRandall Stewart { 1292f8829a4aSRandall Stewart struct sctp_nets *alt; 1293f8829a4aSRandall Stewart 1294cd0a4ff6SPedro F. Giffuni /* first threshold management */ 1295f8829a4aSRandall Stewart if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) { 1296f8829a4aSRandall Stewart /* Assoc is over */ 1297f8829a4aSRandall Stewart return (1); 1298f8829a4aSRandall Stewart } 1299b61c3588SMichael Tuexen sctp_backoff_on_timeout(stcb, net, 1, 0, 0); 1300f8829a4aSRandall Stewart /* second select an alternative */ 1301f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, net, 0); 1302f8829a4aSRandall Stewart 1303f8829a4aSRandall Stewart /* third generate a shutdown into the queue for out net */ 1304f8829a4aSRandall Stewart sctp_send_shutdown(stcb, alt); 1305b61c3588SMichael Tuexen 1306f8829a4aSRandall Stewart /* fourth restart timer */ 1307f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt); 1308f8829a4aSRandall Stewart return (0); 1309f8829a4aSRandall Stewart } 1310f8829a4aSRandall Stewart 1311f8829a4aSRandall Stewart int 1312f8829a4aSRandall Stewart sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 1313f8829a4aSRandall Stewart struct sctp_nets *net) 1314f8829a4aSRandall Stewart { 1315f8829a4aSRandall Stewart struct sctp_nets *alt; 1316f8829a4aSRandall Stewart 1317cd0a4ff6SPedro F. Giffuni /* first threshold management */ 1318f8829a4aSRandall Stewart if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) { 1319f8829a4aSRandall Stewart /* Assoc is over */ 1320f8829a4aSRandall Stewart return (1); 1321f8829a4aSRandall Stewart } 1322b61c3588SMichael Tuexen sctp_backoff_on_timeout(stcb, net, 1, 0, 0); 1323f8829a4aSRandall Stewart /* second select an alternative */ 1324f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, net, 0); 1325f8829a4aSRandall Stewart 1326f8829a4aSRandall Stewart /* third generate a shutdown into the queue for out net */ 1327f8829a4aSRandall Stewart sctp_send_shutdown_ack(stcb, alt); 1328f8829a4aSRandall Stewart 1329f8829a4aSRandall Stewart /* fourth restart timer */ 1330f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt); 1331f8829a4aSRandall Stewart return (0); 1332f8829a4aSRandall Stewart } 1333f8829a4aSRandall Stewart 1334f8829a4aSRandall Stewart static void 1335f8829a4aSRandall Stewart sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp, 1336f8829a4aSRandall Stewart struct sctp_tcb *stcb) 1337f8829a4aSRandall Stewart { 1338f8829a4aSRandall Stewart struct sctp_stream_queue_pending *sp; 1339f7a77f6fSMichael Tuexen unsigned int i, chks_in_queue = 0; 1340f8829a4aSRandall Stewart int being_filled = 0; 1341f8829a4aSRandall Stewart 1342f8829a4aSRandall Stewart /* 1343f8829a4aSRandall Stewart * This function is ONLY called when the send/sent queues are empty. 1344f8829a4aSRandall Stewart */ 1345f8829a4aSRandall Stewart if ((stcb == NULL) || (inp == NULL)) 1346f8829a4aSRandall Stewart return; 1347f8829a4aSRandall Stewart 1348f8829a4aSRandall Stewart if (stcb->asoc.sent_queue_retran_cnt) { 1349ad81507eSRandall Stewart SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n", 1350f8829a4aSRandall Stewart stcb->asoc.sent_queue_retran_cnt); 1351f8829a4aSRandall Stewart stcb->asoc.sent_queue_retran_cnt = 0; 1352f8829a4aSRandall Stewart } 1353f7a77f6fSMichael Tuexen if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) { 1354252f7f93SMichael Tuexen /* No stream scheduler information, initialize scheduler */ 1355252f7f93SMichael Tuexen stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0); 1356252f7f93SMichael Tuexen if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) { 1357252f7f93SMichael Tuexen /* yep, we lost a stream or two */ 1358252f7f93SMichael Tuexen SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n"); 1359f8829a4aSRandall Stewart } else { 1360252f7f93SMichael Tuexen /* no streams lost */ 1361f8829a4aSRandall Stewart stcb->asoc.total_output_queue_size = 0; 1362f8829a4aSRandall Stewart } 1363f8829a4aSRandall Stewart } 1364f8829a4aSRandall Stewart /* Check to see if some data queued, if so report it */ 1365f7a77f6fSMichael Tuexen for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 1366f7a77f6fSMichael Tuexen if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { 1367f7a77f6fSMichael Tuexen TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) { 1368f8829a4aSRandall Stewart if (sp->msg_is_complete) 1369f8829a4aSRandall Stewart being_filled++; 1370f8829a4aSRandall Stewart chks_in_queue++; 1371f8829a4aSRandall Stewart } 1372f8829a4aSRandall Stewart } 1373f8829a4aSRandall Stewart } 1374f8829a4aSRandall Stewart if (chks_in_queue != stcb->asoc.stream_queue_cnt) { 1375ad81507eSRandall Stewart SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n", 1376f8829a4aSRandall Stewart stcb->asoc.stream_queue_cnt, chks_in_queue); 1377f8829a4aSRandall Stewart } 1378f8829a4aSRandall Stewart if (chks_in_queue) { 1379f8829a4aSRandall Stewart /* call the output queue function */ 1380ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); 1381f8829a4aSRandall Stewart if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) && 1382f8829a4aSRandall Stewart (TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 1383f8829a4aSRandall Stewart /* 1384f8829a4aSRandall Stewart * Probably should go in and make it go back through 1385f8829a4aSRandall Stewart * and add fragments allowed 1386f8829a4aSRandall Stewart */ 1387f8829a4aSRandall Stewart if (being_filled == 0) { 1388ad81507eSRandall Stewart SCTP_PRINTF("Still nothing moved %d chunks are stuck\n", 1389f8829a4aSRandall Stewart chks_in_queue); 1390f8829a4aSRandall Stewart } 1391f8829a4aSRandall Stewart } 1392f8829a4aSRandall Stewart } else { 1393ad81507eSRandall Stewart SCTP_PRINTF("Found no chunks on any queue tot:%lu\n", 1394f8829a4aSRandall Stewart (u_long)stcb->asoc.total_output_queue_size); 1395f8829a4aSRandall Stewart stcb->asoc.total_output_queue_size = 0; 1396f8829a4aSRandall Stewart } 1397f8829a4aSRandall Stewart } 1398f8829a4aSRandall Stewart 1399f8829a4aSRandall Stewart int 1400f8829a4aSRandall Stewart sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 1401ca85e948SMichael Tuexen struct sctp_nets *net) 1402f8829a4aSRandall Stewart { 1403ca85e948SMichael Tuexen uint8_t net_was_pf; 1404b54d3a6cSRandall Stewart 1405ca85e948SMichael Tuexen if (net->dest_state & SCTP_ADDR_PF) { 1406ca85e948SMichael Tuexen net_was_pf = 1; 140760990c0cSMichael Tuexen } else { 140860990c0cSMichael Tuexen net_was_pf = 0; 1409ca85e948SMichael Tuexen } 1410f8829a4aSRandall Stewart if (net->hb_responded == 0) { 141142551e99SRandall Stewart if (net->ro._s_addr) { 141242551e99SRandall Stewart /* 141360990c0cSMichael Tuexen * Invalidate the src address if we did not get a 141460990c0cSMichael Tuexen * response last time. 141542551e99SRandall Stewart */ 141642551e99SRandall Stewart sctp_free_ifa(net->ro._s_addr); 141742551e99SRandall Stewart net->ro._s_addr = NULL; 141842551e99SRandall Stewart net->src_addr_selected = 0; 141942551e99SRandall Stewart } 142044fbe462SRandall Stewart sctp_backoff_on_timeout(stcb, net, 1, 0, 0); 1421ca85e948SMichael Tuexen if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) { 1422ca85e948SMichael Tuexen /* Assoc is over */ 1423ca85e948SMichael Tuexen return (1); 1424ca85e948SMichael Tuexen } 1425f8829a4aSRandall Stewart } 1426f8829a4aSRandall Stewart /* Zero PBA, if it needs it */ 1427f8829a4aSRandall Stewart if (net->partial_bytes_acked) { 1428f8829a4aSRandall Stewart net->partial_bytes_acked = 0; 1429f8829a4aSRandall Stewart } 1430f8829a4aSRandall Stewart if ((stcb->asoc.total_output_queue_size > 0) && 1431f8829a4aSRandall Stewart (TAILQ_EMPTY(&stcb->asoc.send_queue)) && 1432f8829a4aSRandall Stewart (TAILQ_EMPTY(&stcb->asoc.sent_queue))) { 1433f8829a4aSRandall Stewart sctp_audit_stream_queues_for_size(inp, stcb); 1434f8829a4aSRandall Stewart } 1435ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_NOHB) && 1436ca85e948SMichael Tuexen !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) { 1437f8829a4aSRandall Stewart /* 1438ca85e948SMichael Tuexen * when move to PF during threshold mangement, a HB has been 1439ca85e948SMichael Tuexen * queued in that routine 1440f8829a4aSRandall Stewart */ 1441ca7567c9SMichael Tuexen uint32_t ms_gone_by; 1442ca7567c9SMichael Tuexen 1443ca7567c9SMichael Tuexen if ((net->last_sent_time.tv_sec > 0) || 1444ca7567c9SMichael Tuexen (net->last_sent_time.tv_usec > 0)) { 1445ca7567c9SMichael Tuexen struct timeval diff; 1446ca7567c9SMichael Tuexen 1447ca7567c9SMichael Tuexen SCTP_GETTIME_TIMEVAL(&diff); 1448ca7567c9SMichael Tuexen timevalsub(&diff, &net->last_sent_time); 1449ca7567c9SMichael Tuexen ms_gone_by = (uint32_t)(diff.tv_sec * 1000) + 1450ca7567c9SMichael Tuexen (uint32_t)(diff.tv_usec / 1000); 1451ca7567c9SMichael Tuexen } else { 1452ca7567c9SMichael Tuexen ms_gone_by = 0xffffffff; 1453ca7567c9SMichael Tuexen } 14544dca0ef4SMichael Tuexen if ((ms_gone_by >= net->heart_beat_delay) || 14554dca0ef4SMichael Tuexen (net->dest_state & SCTP_ADDR_PF)) { 1456ca85e948SMichael Tuexen sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); 1457f8829a4aSRandall Stewart } 1458ca7567c9SMichael Tuexen } 1459f8829a4aSRandall Stewart return (0); 1460f8829a4aSRandall Stewart } 1461f8829a4aSRandall Stewart 1462f8829a4aSRandall Stewart void 1463f8829a4aSRandall Stewart sctp_pathmtu_timer(struct sctp_inpcb *inp, 1464f8829a4aSRandall Stewart struct sctp_tcb *stcb, 1465f8829a4aSRandall Stewart struct sctp_nets *net) 1466f8829a4aSRandall Stewart { 1467c54a18d2SRandall Stewart uint32_t next_mtu, mtu; 1468f8829a4aSRandall Stewart 14697215cc1bSMichael Tuexen next_mtu = sctp_get_next_mtu(net->mtu); 147017205eccSRandall Stewart 1471c54a18d2SRandall Stewart if ((next_mtu > net->mtu) && (net->port == 0)) { 147217205eccSRandall Stewart if ((net->src_addr_selected == 0) || 147317205eccSRandall Stewart (net->ro._s_addr == NULL) || 147417205eccSRandall Stewart (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) { 1475ad81507eSRandall Stewart if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) { 147617205eccSRandall Stewart sctp_free_ifa(net->ro._s_addr); 147717205eccSRandall Stewart net->ro._s_addr = NULL; 147817205eccSRandall Stewart net->src_addr_selected = 0; 1479ad81507eSRandall Stewart } else if (net->ro._s_addr == NULL) { 1480c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE) 1481c54a18d2SRandall Stewart if (net->ro._l_addr.sa.sa_family == AF_INET6) { 1482c54a18d2SRandall Stewart struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1483c54a18d2SRandall Stewart 1484c54a18d2SRandall Stewart /* KAME hack: embed scopeid */ 1485482444b4SRandall Stewart (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); 1486c54a18d2SRandall Stewart } 1487c54a18d2SRandall Stewart #endif 1488c54a18d2SRandall Stewart 148917205eccSRandall Stewart net->ro._s_addr = sctp_source_address_selection(inp, 149017205eccSRandall Stewart stcb, 149117205eccSRandall Stewart (sctp_route_t *)&net->ro, 149217205eccSRandall Stewart net, 0, stcb->asoc.vrf_id); 1493c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE) 1494c54a18d2SRandall Stewart if (net->ro._l_addr.sa.sa_family == AF_INET6) { 1495c54a18d2SRandall Stewart struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1496c54a18d2SRandall Stewart 1497c54a18d2SRandall Stewart (void)sa6_recoverscope(sin6); 1498c54a18d2SRandall Stewart } 1499c54a18d2SRandall Stewart #endif /* INET6 */ 1500ad81507eSRandall Stewart } 150117205eccSRandall Stewart if (net->ro._s_addr) 150217205eccSRandall Stewart net->src_addr_selected = 1; 150317205eccSRandall Stewart } 150417205eccSRandall Stewart if (net->ro._s_addr) { 150517205eccSRandall Stewart mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt); 15064474d71aSMichael Tuexen #if defined(INET) || defined(INET6) 1507830d754dSRandall Stewart if (net->port) { 1508830d754dSRandall Stewart mtu -= sizeof(struct udphdr); 1509830d754dSRandall Stewart } 15104474d71aSMichael Tuexen #endif 151117205eccSRandall Stewart if (mtu > next_mtu) { 1512f8829a4aSRandall Stewart net->mtu = next_mtu; 1513faadc1b4SMichael Tuexen } else { 1514faadc1b4SMichael Tuexen net->mtu = mtu; 1515f8829a4aSRandall Stewart } 1516f8829a4aSRandall Stewart } 1517f8829a4aSRandall Stewart } 1518f8829a4aSRandall Stewart /* restart the timer */ 1519f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 1520f8829a4aSRandall Stewart } 1521f8829a4aSRandall Stewart 1522f8829a4aSRandall Stewart void 1523*a57fb68bSMichael Tuexen sctp_autoclose_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 1524f8829a4aSRandall Stewart { 1525f8829a4aSRandall Stewart struct timeval tn, *tim_touse; 1526f8829a4aSRandall Stewart struct sctp_association *asoc; 1527f8829a4aSRandall Stewart int ticks_gone_by; 1528f8829a4aSRandall Stewart 15296e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&tn); 1530f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks && 1531f8829a4aSRandall Stewart sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { 1532f8829a4aSRandall Stewart /* Auto close is on */ 1533f8829a4aSRandall Stewart asoc = &stcb->asoc; 1534f8829a4aSRandall Stewart /* pick the time to use */ 1535f8829a4aSRandall Stewart if (asoc->time_last_rcvd.tv_sec > 1536f8829a4aSRandall Stewart asoc->time_last_sent.tv_sec) { 1537f8829a4aSRandall Stewart tim_touse = &asoc->time_last_rcvd; 1538f8829a4aSRandall Stewart } else { 1539f8829a4aSRandall Stewart tim_touse = &asoc->time_last_sent; 1540f8829a4aSRandall Stewart } 1541f8829a4aSRandall Stewart /* Now has long enough transpired to autoclose? */ 1542f8829a4aSRandall Stewart ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec); 1543f8829a4aSRandall Stewart if ((ticks_gone_by > 0) && 1544f8829a4aSRandall Stewart (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) { 1545f8829a4aSRandall Stewart /* 1546f8829a4aSRandall Stewart * autoclose time has hit, call the output routine, 1547f8829a4aSRandall Stewart * which should do nothing just to be SURE we don't 1548f8829a4aSRandall Stewart * have hanging data. We can then safely check the 1549f8829a4aSRandall Stewart * queues and know that we are clear to send 1550f8829a4aSRandall Stewart * shutdown 1551f8829a4aSRandall Stewart */ 1552ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED); 1553f8829a4aSRandall Stewart /* Are we clean? */ 1554f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->send_queue) && 1555f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->sent_queue)) { 1556f8829a4aSRandall Stewart /* 1557f8829a4aSRandall Stewart * there is nothing queued to send, so I'm 1558f8829a4aSRandall Stewart * done... 1559f8829a4aSRandall Stewart */ 1560839d21d6SMichael Tuexen if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) { 1561f8829a4aSRandall Stewart /* only send SHUTDOWN 1st time thru */ 1562*a57fb68bSMichael Tuexen struct sctp_nets *net; 1563ca85e948SMichael Tuexen 1564839d21d6SMichael Tuexen if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 1565839d21d6SMichael Tuexen (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 1566f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 1567f42a358aSRandall Stewart } 1568839d21d6SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); 1569c39cfa1fSMichael Tuexen sctp_stop_timers_for_shutdown(stcb); 1570c39cfa1fSMichael Tuexen if (stcb->asoc.alternate) { 1571*a57fb68bSMichael Tuexen net = stcb->asoc.alternate; 1572c39cfa1fSMichael Tuexen } else { 1573*a57fb68bSMichael Tuexen net = stcb->asoc.primary_destination; 1574c39cfa1fSMichael Tuexen } 1575*a57fb68bSMichael Tuexen sctp_send_shutdown(stcb, net); 1576f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 1577*a57fb68bSMichael Tuexen stcb->sctp_ep, stcb, net); 1578f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1579*a57fb68bSMichael Tuexen stcb->sctp_ep, stcb, NULL); 1580f8829a4aSRandall Stewart } 1581f8829a4aSRandall Stewart } 1582f8829a4aSRandall Stewart } else { 1583f8829a4aSRandall Stewart /* 1584f8829a4aSRandall Stewart * No auto close at this time, reset t-o to check 1585f8829a4aSRandall Stewart * later 1586f8829a4aSRandall Stewart */ 1587f8829a4aSRandall Stewart int tmp; 1588f8829a4aSRandall Stewart 1589f8829a4aSRandall Stewart /* fool the timer startup to use the time left */ 1590f8829a4aSRandall Stewart tmp = asoc->sctp_autoclose_ticks; 1591f8829a4aSRandall Stewart asoc->sctp_autoclose_ticks -= ticks_gone_by; 1592*a57fb68bSMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); 1593f8829a4aSRandall Stewart /* restore the real tick value */ 1594f8829a4aSRandall Stewart asoc->sctp_autoclose_ticks = tmp; 1595f8829a4aSRandall Stewart } 1596f8829a4aSRandall Stewart } 1597f8829a4aSRandall Stewart } 1598