Home | History | Annotate | Download | only in netinet
      1 /*-
      2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
      3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
      4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are met:
      8  *
      9  * a) Redistributions of source code must retain the above copyright notice,
     10  *    this list of conditions and the following disclaimer.
     11  *
     12  * b) Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in
     14  *    the documentation and/or other materials provided with the distribution.
     15  *
     16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
     17  *    contributors may be used to endorse or promote products derived
     18  *    from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     30  * THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifdef __FreeBSD__
     34 #include <sys/cdefs.h>
     35 __FBSDID("$FreeBSD: head/sys/netinet/sctp_timer.c 263237 2014-03-16 12:32:16Z tuexen $");
     36 #endif
     37 
     38 #define _IP_VHL
     39 #include <netinet/sctp_os.h>
     40 #include <netinet/sctp_pcb.h>
     41 #ifdef INET6
     42 #if defined(__Userspace_os_FreeBSD)
     43 #include <netinet6/sctp6_var.h>
     44 #endif
     45 #endif
     46 #include <netinet/sctp_var.h>
     47 #include <netinet/sctp_sysctl.h>
     48 #include <netinet/sctp_timer.h>
     49 #include <netinet/sctputil.h>
     50 #include <netinet/sctp_output.h>
     51 #include <netinet/sctp_header.h>
     52 #include <netinet/sctp_indata.h>
     53 #include <netinet/sctp_asconf.h>
     54 #include <netinet/sctp_input.h>
     55 #include <netinet/sctp.h>
     56 #include <netinet/sctp_uio.h>
     57 #if !defined(__Userspace_os_Windows)
     58 #include <netinet/udp.h>
     59 #endif
     60 
     61 #if defined(__APPLE__)
     62 #define APPLE_FILE_NO 6
     63 #endif
     64 
     65 void
     66 sctp_audit_retranmission_queue(struct sctp_association *asoc)
     67 {
     68 	struct sctp_tmit_chunk *chk;
     69 
     70 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
     71 			asoc->sent_queue_retran_cnt,
     72 			asoc->sent_queue_cnt);
     73 	asoc->sent_queue_retran_cnt = 0;
     74 	asoc->sent_queue_cnt = 0;
     75 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
     76 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
     77 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
     78 		}
     79 		asoc->sent_queue_cnt++;
     80 	}
     81 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
     82 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
     83 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
     84 		}
     85 	}
     86 	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
     87 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
     88 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
     89 		}
     90 	}
     91 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
     92 		asoc->sent_queue_retran_cnt,
     93 		asoc->sent_queue_cnt);
     94 }
     95 
     96 int
     97 sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
     98     struct sctp_nets *net, uint16_t threshold)
     99 {
    100 	if (net) {
    101 		net->error_count++;
    102 		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
    103 			(void *)net, net->error_count,
    104 			net->failure_threshold);
    105 		if (net->error_count > net->failure_threshold) {
    106 			/* We had a threshold failure */
    107 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
    108 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
    109 				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
    110 				net->dest_state &= ~SCTP_ADDR_PF;
    111 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
    112 				    stcb, 0,
    113 				    (void *)net, SCTP_SO_NOT_LOCKED);
    114 			}
    115 		} else if ((net->pf_threshold < net->failure_threshold) &&
    116 		           (net->error_count > net->pf_threshold)) {
    117 			if (!(net->dest_state & SCTP_ADDR_PF)) {
    118 				net->dest_state |= SCTP_ADDR_PF;
    119 				net->last_active = sctp_get_tick_count();
    120 				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
    121 				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
    122 				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
    123 			}
    124 		}
    125 	}
    126 	if (stcb == NULL)
    127 		return (0);
    128 
    129 	if (net) {
    130 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
    131 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
    132 				sctp_misc_ints(SCTP_THRESHOLD_INCR,
    133 					       stcb->asoc.overall_error_count,
    134 					       (stcb->asoc.overall_error_count+1),
    135 					       SCTP_FROM_SCTP_TIMER,
    136 					       __LINE__);
    137 			}
    138 			stcb->asoc.overall_error_count++;
    139 		}
    140 	} else {
    141 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
    142 			sctp_misc_ints(SCTP_THRESHOLD_INCR,
    143 				       stcb->asoc.overall_error_count,
    144 				       (stcb->asoc.overall_error_count+1),
    145 				       SCTP_FROM_SCTP_TIMER,
    146 				       __LINE__);
    147 		}
    148 		stcb->asoc.overall_error_count++;
    149 	}
    150 	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
    151 		(void *)&stcb->asoc, stcb->asoc.overall_error_count,
    152 		(uint32_t)threshold,
    153 		((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
    154 	/*
    155 	 * We specifically do not do >= to give the assoc one more change
    156 	 * before we fail it.
    157 	 */
    158 	if (stcb->asoc.overall_error_count > threshold) {
    159 		/* Abort notification sends a ULP notify */
    160 		struct mbuf *op_err;
    161 
    162 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
    163 		                             "Association error couter exceeded");
    164 		inp->last_abort_code = SCTP_FROM_SCTP_TIMER+SCTP_LOC_1;
    165 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
    166 		return (1);
    167 	}
    168 	return (0);
    169 }
    170 
    171 /*
    172  * sctp_find_alternate_net() returns a non-NULL pointer as long
    173  * the argument net is non-NULL.
    174  */
    175 struct sctp_nets *
    176 sctp_find_alternate_net(struct sctp_tcb *stcb,
    177     struct sctp_nets *net,
    178     int mode)
    179 {
    180 	/* Find and return an alternate network if possible */
    181 	struct sctp_nets *alt, *mnet, *min_errors_net = NULL , *max_cwnd_net = NULL;
    182 	int once;
    183 	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
    184 	int min_errors = -1;
    185 	uint32_t max_cwnd = 0;
    186 
    187 	if (stcb->asoc.numnets == 1) {
    188 		/* No others but net */
    189 		return (TAILQ_FIRST(&stcb->asoc.nets));
    190 	}
    191 	/*
    192 	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate net algorithm.
    193 	 * This algorithm chooses the active destination (not in PF state) with the largest
    194 	 * cwnd value. If all destinations are in PF state, unreachable, or unconfirmed, choose
    195 	 * the desination that is in PF state with the lowest error count. In case of a tie,
    196 	 * choose the destination that was most recently active.
    197 	 */
    198 	if (mode == 2) {
    199 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
    200 			/* JRS 5/14/07 - If the destination is unreachable or unconfirmed, skip it. */
    201 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
    202 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
    203 				continue;
    204 			}
    205 			/*
    206 			 * JRS 5/14/07 -  If the destination is reachable but in PF state, compare
    207 			 *  the error count of the destination to the minimum error count seen thus far.
    208 			 *  Store the destination with the lower error count.  If the error counts are
    209 			 *  equal, store the destination that was most recently active.
    210 			 */
    211 			if (mnet->dest_state & SCTP_ADDR_PF) {
    212 				/*
    213 				 * JRS 5/14/07 - If the destination under consideration is the current
    214 				 *  destination, work as if the error count is one higher.  The
    215 				 *  actual error count will not be incremented until later in the
    216 				 *  t3 handler.
    217 				 */
    218 				if (mnet == net) {
    219  					if (min_errors == -1) {
    220 						min_errors = mnet->error_count + 1;
    221 						min_errors_net = mnet;
    222 					} else if (mnet->error_count + 1 < min_errors) {
    223 						min_errors = mnet->error_count + 1;
    224 						min_errors_net = mnet;
    225 					} else if (mnet->error_count + 1 == min_errors
    226 								&& mnet->last_active > min_errors_net->last_active) {
    227 						min_errors_net = mnet;
    228 						min_errors = mnet->error_count + 1;
    229 					}
    230 					continue;
    231 				} else {
    232  					if (min_errors == -1) {
    233 						min_errors = mnet->error_count;
    234 						min_errors_net = mnet;
    235 					} else if (mnet->error_count < min_errors) {
    236 						min_errors = mnet->error_count;
    237 						min_errors_net = mnet;
    238 					} else if (mnet->error_count == min_errors
    239 								&& mnet->last_active > min_errors_net->last_active) {
    240 						min_errors_net = mnet;
    241 						min_errors = mnet->error_count;
    242 					}
    243 					continue;
    244 				}
    245 			}
    246 			/*
    247 			 * JRS 5/14/07 - If the destination is reachable and not in PF state, compare the
    248 			 *  cwnd of the destination to the highest cwnd seen thus far.  Store the
    249 			 *  destination with the higher cwnd value.  If the cwnd values are equal,
    250 			 *  randomly choose one of the two destinations.
    251 			 */
    252 			if (max_cwnd < mnet->cwnd) {
    253 				max_cwnd_net = mnet;
    254 				max_cwnd = mnet->cwnd;
    255 			} else if (max_cwnd == mnet->cwnd) {
    256 				uint32_t rndval;
    257 				uint8_t this_random;
    258 
    259 				if (stcb->asoc.hb_random_idx > 3) {
    260 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
    261 					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
    262 					this_random = stcb->asoc.hb_random_values[0];
    263 					stcb->asoc.hb_random_idx++;
    264 					stcb->asoc.hb_ect_randombit = 0;
    265 				} else {
    266 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
    267 					stcb->asoc.hb_random_idx++;
    268 					stcb->asoc.hb_ect_randombit = 0;
    269 				}
    270 				if (this_random % 2 == 1) {
    271 					max_cwnd_net = mnet;
    272 					max_cwnd = mnet->cwnd; /* Useless? */
    273 				}
    274 			}
    275 		}
    276 		if (max_cwnd_net == NULL) {
    277 			if (min_errors_net == NULL) {
    278 				return (net);
    279 			}
    280 			return (min_errors_net);
    281 		} else {
    282 			return (max_cwnd_net);
    283 		}
    284 	} /* JRS 5/14/07 - If mode is set to 1, use the CMT policy for choosing an alternate net. */
    285 	else if (mode == 1) {
    286 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
    287 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
    288 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
    289 				/*
    290 				 * will skip ones that are not-reachable or
    291 				 * unconfirmed
    292 				 */
    293 				continue;
    294 			}
    295 			if (max_cwnd < mnet->cwnd) {
    296 				max_cwnd_net = mnet;
    297 				max_cwnd = mnet->cwnd;
    298 			} else if (max_cwnd == mnet->cwnd) {
    299 				uint32_t rndval;
    300 				uint8_t this_random;
    301 
    302 				if (stcb->asoc.hb_random_idx > 3) {
    303 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
    304 					memcpy(stcb->asoc.hb_random_values, &rndval,
    305 					    sizeof(stcb->asoc.hb_random_values));
    306 					this_random = stcb->asoc.hb_random_values[0];
    307 					stcb->asoc.hb_random_idx = 0;
    308 					stcb->asoc.hb_ect_randombit = 0;
    309 				} else {
    310 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
    311 					stcb->asoc.hb_random_idx++;
    312 					stcb->asoc.hb_ect_randombit = 0;
    313 				}
    314 				if (this_random % 2) {
    315 					max_cwnd_net = mnet;
    316 					max_cwnd = mnet->cwnd;
    317 				}
    318 			}
    319 		}
    320 		if (max_cwnd_net) {
    321 			return (max_cwnd_net);
    322 		}
    323 	}
    324 	mnet = net;
    325 	once = 0;
    326 
    327 	if (mnet == NULL) {
    328 		mnet = TAILQ_FIRST(&stcb->asoc.nets);
    329 		if (mnet == NULL) {
    330 			return (NULL);
    331 		}
    332 	}
    333 	do {
    334 		alt = TAILQ_NEXT(mnet, sctp_next);
    335 		if (alt == NULL)
    336  		{
    337 			once++;
    338 			if (once > 1) {
    339 				break;
    340 			}
    341 			alt = TAILQ_FIRST(&stcb->asoc.nets);
    342 			if (alt == NULL) {
    343 				return (NULL);
    344 			}
    345 		}
    346 		if (alt->ro.ro_rt == NULL) {
    347 			if (alt->ro._s_addr) {
    348 				sctp_free_ifa(alt->ro._s_addr);
    349 				alt->ro._s_addr = NULL;
    350 			}
    351 			alt->src_addr_selected = 0;
    352 		}
    353 		/*sa_ignore NO_NULL_CHK*/
    354 		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
    355 		    (alt->ro.ro_rt != NULL) &&
    356 		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
    357 			/* Found a reachable address */
    358 			break;
    359 		}
    360 		mnet = alt;
    361 	} while (alt != NULL);
    362 
    363 	if (alt == NULL) {
    364 		/* Case where NO insv network exists (dormant state) */
    365 		/* we rotate destinations */
    366 		once = 0;
    367 		mnet = net;
    368 		do {
    369 			if (mnet == NULL) {
    370 				return (TAILQ_FIRST(&stcb->asoc.nets));
    371 			}
    372 			alt = TAILQ_NEXT(mnet, sctp_next);
    373 			if (alt == NULL) {
    374 				once++;
    375 				if (once > 1) {
    376 					break;
    377 				}
    378 				alt = TAILQ_FIRST(&stcb->asoc.nets);
    379 			}
    380 			/*sa_ignore NO_NULL_CHK*/
    381 			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
    382 			    (alt != net)) {
    383 				/* Found an alternate address */
    384 				break;
    385 			}
    386 			mnet = alt;
    387 		} while (alt != NULL);
    388 	}
    389 	if (alt == NULL) {
    390 		return (net);
    391 	}
    392 	return (alt);
    393 }
    394 
    395 static void
    396 sctp_backoff_on_timeout(struct sctp_tcb *stcb,
    397     struct sctp_nets *net,
    398     int win_probe,
    399     int num_marked, int num_abandoned)
    400 {
    401 	if (net->RTO == 0) {
    402 		net->RTO = stcb->asoc.minrto;
    403 	}
    404 	net->RTO <<= 1;
    405 	if (net->RTO > stcb->asoc.maxrto) {
    406 		net->RTO = stcb->asoc.maxrto;
    407 	}
    408 	if ((win_probe == 0) && (num_marked || num_abandoned)) {
    409 		/* We don't apply penalty to window probe scenarios */
    410 		/* JRS - Use the congestion control given in the CC module */
    411 		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
    412 	}
    413 }
    414 
    415 #ifndef INVARIANTS
    416 static void
    417 sctp_recover_sent_list(struct sctp_tcb *stcb)
    418 {
    419 	struct sctp_tmit_chunk *chk, *nchk;
    420 	struct sctp_association *asoc;
    421 
    422 	asoc = &stcb->asoc;
    423 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
    424 		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
    425 			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
    426 			            (void *)chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
    427 			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
    428 				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
    429 					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
    430 				}
    431 			}
    432 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
    433 			if (PR_SCTP_ENABLED(chk->flags)) {
    434 				if (asoc->pr_sctp_cnt != 0)
    435 					asoc->pr_sctp_cnt--;
    436 			}
    437 			if (chk->data) {
    438 				/*sa_ignore NO_NULL_CHK*/
    439 				sctp_free_bufspace(stcb, asoc, chk, 1);
    440 				sctp_m_freem(chk->data);
    441 				chk->data = NULL;
    442 				if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(chk->flags)) {
    443 					asoc->sent_queue_cnt_removeable--;
    444 				}
    445 			}
    446 			asoc->sent_queue_cnt--;
    447 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
    448 		}
    449 	}
    450 	SCTP_PRINTF("after recover order is as follows\n");
    451 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
    452 		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.TSN_seq);
    453 	}
    454 }
    455 #endif
    456 
    457 static int
    458 sctp_mark_all_for_resend(struct sctp_tcb *stcb,
    459     struct sctp_nets *net,
    460     struct sctp_nets *alt,
    461     int window_probe,
    462     int *num_marked,
    463     int *num_abandoned)
    464 {
    465 
    466 	/*
    467 	 * Mark all chunks (well not all) that were sent to *net for
    468 	 * retransmission. Move them to alt for there destination as well...
    469 	 * We only mark chunks that have been outstanding long enough to
    470 	 * have received feed-back.
    471 	 */
    472 	struct sctp_tmit_chunk *chk, *nchk;
    473 	struct sctp_nets *lnets;
    474 	struct timeval now, min_wait, tv;
    475 	int cur_rto;
    476 	int cnt_abandoned;
    477 	int audit_tf, num_mk, fir;
    478 	unsigned int cnt_mk;
    479 	uint32_t orig_flight, orig_tf;
    480 	uint32_t tsnlast, tsnfirst;
    481 	int recovery_cnt = 0;
    482 
    483 
    484 	/* none in flight now */
    485 	audit_tf = 0;
    486 	fir = 0;
    487 	/*
    488 	 * figure out how long a data chunk must be pending before we can
    489 	 * mark it ..
    490 	 */
    491 	(void)SCTP_GETTIME_TIMEVAL(&now);
    492 	/* get cur rto in micro-seconds */
    493 	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
    494 	cur_rto *= 1000;
    495 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    496 		sctp_log_fr(cur_rto,
    497 			    stcb->asoc.peers_rwnd,
    498 			    window_probe,
    499 			    SCTP_FR_T3_MARK_TIME);
    500 		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
    501 		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
    502 	}
    503 	tv.tv_sec = cur_rto / 1000000;
    504 	tv.tv_usec = cur_rto % 1000000;
    505 #ifndef __FreeBSD__
    506 	timersub(&now, &tv, &min_wait);
    507 #else
    508 	min_wait = now;
    509 	timevalsub(&min_wait, &tv);
    510 #endif
    511 	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
    512 		/*
    513 		 * if we hit here, we don't have enough seconds on the clock
    514 		 * to account for the RTO. We just let the lower seconds be
    515 		 * the bounds and don't worry about it. This may mean we
    516 		 * will mark a lot more than we should.
    517 		 */
    518 		min_wait.tv_sec = min_wait.tv_usec = 0;
    519 	}
    520 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    521 		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
    522 		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
    523 	}
    524 	/*
    525 	 * Our rwnd will be incorrect here since we are not adding back the
    526 	 * cnt * mbuf but we will fix that down below.
    527 	 */
    528 	orig_flight = net->flight_size;
    529 	orig_tf = stcb->asoc.total_flight;
    530 
    531 	net->fast_retran_ip = 0;
    532 	/* Now on to each chunk */
    533 	cnt_abandoned = 0;
    534 	num_mk = cnt_mk = 0;
    535 	tsnfirst = tsnlast = 0;
    536 #ifndef INVARIANTS
    537  start_again:
    538 #endif
    539 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
    540 		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
    541 			/* Strange case our list got out of order? */
    542 			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
    543 			            (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
    544 			recovery_cnt++;
    545 #ifdef INVARIANTS
    546 			panic("last acked >= chk on sent-Q");
    547 #else
    548 			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
    549 			sctp_recover_sent_list(stcb);
    550 			if (recovery_cnt < 10) {
    551 				goto start_again;
    552 			} else {
    553 				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
    554 			}
    555 #endif
    556 		}
    557 		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
    558 			/*
    559 			 * found one to mark: If it is less than
    560 			 * DATAGRAM_ACKED it MUST not be a skipped or marked
    561 			 * TSN but instead one that is either already set
    562 			 * for retransmission OR one that needs
    563 			 * retransmission.
    564 			 */
    565 
    566 			/* validate its been outstanding long enough */
    567 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    568 				sctp_log_fr(chk->rec.data.TSN_seq,
    569 					    chk->sent_rcv_time.tv_sec,
    570 					    chk->sent_rcv_time.tv_usec,
    571 					    SCTP_FR_T3_MARK_TIME);
    572 			}
    573 			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
    574 				/*
    575 				 * we have reached a chunk that was sent
    576 				 * some seconds past our min.. forget it we
    577 				 * will find no more to send.
    578 				 */
    579 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    580 					sctp_log_fr(0,
    581 						    chk->sent_rcv_time.tv_sec,
    582 						    chk->sent_rcv_time.tv_usec,
    583 						    SCTP_FR_T3_STOPPED);
    584 				}
    585 				continue;
    586 			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
    587 				   (window_probe == 0)) {
    588 				/*
    589 				 * we must look at the micro seconds to
    590 				 * know.
    591 				 */
    592 				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
    593 					/*
    594 					 * ok it was sent after our boundary
    595 					 * time.
    596 					 */
    597 					continue;
    598 				}
    599 			}
    600 			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_TTL_ENABLED(chk->flags)) {
    601 				/* Is it expired? */
    602 #ifndef __FreeBSD__
    603 				if (timercmp(&now, &chk->rec.data.timetodrop, >)) {
    604 #else
    605 				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
    606 #endif
    607 					/* Yes so drop it */
    608 					if (chk->data) {
    609 						(void)sctp_release_pr_sctp_chunk(stcb,
    610 										 chk,
    611 										 1,
    612 										 SCTP_SO_NOT_LOCKED);
    613 						cnt_abandoned++;
    614 					}
    615 					continue;
    616 				}
    617 			}
    618 			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_RTX_ENABLED(chk->flags)) {
    619 				/* Has it been retransmitted tv_sec times? */
    620 				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
    621 					if (chk->data) {
    622 						(void)sctp_release_pr_sctp_chunk(stcb,
    623 										 chk,
    624 										 1,
    625 										 SCTP_SO_NOT_LOCKED);
    626 						cnt_abandoned++;
    627 					}
    628 					continue;
    629 				}
    630 			}
    631 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
    632 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
    633 				num_mk++;
    634 				if (fir == 0) {
    635 					fir = 1;
    636 					tsnfirst = chk->rec.data.TSN_seq;
    637 				}
    638 				tsnlast = chk->rec.data.TSN_seq;
    639 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    640 					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
    641 						    0, SCTP_FR_T3_MARKED);
    642 				}
    643 
    644 				if (chk->rec.data.chunk_was_revoked) {
    645 					/* deflate the cwnd */
    646 					chk->whoTo->cwnd -= chk->book_size;
    647 					chk->rec.data.chunk_was_revoked = 0;
    648 				}
    649 				net->marked_retrans++;
    650 				stcb->asoc.marked_retrans++;
    651 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
    652 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
    653 						       chk->whoTo->flight_size,
    654 						       chk->book_size,
    655 						       (uintptr_t)chk->whoTo,
    656 						       chk->rec.data.TSN_seq);
    657 				}
    658 				sctp_flight_size_decrease(chk);
    659 				sctp_total_flight_decrease(stcb, chk);
    660 				stcb->asoc.peers_rwnd += chk->send_size;
    661 				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
    662 			}
    663 			chk->sent = SCTP_DATAGRAM_RESEND;
    664 			SCTP_STAT_INCR(sctps_markedretrans);
    665 
    666 			/* reset the TSN for striking and other FR stuff */
    667 			chk->rec.data.doing_fast_retransmit = 0;
    668 			/* Clear any time so NO RTT is being done */
    669 
    670 			if (chk->do_rtt) {
    671 				if (chk->whoTo->rto_needed == 0) {
    672 					chk->whoTo->rto_needed = 1;
    673 				}
    674 			}
    675 			chk->do_rtt = 0;
    676 			if (alt != net) {
    677 				sctp_free_remote_addr(chk->whoTo);
    678 				chk->no_fr_allowed = 1;
    679 				chk->whoTo = alt;
    680 				atomic_add_int(&alt->ref_count, 1);
    681 			} else {
    682 				chk->no_fr_allowed = 0;
    683 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
    684 					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
    685 				} else {
    686 					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
    687 				}
    688 			}
    689 			/* CMT: Do not allow FRs on retransmitted TSNs.
    690 			 */
    691 			if (stcb->asoc.sctp_cmt_on_off > 0) {
    692 				chk->no_fr_allowed = 1;
    693 			}
    694 #ifdef THIS_SHOULD_NOT_BE_DONE
    695 		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
    696 			/* remember highest acked one */
    697 			could_be_sent = chk;
    698 #endif
    699 		}
    700 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
    701 			cnt_mk++;
    702 		}
    703 	}
    704 	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
    705 		/* we did not subtract the same things? */
    706 		audit_tf = 1;
    707 	}
    708 
    709 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    710 		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
    711 	}
    712 #ifdef SCTP_DEBUG
    713 	if (num_mk) {
    714 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
    715 			tsnlast);
    716 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
    717 			num_mk, (u_long)stcb->asoc.peers_rwnd);
    718 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
    719 			tsnlast);
    720 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
    721 			num_mk,
    722 			(int)stcb->asoc.peers_rwnd);
    723 	}
    724 #endif
    725 	*num_marked = num_mk;
    726 	*num_abandoned = cnt_abandoned;
    727 	/* Now check for a ECN Echo that may be stranded And
    728 	 * include the cnt_mk'd to have all resends in the
    729 	 * control queue.
    730 	 */
    731 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
    732 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
    733 			cnt_mk++;
    734 		}
    735 		if ((chk->whoTo == net) &&
    736 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
    737 			sctp_free_remote_addr(chk->whoTo);
    738 			chk->whoTo = alt;
    739 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
    740 				chk->sent = SCTP_DATAGRAM_RESEND;
    741 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
    742 				cnt_mk++;
    743 			}
    744 			atomic_add_int(&alt->ref_count, 1);
    745 		}
    746 	}
    747 #ifdef THIS_SHOULD_NOT_BE_DONE
    748 	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
    749 		/* fix it so we retransmit the highest acked anyway */
    750 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
    751 		cnt_mk++;
    752 		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
    753 	}
    754 #endif
    755 	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
    756 #ifdef INVARIANTS
    757 		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
    758 			    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
    759 #endif
    760 #ifndef SCTP_AUDITING_ENABLED
    761 		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
    762 #endif
    763 	}
    764 	if (audit_tf) {
    765 		SCTPDBG(SCTP_DEBUG_TIMER4,
    766 			"Audit total flight due to negative value net:%p\n",
    767 			(void *)net);
    768 		stcb->asoc.total_flight = 0;
    769 		stcb->asoc.total_flight_count = 0;
    770 		/* Clear all networks flight size */
    771 		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
    772 			lnets->flight_size = 0;
    773 			SCTPDBG(SCTP_DEBUG_TIMER4,
    774 				"Net:%p c-f cwnd:%d ssthresh:%d\n",
    775 				(void *)lnets, lnets->cwnd, lnets->ssthresh);
    776 		}
    777 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
    778 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
    779 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
    780 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
    781 						       chk->whoTo->flight_size,
    782 						       chk->book_size,
    783 						       (uintptr_t)chk->whoTo,
    784 						       chk->rec.data.TSN_seq);
    785 				}
    786 
    787 				sctp_flight_size_increase(chk);
    788 				sctp_total_flight_increase(stcb, chk);
    789 			}
    790 		}
    791 	}
    792 	/* We return 1 if we only have a window probe outstanding */
    793 	return (0);
    794 }
    795 
    796 
    797 int
    798 sctp_t3rxt_timer(struct sctp_inpcb *inp,
    799     struct sctp_tcb *stcb,
    800     struct sctp_nets *net)
    801 {
    802 	struct sctp_nets *alt;
    803 	int win_probe, num_mk, num_abandoned;
    804 
    805 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
    806 		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
    807 	}
    808 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
    809 		struct sctp_nets *lnet;
    810 
    811 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
    812 			if (net == lnet) {
    813 				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
    814 			} else {
    815 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
    816 			}
    817 		}
    818 	}
    819 	/* Find an alternate and mark those for retransmission */
    820 	if ((stcb->asoc.peers_rwnd == 0) &&
    821 	    (stcb->asoc.total_flight < net->mtu)) {
    822 		SCTP_STAT_INCR(sctps_timowindowprobe);
    823 		win_probe = 1;
    824 	} else {
    825 		win_probe = 0;
    826 	}
    827 
    828 	if (win_probe == 0) {
    829 		/* We don't do normal threshold management on window probes */
    830 		if (sctp_threshold_management(inp, stcb, net,
    831 		    stcb->asoc.max_send_times)) {
    832 			/* Association was destroyed */
    833 			return (1);
    834 		} else {
    835 			if (net != stcb->asoc.primary_destination) {
    836 				/* send a immediate HB if our RTO is stale */
    837 				struct timeval now;
    838 				unsigned int ms_goneby;
    839 
    840 				(void)SCTP_GETTIME_TIMEVAL(&now);
    841 				if (net->last_sent_time.tv_sec) {
    842 					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
    843 				} else {
    844 					ms_goneby = 0;
    845 				}
    846 				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
    847 					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
    848 						/*
    849 						 * no recent feed back in an RTO or
    850 						 * more, request a RTT update
    851 						 */
    852 						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
    853 					}
    854 				}
    855 			}
    856 		}
    857 	} else {
    858 		/*
    859 		 * For a window probe we don't penalize the net's but only
    860 		 * the association. This may fail it if SACKs are not coming
    861 		 * back. If sack's are coming with rwnd locked at 0, we will
    862 		 * continue to hold things waiting for rwnd to raise
    863 		 */
    864 		if (sctp_threshold_management(inp, stcb, NULL,
    865 		    stcb->asoc.max_send_times)) {
    866 			/* Association was destroyed */
    867 			return (1);
    868 		}
    869 	}
    870 	if (stcb->asoc.sctp_cmt_on_off > 0) {
    871 		if (net->pf_threshold < net->failure_threshold) {
    872 			alt = sctp_find_alternate_net(stcb, net, 2);
    873 		} else {
    874 		        /*
    875 			 * CMT: Using RTX_SSTHRESH policy for CMT.
    876 			 * If CMT is being used, then pick dest with
    877 			 * largest ssthresh for any retransmission.
    878 			 */
    879 			alt = sctp_find_alternate_net(stcb, net, 1);
    880 			/*
    881 			 * CUCv2: If a different dest is picked for
    882 			 * the retransmission, then new
    883 			 * (rtx-)pseudo_cumack needs to be tracked
    884 			 * for orig dest. Let CUCv2 track new (rtx-)
    885 			 * pseudo-cumack always.
    886 			 */
    887 			net->find_pseudo_cumack = 1;
    888 			net->find_rtx_pseudo_cumack = 1;
    889 		}
    890 	} else {
    891 		alt = sctp_find_alternate_net(stcb, net, 0);
    892 	}
    893 
    894 	num_mk = 0;
    895 	num_abandoned = 0;
    896 	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
    897 				      &num_mk, &num_abandoned);
    898 	/* FR Loss recovery just ended with the T3. */
    899 	stcb->asoc.fast_retran_loss_recovery = 0;
    900 
    901 	/* CMT FR loss recovery ended with the T3 */
    902 	net->fast_retran_loss_recovery = 0;
    903 	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
    904 	    (net->flight_size == 0)) {
    905 		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net);
    906 	}
    907 
    908 	/*
    909 	 * setup the sat loss recovery that prevents satellite cwnd advance.
    910 	 */
    911 	stcb->asoc.sat_t3_loss_recovery = 1;
    912 	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
    913 
    914 	/* Backoff the timer and cwnd */
    915 	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
    916 	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
    917 	    (net->dest_state & SCTP_ADDR_PF)) {
    918 		/* Move all pending over too */
    919 		sctp_move_chunks_from_net(stcb, net);
    920 
    921 		/* Get the address that failed, to
    922 		 * force a new src address selecton and
    923 		 * a route allocation.
    924 		 */
    925 		if (net->ro._s_addr) {
    926 			sctp_free_ifa(net->ro._s_addr);
    927 	 		net->ro._s_addr = NULL;
    928 		}
    929  		net->src_addr_selected = 0;
    930 
    931 		/* Force a route allocation too */
    932 		if (net->ro.ro_rt) {
    933 			RTFREE(net->ro.ro_rt);
    934 			net->ro.ro_rt = NULL;
    935 		}
    936 
    937 		/* Was it our primary? */
    938 		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
    939 			/*
    940 			 * Yes, note it as such and find an alternate note:
    941 			 * this means HB code must use this to resent the
    942 			 * primary if it goes active AND if someone does a
    943 			 * change-primary then this flag must be cleared
    944 			 * from any net structures.
    945 			 */
    946 			if (stcb->asoc.alternate) {
    947 				sctp_free_remote_addr(stcb->asoc.alternate);
    948 			}
    949 			stcb->asoc.alternate = alt;
    950 			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
    951 		}
    952 	}
    953 	/*
    954 	 * Special case for cookie-echo'ed case, we don't do output but must
    955 	 * await the COOKIE-ACK before retransmission
    956 	 */
    957 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
    958 		/*
    959 		 * Here we just reset the timer and start again since we
    960 		 * have not established the asoc
    961 		 */
    962 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
    963 		return (0);
    964 	}
    965 	if (stcb->asoc.peer_supports_prsctp) {
    966 		struct sctp_tmit_chunk *lchk;
    967 
    968 		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
    969 		/* C3. See if we need to send a Fwd-TSN */
    970 		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
    971 			send_forward_tsn(stcb, &stcb->asoc);
    972 			if (lchk) {
    973 				/* Assure a timer is up */
    974 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
    975 			}
    976 		}
    977 	}
    978 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
    979 		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
    980 	}
    981 	return (0);
    982 }
    983 
    984 int
    985 sctp_t1init_timer(struct sctp_inpcb *inp,
    986     struct sctp_tcb *stcb,
    987     struct sctp_nets *net)
    988 {
    989 	/* bump the thresholds */
    990 	if (stcb->asoc.delayed_connection) {
    991 		/*
    992 		 * special hook for delayed connection. The library did NOT
    993 		 * complete the rest of its sends.
    994 		 */
    995 		stcb->asoc.delayed_connection = 0;
    996 		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
    997 		return (0);
    998 	}
    999 	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
   1000 		return (0);
   1001 	}
   1002 	if (sctp_threshold_management(inp, stcb, net,
   1003 	    stcb->asoc.max_init_times)) {
   1004 		/* Association was destroyed */
   1005 		return (1);
   1006 	}
   1007 	stcb->asoc.dropped_special_cnt = 0;
   1008 	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
   1009 	if (stcb->asoc.initial_init_rto_max < net->RTO) {
   1010 		net->RTO = stcb->asoc.initial_init_rto_max;
   1011 	}
   1012 	if (stcb->asoc.numnets > 1) {
   1013 		/* If we have more than one addr use it */
   1014 		struct sctp_nets *alt;
   1015 
   1016 		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
   1017 		if (alt != stcb->asoc.primary_destination) {
   1018 			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
   1019 			stcb->asoc.primary_destination = alt;
   1020 		}
   1021 	}
   1022 	/* Send out a new init */
   1023 	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
   1024 	return (0);
   1025 }
   1026 
   1027 /*
   1028  * For cookie and asconf we actually need to find and mark for resend, then
   1029  * increment the resend counter (after all the threshold management stuff of
   1030  * course).
   1031  */
   1032 int
   1033 sctp_cookie_timer(struct sctp_inpcb *inp,
   1034     struct sctp_tcb *stcb,
   1035     struct sctp_nets *net SCTP_UNUSED)
   1036 {
   1037 	struct sctp_nets *alt;
   1038 	struct sctp_tmit_chunk *cookie;
   1039 
   1040 	/* first before all else we must find the cookie */
   1041 	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
   1042 		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
   1043 			break;
   1044 		}
   1045 	}
   1046 	if (cookie == NULL) {
   1047 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
   1048 			/* FOOBAR! */
   1049 			struct mbuf *op_err;
   1050 
   1051 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
   1052 			                             "Cookie timer expired, but no cookie");
   1053 			inp->last_abort_code = SCTP_FROM_SCTP_TIMER+SCTP_LOC_4;
   1054 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
   1055 		} else {
   1056 #ifdef INVARIANTS
   1057 			panic("Cookie timer expires in wrong state?");
   1058 #else
   1059 			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
   1060 			return (0);
   1061 #endif
   1062 		}
   1063 		return (0);
   1064 	}
   1065 	/* Ok we found the cookie, threshold management next */
   1066 	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
   1067 	    stcb->asoc.max_init_times)) {
   1068 		/* Assoc is over */
   1069 		return (1);
   1070 	}
   1071 	/*
   1072 	 * cleared theshold management now lets backoff the address & select
   1073 	 * an alternate
   1074 	 */
   1075 	stcb->asoc.dropped_special_cnt = 0;
   1076 	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
   1077 	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
   1078 	if (alt != cookie->whoTo) {
   1079 		sctp_free_remote_addr(cookie->whoTo);
   1080 		cookie->whoTo = alt;
   1081 		atomic_add_int(&alt->ref_count, 1);
   1082 	}
   1083 	/* Now mark the retran info */
   1084 	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
   1085 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1086 	}
   1087 	cookie->sent = SCTP_DATAGRAM_RESEND;
   1088 	/*
   1089 	 * Now call the output routine to kick out the cookie again, Note we
   1090 	 * don't mark any chunks for retran so that FR will need to kick in
   1091 	 * to move these (or a send timer).
   1092 	 */
   1093 	return (0);
   1094 }
   1095 
   1096 int
   1097 sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1098     struct sctp_nets *net)
   1099 {
   1100 	struct sctp_nets *alt;
   1101 	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
   1102 
   1103 	if (stcb->asoc.stream_reset_outstanding == 0) {
   1104 		return (0);
   1105 	}
   1106 	/* find the existing STRRESET, we use the seq number we sent out on */
   1107 	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
   1108 	if (strrst == NULL) {
   1109 		return (0);
   1110 	}
   1111 	/* do threshold management */
   1112 	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
   1113 	    stcb->asoc.max_send_times)) {
   1114 		/* Assoc is over */
   1115 		return (1);
   1116 	}
   1117 	/*
   1118 	 * cleared theshold management now lets backoff the address & select
   1119 	 * an alternate
   1120 	 */
   1121 	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
   1122 	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
   1123 	sctp_free_remote_addr(strrst->whoTo);
   1124 	strrst->whoTo = alt;
   1125 	atomic_add_int(&alt->ref_count, 1);
   1126 
   1127 	/* See if a ECN Echo is also stranded */
   1128 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
   1129 		if ((chk->whoTo == net) &&
   1130 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
   1131 			sctp_free_remote_addr(chk->whoTo);
   1132 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
   1133 				chk->sent = SCTP_DATAGRAM_RESEND;
   1134 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1135 			}
   1136 			chk->whoTo = alt;
   1137 			atomic_add_int(&alt->ref_count, 1);
   1138 		}
   1139 	}
   1140 	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
   1141 		/*
   1142 		 * If the address went un-reachable, we need to move to
   1143 		 * alternates for ALL chk's in queue
   1144 		 */
   1145 		sctp_move_chunks_from_net(stcb, net);
   1146 	}
   1147 	/* mark the retran info */
   1148 	if (strrst->sent != SCTP_DATAGRAM_RESEND)
   1149 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1150 	strrst->sent = SCTP_DATAGRAM_RESEND;
   1151 
   1152 	/* restart the timer */
   1153 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
   1154 	return (0);
   1155 }
   1156 
   1157 int
   1158 sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1159 		  struct sctp_nets *net)
   1160 {
   1161 	struct sctp_nets *alt;
   1162 	struct sctp_tmit_chunk *asconf, *chk;
   1163 
   1164 	/* is this a first send, or a retransmission? */
   1165 	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
   1166 		/* compose a new ASCONF chunk and send it */
   1167 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
   1168 	} else {
   1169 		/*
   1170 		 * Retransmission of the existing ASCONF is needed
   1171 		 */
   1172 
   1173 		/* find the existing ASCONF */
   1174 		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
   1175 		if (asconf == NULL) {
   1176 			return (0);
   1177 		}
   1178 		/* do threshold management */
   1179 		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
   1180 		    stcb->asoc.max_send_times)) {
   1181 			/* Assoc is over */
   1182 			return (1);
   1183 		}
   1184 		if (asconf->snd_count > stcb->asoc.max_send_times) {
   1185 			/*
   1186 			 * Something is rotten: our peer is not responding to
   1187 			 * ASCONFs but apparently is to other chunks.  i.e. it
   1188 			 * is not properly handling the chunk type upper bits.
   1189 			 * Mark this peer as ASCONF incapable and cleanup.
   1190 			 */
   1191 			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
   1192 			sctp_asconf_cleanup(stcb, net);
   1193 			return (0);
   1194 		}
   1195 		/*
   1196 		 * cleared threshold management, so now backoff the net and
   1197 		 * select an alternate
   1198 		 */
   1199 		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
   1200 		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
   1201 		if (asconf->whoTo != alt) {
   1202 			sctp_free_remote_addr(asconf->whoTo);
   1203 			asconf->whoTo = alt;
   1204 			atomic_add_int(&alt->ref_count, 1);
   1205 		}
   1206 
   1207 		/* See if an ECN Echo is also stranded */
   1208 		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
   1209 			if ((chk->whoTo == net) &&
   1210 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
   1211 				sctp_free_remote_addr(chk->whoTo);
   1212 				chk->whoTo = alt;
   1213 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
   1214 					chk->sent = SCTP_DATAGRAM_RESEND;
   1215 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1216 				}
   1217 				atomic_add_int(&alt->ref_count, 1);
   1218 			}
   1219 		}
   1220 		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
   1221 			if (chk->whoTo != alt) {
   1222 				sctp_free_remote_addr(chk->whoTo);
   1223 				chk->whoTo = alt;
   1224 				atomic_add_int(&alt->ref_count, 1);
   1225 			}
   1226 			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
   1227 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1228 			chk->sent = SCTP_DATAGRAM_RESEND;
   1229 		}
   1230 		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
   1231 			/*
   1232 			 * If the address went un-reachable, we need to move
   1233 			 * to the alternate for ALL chunks in queue
   1234 			 */
   1235 			sctp_move_chunks_from_net(stcb, net);
   1236 		}
   1237 		/* mark the retran info */
   1238 		if (asconf->sent != SCTP_DATAGRAM_RESEND)
   1239 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
   1240 		asconf->sent = SCTP_DATAGRAM_RESEND;
   1241 
   1242 		/* send another ASCONF if any and we can do */
   1243 		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
   1244 	}
   1245 	return (0);
   1246 }
   1247 
   1248 /* Mobility adaptation */
   1249 void
   1250 sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1251                        struct sctp_nets *net SCTP_UNUSED)
   1252 {
   1253 	if (stcb->asoc.deleted_primary == NULL) {
   1254 		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
   1255 		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
   1256 		return;
   1257 	}
   1258 	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
   1259 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
   1260 	sctp_free_remote_addr(stcb->asoc.deleted_primary);
   1261 	stcb->asoc.deleted_primary = NULL;
   1262 	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
   1263 	return;
   1264 }
   1265 
   1266 /*
   1267  * For the shutdown and shutdown-ack, we do not keep one around on the
   1268  * control queue. This means we must generate a new one and call the general
   1269  * chunk output routine, AFTER having done threshold management.
   1270  * It is assumed that net is non-NULL.
   1271  */
   1272 int
   1273 sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1274     struct sctp_nets *net)
   1275 {
   1276 	struct sctp_nets *alt;
   1277 
   1278 	/* first threshold managment */
   1279 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
   1280 		/* Assoc is over */
   1281 		return (1);
   1282 	}
   1283 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
   1284 	/* second select an alternative */
   1285 	alt = sctp_find_alternate_net(stcb, net, 0);
   1286 
   1287 	/* third generate a shutdown into the queue for out net */
   1288 	sctp_send_shutdown(stcb, alt);
   1289 
   1290 	/* fourth restart timer */
   1291 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
   1292 	return (0);
   1293 }
   1294 
   1295 int
   1296 sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1297     struct sctp_nets *net)
   1298 {
   1299 	struct sctp_nets *alt;
   1300 
   1301 	/* first threshold managment */
   1302 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
   1303 		/* Assoc is over */
   1304 		return (1);
   1305 	}
   1306 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
   1307 	/* second select an alternative */
   1308 	alt = sctp_find_alternate_net(stcb, net, 0);
   1309 
   1310 	/* third generate a shutdown into the queue for out net */
   1311 	sctp_send_shutdown_ack(stcb, alt);
   1312 
   1313 	/* fourth restart timer */
   1314 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
   1315 	return (0);
   1316 }
   1317 
   1318 static void
   1319 sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
   1320     struct sctp_tcb *stcb)
   1321 {
   1322 	struct sctp_stream_queue_pending *sp;
   1323 	unsigned int i, chks_in_queue = 0;
   1324 	int being_filled = 0;
   1325 	/*
   1326 	 * This function is ONLY called when the send/sent queues are empty.
   1327 	 */
   1328 	if ((stcb == NULL) || (inp == NULL))
   1329 		return;
   1330 
   1331 	if (stcb->asoc.sent_queue_retran_cnt) {
   1332 		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
   1333 			    stcb->asoc.sent_queue_retran_cnt);
   1334 		stcb->asoc.sent_queue_retran_cnt = 0;
   1335 	}
   1336 	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
   1337 		/* No stream scheduler information, initialize scheduler */
   1338 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
   1339 		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
   1340 			/* yep, we lost a stream or two */
   1341 			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
   1342 		} else {
   1343 			/* no streams lost */
   1344 			stcb->asoc.total_output_queue_size = 0;
   1345 		}
   1346 	}
   1347 	/* Check to see if some data queued, if so report it */
   1348 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
   1349 		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
   1350 			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
   1351 				if (sp->msg_is_complete)
   1352 					being_filled++;
   1353 				chks_in_queue++;
   1354 			}
   1355 		}
   1356 	}
   1357 	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
   1358 		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
   1359 			    stcb->asoc.stream_queue_cnt, chks_in_queue);
   1360 	}
   1361 	if (chks_in_queue) {
   1362 		/* call the output queue function */
   1363 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
   1364 		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
   1365 		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
   1366 			/*
   1367 			 * Probably should go in and make it go back through
   1368 			 * and add fragments allowed
   1369 			 */
   1370 			if (being_filled == 0) {
   1371 				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
   1372 					    chks_in_queue);
   1373 			}
   1374 		}
   1375 	} else {
   1376 		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
   1377 			    (u_long)stcb->asoc.total_output_queue_size);
   1378 		stcb->asoc.total_output_queue_size = 0;
   1379 	}
   1380 }
   1381 
   1382 int
   1383 sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   1384     struct sctp_nets *net)
   1385 {
   1386 	uint8_t net_was_pf;
   1387 
   1388 	if (net->dest_state & SCTP_ADDR_PF) {
   1389 		net_was_pf = 1;
   1390 	} else {
   1391 		net_was_pf = 0;
   1392 	}
   1393 	if (net->hb_responded == 0) {
   1394 		if (net->ro._s_addr) {
   1395 			/* Invalidate the src address if we did not get
   1396 			 * a response last time.
   1397 			 */
   1398 			sctp_free_ifa(net->ro._s_addr);
   1399 			net->ro._s_addr = NULL;
   1400 			net->src_addr_selected = 0;
   1401 		}
   1402 		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
   1403 		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
   1404 			/* Assoc is over */
   1405 			return (1);
   1406 		}
   1407 	}
   1408 	/* Zero PBA, if it needs it */
   1409 	if (net->partial_bytes_acked) {
   1410 		net->partial_bytes_acked = 0;
   1411 	}
   1412 	if ((stcb->asoc.total_output_queue_size > 0) &&
   1413 	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
   1414 	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
   1415 		sctp_audit_stream_queues_for_size(inp, stcb);
   1416 	}
   1417 	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
   1418 	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
   1419 		/* when move to PF during threshold mangement, a HB has been
   1420 		   queued in that routine */
   1421 		uint32_t ms_gone_by;
   1422 
   1423 		if ((net->last_sent_time.tv_sec > 0) ||
   1424 		    (net->last_sent_time.tv_usec > 0)) {
   1425 #ifdef __FreeBSD__
   1426 			struct timeval diff;
   1427 
   1428 			SCTP_GETTIME_TIMEVAL(&diff);
   1429 			timevalsub(&diff, &net->last_sent_time);
   1430 #else
   1431 			struct timeval diff, now;
   1432 
   1433 			SCTP_GETTIME_TIMEVAL(&now);
   1434 			timersub(&now, &net->last_sent_time, &diff);
   1435 #endif
   1436 			ms_gone_by = (uint32_t)(diff.tv_sec * 1000) +
   1437 			             (uint32_t)(diff.tv_usec / 1000);
   1438 		} else {
   1439 			ms_gone_by = 0xffffffff;
   1440 		}
   1441 		if ((ms_gone_by >= net->heart_beat_delay) ||
   1442 		    (net->dest_state & SCTP_ADDR_PF)) {
   1443 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
   1444 		}
   1445 	}
   1446 	return (0);
   1447 }
   1448 
   1449 void
   1450 sctp_pathmtu_timer(struct sctp_inpcb *inp,
   1451     struct sctp_tcb *stcb,
   1452     struct sctp_nets *net)
   1453 {
   1454 	uint32_t next_mtu, mtu;
   1455 
   1456 	next_mtu = sctp_get_next_mtu(net->mtu);
   1457 
   1458 	if ((next_mtu > net->mtu) && (net->port == 0)) {
   1459 		if ((net->src_addr_selected == 0) ||
   1460 		    (net->ro._s_addr == NULL) ||
   1461 		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
   1462 			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
   1463 				sctp_free_ifa(net->ro._s_addr);
   1464 				net->ro._s_addr = NULL;
   1465 				net->src_addr_selected = 0;
   1466 			} else  if (net->ro._s_addr == NULL) {
   1467 #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
   1468 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
   1469 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
   1470 					/* KAME hack: embed scopeid */
   1471 #if defined(__APPLE__)
   1472 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
   1473 					(void)in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL);
   1474 #else
   1475 					(void)in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL);
   1476 #endif
   1477 #elif defined(SCTP_KAME)
   1478 					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
   1479 #else
   1480                 			(void)in6_embedscope(&sin6->sin6_addr, sin6);
   1481 #endif
   1482 				}
   1483 #endif
   1484 
   1485 				net->ro._s_addr = sctp_source_address_selection(inp,
   1486 										stcb,
   1487 										(sctp_route_t *)&net->ro,
   1488 										net, 0, stcb->asoc.vrf_id);
   1489 #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
   1490 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
   1491 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
   1492 #ifdef SCTP_KAME
   1493 					(void)sa6_recoverscope(sin6);
   1494 #else
   1495 					(void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
   1496 #endif	/* SCTP_KAME */
   1497 				}
   1498 #endif	/* INET6 */
   1499 			}
   1500 			if (net->ro._s_addr)
   1501 				net->src_addr_selected = 1;
   1502 		}
   1503 		if (net->ro._s_addr) {
   1504 			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
   1505 			if (net->port) {
   1506 				mtu -= sizeof(struct udphdr);
   1507 			}
   1508 			if (mtu > next_mtu) {
   1509 				net->mtu = next_mtu;
   1510 			}
   1511 		}
   1512 	}
   1513 	/* restart the timer */
   1514 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
   1515 }
   1516 
   1517 void
   1518 sctp_autoclose_timer(struct sctp_inpcb *inp,
   1519     struct sctp_tcb *stcb,
   1520     struct sctp_nets *net)
   1521 {
   1522 	struct timeval tn, *tim_touse;
   1523 	struct sctp_association *asoc;
   1524 	int ticks_gone_by;
   1525 
   1526 	(void)SCTP_GETTIME_TIMEVAL(&tn);
   1527 	if (stcb->asoc.sctp_autoclose_ticks &&
   1528 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
   1529 		/* Auto close is on */
   1530 		asoc = &stcb->asoc;
   1531 		/* pick the time to use */
   1532 		if (asoc->time_last_rcvd.tv_sec >
   1533 		    asoc->time_last_sent.tv_sec) {
   1534 			tim_touse = &asoc->time_last_rcvd;
   1535 		} else {
   1536 			tim_touse = &asoc->time_last_sent;
   1537 		}
   1538 		/* Now has long enough transpired to autoclose? */
   1539 		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
   1540 		if ((ticks_gone_by > 0) &&
   1541 		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
   1542 			/*
   1543 			 * autoclose time has hit, call the output routine,
   1544 			 * which should do nothing just to be SURE we don't
   1545 			 * have hanging data. We can then safely check the
   1546 			 * queues and know that we are clear to send
   1547 			 * shutdown
   1548 			 */
   1549 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
   1550 			/* Are we clean? */
   1551 			if (TAILQ_EMPTY(&asoc->send_queue) &&
   1552 			    TAILQ_EMPTY(&asoc->sent_queue)) {
   1553 				/*
   1554 				 * there is nothing queued to send, so I'm
   1555 				 * done...
   1556 				 */
   1557 				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
   1558 					/* only send SHUTDOWN 1st time thru */
   1559 					struct sctp_nets *netp;
   1560 
   1561 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
   1562 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
   1563 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
   1564 					}
   1565 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
   1566 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
   1567 					sctp_stop_timers_for_shutdown(stcb);
   1568 					if (stcb->asoc.alternate) {
   1569 						netp = stcb->asoc.alternate;
   1570 					} else {
   1571 						netp = stcb->asoc.primary_destination;
   1572 					}
   1573 					sctp_send_shutdown(stcb, netp);
   1574 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
   1575 							 stcb->sctp_ep, stcb,
   1576 							 netp);
   1577 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
   1578 							 stcb->sctp_ep, stcb,
   1579 							 netp);
   1580 				}
   1581 			}
   1582 		} else {
   1583 			/*
   1584 			 * No auto close at this time, reset t-o to check
   1585 			 * later
   1586 			 */
   1587 			int tmp;
   1588 
   1589 			/* fool the timer startup to use the time left */
   1590 			tmp = asoc->sctp_autoclose_ticks;
   1591 			asoc->sctp_autoclose_ticks -= ticks_gone_by;
   1592 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
   1593 			    net);
   1594 			/* restore the real tick value */
   1595 			asoc->sctp_autoclose_ticks = tmp;
   1596 		}
   1597 	}
   1598 }
   1599 
   1600