netwerk/sctp/src/netinet/sctp_timer.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rwxr-xr-x

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial